comparison agent/src/share/classes/sun/jvm/hotspot/tools/Tool.java @ 12932:7fe6ef09d242

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
author farvidsson
date Wed, 16 Oct 2013 09:20:23 +0200
parents 38ea2efa32a7
children 5280822ddfcd
comparison
equal deleted inserted replaced
12859:f509b8f4699b 12932:7fe6ef09d242
24 24
25 package sun.jvm.hotspot.tools; 25 package sun.jvm.hotspot.tools;
26 26
27 import java.io.PrintStream; 27 import java.io.PrintStream;
28 import java.util.Hashtable; 28 import java.util.Hashtable;
29
29 import sun.jvm.hotspot.*; 30 import sun.jvm.hotspot.*;
30 import sun.jvm.hotspot.runtime.*; 31 import sun.jvm.hotspot.runtime.*;
31 import sun.jvm.hotspot.debugger.*; 32 import sun.jvm.hotspot.debugger.*;
32 33
33 // generic command line or GUI tool. 34 // generic command line or GUI tool.
103 /* 104 /*
104 Derived class main should be of the following form: 105 Derived class main should be of the following form:
105 106
106 public static void main(String[] args) { 107 public static void main(String[] args) {
107 <derived class> obj = new <derived class>; 108 <derived class> obj = new <derived class>;
108 obj.start(args); 109 obj.execute(args);
109 } 110 }
110 111
111 */ 112 */
112 113
113 protected void stop() { 114 protected void execute(String[] args) {
115 int returnStatus = 1;
116
117 try {
118 returnStatus = start(args);
119 } finally {
120 stop();
121 }
122
123 // Exit with 0 or 1
124 System.exit(returnStatus);
125 }
126
127 public void stop() {
114 if (agent != null) { 128 if (agent != null) {
115 agent.detach(); 129 agent.detach();
116 } 130 }
117 } 131 }
118 132
119 protected void start(String[] args) { 133 private int start(String[] args) {
134
120 if ((args.length < 1) || (args.length > 2)) { 135 if ((args.length < 1) || (args.length > 2)) {
121 usage(); 136 usage();
122 return; 137 return 1;
123 } 138 }
124 139
125 // Attempt to handle -h or -help or some invalid flag 140 // Attempt to handle -h or -help or some invalid flag
126 if (args[0].startsWith("-")) { 141 if (args[0].startsWith("-h")) {
127 usage(); 142 usage();
143 return 0;
144 } else if (args[0].startsWith("-")) {
145 usage();
146 return 1;
128 } 147 }
129 148
130 PrintStream err = System.err; 149 PrintStream err = System.err;
131 150
132 int pid = 0; 151 int pid = 0;
152 debugeeType = DEBUGEE_CORE; 171 debugeeType = DEBUGEE_CORE;
153 break; 172 break;
154 173
155 default: 174 default:
156 usage(); 175 usage();
176 return 1;
157 } 177 }
158 178
159 agent = new HotSpotAgent(); 179 agent = new HotSpotAgent();
160 try { 180 try {
161 switch (debugeeType) { 181 switch (debugeeType) {
189 case DEBUGEE_REMOTE: 209 case DEBUGEE_REMOTE:
190 err.print("Error attaching to remote server: "); 210 err.print("Error attaching to remote server: ");
191 break; 211 break;
192 } 212 }
193 if (e.getMessage() != null) { 213 if (e.getMessage() != null) {
194 err.print(e.getMessage()); 214 err.println(e.getMessage());
195 e.printStackTrace(); 215 e.printStackTrace();
196 } 216 }
197 err.println(); 217 err.println();
198 return; 218 return 1;
199 } 219 }
200 220
201 err.println("Debugger attached successfully."); 221 err.println("Debugger attached successfully.");
202 startInternal(); 222 startInternal();
223 return 0;
203 } 224 }
204 225
205 // When using an existing JVMDebugger. 226 // When using an existing JVMDebugger.
206 public void start() { 227 public void start() {
207 228