Skip to content

Commit

Permalink
bin: prefer symbol table names over DWARF names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
philipc committed Oct 4, 2024
1 parent 9055f28 commit ee4a8fd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ codegen-units = 1
default = ["rustc-demangle", "cpp_demangle", "loader", "fallible-iterator", "smallvec"]
std = ["gimli/std"]
loader = ["std", "dep:object", "dep:memmap2", "dep:typed-arena"]
bin = ["loader", "rustc-demangle", "cpp_demangle", "smallvec", "dep:clap"]
bin = ["loader", "rustc-demangle", "cpp_demangle", "fallible-iterator", "smallvec", "dep:clap"]
all = ["bin"]

# Use of --all-features is not supported.
Expand Down
19 changes: 15 additions & 4 deletions src/bin/addr2line.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fallible_iterator::FallibleIterator;
use std::borrow::Cow;
use std::io::{BufRead, Lines, StdinLock, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -210,7 +211,7 @@ fn main() {
if opts.do_functions || opts.do_inlines {
let mut printed_anything = false;
if let Some(probe) = probe {
let mut frames = ctx.find_frames(probe).unwrap();
let mut frames = ctx.find_frames(probe).unwrap().peekable();
let mut first = true;
while let Some(frame) = frames.next().unwrap() {
if opts.pretty && !first {
Expand All @@ -219,15 +220,25 @@ fn main() {
first = false;

if opts.do_functions {
if let Some(func) = frame.function {
// Only use the symbol table if this isn't an inlined function.
let symbol = if matches!(frames.peek(), Ok(None)) {
ctx.find_symbol(probe)
} else {
None
};
if symbol.is_some() {
// 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
print_function(symbol, None, opts.demangle);
} else if let Some(func) = frame.function {
print_function(
func.raw_name().ok().as_deref(),
func.language,
opts.demangle,
);
} else {
let name = ctx.find_symbol(probe);
print_function(name, None, opts.demangle);
print_function(None, None, opts.demangle);
}

if opts.pretty {
Expand Down
18 changes: 9 additions & 9 deletions testoutput/dwarf/base-clang-g1
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ _ZN3Foo3barEPKc
_ZN3Foo3barEPKc
/object/testfiles/dwarf/base.cpp:16
0x00000000000011a0
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:19
0x00000000000011ac
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:20
0x00000000000011b0
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:?
0x00000000000011d0
bar
/object/testfiles/dwarf/base.cpp:15
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:21
0x000000000000120e
bar
/object/testfiles/dwarf/base.cpp:14
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:21
0x000000000000121c
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:?
0x0000000000001230
bar
/object/testfiles/dwarf/base.cpp:15
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:23
0x000000000000126e
bar
/object/testfiles/dwarf/base.cpp:14
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:23
0x000000000000127a
foo
_ZN3Foo3fooEi
/object/testfiles/dwarf/base.cpp:24
0x0000000000001280
main
Expand Down
14 changes: 7 additions & 7 deletions testoutput/dwarf/cold
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
0x0000000000001060
_ZN3Foo3fooEi
_ZN3Foo3fooEi.cold
/object/testfiles/dwarf/cold.cpp:29
0x0000000000001067
_ZN3Foo3fooEi
_ZN3Foo3fooEi.cold
/object/testfiles/dwarf/cold.cpp:29
0x000000000000106e
_ZN3Foo3barEPKc
/object/testfiles/dwarf/cold.cpp:20
_ZN3Foo3fooEi
_ZN3Foo3fooEi.cold
/object/testfiles/dwarf/cold.cpp:29
0x0000000000001075
_ZN3Foo3barEPKc
/object/testfiles/dwarf/cold.cpp:21
_ZN3Foo3fooEi
_ZN3Foo3fooEi.cold
/object/testfiles/dwarf/cold.cpp:29
0x0000000000001078
_ZN3Foo3barEPKc
/object/testfiles/dwarf/cold.cpp:20
_ZN3Foo3fooEi
_ZN3Foo3fooEi.cold
/object/testfiles/dwarf/cold.cpp:29
0x000000000000107c
_ZN3Foo3barEPKc
/object/testfiles/dwarf/cold.cpp:20
_ZN3Foo3fooEi
_ZN3Foo3fooEi.cold
/object/testfiles/dwarf/cold.cpp:29
0x0000000000001081
_ZN3Foo3fooEi
_ZN3Foo3fooEi.cold
/object/testfiles/dwarf/cold.cpp:30
0x0000000000001082
_ZN3Foo3barEPKc
Expand Down

0 comments on commit ee4a8fd

Please sign in to comment.