1 /* 2 * Copyright 1997-2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 # include "incls/_precompiled.incl" 26 # include "incls/_nmethod.cpp.incl" 27 28 #ifdef DTRACE_ENABLED 29 30 // Only bother with this argument setup if dtrace is available 31 32 HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load, 33 const char*, int, const char*, int, const char*, int, void*, size_t); 34 35 HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload, 36 char*, int, char*, int, char*, int); 37 38 #define DTRACE_METHOD_UNLOAD_PROBE(method) \ 39 { \ 40 methodOop m = (method); \ 41 if (m != NULL) { \ 42 symbolOop klass_name = m->klass_name(); \ 43 symbolOop name = m->name(); \ 44 symbolOop signature = m->signature(); \ 45 HS_DTRACE_PROBE6(hotspot, compiled__method__unload, \ 46 klass_name->bytes(), klass_name->utf8_length(), \ 47 name->bytes(), name->utf8_length(), \ 48 signature->bytes(), signature->utf8_length()); \ 49 } \ 50 } 51 52 #else // ndef DTRACE_ENABLED 53 54 #define DTRACE_METHOD_UNLOAD_PROBE(method) 55 56 #endif 57 58 bool nmethod::is_compiled_by_c1() const { 59 if (compiler() == NULL || method() == NULL) return false; // can happen during debug printing 60 if (is_native_method()) return false; 61 return compiler()->is_c1(); 62 } 63 bool nmethod::is_compiled_by_c2() const { 64 if (compiler() == NULL || method() == NULL) return false; // can happen during debug printing 65 if (is_native_method()) return false; 66 return compiler()->is_c2(); 67 } 68 69 70 71 //--------------------------------------------------------------------------------- 72 // NMethod statistics 73 // They are printed under various flags, including: 74 // PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation. 75 // (In the latter two cases, they like other stats are printed to the log only.) 76 77 #ifndef PRODUCT 78 // These variables are put into one block to reduce relocations 79 // and make it simpler to print from the debugger. 80 static 81 struct nmethod_stats_struct { 82 int nmethod_count; 83 int total_size; 84 int relocation_size; 85 int code_size; 86 int stub_size; 87 int consts_size; 88 int scopes_data_size; 89 int scopes_pcs_size; 90 int dependencies_size; 91 int handler_table_size; 92 int nul_chk_table_size; 93 int oops_size; 94 95 void note_nmethod(nmethod* nm) { 96 nmethod_count += 1; 97 total_size += nm->size(); 98 relocation_size += nm->relocation_size(); 99 code_size += nm->code_size(); 100 stub_size += nm->stub_size(); 101 consts_size += nm->consts_size(); 102 scopes_data_size += nm->scopes_data_size(); 103 scopes_pcs_size += nm->scopes_pcs_size(); 104 dependencies_size += nm->dependencies_size(); 105 handler_table_size += nm->handler_table_size(); 106 nul_chk_table_size += nm->nul_chk_table_size(); 107 oops_size += nm->oops_size(); 108 } 109 void print_nmethod_stats() { 110 if (nmethod_count == 0) return; 111 tty->print_cr("Statistics for %d bytecoded nmethods:", nmethod_count); 112 if (total_size != 0) tty->print_cr(" total in heap = %d", total_size); 113 if (relocation_size != 0) tty->print_cr(" relocation = %d", relocation_size); 114 if (code_size != 0) tty->print_cr(" main code = %d", code_size); 115 if (stub_size != 0) tty->print_cr(" stub code = %d", stub_size); 116 if (consts_size != 0) tty->print_cr(" constants = %d", consts_size); 117 if (scopes_data_size != 0) tty->print_cr(" scopes data = %d", scopes_data_size); 118 if (scopes_pcs_size != 0) tty->print_cr(" scopes pcs = %d", scopes_pcs_size); 119 if (dependencies_size != 0) tty->print_cr(" dependencies = %d", dependencies_size); 120 if (handler_table_size != 0) tty->print_cr(" handler table = %d", handler_table_size); 121 if (nul_chk_table_size != 0) tty->print_cr(" nul chk table = %d", nul_chk_table_size); 122 if (oops_size != 0) tty->print_cr(" oops = %d", oops_size); 123 } 124 125 int native_nmethod_count; 126 int native_total_size; 127 int native_relocation_size; 128 int native_code_size; 129 int native_oops_size; 130 void note_native_nmethod(nmethod* nm) { 131 native_nmethod_count += 1; 132 native_total_size += nm->size(); 133 native_relocation_size += nm->relocation_size(); 134 native_code_size += nm->code_size(); 135 native_oops_size += nm->oops_size(); 136 } 137 void print_native_nmethod_stats() { 138 if (native_nmethod_count == 0) return; 139 tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count); 140 if (native_total_size != 0) tty->print_cr(" N. total size = %d", native_total_size); 141 if (native_relocation_size != 0) tty->print_cr(" N. relocation = %d", native_relocation_size); 142 if (native_code_size != 0) tty->print_cr(" N. main code = %d", native_code_size); 143 if (native_oops_size != 0) tty->print_cr(" N. oops = %d", native_oops_size); 144 } 145 146 int pc_desc_resets; // number of resets (= number of caches) 147 int pc_desc_queries; // queries to nmethod::find_pc_desc 148 int pc_desc_approx; // number of those which have approximate true 149 int pc_desc_repeats; // number of _last_pc_desc hits 150 int pc_desc_hits; // number of LRU cache hits 151 int pc_desc_tests; // total number of PcDesc examinations 152 int pc_desc_searches; // total number of quasi-binary search steps 153 int pc_desc_adds; // number of LUR cache insertions 154 155 void print_pc_stats() { 156 tty->print_cr("PcDesc Statistics: %d queries, %.2f comparisons per query", 157 pc_desc_queries, 158 (double)(pc_desc_tests + pc_desc_searches) 159 / pc_desc_queries); 160 tty->print_cr(" caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d", 161 pc_desc_resets, 162 pc_desc_queries, pc_desc_approx, 163 pc_desc_repeats, pc_desc_hits, 164 pc_desc_tests, pc_desc_searches, pc_desc_adds); 165 } 166 } nmethod_stats; 167 #endif //PRODUCT 168 169 //--------------------------------------------------------------------------------- 170 171 172 // The _unwind_handler is a special marker address, which says that 173 // for given exception oop and address, the frame should be removed 174 // as the tuple cannot be caught in the nmethod 175 address ExceptionCache::_unwind_handler = (address) -1; 176 177 178 ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) { 179 assert(pc != NULL, "Must be non null"); 180 assert(exception.not_null(), "Must be non null"); 181 assert(handler != NULL, "Must be non null"); 182 183 _count = 0; 184 _exception_type = exception->klass(); 185 _next = NULL; 186 187 add_address_and_handler(pc,handler); 188 } 189 190 191 address ExceptionCache::match(Handle exception, address pc) { 192 assert(pc != NULL,"Must be non null"); 193 assert(exception.not_null(),"Must be non null"); 194 if (exception->klass() == exception_type()) { 195 return (test_address(pc)); 196 } 197 198 return NULL; 199 } 200 201 202 bool ExceptionCache::match_exception_with_space(Handle exception) { 203 assert(exception.not_null(),"Must be non null"); 204 if (exception->klass() == exception_type() && count() < cache_size) { 205 return true; 206 } 207 return false; 208 } 209 210 211 address ExceptionCache::test_address(address addr) { 212 for (int i=0; i<count(); i++) { 213 if (pc_at(i) == addr) { 214 return handler_at(i); 215 } 216 } 217 return NULL; 218 } 219 220 221 bool ExceptionCache::add_address_and_handler(address addr, address handler) { 222 if (test_address(addr) == handler) return true; 223 if (count() < cache_size) { 224 set_pc_at(count(),addr); 225 set_handler_at(count(), handler); 226 increment_count(); 227 return true; 228 } 229 return false; 230 } 231 232 233 // private method for handling exception cache 234 // These methods are private, and used to manipulate the exception cache 235 // directly. 236 ExceptionCache* nmethod::exception_cache_entry_for_exception(Handle exception) { 237 ExceptionCache* ec = exception_cache(); 238 while (ec != NULL) { 239 if (ec->match_exception_with_space(exception)) { 240 return ec; 241 } 242 ec = ec->next(); 243 } 244 return NULL; 245 } 246 247 248 //----------------------------------------------------------------------------- 249 250 251 // Helper used by both find_pc_desc methods. 252 static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) { 253 NOT_PRODUCT(++nmethod_stats.pc_desc_tests); 254 if (!approximate) 255 return pc->pc_offset() == pc_offset; 256 else 257 return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset(); 258 } 259 260 void PcDescCache::reset_to(PcDesc* initial_pc_desc) { 261 if (initial_pc_desc == NULL) { 262 _last_pc_desc = NULL; // native method 263 return; 264 } 265 NOT_PRODUCT(++nmethod_stats.pc_desc_resets); 266 // reset the cache by filling it with benign (non-null) values 267 assert(initial_pc_desc->pc_offset() < 0, "must be sentinel"); 268 _last_pc_desc = initial_pc_desc + 1; // first valid one is after sentinel 269 for (int i = 0; i < cache_size; i++) 270 _pc_descs[i] = initial_pc_desc; 271 } 272 273 PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) { 274 NOT_PRODUCT(++nmethod_stats.pc_desc_queries); 275 NOT_PRODUCT(if (approximate) ++nmethod_stats.pc_desc_approx); 276 277 // In order to prevent race conditions do not load cache elements 278 // repeatedly, but use a local copy: 279 PcDesc* res; 280 281 // Step one: Check the most recently returned value. 282 res = _last_pc_desc; 283 if (res == NULL) return NULL; // native method; no PcDescs at all 284 if (match_desc(res, pc_offset, approximate)) { 285 NOT_PRODUCT(++nmethod_stats.pc_desc_repeats); 286 return res; 287 } 288 289 // Step two: Check the LRU cache. 290 for (int i = 0; i < cache_size; i++) { 291 res = _pc_descs[i]; 292 if (res->pc_offset() < 0) break; // optimization: skip empty cache 293 if (match_desc(res, pc_offset, approximate)) { 294 NOT_PRODUCT(++nmethod_stats.pc_desc_hits); 295 _last_pc_desc = res; // record this cache hit in case of repeat 296 return res; 297 } 298 } 299 300 // Report failure. 301 return NULL; 302 } 303 304 void PcDescCache::add_pc_desc(PcDesc* pc_desc) { 305 NOT_PRODUCT(++nmethod_stats.pc_desc_adds); 306 // Update the LRU cache by shifting pc_desc forward: 307 for (int i = 0; i < cache_size; i++) { 308 PcDesc* next = _pc_descs[i]; 309 _pc_descs[i] = pc_desc; 310 pc_desc = next; 311 } 312 // Note: Do not update _last_pc_desc. It fronts for the LRU cache. 313 } 314 315 // adjust pcs_size so that it is a multiple of both oopSize and 316 // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple 317 // of oopSize, then 2*sizeof(PcDesc) is) 318 static int adjust_pcs_size(int pcs_size) { 319 int nsize = round_to(pcs_size, oopSize); 320 if ((nsize % sizeof(PcDesc)) != 0) { 321 nsize = pcs_size + sizeof(PcDesc); 322 } 323 assert((nsize % oopSize) == 0, "correct alignment"); 324 return nsize; 325 } 326 327 //----------------------------------------------------------------------------- 328 329 330 void nmethod::add_exception_cache_entry(ExceptionCache* new_entry) { 331 assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock"); 332 assert(new_entry != NULL,"Must be non null"); 333 assert(new_entry->next() == NULL, "Must be null"); 334 335 if (exception_cache() != NULL) { 336 new_entry->set_next(exception_cache()); 337 } 338 set_exception_cache(new_entry); 339 } 340 341 void nmethod::remove_from_exception_cache(ExceptionCache* ec) { 342 ExceptionCache* prev = NULL; 343 ExceptionCache* curr = exception_cache(); 344 assert(curr != NULL, "nothing to remove"); 345 // find the previous and next entry of ec 346 while (curr != ec) { 347 prev = curr; 348 curr = curr->next(); 349 assert(curr != NULL, "ExceptionCache not found"); 350 } 351 // now: curr == ec 352 ExceptionCache* next = curr->next(); 353 if (prev == NULL) { 354 set_exception_cache(next); 355 } else { 356 prev->set_next(next); 357 } 358 delete curr; 359 } 360 361 362 // public method for accessing the exception cache 363 // These are the public access methods. 364 address nmethod::handler_for_exception_and_pc(Handle exception, address pc) { 365 // We never grab a lock to read the exception cache, so we may 366 // have false negatives. This is okay, as it can only happen during 367 // the first few exception lookups for a given nmethod. 368 ExceptionCache* ec = exception_cache(); 369 while (ec != NULL) { 370 address ret_val; 371 if ((ret_val = ec->match(exception,pc)) != NULL) { 372 return ret_val; 373 } 374 ec = ec->next(); 375 } 376 return NULL; 377 } 378 379 380 void nmethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) { 381 // There are potential race conditions during exception cache updates, so we 382 // must own the ExceptionCache_lock before doing ANY modifications. Because 383 // we don't lock during reads, it is possible to have several threads attempt 384 // to update the cache with the same data. We need to check for already inserted 385 // copies of the current data before adding it. 386 387 MutexLocker ml(ExceptionCache_lock); 388 ExceptionCache* target_entry = exception_cache_entry_for_exception(exception); 389 390 if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) { 391 target_entry = new ExceptionCache(exception,pc,handler); 392 add_exception_cache_entry(target_entry); 393 } 394 } 395 396 397 //-------------end of code for ExceptionCache-------------- 398 399 400 void nmFlags::clear() { 401 assert(sizeof(nmFlags) == sizeof(int), "using more than one word for nmFlags"); 402 *(jint*)this = 0; 403 } 404 405 int nmethod::total_size() const { 406 return 407 code_size() + 408 stub_size() + 409 consts_size() + 410 scopes_data_size() + 411 scopes_pcs_size() + 412 handler_table_size() + 413 nul_chk_table_size(); 414 } 415 416 const char* nmethod::compile_kind() const { 417 if (is_osr_method()) return "osr"; 418 if (method() != NULL && is_native_method()) return "c2n"; 419 return NULL; 420 } 421 422 // %%% This variable is no longer used? 423 int nmethod::_zombie_instruction_size = NativeJump::instruction_size; 424 425 426 nmethod* nmethod::new_native_nmethod(methodHandle method, 427 CodeBuffer *code_buffer, 428 int vep_offset, 429 int frame_complete, 430 int frame_size, 431 ByteSize basic_lock_owner_sp_offset, 432 ByteSize basic_lock_sp_offset, 433 OopMapSet* oop_maps) { 434 // create nmethod 435 nmethod* nm = NULL; 436 { 437 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 438 int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); 439 CodeOffsets offsets; 440 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); 441 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); 442 nm = new (native_nmethod_size) 443 nmethod(method(), native_nmethod_size, &offsets, 444 code_buffer, frame_size, 445 basic_lock_owner_sp_offset, basic_lock_sp_offset, 446 oop_maps); 447 NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_native_nmethod(nm)); 448 if (PrintAssembly && nm != NULL) 449 Disassembler::decode(nm); 450 } 451 // verify nmethod 452 debug_only(if (nm) nm->verify();) // might block 453 454 if (nm != NULL) { 455 nm->log_new_nmethod(); 456 } 457 458 return nm; 459 } 460 461 #ifdef HAVE_DTRACE_H 462 nmethod* nmethod::new_dtrace_nmethod(methodHandle method, 463 CodeBuffer *code_buffer, 464 int vep_offset, 465 int trap_offset, 466 int frame_complete, 467 int frame_size) { 468 // create nmethod 469 nmethod* nm = NULL; 470 { 471 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 472 int nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); 473 CodeOffsets offsets; 474 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); 475 offsets.set_value(CodeOffsets::Dtrace_trap, trap_offset); 476 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); 477 478 nm = new (nmethod_size) nmethod(method(), nmethod_size, &offsets, code_buffer, frame_size); 479 480 NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_nmethod(nm)); 481 if (PrintAssembly && nm != NULL) 482 Disassembler::decode(nm); 483 } 484 // verify nmethod 485 debug_only(if (nm) nm->verify();) // might block 486 487 if (nm != NULL) { 488 nm->log_new_nmethod(); 489 } 490 491 return nm; 492 } 493 494 #endif // def HAVE_DTRACE_H 495 496 nmethod* nmethod::new_nmethod(methodHandle method, 497 int compile_id, 498 int entry_bci, 499 CodeOffsets* offsets, 500 int orig_pc_offset, 501 DebugInformationRecorder* debug_info, 502 Dependencies* dependencies, 503 CodeBuffer* code_buffer, int frame_size, 504 OopMapSet* oop_maps, 505 ExceptionHandlerTable* handler_table, 506 ImplicitExceptionTable* nul_chk_table, 507 AbstractCompiler* compiler, 508 int comp_level 509 ) 510 { 511 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); 512 // create nmethod 513 nmethod* nm = NULL; 514 { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 515 int nmethod_size = 516 allocation_size(code_buffer, sizeof(nmethod)) 517 + adjust_pcs_size(debug_info->pcs_size()) 518 + round_to(dependencies->size_in_bytes() , oopSize) 519 + round_to(handler_table->size_in_bytes(), oopSize) 520 + round_to(nul_chk_table->size_in_bytes(), oopSize) 521 + round_to(debug_info->data_size() , oopSize); 522 nm = new (nmethod_size) 523 nmethod(method(), nmethod_size, compile_id, entry_bci, offsets, 524 orig_pc_offset, debug_info, dependencies, code_buffer, frame_size, 525 oop_maps, 526 handler_table, 527 nul_chk_table, 528 compiler, 529 comp_level); 530 if (nm != NULL) { 531 // To make dependency checking during class loading fast, record 532 // the nmethod dependencies in the classes it is dependent on. 533 // This allows the dependency checking code to simply walk the 534 // class hierarchy above the loaded class, checking only nmethods 535 // which are dependent on those classes. The slow way is to 536 // check every nmethod for dependencies which makes it linear in 537 // the number of methods compiled. For applications with a lot 538 // classes the slow way is too slow. 539 for (Dependencies::DepStream deps(nm); deps.next(); ) { 540 klassOop klass = deps.context_type(); 541 if (klass == NULL) continue; // ignore things like evol_method 542 543 // record this nmethod as dependent on this klass 544 instanceKlass::cast(klass)->add_dependent_nmethod(nm); 545 } 546 } 547 NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_nmethod(nm)); 548 if (PrintAssembly && nm != NULL) 549 Disassembler::decode(nm); 550 } 551 552 // verify nmethod 553 debug_only(if (nm) nm->verify();) // might block 554 555 if (nm != NULL) { 556 nm->log_new_nmethod(); 557 } 558 559 // done 560 return nm; 561 } 562 563 564 // For native wrappers 565 nmethod::nmethod( 566 methodOop method, 567 int nmethod_size, 568 CodeOffsets* offsets, 569 CodeBuffer* code_buffer, 570 int frame_size, 571 ByteSize basic_lock_owner_sp_offset, 572 ByteSize basic_lock_sp_offset, 573 OopMapSet* oop_maps ) 574 : CodeBlob("native nmethod", code_buffer, sizeof(nmethod), 575 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), 576 _compiled_synchronized_native_basic_lock_owner_sp_offset(basic_lock_owner_sp_offset), 577 _compiled_synchronized_native_basic_lock_sp_offset(basic_lock_sp_offset) 578 { 579 { 580 debug_only(No_Safepoint_Verifier nsv;) 581 assert_locked_or_safepoint(CodeCache_lock); 582 583 NOT_PRODUCT(_has_debug_info = false); 584 _oops_do_mark_link = NULL; 585 _method = method; 586 _entry_bci = InvocationEntryBci; 587 _osr_link = NULL; 588 _scavenge_root_link = NULL; 589 _scavenge_root_state = 0; 590 _saved_nmethod_link = NULL; 591 _compiler = NULL; 592 // We have no exception handler or deopt handler make the 593 // values something that will never match a pc like the nmethod vtable entry 594 _exception_offset = 0; 595 _deoptimize_offset = 0; 596 _deoptimize_mh_offset = 0; 597 _orig_pc_offset = 0; 598 #ifdef HAVE_DTRACE_H 599 _trap_offset = 0; 600 #endif // def HAVE_DTRACE_H 601 _stub_offset = data_offset(); 602 _consts_offset = data_offset(); 603 _scopes_data_offset = data_offset(); 604 _scopes_pcs_offset = _scopes_data_offset; 605 _dependencies_offset = _scopes_pcs_offset; 606 _handler_table_offset = _dependencies_offset; 607 _nul_chk_table_offset = _handler_table_offset; 608 _nmethod_end_offset = _nul_chk_table_offset; 609 _compile_id = 0; // default 610 _comp_level = CompLevel_none; 611 _entry_point = instructions_begin(); 612 _verified_entry_point = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry); 613 _osr_entry_point = NULL; 614 _exception_cache = NULL; 615 _pc_desc_cache.reset_to(NULL); 616 617 flags.clear(); 618 flags.state = alive; 619 _markedForDeoptimization = 0; 620 621 _lock_count = 0; 622 _stack_traversal_mark = 0; 623 624 code_buffer->copy_oops_to(this); 625 debug_only(verify_scavenge_root_oops()); 626 CodeCache::commit(this); 627 VTune::create_nmethod(this); 628 } 629 630 if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { 631 ttyLocker ttyl; // keep the following output all in one block 632 // This output goes directly to the tty, not the compiler log. 633 // To enable tools to match it up with the compilation activity, 634 // be sure to tag this tty output with the compile ID. 635 if (xtty != NULL) { 636 xtty->begin_head("print_native_nmethod"); 637 xtty->method(_method); 638 xtty->stamp(); 639 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); 640 } 641 // print the header part first 642 print(); 643 // then print the requested information 644 if (PrintNativeNMethods) { 645 print_code(); 646 oop_maps->print(); 647 } 648 if (PrintRelocations) { 649 print_relocations(); 650 } 651 if (xtty != NULL) { 652 xtty->tail("print_native_nmethod"); 653 } 654 } 655 Events::log("Create nmethod " INTPTR_FORMAT, this); 656 } 657 658 // For dtrace wrappers 659 #ifdef HAVE_DTRACE_H 660 nmethod::nmethod( 661 methodOop method, 662 int nmethod_size, 663 CodeOffsets* offsets, 664 CodeBuffer* code_buffer, 665 int frame_size) 666 : CodeBlob("dtrace nmethod", code_buffer, sizeof(nmethod), 667 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, NULL), 668 _compiled_synchronized_native_basic_lock_owner_sp_offset(in_ByteSize(-1)), 669 _compiled_synchronized_native_basic_lock_sp_offset(in_ByteSize(-1)) 670 { 671 { 672 debug_only(No_Safepoint_Verifier nsv;) 673 assert_locked_or_safepoint(CodeCache_lock); 674 675 NOT_PRODUCT(_has_debug_info = false); 676 _oops_do_mark_link = NULL; 677 _method = method; 678 _entry_bci = InvocationEntryBci; 679 _osr_link = NULL; 680 _scavenge_root_link = NULL; 681 _scavenge_root_state = 0; 682 _compiler = NULL; 683 // We have no exception handler or deopt handler make the 684 // values something that will never match a pc like the nmethod vtable entry 685 _exception_offset = 0; 686 _deoptimize_offset = 0; 687 _deoptimize_mh_offset = 0; 688 _unwind_handler_offset = -1; 689 _trap_offset = offsets->value(CodeOffsets::Dtrace_trap); 690 _orig_pc_offset = 0; 691 _stub_offset = data_offset(); 692 _consts_offset = data_offset(); 693 _scopes_data_offset = data_offset(); 694 _scopes_pcs_offset = _scopes_data_offset; 695 _dependencies_offset = _scopes_pcs_offset; 696 _handler_table_offset = _dependencies_offset; 697 _nul_chk_table_offset = _handler_table_offset; 698 _nmethod_end_offset = _nul_chk_table_offset; 699 _compile_id = 0; // default 700 _comp_level = CompLevel_none; 701 _entry_point = instructions_begin(); 702 _verified_entry_point = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry); 703 _osr_entry_point = NULL; 704 _exception_cache = NULL; 705 _pc_desc_cache.reset_to(NULL); 706 707 flags.clear(); 708 flags.state = alive; 709 _markedForDeoptimization = 0; 710 711 _lock_count = 0; 712 _stack_traversal_mark = 0; 713 714 code_buffer->copy_oops_to(this); 715 debug_only(verify_scavenge_root_oops()); 716 CodeCache::commit(this); 717 VTune::create_nmethod(this); 718 } 719 720 if (PrintNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { 721 ttyLocker ttyl; // keep the following output all in one block 722 // This output goes directly to the tty, not the compiler log. 723 // To enable tools to match it up with the compilation activity, 724 // be sure to tag this tty output with the compile ID. 725 if (xtty != NULL) { 726 xtty->begin_head("print_dtrace_nmethod"); 727 xtty->method(_method); 728 xtty->stamp(); 729 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); 730 } 731 // print the header part first 732 print(); 733 // then print the requested information 734 if (PrintNMethods) { 735 print_code(); 736 } 737 if (PrintRelocations) { 738 print_relocations(); 739 } 740 if (xtty != NULL) { 741 xtty->tail("print_dtrace_nmethod"); 742 } 743 } 744 Events::log("Create nmethod " INTPTR_FORMAT, this); 745 } 746 #endif // def HAVE_DTRACE_H 747 748 void* nmethod::operator new(size_t size, int nmethod_size) { 749 // Always leave some room in the CodeCache for I2C/C2I adapters 750 if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) return NULL; 751 return CodeCache::allocate(nmethod_size); 752 } 753 754 755 nmethod::nmethod( 756 methodOop method, 757 int nmethod_size, 758 int compile_id, 759 int entry_bci, 760 CodeOffsets* offsets, 761 int orig_pc_offset, 762 DebugInformationRecorder* debug_info, 763 Dependencies* dependencies, 764 CodeBuffer *code_buffer, 765 int frame_size, 766 OopMapSet* oop_maps, 767 ExceptionHandlerTable* handler_table, 768 ImplicitExceptionTable* nul_chk_table, 769 AbstractCompiler* compiler, 770 int comp_level 771 ) 772 : CodeBlob("nmethod", code_buffer, sizeof(nmethod), 773 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), 774 _compiled_synchronized_native_basic_lock_owner_sp_offset(in_ByteSize(-1)), 775 _compiled_synchronized_native_basic_lock_sp_offset(in_ByteSize(-1)) 776 { 777 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); 778 { 779 debug_only(No_Safepoint_Verifier nsv;) 780 assert_locked_or_safepoint(CodeCache_lock); 781 782 NOT_PRODUCT(_has_debug_info = false); 783 _oops_do_mark_link = NULL; 784 _method = method; 785 _compile_id = compile_id; 786 _comp_level = comp_level; 787 _entry_bci = entry_bci; 788 _osr_link = NULL; 789 _scavenge_root_link = NULL; 790 _scavenge_root_state = 0; 791 _compiler = compiler; 792 _orig_pc_offset = orig_pc_offset; 793 #ifdef HAVE_DTRACE_H 794 _trap_offset = 0; 795 #endif // def HAVE_DTRACE_H 796 _stub_offset = instructions_offset() + code_buffer->total_offset_of(code_buffer->stubs()->start()); 797 798 // Exception handler and deopt handler are in the stub section 799 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions); 800 _deoptimize_offset = _stub_offset + offsets->value(CodeOffsets::Deopt); 801 _deoptimize_mh_offset = _stub_offset + offsets->value(CodeOffsets::DeoptMH); 802 if (offsets->value(CodeOffsets::UnwindHandler) != -1) { 803 _unwind_handler_offset = instructions_offset() + offsets->value(CodeOffsets::UnwindHandler); 804 } else { 805 _unwind_handler_offset = -1; 806 } 807 _consts_offset = instructions_offset() + code_buffer->total_offset_of(code_buffer->consts()->start()); 808 _scopes_data_offset = data_offset(); 809 _scopes_pcs_offset = _scopes_data_offset + round_to(debug_info->data_size (), oopSize); 810 _dependencies_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size()); 811 _handler_table_offset = _dependencies_offset + round_to(dependencies->size_in_bytes (), oopSize); 812 _nul_chk_table_offset = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize); 813 _nmethod_end_offset = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize); 814 815 _entry_point = instructions_begin(); 816 _verified_entry_point = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry); 817 _osr_entry_point = instructions_begin() + offsets->value(CodeOffsets::OSR_Entry); 818 _exception_cache = NULL; 819 _pc_desc_cache.reset_to(scopes_pcs_begin()); 820 821 flags.clear(); 822 flags.state = alive; 823 _markedForDeoptimization = 0; 824 825 _unload_reported = false; // jvmti state 826 827 _lock_count = 0; 828 _stack_traversal_mark = 0; 829 830 // Copy contents of ScopeDescRecorder to nmethod 831 code_buffer->copy_oops_to(this); 832 debug_info->copy_to(this); 833 dependencies->copy_to(this); 834 if (ScavengeRootsInCode && detect_scavenge_root_oops()) { 835 CodeCache::add_scavenge_root_nmethod(this); 836 } 837 debug_only(verify_scavenge_root_oops()); 838 839 CodeCache::commit(this); 840 841 VTune::create_nmethod(this); 842 843 // Copy contents of ExceptionHandlerTable to nmethod 844 handler_table->copy_to(this); 845 nul_chk_table->copy_to(this); 846 847 // we use the information of entry points to find out if a method is 848 // static or non static 849 assert(compiler->is_c2() || 850 _method->is_static() == (entry_point() == _verified_entry_point), 851 " entry points must be same for static methods and vice versa"); 852 } 853 854 bool printnmethods = PrintNMethods 855 || CompilerOracle::should_print(_method) 856 || CompilerOracle::has_option_string(_method, "PrintNMethods"); 857 if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) { 858 print_nmethod(printnmethods); 859 } 860 861 // Note: Do not verify in here as the CodeCache_lock is 862 // taken which would conflict with the CompiledIC_lock 863 // which taken during the verification of call sites. 864 // (was bug - gri 10/25/99) 865 866 Events::log("Create nmethod " INTPTR_FORMAT, this); 867 } 868 869 870 // Print a short set of xml attributes to identify this nmethod. The 871 // output should be embedded in some other element. 872 void nmethod::log_identity(xmlStream* log) const { 873 log->print(" compile_id='%d'", compile_id()); 874 const char* nm_kind = compile_kind(); 875 if (nm_kind != NULL) log->print(" compile_kind='%s'", nm_kind); 876 if (compiler() != NULL) { 877 log->print(" compiler='%s'", compiler()->name()); 878 } 879 #ifdef TIERED 880 log->print(" level='%d'", comp_level()); 881 #endif // TIERED 882 } 883 884 885 #define LOG_OFFSET(log, name) \ 886 if ((intptr_t)name##_end() - (intptr_t)name##_begin()) \ 887 log->print(" " XSTR(name) "_offset='%d'" , \ 888 (intptr_t)name##_begin() - (intptr_t)this) 889 890 891 void nmethod::log_new_nmethod() const { 892 if (LogCompilation && xtty != NULL) { 893 ttyLocker ttyl; 894 HandleMark hm; 895 xtty->begin_elem("nmethod"); 896 log_identity(xtty); 897 xtty->print(" entry='" INTPTR_FORMAT "' size='%d'", 898 instructions_begin(), size()); 899 xtty->print(" address='" INTPTR_FORMAT "'", (intptr_t) this); 900 901 LOG_OFFSET(xtty, relocation); 902 LOG_OFFSET(xtty, code); 903 LOG_OFFSET(xtty, stub); 904 LOG_OFFSET(xtty, consts); 905 LOG_OFFSET(xtty, scopes_data); 906 LOG_OFFSET(xtty, scopes_pcs); 907 LOG_OFFSET(xtty, dependencies); 908 LOG_OFFSET(xtty, handler_table); 909 LOG_OFFSET(xtty, nul_chk_table); 910 LOG_OFFSET(xtty, oops); 911 912 xtty->method(method()); 913 xtty->stamp(); 914 xtty->end_elem(); 915 } 916 } 917 918 #undef LOG_OFFSET 919 920 921 // Print out more verbose output usually for a newly created nmethod. 922 void nmethod::print_on(outputStream* st, const char* title) const { 923 if (st != NULL) { 924 ttyLocker ttyl; 925 // Print a little tag line that looks like +PrintCompilation output: 926 int tlen = (int) strlen(title); 927 bool do_nl = false; 928 if (tlen > 0 && title[tlen-1] == '\n') { tlen--; do_nl = true; } 929 st->print("%3d%c %.*s", 930 compile_id(), 931 is_osr_method() ? '%' : 932 method() != NULL && 933 is_native_method() ? 'n' : ' ', 934 tlen, title); 935 #ifdef TIERED 936 st->print(" (%d) ", comp_level()); 937 #endif // TIERED 938 if (WizardMode) st->print(" (" INTPTR_FORMAT ")", this); 939 if (Universe::heap()->is_gc_active() && method() != NULL) { 940 st->print("(method)"); 941 } else if (method() != NULL) { 942 method()->print_short_name(st); 943 if (is_osr_method()) 944 st->print(" @ %d", osr_entry_bci()); 945 if (method()->code_size() > 0) 946 st->print(" (%d bytes)", method()->code_size()); 947 } 948 949 if (do_nl) st->cr(); 950 } 951 } 952 953 954 void nmethod::print_nmethod(bool printmethod) { 955 ttyLocker ttyl; // keep the following output all in one block 956 if (xtty != NULL) { 957 xtty->begin_head("print_nmethod"); 958 xtty->stamp(); 959 xtty->end_head(); 960 } 961 // print the header part first 962 print(); 963 // then print the requested information 964 if (printmethod) { 965 print_code(); 966 print_pcs(); 967 oop_maps()->print(); 968 } 969 if (PrintDebugInfo) { 970 print_scopes(); 971 } 972 if (PrintRelocations) { 973 print_relocations(); 974 } 975 if (PrintDependencies) { 976 print_dependencies(); 977 } 978 if (PrintExceptionHandlers) { 979 print_handler_table(); 980 print_nul_chk_table(); 981 } 982 if (xtty != NULL) { 983 xtty->tail("print_nmethod"); 984 } 985 } 986 987 988 void nmethod::set_version(int v) { 989 flags.version = v; 990 } 991 992 993 ScopeDesc* nmethod::scope_desc_at(address pc) { 994 PcDesc* pd = pc_desc_at(pc); 995 guarantee(pd != NULL, "scope must be present"); 996 return new ScopeDesc(this, pd->scope_decode_offset(), 997 pd->obj_decode_offset(), pd->should_reexecute(), 998 pd->return_oop()); 999 } 1000 1001 1002 void nmethod::clear_inline_caches() { 1003 assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint"); 1004 if (is_zombie()) { 1005 return; 1006 } 1007 1008 RelocIterator iter(this); 1009 while (iter.next()) { 1010 iter.reloc()->clear_inline_cache(); 1011 } 1012 } 1013 1014 1015 void nmethod::cleanup_inline_caches() { 1016 1017 assert_locked_or_safepoint(CompiledIC_lock); 1018 1019 // If the method is not entrant or zombie then a JMP is plastered over the 1020 // first few bytes. If an oop in the old code was there, that oop 1021 // should not get GC'd. Skip the first few bytes of oops on 1022 // not-entrant methods. 1023 address low_boundary = verified_entry_point(); 1024 if (!is_in_use()) { 1025 low_boundary += NativeJump::instruction_size; 1026 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1027 // This means that the low_boundary is going to be a little too high. 1028 // This shouldn't matter, since oops of non-entrant methods are never used. 1029 // In fact, why are we bothering to look at oops in a non-entrant method?? 1030 } 1031 1032 // Find all calls in an nmethod, and clear the ones that points to zombie methods 1033 ResourceMark rm; 1034 RelocIterator iter(this, low_boundary); 1035 while(iter.next()) { 1036 switch(iter.type()) { 1037 case relocInfo::virtual_call_type: 1038 case relocInfo::opt_virtual_call_type: { 1039 CompiledIC *ic = CompiledIC_at(iter.reloc()); 1040 // Ok, to lookup references to zombies here 1041 CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination()); 1042 if( cb != NULL && cb->is_nmethod() ) { 1043 nmethod* nm = (nmethod*)cb; 1044 // Clean inline caches pointing to both zombie and not_entrant methods 1045 if (!nm->is_in_use() || (nm->method()->code() != nm)) ic->set_to_clean(); 1046 } 1047 break; 1048 } 1049 case relocInfo::static_call_type: { 1050 CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc()); 1051 CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination()); 1052 if( cb != NULL && cb->is_nmethod() ) { 1053 nmethod* nm = (nmethod*)cb; 1054 // Clean inline caches pointing to both zombie and not_entrant methods 1055 if (!nm->is_in_use() || (nm->method()->code() != nm)) csc->set_to_clean(); 1056 } 1057 break; 1058 } 1059 } 1060 } 1061 } 1062 1063 // This is a private interface with the sweeper. 1064 void nmethod::mark_as_seen_on_stack() { 1065 assert(is_not_entrant(), "must be a non-entrant method"); 1066 set_stack_traversal_mark(NMethodSweeper::traversal_count()); 1067 } 1068 1069 // Tell if a non-entrant method can be converted to a zombie (i.e., there is no activations on the stack) 1070 bool nmethod::can_not_entrant_be_converted() { 1071 assert(is_not_entrant(), "must be a non-entrant method"); 1072 1073 // Since the nmethod sweeper only does partial sweep the sweeper's traversal 1074 // count can be greater than the stack traversal count before it hits the 1075 // nmethod for the second time. 1076 return stack_traversal_mark()+1 < NMethodSweeper::traversal_count(); 1077 } 1078 1079 void nmethod::inc_decompile_count() { 1080 // Could be gated by ProfileTraps, but do not bother... 1081 methodOop m = method(); 1082 if (m == NULL) return; 1083 methodDataOop mdo = m->method_data(); 1084 if (mdo == NULL) return; 1085 // There is a benign race here. See comments in methodDataOop.hpp. 1086 mdo->inc_decompile_count(); 1087 } 1088 1089 void nmethod::make_unloaded(BoolObjectClosure* is_alive, oop cause) { 1090 1091 post_compiled_method_unload(); 1092 1093 // Since this nmethod is being unloaded, make sure that dependencies 1094 // recorded in instanceKlasses get flushed and pass non-NULL closure to 1095 // indicate that this work is being done during a GC. 1096 assert(Universe::heap()->is_gc_active(), "should only be called during gc"); 1097 assert(is_alive != NULL, "Should be non-NULL"); 1098 // A non-NULL is_alive closure indicates that this is being called during GC. 1099 flush_dependencies(is_alive); 1100 1101 // Break cycle between nmethod & method 1102 if (TraceClassUnloading && WizardMode) { 1103 tty->print_cr("[Class unloading: Making nmethod " INTPTR_FORMAT 1104 " unloadable], methodOop(" INTPTR_FORMAT 1105 "), cause(" INTPTR_FORMAT ")", 1106 this, (address)_method, (address)cause); 1107 if (!Universe::heap()->is_gc_active()) 1108 cause->klass()->print(); 1109 } 1110 // Unlink the osr method, so we do not look this up again 1111 if (is_osr_method()) { 1112 invalidate_osr_method(); 1113 } 1114 // If _method is already NULL the methodOop is about to be unloaded, 1115 // so we don't have to break the cycle. Note that it is possible to 1116 // have the methodOop live here, in case we unload the nmethod because 1117 // it is pointing to some oop (other than the methodOop) being unloaded. 1118 if (_method != NULL) { 1119 // OSR methods point to the methodOop, but the methodOop does not 1120 // point back! 1121 if (_method->code() == this) { 1122 _method->clear_code(); // Break a cycle 1123 } 1124 _method = NULL; // Clear the method of this dead nmethod 1125 } 1126 // Make the class unloaded - i.e., change state and notify sweeper 1127 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1128 if (is_in_use()) { 1129 // Transitioning directly from live to unloaded -- so 1130 // we need to force a cache clean-up; remember this 1131 // for later on. 1132 CodeCache::set_needs_cache_clean(true); 1133 } 1134 flags.state = unloaded; 1135 1136 // Log the unloading. 1137 log_state_change(); 1138 1139 // The methodOop is gone at this point 1140 assert(_method == NULL, "Tautology"); 1141 1142 set_osr_link(NULL); 1143 //set_scavenge_root_link(NULL); // done by prune_scavenge_root_nmethods 1144 NMethodSweeper::notify(this); 1145 } 1146 1147 void nmethod::invalidate_osr_method() { 1148 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod"); 1149 // Remove from list of active nmethods 1150 if (method() != NULL) 1151 instanceKlass::cast(method()->method_holder())->remove_osr_nmethod(this); 1152 // Set entry as invalid 1153 _entry_bci = InvalidOSREntryBci; 1154 } 1155 1156 void nmethod::log_state_change() const { 1157 if (LogCompilation) { 1158 if (xtty != NULL) { 1159 ttyLocker ttyl; // keep the following output all in one block 1160 if (flags.state == unloaded) { 1161 xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'", 1162 os::current_thread_id()); 1163 } else { 1164 xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s", 1165 os::current_thread_id(), 1166 (flags.state == zombie ? " zombie='1'" : "")); 1167 } 1168 log_identity(xtty); 1169 xtty->stamp(); 1170 xtty->end_elem(); 1171 } 1172 } 1173 if (PrintCompilation && flags.state != unloaded) { 1174 print_on(tty, flags.state == zombie ? "made zombie " : "made not entrant "); 1175 tty->cr(); 1176 } 1177 } 1178 1179 // Common functionality for both make_not_entrant and make_zombie 1180 bool nmethod::make_not_entrant_or_zombie(unsigned int state) { 1181 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant"); 1182 1183 bool was_alive = false; 1184 1185 // Make sure the nmethod is not flushed in case of a safepoint in code below. 1186 nmethodLocker nml(this); 1187 1188 { 1189 // If the method is already zombie there is nothing to do 1190 if (is_zombie()) { 1191 return false; 1192 } 1193 1194 // invalidate osr nmethod before acquiring the patching lock since 1195 // they both acquire leaf locks and we don't want a deadlock. 1196 // This logic is equivalent to the logic below for patching the 1197 // verified entry point of regular methods. 1198 if (is_osr_method()) { 1199 // this effectively makes the osr nmethod not entrant 1200 invalidate_osr_method(); 1201 } 1202 1203 // Enter critical section. Does not block for safepoint. 1204 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag); 1205 1206 if (flags.state == state) { 1207 // another thread already performed this transition so nothing 1208 // to do, but return false to indicate this. 1209 return false; 1210 } 1211 1212 // The caller can be calling the method statically or through an inline 1213 // cache call. 1214 if (!is_osr_method() && !is_not_entrant()) { 1215 NativeJump::patch_verified_entry(entry_point(), verified_entry_point(), 1216 SharedRuntime::get_handle_wrong_method_stub()); 1217 assert (NativeJump::instruction_size == nmethod::_zombie_instruction_size, ""); 1218 } 1219 1220 was_alive = is_in_use(); // Read state under lock 1221 1222 // Change state 1223 flags.state = state; 1224 1225 // Log the transition once 1226 log_state_change(); 1227 1228 } // leave critical region under Patching_lock 1229 1230 // When the nmethod becomes zombie it is no longer alive so the 1231 // dependencies must be flushed. nmethods in the not_entrant 1232 // state will be flushed later when the transition to zombie 1233 // happens or they get unloaded. 1234 if (state == zombie) { 1235 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1236 flush_dependencies(NULL); 1237 } else { 1238 assert(state == not_entrant, "other cases may need to be handled differently"); 1239 } 1240 1241 if (state == not_entrant) { 1242 Events::log("Make nmethod not entrant " INTPTR_FORMAT, this); 1243 } else { 1244 Events::log("Make nmethod zombie " INTPTR_FORMAT, this); 1245 } 1246 1247 if (TraceCreateZombies) { 1248 tty->print_cr("nmethod <" INTPTR_FORMAT "> code made %s", this, (state == not_entrant) ? "not entrant" : "zombie"); 1249 } 1250 1251 // Make sweeper aware that there is a zombie method that needs to be removed 1252 NMethodSweeper::notify(this); 1253 1254 // not_entrant only stuff 1255 if (state == not_entrant) { 1256 mark_as_seen_on_stack(); 1257 } 1258 1259 if (was_alive) { 1260 // It's a true state change, so mark the method as decompiled. 1261 // Do it only for transition from alive. 1262 inc_decompile_count(); 1263 } 1264 1265 // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload event 1266 // and it hasn't already been reported for this nmethod then report it now. 1267 // (the event may have been reported earilier if the GC marked it for unloading). 1268 if (state == zombie) { 1269 1270 DTRACE_METHOD_UNLOAD_PROBE(method()); 1271 1272 if (JvmtiExport::should_post_compiled_method_unload() && 1273 !unload_reported()) { 1274 assert(method() != NULL, "checking"); 1275 { 1276 HandleMark hm; 1277 JvmtiExport::post_compiled_method_unload_at_safepoint( 1278 method()->jmethod_id(), code_begin()); 1279 } 1280 set_unload_reported(); 1281 } 1282 } 1283 1284 1285 // Zombie only stuff 1286 if (state == zombie) { 1287 VTune::delete_nmethod(this); 1288 } 1289 1290 // Check whether method got unloaded at a safepoint before this, 1291 // if so we can skip the flushing steps below 1292 if (method() == NULL) return true; 1293 1294 // Remove nmethod from method. 1295 // We need to check if both the _code and _from_compiled_code_entry_point 1296 // refer to this nmethod because there is a race in setting these two fields 1297 // in methodOop as seen in bugid 4947125. 1298 // If the vep() points to the zombie nmethod, the memory for the nmethod 1299 // could be flushed and the compiler and vtable stubs could still call 1300 // through it. 1301 if (method()->code() == this || 1302 method()->from_compiled_entry() == verified_entry_point()) { 1303 HandleMark hm; 1304 method()->clear_code(); 1305 } 1306 1307 return true; 1308 } 1309 1310 void nmethod::flush() { 1311 // Note that there are no valid oops in the nmethod anymore. 1312 assert(is_zombie() || (is_osr_method() && is_unloaded()), "must be a zombie method"); 1313 assert(is_marked_for_reclamation() || (is_osr_method() && is_unloaded()), "must be marked for reclamation"); 1314 1315 assert (!is_locked_by_vm(), "locked methods shouldn't be flushed"); 1316 assert_locked_or_safepoint(CodeCache_lock); 1317 1318 // completely deallocate this method 1319 EventMark m("flushing nmethod " INTPTR_FORMAT " %s", this, ""); 1320 if (PrintMethodFlushing) { 1321 tty->print_cr("*flushing nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT "/Free CodeCache:" SIZE_FORMAT "Kb", 1322 _compile_id, this, CodeCache::nof_blobs(), CodeCache::unallocated_capacity()/1024); 1323 } 1324 1325 // We need to deallocate any ExceptionCache data. 1326 // Note that we do not need to grab the nmethod lock for this, it 1327 // better be thread safe if we're disposing of it! 1328 ExceptionCache* ec = exception_cache(); 1329 set_exception_cache(NULL); 1330 while(ec != NULL) { 1331 ExceptionCache* next = ec->next(); 1332 delete ec; 1333 ec = next; 1334 } 1335 1336 if (on_scavenge_root_list()) { 1337 CodeCache::drop_scavenge_root_nmethod(this); 1338 } 1339 1340 if (is_speculatively_disconnected()) { 1341 CodeCache::remove_saved_code(this); 1342 } 1343 1344 ((CodeBlob*)(this))->flush(); 1345 1346 CodeCache::free(this); 1347 } 1348 1349 1350 // 1351 // Notify all classes this nmethod is dependent on that it is no 1352 // longer dependent. This should only be called in two situations. 1353 // First, when a nmethod transitions to a zombie all dependents need 1354 // to be clear. Since zombification happens at a safepoint there's no 1355 // synchronization issues. The second place is a little more tricky. 1356 // During phase 1 of mark sweep class unloading may happen and as a 1357 // result some nmethods may get unloaded. In this case the flushing 1358 // of dependencies must happen during phase 1 since after GC any 1359 // dependencies in the unloaded nmethod won't be updated, so 1360 // traversing the dependency information in unsafe. In that case this 1361 // function is called with a non-NULL argument and this function only 1362 // notifies instanceKlasses that are reachable 1363 1364 void nmethod::flush_dependencies(BoolObjectClosure* is_alive) { 1365 assert_locked_or_safepoint(CodeCache_lock); 1366 assert(Universe::heap()->is_gc_active() == (is_alive != NULL), 1367 "is_alive is non-NULL if and only if we are called during GC"); 1368 if (!has_flushed_dependencies()) { 1369 set_has_flushed_dependencies(); 1370 for (Dependencies::DepStream deps(this); deps.next(); ) { 1371 klassOop klass = deps.context_type(); 1372 if (klass == NULL) continue; // ignore things like evol_method 1373 1374 // During GC the is_alive closure is non-NULL, and is used to 1375 // determine liveness of dependees that need to be updated. 1376 if (is_alive == NULL || is_alive->do_object_b(klass)) { 1377 instanceKlass::cast(klass)->remove_dependent_nmethod(this); 1378 } 1379 } 1380 } 1381 } 1382 1383 1384 // If this oop is not live, the nmethod can be unloaded. 1385 bool nmethod::can_unload(BoolObjectClosure* is_alive, 1386 OopClosure* keep_alive, 1387 oop* root, bool unloading_occurred) { 1388 assert(root != NULL, "just checking"); 1389 oop obj = *root; 1390 if (obj == NULL || is_alive->do_object_b(obj)) { 1391 return false; 1392 } 1393 if (obj->is_compiledICHolder()) { 1394 compiledICHolderOop cichk_oop = compiledICHolderOop(obj); 1395 if (is_alive->do_object_b( 1396 cichk_oop->holder_method()->method_holder()) && 1397 is_alive->do_object_b(cichk_oop->holder_klass())) { 1398 // The oop should be kept alive 1399 keep_alive->do_oop(root); 1400 return false; 1401 } 1402 } 1403 // If ScavengeRootsInCode is true, an nmethod might be unloaded 1404 // simply because one of its constant oops has gone dead. 1405 // No actual classes need to be unloaded in order for this to occur. 1406 assert(unloading_occurred || ScavengeRootsInCode, "Inconsistency in unloading"); 1407 make_unloaded(is_alive, obj); 1408 return true; 1409 } 1410 1411 // ------------------------------------------------------------------ 1412 // post_compiled_method_load_event 1413 // new method for install_code() path 1414 // Transfer information from compilation to jvmti 1415 void nmethod::post_compiled_method_load_event() { 1416 1417 methodOop moop = method(); 1418 HS_DTRACE_PROBE8(hotspot, compiled__method__load, 1419 moop->klass_name()->bytes(), 1420 moop->klass_name()->utf8_length(), 1421 moop->name()->bytes(), 1422 moop->name()->utf8_length(), 1423 moop->signature()->bytes(), 1424 moop->signature()->utf8_length(), 1425 code_begin(), code_size()); 1426 1427 if (JvmtiExport::should_post_compiled_method_load()) { 1428 JvmtiExport::post_compiled_method_load(this); 1429 } 1430 } 1431 1432 void nmethod::post_compiled_method_unload() { 1433 assert(_method != NULL && !is_unloaded(), "just checking"); 1434 DTRACE_METHOD_UNLOAD_PROBE(method()); 1435 1436 // If a JVMTI agent has enabled the CompiledMethodUnload event then 1437 // post the event. Sometime later this nmethod will be made a zombie by 1438 // the sweeper but the methodOop will not be valid at that point. 1439 if (JvmtiExport::should_post_compiled_method_unload()) { 1440 assert(!unload_reported(), "already unloaded"); 1441 HandleMark hm; 1442 JvmtiExport::post_compiled_method_unload_at_safepoint( 1443 method()->jmethod_id(), code_begin()); 1444 } 1445 1446 // The JVMTI CompiledMethodUnload event can be enabled or disabled at 1447 // any time. As the nmethod is being unloaded now we mark it has 1448 // having the unload event reported - this will ensure that we don't 1449 // attempt to report the event in the unlikely scenario where the 1450 // event is enabled at the time the nmethod is made a zombie. 1451 set_unload_reported(); 1452 } 1453 1454 // This is called at the end of the strong tracing/marking phase of a 1455 // GC to unload an nmethod if it contains otherwise unreachable 1456 // oops. 1457 1458 void nmethod::do_unloading(BoolObjectClosure* is_alive, 1459 OopClosure* keep_alive, bool unloading_occurred) { 1460 // Make sure the oop's ready to receive visitors 1461 assert(!is_zombie() && !is_unloaded(), 1462 "should not call follow on zombie or unloaded nmethod"); 1463 1464 // If the method is not entrant then a JMP is plastered over the 1465 // first few bytes. If an oop in the old code was there, that oop 1466 // should not get GC'd. Skip the first few bytes of oops on 1467 // not-entrant methods. 1468 address low_boundary = verified_entry_point(); 1469 if (is_not_entrant()) { 1470 low_boundary += NativeJump::instruction_size; 1471 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1472 // (See comment above.) 1473 } 1474 1475 // The RedefineClasses() API can cause the class unloading invariant 1476 // to no longer be true. See jvmtiExport.hpp for details. 1477 // Also, leave a debugging breadcrumb in local flag. 1478 bool a_class_was_redefined = JvmtiExport::has_redefined_a_class(); 1479 if (a_class_was_redefined) { 1480 // This set of the unloading_occurred flag is done before the 1481 // call to post_compiled_method_unload() so that the unloading 1482 // of this nmethod is reported. 1483 unloading_occurred = true; 1484 } 1485 1486 // Follow methodOop 1487 if (can_unload(is_alive, keep_alive, (oop*)&_method, unloading_occurred)) { 1488 return; 1489 } 1490 1491 // Exception cache 1492 ExceptionCache* ec = exception_cache(); 1493 while (ec != NULL) { 1494 oop* ex_addr = (oop*)ec->exception_type_addr(); 1495 oop ex = *ex_addr; 1496 ExceptionCache* next_ec = ec->next(); 1497 if (ex != NULL && !is_alive->do_object_b(ex)) { 1498 assert(!ex->is_compiledICHolder(), "Possible error here"); 1499 remove_from_exception_cache(ec); 1500 } 1501 ec = next_ec; 1502 } 1503 1504 // If class unloading occurred we first iterate over all inline caches and 1505 // clear ICs where the cached oop is referring to an unloaded klass or method. 1506 // The remaining live cached oops will be traversed in the relocInfo::oop_type 1507 // iteration below. 1508 if (unloading_occurred) { 1509 RelocIterator iter(this, low_boundary); 1510 while(iter.next()) { 1511 if (iter.type() == relocInfo::virtual_call_type) { 1512 CompiledIC *ic = CompiledIC_at(iter.reloc()); 1513 oop ic_oop = ic->cached_oop(); 1514 if (ic_oop != NULL && !is_alive->do_object_b(ic_oop)) { 1515 // The only exception is compiledICHolder oops which may 1516 // yet be marked below. (We check this further below). 1517 if (ic_oop->is_compiledICHolder()) { 1518 compiledICHolderOop cichk_oop = compiledICHolderOop(ic_oop); 1519 if (is_alive->do_object_b( 1520 cichk_oop->holder_method()->method_holder()) && 1521 is_alive->do_object_b(cichk_oop->holder_klass())) { 1522 continue; 1523 } 1524 } 1525 ic->set_to_clean(); 1526 assert(ic->cached_oop() == NULL, 1527 "cached oop in IC should be cleared"); 1528 } 1529 } 1530 } 1531 } 1532 1533 // Compiled code 1534 RelocIterator iter(this, low_boundary); 1535 while (iter.next()) { 1536 if (iter.type() == relocInfo::oop_type) { 1537 oop_Relocation* r = iter.oop_reloc(); 1538 // In this loop, we must only traverse those oops directly embedded in 1539 // the code. Other oops (oop_index>0) are seen as part of scopes_oops. 1540 assert(1 == (r->oop_is_immediate()) + 1541 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), 1542 "oop must be found in exactly one place"); 1543 if (r->oop_is_immediate() && r->oop_value() != NULL) { 1544 if (can_unload(is_alive, keep_alive, r->oop_addr(), unloading_occurred)) { 1545 return; 1546 } 1547 } 1548 } 1549 } 1550 1551 1552 // Scopes 1553 for (oop* p = oops_begin(); p < oops_end(); p++) { 1554 if (*p == Universe::non_oop_word()) continue; // skip non-oops 1555 if (can_unload(is_alive, keep_alive, p, unloading_occurred)) { 1556 return; 1557 } 1558 } 1559 1560 #ifndef PRODUCT 1561 // This nmethod was not unloaded; check below that all CompiledICs 1562 // refer to marked oops. 1563 { 1564 RelocIterator iter(this, low_boundary); 1565 while (iter.next()) { 1566 if (iter.type() == relocInfo::virtual_call_type) { 1567 CompiledIC *ic = CompiledIC_at(iter.reloc()); 1568 oop ic_oop = ic->cached_oop(); 1569 assert(ic_oop == NULL || is_alive->do_object_b(ic_oop), 1570 "Found unmarked ic_oop in reachable nmethod"); 1571 } 1572 } 1573 } 1574 #endif // !PRODUCT 1575 } 1576 1577 // This method is called twice during GC -- once while 1578 // tracing the "active" nmethods on thread stacks during 1579 // the (strong) marking phase, and then again when walking 1580 // the code cache contents during the weak roots processing 1581 // phase. The two uses are distinguished by means of the 1582 // 'do_strong_roots_only' flag, which is true in the first 1583 // case. We want to walk the weak roots in the nmethod 1584 // only in the second case. The weak roots in the nmethod 1585 // are the oops in the ExceptionCache and the InlineCache 1586 // oops. 1587 void nmethod::oops_do(OopClosure* f, bool do_strong_roots_only) { 1588 // make sure the oops ready to receive visitors 1589 assert(!is_zombie() && !is_unloaded(), 1590 "should not call follow on zombie or unloaded nmethod"); 1591 1592 // If the method is not entrant or zombie then a JMP is plastered over the 1593 // first few bytes. If an oop in the old code was there, that oop 1594 // should not get GC'd. Skip the first few bytes of oops on 1595 // not-entrant methods. 1596 address low_boundary = verified_entry_point(); 1597 if (is_not_entrant()) { 1598 low_boundary += NativeJump::instruction_size; 1599 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 1600 // (See comment above.) 1601 } 1602 1603 // Compiled code 1604 f->do_oop((oop*) &_method); 1605 if (!do_strong_roots_only) { 1606 // weak roots processing phase -- update ExceptionCache oops 1607 ExceptionCache* ec = exception_cache(); 1608 while(ec != NULL) { 1609 f->do_oop((oop*)ec->exception_type_addr()); 1610 ec = ec->next(); 1611 } 1612 } // Else strong roots phase -- skip oops in ExceptionCache 1613 1614 RelocIterator iter(this, low_boundary); 1615 1616 while (iter.next()) { 1617 if (iter.type() == relocInfo::oop_type ) { 1618 oop_Relocation* r = iter.oop_reloc(); 1619 // In this loop, we must only follow those oops directly embedded in 1620 // the code. Other oops (oop_index>0) are seen as part of scopes_oops. 1621 assert(1 == (r->oop_is_immediate()) + 1622 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), 1623 "oop must be found in exactly one place"); 1624 if (r->oop_is_immediate() && r->oop_value() != NULL) { 1625 f->do_oop(r->oop_addr()); 1626 } 1627 } 1628 } 1629 1630 // Scopes 1631 // This includes oop constants not inlined in the code stream. 1632 for (oop* p = oops_begin(); p < oops_end(); p++) { 1633 if (*p == Universe::non_oop_word()) continue; // skip non-oops 1634 f->do_oop(p); 1635 } 1636 } 1637 1638 #define NMETHOD_SENTINEL ((nmethod*)badAddress) 1639 1640 nmethod* volatile nmethod::_oops_do_mark_nmethods; 1641 1642 // An nmethod is "marked" if its _mark_link is set non-null. 1643 // Even if it is the end of the linked list, it will have a non-null link value, 1644 // as long as it is on the list. 1645 // This code must be MP safe, because it is used from parallel GC passes. 1646 bool nmethod::test_set_oops_do_mark() { 1647 assert(nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called"); 1648 nmethod* observed_mark_link = _oops_do_mark_link; 1649 if (observed_mark_link == NULL) { 1650 // Claim this nmethod for this thread to mark. 1651 observed_mark_link = (nmethod*) 1652 Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_link, NULL); 1653 if (observed_mark_link == NULL) { 1654 1655 // Atomically append this nmethod (now claimed) to the head of the list: 1656 nmethod* observed_mark_nmethods = _oops_do_mark_nmethods; 1657 for (;;) { 1658 nmethod* required_mark_nmethods = observed_mark_nmethods; 1659 _oops_do_mark_link = required_mark_nmethods; 1660 observed_mark_nmethods = (nmethod*) 1661 Atomic::cmpxchg_ptr(this, &_oops_do_mark_nmethods, required_mark_nmethods); 1662 if (observed_mark_nmethods == required_mark_nmethods) 1663 break; 1664 } 1665 // Mark was clear when we first saw this guy. 1666 NOT_PRODUCT(if (TraceScavenge) print_on(tty, "oops_do, mark\n")); 1667 return false; 1668 } 1669 } 1670 // On fall through, another racing thread marked this nmethod before we did. 1671 return true; 1672 } 1673 1674 void nmethod::oops_do_marking_prologue() { 1675 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("[oops_do_marking_prologue")); 1676 assert(_oops_do_mark_nmethods == NULL, "must not call oops_do_marking_prologue twice in a row"); 1677 // We use cmpxchg_ptr instead of regular assignment here because the user 1678 // may fork a bunch of threads, and we need them all to see the same state. 1679 void* observed = Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_nmethods, NULL); 1680 guarantee(observed == NULL, "no races in this sequential code"); 1681 } 1682 1683 void nmethod::oops_do_marking_epilogue() { 1684 assert(_oops_do_mark_nmethods != NULL, "must not call oops_do_marking_epilogue twice in a row"); 1685 nmethod* cur = _oops_do_mark_nmethods; 1686 while (cur != NMETHOD_SENTINEL) { 1687 assert(cur != NULL, "not NULL-terminated"); 1688 nmethod* next = cur->_oops_do_mark_link; 1689 cur->_oops_do_mark_link = NULL; 1690 NOT_PRODUCT(if (TraceScavenge) cur->print_on(tty, "oops_do, unmark\n")); 1691 cur = next; 1692 } 1693 void* required = _oops_do_mark_nmethods; 1694 void* observed = Atomic::cmpxchg_ptr(NULL, &_oops_do_mark_nmethods, required); 1695 guarantee(observed == required, "no races in this sequential code"); 1696 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("oops_do_marking_epilogue]")); 1697 } 1698 1699 class DetectScavengeRoot: public OopClosure { 1700 bool _detected_scavenge_root; 1701 public: 1702 DetectScavengeRoot() : _detected_scavenge_root(false) 1703 { NOT_PRODUCT(_print_nm = NULL); } 1704 bool detected_scavenge_root() { return _detected_scavenge_root; } 1705 virtual void do_oop(oop* p) { 1706 if ((*p) != NULL && (*p)->is_scavengable()) { 1707 NOT_PRODUCT(maybe_print(p)); 1708 _detected_scavenge_root = true; 1709 } 1710 } 1711 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } 1712 1713 #ifndef PRODUCT 1714 nmethod* _print_nm; 1715 void maybe_print(oop* p) { 1716 if (_print_nm == NULL) return; 1717 if (!_detected_scavenge_root) _print_nm->print_on(tty, "new scavenge root"); 1718 tty->print_cr(""PTR_FORMAT"[offset=%d] detected non-perm oop "PTR_FORMAT" (found at "PTR_FORMAT")", 1719 _print_nm, (int)((intptr_t)p - (intptr_t)_print_nm), 1720 (intptr_t)(*p), (intptr_t)p); 1721 (*p)->print(); 1722 } 1723 #endif //PRODUCT 1724 }; 1725 1726 bool nmethod::detect_scavenge_root_oops() { 1727 DetectScavengeRoot detect_scavenge_root; 1728 NOT_PRODUCT(if (TraceScavenge) detect_scavenge_root._print_nm = this); 1729 oops_do(&detect_scavenge_root); 1730 return detect_scavenge_root.detected_scavenge_root(); 1731 } 1732 1733 // Method that knows how to preserve outgoing arguments at call. This method must be 1734 // called with a frame corresponding to a Java invoke 1735 void nmethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { 1736 if (!method()->is_native()) { 1737 SimpleScopeDesc ssd(this, fr.pc()); 1738 Bytecode_invoke* call = Bytecode_invoke_at(ssd.method(), ssd.bci()); 1739 bool has_receiver = call->has_receiver(); 1740 symbolOop signature = call->signature(); 1741 fr.oops_compiled_arguments_do(signature, has_receiver, reg_map, f); 1742 } 1743 } 1744 1745 1746 oop nmethod::embeddedOop_at(u_char* p) { 1747 RelocIterator iter(this, p, p + oopSize); 1748 while (iter.next()) 1749 if (iter.type() == relocInfo::oop_type) { 1750 return iter.oop_reloc()->oop_value(); 1751 } 1752 return NULL; 1753 } 1754 1755 1756 inline bool includes(void* p, void* from, void* to) { 1757 return from <= p && p < to; 1758 } 1759 1760 1761 void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) { 1762 assert(count >= 2, "must be sentinel values, at least"); 1763 1764 #ifdef ASSERT 1765 // must be sorted and unique; we do a binary search in find_pc_desc() 1766 int prev_offset = pcs[0].pc_offset(); 1767 assert(prev_offset == PcDesc::lower_offset_limit, 1768 "must start with a sentinel"); 1769 for (int i = 1; i < count; i++) { 1770 int this_offset = pcs[i].pc_offset(); 1771 assert(this_offset > prev_offset, "offsets must be sorted"); 1772 prev_offset = this_offset; 1773 } 1774 assert(prev_offset == PcDesc::upper_offset_limit, 1775 "must end with a sentinel"); 1776 #endif //ASSERT 1777 1778 // Search for MethodHandle invokes and tag the nmethod. 1779 for (int i = 0; i < count; i++) { 1780 if (pcs[i].is_method_handle_invoke()) { 1781 set_has_method_handle_invokes(true); 1782 break; 1783 } 1784 } 1785 1786 int size = count * sizeof(PcDesc); 1787 assert(scopes_pcs_size() >= size, "oob"); 1788 memcpy(scopes_pcs_begin(), pcs, size); 1789 1790 // Adjust the final sentinel downward. 1791 PcDesc* last_pc = &scopes_pcs_begin()[count-1]; 1792 assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity"); 1793 last_pc->set_pc_offset(instructions_size() + 1); 1794 for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) { 1795 // Fill any rounding gaps with copies of the last record. 1796 last_pc[1] = last_pc[0]; 1797 } 1798 // The following assert could fail if sizeof(PcDesc) is not 1799 // an integral multiple of oopSize (the rounding term). 1800 // If it fails, change the logic to always allocate a multiple 1801 // of sizeof(PcDesc), and fill unused words with copies of *last_pc. 1802 assert(last_pc + 1 == scopes_pcs_end(), "must match exactly"); 1803 } 1804 1805 void nmethod::copy_scopes_data(u_char* buffer, int size) { 1806 assert(scopes_data_size() >= size, "oob"); 1807 memcpy(scopes_data_begin(), buffer, size); 1808 } 1809 1810 1811 #ifdef ASSERT 1812 static PcDesc* linear_search(nmethod* nm, int pc_offset, bool approximate) { 1813 PcDesc* lower = nm->scopes_pcs_begin(); 1814 PcDesc* upper = nm->scopes_pcs_end(); 1815 lower += 1; // exclude initial sentinel 1816 PcDesc* res = NULL; 1817 for (PcDesc* p = lower; p < upper; p++) { 1818 NOT_PRODUCT(--nmethod_stats.pc_desc_tests); // don't count this call to match_desc 1819 if (match_desc(p, pc_offset, approximate)) { 1820 if (res == NULL) 1821 res = p; 1822 else 1823 res = (PcDesc*) badAddress; 1824 } 1825 } 1826 return res; 1827 } 1828 #endif 1829 1830 1831 // Finds a PcDesc with real-pc equal to "pc" 1832 PcDesc* nmethod::find_pc_desc_internal(address pc, bool approximate) { 1833 address base_address = instructions_begin(); 1834 if ((pc < base_address) || 1835 (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) { 1836 return NULL; // PC is wildly out of range 1837 } 1838 int pc_offset = (int) (pc - base_address); 1839 1840 // Check the PcDesc cache if it contains the desired PcDesc 1841 // (This as an almost 100% hit rate.) 1842 PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate); 1843 if (res != NULL) { 1844 assert(res == linear_search(this, pc_offset, approximate), "cache ok"); 1845 return res; 1846 } 1847 1848 // Fallback algorithm: quasi-linear search for the PcDesc 1849 // Find the last pc_offset less than the given offset. 1850 // The successor must be the required match, if there is a match at all. 1851 // (Use a fixed radix to avoid expensive affine pointer arithmetic.) 1852 PcDesc* lower = scopes_pcs_begin(); 1853 PcDesc* upper = scopes_pcs_end(); 1854 upper -= 1; // exclude final sentinel 1855 if (lower >= upper) return NULL; // native method; no PcDescs at all 1856 1857 #define assert_LU_OK \ 1858 /* invariant on lower..upper during the following search: */ \ 1859 assert(lower->pc_offset() < pc_offset, "sanity"); \ 1860 assert(upper->pc_offset() >= pc_offset, "sanity") 1861 assert_LU_OK; 1862 1863 // Use the last successful return as a split point. 1864 PcDesc* mid = _pc_desc_cache.last_pc_desc(); 1865 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); 1866 if (mid->pc_offset() < pc_offset) { 1867 lower = mid; 1868 } else { 1869 upper = mid; 1870 } 1871 1872 // Take giant steps at first (4096, then 256, then 16, then 1) 1873 const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1); 1874 const int RADIX = (1 << LOG2_RADIX); 1875 for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) { 1876 while ((mid = lower + step) < upper) { 1877 assert_LU_OK; 1878 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); 1879 if (mid->pc_offset() < pc_offset) { 1880 lower = mid; 1881 } else { 1882 upper = mid; 1883 break; 1884 } 1885 } 1886 assert_LU_OK; 1887 } 1888 1889 // Sneak up on the value with a linear search of length ~16. 1890 while (true) { 1891 assert_LU_OK; 1892 mid = lower + 1; 1893 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); 1894 if (mid->pc_offset() < pc_offset) { 1895 lower = mid; 1896 } else { 1897 upper = mid; 1898 break; 1899 } 1900 } 1901 #undef assert_LU_OK 1902 1903 if (match_desc(upper, pc_offset, approximate)) { 1904 assert(upper == linear_search(this, pc_offset, approximate), "search ok"); 1905 _pc_desc_cache.add_pc_desc(upper); 1906 return upper; 1907 } else { 1908 assert(NULL == linear_search(this, pc_offset, approximate), "search ok"); 1909 return NULL; 1910 } 1911 } 1912 1913 1914 bool nmethod::check_all_dependencies() { 1915 bool found_check = false; 1916 // wholesale check of all dependencies 1917 for (Dependencies::DepStream deps(this); deps.next(); ) { 1918 if (deps.check_dependency() != NULL) { 1919 found_check = true; 1920 NOT_DEBUG(break); 1921 } 1922 } 1923 return found_check; // tell caller if we found anything 1924 } 1925 1926 bool nmethod::check_dependency_on(DepChange& changes) { 1927 // What has happened: 1928 // 1) a new class dependee has been added 1929 // 2) dependee and all its super classes have been marked 1930 bool found_check = false; // set true if we are upset 1931 for (Dependencies::DepStream deps(this); deps.next(); ) { 1932 // Evaluate only relevant dependencies. 1933 if (deps.spot_check_dependency_at(changes) != NULL) { 1934 found_check = true; 1935 NOT_DEBUG(break); 1936 } 1937 } 1938 return found_check; 1939 } 1940 1941 bool nmethod::is_evol_dependent_on(klassOop dependee) { 1942 instanceKlass *dependee_ik = instanceKlass::cast(dependee); 1943 objArrayOop dependee_methods = dependee_ik->methods(); 1944 for (Dependencies::DepStream deps(this); deps.next(); ) { 1945 if (deps.type() == Dependencies::evol_method) { 1946 methodOop method = deps.method_argument(0); 1947 for (int j = 0; j < dependee_methods->length(); j++) { 1948 if ((methodOop) dependee_methods->obj_at(j) == method) { 1949 // RC_TRACE macro has an embedded ResourceMark 1950 RC_TRACE(0x01000000, 1951 ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)", 1952 _method->method_holder()->klass_part()->external_name(), 1953 _method->name()->as_C_string(), 1954 _method->signature()->as_C_string(), compile_id(), 1955 method->method_holder()->klass_part()->external_name(), 1956 method->name()->as_C_string(), 1957 method->signature()->as_C_string())); 1958 if (TraceDependencies || LogCompilation) 1959 deps.log_dependency(dependee); 1960 return true; 1961 } 1962 } 1963 } 1964 } 1965 return false; 1966 } 1967 1968 // Called from mark_for_deoptimization, when dependee is invalidated. 1969 bool nmethod::is_dependent_on_method(methodOop dependee) { 1970 for (Dependencies::DepStream deps(this); deps.next(); ) { 1971 if (deps.type() != Dependencies::evol_method) 1972 continue; 1973 methodOop method = deps.method_argument(0); 1974 if (method == dependee) return true; 1975 } 1976 return false; 1977 } 1978 1979 1980 bool nmethod::is_patchable_at(address instr_addr) { 1981 assert (code_contains(instr_addr), "wrong nmethod used"); 1982 if (is_zombie()) { 1983 // a zombie may never be patched 1984 return false; 1985 } 1986 return true; 1987 } 1988 1989 1990 address nmethod::continuation_for_implicit_exception(address pc) { 1991 // Exception happened outside inline-cache check code => we are inside 1992 // an active nmethod => use cpc to determine a return address 1993 int exception_offset = pc - instructions_begin(); 1994 int cont_offset = ImplicitExceptionTable(this).at( exception_offset ); 1995 #ifdef ASSERT 1996 if (cont_offset == 0) { 1997 Thread* thread = ThreadLocalStorage::get_thread_slow(); 1998 ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY 1999 HandleMark hm(thread); 2000 ResourceMark rm(thread); 2001 CodeBlob* cb = CodeCache::find_blob(pc); 2002 assert(cb != NULL && cb == this, ""); 2003 tty->print_cr("implicit exception happened at " INTPTR_FORMAT, pc); 2004 print(); 2005 method()->print_codes(); 2006 print_code(); 2007 print_pcs(); 2008 } 2009 #endif 2010 if (cont_offset == 0) { 2011 // Let the normal error handling report the exception 2012 return NULL; 2013 } 2014 return instructions_begin() + cont_offset; 2015 } 2016 2017 2018 2019 void nmethod_init() { 2020 // make sure you didn't forget to adjust the filler fields 2021 assert(sizeof(nmFlags) <= 4, "nmFlags occupies more than a word"); 2022 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word"); 2023 } 2024 2025 2026 //------------------------------------------------------------------------------------------- 2027 2028 2029 // QQQ might we make this work from a frame?? 2030 nmethodLocker::nmethodLocker(address pc) { 2031 CodeBlob* cb = CodeCache::find_blob(pc); 2032 guarantee(cb != NULL && cb->is_nmethod(), "bad pc for a nmethod found"); 2033 _nm = (nmethod*)cb; 2034 lock_nmethod(_nm); 2035 } 2036 2037 void nmethodLocker::lock_nmethod(nmethod* nm) { 2038 if (nm == NULL) return; 2039 Atomic::inc(&nm->_lock_count); 2040 guarantee(!nm->is_zombie(), "cannot lock a zombie method"); 2041 } 2042 2043 void nmethodLocker::unlock_nmethod(nmethod* nm) { 2044 if (nm == NULL) return; 2045 Atomic::dec(&nm->_lock_count); 2046 guarantee(nm->_lock_count >= 0, "unmatched nmethod lock/unlock"); 2047 } 2048 2049 2050 // ----------------------------------------------------------------------------- 2051 // nmethod::get_deopt_original_pc 2052 // 2053 // Return the original PC for the given PC if: 2054 // (a) the given PC belongs to a nmethod and 2055 // (b) it is a deopt PC 2056 address nmethod::get_deopt_original_pc(const frame* fr) { 2057 if (fr->cb() == NULL) return NULL; 2058 2059 nmethod* nm = fr->cb()->as_nmethod_or_null(); 2060 if (nm != NULL && nm->is_deopt_pc(fr->pc())) 2061 return nm->get_original_pc(fr); 2062 2063 return NULL; 2064 } 2065 2066 2067 // ----------------------------------------------------------------------------- 2068 // MethodHandle 2069 2070 bool nmethod::is_method_handle_return(address return_pc) { 2071 if (!has_method_handle_invokes()) return false; 2072 PcDesc* pd = pc_desc_at(return_pc); 2073 if (pd == NULL) 2074 return false; 2075 return pd->is_method_handle_invoke(); 2076 } 2077 2078 2079 // ----------------------------------------------------------------------------- 2080 // Verification 2081 2082 class VerifyOopsClosure: public OopClosure { 2083 nmethod* _nm; 2084 bool _ok; 2085 public: 2086 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { } 2087 bool ok() { return _ok; } 2088 virtual void do_oop(oop* p) { 2089 if ((*p) == NULL || (*p)->is_oop()) return; 2090 if (_ok) { 2091 _nm->print_nmethod(true); 2092 _ok = false; 2093 } 2094 tty->print_cr("*** non-oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", 2095 (intptr_t)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); 2096 } 2097 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } 2098 }; 2099 2100 void nmethod::verify() { 2101 2102 // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant 2103 // seems odd. 2104 2105 if( is_zombie() || is_not_entrant() ) 2106 return; 2107 2108 // Make sure all the entry points are correctly aligned for patching. 2109 NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point()); 2110 2111 assert(method()->is_oop(), "must be valid"); 2112 2113 ResourceMark rm; 2114 2115 if (!CodeCache::contains(this)) { 2116 fatal(err_msg("nmethod at " INTPTR_FORMAT " not in zone", this)); 2117 } 2118 2119 if(is_native_method() ) 2120 return; 2121 2122 nmethod* nm = CodeCache::find_nmethod(verified_entry_point()); 2123 if (nm != this) { 2124 fatal(err_msg("findNMethod did not find this nmethod (" INTPTR_FORMAT ")", 2125 this)); 2126 } 2127 2128 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { 2129 if (! p->verify(this)) { 2130 tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", this); 2131 } 2132 } 2133 2134 VerifyOopsClosure voc(this); 2135 oops_do(&voc); 2136 assert(voc.ok(), "embedded oops must be OK"); 2137 verify_scavenge_root_oops(); 2138 2139 verify_scopes(); 2140 } 2141 2142 2143 void nmethod::verify_interrupt_point(address call_site) { 2144 // This code does not work in release mode since 2145 // owns_lock only is available in debug mode. 2146 CompiledIC* ic = NULL; 2147 Thread *cur = Thread::current(); 2148 if (CompiledIC_lock->owner() == cur || 2149 ((cur->is_VM_thread() || cur->is_ConcurrentGC_thread()) && 2150 SafepointSynchronize::is_at_safepoint())) { 2151 ic = CompiledIC_at(call_site); 2152 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 2153 } else { 2154 MutexLocker ml_verify (CompiledIC_lock); 2155 ic = CompiledIC_at(call_site); 2156 } 2157 PcDesc* pd = pc_desc_at(ic->end_of_call()); 2158 assert(pd != NULL, "PcDesc must exist"); 2159 for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(), 2160 pd->obj_decode_offset(), pd->should_reexecute(), 2161 pd->return_oop()); 2162 !sd->is_top(); sd = sd->sender()) { 2163 sd->verify(); 2164 } 2165 } 2166 2167 void nmethod::verify_scopes() { 2168 if( !method() ) return; // Runtime stubs have no scope 2169 if (method()->is_native()) return; // Ignore stub methods. 2170 // iterate through all interrupt point 2171 // and verify the debug information is valid. 2172 RelocIterator iter((nmethod*)this); 2173 while (iter.next()) { 2174 address stub = NULL; 2175 switch (iter.type()) { 2176 case relocInfo::virtual_call_type: 2177 verify_interrupt_point(iter.addr()); 2178 break; 2179 case relocInfo::opt_virtual_call_type: 2180 stub = iter.opt_virtual_call_reloc()->static_stub(); 2181 verify_interrupt_point(iter.addr()); 2182 break; 2183 case relocInfo::static_call_type: 2184 stub = iter.static_call_reloc()->static_stub(); 2185 //verify_interrupt_point(iter.addr()); 2186 break; 2187 case relocInfo::runtime_call_type: 2188 address destination = iter.reloc()->value(); 2189 // Right now there is no way to find out which entries support 2190 // an interrupt point. It would be nice if we had this 2191 // information in a table. 2192 break; 2193 } 2194 assert(stub == NULL || stub_contains(stub), "static call stub outside stub section"); 2195 } 2196 } 2197 2198 2199 // ----------------------------------------------------------------------------- 2200 // Non-product code 2201 #ifndef PRODUCT 2202 2203 class DebugScavengeRoot: public OopClosure { 2204 nmethod* _nm; 2205 bool _ok; 2206 public: 2207 DebugScavengeRoot(nmethod* nm) : _nm(nm), _ok(true) { } 2208 bool ok() { return _ok; } 2209 virtual void do_oop(oop* p) { 2210 if ((*p) == NULL || !(*p)->is_scavengable()) return; 2211 if (_ok) { 2212 _nm->print_nmethod(true); 2213 _ok = false; 2214 } 2215 tty->print_cr("*** non-perm oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", 2216 (intptr_t)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); 2217 (*p)->print(); 2218 } 2219 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } 2220 }; 2221 2222 void nmethod::verify_scavenge_root_oops() { 2223 if (!on_scavenge_root_list()) { 2224 // Actually look inside, to verify the claim that it's clean. 2225 DebugScavengeRoot debug_scavenge_root(this); 2226 oops_do(&debug_scavenge_root); 2227 if (!debug_scavenge_root.ok()) 2228 fatal("found an unadvertised bad non-perm oop in the code cache"); 2229 } 2230 assert(scavenge_root_not_marked(), ""); 2231 } 2232 2233 #endif // PRODUCT 2234 2235 // Printing operations 2236 2237 void nmethod::print() const { 2238 ResourceMark rm; 2239 ttyLocker ttyl; // keep the following output all in one block 2240 2241 tty->print("Compiled "); 2242 2243 if (is_compiled_by_c1()) { 2244 tty->print("(c1) "); 2245 } else if (is_compiled_by_c2()) { 2246 tty->print("(c2) "); 2247 } else { 2248 tty->print("(nm) "); 2249 } 2250 2251 print_on(tty, "nmethod"); 2252 tty->cr(); 2253 if (WizardMode) { 2254 tty->print("((nmethod*) "INTPTR_FORMAT ") ", this); 2255 tty->print(" for method " INTPTR_FORMAT , (address)method()); 2256 tty->print(" { "); 2257 if (version()) tty->print("v%d ", version()); 2258 if (is_in_use()) tty->print("in_use "); 2259 if (is_not_entrant()) tty->print("not_entrant "); 2260 if (is_zombie()) tty->print("zombie "); 2261 if (is_unloaded()) tty->print("unloaded "); 2262 if (on_scavenge_root_list()) tty->print("scavenge_root "); 2263 tty->print_cr("}:"); 2264 } 2265 if (size () > 0) tty->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2266 (address)this, 2267 (address)this + size(), 2268 size()); 2269 if (relocation_size () > 0) tty->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2270 relocation_begin(), 2271 relocation_end(), 2272 relocation_size()); 2273 if (code_size () > 0) tty->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2274 code_begin(), 2275 code_end(), 2276 code_size()); 2277 if (stub_size () > 0) tty->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2278 stub_begin(), 2279 stub_end(), 2280 stub_size()); 2281 if (consts_size () > 0) tty->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2282 consts_begin(), 2283 consts_end(), 2284 consts_size()); 2285 if (scopes_data_size () > 0) tty->print_cr(" scopes data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2286 scopes_data_begin(), 2287 scopes_data_end(), 2288 scopes_data_size()); 2289 if (scopes_pcs_size () > 0) tty->print_cr(" scopes pcs [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2290 scopes_pcs_begin(), 2291 scopes_pcs_end(), 2292 scopes_pcs_size()); 2293 if (dependencies_size () > 0) tty->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2294 dependencies_begin(), 2295 dependencies_end(), 2296 dependencies_size()); 2297 if (handler_table_size() > 0) tty->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2298 handler_table_begin(), 2299 handler_table_end(), 2300 handler_table_size()); 2301 if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2302 nul_chk_table_begin(), 2303 nul_chk_table_end(), 2304 nul_chk_table_size()); 2305 if (oops_size () > 0) tty->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", 2306 oops_begin(), 2307 oops_end(), 2308 oops_size()); 2309 } 2310 2311 void nmethod::print_code() { 2312 HandleMark hm; 2313 ResourceMark m; 2314 Disassembler::decode(this); 2315 } 2316 2317 2318 #ifndef PRODUCT 2319 2320 void nmethod::print_scopes() { 2321 // Find the first pc desc for all scopes in the code and print it. 2322 ResourceMark rm; 2323 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { 2324 if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null) 2325 continue; 2326 2327 ScopeDesc* sd = scope_desc_at(p->real_pc(this)); 2328 sd->print_on(tty, p); 2329 } 2330 } 2331 2332 void nmethod::print_dependencies() { 2333 ResourceMark rm; 2334 ttyLocker ttyl; // keep the following output all in one block 2335 tty->print_cr("Dependencies:"); 2336 for (Dependencies::DepStream deps(this); deps.next(); ) { 2337 deps.print_dependency(); 2338 klassOop ctxk = deps.context_type(); 2339 if (ctxk != NULL) { 2340 Klass* k = Klass::cast(ctxk); 2341 if (k->oop_is_instance() && ((instanceKlass*)k)->is_dependent_nmethod(this)) { 2342 tty->print_cr(" [nmethod<=klass]%s", k->external_name()); 2343 } 2344 } 2345 deps.log_dependency(); // put it into the xml log also 2346 } 2347 } 2348 2349 2350 void nmethod::print_relocations() { 2351 ResourceMark m; // in case methods get printed via the debugger 2352 tty->print_cr("relocations:"); 2353 RelocIterator iter(this); 2354 iter.print(); 2355 if (UseRelocIndex) { 2356 jint* index_end = (jint*)relocation_end() - 1; 2357 jint index_size = *index_end; 2358 jint* index_start = (jint*)( (address)index_end - index_size ); 2359 tty->print_cr(" index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size); 2360 if (index_size > 0) { 2361 jint* ip; 2362 for (ip = index_start; ip+2 <= index_end; ip += 2) 2363 tty->print_cr(" (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT, 2364 ip[0], 2365 ip[1], 2366 header_end()+ip[0], 2367 relocation_begin()-1+ip[1]); 2368 for (; ip < index_end; ip++) 2369 tty->print_cr(" (%d ?)", ip[0]); 2370 tty->print_cr(" @" INTPTR_FORMAT ": index_size=%d", ip, *ip++); 2371 tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip); 2372 } 2373 } 2374 } 2375 2376 2377 void nmethod::print_pcs() { 2378 ResourceMark m; // in case methods get printed via debugger 2379 tty->print_cr("pc-bytecode offsets:"); 2380 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { 2381 p->print(this); 2382 } 2383 } 2384 2385 #endif // PRODUCT 2386 2387 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) { 2388 RelocIterator iter(this, begin, end); 2389 bool have_one = false; 2390 while (iter.next()) { 2391 have_one = true; 2392 switch (iter.type()) { 2393 case relocInfo::none: return "no_reloc"; 2394 case relocInfo::oop_type: { 2395 stringStream st; 2396 oop_Relocation* r = iter.oop_reloc(); 2397 oop obj = r->oop_value(); 2398 st.print("oop("); 2399 if (obj == NULL) st.print("NULL"); 2400 else obj->print_value_on(&st); 2401 st.print(")"); 2402 return st.as_string(); 2403 } 2404 case relocInfo::virtual_call_type: return "virtual_call"; 2405 case relocInfo::opt_virtual_call_type: return "optimized virtual_call"; 2406 case relocInfo::static_call_type: return "static_call"; 2407 case relocInfo::static_stub_type: return "static_stub"; 2408 case relocInfo::runtime_call_type: return "runtime_call"; 2409 case relocInfo::external_word_type: return "external_word"; 2410 case relocInfo::internal_word_type: return "internal_word"; 2411 case relocInfo::section_word_type: return "section_word"; 2412 case relocInfo::poll_type: return "poll"; 2413 case relocInfo::poll_return_type: return "poll_return"; 2414 case relocInfo::type_mask: return "type_bit_mask"; 2415 } 2416 } 2417 return have_one ? "other" : NULL; 2418 } 2419 2420 // Return a the last scope in (begin..end] 2421 ScopeDesc* nmethod::scope_desc_in(address begin, address end) { 2422 PcDesc* p = pc_desc_near(begin+1); 2423 if (p != NULL && p->real_pc(this) <= end) { 2424 return new ScopeDesc(this, p->scope_decode_offset(), 2425 p->obj_decode_offset(), p->should_reexecute(), 2426 p->return_oop()); 2427 } 2428 return NULL; 2429 } 2430 2431 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin) { 2432 if (block_begin == entry_point()) stream->print_cr("[Entry Point]"); 2433 if (block_begin == verified_entry_point()) stream->print_cr("[Verified Entry Point]"); 2434 if (block_begin == exception_begin()) stream->print_cr("[Exception Handler]"); 2435 if (block_begin == stub_begin()) stream->print_cr("[Stub Code]"); 2436 if (block_begin == deopt_handler_begin()) stream->print_cr("[Deopt Handler Code]"); 2437 if (block_begin == deopt_mh_handler_begin()) stream->print_cr("[Deopt MH Handler Code]"); 2438 if (block_begin == consts_begin()) stream->print_cr("[Constants]"); 2439 if (block_begin == entry_point()) { 2440 methodHandle m = method(); 2441 if (m.not_null()) { 2442 stream->print(" # "); 2443 m->print_value_on(stream); 2444 stream->cr(); 2445 } 2446 if (m.not_null() && !is_osr_method()) { 2447 ResourceMark rm; 2448 int sizeargs = m->size_of_parameters(); 2449 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); 2450 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); 2451 { 2452 int sig_index = 0; 2453 if (!m->is_static()) 2454 sig_bt[sig_index++] = T_OBJECT; // 'this' 2455 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) { 2456 BasicType t = ss.type(); 2457 sig_bt[sig_index++] = t; 2458 if (type2size[t] == 2) { 2459 sig_bt[sig_index++] = T_VOID; 2460 } else { 2461 assert(type2size[t] == 1, "size is 1 or 2"); 2462 } 2463 } 2464 assert(sig_index == sizeargs, ""); 2465 } 2466 const char* spname = "sp"; // make arch-specific? 2467 intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, false); 2468 int stack_slot_offset = this->frame_size() * wordSize; 2469 int tab1 = 14, tab2 = 24; 2470 int sig_index = 0; 2471 int arg_index = (m->is_static() ? 0 : -1); 2472 bool did_old_sp = false; 2473 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) { 2474 bool at_this = (arg_index == -1); 2475 bool at_old_sp = false; 2476 BasicType t = (at_this ? T_OBJECT : ss.type()); 2477 assert(t == sig_bt[sig_index], "sigs in sync"); 2478 if (at_this) 2479 stream->print(" # this: "); 2480 else 2481 stream->print(" # parm%d: ", arg_index); 2482 stream->move_to(tab1); 2483 VMReg fst = regs[sig_index].first(); 2484 VMReg snd = regs[sig_index].second(); 2485 if (fst->is_reg()) { 2486 stream->print("%s", fst->name()); 2487 if (snd->is_valid()) { 2488 stream->print(":%s", snd->name()); 2489 } 2490 } else if (fst->is_stack()) { 2491 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset; 2492 if (offset == stack_slot_offset) at_old_sp = true; 2493 stream->print("[%s+0x%x]", spname, offset); 2494 } else { 2495 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd); 2496 } 2497 stream->print(" "); 2498 stream->move_to(tab2); 2499 stream->print("= "); 2500 if (at_this) { 2501 m->method_holder()->print_value_on(stream); 2502 } else { 2503 bool did_name = false; 2504 if (!at_this && ss.is_object()) { 2505 symbolOop name = ss.as_symbol_or_null(); 2506 if (name != NULL) { 2507 name->print_value_on(stream); 2508 did_name = true; 2509 } 2510 } 2511 if (!did_name) 2512 stream->print("%s", type2name(t)); 2513 } 2514 if (at_old_sp) { 2515 stream->print(" (%s of caller)", spname); 2516 did_old_sp = true; 2517 } 2518 stream->cr(); 2519 sig_index += type2size[t]; 2520 arg_index += 1; 2521 if (!at_this) ss.next(); 2522 } 2523 if (!did_old_sp) { 2524 stream->print(" # "); 2525 stream->move_to(tab1); 2526 stream->print("[%s+0x%x]", spname, stack_slot_offset); 2527 stream->print(" (%s of caller)", spname); 2528 stream->cr(); 2529 } 2530 } 2531 } 2532 } 2533 2534 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) { 2535 // First, find an oopmap in (begin, end]. 2536 // We use the odd half-closed interval so that oop maps and scope descs 2537 // which are tied to the byte after a call are printed with the call itself. 2538 address base = instructions_begin(); 2539 OopMapSet* oms = oop_maps(); 2540 if (oms != NULL) { 2541 for (int i = 0, imax = oms->size(); i < imax; i++) { 2542 OopMap* om = oms->at(i); 2543 address pc = base + om->offset(); 2544 if (pc > begin) { 2545 if (pc <= end) { 2546 st->move_to(column); 2547 st->print("; "); 2548 om->print_on(st); 2549 } 2550 break; 2551 } 2552 } 2553 } 2554 2555 // Print any debug info present at this pc. 2556 ScopeDesc* sd = scope_desc_in(begin, end); 2557 if (sd != NULL) { 2558 st->move_to(column); 2559 if (sd->bci() == SynchronizationEntryBCI) { 2560 st->print(";*synchronization entry"); 2561 } else { 2562 if (sd->method().is_null()) { 2563 st->print("method is NULL"); 2564 } else if (sd->method()->is_native()) { 2565 st->print("method is native"); 2566 } else { 2567 address bcp = sd->method()->bcp_from(sd->bci()); 2568 Bytecodes::Code bc = Bytecodes::java_code_at(bcp); 2569 st->print(";*%s", Bytecodes::name(bc)); 2570 switch (bc) { 2571 case Bytecodes::_invokevirtual: 2572 case Bytecodes::_invokespecial: 2573 case Bytecodes::_invokestatic: 2574 case Bytecodes::_invokeinterface: 2575 { 2576 Bytecode_invoke* invoke = Bytecode_invoke_at(sd->method(), sd->bci()); 2577 st->print(" "); 2578 if (invoke->name() != NULL) 2579 invoke->name()->print_symbol_on(st); 2580 else 2581 st->print("<UNKNOWN>"); 2582 break; 2583 } 2584 case Bytecodes::_getfield: 2585 case Bytecodes::_putfield: 2586 case Bytecodes::_getstatic: 2587 case Bytecodes::_putstatic: 2588 { 2589 methodHandle sdm = sd->method(); 2590 Bytecode_field* field = Bytecode_field_at(sdm(), sdm->bcp_from(sd->bci())); 2591 constantPoolOop sdmc = sdm->constants(); 2592 symbolOop name = sdmc->name_ref_at(field->index()); 2593 st->print(" "); 2594 if (name != NULL) 2595 name->print_symbol_on(st); 2596 else 2597 st->print("<UNKNOWN>"); 2598 } 2599 } 2600 } 2601 } 2602 2603 // Print all scopes 2604 for (;sd != NULL; sd = sd->sender()) { 2605 st->move_to(column); 2606 st->print("; -"); 2607 if (sd->method().is_null()) { 2608 st->print("method is NULL"); 2609 } else { 2610 sd->method()->print_short_name(st); 2611 } 2612 int lineno = sd->method()->line_number_from_bci(sd->bci()); 2613 if (lineno != -1) { 2614 st->print("@%d (line %d)", sd->bci(), lineno); 2615 } else { 2616 st->print("@%d", sd->bci()); 2617 } 2618 st->cr(); 2619 } 2620 } 2621 2622 // Print relocation information 2623 const char* str = reloc_string_for(begin, end); 2624 if (str != NULL) { 2625 if (sd != NULL) st->cr(); 2626 st->move_to(column); 2627 st->print("; {%s}", str); 2628 } 2629 int cont_offset = ImplicitExceptionTable(this).at(begin - instructions_begin()); 2630 if (cont_offset != 0) { 2631 st->move_to(column); 2632 st->print("; implicit exception: dispatches to " INTPTR_FORMAT, instructions_begin() + cont_offset); 2633 } 2634 2635 } 2636 2637 #ifndef PRODUCT 2638 2639 void nmethod::print_value_on(outputStream* st) const { 2640 print_on(st, "nmethod"); 2641 } 2642 2643 void nmethod::print_calls(outputStream* st) { 2644 RelocIterator iter(this); 2645 while (iter.next()) { 2646 switch (iter.type()) { 2647 case relocInfo::virtual_call_type: 2648 case relocInfo::opt_virtual_call_type: { 2649 VerifyMutexLocker mc(CompiledIC_lock); 2650 CompiledIC_at(iter.reloc())->print(); 2651 break; 2652 } 2653 case relocInfo::static_call_type: 2654 st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr()); 2655 compiledStaticCall_at(iter.reloc())->print(); 2656 break; 2657 } 2658 } 2659 } 2660 2661 void nmethod::print_handler_table() { 2662 ExceptionHandlerTable(this).print(); 2663 } 2664 2665 void nmethod::print_nul_chk_table() { 2666 ImplicitExceptionTable(this).print(instructions_begin()); 2667 } 2668 2669 void nmethod::print_statistics() { 2670 ttyLocker ttyl; 2671 if (xtty != NULL) xtty->head("statistics type='nmethod'"); 2672 nmethod_stats.print_native_nmethod_stats(); 2673 nmethod_stats.print_nmethod_stats(); 2674 DebugInformationRecorder::print_statistics(); 2675 nmethod_stats.print_pc_stats(); 2676 Dependencies::print_statistics(); 2677 if (xtty != NULL) xtty->tail("statistics"); 2678 } 2679 2680 #endif // PRODUCT