perf powerpc: Unify the skip-callchain-idx libdw with that for addr2line

Rather than have 2 Dwfl unify the Dwfl in skip-callchain-idx with that
is used by libdw__addr2line().

Rename that variable in 'struct dso' from 'a2l_libdw' to just 'libdw' as
it is now used in more than addr2line.

The Dwfl in skip-callchain-idx uses a map address when being read with
dwfl_report_elf (rather than dwfl_report_offline that addr2line
uses).

skip-callchain-idx is wrong as the map address can vary between
processes because of ASLR, ie it should need a different Dwfl per
process.

In the code after this patch the base address becomes 0 and the mapped
PC is used with the dwfl functions.

This should increase the accuracy of skip-callchain-idx, but the impact
has only been build tested.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Aditya Bodkhe <aditya.b1@linux.ibm.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Guo Ren <guoren@kernel.org>
Cc: Haibo Xu <haibo1.xu@intel.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Krzysztof Łopatowski <krzysztof.m.lopatowski@gmail.com>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Mark Wielaard <mark@klomp.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sergei Trofimovich <slyich@gmail.com>
Cc: Shimin Guo <shimin.guo@skydio.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Thomas Falcon <thomas.falcon@intel.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Ian Rogers 2026-01-16 21:28:33 -08:00 committed by Arnaldo Carvalho de Melo
parent 8c59835851
commit b7a2b011e9
6 changed files with 88 additions and 102 deletions

View File

@ -30,14 +30,6 @@
* The libdwfl code in this file is based on code from elfutils * The libdwfl code in this file is based on code from elfutils
* (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc). * (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc).
*/ */
static char *debuginfo_path;
static const Dwfl_Callbacks offline_callbacks = {
.debuginfo_path = &debuginfo_path,
.find_debuginfo = dwfl_standard_find_debuginfo,
.section_address = dwfl_offline_section_address,
};
/* /*
* Use the DWARF expression for the Call-frame-address and determine * Use the DWARF expression for the Call-frame-address and determine
@ -149,44 +141,22 @@ static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
* yet used) * yet used)
* -1 in case of errors * -1 in case of errors
*/ */
static int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc) static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc)
{ {
int rc = -1; int rc = -1;
Dwfl *dwfl; Dwfl *dwfl;
Dwfl_Module *mod; Dwfl_Module *mod;
Dwarf_Frame *frame; Dwarf_Frame *frame;
int ra_regno; int ra_regno;
Dwarf_Addr start = pc; Dwarf_Addr start = mapped_pc;
Dwarf_Addr end = pc; Dwarf_Addr end = mapped_pc;
bool signalp; bool signalp;
const char *exec_file = dso__long_name(dso);
dwfl = RC_CHK_ACCESS(dso)->dwfl; dwfl = dso__libdw_dwfl(dso);
if (!dwfl)
return -1;
if (!dwfl) { mod = dwfl_addrmodule(dwfl, mapped_pc);
dwfl = dwfl_begin(&offline_callbacks);
if (!dwfl) {
pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));
return -1;
}
mod = dwfl_report_elf(dwfl, exec_file, exec_file, -1,
map_start, false);
if (!mod) {
pr_debug("dwfl_report_elf() failed %s\n",
dwarf_errmsg(-1));
/*
* We normally cache the DWARF debug info and never
* call dwfl_end(). But to prevent fd leak, free in
* case of error.
*/
dwfl_end(dwfl);
goto out;
}
RC_CHK_ACCESS(dso)->dwfl = dwfl;
}
mod = dwfl_addrmodule(dwfl, pc);
if (!mod) { if (!mod) {
pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1)); pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
goto out; goto out;
@ -196,9 +166,9 @@ static int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc)
* To work with split debug info files (eg: glibc), check both * To work with split debug info files (eg: glibc), check both
* .eh_frame and .debug_frame sections of the ELF header. * .eh_frame and .debug_frame sections of the ELF header.
*/ */
frame = get_eh_frame(mod, pc); frame = get_eh_frame(mod, mapped_pc);
if (!frame) { if (!frame) {
frame = get_dwarf_frame(mod, pc); frame = get_dwarf_frame(mod, mapped_pc);
if (!frame) if (!frame)
goto out; goto out;
} }
@ -264,7 +234,7 @@ int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
return skip_slot; return skip_slot;
} }
rc = check_return_addr(dso, map__start(al.map), ip); rc = check_return_addr(dso, map__map_ip(al.map, ip));
pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n", pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
dso__long_name(dso), al.sym->name, ip, rc); dso__long_name(dso), al.sym->name, ip, rc);

View File

@ -1612,7 +1612,7 @@ void dso__delete(struct dso *dso)
auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache); auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache);
dso_cache__free(dso); dso_cache__free(dso);
dso__free_a2l(dso); dso__free_a2l(dso);
dso__free_a2l_libdw(dso); dso__free_libdw(dso);
dso__free_symsrc_filename(dso); dso__free_symsrc_filename(dso);
nsinfo__zput(RC_CHK_ACCESS(dso)->nsinfo); nsinfo__zput(RC_CHK_ACCESS(dso)->nsinfo);
mutex_destroy(dso__lock(dso)); mutex_destroy(dso__lock(dso));

