1 /*
   2  * Copyright (c) 2013, 2022, 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 "jvm.h"
  27 #include "ci/ciMethodData.hpp"
  28 #include "ci/ciReplay.hpp"
  29 #include "ci/ciSymbol.hpp"
  30 #include "ci/ciKlass.hpp"
  31 #include "ci/ciUtilities.inline.hpp"
  32 #include "classfile/javaClasses.hpp"
  33 #include "classfile/symbolTable.hpp"
  34 #include "classfile/systemDictionary.hpp"
  35 #include "compiler/compilationPolicy.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "interpreter/linkResolver.hpp"
  38 #include "memory/allocation.inline.hpp"
  39 #include "memory/oopFactory.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/constantPool.hpp"
  42 #include "oops/cpCache.inline.hpp"
  43 #include "oops/fieldStreams.inline.hpp"
  44 #include "oops/inlineKlass.inline.hpp"
  45 #include "oops/klass.inline.hpp"
  46 #include "oops/method.inline.hpp"
  47 #include "oops/oop.inline.hpp"
  48 #include "prims/jvmtiExport.hpp"
  49 #include "prims/methodHandles.hpp"
  50 #include "runtime/fieldDescriptor.inline.hpp"
  51 #include "runtime/globals_extension.hpp"
  52 #include "runtime/handles.inline.hpp"
  53 #include "runtime/jniHandles.inline.hpp"
  54 #include "runtime/java.hpp"
  55 #include "utilities/copy.hpp"
  56 #include "utilities/macros.hpp"
  57 #include "utilities/utf8.hpp"
  58 
  59 #ifndef PRODUCT
  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 
 136   // "compile" data
 137   ciKlass* _iklass;
 138   Method*  _imethod;
 139   int      _entry_bci;
 140   int      _comp_level;
 141 
 142  public:
 143   CompileReplay(const char* filename, TRAPS) {
 144     _thread = THREAD;
 145     _loader = Handle(_thread, SystemDictionary::java_system_loader());
 146     _protection_domain = Handle();
 147     _protection_domain_initialized = false;
 148 
 149     _stream = os::fopen(filename, "rt");
 150     if (_stream == NULL) {
 151       fprintf(stderr, "ERROR: Can't open replay file %s\n", filename);
 152     }
 153 
 154     _ci_inline_records = NULL;
 155     _error_message = NULL;
 156 
 157     _buffer_length = 32;
 158     _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length);
 159     _bufptr = _buffer;
 160 
 161     _imethod = NULL;
 162     _iklass  = NULL;
 163     _entry_bci  = 0;
 164     _comp_level = 0;
 165     _version = 0;
 166 
 167     test();
 168   }
 169 
 170   ~CompileReplay() {
 171     if (_stream != NULL) fclose(_stream);
 172   }
 173 
 174   void test() {
 175     strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
 176     _bufptr = _buffer;
 177     assert(parse_int("test") == 1, "what");
 178     assert(parse_int("test") == 2, "what");
 179     assert(strcmp(parse_string(), "foo") == 0, "what");
 180     assert(parse_int("test") == 4, "what");
 181     assert(strcmp(parse_string(), "bar") == 0, "what");
 182     assert(parse_intptr_t("test") == 9, "what");
 183     assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
 184   }
 185 
 186   bool had_error() {
 187     return _error_message != NULL || _thread->has_pending_exception();
 188   }
 189 
 190   bool can_replay() {
 191     return !(_stream == NULL || had_error());
 192   }
 193 
 194   void report_error(const char* msg) {
 195     _error_message = msg;
 196   }
 197 
 198   int parse_int(const char* label) {
 199     if (had_error()) {
 200       return 0;
 201     }
 202 
 203     int v = 0;
 204     int read;
 205     if (sscanf(_bufptr, "%i%n", &v, &read) != 1) {
 206       report_error(label);
 207     } else {
 208       _bufptr += read;
 209     }
 210     return v;
 211   }
 212 
 213   intptr_t parse_intptr_t(const char* label) {
 214     if (had_error()) {
 215       return 0;
 216     }
 217 
 218     intptr_t v = 0;
 219     int read;
 220     if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
 221       report_error(label);
 222     } else {
 223       _bufptr += read;
 224     }
 225     return v;
 226   }
 227 
 228   void skip_ws() {
 229     // Skip any leading whitespace
 230     while (*_bufptr == ' ' || *_bufptr == '\t') {
 231       _bufptr++;
 232     }
 233   }
 234 
 235   // Ignore the rest of the line
 236   void skip_remaining() {
 237     _bufptr = &_bufptr[strlen(_bufptr)]; // skip ahead to terminator
 238   }
 239 
 240   char* scan_and_terminate(char delim) {
 241     char* str = _bufptr;
 242     while (*_bufptr != delim && *_bufptr != '\0') {
 243       _bufptr++;
 244     }
 245     if (*_bufptr != '\0') {
 246       *_bufptr++ = '\0';
 247     }
 248     if (_bufptr == str) {
 249       // nothing here
 250       return NULL;
 251     }
 252     return str;
 253   }
 254 
 255   char* parse_string() {
 256     if (had_error()) return NULL;
 257 
 258     skip_ws();
 259     return scan_and_terminate(' ');
 260   }
 261 
 262   char* parse_quoted_string() {
 263     if (had_error()) return NULL;
 264 
 265     skip_ws();
 266 
 267     if (*_bufptr == '"') {
 268       _bufptr++;
 269       return scan_and_terminate('"');
 270     } else {
 271       return scan_and_terminate(' ');
 272     }
 273   }
 274 
 275   char* parse_escaped_string() {
 276     char* result = parse_quoted_string();
 277     if (result != NULL) {
 278       unescape_string(result);
 279     }
 280     return result;
 281   }
 282 
 283   // Look for the tag 'tag' followed by an
 284   bool parse_tag_and_count(const char* tag, int& length) {
 285     const char* t = parse_string();
 286     if (t == NULL) {
 287       return false;
 288     }
 289 
 290     if (strcmp(tag, t) != 0) {
 291       report_error(tag);
 292       return false;
 293     }
 294     length = parse_int("parse_tag_and_count");
 295     return !had_error();
 296   }
 297 
 298   // Parse a sequence of raw data encoded as bytes and return the
 299   // resulting data.
 300   char* parse_data(const char* tag, int& length) {
 301     int read_size = 0;
 302     if (!parse_tag_and_count(tag, read_size)) {
 303       return NULL;
 304     }
 305 
 306     int actual_size = sizeof(MethodData::CompilerCounters);
 307     char *result = NEW_RESOURCE_ARRAY(char, actual_size);
 308     int i = 0;
 309     if (read_size != actual_size) {
 310       tty->print_cr("Warning: ciMethodData parsing sees MethodData size %i in file, current is %i", read_size,
 311                     actual_size);
 312       // Replay serializes the entire MethodData, but the data is at the end.
 313       // If the MethodData instance size has changed, we can pad or truncate in the beginning
 314       int padding = actual_size - read_size;
 315       if (padding > 0) {
 316         // pad missing data with zeros
 317         tty->print_cr("- Padding MethodData");
 318         for (; i < padding; i++) {
 319           result[i] = 0;
 320         }
 321       } else if (padding < 0) {
 322         // drop some data
 323         tty->print_cr("- Truncating MethodData");
 324         for (int j = 0; j < -padding; j++) {
 325           int val = parse_int("data");
 326           // discard val
 327         }
 328       }
 329     }
 330 
 331     assert(i < actual_size, "At least some data must remain to be copied");
 332     for (; i < actual_size; i++) {
 333       int val = parse_int("data");
 334       result[i] = val;
 335     }
 336     length = actual_size;
 337     return result;
 338   }
 339 
 340   // Parse a standard chunk of data emitted as:
 341   //   'tag' <length> # # ...
 342   // Where each # is an intptr_t item
 343   intptr_t* parse_intptr_data(const char* tag, int& length) {
 344     if (!parse_tag_and_count(tag, length)) {
 345       return NULL;
 346     }
 347 
 348     intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
 349     for (int i = 0; i < length; i++) {
 350       skip_ws();
 351       intptr_t val = parse_intptr_t("data");
 352       result[i] = val;
 353     }
 354     return result;
 355   }
 356 
 357   // Parse a possibly quoted version of a symbol into a symbolOop
 358   Symbol* parse_symbol() {
 359     const char* str = parse_escaped_string();
 360     if (str != NULL) {
 361       Symbol* sym = SymbolTable::new_symbol(str);
 362       return sym;
 363     }
 364     return NULL;
 365   }
 366 
 367   bool parse_terminator() {
 368     char* terminator = parse_string();
 369     if (terminator != NULL && strcmp(terminator, ";") == 0) {
 370       return true;
 371     }
 372     return false;
 373   }
 374 
 375   // Parse a special hidden klass location syntax
 376   // syntax: @bci <klass> <name> <signature> <bci> <location>* ;
 377   // syntax: @cpi <klass> <cpi> <location>* ;
 378   Klass* parse_cp_ref(TRAPS) {
 379     JavaThread* thread = THREAD;
 380     oop obj = NULL;
 381     char* ref = parse_string();
 382     if (strcmp(ref, "bci") == 0) {
 383       Method* m = parse_method(CHECK_NULL);
 384       if (m == NULL) {
 385         return NULL;
 386       }
 387 
 388       InstanceKlass* ik = m->method_holder();
 389       const constantPoolHandle cp(Thread::current(), ik->constants());
 390 
 391       // invokedynamic or invokehandle
 392 
 393       methodHandle caller(Thread::current(), m);
 394       int bci = parse_int("bci");
 395       if (m->validate_bci(bci) != bci) {
 396         report_error("bad bci");
 397         return NULL;
 398       }
 399 
 400       ik->link_class(CHECK_NULL);
 401 
 402       Bytecode_invoke bytecode(caller, bci);
 403       int index = bytecode.index();
 404 
 405       ConstantPoolCacheEntry* cp_cache_entry = NULL;
 406       CallInfo callInfo;
 407       Bytecodes::Code bc = bytecode.invoke_code();
 408       LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, bc, CHECK_NULL);
 409       if (bytecode.is_invokedynamic()) {
 410         cp_cache_entry = cp->invokedynamic_cp_cache_entry_at(index);
 411         cp_cache_entry->set_dynamic_call(cp, callInfo);
 412       } else if (bytecode.is_invokehandle()) {
 413 #ifdef ASSERT
 414         Klass* holder = cp->klass_ref_at(index, CHECK_NULL);
 415         Symbol* name = cp->name_ref_at(index);
 416         assert(MethodHandles::is_signature_polymorphic_name(holder, name), "");
 417 #endif
 418         cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));
 419         cp_cache_entry->set_method_handle(cp, callInfo);
 420       } else {
 421         report_error("no dynamic invoke found");
 422         return NULL;
 423       }
 424       char* dyno_ref = parse_string();
 425       if (strcmp(dyno_ref, "<appendix>") == 0) {
 426         obj = cp_cache_entry->appendix_if_resolved(cp);
 427       } else if (strcmp(dyno_ref, "<adapter>") == 0) {
 428         if (!parse_terminator()) {
 429           report_error("no dynamic invoke found");
 430           return NULL;
 431         }
 432         Method* adapter = cp_cache_entry->f1_as_method();
 433         if (adapter == NULL) {
 434           report_error("no adapter found");
 435           return NULL;
 436         }
 437         return adapter->method_holder();
 438       } else if (strcmp(dyno_ref, "<bsm>") == 0) {
 439         int pool_index = cp_cache_entry->constant_pool_index();
 440         BootstrapInfo bootstrap_specifier(cp, pool_index, index);
 441         obj = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), CHECK_NULL);
 442       } else {
 443         report_error("unrecognized token");
 444         return NULL;
 445       }
 446     } else {
 447       // constant pool ref (MethodHandle)
 448       if (strcmp(ref, "cpi") != 0) {
 449         report_error("unexpected token");
 450         return NULL;
 451       }
 452 
 453       Klass* k = parse_klass(CHECK_NULL);
 454       if (k == NULL) {
 455         return NULL;
 456       }
 457       InstanceKlass* ik = InstanceKlass::cast(k);
 458       const constantPoolHandle cp(Thread::current(), ik->constants());
 459 
 460       int cpi = parse_int("cpi");
 461 
 462       if (cpi >= cp->length()) {
 463         report_error("bad cpi");
 464         return NULL;
 465       }
 466       if (!cp->tag_at(cpi).is_method_handle()) {
 467         report_error("no method handle found at cpi");
 468         return NULL;
 469       }
 470       ik->link_class(CHECK_NULL);
 471       obj = cp->resolve_possibly_cached_constant_at(cpi, CHECK_NULL);
 472     }
 473     if (obj == NULL) {
 474       report_error("null cp object found");
 475       return NULL;
 476     }
 477     Klass* k = NULL;
 478     skip_ws();
 479     // loop: read fields
 480     char* field = NULL;
 481     do {
 482       field = parse_string();
 483       if (field == NULL) {
 484         report_error("no field found");
 485         return NULL;
 486       }
 487       if (strcmp(field, ";") == 0) {
 488         break;
 489       }
 490       // raw Method*
 491       if (strcmp(field, "<vmtarget>") == 0) {
 492         Method* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
 493         k = (vmtarget == NULL) ? NULL : vmtarget->method_holder();
 494         if (k == NULL) {
 495           report_error("null vmtarget found");
 496           return NULL;
 497         }
 498         if (!parse_terminator()) {
 499           report_error("missing terminator");
 500           return NULL;
 501         }
 502         return k;
 503       }
 504       obj = ciReplay::obj_field(obj, field);
 505       // array
 506       if (obj != NULL && obj->is_objArray()) {
 507         objArrayOop arr = (objArrayOop)obj;
 508         int index = parse_int("index");
 509         if (index >= arr->length()) {
 510           report_error("bad array index");
 511           return NULL;
 512         }
 513         obj = arr->obj_at(index);
 514       }
 515     } while (obj != NULL);
 516     if (obj == NULL) {
 517       report_error("null field found");
 518       return NULL;
 519     }
 520     k = obj->klass();
 521     return k;
 522   }
 523 
 524   // Parse a valid klass name and look it up
 525   // syntax: <name>
 526   // syntax: <constant pool ref>
 527   Klass* parse_klass(TRAPS) {
 528     skip_ws();
 529     // check for constant pool object reference (for a dynamic/hidden class)
 530     bool cp_ref = (*_bufptr == '@');
 531     if (cp_ref) {
 532       ++_bufptr;
 533       Klass* k = parse_cp_ref(CHECK_NULL);
 534       if (k != NULL && !k->is_hidden()) {
 535         report_error("expected hidden class");
 536         return NULL;
 537       }
 538       return k;
 539     }
 540     char* str = parse_escaped_string();
 541     Symbol* klass_name = SymbolTable::new_symbol(str);
 542     if (klass_name != NULL) {
 543       Klass* k = NULL;
 544       if (_iklass != NULL) {
 545         k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding();
 546       } else {
 547         k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 548       }
 549       if (HAS_PENDING_EXCEPTION) {
 550         oop throwable = PENDING_EXCEPTION;
 551         java_lang_Throwable::print(throwable, tty);
 552         tty->cr();
 553         report_error(str);
 554         if (ReplayIgnoreInitErrors) {
 555           CLEAR_PENDING_EXCEPTION;
 556           _error_message = NULL;
 557         }
 558         return NULL;
 559       }
 560       return k;
 561     }
 562     return NULL;
 563   }
 564 
 565   // Lookup a klass
 566   Klass* resolve_klass(const char* klass, TRAPS) {
 567     Symbol* klass_name = SymbolTable::new_symbol(klass);
 568     return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 569   }
 570 
 571   // Parse the standard tuple of <klass> <name> <signature>
 572   Method* parse_method(TRAPS) {
 573     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
 574     if (k == NULL) {
 575       report_error("Can't find holder klass");
 576       return NULL;
 577     }
 578     Symbol* method_name = parse_symbol();
 579     Symbol* method_signature = parse_symbol();
 580     Method* m = k->find_method(method_name, method_signature);
 581     if (m == NULL) {
 582       report_error("Can't find method");
 583     }
 584     return m;
 585   }
 586 
 587   int get_line(int c) {
 588     int buffer_pos = 0;
 589     while(c != EOF) {
 590       if (buffer_pos + 1 >= _buffer_length) {
 591         int new_length = _buffer_length * 2;
 592         // Next call will throw error in case of OOM.
 593         _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length);
 594         _buffer_length = new_length;
 595       }
 596       if (c == '\n') {
 597         c = getc(_stream); // get next char
 598         break;
 599       } else if (c == '\r') {
 600         // skip LF
 601       } else {
 602         _buffer[buffer_pos++] = c;
 603       }
 604       c = getc(_stream);
 605     }
 606     // null terminate it, reset the pointer
 607     _buffer[buffer_pos] = '\0'; // NL or EOF
 608     _bufptr = _buffer;
 609     return c;
 610   }
 611 
 612   // Process each line of the replay file executing each command until
 613   // the file ends.
 614   void process(TRAPS) {
 615     int line_no = 1;
 616     int c = getc(_stream);
 617     while(c != EOF) {
 618       c = get_line(c);
 619       process_command(THREAD);
 620       if (had_error()) {
 621         int pos = _bufptr - _buffer + 1;
 622         tty->print_cr("Error while parsing line %d at position %d: %s\n", line_no, pos, _error_message);
 623         if (ReplayIgnoreInitErrors) {
 624           CLEAR_PENDING_EXCEPTION;
 625           _error_message = NULL;
 626         } else {
 627           return;
 628         }
 629       }
 630       line_no++;
 631     }
 632   }
 633 
 634   void process_command(TRAPS) {
 635     char* cmd = parse_string();
 636     if (cmd == NULL) {
 637       return;
 638     }
 639     if (strcmp("#", cmd) == 0) {
 640       // comment line, print or ignore
 641       if (Verbose) {
 642         tty->print_cr("# %s", _bufptr);
 643       }
 644       skip_remaining();
 645     } else if (strcmp("version", cmd) == 0) {
 646       _version = parse_int("version");
 647       if (_version < 0 || _version > REPLAY_VERSION) {
 648         tty->print_cr("# unrecognized version %d, expected 0 <= version <= %d", _version, REPLAY_VERSION);
 649       }
 650     } else if (strcmp("compile", cmd) == 0) {
 651       process_compile(CHECK);
 652     } else if (strcmp("ciMethod", cmd) == 0) {
 653       process_ciMethod(CHECK);
 654     } else if (strcmp("ciMethodData", cmd) == 0) {
 655       process_ciMethodData(CHECK);
 656     } else if (strcmp("staticfield", cmd) == 0) {
 657       process_staticfield(CHECK);
 658     } else if (strcmp("ciInstanceKlass", cmd) == 0) {
 659       process_ciInstanceKlass(CHECK);
 660     } else if (strcmp("instanceKlass", cmd) == 0) {
 661       process_instanceKlass(CHECK);
 662 #if INCLUDE_JVMTI
 663     } else if (strcmp("JvmtiExport", cmd) == 0) {
 664       process_JvmtiExport(CHECK);
 665 #endif // INCLUDE_JVMTI
 666     } else {
 667       report_error("unknown command");
 668     }
 669     if (!had_error() && *_bufptr != '\0') {
 670       report_error("line not properly terminated");
 671     }
 672   }
 673 
 674   // validation of comp_level
 675   bool is_valid_comp_level(int comp_level) {
 676     const int msg_len = 256;
 677     char* msg = NULL;
 678     if (!is_compile(comp_level)) {
 679       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 680       jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
 681     } else if (is_c1_compile(comp_level) && !CompilerConfig::is_c1_enabled()) {
 682       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 683       jio_snprintf(msg, msg_len, "compilation level %d requires C1", comp_level);
 684     } else if (is_c2_compile(comp_level) && !CompilerConfig::is_c2_enabled()) {
 685       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 686       jio_snprintf(msg, msg_len, "compilation level %d requires C2", comp_level);
 687     }
 688     if (msg != NULL) {
 689       report_error(msg);
 690       return false;
 691     }
 692     return true;
 693   }
 694 
 695   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <klass> <name> <signature>)*
 696   void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) {
 697     _imethod    = m;
 698     _iklass     = imethod->holder();
 699     _entry_bci  = entry_bci;
 700     _comp_level = comp_level;
 701     int line_no = 1;
 702     int c = getc(_stream);
 703     while(c != EOF) {
 704       c = get_line(c);
 705       // Expecting only lines with "compile" command in inline replay file.
 706       char* cmd = parse_string();
 707       if (cmd == NULL || strcmp("compile", cmd) != 0) {
 708         return NULL;
 709       }
 710       process_compile(CHECK_NULL);
 711       if (had_error()) {
 712         tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 713         tty->print_cr("%s", _buffer);
 714         return NULL;
 715       }
 716       if (_ci_inline_records != NULL && _ci_inline_records->length() > 0) {
 717         // Found inlining record for the requested method.
 718         return _ci_inline_records;
 719       }
 720       line_no++;
 721     }
 722     return NULL;
 723   }
 724 
 725   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <inline_late> <klass> <name> <signature>)*
 726   void process_compile(TRAPS) {
 727     Method* method = parse_method(CHECK);
 728     if (had_error()) return;
 729     int entry_bci = parse_int("entry_bci");
 730     const char* comp_level_label = "comp_level";
 731     int comp_level = parse_int(comp_level_label);
 732     // old version w/o comp_level
 733     if (had_error() && (error_message() == comp_level_label)) {
 734       // use highest available tier
 735       comp_level = CompilationPolicy::highest_compile_level();
 736     }
 737     if (!is_valid_comp_level(comp_level)) {
 738       return;
 739     }
 740     if (_imethod != NULL) {
 741       // Replay Inlining
 742       if (entry_bci != _entry_bci || comp_level != _comp_level) {
 743         return;
 744       }
 745       const char* iklass_name  = _imethod->method_holder()->name()->as_utf8();
 746       const char* imethod_name = _imethod->name()->as_utf8();
 747       const char* isignature   = _imethod->signature()->as_utf8();
 748       const char* klass_name   = method->method_holder()->name()->as_utf8();
 749       const char* method_name  = method->name()->as_utf8();
 750       const char* signature    = method->signature()->as_utf8();
 751       if (strcmp(iklass_name,  klass_name)  != 0 ||
 752           strcmp(imethod_name, method_name) != 0 ||
 753           strcmp(isignature,   signature)   != 0) {
 754         return;
 755       }
 756     }
 757     int inline_count = 0;
 758     if (parse_tag_and_count("inline", inline_count)) {
 759       // Record inlining data
 760       _ci_inline_records = new GrowableArray<ciInlineRecord*>();
 761       for (int i = 0; i < inline_count; i++) {
 762         int depth = parse_int("inline_depth");
 763         int bci = parse_int("inline_bci");
 764         if (had_error()) {
 765           break;
 766         }
 767         int inline_late = 0;
 768         if (_version >= 2) {
 769           inline_late = parse_int("inline_late");
 770           if (had_error()) {
 771               break;
 772           }
 773         }
 774 
 775         Method* inl_method = parse_method(CHECK);
 776         if (had_error()) {
 777           break;
 778         }
 779         new_ciInlineRecord(inl_method, bci, depth, inline_late);
 780       }
 781     }
 782     if (_imethod != NULL) {
 783       return; // Replay Inlining
 784     }
 785     InstanceKlass* ik = method->method_holder();
 786     ik->initialize(THREAD);
 787     if (HAS_PENDING_EXCEPTION) {
 788       oop throwable = PENDING_EXCEPTION;
 789       java_lang_Throwable::print(throwable, tty);
 790       tty->cr();
 791       if (ReplayIgnoreInitErrors) {
 792         CLEAR_PENDING_EXCEPTION;
 793         ik->set_init_state(InstanceKlass::fully_initialized);
 794       } else {
 795         return;
 796       }
 797     }
 798     // Make sure the existence of a prior compile doesn't stop this one
 799     CompiledMethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
 800     if (nm != NULL) {
 801       nm->make_not_entrant();
 802     }
 803     replay_state = this;
 804     CompileBroker::compile_method(methodHandle(THREAD, method), entry_bci, comp_level,
 805                                   methodHandle(), 0, CompileTask::Reason_Replay, THREAD);
 806     replay_state = NULL;
 807     reset();
 808   }
 809 
 810   // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
 811   void process_ciMethod(TRAPS) {
 812     Method* method = parse_method(CHECK);
 813     if (had_error()) return;
 814     ciMethodRecord* rec = new_ciMethod(method);
 815     rec->_invocation_counter = parse_int("invocation_counter");
 816     rec->_backedge_counter = parse_int("backedge_counter");
 817     rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count");
 818     rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count");
 819     rec->_instructions_size = parse_int("instructions_size");
 820   }
 821 
 822   // ciMethodData <klass> <name> <signature> <state> <invocation_counter> orig <length> <byte>* data <length> <ptr>* oops <length> (<offset> <klass>)* methods <length> (<offset> <klass> <name> <signature>)*
 823   void process_ciMethodData(TRAPS) {
 824     Method* method = parse_method(CHECK);
 825     if (had_error()) return;
 826     /* just copied from Method, to build interpret data*/
 827 
 828     // To be properly initialized, some profiling in the MDO needs the
 829     // method to be rewritten (number of arguments at a call for
 830     // instance)
 831     method->method_holder()->link_class(CHECK);
 832     // Method::build_interpreter_method_data(method, CHECK);
 833     {
 834       // Grab a lock here to prevent multiple
 835       // MethodData*s from being created.
 836       MutexLocker ml(THREAD, MethodData_lock);
 837       if (method->method_data() == NULL) {
 838         ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 839         MethodData* method_data = MethodData::allocate(loader_data, methodHandle(THREAD, method), CHECK);
 840         method->set_method_data(method_data);
 841       }
 842     }
 843 
 844     // collect and record all the needed information for later
 845     ciMethodDataRecord* rec = new_ciMethodData(method);
 846     rec->_state = parse_int("state");
 847     if (_version < 1) {
 848       parse_int("current_mileage");
 849     } else {
 850       rec->_invocation_counter = parse_int("invocation_counter");
 851     }
 852 
 853     rec->_orig_data = parse_data("orig", rec->_orig_data_length);
 854     if (rec->_orig_data == NULL) {
 855       return;
 856     }
 857     rec->_data = parse_intptr_data("data", rec->_data_length);
 858     if (rec->_data == NULL) {
 859       return;
 860     }
 861     if (!parse_tag_and_count("oops", rec->_classes_length)) {
 862       return;
 863     }
 864     rec->_classes = NEW_RESOURCE_ARRAY(Klass*, rec->_classes_length);
 865     rec->_classes_offsets = NEW_RESOURCE_ARRAY(int, rec->_classes_length);
 866     for (int i = 0; i < rec->_classes_length; i++) {
 867       int offset = parse_int("offset");
 868       if (had_error()) {
 869         return;
 870       }
 871       Klass* k = parse_klass(CHECK);
 872       rec->_classes_offsets[i] = offset;
 873       rec->_classes[i] = k;
 874     }
 875 
 876     if (!parse_tag_and_count("methods", rec->_methods_length)) {
 877       return;
 878     }
 879     rec->_methods = NEW_RESOURCE_ARRAY(Method*, rec->_methods_length);
 880     rec->_methods_offsets = NEW_RESOURCE_ARRAY(int, rec->_methods_length);
 881     for (int i = 0; i < rec->_methods_length; i++) {
 882       int offset = parse_int("offset");
 883       if (had_error()) {
 884         return;
 885       }
 886       Method* m = parse_method(CHECK);
 887       rec->_methods_offsets[i] = offset;
 888       rec->_methods[i] = m;
 889     }
 890   }
 891 
 892   // instanceKlass <name>
 893   // instanceKlass <constant pool ref> # <original hidden class name>
 894   //
 895   // Loads and initializes the klass 'name'.  This can be used to
 896   // create particular class loading environments
 897   void process_instanceKlass(TRAPS) {
 898     // just load the referenced class
 899     Klass* k = parse_klass(CHECK);
 900 
 901     if (_version >= 1) {
 902       if (!_protection_domain_initialized && k != NULL) {
 903         assert(_protection_domain() == NULL, "must be uninitialized");
 904         // The first entry is the holder class of the method for which a replay compilation is requested.
 905         // Use the same protection domain to load all subsequent classes in order to resolve all classes
 906         // in signatures of inlinees. This ensures that inlining can be done as stated in the replay file.
 907         _protection_domain = Handle(_thread, k->protection_domain());
 908       }
 909 
 910       _protection_domain_initialized = true;
 911     }
 912 
 913     if (k == NULL) {
 914       return;
 915     }
 916     const char* comment = parse_string();
 917     bool is_comment = comment != NULL && strcmp(comment, "#") == 0;
 918     if (k->is_hidden() != is_comment) {
 919       report_error("hidden class with comment expected");
 920       return;
 921     }
 922     // comment, print or ignore
 923     if (is_comment) {
 924       if (Verbose) {
 925         const char* hidden = parse_string();
 926         tty->print_cr("Found %s for %s", k->name()->as_quoted_ascii(), hidden);
 927       }
 928       skip_remaining();
 929     }
 930   }
 931 
 932   // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag*
 933   //
 934   // Load the klass 'name' and link or initialize it.  Verify that the
 935   // constant pool is the same length as 'length' and make sure the
 936   // constant pool tags are in the same state.
 937   void process_ciInstanceKlass(TRAPS) {
 938     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK);
 939     if (k == NULL) {
 940       skip_remaining();
 941       return;
 942     }
 943     int is_linked = parse_int("is_linked");
 944     int is_initialized = parse_int("is_initialized");
 945     int length = parse_int("length");
 946     if (is_initialized) {
 947       k->initialize(THREAD);
 948       if (HAS_PENDING_EXCEPTION) {
 949         oop throwable = PENDING_EXCEPTION;
 950         java_lang_Throwable::print(throwable, tty);
 951         tty->cr();
 952         if (ReplayIgnoreInitErrors) {
 953           CLEAR_PENDING_EXCEPTION;
 954           k->set_init_state(InstanceKlass::fully_initialized);
 955         } else {
 956           return;
 957         }
 958       }
 959     } else if (is_linked) {
 960       k->link_class(CHECK);
 961     }
 962     new_ciInstanceKlass(k);
 963     ConstantPool* cp = k->constants();
 964     if (length != cp->length()) {
 965       report_error("constant pool length mismatch: wrong class files?");
 966       return;
 967     }
 968 
 969     int parsed_two_word = 0;
 970     for (int i = 1; i < length; i++) {
 971       int tag = parse_int("tag");
 972       if (had_error()) {
 973         return;
 974       }
 975       switch (cp->tag_at(i).value()) {
 976         case JVM_CONSTANT_UnresolvedClass: {
 977           if (tag == JVM_CONSTANT_Class) {
 978             tty->print_cr("Resolving klass %s at %d", cp->klass_name_at(i)->as_utf8(), i);
 979             Klass* k = cp->klass_at(i, CHECK);
 980           }
 981           break;
 982         }
 983 
 984         case JVM_CONSTANT_Long:
 985         case JVM_CONSTANT_Double:
 986           parsed_two_word = i + 1;
 987 
 988         case JVM_CONSTANT_ClassIndex:
 989         case JVM_CONSTANT_StringIndex:
 990         case JVM_CONSTANT_String:
 991         case JVM_CONSTANT_UnresolvedClassInError:
 992         case JVM_CONSTANT_Fieldref:
 993         case JVM_CONSTANT_Methodref:
 994         case JVM_CONSTANT_InterfaceMethodref:
 995         case JVM_CONSTANT_NameAndType:
 996         case JVM_CONSTANT_Utf8:
 997         case JVM_CONSTANT_Integer:
 998         case JVM_CONSTANT_Float:
 999         case JVM_CONSTANT_MethodHandle:
1000         case JVM_CONSTANT_MethodType:
1001         case JVM_CONSTANT_Dynamic:
1002         case JVM_CONSTANT_InvokeDynamic:
1003           if (tag != cp->tag_at(i).value()) {
1004             report_error("tag mismatch: wrong class files?");
1005             return;
1006           }
1007           break;
1008 
1009         case JVM_CONSTANT_Class:
1010           if (tag == JVM_CONSTANT_UnresolvedClass) {
1011             Klass* k = cp->klass_at(i, CHECK);
1012             tty->print_cr("Warning: entry was unresolved in the replay data: %s", k->name()->as_utf8());
1013           } else if (tag != JVM_CONSTANT_Class) {
1014             report_error("Unexpected tag");
1015             return;
1016           }
1017           break;
1018 
1019         case 0:
1020           if (parsed_two_word == i) continue;
1021 
1022         default:
1023           fatal("Unexpected tag: %d", cp->tag_at(i).value());
1024           break;
1025       }
1026 
1027     }
1028   }
1029 
1030   class InlineTypeFieldInitializer : public FieldClosure {
1031     oop _vt;
1032     CompileReplay* _replay;
1033   public:
1034     InlineTypeFieldInitializer(oop vt, CompileReplay* replay)
1035   : _vt(vt), _replay(replay) {}
1036 
1037     void do_field(fieldDescriptor* fd) {
1038       BasicType bt = fd->field_type();
1039       const char* string_value = bt != T_PRIMITIVE_OBJECT ? _replay->parse_escaped_string() : NULL;
1040       switch (bt) {
1041       case T_BYTE: {
1042         int value = atoi(string_value);
1043         _vt->byte_field_put(fd->offset(), value);
1044         break;
1045       }
1046       case T_BOOLEAN: {
1047         int value = atoi(string_value);
1048         _vt->bool_field_put(fd->offset(), value);
1049         break;
1050       }
1051       case T_SHORT: {
1052         int value = atoi(string_value);
1053         _vt->short_field_put(fd->offset(), value);
1054         break;
1055       }
1056       case T_CHAR: {
1057         int value = atoi(string_value);
1058         _vt->char_field_put(fd->offset(), value);
1059         break;
1060       }
1061       case T_INT: {
1062         int value = atoi(string_value);
1063         _vt->int_field_put(fd->offset(), value);
1064         break;
1065       }
1066       case T_LONG: {
1067         jlong value;
1068         if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
1069           fprintf(stderr, "Error parsing long: %s\n", string_value);
1070           break;
1071         }
1072         _vt->long_field_put(fd->offset(), value);
1073         break;
1074       }
1075       case T_FLOAT: {
1076         float value = atof(string_value);
1077         _vt->float_field_put(fd->offset(), value);
1078         break;
1079       }
1080       case T_DOUBLE: {
1081         double value = atof(string_value);
1082         _vt->double_field_put(fd->offset(), value);
1083         break;
1084       }
1085       case T_ARRAY:
1086       case T_OBJECT: {
1087         JavaThread* THREAD = JavaThread::current();
1088         bool res = _replay->process_staticfield_reference(string_value, _vt, fd, THREAD);
1089         assert(res, "should succeed for arrays & objects");
1090         break;
1091       }
1092       case T_PRIMITIVE_OBJECT: {
1093         InlineKlass* vk = InlineKlass::cast(fd->field_holder()->get_inline_type_field_klass(fd->index()));
1094         if (fd->is_inlined()) {
1095           int field_offset = fd->offset() - vk->first_field_offset();
1096           oop obj = cast_to_oop(cast_from_oop<address>(_vt) + field_offset);
1097           InlineTypeFieldInitializer init_fields(obj, _replay);
1098           vk->do_nonstatic_fields(&init_fields);
1099         } else {
1100           oop value = vk->allocate_instance(JavaThread::current());
1101           _vt->obj_field_put(fd->offset(), value);
1102         }
1103         break;
1104       }
1105       default: {
1106         fatal("Unhandled type: %s", type2name(bt));
1107       }
1108       }
1109     }
1110   };
1111 
1112   bool process_staticfield_reference(const char* field_signature, oop java_mirror, fieldDescriptor* fd, TRAPS) {
1113     if (field_signature[0] == JVM_SIGNATURE_ARRAY) {
1114       int length = parse_int("array length");
1115       oop value = NULL;
1116 
1117       if (field_signature[1] == JVM_SIGNATURE_ARRAY) {
1118         // multi dimensional array
1119         Klass* k = resolve_klass(field_signature, CHECK_(true));
1120         ArrayKlass* kelem = (ArrayKlass *)k;
1121         int rank = 0;
1122         while (field_signature[rank] == JVM_SIGNATURE_ARRAY) {
1123           rank++;
1124         }
1125         jint* dims = NEW_RESOURCE_ARRAY(jint, rank);
1126         dims[0] = length;
1127         for (int i = 1; i < rank; i++) {
1128           dims[i] = 1; // These aren't relevant to the compiler
1129         }
1130         value = kelem->multi_allocate(rank, dims, CHECK_(true));
1131       } else {
1132         if (strcmp(field_signature, "[B") == 0) {
1133           value = oopFactory::new_byteArray(length, CHECK_(true));
1134         } else if (strcmp(field_signature, "[Z") == 0) {
1135           value = oopFactory::new_boolArray(length, CHECK_(true));
1136         } else if (strcmp(field_signature, "[C") == 0) {
1137           value = oopFactory::new_charArray(length, CHECK_(true));
1138         } else if (strcmp(field_signature, "[S") == 0) {
1139           value = oopFactory::new_shortArray(length, CHECK_(true));
1140         } else if (strcmp(field_signature, "[F") == 0) {
1141           value = oopFactory::new_floatArray(length, CHECK_(true));
1142         } else if (strcmp(field_signature, "[D") == 0) {
1143           value = oopFactory::new_doubleArray(length, CHECK_(true));
1144         } else if (strcmp(field_signature, "[I") == 0) {
1145           value = oopFactory::new_intArray(length, CHECK_(true));
1146         } else if (strcmp(field_signature, "[J") == 0) {
1147           value = oopFactory::new_longArray(length, CHECK_(true));
1148         } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1149                    field_signature[1] == JVM_SIGNATURE_CLASS) {
1150           Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
1151           value = oopFactory::new_objArray(kelem, length, CHECK_(true));
1152         } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1153                    field_signature[1] == JVM_SIGNATURE_PRIMITIVE_OBJECT) {
1154           Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
1155           value = oopFactory::new_valueArray(kelem, length, CHECK_(true));
1156         } else {
1157           report_error("unhandled array staticfield");
1158         }
1159       }
1160       java_mirror->obj_field_put(fd->offset(), value);
1161       return true;
1162     } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
1163       const char* string_value = parse_escaped_string();
1164       Handle value = java_lang_String::create_from_str(string_value, CHECK_(true));
1165       java_mirror->obj_field_put(fd->offset(), value());
1166       return true;
1167     } else if (field_signature[0] == 'L') {
1168       const char* instance = parse_escaped_string();
1169       Klass* k = resolve_klass(instance, CHECK_(true));
1170       oop value = InstanceKlass::cast(k)->allocate_instance(CHECK_(true));
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 == NULL || ReplaySuppressInitializers == 0 ||
1185         (ReplaySuppressInitializers == 2 && k->class_loader() == NULL)) {
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 (field_signature[0] == JVM_SIGNATURE_PRIMITIVE_OBJECT) {
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 NULL;
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 NULL;
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 NULL;
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 != NULL) {
1363       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1364     }
1365     return NULL;
1366   }
1367 
1368   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1369                                       Method* method, int bci, int depth) {
1370     if (records != NULL) {
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 NULL;
1386   }
1387 
1388   const char* error_message() {
1389     return _error_message;
1390   }
1391 
1392   void reset() {
1393     _error_message = NULL;
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 == NULL;
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 NULL;
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 NULL;
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 NULL;
1479   }
1480 
1481   if (rp.had_error()) {
1482     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1483     return NULL;
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 intializers 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 == NULL) {
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 != NULL) {
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()) == NULL;
1590 }
1591 
1592 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth, bool& should_delay) {
1593   if (data != NULL) {
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 == NULL) {
1599       return false;
1600     }
1601     should_delay = record->_inline_late;
1602     return true;
1603   } else if (replay_state != NULL) {
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 == NULL) {
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 != NULL) {
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) == NULL;
1622   } else if (replay_state != NULL) {
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) == NULL;
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 == NULL) {
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->_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 != NULL, "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 != NULL, "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 != NULL;
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 == NULL;
1689 }
1690 #endif // PRODUCT
1691 
1692 oop ciReplay::obj_field(oop obj, Symbol* name) {
1693   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
1694 
1695   do {
1696     if (!ik->has_nonstatic_fields()) {
1697       ik = ik->java_super();
1698       continue;
1699     }
1700 
1701     for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
1702       if (fs.access_flags().is_static()) {
1703         continue;
1704       }
1705       if (fs.name() == name) {
1706         int offset = fs.offset();
1707 #ifdef ASSERT
1708         fieldDescriptor fd = fs.field_descriptor();
1709         assert(fd.offset() == ik->field_offset(fd.index()), "!");
1710 #endif
1711         oop f = obj->obj_field(offset);
1712         return f;
1713       }
1714     }
1715 
1716     ik = ik->java_super();
1717   } while (ik != NULL);
1718   return NULL;
1719 }
1720 
1721 oop ciReplay::obj_field(oop obj, const char *name) {
1722   Symbol* fname = SymbolTable::probe(name, (int)strlen(name));
1723   if (fname == NULL) {
1724     return NULL;
1725   }
1726   return obj_field(obj, fname);
1727 }