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