wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Support DWARF in wasm-bindgen

Open mitsuhiko opened this issue 3 years ago • 5 comments

Motivation

Chrome, Firefox and now also Sentry support DWARF for WebAssembly as debug format. In particularly for crash reporting systems like Sentry DWARF is a very important tool to create accurate stack traces for production builds of web assembly applications.

This issue was previously brought up in #1981.

Proposed Solution

Most likely the bulk of the implementation will be in walrus (see https://github.com/rustwasm/walrus/issues/67).

mitsuhiko avatar Dec 03 '20 20:12 mitsuhiko

Just hoping for an update as this would be really handy for debugging WASM performance issues. Thx

Cypher1 avatar Mar 14 '22 05:03 Cypher1

After some experiments, I find it's possible to show Rust code in the Chrome DevTools with the following steps:

  1. add a --keep-debug in wasm-bindgen when building the wasm file, i.e. wasm-bindgen --keep-debug --out-dir ...
  2. install a C++ chrome extension and enable DWARF support feature in chrome dev tools: https://developer.chrome.com/blog/wasm-debugging-2020/?utm_source=devtools

Then the rust code can show up in the dev tools, but other functionalities such as setting breakpoints and evaluating rust code, are broken:

wecom-temp-bcb7d09421dfc8d9b0d40550932bdf05

It seems the DWARF info is kept when using wasm-bindgen with --keep-debug, but chrome doesn't fully support the dwarf debugging features for rust code.

michael8090 avatar Mar 18 '22 03:03 michael8090

It seems more like the debug info is "broken" after wasm-bindgen processes it, as in enough to make the source show up, but not enough for Chrome to handle it.

CryZe avatar Mar 18 '22 03:03 CryZe

It seems more like the debug info is "broken" after wasm-bindgen processes it, as in enough to make the source show up, but not enough for Chrome to handle it.

The extension is for C++ only, and you can see when I try to evaluate ElementsManager::get_static() in console, it takes the string as C++ code and says it's an invalid one. My guess is that maybe the dwarf info is correct, but it's the C++ extension that cannot understand rust code. I hope maybe someone inside the extension project or wasm-bindgen can give some concrete information about it.

michael8090 avatar Mar 18 '22 03:03 michael8090

Yeah, calling methods and stuff almost definitely won't work, but local variables and their fields, and breakpoints are all things that should work and last I tested DO work... if you don't use wasm-bindgen. So that still seems like a strong indication that the dwarf is broken.

CryZe avatar Mar 18 '22 05:03 CryZe

@CryZe did breakpoints work for you?? The devtools find my rust code but none of the standard library or cargo packages. They are all prefixed like /cargo/... or /rustc/...

stan-irl avatar Nov 10 '22 10:11 stan-irl

but none of the standard library or cargo packages. They are all prefixed like /cargo/... or /rustc/...

It's expected and happens because standard library in Rust is prebuilt on CI with those paths hardcoded. You can remap them to either github or your local rust-src paths via the extension settings.

But, either way, the breakpoints won't work at the moment because, as mentioned above, DWARF remapping is not implemented in wasm-bindgen.

RReverser avatar Nov 10 '22 11:11 RReverser

got it! thankyou for the quick response 🙏

stan-irl avatar Nov 10 '22 11:11 stan-irl

But, either way, the breakpoints won't work at the moment because, as mentioned above, DWARF remapping is not implemented in wasm-bindgen.

Should probably clarify that this is only if you use wasm-bindgen. If you use raw Wasm, it will work (but that's uncommon on the web).

RReverser avatar Nov 10 '22 12:11 RReverser

ah i got. because wasm-bindgen is actually changing the wasm when it generates the bindings but its not changing the DWARF so nothing matches up.

Regular wasm doesnt do that so its not a problem

stan-irl avatar Nov 10 '22 13:11 stan-irl

Note: with a few changes, I managed to successfully step through wasm-bindgen'd Rust code in Chrome using @nokotan's fork of walrus. See https://github.com/rustwasm/walrus/pull/231#issuecomment-1431438647.

However, that fix is very much non-production ready. As I mentioned in the comment linked above, it requires several forks of existing libraries, including wit-walrus which is not maintained anymore (since WIT no longer seem active). wasm-bindgen-cli-support would need a lot of work to drop the dependencies on WIT-related crates, so for now that dependency needs to stay. Once that's done, we'd need to make sure the PR I linked above would be merged. Since DWARF doesn't seem to work well with Chrome (at least on Windows), we'd also need make our own version of wasm-sourcemap.py (since a Python dependency on top of Rust and JS would be a bit much).

Note: I'd like to get more involved with wasm-bindgen (and its dependencies), so if any maintainer sees this and can point me to tasks that would help make this possible, please do!

71 avatar Feb 15 '23 14:02 71

Since DWARF doesn't seem to work well with Chrome (at least on Windows), we'd also need make our own version of wasm-sourcemap.py (since a Python dependency on top of Rust and JS would be a bit much).

Someone has now built this, and playing around with it locally it seems like it works well: https://github.com/mtolmacs/wasm2map

Cldfire avatar Apr 19 '23 16:04 Cldfire

@Cldfire FWIW that comment above was mistaken, DWARF does work well with Chrome directly: https://github.com/rustwasm/walrus/pull/231#issuecomment-1431619795

RReverser avatar Apr 19 '23 17:04 RReverser

Yea, I can also confirm that DWARF works well for me with Chrome, at least on macOS (M1).

d3lm avatar Jul 27 '23 08:07 d3lm

VS Code's JavaScript debugger is also now DWARF aware (nightly build today, and to stable in 2 weeks). Support here would make targeting wasm from Rust much more pleasant for VS/Code folks 🙂

connor4312 avatar Sep 20 '23 23:09 connor4312

Just for visibility: this is currently only blocked on https://github.com/rustwasm/walrus/issues/247.

daxpedda avatar Sep 21 '23 06:09 daxpedda

for visibility: https://github.com/rustwasm/walrus/issues/247 has been fixed.

eiswind avatar Nov 10 '23 13:11 eiswind

Awesome! Can't wait to give this a shot on Monday... so I'll probably try it this weekend 😄

connor4312 avatar Nov 11 '23 01:11 connor4312

Well..... @connor4312 ????? I'm on tenterhooks and I believe this is more important than Thanksgiving because if it works I can give thanks!

squillace avatar Nov 23 '23 09:11 squillace

How is it decided when new wasm-bindgen versions are released? On demand? Looking forward to giving the next release a try 😎

I'm on tenterhooks and I believe this is more important than Thanksgiving because if it works I can give thanks!

At cursory investigation, things work with a few caveats:

  • Publishing of packages still needs to happen. Also, I can only get symbols working if I invoke wasm-bindgen directly with --keep-debug, not via the more common wasm-pack, but maybe this'll be fixed when things are published?
  • Locations work in the Chrome C++ extension but they're off in VS Code's debugger. This is likely on my side to fix; we consume the guts of the Chrome plugin to do the DWARF heavy lifting so I'm probably just holding it wrong. I will look after the holiday.

connor4312 avatar Nov 23 '23 15:11 connor4312

How is it decided when new wasm-bindgen versions are released? On demand?

@haraldreingruber-dedalus I think this should be released. Or maybe I am mistaken.

d3lm avatar Nov 24 '23 15:11 d3lm

Fixed in VS Code, will be released in about 1.5 weeks from today

image

Will be looking forward to a new version of wasm-bindgen/wasm-pack so we can update our docs with Rust debugging instructions :)

connor4312 avatar Nov 26 '23 21:11 connor4312

@connor4312 It looks like the call stack name is not taken from DWARF but rather from the names section? Is that a currently known limitation?

RReverser avatar Nov 26 '23 22:11 RReverser

that's formed from asking the worker for getFunctionInfo, which is the same (from what I understand) as what the devtools extension does. I get the same result there, maybe the issue is upstream?

image

connor4312 avatar Nov 26 '23 22:11 connor4312

Ah weird, could be. It's been couple of years since I tried Rust-produced Wasm in DevTools. I guess they'd need Rust-specific demangling to omit it. Not a big deal either way, thanks for working on this!

RReverser avatar Nov 26 '23 22:11 RReverser

@connor4312 thanks so much for the work on this! I kind of expected this to be released to vscode-insiders but maybe not yet? rustwasm/walrus dependency upgrades are now in the 0.2.89 release (diff)

image

brentmjohnson avatar Nov 28 '23 23:11 brentmjohnson

It's in tomorrow's Insiders

connor4312 avatar Nov 29 '23 02:11 connor4312

Thanks for the wonderful work. I played for a while and found the following:

  1. breakpoint, stepping: works nicely, great
  2. local variable inspecting: it shows undefined for every local variable, no idea if it's something I did wrong: image
  3. function arguments inspecting: arguments are undefined except self. self can be fully expanded, and some owned values can be inspected properly, but boxed values are hidden behind pointers, which makes them hard to be inspected: image

michael8090 avatar Nov 29 '23 02:11 michael8090