Skip to content

feat: add Scopes of functions and inlined calls to the source map - #344

Open
JesusFileto wants to merge 5 commits into
wasm-bindgen:mainfrom
JesusFileto:source-map-scopes
Open

JesusFileto wants to merge 5 commits into
wasm-bindgen:mainfrom
JesusFileto:source-map-scopes

Conversation

@JesusFileto

Copy link
Copy Markdown
Contributor

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 scopes and ranges, with their names: a Module scope per source around a Function scope per source function, a stack-frame range per function, and one per inlined call, nested in its caller's, with its call site. sources also 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 new rustc-demangle dependency, 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 with expect, like the conversion before it.

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.

@wasm-bindgen-bot wasm-bindgen-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>::f names are reduced to a misleading module::f, so inlined frames from trait impls on primitives and other legacy-mangled code get wrong names.

Comment thread src/module/debug/rust_names.rs Resolved
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant