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