Skip to content

Commit b642afd

Browse files
committed
feat: try to add --trace arg, and pass tracer: &mut WasmTracer everywhere
initially was passing `tracing`, but this seems more flexible: to both hold our state and our flag; too many file changes?
1 parent 1b99258 commit b642afd

File tree

22 files changed

+118
-39
lines changed

22 files changed

+118
-39
lines changed

Cargo.lock

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

crates/c_api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ links = "wasmi_c_api"
1717
[dependencies]
1818
wasmi = { workspace = true }
1919
wasmi_c_api_macros = { workspace = true }
20+
wasmi_tracer = { workspace = true }
2021

2122
[lib]
2223
name = "wasmi_c_api"

crates/c_api/src/func.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
use alloc::{boxed::Box, string::String, vec, vec::Vec};
1010
use core::{any::Any, ffi::c_void, hint, iter, ptr, str};
1111
use wasmi::{Error, Extern, Func, FuncRef, Val};
12+
use wasmi_tracer::WasmTracer;
1213

1314
#[cfg(feature = "std")]
1415
use core::panic::AssertUnwindSafe;
@@ -210,7 +211,8 @@ pub unsafe extern "C" fn wasm_func_call(
210211
// can. As a result we catch panics here and transform them to traps to
211212
// allow the caller to have any insulation possible against Rust panics.
212213
std::panic::catch_unwind(AssertUnwindSafe(|| {
213-
f.call(func.inner.store.context_mut(), wt_params, wt_results)
214+
let mut tracer = WasmTracer::no_tracing();
215+
f.call(func.inner.store.context_mut(), wt_params, wt_results, &mut tracer)
214216
}))
215217
}
216218
#[cfg(not(feature = "std"))]

crates/c_api/src/instance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
};
99
use alloc::boxed::Box;
1010
use wasmi::Instance;
11+
use wasmi_tracer::WasmTracer;
1112

