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