Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions doc/source_debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ WAMR supports source level debugging based on DWARF (normally used in C/C++/Rust

**The lldb's ability to debug wasm application is based on the patch [Add class WasmProcess for WebAssembly debugging](https://reviews.llvm.org/D78801). Thanks very much to the author @paolosev for such a great work!**

## Example app code

The following `test.c` file is used in this document as example:

```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```
``` C

#include <stdio.h>

int
main(void)
{
printf("hello\n");

return 0;
}
```

## Build wasm application with debug information
To debug your application, you need to compile them with debug information. You can use `-g` option when compiling the source code if you are using wasi-sdk (also work for emcc and rustc):
``` bash
Expand Down Expand Up @@ -57,6 +73,12 @@ Then you can use lldb commands to debug your applications. Please refer to [lldb

> Note: AOT debugging is experimental and only a few debugging capabilities are supported.

0. Optionally, build llvm if not done already
``` bash
cd ${WAMR_ROOT}/wamr-compiler
./build_llvm.sh # (or "./build_llvm_xtensa.sh" to support xtensa target)
```

1. Build lldb (assume you have already built llvm)
``` bash
cd ${WAMR_ROOT}/core/deps/llvm/build
Expand All @@ -76,21 +98,21 @@ make -j $(nproc)
``` bash
cd ${WAMR_ROOT}/product-mini/platforms/linux
mkdir build && cd build
cmake .. -DWAMR_BUILD_DEBUG_AOT=1
cmake .. -DCMAKE_BUILD_TYPE:STRING="Debug" -DWAMR_BUILD_DEBUG_AOT=1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must we build iwasm with Debug mode?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think so.

i guess his intention was to match with the example below, where lldb is showing the source code of iwasm.
i think it's less confusing to add a note to the example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, how about we add a note here to mention that the -DCMAKE_BUILD_TYPE:STRING="Debug" option is to make lldb be able to showing the source code of iwasm?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess it makes sense. @maht can you confirm?

make
```

4. Compile wasm module to AOT module
``` bash
wamrc -o test.aot test.wasm
${WAMR_ROOT}/wamr-compiler/build/wamrc --opt-level=0 --size-level=0 -o test.aot test.wasm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must we add --opt-level=0 --size-level=0?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when i created the example in #1496,
i was using:

CFLAGS=-Os -g and

wamrc -o test.aot test.wasm

it was on x86-64 mac.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @maht could you help confirm it? Since --opt-level=0 --size-level=0 flags for wamrc impact the AOT performance a lot, we had better remove them if they are not necessary.

```

5. Execute iwasm using lldb

Then you can use lldb commands to debug both wamr runtime and your wasm application in ***current terminal***.

``` bash
% lldb iwasm -- test.aot
% ${WAMR_ROOT}/core/deps/llvm/build/bin/lldb ${WAMR_ROOT}/product-mini/platforms/linux/build/iwasm -- test.aot
(lldb) target create "iwasm"
Current executable set to 'iwasm' (x86_64).
(lldb) settings set -- target.run-args "test.aot"
Expand Down Expand Up @@ -129,7 +151,7 @@ wamrc -o test.aot test.wasm
Current breakpoints:
1: name = 'main', locations = 2, resolved = 2, hit count = 2
1.1: where = iwasm`main + 48 at main.c:294:11, address = 0x0000000100001020, resolved, hit count = 1
1.2: where = JIT(0x100298004)`main + 12 at hello.c:6:9, address = 0x00000001002980a0, resolved, hit count = 1
1.2: where = JIT(0x100298004)`main + 12 at test.c:6:9, address = 0x00000001002980a0, resolved, hit count = 1

(lldb)
```
Expand All @@ -139,7 +161,7 @@ wamrc -o test.aot test.wasm
* The first `main` function, which is in `main.c`, is the main
function of the iwasm command.

* The second `main` function, which is in `hello.c`, is the main
* The second `main` function, which is in `test.c`, is the main
function of the AOT-compiled wasm module.

* WAMR AOT debugging uses the GDB JIT loader mechanism to load
Expand Down
2 changes: 1 addition & 1 deletion samples/native-lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ will be generated.

```bash
cd build
./iwasm --native-lib=libtest_add.so --native-lib=libtest_sqrt.so wasm-app/test.wasm
./iwasm --native-lib=./libtest_add.so --native-lib=./libtest_sqrt.so wasm-app/test.wasm
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i picked this commit in #1643 as it conflicts otherwise.

```

The output is:
Expand Down