Java Code Examples for com.sun.jdi.connect.AttachingConnector#attach()
The following examples show how to use
com.sun.jdi.connect.AttachingConnector#attach() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HotSwapper.java From HotswapAgent with GNU General Public License v2.0 | 6 votes |
/** * Connects to the JVM. * * @param port the port number used for the connection to the JVM. */ public HotSwapper(String port) throws IOException, IllegalConnectorArgumentsException { jvm = null; request = null; newClassFiles = null; trigger = new Trigger(); AttachingConnector connector = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); Map<String,Connector.Argument> arguments = connector.defaultArguments(); arguments.get("hostname").setValue(HOST_NAME); arguments.get("port").setValue(port); jvm = connector.attach(arguments); EventRequestManager manager = jvm.eventRequestManager(); request = methodEntryRequests(manager, TRIGGER_NAME); }
Example 2
Source File: ProcessAttachTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private static void tryDebug(long pid) throws IOException, IllegalConnectorArgumentsException { AttachingConnector ac = Bootstrap.virtualMachineManager().attachingConnectors() .stream() .filter(c -> c.name().equals("com.sun.jdi.ProcessAttach")) .findFirst() .orElseThrow(() -> 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); // list all threads System.out.println("Attached! Now listing threads ..."); vm.allThreads().stream().forEach(System.out::println); System.out.println("Debugger done."); vm.dispose(); }
Example 3
Source File: DebugUtility.java From java-debug with Eclipse Public License 1.0 | 6 votes |
/** * Attach to an existing debuggee VM. * @param vmManager * the virtual machine manager * @param hostName * the machine where the debuggee VM is launched on * @param port * the debug port that the debuggee VM exposed * @param attachTimeout * the timeout when attaching to the debuggee VM * @return an instance of IDebugSession * @throws IOException * when unable to attach. * @throws IllegalConnectorArgumentsException * when one of the connector arguments is invalid. */ public static IDebugSession attach(VirtualMachineManager vmManager, String hostName, int port, int attachTimeout) throws IOException, IllegalConnectorArgumentsException { List<AttachingConnector> connectors = vmManager.attachingConnectors(); AttachingConnector connector = connectors.get(0); // in JDK 10, the first AttachingConnector is not the one we want final String SUN_ATTACH_CONNECTOR = "com.sun.tools.jdi.SocketAttachingConnector"; for (AttachingConnector con : connectors) { if (con.getClass().getName().equals(SUN_ATTACH_CONNECTOR)) { connector = con; break; } } Map<String, Argument> arguments = connector.defaultArguments(); arguments.get(HOSTNAME).setValue(hostName); arguments.get(PORT).setValue(String.valueOf(port)); arguments.get(TIMEOUT).setValue(String.valueOf(attachTimeout)); return new DebugSession(connector.attach(arguments)); }
Example 4
Source File: HotSwapperJpda.java From HotswapAgent with GNU General Public License v2.0 | 6 votes |
/** * Connects to the JVM. * * @param port the port number used for the connection to the JVM. */ public HotSwapperJpda(String port) throws IOException, IllegalConnectorArgumentsException { jvm = null; request = null; newClassFiles = null; trigger = new Trigger(); AttachingConnector connector = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); Map<String,Connector.Argument> arguments = connector.defaultArguments(); arguments.get("hostname").setValue(HOST_NAME); arguments.get("port").setValue(port); jvm = connector.attach(arguments); EventRequestManager manager = jvm.eventRequestManager(); request = methodEntryRequests(manager, TRIGGER_NAME); }
Example 5
Source File: HelloWorld.java From java-svc with BSD 3-Clause "New" or "Revised" License | 5 votes |
private static VirtualMachine connect(String host, String port) throws IOException, IllegalConnectorArgumentsException { AttachingConnector socketConnector = getSocketConnector(); Map<String, Argument> arguments = socketConnector.defaultArguments(); arguments.get("hostname").setValue(host); ((IntegerArgument) arguments.get("port")).setValue(Integer.parseInt(port)); return socketConnector.attach(arguments); }
Example 6
Source File: ProcessAttachDebugger.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 ..."); // list all threads for (ThreadReference thr: vm.allThreads()) { System.out.println(thr); } System.out.println("Debugger done."); }
Example 7
Source File: ProcessAttachDebugger.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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 ..."); // list all threads for (ThreadReference thr: vm.allThreads()) { System.out.println(thr); } System.out.println("Debugger done."); }
Example 8
Source File: ProcessAttachDebugger.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
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 ..."); // list all threads for (ThreadReference thr: vm.allThreads()) { System.out.println(thr); } System.out.println("Debugger done."); }
Example 9
Source File: ProcessAttachDebugger.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
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 ..."); // list all threads for (ThreadReference thr: vm.allThreads()) { System.out.println(thr); } System.out.println("Debugger done."); }
Example 10
Source File: ProcessAttachDebugger.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
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 ..."); // list all threads for (ThreadReference thr: vm.allThreads()) { System.out.println(thr); } System.out.println("Debugger done."); }
Example 11
Source File: ProcessAttachDebugger.java From hottub with GNU General Public License v2.0 | 5 votes |
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 ..."); // list all threads for (ThreadReference thr: vm.allThreads()) { System.out.println(thr); } System.out.println("Debugger done."); }
Example 12
Source File: VMAcquirer.java From nopol with GNU General Public License v2.0 | 5 votes |
private VirtualMachine connect(AttachingConnector connector, String port) throws IllegalConnectorArgumentsException, IOException { Map<String, Connector.Argument> args = connector.defaultArguments(); Connector.Argument pidArgument = args.get("port"); args.get("hostname").setValue("localhost"); if (pidArgument == null) { throw new IllegalStateException(); } pidArgument.setValue(port); return connector.attach(args); }
Example 13
Source File: ProcessAttachDebugger.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
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 ..."); // list all threads for (ThreadReference thr: vm.allThreads()) { System.out.println(thr); } System.out.println("Debugger done."); }
Example 14
Source File: ExclusiveBind.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 15
Source File: ExclusiveBind.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 16
Source File: RunToExit.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
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 server debuggee Process process = launch(address, "Exit0"); // wait for the debugge to be ready while (!ready) { try { Thread.sleep(1000); } catch(Exception ee) { throw ee; } } // 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(); int exitCode = process.waitFor(); // if the server debuggee ran cleanly, we assume we were clean if (exitCode == 0 && error_seen == 0) { System.out.println("Test passed - server debuggee cleanly terminated"); } else { throw new RuntimeException("Test failed - server debuggee generated an error when it terminated"); } }
Example 17
Source File: RunToExit.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
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 server debuggee Process process = launch(address, "Exit0"); // 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); System.out.println("Connection arguments: " + conn_args); VirtualMachine vm = null; while (vm == null) { try { vm = conn.attach(conn_args); } catch (ConnectException e) { e.printStackTrace(System.out); System.out.println("--- Debugee not ready. Retrying in 500ms. ---"); Thread.sleep(500); } } // 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(); int exitCode = process.waitFor(); // if the server debuggee ran cleanly, we assume we were clean if (exitCode == 0 && error_seen == 0) { System.out.println("Test passed - server debuggee cleanly terminated"); } else { throw new RuntimeException("Test failed - server debuggee generated an error when it terminated, " + "exit code was " + exitCode + ", " + error_seen + " error(s) seen in debugee output."); } }
Example 18
Source File: RunToExit.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
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 server debuggee Process process = launch(address, "Exit0"); // wait for the debugge to be ready while (!ready) { try { Thread.sleep(1000); } catch(Exception ee) { throw ee; } } // 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(); int exitCode = process.waitFor(); // if the server debuggee ran cleanly, we assume we were clean if (exitCode == 0 && error_seen == 0) { System.out.println("Test passed - server debuggee cleanly terminated"); } else { throw new RuntimeException("Test failed - server debuggee generated an error when it terminated"); } }
Example 19
Source File: ExclusiveBind.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
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), Utils.adjustTimeout(5000), 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"); } }
Example 20
Source File: JdiUtils.java From java-svc with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Attaches to a dt_socket enabled JVM. * * @param host * the host to connect to. * @param port * the port to connect to. * @return the {@link VirtualMachine} connected to. * @throws IOException * @throws IllegalConnectorArgumentsException */ public static VirtualMachine connect(String host, String port) throws IOException, IllegalConnectorArgumentsException { AttachingConnector socketConnector = getSocketConnector(); Map<String, Argument> arguments = socketConnector.defaultArguments(); arguments.get("hostname").setValue(host); arguments.get("port").setValue(port); return socketConnector.attach(arguments); }