Nff*_*ff3 6 java debugging breakpoints jdi jvmti
我有自己的 JDI 调试器,它toString在某些对象上调用该方法:
com.sun.jdi.ObjectReferenceobject object = ...
ThreadReference threadRef = frameProxy.threadProxy().getThreadReference();
Value value = object.invokeMethod(threadRef, toStringMethod,
Collections.EMPTY_LIST, ObjectReference.INVOKE_SINGLE_THREADED);
Run Code Online (Sandbox Code Playgroud)
问题是即使在 toString() 方法中没有设置断点, invokeMethod 也永远不会终止,所以我的调试器挂起。例如,当我在Double对象上调用 this 时会发生这种情况。
如何invokeMethod在一段时间后终止执行?
更新:我尝试实现我自己的Double对象并System.out.println()在方法的开头和结尾放置了一些语句,toString()看起来该方法执行得很好,但由于某种原因调试器没有收到结果。也许这是 JDI 中的一个错误,因为有很多这样的错误,但我不是在为此寻找解决方案,我只是在寻找一种方法来中止执行,invokeMethod()如果它花费太多时间。
更新 2:我尝试了ThierryB 的建议,但我只能frameProxy.threadProxy().stop(object);在经理线程中调用。并且管理器线程被阻塞,invokeMethod()因此它不会执行我的命令。我试过这样的事情:
boolean[] isFinished = new boolean[2];
isFinished[0] = false;
DebuggerManagerThreadImpl managerThread = debugProcess.getManagerThread();
new Thread(() - > {
try {
Thread.sleep(2000);
if (!isFinished[0]) {
System.out.println("Invoked");
managerThread.invokeCommand(new DebuggerCommand() {
@Override
public void action() {
try {
frameProxy.threadProxy().stop(object);
} catch (InvalidTypeException e) {
e.printStackTrace();
}
int threadStatus = frameProxy.threadProxy().status();
switch (threadStatus) {
case ThreadReference.THREAD_STATUS_RUNNING:
System.out.println("The thread is running.");
break;
case ThreadReference.THREAD_STATUS_ZOMBIE:
System.out.println("The thread has been completed.");
break;
case ThreadReference.THREAD_STATUS_WAIT:
System.out.println("The thread is waiting.");
break;
default:
System.out.println("The thread is not running / not waiting / not completed : but what is it doing right now ? (a little programmer joke ;) )");
}
}
@Override
public void commandCancelled() {
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
Value value = object.invokeMethod(threadRef, toStringMethod,
Collections.EMPTY_LIST, ObjectReference.INVOKE_SINGLE_THREADED);
isFinished[0] = true;
Run Code Online (Sandbox Code Playgroud)
但frameProxy.threadProxy().stop(object);永远不会执行,因为该DebuggerCommand's action方法未被调用(线程被阻塞)。
当我的调试器挂起并且我强行停止该过程时,这也是堆栈跟踪:
com.sun.jdi.VMDisconnectedException
at com.jetbrains.jdi.TargetVM.waitForReply(TargetVM.java:317)
at com.jetbrains.jdi.VirtualMachineImpl.waitForTargetReply(VirtualMachineImpl.java:1170)
at com.jetbrains.jdi.PacketStream.waitForReply(PacketStream.java:86)
at com.jetbrains.jdi.JDWP$ObjectReference$InvokeMethod.waitForReply(JDWP.java:4840)
at com.jetbrains.jdi.ObjectReferenceImpl.invokeMethod(ObjectReferenceImpl.java:413)
Run Code Online (Sandbox Code Playgroud)
更新 3:使用哪个线程来调用方法?目前我正在使用frameProxy.threadProxy().getThreadReference();它在大多数情况下都可以正常工作,但是最好创建一个单独的线程来调用对象上的方法(沿着我的 JDI 调试器,我在应用程序中还有一个检测代理,所以我可以创建一个单独的线程只是为了这个用例(也许这会防止死锁?))。
更新 4:目前我正在使用SUSPEND_ALL暂停策略,使用它会更好SUSPEND_EVENT_THREAD吗?
您可以将接口ThreadReference与方法一起使用void stop(ObjectReference throwable)。javadoc api 告诉我们“由于异步异常而停止此线程。”。
try {
com.sun.jdi.ObjectReferenceobject object = ...
ThreadReference threadRef = frameProxy.threadProxy().getThreadReference();
frameProxy.threadProxy().stop(object);
int threadStatus = frameProxy.threadProxy().status();
switch(threadStatus) {
case ThreadReference.THREAD_STATUS_RUNNING :
log.info("The thread is running.");
break;
case ThreadReference.THREAD_STATUS_ZOMBIE :
log.info("The thread has been completed.");
break;
case ThreadReference.THREAD_STATUS_WAIT :
log.info("The thread is waiting.");
break;
default :
log.info("The thread is not running / not waiting / not completed : but what is it doing right now ? (a little programmer joke ;) )");
}
} catch (ClassNotLoadedException cnle) {
// log exception, or display a message...
} catch (IncompatibleThreadStateException itse) {
// log exception, or display a message...
} catch (InvalidTypeException ite) {
// log exception, or display a message...
}
Run Code Online (Sandbox Code Playgroud)
当您使用该方法检查线程状态时status,您可以发现该值:
希望您能找到一种方法来使用我在这里提供的内容来完成您需要做的事情。此处的示例或Alvin Alexander 网站上发布的以下套件测试也可能有所帮助。
| 归档时间: |
|
| 查看次数: |
253 次 |
| 最近记录: |