From 4c63b7a21a6fae7f7e30689a4644fb8c6a19b28b Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Wed, 28 May 2025 15:59:37 +0900 Subject: [PATCH] ft: add dump flag --- README.md | 11 ++++++----- src/disasm.rs | 17 +++++++++++++---- src/main.rs | 6 +++++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8f2623b..7f9d8f3 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # minix-8086-rs -minix-8086-rs is a Rust-based toolchain for analyzing and interpreting 16-bit 8086 binaries, made with the intention of interpreting binaries compiled for MINIX. +minix-8086-rs is a Rust-based toolchain for analyzing and interpreting 16-bit 8086 binaries, made with the intention of interpreting binaries compiled for MINIX 1.x. It includes: -- 📦 a.out Parser: Parses legacy MINIX executables. +- 📦 a.out Parser: Parses legacy MINIX 1.x executables. - 🛠 8086 Disassembler: Parses 16-bit instructions into an IR and prints them in a `objdump(1)`-style fashion. -- 💻 8086 Interpreter: Interprets the 8086 instructions, i.e., the MINIX binary. +- 💻 8086 Interpreter: Interprets the 8086 instructions, i.e., the MINIX 1.x binary. ## Usage @@ -21,7 +21,7 @@ cargo run -- --help Run with debug output: ``` -RUST_LOG=debug cargo run -- interpret -p ./a.out +RUST_LOG=debug cargo run -- interpret -p ./a.out 2>&1 | less ``` CLI Options: @@ -38,8 +38,9 @@ Commands: Options: -p, --path Path of the binary + -d, --dump Dump progress of disassembly, in case of encountering an error -h, --help Print help - -V, --version Print version + -V, --version Print version ``` ## Status diff --git a/src/disasm.rs b/src/disasm.rs index 358fb39..bca0073 100644 --- a/src/disasm.rs +++ b/src/disasm.rs @@ -43,7 +43,7 @@ impl fmt::Display for DisasmError { DisasmError::IoError(msg) => write!(f, "{}", msg), DisasmError::OpcodeUndefined(opcode) => write!( f, - "Error (Undefined Opcode). '{:#04x} is considered undefined by the Spec", + "Error (Undefined Opcode). '{:#04x} is considered undefined by the Spec for 8086.\nMaybe you are trying to interpret a x86 binary?\nAborting to stop misinterpretation of following instructions.", opcode ), DisasmError::IllegalGroupMnemonic(group, mnemonic) => write!( @@ -106,7 +106,7 @@ impl Disassembler { /// Start the disassmble and allow for some error handling wrapped around /// the actual decoding function. - pub fn disassemble(&mut self) -> Result, DisasmError> { + pub fn disassemble(&mut self, dump: bool) -> Result, DisasmError> { let is_ok = self.decode_instructions(); // a.out pads the text section to byte align, so the fasely interpreted @@ -126,7 +126,16 @@ impl Disassembler { Ok(instructions) } _ => { - println!("Encountered error during disassembly: {e}"); + if dump { + self.instructions.iter().for_each(|i| println!("{i}")); + println!( + "Encountered error during disassembly, but this is the process so far...\n{e}\nRun with RUST_LOG=debug for furhter information." + ); + } else { + println!( + "Encountered error during disassembly.\nRun with --dump to get sucessfully parsed instructions.\n{e}" + ); + } Err(e) } }, @@ -485,7 +494,7 @@ impl Disassembler { // additional raw bytes will be pushed by parse functions self.instruction.raw.push(opcode); - log::debug!("Parsing next opcode with opcode: {opcode:#04}"); + log::debug!("Parsing next opcode with opcode: {opcode:#04x}"); self.instruction.opcode = match opcode { 0x00 => modrm_8b_register!(self, ADD_FromReg), 0x01 => modrm_16b_register!(self, ADD_FromReg), diff --git a/src/main.rs b/src/main.rs index 02d0933..b1fe78e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,10 @@ struct Args { /// Path of the binary #[arg(short, long, global = true)] path: Option, + + /// Dump progress of disassembly, in case of encountering an error. + #[arg(short, long, global = true, action)] + dump: bool, } fn main() { @@ -39,7 +43,7 @@ fn main() { match args.command { Command::Disasm => { let mut disasm = Disassembler::new(&args); - let instructions = disasm.disassemble(); + let instructions = disasm.disassemble(args.dump); match instructions { Ok(instrs) => instrs.iter().for_each(|i| println!("{i}")), _ => {}