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); } }
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(); }
public PermissionDebugger attach() throws IOException, IllegalConnectorArgumentsException { VirtualMachineManager vmm = Bootstrap.virtualMachineManager(); List<AttachingConnector> connectors = vmm.attachingConnectors(); AttachingConnector connector = connectors .stream() .filter(c -> c.transport().name().equals(transport)) .findFirst() .orElseThrow( () -> new IOException(String.format("Failed to find transport %s", transport))); Map<String, Argument> map = connector.defaultArguments(); Argument portArg = map.get(PORT_KEY); portArg.setValue(port); map.put(PORT_KEY, portArg); vm = connector.attach(map); return this; }
/** * 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 arguments = connector.defaultArguments(); ((Connector.Argument) arguments.get("hostname")).setValue(HOST_NAME); ((Connector.Argument) arguments.get("port")).setValue(port); jvm = connector.attach(arguments); EventRequestManager manager = jvm.eventRequestManager(); request = methodEntryRequests(manager, TRIGGER_NAME); }
private AttachingDICookie ( AttachingConnector attachingConnector, Map<String,? extends Argument> args ) { this.attachingConnector = attachingConnector; this.args = args; }
/** * Creates a new instance of AttachingDICookie for given parameters. * * @param attachingConnector a connector to be used * @param args map of arguments * @return a new instance of AttachingDICookie for given parameters */ public static AttachingDICookie create ( AttachingConnector attachingConnector, Map<String,? extends Argument> args ) { return new AttachingDICookie ( attachingConnector, args ); }
private static Map<String,? extends Argument> getArgs ( AttachingConnector attachingConnector, String hostName, int portNumber ) { Map<String,? extends Argument> args = attachingConnector.defaultArguments (); args.get ("hostname").setValue (hostName); args.get ("port").setValue ("" + portNumber); return args; }
private static Map<String,? extends Argument> getArgs ( AttachingConnector attachingConnector, String name ) { Map<String,? extends Argument> args = attachingConnector.defaultArguments (); args.get ("name").setValue (name); return args; }
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."); }
public static VirtualMachine attachDebugger(String hostname, int port) { VirtualMachineManager vmm = com.sun.jdi.Bootstrap.virtualMachineManager(); AttachingConnector atconn = null; for(AttachingConnector c: vmm.attachingConnectors()) if("dt_socket".equalsIgnoreCase(c.transport().name())) { atconn = c; } Map<String, Connector.Argument> prm = atconn.defaultArguments(); prm.get("hostname").setValue(hostname); prm.get("port").setValue(Integer.toString(port)); VirtualMachine vm2 = null; try { vm2 = atconn.attach(prm); ///for(ThreadReference t: vm2.allThreads()) { // log.info(t.name()); // if (t.name().equals("main")) // t.suspend(); //} vm2.resume(); } catch(Exception e) { e.printStackTrace(); //throw new RuntimeException(e); } return vm2; }
/** * Call this with the localhost port to connect to. */ public VirtualMachine connect(int port) throws IOException { String strPort = Integer.toString(port); AttachingConnector connector = getConnector(); try { return connect(connector, strPort); } catch (IllegalConnectorArgumentsException e) { throw new IllegalStateException(e); } }
private AttachingConnector getConnector() { VirtualMachineManager vmManager = Bootstrap.virtualMachineManager(); for (Connector connector : vmManager.attachingConnectors()) { if ("com.sun.jdi.SocketAttach".equals(connector.name())) { return (AttachingConnector) connector; } } throw new IllegalStateException(); }
private VirtualMachine connect(AttachingConnector connector, String port) throws IllegalConnectorArgumentsException, IOException { Map<String, Connector.Argument> args = connector.defaultArguments(); Connector.Argument pidArgument = args.get("port"); if (pidArgument == null) { throw new IllegalStateException(); } pidArgument.setValue(port); return connector.attach(args); }
private AttachingConnector connector(String connectorName) { for (AttachingConnector c : Bootstrap.virtualMachineManager().attachingConnectors()) { if (connectorName.equals(c.name())) { return c; } } return null; }
private static AttachingConnector makePlatformConnector() { Class connectorClass = null; try { connectorClass = Class.forName(SHAREDMEM_ATTACHING_CONN); } catch (ClassNotFoundException cnfe) { } if (connectorClass == null) { throw new RuntimeException("can not load class: " + SHAREDMEM_ATTACHING_CONN); } try { return (AttachingConnector) connectorClass.newInstance(); } catch (Exception exp) { throw new RuntimeException(exp); } }
private static AttachingConnector makePlatformConnector() { Class connectorClass = null; try { connectorClass = Class.forName(SOCKET_ATTACHING_CONN); } catch (ClassNotFoundException cnfe) {} if (connectorClass == null) { throw new RuntimeException("can not load class: " + SOCKET_ATTACHING_CONN); } try { return (AttachingConnector) connectorClass.newInstance(); } catch (Exception exp) { throw new RuntimeException(exp); } }
private static AttachingConnector makePlatformConnector() { Class connectorClass = null; try { connectorClass = Class.forName(PROCESS_ATTACHING_CONN); } catch (ClassNotFoundException cnfe) { } if (connectorClass == null) { throw new RuntimeException("can not load class: " + PROCESS_ATTACHING_CONN); } try { return (AttachingConnector) connectorClass.newInstance(); } catch (Exception exp) { throw new RuntimeException(exp); } }
@Test public void testF3Connectors() { LaunchingConnector conn = F3Bootstrap.virtualMachineManager().defaultConnector(); Assert.assertEquals("org.f3.jdi.connect.F3LaunchingConnector", conn.name()); F3LaunchingConnector conn1 = new F3LaunchingConnector(); Assert.assertEquals("org.f3.jdi.connect.F3LaunchingConnector", conn1.name()); Assert.assertEquals(true, conn1 instanceof LaunchingConnector); F3ProcessAttachingConnector conn2 = new F3ProcessAttachingConnector(); Assert.assertEquals("org.f3.jdi.connect.F3ProcessAttachingConnector", conn2.name()); Assert.assertEquals(true, conn2 instanceof AttachingConnector); F3RawLaunchingConnector conn3 = new F3RawLaunchingConnector(); Assert.assertEquals("org.f3.jdi.connect.F3RawLaunchingConnector", conn3.name()); Assert.assertEquals(true, conn3 instanceof LaunchingConnector); F3SocketAttachingConnector conn4 = new F3SocketAttachingConnector(); Assert.assertEquals("org.f3.jdi.connect.F3SocketAttachingConnector", conn4.name()); Assert.assertEquals(true, conn4 instanceof AttachingConnector); F3SocketListeningConnector conn5 = new F3SocketListeningConnector(); Assert.assertEquals("org.f3.jdi.connect.F3SocketListeningConnector", conn5.name()); Assert.assertEquals(true, conn5 instanceof ListeningConnector); // Conditionally adding F3 shared mem connectors - because underlying platform shared // memory connectors are not available on all platforms if (F3SharedMemoryAttachingConnector.isAvailable()) { F3SharedMemoryAttachingConnector conn6 = new F3SharedMemoryAttachingConnector(); Assert.assertEquals("org.f3.jdi.connect.F3SharedMemoryAttachingConnector", conn6.name()); Assert.assertEquals(true, conn6 instanceof AttachingConnector); } if (F3SharedMemoryListeningConnector.isAvailable()) { F3SharedMemoryListeningConnector conn7 = new F3SharedMemoryListeningConnector(); Assert.assertEquals("org.f3.jdi.connect.F3SharedMemoryListeningConnector", conn7.name()); Assert.assertEquals(true, conn7 instanceof ListeningConnector); } }
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 Process process1 = launch(address, true, "HelloWorld"); // give first debuggee time to suspend Thread.currentThread().sleep(5000); // launch a second debuggee with the same address Process process2 = launch(address, false, "HelloWorld"); // get exit status from second debuggee int exitCode = 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"); } }
@Override public VirtualMachine connect() throws Exception { List<AttachingConnector> connectors = Bootstrap.virtualMachineManager() .attachingConnectors(); AttachingConnector connector = findConnector( "com.sun.jdi.SocketAttach", connectors); Map<String, Connector.Argument> arguments = connectorArguments(connector); VirtualMachine vm = connector.attach(arguments); // TODO - redirect stdout and stderr? return vm; }
/** * Set the socket-attaching connector's arguments. * * @param connector A socket-attaching connector * @return The socket-attaching connector's arguments */ private Map<String, Connector.Argument> connectorArguments( AttachingConnector connector) { Map<String, Connector.Argument> arguments = connector .defaultArguments(); arguments.get("hostname").setValue(socketAddress.getHostName()); arguments.get("port").setValue( Integer.toString(socketAddress.getPort())); return arguments; }
/** * Call this with the localhost port to connect to. */ public VirtualMachine connect(int port) throws IOException { String strPort = Integer.toString(port); AttachingConnector connector = getConnector(); try { VirtualMachine vm = connect(connector, strPort); return vm; } catch (IllegalConnectorArgumentsException e) { throw new IllegalStateException(e); } }
private AttachingConnector getConnector() { VirtualMachineManager vmManager = Bootstrap .virtualMachineManager(); for (AttachingConnector connector : vmManager .attachingConnectors()) { if ("com.sun.jdi.SocketAttach".equals(connector .name())) { return (AttachingConnector) connector; } } throw new IllegalStateException(); }