< prev index next >

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

Print this page
rev 50303 : Thread Dump Extension (memory allocation)


  31 import com.sun.tools.attach.VirtualMachine;
  32 import com.sun.tools.attach.VirtualMachineDescriptor;
  33 import sun.tools.attach.HotSpotVirtualMachine;
  34 import sun.tools.common.ProcessArgumentMatcher;
  35 
  36 /*
  37  * This class is the main class for the JStack utility. It parses its arguments
  38  * and decides if the command should be executed by the SA JStack tool or by
  39  * obtained the thread dump from a target process using the VM attach mechanism
  40  */
  41 public class JStack {
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (args.length == 0) {
  45             usage(1); // no arguments
  46         }
  47 
  48         checkForUnsupportedOptions(args);
  49 
  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("-?")     ||
  60                 arg.equals("-h")     ||
  61                 arg.equals("--help") ||
  62                 // -help: legacy.
  63                 arg.equals("-help")) {
  64                 usage(0);
  65             }
  66             else {
  67                 if (arg.equals("-l")) {
  68                     locks = true;
  69                 } else {



  70                     usage(1);
  71                 }
  72             }

  73             optionCount++;
  74         }
  75 
  76         // Next we check the parameter count.
  77         int paramCount = args.length - optionCount;
  78         if (paramCount != 1) {
  79             usage(1);
  80         }
  81 
  82         // pass -l to thread dump operation to get extra lock info
  83         String pidArg = args[optionCount];
  84         String params[];



  85         if (locks) {
  86             params = new String[] { "-l" };
  87         } else {
  88             params = new String[0];
  89         }

  90         ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);
  91         Collection<String> pids = ap.getVirtualMachinePids(JStack.class);
  92 
  93         if (pids.isEmpty()) {
  94             System.err.println("Could not find any processes matching : '" + pidArg + "'");
  95             System.exit(1);
  96         }
  97 
  98         for (String pid : pids) {
  99             if (pids.size() > 1) {
 100                 System.out.println("Pid:" + pid);
 101             }
 102             runThreadDump(pid, params);
 103         }
 104     }
 105 
 106     // Attach to pid and perform a thread dump
 107     private static void runThreadDump(String pid, String args[]) throws Exception {
 108         VirtualMachine vm = null;
 109         try {


 153 
 154             if (! s.startsWith("-")) {
 155                 paramCount += 1;
 156             }
 157         }
 158 
 159         if (paramCount > 1) {
 160             SAOptionError("More than one non-option argument");
 161         }
 162     }
 163 
 164     private static void SAOptionError(String msg) {
 165         System.err.println("Error: " + msg);
 166         System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jstack instead");
 167         System.exit(1);
 168     }
 169 
 170     // print usage message
 171     private static void usage(int exit) {
 172         System.err.println("Usage:");
 173         System.err.println("    jstack [-l] <pid>");
 174         System.err.println("        (to connect to running process)");
 175         System.err.println("");
 176         System.err.println("Options:");
 177         System.err.println("    -l  long listing. Prints additional information about locks");

 178         System.err.println("    -? -h --help -help to print this help message");
 179         System.exit(exit);
 180     }
 181 }


  31 import com.sun.tools.attach.VirtualMachine;
  32 import com.sun.tools.attach.VirtualMachineDescriptor;
  33 import sun.tools.attach.HotSpotVirtualMachine;
  34 import sun.tools.common.ProcessArgumentMatcher;
  35 
  36 /*
  37  * This class is the main class for the JStack utility. It parses its arguments
  38  * and decides if the command should be executed by the SA JStack tool or by
  39  * obtained the thread dump from a target process using the VM attach mechanism
  40  */
  41 public class JStack {
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (args.length == 0) {
  45             usage(1); // no arguments
  46         }
  47 
  48         checkForUnsupportedOptions(args);
  49 
  50         boolean locks = false;
  51         boolean extended = false;
  52 
  53         // Parse the options (arguments starting with "-" )
  54         int optionCount = 0;
  55         while (optionCount < args.length) {
  56             String arg = args[optionCount];
  57             if (!arg.startsWith("-")) {
  58                 break;
  59             }
  60             if (arg.equals("-?")     ||
  61                 arg.equals("-h")     ||
  62                 arg.equals("--help") ||
  63                 // -help: legacy.
  64                 arg.equals("-help")) {
  65                 usage(0);
  66             }
  67             else {
  68                 if (arg.equals("-l")) {
  69                     locks = true;
  70                 } else {
  71                     if (arg.equals("-e")) {
  72                         extended = true;
  73                     } else {
  74                         usage(1);
  75                     }
  76                 }
  77             }
  78             optionCount++;
  79         }
  80 
  81         // Next we check the parameter count.
  82         int paramCount = args.length - optionCount;
  83         if (paramCount != 1) {
  84             usage(1);
  85         }
  86 
  87         // pass -l to thread dump operation to get extra lock info
  88         String pidArg = args[optionCount];
  89         String params[]= new String[] { "" };
  90         if (extended) {
  91             params[0] += "-e ";
  92         }
  93         if (locks) {
  94             params[0] += "-l";


  95         }
  96 
  97         ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);
  98         Collection<String> pids = ap.getVirtualMachinePids(JStack.class);
  99 
 100         if (pids.isEmpty()) {
 101             System.err.println("Could not find any processes matching : '" + pidArg + "'");
 102             System.exit(1);
 103         }
 104 
 105         for (String pid : pids) {
 106             if (pids.size() > 1) {
 107                 System.out.println("Pid:" + pid);
 108             }
 109             runThreadDump(pid, params);
 110         }
 111     }
 112 
 113     // Attach to pid and perform a thread dump
 114     private static void runThreadDump(String pid, String args[]) throws Exception {
 115         VirtualMachine vm = null;
 116         try {


 160 
 161             if (! s.startsWith("-")) {
 162                 paramCount += 1;
 163             }
 164         }
 165 
 166         if (paramCount > 1) {
 167             SAOptionError("More than one non-option argument");
 168         }
 169     }
 170 
 171     private static void SAOptionError(String msg) {
 172         System.err.println("Error: " + msg);
 173         System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jstack instead");
 174         System.exit(1);
 175     }
 176 
 177     // print usage message
 178     private static void usage(int exit) {
 179         System.err.println("Usage:");
 180         System.err.println("    jstack [-l][-e] <pid>");
 181         System.err.println("        (to connect to running process)");
 182         System.err.println("");
 183         System.err.println("Options:");
 184         System.err.println("    -l  long listing. Prints additional information about locks");
 185         System.err.println("    -e  extended listing. Prints additional information about threads");
 186         System.err.println("    -? -h --help -help to print this help message");
 187         System.exit(exit);
 188     }
 189 }
< prev index next >