Skip to content

Commit 1002b22

Browse files
committed
feat: try to add --trace arg mapped to tracing flag: too many file changes?
1 parent 1b99258 commit 1002b22

File tree

15 files changed

+74
-35
lines changed

15 files changed

+74
-35
lines changed

crates/c_api/src/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub unsafe extern "C" fn wasm_func_call(
210210
// can. As a result we catch panics here and transform them to traps to
211211
// allow the caller to have any insulation possible against Rust panics.
212212
std::panic::catch_unwind(AssertUnwindSafe(|| {
213-
f.call(func.inner.store.context_mut(), wt_params, wt_results)
213+
f.call(func.inner.store.context_mut(), wt_params, wt_results, false)
214214
}))
215215
}
216216
#[cfg(not(feature = "std"))]

crates/c_api/src/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub unsafe extern "C" fn wasm_instance_new(
5656
.iter()
5757
.filter_map(|import| import.as_ref().map(|i| i.which))
5858
.collect::<Box<[_]>>();
59-
match Instance::new(store.inner.context_mut(), &wasm_module.inner, &imports) {
59+
match Instance::new(store.inner.context_mut(), &wasm_module.inner, &imports, false) {
6060
Ok(instance) => Some(Box::new(wasm_instance_t::new(
6161
store.inner.clone(),
6262
instance,

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
@@ -13,6 +13,8 @@ pub struct Context {
1313
store: Store<WasiCtx>,
1414
/// The Wasm module instance to operate on.
1515
instance: Instance,
16+
// Is the tracing enabled
17+
// tracing: bool,
1618
}
1719

1820
impl Context {
@@ -27,6 +29,7 @@ impl Context {
2729
wasi_ctx: WasiCtx,
2830
fuel: Option<u64>,
2931
compilation_mode: CompilationMode,
32+
tracing: bool,
3033
) -> Result<Self, Error> {
3134
let mut config = Config::default();
3235
if fuel.is_some() {
@@ -52,12 +55,13 @@ impl Context {
5255
.map_err(|error| anyhow!("failed to add WASI definitions to the linker: {error}"))?;
5356
let instance = linker
5457
.instantiate(&mut store, &module)
55-
.and_then(|pre| pre.start(&mut store))
58+
.and_then(|pre| pre.start(&mut store, tracing))
5659
.map_err(|error| anyhow!("failed to instantiate and start the Wasm module: {error}"))?;
5760
Ok(Self {
5861
module,
5962
store,
6063
instance,
64+
// tracing,
6165
})
6266
}
6367

crates/cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() -> Result<()> {
2020
let args = Args::parse();
2121
let wasm_file = args.wasm_file();
2222
let wasi_ctx = args.wasi_context()?;
23-
let mut ctx = Context::new(wasm_file, wasi_ctx, args.fuel(), args.compilation_mode())?;
23+
let mut ctx = Context::new(wasm_file, wasi_ctx, args.fuel(), args.compilation_mode(), args.tracing())?;
2424
let (func_name, func) = get_invoked_func(&args, &ctx)?;
2525
let ty = func.ty(ctx.store());
2626
let func_args = utils::decode_func_args(&ty, args.func_args())?;
@@ -39,7 +39,7 @@ fn main() -> Result<()> {
3939
)
4040
}
4141

42-
match func.call(ctx.store_mut(), &func_args, &mut func_results) {
42+
match func.call(ctx.store_mut(), &func_args, &mut func_results, args.tracing()) {
4343
Ok(()) => {
4444
print_remaining_fuel(&args, &ctx);
4545
print_pretty_results(&func_results);

crates/wasmi/src/engine/executor/instrs.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ pub fn execute_instrs<'engine, T>(
7171
store: &mut Store<T>,
7272
stack: &'engine mut Stack,
7373
code_map: &'engine CodeMap,
74+
tracing: bool,
7475
) -> Result<(), Error> {
7576
let instance = stack.calls.instance_expect();
7677
let cache = CachedInstance::new(&mut store.inner, instance);
77-
Executor::new(stack, code_map, cache).execute(store)
78+
Executor::new(stack, code_map, cache, tracing).execute(store)
7879
}
7980

8081
/// An execution context for executing a Wasmi function frame.
@@ -95,6 +96,7 @@ struct Executor<'engine> {
9596

9697
// debug:
9798
// TODO tracer: &'engine mut WasmTracer,
99+
tracing: bool,
98100
}
99101

100102
impl<'engine> Executor<'engine> {
@@ -104,6 +106,7 @@ impl<'engine> Executor<'engine> {
104106
stack: &'engine mut Stack,
105107
code_map: &'engine CodeMap,
106108
cache: CachedInstance,
109+
tracing: bool,
107110
) -> Self {
108111
let frame = stack
109112
.calls
@@ -122,6 +125,7 @@ impl<'engine> Executor<'engine> {
122125
stack,
123126
code_map,
124127
// TODO tracer: &mut tracer, // probably were not gonna make it
128+
tracing,
125129
}
126130
}
127131

@@ -130,16 +134,17 @@ impl<'engine> Executor<'engine> {
130134
fn execute<T>(mut self, store: &mut Store<T>) -> Result<(), Error> {
131135
use Instruction as Instr;
132136
loop {
133-
// TODO: change that, just startting from somewehre
134-
// Args: path : Path, instruction address(?) , step?
135-
// TODO: good way to take address
136-
//let address = usize::from(self.ip.get());
137-
let address = 0x000000010; // should be in the main subprogram >= low_pc
138-
//std::println!("address of {:?}: {:?}", *self.ip.get(), address);
139-
// TODO: make this be a field with correct lifetime
140-
let mut tracer = WasmTracer::new(std::path::Path::new("/home/pesho/code/codetracer-wasmi-recorder/wasm_test.wasm")); // "<path to test.wasm>: for now hardcoded TODO pass");
141-
tracer.load_local_variables(address);
142-
137+
if self.tracing {
138+
// TODO: change that, just startting from somewehre
139+
// Args: path : Path, instruction address(?) , step?
140+
// TODO: good way to take address
141+
//let address = usize::from(self.ip.get());
142+
let address = 0x000000010; // should be in the main subprogram >= low_pc
143+
//std::println!("address of {:?}: {:?}", *self.ip.get(), address);
144+
// TODO: make this be a field with correct lifetime
145+
let mut tracer = WasmTracer::new(std::path::Path::new("/home/pesho/code/codetracer-wasmi-recorder/wasm_test.wasm")); // "<path to test.wasm>: for now hardcoded TODO pass");
146+
tracer.load_local_variables(address);
147+
}
143148
// load debuginfo from gimli <- for this ip;
144149
// -> find out current scope and vars in it:
145150
// some kind of mapping between them and locations

crates/wasmi/src/engine/executor/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ impl EngineInner {
4141
func: &Func,
4242
params: impl CallParams,
4343
results: Results,
44+
tracing: bool,
4445
) -> Result<<Results as CallResults>::Results, Error>
4546
where
4647
Results: CallResults,
4748
{
4849
let mut stack = self.stacks.lock().reuse_or_new();
4950
let results = EngineExecutor::new(&self.code_map, &mut stack)
50-
.execute_root_func(ctx.store, func, params, results)
51+
.execute_root_func(ctx.store, func, params, results, tracing)
5152
.map_err(|error| match error.into_resumable() {
5253
Ok(error) => error.into_error(),
5354
Err(error) => error,
@@ -69,14 +70,15 @@ impl EngineInner {
6970
func: &Func,
7071
params: impl CallParams,
7172
results: Results,
73+
tracing: bool,
7274
) -> Result<ResumableCallBase<<Results as CallResults>::Results>, Error>
7375
where
7476
Results: CallResults,
7577
{
7678
let store = ctx.store;
7779
let mut stack = self.stacks.lock().reuse_or_new();
7880
let results = EngineExecutor::new(&self.code_map, &mut stack)
79-
.execute_root_func(store, func, params, results);
81+
.execute_root_func(store, func, params, results, tracing);
8082
match results {
8183
Ok(results) => {
8284
self.stacks.lock().recycle(stack);
@@ -117,6 +119,7 @@ impl EngineInner {
117119
mut invocation: ResumableInvocation,
118120
params: impl CallParams,
119121
results: Results,
122+
tracing: bool,
120123
) -> Result<ResumableCallBase<<Results as CallResults>::Results>, Error>
121124
where
122125
Results: CallResults,
@@ -129,6 +132,7 @@ impl EngineInner {
129132
params,
130133
caller_results,
131134
results,
135+
tracing,
132136
);
133137
match results {
134138
Ok(results) => {
@@ -185,6 +189,7 @@ impl<'engine> EngineExecutor<'engine> {
185189
func: &Func,
186190
params: impl CallParams,
187191
results: Results,
192+
tracing: bool,
188193
) -> Result<<Results as CallResults>::Results, Error>
189194
where
190195
Results: CallResults,
@@ -217,7 +222,7 @@ impl<'engine> EngineExecutor<'engine> {
217222
Some(instance),
218223
)?;
219224
store.invoke_call_hook(CallHook::CallingWasm)?;
220-
self.execute_func(store)?;
225+
self.execute_func(store, tracing)?;
221226
store.invoke_call_hook(CallHook::ReturningFromWasm)?;
222227
}
223228
FuncEntity::Host(host_func) => {
@@ -259,6 +264,7 @@ impl<'engine> EngineExecutor<'engine> {
259264
params: impl CallParams,
260265
caller_results: RegSpan,
261266
results: Results,
267+
tracing: bool,
262268
) -> Result<<Results as CallResults>::Results, Error>
263269
where
264270
Results: CallResults,
@@ -274,7 +280,7 @@ impl<'engine> EngineExecutor<'engine> {
274280
for (result, param) in caller_results.iter_sized(len_params).zip(call_params) {
275281
unsafe { caller_sp.set(result, param) };
276282
}
277-
self.execute_func(store)?;
283+
self.execute_func(store, tracing)?;
278284
let results = self.write_results_back(results);
279285
Ok(results)
280286
}
@@ -285,8 +291,8 @@ impl<'engine> EngineExecutor<'engine> {
285291
///
286292
/// When encountering a Wasm or host trap during execution.
287293
#[inline(always)]
288-
fn execute_func<T>(&mut self, store: &mut Store<T>) -> Result<(), Error> {
289-
execute_instrs(store, self.stack, self.code_map)
294+
fn execute_func<T>(&mut self, store: &mut Store<T>, tracing: bool) -> Result<(), Error> {
295+
execute_instrs(store, self.stack, self.code_map, tracing)
290296
}
291297

292298
/// Convenience forwarder to [`dispatch_host_func`].

crates/wasmi/src/engine/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,12 @@ impl Engine {
295295
func: &Func,
296296
params: impl CallParams,
297297
results: Results,
298+
tracing: bool,
298299
) -> Result<<Results as CallResults>::Results, Error>
299300
where
300301
Results: CallResults,
301302
{
302-
self.inner.execute_func(ctx, func, params, results)
303+
self.inner.execute_func(ctx, func, params, results, tracing)
303304
}
304305

305306
/// Executes the given [`Func`] resumably with parameters `params` and returns.
@@ -331,12 +332,13 @@ impl Engine {
331332
func: &Func,
332333
params: impl CallParams,
333334
results: Results,
335+
tracing: bool,
334336
) -> Result<ResumableCallBase<<Results as CallResults>::Results>, Error>
335337
where
336338
Results: CallResults,
337339
{
338340
self.inner
339-
.execute_func_resumable(ctx, func, params, results)
341+
.execute_func_resumable(ctx, func, params, results, tracing)
340342
}
341343

342344
/// Resumes the given `invocation` given the `params`.
@@ -368,11 +370,12 @@ impl Engine {
368370
invocation: ResumableInvocation,
369371
params: impl CallParams,
370372
results: Results,
373+
tracing: bool,
371374
) -> Result<ResumableCallBase<<Results as CallResults>::Results>, Error>
372375
where
373376
Results: CallResults,
374377
{
375-
self.inner.resume_func(ctx, invocation, params, results)
378+
self.inner.resume_func(ctx, invocation, params, results, tracing)
376379
}
377380

378381
/// Recycles the given [`Stack`] for reuse in the [`Engine`].

crates/wasmi/src/engine/resumable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ impl ResumableInvocation {
212212
mut ctx: impl AsContextMut<Data = T>,
213213
inputs: &[Val],
214214
outputs: &mut [Val],
215+
tracing: bool,
215216
) -> Result<ResumableCall, Error> {
216217
self.engine
217218
.resolve_func_type(self.host_func().ty_dedup(ctx.as_context()), |func_type| {
@@ -225,7 +226,7 @@ impl ResumableInvocation {
225226
})?;
226227
self.engine
227228
.clone()
228-
.resume_func(ctx.as_context_mut(), self, inputs, outputs)
229+
.resume_func(ctx.as_context_mut(), self, inputs, outputs, tracing)
229230
.map(ResumableCall::new)
230231
}
231232
}
@@ -288,6 +289,7 @@ impl<Results> TypedResumableInvocation<Results> {
288289
self,
289290
mut ctx: impl AsContextMut<Data = T>,
290291
inputs: &[Val],
292+
tracing: bool,
291293
) -> Result<TypedResumableCall<Results>, Error>
292294
where
293295
Results: WasmResults,
@@ -303,6 +305,7 @@ impl<Results> TypedResumableInvocation<Results> {
303305
self.invocation,
304306
inputs,
305307
<CallResultsTuple<Results>>::default(),
308+
tracing,
306309
)
307310
.map(TypedResumableCall::new)
308311
}

crates/wasmi/src/func/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ impl Func {
415415
mut ctx: impl AsContextMut<Data = T>,
416416
inputs: &[Val],
417417
outputs: &mut [Val],
418+
tracing: bool,
418419
) -> Result<(), Error> {
419420
self.verify_and_prepare_inputs_outputs(ctx.as_context(), inputs, outputs)?;
420421
// Note: Cloning an [`Engine`] is intentionally a cheap operation.
@@ -423,6 +424,7 @@ impl Func {
423424
self,
424425
inputs,
425426
outputs,
427+
tracing,
426428
)?;
427429
Ok(())
428430
}
@@ -455,14 +457,15 @@ impl Func {
455457
mut ctx: impl AsContextMut<Data = T>,
456458
inputs: &[Val],
457459
outputs: &mut [Val],
460+
tracing: bool,
458461
) -> Result<ResumableCall, Error> {
459462
self.verify_and_prepare_inputs_outputs(ctx.as_context(), inputs, outputs)?;
460463
// Note: Cloning an [`Engine`] is intentionally a cheap operation.
461464
ctx.as_context()
462465
.store
463466
.engine()
464467
.clone()
465-
.execute_func_resumable(ctx.as_context_mut(), self, inputs, outputs)
468+
.execute_func_resumable(ctx.as_context_mut(), self, inputs, outputs, tracing)
466469
.map(ResumableCall::new)
467470
}
468471

0 commit comments

Comments
 (0)