Skip to content

Commit e7fd9c0

Browse files
committed
Enable native debugging for wasm modules/components
Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 5eefd78 commit e7fd9c0

File tree

16 files changed

+372
-51
lines changed

16 files changed

+372
-51
lines changed

.vscode/launch.json

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,96 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
// this can be used to debug tests
6-
"version": "0.2.0",
2+
"inputs": [
3+
{
4+
"id": "program",
5+
"type": "promptString",
6+
"default": "x64/debug/wasm_runtime",
7+
"description": "Path to the program to debug",
8+
}
9+
],
710
"configurations": [
811
{
912
"type": "lldb",
1013
"request": "launch",
11-
"name": "Cargo test",
14+
"name": "Debug example 'guest-debugging'",
15+
"cargo": {
16+
"args": [
17+
"build",
18+
"--example=guest-debugging",
19+
"--package=hyperlight-wasm",
20+
"--features=gdb",
21+
],
22+
"filter": {
23+
"name": "guest-debugging",
24+
"kind": "example"
25+
}
26+
},
27+
"env": {
28+
"RUST_DIR_FOR_DEBUGGING_TESTS": "${workspaceFolder}/src/hyperlight_wasm",
29+
},
30+
"args": [],
31+
"cwd": "${workspaceFolder}"
32+
},
33+
{
34+
"type": "lldb",
35+
"request": "launch",
36+
"name": "Debug example 'rust_wasm_examples'",
1237
"cargo": {
13-
"args": [
14-
"test",
15-
"--profile=dev",
16-
"--lib",
17-
"--no-run"
18-
19-
],
20-
"filter": {
21-
"name": "hyperlight_wasm",
22-
"kind": "lib"
23-
}
38+
"args": [
39+
"build",
40+
"--example=rust_wasm_examples",
41+
"--package=hyperlight-wasm",
42+
],
43+
"filter": {
44+
"name": "rust_wasm_examples",
45+
"kind": "example"
46+
}
2447
},
2548
"env": {
26-
"RUST_DIR_FOR_DEBUGGING_TESTS": "${workspaceFolder}/src/hyperlight_wasm"
49+
"RUST_DIR_FOR_DEBUGGING_TESTS": "${workspaceFolder}/src/hyperlight_wasm",
50+
},
51+
"args": [],
52+
"cwd": "${workspaceFolder}"
53+
},
54+
{
55+
"name": "Remote GDB attach",
56+
"type": "cppdbg",
57+
"request": "launch",
58+
"program": "${input:program}",
59+
"args": [],
60+
"stopAtEntry": true,
61+
"hardwareBreakpoints": {
62+
"require": false,
63+
"limit": 4
2764
},
28-
"args": [
29-
"--exact",
30-
"sandbox::loaded_wasm_sandbox::tests::test_call_host_func_with_vecbytes"
65+
"cwd": "${workspaceFolder}",
66+
"environment": [],
67+
"externalConsole": false,
68+
"MIMode": "gdb",
69+
"miDebuggerPath": "/usr/bin/gdb",
70+
"miDebuggerServerAddress": "localhost:8080",
71+
"setupCommands": [
72+
{
73+
"description": "Enable pretty-printing for gdb",
74+
"text": "-enable-pretty-printing",
75+
"ignoreFailures": true
76+
},
77+
{
78+
"description": "Set Disassembly Flavor to Intel",
79+
"text": "-gdb-set disassembly-flavor intel",
80+
"ignoreFailures": true
81+
},
3182
]
32-
}
83+
},
84+
{
85+
"name": "Remote LLDB attach",
86+
"type": "lldb",
87+
"request": "launch",
88+
"targetCreateCommands": [
89+
"target create ${input:program}",
90+
],
91+
"processCreateCommands": [
92+
"gdb-remote localhost:8080"
93+
],
94+
},
3395
]
3496
}

Cargo.lock

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Justfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ensure-tools:
1919
cargo install cargo-component --locked --version 0.21.1
2020
cargo install wit-bindgen-cli --locked --version 0.43.0
2121

22-
build-all target=default-target: (build target) (build-wasm-examples target) (build-rust-wasm-examples target) (build-rust-component-examples target) (build-wasm-runtime target)
22+
build-all target=default-target features="": (build target features) (build-wasm-examples target features) (build-rust-wasm-examples target features) (build-rust-component-examples target) (build-wasm-runtime target features)
2323

2424
build target=default-target features="": (fmt-check)
2525
cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --verbose --profile={{ if target == "debug" {"dev"} else { target } }}
@@ -32,23 +32,23 @@ compile-wit:
3232
wasm-tools component wit ./src/wasmsamples/components/runcomponent.wit -w -o ./src/wasmsamples/components/runcomponent-world.wasm
3333
wasm-tools component wit ./src/component_sample/wit/example.wit -w -o ./src/component_sample/wit/component-world.wasm
3434

35-
build-wasm-runtime target=default-target:
36-
cd ./src/wasm_runtime && cargo build --verbose --profile={{ if target == "debug" {"dev"} else { target } }} && rm -R target
35+
build-wasm-runtime target=default-target features="":
36+
cd ./src/wasm_runtime && cargo build --verbose {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} && rm -R target
3737

