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 <[email protected]>
---
 .../arch/powerpc/util/skip-callchain-idx.c    |  52 ++-------
 tools/perf/util/dso.c                         |   2 +-
 tools/perf/util/dso.h                         |  23 ++--
 tools/perf/util/libdw.c                       | 101 ++++++++++--------
 tools/perf/util/libdw.h                       |  12 +--
 tools/perf/util/srcline.c                     |   2 +-
 6 files changed, 89 insertions(+), 103 deletions(-)

diff --git a/tools/perf/arch/powerpc/util/skip-callchain-idx.c 
b/tools/perf/arch/powerpc/util/skip-callchain-idx.c
index 356786432fd3..e57f10798fa6 100644
--- a/tools/perf/arch/powerpc/util/skip-callchain-idx.c
+++ b/tools/perf/arch/powerpc/util/skip-callchain-idx.c
@@ -30,14 +30,6 @@
  * 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);
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 143720d1ecb1..dce207c7f862 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -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));
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 4aee23775054..295388085031 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -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;
diff --git a/tools/perf/util/libdw.c b/tools/perf/util/libdw.c
index b96c4e0d728f..216977884103 100644
--- a/tools/perf/util/libdw.c
+++ b/tools/perf/util/libdw.c
@@ -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;
 
diff --git a/tools/perf/util/libdw.h b/tools/perf/util/libdw.h
index 0f8d7b4a11a5..b12094737415 100644
--- a/tools/perf/util/libdw.h
+++ b/tools/perf/util/libdw.h
@@ -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 */
diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index 28fa1abd1fd3..9be42f398440 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -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:
-- 
2.52.0.457.g6b5491de43-goog


Reply via email to