src/share/classes/sun/tools/jmap/JMap.java

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

@@ -58,11 +58,11 @@
     // Default option (if nothing provided)
     private static String DEFAULT_OPTION = "-pmap";
 
     public static void main(String[] args) throws Exception {
         if (args.length == 0) {
-            usage(); // no arguments
+            usage(1); // no arguments
         }
 
         // used to indicate if we should use SA
         boolean useSA = false;
 

@@ -75,15 +75,17 @@
         while (optionCount < args.length) {
             String arg = args[optionCount];
             if (!arg.startsWith("-")) {
                 break;
             }
-            if (arg.equals(FORCE_SA_OPTION)) {
+            if (arg.equals("-help") || arg.equals("-h")) {
+                usage(0);
+            } else if (arg.equals(FORCE_SA_OPTION)) {
                 useSA = true;
             } else {
                 if (option != null) {
-                    usage();  // option already specified
+                    usage(1);  // option already specified
                 }
                 option = arg;
             }
             optionCount++;
         }

@@ -99,11 +101,11 @@
         // Next we check the parameter count. For the SA tools there are
         // one or two parameters. For the built-in -dump option there is
         // only one parameter (the process-id)
         int paramCount = args.length - optionCount;
         if (paramCount == 0 || paramCount > 2) {
-            usage();
+            usage(1);
         }
 
         if (optionCount == 0 || paramCount != 1) {
             useSA = true;
         } else {

@@ -137,11 +139,11 @@
             } else if (option.equals(LIVE_HISTO_OPTION)) {
                 histo(pid, true);
             } else if (option.startsWith(DUMP_OPTION_PREFIX)) {
                 dump(pid, option);
             } else {
-                usage();
+                usage(1);
             }
         }
     }
 
     // Invoke SA tool  with the given arguments

@@ -159,11 +161,13 @@
 
         // -dump option needs to be handled in a special way
         if (option.startsWith(DUMP_OPTION_PREFIX)) {
             // first check that the option can be parsed
             String fn = parseDumpOptions(option);
-            if (fn == null) usage();
+            if (fn == null) {
+                usage(1);
+            }
 
             // tool for heap dumping
             tool = "sun.jvm.hotspot.tools.HeapDumper";
 
             // HeapDumper -f <file>

@@ -178,17 +182,17 @@
                 }
                 i++;
             }
         }
         if (tool == null) {
-            usage();   // no mapping to tool
+            usage(1);   // no mapping to tool
         }
 
         // Tool not available on this  platform.
         Class<?> c = loadClass(tool);
         if (c == null) {
-            usage();
+            usage(1);
         }
 
         // invoke the main method with the arguments
         Class[] argTypes = { String[].class } ;
         Method m = c.getDeclaredMethod("main", argTypes);

@@ -223,11 +227,11 @@
 
     private static void dump(String pid, String options) throws IOException {
         // parse the options to get the dump filename
         String filename = parseDumpOptions(options);
         if (filename == null) {
-            usage();  // invalid options or no filename
+            usage(1);  // invalid options or no filename
         }
 
         // get the canonical path - important to avoid just passing
         // a "heap.bin" and having the dump created in the target VM
         // working directory rather than the directory where jmap

@@ -339,51 +343,51 @@
         Class<?> c = loadClass("sun.jvm.hotspot.tools.HeapSummary");
         return (c != null);
     }
 
     // print usage message
