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