|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (C) 2024 Amazon Inc. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +# |
| 6 | + |
| 7 | +""" |
| 8 | +This tool corrects function names in call stacks based on the |
| 9 | +instruction pointers. |
| 10 | +
|
| 11 | +When the AOT file is generated with excluded func-idx in the |
| 12 | +`--call-stack-features` parameter, the function indexes are |
| 13 | +incorrect (likely they're zero). This script uses instruction |
| 14 | +pointers and the original WASM file to generate a call stack |
| 15 | +file with the correct function indexes (or function names, |
| 16 | +when available). |
| 17 | +
|
| 18 | +Example input (call_stack.txt) - note that `__imported_wasi_snapshot_preview1_fd_close` |
| 19 | +had index 0, therefore it appears as a name in every line: |
| 20 | +``` |
| 21 | +#00: 0x0505 - __imported_wasi_snapshot_preview1_fd_close |
| 22 | +#01: 0x0309 - __imported_wasi_snapshot_preview1_fd_close |
| 23 | +#02: 0x037c - __imported_wasi_snapshot_preview1_fd_close |
| 24 | +#03: 0x03b2 - __imported_wasi_snapshot_preview1_fd_close |
| 25 | +#04: 0x03e4 - __imported_wasi_snapshot_preview1_fd_close |
| 26 | +#05: 0x02e6 - __imported_wasi_snapshot_preview1_fd_close |
| 27 | +``` |
| 28 | +
|
| 29 | +Conversion command: |
| 30 | +``` |
| 31 | +python3 test-tools/ip2function/ip2function.py \ |
| 32 | + --wasm-file opt-samp/tiny.wasm \ |
| 33 | + call_stack.txt |
| 34 | +``` |
| 35 | +
|
| 36 | +Output: |
| 37 | +``` |
| 38 | +#0: 0x0505 - abort |
| 39 | +#1: 0x0309 - baz |
| 40 | +#2: 0x037c - bar |
| 41 | +#3: 0x03b2 - foo |
| 42 | +#4: 0x03e4 - __original_main |
| 43 | +#5: 0x02e6 - _start |
| 44 | +``` |
| 45 | +""" |
| 46 | + |
| 47 | +import argparse |
| 48 | +import bisect |
| 49 | +import os |
| 50 | +import re |
| 51 | +import subprocess |
| 52 | +import sys |
| 53 | + |
| 54 | +from typing import NamedTuple, Optional |
| 55 | +from typing import TextIO |
| 56 | +from pathlib import Path |
| 57 | +import shutil |
| 58 | + |
| 59 | + |
| 60 | +class FunctionInfo(NamedTuple): |
| 61 | + start_address: int |
| 62 | + idx: int |
| 63 | + name: Optional[str] |
| 64 | + |
| 65 | + def __str__(self) -> str: |
| 66 | + return self.name if self.name else f"$f{self.idx}" |
| 67 | + |
| 68 | + |
| 69 | +def load_functions(wasm_objdump: Path, wasm_file: Path) -> list[FunctionInfo]: |
| 70 | + objdump_function_pattern = re.compile( |
| 71 | + r"^([0-9a-f]+)\sfunc\[(\d+)\](?:\s\<(.+)\>)?\:$" |
| 72 | + ) |
| 73 | + |
| 74 | + def parse_objdump_function_line( |
| 75 | + line: str, |
| 76 | + ) -> Optional[FunctionInfo]: |
| 77 | + match = objdump_function_pattern.match(line.strip()) |
| 78 | + return ( |
| 79 | + FunctionInfo(int(match[1], 16), int(match[2]), match[3]) if match else None |
| 80 | + ) |
| 81 | + |
| 82 | + p = subprocess.run( |
| 83 | + [wasm_objdump, "--disassemble", wasm_file], |
| 84 | + check=True, |
| 85 | + capture_output=True, |
| 86 | + text=True, |
| 87 | + universal_newlines=True, |
| 88 | + ) |
| 89 | + |
| 90 | + return list( |
| 91 | + filter( |
| 92 | + None, |
| 93 | + ( |
| 94 | + parse_objdump_function_line(line.strip()) |
| 95 | + for line in p.stdout.split(os.linesep) |
| 96 | + ), |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def parse_call_stack_file( |
| 102 | + functions: list[FunctionInfo], call_stack_file: TextIO, output_file: TextIO |
| 103 | +) -> None: |
| 104 | + call_stack_line_pattern = re.compile(r"^(#\d+): (0x[0-9a-f]+) \- (\S+)$") |
| 105 | + for line in call_stack_file: |
| 106 | + match = call_stack_line_pattern.match(line.strip()) |
| 107 | + if not match: |
| 108 | + output_file.write(line) |
| 109 | + continue |
| 110 | + index = match[1] |
| 111 | + address = match[2] |
| 112 | + |
| 113 | + func_pos = bisect.bisect_right( |
| 114 | + functions, int(address, 16), key=lambda x: x.start_address |
| 115 | + ) |
| 116 | + if func_pos <= 0: |
| 117 | + raise ValueError(f"Cannot find function for address {address}") |
| 118 | + output_file.write(f"{index}: {address} - {functions[func_pos -1]}\n") |
| 119 | + |
| 120 | + |
| 121 | +def main() -> int: |
| 122 | + parser = argparse.ArgumentParser(description="addr2line for wasm") |
| 123 | + parser.add_argument( |
| 124 | + "--wasm-objdump", type=Path, default="wasm-objdump", help="path to wasm objdump" |
| 125 | + ) |
| 126 | + parser.add_argument( |
| 127 | + "--wasm-file", required=True, type=Path, help="path to wasm file" |
| 128 | + ) |
| 129 | + parser.add_argument( |
| 130 | + "call_stack_file", type=argparse.FileType("r"), help="path to a call stack file" |
| 131 | + ) |
| 132 | + parser.add_argument( |
| 133 | + "-o", |
| 134 | + "--output", |
| 135 | + type=argparse.FileType("w"), |
| 136 | + default=sys.stdout, |
| 137 | + help="Output file path (default is stdout)", |
| 138 | + ) |
| 139 | + |
| 140 | + args = parser.parse_args() |
| 141 | + |
| 142 | + wasm_objdump: Path = shutil.which(args.wasm_objdump) |
| 143 | + assert wasm_objdump is not None |
| 144 | + |
| 145 | + wasm_file: Path = args.wasm_file |
| 146 | + assert wasm_file.exists() |
| 147 | + |
| 148 | + parse_call_stack_file( |
| 149 | + load_functions(wasm_objdump, wasm_file), args.call_stack_file, args.output |
| 150 | + ) |
| 151 | + |
| 152 | + return 0 |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + sys.exit(main()) |
0 commit comments