Search in sources :

Example 16 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project jdk8u_jdk by JetBrains.

the class JMap method dump.

private static void dump(String pid, String options) throws IOException {
    // parse the options to get the dump filename
    String filename = parseDumpOptions(options);
    if (filename == null) {
        // invalid options or no filename
        usage(1);
    }
    // get the canonical path - important to avoid just passing
    // a "heap.bin" and having the dump created in the target VM
    // working directory rather than the directory where jmap
    // is executed.
    filename = new File(filename).getCanonicalPath();
    // dump live objects only or not
    boolean live = isDumpLiveObjects(options);
    VirtualMachine vm = attach(pid);
    System.out.println("Dumping heap to " + filename + " ...");
    InputStream in = ((HotSpotVirtualMachine) vm).dumpHeap((Object) filename, (live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION));
    drain(vm, in);
}
Also used : InputStream(java.io.InputStream) HotSpotVirtualMachine(sun.tools.attach.HotSpotVirtualMachine) File(java.io.File) VirtualMachine(com.sun.tools.attach.VirtualMachine) HotSpotVirtualMachine(sun.tools.attach.HotSpotVirtualMachine)

Example 17 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project jdk8u_jdk by JetBrains.

the class JInfo method flag.

private static void flag(String pid, String option) throws IOException {
    VirtualMachine vm = attach(pid);
    String flag;
    InputStream in;
    int index = option.indexOf('=');
    if (index != -1) {
        flag = option.substring(0, index);
        String value = option.substring(index + 1);
        in = ((HotSpotVirtualMachine) vm).setFlag(flag, value);
    } else {
        char c = option.charAt(0);
        switch(c) {
            case '+':
                flag = option.substring(1);
                in = ((HotSpotVirtualMachine) vm).setFlag(flag, "1");
                break;
            case '-':
                flag = option.substring(1);
                in = ((HotSpotVirtualMachine) vm).setFlag(flag, "0");
                break;
            default:
                flag = option;
                in = ((HotSpotVirtualMachine) vm).printFlag(flag);
                break;
        }
    }
    drain(vm, in);
}
Also used : InputStream(java.io.InputStream) VirtualMachine(com.sun.tools.attach.VirtualMachine) HotSpotVirtualMachine(sun.tools.attach.HotSpotVirtualMachine)

Example 18 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project jdk8u_jdk by JetBrains.

the class JStack method runThreadDump.

// Attach to pid and perform a thread dump
private static void runThreadDump(String pid, String[] args) throws Exception {
    VirtualMachine vm = null;
    try {
        vm = VirtualMachine.attach(pid);
    } catch (Exception x) {
        String msg = x.getMessage();
        if (msg != null) {
            System.err.println(pid + ": " + msg);
        } else {
            x.printStackTrace();
        }
        if ((x instanceof AttachNotSupportedException) && (loadSAClass() != null)) {
            System.err.println("The -F option can be used when the target " + "process is not responding");
        }
        System.exit(1);
    }
    // Cast to HotSpotVirtualMachine as this is implementation specific
    // method.
    InputStream in = ((HotSpotVirtualMachine) vm).remoteDataDump((Object[]) args);
    // read to EOF and just print output
    byte[] b = new byte[256];
    int n;
    do {
        n = in.read(b);
        if (n > 0) {
            String s = new String(b, 0, n, "UTF-8");
            System.out.print(s);
        }
    } while (n > 0);
    in.close();
    vm.detach();
}
Also used : InputStream(java.io.InputStream) HotSpotVirtualMachine(sun.tools.attach.HotSpotVirtualMachine) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) IOException(java.io.IOException) VirtualMachine(com.sun.tools.attach.VirtualMachine) HotSpotVirtualMachine(sun.tools.attach.HotSpotVirtualMachine)

Example 19 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project jdk8u_jdk by JetBrains.

the class LocalVirtualMachine method getAttachableVMs.

private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) {
    List<VirtualMachineDescriptor> vms = VirtualMachine.list();
    for (VirtualMachineDescriptor vmd : vms) {
        try {
            Integer vmid = Integer.valueOf(vmd.id());
            if (!map.containsKey(vmid)) {
                boolean attachable = false;
                String address = null;
                try {
                    VirtualMachine vm = VirtualMachine.attach(vmd);
                    attachable = true;
                    Properties agentProps = vm.getAgentProperties();
                    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
                    vm.detach();
                } catch (AttachNotSupportedException x) {
                // not attachable
                } catch (IOException x) {
                // ignore
                }
                map.put(vmid, new LocalVirtualMachine(vmid.intValue(), vmd.displayName(), attachable, address));
            }
        } catch (NumberFormatException e) {
        // do not support vmid different than pid
        }
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 20 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project warn-report by saaavsaaa.

the class TestAgentVMAttacher method main.

public static void main(String[] args) throws Exception {
    String pid = "6236";
    String agentPath = "/home/aaa/Code/agent/agentest.so";
    VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid);
    virtualMachine.loadAgentPath(agentPath, null);
    // virtualmachine.loadAgent("/home/aaa/Code/agent-1.0-SNAPSHOT.jar");
    virtualMachine.detach();
}
Also used : VirtualMachine(com.sun.tools.attach.VirtualMachine)

Aggregations

VirtualMachine (com.sun.tools.attach.VirtualMachine)20 IOException (java.io.IOException)8 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)6 File (java.io.File)5 InputStream (java.io.InputStream)5 HotSpotVirtualMachine (sun.tools.attach.HotSpotVirtualMachine)5 Properties (java.util.Properties)4 JMXServiceURL (javax.management.remote.JMXServiceURL)4 AgentInitializationException (com.sun.tools.attach.AgentInitializationException)3 AgentLoadException (com.sun.tools.attach.AgentLoadException)3 VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)3 Test (org.junit.Test)2 SmartFile (com.sun.enterprise.universal.io.SmartFile)1 AttachOperationFailedException (com.sun.tools.attach.AttachOperationFailedException)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 JMXConnector (javax.management.remote.JMXConnector)1