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