sun.tools.attach.HotSpotVirtualMachine Java Examples

The following examples show how to use sun.tools.attach.HotSpotVirtualMachine. 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: JMap.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #2
Source File: JMap.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #3
Source File: JMap.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #4
Source File: JMap.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #5
Source File: JMap.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #6
Source File: JMap.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #7
Source File: JMap.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #8
Source File: JMap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #9
Source File: JMap.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #10
Source File: JMap.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #11
Source File: JMap.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
    boolean mini = isMiniDump(options);

    VirtualMachine vm = attach(pid);
    String heapName = mini ? "mini-heap" : "heap";
    System.out.println("Dumping " + heapName + " to " + filename + " ...");
    InputStream in = ((HotSpotVirtualMachine)vm).dumpHeap((Object)filename,
                        (live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION),
                        mini ? MINIDUMP_OPTION : null);
    drain(vm, in);
}
 
Example #12
Source File: JMap.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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) {
        usage(1);  // invalid options or no filename
    }

    // 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);
}
 
Example #13
Source File: RuntimeMXBeanTest.java    From code with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, AttachNotSupportedException {

        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        String name = bean.getName();
        int index = name.indexOf('@');
        String pid = name.substring(0, index);
        //这里要区分操作系统
        HotSpotVirtualMachine machine = (HotSpotVirtualMachine) new sun.tools.attach.WindowsAttachProvider().attachVirtualMachine(pid);
        InputStream is = machine.heapHisto("-all");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int len;
        byte[] buff = new byte[1024];
        while ((len = is.read(buff)) > 0)
            os.write(buff, 0, len);
        is.close();
        machine.detach();
        System.out.println(os);

    }
 
Example #14
Source File: JMap.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void executeCommandForPid(String pid, String command, Object ... args)
    throws AttachNotSupportedException, IOException,
           UnsupportedEncodingException {
    VirtualMachine vm = VirtualMachine.attach(pid);

    // Cast to HotSpotVirtualMachine as this is an
    // implementation specific method.
    HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
    try (InputStream in = hvm.executeCommand(command, 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);
    }
    vm.detach();
}
 
Example #15
Source File: LocalVirtualMachineTemplateTest.java    From confucius-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecute() throws IOException, AttachNotSupportedException {
    LocalVirtualMachineTemplate localVirtualMachineTemplate = new LocalVirtualMachineTemplate();

    AttachProvider result = localVirtualMachineTemplate.execute(new HotSpotVirtualMachineCallback<AttachProvider>() {
        @Override
        public AttachProvider doInVirtualMachine(HotSpotVirtualMachine virtualMachine) throws IOException {
            AttachProvider attachProvider = virtualMachine.provider();
            return attachProvider;
        }
    });

    Assert.assertNotNull(result);
}
 
Example #16
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 #17
Source File: JStack.java    From jdk8u-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 #18
Source File: TestLoggerWeakRefLeak.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example #19
Source File: JCmd.java    From jdk8u_jdk 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 #20
Source File: JCmd.java    From jdk8u-jdk 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 #21
Source File: AttachWithStalePidFile.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example #22
Source File: AttachWithStalePidFile.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example #23
Source File: JCmd.java    From hottub 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 #24
Source File: JStack.java    From hottub 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 #25
Source File: AttachWithStalePidFile.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example #26
Source File: JCmd.java    From jdk8u-jdk 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 #27
Source File: JStack.java    From jdk8u-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 #28
Source File: CheckOrigin.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void setOptionUsingAttach(String option, String value) throws Exception {
    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(ProcessTools.getProcessId()+"");
    InputStream in = vm.setFlag(option, value);
    System.out.println("Result from setting '" + option + "' to '" + value + "' using attach:");
    drain(vm, in);
    System.out.println("-- end -- ");
}
 
Example #29
Source File: TestLoggerWeakRefLeak.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Long.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example #30
Source File: JStack.java    From openjdk-jdk9 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();
        }
        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();
}