clasp icon indicating copy to clipboard operation
clasp copied to clipboard

VM Backtraces

Open Bike opened this issue 2 years ago • 2 comments

We would like to support good backtraces for bytecode functions. This means that the existing backtrace machinery for native code needs to be extended so it can also handle bytecode functions.

The idea we've come with to support this is to make VM frames work a little more like native frames, by pushing the IP to the VM stack just before a call. The caller does not actually need this address to return, like you would natively, but its presence on the stack can be used to get the chain of function calls and make a backtrace.

In order to present any useful information we would also need DWARF-like line tables (#1449).

Bike avatar Apr 01 '23 21:04 Bike

To reconstruct a backtrace, we would follow these steps:

  1. Get a system backtrace containing return PC addresses using the C backtrace(void **buffer, int size) function.
  2. We get a VM backtrace containing return IP values using our own backtrace_vm function. To achieve this, we will have to push/pop the IP within bytecode_vm every time we call from the VM. We must also maintain a linked list of VM frame pointers on the VM stack.
  3. We search for return PC addresses that fall within the bytecode_vm function executable code.
  4. For each bytecode_vm return PC address, we look in the VM stack for the corresponding IP value. There needs to be a one-to-one correspondence between bytecode_vm PC addresses and IP addresses. This will be violated if an error occurs when we are within the bytecode_vm function. If we could get retain perfect debug information of the IP register within the bytecode_vm function we could recover the IP wherever we are within the bytecode_vm function.

drmeister avatar Apr 02 '23 12:04 drmeister

With an IP backtrace, we can use DWARF-like line tables that our bytecode compiler will generate to convert IP addresses to source information. When our bytecode compiler generates these line tables along with the bytecode modules, it will register the bytecode line tables with clasp's runtime just like we register object files generated by llvm.

drmeister avatar Apr 02 '23 12:04 drmeister