Skip to content

Commit

Permalink
addr2line.py: Support sourceMappingURL section produced by emcc (#3302)
Browse files Browse the repository at this point in the history
And update the debug-tools sample.
  • Loading branch information
lum1n0us authored Apr 12, 2024
1 parent 1a043b6 commit fef26ea
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 107 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/compilation_on_android_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ jobs:
cd /opt
sudo wget ${{ matrix.wasi_sdk_release }}
sudo tar -xzf wasi-sdk-*.tar.gz
sudo mv wasi-sdk-20.0 wasi-sdk
sudo ln -sf wasi-sdk-20.0 wasi-sdk
- name: download and install wabt
run: |
cd /opt
sudo wget ${{ matrix.wabt_release }}
sudo tar -xzf wabt-1.0.31-*.tar.gz
sudo mv wabt-1.0.31 wabt
sudo ln -sf wabt-1.0.31 wabt
- name: Get LLVM libraries
id: retrieve_llvm_libs
uses: actions/cache@v4
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/compilation_on_macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ jobs:
cd /opt
sudo wget ${{ matrix.wasi_sdk_release }}
sudo tar -xzf wasi-sdk-*.tar.gz
sudo mv wasi-sdk-20.0 wasi-sdk
sudo ln -sf wasi-sdk-20.0 wasi-sdk
- name: download and install wabt
run: |
cd /opt
sudo wget ${{ matrix.wabt_release }}
sudo tar -xzf wabt-1.0.31-*.tar.gz
sudo mv wabt-1.0.31 wabt
sudo ln -sf wabt-1.0.31 wabt
- name: Build Sample [basic]
run: |
Expand Down Expand Up @@ -346,7 +346,7 @@ jobs:
cmake ..
cmake --build . --config Release --parallel 4
working-directory: wamr-compiler

- name: Build Sample [wasi-threads]
run: |
cd samples/wasi-threads
Expand Down Expand Up @@ -378,4 +378,4 @@ jobs:
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
bash -x ../symbolicate.sh
12 changes: 6 additions & 6 deletions .github/workflows/nightly_run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
types:
- opened
- synchronize
# running nightly pipeline if you're changing it
# running nightly pipeline if you're changing it
# stress tests are run only in nightly at the moment, so running them in they are changed
paths:
- ".github/workflows/nightly_run.yml"
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
with:
os: "ubuntu-22.04"
arch: "X86"

build_wamrc:
needs:
[
Expand All @@ -65,7 +65,7 @@ jobs:
matrix:
include:
- os: ubuntu-20.04
llvm_cache_key: ${{ needs.build_llvm_libraries_on_ubuntu_2004.outputs.cache_key }}
llvm_cache_key: ${{ needs.build_llvm_libraries_on_ubuntu_2004.outputs.cache_key }}
steps:
- name: checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -459,13 +459,13 @@ jobs:
cd /opt
sudo wget ${{ matrix.wasi_sdk_release }}
sudo tar -xzf wasi-sdk-*.tar.gz
sudo mv wasi-sdk-20.0 wasi-sdk
sudo ln -sf wasi-sdk-20.0 wasi-sdk
- name: download and install wabt
run: |
cd /opt
sudo wget ${{ matrix.wabt_release }}
sudo tar -xzf wabt-1.0.31-*.tar.gz
sudo mv wabt-1.0.31 wabt
sudo ln -sf wabt-1.0.31 wabt
- name: Get LLVM libraries
id: retrieve_llvm_libs
Expand Down Expand Up @@ -643,7 +643,7 @@ jobs:
sudo tar -xzf wasi-sdk-*.tar.gz
sudo mv wasi-sdk-20.0 wasi-sdk
# It is a temporary solution until new wasi-sdk that includes bug fixes is released
# It is a temporary solution until new wasi-sdk that includes bug fixes is released
- name: build wasi-libc from source
if: matrix.test_option == '$WASI_TEST_OPTIONS'
run: |
Expand Down
33 changes: 32 additions & 1 deletion samples/debug-tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ include(CheckPIESupported)

project(debug_tools_sample)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(WASISDK REQUIRED)

option(SOURCE_MAP_DEMO "Enable source map demo" OFF)
if (SOURCE_MAP_DEMO)
find_package(EMSCRIPTEN 3.1.50 REQUIRED)
endif ()

################ runtime settings ################
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
if (APPLE)
Expand Down Expand Up @@ -61,7 +69,30 @@ include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})

################ wasm application ################
add_subdirectory(wasm-apps)
include(ExternalProject)

# wasm32-wasi
ExternalProject_Add(wasm33-wasi
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps"
CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build
-DWASI_SDK_PREFIX=${WASISDK_HOME}
-DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN}
BUILD_COMMAND ${CMAKE_COMMAND} --build build
INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}
)

if (EMSCRIPTEN_FOUND)
# wasm32-emscripten
ExternalProject_Add(wasm32-emscripten
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps"
CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build
-DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN_TOOLCHAIN}
-DCMAKE_VERBOSE_MAKEFILE=On
-DSOURCE_MAP_DEMO=On
BUILD_COMMAND ${CMAKE_COMMAND} --build build
INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}/emscripten
)
endif ()

