2022-09-17 00:19:46 +01:00
|
|
|
use std::sync::{Arc, Condvar, Mutex};
|
|
|
|
use std::thread::JoinHandle;
|
|
|
|
|
|
|
|
type SendSync<T> = Arc<Mutex<T>>;
|
|
|
|
|
2022-09-18 22:10:55 +01:00
|
|
|
use arm7tdmi::gdbstub::common::Signal;
|
2022-09-17 00:19:46 +01:00
|
|
|
use arm7tdmi::gdbstub::stub::{DisconnectReason, SingleThreadStopReason};
|
|
|
|
use arm7tdmi::gdbstub::target::TargetError;
|
|
|
|
use arm7tdmi::gdbstub::target::{ext::base::singlethread::SingleThreadBase, Target};
|
|
|
|
use arm7tdmi::gdbstub_arch::arm::reg::ArmCoreRegs;
|
|
|
|
use arm7tdmi::memory::Addr;
|
2022-09-20 21:58:54 +01:00
|
|
|
use crossbeam::channel::Receiver;
|
2022-09-17 00:19:46 +01:00
|
|
|
|
|
|
|
// mod target;
|
|
|
|
mod event_loop;
|
|
|
|
pub(crate) mod gdb_thread;
|
|
|
|
mod memory_map;
|
|
|
|
mod target;
|
2022-09-20 21:58:54 +01:00
|
|
|
use target::DebuggerTarget;
|
2022-09-17 00:19:46 +01:00
|
|
|
|
|
|
|
use crate::GameBoyAdvance;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-09-18 22:10:55 +01:00
|
|
|
pub(crate) enum DebuggerRequest {
|
2022-09-17 00:19:46 +01:00
|
|
|
ReadRegs(SendSync<ArmCoreRegs>),
|
|
|
|
WriteRegs(ArmCoreRegs),
|
|
|
|
ReadAddrs(Addr, SendSync<Box<[u8]>>),
|
|
|
|
#[allow(unused)]
|
|
|
|
WriteAddrs(Addr, Box<[u8]>),
|
|
|
|
AddSwBreakpoint(Addr),
|
|
|
|
DelSwBreakpoint(Addr),
|
2022-09-18 22:10:55 +01:00
|
|
|
Interrupt,
|
2022-09-17 00:19:46 +01:00
|
|
|
Resume,
|
|
|
|
SingleStep,
|
2022-10-04 20:45:06 +01:00
|
|
|
Reset,
|
2022-09-17 00:19:46 +01:00
|
|
|
Disconnected(DisconnectReason),
|
|
|
|
}
|
|
|
|
|
2022-09-18 22:10:55 +01:00
|
|
|
pub(crate) struct DebuggerRequestHandler {
|
|
|
|
rx: Receiver<DebuggerRequest>,
|
|
|
|
request_complete_signal: Arc<(Mutex<bool>, Condvar)>,
|
2022-09-20 21:58:54 +01:00
|
|
|
stop_signal: Arc<(Mutex<Option<SingleThreadStopReason<u32>>>, Condvar)>,
|
2022-09-17 00:19:46 +01:00
|
|
|
thread: JoinHandle<()>,
|
2022-10-04 20:45:06 +01:00
|
|
|
pub(crate) stopped: bool,
|
2022-09-17 00:19:46 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 22:10:55 +01:00
|
|
|
impl DebuggerRequestHandler {
|
2022-09-20 21:58:54 +01:00
|
|
|
pub fn handle_incoming_requests(
|
|
|
|
mut self,
|
|
|
|
gba: &mut GameBoyAdvance,
|
|
|
|
) -> Option<DebuggerRequestHandler> {
|
|
|
|
if self.thread.is_finished() {
|
|
|
|
warn!("gdb server thread unexpectdly died");
|
|
|
|
return self.terminate();
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
// Handle as much as messages as possible
|
|
|
|
while let Ok(mut req) = self.rx.try_recv() {
|
|
|
|
match self.handle_request(gba, &mut req) {
|
|
|
|
Ok(Some(disconnect_reason)) => {
|
|
|
|
debug!("Debugger disconnected due to {:?}", disconnect_reason);
|
|
|
|
debug!("closing gdbserver thread");
|
|
|
|
return self.terminate();
|
|
|
|
}
|
|
|
|
Ok(None) => {}
|
|
|
|
Err(_) => {
|
|
|
|
error!("An error occured while handling debug request {:?}", req);
|
|
|
|
return self.terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If target is in stopped state, stay here.
|
|
|
|
if !self.stopped {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
|
2022-09-18 22:10:55 +01:00
|
|
|
fn handle_request(
|
|
|
|
&mut self,
|
|
|
|
gba: &mut GameBoyAdvance,
|
|
|
|
req: &mut DebuggerRequest,
|
2022-09-20 21:58:54 +01:00
|
|
|
) -> Result<Option<DisconnectReason>, TargetError<<DebuggerTarget as Target>::Error>> {
|
2022-09-18 22:10:55 +01:00
|
|
|
use DebuggerRequest::*;
|
|
|
|
match req {
|
|
|
|
ReadRegs(regs) => {
|
|
|
|
let mut regs = regs.lock().unwrap();
|
|
|
|
gba.cpu.read_registers(&mut regs)?;
|
2022-09-20 21:58:54 +01:00
|
|
|
trace!("Debugger requested to read regs: {:?}", regs);
|
|
|
|
self.complete_request(None)
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
WriteRegs(regs) => {
|
2022-09-20 21:58:54 +01:00
|
|
|
trace!("Debugger requested to write regs: {:?}", regs);
|
2022-09-18 22:10:55 +01:00
|
|
|
gba.cpu.write_registers(regs)?;
|
2022-09-20 21:58:54 +01:00
|
|
|
self.complete_request(None)
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
ReadAddrs(addr, data) => {
|
|
|
|
let mut data = data.lock().unwrap();
|
2022-09-20 21:58:54 +01:00
|
|
|
trace!(
|
2022-09-18 22:10:55 +01:00
|
|
|
"Debugger requested to read {} bytes from 0x{:08x}",
|
|
|
|
data.len(),
|
|
|
|
addr
|
|
|
|
);
|
|
|
|
gba.cpu.read_addrs(*addr, &mut data)?;
|
2022-09-20 21:58:54 +01:00
|
|
|
self.complete_request(None)
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
WriteAddrs(addr, data) => {
|
2022-09-20 21:58:54 +01:00
|
|
|
trace!(
|
2022-09-18 22:10:55 +01:00
|
|
|
"Debugger requested to write {} bytes at 0x{:08x}",
|
|
|
|
data.len(),
|
|
|
|
addr
|
|
|
|
);
|
2024-03-22 20:35:05 +00:00
|
|
|
gba.cpu.write_addrs(*addr, data)?;
|
2022-09-20 21:58:54 +01:00
|
|
|
self.complete_request(None)
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
Interrupt => {
|
2022-09-20 21:58:54 +01:00
|
|
|
debug!("Ctrl-C from debugger");
|
|
|
|
self.stopped = true;
|
|
|
|
self.complete_request(Some(SingleThreadStopReason::Signal(Signal::SIGINT)))
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
Resume => {
|
2022-09-20 21:58:54 +01:00
|
|
|
debug!("Resume");
|
2022-09-18 22:10:55 +01:00
|
|
|
self.stopped = false;
|
2022-09-20 21:58:54 +01:00
|
|
|
self.complete_request(None)
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
2022-10-04 20:45:06 +01:00
|
|
|
Reset => {
|
|
|
|
debug!("Sending reset interrupt to gba");
|
|
|
|
self.stopped = true;
|
|
|
|
gba.cpu.reset();
|
|
|
|
self.complete_request(Some(SingleThreadStopReason::Signal(Signal::SIGTRAP)))
|
|
|
|
}
|
2022-09-18 22:10:55 +01:00
|
|
|
SingleStep => {
|
|
|
|
debug!("Debugger requested single step");
|
2022-09-20 21:58:54 +01:00
|
|
|
self.stopped = true;
|
|
|
|
gba.single_step();
|
|
|
|
let _ = gba.handle_events();
|
|
|
|
self.complete_request(Some(SingleThreadStopReason::DoneStep))
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
AddSwBreakpoint(addr) => {
|
|
|
|
gba.cpu.add_breakpoint(*addr);
|
2022-09-20 21:58:54 +01:00
|
|
|
self.complete_request(None)
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
DelSwBreakpoint(addr) => {
|
|
|
|
gba.cpu.del_breakpoint(*addr);
|
2022-09-20 21:58:54 +01:00
|
|
|
self.complete_request(None)
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
2022-09-20 21:58:54 +01:00
|
|
|
Disconnected(reason) => Ok(Some(*reason)),
|
2022-09-18 22:10:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 21:58:54 +01:00
|
|
|
fn terminate(mut self) -> Option<DebuggerRequestHandler> {
|
2022-09-18 22:10:55 +01:00
|
|
|
self.notify_stop_reason(SingleThreadStopReason::Exited(1));
|
|
|
|
self.thread.join().unwrap();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify_request_complete(&mut self) {
|
|
|
|
let (lock, cvar) = &*self.request_complete_signal;
|
|
|
|
let mut finished = lock.lock().unwrap();
|
|
|
|
*finished = true;
|
|
|
|
cvar.notify_one();
|
2022-10-04 20:45:06 +01:00
|
|
|
// wait for the ack
|
|
|
|
while *finished {
|
|
|
|
finished = cvar.wait(finished).unwrap();
|
|
|
|
}
|
2022-09-17 00:19:46 +01:00
|
|
|
}
|
|
|
|
|
2022-09-20 21:58:54 +01:00
|
|
|
fn complete_request(
|
|
|
|
&mut self,
|
|
|
|
stop_reason: Option<SingleThreadStopReason<u32>>,
|
|
|
|
) -> Result<Option<DisconnectReason>, TargetError<<DebuggerTarget as Target>::Error>> {
|
|
|
|
self.notify_request_complete();
|
|
|
|
if let Some(stop_reason) = stop_reason {
|
|
|
|
self.notify_stop_reason(stop_reason);
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2022-09-17 00:19:46 +01:00
|
|
|
pub fn notify_stop_reason(&mut self, reason: SingleThreadStopReason<u32>) {
|
2022-09-20 21:58:54 +01:00
|
|
|
debug!("Notifying debugger on stop reason: {:?}", reason);
|
2022-09-17 00:19:46 +01:00
|
|
|
let (lock, cvar) = &*self.stop_signal;
|
|
|
|
let mut stop_reason = lock.lock().unwrap();
|
2022-09-20 21:58:54 +01:00
|
|
|
*stop_reason = Some(reason);
|
2022-09-17 00:19:46 +01:00
|
|
|
cvar.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn notify_breakpoint(&mut self, _bp: Addr) {
|
2022-09-20 21:58:54 +01:00
|
|
|
self.stopped = true;
|
2022-09-17 00:19:46 +01:00
|
|
|
self.notify_stop_reason(SingleThreadStopReason::SwBreak(()));
|
|
|
|
}
|
|
|
|
}
|