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