1 /* 2 * Copyright (c) 2013, 2024, 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/klass.inline.hpp" 46 #include "oops/method.inline.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "oops/resolvedIndyEntry.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 == nullptr) { 151 fprintf(stderr, "ERROR: Can't open replay file %s\n", filename); 152 } 153 154 _ci_inline_records = nullptr; 155 _error_message = nullptr; 156 157 _buffer_length = 32; 158 _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length); 159 _bufptr = _buffer; 160 161 _imethod = nullptr; 162 _iklass = nullptr; 163 _entry_bci = 0; 164 _comp_level = 0; 165 _version = 0; 166 167 test(); 168 } 169 170 ~CompileReplay() { 171 if (_stream != nullptr) 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 != nullptr || _thread->has_pending_exception(); 188 } 189 190 bool can_replay() { 191 return !(_stream == nullptr || 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 nullptr; 251 } 252 return str; 253 } 254 255 char* parse_string() { 256 if (had_error()) return nullptr; 257 258 skip_ws(); 259 return scan_and_terminate(' '); 260 } 261 262 char* parse_quoted_string() { 263 if (had_error()) return nullptr; 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 != nullptr) { 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 == nullptr) { 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 nullptr; 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 nullptr; 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 != nullptr) { 361 Symbol* sym = SymbolTable::new_symbol(str); 362 return sym; 363 } 364 return nullptr; 365 } 366 367 bool parse_terminator() { 368 char* terminator = parse_string(); 369 if (terminator != nullptr && 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 = nullptr; 381 char* ref = parse_string(); 382 if (strcmp(ref, "bci") == 0) { 383 Method* m = parse_method(CHECK_NULL); 384 if (m == nullptr) { 385 return nullptr; 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 nullptr; 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 nullptr; 406 } 407 bytecode.verify(); 408 int index = bytecode.index(); 409 410 CallInfo callInfo; 411 Bytecodes::Code bc = bytecode.invoke_code(); 412 LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, bc, CHECK_NULL); 413 414 oop appendix = nullptr; 415 Method* adapter_method = nullptr; 416 int pool_index = 0; 417 418 if (bytecode.is_invokedynamic()) { 419 cp->cache()->set_dynamic_call(callInfo, index); 420 421 appendix = cp->resolved_reference_from_indy(index); 422 adapter_method = cp->resolved_indy_entry_at(index)->method(); 423 pool_index = cp->resolved_indy_entry_at(index)->constant_pool_index(); 424 } else if (bytecode.is_invokehandle()) { 425 #ifdef ASSERT 426 Klass* holder = cp->klass_ref_at(index, bytecode.code(), CHECK_NULL); 427 Symbol* name = cp->name_ref_at(index, bytecode.code()); 428 assert(MethodHandles::is_signature_polymorphic_name(holder, name), ""); 429 #endif 430 ResolvedMethodEntry* method_entry = cp->cache()->set_method_handle(index, callInfo); 431 appendix = cp->cache()->appendix_if_resolved(method_entry); 432 adapter_method = method_entry->method(); 433 pool_index = method_entry->constant_pool_index(); 434 } else { 435 report_error("no dynamic invoke found"); 436 return nullptr; 437 } 438 char* dyno_ref = parse_string(); 439 if (strcmp(dyno_ref, "<appendix>") == 0) { 440 obj = appendix; 441 } else if (strcmp(dyno_ref, "<adapter>") == 0) { 442 if (!parse_terminator()) { 443 report_error("no dynamic invoke found"); 444 return nullptr; 445 } 446 Method* adapter = adapter_method; 447 if (adapter == nullptr) { 448 report_error("no adapter found"); 449 return nullptr; 450 } 451 return adapter->method_holder(); 452 } else if (strcmp(dyno_ref, "<bsm>") == 0) { 453 BootstrapInfo bootstrap_specifier(cp, pool_index, index); 454 obj = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), CHECK_NULL); 455 } else { 456 report_error("unrecognized token"); 457 return nullptr; 458 } 459 } else { 460 // constant pool ref (MethodHandle) 461 if (strcmp(ref, "cpi") != 0) { 462 report_error("unexpected token"); 463 return nullptr; 464 } 465 466 Klass* k = parse_klass(CHECK_NULL); 467 if (k == nullptr) { 468 return nullptr; 469 } 470 InstanceKlass* ik = InstanceKlass::cast(k); 471 const constantPoolHandle cp(Thread::current(), ik->constants()); 472 473 int cpi = parse_int("cpi"); 474 475 if (cpi >= cp->length()) { 476 report_error("bad cpi"); 477 return nullptr; 478 } 479 if (!cp->tag_at(cpi).is_method_handle()) { 480 report_error("no method handle found at cpi"); 481 return nullptr; 482 } 483 ik->link_class(CHECK_NULL); 484 obj = cp->resolve_possibly_cached_constant_at(cpi, CHECK_NULL); 485 } 486 if (obj == nullptr) { 487 report_error("null cp object found"); 488 return nullptr; 489 } 490 Klass* k = nullptr; 491 skip_ws(); 492 // loop: read fields 493 char* field = nullptr; 494 do { 495 field = parse_string(); 496 if (field == nullptr) { 497 report_error("no field found"); 498 return nullptr; 499 } 500 if (strcmp(field, ";") == 0) { 501 break; 502 } 503 // raw Method* 504 if (strcmp(field, "<vmtarget>") == 0) { 505 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(obj); 506 k = (vmtarget == nullptr) ? nullptr : vmtarget->method_holder(); 507 if (k == nullptr) { 508 report_error("null vmtarget found"); 509 return nullptr; 510 } 511 if (!parse_terminator()) { 512 report_error("missing terminator"); 513 return nullptr; 514 } 515 return k; 516 } 517 obj = ciReplay::obj_field(obj, field); 518 // array 519 if (obj != nullptr && obj->is_objArray()) { 520 objArrayOop arr = (objArrayOop)obj; 521 int index = parse_int("index"); 522 if (index >= arr->length()) { 523 report_error("bad array index"); 524 return nullptr; 525 } 526 obj = arr->obj_at(index); 527 } 528 } while (obj != nullptr); 529 if (obj == nullptr) { 530 report_error("null field found"); 531 return nullptr; 532 } 533 k = obj->klass(); 534 return k; 535 } 536 537 // Parse a valid klass name and look it up 538 // syntax: <name> 539 // syntax: <constant pool ref> 540 Klass* parse_klass(TRAPS) { 541 skip_ws(); 542 // check for constant pool object reference (for a dynamic/hidden class) 543 bool cp_ref = (*_bufptr == '@'); 544 if (cp_ref) { 545 ++_bufptr; 546 Klass* k = parse_cp_ref(CHECK_NULL); 547 if (k != nullptr && !k->is_hidden()) { 548 report_error("expected hidden class"); 549 return nullptr; 550 } 551 return k; 552 } 553 char* str = parse_escaped_string(); 554 Symbol* klass_name = SymbolTable::new_symbol(str); 555 if (klass_name != nullptr) { 556 Klass* k = nullptr; 557 if (_iklass != nullptr) { 558 k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding(); 559 } else { 560 k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD); 561 } 562 if (HAS_PENDING_EXCEPTION) { 563 oop throwable = PENDING_EXCEPTION; 564 java_lang_Throwable::print(throwable, tty); 565 tty->cr(); 566 report_error(str); 567 if (ReplayIgnoreInitErrors) { 568 CLEAR_PENDING_EXCEPTION; 569 _error_message = nullptr; 570 } 571 return nullptr; 572 } 573 return k; 574 } 575 return nullptr; 576 } 577 578 // Lookup a klass 579 Klass* resolve_klass(const char* klass, TRAPS) { 580 Symbol* klass_name = SymbolTable::new_symbol(klass); 581 return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD); 582 } 583 584 // Parse the standard tuple of <klass> <name> <signature> 585 Method* parse_method(TRAPS) { 586 InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL); 587 if (k == nullptr) { 588 report_error("Can't find holder klass"); 589 return nullptr; 590 } 591 Symbol* method_name = parse_symbol(); 592 Symbol* method_signature = parse_symbol(); 593 Method* m = k->find_method(method_name, method_signature); 594 if (m == nullptr) { 595 report_error("Can't find method"); 596 } 597 return m; 598 } 599 600 int get_line(int c) { 601 int buffer_pos = 0; 602 while(c != EOF) { 603 if (buffer_pos + 1 >= _buffer_length) { 604 int new_length = _buffer_length * 2; 605 // Next call will throw error in case of OOM. 606 _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length); 607 _buffer_length = new_length; 608 } 609 if (c == '\n') { 610 c = getc(_stream); // get next char 611 break; 612 } else if (c == '\r') { 613 // skip LF 614 } else { 615 _buffer[buffer_pos++] = c; 616 } 617 c = getc(_stream); 618 } 619 // null terminate it, reset the pointer 620 _buffer[buffer_pos] = '\0'; // NL or EOF 621 _bufptr = _buffer; 622 return c; 623 } 624 625 // Process each line of the replay file executing each command until 626 // the file ends. 627 void process(TRAPS) { 628 int line_no = 1; 629 int c = getc(_stream); 630 while(c != EOF) { 631 c = get_line(c); 632 process_command(false, THREAD); 633 if (had_error()) { 634 int pos = _bufptr - _buffer + 1; 635 tty->print_cr("Error while parsing line %d at position %d: %s\n", line_no, pos, _error_message); 636 if (ReplayIgnoreInitErrors) { 637 CLEAR_PENDING_EXCEPTION; 638 _error_message = nullptr; 639 } else { 640 return; 641 } 642 } 643 line_no++; 644 } 645 reset(); 646 } 647 648 void process_command(bool is_replay_inline, TRAPS) { 649 char* cmd = parse_string(); 650 if (cmd == nullptr) { 651 return; 652 } 653 if (strcmp("#", cmd) == 0) { 654 // comment line, print or ignore 655 if (Verbose) { 656 tty->print_cr("# %s", _bufptr); 657 } 658 skip_remaining(); 659 } else if (strcmp("version", cmd) == 0) { 660 _version = parse_int("version"); 661 if (_version < 0 || _version > REPLAY_VERSION) { 662 tty->print_cr("# unrecognized version %d, expected 0 <= version <= %d", _version, REPLAY_VERSION); 663 } 664 } else if (strcmp("compile", cmd) == 0) { 665 process_compile(CHECK); 666 } else if (!is_replay_inline) { 667 if (strcmp("ciMethod", cmd) == 0) { 668 process_ciMethod(CHECK); 669 } else if (strcmp("ciMethodData", cmd) == 0) { 670 process_ciMethodData(CHECK); 671 } else if (strcmp("staticfield", cmd) == 0) { 672 process_staticfield(CHECK); 673 } else if (strcmp("ciInstanceKlass", cmd) == 0) { 674 process_ciInstanceKlass(CHECK); 675 } else if (strcmp("instanceKlass", cmd) == 0) { 676 process_instanceKlass(CHECK); 677 #if INCLUDE_JVMTI 678 } else if (strcmp("JvmtiExport", cmd) == 0) { 679 process_JvmtiExport(CHECK); 680 #endif // INCLUDE_JVMTI 681 } else { 682 report_error("unknown command"); 683 } 684 } else { 685 report_error("unknown command"); 686 } 687 if (!had_error() && *_bufptr != '\0') { 688 report_error("line not properly terminated"); 689 } 690 } 691 692 // validation of comp_level 693 bool is_valid_comp_level(int comp_level) { 694 const int msg_len = 256; 695 char* msg = nullptr; 696 if (!is_compile(comp_level)) { 697 msg = NEW_RESOURCE_ARRAY(char, msg_len); 698 jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level); 699 } else if (is_c1_compile(comp_level) && !CompilerConfig::is_c1_enabled()) { 700 msg = NEW_RESOURCE_ARRAY(char, msg_len); 701 jio_snprintf(msg, msg_len, "compilation level %d requires C1", comp_level); 702 } else if (is_c2_compile(comp_level) && !CompilerConfig::is_c2_enabled()) { 703 msg = NEW_RESOURCE_ARRAY(char, msg_len); 704 jio_snprintf(msg, msg_len, "compilation level %d requires C2", comp_level); 705 } 706 if (msg != nullptr) { 707 report_error(msg); 708 return false; 709 } 710 return true; 711 } 712 713 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <klass> <name> <signature>)* 714 void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) { 715 _imethod = m; 716 _iklass = imethod->holder(); 717 _entry_bci = entry_bci; 718 _comp_level = comp_level; 719 int line_no = 1; 720 int c = getc(_stream); 721 while(c != EOF) { 722 c = get_line(c); 723 process_command(true, CHECK_NULL); 724 if (had_error()) { 725 tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message); 726 tty->print_cr("%s", _buffer); 727 return nullptr; 728 } 729 if (_ci_inline_records != nullptr && _ci_inline_records->length() > 0) { 730 // Found inlining record for the requested method. 731 return _ci_inline_records; 732 } 733 line_no++; 734 } 735 return nullptr; 736 } 737 738 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <inline_late> <klass> <name> <signature>)* 739 void process_compile(TRAPS) { 740 Method* method = parse_method(CHECK); 741 if (had_error()) return; 742 int entry_bci = parse_int("entry_bci"); 743 int comp_level = parse_int("comp_level"); 744 if (!is_valid_comp_level(comp_level)) { 745 return; 746 } 747 if (_imethod != nullptr) { 748 // Replay Inlining 749 if (entry_bci != _entry_bci || comp_level != _comp_level) { 750 return; 751 } 752 const char* iklass_name = _imethod->method_holder()->name()->as_utf8(); 753 const char* imethod_name = _imethod->name()->as_utf8(); 754 const char* isignature = _imethod->signature()->as_utf8(); 755 const char* klass_name = method->method_holder()->name()->as_utf8(); 756 const char* method_name = method->name()->as_utf8(); 757 const char* signature = method->signature()->as_utf8(); 758 if (strcmp(iklass_name, klass_name) != 0 || 759 strcmp(imethod_name, method_name) != 0 || 760 strcmp(isignature, signature) != 0) { 761 return; 762 } 763 } 764 int inline_count = 0; 765 if (parse_tag_and_count("inline", inline_count)) { 766 // Record inlining data 767 _ci_inline_records = new GrowableArray<ciInlineRecord*>(); 768 for (int i = 0; i < inline_count; i++) { 769 int depth = parse_int("inline_depth"); 770 int bci = parse_int("inline_bci"); 771 if (had_error()) { 772 break; 773 } 774 int inline_late = 0; 775 if (_version >= 2) { 776 inline_late = parse_int("inline_late"); 777 if (had_error()) { 778 break; 779 } 780 } 781 782 Method* inl_method = parse_method(CHECK); 783 if (had_error()) { 784 break; 785 } 786 new_ciInlineRecord(inl_method, bci, depth, inline_late); 787 } 788 } 789 if (_imethod != nullptr) { 790 return; // Replay Inlining 791 } 792 InstanceKlass* ik = method->method_holder(); 793 ik->initialize(THREAD); 794 if (HAS_PENDING_EXCEPTION) { 795 oop throwable = PENDING_EXCEPTION; 796 java_lang_Throwable::print(throwable, tty); 797 tty->cr(); 798 if (ReplayIgnoreInitErrors) { 799 CLEAR_PENDING_EXCEPTION; 800 ik->set_init_state(InstanceKlass::fully_initialized); 801 } else { 802 return; 803 } 804 } 805 // Make sure the existence of a prior compile doesn't stop this one 806 nmethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code(); 807 if (nm != nullptr) { 808 nm->make_not_entrant(); 809 } 810 replay_state = this; 811 CompileBroker::compile_method(methodHandle(THREAD, method), entry_bci, comp_level, 812 methodHandle(), 0, true, CompileTask::Reason_Replay, THREAD); 813 replay_state = nullptr; 814 } 815 816 // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size> 817 void process_ciMethod(TRAPS) { 818 Method* method = parse_method(CHECK); 819 if (had_error()) return; 820 ciMethodRecord* rec = new_ciMethod(method); 821 rec->_invocation_counter = parse_int("invocation_counter"); 822 rec->_backedge_counter = parse_int("backedge_counter"); 823 rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count"); 824 rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count"); 825 rec->_instructions_size = parse_int("instructions_size"); 826 } 827 828 // ciMethodData <klass> <name> <signature> <state> <invocation_counter> orig <length> <byte>* data <length> <ptr>* oops <length> (<offset> <klass>)* methods <length> (<offset> <klass> <name> <signature>)* 829 void process_ciMethodData(TRAPS) { 830 Method* method = parse_method(CHECK); 831 if (had_error()) return; 832 /* just copied from Method, to build interpret data*/ 833 834 // To be properly initialized, some profiling in the MDO needs the 835 // method to be rewritten (number of arguments at a call for instance) 836 method->method_holder()->link_class(CHECK); 837 assert(method->method_data() == nullptr, "Should only be initialized once"); 838 method->build_profiling_method_data(methodHandle(THREAD, method), CHECK); 839 840 // collect and record all the needed information for later 841 ciMethodDataRecord* rec = new_ciMethodData(method); 842 rec->_state = parse_int("state"); 843 if (_version < 1) { 844 parse_int("current_mileage"); 845 } else { 846 rec->_invocation_counter = parse_int("invocation_counter"); 847 } 848 849 rec->_orig_data = parse_data("orig", rec->_orig_data_length); 850 if (rec->_orig_data == nullptr) { 851 return; 852 } 853 rec->_data = parse_intptr_data("data", rec->_data_length); 854 if (rec->_data == nullptr) { 855 return; 856 } 857 if (!parse_tag_and_count("oops", rec->_classes_length)) { 858 return; 859 } 860 rec->_classes = NEW_RESOURCE_ARRAY(Klass*, rec->_classes_length); 861 rec->_classes_offsets = NEW_RESOURCE_ARRAY(int, rec->_classes_length); 862 for (int i = 0; i < rec->_classes_length; i++) { 863 int offset = parse_int("offset"); 864 if (had_error()) { 865 return; 866 } 867 Klass* k = parse_klass(CHECK); 868 rec->_classes_offsets[i] = offset; 869 rec->_classes[i] = k; 870 } 871 872 if (!parse_tag_and_count("methods", rec->_methods_length)) { 873 return; 874 } 875 rec->_methods = NEW_RESOURCE_ARRAY(Method*, rec->_methods_length); 876 rec->_methods_offsets = NEW_RESOURCE_ARRAY(int, rec->_methods_length); 877 for (int i = 0; i < rec->_methods_length; i++) { 878 int offset = parse_int("offset"); 879 if (had_error()) { 880 return; 881 } 882 Method* m = parse_method(CHECK); 883 rec->_methods_offsets[i] = offset; 884 rec->_methods[i] = m; 885 } 886 } 887 888 // instanceKlass <name> 889 // instanceKlass <constant pool ref> # <original hidden class name> 890 // 891 // Loads and initializes the klass 'name'. This can be used to 892 // create particular class loading environments 893 void process_instanceKlass(TRAPS) { 894 // just load the referenced class 895 Klass* k = parse_klass(CHECK); 896 897 if (_version >= 1) { 898 if (!_protection_domain_initialized && k != nullptr) { 899 assert(_protection_domain() == nullptr, "must be uninitialized"); 900 // The first entry is the holder class of the method for which a replay compilation is requested. 901 // Use the same protection domain to load all subsequent classes in order to resolve all classes 902 // in signatures of inlinees. This ensures that inlining can be done as stated in the replay file. 903 _protection_domain = Handle(_thread, k->protection_domain()); 904 } 905 906 _protection_domain_initialized = true; 907 } 908 909 if (k == nullptr) { 910 return; 911 } 912 const char* comment = parse_string(); 913 bool is_comment = comment != nullptr && strcmp(comment, "#") == 0; 914 if (k->is_hidden() != is_comment) { 915 report_error("hidden class with comment expected"); 916 return; 917 } 918 // comment, print or ignore 919 if (is_comment) { 920 if (Verbose) { 921 const char* hidden = parse_string(); 922 tty->print_cr("Found %s for %s", k->name()->as_quoted_ascii(), hidden); 923 } 924 skip_remaining(); 925 } 926 } 927 928 // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag* 929 // 930 // Load the klass 'name' and link or initialize it. Verify that the 931 // constant pool is the same length as 'length' and make sure the 932 // constant pool tags are in the same state. 933 void process_ciInstanceKlass(TRAPS) { 934 InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK); 935 if (k == nullptr) { 936 skip_remaining(); 937 return; 938 } 939 int is_linked = parse_int("is_linked"); 940 int is_initialized = parse_int("is_initialized"); 941 int length = parse_int("length"); 942 if (is_initialized) { 943 k->initialize(THREAD); 944 if (HAS_PENDING_EXCEPTION) { 945 oop throwable = PENDING_EXCEPTION; 946 java_lang_Throwable::print(throwable, tty); 947 tty->cr(); 948 if (ReplayIgnoreInitErrors) { 949 CLEAR_PENDING_EXCEPTION; 950 k->set_init_state(InstanceKlass::fully_initialized); 951 } else { 952 return; 953 } 954 } 955 } else if (is_linked) { 956 k->link_class(CHECK); 957 } 958 new_ciInstanceKlass(k); 959 ConstantPool* cp = k->constants(); 960 if (length != cp->length()) { 961 report_error("constant pool length mismatch: wrong class files?"); 962 return; 963 } 964 965 int parsed_two_word = 0; 966 for (int i = 1; i < length; i++) { 967 int tag = parse_int("tag"); 968 if (had_error()) { 969 return; 970 } 971 switch (cp->tag_at(i).value()) { 972 case JVM_CONSTANT_UnresolvedClass: { 973 if (tag == JVM_CONSTANT_Class) { 974 tty->print_cr("Resolving klass %s at %d", cp->klass_name_at(i)->as_utf8(), i); 975 Klass* k = cp->klass_at(i, CHECK); 976 } 977 break; 978 } 979 case JVM_CONSTANT_Long: 980 case JVM_CONSTANT_Double: 981 parsed_two_word = i + 1; 982 983 case JVM_CONSTANT_ClassIndex: 984 case JVM_CONSTANT_StringIndex: 985 case JVM_CONSTANT_String: 986 case JVM_CONSTANT_UnresolvedClassInError: 987 case JVM_CONSTANT_Fieldref: 988 case JVM_CONSTANT_Methodref: 989 case JVM_CONSTANT_InterfaceMethodref: 990 case JVM_CONSTANT_NameAndType: 991 case JVM_CONSTANT_Utf8: 992 case JVM_CONSTANT_Integer: 993 case JVM_CONSTANT_Float: 994 case JVM_CONSTANT_MethodHandle: 995 case JVM_CONSTANT_MethodType: 996 case JVM_CONSTANT_Dynamic: 997 case JVM_CONSTANT_InvokeDynamic: 998 if (tag != cp->tag_at(i).value()) { 999 report_error("tag mismatch: wrong class files?"); 1000 return; 1001 } 1002 break; 1003 1004 case JVM_CONSTANT_Class: 1005 if (tag == JVM_CONSTANT_UnresolvedClass) { 1006 Klass* k = cp->klass_at(i, CHECK); 1007 tty->print_cr("Warning: entry was unresolved in the replay data: %s", k->name()->as_utf8()); 1008 } else if (tag != JVM_CONSTANT_Class) { 1009 report_error("Unexpected tag"); 1010 return; 1011 } 1012 break; 1013 1014 case 0: 1015 if (parsed_two_word == i) continue; 1016 1017 default: 1018 fatal("Unexpected tag: %d", cp->tag_at(i).value()); 1019 break; 1020 } 1021 1022 } 1023 } 1024 1025 // staticfield <klass> <name> <signature> <value> 1026 // 1027 // Initialize a class and fill in the value for a static field. 1028 // This is useful when the compile was dependent on the value of 1029 // static fields but it's impossible to properly rerun the static 1030 // initializer. 1031 void process_staticfield(TRAPS) { 1032 InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK); 1033 1034 if (k == nullptr || ReplaySuppressInitializers == 0 || 1035 (ReplaySuppressInitializers == 2 && k->class_loader() == nullptr)) { 1036 skip_remaining(); 1037 return; 1038 } 1039 1040 assert(k->is_initialized(), "must be"); 1041 1042 const char* field_name = parse_escaped_string(); 1043 const char* field_signature = parse_string(); 1044 fieldDescriptor fd; 1045 Symbol* name = SymbolTable::new_symbol(field_name); 1046 Symbol* sig = SymbolTable::new_symbol(field_signature); 1047 if (!k->find_local_field(name, sig, &fd) || 1048 !fd.is_static() || 1049 fd.has_initial_value()) { 1050 report_error(field_name); 1051 return; 1052 } 1053 1054 oop java_mirror = k->java_mirror(); 1055 if (field_signature[0] == JVM_SIGNATURE_ARRAY) { 1056 int length = parse_int("array length"); 1057 oop value = nullptr; 1058 1059 if (length != -1) { 1060 if (field_signature[1] == JVM_SIGNATURE_ARRAY) { 1061 // multi dimensional array 1062 ArrayKlass* kelem = (ArrayKlass *)parse_klass(CHECK); 1063 if (kelem == nullptr) { 1064 return; 1065 } 1066 int rank = 0; 1067 while (field_signature[rank] == JVM_SIGNATURE_ARRAY) { 1068 rank++; 1069 } 1070 jint* dims = NEW_RESOURCE_ARRAY(jint, rank); 1071 dims[0] = length; 1072 for (int i = 1; i < rank; i++) { 1073 dims[i] = 1; // These aren't relevant to the compiler 1074 } 1075 value = kelem->multi_allocate(rank, dims, CHECK); 1076 } else { 1077 if (strcmp(field_signature, "[B") == 0) { 1078 value = oopFactory::new_byteArray(length, CHECK); 1079 } else if (strcmp(field_signature, "[Z") == 0) { 1080 value = oopFactory::new_boolArray(length, CHECK); 1081 } else if (strcmp(field_signature, "[C") == 0) { 1082 value = oopFactory::new_charArray(length, CHECK); 1083 } else if (strcmp(field_signature, "[S") == 0) { 1084 value = oopFactory::new_shortArray(length, CHECK); 1085 } else if (strcmp(field_signature, "[F") == 0) { 1086 value = oopFactory::new_floatArray(length, CHECK); 1087 } else if (strcmp(field_signature, "[D") == 0) { 1088 value = oopFactory::new_doubleArray(length, CHECK); 1089 } else if (strcmp(field_signature, "[I") == 0) { 1090 value = oopFactory::new_intArray(length, CHECK); 1091 } else if (strcmp(field_signature, "[J") == 0) { 1092 value = oopFactory::new_longArray(length, CHECK); 1093 } else if (field_signature[0] == JVM_SIGNATURE_ARRAY && 1094 field_signature[1] == JVM_SIGNATURE_CLASS) { 1095 Klass* actual_array_klass = parse_klass(CHECK); 1096 Klass* kelem = ObjArrayKlass::cast(actual_array_klass)->element_klass(); 1097 value = oopFactory::new_objArray(kelem, length, CHECK); 1098 } else { 1099 report_error("unhandled array staticfield"); 1100 } 1101 } 1102 } 1103 java_mirror->obj_field_put(fd.offset(), value); 1104 } else { 1105 const char* string_value = parse_escaped_string(); 1106 if (strcmp(field_signature, "I") == 0) { 1107 int value = atoi(string_value); 1108 java_mirror->int_field_put(fd.offset(), value); 1109 } else if (strcmp(field_signature, "B") == 0) { 1110 int value = atoi(string_value); 1111 java_mirror->byte_field_put(fd.offset(), value); 1112 } else if (strcmp(field_signature, "C") == 0) { 1113 int value = atoi(string_value); 1114 java_mirror->char_field_put(fd.offset(), value); 1115 } else if (strcmp(field_signature, "S") == 0) { 1116 int value = atoi(string_value); 1117 java_mirror->short_field_put(fd.offset(), value); 1118 } else if (strcmp(field_signature, "Z") == 0) { 1119 int value = atoi(string_value); 1120 java_mirror->bool_field_put(fd.offset(), value); 1121 } else if (strcmp(field_signature, "J") == 0) { 1122 jlong value; 1123 if (sscanf(string_value, JLONG_FORMAT, &value) != 1) { 1124 fprintf(stderr, "Error parsing long: %s\n", string_value); 1125 return; 1126 } 1127 java_mirror->long_field_put(fd.offset(), value); 1128 } else if (strcmp(field_signature, "F") == 0) { 1129 float value = atof(string_value); 1130 java_mirror->float_field_put(fd.offset(), value); 1131 } else if (strcmp(field_signature, "D") == 0) { 1132 double value = atof(string_value); 1133 java_mirror->double_field_put(fd.offset(), value); 1134 } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) { 1135 Handle value = java_lang_String::create_from_str(string_value, CHECK); 1136 java_mirror->obj_field_put(fd.offset(), value()); 1137 } else if (field_signature[0] == JVM_SIGNATURE_CLASS) { 1138 oop value = nullptr; 1139 if (string_value != nullptr) { 1140 Klass* k = resolve_klass(string_value, CHECK); 1141 value = InstanceKlass::cast(k)->allocate_instance(CHECK); 1142 } 1143 java_mirror->obj_field_put(fd.offset(), value); 1144 } else { 1145 report_error("unhandled staticfield"); 1146 } 1147 } 1148 } 1149 1150 #if INCLUDE_JVMTI 1151 // JvmtiExport <field> <value> 1152 void process_JvmtiExport(TRAPS) { 1153 const char* field = parse_string(); 1154 bool value = parse_int("JvmtiExport flag") != 0; 1155 if (strcmp(field, "can_access_local_variables") == 0) { 1156 JvmtiExport::set_can_access_local_variables(value); 1157 } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) { 1158 JvmtiExport::set_can_hotswap_or_post_breakpoint(value); 1159 } else if (strcmp(field, "can_post_on_exceptions") == 0) { 1160 JvmtiExport::set_can_post_on_exceptions(value); 1161 } else { 1162 report_error("Unrecognized JvmtiExport directive"); 1163 } 1164 } 1165 #endif // INCLUDE_JVMTI 1166 1167 // Create and initialize a record for a ciMethod 1168 ciMethodRecord* new_ciMethod(Method* method) { 1169 ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord); 1170 rec->_klass_name = method->method_holder()->name()->as_utf8(); 1171 rec->_method_name = method->name()->as_utf8(); 1172 rec->_signature = method->signature()->as_utf8(); 1173 _ci_method_records.append(rec); 1174 return rec; 1175 } 1176 1177 // Lookup data for a ciMethod 1178 ciMethodRecord* find_ciMethodRecord(Method* method) { 1179 const char* klass_name = method->method_holder()->name()->as_utf8(); 1180 const char* method_name = method->name()->as_utf8(); 1181 const char* signature = method->signature()->as_utf8(); 1182 for (int i = 0; i < _ci_method_records.length(); i++) { 1183 ciMethodRecord* rec = _ci_method_records.at(i); 1184 if (strcmp(rec->_klass_name, klass_name) == 0 && 1185 strcmp(rec->_method_name, method_name) == 0 && 1186 strcmp(rec->_signature, signature) == 0) { 1187 return rec; 1188 } 1189 } 1190 return nullptr; 1191 } 1192 1193 // Create and initialize a record for a ciInstanceKlass which was present at replay dump time. 1194 void new_ciInstanceKlass(const InstanceKlass* klass) { 1195 ciInstanceKlassRecord* rec = NEW_RESOURCE_OBJ(ciInstanceKlassRecord); 1196 rec->_klass = klass; 1197 oop java_mirror = klass->java_mirror(); 1198 Handle h_java_mirror(_thread, java_mirror); 1199 rec->_java_mirror = JNIHandles::make_global(h_java_mirror); 1200 _ci_instance_klass_records.append(rec); 1201 } 1202 1203 // Check if a ciInstanceKlass was present at replay dump time for a klass. 1204 ciInstanceKlassRecord* find_ciInstanceKlass(const InstanceKlass* klass) { 1205 for (int i = 0; i < _ci_instance_klass_records.length(); i++) { 1206 ciInstanceKlassRecord* rec = _ci_instance_klass_records.at(i); 1207 if (klass == rec->_klass) { 1208 // ciInstanceKlass for this klass was resolved. 1209 return rec; 1210 } 1211 } 1212 return nullptr; 1213 } 1214 1215 // Create and initialize a record for a ciMethodData 1216 ciMethodDataRecord* new_ciMethodData(Method* method) { 1217 ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord); 1218 rec->_klass_name = method->method_holder()->name()->as_utf8(); 1219 rec->_method_name = method->name()->as_utf8(); 1220 rec->_signature = method->signature()->as_utf8(); 1221 _ci_method_data_records.append(rec); 1222 return rec; 1223 } 1224 1225 // Lookup data for a ciMethodData 1226 ciMethodDataRecord* find_ciMethodDataRecord(Method* method) { 1227 const char* klass_name = method->method_holder()->name()->as_utf8(); 1228 const char* method_name = method->name()->as_utf8(); 1229 const char* signature = method->signature()->as_utf8(); 1230 for (int i = 0; i < _ci_method_data_records.length(); i++) { 1231 ciMethodDataRecord* rec = _ci_method_data_records.at(i); 1232 if (strcmp(rec->_klass_name, klass_name) == 0 && 1233 strcmp(rec->_method_name, method_name) == 0 && 1234 strcmp(rec->_signature, signature) == 0) { 1235 return rec; 1236 } 1237 } 1238 return nullptr; 1239 } 1240 1241 // Create and initialize a record for a ciInlineRecord 1242 ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth, int inline_late) { 1243 ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord); 1244 rec->_klass_name = method->method_holder()->name()->as_utf8(); 1245 rec->_method_name = method->name()->as_utf8(); 1246 rec->_signature = method->signature()->as_utf8(); 1247 rec->_inline_bci = bci; 1248 rec->_inline_depth = depth; 1249 rec->_inline_late = inline_late; 1250 _ci_inline_records->append(rec); 1251 return rec; 1252 } 1253 1254 // Lookup inlining data for a ciMethod 1255 ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) { 1256 if (_ci_inline_records != nullptr) { 1257 return find_ciInlineRecord(_ci_inline_records, method, bci, depth); 1258 } 1259 return nullptr; 1260 } 1261 1262 static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>* records, 1263 Method* method, int bci, int depth) { 1264 if (records != nullptr) { 1265 const char* klass_name = method->method_holder()->name()->as_utf8(); 1266 const char* method_name = method->name()->as_utf8(); 1267 const char* signature = method->signature()->as_utf8(); 1268 for (int i = 0; i < records->length(); i++) { 1269 ciInlineRecord* rec = records->at(i); 1270 if ((rec->_inline_bci == bci) && 1271 (rec->_inline_depth == depth) && 1272 (strcmp(rec->_klass_name, klass_name) == 0) && 1273 (strcmp(rec->_method_name, method_name) == 0) && 1274 (strcmp(rec->_signature, signature) == 0)) { 1275 return rec; 1276 } 1277 } 1278 } 1279 return nullptr; 1280 } 1281 1282 const char* error_message() { 1283 return _error_message; 1284 } 1285 1286 void reset() { 1287 _error_message = nullptr; 1288 _ci_method_records.clear(); 1289 _ci_method_data_records.clear(); 1290 } 1291 1292 // Take an ascii string contain \u#### escapes and convert it to utf8 1293 // in place. 1294 static void unescape_string(char* value) { 1295 char* from = value; 1296 char* to = value; 1297 while (*from != '\0') { 1298 if (*from != '\\') { 1299 *from++ = *to++; 1300 } else { 1301 switch (from[1]) { 1302 case 'u': { 1303 from += 2; 1304 jchar value=0; 1305 for (int i=0; i<4; i++) { 1306 char c = *from++; 1307 switch (c) { 1308 case '0': case '1': case '2': case '3': case '4': 1309 case '5': case '6': case '7': case '8': case '9': 1310 value = (value << 4) + c - '0'; 1311 break; 1312 case 'a': case 'b': case 'c': 1313 case 'd': case 'e': case 'f': 1314 value = (value << 4) + 10 + c - 'a'; 1315 break; 1316 case 'A': case 'B': case 'C': 1317 case 'D': case 'E': case 'F': 1318 value = (value << 4) + 10 + c - 'A'; 1319 break; 1320 default: 1321 ShouldNotReachHere(); 1322 } 1323 } 1324 UNICODE::convert_to_utf8(&value, 1, to); 1325 to++; 1326 break; 1327 } 1328 case 't': *to++ = '\t'; from += 2; break; 1329 case 'n': *to++ = '\n'; from += 2; break; 1330 case 'r': *to++ = '\r'; from += 2; break; 1331 case 'f': *to++ = '\f'; from += 2; break; 1332 default: 1333 ShouldNotReachHere(); 1334 } 1335 } 1336 } 1337 *from = *to; 1338 } 1339 }; 1340 1341 void ciReplay::replay(TRAPS) { 1342 int exit_code = replay_impl(THREAD); 1343 1344 Threads::destroy_vm(); 1345 1346 vm_exit(exit_code); 1347 } 1348 1349 bool ciReplay::no_replay_state() { 1350 return replay_state == nullptr; 1351 } 1352 1353 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) { 1354 if (FLAG_IS_DEFAULT(InlineDataFile)) { 1355 tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt)."); 1356 return nullptr; 1357 } 1358 1359 VM_ENTRY_MARK; 1360 // Load and parse the replay data 1361 CompileReplay rp(InlineDataFile, THREAD); 1362 if (!rp.can_replay()) { 1363 tty->print_cr("ciReplay: !rp.can_replay()"); 1364 return nullptr; 1365 } 1366 void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD); 1367 if (HAS_PENDING_EXCEPTION) { 1368 Handle throwable(THREAD, PENDING_EXCEPTION); 1369 CLEAR_PENDING_EXCEPTION; 1370 java_lang_Throwable::print_stack_trace(throwable, tty); 1371 tty->cr(); 1372 return nullptr; 1373 } 1374 1375 if (rp.had_error()) { 1376 tty->print_cr("ciReplay: Failed on %s", rp.error_message()); 1377 return nullptr; 1378 } 1379 return data; 1380 } 1381 1382 int ciReplay::replay_impl(TRAPS) { 1383 HandleMark hm(THREAD); 1384 ResourceMark rm(THREAD); 1385 1386 if (ReplaySuppressInitializers > 2) { 1387 // ReplaySuppressInitializers > 2 means that we want to allow 1388 // normal VM bootstrap but once we get into the replay itself 1389 // don't allow any initializers to be run. 1390 ReplaySuppressInitializers = 1; 1391 } 1392 1393 if (FLAG_IS_DEFAULT(ReplayDataFile)) { 1394 tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt)."); 1395 return 1; 1396 } 1397 1398 // Load and parse the replay data 1399 CompileReplay rp(ReplayDataFile, THREAD); 1400 int exit_code = 0; 1401 if (rp.can_replay()) { 1402 rp.process(THREAD); 1403 } else { 1404 exit_code = 1; 1405 return exit_code; 1406 } 1407 1408 if (HAS_PENDING_EXCEPTION) { 1409 Handle throwable(THREAD, PENDING_EXCEPTION); 1410 CLEAR_PENDING_EXCEPTION; 1411 java_lang_Throwable::print_stack_trace(throwable, tty); 1412 tty->cr(); 1413 exit_code = 2; 1414 } 1415 1416 if (rp.had_error()) { 1417 tty->print_cr("Failed on %s", rp.error_message()); 1418 exit_code = 1; 1419 } 1420 return exit_code; 1421 } 1422 1423 void ciReplay::initialize(ciMethodData* m) { 1424 if (no_replay_state()) { 1425 return; 1426 } 1427 1428 ASSERT_IN_VM; 1429 ResourceMark rm; 1430 1431 Method* method = m->get_MethodData()->method(); 1432 ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method); 1433 if (rec == nullptr) { 1434 // This indicates some mismatch with the original environment and 1435 // the replay environment though it's not always enough to 1436 // interfere with reproducing a bug 1437 tty->print_cr("Warning: requesting ciMethodData record for method with no data: "); 1438 method->print_name(tty); 1439 tty->cr(); 1440 } else { 1441 m->_state = rec->_state; 1442 m->_invocation_counter = rec->_invocation_counter; 1443 if (rec->_data_length != 0) { 1444 assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) || 1445 m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree"); 1446 1447 // Write the correct ciObjects back into the profile data 1448 ciEnv* env = ciEnv::current(); 1449 for (int i = 0; i < rec->_classes_length; i++) { 1450 Klass *k = rec->_classes[i]; 1451 // In case this class pointer is is tagged, preserve the tag bits 1452 intptr_t status = 0; 1453 if (k != nullptr) { 1454 status = ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]); 1455 } 1456 rec->_data[rec->_classes_offsets[i]] = status; 1457 } 1458 for (int i = 0; i < rec->_methods_length; i++) { 1459 Method *m = rec->_methods[i]; 1460 *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) = 1461 env->get_metadata(m); 1462 } 1463 // Copy the updated profile data into place as intptr_ts 1464 #ifdef _LP64 1465 Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length); 1466 #else 1467 Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length); 1468 #endif 1469 } 1470 1471 // copy in the original header 1472 Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length); 1473 } 1474 } 1475 1476 1477 bool ciReplay::should_not_inline(ciMethod* method) { 1478 if (no_replay_state()) { 1479 return false; 1480 } 1481 VM_ENTRY_MARK; 1482 // ciMethod without a record shouldn't be inlined. 1483 return replay_state->find_ciMethodRecord(method->get_Method()) == nullptr; 1484 } 1485 1486 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth, bool& should_delay) { 1487 if (data != nullptr) { 1488 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data; 1489 VM_ENTRY_MARK; 1490 // Inline record are ordered by bci and depth. 1491 ciInlineRecord* record = CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth); 1492 if (record == nullptr) { 1493 return false; 1494 } 1495 should_delay = record->_inline_late; 1496 return true; 1497 } else if (replay_state != nullptr) { 1498 VM_ENTRY_MARK; 1499 // Inline record are ordered by bci and depth. 1500 ciInlineRecord* record = replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth); 1501 if (record == nullptr) { 1502 return false; 1503 } 1504 should_delay = record->_inline_late; 1505 return true; 1506 } 1507 return false; 1508 } 1509 1510 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) { 1511 if (data != nullptr) { 1512 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data; 1513 VM_ENTRY_MARK; 1514 // Inline record are ordered by bci and depth. 1515 return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == nullptr; 1516 } else if (replay_state != nullptr) { 1517 VM_ENTRY_MARK; 1518 // Inline record are ordered by bci and depth. 1519 return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == nullptr; 1520 } 1521 return false; 1522 } 1523 1524 void ciReplay::initialize(ciMethod* m) { 1525 if (no_replay_state()) { 1526 return; 1527 } 1528 1529 ASSERT_IN_VM; 1530 ResourceMark rm; 1531 1532 Method* method = m->get_Method(); 1533 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method); 1534 if (rec == nullptr) { 1535 // This indicates some mismatch with the original environment and 1536 // the replay environment though it's not always enough to 1537 // interfere with reproducing a bug 1538 tty->print_cr("Warning: requesting ciMethod record for method with no data: "); 1539 method->print_name(tty); 1540 tty->cr(); 1541 } else { 1542 EXCEPTION_CONTEXT; 1543 // m->_instructions_size = rec->_instructions_size; 1544 m->_inline_instructions_size = -1; 1545 m->_interpreter_invocation_count = rec->_interpreter_invocation_count; 1546 m->_interpreter_throwout_count = rec->_interpreter_throwout_count; 1547 MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR); 1548 guarantee(mcs != nullptr, "method counters allocation failed"); 1549 mcs->invocation_counter()->_counter = rec->_invocation_counter; 1550 mcs->backedge_counter()->_counter = rec->_backedge_counter; 1551 } 1552 } 1553 1554 void ciReplay::initialize(ciInstanceKlass* ci_ik, InstanceKlass* ik) { 1555 assert(!no_replay_state(), "must have replay state"); 1556 1557 ASSERT_IN_VM; 1558 ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(ik); 1559 assert(rec != nullptr, "ciInstanceKlass must be whitelisted"); 1560 ci_ik->_java_mirror = CURRENT_ENV->get_instance(JNIHandles::resolve(rec->_java_mirror)); 1561 } 1562 1563 bool ciReplay::is_loaded(Method* method) { 1564 if (no_replay_state()) { 1565 return true; 1566 } 1567 1568 ASSERT_IN_VM; 1569 ResourceMark rm; 1570 1571 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method); 1572 return rec != nullptr; 1573 } 1574 1575 bool ciReplay::is_klass_unresolved(const InstanceKlass* klass) { 1576 if (no_replay_state()) { 1577 return false; 1578 } 1579 1580 // Check if klass is found on whitelist. 1581 ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(klass); 1582 return rec == nullptr; 1583 } 1584 1585 oop ciReplay::obj_field(oop obj, Symbol* name) { 1586 InstanceKlass* ik = InstanceKlass::cast(obj->klass()); 1587 1588 do { 1589 if (!ik->has_nonstatic_fields()) { 1590 ik = ik->java_super(); 1591 continue; 1592 } 1593 1594 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { 1595 if (fs.access_flags().is_static()) { 1596 continue; 1597 } 1598 if (fs.name() == name) { 1599 int offset = fs.offset(); 1600 #ifdef ASSERT 1601 fieldDescriptor fd = fs.field_descriptor(); 1602 assert(fd.offset() == ik->field_offset(fd.index()), "!"); 1603 #endif 1604 oop f = obj->obj_field(offset); 1605 return f; 1606 } 1607 } 1608 1609 ik = ik->java_super(); 1610 } while (ik != nullptr); 1611 return nullptr; 1612 } 1613 1614 oop ciReplay::obj_field(oop obj, const char *name) { 1615 Symbol* fname = SymbolTable::probe(name, (int)strlen(name)); 1616 if (fname == nullptr) { 1617 return nullptr; 1618 } 1619 return obj_field(obj, fname); 1620 }