depth or breadth first?

This commit is contained in:
🪞👃🪞 2025-02-23 22:37:34 +02:00
parent b629beb5fc
commit 8a404c3bd5
2 changed files with 28 additions and 28 deletions

View file

@ -14,36 +14,27 @@ impl Module {
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 source = self.pe.offset_to_rva(Offset((position + self.code_start) as u32))?.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 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)

View file

@ -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 {