TrapBacktrace - Print a backtrace on TRAP and ABORT
When a trap happens, Project Oberon prints the module name and the code position where the trap originates. Often, these traps are inside system modules and caused by passing an invalid value to some exported function. But without a backtrace, it is hard to find where exactly the error happens.
This modification add a heuristic to the trap handler that scans the stack for likely return addresses (they have to lie inside the code of a module and the previous instruction needs to be a jump instruction), and prints a backtrace of those. Note that this heuristic can have false positives in exotic cases, resulting in false stack frames printed among the real ones (which are still all there). But usually these false stack frames can be identified easily when following the execution flow.
In conditional trap jumps, the code position is encoded in some free bits of the instruction itself, therefore no symbol files are required to print the position in the trap output. However, function call jumps lack those free bits, so a different way is needed to encode this information. This patch takes a simple approach and adds a "jump never" instruction before such function call jumps, which is used to encode the code position.
ABORT (pushing the reset button) has another problem: The stack register gets overwritten by the boot loader, making it impossible to find a starting point for the stack walk. This overwrite happens in the module header emitted by the compiler. Therefore patch the compiler to emit an additional instruction to save the previous stack pointer in Register 11, and patch the boot loader to use the value as stack pointer before handing control back to the system.
Note that a patched bootloader is not required to get backtraces on TRAPs, only to also get backtraces on ABORTs.
-
Apply
TrapBacktrace.patch
toEdit.Mod
,System.Mod
,ORG.Mod
andBootLoad.Mod
. -
Compile the patched modules:
ORP.Compile Edit.Mod System.Mod ~ ORP.Compile ORG.Mod ~ ORP.Compile BootLoad.Mod ~
-
Optionally update the boot loader on your FPGA.
-
Restart the system.
-
Recompile all modules that you want to have code positions in the backtrace (or all modules if you are unsure).