Search in sources :

Example 1 with VirtualMachineManager

use of com.sun.jdi.VirtualMachineManager in project intellij-community by JetBrains.

the class ConnectionServiceWrapper method createVirtualMachine.

public VirtualMachine createVirtualMachine() throws IOException {
    try {
        final VirtualMachineManager virtualMachineManager = Bootstrap.virtualMachineManager();
        //noinspection HardCodedStringLiteral
        final Method method = virtualMachineManager.getClass().getMethod("createVirtualMachine", new Class[] { myDelegateClass });
        return (VirtualMachine) method.invoke(virtualMachineManager, new Object[] { myConnection });
    } catch (NoSuchMethodException | IllegalAccessException e) {
        LOG.error(e);
    } catch (InvocationTargetException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        }
        if (cause instanceof VMDisconnectedException) {
            // ignore this one
            return null;
        }
        LOG.error(e);
    }
    return null;
}
Also used : VMDisconnectedException(com.sun.jdi.VMDisconnectedException) VirtualMachineManager(com.sun.jdi.VirtualMachineManager) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) VirtualMachine(com.sun.jdi.VirtualMachine)

Example 2 with VirtualMachineManager

use of com.sun.jdi.VirtualMachineManager 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)

Example 3 with VirtualMachineManager

use of com.sun.jdi.VirtualMachineManager in project ceylon by eclipse.

the class TracerImpl method start.

public void start() throws Exception {
    VirtualMachineManager vmm = com.sun.jdi.Bootstrap.virtualMachineManager();
    LaunchingConnector conn = vmm.defaultConnector();
    Map<String, Argument> defaultArguments = conn.defaultArguments();
    defaultArguments.get("main").setValue(mainClass);
    defaultArguments.get("options").setValue("-cp " + classPath);
    System.out.println(defaultArguments);
    vm = conn.launch(defaultArguments);
    err = vm.process().getErrorStream();
    out = vm.process().getInputStream();
    eq = vm.eventQueue();
    rm = vm.eventRequestManager();
    outer: while (true) {
        echo(err, System.err);
        echo(out, System.out);
        events = eq.remove();
        for (Event event : events) {
            if (event instanceof VMStartEvent) {
                System.out.println(event);
                break outer;
            } else if (event instanceof VMDisconnectEvent || event instanceof VMDeathEvent) {
                System.out.println(event);
                vm = null;
                rm = null;
                eq = null;
                break outer;
            }
        }
        events.resume();
    }
    echo(err, System.err);
    echo(out, System.out);
}
Also used : VMDisconnectEvent(com.sun.jdi.event.VMDisconnectEvent) Argument(com.sun.jdi.connect.Connector.Argument) VMDeathEvent(com.sun.jdi.event.VMDeathEvent) LaunchingConnector(com.sun.jdi.connect.LaunchingConnector) VMStartEvent(com.sun.jdi.event.VMStartEvent) MethodEntryEvent(com.sun.jdi.event.MethodEntryEvent) Event(com.sun.jdi.event.Event) VMDisconnectEvent(com.sun.jdi.event.VMDisconnectEvent) VMDeathEvent(com.sun.jdi.event.VMDeathEvent) StepEvent(com.sun.jdi.event.StepEvent) VMStartEvent(com.sun.jdi.event.VMStartEvent) MethodExitEvent(com.sun.jdi.event.MethodExitEvent) VirtualMachineManager(com.sun.jdi.VirtualMachineManager)

Example 4 with VirtualMachineManager

use of com.sun.jdi.VirtualMachineManager in project warn-report by saaavsaaa.

the class debugger method rTest.

