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