Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/PointerFinder.java
41161 views
/*1* Copyright (c) 2000, 2021, 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.utilities;2526import sun.jvm.hotspot.code.*;27import sun.jvm.hotspot.debugger.*;28import sun.jvm.hotspot.debugger.cdbg.*;29import sun.jvm.hotspot.gc.shared.*;30import sun.jvm.hotspot.interpreter.*;31import sun.jvm.hotspot.memory.*;32import sun.jvm.hotspot.oops.Metadata;33import sun.jvm.hotspot.runtime.*;34import sun.jvm.hotspot.types.WrongTypeException;3536/** This class, only intended for use in the debugging system,37provides the functionality of find() in the VM. */3839public class PointerFinder {40public static PointerLocation find(Address a) {41PointerLocation loc = new PointerLocation(a);42Threads threads = VM.getVM().getThreads();4344// Check if address is a pointer to a Metadata object.45try {46loc.metadata = Metadata.instantiateWrapperFor(a);47return loc;48} catch (Exception e) {49// Just ignore. This just means we aren't dealing with a Metadata pointer.50}5152// Check if address is some other C++ type that we can deduce53loc.ctype = VM.getVM().getTypeDataBase().guessTypeForAddress(a);54if (loc.ctype == null && VM.getVM().isSharingEnabled()) {55// Check if the value falls in the _md_region56try {57Address loc1 = a.getAddressAt(0);58FileMapInfo cdsFileMapInfo = VM.getVM().getFileMapInfo();59if (cdsFileMapInfo.inCopiedVtableSpace(loc1)) {60loc.ctype = cdsFileMapInfo.getTypeForVptrAddress(loc1);61}62} catch (AddressException | WrongTypeException e) {63// This can happen if "a" or "loc1" is a bad address. Just ignore.64}65}66if (loc.ctype != null) {67return loc;68}6970// Check if address is in the stack of a JavaThread71for (int i = 0; i < threads.getNumberOfThreads(); i++) {72JavaThread t = threads.getJavaThreadAt(i);73Address stackBase = t.getStackBase();74if (stackBase != null) {75Long stackSize = t.getStackSize();76Address stackEnd = stackBase.addOffsetTo(-stackSize);77if (a.lessThanOrEqual(stackBase) && a.greaterThan(stackEnd)) {78loc.stackThread = t;79return loc;80}81}82}8384// Check if address is in the java heap.85CollectedHeap heap = VM.getVM().getUniverse().heap();86if (heap instanceof GenCollectedHeap) {87GenCollectedHeap genheap = (GenCollectedHeap) heap;88if (genheap.isIn(a)) {89for (int i = 0; i < genheap.nGens(); i++) {90Generation g = genheap.getGen(i);91if (g.isIn(a)) {92loc.gen = g;93break;94}95}9697if (Assert.ASSERTS_ENABLED) {98Assert.that(loc.gen != null, "Should have found this in a generation");99}100101if (VM.getVM().getUseTLAB()) {102// Try to find thread containing it103for (int i = 0; i < threads.getNumberOfThreads(); i++) {104JavaThread t = threads.getJavaThreadAt(i);105ThreadLocalAllocBuffer tlab = t.tlab();106if (tlab.contains(a)) {107loc.inTLAB = true;108loc.tlabThread = t;109loc.tlab = tlab;110break;111}112}113}114115return loc;116}117} else {118if (heap.isIn(a)) {119loc.heap = heap;120return loc;121}122}123124// Check if address is in the interpreter125Interpreter interp = VM.getVM().getInterpreter();126if (interp.contains(a)) {127loc.inInterpreter = true;128loc.interpreterCodelet = interp.getCodeletContaining(a);129return loc;130}131132// Check if address is in the code cache133if (!VM.getVM().isCore()) {134CodeCache c = VM.getVM().getCodeCache();135if (c.contains(a)) {136loc.inCodeCache = true;137loc.blob = c.findBlobUnsafe(a);138if (Assert.ASSERTS_ENABLED) {139Assert.that(loc.blob != null, "Should have found CodeBlob");140}141loc.inBlobCode = loc.blob.codeContains(a);142loc.inBlobData = loc.blob.dataContains(a);143144if (loc.blob.isNMethod()) {145NMethod nm = (NMethod) loc.blob;146loc.inBlobOops = nm.oopsContains(a);147}148149loc.inBlobUnknownLocation = (!(loc.inBlobCode ||150loc.inBlobData ||151loc.inBlobOops));152return loc;153}154}155156// Check JNIHandles; both local and global157JNIHandles handles = VM.getVM().getJNIHandles();158159// --- looking in oopstorage should model OopStorage::allocation_status?160// --- that is, if in a block but not allocated, then not valid.161162// Look in global handles163OopStorage storage = handles.globalHandles();164if ((storage != null) && storage.findOop(a)) {165loc.inStrongGlobalJNIHandles = true;166return loc;167}168// Look in weak global handles169storage = handles.weakGlobalHandles();170if ((storage != null) && storage.findOop(a)) {171loc.inWeakGlobalJNIHandles = true;172return loc;173}174// Look in thread-local handles175for (int i = 0; i < threads.getNumberOfThreads(); i++) {176JavaThread t = threads.getJavaThreadAt(i);177JNIHandleBlock handleBlock = t.activeHandles();178if (handleBlock != null) {179handleBlock = handleBlock.blockContainingHandle(a);180if (handleBlock != null) {181loc.inLocalJNIHandleBlock = true;182loc.handleBlock = handleBlock;183loc.handleThread = t;184return loc;185}186}187}188189// Check if address is a native (C++) symbol. Do this last because we don't always190// do a good job of computing if an address is actually within a native lib, and sometimes191// an address outside of a lib will be found as inside.192JVMDebugger dbg = VM.getVM().getDebugger();193CDebugger cdbg = dbg.getCDebugger();194if (cdbg != null) {195loc.loadObject = cdbg.loadObjectContainingPC(a);196if (loc.loadObject != null) {197loc.nativeSymbol = loc.loadObject.closestSymbolToPC(a);198return loc;199}200}201202// Fall through; have to return it anyway.203return loc;204}205}206207208