6

Is there a way to add additional information to a java stacktrace?

I am developing an interpreter for a script language and would like to see the corresponding lines of script code in the java stacktrace.

The output could look something like this:

java.lang.NullPointerException 
at package.IPF_Try.execute(IPF_Try.java:76) called in script.scr:155
at package.IPF_Block.execute(IPF_Block.java:304) 
at package.IPF_If.execute(IPF_If.java:105)  called in script.scr:130
at package.IPF_Block.execute(IPF_Block.java:304) 
at package.IPF_Main.execute(IPF_Main.java:147) 
...

or this:

java.lang.NullPointerException 
at package.IPF_Try.execute(IPF_Try.java:76)
  --- called in script.scr:155 ---
at package.IPF_Block.execute(IPF_Block.java:304) 
at package.IPF_If.execute(IPF_If.java:105)
 --- called in script.scr:130---
at package.IPF_Block.execute(IPF_Block.java:304) 
at package.IPF_Main.execute(IPF_Main.java:147) 
...

This would make debugging a lot easier, unfortunately google could not find anything to achieve this.

The only way I could think of was to dynamlically generate a lot of classes with methods, whose name contains the information I need and which simply call the next method in the stacktrace - but that seems like a waste of (permgen) memory and cpu cycles to me.

4
  • Interesting question. The java compiler puts in debug symbols with file names and line numbers into the bytecode. I suppose if you messed with the bytecode, you could tweak that to better fit your needs. Are JSP compilers doing something like that? Commented Aug 29, 2011 at 7:49
  • @Thilo - yes - JSPs are compiled to bytecode (sometimes using an intermediary Java form); you can generally find the compiled classes by poking round your server's "working" directories. Commented Aug 29, 2011 at 7:55
  • the problem is that I cannot simply mess with the bytecode of my methods, because they are called from various scripts, so I can't add one specific piece of information to it. I could however dynamically create some classes that contain the necessary information in their method names or similar, but I am concerned about the performance implications. Commented Aug 29, 2011 at 7:55
  • @McDowell: and does it say index.jsp:304, or __internal_index__jsp.java:5068 ? Commented Aug 29, 2011 at 7:56

1 Answer 1

1

If you translate your scripts to bytecode, you can provide debugging details using the SourceFile and LineNumber attributes.

I am not aware of a mechanism to inject information into the call-stack at runtime.

Sign up to request clarification or add additional context in comments.

3 Comments

that's too bad, thank you for the reference on the SourceFile and Linenumber-Attributes. Maybe I will have to generate classes after all, however since I will need a class per file and per line number, this would mean generating several thousand dummy classes...
@ChristophK, why do you need one class per line? Could you use one class per routine/section in your script instead?
that may work, however I experimented a bit with creating "dummy classes", and even a few thousand seem to use up quite a lot of Permanent Generation space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.