1 /* 2 * Copyright (c) 1997, 2014, 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 "code/codeCache.hpp" 27 #include "code/compiledIC.hpp" 28 #include "code/dependencies.hpp" 29 #include "code/nmethod.hpp" 30 #include "code/scopeDesc.hpp" 31 #include "compiler/abstractCompiler.hpp" 32 #include "compiler/compileBroker.hpp" 33 #include "compiler/compileLog.hpp" 34 #include "compiler/compilerOracle.hpp" 35 #include "compiler/disassembler.hpp" 36 #include "interpreter/bytecode.hpp" 37 #include "oops/methodData.hpp" 38 #include "prims/jvmtiRedefineClassesTrace.hpp" 39 #include "prims/jvmtiImpl.hpp" 40 #include "runtime/atomic.inline.hpp" 41 #include "runtime/orderAccess.inline.hpp" 42 #include "runtime/sharedRuntime.hpp" 43 #include "runtime/sweeper.hpp" 44 #include "utilities/resourceHash.hpp" 45 #include "utilities/dtrace.hpp" 46 #include "utilities/events.hpp" 47 #include "utilities/xmlstream.hpp" 48 #ifdef SHARK 49 #include "shark/sharkCompiler.hpp" 50 #endif 51 52 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 53 54 unsigned char nmethod::_global_unloading_clock = 0; 55 56 #ifdef DTRACE_ENABLED 57 58 // Only bother with this argument setup if dtrace is available 59 60 #define DTRACE_METHOD_UNLOAD_PROBE(method) \ 61 { \ 62 Method* m = (method); \ 63 if (m != NULL) { \ 64 Symbol* klass_name = m->klass_name(); \ 65 Symbol* name = m->name(); \ 66 Symbol* signature = m->signature(); \ 67 HOTSPOT_COMPILED_METHOD_UNLOAD( \ 68 (char *) klass_name->bytes(), klass_name->utf8_length(), \ 69 (char *) name->bytes(), name->utf8_length(), \ 70 (char *) signature->bytes(), signature->utf8_length()); \ 71 } \ 72 } 73 74 #else // ndef DTRACE_ENABLED 75 76 #define DTRACE_METHOD_UNLOAD_PROBE(method) 77 78 #endif 79 80 bool nmethod::is_compiled_by_c1() const { 81 if (compiler() == NULL) { 82 return false; 83 } 84 return compiler()->is_c1(); 85 } 86 bool nmethod::is_compiled_by_c2() const { 87 if (compiler() == NULL) { 88 return false; 89 } 90 return compiler()->is_c2(); 91 } 92 bool nmethod::is_compiled_by_shark() const { 93 if (compiler() == NULL) { 94 return false; 95 } 96 return compiler()->is_shark(); 97 } 98 99 100 101 //--------------------------------------------------------------------------------- 102 // NMethod statistics 103 // They are printed under various flags, including: 104 // PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation. 105 // (In the latter two cases, they like other stats are printed to the log only.) 106 107 #ifndef PRODUCT 108 // These variables are put into one block to reduce relocations 109 // and make it simpler to print from the debugger. 110 static 111 struct nmethod_stats_struct { 112 int nmethod_count; 113 int total_size; 114 int relocation_size; 115 int consts_size; 116 int insts_size; 117 int stub_size; 118 int scopes_data_size; 119 int scopes_pcs_size; 120 int dependencies_size; 121 int handler_table_size; 122 int nul_chk_table_size; 123 int oops_size; 124 125 void note_nmethod(nmethod* nm) { 126 nmethod_count += 1; 127 total_size += nm->size(); 128 relocation_size += nm->relocation_size(); 129 consts_size += nm->consts_size(); 130 insts_size += nm->insts_size(); 131 stub_size += nm->stub_size(); 132 oops_size += nm->oops_size(); 133 scopes_data_size += nm->scopes_data_size(); 134 scopes_pcs_size += nm->scopes_pcs_size(); 135 dependencies_size += nm->dependencies_size(); 136 handler_table_size += nm->handler_table_size(); 137 nul_chk_table_size += nm->nul_chk_table_size(); 138 } 139 void print_nmethod_stats() { 140 if (nmethod_count == 0) return; 141 tty->print_cr("Statistics for %d bytecoded nmethods:", nmethod_count); 142 if (total_size != 0) tty->print_cr(" total in heap = %d", total_size); 143 if (relocation_size != 0) tty->print_cr(" relocation = %d", relocation_size); 144 if (consts_size != 0) tty->print_cr(" constants = %d", consts_size); 145 if (insts_size != 0) tty->print_cr(" main code = %d", insts_size); 146 if (stub_size != 0) tty->print_cr(" stub code = %d", stub_size); 147 if (oops_size != 0) tty->print_cr(" oops = %d", oops_size); 148 if (scopes_data_size != 0) tty->print_cr(" scopes data = %d", scopes_data_size); 149 if (scopes_pcs_size != 0) tty->print_cr(" scopes pcs = %d", scopes_pcs_size); 150 if (dependencies_size != 0) tty->print_cr(" dependencies = %d", dependencies_size); 151 if (handler_table_size != 0) tty->print_cr(" handler table = %d", handler_table_size); 152 if (nul_chk_table_size != 0) tty->print_cr(" nul chk table = %d", nul_chk_table_size); 153 } 154 155 int native_nmethod_count; 156 int native_total_size; 157 int native_relocation_size; 158 int native_insts_size; 159 int native_oops_size; 160 void note_native_nmethod(nmethod* nm) { 161 native_nmethod_count += 1; 162 native_total_size += nm->size(); 163 native_relocation_size += nm->relocation_size(); 164 native_insts_size += nm->insts_size(); 165 native_oops_size += nm->oops_size(); 166 } 167 void print_native_nmethod_stats() { 168 if (native_nmethod_count == 0) return; 169 tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count); 170 if (native_total_size != 0) tty->print_cr(" N. total size = %d", native_total_size); 171 if (native_relocation_size != 0) tty->print_cr(" N. relocation = %d", native_relocation_size); 172 if (native_insts_size != 0) tty->print_cr(" N. main code = %d", native_insts_size); 173 if (native_oops_size != 0) tty->print_cr(" N. oops = %d", native_oops_size); 174 } 175 176 int pc_desc_resets; // number of resets (= number of caches) 177 int pc_desc_queries; // queries to nmethod::find_pc_desc 178 int pc_desc_approx; // number of those which have approximate true 179 int pc_desc_repeats; // number of _pc_descs[0] hits 180 int pc_desc_hits; // number of LRU cache hits 181 int pc_desc_tests; // total number of PcDesc examinations 182 int pc_desc_searches; // total number of quasi-binary search steps 183 int pc_desc_adds; // number of LUR cache insertions 184 185 void print_pc_stats() { 186 tty->print_cr("PcDesc Statistics: %d queries, %.2f comparisons per query", 187 pc_desc_queries, 188 (double)(pc_desc_tests + pc_desc_searches) 189 / pc_desc_queries); 190 tty->print_cr(" caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d", 191 pc_desc_resets, 192 pc_desc_queries, pc_desc_approx, 193 pc_desc_repeats, pc_desc_hits, 194 pc_desc_tests, pc_desc_searches, pc_desc_adds); 195 } 196 } nmethod_stats; 197 #endif //PRODUCT 198 199 200 //--------------------------------------------------------------------------------- 201 202 203 ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) { 204 assert(pc != NULL, "Must be non null"); 205 assert(exception.not_null(), "Must be non null"); 206 assert(handler != NULL, "Must be non null"); 207 208 _count = 0; 209 _exception_type = exception->klass(); 210 _next = NULL; 211 212 add_address_and_handler(pc,handler); 213 } 214 215 216 address ExceptionCache::match(Handle exception, address pc) { 217 assert(pc != NULL,"Must be non null"); 218 assert(exception.not_null(),"Must be non null"); 219 if (exception->klass() == exception_type()) { 220 return (test_address(pc)); 221 } 222 223 return NULL; 224 } 225 226 227 bool ExceptionCache::match_exception_with_space(Handle exception) { 228 assert(exception.not_null(),"Must be non null"); 229 if (exception->klass() == exception_type() && count() < cache_size) { 230 return true; 231 } 232 return false; 233 } 234 235 236 address ExceptionCache::test_address(address addr) { 237 for (int i=0; i<count(); i++) { 238 if (pc_at(i) == addr) { 239 return handler_at(i); 240 } 241 } 242 return NULL; 243 } 244 245 246 bool ExceptionCache::add_address_and_handler(address addr, address handler) { 247 if (test_address(addr) == handler) return true; 248 if (count() < cache_size) { 249 set_pc_at(count(),addr); 250 set_handler_at(count(), handler); 251 increment_count(); 252 return true; 253 } 254 return false; 255 } 256 257 258 // private method for handling exception cache 259 // These methods are private, and used to manipulate the exception cache 260 // directly. 261 ExceptionCache* nmethod::exception_cache_entry_for_exception(Handle exception) { 262 ExceptionCache* ec = exception_cache(); 263 while (ec != NULL) { 264 if (ec->match_exception_with_space(exception)) { 265 return ec; 266 } 267 ec = ec->next(); 268 } 269 return NULL; 270 } 271 272 273 //----------------------------------------------------------------------------- 274 275 276 // Helper used by both find_pc_desc methods. 277 static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) { 278 NOT_PRODUCT(++nmethod_stats.pc_desc_tests); 279 if (!approximate) 280 return pc->pc_offset() == pc_offset; 281 else 282 return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset(); 283 } 284 285 void PcDescCache::reset_to(PcDesc* initial_pc_desc) { 286 if (initial_pc_desc == NULL) { 287 _pc_descs[0] = NULL; // native method; no PcDescs at all 288 return; 289 } 290 NOT_PRODUCT(++nmethod_stats.pc_desc_resets); 291 // reset the cache by filling it with benign (non-null) values 292 assert(initial_pc_desc->pc_offset() < 0, "must be sentinel"); 293 for (int i = 0; i < cache_size; i++) 294 _pc_descs[i] = initial_pc_desc; 295 } 296 297 PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) { 298 NOT_PRODUCT(++nmethod_stats.pc_desc_queries); 299 NOT_PRODUCT(if (approximate) ++nmethod_stats.pc_desc_approx); 300 301 // Note: one might think that caching the most recently 302 // read value separately would be a win, but one would be 303 // wrong. When many threads are updating it, the cache 304 // line it's in would bounce between caches, negating 305 // any benefit. 306 307 // In order to prevent race conditions do not load cache elements 308 // repeatedly, but use a local copy: 309 PcDesc* res; 310 311 // Step one: Check the most recently added value. 312 res = _pc_descs[0]; 313 if (res == NULL) return NULL; // native method; no PcDescs at all 314 if (match_desc(res, pc_offset, approximate)) { 315 NOT_PRODUCT(++nmethod_stats.pc_desc_repeats); 316 return res; 317 } 318 319 // Step two: Check the rest of the LRU cache. 320 for (int i = 1; i < cache_size; ++i) { 321 res = _pc_descs[i]; 322 if (res->pc_offset() < 0) break; // optimization: skip empty cache 323 if (match_desc(res, pc_offset, approximate)) { 324 NOT_PRODUCT(++nmethod_stats.pc_desc_hits); 325 return res; 326 } 327 } 328 329 // Report failure. 330 return NULL; 331 } 332 333 void PcDescCache::add_pc_desc(PcDesc* pc_desc) { 334 NOT_PRODUCT(++nmethod_stats.pc_desc_adds); 335 // Update the LRU cache by shifting pc_desc forward. 336 for (int i = 0; i < cache_size; i++) { 337 PcDesc* next = _pc_descs[i]; 338 _pc_descs[i] = pc_desc; 339 pc_desc = next; 340 } 341 } 342 343 // adjust pcs_size so that it is a multiple of both oopSize and 344 // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple 345 // of oopSize, then 2*sizeof(PcDesc) is) 346 static int adjust_pcs_size(int pcs_size) { 347 int nsize = round_to(pcs_size, oopSize); 348 if ((nsize % sizeof(PcDesc)) != 0) { 349 nsize = pcs_size + sizeof(PcDesc); 350 } 351 assert((nsize % oopSize) == 0, "correct alignment"); 352 return nsize; 353 } 354 355 //----------------------------------------------------------------------------- 356 357 358 void nmethod::add_exception_cache_entry(ExceptionCache* new_entry) { 359 assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock"); 360 assert(new_entry != NULL,"Must be non null"); 361 assert(new_entry->next() == NULL, "Must be null"); 362 363 if (exception_cache() != NULL) { 364 new_entry->set_next(exception_cache()); 365 } 366 set_exception_cache(new_entry); 367 } 368 369 void nmethod::clean_exception_cache(BoolObjectClosure* is_alive) { 370 ExceptionCache* prev = NULL; 371 ExceptionCache* curr = exception_cache(); 372 373 while (curr != NULL) { 374 ExceptionCache* next = curr->next(); 375 376 Klass* ex_klass = curr->exception_type(); 377 if (ex_klass != NULL && !ex_klass->is_loader_alive(is_alive)) { 378 if (prev == NULL) { 379 set_exception_cache(next); 380 } else { 381 prev->set_next(next); 382 } 383 delete curr; 384 // prev stays the same. 385 } else { 386 prev = curr; 387 } 388 389 curr = next; 390 } 391 } 392 393 // public method for accessing the exception cache 394 // These are the public access methods. 395 address nmethod::handler_for_exception_and_pc(Handle exception, address pc) { 396 // We never grab a lock to read the exception cache, so we may 397 // have false negatives. This is okay, as it can only happen during 398 // the first few exception lookups for a given nmethod. 399 ExceptionCache* ec = exception_cache(); 400 while (ec != NULL) { 401 address ret_val; 402 if ((ret_val = ec->match(exception,pc)) != NULL) { 403 return ret_val; 404 } 405 ec = ec->next(); 406 } 407 return NULL; 408 } 409 410 411 void nmethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) { 412 // There are potential race conditions during exception cache updates, so we 413 // must own the ExceptionCache_lock before doing ANY modifications. Because 414 // we don't lock during reads, it is possible to have several threads attempt 415 // to update the cache with the same data. We need to check for already inserted 416 // copies of the current data before adding it. 417 418 MutexLocker ml(ExceptionCache_lock); 419 ExceptionCache* target_entry = exception_cache_entry_for_exception(exception); 420 421 if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) { 422 target_entry = new ExceptionCache(exception,pc,handler); 423 add_exception_cache_entry(target_entry); 424 } 425 } 426 427 428 //-------------end of code for ExceptionCache-------------- 429 430 431 int nmethod::total_size() const { 432 return 433 consts_size() + 434 insts_size() + 435 stub_size() + 436 scopes_data_size() + 437 scopes_pcs_size() + 438 handler_table_size() + 439 nul_chk_table_size(); 440 } 441 442 const char* nmethod::compile_kind() const { 443 if (is_osr_method()) return "osr"; 444 if (method() != NULL && is_native_method()) return "c2n"; 445 return NULL; 446 } 447 448 // Fill in default values for various flag fields 449 void nmethod::init_defaults() { 450 _state = in_use; 451 _unloading_clock = 0; 452 _marked_for_reclamation = 0; 453 _has_flushed_dependencies = 0; 454 _has_unsafe_access = 0; 455 _has_method_handle_invokes = 0; 456 _lazy_critical_native = 0; 457 _has_wide_vectors = 0; 458 _marked_for_deoptimization = 0; 459 _lock_count = 0; 460 _stack_traversal_mark = 0; 461 _unload_reported = false; // jvmti state 462 463 #ifdef ASSERT 464 _oops_are_stale = false; 465 #endif 466 467 _oops_do_mark_link = NULL; 468 _jmethod_id = NULL; 469 _osr_link = NULL; 470 if (UseG1GC) { 471 _unloading_next = NULL; 472 } else { 473 _scavenge_root_link = NULL; 474 } 475 _scavenge_root_state = 0; 476 _compiler = NULL; 477 #if INCLUDE_RTM_OPT 478 _rtm_state = NoRTM; 479 #endif 480 #ifdef HAVE_DTRACE_H 481 _trap_offset = 0; 482 #endif // def HAVE_DTRACE_H 483 } 484 485 nmethod* nmethod::new_native_nmethod(methodHandle method, 486 int compile_id, 487 CodeBuffer *code_buffer, 488 int vep_offset, 489 int frame_complete, 490 int frame_size, 491 ByteSize basic_lock_owner_sp_offset, 492 ByteSize basic_lock_sp_offset, 493 OopMapSet* oop_maps) { 494 code_buffer->finalize_oop_references(method); 495 // create nmethod 496 nmethod* nm = NULL; 497 { 498 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 499 int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); 500 CodeOffsets offsets; 501 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); 502 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); 503 nm = new (native_nmethod_size, CompLevel_none) nmethod(method(), native_nmethod_size, 504 compile_id, &offsets, 505 code_buffer, frame_size, 506 basic_lock_owner_sp_offset, 507 basic_lock_sp_offset, oop_maps); 508 NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_native_nmethod(nm)); 509 if (PrintAssembly && nm != NULL) { 510 Disassembler::decode(nm); 511 } 512 } 513 // verify nmethod 514 debug_only(if (nm) nm->verify();) // might block 515 516 if (nm != NULL) { 517 nm->log_new_nmethod(); 518 } 519 520 return nm; 521 } 522 523 #ifdef HAVE_DTRACE_H 524 nmethod* nmethod::new_dtrace_nmethod(methodHandle method, 525 CodeBuffer *code_buffer, 526 int vep_offset, 527 int trap_offset, 528 int frame_complete, 529 int frame_size) { 530 code_buffer->finalize_oop_references(method); 531 // create nmethod 532 nmethod* nm = NULL; 533 { 534 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 535 int nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); 536 CodeOffsets offsets; 537 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); 538 offsets.set_value(CodeOffsets::Dtrace_trap, trap_offset); 539 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); 540 541 nm = new (nmethod_size, CompLevel_none) nmethod(method(), nmethod_size, 542 &offsets, code_buffer, frame_size); 543 544 NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_nmethod(nm)); 545 if (PrintAssembly && nm != NULL) { 546 Disassembler::decode(nm); 547 } 548 } 549 // verify nmethod 550 debug_only(if (nm) nm->verify();) // might block 551 552 if (nm != NULL) { 553 nm->log_new_nmethod(); 554 } 555 556 return nm; 557 } 558 559 #endif // def HAVE_DTRACE_H 560 561 nmethod* nmethod::new_nmethod(methodHandle method, 562 int compile_id, 563 int entry_bci, 564 CodeOffsets* offsets, 565 int orig_pc_offset, 566 DebugInformationRecorder* debug_info, 567 Dependencies* dependencies, 568 CodeBuffer* code_buffer, int frame_size, 569 OopMapSet* oop_maps, 570 ExceptionHandlerTable* handler_table, 571 ImplicitExceptionTable* nul_chk_table, 572 AbstractCompiler* compiler, 573 int comp_level 574 ) 575 { 576 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); 577 code_buffer->finalize_oop_references(method); 578 // create nmethod 579 nmethod* nm = NULL; 580 { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 581 int nmethod_size = 582 allocation_size(code_buffer, sizeof(nmethod)) 583 + adjust_pcs_size(debug_info->pcs_size()) 584 + round_to(dependencies->size_in_bytes() , oopSize) 585 + round_to(handler_table->size_in_bytes(), oopSize) 586 + round_to(nul_chk_table->size_in_bytes(), oopSize) 587 + round_to(debug_info->data_size() , oopSize); 588 589 nm = new (nmethod_size, comp_level) 590 nmethod(method(), nmethod_size, compile_id, entry_bci, offsets, 591 orig_pc_offset, debug_info, dependencies, code_buffer, frame_size, 592 oop_maps, 593 handler_table, 594 nul_chk_table, 595 compiler, 596 comp_level); 597 598 if (nm != NULL) { 599 // To make dependency checking during class loading fast, record 600 // the nmethod dependencies in the classes it is dependent on. 601 // This allows the dependency checking code to simply walk the 602 // class hierarchy above the loaded class, checking only nmethods 603 // which are dependent on those classes. The slow way is to 604 // check every nmethod for dependencies which makes it linear in 605 // the number of methods compiled. For applications with a lot 606 // classes the slow way is too slow. 607 for (Dependencies::DepStream deps(nm); deps.next(); ) { 608 Klass* klass = deps.context_type(); 609 if (klass == NULL) { 610 continue; // ignore things like evol_method 611 } 612 613 // record this nmethod as dependent on this klass 614 InstanceKlass::cast(klass)->add_dependent_nmethod(nm); 615 } 616 NOT_PRODUCT(nmethod_stats.note_nmethod(nm)); 617 if (PrintAssembly || CompilerOracle::has_option_string(method, "PrintAssembly")) { 618 Disassembler::decode(nm); 619 } 620 } 621 } 622 // Do verification and logging outside CodeCache_lock. 623 if (nm != NULL) { 624 // Safepoints in nmethod::verify aren't allowed because nm hasn't been installed yet. 625 DEBUG_ONLY(nm->verify();) 626 nm->log_new_nmethod(); 627 } 628 return nm; 629 } 630 631 632 // For native wrappers 633 nmethod::nmethod( 634 Method* method, 635 int nmethod_size, 636 int compile_id, 637 CodeOffsets* offsets, 638 CodeBuffer* code_buffer, 639 int frame_size, 640 ByteSize basic_lock_owner_sp_offset, 641 ByteSize basic_lock_sp_offset, 642 OopMapSet* oop_maps ) 643 : CodeBlob("native nmethod", code_buffer, sizeof(nmethod), 644 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), 645 _native_receiver_sp_offset(basic_lock_owner_sp_offset), 646 _native_basic_lock_sp_offset(basic_lock_sp_offset) 647 { 648 { 649 debug_only(No_Safepoint_Verifier nsv;) 650 assert_locked_or_safepoint(CodeCache_lock); 651 652 init_defaults(); 653 _method = method; 654 _entry_bci = InvocationEntryBci; 655 // We have no exception handler or deopt handler make the 656 // values something that will never match a pc like the nmethod vtable entry 657 _exception_offset = 0; 658 _deoptimize_offset = 0; 659 _deoptimize_mh_offset = 0; 660 _orig_pc_offset = 0; 661 662 _consts_offset = data_offset(); 663 _stub_offset = data_offset(); 664 _oops_offset = data_offset(); 665 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); 666 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); 667 _scopes_pcs_offset = _scopes_data_offset; 668 _dependencies_offset = _scopes_pcs_offset; 669 _handler_table_offset = _dependencies_offset; 670 _nul_chk_table_offset = _handler_table_offset; 671 _nmethod_end_offset = _nul_chk_table_offset; 672 _compile_id = compile_id; 673 _comp_level = CompLevel_none; 674 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); 675 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); 676 _osr_entry_point = NULL; 677 _exception_cache = NULL; 678 _pc_desc_cache.reset_to(NULL); 679 _hotness_counter = NMethodSweeper::hotness_counter_reset_val(); 680 681 code_buffer->copy_values_to(this); 682 if (ScavengeRootsInCode) { 683 if (detect_scavenge_root_oops()) { 684 CodeCache::add_scavenge_root_nmethod(this); 685 } 686 Universe::heap()->register_nmethod(this); 687 } 688 debug_only(verify_scavenge_root_oops()); 689 CodeCache::commit(this); 690 } 691 692 if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { 693 ttyLocker ttyl; // keep the following output all in one block 694 // This output goes directly to the tty, not the compiler log. 695 // To enable tools to match it up with the compilation activity, 696 // be sure to tag this tty output with the compile ID. 697 if (xtty != NULL) { 698 xtty->begin_head("print_native_nmethod"); 699 xtty->method(_method); 700 xtty->stamp(); 701 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); 702 } 703 // print the header part first 704 print(); 705 // then print the requested information 706 if (PrintNativeNMethods) { 707 print_code(); 708 if (oop_maps != NULL) { 709 oop_maps->print(); 710 } 711 } 712 if (PrintRelocations) { 713 print_relocations(); 714 } 715 if (xtty != NULL) { 716 xtty->tail("print_native_nmethod"); 717 } 718 } 719 } 720 721 // For dtrace wrappers 722 #ifdef HAVE_DTRACE_H 723 nmethod::nmethod( 724 Method* method, 725 int nmethod_size, 726 CodeOffsets* offsets, 727 CodeBuffer* code_buffer, 728 int frame_size) 729 : CodeBlob("dtrace nmethod", code_buffer, sizeof(nmethod), 730 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, NULL), 731 _native_receiver_sp_offset(in_ByteSize(-1)), 732 _native_basic_lock_sp_offset(in_ByteSize(-1)) 733 { 734 { 735 debug_only(No_Safepoint_Verifier nsv;) 736 assert_locked_or_safepoint(CodeCache_lock); 737 738 init_defaults(); 739 _method = method; 740 _entry_bci = InvocationEntryBci; 741 // We have no exception handler or deopt handler make the 742 // values something that will never match a pc like the nmethod vtable entry 743 _exception_offset = 0; 744 _deoptimize_offset = 0; 745 _deoptimize_mh_offset = 0; 746 _unwind_handler_offset = -1; 747 _trap_offset = offsets->value(CodeOffsets::Dtrace_trap); 748 _orig_pc_offset = 0; 749 _consts_offset = data_offset(); 750 _stub_offset = data_offset(); 751 _oops_offset = data_offset(); 752 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); 753 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); 754 _scopes_pcs_offset = _scopes_data_offset; 755 _dependencies_offset = _scopes_pcs_offset; 756 _handler_table_offset = _dependencies_offset; 757 _nul_chk_table_offset = _handler_table_offset; 758 _nmethod_end_offset = _nul_chk_table_offset; 759 _compile_id = 0; // default 760 _comp_level = CompLevel_none; 761 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); 762 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); 763 _osr_entry_point = NULL; 764 _exception_cache = NULL; 765 _pc_desc_cache.reset_to(NULL); 766 _hotness_counter = NMethodSweeper::hotness_counter_reset_val(); 767 768 code_buffer->copy_values_to(this); 769 if (ScavengeRootsInCode) { 770 if (detect_scavenge_root_oops()) { 771 CodeCache::add_scavenge_root_nmethod(this); 772 } 773 Universe::heap()->register_nmethod(this); 774 } 775 DEBUG_ONLY(verify_scavenge_root_oops();) 776 CodeCache::commit(this); 777 } 778 779 if (PrintNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { 780 ttyLocker ttyl; // keep the following output all in one block 781 // This output goes directly to the tty, not the compiler log. 782 // To enable tools to match it up with the compilation activity, 783 // be sure to tag this tty output with the compile ID. 784 if (xtty != NULL) { 785 xtty->begin_head("print_dtrace_nmethod"); 786 xtty->method(_method); 787 xtty->stamp(); 788 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); 789 } 790 // print the header part first 791 print(); 792 // then print the requested information 793 if (PrintNMethods) { 794 print_code(); 795 } 796 if (PrintRelocations) { 797 print_relocations(); 798 } 799 if (xtty != NULL) { 800 xtty->tail("print_dtrace_nmethod"); 801 } 802 } 803 } 804 #endif // def HAVE_DTRACE_H 805 806 void* nmethod::operator new(size_t size, int nmethod_size, int comp_level) throw () { 807 // With a SegmentedCodeCache, nmethods are allocated on separate heaps and therefore do not share memory 808 // with critical CodeBlobs. We define the allocation as critical to make sure all code heap memory is used. 809 bool is_critical = SegmentedCodeCache; 810 return CodeCache::allocate(nmethod_size, CodeCache::get_code_blob_type(comp_level), is_critical); 811 } 812 813 nmethod::nmethod( 814 Method* method, 815 int nmethod_size, 816 int compile_id, 817 int entry_bci, 818 CodeOffsets* offsets, 819 int orig_pc_offset, 820 DebugInformationRecorder* debug_info, 821 Dependencies* dependencies, 822 CodeBuffer *code_buffer, 823 int frame_size, 824 OopMapSet* oop_maps, 825 ExceptionHandlerTable* handler_table, 826 ImplicitExceptionTable* nul_chk_table, 827 AbstractCompiler* compiler, 828 int comp_level 829 ) 830 : CodeBlob("nmethod", code_buffer, sizeof(nmethod), 831 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), 832 _native_receiver_sp_offset(in_ByteSize(-1)), 833 _native_basic_lock_sp_offset(in_ByteSize(-1)) 834 { 835 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); 836 { 837 debug_only(No_Safepoint_Verifier nsv;) 838 assert_locked_or_safepoint(CodeCache_lock); 839 840 init_defaults(); 841 _method = method; 842 _entry_bci = entry_bci; 843 _compile_id = compile_id; 844 _comp_level = comp_level; 845 _compiler = compiler; 846 _orig_pc_offset = orig_pc_offset; 847 _hotness_counter = NMethodSweeper::hotness_counter_reset_val(); 848 849 // Section offsets 850 _consts_offset = content_offset() + code_buffer->total_offset_of(code_buffer->consts()); 851 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs()); 852 853 // Exception handler and deopt handler are in the stub section 854 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must be set"); 855 assert(offsets->value(CodeOffsets::Deopt ) != -1, "must be set"); 856 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions); 857 _deoptimize_offset = _stub_offset + offsets->value(CodeOffsets::Deopt); 858 if (offsets->value(CodeOffsets::DeoptMH) != -1) { 859 _deoptimize_mh_offset = _stub_offset + offsets->value(CodeOffsets::DeoptMH); 860 } else { 861 _deoptimize_mh_offset = -1; 862 } 863 if (offsets->value(CodeOffsets::UnwindHandler) != -1) { 864 _unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler); 865 } else { 866 _unwind_handler_offset = -1; 867 } 868 869 _oops_offset = data_offset(); 870 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); 871 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); 872 873 _scopes_pcs_offset = _scopes_data_offset + round_to(debug_info->data_size (), oopSize); 874 _dependencies_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size()); 875 _handler_table_offset = _dependencies_offset + round_to(dependencies->size_in_bytes (), oopSize); 876 _nul_chk_table_offset = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize); 877 _nmethod_end_offset = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize); 878 879 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); 880 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); 881 _osr_entry_point = code_begin() + offsets->value(CodeOffsets::OSR_Entry); 882 _exception_cache = NULL; 883 _pc_desc_cache.reset_to(scopes_pcs_begin()); 884 885 // Copy contents of ScopeDescRecorder to nmethod 886 code_buffer->copy_values_to(this); 887 debug_info->copy_to(this); 888 dependencies->copy_to(this); 889 if (ScavengeRootsInCode) { 890 if (detect_scavenge_root_oops()) { 891 CodeCache::add_scavenge_root_nmethod(this); 892 } 893 Universe::heap()->register_nmethod(this); 894 } 895 debug_only(verify_scavenge_root_oops()); 896 897 CodeCache::commit(this); 898 899 // Copy contents of ExceptionHandlerTable to nmethod 900 handler_table->copy_to(this); 901 nul_chk_table->copy_to(this); 902 903 // we use the information of entry points to find out if a method is 904 // static or non static 905 assert(compiler->is_c2() || 906 _method->is_static() == (entry_point() == _verified_entry_point), 907 " entry points must be same for static methods and vice versa"); 908 } 909 910 bool printnmethods = PrintNMethods 911 || CompilerOracle::should_print(_method) 912 || CompilerOracle::has_option_string(_method, "PrintNMethods"); 913 if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) { 914 print_nmethod(printnmethods); 915 } 916 } 917 918 919 // Print a short set of xml attributes to identify this nmethod. The 920 // output should be embedded in some other element. 921 void nmethod::log_identity(xmlStream* log) const { 922 log->print(" compile_id='%d'", compile_id()); 923 const char* nm_kind = compile_kind(); 924 if (nm_kind != NULL) log->print(" compile_kind='%s'", nm_kind); 925 if (compiler() != NULL) { 926 log->print(" compiler='%s'", compiler()->name()); 927 } 928 if (TieredCompilation) { 929 log->print(" level='%d'", comp_level()); 930 } 931 } 932 933 934 #define LOG_OFFSET(log, name) \ 935 if ((intptr_t)name##_end() - (intptr_t)name##_begin()) \ 936 log->print(" " XSTR(name) "_offset='%d'" , \ 937 (intptr_t)name##_begin() - (intptr_t)this) 938 939 940 void nmethod::log_new_nmethod() const { 941 if (LogCompilation && xtty != NULL) { 942 ttyLocker ttyl; 943 HandleMark hm; 944 xtty->begin_elem("nmethod"); 945 log_identity(xtty); 946 xtty->print(" entry='" INTPTR_FORMAT "' size='%d'", code_begin(), size()); 947 xtty->print(" address='" INTPTR_FORMAT "'", (intptr_t) this); 948 949 LOG_OFFSET(xtty, relocation); 950 LOG_OFFSET(xtty, consts); 951 LOG_OFFSET(xtty, insts); 952 LOG_OFFSET(xtty, stub); 953 LOG_OFFSET(xtty, scopes_data); 954 LOG_OFFSET(xtty, scopes_pcs); 955 LOG_OFFSET(xtty, dependencies); 956 LOG_OFFSET(xtty, handler_table); 957 LOG_OFFSET(xtty, nul_chk_table); 958 LOG_OFFSET(xtty, oops); 959 960 xtty->method(method()); 961 xtty->stamp(); 962 xtty->end_elem(); 963 } 964 } 965 966 #undef LOG_OFFSET 967 968 969 // Print out more verbose output usually for a newly created nmethod. 970 void nmethod::print_on(outputStream* st, const char* msg) const { 971 if (st != NULL) { 972 ttyLocker ttyl; 973 if (WizardMode) { 974 CompileTask::print_compilation(st, this, msg, /*short_form:*/ true); 975 st->print_cr(" (" INTPTR_FORMAT ")", this); 976 } else { 977 CompileTask::print_compilation(st, this, msg, /*short_form:*/ false); 978 } 979 } 980 } 981 982 983 void nmethod::print_nmethod(bool printmethod) { 984 ttyLocker ttyl; // keep the following output all in one block 985 if (xtty != NULL) { 986 xtty->begin_head("print_nmethod"); 987 xtty->stamp(); 988 xtty->end_head(); 989 } 990 // print the header part first 991 print(); 992 // then print the requested information 993 if (printmethod) { 994 print_code(); 995 print_pcs(); 996 if (oop_maps()) { 997 oop_maps()->print(); 998 } 999 } 1000 if (PrintDebugInfo) { 1001 print_scopes(); 1002 } 1003 if (PrintRelocations) { 1004 print_relocations(); 1005 } 1006 if (PrintDependencies) { 1007 print_dependencies(); 1008 } 1009 if (PrintExceptionHandlers) { 1010 print_handler_table(); 1011 print_nul_chk_table(); 1012 } 1013 if (xtty != NULL) { 1014 xtty->tail("print_nmethod"); 1015 } 1016 } 1017 1018 1019 // Promote one word from an assembly-time handle to a live embedded oop. 1020 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) { 1021 if (handle == NULL || 1022 // As a special case, IC oops are initialized to 1 or -1. 1023 handle == (jobject) Universe::non_oop_word()) { 1024 (*dest) = (oop) handle; 1025 } else { 1026 (*dest) = JNIHandles::resolve_non_null(handle); 1027 } 1028 } 1029 1030 1031 // Have to have the same name because it's called by a template 1032 void nmethod::copy_values(GrowableArray<jobject>* array) { 1033 int length = array->length(); 1034 assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough"); 1035 oop* dest = oops_begin(); 1036 for (int index = 0 ; index < length; index++) { 1037 initialize_immediate_oop(&dest[index], array->at(index)); 1038 } 1039 1040 // Now we can fix up all the oops in the code. We need to do this 1041 // in the code because the assembler uses jobjects as placeholders. 1042 // The code and relocations have already been initialized by the 1043 // CodeBlob constructor, so it is valid even at this early point to 1044 // iterate over relocations and patch the code. 1045 fix_oop_relocations(NULL, NULL, /*initialize_immediates=*/ true); 1046 } 1047 1048 void nmethod::copy_values(GrowableArray<Metadata*>* array) { 1049 int length = array->length(); 1050 assert((address)(metadata_begin() + length) <= (address)metadata_end(), "big enough"); 1051 Metadata** dest = metadata_begin(); 1052 for (int index = 0 ; index < length; index++) { 1053 dest[index] = array->at(index); 1054 } 1055 } 1056 1057 bool nmethod::is_at_poll_return(address pc) { 1058 RelocIterator iter(this, pc, pc+1); 1059 while (iter.next()) { 1060 if (iter.type() == relocInfo::poll_return_type) 1061 return true; 1062 } 1063 return false; 1064 } 1065 1066 1067 bool nmethod::is_at_poll_or_poll_return(address pc) { 1068 RelocIterator iter(this, pc, pc+1); 1069 while (iter.next()) { 1070 relocInfo::relocType t = iter.type(); 1071 if (t == relocInfo::poll_return_type || t == relocInfo::poll_type) 1072 return true; 1073 } 1074 return false; 1075 } 1076 1077 1078 void nmethod::fix_oop_relocations(address begin, address end, bool initialize_immediates) { 1079 // re-patch all oop-bearing instructions, just in case some oops moved 1080 RelocIterator iter(this, begin, end); 1081 while (iter.next()) { 1082 if (iter.type() == relocInfo::oop_type) { 1083 oop_Relocation* reloc = iter.oop_reloc(); 1084 if (initialize_immediates && reloc->oop_is_immediate()) { 1085 oop* dest = reloc->oop_addr(); 1086 initialize_immediate_oop(dest, (jobject) *dest); 1087 } 1088 // Refresh the oop-related bits of this instruction. 1089 reloc->fix_oop_relocation(); 1090 } else if (iter.type() == relocInfo::metadata_type) { 1091 metadata_Relocation* reloc = iter.metadata_reloc(); 1092 reloc->fix_metadata_relocation(); 1093 } 1094 } 1095 } 1096 1097 1098 void nmethod::verify_oop_relocations() { 1099 // Ensure sure that the code matches the current oop values 1100 RelocIterator iter(this, NULL, NULL); 1101 while (iter.next()) { 1102 if (iter.type() == relocInfo::oop_type) { 1103 oop_Relocation* reloc = iter.oop_reloc(); 1104 if (!reloc->oop_is_immediate()) { 1105 reloc->verify_oop_relocation(); 1106 } 1107 } 1108 } 1109 } 1110 1111 1112 ScopeDesc* nmethod::scope_desc_at(address pc) { 1113 PcDesc* pd = pc_desc_at(pc); 1114 guarantee(pd != NULL, "scope must be present"); 1115 return new ScopeDesc(this, pd->scope_decode_offset(), 1116 pd->obj_decode_offset(), pd->should_reexecute(), 1117 pd->return_oop()); 1118 } 1119 1120 1121 void nmethod::clear_inline_caches() { 1122 assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint"); 1123 if (is_zombie()) { 1124 return; 1125 } 1126 1127 RelocIterator iter(this); 1128 while (iter.next()) { 1129 iter.reloc()->clear_inline_cache(); 1130 } 1131 } 1132 1133 // Clear ICStubs of all compiled ICs 1134 void nmethod::clear_ic_stubs() { 1135 assert_locked_or_safepoint(CompiledIC_lock); 1136 RelocIterator iter(this); 1137 while(iter.next()) { 1138 if (iter.type() == relocInfo::virtual_call_type) { 1139 CompiledIC* ic = CompiledIC_at(&iter); 1140 ic->clear_ic_stub(); 1141 } 1142 } 1143 } 1144 1145 1146 void nmethod::cleanup_inline_caches() { 1147 1148 assert_locked_or_safepoint(CompiledIC_lock); 1149 1150 // If the method is not entrant or zombie then a JMP is plastered over the 1151 // first few bytes. If an oop in the old code was there, that oop 1152 // should not get GC'd. Skip the first few bytes of oops on 1153 // not-entrant methods. 1154 address low_boundary = verified_entry_point(); 1155 if (!is_in_use()) { 1156 low_boundary += NativeJump::instruction_size; 1157 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1158 // This means that the low_boundary is going to be a little too high. 1159 // This shouldn't matter, since oops of non-entrant methods are never used. 1160 // In fact, why are we bothering to look at oops in a non-entrant method?? 1161 } 1162 1163 // Find all calls in an nmethod, and clear the ones that points to zombie methods 1164 ResourceMark rm; 1165 RelocIterator iter(this, low_boundary); 1166 while(iter.next()) { 1167 switch(iter.type()) { 1168 case relocInfo::virtual_call_type: 1169 case relocInfo::opt_virtual_call_type: { 1170 CompiledIC *ic = CompiledIC_at(&iter); 1171 // Ok, to lookup references to zombies here 1172 CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination()); 1173 if( cb != NULL && cb->is_nmethod() ) { 1174 nmethod* nm = (nmethod*)cb; 1175 // Clean inline caches pointing to both zombie and not_entrant methods 1176 if (!nm->is_in_use() || (nm->method()->code() != nm)) ic->set_to_clean(); 1177 } 1178 break; 1179 } 1180 case relocInfo::static_call_type: { 1181 CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc()); 1182 CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination()); 1183 if( cb != NULL && cb->is_nmethod() ) { 1184 nmethod* nm = (nmethod*)cb; 1185 // Clean inline caches pointing to both zombie and not_entrant methods 1186 if (!nm->is_in_use() || (nm->method()->code() != nm)) csc->set_to_clean(); 1187 } 1188 break; 1189 } 1190 } 1191 } 1192 } 1193 1194 void nmethod::verify_clean_inline_caches() { 1195 assert_locked_or_safepoint(CompiledIC_lock); 1196 1197 // If the method is not entrant or zombie then a JMP is plastered over the 1198 // first few bytes. If an oop in the old code was there, that oop 1199 // should not get GC'd. Skip the first few bytes of oops on 1200 // not-entrant methods. 1201 address low_boundary = verified_entry_point(); 1202 if (!is_in_use()) { 1203 low_boundary += NativeJump::instruction_size; 1204 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1205 // This means that the low_boundary is going to be a little too high. 1206 // This shouldn't matter, since oops of non-entrant methods are never used. 1207 // In fact, why are we bothering to look at oops in a non-entrant method?? 1208 } 1209 1210 ResourceMark rm; 1211 RelocIterator iter(this, low_boundary); 1212 while(iter.next()) { 1213 switch(iter.type()) { 1214 case relocInfo::virtual_call_type: 1215 case relocInfo::opt_virtual_call_type: { 1216 CompiledIC *ic = CompiledIC_at(&iter); 1217 // Ok, to lookup references to zombies here 1218 CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination()); 1219 if( cb != NULL && cb->is_nmethod() ) { 1220 nmethod* nm = (nmethod*)cb; 1221 // Verify that inline caches pointing to both zombie and not_entrant methods are clean 1222 if (!nm->is_in_use() || (nm->method()->code() != nm)) { 1223 assert(ic->is_clean(), "IC should be clean"); 1224 } 1225 } 1226 break; 1227 } 1228 case relocInfo::static_call_type: { 1229 CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc()); 1230 CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination()); 1231 if( cb != NULL && cb->is_nmethod() ) { 1232 nmethod* nm = (nmethod*)cb; 1233 // Verify that inline caches pointing to both zombie and not_entrant methods are clean 1234 if (!nm->is_in_use() || (nm->method()->code() != nm)) { 1235 assert(csc->is_clean(), "IC should be clean"); 1236 } 1237 } 1238 break; 1239 } 1240 } 1241 } 1242 } 1243 1244 int nmethod::verify_icholder_relocations() { 1245 int count = 0; 1246 1247 RelocIterator iter(this); 1248 while(iter.next()) { 1249 if (iter.type() == relocInfo::virtual_call_type) { 1250 if (CompiledIC::is_icholder_call_site(iter.virtual_call_reloc())) { 1251 CompiledIC *ic = CompiledIC_at(&iter); 1252 if (TraceCompiledIC) { 1253 tty->print("noticed icholder " INTPTR_FORMAT " ", p2i(ic->cached_icholder())); 1254 ic->print(); 1255 } 1256 assert(ic->cached_icholder() != NULL, "must be non-NULL"); 1257 count++; 1258 } 1259 } 1260 } 1261 1262 return count; 1263 } 1264 1265 // This is a private interface with the sweeper. 1266 void nmethod::mark_as_seen_on_stack() { 1267 assert(is_alive(), "Must be an alive method"); 1268 // Set the traversal mark to ensure that the sweeper does 2 1269 // cleaning passes before moving to zombie. 1270 set_stack_traversal_mark(NMethodSweeper::traversal_count()); 1271 } 1272 1273 // Tell if a non-entrant method can be converted to a zombie (i.e., 1274 // there are no activations on the stack, not in use by the VM, 1275 // and not in use by the ServiceThread) 1276 bool nmethod::can_not_entrant_be_converted() { 1277 assert(is_not_entrant(), "must be a non-entrant method"); 1278 1279 // Since the nmethod sweeper only does partial sweep the sweeper's traversal 1280 // count can be greater than the stack traversal count before it hits the 1281 // nmethod for the second time. 1282 return stack_traversal_mark()+1 < NMethodSweeper::traversal_count() && 1283 !is_locked_by_vm(); 1284 } 1285 1286 void nmethod::inc_decompile_count() { 1287 if (!is_compiled_by_c2()) return; 1288 // Could be gated by ProfileTraps, but do not bother... 1289 Method* m = method(); 1290 if (m == NULL) return; 1291 MethodData* mdo = m->method_data(); 1292 if (mdo == NULL) return; 1293 // There is a benign race here. See comments in methodData.hpp. 1294 mdo->inc_decompile_count(); 1295 } 1296 1297 void nmethod::increase_unloading_clock() { 1298 _global_unloading_clock++; 1299 if (_global_unloading_clock == 0) { 1300 // _nmethods are allocated with _unloading_clock == 0, 1301 // so 0 is never used as a clock value. 1302 _global_unloading_clock = 1; 1303 } 1304 } 1305 1306 void nmethod::set_unloading_clock(unsigned char unloading_clock) { 1307 OrderAccess::release_store((volatile jubyte*)&_unloading_clock, unloading_clock); 1308 } 1309 1310 unsigned char nmethod::unloading_clock() { 1311 return (unsigned char)OrderAccess::load_acquire((volatile jubyte*)&_unloading_clock); 1312 } 1313 1314 void nmethod::make_unloaded(BoolObjectClosure* is_alive, oop cause) { 1315 1316 post_compiled_method_unload(); 1317 1318 // Since this nmethod is being unloaded, make sure that dependencies 1319 // recorded in instanceKlasses get flushed and pass non-NULL closure to 1320 // indicate that this work is being done during a GC. 1321 assert(Universe::heap()->is_gc_active(), "should only be called during gc"); 1322 assert(is_alive != NULL, "Should be non-NULL"); 1323 // A non-NULL is_alive closure indicates that this is being called during GC. 1324 flush_dependencies(is_alive); 1325 1326 // Break cycle between nmethod & method 1327 if (TraceClassUnloading && WizardMode) { 1328 tty->print_cr("[Class unloading: Making nmethod " INTPTR_FORMAT 1329 " unloadable], Method*(" INTPTR_FORMAT 1330 "), cause(" INTPTR_FORMAT ")", 1331 this, (address)_method, (address)cause); 1332 if (!Universe::heap()->is_gc_active()) 1333 cause->klass()->print(); 1334 } 1335 // Unlink the osr method, so we do not look this up again 1336 if (is_osr_method()) { 1337 invalidate_osr_method(); 1338 } 1339 // If _method is already NULL the Method* is about to be unloaded, 1340 // so we don't have to break the cycle. Note that it is possible to 1341 // have the Method* live here, in case we unload the nmethod because 1342 // it is pointing to some oop (other than the Method*) being unloaded. 1343 if (_method != NULL) { 1344 // OSR methods point to the Method*, but the Method* does not 1345 // point back! 1346 if (_method->code() == this) { 1347 _method->clear_code(); // Break a cycle 1348 } 1349 _method = NULL; // Clear the method of this dead nmethod 1350 } 1351 // Make the class unloaded - i.e., change state and notify sweeper 1352 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1353 if (is_in_use()) { 1354 // Transitioning directly from live to unloaded -- so 1355 // we need to force a cache clean-up; remember this 1356 // for later on. 1357 CodeCache::set_needs_cache_clean(true); 1358 } 1359 1360 // Unregister must be done before the state change 1361 Universe::heap()->unregister_nmethod(this); 1362 1363 _state = unloaded; 1364 1365 // Log the unloading. 1366 log_state_change(); 1367 1368 // The Method* is gone at this point 1369 assert(_method == NULL, "Tautology"); 1370 1371 set_osr_link(NULL); 1372 //set_scavenge_root_link(NULL); // done by prune_scavenge_root_nmethods 1373 NMethodSweeper::report_state_change(this); 1374 } 1375 1376 void nmethod::invalidate_osr_method() { 1377 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod"); 1378 // Remove from list of active nmethods 1379 if (method() != NULL) 1380 method()->method_holder()->remove_osr_nmethod(this); 1381 } 1382 1383 void nmethod::log_state_change() const { 1384 if (LogCompilation) { 1385 if (xtty != NULL) { 1386 ttyLocker ttyl; // keep the following output all in one block 1387 if (_state == unloaded) { 1388 xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'", 1389 os::current_thread_id()); 1390 } else { 1391 xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s", 1392 os::current_thread_id(), 1393 (_state == zombie ? " zombie='1'" : "")); 1394 } 1395 log_identity(xtty); 1396 xtty->stamp(); 1397 xtty->end_elem(); 1398 } 1399 } 1400 if (PrintCompilation && _state != unloaded) { 1401 print_on(tty, _state == zombie ? "made zombie" : "made not entrant"); 1402 } 1403 } 1404 1405 /** 1406 * Common functionality for both make_not_entrant and make_zombie 1407 */ 1408 bool nmethod::make_not_entrant_or_zombie(unsigned int state) { 1409 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant"); 1410 assert(!is_zombie(), "should not already be a zombie"); 1411 1412 // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below. 1413 nmethodLocker nml(this); 1414 methodHandle the_method(method()); 1415 No_Safepoint_Verifier nsv; 1416 1417 // during patching, depending on the nmethod state we must notify the GC that 1418 // code has been unloaded, unregistering it. We cannot do this right while 1419 // holding the Patching_lock because we need to use the CodeCache_lock. This 1420 // would be prone to deadlocks. 1421 // This flag is used to remember whether we need to later lock and unregister. 1422 bool nmethod_needs_unregister = false; 1423 1424 { 1425 // invalidate osr nmethod before acquiring the patching lock since 1426 // they both acquire leaf locks and we don't want a deadlock. 1427 // This logic is equivalent to the logic below for patching the 1428 // verified entry point of regular methods. 1429 if (is_osr_method()) { 1430 // this effectively makes the osr nmethod not entrant 1431 invalidate_osr_method(); 1432 } 1433 1434 // Enter critical section. Does not block for safepoint. 1435 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag); 1436 1437 if (_state == state) { 1438 // another thread already performed this transition so nothing 1439 // to do, but return false to indicate this. 1440 return false; 1441 } 1442 1443 // The caller can be calling the method statically or through an inline 1444 // cache call. 1445 if (!is_osr_method() && !is_not_entrant()) { 1446 NativeJump::patch_verified_entry(entry_point(), verified_entry_point(), 1447 SharedRuntime::get_handle_wrong_method_stub()); 1448 } 1449 1450 if (is_in_use()) { 1451 // It's a true state change, so mark the method as decompiled. 1452 // Do it only for transition from alive. 1453 inc_decompile_count(); 1454 } 1455 1456 // If the state is becoming a zombie, signal to unregister the nmethod with 1457 // the heap. 1458 // This nmethod may have already been unloaded during a full GC. 1459 if ((state == zombie) && !is_unloaded()) { 1460 nmethod_needs_unregister = true; 1461 } 1462 1463 // Must happen before state change. Otherwise we have a race condition in 1464 // nmethod::can_not_entrant_be_converted(). I.e., a method can immediately 1465 // transition its state from 'not_entrant' to 'zombie' without having to wait 1466 // for stack scanning. 1467 if (state == not_entrant) { 1468 mark_as_seen_on_stack(); 1469 OrderAccess::storestore(); 1470 } 1471 1472 // Change state 1473 _state = state; 1474 1475 // Log the transition once 1476 log_state_change(); 1477 1478 // Remove nmethod from method. 1479 // We need to check if both the _code and _from_compiled_code_entry_point 1480 // refer to this nmethod because there is a race in setting these two fields 1481 // in Method* as seen in bugid 4947125. 1482 // If the vep() points to the zombie nmethod, the memory for the nmethod 1483 // could be flushed and the compiler and vtable stubs could still call 1484 // through it. 1485 if (method() != NULL && (method()->code() == this || 1486 method()->from_compiled_entry() == verified_entry_point())) { 1487 HandleMark hm; 1488 method()->clear_code(); 1489 } 1490 } // leave critical region under Patching_lock 1491 1492 // When the nmethod becomes zombie it is no longer alive so the 1493 // dependencies must be flushed. nmethods in the not_entrant 1494 // state will be flushed later when the transition to zombie 1495 // happens or they get unloaded. 1496 if (state == zombie) { 1497 { 1498 // Flushing dependecies must be done before any possible 1499 // safepoint can sneak in, otherwise the oops used by the 1500 // dependency logic could have become stale. 1501 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1502 if (nmethod_needs_unregister) { 1503 Universe::heap()->unregister_nmethod(this); 1504 } 1505 flush_dependencies(NULL); 1506 } 1507 1508 1509 1510 // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload 1511 // event and it hasn't already been reported for this nmethod then 1512 // report it now. The event may have been reported earilier if the GC 1513 // marked it for unloading). JvmtiDeferredEventQueue support means 1514 // we no longer go to a safepoint here. 1515 post_compiled_method_unload(); 1516 1517 #ifdef ASSERT 1518 // It's no longer safe to access the oops section since zombie 1519 // nmethods aren't scanned for GC. 1520 _oops_are_stale = true; 1521 #endif 1522 // the Method may be reclaimed by class unloading now that the 1523 // nmethod is in zombie state 1524 set_method(NULL); 1525 } else { 1526 assert(state == not_entrant, "other cases may need to be handled differently"); 1527 } 1528 1529 if (TraceCreateZombies) { 1530 tty->print_cr("nmethod <" INTPTR_FORMAT "> code made %s", this, (state == not_entrant) ? "not entrant" : "zombie"); 1531 } 1532 1533 NMethodSweeper::report_state_change(this); 1534 return true; 1535 } 1536 1537 void nmethod::flush() { 1538 // Note that there are no valid oops in the nmethod anymore. 1539 assert(is_zombie() || (is_osr_method() && is_unloaded()), "must be a zombie method"); 1540 assert(is_marked_for_reclamation() || (is_osr_method() && is_unloaded()), "must be marked for reclamation"); 1541 1542 assert (!is_locked_by_vm(), "locked methods shouldn't be flushed"); 1543 assert_locked_or_safepoint(CodeCache_lock); 1544 1545 // completely deallocate this method 1546 Events::log(JavaThread::current(), "flushing nmethod " INTPTR_FORMAT, this); 1547 if (PrintMethodFlushing) { 1548 tty->print_cr("*flushing nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT "/Free CodeCache:" SIZE_FORMAT "Kb", 1549 _compile_id, this, CodeCache::nof_blobs(), CodeCache::unallocated_capacity(CodeCache::get_code_blob_type(_comp_level))/1024); 1550 } 1551 1552 // We need to deallocate any ExceptionCache data. 1553 // Note that we do not need to grab the nmethod lock for this, it 1554 // better be thread safe if we're disposing of it! 1555 ExceptionCache* ec = exception_cache(); 1556 set_exception_cache(NULL); 1557 while(ec != NULL) { 1558 ExceptionCache* next = ec->next(); 1559 delete ec; 1560 ec = next; 1561 } 1562 1563 if (on_scavenge_root_list()) { 1564 CodeCache::drop_scavenge_root_nmethod(this); 1565 } 1566 1567 #ifdef SHARK 1568 ((SharkCompiler *) compiler())->free_compiled_method(insts_begin()); 1569 #endif // SHARK 1570 1571 ((CodeBlob*)(this))->flush(); 1572 1573 CodeCache::free(this); 1574 } 1575 1576 // 1577 // Notify all classes this nmethod is dependent on that it is no 1578 // longer dependent. This should only be called in two situations. 1579 // First, when a nmethod transitions to a zombie all dependents need 1580 // to be clear. Since zombification happens at a safepoint there's no 1581 // synchronization issues. The second place is a little more tricky. 1582 // During phase 1 of mark sweep class unloading may happen and as a 1583 // result some nmethods may get unloaded. In this case the flushing 1584 // of dependencies must happen during phase 1 since after GC any 1585 // dependencies in the unloaded nmethod won't be updated, so 1586 // traversing the dependency information in unsafe. In that case this 1587 // function is called with a non-NULL argument and this function only 1588 // notifies instanceKlasses that are reachable 1589 1590 void nmethod::flush_dependencies(BoolObjectClosure* is_alive) { 1591 assert_locked_or_safepoint(CodeCache_lock); 1592 assert(Universe::heap()->is_gc_active() == (is_alive != NULL), 1593 "is_alive is non-NULL if and only if we are called during GC"); 1594 if (!has_flushed_dependencies()) { 1595 set_has_flushed_dependencies(); 1596 for (Dependencies::DepStream deps(this); deps.next(); ) { 1597 Klass* klass = deps.context_type(); 1598 if (klass == NULL) continue; // ignore things like evol_method 1599 1600 // During GC the is_alive closure is non-NULL, and is used to 1601 // determine liveness of dependees that need to be updated. 1602 if (is_alive == NULL || klass->is_loader_alive(is_alive)) { 1603 InstanceKlass::cast(klass)->remove_dependent_nmethod(this); 1604 } 1605 } 1606 } 1607 } 1608 1609 1610 // If this oop is not live, the nmethod can be unloaded. 1611 bool nmethod::can_unload(BoolObjectClosure* is_alive, oop* root, bool unloading_occurred) { 1612 assert(root != NULL, "just checking"); 1613 oop obj = *root; 1614 if (obj == NULL || is_alive->do_object_b(obj)) { 1615 return false; 1616 } 1617 1618 // If ScavengeRootsInCode is true, an nmethod might be unloaded 1619 // simply because one of its constant oops has gone dead. 1620 // No actual classes need to be unloaded in order for this to occur. 1621 assert(unloading_occurred || ScavengeRootsInCode, "Inconsistency in unloading"); 1622 make_unloaded(is_alive, obj); 1623 return true; 1624 } 1625 1626 // ------------------------------------------------------------------ 1627 // post_compiled_method_load_event 1628 // new method for install_code() path 1629 // Transfer information from compilation to jvmti 1630 void nmethod::post_compiled_method_load_event() { 1631 1632 Method* moop = method(); 1633 HOTSPOT_COMPILED_METHOD_LOAD( 1634 (char *) moop->klass_name()->bytes(), 1635 moop->klass_name()->utf8_length(), 1636 (char *) moop->name()->bytes(), 1637 moop->name()->utf8_length(), 1638 (char *) moop->signature()->bytes(), 1639 moop->signature()->utf8_length(), 1640 insts_begin(), insts_size()); 1641 1642 if (JvmtiExport::should_post_compiled_method_load() || 1643 JvmtiExport::should_post_compiled_method_unload()) { 1644 get_and_cache_jmethod_id(); 1645 } 1646 1647 if (JvmtiExport::should_post_compiled_method_load()) { 1648 // Let the Service thread (which is a real Java thread) post the event 1649 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); 1650 JvmtiDeferredEventQueue::enqueue( 1651 JvmtiDeferredEvent::compiled_method_load_event(this)); 1652 } 1653 } 1654 1655 jmethodID nmethod::get_and_cache_jmethod_id() { 1656 if (_jmethod_id == NULL) { 1657 // Cache the jmethod_id since it can no longer be looked up once the 1658 // method itself has been marked for unloading. 1659 _jmethod_id = method()->jmethod_id(); 1660 } 1661 return _jmethod_id; 1662 } 1663 1664 void nmethod::post_compiled_method_unload() { 1665 if (unload_reported()) { 1666 // During unloading we transition to unloaded and then to zombie 1667 // and the unloading is reported during the first transition. 1668 return; 1669 } 1670 1671 assert(_method != NULL && !is_unloaded(), "just checking"); 1672 DTRACE_METHOD_UNLOAD_PROBE(method()); 1673 1674 // If a JVMTI agent has enabled the CompiledMethodUnload event then 1675 // post the event. Sometime later this nmethod will be made a zombie 1676 // by the sweeper but the Method* will not be valid at that point. 1677 // If the _jmethod_id is null then no load event was ever requested 1678 // so don't bother posting the unload. The main reason for this is 1679 // that the jmethodID is a weak reference to the Method* so if 1680 // it's being unloaded there's no way to look it up since the weak 1681 // ref will have been cleared. 1682 if (_jmethod_id != NULL && JvmtiExport::should_post_compiled_method_unload()) { 1683 assert(!unload_reported(), "already unloaded"); 1684 JvmtiDeferredEvent event = 1685 JvmtiDeferredEvent::compiled_method_unload_event(this, 1686 _jmethod_id, insts_begin()); 1687 if (SafepointSynchronize::is_at_safepoint()) { 1688 // Don't want to take the queueing lock. Add it as pending and 1689 // it will get enqueued later. 1690 JvmtiDeferredEventQueue::add_pending_event(event); 1691 } else { 1692 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); 1693 JvmtiDeferredEventQueue::enqueue(event); 1694 } 1695 } 1696 1697 // The JVMTI CompiledMethodUnload event can be enabled or disabled at 1698 // any time. As the nmethod is being unloaded now we mark it has 1699 // having the unload event reported - this will ensure that we don't 1700 // attempt to report the event in the unlikely scenario where the 1701 // event is enabled at the time the nmethod is made a zombie. 1702 set_unload_reported(); 1703 } 1704 1705 void static clean_ic_if_metadata_is_dead(CompiledIC *ic, BoolObjectClosure *is_alive) { 1706 if (ic->is_icholder_call()) { 1707 // The only exception is compiledICHolder oops which may 1708 // yet be marked below. (We check this further below). 1709 CompiledICHolder* cichk_oop = ic->cached_icholder(); 1710 if (cichk_oop->holder_method()->method_holder()->is_loader_alive(is_alive) && 1711 cichk_oop->holder_klass()->is_loader_alive(is_alive)) { 1712 return; 1713 } 1714 } else { 1715 Metadata* ic_oop = ic->cached_metadata(); 1716 if (ic_oop != NULL) { 1717 if (ic_oop->is_klass()) { 1718 if (((Klass*)ic_oop)->is_loader_alive(is_alive)) { 1719 return; 1720 } 1721 } else if (ic_oop->is_method()) { 1722 if (((Method*)ic_oop)->method_holder()->is_loader_alive(is_alive)) { 1723 return; 1724 } 1725 } else { 1726 ShouldNotReachHere(); 1727 } 1728 } 1729 } 1730 1731 ic->set_to_clean(); 1732 } 1733 1734 // This is called at the end of the strong tracing/marking phase of a 1735 // GC to unload an nmethod if it contains otherwise unreachable 1736 // oops. 1737 1738 void nmethod::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) { 1739 // Make sure the oop's ready to receive visitors 1740 assert(!is_zombie() && !is_unloaded(), 1741 "should not call follow on zombie or unloaded nmethod"); 1742 1743 // If the method is not entrant then a JMP is plastered over the 1744 // first few bytes. If an oop in the old code was there, that oop 1745 // should not get GC'd. Skip the first few bytes of oops on 1746 // not-entrant methods. 1747 address low_boundary = verified_entry_point(); 1748 if (is_not_entrant()) { 1749 low_boundary += NativeJump::instruction_size; 1750 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1751 // (See comment above.) 1752 } 1753 1754 // The RedefineClasses() API can cause the class unloading invariant 1755 // to no longer be true. See jvmtiExport.hpp for details. 1756 // Also, leave a debugging breadcrumb in local flag. 1757 bool a_class_was_redefined = JvmtiExport::has_redefined_a_class(); 1758 if (a_class_was_redefined) { 1759 // This set of the unloading_occurred flag is done before the 1760 // call to post_compiled_method_unload() so that the unloading 1761 // of this nmethod is reported. 1762 unloading_occurred = true; 1763 } 1764 1765 // Exception cache 1766 clean_exception_cache(is_alive); 1767 1768 // If class unloading occurred we first iterate over all inline caches and 1769 // clear ICs where the cached oop is referring to an unloaded klass or method. 1770 // The remaining live cached oops will be traversed in the relocInfo::oop_type 1771 // iteration below. 1772 if (unloading_occurred) { 1773 RelocIterator iter(this, low_boundary); 1774 while(iter.next()) { 1775 if (iter.type() == relocInfo::virtual_call_type) { 1776 CompiledIC *ic = CompiledIC_at(&iter); 1777 clean_ic_if_metadata_is_dead(ic, is_alive); 1778 } 1779 } 1780 } 1781 1782 // Compiled code 1783 { 1784 RelocIterator iter(this, low_boundary); 1785 while (iter.next()) { 1786 if (iter.type() == relocInfo::oop_type) { 1787 oop_Relocation* r = iter.oop_reloc(); 1788 // In this loop, we must only traverse those oops directly embedded in 1789 // the code. Other oops (oop_index>0) are seen as part of scopes_oops. 1790 assert(1 == (r->oop_is_immediate()) + 1791 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), 1792 "oop must be found in exactly one place"); 1793 if (r->oop_is_immediate() && r->oop_value() != NULL) { 1794 if (can_unload(is_alive, r->oop_addr(), unloading_occurred)) { 1795 return; 1796 } 1797 } 1798 } 1799 } 1800 } 1801 1802 1803 // Scopes 1804 for (oop* p = oops_begin(); p < oops_end(); p++) { 1805 if (*p == Universe::non_oop_word()) continue; // skip non-oops 1806 if (can_unload(is_alive, p, unloading_occurred)) { 1807 return; 1808 } 1809 } 1810 1811 // Ensure that all metadata is still alive 1812 verify_metadata_loaders(low_boundary, is_alive); 1813 } 1814 1815 template <class CompiledICorStaticCall> 1816 static bool clean_if_nmethod_is_unloaded(CompiledICorStaticCall *ic, address addr, BoolObjectClosure *is_alive, nmethod* from) { 1817 // Ok, to lookup references to zombies here 1818 CodeBlob *cb = CodeCache::find_blob_unsafe(addr); 1819 if (cb != NULL && cb->is_nmethod()) { 1820 nmethod* nm = (nmethod*)cb; 1821 1822 if (nm->unloading_clock() != nmethod::global_unloading_clock()) { 1823 // The nmethod has not been processed yet. 1824 return true; 1825 } 1826 1827 // Clean inline caches pointing to both zombie and not_entrant methods 1828 if (!nm->is_in_use() || (nm->method()->code() != nm)) { 1829 ic->set_to_clean(); 1830 assert(ic->is_clean(), err_msg("nmethod " PTR_FORMAT "not clean %s", from, from->method()->name_and_sig_as_C_string())); 1831 } 1832 } 1833 1834 return false; 1835 } 1836 1837 static bool clean_if_nmethod_is_unloaded(CompiledIC *ic, BoolObjectClosure *is_alive, nmethod* from) { 1838 return clean_if_nmethod_is_unloaded(ic, ic->ic_destination(), is_alive, from); 1839 } 1840 1841 static bool clean_if_nmethod_is_unloaded(CompiledStaticCall *csc, BoolObjectClosure *is_alive, nmethod* from) { 1842 return clean_if_nmethod_is_unloaded(csc, csc->destination(), is_alive, from); 1843 } 1844 1845 bool nmethod::do_unloading_parallel(BoolObjectClosure* is_alive, bool unloading_occurred) { 1846 ResourceMark rm; 1847 1848 // Make sure the oop's ready to receive visitors 1849 assert(!is_zombie() && !is_unloaded(), 1850 "should not call follow on zombie or unloaded nmethod"); 1851 1852 // If the method is not entrant then a JMP is plastered over the 1853 // first few bytes. If an oop in the old code was there, that oop 1854 // should not get GC'd. Skip the first few bytes of oops on 1855 // not-entrant methods. 1856 address low_boundary = verified_entry_point(); 1857 if (is_not_entrant()) { 1858 low_boundary += NativeJump::instruction_size; 1859 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1860 // (See comment above.) 1861 } 1862 1863 // The RedefineClasses() API can cause the class unloading invariant 1864 // to no longer be true. See jvmtiExport.hpp for details. 1865 // Also, leave a debugging breadcrumb in local flag. 1866 bool a_class_was_redefined = JvmtiExport::has_redefined_a_class(); 1867 if (a_class_was_redefined) { 1868 // This set of the unloading_occurred flag is done before the 1869 // call to post_compiled_method_unload() so that the unloading 1870 // of this nmethod is reported. 1871 unloading_occurred = true; 1872 } 1873 1874 // Exception cache 1875 clean_exception_cache(is_alive); 1876 1877 bool is_unloaded = false; 1878 bool postponed = false; 1879 1880 RelocIterator iter(this, low_boundary); 1881 while(iter.next()) { 1882 1883 switch (iter.type()) { 1884 1885 case relocInfo::virtual_call_type: 1886 if (unloading_occurred) { 1887 // If class unloading occurred we first iterate over all inline caches and 1888 // clear ICs where the cached oop is referring to an unloaded klass or method. 1889 clean_ic_if_metadata_is_dead(CompiledIC_at(&iter), is_alive); 1890 } 1891 1892 postponed |= clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), is_alive, this); 1893 break; 1894 1895 case relocInfo::opt_virtual_call_type: 1896 postponed |= clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), is_alive, this); 1897 break; 1898 1899 case relocInfo::static_call_type: 1900 postponed |= clean_if_nmethod_is_unloaded(compiledStaticCall_at(iter.reloc()), is_alive, this); 1901 break; 1902 1903 case relocInfo::oop_type: 1904 if (!is_unloaded) { 1905 // Unload check 1906 oop_Relocation* r = iter.oop_reloc(); 1907 // Traverse those oops directly embedded in the code. 1908 // Other oops (oop_index>0) are seen as part of scopes_oops. 1909 assert(1 == (r->oop_is_immediate()) + 1910 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), 1911 "oop must be found in exactly one place"); 1912 if (r->oop_is_immediate() && r->oop_value() != NULL) { 1913 if (can_unload(is_alive, r->oop_addr(), unloading_occurred)) { 1914 is_unloaded = true; 1915 } 1916 } 1917 } 1918 break; 1919 1920 } 1921 } 1922 1923 if (is_unloaded) { 1924 return postponed; 1925 } 1926 1927 // Scopes 1928 for (oop* p = oops_begin(); p < oops_end(); p++) { 1929 if (*p == Universe::non_oop_word()) continue; // skip non-oops 1930 if (can_unload(is_alive, p, unloading_occurred)) { 1931 is_unloaded = true; 1932 break; 1933 } 1934 } 1935 1936 if (is_unloaded) { 1937 return postponed; 1938 } 1939 1940 // Ensure that all metadata is still alive 1941 verify_metadata_loaders(low_boundary, is_alive); 1942 1943 return postponed; 1944 } 1945 1946 void nmethod::do_unloading_parallel_postponed(BoolObjectClosure* is_alive, bool unloading_occurred) { 1947 ResourceMark rm; 1948 1949 // Make sure the oop's ready to receive visitors 1950 assert(!is_zombie(), 1951 "should not call follow on zombie nmethod"); 1952 1953 // If the method is not entrant then a JMP is plastered over the 1954 // first few bytes. If an oop in the old code was there, that oop 1955 // should not get GC'd. Skip the first few bytes of oops on 1956 // not-entrant methods. 1957 address low_boundary = verified_entry_point(); 1958 if (is_not_entrant()) { 1959 low_boundary += NativeJump::instruction_size; 1960 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1961 // (See comment above.) 1962 } 1963 1964 RelocIterator iter(this, low_boundary); 1965 while(iter.next()) { 1966 1967 switch (iter.type()) { 1968 1969 case relocInfo::virtual_call_type: 1970 clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), is_alive, this); 1971 break; 1972 1973 case relocInfo::opt_virtual_call_type: 1974 clean_if_nmethod_is_unloaded(CompiledIC_at(&iter), is_alive, this); 1975 break; 1976 1977 case relocInfo::static_call_type: 1978 clean_if_nmethod_is_unloaded(compiledStaticCall_at(iter.reloc()), is_alive, this); 1979 break; 1980 } 1981 } 1982 } 1983 1984 #ifdef ASSERT 1985 1986 class CheckClass : AllStatic { 1987 static BoolObjectClosure* _is_alive; 1988 1989 // Check class_loader is alive for this bit of metadata. 1990 static void check_class(Metadata* md) { 1991 Klass* klass = NULL; 1992 if (md->is_klass()) { 1993 klass = ((Klass*)md); 1994 } else if (md->is_method()) { 1995 klass = ((Method*)md)->method_holder(); 1996 } else if (md->is_methodData()) { 1997 klass = ((MethodData*)md)->method()->method_holder(); 1998 } else { 1999 md->print(); 2000 ShouldNotReachHere(); 2001 } 2002 assert(klass->is_loader_alive(_is_alive), "must be alive"); 2003 } 2004 public: 2005 static void do_check_class(BoolObjectClosure* is_alive, nmethod* nm) { 2006 assert(SafepointSynchronize::is_at_safepoint(), "this is only ok at safepoint"); 2007 _is_alive = is_alive; 2008 nm->metadata_do(check_class); 2009 } 2010 }; 2011 2012 // This is called during a safepoint so can use static data 2013 BoolObjectClosure* CheckClass::_is_alive = NULL; 2014 #endif // ASSERT 2015 2016 2017 // Processing of oop references should have been sufficient to keep 2018 // all strong references alive. Any weak references should have been 2019 // cleared as well. Visit all the metadata and ensure that it's 2020 // really alive. 2021 void nmethod::verify_metadata_loaders(address low_boundary, BoolObjectClosure* is_alive) { 2022 #ifdef ASSERT 2023 RelocIterator iter(this, low_boundary); 2024 while (iter.next()) { 2025 // static_stub_Relocations may have dangling references to 2026 // Method*s so trim them out here. Otherwise it looks like 2027 // compiled code is maintaining a link to dead metadata. 2028 address static_call_addr = NULL; 2029 if (iter.type() == relocInfo::opt_virtual_call_type) { 2030 CompiledIC* cic = CompiledIC_at(&iter); 2031 if (!cic->is_call_to_interpreted()) { 2032 static_call_addr = iter.addr(); 2033 } 2034 } else if (iter.type() == relocInfo::static_call_type) { 2035 CompiledStaticCall* csc = compiledStaticCall_at(iter.reloc()); 2036 if (!csc->is_call_to_interpreted()) { 2037 static_call_addr = iter.addr(); 2038 } 2039 } 2040 if (static_call_addr != NULL) { 2041 RelocIterator sciter(this, low_boundary); 2042 while (sciter.next()) { 2043 if (sciter.type() == relocInfo::static_stub_type && 2044 sciter.static_stub_reloc()->static_call() == static_call_addr) { 2045 sciter.static_stub_reloc()->clear_inline_cache(); 2046 } 2047 } 2048 } 2049 } 2050 // Check that the metadata embedded in the nmethod is alive 2051 CheckClass::do_check_class(is_alive, this); 2052 #endif 2053 } 2054 2055 2056 // Iterate over metadata calling this function. Used by RedefineClasses 2057 void nmethod::metadata_do(void f(Metadata*)) { 2058 address low_boundary = verified_entry_point(); 2059 if (is_not_entrant()) { 2060 low_boundary += NativeJump::instruction_size; 2061 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 2062 // (See comment above.) 2063 } 2064 { 2065 // Visit all immediate references that are embedded in the instruction stream. 2066 RelocIterator iter(this, low_boundary); 2067 while (iter.next()) { 2068 if (iter.type() == relocInfo::metadata_type ) { 2069 metadata_Relocation* r = iter.metadata_reloc(); 2070 // In this lmetadata, we must only follow those metadatas directly embedded in 2071 // the code. Other metadatas (oop_index>0) are seen as part of 2072 // the metadata section below. 2073 assert(1 == (r->metadata_is_immediate()) + 2074 (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()), 2075 "metadata must be found in exactly one place"); 2076 if (r->metadata_is_immediate() && r->metadata_value() != NULL) { 2077 Metadata* md = r->metadata_value(); 2078 if (md != _method) f(md); 2079 } 2080 } else if (iter.type() == relocInfo::virtual_call_type) { 2081 // Check compiledIC holders associated with this nmethod 2082 CompiledIC *ic = CompiledIC_at(&iter); 2083 if (ic->is_icholder_call()) { 2084 CompiledICHolder* cichk = ic->cached_icholder(); 2085 f(cichk->holder_method()); 2086 f(cichk->holder_klass()); 2087 } else { 2088 Metadata* ic_oop = ic->cached_metadata(); 2089 if (ic_oop != NULL) { 2090 f(ic_oop); 2091 } 2092 } 2093 } 2094 } 2095 } 2096 2097 // Visit the metadata section 2098 for (Metadata** p = metadata_begin(); p < metadata_end(); p++) { 2099 if (*p == Universe::non_oop_word() || *p == NULL) continue; // skip non-oops 2100 Metadata* md = *p; 2101 f(md); 2102 } 2103 2104 // Call function Method*, not embedded in these other places. 2105 if (_method != NULL) f(_method); 2106 } 2107 2108 void nmethod::oops_do(OopClosure* f, bool allow_zombie) { 2109 // make sure the oops ready to receive visitors 2110 assert(allow_zombie || !is_zombie(), "should not call follow on zombie nmethod"); 2111 assert(!is_unloaded(), "should not call follow on unloaded nmethod"); 2112 2113 // If the method is not entrant or zombie then a JMP is plastered over the 2114 // first few bytes. If an oop in the old code was there, that oop 2115 // should not get GC'd. Skip the first few bytes of oops on 2116 // not-entrant methods. 2117 address low_boundary = verified_entry_point(); 2118 if (is_not_entrant()) { 2119 low_boundary += NativeJump::instruction_size; 2120 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 2121 // (See comment above.) 2122 } 2123 2124 RelocIterator iter(this, low_boundary); 2125 2126 while (iter.next()) { 2127 if (iter.type() == relocInfo::oop_type ) { 2128 oop_Relocation* r = iter.oop_reloc(); 2129 // In this loop, we must only follow those oops directly embedded in 2130 // the code. Other oops (oop_index>0) are seen as part of scopes_oops. 2131 assert(1 == (r->oop_is_immediate()) + 2132 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), 2133 "oop must be found in exactly one place"); 2134 if (r->oop_is_immediate() && r->oop_value() != NULL) { 2135 f->do_oop(r->oop_addr()); 2136 } 2137 } 2138 } 2139 2140 // Scopes 2141 // This includes oop constants not inlined in the code stream. 2142 for (oop* p = oops_begin(); p < oops_end(); p++) { 2143 if (*p == Universe::non_oop_word()) continue; // skip non-oops 2144 f->do_oop(p); 2145 } 2146 } 2147 2148 #define NMETHOD_SENTINEL ((nmethod*)badAddress) 2149 2150 nmethod* volatile nmethod::_oops_do_mark_nmethods; 2151 2152 // An nmethod is "marked" if its _mark_link is set non-null. 2153 // Even if it is the end of the linked list, it will have a non-null link value, 2154 // as long as it is on the list. 2155 // This code must be MP safe, because it is used from parallel GC passes. 2156 bool nmethod::test_set_oops_do_mark() { 2157 assert(nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called"); 2158 nmethod* observed_mark_link = _oops_do_mark_link; 2159 if (observed_mark_link == NULL) { 2160 // Claim this nmethod for this thread to mark. 2161 observed_mark_link = (nmethod*) 2162 Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_link, NULL); 2163 if (observed_mark_link == NULL) { 2164 2165 // Atomically append this nmethod (now claimed) to the head of the list: 2166 nmethod* observed_mark_nmethods = _oops_do_mark_nmethods; 2167 for (;;) { 2168 nmethod* required_mark_nmethods = observed_mark_nmethods; 2169 _oops_do_mark_link = required_mark_nmethods; 2170 observed_mark_nmethods = (nmethod*) 2171 Atomic::cmpxchg_ptr(this, &_oops_do_mark_nmethods, required_mark_nmethods); 2172 if (observed_mark_nmethods == required_mark_nmethods) 2173 break; 2174 } 2175 // Mark was clear when we first saw this guy. 2176 NOT_PRODUCT(if (TraceScavenge) print_on(tty, "oops_do, mark")); 2177 return false; 2178 } 2179 } 2180 // On fall through, another racing thread marked this nmethod before we did. 2181 return true; 2182 } 2183 2184 void nmethod::oops_do_marking_prologue() { 2185 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("[oops_do_marking_prologue")); 2186 assert(_oops_do_mark_nmethods == NULL, "must not call oops_do_marking_prologue twice in a row"); 2187 // We use cmpxchg_ptr instead of regular assignment here because the user 2188 // may fork a bunch of threads, and we need them all to see the same state. 2189 void* observed = Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_nmethods, NULL); 2190 guarantee(observed == NULL, "no races in this sequential code"); 2191 } 2192 2193 void nmethod::oops_do_marking_epilogue() { 2194 assert(_oops_do_mark_nmethods != NULL, "must not call oops_do_marking_epilogue twice in a row"); 2195 nmethod* cur = _oops_do_mark_nmethods; 2196 while (cur != NMETHOD_SENTINEL) { 2197 assert(cur != NULL, "not NULL-terminated"); 2198 nmethod* next = cur->_oops_do_mark_link; 2199 cur->_oops_do_mark_link = NULL; 2200 cur->verify_oop_relocations(); 2201 NOT_PRODUCT(if (TraceScavenge) cur->print_on(tty, "oops_do, unmark")); 2202 cur = next; 2203 } 2204 void* required = _oops_do_mark_nmethods; 2205 void* observed = Atomic::cmpxchg_ptr(NULL, &_oops_do_mark_nmethods, required); 2206 guarantee(observed == required, "no races in this sequential code"); 2207 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("oops_do_marking_epilogue]")); 2208 } 2209 2210 class DetectScavengeRoot: public OopClosure { 2211 bool _detected_scavenge_root; 2212 public: 2213 DetectScavengeRoot() : _detected_scavenge_root(false) 2214 { NOT_PRODUCT(_print_nm = NULL); } 2215 bool detected_scavenge_root() { return _detected_scavenge_root; } 2216 virtual void do_oop(oop* p) { 2217 if ((*p) != NULL && (*p)->is_scavengable()) { 2218 NOT_PRODUCT(maybe_print(p)); 2219 _detected_scavenge_root = true; 2220 } 2221 } 2222 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } 2223 2224 #ifndef PRODUCT 2225 nmethod* _print_nm; 2226 void maybe_print(oop* p) { 2227 if (_print_nm == NULL) return; 2228 if (!_detected_scavenge_root) _print_nm->print_on(tty, "new scavenge root"); 2229 tty->print_cr(""PTR_FORMAT"[offset=%d] detected scavengable oop "PTR_FORMAT" (found at "PTR_FORMAT")", 2230 _print_nm, (int)((intptr_t)p - (intptr_t)_print_nm), 2231 (void *)(*p), (intptr_t)p); 2232 (*p)->print(); 2233 } 2234 #endif //PRODUCT 2235 }; 2236 2237 bool nmethod::detect_scavenge_root_oops() { 2238 DetectScavengeRoot detect_scavenge_root; 2239 NOT_PRODUCT(if (TraceScavenge) detect_scavenge_root._print_nm = this); 2240 oops_do(&detect_scavenge_root); 2241 return detect_scavenge_root.detected_scavenge_root(); 2242 } 2243 2244 // Method that knows how to preserve outgoing arguments at call. This method must be 2245 // called with a frame corresponding to a Java invoke 2246 void nmethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { 2247 #ifndef SHARK 2248 if (!method()->is_native()) { 2249 SimpleScopeDesc ssd(this, fr.pc()); 2250 Bytecode_invoke call(ssd.method(), ssd.bci()); 2251 bool has_receiver = call.has_receiver(); 2252 bool has_appendix = call.has_appendix(); 2253 Symbol* signature = call.signature(); 2254 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f); 2255 } 2256 #endif // !SHARK 2257 } 2258 2259 2260 oop nmethod::embeddedOop_at(u_char* p) { 2261 RelocIterator iter(this, p, p + 1); 2262 while (iter.next()) 2263 if (iter.type() == relocInfo::oop_type) { 2264 return iter.oop_reloc()->oop_value(); 2265 } 2266 return NULL; 2267 } 2268 2269 2270 inline bool includes(void* p, void* from, void* to) { 2271 return from <= p && p < to; 2272 } 2273 2274 2275 void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) { 2276 assert(count >= 2, "must be sentinel values, at least"); 2277 2278 #ifdef ASSERT 2279 // must be sorted and unique; we do a binary search in find_pc_desc() 2280 int prev_offset = pcs[0].pc_offset(); 2281 assert(prev_offset == PcDesc::lower_offset_limit, 2282 "must start with a sentinel"); 2283 for (int i = 1; i < count; i++) { 2284 int this_offset = pcs[i].pc_offset(); 2285 assert(this_offset > prev_offset, "offsets must be sorted"); 2286 prev_offset = this_offset; 2287 } 2288 assert(prev_offset == PcDesc::upper_offset_limit, 2289 "must end with a sentinel"); 2290 #endif //ASSERT 2291 2292 // Search for MethodHandle invokes and tag the nmethod. 2293 for (int i = 0; i < count; i++) { 2294 if (pcs[i].is_method_handle_invoke()) { 2295 set_has_method_handle_invokes(true); 2296 break; 2297 } 2298 } 2299 assert(has_method_handle_invokes() == (_deoptimize_mh_offset != -1), "must have deopt mh handler"); 2300 2301 int size = count * sizeof(PcDesc); 2302 assert(scopes_pcs_size() >= size, "oob"); 2303 memcpy(scopes_pcs_begin(), pcs, size); 2304 2305 // Adjust the final sentinel downward. 2306 PcDesc* last_pc = &scopes_pcs_begin()[count-1]; 2307 assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity"); 2308 last_pc->set_pc_offset(content_size() + 1); 2309 for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) { 2310 // Fill any rounding gaps with copies of the last record. 2311 last_pc[1] = last_pc[0]; 2312 } 2313 // The following assert could fail if sizeof(PcDesc) is not 2314 // an integral multiple of oopSize (the rounding term). 2315 // If it fails, change the logic to always allocate a multiple 2316 // of sizeof(PcDesc), and fill unused words with copies of *last_pc. 2317 assert(last_pc + 1 == scopes_pcs_end(), "must match exactly"); 2318 } 2319 2320 void nmethod::copy_scopes_data(u_char* buffer, int size) { 2321 assert(scopes_data_size() >= size, "oob"); 2322 memcpy(scopes_data_begin(), buffer, size); 2323 } 2324 2325 2326 #ifdef ASSERT 2327 static PcDesc* linear_search(nmethod* nm, int pc_offset, bool approximate) { 2328 PcDesc* lower = nm->scopes_pcs_begin(); 2329 PcDesc* upper = nm->scopes_pcs_end(); 2330 lower += 1; // exclude initial sentinel 2331 PcDesc* res = NULL; 2332 for (PcDesc* p = lower; p < upper; p++) { 2333 NOT_PRODUCT(--nmethod_stats.pc_desc_tests); // don't count this call to match_desc 2334 if (match_desc(p, pc_offset, approximate)) { 2335 if (res == NULL) 2336 res = p; 2337 else 2338 res = (PcDesc*) badAddress; 2339 } 2340 } 2341 return res; 2342 } 2343 #endif 2344 2345 2346 // Finds a PcDesc with real-pc equal to "pc" 2347 PcDesc* nmethod::find_pc_desc_internal(address pc, bool approximate) { 2348 address base_address = code_begin(); 2349 if ((pc < base_address) || 2350 (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) { 2351 return NULL; // PC is wildly out of range 2352 } 2353 int pc_offset = (int) (pc - base_address); 2354 2355 // Check the PcDesc cache if it contains the desired PcDesc 2356 // (This as an almost 100% hit rate.) 2357 PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate); 2358 if (res != NULL) { 2359 assert(res == linear_search(this, pc_offset, approximate), "cache ok"); 2360 return res; 2361 } 2362 2363 // Fallback algorithm: quasi-linear search for the PcDesc 2364 // Find the last pc_offset less than the given offset. 2365 // The successor must be the required match, if there is a match at all. 2366 // (Use a fixed radix to avoid expensive affine pointer arithmetic.) 2367 PcDesc* lower = scopes_pcs_begin(); 2368 PcDesc* upper = scopes_pcs_end(); 2369 upper -= 1; // exclude final sentinel 2370 if (lower >= upper) return NULL; // native method; no PcDescs at all 2371 2372 #define assert_LU_OK \ 2373 /* invariant on lower..upper during the following search: */ \ 2374 assert(lower->pc_offset() < pc_offset, "sanity"); \ 2375 assert(upper->pc_offset() >= pc_offset, "sanity") 2376 assert_LU_OK; 2377 2378 // Use the last successful return as a split point. 2379 PcDesc* mid = _pc_desc_cache.last_pc_desc(); 2380 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); 2381 if (mid->pc_offset() < pc_offset) { 2382 lower = mid; 2383 } else { 2384 upper = mid; 2385 } 2386 2387 // Take giant steps at first (4096, then 256, then 16, then 1) 2388 const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1); 2389 const int RADIX = (1 << LOG2_RADIX); 2390 for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) { 2391 while ((mid = lower + step) < upper) { 2392 assert_LU_OK; 2393 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); 2394 if (mid->pc_offset() < pc_offset) { 2395 lower = mid; 2396 } else { 2397 upper = mid; 2398 break; 2399 } 2400 } 2401 assert_LU_OK; 2402 } 2403 2404 // Sneak up on the value with a linear search of length ~16. 2405 while (true) { 2406 assert_LU_OK; 2407 mid = lower + 1; 2408 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); 2409 if (mid->pc_offset() < pc_offset) { 2410 lower = mid; 2411 } else { 2412 upper = mid; 2413 break; 2414 } 2415 } 2416 #undef assert_LU_OK 2417 2418 if (match_desc(upper, pc_offset, approximate)) { 2419 assert(upper == linear_search(this, pc_offset, approximate), "search ok"); 2420 _pc_desc_cache.add_pc_desc(upper); 2421 return upper; 2422 } else { 2423 assert(NULL == linear_search(this, pc_offset, approximate), "search ok"); 2424 return NULL; 2425 } 2426 } 2427 2428 2429 void nmethod::check_all_dependencies(DepChange& changes) { 2430 // Checked dependencies are allocated into this ResourceMark 2431 ResourceMark rm; 2432 2433 // Turn off dependency tracing while actually testing dependencies. 2434 NOT_PRODUCT( FlagSetting fs(TraceDependencies, false) ); 2435 2436 typedef ResourceHashtable<DependencySignature, int, &DependencySignature::hash, 2437 &DependencySignature::equals, 11027> DepTable; 2438 2439 DepTable* table = new DepTable(); 2440 2441 // Iterate over live nmethods and check dependencies of all nmethods that are not 2442 // marked for deoptimization. A particular dependency is only checked once. 2443 NMethodIterator iter; 2444 while(iter.next()) { 2445 nmethod* nm = iter.method(); 2446 // Only notify for live nmethods 2447 if (nm->is_alive() && !nm->is_marked_for_deoptimization()) { 2448 for (Dependencies::DepStream deps(nm); deps.next(); ) { 2449 // Construct abstraction of a dependency. 2450 DependencySignature* current_sig = new DependencySignature(deps); 2451 2452 // Determine if dependency is already checked. table->put(...) returns 2453 // 'true' if the dependency is added (i.e., was not in the hashtable). 2454 if (table->put(*current_sig, 1)) { 2455 if (deps.check_dependency() != NULL) { 2456 // Dependency checking failed. Print out information about the failed 2457 // dependency and finally fail with an assert. We can fail here, since 2458 // dependency checking is never done in a product build. 2459 changes.print(); 2460 nm->print(); 2461 nm->print_dependencies(); 2462 assert(false, "Should have been marked for deoptimization"); 2463 } 2464 } 2465 } 2466 } 2467 } 2468 } 2469 2470 bool nmethod::check_dependency_on(DepChange& changes) { 2471 // What has happened: 2472 // 1) a new class dependee has been added 2473 // 2) dependee and all its super classes have been marked 2474 bool found_check = false; // set true if we are upset 2475 for (Dependencies::DepStream deps(this); deps.next(); ) { 2476 // Evaluate only relevant dependencies. 2477 if (deps.spot_check_dependency_at(changes) != NULL) { 2478 found_check = true; 2479 NOT_DEBUG(break); 2480 } 2481 } 2482 return found_check; 2483 } 2484 2485 bool nmethod::is_evol_dependent_on(Klass* dependee) { 2486 InstanceKlass *dependee_ik = InstanceKlass::cast(dependee); 2487 Array<Method*>* dependee_methods = dependee_ik->methods(); 2488 for (Dependencies::DepStream deps(this); deps.next(); ) { 2489 if (deps.type() == Dependencies::evol_method) { 2490 Method* method = deps.method_argument(0); 2491 for (int j = 0; j < dependee_methods->length(); j++) { 2492 if (dependee_methods->at(j) == method) { 2493 // RC_TRACE macro has an embedded ResourceMark 2494 RC_TRACE(0x01000000, 2495 ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)", 2496 _method->method_holder()->external_name(), 2497 _method->name()->as_C_string(), 2498 _method->signature()->as_C_string(), compile_id(), 2499 method->method_holder()->external_name(), 2500 method->name()->as_C_string(), 2501 method->signature()->as_C_string())); 2502 if (TraceDependencies || LogCompilation) 2503 deps.log_dependency(dependee); 2504 return true; 2505 } 2506 } 2507 } 2508 } 2509 return false; 2510 } 2511 2512 // Called from mark_for_deoptimization, when dependee is invalidated. 2513 bool nmethod::is_dependent_on_method(Method* dependee) { 2514 for (Dependencies::DepStream deps(this); deps.next(); ) { 2515 if (deps.type() != Dependencies::evol_method) 2516 continue; 2517 Method* method = deps.method_argument(0); 2518 if (method == dependee) return true; 2519 } 2520 return false; 2521 } 2522 2523 2524 bool nmethod::is_patchable_at(address instr_addr) { 2525 assert(insts_contains(instr_addr), "wrong nmethod used"); 2526 if (is_zombie()) { 2527 // a zombie may never be patched 2528 return false; 2529 } 2530 return true; 2531 } 2532 2533 2534 address nmethod::continuation_for_implicit_exception(address pc) { 2535 // Exception happened outside inline-cache check code => we are inside 2536 // an active nmethod => use cpc to determine a return address 2537 int exception_offset = pc - code_begin(); 2538 int cont_offset = ImplicitExceptionTable(this).at( exception_offset ); 2539 #ifdef ASSERT 2540 if (cont_offset == 0) { 2541 Thread* thread = ThreadLocalStorage::get_thread_slow(); 2542 ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY 2543 HandleMark hm(thread); 2544 ResourceMark rm(thread); 2545 CodeBlob* cb = CodeCache::find_blob(pc); 2546 assert(cb != NULL && cb == this, ""); 2547 tty->print_cr("implicit exception happened at " INTPTR_FORMAT, pc); 2548 print(); 2549 method()->print_codes(); 2550 print_code(); 2551 print_pcs(); 2552 } 2553 #endif 2554 if (cont_offset == 0) { 2555 // Let the normal error handling report the exception 2556 return NULL; 2557 } 2558 return code_begin() + cont_offset; 2559 } 2560 2561 2562 2563 void nmethod_init() { 2564 // make sure you didn't forget to adjust the filler fields 2565 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word"); 2566 } 2567 2568 2569 //------------------------------------------------------------------------------------------- 2570 2571 2572 // QQQ might we make this work from a frame?? 2573 nmethodLocker::nmethodLocker(address pc) { 2574 CodeBlob* cb = CodeCache::find_blob(pc); 2575 guarantee(cb != NULL && cb->is_nmethod(), "bad pc for a nmethod found"); 2576 _nm = (nmethod*)cb; 2577 lock_nmethod(_nm); 2578 } 2579 2580 // Only JvmtiDeferredEvent::compiled_method_unload_event() 2581 // should pass zombie_ok == true. 2582 void nmethodLocker::lock_nmethod(nmethod* nm, bool zombie_ok) { 2583 if (nm == NULL) return; 2584 Atomic::inc(&nm->_lock_count); 2585 assert(zombie_ok || !nm->is_zombie(), "cannot lock a zombie method"); 2586 } 2587 2588 void nmethodLocker::unlock_nmethod(nmethod* nm) { 2589 if (nm == NULL) return; 2590 Atomic::dec(&nm->_lock_count); 2591 assert(nm->_lock_count >= 0, "unmatched nmethod lock/unlock"); 2592 } 2593 2594 2595 // ----------------------------------------------------------------------------- 2596 // nmethod::get_deopt_original_pc 2597 // 2598 // Return the original PC for the given PC if: 2599 // (a) the given PC belongs to a nmethod and 2600 // (b) it is a deopt PC 2601 address nmethod::get_deopt_original_pc(const frame* fr) { 2602 if (fr->cb() == NULL) return NULL; 2603 2604 nmethod* nm = fr->cb()->as_nmethod_or_null(); 2605 if (nm != NULL && nm->is_deopt_pc(fr->pc())) 2606 return nm->get_original_pc(fr); 2607 2608 return NULL; 2609 } 2610 2611 2612 // ----------------------------------------------------------------------------- 2613 // MethodHandle 2614 2615 bool nmethod::is_method_handle_return(address return_pc) { 2616 if (!has_method_handle_invokes()) return false; 2617 PcDesc* pd = pc_desc_at(return_pc); 2618 if (pd == NULL) 2619 return false; 2620 return pd->is_method_handle_invoke(); 2621 } 2622 2623 2624 // ----------------------------------------------------------------------------- 2625 // Verification 2626 2627 class VerifyOopsClosure: public OopClosure { 2628 nmethod* _nm; 2629 bool _ok; 2630 public: 2631 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { } 2632 bool ok() { return _ok; } 2633 virtual void do_oop(oop* p) { 2634 if ((*p) == NULL || (*p)->is_oop()) return; 2635 if (_ok) { 2636 _nm->print_nmethod(true); 2637 _ok = false; 2638 } 2639 tty->print_cr("*** non-oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", 2640 (void *)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); 2641 } 2642 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } 2643 }; 2644 2645 void nmethod::verify() { 2646 2647 // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant 2648 // seems odd. 2649 2650 if( is_zombie() || is_not_entrant() ) 2651 return; 2652 2653 // Make sure all the entry points are correctly aligned for patching. 2654 NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point()); 2655 2656 // assert(method()->is_oop(), "must be valid"); 2657 2658 ResourceMark rm; 2659 2660 if (!CodeCache::contains(this)) { 2661 fatal(err_msg("nmethod at " INTPTR_FORMAT " not in zone", this)); 2662 } 2663 2664 if(is_native_method() ) 2665 return; 2666 2667 nmethod* nm = CodeCache::find_nmethod(verified_entry_point()); 2668 if (nm != this) { 2669 fatal(err_msg("findNMethod did not find this nmethod (" INTPTR_FORMAT ")", 2670 this)); 2671 } 2672 2673 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { 2674 if (! p->verify(this)) { 2675 tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", this); 2676 } 2677 } 2678 2679 VerifyOopsClosure voc(this); 2680 oops_do(&voc); 2681 assert(voc.ok(), "embedded oops must be OK"); 2682 verify_scavenge_root_oops(); 2683 2684 verify_scopes(); 2685 } 2686 2687 2688 void nmethod::verify_interrupt_point(address call_site) { 2689 // Verify IC only when nmethod installation is finished. 2690 bool is_installed = (method()->code() == this) // nmethod is in state 'in_use' and installed 2691 || !this->is_in_use(); // nmethod is installed, but not in 'in_use' state 2692 if (is_installed) { 2693 Thread *cur = Thread::current(); 2694 if (CompiledIC_lock->owner() == cur || 2695 ((cur->is_VM_thread() || cur->is_ConcurrentGC_thread()) && 2696 SafepointSynchronize::is_at_safepoint())) { 2697 CompiledIC_at(this, call_site); 2698 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 2699 } else { 2700 MutexLocker ml_verify (CompiledIC_lock); 2701 CompiledIC_at(this, call_site); 2702 } 2703 } 2704 2705 PcDesc* pd = pc_desc_at(nativeCall_at(call_site)->return_address()); 2706 assert(pd != NULL, "PcDesc must exist"); 2707 for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(), 2708 pd->obj_decode_offset(), pd->should_reexecute(), 2709 pd->return_oop()); 2710 !sd->is_top(); sd = sd->sender()) { 2711 sd->verify(); 2712 } 2713 } 2714 2715 void nmethod::verify_scopes() { 2716 if( !method() ) return; // Runtime stubs have no scope 2717 if (method()->is_native()) return; // Ignore stub methods. 2718 // iterate through all interrupt point 2719 // and verify the debug information is valid. 2720 RelocIterator iter((nmethod*)this); 2721 while (iter.next()) { 2722 address stub = NULL; 2723 switch (iter.type()) { 2724 case relocInfo::virtual_call_type: 2725 verify_interrupt_point(iter.addr()); 2726 break; 2727 case relocInfo::opt_virtual_call_type: 2728 stub = iter.opt_virtual_call_reloc()->static_stub(); 2729 verify_interrupt_point(iter.addr()); 2730 break; 2731 case relocInfo::static_call_type: 2732 stub = iter.static_call_reloc()->static_stub(); 2733 //verify_interrupt_point(iter.addr()); 2734 break; 2735 case relocInfo::runtime_call_type: 2736 address destination = iter.reloc()->value(); 2737 // Right now there is no way to find out which entries support 2738 // an interrupt point. It would be nice if we had this 2739 // information in a table. 2740 break; 2741 } 2742 assert(stub == NULL || stub_contains(stub), "static call stub outside stub section"); 2743 } 2744 } 2745 2746 2747 // ----------------------------------------------------------------------------- 2748 // Non-product code 2749 #ifndef PRODUCT 2750 2751 class DebugScavengeRoot: public OopClosure { 2752 nmethod* _nm; 2753 bool _ok; 2754 public: 2755 DebugScavengeRoot(nmethod* nm) : _nm(nm), _ok(true) { } 2756 bool ok() { return _ok; } 2757 virtual void do_oop(oop* p) { 2758 if ((*p) == NULL || !(*p)->is_scavengable()) return; 2759 if (_ok) { 2760 _nm->print_nmethod(true); 2761 _ok = false; 2762 } 2763 tty->print_cr("*** scavengable oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", 2764 (void *)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); 2765 (*p)->print(); 2766 } 2767 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } 2768 }; 2769 2770 void nmethod::verify_scavenge_root_oops() { 2771 if (UseG1GC) { 2772 return; 2773 } 2774 2775 if (!on_scavenge_root_list()) { 2776 // Actually look inside, to verify the claim that it's clean. 2777 DebugScavengeRoot debug_scavenge_root(this); 2778 oops_do(&debug_scavenge_root); 2779 if (!debug_scavenge_root.ok()) 2780 fatal("found an unadvertised bad scavengable oop in the code cache"); 2781 } 2782 assert(scavenge_root_not_marked(), ""); 2783 } 2784 2785 #endif // PRODUCT 2786 2787 // Printing operations 2788 2789 void nmethod::print() const { 2790 ResourceMark rm; 2791 ttyLocker ttyl; // keep the following output all in one block 2792 2793 tty->print("Compiled method "); 2794 2795 if (is_compiled_by_c1()) { 2796 tty->print("(c1) "); 2797 } else if (is_compiled_by_c2()) { 2798 tty->print("(c2) "); 2799 } else if (is_compiled_by_shark()) { 2800 tty->print("(shark) "); 2801 } else { 2802 tty->print("(nm) "); 2803 } 2804 2805 print_on(tty, NULL); 2806 2807 if (WizardMode) { 2808 tty->print("((nmethod*) "INTPTR_FORMAT ") ", this); 2809 tty->print(" for method " INTPTR_FORMAT , (address)method()); 2810 tty->print(" { "); 2811 if (is_in_use()) tty->print("in_use "); 2812 if (is_not_entrant()) tty->print("not_entrant "); 2813 if (is_zombie()) tty->print("zombie "); 2814 if (is_unloaded()) tty->print("unloaded "); 2815 if (on_scavenge_root_list()) tty->print("scavenge_root "); 2816 tty->print_cr("}:"); 2817 } 2818 if (size () > 0) tty->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2819 (address)this, 2820 (address)this + size(), 2821 size()); 2822 if (relocation_size () > 0) tty->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2823 relocation_begin(), 2824 relocation_end(), 2825 relocation_size()); 2826 if (consts_size () > 0) tty->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2827 consts_begin(), 2828 consts_end(), 2829 consts_size()); 2830 if (insts_size () > 0) tty->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2831 insts_begin(), 2832 insts_end(), 2833 insts_size()); 2834 if (stub_size () > 0) tty->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2835 stub_begin(), 2836 stub_end(), 2837 stub_size()); 2838 if (oops_size () > 0) tty->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2839 oops_begin(), 2840 oops_end(), 2841 oops_size()); 2842 if (metadata_size () > 0) tty->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2843 metadata_begin(), 2844 metadata_end(), 2845 metadata_size()); 2846 if (scopes_data_size () > 0) tty->print_cr(" scopes data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2847 scopes_data_begin(), 2848 scopes_data_end(), 2849 scopes_data_size()); 2850 if (scopes_pcs_size () > 0) tty->print_cr(" scopes pcs [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2851 scopes_pcs_begin(), 2852 scopes_pcs_end(), 2853 scopes_pcs_size()); 2854 if (dependencies_size () > 0) tty->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2855 dependencies_begin(), 2856 dependencies_end(), 2857 dependencies_size()); 2858 if (handler_table_size() > 0) tty->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2859 handler_table_begin(), 2860 handler_table_end(), 2861 handler_table_size()); 2862 if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2863 nul_chk_table_begin(), 2864 nul_chk_table_end(), 2865 nul_chk_table_size()); 2866 } 2867 2868 void nmethod::print_code() { 2869 HandleMark hm; 2870 ResourceMark m; 2871 Disassembler::decode(this); 2872 } 2873 2874 2875 #ifndef PRODUCT 2876 2877 void nmethod::print_scopes() { 2878 // Find the first pc desc for all scopes in the code and print it. 2879 ResourceMark rm; 2880 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { 2881 if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null) 2882 continue; 2883 2884 ScopeDesc* sd = scope_desc_at(p->real_pc(this)); 2885 sd->print_on(tty, p); 2886 } 2887 } 2888 2889 void nmethod::print_dependencies() { 2890 ResourceMark rm; 2891 ttyLocker ttyl; // keep the following output all in one block 2892 tty->print_cr("Dependencies:"); 2893 for (Dependencies::DepStream deps(this); deps.next(); ) { 2894 deps.print_dependency(); 2895 Klass* ctxk = deps.context_type(); 2896 if (ctxk != NULL) { 2897 if (ctxk->oop_is_instance() && ((InstanceKlass*)ctxk)->is_dependent_nmethod(this)) { 2898 tty->print_cr(" [nmethod<=klass]%s", ctxk->external_name()); 2899 } 2900 } 2901 deps.log_dependency(); // put it into the xml log also 2902 } 2903 } 2904 2905 2906 void nmethod::print_relocations() { 2907 ResourceMark m; // in case methods get printed via the debugger 2908 tty->print_cr("relocations:"); 2909 RelocIterator iter(this); 2910 iter.print(); 2911 if (UseRelocIndex) { 2912 jint* index_end = (jint*)relocation_end() - 1; 2913 jint index_size = *index_end; 2914 jint* index_start = (jint*)( (address)index_end - index_size ); 2915 tty->print_cr(" index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size); 2916 if (index_size > 0) { 2917 jint* ip; 2918 for (ip = index_start; ip+2 <= index_end; ip += 2) 2919 tty->print_cr(" (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT, 2920 ip[0], 2921 ip[1], 2922 header_end()+ip[0], 2923 relocation_begin()-1+ip[1]); 2924 for (; ip < index_end; ip++) 2925 tty->print_cr(" (%d ?)", ip[0]); 2926 tty->print_cr(" @" INTPTR_FORMAT ": index_size=%d", ip, *ip); 2927 ip++; 2928 tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip); 2929 } 2930 } 2931 } 2932 2933 2934 void nmethod::print_pcs() { 2935 ResourceMark m; // in case methods get printed via debugger 2936 tty->print_cr("pc-bytecode offsets:"); 2937 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { 2938 p->print(this); 2939 } 2940 } 2941 2942 #endif // PRODUCT 2943 2944 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) { 2945 RelocIterator iter(this, begin, end); 2946 bool have_one = false; 2947 while (iter.next()) { 2948 have_one = true; 2949 switch (iter.type()) { 2950 case relocInfo::none: return "no_reloc"; 2951 case relocInfo::oop_type: { 2952 stringStream st; 2953 oop_Relocation* r = iter.oop_reloc(); 2954 oop obj = r->oop_value(); 2955 st.print("oop("); 2956 if (obj == NULL) st.print("NULL"); 2957 else obj->print_value_on(&st); 2958 st.print(")"); 2959 return st.as_string(); 2960 } 2961 case relocInfo::metadata_type: { 2962 stringStream st; 2963 metadata_Relocation* r = iter.metadata_reloc(); 2964 Metadata* obj = r->metadata_value(); 2965 st.print("metadata("); 2966 if (obj == NULL) st.print("NULL"); 2967 else obj->print_value_on(&st); 2968 st.print(")"); 2969 return st.as_string(); 2970 } 2971 case relocInfo::virtual_call_type: return "virtual_call"; 2972 case relocInfo::opt_virtual_call_type: return "optimized virtual_call"; 2973 case relocInfo::static_call_type: return "static_call"; 2974 case relocInfo::static_stub_type: return "static_stub"; 2975 case relocInfo::runtime_call_type: return "runtime_call"; 2976 case relocInfo::external_word_type: return "external_word"; 2977 case relocInfo::internal_word_type: return "internal_word"; 2978 case relocInfo::section_word_type: return "section_word"; 2979 case relocInfo::poll_type: return "poll"; 2980 case relocInfo::poll_return_type: return "poll_return"; 2981 case relocInfo::type_mask: return "type_bit_mask"; 2982 } 2983 } 2984 return have_one ? "other" : NULL; 2985 } 2986 2987 // Return a the last scope in (begin..end] 2988 ScopeDesc* nmethod::scope_desc_in(address begin, address end) { 2989 PcDesc* p = pc_desc_near(begin+1); 2990 if (p != NULL && p->real_pc(this) <= end) { 2991 return new ScopeDesc(this, p->scope_decode_offset(), 2992 p->obj_decode_offset(), p->should_reexecute(), 2993 p->return_oop()); 2994 } 2995 return NULL; 2996 } 2997 2998 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin) const { 2999 if (block_begin == entry_point()) stream->print_cr("[Entry Point]"); 3000 if (block_begin == verified_entry_point()) stream->print_cr("[Verified Entry Point]"); 3001 if (block_begin == exception_begin()) stream->print_cr("[Exception Handler]"); 3002 if (block_begin == stub_begin()) stream->print_cr("[Stub Code]"); 3003 if (block_begin == deopt_handler_begin()) stream->print_cr("[Deopt Handler Code]"); 3004 3005 if (has_method_handle_invokes()) 3006 if (block_begin == deopt_mh_handler_begin()) stream->print_cr("[Deopt MH Handler Code]"); 3007 3008 if (block_begin == consts_begin()) stream->print_cr("[Constants]"); 3009 3010 if (block_begin == entry_point()) { 3011 methodHandle m = method(); 3012 if (m.not_null()) { 3013 stream->print(" # "); 3014 m->print_value_on(stream); 3015 stream->cr(); 3016 } 3017 if (m.not_null() && !is_osr_method()) { 3018 ResourceMark rm; 3019 int sizeargs = m->size_of_parameters(); 3020 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); 3021 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); 3022 { 3023 int sig_index = 0; 3024 if (!m->is_static()) 3025 sig_bt[sig_index++] = T_OBJECT; // 'this' 3026 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) { 3027 BasicType t = ss.type(); 3028 sig_bt[sig_index++] = t; 3029 if (type2size[t] == 2) { 3030 sig_bt[sig_index++] = T_VOID; 3031 } else { 3032 assert(type2size[t] == 1, "size is 1 or 2"); 3033 } 3034 } 3035 assert(sig_index == sizeargs, ""); 3036 } 3037 const char* spname = "sp"; // make arch-specific? 3038 intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, false); 3039 int stack_slot_offset = this->frame_size() * wordSize; 3040 int tab1 = 14, tab2 = 24; 3041 int sig_index = 0; 3042 int arg_index = (m->is_static() ? 0 : -1); 3043 bool did_old_sp = false; 3044 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) { 3045 bool at_this = (arg_index == -1); 3046 bool at_old_sp = false; 3047 BasicType t = (at_this ? T_OBJECT : ss.type()); 3048 assert(t == sig_bt[sig_index], "sigs in sync"); 3049 if (at_this) 3050 stream->print(" # this: "); 3051 else 3052 stream->print(" # parm%d: ", arg_index); 3053 stream->move_to(tab1); 3054 VMReg fst = regs[sig_index].first(); 3055 VMReg snd = regs[sig_index].second(); 3056 if (fst->is_reg()) { 3057 stream->print("%s", fst->name()); 3058 if (snd->is_valid()) { 3059 stream->print(":%s", snd->name()); 3060 } 3061 } else if (fst->is_stack()) { 3062 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset; 3063 if (offset == stack_slot_offset) at_old_sp = true; 3064 stream->print("[%s+0x%x]", spname, offset); 3065 } else { 3066 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd); 3067 } 3068 stream->print(" "); 3069 stream->move_to(tab2); 3070 stream->print("= "); 3071 if (at_this) { 3072 m->method_holder()->print_value_on(stream); 3073 } else { 3074 bool did_name = false; 3075 if (!at_this && ss.is_object()) { 3076 Symbol* name = ss.as_symbol_or_null(); 3077 if (name != NULL) { 3078 name->print_value_on(stream); 3079 did_name = true; 3080 } 3081 } 3082 if (!did_name) 3083 stream->print("%s", type2name(t)); 3084 } 3085 if (at_old_sp) { 3086 stream->print(" (%s of caller)", spname); 3087 did_old_sp = true; 3088 } 3089 stream->cr(); 3090 sig_index += type2size[t]; 3091 arg_index += 1; 3092 if (!at_this) ss.next(); 3093 } 3094 if (!did_old_sp) { 3095 stream->print(" # "); 3096 stream->move_to(tab1); 3097 stream->print("[%s+0x%x]", spname, stack_slot_offset); 3098 stream->print(" (%s of caller)", spname); 3099 stream->cr(); 3100 } 3101 } 3102 } 3103 } 3104 3105 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) { 3106 // First, find an oopmap in (begin, end]. 3107 // We use the odd half-closed interval so that oop maps and scope descs 3108 // which are tied to the byte after a call are printed with the call itself. 3109 address base = code_begin(); 3110 OopMapSet* oms = oop_maps(); 3111 if (oms != NULL) { 3112 for (int i = 0, imax = oms->size(); i < imax; i++) { 3113 OopMap* om = oms->at(i); 3114 address pc = base + om->offset(); 3115 if (pc > begin) { 3116 if (pc <= end) { 3117 st->move_to(column); 3118 st->print("; "); 3119 om->print_on(st); 3120 } 3121 break; 3122 } 3123 } 3124 } 3125 3126 // Print any debug info present at this pc. 3127 ScopeDesc* sd = scope_desc_in(begin, end); 3128 if (sd != NULL) { 3129 st->move_to(column); 3130 if (sd->bci() == SynchronizationEntryBCI) { 3131 st->print(";*synchronization entry"); 3132 } else { 3133 if (sd->method() == NULL) { 3134 st->print("method is NULL"); 3135 } else if (sd->method()->is_native()) { 3136 st->print("method is native"); 3137 } else { 3138 Bytecodes::Code bc = sd->method()->java_code_at(sd->bci()); 3139 st->print(";*%s", Bytecodes::name(bc)); 3140 switch (bc) { 3141 case Bytecodes::_invokevirtual: 3142 case Bytecodes::_invokespecial: 3143 case Bytecodes::_invokestatic: 3144 case Bytecodes::_invokeinterface: 3145 { 3146 Bytecode_invoke invoke(sd->method(), sd->bci()); 3147 st->print(" "); 3148 if (invoke.name() != NULL) 3149 invoke.name()->print_symbol_on(st); 3150 else 3151 st->print("<UNKNOWN>"); 3152 break; 3153 } 3154 case Bytecodes::_getfield: 3155 case Bytecodes::_putfield: 3156 case Bytecodes::_getstatic: 3157 case Bytecodes::_putstatic: 3158 { 3159 Bytecode_field field(sd->method(), sd->bci()); 3160 st->print(" "); 3161 if (field.name() != NULL) 3162 field.name()->print_symbol_on(st); 3163 else 3164 st->print("<UNKNOWN>"); 3165 } 3166 } 3167 } 3168 } 3169 3170 // Print all scopes 3171 for (;sd != NULL; sd = sd->sender()) { 3172 st->move_to(column); 3173 st->print("; -"); 3174 if (sd->method() == NULL) { 3175 st->print("method is NULL"); 3176 } else { 3177 sd->method()->print_short_name(st); 3178 } 3179 int lineno = sd->method()->line_number_from_bci(sd->bci()); 3180 if (lineno != -1) { 3181 st->print("@%d (line %d)", sd->bci(), lineno); 3182 } else { 3183 st->print("@%d", sd->bci()); 3184 } 3185 st->cr(); 3186 } 3187 } 3188 3189 // Print relocation information 3190 const char* str = reloc_string_for(begin, end); 3191 if (str != NULL) { 3192 if (sd != NULL) st->cr(); 3193 st->move_to(column); 3194 st->print("; {%s}", str); 3195 } 3196 int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin()); 3197 if (cont_offset != 0) { 3198 st->move_to(column); 3199 st->print("; implicit exception: dispatches to " INTPTR_FORMAT, code_begin() + cont_offset); 3200 } 3201 3202 } 3203 3204 #ifndef PRODUCT 3205 3206 void nmethod::print_value_on(outputStream* st) const { 3207 st->print("nmethod"); 3208 print_on(st, NULL); 3209 } 3210 3211 void nmethod::print_calls(outputStream* st) { 3212 RelocIterator iter(this); 3213 while (iter.next()) { 3214 switch (iter.type()) { 3215 case relocInfo::virtual_call_type: 3216 case relocInfo::opt_virtual_call_type: { 3217 VerifyMutexLocker mc(CompiledIC_lock); 3218 CompiledIC_at(&iter)->print(); 3219 break; 3220 } 3221 case relocInfo::static_call_type: 3222 st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr()); 3223 compiledStaticCall_at(iter.reloc())->print(); 3224 break; 3225 } 3226 } 3227 } 3228 3229 void nmethod::print_handler_table() { 3230 ExceptionHandlerTable(this).print(); 3231 } 3232 3233 void nmethod::print_nul_chk_table() { 3234 ImplicitExceptionTable(this).print(code_begin()); 3235 } 3236 3237 void nmethod::print_statistics() { 3238 ttyLocker ttyl; 3239 if (xtty != NULL) xtty->head("statistics type='nmethod'"); 3240 nmethod_stats.print_native_nmethod_stats(); 3241 nmethod_stats.print_nmethod_stats(); 3242 DebugInformationRecorder::print_statistics(); 3243 nmethod_stats.print_pc_stats(); 3244 Dependencies::print_statistics(); 3245 if (xtty != NULL) xtty->tail("statistics"); 3246 } 3247 3248 #endif // PRODUCT