2
|
1 |
/*
|
715
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. 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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.example.debug.gui;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.util.*;
|
|
30 |
|
|
31 |
import com.sun.jdi.*;
|
|
32 |
import com.sun.jdi.request.*;
|
|
33 |
|
|
34 |
import com.sun.tools.example.debug.bdi.*;
|
|
35 |
|
|
36 |
public class CommandInterpreter {
|
|
37 |
|
|
38 |
boolean echo;
|
|
39 |
|
|
40 |
Environment env;
|
|
41 |
|
|
42 |
private ContextManager context;
|
|
43 |
private ExecutionManager runtime;
|
|
44 |
private ClassManager classManager;
|
|
45 |
private SourceManager sourceManager;
|
|
46 |
|
|
47 |
private OutputSink out; //### Hack! Should be local in each method used.
|
|
48 |
private String lastCommand = "help";
|
|
49 |
|
|
50 |
public CommandInterpreter(Environment env) {
|
|
51 |
this(env, true);
|
|
52 |
}
|
|
53 |
|
|
54 |
public CommandInterpreter(Environment env, boolean echo) {
|
|
55 |
this.env = env;
|
|
56 |
this.echo = echo;
|
|
57 |
this.runtime = env.getExecutionManager();
|
|
58 |
this.context = env.getContextManager();
|
|
59 |
this.classManager = env.getClassManager();
|
|
60 |
this.sourceManager = env.getSourceManager();
|
|
61 |
}
|
|
62 |
|
|
63 |
private ThreadReference[] threads = null;
|
|
64 |
|
|
65 |
/*
|
|
66 |
* The numbering of threads is relative to the current set of threads,
|
|
67 |
* and may be affected by the creation and termination of new threads.
|
|
68 |
* Commands issued using such thread ids will only give reliable behavior
|
|
69 |
* relative to what was shown earlier in 'list' commands if the VM is interrupted.
|
|
70 |
* We need a better scheme.
|
|
71 |
*/
|
|
72 |
|
|
73 |
private ThreadReference[] threads() throws NoSessionException {
|
|
74 |
if (threads == null) {
|
|
75 |
ThreadIterator ti = new ThreadIterator(getDefaultThreadGroup());
|
|
76 |
List<ThreadReference> tlist = new ArrayList<ThreadReference>();
|
|
77 |
while (ti.hasNext()) {
|
|
78 |
tlist.add(ti.nextThread());
|
|
79 |
}
|
51
|
80 |
threads = tlist.toArray(new ThreadReference[tlist.size()]);
|
2
|
81 |
}
|
|
82 |
return threads;
|
|
83 |
}
|
|
84 |
|
|
85 |
private ThreadReference findThread(String idToken) throws NoSessionException {
|
|
86 |
String id;
|
|
87 |
ThreadReference thread = null;
|
|
88 |
if (idToken.startsWith("t@")) {
|
|
89 |
id = idToken.substring(2);
|
|
90 |
} else {
|
|
91 |
id = idToken;
|
|
92 |
}
|
|
93 |
try {
|
|
94 |
ThreadReference[] threads = threads();
|
|
95 |
long threadID = Long.parseLong(id, 16);
|
|
96 |
for (int i = 0; i < threads.length; i++) {
|
|
97 |
if (threads[i].uniqueID() == threadID) {
|
|
98 |
thread = threads[i];
|
|
99 |
break;
|
|
100 |
}
|
|
101 |
}
|
|
102 |
if (thread == null) {
|
|
103 |
//env.failure("No thread for id \"" + idToken + "\"");
|
|
104 |
env.failure("\"" + idToken + "\" is not a valid thread id.");
|
|
105 |
}
|
|
106 |
} catch (NumberFormatException e) {
|
|
107 |
env.error("Thread id \"" + idToken + "\" is ill-formed.");
|
|
108 |
thread = null;
|
|
109 |
}
|
|
110 |
return thread;
|
|
111 |
}
|
|
112 |
|
|
113 |
private ThreadIterator allThreads() throws NoSessionException {
|
|
114 |
threads = null;
|
|
115 |
//### Why not use runtime.allThreads().iterator() ?
|
|
116 |
return new ThreadIterator(runtime.topLevelThreadGroups());
|
|
117 |
}
|
|
118 |
|
|
119 |
private ThreadIterator currentThreadGroupThreads() throws NoSessionException {
|
|
120 |
threads = null;
|
|
121 |
return new ThreadIterator(getDefaultThreadGroup());
|
|
122 |
}
|
|
123 |
|
|
124 |
private ThreadGroupIterator allThreadGroups() throws NoSessionException {
|
|
125 |
threads = null;
|
|
126 |
return new ThreadGroupIterator(runtime.topLevelThreadGroups());
|
|
127 |
}
|
|
128 |
|
|
129 |
private ThreadGroupReference defaultThreadGroup;
|
|
130 |
|
|
131 |
private ThreadGroupReference getDefaultThreadGroup() throws NoSessionException {
|
|
132 |
if (defaultThreadGroup == null) {
|
|
133 |
defaultThreadGroup = runtime.systemThreadGroup();
|
|
134 |
}
|
|
135 |
return defaultThreadGroup;
|
|
136 |
}
|
|
137 |
|
|
138 |
private void setDefaultThreadGroup(ThreadGroupReference tg) {
|
|
139 |
defaultThreadGroup = tg;
|
|
140 |
}
|
|
141 |
|
|
142 |
/*
|
|
143 |
* Command handlers.
|
|
144 |
*/
|
|
145 |
|
|
146 |
// Command: classes
|
|
147 |
|
|
148 |
private void commandClasses() throws NoSessionException {
|
|
149 |
OutputSink out = env.getOutputSink();
|
|
150 |
//out.println("** classes list **");
|
51
|
151 |
for (ReferenceType refType : runtime.allClasses()) {
|
2
|
152 |
out.println(refType.name());
|
|
153 |
}
|
|
154 |
out.show();
|
|
155 |
}
|
|
156 |
|
|
157 |
|
|
158 |
// Command: methods
|
|
159 |
|
|
160 |
private void commandMethods(StringTokenizer t) throws NoSessionException {
|
|
161 |
if (!t.hasMoreTokens()) {
|
|
162 |
env.error("No class specified.");
|
|
163 |
return;
|
|
164 |
}
|
|
165 |
String idClass = t.nextToken();
|
|
166 |
ReferenceType cls = findClass(idClass);
|
|
167 |
if (cls != null) {
|
51
|
168 |
List<Method> methods = cls.allMethods();
|
2
|
169 |
OutputSink out = env.getOutputSink();
|
|
170 |
for (int i = 0; i < methods.size(); i++) {
|
51
|
171 |
Method method = methods.get(i);
|
2
|
172 |
out.print(method.declaringType().name() + " " +
|
|
173 |
method.name() + "(");
|
51
|
174 |
Iterator<String> it = method.argumentTypeNames().iterator();
|
2
|
175 |
if (it.hasNext()) {
|
|
176 |
while (true) {
|
51
|
177 |
out.print(it.next());
|
2
|
178 |
if (!it.hasNext()) {
|
|
179 |
break;
|
|
180 |
}
|
|
181 |
out.print(", ");
|
|
182 |
}
|
|
183 |
}
|
|
184 |
out.println(")");
|
|
185 |
}
|
|
186 |
out.show();
|
|
187 |
} else {
|
|
188 |
//### Should validate class name syntax.
|
|
189 |
env.failure("\"" + idClass + "\" is not a valid id or class name.");
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
private ReferenceType findClass(String pattern) throws NoSessionException {
|
51
|
194 |
List<ReferenceType> results = runtime.findClassesMatchingPattern(pattern);
|
2
|
195 |
if (results.size() > 0) {
|
|
196 |
//### Should handle multiple results sensibly.
|
51
|
197 |
return results.get(0);
|
2
|
198 |
}
|
|
199 |
return null;
|
|
200 |
}
|
|
201 |
|
|
202 |
// Command: threads
|
|
203 |
|
|
204 |
private void commandThreads(StringTokenizer t) throws NoSessionException {
|
|
205 |
if (!t.hasMoreTokens()) {
|
|
206 |
OutputSink out = env.getOutputSink();
|
|
207 |
printThreadGroup(out, getDefaultThreadGroup(), 0);
|
|
208 |
out.show();
|
|
209 |
return;
|
|
210 |
}
|
|
211 |
String name = t.nextToken();
|
|
212 |
ThreadGroupReference tg = findThreadGroup(name);
|
|
213 |
if (tg == null) {
|
|
214 |
env.failure(name + " is not a valid threadgroup name.");
|
|
215 |
} else {
|
|
216 |
OutputSink out = env.getOutputSink();
|
|
217 |
printThreadGroup(out, tg, 0);
|
|
218 |
out.show();
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
private ThreadGroupReference findThreadGroup(String name) throws NoSessionException {
|
|
223 |
//### Issue: Uniqueness of thread group names is not enforced.
|
|
224 |
ThreadGroupIterator tgi = allThreadGroups();
|
|
225 |
while (tgi.hasNext()) {
|
|
226 |
ThreadGroupReference tg = tgi.nextThreadGroup();
|
|
227 |
if (tg.name().equals(name)) {
|
|
228 |
return tg;
|
|
229 |
}
|
|
230 |
}
|
|
231 |
return null;
|
|
232 |
}
|
|
233 |
|
|
234 |
private int printThreadGroup(OutputSink out, ThreadGroupReference tg, int iThread) {
|
|
235 |
out.println("Group " + tg.name() + ":");
|
51
|
236 |
List<ThreadReference> tlist = tg.threads();
|
2
|
237 |
int maxId = 0;
|
|
238 |
int maxName = 0;
|
|
239 |
for (int i = 0 ; i < tlist.size() ; i++) {
|
51
|
240 |
ThreadReference thr = tlist.get(i);
|
2
|
241 |
int len = Utils.description(thr).length();
|
|
242 |
if (len > maxId)
|
|
243 |
maxId = len;
|
|
244 |
String name = thr.name();
|
|
245 |
int iDot = name.lastIndexOf('.');
|
|
246 |
if (iDot >= 0 && name.length() > iDot) {
|
|
247 |
name = name.substring(iDot + 1);
|
|
248 |
}
|
|
249 |
if (name.length() > maxName)
|
|
250 |
maxName = name.length();
|
|
251 |
}
|
|
252 |
String maxNumString = String.valueOf(iThread + tlist.size());
|
|
253 |
int maxNumDigits = maxNumString.length();
|
|
254 |
for (int i = 0 ; i < tlist.size() ; i++) {
|
51
|
255 |
ThreadReference thr = tlist.get(i);
|
2
|
256 |
char buf[] = new char[80];
|
|
257 |
for (int j = 0; j < 79; j++) {
|
|
258 |
buf[j] = ' ';
|
|
259 |
}
|
|
260 |
buf[79] = '\0';
|
|
261 |
StringBuffer sbOut = new StringBuffer();
|
|
262 |
sbOut.append(buf);
|
|
263 |
|
|
264 |
// Right-justify the thread number at start of output string
|
|
265 |
String numString = String.valueOf(iThread + i + 1);
|
|
266 |
sbOut.insert(maxNumDigits - numString.length(),
|
|
267 |
numString);
|
|
268 |
sbOut.insert(maxNumDigits, ".");
|
|
269 |
|
|
270 |
int iBuf = maxNumDigits + 2;
|
|
271 |
sbOut.insert(iBuf, Utils.description(thr));
|
|
272 |
iBuf += maxId + 1;
|
|
273 |
String name = thr.name();
|
|
274 |
int iDot = name.lastIndexOf('.');
|
|
275 |
if (iDot >= 0 && name.length() > iDot) {
|
|
276 |
name = name.substring(iDot + 1);
|
|
277 |
}
|
|
278 |
sbOut.insert(iBuf, name);
|
|
279 |
iBuf += maxName + 1;
|
|
280 |
sbOut.insert(iBuf, Utils.getStatus(thr));
|
|
281 |
sbOut.setLength(79);
|
|
282 |
out.println(sbOut.toString());
|
|
283 |
}
|
51
|
284 |
for (ThreadGroupReference tg0 : tg.threadGroups()) {
|
2
|
285 |
if (!tg.equals(tg0)) { // TODO ref mgt
|
|
286 |
iThread += printThreadGroup(out, tg0, iThread + tlist.size());
|
|
287 |
}
|
|
288 |
}
|
|
289 |
return tlist.size();
|
|
290 |
}
|
|
291 |
|
|
292 |
// Command: threadgroups
|
|
293 |
|
|
294 |
private void commandThreadGroups() throws NoSessionException {
|
|
295 |
ThreadGroupIterator it = allThreadGroups();
|
|
296 |
int cnt = 0;
|
|
297 |
OutputSink out = env.getOutputSink();
|
|
298 |
while (it.hasNext()) {
|
|
299 |
ThreadGroupReference tg = it.nextThreadGroup();
|
|
300 |
++cnt;
|
|
301 |
out.println("" + cnt + ". " + Utils.description(tg) + " " + tg.name());
|
|
302 |
}
|
|
303 |
out.show();
|
|
304 |
}
|
|
305 |
|
|
306 |
// Command: thread
|
|
307 |
|
|
308 |
private void commandThread(StringTokenizer t) throws NoSessionException {
|
|
309 |
if (!t.hasMoreTokens()) {
|
|
310 |
env.error("Thread number not specified.");
|
|
311 |
return;
|
|
312 |
}
|
|
313 |
ThreadReference thread = findThread(t.nextToken());
|
|
314 |
if (thread != null) {
|
|
315 |
//### Should notify user.
|
|
316 |
context.setCurrentThread(thread);
|
|
317 |
}
|
|
318 |
}
|
|
319 |
|
|
320 |
// Command: threadgroup
|
|
321 |
|
|
322 |
private void commandThreadGroup(StringTokenizer t) throws NoSessionException {
|
|
323 |
if (!t.hasMoreTokens()) {
|
|
324 |
env.error("Threadgroup name not specified.");
|
|
325 |
return;
|
|
326 |
}
|
|
327 |
String name = t.nextToken();
|
|
328 |
ThreadGroupReference tg = findThreadGroup(name);
|
|
329 |
if (tg == null) {
|
|
330 |
env.failure(name + " is not a valid threadgroup name.");
|
|
331 |
} else {
|
|
332 |
//### Should notify user.
|
|
333 |
setDefaultThreadGroup(tg);
|
|
334 |
}
|
|
335 |
}
|
|
336 |
|
|
337 |
// Command: run
|
|
338 |
|
|
339 |
private void commandRun(StringTokenizer t) throws NoSessionException {
|
|
340 |
if (doLoad(false, t)) {
|
|
341 |
env.notice("Running ...");
|
|
342 |
}
|
|
343 |
}
|
|
344 |
|
|
345 |
// Command: load
|
|
346 |
|
|
347 |
private void commandLoad(StringTokenizer t) throws NoSessionException {
|
|
348 |
if (doLoad(true, t)) {}
|
|
349 |
}
|
|
350 |
|
|
351 |
private boolean doLoad(boolean suspended,
|
|
352 |
StringTokenizer t) throws NoSessionException {
|
|
353 |
|
|
354 |
String clname;
|
|
355 |
|
|
356 |
if (!t.hasMoreTokens()) {
|
|
357 |
clname = context.getMainClassName();
|
|
358 |
if (!clname.equals("")) {
|
|
359 |
// Run from prevously-set class name.
|
|
360 |
try {
|
|
361 |
String vmArgs = context.getVmArguments();
|
|
362 |
runtime.run(suspended,
|
|
363 |
vmArgs,
|
|
364 |
clname,
|
|
365 |
context.getProgramArguments());
|
|
366 |
return true;
|
|
367 |
} catch (VMLaunchFailureException e) {
|
|
368 |
env.failure("Attempt to launch main class \"" + clname + "\" failed.");
|
|
369 |
}
|
|
370 |
} else {
|
|
371 |
env.failure("No main class specifed and no current default defined.");
|
|
372 |
}
|
|
373 |
} else {
|
|
374 |
clname = t.nextToken();
|
|
375 |
StringBuffer sbuf = new StringBuffer();
|
|
376 |
// Allow VM arguments to be specified here?
|
|
377 |
while (t.hasMoreTokens()) {
|
|
378 |
String tok = t.nextToken();
|
|
379 |
sbuf.append(tok);
|
|
380 |
if (t.hasMoreTokens()) {
|
|
381 |
sbuf.append(' ');
|
|
382 |
}
|
|
383 |
}
|
|
384 |
String args = sbuf.toString();
|
|
385 |
try {
|
|
386 |
String vmArgs = context.getVmArguments();
|
|
387 |
runtime.run(suspended, vmArgs, clname, args);
|
|
388 |
context.setMainClassName(clname);
|
|
389 |
//context.setVmArguments(vmArgs);
|
|
390 |
context.setProgramArguments(args);
|
|
391 |
return true;
|
|
392 |
} catch (VMLaunchFailureException e) {
|
|
393 |
env.failure("Attempt to launch main class \"" + clname + "\" failed.");
|
|
394 |
}
|
|
395 |
}
|
|
396 |
return false;
|
|
397 |
}
|
|
398 |
|
|
399 |
// Command: connect
|
|
400 |
|
|
401 |
private void commandConnect(StringTokenizer t) {
|
|
402 |
try {
|
|
403 |
LaunchTool.queryAndLaunchVM(runtime);
|
|
404 |
} catch (VMLaunchFailureException e) {
|
|
405 |
env.failure("Attempt to connect failed.");
|
|
406 |
}
|
|
407 |
}
|
|
408 |
|
|
409 |
// Command: attach
|
|
410 |
|
|
411 |
private void commandAttach(StringTokenizer t) {
|
|
412 |
String portName;
|
|
413 |
if (!t.hasMoreTokens()) {
|
|
414 |
portName = context.getRemotePort();
|
|
415 |
if (!portName.equals("")) {
|
|
416 |
try {
|
|
417 |
runtime.attach(portName);
|
|
418 |
} catch (VMLaunchFailureException e) {
|
|
419 |
env.failure("Attempt to attach to port \"" + portName + "\" failed.");
|
|
420 |
}
|
|
421 |
} else {
|
|
422 |
env.failure("No port specifed and no current default defined.");
|
|
423 |
}
|
|
424 |
} else {
|
|
425 |
portName = t.nextToken();
|
|
426 |
try {
|
|
427 |
runtime.attach(portName);
|
|
428 |
} catch (VMLaunchFailureException e) {
|
|
429 |
env.failure("Attempt to attach to port \"" + portName + "\" failed.");
|
|
430 |
}
|
|
431 |
context.setRemotePort(portName);
|
|
432 |
}
|
|
433 |
}
|
|
434 |
|
|
435 |
// Command: detach
|
|
436 |
|
|
437 |
private void commandDetach(StringTokenizer t) throws NoSessionException {
|
|
438 |
runtime.detach();
|
|
439 |
}
|
|
440 |
|
|
441 |
// Command: interrupt
|
|
442 |
|
|
443 |
private void commandInterrupt(StringTokenizer t) throws NoSessionException {
|
|
444 |
runtime.interrupt();
|
|
445 |
}
|
|
446 |
|
|
447 |
// Command: suspend
|
|
448 |
|
|
449 |
private void commandSuspend(StringTokenizer t) throws NoSessionException {
|
|
450 |
if (!t.hasMoreTokens()) {
|
|
451 |
// Suspend all threads in the current thread group.
|
|
452 |
//### Issue: help message says default is all threads.
|
|
453 |
//### Behavior here agrees with 'jdb', however.
|
|
454 |
ThreadIterator ti = currentThreadGroupThreads();
|
|
455 |
while (ti.hasNext()) {
|
|
456 |
// TODO - don't suspend debugger threads
|
|
457 |
ti.nextThread().suspend();
|
|
458 |
}
|
|
459 |
env.notice("All (non-system) threads suspended.");
|
|
460 |
} else {
|
|
461 |
while (t.hasMoreTokens()) {
|
|
462 |
ThreadReference thread = findThread(t.nextToken());
|
|
463 |
if (thread != null) {
|
|
464 |
//thread.suspend();
|
|
465 |
runtime.suspendThread(thread);
|
|
466 |
}
|
|
467 |
}
|
|
468 |
}
|
|
469 |
}
|
|
470 |
|
|
471 |
// Command: resume
|
|
472 |
|
|
473 |
private void commandResume(StringTokenizer t) throws NoSessionException {
|
|
474 |
if (!t.hasMoreTokens()) {
|
|
475 |
// Suspend all threads in the current thread group.
|
|
476 |
//### Issue: help message says default is all threads.
|
|
477 |
//### Behavior here agrees with 'jdb', however.
|
|
478 |
ThreadIterator ti = currentThreadGroupThreads();
|
|
479 |
while (ti.hasNext()) {
|
|
480 |
// TODO - don't suspend debugger threads
|
|
481 |
ti.nextThread().resume();
|
|
482 |
}
|
|
483 |
env.notice("All threads resumed.");
|
|
484 |
} else {
|
|
485 |
while (t.hasMoreTokens()) {
|
|
486 |
ThreadReference thread = findThread(t.nextToken());
|
|
487 |
if (thread != null) {
|
|
488 |
//thread.resume();
|
|
489 |
runtime.resumeThread(thread);
|
|
490 |
}
|
|
491 |
}
|
|
492 |
}
|
|
493 |
}
|
|
494 |
|
|
495 |
// Command: cont
|
|
496 |
|
|
497 |
private void commandCont() throws NoSessionException {
|
|
498 |
try {
|
|
499 |
runtime.go();
|
|
500 |
} catch (VMNotInterruptedException e) {
|
|
501 |
//### failure?
|
|
502 |
env.notice("Target VM is already running.");
|
|
503 |
}
|
|
504 |
}
|
|
505 |
|
|
506 |
// Command: step
|
|
507 |
|
|
508 |
private void commandStep(StringTokenizer t) throws NoSessionException{
|
|
509 |
ThreadReference current = context.getCurrentThread();
|
|
510 |
if (current == null) {
|
|
511 |
env.failure("No current thread.");
|
|
512 |
return;
|
|
513 |
}
|
|
514 |
try {
|
|
515 |
if (t.hasMoreTokens() &&
|
|
516 |
t.nextToken().toLowerCase().equals("up")) {
|
|
517 |
runtime.stepOut(current);
|
|
518 |
} else {
|
|
519 |
runtime.stepIntoLine(current);
|
|
520 |
}
|
|
521 |
} catch (AbsentInformationException e) {
|
|
522 |
env.failure("No linenumber information available -- " +
|
|
523 |
"Try \"stepi\" to step by instructions.");
|
|
524 |
}
|
|
525 |
}
|
|
526 |
|
|
527 |
// Command: stepi
|
|
528 |
|
|
529 |
private void commandStepi() throws NoSessionException {
|
|
530 |
ThreadReference current = context.getCurrentThread();
|
|
531 |
if (current == null) {
|
|
532 |
env.failure("No current thread.");
|
|
533 |
return;
|
|
534 |
}
|
|
535 |
runtime.stepIntoInstruction(current);
|
|
536 |
}
|
|
537 |
|
|
538 |
// Command: next
|
|
539 |
|
|
540 |
private void commandNext() throws NoSessionException {
|
|
541 |
ThreadReference current = context.getCurrentThread();
|
|
542 |
if (current == null) {
|
|
543 |
env.failure("No current thread.");
|
|
544 |
return;
|
|
545 |
}
|
|
546 |
try {
|
|
547 |
runtime.stepOverLine(current);
|
|
548 |
} catch (AbsentInformationException e) {
|
|
549 |
env.failure("No linenumber information available -- " +
|
|
550 |
"Try \"nexti\" to step by instructions.");
|
|
551 |
}
|
|
552 |
}
|
|
553 |
|
|
554 |
// Command: nexti (NEW)
|
|
555 |
|
|
556 |
private void commandNexti() throws NoSessionException {
|
|
557 |
ThreadReference current = context.getCurrentThread();
|
|
558 |
if (current == null) {
|
|
559 |
env.failure("No current thread.");
|
|
560 |
return;
|
|
561 |
}
|
|
562 |
runtime.stepOverInstruction(current);
|
|
563 |
}
|
|
564 |
|
|
565 |
// Command: kill
|
|
566 |
|
|
567 |
private void commandKill(StringTokenizer t) throws NoSessionException {
|
|
568 |
//### Should change the way in which thread ids and threadgroup names
|
|
569 |
//### are distinguished.
|
|
570 |
if (!t.hasMoreTokens()) {
|
|
571 |
env.error("Usage: kill <threadgroup name> or <thread id>");
|
|
572 |
return;
|
|
573 |
}
|
|
574 |
while (t.hasMoreTokens()) {
|
|
575 |
String idToken = t.nextToken();
|
|
576 |
ThreadReference thread = findThread(idToken);
|
|
577 |
if (thread != null) {
|
|
578 |
runtime.stopThread(thread);
|
|
579 |
env.notice("Thread " + thread.name() + " killed.");
|
|
580 |
return;
|
|
581 |
} else {
|
|
582 |
/* Check for threadgroup name, NOT skipping "system". */
|
|
583 |
//### Should skip "system"? Classic 'jdb' does this.
|
|
584 |
//### Should deal with possible non-uniqueness of threadgroup names.
|
|
585 |
ThreadGroupIterator itg = allThreadGroups();
|
|
586 |
while (itg.hasNext()) {
|
|
587 |
ThreadGroupReference tg = itg.nextThreadGroup();
|
|
588 |
if (tg.name().equals(idToken)) {
|
|
589 |
ThreadIterator it = new ThreadIterator(tg);
|
|
590 |
while (it.hasNext()) {
|
|
591 |
runtime.stopThread(it.nextThread());
|
|
592 |
}
|
|
593 |
env.notice("Threadgroup " + tg.name() + "killed.");
|
|
594 |
return;
|
|
595 |
}
|
|
596 |
}
|
|
597 |
env.failure("\"" + idToken +
|
|
598 |
"\" is not a valid threadgroup or id.");
|
|
599 |
}
|
|
600 |
}
|
|
601 |
}
|
|
602 |
|
|
603 |
|
|
604 |
/*************
|
|
605 |
// TODO
|
|
606 |
private void commandCatchException(StringTokenizer t) throws NoSessionException {}
|
|
607 |
// TODO
|
|
608 |
private void commandIgnoreException(StringTokenizer t) throws NoSessionException {}
|
|
609 |
*************/
|
|
610 |
|
|
611 |
// Command: up
|
|
612 |
|
|
613 |
//### Print current frame after command?
|
|
614 |
|
|
615 |
int readCount(StringTokenizer t) {
|
|
616 |
int cnt = 1;
|
|
617 |
if (t.hasMoreTokens()) {
|
|
618 |
String idToken = t.nextToken();
|
|
619 |
int n;
|
|
620 |
try {
|
|
621 |
cnt = Integer.valueOf(idToken).intValue();
|
|
622 |
} catch (NumberFormatException e) {
|
|
623 |
cnt = -1;
|
|
624 |
}
|
|
625 |
}
|
|
626 |
return cnt;
|
|
627 |
}
|
|
628 |
|
|
629 |
void commandUp(StringTokenizer t) throws NoSessionException {
|
|
630 |
ThreadReference current = context.getCurrentThread();
|
|
631 |
if (current == null) {
|
|
632 |
env.failure("No current thread.");
|
|
633 |
return;
|
|
634 |
}
|
|
635 |
int nLevels = readCount(t);
|
|
636 |
if (nLevels <= 0) {
|
|
637 |
env.error("usage: up [n frames]");
|
|
638 |
return;
|
|
639 |
}
|
|
640 |
try {
|
|
641 |
int delta = context.moveCurrentFrameIndex(current, -nLevels);
|
|
642 |
if (delta == 0) {
|
|
643 |
env.notice("Already at top of stack.");
|
|
644 |
} else if (-delta < nLevels) {
|
|
645 |
env.notice("Moved up " + delta + " frames to top of stack.");
|
|
646 |
}
|
|
647 |
} catch (VMNotInterruptedException e) {
|
|
648 |
env.failure("Target VM must be in interrupted state.");
|
|
649 |
}
|
|
650 |
}
|
|
651 |
|
|
652 |
private void commandDown(StringTokenizer t) throws NoSessionException {
|
|
653 |
ThreadReference current = context.getCurrentThread();
|
|
654 |
if (current == null) {
|
|
655 |
env.failure("No current thread.");
|
|
656 |
return;
|
|
657 |
}
|
|
658 |
int nLevels = readCount(t);
|
|
659 |
if (nLevels <= 0) {
|
|
660 |
env.error("usage: down [n frames]");
|
|
661 |
return;
|
|
662 |
}
|
|
663 |
try {
|
|
664 |
int delta = context.moveCurrentFrameIndex(current, nLevels);
|
|
665 |
if (delta == 0) {
|
|
666 |
env.notice("Already at bottom of stack.");
|
|
667 |
} else if (delta < nLevels) {
|
|
668 |
env.notice("Moved down " + delta + " frames to bottom of stack.");
|
|
669 |
}
|
|
670 |
} catch (VMNotInterruptedException e) {
|
|
671 |
env.failure("Target VM must be in interrupted state.");
|
|
672 |
}
|
|
673 |
}
|
|
674 |
|
|
675 |
// Command: frame
|
|
676 |
|
|
677 |
private void commandFrame(StringTokenizer t) throws NoSessionException {
|
|
678 |
ThreadReference current = context.getCurrentThread();
|
|
679 |
if (current == null) {
|
|
680 |
env.failure("No current thread.");
|
|
681 |
return;
|
|
682 |
}
|
|
683 |
if (!t.hasMoreTokens()) {
|
|
684 |
env.error("usage: frame <frame-index>");
|
|
685 |
return;
|
|
686 |
}
|
|
687 |
String idToken = t.nextToken();
|
|
688 |
int n;
|
|
689 |
try {
|
|
690 |
n = Integer.valueOf(idToken).intValue();
|
|
691 |
} catch (NumberFormatException e) {
|
|
692 |
n = 0;
|
|
693 |
}
|
|
694 |
if (n <= 0) {
|
|
695 |
env.error("use positive frame index");
|
|
696 |
return;
|
|
697 |
}
|
|
698 |
try {
|
|
699 |
int delta = context.setCurrentFrameIndex(current, n);
|
|
700 |
if (delta == 0) {
|
|
701 |
env.notice("Frame unchanged.");
|
|
702 |
} else if (delta < 0) {
|
|
703 |
env.notice("Moved up " + -delta + " frames.");
|
|
704 |
} else {
|
|
705 |
env.notice("Moved down " + delta + " frames.");
|
|
706 |
}
|
|
707 |
} catch (VMNotInterruptedException e) {
|
|
708 |
env.failure("Target VM must be in interrupted state.");
|
|
709 |
}
|
|
710 |
}
|
|
711 |
|
|
712 |
// Command: where
|
|
713 |
|
|
714 |
//### Should we insist that VM be interrupted here?
|
|
715 |
//### There is an inconsistency between the 'where' command
|
|
716 |
//### and 'up' and 'down' in this respect.
|
|
717 |
|
|
718 |
private void commandWhere(StringTokenizer t, boolean showPC)
|
|
719 |
throws NoSessionException {
|
|
720 |
ThreadReference current = context.getCurrentThread();
|
|
721 |
if (!t.hasMoreTokens()) {
|
|
722 |
if (current == null) {
|
|
723 |
env.error("No thread specified.");
|
|
724 |
return;
|
|
725 |
}
|
|
726 |
dumpStack(current, showPC);
|
|
727 |
} else {
|
|
728 |
String token = t.nextToken();
|
|
729 |
if (token.toLowerCase().equals("all")) {
|
|
730 |
ThreadIterator it = allThreads();
|
|
731 |
while (it.hasNext()) {
|
51
|
732 |
ThreadReference thread = it.next();
|
2
|
733 |
out.println(thread.name() + ": ");
|
|
734 |
dumpStack(thread, showPC);
|
|
735 |
}
|
|
736 |
} else {
|
|
737 |
ThreadReference thread = findThread(t.nextToken());
|
|
738 |
//### Do we want to set current thread here?
|
|
739 |
//### Should notify user of change.
|
|
740 |
if (thread != null) {
|
|
741 |
context.setCurrentThread(thread);
|
|
742 |
}
|
|
743 |
dumpStack(thread, showPC);
|
|
744 |
}
|
|
745 |
}
|
|
746 |
}
|
|
747 |
|
|
748 |
private void dumpStack(ThreadReference thread, boolean showPC) {
|
|
749 |
//### Check for these.
|
|
750 |
//env.failure("Thread no longer exists.");
|
|
751 |
//env.failure("Target VM must be in interrupted state.");
|
|
752 |
//env.failure("Current thread isn't suspended.");
|
|
753 |
//### Should handle extremely long stack traces sensibly for user.
|
51
|
754 |
List<StackFrame> stack = null;
|
2
|
755 |
try {
|
|
756 |
stack = thread.frames();
|
|
757 |
} catch (IncompatibleThreadStateException e) {
|
|
758 |
env.failure("Thread is not suspended.");
|
|
759 |
}
|
|
760 |
//### Fix this!
|
|
761 |
//### Previously mishandled cases where thread was not current.
|
|
762 |
//### Now, prints all of the stack regardless of current frame.
|
|
763 |
int frameIndex = 0;
|
|
764 |
//int frameIndex = context.getCurrentFrameIndex();
|
|
765 |
if (stack == null) {
|
|
766 |
env.failure("Thread is not running (no stack).");
|
|
767 |
} else {
|
|
768 |
OutputSink out = env.getOutputSink();
|
|
769 |
int nFrames = stack.size();
|
|
770 |
for (int i = frameIndex; i < nFrames; i++) {
|
51
|
771 |
StackFrame frame = stack.get(i);
|
2
|
772 |
Location loc = frame.location();
|
|
773 |
Method meth = loc.method();
|
|
774 |
out.print(" [" + (i + 1) + "] ");
|
|
775 |
out.print(meth.declaringType().name());
|
|
776 |
out.print('.');
|
|
777 |
out.print(meth.name());
|
|
778 |
out.print(" (");
|
51
|
779 |
if (meth.isNative()) {
|
2
|
780 |
out.print("native method");
|
|
781 |
} else if (loc.lineNumber() != -1) {
|
|
782 |
try {
|
|
783 |
out.print(loc.sourceName());
|
|
784 |
} catch (AbsentInformationException e) {
|
|
785 |
out.print("<unknown>");
|
|
786 |
}
|
|
787 |
out.print(':');
|
|
788 |
out.print(loc.lineNumber());
|
|
789 |
}
|
|
790 |
out.print(')');
|
|
791 |
if (showPC) {
|
|
792 |
long pc = loc.codeIndex();
|
|
793 |
if (pc != -1) {
|
|
794 |
out.print(", pc = " + pc);
|
|
795 |
}
|
|
796 |
}
|
|
797 |
out.println();
|
|
798 |
}
|
|
799 |
out.show();
|
|
800 |
}
|
|
801 |
}
|
|
802 |
|
|
803 |
private void listEventRequests() throws NoSessionException {
|
|
804 |
// Print set breakpoints
|
51
|
805 |
List<EventRequestSpec> specs = runtime.eventRequestSpecs();
|
|
806 |
if (specs.isEmpty()) {
|
2
|
807 |
env.notice("No breakpoints/watchpoints/exceptions set.");
|
|
808 |
} else {
|
|
809 |
OutputSink out = env.getOutputSink();
|
|
810 |
out.println("Current breakpoints/watchpoints/exceptions set:");
|
51
|
811 |
for (EventRequestSpec bp : specs) {
|
2
|
812 |
out.println("\t" + bp);
|
|
813 |
}
|
|
814 |
out.show();
|
|
815 |
}
|
|
816 |
}
|
|
817 |
|
|
818 |
private BreakpointSpec parseBreakpointSpec(String bptSpec) {
|
|
819 |
StringTokenizer t = new StringTokenizer(bptSpec);
|
|
820 |
BreakpointSpec bpSpec = null;
|
|
821 |
// try {
|
|
822 |
String token = t.nextToken("@:( \t\n\r");
|
|
823 |
// We can't use hasMoreTokens here because it will cause any leading
|
|
824 |
// paren to be lost.
|
|
825 |
String rest;
|
|
826 |
try {
|
|
827 |
rest = t.nextToken("").trim();
|
|
828 |
} catch (NoSuchElementException e) {
|
|
829 |
rest = null;
|
|
830 |
}
|
|
831 |
if ((rest != null) && rest.startsWith("@")) {
|
|
832 |
t = new StringTokenizer(rest.substring(1));
|
|
833 |
String sourceName = token;
|
|
834 |
String lineToken = t.nextToken();
|
|
835 |
int lineNumber = Integer.valueOf(lineToken).intValue();
|
|
836 |
if (t.hasMoreTokens()) {
|
|
837 |
return null;
|
|
838 |
}
|
|
839 |
bpSpec = runtime.createSourceLineBreakpoint(sourceName,
|
|
840 |
lineNumber);
|
|
841 |
} else if ((rest != null) && rest.startsWith(":")) {
|
|
842 |
t = new StringTokenizer(rest.substring(1));
|
|
843 |
String classId = token;
|
|
844 |
String lineToken = t.nextToken();
|
|
845 |
int lineNumber = Integer.valueOf(lineToken).intValue();
|
|
846 |
if (t.hasMoreTokens()) {
|
|
847 |
return null;
|
|
848 |
}
|
|
849 |
bpSpec = runtime.createClassLineBreakpoint(classId, lineNumber);
|
|
850 |
} else {
|
|
851 |
// Try stripping method from class.method token.
|
|
852 |
int idot = token.lastIndexOf(".");
|
|
853 |
if ( (idot <= 0) || /* No dot or dot in first char */
|
|
854 |
(idot >= token.length() - 1) ) { /* dot in last char */
|
|
855 |
return null;
|
|
856 |
}
|
|
857 |
String methodName = token.substring(idot + 1);
|
|
858 |
String classId = token.substring(0, idot);
|
|
859 |
List<String> argumentList = null;
|
|
860 |
if (rest != null) {
|
|
861 |
if (!rest.startsWith("(") || !rest.endsWith(")")) {
|
|
862 |
//### Should throw exception with error message
|
|
863 |
//out.println("Invalid method specification: "
|
|
864 |
// + methodName + rest);
|
|
865 |
return null;
|
|
866 |
}
|
|
867 |
// Trim the parens
|
|
868 |
//### What about spaces in arglist?
|
|
869 |
rest = rest.substring(1, rest.length() - 1);
|
|
870 |
argumentList = new ArrayList<String>();
|
|
871 |
t = new StringTokenizer(rest, ",");
|
|
872 |
while (t.hasMoreTokens()) {
|
|
873 |
argumentList.add(t.nextToken());
|
|
874 |
}
|
|
875 |
}
|
|
876 |
bpSpec = runtime.createMethodBreakpoint(classId,
|
|
877 |
methodName,
|
|
878 |
argumentList);
|
|
879 |
}
|
|
880 |
// } catch (Exception e) {
|
|
881 |
// env.error("Exception attempting to create breakpoint: " + e);
|
|
882 |
// return null;
|
|
883 |
// }
|
|
884 |
return bpSpec;
|
|
885 |
}
|
|
886 |
|
|
887 |
private void commandStop(StringTokenizer t) throws NoSessionException {
|
|
888 |
Location bploc;
|
|
889 |
String token;
|
|
890 |
|
|
891 |
if (!t.hasMoreTokens()) {
|
|
892 |
listEventRequests();
|
|
893 |
} else {
|
|
894 |
token = t.nextToken();
|
|
895 |
// Ignore optional "at" or "in" token.
|
|
896 |
// Allowed for backward compatibility.
|
|
897 |
if (token.equals("at") || token.equals("in")) {
|
|
898 |
if (t.hasMoreTokens()) {
|
|
899 |
token = t.nextToken();
|
|
900 |
} else {
|
|
901 |
env.error("Missing breakpoint specification.");
|
|
902 |
return;
|
|
903 |
}
|
|
904 |
}
|
|
905 |
BreakpointSpec bpSpec = parseBreakpointSpec(token);
|
|
906 |
if (bpSpec != null) {
|
|
907 |
//### Add sanity-checks for deferred breakpoint.
|
|
908 |
runtime.install(bpSpec);
|
|
909 |
} else {
|
|
910 |
env.error("Ill-formed breakpoint specification.");
|
|
911 |
}
|
|
912 |
}
|
|
913 |
}
|
|
914 |
|
|
915 |
private void commandClear(StringTokenizer t) throws NoSessionException {
|
|
916 |
if (!t.hasMoreTokens()) {
|
|
917 |
// Print set breakpoints
|
|
918 |
listEventRequests();
|
|
919 |
return;
|
|
920 |
}
|
|
921 |
//### need 'clear all'
|
|
922 |
BreakpointSpec bpSpec = parseBreakpointSpec(t.nextToken());
|
|
923 |
if (bpSpec != null) {
|
51
|
924 |
List<EventRequestSpec> specs = runtime.eventRequestSpecs();
|
|
925 |
|
|
926 |
if (specs.isEmpty()) {
|
2
|
927 |
env.notice("No breakpoints set.");
|
|
928 |
} else {
|
51
|
929 |
List<EventRequestSpec> toDelete = new ArrayList<EventRequestSpec>();
|
|
930 |
for (EventRequestSpec spec : specs) {
|
2
|
931 |
if (spec.equals(bpSpec)) {
|
|
932 |
toDelete.add(spec);
|
|
933 |
}
|
|
934 |
}
|
|
935 |
// The request used for matching should be found
|
|
936 |
if (toDelete.size() <= 1) {
|
|
937 |
env.notice("No matching breakpoint set.");
|
|
938 |
}
|
51
|
939 |
for (EventRequestSpec spec : toDelete) {
|
2
|
940 |
runtime.delete(spec);
|
|
941 |
}
|
|
942 |
}
|
|
943 |
} else {
|
|
944 |
env.error("Ill-formed breakpoint specification.");
|
|
945 |
}
|
|
946 |
}
|
|
947 |
|
|
948 |
// Command: list
|
|
949 |
|
|
950 |
private void commandList(StringTokenizer t) throws NoSessionException {
|
|
951 |
ThreadReference current = context.getCurrentThread();
|
|
952 |
if (current == null) {
|
|
953 |
env.error("No thread specified.");
|
|
954 |
return;
|
|
955 |
}
|
|
956 |
Location loc;
|
|
957 |
try {
|
|
958 |
StackFrame frame = context.getCurrentFrame(current);
|
|
959 |
if (frame == null) {
|
|
960 |
env.failure("Thread has not yet begun execution.");
|
|
961 |
return;
|
|
962 |
}
|
|
963 |
loc = frame.location();
|
|
964 |
} catch (VMNotInterruptedException e) {
|
|
965 |
env.failure("Target VM must be in interrupted state.");
|
|
966 |
return;
|
|
967 |
}
|
|
968 |
SourceModel source = sourceManager.sourceForLocation(loc);
|
|
969 |
if (source == null) {
|
|
970 |
if (loc.method().isNative()) {
|
|
971 |
env.failure("Current method is native.");
|
|
972 |
return;
|
|
973 |
}
|
|
974 |
env.failure("No source available for " + Utils.locationString(loc) + ".");
|
|
975 |
return;
|
|
976 |
}
|
|
977 |
ReferenceType refType = loc.declaringType();
|
|
978 |
int lineno = loc.lineNumber();
|
|
979 |
if (t.hasMoreTokens()) {
|
|
980 |
String id = t.nextToken();
|
|
981 |
// See if token is a line number.
|
|
982 |
try {
|
|
983 |
lineno = Integer.valueOf(id).intValue();
|
|
984 |
} catch (NumberFormatException nfe) {
|
|
985 |
// It isn't -- see if it's a method name.
|
51
|
986 |
List<Method> meths = refType.methodsByName(id);
|
2
|
987 |
if (meths == null || meths.size() == 0) {
|
|
988 |
env.failure(id +
|
|
989 |
" is not a valid line number or " +
|
|
990 |
"method name for class " +
|
|
991 |
refType.name());
|
|
992 |
return;
|
|
993 |
} else if (meths.size() > 1) {
|
|
994 |
env.failure(id +
|
|
995 |
" is an ambiguous method name in" +
|
|
996 |
refType.name());
|
|
997 |
return;
|
|
998 |
}
|
51
|
999 |
loc = meths.get(0).location();
|
2
|
1000 |
lineno = loc.lineNumber();
|
|
1001 |
}
|
|
1002 |
}
|
|
1003 |
int startLine = (lineno > 4) ? lineno - 4 : 1;
|
|
1004 |
int endLine = startLine + 9;
|
|
1005 |
String sourceLine = source.sourceLine(lineno);
|
|
1006 |
if (sourceLine == null) {
|
|
1007 |
env.failure("" +
|
|
1008 |
lineno +
|
|
1009 |
" is an invalid line number for " +
|
|
1010 |
refType.name());
|
|
1011 |
} else {
|
|
1012 |
OutputSink out = env.getOutputSink();
|
|
1013 |
for (int i = startLine; i <= endLine; i++) {
|
|
1014 |
sourceLine = source.sourceLine(i);
|
|
1015 |
if (sourceLine == null) {
|
|
1016 |
break;
|
|
1017 |
}
|
|
1018 |
out.print(i);
|
|
1019 |
out.print("\t");
|
|
1020 |
if (i == lineno) {
|
|
1021 |
out.print("=> ");
|
|
1022 |
} else {
|
|
1023 |
out.print(" ");
|
|
1024 |
}
|
|
1025 |
out.println(sourceLine);
|
|
1026 |
}
|
|
1027 |
out.show();
|
|
1028 |
}
|
|
1029 |
}
|
|
1030 |
|
|
1031 |
// Command: use
|
|
1032 |
// Get or set the source file path list.
|
|
1033 |
|
|
1034 |
private void commandUse(StringTokenizer t) {
|
|
1035 |
if (!t.hasMoreTokens()) {
|
|
1036 |
out.println(sourceManager.getSourcePath().asString());
|
|
1037 |
} else {
|
|
1038 |
//### Should throw exception for invalid path.
|
|
1039 |
//### E.g., vetoable property change.
|
|
1040 |
sourceManager.setSourcePath(new SearchPath(t.nextToken()));
|
|
1041 |
}
|
|
1042 |
}
|
|
1043 |
|
|
1044 |
// Command: sourcepath
|
|
1045 |
// Get or set the source file path list. (Alternate to 'use'.)
|
|
1046 |
|
|
1047 |
private void commandSourcepath(StringTokenizer t) {
|
|
1048 |
if (!t.hasMoreTokens()) {
|
|
1049 |
out.println(sourceManager.getSourcePath().asString());
|
|
1050 |
} else {
|
|
1051 |
//### Should throw exception for invalid path.
|
|
1052 |
//### E.g., vetoable property change.
|
|
1053 |
sourceManager.setSourcePath(new SearchPath(t.nextToken()));
|
|
1054 |
}
|
|
1055 |
}
|
|
1056 |
|
|
1057 |
// Command: classpath
|
|
1058 |
// Get or set the class file path list.
|
|
1059 |
|
|
1060 |
private void commandClasspath(StringTokenizer t) {
|
|
1061 |
if (!t.hasMoreTokens()) {
|
|
1062 |
out.println(classManager.getClassPath().asString());
|
|
1063 |
} else {
|
|
1064 |
//### Should throw exception for invalid path.
|
|
1065 |
//### E.g., vetoable property change.
|
|
1066 |
classManager.setClassPath(new SearchPath(t.nextToken()));
|
|
1067 |
}
|
|
1068 |
}
|
|
1069 |
|
|
1070 |
// Command: view
|
|
1071 |
// Display source for source file or class.
|
|
1072 |
|
|
1073 |
private void commandView(StringTokenizer t) throws NoSessionException {
|
|
1074 |
if (!t.hasMoreTokens()) {
|
|
1075 |
env.error("Argument required");
|
|
1076 |
} else {
|
|
1077 |
String name = t.nextToken();
|
|
1078 |
if (name.endsWith(".java") ||
|
|
1079 |
name.indexOf(File.separatorChar) >= 0) {
|
|
1080 |
env.viewSource(name);
|
|
1081 |
} else {
|
|
1082 |
//### JDI crashes taking line number for class.
|
|
1083 |
/*****
|
|
1084 |
ReferenceType cls = findClass(name);
|
|
1085 |
if (cls != null) {
|
|
1086 |
env.viewLocation(cls.location());
|
|
1087 |
} else {
|
|
1088 |
env.failure("No such class");
|
|
1089 |
}
|
|
1090 |
*****/
|
|
1091 |
String fileName = name.replace('.', File.separatorChar) + ".java";
|
|
1092 |
env.viewSource(fileName);
|
|
1093 |
}
|
|
1094 |
}
|
|
1095 |
}
|
|
1096 |
|
|
1097 |
// Command: locals
|
|
1098 |
// Print all local variables in current stack frame.
|
|
1099 |
|
|
1100 |
private void commandLocals() throws NoSessionException {
|
|
1101 |
ThreadReference current = context.getCurrentThread();
|
|
1102 |
if (current == null) {
|
|
1103 |
env.failure("No default thread specified: " +
|
|
1104 |
"use the \"thread\" command first.");
|
|
1105 |
return;
|
|
1106 |
}
|
|
1107 |
StackFrame frame;
|
|
1108 |
try {
|
|
1109 |
frame = context.getCurrentFrame(current);
|
|
1110 |
if (frame == null) {
|
|
1111 |
env.failure("Thread has not yet created any stack frames.");
|
|
1112 |
return;
|
|
1113 |
}
|
|
1114 |
} catch (VMNotInterruptedException e) {
|
|
1115 |
env.failure("Target VM must be in interrupted state.");
|
|
1116 |
return;
|
|
1117 |
}
|
|
1118 |
|
51
|
1119 |
List<LocalVariable> vars;
|
2
|
1120 |
try {
|
|
1121 |
vars = frame.visibleVariables();
|
|
1122 |
if (vars == null || vars.size() == 0) {
|
|
1123 |
env.failure("No local variables");
|
|
1124 |
return;
|
|
1125 |
}
|
|
1126 |
} catch (AbsentInformationException e) {
|
|
1127 |
env.failure("Local variable information not available." +
|
|
1128 |
" Compile with -g to generate variable information");
|
|
1129 |
return;
|
|
1130 |
}
|
|
1131 |
|
|
1132 |
OutputSink out = env.getOutputSink();
|
|
1133 |
out.println("Method arguments:");
|
51
|
1134 |
for (LocalVariable var : vars) {
|
2
|
1135 |
if (var.isArgument()) {
|
|
1136 |
printVar(out, var, frame);
|
|
1137 |
}
|
|
1138 |
}
|
|
1139 |
out.println("Local variables:");
|
51
|
1140 |
for (LocalVariable var : vars) {
|
2
|
1141 |
if (!var.isArgument()) {
|
|
1142 |
printVar(out, var, frame);
|
|
1143 |
}
|
|
1144 |
}
|
|
1145 |
out.show();
|
|
1146 |
return;
|
|
1147 |
}
|
|
1148 |
|
|
1149 |
/**
|
|
1150 |
* Command: monitor
|
|
1151 |
* Monitor an expression
|
|
1152 |
*/
|
|
1153 |
private void commandMonitor(StringTokenizer t) throws NoSessionException {
|
|
1154 |
if (!t.hasMoreTokens()) {
|
|
1155 |
env.error("Argument required");
|
|
1156 |
} else {
|
|
1157 |
env.getMonitorListModel().add(t.nextToken(""));
|
|
1158 |
}
|
|
1159 |
}
|
|
1160 |
|
|
1161 |
/**
|
|
1162 |
* Command: unmonitor
|
|
1163 |
* Unmonitor an expression
|
|
1164 |
*/
|
|
1165 |
private void commandUnmonitor(StringTokenizer t) throws NoSessionException {
|
|
1166 |
if (!t.hasMoreTokens()) {
|
|
1167 |
env.error("Argument required");
|
|
1168 |
} else {
|
|
1169 |
env.getMonitorListModel().remove(t.nextToken(""));
|
|
1170 |
}
|
|
1171 |
}
|
|
1172 |
|
|
1173 |
// Print a stack variable.
|
|
1174 |
|
|
1175 |
private void printVar(OutputSink out, LocalVariable var, StackFrame frame) {
|
|
1176 |
out.print(" " + var.name());
|
|
1177 |
if (var.isVisible(frame)) {
|
|
1178 |
Value val = frame.getValue(var);
|
|
1179 |
out.println(" = " + val.toString());
|
|
1180 |
} else {
|
|
1181 |
out.println(" is not in scope");
|
|
1182 |
}
|
|
1183 |
}
|
|
1184 |
|
|
1185 |
// Command: print
|
|
1186 |
// Evaluate an expression.
|
|
1187 |
|
|
1188 |
private void commandPrint(StringTokenizer t, boolean dumpObject) throws NoSessionException {
|
|
1189 |
if (!t.hasMoreTokens()) {
|
|
1190 |
//### Probably confused if expresion contains whitespace.
|
|
1191 |
env.error("No expression specified.");
|
|
1192 |
return;
|
|
1193 |
}
|
|
1194 |
ThreadReference current = context.getCurrentThread();
|
|
1195 |
if (current == null) {
|
|
1196 |
env.failure("No default thread specified: " +
|
|
1197 |
"use the \"thread\" command first.");
|
|
1198 |
return;
|
|
1199 |
}
|
|
1200 |
StackFrame frame;
|
|
1201 |
try {
|
|
1202 |
frame = context.getCurrentFrame(current);
|
|
1203 |
if (frame == null) {
|
|
1204 |
env.failure("Thread has not yet created any stack frames.");
|
|
1205 |
return;
|
|
1206 |
}
|
|
1207 |
} catch (VMNotInterruptedException e) {
|
|
1208 |
env.failure("Target VM must be in interrupted state.");
|
|
1209 |
return;
|
|
1210 |
}
|
|
1211 |
while (t.hasMoreTokens()) {
|
|
1212 |
String expr = t.nextToken("");
|
|
1213 |
Value val = null;
|
|
1214 |
try {
|
|
1215 |
val = runtime.evaluate(frame, expr);
|
|
1216 |
} catch(Exception e) {
|
|
1217 |
env.error("Exception: " + e);
|
|
1218 |
//### Fix this!
|
|
1219 |
}
|
|
1220 |
if (val == null) {
|
|
1221 |
return; // Error message already printed
|
|
1222 |
}
|
|
1223 |
OutputSink out = env.getOutputSink();
|
|
1224 |
if (dumpObject && (val instanceof ObjectReference) &&
|
|
1225 |
!(val instanceof StringReference)) {
|
|
1226 |
ObjectReference obj = (ObjectReference)val;
|
|
1227 |
ReferenceType refType = obj.referenceType();
|
|
1228 |
out.println(expr + " = " + val.toString() + " {");
|
|
1229 |
dump(out, obj, refType, refType);
|
|
1230 |
out.println("}");
|
|
1231 |
} else {
|
|
1232 |
out.println(expr + " = " + val.toString());
|
|
1233 |
}
|
|
1234 |
out.show();
|
|
1235 |
}
|
|
1236 |
}
|
|
1237 |
|
|
1238 |
private void dump(OutputSink out,
|
|
1239 |
ObjectReference obj, ReferenceType refType,
|
|
1240 |
ReferenceType refTypeBase) {
|
51
|
1241 |
for (Field field : refType.fields()) {
|
2
|
1242 |
out.print(" ");
|
|
1243 |
if (!refType.equals(refTypeBase)) {
|
|
1244 |
out.print(refType.name() + ".");
|
|
1245 |
}
|
|
1246 |
out.print(field.name() + ": ");
|
|
1247 |
Object o = obj.getValue(field);
|
|
1248 |
out.println((o == null) ? "null" : o.toString()); // Bug ID 4374471
|
|
1249 |
}
|
|
1250 |
if (refType instanceof ClassType) {
|
|
1251 |
ClassType sup = ((ClassType)refType).superclass();
|
|
1252 |
if (sup != null) {
|
|
1253 |
dump(out, obj, sup, refTypeBase);
|
|
1254 |
}
|
|
1255 |
} else if (refType instanceof InterfaceType) {
|
51
|
1256 |
for (InterfaceType sup : ((InterfaceType)refType).superinterfaces()) {
|
|
1257 |
dump(out, obj, sup, refTypeBase);
|
2
|
1258 |
}
|
|
1259 |
}
|
|
1260 |
}
|
|
1261 |
|
|
1262 |
/*
|
|
1263 |
* Display help message.
|
|
1264 |
*/
|
|
1265 |
|
|
1266 |
private void help() {
|
|
1267 |
out.println("** command list **");
|
|
1268 |
out.println("threads [threadgroup] -- list threads");
|
|
1269 |
out.println("thread <thread id> -- set default thread");
|
|
1270 |
out.println("suspend [thread id(s)] -- suspend threads (default: all)");
|
|
1271 |
out.println("resume [thread id(s)] -- resume threads (default: all)");
|
|
1272 |
out.println("where [thread id] | all -- dump a thread's stack");
|
|
1273 |
out.println("wherei [thread id] | all -- dump a thread's stack, with pc info");
|
|
1274 |
out.println("threadgroups -- list threadgroups");
|
|
1275 |
out.println("threadgroup <name> -- set current threadgroup\n");
|
|
1276 |
// out.println("print <expression> -- print value of expression");
|
|
1277 |
out.println("dump <expression> -- print all object information\n");
|
|
1278 |
// out.println("eval <expression> -- evaluate expression (same as print)");
|
|
1279 |
out.println("locals -- print all local variables in current stack frame\n");
|
|
1280 |
out.println("classes -- list currently known classes");
|
|
1281 |
out.println("methods <class id> -- list a class's methods\n");
|
|
1282 |
out.println("stop [in] <class id>.<method>[(argument_type,...)] -- set a breakpoint in a method");
|
|
1283 |
out.println("stop [at] <class id>:<line> -- set a breakpoint at a line");
|
|
1284 |
out.println("up [n frames] -- move up a thread's stack");
|
|
1285 |
out.println("down [n frames] -- move down a thread's stack");
|
|
1286 |
out.println("frame <frame-id> -- to a frame");
|
|
1287 |
out.println("clear <class id>.<method>[(argument_type,...)] -- clear a breakpoint in a method");
|
|
1288 |
out.println("clear <class id>:<line> -- clear a breakpoint at a line");
|
|
1289 |
out.println("clear -- list breakpoints");
|
|
1290 |
out.println("step -- execute current line");
|
|
1291 |
out.println("step up -- execute until the current method returns to its caller");
|
|
1292 |
out.println("stepi -- execute current instruction");
|
|
1293 |
out.println("next -- step one line (step OVER calls)");
|
|
1294 |
out.println("nexti -- step one instruction (step OVER calls)");
|
|
1295 |
out.println("cont -- continue execution from breakpoint\n");
|
|
1296 |
// out.println("catch <class id> -- break for the specified exception");
|
|
1297 |
// out.println("ignore <class id> -- ignore when the specified exception\n");
|
|
1298 |
out.println("view classname|filename -- display source file");
|
|
1299 |
out.println("list [line number|method] -- print source code context at line or method");
|
|
1300 |
out.println("use <source file path> -- display or change the source path\n");
|
|
1301 |
//### new
|
|
1302 |
out.println("sourcepath <source file path> -- display or change the source path\n");
|
|
1303 |
//### new
|
|
1304 |
out.println("classpath <class file path> -- display or change the class path\n");
|
|
1305 |
out.println("monitor <expression> -- evaluate an expression each time the program stops\n");
|
|
1306 |
out.println("unmonitor <monitor#> -- delete a monitor\n");
|
|
1307 |
out.println("read <filename> -- read and execute a command file\n");
|
|
1308 |
// out.println("memory -- report memory usage");
|
|
1309 |
// out.println("gc -- free unused objects\n");
|
|
1310 |
out.println("run <class> [args] -- start execution of a Java class");
|
|
1311 |
out.println("run -- re-execute last class run");
|
|
1312 |
out.println("load <class> [args] -- start execution of a Java class, initially suspended");
|
|
1313 |
out.println("load -- re-execute last class run, initially suspended");
|
|
1314 |
out.println("attach <portname> -- debug existing process\n");
|
|
1315 |
out.println("detach -- detach from debuggee process\n");
|
|
1316 |
out.println("kill <thread(group)> -- kill a thread or threadgroup\n");
|
|
1317 |
out.println("!! -- repeat last command");
|
|
1318 |
out.println("help (or ?) -- list commands");
|
|
1319 |
out.println("exit (or quit) -- exit debugger");
|
|
1320 |
}
|
|
1321 |
|
|
1322 |
/*
|
|
1323 |
* Execute a command.
|
|
1324 |
*/
|
|
1325 |
|
|
1326 |
public void executeCommand(String command) {
|
|
1327 |
//### Treatment of 'out' here is dirty...
|
|
1328 |
out = env.getOutputSink();
|
|
1329 |
if (echo) {
|
|
1330 |
out.println(">>> " + command);
|
|
1331 |
}
|
|
1332 |
StringTokenizer t = new StringTokenizer(command);
|
|
1333 |
try {
|
|
1334 |
String cmd;
|
|
1335 |
if (t.hasMoreTokens()) {
|
|
1336 |
cmd = t.nextToken().toLowerCase();
|
|
1337 |
lastCommand = cmd;
|
|
1338 |
} else {
|
|
1339 |
cmd = lastCommand;
|
|
1340 |
}
|
|
1341 |
if (cmd.equals("print")) {
|
|
1342 |
commandPrint(t, false);
|
|
1343 |
} else if (cmd.equals("eval")) {
|
|
1344 |
commandPrint(t, false);
|
|
1345 |
} else if (cmd.equals("dump")) {
|
|
1346 |
commandPrint(t, true);
|
|
1347 |
} else if (cmd.equals("locals")) {
|
|
1348 |
commandLocals();
|
|
1349 |
} else if (cmd.equals("classes")) {
|
|
1350 |
commandClasses();
|
|
1351 |
} else if (cmd.equals("methods")) {
|
|
1352 |
commandMethods(t);
|
|
1353 |
} else if (cmd.equals("threads")) {
|
|
1354 |
commandThreads(t);
|
|
1355 |
} else if (cmd.equals("thread")) {
|
|
1356 |
commandThread(t);
|
|
1357 |
} else if (cmd.equals("suspend")) {
|
|
1358 |
commandSuspend(t);
|
|
1359 |
} else if (cmd.equals("resume")) {
|
|
1360 |
commandResume(t);
|
|
1361 |
} else if (cmd.equals("cont")) {
|
|
1362 |
commandCont();
|
|
1363 |
} else if (cmd.equals("threadgroups")) {
|
|
1364 |
commandThreadGroups();
|
|
1365 |
} else if (cmd.equals("threadgroup")) {
|
|
1366 |
commandThreadGroup(t);
|
|
1367 |
} else if (cmd.equals("run")) {
|
|
1368 |
commandRun(t);
|
|
1369 |
} else if (cmd.equals("load")) {
|
|
1370 |
commandLoad(t);
|
|
1371 |
} else if (cmd.equals("connect")) {
|
|
1372 |
commandConnect(t);
|
|
1373 |
} else if (cmd.equals("attach")) {
|
|
1374 |
commandAttach(t);
|
|
1375 |
} else if (cmd.equals("detach")) {
|
|
1376 |
commandDetach(t);
|
|
1377 |
} else if (cmd.equals("interrupt")) {
|
|
1378 |
commandInterrupt(t);
|
|
1379 |
//### Not implemented.
|
|
1380 |
// } else if (cmd.equals("catch")) {
|
|
1381 |
// commandCatchException(t);
|
|
1382 |
//### Not implemented.
|
|
1383 |
// } else if (cmd.equals("ignore")) {
|
|
1384 |
// commandIgnoreException(t);
|
|
1385 |
} else if (cmd.equals("step")) {
|
|
1386 |
commandStep(t);
|
|
1387 |
} else if (cmd.equals("stepi")) {
|
|
1388 |
commandStepi();
|
|
1389 |
} else if (cmd.equals("next")) {
|
|
1390 |
commandNext();
|
|
1391 |
} else if (cmd.equals("nexti")) {
|
|
1392 |
commandNexti();
|
|
1393 |
} else if (cmd.equals("kill")) {
|
|
1394 |
commandKill(t);
|
|
1395 |
} else if (cmd.equals("where")) {
|
|
1396 |
commandWhere(t, false);
|
|
1397 |
} else if (cmd.equals("wherei")) {
|
|
1398 |
commandWhere(t, true);
|
|
1399 |
} else if (cmd.equals("up")) {
|
|
1400 |
commandUp(t);
|
|
1401 |
} else if (cmd.equals("down")) {
|
|
1402 |
commandDown(t);
|
|
1403 |
} else if (cmd.equals("frame")) {
|
|
1404 |
commandFrame(t);
|
|
1405 |
} else if (cmd.equals("stop")) {
|
|
1406 |
commandStop(t);
|
|
1407 |
} else if (cmd.equals("clear")) {
|
|
1408 |
commandClear(t);
|
|
1409 |
} else if (cmd.equals("list")) {
|
|
1410 |
commandList(t);
|
|
1411 |
} else if (cmd.equals("use")) {
|
|
1412 |
commandUse(t);
|
|
1413 |
} else if (cmd.equals("sourcepath")) {
|
|
1414 |
commandSourcepath(t);
|
|
1415 |
} else if (cmd.equals("classpath")) {
|
|
1416 |
commandClasspath(t);
|
|
1417 |
} else if (cmd.equals("monitor")) {
|
|
1418 |
commandMonitor(t);
|
|
1419 |
} else if (cmd.equals("unmonitor")) {
|
|
1420 |
commandUnmonitor(t);
|
|
1421 |
} else if (cmd.equals("view")) {
|
|
1422 |
commandView(t);
|
|
1423 |
// } else if (cmd.equals("read")) {
|
|
1424 |
// readCommand(t);
|
|
1425 |
} else if (cmd.equals("help") || cmd.equals("?")) {
|
|
1426 |
help();
|
|
1427 |
} else if (cmd.equals("quit") || cmd.equals("exit")) {
|
|
1428 |
try {
|
|
1429 |
runtime.detach();
|
|
1430 |
} catch (NoSessionException e) {
|
|
1431 |
// ignore
|
|
1432 |
}
|
|
1433 |
env.terminate();
|
|
1434 |
} else {
|
|
1435 |
//### Dubious repeat-count feature inherited from 'jdb'
|
|
1436 |
if (t.hasMoreTokens()) {
|
|
1437 |
try {
|
|
1438 |
int repeat = Integer.parseInt(cmd);
|
|
1439 |
String subcom = t.nextToken("");
|
|
1440 |
while (repeat-- > 0) {
|
|
1441 |
executeCommand(subcom);
|
|
1442 |
}
|
|
1443 |
return;
|
|
1444 |
} catch (NumberFormatException exc) {
|
|
1445 |
}
|
|
1446 |
}
|
|
1447 |
out.println("huh? Try help...");
|
|
1448 |
out.flush();
|
|
1449 |
}
|
|
1450 |
} catch (NoSessionException e) {
|
|
1451 |
out.println("There is no currently attached VM session.");
|
|
1452 |
out.flush();
|
|
1453 |
} catch (Exception e) {
|
|
1454 |
out.println("Internal exception: " + e.toString());
|
|
1455 |
out.flush();
|
|
1456 |
System.out.println("JDB internal exception: " + e.toString());
|
|
1457 |
e.printStackTrace();
|
|
1458 |
}
|
|
1459 |
out.show();
|
|
1460 |
}
|
|
1461 |
}
|