Search in sources :

Example 1 with AttachingConnector

use of com.sun.jdi.connect.AttachingConnector in project che by eclipse.

the class JavaDebugger method connect.

/**
     * Attach to a JVM that is already running at specified host.
     *
     * @throws DebuggerException
     *         when connection to Java VM is not established
     */
private void connect() throws DebuggerException {
    final String connectorName = "com.sun.jdi.SocketAttach";
    AttachingConnector connector = connector(connectorName);
    if (connector == null) {
        throw new DebuggerException(String.format("Unable connect to target Java VM. Requested connector '%s' not found. ", connectorName));
    }
    Map<String, Connector.Argument> arguments = connector.defaultArguments();
    arguments.get("hostname").setValue(host);
    ((Connector.IntegerArgument) arguments.get("port")).setValue(port);
    int attempt = 0;
    for (; ; ) {
        try {
            Thread.sleep(2000);
            vm = connector.attach(arguments);
            vm.suspend();
            break;
        } catch (UnknownHostException | IllegalConnectorArgumentsException e) {
            throw new DebuggerException(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            if (++attempt > 10) {
                throw new DebuggerException(e.getMessage(), e);
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ignored) {
            }
        } catch (InterruptedException ignored) {
        }
    }
    eventsCollector = new EventsCollector(vm.eventQueue(), this);
    LOG.debug("Connect {}:{}", host, port);
}
Also used : IllegalConnectorArgumentsException(com.sun.jdi.connect.IllegalConnectorArgumentsException) UnknownHostException(java.net.UnknownHostException) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) AttachingConnector(com.sun.jdi.connect.AttachingConnector) IOException(java.io.IOException) Breakpoint(org.eclipse.che.api.debug.shared.model.Breakpoint)

Example 2 with AttachingConnector

use of com.sun.jdi.connect.AttachingConnector in project jdk8u_jdk by JetBrains.

the class ExclusiveBind method main.

/*
     * - pick a TCP port
     * - Launch a debuggee in server=y,suspend=y,address=${port}
     * - Launch a second debuggee in server=y,suspend=n with the same port
     * - Second debuggee should fail with an error (address already in use)
     * - For clean-up we attach to the first debuggee and resume it.
     */
public static void main(String[] args) throws Exception {
    // find a free port
    ServerSocket ss = new ServerSocket(0);
    int port = ss.getLocalPort();
    ss.close();
    String address = String.valueOf(port);
    // launch the first debuggee
    ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
    // start the debuggee and wait for the "ready" message
    Process p = ProcessTools.startProcess("process1", process1, line -> line.equals("Listening for transport dt_socket at address: " + address), Math.round(5000 * Utils.TIMEOUT_FACTOR), TimeUnit.MILLISECONDS);
    // launch a second debuggee with the same address
    ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");
    // get exit status from second debuggee
    int exitCode = ProcessTools.startProcess("process2", process2).waitFor();
    // clean-up - attach to first debuggee and resume it
    AttachingConnector conn = (AttachingConnector) findConnector("com.sun.jdi.SocketAttach");
    Map conn_args = conn.defaultArguments();
    Connector.IntegerArgument port_arg = (Connector.IntegerArgument) conn_args.get("port");
    port_arg.setValue(port);
    VirtualMachine vm = conn.attach(conn_args);
    vm.resume();
    // if the second debuggee ran to completion then we've got a problem
    if (exitCode == 0) {
        throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
    } else {
        System.out.println("Test passed - second debuggee correctly failed to bind");
    }
}
Also used : AttachingConnector(com.sun.jdi.connect.AttachingConnector) Connector(com.sun.jdi.connect.Connector) AttachingConnector(com.sun.jdi.connect.AttachingConnector) ServerSocket(java.net.ServerSocket) Map(java.util.Map) VirtualMachine(com.sun.jdi.VirtualMachine)

Example 3 with AttachingConnector

use of com.sun.jdi.connect.AttachingConnector in project jdk8u_jdk by JetBrains.

the class BadHandshakeTest method main.

/*
     * - pick a TCP port
     * - Launch a server debuggee: server=y,suspend=y,address=${port}
     * - run it to VM death
     * - verify we saw no error
     */
public static void main(String[] args) throws Exception {
    int port = Utils.getFreePort();
    String address = String.valueOf(port);
    // launch the server debuggee
    Process process = launch(address, "Exit0");
    if (process == null) {
        throw new RuntimeException("Unable to start debugee");
    }
    // Connect to the debuggee and handshake with garbage
    Socket s = new Socket(InetAddress.getLocalHost(), port);
    s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8"));
    s.close();
    // Re-connect and to a partial handshake - don't disconnect
    s = new Socket(InetAddress.getLocalHost(), port);
    s.getOutputStream().write("JDWP-".getBytes("UTF-8"));
    // attach to server debuggee and resume it so it can exit
    AttachingConnector conn = (AttachingConnector) findConnector("com.sun.jdi.SocketAttach");
    Map conn_args = conn.defaultArguments();
    Connector.IntegerArgument port_arg = (Connector.IntegerArgument) conn_args.get("port");
    port_arg.setValue(port);
    VirtualMachine vm = conn.attach(conn_args);
    // The first event is always a VMStartEvent, and it is always in
    // an EventSet by itself.  Wait for it.
    EventSet evtSet = vm.eventQueue().remove();
    for (Event event : evtSet) {
        if (event instanceof VMStartEvent) {
            break;
        }
        throw new RuntimeException("Test failed - debuggee did not start properly");
    }
    vm.eventRequestManager().deleteAllBreakpoints();
    vm.resume();
    process.waitFor();
}
Also used : AttachingConnector(com.sun.jdi.connect.AttachingConnector) Connector(com.sun.jdi.connect.Connector) AttachingConnector(com.sun.jdi.connect.AttachingConnector) Map(java.util.Map) Socket(java.net.Socket) VirtualMachine(com.sun.jdi.VirtualMachine)

