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}, codegen::{Event, EventSink, FunctionMiddleware, InternalEvent},
module::ModuleInfo, 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 { impl FunctionMiddleware for CallTrace {
type Error = String; type Error = String;
@ -13,10 +24,13 @@ impl FunctionMiddleware for CallTrace {
_module_info: &ModuleInfo, _module_info: &ModuleInfo,
sink: &mut EventSink<'a, 'b>, sink: &mut EventSink<'a, 'b>,
) -> Result<(), Self::Error> { ) -> Result<(), Self::Error> {
let counter = self.counter.clone();
match op { match op {
Event::Internal(InternalEvent::FunctionBegin(id)) => sink.push(Event::Internal( Event::Internal(InternalEvent::FunctionBegin(id)) => sink.push(Event::Internal(
InternalEvent::Breakpoint(Box::new(move |_| { InternalEvent::Breakpoint(Box::new(move |_| {
eprintln!("func ({})", id); let idx = counter.fetch_add(1, Ordering::SeqCst);
eprintln!("[{}] func ({})", idx, id);
Ok(()) Ok(())
})), })),
)), )),