Skip to content

Commit 1b9fbb1

Browse files
authored
addr2line.py: Fix issue of applying offset in call stacks information (#3194)
The offset value is based on the start of the wasm file, it also equals to the value of `wasm-objdump -d <wasm_file>`.
1 parent 21819fc commit 1b9fbb1

File tree

1 file changed

+2
-48
lines changed

1 file changed

+2
-48
lines changed

test-tools/addr2line/addr2line.py

+2-48
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
- run the following command to transfer the address to line info:
3232
```
3333
$ cd test-tools/addr2line
34-
$ python3 addr2line.py --wasi-sdk <wasi-sdk installation> --wabt <wabt installation> --wasm-file <wasm file path> call_stack.txt
34+
$ python3 addr2line.py --wasi-sdk <wasi-sdk installation> --wasm-file <wasm file path> call_stack.txt
3535
```
36-
- the script will use *wasm-objdump* in wabt to transform address, then use *llvm-dwarfdump* to lookup the line info for each address
37-
in the call-stack dump.
36+
- the script will use *llvm-dwarfdump* to lookup the line info for each address in the call-stack dump.
3837
- the output will be:
3938
```
4039
#00: 0x0a04 - $f18
@@ -46,42 +45,6 @@
4645
"""
4746

4847

49-
def get_code_section_start(wasm_objdump: Path, wasm_file: Path) -> int:
50-
"""
51-
Find the start offset of Code section in a wasm file.
52-
53-
if the code section header likes:
54-
Code start=0x0000017c end=0x00004382 (size=0x00004206) count: 47
55-
56-
the start offset is 0x0000017c
57-
"""
58-
cmd = f"{wasm_objdump} -h {wasm_file}"
59-
p = subprocess.run(
60-
shlex.split(cmd),
61-
check=True,
62-
capture_output=True,
63-
text=True,
64-
universal_newlines=True,
65-
)
66-
outputs = p.stdout.split(os.linesep)
67-
68-
# if there is no .debug section, return -1
69-
for line in outputs:
70-
line = line.strip()
71-
if ".debug_info" in line:
72-
break
73-
else:
74-
print(f"No .debug_info section found {wasm_file}")
75-
return -1
76-
77-
for line in outputs:
78-
line = line.strip()
79-
if "Code" in line:
80-
return int(line.split()[1].split("=")[1], 16)
81-
82-
return -1
83-
84-
8548
def get_line_info(dwarf_dump: Path, wasm_file: Path, offset: int) -> str:
8649
"""
8750
Find the location info of a given offset in a wasm file.
@@ -142,21 +105,13 @@ def parse_call_stack_line(line: str) -> ():
142105
def main():
143106
parser = argparse.ArgumentParser(description="addr2line for wasm")
144107
parser.add_argument("--wasi-sdk", type=Path, help="path to wasi-sdk")
145-
parser.add_argument("--wabt", type=Path, help="path to wabt")
146108
parser.add_argument("--wasm-file", type=Path, help="path to wasm file")
147109
parser.add_argument("call_stack_file", type=Path, help="path to a call stack file")
148110
args = parser.parse_args()
149111

150-
wasm_objdump = args.wabt.joinpath("bin/wasm-objdump")
151-
assert wasm_objdump.exists()
152-
153112
llvm_dwarf_dump = args.wasi_sdk.joinpath("bin/llvm-dwarfdump")
154113
assert llvm_dwarf_dump.exists()
155114

156-
code_section_start = get_code_section_start(wasm_objdump, args.wasm_file)
157-
if code_section_start == -1:
158-
return -1
159-
160115
assert args.call_stack_file.exists()
161116
with open(args.call_stack_file, "rt", encoding="ascii") as f:
162117
for line in f:
@@ -173,7 +128,6 @@ def main():
173128
_, offset, _ = splitted
174129

175130
offset = int(offset, 16)
176-
offset = offset - code_section_start
177131
line_info = get_line_info(llvm_dwarf_dump, args.wasm_file, offset)
178132
if not line_info:
179133
print(line)

0 commit comments

Comments
 (0)