< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Debug.java

Print this page



   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.parser.TokenType.EOF;


  29 import jdk.nashorn.internal.parser.Lexer;
  30 import jdk.nashorn.internal.parser.Token;
  31 import jdk.nashorn.internal.parser.TokenStream;
  32 import jdk.nashorn.internal.parser.TokenType;
  33 
  34 /**
  35  * Utilities for debugging Nashorn.
  36  *
  37  */
  38 public final class Debug {
  39     private Debug() {
  40     }
  41 
  42     /**
  43      * Return the topmost JavaScript frame in a stack trace
  44      * @param t throwable that contains the stack trace
  45      * @return line describing the topmost JavaScript frame
  46      */
  47     public static String firstJSFrame(final Throwable t) {
  48         for (final StackTraceElement ste : t.getStackTrace()) {
  49             if (ECMAErrors.isScriptFrame(ste)) {
  50                 return ste.toString();
  51             }
  52         }
  53         return "<native code>";
  54     }
  55 
  56     /**
  57      * Return the topmost JavaScript frame from the current
  58      * continuation
  59      * @return line describing the topmost JavaScript frame
  60      */
  61     public static String firstJSFrame() {
  62         return firstJSFrame(new Throwable());









  63     }
  64 
  65     /**
  66      * Return the system identity hashcode for an object as a human readable
  67      * string
  68      *
  69      * @param x object
  70      * @return system identity hashcode as string
  71      */
  72     public static String id(final Object x) {
  73         return String.format("0x%08x", System.identityHashCode(x));
  74     }
  75 
  76     /**
  77      * Same as {@link Debug#id} but returns the identity hashcode as
  78      * an integer
  79      *
  80      * @param x object
  81      * @return system identity hashcode
  82      */



   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.parser.TokenType.EOF;
  29 
  30 import jdk.nashorn.api.scripting.NashornException;
  31 import jdk.nashorn.internal.parser.Lexer;
  32 import jdk.nashorn.internal.parser.Token;
  33 import jdk.nashorn.internal.parser.TokenStream;
  34 import jdk.nashorn.internal.parser.TokenType;
  35 
  36 /**
  37  * Utilities for debugging Nashorn.
  38  *
  39  */
  40 public final class Debug {
  41     private Debug() {
  42     }
  43 
  44     /**
  45      * Return the topmost JavaScript frame in a stack trace
  46      * @param t throwable that contains the stack trace
  47      * @return line describing the topmost JavaScript frame
  48      */
  49     public static String firstJSFrame(final Throwable t) {
  50         for (final StackTraceElement ste : t.getStackTrace()) {
  51             if (ECMAErrors.isScriptFrame(ste)) {
  52                 return ste.toString();
  53             }
  54         }
  55         return "<native code>";
  56     }
  57 
  58     /**
  59      * Return the topmost JavaScript frame from the current
  60      * continuation
  61      * @return line describing the topmost JavaScript frame
  62      */
  63     public static String firstJSFrame() {
  64         return firstJSFrame(new Throwable());
  65     }
  66 
  67     /**
  68      * Return a formatted script stack trace string with frames information separated by '\n'.
  69      * This is a shortcut for {@code NashornException.getScriptStackString(new Throwable())}.
  70      * @return formatted stack trace string
  71      */
  72     public static String scriptStack() {
  73         return NashornException.getScriptStackString(new Throwable());
  74     }
  75 
  76     /**
  77      * Return the system identity hashcode for an object as a human readable
  78      * string
  79      *
  80      * @param x object
  81      * @return system identity hashcode as string
  82      */
  83     public static String id(final Object x) {
  84         return String.format("0x%08x", System.identityHashCode(x));
  85     }
  86 
  87     /**
  88      * Same as {@link Debug#id} but returns the identity hashcode as
  89      * an integer
  90      *
  91      * @param x object
  92      * @return system identity hashcode
  93      */


< prev index next >