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