mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
kernel: support pluggable debug printing
This allows us to support different printers in the kernel. For example, this can be used to swap out the println!() function to print to GDB when a debugger is attached. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
931b992063
commit
e4d02301ee
2 changed files with 75 additions and 35 deletions
|
|
@ -2,6 +2,7 @@ use gdbstub::common::Tid;
|
||||||
use gdbstub::target::ext::base::multithread::{
|
use gdbstub::target::ext::base::multithread::{
|
||||||
GdbInterrupt, MultiThreadOps, ResumeAction, ThreadStopReason,
|
GdbInterrupt, MultiThreadOps, ResumeAction, ThreadStopReason,
|
||||||
};
|
};
|
||||||
|
use gdbstub::{DisconnectReason, GdbStubError};
|
||||||
|
|
||||||
use gdbstub::state_machine::GdbStubStateMachine;
|
use gdbstub::state_machine::GdbStubStateMachine;
|
||||||
use gdbstub::target::ext::base::BaseOps;
|
use gdbstub::target::ext::base::BaseOps;
|
||||||
|
|
@ -40,12 +41,12 @@ impl Target for XousTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MultiThreadOps for XousTarget {
|
impl MultiThreadOps for XousTarget {
|
||||||
#[inline(never)]
|
|
||||||
fn resume(
|
fn resume(
|
||||||
&mut self,
|
&mut self,
|
||||||
default_resume_action: ResumeAction,
|
default_resume_action: ResumeAction,
|
||||||
_gdb_interrupt: GdbInterrupt<'_>,
|
_gdb_interrupt: GdbInterrupt<'_>,
|
||||||
) -> Result<Option<ThreadStopReason<u32>>, Self::Error> {
|
) -> Result<Option<ThreadStopReason<u32>>, Self::Error> {
|
||||||
|
unsafe { HALTED = false };
|
||||||
match default_resume_action {
|
match default_resume_action {
|
||||||
ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
|
ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
|
||||||
Err("single-stepping not supported")?
|
Err("single-stepping not supported")?
|
||||||
|
|
@ -59,17 +60,14 @@ impl MultiThreadOps for XousTarget {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
|
||||||
fn clear_resume_actions(&mut self) -> Result<(), Self::Error> {
|
fn clear_resume_actions(&mut self) -> Result<(), Self::Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
|
||||||
fn set_resume_action(&mut self, _tid: Tid, _action: ResumeAction) -> Result<(), Self::Error> {
|
fn set_resume_action(&mut self, _tid: Tid, _action: ResumeAction) -> Result<(), Self::Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
|
||||||
fn read_registers(
|
fn read_registers(
|
||||||
&mut self,
|
&mut self,
|
||||||
regs: &mut gdbstub_arch::riscv::reg::RiscvCoreRegs<u32>,
|
regs: &mut gdbstub_arch::riscv::reg::RiscvCoreRegs<u32>,
|
||||||
|
|
@ -105,7 +103,6 @@ impl MultiThreadOps for XousTarget {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
|
||||||
fn write_registers(
|
fn write_registers(
|
||||||
&mut self,
|
&mut self,
|
||||||
regs: &gdbstub_arch::riscv::reg::RiscvCoreRegs<u32>,
|
regs: &gdbstub_arch::riscv::reg::RiscvCoreRegs<u32>,
|
||||||
|
|
@ -139,7 +136,6 @@ impl MultiThreadOps for XousTarget {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
|
||||||
fn read_addrs(
|
fn read_addrs(
|
||||||
&mut self,
|
&mut self,
|
||||||
start_addr: u32,
|
start_addr: u32,
|
||||||
|
|
@ -173,7 +169,6 @@ impl MultiThreadOps for XousTarget {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
|
||||||
fn write_addrs(
|
fn write_addrs(
|
||||||
&mut self,
|
&mut self,
|
||||||
start_addr: u32,
|
start_addr: u32,
|
||||||
|
|
@ -211,7 +206,6 @@ impl MultiThreadOps for XousTarget {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
|
||||||
fn list_active_threads(
|
fn list_active_threads(
|
||||||
&mut self,
|
&mut self,
|
||||||
register_thread: &mut dyn FnMut(Tid),
|
register_thread: &mut dyn FnMut(Tid),
|
||||||
|
|
@ -229,7 +223,7 @@ impl MultiThreadOps for XousTarget {
|
||||||
.activate()
|
.activate()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
crate::arch::process::Process::current().for_each_thread_mut(|tid, _thr| {
|
crate::arch::process::Process::current().for_each_thread_mut(|tid, _thr| {
|
||||||
// gprintln!("Registering thread {}", tid);
|
println!("Registering thread {}", tid);
|
||||||
register_thread(Tid::new(tid).unwrap());
|
register_thread(Tid::new(tid).unwrap());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -245,7 +239,6 @@ impl MultiThreadOps for XousTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle(b: u8) -> bool {
|
pub fn handle(b: u8) -> bool {
|
||||||
use gdbstub::{DisconnectReason, GdbStubError};
|
|
||||||
if let Some(XousDebugState {
|
if let Some(XousDebugState {
|
||||||
mut target,
|
mut target,
|
||||||
server: gdb,
|
server: gdb,
|
||||||
|
|
@ -255,7 +248,7 @@ pub fn handle(b: u8) -> bool {
|
||||||
let new_gdb = match gdb {
|
let new_gdb = match gdb {
|
||||||
GdbStubStateMachine::Pump(gdb_state) => match gdb_state.pump(&mut target, b) {
|
GdbStubStateMachine::Pump(gdb_state) => match gdb_state.pump(&mut target, b) {
|
||||||
// Remote disconnected -- leave the `GDB_SERVER` as `None`.
|
// Remote disconnected -- leave the `GDB_SERVER` as `None`.
|
||||||
Ok((gdb, Some(DisconnectReason::TargetTerminated(r)))) => {
|
Ok((gdb, Some(DisconnectReason::TargetTerminated(_r)))) => {
|
||||||
// gprintln!("Target halted: {}", r);
|
// gprintln!("Target halted: {}", r);
|
||||||
crate::services::SystemServices::with_mut(|system_services| {
|
crate::services::SystemServices::with_mut(|system_services| {
|
||||||
system_services
|
system_services
|
||||||
|
|
@ -271,14 +264,17 @@ pub fn handle(b: u8) -> bool {
|
||||||
// DisconnectReason::TargetTerminated(_) => unreachable!(),
|
// DisconnectReason::TargetTerminated(_) => unreachable!(),
|
||||||
// DisconnectReason::Kill => gprintln!("GDB sent a kill command"),
|
// DisconnectReason::Kill => gprintln!("GDB sent a kill command"),
|
||||||
// }
|
// }
|
||||||
|
cleanup();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Err(GdbStubError::TargetError(e)) => {
|
Err(GdbStubError::TargetError(e)) => {
|
||||||
// gprintln!("Target raised a fatal error: {}", e);
|
println!("Target raised a fatal error: {}", e);
|
||||||
|
cleanup();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// gprintln!("gdbstub internal error: {}", e);
|
println!("gdbstub internal error: {}", e);
|
||||||
|
cleanup();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Ok((gdb, None)) => gdb,
|
Ok((gdb, None)) => gdb,
|
||||||
|
|
@ -295,11 +291,13 @@ pub fn handle(b: u8) -> bool {
|
||||||
gdb
|
gdb
|
||||||
}
|
}
|
||||||
Ok((_, Some(disconnect_reason))) => {
|
Ok((_, Some(disconnect_reason))) => {
|
||||||
// gprintln!("client disconnected: {:?}", disconnect_reason);
|
println!("client disconnected: {:?}", disconnect_reason);
|
||||||
|
cleanup();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// gprintln!("deferred_stop_reason_error: {:?}", e);
|
println!("deferred_stop_reason_error: {:?}", e);
|
||||||
|
cleanup();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -328,9 +326,53 @@ pub fn setup() {
|
||||||
target: XousTarget::new(),
|
target: XousTarget::new(),
|
||||||
server: state,
|
server: state,
|
||||||
});
|
});
|
||||||
|
super::DEBUG_OUTPUT = Some(&mut GUART);
|
||||||
|
HALTED = true;
|
||||||
},
|
},
|
||||||
Err(e) => println!("Unable to start GDB state machine: {}", e),
|
Err(e) => println!("Unable to start GDB state machine: {}", e),
|
||||||
},
|
},
|
||||||
Err(e) => println!("Unable to start GDB server: {}", e),
|
Err(e) => println!("Unable to start GDB server: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cleanup() {
|
||||||
|
unsafe { super::DEBUG_OUTPUT = Some(&mut super::UART) };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Support printf() when running under a debugger
|
||||||
|
pub struct GUart {}
|
||||||
|
static mut GUART: GUart = GUart {};
|
||||||
|
static mut HALTED: bool = false;
|
||||||
|
|
||||||
|
impl core::fmt::Write for GUart {
|
||||||
|
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
|
||||||
|
fn putc(checksum: u8, c: u8) -> u8 {
|
||||||
|
unsafe { super::UART.putc(c) };
|
||||||
|
checksum.wrapping_add(c)
|
||||||
|
}
|
||||||
|
fn puth(checksum: u8, c: u8) -> u8 {
|
||||||
|
let hex = b"0123456789abcdef";
|
||||||
|
let c = c as usize;
|
||||||
|
let checksum = putc(checksum, hex[(c >> 4) & 0xf]);
|
||||||
|
putc(checksum, hex[(c >> 0) & 0xf])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can't write when we're halted
|
||||||
|
if unsafe { HALTED } {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut checksum = 0u8;
|
||||||
|
putc(0, b'$');
|
||||||
|
|
||||||
|
checksum = putc(checksum, b'O');
|
||||||
|
for c in s.as_bytes() {
|
||||||
|
checksum = puth(checksum, *c);
|
||||||
|
}
|
||||||
|
|
||||||
|
putc(0, b'#');
|
||||||
|
puth(0, checksum);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io>
|
// SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io>
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
#[cfg(baremetal)]
|
|
||||||
use core::fmt::{Error, Write};
|
use core::fmt::{Error, Write};
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
use utralib::generated::*;
|
use utralib::generated::*;
|
||||||
|
|
||||||
|
pub static mut DEBUG_OUTPUT: Option<&'static mut dyn Write> = None;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
#[cfg(all(
|
#[cfg(all(
|
||||||
not(test),
|
not(test),
|
||||||
|
|
@ -15,15 +16,6 @@ use utralib::generated::*;
|
||||||
pub mod debug_print_hardware {
|
pub mod debug_print_hardware {
|
||||||
// the HW device mapping is done in main.rs/init(); the virtual address has to be in the top 4MiB as it is the only page shared among all processes
|
// the HW device mapping is done in main.rs/init(); the virtual address has to be in the top 4MiB as it is the only page shared among all processes
|
||||||
pub const SUPERVISOR_UART_ADDR: *mut usize = 0xffcf_0000 as *mut usize; // see https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
pub const SUPERVISOR_UART_ADDR: *mut usize = 0xffcf_0000 as *mut usize; // see https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! print
|
|
||||||
{
|
|
||||||
($($args:tt)+) => ({
|
|
||||||
use core::fmt::Write;
|
|
||||||
let _ = write!(crate::debug::Uart {}, $($args)+);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[cfg(all(
|
#[cfg(all(
|
||||||
not(test),
|
not(test),
|
||||||
|
|
@ -32,15 +24,15 @@ pub mod debug_print_hardware {
|
||||||
))]
|
))]
|
||||||
pub use crate::debug::debug_print_hardware::SUPERVISOR_UART_ADDR;
|
pub use crate::debug::debug_print_hardware::SUPERVISOR_UART_ADDR;
|
||||||
|
|
||||||
#[cfg(all(
|
#[cfg(all(not(test), baremetal))]
|
||||||
not(test),
|
|
||||||
baremetal,
|
|
||||||
not(any(feature = "debug-print", feature = "print-panics"))
|
|
||||||
))]
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! print {
|
macro_rules! print {
|
||||||
($($args:tt)+) => {{
|
($($args:tt)+) => {{
|
||||||
()
|
unsafe {
|
||||||
|
if let Some(mut stream) = crate::debug::DEBUG_OUTPUT.as_mut() {
|
||||||
|
write!(&mut stream, $($args)+).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,7 +54,7 @@ macro_rules! println
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
pub struct Uart {}
|
pub struct Uart {}
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
static mut INITIALIZED: bool = false;
|
pub static mut UART: Uart = Uart {};
|
||||||
|
|
||||||
#[cfg(all(baremetal, feature = "wrap-print"))]
|
#[cfg(all(baremetal, feature = "wrap-print"))]
|
||||||
static mut CHAR_COUNT: usize = 0;
|
static mut CHAR_COUNT: usize = 0;
|
||||||
|
|
@ -71,13 +63,13 @@ static mut CHAR_COUNT: usize = 0;
|
||||||
impl Uart {
|
impl Uart {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn init(self) {
|
pub fn init(self) {
|
||||||
unsafe { INITIALIZED = true };
|
unsafe { DEBUG_OUTPUT = Some(&mut UART) };
|
||||||
let mut uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
let mut uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
||||||
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
|
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn putc(&self, c: u8) {
|
pub fn putc(&self, c: u8) {
|
||||||
if unsafe { INITIALIZED != true } {
|
if unsafe { DEBUG_OUTPUT.is_none() } {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,6 +99,9 @@ impl Uart {
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn getc(&self) -> Option<u8> {
|
pub fn getc(&self) -> Option<u8> {
|
||||||
|
if unsafe { DEBUG_OUTPUT.is_none() } {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
let uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
let uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
||||||
// If EV_PENDING_RX is 1, return the pending character.
|
// If EV_PENDING_RX is 1, return the pending character.
|
||||||
// Otherwise, return None.
|
// Otherwise, return None.
|
||||||
|
|
@ -117,6 +112,9 @@ impl Uart {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn acknowledge_irq(&self) {
|
pub fn acknowledge_irq(&self) {
|
||||||
|
if unsafe { DEBUG_OUTPUT.is_none() } {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let mut uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
let mut uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
||||||
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
|
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +128,7 @@ impl gdbstub::Connection for Uart {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
|
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
|
||||||
if unsafe { INITIALIZED != true } {
|
if unsafe { DEBUG_OUTPUT.is_none() } {
|
||||||
Err(())?;
|
Err(())?;
|
||||||
}
|
}
|
||||||
let mut uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
let mut uart_csr = CSR::new(crate::debug::SUPERVISOR_UART_ADDR as *mut u32);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue