Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/ProcessUtils.java
41153 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share;2324import java.io.File;25import java.io.IOException;26import java.lang.management.ManagementFactory;27import java.lang.reflect.Field;2829import nsk.share.TestBug;3031import com.sun.management.HotSpotDiagnosticMXBean;3233public final class ProcessUtils {34static {35System.loadLibrary("ProcessUtils");36}3738private ProcessUtils() {}3940/**41* Send Ctrl-\ to java process and Ctrl-Break on Windows.42* This will usually trigger stack dump for all threads and43* may trigger heap dump.44*45* @return true if it was successful46*/47public static native boolean sendCtrlBreak();4849/**50* Send any signal to java process on Unix. It currently does nothing on Windows.51*52* @return true if it was successful53*/54public static native boolean sendSignal(int signalNum);5556/**57* Force java process to dump core.58*59* This is done by sending SIGSEGV on unix systems.60*61* @return true if it was successful, false if not (for example on Windows)62*/63public static native boolean dumpCore();6465/**66* Get PID of java process.67*68* @return PID69*/70public static native int getPid();7172public static int getPid(Process process) {73Throwable exception;74try {75Field pidField = process.getClass().getDeclaredField("pid");76pidField.setAccessible(true);77return ((Integer) pidField.get(process)).intValue();78} catch (NoSuchFieldException e) {79exception = e;80} catch (IllegalAccessException e) {81exception = e;82}83// Try to get Windows handle84try {85Field handleField = process.getClass().getDeclaredField("handle");86handleField.setAccessible(true);87long handle = ((Long) handleField.get(process)).longValue();88return getWindowsPid(handle);89} catch (NoSuchFieldException e) {90exception = e;91} catch (IllegalAccessException e) {92exception = e;93}94throw new TestBug("Unable to determine pid from process class " + process.getClass(), exception);95}9697private static native int getWindowsPid(long handle);9899@SuppressWarnings("restriction")100public static void dumpHeapWithHotspotDiagnosticMXBean(String fileName) throws IOException {101System.err.println("Dumping heap to " + fileName);102103File f = new File(fileName);104if (f.exists())105f.delete();106107HotSpotDiagnosticMXBean b = ManagementFactory.getPlatformMXBeans(108com.sun.management.HotSpotDiagnosticMXBean.class).get(0);109b.dumpHeap(fileName, false);110}111}112113114