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