2019-06-25 19:31:45 +01:00
|
|
|
use rustyline::error::ReadlineError;
|
|
|
|
use rustyline::Editor;
|
|
|
|
|
|
|
|
use colored::*;
|
|
|
|
|
|
|
|
use hexdump;
|
|
|
|
|
|
|
|
use super::arm7tdmi::cpu;
|
|
|
|
use super::sysbus::SysBus;
|
|
|
|
|
|
|
|
use crate::disass::Disassembler;
|
|
|
|
|
|
|
|
mod parser;
|
2019-06-26 10:05:53 +01:00
|
|
|
use parser::{parse_expr, Expr, Value};
|
2019-06-25 19:31:45 +01:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum DebuggerError {
|
|
|
|
ParsingError(String),
|
|
|
|
CpuError(cpu::CpuError),
|
|
|
|
InvalidCommand(String),
|
2019-06-26 10:05:53 +01:00
|
|
|
InvalidArgument(String),
|
|
|
|
InvalidCommandFormat(String),
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<cpu::CpuError> for DebuggerError {
|
|
|
|
fn from(e: cpu::CpuError) -> DebuggerError {
|
|
|
|
DebuggerError::CpuError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DebuggerResult<T> = Result<T, DebuggerError>;
|
|
|
|
|
2019-06-26 22:41:54 +01:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2019-06-26 10:05:53 +01:00
|
|
|
pub enum Command {
|
2019-06-25 19:31:45 +01:00
|
|
|
Info,
|
|
|
|
SingleStep,
|
|
|
|
Continue,
|
|
|
|
HexDump(u32, usize),
|
2019-06-26 10:05:53 +01:00
|
|
|
Disass(u32, usize),
|
2019-06-25 19:31:45 +01:00
|
|
|
AddBreakpoint(u32),
|
|
|
|
ListBreakpoints,
|
|
|
|
Reset,
|
|
|
|
Quit,
|
|
|
|
}
|
|
|
|
|
2019-06-26 10:05:53 +01:00
|
|
|
impl Command {
|
|
|
|
fn run(&self, debugger: &mut Debugger) {
|
|
|
|
use Command::*;
|
|
|
|
match *self {
|
|
|
|
Info => println!("{}", debugger.cpu),
|
2019-06-25 19:31:45 +01:00
|
|
|
SingleStep => {
|
2019-06-26 10:05:53 +01:00
|
|
|
match debugger.cpu.step(&mut debugger.sysbus) {
|
2019-06-25 19:31:45 +01:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
println!("{}: {}", "cpu encountered an error".red(), e);
|
2019-06-26 10:05:53 +01:00
|
|
|
println!("cpu: {:#x?}", debugger.cpu);
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
|
|
|
};
|
2019-06-26 10:05:53 +01:00
|
|
|
if debugger.is_breakpoint_reached() {
|
|
|
|
println!("breakpoint 0x{:08x} reached!", debugger.cpu.pc)
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Continue => loop {
|
2019-06-26 10:05:53 +01:00
|
|
|
match debugger.cpu.step(&mut debugger.sysbus) {
|
2019-06-25 19:31:45 +01:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
println!("{}: {}", "cpu encountered an error".red(), e);
|
2019-06-26 10:05:53 +01:00
|
|
|
println!("cpu: {:#x?}", debugger.cpu);
|
2019-06-25 19:31:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2019-06-26 10:05:53 +01:00
|
|
|
if debugger.is_breakpoint_reached() {
|
|
|
|
println!("breakpoint 0x{:08x} reached!", debugger.cpu.pc)
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
HexDump(addr, nbytes) => {
|
2019-06-26 10:05:53 +01:00
|
|
|
let bytes = match debugger.sysbus.get_bytes(addr, nbytes) {
|
2019-06-25 19:31:45 +01:00
|
|
|
Some(bytes) => bytes,
|
|
|
|
None => {
|
|
|
|
println!("requested content out of bounds");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
hexdump::hexdump(bytes);
|
|
|
|
}
|
2019-06-26 10:05:53 +01:00
|
|
|
Disass(addr, n) => {
|
|
|
|
let bytes = match debugger.sysbus.get_bytes(addr, 4 * n) {
|
2019-06-25 19:31:45 +01:00
|
|
|
Some(bytes) => bytes,
|
|
|
|
None => {
|
|
|
|
println!("requested content out of bounds");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let disass = Disassembler::new(addr, bytes);
|
|
|
|
for line in disass {
|
|
|
|
println!("{}", line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Quit => {
|
|
|
|
print!("Quitting!");
|
2019-06-26 10:05:53 +01:00
|
|
|
debugger.stop();
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
|
|
|
AddBreakpoint(addr) => {
|
2019-06-26 10:05:53 +01:00
|
|
|
if !debugger.breakpoints.contains(&addr) {
|
|
|
|
let new_index = debugger.breakpoints.len();
|
|
|
|
debugger.breakpoints.push(addr);
|
2019-06-25 19:31:45 +01:00
|
|
|
println!("added breakpoint [{}] 0x{:08x}", new_index, addr);
|
|
|
|
} else {
|
|
|
|
println!("breakpoint already exists!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ListBreakpoints => {
|
|
|
|
println!("breakpoint list:");
|
2019-06-26 10:05:53 +01:00
|
|
|
for (i, b) in debugger.breakpoints.iter().enumerate() {
|
2019-06-25 19:31:45 +01:00
|
|
|
println!("[{}] 0x{:08x}", i, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Reset => {
|
|
|
|
println!("resetting cpu...");
|
2019-06-26 10:05:53 +01:00
|
|
|
debugger.cpu.reset();
|
2019-06-25 19:31:45 +01:00
|
|
|
println!("cpu is restarted!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Debugger {
|
|
|
|
cpu: cpu::Core,
|
|
|
|
sysbus: SysBus,
|
|
|
|
running: bool,
|
|
|
|
breakpoints: Vec<u32>,
|
2019-06-26 22:41:54 +01:00
|
|
|
previous_command: Option<Command>,
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Debugger {
|
|
|
|
pub fn new(cpu: cpu::Core, sysbus: SysBus) -> Debugger {
|
|
|
|
Debugger {
|
|
|
|
cpu: cpu,
|
|
|
|
sysbus: sysbus,
|
|
|
|
breakpoints: Vec::new(),
|
|
|
|
running: false,
|
2019-06-26 22:41:54 +01:00
|
|
|
previous_command: None
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_breakpoint_reached(&self) -> bool {
|
|
|
|
let pc = self.cpu.pc;
|
|
|
|
for b in &self.breakpoints {
|
|
|
|
if *b == pc {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-26 14:34:42 +01:00
|
|
|
fn decode_reg(&self, s: &str) -> DebuggerResult<usize> {
|
2019-06-26 10:05:53 +01:00
|
|
|
let reg_names = vec![
|
|
|
|
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "fp", "ip", "sp", "lr",
|
|
|
|
"pc"];
|
|
|
|
|
|
|
|
match reg_names.into_iter().position(|r| r == s) {
|
|
|
|
Some(index) => Ok(index),
|
|
|
|
None => Err(DebuggerError::InvalidArgument(format!("{:?} is not a register name", s)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:34:42 +01:00
|
|
|
fn val_reg(&self, arg: &Value) -> DebuggerResult<usize> {
|
2019-06-26 10:05:53 +01:00
|
|
|
match arg {
|
|
|
|
Value::Name(reg) => {
|
2019-06-26 14:34:42 +01:00
|
|
|
self.decode_reg(®)
|
2019-06-26 10:05:53 +01:00
|
|
|
},
|
|
|
|
v => Err(DebuggerError::InvalidArgument(format!("expected a number, got {:?}", v)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:34:42 +01:00
|
|
|
fn val_number(&self, arg: &Value) -> DebuggerResult<u32> {
|
2019-06-26 10:05:53 +01:00
|
|
|
match arg {
|
|
|
|
Value::Num(n) => Ok(*n),
|
|
|
|
v => Err(DebuggerError::InvalidArgument(format!("expected a number, got {:?}", v)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:34:42 +01:00
|
|
|
fn val_address(&self, arg: &Value) -> DebuggerResult<u32> {
|
2019-06-26 10:05:53 +01:00
|
|
|
match arg {
|
|
|
|
Value::Num(n) => Ok(*n),
|
|
|
|
Value::Name(reg) => {
|
2019-06-26 14:34:42 +01:00
|
|
|
let reg = self.decode_reg(®)?;
|
2019-06-26 10:05:53 +01:00
|
|
|
Ok(self.cpu.get_reg(reg))
|
|
|
|
}
|
|
|
|
v => Err(DebuggerError::InvalidArgument(format!("addr: expected a number or register, got {:?}", v)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_command(&self, command: Value, args: Vec<Value>) -> DebuggerResult<Command> {
|
|
|
|
let command = match command {
|
|
|
|
Value::Name(command) => command,
|
|
|
|
_ => {
|
|
|
|
return Err(DebuggerError::InvalidCommand("expected a name".to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match command.as_ref() {
|
|
|
|
"i" | "info" => Ok(Command::Info),
|
|
|
|
"s" | "step" => Ok(Command::SingleStep),
|
|
|
|
"c" | "continue" => Ok(Command::Continue),
|
|
|
|
"xxd" => {
|
2019-06-26 14:34:42 +01:00
|
|
|
let addr = self.val_address(&args[0])?;
|
|
|
|
let nbytes = self.val_number(&args[1])?;
|
2019-06-26 10:05:53 +01:00
|
|
|
Ok(Command::HexDump(addr, nbytes as usize))
|
|
|
|
}
|
|
|
|
"d" | "disass" => {
|
|
|
|
let (addr, n) = match args.len() {
|
|
|
|
2 => {
|
2019-06-26 14:34:42 +01:00
|
|
|
let addr = self.val_address(&args[0])?;
|
|
|
|
let n = self.val_number(&args[1])?;
|
2019-06-26 10:05:53 +01:00
|
|
|
|
|
|
|
(addr, n as usize)
|
|
|
|
}
|
|
|
|
1 => {
|
2019-06-26 14:34:42 +01:00
|
|
|
let addr = self.val_address(&args[0])?;
|
2019-06-26 10:05:53 +01:00
|
|
|
|
|
|
|
(addr, 10)
|
|
|
|
}
|
2019-06-26 22:41:54 +01:00
|
|
|
0 => {
|
|
|
|
if let Some(Command::Disass(addr, n)) = self.previous_command {
|
|
|
|
(addr + (4 * n as u32), 10)
|
|
|
|
} else {
|
|
|
|
(self.cpu.get_reg(15), 10)
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 10:05:53 +01:00
|
|
|
_ => {
|
|
|
|
return Err(DebuggerError::InvalidCommandFormat(
|
|
|
|
"disass [addr] [n]".to_string(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Command::Disass(addr, n))
|
|
|
|
}
|
|
|
|
"b" | "break" => {
|
|
|
|
if args.len() != 1 {
|
|
|
|
Err(DebuggerError::InvalidCommandFormat(
|
|
|
|
"disass <addr>".to_string(),
|
|
|
|
))
|
|
|
|
} else {
|
2019-06-26 14:34:42 +01:00
|
|
|
let addr = self.val_address(&args[0])?;
|
2019-06-26 10:05:53 +01:00
|
|
|
Ok(Command::AddBreakpoint(addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"bl" => Ok(Command::ListBreakpoints),
|
|
|
|
"q" | "quit" => Ok(Command::Quit),
|
|
|
|
"r" | "reset" => Ok(Command::Reset),
|
|
|
|
_ => Err(DebuggerError::InvalidCommand(command)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:34:42 +01:00
|
|
|
fn eval_assignment(&mut self, lvalue: Value, rvalue: Value) -> DebuggerResult<()> {
|
|
|
|
let lvalue = self.val_reg(&lvalue)?;
|
|
|
|
let rvalue = self.val_address(&rvalue)?;
|
|
|
|
|
|
|
|
self.cpu.set_reg(lvalue, rvalue);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-06-26 10:05:53 +01:00
|
|
|
fn eval_expr(&mut self, expr: Expr) {
|
|
|
|
match expr {
|
|
|
|
Expr::Command(c, a) => match self.eval_command(c, a) {
|
2019-06-26 22:41:54 +01:00
|
|
|
Ok(cmd) => {
|
|
|
|
self.previous_command = Some(cmd.clone());
|
|
|
|
cmd.run(self)
|
|
|
|
},
|
2019-06-26 10:05:53 +01:00
|
|
|
Err(DebuggerError::InvalidCommand(c)) => {
|
|
|
|
println!("{}: {:?}", "invalid command".red(), c)
|
|
|
|
}
|
|
|
|
Err(DebuggerError::InvalidArgument(m)) => {
|
|
|
|
println!("{}: {}", "invalid argument".red(), m)
|
|
|
|
}
|
|
|
|
Err(DebuggerError::InvalidCommandFormat(m)) => {
|
|
|
|
println!("help: {}", m.bright_yellow())
|
|
|
|
}
|
|
|
|
Err(e) => println!("{} {:?}", "failed to build command".red(), e),
|
|
|
|
},
|
2019-06-26 14:34:42 +01:00
|
|
|
Expr::Assignment(lvalue, rvalue) => match self.eval_assignment(lvalue, rvalue) {
|
|
|
|
Err(DebuggerError::InvalidArgument(m)) => println!("{}: {}", "assignment error".red(), m),
|
|
|
|
_ => ()
|
|
|
|
}
|
2019-06-26 10:05:53 +01:00
|
|
|
Expr::Empty => println!("Got empty expr"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) {
|
|
|
|
self.running = false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 19:31:45 +01:00
|
|
|
pub fn repl(&mut self) -> DebuggerResult<()> {
|
|
|
|
self.running = true;
|
|
|
|
let mut rl = Editor::<()>::new();
|
2019-06-26 22:22:41 +01:00
|
|
|
rl.load_history(".rustboyadvance_history");
|
2019-06-25 19:31:45 +01:00
|
|
|
while self.running {
|
|
|
|
let readline = rl.readline(&format!("({}) >> ", "rustboyadvance-dbg".cyan()));
|
|
|
|
match readline {
|
|
|
|
Ok(line) => {
|
|
|
|
if line.is_empty() {
|
2019-06-26 22:41:54 +01:00
|
|
|
self.previous_command = None;
|
2019-06-26 10:05:53 +01:00
|
|
|
continue;
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
2019-06-26 22:22:41 +01:00
|
|
|
rl.add_history_entry(line.as_str());
|
2019-06-26 10:05:53 +01:00
|
|
|
let expr = parse_expr(&line);
|
|
|
|
match expr {
|
|
|
|
Ok(expr) => self.eval_expr(expr),
|
|
|
|
Err(DebuggerError::ParsingError(msg)) => println!("Parsing error: {}", msg),
|
|
|
|
_ => (),
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(ReadlineError::Interrupted) => {
|
|
|
|
println!("CTRL-C");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(ReadlineError::Eof) => {
|
|
|
|
println!("CTRL-D");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
println!("Error: {:?}", err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 22:22:41 +01:00
|
|
|
rl.save_history(".rustboyadvance_history").unwrap();
|
2019-06-25 19:31:45 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|