Add a BlockTrace middleware.

This commit is contained in:
losfair 2019-10-13 20:51:03 +08:00
parent 5499a69ddc
commit 128b006bf7
2 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,105 @@
use wasmer_runtime_core::{
codegen::{Event, EventSink, FunctionMiddleware, InternalEvent},
module::ModuleInfo,
wasmparser::Operator,
};
pub struct BlockTrace {
func_idx: usize,
evt_idx: usize,
}
impl BlockTrace {
pub fn new() -> BlockTrace {
BlockTrace {
func_idx: std::usize::MAX,
evt_idx: 0,
}
}
}
impl FunctionMiddleware for BlockTrace {
type Error = String;
fn feed_event<'a, 'b: 'a>(
&mut self,
op: Event<'a, 'b>,
_module_info: &ModuleInfo,
sink: &mut EventSink<'a, 'b>,
) -> Result<(), Self::Error> {
match op {
Event::Internal(InternalEvent::FunctionBegin(_)) => {
self.func_idx = self.func_idx.wrapping_add(1);
self.evt_idx = 0;
let func_idx = self.func_idx;
let evt_idx = self.evt_idx;
sink.push(op);
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
move |_| {
eprintln!("[BlockTrace] ({}, {}) -> enter_func", func_idx, evt_idx);
Ok(())
},
))))
}
Event::Wasm(Operator::Call { .. }) => {
let func_idx = self.func_idx;
let evt_idx = self.evt_idx;
sink.push(op);
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
move |_| {
eprintln!("[BlockTrace] ({}, {}) -> leave_call", func_idx, evt_idx);
Ok(())
},
))))
}
Event::Wasm(Operator::Block { .. }) => {
let func_idx = self.func_idx;
let evt_idx = self.evt_idx;
sink.push(op);
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
move |_| {
eprintln!("[BlockTrace] ({}, {}) -> block", func_idx, evt_idx);
Ok(())
},
))))
}
Event::Wasm(Operator::Loop { .. }) => {
let func_idx = self.func_idx;
let evt_idx = self.evt_idx;
sink.push(op);
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
move |_| {
eprintln!("[BlockTrace] ({}, {}) -> loop", func_idx, evt_idx);
Ok(())
},
))))
}
Event::Wasm(Operator::If { .. }) => {
let func_idx = self.func_idx;
let evt_idx = self.evt_idx;
sink.push(op);
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
move |_| {
eprintln!("[BlockTrace] ({}, {}) -> if", func_idx, evt_idx);
Ok(())
},
))))
}
Event::Wasm(Operator::Else { .. }) => {
let func_idx = self.func_idx;
let evt_idx = self.evt_idx;
sink.push(op);
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
move |_| {
eprintln!("[BlockTrace] ({}, {}) -> else", func_idx, evt_idx);
Ok(())
},
))))
}
_ => {
sink.push(op);
}
}
self.evt_idx += 1;
Ok(())
}
}

View File

@ -10,5 +10,6 @@
#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
#![doc(html_logo_url = "https://avatars3.githubusercontent.com/u/44205449?s=200&v=4")]
pub mod block_trace;
pub mod call_trace;
pub mod metering;