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