src/share/classes/sun/tools/jstack/JStack.java

Print this page
rev 8717 : 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
Reviewed-by:


  25 
  26 package sun.tools.jstack;
  27 
  28 import java.lang.reflect.Method;
  29 import java.lang.reflect.Constructor;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 
  33 import com.sun.tools.attach.VirtualMachine;
  34 import com.sun.tools.attach.AttachNotSupportedException;
  35 import sun.tools.attach.HotSpotVirtualMachine;
  36 
  37 /*
  38  * This class is the main class for the JStack utility. It parses its arguments
  39  * and decides if the command should be executed by the SA JStack tool or by
  40  * obtained the thread dump from a target process using the VM attach mechanism
  41  */
  42 public class JStack {
  43     public static void main(String[] args) throws Exception {
  44         if (args.length == 0) {
  45             usage(); // no arguments
  46         }
  47 
  48         boolean useSA = false;
  49         boolean mixed = false;
  50         boolean locks = false;
  51 
  52         // Parse the options (arguments starting with "-" )
  53         int optionCount = 0;
  54         while (optionCount < args.length) {
  55             String arg = args[optionCount];
  56             if (!arg.startsWith("-")) {
  57                 break;
  58             }
  59             if (arg.equals("-F")) {



  60                 useSA = true;
  61             } else {

  62                 if (arg.equals("-m")) {
  63                     mixed = true;
  64                 } else {
  65                     if (arg.equals("-l")) {
  66                        locks = true;
  67                     } else {
  68                         usage();
  69                     }
  70                 }
  71             }
  72             optionCount++;
  73         }
  74 
  75         // mixed stack implies SA tool
  76         if (mixed) {
  77             useSA = true;
  78         }
  79 
  80         // Next we check the parameter count. If there are two parameters
  81         // we assume core file and executable so we use SA.
  82         int paramCount = args.length - optionCount;
  83         if (paramCount == 0 || paramCount > 2) {
  84             usage();
  85         }
  86         if (paramCount == 2) {
  87             useSA = true;
  88         } else {
  89             // If we can't parse it as a pid then it must be debug server
  90             if (!args[optionCount].matches("[0-9]+")) {
  91                 useSA = true;
  92             }
  93         }
  94 
  95         // now execute using the SA JStack tool or the built-in thread dumper
  96         if (useSA) {
  97             // parameters (<pid> or <exe> <core>
  98             String params[] = new String[paramCount];
  99             for (int i=optionCount; i<args.length; i++ ){
 100                 params[i-optionCount] = args[i];
 101             }
 102             runJStackTool(mixed, locks, params);
 103         } else {
 104             // pass -l to thread dump operation to get extra lock info
 105             String pid = args[optionCount];
 106             String params[];
 107             if (locks) {
 108                 params = new String[] { "-l" };
 109             } else {
 110                 params = new String[0];
 111             }
 112             runThreadDump(pid, params);
 113         }
 114     }
 115 
 116 
 117     // SA JStack tool
 118     private static void runJStackTool(boolean mixed, boolean locks, String args[]) throws Exception {
 119         Class<?> cl = loadSAClass();
 120         if (cl == null) {
 121             usage();            // SA not available
 122         }
 123 
 124         // JStack tool also takes -m and -l arguments
 125         if (mixed) {
 126             args = prepend("-m", args);
 127         }
 128         if (locks) {
 129             args = prepend("-l", args);
 130         }
 131 
 132         Class[] argTypes = { String[].class };
 133         Method m = cl.getDeclaredMethod("main", argTypes);
 134 
 135         Object[] invokeArgs = { args };
 136         m.invoke(null, invokeArgs);
 137     }
 138 
 139     // Returns sun.jvm.hotspot.tools.JStack if available, otherwise null.
 140     private static Class<?> loadSAClass() {
 141         //


 182         do {
 183             n = in.read(b);
 184             if (n > 0) {
 185                 String s = new String(b, 0, n, "UTF-8");
 186                 System.out.print(s);
 187             }
 188         } while (n > 0);
 189         in.close();
 190         vm.detach();
 191     }
 192 
 193     // return a new string array with arg as the first element
 194     private static String[] prepend(String arg, String args[]) {
 195         String[] newargs = new String[args.length+1];
 196         newargs[0] = arg;
 197         System.arraycopy(args, 0, newargs, 1, args.length);
 198         return newargs;
 199     }
 200 
 201     // print usage message
 202     private static void usage() {
 203         System.out.println("Usage:");
 204         System.out.println("    jstack [-l] <pid>");
 205         System.out.println("        (to connect to running process)");
 206 
 207         if (loadSAClass() != null) {
 208             System.out.println("    jstack -F [-m] [-l] <pid>");
 209             System.out.println("        (to connect to a hung process)");
 210             System.out.println("    jstack [-m] [-l] <executable> <core>");
 211             System.out.println("        (to connect to a core file)");
 212             System.out.println("    jstack [-m] [-l] [server_id@]<remote server IP or hostname>");
 213             System.out.println("        (to connect to a remote debug server)");
 214         }
 215 
 216         System.out.println("");
 217         System.out.println("Options:");
 218 
 219         if (loadSAClass() != null) {
 220             System.out.println("    -F  to force a thread dump. Use when jstack <pid> does not respond" +
 221                 " (process is hung)");
 222             System.out.println("    -m  to print both java and native frames (mixed mode)");
 223         }
 224 
 225         System.out.println("    -l  long listing. Prints additional information about locks");
 226         System.out.println("    -h or -help to print this help message");
 227         System.exit(1);
 228     }
 229 }


  25 
  26 package sun.tools.jstack;
  27 
  28 import java.lang.reflect.Method;
  29 import java.lang.reflect.Constructor;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 
  33 import com.sun.tools.attach.VirtualMachine;
  34 import com.sun.tools.attach.AttachNotSupportedException;
  35 import sun.tools.attach.HotSpotVirtualMachine;
  36 
  37 /*
  38  * This class is the main class for the JStack utility. It parses its arguments
  39  * and decides if the command should be executed by the SA JStack tool or by
  40  * obtained the thread dump from a target process using the VM attach mechanism
  41  */
  42 public class JStack {
  43     public static void main(String[] args) throws Exception {
  44         if (args.length == 0) {
  45             usage(1); // no arguments
  46         }
  47 
  48         boolean useSA = false;
  49         boolean mixed = false;
  50         boolean locks = false;
  51 
  52         // Parse the options (arguments starting with "-" )
  53         int optionCount = 0;
  54         while (optionCount < args.length) {
  55             String arg = args[optionCount];
  56             if (!arg.startsWith("-")) {
  57                 break;
  58             }
  59             if (arg.equals("-help") || arg.equals("-h")) {
  60                 usage(0);
  61             }
  62             else if (arg.equals("-F")) {
  63                 useSA = true;
  64             }
  65             else {
  66                 if (arg.equals("-m")) {
  67                     mixed = true;
  68                 } else {
  69                     if (arg.equals("-l")) {
  70                        locks = true;
  71                     } else {
  72                         usage(1);
  73                     }
  74                 }
  75             }
  76             optionCount++;
  77         }
  78 
  79         // mixed stack implies SA tool
  80         if (mixed) {
  81             useSA = true;
  82         }
  83 
  84         // Next we check the parameter count. If there are two parameters
  85         // we assume core file and executable so we use SA.
  86         int paramCount = args.length - optionCount;
  87         if (paramCount == 0 || paramCount > 2) {
  88             usage(1);
  89         }
  90         if (paramCount == 2) {
  91             useSA = true;
  92         } else {
  93             // If we can't parse it as a pid then it must be debug server
  94             if (!args[optionCount].matches("[0-9]+")) {
  95                 useSA = true;
  96             }
  97         }
  98 
  99         // now execute using the SA JStack tool or the built-in thread dumper
 100         if (useSA) {
 101             // parameters (<pid> or <exe> <core>
 102             String params[] = new String[paramCount];
 103             for (int i=optionCount; i<args.length; i++ ){
 104                 params[i-optionCount] = args[i];
 105             }
 106             runJStackTool(mixed, locks, params);
 107         } else {
 108             // pass -l to thread dump operation to get extra lock info
 109             String pid = args[optionCount];
 110             String params[];
 111             if (locks) {
 112                 params = new String[] { "-l" };
 113             } else {
 114                 params = new String[0];
 115             }
 116             runThreadDump(pid, params);
 117         }
 118     }
 119 
 120 
 121     // SA JStack tool
 122     private static void runJStackTool(boolean mixed, boolean locks, String args[]) throws Exception {
 123         Class<?> cl = loadSAClass();
 124         if (cl == null) {
 125             usage(1);            // SA not available
 126         }
 127 
 128         // JStack tool also takes -m and -l arguments
 129         if (mixed) {
 130             args = prepend("-m", args);
 131         }
 132         if (locks) {
 133             args = prepend("-l", args);
 134         }
 135 
 136         Class[] argTypes = { String[].class };
 137         Method m = cl.getDeclaredMethod("main", argTypes);
 138 
 139         Object[] invokeArgs = { args };
 140         m.invoke(null, invokeArgs);
 141     }
 142 
 143     // Returns sun.jvm.hotspot.tools.JStack if available, otherwise null.
 144     private static Class<?> loadSAClass() {
 145         //


 186         do {
 187             n = in.read(b);
 188             if (n > 0) {
 189                 String s = new String(b, 0, n, "UTF-8");
 190                 System.out.print(s);
 191             }
 192         } while (n > 0);
 193         in.close();
 194         vm.detach();
 195     }
 196 
 197     // return a new string array with arg as the first element
 198     private static String[] prepend(String arg, String args[]) {
 199         String[] newargs = new String[args.length+1];
 200         newargs[0] = arg;
 201         System.arraycopy(args, 0, newargs, 1, args.length);
 202         return newargs;
 203     }
 204 
 205     // print usage message
 206     private static void usage(int exit) {
 207         System.err.println("Usage:");
 208         System.err.println("    jstack [-l] <pid>");
 209         System.err.println("        (to connect to running process)");
 210 
 211         if (loadSAClass() != null) {
 212             System.err.println("    jstack -F [-m] [-l] <pid>");
 213             System.err.println("        (to connect to a hung process)");
 214             System.err.println("    jstack [-m] [-l] <executable> <core>");
 215             System.err.println("        (to connect to a core file)");
 216             System.err.println("    jstack [-m] [-l] [server_id@]<remote server IP or hostname>");
 217             System.err.println("        (to connect to a remote debug server)");
 218         }
 219 
 220         System.err.println("");
 221         System.err.println("Options:");
 222 
 223         if (loadSAClass() != null) {
 224             System.err.println("    -F  to force a thread dump. Use when jstack <pid> does not respond" +
 225                 " (process is hung)");
 226             System.err.println("    -m  to print both java and native frames (mixed mode)");
 227         }
 228 
 229         System.err.println("    -l  long listing. Prints additional information about locks");
 230         System.err.println("    -h or -help to print this help message");
 231         System.exit(exit);
 232     }
 233 }