mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
kernel: basic version of gdb serevr
This adds a basic gdb server. Currently, it is possible to list processes and inspect memory and threads. It is not yet possible to actually interact with code (e.g. no breakpoints and no single stepping). Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
13029a76a2
commit
a34733e5e9
4 changed files with 276 additions and 196 deletions
|
|
@ -33,6 +33,9 @@ fn sim_write(new: usize) {
|
||||||
|
|
||||||
/// Disable external interrupts
|
/// Disable external interrupts
|
||||||
pub fn disable_all_irqs() {
|
pub fn disable_all_irqs() {
|
||||||
|
if !IRQ_ENABLED.load(Ordering::Relaxed) {
|
||||||
|
panic!("IRQs were disabled twice!");
|
||||||
|
}
|
||||||
SIM_BACKING.store(sim_read(), Ordering::Relaxed);
|
SIM_BACKING.store(sim_read(), Ordering::Relaxed);
|
||||||
IRQ_ENABLED.store(false, Ordering::Relaxed);
|
IRQ_ENABLED.store(false, Ordering::Relaxed);
|
||||||
sim_write(0x0);
|
sim_write(0x0);
|
||||||
|
|
@ -51,11 +54,16 @@ pub fn enable_irq(irq_no: usize) {
|
||||||
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
|
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
|
||||||
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
|
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
|
||||||
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
||||||
sim_write(sim_read() | (1 << irq_no));
|
let existing = sim_read();
|
||||||
|
let new = existing | (1 << irq_no);
|
||||||
|
// println!("Enabling IRQ {} (active): {:08x} -> {:08x}", irq_no, existing, new);
|
||||||
|
sim_write(new);
|
||||||
} else {
|
} else {
|
||||||
SIM_BACKING
|
SIM_BACKING
|
||||||
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |v| {
|
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
|
||||||
Some(v | (1 << irq_no))
|
let new = existing | (1 << irq_no);
|
||||||
|
// println!("Enabling IRQ {} (masked: {:08x}): {:08x} -> {:08x}", irq_no, sim_read(), existing, new);
|
||||||
|
Some(new)
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
@ -65,11 +73,25 @@ pub fn enable_irq(irq_no: usize) {
|
||||||
/// SIM backing instead so that it will be disabled when interrupts are restored.
|
/// SIM backing instead so that it will be disabled when interrupts are restored.
|
||||||
pub fn disable_irq(irq_no: usize) {
|
pub fn disable_irq(irq_no: usize) {
|
||||||
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
||||||
sim_write(sim_read() & !(1 << irq_no));
|
let existing = sim_read();
|
||||||
|
let new = existing & !(1 << irq_no);
|
||||||
|
// println!(
|
||||||
|
// "Disabling IRQ {} (active): {:08x} -> {:08x}",
|
||||||
|
// irq_no, existing, new
|
||||||
|
// );
|
||||||
|
sim_write(new);
|
||||||
} else {
|
} else {
|
||||||
SIM_BACKING
|
SIM_BACKING
|
||||||
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |v| {
|
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
|
||||||
Some(v & !(1 << irq_no))
|
let new = existing & !(1 << irq_no);
|
||||||
|
println!(
|
||||||
|
"Disabling IRQ {} (masked: {:08x}): {:08x} -> {:08x}",
|
||||||
|
irq_no,
|
||||||
|
sim_read(),
|
||||||
|
existing,
|
||||||
|
new
|
||||||
|
);
|
||||||
|
Some(new)
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
@ -371,6 +393,7 @@ pub extern "C" fn trap_handler(
|
||||||
{
|
{
|
||||||
ss.pause_process_for_debug(pid)
|
ss.pause_process_for_debug(pid)
|
||||||
.expect("couldn't debug current process");
|
.expect("couldn't debug current process");
|
||||||
|
crate::debug::gdb::report_terminated(pid);
|
||||||
println!("Program suspended. You may inspect it using gdb.");
|
println!("Program suspended. You may inspect it using gdb.");
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "gdb-stub"))]
|
#[cfg(not(feature = "gdb-stub"))]
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use gdbstub::common::{Signal, Tid};
|
use gdbstub::common::{Signal, Tid};
|
||||||
|
use gdbstub::conn::Connection;
|
||||||
use gdbstub::stub::state_machine::GdbStubStateMachine;
|
use gdbstub::stub::state_machine::GdbStubStateMachine;
|
||||||
use gdbstub::stub::{GdbStubBuilder, GdbStubError, MultiThreadStopReason};
|
use gdbstub::stub::{GdbStubBuilder, GdbStubError, MultiThreadStopReason};
|
||||||
use gdbstub::target;
|
use gdbstub::target;
|
||||||
|
|
@ -8,35 +9,49 @@ use gdbstub::target::ext::base::single_register_access::{
|
||||||
};
|
};
|
||||||
use gdbstub::target::ext::base::BaseOps;
|
use gdbstub::target::ext::base::BaseOps;
|
||||||
use gdbstub::target::ext::monitor_cmd::MonitorCmd;
|
use gdbstub::target::ext::monitor_cmd::MonitorCmd;
|
||||||
use gdbstub::target::TargetError;
|
use gdbstub::target::{Target, TargetError, TargetResult};
|
||||||
use gdbstub::target::{Target, TargetResult};
|
|
||||||
use gdbstub_arch::riscv::reg::id::RiscvRegId;
|
use gdbstub_arch::riscv::reg::id::RiscvRegId;
|
||||||
|
|
||||||
use crate::io::SerialRead;
|
use crate::io::SerialRead;
|
||||||
use crate::platform::precursor::gdbuart::GdbUart;
|
use crate::platform::precursor::gdbuart::GdbUart;
|
||||||
|
|
||||||
use core::convert::TryInto;
|
use core::convert::TryInto;
|
||||||
|
// use core::num::{NonZeroU16, NonZeroUsize};
|
||||||
|
|
||||||
pub struct XousTarget {
|
pub struct XousTarget {
|
||||||
pid: Option<xous_kernel::PID>,
|
pid: Option<xous_kernel::PID>,
|
||||||
|
// breakpoints: [Option<(NonZeroUsize, NonZeroU16)>; 16],
|
||||||
}
|
}
|
||||||
pub struct XousDebugState<'a> {
|
pub struct XousDebugState<'a> {
|
||||||
pub target: XousTarget,
|
pub target: XousTarget,
|
||||||
pub server: GdbStubStateMachine<'a, XousTarget, crate::platform::precursor::gdbuart::GdbUart>,
|
pub server: GdbStubStateMachine<'a, XousTarget, crate::platform::precursor::gdbuart::GdbUart>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static mut GDB_STATE: Option<XousDebugState> = None;
|
static mut GDB_STATE: Option<XousDebugState> = None;
|
||||||
pub static mut GDB_BUFFER: [u8; 4096] = [0u8; 4096];
|
static mut GDB_BUFFER: [u8; 4096] = [0u8; 4096];
|
||||||
|
|
||||||
|
trait ProcessPid {
|
||||||
|
fn pid(&self) -> Option<xous_kernel::PID>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProcessPid for XousTarget {
|
||||||
|
fn pid(&self) -> Option<xous_kernel::PID> {
|
||||||
|
self.pid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn receive_irq(uart: &mut GdbUart) {
|
fn receive_irq(uart: &mut GdbUart) {
|
||||||
while let Some(c) = uart.getc() {
|
while let Some(c) = uart.getc() {
|
||||||
advance_gdb_or_recreate(Some(c));
|
process_character(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XousTarget {
|
impl XousTarget {
|
||||||
pub fn new() -> XousTarget {
|
pub fn new() -> XousTarget {
|
||||||
XousTarget { pid: None }
|
XousTarget {
|
||||||
|
pid: None,
|
||||||
|
// breakpoints: [None; 16],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,6 +69,12 @@ impl Target for XousTarget {
|
||||||
Some(self)
|
Some(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Opt in to having GDB handle breakpoints for us. This allows for an unlimited number
|
||||||
|
/// of breakpoints without having us keep track of the breakpoints ourselves.
|
||||||
|
fn guard_rail_implicit_sw_breakpoints(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
fn support_monitor_cmd(&mut self) -> Option<target::ext::monitor_cmd::MonitorCmdOps<'_, Self>> {
|
fn support_monitor_cmd(&mut self) -> Option<target::ext::monitor_cmd::MonitorCmdOps<'_, Self>> {
|
||||||
Some(self)
|
Some(self)
|
||||||
}
|
}
|
||||||
|
|
@ -380,7 +401,7 @@ impl SingleRegisterAccess<Tid> for XousTarget {
|
||||||
|
|
||||||
impl MultiThreadResume for XousTarget {
|
impl MultiThreadResume for XousTarget {
|
||||||
fn resume(&mut self) -> Result<(), Self::Error> {
|
fn resume(&mut self) -> Result<(), Self::Error> {
|
||||||
println!("Resuming process {:?}", self.pid);
|
// println!("Resuming process {:?}", self.pid);
|
||||||
// unsafe { HALTED = false };
|
// unsafe { HALTED = false };
|
||||||
// match default_resume_action {
|
// match default_resume_action {
|
||||||
// ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
|
// ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
|
||||||
|
|
@ -398,7 +419,7 @@ impl MultiThreadResume for XousTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear_resume_actions(&mut self) -> Result<(), Self::Error> {
|
fn clear_resume_actions(&mut self) -> Result<(), Self::Error> {
|
||||||
println!("Clearing resume actions");
|
// println!("Clearing resume actions");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -422,33 +443,30 @@ impl MultiThreadResume for XousTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl target::ext::breakpoints::Breakpoints for XousTarget {
|
impl target::ext::breakpoints::Breakpoints for XousTarget {
|
||||||
#[inline(always)]
|
// fn support_sw_breakpoint(
|
||||||
fn support_sw_breakpoint(
|
// &mut self,
|
||||||
&mut self,
|
// ) -> Option<target::ext::breakpoints::SwBreakpointOps<'_, Self>> {
|
||||||
) -> Option<target::ext::breakpoints::SwBreakpointOps<'_, Self>> {
|
// Some(self)
|
||||||
Some(self)
|
// }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl target::ext::breakpoints::SwBreakpoint for XousTarget {
|
// impl target::ext::breakpoints::SwBreakpoint for XousTarget {
|
||||||
#[inline(never)]
|
// fn add_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
||||||
fn add_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
// println!(
|
||||||
println!(
|
// "GDB asked us to add a software breakpoint at {:08x} of kind {:?}",
|
||||||
"GDB asked us to add a software breakpoint at {:08x} of kind {:?}",
|
// _addr, _kind
|
||||||
_addr, _kind
|
// );
|
||||||
);
|
// Ok(true)
|
||||||
Ok(true)
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(never)]
|
// fn remove_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
||||||
fn remove_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
// println!(
|
||||||
println!(
|
// "GDB asked us to remove a software breakpoint at {:08x} of kind {:?}",
|
||||||
"GDB asked us to remove a software breakpoint at {:08x} of kind {:?}",
|
// _addr, _kind
|
||||||
_addr, _kind
|
// );
|
||||||
);
|
// Ok(true)
|
||||||
Ok(true)
|
// }
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
impl MonitorCmd for XousTarget {
|
impl MonitorCmd for XousTarget {
|
||||||
fn handle_monitor_cmd(
|
fn handle_monitor_cmd(
|
||||||
|
|
@ -521,128 +539,170 @@ impl MonitorCmd for XousTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn advance_gdb_or_recreate(byte: Option<u8>) {
|
fn state_can_accept_characters<'a, T: Target + ProcessPid, C: Connection>(
|
||||||
advance_gdb_state(byte);
|
machine: &GdbStubStateMachine<'a, T, C>,
|
||||||
if unsafe { GDB_STATE.is_none() } {
|
) -> bool {
|
||||||
println!("GDB went away -- recreating it");
|
match machine {
|
||||||
init();
|
GdbStubStateMachine::Idle(_) | GdbStubStateMachine::Running(_) => true,
|
||||||
|
GdbStubStateMachine::CtrlCInterrupt(_) | GdbStubStateMachine::Disconnected(_) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn advance_gdb_state(byte: Option<u8>) -> bool {
|
fn ensure_can_accept_characters_inner<'a, T: Target + ProcessPid, C: Connection>(
|
||||||
let Some(XousDebugState {
|
machine: GdbStubStateMachine<'a, T, C>,
|
||||||
mut target,
|
target: &mut T,
|
||||||
server: gdb,
|
recurse_count: usize,
|
||||||
}) = (unsafe { GDB_STATE.take() }) else {
|
) -> Option<GdbStubStateMachine<'a, T, C>> {
|
||||||
println!("No GDB!");
|
if recurse_count == 0 {
|
||||||
return false;
|
return None;
|
||||||
};
|
|
||||||
|
|
||||||
let mut new_gdb = match gdb {
|
|
||||||
GdbStubStateMachine::Idle(gdb_stm_inner) => match byte {
|
|
||||||
Some(byte) => match gdb_stm_inner.incoming_data(&mut target, byte) {
|
|
||||||
Ok(gdb) => gdb,
|
|
||||||
Err(e) => {
|
|
||||||
println!("gdbstub error during idle operation: {:?}", e);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
println!("gdbstub was idle with no characters?!");
|
|
||||||
GdbStubStateMachine::Idle(gdb_stm_inner)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
GdbStubStateMachine::Running(gdb_stm_inner) => {
|
|
||||||
// If we're here we were running but have stopped now (either
|
|
||||||
// because we hit Ctrl+c in gdb and hence got a serial interrupt
|
|
||||||
// or we hit a breakpoint).
|
|
||||||
// let conn = gdb_stm_inner.borrow_conn();
|
|
||||||
// conn.disable_irq();
|
|
||||||
// let data_to_read = conn.peek().unwrap().is_some();
|
|
||||||
// let mut stop_reason = target.determine_stop_reason(reason);
|
|
||||||
|
|
||||||
if let Some(byte) = byte {
|
|
||||||
match gdb_stm_inner.incoming_data(&mut target, byte) {
|
|
||||||
Ok(pumped_stm) => {
|
|
||||||
if let GdbStubStateMachine::CtrlCInterrupt(_) = pumped_stm {
|
|
||||||
println!(
|
|
||||||
"Looks like we maybe just transitioned from running to ctrl-c?"
|
|
||||||
);
|
|
||||||
} else if let GdbStubStateMachine::Idle(_) = pumped_stm {
|
|
||||||
println!("Looks like we maybe just transitioned from running to idle?");
|
|
||||||
}
|
|
||||||
pumped_stm
|
|
||||||
}
|
|
||||||
Err(GdbStubError::TargetError(e)) => {
|
|
||||||
println!("Target raised a fatal error: {:?}", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("gdbstub error in DeferredStopReason.pump: {:?}", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// } else if let Some(reason) = stop_reason.take() {
|
|
||||||
// match gdb_stm_inner.report_stop(&mut target, MultiThreadStopReason::DoneStep) {
|
|
||||||
// Ok(gdb_stm_new) => gdb_stm_new,
|
|
||||||
// Err(GdbStubError::TargetError(e)) => {
|
|
||||||
// println!("Target raised a fatal error {:?}", e);
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// Err(e) => {
|
|
||||||
// println!("gdbstub internal error {:?}", e);
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } else if target.resume_with.is_some() {
|
|
||||||
// // // We don't have a `stop_reason` and we don't have something
|
|
||||||
// // // to read on the line. This probably means we're done and
|
|
||||||
// // // we should run again.
|
|
||||||
// // conn.enable_irq();
|
|
||||||
// // let r = GDB_STUB
|
|
||||||
// // .lock()
|
|
||||||
// // .replace(GdbStubStateMachine::Running(gdb_stm_inner));
|
|
||||||
// // assert!(
|
|
||||||
// // r.is_none(),
|
|
||||||
// // "Put something in GDB_STUB which we shouldn't have..."
|
|
||||||
// // );
|
|
||||||
// // return false;
|
|
||||||
// GdbStubStateMachine::Running(gdb_stm_inner)
|
|
||||||
} else {
|
|
||||||
panic!("Can't happen?");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GdbStubStateMachine::CtrlCInterrupt(_gdb_stm_inner) => {
|
match machine {
|
||||||
panic!("debug server was in an unexpected state");
|
GdbStubStateMachine::Idle(_) | GdbStubStateMachine::Running(_) => Some(machine),
|
||||||
}
|
GdbStubStateMachine::CtrlCInterrupt(gdb_stm_inner) => {
|
||||||
|
if let Some(pid) = target.pid() {
|
||||||
GdbStubStateMachine::Disconnected(_gdb_stm_inner) => {
|
// println!("Starting debug on process {:?}", pid);
|
||||||
println!("GdbStubStateMachine::Disconnected byebye");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if let GdbStubStateMachine::CtrlCInterrupt(gdb_stm_inner) = new_gdb {
|
|
||||||
println!("Just transitioned into CtrlCInterrupt");
|
|
||||||
match gdb_stm_inner.interrupt_handled(&mut target, Some(MultiThreadStopReason::DoneStep)) {
|
|
||||||
Ok(gdb) => new_gdb = gdb,
|
|
||||||
Err(e) => {
|
|
||||||
println!("gdbstub error during ctrl-c interrupt{:?}", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(pid) = target.pid {
|
|
||||||
println!("Starting debug on process {:?}", pid);
|
|
||||||
crate::services::SystemServices::with_mut(|system_services| {
|
crate::services::SystemServices::with_mut(|system_services| {
|
||||||
system_services.pause_process_for_debug(pid).unwrap()
|
system_services.pause_process_for_debug(pid).unwrap()
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
println!("No process specified! Not debugging");
|
println!("No process specified! Not debugging");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let Ok(new_server) = gdb_stm_inner.interrupt_handled(target, Some(MultiThreadStopReason::Signal(Signal::SIGINT))) else {
|
||||||
|
return None
|
||||||
|
};
|
||||||
|
ensure_can_accept_characters_inner(new_server, target, recurse_count - 1)
|
||||||
}
|
}
|
||||||
|
GdbStubStateMachine::Disconnected(gdb_stm_inner) => {
|
||||||
|
println!(
|
||||||
|
"GdbStubStateMachine::Disconnected due to {:?}",
|
||||||
|
gdb_stm_inner.get_reason()
|
||||||
|
);
|
||||||
|
ensure_can_accept_characters_inner(
|
||||||
|
gdb_stm_inner.return_to_idle(),
|
||||||
|
target,
|
||||||
|
recurse_count - 1,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_can_accept_characters<'a, T: Target + ProcessPid, C: Connection>(
|
||||||
|
machine: GdbStubStateMachine<'a, T, C>,
|
||||||
|
target: &mut T,
|
||||||
|
) -> Option<GdbStubStateMachine<'a, T, C>> {
|
||||||
|
ensure_can_accept_characters_inner(machine, target, 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Advance the GDB state.
|
||||||
|
///
|
||||||
|
/// Two states accept characters:
|
||||||
|
///
|
||||||
|
/// GdbStubStateMachine::Idle
|
||||||
|
/// GdbStubStateMachine::Running
|
||||||
|
///
|
||||||
|
/// Two states exist merely to transition to other states:
|
||||||
|
///
|
||||||
|
/// GdbStubStateMachine::CtrlCInterrupt
|
||||||
|
/// GdbStubStateMachine::Disconnected
|
||||||
|
fn process_character(byte: u8) {
|
||||||
|
let XousDebugState { mut target, server } = unsafe {
|
||||||
|
GDB_STATE.take().unwrap_or_else(|| {
|
||||||
|
let gdb = GdbStubBuilder::new(GdbUart::new(receive_irq).unwrap())
|
||||||
|
.with_packet_buffer(&mut GDB_BUFFER)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let mut target = XousTarget::new();
|
||||||
|
let server = gdb
|
||||||
|
.run_state_machine(&mut target)
|
||||||
|
.expect("couldn't create gdb");
|
||||||
|
XousDebugState { target, server }
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
if !state_can_accept_characters(&server) {
|
||||||
|
println!("GDB server was not in a state to accept characters");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_server = match server {
|
||||||
|
GdbStubStateMachine::Idle(gdb_stm_inner) => {
|
||||||
|
let Ok(gdb) = gdb_stm_inner.incoming_data(&mut target, byte).map_err(|e| println!("gdbstub error during idle operation: {:?}", e)) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
gdb
|
||||||
|
}
|
||||||
|
|
||||||
|
GdbStubStateMachine::Running(gdb_stm_inner) => {
|
||||||
|
// If we're here we were running but have stopped now (either
|
||||||
|
// because we hit Ctrl+c in gdb and hence got a serial interrupt
|
||||||
|
// or we hit a breakpoint).
|
||||||
|
|
||||||
|
match gdb_stm_inner.incoming_data(&mut target, byte) {
|
||||||
|
Ok(pumped_stm) => pumped_stm,
|
||||||
|
Err(GdbStubError::TargetError(e)) => {
|
||||||
|
println!("Target raised a fatal error: {:?}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("gdbstub error in DeferredStopReason.pump: {:?}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
println!("GDB is in an unexpected state!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(server) = ensure_can_accept_characters(new_server, &mut target) else {
|
||||||
|
println!("Couldn't convert GDB into a state that accepts characters");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
unsafe { GDB_STATE = Some(XousDebugState { target, server }) };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn report_terminated(pid: xous_kernel::PID) {
|
||||||
|
println!("Reporting that process {:?} has terminated!", pid);
|
||||||
|
let Some(XousDebugState {
|
||||||
|
mut target,
|
||||||
|
server: gdb,
|
||||||
|
}) = (unsafe { GDB_STATE.take() }) else {
|
||||||
|
println!("No GDB!");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let new_gdb = match gdb {
|
||||||
|
GdbStubStateMachine::Running(inner) => {
|
||||||
|
println!("Reporting a STOP");
|
||||||
|
match inner.report_stop(
|
||||||
|
&mut target,
|
||||||
|
MultiThreadStopReason::Signal(Signal::EXC_BAD_ACCESS),
|
||||||
|
) {
|
||||||
|
Ok(new_gdb) => new_gdb,
|
||||||
|
Err(e) => {
|
||||||
|
println!("Unable to report stop: {:?}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GdbStubStateMachine::CtrlCInterrupt(_inner) => {
|
||||||
|
println!("GDB state was in CtrlCInterrupt, which shouldn't be possible!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GdbStubStateMachine::Disconnected(_inner) => {
|
||||||
|
println!("GDB state was in Disconnect, which shouldn't be possible!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GdbStubStateMachine::Idle(_inner) => {
|
||||||
|
println!("GDB state was in Idle, which shouldn't be possible!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
GDB_STATE = Some(XousDebugState {
|
GDB_STATE = Some(XousDebugState {
|
||||||
|
|
@ -650,28 +710,21 @@ fn advance_gdb_state(byte: Option<u8>) -> bool {
|
||||||
server: new_gdb,
|
server: new_gdb,
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
let uart = GdbUart::new(receive_irq).unwrap();
|
let uart = GdbUart::new(receive_irq).unwrap();
|
||||||
let mut target = XousTarget::new();
|
let mut target = XousTarget::new();
|
||||||
|
|
||||||
match GdbStubBuilder::new(uart)
|
let server = GdbStubBuilder::new(uart)
|
||||||
.with_packet_buffer(unsafe { &mut GDB_BUFFER })
|
.with_packet_buffer(unsafe { &mut GDB_BUFFER })
|
||||||
.build()
|
.build()
|
||||||
{
|
.expect("unable to build gdb server")
|
||||||
Ok(gdb) => match gdb.run_state_machine(&mut target) {
|
.run_state_machine(&mut target)
|
||||||
Ok(state) => unsafe {
|
.expect("unable to start gdb state machine");
|
||||||
GDB_STATE = Some(XousDebugState {
|
unsafe {
|
||||||
target: target,
|
GDB_STATE = Some(XousDebugState { target, server });
|
||||||
server: state,
|
|
||||||
});
|
|
||||||
HALTED = true;
|
HALTED = true;
|
||||||
},
|
|
||||||
Err(e) => println!("Unable to start GDB state machine: {}", e),
|
|
||||||
},
|
|
||||||
Err(e) => println!("Unable to start GDB server: {}", e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,12 @@ use crate::{
|
||||||
mem::MemoryManager,
|
mem::MemoryManager,
|
||||||
PID,
|
PID,
|
||||||
};
|
};
|
||||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
use utralib::generated::*;
|
use utralib::generated::*;
|
||||||
use xous_kernel::{MemoryFlags, MemoryType};
|
use xous_kernel::{MemoryFlags, MemoryType};
|
||||||
|
|
||||||
static UART_USAGE_COUNT: AtomicUsize = AtomicUsize::new(0);
|
static UART_CALLBACK_POINTER: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
static UART_ALLOCATED: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
/// UART virtual address.
|
/// UART virtual address.
|
||||||
///
|
///
|
||||||
|
|
@ -21,16 +22,41 @@ pub const APP_UART_ADDR: usize = 0xffcc_0000;
|
||||||
/// UART peripheral driver.
|
/// UART peripheral driver.
|
||||||
pub struct GdbUart {
|
pub struct GdbUart {
|
||||||
uart_csr: CSR<u32>,
|
uart_csr: CSR<u32>,
|
||||||
callback: fn(&mut Self),
|
}
|
||||||
|
|
||||||
|
fn gdbuart_isr(_irq_no: usize, _arg: *mut usize) {
|
||||||
|
let target = UART_CALLBACK_POINTER.load(Ordering::Relaxed);
|
||||||
|
// Return if uninitialized
|
||||||
|
if target == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cb = unsafe { core::mem::transmute::<_, fn(&mut GdbUart)>(target) };
|
||||||
|
cb(&mut GdbUart {
|
||||||
|
uart_csr: CSR::new(APP_UART_ADDR as *mut u32),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GdbUart {
|
impl GdbUart {
|
||||||
pub fn new(callback: fn(&mut Self)) -> Option<GdbUart> {
|
pub fn new(callback: fn(&mut Self)) -> Option<GdbUart> {
|
||||||
if UART_USAGE_COUNT
|
UART_CALLBACK_POINTER.store(callback as usize, Ordering::Relaxed);
|
||||||
.compare_exchange(0, 1, Ordering::Relaxed, Ordering::Relaxed)
|
|
||||||
|
Some(GdbUart {
|
||||||
|
uart_csr: CSR::new(APP_UART_ADDR as *mut u32),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enable(&mut self) {
|
||||||
|
self.allocate();
|
||||||
|
self.uart_csr.rmwf(utra::app_uart::EV_ENABLE_RX, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn allocate(&mut self) {
|
||||||
|
if UART_ALLOCATED
|
||||||
|
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
return None;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map the UART peripheral.
|
// Map the UART peripheral.
|
||||||
|
|
@ -46,38 +72,28 @@ impl GdbUart {
|
||||||
)
|
)
|
||||||
.expect("unable to map serial port")
|
.expect("unable to map serial port")
|
||||||
});
|
});
|
||||||
let mut gdb_uart = GdbUart {
|
|
||||||
uart_csr: CSR::new(APP_UART_ADDR as *mut u32),
|
|
||||||
callback,
|
|
||||||
};
|
|
||||||
|
|
||||||
xous_kernel::claim_interrupt(
|
xous_kernel::claim_interrupt(
|
||||||
utra::app_uart::APP_UART_IRQ,
|
utra::app_uart::APP_UART_IRQ,
|
||||||
GdbUart::irq,
|
gdbuart_isr,
|
||||||
(&mut gdb_uart as *mut GdbUart) as *mut usize,
|
core::ptr::null_mut(),
|
||||||
)
|
)
|
||||||
.expect("Couldn't claim debug interrupt");
|
.expect("Couldn't claim debug interrupt");
|
||||||
|
|
||||||
Some(gdb_uart)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enable(&mut self) {
|
#[allow(dead_code)]
|
||||||
self.uart_csr.rmwf(utra::app_uart::EV_ENABLE_RX, 1);
|
pub fn deallocate(&mut self) {
|
||||||
|
self.uart_csr.rmwf(utra::app_uart::EV_ENABLE_RX, 0);
|
||||||
|
|
||||||
|
// Note: This can cause an ABA error if it's multi-threaded and `.allocate()`
|
||||||
|
// is called at the same time as `.deallocate()`.
|
||||||
|
if UART_ALLOCATED
|
||||||
|
.compare_exchange(true, false, Ordering::Relaxed, Ordering::Relaxed)
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn irq(_irq_number: usize, arg: *mut usize) {
|
|
||||||
let uart = unsafe { &mut *(arg as *mut GdbUart) };
|
|
||||||
(uart.callback)(uart);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for GdbUart {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
if UART_USAGE_COUNT.fetch_sub(1, Ordering::Relaxed) != 1 {
|
|
||||||
panic!("UART ad a usage count of more than 1");
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Freeing interrupt since the count has gone to zero");
|
|
||||||
xous_kernel::rsyscall(xous_kernel::SysCall::FreeInterrupt(
|
xous_kernel::rsyscall(xous_kernel::SysCall::FreeInterrupt(
|
||||||
utra::app_uart::APP_UART_IRQ,
|
utra::app_uart::APP_UART_IRQ,
|
||||||
))
|
))
|
||||||
|
|
@ -116,13 +132,16 @@ impl SerialRead for GdbUart {
|
||||||
|
|
||||||
impl gdbstub::conn::Connection for GdbUart {
|
impl gdbstub::conn::Connection for GdbUart {
|
||||||
type Error = &'static str;
|
type Error = &'static str;
|
||||||
|
|
||||||
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
|
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
|
||||||
self.putc(byte);
|
self.putc(byte);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> Result<(), Self::Error> {
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_session_start(&mut self) -> Result<(), Self::Error> {
|
fn on_session_start(&mut self) -> Result<(), Self::Error> {
|
||||||
self.enable();
|
self.enable();
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -2255,7 +2255,6 @@ impl SystemServices {
|
||||||
|
|
||||||
#[cfg(feature = "gdb-stub")]
|
#[cfg(feature = "gdb-stub")]
|
||||||
pub fn pause_process_for_debug(&mut self, pid: PID) -> Result<(), xous_kernel::Error> {
|
pub fn pause_process_for_debug(&mut self, pid: PID) -> Result<(), xous_kernel::Error> {
|
||||||
println!("Pausing process {:?} for debug...", pid);
|
|
||||||
let (process_state, parent_pid) = {
|
let (process_state, parent_pid) = {
|
||||||
let process = self.get_process_mut(pid)?;
|
let process = self.get_process_mut(pid)?;
|
||||||
(process.state, process.ppid)
|
(process.state, process.ppid)
|
||||||
|
|
@ -2263,12 +2262,7 @@ impl SystemServices {
|
||||||
|
|
||||||
// Disable all interrupts that belong to this process
|
// Disable all interrupts that belong to this process
|
||||||
crate::irq::for_each_irq(|irq_no, irq_pid, _, _| {
|
crate::irq::for_each_irq(|irq_no, irq_pid, _, _| {
|
||||||
println!("Examining IRQ {}...", irq_no);
|
|
||||||
if pid == *irq_pid {
|
if pid == *irq_pid {
|
||||||
println!(
|
|
||||||
"Disabling IRQ {} since it's owned by process {:?}",
|
|
||||||
irq_no, pid
|
|
||||||
);
|
|
||||||
crate::arch::irq::disable_irq(irq_no);
|
crate::arch::irq::disable_irq(irq_no);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -2313,10 +2307,6 @@ impl SystemServices {
|
||||||
};
|
};
|
||||||
{
|
{
|
||||||
let process = self.get_process_mut(pid).unwrap();
|
let process = self.get_process_mut(pid).unwrap();
|
||||||
println!(
|
|
||||||
"Process {:?} went from {:?} to {:?}",
|
|
||||||
pid, process.state, new_process_state
|
|
||||||
);
|
|
||||||
// let old_state = process.state;
|
// let old_state = process.state;
|
||||||
process.state = new_process_state;
|
process.state = new_process_state;
|
||||||
// log_process_update(file!(), line!(), process, old_state);
|
// log_process_update(file!(), line!(), process, old_state);
|
||||||
|
|
@ -2328,7 +2318,6 @@ impl SystemServices {
|
||||||
#[cfg(feature = "gdb-stub")]
|
#[cfg(feature = "gdb-stub")]
|
||||||
pub fn resume_process_from_debug(&mut self, pid: PID) -> Result<(), xous_kernel::Error> {
|
pub fn resume_process_from_debug(&mut self, pid: PID) -> Result<(), xous_kernel::Error> {
|
||||||
let process = self.get_process_mut(pid)?;
|
let process = self.get_process_mut(pid)?;
|
||||||
let old_state = process.state;
|
|
||||||
process.state = match process.state {
|
process.state = match process.state {
|
||||||
ProcessState::Allocated => ProcessState::Allocated,
|
ProcessState::Allocated => ProcessState::Allocated,
|
||||||
ProcessState::Free => ProcessState::Free,
|
ProcessState::Free => ProcessState::Free,
|
||||||
|
|
@ -2342,10 +2331,6 @@ impl SystemServices {
|
||||||
ProcessState::Debug(tids) => ProcessState::Ready(tids),
|
ProcessState::Debug(tids) => ProcessState::Ready(tids),
|
||||||
ProcessState::Running(tids) => ProcessState::Running(tids),
|
ProcessState::Running(tids) => ProcessState::Running(tids),
|
||||||
};
|
};
|
||||||
println!(
|
|
||||||
"Resuming process {:?} from debug, going from {:?} to {:?}",
|
|
||||||
pid, old_state, process.state
|
|
||||||
);
|
|
||||||
|
|
||||||
// Resume all interrupts that belong to this process
|
// Resume all interrupts that belong to this process
|
||||||
crate::irq::for_each_irq(|irq, irq_pid, _, _| {
|
crate::irq::for_each_irq(|irq, irq_pid, _, _| {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue