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