Repository navigation
feat: add Scopes of functions and inlined calls to the source map - #344
JesusFileto wants to merge 5 commits into
Conversation
Tools that show Wasm stack traces read source maps, not DWARF. walrus already rewrites DWARF to the emitted code's offsets, so the map's mappings can come from those line tables without a second parse. ModuleConfig::generate_source_map(true) turns it on, and Module::emit_wasm_with_source_map(url) returns the module and its map, with a sourceMappingURL section that points at url in place of any the module had.
Optimized Rust inlines most calls, so with only mappings a stack frame loses the calls inlined into it. Scopes, the ECMA-426 proposal that Chrome DevTools reads, describe the source functions and the calls inlined at each offset, so a reader can expand a frame into them. They come from the debugging information entries walrus already rewrites: a Module scope per source around a Function scope per source function, and a range per function and per inlined call, with its call site. Naming functions, and telling which ones are copies of one source function, depends on the language, so the Rust rules (demangling with rustc-demangle, dropping generic arguments and shims, the shared `<_ as T>::f` name) live in rust_names.rs behind an internal trait. Scopes make the map about four to six times larger, so ModuleConfig::source_map_scopes(false) leaves them out. They are on by default.
There was a problem hiding this comment.
This PR adds ECMA-426 Scopes to the source map that #343 builds from the rewritten DWARF. Each source gets a Module scope holding a Function scope per source function, and each module function and each inlined call gets a generated range with its call site, so tools like Chrome DevTools can rebuild the inlined Rust frames in a stack trace. My guess is that wasm-bindgen will use this for symbolicated stack traces. Scopes are on by default, and ModuleConfig::source_map_scopes(false) turns them off. Function naming is language-specific, so the Rust rules sit behind an internal trait meant to become a hook later. I checked the encoding against the current proposal-scopes branch of the spec: per-source scopes with null entries, segmented ranges, per-string accumulator resets, the flag bits, and the E/I order and frame rules for inlined ranges. All of that matches. Part of that branch's text was cut off when I fetched it, so I couldn't confirm the RangeDefinition field encoding. One problem remains: with legacy mangling, <impl Trait for Type> path segments are stripped as if they were turbofish generics, which gives misleading scope names (inline comment).
Not merge-ready:
- Legacy-mangled
<impl Trait for Type>::fnames are reduced to a misleadingmodule::f, so inlined frames from trait impls on primitives and other legacy-mangled code get wrong names.
Directory 0 of a line program is the unit's compilation directory, and the path already starts from it, so a relative one (as left by `--remap-path-prefix`) came out twice: `myproj/myproj/src/lib.rs`. Skip directory 0, as addr2line does.
Legacy demangling writes trait impls as `core::fmt::num::imp::<impl core::fmt::Display for u32>::fmt`. The `<` after `::` was stripped like a turbofish, leaving `imp::fmt`, a free function that doesn't exist, which every impl from that macro merged under. Keep `<impl ...>` segments without the generics inside them, and name copies `<impl core::fmt::Display for _>::fmt`, as v0 names get `<_ as core::fmt::Display>::fmt`.
Declared scopes that start at the same place merge when their names
end with the same segment, so that copies whose self types differ are
one scope. For closures that segment is `{closure#0}`, so an
attribute macro's functions merged: the body of an `async fn fetch`
(`fetch::{closure#0}`), its exported wrapper's closure and its own
inner closure all became `_::{closure#0}`, the main frame of a Worker.
Compare the path from the last named segment instead, so
`fetch::{closure#0}` and `__wasm_bindgen_generated_fetch::{closure#0}`
stay apart.
feat: source maps that let stack traces show inlined Rust calls
Stacked on #343; only the top commit is new here.
Optimized Rust inlines most calls, so with only mappings the calls inlined into a Wasm frame are lost. Scopes (the ECMA-426 proposal that Chrome DevTools reads) describe the source functions and the calls inlined at each offset, so a reader can expand a frame into those calls. They come from the debugging information entries that walrus already rewrites at emit.
The flag.
ModuleConfig::source_map_scopes(false)gives the map without Scopes. They are on by default, so maps grow: on a Rust module with 13,400 functions (6.1 MB of Wasm), the map goes from 2.4 MB to 9.5 MB (0.70 to 1.70 MB gzipped), and emit from 0.95 s to 1.65 s.The map. Adds
scopesandranges, with theirnames: aModulescope per source around aFunctionscope per source function, a stack-frame range per function, and one per inlined call, nested in its caller's, with its call site.sourcesalso lists the files that only scopes and call sites name.Names. The generator reads DWARF only. Rust naming lives in
src/module/debug/rust_names.rs, behind an internal trait: linkage names demangled with the newrustc-demangledependency, generic arguments and shims dropped so copies of a function share its scope, and copies with different self types sharing<_ as T>::f. The plan is to expose the trait later as a hook, with these rules as the default, so other languages can supply names. Unreadable rewritten DWARF panics withexpect, like the conversion before it.