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