@Test
public void rTest() throws Exception {
    VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
    List<AttachingConnector> connectors = vmm.attachingConnectors();
    SocketAttachingConnector sac = null;
    for (AttachingConnector ac : connectors) {
        if (ac instanceof SocketAttachingConnector) {
            sac = (SocketAttachingConnector) ac;
            break;
        }
    }
    if (sac == null) {
        System.out.println("JDI error");
        return;
    }
    Map arguments = sac.defaultArguments();
    Connector.Argument hostArg = (Connector.Argument) arguments.get(HOST);
    Connector.Argument portArg = (Connector.Argument) arguments.get(PORT);
    hostArg.setValue("127.0.0.1");
    portArg.setValue(String.valueOf(9999));
    vm = sac.attach(arguments);
    List<ReferenceType> classesByName = vm.classesByName("cn.tellwhy.DailyTest");
    if (classesByName == null || classesByName.size() == 0) {
        System.out.println("No class found");
        return;
    }
    ReferenceType rt = classesByName.get(0);
    List<Method> methodsByName = rt.methodsByName("daily");
    if (methodsByName == null || methodsByName.size() == 0) {
        System.out.println("No method found");
        return;
    }
    Method method = methodsByName.get(0);
    vm.setDebugTraceMode(VirtualMachine.TRACE_EVENTS);
    vm.resume();
    EventRequestManager erm = vm.eventRequestManager();
    MethodEntryRequest methodEntryRequest = erm.createMethodEntryRequest();
    methodEntryRequest.addClassFilter(rt);
    methodEntryRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
    methodEntryRequest.enable();
    BreakpointRequest breakpointRequest = erm.createBreakpointRequest(method.location());
    breakpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
    breakpointRequest.enable();
    eventLoop();
}
Also used : SocketAttachingConnector(com.sun.tools.jdi.SocketAttachingConnector) AttachingConnector(com.sun.jdi.connect.AttachingConnector) Connector(com.sun.jdi.connect.Connector) LaunchingConnector(com.sun.jdi.connect.LaunchingConnector) SocketAttachingConnector(com.sun.tools.jdi.SocketAttachingConnector) AttachingConnector(com.sun.jdi.connect.AttachingConnector) BreakpointRequest(com.sun.jdi.request.BreakpointRequest) MethodEntryRequest(com.sun.jdi.request.MethodEntryRequest) Method(com.sun.jdi.Method) SocketAttachingConnector(com.sun.tools.jdi.SocketAttachingConnector) EventRequestManager(com.sun.jdi.request.EventRequestManager) ReferenceType(com.sun.jdi.ReferenceType) VirtualMachineManager(com.sun.jdi.VirtualMachineManager) Map(java.util.Map) Test(org.junit.Test)

Example 5 with VirtualMachineManager

use of com.sun.jdi.VirtualMachineManager in project warn-report by saaavsaaa.

the class debugger method test.

@Test
public void test() throws VMStartException, IllegalConnectorArgumentsException, IOException {
    // Debugger 通过 Bootstrap 获取唯一的虚拟机管理器
    VirtualMachineManager virtualMachineManager = Bootstrap.virtualMachineManager();
    // 虚拟机管理器将在第一次被调用时初始化可用的链接器
    LaunchingConnector defaultConnector = virtualMachineManager.defaultConnector();
    // 启动目标程序,连接调试器(Debugger)与目标虚拟机(VirtualMachine)
    Map<String, Connector.Argument> arguments = defaultConnector.defaultArguments();
    Connector.Argument arg = arguments.get("main");
    arg.setValue("cn.tellwhy.DailyTest");
    VirtualMachine targetVM = defaultConnector.launch(arguments);
    // 获取 Mirror 对象实例的虚拟机
    VirtualMachine virtualMachine = targetVM.virtualMachine();
    virtualMachine.allClasses().forEach(a -> System.out.println("===============:" + a.name()));
}
Also used : SocketAttachingConnector(com.sun.tools.jdi.SocketAttachingConnector) AttachingConnector(com.sun.jdi.connect.AttachingConnector) Connector(com.sun.jdi.connect.Connector) LaunchingConnector(com.sun.jdi.connect.LaunchingConnector) LaunchingConnector(com.sun.jdi.connect.LaunchingConnector) VirtualMachineManager(com.sun.jdi.VirtualMachineManager) VirtualMachine(com.sun.jdi.VirtualMachine) Test(org.junit.Test)

Aggregations

VirtualMachineManager (com.sun.jdi.VirtualMachineManager)6 LaunchingConnector (com.sun.jdi.connect.LaunchingConnector)4 AttachingConnector (com.sun.jdi.connect.AttachingConnector)3 Argument (com.sun.jdi.connect.Connector.Argument)3 VirtualMachine (com.sun.jdi.VirtualMachine)2 Connector (com.sun.jdi.connect.Connector)2 Event (com.sun.jdi.event.Event)2 MethodEntryEvent (com.sun.jdi.event.MethodEntryEvent)2 MethodExitEvent (com.sun.jdi.event.MethodExitEvent)2 StepEvent (com.sun.jdi.event.StepEvent)2 VMDeathEvent (com.sun.jdi.event.VMDeathEvent)2 VMDisconnectEvent (com.sun.jdi.event.VMDisconnectEvent)2 VMStartEvent (com.sun.jdi.event.VMStartEvent)2 SocketAttachingConnector (com.sun.tools.jdi.SocketAttachingConnector)2 Test (org.junit.Test)2 Method (com.sun.jdi.Method)1 ReferenceType (com.sun.jdi.ReferenceType)1 VMDisconnectedException (com.sun.jdi.VMDisconnectedException)1 IllegalConnectorArgumentsException (com.sun.jdi.connect.IllegalConnectorArgumentsException)1 BreakpointRequest (com.sun.jdi.request.BreakpointRequest)1