Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom JVM Language: Creating workable stack traces?

Tags:

jvm

How do stack traces on the JVM work?

Is it possible to transform classfiles to have stack traces that relate back to the parent language, rather then pseudo java files?

To be specific, could this Mixin Library https://github.com/SpongePowered/Mixin be modified, so that when it overwrites / injects code into methods, that if an error occurred, it would point to the correct mixin pseudo class in the source?

like image 449
Ryan Leach Avatar asked Apr 01 '26 15:04

Ryan Leach


1 Answers

There is no need to have Java source code files at all.

There are only two relevant attributes.

  1. The SourceFile class attribute which specifies the name of the source code file, which doesn’t have to be a .java file.

  2. The LineNumberTable attribute applied to Code attributes, telling how the byte code instructions map to source code lines.

The stack trace just reports the class and method names together with the source file name and line number as reported by the two attributes named above. There is no additional semantic behind it.

These attribute are already sufficient to do step debugging, as the debugger only needs to load the specified file (assuming it to be text based) and highlight the particular line. I already stepped through an XSLT file this way, which had been dynamically compiled to byte code by the XSLT processor. If you want to make local variables inspectable, you also have to add a LocalVariableTable attribute to the code.

I also used them in the way, you intend, to have generated code whose meta information point to the original code which triggered the code generation. It even happens when ordinary Java source code gets compiled, as the synthetic methods generated for lambda expressions have line number tables pointing to the lambda expression within the source code level method defining it.

like image 194
Holger Avatar answered Apr 08 '26 17:04

Holger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!