Mercurial > hg > truffle
annotate agent/src/share/classes/sun/jvm/hotspot/CommandProcessor.java @ 6782:5a98bf7d847b
6879063: SA should use hsdis for disassembly
Summary: We should in SA to use hsdis for it like the JVM does to replace the current java based disassembler.
Reviewed-by: twisti, jrose, sla
Contributed-by: yumin.qi@oracle.com
author | minqi |
---|---|
date | Mon, 24 Sep 2012 12:44:00 -0700 |
parents | da91efe96a93 |
children | bd7a7ce2e264 |
rev | line source |
---|---|
0 | 1 /* |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
0 | 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. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1385
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1385
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1385
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 package sun.jvm.hotspot; | |
26 | |
27 import java.io.*; | |
28 import java.math.*; | |
29 import java.util.*; | |
30 import java.util.regex.*; | |
31 | |
32 import sun.jvm.hotspot.types.Type; | |
33 import sun.jvm.hotspot.types.Field; | |
34 import sun.jvm.hotspot.HotSpotTypeDataBase; | |
35 import sun.jvm.hotspot.types.basic.BasicType; | |
36 import sun.jvm.hotspot.types.CIntegerType; | |
37 import sun.jvm.hotspot.code.*; | |
38 import sun.jvm.hotspot.compiler.*; | |
39 import sun.jvm.hotspot.debugger.*; | |
40 import sun.jvm.hotspot.interpreter.*; | |
41 import sun.jvm.hotspot.memory.*; | |
42 import sun.jvm.hotspot.oops.*; | |
3939 | 43 import sun.jvm.hotspot.opto.*; |
44 import sun.jvm.hotspot.ci.*; | |
6782 | 45 import sun.jvm.hotspot.asm.*; |
0 | 46 import sun.jvm.hotspot.runtime.*; |
47 import sun.jvm.hotspot.utilities.*; | |
48 import sun.jvm.hotspot.utilities.soql.*; | |
49 import sun.jvm.hotspot.ui.classbrowser.*; | |
50 import sun.jvm.hotspot.ui.tree.*; | |
51 import sun.jvm.hotspot.tools.*; | |
52 import sun.jvm.hotspot.tools.ObjectHistogram; | |
53 import sun.jvm.hotspot.tools.StackTrace; | |
3939 | 54 import sun.jvm.hotspot.tools.jcore.ClassDump; |
55 import sun.jvm.hotspot.tools.jcore.ClassFilter; | |
0 | 56 |
57 public class CommandProcessor { | |
58 public abstract static class DebuggerInterface { | |
59 public abstract HotSpotAgent getAgent(); | |
60 public abstract boolean isAttached(); | |
61 public abstract void attach(String pid); | |
62 public abstract void attach(String java, String core); | |
63 public abstract void detach(); | |
64 public abstract void reattach(); | |
65 } | |
66 | |
3939 | 67 public static class BootFilter implements ClassFilter { |
68 public boolean canInclude(InstanceKlass kls) { | |
69 return kls.getClassLoader() == null; | |
70 } | |
71 } | |
72 | |
73 public static class NonBootFilter implements ClassFilter { | |
74 private HashMap emitted = new HashMap(); | |
75 public boolean canInclude(InstanceKlass kls) { | |
76 if (kls.getClassLoader() == null) return false; | |
77 if (emitted.get(kls.getName()) != null) { | |
78 // Since multiple class loaders are being shoved | |
79 // together duplicate classes are a possibilty. For | |
80 // now just ignore them. | |
81 return false; | |
82 } | |
83 emitted.put(kls.getName(), kls); | |
84 return true; | |
85 } | |
86 } | |
87 | |
0 | 88 static class Tokens { |
89 final String input; | |
90 int i; | |
91 String[] tokens; | |
92 int length; | |
93 | |
94 String[] splitWhitespace(String cmd) { | |
95 String[] t = cmd.split("\\s"); | |
96 if (t.length == 1 && t[0].length() == 0) { | |
97 return new String[0]; | |
98 } | |
99 return t; | |
100 } | |
101 | |
102 void add(String s, ArrayList t) { | |
103 if (s.length() > 0) { | |
104 t.add(s); | |
105 } | |
106 } | |
107 | |
108 Tokens(String cmd) { | |
109 input = cmd; | |
110 | |
111 // check for quoting | |
112 int quote = cmd.indexOf('"'); | |
113 ArrayList t = new ArrayList(); | |
114 if (quote != -1) { | |
115 while (cmd.length() > 0) { | |
116 if (quote != -1) { | |
117 int endquote = cmd.indexOf('"', quote + 1); | |
118 if (endquote == -1) { | |
119 throw new RuntimeException("mismatched quotes: " + input); | |
120 } | |
121 | |
122 String before = cmd.substring(0, quote).trim(); | |
123 String quoted = cmd.substring(quote + 1, endquote); | |
124 cmd = cmd.substring(endquote + 1).trim(); | |
125 if (before.length() > 0) { | |
126 String[] w = splitWhitespace(before); | |
127 for (int i = 0; i < w.length; i++) { | |
128 add(w[i], t); | |
129 } | |
130 } | |
131 add(quoted, t); | |
132 quote = cmd.indexOf('"'); | |
133 } else { | |
134 String[] w = splitWhitespace(cmd); | |
135 for (int i = 0; i < w.length; i++) { | |
136 add(w[i], t); | |
137 } | |
138 cmd = ""; | |
139 | |
140 } | |
141 } | |
142 } else { | |
143 String[] w = splitWhitespace(cmd); | |
144 for (int i = 0; i < w.length; i++) { | |
145 add(w[i], t); | |
146 } | |
147 } | |
148 tokens = (String[])t.toArray(new String[0]); | |
149 i = 0; | |
150 length = tokens.length; | |
151 | |
152 //for (int i = 0; i < tokens.length; i++) { | |
153 // System.out.println("\"" + tokens[i] + "\""); | |
154 //} | |
155 } | |
156 | |
157 String nextToken() { | |
158 return tokens[i++]; | |
159 } | |
160 boolean hasMoreTokens() { | |
161 return i < length; | |
162 } | |
163 int countTokens() { | |
164 return length - i; | |
165 } | |
166 void trim(int n) { | |
167 if (length >= n) { | |
168 length -= n; | |
169 } else { | |
170 throw new IndexOutOfBoundsException(String.valueOf(n)); | |
171 } | |
172 } | |
173 String join(String sep) { | |
174 StringBuffer result = new StringBuffer(); | |
175 for (int w = i; w < length; w++) { | |
176 result.append(tokens[w]); | |
177 if (w + 1 < length) { | |
178 result.append(sep); | |
179 } | |
180 } | |
181 return result.toString(); | |
182 } | |
183 | |
184 String at(int i) { | |
185 if (i < 0 || i >= length) { | |
186 throw new IndexOutOfBoundsException(String.valueOf(i)); | |
187 } | |
188 return tokens[i]; | |
189 } | |
190 } | |
191 | |
192 | |
193 abstract class Command { | |
194 Command(String n, String u, boolean ok) { | |
195 name = n; | |
196 usage = u; | |
197 okIfDisconnected = ok; | |
198 } | |
199 | |
200 Command(String n, boolean ok) { | |
201 name = n; | |
202 usage = n; | |
203 okIfDisconnected = ok; | |
204 } | |
205 | |
206 final String name; | |
207 final String usage; | |
208 final boolean okIfDisconnected; | |
209 abstract void doit(Tokens t); | |
210 void usage() { | |
211 out.println("Usage: " + usage); | |
212 } | |
213 | |
214 void printOopValue(Oop oop) { | |
215 if (oop != null) { | |
216 Klass k = oop.getKlass(); | |
217 Symbol s = k.getName(); | |
218 if (s != null) { | |
219 out.print("Oop for " + s.asString() + " @ "); | |
220 } else { | |
221 out.print("Oop @ "); | |
222 } | |
223 Oop.printOopAddressOn(oop, out); | |
224 } else { | |
225 out.print("null"); | |
226 } | |
227 } | |
228 | |
229 void printNode(SimpleTreeNode node) { | |
230 int count = node.getChildCount(); | |
231 for (int i = 0; i < count; i++) { | |
232 try { | |
233 SimpleTreeNode field = node.getChild(i); | |
234 if (field instanceof OopTreeNodeAdapter) { | |
235 out.print(field); | |
236 out.print(" "); | |
237 printOopValue(((OopTreeNodeAdapter)field).getOop()); | |
238 out.println(); | |
239 } else { | |
240 out.println(field); | |
241 } | |
242 } catch (Exception e) { | |
243 out.println(); | |
244 out.println("Error: " + e); | |
245 if (verboseExceptions) { | |
246 e.printStackTrace(out); | |
247 } | |
248 } | |
249 } | |
250 } | |
251 } | |
252 | |
253 void quote(String s) { | |
254 if (s.indexOf(" ") == -1) { | |
255 out.print(s); | |
256 } else { | |
257 out.print("\""); | |
258 out.print(s); | |
259 out.print("\""); | |
260 } | |
261 } | |
262 | |
263 void dumpType(Type type) { | |
264 out.print("type "); | |
265 quote(type.getName()); | |
266 out.print(" "); | |
267 if (type.getSuperclass() != null) { | |
268 quote(type.getSuperclass().getName()); | |
269 out.print(" "); | |
270 } else { | |
271 out.print("null "); | |
272 } | |
273 out.print(type.isOopType()); | |
274 out.print(" "); | |
275 if (type.isCIntegerType()) { | |
276 out.print("true "); | |
277 out.print(((CIntegerType)type).isUnsigned()); | |
278 out.print(" "); | |
279 } else { | |
280 out.print("false false "); | |
281 } | |
282 out.print(type.getSize()); | |
283 out.println(); | |
284 } | |
285 | |
286 void dumpFields(Type type) { | |
3939 | 287 dumpFields(type, true); |
288 } | |
289 | |
290 void dumpFields(Type type, boolean allowStatic) { | |
0 | 291 Iterator i = type.getFields(); |
292 while (i.hasNext()) { | |
293 Field f = (Field) i.next(); | |
3939 | 294 if (!allowStatic && f.isStatic()) continue; |
0 | 295 out.print("field "); |
296 quote(type.getName()); | |
297 out.print(" "); | |
298 out.print(f.getName()); | |
299 out.print(" "); | |
300 quote(f.getType().getName()); | |
301 out.print(" "); | |
302 out.print(f.isStatic()); | |
303 out.print(" "); | |
304 if (f.isStatic()) { | |
305 out.print("0 "); | |
306 out.print(f.getStaticFieldAddress()); | |
307 } else { | |
308 out.print(f.getOffset()); | |
309 out.print(" 0x0"); | |
310 } | |
311 out.println(); | |
312 } | |
313 } | |
314 | |
315 | |
316 Address lookup(String symbol) { | |
317 if (symbol.indexOf("::") != -1) { | |
318 String[] parts = symbol.split("::"); | |
319 StringBuffer mangled = new StringBuffer("__1c"); | |
320 for (int i = 0; i < parts.length; i++) { | |
321 int len = parts[i].length(); | |
322 if (len >= 26) { | |
323 mangled.append((char)('a' + (len / 26))); | |
324 len = len % 26; | |
325 } | |
326 mangled.append((char)('A' + len)); | |
327 mangled.append(parts[i]); | |
328 } | |
329 mangled.append("_"); | |
330 symbol = mangled.toString(); | |
331 } | |
332 return VM.getVM().getDebugger().lookup(null, symbol); | |
333 } | |
334 | |
335 Address parseAddress(String addr) { | |
336 return VM.getVM().getDebugger().parseAddress(addr); | |
337 } | |
338 | |
339 private final Command[] commandList = { | |
340 new Command("reattach", true) { | |
341 public void doit(Tokens t) { | |
342 int tokens = t.countTokens(); | |
343 if (tokens != 0) { | |
344 usage(); | |
345 return; | |
346 } | |
347 preAttach(); | |
348 debugger.reattach(); | |
349 postAttach(); | |
350 } | |
351 }, | |
352 new Command("attach", "attach pid | exec core", true) { | |
353 public void doit(Tokens t) { | |
354 int tokens = t.countTokens(); | |
355 if (tokens == 1) { | |
356 preAttach(); | |
357 debugger.attach(t.nextToken()); | |
358 postAttach(); | |
359 } else if (tokens == 2) { | |
360 preAttach(); | |
361 debugger.attach(t.nextToken(), t.nextToken()); | |
362 postAttach(); | |
363 } else { | |
364 usage(); | |
365 } | |
366 } | |
367 }, | |
368 new Command("detach", false) { | |
369 public void doit(Tokens t) { | |
370 if (t.countTokens() != 0) { | |
371 usage(); | |
372 } else { | |
373 debugger.detach(); | |
374 } | |
375 } | |
376 }, | |
377 new Command("examine", "examine [ address/count ] | [ address,address]", false) { | |
378 Pattern args1 = Pattern.compile("^(0x[0-9a-f]+)(/([0-9]*)([a-z]*))?$"); | |
379 Pattern args2 = Pattern.compile("^(0x[0-9a-f]+),(0x[0-9a-f]+)(/[a-z]*)?$"); | |
380 | |
381 String fill(Address a, int width) { | |
382 String s = "0x0"; | |
383 if (a != null) { | |
384 s = a.toString(); | |
385 } | |
386 if (s.length() != width) { | |
387 return s.substring(0, 2) + "000000000000000000000".substring(0, width - s.length()) + s.substring(2); | |
388 } | |
389 return s; | |
390 } | |
391 | |
392 public void doit(Tokens t) { | |
393 if (t.countTokens() != 1) { | |
394 usage(); | |
395 } else { | |
396 String arg = t.nextToken(); | |
397 Matcher m1 = args1.matcher(arg); | |
398 Matcher m2 = args2.matcher(arg); | |
399 Address start = null; | |
400 Address end = null; | |
401 String format = ""; | |
402 int formatSize = (int)VM.getVM().getAddressSize(); | |
403 | |
404 if (m1.matches()) { | |
405 start = VM.getVM().getDebugger().parseAddress(m1.group(1)); | |
406 int count = 1; | |
407 if (m1.group(2) != null) { | |
408 count = Integer.parseInt(m1.group(3)); | |
409 } | |
410 end = start.addOffsetTo(count * formatSize); | |
411 } else if (m2.matches()) { | |
412 start = VM.getVM().getDebugger().parseAddress(m2.group(1)); | |
413 end = VM.getVM().getDebugger().parseAddress(m2.group(2)); | |
414 } else { | |
415 usage(); | |
416 return; | |
417 } | |
418 int line = 80; | |
419 int formatWidth = formatSize * 8 / 4 + 2; | |
420 | |
421 out.print(fill(start, formatWidth)); | |
422 out.print(": "); | |
423 int width = line - formatWidth - 2; | |
424 | |
425 boolean needsPrintln = true; | |
426 while (start != null && start.lessThan(end)) { | |
427 Address val = start.getAddressAt(0); | |
428 out.print(fill(val, formatWidth)); | |
429 needsPrintln = true; | |
430 width -= formatWidth; | |
431 start = start.addOffsetTo(formatSize); | |
432 if (width <= formatWidth) { | |
433 out.println(); | |
434 needsPrintln = false; | |
435 if (start.lessThan(end)) { | |
436 out.print(fill(start, formatWidth)); | |
437 out.print(": "); | |
438 width = line - formatWidth - 2; | |
439 } | |
440 } else { | |
441 out.print(" "); | |
442 width -= 1; | |
443 } | |
444 } | |
445 if (needsPrintln) { | |
446 out.println(); | |
447 } | |
448 } | |
449 } | |
450 }, | |
451 new Command("findpc", "findpc address", false) { | |
452 public void doit(Tokens t) { | |
453 if (t.countTokens() != 1) { | |
454 usage(); | |
455 } else { | |
456 Address a = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
457 PointerLocation loc = PointerFinder.find(a); | |
458 loc.printOn(out); | |
459 } | |
460 } | |
461 }, | |
2177
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
462 new Command("symbol", "symbol address", false) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
463 public void doit(Tokens t) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
464 if (t.countTokens() != 1) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
465 usage(); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
466 } else { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
467 Address a = VM.getVM().getDebugger().parseAddress(t.nextToken()); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
468 Symbol.create(a).printValueOn(out); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
469 out.println(); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
470 } |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
471 } |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
472 }, |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
473 new Command("symboltable", "symboltable name", false) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
474 public void doit(Tokens t) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
475 if (t.countTokens() != 1) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
476 usage(); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
477 } else { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
478 out.println(SymbolTable.getTheTable().probe(t.nextToken())); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
479 } |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
480 } |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
481 }, |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
482 new Command("symboldump", "symboldump", false) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
483 public void doit(Tokens t) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
484 SymbolTable.getTheTable().symbolsDo(new SymbolTable.SymbolVisitor() { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
485 public void visit(Symbol sym) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
486 sym.printValueOn(out); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
487 out.println(); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
488 } |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
489 }); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
490 } |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
491 }, |
3939 | 492 new Command("flags", "flags [ flag | -nd ]", false) { |
0 | 493 public void doit(Tokens t) { |
494 int tokens = t.countTokens(); | |
495 if (tokens != 0 && tokens != 1) { | |
496 usage(); | |
497 } else { | |
498 String name = tokens > 0 ? t.nextToken() : null; | |
3939 | 499 boolean nonDefault = false; |
500 if (name != null && name.equals("-nd")) { | |
501 name = null; | |
502 nonDefault = true; | |
503 } | |
0 | 504 |
505 VM.Flag[] flags = VM.getVM().getCommandLineFlags(); | |
506 if (flags == null) { | |
507 out.println("Command Flag info not available (use 1.4.1_03 or later)!"); | |
508 } else { | |
509 boolean printed = false; | |
510 for (int f = 0; f < flags.length; f++) { | |
511 VM.Flag flag = flags[f]; | |
512 if (name == null || flag.getName().equals(name)) { | |
3939 | 513 |
514 if (nonDefault && flag.getOrigin() == 0) { | |
515 // only print flags which aren't their defaults | |
516 continue; | |
517 } | |
518 out.println(flag.getName() + " = " + flag.getValue() + " " + flag.getOrigin()); | |
0 | 519 printed = true; |
520 } | |
521 } | |
522 if (name != null && !printed) { | |
523 out.println("Couldn't find flag: " + name); | |
524 } | |
525 } | |
526 } | |
527 } | |
528 }, | |
529 new Command("help", "help [ command ]", true) { | |
530 public void doit(Tokens t) { | |
531 int tokens = t.countTokens(); | |
532 Command cmd = null; | |
533 if (tokens == 1) { | |
534 cmd = findCommand(t.nextToken()); | |
535 } | |
536 | |
537 if (cmd != null) { | |
538 cmd.usage(); | |
539 } else if (tokens == 0) { | |
540 out.println("Available commands:"); | |
541 Object[] keys = commands.keySet().toArray(); | |
542 Arrays.sort(keys, new Comparator() { | |
543 public int compare(Object o1, Object o2) { | |
544 return o1.toString().compareTo(o2.toString()); | |
545 } | |
546 }); | |
547 for (int i = 0; i < keys.length; i++) { | |
548 out.print(" "); | |
549 out.println(((Command)commands.get(keys[i])).usage); | |
550 } | |
551 } | |
552 } | |
553 }, | |
554 new Command("history", "history", true) { | |
555 public void doit(Tokens t) { | |
556 int tokens = t.countTokens(); | |
557 if (tokens != 0 && (tokens != 1 || !t.nextToken().equals("-h"))) { | |
558 usage(); | |
559 return; | |
560 } | |
561 boolean printIndex = tokens == 0; | |
562 for (int i = 0; i < history.size(); i++) { | |
563 if (printIndex) out.print(i + " "); | |
564 out.println(history.get(i)); | |
565 } | |
566 } | |
567 }, | |
6782 | 568 // decode raw address |
569 new Command("dis", "dis address [length]", false) { | |
570 public void doit(Tokens t) { | |
571 int tokens = t.countTokens(); | |
572 if (tokens != 1 && tokens != 2) { | |
573 usage(); | |
574 return; | |
575 } | |
576 String name = t.nextToken(); | |
577 Address addr = null; | |
578 int len = 0x10; // default length | |
579 try { | |
580 addr = VM.getVM().getDebugger().parseAddress(name); | |
581 } catch (NumberFormatException e) { | |
582 out.println(e); | |
583 return; | |
584 } | |
585 if (tokens == 2) { | |
586 try { | |
587 len = Integer.parseInt(t.nextToken()); | |
588 } catch (NumberFormatException e) { | |
589 out.println(e); | |
590 return; | |
591 } | |
592 } | |
593 HTMLGenerator generator = new HTMLGenerator(false); | |
594 out.println(generator.genHTMLForRawDisassembly(addr, len)); | |
595 } | |
596 | |
597 }, | |
598 // decode codeblob or nmethod | |
599 new Command("disassemble", "disassemble address", false) { | |
600 public void doit(Tokens t) { | |
601 int tokens = t.countTokens(); | |
602 if (tokens != 1) { | |
603 usage(); | |
604 return; | |
605 } | |
606 String name = t.nextToken(); | |
607 Address addr = null; | |
608 try { | |
609 addr = VM.getVM().getDebugger().parseAddress(name); | |
610 } catch (NumberFormatException e) { | |
611 out.println(e); | |
612 return; | |
613 } | |
614 | |
615 HTMLGenerator generator = new HTMLGenerator(false); | |
616 out.println(generator.genHTML(addr)); | |
617 } | |
618 }, | |
619 // print Java bytecode disassembly | |
620 new Command("jdis", "jdis address", false) { | |
621 public void doit(Tokens t) { | |
622 int tokens = t.countTokens(); | |
623 if (tokens != 1) { | |
624 usage(); | |
625 return; | |
626 } | |
627 Address a = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
628 Method m = (Method)Metadata.instantiateWrapperFor(a); | |
629 HTMLGenerator html = new HTMLGenerator(false); | |
630 out.println(html.genHTML(m)); | |
631 } | |
632 }, | |
1385 | 633 new Command("revptrs", "revptrs address", false) { |
634 public void doit(Tokens t) { | |
635 int tokens = t.countTokens(); | |
636 if (tokens != 1 && (tokens != 2 || !t.nextToken().equals("-c"))) { | |
637 usage(); | |
638 return; | |
639 } | |
640 boolean chase = tokens == 2; | |
641 ReversePtrs revptrs = VM.getVM().getRevPtrs(); | |
642 if (revptrs == null) { | |
643 out.println("Computing reverse pointers..."); | |
644 ReversePtrsAnalysis analysis = new ReversePtrsAnalysis(); | |
645 final boolean[] complete = new boolean[1]; | |
646 HeapProgressThunk thunk = new HeapProgressThunk() { | |
647 public void heapIterationFractionUpdate(double d) {} | |
648 public synchronized void heapIterationComplete() { | |
649 complete[0] = true; | |
650 notify(); | |
651 } | |
652 }; | |
653 analysis.setHeapProgressThunk(thunk); | |
654 analysis.run(); | |
655 while (!complete[0]) { | |
656 synchronized (thunk) { | |
657 try { | |
658 thunk.wait(); | |
659 } catch (Exception e) { | |
660 } | |
661 } | |
662 } | |
663 revptrs = VM.getVM().getRevPtrs(); | |
664 out.println("Done."); | |
665 } | |
666 Address a = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
667 if (VM.getVM().getUniverse().heap().isInReserved(a)) { | |
668 OopHandle handle = a.addOffsetToAsOopHandle(0); | |
669 Oop oop = VM.getVM().getObjectHeap().newOop(handle); | |
670 ArrayList ptrs = revptrs.get(oop); | |
671 if (ptrs == null) { | |
672 out.println("no live references to " + a); | |
673 } else { | |
674 if (chase) { | |
675 while (ptrs.size() == 1) { | |
676 LivenessPathElement e = (LivenessPathElement)ptrs.get(0); | |
677 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
678 Oop.printOopValueOn(e.getObj(), new PrintStream(bos)); | |
679 out.println(bos.toString()); | |
680 ptrs = revptrs.get(e.getObj()); | |
681 } | |
682 } else { | |
683 for (int i = 0; i < ptrs.size(); i++) { | |
684 LivenessPathElement e = (LivenessPathElement)ptrs.get(i); | |
685 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
686 Oop.printOopValueOn(e.getObj(), new PrintStream(bos)); | |
687 out.println(bos.toString()); | |
688 oop = e.getObj(); | |
689 } | |
690 } | |
691 } | |
692 } | |
693 } | |
694 }, | |
3939 | 695 new Command("printmdo", "printmdo [ -a | expression ]", false) { |
696 // Print every MDO in the heap or the one referenced by expression. | |
697 public void doit(Tokens t) { | |
698 if (t.countTokens() != 1) { | |
699 usage(); | |
700 } else { | |
701 String s = t.nextToken(); | |
702 if (s.equals("-a")) { | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
703 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
704 sysDict.allClassesDo(new SystemDictionary.ClassVisitor() { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
705 public void visit(Klass k) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
706 if (k instanceof InstanceKlass) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
707 MethodArray methods = ((InstanceKlass)k).getMethods(); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
708 for (int i = 0; i < methods.length(); i++) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
709 Method m = methods.at(i); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
710 MethodData mdo = m.getMethodData(); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
711 if (mdo != null) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
712 out.println("MethodData " + mdo.getAddress() + " for " + |
3939 | 713 "method " + m.getMethodHolder().getName().asString() + "." + |
714 m.getName().asString() + | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
715 m.getSignature().asString() + "@" + m.getAddress()); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
716 mdo.printDataOn(out); |
3939 | 717 } |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
718 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
719 } |
3939 | 720 } |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
721 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
722 ); |
3939 | 723 } else { |
724 Address a = VM.getVM().getDebugger().parseAddress(s); | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
725 MethodData mdo = (MethodData) Metadata.instantiateWrapperFor(a); |
3939 | 726 mdo.printDataOn(out); |
727 } | |
728 } | |
729 } | |
730 }, | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
731 new Command("printall", "printall", false) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
732 // Print every MDO in the heap or the one referenced by expression. |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
733 public void doit(Tokens t) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
734 if (t.countTokens() != 0) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
735 usage(); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
736 } else { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
737 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
738 sysDict.allClassesDo(new SystemDictionary.ClassVisitor() { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
739 public void visit(Klass k) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
740 if (k instanceof InstanceKlass && ((InstanceKlass)k).getConstants().getCache() != null) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
741 MethodArray methods = ((InstanceKlass)k).getMethods(); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
742 for (int i = 0; i < methods.length(); i++) { |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
743 Method m = methods.at(i); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
744 HTMLGenerator gen = new HTMLGenerator(false); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
745 out.println(gen.genHTML(m)); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
746 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
747 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
748 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
749 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
750 ); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
751 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
752 } |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
753 }, |
3939 | 754 new Command("dumpideal", "dumpideal { -a | id }", false) { |
755 // Do a full dump of the nodes reachabile from root in each compiler thread. | |
756 public void doit(Tokens t) { | |
757 if (t.countTokens() != 1) { | |
758 usage(); | |
759 } else { | |
760 String name = t.nextToken(); | |
761 boolean all = name.equals("-a"); | |
762 Threads threads = VM.getVM().getThreads(); | |
763 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
764 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
765 thread.printThreadIDOn(new PrintStream(bos)); | |
766 if (all || bos.toString().equals(name)) { | |
767 if (thread instanceof CompilerThread) { | |
768 CompilerThread ct = (CompilerThread)thread; | |
769 out.println(ct); | |
770 ciEnv env = ct.env(); | |
771 if (env != null) { | |
772 Compile c = env.compilerData(); | |
773 c.root().dump(9999, out); | |
774 } else { | |
775 out.println(" not compiling"); | |
776 } | |
777 } | |
778 } | |
779 } | |
780 } | |
781 } | |
782 }, | |
783 new Command("dumpcfg", "dumpcfg { -a | id }", false) { | |
784 // Dump the PhaseCFG for every compiler thread that has one live. | |
785 public void doit(Tokens t) { | |
786 if (t.countTokens() != 1) { | |
787 usage(); | |
788 } else { | |
789 String name = t.nextToken(); | |
790 boolean all = name.equals("-a"); | |
791 Threads threads = VM.getVM().getThreads(); | |
792 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
793 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
794 thread.printThreadIDOn(new PrintStream(bos)); | |
795 if (all || bos.toString().equals(name)) { | |
796 if (thread instanceof CompilerThread) { | |
797 CompilerThread ct = (CompilerThread)thread; | |
798 out.println(ct); | |
799 ciEnv env = ct.env(); | |
800 if (env != null) { | |
801 Compile c = env.compilerData(); | |
802 c.cfg().dump(out); | |
803 } | |
804 } | |
805 } | |
806 } | |
807 } | |
808 } | |
809 }, | |
810 new Command("dumpilt", "dumpilt { -a | id }", false) { | |
811 // dumps the InlineTree of a C2 compile | |
812 public void doit(Tokens t) { | |
813 if (t.countTokens() != 1) { | |
814 usage(); | |
815 } else { | |
816 String name = t.nextToken(); | |
817 boolean all = name.equals("-a"); | |
818 Threads threads = VM.getVM().getThreads(); | |
819 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
820 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
821 thread.printThreadIDOn(new PrintStream(bos)); | |
822 if (all || bos.toString().equals(name)) { | |
823 if (thread instanceof CompilerThread) { | |
824 CompilerThread ct = (CompilerThread)thread; | |
825 ciEnv env = ct.env(); | |
826 if (env != null) { | |
827 Compile c = env.compilerData(); | |
828 InlineTree ilt = c.ilt(); | |
829 if (ilt != null) { | |
830 ilt.print(out); | |
831 } | |
832 } | |
833 } | |
834 } | |
835 } | |
836 } | |
837 } | |
838 }, | |
839 new Command("vmstructsdump", "vmstructsdump", false) { | |
840 public void doit(Tokens t) { | |
841 if (t.countTokens() != 0) { | |
842 usage(); | |
843 return; | |
844 } | |
845 | |
846 // Dump a copy of the type database in a form that can | |
847 // be read back. | |
848 Iterator i = agent.getTypeDataBase().getTypes(); | |
849 // Make sure the types are emitted in an order than can be read back in | |
850 HashSet emitted = new HashSet(); | |
851 Stack pending = new Stack(); | |
852 while (i.hasNext()) { | |
853 Type n = (Type)i.next(); | |
854 if (emitted.contains(n.getName())) { | |
855 continue; | |
856 } | |
857 | |
858 while (n != null && !emitted.contains(n.getName())) { | |
859 pending.push(n); | |
860 n = n.getSuperclass(); | |
861 } | |
862 while (!pending.empty()) { | |
863 n = (Type)pending.pop(); | |
864 dumpType(n); | |
865 emitted.add(n.getName()); | |
866 } | |
867 } | |
868 i = agent.getTypeDataBase().getTypes(); | |
869 while (i.hasNext()) { | |
870 dumpFields((Type)i.next(), false); | |
871 } | |
872 } | |
873 }, | |
874 | |
0 | 875 new Command("inspect", "inspect expression", false) { |
876 public void doit(Tokens t) { | |
877 if (t.countTokens() != 1) { | |
878 usage(); | |
879 } else { | |
880 Address a = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
881 SimpleTreeNode node = null; | |
882 if (VM.getVM().getUniverse().heap().isInReserved(a)) { | |
883 OopHandle handle = a.addOffsetToAsOopHandle(0); | |
884 Oop oop = VM.getVM().getObjectHeap().newOop(handle); | |
885 node = new OopTreeNodeAdapter(oop, null); | |
886 | |
887 out.println("instance of " + node.getValue() + " @ " + a + | |
888 " (size = " + oop.getObjectSize() + ")"); | |
889 } else if (VM.getVM().getCodeCache().contains(a)) { | |
890 CodeBlob blob = VM.getVM().getCodeCache().findBlobUnsafe(a); | |
891 a = blob.headerBegin(); | |
892 } | |
893 if (node == null) { | |
894 Type type = VM.getVM().getTypeDataBase().guessTypeForAddress(a); | |
895 if (type != null) { | |
896 out.println("Type is " + type.getName() + " (size of " + type.getSize() + ")"); | |
897 node = new CTypeTreeNodeAdapter(a, type, null); | |
898 } | |
899 } | |
900 if (node != null) { | |
901 printNode(node); | |
902 } | |
903 } | |
904 } | |
905 }, | |
906 new Command("jhisto", "jhisto", false) { | |
907 public void doit(Tokens t) { | |
908 ObjectHistogram histo = new ObjectHistogram(); | |
909 histo.run(out, err); | |
910 } | |
911 }, | |
912 new Command("jstack", "jstack [-v]", false) { | |
913 public void doit(Tokens t) { | |
914 boolean verbose = false; | |
915 if (t.countTokens() > 0 && t.nextToken().equals("-v")) { | |
916 verbose = true; | |
917 } | |
918 StackTrace jstack = new StackTrace(verbose, true); | |
919 jstack.run(out); | |
920 } | |
921 }, | |
922 new Command("print", "print expression", false) { | |
923 public void doit(Tokens t) { | |
924 if (t.countTokens() != 1) { | |
925 usage(); | |
926 } else { | |
927 Address a = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
928 HTMLGenerator gen = new HTMLGenerator(false); | |
929 out.println(gen.genHTML(a)); | |
930 } | |
931 } | |
932 }, | |
933 new Command("printas", "printas type expression", false) { | |
934 public void doit(Tokens t) { | |
935 if (t.countTokens() != 2) { | |
936 usage(); | |
937 } else { | |
938 Type type = agent.getTypeDataBase().lookupType(t.nextToken()); | |
939 Address a = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
940 CTypeTreeNodeAdapter node = new CTypeTreeNodeAdapter(a, type, null); | |
941 | |
942 out.println("pointer to " + type + " @ " + a + | |
943 " (size = " + type.getSize() + ")"); | |
944 printNode(node); | |
945 } | |
946 } | |
947 }, | |
948 new Command("printstatics", "printstatics [ type ]", false) { | |
949 public void doit(Tokens t) { | |
950 if (t.countTokens() > 1) { | |
951 usage(); | |
952 } else { | |
953 if (t.countTokens() == 0) { | |
954 out.println("All known static fields"); | |
955 printNode(new CTypeTreeNodeAdapter(agent.getTypeDataBase().getTypes())); | |
956 } else { | |
957 Type type = agent.getTypeDataBase().lookupType(t.nextToken()); | |
958 out.println("Static fields of " + type.getName()); | |
959 printNode(new CTypeTreeNodeAdapter(type)); | |
960 } | |
961 } | |
962 } | |
963 }, | |
964 new Command("pmap", "pmap", false) { | |
965 public void doit(Tokens t) { | |
966 PMap pmap = new PMap(); | |
967 pmap.run(out, debugger.getAgent().getDebugger()); | |
968 } | |
969 }, | |
970 new Command("pstack", "pstack [-v]", false) { | |
971 public void doit(Tokens t) { | |
972 boolean verbose = false; | |
973 if (t.countTokens() > 0 && t.nextToken().equals("-v")) { | |
974 verbose = true; | |
975 } | |
976 PStack pstack = new PStack(verbose, true); | |
977 pstack.run(out, debugger.getAgent().getDebugger()); | |
978 } | |
979 }, | |
980 new Command("quit", true) { | |
981 public void doit(Tokens t) { | |
982 if (t.countTokens() != 0) { | |
983 usage(); | |
984 } else { | |
985 debugger.detach(); | |
986 System.exit(0); | |
987 } | |
988 } | |
989 }, | |
990 new Command("echo", "echo [ true | false ]", true) { | |
991 public void doit(Tokens t) { | |
992 if (t.countTokens() == 0) { | |
993 out.println("echo is " + doEcho); | |
994 } else if (t.countTokens() == 1) { | |
995 doEcho = Boolean.valueOf(t.nextToken()).booleanValue(); | |
996 } else { | |
997 usage(); | |
998 } | |
999 } | |
1000 }, | |
1001 new Command("versioncheck", "versioncheck [ true | false ]", true) { | |
1002 public void doit(Tokens t) { | |
1003 if (t.countTokens() == 0) { | |
1004 out.println("versioncheck is " + | |
1005 (System.getProperty("sun.jvm.hotspot.runtime.VM.disableVersionCheck") == null)); | |
1006 } else if (t.countTokens() == 1) { | |
1007 if (Boolean.valueOf(t.nextToken()).booleanValue()) { | |
1008 System.setProperty("sun.jvm.hotspot.runtime.VM.disableVersionCheck", null); | |
1009 } else { | |
1010 System.setProperty("sun.jvm.hotspot.runtime.VM.disableVersionCheck", "true"); | |
1011 } | |
1012 } else { | |
1013 usage(); | |
1014 } | |
1015 } | |
1016 }, | |
1017 new Command("scanoops", "scanoops start end [ type ]", false) { | |
1018 public void doit(Tokens t) { | |
1019 if (t.countTokens() != 2 && t.countTokens() != 3) { | |
1020 usage(); | |
1021 } else { | |
1022 long stride = VM.getVM().getAddressSize(); | |
1023 Address base = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
1024 Address end = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
1025 Klass klass = null; | |
1026 if (t.countTokens() == 1) { | |
1027 klass = SystemDictionaryHelper.findInstanceKlass(t.nextToken()); | |
1028 } | |
1029 while (base != null && base.lessThan(end)) { | |
1030 long step = stride; | |
1031 OopHandle handle = base.addOffsetToAsOopHandle(0); | |
1032 if (RobustOopDeterminator.oopLooksValid(handle)) { | |
1033 try { | |
1034 Oop oop = VM.getVM().getObjectHeap().newOop(handle); | |
1035 if (klass == null || oop.getKlass().isSubtypeOf(klass)) | |
1036 out.println(handle.toString() + " " + oop.getKlass().getName().asString()); | |
1037 step = oop.getObjectSize(); | |
1038 } catch (UnknownOopException ex) { | |
1039 // ok | |
1040 } catch (RuntimeException ex) { | |
1041 ex.printStackTrace(); | |
1042 } | |
1043 } | |
1044 base = base.addOffsetTo(step); | |
1045 } | |
1046 } | |
1047 } | |
1048 }, | |
3939 | 1049 new Command("intConstant", "intConstant [ name [ value ] ]", true) { |
1050 public void doit(Tokens t) { | |
1051 if (t.countTokens() != 1 && t.countTokens() != 0 && t.countTokens() != 2) { | |
1052 usage(); | |
1053 return; | |
1054 } | |
1055 HotSpotTypeDataBase db = (HotSpotTypeDataBase)agent.getTypeDataBase(); | |
1056 if (t.countTokens() == 1) { | |
1057 out.println("intConstant " + name + " " + db.lookupIntConstant(name)); | |
1058 } else if (t.countTokens() == 0) { | |
1059 Iterator i = db.getIntConstants(); | |
1060 while (i.hasNext()) { | |
1061 String name = (String)i.next(); | |
1062 out.println("intConstant " + name + " " + db.lookupIntConstant(name)); | |
1063 } | |
1064 } else if (t.countTokens() == 2) { | |
1065 String name = t.nextToken(); | |
1066 Integer value = Integer.valueOf(t.nextToken()); | |
1067 db.addIntConstant(name, value); | |
1068 } | |
1069 } | |
1070 }, | |
1071 new Command("longConstant", "longConstant [ name [ value ] ]", true) { | |
1072 public void doit(Tokens t) { | |
1073 if (t.countTokens() != 1 && t.countTokens() != 0 && t.countTokens() != 2) { | |
1074 usage(); | |
1075 return; | |
1076 } | |
1077 HotSpotTypeDataBase db = (HotSpotTypeDataBase)agent.getTypeDataBase(); | |
1078 if (t.countTokens() == 1) { | |
1079 out.println("longConstant " + name + " " + db.lookupLongConstant(name)); | |
1080 } else if (t.countTokens() == 0) { | |
1081 Iterator i = db.getLongConstants(); | |
1082 while (i.hasNext()) { | |
1083 String name = (String)i.next(); | |
1084 out.println("longConstant " + name + " " + db.lookupLongConstant(name)); | |
1085 } | |
1086 } else if (t.countTokens() == 2) { | |
1087 String name = t.nextToken(); | |
1088 Long value = Long.valueOf(t.nextToken()); | |
1089 db.addLongConstant(name, value); | |
1090 } | |
1091 } | |
1092 }, | |
0 | 1093 new Command("field", "field [ type [ name fieldtype isStatic offset address ] ]", true) { |
1094 public void doit(Tokens t) { | |
1095 if (t.countTokens() != 1 && t.countTokens() != 0 && t.countTokens() != 6) { | |
1096 usage(); | |
1097 return; | |
1098 } | |
1099 if (t.countTokens() == 1) { | |
1100 Type type = agent.getTypeDataBase().lookupType(t.nextToken()); | |
1101 dumpFields(type); | |
1102 } else if (t.countTokens() == 0) { | |
1103 Iterator i = agent.getTypeDataBase().getTypes(); | |
1104 while (i.hasNext()) { | |
1105 dumpFields((Type)i.next()); | |
1106 } | |
1107 } else { | |
1108 BasicType containingType = (BasicType)agent.getTypeDataBase().lookupType(t.nextToken()); | |
1109 | |
1110 String fieldName = t.nextToken(); | |
1111 | |
1112 // The field's Type must already be in the database -- no exceptions | |
1113 Type fieldType = agent.getTypeDataBase().lookupType(t.nextToken()); | |
1114 | |
1115 boolean isStatic = Boolean.valueOf(t.nextToken()).booleanValue(); | |
1116 long offset = Long.parseLong(t.nextToken()); | |
1117 Address staticAddress = parseAddress(t.nextToken()); | |
1118 if (isStatic && staticAddress == null) { | |
1119 staticAddress = lookup(containingType.getName() + "::" + fieldName); | |
1120 } | |
1121 | |
1122 // check to see if the field already exists | |
1123 Iterator i = containingType.getFields(); | |
1124 while (i.hasNext()) { | |
1125 Field f = (Field) i.next(); | |
1126 if (f.getName().equals(fieldName)) { | |
1127 if (f.isStatic() != isStatic) { | |
1128 throw new RuntimeException("static/nonstatic mismatch: " + t.input); | |
1129 } | |
1130 if (!isStatic) { | |
1131 if (f.getOffset() != offset) { | |
1132 throw new RuntimeException("bad redefinition of field offset: " + t.input); | |
1133 } | |
1134 } else { | |
1135 if (!f.getStaticFieldAddress().equals(staticAddress)) { | |
1136 throw new RuntimeException("bad redefinition of field location: " + t.input); | |
1137 } | |
1138 } | |
1139 if (f.getType() != fieldType) { | |
1140 throw new RuntimeException("bad redefinition of field type: " + t.input); | |
1141 } | |
1142 return; | |
1143 } | |
1144 } | |
1145 | |
1146 // Create field by type | |
1147 HotSpotTypeDataBase db = (HotSpotTypeDataBase)agent.getTypeDataBase(); | |
1148 db.createField(containingType, | |
1149 fieldName, fieldType, | |
1150 isStatic, | |
1151 offset, | |
1152 staticAddress); | |
1153 | |
1154 } | |
1155 } | |
1156 | |
1157 }, | |
1158 new Command("tokenize", "tokenize ...", true) { | |
1159 public void doit(Tokens t) { | |
1160 while (t.hasMoreTokens()) { | |
1161 out.println("\"" + t.nextToken() + "\""); | |
1162 } | |
1163 } | |
1164 }, | |
1165 new Command("type", "type [ type [ name super isOop isInteger isUnsigned size ] ]", true) { | |
1166 public void doit(Tokens t) { | |
1167 if (t.countTokens() != 1 && t.countTokens() != 0 && t.countTokens() != 6) { | |
1168 usage(); | |
1169 return; | |
1170 } | |
1171 if (t.countTokens() == 6) { | |
1172 String typeName = t.nextToken(); | |
1173 String superclassName = t.nextToken(); | |
1174 if (superclassName.equals("null")) { | |
1175 superclassName = null; | |
1176 } | |
1177 boolean isOop = Boolean.valueOf(t.nextToken()).booleanValue(); | |
1178 boolean isInteger = Boolean.valueOf(t.nextToken()).booleanValue(); | |
1179 boolean isUnsigned = Boolean.valueOf(t.nextToken()).booleanValue(); | |
1180 long size = Long.parseLong(t.nextToken()); | |
1181 | |
1182 BasicType type = null; | |
1183 try { | |
1184 type = (BasicType)agent.getTypeDataBase().lookupType(typeName); | |
1185 } catch (RuntimeException e) { | |
1186 } | |
1187 if (type != null) { | |
1188 if (type.isOopType() != isOop) { | |
1189 throw new RuntimeException("oop mismatch in type definition: " + t.input); | |
1190 } | |
1191 if (type.isCIntegerType() != isInteger) { | |
1192 throw new RuntimeException("integer type mismatch in type definition: " + t.input); | |
1193 } | |
1194 if (type.isCIntegerType() && (((CIntegerType)type).isUnsigned()) != isUnsigned) { | |
1195 throw new RuntimeException("unsigned mismatch in type definition: " + t.input); | |
1196 } | |
1197 if (type.getSuperclass() == null) { | |
1198 if (superclassName != null) { | |
1199 if (type.getSize() == -1) { | |
1200 type.setSuperclass(agent.getTypeDataBase().lookupType(superclassName)); | |
1201 } else { | |
1202 throw new RuntimeException("unexpected superclass in type definition: " + t.input); | |
1203 } | |
1204 } | |
1205 } else { | |
1206 if (superclassName == null) { | |
1207 throw new RuntimeException("missing superclass in type definition: " + t.input); | |
1208 } | |
1209 if (!type.getSuperclass().getName().equals(superclassName)) { | |
1210 throw new RuntimeException("incorrect superclass in type definition: " + t.input); | |
1211 } | |
1212 } | |
1213 if (type.getSize() != size) { | |
1214 if (type.getSize() == -1) { | |
1215 type.setSize(size); | |
1216 } | |
1217 throw new RuntimeException("size mismatch in type definition: " + t.input); | |
1218 } | |
1219 return; | |
1220 } | |
1221 | |
1222 // Create type | |
1223 HotSpotTypeDataBase db = (HotSpotTypeDataBase)agent.getTypeDataBase(); | |
1224 db.createType(typeName, superclassName, isOop, isInteger, isUnsigned, size); | |
1225 } else if (t.countTokens() == 1) { | |
1226 Type type = agent.getTypeDataBase().lookupType(t.nextToken()); | |
1227 dumpType(type); | |
1228 } else { | |
1229 Iterator i = agent.getTypeDataBase().getTypes(); | |
1385 | 1230 // Make sure the types are emitted in an order than can be read back in |
1231 HashSet emitted = new HashSet(); | |
1232 Stack pending = new Stack(); | |
0 | 1233 while (i.hasNext()) { |
1385 | 1234 Type n = (Type)i.next(); |
1235 if (emitted.contains(n.getName())) { | |
1236 continue; | |
1237 } | |
1238 | |
1239 while (n != null && !emitted.contains(n.getName())) { | |
1240 pending.push(n); | |
1241 n = n.getSuperclass(); | |
1242 } | |
1243 while (!pending.empty()) { | |
1244 n = (Type)pending.pop(); | |
1245 dumpType(n); | |
1246 emitted.add(n.getName()); | |
1247 } | |
0 | 1248 } |
1249 } | |
1250 } | |
1251 | |
1252 }, | |
1253 new Command("source", "source filename", true) { | |
1254 public void doit(Tokens t) { | |
1255 if (t.countTokens() != 1) { | |
1256 usage(); | |
1257 return; | |
1258 } | |
1259 String file = t.nextToken(); | |
1260 BufferedReader savedInput = in; | |
1261 try { | |
1262 BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file))); | |
1263 in = input; | |
1264 run(false); | |
1265 } catch (Exception e) { | |
1266 out.println("Error: " + e); | |
1267 if (verboseExceptions) { | |
1268 e.printStackTrace(out); | |
1269 } | |
1270 } finally { | |
1271 in = savedInput; | |
1272 } | |
1273 | |
1274 } | |
1275 }, | |
1385 | 1276 new Command("search", "search [ heap | perm | rawheap | codecache | threads ] value", false) { |
0 | 1277 public void doit(Tokens t) { |
1278 if (t.countTokens() != 2) { | |
1279 usage(); | |
1385 | 1280 return; |
1281 } | |
1282 String type = t.nextToken(); | |
1283 final Address value = VM.getVM().getDebugger().parseAddress(t.nextToken()); | |
1284 final long stride = VM.getVM().getAddressSize(); | |
1285 if (type.equals("threads")) { | |
1286 Threads threads = VM.getVM().getThreads(); | |
1287 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
1288 Address base = thread.getBaseOfStackPointer(); | |
1289 Address end = thread.getLastJavaSP(); | |
1290 if (end == null) continue; | |
1291 if (end.lessThan(base)) { | |
1292 Address tmp = base; | |
1293 base = end; | |
1294 end = tmp; | |
1295 } | |
1296 out.println("Searching " + base + " " + end); | |
1297 while (base != null && base.lessThan(end)) { | |
1298 Address val = base.getAddressAt(0); | |
1299 if (AddressOps.equal(val, value)) { | |
1300 out.println(base); | |
1301 } | |
1302 base = base.addOffsetTo(stride); | |
1303 } | |
1304 } | |
1305 } else if (type.equals("rawheap")) { | |
1306 RawHeapVisitor iterator = new RawHeapVisitor() { | |
1307 public void prologue(long used) { | |
0 | 1308 } |
1385 | 1309 |
1310 public void visitAddress(Address addr) { | |
1311 Address val = addr.getAddressAt(0); | |
0 | 1312 if (AddressOps.equal(val, value)) { |
1385 | 1313 out.println("found at " + addr); |
0 | 1314 } |
1385 | 1315 } |
1316 public void visitCompOopAddress(Address addr) { | |
1317 Address val = addr.getCompOopAddressAt(0); | |
1318 if (AddressOps.equal(val, value)) { | |
1319 out.println("found at " + addr); | |
1320 } | |
0 | 1321 } |
1385 | 1322 public void epilogue() { |
1323 } | |
1324 }; | |
1325 VM.getVM().getObjectHeap().iterateRaw(iterator); | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3939
diff
changeset
|
1326 } else if (type.equals("heap")) { |
1385 | 1327 HeapVisitor iterator = new DefaultHeapVisitor() { |
1328 public boolean doObj(Oop obj) { | |
1329 int index = 0; | |
1330 Address start = obj.getHandle(); | |
1331 long end = obj.getObjectSize(); | |
1332 while (index < end) { | |
1333 Address val = start.getAddressAt(index); | |
0 | 1334 if (AddressOps.equal(val, value)) { |
1385 | 1335 out.println("found in " + obj.getHandle()); |
1336 break; | |
0 | 1337 } |
1385 | 1338 index += 4; |
0 | 1339 } |
1385 | 1340 return false; |
1341 } | |
1342 }; | |
1343 VM.getVM().getObjectHeap().iterate(iterator); | |
1344 } else if (type.equals("codecache")) { | |
1345 CodeCacheVisitor v = new CodeCacheVisitor() { | |
1346 public void prologue(Address start, Address end) { | |
1347 } | |
1348 public void visit(CodeBlob blob) { | |
1349 boolean printed = false; | |
1350 Address base = blob.getAddress(); | |
1351 Address end = base.addOffsetTo(blob.getSize()); | |
1352 while (base != null && base.lessThan(end)) { | |
1353 Address val = base.getAddressAt(0); | |
1354 if (AddressOps.equal(val, value)) { | |
1355 if (!printed) { | |
1356 printed = true; | |
3388
a80577f854f9
7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents:
2426
diff
changeset
|
1357 try { |
a80577f854f9
7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents:
2426
diff
changeset
|
1358 blob.printOn(out); |
a80577f854f9
7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents:
2426
diff
changeset
|
1359 } catch (Exception e) { |
a80577f854f9
7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents:
2426
diff
changeset
|
1360 out.println("Exception printing blob at " + base); |
a80577f854f9
7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents:
2426
diff
changeset
|
1361 e.printStackTrace(); |
a80577f854f9
7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents:
2426
diff
changeset
|
1362 } |
0 | 1363 } |
1385 | 1364 out.println("found at " + base + "\n"); |
0 | 1365 } |
1385 | 1366 base = base.addOffsetTo(stride); |
0 | 1367 } |
1385 | 1368 } |
1369 public void epilogue() { | |
1370 } | |
0 | 1371 |
1372 | |
1385 | 1373 }; |
1374 VM.getVM().getCodeCache().iterate(v); | |
0 | 1375 |
1376 } | |
1377 } | |
1378 }, | |
1040
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1379 new Command("dumpcodecache", "dumpcodecache", false) { |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1380 public void doit(Tokens t) { |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1381 if (t.countTokens() != 0) { |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1382 usage(); |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1383 } else { |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1384 final PrintStream fout = out; |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1385 final HTMLGenerator gen = new HTMLGenerator(false); |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1386 CodeCacheVisitor v = new CodeCacheVisitor() { |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1387 public void prologue(Address start, Address end) { |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1388 } |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1389 public void visit(CodeBlob blob) { |
1748 | 1390 fout.println(gen.genHTML(blob.contentBegin())); |
1040
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1391 } |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1392 public void epilogue() { |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1393 } |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1394 |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1395 |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1396 }; |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1397 VM.getVM().getCodeCache().iterate(v); |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1398 } |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1399 } |
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
196
diff
changeset
|
1400 }, |
0 | 1401 new Command("where", "where { -a | id }", false) { |
1402 public void doit(Tokens t) { | |
1403 if (t.countTokens() != 1) { | |
1404 usage(); | |
1405 } else { | |
1406 String name = t.nextToken(); | |
1407 Threads threads = VM.getVM().getThreads(); | |
1408 boolean all = name.equals("-a"); | |
1409 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
1410 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
1411 thread.printThreadIDOn(new PrintStream(bos)); | |
1412 if (all || bos.toString().equals(name)) { | |
1385 | 1413 out.println(bos.toString() + " = " + thread.getAddress()); |
0 | 1414 HTMLGenerator gen = new HTMLGenerator(false); |
1385 | 1415 try { |
1416 out.println(gen.genHTMLForJavaStackTrace(thread)); | |
1417 } catch (Exception e) { | |
1418 err.println("Error: " + e); | |
1419 if (verboseExceptions) { | |
1420 e.printStackTrace(err); | |
1421 } | |
1422 } | |
0 | 1423 if (!all) return; |
1424 } | |
1425 } | |
1426 if (!all) out.println("Couldn't find thread " + name); | |
1427 } | |
1428 } | |
1429 }, | |
1385 | 1430 new Command("thread", "thread { -a | id }", false) { |
1431 public void doit(Tokens t) { | |
1432 if (t.countTokens() != 1) { | |
1433 usage(); | |
1434 } else { | |
1435 String name = t.nextToken(); | |
1436 Threads threads = VM.getVM().getThreads(); | |
1437 boolean all = name.equals("-a"); | |
1438 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
1439 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
1440 thread.printThreadIDOn(new PrintStream(bos)); | |
1441 if (all || bos.toString().equals(name)) { | |
1442 out.println(bos.toString() + " = " + thread.getAddress()); | |
1443 if (!all) return; | |
1444 } | |
1445 } | |
1446 out.println("Couldn't find thread " + name); | |
1447 } | |
1448 } | |
1449 }, | |
0 | 1450 |
1451 new Command("threads", false) { | |
1452 public void doit(Tokens t) { | |
1453 if (t.countTokens() != 0) { | |
1454 usage(); | |
1455 } else { | |
1456 Threads threads = VM.getVM().getThreads(); | |
1457 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
1458 thread.printThreadIDOn(out); | |
1459 out.println(" " + thread.getThreadName()); | |
1460 } | |
1461 } | |
1462 } | |
1463 }, | |
1464 | |
1465 new Command("livenmethods", false) { | |
1466 public void doit(Tokens t) { | |
1467 if (t.countTokens() != 0) { | |
1468 usage(); | |
1469 } else { | |
1470 ArrayList nmethods = new ArrayList(); | |
1471 Threads threads = VM.getVM().getThreads(); | |
1472 HTMLGenerator gen = new HTMLGenerator(false); | |
1473 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { | |
1474 try { | |
1475 for (JavaVFrame vf = thread.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) { | |
1476 if (vf instanceof CompiledVFrame) { | |
1477 NMethod c = ((CompiledVFrame)vf).getCode(); | |
1478 if (!nmethods.contains(c)) { | |
1479 nmethods.add(c); | |
1480 out.println(gen.genHTML(c)); | |
1481 } | |
1482 } | |
1483 } | |
1484 } catch (Exception e) { | |
1485 e.printStackTrace(); | |
1486 } | |
1487 } | |
1488 } | |
1489 } | |
1490 }, | |
1491 new Command("universe", false) { | |
1492 public void doit(Tokens t) { | |
1493 if (t.countTokens() != 0) { | |
1494 usage(); | |
1495 } else { | |
1496 Universe u = VM.getVM().getUniverse(); | |
1497 out.println("Heap Parameters:"); | |
1498 u.heap().printOn(out); | |
1499 } | |
1500 } | |
1501 }, | |
1502 new Command("verbose", "verbose true | false", true) { | |
1503 public void doit(Tokens t) { | |
1504 if (t.countTokens() != 1) { | |
1505 usage(); | |
1506 } else { | |
1507 verboseExceptions = Boolean.valueOf(t.nextToken()).booleanValue(); | |
1508 } | |
1509 } | |
1510 }, | |
1511 new Command("assert", "assert true | false", true) { | |
1512 public void doit(Tokens t) { | |
1513 if (t.countTokens() != 1) { | |
1514 usage(); | |
1515 } else { | |
1516 Assert.ASSERTS_ENABLED = Boolean.valueOf(t.nextToken()).booleanValue(); | |
1517 } | |
1518 } | |
1519 }, | |
1520 }; | |
1521 | |
1522 private boolean verboseExceptions = false; | |
1523 private ArrayList history = new ArrayList(); | |
1524 private HashMap commands = new HashMap(); | |
1525 private boolean doEcho = false; | |
1526 | |
1527 private Command findCommand(String key) { | |
1528 return (Command)commands.get(key); | |
1529 } | |
1530 | |
1531 public void printPrompt() { | |
1532 out.print("hsdb> "); | |
1533 } | |
1534 | |
1535 private DebuggerInterface debugger; | |
1536 private HotSpotAgent agent; | |
1537 private JSJavaScriptEngine jsengine; | |
1538 private BufferedReader in; | |
1539 private PrintStream out; | |
1540 private PrintStream err; | |
1541 | |
1542 // called before debuggee attach | |
1543 private void preAttach() { | |
1544 // nothing for now.. | |
1545 } | |
1546 | |
1547 // called after debuggee attach | |
1548 private void postAttach() { | |
1549 // create JavaScript engine and start it | |
1550 jsengine = new JSJavaScriptEngine() { | |
1551 private ObjectReader reader = new ObjectReader(); | |
1552 private JSJavaFactory factory = new JSJavaFactoryImpl(); | |
1553 public ObjectReader getObjectReader() { | |
1554 return reader; | |
1555 } | |
1556 public JSJavaFactory getJSJavaFactory() { | |
1557 return factory; | |
1558 } | |
1559 protected void quit() { | |
1560 debugger.detach(); | |
1561 System.exit(0); | |
1562 } | |
1563 protected BufferedReader getInputReader() { | |
1564 return in; | |
1565 } | |
1566 protected PrintStream getOutputStream() { | |
1567 return out; | |
1568 } | |
1569 protected PrintStream getErrorStream() { | |
1570 return err; | |
1571 } | |
1572 }; | |
1573 try { | |
1574 jsengine.defineFunction(this, | |
1575 this.getClass().getMethod("registerCommand", | |
1576 new Class[] { | |
1577 String.class, String.class, String.class | |
1578 })); | |
1579 } catch (NoSuchMethodException exp) { | |
1580 // should not happen, see below...!! | |
1581 exp.printStackTrace(); | |
1582 } | |
1583 jsengine.start(); | |
1584 } | |
1585 | |
1586 public void registerCommand(String cmd, String usage, final String func) { | |
1587 commands.put(cmd, new Command(cmd, usage, false) { | |
1588 public void doit(Tokens t) { | |
1589 final int len = t.countTokens(); | |
1590 Object[] args = new Object[len]; | |
1591 for (int i = 0; i < len; i++) { | |
1592 args[i] = t.nextToken(); | |
1593 } | |
1594 jsengine.call(func, args); | |
1595 } | |
1596 }); | |
1597 } | |
1598 | |
1599 public void setOutput(PrintStream o) { | |
1600 out = o; | |
1601 } | |
1602 | |
1603 public void setErr(PrintStream e) { | |
1604 err = e; | |
1605 } | |
1606 | |
1607 public CommandProcessor(DebuggerInterface debugger, BufferedReader in, PrintStream out, PrintStream err) { | |
1608 this.debugger = debugger; | |
1609 this.agent = debugger.getAgent(); | |
1610 this.in = in; | |
1611 this.out = out; | |
1612 this.err = err; | |
1613 for (int i = 0; i < commandList.length; i++) { | |
1614 Command c = commandList[i]; | |
2177
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
1615 if (commands.get(c.name) != null) { |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
1616 throw new InternalError(c.name + " has multiple definitions"); |
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
1748
diff
changeset
|
1617 } |
0 | 1618 commands.put(c.name, c); |
1619 } | |
1620 if (debugger.isAttached()) { | |
1621 postAttach(); | |
1622 } | |
1623 } | |
1624 | |
1625 | |
1626 public void run(boolean prompt) { | |
1627 // Process interactive commands. | |
1628 while (true) { | |
1629 if (prompt) printPrompt(); | |
1630 String ln = null; | |
1631 try { | |
1632 ln = in.readLine(); | |
1633 } catch (IOException e) { | |
1634 } | |
1635 if (ln == null) { | |
1636 if (prompt) err.println("Input stream closed."); | |
1637 return; | |
1638 } | |
1639 | |
3939 | 1640 executeCommand(ln, prompt); |
0 | 1641 } |
1642 } | |
1643 | |
1385 | 1644 static Pattern historyPattern = Pattern.compile("((!\\*)|(!\\$)|(!!-?)|(!-?[0-9][0-9]*)|(![a-zA-Z][^ ]*))"); |
0 | 1645 |
3939 | 1646 public void executeCommand(String ln, boolean putInHistory) { |
0 | 1647 if (ln.indexOf('!') != -1) { |
1648 int size = history.size(); | |
1649 if (size == 0) { | |
1650 ln = ""; | |
1651 err.println("History is empty"); | |
1652 } else { | |
1653 StringBuffer result = new StringBuffer(); | |
1654 Matcher m = historyPattern.matcher(ln); | |
1655 int start = 0; | |
1656 while (m.find()) { | |
1657 if (m.start() > start) { | |
1658 result.append(ln.substring(start, m.start() - start)); | |
1659 } | |
1660 start = m.end(); | |
1661 | |
1662 String cmd = m.group(); | |
1663 if (cmd.equals("!!")) { | |
1664 result.append((String)history.get(history.size() - 1)); | |
1665 } else if (cmd.equals("!!-")) { | |
1666 Tokens item = new Tokens((String)history.get(history.size() - 1)); | |
1667 item.trim(1); | |
1668 result.append(item.join(" ")); | |
1669 } else if (cmd.equals("!*")) { | |
1670 Tokens item = new Tokens((String)history.get(history.size() - 1)); | |
1671 item.nextToken(); | |
1672 result.append(item.join(" ")); | |
1673 } else if (cmd.equals("!$")) { | |
1674 Tokens item = new Tokens((String)history.get(history.size() - 1)); | |
1675 result.append(item.at(item.countTokens() - 1)); | |
1676 } else { | |
1677 String tail = cmd.substring(1); | |
1385 | 1678 switch (tail.charAt(0)) { |
1679 case '0': | |
1680 case '1': | |
1681 case '2': | |
1682 case '3': | |
1683 case '4': | |
1684 case '5': | |
1685 case '6': | |
1686 case '7': | |
1687 case '8': | |
1688 case '9': | |
1689 case '-': { | |
1690 int index = Integer.parseInt(tail); | |
1691 if (index < 0) { | |
1692 index = history.size() + index; | |
1693 } | |
1694 if (index > size) { | |
1695 err.println("No such history item"); | |
1696 } else { | |
1697 result.append((String)history.get(index)); | |
1698 } | |
1699 break; | |
0 | 1700 } |
1385 | 1701 default: { |
1702 for (int i = history.size() - 1; i >= 0; i--) { | |
1703 String s = (String)history.get(i); | |
1704 if (s.startsWith(tail)) { | |
1705 result.append(s); | |
1706 } | |
1707 } | |
1708 } | |
0 | 1709 } |
1710 } | |
1711 } | |
1712 if (result.length() == 0) { | |
1713 err.println("malformed history reference"); | |
1714 ln = ""; | |
1715 } else { | |
1716 if (start < ln.length()) { | |
1717 result.append(ln.substring(start)); | |
1718 } | |
1719 ln = result.toString(); | |
1720 if (!doEcho) { | |
1721 out.println(ln); | |
1722 } | |
1723 } | |
1724 } | |
1725 } | |
1726 | |
1727 if (doEcho) { | |
1728 out.println("+ " + ln); | |
1729 } | |
1730 | |
1731 PrintStream redirect = null; | |
1732 Tokens t = new Tokens(ln); | |
1733 if (t.hasMoreTokens()) { | |
1734 boolean error = false; | |
3939 | 1735 if (putInHistory) history.add(ln); |
0 | 1736 int len = t.countTokens(); |
1737 if (len > 2) { | |
1738 String r = t.at(len - 2); | |
1739 if (r.equals(">") || r.equals(">>")) { | |
1740 boolean append = r.length() == 2; | |
1741 String file = t.at(len - 1); | |
1742 try { | |
1743 redirect = new PrintStream(new BufferedOutputStream(new FileOutputStream(file, append))); | |
1744 t.trim(2); | |
1745 } catch (Exception e) { | |
1746 out.println("Error: " + e); | |
1747 if (verboseExceptions) { | |
1748 e.printStackTrace(out); | |
1749 } | |
1750 error = true; | |
1751 } | |
1752 } | |
1753 } | |
1754 if (!error) { | |
1755 PrintStream savedout = out; | |
1756 if (redirect != null) { | |
1757 out = redirect; | |
1758 } | |
1759 try { | |
1760 executeCommand(t); | |
1761 } catch (Exception e) { | |
1762 err.println("Error: " + e); | |
1763 if (verboseExceptions) { | |
1764 e.printStackTrace(err); | |
1765 } | |
1766 } finally { | |
1767 if (redirect != null) { | |
1768 out = savedout; | |
1769 redirect.close(); | |
1770 } | |
1771 } | |
1772 } | |
1773 } | |
1774 } | |
1775 | |
1776 void executeCommand(Tokens args) { | |
1777 String cmd = args.nextToken(); | |
1778 | |
1779 Command doit = findCommand(cmd); | |
1780 | |
1781 /* | |
1782 * Check for an unknown command | |
1783 */ | |
1784 if (doit == null) { | |
1785 out.println("Unrecognized command. Try help..."); | |
1786 } else if (!debugger.isAttached() && !doit.okIfDisconnected) { | |
1787 out.println("Command not valid until the attached to a VM"); | |
1788 } else { | |
1789 try { | |
1790 doit.doit(args); | |
1791 } catch (Exception e) { | |
1792 out.println("Error: " + e); | |
1793 if (verboseExceptions) { | |
1794 e.printStackTrace(out); | |
1795 } | |
1796 } | |
1797 } | |
1798 } | |
1799 } |