Skip to content

Commit

Permalink
Implement continuous unassemble
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMisiak committed Jan 17, 2024
1 parent fcb6bdb commit 15cc5ac
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod grammar {
Evaluate(#[rust_sitter::leaf(text = "?")] (), Box<EvalExpr>),
ListNearest(#[rust_sitter::leaf(text = "ln")] (), Box<EvalExpr>),
Unassemble(#[rust_sitter::leaf(text = "u")] (), Box<EvalExpr>),
UnassembleRip(#[rust_sitter::leaf(text = "u")] ()),
UnassembleContinue(#[rust_sitter::leaf(text = "u")] ()),
Quit(#[rust_sitter::leaf(text = "q")] ()),
}

Expand Down
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fn main_debugger_loop(process: HANDLE) {
},
}

let mut next_unassemble_address = ctx.context.Rip;
let mut continue_execution = false;

while !continue_execution {
Expand Down Expand Up @@ -218,14 +219,11 @@ fn main_debugger_loop(process: HANDLE) {
}
CommandExpr::Unassemble(_, expr) => {
if let Some(addr) = eval_expr(expr) {
unassemble::unassemble(mem_source.as_ref(), addr, 16);
println!();
next_unassemble_address = unassemble::unassemble(mem_source.as_ref(), addr, 16);
}
}
CommandExpr::UnassembleRip(_) => {
let addr = ctx.context.Rip;
unassemble::unassemble(mem_source.as_ref(), addr, 16);
println!();
CommandExpr::UnassembleContinue(_) => {
next_unassemble_address = unassemble::unassemble(mem_source.as_ref(), next_unassemble_address, 16);
}
CommandExpr::SetBreakpoint(_, expr) => {
if let Some(addr) = eval_expr(expr) {
Expand Down
5 changes: 4 additions & 1 deletion src/unassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use iced_x86::{Decoder, DecoderOptions, Formatter, Instruction, MasmFormatter};

use crate::memory::MemorySource;

pub fn unassemble(memory_source: &dyn MemorySource, va: u64, lines: usize) {
pub fn unassemble(memory_source: &dyn MemorySource, va: u64, lines: usize) -> u64 {

// We'll never need more than lines * 15
let bytes = memory_source.read_raw_memory(va, lines * 15);
Expand Down Expand Up @@ -40,6 +40,7 @@ pub fn unassemble(memory_source: &dyn MemorySource, va: u64, lines: usize) {
// let instructions: Vec<_> = decoder.into_iter().collect();
// but can_decode()/decode_out() is a little faster:
let mut instruction_count = 0;
let mut last_rip = 0;
while decoder.can_decode() && instruction_count < lines {
// There's also a decode() method that returns an instruction but that also
// means it copies an instruction (40 bytes):
Expand All @@ -64,5 +65,7 @@ pub fn unassemble(memory_source: &dyn MemorySource, va: u64, lines: usize) {
}
println!(" {}", output);
instruction_count += 1;
last_rip = instruction.ip() + instr_bytes.len() as u64;
}
last_rip
}

0 comments on commit 15cc5ac

Please sign in to comment.