ft: add dump flag

This commit is contained in:
2025-05-28 15:59:37 +09:00
parent 7e8fdeba54
commit 4c63b7a21a
3 changed files with 24 additions and 10 deletions

View File

@@ -1,11 +1,11 @@
# minix-8086-rs # 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: 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 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 ## Usage
@@ -21,7 +21,7 @@ cargo run -- --help
Run with debug output: 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: CLI Options:
@@ -38,6 +38,7 @@ Commands:
Options: Options:
-p, --path <PATH> Path of the binary -p, --path <PATH> Path of the binary
-d, --dump Dump progress of disassembly, in case of encountering an error
-h, --help Print help -h, --help Print help
-V, --version Print version -V, --version Print version
``` ```

View File

@@ -43,7 +43,7 @@ impl fmt::Display for DisasmError {
DisasmError::IoError(msg) => write!(f, "{}", msg), DisasmError::IoError(msg) => write!(f, "{}", msg),
DisasmError::OpcodeUndefined(opcode) => write!( DisasmError::OpcodeUndefined(opcode) => write!(
f, 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 opcode
), ),
DisasmError::IllegalGroupMnemonic(group, mnemonic) => write!( DisasmError::IllegalGroupMnemonic(group, mnemonic) => write!(
@@ -106,7 +106,7 @@ impl Disassembler {
/// Start the disassmble and allow for some error handling wrapped around /// Start the disassmble and allow for some error handling wrapped around
/// the actual decoding function. /// the actual decoding function.
pub fn disassemble(&mut self) -> Result<Vec<Instruction>, DisasmError> { pub fn disassemble(&mut self, dump: bool) -> Result<Vec<Instruction>, DisasmError> {
let is_ok = self.decode_instructions(); let is_ok = self.decode_instructions();
// a.out pads the text section to byte align, so the fasely interpreted // a.out pads the text section to byte align, so the fasely interpreted
@@ -126,7 +126,16 @@ impl Disassembler {
Ok(instructions) 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) Err(e)
} }
}, },
@@ -485,7 +494,7 @@ impl Disassembler {
// additional raw bytes will be pushed by parse functions // additional raw bytes will be pushed by parse functions
self.instruction.raw.push(opcode); 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 { self.instruction.opcode = match opcode {
0x00 => modrm_8b_register!(self, ADD_FromReg), 0x00 => modrm_8b_register!(self, ADD_FromReg),
0x01 => modrm_16b_register!(self, ADD_FromReg), 0x01 => modrm_16b_register!(self, ADD_FromReg),

View File

@@ -28,6 +28,10 @@ struct Args {
/// Path of the binary /// Path of the binary
#[arg(short, long, global = true)] #[arg(short, long, global = true)]
path: Option<String>, path: Option<String>,
/// Dump progress of disassembly, in case of encountering an error.
#[arg(short, long, global = true, action)]
dump: bool,
} }
fn main() { fn main() {
@@ -39,7 +43,7 @@ fn main() {
match args.command { match args.command {
Command::Disasm => { Command::Disasm => {
let mut disasm = Disassembler::new(&args); let mut disasm = Disassembler::new(&args);
let instructions = disasm.disassemble(); let instructions = disasm.disassemble(args.dump);
match instructions { match instructions {
Ok(instrs) => instrs.iter().for_each(|i| println!("{i}")), Ok(instrs) => instrs.iter().for_each(|i| println!("{i}")),
_ => {} _ => {}