-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Sometimes it would be really useful to have some debugging mechanism at the WASM/WAT level instead of only at the C/Rust level. GDB is a good debugging tool, but it can only work at either the C/Rust level (with sufficient DWARF information) or at the native machine-code/assembly level. Tracing variables at the intermediate WASM level is not easy.
Two of the most primitive debug helpers at the WASM level would be mechanisms to print out variables/strings on the WASM stack. One straightforward way to achieve this would be to add two debug imported functions into the compiled WASM binary (e.g. __lind_debug_num and __lind_debug_str), which can retrieve the value at the current WASM stack, print it, then return the value back to the WASM stack (to ensure the value is not consumed and does not influence subsequent code logic). Then we can place these debug functions at any location where we want to peek at WASM stack variables.
I previously had to manually implement this debug mechanism every time I needed it, which is quite repetitive. It would be nice to have this debug mechanism implemented directly in the main branch, with configurations to optionally enable it.
To implement this, there needs to be an __lind_debug_num, etc., defined as imported WASM functions in places like crt1.c (and we must ensure they are not optimized out during the C→WASM compilation stage). Then, in wasmtime, we define these imported WASM functions and link them to the WASM module.
The modification in glibc should be wrapped inside a #ifdef, and the modification in wasmtime should be wrapped inside a cfg to serve as optional debug support.