Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add user-specified information to java stack traces

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.

like image 811
ChristophK Avatar asked Aug 29 '11 07:08

ChristophK


1 Answers

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.

like image 100
McDowell Avatar answered Oct 05 '22 22:10

McDowell