1 /* 2 * Copyright (c) 2013, 2022, 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.inline.hpp" 31 #include "classfile/javaClasses.hpp" 32 #include "classfile/symbolTable.hpp" 33 #include "classfile/systemDictionary.hpp" 34 #include "compiler/compilationPolicy.hpp" 35 #include "compiler/compileBroker.hpp" 36 #include "compiler/compilerDefinitions.inline.hpp" 37 #include "interpreter/linkResolver.hpp" 38 #include "jvm.h" 39 #include "memory/allocation.inline.hpp" 40 #include "memory/oopFactory.hpp" 41 #include "memory/resourceArea.hpp" 42 #include "oops/constantPool.hpp" 43 #include "oops/cpCache.inline.hpp" 44 #include "oops/fieldStreams.inline.hpp" 45 #include "oops/inlineKlass.inline.hpp" 46 #include "oops/klass.inline.hpp" 47 #include "oops/method.inline.hpp" 48 #include "oops/oop.inline.hpp" 49 #include "prims/jvmtiExport.hpp" 50 #include "prims/methodHandles.hpp" 51 #include "runtime/fieldDescriptor.inline.hpp" 52 #include "runtime/globals_extension.hpp" 53 #include "runtime/handles.inline.hpp" 54 #include "runtime/java.hpp" 55 #include "runtime/jniHandles.inline.hpp" 56 #include "runtime/threads.hpp" 57 #include "utilities/copy.hpp" 58 #include "utilities/macros.hpp" 59 #include "utilities/utf8.hpp" 60 61 // ciReplay 62 63 typedef struct _ciMethodDataRecord { 64 const char* _klass_name; 65 const char* _method_name; 66 const char* _signature; 67 68 int _state; 69 int _invocation_counter; 70 71 intptr_t* _data; 72 char* _orig_data; 73 Klass** _classes; 74 Method** _methods; 75 int* _classes_offsets; 76 int* _methods_offsets; 77 int _data_length; 78 int _orig_data_length; 79 int _classes_length; 80 int _methods_length; 81 } ciMethodDataRecord; 82 83 typedef struct _ciMethodRecord { 84 const char* _klass_name; 85 const char* _method_name; 86 const char* _signature; 87 88 int _instructions_size; 89 int _interpreter_invocation_count; 90 int _interpreter_throwout_count; 91 int _invocation_counter; 92 int _backedge_counter; 93 } ciMethodRecord; 94 95 typedef struct _ciInstanceKlassRecord { 96 const InstanceKlass* _klass; 97 jobject _java_mirror; // Global handle to java mirror to prevent unloading 98 } ciInstanceKlassRecord; 99 100 typedef struct _ciInlineRecord { 101 const char* _klass_name; 102 const char* _method_name; 103 const char* _signature; 104 105 int _inline_depth; 106 int _inline_bci; 107 bool _inline_late; 108 } ciInlineRecord; 109 110 class CompileReplay; 111 static CompileReplay* replay_state; 112 113 class CompileReplay : public StackObj { 114 private: 115 FILE* _stream; 116 Thread* _thread; 117 Handle _protection_domain; 118 bool _protection_domain_initialized; 119 Handle _loader; 120 int _version; 121 122 GrowableArray<ciMethodRecord*> _ci_method_records; 123 GrowableArray<ciMethodDataRecord*> _ci_method_data_records; 124 GrowableArray<ciInstanceKlassRecord*> _ci_instance_klass_records; 125 126 // Use pointer because we may need to return inline records 127 // without destroying them. 128 GrowableArray<ciInlineRecord*>* _ci_inline_records; 129 130 const char* _error_message; 131 132 char* _bufptr; 133 char* _buffer; 134 int _buffer_length; 135 136 // "compile" data 137 ciKlass* _iklass; 138 Method* _imethod; 139 int _entry_bci; 140 int _comp_level; 141 142 public: 143 CompileReplay(const char* filename, TRAPS) { 144 _thread = THREAD; 145 _loader = Handle(_thread, SystemDictionary::java_system_loader()); 146 _protection_domain = Handle(); 147 _protection_domain_initialized = false; 148 149 _stream = os::fopen(filename, "rt"); 150 if (_stream == NULL) { 151 fprintf(stderr, "ERROR: Can't open replay file %s\n", filename); 152 } 153 154 _ci_inline_records = NULL; 155 _error_message = NULL; 156 157 _buffer_length = 32; 158 _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length); 159 _bufptr = _buffer; 160 161 _imethod = NULL; 162 _iklass = NULL; 163 _entry_bci = 0; 164 _comp_level = 0; 165 _version = 0; 166 167 test(); 168 } 169 170 ~CompileReplay() { 171 if (_stream != NULL) fclose(_stream); 172 } 173 174 void test() { 175 strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\""); 176 _bufptr = _buffer; 177 assert(parse_int("test") == 1, "what"); 178 assert(parse_int("test") == 2, "what"); 179 assert(strcmp(parse_string(), "foo") == 0, "what"); 180 assert(parse_int("test") == 4, "what"); 181 assert(strcmp(parse_string(), "bar") == 0, "what"); 182 assert(parse_intptr_t("test") == 9, "what"); 183 assert(strcmp(parse_quoted_string(), "this is it") == 0, "what"); 184 } 185 186 bool had_error() { 187 return _error_message != NULL || _thread->has_pending_exception(); 188 } 189 190 bool can_replay() { 191 return !(_stream == NULL || had_error()); 192 } 193 194 void report_error(const char* msg) { 195 _error_message = msg; 196 } 197 198 int parse_int(const char* label) { 199 if (had_error()) { 200 return 0; 201 } 202 203 int v = 0; 204 int read; 205 if (sscanf(_bufptr, "%i%n", &v, &read) != 1) { 206 report_error(label); 207 } else { 208 _bufptr += read; 209 } 210 return v; 211 } 212 213 intptr_t parse_intptr_t(const char* label) { 214 if (had_error()) { 215 return 0; 216 } 217 218 intptr_t v = 0; 219 int read; 220 if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) { 221 report_error(label); 222 } else { 223 _bufptr += read; 224 } 225 return v; 226 } 227 228 void skip_ws() { 229 // Skip any leading whitespace 230 while (*_bufptr == ' ' || *_bufptr == '\t') { 231 _bufptr++; 232 } 233 } 234 235 // Ignore the rest of the line 236 void skip_remaining() { 237 _bufptr = &_bufptr[strlen(_bufptr)]; // skip ahead to terminator 238 } 239 240 char* scan_and_terminate(char delim) { 241 char* str = _bufptr; 242 while (*_bufptr != delim && *_bufptr != '\0') { 243 _bufptr++; 244 } 245 if (*_bufptr != '\0') { 246 *_bufptr++ = '\0'; 247 } 248 if (_bufptr == str) { 249 // nothing here 250 return NULL; 251 } 252 return str; 253 } 254 255 char* parse_string() { 256 if (had_error()) return NULL; 257 258 skip_ws(); 259 return scan_and_terminate(' '); 260 } 261 262 char* parse_quoted_string() { 263 if (had_error()) return NULL; 264 265 skip_ws(); 266 267 if (*_bufptr == '"') { 268 _bufptr++; 269 return scan_and_terminate('"'); 270 } else { 271 return scan_and_terminate(' '); 272 } 273 } 274 275 char* parse_escaped_string() { 276 char* result = parse_quoted_string(); 277 if (result != NULL) { 278 unescape_string(result); 279 } 280 return result; 281 } 282 283 // Look for the tag 'tag' followed by an 284 bool parse_tag_and_count(const char* tag, int& length) { 285 const char* t = parse_string(); 286 if (t == NULL) { 287 return false; 288 } 289 290 if (strcmp(tag, t) != 0) { 291 report_error(tag); 292 return false; 293 } 294 length = parse_int("parse_tag_and_count"); 295 return !had_error(); 296 } 297 298 // Parse a sequence of raw data encoded as bytes and return the 299 // resulting data. 300 char* parse_data(const char* tag, int& length) { 301 int read_size = 0; 302 if (!parse_tag_and_count(tag, read_size)) { 303 return NULL; 304 } 305 306 int actual_size = sizeof(MethodData::CompilerCounters); 307 char *result = NEW_RESOURCE_ARRAY(char, actual_size); 308 int i = 0; 309 if (read_size != actual_size) { 310 tty->print_cr("Warning: ciMethodData parsing sees MethodData size %i in file, current is %i", read_size, 311 actual_size); 312 // Replay serializes the entire MethodData, but the data is at the end. 313 // If the MethodData instance size has changed, we can pad or truncate in the beginning 314 int padding = actual_size - read_size; 315 if (padding > 0) { 316 // pad missing data with zeros 317 tty->print_cr("- Padding MethodData"); 318 for (; i < padding; i++) { 319 result[i] = 0; 320 } 321 } else if (padding < 0) { 322 // drop some data 323 tty->print_cr("- Truncating MethodData"); 324 for (int j = 0; j < -padding; j++) { 325 int val = parse_int("data"); 326 // discard val 327 } 328 } 329 } 330 331 assert(i < actual_size, "At least some data must remain to be copied"); 332 for (; i < actual_size; i++) { 333 int val = parse_int("data"); 334 result[i] = val; 335 } 336 length = actual_size; 337 return result; 338 } 339 340 // Parse a standard chunk of data emitted as: 341 // 'tag' <length> # # ... 342 // Where each # is an intptr_t item 343 intptr_t* parse_intptr_data(const char* tag, int& length) { 344 if (!parse_tag_and_count(tag, length)) { 345 return NULL; 346 } 347 348 intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length); 349 for (int i = 0; i < length; i++) { 350 skip_ws(); 351 intptr_t val = parse_intptr_t("data"); 352 result[i] = val; 353 } 354 return result; 355 } 356 357 // Parse a possibly quoted version of a symbol into a symbolOop 358 Symbol* parse_symbol() { 359 const char* str = parse_escaped_string(); 360 if (str != NULL) { 361 Symbol* sym = SymbolTable::new_symbol(str); 362 return sym; 363 } 364 return NULL; 365 } 366 367 bool parse_terminator() { 368 char* terminator = parse_string(); 369 if (terminator != NULL && strcmp(terminator, ";") == 0) { 370 return true; 371 } 372 return false; 373 } 374 375 // Parse a special hidden klass location syntax 376 // syntax: @bci <klass> <name> <signature> <bci> <location>* ; 377 // syntax: @cpi <klass> <cpi> <location>* ; 378 Klass* parse_cp_ref(TRAPS) { 379 JavaThread* thread = THREAD; 380 oop obj = NULL; 381 char* ref = parse_string(); 382 if (strcmp(ref, "bci") == 0) { 383 Method* m = parse_method(CHECK_NULL); 384 if (m == NULL) { 385 return NULL; 386 } 387 388 InstanceKlass* ik = m->method_holder(); 389 const constantPoolHandle cp(Thread::current(), ik->constants()); 390 391 // invokedynamic or invokehandle 392 393 methodHandle caller(Thread::current(), m); 394 int bci = parse_int("bci"); 395 if (m->validate_bci(bci) != bci) { 396 report_error("bad bci"); 397 return NULL; 398 } 399 400 ik->link_class(CHECK_NULL); 401 402 Bytecode_invoke bytecode = Bytecode_invoke_check(caller, bci); 403 if (!Bytecodes::is_defined(bytecode.code()) || !bytecode.is_valid()) { 404 report_error("no invoke found at bci"); 405 return NULL; 406 } 407 bytecode.verify(); 408 int index = bytecode.index(); 409 410 ConstantPoolCacheEntry* cp_cache_entry = NULL; 411 CallInfo callInfo; 412 Bytecodes::Code bc = bytecode.invoke_code(); 413 LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, bc, CHECK_NULL); 414 if (bytecode.is_invokedynamic()) { 415 cp_cache_entry = cp->invokedynamic_cp_cache_entry_at(index); 416 cp_cache_entry->set_dynamic_call(cp, callInfo); 417 } else if (bytecode.is_invokehandle()) { 418 #ifdef ASSERT 419 Klass* holder = cp->klass_ref_at(index, CHECK_NULL); 420 Symbol* name = cp->name_ref_at(index); 421 assert(MethodHandles::is_signature_polymorphic_name(holder, name), ""); 422 #endif 423 cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index)); 424 cp_cache_entry->set_method_handle(cp, callInfo); 425 } else { 426 report_error("no dynamic invoke found"); 427 return NULL; 428 } 429 char* dyno_ref = parse_string(); 430 if (strcmp(dyno_ref, "<appendix>") == 0) { 431 obj = cp_cache_entry->appendix_if_resolved(cp); 432 } else if (strcmp(dyno_ref, "<adapter>") == 0) { 433 if (!parse_terminator()) { 434 report_error("no dynamic invoke found"); 435 return NULL; 436 } 437 Method* adapter = cp_cache_entry->f1_as_method(); 438 if (adapter == NULL) { 439 report_error("no adapter found"); 440 return NULL; 441 } 442 return adapter->method_holder(); 443 } else if (strcmp(dyno_ref, "<bsm>") == 0) { 444 int pool_index = cp_cache_entry->constant_pool_index(); 445 BootstrapInfo bootstrap_specifier(cp, pool_index, index); 446 obj = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), CHECK_NULL); 447 } else { 448 report_error("unrecognized token"); 449 return NULL; 450 } 451 } else { 452 // constant pool ref (MethodHandle) 453 if (strcmp(ref, "cpi") != 0) { 454 report_error("unexpected token"); 455 return NULL; 456 } 457 458 Klass* k = parse_klass(CHECK_NULL); 459 if (k == NULL) { 460 return NULL; 461 } 462 InstanceKlass* ik = InstanceKlass::cast(k); 463 const constantPoolHandle cp(Thread::current(), ik->constants()); 464 465 int cpi = parse_int("cpi"); 466 467 if (cpi >= cp->length()) { 468 report_error("bad cpi"); 469 return NULL; 470 } 471 if (!cp->tag_at(cpi).is_method_handle()) { 472 report_error("no method handle found at cpi"); 473 return NULL; 474 } 475 ik->link_class(CHECK_NULL); 476 obj = cp->resolve_possibly_cached_constant_at(cpi, CHECK_NULL); 477 } 478 if (obj == NULL) { 479 report_error("null cp object found"); 480 return NULL; 481 } 482 Klass* k = NULL; 483 skip_ws(); 484 // loop: read fields 485 char* field = NULL; 486 do { 487 field = parse_string(); 488 if (field == NULL) { 489 report_error("no field found"); 490 return NULL; 491 } 492 if (strcmp(field, ";") == 0) { 493 break; 494 } 495 // raw Method* 496 if (strcmp(field, "<vmtarget>") == 0) { 497 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(obj); 498 k = (vmtarget == NULL) ? NULL : vmtarget->method_holder(); 499 if (k == NULL) { 500 report_error("null vmtarget found"); 501 return NULL; 502 } 503 if (!parse_terminator()) { 504 report_error("missing terminator"); 505 return NULL; 506 } 507 return k; 508 } 509 obj = ciReplay::obj_field(obj, field); 510 // array 511 if (obj != NULL && obj->is_objArray()) { 512 objArrayOop arr = (objArrayOop)obj; 513 int index = parse_int("index"); 514 if (index >= arr->length()) { 515 report_error("bad array index"); 516 return NULL; 517 } 518 obj = arr->obj_at(index); 519 } 520 } while (obj != NULL); 521 if (obj == NULL) { 522 report_error("null field found"); 523 return NULL; 524 } 525 k = obj->klass(); 526 return k; 527 } 528 529 // Parse a valid klass name and look it up 530 // syntax: <name> 531 // syntax: <constant pool ref> 532 Klass* parse_klass(TRAPS) { 533 skip_ws(); 534 // check for constant pool object reference (for a dynamic/hidden class) 535 bool cp_ref = (*_bufptr == '@'); 536 if (cp_ref) { 537 ++_bufptr; 538 Klass* k = parse_cp_ref(CHECK_NULL); 539 if (k != NULL && !k->is_hidden()) { 540 report_error("expected hidden class"); 541 return NULL; 542 } 543 return k; 544 } 545 char* str = parse_escaped_string(); 546 Symbol* klass_name = SymbolTable::new_symbol(str); 547 if (klass_name != NULL) { 548 Klass* k = NULL; 549 if (_iklass != NULL) { 550 k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding(); 551 } else { 552 k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD); 553 } 554 if (HAS_PENDING_EXCEPTION) { 555 oop throwable = PENDING_EXCEPTION; 556 java_lang_Throwable::print(throwable, tty); 557 tty->cr(); 558 report_error(str); 559 if (ReplayIgnoreInitErrors) { 560 CLEAR_PENDING_EXCEPTION; 561 _error_message = NULL; 562 } 563 return NULL; 564 } 565 return k; 566 } 567 return NULL; 568 } 569 570 // Lookup a klass 571 Klass* resolve_klass(const char* klass, TRAPS) { 572 Symbol* klass_name = SymbolTable::new_symbol(klass); 573 return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD); 574 } 575 576 // Parse the standard tuple of <klass> <name> <signature> 577 Method* parse_method(TRAPS) { 578 InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL); 579 if (k == NULL) { 580 report_error("Can't find holder klass"); 581 return NULL; 582 } 583 Symbol* method_name = parse_symbol(); 584 Symbol* method_signature = parse_symbol(); 585 Method* m = k->find_method(method_name, method_signature); 586 if (m == NULL) { 587 report_error("Can't find method"); 588 } 589 return m; 590 } 591 592 int get_line(int c) { 593 int buffer_pos = 0; 594 while(c != EOF) { 595 if (buffer_pos + 1 >= _buffer_length) { 596 int new_length = _buffer_length * 2; 597 // Next call will throw error in case of OOM. 598 _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length); 599 _buffer_length = new_length; 600 } 601 if (c == '\n') { 602 c = getc(_stream); // get next char 603 break; 604 } else if (c == '\r') { 605 // skip LF 606 } else { 607 _buffer[buffer_pos++] = c; 608 } 609 c = getc(_stream); 610 } 611 // null terminate it, reset the pointer 612 _buffer[buffer_pos] = '\0'; // NL or EOF 613 _bufptr = _buffer; 614 return c; 615 } 616 617 // Process each line of the replay file executing each command until 618 // the file ends. 619 void process(TRAPS) { 620 int line_no = 1; 621 int c = getc(_stream); 622 while(c != EOF) { 623 c = get_line(c); 624 process_command(THREAD); 625 if (had_error()) { 626 int pos = _bufptr - _buffer + 1; 627 tty->print_cr("Error while parsing line %d at position %d: %s\n", line_no, pos, _error_message); 628 if (ReplayIgnoreInitErrors) { 629 CLEAR_PENDING_EXCEPTION; 630 _error_message = NULL; 631 } else { 632 return; 633 } 634 } 635 line_no++; 636 } 637 reset(); 638 } 639 640 void process_command(TRAPS) { 641 char* cmd = parse_string(); 642 if (cmd == NULL) { 643 return; 644 } 645 if (strcmp("#", cmd) == 0) { 646 // comment line, print or ignore 647 if (Verbose) { 648 tty->print_cr("# %s", _bufptr); 649 } 650 skip_remaining(); 651 } else if (strcmp("version", cmd) == 0) { 652 _version = parse_int("version"); 653 if (_version < 0 || _version > REPLAY_VERSION) { 654 tty->print_cr("# unrecognized version %d, expected 0 <= version <= %d", _version, REPLAY_VERSION); 655 } 656 } else if (strcmp("compile", cmd) == 0) { 657 process_compile(CHECK); 658 } else if (strcmp("ciMethod", cmd) == 0) { 659 process_ciMethod(CHECK); 660 } else if (strcmp("ciMethodData", cmd) == 0) { 661 process_ciMethodData(CHECK); 662 } else if (strcmp("staticfield", cmd) == 0) { 663 process_staticfield(CHECK); 664 } else if (strcmp("ciInstanceKlass", cmd) == 0) { 665 process_ciInstanceKlass(CHECK); 666 } else if (strcmp("instanceKlass", cmd) == 0) { 667 process_instanceKlass(CHECK); 668 #if INCLUDE_JVMTI 669 } else if (strcmp("JvmtiExport", cmd) == 0) { 670 process_JvmtiExport(CHECK); 671 #endif // INCLUDE_JVMTI 672 } else { 673 report_error("unknown command"); 674 } 675 if (!had_error() && *_bufptr != '\0') { 676 report_error("line not properly terminated"); 677 } 678 } 679 680 // validation of comp_level 681 bool is_valid_comp_level(int comp_level) { 682 const int msg_len = 256; 683 char* msg = NULL; 684 if (!is_compile(comp_level)) { 685 msg = NEW_RESOURCE_ARRAY(char, msg_len); 686 jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level); 687 } else if (is_c1_compile(comp_level) && !CompilerConfig::is_c1_enabled()) { 688 msg = NEW_RESOURCE_ARRAY(char, msg_len); 689 jio_snprintf(msg, msg_len, "compilation level %d requires C1", comp_level); 690 } else if (is_c2_compile(comp_level) && !CompilerConfig::is_c2_enabled()) { 691 msg = NEW_RESOURCE_ARRAY(char, msg_len); 692 jio_snprintf(msg, msg_len, "compilation level %d requires C2", comp_level); 693 } 694 if (msg != NULL) { 695 report_error(msg); 696 return false; 697 } 698 return true; 699 } 700 701 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <klass> <name> <signature>)* 702 void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) { 703 _imethod = m; 704 _iklass = imethod->holder(); 705 _entry_bci = entry_bci; 706 _comp_level = comp_level; 707 int line_no = 1; 708 int c = getc(_stream); 709 while(c != EOF) { 710 c = get_line(c); 711 // Expecting only lines with "compile" command in inline replay file. 712 char* cmd = parse_string(); 713 if (cmd == NULL || strcmp("compile", cmd) != 0) { 714 return NULL; 715 } 716 process_compile(CHECK_NULL); 717 if (had_error()) { 718 tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message); 719 tty->print_cr("%s", _buffer); 720 return NULL; 721 } 722 if (_ci_inline_records != NULL && _ci_inline_records->length() > 0) { 723 // Found inlining record for the requested method. 724 return _ci_inline_records; 725 } 726 line_no++; 727 } 728 return NULL; 729 } 730 731 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <inline_late> <klass> <name> <signature>)* 732 void process_compile(TRAPS) { 733 Method* method = parse_method(CHECK); 734 if (had_error()) return; 735 int entry_bci = parse_int("entry_bci"); 736 int comp_level = parse_int("comp_level"); 737 if (!is_valid_comp_level(comp_level)) { 738 return; 739 } 740 if (_imethod != NULL) { 741 // Replay Inlining 742 if (entry_bci != _entry_bci || comp_level != _comp_level) { 743 return; 744 } 745 const char* iklass_name = _imethod->method_holder()->name()->as_utf8(); 746 const char* imethod_name = _imethod->name()->as_utf8(); 747 const char* isignature = _imethod->signature()->as_utf8(); 748 const char* klass_name = method->method_holder()->name()->as_utf8(); 749 const char* method_name = method->name()->as_utf8(); 750 const char* signature = method->signature()->as_utf8(); 751 if (strcmp(iklass_name, klass_name) != 0 || 752 strcmp(imethod_name, method_name) != 0 || 753 strcmp(isignature, signature) != 0) { 754 return; 755 } 756 } 757 int inline_count = 0; 758 if (parse_tag_and_count("inline", inline_count)) { 759 // Record inlining data 760 _ci_inline_records = new GrowableArray<ciInlineRecord*>(); 761 for (int i = 0; i < inline_count; i++) { 762 int depth = parse_int("inline_depth"); 763 int bci = parse_int("inline_bci"); 764 if (had_error()) { 765 break; 766 } 767 int inline_late = 0; 768 if (_version >= 2) { 769 inline_late = parse_int("inline_late"); 770 if (had_error()) { 771 break; 772 } 773 } 774 775 Method* inl_method = parse_method(CHECK); 776 if (had_error()) { 777 break; 778 } 779 new_ciInlineRecord(inl_method, bci, depth, inline_late); 780 } 781 } 782 if (_imethod != NULL) { 783 return; // Replay Inlining 784 } 785 InstanceKlass* ik = method->method_holder(); 786 ik->initialize(THREAD); 787 if (HAS_PENDING_EXCEPTION) { 788 oop throwable = PENDING_EXCEPTION; 789 java_lang_Throwable::print(throwable, tty); 790 tty->cr(); 791 if (ReplayIgnoreInitErrors) { 792 CLEAR_PENDING_EXCEPTION; 793 ik->set_init_state(InstanceKlass::fully_initialized); 794 } else { 795 return; 796 } 797 } 798 // Make sure the existence of a prior compile doesn't stop this one 799 CompiledMethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code(); 800 if (nm != NULL) { 801 nm->make_not_entrant(); 802 } 803 replay_state = this; 804 CompileBroker::compile_method(methodHandle(THREAD, method), entry_bci, comp_level, 805 methodHandle(), 0, CompileTask::Reason_Replay, THREAD); 806 replay_state = NULL; 807 } 808 809 // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size> 810 void process_ciMethod(TRAPS) { 811 Method* method = parse_method(CHECK); 812 if (had_error()) return; 813 ciMethodRecord* rec = new_ciMethod(method); 814 rec->_invocation_counter = parse_int("invocation_counter"); 815 rec->_backedge_counter = parse_int("backedge_counter"); 816 rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count"); 817 rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count"); 818 rec->_instructions_size = parse_int("instructions_size"); 819 } 820 821 // ciMethodData <klass> <name> <signature> <state> <invocation_counter> orig <length> <byte>* data <length> <ptr>* oops <length> (<offset> <klass>)* methods <length> (<offset> <klass> <name> <signature>)* 822 void process_ciMethodData(TRAPS) { 823 Method* method = parse_method(CHECK); 824 if (had_error()) return; 825 /* just copied from Method, to build interpret data*/ 826 827 // To be properly initialized, some profiling in the MDO needs the 828 // method to be rewritten (number of arguments at a call for instance) 829 method->method_holder()->link_class(CHECK); 830 assert(method->method_data() == NULL, "Should only be initialized once"); 831 ClassLoaderData* loader_data = method->method_holder()->class_loader_data(); 832 MethodData* method_data = MethodData::allocate(loader_data, methodHandle(THREAD, method), CHECK); 833 method->set_method_data(method_data); 834 835 // collect and record all the needed information for later 836 ciMethodDataRecord* rec = new_ciMethodData(method); 837 rec->_state = parse_int("state"); 838 if (_version < 1) { 839 parse_int("current_mileage"); 840 } else { 841 rec->_invocation_counter = parse_int("invocation_counter"); 842 } 843 844 rec->_orig_data = parse_data("orig", rec->_orig_data_length); 845 if (rec->_orig_data == NULL) { 846 return; 847 } 848 rec->_data = parse_intptr_data("data", rec->_data_length); 849 if (rec->_data == NULL) { 850 return; 851 } 852 if (!parse_tag_and_count("oops", rec->_classes_length)) { 853 return; 854 } 855 rec->_classes = NEW_RESOURCE_ARRAY(Klass*, rec->_classes_length); 856 rec->_classes_offsets = NEW_RESOURCE_ARRAY(int, rec->_classes_length); 857 for (int i = 0; i < rec->_classes_length; i++) { 858 int offset = parse_int("offset"); 859 if (had_error()) { 860 return; 861 } 862 Klass* k = parse_klass(CHECK); 863 rec->_classes_offsets[i] = offset; 864 rec->_classes[i] = k; 865 } 866 867 if (!parse_tag_and_count("methods", rec->_methods_length)) { 868 return; 869 } 870 rec->_methods = NEW_RESOURCE_ARRAY(Method*, rec->_methods_length); 871 rec->_methods_offsets = NEW_RESOURCE_ARRAY(int, rec->_methods_length); 872 for (int i = 0; i < rec->_methods_length; i++) { 873 int offset = parse_int("offset"); 874 if (had_error()) { 875 return; 876 } 877 Method* m = parse_method(CHECK); 878 rec->_methods_offsets[i] = offset; 879 rec->_methods[i] = m; 880 } 881 } 882 883 // instanceKlass <name> 884 // instanceKlass <constant pool ref> # <original hidden class name> 885 // 886 // Loads and initializes the klass 'name'. This can be used to 887 // create particular class loading environments 888 void process_instanceKlass(TRAPS) { 889 // just load the referenced class 890 Klass* k = parse_klass(CHECK); 891 892 if (_version >= 1) { 893 if (!_protection_domain_initialized && k != NULL) { 894 assert(_protection_domain() == NULL, "must be uninitialized"); 895 // The first entry is the holder class of the method for which a replay compilation is requested. 896 // Use the same protection domain to load all subsequent classes in order to resolve all classes 897 // in signatures of inlinees. This ensures that inlining can be done as stated in the replay file. 898 _protection_domain = Handle(_thread, k->protection_domain()); 899 } 900 901 _protection_domain_initialized = true; 902 } 903 904 if (k == NULL) { 905 return; 906 } 907 const char* comment = parse_string(); 908 bool is_comment = comment != NULL && strcmp(comment, "#") == 0; 909 if (k->is_hidden() != is_comment) { 910 report_error("hidden class with comment expected"); 911 return; 912 } 913 // comment, print or ignore 914 if (is_comment) { 915 if (Verbose) { 916 const char* hidden = parse_string(); 917 tty->print_cr("Found %s for %s", k->name()->as_quoted_ascii(), hidden); 918 } 919 skip_remaining(); 920 } 921 } 922 923 // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag* 924 // 925 // Load the klass 'name' and link or initialize it. Verify that the 926 // constant pool is the same length as 'length' and make sure the 927 // constant pool tags are in the same state. 928 void process_ciInstanceKlass(TRAPS) { 929 InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK); 930 if (k == NULL) { 931 skip_remaining(); 932 return; 933 } 934 int is_linked = parse_int("is_linked"); 935 int is_initialized = parse_int("is_initialized"); 936 int length = parse_int("length"); 937 if (is_initialized) { 938 k->initialize(THREAD); 939 if (HAS_PENDING_EXCEPTION) { 940 oop throwable = PENDING_EXCEPTION; 941 java_lang_Throwable::print(throwable, tty); 942 tty->cr(); 943 if (ReplayIgnoreInitErrors) { 944 CLEAR_PENDING_EXCEPTION; 945 k->set_init_state(InstanceKlass::fully_initialized); 946 } else { 947 return; 948 } 949 } 950 } else if (is_linked) { 951 k->link_class(CHECK); 952 } 953 new_ciInstanceKlass(k); 954 ConstantPool* cp = k->constants(); 955 if (length != cp->length()) { 956 report_error("constant pool length mismatch: wrong class files?"); 957 return; 958 } 959 960 int parsed_two_word = 0; 961 for (int i = 1; i < length; i++) { 962 int tag = parse_int("tag"); 963 if (had_error()) { 964 return; 965 } 966 switch (cp->tag_at(i).value()) { 967 case JVM_CONSTANT_UnresolvedClass: { 968 if (tag == JVM_CONSTANT_Class) { 969 tty->print_cr("Resolving klass %s at %d", cp->klass_name_at(i)->as_utf8(), i); 970 Klass* k = cp->klass_at(i, CHECK); 971 } 972 break; 973 } 974 975 case JVM_CONSTANT_Long: 976 case JVM_CONSTANT_Double: 977 parsed_two_word = i + 1; 978 979 case JVM_CONSTANT_ClassIndex: 980 case JVM_CONSTANT_StringIndex: 981 case JVM_CONSTANT_String: 982 case JVM_CONSTANT_UnresolvedClassInError: 983 case JVM_CONSTANT_Fieldref: 984 case JVM_CONSTANT_Methodref: 985 case JVM_CONSTANT_InterfaceMethodref: 986 case JVM_CONSTANT_NameAndType: 987 case JVM_CONSTANT_Utf8: 988 case JVM_CONSTANT_Integer: 989 case JVM_CONSTANT_Float: 990 case JVM_CONSTANT_MethodHandle: 991 case JVM_CONSTANT_MethodType: 992 case JVM_CONSTANT_Dynamic: 993 case JVM_CONSTANT_InvokeDynamic: 994 if (tag != cp->tag_at(i).value()) { 995 report_error("tag mismatch: wrong class files?"); 996 return; 997 } 998 break; 999 1000 case JVM_CONSTANT_Class: 1001 if (tag == JVM_CONSTANT_UnresolvedClass) { 1002 Klass* k = cp->klass_at(i, CHECK); 1003 tty->print_cr("Warning: entry was unresolved in the replay data: %s", k->name()->as_utf8()); 1004 } else if (tag != JVM_CONSTANT_Class) { 1005 report_error("Unexpected tag"); 1006 return; 1007 } 1008 break; 1009 1010 case 0: 1011 if (parsed_two_word == i) continue; 1012 1013 default: 1014 fatal("Unexpected tag: %d", cp->tag_at(i).value()); 1015 break; 1016 } 1017 1018 } 1019 } 1020 1021 class InlineTypeFieldInitializer : public FieldClosure { 1022 oop _vt; 1023 CompileReplay* _replay; 1024 public: 1025 InlineTypeFieldInitializer(oop vt, CompileReplay* replay) 1026 : _vt(vt), _replay(replay) {} 1027 1028 void do_field(fieldDescriptor* fd) { 1029 BasicType bt = fd->field_type(); 1030 const char* string_value = bt != T_PRIMITIVE_OBJECT ? _replay->parse_escaped_string() : NULL; 1031 switch (bt) { 1032 case T_BYTE: { 1033 int value = atoi(string_value); 1034 _vt->byte_field_put(fd->offset(), value); 1035 break; 1036 } 1037 case T_BOOLEAN: { 1038 int value = atoi(string_value); 1039 _vt->bool_field_put(fd->offset(), value); 1040 break; 1041 } 1042 case T_SHORT: { 1043 int value = atoi(string_value); 1044 _vt->short_field_put(fd->offset(), value); 1045 break; 1046 } 1047 case T_CHAR: { 1048 int value = atoi(string_value); 1049 _vt->char_field_put(fd->offset(), value); 1050 break; 1051 } 1052 case T_INT: { 1053 int value = atoi(string_value); 1054 _vt->int_field_put(fd->offset(), value); 1055 break; 1056 } 1057 case T_LONG: { 1058 jlong value; 1059 if (sscanf(string_value, JLONG_FORMAT, &value) != 1) { 1060 fprintf(stderr, "Error parsing long: %s\n", string_value); 1061 break; 1062 } 1063 _vt->long_field_put(fd->offset(), value); 1064 break; 1065 } 1066 case T_FLOAT: { 1067 float value = atof(string_value); 1068 _vt->float_field_put(fd->offset(), value); 1069 break; 1070 } 1071 case T_DOUBLE: { 1072 double value = atof(string_value); 1073 _vt->double_field_put(fd->offset(), value); 1074 break; 1075 } 1076 case T_ARRAY: 1077 case T_OBJECT: { 1078 JavaThread* THREAD = JavaThread::current(); 1079 bool res = _replay->process_staticfield_reference(string_value, _vt, fd, THREAD); 1080 assert(res, "should succeed for arrays & objects"); 1081 break; 1082 } 1083 case T_PRIMITIVE_OBJECT: { 1084 InlineKlass* vk = InlineKlass::cast(fd->field_holder()->get_inline_type_field_klass(fd->index())); 1085 if (fd->is_inlined()) { 1086 int field_offset = fd->offset() - vk->first_field_offset(); 1087 oop obj = cast_to_oop(cast_from_oop<address>(_vt) + field_offset); 1088 InlineTypeFieldInitializer init_fields(obj, _replay); 1089 vk->do_nonstatic_fields(&init_fields); 1090 } else { 1091 oop value = vk->allocate_instance(JavaThread::current()); 1092 _vt->obj_field_put(fd->offset(), value); 1093 } 1094 break; 1095 } 1096 default: { 1097 fatal("Unhandled type: %s", type2name(bt)); 1098 } 1099 } 1100 } 1101 }; 1102 1103 bool process_staticfield_reference(const char* field_signature, oop java_mirror, fieldDescriptor* fd, TRAPS) { 1104 if (field_signature[0] == JVM_SIGNATURE_ARRAY) { 1105 int length = parse_int("array length"); 1106 oop value = NULL; 1107 1108 if (field_signature[1] == JVM_SIGNATURE_ARRAY) { 1109 // multi dimensional array 1110 Klass* k = resolve_klass(field_signature, CHECK_(true)); 1111 ArrayKlass* kelem = (ArrayKlass *)k; 1112 int rank = 0; 1113 while (field_signature[rank] == JVM_SIGNATURE_ARRAY) { 1114 rank++; 1115 } 1116 jint* dims = NEW_RESOURCE_ARRAY(jint, rank); 1117 dims[0] = length; 1118 for (int i = 1; i < rank; i++) { 1119 dims[i] = 1; // These aren't relevant to the compiler 1120 } 1121 value = kelem->multi_allocate(rank, dims, CHECK_(true)); 1122 } else { 1123 if (strcmp(field_signature, "[B") == 0) { 1124 value = oopFactory::new_byteArray(length, CHECK_(true)); 1125 } else if (strcmp(field_signature, "[Z") == 0) { 1126 value = oopFactory::new_boolArray(length, CHECK_(true)); 1127 } else if (strcmp(field_signature, "[C") == 0) { 1128 value = oopFactory::new_charArray(length, CHECK_(true)); 1129 } else if (strcmp(field_signature, "[S") == 0) { 1130 value = oopFactory::new_shortArray(length, CHECK_(true)); 1131 } else if (strcmp(field_signature, "[F") == 0) { 1132 value = oopFactory::new_floatArray(length, CHECK_(true)); 1133 } else if (strcmp(field_signature, "[D") == 0) { 1134 value = oopFactory::new_doubleArray(length, CHECK_(true)); 1135 } else if (strcmp(field_signature, "[I") == 0) { 1136 value = oopFactory::new_intArray(length, CHECK_(true)); 1137 } else if (strcmp(field_signature, "[J") == 0) { 1138 value = oopFactory::new_longArray(length, CHECK_(true)); 1139 } else if (field_signature[0] == JVM_SIGNATURE_ARRAY && 1140 field_signature[1] == JVM_SIGNATURE_CLASS) { 1141 Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true)); 1142 value = oopFactory::new_objArray(kelem, length, CHECK_(true)); 1143 } else if (field_signature[0] == JVM_SIGNATURE_ARRAY && 1144 field_signature[1] == JVM_SIGNATURE_PRIMITIVE_OBJECT) { 1145 Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true)); 1146 value = oopFactory::new_valueArray(kelem, length, CHECK_(true)); 1147 } else { 1148 report_error("unhandled array staticfield"); 1149 } 1150 } 1151 java_mirror->obj_field_put(fd->offset(), value); 1152 return true; 1153 } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) { 1154 const char* string_value = parse_escaped_string(); 1155 Handle value = java_lang_String::create_from_str(string_value, CHECK_(true)); 1156 java_mirror->obj_field_put(fd->offset(), value()); 1157 return true; 1158 } else if (field_signature[0] == 'L') { 1159 const char* instance = parse_escaped_string(); 1160 Klass* k = resolve_klass(instance, CHECK_(true)); 1161 oop value = InstanceKlass::cast(k)->allocate_instance(CHECK_(true)); 1162 java_mirror->obj_field_put(fd->offset(), value); 1163 return true; 1164 } 1165 return false; 1166 } 1167 1168 // Initialize a class and fill in the value for a static field. 1169 // This is useful when the compile was dependent on the value of 1170 // static fields but it's impossible to properly rerun the static 1171 // initializer. 1172 void process_staticfield(TRAPS) { 1173 InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK); 1174 1175 if (k == NULL || ReplaySuppressInitializers == 0 || 1176 (ReplaySuppressInitializers == 2 && k->class_loader() == NULL)) { 1177 skip_remaining(); 1178 return; 1179 } 1180 1181 assert(k->is_initialized(), "must be"); 1182 1183 const char* field_name = parse_escaped_string(); 1184 const char* field_signature = parse_string(); 1185 fieldDescriptor fd; 1186 Symbol* name = SymbolTable::new_symbol(field_name); 1187 Symbol* sig = SymbolTable::new_symbol(field_signature); 1188 if (!k->find_local_field(name, sig, &fd) || 1189 !fd.is_static() || 1190 fd.has_initial_value()) { 1191 report_error(field_name); 1192 return; 1193 } 1194 1195 oop java_mirror = k->java_mirror(); 1196 if (strcmp(field_signature, "I") == 0) { 1197 const char* string_value = parse_escaped_string(); 1198 int value = atoi(string_value); 1199 java_mirror->int_field_put(fd.offset(), value); 1200 } else if (strcmp(field_signature, "B") == 0) { 1201 const char* string_value = parse_escaped_string(); 1202 int value = atoi(string_value); 1203 java_mirror->byte_field_put(fd.offset(), value); 1204 } else if (strcmp(field_signature, "C") == 0) { 1205 const char* string_value = parse_escaped_string(); 1206 int value = atoi(string_value); 1207 java_mirror->char_field_put(fd.offset(), value); 1208 } else if (strcmp(field_signature, "S") == 0) { 1209 const char* string_value = parse_escaped_string(); 1210 int value = atoi(string_value); 1211 java_mirror->short_field_put(fd.offset(), value); 1212 } else if (strcmp(field_signature, "Z") == 0) { 1213 const char* string_value = parse_escaped_string(); 1214 int value = atoi(string_value); 1215 java_mirror->bool_field_put(fd.offset(), value); 1216 } else if (strcmp(field_signature, "J") == 0) { 1217 const char* string_value = parse_escaped_string(); 1218 jlong value; 1219 if (sscanf(string_value, JLONG_FORMAT, &value) != 1) { 1220 fprintf(stderr, "Error parsing long: %s\n", string_value); 1221 return; 1222 } 1223 java_mirror->long_field_put(fd.offset(), value); 1224 } else if (strcmp(field_signature, "F") == 0) { 1225 const char* string_value = parse_escaped_string(); 1226 float value = atof(string_value); 1227 java_mirror->float_field_put(fd.offset(), value); 1228 } else if (strcmp(field_signature, "D") == 0) { 1229 const char* string_value = parse_escaped_string(); 1230 double value = atof(string_value); 1231 java_mirror->double_field_put(fd.offset(), value); 1232 } else if (field_signature[0] == JVM_SIGNATURE_PRIMITIVE_OBJECT) { 1233 Klass* kelem = resolve_klass(field_signature, CHECK); 1234 InlineKlass* vk = InlineKlass::cast(kelem); 1235 oop value = vk->allocate_instance(CHECK); 1236 InlineTypeFieldInitializer init_fields(value, this); 1237 vk->do_nonstatic_fields(&init_fields); 1238 java_mirror->obj_field_put(fd.offset(), value); 1239 } else { 1240 bool res = process_staticfield_reference(field_signature, java_mirror, &fd, CHECK); 1241 if (!res) { 1242 report_error("unhandled staticfield"); 1243 } 1244 } 1245 } 1246 1247 #if INCLUDE_JVMTI 1248 // JvmtiExport <field> <value> 1249 void process_JvmtiExport(TRAPS) { 1250 const char* field = parse_string(); 1251 bool value = parse_int("JvmtiExport flag") != 0; 1252 if (strcmp(field, "can_access_local_variables") == 0) { 1253 JvmtiExport::set_can_access_local_variables(value); 1254 } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) { 1255 JvmtiExport::set_can_hotswap_or_post_breakpoint(value); 1256 } else if (strcmp(field, "can_post_on_exceptions") == 0) { 1257 JvmtiExport::set_can_post_on_exceptions(value); 1258 } else { 1259 report_error("Unrecognized JvmtiExport directive"); 1260 } 1261 } 1262 #endif // INCLUDE_JVMTI 1263 1264 // Create and initialize a record for a ciMethod 1265 ciMethodRecord* new_ciMethod(Method* method) { 1266 ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord); 1267 rec->_klass_name = method->method_holder()->name()->as_utf8(); 1268 rec->_method_name = method->name()->as_utf8(); 1269 rec->_signature = method->signature()->as_utf8(); 1270 _ci_method_records.append(rec); 1271 return rec; 1272 } 1273 1274 // Lookup data for a ciMethod 1275 ciMethodRecord* find_ciMethodRecord(Method* method) { 1276 const char* klass_name = method->method_holder()->name()->as_utf8(); 1277 const char* method_name = method->name()->as_utf8(); 1278 const char* signature = method->signature()->as_utf8(); 1279 for (int i = 0; i < _ci_method_records.length(); i++) { 1280 ciMethodRecord* rec = _ci_method_records.at(i); 1281 if (strcmp(rec->_klass_name, klass_name) == 0 && 1282 strcmp(rec->_method_name, method_name) == 0 && 1283 strcmp(rec->_signature, signature) == 0) { 1284 return rec; 1285 } 1286 } 1287 return NULL; 1288 } 1289 1290 // Create and initialize a record for a ciInstanceKlass which was present at replay dump time. 1291 void new_ciInstanceKlass(const InstanceKlass* klass) { 1292 ciInstanceKlassRecord* rec = NEW_RESOURCE_OBJ(ciInstanceKlassRecord); 1293 rec->_klass = klass; 1294 oop java_mirror = klass->java_mirror(); 1295 Handle h_java_mirror(_thread, java_mirror); 1296 rec->_java_mirror = JNIHandles::make_global(h_java_mirror); 1297 _ci_instance_klass_records.append(rec); 1298 } 1299 1300 // Check if a ciInstanceKlass was present at replay dump time for a klass. 1301 ciInstanceKlassRecord* find_ciInstanceKlass(const InstanceKlass* klass) { 1302 for (int i = 0; i < _ci_instance_klass_records.length(); i++) { 1303 ciInstanceKlassRecord* rec = _ci_instance_klass_records.at(i); 1304 if (klass == rec->_klass) { 1305 // ciInstanceKlass for this klass was resolved. 1306 return rec; 1307 } 1308 } 1309 return NULL; 1310 } 1311 1312 // Create and initialize a record for a ciMethodData 1313 ciMethodDataRecord* new_ciMethodData(Method* method) { 1314 ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord); 1315 rec->_klass_name = method->method_holder()->name()->as_utf8(); 1316 rec->_method_name = method->name()->as_utf8(); 1317 rec->_signature = method->signature()->as_utf8(); 1318 _ci_method_data_records.append(rec); 1319 return rec; 1320 } 1321 1322 // Lookup data for a ciMethodData 1323 ciMethodDataRecord* find_ciMethodDataRecord(Method* method) { 1324 const char* klass_name = method->method_holder()->name()->as_utf8(); 1325 const char* method_name = method->name()->as_utf8(); 1326 const char* signature = method->signature()->as_utf8(); 1327 for (int i = 0; i < _ci_method_data_records.length(); i++) { 1328 ciMethodDataRecord* rec = _ci_method_data_records.at(i); 1329 if (strcmp(rec->_klass_name, klass_name) == 0 && 1330 strcmp(rec->_method_name, method_name) == 0 && 1331 strcmp(rec->_signature, signature) == 0) { 1332 return rec; 1333 } 1334 } 1335 return NULL; 1336 } 1337 1338 // Create and initialize a record for a ciInlineRecord 1339 ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth, int inline_late) { 1340 ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord); 1341 rec->_klass_name = method->method_holder()->name()->as_utf8(); 1342 rec->_method_name = method->name()->as_utf8(); 1343 rec->_signature = method->signature()->as_utf8(); 1344 rec->_inline_bci = bci; 1345 rec->_inline_depth = depth; 1346 rec->_inline_late = inline_late; 1347 _ci_inline_records->append(rec); 1348 return rec; 1349 } 1350 1351 // Lookup inlining data for a ciMethod 1352 ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) { 1353 if (_ci_inline_records != NULL) { 1354 return find_ciInlineRecord(_ci_inline_records, method, bci, depth); 1355 } 1356 return NULL; 1357 } 1358 1359 static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>* records, 1360 Method* method, int bci, int depth) { 1361 if (records != NULL) { 1362 const char* klass_name = method->method_holder()->name()->as_utf8(); 1363 const char* method_name = method->name()->as_utf8(); 1364 const char* signature = method->signature()->as_utf8(); 1365 for (int i = 0; i < records->length(); i++) { 1366 ciInlineRecord* rec = records->at(i); 1367 if ((rec->_inline_bci == bci) && 1368 (rec->_inline_depth == depth) && 1369 (strcmp(rec->_klass_name, klass_name) == 0) && 1370 (strcmp(rec->_method_name, method_name) == 0) && 1371 (strcmp(rec->_signature, signature) == 0)) { 1372 return rec; 1373 } 1374 } 1375 } 1376 return NULL; 1377 } 1378 1379 const char* error_message() { 1380 return _error_message; 1381 } 1382 1383 void reset() { 1384 _error_message = NULL; 1385 _ci_method_records.clear(); 1386 _ci_method_data_records.clear(); 1387 } 1388 1389 // Take an ascii string contain \u#### escapes and convert it to utf8 1390 // in place. 1391 static void unescape_string(char* value) { 1392 char* from = value; 1393 char* to = value; 1394 while (*from != '\0') { 1395 if (*from != '\\') { 1396 *from++ = *to++; 1397 } else { 1398 switch (from[1]) { 1399 case 'u': { 1400 from += 2; 1401 jchar value=0; 1402 for (int i=0; i<4; i++) { 1403 char c = *from++; 1404 switch (c) { 1405 case '0': case '1': case '2': case '3': case '4': 1406 case '5': case '6': case '7': case '8': case '9': 1407 value = (value << 4) + c - '0'; 1408 break; 1409 case 'a': case 'b': case 'c': 1410 case 'd': case 'e': case 'f': 1411 value = (value << 4) + 10 + c - 'a'; 1412 break; 1413 case 'A': case 'B': case 'C': 1414 case 'D': case 'E': case 'F': 1415 value = (value << 4) + 10 + c - 'A'; 1416 break; 1417 default: 1418 ShouldNotReachHere(); 1419 } 1420 } 1421 UNICODE::convert_to_utf8(&value, 1, to); 1422 to++; 1423 break; 1424 } 1425 case 't': *to++ = '\t'; from += 2; break; 1426 case 'n': *to++ = '\n'; from += 2; break; 1427 case 'r': *to++ = '\r'; from += 2; break; 1428 case 'f': *to++ = '\f'; from += 2; break; 1429 default: 1430 ShouldNotReachHere(); 1431 } 1432 } 1433 } 1434 *from = *to; 1435 } 1436 }; 1437 1438 void ciReplay::replay(TRAPS) { 1439 int exit_code = replay_impl(THREAD); 1440 1441 Threads::destroy_vm(); 1442 1443 vm_exit(exit_code); 1444 } 1445 1446 bool ciReplay::no_replay_state() { 1447 return replay_state == NULL; 1448 } 1449 1450 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) { 1451 if (FLAG_IS_DEFAULT(InlineDataFile)) { 1452 tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt)."); 1453 return NULL; 1454 } 1455 1456 VM_ENTRY_MARK; 1457 // Load and parse the replay data 1458 CompileReplay rp(InlineDataFile, THREAD); 1459 if (!rp.can_replay()) { 1460 tty->print_cr("ciReplay: !rp.can_replay()"); 1461 return NULL; 1462 } 1463 void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD); 1464 if (HAS_PENDING_EXCEPTION) { 1465 Handle throwable(THREAD, PENDING_EXCEPTION); 1466 CLEAR_PENDING_EXCEPTION; 1467 java_lang_Throwable::print_stack_trace(throwable, tty); 1468 tty->cr(); 1469 return NULL; 1470 } 1471 1472 if (rp.had_error()) { 1473 tty->print_cr("ciReplay: Failed on %s", rp.error_message()); 1474 return NULL; 1475 } 1476 return data; 1477 } 1478 1479 int ciReplay::replay_impl(TRAPS) { 1480 HandleMark hm(THREAD); 1481 ResourceMark rm(THREAD); 1482 1483 if (ReplaySuppressInitializers > 2) { 1484 // ReplaySuppressInitializers > 2 means that we want to allow 1485 // normal VM bootstrap but once we get into the replay itself 1486 // don't allow any initializers to be run. 1487 ReplaySuppressInitializers = 1; 1488 } 1489 1490 if (FLAG_IS_DEFAULT(ReplayDataFile)) { 1491 tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt)."); 1492 return 1; 1493 } 1494 1495 // Load and parse the replay data 1496 CompileReplay rp(ReplayDataFile, THREAD); 1497 int exit_code = 0; 1498 if (rp.can_replay()) { 1499 rp.process(THREAD); 1500 } else { 1501 exit_code = 1; 1502 return exit_code; 1503 } 1504 1505 if (HAS_PENDING_EXCEPTION) { 1506 Handle throwable(THREAD, PENDING_EXCEPTION); 1507 CLEAR_PENDING_EXCEPTION; 1508 java_lang_Throwable::print_stack_trace(throwable, tty); 1509 tty->cr(); 1510 exit_code = 2; 1511 } 1512 1513 if (rp.had_error()) { 1514 tty->print_cr("Failed on %s", rp.error_message()); 1515 exit_code = 1; 1516 } 1517 return exit_code; 1518 } 1519 1520 void ciReplay::initialize(ciMethodData* m) { 1521 if (no_replay_state()) { 1522 return; 1523 } 1524 1525 ASSERT_IN_VM; 1526 ResourceMark rm; 1527 1528 Method* method = m->get_MethodData()->method(); 1529 ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method); 1530 if (rec == NULL) { 1531 // This indicates some mismatch with the original environment and 1532 // the replay environment though it's not always enough to 1533 // interfere with reproducing a bug 1534 tty->print_cr("Warning: requesting ciMethodData record for method with no data: "); 1535 method->print_name(tty); 1536 tty->cr(); 1537 } else { 1538 m->_state = rec->_state; 1539 m->_invocation_counter = rec->_invocation_counter; 1540 if (rec->_data_length != 0) { 1541 assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) || 1542 m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree"); 1543 1544 // Write the correct ciObjects back into the profile data 1545 ciEnv* env = ciEnv::current(); 1546 for (int i = 0; i < rec->_classes_length; i++) { 1547 Klass *k = rec->_classes[i]; 1548 // In case this class pointer is is tagged, preserve the tag bits 1549 intptr_t status = 0; 1550 if (k != NULL) { 1551 status = ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]); 1552 } 1553 rec->_data[rec->_classes_offsets[i]] = status; 1554 } 1555 for (int i = 0; i < rec->_methods_length; i++) { 1556 Method *m = rec->_methods[i]; 1557 *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) = 1558 env->get_metadata(m); 1559 } 1560 // Copy the updated profile data into place as intptr_ts 1561 #ifdef _LP64 1562 Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length); 1563 #else 1564 Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length); 1565 #endif 1566 } 1567 1568 // copy in the original header 1569 Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length); 1570 } 1571 } 1572 1573 1574 bool ciReplay::should_not_inline(ciMethod* method) { 1575 if (no_replay_state()) { 1576 return false; 1577 } 1578 VM_ENTRY_MARK; 1579 // ciMethod without a record shouldn't be inlined. 1580 return replay_state->find_ciMethodRecord(method->get_Method()) == NULL; 1581 } 1582 1583 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth, bool& should_delay) { 1584 if (data != NULL) { 1585 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data; 1586 VM_ENTRY_MARK; 1587 // Inline record are ordered by bci and depth. 1588 ciInlineRecord* record = CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth); 1589 if (record == NULL) { 1590 return false; 1591 } 1592 should_delay = record->_inline_late; 1593 return true; 1594 } else if (replay_state != NULL) { 1595 VM_ENTRY_MARK; 1596 // Inline record are ordered by bci and depth. 1597 ciInlineRecord* record = replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth); 1598 if (record == NULL) { 1599 return false; 1600 } 1601 should_delay = record->_inline_late; 1602 return true; 1603 } 1604 return false; 1605 } 1606 1607 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) { 1608 if (data != NULL) { 1609 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data; 1610 VM_ENTRY_MARK; 1611 // Inline record are ordered by bci and depth. 1612 return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL; 1613 } else if (replay_state != NULL) { 1614 VM_ENTRY_MARK; 1615 // Inline record are ordered by bci and depth. 1616 return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL; 1617 } 1618 return false; 1619 } 1620 1621 void ciReplay::initialize(ciMethod* m) { 1622 if (no_replay_state()) { 1623 return; 1624 } 1625 1626 ASSERT_IN_VM; 1627 ResourceMark rm; 1628 1629 Method* method = m->get_Method(); 1630 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method); 1631 if (rec == NULL) { 1632 // This indicates some mismatch with the original environment and 1633 // the replay environment though it's not always enough to 1634 // interfere with reproducing a bug 1635 tty->print_cr("Warning: requesting ciMethod record for method with no data: "); 1636 method->print_name(tty); 1637 tty->cr(); 1638 } else { 1639 EXCEPTION_CONTEXT; 1640 // m->_instructions_size = rec->_instructions_size; 1641 m->_instructions_size = -1; 1642 m->_interpreter_invocation_count = rec->_interpreter_invocation_count; 1643 m->_interpreter_throwout_count = rec->_interpreter_throwout_count; 1644 MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR); 1645 guarantee(mcs != NULL, "method counters allocation failed"); 1646 mcs->invocation_counter()->_counter = rec->_invocation_counter; 1647 mcs->backedge_counter()->_counter = rec->_backedge_counter; 1648 } 1649 } 1650 1651 void ciReplay::initialize(ciInstanceKlass* ci_ik, InstanceKlass* ik) { 1652 assert(!no_replay_state(), "must have replay state"); 1653 1654 ASSERT_IN_VM; 1655 ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(ik); 1656 assert(rec != NULL, "ciInstanceKlass must be whitelisted"); 1657 ci_ik->_java_mirror = CURRENT_ENV->get_instance(JNIHandles::resolve(rec->_java_mirror)); 1658 } 1659 1660 bool ciReplay::is_loaded(Method* method) { 1661 if (no_replay_state()) { 1662 return true; 1663 } 1664 1665 ASSERT_IN_VM; 1666 ResourceMark rm; 1667 1668 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method); 1669 return rec != NULL; 1670 } 1671 1672 bool ciReplay::is_klass_unresolved(const InstanceKlass* klass) { 1673 if (no_replay_state()) { 1674 return false; 1675 } 1676 1677 // Check if klass is found on whitelist. 1678 ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(klass); 1679 return rec == NULL; 1680 } 1681 1682 oop ciReplay::obj_field(oop obj, Symbol* name) { 1683 InstanceKlass* ik = InstanceKlass::cast(obj->klass()); 1684 1685 do { 1686 if (!ik->has_nonstatic_fields()) { 1687 ik = ik->java_super(); 1688 continue; 1689 } 1690 1691 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { 1692 if (fs.access_flags().is_static()) { 1693 continue; 1694 } 1695 if (fs.name() == name) { 1696 int offset = fs.offset(); 1697 #ifdef ASSERT 1698 fieldDescriptor fd = fs.field_descriptor(); 1699 assert(fd.offset() == ik->field_offset(fd.index()), "!"); 1700 #endif 1701 oop f = obj->obj_field(offset); 1702 return f; 1703 } 1704 } 1705 1706 ik = ik->java_super(); 1707 } while (ik != NULL); 1708 return NULL; 1709 } 1710 1711 oop ciReplay::obj_field(oop obj, const char *name) { 1712 Symbol* fname = SymbolTable::probe(name, (int)strlen(name)); 1713 if (fname == NULL) { 1714 return NULL; 1715 } 1716 return obj_field(obj, fname); 1717 }