Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstack/JStack.java
41159 views
/*1* Copyright (c) 2005, 2019, 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.io.InputStream;28import java.util.Collection;2930import com.sun.tools.attach.VirtualMachine;31import sun.tools.attach.HotSpotVirtualMachine;32import sun.tools.common.ProcessArgumentMatcher;33import sun.tools.common.PrintStreamPrinter;3435/*36* This class is the main class for the JStack utility. It parses its arguments37* and decides if the command should be executed by the SA JStack tool or by38* obtained the thread dump from a target process using the VM attach mechanism39*/40public class JStack {4142public static void main(String[] args) throws Exception {43if (args.length == 0) {44usage(1); // no arguments45}4647checkForUnsupportedOptions(args);4849boolean locks = false;50boolean extended = false;5152// Parse the options (arguments starting with "-" )53int optionCount = 0;54while (optionCount < args.length) {55String arg = args[optionCount];56if (!arg.startsWith("-")) {57break;58}59if (arg.equals("-?") ||60arg.equals("-h") ||61arg.equals("--help") ||62// -help: legacy.63arg.equals("-help")) {64usage(0);65}66else {67if (arg.equals("-l")) {68locks = true;69} else {70if (arg.equals("-e")) {71extended = true;72} else {73usage(1);74}75}76}77optionCount++;78}7980// Next we check the parameter count.81int paramCount = args.length - optionCount;82if (paramCount != 1) {83usage(1);84}8586// pass -l to thread dump operation to get extra lock info87String pidArg = args[optionCount];88String params[]= new String[] { "" };89if (extended) {90params[0] += "-e ";91}92if (locks) {93params[0] += "-l";94}9596ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);97Collection<String> pids = ap.getVirtualMachinePids(JStack.class);9899if (pids.isEmpty()) {100System.err.println("Could not find any processes matching : '" + pidArg + "'");101System.exit(1);102}103104for (String pid : pids) {105if (pids.size() > 1) {106System.out.println("Pid:" + pid);107}108runThreadDump(pid, params);109}110}111112// Attach to pid and perform a thread dump113private static void runThreadDump(String pid, String args[]) throws Exception {114VirtualMachine vm = null;115try {116vm = VirtualMachine.attach(pid);117} catch (Exception x) {118String msg = x.getMessage();119if (msg != null) {120System.err.println(pid + ": " + msg);121} else {122x.printStackTrace();123}124System.exit(1);125}126127// Cast to HotSpotVirtualMachine as this is implementation specific128// method.129InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);130// read to EOF and just print output131PrintStreamPrinter.drainUTF8(in, System.out);132vm.detach();133}134135private static void checkForUnsupportedOptions(String[] args) {136// Check arguments for -F, -m, and non-numeric value137// and warn the user that SA is not supported anymore138139int paramCount = 0;140141for (String s : args) {142if (s.equals("-F")) {143SAOptionError("-F option used");144}145146if (s.equals("-m")) {147SAOptionError("-m option used");148}149150if (! s.startsWith("-")) {151paramCount += 1;152}153}154155if (paramCount > 1) {156SAOptionError("More than one non-option argument");157}158}159160private static void SAOptionError(String msg) {161System.err.println("Error: " + msg);162System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jstack instead");163System.exit(1);164}165166// print usage message167private static void usage(int exit) {168System.err.println("Usage:");169System.err.println(" jstack [-l][-e] <pid>");170System.err.println(" (to connect to running process)");171System.err.println("");172System.err.println("Options:");173System.err.println(" -l long listing. Prints additional information about locks");174System.err.println(" -e extended listing. Prints additional information about threads");175System.err.println(" -? -h --help -help to print this help message");176System.exit(exit);177}178}179180181