1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
|
<html>
<head>
<title>
JavaScript interface to Hotspot Serviceability Agent
</title>
</head>
<body>
<h1>JavaScript interface to Hotspot Serviceability Agent</h1>
<p>
Serviceability Agent (SA) provides Java API and tools to diagnose HotSpot Virtual Machine and
Java apps running on it. SA is a snapshot debugger -- can be used to observe state of a frozen java process or java core dump.
</p>
<h2>Existing SA APIs</h2>
<p>
There are two application programmer interfaces (APIs) for SA:
<dl>
<dt>1. Private java API
</dt>
<dd>
This tries to mimic hotspot VM's internal C++ classes and methods. Because VM data structures
are a moving target, this API can never be 'stable'! Besides, to use SA's private API knowledge of
HotSpot code base is essential.
</dd>
<dt>2. SA-JDI -- Java Debugger Interface read-only subset API
</dt>
<dd>
This is read-only subset of JDI (Java Debugger Interface)
This is a standardized interface to get java level state of a java process or java core dump. While this
interface is useful, this misses parts of java level state from target process or core such as
<ul>
<li>heap walking interface -- only objects traceable to static variables (of classes) and local
variables of stack frames can be accessed.
<li>re-constructing .class from debuggee are missing.
<li>re-constructing object mirrors for Java objects of the debuggee.
</ul>
</dd>
</dl>
</p>
<h2>SA Scripting interface</h2>
<p>
Traditionally, platform debuggers such as dbx, gdb and Solaris mdb (Module Debugger), provide a scripting
language interface. Scripting language interface provides easy-to-use, dynamically typed
interface to access data structures from debuggee. dbx and mdb even allow user to write
C/C++ modules to extend the scripting language commands.
</p>
<p>
SA provides SOQL - Simple Object Query Language -- a SQL-like query language to access
Java heap as an object database. SA's main GUI (HSDB) also exposes scripting interface of underlying debugger such as dbx, windbg.
But to use this interface, user has to learn scripting interface of multiple debugger back-ends such as dbx, windbg.
And these scripting interfaces are 'raw' in the sense that no java state is exposed -- only C/C++ state of VM is exposed.
Higher level SA services are not available through scripting interface.
</p>
<p>
<b>jsdb -- JavaScript Debugger</b> attempts to provide JavaScript interface to SA.
jsdb provides
<ul>
<li>high-level hotspot (and SA) independent scripting interface
<li>low-level SA-aware scripting interface.
</ul>
</p>
<h2>High level interface (Java State)</h2>
<b>jsdb</b> is a command line <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">JavaScript</a> shell based on
<a href="http://www.mozilla.org/rhino/">Mozilla's Rhino JavaScript Engine</a>.
This command line utility attaches to Java process or core file or remote debug server and waits for user input.
This shell supports the following global functions and objects in addition to the standard JavaScript functions and
objects:
<h3>jdsb globals</h3>
<table border="1">
<tr>
<th>Function/Variable</th>
<th>Description</th>
</tr>
<tr>
<td>
address(jobject)
</td>
<td>
function that returns the address of the Java object as a string
</td>
</tr>
<td>
classof(jobject)
</td>
<td>
function that returns the JavaScript object that represents class object of the Java object
</td>
</tr>
<td>
dumpClass(jclass,[dir])
</td>
<td>
function that writes .class for the given Java Class. Optionally (second arg) accepts the directory where the
.class has to be written.
</td>
</tr>
<td>
help()
</td>
<td>
function that prints help message for global functions and objects
</td>
</tr>
<tr>
<td>
identityHash(jobject)
</td>
<td>
function that returns the identity hashCode of the Java object
</td>
</tr>
<tr>
<td>
mirror(jobject)
</td>
<td>
function that returns a local mirror of the Java object.
</td>
</tr>
<tr>
<td>
load([file1, file2,...])
</td>
<td>
function that loads zero or more JavaScript file(s). With no arguments, reads <stdin> for
JavaScript code.
</td>
</tr>
<tr>
<td>
object(string)
</td>
<td>
function that converts a string address into Java object
</td>
</tr>
<tr>
<td>
owner(jobject)
</td>
<td>
function that returns the owner thread of this monitor or null
</td>
</tr>
<tr>
<td>
sizeof(jobject)
</td>
<td>
function that returns the size of Java object in bytes
</td>
</tr>
<tr>
<td>
staticof(jclass, field)
</td>
<td>
function that returns the value of given field of the given Java class
</td>
</tr>
<tr>
<td>
print(expr1, expr2,...)
</td>
<td>
function that prints zero or more JavaScript expressions after converting those as strings
</td>
</tr>
<tr>
<td>
println(expr1, expr2..)
</td>
<td>
function that same as print, but prints a newline at the end
</td>
</tr>
<tr>
<td>
read([prompt])
</td>
<td>
function that reads a single line from standard input
</td>
</tr>
<tr>
<td>
quit()
</td>
<td>
function that quits the interactive load call as well as the shell
</td>
</tr>
<tr>
<td>
jvm
</td>
<td>
variable -- a JavaScript object that represents the target jvm that is being debugged
</td>
</tr>
</table>
<h3>jvm object</h3>
<p>
jvm object supports the following read-only properties.
</p>
<table border="1">
<tr>
<th>
Property name
</th>
<th>
Description
</th>
</tr>
<tr>
<td>
threads
</td>
<td>
array of Java threads from the debuggee
</td>
</tr>
<tr>
<td>
heap
</td>
<td>
object representing the heap of the debuggee
</td>
</tr>
<tr>
<td>
type
</td>
<td>
string value that is either "Server" or "Client" or "Core" -- the flavour of debuggee VM
</td>
</tr>
<tr>
<td>
bootClassPath
</td>
<td>
string value of bootclasspath of the debuggee
</td>
</tr>
<tr>
<td>
cpu
</td>
<td>
string value of cpu on which the debuggee runs/ran
</td>
</tr>
<tr>
<td>
sysProps
</td>
<td>
name-value pairs (JavaScript associative array) of Java System properties of the debuggee
</td>
</tr>
<tr>
<td>
addressSize
</td>
<td>
int value -- 32 for 32 bit debuggee, 64 for 64 bit debuggee
</td>
</tr>
<tr>
<td>
os
</td>
<td>
string value of OS on which the debuggee runs/ran
</td>
</tr>
<tr>
<td>
buildInfo
</td>
<td>
internal build info string from debuggee
</td>
</tr>
<tr>
<td>
flags
</td>
<td>
name-value pairs (JavaScript associative array) of JVM command line flags of the debuggee
</td>
</tr>
<tr>
<td>
classPath
</td>
<td>
string value of classpath of the debuggee
</td>
</tr>
<tr>
<td>
userDir
</td>
<td>
string value of user.dir System property of the debuggee
</td>
</tr>
</table>
<h3>heap object</h3>
<p>
heap object represents Java heap of the debuggee VM
</p>
<table border="1">
<tr>
<th>
Function or property name
</th>
<th>
Description
</th>
</tr>
<tr>
<td>
capacity
</td>
<td>
byte size of capacity of the heap
</td>
</tr>
<tr>
<td>
used
</td>
<td>
byte size of used portion (of live objects) of the heap
</td>
</tr>
<tr>
<td>
forEachObject(func, [class], [include subtypes -- true|false])
</td>
<td>
This function accepts a callback function 'func' and optionally class name and boolean arguments.
This function calls the callback for each Java object in the debuggee's heap. The optional class
argument may be used to receive objects of given class only. The third arguments specifies whether
to include objects of subtype of given class [or interface] or not. The default value of class is "java.lang.Object"
and and that of the third argument is true. i.e., by default all objects are included.
</td>
</tr>
<tr>
<td>
forEachClass(func, [initiating loader -- true|false])
</td>
<td>
This function accepts a callback function 'func'. This function iterates through the classes of the debuggee and calls the
callback for each class. The second parameter tells whether to pass initiating loader to the iterator callback or not.
</td>
</tr>
</table>
<h3>Accessing Java objects and arrays in script</h3>
<p>
From a given Java object, we can access all fields of the Java object by usual '.' operator. i.e., if you got a Java object
called 'o' of type java.lang.Thread from debuggee, you can access 'stackSize' field by o.stackSize syntax. Similarly, length of Java array
objects can be accessed by length property. And array indexing follows usual syntax. i.e., n'th element of array 'a' is
accessed by a[n].
</p>
<h3>jvm.threads array</h3>
<p>
This is a JavaScript array of Java threads of the debuggee. As usual, 'length' property tells the number of threads and individual
threads may be accessed by index operator -- i.e, jvm.threads[0] returns the first thread.
</p>
<h3>thread object</h3>
<p>
In addition to the fields of java.lang.Thread (or subclass) fields, thread objects have two additional properties.
<ul>
<li>frames -- array of stack frame objects
<li>monitors -- array of monitor objects owned by the thread
</ul>
</p>
<h3>stack frame object</h3>
<table border="1">
<tr>
<th>
Property name
</th>
<th>
Description
</th>
</tr>
<td>
thisObject
</td>
<td>
Object representing 'this' of the current frame [will be null for static methods]
</td>
</tr>
<tr>
<td>
locals
</td>
<td>
name-value pairs of local variables [JavaScript associative array]
</td>
</tr>
<tr>
<td>
line
</td>
<td>
Java source line number at which the frame is executing
</td>
</tr>
<tr>
<td>
bci
</td>
<td>
byte code index of the bytecode that the frame is executing
</td>
</tr>
<tr>
<td>
thread
</td>
<td>
thread to which this frame belongs
</td>
</tr>
<tr>
<td>
method
</td>
<td>
Java method that the frame is executing
</td>
</tr>
</table>
<h3>method object</h3>
<p>
method object represents a Java method of debuggee
</p>
<table border="1">
<tr>
<th>
Property name
</th>
<th>
Description
</th>
</tr>
<tr>
<td>
isStatic
</td>
<td>
boolean - true for static methods and false for non-static methods
</td>
</tr>
<tr>
<td>
isSynchronized
</td>
<td>
boolean - true for synchronized methods and false for non-synchronized methods
</td>
</tr>
<tr>
<td>
isNative
</td>
<td>
boolean - true for native methods and false for non-native methods
</td>
</tr>
<tr>
<td>
isProtected
</td>
<td>
boolean - true for protected methods and false for non-protected methods
</td>
</tr>
<tr>
<td>
isPrivate
</td>
<td>
boolean - true for private methods and false for non-private methods
</td>
</tr>
<tr>
<td>
isSynthetic
</td>
<td>
boolean - true for Javac generated synthetic methods and false for non-synthetic methods
</td>
</tr>
<tr>
<td>
isPackagePrivate
</td>
<td>
boolean - true for package-private methods and false for non-package-private methods
</td>
</tr>
<tr>
<td>
isPublic
</td>
<td>
boolean - true for public methods and false for non-public methods
</td>
</tr>
<tr>
<td>
holder
</td>
<td>
an object that represents Class that contains this method
</td>
</tr>
<tr>
<td>
signature
</td>
<td>
string -- signature of this method
</td>
</tr>
<tr>
<td>
isObsolete
</td>
<td>
boolean - true for obsolete (hotswapped) methods and false for non-obsolete methods
</td>
</tr>
<tr>
<td>
isStrict
</td>
<td>
boolean - true for strictfp methods and false for non-strictfp methods
</td>
</tr>
<tr>
<td>
isFinal
</td>
<td>
boolean - true for final methods and false for non-final methods
</td>
</tr>
<tr>
<td>
name
</td>
<td>
string - name of this method
</td>
</tr>
</table>
<h3>class object</h3>
<p>
A class object represents loaded Java class in debuggee VM. This represents java.lang.Class instance in the debuggee.
This is type of return value of classof global function. Also, method.holder property and field.holder are
of this type.
</p>
<table border="1">
<tr>
<th>
Property name
</th>
<th>
Description
</th>
</tr>
<tr>
<td>
name
</td>
<td>
name of this class
</td>
</tr>
<tr>
<td>
superClass
</td>
<td>
class object representing super class of this class
</td>
</tr>
<tr>
<td>
isArrayClass
</td>
<td>
boolean -- is the current class an array class?
</td>
</tr>
<tr>
<td>
isStatic
</td>
<td>
boolean -- is the current class static or not
</td>
</tr>
<tr>
<td>
isInterface
</td>
<td>
boolean -- is the current class an interface
</td>
</tr>
<tr>
<td>
isAbstract
</td>
<td>
boolean -- is the current class abstract or not
</td>
</tr>
<tr>
<td>
isProtected
</td>
<td>
boolean -- is the current class protected or not
</td>
</tr>
<tr>
<td>
isPrivate
</td>
<td>
boolean -- is the current class private or not
</td>
</tr>
<tr>
<td>
isPackagePrivate
</td>
<td>
boolean -- is the current class package private or not
</td>
</tr>
<tr>
<td>
isSynthetic
</td>
<td>
boolean -- is the current class synthetic or not
</td>
</tr>
<tr>
<td>
classLoader
</td>
<td>
object that represents ClassLoader object that loaded the current class
</td>
</tr>
<tr>
<td>
fields
</td>
<td>
array of static and instance fields of the current class
</td>
</tr>
<tr>
<td>
protectionDomain
</td>
<td>
protection domain to which current class belongs
</td>
</tr>
<tr>
<td>
isPublic
</td>
<td>
boolean -- is the current class public or not
</td>
</tr>
<tr>
<td>
signers
</td>
<td>
array of signers for current class
</td>
</tr>
<tr>
<td>
sourceFile
</td>
<td>
string -- name of the source file for current class
</td>
</tr>
<tr>
<td>
interfaces
</td>
<td>
array -- interfaces implemented by current class
</td>
</tr>
<tr>
<td>
isStrict
</td>
<td>
boolean -- is the current class strictfp or not
</td>
</tr>
<tr>
<td>
methods
</td>
<td>
array of methods (static and instance) of the current class
</td>
</tr>
<tr>
<td>
isFinal
</td>
<td>
boolean -- is the current class final or not
</td>
</tr>
<tr>
<td>
statics
</td>
<td>
name-value pairs (JavaScript associate array) of static fields of the current class
</td>
</tr>
</table>
<h3>field object</h3>
<p>
field represents a static or instance field of some class in debuggee
</p>
<table border="1">
<tr>
<th>
Property name
</th>
<th>
Description
</th>
</tr>
<tr>
<td>
isStatic
</td>
<td>
boolean -- is this field a static field?
</td>
</tr>
<tr>
<td>
holder
</td>
<td>
class that owns this field
</td>
</tr>
<tr>
<td>
signature
</td>
<td>
string signature of this field
</td>
</tr>
<tr>
<td>
isProtected
</td>
<td>
boolean - is this field a protected field or not?
</td>
</tr>
<tr>
<td>
isPrivate
</td>
<td>
boolean - is this field a private field or not?
</td>
</tr>
<tr>
<td>
isSynthetic
</td>
<td>
boolean - is this javac generated synthetic field or not?
</td>
</tr>
<tr>
<td>
isPackagePrivate
</td>
<td>
boolean - is this field a package private field or not?
</td>
</tr>
<tr>
<td>
isTransient
</td>
<td>
boolean - is this field a transient field or not?
</td>
</tr>
<tr>
<td>
isFinal
</td>
<td>
boolean - is this field final or not?
</td>
</tr>
<tr>
<td>
name
</td>
<td>
string - name of this field
</td>
</tr>
<tr>
<td>
isPublic
</td>
<td>
boolean - is this field public or not?
</td>
</tr>
</table>
<h3>Initialization Script</h3>
<p>
jsdb engine looks for initialization script file named <b>jsdb.js</b> in user's home directory. If found, it loads just after attaching to debuggee but before printing prompt for user's input. User can assume that s/he can access debuggee VM
state during initialization script.
</p>
<h3>Sample scripts</h3>
Semantics and knowledge of application classes (for eg. AppServer's classes) would be needed to create app specific
scripts. The following script samples are app-independent and provide a flavour of kind of scripts that can be written.
<h4>Script to print system properties of JVM</h4>
<pre>
<code>
jvm.sysProps.toString()
</code>
</pre>
<h4>Script to print JVM command line flags</h4>
<pre>
<code>
jvm.flags.toString()
</code>
</pre>
<h4>Script to print class-wise histogram of objects</h4>
<pre>
<code>
// associate array to hold histogram
var histo;
function func(obj) {
var classname = classof(obj).name;
if (histo[classname] == undefined) {
// first time we are visiting this class type
histo[classname] = 1;
} else {
histo[classname]++;
}
}
// iterate through java heap calling 'func' for each object
jvm.heap.forEachObject(func);
// print the histogram
for (i in histo) {
println('number of instances of ', i, ' = ', histo[i]);
}
</code>
</pre>
<h4>Script to print stack trace of all Java threads</h4>
<pre>
<code>
function printStackTrace(t) {
println(t.name);
println('');
for (i in t.frames) {
println(t.frames[i]);
}
println('');
}
// walk through the list of threads and call printStackTrace
// for each thread
for (o in jvm.threads) {
printStackTrace(jvm.threads[o]);
}
</code>
</pre>
<h4>Script to re-construct .class files for all non-bootstrap classes</h4>
<pre>
<code>
function dump(cl) {
if (!cl.isArrayClass && cl.classLoader != null) {
// not an array class and a non-bootstrap class
// create .class files in e:\tmp dir
dumpClass(cl, "e:\\tmp);
} else {
println("skipping bootstrap class ", cl.name);
}
}
// walk thru heap and call callback for each java.lang.Class instance
jvm.heap.forEachObject(dump, "java.lang.Class");
</code>
</pre>
<h4>Script to print paths of all java.io.File's currently accessed</h4>
<pre>
<code>
function printFile(f) {
// construct a mirror java.io.File here and
// print absolute path here
println(mirror(f).getAbsolutePath());
}
jvm.heap.forEachObject(printFile, "java.io.File");
</code>
</pre>
<h4>Script to print static fields of java.lang.Thread class</h4>
<pre>
<code>
var threadClass = classof("java.lang.Thread");
for (i in threadClass.statics) {
println(i, '=', threadClass.statics[i]);
}
</code>
</pre>
<h3>Low level interface (VM State)</h3>
<p>
Low level jsdb interface works by <a href="http://www.mozilla.org/rhino/ScriptingJava.html">JavaScript-to-Java (previously known as "LiveConnect")
interface</a> provided by Rhino JavaScript engine.
</p>
<h2>sapkg object</h2>
<p>
This object provides short names for SA package names. For eg. instead of writing
Packages.sun.jvm.hotspot.memory, we can write sapkg.memory.
</p>
<h2>sa object</h2>
<p>
This object contains all SA singleton objects such as VM, Universe, SymbolTable,
SystemDictionary, ObjectHeap, CollectedHeap, Debugger, CDebugger (if available),
Interpreter, TypeDataBase and Threads. For eg. to access SymbolTable of Java debuggee,
we can use sa.symbolTable. User can execute the following code to get fields of this object.
</p>
<pre>
<code>
for (i in sa) {
println(i);
}
</code>
</pre>
<h4>Heap Iterators</h4>
<dl>
<dt>forEachOop(callback)</dt>
<dd>calls a callback function for each Oop in Java heap</dd>
<dt>forEachOopOfKlass(callback, klass, [includeSubtypes])</dt>
<dd>calls a callback function for each Oop of a give Klass type
Optinally, third argument can specify whether to include subtype Oops
or not.
</dd>
</dl>
<h4>System Dictionary Access</h4>
<dl>
<dt>forEachKlass(callback)</dt>
<dd>calls a callback function for each Klass in Java heap</dd>
<dt>forEachKlassAndLoader(callback)</dt>
<dd>
calls callback with Klass and initiating loader (Oop) for System dictionary
entry.
</dd>
<dt>forEachPrimArrayKlass(callback)</dt>
<dd>
calls callback with Klass and initiating loader (Oop) for each
primitive array Klass in the system.
</dd>
<dt>findInstanceKlass(name)</dt>
<dd>
finds the first instance klass with given name from System dictionary
</dd>
</dl>
<h4>Thread, Frame Iterators</h4>
<dl>
<dt>forEachJavaThread(callback)</dt>
<dd>calls callback for each Java Thread</dd>
<dt>forEachFrame(javaThread, callback)</dt>
<dd>calls callback for each Frame of a given JavaThread</dd>
<dt>forEachVFrame(javaThread, callback)</dt>
<dd>calls callback for each JavaVFrame of a given JavaThread</dd>
<dt>forEachThread(callback)</dt>
<dd>calls callback for each (native) ThreadProxy (obtained by CDebugger.getThreadList)
</dd>
<dt>forEachCFrame(threadProxy, callback)</dt>
<dd>
calls callback for each CFrame of a given ThreadProxy object
</dd>
</dl>
<h4>Code blobs, Interpreter codelets</h4>
<dl>
<dt>forEachCodeBlob(callback)</dt>
<dd>
calls callback with each code blob in code cache
</dd>
<dt>findCodeBlob(address)</dt>
<dd>
finds the code blob, if any, that contains the given address.
Returns null, on failure.
</dd>
<dt>findNMethod(address)</dt>
<dd>
finds the NMethod that contains given address.
</dd>
<dt>pcDescAt(addr)</dt>
<dd>
returns PCDesc at given address or null.
</dd>
<dt>forEachInterpCodelet(callbacl)</dt>
<dd>
calls callback with each Interpreter codelet
</dd>
</dl>
<h4>VM structs, constants</h4>
<dl>
<dt>forEachType(callback)</dt>
<dd>
calls callback for each Type in VM's type database
</dd>
<dt>forEachVMIntConst(callback)</dt>
<dd>
calls callback for each named integer constant. passes name
as argument.
</dd>
<dt>forEachVMLongConst(callback)</dt>
<dd>
calls callback for each named long constant. passes name
as argument.
</dd>
<dt>findVMType(name)</dt>
<dd>
finds a VM type by name. returns null if no known Type of given name
exists in type database.
</dd>
<dt>findVMIntConst(name)</dt>
<dd>
finds an integer constant in type data base by name.
</dd>
<dt>findVMLongConst(name)</dt>
<dd>
finds an long constant in type data base by name.
</dd>
<dt>vmTypeof(addr)</dt>
<dd>
returns VM type of object at 'addr' if any. Else, returns null.
</dd>
<dt>isOfVMType(addr, type)</dt>
<dd>
returns whether object at 'addr' is of VM type 'type' or not.
</dd>
<dt>printVMType(type, addr)</dt>
<dd>
prints 'addr' as VM object of type 'type'
</dd>
<dt>print<i>XXX</i>(addr)</dt>
<dd>
For each VM type, these functions are defined. For eg. there is printUniverse,
printSystemDictionary etc. are available. Without 'addr' being passed static fields are printed. With 'addr' param being passed, instance fields are printed.
</dd>
</dl>
<h4>Low level debugger facilities</h4>
<dl>
<dt>num2addr(number)</dt>
<dd>
converts a (long) number to SA Address instance
</dd>
<dt>str2addr(string)</dt>
<dd>
converts a given hex string to SA Address instance
</dd>
<dt>any2addr(any)</dt>
<dd>
Takes a number or a string or an Address and returns
an Address instance. For other types, returns 'undefined'
</dd>
<dt>addr2str(addr)</dt>
<dd>
converts a given Address instance to a hex string
</dd>
<dt>addr2num(addr)</dt>
<dd>
converts a given Address instance to a (long) number
</dd>
<dt>sym2addr(library, symbol)</dt>
<dd>
returns Address of a given symbol in a given library (shared object or DLL)
Example: sym2addr('jvm.dll', 'JNI_CreateJavaVM')
<dt>addr2sym(addr)</dt>
<dd>
Returns nearest symbol to a given address (if any). If no such symbol is found,
returns the given address as a string.
</dd>
<dt>readBytesAt(addr, num)</dt>
<dd>
returns 'num' bytes at 'addr' as a Java byte[]
</dd>
<dt>readWordsAt(addr, num)</dt>
<dd>
returns 'num' words at 'addr' as a Java long[]
</dd>
<dt>readCStrAt(addr)</dt>
<dd>
returns 'C' String at given address
</dd>
<dt>readCStrLen(addr)</dt>
<dd>
returns the length of the 'C' String at given address
</dd>
<dt>readRegs(threadProxy)</dt>
<dd>
returns register set (of Thread Context) of a given thread specified
by threadProxy. return value is an associate array having name-value pairs
of registers.
</dd>
<dt>regs(threadProxy)</dt>
<dd>
prints register set of a given thread.
</dd>
<dt>mem(addr, [num])</dt>
<dd>
prints 'num' words (address size) at 'addr'. Prints nearest symbol for address, if found.
</dd>
<dt>dis(addr, [num])</dt>
<dd>prints native code disassembly of 'num' bytes at given address 'addr'.
Default value of 'num' is 4. This automatically detects whether the given address
inside a nmethod. If so, it prints safepoint info, entry points , method signature etc.
of the nmethod.
</dd>
<dt>jdis(method [or addr])</dt>
<dd>
prints Java bytecode disassembly for given method Oop or address of a method Oop.
</dd>
<dt>nmethoddis(nmethod)</dt>
<dd>
prints disassembly of given nmethod object. Note that you don't have to call this directly
instead use 'dis'.
</dd>
<dt>where</dt>
<dd>
prints Java stack trace for all Java threads
</dd>
</dl>
<h4>Miscellaneous</h4>
<dl>
<dt>addr2oop(addr)</dt>
<dd>
converts a given address to a Oop object
</dd>
<dt>oop2addr(oop)</dt>
<dd>
returns address of a given Oop object
</dd>
<dt>isOfVMType(addr, type)</dt>
<dd>
returns whether the given 'addr' points to a (C++) VM object of specified
type. type may be specified by SA Type object or string name of the type.
</dd>
<dt>newVMObject(addr)</dt>
<dd>
returns instance of SA object for a given address (similar to SA VirtualConstructor
interface).
</dd>
<dt>vmobj2addr(vmobject)</dt>
<dd>
returns Address represented by a given SA VMObject
</dd>
<dt>addr2vmobj(addr)</dt>
<dd>same as newVMObject(addr)</dd>
<dt>whatis(addr)</dt>
<dd>
returns string description of given address (using SA FindPointer and guess type API).
<dt>isOop(addr)</dt>
<dd>
returns whether a given address is a valid Oop address or not
</dd>
</dl>
<h4>Moving b/w jsdb low level and high level interfaces</h4>
<p>
Java objects of debuggee are represented by different script wrappers in high level
interface. In the low-level interface these are instances of SA Oop class or its'
subclass. To move b/w low-level and high-level interfaces the following functions may
be used
</p>
<dl>
<dt>oop2obj(oop)</dt>
<dd>
converts a given Oop object to a high-level wrapper object
</dd>
<dt>obj2oop(obj)</dt>
<dd>
converts a jsdb high level wrapper to underlying Oop instance
</dd>
</dl>
<h3>JavaScript tips</h3>
<ul>
<li>to know properties, functions of any object, use the script
<pre>
<core>
for(i in object) { println(i); }
</code>
</pre>
<li>to view the source code of any function, just type the name of
function in jsdb prompt
<li>to view global functions, properties, run
<pre>
<code>
for(i in this) { println(i); }
</code>
</pre>
</ul>
</body>
</html>
|