Skip to content

Commit

Permalink
Support index trace for functions (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored and atenjin committed Jun 2, 2021
1 parent 299c940 commit 8a7e0ca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
28 changes: 13 additions & 15 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ use parity_wasm::elements::Local;
pub struct FuncRef {
/// Function Instance
instance: Rc<FuncInstance>,
/// Function name
name: Option<String>,
/// Function name and index for debug
info: Option<(usize, String)>,
}

impl FuncRef {
/// Return the name of function
pub fn name(&self) -> Option<&String> {
self.name.as_ref()
/// Get function info
pub fn info(&self) -> Option<&(usize, String)> {
self.info.as_ref()
}

/// Mark the name of the function
pub fn set_name(mut self, name: Option<&String>) -> Self {
if let Some(name) = name {
self.name = Some(name.clone());
}
/// Set function info
pub fn set_info(mut self, index: usize, name: String) -> Self {
self.info = Some((index, name));
self
}
}
Expand Down Expand Up @@ -107,8 +105,8 @@ impl FuncInstance {
host_func_index,
};
FuncRef {
name: None,
instance: Rc::new(FuncInstance(func)),
info: None,
}
}

Expand Down Expand Up @@ -139,8 +137,8 @@ impl FuncInstance {
body: Rc::new(body),
};
FuncRef {
name: None,
instance: Rc::new(FuncInstance(func)),
info: None,
}
}

Expand Down Expand Up @@ -172,14 +170,14 @@ impl FuncInstance {
let res = interpreter.start_execution(externals);
if res.is_err() {
let mut stack = interpreter
.trace_stack()
.trace()
.iter()
.filter_map(|n| n.as_ref())
.map(|n| rustc_demangle::demangle(n).to_string())
.map(|n| format!("{:#}[{}]", rustc_demangle::demangle(&n.1), n.0))
.collect::<Vec<_>>();
stack.reverse();

// Embed this info into the trap
println!("{:#?}", &stack);
res.map_err(|e| e.set_wasm_trace(stack))
} else {
res
Expand Down
4 changes: 3 additions & 1 deletion src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ impl ModuleInstance {
let mut func_instance =
FuncInstance::alloc_internal(Rc::downgrade(&instance.0), signature, func_body);
if has_name_section {
func_instance = func_instance.set_name(function_names.get(index as u32));
if let Some(name) = function_names.get(index as u32) {
func_instance = func_instance.set_info(index, name.to_string());
}
}
instance.push_func(func_instance);
}
Expand Down
19 changes: 8 additions & 11 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl Interpreter {
}

/// Get the stack functions
pub fn trace_stack(&self) -> Vec<Option<&String>> {
pub fn trace(&self) -> Vec<&(usize, String)> {
self.call_stack.trace()
}

Expand Down Expand Up @@ -1307,9 +1307,8 @@ impl FunctionContext {
self.memory.as_ref()
}

/// Get the function name
pub fn name(&self) -> Option<&String> {
self.function.name()
pub fn info(&self) -> Option<&(usize, String)> {
self.function.info()
}
}

Expand Down Expand Up @@ -1461,13 +1460,6 @@ struct CallStack {
limit: usize,
}

impl CallStack {
/// Get the functions of current the stack
pub fn trace(&self) -> Vec<Option<&String>> {
self.buf.iter().map(|f| f.name()).collect::<Vec<_>>()
}
}

impl CallStack {
fn push(&mut self, ctx: FunctionContext) {
self.buf.push(ctx);
Expand All @@ -1484,6 +1476,11 @@ impl CallStack {
fn is_full(&self) -> bool {
self.buf.len() + 1 >= self.limit
}

/// Get the functions of current the stack
pub fn trace(&self) -> Vec<&(usize, String)> {
self.buf.iter().filter_map(|f| f.info()).collect::<Vec<_>>()
}
}

/// Used to recycle stacks instead of allocating them repeatedly.
Expand Down

0 comments on commit 8a7e0ca

Please sign in to comment.