809 lines · 24.8 KB · HTML
RawDownload
<!--Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. This code is free software; you can redistribute it and/or modify itunder the terms of the GNU General Public License version 2 only, aspublished by the Free Software Foundation.  Oracle designates thisparticular file as subject to the "Classpath" exception as providedby Oracle in the LICENSE file that accompanied this code. This code is distributed in the hope that it will be useful, but WITHOUTANY WARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public Licenseversion 2 for more details (a copy is included in the LICENSE file thataccompanied this code). You should have received a copy of the GNU General Public License version2 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 USAor visit www.oracle.com if you need additional information or have anyquestions.--> <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>OQL is SQL-like query language to query Java heap. OQL allows to filter/select informationwanted 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 expressionlanguage.</p> <p>OQL query is of the form <pre><code>         <span class="key">select</span> &lt;JavaScript expression to select&gt;         [ <span class="key">from</span> [<span class="key">instanceof</span>] &lt;class name&gt; &lt;identifier&gt;         [ <span class="key">where</span> &lt;JavaScript boolean expression to filter&gt; ] ]</code></pre>where class name is fully qualified Java class name (example: java.net.URL) or array class name.[C is char array name, [Ljava.io.File; is name of java.io.File[] and so on. 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 beid 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>    select s from java.lang.String s where s.count >= 100</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>    select s.value.toString() from java.lang.String s     where /java/(s.value.toString())</code></pre><li>show path value of all File objects<pre><code</b>    select file.path.value.toString() from java.io.File file</code></pre><li>show names of all ClassLoader classes<pre><code>    select <a href="#classof">classof</a>(cl).name     from instanceof java.lang.ClassLoader cl</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 bylooking 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> <li><b>heap.forEachClass</b> -- calls a callback function for each Java Class<pre><code>    heap.forEachClass(callback);</code></pre><li><b>heap.forEachObject</b> -- calls a callback function for each Java object<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><li><b>heap.findClass</b> -- finds Java Class of given name<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>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, signatureproperties.<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 indirectsuperclass 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><li><b>heap.findObject</b> -- finds object from given object id<pre><code>    heap.findObject(stringIdOfObject);</code></pre><a name="classes"></a><li><b>heap.classes</b> -- returns an enumeration of all Java classes<a name="objects"></a><li><b>heap.objects</b> -- returns an enumeration of Java objects <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 filterthe result set of objects.<a name="finalizables"></a><li><b>heap.finalizables</b> -- returns an enumeration of Java objects that arepending to be finalized.<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 booleanflag. 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>    select heap.livepaths(s) from java.lang.String s</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.<li><b>heap.roots</b> -- returns an Enumeration of Roots of the heap. <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><li>access static field 'props' of class java.lang.System<pre><code>    select heap.findClass("java.lang.System").statics.props    select heap.findClass("java.lang.System").props</code></pre><li>get number of fields of java.lang.String class <pre><code>    select heap.findClass("java.lang.String").fields.length</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>    select <a href="#filter">filter</a>(heap.classes(), "/java.net./(it.name)")</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 followingproperties:<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 thefollowing properties:<ul><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, signatureproperties.<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 indirectsuperclass 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>    select classof(o).name from instanceof java.lang.ref.Reference o</code><li>show all subclasses of java.io.InputStream<pre><code>    select heap.findClass("java.io.InputStream").subclasses()</code><li>show all superclasses of java.io.BufferedInputStream<pre><code>    select heap.findClass("java.io.BufferedInputStream").superclasses()</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>    select identical(heap.findClass("Foo").statics.bar, heap.findClass("AnotherClass").statics.bar)</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<a href="#findObject">heap.findObject</a> and may also be used to compareobjects for identity.</p>Example:<pre><code>    select objectid(o) from java.lang.Object o</code></pre> <a name="reachables"></a><h4>reachables function</h4><p>Returns an array of Java objects that are transitively referred from thegiven Java object. Optionally accepts a second parameter that is commaseparated 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>    select reachables(p) from java.util.Properties p</code></pre><li>print all reachables from each java.net.URL but omit the objects reachablevia the fields specified.<pre><code>    select reachables(u, 'java.net.URL.handler') from java.net.URL u</code></pre></ul> <a name="referrers"></a><h4>referrers function</h4><p>Returns an enumeration of Java objects that hold reference to a given Javaobject.</p>Examples:<ul><li> print number of referrers for each java.lang.Object instance<pre><code>    select count(referrers(o)) from java.lang.Object o</code></pre><li>print referrers for each java.io.File object<pre><code>    select referrers(f) from java.io.File f</code></pre><li>print URL objects only if referred by 2 or more <pre><code>    select u from java.net.URL u where count(referrers(u)) > 2</code></pre></ul> <a name="referees"></a><h4>referees function</h4><p>Returns an array of Java objects to which the given Javaobject directly refers to.</p>Example: to print all static reference fields of java.io.File class<pre><code>    select referees(<a href="#findClass">heap.findClass</a>("java.io.File"))</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 returnsa 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 bytesExample: <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 calledautomatically for objects selected by select expression. But, it may be usefulto print more complex output. Example: print hyperlink in bold font weight<pre><code>    select "&lt;b&gt;" + toHtml(o) + "&lt;/b&gt;" from java.lang.Object o</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 }     from instanceof java.lang.Thread t</code></pre> <h3>array/iterator/enumeration manipulation functions</h3> <p>These functions accept an array/iterator/enumeration and an expression string [or a callback function] as input. These functions iterate the array/iterator/enumeration and apply the expression (or function) on each element. Note that JavaScript objects are associative arrays. So, these functions may also be used with arbitrary JavaScript objects.</p> <ul><li><a href="#concat">concat(array1/enumeration1, array2/enumeration2)</a><li><a href="#contains">contains(array/enumeration, expression)</a><li><a href="#count">count(array/enumeration, expression)</a><li><a href="#filter">filter(array/enumeration, expression)</a><li><a href="#length">length(array/enumeration)</a><li><a href="#map">map(array/enumeration, expression)</a><li><a href="#max">max(array/enumeration, [expression])</a><li><a href="#min">min(array/enumeration, [expression])</a><li><a href="#sort">sort(array/enumeration, [expression])</a><li><a href="#sum">sum(array/enumeration, [expression])</a><li><a href="#toArray">toArray(array/enumeration)</a><li><a href="#unique">unique(array/enumeration, [expression])</a></ul> <a name="concat"></a><h4>concat function</h4><p>Concatenates two arrays or enumerations (i.e., returns compositeenumeration).

Lines 1–500 of 809.