1 /*
   2  * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciMethodData.hpp"
  27 #include "ci/ciReplay.hpp"
  28 #include "ci/ciSymbol.hpp"
  29 #include "ci/ciKlass.hpp"
  30 #include "ci/ciUtilities.inline.hpp"
  31 #include "classfile/javaClasses.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "compiler/compilationPolicy.hpp"
  35 #include "compiler/compileBroker.hpp"
  36 #include "compiler/compilerDefinitions.inline.hpp"
  37 #include "interpreter/linkResolver.hpp"
  38 #include "jvm.h"
  39 #include "memory/allocation.inline.hpp"
  40 #include "memory/oopFactory.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "oops/constantPool.inline.hpp"
  43 #include "oops/cpCache.inline.hpp"
  44 #include "oops/fieldStreams.inline.hpp"
  45 #include "oops/inlineKlass.inline.hpp"
  46 #include "oops/klass.inline.hpp"
  47 #include "oops/method.inline.hpp"
  48 #include "oops/oop.inline.hpp"
  49 #include "oops/resolvedIndyEntry.hpp"
  50 #include "prims/jvmtiExport.hpp"
  51 #include "prims/methodHandles.hpp"
  52 #include "runtime/fieldDescriptor.inline.hpp"
  53 #include "runtime/globals_extension.hpp"
  54 #include "runtime/handles.inline.hpp"
  55 #include "runtime/java.hpp"
  56 #include "runtime/jniHandles.inline.hpp"
  57 #include "runtime/threads.hpp"
  58 #include "utilities/copy.hpp"
  59 #include "utilities/macros.hpp"
  60 #include "utilities/utf8.hpp"
  61 
  62 // ciReplay
  63 
  64 typedef struct _ciMethodDataRecord {
  65   const char* _klass_name;
  66   const char* _method_name;
  67   const char* _signature;
  68 
  69   int _state;
  70   int _invocation_counter;
  71 
  72   intptr_t* _data;
  73   char*     _orig_data;
  74   Klass**   _classes;
  75   Method**  _methods;
  76   int*      _classes_offsets;
  77   int*      _methods_offsets;
  78   int       _data_length;
  79   int       _orig_data_length;
  80   int       _classes_length;
  81   int       _methods_length;
  82 } ciMethodDataRecord;
  83 
  84 typedef struct _ciMethodRecord {
  85   const char* _klass_name;
  86   const char* _method_name;
  87   const char* _signature;
  88 
  89   int _instructions_size;
  90   int _interpreter_invocation_count;
  91   int _interpreter_throwout_count;
  92   int _invocation_counter;
  93   int _backedge_counter;
  94 } ciMethodRecord;
  95 
  96 typedef struct _ciInstanceKlassRecord {
  97   const InstanceKlass* _klass;
  98   jobject _java_mirror; // Global handle to java mirror to prevent unloading
  99 } ciInstanceKlassRecord;
 100 
 101 typedef struct _ciInlineRecord {
 102   const char* _klass_name;
 103   const char* _method_name;
 104   const char* _signature;
 105 
 106   int _inline_depth;
 107   int _inline_bci;
 108   bool _inline_late;
 109 } ciInlineRecord;
 110 
 111 class  CompileReplay;
 112 static CompileReplay* replay_state;
 113 
 114 class CompileReplay : public StackObj {
 115  private:
 116   FILE*   _stream;
 117   Thread* _thread;
 118   Handle  _protection_domain;
 119   bool    _protection_domain_initialized;
 120   Handle  _loader;
 121   int     _version;
 122 
 123   GrowableArray<ciMethodRecord*>     _ci_method_records;
 124   GrowableArray<ciMethodDataRecord*> _ci_method_data_records;
 125   GrowableArray<ciInstanceKlassRecord*> _ci_instance_klass_records;
 126 
 127   // Use pointer because we may need to return inline records
 128   // without destroying them.
 129   GrowableArray<ciInlineRecord*>*    _ci_inline_records;
 130 
 131   const char* _error_message;
 132 
 133   char* _bufptr;
 134   char* _buffer;
 135   int   _buffer_length;
 136   ReallocMark _nesting; // Safety checks for arena reallocation
 137 
 138   // "compile" data
 139   ciKlass* _iklass;
 140   Method*  _imethod;
 141   int      _entry_bci;
 142   int      _comp_level;
 143 
 144  public:
 145   CompileReplay(const char* filename, TRAPS) {
 146     _thread = THREAD;
 147     _loader = Handle(_thread, SystemDictionary::java_system_loader());
 148     _protection_domain = Handle();
 149     _protection_domain_initialized = false;
 150 
 151     _stream = os::fopen(filename, "rt");
 152     if (_stream == nullptr) {
 153       fprintf(stderr, "ERROR: Can't open replay file %s\n", filename);
 154     }
 155 
 156     _ci_inline_records = nullptr;
 157     _error_message = nullptr;
 158 
 159     _buffer_length = 32;
 160     _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length);
 161     _bufptr = _buffer;
 162 
 163     _imethod = nullptr;
 164     _iklass  = nullptr;
 165     _entry_bci  = 0;
 166     _comp_level = 0;
 167     _version = 0;
 168 
 169     test();
 170   }
 171 
 172   ~CompileReplay() {
 173     if (_stream != nullptr) fclose(_stream);
 174   }
 175 
 176   void test() {
 177     strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
 178     _bufptr = _buffer;
 179     assert(parse_int("test") == 1, "what");
 180     assert(parse_int("test") == 2, "what");
 181     assert(strcmp(parse_string(), "foo") == 0, "what");
 182     assert(parse_int("test") == 4, "what");
 183     assert(strcmp(parse_string(), "bar") == 0, "what");
 184     assert(parse_intptr_t("test") == 9, "what");
 185     assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
 186   }
 187 
 188   bool had_error() {
 189     return _error_message != nullptr || _thread->has_pending_exception();
 190   }
 191 
 192   bool can_replay() {
 193     return !(_stream == nullptr || had_error());
 194   }
 195 
 196   void report_error(const char* msg) {
 197     _error_message = msg;
 198   }
 199 
 200   int parse_int(const char* label) {
 201     if (had_error()) {
 202       return 0;
 203     }
 204 
 205     int v = 0;
 206     int read;
 207     if (sscanf(_bufptr, "%i%n", &v, &read) != 1) {
 208       report_error(label);
 209     } else {
 210       _bufptr += read;
 211     }
 212     return v;
 213   }
 214 
 215   intptr_t parse_intptr_t(const char* label) {
 216     if (had_error()) {
 217       return 0;
 218     }
 219 
 220     intptr_t v = 0;
 221     int read;
 222     if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
 223       report_error(label);
 224     } else {
 225       _bufptr += read;
 226     }
 227     return v;
 228   }
 229 
 230   void skip_ws() {
 231     // Skip any leading whitespace
 232     while (*_bufptr == ' ' || *_bufptr == '\t') {
 233       _bufptr++;
 234     }
 235   }
 236 
 237   // Ignore the rest of the line
 238   void skip_remaining() {
 239     _bufptr = &_bufptr[strlen(_bufptr)]; // skip ahead to terminator
 240   }
 241 
 242   char* scan_and_terminate(char delim) {
 243     char* str = _bufptr;
 244     while (*_bufptr != delim && *_bufptr != '\0') {
 245       _bufptr++;
 246     }
 247     if (*_bufptr != '\0') {
 248       *_bufptr++ = '\0';
 249     }
 250     if (_bufptr == str) {
 251       // nothing here
 252       return nullptr;
 253     }
 254     return str;
 255   }
 256 
 257   char* parse_string() {
 258     if (had_error()) return nullptr;
 259 
 260     skip_ws();
 261     return scan_and_terminate(' ');
 262   }
 263 
 264   char* parse_quoted_string() {
 265     if (had_error()) return nullptr;
 266 
 267     skip_ws();
 268 
 269     if (*_bufptr == '"') {
 270       _bufptr++;
 271       return scan_and_terminate('"');
 272     } else {
 273       return scan_and_terminate(' ');
 274     }
 275   }
 276 
 277   char* parse_escaped_string() {
 278     char* result = parse_quoted_string();
 279     if (result != nullptr) {
 280       unescape_string(result);
 281     }
 282     return result;
 283   }
 284 
 285   // Look for the tag 'tag' followed by an
 286   bool parse_tag_and_count(const char* tag, int& length) {
 287     const char* t = parse_string();
 288     if (t == nullptr) {
 289       return false;
 290     }
 291 
 292     if (strcmp(tag, t) != 0) {
 293       report_error(tag);
 294       return false;
 295     }
 296     length = parse_int("parse_tag_and_count");
 297     return !had_error();
 298   }
 299 
 300   // Parse a sequence of raw data encoded as bytes and return the
 301   // resulting data.
 302   char* parse_data(const char* tag, int& length) {
 303     int read_size = 0;
 304     if (!parse_tag_and_count(tag, read_size)) {
 305       return nullptr;
 306     }
 307 
 308     int actual_size = sizeof(MethodData::CompilerCounters);
 309     char *result = NEW_RESOURCE_ARRAY(char, actual_size);
 310     int i = 0;
 311     if (read_size != actual_size) {
 312       tty->print_cr("Warning: ciMethodData parsing sees MethodData size %i in file, current is %i", read_size,
 313                     actual_size);
 314       // Replay serializes the entire MethodData, but the data is at the end.
 315       // If the MethodData instance size has changed, we can pad or truncate in the beginning
 316       int padding = actual_size - read_size;
 317       if (padding > 0) {
 318         // pad missing data with zeros
 319         tty->print_cr("- Padding MethodData");
 320         for (; i < padding; i++) {
 321           result[i] = 0;
 322         }
 323       } else if (padding < 0) {
 324         // drop some data
 325         tty->print_cr("- Truncating MethodData");
 326         for (int j = 0; j < -padding; j++) {
 327           int val = parse_int("data");
 328           // discard val
 329         }
 330       }
 331     }
 332 
 333     assert(i < actual_size, "At least some data must remain to be copied");
 334     for (; i < actual_size; i++) {
 335       int val = parse_int("data");
 336       result[i] = val;
 337     }
 338     length = actual_size;
 339     return result;
 340   }
 341 
 342   // Parse a standard chunk of data emitted as:
 343   //   'tag' <length> # # ...
 344   // Where each # is an intptr_t item
 345   intptr_t* parse_intptr_data(const char* tag, int& length) {
 346     if (!parse_tag_and_count(tag, length)) {
 347       return nullptr;
 348     }
 349 
 350     intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
 351     for (int i = 0; i < length; i++) {
 352       skip_ws();
 353       intptr_t val = parse_intptr_t("data");
 354       result[i] = val;
 355     }
 356     return result;
 357   }
 358 
 359   // Parse a possibly quoted version of a symbol into a symbolOop
 360   Symbol* parse_symbol() {
 361     const char* str = parse_escaped_string();
 362     if (str != nullptr) {
 363       Symbol* sym = SymbolTable::new_symbol(str);
 364       return sym;
 365     }
 366     return nullptr;
 367   }
 368 
 369   bool parse_terminator() {
 370     char* terminator = parse_string();
 371     if (terminator != nullptr && strcmp(terminator, ";") == 0) {
 372       return true;
 373     }
 374     return false;
 375   }
 376 
 377   // Parse a special hidden klass location syntax
 378   // syntax: @bci <klass> <name> <signature> <bci> <location>* ;
 379   // syntax: @cpi <klass> <cpi> <location>* ;
 380   Klass* parse_cp_ref(TRAPS) {
 381     JavaThread* thread = THREAD;
 382     oop obj = nullptr;
 383     char* ref = parse_string();
 384     if (strcmp(ref, "bci") == 0) {
 385       Method* m = parse_method(CHECK_NULL);
 386       if (m == nullptr) {
 387         return nullptr;
 388       }
 389 
 390       InstanceKlass* ik = m->method_holder();
 391       const constantPoolHandle cp(Thread::current(), ik->constants());
 392 
 393       // invokedynamic or invokehandle
 394 
 395       methodHandle caller(Thread::current(), m);
 396       int bci = parse_int("bci");
 397       if (m->validate_bci(bci) != bci) {
 398         report_error("bad bci");
 399         return nullptr;
 400       }
 401 
 402       ik->link_class(CHECK_NULL);
 403 
 404       Bytecode_invoke bytecode = Bytecode_invoke_check(caller, bci);
 405       if (!Bytecodes::is_defined(bytecode.code()) || !bytecode.is_valid()) {
 406         report_error("no invoke found at bci");
 407         return nullptr;
 408       }
 409       bytecode.verify();
 410       int index = bytecode.index();
 411 
 412       CallInfo callInfo;
 413       Bytecodes::Code bc = bytecode.invoke_code();
 414       LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, bc, CHECK_NULL);
 415 
 416       oop appendix = nullptr;
 417       Method* adapter_method = nullptr;
 418       int pool_index = 0;
 419 
 420       if (bytecode.is_invokedynamic()) {
 421         cp->cache()->set_dynamic_call(callInfo, index);
 422 
 423         appendix = cp->resolved_reference_from_indy(index);
 424         adapter_method = cp->resolved_indy_entry_at(index)->method();
 425         pool_index = cp->resolved_indy_entry_at(index)->constant_pool_index();
 426       } else if (bytecode.is_invokehandle()) {
 427 #ifdef ASSERT
 428         Klass* holder = cp->klass_ref_at(index, bytecode.code(), CHECK_NULL);
 429         Symbol* name = cp->name_ref_at(index, bytecode.code());
 430         assert(MethodHandles::is_signature_polymorphic_name(holder, name), "");
 431 #endif
 432         ResolvedMethodEntry* method_entry = cp->cache()->set_method_handle(index, callInfo);
 433         appendix = cp->cache()->appendix_if_resolved(method_entry);
 434         adapter_method = method_entry->method();
 435         pool_index = method_entry->constant_pool_index();
 436       } else {
 437         report_error("no dynamic invoke found");
 438         return nullptr;
 439       }
 440       char* dyno_ref = parse_string();
 441       if (strcmp(dyno_ref, "<appendix>") == 0) {
 442         obj = appendix;
 443       } else if (strcmp(dyno_ref, "<adapter>") == 0) {
 444         if (!parse_terminator()) {
 445           report_error("no dynamic invoke found");
 446           return nullptr;
 447         }
 448         Method* adapter = adapter_method;
 449         if (adapter == nullptr) {
 450           report_error("no adapter found");
 451           return nullptr;
 452         }
 453         return adapter->method_holder();
 454       } else if (strcmp(dyno_ref, "<bsm>") == 0) {
 455         BootstrapInfo bootstrap_specifier(cp, pool_index, index);
 456         obj = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), CHECK_NULL);
 457       } else {
 458         report_error("unrecognized token");
 459         return nullptr;
 460       }
 461     } else {
 462       // constant pool ref (MethodHandle)
 463       if (strcmp(ref, "cpi") != 0) {
 464         report_error("unexpected token");
 465         return nullptr;
 466       }
 467 
 468       Klass* k = parse_klass(CHECK_NULL);
 469       if (k == nullptr) {
 470         return nullptr;
 471       }
 472       InstanceKlass* ik = InstanceKlass::cast(k);
 473       const constantPoolHandle cp(Thread::current(), ik->constants());
 474 
 475       int cpi = parse_int("cpi");
 476 
 477       if (cpi >= cp->length()) {
 478         report_error("bad cpi");
 479         return nullptr;
 480       }
 481       if (!cp->tag_at(cpi).is_method_handle()) {
 482         report_error("no method handle found at cpi");
 483         return nullptr;
 484       }
 485       ik->link_class(CHECK_NULL);
 486       obj = cp->resolve_possibly_cached_constant_at(cpi, CHECK_NULL);
 487     }
 488     if (obj == nullptr) {
 489       report_error("null cp object found");
 490       return nullptr;
 491     }
 492     Klass* k = nullptr;
 493     skip_ws();
 494     // loop: read fields
 495     char* field = nullptr;
 496     do {
 497       field = parse_string();
 498       if (field == nullptr) {
 499         report_error("no field found");
 500         return nullptr;
 501       }
 502       if (strcmp(field, ";") == 0) {
 503         break;
 504       }
 505       // raw Method*
 506       if (strcmp(field, "<vmtarget>") == 0) {
 507         Method* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
 508         k = (vmtarget == nullptr) ? nullptr : vmtarget->method_holder();
 509         if (k == nullptr) {
 510           report_error("null vmtarget found");
 511           return nullptr;
 512         }
 513         if (!parse_terminator()) {
 514           report_error("missing terminator");
 515           return nullptr;
 516         }
 517         return k;
 518       }
 519       obj = ciReplay::obj_field(obj, field);
 520       // array
 521       if (obj != nullptr && obj->is_objArray()) {
 522         objArrayOop arr = (objArrayOop)obj;
 523         int index = parse_int("index");
 524         if (index >= arr->length()) {
 525           report_error("bad array index");
 526           return nullptr;
 527         }
 528         obj = arr->obj_at(index);
 529       }
 530     } while (obj != nullptr);
 531     if (obj == nullptr) {
 532       report_error("null field found");
 533       return nullptr;
 534     }
 535     k = obj->klass();
 536     return k;
 537   }
 538 
 539   // Parse a valid klass name and look it up
 540   // syntax: <name>
 541   // syntax: <constant pool ref>
 542   Klass* parse_klass(TRAPS) {
 543     skip_ws();
 544     // check for constant pool object reference (for a dynamic/hidden class)
 545     bool cp_ref = (*_bufptr == '@');
 546     if (cp_ref) {
 547       ++_bufptr;
 548       Klass* k = parse_cp_ref(CHECK_NULL);
 549       if (k != nullptr && !k->is_hidden()) {
 550         report_error("expected hidden class");
 551         return nullptr;
 552       }
 553       return k;
 554     }
 555     char* str = parse_escaped_string();
 556     Symbol* klass_name = SymbolTable::new_symbol(str);
 557     if (klass_name != nullptr) {
 558       Klass* k = nullptr;
 559       if (_iklass != nullptr) {
 560         k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding();
 561       } else {
 562         k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 563       }
 564       if (HAS_PENDING_EXCEPTION) {
 565         oop throwable = PENDING_EXCEPTION;
 566         java_lang_Throwable::print(throwable, tty);
 567         tty->cr();
 568         report_error(str);
 569         if (ReplayIgnoreInitErrors) {
 570           CLEAR_PENDING_EXCEPTION;
 571           _error_message = nullptr;
 572         }
 573         return nullptr;
 574       }
 575       return k;
 576     }
 577     return nullptr;
 578   }
 579 
 580   // Lookup a klass
 581   Klass* resolve_klass(const char* klass, TRAPS) {
 582     Symbol* klass_name = SymbolTable::new_symbol(klass);
 583     return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 584   }
 585 
 586   // Parse the standard tuple of <klass> <name> <signature>
 587   Method* parse_method(TRAPS) {
 588     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
 589     if (k == nullptr) {
 590       report_error("Can't find holder klass");
 591       return nullptr;
 592     }
 593     Symbol* method_name = parse_symbol();
 594     Symbol* method_signature = parse_symbol();
 595     Method* m = k->find_method(method_name, method_signature);
 596     if (m == nullptr) {
 597       report_error("Can't find method");
 598     }
 599     return m;
 600   }
 601 
 602   int get_line(int c) {
 603     int buffer_pos = 0;
 604     while(c != EOF) {
 605       if (buffer_pos + 1 >= _buffer_length) {
 606         _nesting.check(); // Check if a reallocation in the resource arena is safe
 607         int new_length = _buffer_length * 2;
 608         // Next call will throw error in case of OOM.
 609         _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length);
 610         _buffer_length = new_length;
 611       }
 612       if (c == '\n') {
 613         c = getc(_stream); // get next char
 614         break;
 615       } else if (c == '\r') {
 616         // skip LF
 617       } else {
 618         _buffer[buffer_pos++] = c;
 619       }
 620       c = getc(_stream);
 621     }
 622     // null terminate it, reset the pointer
 623     _buffer[buffer_pos] = '\0'; // NL or EOF
 624     _bufptr = _buffer;
 625     return c;
 626   }
 627 
 628   // Process each line of the replay file executing each command until
 629   // the file ends.
 630   void process(TRAPS) {
 631     int line_no = 1;
 632     int c = getc(_stream);
 633     while(c != EOF) {
 634       c = get_line(c);
 635       process_command(false, THREAD);
 636       if (had_error()) {
 637         int pos = _bufptr - _buffer + 1;
 638         tty->print_cr("Error while parsing line %d at position %d: %s\n", line_no, pos, _error_message);
 639         if (ReplayIgnoreInitErrors) {
 640           CLEAR_PENDING_EXCEPTION;
 641           _error_message = nullptr;
 642         } else {
 643           return;
 644         }
 645       }
 646       line_no++;
 647     }
 648     reset();
 649   }
 650 
 651   void process_command(bool is_replay_inline, TRAPS) {
 652     char* cmd = parse_string();
 653     if (cmd == nullptr) {
 654       return;
 655     }
 656     if (strcmp("#", cmd) == 0) {
 657       // comment line, print or ignore
 658       if (Verbose) {
 659         tty->print_cr("# %s", _bufptr);
 660       }
 661       skip_remaining();
 662     } else if (strcmp("version", cmd) == 0) {
 663       _version = parse_int("version");
 664       if (_version < 0 || _version > REPLAY_VERSION) {
 665         tty->print_cr("# unrecognized version %d, expected 0 <= version <= %d", _version, REPLAY_VERSION);
 666       }
 667     } else if (strcmp("compile", cmd) == 0) {
 668       process_compile(CHECK);
 669     } else if (!is_replay_inline) {
 670       if (strcmp("ciMethod", cmd) == 0) {
 671         process_ciMethod(CHECK);
 672       } else if (strcmp("ciMethodData", cmd) == 0) {
 673         process_ciMethodData(CHECK);
 674       } else if (strcmp("staticfield", cmd) == 0) {
 675         process_staticfield(CHECK);
 676       } else if (strcmp("ciInstanceKlass", cmd) == 0) {
 677         process_ciInstanceKlass(CHECK);
 678       } else if (strcmp("instanceKlass", cmd) == 0) {
 679         process_instanceKlass(CHECK);
 680 #if INCLUDE_JVMTI
 681       } else if (strcmp("JvmtiExport", cmd) == 0) {
 682         process_JvmtiExport(CHECK);
 683 #endif // INCLUDE_JVMTI
 684       } else {
 685         report_error("unknown command");
 686       }
 687     } else {
 688       report_error("unknown command");
 689     }
 690     if (!had_error() && *_bufptr != '\0') {
 691       report_error("line not properly terminated");
 692     }
 693   }
 694 
 695   // validation of comp_level
 696   bool is_valid_comp_level(int comp_level) {
 697     const int msg_len = 256;
 698     char* msg = nullptr;
 699     if (!is_compile(comp_level)) {
 700       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 701       jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
 702     } else if (is_c1_compile(comp_level) && !CompilerConfig::is_c1_enabled()) {
 703       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 704       jio_snprintf(msg, msg_len, "compilation level %d requires C1", comp_level);
 705     } else if (is_c2_compile(comp_level) && !CompilerConfig::is_c2_enabled()) {
 706       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 707       jio_snprintf(msg, msg_len, "compilation level %d requires C2", comp_level);
 708     }
 709     if (msg != nullptr) {
 710       report_error(msg);
 711       return false;
 712     }
 713     return true;
 714   }
 715 
 716   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <klass> <name> <signature>)*
 717   void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) {
 718     _imethod    = m;
 719     _iklass     = imethod->holder();
 720     _entry_bci  = entry_bci;
 721     _comp_level = comp_level;
 722     int line_no = 1;
 723     int c = getc(_stream);
 724     while(c != EOF) {
 725       c = get_line(c);
 726       process_command(true, CHECK_NULL);
 727       if (had_error()) {
 728         tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 729         tty->print_cr("%s", _buffer);
 730         return nullptr;
 731       }
 732       if (_ci_inline_records != nullptr && _ci_inline_records->length() > 0) {
 733         // Found inlining record for the requested method.
 734         return _ci_inline_records;
 735       }
 736       line_no++;
 737     }
 738     return nullptr;
 739   }
 740 
 741   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <inline_late> <klass> <name> <signature>)*
 742   void process_compile(TRAPS) {
 743     Method* method = parse_method(CHECK);
 744     if (had_error()) return;
 745     int entry_bci = parse_int("entry_bci");
 746     int comp_level = parse_int("comp_level");
 747     if (!is_valid_comp_level(comp_level)) {
 748       return;
 749     }
 750     if (_imethod != nullptr) {
 751       // Replay Inlining
 752       if (entry_bci != _entry_bci || comp_level != _comp_level) {
 753         return;
 754       }
 755       const char* iklass_name  = _imethod->method_holder()->name()->as_utf8();
 756       const char* imethod_name = _imethod->name()->as_utf8();
 757       const char* isignature   = _imethod->signature()->as_utf8();
 758       const char* klass_name   = method->method_holder()->name()->as_utf8();
 759       const char* method_name  = method->name()->as_utf8();
 760       const char* signature    = method->signature()->as_utf8();
 761       if (strcmp(iklass_name,  klass_name)  != 0 ||
 762           strcmp(imethod_name, method_name) != 0 ||
 763           strcmp(isignature,   signature)   != 0) {
 764         return;
 765       }
 766     }
 767     int inline_count = 0;
 768     if (parse_tag_and_count("inline", inline_count)) {
 769       // Record inlining data
 770       _ci_inline_records = new GrowableArray<ciInlineRecord*>();
 771       for (int i = 0; i < inline_count; i++) {
 772         int depth = parse_int("inline_depth");
 773         int bci = parse_int("inline_bci");
 774         if (had_error()) {
 775           break;
 776         }
 777         int inline_late = 0;
 778         if (_version >= 2) {
 779           inline_late = parse_int("inline_late");
 780           if (had_error()) {
 781               break;
 782           }
 783         }
 784 
 785         Method* inl_method = parse_method(CHECK);
 786         if (had_error()) {
 787           break;
 788         }
 789         new_ciInlineRecord(inl_method, bci, depth, inline_late);
 790       }
 791     }
 792     if (_imethod != nullptr) {
 793       return; // Replay Inlining
 794     }
 795     InstanceKlass* ik = method->method_holder();
 796     ik->initialize(THREAD);
 797     if (HAS_PENDING_EXCEPTION) {
 798       oop throwable = PENDING_EXCEPTION;
 799       java_lang_Throwable::print(throwable, tty);
 800       tty->cr();
 801       if (ReplayIgnoreInitErrors) {
 802         CLEAR_PENDING_EXCEPTION;
 803         ik->set_init_state(InstanceKlass::fully_initialized);
 804       } else {
 805         return;
 806       }
 807     }
 808     // Make sure the existence of a prior compile doesn't stop this one
 809     nmethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
 810     if (nm != nullptr) {
 811       nm->make_not_entrant();
 812     }
 813     replay_state = this;
 814     CompileBroker::compile_method(methodHandle(THREAD, method), entry_bci, comp_level,
 815                                   methodHandle(), 0, CompileTask::Reason_Replay, THREAD);
 816     replay_state = nullptr;
 817   }
 818 
 819   // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
 820   void process_ciMethod(TRAPS) {
 821     Method* method = parse_method(CHECK);
 822     if (had_error()) return;
 823     ciMethodRecord* rec = new_ciMethod(method);
 824     rec->_invocation_counter = parse_int("invocation_counter");
 825     rec->_backedge_counter = parse_int("backedge_counter");
 826     rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count");
 827     rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count");
 828     rec->_instructions_size = parse_int("instructions_size");
 829   }
 830 
 831   // ciMethodData <klass> <name> <signature> <state> <invocation_counter> orig <length> <byte>* data <length> <ptr>* oops <length> (<offset> <klass>)* methods <length> (<offset> <klass> <name> <signature>)*
 832   void process_ciMethodData(TRAPS) {
 833     Method* method = parse_method(CHECK);
 834     if (had_error()) return;
 835     /* just copied from Method, to build interpret data*/
 836 
 837     // To be properly initialized, some profiling in the MDO needs the
 838     // method to be rewritten (number of arguments at a call for instance)
 839     method->method_holder()->link_class(CHECK);
 840     assert(method->method_data() == nullptr, "Should only be initialized once");
 841     method->build_profiling_method_data(methodHandle(THREAD, method), CHECK);
 842 
 843     // collect and record all the needed information for later
 844     ciMethodDataRecord* rec = new_ciMethodData(method);
 845     rec->_state = parse_int("state");
 846     if (_version < 1) {
 847       parse_int("current_mileage");
 848     } else {
 849       rec->_invocation_counter = parse_int("invocation_counter");
 850     }
 851 
 852     rec->_orig_data = parse_data("orig", rec->_orig_data_length);
 853     if (rec->_orig_data == nullptr) {
 854       return;
 855     }
 856     rec->_data = parse_intptr_data("data", rec->_data_length);
 857     if (rec->_data == nullptr) {
 858       return;
 859     }
 860     if (!parse_tag_and_count("oops", rec->_classes_length)) {
 861       return;
 862     }
 863     rec->_classes = NEW_RESOURCE_ARRAY(Klass*, rec->_classes_length);
 864     rec->_classes_offsets = NEW_RESOURCE_ARRAY(int, rec->_classes_length);
 865     for (int i = 0; i < rec->_classes_length; i++) {
 866       int offset = parse_int("offset");
 867       if (had_error()) {
 868         return;
 869       }
 870       Klass* k = parse_klass(CHECK);
 871       rec->_classes_offsets[i] = offset;
 872       rec->_classes[i] = k;
 873     }
 874 
 875     if (!parse_tag_and_count("methods", rec->_methods_length)) {
 876       return;
 877     }
 878     rec->_methods = NEW_RESOURCE_ARRAY(Method*, rec->_methods_length);
 879     rec->_methods_offsets = NEW_RESOURCE_ARRAY(int, rec->_methods_length);
 880     for (int i = 0; i < rec->_methods_length; i++) {
 881       int offset = parse_int("offset");
 882       if (had_error()) {
 883         return;
 884       }
 885       Method* m = parse_method(CHECK);
 886       rec->_methods_offsets[i] = offset;
 887       rec->_methods[i] = m;
 888     }
 889   }
 890 
 891   // instanceKlass <name>
 892   // instanceKlass <constant pool ref> # <original hidden class name>
 893   //
 894   // Loads and initializes the klass 'name'.  This can be used to
 895   // create particular class loading environments
 896   void process_instanceKlass(TRAPS) {
 897     // just load the referenced class
 898     Klass* k = parse_klass(CHECK);
 899 
 900     if (_version >= 1) {
 901       if (!_protection_domain_initialized && k != nullptr) {
 902         assert(_protection_domain() == nullptr, "must be uninitialized");
 903         // The first entry is the holder class of the method for which a replay compilation is requested.
 904         // Use the same protection domain to load all subsequent classes in order to resolve all classes
 905         // in signatures of inlinees. This ensures that inlining can be done as stated in the replay file.
 906         _protection_domain = Handle(_thread, k->protection_domain());
 907       }
 908 
 909       _protection_domain_initialized = true;
 910     }
 911 
 912     if (k == nullptr) {
 913       return;
 914     }
 915     const char* comment = parse_string();
 916     bool is_comment = comment != nullptr && strcmp(comment, "#") == 0;
 917     if (k->is_hidden() != is_comment) {
 918       report_error("hidden class with comment expected");
 919       return;
 920     }
 921     // comment, print or ignore
 922     if (is_comment) {
 923       if (Verbose) {
 924         const char* hidden = parse_string();
 925         tty->print_cr("Found %s for %s", k->name()->as_quoted_ascii(), hidden);
 926       }
 927       skip_remaining();
 928     }
 929   }
 930 
 931   // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag*
 932   //
 933   // Load the klass 'name' and link or initialize it.  Verify that the
 934   // constant pool is the same length as 'length' and make sure the
 935   // constant pool tags are in the same state.
 936   void process_ciInstanceKlass(TRAPS) {
 937     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK);
 938     if (k == nullptr) {
 939       skip_remaining();
 940       return;
 941     }
 942     int is_linked = parse_int("is_linked");
 943     int is_initialized = parse_int("is_initialized");
 944     int length = parse_int("length");
 945     if (is_initialized) {
 946       k->initialize(THREAD);
 947       if (HAS_PENDING_EXCEPTION) {
 948         oop throwable = PENDING_EXCEPTION;
 949         java_lang_Throwable::print(throwable, tty);
 950         tty->cr();
 951         if (ReplayIgnoreInitErrors) {
 952           CLEAR_PENDING_EXCEPTION;
 953           k->set_init_state(InstanceKlass::fully_initialized);
 954         } else {
 955           return;
 956         }
 957       }
 958     } else if (is_linked) {
 959       k->link_class(CHECK);
 960     }
 961     new_ciInstanceKlass(k);
 962     ConstantPool* cp = k->constants();
 963     if (length != cp->length()) {
 964       report_error("constant pool length mismatch: wrong class files?");
 965       return;
 966     }
 967 
 968     int parsed_two_word = 0;
 969     for (int i = 1; i < length; i++) {
 970       int tag = parse_int("tag");
 971       if (had_error()) {
 972         return;
 973       }
 974       switch (cp->tag_at(i).value()) {
 975         case JVM_CONSTANT_UnresolvedClass: {
 976           if (tag == JVM_CONSTANT_Class) {
 977             tty->print_cr("Resolving klass %s at %d", cp->klass_name_at(i)->as_utf8(), i);
 978             Klass* k = cp->klass_at(i, CHECK);
 979           }
 980           break;
 981         }
 982 
 983         case JVM_CONSTANT_Long:
 984         case JVM_CONSTANT_Double:
 985           parsed_two_word = i + 1;
 986 
 987         case JVM_CONSTANT_ClassIndex:
 988         case JVM_CONSTANT_StringIndex:
 989         case JVM_CONSTANT_String:
 990         case JVM_CONSTANT_UnresolvedClassInError:
 991         case JVM_CONSTANT_Fieldref:
 992         case JVM_CONSTANT_Methodref:
 993         case JVM_CONSTANT_InterfaceMethodref:
 994         case JVM_CONSTANT_NameAndType:
 995         case JVM_CONSTANT_Utf8:
 996         case JVM_CONSTANT_Integer:
 997         case JVM_CONSTANT_Float:
 998         case JVM_CONSTANT_MethodHandle:
 999         case JVM_CONSTANT_MethodType:
1000         case JVM_CONSTANT_Dynamic:
1001         case JVM_CONSTANT_InvokeDynamic:
1002           if (tag != cp->tag_at(i).value()) {
1003             report_error("tag mismatch: wrong class files?");
1004             return;
1005           }
1006           break;
1007 
1008         case JVM_CONSTANT_Class:
1009           if (tag == JVM_CONSTANT_UnresolvedClass) {
1010             Klass* k = cp->klass_at(i, CHECK);
1011             tty->print_cr("Warning: entry was unresolved in the replay data: %s", k->name()->as_utf8());
1012           } else if (tag != JVM_CONSTANT_Class) {
1013             report_error("Unexpected tag");
1014             return;
1015           }
1016           break;
1017 
1018         case 0:
1019           if (parsed_two_word == i) continue;
1020 
1021         default:
1022           fatal("Unexpected tag: %d", cp->tag_at(i).value());
1023           break;
1024       }
1025 
1026     }
1027   }
1028 
1029   class InlineTypeFieldInitializer : public FieldClosure {
1030     oop _vt;
1031     CompileReplay* _replay;
1032   public:
1033     InlineTypeFieldInitializer(oop vt, CompileReplay* replay)
1034   : _vt(vt), _replay(replay) {}
1035 
1036     void do_field(fieldDescriptor* fd) {
1037       BasicType bt = fd->field_type();
1038       const char* string_value = fd->is_null_free_inline_type() ? nullptr : _replay->parse_escaped_string();
1039       switch (bt) {
1040       case T_BYTE: {
1041         int value = atoi(string_value);
1042         _vt->byte_field_put(fd->offset(), value);
1043         break;
1044       }
1045       case T_BOOLEAN: {
1046         int value = atoi(string_value);
1047         _vt->bool_field_put(fd->offset(), value);
1048         break;
1049       }
1050       case T_SHORT: {
1051         int value = atoi(string_value);
1052         _vt->short_field_put(fd->offset(), value);
1053         break;
1054       }
1055       case T_CHAR: {
1056         int value = atoi(string_value);
1057         _vt->char_field_put(fd->offset(), value);
1058         break;
1059       }
1060       case T_INT: {
1061         int value = atoi(string_value);
1062         _vt->int_field_put(fd->offset(), value);
1063         break;
1064       }
1065       case T_LONG: {
1066         jlong value;
1067         if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
1068           fprintf(stderr, "Error parsing long: %s\n", string_value);
1069           break;
1070         }
1071         _vt->long_field_put(fd->offset(), value);
1072         break;
1073       }
1074       case T_FLOAT: {
1075         float value = atof(string_value);
1076         _vt->float_field_put(fd->offset(), value);
1077         break;
1078       }
1079       case T_DOUBLE: {
1080         double value = atof(string_value);
1081         _vt->double_field_put(fd->offset(), value);
1082         break;
1083       }
1084       case T_ARRAY:
1085       case T_OBJECT:
1086         if (!fd->is_null_free_inline_type()) {
1087           JavaThread* THREAD = JavaThread::current();
1088           bool res = _replay->process_staticfield_reference(string_value, _vt, fd, THREAD);
1089           assert(res, "should succeed for arrays & objects");
1090           break;
1091         } else {
1092           InlineKlass* vk = InlineKlass::cast(fd->field_holder()->get_inline_type_field_klass(fd->index()));
1093           if (fd->is_flat()) {
1094             int field_offset = fd->offset() - vk->first_field_offset();
1095             oop obj = cast_to_oop(cast_from_oop<address>(_vt) + field_offset);
1096             InlineTypeFieldInitializer init_fields(obj, _replay);
1097             vk->do_nonstatic_fields(&init_fields);
1098           } else {
1099             oop value = vk->allocate_instance(JavaThread::current());
1100             _vt->obj_field_put(fd->offset(), value);
1101           }
1102           break;
1103         }
1104       default: {
1105         fatal("Unhandled type: %s", type2name(bt));
1106       }
1107       }
1108     }
1109   };
1110 
1111   bool process_staticfield_reference(const char* field_signature, oop java_mirror, fieldDescriptor* fd, TRAPS) {
1112     if (field_signature[0] == JVM_SIGNATURE_ARRAY) {
1113       int length = parse_int("array length");
1114       oop value = nullptr;
1115 
1116       if (length != -1) {
1117         if (field_signature[1] == JVM_SIGNATURE_ARRAY) {
1118           // multi dimensional array
1119           Klass* k = resolve_klass(field_signature, CHECK_(true));
1120           ArrayKlass* kelem = (ArrayKlass *)k;
1121           int rank = 0;
1122           while (field_signature[rank] == JVM_SIGNATURE_ARRAY) {
1123             rank++;
1124           }
1125           jint* dims = NEW_RESOURCE_ARRAY(jint, rank);
1126           dims[0] = length;
1127           for (int i = 1; i < rank; i++) {
1128             dims[i] = 1; // These aren't relevant to the compiler
1129           }
1130           value = kelem->multi_allocate(rank, dims, CHECK_(true));
1131         } else {
1132           if (strcmp(field_signature, "[B") == 0) {
1133             value = oopFactory::new_byteArray(length, CHECK_(true));
1134           } else if (strcmp(field_signature, "[Z") == 0) {
1135             value = oopFactory::new_boolArray(length, CHECK_(true));
1136           } else if (strcmp(field_signature, "[C") == 0) {
1137             value = oopFactory::new_charArray(length, CHECK_(true));
1138           } else if (strcmp(field_signature, "[S") == 0) {
1139             value = oopFactory::new_shortArray(length, CHECK_(true));
1140           } else if (strcmp(field_signature, "[F") == 0) {
1141             value = oopFactory::new_floatArray(length, CHECK_(true));
1142           } else if (strcmp(field_signature, "[D") == 0) {
1143             value = oopFactory::new_doubleArray(length, CHECK_(true));
1144           } else if (strcmp(field_signature, "[I") == 0) {
1145             value = oopFactory::new_intArray(length, CHECK_(true));
1146           } else if (strcmp(field_signature, "[J") == 0) {
1147             value = oopFactory::new_longArray(length, CHECK_(true));
1148           } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1149                      field_signature[1] == JVM_SIGNATURE_CLASS) {
1150             Klass* actual_array_klass = parse_klass(CHECK_(true));
1151             Klass* kelem = ObjArrayKlass::cast(actual_array_klass)->element_klass();
1152             value = oopFactory::new_objArray(kelem, length, CHECK_(true));
1153           } else if (field_signature[0] == JVM_SIGNATURE_ARRAY) {
1154             Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
1155             parse_klass(CHECK_(true)); // eat up the array class name
1156             value = oopFactory::new_valueArray(kelem, length, CHECK_(true));
1157           } else {
1158             report_error("unhandled array staticfield");
1159           }
1160         }
1161         java_mirror->obj_field_put(fd->offset(), value);
1162         return true;
1163       }
1164     } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
1165       const char* string_value = parse_escaped_string();
1166       Handle value = java_lang_String::create_from_str(string_value, CHECK_(true));
1167       java_mirror->obj_field_put(fd->offset(), value());
1168       return true;
1169     } else if (field_signature[0] == JVM_SIGNATURE_CLASS) {
1170       const char* instance = parse_escaped_string();
1171       oop value = nullptr;
1172       if (instance != nullptr) {
1173         Klass* k = resolve_klass(instance, CHECK_(true));
1174         value = InstanceKlass::cast(k)->allocate_instance(CHECK_(true));
1175       }
1176       java_mirror->obj_field_put(fd->offset(), value);
1177       return true;
1178     }
1179     return false;
1180   }
1181 
1182   // Initialize a class and fill in the value for a static field.
1183   // This is useful when the compile was dependent on the value of
1184   // static fields but it's impossible to properly rerun the static
1185   // initializer.
1186   void process_staticfield(TRAPS) {
1187     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
1188 
1189     if (k == nullptr || ReplaySuppressInitializers == 0 ||
1190         (ReplaySuppressInitializers == 2 && k->class_loader() == nullptr)) {
1191         skip_remaining();
1192       return;
1193     }
1194 
1195     assert(k->is_initialized(), "must be");
1196 
1197     const char* field_name = parse_escaped_string();
1198     const char* field_signature = parse_string();
1199     fieldDescriptor fd;
1200     Symbol* name = SymbolTable::new_symbol(field_name);
1201     Symbol* sig = SymbolTable::new_symbol(field_signature);
1202     if (!k->find_local_field(name, sig, &fd) ||
1203         !fd.is_static() ||
1204         fd.has_initial_value()) {
1205       report_error(field_name);
1206       return;
1207     }
1208 
1209     oop java_mirror = k->java_mirror();
1210     if (strcmp(field_signature, "I") == 0) {
1211       const char* string_value = parse_escaped_string();
1212       int value = atoi(string_value);
1213       java_mirror->int_field_put(fd.offset(), value);
1214     } else if (strcmp(field_signature, "B") == 0) {
1215       const char* string_value = parse_escaped_string();
1216       int value = atoi(string_value);
1217       java_mirror->byte_field_put(fd.offset(), value);
1218     } else if (strcmp(field_signature, "C") == 0) {
1219       const char* string_value = parse_escaped_string();
1220       int value = atoi(string_value);
1221       java_mirror->char_field_put(fd.offset(), value);
1222     } else if (strcmp(field_signature, "S") == 0) {
1223       const char* string_value = parse_escaped_string();
1224       int value = atoi(string_value);
1225       java_mirror->short_field_put(fd.offset(), value);
1226     } else if (strcmp(field_signature, "Z") == 0) {
1227       const char* string_value = parse_escaped_string();
1228       int value = atoi(string_value);
1229       java_mirror->bool_field_put(fd.offset(), value);
1230     } else if (strcmp(field_signature, "J") == 0) {
1231       const char* string_value = parse_escaped_string();
1232       jlong value;
1233       if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
1234         fprintf(stderr, "Error parsing long: %s\n", string_value);
1235         return;
1236       }
1237       java_mirror->long_field_put(fd.offset(), value);
1238     } else if (strcmp(field_signature, "F") == 0) {
1239       const char* string_value = parse_escaped_string();
1240       float value = atof(string_value);
1241       java_mirror->float_field_put(fd.offset(), value);
1242     } else if (strcmp(field_signature, "D") == 0) {
1243       const char* string_value = parse_escaped_string();
1244       double value = atof(string_value);
1245       java_mirror->double_field_put(fd.offset(), value);
1246     } else if (fd.is_null_free_inline_type()) {
1247       Klass* kelem = resolve_klass(field_signature, CHECK);
1248       InlineKlass* vk = InlineKlass::cast(kelem);
1249       oop value = vk->allocate_instance(CHECK);
1250       InlineTypeFieldInitializer init_fields(value, this);
1251       vk->do_nonstatic_fields(&init_fields);
1252       java_mirror->obj_field_put(fd.offset(), value);
1253     } else {
1254       bool res = process_staticfield_reference(field_signature, java_mirror, &fd, CHECK);
1255       if (!res)  {
1256         report_error("unhandled staticfield");
1257       }
1258     }
1259   }
1260 
1261 #if INCLUDE_JVMTI
1262   // JvmtiExport <field> <value>
1263   void process_JvmtiExport(TRAPS) {
1264     const char* field = parse_string();
1265     bool value = parse_int("JvmtiExport flag") != 0;
1266     if (strcmp(field, "can_access_local_variables") == 0) {
1267       JvmtiExport::set_can_access_local_variables(value);
1268     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
1269       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
1270     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
1271       JvmtiExport::set_can_post_on_exceptions(value);
1272     } else {
1273       report_error("Unrecognized JvmtiExport directive");
1274     }
1275   }
1276 #endif // INCLUDE_JVMTI
1277 
1278   // Create and initialize a record for a ciMethod
1279   ciMethodRecord* new_ciMethod(Method* method) {
1280     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
1281     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1282     rec->_method_name = method->name()->as_utf8();
1283     rec->_signature = method->signature()->as_utf8();
1284     _ci_method_records.append(rec);
1285     return rec;
1286   }
1287 
1288   // Lookup data for a ciMethod
1289   ciMethodRecord* find_ciMethodRecord(Method* method) {
1290     const char* klass_name =  method->method_holder()->name()->as_utf8();
1291     const char* method_name = method->name()->as_utf8();
1292     const char* signature = method->signature()->as_utf8();
1293     for (int i = 0; i < _ci_method_records.length(); i++) {
1294       ciMethodRecord* rec = _ci_method_records.at(i);
1295       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1296           strcmp(rec->_method_name, method_name) == 0 &&
1297           strcmp(rec->_signature, signature) == 0) {
1298         return rec;
1299       }
1300     }
1301     return nullptr;
1302   }
1303 
1304   // Create and initialize a record for a ciInstanceKlass which was present at replay dump time.
1305   void new_ciInstanceKlass(const InstanceKlass* klass) {
1306     ciInstanceKlassRecord* rec = NEW_RESOURCE_OBJ(ciInstanceKlassRecord);
1307     rec->_klass = klass;
1308     oop java_mirror = klass->java_mirror();
1309     Handle h_java_mirror(_thread, java_mirror);
1310     rec->_java_mirror = JNIHandles::make_global(h_java_mirror);
1311     _ci_instance_klass_records.append(rec);
1312   }
1313 
1314   // Check if a ciInstanceKlass was present at replay dump time for a klass.
1315   ciInstanceKlassRecord* find_ciInstanceKlass(const InstanceKlass* klass) {
1316     for (int i = 0; i < _ci_instance_klass_records.length(); i++) {
1317       ciInstanceKlassRecord* rec = _ci_instance_klass_records.at(i);
1318       if (klass == rec->_klass) {
1319         // ciInstanceKlass for this klass was resolved.
1320         return rec;
1321       }
1322     }
1323     return nullptr;
1324   }
1325 
1326   // Create and initialize a record for a ciMethodData
1327   ciMethodDataRecord* new_ciMethodData(Method* method) {
1328     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
1329     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1330     rec->_method_name = method->name()->as_utf8();
1331     rec->_signature = method->signature()->as_utf8();
1332     _ci_method_data_records.append(rec);
1333     return rec;
1334   }
1335 
1336   // Lookup data for a ciMethodData
1337   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
1338     const char* klass_name =  method->method_holder()->name()->as_utf8();
1339     const char* method_name = method->name()->as_utf8();
1340     const char* signature = method->signature()->as_utf8();
1341     for (int i = 0; i < _ci_method_data_records.length(); i++) {
1342       ciMethodDataRecord* rec = _ci_method_data_records.at(i);
1343       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1344           strcmp(rec->_method_name, method_name) == 0 &&
1345           strcmp(rec->_signature, signature) == 0) {
1346         return rec;
1347       }
1348     }
1349     return nullptr;
1350   }
1351 
1352   // Create and initialize a record for a ciInlineRecord
1353   ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth, int inline_late) {
1354     ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
1355     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1356     rec->_method_name = method->name()->as_utf8();
1357     rec->_signature = method->signature()->as_utf8();
1358     rec->_inline_bci = bci;
1359     rec->_inline_depth = depth;
1360     rec->_inline_late = inline_late;
1361     _ci_inline_records->append(rec);
1362     return rec;
1363   }
1364 
1365   // Lookup inlining data for a ciMethod
1366   ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
1367     if (_ci_inline_records != nullptr) {
1368       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1369     }
1370     return nullptr;
1371   }
1372 
1373   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1374                                       Method* method, int bci, int depth) {
1375     if (records != nullptr) {
1376       const char* klass_name  = method->method_holder()->name()->as_utf8();
1377       const char* method_name = method->name()->as_utf8();
1378       const char* signature   = method->signature()->as_utf8();
1379       for (int i = 0; i < records->length(); i++) {
1380         ciInlineRecord* rec = records->at(i);
1381         if ((rec->_inline_bci == bci) &&
1382             (rec->_inline_depth == depth) &&
1383             (strcmp(rec->_klass_name, klass_name) == 0) &&
1384             (strcmp(rec->_method_name, method_name) == 0) &&
1385             (strcmp(rec->_signature, signature) == 0)) {
1386           return rec;
1387         }
1388       }
1389     }
1390     return nullptr;
1391   }
1392 
1393   const char* error_message() {
1394     return _error_message;
1395   }
1396 
1397   void reset() {
1398     _error_message = nullptr;
1399     _ci_method_records.clear();
1400     _ci_method_data_records.clear();
1401   }
1402 
1403   // Take an ascii string contain \u#### escapes and convert it to utf8
1404   // in place.
1405   static void unescape_string(char* value) {
1406     char* from = value;
1407     char* to = value;
1408     while (*from != '\0') {
1409       if (*from != '\\') {
1410         *from++ = *to++;
1411       } else {
1412         switch (from[1]) {
1413           case 'u': {
1414             from += 2;
1415             jchar value=0;
1416             for (int i=0; i<4; i++) {
1417               char c = *from++;
1418               switch (c) {
1419                 case '0': case '1': case '2': case '3': case '4':
1420                 case '5': case '6': case '7': case '8': case '9':
1421                   value = (value << 4) + c - '0';
1422                   break;
1423                 case 'a': case 'b': case 'c':
1424                 case 'd': case 'e': case 'f':
1425                   value = (value << 4) + 10 + c - 'a';
1426                   break;
1427                 case 'A': case 'B': case 'C':
1428                 case 'D': case 'E': case 'F':
1429                   value = (value << 4) + 10 + c - 'A';
1430                   break;
1431                 default:
1432                   ShouldNotReachHere();
1433               }
1434             }
1435             UNICODE::convert_to_utf8(&value, 1, to);
1436             to++;
1437             break;
1438           }
1439           case 't': *to++ = '\t'; from += 2; break;
1440           case 'n': *to++ = '\n'; from += 2; break;
1441           case 'r': *to++ = '\r'; from += 2; break;
1442           case 'f': *to++ = '\f'; from += 2; break;
1443           default:
1444             ShouldNotReachHere();
1445         }
1446       }
1447     }
1448     *from = *to;
1449   }
1450 };
1451 
1452 void ciReplay::replay(TRAPS) {
1453   int exit_code = replay_impl(THREAD);
1454 
1455   Threads::destroy_vm();
1456 
1457   vm_exit(exit_code);
1458 }
1459 
1460 bool ciReplay::no_replay_state() {
1461   return replay_state == nullptr;
1462 }
1463 
1464 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1465   if (FLAG_IS_DEFAULT(InlineDataFile)) {
1466     tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1467     return nullptr;
1468   }
1469 
1470   VM_ENTRY_MARK;
1471   // Load and parse the replay data
1472   CompileReplay rp(InlineDataFile, THREAD);
1473   if (!rp.can_replay()) {
1474     tty->print_cr("ciReplay: !rp.can_replay()");
1475     return nullptr;
1476   }
1477   void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1478   if (HAS_PENDING_EXCEPTION) {
1479     Handle throwable(THREAD, PENDING_EXCEPTION);
1480     CLEAR_PENDING_EXCEPTION;
1481     java_lang_Throwable::print_stack_trace(throwable, tty);
1482     tty->cr();
1483     return nullptr;
1484   }
1485 
1486   if (rp.had_error()) {
1487     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1488     return nullptr;
1489   }
1490   return data;
1491 }
1492 
1493 int ciReplay::replay_impl(TRAPS) {
1494   HandleMark hm(THREAD);
1495   ResourceMark rm(THREAD);
1496 
1497   if (ReplaySuppressInitializers > 2) {
1498     // ReplaySuppressInitializers > 2 means that we want to allow
1499     // normal VM bootstrap but once we get into the replay itself
1500     // don't allow any initializers to be run.
1501     ReplaySuppressInitializers = 1;
1502   }
1503 
1504   if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1505     tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1506     return 1;
1507   }
1508 
1509   // Load and parse the replay data
1510   CompileReplay rp(ReplayDataFile, THREAD);
1511   int exit_code = 0;
1512   if (rp.can_replay()) {
1513     rp.process(THREAD);
1514   } else {
1515     exit_code = 1;
1516     return exit_code;
1517   }
1518 
1519   if (HAS_PENDING_EXCEPTION) {
1520     Handle throwable(THREAD, PENDING_EXCEPTION);
1521     CLEAR_PENDING_EXCEPTION;
1522     java_lang_Throwable::print_stack_trace(throwable, tty);
1523     tty->cr();
1524     exit_code = 2;
1525   }
1526 
1527   if (rp.had_error()) {
1528     tty->print_cr("Failed on %s", rp.error_message());
1529     exit_code = 1;
1530   }
1531   return exit_code;
1532 }
1533 
1534 void ciReplay::initialize(ciMethodData* m) {
1535   if (no_replay_state()) {
1536     return;
1537   }
1538 
1539   ASSERT_IN_VM;
1540   ResourceMark rm;
1541 
1542   Method* method = m->get_MethodData()->method();
1543   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1544   if (rec == nullptr) {
1545     // This indicates some mismatch with the original environment and
1546     // the replay environment though it's not always enough to
1547     // interfere with reproducing a bug
1548     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1549     method->print_name(tty);
1550     tty->cr();
1551   } else {
1552     m->_state = rec->_state;
1553     m->_invocation_counter = rec->_invocation_counter;
1554     if (rec->_data_length != 0) {
1555       assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) ||
1556              m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1557 
1558       // Write the correct ciObjects back into the profile data
1559       ciEnv* env = ciEnv::current();
1560       for (int i = 0; i < rec->_classes_length; i++) {
1561         Klass *k = rec->_classes[i];
1562         // In case this class pointer is is tagged, preserve the tag bits
1563         intptr_t status = 0;
1564         if (k != nullptr) {
1565           status = ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]);
1566         }
1567         rec->_data[rec->_classes_offsets[i]] = status;
1568       }
1569       for (int i = 0; i < rec->_methods_length; i++) {
1570         Method *m = rec->_methods[i];
1571         *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) =
1572           env->get_metadata(m);
1573       }
1574       // Copy the updated profile data into place as intptr_ts
1575 #ifdef _LP64
1576       Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1577 #else
1578       Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1579 #endif
1580     }
1581 
1582     // copy in the original header
1583     Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1584   }
1585 }
1586 
1587 
1588 bool ciReplay::should_not_inline(ciMethod* method) {
1589   if (no_replay_state()) {
1590     return false;
1591   }
1592   VM_ENTRY_MARK;
1593   // ciMethod without a record shouldn't be inlined.
1594   return replay_state->find_ciMethodRecord(method->get_Method()) == nullptr;
1595 }
1596 
1597 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth, bool& should_delay) {
1598   if (data != nullptr) {
1599     GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1600     VM_ENTRY_MARK;
1601     // Inline record are ordered by bci and depth.
1602     ciInlineRecord* record = CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth);
1603     if (record == nullptr) {
1604       return false;
1605     }
1606     should_delay = record->_inline_late;
1607     return true;
1608   } else if (replay_state != nullptr) {
1609     VM_ENTRY_MARK;
1610     // Inline record are ordered by bci and depth.
1611     ciInlineRecord* record = replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth);
1612     if (record == nullptr) {
1613       return false;
1614     }
1615     should_delay = record->_inline_late;
1616     return true;
1617   }
1618   return false;
1619 }
1620 
1621 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1622   if (data != nullptr) {
1623     GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1624     VM_ENTRY_MARK;
1625     // Inline record are ordered by bci and depth.
1626     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == nullptr;
1627   } else if (replay_state != nullptr) {
1628     VM_ENTRY_MARK;
1629     // Inline record are ordered by bci and depth.
1630     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == nullptr;
1631   }
1632   return false;
1633 }
1634 
1635 void ciReplay::initialize(ciMethod* m) {
1636   if (no_replay_state()) {
1637     return;
1638   }
1639 
1640   ASSERT_IN_VM;
1641   ResourceMark rm;
1642 
1643   Method* method = m->get_Method();
1644   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1645   if (rec == nullptr) {
1646     // This indicates some mismatch with the original environment and
1647     // the replay environment though it's not always enough to
1648     // interfere with reproducing a bug
1649     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1650     method->print_name(tty);
1651     tty->cr();
1652   } else {
1653     EXCEPTION_CONTEXT;
1654     // m->_instructions_size = rec->_instructions_size;
1655     m->_inline_instructions_size = -1;
1656     m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1657     m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1658     MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1659     guarantee(mcs != nullptr, "method counters allocation failed");
1660     mcs->invocation_counter()->_counter = rec->_invocation_counter;
1661     mcs->backedge_counter()->_counter = rec->_backedge_counter;
1662   }
1663 }
1664 
1665 void ciReplay::initialize(ciInstanceKlass* ci_ik, InstanceKlass* ik) {
1666   assert(!no_replay_state(), "must have replay state");
1667 
1668   ASSERT_IN_VM;
1669   ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(ik);
1670   assert(rec != nullptr, "ciInstanceKlass must be whitelisted");
1671   ci_ik->_java_mirror = CURRENT_ENV->get_instance(JNIHandles::resolve(rec->_java_mirror));
1672 }
1673 
1674 bool ciReplay::is_loaded(Method* method) {
1675   if (no_replay_state()) {
1676     return true;
1677   }
1678 
1679   ASSERT_IN_VM;
1680   ResourceMark rm;
1681 
1682   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1683   return rec != nullptr;
1684 }
1685 
1686 bool ciReplay::is_klass_unresolved(const InstanceKlass* klass) {
1687   if (no_replay_state()) {
1688     return false;
1689   }
1690 
1691   // Check if klass is found on whitelist.
1692   ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(klass);
1693   return rec == nullptr;
1694 }
1695 
1696 oop ciReplay::obj_field(oop obj, Symbol* name) {
1697   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
1698 
1699   do {
1700     if (!ik->has_nonstatic_fields()) {
1701       ik = ik->java_super();
1702       continue;
1703     }
1704 
1705     for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
1706       if (fs.access_flags().is_static()) {
1707         continue;
1708       }
1709       if (fs.name() == name) {
1710         int offset = fs.offset();
1711 #ifdef ASSERT
1712         fieldDescriptor fd = fs.field_descriptor();
1713         assert(fd.offset() == ik->field_offset(fd.index()), "!");
1714 #endif
1715         oop f = obj->obj_field(offset);
1716         return f;
1717       }
1718     }
1719 
1720     ik = ik->java_super();
1721   } while (ik != nullptr);
1722   return nullptr;
1723 }
1724 
1725 oop ciReplay::obj_field(oop obj, const char *name) {
1726   Symbol* fname = SymbolTable::probe(name, (int)strlen(name));
1727   if (fname == nullptr) {
1728     return nullptr;
1729   }
1730   return obj_field(obj, fname);
1731 }