Java Code Examples for com.sun.tools.attach.VirtualMachine#detach()

The following examples show how to use com.sun.tools.attach.VirtualMachine#detach() . 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: AttachSelf.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        String value = System.getProperty("jdk.attach.allowAttachSelf");
        boolean canAttachSelf = (value != null) && !value.equals("false");

        String vmid = "" + ProcessHandle.current().pid();

        VirtualMachine vm = null;
        try {
            vm = VirtualMachine.attach(vmid);
            if (!canAttachSelf)
                throw new RuntimeException("Attached to self not expected");
        } catch (IOException ioe) {
            if (canAttachSelf)
                throw ioe;
        } finally {
            if (vm != null) vm.detach();
        }

    }
 
Example 2
Source File: LocalVirtualMachine.java    From jvmtop with GNU General Public License v2.0 6 votes vote down vote up
public static LocalVirtualMachine getLocalVirtualMachine(int vmid)
    throws Exception
{
  Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines();
  LocalVirtualMachine lvm = map.get(vmid);
  if (lvm == null)
  {
    // Check if the VM is attachable but not included in the list
    // if it's running with a different security context.
    // For example, Windows services running
    // local SYSTEM account are attachable if you have Adminstrator
    // privileges.
    boolean attachable = false;
    String address = null;
    String name = String.valueOf(vmid); // default display name to pid

      VirtualMachine vm = VirtualMachine.attach(name);
      attachable = true;
      Properties agentProps = vm.getAgentProperties();
      address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
      vm.detach();
      lvm = new LocalVirtualMachine(vmid, name, attachable, address);

  }
  return lvm;
}
 
Example 3
Source File: JStack.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: JCmd.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void executeCommandForPid(String pid, String command)
    throws AttachNotSupportedException, IOException,
           UnsupportedEncodingException {
    VirtualMachine vm = VirtualMachine.attach(pid);

    // Cast to HotSpotVirtualMachine as this is an
    // implementation specific method.
    HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
    String lines[] = command.split("\\n");
    for (String line : lines) {
        if (line.trim().equals("stop")) {
            break;
        }
        try (InputStream in = hvm.executeJCmd(line);) {
            // read to EOF and just print output
            byte b[] = new byte[256];
            int n;
            boolean messagePrinted = false;
            do {
                n = in.read(b);
                if (n > 0) {
                    String s = new String(b, 0, n, "UTF-8");
                    System.out.print(s);
                    messagePrinted = true;
                }
            } while (n > 0);
            if (!messagePrinted) {
                System.out.println("Command executed successfully");
            }
        }
    }
    vm.detach();
}
 
Example 5
Source File: JCmd.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void executeCommandForPid(String pid, String command)
    throws AttachNotSupportedException, IOException,
           UnsupportedEncodingException {
    VirtualMachine vm = VirtualMachine.attach(pid);

    // Cast to HotSpotVirtualMachine as this is an
    // implementation specific method.
    HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
    String lines[] = command.split("\\n");
    for (String line : lines) {
        if (line.trim().equals("stop")) {
            break;
        }
        try (InputStream in = hvm.executeJCmd(line);) {
            // read to EOF and just print output
            byte b[] = new byte[256];
            int n;
            boolean messagePrinted = false;
            do {
                n = in.read(b);
                if (n > 0) {
                    String s = new String(b, 0, n, "UTF-8");
                    System.out.print(s);
                    messagePrinted = true;
                }
            } while (n > 0);
            if (!messagePrinted) {
                System.out.println("Command executed successfully");
            }
        }
    }
    vm.detach();
}
 
Example 6
Source File: JCmd.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void executeCommandForPid(String pid, String command)
    throws AttachNotSupportedException, IOException,
           UnsupportedEncodingException {
    VirtualMachine vm = VirtualMachine.attach(pid);

    // Cast to HotSpotVirtualMachine as this is an
    // implementation specific method.
    HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
    String lines[] = command.split("\\n");
    for (String line : lines) {
        if (line.trim().equals("stop")) {
            break;
        }
        try (InputStream in = hvm.executeJCmd(line);) {
            // read to EOF and just print output
            byte b[] = new byte[256];
            int n;
            boolean messagePrinted = false;
            do {
                n = in.read(b);
                if (n > 0) {
                    String s = new String(b, 0, n, "UTF-8");
                    System.out.print(s);
                    messagePrinted = true;
                }
            } while (n > 0);
            if (!messagePrinted) {
                System.out.println("Command executed successfully");
            }
        }
    }
    vm.detach();
}
 
Example 7
Source File: JMap.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
    // 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();
}
 
