|
| 1 | +# Guest (Wasm-only) Debugging with `lldb` |
| 2 | + |
| 3 | +The following steps describe how to use `lldb` to debug the Wasm guest |
| 4 | +on its own -- as if it were running on a virtual Wasm-instruction-set |
| 5 | +computer with hostcalls as single-step indivisible actions. This |
| 6 | +functionality, called "guest debugging", allows for disassembly and |
| 7 | +single instruction stepping at the Wasm bytecode level. |
| 8 | + |
| 9 | +1. Compile your WebAssembly with debug info enabled, usually `-g`; for |
| 10 | + example: |
| 11 | + |
| 12 | + ```console |
| 13 | + clang foo.c -g -o foo.wasm |
| 14 | + ``` |
| 15 | + |
| 16 | +2. Ensure that you have a build of Wasmtime that has the `gdbstub` |
| 17 | + feature enabled, which is off by default: |
| 18 | + |
| 19 | + - Published CLI binary releases already have this feature. |
| 20 | + - If building from source, use `cargo build --features gdbstub`. |
| 21 | + |
| 22 | +3. Run Wasmtime, enabling the gdbstub server: |
| 23 | + |
| 24 | + ```console |
| 25 | + wasmtime run -g 1234 foo.wasm |
| 26 | + |
| 27 | + Debugger: Debugger listening on 127.0.0.1:1234 |
| 28 | + Debugger: In LLDB, attach with: process connect --plugin wasm connect://127.0.0.1:1234 |
| 29 | + ``` |
| 30 | + |
| 31 | + This will start a "debug server" waiting on local TCP port 1234 for |
| 32 | + a debugger to connect. Execution will not start until the debugger |
| 33 | + connects and issues a "continue" command. |
| 34 | + |
| 35 | +4. Run LLDB, connect and debug. |
| 36 | + |
| 37 | + You'll need a recent version of LLDB (v32 or later) with Wasm |
| 38 | + support enabled. The [wasi-sdk] distribution provides such a |
| 39 | + build. |
| 40 | + |
| 41 | + ```console |
| 42 | + /opt/wasi-sdk/bin/lldb |
| 43 | + (lldb) process connect --plugin wasm connect://0:1234 |
| 44 | + Process 1 stopped |
| 45 | + * thread #1, stop reason = signal SIGTRAP |
| 46 | + frame #0: 0x00000000 |
| 47 | + error: memory read failed for 0x0 |
| 48 | + (lldb) b my_function |
| 49 | + (lldb) continue |
| 50 | + ``` |
| 51 | + |
| 52 | + and use LLDB like normal, setting breakpoints, continuing and |
| 53 | + stepping, examining memory and variable state, etc. |
| 54 | + |
| 55 | +This functionality should work on any platform that Wasmtime runs on: |
| 56 | +debugging is based on code instrumentation, so does not depend on any |
| 57 | +native system debugging interfaces or introspection capabilities; and |
| 58 | +is supported on all native-compilation ISAs and on Pulley, Wasmtime's |
| 59 | +bytecode platform that runs everywhere. |
| 60 | + |
| 61 | +[wasi-sdk]: https://github.com/WebAssembly/wasi-sdk/ |
0 commit comments