src/share/vm/ci/ciReplay.cpp

Tue, 16 Oct 2018 10:40:23 -0400

author
phh
date
Tue, 16 Oct 2018 10:40:23 -0400
changeset 9507
7e72702243a4
parent 6655
653e11c86c5a
child 9572
624a0741915c
permissions
-rw-r--r--

8064811: Use THEAD instead of CHECK_NULL in return statements
Summary: Backport from JDK9
Reviewed-by: dholmes, coffeys

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

mercurial