Skip to content
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

wasm2wat can't read debug names of params / vars #1290

Open
art-in opened this issue Jan 9, 2020 · 5 comments
Open

wasm2wat can't read debug names of params / vars #1290

art-in opened this issue Jan 9, 2020 · 5 comments

Comments

@art-in
Copy link

art-in commented Jan 9, 2020

i'm compiling rust project to wasm with wasm-pack, then converting resulting wasm to wat and trying to read it.

currently I can find functions by their original names, but function params and variables lack original names.

so for this rust function

pub fn add_two(my_param: i32) -> i32 {
    let my_arg = 2;
    my_param + my_arg
}

i have this wat

...
  (func $wasm_debug_symbols::add_two::h8849f07edd700d60 (type $t3) (param $p0 i32) (result i32)
...

there's only auto generated names for params/vars like $p0, $p1, $l1, $l2, etc.

but this is strange because I've configured rust project to include DWARF debug symbols to wasm

[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
dwarf-debug-info = true

i can even see those my_param and my_arg strings in target wasm file

$ hexdump -C wasm_debug_symbols_bg.wasm | grep -A 1 -B 1 my

001174a0  73 63 72 69 62 65 5f 61  64 64 5f 74 77 6f 00 6d  |scribe_add_two.m|
001174b0  79 5f 70 61 72 61 6d 00  6d 79 5f 61 72 67 00 61  |y_param.my_arg.a|
001174c0  72 67 30 00 5f 72 65 74  00 63 6c 61 6e 67 20 4c  |rg0._ret.clang L|

apparently original names for params/vars are included in wasm, but wasm2wat cannot find them.

also i've checked that wasm file in dev tools of chrome and firefox, and i don't see original params/vars there either (i assume those browsers have their own wasm to wat converters)

also i've tried to write simple function in wat, feed it to wat2wasm, and then feed resulting wasm to wasm2wat back again. and i see that custom names for params are preserved. it means wasm2wat can extract param names from wasm ok.

simple function in wat
(module
  (type $t0 (func (param i32 i32) (result i32)))
  (func $addTwo (export "add_two") (type $t0) (param $my_param i32) (param $p1 i32) (result i32)
    (i32.add
      (local.get $my_param)
      (local.get $my_param))))

so i assume that problem is

  • either in wasm-pack, which generates invalid debug symbols, so wasm2wat cannot extract them
  • or in wasm2wat , which cannot read valid debug symbols from wasm

any thoughts?

i've created minimum rust project for testing.

wasm_debug_symbols_bg.wasm (from wasm-pack)
wasm_debug_symbols_bg.wat (from wasm2wat)

@binji
Copy link
Member

binji commented Jan 9, 2020

The was format specifies a way to provide names for various elements (functions, variables, etc.) in a custom name section. This has been in the spec for a long time, and wabt tools (including wasm2wat) support it.

The dwarf section data is much newer, and wabt tools do not yet support it. I don't know if there's a way for LLVM to produce both sections (@tlively do you know?)

@tlively
Copy link
Member

tlively commented Jan 9, 2020

Redirecting to @kripken for comments on how debuginfo works with the name section.

@kripken
Copy link
Member

kripken commented Jan 9, 2020

I think the two sections are orthogonal. A wasm file can have both DWARF debug sections (that include name info) and a Names section, that's what happens in practice actually when you build with -g in clang (and emscripten).

@art-in
Copy link
Author

art-in commented Jan 10, 2020

so it's incompatibility issue, since we have two types of debug sections...

what's the global plan for wasm debug sections? is wasm spec Name section going to be stripped in favor of DWARF section, or somehow extended to have all metadata DWARF has?

what's more appropriate to do right now? ask wasm-pack to generate Name section, or wasm2wat to read DWARF section?

@kripken

i've tried to compile simple c++ function with emcc -g (1.39.4) and don't see var/param names neither from wasm2wat, nor in generated .wast file, nor in wasm hexdump. what i do wrong?

@yurydelendik
Copy link
Contributor

The name section allows assigning names to wasm locals. When you compile using clang or rustc, the variables often are not presented as wasm local. It can be a location in memory (via shadow stack) or placed on the operand stack, be a constant, just a wasm var/param. Or it can be all of the above but in different parts of the wasm function. IMHO the name section is useless in presenting original language variable names.

At this moment DWARF's .debug_info section is the best way to represent original language variable locations. See docs for DW_TAG_variable.

Also, keep in mind, if output wasm from clang or rustc, processed by wasm-pack, wasm-opt or any other tools, the DWARF section information/references are invalid.

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

No branches or pull requests

5 participants