View File

@ -268,11 +268,8 @@ DECLARE_RC_STRUCT(dso) {
const char *short_name; const char *short_name;
const char *long_name; const char *long_name;
void *a2l; void *a2l;
void *a2l_libdw; void *libdw;
char *symsrc_filename; char *symsrc_filename;
#if defined(__powerpc__)
void *dwfl; /* DWARF debug info */
#endif
struct nsinfo *nsinfo; struct nsinfo *nsinfo;
struct auxtrace_cache *auxtrace_cache; struct auxtrace_cache *auxtrace_cache;
union { /* Tool specific area */ union { /* Tool specific area */
@ -335,16 +332,26 @@ static inline void dso__set_a2l(struct dso *dso, void *val)
RC_CHK_ACCESS(dso)->a2l = val; RC_CHK_ACCESS(dso)->a2l = val;
} }
static inline void *dso__a2l_libdw(const struct dso *dso) static inline void *dso__libdw(const struct dso *dso)
{ {
return RC_CHK_ACCESS(dso)->a2l_libdw; return RC_CHK_ACCESS(dso)->libdw;
} }
static inline void dso__set_a2l_libdw(struct dso *dso, void *val) static inline void dso__set_libdw(struct dso *dso, void *val)
{ {
RC_CHK_ACCESS(dso)->a2l_libdw = val; RC_CHK_ACCESS(dso)->libdw = val;
} }
struct Dwfl;
#ifdef HAVE_LIBDW_SUPPORT
struct Dwfl *dso__libdw_dwfl(struct dso *dso);
#else
static inline struct Dwfl *dso__libdw_dwfl(struct dso *dso __maybe_unused)
{
return NULL;
}
#endif
static inline unsigned int dso__a2l_fails(const struct dso *dso) static inline unsigned int dso__a2l_fails(const struct dso *dso)
{ {
return RC_CHK_ACCESS(dso)->a2l_fails; return RC_CHK_ACCESS(dso)->a2l_fails;

View File

@ -8,16 +8,64 @@
#include <unistd.h> #include <unistd.h>
#include <elfutils/libdwfl.h> #include <elfutils/libdwfl.h>
void dso__free_a2l_libdw(struct dso *dso) static const Dwfl_Callbacks offline_callbacks = {
.find_debuginfo = dwfl_standard_find_debuginfo,
.section_address = dwfl_offline_section_address,
.find_elf = dwfl_build_id_find_elf,
};
void dso__free_libdw(struct dso *dso)
{ {
Dwfl *dwfl = dso__a2l_libdw(dso); Dwfl *dwfl = dso__libdw(dso);
if (dwfl) { if (dwfl) {
dwfl_end(dwfl); dwfl_end(dwfl);
dso__set_a2l_libdw(dso, NULL); dso__set_libdw(dso, NULL);
} }
} }
struct Dwfl *dso__libdw_dwfl(struct dso *dso)
{
Dwfl *dwfl = dso__libdw(dso);
const char *dso_name;
Dwfl_Module *mod;
int fd;
if (dwfl)
return dwfl;
dso_name = dso__long_name(dso);
/*
* Initialize Dwfl session.
* We need to open the DSO file to report it to libdw.
*/
fd = open(dso_name, O_RDONLY);
if (fd < 0)
return NULL;
dwfl = dwfl_begin(&offline_callbacks);
if (!dwfl) {
close(fd);
return NULL;
}
/*
* If the report is successful, the file descriptor fd is consumed
* and closed by the Dwfl. If not, it is not closed.
*/
mod = dwfl_report_offline(dwfl, dso_name, dso_name, fd);
if (!mod) {
dwfl_end(dwfl);
close(fd);
return NULL;
}
dwfl_report_end(dwfl, /*removed=*/NULL, /*arg=*/NULL);
dso__set_libdw(dso, dwfl);
return dwfl;
}
struct libdw_a2l_cb_args { struct libdw_a2l_cb_args {
struct dso *dso; struct dso *dso;
struct symbol *sym; struct symbol *sym;
@ -63,58 +111,21 @@ static int libdw_a2l_cb(Dwarf_Die *die, void *_args)
return 0; return 0;
} }
int libdw__addr2line(const char *dso_name, u64 addr, int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,
char **file, unsigned int *line_nr,
struct dso *dso, bool unwind_inlines, struct dso *dso, bool unwind_inlines,
struct inline_node *node, struct symbol *sym) struct inline_node *node, struct symbol *sym)
{ {
static const Dwfl_Callbacks offline_callbacks = { Dwfl *dwfl = dso__libdw_dwfl(dso);
.find_debuginfo = dwfl_standard_find_debuginfo,
.section_address = dwfl_offline_section_address,
.find_elf = dwfl_build_id_find_elf,
};
Dwfl *dwfl = dso__a2l_libdw(dso);
Dwfl_Module *mod; Dwfl_Module *mod;
Dwfl_Line *dwline; Dwfl_Line *dwline;
Dwarf_Addr bias; Dwarf_Addr bias;
const char *src; const char *src;
int lineno = 0; int lineno = 0;
if (!dwfl) { if (!dwfl)
/* return 0;
* Initialize Dwfl session.
* We need to open the DSO file to report it to libdw.
*/
int fd;
fd = open(dso_name, O_RDONLY);
if (fd < 0)
return 0;
dwfl = dwfl_begin(&offline_callbacks);
if (!dwfl) {
close(fd);
return 0;
}
/*
* If the report is successful, the file descriptor fd is consumed
* and closed by the Dwfl. If not, it is not closed.
*/
mod = dwfl_report_offline(dwfl, dso_name, dso_name, fd);
if (!mod) {
dwfl_end(dwfl);
close(fd);
return 0;
}
dwfl_report_end(dwfl, /*removed=*/NULL, /*arg=*/NULL);
dso__set_a2l_libdw(dso, dwfl);
} else {
/* Dwfl session already initialized, get module for address. */
mod = dwfl_addrmodule(dwfl, addr);
}
mod = dwfl_addrmodule(dwfl, addr);
if (!mod) if (!mod)
return 0; return 0;

View File

@ -11,7 +11,6 @@ struct symbol;
#ifdef HAVE_LIBDW_SUPPORT #ifdef HAVE_LIBDW_SUPPORT
/* /*
* libdw__addr2line - Convert address to source location using libdw * libdw__addr2line - Convert address to source location using libdw
* @dso_name: Name of the DSO
* @addr: Address to resolve * @addr: Address to resolve
* @file: Pointer to return filename (caller must free) * @file: Pointer to return filename (caller must free)
* @line_nr: Pointer to return line number * @line_nr: Pointer to return line number
@ -26,23 +25,22 @@ struct symbol;
* *
* Returns 1 on success (found), 0 on failure (not found). * Returns 1 on success (found), 0 on failure (not found).
*/ */
int libdw__addr2line(const char *dso_name, u64 addr, char **file, int libdw__addr2line(u64 addr, char **file,
unsigned int *line_nr, struct dso *dso, unsigned int *line_nr, struct dso *dso,
bool unwind_inlines, struct inline_node *node, bool unwind_inlines, struct inline_node *node,
struct symbol *sym); struct symbol *sym);
/* /*
* dso__free_a2l_libdw - Free libdw resources associated with the DSO * dso__free_libdw - Free libdw resources associated with the DSO
* @dso: The dso to free resources for * @dso: The dso to free resources for
* *
* This function cleans up the Dwfl context used for addr2line lookups. * This function cleans up the Dwfl context used for addr2line lookups.
*/ */
void dso__free_a2l_libdw(struct dso *dso); void dso__free_libdw(struct dso *dso);
#else /* HAVE_LIBDW_SUPPORT */ #else /* HAVE_LIBDW_SUPPORT */
static inline int libdw__addr2line(const char *dso_name __maybe_unused, static inline int libdw__addr2line(u64 addr __maybe_unused, char **file __maybe_unused,
u64 addr __maybe_unused, char **file __maybe_unused,
unsigned int *line_nr __maybe_unused, unsigned int *line_nr __maybe_unused,
struct dso *dso __maybe_unused, struct dso *dso __maybe_unused,
bool unwind_inlines __maybe_unused, bool unwind_inlines __maybe_unused,
@ -52,7 +50,7 @@ static inline int libdw__addr2line(const char *dso_name __maybe_unused,
return 0; return 0;
} }
static inline void dso__free_a2l_libdw(struct dso *dso __maybe_unused) static inline void dso__free_libdw(struct dso *dso __maybe_unused)
{ {
} }
#endif /* HAVE_LIBDW_SUPPORT */ #endif /* HAVE_LIBDW_SUPPORT */

View File

@ -161,7 +161,7 @@ static int addr2line(const char *dso_name, u64 addr, char **file, unsigned int *
for (size_t i = 0; i < ARRAY_SIZE(symbol_conf.addr2line_style); i++) { for (size_t i = 0; i < ARRAY_SIZE(symbol_conf.addr2line_style); i++) {
switch (symbol_conf.addr2line_style[i]) { switch (symbol_conf.addr2line_style[i]) {
case A2L_STYLE_LIBDW: case A2L_STYLE_LIBDW:
ret = libdw__addr2line(dso_name, addr, file, line_nr, dso, unwind_inlines, ret = libdw__addr2line(addr, file, line_nr, dso, unwind_inlines,
node, sym); node, sym);
break; break;
case A2L_STYLE_LLVM: case A2L_STYLE_LLVM: