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