################ wamr runtime ################
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
Expand Down
35 changes: 34 additions & 1 deletion samples/debug-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,39 @@ $ python3 ../../../test-tools/addr2line/addr2line.py \
call_stack.txt --no-addr
```

#### sourcemap

This script also supports _sourcemap_ which is produced by [_emscripten_](https://emscripten.org/docs/tools_reference/emcc.html). The _sourcemap_ is used to map the wasm function to the original source file. To use it, add `-gsource-map` option to _emcc_ command line. The output should be a section named "sourceMappingURL" and a separated file named "_.map_.

If the wasm file is with _sourcemap_, the script will use it to get the source file and line info. It needs an extra command line option `--emsdk` to specify the path of _emsdk_. The script will use _emsymbolizer_ to query the source file and line info.

````bash
$ python3 ../../../test-tools/addr2line/addr2line.py \
--wasi-sdk /opt/wasi-sdk \
--wabt /opt/wabt \
--wasm-file emscripten/wasm-apps/trap.wasm \
--emsdk /opt/emsdk \
call_stack.from_wasm_w_sourcemap.txt

The output should be something like:

```text
1: c
at ../../../../../wasm-apps/trap.c:5:1
2: b
at ../../../../../wasm-apps/trap.c:11:12
3: a
at ../../../../../wasm-apps/trap.c:17:12
4: main
at ../../../../../wasm-apps/trap.c:24:5
5: __main_void
at ../../../../../../../../../emsdk/emscripten/system/lib/standalone/__main_void.c:53:10
6: _start
at ../../../../../../../../../emsdk/emscripten/system/lib/libc/crt1.c:27:3
````
> The script assume the separated map file _.map_ is in the same directory as the wasm file.
### Another approach
If the wasm file is with "name" section, it is able to output function name in the stack trace. To achieve that, need to enable `WAMR_BUILD_LOAD_CUSTOM_SECTION` and `WAMR_BUILD_CUSTOM_NAME_SECTION`. If using .aot file, need to add `--emit-custom-sections=name` into wamrc command line options.
Expand All @@ -97,4 +130,4 @@ Then the output should be something like
Exception: unreachable
```

Also, it is able to use *addr2line.py* to add file and line info to the stack trace.
Also, it is able to use _addr2line.py_ to add file and line info to the stack trace.
45 changes: 45 additions & 0 deletions samples/debug-tools/cmake/FindEMSCRIPTEN.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(FindPackageHandleStandardArgs)

find_path(EMSCRIPTEN_HOME
NAMES upstream/emscripten
PATHS /opt/emsdk
NO_DEFAULT_PATH
NO_CMAKE_PATH
NO_CMAKE_SYSTEM_PATH
NO_CMAKE_FIND_ROOT_PATH
REQUIRED
)

find_file(EMSCRIPTEN_VERSION_FILE
NAMES emscripten-version.txt
PATHS ${EMSCRIPTEN_HOME}/upstream/emscripten
NO_DEFAULT_PATH
NO_CMAKE_PATH
NO_CMAKE_SYSTEM_PATH
NO_CMAKE_FIND_ROOT_PATH
REQUIRED
)

file(READ ${EMSCRIPTEN_VERSION_FILE} EMSCRIPTEN_VERSION_FILE_CONTENT)

string(REGEX
MATCH
"[0-9]+\.[0-9]+(\.[0-9]+)*"
EMSCRIPTEN_VERSION
${EMSCRIPTEN_VERSION_FILE_CONTENT}
)

find_package_handle_standard_args(EMSCRIPTEN
REQUIRED_VARS EMSCRIPTEN_HOME
VERSION_VAR EMSCRIPTEN_VERSION
HANDLE_VERSION_RANGE
)

if(EMSCRIPTEN_FOUND)
set(EMSCRIPTEN_TOOLCHAIN ${EMSCRIPTEN_HOME}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake)
set(EMCC ${EMSCRIPTEN_HOME}/upstream/emscripten/emcc)
endif()
mark_as_advanced(EMSCRIPTEN_TOOLCHAIN EMCC)
27 changes: 27 additions & 0 deletions samples/debug-tools/cmake/FindWAMRC.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(FindPackageHandleStandardArgs)

find_path(WAMRC_HOME
wamr-compiler
PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../..
NO_DEFAULT_PATH
NO_CMAKE_PATH
NO_CMAKE_SYSTEM_PATH
NO_CMAKE_FIND_ROOT_PATH
REQUIRED
)

find_file(WAMRC_BIN
wamrc
HINTS ${WAMRC_HOME}/wamr-compiler/build
NO_DEFAULT_PATH
NO_CMAKE_PATH
NO_CMAKE_SYSTEM_PATH
NO_CMAKE_FIND_ROOT_PATH
REQUIRED
)

find_package_handle_standard_args(WAMRC REQUIRED_VARS WAMRC_BIN)
mark_as_advanced(WAMRC_BIN)
24 changes: 24 additions & 0 deletions samples/debug-tools/cmake/FindWASISDK.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(FindPackageHandleStandardArgs)

file(GLOB WASISDK_SEARCH_PATH "/opt/wasi-sdk-*")
find_path(WASISDK_HOME
NAMES share/wasi-sysroot
PATHS ${WASISDK_SEARCH_PATH}
NO_DEFAULT_PATH
REQUIRED
)

string(REGEX MATCH [0-9]+\.[0-9]+\.*[0-9]* WASISDK_VERSION ${WASISDK_HOME})

find_package_handle_standard_args(WASISDK REQUIRED_VARS WASISDK_HOME VERSION_VAR WASISDK_VERSION)

if(WASISDK_FOUND)
set(WASISDK_CC_COMMAND ${WASISDK_HOME}/bin/clang)
set(WASISDK_CXX_COMMAND ${WASISDK_HOME}/bin/clang++)
set(WASISDK_TOOLCHAIN ${WASISDK_HOME}/share/cmake/wasi-sdk.cmake)
set(WASISDK_SYSROOT ${WASISDK_HOME}/share/wasi-sysroot)
endif()
mark_as_advanced(WASISDK_CC_COMMAND WASISDK_CXX_COMMAND WASISDK_TOOLCHAIN WASISDK_SYSROOT WASISDK_HOME)
Loading

0 comments on commit fef26ea

Please sign in to comment.