From 8a404c3bd54adc19c8ffda0ae1bbe84aeaf24bc8 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 23 Feb 2025 22:37:34 +0200 Subject: [PATCH] depth or breadth first? --- crates/vestal/src/call_sites.rs | 35 ++++++++++++--------------------- crates/vestal/src/show.rs | 21 ++++++++++++++------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/crates/vestal/src/call_sites.rs b/crates/vestal/src/call_sites.rs index ea63a83..6bbf6a3 100644 --- a/crates/vestal/src/call_sites.rs +++ b/crates/vestal/src/call_sites.rs @@ -13,37 +13,28 @@ impl Module { let instruction = decoder.decode(); let opcodes = &self.code[position..position+instruction.len()]; if CallSite::matches(&instruction) && !CallSite::skip(opcodes) { - let offset = (position + self.code_start) as u32; - let source = self.pe.offset_to_rva(Offset(offset))?.0; - let target = CallSite::target(source, opcodes).unwrap_or(0); - let imports = self.imports.read().unwrap(); - let label = imports.get(&target) - .map(|(module, method)|format!("{module}::{method}")) - .unwrap_or_else(||format!("{RED}unresolved{RESET}")); - println!(" (call {} {} {} {DIM}{:20}{RESET} {} {BOLD}{}{RESET})", - &self.name, - fmt_num(offset as usize), - fmt_num(source as usize), - fmt_bytes(opcodes), - fmt_num(target as usize), - label); - if !targets.contains_key(&target) { - targets.insert(target, vec![]); - } - let import = imports.get(&target).clone(); - targets.get_mut(&target).unwrap().push(Arc::new(CallSite { + let offset = (position + self.code_start) as u32; + let source = self.pe.offset_to_rva(Offset((position + self.code_start) as u32))?.0; + let target = CallSite::target(source, opcodes).unwrap_or(0); + let import = self.imports.read().unwrap().get(&target).cloned(); + let call_site = Arc::new(CallSite { caller: self.clone(), source, offset, - length: opcodes.len(), target, - method: import.map(|x|x.1.clone()), + length: opcodes.len(), + method: import.clone().map(|x|x.1), module: if let Some(import) = import { self.dependencies.read().unwrap().get(&import.0).cloned() } else { None } - })); + }); + call_site.show(Some(opcodes)); + if !targets.contains_key(&call_site.target) { + targets.insert(call_site.target, vec![]); + } + targets.get_mut(&call_site.target).unwrap().push(call_site); } } Ok(self) diff --git a/crates/vestal/src/show.rs b/crates/vestal/src/show.rs index d16e50a..8eefa75 100644 --- a/crates/vestal/src/show.rs +++ b/crates/vestal/src/show.rs @@ -130,12 +130,21 @@ impl Module { } impl CallSite { - pub fn show (&self) { - let caller = self.caller.name.as_ref(); - let module = self.module.as_ref(); - let method = self.method.as_ref(); - let style = GREEN; - println!("╰--------> {caller} -> {style}{module:?}::{method:?}{RESET}"); + pub fn show (&self, opcodes: Option<&[u8]>) { + let label = self.caller.imports.read().unwrap().get(&self.target) + .map(|(module, method)|format!("{module}::{method}")) + .unwrap_or_else(||format!("{RED}unresolved{RESET}")); + println!(" ╰-> (call {} {} {} {DIM}{:20}{RESET} {} {BOLD}{}{RESET})", + &self.caller.name, + fmt_num(self.offset as usize), + fmt_num(self.source as usize), + fmt_bytes(opcodes.unwrap_or(&[])), + fmt_num(self.target as usize), + label); + //let caller = self.caller.name.as_ref(); + //let module = self.module.as_ref(); + //let method = self.method.as_ref(); + //println!("╰--------> {caller} -> {label}{RESET}"); //module.map(|x|x.as_ref()).unwrap_or(&""), //method.map(|x|x.as_ref()).unwrap_or(&"")); //} else {