38-
build-wasm-examples target=default-target: (compile-wit)
39-
{{ build-wasm-examples-command }} {{target}}
38+
build-wasm-examples target=default-target features="": (compile-wit)
39+
{{ build-wasm-examples-command }} {{target}} {{features}}
4040

41-
build-rust-wasm-examples target=default-target: (mkdir-redist target)
41+
build-rust-wasm-examples target=default-target features="": (mkdir-redist target)
4242
rustup target add wasm32-unknown-unknown
4343
cd ./src/rust_wasm_samples && cargo build --target wasm32-unknown-unknown --profile={{ if target == "debug" {"dev"} else { target } }}
44-
cargo run -p hyperlight-wasm-aot compile ./src/rust_wasm_samples/target/wasm32-unknown-unknown/{{ target }}/rust_wasm_samples.wasm ./x64/{{ target }}/rust_wasm_samples.aot
44+
cargo run {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} -p hyperlight-wasm-aot compile ./src/rust_wasm_samples/target/wasm32-unknown-unknown/{{ target }}/rust_wasm_samples.wasm ./x64/{{ target }}/rust_wasm_samples.aot
4545

46-
build-rust-component-examples target=default-target: (compile-wit)
46+
build-rust-component-examples target=default-target features="": (compile-wit)
4747
# use cargo component so we don't get all the wasi imports https://github.com/bytecodealliance/cargo-component?tab=readme-ov-file#relationship-with-wasm32-wasip2
4848
# we also explicitly target wasm32-unknown-unknown since cargo component might try to pull in wasi imports https://github.com/bytecodealliance/cargo-component/issues/290
4949
rustup target add wasm32-unknown-unknown
5050
cd ./src/component_sample && cargo component build --target wasm32-unknown-unknown --profile={{ if target == "debug" {"dev"} else { target } }}
51-
cargo run -p hyperlight-wasm-aot compile --component ./src/component_sample/target/wasm32-unknown-unknown/{{ target }}/component_sample.wasm ./x64/{{ target }}/component_sample.aot
51+
cargo run {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} -p hyperlight-wasm-aot compile --component ./src/component_sample/target/wasm32-unknown-unknown/{{ target }}/component_sample.wasm ./x64/{{ target }}/component_sample.aot
5252

5353
check target=default-target:
5454
cargo check --profile={{ if target == "debug" {"dev"} else { target } }}

src/hyperlight_wasm/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ name = "helloworld"
2929
path = "examples/helloworld/main.rs"
3030
test = true
3131

32+
[[example]]
33+
name = "guest-debugging"
34+
path = "examples/guest-debugging/main.rs"
35+
test = true
36+
3237
[[example]]
3338
name = "hostfuncs"
3439
path = "examples/hostfuncs/main.rs"
@@ -52,6 +57,7 @@ tracing = "0.1.27"
5257
log = "0.4.28"
5358
cfg-if = { version = "1" }
5459
metrics = "0.24.2"
60+
env_logger = "0.11.8"
5561

5662
[target.'cfg(windows)'.dependencies]
5763
windows = { version = "0.62", features = ["Win32_System_Threading"] }
@@ -68,6 +74,7 @@ metrics-util = "0.20.0"
6874
metrics-exporter-prometheus = "0.17"
6975

7076
[build-dependencies]
77+
cfg_aliases = "0.2.1"
7178
chrono = "0.4"
7279
blake3 = "1.8"
7380
built = { version = "0.8.0", features = ["chrono", "git2"] }
@@ -81,6 +88,7 @@ function_call_metrics = ["hyperlight-host/function_call_metrics"]
8188
seccomp = ["hyperlight-host/seccomp"]
8289
print_debug = ["hyperlight-host/print_debug"]
8390
crashdump = ["hyperlight-host/crashdump"]
91+
gdb = ["hyperlight-host/gdb"]
8492
kvm = ["hyperlight-host/kvm"]
8593
mshv2 = ["hyperlight-host/mshv2"]
8694
mshv3 = ["hyperlight-host/mshv3"]

src/hyperlight_wasm/build.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn build_wasm_runtime() -> PathBuf {
125125
env_vars.retain(|(key, _)| !key.starts_with("CARGO_"));
126126

127127
let mut cargo_cmd = std::process::Command::new(&cargo_bin);
128-
let cmd = cargo_cmd
128+
let mut cmd = cargo_cmd
129129
.arg("build")
130130
.arg("--profile")
131131
.arg(cargo_profile)
@@ -137,6 +137,12 @@ fn build_wasm_runtime() -> PathBuf {
137137
.envs(env_vars)
138138
.env("PATH", path_with(&toolchain_dir))
139139
.env("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT", &toolchain_dir);
140+
141+
// Add --features gdb if the gdb feature is enabled for this build script
142+
if std::env::var("CARGO_FEATURE_GDB").is_ok() {
143+
cmd = cmd.arg("--features").arg("gdb");
144+
}
145+
140146
let status = cmd
141147
.status()
142148
.unwrap_or_else(|e| panic!("could not run cargo build wasm_runtime: {}", e));
@@ -239,5 +245,9 @@ fn main() -> Result<()> {
239245

240246
println!("cargo:rerun-if-changed=build.rs");
241247

248+
cfg_aliases::cfg_aliases! {
249+
gdb: { all(feature = "gdb", debug_assertions) },
250+
}
251+
242252
Ok(())
243253
}

0 commit comments

Comments
 (0)