Skip to content

Commit b43dfaa

Browse files
authored
bin: prefer symbol table names over DWARF names (#332)
Prefer the symbol table over the DWARF name because: - the symbol can include a clone suffix - llvm may omit the linkage name in the DWARF with -g1 These two cases are tested in testoutput/dwarf/cold and testoutput/dwarf/base-clang-g1. This also fixes the case where the DWARF includes an inlined function with no name, and we wrongly used the symbol table. I don't have a test case for this.
1 parent 9055f28 commit b43dfaa

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ codegen-units = 1
5959
default = ["rustc-demangle", "cpp_demangle", "loader", "fallible-iterator", "smallvec"]
6060
std = ["gimli/std"]
6161
loader = ["std", "dep:object", "dep:memmap2", "dep:typed-arena"]
62-
bin = ["loader", "rustc-demangle", "cpp_demangle", "smallvec", "dep:clap"]
62+
bin = ["loader", "rustc-demangle", "cpp_demangle", "fallible-iterator", "smallvec", "dep:clap"]
6363
all = ["bin"]
6464

6565
# Use of --all-features is not supported.

src/bin/addr2line.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use fallible_iterator::FallibleIterator;
12
use std::borrow::Cow;
23
use std::io::{BufRead, Lines, StdinLock, Write};
34
use std::path::{Path, PathBuf};
@@ -210,7 +211,7 @@ fn main() {
210211
if opts.do_functions || opts.do_inlines {
211212
let mut printed_anything = false;
212213
if let Some(probe) = probe {
213-
let mut frames = ctx.find_frames(probe).unwrap();
214+
let mut frames = ctx.find_frames(probe).unwrap().peekable();
214215
let mut first = true;
215216
while let Some(frame) = frames.next().unwrap() {
216217
if opts.pretty && !first {
@@ -219,15 +220,25 @@ fn main() {
219220
first = false;
220221

221222
if opts.do_functions {
222-
if let Some(func) = frame.function {
223+
// Only use the symbol table if this isn't an inlined function.
224+
let symbol = if matches!(frames.peek(), Ok(None)) {
225+
ctx.find_symbol(probe)
226+
} else {
227+
None
228+
};
229+
if symbol.is_some() {
230+
// Prefer the symbol table over the DWARF name because:
231+
// - the symbol can include a clone suffix
232+
// - llvm may omit the linkage name in the DWARF with -g1
233+
print_function(symbol, None, opts.demangle);
234+
} else if let Some(func) = frame.function {
223235
print_function(
224236
func.raw_name().ok().as_deref(),
225237
func.language,
226238
opts.demangle,
227239
);
228240
} else {
229-
let name = ctx.find_symbol(probe);
230-
print_function(name, None, opts.demangle);
241+
print_function(None, None, opts.demangle);
231242
}
232243

233244
if opts.pretty {

testoutput/dwarf/base-clang-g1

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@ _ZN3Foo3barEPKc
1111
_ZN3Foo3barEPKc
1212
/object/testfiles/dwarf/base.cpp:16
1313
0x00000000000011a0
14-
foo
14+
_ZN3Foo3fooEi
1515
/object/testfiles/dwarf/base.cpp:19
1616
0x00000000000011ac
17-
foo
17+
_ZN3Foo3fooEi
1818
/object/testfiles/dwarf/base.cpp:20
1919
0x00000000000011b0
20-
foo
20+
_ZN3Foo3fooEi
2121
/object/testfiles/dwarf/base.cpp:?
2222
0x00000000000011d0
2323
bar
2424
/object/testfiles/dwarf/base.cpp:15
25-
foo
25+
_ZN3Foo3fooEi
2626
/object/testfiles/dwarf/base.cpp:21
2727
0x000000000000120e
2828
bar
2929
/object/testfiles/dwarf/base.cpp:14
30-
foo
30+
_ZN3Foo3fooEi
3131
/object/testfiles/dwarf/base.cpp:21
3232
0x000000000000121c
33-
foo
33+
_ZN3Foo3fooEi
3434
/object/testfiles/dwarf/base.cpp:?
3535
0x0000000000001230
3636
bar
3737
/object/testfiles/dwarf/base.cpp:15
38-
foo
38+
_ZN3Foo3fooEi
3939
/object/testfiles/dwarf/base.cpp:23
4040
0x000000000000126e
4141
bar
4242
/object/testfiles/dwarf/base.cpp:14
43-
foo
43+
_ZN3Foo3fooEi
4444
/object/testfiles/dwarf/base.cpp:23
4545
0x000000000000127a
46-
foo
46+
_ZN3Foo3fooEi
4747
/object/testfiles/dwarf/base.cpp:24
4848
0x0000000000001280
4949
main

testoutput/dwarf/cold

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
0x0000000000001060
2-
_ZN3Foo3fooEi
2+
_ZN3Foo3fooEi.cold
33
/object/testfiles/dwarf/cold.cpp:29
44
0x0000000000001067
5-
_ZN3Foo3fooEi
5+
_ZN3Foo3fooEi.cold
66
/object/testfiles/dwarf/cold.cpp:29
77
0x000000000000106e
88
_ZN3Foo3barEPKc
99
/object/testfiles/dwarf/cold.cpp:20
10-
_ZN3Foo3fooEi
10+
_ZN3Foo3fooEi.cold
1111
/object/testfiles/dwarf/cold.cpp:29
1212
0x0000000000001075
1313
_ZN3Foo3barEPKc
1414
/object/testfiles/dwarf/cold.cpp:21
15-
_ZN3Foo3fooEi
15+
_ZN3Foo3fooEi.cold
1616
/object/testfiles/dwarf/cold.cpp:29
1717
0x0000000000001078
1818
_ZN3Foo3barEPKc
1919
/object/testfiles/dwarf/cold.cpp:20
20-
_ZN3Foo3fooEi
20+
_ZN3Foo3fooEi.cold
2121
/object/testfiles/dwarf/cold.cpp:29
2222
0x000000000000107c
2323
_ZN3Foo3barEPKc
2424
/object/testfiles/dwarf/cold.cpp:20
25-
_ZN3Foo3fooEi
25+
_ZN3Foo3fooEi.cold
2626
/object/testfiles/dwarf/cold.cpp:29
2727
0x0000000000001081
28-
_ZN3Foo3fooEi
28+
_ZN3Foo3fooEi.cold
2929
/object/testfiles/dwarf/cold.cpp:30
3030
0x0000000000001082
3131
_ZN3Foo3barEPKc

0 commit comments

Comments
 (0)