-
Notifications
You must be signed in to change notification settings - Fork 811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for stack traces from wasm #827
Comments
Actually Wasmer already has backtrace support for
The produced backtrace contains WASM function indexes, locals and stack values for each stack frame. What's missing now is parsing function names from the WASM binary, and your implementation here looks good! Also, it would be better to implement the |
Oh wow, I've completely missed the relevant code in I've now had another look around the code base using the clues you provided, and it was actually still a bit tricky to figure out how I would get stack tracing to work using just the runtime. It seems that currently the support is there mostly for the Nevertheless, by mirroring some of the CLI's code, I've managed to convince the I am assuming the functionality is a work in progress, as for example the Rust crate documentation for the runtime doesn't mention tiering or stack tracing. It also looks like tiered execution is a part of a longer story around a multi-backend runtime, which probably comes with its own set of tradeoffs. It is a bit unclear whether user code should be following the patterns that With this in mind (and please correct me if my assumptions are wrong), what are your plans for making stack tracing / debugging more of a first-class citizen? |
@losfair is the goal to use the common trap and signal handling code from |
where should we be enabling |
We added support for stack traces in the refactor. Once it lands into |
@syrusakbary that's great, where is that documented? |
Thanks to the refactor, we now support stack traces when using the Cranelift backend. Closing the issue |
I would love to know that too. I cannot find the For llvm
(In general, is there a list of things that changed during the "big refactor"? It's so hard to figure out which information in this repo is still up to do date.) |
Motivation
Stack traces can be immensely helpful in tracking down bugs. In their absence, one resorts to "printf debugging", and binary-chopping the code. At Embark we use
wasmer-runtime
as our primary extension driver, meaning a lot of code goes through the WebAssembly path. As complexity and volume of said code increases, so does our need for better debugging tools.Are there any current plans in
wasmer
to support stack tracing? Is it something that you have in your roadmap, or is it a lower priority thing, for which you would rather accept pull requests?Proposed solution
In an effort to understand what would be involved to get stack tracing to work, I have implemented a proof-of-concept specifically for the Cranelift backend on x86_64 Linux. The platform-specific code is minimal, and it might even work on Mac OS already.
I opted to capture stack traces by following the linked list of stack frames via the
RBP
register. Perhaps a more robust approach would involve using stack tracing libraries, but this was the lightest solution without any extra dependencies.Stacks are captured in guest land (wasm), immediately before leaving it via
longjmp()
. The latter seems to modify the stack, as I could not get valid traces back in host land. I stash the captured instruction pointers into a thread-localCell
similarly to howCAUGHT_ADDRESSES
works in the Cranelift backend runtime.The stack trace is returned in quite an ad-hoc struct dubbed
WasmTrapWrapper
. The latter also contains what was previously returned as error info:WasmTrapInfo
:Ideally the stack addresses would be resolved into function names, and passed along in another (user-facing)
Error
type, but in the quick implementation, it is instead printed in a rather arbitrary location via theprint_stack_trace
function. The latter one needs aCtx
instance in order to match addresses to functions, and functions to names.Symbol names are extracted from the "name" custom section in WebAssembly binaries, and matched via function indices.
Alternatives
Clearly the implementation is not production-ready, and some work would be required to clean it up, as well as add support for the other platforms and backends.
Since my familiarity with
wasmer
is limited, I'm probably missing other potential mechanisms of accomplishing the same goal. Adding information about the source file and line for the stack trace would likely entail a separate investigation.Additional context
Below you can find output from a modified version of the
plugin
example, hard-wired to panic in wasm by accessing an out-of-bounds vector index.I hope this implementation will be useful should you choose to implement stack tracing! Otherwise perhaps we could discuss how this aligns with your plans and ideas, and if the code could be brought up to your standards (either by ourselves or some other contributors).
Thanks!
The text was updated successfully, but these errors were encountered: