From 096ead5c7d508e45f2ca998ec58c76aedfd3b752 Mon Sep 17 00:00:00 2001 From: TurtleP Date: Sun, 8 Feb 2026 13:46:08 -0500 Subject: [PATCH] add log option of remote debugging --- src/commands/debug.rs | 17 +++++++++++++++-- src/platforms/addr2line.rs | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/commands/debug.rs b/src/commands/debug.rs index a920ae9..207ba1d 100644 --- a/src/commands/debug.rs +++ b/src/commands/debug.rs @@ -1,6 +1,8 @@ use std::{ + fs::File, io::{Write, stdout}, net::Ipv4Addr, + path::PathBuf, }; use anyhow::Result; @@ -13,7 +15,10 @@ use crate::platforms::addr2line::find_candidate; #[derive(Subcommand)] pub enum DebugCmd { /// Attach to a remote target - Attach { address: String }, + Attach { + address: String, + logfile: Option, + }, /// Debug a local binary using addr2line Addr2line { filepath: String, @@ -23,14 +28,22 @@ pub enum DebugCmd { pub fn handle_debug(command: DebugCmd, config: Config) -> Result<()> { match command { - DebugCmd::Attach { address } => { + DebugCmd::Attach { address, logfile } => { let target = match config.get(&address) { Some(addr) => *addr, None => address.parse::()?, }; + let mut file = if let Some(path) = logfile { + Some(File::create(path)?) + } else { + None + }; let mut socket = Socket::new((target, config.get_port()))?; while let Some(data) = socket.read()? { stdout().write_all(data)?; + if let Some(f) = file.as_mut() { + f.write_all(data)?; + } } } DebugCmd::Addr2line { diff --git a/src/platforms/addr2line.rs b/src/platforms/addr2line.rs index 6b8aeec..5f8f683 100644 --- a/src/platforms/addr2line.rs +++ b/src/platforms/addr2line.rs @@ -12,6 +12,18 @@ pub struct Candidate { args: &'static [&'static str], search_path: &'static str, device: &'static str, + runtime_base: u32, + elf_text_base: u32, +} + +impl Candidate { + pub fn offset_address(&self, address: &str) -> Result { + let value = u32::from_str_radix(address.trim_start_matches("0x"), 16)?; + Ok(format!( + "0x{:08X}", + value - self.runtime_base + self.elf_text_base + )) + } } const CANDIDATES: [Candidate; 3] = [ @@ -21,6 +33,8 @@ const CANDIDATES: [Candidate; 3] = [ args: &["arm"], search_path: "devkitARM/bin", device: "Nintendo 3DS", + runtime_base: 0, + elf_text_base: 0, }, Candidate { binary: "aarch64-none-elf-addr2line", @@ -28,6 +42,8 @@ const CANDIDATES: [Candidate; 3] = [ args: &[""], search_path: "devkitA64/bin", device: "Nintendo Switch", + runtime_base: 0, + elf_text_base: 0, }, Candidate { binary: "powerpc-eabi-addr2line", @@ -35,6 +51,8 @@ const CANDIDATES: [Candidate; 3] = [ args: &[""], search_path: "devkitPPC/bin", device: "Nintendo Wiiᵘ", + runtime_base: 0x0C000000, + elf_text_base: 0x02000000, }, ]; @@ -83,7 +101,7 @@ impl Candidate { .arg(filepath); for address in addresses { - command.arg(address); + command.arg(self.offset_address(address)?); } let output = match command.output() {