8025638: jmap returns 0 instead of 1 when it fails.
authorfarvidsson
Wed, 16 Oct 2013 09:20:23 +0200
changeset 21065 b8281f54308b
parent 20686 1fa9a75192c3
child 21066 c0702325ba9d
child 21067 5288d9ba4f49
8025638: jmap returns 0 instead of 1 when it fails. Summary: Re-factored some code handling return values and fails/errors during tool execution. Reviewed-by: sla, kevinw Contributed-by: fredrik.arvidsson@oracle.com
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/ClassLoaderStats.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/FinalizerInfo.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/FlagDumper.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/HeapDumper.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/HeapSummary.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JInfo.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JSnap.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JStack.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/ObjectHistogram.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/PMap.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/PStack.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/StackTrace.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/SysPropsDumper.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/Tool.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassDump.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/soql/JSDB.java
hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/soql/SOQL.java
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/ClassLoaderStats.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/ClassLoaderStats.java	Wed Oct 16 09:20:23 2013 +0200
@@ -51,8 +51,7 @@
 
    public static void main(String[] args) {
       ClassLoaderStats cls = new ClassLoaderStats();
-      cls.start(args);
-      cls.stop();
+      cls.execute(args);
    }
 
    private static class ClassData {
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/FinalizerInfo.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/FinalizerInfo.java	Wed Oct 16 09:20:23 2013 +0200
@@ -54,8 +54,7 @@
 
     public static void main(String[] args) {
         FinalizerInfo finfo = new FinalizerInfo();
-        finfo.start(args);
-        finfo.stop();
+        finfo.execute(args);
     }
 
     public void run() {
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/FlagDumper.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/FlagDumper.java	Wed Oct 16 09:20:23 2013 +0200
@@ -54,7 +54,6 @@
 
    public static void main(String[] args) {
       FlagDumper fd = new FlagDumper();
-      fd.start(args);
-      fd.stop();
+      fd.execute(args);
    }
 }
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/HeapDumper.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/HeapDumper.java	Wed Oct 16 09:20:23 2013 +0200
@@ -80,8 +80,7 @@
         }
 
         HeapDumper dumper = new HeapDumper(file);
-        dumper.start(args);
-        dumper.stop();
+        dumper.execute(args);
     }
 
 }
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/HeapSummary.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/HeapSummary.java	Wed Oct 16 09:20:23 2013 +0200
@@ -46,8 +46,7 @@
 
    public static void main(String[] args) {
       HeapSummary hs = new HeapSummary();
-      hs.start(args);
-      hs.stop();
+      hs.execute(args);
    }
 
    public void run() {
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JInfo.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JInfo.java	Wed Oct 16 09:20:23 2013 +0200
@@ -134,8 +134,7 @@
         }
 
         JInfo jinfo = new JInfo(mode);
-        jinfo.start(args);
-        jinfo.stop();
+        jinfo.execute(args);
     }
 
     private void printVMFlags() {
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java	Wed Oct 16 09:20:23 2013 +0200
@@ -136,7 +136,9 @@
                         mode = MODE_HEAP_GRAPH_GXL;
                     } else {
                         System.err.println("unknown heap format:" + format);
-                        return;
+
+                        // Exit with error status
+                        System.exit(1);
                     }
                 } else {
                     copyArgs = false;
@@ -153,8 +155,7 @@
         }
 
         JMap jmap = new JMap(mode);
-        jmap.start(args);
-        jmap.stop();
+        jmap.execute(args);
     }
 
     public boolean writeHeapHprofBin(String fileName) {
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JSnap.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JSnap.java	Wed Oct 16 09:20:23 2013 +0200
@@ -64,7 +64,6 @@
 
     public static void main(String[] args) {
         JSnap js = new JSnap();
-        js.start(args);
-        js.stop();
+        js.execute(args);
     }
 }
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JStack.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JStack.java	Wed Oct 16 09:20:23 2013 +0200
@@ -89,8 +89,7 @@
         }
 
         JStack jstack = new JStack(mixedMode, concurrentLocks);
