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