From 482afd6e85defffab283cbc538bacfd622186241 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 6 Jun 2024 17:51:24 -0700 Subject: [PATCH 1/2] Recommend `-O opt-level=0` when debugging wasm This improves inspection of local variables by avoiding the egraph pass which doesn't have full fidelity in terms of preserving debug information. --- docs/examples-debugging-native-debugger.md | 5 +++-- examples/fib-debug/main.c | 1 + examples/fib-debug/main.rs | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/examples-debugging-native-debugger.md b/docs/examples-debugging-native-debugger.md index b61fe494fef3..1a64538ae8dd 100644 --- a/docs/examples-debugging-native-debugger.md +++ b/docs/examples-debugging-native-debugger.md @@ -13,7 +13,8 @@ the same time: 2. Run Wasmtime with the debug info enabled; this is `-D debug-info` from the CLI and `Config::debug_info(true)` in an embedding (e.g. see [debugging in a - Rust embedding](./examples-rust-debugging.md)) + Rust embedding](./examples-rust-debugging.md)). It's also recommended to use + `-O opt-level=0` for better inspection of local variables if desired. 3. Use a supported debugger: @@ -21,7 +22,7 @@ the same time: lldb -- wasmtime run -D debug-info foo.wasm ``` ```sh - gdb --args wasmtime run -D debug-info foo.wasm + gdb --args wasmtime run -D debug-info -O opt-level=0 foo.wasm ``` If you run into trouble, the following discussions might help: diff --git a/examples/fib-debug/main.c b/examples/fib-debug/main.c index dee99461eeef..8633fb030129 100644 --- a/examples/fib-debug/main.c +++ b/examples/fib-debug/main.c @@ -16,6 +16,7 @@ int main(int argc, const char *argv[]) { // original fib-wasm.c source code and variables. wasm_config_t *config = wasm_config_new(); wasmtime_config_debug_info_set(config, true); + wasmtime_config_cranelift_opt_level_set(config, WASMTIME_OPT_LEVEL_NONE); // Initialize. printf("Initializing...\n"); diff --git a/examples/fib-debug/main.rs b/examples/fib-debug/main.rs index ab8f81482717..3c12cd900276 100644 --- a/examples/fib-debug/main.rs +++ b/examples/fib-debug/main.rs @@ -14,7 +14,7 @@ fn main() -> Result<()> { // Load our previously compiled wasm file (built previously with Cargo) and // also ensure that we generate debuginfo so this executable can be // debugged in GDB. - let engine = Engine::new(Config::new().debug_info(true))?; + let engine = Engine::new(Config::new().debug_info(true).opt_level(OptLevel::None))?; let mut store = Store::new(&engine, ()); let module = Module::from_file(&engine, "target/wasm32-unknown-unknown/debug/fib.wasm")?; let instance = Instance::new(&mut store, &module, &[])?; From a4539111a7568fad5ff85871d548ed8dda83a31d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 6 Jun 2024 19:30:22 -0700 Subject: [PATCH 2/2] Fix example compile --- examples/fib-debug/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/fib-debug/main.rs b/examples/fib-debug/main.rs index 3c12cd900276..55a6e84cafe2 100644 --- a/examples/fib-debug/main.rs +++ b/examples/fib-debug/main.rs @@ -14,7 +14,11 @@ fn main() -> Result<()> { // Load our previously compiled wasm file (built previously with Cargo) and // also ensure that we generate debuginfo so this executable can be // debugged in GDB. - let engine = Engine::new(Config::new().debug_info(true).opt_level(OptLevel::None))?; + let engine = Engine::new( + Config::new() + .debug_info(true) + .cranelift_opt_level(OptLevel::None), + )?; let mut store = Store::new(&engine, ()); let module = Module::from_file(&engine, "target/wasm32-unknown-unknown/debug/fib.wasm")?; let instance = Instance::new(&mut store, &module, &[])?;