1 /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "ci/ciMethodData.hpp"
  26 #include "ci/ciReplay.hpp"
  27 #include "ci/ciUtilities.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/oopFactory.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "utilities/copy.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 #ifndef PRODUCT
  36 
  37 // ciReplay
  38 
  39 typedef struct _ciMethodDataRecord {
  40   const char* klass;
  41   const char* method;
  42   const char* signature;
  43   int state;
  44   int current_mileage;
  45   intptr_t* data;
  46   int data_length;
  47   char* orig_data;
  48   int orig_data_length;
  49   int oops_length;
  50   jobject* oops_handles;
  51   int* oops_offsets;
  52 } ciMethodDataRecord;
  53 
  54 typedef struct _ciMethodRecord {
  55   const char* klass;
  56   const char* method;
  57   const char* signature;
  58   int instructions_size;
  59   int interpreter_invocation_count;
  60   int interpreter_throwout_count;
  61   int invocation_counter;
  62   int backedge_counter;
  63 } ciMethodRecord;
  64 
  65 class CompileReplay;
  66 static CompileReplay* replay_state;
  67 
  68 class CompileReplay : public StackObj {
  69  private:
  70   FILE*   stream;
  71   Thread* thread;
  72   Handle  protection_domain;
  73   Handle  loader;
  74 
  75   GrowableArray<ciMethodRecord*>     ci_method_records;
  76   GrowableArray<ciMethodDataRecord*> ci_method_data_records;
  77 
  78   const char* _error_message;
  79 
  80   char* bufptr;
  81   char* buffer;
  82   int   buffer_length;
  83   int   buffer_end;
  84   int   line_no;
  85 
  86  public:
  87   CompileReplay(const char* filename, TRAPS) {
  88     thread = THREAD;
  89     loader = Handle(thread, SystemDictionary::java_system_loader());
  90     stream = fopen(filename, "rt");
  91     if (stream == NULL) {
  92       fprintf(stderr, "Can't open replay file %s\n", filename);
  93     }
  94     buffer_length = 32;
  95     buffer = NEW_RESOURCE_ARRAY(char, buffer_length);
  96     _error_message = NULL;
  97 
  98     test();
  99   }
 100 
 101   ~CompileReplay() {
 102     if (stream != NULL) fclose(stream);
 103   }
 104 
 105   void test() {
 106     strcpy(buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
 107     bufptr = buffer;
 108     assert(parse_int("test") == 1, "what");
 109     assert(parse_int("test") == 2, "what");
 110     assert(strcmp(parse_string(), "foo") == 0, "what");
 111     assert(parse_int("test") == 4, "what");
 112     assert(strcmp(parse_string(), "bar") == 0, "what");
 113     assert(parse_intptr_t("test") == 9, "what");
 114     assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
 115   }
 116 
 117   bool had_error() {
 118     return _error_message != NULL || thread->has_pending_exception();
 119   }
 120 
 121   bool can_replay() {
 122     return !(stream == NULL || had_error());
 123   }
 124 
 125   void report_error(const char* msg) {
 126     _error_message = msg;
 127     // Restore the buffer contents for error reporting
 128     for (int i = 0; i < buffer_end; i++) {
 129       if (buffer[i] == '\0') buffer[i] = ' ';
 130     }
 131   }
 132 
 133   int parse_int(const char* label) {
 134     if (had_error()) {
 135       return 0;
 136     }
 137 
 138     int v = 0;
 139     int read;
 140     if (sscanf(bufptr, "%i%n", &v, &read) != 1) {
 141       report_error(label);
 142     } else {
 143       bufptr += read;
 144     }
 145     return v;
 146   }
 147 
 148   intptr_t parse_intptr_t(const char* label) {
 149     if (had_error()) {
 150       return 0;
 151     }
 152 
 153     intptr_t v = 0;
 154     int read;
 155     if (sscanf(bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
 156       report_error(label);
 157     } else {
 158       bufptr += read;
 159     }
 160     return v;
 161   }
 162 
 163   void skip_ws() {
 164     // Skip any leading whitespace
 165     while (*bufptr == ' ' || *bufptr == '\t') {
 166       bufptr++;
 167     }
 168   }
 169 
 170 
 171   char* scan_and_terminate(char delim) {
 172     char* str = bufptr;
 173     while (*bufptr != delim && *bufptr != '\0') {
 174       bufptr++;
 175     }
 176     if (*bufptr != '\0') {
 177       *bufptr++ = '\0';
 178     }
 179     if (bufptr == str) {
 180       // nothing here
 181       return NULL;
 182     }
 183     return str;
 184   }
 185 
 186   char* parse_string() {
 187     if (had_error()) return NULL;
 188 
 189     skip_ws();
 190     return scan_and_terminate(' ');
 191   }
 192 
 193   char* parse_quoted_string() {
 194     if (had_error()) return NULL;
 195 
 196     skip_ws();
 197 
 198     if (*bufptr == '"') {
 199       bufptr++;
 200       return scan_and_terminate('"');
 201     } else {
 202       return scan_and_terminate(' ');
 203     }
 204   }
 205 
 206   const char* parse_escaped_string() {
 207     char* result = parse_quoted_string();
 208     if (result != NULL) {
 209       unescape_string(result);
 210     }
 211     return result;
 212   }
 213 
 214   // Look for the tag 'tag' followed by an
 215   bool parse_tag_and_count(const char* tag, int& length) {
 216     const char* t = parse_string();
 217     if (t == NULL) {
 218       return false;
 219     }
 220 
 221     if (strcmp(tag, t) != 0) {
 222       report_error(tag);
 223       return false;
 224     }
 225     length = parse_int("parse_tag_and_count");
 226     return !had_error();
 227   }
 228 
 229   // Parse a sequence of raw data encoded as bytes and return the
 230   // resulting data.
 231   char* parse_data(const char* tag, int& length) {
 232     if (!parse_tag_and_count(tag, length)) {
 233       return NULL;
 234     }
 235 
 236     char * result = NEW_RESOURCE_ARRAY(char, length);
 237     for (int i = 0; i < length; i++) {
 238       int val = parse_int("data");
 239       result[i] = val;
 240     }
 241     return result;
 242   }
 243 
 244   // Parse a standard chunk of data emitted as:
 245   //   'tag' <length> # # ...
 246   // Where each # is an intptr_t item
 247   intptr_t* parse_intptr_data(const char* tag, int& length) {
 248     if (!parse_tag_and_count(tag, length)) {
 249       return NULL;
 250     }
 251 
 252     intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
 253     for (int i = 0; i < length; i++) {
 254       skip_ws();
 255       intptr_t val = parse_intptr_t("data");
 256       result[i] = val;
 257     }
 258     return result;
 259   }
 260 
 261   // Parse a possibly quoted version of a symbol into a symbolOop
 262   Symbol* parse_symbol(TRAPS) {
 263     const char* str = parse_escaped_string();
 264     if (str != NULL) {
 265       Symbol* sym = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
 266       return sym;
 267     }
 268     return NULL;
 269   }
 270 
 271   // Parse a valid klass name and look it up
 272   Klass* parse_klass(TRAPS) {
 273     const char* str = parse_escaped_string();
 274     Symbol* klass_name = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
 275     if (klass_name != NULL) {
 276       Klass* k = SystemDictionary::resolve_or_fail(klass_name, loader, protection_domain, true, THREAD);
 277       if (HAS_PENDING_EXCEPTION) {
 278         oop throwable = PENDING_EXCEPTION;
 279         java_lang_Throwable::print(throwable, tty);
 280         tty->cr();
 281         report_error(str);
 282         return NULL;
 283       }
 284       return k;
 285     }
 286     return NULL;
 287   }
 288 
 289   // Lookup a klass
 290   Klass* resolve_klass(const char* klass, TRAPS) {
 291     Symbol* klass_name = SymbolTable::lookup(klass, (int)strlen(klass), CHECK_NULL);
 292     return SystemDictionary::resolve_or_fail(klass_name, loader, protection_domain, true, CHECK_NULL);
 293   }
 294 
 295   // Parse the standard tuple of <klass> <name> <signature>
 296   Method* parse_method(TRAPS) {
 297     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
 298     Symbol* method_name = parse_symbol(CHECK_NULL);
 299     Symbol* method_signature = parse_symbol(CHECK_NULL);
 300     Method* m = k->find_method(method_name, method_signature);
 301     if (m == NULL) {
 302       report_error("can't find method");
 303     }
 304     return m;
 305   }
 306 
 307   // Process each line of the replay file executing each command until
 308   // the file ends.
 309   void process(TRAPS) {
 310     line_no = 1;
 311     int pos = 0;
 312     int c = getc(stream);
 313     while(c != EOF) {
 314       if (pos + 1 >= buffer_length) {
 315         int newl = buffer_length * 2;
 316         char* newb = NEW_RESOURCE_ARRAY(char, newl);
 317         memcpy(newb, buffer, pos);
 318         buffer = newb;
 319         buffer_length = newl;
 320       }
 321       if (c == '\n') {
 322         // null terminate it, reset the pointer and process the line
 323         buffer[pos] = '\0';
 324         buffer_end = pos++;
 325         bufptr = buffer;
 326         process_command(CHECK);
 327         if (had_error()) {
 328           tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 329           tty->print_cr("%s", buffer);
 330           assert(false, "error");
 331           return;
 332         }
 333         pos = 0;
 334         buffer_end = 0;
 335         line_no++;
 336       } else if (c == '\r') {
 337         // skip LF
 338       } else {
 339         buffer[pos++] = c;
 340       }
 341       c = getc(stream);
 342     }
 343   }
 344 
 345   void process_command(TRAPS) {
 346     char* cmd = parse_string();
 347     if (cmd == NULL) {
 348       return;
 349     }
 350     if (strcmp("#", cmd) == 0) {
 351       // ignore
 352     } else if (strcmp("compile", cmd) == 0) {
 353       process_compile(CHECK);
 354     } else if (strcmp("ciMethod", cmd) == 0) {
 355       process_ciMethod(CHECK);
 356     } else if (strcmp("ciMethodData", cmd) == 0) {
 357       process_ciMethodData(CHECK);
 358     } else if (strcmp("staticfield", cmd) == 0) {
 359       process_staticfield(CHECK);
 360     } else if (strcmp("ciInstanceKlass", cmd) == 0) {
 361       process_ciInstanceKlass(CHECK);
 362     } else if (strcmp("instanceKlass", cmd) == 0) {
 363       process_instanceKlass(CHECK);
 364 #if INCLUDE_JVMTI
 365     } else if (strcmp("JvmtiExport", cmd) == 0) {
 366       process_JvmtiExport(CHECK);
 367 #endif // INCLUDE_JVMTI
 368     } else {
 369       report_error("unknown command");
 370     }
 371   }
 372 
 373   // validation of comp_level
 374   bool is_valid_comp_level(int comp_level) {
 375     const int msg_len = 256;
 376     char* msg = NULL;
 377     if (!is_compile(comp_level)) {
 378       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 379       jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
 380     } else if (!TieredCompilation && (comp_level != CompLevel_highest_tier)) {
 381       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 382       switch (comp_level) {
 383         case CompLevel_simple:
 384           jio_snprintf(msg, msg_len, "compilation level %d requires Client VM or TieredCompilation", comp_level);
 385           break;
 386         case CompLevel_full_optimization:
 387           jio_snprintf(msg, msg_len, "compilation level %d requires Server VM", comp_level);
 388           break;
 389         default:
 390           jio_snprintf(msg, msg_len, "compilation level %d requires TieredCompilation", comp_level);
 391       }
 392     }
 393     if (msg != NULL) {
 394       report_error(msg);
 395       return false;
 396     }
 397     return true;
 398   }
 399 
 400   // compile <klass> <name> <signature> <entry_bci> <comp_level>
 401   void process_compile(TRAPS) {
 402     // methodHandle method;
 403     Method* method = parse_method(CHECK);
 404     int entry_bci = parse_int("entry_bci");
 405     const char* comp_level_label = "comp_level";
 406     int comp_level = parse_int(comp_level_label);
 407     // old version w/o comp_level
 408     if (had_error() && (error_message() == comp_level_label)) {
 409       comp_level = CompLevel_full_optimization;
 410     }
 411     if (!is_valid_comp_level(comp_level)) {
 412       return;
 413     }
 414     Klass* k = method->method_holder();
 415     ((InstanceKlass*)k)->initialize(THREAD);
 416     if (HAS_PENDING_EXCEPTION) {
 417       oop throwable = PENDING_EXCEPTION;
 418       java_lang_Throwable::print(throwable, tty);
 419       tty->cr();
 420       if (ReplayIgnoreInitErrors) {
 421         CLEAR_PENDING_EXCEPTION;
 422         ((InstanceKlass*)k)->set_init_state(InstanceKlass::fully_initialized);
 423       } else {
 424         return;
 425       }
 426     }
 427     // Make sure the existence of a prior compile doesn't stop this one
 428     nmethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
 429     if (nm != NULL) {
 430       nm->make_not_entrant();
 431     }
 432     replay_state = this;
 433     CompileBroker::compile_method(method, entry_bci, comp_level,
 434                                   methodHandle(), 0, "replay", THREAD);
 435     replay_state = NULL;
 436     reset();
 437   }
 438 
 439   // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
 440   //
 441   //
 442   void process_ciMethod(TRAPS) {
 443     Method* method = parse_method(CHECK);
 444     ciMethodRecord* rec = new_ciMethod(method);
 445     rec->invocation_counter = parse_int("invocation_counter");
 446     rec->backedge_counter = parse_int("backedge_counter");
 447     rec->interpreter_invocation_count = parse_int("interpreter_invocation_count");
 448     rec->interpreter_throwout_count = parse_int("interpreter_throwout_count");
 449     rec->instructions_size = parse_int("instructions_size");
 450   }
 451 
 452   // ciMethodData <klass> <name> <signature> <state> <current mileage> orig <length> # # ... data <length> # # ... oops <length>
 453   void process_ciMethodData(TRAPS) {
 454     Method* method = parse_method(CHECK);
 455     /* jsut copied from Method, to build interpret data*/
 456     if (InstanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) {
 457       return;
 458     }
 459     // methodOopDesc::build_interpreter_method_data(method, CHECK);
 460     {
 461       // Grab a lock here to prevent multiple
 462       // MethodData*s from being created.
 463       MutexLocker ml(MethodData_lock, THREAD);
 464       if (method->method_data() == NULL) {
 465         ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 466         MethodData* method_data = MethodData::allocate(loader_data, method, CHECK);
 467         method->set_method_data(method_data);
 468       }
 469     }
 470 
 471     // collect and record all the needed information for later
 472     ciMethodDataRecord* rec = new_ciMethodData(method);
 473     rec->state = parse_int("state");
 474     rec->current_mileage = parse_int("current_mileage");
 475 
 476     rec->orig_data = parse_data("orig", rec->orig_data_length);
 477     if (rec->orig_data == NULL) {
 478       return;
 479     }
 480     rec->data = parse_intptr_data("data", rec->data_length);
 481     if (rec->data == NULL) {
 482       return;
 483     }
 484     if (!parse_tag_and_count("oops", rec->oops_length)) {
 485       return;
 486     }
 487     rec->oops_handles = NEW_RESOURCE_ARRAY(jobject, rec->oops_length);
 488     rec->oops_offsets = NEW_RESOURCE_ARRAY(int, rec->oops_length);
 489     for (int i = 0; i < rec->oops_length; i++) {
 490       int offset = parse_int("offset");
 491       if (had_error()) {
 492         return;
 493       }
 494       Klass* k = parse_klass(CHECK);
 495       rec->oops_offsets[i] = offset;
 496       rec->oops_handles[i] = (jobject)(new KlassHandle(THREAD, k));
 497     }
 498   }
 499 
 500   // instanceKlass <name>
 501   //
 502   // Loads and initializes the klass 'name'.  This can be used to
 503   // create particular class loading environments
 504   void process_instanceKlass(TRAPS) {
 505     // just load the referenced class
 506     Klass* k = parse_klass(CHECK);
 507   }
 508 
 509   // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag # # # ...
 510   //
 511   // Load the klass 'name' and link or initialize it.  Verify that the
 512   // constant pool is the same length as 'length' and make sure the
 513   // constant pool tags are in the same state.
 514   void process_ciInstanceKlass(TRAPS) {
 515     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 516     int is_linked = parse_int("is_linked");
 517     int is_initialized = parse_int("is_initialized");
 518     int length = parse_int("length");
 519     if (is_initialized) {
 520       k->initialize(THREAD);
 521       if (HAS_PENDING_EXCEPTION) {
 522         oop throwable = PENDING_EXCEPTION;
 523         java_lang_Throwable::print(throwable, tty);
 524         tty->cr();
 525         if (ReplayIgnoreInitErrors) {
 526           CLEAR_PENDING_EXCEPTION;
 527           k->set_init_state(InstanceKlass::fully_initialized);
 528         } else {
 529           return;
 530         }
 531       }
 532     } else if (is_linked) {
 533       k->link_class(CHECK);
 534     }
 535     ConstantPool* cp = k->constants();
 536     if (length != cp->length()) {
 537       report_error("constant pool length mismatch: wrong class files?");
 538       return;
 539     }
 540 
 541     int parsed_two_word = 0;
 542     for (int i = 1; i < length; i++) {
 543       int tag = parse_int("tag");
 544       if (had_error()) {
 545         return;
 546       }
 547       switch (cp->tag_at(i).value()) {
 548         case JVM_CONSTANT_UnresolvedClass: {
 549           if (tag == JVM_CONSTANT_Class) {
 550             tty->print_cr("Resolving klass %s at %d", cp->unresolved_klass_at(i)->as_utf8(), i);
 551             Klass* k = cp->klass_at(i, CHECK);
 552           }
 553           break;
 554         }
 555         case JVM_CONSTANT_Long:
 556         case JVM_CONSTANT_Double:
 557           parsed_two_word = i + 1;
 558 
 559         case JVM_CONSTANT_ClassIndex:
 560         case JVM_CONSTANT_StringIndex:
 561         case JVM_CONSTANT_String:
 562         case JVM_CONSTANT_UnresolvedClassInError:
 563         case JVM_CONSTANT_Fieldref:
 564         case JVM_CONSTANT_Methodref:
 565         case JVM_CONSTANT_InterfaceMethodref:
 566         case JVM_CONSTANT_NameAndType:
 567         case JVM_CONSTANT_Utf8:
 568         case JVM_CONSTANT_Integer:
 569         case JVM_CONSTANT_Float:
 570           if (tag != cp->tag_at(i).value()) {
 571             report_error("tag mismatch: wrong class files?");
 572             return;
 573           }
 574           break;
 575 
 576         case JVM_CONSTANT_Class:
 577           if (tag == JVM_CONSTANT_Class) {
 578           } else if (tag == JVM_CONSTANT_UnresolvedClass) {
 579             tty->print_cr("Warning: entry was unresolved in the replay data");
 580           } else {
 581             report_error("Unexpected tag");
 582             return;
 583           }
 584           break;
 585 
 586         case 0:
 587           if (parsed_two_word == i) continue;
 588 
 589         default:
 590           ShouldNotReachHere();
 591           break;
 592       }
 593 
 594     }
 595   }
 596 
 597   // Initialize a class and fill in the value for a static field.
 598   // This is useful when the compile was dependent on the value of
 599   // static fields but it's impossible to properly rerun the static
 600   // initiailizer.
 601   void process_staticfield(TRAPS) {
 602     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
 603 
 604     if (ReplaySuppressInitializers == 0 ||
 605         ReplaySuppressInitializers == 2 && k->class_loader() == NULL) {
 606       return;
 607     }
 608 
 609     assert(k->is_initialized(), "must be");
 610 
 611     const char* field_name = parse_escaped_string();;
 612     const char* field_signature = parse_string();
 613     fieldDescriptor fd;
 614     Symbol* name = SymbolTable::lookup(field_name, (int)strlen(field_name), CHECK);
 615     Symbol* sig = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 616     if (!k->find_local_field(name, sig, &fd) ||
 617         !fd.is_static() ||
 618         fd.has_initial_value()) {
 619       report_error(field_name);
 620       return;
 621     }
 622 
 623     oop java_mirror = k->java_mirror();
 624     if (field_signature[0] == '[') {
 625       int length = parse_int("array length");
 626       oop value = NULL;
 627 
 628       if (field_signature[1] == '[') {
 629         // multi dimensional array
 630         ArrayKlass* kelem = (ArrayKlass *)parse_klass(CHECK);
 631         int rank = 0;
 632         while (field_signature[rank] == '[') {
 633           rank++;
 634         }
 635         int* dims = NEW_RESOURCE_ARRAY(int, rank);
 636         dims[0] = length;
 637         for (int i = 1; i < rank; i++) {
 638           dims[i] = 1; // These aren't relevant to the compiler
 639         }
 640         value = kelem->multi_allocate(rank, dims, CHECK);
 641       } else {
 642         if (strcmp(field_signature, "[B") == 0) {
 643           value = oopFactory::new_byteArray(length, CHECK);
 644         } else if (strcmp(field_signature, "[Z") == 0) {
 645           value = oopFactory::new_boolArray(length, CHECK);
 646         } else if (strcmp(field_signature, "[C") == 0) {
 647           value = oopFactory::new_charArray(length, CHECK);
 648         } else if (strcmp(field_signature, "[S") == 0) {
 649           value = oopFactory::new_shortArray(length, CHECK);
 650         } else if (strcmp(field_signature, "[F") == 0) {
 651           value = oopFactory::new_singleArray(length, CHECK);
 652         } else if (strcmp(field_signature, "[D") == 0) {
 653           value = oopFactory::new_doubleArray(length, CHECK);
 654         } else if (strcmp(field_signature, "[I") == 0) {
 655           value = oopFactory::new_intArray(length, CHECK);
 656         } else if (strcmp(field_signature, "[J") == 0) {
 657           value = oopFactory::new_longArray(length, CHECK);
 658         } else if (field_signature[0] == '[' && field_signature[1] == 'L') {
 659           KlassHandle kelem = resolve_klass(field_signature + 1, CHECK);
 660           value = oopFactory::new_objArray(kelem(), length, CHECK);
 661         } else {
 662           report_error("unhandled array staticfield");
 663         }
 664       }
 665       java_mirror->obj_field_put(fd.offset(), value);
 666     } else {
 667       const char* string_value = parse_escaped_string();
 668       if (strcmp(field_signature, "I") == 0) {
 669         int value = atoi(string_value);
 670         java_mirror->int_field_put(fd.offset(), value);
 671       } else if (strcmp(field_signature, "B") == 0) {
 672         int value = atoi(string_value);
 673         java_mirror->byte_field_put(fd.offset(), value);
 674       } else if (strcmp(field_signature, "C") == 0) {
 675         int value = atoi(string_value);
 676         java_mirror->char_field_put(fd.offset(), value);
 677       } else if (strcmp(field_signature, "S") == 0) {
 678         int value = atoi(string_value);
 679         java_mirror->short_field_put(fd.offset(), value);
 680       } else if (strcmp(field_signature, "Z") == 0) {
 681         int value = atol(string_value);
 682         java_mirror->bool_field_put(fd.offset(), value);
 683       } else if (strcmp(field_signature, "J") == 0) {
 684         jlong value;
 685         if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
 686           fprintf(stderr, "Error parsing long: %s\n", string_value);
 687           return;
 688         }
 689         java_mirror->long_field_put(fd.offset(), value);
 690       } else if (strcmp(field_signature, "F") == 0) {
 691         float value = atof(string_value);
 692         java_mirror->float_field_put(fd.offset(), value);
 693       } else if (strcmp(field_signature, "D") == 0) {
 694         double value = atof(string_value);
 695         java_mirror->double_field_put(fd.offset(), value);
 696       } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
 697         Handle value = java_lang_String::create_from_str(string_value, CHECK);
 698         java_mirror->obj_field_put(fd.offset(), value());
 699       } else if (field_signature[0] == 'L') {
 700         Symbol* klass_name = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
 701         KlassHandle kelem = resolve_klass(field_signature, CHECK);
 702         oop value = ((InstanceKlass*)kelem())->allocate_instance(CHECK);
 703         java_mirror->obj_field_put(fd.offset(), value);
 704       } else {
 705         report_error("unhandled staticfield");
 706       }
 707     }
 708   }
 709 
 710 #if INCLUDE_JVMTI
 711   void process_JvmtiExport(TRAPS) {
 712     const char* field = parse_string();
 713     bool value = parse_int("JvmtiExport flag") != 0;
 714     if (strcmp(field, "can_access_local_variables") == 0) {
 715       JvmtiExport::set_can_access_local_variables(value);
 716     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
 717       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
 718     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
 719       JvmtiExport::set_can_post_on_exceptions(value);
 720     } else {
 721       report_error("Unrecognized JvmtiExport directive");
 722     }
 723   }
 724 #endif // INCLUDE_JVMTI
 725 
 726   // Create and initialize a record for a ciMethod
 727   ciMethodRecord* new_ciMethod(Method* method) {
 728     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
 729     rec->klass =  method->method_holder()->name()->as_utf8();
 730     rec->method = method->name()->as_utf8();
 731     rec->signature = method->signature()->as_utf8();
 732     ci_method_records.append(rec);
 733     return rec;
 734   }
 735 
 736   // Lookup data for a ciMethod
 737   ciMethodRecord* find_ciMethodRecord(Method* method) {
 738     const char* klass_name =  method->method_holder()->name()->as_utf8();
 739     const char* method_name = method->name()->as_utf8();
 740     const char* signature = method->signature()->as_utf8();
 741     for (int i = 0; i < ci_method_records.length(); i++) {
 742       ciMethodRecord* rec = ci_method_records.at(i);
 743       if (strcmp(rec->klass, klass_name) == 0 &&
 744           strcmp(rec->method, method_name) == 0 &&
 745           strcmp(rec->signature, signature) == 0) {
 746         return rec;
 747       }
 748     }
 749     return NULL;
 750   }
 751 
 752   // Create and initialize a record for a ciMethodData
 753   ciMethodDataRecord* new_ciMethodData(Method* method) {
 754     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
 755     rec->klass =  method->method_holder()->name()->as_utf8();
 756     rec->method = method->name()->as_utf8();
 757     rec->signature = method->signature()->as_utf8();
 758     ci_method_data_records.append(rec);
 759     return rec;
 760   }
 761 
 762   // Lookup data for a ciMethodData
 763   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
 764     const char* klass_name =  method->method_holder()->name()->as_utf8();
 765     const char* method_name = method->name()->as_utf8();
 766     const char* signature = method->signature()->as_utf8();
 767     for (int i = 0; i < ci_method_data_records.length(); i++) {
 768       ciMethodDataRecord* rec = ci_method_data_records.at(i);
 769       if (strcmp(rec->klass, klass_name) == 0 &&
 770           strcmp(rec->method, method_name) == 0 &&
 771           strcmp(rec->signature, signature) == 0) {
 772         return rec;
 773       }
 774     }
 775     return NULL;
 776   }
 777 
 778   const char* error_message() {
 779     return _error_message;
 780   }
 781 
 782   void reset() {
 783     _error_message = NULL;
 784     ci_method_records.clear();
 785     ci_method_data_records.clear();
 786   }
 787 
 788   // Take an ascii string contain \u#### escapes and convert it to utf8
 789   // in place.
 790   static void unescape_string(char* value) {
 791     char* from = value;
 792     char* to = value;
 793     while (*from != '\0') {
 794       if (*from != '\\') {
 795         *from++ = *to++;
 796       } else {
 797         switch (from[1]) {
 798           case 'u': {
 799             from += 2;
 800             jchar value=0;
 801             for (int i=0; i<4; i++) {
 802               char c = *from++;
 803               switch (c) {
 804                 case '0': case '1': case '2': case '3': case '4':
 805                 case '5': case '6': case '7': case '8': case '9':
 806                   value = (value << 4) + c - '0';
 807                   break;
 808                 case 'a': case 'b': case 'c':
 809                 case 'd': case 'e': case 'f':
 810                   value = (value << 4) + 10 + c - 'a';
 811                   break;
 812                 case 'A': case 'B': case 'C':
 813                 case 'D': case 'E': case 'F':
 814                   value = (value << 4) + 10 + c - 'A';
 815                   break;
 816                 default:
 817                   ShouldNotReachHere();
 818               }
 819             }
 820             UNICODE::convert_to_utf8(&value, 1, to);
 821             to++;
 822             break;
 823           }
 824           case 't': *to++ = '\t'; from += 2; break;
 825           case 'n': *to++ = '\n'; from += 2; break;
 826           case 'r': *to++ = '\r'; from += 2; break;
 827           case 'f': *to++ = '\f'; from += 2; break;
 828           default:
 829             ShouldNotReachHere();
 830         }
 831       }
 832     }
 833     *from = *to;
 834   }
 835 };
 836 
 837 void ciReplay::replay(TRAPS) {
 838   int exit_code = replay_impl(THREAD);
 839 
 840   Threads::destroy_vm();
 841 
 842   vm_exit(exit_code);
 843 }
 844 
 845 int ciReplay::replay_impl(TRAPS) {
 846   HandleMark hm;
 847   ResourceMark rm;
 848   // Make sure we don't run with background compilation
 849   BackgroundCompilation = false;
 850 
 851   if (ReplaySuppressInitializers > 2) {
 852     // ReplaySuppressInitializers > 2 means that we want to allow
 853     // normal VM bootstrap but once we get into the replay itself
 854     // don't allow any intializers to be run.
 855     ReplaySuppressInitializers = 1;
 856   }
 857 
 858   // Load and parse the replay data
 859   CompileReplay rp(ReplayDataFile, THREAD);
 860   int exit_code = 0;
 861   if (rp.can_replay()) {
 862     rp.process(THREAD);
 863   } else {
 864     exit_code = 1;
 865     return exit_code;
 866   }
 867 
 868   if (HAS_PENDING_EXCEPTION) {
 869     oop throwable = PENDING_EXCEPTION;
 870     CLEAR_PENDING_EXCEPTION;
 871     java_lang_Throwable::print(throwable, tty);
 872     tty->cr();
 873     java_lang_Throwable::print_stack_trace(throwable, tty);
 874     tty->cr();
 875     exit_code = 2;
 876   }
 877 
 878   if (rp.had_error()) {
 879     tty->print_cr("Failed on %s", rp.error_message());
 880     exit_code = 1;
 881   }
 882   return exit_code;
 883 }
 884 
 885 
 886 void ciReplay::initialize(ciMethodData* m) {
 887   if (replay_state == NULL) {
 888     return;
 889   }
 890 
 891   ASSERT_IN_VM;
 892   ResourceMark rm;
 893 
 894   Method* method = m->get_MethodData()->method();
 895   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
 896   if (rec == NULL) {
 897     // This indicates some mismatch with the original environment and
 898     // the replay environment though it's not always enough to
 899     // interfere with reproducing a bug
 900     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
 901     method->print_name(tty);
 902     tty->cr();
 903   } else {
 904     m->_state = rec->state;
 905     m->_current_mileage = rec->current_mileage;
 906     if (rec->data_length != 0) {
 907       assert(m->_data_size == rec->data_length * (int)sizeof(rec->data[0]), "must agree");
 908 
 909       // Write the correct ciObjects back into the profile data
 910       ciEnv* env = ciEnv::current();
 911       for (int i = 0; i < rec->oops_length; i++) {
 912         KlassHandle *h = (KlassHandle *)rec->oops_handles[i];
 913         *(ciMetadata**)(rec->data + rec->oops_offsets[i]) =
 914           env->get_metadata((*h)());
 915       }
 916       // Copy the updated profile data into place as intptr_ts
 917 #ifdef _LP64
 918       Copy::conjoint_jlongs_atomic((jlong *)rec->data, (jlong *)m->_data, rec->data_length);
 919 #else
 920       Copy::conjoint_jints_atomic((jint *)rec->data, (jint *)m->_data, rec->data_length);
 921 #endif
 922     }
 923 
 924     // copy in the original header
 925     Copy::conjoint_jbytes(rec->orig_data, (char*)&m->_orig, rec->orig_data_length);
 926   }
 927 }
 928 
 929 
 930 bool ciReplay::should_not_inline(ciMethod* method) {
 931   if (replay_state == NULL) {
 932     return false;
 933   }
 934 
 935   VM_ENTRY_MARK;
 936   // ciMethod without a record shouldn't be inlined.
 937   return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
 938 }
 939 
 940 
 941 void ciReplay::initialize(ciMethod* m) {
 942   if (replay_state == NULL) {
 943     return;
 944   }
 945 
 946   ASSERT_IN_VM;
 947   ResourceMark rm;
 948 
 949   Method* method = m->get_Method();
 950   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
 951   if (rec == NULL) {
 952     // This indicates some mismatch with the original environment and
 953     // the replay environment though it's not always enough to
 954     // interfere with reproducing a bug
 955     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
 956     method->print_name(tty);
 957     tty->cr();
 958   } else {
 959     // m->_instructions_size = rec->instructions_size;
 960     m->_instructions_size = -1;
 961     m->_interpreter_invocation_count = rec->interpreter_invocation_count;
 962     m->_interpreter_throwout_count = rec->interpreter_throwout_count;
 963     method->invocation_counter()->_counter = rec->invocation_counter;
 964     method->backedge_counter()->_counter = rec->backedge_counter;
 965   }
 966 }
 967 
 968 bool ciReplay::is_loaded(Method* method) {
 969   if (replay_state == NULL) {
 970     return true;
 971   }
 972 
 973   ASSERT_IN_VM;
 974   ResourceMark rm;
 975 
 976   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
 977   return rec != NULL;
 978 }
 979 #endif // PRODUCT