Example 8
Source File: JInfo.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
    // 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();
}
 
Example 9
Source File: StartManagementAgent.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void runTests(int pid) throws Exception {
    VirtualMachine vm = VirtualMachine.attach(""+pid);
    try {

        basicTests(vm);

        testLocalAgent(vm);

        // we retry the remote case several times in case the error
        // was caused by a port conflict
        int i = 0;
        boolean success = false;
        do {
            try {
                System.err.println("Trying remote agent. Try #" + i);
                testRemoteAgent(vm);
                success = true;
            } catch(Exception ex) {
                System.err.println("testRemoteAgent failed with exception:");
                ex.printStackTrace();
                System.err.println("Retrying.");
            }
            i++;
        } while(!success && i < MAX_RETRIES);
        if (!success) {
            throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
        }
    } finally {
        vm.detach();
    }
}
 
Example 10
Source File: StartManagementAgent.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void runTests(int pid) throws Exception {
    VirtualMachine vm = VirtualMachine.attach(""+pid);
    try {

        basicTests(vm);

        testLocalAgent(vm);

        // we retry the remote case several times in case the error
        // was caused by a port conflict
        int i = 0;
        boolean success = false;
        do {
            try {
                System.err.println("Trying remote agent. Try #" + i);
                testRemoteAgent(vm);
                success = true;
            } catch(Exception ex) {
                System.err.println("testRemoteAgent failed with exception:");
                ex.printStackTrace();
                System.err.println("Retrying.");
            }
            i++;
        } while(!success && i < MAX_RETRIES);
        if (!success) {
            throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
        }
    } finally {
        vm.detach();
    }
}
 
Example 11
Source File: JMap.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
    // 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();
}
 
Example 12
Source File: JInfo.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
    // 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();
}
 
Example 13
Source File: JInfo.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
    // 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();
}
 
Example 14
Source File: StartManagementAgent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void runTests(int pid) throws Exception {
    VirtualMachine vm = VirtualMachine.attach(""+pid);
    try {

        basicTests(vm);

        testLocalAgent(vm);

        // we retry the remote case several times in case the error
        // was caused by a port conflict
        int i = 0;
        boolean success = false;
        do {
            try {
                System.err.println("Trying remote agent. Try #" + i);
                testRemoteAgent(vm);
                success = true;
            } catch(Exception ex) {
                System.err.println("testRemoteAgent failed with exception:");
                ex.printStackTrace();
                System.err.println("Retrying.");
            }
            i++;
        } while(!success && i < MAX_RETRIES);
        if (!success) {
            throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
        }
    } finally {
        vm.detach();
    }
}
 
Example 15
Source File: JpomClose.java    From Jpom with MIT License 5 votes vote down vote up
public String stop(String tag) throws IOException {
    VirtualMachine virtualMachine = JvmUtil.getVirtualMachine(tag);
    if (virtualMachine == null) {
        return null;
    }
    try {
        return virtualMachine.id();
    } finally {
        virtualMachine.detach();
    }
}
 
Example 16
Source File: JInfo.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
    // 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();
}
 
Example 17
Source File: JStack.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 18
Source File: JStack.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 19
Source File: Agent.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static public void main(String[] args) throws Exception {
    // Create speculative trap entries
    Test.m();

    String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
    int p = nameOfRunningVM.indexOf('@');
    String pid = nameOfRunningVM.substring(0, p);

    // Make the nmethod go away
    for (int i = 0; i < 10; i++) {
        System.gc();
    }

    // Redefine class
    try {
        VirtualMachine vm = VirtualMachine.attach(pid);
        vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", "");
        vm.detach();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    Test.m();
    // GC will hit dead method pointer
    for (int i = 0; i < 10; i++) {
        System.gc();
    }
}
 
Example 20
Source File: StartManagementAgent.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void runTests(int pid) throws Exception {
    VirtualMachine vm = VirtualMachine.attach(""+pid);
    try {

        basicTests(vm);

        testLocalAgent(vm);

        // we retry the remote case several times in case the error
        // was caused by a port conflict
        int i = 0;
        boolean success = false;
        do {
            try {
                System.err.println("Trying remote agent. Try #" + i);
                testRemoteAgent(vm);
                success = true;
            } catch(Exception ex) {
                System.err.println("testRemoteAgent failed with exception:");
                ex.printStackTrace();
                System.err.println("Retrying.");
            }
            i++;
        } while(!success && i < MAX_RETRIES);
        if (!success) {
            throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
        }
    } finally {
        vm.detach();
    }
}