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