Mercurial > hg > truffle
annotate agent/src/share/classes/sun/jvm/hotspot/utilities/soql/sa.js @ 1602:136b78722a08
6939203: JSR 292 needs method handle constants
Summary: Add new CP types CONSTANT_MethodHandle, CONSTANT_MethodType; extend 'ldc' bytecode.
Reviewed-by: twisti, never
author | jrose |
---|---|
date | Wed, 09 Jun 2010 18:50:45 -0700 |
parents | c18cbe5936b8 |
children | 0a8e0d4345b3 |
rev | line source |
---|---|
0 | 1 /* |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1040
diff
changeset
|
2 * Copyright (c) 2004, 2007, 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:
1040
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1040
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:
1040
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 // shorter names for SA packages | |
26 | |
27 | |
28 // SA package name abbreviations are kept in 'sapkg' object | |
29 // to avoid global namespace pollution | |
30 var sapkg = new Object(); | |
31 | |
32 sapkg.hotspot = Packages.sun.jvm.hotspot; | |
33 sapkg.asm = sapkg.hotspot.asm; | |
34 sapkg.bugspot = sapkg.hotspot.bugspot; | |
35 sapkg.c1 = sapkg.hotspot.c1; | |
36 sapkg.code = sapkg.hotspot.code; | |
37 sapkg.compiler = sapkg.hotspot.compiler; | |
38 | |
39 // 'debugger' is a JavaScript keyword :-( | |
40 // sapkg.debugger = sapkg.hotspot.debugger; | |
41 | |
42 sapkg.interpreter = sapkg.hotspot.interpreter; | |
43 sapkg.livejvm = sapkg.hotspot.livejvm; | |
44 sapkg.jdi = sapkg.hotspot.jdi; | |
45 sapkg.memory = sapkg.hotspot.memory; | |
46 sapkg.oops = sapkg.hotspot.oops; | |
47 sapkg.runtime = sapkg.hotspot.runtime; | |
48 sapkg.tools = sapkg.hotspot.tools; | |
49 sapkg.types = sapkg.hotspot.types; | |
50 sapkg.ui = sapkg.hotspot.ui; | |
51 sapkg.utilities = sapkg.hotspot.utilities; | |
52 | |
53 // SA singletons are kept in 'sa' object | |
54 var sa = new Object(); | |
55 sa.vm = sapkg.runtime.VM.getVM(); | |
56 sa.dbg = sa.vm.getDebugger(); | |
57 sa.cdbg = sa.dbg.CDebugger; | |
58 sa.heap = sa.vm.universe.heap(); | |
59 sa.systemDictionary = sa.vm.systemDictionary; | |
60 sa.sysDict = sa.systemDictionary; | |
61 sa.symbolTable = sa.vm.symbolTable; | |
62 sa.symTbl = sa.symbolTable; | |
63 sa.threads = sa.vm.threads; | |
64 sa.interpreter = sa.vm.interpreter; | |
65 sa.typedb = sa.vm.typeDataBase; | |
66 sa.codeCache = sa.vm.codeCache; | |
67 // 'objHeap' is different from 'heap'!. | |
68 // This is SA's Oop factory and heap-walker | |
69 sa.objHeap = sa.vm.objectHeap; | |
70 | |
71 // few useful global variables | |
72 var OS = sa.vm.OS; | |
73 var CPU = sa.vm.CPU; | |
74 var LP64 = sa.vm.LP64; | |
75 var isClient = sa.vm.clientCompiler; | |
76 var isServer = sa.vm.serverCompiler; | |
77 var isCore = sa.vm.isCore(); | |
78 var addressSize = sa.vm.addressSize; | |
79 var oopSize = sa.vm.oopSize; | |
80 | |
81 // this "main" function is called immediately | |
82 // after loading this script file | |
83 function main(globals, jvmarg) { | |
84 // wrap a sun.jvm.hotspot.utilities.soql.ScriptObject | |
85 // object so that the properties of it can be accessed | |
86 // in natural object.field syntax. | |
87 function wrapScriptObject(so) { | |
88 function unwrapScriptObject(wso) { | |
89 var objType = typeof(wso); | |
90 if ((objType == 'object' || | |
91 objType == 'function') | |
92 && "__wrapped__" in wso) { | |
93 return wso.__wrapped__; | |
94 } else { | |
95 return wso; | |
96 } | |
97 } | |
98 | |
99 function prepareArgsArray(array) { | |
100 var args = new Array(array.length); | |
101 for (var a = 0; a < array.length; a++) { | |
102 var elem = array[a]; | |
103 elem = unwrapScriptObject(elem); | |
104 if (typeof(elem) == 'function') { | |
105 args[a] = new sapkg.utilities.soql.Callable() { | |
106 call: function(myargs) { | |
107 var tmp = new Array(myargs.length); | |
108 for (var i = 0; i < myargs.length; i++) { | |
109 tmp[i] = wrapScriptObject(myargs[i]); | |
110 } | |
111 return elem.apply(this, tmp); | |
112 } | |
113 } | |
114 } else { | |
115 args[a] = elem; | |
116 } | |
117 } | |
118 return args; | |
119 } | |
120 | |
121 if (so instanceof sapkg.utilities.soql.ScriptObject) { | |
122 return new JSAdapter() { | |
123 __getIds__: function() { | |
124 return so.getIds(); | |
125 }, | |
126 | |
127 __has__ : function(name) { | |
128 if (typeof(name) == 'number') { | |
129 return so["has(int)"](name); | |
130 } else { | |
131 if (name == '__wrapped__') { | |
132 return true; | |
133 } else if (so["has(java.lang.String)"](name)) { | |
134 return true; | |
135 } else if (name.equals('toString')) { | |
136 return true; | |
137 } else { | |
138 return false; | |
139 } | |
140 } | |
141 }, | |
142 | |
143 __delete__ : function(name) { | |
144 if (typeof(name) == 'number') { | |
145 return so["delete(int)"](name); | |
146 } else { | |
147 return so["delete(java.lang.String)"](name); | |
148 } | |
149 }, | |
150 | |
151 __get__ : function(name) { | |
152 if (! this.__has__(name)) { | |
153 return undefined; | |
154 } | |
155 if (typeof(name) == 'number') { | |
156 return wrapScriptObject(so["get(int)"](name)); | |
157 } else { | |
158 if (name == '__wrapped__') { | |
159 return so; | |
160 } else { | |
161 var value = so["get(java.lang.String)"](name); | |
162 if (value instanceof sapkg.utilities.soql.Callable) { | |
163 return function() { | |
164 var args = prepareArgsArray(arguments); | |
165 var r; | |
166 try { | |
167 r = value.call(args); | |
168 } catch (e) { | |
169 println("call to " + name + " failed!"); | |
170 throw e; | |
171 } | |
172 return wrapScriptObject(r); | |
173 } | |
174 } else if (name == 'toString') { | |
175 return function() { | |
176 return so.toString(); | |
177 } | |
178 } else { | |
179 return wrapScriptObject(value); | |
180 } | |
181 } | |
182 } | |
183 } | |
184 }; | |
185 } else { | |
186 return so; | |
187 } | |
188 } | |
189 | |
190 // set "jvm" global variable that wraps a | |
191 // sun.jvm.hotspot.utilities.soql.JSJavaVM instance | |
192 if (jvmarg != null) { | |
193 jvm = wrapScriptObject(jvmarg); | |
194 // expose "heap" global variable | |
195 heap = jvm.heap; | |
196 } | |
197 | |
198 // expose all "function" type properties of | |
199 // sun.jvm.hotspot.utilitites.soql.JSJavaScriptEngine | |
200 // as global functions here. | |
201 globals = wrapScriptObject(globals); | |
202 for (var prop in globals) { | |
203 if (typeof(globals[prop]) == 'function') { | |
204 this[prop] = globals[prop]; | |
205 } | |
206 } | |
207 | |
208 // define "writeln" and "write" if not defined | |
209 if (typeof(writeln) == 'undefined') { | |
210 writeln = println; | |
211 } | |
212 | |
213 if (typeof(write) == 'undefined') { | |
214 write = print; | |
215 } | |
216 | |
217 // "registerCommand" function is defined if we | |
218 // are running as part of "CLHSDB" tool. CLHSDB | |
219 // tool exposes Unix-style commands. | |
220 | |
221 // if "registerCommand" function is defined | |
222 // then register few global functions as "commands". | |
223 if (typeof(registerCommand) == 'function') { | |
224 this.printDis = function(addr, len) { | |
225 if (!addr) { | |
226 writeln("Usage: dis address [ length ]"); | |
227 } else { | |
228 dis(addr, len); | |
229 } | |
230 } | |
231 registerCommand("dis", "dis address [ length ]", "printDis"); | |
232 | |
233 this.jclass = function(name) { | |
234 if (typeof(name) == "string") { | |
235 var clazz = sapkg.utilities.SystemDictionaryHelper.findInstanceKlass(name); | |
236 if (clazz) { | |
237 writeln(clazz.getName().asString() + " @" + clazz.getHandle().toString()); | |
238 } else { | |
239 writeln("class not found: " + name); | |
240 } | |
241 } else { | |
242 writeln("Usage: class name"); | |
243 } | |
244 } | |
245 registerCommand("class", "class name", "jclass"); | |
246 | |
247 this.jclasses = function() { | |
248 forEachKlass(function (clazz) { | |
249 writeln(clazz.getName().asString() + " @" + clazz.getHandle().toString()); | |
250 }); | |
251 } | |
252 registerCommand("classes", "classes", "jclasses"); | |
253 | |
254 this.printJDis = function(addr) { | |
255 if (!addr) { | |
256 writeln("Usage: jdis address"); | |
257 } else { | |
258 jdis(addr); | |
259 } | |
260 } | |
261 registerCommand("jdis", "jdis address", "printJDis"); | |
262 | |
263 this.dclass = function(clazz, dir) { | |
264 if (!clazz) { | |
265 writeln("Usage: dumpclass { address | name } [ directory ]"); | |
266 } else { | |
267 if (!dir) { dir = "."; } | |
268 dumpClass(clazz, dir); | |
269 } | |
270 } | |
271 registerCommand("dumpclass", "dumpclass { address | name } [ directory ]", "dclass"); | |
272 registerCommand("dumpheap", "dumpheap [ file ]", "dumpHeap"); | |
273 | |
274 this.jseval = function(str) { | |
275 if (!str) { | |
276 writeln("Usage: jseval script"); | |
277 } else { | |
278 var res = eval(str); | |
279 if (res) { writeln(res); } | |
280 } | |
281 } | |
282 registerCommand("jseval", "jseval script", "jseval"); | |
283 | |
284 this.jsload = function(file) { | |
285 if (!file) { | |
286 writeln("Usage: jsload file"); | |
287 } else { | |
288 load(file); | |
289 } | |
290 } | |
291 registerCommand("jsload", "jsload file", "jsload"); | |
292 | |
293 this.printMem = function(addr, len) { | |
294 if (!addr) { | |
295 writeln("Usage: mem [ length ]"); | |
296 } else { | |
297 mem(addr, len); | |
298 } | |
299 } | |
300 registerCommand("mem", "mem address [ length ]", "printMem"); | |
301 | |
302 this.sysProps = function() { | |
303 for (var i in jvm.sysProps) { | |
304 writeln(i + ' = ' + jvm.sysProps[i]); | |
305 } | |
306 } | |
307 registerCommand("sysprops", "sysprops", "sysProps"); | |
308 | |
309 this.printWhatis = function(addr) { | |
310 if (!addr) { | |
311 writeln("Usage: whatis address"); | |
312 } else { | |
313 writeln(whatis(addr)); | |
314 } | |
315 } | |
316 registerCommand("whatis", "whatis address", "printWhatis"); | |
317 } | |
318 } | |
319 | |
320 // debugger functionality | |
321 | |
322 // string-to-Address | |
323 function str2addr(str) { | |
324 return sa.dbg.parseAddress(str); | |
325 } | |
326 | |
327 // number-to-Address | |
328 if (addressSize == 4) { | |
329 eval("function num2addr(num) { \ | |
330 return str2addr('0x' + java.lang.Integer.toHexString(0xffffffff & num)); \ | |
331 }"); | |
332 } else { | |
333 eval("function num2addr(num) { \ | |
334 return str2addr('0x' + java.lang.Long.toHexString(num)); \ | |
335 }"); | |
336 } | |
337 | |
338 // generic any-type-to-Address | |
339 // use this convenience function to accept address in any | |
340 // format -- number, string or an Address instance. | |
341 function any2addr(addr) { | |
342 var type = typeof(addr); | |
343 if (type == 'number') { | |
344 return num2addr(addr); | |
345 } else if (type == 'string') { | |
346 return str2addr(addr); | |
347 } else { | |
348 return addr; | |
349 } | |
350 } | |
351 | |
352 // Address-to-string | |
353 function addr2str(addr) { | |
354 if (addr == null) { | |
355 return (addressSize == 4)? '0x00000000' : '0x0000000000000000'; | |
356 } else { | |
357 return addr + ''; | |
358 } | |
359 } | |
360 | |
361 // Address-to-number | |
362 function addr2num(addr) { | |
363 return sa.dbg.getAddressValue(addr); | |
364 } | |
365 | |
366 // symbol-to-Address | |
367 function sym2addr(dso, sym) { | |
368 return sa.dbg.lookup(dso, sym); | |
369 } | |
370 | |
371 // returns the ClosestSymbol or null | |
372 function closestSymbolFor(addr) { | |
373 if (sa.cdbg == null) { | |
374 // no CDebugger support, return null | |
375 return null; | |
376 } else { | |
377 var dso = sa.cdbg.loadObjectContainingPC(addr); | |
378 if (dso != null) { | |
379 return dso.closestSymbolToPC(addr); | |
380 } else { | |
381 return null; | |
382 } | |
383 } | |
384 } | |
385 | |
386 // Address-to-symbol | |
387 // returns nearest symbol as string if found | |
388 // else returns address as string | |
389 function addr2sym(addr) { | |
390 var sym = closestSymbolFor(addr); | |
391 if (sym != null) { | |
392 return sym.name + '+' + sym.offset; | |
393 } else { | |
394 return addr2str(addr); | |
395 } | |
396 } | |
397 | |
398 // read 'num' bytes at 'addr' and return an array as result. | |
399 // returns Java byte[] type result and not a JavaScript array. | |
400 function readBytesAt(addr, num) { | |
401 addr = any2addr(addr); | |
402 var res = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, num); | |
403 var i; | |
404 for (i = 0; i < num; i++) { | |
405 res[i] = addr.getJByteAt(i); | |
406 } | |
407 return res; | |
408 } | |
409 | |
410 // read 'num' words at 'addr' and return an array as result. | |
411 // returns Java long[] type result and not a JavaScript array. | |
412 function readWordsAt(addr, num) { | |
413 addr = any2addr(addr); | |
414 var res = java.lang.reflect.Array.newInstance(java.lang.Long.TYPE, num); | |
415 var i; | |
416 for (i = 0; i < num; i++) { | |
417 res[i] = addr2num(addr.getAddressAt(i * addressSize)); | |
418 } | |
419 return res; | |
420 } | |
421 | |
422 // read the 'C' string at 'addr' | |
423 function readCStrAt(addr) { | |
424 addr = any2addr(addr); | |
425 return sapkg.utilities.CStringUtilities.getString(addr); | |
426 } | |
427 | |
428 // read the length of the 'C' string at 'addr' | |
429 function readCStrLen(addr) { | |
430 addr = any2addr(addr); | |
431 return sapkg.utilities.CStringUtilities.getStringLength(addr); | |
432 } | |
433 | |
434 // iterate through ThreadList of CDebugger | |
435 function forEachThread(callback) { | |
436 if (sa.cdbg == null) { | |
437 // no CDebugger support | |
438 return; | |
439 } else { | |
440 var itr = sa.cdbg.threadList.iterator(); | |
441 while (itr.hasNext()) { | |
442 if (callback(itr.next()) == false) return; | |
443 } | |
444 } | |
445 } | |
446 | |
447 // read register set of a ThreadProxy as name-value pairs | |
448 function readRegs(threadProxy) { | |
449 var ctx = threadProxy.context; | |
450 var num = ctx.numRegisters; | |
451 var res = new Object(); | |
452 var i; | |
453 for (i = 0; i < num; i++) { | |
454 res[ctx.getRegisterName(i)]= addr2str(ctx.getRegisterAsAddress(i)); | |
455 } | |
456 return res; | |
457 } | |
458 | |
459 // print register set for a given ThreaProxy | |
460 function regs(threadProxy) { | |
461 var res = readRegs(threadProxy); | |
462 for (i in res) { | |
463 writeln(i, '=', res[i]); | |
464 } | |
465 } | |
466 | |
467 // iterate through each CFrame of a given ThreadProxy | |
468 function forEachCFrame(threadProxy, callback) { | |
469 if (sa.cdbg == null) { | |
470 // no CDebugger support | |
471 return; | |
472 } else { | |
473 var cframe = sa.cdbg.topFrameForThread(threadProxy); | |
474 while (cframe != null) { | |
475 if (callback(cframe) == false) return; | |
476 cframe = cframe.sender(); | |
477 } | |
478 } | |
479 } | |
480 | |
481 // iterate through list of load objects (DLLs, DSOs) | |
482 function forEachLoadObject(callback) { | |
483 if (sa.cdbg == null) { | |
484 // no CDebugger support | |
485 return; | |
486 } else { | |
487 var itr = sa.cdbg.loadObjectList.iterator(); | |
488 while (itr.hasNext()) { | |
489 if (callback(itr.next()) == false) return; | |
490 } | |
491 } | |
492 } | |
493 | |
494 // print 'num' words at 'addr' | |
495 function mem(addr, num) { | |
496 if (num == undefined) { | |
497 num = 1; | |
498 } | |
499 addr = any2addr(addr); | |
500 var i; | |
501 for (i = 0; i < num; i++) { | |
502 var value = addr.getAddressAt(0); | |
503 writeln(addr2sym(addr) + ':', addr2str(value)); | |
504 addr = addr.addOffsetTo(addressSize); | |
505 } | |
506 writeln(); | |
507 } | |
508 | |
509 // return the disassemble class for current CPU | |
510 function disassemblerClass() { | |
511 var DisAsmClass; | |
512 if (CPU == 'x86') { | |
513 DisAsmClass = sapkg.asm.x86.X86Disassembler; | |
514 } else if (CPU == 'sparc') { | |
515 DisAsmClass = sapkg.asm.sparc.SPARCV9Disassembler; | |
516 } | |
517 return DisAsmClass; | |
518 } | |
519 | |
520 // print native code disassembly of 'num' bytes at 'addr' | |
521 function dis(addr, num) { | |
522 addr = any2addr(addr); | |
523 var nmethod = findNMethod(addr); | |
524 if (nmethod != null) { | |
525 // disassemble it as nmethod | |
526 nmethoddis(nmethod); | |
527 } else { | |
528 // raw disassembly | |
529 if (num == undefined) { | |
530 // size of one SPARC instruction and | |
531 // unknown number of Intel instructions. | |
532 num = 4; | |
533 } | |
534 DisAsmClass = disassemblerClass(); | |
535 if (DisAsmClass == undefined) { | |
536 // unsupported CPU | |
537 writeln(CPU + " is not yet supported!"); | |
538 return; | |
539 } | |
540 | |
541 var bytes = readBytesAt(addr, num); | |
542 var disAsm = new DisAsmClass(addr2num(addr), bytes); | |
543 disAsm.decode(new sapkg.asm.InstructionVisitor() { | |
544 visit: function (pc, instr) { | |
545 write(addr2sym(num2addr(pc)) + ':', '\t'); | |
546 writeln(instr.asString(pc, | |
547 new sapkg.asm.SymbolFinder() { | |
548 getSymbolFor: function(addr) { | |
549 return addr2sym(num2addr(addr)); | |
550 } | |
551 })); | |
552 } | |
553 }); | |
554 } | |
555 } | |
556 | |
557 // System dictionary functions | |
558 | |
559 // find InstanceKlass by name | |
560 function findInstanceKlass(name) { | |
561 return sapkg.utilities.SystemDictionaryHelper.findInstanceKlass(name); | |
562 } | |
563 | |
564 // get Java system loader (i.e., application launcher loader) | |
565 function systemLoader() { | |
566 return sa.sysDict.javaSystemLoader(); | |
567 } | |
568 | |
569 // iterate system dictionary for each 'Klass' | |
570 function forEachKlass(callback) { | |
571 var VisitorClass = sapkg.memory.SystemDictionary.ClassVisitor; | |
572 var visitor = new VisitorClass() { visit: callback }; | |
573 sa.sysDict["classesDo(sun.jvm.hotspot.memory.SystemDictionary$ClassVisitor)"](visitor); | |
574 } | |
575 | |
576 // iterate system dictionary for each 'Klass' and initiating loader | |
577 function forEachKlassAndLoader(callback) { | |
578 var VisitorClass = sapkg.memory.SystemDictionary.ClassAndLoaderVisitor; | |
579 var visitor = new VisitorClass() { visit: callback }; | |
580 sa.sysDict["classesDo(sun.jvm.hotspot.memory.SystemDictionary$ClassAndLoaderVisitor)"](visitor); | |
581 } | |
582 | |
583 // iterate system dictionary for each primitive array klass | |
584 function forEachPrimArrayKlass(callback) { | |
585 var VisitorClass = sapkg.memory.SystemDictionary.ClassAndLoaderVisitor; | |
586 sa.sysDict.primArrayClassesDo(new VisitorClass() { visit: callback }); | |
587 } | |
588 | |
589 // (hotspot) symbol table functions | |
590 | |
591 // String-to-Symbol | |
592 function str2sym(str) { | |
593 return sa.symTbl.probe(str); | |
594 } | |
595 | |
596 // Symbol-to-String | |
597 function sym2str(sym) { | |
598 return sym.asString(); | |
599 } | |
600 | |
601 // oop functions | |
602 | |
603 // Address-to-Oop | |
604 function addr2oop(addr) { | |
605 addr = any2addr(addr); | |
606 return sa.objHeap.newOop(addr.addOffsetToAsOopHandle(0)); | |
607 } | |
608 | |
609 // Oop-to-Address | |
610 function oop2addr(oop) { | |
611 return oop.handle; | |
612 } | |
613 | |
614 // 'oop' to higher-level java object wrapper in which for(i in o) | |
615 // works by iterating java level fields and javaobject.javafield | |
616 // syntax works. | |
617 function oop2obj(oop) { | |
618 return object(addr2str(oop.handle)); | |
619 } | |
620 | |
621 // higher level java object wrapper to oop | |
622 function obj2oop(obj) { | |
623 return addr2oop(str2addr(address(obj))); | |
624 } | |
625 | |
626 // Java heap iteration | |
627 | |
628 // iterates Java heap for each Oop | |
629 function forEachOop(callback) { | |
630 sa.objHeap.iterate(new sapkg.oops.HeapVisitor() { doObj: callback }); | |
631 } | |
632 | |
633 // iterates Java heap for each Oop of given 'klass'. | |
634 // 'includeSubtypes' tells whether to include objects | |
635 // of subtypes of 'klass' or not | |
636 function forEachOopOfKlass(callback, klass, includeSubtypes) { | |
637 if (klass == undefined) { | |
638 klass = findInstanceKlass("java.lang.Object"); | |
639 } | |
640 | |
641 if (includeSubtypes == undefined) { | |
642 includeSubtypes = true; | |
643 } | |
644 sa.objHeap.iterateObjectsOfKlass( | |
645 new sapkg.oops.HeapVisitor() { doObj: callback }, | |
646 klass, includeSubtypes); | |
647 } | |
648 | |
649 // code cache functions | |
650 | |
651 // iterates CodeCache for each 'CodeBlob' | |
652 function forEachCodeBlob(callback) { | |
653 var VisitorClass = sapkg.code.CodeCacheVisitor; | |
654 sa.codeCache.iterate(new VisitorClass() { visit: callback }); | |
655 } | |
656 | |
657 // find the ClodBlob (if any) that contains given address | |
658 function findCodeBlob(addr) { | |
659 addr = any2addr(addr); | |
660 return sa.codeCache.findBlobUnsafe(addr); | |
661 } | |
662 | |
663 // find the NMethod (if any) that contains given address | |
664 function findNMethod(addr) { | |
665 var codeBlob = findCodeBlob(addr); | |
666 return (codeBlob != null && codeBlob.isNMethod())? codeBlob : null; | |
667 } | |
668 | |
669 // returns PcDesc at given address or null | |
670 function pcDescAt(addr) { | |
671 addr = any2addr(addr); | |
672 var nmethod = findNMethod(addr); | |
673 return (nmethod != null)? nmethod.safepoints.get(addr) : null; | |
674 } | |
675 | |
676 // helpers for nmethod disassembler | |
677 function printScope(scopeDesc) { | |
678 if (scopeDesc == null) { | |
679 return; | |
680 } | |
681 printScope(scopeDesc.sender()); | |
682 var method = scopeDesc.method; | |
683 var bci = scopeDesc.BCI; | |
684 var line = -1; | |
685 if (method.hasLineNumberTable()) { | |
686 line = method.getLineNumberFromBCI(bci); | |
687 } | |
688 | |
689 write('\t', method.externalNameAndSignature(), '@', method.handle, 'bci=' + bci); | |
690 if (line != -1) { | |
691 write('line=' + line); | |
692 } | |
693 writeln(); | |
694 } | |
695 | |
696 function printSafepointInfo(nmethod, pcDesc) { | |
697 var scopeDesc = nmethod.getScopeDescAt( | |
698 pcDesc.getRealPC(nmethod), | |
699 pcDesc.isAtCall()); | |
700 printScope(scopeDesc); | |
701 } | |
702 | |
703 // print disassembly for a given nmethod | |
704 function nmethoddis(nmethod) { | |
705 var DisAsmClass = disassemblerClass(); | |
706 if (DisAsmClass == undefined) { | |
707 writeln(CPU + " is not yet supported!"); | |
708 return; | |
709 } | |
710 | |
711 var method = nmethod.method; | |
712 writeln('NMethod:', method.externalNameAndSignature(), '@', method.handle); | |
713 | |
714 var codeBegin = nmethod.codeBegin(); | |
715 var codeEnd = nmethod.codeEnd(); | |
716 var size = codeEnd.minus(codeBegin); | |
717 var code = readBytesAt(codeBegin, size); | |
718 var startPc = addr2num(codeBegin); | |
719 var verifiedEntryPoint = addr2num(nmethod.verifiedEntryPoint); | |
720 var entryPoint = addr2num(nmethod.entryPoint); | |
721 var interpreterEntryPoint = addr2num(nmethod.interpreterEntryPointOrNull); | |
722 var safepoints = nmethod.safepoints; | |
723 var disAsm = new DisAsmClass(startPc, code); | |
724 disAsm.decode(new sapkg.asm.InstructionVisitor() { | |
725 visit: function(curPc, instr) { | |
726 if (curPc == verifiedEntryPoint) { | |
727 writeln(); | |
728 writeln("Verified Entry Point:"); | |
729 } | |
730 if (curPc == entryPoint) { | |
731 writeln(); | |
732 writeln("Entry Point:"); | |
733 } | |
734 if (curPc == interpreterEntryPoint) { | |
735 writeln(""); | |
736 writeln("Interpreter Entry Point:"); | |
737 } | |
738 | |
739 var pcDesc = safepoints.get(num2addr(curPc)); | |
740 var isSafepoint = (pcDesc != null); | |
741 if (isSafepoint && pcDesc.isAtCall()) { | |
742 printSafepointInfo(nmethod, pcDesc); | |
743 } | |
744 | |
745 write(num2addr(curPc) + ':', '\t'); | |
746 writeln(instr.asString(curPc, | |
747 new sapkg.asm.SymbolFinder() { | |
748 getSymbolFor: function(addr) { | |
749 return addr2sym(num2addr(addr)); | |
750 } | |
751 })); | |
752 | |
753 if (isSafepoint && !pcDesc.isAtCall()) { | |
754 printSafepointInfo(nmethod, pcDesc); | |
755 } | |
756 } | |
757 }); | |
758 } | |
759 | |
760 // bytecode interpreter functions | |
761 | |
762 // iterates interpreter codelets for each interpreter codelet | |
763 function forEachInterpCodelet(callback) { | |
764 var stubQueue = sa.interpreter.code; | |
765 var stub = stubQueue.first; | |
766 while (stub != null) { | |
767 if (callback(stub) == false) return; | |
768 stub = stubQueue.getNext(stub); | |
769 } | |
770 } | |
771 | |
772 // helper for bytecode disassembler | |
773 function printExceptionTable(method) { | |
774 var expTbl = method.getExceptionTable(); | |
775 var len = expTbl.getLength(); | |
776 if (len != 0) { | |
777 var i; | |
778 var cpool = method.constants; | |
779 writeln("start", '\t', "end", '\t', "handler", '\t', "exception"); | |
780 writeln(""); | |
781 for (i = 0; i < len; i += 4) { | |
782 write(expTbl.getIntAt(i), '\t', | |
783 expTbl.getIntAt(i + 1), '\t', | |
784 expTbl.getIntAt(i + 2), '\t'); | |
785 var cpIndex = expTbl.getIntAt(i + 3); | |
786 var oop = (cpIndex == 0)? null : cpool.getObjAt(cpIndex); | |
787 if (oop == null) { | |
788 writeln("<any>"); | |
789 } else if (oop.isSymbol()) { | |
790 writeln(oop.asString().replace('/', '.')); | |
791 } else if (oop.isKlass()) { | |
792 writeln(oop.name.asString().replace('/', '.')); | |
793 } else { | |
794 writeln(cpIndex); | |
795 } | |
796 } | |
797 } | |
798 } | |
799 | |
800 // print Java bytecode disassembly | |
801 function jdis(method) { | |
802 if (method.getByteCode == undefined) { | |
803 // method oop may be specified by address | |
804 method = addr2oop(any2addr(method)); | |
805 } | |
806 writeln(method, '-', method.externalNameAndSignature()); | |
807 if (method.isNative()) { | |
808 writeln("native method"); | |
809 return; | |
810 } | |
811 if (method.isAbstract()) { | |
812 writeln("abstract method"); | |
813 return; | |
814 } | |
815 | |
816 writeln(); | |
817 var BytecodeDisAsmClass = sapkg.interpreter.BytecodeDisassembler; | |
818 var disAsm = new BytecodeDisAsmClass(method); | |
819 var bci = 0; | |
820 var hasLines = method.hasLineNumberTable(); | |
821 if (hasLines) { | |
822 writeln("bci", '\t', "line", '\t', "instruction"); | |
823 } else { | |
824 writeln("bci", '\t', "instruction"); | |
825 } | |
826 writeln(""); | |
827 disAsm.decode(new sapkg.interpreter.BytecodeVisitor() { | |
1602 | 828 prologue: function(method) { }, |
829 epilogue: function() { }, | |
0 | 830 visit: function(bytecode) { |
831 if (hasLines) { | |
832 var line = method.getLineNumberFromBCI(bci); | |
833 writeln(bci, '\t', line, '\t', bytecode); | |
834 } else { | |
835 writeln(bci, '\t', bytecode); | |
836 } | |
837 bci++; | |
838 } | |
839 }); | |
840 | |
841 writeln(); | |
842 printExceptionTable(method); | |
843 } | |
844 | |
845 // Java thread | |
846 | |
847 // iterates each Thread | |
848 function forEachJavaThread(callback) { | |
849 var threads = sa.threads; | |
850 var thread = threads.first(); | |
851 while (thread != null) { | |
852 if (callback(thread) == false) return; | |
853 thread = thread.next(); | |
854 } | |
855 } | |
856 | |
857 // iterate Frames of a given thread | |
858 function forEachFrame(javaThread, callback) { | |
859 var fr = javaThread.getLastFrameDbg(); | |
860 while (fr != null) { | |
861 if (callback(fr) == false) return; | |
862 fr = fr.sender(); | |
863 } | |
864 } | |
865 | |
866 // iterate JavaVFrames of a given JavaThread | |
867 function forEachVFrame(javaThread, callback) { | |
868 var vfr = javaThread.getLastJavaVFrameDbg(); | |
869 while (vfr != null) { | |
870 if (callback(vfr) == false) return; | |
871 vfr = vfr.javaSender(); | |
872 } | |
873 } | |
874 | |
875 function printStackTrace(javaThread) { | |
876 write("Thread "); | |
877 javaThread.printThreadIDOn(java.lang.System.out); | |
878 writeln(); | |
879 forEachVFrame(javaThread, function (vf) { | |
880 var method = vf.method; | |
881 write(' - ', method.externalNameAndSignature(), '@bci =', vf.getBCI()); | |
882 var line = method.getLineNumberFromBCI(vf.getBCI()); | |
883 if (line != -1) { write(', line=', line); } | |
884 if (vf.isCompiledFrame()) { write(" (Compiled Frame)"); } | |
885 if (vf.isInterpretedFrame()) { write(" (Interpreted Frame)"); } | |
886 writeln(); | |
887 }); | |
888 writeln(); | |
889 writeln(); | |
890 } | |
891 | |
892 // print Java stack trace for all threads | |
893 function where(javaThread) { | |
894 if (javaThread == undefined) { | |
895 forEachJavaThread(function (jt) { printStackTrace(jt); }); | |
896 } else { | |
897 printStackTrace(javaThread); | |
898 } | |
899 } | |
900 | |
901 // vmStructs access -- type database functions | |
902 | |
903 // find a VM type | |
904 function findVMType(typeName) { | |
905 return sa.typedb.lookupType(typeName); | |
906 } | |
907 | |
908 // iterate VM types | |
909 function forEachVMType(callback) { | |
910 var itr = sa.typedb.types; | |
911 while (itr.hasNext()) { | |
912 if (callback(itr.next()) == false) return; | |
913 } | |
914 } | |
915 | |
916 // find VM int constant | |
917 function findVMIntConst(name) { | |
918 return sa.typedb.lookupIntConstant(name); | |
919 } | |
920 | |
921 // find VM long constant | |
922 function findVMLongConst(name) { | |
923 return sa.typedb.lookupLongConstant(name); | |
924 } | |
925 | |
926 // iterate VM int constants | |
927 function forEachVMIntConst(callback) { | |
928 var itr = sa.typedb.intConstants; | |
929 while (itr.hasNext()) { | |
930 if (callback(itr.next()) == false) return; | |
931 } | |
932 } | |
933 | |
934 // iterate VM long constants | |
935 function forEachVMLongConst(callback) { | |
936 var itr = sa.typedb.longConstants; | |
937 while (itr.hasNext()) { | |
938 if (callback(itr.next()) == false) return; | |
939 } | |
940 } | |
941 | |
942 // returns VM Type at address | |
943 function vmTypeof(addr) { | |
944 addr = any2addr(addr); | |
945 return sa.typedb.guessTypeForAddress(addr); | |
946 } | |
947 | |
948 // does the given 'addr' points to an object of given 'type'? | |
949 // OR any valid Type at all (if type is undefined) | |
950 function isOfVMType(addr, type) { | |
951 addr = any2addr(addr); | |
952 if (type == undefined) { | |
953 return vmTypeof(addr) != null; | |
954 } else { | |
955 if (typeof(type) == 'string') { | |
956 type = findVMType(type); | |
957 } | |
958 return sa.typedb.addressTypeIsEqualToType(addr, type); | |
959 } | |
960 } | |
961 | |
962 // reads static field value | |
963 function readVMStaticField(field) { | |
964 var type = field.type; | |
965 if (type.isCIntegerType() || type.isJavaPrimitiveType()) { | |
966 return field.value; | |
967 } else if (type.isPointerType()) { | |
968 return field.address; | |
969 } else if (type.isOopType()) { | |
970 return field.oopHandle; | |
971 } else { | |
972 return field.staticFieldAddress; | |
973 } | |
974 } | |
975 | |
976 // reads given instance field of VM object at 'addr' | |
977 function readVMInstanceField(field, addr) { | |
978 var type = field.type; | |
979 if (type.isCIntegerType() || type.isJavaPrimitiveType()) { | |
980 return field.getValue(addr); | |
981 } else if (type.isPointerType()) { | |
982 return field.getAddress(addr); | |
983 } else if (type.isOopType()) { | |
984 return field.getOopHandle(addr); | |
985 } else { | |
986 return addr.addOffsetTo(field.offset); | |
987 } | |
988 } | |
989 | |
990 // returns name-value of pairs of VM type at given address. | |
991 // If address is unspecified, reads static fields as name-value pairs. | |
992 function readVMType(type, addr) { | |
993 if (typeof(type) == 'string') { | |
994 type = findVMType(type); | |
995 } | |
996 if (addr != undefined) { | |
997 addr = any2addr(addr); | |
998 } | |
999 | |
1000 var result = new Object(); | |
1001 var staticOnly = (addr == undefined); | |
1002 while (type != null) { | |
1003 var itr = type.fields; | |
1004 while (itr.hasNext()) { | |
1005 var field = itr.next(); | |
1006 var isStatic = field.isStatic(); | |
1007 if (staticOnly && isStatic) { | |
1008 result[field.name] = readVMStaticField(field); | |
1009 } else if (!staticOnly && !isStatic) { | |
1010 result[field.name] = readVMInstanceField(field, addr); | |
1011 } | |
1012 } | |
1013 type = type.superclass; | |
1014 } | |
1015 return result; | |
1016 } | |
1017 | |
1018 function printVMType(type, addr) { | |
1019 if (typeof(type) == 'string') { | |
1020 type = findVMType(type); | |
1021 } | |
1022 var obj = readVMType(type, addr); | |
1023 while (type != null) { | |
1024 var itr = type.fields; | |
1025 while (itr.hasNext()) { | |
1026 var field = itr.next(); | |
1027 var name = field.name; | |
1028 var value = obj[name]; | |
1029 if (value != undefined) { | |
1030 writeln(field.type.name, type.name + '::' + name, '=', value); | |
1031 } | |
1032 } | |
1033 type = type.superclass; | |
1034 } | |
1035 } | |
1036 | |
1037 // define readXXX and printXXX functions for each VM struct/class Type | |
1038 tmp = new Object(); | |
1039 tmp.itr = sa.typedb.types; | |
1040 while (tmp.itr.hasNext()) { | |
1041 tmp.type = tmp.itr.next(); | |
1042 tmp.name = tmp.type.name; | |
1043 if (tmp.type.isPointerType() || tmp.type.isOopType() || | |
1044 tmp.type.isCIntegerType() || tmp.type.isJavaPrimitiveType() || | |
1045 tmp.name.equals('address') || | |
1046 tmp.name.equals("<opaque>")) { | |
1047 // ignore; | |
1048 continue; | |
1049 } else { | |
1050 // some type names have ':'. replace to make it as a | |
1051 // JavaScript identifier | |
1040
873ec3787992
6892186: SA does not dump debug info for scalar replaced objects
kvn
parents:
0
diff
changeset
|
1052 tmp.name = tmp.name.replace(':', '_').replace('<', '_').replace('>', '_').replace('*', '_').replace(' ', '_'); |
0 | 1053 eval("function read" + tmp.name + "(addr) {" + |
1054 " return readVMType('" + tmp.name + "', addr);}"); | |
1055 eval("function print" + tmp.name + "(addr) {" + | |
1056 " printVMType('" + tmp.name + "', addr); }"); | |
1057 | |
1058 /* FIXME: do we need this? | |
1059 if (typeof(registerCommand) != 'undefined') { | |
1060 var name = "print" + tmp.name; | |
1061 registerCommand(name, name + " [address]", name); | |
1062 } | |
1063 */ | |
1064 } | |
1065 } | |
1066 //clean-up the temporary | |
1067 delete tmp; | |
1068 | |
1069 // VMObject factory | |
1070 | |
1071 // VM type to SA class map | |
1072 var vmType2Class = new Object(); | |
1073 | |
1074 // This is *not* exhaustive. Add more if needed. | |
1075 // code blobs | |
1076 vmType2Class["BufferBlob"] = sapkg.code.BufferBlob; | |
1077 vmType2Class["nmethod"] = sapkg.code.NMethod; | |
1078 vmType2Class["RuntimeStub"] = sapkg.code.RuntimeStub; | |
1079 vmType2Class["SafepointBlob"] = sapkg.code.SafepointBlob; | |
1080 vmType2Class["C2IAdapter"] = sapkg.code.C2IAdapter; | |
1081 vmType2Class["DeoptimizationBlob"] = sapkg.code.DeoptimizationBlob; | |
1082 vmType2Class["ExceptionBlob"] = sapkg.code.ExceptionBlob; | |
1083 vmType2Class["I2CAdapter"] = sapkg.code.I2CAdapter; | |
1084 vmType2Class["OSRAdapter"] = sapkg.code.OSRAdapter; | |
1085 vmType2Class["UncommonTrapBlob"] = sapkg.code.UncommonTrapBlob; | |
1086 vmType2Class["PCDesc"] = sapkg.code.PCDesc; | |
1087 | |
1088 // interpreter | |
1089 vmType2Class["InterpreterCodelet"] = sapkg.interpreter.InterpreterCodelet; | |
1090 | |
1091 // Java Threads | |
1092 vmType2Class["JavaThread"] = sapkg.runtime.JavaThread; | |
1093 vmType2Class["CompilerThread"] = sapkg.runtime.CompilerThread; | |
1094 vmType2Class["SurrogateLockerThread"] = sapkg.runtime.JavaThread; | |
1095 vmType2Class["DebuggerThread"] = sapkg.runtime.DebuggerThread; | |
1096 | |
1097 // gc | |
1098 vmType2Class["GenCollectedHeap"] = sapkg.memory.GenCollectedHeap; | |
1099 vmType2Class["CompactingPermGenGen"] = sapkg.memory.CompactingPermGenGen; | |
1100 vmType2Class["DefNewGeneration"] = sapkg.memory.DefNewGeneration; | |
1101 vmType2Class["TenuredGeneration"] = sapkg.memory.TenuredGeneration; | |
1102 | |
1103 // generic VMObject factory for a given address | |
1104 // This is equivalent to VirtualConstructor. | |
1105 function newVMObject(addr) { | |
1106 addr = any2addr(addr); | |
1107 var result = null; | |
1108 forEachVMType(function (type) { | |
1109 if (isOfVMType(addr, type)) { | |
1110 var clazz = vmType2Class[type.name]; | |
1111 if (clazz != undefined) { | |
1112 result = new clazz(addr); | |
1113 } | |
1114 return false; | |
1115 } else { | |
1116 return true; | |
1117 } | |
1118 }); | |
1119 return result; | |
1120 } | |
1121 | |
1122 function vmobj2addr(vmobj) { | |
1123 return vmobj.address; | |
1124 } | |
1125 | |
1126 function addr2vmobj(addr) { | |
1127 return newVMObject(addr); | |
1128 } | |
1129 | |
1130 // Miscellaneous utilities | |
1131 | |
1132 // returns PointerLocation that describes the given pointer | |
1133 function findPtr(addr) { | |
1134 addr = any2addr(addr); | |
1135 return sapkg.utilities.PointerFinder.find(addr); | |
1136 } | |
1137 | |
1138 // is given address a valid Oop? | |
1139 function isOop(addr) { | |
1140 addr = any2addr(addr); | |
1141 var oopHandle = addr.addOffsetToAsOopHandle(0); | |
1142 return sapkg.utilities.RobustOopDeterminator.oopLooksValid(oopHandle); | |
1143 } | |
1144 | |
1145 // returns description of given pointer as a String | |
1146 function whatis(addr) { | |
1147 addr = any2addr(addr); | |
1148 var ptrLoc = findPtr(addr); | |
1149 if (ptrLoc.isUnknown()) { | |
1150 var vmType = vmTypeof(addr); | |
1151 if (vmType != null) { | |
1152 return "pointer to " + vmType.name; | |
1153 } else { | |
1154 var sym = closestSymbolFor(addr); | |
1155 if (sym != null) { | |
1156 return sym.name + '+' + sym.offset; | |
1157 } else { | |
1158 return ptrLoc.toString(); | |
1159 } | |
1160 } | |
1161 } else { | |
1162 return ptrLoc.toString(); | |
1163 } | |
1164 } |