Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jstack/JStack.java
38918 views
/*1* Copyright (c) 2005, 2013, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstack;2627import java.lang.reflect.Method;28import java.lang.reflect.Constructor;29import java.io.IOException;30import java.io.InputStream;3132import com.sun.tools.attach.VirtualMachine;33import com.sun.tools.attach.AttachNotSupportedException;34import sun.tools.attach.HotSpotVirtualMachine;3536/*37* This class is the main class for the JStack utility. It parses its arguments38* and decides if the command should be executed by the SA JStack tool or by39* obtained the thread dump from a target process using the VM attach mechanism40*/41public class JStack {42public static void main(String[] args) throws Exception {43if (args.length == 0) {44usage(1); // no arguments45}4647boolean useSA = false;48boolean mixed = false;49boolean locks = false;5051// Parse the options (arguments starting with "-" )52int optionCount = 0;53while (optionCount < args.length) {54String arg = args[optionCount];55if (!arg.startsWith("-")) {56break;57}58if (arg.equals("-help") || arg.equals("-h")) {59usage(0);60}61else if (arg.equals("-F")) {62useSA = true;63}64else {65if (arg.equals("-m")) {66mixed = true;67} else {68if (arg.equals("-l")) {69locks = true;70} else {71usage(1);72}73}74}75optionCount++;76}7778// mixed stack implies SA tool79if (mixed) {80useSA = true;81}8283// Next we check the parameter count. If there are two parameters84// we assume core file and executable so we use SA.85int paramCount = args.length - optionCount;86if (paramCount == 0 || paramCount > 2) {87usage(1);88}89if (paramCount == 2) {90useSA = true;91} else {92// If we can't parse it as a pid then it must be debug server93if (!args[optionCount].matches("[0-9]+")) {94useSA = true;95}96}9798// now execute using the SA JStack tool or the built-in thread dumper99if (useSA) {100// parameters (<pid> or <exe> <core>101String params[] = new String[paramCount];102for (int i=optionCount; i<args.length; i++ ){103params[i-optionCount] = args[i];104}105runJStackTool(mixed, locks, params);106} else {107// pass -l to thread dump operation to get extra lock info108String pid = args[optionCount];109String params[];110if (locks) {111params = new String[] { "-l" };112} else {113params = new String[0];114}115runThreadDump(pid, params);116}117}118119120// SA JStack tool121private static void runJStackTool(boolean mixed, boolean locks, String args[]) throws Exception {122Class<?> cl = loadSAClass();123if (cl == null) {124usage(1); // SA not available125}126127// JStack tool also takes -m and -l arguments128if (mixed) {129args = prepend("-m", args);130}131if (locks) {132args = prepend("-l", args);133}134135Class[] argTypes = { String[].class };136Method m = cl.getDeclaredMethod("main", argTypes);137138Object[] invokeArgs = { args };139m.invoke(null, invokeArgs);140}141142// Returns sun.jvm.hotspot.tools.JStack if available, otherwise null.143private static Class<?> loadSAClass() {144//145// Attempt to load JStack class - we specify the system class146// loader so as to cater for development environments where147// this class is on the boot class path but sa-jdi.jar is on148// the system class path. Once the JDK is deployed then both149// tools.jar and sa-jdi.jar are on the system class path.150//151try {152return Class.forName("sun.jvm.hotspot.tools.JStack", true,153ClassLoader.getSystemClassLoader());154} catch (Exception x) { }155return null;156}157158// Attach to pid and perform a thread dump159private static void runThreadDump(String pid, String args[]) throws Exception {160VirtualMachine vm = null;161try {162vm = VirtualMachine.attach(pid);163} catch (Exception x) {164String msg = x.getMessage();165if (msg != null) {166System.err.println(pid + ": " + msg);167} else {168x.printStackTrace();169}170if ((x instanceof AttachNotSupportedException) &&171(loadSAClass() != null)) {172System.err.println("The -F option can be used when the target " +173"process is not responding");174}175System.exit(1);176}177178// Cast to HotSpotVirtualMachine as this is implementation specific179// method.180InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);181182// read to EOF and just print output183byte b[] = new byte[256];184int n;185do {186n = in.read(b);187if (n > 0) {188String s = new String(b, 0, n, "UTF-8");189System.out.print(s);190}191} while (n > 0);192in.close();193vm.detach();194}195196// return a new string array with arg as the first element197private static String[] prepend(String arg, String args[]) {198String[] newargs = new String[args.length+1];199newargs[0] = arg;200System.arraycopy(args, 0, newargs, 1, args.length);201return newargs;202}203204// print usage message205private static void usage(int exit) {206System.err.println("Usage:");207System.err.println(" jstack [-l] <pid>");208System.err.println(" (to connect to running process)");209210if (loadSAClass() != null) {211System.err.println(" jstack -F [-m] [-l] <pid>");212System.err.println(" (to connect to a hung process)");213System.err.println(" jstack [-m] [-l] <executable> <core>");214System.err.println(" (to connect to a core file)");215System.err.println(" jstack [-m] [-l] [server_id@]<remote server IP or hostname>");216System.err.println(" (to connect to a remote debug server)");217}218219System.err.println("");220System.err.println("Options:");221222if (loadSAClass() != null) {223System.err.println(" -F to force a thread dump. Use when jstack <pid> does not respond" +224" (process is hung)");225System.err.println(" -m to print both java and native frames (mixed mode)");226}227228System.err.println(" -l long listing. Prints additional information about locks");229System.err.println(" -h or -help to print this help message");230System.exit(exit);231}232}233234235