Skip to content

Commit 9592c92

Browse files
authored
Improve resolving symbols (#581)
* Improve resolving symbols * CHANGELOG.md * Appease Clippy * Don't repeat the address if the line only consists of it * Make Clippy happy, again
1 parent 0ae82dc commit 9592c92

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4141
- `espflash` will now exit with an error if `defmt` is selected but not usable (#524)
4242
- Unify configuration methods (#551)
4343
- MSRV bumped to `1.73.0` (#578)
44+
- Improved symbol resolving (#581)
4445

4546
### Removed
4647

espflash/src/cli/monitor/parser/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ fn resolve_addresses(
3838
let name = symbols.get_name(addr);
3939
let location = symbols.get_location(addr);
4040

41-
let name = name.as_deref().unwrap_or("??");
42-
let output = if let Some((file, line_num)) = location {
43-
format!("{matched} - {name}\r\n at {file}:{line_num}\r\n")
44-
} else {
45-
format!("{matched} - {name}\r\n at ??:??\r\n")
46-
};
41+
if let Some(name) = name {
42+
let output = if line.trim() == format!("0x{:x}", addr) {
43+
if let Some((file, line_num)) = location {
44+
format!("{name}\r\n at {file}:{line_num}\r\n")
45+
} else {
46+
format!("{name}\r\n at ??:??\r\n")
47+
}
48+
} else if let Some((file, line_num)) = location {
49+
format!("{matched} - {name}\r\n at {file}:{line_num}\r\n")
50+
} else {
51+
format!("{matched} - {name}\r\n at ??:??\r\n")
52+
};
4753

48-
out.queue(PrintStyledContent(output.with(Color::Yellow)))?;
54+
out.queue(PrintStyledContent(output.with(Color::Yellow)))?;
55+
}
4956
}
5057

5158
Ok(())

espflash/src/cli/monitor/symbols.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::error::Error;
22

33
use addr2line::{
44
gimli::{EndianRcSlice, RunTimeEndian},
5-
object::{read::File, Object},
5+
object::{read::File, Object, ObjectSegment},
66
Context, LookupResult,
77
};
88

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

2424
/// Returns the name of the function at the given address, if one can be found.
2525
pub fn get_name(&self, addr: u64) -> Option<String> {
26+
// no need to try an address not contained in any segment
27+
if !self.file.segments().any(|segment| {
28+
(segment.address()..(segment.address() + segment.size())).contains(&addr)
29+
}) {
30+
return None;
31+
}
32+
2633
// The basic steps here are:
2734
// 1. find which frame `addr` is in
2835
// 2. look up and demangle the function name
@@ -44,10 +51,10 @@ impl<'sym> Symbols<'sym> {
4451
.and_then(|name| name.demangle().map(|s| s.into_owned()).ok())
4552
})
4653
.or_else(|| {
47-
self.file
48-
.symbol_map()
49-
.get(addr)
50-
.map(|sym| sym.name().to_string())
54+
self.file.symbol_map().get(addr).map(|sym| {
55+
addr2line::demangle_auto(std::borrow::Cow::Borrowed(sym.name()), None)
56+
.to_string()
57+
})
5158
})
5259
}
5360

0 commit comments

Comments
 (0)