author | hseigel |
Wed, 20 Feb 2019 13:21:36 -0500 | |
changeset 53853 | 7ca9e625d6b2 |
parent 53782 | 37c12515c30f |
child 58074 | 6eca527d3689 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53782
37c12515c30f
8218941: jdb should support a dbgtrace command that acts the same as the dbgtrace command line option
cjplummer
parents:
52366
diff
changeset
|
2 |
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
26 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
27 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
28 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
29 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
30 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
31 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
32 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
33 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
34 |
|
2 | 35 |
package com.sun.tools.example.debug.tty; |
36 |
||
37 |
import com.sun.jdi.*; |
|
38 |
import com.sun.jdi.event.*; |
|
39 |
import com.sun.jdi.request.*; |
|
40 |
import com.sun.jdi.connect.*; |
|
41 |
||
42 |
import java.util.*; |
|
43 |
import java.io.*; |
|
44 |
||
45 |
public class TTY implements EventNotifier { |
|
46 |
EventHandler handler = null; |
|
47 |
||
48 |
/** |
|
49 |
* List of Strings to execute at each stop. |
|
50 |
*/ |
|
51 |
private List<String> monitorCommands = new ArrayList<String>(); |
|
52 |
private int monitorCount = 0; |
|
53 |
||
54 |
/** |
|
55 |
* The name of this tool. |
|
56 |
*/ |
|
57 |
private static final String progname = "jdb"; |
|
58 |
||
38348
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
59 |
private volatile boolean shuttingDown = false; |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
60 |
|
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
61 |
public void setShuttingDown(boolean s) { |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
62 |
shuttingDown = s; |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
63 |
} |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
64 |
|
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
65 |
public boolean isShuttingDown() { |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
66 |
return shuttingDown; |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
67 |
} |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
68 |
|
9520 | 69 |
@Override |
2 | 70 |
public void vmStartEvent(VMStartEvent se) { |
71 |
Thread.yield(); // fetch output |
|
72 |
MessageOutput.lnprint("VM Started:"); |
|
73 |
} |
|
74 |
||
9520 | 75 |
@Override |
2 | 76 |
public void vmDeathEvent(VMDeathEvent e) { |
77 |
} |
|
78 |
||
9520 | 79 |
@Override |
2 | 80 |
public void vmDisconnectEvent(VMDisconnectEvent e) { |
81 |
} |
|
82 |
||
9520 | 83 |
@Override |
2 | 84 |
public void threadStartEvent(ThreadStartEvent e) { |
85 |
} |
|
86 |
||
9520 | 87 |
@Override |
2 | 88 |
public void threadDeathEvent(ThreadDeathEvent e) { |
89 |
} |
|
90 |
||
9520 | 91 |
@Override |
2 | 92 |
public void classPrepareEvent(ClassPrepareEvent e) { |
93 |
} |
|
94 |
||
9520 | 95 |
@Override |
2 | 96 |
public void classUnloadEvent(ClassUnloadEvent e) { |
97 |
} |
|
98 |
||
9520 | 99 |
@Override |
2 | 100 |
public void breakpointEvent(BreakpointEvent be) { |
101 |
Thread.yield(); // fetch output |
|
102 |
MessageOutput.lnprint("Breakpoint hit:"); |
|
52228
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
103 |
// Print breakpoint location and prompt if suspend policy is |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
104 |
// SUSPEND_NONE or SUSPEND_EVENT_THREAD. In case of SUSPEND_ALL |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
105 |
// policy this is handled by vmInterrupted() method. |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
106 |
int suspendPolicy = be.request().suspendPolicy(); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
107 |
switch (suspendPolicy) { |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
108 |
case EventRequest.SUSPEND_EVENT_THREAD: |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
109 |
case EventRequest.SUSPEND_NONE: |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
110 |
printBreakpointLocation(be); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
111 |
MessageOutput.printPrompt(); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
112 |
break; |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
113 |
} |
2 | 114 |
} |
115 |
||
9520 | 116 |
@Override |
2 | 117 |
public void fieldWatchEvent(WatchpointEvent fwe) { |
118 |
Field field = fwe.field(); |
|
119 |
ObjectReference obj = fwe.object(); |
|
120 |
Thread.yield(); // fetch output |
|
121 |
||
122 |
if (fwe instanceof ModificationWatchpointEvent) { |
|
123 |
MessageOutput.lnprint("Field access encountered before after", |
|
124 |
new Object [] {field, |
|
125 |
fwe.valueCurrent(), |
|
126 |
((ModificationWatchpointEvent)fwe).valueToBe()}); |
|
127 |
} else { |
|
128 |
MessageOutput.lnprint("Field access encountered", field.toString()); |
|
129 |
} |
|
130 |
} |
|
131 |
||
9520 | 132 |
@Override |
2 | 133 |
public void stepEvent(StepEvent se) { |
134 |
Thread.yield(); // fetch output |
|
135 |
MessageOutput.lnprint("Step completed:"); |
|
136 |
} |
|
137 |
||
9520 | 138 |
@Override |
2 | 139 |
public void exceptionEvent(ExceptionEvent ee) { |
140 |
Thread.yield(); // fetch output |
|
141 |
Location catchLocation = ee.catchLocation(); |
|
142 |
if (catchLocation == null) { |
|
143 |
MessageOutput.lnprint("Exception occurred uncaught", |
|
144 |
ee.exception().referenceType().name()); |
|
145 |
} else { |
|
146 |
MessageOutput.lnprint("Exception occurred caught", |
|
147 |
new Object [] {ee.exception().referenceType().name(), |
|
148 |
Commands.locationString(catchLocation)}); |
|
149 |
} |
|
150 |
} |
|
151 |
||
9520 | 152 |
@Override |
2 | 153 |
public void methodEntryEvent(MethodEntryEvent me) { |
154 |
Thread.yield(); // fetch output |
|
155 |
/* |
|
156 |
* These can be very numerous, so be as efficient as possible. |
|
157 |
* If we are stopping here, then we will see the normal location |
|
158 |
* info printed. |
|
159 |
*/ |
|
160 |
if (me.request().suspendPolicy() != EventRequest.SUSPEND_NONE) { |
|
161 |
// We are stopping; the name will be shown by the normal mechanism |
|
162 |
MessageOutput.lnprint("Method entered:"); |
|
163 |
} else { |
|
164 |
// We aren't stopping, show the name |
|
165 |
MessageOutput.print("Method entered:"); |
|
166 |
printLocationOfEvent(me); |
|
167 |
} |
|
168 |
} |
|
169 |
||
9520 | 170 |
@Override |
2 | 171 |
public boolean methodExitEvent(MethodExitEvent me) { |
172 |
Thread.yield(); // fetch output |
|
173 |
/* |
|
174 |
* These can be very numerous, so be as efficient as possible. |
|
175 |
*/ |
|
176 |
Method mmm = Env.atExitMethod(); |
|
177 |
Method meMethod = me.method(); |
|
178 |
||
179 |
if (mmm == null || mmm.equals(meMethod)) { |
|
180 |
// Either we are not tracing a specific method, or we are |
|
181 |
// and we are exitting that method. |
|
182 |
||
183 |
if (me.request().suspendPolicy() != EventRequest.SUSPEND_NONE) { |
|
184 |
// We will be stopping here, so do a newline |
|
185 |
MessageOutput.println(); |
|
186 |
} |
|
187 |
if (Env.vm().canGetMethodReturnValues()) { |
|
188 |
MessageOutput.print("Method exitedValue:", me.returnValue() + ""); |
|
189 |
} else { |
|
190 |
MessageOutput.print("Method exited:"); |
|
191 |
} |
|
192 |
||
193 |
if (me.request().suspendPolicy() == EventRequest.SUSPEND_NONE) { |
|
194 |
// We won't be stopping here, so show the method name |
|
195 |
printLocationOfEvent(me); |
|
196 |
||
197 |
} |
|
198 |
||
199 |
// In case we want to have a one shot trace exit some day, this |
|
200 |
// code disables the request so we don't hit it again. |
|
201 |
if (false) { |
|
202 |
// This is a one shot deal; we don't want to stop |
|
203 |
// here the next time. |
|
204 |
Env.setAtExitMethod(null); |
|
205 |
EventRequestManager erm = Env.vm().eventRequestManager(); |
|
51 | 206 |
for (EventRequest eReq : erm.methodExitRequests()) { |
2 | 207 |
if (eReq.equals(me.request())) { |
208 |
eReq.disable(); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
return true; |
|
213 |
} |
|
214 |
||
215 |
// We are tracing a specific method, and this isn't it. Keep going. |
|
216 |
return false; |
|
217 |
} |
|
218 |
||
9520 | 219 |
@Override |
2 | 220 |
public void vmInterrupted() { |
221 |
Thread.yield(); // fetch output |
|
222 |
printCurrentLocation(); |
|
51 | 223 |
for (String cmd : monitorCommands) { |
224 |
StringTokenizer t = new StringTokenizer(cmd); |
|
2 | 225 |
t.nextToken(); // get rid of monitor number |
226 |
executeCommand(t); |
|
227 |
} |
|
228 |
MessageOutput.printPrompt(); |
|
229 |
} |
|
230 |
||
9520 | 231 |
@Override |
2 | 232 |
public void receivedEvent(Event event) { |
233 |
} |
|
234 |
||
235 |
private void printBaseLocation(String threadName, Location loc) { |
|
236 |
MessageOutput.println("location", |
|
237 |
new Object [] {threadName, |
|
238 |
Commands.locationString(loc)}); |
|
239 |
} |
|
240 |
||
52228
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
241 |
private void printBreakpointLocation(BreakpointEvent be) { |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
242 |
printLocationWithSourceLine(be.thread().name(), be.location()); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
243 |
} |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
244 |
|
2 | 245 |
private void printCurrentLocation() { |
246 |
ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo(); |
|
247 |
StackFrame frame; |
|
248 |
try { |
|
249 |
frame = threadInfo.getCurrentFrame(); |
|
250 |
} catch (IncompatibleThreadStateException exc) { |
|
251 |
MessageOutput.println("<location unavailable>"); |
|
252 |
return; |
|
253 |
} |
|
254 |
if (frame == null) { |
|
255 |
MessageOutput.println("No frames on the current call stack"); |
|
256 |
} else { |
|
52228
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
257 |
printLocationWithSourceLine(threadInfo.getThread().name(), frame.location()); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
258 |
} |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
259 |
MessageOutput.println(); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
260 |
} |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
261 |
|
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
262 |
private void printLocationWithSourceLine(String threadName, Location loc) { |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
263 |
printBaseLocation(threadName, loc); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
264 |
// Output the current source line, if possible |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
265 |
if (loc.lineNumber() != -1) { |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
266 |
String line; |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
267 |
try { |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
268 |
line = Env.sourceLine(loc, loc.lineNumber()); |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
269 |
} catch (java.io.IOException e) { |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
270 |
line = null; |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
271 |
} |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
272 |
if (line != null) { |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
273 |
MessageOutput.println("source line number and line", |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
274 |
new Object [] {loc.lineNumber(), |
0ca91992dd8e
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
dtitov
parents:
52006
diff
changeset
|
275 |
line}); |
2 | 276 |
} |
277 |
} |
|
278 |
} |
|
279 |
||
280 |
private void printLocationOfEvent(LocatableEvent theEvent) { |
|
281 |
printBaseLocation(theEvent.thread().name(), theEvent.location()); |
|
282 |
} |
|
283 |
||
284 |
void help() { |
|
285 |
MessageOutput.println("zz help text"); |
|
286 |
} |
|
287 |
||
288 |
private static final String[][] commandList = { |
|
289 |
/* |
|
290 |
* NOTE: this list must be kept sorted in ascending ASCII |
|
291 |
* order by element [0]. Ref: isCommand() below. |
|
292 |
* |
|
293 |
*Command OK when OK when |
|
294 |
* name disconnected? readonly? |
|
295 |
*------------------------------------ |
|
296 |
*/ |
|
297 |
{"!!", "n", "y"}, |
|
298 |
{"?", "y", "y"}, |
|
299 |
{"bytecodes", "n", "y"}, |
|
300 |
{"catch", "y", "n"}, |
|
301 |
{"class", "n", "y"}, |
|
302 |
{"classes", "n", "y"}, |
|
303 |
{"classpath", "n", "y"}, |
|
304 |
{"clear", "y", "n"}, |
|
305 |
{"connectors", "y", "y"}, |
|
306 |
{"cont", "n", "n"}, |
|
53782
37c12515c30f
8218941: jdb should support a dbgtrace command that acts the same as the dbgtrace command line option
cjplummer
parents:
52366
diff
changeset
|
307 |
{"dbgtrace", "y", "y"}, |
2 | 308 |
{"disablegc", "n", "n"}, |
309 |
{"down", "n", "y"}, |
|
310 |
{"dump", "n", "y"}, |
|
311 |
{"enablegc", "n", "n"}, |
|
312 |
{"eval", "n", "y"}, |
|
313 |
{"exclude", "y", "n"}, |
|
314 |
{"exit", "y", "y"}, |
|
315 |
{"extension", "n", "y"}, |
|
316 |
{"fields", "n", "y"}, |
|
317 |
{"gc", "n", "n"}, |
|
318 |
{"help", "y", "y"}, |
|
319 |
{"ignore", "y", "n"}, |
|
320 |
{"interrupt", "n", "n"}, |
|
321 |
{"kill", "n", "n"}, |
|
322 |
{"lines", "n", "y"}, |
|
323 |
{"list", "n", "y"}, |
|
324 |
{"load", "n", "y"}, |
|
325 |
{"locals", "n", "y"}, |
|
326 |
{"lock", "n", "n"}, |
|
327 |
{"memory", "n", "y"}, |
|
328 |
{"methods", "n", "y"}, |
|
329 |
{"monitor", "n", "n"}, |
|
330 |
{"next", "n", "n"}, |
|
331 |
{"pop", "n", "n"}, |
|
332 |
{"print", "n", "y"}, |
|
333 |
{"quit", "y", "y"}, |
|
334 |
{"read", "y", "y"}, |
|
335 |
{"redefine", "n", "n"}, |
|
336 |
{"reenter", "n", "n"}, |
|
337 |
{"resume", "n", "n"}, |
|
338 |
{"run", "y", "n"}, |
|
339 |
{"save", "n", "n"}, |
|
340 |
{"set", "n", "n"}, |
|
341 |
{"sourcepath", "y", "y"}, |
|
342 |
{"step", "n", "n"}, |
|
343 |
{"stepi", "n", "n"}, |
|
344 |
{"stop", "y", "n"}, |
|
345 |
{"suspend", "n", "n"}, |
|
346 |
{"thread", "n", "y"}, |
|
347 |
{"threadgroup", "n", "y"}, |
|
348 |
{"threadgroups", "n", "y"}, |
|
349 |
{"threadlocks", "n", "y"}, |
|
350 |
{"threads", "n", "y"}, |
|
351 |
{"trace", "n", "n"}, |
|
352 |
{"unmonitor", "n", "n"}, |
|
353 |
{"untrace", "n", "n"}, |
|
354 |
{"unwatch", "y", "n"}, |
|
355 |
{"up", "n", "y"}, |
|
356 |
{"use", "y", "y"}, |
|
357 |
{"version", "y", "y"}, |
|
358 |
{"watch", "y", "n"}, |
|
359 |
{"where", "n", "y"}, |
|
360 |
{"wherei", "n", "y"}, |
|
361 |
}; |
|
362 |
||
363 |
/* |
|
364 |
* Look up the command string in commandList. |
|
365 |
* If found, return the index. |
|
366 |
* If not found, return index < 0 |
|
367 |
*/ |
|
368 |
private int isCommand(String key) { |
|
369 |
//Reference: binarySearch() in java/util/Arrays.java |
|
370 |
// Adapted for use with String[][0]. |
|
371 |
int low = 0; |
|
372 |
int high = commandList.length - 1; |
|
373 |
while (low <= high) { |
|
374 |
int mid = (low + high) >>> 1; |
|
375 |
String midVal = commandList[mid][0]; |
|
376 |
int compare = midVal.compareTo(key); |
|
9520 | 377 |
if (compare < 0) { |
2 | 378 |
low = mid + 1; |
9520 | 379 |
} else if (compare > 0) { |
2 | 380 |
high = mid - 1; |
9520 | 381 |
} |
382 |
else { |
|
2 | 383 |
return mid; // key found |
384 |
} |
|
9520 | 385 |
} |
2 | 386 |
return -(low + 1); // key not found. |
387 |
}; |
|
388 |
||
389 |
/* |
|
390 |
* Return true if the command is OK when disconnected. |
|
391 |
*/ |
|
392 |
private boolean isDisconnectCmd(int ii) { |
|
9520 | 393 |
if (ii < 0 || ii >= commandList.length) { |
394 |
return false; |
|
395 |
} |
|
2 | 396 |
return (commandList[ii][1].equals("y")); |
397 |
} |
|
398 |
||
399 |
/* |
|
400 |
* Return true if the command is OK when readonly. |
|
401 |
*/ |
|
402 |
private boolean isReadOnlyCmd(int ii) { |
|
9520 | 403 |
if (ii < 0 || ii >= commandList.length) { |
404 |
return false; |
|
405 |
} |
|
2 | 406 |
return (commandList[ii][2].equals("y")); |
407 |
}; |
|
408 |
||
409 |
||
410 |
void executeCommand(StringTokenizer t) { |
|
411 |
String cmd = t.nextToken().toLowerCase(); |
|
412 |
// Normally, prompt for the next command after this one is done |
|
413 |
boolean showPrompt = true; |
|
414 |
||
415 |
||
416 |
/* |
|
417 |
* Anything starting with # is discarded as a no-op or 'comment'. |
|
418 |
*/ |
|
419 |
if (!cmd.startsWith("#")) { |
|
420 |
/* |
|
421 |
* Next check for an integer repetition prefix. If found, |
|
422 |
* recursively execute cmd that number of times. |
|
423 |
*/ |
|
424 |
if (Character.isDigit(cmd.charAt(0)) && t.hasMoreTokens()) { |
|
425 |
try { |
|
426 |
int repeat = Integer.parseInt(cmd); |
|
427 |
String subcom = t.nextToken(""); |
|
428 |
while (repeat-- > 0) { |
|
429 |
executeCommand(new StringTokenizer(subcom)); |
|
430 |
showPrompt = false; // Bypass the printPrompt() below. |
|
431 |
} |
|
432 |
} catch (NumberFormatException exc) { |
|
433 |
MessageOutput.println("Unrecognized command. Try help...", cmd); |
|
434 |
} |
|
435 |
} else { |
|
436 |
int commandNumber = isCommand(cmd); |
|
437 |
/* |
|
438 |
* Check for an unknown command |
|
439 |
*/ |
|
440 |
if (commandNumber < 0) { |
|
441 |
MessageOutput.println("Unrecognized command. Try help...", cmd); |
|
442 |
} else if (!Env.connection().isOpen() && !isDisconnectCmd(commandNumber)) { |
|
443 |
MessageOutput.println("Command not valid until the VM is started with the run command", |
|
444 |
cmd); |
|
445 |
} else if (Env.connection().isOpen() && !Env.vm().canBeModified() && |
|
446 |
!isReadOnlyCmd(commandNumber)) { |
|
447 |
MessageOutput.println("Command is not supported on a read-only VM connection", |
|
448 |
cmd); |
|
449 |
} else { |
|
450 |
||
451 |
Commands evaluator = new Commands(); |
|
452 |
try { |
|
453 |
if (cmd.equals("print")) { |
|
454 |
evaluator.commandPrint(t, false); |
|
455 |
showPrompt = false; // asynchronous command |
|
456 |
} else if (cmd.equals("eval")) { |
|
457 |
evaluator.commandPrint(t, false); |
|
458 |
showPrompt = false; // asynchronous command |
|
459 |
} else if (cmd.equals("set")) { |
|
460 |
evaluator.commandSet(t); |
|
461 |
showPrompt = false; // asynchronous command |
|
462 |
} else if (cmd.equals("dump")) { |
|
463 |
evaluator.commandPrint(t, true); |
|
464 |
showPrompt = false; // asynchronous command |
|
465 |
} else if (cmd.equals("locals")) { |
|
466 |
evaluator.commandLocals(); |
|
467 |
} else if (cmd.equals("classes")) { |
|
468 |
evaluator.commandClasses(); |
|
469 |
} else if (cmd.equals("class")) { |
|
470 |
evaluator.commandClass(t); |
|
471 |
} else if (cmd.equals("connectors")) { |
|
472 |
evaluator.commandConnectors(Bootstrap.virtualMachineManager()); |
|
473 |
} else if (cmd.equals("methods")) { |
|
474 |
evaluator.commandMethods(t); |
|
475 |
} else if (cmd.equals("fields")) { |
|
476 |
evaluator.commandFields(t); |
|
477 |
} else if (cmd.equals("threads")) { |
|
478 |
evaluator.commandThreads(t); |
|
479 |
} else if (cmd.equals("thread")) { |
|
480 |
evaluator.commandThread(t); |
|
481 |
} else if (cmd.equals("suspend")) { |
|
482 |
evaluator.commandSuspend(t); |
|
483 |
} else if (cmd.equals("resume")) { |
|
484 |
evaluator.commandResume(t); |
|
485 |
} else if (cmd.equals("cont")) { |
|
52006
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
486 |
MessageOutput.printPrompt(true); |
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
487 |
showPrompt = false; |
2 | 488 |
evaluator.commandCont(); |
489 |
} else if (cmd.equals("threadgroups")) { |
|
490 |
evaluator.commandThreadGroups(); |
|
491 |
} else if (cmd.equals("threadgroup")) { |
|
492 |
evaluator.commandThreadGroup(t); |
|
493 |
} else if (cmd.equals("catch")) { |
|
494 |
evaluator.commandCatchException(t); |
|
495 |
} else if (cmd.equals("ignore")) { |
|
496 |
evaluator.commandIgnoreException(t); |
|
497 |
} else if (cmd.equals("step")) { |
|
52006
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
498 |
MessageOutput.printPrompt(true); |
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
499 |
showPrompt = false; |
2 | 500 |
evaluator.commandStep(t); |
501 |
} else if (cmd.equals("stepi")) { |
|
52006
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
502 |
MessageOutput.printPrompt(true); |
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
503 |
showPrompt = false; |
2 | 504 |
evaluator.commandStepi(); |
505 |
} else if (cmd.equals("next")) { |
|
52006
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
506 |
MessageOutput.printPrompt(true); |
82dc590fa586
8169718: nsk/jdb/locals/locals002: ERROR: Cannot find boolVar with expected value: false
gadams
parents:
48543
diff
changeset
|
507 |
showPrompt = false; |
2 | 508 |
evaluator.commandNext(); |
509 |
} else if (cmd.equals("kill")) { |
|
52366
8f543813b402
8211013: [TESTBUG] nsk/jdb/kill/kill002 wait for message and prompt
gadams
parents:
52228
diff
changeset
|
510 |
showPrompt = false; // asynchronous command |
2 | 511 |
evaluator.commandKill(t); |
512 |
} else if (cmd.equals("interrupt")) { |
|
513 |
evaluator.commandInterrupt(t); |
|
514 |
} else if (cmd.equals("trace")) { |
|
515 |
evaluator.commandTrace(t); |
|
516 |
} else if (cmd.equals("untrace")) { |
|
517 |
evaluator.commandUntrace(t); |
|
518 |
} else if (cmd.equals("where")) { |
|
519 |
evaluator.commandWhere(t, false); |
|
520 |
} else if (cmd.equals("wherei")) { |
|
521 |
evaluator.commandWhere(t, true); |
|
522 |
} else if (cmd.equals("up")) { |
|
523 |
evaluator.commandUp(t); |
|
524 |
} else if (cmd.equals("down")) { |
|
525 |
evaluator.commandDown(t); |
|
526 |
} else if (cmd.equals("load")) { |
|
527 |
evaluator.commandLoad(t); |
|
528 |
} else if (cmd.equals("run")) { |
|
529 |
evaluator.commandRun(t); |
|
530 |
/* |
|
531 |
* Fire up an event handler, if the connection was just |
|
532 |
* opened. Since this was done from the run command |
|
533 |
* we don't stop the VM on its VM start event (so |
|
534 |
* arg 2 is false). |
|
535 |
*/ |
|
536 |
if ((handler == null) && Env.connection().isOpen()) { |
|
537 |
handler = new EventHandler(this, false); |
|
538 |
} |
|
539 |
} else if (cmd.equals("memory")) { |
|
540 |
evaluator.commandMemory(); |
|
541 |
} else if (cmd.equals("gc")) { |
|
542 |
evaluator.commandGC(); |
|
543 |
} else if (cmd.equals("stop")) { |
|
544 |
evaluator.commandStop(t); |
|
545 |
} else if (cmd.equals("clear")) { |
|
546 |
evaluator.commandClear(t); |
|
547 |
} else if (cmd.equals("watch")) { |
|
548 |
evaluator.commandWatch(t); |
|
549 |
} else if (cmd.equals("unwatch")) { |
|
550 |
evaluator.commandUnwatch(t); |
|
551 |
} else if (cmd.equals("list")) { |
|
552 |
evaluator.commandList(t); |
|
553 |
} else if (cmd.equals("lines")) { // Undocumented command: useful for testing. |
|
554 |
evaluator.commandLines(t); |
|
555 |
} else if (cmd.equals("classpath")) { |
|
556 |
evaluator.commandClasspath(t); |
|
557 |
} else if (cmd.equals("use") || cmd.equals("sourcepath")) { |
|
558 |
evaluator.commandUse(t); |
|
559 |
} else if (cmd.equals("monitor")) { |
|
560 |
monitorCommand(t); |
|
561 |
} else if (cmd.equals("unmonitor")) { |
|
562 |
unmonitorCommand(t); |
|
563 |
} else if (cmd.equals("lock")) { |
|
564 |
evaluator.commandLock(t); |
|
565 |
showPrompt = false; // asynchronous command |
|
566 |
} else if (cmd.equals("threadlocks")) { |
|
567 |
evaluator.commandThreadlocks(t); |
|
568 |
} else if (cmd.equals("disablegc")) { |
|
569 |
evaluator.commandDisableGC(t); |
|
570 |
showPrompt = false; // asynchronous command |
|
571 |
} else if (cmd.equals("enablegc")) { |
|
572 |
evaluator.commandEnableGC(t); |
|
573 |
showPrompt = false; // asynchronous command |
|
574 |
} else if (cmd.equals("save")) { // Undocumented command: useful for testing. |
|
575 |
evaluator.commandSave(t); |
|
576 |
showPrompt = false; // asynchronous command |
|
577 |
} else if (cmd.equals("bytecodes")) { // Undocumented command: useful for testing. |
|
578 |
evaluator.commandBytecodes(t); |
|
579 |
} else if (cmd.equals("redefine")) { |
|
580 |
evaluator.commandRedefine(t); |
|
581 |
} else if (cmd.equals("pop")) { |
|
582 |
evaluator.commandPopFrames(t, false); |
|
583 |
} else if (cmd.equals("reenter")) { |
|
584 |
evaluator.commandPopFrames(t, true); |
|
585 |
} else if (cmd.equals("extension")) { |
|
586 |
evaluator.commandExtension(t); |
|
587 |
} else if (cmd.equals("exclude")) { |
|
588 |
evaluator.commandExclude(t); |
|
589 |
} else if (cmd.equals("read")) { |
|
590 |
readCommand(t); |
|
53782
37c12515c30f
8218941: jdb should support a dbgtrace command that acts the same as the dbgtrace command line option
cjplummer
parents:
52366
diff
changeset
|
591 |
} else if (cmd.equals("dbgtrace")) { |
37c12515c30f
8218941: jdb should support a dbgtrace command that acts the same as the dbgtrace command line option
cjplummer
parents:
52366
diff
changeset
|
592 |
evaluator.commandDbgTrace(t); |
2 | 593 |
} else if (cmd.equals("help") || cmd.equals("?")) { |
594 |
help(); |
|
595 |
} else if (cmd.equals("version")) { |
|
596 |
evaluator.commandVersion(progname, |
|
597 |
Bootstrap.virtualMachineManager()); |
|
598 |
} else if (cmd.equals("quit") || cmd.equals("exit")) { |
|
599 |
if (handler != null) { |
|
600 |
handler.shutdown(); |
|
601 |
} |
|
602 |
Env.shutdown(); |
|
603 |
} else { |
|
604 |
MessageOutput.println("Unrecognized command. Try help...", cmd); |
|
605 |
} |
|
606 |
} catch (VMCannotBeModifiedException rovm) { |
|
607 |
MessageOutput.println("Command is not supported on a read-only VM connection", cmd); |
|
608 |
} catch (UnsupportedOperationException uoe) { |
|
609 |
MessageOutput.println("Command is not supported on the target VM", cmd); |
|
610 |
} catch (VMNotConnectedException vmnse) { |
|
611 |
MessageOutput.println("Command not valid until the VM is started with the run command", |
|
612 |
cmd); |
|
613 |
} catch (Exception e) { |
|
614 |
MessageOutput.printException("Internal exception:", e); |
|
615 |
} |
|
616 |
} |
|
617 |
} |
|
618 |
} |
|
619 |
if (showPrompt) { |
|
620 |
MessageOutput.printPrompt(); |
|
621 |
} |
|
622 |
} |
|
623 |
||
624 |
/* |
|
625 |
* Maintain a list of commands to execute each time the VM is suspended. |
|
626 |
*/ |
|
627 |
void monitorCommand(StringTokenizer t) { |
|
628 |
if (t.hasMoreTokens()) { |
|
629 |
++monitorCount; |
|
630 |
monitorCommands.add(monitorCount + ": " + t.nextToken("")); |
|
631 |
} else { |
|
51 | 632 |
for (String cmd : monitorCommands) { |
633 |
MessageOutput.printDirectln(cmd);// Special case: use printDirectln() |
|
2 | 634 |
} |
635 |
} |
|
636 |
} |
|
637 |
||
638 |
void unmonitorCommand(StringTokenizer t) { |
|
639 |
if (t.hasMoreTokens()) { |
|
640 |
String monTok = t.nextToken(); |
|
641 |
int monNum; |
|
642 |
try { |
|
643 |
monNum = Integer.parseInt(monTok); |
|
644 |
} catch (NumberFormatException exc) { |
|
645 |
MessageOutput.println("Not a monitor number:", monTok); |
|
646 |
return; |
|
647 |
} |
|
648 |
String monStr = monTok + ":"; |
|
51 | 649 |
for (String cmd : monitorCommands) { |
2 | 650 |
StringTokenizer ct = new StringTokenizer(cmd); |
651 |
if (ct.nextToken().equals(monStr)) { |
|
652 |
monitorCommands.remove(cmd); |
|
653 |
MessageOutput.println("Unmonitoring", cmd); |
|
654 |
return; |
|
655 |
} |
|
656 |
} |
|
657 |
MessageOutput.println("No monitor numbered:", monTok); |
|
658 |
} else { |
|
659 |
MessageOutput.println("Usage: unmonitor <monitor#>"); |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
||
664 |
void readCommand(StringTokenizer t) { |
|
665 |
if (t.hasMoreTokens()) { |
|
666 |
String cmdfname = t.nextToken(); |
|
667 |
if (!readCommandFile(new File(cmdfname))) { |
|
668 |
MessageOutput.println("Could not open:", cmdfname); |
|
669 |
} |
|
670 |
} else { |
|
671 |
MessageOutput.println("Usage: read <command-filename>"); |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
/** |
|
676 |
* Read and execute a command file. Return true if the file was read |
|
677 |
* else false; |
|
678 |
*/ |
|
679 |
boolean readCommandFile(File f) { |
|
680 |
BufferedReader inFile = null; |
|
681 |
try { |
|
682 |
if (f.canRead()) { |
|
683 |
// Process initial commands. |
|
684 |
MessageOutput.println("*** Reading commands from", f.getPath()); |
|
685 |
inFile = new BufferedReader(new FileReader(f)); |
|
686 |
String ln; |
|
687 |
while ((ln = inFile.readLine()) != null) { |
|
688 |
StringTokenizer t = new StringTokenizer(ln); |
|
689 |
if (t.hasMoreTokens()) { |
|
690 |
executeCommand(t); |
|
691 |
} |
|
692 |
} |
|
693 |
} |
|
694 |
} catch (IOException e) { |
|
695 |
} finally { |
|
696 |
if (inFile != null) { |
|
697 |
try { |
|
698 |
inFile.close(); |
|
699 |
} catch (Exception exc) { |
|
700 |
} |
|
701 |
} |
|
702 |
} |
|
703 |
return inFile != null; |
|
704 |
} |
|
705 |
||
706 |
/** |
|
707 |
* Try to read commands from dir/fname, unless |
|
708 |
* the canonical path passed in is the same as that |
|
709 |
* for dir/fname. |
|
710 |
* Return null if that file doesn't exist, |
|
711 |
* else return the canonical path of that file. |
|
712 |
*/ |
|
713 |
String readStartupCommandFile(String dir, String fname, String canonPath) { |
|
714 |
File dotInitFile = new File(dir, fname); |
|
715 |
if (!dotInitFile.exists()) { |
|
716 |
return null; |
|
717 |
} |
|
718 |
||
719 |
String myCanonFile; |
|
720 |
try { |
|
721 |
myCanonFile = dotInitFile.getCanonicalPath(); |
|
722 |
} catch (IOException ee) { |
|
723 |
MessageOutput.println("Could not open:", dotInitFile.getPath()); |
|
724 |
return null; |
|
725 |
} |
|
726 |
if (canonPath == null || !canonPath.equals(myCanonFile)) { |
|
727 |
if (!readCommandFile(dotInitFile)) { |
|
728 |
MessageOutput.println("Could not open:", dotInitFile.getPath()); |
|
729 |
} |
|
730 |
} |
|
731 |
return myCanonFile; |
|
732 |
} |
|
733 |
||
734 |
||
735 |
public TTY() throws Exception { |
|
736 |
||
737 |
MessageOutput.println("Initializing progname", progname); |
|
738 |
||
739 |
if (Env.connection().isOpen() && Env.vm().canBeModified()) { |
|
740 |
/* |
|
741 |
* Connection opened on startup. Start event handler |
|
742 |
* immediately, telling it (through arg 2) to stop on the |
|
743 |
* VM start event. |
|
744 |
*/ |
|
745 |
this.handler = new EventHandler(this, true); |
|
746 |
} |
|
747 |
try { |
|
748 |
BufferedReader in = |
|
749 |
new BufferedReader(new InputStreamReader(System.in)); |
|
750 |
||
751 |
String lastLine = null; |
|
752 |
||
753 |
Thread.currentThread().setPriority(Thread.NORM_PRIORITY); |
|
754 |
||
755 |
/* |
|
756 |
* Read start up files. This mimics the behavior |
|
757 |
* of gdb which will read both ~/.gdbinit and then |
|
758 |
* ./.gdbinit if they exist. We have the twist that |
|
759 |
* we allow two different names, so we do this: |
|
760 |
* if ~/jdb.ini exists, |
|
761 |
* read it |
|
762 |
* else if ~/.jdbrc exists, |
|
763 |
* read it |
|
764 |
* |
|
765 |
* if ./jdb.ini exists, |
|
766 |
* if it hasn't been read, read it |
|
767 |
* It could have been read above because ~ == . |
|
768 |
* or because of symlinks, ... |
|
769 |
* else if ./jdbrx exists |
|
770 |
* if it hasn't been read, read it |
|
771 |
*/ |
|
772 |
{ |
|
773 |
String userHome = System.getProperty("user.home"); |
|
774 |
String canonPath; |
|
775 |
||
776 |
if ((canonPath = readStartupCommandFile(userHome, "jdb.ini", null)) == null) { |
|
777 |
// Doesn't exist, try alternate spelling |
|
778 |
canonPath = readStartupCommandFile(userHome, ".jdbrc", null); |
|
779 |
} |
|
780 |
||
781 |
String userDir = System.getProperty("user.dir"); |
|
782 |
if (readStartupCommandFile(userDir, "jdb.ini", canonPath) == null) { |
|
783 |
// Doesn't exist, try alternate spelling |
|
784 |
readStartupCommandFile(userDir, ".jdbrc", canonPath); |
|
785 |
} |
|
786 |
} |
|
787 |
||
788 |
// Process interactive commands. |
|
789 |
MessageOutput.printPrompt(); |
|
790 |
while (true) { |
|
791 |
String ln = in.readLine(); |
|
792 |
if (ln == null) { |
|
38348
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
793 |
/* |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
794 |
* Jdb is being shutdown because debuggee exited, ignore any 'null' |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
795 |
* returned by readLine() during shutdown. JDK-8154144. |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
796 |
*/ |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
797 |
if (!isShuttingDown()) { |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
798 |
MessageOutput.println("Input stream closed."); |
bd578f1c54b6
8154144: Tests in com/sun/jdi fails intermittently with "jdb input stream closed prematurely"
dsamersoff
parents:
25859
diff
changeset
|
799 |
} |
2 | 800 |
ln = "quit"; |
801 |
} |
|
802 |
||
803 |
if (ln.startsWith("!!") && lastLine != null) { |
|
804 |
ln = lastLine + ln.substring(2); |
|
805 |
MessageOutput.printDirectln(ln);// Special case: use printDirectln() |
|
806 |
} |
|
807 |
||
808 |
StringTokenizer t = new StringTokenizer(ln); |
|
809 |
if (t.hasMoreTokens()) { |
|
810 |
lastLine = ln; |
|
811 |
executeCommand(t); |
|
812 |
} else { |
|
813 |
MessageOutput.printPrompt(); |
|
814 |
} |
|
815 |
} |
|
816 |
} catch (VMDisconnectedException e) { |
|
817 |
handler.handleDisconnectedException(); |
|
818 |
} |
|
819 |
} |
|
820 |
||
821 |
private static void usage() { |
|
822 |
MessageOutput.println("zz usage text", new Object [] {progname, |
|
823 |
File.pathSeparator}); |
|
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
824 |
System.exit(0); |
2 | 825 |
} |
826 |
||
827 |
static void usageError(String messageKey) { |
|
828 |
MessageOutput.println(messageKey); |
|
829 |
MessageOutput.println(); |
|
830 |
usage(); |
|
831 |
} |
|
832 |
||
833 |
static void usageError(String messageKey, String argument) { |
|
834 |
MessageOutput.println(messageKey, argument); |
|
835 |
MessageOutput.println(); |
|
836 |
usage(); |
|
837 |
} |
|
838 |
||
839 |
private static boolean supportsSharedMemory() { |
|
51 | 840 |
for (Connector connector : |
841 |
Bootstrap.virtualMachineManager().allConnectors()) { |
|
2 | 842 |
if (connector.transport() == null) { |
843 |
continue; |
|
844 |
} |
|
845 |
if ("dt_shmem".equals(connector.transport().name())) { |
|
846 |
return true; |
|
847 |
} |
|
848 |
} |
|
849 |
return false; |
|
850 |
} |
|
851 |
||
852 |
private static String addressToSocketArgs(String address) { |
|
853 |
int index = address.indexOf(':'); |
|
854 |
if (index != -1) { |
|
855 |
String hostString = address.substring(0, index); |
|
856 |
String portString = address.substring(index + 1); |
|
857 |
return "hostname=" + hostString + ",port=" + portString; |
|
858 |
} else { |
|
859 |
return "port=" + address; |
|
860 |
} |
|
861 |
} |
|
862 |
||
863 |
private static boolean hasWhitespace(String string) { |
|
864 |
int length = string.length(); |
|
865 |
for (int i = 0; i < length; i++) { |
|
866 |
if (Character.isWhitespace(string.charAt(i))) { |
|
867 |
return true; |
|
868 |
} |
|
869 |
} |
|
870 |
return false; |
|
871 |
} |
|
872 |
||
873 |
private static String addArgument(String string, String argument) { |
|
874 |
if (hasWhitespace(argument) || argument.indexOf(',') != -1) { |
|
875 |
// Quotes were stripped out for this argument, add 'em back. |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
876 |
StringBuilder sb = new StringBuilder(string); |
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
877 |
sb.append('"'); |
2 | 878 |
for (int i = 0; i < argument.length(); i++) { |
879 |
char c = argument.charAt(i); |
|
880 |
if (c == '"') { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
881 |
sb.append('\\'); |
2 | 882 |
} |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
883 |
sb.append(c); |
2 | 884 |
} |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
885 |
sb.append("\" "); |
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
886 |
return sb.toString(); |
2 | 887 |
} else { |
888 |
return string + argument + ' '; |
|
889 |
} |
|
890 |
} |
|
891 |
||
892 |
public static void main(String argv[]) throws MissingResourceException { |
|
893 |
String cmdLine = ""; |
|
894 |
String javaArgs = ""; |
|
895 |
int traceFlags = VirtualMachine.TRACE_NONE; |
|
896 |
boolean launchImmediately = false; |
|
897 |
String connectSpec = null; |
|
898 |
||
899 |
MessageOutput.textResources = ResourceBundle.getBundle |
|
900 |
("com.sun.tools.example.debug.tty.TTYResources", |
|
901 |
Locale.getDefault()); |
|
902 |
||
903 |
for (int i = 0; i < argv.length; i++) { |
|
904 |
String token = argv[i]; |
|
905 |
if (token.equals("-dbgtrace")) { |
|
906 |
if ((i == argv.length - 1) || |
|
907 |
! Character.isDigit(argv[i+1].charAt(0))) { |
|
908 |
traceFlags = VirtualMachine.TRACE_ALL; |
|
909 |
} else { |
|
910 |
String flagStr = ""; |
|
911 |
try { |
|
912 |
flagStr = argv[++i]; |
|
913 |
traceFlags = Integer.decode(flagStr).intValue(); |
|
914 |
} catch (NumberFormatException nfe) { |
|
915 |
usageError("dbgtrace flag value must be an integer:", |
|
916 |
flagStr); |
|
917 |
return; |
|
918 |
} |
|
919 |
} |
|
920 |
} else if (token.equals("-X")) { |
|
921 |
usageError("Use java minus X to see"); |
|
922 |
return; |
|
923 |
} else if ( |
|
924 |
// Standard VM options passed on |
|
925 |
token.equals("-v") || token.startsWith("-v:") || // -v[:...] |
|
926 |
token.startsWith("-verbose") || // -verbose[:...] |
|
927 |
token.startsWith("-D") || |
|
928 |
// -classpath handled below |
|
929 |
// NonStandard options passed on |
|
930 |
token.startsWith("-X") || |
|
931 |
// Old-style options (These should remain in place as long as |
|
932 |
// the standard VM accepts them) |
|
933 |
token.equals("-noasyncgc") || token.equals("-prof") || |
|
53853 | 934 |
token.equals("-verify") || |
2 | 935 |
token.equals("-verifyremote") || |
936 |
token.equals("-verbosegc") || |
|
937 |
token.startsWith("-ms") || token.startsWith("-mx") || |
|
938 |
token.startsWith("-ss") || token.startsWith("-oss") ) { |
|
939 |
||
940 |
javaArgs = addArgument(javaArgs, token); |
|
941 |
} else if (token.equals("-tclassic")) { |
|
942 |
usageError("Classic VM no longer supported."); |
|
943 |
return; |
|
944 |
} else if (token.equals("-tclient")) { |
|
945 |
// -client must be the first one |
|
946 |
javaArgs = "-client " + javaArgs; |
|
947 |
} else if (token.equals("-tserver")) { |
|
948 |
// -server must be the first one |
|
949 |
javaArgs = "-server " + javaArgs; |
|
950 |
} else if (token.equals("-sourcepath")) { |
|
951 |
if (i == (argv.length - 1)) { |
|
952 |
usageError("No sourcepath specified."); |
|
953 |
return; |
|
954 |
} |
|
955 |
Env.setSourcePath(argv[++i]); |
|
956 |
} else if (token.equals("-classpath")) { |
|
957 |
if (i == (argv.length - 1)) { |
|
958 |
usageError("No classpath specified."); |
|
959 |
return; |
|
960 |
} |
|
961 |
javaArgs = addArgument(javaArgs, token); |
|
962 |
javaArgs = addArgument(javaArgs, argv[++i]); |
|
963 |
} else if (token.equals("-attach")) { |
|
964 |
if (connectSpec != null) { |
|
965 |
usageError("cannot redefine existing connection", token); |
|
966 |
return; |
|
967 |
} |
|
968 |
if (i == (argv.length - 1)) { |
|
969 |
usageError("No attach address specified."); |
|
970 |
return; |
|
971 |
} |
|
972 |
String address = argv[++i]; |
|
973 |
||
974 |
/* |
|
975 |
* -attach is shorthand for one of the reference implementation's |
|
976 |
* attaching connectors. Use the shared memory attach if it's |
|
977 |
* available; otherwise, use sockets. Build a connect |
|
978 |
* specification string based on this decision. |
|
979 |
*/ |
|
980 |
if (supportsSharedMemory()) { |
|
981 |
connectSpec = "com.sun.jdi.SharedMemoryAttach:name=" + |
|
982 |
address; |
|
983 |
} else { |
|
984 |
String suboptions = addressToSocketArgs(address); |
|
985 |
connectSpec = "com.sun.jdi.SocketAttach:" + suboptions; |
|
986 |
} |
|
987 |
} else if (token.equals("-listen") || token.equals("-listenany")) { |
|
988 |
if (connectSpec != null) { |
|
989 |
usageError("cannot redefine existing connection", token); |
|
990 |
return; |
|
991 |
} |
|
992 |
String address = null; |
|
993 |
if (token.equals("-listen")) { |
|
994 |
if (i == (argv.length - 1)) { |
|
995 |
usageError("No attach address specified."); |
|
996 |
return; |
|
997 |
} |
|
998 |
address = argv[++i]; |
|
999 |
} |
|
1000 |
||
1001 |
/* |
|
1002 |
* -listen[any] is shorthand for one of the reference implementation's |
|
1003 |
* listening connectors. Use the shared memory listen if it's |
|
1004 |
* available; otherwise, use sockets. Build a connect |
|
1005 |
* specification string based on this decision. |
|
1006 |
*/ |
|
1007 |
if (supportsSharedMemory()) { |
|
1008 |
connectSpec = "com.sun.jdi.SharedMemoryListen:"; |
|
1009 |
if (address != null) { |
|
1010 |
connectSpec += ("name=" + address); |
|
1011 |
} |
|
1012 |
} else { |
|
1013 |
connectSpec = "com.sun.jdi.SocketListen:"; |
|
1014 |
if (address != null) { |
|
1015 |
connectSpec += addressToSocketArgs(address); |
|
1016 |
} |
|
1017 |
} |
|
1018 |
} else if (token.equals("-launch")) { |
|
1019 |
launchImmediately = true; |
|
1020 |
} else if (token.equals("-listconnectors")) { |
|
1021 |
Commands evaluator = new Commands(); |
|
1022 |
evaluator.commandConnectors(Bootstrap.virtualMachineManager()); |
|
1023 |
return; |
|
1024 |
} else if (token.equals("-connect")) { |
|
1025 |
/* |
|
1026 |
* -connect allows the user to pick the connector |
|
1027 |
* used in bringing up the target VM. This allows |
|
1028 |
* use of connectors other than those in the reference |
|
1029 |
* implementation. |
|
1030 |
*/ |
|
1031 |
if (connectSpec != null) { |
|
1032 |
usageError("cannot redefine existing connection", token); |
|
1033 |
return; |
|
1034 |
} |
|
1035 |
if (i == (argv.length - 1)) { |
|
1036 |
usageError("No connect specification."); |
|
1037 |
return; |
|
1038 |
} |
|
1039 |
connectSpec = argv[++i]; |
|
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
1040 |
} else if (token.equals("-?") || |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
1041 |
token.equals("-h") || |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
1042 |
token.equals("--help") || |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
1043 |
// -help: legacy. |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
1044 |
token.equals("-help")) { |
2 | 1045 |
usage(); |
1046 |
} else if (token.equals("-version")) { |
|
1047 |
Commands evaluator = new Commands(); |
|
1048 |
evaluator.commandVersion(progname, |
|
1049 |
Bootstrap.virtualMachineManager()); |
|
1050 |
System.exit(0); |
|
1051 |
} else if (token.startsWith("-")) { |
|
1052 |
usageError("invalid option", token); |
|
1053 |
return; |
|
1054 |
} else { |
|
1055 |
// Everything from here is part of the command line |
|
1056 |
cmdLine = addArgument("", token); |
|
1057 |
for (i++; i < argv.length; i++) { |
|
1058 |
cmdLine = addArgument(cmdLine, argv[i]); |
|
1059 |
} |
|
1060 |
break; |
|
1061 |
} |
|
1062 |
} |
|
1063 |
||
1064 |
/* |
|
1065 |
* Unless otherwise specified, set the default connect spec. |
|
1066 |
*/ |
|
1067 |
||
1068 |
/* |
|
1069 |
* Here are examples of jdb command lines and how the options |
|
1070 |
* are interpreted as arguments to the program being debugged. |
|
1071 |
* arg1 arg2 |
|
1072 |
* ---- ---- |
|
1073 |
* jdb hello a b a b |
|
1074 |
* jdb hello "a b" a b |
|
1075 |
* jdb hello a,b a,b |
|
1076 |
* jdb hello a, b a, b |
|
1077 |
* jdb hello "a, b" a, b |
|
1078 |
* jdb -connect "com.sun.jdi.CommandLineLaunch:main=hello a,b" illegal |
|
1079 |
* jdb -connect com.sun.jdi.CommandLineLaunch:main=hello "a,b" illegal |
|
1080 |
* jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a,b"' arg1 = a,b |
|
1081 |
* jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a b"' arg1 = a b |
|
1082 |
* jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello a b' arg1 = a arg2 = b |
|
1083 |
* jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a," b' arg1 = a, arg2 = b |
|
1084 |
*/ |
|
1085 |
if (connectSpec == null) { |
|
1086 |
connectSpec = "com.sun.jdi.CommandLineLaunch:"; |
|
1087 |
} else if (!connectSpec.endsWith(",") && !connectSpec.endsWith(":")) { |
|
1088 |
connectSpec += ","; // (Bug ID 4285874) |
|
1089 |
} |
|
1090 |
||
1091 |
cmdLine = cmdLine.trim(); |
|
1092 |
javaArgs = javaArgs.trim(); |
|
1093 |
||
1094 |
if (cmdLine.length() > 0) { |
|
1095 |
if (!connectSpec.startsWith("com.sun.jdi.CommandLineLaunch:")) { |
|
1096 |
usageError("Cannot specify command line with connector:", |
|
1097 |
connectSpec); |
|
1098 |
return; |
|
1099 |
} |
|
1100 |
connectSpec += "main=" + cmdLine + ","; |
|
1101 |
} |
|
1102 |
||
1103 |
if (javaArgs.length() > 0) { |
|
1104 |
if (!connectSpec.startsWith("com.sun.jdi.CommandLineLaunch:")) { |
|
1105 |
usageError("Cannot specify target vm arguments with connector:", |
|
1106 |
connectSpec); |
|
1107 |
return; |
|
1108 |
} |
|
1109 |
connectSpec += "options=" + javaArgs + ","; |
|
1110 |
} |
|
1111 |
||
1112 |
try { |
|
1113 |
if (! connectSpec.endsWith(",")) { |
|
1114 |
connectSpec += ","; // (Bug ID 4285874) |
|
1115 |
} |
|
1116 |
Env.init(connectSpec, launchImmediately, traceFlags); |
|
1117 |
new TTY(); |
|
1118 |
} catch(Exception e) { |
|
1119 |
MessageOutput.printException("Internal exception:", e); |
|
1120 |
} |
|
1121 |
} |
|
1122 |
} |