1213
/// A Wasm instance.
1314
///
@@ -56,7 +57,8 @@ pub unsafe extern "C" fn wasm_instance_new(
5657
.iter()
5758
.filter_map(|import| import.as_ref().map(|i| i.which))
5859
.collect::<Box<[_]>>();
59-
match Instance::new(store.inner.context_mut(), &wasm_module.inner, &imports) {
60+
let mut tracer = WasmTracer::no_tracing();
61+
match Instance::new(store.inner.context_mut(), &wasm_module.inner, &imports, &mut tracer) {
6062
Ok(instance) => Some(Box::new(wasm_instance_t::new(
6163
store.inner.clone(),
6264
instance,

crates/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exclude.workspace = true
1616
[dependencies]
1717
wasmi = { workspace = true, features = ["wat"] }
1818
wasmi_wasi = { workspace = true }
19+
wasmi_tracer = { workspace = true }
1920
anyhow = "1"
2021
clap = { version = "4", features = ["derive"] }
2122

crates/cli/src/args.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ pub struct Args {
9999
/// Arguments given to the Wasm module or the invoked function.
100100
#[clap(value_name = "ARGS")]
101101
func_args: Vec<String>,
102+
103+
#[clap(
104+
long = "trace",
105+
default_value_t = false,
106+
)]
107+
tracing: bool,
102108
}
103109

104110
/// The chosen Wasmi compilation mode.
@@ -151,6 +157,11 @@ impl Args {
151157
self.verbose
152158
}
153159

160+
/// Returns `true` if tracing is enabled.
161+
pub fn tracing(&self) -> bool {
162+
self.tracing
163+
}
164+
154165
/// Pre-opens all directories given in `--dir` and returns them for use by the [`WasiCtx`].
155166
///
156167
/// # Errors

crates/cli/src/context.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{anyhow, Error};
22
use std::{fs, path::Path};
33
use wasmi::{CompilationMode, Config, ExternType, Func, FuncType, Instance, Module, Store};
44
use wasmi_wasi::WasiCtx;
5+
use wasmi_tracer::WasmTracer;
56

67
/// The [`Context`] for the Wasmi CLI application.
78
///
@@ -13,6 +14,8 @@ pub struct Context {
1314
store: Store<WasiCtx>,
1415
/// The Wasm module instance to operate on.
1516
instance: Instance,
17+
// Is the tracing enabled
18+
// tracing: bool,
1619
}
1720

1821
impl Context {
@@ -27,6 +30,7 @@ impl Context {
2730
wasi_ctx: WasiCtx,
2831
fuel: Option<u64>,
2932
compilation_mode: CompilationMode,
33+
tracer: &mut WasmTracer,
3034
) -> Result<Self, Error> {
3135
let mut config = Config::default();
3236
if fuel.is_some() {
@@ -52,7 +56,7 @@ impl Context {
5256
.map_err(|error| anyhow!("failed to add WASI definitions to the linker: {error}"))?;
5357
let instance = linker
5458
.instantiate(&mut store, &module)
55-
.and_then(|pre| pre.start(&mut store))
59+
.and_then(|pre| pre.start(&mut store, tracer))
5660
.map_err(|error| anyhow!("failed to instantiate and start the Wasm module: {error}"))?;
5761
Ok(Self {
5862
module,

crates/cli/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clap::Parser;
77
use context::Context;
88
use std::{path::Path, process};
99
use wasmi::{Func, FuncType, Val};
10+
use wasmi_tracer::WasmTracer;
1011

1112
mod args;
1213
mod context;
@@ -20,7 +21,9 @@ fn main() -> Result<()> {
2021
let args = Args::parse();
2122
let wasm_file = args.wasm_file();
2223
let wasi_ctx = args.wasi_context()?;
23-
let mut ctx = Context::new(wasm_file, wasi_ctx, args.fuel(), args.compilation_mode())?;
24+
let wasm_file_full_path = std::fs::canonicalize(&wasm_file)?;
25+
let mut tracer = WasmTracer::new(args.tracing(), &wasm_file_full_path);
26+
let mut ctx = Context::new(wasm_file, wasi_ctx, args.fuel(), args.compilation_mode(), &mut tracer)?;
2427
let (func_name, func) = get_invoked_func(&args, &ctx)?;
2528
let ty = func.ty(ctx.store());
2629
let func_args = utils::decode_func_args(&ty, args.func_args())?;
@@ -39,7 +42,7 @@ fn main() -> Result<()> {
3942
)
4043
}
4144

42-
match func.call(ctx.store_mut(), &func_args, &mut func_results) {
45+
match func.call(ctx.store_mut(), &func_args, &mut func_results, &mut tracer) {
4346
Ok(()) => {
4447
print_remaining_fuel(&args, &ctx);
4548
print_pretty_results(&func_results);

crates/tracer/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl DebugInfo {
2525

2626
#[derive(Debug, Clone)]
2727
pub struct WasmTracer {
28+
pub tracing: bool,
2829
debug_info: DebugInfo,
2930
// TODO: tracer: runtime_tracing.Tracer,
3031
// etc
@@ -33,12 +34,20 @@ pub struct WasmTracer {
3334
// just to make it build so we can branch-out
3435

3536
impl WasmTracer {
36-
pub fn new(wasm_exe_path: &Path) -> Self {
37+
pub fn new(tracing: bool, wasm_exe_path: &Path) -> Self {
3738
WasmTracer {
39+
tracing,
3840
debug_info: DebugInfo::new(wasm_exe_path),
3941
}
4042
}
4143

44+
pub fn no_tracing() -> Self {
45+
WasmTracer {
46+
tracing: false,
47+
debug_info: DebugInfo::new(&Path::new("")),
48+
}
49+
}
50+
4251
pub fn load_local_variables(&mut self, address: usize) { // -> ???
4352
println!("load_local_variables {address}");
4453
// e.g. here we might call something like

crates/wasmi/src/engine/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct Config {
2727
compilation_mode: CompilationMode,
2828
/// Enforced limits for Wasm module parsing and compilation.
2929
limits: EnforcedLimits,
30+
// tracing: bool,
3031
}
3132

3233
/// Type storing all kinds of fuel costs of instructions.

0 commit comments

Comments
 (0)