-    private static void usage() {
-        System.out.println("Usage:");
+    private static void usage(int exit) {
+        System.err.println("Usage:");
         if (haveSA()) {
-            System.out.println("    jmap [option] <pid>");
-            System.out.println("        (to connect to running process)");
-            System.out.println("    jmap [option] <executable <core>");
-            System.out.println("        (to connect to a core file)");
-            System.out.println("    jmap [option] [server_id@]<remote server IP or hostname>");
-            System.out.println("        (to connect to remote debug server)");
-            System.out.println("");
-            System.out.println("where <option> is one of:");
-            System.out.println("    <none>               to print same info as Solaris pmap");
-            System.out.println("    -heap                to print java heap summary");
-            System.out.println("    -histo[:live]        to print histogram of java object heap; if the \"live\"");
-            System.out.println("                         suboption is specified, only count live objects");
-            System.out.println("    -clstats             to print class loader statistics");
-            System.out.println("    -finalizerinfo       to print information on objects awaiting finalization");
-            System.out.println("    -dump:<dump-options> to dump java heap in hprof binary format");
-            System.out.println("                         dump-options:");
-            System.out.println("                           live         dump only live objects; if not specified,");
-            System.out.println("                                        all objects in the heap are dumped.");
-            System.out.println("                           format=b     binary format");
-            System.out.println("                           file=<file>  dump heap to <file>");
-            System.out.println("                         Example: jmap -dump:live,format=b,file=heap.bin <pid>");
-            System.out.println("    -F                   force. Use with -dump:<dump-options> <pid> or -histo");
-            System.out.println("                         to force a heap dump or histogram when <pid> does not");
-            System.out.println("                         respond. The \"live\" suboption is not supported");
-            System.out.println("                         in this mode.");
-            System.out.println("    -h | -help           to print this help message");
-            System.out.println("    -J<flag>             to pass <flag> directly to the runtime system");
+            System.err.println("    jmap [option] <pid>");
+            System.err.println("        (to connect to running process)");
+            System.err.println("    jmap [option] <executable <core>");
+            System.err.println("        (to connect to a core file)");
+            System.err.println("    jmap [option] [server_id@]<remote server IP or hostname>");
+            System.err.println("        (to connect to remote debug server)");
+            System.err.println("");
+            System.err.println("where <option> is one of:");
+            System.err.println("    <none>               to print same info as Solaris pmap");
+            System.err.println("    -heap                to print java heap summary");
+            System.err.println("    -histo[:live]        to print histogram of java object heap; if the \"live\"");
+            System.err.println("                         suboption is specified, only count live objects");
+            System.err.println("    -clstats             to print class loader statistics");
+            System.err.println("    -finalizerinfo       to print information on objects awaiting finalization");
+            System.err.println("    -dump:<dump-options> to dump java heap in hprof binary format");
+            System.err.println("                         dump-options:");
+            System.err.println("                           live         dump only live objects; if not specified,");
+            System.err.println("                                        all objects in the heap are dumped.");
+            System.err.println("                           format=b     binary format");
+            System.err.println("                           file=<file>  dump heap to <file>");
+            System.err.println("                         Example: jmap -dump:live,format=b,file=heap.bin <pid>");
+            System.err.println("    -F                   force. Use with -dump:<dump-options> <pid> or -histo");
+            System.err.println("                         to force a heap dump or histogram when <pid> does not");
+            System.err.println("                         respond. The \"live\" suboption is not supported");
+            System.err.println("                         in this mode.");
+            System.err.println("    -h | -help           to print this help message");
+            System.err.println("    -J<flag>             to pass <flag> directly to the runtime system");
         } else {
-            System.out.println("    jmap -histo <pid>");
-            System.out.println("      (to connect to running process and print histogram of java object heap");
-            System.out.println("    jmap -dump:<dump-options> <pid>");
-            System.out.println("      (to connect to running process and dump java heap)");
-            System.out.println("");
-            System.out.println("    dump-options:");
-            System.out.println("      format=b     binary default");
-            System.out.println("      file=<file>  dump heap to <file>");
-            System.out.println("");
-            System.out.println("    Example:       jmap -dump:format=b,file=heap.bin <pid>");
+            System.err.println("    jmap -histo <pid>");
+            System.err.println("      (to connect to running process and print histogram of java object heap");
+            System.err.println("    jmap -dump:<dump-options> <pid>");
+            System.err.println("      (to connect to running process and dump java heap)");
+            System.err.println("");
+            System.err.println("    dump-options:");
+            System.err.println("      format=b     binary default");
+            System.err.println("      file=<file>  dump heap to <file>");
+            System.err.println("");
+            System.err.println("    Example:       jmap -dump:format=b,file=heap.bin <pid>");
         }
 
-        System.exit(1);
+        System.exit(exit);
     }
 }