0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.jstack;
0N/A
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.lang.reflect.Constructor;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/A
0N/Aimport com.sun.tools.attach.VirtualMachine;
0N/Aimport com.sun.tools.attach.AttachNotSupportedException;
0N/Aimport sun.tools.attach.HotSpotVirtualMachine;
0N/A
0N/A/*
0N/A * This class is the main class for the JStack utility. It parses its arguments
0N/A * and decides if the command should be executed by the SA JStack tool or by
0N/A * obtained the thread dump from a target process using the VM attach mechanism
0N/A */
0N/Apublic class JStack {
0N/A public static void main(String[] args) throws Exception {
0N/A if (args.length == 0) {
0N/A usage(); // no arguments
0N/A }
0N/A
0N/A boolean useSA = false;
0N/A boolean mixed = false;
0N/A boolean locks = false;
0N/A
0N/A // Parse the options (arguments starting with "-" )
0N/A int optionCount = 0;
0N/A while (optionCount < args.length) {
0N/A String arg = args[optionCount];
0N/A if (!arg.startsWith("-")) {
0N/A break;
0N/A }
0N/A if (arg.equals("-F")) {
0N/A useSA = true;
0N/A } else {
0N/A if (arg.equals("-m")) {
0N/A mixed = true;
0N/A } else {
0N/A if (arg.equals("-l")) {
0N/A locks = true;
0N/A } else {
0N/A usage();
0N/A }
0N/A }
0N/A }
0N/A optionCount++;
0N/A }
0N/A
0N/A // mixed stack implies SA tool
0N/A if (mixed) {
0N/A useSA = true;
0N/A }
0N/A
0N/A // Next we check the parameter count. If there are two parameters
0N/A // we assume core file and executable so we use SA.
0N/A int paramCount = args.length - optionCount;
0N/A if (paramCount == 0 || paramCount > 2) {
0N/A usage();
0N/A }
0N/A if (paramCount == 2) {
0N/A useSA = true;
0N/A } else {
0N/A // If we can't parse it as a pid then it must be debug server
0N/A if (!args[optionCount].matches("[0-9]+")) {
0N/A useSA = true;
0N/A }
0N/A }
0N/A
0N/A // now execute using the SA JStack tool or the built-in thread dumper
0N/A if (useSA) {
0N/A // parameters (<pid> or <exe> <core>
0N/A String params[] = new String[paramCount];
0N/A for (int i=optionCount; i<args.length; i++ ){
0N/A params[i-optionCount] = args[i];
0N/A }
0N/A runJStackTool(mixed, locks, params);
0N/A } else {
0N/A // pass -l to thread dump operation to get extra lock info
0N/A String pid = args[optionCount];
0N/A String params[];
0N/A if (locks) {
0N/A params = new String[] { "-l" };
0N/A } else {
0N/A params = new String[0];
0N/A }
0N/A runThreadDump(pid, params);
0N/A }
0N/A }
0N/A
0N/A
0N/A // SA JStack tool
0N/A private static void runJStackTool(boolean mixed, boolean locks, String args[]) throws Exception {
0N/A Class<?> cl = loadSAClass();
0N/A if (cl == null) {
0N/A usage(); // SA not available
0N/A }
0N/A
0N/A // JStack tool also takes -m and -l arguments
0N/A if (mixed) {
0N/A args = prepend("-m", args);
0N/A }
0N/A if (locks) {
0N/A args = prepend("-l", args);
0N/A }
0N/A
0N/A Class[] argTypes = { String[].class };
0N/A Method m = cl.getDeclaredMethod("main", argTypes);
0N/A
0N/A Object[] invokeArgs = { args };
0N/A m.invoke(null, invokeArgs);
0N/A }
0N/A
0N/A // Returns sun.jvm.hotspot.tools.JStack if available, otherwise null.
0N/A private static Class loadSAClass() {
0N/A //
0N/A // Attempt to load JStack class - we specify the system class
0N/A // loader so as to cater for development environments where
0N/A // this class is on the boot class path but sa-jdi.jar is on
0N/A // the system class path. Once the JDK is deployed then both
0N/A // tools.jar and sa-jdi.jar are on the system class path.
0N/A //
0N/A try {
0N/A return Class.forName("sun.jvm.hotspot.tools.JStack", true,
0N/A ClassLoader.getSystemClassLoader());
0N/A } catch (Exception x) { }
0N/A return null;
0N/A }
0N/A
0N/A // Attach to pid and perform a thread dump
0N/A private static void runThreadDump(String pid, String args[]) throws Exception {
0N/A VirtualMachine vm = null;
0N/A try {
0N/A vm = VirtualMachine.attach(pid);
0N/A } catch (Exception x) {
0N/A String msg = x.getMessage();
0N/A if (msg != null) {
0N/A System.err.println(pid + ": " + msg);
0N/A } else {
0N/A x.printStackTrace();
0N/A }
0N/A if ((x instanceof AttachNotSupportedException) &&
0N/A (loadSAClass() != null)) {
0N/A System.err.println("The -F option can be used when the target " +
0N/A "process is not responding");
0N/A }
0N/A System.exit(1);
0N/A }
0N/A
0N/A // Cast to HotSpotVirtualMachine as this is implementation specific
0N/A // method.
0N/A InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);
0N/A
0N/A // read to EOF and just print output
0N/A byte b[] = new byte[256];
0N/A int n;
0N/A do {
0N/A n = in.read(b);
0N/A if (n > 0) {
0N/A String s = new String(b, 0, n, "UTF-8");
0N/A System.out.print(s);
0N/A }
0N/A } while (n > 0);
0N/A in.close();
0N/A vm.detach();
0N/A }
0N/A
0N/A // return a new string array with arg as the first element
0N/A private static String[] prepend(String arg, String args[]) {
0N/A String[] newargs = new String[args.length+1];
0N/A newargs[0] = arg;
0N/A System.arraycopy(args, 0, newargs, 1, args.length);
0N/A return newargs;
0N/A }
0N/A
0N/A // print usage message
0N/A private static void usage() {
0N/A System.out.println("Usage:");
0N/A System.out.println(" jstack [-l] <pid>");
0N/A System.out.println(" (to connect to running process)");
0N/A
0N/A if (loadSAClass() != null) {
0N/A System.out.println(" jstack -F [-m] [-l] <pid>");
0N/A System.out.println(" (to connect to a hung process)");
0N/A System.out.println(" jstack [-m] [-l] <executable> <core>");
0N/A System.out.println(" (to connect to a core file)");
0N/A System.out.println(" jstack [-m] [-l] [server_id@]<remote server IP or hostname>");
0N/A System.out.println(" (to connect to a remote debug server)");
0N/A }
0N/A
0N/A System.out.println("");
0N/A System.out.println("Options:");
0N/A
0N/A if (loadSAClass() != null) {
0N/A System.out.println(" -F to force a thread dump. Use when jstack <pid> does not respond" +
0N/A " (process is hung)");
0N/A System.out.println(" -m to print both java and native frames (mixed mode)");
0N/A }
0N/A
0N/A System.out.println(" -l long listing. Prints additional information about locks");
0N/A System.out.println(" -h or -help to print this help message");
0N/A System.exit(1);
0N/A }
0N/A}