gpt4 book ai didi

java - 是否可以在调试端获取JDI当前Java中的StackFrame?

转载 作者:行者123 更新时间:2023-12-03 08:42:07 24 4
gpt4 key购买 nike

因此,JDI 允许我们在调试对象应用程序中设置断点,然后通过 JDWP 获取当前的 StackFrame。据我了解,JVMTI用于被调试端将请求的信息通过JDWP发送到JDI

是否可以从调试对象本身获取当前的 StackFrame(因此无需将其发送到调试器...调试对象将是其自己的调试器)?

例如考虑以下代码:

//client code
int a = 5;
StackFrame frame = ...

//list will contain variable "a"
List<LocalVariable> visibleVariables = frame.visibleVariables();

最佳答案

这是可能的,但有一些问题。

必须已在启动时启用 JVM 的调试。要连接您自己的 JVM,您需要使用应用程序知道的预定义端口或附加功能,这需要为最新的 JVM 显式启用自附加。

然后,由于您必须挂起要检查的线程,因此它不能是执行检查的同一个线程。所以你必须将任务委托(delegate)给不同的线程。

例如

public static void main(String[] args) throws Exception {
Object o = null;
int test = 42;
String s = "hello";
Map<String, Object> vars = variables();
System.out.println(vars);
}
// get the variables in the caller’s frame
static Map<String,Object> variables() throws Exception {
Thread th = Thread.currentThread();
String oldName = th.getName(), tmpName = UUID.randomUUID().toString();
th.setName(tmpName);
long depth = StackWalker.getInstance(
StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(Stream::count) - 1;

ExecutorService es = Executors.newSingleThreadExecutor();
try {
return es.<Map<String,Object>>submit(() -> {
VirtualMachineManager m = Bootstrap.virtualMachineManager();
for(var ac: m.attachingConnectors()) {
Map<String, Connector.Argument> arg = ac.defaultArguments();
Connector.Argument a = arg.get("pid");
if(a == null) continue;
a.setValue(String.valueOf(ProcessHandle.current().pid()));
VirtualMachine vm = ac.attach(arg);
return getVariableValues(vm, tmpName, depth);
}
return Map.of();
}).get();
} finally {
th.setName(oldName);
es.shutdown();
}
}

private static Map<String,Object> getVariableValues(
VirtualMachine vm, String tmpName, long depth)
throws IncompatibleThreadStateException, AbsentInformationException {

for(ThreadReference r: vm.allThreads()) {
if(!r.name().equals(tmpName)) continue;
r.suspend();
try {
StackFrame frame = r.frame((int)(r.frameCount() - depth));
return frame.getValues(frame.visibleVariables())
.entrySet().stream().collect(HashMap::new,
(m,e) -> m.put(e.getKey().name(), t(e.getValue())), Map::putAll);
} finally {
r.resume();
}
}
return Map.of();
}
private static Object t(Value v) {
if(v == null) return null;
switch(v.type().signature()) {
case "Z": return ((PrimitiveValue)v).booleanValue();
case "B": return ((PrimitiveValue)v).byteValue();
case "S": return ((PrimitiveValue)v).shortValue();
case "C": return ((PrimitiveValue)v).charValue();
case "I": return ((PrimitiveValue)v).intValue();
case "J": return ((PrimitiveValue)v).longValue();
case "F": return ((PrimitiveValue)v).floatValue();
case "D": return ((PrimitiveValue)v).doubleValue();
case "Ljava/lang/String;": return ((StringReference)v).value();
}
if(v instanceof ArrayReference)
return ((ArrayReference)v).getValues().stream().map(e -> t(e)).toArray();
return v.type().name()+'@'+Integer.toHexString(v.hashCode());
}

当我使用选项在装有 JDK 12 的计算机上运行此程序时
-Djdk.attach.allowAttachSelf -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,它打印

Listening for transport dt_socket at address: 50961
{args=[Ljava.lang.Object;@146ba0ac, s=hello, test=42, o=null}

关于java - 是否可以在调试端获取JDI当前Java中的StackFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62433660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com