|  | 
|  | 1 | +# Debugging WebAssembly Modules | 
|  | 2 | + | 
|  | 3 | +This guide provides an overview of the methods and tools available for debugging native Wasm modules in the Hyperlight-Wasm environment. Effective debugging is crucial for identifying and resolving issues in Wasm modules, ensuring they function correctly within the host application. | 
|  | 4 | + | 
|  | 5 | +## Building with Debug Information | 
|  | 6 | + | 
|  | 7 | +To facilitate debugging, it is recommended to compile your Wasm modules with debug information included. This can be achieved by using the appropriate flags during the compilation process. | 
|  | 8 | +For example, when using `wasi-sdk/clang`, you can include the `-g` flag to generate debug information and `-O0` to disable optimizations. | 
|  | 9 | +You can find examples of native Wasm modules in the repository under the `rust_wasm_samples`, `wasm_samples` and `component_sample` directories. | 
|  | 10 | + | 
|  | 11 | +Next, ensure that the pre-compiled Wasm modules are built with debug information as well. To do this, you can use the `hyperlight-wasm-aot` tool with the `--features gdb` flag: | 
|  | 12 | + | 
|  | 13 | +```bash | 
|  | 14 | +cargo run --features gdb -p hyperlight-wasm-aot compile input.wasm output.aot | 
|  | 15 | +``` | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +## Example of debugging a Wasm Module | 
|  | 19 | +We have a simple example of debugging a Wasm module in the [examples/guest-debugging](../src/hyperlight_wasm/examples/guest-debugging) directory. | 
|  | 20 | + | 
|  | 21 | +This example demonstrates how to configure a Hyperlight Sandbox to enable debugging support for a Wasm module. | 
|  | 22 | + | 
|  | 23 | +When running the example, it starts a GDB server on `localhost:8080`, allowing you to connect your debugger to this address. | 
|  | 24 | +It hangs waiting for a debugger to attach before proceeding with the execution. | 
|  | 25 | +You can use either GDB or LLDB to attach to the running process. | 
|  | 26 | +You can find a sample `launch.json` configuration for Visual Studio Code that sets up remote debugging for the Wasm module using both GDB and LLDB at [VSCode launch.json Configuration](#vscode-launchjson-configuration). | 
|  | 27 | + | 
|  | 28 | +The json configuration asks for the path to the guest binary to debug, which you see printed as a `cargo:warning` when `hyperlight-wasm`, with `gdb` feature enabled, is built. | 
|  | 29 | +After you attach, you can set breakpoints, step through the code, and inspect variables in your wasm module as you would with any native application. | 
|  | 30 | + | 
|  | 31 | +**NOTE**: The debugger stops at the entry point of the guest binary, which is the `hyperlight-wasm` runtime in this case, so you may need to set additional breakpoints in your Wasm module code to effectively debug it. | 
|  | 32 | + | 
|  | 33 | +### Visual Studio Code `launch.json` Configuration | 
|  | 34 | + | 
|  | 35 | +```json | 
|  | 36 | + | 
|  | 37 | +{ | 
|  | 38 | +  "inputs": [ | 
|  | 39 | +    { | 
|  | 40 | +      "id": "program", | 
|  | 41 | +      "type": "promptString", | 
|  | 42 | +      "default": "x64/debug/wasm_runtime", | 
|  | 43 | +      "description": "Path to the program to debug" | 
|  | 44 | +    }, | 
|  | 45 | +  ], | 
|  | 46 | +  "configurations": [ | 
|  | 47 | +    { | 
|  | 48 | +      "name": "Remote GDB attach", | 
|  | 49 | +      "type": "cppdbg", | 
|  | 50 | +      "request": "launch", | 
|  | 51 | +      "program": "${input:program}", | 
|  | 52 | +      "args": [], | 
|  | 53 | +      "stopAtEntry": true, | 
|  | 54 | +      "hardwareBreakpoints": { | 
|  | 55 | +        "require": false, | 
|  | 56 | +        "limit": 4 | 
|  | 57 | +      }, | 
|  | 58 | +      "cwd": "${workspaceFolder}", | 
|  | 59 | +      "environment": [], | 
|  | 60 | +      "externalConsole": false, | 
|  | 61 | +      "MIMode": "gdb", | 
|  | 62 | +      "miDebuggerPath": "/usr/bin/gdb", | 
|  | 63 | +      "miDebuggerServerAddress": "localhost:8080", | 
|  | 64 | +      "setupCommands": [ | 
|  | 65 | +        { | 
|  | 66 | +          "description": "Enable pretty-printing for gdb", | 
|  | 67 | +          "text": "-enable-pretty-printing", | 
|  | 68 | +          "ignoreFailures": true | 
|  | 69 | +        }, | 
|  | 70 | +        { | 
|  | 71 | +          "description": "Set Disassembly Flavor to Intel", | 
|  | 72 | +          "text": "-gdb-set disassembly-flavor intel", | 
|  | 73 | +          "ignoreFailures": true | 
|  | 74 | +        } | 
|  | 75 | +      ] | 
|  | 76 | +    }, | 
|  | 77 | +    { | 
|  | 78 | +      "name": "Remote LLDB attach", | 
|  | 79 | +      "type": "lldb", | 
|  | 80 | +      "request": "launch", | 
|  | 81 | +      "targetCreateCommands": [ | 
|  | 82 | +        "target create ${input:program}" | 
|  | 83 | +      ], | 
|  | 84 | +      "processCreateCommands": [ | 
|  | 85 | +        "gdb-remote localhost:8080" | 
|  | 86 | +      ] | 
|  | 87 | +    }, | 
|  | 88 | +  ] | 
|  | 89 | +} | 
|  | 90 | +``` | 
0 commit comments