8025638: jmap returns 0 instead of 1 when it fails

Re-factored some code handling return values and fails/errors during tool execution.

Reviewed-by: sla, kevinw
This commit is contained in:
Fredrik Arvidsson 2013-10-16 09:20:23 +02:00 committed by Fredrik Arvidsson
parent 232c4efbdb
commit ec5d05699e
18 changed files with 48 additions and 42 deletions

View file

@ -51,8 +51,7 @@ public class ClassLoaderStats extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
ClassLoaderStats cls = new ClassLoaderStats(); ClassLoaderStats cls = new ClassLoaderStats();
cls.start(args); cls.execute(args);
cls.stop();
} }
private static class ClassData { private static class ClassData {

View file

@ -54,8 +54,7 @@ public class FinalizerInfo extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
FinalizerInfo finfo = new FinalizerInfo(); FinalizerInfo finfo = new FinalizerInfo();
finfo.start(args); finfo.execute(args);
finfo.stop();
} }
public void run() { public void run() {

View file

@ -54,7 +54,6 @@ public class FlagDumper extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
FlagDumper fd = new FlagDumper(); FlagDumper fd = new FlagDumper();
fd.start(args); fd.execute(args);
fd.stop();
} }
} }

View file

@ -80,8 +80,7 @@ public class HeapDumper extends Tool {
} }
HeapDumper dumper = new HeapDumper(file); HeapDumper dumper = new HeapDumper(file);
dumper.start(args); dumper.execute(args);
dumper.stop();
} }
} }

View file

@ -46,8 +46,7 @@ public class HeapSummary extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
HeapSummary hs = new HeapSummary(); HeapSummary hs = new HeapSummary();
hs.start(args); hs.execute(args);
hs.stop();
} }
public void run() { public void run() {

View file

@ -134,8 +134,7 @@ public class JInfo extends Tool {
} }
JInfo jinfo = new JInfo(mode); JInfo jinfo = new JInfo(mode);
jinfo.start(args); jinfo.execute(args);
jinfo.stop();
} }
private void printVMFlags() { private void printVMFlags() {

View file

@ -136,7 +136,9 @@ public class JMap extends Tool {
mode = MODE_HEAP_GRAPH_GXL; mode = MODE_HEAP_GRAPH_GXL;
} else { } else {
System.err.println("unknown heap format:" + format); System.err.println("unknown heap format:" + format);
return;
// Exit with error status
System.exit(1);
} }
} else { } else {
copyArgs = false; copyArgs = false;
@ -153,8 +155,7 @@ public class JMap extends Tool {
} }
JMap jmap = new JMap(mode); JMap jmap = new JMap(mode);
jmap.start(args); jmap.execute(args);
jmap.stop();
} }
public boolean writeHeapHprofBin(String fileName) { public boolean writeHeapHprofBin(String fileName) {

View file

@ -64,7 +64,6 @@ public class JSnap extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
JSnap js = new JSnap(); JSnap js = new JSnap();
js.start(args); js.execute(args);
js.stop();
} }
} }

View file

@ -89,8 +89,7 @@ public class JStack extends Tool {
} }
JStack jstack = new JStack(mixedMode, concurrentLocks); JStack jstack = new JStack(mixedMode, concurrentLocks);
jstack.start(args); jstack.execute(args);
jstack.stop();
} }
private boolean mixedMode; private boolean mixedMode;

View file

@ -61,7 +61,6 @@ public class ObjectHistogram extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
ObjectHistogram oh = new ObjectHistogram(); ObjectHistogram oh = new ObjectHistogram();
oh.start(args); oh.execute(args);
oh.stop();
} }
} }

View file

@ -69,7 +69,6 @@ public class PMap extends Tool {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
PMap t = new PMap(); PMap t = new PMap();
t.start(args); t.execute(args);
t.stop();
} }
} }

View file

@ -182,8 +182,7 @@ public class PStack extends Tool {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
PStack t = new PStack(); PStack t = new PStack();
t.start(args); t.execute(args);
t.stop();
} }
// -- Internals only below this point // -- Internals only below this point

View file

@ -137,8 +137,7 @@ public class StackTrace extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
StackTrace st = new StackTrace(); StackTrace st = new StackTrace();
st.start(args); st.execute(args);
st.stop();
} }
private boolean verbose; private boolean verbose;

View file

@ -58,7 +58,6 @@ public class SysPropsDumper extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
SysPropsDumper pd = new SysPropsDumper(); SysPropsDumper pd = new SysPropsDumper();
pd.start(args); pd.execute(args);
pd.stop();
} }
} }

View file

@ -26,6 +26,7 @@ package sun.jvm.hotspot.tools;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Hashtable; import java.util.Hashtable;
import sun.jvm.hotspot.*; import sun.jvm.hotspot.*;
import sun.jvm.hotspot.runtime.*; import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.debugger.*; import sun.jvm.hotspot.debugger.*;
@ -105,26 +106,44 @@ public abstract class Tool implements Runnable {
public static void main(String[] args) { public static void main(String[] args) {
<derived class> obj = new <derived class>; <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) { if (agent != null) {
agent.detach(); agent.detach();
} }
} }
protected void start(String[] args) { private int start(String[] args) {
if ((args.length < 1) || (args.length > 2)) { if ((args.length < 1) || (args.length > 2)) {
usage(); usage();
return; return 1;
} }
// Attempt to handle -h or -help or some invalid flag // Attempt to handle -h or -help or some invalid flag
if (args[0].startsWith("-")) { if (args[0].startsWith("-h")) {
usage(); usage();
return 0;
} else if (args[0].startsWith("-")) {
usage();
return 1;
} }
PrintStream err = System.err; PrintStream err = System.err;
@ -154,6 +173,7 @@ public abstract class Tool implements Runnable {
default: default:
usage(); usage();
return 1;
} }
agent = new HotSpotAgent(); agent = new HotSpotAgent();
@ -191,15 +211,16 @@ public abstract class Tool implements Runnable {
break; break;
} }
if (e.getMessage() != null) { if (e.getMessage() != null) {
err.print(e.getMessage()); err.println(e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
err.println(); err.println();
return; return 1;
} }
err.println("Debugger attached successfully."); err.println("Debugger attached successfully.");
startInternal(); startInternal();
return 0;
} }
// When using an existing JVMDebugger. // When using an existing JVMDebugger.

View file

@ -177,7 +177,6 @@ public class ClassDump extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
ClassDump cd = new ClassDump(); ClassDump cd = new ClassDump();
cd.start(args); cd.execute(args);
cd.stop();
} }
} }

View file

@ -42,8 +42,7 @@ public class JSDB extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
JSDB jsdb = new JSDB(); JSDB jsdb = new JSDB();
jsdb.start(args); jsdb.execute(args);
jsdb.stop();
} }
public void run() { public void run() {

View file

@ -40,8 +40,7 @@ import sun.jvm.hotspot.utilities.soql.*;
public class SOQL extends Tool { public class SOQL extends Tool {
public static void main(String[] args) { public static void main(String[] args) {
SOQL soql = new SOQL(); SOQL soql = new SOQL();
soql.start(args); soql.execute(args);
soql.stop();
} }
public SOQL() { public SOQL() {