Skip to content

Commit 0e4c479

Browse files
authored
Get location info from function indexes in addr2line script (bytecodealliance#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).
1 parent a43018f commit 0e4c479

File tree

9 files changed

+470
-57
lines changed

9 files changed

+470
-57
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

+10
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,16 @@ jobs:
448448
./build.sh
449449
./run.sh
450450
451+
- name: Build Sample [debug-tools]
452+
run: |
453+
cd samples/debug-tools
454+
mkdir build && cd build
455+
cmake ..
456+
cmake --build . --config Debug --parallel 4
457+
./iwasm wasm-apps/trap.wasm | grep "#" > call_stack.txt
458+
./iwasm wasm-apps/trap.aot | grep "#" > call_stack_aot.txt
459+
bash -x ../symbolicate.sh
460+
451461
test:
452462
needs:
453463
[

.github/workflows/compilation_on_macos.yml

+10
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,13 @@ jobs:
369369
cd samples/terminate
370370
./build.sh
371371
./run.sh
372+
373+
- name: Build Sample [debug-tools]
374+
run: |
375+
cd samples/debug-tools
376+
mkdir build && cd build
377+
cmake ..
378+
cmake --build . --config Debug --parallel 4
379+
./iwasm wasm-apps/trap.wasm | grep "#" > call_stack.txt
380+
./iwasm wasm-apps/trap.aot | grep "#" > call_stack_aot.txt
381+
bash -x ../symbolicate.sh

samples/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
# Samples
2+
33
- [**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.
44
- **[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.
55
- **[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 @@
1212
- **[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.
1313
- **[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.
1414
- **[workload](./workload/README.md)**: Demonstrating how to build and run some complex workloads, e.g. tensorflow-lite, XNNPACK, wasm-av1, meshoptimizer and bwa.
15+
- **[debug-tools](./debug-tools/README.md)**: Demonstrating how to symbolicate a stack trace.

samples/debug-tools/CMakeLists.txt

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
8+
project(debug_tools_sample)
9+
10+
################ runtime settings ################
11+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
12+
if (APPLE)
13+
add_definitions(-DBH_PLATFORM_DARWIN)
14+
endif ()
15+
16+
# Resetdefault linker flags
17+
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
18+
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
19+
20+
# WAMR features switch
21+
22+
# Set WAMR_BUILD_TARGET, currently values supported:
23+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
24+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
25+
if (NOT DEFINED WAMR_BUILD_TARGET)
26+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
27+
set (WAMR_BUILD_TARGET "AARCH64")
28+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
29+
set (WAMR_BUILD_TARGET "RISCV64")
30+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
31+
# Build as X86_64 by default in 64-bit platform
32+
set (WAMR_BUILD_TARGET "X86_64")
33+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
34+
# Build as X86_32 by default in 32-bit platform
35+
set (WAMR_BUILD_TARGET "X86_32")
36+
else ()
37+
message(SEND_ERROR "Unsupported build target platform!")
38+
endif ()
39+
endif ()
40+
41+
if (NOT CMAKE_BUILD_TYPE)
42+
set (CMAKE_BUILD_TYPE Release)
43+
endif ()
44+
45+
set(WAMR_BUILD_INTERP 1)
46+
set(WAMR_BUILD_LIBC_WASI 1)
47+
set(WAMR_BUILD_FAST_INTERP 0) # Otherwise addresses don't match llvm-dwarfdump (addr2line)
48+
set(WAMR_BUILD_AOT 1)
49+
set(WAMR_BUILD_DUMP_CALL_STACK 1) # Otherwise stack trace is not printed (addr2line)
50+
51+
# compiling and linking flags
52+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
53+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
54+
endif ()
55+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
56+
57+
# build out vmlib
58+
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
59+
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
60+
61+
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
62+
63+
################ wasm application ################
64+
add_subdirectory(wasm-apps)
65+
66+
################ wamr runtime ################
67+
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
68+
69+
set (RUNTIME_SOURCE_ALL
70+
${CMAKE_CURRENT_LIST_DIR}/../../product-mini/platforms/linux/main.c
71+
${UNCOMMON_SHARED_SOURCE}
72+
)
73+
add_executable (iwasm ${RUNTIME_SOURCE_ALL})
74+
check_pie_supported()
75+
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
76+
target_link_libraries(iwasm vmlib -lm -ldl)

samples/debug-tools/README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# "debug-tools" sample introduction
2+
3+
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.
4+
5+
## Build and run the sample
6+
7+
### Generate the stack trace
8+
9+
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`):
10+
11+
```bash
12+
$ mkdir build && cd build
13+
$ cmake ..
14+
$ make
15+
$ ./iwasm wasm-apps/trap.wasm
16+
```
17+
18+
The output should be something like
19+
20+
```text
21+
#00: 0x0159 - $f5
22+
#01: 0x01b2 - $f6
23+
#02: 0x0200 - $f7
24+
#03: 0x026b - $f8
25+
#04: 0x236b - $f15
26+
#05: 0x011f - _start
27+
28+
Exception: unreachable
29+
```
30+
31+
Copy the stack trace printed to stdout into a separate file (`call_stack.txt`):
32+
33+
```bash
34+
$ ./iwasm wasm-apps/trap.wasm | grep "#" > call_stack.txt
35+
```
36+
37+
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:
38+
39+
```bash
40+
$ ./iwasm wasm-apps/trap.aot | grep "#" > call_stack.txt
41+
```
42+
43+
### Symbolicate the stack trace
44+
45+
Run the [addr2line](../../test-tools/addr2line/addr2line.py) script to symbolicate the stack trace:
46+
47+
```bash
48+
$ python3 ../../../test-tools/addr2line/addr2line.py \
49+
--wasi-sdk /opt/wasi-sdk \
50+
--wabt /opt/wabt \
51+
--wasm-file wasm-apps/trap.wasm \
52+
call_stack.txt
53+
```
54+
55+
The output should be something like:
56+
57+
```text
58+
0: c
59+
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:5:1
60+
1: b
61+
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:11:12
62+
2: a
63+
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:17:12
64+
3: main
65+
at wasm-micro-runtime/samples/debug-tools/wasm-apps/trap.c:24:5
66+
4: <unknown>
67+
at unknown:?:?
68+
5: _start
69+
```
70+
71+
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.
72+
If WAMR <= `1.3.2` is used, the stack trace does not contain addresses.
73+
In those two cases, run the script with `--no-addr`: the line info returned refers to the start of the function
74+
75+
```bash
76+
$ python3 ../../../test-tools/addr2line/addr2line.py \
77+
--wasi-sdk /opt/wasi-sdk \
78+
--wabt /opt/wabt \
79+
--wasm-file wasm-apps/trap.wasm \
80+
call_stack.txt --no-addr
81+
```

samples/debug-tools/symbolicate.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -euox pipefail
3+
4+
# Symbolicate .wasm
5+
python3 ../../../test-tools/addr2line/addr2line.py \
6+
--wasi-sdk /opt/wasi-sdk \
7+
--wabt /opt/wabt \
8+
--wasm-file wasm-apps/trap.wasm \
9+
call_stack.txt
10+
11+
# Symbolicate .wasm with `--no-addr`
12+
python3 ../../../test-tools/addr2line/addr2line.py \
13+
--wasi-sdk /opt/wasi-sdk \
14+
--wabt /opt/wabt \
15+
--wasm-file wasm-apps/trap.wasm \
16+
call_stack.txt --no-addr
17+
18+
# Symbolicate .aot
19+
python3 ../../../test-tools/addr2line/addr2line.py \
20+
--wasi-sdk /opt/wasi-sdk \
21+
--wabt /opt/wabt \
22+
--wasm-file wasm-apps/trap.wasm \
23+
call_stack_aot.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
if (APPLE)
5+
set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
6+
set (CMAKE_C_LINK_FLAGS "")
7+
set (CMAKE_CXX_LINK_FLAGS "")
8+
endif ()
9+
10+
if (NOT DEFINED WASI_SDK_DIR)
11+
set (WASI_SDK_DIR "/opt/wasi-sdk")
12+
endif ()
13+
14+
if (DEFINED WASI_SYSROOT)
15+
set (CMAKE_SYSROOT "${WASI_SYSROOT}")
16+
endif ()
17+
18+
set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
19+
set (CMAKE_ASM_COMPILER "${WASI_SDK_DIR}/bin/clang")
20+
set (CMAKE_EXE_LINKER_FLAGS "-target wasm32-wasi")
21+
22+
################ wabt and wamrc dependencies ################
23+
message(CHECK_START "Detecting WABT")
24+
if(NOT (DEFINED WABT_DIR OR DEFINED CACHE{WABT_DIR}))
25+
find_path(WABT_DIR
26+
wabt
27+
PATHS /opt
28+
NO_DEFAULT_PATH
29+
NO_CMAKE_FIND_ROOT_PATH
30+
)
31+
if(DEFINED WABT_DIR)
32+
set(WABT_DIR ${WABT_DIR}/wabt)
33+
endif()
34+
endif()
35+
if(WABT_DIR)
36+
message(CHECK_PASS "found")
37+
else()
38+
message(CHECK_FAIL "not found")
39+
endif()
40+
41+
message(CHECK_START "Detecting WASM_OBJDUMP at ${WABT_DIR}")
42+
find_program(WASM_OBJDUMP
43+
wasm-objdump
44+
PATHS "${WABT_DIR}/bin"
45+
NO_DEFAULT_PATH
46+
NO_CMAKE_FIND_ROOT_PATH
47+
)
48+
if(WASM_OBJDUMP)
49+
message(CHECK_PASS "found")
50+
else()
51+
message(CHECK_FAIL "not found")
52+
endif()
53+
if((NOT EXISTS ${WASM_OBJDUMP}) )
54+
message(FATAL_ERROR "Please make sure to have wasm-objdump under the path=${WABT_DIR}/bin ")
55+
endif()
56+
57+
set(WAMR_COMPILER_DIR ${CMAKE_CURRENT_LIST_DIR}/../../wamr-compiler/build)
58+
message(CHECK_START "Detecting WAMR_COMPILER at ${WAMR_COMPILER_DIR}")
59+
find_file(WAMR_COMPILER
60+
wamrc
61+
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../../wamr-compiler/build"
62+
NO_DEFAULT_PATH
63+
NO_CMAKE_FIND_ROOT_PATH
64+
)
65+
if(WAMR_COMPILER)
66+
message(CHECK_PASS "found")
67+
else()
68+
message(CHECK_FAIL "not found")
69+
endif()
70+
if((NOT EXISTS ${WAMR_COMPILER}) )
71+
message(FATAL_ERROR "Please build wamrc under the path=${WAMR_ROOT_DIR}/wamr-compiler/")
72+
endif()
73+
74+
################ wasm and aot compilation ################
75+
function (compile_sample SOURCE_FILE)
76+
get_filename_component (FILE_NAME ${SOURCE_FILE} NAME_WLE)
77+
set (WASM_MODULE ${FILE_NAME}.wasm)
78+
add_executable (${WASM_MODULE} ${SOURCE_FILE})
79+
80+
add_custom_target(
81+
wasm_to_aot
82+
ALL
83+
DEPENDS ${WAMR_COMPILER} ${WASM_MODULE}
84+
# Use --enable-dump-call-stack to generate stack trace (addr2line)
85+
COMMAND ${WAMR_COMPILER} --size-level=0 --enable-dump-call-stack -o wasm-apps/trap.aot wasm-apps/trap.wasm
86+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
87+
)
88+
endfunction ()
89+
90+
set(CMAKE_BUILD_TYPE Debug) # Otherwise no debug symbols (addr2line)
91+
compile_sample(trap.c)

samples/debug-tools/wasm-apps/trap.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
int
2+
c(int n)
3+
{
4+
__builtin_trap();
5+
}
6+
7+
int
8+
b(int n)
9+
{
10+
n += 3;
11+
return c(n);
12+
}
13+
14+
int
15+
a(int n)
16+
{
17+
return b(n);
18+
}
19+
20+
int
21+
main(int argc, char **argv)
22+
{
23+
int i = 5;
24+
a(i);
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)