Get location info from function indexes in addr2line script (#3206)

Update the `addr2line` script so that:
- line info is printed in a more convenient format, e.g.
```
0: c
        at wasm-micro-runtime/test-tools/addr2line/trap.c:5:1
1: b
        at wasm-micro-runtime/test-tools/addr2line/trap.c:11:12
2: a
        at wasm-micro-runtime/test-tools/addr2line/trap.c:17:12
```
similar to how Rust prints stack traces when there's a panic. In an IDE, the user
can conveniently click on the printed path and be redirected to the file line.
- a new `--no-addr` argument can be provided to the script 

It can be used in fast interpreter mode (that is not supported by the script otherwise)
or with older wamr versions (where the stack trace only had the function index info
and not the function address). In that case, `wasm-objdump` is used to get the function
name from the index and `llvm-dwarfdump` to obtain the location info (where the line
refers to the start of the function).
This commit is contained in:
Enrico Loparco 2024-03-08 03:20:04 +01:00 committed by GitHub
parent a43018ff72
commit 0e4c4799b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 469 additions and 56 deletions

View File

@ -448,6 +448,16 @@ jobs:
./build.sh
./run.sh
- name: Build Sample [debug-tools]
run: |
cd samples/debug-tools
mkdir build && cd build
cmake ..
cmake --build . --config Debug --parallel 4
./iwasm wasm-apps/trap.wasm | grep "#" > call_stack.txt
./iwasm wasm-apps/trap.aot | grep "#" > call_stack_aot.txt
bash -x ../symbolicate.sh
test:
needs:
[

View File

@ -369,3 +369,13 @@ jobs:
cd samples/terminate
./build.sh
./run.sh
- name: Build Sample [debug-tools]
run: |
cd samples/debug-tools
mkdir build && cd build
cmake ..
cmake --build . --config Debug --parallel 4
./iwasm wasm-apps/trap.wasm | grep "#" > call_stack.txt
./iwasm wasm-apps/trap.aot | grep "#" > call_stack_aot.txt
bash -x ../symbolicate.sh

View File

@ -1,5 +1,5 @@
# Samples
- [**basic**](./basic): Demonstrating how to use runtime exposed API's to call WASM functions, how to register native functions and call them, and how to call WASM function from native function.
- **[file](./file/README.md)**: Demonstrating the supported file interaction API of WASI. This sample can also demonstrate the SGX IPFS (Intel Protected File System), enabling an enclave to seal and unseal data at rest.
- **[multi-thread](./multi-thread/)**: Demonstrating how to run wasm application which creates multiple threads to execute wasm functions concurrently, and uses mutex/cond by calling pthread related API's.
@ -12,3 +12,4 @@
- **[native-lib](./native-lib/README.md)**: Demonstrating how to write required interfaces in native library, build it into a shared library and register the shared library to iwasm.
- **[sgx-ra](./sgx-ra/README.md)**: Demonstrating how to execute Remote Attestation on SGX with [librats](https://github.com/inclavare-containers/librats), which enables mutual attestation with other runtimes or other entities that support librats to ensure that each is running within the TEE.
- **[workload](./workload/README.md)**: Demonstrating how to build and run some complex workloads, e.g. tensorflow-lite, XNNPACK, wasm-av1, meshoptimizer and bwa.
- **[debug-tools](./debug-tools/README.md)**: Demonstrating how to symbolicate a stack trace.

View File

@ -0,0 +1,76 @@
# Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 3.14)
include(CheckPIESupported)
project(debug_tools_sample)
################ runtime settings ################
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
if (APPLE)
add_definitions(-DBH_PLATFORM_DARWIN)
endif ()
# Resetdefault linker flags
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# WAMR features switch
# Set WAMR_BUILD_TARGET, currently values supported:
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
if (NOT DEFINED WAMR_BUILD_TARGET)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
set (WAMR_BUILD_TARGET "AARCH64")
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
set (WAMR_BUILD_TARGET "RISCV64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
# Build as X86_64 by default in 64-bit platform
set (WAMR_BUILD_TARGET "X86_64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
# Build as X86_32 by default in 32-bit platform
set (WAMR_BUILD_TARGET "X86_32")
else ()
message(SEND_ERROR "Unsupported build target platform!")
endif ()
endif ()
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Release)
endif ()
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_LIBC_WASI 1)
set(WAMR_BUILD_FAST_INTERP 0) # Otherwise addresses don't match llvm-dwarfdump (addr2line)
set(WAMR_BUILD_AOT 1)
set(WAMR_BUILD_DUMP_CALL_STACK 1) # Otherwise stack trace is not printed (addr2line)
# compiling and linking flags
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
endif ()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
# build out vmlib
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
################ wasm application ################
add_subdirectory(wasm-apps)
################ wamr runtime ################
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
set (RUNTIME_SOURCE_ALL
${CMAKE_CURRENT_LIST_DIR}/../../product-mini/platforms/linux/main.c
${UNCOMMON_SHARED_SOURCE}
)
add_executable (iwasm ${RUNTIME_SOURCE_ALL})
check_pie_supported()
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(iwasm vmlib -lm -ldl)

View File

@ -0,0 +1,81 @@
# "debug-tools" sample introduction
Tool to symoblicate stack traces. When using wasm in production, debug info are usually stripped using tools like `wasm-opt`, to decrease the binary size. If a corresponding unstripped wasm file is kept, location information (function, file, line, column) can be retrieved from the stripped stack trace.
## Build and run the sample
### Generate the stack trace
Build `iwasm` with `WAMR_BUILD_DUMP_CALL_STACK=1` and `WAMR_BUILD_FAST_INTERP=0` and the wasm file with debug info (e.g. `clang -g`). As it is done in [CMakeLists.txt](./CMakeLists.txt) and [wasm-apps/CMakeLists.txt](./wasm-apps/CMakeLists.txt) (look for `addr2line`):
```bash
$ mkdir build && cd build
$ cmake ..
$ make
$ ./iwasm wasm-apps/trap.wasm
```
The output should be something like
```text
#00: 0x0159 - $f5
#01: 0x01b2 - $f6
#02: 0x0200 - $f7
#03: 0x026b - $f8
#04: 0x236b - $f15
#05: 0x011f - _start
Exception: unreachable
```
Copy the stack trace printed to stdout into a separate file (`call_stack.txt`):
```bash
$ ./iwasm wasm-apps/trap.wasm | grep "#" > call_stack.txt
```
Same for AOT. The AOT binary has to be generated using the `--enable-dump-call-stack` option of `wamrc`, as in [CMakeLists.txt](./wasm-apps/CMakeLists.txt). Then run:
```bash
$ ./iwasm wasm-apps/trap.aot | grep "#" > call_stack.txt
```
### Symbolicate the stack trace
Run the [addr2line](../../test-tools/addr2line/addr2line.py) script to symbolicate the stack trace:
```bash
$ python3 ../../../test-tools/addr2line/addr2line.py \
--wasi-sdk /opt/wasi-sdk \
--wabt /opt/wabt \
--wasm-file wasm-apps/trap.wasm \
call_stack.txt
```
The output should be something like:
```text
0: c
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:5:1
1: b
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:11:12
2: a
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:17:12
3: main
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:24:5
4: <unknown>
at unknown:?:?
5: _start
```
If WAMR is run in fast interpreter mode (`WAMR_BUILD_FAST_INTERP=1`), addresses in the stack trace cannot be tracked back to location info.
If WAMR <= `1.3.2` is used, the stack trace does not contain addresses.
In those two cases, run the script with `--no-addr`: the line info returned refers to the start of the function
```bash
$ python3 ../../../test-tools/addr2line/addr2line.py \
--wasi-sdk /opt/wasi-sdk \
--wabt /opt/wabt \
--wasm-file wasm-apps/trap.wasm \
call_stack.txt --no-addr
```

View File

@ -0,0 +1,23 @@
#!/bin/bash
set -euox pipefail
# Symbolicate .wasm
python3 ../../../test-tools/addr2line/addr2line.py \
--wasi-sdk /opt/wasi-sdk \
--wabt /opt/wabt \
--wasm-file wasm-apps/trap.wasm \
call_stack.txt
# Symbolicate .wasm with `--no-addr`
python3 ../../../test-tools/addr2line/addr2line.py \
--wasi-sdk /opt/wasi-sdk \
--wabt /opt/wabt \
--wasm-file wasm-apps/trap.wasm \
call_stack.txt --no-addr
# Symbolicate .aot
python3 ../../../test-tools/addr2line/addr2line.py \
--wasi-sdk /opt/wasi-sdk \
--wabt /opt/wabt \
--wasm-file wasm-apps/trap.wasm \
call_stack_aot.txt

View File

@ -0,0 +1,91 @@
# Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
if (APPLE)
set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
set (CMAKE_C_LINK_FLAGS "")
set (CMAKE_CXX_LINK_FLAGS "")
endif ()
if (NOT DEFINED WASI_SDK_DIR)
set (WASI_SDK_DIR "/opt/wasi-sdk")
endif ()
if (DEFINED WASI_SYSROOT)
set (CMAKE_SYSROOT "${WASI_SYSROOT}")
endif ()
set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
set (CMAKE_ASM_COMPILER "${WASI_SDK_DIR}/bin/clang")
set (CMAKE_EXE_LINKER_FLAGS "-target wasm32-wasi")
################ wabt and wamrc dependencies ################
message(CHECK_START "Detecting WABT")
if(NOT (DEFINED WABT_DIR OR DEFINED CACHE{WABT_DIR}))
find_path(WABT_DIR
wabt
PATHS /opt
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
if(DEFINED WABT_DIR)
set(WABT_DIR ${WABT_DIR}/wabt)
endif()
endif()
if(WABT_DIR)
message(CHECK_PASS "found")
else()
message(CHECK_FAIL "not found")
endif()
message(CHECK_START "Detecting WASM_OBJDUMP at ${WABT_DIR}")
find_program(WASM_OBJDUMP
wasm-objdump
PATHS "${WABT_DIR}/bin"
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
if(WASM_OBJDUMP)
message(CHECK_PASS "found")
else()
message(CHECK_FAIL "not found")
endif()
if((NOT EXISTS ${WASM_OBJDUMP}) )
message(FATAL_ERROR "Please make sure to have wasm-objdump under the path=${WABT_DIR}/bin ")
endif()
set(WAMR_COMPILER_DIR ${CMAKE_CURRENT_LIST_DIR}/../../wamr-compiler/build)
message(CHECK_START "Detecting WAMR_COMPILER at ${WAMR_COMPILER_DIR}")
find_file(WAMR_COMPILER
wamrc
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../../wamr-compiler/build"
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
if(WAMR_COMPILER)
message(CHECK_PASS "found")
else()
message(CHECK_FAIL "not found")
endif()
if((NOT EXISTS ${WAMR_COMPILER}) )
message(FATAL_ERROR "Please build wamrc under the path=${WAMR_ROOT_DIR}/wamr-compiler/")
endif()
################ wasm and aot compilation ################
function (compile_sample SOURCE_FILE)
get_filename_component (FILE_NAME ${SOURCE_FILE} NAME_WLE)
set (WASM_MODULE ${FILE_NAME}.wasm)
add_executable (${WASM_MODULE} ${SOURCE_FILE})
add_custom_target(
wasm_to_aot
ALL
DEPENDS ${WAMR_COMPILER} ${WASM_MODULE}
# Use --enable-dump-call-stack to generate stack trace (addr2line)
COMMAND ${WAMR_COMPILER} --size-level=0 --enable-dump-call-stack -o wasm-apps/trap.aot wasm-apps/trap.wasm
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
endfunction ()
set(CMAKE_BUILD_TYPE Debug) # Otherwise no debug symbols (addr2line)
compile_sample(trap.c)

View File

@ -0,0 +1,27 @@
int
c(int n)
{
__builtin_trap();
}
int
b(int n)
{
n += 3;
return c(n);
}
int
a(int n)
{
return b(n);
}
int
main(int argc, char **argv)
{
int i = 5;
a(i);
return 0;
}

View File

@ -12,9 +12,7 @@ import subprocess
import sys
"""
it is a tool to transfer the address, which is from a call-stack dump generated by iwasm, to line info for a wasm file.
> in order to generate the call-stack dump, you can use the following command: `$ cmake -DWAMR_BUILD_DUMP_CALL_STACK=1 ...`
This is a tool to convert addresses, which are from a call-stack dump generated by iwasm, into line info for a wasm file.
When a wasm file is compiled with debug info, it is possible to transfer the address to line info.
@ -28,21 +26,20 @@ For example, there is a call-stack dump:
```
- store the call-stack dump into a file, e.g. call_stack.txt
- run the following command to transfer the address to line info:
- run the following command to convert the address into line info:
```
$ cd test-tools/addr2line
$ python3 addr2line.py --wasi-sdk <wasi-sdk installation> --wabt <wabt installation> --wasm-file <wasm file path> call_stack.txt
```
- the script will use *wasm-objdump* in wabt to transform address, then use *llvm-dwarfdump* to lookup the line info for each address
The script will use *wasm-objdump* in wabt to transform address, then use *llvm-dwarfdump* to lookup the line info for each address
in the call-stack dump.
- the output will be:
- if addresses are not available in the stack trace (i.e. iwasm <= 1.3.2) or iwasm is used in fast interpreter mode,
run the following command to convert the function index into line info (passing the `--no-addr` option):
```
#00: 0x0a04 - $f18
#01: 0x08e4 - $f11 (FILE:quicksort.c LINE: 176 COLUMN: 11 FUNC:Quick)
#02: 0x096f - $f12 (FILE:quicksort.c LINE: 182 COLUMN: 3 FUNC:main)
#03: 0x01aa - _start
$ python3 addr2line.py --wasi-sdk <wasi-sdk installation> --wabt <wabt installation> --wasm-file <wasm file path> call_stack.txt --no-addr
```
The script will use *wasm-objdump* in wabt to get the function names corresponding to function indexes, then use *llvm-dwarfdump* to lookup the line info for each
function index in the call-stack dump.
"""
@ -82,7 +79,9 @@ def get_code_section_start(wasm_objdump: Path, wasm_file: Path) -> int:
return -1
def get_line_info(dwarf_dump: Path, wasm_file: Path, offset: int) -> str:
def get_line_info_from_function_addr(
dwarf_dump: Path, wasm_file: Path, offset: int
) -> tuple[str, str, str, str]:
"""
Find the location info of a given offset in a wasm file.
"""
@ -96,29 +95,72 @@ def get_line_info(dwarf_dump: Path, wasm_file: Path, offset: int) -> str:
)
outputs = p.stdout.split(os.linesep)
capture_name = False
function_name, function_file = "<unknown>", "unknown"
function_line, function_column = "?", "?"
for line in outputs:
line = line.strip()
if "DW_TAG_subprogram" in line:
capture_name = True
continue
if "DW_AT_name" in line:
function_name = get_dwarf_tag_value("DW_AT_name", line)
if "DW_AT_name" in line and capture_name:
PATTERN = r"DW_AT_name\s+\(\"(\S+)\"\)"
m = re.match(PATTERN, line)
assert m is not None
if "DW_AT_decl_file" in line:
function_file = get_dwarf_tag_value("DW_AT_decl_file", line)
function_name = m.groups()[0]
if "Line info" in line:
_, function_line, function_column = parse_line_info(line)
if line.startswith("Line info"):
location = line
return (function_name, location)
return ()
return (function_name, function_file, function_line, function_column)
def parse_line_info(line_info: str) -> ():
def get_dwarf_tag_value(tag: str, line: str) -> str:
# Try extracting value as string
STR_PATTERN = rf"{tag}\s+\(\"(.*)\"\)"
m = re.match(STR_PATTERN, line)
if m:
return m.groups()[0]
# Try extracting value as integer
INT_PATTERN = rf"{tag}\s+\((\d+)\)"
m = re.match(INT_PATTERN, line)
return m.groups()[0]
def get_line_info_from_function_name(
dwarf_dump: Path, wasm_file: Path, function_name: str
) -> tuple[str, str, str]:
"""
Find the location info of a given function in a wasm file.
"""
cmd = f"{dwarf_dump} --name={function_name} {wasm_file}"
p = subprocess.run(
shlex.split(cmd),
check=False,
capture_output=True,
text=True,
universal_newlines=True,
)
outputs = p.stdout.split(os.linesep)
function_name, function_file = "<unknown>", "unknown"
function_line = "?"
for line in outputs:
line = line.strip()
if "DW_AT_name" in line:
function_name = get_dwarf_tag_value("DW_AT_name", line)
if "DW_AT_decl_file" in line:
function_file = get_dwarf_tag_value("DW_AT_decl_file", line)
if "DW_AT_decl_line" in line:
function_line = get_dwarf_tag_value("DW_AT_decl_line", line)
return (function_name, function_file, function_line)
def parse_line_info(line_info: str) -> tuple[str, str, str]:
"""
line_info -> [file, line, column]
"""
@ -130,13 +172,55 @@ def parse_line_info(line_info: str) -> ():
return (file, int(line), int(column))
def parse_call_stack_line(line: str) -> ():
def parse_call_stack_line(line: str) -> tuple[str, str, str]:
"""
New format (WAMR > 1.3.2):
#00: 0x0a04 - $f18 => (00, 0x0a04, $f18)
Old format:
#00 $f18 => (00, _, $f18)
"""
# New format
PATTERN = r"#([0-9]+): 0x([0-9a-f]+) - (\S+)"
m = re.match(PATTERN, line)
return m.groups() if m else None
if m is not None:
return m.groups()
# Old format
PATTERN = r"#([0-9]+) (\S+)"
m = re.match(PATTERN, line)
if m is not None:
return (m.groups()[0], None, m.groups()[1])
return None
def parse_module_functions(wasm_objdump: Path, wasm_file: Path) -> dict[str, str]:
function_index_to_name = {}
cmd = f"{wasm_objdump} -x {wasm_file} --section=function"
p = subprocess.run(
shlex.split(cmd),
check=True,
capture_output=True,
text=True,
universal_newlines=True,
)
outputs = p.stdout.split(os.linesep)
for line in outputs:
if not f"func[" in line:
continue
PATTERN = r".*func\[([0-9]+)\].*<(.*)>"
m = re.match(PATTERN, line)
assert m is not None
index = m.groups()[0]
name = m.groups()[1]
function_index_to_name[index] = name
return function_index_to_name
def main():
@ -145,6 +229,11 @@ def main():
parser.add_argument("--wabt", type=Path, help="path to wabt")
parser.add_argument("--wasm-file", type=Path, help="path to wasm file")
parser.add_argument("call_stack_file", type=Path, help="path to a call stack file")
parser.add_argument(
"--no-addr",
action="store_true",
help="use call stack without addresses or from fast interpreter mode",
)
args = parser.parse_args()
wasm_objdump = args.wabt.joinpath("bin/wasm-objdump")
@ -157,46 +246,51 @@ def main():
if code_section_start == -1:
return -1
if args.no_addr:
function_index_to_name = parse_module_functions(wasm_objdump, args.wasm_file)
assert args.call_stack_file.exists()
with open(args.call_stack_file, "rt", encoding="ascii") as f:
for line in f:
for i, line in enumerate(f):
line = line.strip()
if not line:
continue
splitted = parse_call_stack_line(line)
if splitted is None:
print(line)
assert splitted is not None
_, offset, index = splitted
if not index.startswith("$f"): # E.g. _start
print(f"{i}: {index}")
continue
index = index[2:]
_, offset, _ = splitted
if args.no_addr:
if index not in function_index_to_name:
print(f"{i}: {line}")
continue
offset = int(offset, 16)
offset = offset - code_section_start
line_info = get_line_info(llvm_dwarf_dump, args.wasm_file, offset)
if not line_info:
print(line)
continue
line_info = get_line_info_from_function_name(
llvm_dwarf_dump, args.wasm_file, function_index_to_name[index]
)
function_name, line_info = line_info
src_file, src_line, src_column = parse_line_info(line_info)
print(
f"{line} (FILE:{src_file} LINE:{src_line:5} COLUMN:{src_column:3} FUNC:{function_name})"
)
_, funciton_file, function_line = line_info
function_name = function_index_to_name[index]
print(f"{i}: {function_name}")
print(f"\tat {funciton_file}:{function_line}")
else:
offset = int(offset, 16)
offset = offset - code_section_start
line_info = get_line_info_from_function_addr(
llvm_dwarf_dump, args.wasm_file, offset
)
function_name, funciton_file, function_line, function_column = line_info
print(f"{i}: {function_name}")
print(f"\tat {funciton_file}:{function_line}:{function_column}")
return 0
if __name__ == "__main__":
print(
"**************************************************\n"
+ "Before running this script, please make sure:\n"
+ " - the wasm file is compiled with debug info. (like: clang -g) \n"
+ " - the call-stack dump is generated by iwasm\n"
+ " - iwasm is compiled with -DWAMR_BUILD_DUMP_CALL_STACK=1\n"
+ " - iwasm isn't running under fast-interp mode. -DWAMR_BUILD_FAST_INTERP=0\n"
+ " - if using .aot, the aot file is generated with `--enable-dump-call-stack`\n"
+ "**************************************************\n"
)
sys.exit(main())