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