问题描述
我有自己的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);
问题在于,即使在toString()方法中未设置任何断点,invokeMethod也永远不会终止,因此调试器会挂起。例如,当我在Double
对象上调用它时,就会发生这种情况。
一段时间后如何取消invokeMethod
的执行?
更新:我尝试实现自己的Double
对象,并在System.out.println()
方法的开始和结束处放置了一些toString()
语句,似乎方法执行得很好,但是由于某种原因,调试器未收到结果。也许这是JDI中的错误,因为有很多这样的错误,但是我不是寻求解决方案,我只是在寻找一种方法来中止invokeMethod()
的执行,如果需要太多时间。
Update2:我尝试了 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,ObjectReference.INVOKE_SINGLE_THREADED);
isFinished[0] = true;
但是从不执行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)
更新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...
}
使用status
方法检查线程状态时,可以找到以下值:
- THREAD_STATUS_UNKNOWN
- THREAD_STATUS_ZOMBIE
- THREAD_STATUS_RUNNING
- THREAD_STATUS_SLEEPING
- THREAD_STATUS_MONITOR
- THREAD_STATUS_WAIT
- THREAD_STATUS_NOT_STARTED
希望您会找到一种方法来处理我在此提供的内容。 来自here或the following suite test published on the website of Alvin Alexander的示例也可能有帮助。