Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/code/NMethod.java
41171 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.code;2526import java.io.*;27import java.util.*;28import sun.jvm.hotspot.debugger.*;29import sun.jvm.hotspot.oops.*;30import sun.jvm.hotspot.runtime.*;31import sun.jvm.hotspot.types.*;32import sun.jvm.hotspot.utilities.*;33import sun.jvm.hotspot.utilities.Observable;34import sun.jvm.hotspot.utilities.Observer;3536public class NMethod extends CompiledMethod {37private static long pcDescSize;38/** != InvocationEntryBci if this nmethod is an on-stack replacement method */39private static CIntegerField entryBCIField;40/** To support simple linked-list chaining of nmethods */41private static AddressField osrLinkField;4243/** Offsets for different nmethod parts */44private static CIntegerField exceptionOffsetField;45private static CIntegerField origPCOffsetField;46private static CIntegerField stubOffsetField;47private static CIntegerField oopsOffsetField;48private static CIntegerField metadataOffsetField;49private static CIntegerField scopesPCsOffsetField;50private static CIntegerField dependenciesOffsetField;51private static CIntegerField handlerTableOffsetField;52private static CIntegerField nulChkTableOffsetField;53private static CIntegerField nmethodEndOffsetField;5455/** Offsets for entry points */56/** Entry point with class check */57private static AddressField entryPointField;58/** Entry point without class check */59private static AddressField verifiedEntryPointField;60/** Entry point for on stack replacement */61private static AddressField osrEntryPointField;6263// FIXME: add access to flags (how?)6465/** NMethod Flushing lock (if non-zero, then the nmethod is not removed) */66private static JIntField lockCountField;6768/** not_entrant method removal. Each mark_sweep pass will update69this mark to current sweep invocation count if it is seen on the70stack. An not_entrant method can be removed when there is no71more activations, i.e., when the _stack_traversal_mark is less than72current sweep traversal index. */73private static CIntegerField stackTraversalMarkField;7475private static CIntegerField compLevelField;7677static {78VM.registerVMInitializedObserver(new Observer() {79public void update(Observable o, Object data) {80initialize(VM.getVM().getTypeDataBase());81}82});83}8485private static void initialize(TypeDataBase db) {86Type type = db.lookupType("nmethod");8788entryBCIField = type.getCIntegerField("_entry_bci");89osrLinkField = type.getAddressField("_osr_link");9091exceptionOffsetField = type.getCIntegerField("_exception_offset");92origPCOffsetField = type.getCIntegerField("_orig_pc_offset");93stubOffsetField = type.getCIntegerField("_stub_offset");94oopsOffsetField = type.getCIntegerField("_oops_offset");95metadataOffsetField = type.getCIntegerField("_metadata_offset");96scopesPCsOffsetField = type.getCIntegerField("_scopes_pcs_offset");97dependenciesOffsetField = type.getCIntegerField("_dependencies_offset");98handlerTableOffsetField = type.getCIntegerField("_handler_table_offset");99nulChkTableOffsetField = type.getCIntegerField("_nul_chk_table_offset");100nmethodEndOffsetField = type.getCIntegerField("_nmethod_end_offset");101entryPointField = type.getAddressField("_entry_point");102verifiedEntryPointField = type.getAddressField("_verified_entry_point");103osrEntryPointField = type.getAddressField("_osr_entry_point");104lockCountField = type.getJIntField("_lock_count");105stackTraversalMarkField = type.getCIntegerField("_stack_traversal_mark");106compLevelField = type.getCIntegerField("_comp_level");107pcDescSize = db.lookupType("PcDesc").getSize();108}109110public NMethod(Address addr) {111super(addr);112}113114// Accessors115public Address getAddress() {116return addr;117}118119// Type info120public boolean isNMethod() { return true; }121public boolean isJavaMethod() { return !getMethod().isNative(); }122public boolean isNativeMethod() { return getMethod().isNative(); }123public boolean isOSRMethod() { return getEntryBCI() != VM.getVM().getInvocationEntryBCI(); }124125/** Boundaries for different parts */126public Address constantsBegin() { return contentBegin(); }127public Address constantsEnd() { return getEntryPoint(); }128public Address instsBegin() { return codeBegin(); }129public Address instsEnd() { return headerBegin().addOffsetTo(getStubOffset()); }130public Address exceptionBegin() { return headerBegin().addOffsetTo(getExceptionOffset()); }131public Address stubBegin() { return headerBegin().addOffsetTo(getStubOffset()); }132public Address stubEnd() { return headerBegin().addOffsetTo(getOopsOffset()); }133public Address oopsBegin() { return headerBegin().addOffsetTo(getOopsOffset()); }134public Address oopsEnd() { return headerBegin().addOffsetTo(getMetadataOffset()); }135public Address metadataBegin() { return headerBegin().addOffsetTo(getMetadataOffset()); }136public Address metadataEnd() { return scopesDataBegin(); }137public Address scopesDataEnd() { return headerBegin().addOffsetTo(getScopesPCsOffset()); }138public Address scopesPCsBegin() { return headerBegin().addOffsetTo(getScopesPCsOffset()); }139public Address scopesPCsEnd() { return headerBegin().addOffsetTo(getDependenciesOffset()); }140public Address dependenciesBegin() { return headerBegin().addOffsetTo(getDependenciesOffset()); }141public Address dependenciesEnd() { return headerBegin().addOffsetTo(getHandlerTableOffset()); }142public Address handlerTableBegin() { return headerBegin().addOffsetTo(getHandlerTableOffset()); }143public Address handlerTableEnd() { return headerBegin().addOffsetTo(getNulChkTableOffset()); }144public Address nulChkTableBegin() { return headerBegin().addOffsetTo(getNulChkTableOffset()); }145public Address nulChkTableEnd() { return headerBegin().addOffsetTo(getNMethodEndOffset()); }146147public int constantsSize() { return (int) constantsEnd() .minus(constantsBegin()); }148public int instsSize() { return (int) instsEnd() .minus(instsBegin()); }149public int stubSize() { return (int) stubEnd() .minus(stubBegin()); }150public int oopsSize() { return (int) oopsEnd() .minus(oopsBegin()); }151public int metadataSize() { return (int) metadataEnd() .minus(metadataBegin()); }152public int scopesDataSize() { return (int) scopesDataEnd() .minus(scopesDataBegin()); }153public int scopesPCsSize() { return (int) scopesPCsEnd() .minus(scopesPCsBegin()); }154public int dependenciesSize() { return (int) dependenciesEnd().minus(dependenciesBegin()); }155public int handlerTableSize() { return (int) handlerTableEnd().minus(handlerTableBegin()); }156public int nulChkTableSize() { return (int) nulChkTableEnd() .minus(nulChkTableBegin()); }157public int origPCOffset() { return (int) origPCOffsetField.getValue(addr); }158159public int totalSize() {160return161constantsSize() +162instsSize() +163stubSize() +164scopesDataSize() +165scopesPCsSize() +166dependenciesSize() +167handlerTableSize() +168nulChkTableSize();169}170171public boolean constantsContains (Address addr) { return constantsBegin() .lessThanOrEqual(addr) && constantsEnd() .greaterThan(addr); }172public boolean instsContains (Address addr) { return instsBegin() .lessThanOrEqual(addr) && instsEnd() .greaterThan(addr); }173public boolean stubContains (Address addr) { return stubBegin() .lessThanOrEqual(addr) && stubEnd() .greaterThan(addr); }174public boolean oopsContains (Address addr) { return oopsBegin() .lessThanOrEqual(addr) && oopsEnd() .greaterThan(addr); }175public boolean metadataContains (Address addr) { return metadataBegin() .lessThanOrEqual(addr) && metadataEnd() .greaterThan(addr); }176public boolean scopesDataContains (Address addr) { return scopesDataBegin() .lessThanOrEqual(addr) && scopesDataEnd() .greaterThan(addr); }177public boolean scopesPCsContains (Address addr) { return scopesPCsBegin() .lessThanOrEqual(addr) && scopesPCsEnd() .greaterThan(addr); }178public boolean handlerTableContains(Address addr) { return handlerTableBegin().lessThanOrEqual(addr) && handlerTableEnd().greaterThan(addr); }179public boolean nulChkTableContains (Address addr) { return nulChkTableBegin() .lessThanOrEqual(addr) && nulChkTableEnd() .greaterThan(addr); }180181public int getOopsLength() { return (int) (oopsSize() / VM.getVM().getOopSize()); }182public int getMetadataLength() { return (int) (metadataSize() / VM.getVM().getOopSize()); }183184/** Entry points */185public Address getEntryPoint() { return entryPointField.getValue(addr); }186public Address getVerifiedEntryPoint() { return verifiedEntryPointField.getValue(addr); }187188/** Support for oops in scopes and relocs. Note: index 0 is reserved for null. */189public OopHandle getOopAt(int index) {190if (index == 0) return null;191if (Assert.ASSERTS_ENABLED) {192Assert.that(index > 0 && index <= getOopsLength(), "must be a valid non-zero index");193}194return oopsBegin().getOopHandleAt((index - 1) * VM.getVM().getOopSize());195}196197/** Support for metadata in scopes and relocs. Note: index 0 is reserved for null. */198public Address getMetadataAt(int index) {199if (index == 0) return null;200if (Assert.ASSERTS_ENABLED) {201Assert.that(index > 0 && index <= getMetadataLength(), "must be a valid non-zero index");202}203return metadataBegin().getAddressAt((index - 1) * VM.getVM().getOopSize());204}205206public Method getMethodAt(int index) {207return (Method)Metadata.instantiateWrapperFor(getMetadataAt(index));208}209210// FIXME: add interpreter_entry_point()211// FIXME: add lazy_interpreter_entry_point() for C2212213// **********214// * FIXME: * ADD ACCESS TO FLAGS!!!!215// **********216// public boolean isInUse();217// public boolean isAlive();218// public boolean isNotEntrant();219// public boolean isZombie();220221// ********************************222// * MAJOR FIXME: MAJOR HACK HERE *223// ********************************224public boolean isZombie() { return false; }225226// public boolean isUnloaded();227// public boolean isYoung();228// public boolean isOld();229// public int age();230// public boolean isMarkedForDeoptimization();231// public boolean isMarkedForUnloading();232// public int level();233// public int version();234235// FIXME: add mutators for above236// FIXME: add exception cache access?237238/** On-stack replacement support */239// FIXME: add mutators240public int getOSREntryBCI() {241if (Assert.ASSERTS_ENABLED) {242Assert.that(getEntryBCI() != VM.getVM().getInvocationEntryBCI(), "wrong kind of nmethod");243}244return getEntryBCI();245}246247public NMethod getOSRLink() {248return (NMethod) VMObjectFactory.newObject(NMethod.class, osrLinkField.getValue(addr));249}250251// MethodHandle252public boolean isMethodHandleReturn(Address returnPc) {253// Hard to read a bit fields from Java and it's only there for performance254// so just go directly to the PCDesc255// if (!hasMethodHandleInvokes()) return false;256PCDesc pd = getPCDescAt(returnPc);257if (pd == null)258return false;259return pd.isMethodHandleInvoke();260}261262// Deopt263// Return true is the PC is one would expect if the frame is being deopted.264public boolean isDeoptPc (Address pc) { return isDeoptEntry(pc) || isDeoptMhEntry(pc); }265public boolean isDeoptEntry (Address pc) { return pc == deoptHandlerBegin(); }266public boolean isDeoptMhEntry (Address pc) { return pc == deoptMhHandlerBegin(); }267268/** Tells whether frames described by this nmethod can be269deoptimized. Note: native wrappers cannot be deoptimized. */270public boolean canBeDeoptimized() { return isJavaMethod(); }271272// FIXME: add inline cache support273// FIXME: add flush()274275public boolean isLockedByVM() { return lockCountField.getValue(addr) > 0; }276277// FIXME: add mark_as_seen_on_stack278// FIXME: add can_not_entrant_be_converted279280// FIXME: add GC support281// void follow_roots_or_mark_for_unloading(bool unloading_occurred, bool& marked_for_unloading);282// void follow_root_or_mark_for_unloading(oop* root, bool unloading_occurred, bool& marked_for_unloading);283// void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, void f(oop*));284// void adjust_pointers();285286/** Finds a PCDesc with real-pc equal to "pc" */287public PCDesc getPCDescAt(Address pc) {288// FIXME: consider adding cache like the one down in the VM289for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) {290PCDesc pcDesc = new PCDesc(p);291if (pcDesc.getRealPC(this).equals(pc)) {292return pcDesc;293}294}295return null;296}297298/** ScopeDesc for an instruction */299public ScopeDesc getScopeDescAt(Address pc) {300PCDesc pd = getPCDescAt(pc);301if (Assert.ASSERTS_ENABLED) {302Assert.that(pd != null, "scope must be present");303}304return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getObjDecodeOffset(), pd.getReexecute());305}306307/** This is only for use by the debugging system, and is only308intended for use in the topmost frame, where we are not309guaranteed to be at a PC for which we have a PCDesc. It finds310the PCDesc with realPC closest to the current PC that has311a valid scope decode offset. */312public PCDesc getPCDescNearDbg(Address pc) {313PCDesc bestGuessPCDesc = null;314long bestDistance = 0;315for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) {316PCDesc pcDesc = new PCDesc(p);317if (pcDesc.getScopeDecodeOffset() == DebugInformationRecorder.SERIALIZED_NULL) {318// We've observed a serialized null decode offset. Ignore this PcDesc.319continue;320}321// In case pc is null322long distance = -pcDesc.getRealPC(this).minus(pc);323if ((bestGuessPCDesc == null) ||324((distance >= 0) && (distance < bestDistance))) {325bestGuessPCDesc = pcDesc;326bestDistance = distance;327}328}329return bestGuessPCDesc;330}331332PCDesc find_pc_desc(long pc, boolean approximate) {333return find_pc_desc_internal(pc, approximate);334}335336// Finds a PcDesc with real-pc equal to "pc"337PCDesc find_pc_desc_internal(long pc, boolean approximate) {338long base_address = VM.getAddressValue(codeBegin());339int pc_offset = (int) (pc - base_address);340341// Fallback algorithm: quasi-linear search for the PcDesc342// Find the last pc_offset less than the given offset.343// The successor must be the required match, if there is a match at all.344// (Use a fixed radix to avoid expensive affine pointer arithmetic.)345Address lower = scopesPCsBegin();346Address upper = scopesPCsEnd();347upper = upper.addOffsetTo(-pcDescSize); // exclude final sentinel348if (lower.greaterThan(upper)) return null; // native method; no PcDescs at all349350// Take giant steps at first (4096, then 256, then 16, then 1)351int LOG2_RADIX = 4;352int RADIX = (1 << LOG2_RADIX);353Address mid;354for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) {355while ((mid = lower.addOffsetTo(step * pcDescSize)).lessThan(upper)) {356PCDesc m = new PCDesc(mid);357if (m.getPCOffset() < pc_offset) {358lower = mid;359} else {360upper = mid;361break;362}363}364}365// Sneak up on the value with a linear search of length ~16.366while (true) {367mid = lower.addOffsetTo(pcDescSize);368PCDesc m = new PCDesc(mid);369if (m.getPCOffset() < pc_offset) {370lower = mid;371} else {372upper = mid;373break;374}375}376377PCDesc u = new PCDesc(upper);378if (match_desc(u, pc_offset, approximate)) {379return u;380} else {381return null;382}383}384385// ScopeDesc retrieval operation386PCDesc pc_desc_at(long pc) { return find_pc_desc(pc, false); }387// pc_desc_near returns the first PCDesc at or after the givne pc.388PCDesc pc_desc_near(long pc) { return find_pc_desc(pc, true); }389390// Return a the last scope in (begin..end]391public ScopeDesc scope_desc_in(long begin, long end) {392PCDesc p = pc_desc_near(begin+1);393if (p != null && VM.getAddressValue(p.getRealPC(this)) <= end) {394return new ScopeDesc(this, p.getScopeDecodeOffset(), p.getObjDecodeOffset(), p.getReexecute());395}396return null;397}398399static boolean match_desc(PCDesc pc, int pc_offset, boolean approximate) {400if (!approximate) {401return pc.getPCOffset() == pc_offset;402} else {403PCDesc prev = new PCDesc(pc.getAddress().addOffsetTo(-pcDescSize));404return prev.getPCOffset() < pc_offset && pc_offset <= pc.getPCOffset();405}406}407408/** This is only for use by the debugging system, and is only409intended for use in the topmost frame, where we are not410guaranteed to be at a PC for which we have a PCDesc. It finds411the ScopeDesc closest to the current PC. NOTE that this may412return NULL for compiled methods which don't have any413ScopeDescs! */414public ScopeDesc getScopeDescNearDbg(Address pc) {415PCDesc pd = getPCDescNearDbg(pc);416if (pd == null) return null;417return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getObjDecodeOffset(), pd.getReexecute());418}419420public Map<sun.jvm.hotspot.debugger.Address, PCDesc> getSafepoints() {421Map<sun.jvm.hotspot.debugger.Address, PCDesc> safepoints = new HashMap<>();422sun.jvm.hotspot.debugger.Address p = null;423for (p = scopesPCsBegin(); p.lessThan(scopesPCsEnd());424p = p.addOffsetTo(pcDescSize)) {425PCDesc pcDesc = new PCDesc(p);426sun.jvm.hotspot.debugger.Address pc = pcDesc.getRealPC(this);427safepoints.put(pc, pcDesc);428}429return safepoints;430}431432// FIXME: add getPCOffsetForBCI()433// FIXME: add embeddedOopAt()434// FIXME: add isDependentOn()435// FIXME: add isPatchableAt()436437/** Support for code generation. Only here for proof-of-concept. */438public static int getEntryPointOffset() { return (int) entryPointField.getOffset(); }439public static int getVerifiedEntryPointOffset() { return (int) verifiedEntryPointField.getOffset(); }440public static int getOSREntryPointOffset() { return (int) osrEntryPointField.getOffset(); }441public static int getEntryBCIOffset() { return (int) entryBCIField.getOffset(); }442443public void print() {444printOn(System.out);445}446447protected void printComponentsOn(PrintStream tty) {448// FIXME: add relocation information449tty.println(" content: [" + contentBegin() + ", " + contentEnd() + "), " +450" code: [" + codeBegin() + ", " + codeEnd() + "), " +451" data: [" + dataBegin() + ", " + dataEnd() + "), " +452" oops: [" + oopsBegin() + ", " + oopsEnd() + "), " +453" frame size: " + getFrameSize());454}455456public String toString() {457Method method = getMethod();458return "NMethod for " +459method.getMethodHolder().getName().asString() + "." +460method.getName().asString() + method.getSignature().asString() + "==>n" +461super.toString();462}463464public String flagsToString() {465// FIXME need access to flags...466return "";467}468469public String getName() {470Method method = getMethod();471return "NMethod for " +472method.getMethodHolder().getName().asString() + "." +473method.getName().asString() +474method.getSignature().asString();475}476477public void dumpReplayData(PrintStream out) {478HashMap<Metadata, Metadata> h = new HashMap<>();479for (int i = 1; i < getMetadataLength(); i++) {480Metadata meta = Metadata.instantiateWrapperFor(getMetadataAt(i));481System.err.println(meta);482if (h.get(meta) != null) continue;483h.put(meta, meta);484if (meta instanceof InstanceKlass) {485((InstanceKlass)meta).dumpReplayData(out);486} else if (meta instanceof Method) {487((Method)meta).dumpReplayData(out);488MethodData mdo = ((Method)meta).getMethodData();489if (mdo != null) {490mdo.dumpReplayData(out);491}492}493}494Method method = getMethod();495if (h.get(method) == null) {496method.dumpReplayData(out);497MethodData mdo = method.getMethodData();498if (mdo != null) {499mdo.dumpReplayData(out);500}501}502if (h.get(method.getMethodHolder()) == null) {503((InstanceKlass)method.getMethodHolder()).dumpReplayData(out);504}505Klass holder = method.getMethodHolder();506out.println("compile " + holder.getName().asString() + " " +507OopUtilities.escapeString(method.getName().asString()) + " " +508method.getSignature().asString() + " " +509getEntryBCI() + " " + getCompLevel());510511}512513//--------------------------------------------------------------------------------514// Internals only below this point515//516517private int getEntryBCI() { return (int) entryBCIField .getValue(addr); }518private int getExceptionOffset() { return (int) exceptionOffsetField .getValue(addr); }519private int getStubOffset() { return (int) stubOffsetField .getValue(addr); }520private int getOopsOffset() { return (int) oopsOffsetField .getValue(addr); }521private int getMetadataOffset() { return (int) metadataOffsetField .getValue(addr); }522private int getScopesPCsOffset() { return (int) scopesPCsOffsetField .getValue(addr); }523private int getDependenciesOffset() { return (int) dependenciesOffsetField.getValue(addr); }524private int getHandlerTableOffset() { return (int) handlerTableOffsetField.getValue(addr); }525private int getNulChkTableOffset() { return (int) nulChkTableOffsetField .getValue(addr); }526private int getNMethodEndOffset() { return (int) nmethodEndOffsetField .getValue(addr); }527private int getCompLevel() { return (int) compLevelField .getValue(addr); }528}529530531