Update call_trace middleware to include a counter.

This commit is contained in:
losfair 2019-10-11 21:04:09 +08:00
parent 8ee4b7f7b0
commit 2e1fb7abca

View File

@ -2,8 +2,19 @@ use wasmer_runtime_core::{
codegen::{Event, EventSink, FunctionMiddleware, InternalEvent},
module::ModuleInfo,
};
use std::sync::{Arc, atomic::{Ordering, AtomicU32}};
pub struct CallTrace;
pub struct CallTrace {
counter: Arc<AtomicU32>,
}
impl CallTrace {
pub fn new() -> CallTrace {
CallTrace {
counter: Arc::new(AtomicU32::new(0)),
}
}
}
impl FunctionMiddleware for CallTrace {
type Error = String;
@ -13,10 +24,13 @@ impl FunctionMiddleware for CallTrace {
_module_info: &ModuleInfo,
sink: &mut EventSink<'a, 'b>,
) -> Result<(), Self::Error> {
let counter = self.counter.clone();
match op {
Event::Internal(InternalEvent::FunctionBegin(id)) => sink.push(Event::Internal(
InternalEvent::Breakpoint(Box::new(move |_| {
eprintln!("func ({})", id);
let idx = counter.fetch_add(1, Ordering::SeqCst);
eprintln!("[{}] func ({})", idx, id);
Ok(())
})),
)),