854N/ACopyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 889N/ADO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/AThis code is free software; you can redistribute it and/or modify it 0N/Aunder the terms of the GNU General Public License version 2 only, as 854N/Apublished by the Free Software Foundation. Oracle designates this 0N/Aparticular file as subject to the "Classpath" exception as provided 0N/Aby Oracle in the LICENSE file that accompanied this code. 0N/AThis code is distributed in the hope that it will be useful, but WITHOUT 0N/AANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/AFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/Aversion 2 for more details (a copy is included in the LICENSE file that You should have received a copy of the GNU General Public License version 2 along with this work; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA or visit www.oracle.com if you need additional information or have any .key { color: red; font-weight: bold }
Object Query Language (OQL)
<
h1>Object Query Language (OQL)</
h1>
OQL is SQL-like query language to query Java heap. OQL allows to
filter/
select information
wanted from Java heap. While pre-defined queries such as "show all instances of class X"
are already supported by HAT, OQL adds more flexibility. OQL is based on JavaScript expression
<
span class="key">select</
span> <JavaScript expression to select>
[ <
span class="key">from</
span> [<
span class="key">instanceof</
span>] <class name> <identifier>
[ <
span class="key">where</
span> <JavaScript boolean expression to filter> ] ]
where class name is fully qualified Java class name (example:
java.
net.
URL) or array class name.
Note that fully qualified class name does not always uniquely identify a
Java class at runtime. There may be more than one Java class with the same
name but loaded by different loaders. So, class name is permitted to be
id string of the class object.
If <
span class="key">instanceof</
span> keyword is used, subtype objects are selected. If this
keyword is not specified, only the instances of exact class specified are selected. Both
<
span class="key">from</
span> and <
span class="key">where</
span> clauses are optional.
In <
span class="key">select</
span> and (optional) <
span class="key">where</
span> clauses, the expression
used in JavaScript expression. Java heap objects are wrapped as convenient script objects so that
fields may be accessed in natural syntax. For example, Java fields can be accessed with
obj.field_name syntax and array elements can be accessed with array[index] syntax. Each Java object selected is
bound to a JavaScript variable of the identifier name specified in <
span class="key">from</
span> clause.
<
li>select all Strings of length 100 or more
<
li>select all int arrays of length 256 or more
select a from [I a where
a.length >= 256
<
li>show content of Strings that match a regular expression
<
li>show path value of all File objects
<
li>show names of all ClassLoader classes
select <
a href="#classof">classof</
a>(cl).name
<
li>show instances of the Class identified by given id string
select o from instanceof 0xd404b198 o
Note that 0xd404b198 is id of a Class (in a session). This is found by
looking at the id shown in that class's page.
<
h2>OQL built-in objects, functions</
h2>
The <
b>heap</
b> built-in object supports the following methods:
<
code>clazz</
code> is the class whose instances are selected. If not specified, defaults to
java.lang.Object. <
code>includeSubtypes</
code> is a boolean flag
that specifies whether to include subtype instances or not. Default value of
where <
code>className</
code> is name of the class to find. The resulting Class
object has following properties:
<
li>name - name of the class.
<
li>superclass - Class object for super class (or null if
java.
lang.
Object).
<
li>statics - name, value pairs for static fields of the Class.
<
li>fields - array of field objects. field object has name, signature
<
li>loader - ClassLoader object that loaded this class.
<
li>signers - signers that signed this class.
<
li>protectionDomain - protection domain to which this class belongs.
Class objects have the following methods:
<
li>isSubclassOf - tests whether given class is direct or indirect
subclass of this class or not.
<
li>isSuperclassOf - tests whether given Class is direct or indirect
superclass of this class or not.
<
li>subclasses - returns array of direct and indirect subclasses.
<
li>superclasses - returns array of direct and indirect superclasses.
<
a name="findObject"></
a>
<
li><
b>
heap.classes</
b> -- returns an enumeration of all Java classes
<
li><
b>
heap.objects</
b> -- returns an enumeration of Java objects
<
code>clazz</
code> is the class whose instances are selected. If not specified, defaults to
java.lang.Object. <
code>includeSubtypes</
code> is a boolean flag
that specifies whether to include subtype instances or not. Default value of
this flag is true. This method accepts an optional filter expression to filter
the result set of objects.
<
a name="finalizables"></
a>
<
li><
b>
heap.livepaths</
b> -- return an array of paths by which a given object
is alive. This method accepts optional second parameter that is a boolean
flag. This flag tells whether to include paths with weak reference(s) or not.
By default, paths with weak reference(s) are not included.
Each element of this array itself is another array. The later array is
contains an objects that are in the 'reference chain' of the path.
<
li><
b>
heap.roots</
b> -- returns an Enumeration of Roots of the heap.
Each Root object has the following properties:
<
li>id - String id of the object that is referred by this root
<
li>type - descriptive type of Root (JNI Global, JNI Local, Java Static etc)
<
li>description - String description of the Root
<
li>referrer - Thread Object or Class object that is responsible for this root or null
<
li> find the object whose object id is given
<
li>select all classes that have name pattern
java.net.*
<
h3>functions on individual objects</
h3>
<
li><
a href="#allocTrace">allocTrace(jobject)</
a>
<
li><
a href="#classof">classof(jobject)</
a>
<
li><
a href="#forEachReferrer">forEachReferrer(callback, jobject)</
a>
<
li><
a href="#identical">identical(o1, o2)</
a>
<
li><
a href="#objectid">objectid(jobject)</
a>
<
li><
a href="#reachables">reachables(jobject, excludedFields)</
a>
<
li><
a href="#referrers">referrers(jobject)</
a>
<
li><
a href="#referees">referees(jobject)</
a>
<
li><
a href="#refers">refers(jobject)</
a>
<
li><
a href="#root">root(jobject)</
a>
<
li><
a href="#sizeof">sizeof(jobject)</
a>
<
li><
a href="#toHtml">toHtml(obj)</
a>
<
a name="allocTrace"></
a>
<
h4>allocTrace function</
h4>
This returns allocation site trace of a given Java object if available.
allocTrace returns array of frame objects. Each frame object has the following
<
li>className - name of the Java class whose method is running in the frame.
<
li>methodName - name of the Java method running in the frame.
<
li>methodSignature - signature of the Java method running in the frame.
<
li>sourceFileName - name of source file of the Java class running in the frame.
<
li>lineNumber - source line number within the method.
<
h4>classof function</
h4>
Returns Class object of a given Java Object. The result object supports the
<
li>name - name of the class.
<
li>superclass - Class object for super class (or null if
java.
lang.
Object).
<
li>statics - name, value pairs for static fields of the Class.
<
li>fields - array of field objects. Field objects have name, signature
<
li>loader - ClassLoader object that loaded this class.
<
li>signers - signers that signed this class.
<
li>protectionDomain - protection domain to which this class belongs.
Class objects have the following methods:
<
li>isSubclassOf - tests whether given class is direct or indirect
subclass of this class or not.
<
li>isSuperclassOf - tests whether given Class is direct or indirect
superclass of this class or not.
<
li>subclasses - returns array of direct and indirect subclasses.
<
li>superclasses - returns array of direct and indirect superclasses.
<
li>show class name of each Reference type object
<
a name="forEachReferrer"></
a>
<
h4>forEachReferrer function</
h4>
calls a callback function for each referrer of a given Java object.
<
h4>identical function</
h4>
Returns whether two given Java objects are identical or not.
<
h4>objectid function</
h4>
Returns String id of a given Java object. This id can be passed to
<
a name="reachables"></
a>
<
h4>reachables function</
h4>
Returns an array of Java objects that are transitively referred from the
given Java object. Optionally accepts a second parameter that is comma
separated field names to be excluded from reachability computation.
<
li>print all reachable objects from each Properties instance.
<
li>print all reachables from each
java.
net.
URL but omit the objects reachable
via the fields specified.
<
h4>referrers function</
h4>
Returns an enumeration of Java objects that hold reference to a given Java
<
li>print URL objects only if referred by 2 or more
select u from
java.
net.
URL u where count(referrers(u)) > 2
<
h4>referees function</
h4>
Returns an array of Java objects to which the given Java
object directly refers to.
Example: to print all static reference fields of
java.
io.
File class
Returns whether first Java object refers to second Java object or not.
If given object is a member of root set of objects, this function returns
a descriptive <
a href="#rootobj">Root object</
a> describing why it is so.
If given object is not a root, then this function returns null.
Returns size of the given Java object in bytes
select sizeof(o) from [I o
Returns HTML string for the given Java object. Note that this is called
automatically for objects selected by select expression. But, it may be useful
to print more complex output.
Example: print hyperlink in bold font weight
<
h3>Selecting multiple values</
h3>
Multiple values can be selected using JavaScript object literals or arrays.
Example: show name and thread for each thread object
expression string [or a callback function] as input. These functions iterate
each element. Note that JavaScript objects are associative arrays. So,
these functions may also be used with arbitrary JavaScript objects.
Concatenates two arrays or enumerations (
i.e., returns composite
<
h4>contains function</
h4>
the given boolean expression specified in code. The code evaluated
can refer to the following built-in variables.
<
li>it -> currently visited element
<
li>index -> index of the current element
Example: select all Properties objects that are referred by
some static field some class.
where contains(<
a href="#referrers">referrers</
a>(p), "<
a href="#classof">classof</
a>(it).name == '
java.
lang.
Class'")
that satisfy the given boolean expression. The boolean expression code can
refer to the following built-in variables.
<
li>it -> currently visited element
<
li>index -> index of the current element
Example: print number of classes that have specific name pattern
expression. The boolean expression code can refer to the following built-in
<
li>it -> currently visited element
<
li>index -> index of the current element
<
li>show all classes that have
java.io.* name pattern
<
li> show all referrers of URL object where the referrer is not from
select filter(<
a href="#referrers">referrers</
a>(u), "! /
java.net./(<
a href="#classof">classof</
a>(it).name)")
on each element. The code evaluated can refer to the following built-in
<
li>it -> currently visited element
<
li>index -> index of the current element
Example: show all static fields of
java.
io.
File with name and value
select map(<
a href="#findClass">
heap.findClass</
a>("
java.
io.
File").statics, "index + '=' + <
a href="#toHtml">toHtml</
a>(it)")
Optionally accepts code expression to compare elements of the array.
By default numerical comparison is used. The comparison expression can
use the following built-in variables:
<
li>lhs -> left side element for comparison
<
li>rhs -> right side element for comparison
<
li>find the maximum length of any String instance
<
li>find string instance that has the maximum length
accepts code expression to compare elements of the array. By default numerical
comparison is used. The comparison expression can use the following built-in
<
li>lhs -> left side element for comparison
<
li>rhs -> right side element for comparison
<
li>find the minimum size of any Vector instance
<
li>find Vector instance that has the maximum length
compare elements of the array. By default numerical comparison is used. The comparison expression can use the following built-in variables: <li>lhs -> left side element for comparison <li>rhs -> right side element for comparison <li> print all char[] objects in the order of size. select sort(<a href="#objects">heap.objects</a>('[
C'), '<a href="#sizeof">sizeof</
a>(lhs) - sizeof(rhs)')
<
li> print all char[] objects in the order of size but print
select <
a href="#map">map</
a>(sort(<
a href="#objects">
heap.objects</
a>('[C'), '<
a href="#sizeof">sizeof</
a>(lhs) - sizeof(rhs)'), '{ size: sizeof(it), obj: it }')
This function returns the sum of all the elements of the given input array or
enumeration. Optionally, accepts an expression as second param. This is used
to map the input elements before summing those.
Example: return sum of sizes of the reachable objects from each Properties object
select sum(<
a href="#map">map</
a>(<
a href="#reachables">reachables</
a>(p), '<
a href="#sizeof">sizeof</
a>(it)'))
// or omit the map as in ...
select sum(<
a href="#reachables">reachables</
a>(p), '<
a href="#sizeof">sizeof</
a>(it)')
<
h4>toArray function</
h4>
This function returns an array that contains elements of the input
Example: select unique char[] instances referenced from Strings. Note that
more than one String instance can share the same char[] for the content.
// number of unique char[] instances referenced from any String
// total number of Strings
<
h3>More complex examples</
h3>
<
h4>Print histogram of each class loader and number of classes loaded by it</
h4>
'toHtml(
it) +
"<br>"') field called <b>classes</b> of type <b>java.util.Vector</b> and Vector has a private field named <b>elementCount</b> that is number of elements in the vector. We select multiple values (loader, count) using JavaScript object literal and map function. We sort the result by count (i.e., number of classes loaded) using sort function with comparison expression. <h4>Show parent-child chain for each class loader instance</h4> res += toHtml(it) + "->"; return res + "<br>"; and walk until parent is null using the callback function to map call. <h4>Printing value of all System properties</h4> The above query uses the following facts: entry points the next entry (or null) in the same hashtable bucket. <b>Note that this query (and many other queries) may not be stable - because private fields of Java platform classes may be modified/removed without any notification! (implementation detail)</b>. But, using such queries on user classes may be safe - given that user has the control over the classes.