2019-06-25 19:31:45 +01:00
|
|
|
use rustyline::error::ReadlineError;
|
|
|
|
use rustyline::Editor;
|
|
|
|
|
|
|
|
use colored::*;
|
|
|
|
|
2019-06-28 23:52:10 +01:00
|
|
|
use super::arm7tdmi;
|
2019-07-02 11:31:02 +01:00
|
|
|
use super::arm7tdmi::{Addr, Bus};
|
2019-06-25 19:31:45 +01:00
|
|
|
use super::sysbus::SysBus;
|
|
|
|
|
|
|
|
mod parser;
|
2019-07-02 11:31:02 +01:00
|
|
|
use parser::{parse_expr, DerefType, Expr, Value};
|
2019-06-25 19:31:45 +01:00
|
|
|
|
2019-06-27 10:55:28 +01:00
|
|
|
mod command;
|
|
|
|
use command::Command;
|
|
|
|
|
2019-06-25 19:31:45 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum DebuggerError {
|
|
|
|
ParsingError(String),
|
2019-06-28 23:52:10 +01:00
|
|
|
CpuError(arm7tdmi::CpuError),
|
2019-06-25 19:31:45 +01:00
|
|
|
InvalidCommand(String),
|
2019-06-26 10:05:53 +01:00
|
|
|
InvalidArgument(String),
|
|
|
|
InvalidCommandFormat(String),
|
2019-06-25 19:31:45 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 23:52:10 +01:00
|
|
|
impl From<arm7tdmi::CpuError> for DebuggerError {
|
|
|
|
fn from(e: arm7tdmi::CpuError) -> DebuggerError {
|
2019-06-25 19:31:45 +01:00
|
|
|
DebuggerError::CpuError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DebuggerResult<T> = Result<T, DebuggerError>;
|
|
|
|
|
2019-06-26 10:05:53 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Debugger {
|
2019-06-28 23:52:10 +01:00
|
|
|
pub cpu: arm7tdmi::cpu::Core,
|
2019-06-27 10:55:28 +01:00
|
|
|
pub sysbus: SysBus,
|
2019-06-26 10:05:53 +01:00
|
|
|
running: bool,
|
|
|
|
breakpoints: Vec<u32>,
|
2019-06-27 10:55:28 +01:00
|
|
|
pub previous_command: Option<Command>,
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Debugger {
|
2019-06-28 23:52:10 +01:00
|
|
|
pub fn new(cpu: arm7tdmi::cpu::Core, sysbus: SysBus) -> Debugger {
|
2019-06-26 10:05:53 +01:00
|
|
|
Debugger {
|
|
|
|
cpu: cpu,
|
|
|
|
sysbus: sysbus,
|
|
|
|
breakpoints: Vec::new(),
|
|
|
|
running: false,
|
2019-06-27 10:55:28 +01:00
|
|
|
previous_command: None,
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 10:55:28 +01:00
|
|
|
pub fn check_breakpoint(&self) -> Option<u32> {
|
2019-06-28 23:52:10 +01:00
|
|
|
let next_pc = self.cpu.get_next_pc();
|
2019-06-27 10:55:28 +01:00
|
|
|
for bp in &self.breakpoints {
|
2019-06-28 23:52:10 +01:00
|
|
|
if *bp == next_pc {
|
|
|
|
return Some(next_pc);
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 10:55:28 +01:00
|
|
|
None
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 10:55:28 +01:00
|
|
|
pub fn delete_breakpoint(&mut self, addr: u32) {
|
|
|
|
self.breakpoints.retain(|&a| a != addr);
|
|
|
|
}
|
2019-06-26 10:05:53 +01:00
|
|
|
|
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![
|
2019-06-27 10:55:28 +01:00
|
|
|
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "fp", "ip", "sp",
|
|
|
|
"lr", "pc",
|
|
|
|
];
|
2019-06-26 10:05:53 +01:00
|
|
|
|
|
|
|
match reg_names.into_iter().position(|r| r == s) {
|
|
|
|
Some(index) => Ok(index),
|
2019-06-27 10:55:28 +01:00
|
|
|
None => Err(DebuggerError::InvalidArgument(format!(
|
|
|
|
"{:?} is not a register name",
|
|
|
|
s
|
|
|
|
))),
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-02 14:57:35 +01:00
|
|
|
Value::Identifier(reg) => self.decode_reg(®),
|
2019-06-27 10:55:28 +01:00
|
|
|
v => Err(DebuggerError::InvalidArgument(format!(
|
|
|
|
"expected a number, got {:?}",
|
|
|
|
v
|
|
|
|
))),
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
2019-06-27 10:55:28 +01:00
|
|
|
v => Err(DebuggerError::InvalidArgument(format!(
|
|
|
|
"expected a number, got {:?}",
|
|
|
|
v
|
|
|
|
))),
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 11:31:02 +01:00
|
|
|
fn val_address(&self, arg: &Value) -> DebuggerResult<Addr> {
|
2019-06-26 10:05:53 +01:00
|
|
|
match arg {
|
|
|
|
Value::Num(n) => Ok(*n),
|
2019-07-02 14:57:35 +01:00
|
|
|
Value::Identifier(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))
|
|
|
|
}
|
2019-06-27 10:55:28 +01:00
|
|
|
v => Err(DebuggerError::InvalidArgument(format!(
|
|
|
|
"addr: expected a number or register, got {:?}",
|
|
|
|
v
|
|
|
|
))),
|
2019-06-26 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:34:42 +01:00
|
|
|
fn eval_assignment(&mut self, lvalue: Value, rvalue: Value) -> DebuggerResult<()> {
|
|
|
|
let lvalue = self.val_reg(&lvalue)?;
|
2019-07-02 11:31:02 +01:00
|
|
|
let rvalue = match rvalue {
|
|
|
|
Value::Deref(addr_value, deref_type) => {
|
|
|
|
let addr = self.val_address(&addr_value)?;
|
|
|
|
match deref_type {
|
|
|
|
DerefType::Word => self.sysbus.read_32(addr),
|
|
|
|
DerefType::HalfWord => self.sysbus.read_16(addr) as u32,
|
|
|
|
DerefType::Byte => self.sysbus.read_8(addr) as u32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.val_address(&rvalue)?,
|
|
|
|
};
|
2019-06-26 14:34:42 +01:00
|
|
|
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-27 10:55:28 +01:00
|
|
|
}
|
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) {
|
2019-06-27 10:55:28 +01:00
|
|
|
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<()> {
|
2019-06-27 10:55:28 +01:00
|
|
|
println!("Welcome to rustboyadvance-NG debugger 😎!\n");
|
2019-06-25 19:31:45 +01:00
|
|
|
self.running = true;
|
|
|
|
let mut rl = Editor::<()>::new();
|
2019-07-02 14:57:35 +01:00
|
|
|
rl.load_history(".rustboyadvance_history").unwrap();
|
2019-06-25 19:31:45 +01:00
|
|
|
while self.running {
|
2019-06-28 23:52:10 +01:00
|
|
|
let readline = rl.readline(&format!("({}) ᐅ ", "rustboyadvance-dbg".bold().cyan()));
|
2019-06-25 19:31:45 +01:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|