binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

How to get a call stack when wasm-ctor-eval stops?

Open rdb opened this issue 10 months ago • 6 comments

Hello,

Apologies if this is already documented somewhere, but I was unable to find it. I'm trying to find out how I can easily find where a particular call to an import is being made when wasm-ctor-eval hits something it can't eval. I was hoping to find some debug option so that I could see something like a traceback.

At the moment I am manually searching through the wasm disassembly, which is quite a painstaking process given that this is a huge binary. Any suggestions for making this process easier would be very helpful.

rdb avatar Jan 30 '25 22:01 rdb

We don't have an option for this atm. You can approximate it by running in a debugger and stopping on any FailToEvalException, then inspecting the stack. We eval in an interpreter that will have a stack frame for each function called, and includes the function name in the arguments, so the information is there (at least in a debug build).

Supporting this "properly" would require a shadow stack of some sort. Adding a basic version, that notes functions as it enters and leaves them, would at least give a basic stack trace, and shouldn't be too hard to add. Adding precise binary offsets would be possible but harder.

kripken avatar Jan 30 '25 22:01 kripken

Even just the function names would be very helpful already.

Would it be easier to ask for a spammy debug log feature that just prints an indented name of the function as it enters it? Then I can just work upward from the last bit before it stopped.

rdb avatar Jan 30 '25 23:01 rdb

Oh, we do have a spammy option already I believe. Add

#define WASM_INTERPRETER_DEBUG 1

at the top of src/wasm-interpreter.h.

kripken avatar Jan 30 '25 23:01 kripken

The new interpreter will make it much easier to support this use case.

tlively avatar Jan 30 '25 23:01 tlively

Thanks! I'm looking forward to the new interpreter. I've managed to achieve what I need using wizer in the meantime, which gives a stack trace when it encounters an external call. On top of that, wizer lets me pass in external data (via a wasi call), which I need for my use case while there does not seem to be a way to do this with wasm-ctor-eval.

Are the plans for the new interpreter documented somewhere?

rdb avatar Jan 31 '25 19:01 rdb

The only documentation so far is this PR/commit description: https://github.com/WebAssembly/binaryen/pull/7227

tlively avatar Jan 31 '25 19:01 tlively