How to setup a JDI launching connector?

2019-02-14 08:11发布

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:

-agentlib:jdwp=transport=dt_socket,server=y,address=8000

and then launching my debugger which establishes a connection by the use of an attaching connector:

VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
AttachingConnector ac = vmm.attachingConnectors().get(0);
Map<String, Connector.Argument> env = ac.defaultArguments();
env.get("port").setValue("8000");
env.get("hostname").setValue("localhost");
VirtualMachine vm = ac.attach(env);

But now I want my debugger application to start the debuggee program itself. I understand one must use the launching connector in that case. So I tried this:

VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
LaunchingConnector lc = vmm.launchingConnectors().get(0);
Map<String, Connector.Argument> env = lc.defaultArguments();
env.get("main").setValue("p.DebugDummy");
env.get("suspend").setValue("true");
env.get("home").setValue("C:/Program Files/Java/jdk1.7.0_51");
VirtualMachine vm = lc.launch(env);

However, when I start this application, my debugee program does not get launched. I get no exceptions or anything, despite of having a bunch of follow-up code to the code showed above; stuff like:

    // A single implementor of this interface exists in a particuar VM
    EventRequestManager mgr = vm.eventRequestManager();

    // suspend VM
    vm.suspend();

    // lookup main thread
    ThreadReference mainThread = null;
    List<ThreadReference> threads = vm.allThreads();
    for (ThreadReference thread : threads) {
        if ("main".equals(thread.name())) {
            mainThread = thread;
            break;
        }
    }

    // resume 
    vm.resume();
    mainThread.resume();

    // There is one instance of EventQueue assigned to a particular
    // VirtualMachine.
    EventQueue eventQueue = vm.eventQueue();

    // Waits for start event.
    WAIT_FOR_START: do {
        EventSet eventSet = eventQueue.remove();
        EventIterator eventIterator = eventSet.eventIterator();
        while (eventIterator.hasNext()) {
            Event event = eventIterator.next();
            if (event instanceof VMStartEvent) {
                System.out.println("VMStartEvent.");
                break WAIT_FOR_START;
            }
        }
    } while (true);

    System.out.println("GO...");

It all runs just fine?! I do get no exceptions and all the sysouts (GO... etc). I find that quite strange -- obviously it finds a main thread and a VMStartEvent. But I guess my debugger is doing this all to itself or something? I'm afraid I didn't really grasp what all these method calls do.

So my question: Why does my debuggee program not launch?

As you can see above I did setup the "main" argument to:

env.get("main").setValue("p.DebugDummy");

My debugger application is in that same package (p). So that should be correct? But obviously I'm doing something wrong here. Any ideas?

Thank you!

2条回答
Explosion°爆炸
2楼-- · 2019-02-14 08:38

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<String, Argument> 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;
}
查看更多
爷、活的狠高调
3楼-- · 2019-02-14 08:47

Did you set your classpath in the LaunchingConnector?

env.get("options").setValue("-cp " +
  "/my-project/target/classes:" +
  "/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/tools.jar:" +
  "");
查看更多
登录 后发表回答