Use logging

This commit is contained in:
2025-01-14 23:33:30 -05:00
parent 102aff1580
commit f031bb17b2
7 changed files with 148 additions and 17 deletions
+13 -4
View File
@@ -12,6 +12,7 @@ use tokio::{
select,
sync::{mpsc, oneshot},
};
use tracing::{debug, enabled, error, info, Level};
use crate::emulator::{
DebugEvent, DebugStopReason, EmulatorClient, EmulatorCommand, SimId, VBWatchpointType,
@@ -78,15 +79,19 @@ async fn run_server(
port: u16,
status: &Mutex<GdbServerStatus>,
) {
info!("Connecting to debugger on port {port}...");
let Some(stream) = try_connect(port, status).await else {
return;
};
info!("Connected!");
let mut connection = GdbConnection::new(sim_id, client);
match connection.run(stream).await {
Ok(()) => {
info!("Finished debugging.");
*status.lock().unwrap() = GdbServerStatus::Stopped;
}
Err(error) => {
error!(%error, "Error from debugger.");
*status.lock().unwrap() = GdbServerStatus::Error(error.to_string());
}
}
@@ -97,6 +102,7 @@ async fn try_connect(port: u16, status: &Mutex<GdbServerStatus>) -> Option<TcpSt
let listener = match TcpListener::bind(("127.0.0.1", port)).await {
Ok(l) => l,
Err(err) => {
error!(%err, "Could not open port.");
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
return None;
}
@@ -107,6 +113,7 @@ async fn try_connect(port: u16, status: &Mutex<GdbServerStatus>) -> Option<TcpSt
Some(stream)
}
Err(err) => {
error!(%err, "Could not connect to debugger.");
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
None
}
@@ -169,9 +176,11 @@ impl GdbConnection {
};
if let Some(res) = response {
let buffer = res.finish();
match std::str::from_utf8(&buffer) {
Ok(text) => println!("response: {text}"),
Err(_) => println!("response: {buffer:02x?}"),
if enabled!(Level::DEBUG) {
match std::str::from_utf8(&buffer) {
Ok(text) => debug!("response: {text}"),
Err(_) => debug!("response: {buffer:02x?}"),
}
}
tx.write_all(&buffer).await?;
self.response_buf = Some(buffer);
@@ -196,7 +205,7 @@ impl GdbConnection {
}
fn handle_request(&mut self, mut req: Request<'_>) -> Result<Option<Response>> {
println!("received {:02x?}", req);
debug!("received {:02x?}", req);
if req.kind == RequestKind::Signal {
self.client