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       case T_OBJECT: {
 798         Thread* THREAD = Thread::current();
 799         bool res = _replay->process_staticfield_reference(string_value, _vt, fd, THREAD);
 800         assert(res, "should succeed for arrays & objects");
 801         break;
 802       }
 803       case T_VALUETYPE: {
 804         Thread* THREAD = Thread::current();
 805         SignatureStream ss(fd->signature(), false);
 806         InstanceKlass* holder = fd->field_holder();
 807         Klass* k = ss.as_klass(Handle(THREAD, holder->class_loader()),
 808                                Handle(THREAD, holder->protection_domain()),
 809                                SignatureStream::ReturnNull, THREAD);
 810         assert(k != NULL && !HAS_PENDING_EXCEPTION, "can resolve klass?");
 811         ValueKlass* vk = ValueKlass::cast(k);
 812         int field_offset = fd->offset() - vk->first_field_offset();
 813         oop obj = (oop)((address)_vt + field_offset);
 814         ValueTypeFieldInitializer init_fields(obj, _replay);
 815         vk->do_nonstatic_fields(&init_fields);
 816         break;
 817       }
 818       }
 819     }
 820   };
 821 
 822   bool process_staticfield_reference(const char* field_signature, oop java_mirror, fieldDescriptor* fd, TRAPS) {
 823     if (field_signature[0] == '[') {
 824       int length = parse_int("array length");
 825       oop value = NULL;
 826 
 827       if (field_signature[1] == '[') {
 828         // multi dimensional array
 829         Klass* k = resolve_klass(field_signature, CHECK_(true));
 830         ArrayKlass* kelem = (ArrayKlass *)k;
 831         int rank = 0;
 832         while (field_signature[rank] == '[') {
 833           rank++;
 834         }
 835         int* dims = NEW_RESOURCE_ARRAY(int, rank);
 836         dims[0] = length;
 837         for (int i = 1; i < rank; i++) {
 838           dims[i] = 1; // These aren't relevant to the compiler
 839         }
 840         value = kelem->multi_allocate(rank, dims, CHECK_(true));
 841       } else {
 842         if (strcmp(field_signature, "[B") == 0) {
 843           value = oopFactory::new_byteArray(length, CHECK_(true));
 844         } else if (strcmp(field_signature, "[Z") == 0) {
 845           value = oopFactory::new_boolArray(length, CHECK_(true));
 846         } else if (strcmp(field_signature, "[C") == 0) {
 847           value = oopFactory::new_charArray(length, CHECK_(true));
 848         } else if (strcmp(field_signature, "[S") == 0) {
 849           value = oopFactory::new_shortArray(length, CHECK_(true));
 850         } else if (strcmp(field_signature, "[F") == 0) {
 851           value = oopFactory::new_singleArray(length, CHECK_(true));
 852         } else if (strcmp(field_signature, "[D") == 0) {
 853           value = oopFactory::new_doubleArray(length, CHECK_(true));
 854         } else if (strcmp(field_signature, "[I") == 0) {
 855           value = oopFactory::new_intArray(length, CHECK_(true));
 856         } else if (strcmp(field_signature, "[J") == 0) {
 857           value = oopFactory::new_longArray(length, CHECK_(true));
 858         } else if (field_signature[0] == '[' && field_signature[1] == 'L') {
 859           Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
 860           value = oopFactory::new_objArray(kelem, length, CHECK_(true));
 861         } else {
 862           report_error("unhandled array staticfield");
 863         }
 864       }
 865       java_mirror->obj_field_put(fd->offset(), value);
 866       return true;
 867     } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
 868       const char* string_value = parse_escaped_string();
 869       Handle value = java_lang_String::create_from_str(string_value, CHECK_(true));
 870       java_mirror->obj_field_put(fd->offset(), value());
 871       return true;
 872     } else if (field_signature[0] == 'L') {
 873       Klass* k = resolve_klass(field_signature, CHECK_(true));
 874       oop value = InstanceKlass::cast(k)->allocate_instance(CHECK_(true));
 875       java_mirror->obj_field_put(fd->offset(), value);
 876       return true;
 877     } 
 878     return false;
 879   }
 880 
 881   // Initialize a class and fill in the value for a static field.
 882   // This is useful when the compile was dependent on the value of
 883   // static fields but it's impossible to properly rerun the static
 884   // initiailizer.
 885   void process_staticfield(TRAPS) {
 886     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 887 
 888     if (ReplaySuppressInitializers == 0 ||
 889         ReplaySuppressInitializers == 2 && k->class_loader() == NULL) {
 890       return;
 891     }
 892 
 893     assert(k->is_initialized(), "must be");
 894 
 895     const char* field_name = parse_escaped_string();
 896     const char* field_signature = parse_string();
 897     fieldDescriptor fd;
 898     Symbol* name = SymbolTable::lookup(field_name, (int)strlen(field_name), CHECK);
 899     Symbol* sig = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 900     if (!k->find_local_field(name, sig, &fd) ||
 901         !fd.is_static() ||
 902         fd.has_initial_value()) {
 903       report_error(field_name);
 904       return;
 905     }
 906 
 907     oop java_mirror = k->java_mirror();
 908     if (strcmp(field_signature, "I") == 0) {
 909       const char* string_value = parse_escaped_string();
 910       int value = atoi(string_value);
 911       java_mirror->int_field_put(fd.offset(), value);
 912     } else if (strcmp(field_signature, "B") == 0) {
 913       const char* string_value = parse_escaped_string();
 914       int value = atoi(string_value);
 915       java_mirror->byte_field_put(fd.offset(), value);
 916     } else if (strcmp(field_signature, "C") == 0) {
 917       const char* string_value = parse_escaped_string();
 918       int value = atoi(string_value);
 919       java_mirror->char_field_put(fd.offset(), value);
 920     } else if (strcmp(field_signature, "S") == 0) {
 921       const char* string_value = parse_escaped_string();
 922       int value = atoi(string_value);
 923       java_mirror->short_field_put(fd.offset(), value);
 924     } else if (strcmp(field_signature, "Z") == 0) {
 925       const char* string_value = parse_escaped_string();
 926       int value = atoi(string_value);
 927       java_mirror->bool_field_put(fd.offset(), value);
 928     } else if (strcmp(field_signature, "J") == 0) {
 929       const char* string_value = parse_escaped_string();
 930       jlong value;
 931       if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
 932         fprintf(stderr, "Error parsing long: %s\n", string_value);
 933         return;
 934       }
 935       java_mirror->long_field_put(fd.offset(), value);
 936     } else if (strcmp(field_signature, "F") == 0) {
 937       const char* string_value = parse_escaped_string();
 938       float value = atof(string_value);
 939       java_mirror->float_field_put(fd.offset(), value);
 940     } else if (strcmp(field_signature, "D") == 0) {
 941       const char* string_value = parse_escaped_string();
 942       double value = atof(string_value);
 943       java_mirror->double_field_put(fd.offset(), value);
 944     } else if (field_signature[0] == 'Q') {
 945       Symbol* klass_name = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 946       Klass* kelem = resolve_klass(field_signature, CHECK);
 947       ValueKlass* vk = ValueKlass::cast(kelem);
 948       oop value = vk->allocate_instance(CHECK);
 949       ValueTypeFieldInitializer init_fields(value, this);
 950       vk->do_nonstatic_fields(&init_fields);
 951       if (HAS_PENDING_EXCEPTION) {
 952         return;
 953       }
 954       java_mirror->obj_field_put(fd.offset(), value);
 955     } else {
 956       bool res = process_staticfield_reference(field_signature, java_mirror, &fd, CHECK);
 957       if (!res)  {
 958         report_error("unhandled staticfield");
 959       }
 960     }
 961   }
 962 
 963 #if INCLUDE_JVMTI
 964   void process_JvmtiExport(TRAPS) {
 965     const char* field = parse_string();
 966     bool value = parse_int("JvmtiExport flag") != 0;
 967     if (strcmp(field, "can_access_local_variables") == 0) {
 968       JvmtiExport::set_can_access_local_variables(value);
 969     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
 970       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
 971     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
 972       JvmtiExport::set_can_post_on_exceptions(value);
 973     } else {
 974       report_error("Unrecognized JvmtiExport directive");
 975     }
 976   }
 977 #endif // INCLUDE_JVMTI
 978 
 979   // Create and initialize a record for a ciMethod
 980   ciMethodRecord* new_ciMethod(Method* method) {
 981     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
 982     rec->_klass_name =  method->method_holder()->name()->as_utf8();
 983     rec->_method_name = method->name()->as_utf8();
 984     rec->_signature = method->signature()->as_utf8();
 985     _ci_method_records.append(rec);
 986     return rec;
 987   }
 988 
 989   // Lookup data for a ciMethod
 990   ciMethodRecord* find_ciMethodRecord(Method* method) {
 991     const char* klass_name =  method->method_holder()->name()->as_utf8();
 992     const char* method_name = method->name()->as_utf8();
 993     const char* signature = method->signature()->as_utf8();
 994     for (int i = 0; i < _ci_method_records.length(); i++) {
 995       ciMethodRecord* rec = _ci_method_records.at(i);
 996       if (strcmp(rec->_klass_name, klass_name) == 0 &&
 997           strcmp(rec->_method_name, method_name) == 0 &&
 998           strcmp(rec->_signature, signature) == 0) {
 999         return rec;
1000       }
1001     }
1002     return NULL;
1003   }
1004 
1005   // Create and initialize a record for a ciMethodData
1006   ciMethodDataRecord* new_ciMethodData(Method* method) {
1007     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
1008     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1009     rec->_method_name = method->name()->as_utf8();
1010     rec->_signature = method->signature()->as_utf8();
1011     _ci_method_data_records.append(rec);
1012     return rec;
1013   }
1014 
1015   // Lookup data for a ciMethodData
1016   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
1017     const char* klass_name =  method->method_holder()->name()->as_utf8();
1018     const char* method_name = method->name()->as_utf8();
1019     const char* signature = method->signature()->as_utf8();
1020     for (int i = 0; i < _ci_method_data_records.length(); i++) {
1021       ciMethodDataRecord* rec = _ci_method_data_records.at(i);
1022       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1023           strcmp(rec->_method_name, method_name) == 0 &&
1024           strcmp(rec->_signature, signature) == 0) {
1025         return rec;
1026       }
1027     }
1028     return NULL;
1029   }
1030 
1031   // Create and initialize a record for a ciInlineRecord
1032   ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth) {
1033     ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
1034     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1035     rec->_method_name = method->name()->as_utf8();
1036     rec->_signature = method->signature()->as_utf8();
1037     rec->_inline_bci = bci;
1038     rec->_inline_depth = depth;
1039     _ci_inline_records->append(rec);
1040     return rec;
1041   }
1042 
1043   // Lookup inlining data for a ciMethod
1044   ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
1045     if (_ci_inline_records != NULL) {
1046       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1047     }
1048     return NULL;
1049   }
1050 
1051   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1052                                       Method* method, int bci, int depth) {
1053     if (records != NULL) {
1054       const char* klass_name  = method->method_holder()->name()->as_utf8();
1055       const char* method_name = method->name()->as_utf8();
1056       const char* signature   = method->signature()->as_utf8();
1057       for (int i = 0; i < records->length(); i++) {
1058         ciInlineRecord* rec = records->at(i);
1059         if ((rec->_inline_bci == bci) &&
1060             (rec->_inline_depth == depth) &&
1061             (strcmp(rec->_klass_name, klass_name) == 0) &&
1062             (strcmp(rec->_method_name, method_name) == 0) &&
1063             (strcmp(rec->_signature, signature) == 0)) {
1064           return rec;
1065         }
1066       }
1067     }
1068     return NULL;
1069   }
1070 
1071   const char* error_message() {
1072     return _error_message;
1073   }
1074 
1075   void reset() {
1076     _error_message = NULL;
1077     _ci_method_records.clear();
1078     _ci_method_data_records.clear();
1079   }
1080 
1081   // Take an ascii string contain \u#### escapes and convert it to utf8
1082   // in place.
1083   static void unescape_string(char* value) {
1084     char* from = value;
1085     char* to = value;
1086     while (*from != '\0') {
1087       if (*from != '\\') {
1088         *from++ = *to++;
1089       } else {
1090         switch (from[1]) {
1091           case 'u': {
1092             from += 2;
1093             jchar value=0;
1094             for (int i=0; i<4; i++) {
1095               char c = *from++;
1096               switch (c) {
1097                 case '0': case '1': case '2': case '3': case '4':
1098                 case '5': case '6': case '7': case '8': case '9':
1099                   value = (value << 4) + c - '0';
1100                   break;
1101                 case 'a': case 'b': case 'c':
1102                 case 'd': case 'e': case 'f':
1103                   value = (value << 4) + 10 + c - 'a';
1104                   break;
1105                 case 'A': case 'B': case 'C':
1106                 case 'D': case 'E': case 'F':
1107                   value = (value << 4) + 10 + c - 'A';
1108                   break;
1109                 default:
1110                   ShouldNotReachHere();
1111               }
1112             }
1113             UNICODE::convert_to_utf8(&value, 1, to);
1114             to++;
1115             break;
1116           }
1117           case 't': *to++ = '\t'; from += 2; break;
1118           case 'n': *to++ = '\n'; from += 2; break;
1119           case 'r': *to++ = '\r'; from += 2; break;
1120           case 'f': *to++ = '\f'; from += 2; break;
1121           default:
1122             ShouldNotReachHere();
1123         }
1124       }
1125     }
1126     *from = *to;
1127   }
1128 };
1129 
1130 void ciReplay::replay(TRAPS) {
1131   int exit_code = replay_impl(THREAD);
1132 
1133   Threads::destroy_vm();
1134 
1135   vm_exit(exit_code);
1136 }
1137 
1138 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1139   if (FLAG_IS_DEFAULT(InlineDataFile)) {
1140     tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1141     return NULL;
1142   }
1143 
1144   VM_ENTRY_MARK;
1145   // Load and parse the replay data
1146   CompileReplay rp(InlineDataFile, THREAD);
1147   if (!rp.can_replay()) {
1148     tty->print_cr("ciReplay: !rp.can_replay()");
1149     return NULL;
1150   }
1151   void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1152   if (HAS_PENDING_EXCEPTION) {
1153     Handle throwable(THREAD, PENDING_EXCEPTION);
1154     CLEAR_PENDING_EXCEPTION;
1155     java_lang_Throwable::print_stack_trace(throwable, tty);
1156     tty->cr();
1157     return NULL;
1158   }
1159 
1160   if (rp.had_error()) {
1161     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1162     return NULL;
1163   }
1164   return data;
1165 }
1166 
1167 int ciReplay::replay_impl(TRAPS) {
1168   HandleMark hm;
1169   ResourceMark rm;
1170 
1171   if (ReplaySuppressInitializers > 2) {
1172     // ReplaySuppressInitializers > 2 means that we want to allow
1173     // normal VM bootstrap but once we get into the replay itself
1174     // don't allow any intializers to be run.
1175     ReplaySuppressInitializers = 1;
1176   }
1177 
1178   if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1179     tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1180     return 1;
1181   }
1182 
1183   // Load and parse the replay data
1184   CompileReplay rp(ReplayDataFile, THREAD);
1185   int exit_code = 0;
1186   if (rp.can_replay()) {
1187     rp.process(THREAD);
1188   } else {
1189     exit_code = 1;
1190     return exit_code;
1191   }
1192 
1193   if (HAS_PENDING_EXCEPTION) {
1194     Handle throwable(THREAD, PENDING_EXCEPTION);
1195     CLEAR_PENDING_EXCEPTION;
1196     java_lang_Throwable::print_stack_trace(throwable, tty);
1197     tty->cr();
1198     exit_code = 2;
1199   }
1200 
1201   if (rp.had_error()) {
1202     tty->print_cr("Failed on %s", rp.error_message());
1203     exit_code = 1;
1204   }
1205   return exit_code;
1206 }
1207 
1208 void ciReplay::initialize(ciMethodData* m) {
1209   if (replay_state == NULL) {
1210     return;
1211   }
1212 
1213   ASSERT_IN_VM;
1214   ResourceMark rm;
1215 
1216   Method* method = m->get_MethodData()->method();
1217   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1218   if (rec == NULL) {
1219     // This indicates some mismatch with the original environment and
1220     // the replay environment though it's not always enough to
1221     // interfere with reproducing a bug
1222     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1223     method->print_name(tty);
1224     tty->cr();
1225   } else {
1226     m->_state = rec->_state;
1227     m->_current_mileage = rec->_current_mileage;
1228     if (rec->_data_length != 0) {
1229       assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) ||
1230              m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1231 
1232       // Write the correct ciObjects back into the profile data
1233       ciEnv* env = ciEnv::current();
1234       for (int i = 0; i < rec->_classes_length; i++) {
1235         Klass *k = rec->_classes[i];
1236         // In case this class pointer is is tagged, preserve the tag
1237         // bits
1238         rec->_data[rec->_classes_offsets[i]] =
1239           ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]);
1240       }
1241       for (int i = 0; i < rec->_methods_length; i++) {
1242         Method *m = rec->_methods[i];
1243         *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) =
1244           env->get_metadata(m);
1245       }
1246       // Copy the updated profile data into place as intptr_ts
1247 #ifdef _LP64
1248       Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1249 #else
1250       Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1251 #endif
1252     }
1253 
1254     // copy in the original header
1255     Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1256   }
1257 }
1258 
1259 
1260 bool ciReplay::should_not_inline(ciMethod* method) {
1261   if (replay_state == NULL) {
1262     return false;
1263   }
1264   VM_ENTRY_MARK;
1265   // ciMethod without a record shouldn't be inlined.
1266   return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
1267 }
1268 
1269 bool ciReplay::should_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 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1284   if (data != NULL) {
1285     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1286     VM_ENTRY_MARK;
1287     // Inline record are ordered by bci and depth.
1288     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL;
1289   } else if (replay_state != NULL) {
1290     VM_ENTRY_MARK;
1291     // Inline record are ordered by bci and depth.
1292     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL;
1293   }
1294   return false;
1295 }
1296 
1297 void ciReplay::initialize(ciMethod* m) {
1298   if (replay_state == NULL) {
1299     return;
1300   }
1301 
1302   ASSERT_IN_VM;
1303   ResourceMark rm;
1304 
1305   Method* method = m->get_Method();
1306   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1307   if (rec == NULL) {
1308     // This indicates some mismatch with the original environment and
1309     // the replay environment though it's not always enough to
1310     // interfere with reproducing a bug
1311     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1312     method->print_name(tty);
1313     tty->cr();
1314   } else {
1315     EXCEPTION_CONTEXT;
1316     // m->_instructions_size = rec->_instructions_size;
1317     m->_instructions_size = -1;
1318     m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1319     m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1320     MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1321     guarantee(mcs != NULL, "method counters allocation failed");
1322     mcs->invocation_counter()->_counter = rec->_invocation_counter;
1323     mcs->backedge_counter()->_counter = rec->_backedge_counter;
1324   }
1325 }
1326 
1327 bool ciReplay::is_loaded(Method* method) {
1328   if (replay_state == NULL) {
1329     return true;
1330   }
1331 
1332   ASSERT_IN_VM;
1333   ResourceMark rm;
1334 
1335   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1336   return rec != NULL;
1337 }
1338 #endif // PRODUCT