Skip to content

Commit 138b360

Browse files
committed
feat: add env var-based trace options: looks simpler than adding cli params
we need to pass cli params through many layers/calls: done in another branch and too big patch; also we can just keep tracer local, instead of passing it everywhere
1 parent cfb9ff7 commit 138b360

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

crates/tracer/src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use std::path::{Path, PathBuf};
22
use std::println;
33

4-
pub fn add(left: u64, right: u64) -> u64 {
5-
left + right
6-
}
7-
84
// TODO
95
#[derive(Debug, Clone)]
106
struct DebugInfo {
@@ -25,20 +21,30 @@ impl DebugInfo {
2521

2622
#[derive(Debug, Clone)]
2723
pub struct WasmTracer {
24+
pub tracing: bool,
2825
debug_info: DebugInfo,
26+
info: Vec<String>,
27+
index: usize,
2928
// TODO: tracer: runtime_tracing.Tracer,
3029
// etc
3130
}
3231

3332
// just to make it build so we can branch-out
3433

3534
impl WasmTracer {
36-
pub fn new(wasm_exe_path: &Path) -> Self {
35+
pub fn new(tracing: bool, wasm_exe_path: &Path) -> Self {
3736
WasmTracer {
37+
tracing,
3838
debug_info: DebugInfo::new(wasm_exe_path),
39+
info: vec![],
40+
index: 0,
3941
}
4042
}
4143

44+
pub fn no_tracing() -> Self {
45+
Self::new(false, &Path::new(""))
46+
}
47+
4248
pub fn load_local_variables(&mut self, address: usize) { // -> ???
4349
println!("load_local_variables {address}");
4450
// e.g. here we might call something like
@@ -49,7 +55,10 @@ impl WasmTracer {
4955
if !cached {
5056
// TODO etc
5157
// load debuginfo etc
58+
self.info.push(format!("{}", self.index));
59+
self.index += 1;
5260
}
61+
println!("{:?}", self.info);
5362
}
5463
}
5564

@@ -68,7 +77,5 @@ mod tests {
6877

6978
#[test]
7079
fn it_works() {
71-
let result = add(2, 2);
72-
assert_eq!(result, 4);
7380
}
7481
}

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,29 @@ impl<'engine> Executor<'engine> {
129129
#[inline(always)]
130130
fn execute<T>(mut self, store: &mut Store<T>) -> Result<(), Error> {
131131
use Instruction as Instr;
132+
let tracing = std::env::var("CODETRACER_WASMI_TRACING").unwrap_or(std::string::String::from("")) == "1";
133+
let env_wasm_path = std::env::var("CODETRACER_WASM_PATH").unwrap_or(std::string::String::from(""));
134+
let wasm_path = std::path::Path::new(&env_wasm_path);
135+
let mut tracer = WasmTracer::new(tracing, &wasm_path);
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);
137+
if tracer.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+
tracer.load_local_variables(address);
142146

143-
// load debuginfo from gimli <- for this ip;
144-
// -> find out current scope and vars in it:
145-
// some kind of mapping between them and locations
146-
// -> load them with some general(for us) mechanism from wasmi's memory(?)
147-
// e.g. load_expression(expr, location, ..) -> ValueRecord
148-
// (to be iterated on, just an idea)
149-
match *self.ip.get() {
147+
// load debuginfo from gimli <- for this ip;
148+
// -> find out current scope and vars in it:
149+
// some kind of mapping between them and locations
150+
// -> load them with some general(for us) mechanism from wasmi's memory(?)
151+
// e.g. load_expression(expr, location, ..) -> ValueRecord
152+
// (to be iterated on, just an idea)
153+
}
154+
match *self.ip.get() {
150155
Instr::Trap { trap_code } => self.execute_trap(trap_code)?,
151156
Instr::ConsumeFuel { block_fuel } => {
152157
self.execute_consume_fuel(&mut store.inner, block_fuel)?

0 commit comments

Comments
 (0)