Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve resolving symbols #581

Merged
merged 5 commits into from
Feb 12, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `espflash` will now exit with an error if `defmt` is selected but not usable (#524)
- Unify configuration methods (#551)
- MSRV bumped to `1.73.0` (#578)
- Improved symbol resolving (#581)

### Removed

Expand Down
21 changes: 14 additions & 7 deletions espflash/src/cli/monitor/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ fn resolve_addresses(
let name = symbols.get_name(addr);
let location = symbols.get_location(addr);

let name = name.as_deref().unwrap_or("??");
let output = if let Some((file, line_num)) = location {
format!("{matched} - {name}\r\n at {file}:{line_num}\r\n")
} else {
format!("{matched} - {name}\r\n at ??:??\r\n")
};
if let Some(name) = name {
let output = if line.trim() == format!("0x{:x}", addr) {
if let Some((file, line_num)) = location {
format!("{name}\r\n at {file}:{line_num}\r\n")
} else {
format!("{name}\r\n at ??:??\r\n")
}
} else if let Some((file, line_num)) = location {
format!("{matched} - {name}\r\n at {file}:{line_num}\r\n")
} else {
format!("{matched} - {name}\r\n at ??:??\r\n")
};

out.queue(PrintStyledContent(output.with(Color::Yellow)))?;
out.queue(PrintStyledContent(output.with(Color::Yellow)))?;
}
}

Ok(())
Expand Down
17 changes: 12 additions & 5 deletions espflash/src/cli/monitor/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;

use addr2line::{
gimli::{EndianRcSlice, RunTimeEndian},
object::{read::File, Object},
object::{read::File, Object, ObjectSegment},
Context, LookupResult,
};

Expand All @@ -23,6 +23,13 @@ impl<'sym> Symbols<'sym> {

/// Returns the name of the function at the given address, if one can be found.
pub fn get_name(&self, addr: u64) -> Option<String> {
// no need to try an address not contained in any segment
if !self.file.segments().any(|segment| {
(segment.address()..(segment.address() + segment.size())).contains(&addr)
}) {
return None;
}

// The basic steps here are:
// 1. find which frame `addr` is in
// 2. look up and demangle the function name
Expand All @@ -44,10 +51,10 @@ impl<'sym> Symbols<'sym> {
.and_then(|name| name.demangle().map(|s| s.into_owned()).ok())
})
.or_else(|| {
self.file
.symbol_map()
.get(addr)
.map(|sym| sym.name().to_string())
self.file.symbol_map().get(addr).map(|sym| {
addr2line::demangle_auto(std::borrow::Cow::Borrowed(sym.name()), None)
.to_string()
})
})
}

Expand Down
Loading