changeset 8806:65f7b83ab477

8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent Reviewed-by: alanb, allwin, sspitsyn, mgronlun
author sla
date Thu, 14 Nov 2013 19:31:31 +0100
parents 83c768d6cb93
children f893901ba29c
files src/share/classes/sun/tools/jinfo/JInfo.java src/share/classes/sun/tools/jmap/JMap.java src/share/classes/sun/tools/jps/Jps.java src/share/classes/sun/tools/jstack/JStack.java
diffstat 4 files changed, 126 insertions(+), 106 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/tools/jinfo/JInfo.java	Thu Nov 14 12:17:37 2013 +0100
+++ b/src/share/classes/sun/tools/jinfo/JInfo.java	Thu Nov 14 19:31:31 2013 +0100
@@ -26,7 +26,6 @@
 package sun.tools.jinfo;
 
 import java.lang.reflect.Method;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -44,7 +43,7 @@
 
     public static void main(String[] args) throws Exception {
         if (args.length == 0) {
-            usage(); // no arguments
+            usage(1); // no arguments
         }
 
         boolean useSA = true;
@@ -56,14 +55,20 @@
                 // (<executable> and <code file>). So, total
                 // argument count including option has to 2 or 3.
                 if (args.length != 2 && args.length != 3) {
-                    usage();
+                    usage(1);
                 }
             } else if (arg1.equals("-flag")) {
                 // do not use SA, use attach-on-demand
                 useSA = false;
             } else {
                 // unknown option or -h or -help, print help
-                usage();
+                int exit;
+                if (arg1.equals("-help") || arg1.equals("-h")) {
+                    exit = 0;
+                } else {
+                    exit = 1;
+                }
+                usage(exit);
             }
         }
 
@@ -75,7 +80,13 @@
                 String option = args[1];
                 flag(pid, option);
             } else {
-                usage();
+                int exit;
+                if (arg1.equals("-help") || arg1.equals("-h")) {
+                    exit = 0;
+                } else {
+                    exit = 1;
+                }
+                usage(exit);
             }
         }
     }
@@ -86,7 +97,7 @@
         // Tool not available on this  platform.
         Class<?> c = loadClass(tool);
         if (c == null) {
-            usage();
+            usage(1);
         }
 
         // invoke the main method with the arguments
@@ -176,39 +187,39 @@
 
 
     // print usage message
-    private static void usage() {
+    private static void usage(int exit) {
 
         Class<?> c = loadClass("sun.jvm.hotspot.tools.JInfo");
         boolean usageSA = (c != null);
 
-        System.out.println("Usage:");
+        System.err.println("Usage:");
         if (usageSA) {
-            System.out.println("    jinfo [option] <pid>");
-            System.out.println("        (to connect to running process)");
-            System.out.println("    jinfo [option] <executable <core>");
-            System.out.println("        (to connect to a core file)");
-            System.out.println("    jinfo [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("    -flag <name>         to print the value of the named VM flag");
-            System.out.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
-            System.out.println("    -flag <name>=<value> to set the named VM flag to the given value");
-            System.out.println("    -flags               to print VM flags");
-            System.out.println("    -sysprops            to print Java system properties");
-            System.out.println("    <no option>          to print both of the above");
-            System.out.println("    -h | -help           to print this help message");
+            System.err.println("    jinfo [option] <pid>");
+            System.err.println("        (to connect to running process)");
+            System.err.println("    jinfo [option] <executable <core>");
+            System.err.println("        (to connect to a core file)");
+            System.err.println("    jinfo [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("    -flag <name>         to print the value of the named VM flag");
+            System.err.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
+            System.err.println("    -flag <name>=<value> to set the named VM flag to the given value");
+            System.err.println("    -flags               to print VM flags");
+            System.err.println("    -sysprops            to print Java system properties");
+            System.err.println("    <no option>          to print both of the above");
+            System.err.println("    -h | -help           to print this help message");
         } else {
-            System.out.println("    jinfo <option> <pid>");
-            System.out.println("       (to connect to a running process)");
-            System.out.println("");
-            System.out.println("where <option> is one of:");
-            System.out.println("    -flag <name>         to print the value of the named VM flag");
-            System.out.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
-            System.out.println("    -flag <name>=<value> to set the named VM flag to the given value");
-            System.out.println("    -h | -help           to print this help message");
+            System.err.println("    jinfo <option> <pid>");
+            System.err.println("       (to connect to a running process)");
+            System.err.println("");
+            System.err.println("where <option> is one of:");
+            System.err.println("    -flag <name>         to print the value of the named VM flag");
+            System.err.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
+            System.err.println("    -flag <name>=<value> to set the named VM flag to the given value");
+            System.err.println("    -h | -help           to print this help message");
         }
 
-        System.exit(1);
+        System.exit(exit);
     }
 }
--- a/src/share/classes/sun/tools/jmap/JMap.java	Thu Nov 14 12:17:37 2013 +0100
+++ b/src/share/classes/sun/tools/jmap/JMap.java	Thu Nov 14 19:31:31 2013 +0100
@@ -60,7 +60,7 @@
 
     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
@@ -77,11 +77,13 @@
             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;
             }
@@ -101,7 +103,7 @@
         // only one parameter (the process-id)
         int paramCount = args.length - optionCount;
         if (paramCount == 0 || paramCount > 2) {
-            usage();
+            usage(1);
         }
 
         if (optionCount == 0 || paramCount != 1) {
@@ -139,7 +141,7 @@
             } else if (option.startsWith(DUMP_OPTION_PREFIX)) {
                 dump(pid, option);
             } else {
-                usage();
+                usage(1);
             }
         }
     }
