perf dso: Use libbfd to read build_id and .gnu_debuglink section
PATCH v2 1/3 perf dso: Use libbfd to read build_id and .gnu_debuglink section
Wine generates PE binaries for most of its modules and perf is unable to parse these to get build_id or .gnu_debuglink section.
Using libbfd when available, instead of libelf, makes it possible to resolve debug file location regardless of the dso binary format.
---
v2: Commit message reword.
tools/perf/util/symbol-elf.c | 80 ++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-)
tools/perf/util/symbol-elf.c +77 -3
@@ -50,6 +50,10 @@ typedef Elf64_Nhdr GElf_Nhdr;#define DMGL_ANSI (1 << 1)/* Include const, volatile, etc */ #endif +#ifdef HAVE_LIBBFD_SUPPORT +#define PACKAGE 'perf' +#include <bfd.h> +#else #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT extern char *cplus_demangle (const char *,int );
@@ -65,9 +69,7 @@ static inline char *bfd_demangle(void __maybe_unused *v, {return NULL ; } -#else -#define PACKAGE 'perf' -#include <bfd.h> +#endif #endif #endif
@@ -530,6 +532,36 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size)return err; } +#ifdef HAVE_LIBBFD_SUPPORT + +int filename__read_build_id (const char *filename,void *bf,size_t size) +{ +int err =-1 ; +bfd *abfd; + + abfd =bfd_openr (filename,NULL ); +if (!abfd) +return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, filename); + goto out_close; + } + +if (!abfd->build_id || abfd->build_id->size > size) + goto out_close; + +memcpy (bf, abfd->build_id->data, abfd->build_id->size); +memset (bf + abfd->build_id->size,0 , size - abfd->build_id->size); + err = abfd->build_id->size; + +out_close: +bfd_close (abfd); +return err; +} + +#else +int filename__read_build_id (const char *filename,void *bf,size_t size) {int fd, err =-1 ;
@@ -557,6 +589,8 @@ int filename__read_build_id(const char *filename, void *bf, size_t size) return err; } +#endif +int sysfs__read_build_id (const char *filename,void *build_id,size_t size) {int fd, err =-1 ;
@@ -608,6 +642,44 @@ int sysfs__read_build_id(const char *filename, void *build_id, size_t size)return err; } +#ifdef HAVE_LIBBFD_SUPPORT + +int filename__read_debuglink (const char *filename,char *debuglink, +size_t size) +{ +int err =-1 ; +asection *section; +bfd *abfd; + + abfd =bfd_openr (filename,NULL ); +if (!abfd) +return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, filename); + goto out_close; + } + + section =bfd_get_section_by_name (abfd,".gnu_debuglink" ); +if (!section) + goto out_close; + +if (section->size > size) + goto out_close; + +if (!bfd_get_section_contents (abfd, section, debuglink,0 , + section->size)) + goto out_close; + + err =0 ; + +out_close: +bfd_close (abfd); +return err; +} + +#else +int filename__read_debuglink (const char *filename,char *debuglink,size_t size) {
@@ -660,6 +732,8 @@ int filename__read_debuglink(const char *filename, char *debuglink, return err; } +#endif +static int dso__swap_init (struct dso *dso,unsigned char eidata) {static unsigned int const endian =1 ;
Signature · 1 line
2.27.0
PATCH v2 2/3 perf symbols: Try reading the symbol table with libbfd
Wine generates PE or PDB binaries for its debug files and perf is unable to parse these to get the list of symbols and their locations.
Trying to read symbols on non-ELF binaries with libbfd, when supported, makes it possible for perf to report symbols and annotations for Windows applications running under Wine.
Because libbfd doesn't provide symbol size (probably because of some backends not supporting it), we compute it by first sorting the symbols by addresses and then we consider that they are sequential in a given section.
---
v2: Updated commit message to match the code and a bit of rewording.
tools/perf/util/symbol.c | 129 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/symbol.h | 4 ++ 2 files changed, 133 insertions(+)
tools/perf/util/symbol.c +129 -0
@@ -1523,6 +1523,127 @@ static int dso__load_perf_map(const char *map_path, struct dso *dso)return -1 ; } +#ifdef HAVE_LIBBFD_SUPPORT +#define PACKAGE 'perf' +#include <bfd.h> + +static int bfd_symbols__cmpvalue (const void *a,const void *b) +{ +const asymbol *as = *(const asymbol **)a, *bs = *(const asymbol **)b; + +if (bfd_asymbol_value (as) !=bfd_asymbol_value (bs)) +return bfd_asymbol_value (as) -bfd_asymbol_value (bs); + +return bfd_asymbol_name (as)[0 ] -bfd_asymbol_name (bs)[0 ]; +} + +int dso__load_bfd_symbols (struct dso *dso,const char *debugfile) +{ +int err =-1 ; +long symbols_size, symbols_count; +asection *section; +asymbol **symbols, *sym; +struct symbol *symbol; +bfd *abfd; +u_int i; +u64 start, len; + + abfd =bfd_openr (dso->long_name,NULL ); +if (!abfd) +return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, + dso->long_name); + goto out_close; + } + +if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) + goto out_close; + + section =bfd_get_section_by_name (abfd,".text" ); +if (section) + dso->text_offset = section->vma - section->filepos; + +bfd_close (abfd); + + abfd =bfd_openr (debugfile,NULL ); +if (!abfd) +return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, + debugfile); + goto out_close; + } + +if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) + goto out_close; + + symbols_size =bfd_get_symtab_upper_bound (abfd); +if (symbols_size ==0 ) { +bfd_close (abfd); +return 0 ; + } + +if (symbols_size <0 ) + goto out_close; + + symbols =malloc (symbols_size); +if (!symbols) + goto out_close; + + symbols_count =bfd_canonicalize_symtab (abfd, symbols); +if (symbols_count <0 ) + goto out_free; + +qsort (symbols, symbols_count,sizeof (asymbol *), bfd_symbols__cmpvalue); + +#ifdef bfd_get_section +#define bfd_asymbol_section bfd_get_section +#endif +for (i =0 ; i < symbols_count; ++i) { + sym = symbols[i]; + section =bfd_asymbol_section (sym); +if (!(sym->flags & (BSF_GLOBAL ))) +continue ; + +while (i +1 < symbols_count && +bfd_asymbol_section (symbols[i +1 ]) == section && + !(symbols[i +1 ]->flags &BSF_GLOBAL )) + i++; + +if (i +1 < symbols_count && +bfd_asymbol_section (symbols[i +1 ]) == section) + len = symbols[i +1 ]->value - sym->value; +else + len = section->size - sym->value; + + start =bfd_asymbol_value (sym) - dso->text_offset; + symbol =symbol__new (start, len,STB_GLOBAL ,STT_FUNC , +bfd_asymbol_name (sym)); +if (!symbol) + goto out_free; + +symbols__insert (&dso->symbols, symbol); + } +#ifdef bfd_get_section +#undef bfd_asymbol_section +#endif + +symbols__fixup_end (&dso->symbols); +symbols__fixup_duplicate (&dso->symbols); + dso->adjust_symbols =1 ; + + err =0 ; +out_free: +free (symbols); +out_close: +bfd_close (abfd); +return err; +} +#endif +static bool dso__is_compatible_symtab_type (struct dso *dso,bool kmod,enum dso_binary_type type) {
@@ -1695,6 +1816,7 @@ int dso__load(struct dso *dso, struct map *map)bool next_slot = false;bool is_reg;bool nsexit; +int bfdrc =-1 ;int sirc =-1 ;enum dso_binary_type symtab_type = binary_type_symtab[i];
@@ -1713,12 +1835,19 @@ int dso__load(struct dso *dso, struct map *map)nsinfo__mountns_exit (&nsc); is_reg =is_regular_file (name); +#ifdef HAVE_LIBBFD_SUPPORT if (is_reg) + bfdrc =dso__load_bfd_symbols (dso, name); +#endif +if (is_reg && bfdrc <0 ) sirc =symsrc__init (ss, dso, name, symtab_type);if (nsexit)nsinfo__mountns_enter (dso->nsinfo, &nsc); +if (bfdrc ==0 ) +break ; +if (!is_reg || sirc <0 )continue ;
tools/perf/util/symbol.h +4 -0
@@ -175,6 +175,10 @@ int symbol__config_symfs(const struct option *opt __maybe_unused,struct symsrc ; +#ifdef HAVE_LIBBFD_SUPPORT +int dso__load_bfd_symbols (struct dso *dso,const char *debugfile); +#endif +int dso__load_sym (struct dso *dso,struct map *map,struct symsrc *syms_ss,struct symsrc *runtime_ss,int kmodule);int dso__synthesize_plt_symbols (struct dso *dso,struct symsrc *ss);
Signature · 1 line
2.27.0
Re: [PATCH v2 2/3] perf symbols: Try reading the symbol table with libbfd
On Tue, Aug 04, 2020 at 10:57:35AM +0200, Remi Bernon wrote:
SNIP
Quoted 14 lines
tools/perf/util/symbol.c +14
-0+ +int dso__load_bfd_symbols (struct dso *dso,const char *debugfile) +{ +int err =-1 ; +long symbols_size, symbols_count; +asection *section; +asymbol **symbols, *sym; +struct symbol *symbol; +bfd *abfd; +u_int i; +u64 start, len; + + abfd =bfd_openr (dso->long_name,NULL ); +if (!abfd)
tools/perf/util/symbol.c static int dso__load_perf_map(const char *map_path, struct dso *dso) · line 1562
Quoted 10 lines
tools/perf/util/symbol.c +10
-0+return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, + dso->long_name); + goto out_close; + } + +if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) + goto out_close;
aah, so the code is actualy only for non elf objects, somehow I thought it's replacing the symbol load globaly
jirka
Re: [PATCH v2 2/3] perf symbols: Try reading the symbol table with libbfd
Quoted 32 lines · Jiri Olsa
On Tue, Aug 04, 2020 at 10:57:35AM +0200, Remi Bernon wrote:
SNIP
Quoted 14 lines
tools/perf/util/symbol.c +14
-0+ +int dso__load_bfd_symbols (struct dso *dso,const char *debugfile) +{ +int err =-1 ; +long symbols_size, symbols_count; +asection *section; +asymbol **symbols, *sym; +struct symbol *symbol; +bfd *abfd; +u_int i; +u64 start, len; + + abfd =bfd_openr (dso->long_name,NULL ); +if (!abfd)tools/perf/util/symbol.c static int dso__load_perf_map(const char *map_path, struct dso *dso) · line 1562
Quoted 10 lines
tools/perf/util/symbol.c +10
-0+return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, + dso->long_name); + goto out_close; + } + +if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) + goto out_close;aah, so the code is actualy only for non elf objects, somehow I thought it's replacing the symbol load globaly
Quoted 2 lines
jirka
Maybe it could but because libbfd exposes the common subset of all its backends, it doesn't provide symbol sizes for instance (that's how I understand it). So I didn't want to lose features compared to what the libelf path is currently doing.
For non-elf objects, the implementation here works around that by assuming that all the symbols are following each other contiguously, but that's a little bit incorrect.
Signature · 1 line
Rémi Bernon <[email protected]>
PATCH v2 3/3 perf tests: Add test for PE binary format support
In reply to Remi BernonThis adds a precompiled file in PE binary format, with split debug file, and tries to read its build_id and .gnu_debuglink sections, as well as looking up the main symbol from the debug file. This should succeed if libbfd is supported.
---
v2: No changes.
tools/perf/Makefile.perf | 1 + tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/pe-file-parsing.c | 96 +++++++++++++++++++++++++++++ tools/perf/tests/pe-file.c | 14 +++++ tools/perf/tests/pe-file.exe | Bin 0 -> 75595 bytes tools/perf/tests/pe-file.exe.debug | Bin 0 -> 141644 bytes tools/perf/tests/tests.h | 1 + 8 files changed, 117 insertions(+) create mode 100644 tools/perf/tests/pe-file-parsing.c create mode 100644 tools/perf/tests/pe-file.c create mode 100644 tools/perf/tests/pe-file.exe create mode 100644 tools/perf/tests/pe-file.exe.debug
tools/perf/Makefile.perf +1 -0
@@ -951,6 +951,7 @@ install-tests: all install-gtk $(call QUIET_INSTALL, tests) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) tests/attr.py '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ + $(INSTALL) tests/pe-file.exe* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'; \ $(INSTALL) tests/attr/* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell'; \
tools/perf/tests/Build +1 -0
@@ -59,6 +59,7 @@ perf-y += genelf.o perf-y += api-io.o perf-y += demangle-java-test.o perf-y += pfm.o +perf-y += pe-file-parsing.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/builtin-test.c +4 -0
@@ -337,6 +337,10 @@ static struct test generic_tests[] = { .desc = "Demangle Java", .func = test__demangle_java, }, + { + .desc = "PE file support", + .func = test__pe_file_parsing, + }, { .func = NULL, },
tools/perf/tests/pe-file-parsing.c new file +96 -0
96 lines not on this page · raw message
tools/perf/tests/pe-file.c new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 + +// pe-file.exe and pe-file.exe.debug built with; +// x86_64-w64-mingw32-gcc -o pe-file.exe pe-file.c +// -Wl,--file-alignment,4096 -Wl,--build-id +// x86_64-w64-mingw32-objcopy --only-keep-debug +// --compress-debug-sections pe-file.exe pe-file.exe.debug +// x86_64-w64-mingw32-objcopy --strip-debug +// --add-gnu-debuglink=pe-file.exe.debug pe-file.exe + +int main (int argc,char const *argv[]) +{ +return 0 ; +}
tools/perf/tests/pe-file.exe new file binary +0 -0
tools/perf/tests/pe-file.exe.debug new file binary +0 -0
tools/perf/tests/tests.h +1 -0
@@ -121,6 +121,7 @@ int test__demangle_java(struct test *test, int subtest);int test__pfm (struct test *test,int subtest);const char *test__pfm_subtest_get_desc (int subtest);int test__pfm_subtest_get_nr (void ); +int test__pe_file_parsing (struct test *test,int subtest);bool test__bp_signal_is_supported (void );bool test__bp_account_is_supported (void );
Signature · 1 line
2.27.0
Re: [PATCH v2 3/3] perf tests: Add test for PE binary format support
Quoted 124 lines · Remi Bernon
This adds a precompiled file in PE binary format, with split debug file, and tries to read its
build_idand .gnu_debuglink sections, as well as looking up the main symbol from the debug file. This should succeed if libbfd is supported.Signed-off-by Remi Bernon <[email protected]>Cc Alexander Shishkin <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jiri Olsa <[email protected]>Cc Mark Rutland <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Jacek Caban <[email protected]>---
v2: No changes.
tools/perf/Makefile.perf | 1 + tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/pe-file-parsing.c | 96 +++++++++++++++++++++++++++++ tools/perf/tests/pe-file.c | 14 +++++ tools/perf/tests/pe-file.exe | Bin 0 -> 75595 bytes tools/perf/tests/pe-file.exe.debug | Bin 0 -> 141644 bytes tools/perf/tests/tests.h | 1 + 8 files changed, 117 insertions(+) create mode 100644 tools/perf/tests/pe-file-parsing.c create mode 100644 tools/perf/tests/pe-file.c create mode 100644 tools/perf/tests/pe-file.exe create mode 100644 tools/perf/tests/pe-file.exe.debugtools/perf/Makefile.perf +1
-0@@ -951,6 +951,7 @@ install-tests: all install-gtk $(call QUIET_INSTALL, tests) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) tests/attr.py '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ + $(INSTALL) tests/pe-file.exe* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'; \ $(INSTALL) tests/attr/* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell'; \tools/perf/tests/Build +1
-0@@ -59,6 +59,7 @@ perf-y += genelf.o perf-y += api-io.o perf-y += demangle-java-test.o perf-y += pfm.o +perf-y += pe-file-parsing.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)tools/perf/tests/builtin-test.c +4
-0@@ -337,6 +337,10 @@ static struct test generic_tests[] = { .desc = "Demangle Java", .func = test__demangle_java, }, + { + .desc = "PE file support", + .func = test__pe_file_parsing, + }, { .func = NULL, },tools/perf/tests/pe-file-parsing.c new file +47
-0@@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <stdbool.h> +#include <inttypes.h> +#include <stdlib.h> +#include <string.h> +#include <linux/bitops.h> +#include <linux/kernel.h> +#include <linux/types.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <subcmd/exec-cmd.h> + +#include "debug.h" +#include "util/build-id.h" +#include "util/symbol.h" +#include "util/dso.h" + +#include "tests.h" + +#ifdef HAVE_LIBBFD_SUPPORT + +static int run_dir (const char *d) +{ +char filename[PATH_MAX ]; +char debugfile[PATH_MAX ]; +char build_id[BUILD_ID_SIZE ]; +char debuglink[PATH_MAX ]; +char expect_build_id[] = { +0x5a ,0x0f ,0xd8 ,0x82 ,0xb5 ,0x30 ,0x84 ,0x22 , +0x4b ,0xa4 ,0x7b ,0x62 ,0x4c ,0x55 ,0xa4 ,0x69 , + }; +char expect_debuglink[PATH_MAX ] ="pe-file.exe.debug" ; +struct dso *dso; +struct symbol *sym; +int ret; + +scnprintf (filename,PATH_MAX ,"%s/pe-file.exe" , d); + ret =filename__read_build_id (filename, build_id,BUILD_ID_SIZE ); +TEST_ASSERT_VAL ("Failed to read build_id" , + ret ==sizeof (expect_build_id)); +TEST_ASSERT_VAL ("Wrong build_id" , !memcmp (build_id, expect_build_id, +sizeof (expect_build_id))); + + ret =filename__read_debuglink (filename, debuglink,PATH_MAX ); +TEST_ASSERT_VAL ("Failed to read debuglink" , ret ==0 ); + TEST_ASSERT_VAL("Wrong debuglink",
tools/perf/tests/pe-file-parsing.c line 57
Quoted 10 lines
tools/perf/tests/pe-file-parsing.c new file +10
-0+ !strcmp(debuglink, expect_debuglink)); + +scnprintf (debugfile,PATH_MAX ,"%s/%s" , d, debuglink); + ret =filename__read_build_id (debugfile, build_id,BUILD_ID_SIZE ); +TEST_ASSERT_VAL ("Failed to read debug file build_id" , + ret ==sizeof (expect_build_id)); +TEST_ASSERT_VAL ("Wrong build_id" , !memcmp (build_id, expect_build_id, +sizeof (expect_build_id))); + + dso =dso__new (filename);
missing dso != NULL check
other than this all 3 paches look ok to me
thanks,
jirka