How to setup a JDI launching connector?

前端 未结 2 1128
孤城傲影
孤城傲影 2021-01-06 08:41

So I\'m trying to work myself into JDI. I was already successful hooking my debugger application into my debugee program by first starting the debuggee with VM commands:

2条回答
  •  孤城傲影
    2021-01-06 09:06

    It's probably because you aren't handling the subprocess stdin/stdout streams. I've got a VMLauncher utility class in my JDI scripting project that handles this; the relevant code is:

    public VirtualMachine safeStart()
        throws IOException,
               IllegalConnectorArgumentsException,
               VMStartException
    {
        VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
        LaunchingConnector connector = vmm.defaultConnector();
        Map cArgs = connector.defaultArguments();
        cArgs.get("options").setValue(options);
        cArgs.get("main").setValue(main);
        final VirtualMachine vm = connector.launch(cArgs);
    
        final Thread outThread = redirect("Subproc stdout",
                                          vm.process().getInputStream(),
                                          out);
        final Thread errThread = redirect("Subproc stderr",
                                          vm.process().getErrorStream(),
                                          err);
        if(killOnShutdown) {
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    outThread.interrupt();
                    errThread.interrupt();
                    vm.process().destroy();
                }
            });
        }
    
        return vm;
    }
    
    private Thread redirect(String name, InputStream in, OutputStream out) {
        Thread t = new StreamRedirectThread(name, in, out);
        t.setDaemon(true);
        t.start();
        return t;
    }
    

提交回复
热议问题