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