Skip to content

Commit

Permalink
[WIP] Disassembly support in the repl
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Aug 4, 2020
1 parent 36c88eb commit fd063d2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
17 changes: 16 additions & 1 deletion examples/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ fn main() {
#[cfg(target_os = "linux")]
mod example {
use headcrab::{
symbol::RelocatedDwarf,
symbol::{DisassemblySource, RelocatedDwarf},
target::{AttachOptions, LinuxTarget, UnixTarget},
};

struct Context {
remote: Option<LinuxTarget>,
debuginfo: Option<RelocatedDwarf>,
disassembler: DisassemblySource,
}

impl Context {
Expand Down Expand Up @@ -55,6 +56,7 @@ mod example {
let mut context = Context {
remote: None,
debuginfo: None,
disassembler: DisassemblySource::new(),
};

let mut cmds = vec![];
Expand Down Expand Up @@ -231,6 +233,19 @@ mod example {
);
}
}
Some("dis") | Some("disassemble") => {
let ip = context.remote()?.read_regs()?.rip;
let mut code = [0; 64];
unsafe {
context
.remote()?
.read()
.read(&mut code, ip as usize)
.apply()?;
}
let disassembly = context.disassembler.source_snippet(&code, ip, true)?;
println!("{}", disassembly);
}

// Patch the `pause` instruction inside a function called `breakpoint` to be a
// breakpoint. This is useful while we don't have support for setting breakpoints at
Expand Down
4 changes: 4 additions & 0 deletions src/symbol/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ impl DisassemblySource {
&self,
bytes: &[u8],
addr: u64,
show_address: bool,
) -> Result<String, Box<dyn std::error::Error>> {
use std::fmt::Write;

let mut fmt = String::new();

for insn in self.0.disasm_all(&bytes, addr).unwrap().iter() {
if show_address {
write!(fmt, "0x{:016x}: ", insn.address()).unwrap();
}
if let Some(mnemonic) = insn.mnemonic() {
write!(fmt, "{} ", mnemonic).unwrap();
if let Some(op_str) = insn.op_str() {
Expand Down
3 changes: 2 additions & 1 deletion tests/disassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fn disassemble() -> Result<(), Box<dyn std::error::Error>> {
}
dbg!();

let disassembly = headcrab::symbol::DisassemblySource::new().source_snippet(&code, ip)?;
let disassembly =
headcrab::symbol::DisassemblySource::new().source_snippet(&code, ip, false)?;
assert_eq!(
disassembly,
"nop \n\
Expand Down

0 comments on commit fd063d2

Please sign in to comment.