-        jstack.start(args);
-        jstack.stop();
+        jstack.execute(args);
     }
 
     private boolean mixedMode;
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/ObjectHistogram.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/ObjectHistogram.java	Wed Oct 16 09:20:23 2013 +0200
@@ -61,7 +61,6 @@
 
    public static void main(String[] args) {
       ObjectHistogram oh = new ObjectHistogram();
-      oh.start(args);
-      oh.stop();
+      oh.execute(args);
    }
 }
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/PMap.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/PMap.java	Wed Oct 16 09:20:23 2013 +0200
@@ -69,7 +69,6 @@
 
    public static void main(String[] args) throws Exception {
       PMap t = new PMap();
-      t.start(args);
-      t.stop();
+      t.execute(args);
    }
 }
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/PStack.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/PStack.java	Wed Oct 16 09:20:23 2013 +0200
@@ -182,8 +182,7 @@
 
    public static void main(String[] args) throws Exception {
       PStack t = new PStack();
-      t.start(args);
-      t.stop();
+      t.execute(args);
    }
 
    // -- Internals only below this point
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/StackTrace.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/StackTrace.java	Wed Oct 16 09:20:23 2013 +0200
@@ -137,8 +137,7 @@
 
    public static void main(String[] args) {
       StackTrace st = new StackTrace();
-      st.start(args);
-      st.stop();
+      st.execute(args);
    }
 
    private boolean verbose;
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/SysPropsDumper.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/SysPropsDumper.java	Wed Oct 16 09:20:23 2013 +0200
@@ -58,7 +58,6 @@
 
    public static void main(String[] args) {
       SysPropsDumper pd = new SysPropsDumper();
-      pd.start(args);
-      pd.stop();
+      pd.execute(args);
    }
 }
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/Tool.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/Tool.java	Wed Oct 16 09:20:23 2013 +0200
@@ -26,6 +26,7 @@
 
 import java.io.PrintStream;
 import java.util.Hashtable;
+
 import sun.jvm.hotspot.*;
 import sun.jvm.hotspot.runtime.*;
 import sun.jvm.hotspot.debugger.*;
@@ -105,26 +106,44 @@
 
       public static void main(String[] args) {
          <derived class> obj = new <derived class>;
-         obj.start(args);
+         obj.execute(args);
       }
 
    */
 
-   protected void stop() {
+   protected void execute(String[] args) {
+       int returnStatus = 1;
+
+       try {
+           returnStatus = start(args);
+       } finally {
+           stop();
+       }
+
+       // Exit with 0 or 1
+       System.exit(returnStatus);
+   }
+
+   public void stop() {
       if (agent != null) {
          agent.detach();
       }
    }
 
-   protected void start(String[] args) {
+   private int start(String[] args) {
+
       if ((args.length < 1) || (args.length > 2)) {
          usage();
-         return;
+         return 1;
       }
 
       // Attempt to handle -h or -help or some invalid flag
-      if (args[0].startsWith("-")) {
+      if (args[0].startsWith("-h")) {
           usage();
+          return 0;
+      } else if (args[0].startsWith("-")) {
+          usage();
+          return 1;
       }
 
       PrintStream err = System.err;
@@ -154,6 +173,7 @@
 
         default:
            usage();
+           return 1;
       }
 
       agent = new HotSpotAgent();
@@ -191,15 +211,16 @@
              break;
         }
         if (e.getMessage() != null) {
-          err.print(e.getMessage());
+          err.println(e.getMessage());
           e.printStackTrace();
         }
         err.println();
-        return;
+        return 1;
       }
 
       err.println("Debugger attached successfully.");
       startInternal();
+      return 0;
    }
 
    // When using an existing JVMDebugger.
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassDump.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassDump.java	Wed Oct 16 09:20:23 2013 +0200
@@ -177,7 +177,6 @@
     public static void main(String[] args) {
 
         ClassDump cd = new ClassDump();
-        cd.start(args);
-        cd.stop();
+        cd.execute(args);
     }
 }
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/soql/JSDB.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/soql/JSDB.java	Wed Oct 16 09:20:23 2013 +0200
@@ -42,8 +42,7 @@
 
     public static void main(String[] args) {
         JSDB jsdb = new JSDB();
-        jsdb.start(args);
-        jsdb.stop();
+        jsdb.execute(args);
     }
 
     public void run() {
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/soql/SOQL.java	Tue Oct 15 08:25:43 2013 -0700
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/soql/SOQL.java	Wed Oct 16 09:20:23 2013 +0200
@@ -40,8 +40,7 @@
 public class SOQL extends Tool {
    public static void main(String[] args) {
       SOQL soql = new SOQL();
-      soql.start(args);
-      soql.stop();
+      soql.execute(args);
    }
 
    public SOQL() {