]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf powerpc: Unify the skip-callchain-idx libdw with that for addr2line
authorIan Rogers <irogers@google.com>
Sat, 17 Jan 2026 05:28:33 +0000 (21:28 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 20 Jan 2026 16:04:38 +0000 (13:04 -0300)
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>
tools/perf/arch/powerpc/util/skip-callchain-idx.c
tools/perf/util/dso.c
tools/perf/util/dso.h
tools/perf/util/libdw.c
tools/perf/util/libdw.h
tools/perf/util/srcline.c

index 356786432fd3cac8e3472bcf97b018a41a970515..e57f10798fa6a9a4f08f468fdb7116929b5ba3e7 100644 (file)
  * The libdwfl code in this file is based on code from elfutils
  * (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
@@ -149,44 +141,22 @@ static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
  *             yet used)
  *     -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;
        Dwfl            *dwfl;
        Dwfl_Module     *mod;
        Dwarf_Frame     *frame;
        int             ra_regno;
-       Dwarf_Addr      start = pc;
-       Dwarf_Addr      end = pc;
+       Dwarf_Addr      start = mapped_pc;
+       Dwarf_Addr      end = mapped_pc;
        bool            signalp;
-       const char      *exec_file = dso__long_name(dso);
-
-       dwfl = RC_CHK_ACCESS(dso)->dwfl;
-
-       if (!dwfl) {
-               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);
+       dwfl = dso__libdw_dwfl(dso);
+       if (!dwfl)
+               return -1;
+
+       mod = dwfl_addrmodule(dwfl, mapped_pc);
        if (!mod) {
                pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
                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
         * .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) {
-               frame = get_dwarf_frame(mod, pc);
+               frame = get_dwarf_frame(mod, mapped_pc);
                if (!frame)
                        goto out;
        }
@@ -264,7 +234,7 @@ int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
                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",
                dso__long_name(dso), al.sym->name, ip, rc);
index 143720d1ecb158d1447ec981643eae39fcdd0ee7..dce207c7f862e760557f98a9701008210cb7ebc2 100644 (file)
@@ -1612,7 +1612,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_libdw(dso);
+       dso__free_libdw(dso);
        dso__free_symsrc_filename(dso);
        nsinfo__zput(RC_CHK_ACCESS(dso)->nsinfo);
        mutex_destroy(dso__lock(dso));
index 4aee23775054cf830661994a754c5529b3fd1c05..2953880850318f4827f1d2dd1c3f804185eeb67b 100644 (file)
@@ -268,11 +268,8 @@ DECLARE_RC_STRUCT(dso) {
        const char       *short_name;
        const char       *long_name;
        void             *a2l;
-       void             *a2l_libdw;
+       void             *libdw;
        char             *symsrc_filename;
-#if defined(__powerpc__)
-       void            *dwfl;                  /* DWARF debug info */
-#endif
        struct nsinfo   *nsinfo;
        struct auxtrace_cache *auxtrace_cache;
        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;
 }
 
-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)
 {
        return RC_CHK_ACCESS(dso)->a2l_fails;
index b96c4e0d728fb0b7beda2aa0dfa5286cb66e0902..216977884103087bb376a998b19af4afafac2753 100644 (file)
@@ -8,14 +8,62 @@
 #include <unistd.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) {
                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 {
@@ -63,58 +111,21 @@ static int libdw_a2l_cb(Dwarf_Die *die, void *_args)
        return 0;
 }
 
-int libdw__addr2line(const char *dso_name, u64 addr,
-                    char **file, unsigned int *line_nr,
+int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,
                     struct dso *dso, bool unwind_inlines,
                     struct inline_node *node, struct symbol *sym)
 {
-       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,
-       };
-       Dwfl *dwfl = dso__a2l_libdw(dso);
+       Dwfl *dwfl = dso__libdw_dwfl(dso);
        Dwfl_Module *mod;
        Dwfl_Line *dwline;
        Dwarf_Addr bias;
        const char *src;
        int lineno = 0;
 
-       if (!dwfl) {
-               /*
-                * 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);
-       }
+       if (!dwfl)
+               return 0;
 
+       mod = dwfl_addrmodule(dwfl, addr);
        if (!mod)
                return 0;
 
index 0f8d7b4a11a5499894a6eb2995ba4f9777917947..b1209473741590426e80602cb6bccbe5b09f9732 100644 (file)
@@ -11,7 +11,6 @@ struct symbol;
 #ifdef HAVE_LIBDW_SUPPORT
 /*
  * libdw__addr2line - Convert address to source location using libdw
- * @dso_name: Name of the DSO
  * @addr: Address to resolve
  * @file: Pointer to return filename (caller must free)
  * @line_nr: Pointer to return line number
@@ -26,23 +25,22 @@ struct symbol;
  *
  * 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,
                     bool unwind_inlines, struct inline_node *node,
                     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
  *
  * 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 */
 
-static inline int libdw__addr2line(const char *dso_name __maybe_unused,
-                                  u64 addr __maybe_unused, char **file __maybe_unused,
+static inline int libdw__addr2line(u64 addr __maybe_unused, char **file __maybe_unused,
                                   unsigned int *line_nr __maybe_unused,
                                   struct dso *dso __maybe_unused,
                                   bool unwind_inlines __maybe_unused,
@@ -52,7 +50,7 @@ static inline int libdw__addr2line(const char *dso_name __maybe_unused,
        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 */
index 28fa1abd1fd31b024a54d66f751f7bd778f5d283..9be42f3984405f9edd15836a7e8173aa1ec26a33 100644 (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++) {
                switch (symbol_conf.addr2line_style[i]) {
                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);
                        break;
                case A2L_STYLE_LLVM: