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