Prettify the output a little

This commit is contained in:
Gwilym Inzani 2024-04-01 13:43:08 +01:00
parent 3ab6d08c7f
commit 8453b46eab
2 changed files with 40 additions and 8 deletions

View file

@ -10,7 +10,10 @@ repository = "https://github.com/agbrs/agb"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
clap = { version = "4", features = ["derive"] } clap = { version = "4", features = ["derive"] }
addr2line = "0.21" addr2line = { version = "0.21", default-features = false, features = [
"rustc-demangle",
"std-object",
] }
[profile.dev] [profile.dev]
opt-level = 3 opt-level = 3

View file

@ -13,6 +13,20 @@ struct Args {
dump: String, dump: String,
} }
struct Location {
filename: String,
line: u32,
}
impl Default for Location {
fn default() -> Self {
Self {
filename: "??".to_string(),
line: 0,
}
}
}
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let cli = Args::parse(); let cli = Args::parse();
@ -21,14 +35,29 @@ fn main() -> anyhow::Result<()> {
let ctx = addr2line::Context::new(&object)?; let ctx = addr2line::Context::new(&object)?;
if let Some(location) = ctx.find_location(parse_address(&cli.dump)?)? { let mut frames = ctx
let file = location.file.unwrap_or("unknown file"); .find_frames(parse_address(&cli.dump)?)
let line = location .skip_all_loads()?;
.line
.map(|line| line.to_string())
.unwrap_or_else(|| "??".to_owned());
println!("{file}:{line}"); while let Some(frame) = frames.next()? {
let function_name = if let Some(func) = frame.function {
func.demangle()?.into_owned()
} else {
"unknown function".to_string()
};
let location = frame
.location
.map(|location| Location {
filename: location.file.unwrap_or("??").to_owned(),
line: location.line.unwrap_or(0),
})
.unwrap_or_default();
println!(
"{}:{} ({})",
location.filename, location.line, function_name
);
} }
Ok(()) Ok(())