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);
}
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");
}
}
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();
}
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.");
}
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);
}
}
Aggregations