aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@1: /* aoqi@1: * This file has been modified by Loongson Technology in 2015. These aoqi@1: * modifications are Copyright (c) 2015 Loongson Technology, and are made aoqi@1: * available on the same license terms set forth above. aoqi@1: */ aoqi@1: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "code/compiledIC.hpp" aoqi@0: #include "code/dependencies.hpp" aoqi@0: #include "code/nmethod.hpp" aoqi@0: #include "code/scopeDesc.hpp" aoqi@0: #include "compiler/abstractCompiler.hpp" aoqi@0: #include "compiler/compileBroker.hpp" aoqi@0: #include "compiler/compileLog.hpp" aoqi@0: #include "compiler/compilerOracle.hpp" aoqi@0: #include "compiler/disassembler.hpp" aoqi@0: #include "interpreter/bytecode.hpp" aoqi@0: #include "oops/methodData.hpp" aoqi@0: #include "prims/jvmtiRedefineClassesTrace.hpp" aoqi@0: #include "prims/jvmtiImpl.hpp" aoqi@0: #include "runtime/sharedRuntime.hpp" aoqi@0: #include "runtime/sweeper.hpp" aoqi@0: #include "utilities/dtrace.hpp" aoqi@0: #include "utilities/events.hpp" aoqi@0: #include "utilities/xmlstream.hpp" aoqi@0: #ifdef SHARK aoqi@0: #include "shark/sharkCompiler.hpp" aoqi@0: #endif aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: #ifdef DTRACE_ENABLED aoqi@0: aoqi@0: // Only bother with this argument setup if dtrace is available aoqi@0: aoqi@0: #ifndef USDT2 aoqi@0: HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load, aoqi@0: const char*, int, const char*, int, const char*, int, void*, size_t); aoqi@0: aoqi@0: HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload, aoqi@0: char*, int, char*, int, char*, int); aoqi@0: aoqi@0: #define DTRACE_METHOD_UNLOAD_PROBE(method) \ aoqi@0: { \ aoqi@0: Method* m = (method); \ aoqi@0: if (m != NULL) { \ aoqi@0: Symbol* klass_name = m->klass_name(); \ aoqi@0: Symbol* name = m->name(); \ aoqi@0: Symbol* signature = m->signature(); \ aoqi@0: HS_DTRACE_PROBE6(hotspot, compiled__method__unload, \ aoqi@0: klass_name->bytes(), klass_name->utf8_length(), \ aoqi@0: name->bytes(), name->utf8_length(), \ aoqi@0: signature->bytes(), signature->utf8_length()); \ aoqi@0: } \ aoqi@0: } aoqi@0: #else /* USDT2 */ aoqi@0: #define DTRACE_METHOD_UNLOAD_PROBE(method) \ aoqi@0: { \ aoqi@0: Method* m = (method); \ aoqi@0: if (m != NULL) { \ aoqi@0: Symbol* klass_name = m->klass_name(); \ aoqi@0: Symbol* name = m->name(); \ aoqi@0: Symbol* signature = m->signature(); \ aoqi@0: HOTSPOT_COMPILED_METHOD_UNLOAD( \ aoqi@0: (char *) klass_name->bytes(), klass_name->utf8_length(), \ aoqi@0: (char *) name->bytes(), name->utf8_length(), \ aoqi@0: (char *) signature->bytes(), signature->utf8_length()); \ aoqi@0: } \ aoqi@0: } aoqi@0: #endif /* USDT2 */ aoqi@0: aoqi@0: #else // ndef DTRACE_ENABLED aoqi@0: aoqi@0: #define DTRACE_METHOD_UNLOAD_PROBE(method) aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: bool nmethod::is_compiled_by_c1() const { aoqi@0: if (compiler() == NULL) { aoqi@0: return false; aoqi@0: } aoqi@0: return compiler()->is_c1(); aoqi@0: } aoqi@0: bool nmethod::is_compiled_by_c2() const { aoqi@0: if (compiler() == NULL) { aoqi@0: return false; aoqi@0: } aoqi@0: return compiler()->is_c2(); aoqi@0: } aoqi@0: bool nmethod::is_compiled_by_shark() const { aoqi@0: if (compiler() == NULL) { aoqi@0: return false; aoqi@0: } aoqi@0: return compiler()->is_shark(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //--------------------------------------------------------------------------------- aoqi@0: // NMethod statistics aoqi@0: // They are printed under various flags, including: aoqi@0: // PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation. aoqi@0: // (In the latter two cases, they like other stats are printed to the log only.) aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // These variables are put into one block to reduce relocations aoqi@0: // and make it simpler to print from the debugger. aoqi@0: static aoqi@0: struct nmethod_stats_struct { aoqi@0: int nmethod_count; aoqi@0: int total_size; aoqi@0: int relocation_size; aoqi@0: int consts_size; aoqi@0: int insts_size; aoqi@0: int stub_size; aoqi@0: int scopes_data_size; aoqi@0: int scopes_pcs_size; aoqi@0: int dependencies_size; aoqi@0: int handler_table_size; aoqi@0: int nul_chk_table_size; aoqi@0: int oops_size; aoqi@0: aoqi@0: void note_nmethod(nmethod* nm) { aoqi@0: nmethod_count += 1; aoqi@0: total_size += nm->size(); aoqi@0: relocation_size += nm->relocation_size(); aoqi@0: consts_size += nm->consts_size(); aoqi@0: insts_size += nm->insts_size(); aoqi@0: stub_size += nm->stub_size(); aoqi@0: oops_size += nm->oops_size(); aoqi@0: scopes_data_size += nm->scopes_data_size(); aoqi@0: scopes_pcs_size += nm->scopes_pcs_size(); aoqi@0: dependencies_size += nm->dependencies_size(); aoqi@0: handler_table_size += nm->handler_table_size(); aoqi@0: nul_chk_table_size += nm->nul_chk_table_size(); aoqi@0: } aoqi@0: void print_nmethod_stats() { aoqi@0: if (nmethod_count == 0) return; aoqi@0: tty->print_cr("Statistics for %d bytecoded nmethods:", nmethod_count); aoqi@0: if (total_size != 0) tty->print_cr(" total in heap = %d", total_size); aoqi@0: if (relocation_size != 0) tty->print_cr(" relocation = %d", relocation_size); aoqi@0: if (consts_size != 0) tty->print_cr(" constants = %d", consts_size); aoqi@0: if (insts_size != 0) tty->print_cr(" main code = %d", insts_size); aoqi@0: if (stub_size != 0) tty->print_cr(" stub code = %d", stub_size); aoqi@0: if (oops_size != 0) tty->print_cr(" oops = %d", oops_size); aoqi@0: if (scopes_data_size != 0) tty->print_cr(" scopes data = %d", scopes_data_size); aoqi@0: if (scopes_pcs_size != 0) tty->print_cr(" scopes pcs = %d", scopes_pcs_size); aoqi@0: if (dependencies_size != 0) tty->print_cr(" dependencies = %d", dependencies_size); aoqi@0: if (handler_table_size != 0) tty->print_cr(" handler table = %d", handler_table_size); aoqi@0: if (nul_chk_table_size != 0) tty->print_cr(" nul chk table = %d", nul_chk_table_size); aoqi@0: } aoqi@0: aoqi@0: int native_nmethod_count; aoqi@0: int native_total_size; aoqi@0: int native_relocation_size; aoqi@0: int native_insts_size; aoqi@0: int native_oops_size; aoqi@0: void note_native_nmethod(nmethod* nm) { aoqi@0: native_nmethod_count += 1; aoqi@0: native_total_size += nm->size(); aoqi@0: native_relocation_size += nm->relocation_size(); aoqi@0: native_insts_size += nm->insts_size(); aoqi@0: native_oops_size += nm->oops_size(); aoqi@0: } aoqi@0: void print_native_nmethod_stats() { aoqi@0: if (native_nmethod_count == 0) return; aoqi@0: tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count); aoqi@0: if (native_total_size != 0) tty->print_cr(" N. total size = %d", native_total_size); aoqi@0: if (native_relocation_size != 0) tty->print_cr(" N. relocation = %d", native_relocation_size); aoqi@0: if (native_insts_size != 0) tty->print_cr(" N. main code = %d", native_insts_size); aoqi@0: if (native_oops_size != 0) tty->print_cr(" N. oops = %d", native_oops_size); aoqi@0: } aoqi@0: aoqi@0: int pc_desc_resets; // number of resets (= number of caches) aoqi@0: int pc_desc_queries; // queries to nmethod::find_pc_desc aoqi@0: int pc_desc_approx; // number of those which have approximate true aoqi@0: int pc_desc_repeats; // number of _pc_descs[0] hits aoqi@0: int pc_desc_hits; // number of LRU cache hits aoqi@0: int pc_desc_tests; // total number of PcDesc examinations aoqi@0: int pc_desc_searches; // total number of quasi-binary search steps aoqi@0: int pc_desc_adds; // number of LUR cache insertions aoqi@0: aoqi@0: void print_pc_stats() { aoqi@0: tty->print_cr("PcDesc Statistics: %d queries, %.2f comparisons per query", aoqi@0: pc_desc_queries, aoqi@0: (double)(pc_desc_tests + pc_desc_searches) aoqi@0: / pc_desc_queries); aoqi@0: tty->print_cr(" caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d", aoqi@0: pc_desc_resets, aoqi@0: pc_desc_queries, pc_desc_approx, aoqi@0: pc_desc_repeats, pc_desc_hits, aoqi@0: pc_desc_tests, pc_desc_searches, pc_desc_adds); aoqi@0: } aoqi@0: } nmethod_stats; aoqi@0: #endif //PRODUCT aoqi@0: aoqi@0: aoqi@0: //--------------------------------------------------------------------------------- aoqi@0: aoqi@0: aoqi@0: ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) { aoqi@0: assert(pc != NULL, "Must be non null"); aoqi@0: assert(exception.not_null(), "Must be non null"); aoqi@0: assert(handler != NULL, "Must be non null"); aoqi@0: aoqi@0: _count = 0; aoqi@0: _exception_type = exception->klass(); aoqi@0: _next = NULL; aoqi@0: aoqi@0: add_address_and_handler(pc,handler); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address ExceptionCache::match(Handle exception, address pc) { aoqi@0: assert(pc != NULL,"Must be non null"); aoqi@0: assert(exception.not_null(),"Must be non null"); aoqi@0: if (exception->klass() == exception_type()) { aoqi@0: return (test_address(pc)); aoqi@0: } aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool ExceptionCache::match_exception_with_space(Handle exception) { aoqi@0: assert(exception.not_null(),"Must be non null"); aoqi@0: if (exception->klass() == exception_type() && count() < cache_size) { aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address ExceptionCache::test_address(address addr) { aoqi@0: for (int i=0; imatch_exception_with_space(exception)) { aoqi@0: return ec; aoqi@0: } aoqi@0: ec = ec->next(); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //----------------------------------------------------------------------------- aoqi@0: aoqi@0: aoqi@0: // Helper used by both find_pc_desc methods. aoqi@0: static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) { aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_tests); aoqi@0: if (!approximate) aoqi@0: return pc->pc_offset() == pc_offset; aoqi@0: else aoqi@0: return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset(); aoqi@0: } aoqi@0: aoqi@0: void PcDescCache::reset_to(PcDesc* initial_pc_desc) { aoqi@0: if (initial_pc_desc == NULL) { aoqi@0: _pc_descs[0] = NULL; // native method; no PcDescs at all aoqi@0: return; aoqi@0: } aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_resets); aoqi@0: // reset the cache by filling it with benign (non-null) values aoqi@0: assert(initial_pc_desc->pc_offset() < 0, "must be sentinel"); aoqi@0: for (int i = 0; i < cache_size; i++) aoqi@0: _pc_descs[i] = initial_pc_desc; aoqi@0: } aoqi@0: aoqi@0: PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) { aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_queries); aoqi@0: NOT_PRODUCT(if (approximate) ++nmethod_stats.pc_desc_approx); aoqi@0: aoqi@0: // Note: one might think that caching the most recently aoqi@0: // read value separately would be a win, but one would be aoqi@0: // wrong. When many threads are updating it, the cache aoqi@0: // line it's in would bounce between caches, negating aoqi@0: // any benefit. aoqi@0: aoqi@0: // In order to prevent race conditions do not load cache elements aoqi@0: // repeatedly, but use a local copy: aoqi@0: PcDesc* res; aoqi@0: aoqi@0: // Step one: Check the most recently added value. aoqi@0: res = _pc_descs[0]; aoqi@0: if (res == NULL) return NULL; // native method; no PcDescs at all aoqi@0: if (match_desc(res, pc_offset, approximate)) { aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_repeats); aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: // Step two: Check the rest of the LRU cache. aoqi@0: for (int i = 1; i < cache_size; ++i) { aoqi@0: res = _pc_descs[i]; aoqi@0: if (res->pc_offset() < 0) break; // optimization: skip empty cache aoqi@0: if (match_desc(res, pc_offset, approximate)) { aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_hits); aoqi@0: return res; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Report failure. aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: void PcDescCache::add_pc_desc(PcDesc* pc_desc) { aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_adds); aoqi@0: // Update the LRU cache by shifting pc_desc forward. aoqi@0: for (int i = 0; i < cache_size; i++) { aoqi@0: PcDesc* next = _pc_descs[i]; aoqi@0: _pc_descs[i] = pc_desc; aoqi@0: pc_desc = next; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // adjust pcs_size so that it is a multiple of both oopSize and aoqi@0: // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple aoqi@0: // of oopSize, then 2*sizeof(PcDesc) is) aoqi@0: static int adjust_pcs_size(int pcs_size) { aoqi@0: int nsize = round_to(pcs_size, oopSize); aoqi@0: if ((nsize % sizeof(PcDesc)) != 0) { aoqi@0: nsize = pcs_size + sizeof(PcDesc); aoqi@0: } aoqi@0: assert((nsize % oopSize) == 0, "correct alignment"); aoqi@0: return nsize; aoqi@0: } aoqi@0: aoqi@0: //----------------------------------------------------------------------------- aoqi@0: aoqi@0: aoqi@0: void nmethod::add_exception_cache_entry(ExceptionCache* new_entry) { aoqi@0: assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock"); aoqi@0: assert(new_entry != NULL,"Must be non null"); aoqi@0: assert(new_entry->next() == NULL, "Must be null"); aoqi@0: aoqi@0: if (exception_cache() != NULL) { aoqi@0: new_entry->set_next(exception_cache()); aoqi@0: } aoqi@0: set_exception_cache(new_entry); aoqi@0: } aoqi@0: aoqi@0: void nmethod::remove_from_exception_cache(ExceptionCache* ec) { aoqi@0: ExceptionCache* prev = NULL; aoqi@0: ExceptionCache* curr = exception_cache(); aoqi@0: assert(curr != NULL, "nothing to remove"); aoqi@0: // find the previous and next entry of ec aoqi@0: while (curr != ec) { aoqi@0: prev = curr; aoqi@0: curr = curr->next(); aoqi@0: assert(curr != NULL, "ExceptionCache not found"); aoqi@0: } aoqi@0: // now: curr == ec aoqi@0: ExceptionCache* next = curr->next(); aoqi@0: if (prev == NULL) { aoqi@0: set_exception_cache(next); aoqi@0: } else { aoqi@0: prev->set_next(next); aoqi@0: } aoqi@0: delete curr; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // public method for accessing the exception cache aoqi@0: // These are the public access methods. aoqi@0: address nmethod::handler_for_exception_and_pc(Handle exception, address pc) { aoqi@0: // We never grab a lock to read the exception cache, so we may aoqi@0: // have false negatives. This is okay, as it can only happen during aoqi@0: // the first few exception lookups for a given nmethod. aoqi@0: ExceptionCache* ec = exception_cache(); aoqi@0: while (ec != NULL) { aoqi@0: address ret_val; aoqi@0: if ((ret_val = ec->match(exception,pc)) != NULL) { aoqi@0: return ret_val; aoqi@0: } aoqi@0: ec = ec->next(); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) { aoqi@0: // There are potential race conditions during exception cache updates, so we aoqi@0: // must own the ExceptionCache_lock before doing ANY modifications. Because aoqi@0: // we don't lock during reads, it is possible to have several threads attempt aoqi@0: // to update the cache with the same data. We need to check for already inserted aoqi@0: // copies of the current data before adding it. aoqi@0: aoqi@0: MutexLocker ml(ExceptionCache_lock); aoqi@0: ExceptionCache* target_entry = exception_cache_entry_for_exception(exception); aoqi@0: aoqi@0: if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) { aoqi@0: target_entry = new ExceptionCache(exception,pc,handler); aoqi@0: add_exception_cache_entry(target_entry); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //-------------end of code for ExceptionCache-------------- aoqi@0: aoqi@0: aoqi@0: int nmethod::total_size() const { aoqi@0: return aoqi@0: consts_size() + aoqi@0: insts_size() + aoqi@0: stub_size() + aoqi@0: scopes_data_size() + aoqi@0: scopes_pcs_size() + aoqi@0: handler_table_size() + aoqi@0: nul_chk_table_size(); aoqi@0: } aoqi@0: aoqi@0: const char* nmethod::compile_kind() const { aoqi@0: if (is_osr_method()) return "osr"; aoqi@0: if (method() != NULL && is_native_method()) return "c2n"; aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Fill in default values for various flag fields aoqi@0: void nmethod::init_defaults() { aoqi@0: _state = in_use; aoqi@0: _marked_for_reclamation = 0; aoqi@0: _has_flushed_dependencies = 0; aoqi@0: _has_unsafe_access = 0; aoqi@0: _has_method_handle_invokes = 0; aoqi@0: _lazy_critical_native = 0; aoqi@0: _has_wide_vectors = 0; aoqi@0: _marked_for_deoptimization = 0; aoqi@0: _lock_count = 0; aoqi@0: _stack_traversal_mark = 0; aoqi@0: _unload_reported = false; // jvmti state aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: _oops_are_stale = false; aoqi@0: #endif aoqi@0: aoqi@0: _oops_do_mark_link = NULL; aoqi@0: _jmethod_id = NULL; aoqi@0: _osr_link = NULL; aoqi@0: _scavenge_root_link = NULL; aoqi@0: _scavenge_root_state = 0; aoqi@0: _compiler = NULL; aoqi@0: #if INCLUDE_RTM_OPT aoqi@0: _rtm_state = NoRTM; aoqi@0: #endif aoqi@0: #ifdef HAVE_DTRACE_H aoqi@0: _trap_offset = 0; aoqi@0: #endif // def HAVE_DTRACE_H aoqi@0: } aoqi@0: aoqi@0: nmethod* nmethod::new_native_nmethod(methodHandle method, aoqi@0: int compile_id, aoqi@0: CodeBuffer *code_buffer, aoqi@0: int vep_offset, aoqi@0: int frame_complete, aoqi@0: int frame_size, aoqi@0: ByteSize basic_lock_owner_sp_offset, aoqi@0: ByteSize basic_lock_sp_offset, aoqi@0: OopMapSet* oop_maps) { aoqi@0: code_buffer->finalize_oop_references(method); aoqi@0: // create nmethod aoqi@0: nmethod* nm = NULL; aoqi@0: { aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); aoqi@0: CodeOffsets offsets; aoqi@0: offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); aoqi@0: offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); aoqi@0: nm = new (native_nmethod_size) nmethod(method(), native_nmethod_size, aoqi@0: compile_id, &offsets, aoqi@0: code_buffer, frame_size, aoqi@0: basic_lock_owner_sp_offset, aoqi@0: basic_lock_sp_offset, oop_maps); aoqi@0: NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_native_nmethod(nm)); aoqi@0: if (PrintAssembly && nm != NULL) { aoqi@0: Disassembler::decode(nm); aoqi@0: } aoqi@0: } aoqi@0: // verify nmethod aoqi@0: debug_only(if (nm) nm->verify();) // might block aoqi@0: aoqi@0: if (nm != NULL) { aoqi@0: nm->log_new_nmethod(); aoqi@0: } aoqi@0: aoqi@0: return nm; aoqi@0: } aoqi@0: aoqi@0: #ifdef HAVE_DTRACE_H aoqi@0: nmethod* nmethod::new_dtrace_nmethod(methodHandle method, aoqi@0: CodeBuffer *code_buffer, aoqi@0: int vep_offset, aoqi@0: int trap_offset, aoqi@0: int frame_complete, aoqi@0: int frame_size) { aoqi@0: code_buffer->finalize_oop_references(method); aoqi@0: // create nmethod aoqi@0: nmethod* nm = NULL; aoqi@0: { aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: int nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); aoqi@0: CodeOffsets offsets; aoqi@0: offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); aoqi@0: offsets.set_value(CodeOffsets::Dtrace_trap, trap_offset); aoqi@0: offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); aoqi@0: aoqi@0: nm = new (nmethod_size) nmethod(method(), nmethod_size, aoqi@0: &offsets, code_buffer, frame_size); aoqi@0: aoqi@0: NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_nmethod(nm)); aoqi@0: if (PrintAssembly && nm != NULL) { aoqi@0: Disassembler::decode(nm); aoqi@0: } aoqi@0: } aoqi@0: // verify nmethod aoqi@0: debug_only(if (nm) nm->verify();) // might block aoqi@0: aoqi@0: if (nm != NULL) { aoqi@0: nm->log_new_nmethod(); aoqi@0: } aoqi@0: aoqi@0: return nm; aoqi@0: } aoqi@0: aoqi@0: #endif // def HAVE_DTRACE_H aoqi@0: aoqi@0: nmethod* nmethod::new_nmethod(methodHandle method, aoqi@0: int compile_id, aoqi@0: int entry_bci, aoqi@0: CodeOffsets* offsets, aoqi@0: int orig_pc_offset, aoqi@0: DebugInformationRecorder* debug_info, aoqi@0: Dependencies* dependencies, aoqi@0: CodeBuffer* code_buffer, int frame_size, aoqi@0: OopMapSet* oop_maps, aoqi@0: ExceptionHandlerTable* handler_table, aoqi@0: ImplicitExceptionTable* nul_chk_table, aoqi@0: AbstractCompiler* compiler, aoqi@0: int comp_level aoqi@0: ) aoqi@0: { aoqi@0: assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); aoqi@0: code_buffer->finalize_oop_references(method); aoqi@0: // create nmethod aoqi@0: nmethod* nm = NULL; aoqi@0: { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: int nmethod_size = aoqi@0: allocation_size(code_buffer, sizeof(nmethod)) aoqi@0: + adjust_pcs_size(debug_info->pcs_size()) aoqi@0: + round_to(dependencies->size_in_bytes() , oopSize) aoqi@0: + round_to(handler_table->size_in_bytes(), oopSize) aoqi@0: + round_to(nul_chk_table->size_in_bytes(), oopSize) aoqi@0: + round_to(debug_info->data_size() , oopSize); aoqi@0: aoqi@0: nm = new (nmethod_size) aoqi@0: nmethod(method(), nmethod_size, compile_id, entry_bci, offsets, aoqi@0: orig_pc_offset, debug_info, dependencies, code_buffer, frame_size, aoqi@0: oop_maps, aoqi@0: handler_table, aoqi@0: nul_chk_table, aoqi@0: compiler, aoqi@0: comp_level); aoqi@0: aoqi@0: if (nm != NULL) { aoqi@0: // To make dependency checking during class loading fast, record aoqi@0: // the nmethod dependencies in the classes it is dependent on. aoqi@0: // This allows the dependency checking code to simply walk the aoqi@0: // class hierarchy above the loaded class, checking only nmethods aoqi@0: // which are dependent on those classes. The slow way is to aoqi@0: // check every nmethod for dependencies which makes it linear in aoqi@0: // the number of methods compiled. For applications with a lot aoqi@0: // classes the slow way is too slow. aoqi@0: for (Dependencies::DepStream deps(nm); deps.next(); ) { aoqi@0: Klass* klass = deps.context_type(); aoqi@0: if (klass == NULL) { aoqi@0: continue; // ignore things like evol_method aoqi@0: } aoqi@0: aoqi@0: // record this nmethod as dependent on this klass aoqi@0: InstanceKlass::cast(klass)->add_dependent_nmethod(nm); aoqi@0: } aoqi@0: NOT_PRODUCT(nmethod_stats.note_nmethod(nm)); aoqi@0: if (PrintAssembly || CompilerOracle::has_option_string(method, "PrintAssembly")) { aoqi@0: Disassembler::decode(nm); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // Do verification and logging outside CodeCache_lock. aoqi@0: if (nm != NULL) { aoqi@0: // Safepoints in nmethod::verify aren't allowed because nm hasn't been installed yet. aoqi@0: DEBUG_ONLY(nm->verify();) aoqi@0: nm->log_new_nmethod(); aoqi@0: } aoqi@0: return nm; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // For native wrappers aoqi@0: nmethod::nmethod( aoqi@0: Method* method, aoqi@0: int nmethod_size, aoqi@0: int compile_id, aoqi@0: CodeOffsets* offsets, aoqi@0: CodeBuffer* code_buffer, aoqi@0: int frame_size, aoqi@0: ByteSize basic_lock_owner_sp_offset, aoqi@0: ByteSize basic_lock_sp_offset, aoqi@0: OopMapSet* oop_maps ) aoqi@0: : CodeBlob("native nmethod", code_buffer, sizeof(nmethod), aoqi@0: nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), aoqi@0: _native_receiver_sp_offset(basic_lock_owner_sp_offset), aoqi@0: _native_basic_lock_sp_offset(basic_lock_sp_offset) aoqi@0: { aoqi@0: { aoqi@0: debug_only(No_Safepoint_Verifier nsv;) aoqi@0: assert_locked_or_safepoint(CodeCache_lock); aoqi@0: aoqi@0: init_defaults(); aoqi@0: _method = method; aoqi@0: _entry_bci = InvocationEntryBci; aoqi@0: // We have no exception handler or deopt handler make the aoqi@0: // values something that will never match a pc like the nmethod vtable entry aoqi@0: _exception_offset = 0; aoqi@0: _deoptimize_offset = 0; aoqi@0: _deoptimize_mh_offset = 0; aoqi@0: _orig_pc_offset = 0; aoqi@0: aoqi@0: _consts_offset = data_offset(); aoqi@0: _stub_offset = data_offset(); aoqi@0: _oops_offset = data_offset(); aoqi@0: _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); aoqi@0: _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); aoqi@0: _scopes_pcs_offset = _scopes_data_offset; aoqi@0: _dependencies_offset = _scopes_pcs_offset; aoqi@0: _handler_table_offset = _dependencies_offset; aoqi@0: _nul_chk_table_offset = _handler_table_offset; aoqi@0: _nmethod_end_offset = _nul_chk_table_offset; aoqi@0: _compile_id = compile_id; aoqi@0: _comp_level = CompLevel_none; aoqi@0: _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); aoqi@0: _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); aoqi@0: _osr_entry_point = NULL; aoqi@0: _exception_cache = NULL; aoqi@0: _pc_desc_cache.reset_to(NULL); aoqi@0: _hotness_counter = NMethodSweeper::hotness_counter_reset_val(); aoqi@0: aoqi@0: code_buffer->copy_values_to(this); aoqi@0: if (ScavengeRootsInCode && detect_scavenge_root_oops()) { aoqi@0: CodeCache::add_scavenge_root_nmethod(this); aoqi@0: Universe::heap()->register_nmethod(this); aoqi@0: } aoqi@0: debug_only(verify_scavenge_root_oops()); aoqi@0: CodeCache::commit(this); aoqi@0: } aoqi@0: aoqi@0: if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { aoqi@0: ttyLocker ttyl; // keep the following output all in one block aoqi@0: // This output goes directly to the tty, not the compiler log. aoqi@0: // To enable tools to match it up with the compilation activity, aoqi@0: // be sure to tag this tty output with the compile ID. aoqi@0: if (xtty != NULL) { aoqi@0: xtty->begin_head("print_native_nmethod"); aoqi@0: xtty->method(_method); aoqi@0: xtty->stamp(); aoqi@0: xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); aoqi@0: } aoqi@0: // print the header part first aoqi@0: print(); aoqi@0: // then print the requested information aoqi@0: if (PrintNativeNMethods) { aoqi@0: print_code(); aoqi@0: if (oop_maps != NULL) { aoqi@0: oop_maps->print(); aoqi@0: } aoqi@0: } aoqi@0: if (PrintRelocations) { aoqi@0: print_relocations(); aoqi@0: } aoqi@0: if (xtty != NULL) { aoqi@0: xtty->tail("print_native_nmethod"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // For dtrace wrappers aoqi@0: #ifdef HAVE_DTRACE_H aoqi@0: nmethod::nmethod( aoqi@0: Method* method, aoqi@0: int nmethod_size, aoqi@0: CodeOffsets* offsets, aoqi@0: CodeBuffer* code_buffer, aoqi@0: int frame_size) aoqi@0: : CodeBlob("dtrace nmethod", code_buffer, sizeof(nmethod), aoqi@0: nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, NULL), aoqi@0: _native_receiver_sp_offset(in_ByteSize(-1)), aoqi@0: _native_basic_lock_sp_offset(in_ByteSize(-1)) aoqi@0: { aoqi@0: { aoqi@0: debug_only(No_Safepoint_Verifier nsv;) aoqi@0: assert_locked_or_safepoint(CodeCache_lock); aoqi@0: aoqi@0: init_defaults(); aoqi@0: _method = method; aoqi@0: _entry_bci = InvocationEntryBci; aoqi@0: // We have no exception handler or deopt handler make the aoqi@0: // values something that will never match a pc like the nmethod vtable entry aoqi@0: _exception_offset = 0; aoqi@0: _deoptimize_offset = 0; aoqi@0: _deoptimize_mh_offset = 0; aoqi@0: _unwind_handler_offset = -1; aoqi@0: _trap_offset = offsets->value(CodeOffsets::Dtrace_trap); aoqi@0: _orig_pc_offset = 0; aoqi@0: _consts_offset = data_offset(); aoqi@0: _stub_offset = data_offset(); aoqi@0: _oops_offset = data_offset(); aoqi@0: _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); aoqi@0: _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); aoqi@0: _scopes_pcs_offset = _scopes_data_offset; aoqi@0: _dependencies_offset = _scopes_pcs_offset; aoqi@0: _handler_table_offset = _dependencies_offset; aoqi@0: _nul_chk_table_offset = _handler_table_offset; aoqi@0: _nmethod_end_offset = _nul_chk_table_offset; aoqi@0: _compile_id = 0; // default aoqi@0: _comp_level = CompLevel_none; aoqi@0: _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); aoqi@0: _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); aoqi@0: _osr_entry_point = NULL; aoqi@0: _exception_cache = NULL; aoqi@0: _pc_desc_cache.reset_to(NULL); aoqi@0: _hotness_counter = NMethodSweeper::hotness_counter_reset_val(); aoqi@0: aoqi@0: code_buffer->copy_values_to(this); aoqi@0: if (ScavengeRootsInCode && detect_scavenge_root_oops()) { aoqi@0: CodeCache::add_scavenge_root_nmethod(this); aoqi@0: Universe::heap()->register_nmethod(this); aoqi@0: } aoqi@0: DEBUG_ONLY(verify_scavenge_root_oops();) aoqi@0: CodeCache::commit(this); aoqi@0: } aoqi@0: aoqi@0: if (PrintNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { aoqi@0: ttyLocker ttyl; // keep the following output all in one block aoqi@0: // This output goes directly to the tty, not the compiler log. aoqi@0: // To enable tools to match it up with the compilation activity, aoqi@0: // be sure to tag this tty output with the compile ID. aoqi@0: if (xtty != NULL) { aoqi@0: xtty->begin_head("print_dtrace_nmethod"); aoqi@0: xtty->method(_method); aoqi@0: xtty->stamp(); aoqi@0: xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); aoqi@0: } aoqi@0: // print the header part first aoqi@0: print(); aoqi@0: // then print the requested information aoqi@0: if (PrintNMethods) { aoqi@0: print_code(); aoqi@0: } aoqi@0: if (PrintRelocations) { aoqi@0: print_relocations(); aoqi@0: } aoqi@0: if (xtty != NULL) { aoqi@0: xtty->tail("print_dtrace_nmethod"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif // def HAVE_DTRACE_H aoqi@0: aoqi@0: void* nmethod::operator new(size_t size, int nmethod_size) throw() { aoqi@0: // Not critical, may return null if there is too little continuous memory aoqi@0: return CodeCache::allocate(nmethod_size); aoqi@0: } aoqi@0: aoqi@0: nmethod::nmethod( aoqi@0: Method* method, aoqi@0: int nmethod_size, aoqi@0: int compile_id, aoqi@0: int entry_bci, aoqi@0: CodeOffsets* offsets, aoqi@0: int orig_pc_offset, aoqi@0: DebugInformationRecorder* debug_info, aoqi@0: Dependencies* dependencies, aoqi@0: CodeBuffer *code_buffer, aoqi@0: int frame_size, aoqi@0: OopMapSet* oop_maps, aoqi@0: ExceptionHandlerTable* handler_table, aoqi@0: ImplicitExceptionTable* nul_chk_table, aoqi@0: AbstractCompiler* compiler, aoqi@0: int comp_level aoqi@0: ) aoqi@0: : CodeBlob("nmethod", code_buffer, sizeof(nmethod), aoqi@0: nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), aoqi@0: _native_receiver_sp_offset(in_ByteSize(-1)), aoqi@0: _native_basic_lock_sp_offset(in_ByteSize(-1)) aoqi@0: { aoqi@0: assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); aoqi@0: { aoqi@0: debug_only(No_Safepoint_Verifier nsv;) aoqi@0: assert_locked_or_safepoint(CodeCache_lock); aoqi@0: aoqi@0: init_defaults(); aoqi@0: _method = method; aoqi@0: _entry_bci = entry_bci; aoqi@0: _compile_id = compile_id; aoqi@0: _comp_level = comp_level; aoqi@0: _compiler = compiler; aoqi@0: _orig_pc_offset = orig_pc_offset; aoqi@0: _hotness_counter = NMethodSweeper::hotness_counter_reset_val(); aoqi@0: aoqi@0: // Section offsets aoqi@0: _consts_offset = content_offset() + code_buffer->total_offset_of(code_buffer->consts()); aoqi@0: _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs()); aoqi@0: aoqi@0: // Exception handler and deopt handler are in the stub section aoqi@0: assert(offsets->value(CodeOffsets::Exceptions) != -1, "must be set"); aoqi@0: assert(offsets->value(CodeOffsets::Deopt ) != -1, "must be set"); aoqi@0: _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions); aoqi@0: _deoptimize_offset = _stub_offset + offsets->value(CodeOffsets::Deopt); aoqi@0: if (offsets->value(CodeOffsets::DeoptMH) != -1) { aoqi@0: _deoptimize_mh_offset = _stub_offset + offsets->value(CodeOffsets::DeoptMH); aoqi@0: } else { aoqi@0: _deoptimize_mh_offset = -1; aoqi@0: } aoqi@0: if (offsets->value(CodeOffsets::UnwindHandler) != -1) { aoqi@0: _unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler); aoqi@0: } else { aoqi@0: _unwind_handler_offset = -1; aoqi@0: } aoqi@0: aoqi@0: _oops_offset = data_offset(); aoqi@0: _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); aoqi@0: _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); aoqi@0: aoqi@0: _scopes_pcs_offset = _scopes_data_offset + round_to(debug_info->data_size (), oopSize); aoqi@0: _dependencies_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size()); aoqi@0: _handler_table_offset = _dependencies_offset + round_to(dependencies->size_in_bytes (), oopSize); aoqi@0: _nul_chk_table_offset = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize); aoqi@0: _nmethod_end_offset = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize); aoqi@0: aoqi@0: _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); aoqi@0: _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); aoqi@0: _osr_entry_point = code_begin() + offsets->value(CodeOffsets::OSR_Entry); aoqi@0: _exception_cache = NULL; aoqi@0: _pc_desc_cache.reset_to(scopes_pcs_begin()); aoqi@0: aoqi@0: // Copy contents of ScopeDescRecorder to nmethod aoqi@0: code_buffer->copy_values_to(this); aoqi@0: debug_info->copy_to(this); aoqi@0: dependencies->copy_to(this); aoqi@0: if (ScavengeRootsInCode && detect_scavenge_root_oops()) { aoqi@0: CodeCache::add_scavenge_root_nmethod(this); aoqi@0: Universe::heap()->register_nmethod(this); aoqi@0: } aoqi@0: debug_only(verify_scavenge_root_oops()); aoqi@0: aoqi@0: CodeCache::commit(this); aoqi@0: aoqi@0: // Copy contents of ExceptionHandlerTable to nmethod aoqi@0: handler_table->copy_to(this); aoqi@0: nul_chk_table->copy_to(this); aoqi@0: aoqi@0: // we use the information of entry points to find out if a method is aoqi@0: // static or non static aoqi@0: assert(compiler->is_c2() || aoqi@0: _method->is_static() == (entry_point() == _verified_entry_point), aoqi@0: " entry points must be same for static methods and vice versa"); aoqi@0: } aoqi@0: aoqi@0: bool printnmethods = PrintNMethods aoqi@0: || CompilerOracle::should_print(_method) aoqi@0: || CompilerOracle::has_option_string(_method, "PrintNMethods"); aoqi@0: if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) { aoqi@0: print_nmethod(printnmethods); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Print a short set of xml attributes to identify this nmethod. The aoqi@0: // output should be embedded in some other element. aoqi@0: void nmethod::log_identity(xmlStream* log) const { aoqi@0: log->print(" compile_id='%d'", compile_id()); aoqi@0: const char* nm_kind = compile_kind(); aoqi@0: if (nm_kind != NULL) log->print(" compile_kind='%s'", nm_kind); aoqi@0: if (compiler() != NULL) { aoqi@0: log->print(" compiler='%s'", compiler()->name()); aoqi@0: } aoqi@0: if (TieredCompilation) { aoqi@0: log->print(" level='%d'", comp_level()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #define LOG_OFFSET(log, name) \ aoqi@0: if ((intptr_t)name##_end() - (intptr_t)name##_begin()) \ aoqi@0: log->print(" " XSTR(name) "_offset='%d'" , \ aoqi@0: (intptr_t)name##_begin() - (intptr_t)this) aoqi@0: aoqi@0: aoqi@0: void nmethod::log_new_nmethod() const { aoqi@0: if (LogCompilation && xtty != NULL) { aoqi@0: ttyLocker ttyl; aoqi@0: HandleMark hm; aoqi@0: xtty->begin_elem("nmethod"); aoqi@0: log_identity(xtty); aoqi@0: xtty->print(" entry='" INTPTR_FORMAT "' size='%d'", code_begin(), size()); aoqi@0: xtty->print(" address='" INTPTR_FORMAT "'", (intptr_t) this); aoqi@0: aoqi@0: LOG_OFFSET(xtty, relocation); aoqi@0: LOG_OFFSET(xtty, consts); aoqi@0: LOG_OFFSET(xtty, insts); aoqi@0: LOG_OFFSET(xtty, stub); aoqi@0: LOG_OFFSET(xtty, scopes_data); aoqi@0: LOG_OFFSET(xtty, scopes_pcs); aoqi@0: LOG_OFFSET(xtty, dependencies); aoqi@0: LOG_OFFSET(xtty, handler_table); aoqi@0: LOG_OFFSET(xtty, nul_chk_table); aoqi@0: LOG_OFFSET(xtty, oops); aoqi@0: aoqi@0: xtty->method(method()); aoqi@0: xtty->stamp(); aoqi@0: xtty->end_elem(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #undef LOG_OFFSET aoqi@0: aoqi@0: aoqi@0: // Print out more verbose output usually for a newly created nmethod. aoqi@0: void nmethod::print_on(outputStream* st, const char* msg) const { aoqi@0: if (st != NULL) { aoqi@0: ttyLocker ttyl; aoqi@0: if (WizardMode) { aoqi@0: CompileTask::print_compilation(st, this, msg, /*short_form:*/ true); aoqi@0: st->print_cr(" (" INTPTR_FORMAT ")", this); aoqi@0: } else { aoqi@0: CompileTask::print_compilation(st, this, msg, /*short_form:*/ false); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::print_nmethod(bool printmethod) { aoqi@0: ttyLocker ttyl; // keep the following output all in one block aoqi@0: if (xtty != NULL) { aoqi@0: xtty->begin_head("print_nmethod"); aoqi@0: xtty->stamp(); aoqi@0: xtty->end_head(); aoqi@0: } aoqi@0: // print the header part first aoqi@0: print(); aoqi@0: // then print the requested information aoqi@0: if (printmethod) { aoqi@0: print_code(); aoqi@0: print_pcs(); aoqi@0: if (oop_maps()) { aoqi@0: oop_maps()->print(); aoqi@0: } aoqi@0: } aoqi@0: if (PrintDebugInfo) { aoqi@0: print_scopes(); aoqi@0: } aoqi@0: if (PrintRelocations) { aoqi@0: print_relocations(); aoqi@0: } aoqi@0: if (PrintDependencies) { aoqi@0: print_dependencies(); aoqi@0: } aoqi@0: if (PrintExceptionHandlers) { aoqi@0: print_handler_table(); aoqi@0: print_nul_chk_table(); aoqi@0: } aoqi@0: if (xtty != NULL) { aoqi@0: xtty->tail("print_nmethod"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Promote one word from an assembly-time handle to a live embedded oop. aoqi@0: inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) { aoqi@0: if (handle == NULL || aoqi@0: // As a special case, IC oops are initialized to 1 or -1. aoqi@0: handle == (jobject) Universe::non_oop_word()) { aoqi@0: (*dest) = (oop) handle; aoqi@0: } else { aoqi@0: (*dest) = JNIHandles::resolve_non_null(handle); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Have to have the same name because it's called by a template aoqi@0: void nmethod::copy_values(GrowableArray* array) { aoqi@0: int length = array->length(); aoqi@0: assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough"); aoqi@0: oop* dest = oops_begin(); aoqi@0: for (int index = 0 ; index < length; index++) { aoqi@0: initialize_immediate_oop(&dest[index], array->at(index)); aoqi@0: } aoqi@0: aoqi@0: // Now we can fix up all the oops in the code. We need to do this aoqi@0: // in the code because the assembler uses jobjects as placeholders. aoqi@0: // The code and relocations have already been initialized by the aoqi@0: // CodeBlob constructor, so it is valid even at this early point to aoqi@0: // iterate over relocations and patch the code. aoqi@0: fix_oop_relocations(NULL, NULL, /*initialize_immediates=*/ true); aoqi@0: } aoqi@0: aoqi@0: void nmethod::copy_values(GrowableArray* array) { aoqi@0: int length = array->length(); aoqi@0: assert((address)(metadata_begin() + length) <= (address)metadata_end(), "big enough"); aoqi@0: Metadata** dest = metadata_begin(); aoqi@0: for (int index = 0 ; index < length; index++) { aoqi@0: dest[index] = array->at(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool nmethod::is_at_poll_return(address pc) { aoqi@0: RelocIterator iter(this, pc, pc+1); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::poll_return_type) aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool nmethod::is_at_poll_or_poll_return(address pc) { aoqi@0: RelocIterator iter(this, pc, pc+1); aoqi@0: while (iter.next()) { aoqi@0: relocInfo::relocType t = iter.type(); aoqi@0: if (t == relocInfo::poll_return_type || t == relocInfo::poll_type) aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::fix_oop_relocations(address begin, address end, bool initialize_immediates) { aoqi@0: // re-patch all oop-bearing instructions, just in case some oops moved aoqi@0: RelocIterator iter(this, begin, end); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::oop_type) { aoqi@0: oop_Relocation* reloc = iter.oop_reloc(); aoqi@0: if (initialize_immediates && reloc->oop_is_immediate()) { aoqi@0: oop* dest = reloc->oop_addr(); aoqi@0: initialize_immediate_oop(dest, (jobject) *dest); aoqi@0: } aoqi@0: // Refresh the oop-related bits of this instruction. aoqi@0: reloc->fix_oop_relocation(); aoqi@0: } else if (iter.type() == relocInfo::metadata_type) { aoqi@0: metadata_Relocation* reloc = iter.metadata_reloc(); aoqi@0: reloc->fix_metadata_relocation(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::verify_oop_relocations() { aoqi@0: // Ensure sure that the code matches the current oop values aoqi@0: RelocIterator iter(this, NULL, NULL); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::oop_type) { aoqi@0: oop_Relocation* reloc = iter.oop_reloc(); aoqi@0: if (!reloc->oop_is_immediate()) { aoqi@0: reloc->verify_oop_relocation(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: ScopeDesc* nmethod::scope_desc_at(address pc) { aoqi@0: PcDesc* pd = pc_desc_at(pc); aoqi@0: guarantee(pd != NULL, "scope must be present"); aoqi@0: return new ScopeDesc(this, pd->scope_decode_offset(), aoqi@0: pd->obj_decode_offset(), pd->should_reexecute(), aoqi@0: pd->return_oop()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::clear_inline_caches() { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint"); aoqi@0: if (is_zombie()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: RelocIterator iter(this); aoqi@0: while (iter.next()) { aoqi@0: iter.reloc()->clear_inline_cache(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::cleanup_inline_caches() { aoqi@0: aoqi@0: assert_locked_or_safepoint(CompiledIC_lock); aoqi@0: aoqi@0: // If the method is not entrant or zombie then a JMP is plastered over the aoqi@0: // first few bytes. If an oop in the old code was there, that oop aoqi@0: // should not get GC'd. Skip the first few bytes of oops on aoqi@0: // not-entrant methods. aoqi@0: address low_boundary = verified_entry_point(); aoqi@0: if (!is_in_use()) { aoqi@1: low_boundary += NOT_MIPS64(NativeJump)MIPS64_ONLY(NativeGeneralJump)::instruction_size; aoqi@0: // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. aoqi@0: // This means that the low_boundary is going to be a little too high. aoqi@0: // This shouldn't matter, since oops of non-entrant methods are never used. aoqi@0: // In fact, why are we bothering to look at oops in a non-entrant method?? aoqi@0: } aoqi@0: aoqi@0: // Find all calls in an nmethod, and clear the ones that points to zombie methods aoqi@0: ResourceMark rm; aoqi@0: RelocIterator iter(this, low_boundary); aoqi@0: while(iter.next()) { aoqi@0: switch(iter.type()) { aoqi@0: case relocInfo::virtual_call_type: aoqi@0: case relocInfo::opt_virtual_call_type: { aoqi@0: CompiledIC *ic = CompiledIC_at(iter.reloc()); aoqi@0: // Ok, to lookup references to zombies here aoqi@0: CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination()); aoqi@0: if( cb != NULL && cb->is_nmethod() ) { aoqi@0: nmethod* nm = (nmethod*)cb; aoqi@0: // Clean inline caches pointing to both zombie and not_entrant methods aoqi@0: if (!nm->is_in_use() || (nm->method()->code() != nm)) ic->set_to_clean(); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::static_call_type: { aoqi@0: CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc()); aoqi@0: CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination()); aoqi@0: if( cb != NULL && cb->is_nmethod() ) { aoqi@0: nmethod* nm = (nmethod*)cb; aoqi@0: // Clean inline caches pointing to both zombie and not_entrant methods aoqi@0: if (!nm->is_in_use() || (nm->method()->code() != nm)) csc->set_to_clean(); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // This is a private interface with the sweeper. aoqi@0: void nmethod::mark_as_seen_on_stack() { aoqi@0: assert(is_alive(), "Must be an alive method"); aoqi@0: // Set the traversal mark to ensure that the sweeper does 2 aoqi@0: // cleaning passes before moving to zombie. aoqi@0: set_stack_traversal_mark(NMethodSweeper::traversal_count()); aoqi@0: } aoqi@0: aoqi@0: // Tell if a non-entrant method can be converted to a zombie (i.e., aoqi@0: // there are no activations on the stack, not in use by the VM, aoqi@0: // and not in use by the ServiceThread) aoqi@0: bool nmethod::can_not_entrant_be_converted() { aoqi@0: assert(is_not_entrant(), "must be a non-entrant method"); aoqi@0: aoqi@0: // Since the nmethod sweeper only does partial sweep the sweeper's traversal aoqi@0: // count can be greater than the stack traversal count before it hits the aoqi@0: // nmethod for the second time. aoqi@0: return stack_traversal_mark()+1 < NMethodSweeper::traversal_count() && aoqi@0: !is_locked_by_vm(); aoqi@0: } aoqi@0: aoqi@0: void nmethod::inc_decompile_count() { aoqi@0: if (!is_compiled_by_c2()) return; aoqi@0: // Could be gated by ProfileTraps, but do not bother... aoqi@0: Method* m = method(); aoqi@0: if (m == NULL) return; aoqi@0: MethodData* mdo = m->method_data(); aoqi@0: if (mdo == NULL) return; aoqi@0: // There is a benign race here. See comments in methodData.hpp. aoqi@0: mdo->inc_decompile_count(); aoqi@0: } aoqi@0: aoqi@0: void nmethod::make_unloaded(BoolObjectClosure* is_alive, oop cause) { aoqi@0: aoqi@0: post_compiled_method_unload(); aoqi@0: aoqi@0: // Since this nmethod is being unloaded, make sure that dependencies aoqi@0: // recorded in instanceKlasses get flushed and pass non-NULL closure to aoqi@0: // indicate that this work is being done during a GC. aoqi@0: assert(Universe::heap()->is_gc_active(), "should only be called during gc"); aoqi@0: assert(is_alive != NULL, "Should be non-NULL"); aoqi@0: // A non-NULL is_alive closure indicates that this is being called during GC. aoqi@0: flush_dependencies(is_alive); aoqi@0: aoqi@0: // Break cycle between nmethod & method aoqi@0: if (TraceClassUnloading && WizardMode) { aoqi@0: tty->print_cr("[Class unloading: Making nmethod " INTPTR_FORMAT aoqi@0: " unloadable], Method*(" INTPTR_FORMAT aoqi@0: "), cause(" INTPTR_FORMAT ")", aoqi@0: this, (address)_method, (address)cause); aoqi@0: if (!Universe::heap()->is_gc_active()) aoqi@0: cause->klass()->print(); aoqi@0: } aoqi@0: // Unlink the osr method, so we do not look this up again aoqi@0: if (is_osr_method()) { aoqi@0: invalidate_osr_method(); aoqi@0: } aoqi@0: // If _method is already NULL the Method* is about to be unloaded, aoqi@0: // so we don't have to break the cycle. Note that it is possible to aoqi@0: // have the Method* live here, in case we unload the nmethod because aoqi@0: // it is pointing to some oop (other than the Method*) being unloaded. aoqi@0: if (_method != NULL) { aoqi@0: // OSR methods point to the Method*, but the Method* does not aoqi@0: // point back! aoqi@0: if (_method->code() == this) { aoqi@0: _method->clear_code(); // Break a cycle aoqi@0: } aoqi@0: _method = NULL; // Clear the method of this dead nmethod aoqi@0: } aoqi@0: // Make the class unloaded - i.e., change state and notify sweeper aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); aoqi@0: if (is_in_use()) { aoqi@0: // Transitioning directly from live to unloaded -- so aoqi@0: // we need to force a cache clean-up; remember this aoqi@0: // for later on. aoqi@0: CodeCache::set_needs_cache_clean(true); aoqi@0: } aoqi@0: _state = unloaded; aoqi@0: aoqi@0: // Log the unloading. aoqi@0: log_state_change(); aoqi@0: aoqi@0: // The Method* is gone at this point aoqi@0: assert(_method == NULL, "Tautology"); aoqi@0: aoqi@0: set_osr_link(NULL); aoqi@0: //set_scavenge_root_link(NULL); // done by prune_scavenge_root_nmethods aoqi@0: NMethodSweeper::report_state_change(this); aoqi@0: } aoqi@0: aoqi@0: void nmethod::invalidate_osr_method() { aoqi@0: assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod"); aoqi@0: // Remove from list of active nmethods aoqi@0: if (method() != NULL) aoqi@0: method()->method_holder()->remove_osr_nmethod(this); aoqi@0: // Set entry as invalid aoqi@0: _entry_bci = InvalidOSREntryBci; aoqi@0: } aoqi@0: aoqi@0: void nmethod::log_state_change() const { aoqi@0: if (LogCompilation) { aoqi@0: if (xtty != NULL) { aoqi@0: ttyLocker ttyl; // keep the following output all in one block aoqi@0: if (_state == unloaded) { aoqi@0: xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'", aoqi@0: os::current_thread_id()); aoqi@0: } else { aoqi@0: xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s", aoqi@0: os::current_thread_id(), aoqi@0: (_state == zombie ? " zombie='1'" : "")); aoqi@0: } aoqi@0: log_identity(xtty); aoqi@0: xtty->stamp(); aoqi@0: xtty->end_elem(); aoqi@0: } aoqi@0: } aoqi@0: if (PrintCompilation && _state != unloaded) { aoqi@0: print_on(tty, _state == zombie ? "made zombie" : "made not entrant"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Common functionality for both make_not_entrant and make_zombie aoqi@0: */ aoqi@0: bool nmethod::make_not_entrant_or_zombie(unsigned int state) { aoqi@0: assert(state == zombie || state == not_entrant, "must be zombie or not_entrant"); aoqi@0: assert(!is_zombie(), "should not already be a zombie"); aoqi@0: aoqi@0: // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below. aoqi@0: nmethodLocker nml(this); aoqi@0: methodHandle the_method(method()); aoqi@0: No_Safepoint_Verifier nsv; aoqi@0: aoqi@0: // during patching, depending on the nmethod state we must notify the GC that aoqi@0: // code has been unloaded, unregistering it. We cannot do this right while aoqi@0: // holding the Patching_lock because we need to use the CodeCache_lock. This aoqi@0: // would be prone to deadlocks. aoqi@0: // This flag is used to remember whether we need to later lock and unregister. aoqi@0: bool nmethod_needs_unregister = false; aoqi@0: aoqi@0: { aoqi@0: // invalidate osr nmethod before acquiring the patching lock since aoqi@0: // they both acquire leaf locks and we don't want a deadlock. aoqi@0: // This logic is equivalent to the logic below for patching the aoqi@0: // verified entry point of regular methods. aoqi@0: if (is_osr_method()) { aoqi@0: // this effectively makes the osr nmethod not entrant aoqi@0: invalidate_osr_method(); aoqi@0: } aoqi@0: aoqi@0: // Enter critical section. Does not block for safepoint. aoqi@0: MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag); aoqi@0: aoqi@0: if (_state == state) { aoqi@0: // another thread already performed this transition so nothing aoqi@0: // to do, but return false to indicate this. aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // The caller can be calling the method statically or through an inline aoqi@0: // cache call. aoqi@0: if (!is_osr_method() && !is_not_entrant()) { aoqi@1: NOT_MIPS64(NativeJump)MIPS64_ONLY(NativeGeneralJump)::patch_verified_entry(entry_point(), verified_entry_point(), aoqi@0: SharedRuntime::get_handle_wrong_method_stub()); aoqi@0: } aoqi@0: aoqi@0: if (is_in_use()) { aoqi@0: // It's a true state change, so mark the method as decompiled. aoqi@0: // Do it only for transition from alive. aoqi@0: inc_decompile_count(); aoqi@0: } aoqi@0: aoqi@0: // If the state is becoming a zombie, signal to unregister the nmethod with aoqi@0: // the heap. aoqi@0: // This nmethod may have already been unloaded during a full GC. aoqi@0: if ((state == zombie) && !is_unloaded()) { aoqi@0: nmethod_needs_unregister = true; aoqi@0: } aoqi@0: aoqi@0: // Must happen before state change. Otherwise we have a race condition in aoqi@0: // nmethod::can_not_entrant_be_converted(). I.e., a method can immediately aoqi@0: // transition its state from 'not_entrant' to 'zombie' without having to wait aoqi@0: // for stack scanning. aoqi@0: if (state == not_entrant) { aoqi@0: mark_as_seen_on_stack(); aoqi@0: OrderAccess::storestore(); aoqi@0: } aoqi@0: aoqi@0: // Change state aoqi@0: _state = state; aoqi@0: aoqi@0: // Log the transition once aoqi@0: log_state_change(); aoqi@0: aoqi@0: // Remove nmethod from method. aoqi@0: // We need to check if both the _code and _from_compiled_code_entry_point aoqi@0: // refer to this nmethod because there is a race in setting these two fields aoqi@0: // in Method* as seen in bugid 4947125. aoqi@0: // If the vep() points to the zombie nmethod, the memory for the nmethod aoqi@0: // could be flushed and the compiler and vtable stubs could still call aoqi@0: // through it. aoqi@0: if (method() != NULL && (method()->code() == this || aoqi@0: method()->from_compiled_entry() == verified_entry_point())) { aoqi@0: HandleMark hm; aoqi@0: method()->clear_code(); aoqi@0: } aoqi@0: } // leave critical region under Patching_lock aoqi@0: aoqi@0: // When the nmethod becomes zombie it is no longer alive so the aoqi@0: // dependencies must be flushed. nmethods in the not_entrant aoqi@0: // state will be flushed later when the transition to zombie aoqi@0: // happens or they get unloaded. aoqi@0: if (state == zombie) { aoqi@0: { aoqi@0: // Flushing dependecies must be done before any possible aoqi@0: // safepoint can sneak in, otherwise the oops used by the aoqi@0: // dependency logic could have become stale. aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: if (nmethod_needs_unregister) { aoqi@0: Universe::heap()->unregister_nmethod(this); aoqi@0: } aoqi@0: flush_dependencies(NULL); aoqi@0: } aoqi@0: aoqi@0: // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload aoqi@0: // event and it hasn't already been reported for this nmethod then aoqi@0: // report it now. The event may have been reported earilier if the GC aoqi@0: // marked it for unloading). JvmtiDeferredEventQueue support means aoqi@0: // we no longer go to a safepoint here. aoqi@0: post_compiled_method_unload(); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // It's no longer safe to access the oops section since zombie aoqi@0: // nmethods aren't scanned for GC. aoqi@0: _oops_are_stale = true; aoqi@0: #endif aoqi@0: // the Method may be reclaimed by class unloading now that the aoqi@0: // nmethod is in zombie state aoqi@0: set_method(NULL); aoqi@0: } else { aoqi@0: assert(state == not_entrant, "other cases may need to be handled differently"); aoqi@0: } aoqi@0: aoqi@0: if (TraceCreateZombies) { aoqi@0: tty->print_cr("nmethod <" INTPTR_FORMAT "> code made %s", this, (state == not_entrant) ? "not entrant" : "zombie"); aoqi@0: } aoqi@0: aoqi@0: NMethodSweeper::report_state_change(this); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void nmethod::flush() { aoqi@0: // Note that there are no valid oops in the nmethod anymore. aoqi@0: assert(is_zombie() || (is_osr_method() && is_unloaded()), "must be a zombie method"); aoqi@0: assert(is_marked_for_reclamation() || (is_osr_method() && is_unloaded()), "must be marked for reclamation"); aoqi@0: aoqi@0: assert (!is_locked_by_vm(), "locked methods shouldn't be flushed"); aoqi@0: assert_locked_or_safepoint(CodeCache_lock); aoqi@0: aoqi@0: // completely deallocate this method aoqi@0: Events::log(JavaThread::current(), "flushing nmethod " INTPTR_FORMAT, this); aoqi@0: if (PrintMethodFlushing) { aoqi@0: tty->print_cr("*flushing nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT "/Free CodeCache:" SIZE_FORMAT "Kb", aoqi@0: _compile_id, this, CodeCache::nof_blobs(), CodeCache::unallocated_capacity()/1024); aoqi@0: } aoqi@0: aoqi@0: // We need to deallocate any ExceptionCache data. aoqi@0: // Note that we do not need to grab the nmethod lock for this, it aoqi@0: // better be thread safe if we're disposing of it! aoqi@0: ExceptionCache* ec = exception_cache(); aoqi@0: set_exception_cache(NULL); aoqi@0: while(ec != NULL) { aoqi@0: ExceptionCache* next = ec->next(); aoqi@0: delete ec; aoqi@0: ec = next; aoqi@0: } aoqi@0: aoqi@0: if (on_scavenge_root_list()) { aoqi@0: CodeCache::drop_scavenge_root_nmethod(this); aoqi@0: } aoqi@0: aoqi@0: #ifdef SHARK aoqi@0: ((SharkCompiler *) compiler())->free_compiled_method(insts_begin()); aoqi@0: #endif // SHARK aoqi@0: aoqi@0: ((CodeBlob*)(this))->flush(); aoqi@0: aoqi@0: CodeCache::free(this); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // Notify all classes this nmethod is dependent on that it is no aoqi@0: // longer dependent. This should only be called in two situations. aoqi@0: // First, when a nmethod transitions to a zombie all dependents need aoqi@0: // to be clear. Since zombification happens at a safepoint there's no aoqi@0: // synchronization issues. The second place is a little more tricky. aoqi@0: // During phase 1 of mark sweep class unloading may happen and as a aoqi@0: // result some nmethods may get unloaded. In this case the flushing aoqi@0: // of dependencies must happen during phase 1 since after GC any aoqi@0: // dependencies in the unloaded nmethod won't be updated, so aoqi@0: // traversing the dependency information in unsafe. In that case this aoqi@0: // function is called with a non-NULL argument and this function only aoqi@0: // notifies instanceKlasses that are reachable aoqi@0: aoqi@0: void nmethod::flush_dependencies(BoolObjectClosure* is_alive) { aoqi@0: assert_locked_or_safepoint(CodeCache_lock); aoqi@0: assert(Universe::heap()->is_gc_active() == (is_alive != NULL), aoqi@0: "is_alive is non-NULL if and only if we are called during GC"); aoqi@0: if (!has_flushed_dependencies()) { aoqi@0: set_has_flushed_dependencies(); aoqi@0: for (Dependencies::DepStream deps(this); deps.next(); ) { aoqi@0: Klass* klass = deps.context_type(); aoqi@0: if (klass == NULL) continue; // ignore things like evol_method aoqi@0: aoqi@0: // During GC the is_alive closure is non-NULL, and is used to aoqi@0: // determine liveness of dependees that need to be updated. aoqi@0: if (is_alive == NULL || klass->is_loader_alive(is_alive)) { aoqi@0: InstanceKlass::cast(klass)->remove_dependent_nmethod(this); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // If this oop is not live, the nmethod can be unloaded. aoqi@0: bool nmethod::can_unload(BoolObjectClosure* is_alive, oop* root, bool unloading_occurred) { aoqi@0: assert(root != NULL, "just checking"); aoqi@0: oop obj = *root; aoqi@0: if (obj == NULL || is_alive->do_object_b(obj)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // If ScavengeRootsInCode is true, an nmethod might be unloaded aoqi@0: // simply because one of its constant oops has gone dead. aoqi@0: // No actual classes need to be unloaded in order for this to occur. aoqi@0: assert(unloading_occurred || ScavengeRootsInCode, "Inconsistency in unloading"); aoqi@0: make_unloaded(is_alive, obj); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // post_compiled_method_load_event aoqi@0: // new method for install_code() path aoqi@0: // Transfer information from compilation to jvmti aoqi@0: void nmethod::post_compiled_method_load_event() { aoqi@0: aoqi@0: Method* moop = method(); aoqi@0: #ifndef USDT2 aoqi@0: HS_DTRACE_PROBE8(hotspot, compiled__method__load, aoqi@0: moop->klass_name()->bytes(), aoqi@0: moop->klass_name()->utf8_length(), aoqi@0: moop->name()->bytes(), aoqi@0: moop->name()->utf8_length(), aoqi@0: moop->signature()->bytes(), aoqi@0: moop->signature()->utf8_length(), aoqi@0: insts_begin(), insts_size()); aoqi@0: #else /* USDT2 */ aoqi@0: HOTSPOT_COMPILED_METHOD_LOAD( aoqi@0: (char *) moop->klass_name()->bytes(), aoqi@0: moop->klass_name()->utf8_length(), aoqi@0: (char *) moop->name()->bytes(), aoqi@0: moop->name()->utf8_length(), aoqi@0: (char *) moop->signature()->bytes(), aoqi@0: moop->signature()->utf8_length(), aoqi@0: insts_begin(), insts_size()); aoqi@0: #endif /* USDT2 */ aoqi@0: aoqi@0: if (JvmtiExport::should_post_compiled_method_load() || aoqi@0: JvmtiExport::should_post_compiled_method_unload()) { aoqi@0: get_and_cache_jmethod_id(); aoqi@0: } aoqi@0: aoqi@0: if (JvmtiExport::should_post_compiled_method_load()) { aoqi@0: // Let the Service thread (which is a real Java thread) post the event aoqi@0: MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); aoqi@0: JvmtiDeferredEventQueue::enqueue( aoqi@0: JvmtiDeferredEvent::compiled_method_load_event(this)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: jmethodID nmethod::get_and_cache_jmethod_id() { aoqi@0: if (_jmethod_id == NULL) { aoqi@0: // Cache the jmethod_id since it can no longer be looked up once the aoqi@0: // method itself has been marked for unloading. aoqi@0: _jmethod_id = method()->jmethod_id(); aoqi@0: } aoqi@0: return _jmethod_id; aoqi@0: } aoqi@0: aoqi@0: void nmethod::post_compiled_method_unload() { aoqi@0: if (unload_reported()) { aoqi@0: // During unloading we transition to unloaded and then to zombie aoqi@0: // and the unloading is reported during the first transition. aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: assert(_method != NULL && !is_unloaded(), "just checking"); aoqi@0: DTRACE_METHOD_UNLOAD_PROBE(method()); aoqi@0: aoqi@0: // If a JVMTI agent has enabled the CompiledMethodUnload event then aoqi@0: // post the event. Sometime later this nmethod will be made a zombie aoqi@0: // by the sweeper but the Method* will not be valid at that point. aoqi@0: // If the _jmethod_id is null then no load event was ever requested aoqi@0: // so don't bother posting the unload. The main reason for this is aoqi@0: // that the jmethodID is a weak reference to the Method* so if aoqi@0: // it's being unloaded there's no way to look it up since the weak aoqi@0: // ref will have been cleared. aoqi@0: if (_jmethod_id != NULL && JvmtiExport::should_post_compiled_method_unload()) { aoqi@0: assert(!unload_reported(), "already unloaded"); aoqi@0: JvmtiDeferredEvent event = aoqi@0: JvmtiDeferredEvent::compiled_method_unload_event(this, aoqi@0: _jmethod_id, insts_begin()); aoqi@0: if (SafepointSynchronize::is_at_safepoint()) { aoqi@0: // Don't want to take the queueing lock. Add it as pending and aoqi@0: // it will get enqueued later. aoqi@0: JvmtiDeferredEventQueue::add_pending_event(event); aoqi@0: } else { aoqi@0: MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); aoqi@0: JvmtiDeferredEventQueue::enqueue(event); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // The JVMTI CompiledMethodUnload event can be enabled or disabled at aoqi@0: // any time. As the nmethod is being unloaded now we mark it has aoqi@0: // having the unload event reported - this will ensure that we don't aoqi@0: // attempt to report the event in the unlikely scenario where the aoqi@0: // event is enabled at the time the nmethod is made a zombie. aoqi@0: set_unload_reported(); aoqi@0: } aoqi@0: aoqi@0: // This is called at the end of the strong tracing/marking phase of a aoqi@0: // GC to unload an nmethod if it contains otherwise unreachable aoqi@0: // oops. aoqi@0: aoqi@0: void nmethod::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) { aoqi@0: // Make sure the oop's ready to receive visitors aoqi@0: assert(!is_zombie() && !is_unloaded(), aoqi@0: "should not call follow on zombie or unloaded nmethod"); aoqi@0: aoqi@0: // If the method is not entrant then a JMP is plastered over the aoqi@0: // first few bytes. If an oop in the old code was there, that oop aoqi@0: // should not get GC'd. Skip the first few bytes of oops on aoqi@0: // not-entrant methods. aoqi@0: address low_boundary = verified_entry_point(); aoqi@0: if (is_not_entrant()) { aoqi@1: low_boundary += NOT_MIPS64(NativeJump)MIPS64_ONLY(NativeGeneralJump)::instruction_size; aoqi@0: // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. aoqi@0: // (See comment above.) aoqi@0: } aoqi@0: aoqi@0: // The RedefineClasses() API can cause the class unloading invariant aoqi@0: // to no longer be true. See jvmtiExport.hpp for details. aoqi@0: // Also, leave a debugging breadcrumb in local flag. aoqi@0: bool a_class_was_redefined = JvmtiExport::has_redefined_a_class(); aoqi@0: if (a_class_was_redefined) { aoqi@0: // This set of the unloading_occurred flag is done before the aoqi@0: // call to post_compiled_method_unload() so that the unloading aoqi@0: // of this nmethod is reported. aoqi@0: unloading_occurred = true; aoqi@0: } aoqi@0: aoqi@0: // Exception cache aoqi@0: ExceptionCache* ec = exception_cache(); aoqi@0: while (ec != NULL) { aoqi@0: Klass* ex_klass = ec->exception_type(); aoqi@0: ExceptionCache* next_ec = ec->next(); aoqi@0: if (ex_klass != NULL && !ex_klass->is_loader_alive(is_alive)) { aoqi@0: remove_from_exception_cache(ec); aoqi@0: } aoqi@0: ec = next_ec; aoqi@0: } aoqi@0: aoqi@0: // If class unloading occurred we first iterate over all inline caches and aoqi@0: // clear ICs where the cached oop is referring to an unloaded klass or method. aoqi@0: // The remaining live cached oops will be traversed in the relocInfo::oop_type aoqi@0: // iteration below. aoqi@0: if (unloading_occurred) { aoqi@0: RelocIterator iter(this, low_boundary); aoqi@0: while(iter.next()) { aoqi@0: if (iter.type() == relocInfo::virtual_call_type) { aoqi@0: CompiledIC *ic = CompiledIC_at(iter.reloc()); aoqi@0: if (ic->is_icholder_call()) { aoqi@0: // The only exception is compiledICHolder oops which may aoqi@0: // yet be marked below. (We check this further below). aoqi@0: CompiledICHolder* cichk_oop = ic->cached_icholder(); aoqi@0: if (cichk_oop->holder_method()->method_holder()->is_loader_alive(is_alive) && aoqi@0: cichk_oop->holder_klass()->is_loader_alive(is_alive)) { aoqi@0: continue; aoqi@0: } aoqi@0: } else { aoqi@0: Metadata* ic_oop = ic->cached_metadata(); aoqi@0: if (ic_oop != NULL) { aoqi@0: if (ic_oop->is_klass()) { aoqi@0: if (((Klass*)ic_oop)->is_loader_alive(is_alive)) { aoqi@0: continue; aoqi@0: } aoqi@0: } else if (ic_oop->is_method()) { aoqi@0: if (((Method*)ic_oop)->method_holder()->is_loader_alive(is_alive)) { aoqi@0: continue; aoqi@0: } aoqi@0: } else { aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: ic->set_to_clean(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Compiled code aoqi@0: { aoqi@0: RelocIterator iter(this, low_boundary); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::oop_type) { aoqi@0: oop_Relocation* r = iter.oop_reloc(); aoqi@0: // In this loop, we must only traverse those oops directly embedded in aoqi@0: // the code. Other oops (oop_index>0) are seen as part of scopes_oops. aoqi@0: assert(1 == (r->oop_is_immediate()) + aoqi@0: (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), aoqi@0: "oop must be found in exactly one place"); aoqi@0: if (r->oop_is_immediate() && r->oop_value() != NULL) { aoqi@0: if (can_unload(is_alive, r->oop_addr(), unloading_occurred)) { aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Scopes aoqi@0: for (oop* p = oops_begin(); p < oops_end(); p++) { aoqi@0: if (*p == Universe::non_oop_word()) continue; // skip non-oops aoqi@0: if (can_unload(is_alive, p, unloading_occurred)) { aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Ensure that all metadata is still alive aoqi@0: verify_metadata_loaders(low_boundary, is_alive); aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: aoqi@0: class CheckClass : AllStatic { aoqi@0: static BoolObjectClosure* _is_alive; aoqi@0: aoqi@0: // Check class_loader is alive for this bit of metadata. aoqi@0: static void check_class(Metadata* md) { aoqi@0: Klass* klass = NULL; aoqi@0: if (md->is_klass()) { aoqi@0: klass = ((Klass*)md); aoqi@0: } else if (md->is_method()) { aoqi@0: klass = ((Method*)md)->method_holder(); aoqi@0: } else if (md->is_methodData()) { aoqi@0: klass = ((MethodData*)md)->method()->method_holder(); aoqi@0: } else { aoqi@0: md->print(); aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: assert(klass->is_loader_alive(_is_alive), "must be alive"); aoqi@0: } aoqi@0: public: aoqi@0: static void do_check_class(BoolObjectClosure* is_alive, nmethod* nm) { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "this is only ok at safepoint"); aoqi@0: _is_alive = is_alive; aoqi@0: nm->metadata_do(check_class); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // This is called during a safepoint so can use static data aoqi@0: BoolObjectClosure* CheckClass::_is_alive = NULL; aoqi@0: #endif // ASSERT aoqi@0: aoqi@0: aoqi@0: // Processing of oop references should have been sufficient to keep aoqi@0: // all strong references alive. Any weak references should have been aoqi@0: // cleared as well. Visit all the metadata and ensure that it's aoqi@0: // really alive. aoqi@0: void nmethod::verify_metadata_loaders(address low_boundary, BoolObjectClosure* is_alive) { aoqi@0: #ifdef ASSERT aoqi@0: RelocIterator iter(this, low_boundary); aoqi@0: while (iter.next()) { aoqi@0: // static_stub_Relocations may have dangling references to aoqi@0: // Method*s so trim them out here. Otherwise it looks like aoqi@0: // compiled code is maintaining a link to dead metadata. aoqi@0: address static_call_addr = NULL; aoqi@0: if (iter.type() == relocInfo::opt_virtual_call_type) { aoqi@0: CompiledIC* cic = CompiledIC_at(iter.reloc()); aoqi@0: if (!cic->is_call_to_interpreted()) { aoqi@0: static_call_addr = iter.addr(); aoqi@0: } aoqi@0: } else if (iter.type() == relocInfo::static_call_type) { aoqi@0: CompiledStaticCall* csc = compiledStaticCall_at(iter.reloc()); aoqi@0: if (!csc->is_call_to_interpreted()) { aoqi@0: static_call_addr = iter.addr(); aoqi@0: } aoqi@0: } aoqi@0: if (static_call_addr != NULL) { aoqi@0: RelocIterator sciter(this, low_boundary); aoqi@0: while (sciter.next()) { aoqi@0: if (sciter.type() == relocInfo::static_stub_type && aoqi@0: sciter.static_stub_reloc()->static_call() == static_call_addr) { aoqi@0: sciter.static_stub_reloc()->clear_inline_cache(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // Check that the metadata embedded in the nmethod is alive aoqi@0: CheckClass::do_check_class(is_alive, this); aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Iterate over metadata calling this function. Used by RedefineClasses aoqi@0: void nmethod::metadata_do(void f(Metadata*)) { aoqi@0: address low_boundary = verified_entry_point(); aoqi@0: if (is_not_entrant()) { aoqi@1: low_boundary += NOT_MIPS64(NativeJump)MIPS64_ONLY(NativeGeneralJump)::instruction_size; aoqi@0: // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. aoqi@0: // (See comment above.) aoqi@0: } aoqi@0: { aoqi@0: // Visit all immediate references that are embedded in the instruction stream. aoqi@0: RelocIterator iter(this, low_boundary); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::metadata_type ) { aoqi@0: metadata_Relocation* r = iter.metadata_reloc(); aoqi@0: // In this lmetadata, we must only follow those metadatas directly embedded in aoqi@0: // the code. Other metadatas (oop_index>0) are seen as part of aoqi@0: // the metadata section below. aoqi@0: assert(1 == (r->metadata_is_immediate()) + aoqi@0: (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()), aoqi@0: "metadata must be found in exactly one place"); aoqi@0: if (r->metadata_is_immediate() && r->metadata_value() != NULL) { aoqi@0: Metadata* md = r->metadata_value(); aoqi@0: f(md); aoqi@0: } aoqi@0: } else if (iter.type() == relocInfo::virtual_call_type) { aoqi@0: // Check compiledIC holders associated with this nmethod aoqi@0: CompiledIC *ic = CompiledIC_at(iter.reloc()); aoqi@0: if (ic->is_icholder_call()) { aoqi@0: CompiledICHolder* cichk = ic->cached_icholder(); aoqi@0: f(cichk->holder_method()); aoqi@0: f(cichk->holder_klass()); aoqi@0: } else { aoqi@0: Metadata* ic_oop = ic->cached_metadata(); aoqi@0: if (ic_oop != NULL) { aoqi@0: f(ic_oop); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Visit the metadata section aoqi@0: for (Metadata** p = metadata_begin(); p < metadata_end(); p++) { aoqi@0: if (*p == Universe::non_oop_word() || *p == NULL) continue; // skip non-oops aoqi@0: Metadata* md = *p; aoqi@0: f(md); aoqi@0: } aoqi@0: aoqi@0: // Call function Method*, not embedded in these other places. aoqi@0: if (_method != NULL) f(_method); aoqi@0: } aoqi@0: aoqi@0: void nmethod::oops_do(OopClosure* f, bool allow_zombie) { aoqi@0: // make sure the oops ready to receive visitors aoqi@0: assert(allow_zombie || !is_zombie(), "should not call follow on zombie nmethod"); aoqi@0: assert(!is_unloaded(), "should not call follow on unloaded nmethod"); aoqi@0: aoqi@0: // If the method is not entrant or zombie then a JMP is plastered over the aoqi@0: // first few bytes. If an oop in the old code was there, that oop aoqi@0: // should not get GC'd. Skip the first few bytes of oops on aoqi@0: // not-entrant methods. aoqi@0: address low_boundary = verified_entry_point(); aoqi@0: if (is_not_entrant()) { aoqi@1: low_boundary += NOT_MIPS64(NativeJump)MIPS64_ONLY(NativeGeneralJump)::instruction_size; aoqi@0: // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. aoqi@0: // (See comment above.) aoqi@0: } aoqi@0: aoqi@0: RelocIterator iter(this, low_boundary); aoqi@0: aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::oop_type ) { aoqi@0: oop_Relocation* r = iter.oop_reloc(); aoqi@0: // In this loop, we must only follow those oops directly embedded in aoqi@0: // the code. Other oops (oop_index>0) are seen as part of scopes_oops. aoqi@0: assert(1 == (r->oop_is_immediate()) + aoqi@0: (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), aoqi@0: "oop must be found in exactly one place"); aoqi@0: if (r->oop_is_immediate() && r->oop_value() != NULL) { aoqi@0: f->do_oop(r->oop_addr()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Scopes aoqi@0: // This includes oop constants not inlined in the code stream. aoqi@0: for (oop* p = oops_begin(); p < oops_end(); p++) { aoqi@0: if (*p == Universe::non_oop_word()) continue; // skip non-oops aoqi@0: f->do_oop(p); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #define NMETHOD_SENTINEL ((nmethod*)badAddress) aoqi@0: aoqi@0: nmethod* volatile nmethod::_oops_do_mark_nmethods; aoqi@0: aoqi@0: // An nmethod is "marked" if its _mark_link is set non-null. aoqi@0: // Even if it is the end of the linked list, it will have a non-null link value, aoqi@0: // as long as it is on the list. aoqi@0: // This code must be MP safe, because it is used from parallel GC passes. aoqi@0: bool nmethod::test_set_oops_do_mark() { aoqi@0: assert(nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called"); aoqi@0: nmethod* observed_mark_link = _oops_do_mark_link; aoqi@0: if (observed_mark_link == NULL) { aoqi@0: // Claim this nmethod for this thread to mark. aoqi@0: observed_mark_link = (nmethod*) aoqi@0: Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_link, NULL); aoqi@0: if (observed_mark_link == NULL) { aoqi@0: aoqi@0: // Atomically append this nmethod (now claimed) to the head of the list: aoqi@0: nmethod* observed_mark_nmethods = _oops_do_mark_nmethods; aoqi@0: for (;;) { aoqi@0: nmethod* required_mark_nmethods = observed_mark_nmethods; aoqi@0: _oops_do_mark_link = required_mark_nmethods; aoqi@0: observed_mark_nmethods = (nmethod*) aoqi@0: Atomic::cmpxchg_ptr(this, &_oops_do_mark_nmethods, required_mark_nmethods); aoqi@0: if (observed_mark_nmethods == required_mark_nmethods) aoqi@0: break; aoqi@0: } aoqi@0: // Mark was clear when we first saw this guy. aoqi@0: NOT_PRODUCT(if (TraceScavenge) print_on(tty, "oops_do, mark")); aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: // On fall through, another racing thread marked this nmethod before we did. aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void nmethod::oops_do_marking_prologue() { aoqi@0: NOT_PRODUCT(if (TraceScavenge) tty->print_cr("[oops_do_marking_prologue")); aoqi@0: assert(_oops_do_mark_nmethods == NULL, "must not call oops_do_marking_prologue twice in a row"); aoqi@0: // We use cmpxchg_ptr instead of regular assignment here because the user aoqi@0: // may fork a bunch of threads, and we need them all to see the same state. aoqi@0: void* observed = Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_nmethods, NULL); aoqi@0: guarantee(observed == NULL, "no races in this sequential code"); aoqi@0: } aoqi@0: aoqi@0: void nmethod::oops_do_marking_epilogue() { aoqi@0: assert(_oops_do_mark_nmethods != NULL, "must not call oops_do_marking_epilogue twice in a row"); aoqi@0: nmethod* cur = _oops_do_mark_nmethods; aoqi@0: while (cur != NMETHOD_SENTINEL) { aoqi@0: assert(cur != NULL, "not NULL-terminated"); aoqi@0: nmethod* next = cur->_oops_do_mark_link; aoqi@0: cur->_oops_do_mark_link = NULL; aoqi@0: cur->fix_oop_relocations(); aoqi@0: NOT_PRODUCT(if (TraceScavenge) cur->print_on(tty, "oops_do, unmark")); aoqi@0: cur = next; aoqi@0: } aoqi@0: void* required = _oops_do_mark_nmethods; aoqi@0: void* observed = Atomic::cmpxchg_ptr(NULL, &_oops_do_mark_nmethods, required); aoqi@0: guarantee(observed == required, "no races in this sequential code"); aoqi@0: NOT_PRODUCT(if (TraceScavenge) tty->print_cr("oops_do_marking_epilogue]")); aoqi@0: } aoqi@0: aoqi@0: class DetectScavengeRoot: public OopClosure { aoqi@0: bool _detected_scavenge_root; aoqi@0: public: aoqi@0: DetectScavengeRoot() : _detected_scavenge_root(false) aoqi@0: { NOT_PRODUCT(_print_nm = NULL); } aoqi@0: bool detected_scavenge_root() { return _detected_scavenge_root; } aoqi@0: virtual void do_oop(oop* p) { aoqi@0: if ((*p) != NULL && (*p)->is_scavengable()) { aoqi@0: NOT_PRODUCT(maybe_print(p)); aoqi@0: _detected_scavenge_root = true; aoqi@0: } aoqi@0: } aoqi@0: virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: nmethod* _print_nm; aoqi@0: void maybe_print(oop* p) { aoqi@0: if (_print_nm == NULL) return; aoqi@0: if (!_detected_scavenge_root) _print_nm->print_on(tty, "new scavenge root"); aoqi@0: tty->print_cr(""PTR_FORMAT"[offset=%d] detected scavengable oop "PTR_FORMAT" (found at "PTR_FORMAT")", aoqi@0: _print_nm, (int)((intptr_t)p - (intptr_t)_print_nm), aoqi@0: (void *)(*p), (intptr_t)p); aoqi@0: (*p)->print(); aoqi@0: } aoqi@0: #endif //PRODUCT aoqi@0: }; aoqi@0: aoqi@0: bool nmethod::detect_scavenge_root_oops() { aoqi@0: DetectScavengeRoot detect_scavenge_root; aoqi@0: NOT_PRODUCT(if (TraceScavenge) detect_scavenge_root._print_nm = this); aoqi@0: oops_do(&detect_scavenge_root); aoqi@0: return detect_scavenge_root.detected_scavenge_root(); aoqi@0: } aoqi@0: aoqi@0: // Method that knows how to preserve outgoing arguments at call. This method must be aoqi@0: // called with a frame corresponding to a Java invoke aoqi@0: void nmethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { aoqi@0: #ifndef SHARK aoqi@0: if (!method()->is_native()) { aoqi@0: SimpleScopeDesc ssd(this, fr.pc()); aoqi@0: Bytecode_invoke call(ssd.method(), ssd.bci()); aoqi@0: bool has_receiver = call.has_receiver(); aoqi@0: bool has_appendix = call.has_appendix(); aoqi@0: Symbol* signature = call.signature(); aoqi@0: fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f); aoqi@0: } aoqi@0: #endif // !SHARK aoqi@0: } aoqi@0: aoqi@0: aoqi@0: oop nmethod::embeddedOop_at(u_char* p) { aoqi@0: RelocIterator iter(this, p, p + 1); aoqi@0: while (iter.next()) aoqi@0: if (iter.type() == relocInfo::oop_type) { aoqi@0: return iter.oop_reloc()->oop_value(); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: inline bool includes(void* p, void* from, void* to) { aoqi@0: return from <= p && p < to; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) { aoqi@0: assert(count >= 2, "must be sentinel values, at least"); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // must be sorted and unique; we do a binary search in find_pc_desc() aoqi@0: int prev_offset = pcs[0].pc_offset(); aoqi@0: assert(prev_offset == PcDesc::lower_offset_limit, aoqi@0: "must start with a sentinel"); aoqi@0: for (int i = 1; i < count; i++) { aoqi@0: int this_offset = pcs[i].pc_offset(); aoqi@0: assert(this_offset > prev_offset, "offsets must be sorted"); aoqi@0: prev_offset = this_offset; aoqi@0: } aoqi@0: assert(prev_offset == PcDesc::upper_offset_limit, aoqi@0: "must end with a sentinel"); aoqi@0: #endif //ASSERT aoqi@0: aoqi@0: // Search for MethodHandle invokes and tag the nmethod. aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: if (pcs[i].is_method_handle_invoke()) { aoqi@0: set_has_method_handle_invokes(true); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: assert(has_method_handle_invokes() == (_deoptimize_mh_offset != -1), "must have deopt mh handler"); aoqi@0: aoqi@0: int size = count * sizeof(PcDesc); aoqi@0: assert(scopes_pcs_size() >= size, "oob"); aoqi@0: memcpy(scopes_pcs_begin(), pcs, size); aoqi@0: aoqi@0: // Adjust the final sentinel downward. aoqi@0: PcDesc* last_pc = &scopes_pcs_begin()[count-1]; aoqi@0: assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity"); aoqi@0: last_pc->set_pc_offset(content_size() + 1); aoqi@0: for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) { aoqi@0: // Fill any rounding gaps with copies of the last record. aoqi@0: last_pc[1] = last_pc[0]; aoqi@0: } aoqi@0: // The following assert could fail if sizeof(PcDesc) is not aoqi@0: // an integral multiple of oopSize (the rounding term). aoqi@0: // If it fails, change the logic to always allocate a multiple aoqi@0: // of sizeof(PcDesc), and fill unused words with copies of *last_pc. aoqi@0: assert(last_pc + 1 == scopes_pcs_end(), "must match exactly"); aoqi@0: } aoqi@0: aoqi@0: void nmethod::copy_scopes_data(u_char* buffer, int size) { aoqi@0: assert(scopes_data_size() >= size, "oob"); aoqi@0: memcpy(scopes_data_begin(), buffer, size); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: static PcDesc* linear_search(nmethod* nm, int pc_offset, bool approximate) { aoqi@0: PcDesc* lower = nm->scopes_pcs_begin(); aoqi@0: PcDesc* upper = nm->scopes_pcs_end(); aoqi@0: lower += 1; // exclude initial sentinel aoqi@0: PcDesc* res = NULL; aoqi@0: for (PcDesc* p = lower; p < upper; p++) { aoqi@0: NOT_PRODUCT(--nmethod_stats.pc_desc_tests); // don't count this call to match_desc aoqi@0: if (match_desc(p, pc_offset, approximate)) { aoqi@0: if (res == NULL) aoqi@0: res = p; aoqi@0: else aoqi@0: res = (PcDesc*) badAddress; aoqi@0: } aoqi@0: } aoqi@0: return res; aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: // Finds a PcDesc with real-pc equal to "pc" aoqi@0: PcDesc* nmethod::find_pc_desc_internal(address pc, bool approximate) { aoqi@0: address base_address = code_begin(); aoqi@0: if ((pc < base_address) || aoqi@0: (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) { aoqi@0: return NULL; // PC is wildly out of range aoqi@0: } aoqi@0: int pc_offset = (int) (pc - base_address); aoqi@0: aoqi@0: // Check the PcDesc cache if it contains the desired PcDesc aoqi@0: // (This as an almost 100% hit rate.) aoqi@0: PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate); aoqi@0: if (res != NULL) { aoqi@0: assert(res == linear_search(this, pc_offset, approximate), "cache ok"); aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: // Fallback algorithm: quasi-linear search for the PcDesc aoqi@0: // Find the last pc_offset less than the given offset. aoqi@0: // The successor must be the required match, if there is a match at all. aoqi@0: // (Use a fixed radix to avoid expensive affine pointer arithmetic.) aoqi@0: PcDesc* lower = scopes_pcs_begin(); aoqi@0: PcDesc* upper = scopes_pcs_end(); aoqi@0: upper -= 1; // exclude final sentinel aoqi@0: if (lower >= upper) return NULL; // native method; no PcDescs at all aoqi@0: aoqi@0: #define assert_LU_OK \ aoqi@0: /* invariant on lower..upper during the following search: */ \ aoqi@0: assert(lower->pc_offset() < pc_offset, "sanity"); \ aoqi@0: assert(upper->pc_offset() >= pc_offset, "sanity") aoqi@0: assert_LU_OK; aoqi@0: aoqi@0: // Use the last successful return as a split point. aoqi@0: PcDesc* mid = _pc_desc_cache.last_pc_desc(); aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_searches); aoqi@0: if (mid->pc_offset() < pc_offset) { aoqi@0: lower = mid; aoqi@0: } else { aoqi@0: upper = mid; aoqi@0: } aoqi@0: aoqi@0: // Take giant steps at first (4096, then 256, then 16, then 1) aoqi@0: const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1); aoqi@0: const int RADIX = (1 << LOG2_RADIX); aoqi@0: for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) { aoqi@0: while ((mid = lower + step) < upper) { aoqi@0: assert_LU_OK; aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_searches); aoqi@0: if (mid->pc_offset() < pc_offset) { aoqi@0: lower = mid; aoqi@0: } else { aoqi@0: upper = mid; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: assert_LU_OK; aoqi@0: } aoqi@0: aoqi@0: // Sneak up on the value with a linear search of length ~16. aoqi@0: while (true) { aoqi@0: assert_LU_OK; aoqi@0: mid = lower + 1; aoqi@0: NOT_PRODUCT(++nmethod_stats.pc_desc_searches); aoqi@0: if (mid->pc_offset() < pc_offset) { aoqi@0: lower = mid; aoqi@0: } else { aoqi@0: upper = mid; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: #undef assert_LU_OK aoqi@0: aoqi@0: if (match_desc(upper, pc_offset, approximate)) { aoqi@0: assert(upper == linear_search(this, pc_offset, approximate), "search ok"); aoqi@0: _pc_desc_cache.add_pc_desc(upper); aoqi@0: return upper; aoqi@0: } else { aoqi@0: assert(NULL == linear_search(this, pc_offset, approximate), "search ok"); aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool nmethod::check_all_dependencies() { aoqi@0: bool found_check = false; aoqi@0: // wholesale check of all dependencies aoqi@0: for (Dependencies::DepStream deps(this); deps.next(); ) { aoqi@0: if (deps.check_dependency() != NULL) { aoqi@0: found_check = true; aoqi@0: NOT_DEBUG(break); aoqi@0: } aoqi@0: } aoqi@0: return found_check; // tell caller if we found anything aoqi@0: } aoqi@0: aoqi@0: bool nmethod::check_dependency_on(DepChange& changes) { aoqi@0: // What has happened: aoqi@0: // 1) a new class dependee has been added aoqi@0: // 2) dependee and all its super classes have been marked aoqi@0: bool found_check = false; // set true if we are upset aoqi@0: for (Dependencies::DepStream deps(this); deps.next(); ) { aoqi@0: // Evaluate only relevant dependencies. aoqi@0: if (deps.spot_check_dependency_at(changes) != NULL) { aoqi@0: found_check = true; aoqi@0: NOT_DEBUG(break); aoqi@0: } aoqi@0: } aoqi@0: return found_check; aoqi@0: } aoqi@0: aoqi@0: bool nmethod::is_evol_dependent_on(Klass* dependee) { aoqi@0: InstanceKlass *dependee_ik = InstanceKlass::cast(dependee); aoqi@0: Array* dependee_methods = dependee_ik->methods(); aoqi@0: for (Dependencies::DepStream deps(this); deps.next(); ) { aoqi@0: if (deps.type() == Dependencies::evol_method) { aoqi@0: Method* method = deps.method_argument(0); aoqi@0: for (int j = 0; j < dependee_methods->length(); j++) { aoqi@0: if (dependee_methods->at(j) == method) { aoqi@0: // RC_TRACE macro has an embedded ResourceMark aoqi@0: RC_TRACE(0x01000000, aoqi@0: ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)", aoqi@0: _method->method_holder()->external_name(), aoqi@0: _method->name()->as_C_string(), aoqi@0: _method->signature()->as_C_string(), compile_id(), aoqi@0: method->method_holder()->external_name(), aoqi@0: method->name()->as_C_string(), aoqi@0: method->signature()->as_C_string())); aoqi@0: if (TraceDependencies || LogCompilation) aoqi@0: deps.log_dependency(dependee); aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Called from mark_for_deoptimization, when dependee is invalidated. aoqi@0: bool nmethod::is_dependent_on_method(Method* dependee) { aoqi@0: for (Dependencies::DepStream deps(this); deps.next(); ) { aoqi@0: if (deps.type() != Dependencies::evol_method) aoqi@0: continue; aoqi@0: Method* method = deps.method_argument(0); aoqi@0: if (method == dependee) return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool nmethod::is_patchable_at(address instr_addr) { aoqi@0: assert(insts_contains(instr_addr), "wrong nmethod used"); aoqi@0: if (is_zombie()) { aoqi@0: // a zombie may never be patched aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address nmethod::continuation_for_implicit_exception(address pc) { aoqi@0: // Exception happened outside inline-cache check code => we are inside aoqi@0: // an active nmethod => use cpc to determine a return address aoqi@0: int exception_offset = pc - code_begin(); aoqi@0: int cont_offset = ImplicitExceptionTable(this).at( exception_offset ); aoqi@0: #ifdef ASSERT aoqi@0: if (cont_offset == 0) { aoqi@0: Thread* thread = ThreadLocalStorage::get_thread_slow(); aoqi@0: ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY aoqi@0: HandleMark hm(thread); aoqi@0: ResourceMark rm(thread); aoqi@0: CodeBlob* cb = CodeCache::find_blob(pc); aoqi@0: assert(cb != NULL && cb == this, ""); aoqi@0: tty->print_cr("implicit exception happened at " INTPTR_FORMAT, pc); aoqi@0: print(); aoqi@0: method()->print_codes(); aoqi@0: print_code(); aoqi@0: print_pcs(); aoqi@0: } aoqi@0: #endif aoqi@0: if (cont_offset == 0) { aoqi@0: // Let the normal error handling report the exception aoqi@0: return NULL; aoqi@0: } aoqi@0: return code_begin() + cont_offset; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: void nmethod_init() { aoqi@0: // make sure you didn't forget to adjust the filler fields aoqi@0: assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //------------------------------------------------------------------------------------------- aoqi@0: aoqi@0: aoqi@0: // QQQ might we make this work from a frame?? aoqi@0: nmethodLocker::nmethodLocker(address pc) { aoqi@0: CodeBlob* cb = CodeCache::find_blob(pc); aoqi@0: guarantee(cb != NULL && cb->is_nmethod(), "bad pc for a nmethod found"); aoqi@0: _nm = (nmethod*)cb; aoqi@0: lock_nmethod(_nm); aoqi@0: } aoqi@0: aoqi@0: // Only JvmtiDeferredEvent::compiled_method_unload_event() aoqi@0: // should pass zombie_ok == true. aoqi@0: void nmethodLocker::lock_nmethod(nmethod* nm, bool zombie_ok) { aoqi@0: if (nm == NULL) return; aoqi@0: Atomic::inc(&nm->_lock_count); aoqi@0: guarantee(zombie_ok || !nm->is_zombie(), "cannot lock a zombie method"); aoqi@0: } aoqi@0: aoqi@0: void nmethodLocker::unlock_nmethod(nmethod* nm) { aoqi@0: if (nm == NULL) return; aoqi@0: Atomic::dec(&nm->_lock_count); aoqi@0: guarantee(nm->_lock_count >= 0, "unmatched nmethod lock/unlock"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ----------------------------------------------------------------------------- aoqi@0: // nmethod::get_deopt_original_pc aoqi@0: // aoqi@0: // Return the original PC for the given PC if: aoqi@0: // (a) the given PC belongs to a nmethod and aoqi@0: // (b) it is a deopt PC aoqi@0: address nmethod::get_deopt_original_pc(const frame* fr) { aoqi@0: if (fr->cb() == NULL) return NULL; aoqi@0: aoqi@0: nmethod* nm = fr->cb()->as_nmethod_or_null(); aoqi@0: if (nm != NULL && nm->is_deopt_pc(fr->pc())) aoqi@0: return nm->get_original_pc(fr); aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ----------------------------------------------------------------------------- aoqi@0: // MethodHandle aoqi@0: aoqi@0: bool nmethod::is_method_handle_return(address return_pc) { aoqi@0: if (!has_method_handle_invokes()) return false; aoqi@0: PcDesc* pd = pc_desc_at(return_pc); aoqi@0: if (pd == NULL) aoqi@0: return false; aoqi@0: return pd->is_method_handle_invoke(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ----------------------------------------------------------------------------- aoqi@0: // Verification aoqi@0: aoqi@0: class VerifyOopsClosure: public OopClosure { aoqi@0: nmethod* _nm; aoqi@0: bool _ok; aoqi@0: public: aoqi@0: VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { } aoqi@0: bool ok() { return _ok; } aoqi@0: virtual void do_oop(oop* p) { aoqi@0: if ((*p) == NULL || (*p)->is_oop()) return; aoqi@0: if (_ok) { aoqi@0: _nm->print_nmethod(true); aoqi@0: _ok = false; aoqi@0: } aoqi@0: tty->print_cr("*** non-oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", aoqi@0: (void *)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); aoqi@0: } aoqi@0: virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } aoqi@0: }; aoqi@0: aoqi@0: void nmethod::verify() { aoqi@0: aoqi@0: // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant aoqi@0: // seems odd. aoqi@0: aoqi@0: if( is_zombie() || is_not_entrant() ) aoqi@0: return; aoqi@0: aoqi@0: // Make sure all the entry points are correctly aligned for patching. aoqi@1: NOT_MIPS64(NativeJump)MIPS64_ONLY(NativeGeneralJump)::check_verified_entry_alignment(entry_point(), verified_entry_point()); aoqi@0: aoqi@0: // assert(method()->is_oop(), "must be valid"); aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: if (!CodeCache::contains(this)) { aoqi@0: fatal(err_msg("nmethod at " INTPTR_FORMAT " not in zone", this)); aoqi@0: } aoqi@0: aoqi@0: if(is_native_method() ) aoqi@0: return; aoqi@0: aoqi@0: nmethod* nm = CodeCache::find_nmethod(verified_entry_point()); aoqi@0: if (nm != this) { aoqi@0: fatal(err_msg("findNMethod did not find this nmethod (" INTPTR_FORMAT ")", aoqi@0: this)); aoqi@0: } aoqi@0: aoqi@0: for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { aoqi@0: if (! p->verify(this)) { aoqi@0: tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", this); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: VerifyOopsClosure voc(this); aoqi@0: oops_do(&voc); aoqi@0: assert(voc.ok(), "embedded oops must be OK"); aoqi@0: verify_scavenge_root_oops(); aoqi@0: aoqi@0: verify_scopes(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::verify_interrupt_point(address call_site) { aoqi@0: // Verify IC only when nmethod installation is finished. aoqi@0: bool is_installed = (method()->code() == this) // nmethod is in state 'in_use' and installed aoqi@0: || !this->is_in_use(); // nmethod is installed, but not in 'in_use' state aoqi@0: if (is_installed) { aoqi@0: Thread *cur = Thread::current(); aoqi@0: if (CompiledIC_lock->owner() == cur || aoqi@0: ((cur->is_VM_thread() || cur->is_ConcurrentGC_thread()) && aoqi@0: SafepointSynchronize::is_at_safepoint())) { aoqi@0: CompiledIC_at(this, call_site); aoqi@0: CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); aoqi@0: } else { aoqi@0: MutexLocker ml_verify (CompiledIC_lock); aoqi@0: CompiledIC_at(this, call_site); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: PcDesc* pd = pc_desc_at(nativeCall_at(call_site)->return_address()); aoqi@0: assert(pd != NULL, "PcDesc must exist"); aoqi@0: for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(), aoqi@0: pd->obj_decode_offset(), pd->should_reexecute(), aoqi@0: pd->return_oop()); aoqi@0: !sd->is_top(); sd = sd->sender()) { aoqi@0: sd->verify(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void nmethod::verify_scopes() { aoqi@0: if( !method() ) return; // Runtime stubs have no scope aoqi@0: if (method()->is_native()) return; // Ignore stub methods. aoqi@0: // iterate through all interrupt point aoqi@0: // and verify the debug information is valid. aoqi@0: RelocIterator iter((nmethod*)this); aoqi@0: while (iter.next()) { aoqi@0: address stub = NULL; aoqi@0: switch (iter.type()) { aoqi@0: case relocInfo::virtual_call_type: aoqi@0: verify_interrupt_point(iter.addr()); aoqi@0: break; aoqi@0: case relocInfo::opt_virtual_call_type: aoqi@0: stub = iter.opt_virtual_call_reloc()->static_stub(); aoqi@0: verify_interrupt_point(iter.addr()); aoqi@0: break; aoqi@0: case relocInfo::static_call_type: aoqi@0: stub = iter.static_call_reloc()->static_stub(); aoqi@0: //verify_interrupt_point(iter.addr()); aoqi@0: break; aoqi@0: case relocInfo::runtime_call_type: aoqi@0: address destination = iter.reloc()->value(); aoqi@0: // Right now there is no way to find out which entries support aoqi@0: // an interrupt point. It would be nice if we had this aoqi@0: // information in a table. aoqi@0: break; aoqi@0: } aoqi@0: assert(stub == NULL || stub_contains(stub), "static call stub outside stub section"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ----------------------------------------------------------------------------- aoqi@0: // Non-product code aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: class DebugScavengeRoot: public OopClosure { aoqi@0: nmethod* _nm; aoqi@0: bool _ok; aoqi@0: public: aoqi@0: DebugScavengeRoot(nmethod* nm) : _nm(nm), _ok(true) { } aoqi@0: bool ok() { return _ok; } aoqi@0: virtual void do_oop(oop* p) { aoqi@0: if ((*p) == NULL || !(*p)->is_scavengable()) return; aoqi@0: if (_ok) { aoqi@0: _nm->print_nmethod(true); aoqi@0: _ok = false; aoqi@0: } aoqi@0: tty->print_cr("*** scavengable oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", aoqi@0: (void *)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); aoqi@0: (*p)->print(); aoqi@0: } aoqi@0: virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } aoqi@0: }; aoqi@0: aoqi@0: void nmethod::verify_scavenge_root_oops() { aoqi@0: if (!on_scavenge_root_list()) { aoqi@0: // Actually look inside, to verify the claim that it's clean. aoqi@0: DebugScavengeRoot debug_scavenge_root(this); aoqi@0: oops_do(&debug_scavenge_root); aoqi@0: if (!debug_scavenge_root.ok()) aoqi@0: fatal("found an unadvertised bad scavengable oop in the code cache"); aoqi@0: } aoqi@0: assert(scavenge_root_not_marked(), ""); aoqi@0: } aoqi@0: aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: // Printing operations aoqi@0: aoqi@0: void nmethod::print() const { aoqi@0: ResourceMark rm; aoqi@0: ttyLocker ttyl; // keep the following output all in one block aoqi@0: aoqi@0: tty->print("Compiled method "); aoqi@0: aoqi@0: if (is_compiled_by_c1()) { aoqi@0: tty->print("(c1) "); aoqi@0: } else if (is_compiled_by_c2()) { aoqi@0: tty->print("(c2) "); aoqi@0: } else if (is_compiled_by_shark()) { aoqi@0: tty->print("(shark) "); aoqi@0: } else { aoqi@0: tty->print("(nm) "); aoqi@0: } aoqi@0: aoqi@0: print_on(tty, NULL); aoqi@0: aoqi@0: if (WizardMode) { aoqi@0: tty->print("((nmethod*) "INTPTR_FORMAT ") ", this); aoqi@0: tty->print(" for method " INTPTR_FORMAT , (address)method()); aoqi@0: tty->print(" { "); aoqi@0: if (is_in_use()) tty->print("in_use "); aoqi@0: if (is_not_entrant()) tty->print("not_entrant "); aoqi@0: if (is_zombie()) tty->print("zombie "); aoqi@0: if (is_unloaded()) tty->print("unloaded "); aoqi@0: if (on_scavenge_root_list()) tty->print("scavenge_root "); aoqi@0: tty->print_cr("}:"); aoqi@0: } aoqi@0: if (size () > 0) tty->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: (address)this, aoqi@0: (address)this + size(), aoqi@0: size()); aoqi@0: if (relocation_size () > 0) tty->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: relocation_begin(), aoqi@0: relocation_end(), aoqi@0: relocation_size()); aoqi@0: if (consts_size () > 0) tty->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: consts_begin(), aoqi@0: consts_end(), aoqi@0: consts_size()); aoqi@0: if (insts_size () > 0) tty->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: insts_begin(), aoqi@0: insts_end(), aoqi@0: insts_size()); aoqi@0: if (stub_size () > 0) tty->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: stub_begin(), aoqi@0: stub_end(), aoqi@0: stub_size()); aoqi@0: if (oops_size () > 0) tty->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: oops_begin(), aoqi@0: oops_end(), aoqi@0: oops_size()); aoqi@0: if (metadata_size () > 0) tty->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: metadata_begin(), aoqi@0: metadata_end(), aoqi@0: metadata_size()); aoqi@0: if (scopes_data_size () > 0) tty->print_cr(" scopes data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: scopes_data_begin(), aoqi@0: scopes_data_end(), aoqi@0: scopes_data_size()); aoqi@0: if (scopes_pcs_size () > 0) tty->print_cr(" scopes pcs [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: scopes_pcs_begin(), aoqi@0: scopes_pcs_end(), aoqi@0: scopes_pcs_size()); aoqi@0: if (dependencies_size () > 0) tty->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: dependencies_begin(), aoqi@0: dependencies_end(), aoqi@0: dependencies_size()); aoqi@0: if (handler_table_size() > 0) tty->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: handler_table_begin(), aoqi@0: handler_table_end(), aoqi@0: handler_table_size()); aoqi@0: if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", aoqi@0: nul_chk_table_begin(), aoqi@0: nul_chk_table_end(), aoqi@0: nul_chk_table_size()); aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_code() { aoqi@0: HandleMark hm; aoqi@0: ResourceMark m; aoqi@0: Disassembler::decode(this); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: void nmethod::print_scopes() { aoqi@0: // Find the first pc desc for all scopes in the code and print it. aoqi@0: ResourceMark rm; aoqi@0: for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { aoqi@0: if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null) aoqi@0: continue; aoqi@0: aoqi@0: ScopeDesc* sd = scope_desc_at(p->real_pc(this)); aoqi@0: sd->print_on(tty, p); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_dependencies() { aoqi@0: ResourceMark rm; aoqi@0: ttyLocker ttyl; // keep the following output all in one block aoqi@0: tty->print_cr("Dependencies:"); aoqi@0: for (Dependencies::DepStream deps(this); deps.next(); ) { aoqi@0: deps.print_dependency(); aoqi@0: Klass* ctxk = deps.context_type(); aoqi@0: if (ctxk != NULL) { aoqi@0: if (ctxk->oop_is_instance() && ((InstanceKlass*)ctxk)->is_dependent_nmethod(this)) { aoqi@0: tty->print_cr(" [nmethod<=klass]%s", ctxk->external_name()); aoqi@0: } aoqi@0: } aoqi@0: deps.log_dependency(); // put it into the xml log also aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::print_relocations() { aoqi@0: ResourceMark m; // in case methods get printed via the debugger aoqi@0: tty->print_cr("relocations:"); aoqi@0: RelocIterator iter(this); aoqi@0: iter.print(); aoqi@0: if (UseRelocIndex) { aoqi@0: jint* index_end = (jint*)relocation_end() - 1; aoqi@0: jint index_size = *index_end; aoqi@0: jint* index_start = (jint*)( (address)index_end - index_size ); aoqi@0: tty->print_cr(" index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size); aoqi@0: if (index_size > 0) { aoqi@0: jint* ip; aoqi@0: for (ip = index_start; ip+2 <= index_end; ip += 2) aoqi@0: tty->print_cr(" (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT, aoqi@0: ip[0], aoqi@0: ip[1], aoqi@0: header_end()+ip[0], aoqi@0: relocation_begin()-1+ip[1]); aoqi@0: for (; ip < index_end; ip++) aoqi@0: tty->print_cr(" (%d ?)", ip[0]); aoqi@0: tty->print_cr(" @" INTPTR_FORMAT ": index_size=%d", ip, *ip); aoqi@0: ip++; aoqi@0: tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void nmethod::print_pcs() { aoqi@0: ResourceMark m; // in case methods get printed via debugger aoqi@0: tty->print_cr("pc-bytecode offsets:"); aoqi@0: for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { aoqi@0: p->print(this); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: const char* nmethod::reloc_string_for(u_char* begin, u_char* end) { aoqi@0: RelocIterator iter(this, begin, end); aoqi@0: bool have_one = false; aoqi@0: while (iter.next()) { aoqi@0: have_one = true; aoqi@0: switch (iter.type()) { aoqi@0: case relocInfo::none: return "no_reloc"; aoqi@0: case relocInfo::oop_type: { aoqi@0: stringStream st; aoqi@0: oop_Relocation* r = iter.oop_reloc(); aoqi@0: oop obj = r->oop_value(); aoqi@0: st.print("oop("); aoqi@0: if (obj == NULL) st.print("NULL"); aoqi@0: else obj->print_value_on(&st); aoqi@0: st.print(")"); aoqi@0: return st.as_string(); aoqi@0: } aoqi@0: case relocInfo::metadata_type: { aoqi@0: stringStream st; aoqi@0: metadata_Relocation* r = iter.metadata_reloc(); aoqi@0: Metadata* obj = r->metadata_value(); aoqi@0: st.print("metadata("); aoqi@0: if (obj == NULL) st.print("NULL"); aoqi@0: else obj->print_value_on(&st); aoqi@0: st.print(")"); aoqi@0: return st.as_string(); aoqi@0: } aoqi@0: case relocInfo::virtual_call_type: return "virtual_call"; aoqi@0: case relocInfo::opt_virtual_call_type: return "optimized virtual_call"; aoqi@0: case relocInfo::static_call_type: return "static_call"; aoqi@0: case relocInfo::static_stub_type: return "static_stub"; aoqi@0: case relocInfo::runtime_call_type: return "runtime_call"; aoqi@0: case relocInfo::external_word_type: return "external_word"; aoqi@0: case relocInfo::internal_word_type: return "internal_word"; aoqi@0: case relocInfo::section_word_type: return "section_word"; aoqi@0: case relocInfo::poll_type: return "poll"; aoqi@0: case relocInfo::poll_return_type: return "poll_return"; aoqi@0: case relocInfo::type_mask: return "type_bit_mask"; aoqi@0: } aoqi@0: } aoqi@0: return have_one ? "other" : NULL; aoqi@0: } aoqi@0: aoqi@0: // Return a the last scope in (begin..end] aoqi@0: ScopeDesc* nmethod::scope_desc_in(address begin, address end) { aoqi@0: PcDesc* p = pc_desc_near(begin+1); aoqi@0: if (p != NULL && p->real_pc(this) <= end) { aoqi@0: return new ScopeDesc(this, p->scope_decode_offset(), aoqi@0: p->obj_decode_offset(), p->should_reexecute(), aoqi@0: p->return_oop()); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_nmethod_labels(outputStream* stream, address block_begin) const { aoqi@0: if (block_begin == entry_point()) stream->print_cr("[Entry Point]"); aoqi@0: if (block_begin == verified_entry_point()) stream->print_cr("[Verified Entry Point]"); aoqi@0: if (block_begin == exception_begin()) stream->print_cr("[Exception Handler]"); aoqi@0: if (block_begin == stub_begin()) stream->print_cr("[Stub Code]"); aoqi@0: if (block_begin == deopt_handler_begin()) stream->print_cr("[Deopt Handler Code]"); aoqi@0: aoqi@0: if (has_method_handle_invokes()) aoqi@0: if (block_begin == deopt_mh_handler_begin()) stream->print_cr("[Deopt MH Handler Code]"); aoqi@0: aoqi@0: if (block_begin == consts_begin()) stream->print_cr("[Constants]"); aoqi@0: aoqi@0: if (block_begin == entry_point()) { aoqi@0: methodHandle m = method(); aoqi@0: if (m.not_null()) { aoqi@0: stream->print(" # "); aoqi@0: m->print_value_on(stream); aoqi@0: stream->cr(); aoqi@0: } aoqi@0: if (m.not_null() && !is_osr_method()) { aoqi@0: ResourceMark rm; aoqi@0: int sizeargs = m->size_of_parameters(); aoqi@0: BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); aoqi@0: VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); aoqi@0: { aoqi@0: int sig_index = 0; aoqi@0: if (!m->is_static()) aoqi@0: sig_bt[sig_index++] = T_OBJECT; // 'this' aoqi@0: for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) { aoqi@0: BasicType t = ss.type(); aoqi@0: sig_bt[sig_index++] = t; aoqi@0: if (type2size[t] == 2) { aoqi@0: sig_bt[sig_index++] = T_VOID; aoqi@0: } else { aoqi@0: assert(type2size[t] == 1, "size is 1 or 2"); aoqi@0: } aoqi@0: } aoqi@0: assert(sig_index == sizeargs, ""); aoqi@0: } aoqi@0: const char* spname = "sp"; // make arch-specific? aoqi@0: intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, false); aoqi@0: int stack_slot_offset = this->frame_size() * wordSize; aoqi@0: int tab1 = 14, tab2 = 24; aoqi@0: int sig_index = 0; aoqi@0: int arg_index = (m->is_static() ? 0 : -1); aoqi@0: bool did_old_sp = false; aoqi@0: for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) { aoqi@0: bool at_this = (arg_index == -1); aoqi@0: bool at_old_sp = false; aoqi@0: BasicType t = (at_this ? T_OBJECT : ss.type()); aoqi@0: assert(t == sig_bt[sig_index], "sigs in sync"); aoqi@0: if (at_this) aoqi@0: stream->print(" # this: "); aoqi@0: else aoqi@0: stream->print(" # parm%d: ", arg_index); aoqi@0: stream->move_to(tab1); aoqi@0: VMReg fst = regs[sig_index].first(); aoqi@0: VMReg snd = regs[sig_index].second(); aoqi@0: if (fst->is_reg()) { aoqi@0: stream->print("%s", fst->name()); aoqi@0: if (snd->is_valid()) { aoqi@0: stream->print(":%s", snd->name()); aoqi@0: } aoqi@0: } else if (fst->is_stack()) { aoqi@0: int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset; aoqi@0: if (offset == stack_slot_offset) at_old_sp = true; aoqi@0: stream->print("[%s+0x%x]", spname, offset); aoqi@0: } else { aoqi@0: stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd); aoqi@0: } aoqi@0: stream->print(" "); aoqi@0: stream->move_to(tab2); aoqi@0: stream->print("= "); aoqi@0: if (at_this) { aoqi@0: m->method_holder()->print_value_on(stream); aoqi@0: } else { aoqi@0: bool did_name = false; aoqi@0: if (!at_this && ss.is_object()) { aoqi@0: Symbol* name = ss.as_symbol_or_null(); aoqi@0: if (name != NULL) { aoqi@0: name->print_value_on(stream); aoqi@0: did_name = true; aoqi@0: } aoqi@0: } aoqi@0: if (!did_name) aoqi@0: stream->print("%s", type2name(t)); aoqi@0: } aoqi@0: if (at_old_sp) { aoqi@0: stream->print(" (%s of caller)", spname); aoqi@0: did_old_sp = true; aoqi@0: } aoqi@0: stream->cr(); aoqi@0: sig_index += type2size[t]; aoqi@0: arg_index += 1; aoqi@0: if (!at_this) ss.next(); aoqi@0: } aoqi@0: if (!did_old_sp) { aoqi@0: stream->print(" # "); aoqi@0: stream->move_to(tab1); aoqi@0: stream->print("[%s+0x%x]", spname, stack_slot_offset); aoqi@0: stream->print(" (%s of caller)", spname); aoqi@0: stream->cr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) { aoqi@0: // First, find an oopmap in (begin, end]. aoqi@0: // We use the odd half-closed interval so that oop maps and scope descs aoqi@0: // which are tied to the byte after a call are printed with the call itself. aoqi@0: address base = code_begin(); aoqi@0: OopMapSet* oms = oop_maps(); aoqi@0: if (oms != NULL) { aoqi@0: for (int i = 0, imax = oms->size(); i < imax; i++) { aoqi@0: OopMap* om = oms->at(i); aoqi@0: address pc = base + om->offset(); aoqi@0: if (pc > begin) { aoqi@0: if (pc <= end) { aoqi@0: st->move_to(column); aoqi@0: st->print("; "); aoqi@0: om->print_on(st); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Print any debug info present at this pc. aoqi@0: ScopeDesc* sd = scope_desc_in(begin, end); aoqi@0: if (sd != NULL) { aoqi@0: st->move_to(column); aoqi@0: if (sd->bci() == SynchronizationEntryBCI) { aoqi@0: st->print(";*synchronization entry"); aoqi@0: } else { aoqi@0: if (sd->method() == NULL) { aoqi@0: st->print("method is NULL"); aoqi@0: } else if (sd->method()->is_native()) { aoqi@0: st->print("method is native"); aoqi@0: } else { aoqi@0: Bytecodes::Code bc = sd->method()->java_code_at(sd->bci()); aoqi@0: st->print(";*%s", Bytecodes::name(bc)); aoqi@0: switch (bc) { aoqi@0: case Bytecodes::_invokevirtual: aoqi@0: case Bytecodes::_invokespecial: aoqi@0: case Bytecodes::_invokestatic: aoqi@0: case Bytecodes::_invokeinterface: aoqi@0: { aoqi@0: Bytecode_invoke invoke(sd->method(), sd->bci()); aoqi@0: st->print(" "); aoqi@0: if (invoke.name() != NULL) aoqi@0: invoke.name()->print_symbol_on(st); aoqi@0: else aoqi@0: st->print(""); aoqi@0: break; aoqi@0: } aoqi@0: case Bytecodes::_getfield: aoqi@0: case Bytecodes::_putfield: aoqi@0: case Bytecodes::_getstatic: aoqi@0: case Bytecodes::_putstatic: aoqi@0: { aoqi@0: Bytecode_field field(sd->method(), sd->bci()); aoqi@0: st->print(" "); aoqi@0: if (field.name() != NULL) aoqi@0: field.name()->print_symbol_on(st); aoqi@0: else aoqi@0: st->print(""); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Print all scopes aoqi@0: for (;sd != NULL; sd = sd->sender()) { aoqi@0: st->move_to(column); aoqi@0: st->print("; -"); aoqi@0: if (sd->method() == NULL) { aoqi@0: st->print("method is NULL"); aoqi@0: } else { aoqi@0: sd->method()->print_short_name(st); aoqi@0: } aoqi@0: int lineno = sd->method()->line_number_from_bci(sd->bci()); aoqi@0: if (lineno != -1) { aoqi@0: st->print("@%d (line %d)", sd->bci(), lineno); aoqi@0: } else { aoqi@0: st->print("@%d", sd->bci()); aoqi@0: } aoqi@0: st->cr(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Print relocation information aoqi@0: const char* str = reloc_string_for(begin, end); aoqi@0: if (str != NULL) { aoqi@0: if (sd != NULL) st->cr(); aoqi@0: st->move_to(column); aoqi@0: st->print("; {%s}", str); aoqi@0: } aoqi@0: int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin()); aoqi@0: if (cont_offset != 0) { aoqi@0: st->move_to(column); aoqi@0: st->print("; implicit exception: dispatches to " INTPTR_FORMAT, code_begin() + cont_offset); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: void nmethod::print_value_on(outputStream* st) const { aoqi@0: st->print("nmethod"); aoqi@0: print_on(st, NULL); aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_calls(outputStream* st) { aoqi@0: RelocIterator iter(this); aoqi@0: while (iter.next()) { aoqi@0: switch (iter.type()) { aoqi@0: case relocInfo::virtual_call_type: aoqi@0: case relocInfo::opt_virtual_call_type: { aoqi@0: VerifyMutexLocker mc(CompiledIC_lock); aoqi@0: CompiledIC_at(iter.reloc())->print(); aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::static_call_type: aoqi@0: st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr()); aoqi@0: compiledStaticCall_at(iter.reloc())->print(); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_handler_table() { aoqi@0: ExceptionHandlerTable(this).print(); aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_nul_chk_table() { aoqi@0: ImplicitExceptionTable(this).print(code_begin()); aoqi@0: } aoqi@0: aoqi@0: void nmethod::print_statistics() { aoqi@0: ttyLocker ttyl; aoqi@0: if (xtty != NULL) xtty->head("statistics type='nmethod'"); aoqi@0: nmethod_stats.print_native_nmethod_stats(); aoqi@0: nmethod_stats.print_nmethod_stats(); aoqi@0: DebugInformationRecorder::print_statistics(); aoqi@0: nmethod_stats.print_pc_stats(); aoqi@0: Dependencies::print_statistics(); aoqi@0: if (xtty != NULL) xtty->tail("statistics"); aoqi@0: } aoqi@0: aoqi@0: #endif // PRODUCT