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