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


1067         int rank = 0;
1068         while (field_signature[rank] == JVM_SIGNATURE_ARRAY) {
1069           rank++;
1070         }
1071         jint* dims = NEW_RESOURCE_ARRAY(jint, rank);
1072         dims[0] = length;
1073         for (int i = 1; i < rank; i++) {
1074           dims[i] = 1; // These aren't relevant to the compiler
1075         }
1076         value = kelem->multi_allocate(rank, dims, CHECK_(true));
1077       } else {
1078         if (strcmp(field_signature, "[B") == 0) {
1079           value = oopFactory::new_byteArray(length, CHECK_(true));
1080         } else if (strcmp(field_signature, "[Z") == 0) {
1081           value = oopFactory::new_boolArray(length, CHECK_(true));
1082         } else if (strcmp(field_signature, "[C") == 0) {
1083           value = oopFactory::new_charArray(length, CHECK_(true));
1084         } else if (strcmp(field_signature, "[S") == 0) {
1085           value = oopFactory::new_shortArray(length, CHECK_(true));
1086         } else if (strcmp(field_signature, "[F") == 0) {
1087           value = oopFactory::new_floatArray(length, CHECK_(true));
1088         } else if (strcmp(field_signature, "[D") == 0) {
1089           value = oopFactory::new_doubleArray(length, CHECK_(true));
1090         } else if (strcmp(field_signature, "[I") == 0) {
1091           value = oopFactory::new_intArray(length, CHECK_(true));
1092         } else if (strcmp(field_signature, "[J") == 0) {
1093           value = oopFactory::new_longArray(length, CHECK_(true));
1094         } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1095                    field_signature[1] == JVM_SIGNATURE_CLASS) {
1096           Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
1097           value = oopFactory::new_objArray(kelem, length, CHECK_(true));
1098         } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1099                    field_signature[1] == JVM_SIGNATURE_INLINE_TYPE) {
1100           Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
1101           value = oopFactory::new_valueArray(kelem, length, CHECK_(true));
1102         } else {
1103           report_error("unhandled array staticfield");
1104         }
1105       }
1106       java_mirror->obj_field_put(fd->offset(), value);
1107       return true;
1108     } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
1109       const char* string_value = parse_escaped_string();
1110       Handle value = java_lang_String::create_from_str(string_value, CHECK_(true));
1111       java_mirror->obj_field_put(fd->offset(), value());
1112       return true;
1113     } else if (field_signature[0] == 'L') {
1114       const char* instance = parse_escaped_string();
1115       Klass* k = resolve_klass(instance, CHECK_(true));
1116       oop value = InstanceKlass::cast(k)->allocate_instance(CHECK_(true));
1117       java_mirror->obj_field_put(fd->offset(), value);
1118       return true;
1119     }
1120     return false;
1121   }
1122 
1123   // Initialize a class and fill in the value for a static field.
1124   // This is useful when the compile was dependent on the value of
1125   // static fields but it's impossible to properly rerun the static
1126   // initializer.
1127   void process_staticfield(TRAPS) {
1128     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
1129 
1130     if (k == NULL || ReplaySuppressInitializers == 0 ||
1131         (ReplaySuppressInitializers == 2 && k->class_loader() == NULL)) {
1132       return;
1133     }
1134 
1135     assert(k->is_initialized(), "must be");
1136 
1137     const char* field_name = parse_escaped_string();
1138     const char* field_signature = parse_string();
1139     fieldDescriptor fd;
1140     Symbol* name = SymbolTable::new_symbol(field_name);
1141     Symbol* sig = SymbolTable::new_symbol(field_signature);
1142     if (!k->find_local_field(name, sig, &fd) ||
1143         !fd.is_static() ||
1144         fd.has_initial_value()) {
1145       report_error(field_name);
1146       return;
1147     }
1148 
1149     oop java_mirror = k->java_mirror();
1150     if (strcmp(field_signature, "I") == 0) {
1151       const char* string_value = parse_escaped_string();
1152       int value = atoi(string_value);
1153       java_mirror->int_field_put(fd.offset(), value);
1154     } else if (strcmp(field_signature, "B") == 0) {
1155       const char* string_value = parse_escaped_string();
1156       int value = atoi(string_value);
1157       java_mirror->byte_field_put(fd.offset(), value);
1158     } else if (strcmp(field_signature, "C") == 0) {
1159       const char* string_value = parse_escaped_string();
1160       int value = atoi(string_value);
1161       java_mirror->char_field_put(fd.offset(), value);
1162     } else if (strcmp(field_signature, "S") == 0) {
1163       const char* string_value = parse_escaped_string();
1164       int value = atoi(string_value);
1165       java_mirror->short_field_put(fd.offset(), value);
1166     } else if (strcmp(field_signature, "Z") == 0) {
1167       const char* string_value = parse_escaped_string();
1168       int value = atoi(string_value);
1169       java_mirror->bool_field_put(fd.offset(), value);
1170     } else if (strcmp(field_signature, "J") == 0) {
1171       const char* string_value = parse_escaped_string();
1172       jlong value;
1173       if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
1174         fprintf(stderr, "Error parsing long: %s\n", string_value);
1175         return;
1176       }
1177       java_mirror->long_field_put(fd.offset(), value);
1178     } else if (strcmp(field_signature, "F") == 0) {
1179       const char* string_value = parse_escaped_string();
1180       float value = atof(string_value);
1181       java_mirror->float_field_put(fd.offset(), value);
1182     } else if (strcmp(field_signature, "D") == 0) {
1183       const char* string_value = parse_escaped_string();
1184       double value = atof(string_value);
1185       java_mirror->double_field_put(fd.offset(), value);
1186     } else if (field_signature[0] == JVM_SIGNATURE_INLINE_TYPE) {
1187       Klass* kelem = resolve_klass(field_signature, CHECK);
1188       InlineKlass* vk = InlineKlass::cast(kelem);
1189       oop value = vk->allocate_instance(CHECK);
1190       InlineTypeFieldInitializer init_fields(value, this);
1191       vk->do_nonstatic_fields(&init_fields);
1192       java_mirror->obj_field_put(fd.offset(), value);
1193     } else {
1194       bool res = process_staticfield_reference(field_signature, java_mirror, &fd, CHECK);
1195       if (!res)  {



































1196         report_error("unhandled staticfield");
1197       }
1198     }
1199   }
1200 
1201 #if INCLUDE_JVMTI
1202   // JvmtiExport <field> <value>
1203   void process_JvmtiExport(TRAPS) {
1204     const char* field = parse_string();
1205     bool value = parse_int("JvmtiExport flag") != 0;
1206     if (strcmp(field, "can_access_local_variables") == 0) {
1207       JvmtiExport::set_can_access_local_variables(value);
1208     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
1209       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
1210     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
1211       JvmtiExport::set_can_post_on_exceptions(value);
1212     } else {
1213       report_error("Unrecognized JvmtiExport directive");
1214     }
1215   }
1216 #endif // INCLUDE_JVMTI
1217 
1218   // Create and initialize a record for a ciMethod
1219   ciMethodRecord* new_ciMethod(Method* method) {
1220     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
1221     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1222     rec->_method_name = method->name()->as_utf8();
1223     rec->_signature = method->signature()->as_utf8();
1224     _ci_method_records.append(rec);
1225     return rec;
1226   }
1227 
1228   // Lookup data for a ciMethod
1229   ciMethodRecord* find_ciMethodRecord(Method* method) {
1230     const char* klass_name =  method->method_holder()->name()->as_utf8();
1231     const char* method_name = method->name()->as_utf8();
1232     const char* signature = method->signature()->as_utf8();
1233     for (int i = 0; i < _ci_method_records.length(); i++) {
1234       ciMethodRecord* rec = _ci_method_records.at(i);
1235       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1236           strcmp(rec->_method_name, method_name) == 0 &&
1237           strcmp(rec->_signature, signature) == 0) {
1238         return rec;
1239       }
1240     }
1241     return NULL;
1242   }
1243 
1244   // Create and initialize a record for a ciMethodData
1245   ciMethodDataRecord* new_ciMethodData(Method* method) {
1246     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
1247     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1248     rec->_method_name = method->name()->as_utf8();
1249     rec->_signature = method->signature()->as_utf8();
1250     _ci_method_data_records.append(rec);
1251     return rec;
1252   }
1253 
1254   // Lookup data for a ciMethodData
1255   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
1256     const char* klass_name =  method->method_holder()->name()->as_utf8();
1257     const char* method_name = method->name()->as_utf8();
1258     const char* signature = method->signature()->as_utf8();
1259     for (int i = 0; i < _ci_method_data_records.length(); i++) {
1260       ciMethodDataRecord* rec = _ci_method_data_records.at(i);
1261       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1262           strcmp(rec->_method_name, method_name) == 0 &&
1263           strcmp(rec->_signature, signature) == 0) {
1264         return rec;
1265       }
1266     }
1267     return NULL;
1268   }
1269 
1270   // Create and initialize a record for a ciInlineRecord
1271   ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth) {
1272     ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
1273     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1274     rec->_method_name = method->name()->as_utf8();
1275     rec->_signature = method->signature()->as_utf8();
1276     rec->_inline_bci = bci;
1277     rec->_inline_depth = depth;
1278     _ci_inline_records->append(rec);
1279     return rec;
1280   }
1281 
1282   // Lookup inlining data for a ciMethod
1283   ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
1284     if (_ci_inline_records != NULL) {
1285       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1286     }
1287     return NULL;
1288   }
1289 
1290   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1291                                       Method* method, int bci, int depth) {
1292     if (records != NULL) {
1293       const char* klass_name  = method->method_holder()->name()->as_utf8();
1294       const char* method_name = method->name()->as_utf8();
1295       const char* signature   = method->signature()->as_utf8();
1296       for (int i = 0; i < records->length(); i++) {
1297         ciInlineRecord* rec = records->at(i);
1298         if ((rec->_inline_bci == bci) &&
1299             (rec->_inline_depth == depth) &&
1300             (strcmp(rec->_klass_name, klass_name) == 0) &&
1301             (strcmp(rec->_method_name, method_name) == 0) &&
1302             (strcmp(rec->_signature, signature) == 0)) {
1303           return rec;
1304         }
1305       }
1306     }
1307     return NULL;
1308   }
1309 
1310   const char* error_message() {
1311     return _error_message;
1312   }
1313 
1314   void reset() {
1315     _error_message = NULL;
1316     _ci_method_records.clear();
1317     _ci_method_data_records.clear();
1318   }
1319 
1320   // Take an ascii string contain \u#### escapes and convert it to utf8
1321   // in place.
1322   static void unescape_string(char* value) {
1323     char* from = value;
1324     char* to = value;
1325     while (*from != '\0') {
1326       if (*from != '\\') {
1327         *from++ = *to++;
1328       } else {
1329         switch (from[1]) {
1330           case 'u': {
1331             from += 2;
1332             jchar value=0;
1333             for (int i=0; i<4; i++) {
1334               char c = *from++;
1335               switch (c) {
1336                 case '0': case '1': case '2': case '3': case '4':
1337                 case '5': case '6': case '7': case '8': case '9':
1338                   value = (value << 4) + c - '0';
1339                   break;
1340                 case 'a': case 'b': case 'c':
1341                 case 'd': case 'e': case 'f':
1342                   value = (value << 4) + 10 + c - 'a';
1343                   break;
1344                 case 'A': case 'B': case 'C':
1345                 case 'D': case 'E': case 'F':
1346                   value = (value << 4) + 10 + c - 'A';
1347                   break;
1348                 default:
1349                   ShouldNotReachHere();
1350               }
1351             }
1352             UNICODE::convert_to_utf8(&value, 1, to);
1353             to++;
1354             break;
1355           }
1356           case 't': *to++ = '\t'; from += 2; break;
1357           case 'n': *to++ = '\n'; from += 2; break;
1358           case 'r': *to++ = '\r'; from += 2; break;
1359           case 'f': *to++ = '\f'; from += 2; break;
1360           default:
1361             ShouldNotReachHere();
1362         }
1363       }
1364     }
1365     *from = *to;
1366   }
1367 };
1368 
1369 void ciReplay::replay(TRAPS) {
1370   int exit_code = replay_impl(THREAD);
1371 
1372   Threads::destroy_vm();
1373 
1374   vm_exit(exit_code);
1375 }
1376 
1377 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1378   if (FLAG_IS_DEFAULT(InlineDataFile)) {
1379     tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1380     return NULL;
1381   }
1382 
1383   VM_ENTRY_MARK;
1384   // Load and parse the replay data
1385   CompileReplay rp(InlineDataFile, THREAD);
1386   if (!rp.can_replay()) {
1387     tty->print_cr("ciReplay: !rp.can_replay()");
1388     return NULL;
1389   }
1390   void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1391   if (HAS_PENDING_EXCEPTION) {
1392     Handle throwable(THREAD, PENDING_EXCEPTION);
1393     CLEAR_PENDING_EXCEPTION;
1394     java_lang_Throwable::print_stack_trace(throwable, tty);
1395     tty->cr();
1396     return NULL;
1397   }
1398 
1399   if (rp.had_error()) {
1400     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1401     return NULL;
1402   }
1403   return data;
1404 }
1405 
1406 int ciReplay::replay_impl(TRAPS) {
1407   HandleMark hm(THREAD);
1408   ResourceMark rm(THREAD);
1409 
1410   if (ReplaySuppressInitializers > 2) {
1411     // ReplaySuppressInitializers > 2 means that we want to allow
1412     // normal VM bootstrap but once we get into the replay itself
1413     // don't allow any intializers to be run.
1414     ReplaySuppressInitializers = 1;
1415   }
1416 
1417   if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1418     tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1419     return 1;
1420   }
1421 
1422   // Load and parse the replay data
1423   CompileReplay rp(ReplayDataFile, THREAD);
1424   int exit_code = 0;
1425   if (rp.can_replay()) {
1426     rp.process(THREAD);
1427   } else {
1428     exit_code = 1;
1429     return exit_code;
1430   }
1431 
1432   if (HAS_PENDING_EXCEPTION) {
1433     Handle throwable(THREAD, PENDING_EXCEPTION);
1434     CLEAR_PENDING_EXCEPTION;
1435     java_lang_Throwable::print_stack_trace(throwable, tty);
1436     tty->cr();
1437     exit_code = 2;
1438   }
1439 
1440   if (rp.had_error()) {
1441     tty->print_cr("Failed on %s", rp.error_message());
1442     exit_code = 1;
1443   }
1444   return exit_code;
1445 }
1446 
1447 void ciReplay::initialize(ciMethodData* m) {
1448   if (replay_state == NULL) {
1449     return;
1450   }
1451 
1452   ASSERT_IN_VM;
1453   ResourceMark rm;
1454 
1455   Method* method = m->get_MethodData()->method();
1456   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1457   if (rec == NULL) {
1458     // This indicates some mismatch with the original environment and
1459     // the replay environment though it's not always enough to
1460     // interfere with reproducing a bug
1461     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1462     method->print_name(tty);
1463     tty->cr();
1464   } else {
1465     m->_state = rec->_state;
1466     m->_current_mileage = rec->_current_mileage;
1467     if (rec->_data_length != 0) {
1468       assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) ||
1469              m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1470 
1471       // Write the correct ciObjects back into the profile data
1472       ciEnv* env = ciEnv::current();
1473       for (int i = 0; i < rec->_classes_length; i++) {
1474         Klass *k = rec->_classes[i];
1475         // In case this class pointer is is tagged, preserve the tag bits
1476         intptr_t status = 0;
1477         if (k != NULL) {
1478           status = ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]);
1479         }
1480         rec->_data[rec->_classes_offsets[i]] = status;
1481       }
1482       for (int i = 0; i < rec->_methods_length; i++) {
1483         Method *m = rec->_methods[i];
1484         *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) =
1485           env->get_metadata(m);
1486       }
1487       // Copy the updated profile data into place as intptr_ts
1488 #ifdef _LP64
1489       Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1490 #else
1491       Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1492 #endif
1493     }
1494 
1495     // copy in the original header
1496     Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1497   }
1498 }
1499 
1500 
1501 bool ciReplay::should_not_inline(ciMethod* method) {
1502   if (replay_state == NULL) {
1503     return false;
1504   }
1505   VM_ENTRY_MARK;
1506   // ciMethod without a record shouldn't be inlined.
1507   return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
1508 }
1509 
1510 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1511   if (data != NULL) {
1512     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1513     VM_ENTRY_MARK;
1514     // Inline record are ordered by bci and depth.
1515     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) != NULL;
1516   } else if (replay_state != NULL) {
1517     VM_ENTRY_MARK;
1518     // Inline record are ordered by bci and depth.
1519     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) != NULL;
1520   }
1521   return false;
1522 }
1523 
1524 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1525   if (data != NULL) {
1526     GrowableArray<ciInlineRecord*>*  records = (GrowableArray<ciInlineRecord*>*)data;
1527     VM_ENTRY_MARK;
1528     // Inline record are ordered by bci and depth.
1529     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL;
1530   } else if (replay_state != NULL) {
1531     VM_ENTRY_MARK;
1532     // Inline record are ordered by bci and depth.
1533     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL;
1534   }
1535   return false;
1536 }
1537 
1538 void ciReplay::initialize(ciMethod* m) {
1539   if (replay_state == NULL) {
1540     return;
1541   }
1542 
1543   ASSERT_IN_VM;
1544   ResourceMark rm;
1545 
1546   Method* method = m->get_Method();
1547   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1548   if (rec == NULL) {
1549     // This indicates some mismatch with the original environment and
1550     // the replay environment though it's not always enough to
1551     // interfere with reproducing a bug
1552     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1553     method->print_name(tty);
1554     tty->cr();
1555   } else {
1556     EXCEPTION_CONTEXT;
1557     // m->_instructions_size = rec->_instructions_size;
1558     m->_instructions_size = -1;
1559     m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1560     m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1561     MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1562     guarantee(mcs != NULL, "method counters allocation failed");
1563     mcs->invocation_counter()->_counter = rec->_invocation_counter;
1564     mcs->backedge_counter()->_counter = rec->_backedge_counter;
1565   }
1566 }
1567 
1568 bool ciReplay::is_loaded(Method* method) {
1569   if (replay_state == NULL) {
1570     return true;
1571   }
1572 
1573   ASSERT_IN_VM;
1574   ResourceMark rm;
1575 
1576   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1577   return rec != NULL;
1578 }
1579 #endif // PRODUCT
1580 
1581 oop ciReplay::obj_field(oop obj, Symbol* name) {
1582   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
1583 
1584   do {
1585     if (!ik->has_nonstatic_fields()) {
1586       ik = ik->java_super();
1587       continue;
1588     }
1589 
1590     for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
1591       if (fs.access_flags().is_static()) {
1592         continue;
1593       }
1594       if (fs.name() == name) {
1595         int offset = fs.offset();
1596 #ifdef ASSERT
1597         fieldDescriptor fd = fs.field_descriptor();
1598         assert(fd.offset() == ik->field_offset(fd.index()), "!");
1599 #endif
1600         oop f = obj->obj_field(offset);
1601         return f;
1602       }
1603     }
1604 
1605     ik = ik->java_super();
1606   } while (ik != NULL);
1607   return NULL;
1608 }
1609 
1610 oop ciReplay::obj_field(oop obj, const char *name) {
1611   Symbol* fname = SymbolTable::probe(name, (int)strlen(name));
1612   if (fname == NULL) {
1613     return NULL;
1614   }
1615   return obj_field(obj, fname);
1616 }
--- EOF ---