Example 4 with AttachingConnector

use of com.sun.jdi.connect.AttachingConnector in project jdk8u_jdk by JetBrains.

the class ProcessAttachDebugger method main.

public static void main(String[] main_args) throws Exception {
    String pid = main_args[0];
    // find ProcessAttachingConnector
    List<AttachingConnector> l = Bootstrap.virtualMachineManager().attachingConnectors();
    AttachingConnector ac = null;
    for (AttachingConnector c : l) {
        if (c.name().equals("com.sun.jdi.ProcessAttach")) {
            ac = c;
            break;
        }
    }
    if (ac == null) {
        throw new RuntimeException("Unable to locate ProcessAttachingConnector");
    }
    Map<String, Connector.Argument> args = ac.defaultArguments();
    Connector.StringArgument arg = (Connector.StringArgument) args.get("pid");
    arg.setValue(pid);
    System.out.println("Debugger is attaching to: " + pid + " ...");
    VirtualMachine vm = ac.attach(args);
    System.out.println("Attached! Now listing threads ...");
    for (ThreadReference thr : vm.allThreads()) {
        System.out.println(thr);
    }
    System.out.println("Debugger done.");
}
Also used : AttachingConnector(com.sun.jdi.connect.AttachingConnector) Connector(com.sun.jdi.connect.Connector) AttachingConnector(com.sun.jdi.connect.AttachingConnector) ThreadReference(com.sun.jdi.ThreadReference) VirtualMachine(com.sun.jdi.VirtualMachine)

Example 5 with AttachingConnector

use of com.sun.jdi.connect.AttachingConnector in project HotswapAgent by HotswapProjects.

the class JDIRedefiner method connect.

private VirtualMachine connect(int port) throws IOException {
    VirtualMachineManager manager = Bootstrap.virtualMachineManager();
    // Find appropiate connector
    List<AttachingConnector> connectors = manager.attachingConnectors();
    AttachingConnector chosenConnector = null;
    for (AttachingConnector c : connectors) {
        if (c.transport().name().equals(TRANSPORT_NAME)) {
            chosenConnector = c;
            break;
        }
    }
    if (chosenConnector == null) {
        throw new IllegalStateException("Could not find socket connector");
    }
    // Set port argument
    AttachingConnector connector = chosenConnector;
    Map<String, Argument> defaults = connector.defaultArguments();
    Argument arg = defaults.get(PORT_ARGUMENT_NAME);
    if (arg == null) {
        throw new IllegalStateException("Could not find port argument");
    }
    arg.setValue(Integer.toString(port));
    // Attach
    try {
        System.out.println("Connector arguments: " + defaults);
        return connector.attach(defaults);
    } catch (IllegalConnectorArgumentsException e) {
        throw new IllegalArgumentException("Illegal connector arguments", e);
    }
}
Also used : IllegalConnectorArgumentsException(com.sun.jdi.connect.IllegalConnectorArgumentsException) Argument(com.sun.jdi.connect.Connector.Argument) AttachingConnector(com.sun.jdi.connect.AttachingConnector) VirtualMachineManager(com.sun.jdi.VirtualMachineManager)

Aggregations

AttachingConnector (com.sun.jdi.connect.AttachingConnector)8 VirtualMachine (com.sun.jdi.VirtualMachine)5 Connector (com.sun.jdi.connect.Connector)5 Map (java.util.Map)4 IllegalConnectorArgumentsException (com.sun.jdi.connect.IllegalConnectorArgumentsException)3 VirtualMachineManager (com.sun.jdi.VirtualMachineManager)2 IOException (java.io.IOException)2 ServerSocket (java.net.ServerSocket)2 Method (com.sun.jdi.Method)1 ReferenceType (com.sun.jdi.ReferenceType)1 ThreadReference (com.sun.jdi.ThreadReference)1 Argument (com.sun.jdi.connect.Connector.Argument)1 LaunchingConnector (com.sun.jdi.connect.LaunchingConnector)1 BreakpointRequest (com.sun.jdi.request.BreakpointRequest)1 EventRequestManager (com.sun.jdi.request.EventRequestManager)1 MethodEntryRequest (com.sun.jdi.request.MethodEntryRequest)1 SocketAttachingConnector (com.sun.tools.jdi.SocketAttachingConnector)1 Socket (java.net.Socket)1 UnknownHostException (java.net.UnknownHostException)1 Breakpoint (org.eclipse.che.api.debug.shared.model.Breakpoint)1