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_array(kelem, length, CHECK_(true));
 914         } else {
 915           report_error("unhandled array staticfield");
 916         }
 917       }
 918       java_mirror->obj_field_put(fd->offset(), value);
 919       return true;
 920     } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
 921       const char* string_value = parse_escaped_string();
 922       Handle value = java_lang_String::create_from_str(string_value, CHECK_(true));
 923       java_mirror->obj_field_put(fd->offset(), value());
 924       return true;
 925     } else if (field_signature[0] == 'L') {
 926       Klass* k = resolve_klass(field_signature, CHECK_(true));
 927       oop value = InstanceKlass::cast(k)->allocate_instance(CHECK_(true));
 928       java_mirror->obj_field_put(fd->offset(), value);
 929       return true;
 930     }
 931     return false;
 932   }
 933 
 934   // Initialize a class and fill in the value for a static field.
 935   // This is useful when the compile was dependent on the value of
 936   // static fields but it's impossible to properly rerun the static
 937   // initializer.
 938   void process_staticfield(TRAPS) {
 939     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 940 
 941     if (k == NULL || ReplaySuppressInitializers == 0 ||
 942         (ReplaySuppressInitializers == 2 && k->class_loader() == NULL)) {
 943       return;
 944     }
 945 
 946     assert(k->is_initialized(), "must be");
 947 
 948     const char* field_name = parse_escaped_string();
 949     const char* field_signature = parse_string();
 950     fieldDescriptor fd;
 951     Symbol* name = SymbolTable::lookup(field_name, (int)strlen(field_name), CHECK);
 952     Symbol* sig = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 953     if (!k->find_local_field(name, sig, &fd) ||
 954         !fd.is_static() ||
 955         fd.has_initial_value()) {
 956       report_error(field_name);
 957       return;
 958     }
 959 
 960     oop java_mirror = k->java_mirror();
 961     if (strcmp(field_signature, "I") == 0) {
 962       const char* string_value = parse_escaped_string();
 963       int value = atoi(string_value);
 964       java_mirror->int_field_put(fd.offset(), value);
 965     } else if (strcmp(field_signature, "B") == 0) {
 966       const char* string_value = parse_escaped_string();
 967       int value = atoi(string_value);
 968       java_mirror->byte_field_put(fd.offset(), value);
 969     } else if (strcmp(field_signature, "C") == 0) {
 970       const char* string_value = parse_escaped_string();
 971       int value = atoi(string_value);
 972       java_mirror->char_field_put(fd.offset(), value);
 973     } else if (strcmp(field_signature, "S") == 0) {
 974       const char* string_value = parse_escaped_string();
 975       int value = atoi(string_value);
 976       java_mirror->short_field_put(fd.offset(), value);
 977     } else if (strcmp(field_signature, "Z") == 0) {
 978       const char* string_value = parse_escaped_string();
 979       int value = atoi(string_value);
 980       java_mirror->bool_field_put(fd.offset(), value);
 981     } else if (strcmp(field_signature, "J") == 0) {
 982       const char* string_value = parse_escaped_string();
 983       jlong value;
 984       if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
 985         fprintf(stderr, "Error parsing long: %s\n", string_value);
 986         return;
 987       }
 988       java_mirror->long_field_put(fd.offset(), value);
 989     } else if (strcmp(field_signature, "F") == 0) {
 990       const char* string_value = parse_escaped_string();
 991       float value = atof(string_value);
 992       java_mirror->float_field_put(fd.offset(), value);
 993     } else if (strcmp(field_signature, "D") == 0) {
 994       const char* string_value = parse_escaped_string();
 995       double value = atof(string_value);
 996       java_mirror->double_field_put(fd.offset(), value);
 997     } else if (field_signature[0] == 'Q') {
 998       Klass* kelem = resolve_klass(field_signature, CHECK);
 999       ValueKlass* vk = ValueKlass::cast(kelem);
1000       oop value = vk->allocate_instance(CHECK);
1001       ValueTypeFieldInitializer init_fields(value, this);
1002       vk->do_nonstatic_fields(&init_fields);
1003       java_mirror->obj_field_put(fd.offset(), value);
1004     } else {
1005       bool res = process_staticfield_reference(field_signature, java_mirror, &fd, CHECK);
1006       if (!res)  {
1007         report_error("unhandled staticfield");
1008       }
1009     }
1010   }
1011 
1012 #if INCLUDE_JVMTI
1013   void process_JvmtiExport(TRAPS) {
1014     const char* field = parse_string();
1015     bool value = parse_int("JvmtiExport flag") != 0;
1016     if (strcmp(field, "can_access_local_variables") == 0) {
1017       JvmtiExport::set_can_access_local_variables(value);
1018     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
1019       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
1020     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
1021       JvmtiExport::set_can_post_on_exceptions(value);
1022     } else {
1023       report_error("Unrecognized JvmtiExport directive");
1024     }
1025   }
1026 #endif // INCLUDE_JVMTI
1027 
1028   // Create and initialize a record for a ciMethod
1029   ciMethodRecord* new_ciMethod(Method* method) {
1030     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
1031     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1032     rec->_method_name = method->name()->as_utf8();
1033     rec->_signature = method->signature()->as_utf8();
1034     _ci_method_records.append(rec);
1035     return rec;
1036   }
1037 
1038   // Lookup data for a ciMethod
1039   ciMethodRecord* find_ciMethodRecord(Method* method) {
1040     const char* klass_name =  method->method_holder()->name()->as_utf8();
1041     const char* method_name = method->name()->as_utf8();
1042     const char* signature = method->signature()->as_utf8();
1043     for (int i = 0; i < _ci_method_records.length(); i++) {
1044       ciMethodRecord* rec = _ci_method_records.at(i);
1045       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1046           strcmp(rec->_method_name, method_name) == 0 &&
1047           strcmp(rec->_signature, signature) == 0) {
1048         return rec;
1049       }
1050     }
1051     return NULL;
1052   }
1053 
1054   // Create and initialize a record for a ciMethodData
1055   ciMethodDataRecord* new_ciMethodData(Method* method) {
1056     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
1057     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1058     rec->_method_name = method->name()->as_utf8();
1059     rec->_signature = method->signature()->as_utf8();
1060     _ci_method_data_records.append(rec);
1061     return rec;
1062   }
1063 
1064   // Lookup data for a ciMethodData
1065   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
1066     const char* klass_name =  method->method_holder()->name()->as_utf8();
1067     const char* method_name = method->name()->as_utf8();
1068     const char* signature = method->signature()->as_utf8();
1069     for (int i = 0; i < _ci_method_data_records.length(); i++) {
1070       ciMethodDataRecord* rec = _ci_method_data_records.at(i);
1071       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1072           strcmp(rec->_method_name, method_name) == 0 &&
1073           strcmp(rec->_signature, signature) == 0) {
1074         return rec;
1075       }
1076     }
1077     return NULL;
1078   }
1079 
1080   // Create and initialize a record for a ciInlineRecord
1081   ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth) {
1082     ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
1083     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1084     rec->_method_name = method->name()->as_utf8();
1085     rec->_signature = method->signature()->as_utf8();
1086     rec->_inline_bci = bci;
1087     rec->_inline_depth = depth;
1088     _ci_inline_records->append(rec);
1089     return rec;
1090   }
1091 
1092   // Lookup inlining data for a ciMethod
1093   ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
1094     if (_ci_inline_records != NULL) {
1095       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1096     }
1097     return NULL;
1098   }
1099 
1100   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1101                                       Method* method, int bci, int depth) {
1102     if (records != NULL) {
1103       const char* klass_name  = method->method_holder()->name()->as_utf8();
1104       const char* method_name = method->name()->as_utf8();
1105       const char* signature   = method->signature()->as_utf8();
1106       for (int i = 0; i < records->length(); i++) {
1107         ciInlineRecord* rec = records->at(i);
1108         if ((rec->_inline_bci == bci) &&
1109             (rec->_inline_depth == depth) &&
1110             (strcmp(rec->_klass_name, klass_name) == 0) &&
1111             (strcmp(rec->_method_name, method_name) == 0) &&
1112             (strcmp(rec->_signature, signature) == 0)) {
1113           return rec;
1114         }
1115       }
1116     }
1117     return NULL;
1118   }
1119 
1120   const char* error_message() {
1121     return _error_message;
1122   }
1123 
1124   void reset() {
1125     _error_message = NULL;
1126     _ci_method_records.clear();
1127     _ci_method_data_records.clear();
1128   }
1129 
1130   // Take an ascii string contain \u#### escapes and convert it to utf8
1131   // in place.
1132   static void unescape_string(char* value) {
1133     char* from = value;
1134     char* to = value;
1135     while (*from != '\0') {
1136       if (*from != '\\') {
1137         *from++ = *to++;
1138       } else {
1139         switch (from[1]) {
1140           case 'u': {
1141             from += 2;
1142             jchar value=0;
1143             for (int i=0; i<4; i++) {
1144               char c = *from++;
1145               switch (c) {
1146                 case '0': case '1': case '2': case '3': case '4':
1147                 case '5': case '6': case '7': case '8': case '9':
1148                   value = (value << 4) + c - '0';
1149                   break;
1150                 case 'a': case 'b': case 'c':
1151                 case 'd': case 'e': case 'f':
1152                   value = (value << 4) + 10 + c - 'a';
1153                   break;
1154                 case 'A': case 'B': case 'C':
1155                 case 'D': case 'E': case 'F':
1156                   value = (value << 4) + 10 + c - 'A';
1157                   break;
1158                 default:
1159                   ShouldNotReachHere();
1160               }
1161             }
1162             UNICODE::convert_to_utf8(&value, 1, to);
1163             to++;
1164             break;
1165           }
1166           case 't': *to++ = '\t'; from += 2; break;
1167           case 'n': *to++ = '\n'; from += 2; break;
1168           case 'r': *to++ = '\r'; from += 2; break;
1169           case 'f': *to++ = '\f'; from += 2; break;
1170           default:
1171             ShouldNotReachHere();
1172         }
1173       }
1174     }
1175     *from = *to;
1176   }
1177 };
1178 
1179 void ciReplay::replay(TRAPS) {
1180   int exit_code = replay_impl(THREAD);
1181 
1182   Threads::destroy_vm();
1183 
1184   vm_exit(exit_code);
1185 }
1186 
1187 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1188   if (FLAG_IS_DEFAULT(InlineDataFile)) {
1189     tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1190     return NULL;
1191   }
1192 
1193   VM_ENTRY_MARK;
1194   // Load and parse the replay data
1195   CompileReplay rp(InlineDataFile, THREAD);
1196   if (!rp.can_replay()) {
1197     tty->print_cr("ciReplay: !rp.can_replay()");
1198     return NULL;
1199   }
1200   void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1201   if (HAS_PENDING_EXCEPTION) {
1202     Handle throwable(THREAD, PENDING_EXCEPTION);
1203     CLEAR_PENDING_EXCEPTION;
1204     java_lang_Throwable::print_stack_trace(throwable, tty);
1205     tty->cr();
1206     return NULL;
1207   }
1208 
1209   if (rp.had_error()) {
1210     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1211     return NULL;
1212   }
1213   return data;
1214 }
1215 
1216 int ciReplay::replay_impl(TRAPS) {
1217   HandleMark hm;
1218   ResourceMark rm;
1219 
1220   if (ReplaySuppressInitializers > 2) {
1221     // ReplaySuppressInitializers > 2 means that we want to allow
1222     // normal VM bootstrap but once we get into the replay itself
1223     // don't allow any intializers to be run.
1224     ReplaySuppressInitializers = 1;
1225   }
1226 
1227   if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1228     tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1229     return 1;
1230   }
1231 
1232   // Load and parse the replay data
1233   CompileReplay rp(ReplayDataFile, THREAD);
1234   int exit_code = 0;
1235   if (rp.can_replay()) {
1236     rp.process(THREAD);
1237   } else {
1238     exit_code = 1;
1239     return exit_code;
1240   }
1241 
1242   if (HAS_PENDING_EXCEPTION) {
1243     Handle throwable(THREAD, PENDING_EXCEPTION);
1244     CLEAR_PENDING_EXCEPTION;
1245     java_lang_Throwable::print_stack_trace(throwable, tty);
1246     tty->cr();
1247     exit_code = 2;
1248   }
1249 
1250   if (rp.had_error()) {
1251     tty->print_cr("Failed on %s", rp.error_message());
1252     exit_code = 1;
1253   }
1254   return exit_code;
1255 }
1256 
1257 void ciReplay::initialize(ciMethodData* m) {
1258   if (replay_state == NULL) {
1259     return;
1260   }
1261 
1262   ASSERT_IN_VM;
1263   ResourceMark rm;
1264 
1265   Method* method = m->get_MethodData()->method();
1266   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1267   if (rec == NULL) {
1268     // This indicates some mismatch with the original environment and
1269     // the replay environment though it's not always enough to
1270     // interfere with reproducing a bug
1271     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1272     method->print_name(tty);
1273     tty->cr();
1274   } else {
1275     m->_state = rec->_state;
1276     m->_current_mileage = rec->_current_mileage;
1277     if (rec->_data_length != 0) {
1278       assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) ||
1279              m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1280 
1281       // Write the correct ciObjects back into the profile data
1282       ciEnv* env = ciEnv::current();
1283       for (int i = 0; i < rec->_classes_length; i++) {
1284         Klass *k = rec->_classes[i];
1285         // In case this class pointer is is tagged, preserve the tag bits
1286         intptr_t status = 0;
1287         if (k != NULL) {
1288           status = ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]);
1289         }
1290         rec->_data[rec->_classes_offsets[i]] = status;
1291       }
1292       for (int i = 0; i < rec->_methods_length; i++) {
1293         Method *m = rec->_methods[i];
1294         *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) =
1295           env->get_metadata(m);
1296       }
1297       // Copy the updated profile data into place as intptr_ts
1298 #ifdef _LP64
1299       Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1300 #else
1301       Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1302 #endif
1303     }
1304 
1305     // copy in the original header
1306     Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1307   }
1308 }
1309 
1310 
1311 bool ciReplay::should_not_inline(ciMethod* method) {
1312   if (replay_state == NULL) {
1313     return false;
1314   }
1315   VM_ENTRY_MARK;
1316   // ciMethod without a record shouldn't be inlined.
1317   return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
1318 }
1319 
1320 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1321   if (data != NULL) {
1322     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1323     VM_ENTRY_MARK;
1324     // Inline record are ordered by bci and depth.
1325     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) != NULL;
1326   } else if (replay_state != NULL) {
1327     VM_ENTRY_MARK;
1328     // Inline record are ordered by bci and depth.
1329     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) != NULL;
1330   }
1331   return false;
1332 }
1333 
1334 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1335   if (data != NULL) {
1336     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1337     VM_ENTRY_MARK;
1338     // Inline record are ordered by bci and depth.
1339     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL;
1340   } else if (replay_state != NULL) {
1341     VM_ENTRY_MARK;
1342     // Inline record are ordered by bci and depth.
1343     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL;
1344   }
1345   return false;
1346 }
1347 
1348 void ciReplay::initialize(ciMethod* m) {
1349   if (replay_state == NULL) {
1350     return;
1351   }
1352 
1353   ASSERT_IN_VM;
1354   ResourceMark rm;
1355 
1356   Method* method = m->get_Method();
1357   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1358   if (rec == NULL) {
1359     // This indicates some mismatch with the original environment and
1360     // the replay environment though it's not always enough to
1361     // interfere with reproducing a bug
1362     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1363     method->print_name(tty);
1364     tty->cr();
1365   } else {
1366     EXCEPTION_CONTEXT;
1367     // m->_instructions_size = rec->_instructions_size;
1368     m->_instructions_size = -1;
1369     m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1370     m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1371     MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1372     guarantee(mcs != NULL, "method counters allocation failed");
1373     mcs->invocation_counter()->_counter = rec->_invocation_counter;
1374     mcs->backedge_counter()->_counter = rec->_backedge_counter;
1375   }
1376 }
1377 
1378 bool ciReplay::is_loaded(Method* method) {
1379   if (replay_state == NULL) {
1380     return true;
1381   }
1382 
1383   ASSERT_IN_VM;
1384   ResourceMark rm;
1385 
1386   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1387   return rec != NULL;
1388 }
1389 #endif // PRODUCT