@@ -161,7 +163,9 @@
         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";
@@ -180,13 +184,13 @@
             }
         }
         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
@@ -225,7 +229,7 @@
         // 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
@@ -341,49 +345,49 @@
     }
 
     // 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);
     }
 }
--- a/src/share/classes/sun/tools/jps/Jps.java	Thu Nov 14 12:17:37 2013 +0100
+++ b/src/share/classes/sun/tools/jps/Jps.java	Thu Nov 14 19:31:31 2013 +0100
@@ -45,11 +45,11 @@
         } catch (IllegalArgumentException e) {
             System.err.println(e.getMessage());
             Arguments.printUsage(System.err);
-            return;
+            System.exit(1);
         }
 
         if (arguments.isHelp()) {
-            Arguments.printUsage(System.out);
+            Arguments.printUsage(System.err);
             System.exit(0);
         }
 
@@ -165,6 +165,7 @@
                     e.printStackTrace();
                 }
             }
+            System.exit(1);
         }
     }
 }
--- a/src/share/classes/sun/tools/jstack/JStack.java	Thu Nov 14 12:17:37 2013 +0100
+++ b/src/share/classes/sun/tools/jstack/JStack.java	Thu Nov 14 19:31:31 2013 +0100
@@ -42,7 +42,7 @@
 public class JStack {
     public static void main(String[] args) throws Exception {
         if (args.length == 0) {
-            usage(); // no arguments
+            usage(1); // no arguments
         }
 
         boolean useSA = false;
@@ -56,16 +56,20 @@
             if (!arg.startsWith("-")) {
                 break;
             }
-            if (arg.equals("-F")) {
+            if (arg.equals("-help") || arg.equals("-h")) {
+                usage(0);
+            }
+            else if (arg.equals("-F")) {
                 useSA = true;
-            } else {
+            }
+            else {
                 if (arg.equals("-m")) {
                     mixed = true;
                 } else {
                     if (arg.equals("-l")) {
                        locks = true;
                     } else {
-                        usage();
+                        usage(1);
                     }
                 }
             }
@@ -81,7 +85,7 @@
         // we assume core file and executable so we use SA.
         int paramCount = args.length - optionCount;
         if (paramCount == 0 || paramCount > 2) {
-            usage();
+            usage(1);
         }
         if (paramCount == 2) {
             useSA = true;
@@ -118,7 +122,7 @@
     private static void runJStackTool(boolean mixed, boolean locks, String args[]) throws Exception {
         Class<?> cl = loadSAClass();
         if (cl == null) {
-            usage();            // SA not available
+            usage(1);            // SA not available
         }
 
         // JStack tool also takes -m and -l arguments
@@ -199,31 +203,31 @@
     }
 
     // print usage message
-    private static void usage() {
-        System.out.println("Usage:");
-        System.out.println("    jstack [-l] <pid>");
-        System.out.println("        (to connect to running process)");
+    private static void usage(int exit) {
+        System.err.println("Usage:");
+        System.err.println("    jstack [-l] <pid>");
+        System.err.println("        (to connect to running process)");
 
         if (loadSAClass() != null) {
-            System.out.println("    jstack -F [-m] [-l] <pid>");
-            System.out.println("        (to connect to a hung process)");
-            System.out.println("    jstack [-m] [-l] <executable> <core>");
-            System.out.println("        (to connect to a core file)");
-            System.out.println("    jstack [-m] [-l] [server_id@]<remote server IP or hostname>");
-            System.out.println("        (to connect to a remote debug server)");
+            System.err.println("    jstack -F [-m] [-l] <pid>");
+            System.err.println("        (to connect to a hung process)");
+            System.err.println("    jstack [-m] [-l] <executable> <core>");
+            System.err.println("        (to connect to a core file)");
+            System.err.println("    jstack [-m] [-l] [server_id@]<remote server IP or hostname>");
+            System.err.println("        (to connect to a remote debug server)");
         }
 
-        System.out.println("");
-        System.out.println("Options:");
+        System.err.println("");
+        System.err.println("Options:");
 
         if (loadSAClass() != null) {
-            System.out.println("    -F  to force a thread dump. Use when jstack <pid> does not respond" +
+            System.err.println("    -F  to force a thread dump. Use when jstack <pid> does not respond" +
                 " (process is hung)");
-            System.out.println("    -m  to print both java and native frames (mixed mode)");
+            System.err.println("    -m  to print both java and native frames (mixed mode)");
         }
 
-        System.out.println("    -l  long listing. Prints additional information about locks");
-        System.out.println("    -h or -help to print this help message");
-        System.exit(1);
+        System.err.println("    -l  long listing. Prints additional information about locks");
+        System.err.println("    -h or -help to print this help message");
+        System.exit(exit);
     }
 }