<!--
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
under the terms of the GNU General Public License version 2 only, as
published by the Free Software Foundation. Oracle designates this
particular file as subject to the "Classpath" exception as provided
by Oracle in the LICENSE file that accompanied this code.
This code is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
version 2 for more details (a copy is included in the LICENSE file that
accompanied this code).
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
questions.
-->
<html>
<head>
<style>
.key { color: red; font-weight: bold }
</style>
<title>
Object Query Language (OQL)
</title>
</head>
<body>
<h1>Object Query Language (OQL)</h1>
<p>
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
language.
</p>
<p>
OQL query is of the form
<pre>
<code>
<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> ] ]
</code>
</pre>
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.
</p>
<p>
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.
</p>
<h2>OQL Examples</h2>
<ul>
<li>select all Strings of length 100 or more
<pre>
<code>
</code>
</pre>
<li>select all int arrays of length 256 or more
<pre>
<code>
select a from [I a where a.length >= 256
</code>
</pre>
<li>show content of Strings that match a regular expression
<pre>
<code>
where /java/(s.value.toString())
</code>
</pre>
<li>show path value of all File objects
<pre>
<code</b>
</code>
</pre>
<li>show names of all ClassLoader classes
<pre>
<code>
select <a href="#classof">classof</a>(cl).name
</code>
</pre>
<li>show instances of the Class identified by given id string
<pre>
<code>
select o from instanceof 0xd404b198 o
</code>
</pre>
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.
</ul>
<h2>OQL built-in objects, functions</h2>
<h3>heap object</h3>
The <b>heap</b> built-in object supports the following methods:
<ul>
<pre>
<code>
heap.forEachClass(callback);
</code>
</pre>
<pre>
<code>
heap.forEachObject(callback, clazz, includeSubtypes);
</code>
</pre>
<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.
<a name="findClass"></a>
<pre>
<code>
heap.findClass(className);
</code>
</pre>
where <code>className</code> is name of the class to find. The resulting Class
object has following properties:
<ul>
<li>name - name of the class.
<li>statics - name, value pairs for static fields of the Class.
<li>fields - array of field objects. field object has name, signature
properties.
<li>loader - ClassLoader object that loaded this class.
<li>signers - signers that signed this class.
<li>protectionDomain - protection domain to which this class belongs.
</ul>
Class objects have the following methods:
<ul>
<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.
</ul>
<a name="findObject"></a>
<pre>
<code>
heap.findObject(stringIdOfObject);
</code>
</pre>
<a name="classes"></a>
<a name="objects"></a>
<pre>
<code>
heap.objects(clazz, [includeSubtypes], [filter])
</code>
</pre>
<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>
pending to be finalized.
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.
<pre>
<code>
</code>
</pre>
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.
<a name="rootobj"></a>
Each Root object has the following properties:
<ul>
<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
</ul>
</ul>
Examples:
<ul>
<pre>
<code>
</code>
</pre>
<pre>
<code>
</code>
</pre>
<li> find the object whose object id is given
<pre>
<code>
select heap.findObject("0xf3800b58")
</code>
</pre>
<li>select all classes that have name pattern java.net.*
<pre>
<code>
</code>
</pre>
</ul>
<h3>functions on individual objects</h3>
<ul>
<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>
</ul>
<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
properties:
<ul>
<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.
</ul>
<a name="classof"></a>
<h4>classof function</h4>
Returns Class object of a given Java Object. The result object supports the
following properties:
<ul>
<li>name - name of the class.
<li>statics - name, value pairs for static fields of the Class.
<li>fields - array of field objects. Field objects have name, signature
properties.
<li>loader - ClassLoader object that loaded this class.
<li>signers - signers that signed this class.
<li>protectionDomain - protection domain to which this class belongs.
</ul>
Class objects have the following methods:
<ul>
<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.
</ul>
Examples:
<ul>
<li>show class name of each Reference type object
<pre>
<code>
</code>
<pre>
<code>
</code>
<pre>
<code>
</code>
</pre>
</ul>
<a name="forEachReferrer"></a>
<h4>forEachReferrer function</h4>
calls a callback function for each referrer of a given Java object.
<a name="identical"></a>
<h4>identical function</h4>
<p>
Returns whether two given Java objects are identical or not.
</p>
Example:
<pre>
<code>
</code>
</pre>
<a name="objectid"></a>
<h4>objectid function</h4>
<p>
Returns String id of a given Java object. This id can be passed to
objects for identity.
</p>
Example:
<pre>
<code>
</code>
</pre>
<a name="reachables"></a>
<h4>reachables function</h4>
<p>
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.
Fields are written in class_name.field_name pattern.
</p>
Examples:
<ul>
<li>print all reachable objects from each Properties instance.
<pre>
<code>
</code>
</pre>
via the fields specified.
<pre>
<code>
</code>
</pre>
</ul>
<a name="referrers"></a>
<h4>referrers function</h4>
<p>
Returns an enumeration of Java objects that hold reference to a given Java
object.
</p>
Examples:
<ul>
<pre>
<code>
</code>
</pre>
<pre>
<code>
</code>
</pre>
<li>print URL objects only if referred by 2 or more
<pre>
<code>
</code>
</pre>
</ul>
<a name="referees"></a>
<h4>referees function</h4>
<p>
Returns an array of Java objects to which the given Java
object directly refers to.
</p>
<pre>
<code>
</code>
</pre>
<a name="refers"></a>
<h4>refers function</h4>
<p>
Returns whether first Java object refers to second Java object or not.
</p>
<a name="root"></a>
<h4>root function</h4>
<p>
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.
</p>
<a name="sizeof"></a>
<h4>sizeof function</h4>
Returns size of the given Java object in bytes
Example:
<pre>
<code>
select sizeof(o) from [I o
</code>
</pre>
<a name="toHtml"></a>
<h4>toHtml function</h4>
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
<pre>
<code>
</code>
</pre>
<h3>Selecting multiple values</h3>
<p>
Multiple values can be selected using JavaScript object literals or arrays.
</p>
Example: show name and thread for each thread object
<pre>
<code>
select { name: t.name? t.name.toString() : "null", thread: t }
</code>
</pre>
<p>
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.
</p>
<ul>
</ul>
<a name="concat"></a>
<h4>concat function</h4>
<p>
Concatenates two arrays or enumerations (i.e., returns composite
enumeration).
</p>
<a name="contains"></a>
<h4>contains function</h4>
<p>
Returns whether the given array/enumeration contains an element
the given boolean expression specified in code. The code evaluated
can refer to the following built-in variables.
</p>
<ul>
<li>it -> currently visited element
<li>index -> index of the current element
</ul>
Example: select all Properties objects that are referred by
some static field some class.
<pre>
<code>
where contains(<a href="#referrers">referrers</a>(p), "<a href="#classof">classof</a>(it).name == 'java.lang.Class'")
</code>
</pre>
<a name="count"></a>
<h4>count function</h4>
<p>
count function returns the count of elements of the input array/enumeration
that satisfy the given boolean expression. The boolean expression code can
refer to the following built-in variables.
</p>
<ul>
<li>it -> currently visited element
<li>index -> index of the current element
</ul>
Example: print number of classes that have specific name pattern
<pre>
<code>
</code>
</pre>
<a name="filter"></a>
<h4>filter function</h4>
<p>
filter function returns an array/enumeration that contains elements
of the input array/enumeration that satisfy the given boolean
expression. The boolean expression code can refer to the following built-in
variables.
</p>
<ul>
<li>it -> currently visited element
<li>index -> index of the current element
</ul>
Examples:
<ul>
<li>show all classes that have java.io.* name pattern
<pre>
<code>
</code>
</pre>
<li> show all referrers of URL object where the referrer is not from
java.net package
<pre>
<code>
select filter(<a href="#referrers">referrers</a>(u), "! /java.net./(<a href="#classof">classof</a>(it).name)")
</code>
</pre>
</ul>
<a name="length"></a>
<h4>length function</h4>
<p>
length function returns number of elements of an array/enumeration.
</p>
<a name="map"></a>
<h4>map function</h4>
<p>
Transforms the given array/enumeration by evaluating given code
on each element. The code evaluated can refer to the following built-in
variables.
</p>
<ul>
<li>it -> currently visited element
<li>index -> index of the current element
</ul>
<p>
map function returns an array/enumeration of values created by repeatedly
calling code on each element of input array/enumeration.
</p>
<pre>
<code>
select map(<a href="#findClass">heap.findClass</a>("java.io.File").statics, "index + '=' + <a href="#toHtml">toHtml</a>(it)")
</code>
</pre>
<a name="max"></a>
<h4>max function</h4>
<p>
returns the maximum element of the given array/enumeration.
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:
</p>
<ul>
<li>lhs -> left side element for comparison
<li>rhs -> right side element for comparison
</ul>
Examples:
<ul>
<li>find the maximum length of any String instance
<pre>
<code>
</code>
</pre>
<li>find string instance that has the maximum length
<pre>
<code>
</code>
</pre>
</ul>
<a name="min"></a>
<h4>min function</h4>
<p>
returns the minimum element of the given array/enumeration. 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:
</p>
<ul>
<li>lhs -> left side element for comparison
<li>rhs -> right side element for comparison
</ul>
Examples:
<ul>
<li>find the minimum size of any Vector instance
<pre>
<code>
</code>
</pre>
<li>find Vector instance that has the maximum length
<pre>
<code>
</code>
</ul>
<a name="sort"></a>
<h4>sort function</h4>
<p>
sorts given array/enumeration. 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:
</p>
<ul>
<li>lhs -> left side element for comparison
<li>rhs -> right side element for comparison
</ul>
Examples:
<ul>
<li> print all char[] objects in the order of size.
<pre>
<code>
select sort(<a href="#objects">heap.objects</a>('[C'), '<a href="#sizeof">sizeof</a>(lhs) - sizeof(rhs)')
</code>
</pre>
<li> print all char[] objects in the order of size but print
size as well.
<pre>
<code>
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 }')
</code>
</pre>
</ul>
<a name="sum"></a>
<h4>sum function</h4>
<p>
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.
</p>
Example: return sum of sizes of the reachable objects from each Properties object
<pre>
<code>
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)')
</code>
</code>
</pre>
<a name="toArray"></a>
<h4>toArray function</h4>
<p>
This function returns an array that contains elements of the input
</p>
<a name="unique"></a>
<h4>unique function</h4>
<p>
This function returns an array/enumeration containing unique elements of the
given input array/enumeration
</p>
Example: select unique char[] instances referenced from Strings. Note that
more than one String instance can share the same char[] for the content.
<pre>
<code>
// number of unique char[] instances referenced from any String
// total number of Strings
</code>
</pre>
<h3>More complex examples</h3>
<h4>Print histogram of each class loader and number of classes loaded by it</h4>
<pre>
<code>
'toHtml(it) + "<br>"')
</code>
</pre>
<p>
The above query uses the fact that, <b>java.lang.ClassLoader</b> has a private
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.
</p>
<h4>Show parent-child chain for each class loader instance</h4>
<pre>
<code>
function (it) {
var res = '';
while (it != null) {
res += toHtml(it) + "->";
it = it.parent;
}
res += "null";
return res + "<br>";
})
</code>
</pre>
<p>
Note that we use <b>parent</b> field of <b>java.lang.ClassLoader</b> class
and walk until parent is null using the callback function to map call.
</p>
<h4>Printing value of all System properties</h4>
<pre>
<code>
select <a href="#map">map</a>(<a href="#filter">filter(<a href="#findClass">heap.findClass</a>('java.lang.System').props.table, 'it != null'),
function (it) {
var res = "";
while (it != null) {
res += it.key.value.toString() + '=' +
it = it.next;
}
return res;
});
</code>
</pre>
<p>
The above query uses the following facts:
<ul>
buckets array.
entry points the next entry (or null) in the same hashtable bucket.
</ul>
<p>
<b>Note that this query (and many other queries) may not be stable - because
notification! (implementation detail)</b>. But, using such queries on user
classes may be safe - given that user has the control over the classes.
</p>
</body>
</html>