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