1 /* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "ci/ciMethodData.hpp"
  26 #include "ci/ciReplay.hpp"
  27 #include "ci/ciSymbol.hpp"
  28 #include "ci/ciKlass.hpp"
  29 #include "ci/ciUtilities.hpp"
  30 #include "compiler/compileBroker.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/oopFactory.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "oops/valueKlass.hpp"
  36 #include "utilities/copy.hpp"
  37 #include "utilities/macros.hpp"
  38 
  39 #ifndef PRODUCT
  40 
  41 // ciReplay
  42 
  43 typedef struct _ciMethodDataRecord {
  44   const char* _klass_name;
  45   const char* _method_name;
  46   const char* _signature;
  47 
  48   int _state;
  49   int _current_mileage;
  50 
  51   intptr_t* _data;
  52   char*     _orig_data;
  53   Klass**   _classes;
  54   Method**  _methods;
  55   int*      _classes_offsets;
  56   int*      _methods_offsets;
  57   int       _data_length;
  58   int       _orig_data_length;
  59   int       _classes_length;
  60   int       _methods_length;
  61 } ciMethodDataRecord;
  62 
  63 typedef struct _ciMethodRecord {
  64   const char* _klass_name;
  65   const char* _method_name;
  66   const char* _signature;
  67 
  68   int _instructions_size;
  69   int _interpreter_invocation_count;
  70   int _interpreter_throwout_count;
  71   int _invocation_counter;
  72   int _backedge_counter;
  73 } ciMethodRecord;
  74 
  75 typedef struct _ciInlineRecord {
  76   const char* _klass_name;
  77   const char* _method_name;
  78   const char* _signature;
  79 
  80   int _inline_depth;
  81   int _inline_bci;
  82 } ciInlineRecord;
  83 
  84 class  CompileReplay;
  85 static CompileReplay* replay_state;
  86 
  87 class CompileReplay : public StackObj {
  88  private:
  89   FILE*   _stream;
  90   Thread* _thread;
  91   Handle  _protection_domain;
  92   Handle  _loader;
  93 
  94   GrowableArray<ciMethodRecord*>     _ci_method_records;
  95   GrowableArray<ciMethodDataRecord*> _ci_method_data_records;
  96 
  97   // Use pointer because we may need to return inline records
  98   // without destroying them.
  99   GrowableArray<ciInlineRecord*>*    _ci_inline_records;
 100 
 101   const char* _error_message;
 102 
 103   char* _bufptr;
 104   char* _buffer;
 105   int   _buffer_length;
 106   int   _buffer_pos;
 107 
 108   // "compile" data
 109   ciKlass* _iklass;
 110   Method*  _imethod;
 111   int      _entry_bci;
 112   int      _comp_level;
 113 
 114  public:
 115   CompileReplay(const char* filename, TRAPS) {
 116     _thread = THREAD;
 117     _loader = Handle(_thread, SystemDictionary::java_system_loader());
 118     _protection_domain = Handle();
 119 
 120     _stream = fopen(filename, "rt");
 121     if (_stream == NULL) {
 122       fprintf(stderr, "ERROR: Can't open replay file %s\n", filename);
 123     }
 124 
 125     _ci_inline_records = NULL;
 126     _error_message = NULL;
 127 
 128     _buffer_length = 32;
 129     _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length);
 130     _bufptr = _buffer;
 131     _buffer_pos = 0;
 132 
 133     _imethod = NULL;
 134     _iklass  = NULL;
 135     _entry_bci  = 0;
 136     _comp_level = 0;
 137 
 138     test();
 139   }
 140 
 141   ~CompileReplay() {
 142     if (_stream != NULL) fclose(_stream);
 143   }
 144 
 145   void test() {
 146     strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
 147     _bufptr = _buffer;
 148     assert(parse_int("test") == 1, "what");
 149     assert(parse_int("test") == 2, "what");
 150     assert(strcmp(parse_string(), "foo") == 0, "what");
 151     assert(parse_int("test") == 4, "what");
 152     assert(strcmp(parse_string(), "bar") == 0, "what");
 153     assert(parse_intptr_t("test") == 9, "what");
 154     assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
 155   }
 156 
 157   bool had_error() {
 158     return _error_message != NULL || _thread->has_pending_exception();
 159   }
 160 
 161   bool can_replay() {
 162     return !(_stream == NULL || had_error());
 163   }
 164 
 165   void report_error(const char* msg) {
 166     _error_message = msg;
 167     // Restore the _buffer contents for error reporting
 168     for (int i = 0; i < _buffer_pos; i++) {
 169       if (_buffer[i] == '\0') _buffer[i] = ' ';
 170     }
 171   }
 172 
 173   int parse_int(const char* label) {
 174     if (had_error()) {
 175       return 0;
 176     }
 177 
 178     int v = 0;
 179     int read;
 180     if (sscanf(_bufptr, "%i%n", &v, &read) != 1) {
 181       report_error(label);
 182     } else {
 183       _bufptr += read;
 184     }
 185     return v;
 186   }
 187 
 188   intptr_t parse_intptr_t(const char* label) {
 189     if (had_error()) {
 190       return 0;
 191     }
 192 
 193     intptr_t v = 0;
 194     int read;
 195     if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
 196       report_error(label);
 197     } else {
 198       _bufptr += read;
 199     }
 200     return v;
 201   }
 202 
 203   void skip_ws() {
 204     // Skip any leading whitespace
 205     while (*_bufptr == ' ' || *_bufptr == '\t') {
 206       _bufptr++;
 207     }
 208   }
 209 
 210 
 211   char* scan_and_terminate(char delim) {
 212     char* str = _bufptr;
 213     while (*_bufptr != delim && *_bufptr != '\0') {
 214       _bufptr++;
 215     }
 216     if (*_bufptr != '\0') {
 217       *_bufptr++ = '\0';
 218     }
 219     if (_bufptr == str) {
 220       // nothing here
 221       return NULL;
 222     }
 223     return str;
 224   }
 225 
 226   char* parse_string() {
 227     if (had_error()) return NULL;
 228 
 229     skip_ws();
 230     return scan_and_terminate(' ');
 231   }
 232 
 233   char* parse_quoted_string() {
 234     if (had_error()) return NULL;
 235 
 236     skip_ws();
 237 
 238     if (*_bufptr == '"') {
 239       _bufptr++;
 240       return scan_and_terminate('"');
 241     } else {
 242       return scan_and_terminate(' ');
 243     }
 244   }
 245 
 246   const char* parse_escaped_string() {
 247     char* result = parse_quoted_string();
 248     if (result != NULL) {
 249       unescape_string(result);
 250     }
 251     return result;
 252   }
 253 
 254   // Look for the tag 'tag' followed by an
 255   bool parse_tag_and_count(const char* tag, int& length) {
 256     const char* t = parse_string();
 257     if (t == NULL) {
 258       return false;
 259     }
 260 
 261     if (strcmp(tag, t) != 0) {
 262       report_error(tag);
 263       return false;
 264     }
 265     length = parse_int("parse_tag_and_count");
 266     return !had_error();
 267   }
 268 
 269   // Parse a sequence of raw data encoded as bytes and return the
 270   // resulting data.
 271   char* parse_data(const char* tag, int& length) {
 272     if (!parse_tag_and_count(tag, length)) {
 273       return NULL;
 274     }
 275 
 276     char * result = NEW_RESOURCE_ARRAY(char, length);
 277     for (int i = 0; i < length; i++) {
 278       int val = parse_int("data");
 279       result[i] = val;
 280     }
 281     return result;
 282   }
 283 
 284   // Parse a standard chunk of data emitted as:
 285   //   'tag' <length> # # ...
 286   // Where each # is an intptr_t item
 287   intptr_t* parse_intptr_data(const char* tag, int& length) {
 288     if (!parse_tag_and_count(tag, length)) {
 289       return NULL;
 290     }
 291 
 292     intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
 293     for (int i = 0; i < length; i++) {
 294       skip_ws();
 295       intptr_t val = parse_intptr_t("data");
 296       result[i] = val;
 297     }
 298     return result;
 299   }
 300 
 301   // Parse a possibly quoted version of a symbol into a symbolOop
 302   Symbol* parse_symbol(TRAPS) {
 303     const char* str = parse_escaped_string();
 304     if (str != NULL) {
 305       Symbol* sym = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
 306       return sym;
 307     }
 308     return NULL;
 309   }
 310 
 311   // Parse a valid klass name and look it up
 312   Klass* parse_klass(TRAPS) {
 313     const char* str = parse_escaped_string();
 314     Symbol* klass_name = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
 315     if (klass_name != NULL) {
 316       Klass* k = NULL;
 317       if (_iklass != NULL) {
 318         k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding();
 319       } else {
 320         k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 321       }
 322       if (HAS_PENDING_EXCEPTION) {
 323         oop throwable = PENDING_EXCEPTION;
 324         java_lang_Throwable::print(throwable, tty);
 325         tty->cr();
 326         report_error(str);
 327         return NULL;
 328       }
 329       return k;
 330     }
 331     return NULL;
 332   }
 333 
 334   // Lookup a klass
 335   Klass* resolve_klass(const char* klass, TRAPS) {
 336     Symbol* klass_name = SymbolTable::lookup(klass, (int)strlen(klass), CHECK_NULL);
 337     return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 338   }
 339 
 340   // Parse the standard tuple of <klass> <name> <signature>
 341   Method* parse_method(TRAPS) {
 342     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
 343     Symbol* method_name = parse_symbol(CHECK_NULL);
 344     Symbol* method_signature = parse_symbol(CHECK_NULL);
 345     Method* m = k->find_method(method_name, method_signature);
 346     if (m == NULL) {
 347       report_error("Can't find method");
 348     }
 349     return m;
 350   }
 351 
 352   int get_line(int c) {
 353     while(c != EOF) {
 354       if (_buffer_pos + 1 >= _buffer_length) {
 355         int new_length = _buffer_length * 2;
 356         // Next call will throw error in case of OOM.
 357         _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length);
 358         _buffer_length = new_length;
 359       }
 360       if (c == '\n') {
 361         c = getc(_stream); // get next char
 362         break;
 363       } else if (c == '\r') {
 364         // skip LF
 365       } else {
 366         _buffer[_buffer_pos++] = c;
 367       }
 368       c = getc(_stream);
 369     }
 370     // null terminate it, reset the pointer
 371     _buffer[_buffer_pos] = '\0'; // NL or EOF
 372     _buffer_pos = 0;
 373     _bufptr = _buffer;
 374     return c;
 375   }
 376 
 377   // Process each line of the replay file executing each command until
 378   // the file ends.
 379   void process(TRAPS) {
 380     int line_no = 1;
 381     int c = getc(_stream);
 382     while(c != EOF) {
 383       c = get_line(c);
 384       process_command(THREAD);
 385       if (had_error()) {
 386         tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 387         if (ReplayIgnoreInitErrors) {
 388           CLEAR_PENDING_EXCEPTION;
 389           _error_message = NULL;
 390         } else {
 391           return;
 392         }
 393       }
 394       line_no++;
 395     }
 396   }
 397 
 398   void process_command(TRAPS) {
 399     char* cmd = parse_string();
 400     if (cmd == NULL) {
 401       return;
 402     }
 403     if (strcmp("#", cmd) == 0) {
 404       // ignore
 405     } else if (strcmp("compile", cmd) == 0) {
 406       process_compile(CHECK);
 407     } else if (strcmp("ciMethod", cmd) == 0) {
 408       process_ciMethod(CHECK);
 409     } else if (strcmp("ciMethodData", cmd) == 0) {
 410       process_ciMethodData(CHECK);
 411     } else if (strcmp("staticfield", cmd) == 0) {
 412       process_staticfield(CHECK);
 413     } else if (strcmp("ciInstanceKlass", cmd) == 0) {
 414       process_ciInstanceKlass(CHECK);
 415     } else if (strcmp("instanceKlass", cmd) == 0) {
 416       process_instanceKlass(CHECK);
 417 #if INCLUDE_JVMTI
 418     } else if (strcmp("JvmtiExport", cmd) == 0) {
 419       process_JvmtiExport(CHECK);
 420 #endif // INCLUDE_JVMTI
 421     } else {
 422       report_error("unknown command");
 423     }
 424   }
 425 
 426   // validation of comp_level
 427   bool is_valid_comp_level(int comp_level) {
 428     const int msg_len = 256;
 429     char* msg = NULL;
 430     if (!is_compile(comp_level)) {
 431       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 432       jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
 433     } else if (!TieredCompilation && (comp_level != CompLevel_highest_tier)) {
 434       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 435       switch (comp_level) {
 436         case CompLevel_simple:
 437           jio_snprintf(msg, msg_len, "compilation level %d requires Client VM or TieredCompilation", comp_level);
 438           break;
 439         case CompLevel_full_optimization:
 440           jio_snprintf(msg, msg_len, "compilation level %d requires Server VM", comp_level);
 441           break;
 442         default:
 443           jio_snprintf(msg, msg_len, "compilation level %d requires TieredCompilation", comp_level);
 444       }
 445     }
 446     if (msg != NULL) {
 447       report_error(msg);
 448       return false;
 449     }
 450     return true;
 451   }
 452 
 453   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> <depth> <bci> <klass> <name> <signature> ...
 454   void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) {
 455     _imethod    = m;
 456     _iklass     = imethod->holder();
 457     _entry_bci  = entry_bci;
 458     _comp_level = comp_level;
 459     int line_no = 1;
 460     int c = getc(_stream);
 461     while(c != EOF) {
 462       c = get_line(c);
 463       // Expecting only lines with "compile" command in inline replay file.
 464       char* cmd = parse_string();
 465       if (cmd == NULL || strcmp("compile", cmd) != 0) {
 466         return NULL;
 467       }
 468       process_compile(CHECK_NULL);
 469       if (had_error()) {
 470         tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 471         tty->print_cr("%s", _buffer);
 472         return NULL;
 473       }
 474       if (_ci_inline_records != NULL && _ci_inline_records->length() > 0) {
 475         // Found inlining record for the requested method.
 476         return _ci_inline_records;
 477       }
 478       line_no++;
 479     }
 480     return NULL;
 481   }
 482 
 483   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> <depth> <bci> <klass> <name> <signature> ...
 484   void process_compile(TRAPS) {
 485     Method* method = parse_method(CHECK);
 486     if (had_error()) return;
 487     int entry_bci = parse_int("entry_bci");
 488     const char* comp_level_label = "comp_level";
 489     int comp_level = parse_int(comp_level_label);
 490     // old version w/o comp_level
 491     if (had_error() && (error_message() == comp_level_label)) {
 492       comp_level = CompLevel_full_optimization;
 493     }
 494     if (!is_valid_comp_level(comp_level)) {
 495       return;
 496     }
 497     if (_imethod != NULL) {
 498       // Replay Inlining
 499       if (entry_bci != _entry_bci || comp_level != _comp_level) {
 500         return;
 501       }
 502       const char* iklass_name  = _imethod->method_holder()->name()->as_utf8();
 503       const char* imethod_name = _imethod->name()->as_utf8();
 504       const char* isignature   = _imethod->signature()->as_utf8();
 505       const char* klass_name   = method->method_holder()->name()->as_utf8();
 506       const char* method_name  = method->name()->as_utf8();
 507       const char* signature    = method->signature()->as_utf8();
 508       if (strcmp(iklass_name,  klass_name)  != 0 ||
 509           strcmp(imethod_name, method_name) != 0 ||
 510           strcmp(isignature,   signature)   != 0) {
 511         return;
 512       }
 513     }
 514     int inline_count = 0;
 515     if (parse_tag_and_count("inline", inline_count)) {
 516       // Record inlining data
 517       _ci_inline_records = new GrowableArray<ciInlineRecord*>();
 518       for (int i = 0; i < inline_count; i++) {
 519         int depth = parse_int("inline_depth");
 520         int bci = parse_int("inline_bci");
 521         if (had_error()) {
 522           break;
 523         }
 524         Method* inl_method = parse_method(CHECK);
 525         if (had_error()) {
 526           break;
 527         }
 528         new_ciInlineRecord(inl_method, bci, depth);
 529       }
 530     }
 531     if (_imethod != NULL) {
 532       return; // Replay Inlining
 533     }
 534     InstanceKlass* ik = method->method_holder();
 535     ik->initialize(THREAD);
 536     if (HAS_PENDING_EXCEPTION) {
 537       oop throwable = PENDING_EXCEPTION;
 538       java_lang_Throwable::print(throwable, tty);
 539       tty->cr();
 540       if (ReplayIgnoreInitErrors) {
 541         CLEAR_PENDING_EXCEPTION;
 542         ik->set_init_state(InstanceKlass::fully_initialized);
 543       } else {
 544         return;
 545       }
 546     }
 547     // Make sure the existence of a prior compile doesn't stop this one
 548     nmethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
 549     if (nm != NULL) {
 550       nm->make_not_entrant();
 551     }
 552     replay_state = this;
 553     CompileBroker::compile_method(method, entry_bci, comp_level,
 554                                   methodHandle(), 0, "replay", THREAD);
 555     replay_state = NULL;
 556     reset();
 557   }
 558 
 559   // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
 560   //
 561   //
 562   void process_ciMethod(TRAPS) {
 563     Method* method = parse_method(CHECK);
 564     if (had_error()) return;
 565     ciMethodRecord* rec = new_ciMethod(method);
 566     rec->_invocation_counter = parse_int("invocation_counter");
 567     rec->_backedge_counter = parse_int("backedge_counter");
 568     rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count");
 569     rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count");
 570     rec->_instructions_size = parse_int("instructions_size");
 571   }
 572 
 573   // ciMethodData <klass> <name> <signature> <state> <current mileage> orig <length> # # ... data <length> # # ... oops <length> # ... methods <length>
 574   void process_ciMethodData(TRAPS) {
 575     Method* method = parse_method(CHECK);
 576     if (had_error()) return;
 577     /* just copied from Method, to build interpret data*/
 578     if (InstanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) {
 579       return;
 580     }
 581     // To be properly initialized, some profiling in the MDO needs the
 582     // method to be rewritten (number of arguments at a call for
 583     // instance)
 584     method->method_holder()->link_class(CHECK);
 585     // methodOopDesc::build_interpreter_method_data(method, CHECK);
 586     {
 587       // Grab a lock here to prevent multiple
 588       // MethodData*s from being created.
 589       MutexLocker ml(MethodData_lock, THREAD);
 590       if (method->method_data() == NULL) {
 591         ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 592         MethodData* method_data = MethodData::allocate(loader_data, method, CHECK);
 593         method->set_method_data(method_data);
 594       }
 595     }
 596 
 597     // collect and record all the needed information for later
 598     ciMethodDataRecord* rec = new_ciMethodData(method);
 599     rec->_state = parse_int("state");
 600     rec->_current_mileage = parse_int("current_mileage");
 601 
 602     rec->_orig_data = parse_data("orig", rec->_orig_data_length);
 603     if (rec->_orig_data == NULL) {
 604       return;
 605     }
 606     rec->_data = parse_intptr_data("data", rec->_data_length);
 607     if (rec->_data == NULL) {
 608       return;
 609     }
 610     if (!parse_tag_and_count("oops", rec->_classes_length)) {
 611       return;
 612     }
 613     rec->_classes = NEW_RESOURCE_ARRAY(Klass*, rec->_classes_length);
 614     rec->_classes_offsets = NEW_RESOURCE_ARRAY(int, rec->_classes_length);
 615     for (int i = 0; i < rec->_classes_length; i++) {
 616       int offset = parse_int("offset");
 617       if (had_error()) {
 618         return;
 619       }
 620       Klass* k = parse_klass(CHECK);
 621       rec->_classes_offsets[i] = offset;
 622       rec->_classes[i] = k;
 623     }
 624 
 625     if (!parse_tag_and_count("methods", rec->_methods_length)) {
 626       return;
 627     }
 628     rec->_methods = NEW_RESOURCE_ARRAY(Method*, rec->_methods_length);
 629     rec->_methods_offsets = NEW_RESOURCE_ARRAY(int, rec->_methods_length);
 630     for (int i = 0; i < rec->_methods_length; i++) {
 631       int offset = parse_int("offset");
 632       if (had_error()) {
 633         return;
 634       }
 635       Method* m = parse_method(CHECK);
 636       rec->_methods_offsets[i] = offset;
 637       rec->_methods[i] = m;
 638     }
 639   }
 640 
 641   // instanceKlass <name>
 642   //
 643   // Loads and initializes the klass 'name'.  This can be used to
 644   // create particular class loading environments
 645   void process_instanceKlass(TRAPS) {
 646     // just load the referenced class
 647     Klass* k = parse_klass(CHECK);
 648   }
 649 
 650   // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag # # # ...
 651   //
 652   // Load the klass 'name' and link or initialize it.  Verify that the
 653   // constant pool is the same length as 'length' and make sure the
 654   // constant pool tags are in the same state.
 655   void process_ciInstanceKlass(TRAPS) {
 656     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 657     int is_linked = parse_int("is_linked");
 658     int is_initialized = parse_int("is_initialized");
 659     int length = parse_int("length");
 660     if (is_initialized) {
 661       k->initialize(THREAD);
 662       if (HAS_PENDING_EXCEPTION) {
 663         oop throwable = PENDING_EXCEPTION;
 664         java_lang_Throwable::print(throwable, tty);
 665         tty->cr();
 666         if (ReplayIgnoreInitErrors) {
 667           CLEAR_PENDING_EXCEPTION;
 668           k->set_init_state(InstanceKlass::fully_initialized);
 669         } else {
 670           return;
 671         }
 672       }
 673     } else if (is_linked) {
 674       k->link_class(CHECK);
 675     }
 676     ConstantPool* cp = k->constants();
 677     if (length != cp->length()) {
 678       report_error("constant pool length mismatch: wrong class files?");
 679       return;
 680     }
 681 
 682     int parsed_two_word = 0;
 683     for (int i = 1; i < length; i++) {
 684       int tag = parse_int("tag");
 685       if (had_error()) {
 686         return;
 687       }
 688       switch (cp->tag_at(i).value()) {
 689         case JVM_CONSTANT_UnresolvedClass: {
 690           if (tag == JVM_CONSTANT_Class) {
 691             tty->print_cr("Resolving klass %s at %d", cp->klass_name_at(i)->as_utf8(), i);
 692             Klass* k = cp->klass_at(i, CHECK);
 693           }
 694           break;
 695         }
 696         case JVM_CONSTANT_Long:
 697         case JVM_CONSTANT_Double:
 698           parsed_two_word = i + 1;
 699 
 700         case JVM_CONSTANT_ClassIndex:
 701         case JVM_CONSTANT_StringIndex:
 702         case JVM_CONSTANT_String:
 703         case JVM_CONSTANT_UnresolvedClassInError:
 704         case JVM_CONSTANT_Fieldref:
 705         case JVM_CONSTANT_Methodref:
 706         case JVM_CONSTANT_InterfaceMethodref:
 707         case JVM_CONSTANT_NameAndType:
 708         case JVM_CONSTANT_Utf8:
 709         case JVM_CONSTANT_Integer:
 710         case JVM_CONSTANT_Float:
 711         case JVM_CONSTANT_MethodHandle:
 712         case JVM_CONSTANT_MethodType:
 713         case JVM_CONSTANT_InvokeDynamic:
 714           if (tag != cp->tag_at(i).value()) {
 715             report_error("tag mismatch: wrong class files?");
 716             return;
 717           }
 718           break;
 719 
 720         case JVM_CONSTANT_Class:
 721           if (tag == JVM_CONSTANT_Class) {
 722           } else if (tag == JVM_CONSTANT_UnresolvedClass) {
 723             tty->print_cr("Warning: entry was unresolved in the replay data");
 724           } else {
 725             report_error("Unexpected tag");
 726             return;
 727           }
 728           break;
 729 
 730         case 0:
 731           if (parsed_two_word == i) continue;
 732 
 733         default:
 734           fatal("Unexpected tag: %d", cp->tag_at(i).value());
 735           break;
 736       }
 737 
 738     }
 739   }
 740 
 741   class ValueTypeFieldInitializer : public FieldClosure {
 742     oop _vt;
 743     CompileReplay* _replay;
 744   public:
 745     ValueTypeFieldInitializer(oop vt, CompileReplay* replay)
 746       : _vt(vt), _replay(replay) {}
 747 
 748     void do_field(fieldDescriptor* fd) {
 749       BasicType bt = fd->field_type();
 750       const char* string_value = bt != T_VALUETYPE ? _replay->parse_escaped_string() : NULL;
 751       switch (bt) {
 752         case T_BYTE: {
 753           int value = atoi(string_value);
 754           _vt->byte_field_put(fd->offset(), value);
 755           break;
 756         }
 757         case T_BOOLEAN: {
 758           int value = atoi(string_value);
 759           _vt->bool_field_put(fd->offset(), value);
 760           break;
 761         }
 762         case T_SHORT: {
 763           int value = atoi(string_value);
 764           _vt->short_field_put(fd->offset(), value);
 765           break;
 766         }
 767         case T_CHAR: {
 768           int value = atoi(string_value);
 769           _vt->char_field_put(fd->offset(), value);
 770           break;
 771         }
 772         case T_INT: {
 773           int value = atoi(string_value);
 774           _vt->int_field_put(fd->offset(), value);
 775           break;
 776         }
 777         case T_LONG: {
 778           jlong value;
 779           if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
 780             fprintf(stderr, "Error parsing long: %s\n", string_value);
 781             break;
 782           }
 783           _vt->long_field_put(fd->offset(), value);
 784           break;
 785         }
 786         case T_FLOAT: {
 787           float value = atof(string_value);
 788           _vt->float_field_put(fd->offset(), value);
 789           break;
 790         }
 791         case T_DOUBLE: {
 792           double value = atof(string_value);
 793           _vt->double_field_put(fd->offset(), value);
 794           break;
 795         }
 796         case T_ARRAY:
 797           _replay->report_error("Array in value type unsupported");
 798           break;
 799         case T_OBJECT:
 800           _replay->report_error("Object in value type unsupported");
 801           break;
 802         case T_VALUETYPE: {
 803           Thread* THREAD = Thread::current();
 804           SignatureStream ss(fd->signature(), false);
 805           InstanceKlass* holder = fd->field_holder();
 806           Klass* k = ss.as_klass(Handle(holder->class_loader()), Handle(holder->protection_domain()), SignatureStream::ReturnNull, THREAD);
 807           assert(k != NULL && !HAS_PENDING_EXCEPTION, "can resolve klass?");
 808           ValueKlass* vk = ValueKlass::cast(k);
 809           int field_offset = fd->offset() - vk->first_field_offset();
 810           oop obj = (oop)((address)_vt + field_offset);
 811           ValueTypeFieldInitializer init_fields(obj, _replay);
 812           vk->do_nonstatic_fields(&init_fields);
 813           break;
 814         }
 815       }
 816     }
 817   };
 818 
 819   // Initialize a class and fill in the value for a static field.
 820   // This is useful when the compile was dependent on the value of
 821   // static fields but it's impossible to properly rerun the static
 822   // initiailizer.
 823   void process_staticfield(TRAPS) {
 824     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 825 
 826     if (ReplaySuppressInitializers == 0 ||
 827         ReplaySuppressInitializers == 2 && k->class_loader() == NULL) {
 828       return;
 829     }
 830 
 831     assert(k->is_initialized(), "must be");
 832 
 833     const char* field_name = parse_escaped_string();
 834     const char* field_signature = parse_string();
 835     fieldDescriptor fd;
 836     Symbol* name = SymbolTable::lookup(field_name, (int)strlen(field_name), CHECK);
 837     Symbol* sig = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 838     if (!k->find_local_field(name, sig, &fd) ||
 839         !fd.is_static() ||
 840         fd.has_initial_value()) {
 841       report_error(field_name);
 842       return;
 843     }
 844 
 845     oop java_mirror = k->java_mirror();
 846     if (field_signature[0] == '[') {
 847       int length = parse_int("array length");
 848       oop value = NULL;
 849 
 850       if (field_signature[1] == '[') {
 851         // multi dimensional array
 852         ArrayKlass* kelem = (ArrayKlass *)parse_klass(CHECK);
 853         int rank = 0;
 854         while (field_signature[rank] == '[') {
 855           rank++;
 856         }
 857         int* dims = NEW_RESOURCE_ARRAY(int, rank);
 858         dims[0] = length;
 859         for (int i = 1; i < rank; i++) {
 860           dims[i] = 1; // These aren't relevant to the compiler
 861         }
 862         value = kelem->multi_allocate(rank, dims, CHECK);
 863       } else {
 864         if (strcmp(field_signature, "[B") == 0) {
 865           value = oopFactory::new_byteArray(length, CHECK);
 866         } else if (strcmp(field_signature, "[Z") == 0) {
 867           value = oopFactory::new_boolArray(length, CHECK);
 868         } else if (strcmp(field_signature, "[C") == 0) {
 869           value = oopFactory::new_charArray(length, CHECK);
 870         } else if (strcmp(field_signature, "[S") == 0) {
 871           value = oopFactory::new_shortArray(length, CHECK);
 872         } else if (strcmp(field_signature, "[F") == 0) {
 873           value = oopFactory::new_singleArray(length, CHECK);
 874         } else if (strcmp(field_signature, "[D") == 0) {
 875           value = oopFactory::new_doubleArray(length, CHECK);
 876         } else if (strcmp(field_signature, "[I") == 0) {
 877           value = oopFactory::new_intArray(length, CHECK);
 878         } else if (strcmp(field_signature, "[J") == 0) {
 879           value = oopFactory::new_longArray(length, CHECK);
 880         } else if (field_signature[0] == '[' && field_signature[1] == 'L') {
 881           KlassHandle kelem = resolve_klass(field_signature + 1, CHECK);
 882           value = oopFactory::new_objArray(kelem(), length, CHECK);
 883         } else {
 884           report_error("unhandled array staticfield");
 885         }
 886       }
 887       java_mirror->obj_field_put(fd.offset(), value);
 888     } else {
 889       const char* string_value = field_signature[0] != 'Q' ? parse_escaped_string() : NULL;
 890       if (strcmp(field_signature, "I") == 0) {
 891         int value = atoi(string_value);
 892         java_mirror->int_field_put(fd.offset(), value);
 893       } else if (strcmp(field_signature, "B") == 0) {
 894         int value = atoi(string_value);
 895         java_mirror->byte_field_put(fd.offset(), value);
 896       } else if (strcmp(field_signature, "C") == 0) {
 897         int value = atoi(string_value);
 898         java_mirror->char_field_put(fd.offset(), value);
 899       } else if (strcmp(field_signature, "S") == 0) {
 900         int value = atoi(string_value);
 901         java_mirror->short_field_put(fd.offset(), value);
 902       } else if (strcmp(field_signature, "Z") == 0) {
 903         int value = atol(string_value);
 904         java_mirror->bool_field_put(fd.offset(), value);
 905       } else if (strcmp(field_signature, "J") == 0) {
 906         jlong value;
 907         if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
 908           fprintf(stderr, "Error parsing long: %s\n", string_value);
 909           return;
 910         }
 911         java_mirror->long_field_put(fd.offset(), value);
 912       } else if (strcmp(field_signature, "F") == 0) {
 913         float value = atof(string_value);
 914         java_mirror->float_field_put(fd.offset(), value);
 915       } else if (strcmp(field_signature, "D") == 0) {
 916         double value = atof(string_value);
 917         java_mirror->double_field_put(fd.offset(), value);
 918       } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
 919         Handle value = java_lang_String::create_from_str(string_value, CHECK);
 920         java_mirror->obj_field_put(fd.offset(), value());
 921       } else if (field_signature[0] == 'L') {
 922         Symbol* klass_name = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 923         KlassHandle kelem = resolve_klass(field_signature, CHECK);
 924         oop value = InstanceKlass::cast(kelem())->allocate_instance(CHECK);
 925         java_mirror->obj_field_put(fd.offset(), value);
 926       } else if (field_signature[0] == 'Q') {
 927         Symbol* klass_name = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 928         KlassHandle kelem = resolve_klass(field_signature, CHECK);
 929         ValueKlass* vk = ValueKlass::cast(kelem());
 930         oop value = vk->allocate_instance(CHECK);
 931         ValueTypeFieldInitializer init_fields(value, this);
 932         vk->do_nonstatic_fields(&init_fields);
 933         java_mirror->obj_field_put(fd.offset(), value);
 934       } else {
 935         report_error("unhandled staticfield");
 936       }
 937     }
 938   }
 939 
 940 #if INCLUDE_JVMTI
 941   void process_JvmtiExport(TRAPS) {
 942     const char* field = parse_string();
 943     bool value = parse_int("JvmtiExport flag") != 0;
 944     if (strcmp(field, "can_access_local_variables") == 0) {
 945       JvmtiExport::set_can_access_local_variables(value);
 946     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
 947       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
 948     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
 949       JvmtiExport::set_can_post_on_exceptions(value);
 950     } else {
 951       report_error("Unrecognized JvmtiExport directive");
 952     }
 953   }
 954 #endif // INCLUDE_JVMTI
 955 
 956   // Create and initialize a record for a ciMethod
 957   ciMethodRecord* new_ciMethod(Method* method) {
 958     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
 959     rec->_klass_name =  method->method_holder()->name()->as_utf8();
 960     rec->_method_name = method->name()->as_utf8();
 961     rec->_signature = method->signature()->as_utf8();
 962     _ci_method_records.append(rec);
 963     return rec;
 964   }
 965 
 966   // Lookup data for a ciMethod
 967   ciMethodRecord* find_ciMethodRecord(Method* method) {
 968     const char* klass_name =  method->method_holder()->name()->as_utf8();
 969     const char* method_name = method->name()->as_utf8();
 970     const char* signature = method->signature()->as_utf8();
 971     for (int i = 0; i < _ci_method_records.length(); i++) {
 972       ciMethodRecord* rec = _ci_method_records.at(i);
 973       if (strcmp(rec->_klass_name, klass_name) == 0 &&
 974           strcmp(rec->_method_name, method_name) == 0 &&
 975           strcmp(rec->_signature, signature) == 0) {
 976         return rec;
 977       }
 978     }
 979     return NULL;
 980   }
 981 
 982   // Create and initialize a record for a ciMethodData
 983   ciMethodDataRecord* new_ciMethodData(Method* method) {
 984     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
 985     rec->_klass_name =  method->method_holder()->name()->as_utf8();
 986     rec->_method_name = method->name()->as_utf8();
 987     rec->_signature = method->signature()->as_utf8();
 988     _ci_method_data_records.append(rec);
 989     return rec;
 990   }
 991 
 992   // Lookup data for a ciMethodData
 993   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
 994     const char* klass_name =  method->method_holder()->name()->as_utf8();
 995     const char* method_name = method->name()->as_utf8();
 996     const char* signature = method->signature()->as_utf8();
 997     for (int i = 0; i < _ci_method_data_records.length(); i++) {
 998       ciMethodDataRecord* rec = _ci_method_data_records.at(i);
 999       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1000           strcmp(rec->_method_name, method_name) == 0 &&
1001           strcmp(rec->_signature, signature) == 0) {
1002         return rec;
1003       }
1004     }
1005     return NULL;
1006   }
1007 
1008   // Create and initialize a record for a ciInlineRecord
1009   ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth) {
1010     ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
1011     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1012     rec->_method_name = method->name()->as_utf8();
1013     rec->_signature = method->signature()->as_utf8();
1014     rec->_inline_bci = bci;
1015     rec->_inline_depth = depth;
1016     _ci_inline_records->append(rec);
1017     return rec;
1018   }
1019 
1020   // Lookup inlining data for a ciMethod
1021   ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
1022     if (_ci_inline_records != NULL) {
1023       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1024     }
1025     return NULL;
1026   }
1027 
1028   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1029                                       Method* method, int bci, int depth) {
1030     if (records != NULL) {
1031       const char* klass_name  = method->method_holder()->name()->as_utf8();
1032       const char* method_name = method->name()->as_utf8();
1033       const char* signature   = method->signature()->as_utf8();
1034       for (int i = 0; i < records->length(); i++) {
1035         ciInlineRecord* rec = records->at(i);
1036         if ((rec->_inline_bci == bci) &&
1037             (rec->_inline_depth == depth) &&
1038             (strcmp(rec->_klass_name, klass_name) == 0) &&
1039             (strcmp(rec->_method_name, method_name) == 0) &&
1040             (strcmp(rec->_signature, signature) == 0)) {
1041           return rec;
1042         }
1043       }
1044     }
1045     return NULL;
1046   }
1047 
1048   const char* error_message() {
1049     return _error_message;
1050   }
1051 
1052   void reset() {
1053     _error_message = NULL;
1054     _ci_method_records.clear();
1055     _ci_method_data_records.clear();
1056   }
1057 
1058   // Take an ascii string contain \u#### escapes and convert it to utf8
1059   // in place.
1060   static void unescape_string(char* value) {
1061     char* from = value;
1062     char* to = value;
1063     while (*from != '\0') {
1064       if (*from != '\\') {
1065         *from++ = *to++;
1066       } else {
1067         switch (from[1]) {
1068           case 'u': {
1069             from += 2;
1070             jchar value=0;
1071             for (int i=0; i<4; i++) {
1072               char c = *from++;
1073               switch (c) {
1074                 case '0': case '1': case '2': case '3': case '4':
1075                 case '5': case '6': case '7': case '8': case '9':
1076                   value = (value << 4) + c - '0';
1077                   break;
1078                 case 'a': case 'b': case 'c':
1079                 case 'd': case 'e': case 'f':
1080                   value = (value << 4) + 10 + c - 'a';
1081                   break;
1082                 case 'A': case 'B': case 'C':
1083                 case 'D': case 'E': case 'F':
1084                   value = (value << 4) + 10 + c - 'A';
1085                   break;
1086                 default:
1087                   ShouldNotReachHere();
1088               }
1089             }
1090             UNICODE::convert_to_utf8(&value, 1, to);
1091             to++;
1092             break;
1093           }
1094           case 't': *to++ = '\t'; from += 2; break;
1095           case 'n': *to++ = '\n'; from += 2; break;
1096           case 'r': *to++ = '\r'; from += 2; break;
1097           case 'f': *to++ = '\f'; from += 2; break;
1098           default:
1099             ShouldNotReachHere();
1100         }
1101       }
1102     }
1103     *from = *to;
1104   }
1105 };
1106 
1107 void ciReplay::replay(TRAPS) {
1108   int exit_code = replay_impl(THREAD);
1109 
1110   Threads::destroy_vm();
1111 
1112   vm_exit(exit_code);
1113 }
1114 
1115 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1116   if (FLAG_IS_DEFAULT(InlineDataFile)) {
1117     tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1118     return NULL;
1119   }
1120 
1121   VM_ENTRY_MARK;
1122   // Load and parse the replay data
1123   CompileReplay rp(InlineDataFile, THREAD);
1124   if (!rp.can_replay()) {
1125     tty->print_cr("ciReplay: !rp.can_replay()");
1126     return NULL;
1127   }
1128   void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1129   if (HAS_PENDING_EXCEPTION) {
1130     Handle throwable(THREAD, PENDING_EXCEPTION);
1131     CLEAR_PENDING_EXCEPTION;
1132     java_lang_Throwable::print_stack_trace(throwable, tty);
1133     tty->cr();
1134     return NULL;
1135   }
1136 
1137   if (rp.had_error()) {
1138     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1139     return NULL;
1140   }
1141   return data;
1142 }
1143 
1144 int ciReplay::replay_impl(TRAPS) {
1145   HandleMark hm;
1146   ResourceMark rm;
1147   // Make sure we don't run with background compilation
1148   BackgroundCompilation = false;
1149 
1150   if (ReplaySuppressInitializers > 2) {
1151     // ReplaySuppressInitializers > 2 means that we want to allow
1152     // normal VM bootstrap but once we get into the replay itself
1153     // don't allow any intializers to be run.
1154     ReplaySuppressInitializers = 1;
1155   }
1156 
1157   if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1158     tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1159     return 1;
1160   }
1161 
1162   // Load and parse the replay data
1163   CompileReplay rp(ReplayDataFile, THREAD);
1164   int exit_code = 0;
1165   if (rp.can_replay()) {
1166     rp.process(THREAD);
1167   } else {
1168     exit_code = 1;
1169     return exit_code;
1170   }
1171 
1172   if (HAS_PENDING_EXCEPTION) {
1173     Handle throwable(THREAD, PENDING_EXCEPTION);
1174     CLEAR_PENDING_EXCEPTION;
1175     java_lang_Throwable::print_stack_trace(throwable, tty);
1176     tty->cr();
1177     exit_code = 2;
1178   }
1179 
1180   if (rp.had_error()) {
1181     tty->print_cr("Failed on %s", rp.error_message());
1182     exit_code = 1;
1183   }
1184   return exit_code;
1185 }
1186 
1187 void ciReplay::initialize(ciMethodData* m) {
1188   if (replay_state == NULL) {
1189     return;
1190   }
1191 
1192   ASSERT_IN_VM;
1193   ResourceMark rm;
1194 
1195   Method* method = m->get_MethodData()->method();
1196   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1197   if (rec == NULL) {
1198     // This indicates some mismatch with the original environment and
1199     // the replay environment though it's not always enough to
1200     // interfere with reproducing a bug
1201     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1202     method->print_name(tty);
1203     tty->cr();
1204   } else {
1205     m->_state = rec->_state;
1206     m->_current_mileage = rec->_current_mileage;
1207     if (rec->_data_length != 0) {
1208       assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) ||
1209              m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1210 
1211       // Write the correct ciObjects back into the profile data
1212       ciEnv* env = ciEnv::current();
1213       for (int i = 0; i < rec->_classes_length; i++) {
1214         Klass *k = rec->_classes[i];
1215         // In case this class pointer is is tagged, preserve the tag
1216         // bits
1217         rec->_data[rec->_classes_offsets[i]] =
1218           ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]);
1219       }
1220       for (int i = 0; i < rec->_methods_length; i++) {
1221         Method *m = rec->_methods[i];
1222         *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) =
1223           env->get_metadata(m);
1224       }
1225       // Copy the updated profile data into place as intptr_ts
1226 #ifdef _LP64
1227       Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1228 #else
1229       Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1230 #endif
1231     }
1232 
1233     // copy in the original header
1234     Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1235   }
1236 }
1237 
1238 
1239 bool ciReplay::should_not_inline(ciMethod* method) {
1240   if (replay_state == NULL) {
1241     return false;
1242   }
1243   VM_ENTRY_MARK;
1244   // ciMethod without a record shouldn't be inlined.
1245   return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
1246 }
1247 
1248 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1249   if (data != NULL) {
1250     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1251     VM_ENTRY_MARK;
1252     // Inline record are ordered by bci and depth.
1253     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) != NULL;
1254   } else if (replay_state != NULL) {
1255     VM_ENTRY_MARK;
1256     // Inline record are ordered by bci and depth.
1257     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) != NULL;
1258   }
1259   return false;
1260 }
1261 
1262 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1263   if (data != NULL) {
1264     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1265     VM_ENTRY_MARK;
1266     // Inline record are ordered by bci and depth.
1267     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL;
1268   } else if (replay_state != NULL) {
1269     VM_ENTRY_MARK;
1270     // Inline record are ordered by bci and depth.
1271     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL;
1272   }
1273   return false;
1274 }
1275 
1276 void ciReplay::initialize(ciMethod* m) {
1277   if (replay_state == NULL) {
1278     return;
1279   }
1280 
1281   ASSERT_IN_VM;
1282   ResourceMark rm;
1283 
1284   Method* method = m->get_Method();
1285   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1286   if (rec == NULL) {
1287     // This indicates some mismatch with the original environment and
1288     // the replay environment though it's not always enough to
1289     // interfere with reproducing a bug
1290     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1291     method->print_name(tty);
1292     tty->cr();
1293   } else {
1294     EXCEPTION_CONTEXT;
1295     // m->_instructions_size = rec->_instructions_size;
1296     m->_instructions_size = -1;
1297     m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1298     m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1299     MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1300     guarantee(mcs != NULL, "method counters allocation failed");
1301     mcs->invocation_counter()->_counter = rec->_invocation_counter;
1302     mcs->backedge_counter()->_counter = rec->_backedge_counter;
1303   }
1304 }
1305 
1306 bool ciReplay::is_loaded(Method* method) {
1307   if (replay_state == NULL) {
1308     return true;
1309   }
1310 
1311   ASSERT_IN_VM;
1312   ResourceMark rm;
1313 
1314   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1315   return rec != NULL;
1316 }
1317 #endif // PRODUCT