Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/commands/debug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{
fs::File,
io::{Write, stdout},
net::Ipv4Addr,
path::PathBuf,
};

use anyhow::Result;
Expand All @@ -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<PathBuf>,
},
/// Debug a local binary using addr2line
Addr2line {
filepath: String,
Expand All @@ -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::<Ipv4Addr>()?,
};
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 {
Expand Down
20 changes: 19 additions & 1 deletion src/platforms/addr2line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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] = [
Expand All @@ -21,20 +33,26 @@ 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",
magic: b"switch_crt0.o",
args: &[""],
search_path: "devkitA64/bin",
device: "Nintendo Switch",
runtime_base: 0,
elf_text_base: 0,
},
Candidate {
binary: "powerpc-eabi-addr2line",
magic: b"crt0_rpx.o",
args: &[""],
search_path: "devkitPPC/bin",
device: "Nintendo Wiiᵘ",
runtime_base: 0x0C000000,
elf_text_base: 0x02000000,
},
];

Expand Down Expand Up @@ -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() {
Expand Down