mirror of
https://github.com/transistorfet/moa.git
synced 2024-06-04 14:29:41 +00:00
cbf91309d9
Previously it would show a dump of whatever device was next scheduled to step, if it was Debuggable, but now each debuggable device has a flag for whether that device is being debugging, and a system flag to disable it entirely. When the system flag is set, it will try to enable debugging on the device labelled "cpu". I need to also add a way of setting a breakpoint on a named device, which will enable debugging of that device
177 lines
6.6 KiB
Rust
177 lines
6.6 KiB
Rust
|
|
use std::io::Write;
|
|
|
|
use crate::error::Error;
|
|
use crate::system::System;
|
|
use crate::devices::{Address, Addressable, Debuggable, TransmutableBox};
|
|
|
|
|
|
pub struct Debugger {
|
|
last_command: Option<String>,
|
|
repeat: u32,
|
|
trace_only: bool,
|
|
}
|
|
|
|
|
|
impl Debugger {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
last_command: None,
|
|
repeat: 0,
|
|
trace_only: false,
|
|
}
|
|
}
|
|
|
|
pub fn breakpoint_occurred(&mut self) {
|
|
self.trace_only = false;
|
|
}
|
|
|
|
pub fn run_debugger(&mut self, system: &System, target: TransmutableBox) -> Result<(), Error> {
|
|
let mut target = target.borrow_mut();
|
|
let debug_obj = target.as_debuggable().unwrap();
|
|
println!("@ {} ns", system.clock);
|
|
debug_obj.print_current_step(system)?;
|
|
|
|
if self.trace_only {
|
|
return Ok(());
|
|
}
|
|
|
|
if self.repeat > 0 {
|
|
self.repeat -= 1;
|
|
let last_command = self.last_command.clone().unwrap();
|
|
let args: Vec<&str> = vec![&last_command];
|
|
self.run_debugger_command(system, debug_obj, &args)?;
|
|
return Ok(());
|
|
}
|
|
|
|
loop {
|
|
let mut buffer = String::new();
|
|
std::io::stdout().write_all(b"> ").unwrap();
|
|
std::io::stdin().read_line(&mut buffer).unwrap();
|
|
let args: Vec<&str> = buffer.split_whitespace().collect();
|
|
match self.run_debugger_command(system, debug_obj, &args) {
|
|
Ok(true) => return Ok(()),
|
|
Ok(false) => { },
|
|
Err(err) => {
|
|
println!("Error: {}", err.msg);
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn run_debugger_command(&mut self, system: &System, debug_obj: &mut dyn Debuggable, args: &[&str]) -> Result<bool, Error> {
|
|
if args.len() == 0 {
|
|
// The Default Command
|
|
return Ok(true);
|
|
}
|
|
|
|
match args[0] {
|
|
"b" | "break" | "breakpoint" => {
|
|
if args.len() != 2 {
|
|
println!("Usage: breakpoint <addr>");
|
|
} else {
|
|
let addr = u32::from_str_radix(args[1], 16).map_err(|_| Error::new("Unable to parse breakpoint address"))?;
|
|
debug_obj.add_breakpoint(addr as Address);
|
|
println!("Breakpoint set for {:08x}", addr);
|
|
}
|
|
},
|
|
"r" | "remove" => {
|
|
if args.len() != 2 {
|
|
println!("Usage: remove <addr>");
|
|
} else {
|
|
let addr = u32::from_str_radix(args[1], 16).map_err(|_| Error::new("Unable to parse breakpoint address"))?;
|
|
debug_obj.remove_breakpoint(addr as Address);
|
|
println!("Breakpoint removed for {:08x}", addr);
|
|
}
|
|
},
|
|
"d" | "dump" => {
|
|
if args.len() > 1 {
|
|
let addr = u32::from_str_radix(args[1], 16).map_err(|_| Error::new("Unable to parse address"))?;
|
|
let len = if args.len() > 2 { u32::from_str_radix(args[2], 16).map_err(|_| Error::new("Unable to parse length"))? } else { 0x20 };
|
|
system.get_bus().dump_memory(addr as Address, len as Address);
|
|
} else {
|
|
//self.port.dump_memory(self.state.ssp as Address, 0x40 as Address);
|
|
}
|
|
},
|
|
"i" | "inspect" => {
|
|
if args.len() < 2 {
|
|
println!("Usage: inspect <device_name> [<device specific arguments>]");
|
|
} else {
|
|
let device = system.devices.get(args[1]).ok_or_else(|| Error::new(&format!("No device named {}", args[1])))?;
|
|
let subargs = if args.len() > 2 { &args[2..] } else { &[""] };
|
|
device.borrow_mut().as_inspectable()
|
|
.ok_or_else(|| Error::new("That device is not inspectable"))?
|
|
.inspect(system, subargs)?;
|
|
}
|
|
},
|
|
"dis" | "disassemble" => {
|
|
let addr = if args.len() > 1 {
|
|
Address::from_str_radix(args[1], 16).map_err(|_| Error::new("Unable to parse address"))?
|
|
} else {
|
|
0
|
|
};
|
|
|
|
let count = if args.len() > 2 {
|
|
usize::from_str_radix(args[2], 16).map_err(|_| Error::new("Unable to parse address"))?
|
|
} else {
|
|
0x1000
|
|
};
|
|
|
|
debug_obj.print_disassembly(addr, count);
|
|
},
|
|
"c" | "continue" => {
|
|
self.check_repeat_arg(args)?;
|
|
system.disable_debugging();
|
|
return Ok(true);
|
|
},
|
|
"s" | "step" => {
|
|
self.check_repeat_arg(args)?;
|
|
return Ok(true);
|
|
},
|
|
"t" | "trace" => {
|
|
self.trace_only = true;
|
|
return Ok(true);
|
|
}
|
|
"setb" | "setw" | "setl" => {
|
|
if args.len() != 3 {
|
|
println!("Usage: set[b|w|l] <addr> <data>");
|
|
} else {
|
|
let addr = u64::from_str_radix(args[1], 16).map_err(|_| Error::new("Unable to parse set address"))?;
|
|
let data = u32::from_str_radix(args[2], 16).map_err(|_| Error::new("Unable to parse data"))?;
|
|
match args[0] {
|
|
"setb" => system.get_bus().write_u8(addr, data as u8)?,
|
|
"setw" => system.get_bus().write_beu16(addr, data as u16)?,
|
|
"setl" => system.get_bus().write_beu32(addr, data)?,
|
|
_ => panic!("Unimplemented: {:?}", args[0]),
|
|
}
|
|
}
|
|
},
|
|
//"ds" | "stack" | "dumpstack" => {
|
|
// println!("Stack:");
|
|
// for addr in &self.debugger.stack_tracer.calls {
|
|
// println!(" {:08x}", self.port.read_beu32(*addr as Address)?);
|
|
// }
|
|
//},
|
|
//"so" | "stepout" => {
|
|
// self.debugger.step_until_return = Some(self.debugger.stack_tracer.calls.len() - 1);
|
|
// return Ok(true);
|
|
//},
|
|
_ => {
|
|
if debug_obj.execute_command(system, args)? {
|
|
println!("Error: unknown command {}", args[0]);
|
|
}
|
|
},
|
|
}
|
|
Ok(false)
|
|
}
|
|
|
|
fn check_repeat_arg(&mut self, args: &[&str]) -> Result<(), Error> {
|
|
if args.len() > 1 {
|
|
self.repeat = u32::from_str_radix(args[1], 10).map_err(|_| Error::new("Unable to parse repeat number"))?;
|
|
self.last_command = Some(args[0].to_string());
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|