Skip to content

Commit

Permalink
Demangle function names in stack trace when using addr2line script (b…
Browse files Browse the repository at this point in the history
…ytecodealliance#3211)

Last bit missing from bytecodealliance#3206: demangling of function names (useful for wasm
generated from Rust for instance) using `llvm-cxxfilt`.

Before this PR:
```text
0: abort
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/panic_abort/src/lib.rs:85
1: _ZN3std3sys4wasi14abort_internal17h50698daab05bf73bE
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys/wasi/mod.rs:181
2: _ZN3std7process5abort17h6bc522b6749f17cfE
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/process.rs:2278
3: _ZN3std5alloc8rust_oom17h452ad5ba6cebff96E
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/alloc.rs:364
```

After:
```text
0: abort
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/panic_abort/src/lib.rs:85
1: std::sys::wasi::abort_internal::h50698daab05bf73b
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys/wasi/mod.rs:181
2: std::process::abort::h6bc522b6749f17cf
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/process.rs:2278
3: std::alloc::rust_oom::h452ad5ba6cebff96
        at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/alloc.rs:364
```
  • Loading branch information
eloparco authored Mar 11, 2024
1 parent 0e4c479 commit f550feb
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion test-tools/addr2line/addr2line.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ def parse_module_functions(wasm_objdump: Path, wasm_file: Path) -> dict[str, str
return function_index_to_name


def demangle(cxxfilt: Path, function_name: str) -> str:
cmd = f"{cxxfilt} -n {function_name}"
p = subprocess.run(
shlex.split(cmd),
check=True,
capture_output=True,
text=True,
universal_newlines=True,
)
return p.stdout.strip()


def main():
parser = argparse.ArgumentParser(description="addr2line for wasm")
parser.add_argument("--wasi-sdk", type=Path, help="path to wasi-sdk")
Expand All @@ -242,6 +254,9 @@ def main():
llvm_dwarf_dump = args.wasi_sdk.joinpath("bin/llvm-dwarfdump")
assert llvm_dwarf_dump.exists()

llvm_cxxfilt = args.wasi_sdk.joinpath("bin/llvm-cxxfilt")
assert llvm_cxxfilt.exists()

code_section_start = get_code_section_start(wasm_objdump, args.wasm_file)
if code_section_start == -1:
return -1
Expand Down Expand Up @@ -275,7 +290,7 @@ def main():
)

_, funciton_file, function_line = line_info
function_name = function_index_to_name[index]
function_name = demangle(llvm_cxxfilt, function_index_to_name[index])
print(f"{i}: {function_name}")
print(f"\tat {funciton_file}:{function_line}")
else:
Expand All @@ -286,6 +301,7 @@ def main():
)

function_name, funciton_file, function_line, function_column = line_info
function_name = demangle(llvm_cxxfilt, function_name)
print(f"{i}: {function_name}")
print(f"\tat {funciton_file}:{function_line}:{function_column}")

Expand Down

0 comments on commit f550feb

Please sign in to comment.