2014-10-28 238 views
0

我正在爲使用JDI的Java應用程序編寫調試器。JDI VMDisconnectedException

我運行使用調試進程:

'/usr/lib/jvm/jdk-8-oracle-x64/bin/java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y -cp (...) program.entrypoint.Test' 

這是一個非常簡單的程序。它所做的就是實例化一個Test對象,並在for循環中將foo字段設置爲0..10。

現在,調試程序我下了這個網站: link

public class FieldMonitor { 

    public static void main(String[] args) throws IOException, InterruptedException { 
     // connect 
     VirtualMachine vm = new VMProvider().connect(8000); 

     vm.resume(); 

     EventQueue eventQueue = vm.eventQueue(); 
     while (true) { 
      EventSet eventSet = eventQueue.remove(); 
      for (Event event : eventSet) { 
       try { 
        System.out.println(event); 

        if (event instanceof VMDeathEvent || event instanceof VMDisconnectEvent) { 
         return; 
        } 
        else if(...) 
       } 
       catch(VMDisconnectedException e) { 
        e.printStackTrace(); 
       } 
      } 
      eventSet.resume(); 
     } 
    } 
} 

通常情況下,我聽更多的活動,如打斷點等,但即使是正常的程序執行應該打印至少我:

VMStartEvent in thread main 
VMDeathEvent 

但是,當我嘗試打印該事件時,我將隨機得到一個異常,VMDisconnectedException。然後,我將獲得:

com.sun.jdi.VMDisconnectedException 
    at com.sun.tools.jdi.TargetVM.waitForReply(TargetVM.java:304) 
    at com.sun.tools.jdi.VirtualMachineImpl.waitForTargetReply(VirtualMachineImpl.java:1036) 
    at com.sun.tools.jdi.PacketStream.waitForReply(PacketStream.java:69) 
    at com.sun.tools.jdi.JDWP$ThreadReference$Name.waitForReply(JDWP.java:4931) 
    at com.sun.tools.jdi.JDWP$ThreadReference$Name.process(JDWP.java:4912) 
    at com.sun.tools.jdi.ThreadReferenceImpl.name(ThreadReferenceImpl.java:168) 
    at com.sun.tools.jdi.EventSetImpl$ThreadedEventImpl.toString(EventSetImpl.java:184) 
    at java.lang.String.valueOf(String.java:2981) 
    at java.io.PrintStream.println(PrintStream.java:821) 
    at virtualmachine.FieldMonitor.main(FieldMonitor.java:33) 
VMDeathEvent 

我VMStartEvent(或者,如果我聽多了事件的任何其他)現在安置一個例外。當我把一個斷點追趕子句中,我得到:

com.sun.jdi.VMDisconnectedException: connection is closed 

這個異常後,我還是從集合獲得VMDeathEvent。 現在,我不知道爲什麼會隨機發生,爲什麼它會發生,因爲我的debugee進程是暫停啓動的,我只是在將自己作爲調試器附加後才恢復。

回答

0

首先,需要更多關於您正在處理事件的信息。這裏是我的同類節目的輸出:

VMStartEvent in thread main 
VMDeathEvent 
VMDisconnectEvent 

不過,我沒有聲明:

vm.resume(); 

萬一我有它,我得到相同的異常。

Exception in thread "main" com.sun.jdi.VMDisconnectedException: connection is closed 
    at com.sun.tools.jdi.TargetVM.send(TargetVM.java:274) 
    at com.sun.tools.jdi.VirtualMachineImpl.sendToTarget(VirtualMachineImpl.java:1011) 
    at com.sun.tools.jdi.PacketStream.send(PacketStream.java:41) 
    at com.sun.tools.jdi.JDWP$ThreadReference$Name.enqueueCommand(JDWP.java:4928) 
    at com.sun.tools.jdi.JDWP$ThreadReference$Name.process(JDWP.java:4914) 
    at com.sun.tools.jdi.ThreadReferenceImpl.name(ThreadReferenceImpl.java:77) 
    at com.sun.tools.jdi.EventSetImpl$ThreadedEventImpl.toString(EventSetImpl.java:168) 
    at java.lang.String.valueOf(String.java:2826) 
    at java.io.PrintStream.println(PrintStream.java:771) 

所以,我想它是與引起問題的vm.resume()函數調用有關。 希望,這有幫助。

相關問題