perf libdw: Fix Dwfl discovery with split files
Ian Rogers
reviewerPATCH 1/2 perf libdw: Fix Dwfl discovery with split files
This patch dynamically frees the cached Dwfl object and alternative a2l caches whenever a newly discovered split-debug file is assigned to a dso, and scopes accessors behind a mutex. To safely implement cache teardowns upon rediscovery without introducing races with active readers, the teardown is performed locked.
--- .../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/util/addr2line.c | 35 ++++++++--- tools/perf/util/dso.c | 9 +++ tools/perf/util/dso.h | 5 +- tools/perf/util/libbfd.c | 28 ++++++--- tools/perf/util/libdw.c | 39 ++++++++---- tools/perf/util/srcline.c | 61 ++++++++++++------ tools/perf/util/unwind-libdw.c | 2 + tools/perf/util/unwind-libunwind.c | 62 ++++++++++++++----- 9 files changed, 178 insertions(+), 67 deletions(-)
tools/perf/arch/powerpc/util/skip-callchain-idx.c +3 -1
@@ -152,9 +152,10 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc)Dwarf_Addr end = mapped_pc;bool signalp; +mutex_lock (dso__lock (dso)); dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return -1 ; + goto out; mod =dwfl_addrmodule (dwfl, mapped_pc);if (!mod) {
@@ -183,6 +184,7 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc) rc =check_return_reg (ra_regno, frame); out: +mutex_unlock (dso__lock (dso));return rc; }
tools/perf/util/addr2line.c +28 -7
@@ -284,7 +284,7 @@ int cmd__addr2line(const char *dso_name, u64 addr,struct inline_node *node, struct symbol *sym __maybe_unused) { - struct child_process *a2l = dso__a2l(dso); + struct child_process *a2l;char *record_function =NULL ;char *record_filename =NULL ;unsigned int record_line_nr =0 ;
@@ -296,10 +296,14 @@ int cmd__addr2line(const char *dso_name, u64 addr,ssize_t written;struct io io = { .eof = false };enum cmd_a2l_style cmd_a2l_style; +const char *current_dso_name; + +mutex_lock (dso__lock (dso)); + a2l =dso__a2l (dso);if (!a2l) {if (!filename__has_section (dso_name,".debug_line" )) - goto out; + goto out_unlock;dso__set_a2l (dso,addr2line_subprocess_init (symbol_conf.addr2line_path, dso_name));
@@ -309,11 +313,19 @@ int cmd__addr2line(const char *dso_name, u64 addr,if (a2l ==NULL ) {if (!symbol_conf.addr2line_disable_warn)pr_warning ("%s %s: addr2line_subprocess_init failed\n" , __func__, dso_name); - goto out; + goto out_unlock; } cmd_a2l_style =cmd_addr2line_configure (a2l, dso_name);if (cmd_a2l_style ==BROKEN ) - goto out; + goto out_unlock; + +/* +* Take ownership of the a2l subprocess so we can safely perform +* blocking IPC without holding the dso lock. If another thread +* resolves a symbol concurrently, it will spawn a new a2l process. +*/ +dso__set_a2l (dso,NULL ); +mutex_unlock (dso__lock (dso));/* * Send our request and then *deliberately* send something that can't be
@@ -414,12 +426,21 @@ int cmd__addr2line(const char *dso_name, u64 addr, } out: +mutex_lock (dso__lock (dso));free (record_function);free (record_filename); -if (io.eof) { -dso__set_a2l (dso,NULL ); + + current_dso_name =dso__symsrc_filename (dso) ?:dso__long_name (dso); +if (!io.eof &&dso__a2l (dso) ==NULL && current_dso_name && + !strcmp (current_dso_name, dso_name)) +dso__set_a2l (dso, a2l); +else addr2line_subprocess_cleanup (a2l); - } +mutex_unlock (dso__lock (dso)); +return ret; + +out_unlock: +mutex_unlock (dso__lock (dso));return ret; }
tools/perf/util/dso.c +9 -0
@@ -2075,3 +2075,12 @@ struct debuginfo *dso__debuginfo(struct dso *dso)free (name);return dinfo; } + +void dso__set_symsrc_filename (struct dso *dso,char *val) +{ +RC_CHK_ACCESS (dso)->symsrc_filename = val; +dso__free_libdw (dso); +dso__free_a2l (dso); +dso__set_has_srcline (dso, true); +dso__set_a2l_fails (dso,0 ); +}
tools/perf/util/dso.h +1 -4
@@ -735,10 +735,7 @@ static inline const char *dso__symsrc_filename(const struct dso *dso)return RC_CHK_ACCESS (dso)->symsrc_filename; } -static inline void dso__set_symsrc_filename (struct dso *dso,char *val) -{ -RC_CHK_ACCESS (dso)->symsrc_filename = val; -} +void dso__set_symsrc_filename (struct dso *dso,char *val);static inline void dso__free_symsrc_filename (struct dso *dso) {
tools/perf/util/libbfd.c +20 -8
@@ -226,7 +226,10 @@ int libbfd__addr2line(const char *dso_name, u64 addr, struct symbol *sym) {int ret =0 ; -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l; + +mutex_lock (dso__lock (dso)); + a2l =dso__a2l (dso);if (!a2l) { a2l =addr2line_init (dso_name);
@@ -236,7 +239,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (a2l ==NULL ) {if (!symbol_conf.addr2line_disable_warn)pr_warning ("addr2line_init failed for %s\n" , dso_name); -return 0 ; + ret =-1 ; + goto out; } a2l->addr = addr;
@@ -244,14 +248,18 @@ int libbfd__addr2line(const char *dso_name, u64 addr,bfd_map_over_sections (a2l->abfd, find_address_in_section, a2l); -if (!a2l->found) -return 0 ; +if (!a2l->found) { + ret =0 ; + goto out; + }if (unwind_inlines) {int cnt =0 ; -if (node &&inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (node &&inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + } while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, &a2l->funcname, &a2l->line) &&
@@ -261,8 +269,10 @@ int libbfd__addr2line(const char *dso_name, u64 addr, a2l->filename =NULL ;if (node !=NULL ) { -if (inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + }// found at least one inline frame ret =1 ; }
@@ -277,6 +287,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (line) *line = a2l->line; +out: +mutex_unlock (dso__lock (dso));return ret; }
tools/perf/util/libdw.c +27 -12
@@ -35,7 +35,7 @@ struct Dwfl *dso__libdw_dwfl(struct dso *dso)if (dwfl)return dwfl; - dso_name =dso__long_name (dso); + dso_name =dso__symsrc_filename (dso) ?:dso__long_name (dso);/* * Initialize Dwfl session. * We need to open the DSO file to report it to libdw.
@@ -167,32 +167,41 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, struct dso *dso, bool unwind_inlines, struct inline_node *node, struct symbol *sym) { - Dwfl *dwfl = dso__libdw_dwfl(dso); + Dwfl *dwfl;Dwfl_Module *mod;Dwfl_Line *dwline;Dwarf_Addr bias;const char *src;int lineno =0 ; +int ret =0 ; +mutex_lock (dso__lock (dso)); + dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return 0 ; + goto out; mod =dwfl_addrmodule (dwfl, addr); -if (!mod) -return 0 ; +if (!mod) { + ret =0 ; + goto out; + }/* * Get/ignore the dwarf information. Determine the bias, difference * between the regular ELF addr2line addresses and those to use with * libdw. */ -if (!dwfl_module_getdwarf (mod, &bias)) -return 0 ; +if (!dwfl_module_getdwarf (mod, &bias)) { + ret =-1 ; + goto out; + }/* Find source line information for the address. */ dwline =dwfl_module_getsrc (mod, addr + bias); -if (!dwline) -return 0 ; +if (!dwline) { + ret =-1 ; + goto out; + }/* Get line information. */ src = dwfl_lineinfo(dwline, /*addr=*/NULL, &lineno, /*col=*/NULL, /*mtime=*/NULL,
@@ -219,7 +228,8 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,free (*file); *file =NULL ; } -return 0 ; + ret =0 ; + goto out; }/* Walk from the parent down to the leaf. */
@@ -235,8 +245,13 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, *file =NULL ; }inline_node__clear_frames (node); -return 0 ; + ret =0 ; + goto out; } } -return 1 ; + ret =1 ; + +out: +mutex_unlock (dso__lock (dso)); +return ret; }
tools/perf/util/srcline.c +43 -18
152 lines not on this page · raw message
tools/perf/util/unwind-libdw.c +2 -0
@@ -42,8 +42,10 @@ static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,const struct dso *dso = *userdata;assert (dso); +mutex_lock (dso__lock ((struct dso *)dso));if (dso__symsrc_filename (dso) &&strcmp (file_name,dso__symsrc_filename (dso))) *debuginfo_file_name =strdup (dso__symsrc_filename (dso)); +mutex_unlock (dso__lock ((struct dso *)dso));return -1 ; }
tools/perf/util/unwind-libunwind.c +45 -17
99 lines not on this page · raw message
Signature · 1 line
2.55.0.1032.g73a4cd73de-goog
Ian Rogers
reviewerPATCH 2/2 perf dso: Separate libbfd and cmd addr2line caches to fix confusion
When a dso executes addr2line via libbfd it instantiates an a2l_data pointer. If the DSO later executes via the command-line fallback due to an inlined bug, the a2l pointer is unconditionally populated with a child_process process wrapper. If libbfd is queried again, the offline reader attempts to cast struct a2l_data into child_process, coercing random arbitrary instruction addresses and causing silent aborts. This cleanly splits the caches.
--- tools/perf/util/dso.c | 49 ++++++++++++++++++++++--------------- tools/perf/util/dso.h | 21 ++++++++++++++-- tools/perf/util/libbfd.c | 51 +++++++++++++++++++++++++++------------ tools/perf/util/srcline.c | 2 ++ 4 files changed, 86 insertions(+), 37 deletions(-)
tools/perf/util/dso.c +29 -20
@@ -1,38 +1,45 @@// SPDX-License-Identifier: GPL-2.0 +#include "dso.h" + +#include <errno.h> +#include <stdlib.h> +#include <asm/bug.h> +#include <fcntl.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/zalloc.h> -#include <sys/time.h> #include <sys/resource.h> -#include <sys/types.h> #include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> #include <unistd.h> -#include <errno.h> -#include <fcntl.h> -#include <stdlib.h> -#ifdef HAVE_LIBBPF_SUPPORT -#include <bpf/libbpf.h> -#include "bpf-event.h" -#include "bpf-utils.h" -#endif + +#include "annotate-data.h" +#include "auxtrace.h" #include "compress.h" +#include "debug.h" +#include "dso.h" +#include "dsos.h" #include "env.h" +#include "libbfd.h" +#include "libdw.h" +#include "machine.h" +#include "map.h" #include "namespaces.h" #include "path.h" -#include "map.h" -#include "symbol.h" #include "srcline.h" -#include "dso.h" -#include "dsos.h" -#include "machine.h" -#include "auxtrace.h" -#include "util.h" /* O_CLOEXEC for older systems */ -#include "debug.h" #include "string2.h" +#include "symbol.h" +#include "util.h" /* O_CLOEXEC for older systems */ #include "vdso.h" -#include "annotate-data.h" -#include "libdw.h" + +#ifdef HAVE_LIBBPF_SUPPORT +#include <bpf/libbpf.h> + +#include "bpf-event.h" +#include "bpf-utils.h" +#endif static const char *const debuglink_paths[] = {"%.0s%s" ,
@@ -1757,6 +1764,7 @@ void dso__delete(struct dso *dso)auxtrace_cache__free (RC_CHK_ACCESS (dso)->auxtrace_cache);dso_cache__free (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__free_libdw (dso);dso__free_symsrc_filename (dso);nsinfo__zput (RC_CHK_ACCESS (dso)->nsinfo);
@@ -2081,6 +2089,7 @@ void dso__set_symsrc_filename(struct dso *dso, char *val)RC_CHK_ACCESS (dso)->symsrc_filename = val;dso__free_libdw (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__set_has_srcline (dso, true);dso__set_a2l_fails (dso,0 ); }
tools/perf/util/dso.h +19 -2
@@ -304,7 +304,12 @@ DECLARE_RC_STRUCT(dso) {const char *short_name;const char *long_name;void *a2l; +#ifdef HAVE_LIBBFD_SUPPORT +void *a2l_libbfd; +#endif +#ifdef HAVE_LIBDW_SUPPORT void *libdw; +#endif char *symsrc_filename;struct nsinfo *nsinfo;struct auxtrace_cache *auxtrace_cache;
@@ -368,6 +373,20 @@ static inline void dso__set_a2l(struct dso *dso, void *val)RC_CHK_ACCESS (dso)->a2l = val; } +#ifdef HAVE_LIBBFD_SUPPORT +static inline void *dso__a2l_libbfd (const struct dso *dso) +{ +return RC_CHK_ACCESS (dso)->a2l_libbfd; +} + +static inline void dso__set_a2l_libbfd (struct dso *dso,void *val) +{ +RC_CHK_ACCESS (dso)->a2l_libbfd = val; +} +#endif + +struct Dwfl ; +#ifdef HAVE_LIBDW_SUPPORT static inline void *dso__libdw (const struct dso *dso) {return RC_CHK_ACCESS (dso)->libdw;
@@ -378,8 +397,6 @@ static inline void dso__set_libdw(struct dso *dso, void *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 )
tools/perf/util/libbfd.c +36 -15
136 lines not on this page · raw message
tools/perf/util/srcline.c +2 -0
@@ -300,6 +300,7 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso)); out:
@@ -347,6 +348,7 @@ char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso));
Signature · 1 line
2.55.0.1032.g73a4cd73de-goog
[email protected]
AI reviewRe: [PATCH v1 2/2] perf dso: Separate libbfd and cmd addr2line caches to fix confusion
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
When a dso executes addr2line via libbfd it instantiates an
a2l_datapointer. If the DSO later executes via the command-line fallback due to an inlined bug, the a2l pointer is unconditionally populated with achild_processprocess wrapper. If libbfd is queried again, the offline reader attempts to cast structa2l_dataintochild_process, coercing random arbitrary instruction addresses and causing silent aborts. This cleanly splits the caches.Fixes 257046a36750 ("perf srcline: Fallback between addr2line implementations")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Early error return in cmd__addr2line leaves the addr2line subprocess pipe in a dirty state, corrupting subsequent IPC queries. --
commit 15f52b88a78ae8721f4ec48695f230d6450c86f6
Author: Ian Rogers <[email protected]>
perf libdw: Fix Dwfl discovery with split files
This patch dynamically frees the cached Dwfl object and alternative a2l caches whenever a newly discovered split-debug file is assigned to a dso, and scopes accessors behind a mutex.
Quoted 4 lines
tools/perf/util/addr2line.c +0
-0
[ ... ]
Quoted 4 lines
tools/perf/util/addr2line.c +0
-0@@ -414,12 +426,21 @@ int cmd__addr2line(const char *dso_name, u64 addr, } out:
tools/perf/util/addr2line.c int cmd__addr2line(const char *dso_name, u64 addr, · line 436
Quoted 10 lines
tools/perf/util/addr2line.c +6
-2+mutex_lock (dso__lock (dso));free (record_function);free (record_filename); -if (io.eof) { -dso__set_a2l (dso,NULL ); + + current_dso_name =dso__symsrc_filename (dso) ?:dso__long_name (dso); +if (!io.eof &&dso__a2l (dso) ==NULL && current_dso_name && + !strcmp (current_dso_name, dso_name)) +dso__set_a2l (dso, a2l);
[Severity: High]
This is a pre-existing issue, but an early error return in cmd__addr2line() (such as a memory allocation failure when calling inline_list__append_record() or a parsing failure in read_addr2line_record()) aborts the read loop and jumps here without consuming the rest of the IPC stream.
Since EOF is not reached during an early abort, does this incorrectly restore the dirty subprocess pipe back into the cache? It appears this leaves unread output in the pipe, which could corrupt subsequent address resolutions for this DSO when other threads query it and read the leftover garbage data.
Quoted 10 lines
tools/perf/util/addr2line.c +6
-1+else addr2line_subprocess_cleanup (a2l); - } +mutex_unlock (dso__lock (dso)); +return ret; + +out_unlock: +mutex_unlock (dso__lock (dso));return ret; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerv2 cover perf srcline: Fix addr2line cache and fallback bugs
In reply to Ian RogersThree fixes to source line resolution, all on the addr2line paths.
1) libdw is pointed at the wrong file. dso__libdw_dwfl() opens the Dwfl with the name of the file the samples came from, but when the debug information is in a separate file, symbol loading records that as the dso's symsrc filename and the Dwfl is left referring to a file with no DWARF in it.
2) The dso addr2line cache is shared between implementations. The libbfd reader caches a struct a2l_data and the command line fallback caches a struct child_process through the same pointer, so once a dso has fallen back from one to the other the cached object is read back as the wrong type.
3) libbfd only reports success when the caller asked for a file name. addr2inlines() doesn't, so srcline.c treats a resolved address as a failure and tries the next implementation, which appends its own frames to the ones libbfd already appended. Every frame that isn't inlined is then reported twice. This is the default when perf is built with libbfd but without libdw:
$ perf record --call-graph dwarf -- perf test -w inlineloop 1
$ perf script --fields +srcline
...
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
...With addr2line.style set to "libbfd,addr2line" so the fallback is taken, the script output for that workload drops from 288 lines to 176, and each repeated frame goes from appearing 16 times to 8.
Changes in v2:
- Add patch 3, so that a resolved address isn't retried by the next addr2line implementation and reported twice.
- Patch 1: rewrite the commit message to describe the wrong file the Dwfl is opened with, which is the actual bug, rather than the cache teardown that follows from it, and add the Fixes tag.
- Rebase onto perf-tools-next.
This series is independent of "perf build: Fix builds with clang and BUILD_NONDISTRO" posted earlier and applies with or without it.
Testing:
- "perf test addr2line" and the hists tests pass.
- Builds in 13 configurations, including BUILD_NONDISTRO=1 where the libbfd reader is actually compiled, and with clang and gcc.
- Every patch builds on its own, so the series stays bisectable.
Ian Rogers (3):
perf libdw: Fix Dwfl discovery with split files
perf dso: Separate libbfd and cmd addr2line caches to fix confusion perf libbfd: Report success when an address is found
.../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/util/addr2line.c | 35 ++++++++--- tools/perf/util/dso.c | 58 +++++++++++------ tools/perf/util/dso.h | 26 ++++++-- tools/perf/util/libbfd.c | 52 ++++++++++----- tools/perf/util/libdw.c | 39 ++++++++---- tools/perf/util/srcline.c | 63 +++++++++++++------ tools/perf/util/unwind-libdw.c | 2 + tools/perf/util/unwind-libunwind.c | 62 +++++++++++++----- 9 files changed, 245 insertions(+), 96 deletions(-)
base-commit: 91b0782fc9e9d2f0a40b5256146e014802fdbb36
prerequisite-patch-id: b6fdc526887b71fb66f5fe0c0d41f7ef9493861e
prerequisite-patch-id: d47077f674c700f4f6296e9367f7ddfe004aea89
prerequisite-patch-id: ed0db23450840601762fe85ca1bef06ce6d28fe7
prerequisite-patch-id: d84e6b96d533ee6ed549e26e94216e2da60f7f74
prerequisite-patch-id: 6e8f19551d771621a5037096626cfe7e271b36f9
prerequisite-patch-id: 3768dfd588c7439deb741bce44c74763011c6be7
prerequisite-patch-id: babac99a4faa3b1525e44d3f34ab33a88103334c
prerequisite-patch-id: 5279827453fea1536abc620aa33c887c44e41ee9
prerequisite-patch-id: bdc0d648a577142b9270fb59b54496bc2bf8d5ea
Signature · 1 line
2.55.0.1032.g73a4cd73de-goog
Ian Rogers
reviewerPATCH v2 1/3 perf libdw: Fix Dwfl discovery with split files
dso__libdw_dwfl() opens the Dwfl with dso__long_name(), the file the samples came from. When the debug information lives in a separate file, symbol loading finds it and records it as the dso's symsrc filename, but the Dwfl still refers to the original file, so libdw has no DWARF to resolve addresses against.
Open the Dwfl with the symsrc filename when one is known and fall back to the long name when it isn't.
The symsrc file is found while symbols are loaded, which can happen after the Dwfl has been created and cached, so drop the cached Dwfl and the addr2line cache built from it when the symsrc filename is set. That teardown can run while another thread is using the cache, so take the dso lock across both the teardown and the Dwfl accessors.
--- .../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/util/addr2line.c | 35 ++++++++--- tools/perf/util/dso.c | 9 +++ tools/perf/util/dso.h | 5 +- tools/perf/util/libbfd.c | 28 ++++++--- tools/perf/util/libdw.c | 39 ++++++++---- tools/perf/util/srcline.c | 61 ++++++++++++------ tools/perf/util/unwind-libdw.c | 2 + tools/perf/util/unwind-libunwind.c | 62 ++++++++++++++----- 9 files changed, 178 insertions(+), 67 deletions(-)
tools/perf/arch/powerpc/util/skip-callchain-idx.c +3 -1
@@ -152,9 +152,10 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc)Dwarf_Addr end = mapped_pc;bool signalp; +mutex_lock (dso__lock (dso)); dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return -1 ; + goto out; mod =dwfl_addrmodule (dwfl, mapped_pc);if (!mod) {
@@ -183,6 +184,7 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc) rc =check_return_reg (ra_regno, frame); out: +mutex_unlock (dso__lock (dso));return rc; }
tools/perf/util/addr2line.c +28 -7
@@ -284,7 +284,7 @@ int cmd__addr2line(const char *dso_name, u64 addr,struct inline_node *node, struct symbol *sym __maybe_unused) { - struct child_process *a2l = dso__a2l(dso); + struct child_process *a2l;char *record_function =NULL ;char *record_filename =NULL ;unsigned int record_line_nr =0 ;
@@ -296,10 +296,14 @@ int cmd__addr2line(const char *dso_name, u64 addr,ssize_t written;struct io io = { .eof = false };enum cmd_a2l_style cmd_a2l_style; +const char *current_dso_name; + +mutex_lock (dso__lock (dso)); + a2l =dso__a2l (dso);if (!a2l) {if (!filename__has_section (dso_name,".debug_line" )) - goto out; + goto out_unlock;dso__set_a2l (dso,addr2line_subprocess_init (symbol_conf.addr2line_path, dso_name));
@@ -309,11 +313,19 @@ int cmd__addr2line(const char *dso_name, u64 addr,if (a2l ==NULL ) {if (!symbol_conf.addr2line_disable_warn)pr_warning ("%s %s: addr2line_subprocess_init failed\n" , __func__, dso_name); - goto out; + goto out_unlock; } cmd_a2l_style =cmd_addr2line_configure (a2l, dso_name);if (cmd_a2l_style ==BROKEN ) - goto out; + goto out_unlock; + +/* +* Take ownership of the a2l subprocess so we can safely perform +* blocking IPC without holding the dso lock. If another thread +* resolves a symbol concurrently, it will spawn a new a2l process. +*/ +dso__set_a2l (dso,NULL ); +mutex_unlock (dso__lock (dso));/* * Send our request and then *deliberately* send something that can't be
@@ -414,12 +426,21 @@ int cmd__addr2line(const char *dso_name, u64 addr, } out: +mutex_lock (dso__lock (dso));free (record_function);free (record_filename); -if (io.eof) { -dso__set_a2l (dso,NULL ); + + current_dso_name =dso__symsrc_filename (dso) ?:dso__long_name (dso); +if (!io.eof &&dso__a2l (dso) ==NULL && current_dso_name && + !strcmp (current_dso_name, dso_name)) +dso__set_a2l (dso, a2l); +else addr2line_subprocess_cleanup (a2l); - } +mutex_unlock (dso__lock (dso)); +return ret; + +out_unlock: +mutex_unlock (dso__lock (dso));return ret; }
tools/perf/util/dso.c +9 -0
@@ -2075,3 +2075,12 @@ struct debuginfo *dso__debuginfo(struct dso *dso)free (name);return dinfo; } + +void dso__set_symsrc_filename (struct dso *dso,char *val) +{ +RC_CHK_ACCESS (dso)->symsrc_filename = val; +dso__free_libdw (dso); +dso__free_a2l (dso); +dso__set_has_srcline (dso, true); +dso__set_a2l_fails (dso,0 ); +}
tools/perf/util/dso.h +1 -4
@@ -735,10 +735,7 @@ static inline const char *dso__symsrc_filename(const struct dso *dso)return RC_CHK_ACCESS (dso)->symsrc_filename; } -static inline void dso__set_symsrc_filename (struct dso *dso,char *val) -{ -RC_CHK_ACCESS (dso)->symsrc_filename = val; -} +void dso__set_symsrc_filename (struct dso *dso,char *val);static inline void dso__free_symsrc_filename (struct dso *dso) {
tools/perf/util/libbfd.c +20 -8
@@ -255,7 +255,10 @@ int libbfd__addr2line(const char *dso_name, u64 addr, struct symbol *sym) {int ret =0 ; -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l; + +mutex_lock (dso__lock (dso)); + a2l =dso__a2l (dso);if (!a2l) { a2l =addr2line_init (dso_name);
@@ -265,7 +268,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (a2l ==NULL ) {if (!symbol_conf.addr2line_disable_warn)pr_warning ("addr2line_init failed for %s\n" , dso_name); -return 0 ; + ret =-1 ; + goto out; } a2l->addr = addr;
@@ -273,14 +277,18 @@ int libbfd__addr2line(const char *dso_name, u64 addr,bfd_map_over_sections (a2l->abfd, find_address_in_section, a2l); -if (!a2l->found) -return 0 ; +if (!a2l->found) { + ret =0 ; + goto out; + }if (unwind_inlines) {int cnt =0 ; -if (node &&inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (node &&inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + } while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, &a2l->funcname, &a2l->line) &&
@@ -290,8 +298,10 @@ int libbfd__addr2line(const char *dso_name, u64 addr, a2l->filename =NULL ;if (node !=NULL ) { -if (inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + }// found at least one inline frame ret =1 ; }
@@ -306,6 +316,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (line) *line = a2l->line; +out: +mutex_unlock (dso__lock (dso));return ret; }
tools/perf/util/libdw.c +27 -12
@@ -35,7 +35,7 @@ struct Dwfl *dso__libdw_dwfl(struct dso *dso)if (dwfl)return dwfl; - dso_name =dso__long_name (dso); + dso_name =dso__symsrc_filename (dso) ?:dso__long_name (dso);/* * Initialize Dwfl session. * We need to open the DSO file to report it to libdw.
@@ -167,32 +167,41 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, struct dso *dso, bool unwind_inlines, struct inline_node *node, struct symbol *sym) { - Dwfl *dwfl = dso__libdw_dwfl(dso); + Dwfl *dwfl;Dwfl_Module *mod;Dwfl_Line *dwline;Dwarf_Addr bias;const char *src;int lineno =0 ; +int ret =0 ; +mutex_lock (dso__lock (dso)); + dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return 0 ; + goto out; mod =dwfl_addrmodule (dwfl, addr); -if (!mod) -return 0 ; +if (!mod) { + ret =0 ; + goto out; + }/* * Get/ignore the dwarf information. Determine the bias, difference * between the regular ELF addr2line addresses and those to use with * libdw. */ -if (!dwfl_module_getdwarf (mod, &bias)) -return 0 ; +if (!dwfl_module_getdwarf (mod, &bias)) { + ret =-1 ; + goto out; + }/* Find source line information for the address. */ dwline =dwfl_module_getsrc (mod, addr + bias); -if (!dwline) -return 0 ; +if (!dwline) { + ret =-1 ; + goto out; + }/* Get line information. */ src = dwfl_lineinfo(dwline, /*addr=*/NULL, &lineno, /*col=*/NULL, /*mtime=*/NULL,
@@ -219,7 +228,8 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,free (*file); *file =NULL ; } -return 0 ; + ret =0 ; + goto out; }/* Walk from the parent down to the leaf. */
@@ -235,8 +245,13 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, *file =NULL ; }inline_node__clear_frames (node); -return 0 ; + ret =0 ; + goto out; } } -return 1 ; + ret =1 ; + +out: +mutex_unlock (dso__lock (dso)); +return ret; }
tools/perf/util/srcline.c +43 -18
152 lines not on this page · raw message
tools/perf/util/unwind-libdw.c +2 -0
@@ -42,8 +42,10 @@ static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,const struct dso *dso = *userdata;assert (dso); +mutex_lock (dso__lock ((struct dso *)dso));if (dso__symsrc_filename (dso) &&strcmp (file_name,dso__symsrc_filename (dso))) *debuginfo_file_name =strdup (dso__symsrc_filename (dso)); +mutex_unlock (dso__lock ((struct dso *)dso));return -1 ; }
tools/perf/util/unwind-libunwind.c +45 -17
99 lines not on this page · raw message
Signature · 1 line
2.55.0.1032.g73a4cd73de-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf libbfd: TOCTOU race populating cache with stale dso_name --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf libbfd: TOCTOU race populating cache with stale dso_name
--
commit 6a983b5d3f6eb6250b40233e72f23d9a6454ca97
Author: Ian Rogers <[email protected]>
perf libdw: Fix Dwfl discovery with split files
This commit updates Dwfl initialization to use the symsrc filename when available, falling back to the long name. It drops the cached Dwfl and addr2line cache when the symsrc filename is set, using the dso lock to safely handle concurrent teardown and access.
Quoted 15 lines
tools/perf/util/libbfd.c +4
-1@@ -226,7 +226,10 @@ int libbfd__addr2line(const char *dso_name, u64 addr, struct symbol *sym) {int ret =0 ; -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l; + +mutex_lock (dso__lock (dso)); + a2l =dso__a2l (dso);if (!a2l) { a2l =addr2line_init (dso_name);
tools/perf/util/libbfd.c
Quoted 2 lines
tools/perf/util/libbfd.c +0
-0dso__set_a2l (dso, a2l); }
[Severity: Medium]
Is there a Time-of-Check-to-Time-of-Use race here when populating the a2l cache?
In __get_srcline(), dso_name is fetched before the lock is acquired in libbfd__addr2line():
tools/perf/util/srcline.c:__get_srcline() {
...
dso_name = srcline_dso_name(dso);
if (dso_name == NULL)
goto out_err;
if (!addr2line(dso_name, addr, &file, &line, dso, ...
}If another thread calls dso__set_symsrc_filename(), which updates the debuginfo file and clears the cache, before libbfd__addr2line() acquires its mutex:
Thread 1dso__set_symsrc_filename()
dso__free_a2l(dso);
Thread 2 would see a NULL cache in libbfd__addr2line() and populate it using the stale dso_name argument:
if (!a2l) {
a2l = addr2line_init(dso_name);
dso__set_a2l(dso, a2l);
}Could this permanently poison the cache with a BFD instance for a file lacking debuginfo by not verifying it against the current symsrc_filename under the lock?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v2 2/3 perf dso: Separate libbfd and cmd addr2line caches to fix confusion
In reply to Ian RogersWhen a dso executes addr2line via libbfd it instantiates an a2l_data pointer. If the DSO later executes via the command-line fallback due to an inlined bug, the a2l pointer is unconditionally populated with a child_process process wrapper. If libbfd is queried again, the offline reader attempts to cast struct a2l_data into child_process, coercing random arbitrary instruction addresses and causing silent aborts. This cleanly splits the caches.
--- tools/perf/util/dso.c | 49 +++++++++++++++++++++++---------------- tools/perf/util/dso.h | 21 +++++++++++++++-- tools/perf/util/libbfd.c | 10 ++++---- tools/perf/util/srcline.c | 2 ++ 4 files changed, 55 insertions(+), 27 deletions(-)
tools/perf/util/dso.c +29 -20
@@ -1,38 +1,45 @@// SPDX-License-Identifier: GPL-2.0 +#include "dso.h" + +#include <errno.h> +#include <stdlib.h> +#include <asm/bug.h> +#include <fcntl.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/zalloc.h> -#include <sys/time.h> #include <sys/resource.h> -#include <sys/types.h> #include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> #include <unistd.h> -#include <errno.h> -#include <fcntl.h> -#include <stdlib.h> -#ifdef HAVE_LIBBPF_SUPPORT -#include <bpf/libbpf.h> -#include "bpf-event.h" -#include "bpf-utils.h" -#endif + +#include "annotate-data.h" +#include "auxtrace.h" #include "compress.h" +#include "debug.h" +#include "dso.h" +#include "dsos.h" #include "env.h" +#include "libbfd.h" +#include "libdw.h" +#include "machine.h" +#include "map.h" #include "namespaces.h" #include "path.h" -#include "map.h" -#include "symbol.h" #include "srcline.h" -#include "dso.h" -#include "dsos.h" -#include "machine.h" -#include "auxtrace.h" -#include "util.h" /* O_CLOEXEC for older systems */ -#include "debug.h" #include "string2.h" +#include "symbol.h" +#include "util.h" /* O_CLOEXEC for older systems */ #include "vdso.h" -#include "annotate-data.h" -#include "libdw.h" + +#ifdef HAVE_LIBBPF_SUPPORT +#include <bpf/libbpf.h> + +#include "bpf-event.h" +#include "bpf-utils.h" +#endif static const char *const debuglink_paths[] = {"%.0s%s" ,
@@ -1757,6 +1764,7 @@ void dso__delete(struct dso *dso)auxtrace_cache__free (RC_CHK_ACCESS (dso)->auxtrace_cache);dso_cache__free (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__free_libdw (dso);dso__free_symsrc_filename (dso);nsinfo__zput (RC_CHK_ACCESS (dso)->nsinfo);
@@ -2081,6 +2089,7 @@ void dso__set_symsrc_filename(struct dso *dso, char *val)RC_CHK_ACCESS (dso)->symsrc_filename = val;dso__free_libdw (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__set_has_srcline (dso, true);dso__set_a2l_fails (dso,0 ); }
tools/perf/util/dso.h +19 -2
@@ -304,7 +304,12 @@ DECLARE_RC_STRUCT(dso) {const char *short_name;const char *long_name;void *a2l; +#ifdef HAVE_LIBBFD_SUPPORT +void *a2l_libbfd; +#endif +#ifdef HAVE_LIBDW_SUPPORT void *libdw; +#endif char *symsrc_filename;struct nsinfo *nsinfo;struct auxtrace_cache *auxtrace_cache;
@@ -368,6 +373,20 @@ static inline void dso__set_a2l(struct dso *dso, void *val)RC_CHK_ACCESS (dso)->a2l = val; } +#ifdef HAVE_LIBBFD_SUPPORT +static inline void *dso__a2l_libbfd (const struct dso *dso) +{ +return RC_CHK_ACCESS (dso)->a2l_libbfd; +} + +static inline void dso__set_a2l_libbfd (struct dso *dso,void *val) +{ +RC_CHK_ACCESS (dso)->a2l_libbfd = val; +} +#endif + +struct Dwfl ; +#ifdef HAVE_LIBDW_SUPPORT static inline void *dso__libdw (const struct dso *dso) {return RC_CHK_ACCESS (dso)->libdw;
@@ -378,8 +397,6 @@ static inline void dso__set_libdw(struct dso *dso, void *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 )
tools/perf/util/libbfd.c +5 -5
@@ -239,7 +239,7 @@ static int inline_list__append_dso_a2l(struct dso *dso,struct inline_node *node, struct symbol *sym) { - struct a2l_data *a2l = dso__a2l(dso); + struct a2l_data *a2l = dso__a2l_libbfd(dso);struct symbol *inline_sym =new_inline_sym (dso, sym, a2l->funcname);char *srcline =NULL ;
@@ -258,11 +258,11 @@ int libbfd__addr2line(const char *dso_name, u64 addr,struct a2l_data *a2l;mutex_lock (dso__lock (dso)); - a2l =dso__a2l (dso); + a2l =dso__a2l_libbfd (dso);if (!a2l) { a2l =addr2line_init (dso_name); -dso__set_a2l (dso, a2l); +dso__set_a2l_libbfd (dso, a2l); }if (a2l ==NULL ) {
@@ -323,14 +323,14 @@ int libbfd__addr2line(const char *dso_name, u64 addr,void dso__free_a2l_libbfd (struct dso *dso) { -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l =dso__a2l_libbfd (dso);if (!a2l)return ;addr2line_cleanup (a2l); -dso__set_a2l (dso,NULL ); +dso__set_a2l_libbfd (dso,NULL ); }static int bfd_symbols__cmpvalue (const void *a,const void *b)
tools/perf/util/srcline.c +2 -0
@@ -300,6 +300,7 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso)); out:
@@ -347,6 +348,7 @@ char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso));
Signature · 1 line
2.55.0.1032.g73a4cd73de-goog
[email protected]
AI reviewRe: [PATCH v2 2/3] perf dso: Separate libbfd and cmd addr2line caches to fix confusion
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
When a dso executes addr2line via libbfd it instantiates an
a2l_datapointer. If the DSO later executes via the command-line fallback due to an inlined bug, the a2l pointer is unconditionally populated with achild_processprocess wrapper. If libbfd is queried again, the offline reader attempts to cast structa2l_dataintochild_process, coercing random arbitrary instruction addresses and causing silent aborts. This cleanly splits the caches.Fixes 257046a36750 ("perf srcline: Fallback between addr2line implementations")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v2 3/3 perf libbfd: Report success when an address is found
In reply to Ian Rogerslibbfd__addr2line() only reports success when the caller asked for a file name. addr2inlines() passes a NULL file as it just wants the inline_node populating, so libbfd__addr2line() returns 0 for it unless bfd_find_inliner_info() happened to find an inline frame. addr2line() in srcline.c treats 0 as a failure and tries the next addr2line implementation, which appends its own frames to the inline_node libbfd already appended to. Every frame that isn't inlined is then reported twice, which happens by default when perf is built with libbfd but without libdw as the fallback order is then libbfd followed by the addr2line command:
$ perf record --call-graph dwarf -- perf test -w inlineloop 1
$ perf script --fields +srcline
...
56051a994f8e parent+0x2e (perf)
inlineloop.c:32
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a95841a cmd_test+0xb7a (perf)
??:0
56051a95841a cmd_test+0xb7a (perf)
??:0
...Report success whenever the address is found, like libdw__addr2line() does, and clear the frames appended so far when appending fails so that a following implementation starts from an empty node.
--- tools/perf/util/libbfd.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
tools/perf/util/libbfd.c +13 -3
@@ -286,6 +286,7 @@ int libbfd__addr2line(const char *dso_name, u64 addr,int cnt =0 ;if (node &&inline_list__append_dso_a2l (dso, node, sym)) { +inline_node__clear_frames (node); ret =0 ; goto out; }
@@ -299,23 +300,32 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (node !=NULL ) {if (inline_list__append_dso_a2l (dso, node, sym)) { +inline_node__clear_frames (node); ret =0 ; goto out; } -// found at least one inline frame - ret =1 ; } } }if (file) { *file = a2l->filename ?strdup (a2l->filename) :NULL ; - ret = *file ?1 :0 ; +if (!*file) { +/* Leave ret as 0 so that another addr2line is tried. */ + goto out; + } }if (line) *line = a2l->line; +/* +* The address was found, report success so that the caller doesn't try +* another addr2line implementation that would append the inline frames +* above a second time. +*/ + ret =1 ; + out:mutex_unlock (dso__lock (dso));return ret;
Signature · 1 line
2.55.0.1032.g73a4cd73de-goog
[email protected]
AI reviewRe: [PATCH v2 3/3] perf libbfd: Report success when an address is found
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
libbfd__addr2line()only reports success when the caller asked for a file name.addr2inlines()passes a NULL file as it just wants theinline_nodepopulating, solibbfd__addr2line()returns 0 for it unlessbfd_find_inliner_info()happened to find an inline frame.addr2line()insrcline.ctreats 0 as a failure and tries the next addr2line implementation, which appends its own frames to theinline_nodelibbfd already appended to. Every frame that isn't inlined is then reported twice, which happens by default when perf is built with libbfd but without libdw as the fallback order is then libbfd followed by the addr2line command:$ perf record --call-graph dwarf -- perf test -w inlineloop 1 $ perf script --fields +srcline ... 56051a994f8e parent+0x2e (perf) [ ... ] does, and clear the frames appended so far when appending fails so that a following implementation starts from an empty node.
Quoted 2 lines
Fixes 257046a36750 ("perf srcline: Fallback between addr2line implementations")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerv3 cover perf srcline: Fix addr2line cache and fallback bugs
In reply to Ian RogersThree fixes to source line resolution, all on the addr2line paths.
1) libdw is pointed at the wrong file. dso__libdw_dwfl() opens the Dwfl with the name of the file the samples came from, but when the debug information is in a separate file, symbol loading records that as the dso's symsrc filename and the Dwfl is left referring to a file with no DWARF in it.
2) The dso addr2line cache is shared between implementations. The libbfd reader caches a struct a2l_data and the command line fallback caches a struct child_process through the same pointer, so once a dso has fallen back from one to the other the cached object is read back as the wrong type.
3) libbfd only reports success when the caller asked for a file name. addr2inlines() doesn't, so srcline.c treats a resolved address as a failure and tries the next implementation, which appends its own frames to the ones libbfd already appended. Every frame that isn't inlined is then reported twice. This is the default when perf is built with libbfd but without libdw:
$ perf record --call-graph dwarf -- perf test -w inlineloop 1
$ perf script --fields +srcline
...
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
...With addr2line.style set to "libbfd,addr2line" so the fallback is taken, the script output for that workload drops from 288 lines to 176, and each repeated frame goes from appearing 16 times to 8.
Changes in v3:
- Patch 1: re-read dso__symsrc_filename() under dso__lock() in libbfd__addr2line() and cmd__addr2line() before initializing the addr2line cache. In v2, __get_srcline() fetched dso_name before the lock was acquired, leaving a TOCTOU window where another thread calling dso__set_symsrc_filename() could invalidate the cache and cause the new cache to be initialized with the stale dso_name.
Changes in v2:
- Add patch 3, so that a resolved address isn't retried by the next addr2line implementation and reported twice.
- Patch 1: rewrite the commit message to describe the wrong file the Dwfl is opened with, which is the actual bug, rather than the cache teardown that follows from it, and add the Fixes tag.
- Rebase onto perf-tools-next.
This series is independent of "perf build: Fix builds with clang and BUILD_NONDISTRO" posted earlier and applies with or without it.
Testing:
- "perf test addr2line" and the hists tests pass.
- Builds in 13 configurations, including BUILD_NONDISTRO=1 where the libbfd reader is actually compiled, and with clang and gcc.
- Every patch builds on its own, so the series stays bisectable.
Ian Rogers (3):
perf libdw: Fix Dwfl discovery with split files
perf dso: Separate libbfd and cmd addr2line caches to fix confusion perf libbfd: Report success when an address is found
.../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/util/addr2line.c | 41 +++++++++--- tools/perf/util/dso.c | 58 +++++++++++------ tools/perf/util/dso.h | 26 ++++++-- tools/perf/util/libbfd.c | 53 +++++++++++----- tools/perf/util/libdw.c | 39 ++++++++---- tools/perf/util/srcline.c | 63 +++++++++++++------ tools/perf/util/unwind-libdw.c | 2 + tools/perf/util/unwind-libunwind.c | 62 +++++++++++++----- 9 files changed, 250 insertions(+), 98 deletions(-)
base-commit: 91b0782fc9e9d2f0a40b5256146e014802fdbb36
prerequisite-patch-id: b6fdc526887b71fb66f5fe0c0d41f7ef9493861e
prerequisite-patch-id: d47077f674c700f4f6296e9367f7ddfe004aea89
prerequisite-patch-id: ed0db23450840601762fe85ca1bef06ce6d28fe7
prerequisite-patch-id: d84e6b96d533ee6ed549e26e94216e2da60f7f74
prerequisite-patch-id: 6e8f19551d771621a5037096626cfe7e271b36f9
prerequisite-patch-id: 3768dfd588c7439deb741bce44c74763011c6be7
prerequisite-patch-id: babac99a4faa3b1525e44d3f34ab33a88103334c
prerequisite-patch-id: 5279827453fea1536abc620aa33c887c44e41ee9
prerequisite-patch-id: bdc0d648a577142b9270fb59b54496bc2bf8d5ea
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerPATCH v3 1/3 perf libdw: Fix Dwfl discovery with split files
dso__libdw_dwfl() opens the Dwfl with dso__long_name(), the file the samples came from. When the debug information lives in a separate file, symbol loading finds it and records it as the dso's symsrc filename, but the Dwfl still refers to the original file, so libdw has no DWARF to resolve addresses against.
Open the Dwfl with the symsrc filename when one is known and fall back to the long name when it isn't.
The symsrc file is found while symbols are loaded, which can happen after the Dwfl has been created and cached, so drop the cached Dwfl and the addr2line cache built from it when the symsrc filename is set. That teardown can run while another thread is using the cache, so take the dso lock across both the teardown and the Dwfl accessors, and re-read dso__symsrc_filename() under the dso lock in libbfd__addr2line() and cmd__addr2line() to prevent a TOCTOU race with cache invalidation.
--- .../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/util/addr2line.c | 41 +++++++++--- tools/perf/util/dso.c | 9 +++ tools/perf/util/dso.h | 5 +- tools/perf/util/libbfd.c | 29 ++++++--- tools/perf/util/libdw.c | 39 ++++++++---- tools/perf/util/srcline.c | 61 ++++++++++++------ tools/perf/util/unwind-libdw.c | 2 + tools/perf/util/unwind-libunwind.c | 62 ++++++++++++++----- 9 files changed, 183 insertions(+), 69 deletions(-)
tools/perf/arch/powerpc/util/skip-callchain-idx.c +3 -1
@@ -152,9 +152,10 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc)Dwarf_Addr end = mapped_pc;bool signalp; +mutex_lock (dso__lock (dso)); dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return -1 ; + goto out; mod =dwfl_addrmodule (dwfl, mapped_pc);if (!mod) {
@@ -183,6 +184,7 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc) rc =check_return_reg (ra_regno, frame); out: +mutex_unlock (dso__lock (dso));return rc; }
tools/perf/util/addr2line.c +32 -9
75 lines not on this page · raw message
tools/perf/util/dso.c +9 -0
@@ -2075,3 +2075,12 @@ struct debuginfo *dso__debuginfo(struct dso *dso)free (name);return dinfo; } + +void dso__set_symsrc_filename (struct dso *dso,char *val) +{ +RC_CHK_ACCESS (dso)->symsrc_filename = val; +dso__free_libdw (dso); +dso__free_a2l (dso); +dso__set_has_srcline (dso, true); +dso__set_a2l_fails (dso,0 ); +}
tools/perf/util/dso.h +1 -4
@@ -735,10 +735,7 @@ static inline const char *dso__symsrc_filename(const struct dso *dso)return RC_CHK_ACCESS (dso)->symsrc_filename; } -static inline void dso__set_symsrc_filename (struct dso *dso,char *val) -{ -RC_CHK_ACCESS (dso)->symsrc_filename = val; -} +void dso__set_symsrc_filename (struct dso *dso,char *val);static inline void dso__free_symsrc_filename (struct dso *dso) {
tools/perf/util/libbfd.c +21 -8
@@ -255,7 +255,11 @@ int libbfd__addr2line(const char *dso_name, u64 addr, struct symbol *sym) {int ret =0 ; -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l; + +mutex_lock (dso__lock (dso)); + dso_name =dso__symsrc_filename (dso) ?: dso_name; + a2l =dso__a2l (dso);if (!a2l) { a2l =addr2line_init (dso_name);
@@ -265,7 +269,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (a2l ==NULL ) {if (!symbol_conf.addr2line_disable_warn)pr_warning ("addr2line_init failed for %s\n" , dso_name); -return 0 ; + ret =-1 ; + goto out; } a2l->addr = addr;
@@ -273,14 +278,18 @@ int libbfd__addr2line(const char *dso_name, u64 addr,bfd_map_over_sections (a2l->abfd, find_address_in_section, a2l); -if (!a2l->found) -return 0 ; +if (!a2l->found) { + ret =0 ; + goto out; + }if (unwind_inlines) {int cnt =0 ; -if (node &&inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (node &&inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + } while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, &a2l->funcname, &a2l->line) &&
@@ -290,8 +299,10 @@ int libbfd__addr2line(const char *dso_name, u64 addr, a2l->filename =NULL ;if (node !=NULL ) { -if (inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + }// found at least one inline frame ret =1 ; }
@@ -306,6 +317,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (line) *line = a2l->line; +out: +mutex_unlock (dso__lock (dso));return ret; }
tools/perf/util/libdw.c +27 -12
@@ -35,7 +35,7 @@ struct Dwfl *dso__libdw_dwfl(struct dso *dso)if (dwfl)return dwfl; - dso_name =dso__long_name (dso); + dso_name =dso__symsrc_filename (dso) ?:dso__long_name (dso);/* * Initialize Dwfl session. * We need to open the DSO file to report it to libdw.
@@ -167,32 +167,41 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, struct dso *dso, bool unwind_inlines, struct inline_node *node, struct symbol *sym) { - Dwfl *dwfl = dso__libdw_dwfl(dso); + Dwfl *dwfl;Dwfl_Module *mod;Dwfl_Line *dwline;Dwarf_Addr bias;const char *src;int lineno =0 ; +int ret =0 ; +mutex_lock (dso__lock (dso)); + dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return 0 ; + goto out; mod =dwfl_addrmodule (dwfl, addr); -if (!mod) -return 0 ; +if (!mod) { + ret =0 ; + goto out; + }/* * Get/ignore the dwarf information. Determine the bias, difference * between the regular ELF addr2line addresses and those to use with * libdw. */ -if (!dwfl_module_getdwarf (mod, &bias)) -return 0 ; +if (!dwfl_module_getdwarf (mod, &bias)) { + ret =-1 ; + goto out; + }/* Find source line information for the address. */ dwline =dwfl_module_getsrc (mod, addr + bias); -if (!dwline) -return 0 ; +if (!dwline) { + ret =-1 ; + goto out; + }/* Get line information. */ src = dwfl_lineinfo(dwline, /*addr=*/NULL, &lineno, /*col=*/NULL, /*mtime=*/NULL,
@@ -219,7 +228,8 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,free (*file); *file =NULL ; } -return 0 ; + ret =0 ; + goto out; }/* Walk from the parent down to the leaf. */
@@ -235,8 +245,13 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, *file =NULL ; }inline_node__clear_frames (node); -return 0 ; + ret =0 ; + goto out; } } -return 1 ; + ret =1 ; + +out: +mutex_unlock (dso__lock (dso)); +return ret; }
tools/perf/util/srcline.c +43 -18
152 lines not on this page · raw message
tools/perf/util/unwind-libdw.c +2 -0
@@ -42,8 +42,10 @@ static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,const struct dso *dso = *userdata;assert (dso); +mutex_lock (dso__lock ((struct dso *)dso));if (dso__symsrc_filename (dso) &&strcmp (file_name,dso__symsrc_filename (dso))) *debuginfo_file_name =strdup (dso__symsrc_filename (dso)); +mutex_unlock (dso__lock ((struct dso *)dso));return -1 ; }
tools/perf/util/unwind-libunwind.c +45 -17
99 lines not on this page · raw message
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
dso__libdw_dwfl()opens the Dwfl withdso__long_name(), the file the samples came from. When the debug information lives in a separate file, symbol loading finds it and records it as the dso's symsrc filename, but the Dwfl still refers to the original file, so libdw has no DWARF to resolve addresses against.Open the Dwfl with the symsrc filename when one is known and fall back to the long name when it isn't.
The symsrc file is found while symbols are loaded, which can happen after the Dwfl has been created and cached, so drop the cached Dwfl and the addr2line cache built from it when the symsrc filename is set. That teardown can run while another thread is using the cache, so take the dso lock across both the teardown and the Dwfl accessors, and re-read
dso__symsrc_filename()under the dso lock inlibbfd__addr2line()andcmd__addr2line()to prevent a TOCTOU race with cache invalidation.
Quoted 2 lines
Fixes b7a2b011e962 ("perf powerpc: Unify the skip-callchain-idx libdw with that for addr2line")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v3 2/3 perf dso: Separate libbfd and cmd addr2line caches to fix confusion
In reply to Ian RogersWhen a dso executes addr2line via libbfd it instantiates an a2l_data pointer. If the DSO later executes via the command-line fallback due to an inlined bug, the a2l pointer is unconditionally populated with a child_process process wrapper. If libbfd is queried again, the offline reader attempts to cast struct a2l_data into child_process, coercing random arbitrary instruction addresses and causing silent aborts. This cleanly splits the caches.
--- tools/perf/util/dso.c | 49 +++++++++++++++++++++++---------------- tools/perf/util/dso.h | 21 +++++++++++++++-- tools/perf/util/libbfd.c | 10 ++++---- tools/perf/util/srcline.c | 2 ++ 4 files changed, 55 insertions(+), 27 deletions(-)
tools/perf/util/dso.c +29 -20
@@ -1,38 +1,45 @@// SPDX-License-Identifier: GPL-2.0 +#include "dso.h" + +#include <errno.h> +#include <stdlib.h> +#include <asm/bug.h> +#include <fcntl.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/zalloc.h> -#include <sys/time.h> #include <sys/resource.h> -#include <sys/types.h> #include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> #include <unistd.h> -#include <errno.h> -#include <fcntl.h> -#include <stdlib.h> -#ifdef HAVE_LIBBPF_SUPPORT -#include <bpf/libbpf.h> -#include "bpf-event.h" -#include "bpf-utils.h" -#endif + +#include "annotate-data.h" +#include "auxtrace.h" #include "compress.h" +#include "debug.h" +#include "dso.h" +#include "dsos.h" #include "env.h" +#include "libbfd.h" +#include "libdw.h" +#include "machine.h" +#include "map.h" #include "namespaces.h" #include "path.h" -#include "map.h" -#include "symbol.h" #include "srcline.h" -#include "dso.h" -#include "dsos.h" -#include "machine.h" -#include "auxtrace.h" -#include "util.h" /* O_CLOEXEC for older systems */ -#include "debug.h" #include "string2.h" +#include "symbol.h" +#include "util.h" /* O_CLOEXEC for older systems */ #include "vdso.h" -#include "annotate-data.h" -#include "libdw.h" + +#ifdef HAVE_LIBBPF_SUPPORT +#include <bpf/libbpf.h> + +#include "bpf-event.h" +#include "bpf-utils.h" +#endif static const char *const debuglink_paths[] = {"%.0s%s" ,
@@ -1757,6 +1764,7 @@ void dso__delete(struct dso *dso)auxtrace_cache__free (RC_CHK_ACCESS (dso)->auxtrace_cache);dso_cache__free (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__free_libdw (dso);dso__free_symsrc_filename (dso);nsinfo__zput (RC_CHK_ACCESS (dso)->nsinfo);
@@ -2081,6 +2089,7 @@ void dso__set_symsrc_filename(struct dso *dso, char *val)RC_CHK_ACCESS (dso)->symsrc_filename = val;dso__free_libdw (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__set_has_srcline (dso, true);dso__set_a2l_fails (dso,0 ); }
tools/perf/util/dso.h +19 -2
@@ -304,7 +304,12 @@ DECLARE_RC_STRUCT(dso) {const char *short_name;const char *long_name;void *a2l; +#ifdef HAVE_LIBBFD_SUPPORT +void *a2l_libbfd; +#endif +#ifdef HAVE_LIBDW_SUPPORT void *libdw; +#endif char *symsrc_filename;struct nsinfo *nsinfo;struct auxtrace_cache *auxtrace_cache;
@@ -368,6 +373,20 @@ static inline void dso__set_a2l(struct dso *dso, void *val)RC_CHK_ACCESS (dso)->a2l = val; } +#ifdef HAVE_LIBBFD_SUPPORT +static inline void *dso__a2l_libbfd (const struct dso *dso) +{ +return RC_CHK_ACCESS (dso)->a2l_libbfd; +} + +static inline void dso__set_a2l_libbfd (struct dso *dso,void *val) +{ +RC_CHK_ACCESS (dso)->a2l_libbfd = val; +} +#endif + +struct Dwfl ; +#ifdef HAVE_LIBDW_SUPPORT static inline void *dso__libdw (const struct dso *dso) {return RC_CHK_ACCESS (dso)->libdw;
@@ -378,8 +397,6 @@ static inline void dso__set_libdw(struct dso *dso, void *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 )
tools/perf/util/libbfd.c +5 -5
@@ -239,7 +239,7 @@ static int inline_list__append_dso_a2l(struct dso *dso,struct inline_node *node, struct symbol *sym) { - struct a2l_data *a2l = dso__a2l(dso); + struct a2l_data *a2l = dso__a2l_libbfd(dso);struct symbol *inline_sym =new_inline_sym (dso, sym, a2l->funcname);char *srcline =NULL ;
@@ -259,11 +259,11 @@ int libbfd__addr2line(const char *dso_name, u64 addr,mutex_lock (dso__lock (dso)); dso_name =dso__symsrc_filename (dso) ?: dso_name; - a2l =dso__a2l (dso); + a2l =dso__a2l_libbfd (dso);if (!a2l) { a2l =addr2line_init (dso_name); -dso__set_a2l (dso, a2l); +dso__set_a2l_libbfd (dso, a2l); }if (a2l ==NULL ) {
@@ -324,14 +324,14 @@ int libbfd__addr2line(const char *dso_name, u64 addr,void dso__free_a2l_libbfd (struct dso *dso) { -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l =dso__a2l_libbfd (dso);if (!a2l)return ;addr2line_cleanup (a2l); -dso__set_a2l (dso,NULL ); +dso__set_a2l_libbfd (dso,NULL ); }static int bfd_symbols__cmpvalue (const void *a,const void *b)
tools/perf/util/srcline.c +2 -0
@@ -300,6 +300,7 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso)); out:
@@ -347,6 +348,7 @@ char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso));
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 2/3] perf dso: Separate libbfd and cmd addr2line caches to fix confusion
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] perf: redundant duplicate include of dso.h --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: redundant duplicate include of dso.h
--
commit 44eae5fccba5e70d2ae1177b6b716c83f9242611
Author: Ian Rogers <[email protected]>
perf dso: Separate libbfd and cmd addr2line caches to fix confusion
This commit separates the libbfd and command-line addr2line caches in the dso struct to prevent type confusion during fallback. By splitting the caches, it avoids casting a2l_data into child_process, which previously caused arbitrary instruction address coercion and silent aborts.
Quoted 13 lines
tools/perf/util/dso.c +6
-0@@ -1,38 +1,45 @@// SPDX-License-Identifier: GPL-2.0 +#include "dso.h" + +#include <errno.h> +#include <stdlib.h> +#include <asm/bug.h> +#include <fcntl.h>
[ ... ]
tools/perf/util/dso.c line 22
Quoted 3 lines
#include "
compress.h"tools/perf/util/dso.c +2
-0+#include "debug.h" +#include "dso.h"
[Severity: Low]
Is this second inclusion of dso.h intentional?
The patch already adds an include for "dso.h" at the very top of tools/perf/util/dso.c, so this results in a redundant duplicate include remaining in the alphabetical block.
Quoted 4 lines
+#include "
dsos.h"
#include "env.h"
#include "namespaces.h"
#include "path.h"
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v3 3/3 perf libbfd: Report success when an address is found
In reply to Ian Rogerslibbfd__addr2line() only reports success when the caller asked for a file name. addr2inlines() passes a NULL file as it just wants the inline_node populating, so libbfd__addr2line() returns 0 for it unless bfd_find_inliner_info() happened to find an inline frame. addr2line() in srcline.c treats 0 as a failure and tries the next addr2line implementation, which appends its own frames to the inline_node libbfd already appended to. Every frame that isn't inlined is then reported twice, which happens by default when perf is built with libbfd but without libdw as the fallback order is then libbfd followed by the addr2line command:
$ perf record --call-graph dwarf -- perf test -w inlineloop 1
$ perf script --fields +srcline
...
56051a994f8e parent+0x2e (perf)
inlineloop.c:32
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a95841a cmd_test+0xb7a (perf)
??:0
56051a95841a cmd_test+0xb7a (perf)
??:0
...Report success whenever the address is found, like libdw__addr2line() does, and clear the frames appended so far when appending fails so that a following implementation starts from an empty node.
--- tools/perf/util/libbfd.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
tools/perf/util/libbfd.c +13 -3
@@ -287,6 +287,7 @@ int libbfd__addr2line(const char *dso_name, u64 addr,int cnt =0 ;if (node &&inline_list__append_dso_a2l (dso, node, sym)) { +inline_node__clear_frames (node); ret =0 ; goto out; }
@@ -300,23 +301,32 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (node !=NULL ) {if (inline_list__append_dso_a2l (dso, node, sym)) { +inline_node__clear_frames (node); ret =0 ; goto out; } -// found at least one inline frame - ret =1 ; } } }if (file) { *file = a2l->filename ?strdup (a2l->filename) :NULL ; - ret = *file ?1 :0 ; +if (!*file) { +/* Leave ret as 0 so that another addr2line is tried. */ + goto out; + } }if (line) *line = a2l->line; +/* +* The address was found, report success so that the caller doesn't try +* another addr2line implementation that would append the inline frames +* above a second time. +*/ + ret =1 ; + out:mutex_unlock (dso__lock (dso));return ret;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v3 3/3] perf libbfd: Report success when an address is found
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
libbfd__addr2line()only reports success when the caller asked for a file name.addr2inlines()passes a NULL file as it just wants theinline_nodepopulating, solibbfd__addr2line()returns 0 for it unlessbfd_find_inliner_info()happened to find an inline frame.addr2line()insrcline.ctreats 0 as a failure and tries the next addr2line implementation, which appends its own frames to theinline_nodelibbfd already appended to. Every frame that isn't inlined is then reported twice, which happens by default when perf is built with libbfd but without libdw as the fallback order is then libbfd followed by the addr2line command:$ perf record --call-graph dwarf -- perf test -w inlineloop 1 $ perf script --fields +srcline ... 56051a994f8e parent+0x2e (perf) [ ... ] does, and clear the frames appended so far when appending fails so that a following implementation starts from an empty node.
Quoted 2 lines
Fixes 257046a36750 ("perf srcline: Fallback between addr2line implementations")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerv4 cover perf srcline: Fix addr2line cache and fallback bugs
In reply to Ian RogersThree fixes to source line resolution, all on the addr2line paths.
1) libdw is pointed at the wrong file. dso__libdw_dwfl() opens the Dwfl with the name of the file the samples came from, but when the debug information is in a separate file, symbol loading records that as the dso's symsrc filename and the Dwfl is left referring to a file with no DWARF in it.
2) The dso addr2line cache is shared between implementations. The libbfd reader caches a struct a2l_data and the command line fallback caches a struct child_process through the same pointer, so once a dso has fallen back from one to the other the cached object is read back as the wrong type.
3) libbfd only reports success when the caller asked for a file name. addr2inlines() doesn't, so srcline.c treats a resolved address as a failure and tries the next implementation, which appends its own frames to the ones libbfd already appended. Every frame that isn't inlined is then reported twice. This is the default when perf is built with libbfd but without libdw:
$ perf record --call-graph dwarf -- perf test -w inlineloop 1
$ perf script --fields +srcline
...
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
...With addr2line.style set to "libbfd,addr2line" so the fallback is taken, the script output for that workload drops from 288 lines to 176, and each repeated frame goes from appearing 16 times to 8.
Changes in v4:
- Patch 1: add the headers that are used, rather than relying on glibc including them for us, since these files are rearranging their includes anyway: <unistd.h> in addr2line.c for write(), <stdio.h> in srcline.c for asprintf(), and <unistd.h> and <limits.h> in unwind-libunwind.c for close() and PATH_MAX. These are all pre-existing omissions that musl libc builds would trip over.
- Patch 2: drop the duplicate "dso.h" that the include reordering left behind in the alphabetical block of dso.c, as it is already included at the top of the file.
Changes in v3:
- Patch 1: re-read dso__symsrc_filename() under dso__lock() in libbfd__addr2line() and cmd__addr2line() before initializing the addr2line cache. In v2, __get_srcline() fetched dso_name before the lock was acquired, leaving a TOCTOU window where another thread calling dso__set_symsrc_filename() could invalidate the cache and cause the new cache to be initialized with the stale dso_name.
Changes in v2:
- Add patch 3, so that a resolved address isn't retried by the next addr2line implementation and reported twice.
- Patch 1: rewrite the commit message to describe the wrong file the Dwfl is opened with, which is the actual bug, rather than the cache teardown that follows from it, and add the Fixes tag.
- Rebase onto perf-tools-next.
This series is independent of "perf build: Fix builds with clang and BUILD_NONDISTRO" posted earlier and applies with or without it.
Testing:
- "perf test addr2line" and the hists tests pass.
- Builds in 13 configurations, including BUILD_NONDISTRO=1 where the libbfd reader is actually compiled, and with clang and gcc.
- Every patch builds on its own, so the series stays bisectable.
Ian Rogers (3):
perf libdw: Fix Dwfl discovery with split files
perf dso: Separate libbfd and cmd addr2line caches to fix confusion perf libbfd: Report success when an address is found
.../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/util/addr2line.c | 42 +++++++++--- tools/perf/util/dso.c | 57 +++++++++++------ tools/perf/util/dso.h | 26 ++++++-- tools/perf/util/libbfd.c | 53 ++++++++++----- tools/perf/util/libdw.c | 39 +++++++---- tools/perf/util/srcline.c | 64 +++++++++++++------ tools/perf/util/unwind-libdw.c | 2 + tools/perf/util/unwind-libunwind.c | 64 ++++++++++++++----- 9 files changed, 253 insertions(+), 98 deletions(-)
base-commit: 91b0782fc9e9d2f0a40b5256146e014802fdbb36
prerequisite-patch-id: b6fdc526887b71fb66f5fe0c0d41f7ef9493861e
prerequisite-patch-id: d47077f674c700f4f6296e9367f7ddfe004aea89
prerequisite-patch-id: ed0db23450840601762fe85ca1bef06ce6d28fe7
prerequisite-patch-id: d84e6b96d533ee6ed549e26e94216e2da60f7f74
prerequisite-patch-id: 6e8f19551d771621a5037096626cfe7e271b36f9
prerequisite-patch-id: 3768dfd588c7439deb741bce44c74763011c6be7
prerequisite-patch-id: babac99a4faa3b1525e44d3f34ab33a88103334c
prerequisite-patch-id: 5279827453fea1536abc620aa33c887c44e41ee9
prerequisite-patch-id: bdc0d648a577142b9270fb59b54496bc2bf8d5ea
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerPATCH v4 1/3 perf libdw: Fix Dwfl discovery with split files
dso__libdw_dwfl() opens the Dwfl with dso__long_name(), the file the samples came from. When the debug information lives in a separate file, symbol loading finds it and records it as the dso's symsrc filename, but the Dwfl still refers to the original file, so libdw has no DWARF to resolve addresses against.
Open the Dwfl with the symsrc filename when one is known and fall back to the long name when it isn't.
The symsrc file is found while symbols are loaded, which can happen after the Dwfl has been created and cached, so drop the cached Dwfl and the addr2line cache built from it when the symsrc filename is set. That teardown can run while another thread is using the cache, so take the dso lock across both the teardown and the Dwfl accessors, and re-read dso__symsrc_filename() under the dso lock in libbfd__addr2line() and cmd__addr2line() to prevent a TOCTOU race with cache invalidation.
--- .../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/util/addr2line.c | 42 +++++++++--- tools/perf/util/dso.c | 9 +++ tools/perf/util/dso.h | 5 +- tools/perf/util/libbfd.c | 29 ++++++--- tools/perf/util/libdw.c | 39 +++++++---- tools/perf/util/srcline.c | 62 ++++++++++++------ tools/perf/util/unwind-libdw.c | 2 + tools/perf/util/unwind-libunwind.c | 64 ++++++++++++++----- 9 files changed, 187 insertions(+), 69 deletions(-)
tools/perf/arch/powerpc/util/skip-callchain-idx.c +3 -1
@@ -152,9 +152,10 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc)Dwarf_Addr end = mapped_pc;bool signalp; +mutex_lock (dso__lock (dso)); dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return -1 ; + goto out; mod =dwfl_addrmodule (dwfl, mapped_pc);if (!mod) {
@@ -183,6 +184,7 @@ static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc) rc =check_return_reg (ra_regno, frame); out: +mutex_unlock (dso__lock (dso));return rc; }
tools/perf/util/addr2line.c +33 -9
82 lines not on this page · raw message
tools/perf/util/dso.c +9 -0
@@ -2075,3 +2075,12 @@ struct debuginfo *dso__debuginfo(struct dso *dso)free (name);return dinfo; } + +void dso__set_symsrc_filename (struct dso *dso,char *val) +{ +RC_CHK_ACCESS (dso)->symsrc_filename = val; +dso__free_libdw (dso); +dso__free_a2l (dso); +dso__set_has_srcline (dso, true); +dso__set_a2l_fails (dso,0 ); +}
tools/perf/util/dso.h +1 -4
@@ -735,10 +735,7 @@ static inline const char *dso__symsrc_filename(const struct dso *dso)return RC_CHK_ACCESS (dso)->symsrc_filename; } -static inline void dso__set_symsrc_filename (struct dso *dso,char *val) -{ -RC_CHK_ACCESS (dso)->symsrc_filename = val; -} +void dso__set_symsrc_filename (struct dso *dso,char *val);static inline void dso__free_symsrc_filename (struct dso *dso) {
tools/perf/util/libbfd.c +21 -8
@@ -255,7 +255,11 @@ int libbfd__addr2line(const char *dso_name, u64 addr, struct symbol *sym) {int ret =0 ; -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l; + +mutex_lock (dso__lock (dso)); + dso_name =dso__symsrc_filename (dso) ?: dso_name; + a2l =dso__a2l (dso);if (!a2l) { a2l =addr2line_init (dso_name);
@@ -265,7 +269,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (a2l ==NULL ) {if (!symbol_conf.addr2line_disable_warn)pr_warning ("addr2line_init failed for %s\n" , dso_name); -return 0 ; + ret =-1 ; + goto out; } a2l->addr = addr;
@@ -273,14 +278,18 @@ int libbfd__addr2line(const char *dso_name, u64 addr,bfd_map_over_sections (a2l->abfd, find_address_in_section, a2l); -if (!a2l->found) -return 0 ; +if (!a2l->found) { + ret =0 ; + goto out; + }if (unwind_inlines) {int cnt =0 ; -if (node &&inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (node &&inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + } while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, &a2l->funcname, &a2l->line) &&
@@ -290,8 +299,10 @@ int libbfd__addr2line(const char *dso_name, u64 addr, a2l->filename =NULL ;if (node !=NULL ) { -if (inline_list__append_dso_a2l (dso, node, sym)) -return 0 ; +if (inline_list__append_dso_a2l (dso, node, sym)) { + ret =0 ; + goto out; + }// found at least one inline frame ret =1 ; }
@@ -306,6 +317,8 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (line) *line = a2l->line; +out: +mutex_unlock (dso__lock (dso));return ret; }
tools/perf/util/libdw.c +27 -12
@@ -35,7 +35,7 @@ struct Dwfl *dso__libdw_dwfl(struct dso *dso)if (dwfl)return dwfl; - dso_name =dso__long_name (dso); + dso_name =dso__symsrc_filename (dso) ?:dso__long_name (dso);/* * Initialize Dwfl session. * We need to open the DSO file to report it to libdw.
@@ -167,32 +167,41 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, struct dso *dso, bool unwind_inlines, struct inline_node *node, struct symbol *sym) { - Dwfl *dwfl = dso__libdw_dwfl(dso); + Dwfl *dwfl;Dwfl_Module *mod;Dwfl_Line *dwline;Dwarf_Addr bias;const char *src;int lineno =0 ; +int ret =0 ; +mutex_lock (dso__lock (dso)); + dwfl =dso__libdw_dwfl (dso);if (!dwfl) -return 0 ; + goto out; mod =dwfl_addrmodule (dwfl, addr); -if (!mod) -return 0 ; +if (!mod) { + ret =0 ; + goto out; + }/* * Get/ignore the dwarf information. Determine the bias, difference * between the regular ELF addr2line addresses and those to use with * libdw. */ -if (!dwfl_module_getdwarf (mod, &bias)) -return 0 ; +if (!dwfl_module_getdwarf (mod, &bias)) { + ret =-1 ; + goto out; + }/* Find source line information for the address. */ dwline =dwfl_module_getsrc (mod, addr + bias); -if (!dwline) -return 0 ; +if (!dwline) { + ret =-1 ; + goto out; + }/* Get line information. */ src = dwfl_lineinfo(dwline, /*addr=*/NULL, &lineno, /*col=*/NULL, /*mtime=*/NULL,
@@ -219,7 +228,8 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,free (*file); *file =NULL ; } -return 0 ; + ret =0 ; + goto out; }/* Walk from the parent down to the leaf. */
@@ -235,8 +245,13 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr, *file =NULL ; }inline_node__clear_frames (node); -return 0 ; + ret =0 ; + goto out; } } -return 1 ; + ret =1 ; + +out: +mutex_unlock (dso__lock (dso)); +return ret; }
tools/perf/util/srcline.c +44 -18
153 lines not on this page · raw message
tools/perf/util/unwind-libdw.c +2 -0
@@ -42,8 +42,10 @@ static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,const struct dso *dso = *userdata;assert (dso); +mutex_lock (dso__lock ((struct dso *)dso));if (dso__symsrc_filename (dso) &&strcmp (file_name,dso__symsrc_filename (dso))) *debuginfo_file_name =strdup (dso__symsrc_filename (dso)); +mutex_unlock (dso__lock ((struct dso *)dso));return -1 ; }
tools/perf/util/unwind-libunwind.c +47 -17
101 lines not on this page · raw message
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
dso__libdw_dwfl()opens the Dwfl withdso__long_name(), the file the samples came from. When the debug information lives in a separate file, symbol loading finds it and records it as the dso's symsrc filename, but the Dwfl still refers to the original file, so libdw has no DWARF to resolve addresses against.Open the Dwfl with the symsrc filename when one is known and fall back to the long name when it isn't.
The symsrc file is found while symbols are loaded, which can happen after the Dwfl has been created and cached, so drop the cached Dwfl and the addr2line cache built from it when the symsrc filename is set. That teardown can run while another thread is using the cache, so take the dso lock across both the teardown and the Dwfl accessors, and re-read
dso__symsrc_filename()under the dso lock inlibbfd__addr2line()andcmd__addr2line()to prevent a TOCTOU race with cache invalidation.
Quoted 2 lines
Fixes b7a2b011e962 ("perf powerpc: Unify the skip-callchain-idx libdw with that for addr2line")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v4 2/3 perf dso: Separate libbfd and cmd addr2line caches to fix confusion
In reply to Ian RogersWhen a dso executes addr2line via libbfd it instantiates an a2l_data pointer. If the DSO later executes via the command-line fallback due to an inlined bug, the a2l pointer is unconditionally populated with a child_process process wrapper. If libbfd is queried again, the offline reader attempts to cast struct a2l_data into child_process, coercing random arbitrary instruction addresses and causing silent aborts. This cleanly splits the caches.
--- tools/perf/util/dso.c | 48 +++++++++++++++++++++++---------------- tools/perf/util/dso.h | 21 +++++++++++++++-- tools/perf/util/libbfd.c | 10 ++++---- tools/perf/util/srcline.c | 2 ++ 4 files changed, 54 insertions(+), 27 deletions(-)
tools/perf/util/dso.c +28 -20
@@ -1,38 +1,44 @@// SPDX-License-Identifier: GPL-2.0 +#include "dso.h" + +#include <errno.h> +#include <stdlib.h> +#include <asm/bug.h> +#include <fcntl.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/zalloc.h> -#include <sys/time.h> #include <sys/resource.h> -#include <sys/types.h> #include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> #include <unistd.h> -#include <errno.h> -#include <fcntl.h> -#include <stdlib.h> -#ifdef HAVE_LIBBPF_SUPPORT -#include <bpf/libbpf.h> -#include "bpf-event.h" -#include "bpf-utils.h" -#endif + +#include "annotate-data.h" +#include "auxtrace.h" #include "compress.h" +#include "debug.h" +#include "dsos.h" #include "env.h" +#include "libbfd.h" +#include "libdw.h" +#include "machine.h" +#include "map.h" #include "namespaces.h" #include "path.h" -#include "map.h" -#include "symbol.h" #include "srcline.h" -#include "dso.h" -#include "dsos.h" -#include "machine.h" -#include "auxtrace.h" -#include "util.h" /* O_CLOEXEC for older systems */ -#include "debug.h" #include "string2.h" +#include "symbol.h" +#include "util.h" /* O_CLOEXEC for older systems */ #include "vdso.h" -#include "annotate-data.h" -#include "libdw.h" + +#ifdef HAVE_LIBBPF_SUPPORT +#include <bpf/libbpf.h> + +#include "bpf-event.h" +#include "bpf-utils.h" +#endif static const char *const debuglink_paths[] = {"%.0s%s" ,
@@ -1757,6 +1763,7 @@ void dso__delete(struct dso *dso)auxtrace_cache__free (RC_CHK_ACCESS (dso)->auxtrace_cache);dso_cache__free (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__free_libdw (dso);dso__free_symsrc_filename (dso);nsinfo__zput (RC_CHK_ACCESS (dso)->nsinfo);
@@ -2081,6 +2088,7 @@ void dso__set_symsrc_filename(struct dso *dso, char *val)RC_CHK_ACCESS (dso)->symsrc_filename = val;dso__free_libdw (dso);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso);dso__set_has_srcline (dso, true);dso__set_a2l_fails (dso,0 ); }
tools/perf/util/dso.h +19 -2
@@ -304,7 +304,12 @@ DECLARE_RC_STRUCT(dso) {const char *short_name;const char *long_name;void *a2l; +#ifdef HAVE_LIBBFD_SUPPORT +void *a2l_libbfd; +#endif +#ifdef HAVE_LIBDW_SUPPORT void *libdw; +#endif char *symsrc_filename;struct nsinfo *nsinfo;struct auxtrace_cache *auxtrace_cache;
@@ -368,6 +373,20 @@ static inline void dso__set_a2l(struct dso *dso, void *val)RC_CHK_ACCESS (dso)->a2l = val; } +#ifdef HAVE_LIBBFD_SUPPORT +static inline void *dso__a2l_libbfd (const struct dso *dso) +{ +return RC_CHK_ACCESS (dso)->a2l_libbfd; +} + +static inline void dso__set_a2l_libbfd (struct dso *dso,void *val) +{ +RC_CHK_ACCESS (dso)->a2l_libbfd = val; +} +#endif + +struct Dwfl ; +#ifdef HAVE_LIBDW_SUPPORT static inline void *dso__libdw (const struct dso *dso) {return RC_CHK_ACCESS (dso)->libdw;
@@ -378,8 +397,6 @@ static inline void dso__set_libdw(struct dso *dso, void *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 )
tools/perf/util/libbfd.c +5 -5
@@ -239,7 +239,7 @@ static int inline_list__append_dso_a2l(struct dso *dso,struct inline_node *node, struct symbol *sym) { - struct a2l_data *a2l = dso__a2l(dso); + struct a2l_data *a2l = dso__a2l_libbfd(dso);struct symbol *inline_sym =new_inline_sym (dso, sym, a2l->funcname);char *srcline =NULL ;
@@ -259,11 +259,11 @@ int libbfd__addr2line(const char *dso_name, u64 addr,mutex_lock (dso__lock (dso)); dso_name =dso__symsrc_filename (dso) ?: dso_name; - a2l =dso__a2l (dso); + a2l =dso__a2l_libbfd (dso);if (!a2l) { a2l =addr2line_init (dso_name); -dso__set_a2l (dso, a2l); +dso__set_a2l_libbfd (dso, a2l); }if (a2l ==NULL ) {
@@ -324,14 +324,14 @@ int libbfd__addr2line(const char *dso_name, u64 addr,void dso__free_a2l_libbfd (struct dso *dso) { -struct a2l_data *a2l =dso__a2l (dso); +struct a2l_data *a2l =dso__a2l_libbfd (dso);if (!a2l)return ;addr2line_cleanup (a2l); -dso__set_a2l (dso,NULL ); +dso__set_a2l_libbfd (dso,NULL ); }static int bfd_symbols__cmpvalue (const void *a,const void *b)
tools/perf/util/srcline.c +2 -0
@@ -301,6 +301,7 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso)); out:
@@ -348,6 +349,7 @@ char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)if (dso__a2l_fails (dso) >A2L_FAIL_LIMIT ) {dso__set_has_srcline (dso, false);dso__free_a2l (dso); +dso__free_a2l_libbfd (dso); }mutex_unlock (dso__lock (dso));
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 2/3] perf dso: Separate libbfd and cmd addr2line caches to fix confusion
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
When a dso executes addr2line via libbfd it instantiates an
a2l_datapointer. If the DSO later executes via the command-line fallback due to an inlined bug, the a2l pointer is unconditionally populated with achild_processprocess wrapper. If libbfd is queried again, the offline reader attempts to cast structa2l_dataintochild_process, coercing random arbitrary instruction addresses and causing silent aborts. This cleanly splits the caches.Fixes 257046a36750 ("perf srcline: Fallback between addr2line implementations")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v4 3/3 perf libbfd: Report success when an address is found
In reply to Ian Rogerslibbfd__addr2line() only reports success when the caller asked for a file name. addr2inlines() passes a NULL file as it just wants the inline_node populating, so libbfd__addr2line() returns 0 for it unless bfd_find_inliner_info() happened to find an inline frame. addr2line() in srcline.c treats 0 as a failure and tries the next addr2line implementation, which appends its own frames to the inline_node libbfd already appended to. Every frame that isn't inlined is then reported twice, which happens by default when perf is built with libbfd but without libdw as the fallback order is then libbfd followed by the addr2line command:
$ perf record --call-graph dwarf -- perf test -w inlineloop 1
$ perf script --fields +srcline
...
56051a994f8e parent+0x2e (perf)
inlineloop.c:32
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a99503a inlineloop+0x8a (perf)
inlineloop.c:47
56051a95841a cmd_test+0xb7a (perf)
??:0
56051a95841a cmd_test+0xb7a (perf)
??:0
...Report success whenever the address is found, like libdw__addr2line() does, and clear the frames appended so far when appending fails so that a following implementation starts from an empty node.
--- tools/perf/util/libbfd.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
tools/perf/util/libbfd.c +13 -3
@@ -287,6 +287,7 @@ int libbfd__addr2line(const char *dso_name, u64 addr,int cnt =0 ;if (node &&inline_list__append_dso_a2l (dso, node, sym)) { +inline_node__clear_frames (node); ret =0 ; goto out; }
@@ -300,23 +301,32 @@ int libbfd__addr2line(const char *dso_name, u64 addr,if (node !=NULL ) {if (inline_list__append_dso_a2l (dso, node, sym)) { +inline_node__clear_frames (node); ret =0 ; goto out; } -// found at least one inline frame - ret =1 ; } } }if (file) { *file = a2l->filename ?strdup (a2l->filename) :NULL ; - ret = *file ?1 :0 ; +if (!*file) { +/* Leave ret as 0 so that another addr2line is tried. */ + goto out; + } }if (line) *line = a2l->line; +/* +* The address was found, report success so that the caller doesn't try +* another addr2line implementation that would append the inline frames +* above a second time. +*/ + ret =1 ; + out:mutex_unlock (dso__lock (dso));return ret;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4 3/3] perf libbfd: Report success when an address is found
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
libbfd__addr2line()only reports success when the caller asked for a file name.addr2inlines()passes a NULL file as it just wants theinline_nodepopulating, solibbfd__addr2line()returns 0 for it unlessbfd_find_inliner_info()happened to find an inline frame.addr2line()insrcline.ctreats 0 as a failure and tries the next addr2line implementation, which appends its own frames to theinline_nodelibbfd already appended to. Every frame that isn't inlined is then reported twice, which happens by default when perf is built with libbfd but without libdw as the fallback order is then libbfd followed by the addr2line command:$ perf record --call-graph dwarf -- perf test -w inlineloop 1 $ perf script --fields +srcline ... 56051a994f8e parent+0x2e (perf) [ ... ] does, and clear the frames appended so far when appending fails so that a following implementation starts from an empty node.
Quoted 2 lines
Fixes 257046a36750 ("perf srcline: Fallback between addr2line implementations")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v3 0/3] perf srcline: Fix addr2line cache and fallback bugs
In reply to Ian RogersQuoted 30 lines · Ian Rogers
Three fixes to source line resolution, all on the addr2line paths.
1) libdw is pointed at the wrong file.
dso__libdw_dwfl()opens the Dwfl with the name of the file the samples came from, but when the debug information is in a separate file, symbol loading records that as the dso's symsrc filename and the Dwfl is left referring to a file with no DWARF in it.2) The dso addr2line cache is shared between implementations. The libbfd reader caches a struct
a2l_dataand the command line fallback caches a structchild_processthrough the same pointer, so once a dso has fallen back from one to the other the cached object is read back as the wrong type.3) libbfd only reports success when the caller asked for a file name.
addr2inlines()doesn't, sosrcline.ctreats a resolved address as a failure and tries the next implementation, which appends its own frames to the ones libbfd already appended. Every frame that isn't inlined is then reported twice. This is the default when perf is built with libbfd but without libdw:$ perf record --call-graph dwarf -- perf test -w inlineloop 1 $ perf script --fields +srcline ... 56051a99503a inlineloop+0x8a (perf) inlineloop.c:47 56051a99503a inlineloop+0x8a (perf) inlineloop.c:47 ...
Quoted 3 lines
With addr2line.style set to "libbfd,addr2line" so the fallback is taken, the script output for that workload drops from 288 lines to 176, and each repeated frame goes from appearing 16 times to 8.
Thanks, applied to perf-tools-next, for v7.4.
- Arnaldo