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