1 /*
   2  * Copyright (c) 2013, 2021, 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/javaClasses.hpp"
  33 #include "classfile/symbolTable.hpp"
  34 #include "classfile/systemDictionary.hpp"
  35 #include "compiler/compilationPolicy.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "interpreter/linkResolver.hpp"
  38 #include "memory/allocation.inline.hpp"
  39 #include "memory/oopFactory.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/constantPool.hpp"
  42 #include "oops/cpCache.inline.hpp"
  43 #include "oops/fieldStreams.inline.hpp"

  44 #include "oops/klass.inline.hpp"
  45 #include "oops/method.inline.hpp"
  46 #include "oops/oop.inline.hpp"
  47 #include "prims/jvmtiExport.hpp"
  48 #include "prims/methodHandles.hpp"
  49 #include "runtime/fieldDescriptor.inline.hpp"
  50 #include "runtime/globals_extension.hpp"
  51 #include "runtime/handles.inline.hpp"
  52 #include "runtime/java.hpp"
  53 #include "utilities/copy.hpp"
  54 #include "utilities/macros.hpp"
  55 #include "utilities/utf8.hpp"
  56 
  57 #ifndef PRODUCT
  58 
  59 // ciReplay
  60 
  61 typedef struct _ciMethodDataRecord {
  62   const char* _klass_name;
  63   const char* _method_name;
  64   const char* _signature;
  65 
  66   int _state;
  67   int _current_mileage;
  68 
  69   intptr_t* _data;
  70   char*     _orig_data;
  71   Klass**   _classes;
  72   Method**  _methods;
  73   int*      _classes_offsets;
  74   int*      _methods_offsets;
  75   int       _data_length;
  76   int       _orig_data_length;
  77   int       _classes_length;
  78   int       _methods_length;
  79 } ciMethodDataRecord;
  80 
  81 typedef struct _ciMethodRecord {
  82   const char* _klass_name;
  83   const char* _method_name;
  84   const char* _signature;
  85 
  86   int _instructions_size;
  87   int _interpreter_invocation_count;
  88   int _interpreter_throwout_count;
  89   int _invocation_counter;
  90   int _backedge_counter;
  91 } ciMethodRecord;
  92 
  93 typedef struct _ciInlineRecord {
  94   const char* _klass_name;
  95   const char* _method_name;
  96   const char* _signature;
  97 
  98   int _inline_depth;
  99   int _inline_bci;
 100 } ciInlineRecord;
 101 
 102 class  CompileReplay;
 103 static CompileReplay* replay_state;
 104 
 105 class CompileReplay : public StackObj {
 106  private:
 107   FILE*   _stream;
 108   Thread* _thread;
 109   Handle  _protection_domain;
 110   Handle  _loader;
 111 
 112   GrowableArray<ciMethodRecord*>     _ci_method_records;
 113   GrowableArray<ciMethodDataRecord*> _ci_method_data_records;
 114 
 115   // Use pointer because we may need to return inline records
 116   // without destroying them.
 117   GrowableArray<ciInlineRecord*>*    _ci_inline_records;
 118 
 119   const char* _error_message;
 120 
 121   char* _bufptr;
 122   char* _buffer;
 123   int   _buffer_length;
 124   int   _buffer_pos;
 125 
 126   // "compile" data
 127   ciKlass* _iklass;
 128   Method*  _imethod;
 129   int      _entry_bci;
 130   int      _comp_level;
 131 
 132  public:
 133   CompileReplay(const char* filename, TRAPS) {
 134     _thread = THREAD;
 135     _loader = Handle(_thread, SystemDictionary::java_system_loader());
 136     _protection_domain = Handle();
 137 
 138     _stream = fopen(filename, "rt");
 139     if (_stream == NULL) {
 140       fprintf(stderr, "ERROR: Can't open replay file %s\n", filename);
 141     }
 142 
 143     _ci_inline_records = NULL;
 144     _error_message = NULL;
 145 
 146     _buffer_length = 32;
 147     _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length);
 148     _bufptr = _buffer;
 149     _buffer_pos = 0;
 150 
 151     _imethod = NULL;
 152     _iklass  = NULL;
 153     _entry_bci  = 0;
 154     _comp_level = 0;
 155 
 156     test();
 157   }
 158 
 159   ~CompileReplay() {
 160     if (_stream != NULL) fclose(_stream);
 161   }
 162 
 163   void test() {
 164     strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
 165     _bufptr = _buffer;
 166     assert(parse_int("test") == 1, "what");
 167     assert(parse_int("test") == 2, "what");
 168     assert(strcmp(parse_string(), "foo") == 0, "what");
 169     assert(parse_int("test") == 4, "what");
 170     assert(strcmp(parse_string(), "bar") == 0, "what");
 171     assert(parse_intptr_t("test") == 9, "what");
 172     assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
 173   }
 174 
 175   bool had_error() {
 176     return _error_message != NULL || _thread->has_pending_exception();
 177   }
 178 
 179   bool can_replay() {
 180     return !(_stream == NULL || had_error());
 181   }
 182 
 183   void report_error(const char* msg) {
 184     _error_message = msg;
 185     // Restore the _buffer contents for error reporting
 186     for (int i = 0; i < _buffer_pos; i++) {
 187       if (_buffer[i] == '\0') _buffer[i] = ' ';
 188     }
 189   }
 190 
 191   int parse_int(const char* label) {
 192     if (had_error()) {
 193       return 0;
 194     }
 195 
 196     int v = 0;
 197     int read;
 198     if (sscanf(_bufptr, "%i%n", &v, &read) != 1) {
 199       report_error(label);
 200     } else {
 201       _bufptr += read;
 202     }
 203     return v;
 204   }
 205 
 206   intptr_t parse_intptr_t(const char* label) {
 207     if (had_error()) {
 208       return 0;
 209     }
 210 
 211     intptr_t v = 0;
 212     int read;
 213     if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
 214       report_error(label);
 215     } else {
 216       _bufptr += read;
 217     }
 218     return v;
 219   }
 220 
 221   void skip_ws() {
 222     // Skip any leading whitespace
 223     while (*_bufptr == ' ' || *_bufptr == '\t') {
 224       _bufptr++;
 225     }
 226   }
 227 
 228 
 229   char* scan_and_terminate(char delim) {
 230     char* str = _bufptr;
 231     while (*_bufptr != delim && *_bufptr != '\0') {
 232       _bufptr++;
 233     }
 234     if (*_bufptr != '\0') {
 235       *_bufptr++ = '\0';
 236     }
 237     if (_bufptr == str) {
 238       // nothing here
 239       return NULL;
 240     }
 241     return str;
 242   }
 243 
 244   char* parse_string() {
 245     if (had_error()) return NULL;
 246 
 247     skip_ws();
 248     return scan_and_terminate(' ');
 249   }
 250 
 251   char* parse_quoted_string() {
 252     if (had_error()) return NULL;
 253 
 254     skip_ws();
 255 
 256     if (*_bufptr == '"') {
 257       _bufptr++;
 258       return scan_and_terminate('"');
 259     } else {
 260       return scan_and_terminate(' ');
 261     }
 262   }
 263 
 264   char* parse_escaped_string() {
 265     char* result = parse_quoted_string();
 266     if (result != NULL) {
 267       unescape_string(result);
 268     }
 269     return result;
 270   }
 271 
 272   // Look for the tag 'tag' followed by an
 273   bool parse_tag_and_count(const char* tag, int& length) {
 274     const char* t = parse_string();
 275     if (t == NULL) {
 276       return false;
 277     }
 278 
 279     if (strcmp(tag, t) != 0) {
 280       report_error(tag);
 281       return false;
 282     }
 283     length = parse_int("parse_tag_and_count");
 284     return !had_error();
 285   }
 286 
 287   // Parse a sequence of raw data encoded as bytes and return the
 288   // resulting data.
 289   char* parse_data(const char* tag, int& length) {
 290     int read_size = 0;
 291     if (!parse_tag_and_count(tag, read_size)) {
 292       return NULL;
 293     }
 294 
 295     int actual_size = sizeof(MethodData::CompilerCounters);
 296     char *result = NEW_RESOURCE_ARRAY(char, actual_size);
 297     int i = 0;
 298     if (read_size != actual_size) {
 299       tty->print_cr("Warning: ciMethodData parsing sees MethodData size %i in file, current is %i", read_size,
 300                     actual_size);
 301       // Replay serializes the entire MethodData, but the data is at the end.
 302       // If the MethodData instance size has changed, we can pad or truncate in the beginning
 303       int padding = actual_size - read_size;
 304       if (padding > 0) {
 305         // pad missing data with zeros
 306         tty->print_cr("- Padding MethodData");
 307         for (; i < padding; i++) {
 308           result[i] = 0;
 309         }
 310       } else if (padding < 0) {
 311         // drop some data
 312         tty->print_cr("- Truncating MethodData");
 313         for (int j = 0; j < -padding; j++) {
 314           int val = parse_int("data");
 315           // discard val
 316         }
 317       }
 318     }
 319 
 320     assert(i < actual_size, "At least some data must remain to be copied");
 321     for (; i < actual_size; i++) {
 322       int val = parse_int("data");
 323       result[i] = val;
 324     }
 325     length = actual_size;
 326     return result;
 327   }
 328 
 329   // Parse a standard chunk of data emitted as:
 330   //   'tag' <length> # # ...
 331   // Where each # is an intptr_t item
 332   intptr_t* parse_intptr_data(const char* tag, int& length) {
 333     if (!parse_tag_and_count(tag, length)) {
 334       return NULL;
 335     }
 336 
 337     intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
 338     for (int i = 0; i < length; i++) {
 339       skip_ws();
 340       intptr_t val = parse_intptr_t("data");
 341       result[i] = val;
 342     }
 343     return result;
 344   }
 345 
 346   // Parse a possibly quoted version of a symbol into a symbolOop
 347   Symbol* parse_symbol() {
 348     const char* str = parse_escaped_string();
 349     if (str != NULL) {
 350       Symbol* sym = SymbolTable::new_symbol(str);
 351       return sym;
 352     }
 353     return NULL;
 354   }
 355 
 356   bool parse_terminator() {
 357     char* terminator = parse_string();
 358     if (terminator != NULL && strcmp(terminator, ";") == 0) {
 359       return true;
 360     }
 361     return false;
 362   }
 363 
 364   // Parse a special hidden klass location syntax
 365   // syntax: @bci <klass> <name> <signature> <bci> <location>* ;
 366   // syntax: @cpi <klass> <cpi> <location>* ;
 367   Klass* parse_cp_ref(TRAPS) {
 368     JavaThread* thread = THREAD;
 369     oop obj = NULL;
 370     char* ref = parse_string();
 371     if (strcmp(ref, "bci") == 0) {
 372       Method* m = parse_method(CHECK_NULL);
 373       if (m == NULL) {
 374         return NULL;
 375       }
 376 
 377       InstanceKlass* ik = m->method_holder();
 378       const constantPoolHandle cp(Thread::current(), ik->constants());
 379 
 380       // invokedynamic or invokehandle
 381 
 382       methodHandle caller(Thread::current(), m);
 383       int bci = parse_int("bci");
 384       if (m->validate_bci(bci) != bci) {
 385         report_error("bad bci");
 386         return NULL;
 387       }
 388 
 389       ik->link_class(CHECK_NULL);
 390 
 391       Bytecode_invoke bytecode(caller, bci);
 392       int index = bytecode.index();
 393 
 394       ConstantPoolCacheEntry* cp_cache_entry = NULL;
 395       CallInfo callInfo;
 396       Bytecodes::Code bc = bytecode.invoke_code();
 397       LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, bc, CHECK_NULL);
 398       if (bytecode.is_invokedynamic()) {
 399         cp_cache_entry = cp->invokedynamic_cp_cache_entry_at(index);
 400         cp_cache_entry->set_dynamic_call(cp, callInfo);
 401       } else if (bytecode.is_invokehandle()) {
 402 #ifdef ASSERT
 403         Klass* holder = cp->klass_ref_at(index, CHECK_NULL);
 404         Symbol* name = cp->name_ref_at(index);
 405         assert(MethodHandles::is_signature_polymorphic_name(holder, name), "");
 406 #endif
 407         cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));
 408         cp_cache_entry->set_method_handle(cp, callInfo);
 409       } else {
 410         report_error("no dynamic invoke found");
 411         return NULL;
 412       }
 413       char* dyno_ref = parse_string();
 414       if (strcmp(dyno_ref, "<appendix>") == 0) {
 415         obj = cp_cache_entry->appendix_if_resolved(cp);
 416       } else if (strcmp(dyno_ref, "<adapter>") == 0) {
 417         if (!parse_terminator()) {
 418           report_error("no dynamic invoke found");
 419           return NULL;
 420         }
 421         Method* adapter = cp_cache_entry->f1_as_method();
 422         if (adapter == NULL) {
 423           report_error("no adapter found");
 424           return NULL;
 425         }
 426         return adapter->method_holder();
 427       } else if (strcmp(dyno_ref, "<bsm>") == 0) {
 428         int pool_index = cp_cache_entry->constant_pool_index();
 429         BootstrapInfo bootstrap_specifier(cp, pool_index, index);
 430         obj = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), thread);
 431       } else {
 432         report_error("unrecognized token");
 433         return NULL;
 434       }
 435     } else {
 436       // constant pool ref (MethodHandle)
 437       if (strcmp(ref, "cpi") != 0) {
 438         report_error("unexpected token");
 439         return NULL;
 440       }
 441 
 442       Klass* k = parse_klass(CHECK_NULL);
 443       if (k == NULL) {
 444         return NULL;
 445       }
 446       InstanceKlass* ik = InstanceKlass::cast(k);
 447       const constantPoolHandle cp(Thread::current(), ik->constants());
 448 
 449       int cpi = parse_int("cpi");
 450 
 451       if (cpi >= cp->length()) {
 452         report_error("bad cpi");
 453         return NULL;
 454       }
 455       if (!cp->tag_at(cpi).is_method_handle()) {
 456         report_error("no method handle found at cpi");
 457         return NULL;
 458       }
 459       {
 460         bool found_it;
 461         obj = cp->find_cached_constant_at(cpi, found_it, thread);
 462       }
 463     }
 464     Klass* k = NULL;
 465     if (obj != NULL) {
 466       skip_ws();
 467       // loop: read fields
 468       char* field = NULL;
 469       do {
 470         field = parse_string();
 471         if (field == NULL) {
 472           report_error("no field found");
 473           return NULL;
 474         }
 475         if (strcmp(field, ";") == 0) {
 476           break;
 477         }
 478         // raw Method*
 479         if (strcmp(field, "<vmtarget>") == 0) {
 480           Method* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
 481           k = (vmtarget == NULL) ? NULL : vmtarget->method_holder();
 482           if (k == NULL) {
 483             report_error("null vmtarget found");
 484             return NULL;
 485           }
 486           if (!parse_terminator()) {
 487             report_error("missing terminator");
 488             return NULL;
 489           }
 490           return k;
 491         }
 492         obj = ciReplay::obj_field(obj, field);
 493         // array
 494         if (obj != NULL && obj->is_objArray()) {
 495           objArrayOop arr = (objArrayOop)obj;
 496           int index = parse_int("index");
 497           if (index >= arr->length()) {
 498             report_error("bad array index");
 499             return NULL;
 500           }
 501           obj = arr->obj_at(index);
 502         }
 503       } while (obj != NULL);
 504       if (obj == NULL) {
 505         report_error("null field found");
 506         return NULL;
 507       }
 508       k = obj->klass();
 509     }
 510     return k;
 511   }
 512 
 513   // Parse a valid klass name and look it up
 514   // syntax: <name>
 515   // syntax: <constant pool ref>
 516   Klass* parse_klass(TRAPS) {
 517     skip_ws();
 518     // check for constant pool object reference (for a dynamic/hidden class)
 519     bool cp_ref = (*_bufptr == '@');
 520     if (cp_ref) {
 521       ++_bufptr;
 522       Klass* k = parse_cp_ref(CHECK_NULL);
 523       if (k != NULL && !k->is_hidden()) {
 524         report_error("expected hidden class");
 525         return NULL;
 526       }
 527       return k;
 528     }
 529     char* str = parse_escaped_string();
 530     Symbol* klass_name = SymbolTable::new_symbol(str);
 531     if (klass_name != NULL) {
 532       Klass* k = NULL;
 533       if (_iklass != NULL) {
 534         k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding();
 535       } else {
 536         k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 537       }
 538       if (HAS_PENDING_EXCEPTION) {
 539         oop throwable = PENDING_EXCEPTION;
 540         java_lang_Throwable::print(throwable, tty);
 541         tty->cr();
 542         report_error(str);
 543         if (ReplayIgnoreInitErrors) {
 544           CLEAR_PENDING_EXCEPTION;
 545           _error_message = NULL;
 546         }
 547         return NULL;
 548       }
 549       return k;
 550     }
 551     return NULL;
 552   }
 553 
 554   // Lookup a klass
 555   Klass* resolve_klass(const char* klass, TRAPS) {
 556     Symbol* klass_name = SymbolTable::new_symbol(klass);
 557     return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 558   }
 559 
 560   // Parse the standard tuple of <klass> <name> <signature>
 561   Method* parse_method(TRAPS) {
 562     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
 563     if (k == NULL) {
 564       report_error("Can't find holder klass");
 565       return NULL;
 566     }
 567     Symbol* method_name = parse_symbol();
 568     Symbol* method_signature = parse_symbol();
 569     Method* m = k->find_method(method_name, method_signature);
 570     if (m == NULL) {
 571       report_error("Can't find method");
 572     }
 573     return m;
 574   }
 575 
 576   int get_line(int c) {
 577     while(c != EOF) {
 578       if (_buffer_pos + 1 >= _buffer_length) {
 579         int new_length = _buffer_length * 2;
 580         // Next call will throw error in case of OOM.
 581         _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length);
 582         _buffer_length = new_length;
 583       }
 584       if (c == '\n') {
 585         c = getc(_stream); // get next char
 586         break;
 587       } else if (c == '\r') {
 588         // skip LF
 589       } else {
 590         _buffer[_buffer_pos++] = c;
 591       }
 592       c = getc(_stream);
 593     }
 594     // null terminate it, reset the pointer
 595     _buffer[_buffer_pos] = '\0'; // NL or EOF
 596     _buffer_pos = 0;
 597     _bufptr = _buffer;
 598     return c;
 599   }
 600 
 601   // Process each line of the replay file executing each command until
 602   // the file ends.
 603   void process(TRAPS) {
 604     int line_no = 1;
 605     int c = getc(_stream);
 606     while(c != EOF) {
 607       c = get_line(c);
 608       process_command(THREAD);
 609       if (had_error()) {
 610         tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 611         if (ReplayIgnoreInitErrors) {
 612           CLEAR_PENDING_EXCEPTION;
 613           _error_message = NULL;
 614         } else {
 615           return;
 616         }
 617       }
 618       line_no++;
 619     }
 620   }
 621 
 622   void process_command(TRAPS) {
 623     char* cmd = parse_string();
 624     if (cmd == NULL) {
 625       return;
 626     }
 627     if (strcmp("#", cmd) == 0) {
 628       // ignore
 629     } else if (strcmp("compile", cmd) == 0) {
 630       process_compile(CHECK);
 631     } else if (strcmp("ciMethod", cmd) == 0) {
 632       process_ciMethod(CHECK);
 633     } else if (strcmp("ciMethodData", cmd) == 0) {
 634       process_ciMethodData(CHECK);
 635     } else if (strcmp("staticfield", cmd) == 0) {
 636       process_staticfield(CHECK);
 637     } else if (strcmp("ciInstanceKlass", cmd) == 0) {
 638       process_ciInstanceKlass(CHECK);
 639     } else if (strcmp("instanceKlass", cmd) == 0) {
 640       process_instanceKlass(CHECK);
 641 #if INCLUDE_JVMTI
 642     } else if (strcmp("JvmtiExport", cmd) == 0) {
 643       process_JvmtiExport(CHECK);
 644 #endif // INCLUDE_JVMTI
 645     } else {
 646       report_error("unknown command");
 647     }
 648   }
 649 
 650   // validation of comp_level
 651   bool is_valid_comp_level(int comp_level) {
 652     const int msg_len = 256;
 653     char* msg = NULL;
 654     if (!is_compile(comp_level)) {
 655       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 656       jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
 657     } else if (is_c1_compile(comp_level) && !CompilerConfig::is_c1_enabled()) {
 658       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 659       jio_snprintf(msg, msg_len, "compilation level %d requires C1", comp_level);
 660     } else if (is_c2_compile(comp_level) && !CompilerConfig::is_c2_enabled()) {
 661       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 662       jio_snprintf(msg, msg_len, "compilation level %d requires C2", comp_level);
 663     }
 664     if (msg != NULL) {
 665       report_error(msg);
 666       return false;
 667     }
 668     return true;
 669   }
 670 
 671   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <klass> <name> <signature>)*
 672   void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) {
 673     _imethod    = m;
 674     _iklass     = imethod->holder();
 675     _entry_bci  = entry_bci;
 676     _comp_level = comp_level;
 677     int line_no = 1;
 678     int c = getc(_stream);
 679     while(c != EOF) {
 680       c = get_line(c);
 681       // Expecting only lines with "compile" command in inline replay file.
 682       char* cmd = parse_string();
 683       if (cmd == NULL || strcmp("compile", cmd) != 0) {
 684         return NULL;
 685       }
 686       process_compile(CHECK_NULL);
 687       if (had_error()) {
 688         tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 689         tty->print_cr("%s", _buffer);
 690         return NULL;
 691       }
 692       if (_ci_inline_records != NULL && _ci_inline_records->length() > 0) {
 693         // Found inlining record for the requested method.
 694         return _ci_inline_records;
 695       }
 696       line_no++;
 697     }
 698     return NULL;
 699   }
 700 
 701   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <klass> <name> <signature>)*
 702   void process_compile(TRAPS) {
 703     Method* method = parse_method(CHECK);
 704     if (had_error()) return;
 705     int entry_bci = parse_int("entry_bci");
 706     const char* comp_level_label = "comp_level";
 707     int comp_level = parse_int(comp_level_label);
 708     // old version w/o comp_level
 709     if (had_error() && (error_message() == comp_level_label)) {
 710       // use highest available tier
 711       comp_level = CompilationPolicy::highest_compile_level();
 712     }
 713     if (!is_valid_comp_level(comp_level)) {
 714       return;
 715     }
 716     if (_imethod != NULL) {
 717       // Replay Inlining
 718       if (entry_bci != _entry_bci || comp_level != _comp_level) {
 719         return;
 720       }
 721       const char* iklass_name  = _imethod->method_holder()->name()->as_utf8();
 722       const char* imethod_name = _imethod->name()->as_utf8();
 723       const char* isignature   = _imethod->signature()->as_utf8();
 724       const char* klass_name   = method->method_holder()->name()->as_utf8();
 725       const char* method_name  = method->name()->as_utf8();
 726       const char* signature    = method->signature()->as_utf8();
 727       if (strcmp(iklass_name,  klass_name)  != 0 ||
 728           strcmp(imethod_name, method_name) != 0 ||
 729           strcmp(isignature,   signature)   != 0) {
 730         return;
 731       }
 732     }
 733     int inline_count = 0;
 734     if (parse_tag_and_count("inline", inline_count)) {
 735       // Record inlining data
 736       _ci_inline_records = new GrowableArray<ciInlineRecord*>();
 737       for (int i = 0; i < inline_count; i++) {
 738         int depth = parse_int("inline_depth");
 739         int bci = parse_int("inline_bci");
 740         if (had_error()) {
 741           break;
 742         }
 743         Method* inl_method = parse_method(CHECK);
 744         if (had_error()) {
 745           break;
 746         }
 747         new_ciInlineRecord(inl_method, bci, depth);
 748       }
 749     }
 750     if (_imethod != NULL) {
 751       return; // Replay Inlining
 752     }
 753     InstanceKlass* ik = method->method_holder();
 754     ik->initialize(THREAD);
 755     if (HAS_PENDING_EXCEPTION) {
 756       oop throwable = PENDING_EXCEPTION;
 757       java_lang_Throwable::print(throwable, tty);
 758       tty->cr();
 759       if (ReplayIgnoreInitErrors) {
 760         CLEAR_PENDING_EXCEPTION;
 761         ik->set_init_state(InstanceKlass::fully_initialized);
 762       } else {
 763         return;
 764       }
 765     }
 766     // Make sure the existence of a prior compile doesn't stop this one
 767     CompiledMethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
 768     if (nm != NULL) {
 769       nm->make_not_entrant();
 770     }
 771     replay_state = this;
 772     CompileBroker::compile_method(methodHandle(THREAD, method), entry_bci, comp_level,
 773                                   methodHandle(), 0, CompileTask::Reason_Replay, THREAD);
 774     replay_state = NULL;
 775     reset();
 776   }
 777 
 778   // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
 779   void process_ciMethod(TRAPS) {
 780     Method* method = parse_method(CHECK);
 781     if (had_error()) return;
 782     ciMethodRecord* rec = new_ciMethod(method);
 783     rec->_invocation_counter = parse_int("invocation_counter");
 784     rec->_backedge_counter = parse_int("backedge_counter");
 785     rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count");
 786     rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count");
 787     rec->_instructions_size = parse_int("instructions_size");
 788   }
 789 
 790   // ciMethodData <klass> <name> <signature> <state> <current_mileage> orig <length> <byte>* data <length> <ptr>* oops <length> (<offset> <klass>)* methods <length> (<offset> <klass> <name> <signature>)*
 791   void process_ciMethodData(TRAPS) {
 792     Method* method = parse_method(CHECK);
 793     if (had_error()) return;
 794     /* just copied from Method, to build interpret data*/
 795 
 796     // To be properly initialized, some profiling in the MDO needs the
 797     // method to be rewritten (number of arguments at a call for
 798     // instance)
 799     method->method_holder()->link_class(CHECK);
 800     // Method::build_interpreter_method_data(method, CHECK);
 801     {
 802       // Grab a lock here to prevent multiple
 803       // MethodData*s from being created.
 804       MutexLocker ml(THREAD, MethodData_lock);
 805       if (method->method_data() == NULL) {
 806         ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 807         MethodData* method_data = MethodData::allocate(loader_data, methodHandle(THREAD, method), CHECK);
 808         method->set_method_data(method_data);
 809       }
 810     }
 811 
 812     // collect and record all the needed information for later
 813     ciMethodDataRecord* rec = new_ciMethodData(method);
 814     rec->_state = parse_int("state");
 815     rec->_current_mileage = parse_int("current_mileage");
 816 
 817     rec->_orig_data = parse_data("orig", rec->_orig_data_length);
 818     if (rec->_orig_data == NULL) {
 819       return;
 820     }
 821     rec->_data = parse_intptr_data("data", rec->_data_length);
 822     if (rec->_data == NULL) {
 823       return;
 824     }
 825     if (!parse_tag_and_count("oops", rec->_classes_length)) {
 826       return;
 827     }
 828     rec->_classes = NEW_RESOURCE_ARRAY(Klass*, rec->_classes_length);
 829     rec->_classes_offsets = NEW_RESOURCE_ARRAY(int, rec->_classes_length);
 830     for (int i = 0; i < rec->_classes_length; i++) {
 831       int offset = parse_int("offset");
 832       if (had_error()) {
 833         return;
 834       }
 835       Klass* k = parse_klass(CHECK);
 836       rec->_classes_offsets[i] = offset;
 837       rec->_classes[i] = k;
 838     }
 839 
 840     if (!parse_tag_and_count("methods", rec->_methods_length)) {
 841       return;
 842     }
 843     rec->_methods = NEW_RESOURCE_ARRAY(Method*, rec->_methods_length);
 844     rec->_methods_offsets = NEW_RESOURCE_ARRAY(int, rec->_methods_length);
 845     for (int i = 0; i < rec->_methods_length; i++) {
 846       int offset = parse_int("offset");
 847       if (had_error()) {
 848         return;
 849       }
 850       Method* m = parse_method(CHECK);
 851       rec->_methods_offsets[i] = offset;
 852       rec->_methods[i] = m;
 853     }
 854   }
 855 
 856   // instanceKlass <name>
 857   // instanceKlass <constant pool ref> # <original hidden class name>
 858   //
 859   // Loads and initializes the klass 'name'.  This can be used to
 860   // create particular class loading environments
 861   void process_instanceKlass(TRAPS) {
 862     // just load the referenced class
 863     Klass* k = parse_klass(CHECK);
 864     if (k == NULL) {
 865       return;
 866     }
 867     const char* comment = parse_string();
 868     bool is_comment = comment != NULL && strcmp(comment, "#") == 0;
 869     if (k->is_hidden() != is_comment) {
 870       report_error("hidden class with comment expected");
 871       return;
 872     }
 873     if (is_comment && Verbose) {
 874       const char* hidden = parse_string();
 875       tty->print_cr("Found %s for %s", k->name()->as_quoted_ascii(), hidden);
 876     }
 877   }
 878 
 879   // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag*
 880   //
 881   // Load the klass 'name' and link or initialize it.  Verify that the
 882   // constant pool is the same length as 'length' and make sure the
 883   // constant pool tags are in the same state.
 884   void process_ciInstanceKlass(TRAPS) {
 885     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 886     if (k == NULL) {
 887       return;
 888     }
 889     int is_linked = parse_int("is_linked");
 890     int is_initialized = parse_int("is_initialized");
 891     int length = parse_int("length");
 892     if (is_initialized) {
 893       k->initialize(THREAD);
 894       if (HAS_PENDING_EXCEPTION) {
 895         oop throwable = PENDING_EXCEPTION;
 896         java_lang_Throwable::print(throwable, tty);
 897         tty->cr();
 898         if (ReplayIgnoreInitErrors) {
 899           CLEAR_PENDING_EXCEPTION;
 900           k->set_init_state(InstanceKlass::fully_initialized);
 901         } else {
 902           return;
 903         }
 904       }
 905     } else if (is_linked) {
 906       k->link_class(CHECK);
 907     }
 908     ConstantPool* cp = k->constants();
 909     if (length != cp->length()) {
 910       report_error("constant pool length mismatch: wrong class files?");
 911       return;
 912     }
 913 
 914     int parsed_two_word = 0;
 915     for (int i = 1; i < length; i++) {
 916       int tag = parse_int("tag");
 917       if (had_error()) {
 918         return;
 919       }
 920       switch (cp->tag_at(i).value()) {
 921         case JVM_CONSTANT_UnresolvedClass: {
 922           if (tag == JVM_CONSTANT_Class) {
 923             tty->print_cr("Resolving klass %s at %d", cp->klass_name_at(i)->as_utf8(), i);
 924             Klass* k = cp->klass_at(i, CHECK);
 925           }
 926           break;
 927         }

 928         case JVM_CONSTANT_Long:
 929         case JVM_CONSTANT_Double:
 930           parsed_two_word = i + 1;
 931 
 932         case JVM_CONSTANT_ClassIndex:
 933         case JVM_CONSTANT_StringIndex:
 934         case JVM_CONSTANT_String:
 935         case JVM_CONSTANT_UnresolvedClassInError:
 936         case JVM_CONSTANT_Fieldref:
 937         case JVM_CONSTANT_Methodref:
 938         case JVM_CONSTANT_InterfaceMethodref:
 939         case JVM_CONSTANT_NameAndType:
 940         case JVM_CONSTANT_Utf8:
 941         case JVM_CONSTANT_Integer:
 942         case JVM_CONSTANT_Float:
 943         case JVM_CONSTANT_MethodHandle:
 944         case JVM_CONSTANT_MethodType:
 945         case JVM_CONSTANT_Dynamic:
 946         case JVM_CONSTANT_InvokeDynamic:
 947           if (tag != cp->tag_at(i).value()) {
 948             report_error("tag mismatch: wrong class files?");
 949             return;
 950           }
 951           break;
 952 
 953         case JVM_CONSTANT_Class:
 954           if (tag == JVM_CONSTANT_Class) {
 955           } else if (tag == JVM_CONSTANT_UnresolvedClass) {
 956             tty->print_cr("Warning: entry was unresolved in the replay data");
 957           } else {
 958             report_error("Unexpected tag");
 959             return;
 960           }
 961           break;
 962 
 963         case 0:
 964           if (parsed_two_word == i) continue;
 965 
 966         default:
 967           fatal("Unexpected tag: %d", cp->tag_at(i).value());
 968           break;
 969       }
 970 
 971     }
 972   }
 973 
 974   // staticfield <klass> <name> <signature> <value>
 975   //
 976   // Initialize a class and fill in the value for a static field.
 977   // This is useful when the compile was dependent on the value of
 978   // static fields but it's impossible to properly rerun the static
 979   // initializer.
 980   void process_staticfield(TRAPS) {
 981     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 982 
 983     if (k == NULL || ReplaySuppressInitializers == 0 ||
 984         (ReplaySuppressInitializers == 2 && k->class_loader() == NULL)) {
 985       return;
 986     }
 987 
 988     assert(k->is_initialized(), "must be");
 989 
 990     const char* field_name = parse_escaped_string();
 991     const char* field_signature = parse_string();
 992     fieldDescriptor fd;
 993     Symbol* name = SymbolTable::new_symbol(field_name);
 994     Symbol* sig = SymbolTable::new_symbol(field_signature);
 995     if (!k->find_local_field(name, sig, &fd) ||
 996         !fd.is_static() ||
 997         fd.has_initial_value()) {
 998       report_error(field_name);
 999       return;





















































1000     }

1001 
1002     oop java_mirror = k->java_mirror();
1003     if (field_signature[0] == JVM_SIGNATURE_ARRAY) {
1004       int length = parse_int("array length");
1005       oop value = NULL;
1006 
1007       if (field_signature[1] == JVM_SIGNATURE_ARRAY) {
1008         // multi dimensional array
1009         ArrayKlass* kelem = (ArrayKlass *)parse_klass(CHECK);
1010         if (kelem == NULL) {
1011           return;
1012         }
1013         int rank = 0;
1014         while (field_signature[rank] == JVM_SIGNATURE_ARRAY) {
1015           rank++;
1016         }
1017         jint* dims = NEW_RESOURCE_ARRAY(jint, rank);
1018         dims[0] = length;
1019         for (int i = 1; i < rank; i++) {
1020           dims[i] = 1; // These aren't relevant to the compiler
1021         }
1022         value = kelem->multi_allocate(rank, dims, CHECK);
1023       } else {
1024         if (strcmp(field_signature, "[B") == 0) {
1025           value = oopFactory::new_byteArray(length, CHECK);
1026         } else if (strcmp(field_signature, "[Z") == 0) {
1027           value = oopFactory::new_boolArray(length, CHECK);
1028         } else if (strcmp(field_signature, "[C") == 0) {
1029           value = oopFactory::new_charArray(length, CHECK);
1030         } else if (strcmp(field_signature, "[S") == 0) {
1031           value = oopFactory::new_shortArray(length, CHECK);
1032         } else if (strcmp(field_signature, "[F") == 0) {
1033           value = oopFactory::new_floatArray(length, CHECK);
1034         } else if (strcmp(field_signature, "[D") == 0) {
1035           value = oopFactory::new_doubleArray(length, CHECK);
1036         } else if (strcmp(field_signature, "[I") == 0) {
1037           value = oopFactory::new_intArray(length, CHECK);
1038         } else if (strcmp(field_signature, "[J") == 0) {
1039           value = oopFactory::new_longArray(length, CHECK);
1040         } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1041                    field_signature[1] == JVM_SIGNATURE_CLASS) {
1042           Klass* kelem = resolve_klass(field_signature + 1, CHECK);
1043           value = oopFactory::new_objArray(kelem, length, CHECK);




1044         } else {
1045           report_error("unhandled array staticfield");
1046         }
1047       }






















































































1048       java_mirror->obj_field_put(fd.offset(), value);
1049     } else {
1050       const char* string_value = parse_escaped_string();
1051       if (strcmp(field_signature, "I") == 0) {
1052         int value = atoi(string_value);
1053         java_mirror->int_field_put(fd.offset(), value);
1054       } else if (strcmp(field_signature, "B") == 0) {
1055         int value = atoi(string_value);
1056         java_mirror->byte_field_put(fd.offset(), value);
1057       } else if (strcmp(field_signature, "C") == 0) {
1058         int value = atoi(string_value);
1059         java_mirror->char_field_put(fd.offset(), value);
1060       } else if (strcmp(field_signature, "S") == 0) {
1061         int value = atoi(string_value);
1062         java_mirror->short_field_put(fd.offset(), value);
1063       } else if (strcmp(field_signature, "Z") == 0) {
1064         int value = atoi(string_value);
1065         java_mirror->bool_field_put(fd.offset(), value);
1066       } else if (strcmp(field_signature, "J") == 0) {
1067         jlong value;
1068         if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
1069           fprintf(stderr, "Error parsing long: %s\n", string_value);
1070           return;
1071         }
1072         java_mirror->long_field_put(fd.offset(), value);
1073       } else if (strcmp(field_signature, "F") == 0) {
1074         float value = atof(string_value);
1075         java_mirror->float_field_put(fd.offset(), value);
1076       } else if (strcmp(field_signature, "D") == 0) {
1077         double value = atof(string_value);
1078         java_mirror->double_field_put(fd.offset(), value);
1079       } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
1080         Handle value = java_lang_String::create_from_str(string_value, CHECK);
1081         java_mirror->obj_field_put(fd.offset(), value());
1082       } else if (field_signature[0] == JVM_SIGNATURE_CLASS) {
1083         Klass* k = resolve_klass(string_value, CHECK);
1084         oop value = InstanceKlass::cast(k)->allocate_instance(CHECK);
1085         java_mirror->obj_field_put(fd.offset(), value);
1086       } else {
1087         report_error("unhandled staticfield");
1088       }
1089     }
1090   }
1091 
1092 #if INCLUDE_JVMTI
1093   // JvmtiExport <field> <value>
1094   void process_JvmtiExport(TRAPS) {
1095     const char* field = parse_string();
1096     bool value = parse_int("JvmtiExport flag") != 0;
1097     if (strcmp(field, "can_access_local_variables") == 0) {
1098       JvmtiExport::set_can_access_local_variables(value);
1099     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
1100       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
1101     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
1102       JvmtiExport::set_can_post_on_exceptions(value);
1103     } else {
1104       report_error("Unrecognized JvmtiExport directive");
1105     }
1106   }
1107 #endif // INCLUDE_JVMTI
1108 
1109   // Create and initialize a record for a ciMethod
1110   ciMethodRecord* new_ciMethod(Method* method) {
1111     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
1112     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1113     rec->_method_name = method->name()->as_utf8();
1114     rec->_signature = method->signature()->as_utf8();
1115     _ci_method_records.append(rec);
1116     return rec;
1117   }
1118 
1119   // Lookup data for a ciMethod
1120   ciMethodRecord* find_ciMethodRecord(Method* method) {
1121     const char* klass_name =  method->method_holder()->name()->as_utf8();
1122     const char* method_name = method->name()->as_utf8();
1123     const char* signature = method->signature()->as_utf8();
1124     for (int i = 0; i < _ci_method_records.length(); i++) {
1125       ciMethodRecord* rec = _ci_method_records.at(i);
1126       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1127           strcmp(rec->_method_name, method_name) == 0 &&
1128           strcmp(rec->_signature, signature) == 0) {
1129         return rec;
1130       }
1131     }
1132     return NULL;
1133   }
1134 
1135   // Create and initialize a record for a ciMethodData
1136   ciMethodDataRecord* new_ciMethodData(Method* method) {
1137     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
1138     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1139     rec->_method_name = method->name()->as_utf8();
1140     rec->_signature = method->signature()->as_utf8();
1141     _ci_method_data_records.append(rec);
1142     return rec;
1143   }
1144 
1145   // Lookup data for a ciMethodData
1146   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
1147     const char* klass_name =  method->method_holder()->name()->as_utf8();
1148     const char* method_name = method->name()->as_utf8();
1149     const char* signature = method->signature()->as_utf8();
1150     for (int i = 0; i < _ci_method_data_records.length(); i++) {
1151       ciMethodDataRecord* rec = _ci_method_data_records.at(i);
1152       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1153           strcmp(rec->_method_name, method_name) == 0 &&
1154           strcmp(rec->_signature, signature) == 0) {
1155         return rec;
1156       }
1157     }
1158     return NULL;
1159   }
1160 
1161   // Create and initialize a record for a ciInlineRecord
1162   ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth) {
1163     ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
1164     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1165     rec->_method_name = method->name()->as_utf8();
1166     rec->_signature = method->signature()->as_utf8();
1167     rec->_inline_bci = bci;
1168     rec->_inline_depth = depth;
1169     _ci_inline_records->append(rec);
1170     return rec;
1171   }
1172 
1173   // Lookup inlining data for a ciMethod
1174   ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
1175     if (_ci_inline_records != NULL) {
1176       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1177     }
1178     return NULL;
1179   }
1180 
1181   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1182                                       Method* method, int bci, int depth) {
1183     if (records != NULL) {
1184       const char* klass_name  = method->method_holder()->name()->as_utf8();
1185       const char* method_name = method->name()->as_utf8();
1186       const char* signature   = method->signature()->as_utf8();
1187       for (int i = 0; i < records->length(); i++) {
1188         ciInlineRecord* rec = records->at(i);
1189         if ((rec->_inline_bci == bci) &&
1190             (rec->_inline_depth == depth) &&
1191             (strcmp(rec->_klass_name, klass_name) == 0) &&
1192             (strcmp(rec->_method_name, method_name) == 0) &&
1193             (strcmp(rec->_signature, signature) == 0)) {
1194           return rec;
1195         }
1196       }
1197     }
1198     return NULL;
1199   }
1200 
1201   const char* error_message() {
1202     return _error_message;
1203   }
1204 
1205   void reset() {
1206     _error_message = NULL;
1207     _ci_method_records.clear();
1208     _ci_method_data_records.clear();
1209   }
1210 
1211   // Take an ascii string contain \u#### escapes and convert it to utf8
1212   // in place.
1213   static void unescape_string(char* value) {
1214     char* from = value;
1215     char* to = value;
1216     while (*from != '\0') {
1217       if (*from != '\\') {
1218         *from++ = *to++;
1219       } else {
1220         switch (from[1]) {
1221           case 'u': {
1222             from += 2;
1223             jchar value=0;
1224             for (int i=0; i<4; i++) {
1225               char c = *from++;
1226               switch (c) {
1227                 case '0': case '1': case '2': case '3': case '4':
1228                 case '5': case '6': case '7': case '8': case '9':
1229                   value = (value << 4) + c - '0';
1230                   break;
1231                 case 'a': case 'b': case 'c':
1232                 case 'd': case 'e': case 'f':
1233                   value = (value << 4) + 10 + c - 'a';
1234                   break;
1235                 case 'A': case 'B': case 'C':
1236                 case 'D': case 'E': case 'F':
1237                   value = (value << 4) + 10 + c - 'A';
1238                   break;
1239                 default:
1240                   ShouldNotReachHere();
1241               }
1242             }
1243             UNICODE::convert_to_utf8(&value, 1, to);
1244             to++;
1245             break;
1246           }
1247           case 't': *to++ = '\t'; from += 2; break;
1248           case 'n': *to++ = '\n'; from += 2; break;
1249           case 'r': *to++ = '\r'; from += 2; break;
1250           case 'f': *to++ = '\f'; from += 2; break;
1251           default:
1252             ShouldNotReachHere();
1253         }
1254       }
1255     }
1256     *from = *to;
1257   }
1258 };
1259 
1260 void ciReplay::replay(TRAPS) {
1261   int exit_code = replay_impl(THREAD);
1262 
1263   Threads::destroy_vm();
1264 
1265   vm_exit(exit_code);
1266 }
1267 
1268 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1269   if (FLAG_IS_DEFAULT(InlineDataFile)) {
1270     tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1271     return NULL;
1272   }
1273 
1274   VM_ENTRY_MARK;
1275   // Load and parse the replay data
1276   CompileReplay rp(InlineDataFile, THREAD);
1277   if (!rp.can_replay()) {
1278     tty->print_cr("ciReplay: !rp.can_replay()");
1279     return NULL;
1280   }
1281   void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1282   if (HAS_PENDING_EXCEPTION) {
1283     Handle throwable(THREAD, PENDING_EXCEPTION);
1284     CLEAR_PENDING_EXCEPTION;
1285     java_lang_Throwable::print_stack_trace(throwable, tty);
1286     tty->cr();
1287     return NULL;
1288   }
1289 
1290   if (rp.had_error()) {
1291     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1292     return NULL;
1293   }
1294   return data;
1295 }
1296 
1297 int ciReplay::replay_impl(TRAPS) {
1298   HandleMark hm(THREAD);
1299   ResourceMark rm(THREAD);
1300 
1301   if (ReplaySuppressInitializers > 2) {
1302     // ReplaySuppressInitializers > 2 means that we want to allow
1303     // normal VM bootstrap but once we get into the replay itself
1304     // don't allow any intializers to be run.
1305     ReplaySuppressInitializers = 1;
1306   }
1307 
1308   if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1309     tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1310     return 1;
1311   }
1312 
1313   // Load and parse the replay data
1314   CompileReplay rp(ReplayDataFile, THREAD);
1315   int exit_code = 0;
1316   if (rp.can_replay()) {
1317     rp.process(THREAD);
1318   } else {
1319     exit_code = 1;
1320     return exit_code;
1321   }
1322 
1323   if (HAS_PENDING_EXCEPTION) {
1324     Handle throwable(THREAD, PENDING_EXCEPTION);
1325     CLEAR_PENDING_EXCEPTION;
1326     java_lang_Throwable::print_stack_trace(throwable, tty);
1327     tty->cr();
1328     exit_code = 2;
1329   }
1330 
1331   if (rp.had_error()) {
1332     tty->print_cr("Failed on %s", rp.error_message());
1333     exit_code = 1;
1334   }
1335   return exit_code;
1336 }
1337 
1338 void ciReplay::initialize(ciMethodData* m) {
1339   if (replay_state == NULL) {
1340     return;
1341   }
1342 
1343   ASSERT_IN_VM;
1344   ResourceMark rm;
1345 
1346   Method* method = m->get_MethodData()->method();
1347   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1348   if (rec == NULL) {
1349     // This indicates some mismatch with the original environment and
1350     // the replay environment though it's not always enough to
1351     // interfere with reproducing a bug
1352     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1353     method->print_name(tty);
1354     tty->cr();
1355   } else {
1356     m->_state = rec->_state;
1357     m->_current_mileage = rec->_current_mileage;
1358     if (rec->_data_length != 0) {
1359       assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) ||
1360              m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1361 
1362       // Write the correct ciObjects back into the profile data
1363       ciEnv* env = ciEnv::current();
1364       for (int i = 0; i < rec->_classes_length; i++) {
1365         Klass *k = rec->_classes[i];
1366         // In case this class pointer is is tagged, preserve the tag bits
1367         intptr_t status = 0;
1368         if (k != NULL) {
1369           status = ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]);
1370         }
1371         rec->_data[rec->_classes_offsets[i]] = status;
1372       }
1373       for (int i = 0; i < rec->_methods_length; i++) {
1374         Method *m = rec->_methods[i];
1375         *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) =
1376           env->get_metadata(m);
1377       }
1378       // Copy the updated profile data into place as intptr_ts
1379 #ifdef _LP64
1380       Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1381 #else
1382       Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1383 #endif
1384     }
1385 
1386     // copy in the original header
1387     Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1388   }
1389 }
1390 
1391 
1392 bool ciReplay::should_not_inline(ciMethod* method) {
1393   if (replay_state == NULL) {
1394     return false;
1395   }
1396   VM_ENTRY_MARK;
1397   // ciMethod without a record shouldn't be inlined.
1398   return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
1399 }
1400 
1401 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1402   if (data != NULL) {
1403     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1404     VM_ENTRY_MARK;
1405     // Inline record are ordered by bci and depth.
1406     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) != NULL;
1407   } else if (replay_state != NULL) {
1408     VM_ENTRY_MARK;
1409     // Inline record are ordered by bci and depth.
1410     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) != NULL;
1411   }
1412   return false;
1413 }
1414 
1415 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1416   if (data != NULL) {
1417     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1418     VM_ENTRY_MARK;
1419     // Inline record are ordered by bci and depth.
1420     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL;
1421   } else if (replay_state != NULL) {
1422     VM_ENTRY_MARK;
1423     // Inline record are ordered by bci and depth.
1424     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL;
1425   }
1426   return false;
1427 }
1428 
1429 void ciReplay::initialize(ciMethod* m) {
1430   if (replay_state == NULL) {
1431     return;
1432   }
1433 
1434   ASSERT_IN_VM;
1435   ResourceMark rm;
1436 
1437   Method* method = m->get_Method();
1438   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1439   if (rec == NULL) {
1440     // This indicates some mismatch with the original environment and
1441     // the replay environment though it's not always enough to
1442     // interfere with reproducing a bug
1443     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1444     method->print_name(tty);
1445     tty->cr();
1446   } else {
1447     EXCEPTION_CONTEXT;
1448     // m->_instructions_size = rec->_instructions_size;
1449     m->_instructions_size = -1;
1450     m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1451     m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1452     MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1453     guarantee(mcs != NULL, "method counters allocation failed");
1454     mcs->invocation_counter()->_counter = rec->_invocation_counter;
1455     mcs->backedge_counter()->_counter = rec->_backedge_counter;
1456   }
1457 }
1458 
1459 bool ciReplay::is_loaded(Method* method) {
1460   if (replay_state == NULL) {
1461     return true;
1462   }
1463 
1464   ASSERT_IN_VM;
1465   ResourceMark rm;
1466 
1467   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1468   return rec != NULL;
1469 }
1470 #endif // PRODUCT
1471 
1472 oop ciReplay::obj_field(oop obj, Symbol* name) {
1473   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
1474 
1475   do {
1476     if (!ik->has_nonstatic_fields()) {
1477       ik = ik->java_super();
1478       continue;
1479     }
1480 
1481     for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
1482       if (fs.access_flags().is_static()) {
1483         continue;
1484       }
1485       if (fs.name() == name) {
1486         int offset = fs.offset();
1487 #ifdef ASSERT
1488         fieldDescriptor fd = fs.field_descriptor();
1489         assert(fd.offset() == ik->field_offset(fd.index()), "!");
1490 #endif
1491         oop f = obj->obj_field(offset);
1492         return f;
1493       }
1494     }
1495 
1496     ik = ik->java_super();
1497   } while (ik != NULL);
1498   return NULL;
1499 }
1500 
1501 oop ciReplay::obj_field(oop obj, const char *name) {
1502   Symbol* fname = SymbolTable::probe(name, (int)strlen(name));
1503   if (fname == NULL) {
1504     return NULL;
1505   }
1506   return obj_field(obj, fname);
1507 }
--- EOF ---