1 /*
2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package vm.share;
24 
25 import java.io.File;
26 import java.io.IOException;
27 import java.lang.management.ManagementFactory;
28 import java.lang.reflect.Field;
29 
30 import nsk.share.TestBug;
31 
32 import com.sun.management.HotSpotDiagnosticMXBean;
33 
34 public final class ProcessUtils {
35     static {
36         System.loadLibrary("ProcessUtils");
37     }
38 
ProcessUtils()39     private ProcessUtils() {}
40 
41     /**
42      * Send Ctrl-\ to java process and Ctrl-Break on Windows.
43      * This will usually trigger stack dump for all threads and
44      * may trigger heap dump.
45      *
46      * @return true if it was successful
47      */
sendCtrlBreak()48     public static native boolean sendCtrlBreak();
49 
50     /**
51      * Send any signal to java process on Unix. It currently does nothing on Windows.
52      *
53      * @return true if it was successful
54      */
sendSignal(int signalNum)55     public static native boolean sendSignal(int signalNum);
56 
57     /**
58      * Force java process to dump core.
59      *
60      * This is done by sending SIGSEGV on unix systems.
61      *
62      * @return true if it was successful, false if not (for example on Windows)
63      */
dumpCore()64     public static native boolean dumpCore();
65 
66     /**
67      * Get PID of java process.
68      *
69      * @return PID
70      */
getPid()71     public static native int getPid();
72 
getPid(Process process)73     public static int getPid(Process process) {
74         Throwable exception;
75         try {
76             Field pidField = process.getClass().getDeclaredField("pid");
77             pidField.setAccessible(true);
78             return ((Integer) pidField.get(process)).intValue();
79         } catch (NoSuchFieldException e) {
80             exception = e;
81         } catch (IllegalAccessException e) {
82             exception = e;
83         }
84         // Try to get Windows handle
85         try {
86             Field handleField = process.getClass().getDeclaredField("handle");
87             handleField.setAccessible(true);
88             long handle = ((Long) handleField.get(process)).longValue();
89             return getWindowsPid(handle);
90         } catch (NoSuchFieldException e) {
91             exception = e;
92         } catch (IllegalAccessException e) {
93             exception = e;
94         }
95         throw new TestBug("Unable to determine pid from process class " + process.getClass(), exception);
96     }
97 
getWindowsPid(long handle)98     private static native int getWindowsPid(long handle);
99 
100     @SuppressWarnings("restriction")
dumpHeapWithHotspotDiagnosticMXBean(String fileName)101     public static void dumpHeapWithHotspotDiagnosticMXBean(String fileName) throws IOException {
102         System.err.println("Dumping heap to " + fileName);
103 
104         File f = new File(fileName);
105         if (f.exists())
106             f.delete();
107 
108         HotSpotDiagnosticMXBean b = ManagementFactory.getPlatformMXBeans(
109                 com.sun.management.HotSpotDiagnosticMXBean.class).get(0);
110         b.dumpHeap(fileName, false);
111     }
112 }
113