Skip to contentSkip to conversation

perf: Refactor/add fallbacks for reading build-id and debuglink

PATCH In review Ian Rogers 2 participants 14 messages Dec 1, 2025 – Dec 7, 2025

Subsystems (3)
lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v2 cover perf: Refactor/add fallbacks for reading build-id and debuglink

This cleans up some tech debt mentioned in the thread:
https://lore.kernel.org/lkml/[email protected]/

The perf tool has fallbacks for objdump but not in the case of build-id and debuglink reading. Refactor the code so that in the case of read_build_id it now looks like:

```
...
int filename__read_build_id(const char *filename, struct build_id *bid)
{
	struct kmod_path m = { .name = NULL, };
	char path[PATH_MAX];
	int err, fd;

	if (!filename)
		return -EFAULT;

	if (!is_regular_file(filename))
		return -EWOULDBLOCK;

	err = kmod_path__parse(&m, filename);
	if (err)
		return -1;

	if (m.comp) {
		int error = 0;

		fd = filename__decompress(filename, path, sizeof(path), m.comp, &error);
		if (fd < 0) {
			pr_debug("Failed to decompress (error %d) %s\n",
				 error, filename);
			return -1;
		}
		lseek(fd, 0, SEEK_SET);
		filename = path;
	} else {
		fd = open(filename, O_RDONLY);
		if (fd < 0) {
			pr_debug("Failed to open %s\n", filename);
			return -1;
		}
	}

	err = libbfd__read_build_id(fd, filename, bid);
	if (err == 0)
		goto out;

	err = libelf__read_build_id(fd, filename, bid);
	if (err == 0)
		goto out;

	err = sym_min__read_build_id(fd, filename, bid);
out:
	close(fd);
	if (m.comp)
		unlink(filename);
	return err;
}
...
```

That is what was previously symbol-elf decompression code is now made common for all cases of reading. The handling of "is_regular_file" is now common. There is a clear fallback from libbfd to libelf to the symbol-minimal (sym_min) versions of the functions.

To support this there is refactoring of code into a perf-libelf.c file, to follow patterns established for the capstone, LLVM and libbfd code.

v2: Rebase, resolving merge conflict with James' removal of the block
argument:
https://lore.kernel.org/lkml/[email protected]/
Add a patch fixing an issue with return values from filename__read_build_id. Some tweaks to the PPC arch__sym_update patch commit message and make it 64-bit only. Change the sysfs__read_build_id code combining the libelf and minimal implementations but avoiding the memory read overhead, avoiding a potential buffer overrun if the descsz was particularly large in a build ID note, avoid reads and just using seeks.

v1: https://lore.kernel.org/lkml/[email protected]/

Ian Rogers (10):
perf symbol: Reduce scope of elf__needs_adjust_symbols perf symbol: Reduce scope of arch__sym_update perf symbol: Move libelf code to its own file/header perf symbol: Remove unused includes
perf symbol: Move dso__load_bfd_symbols out of symbol.h perf symbol: Use fallbacks with filename__read_build_id perf symbol: Use fallbacks for filename__read_debuglink perf symbol: Make a common sysfs__read_build_id perf dso: Move type helpers out of symbol and use fallbacks perf symbol: Fix ENOENT case for filename__read_build_id

 tools/perf/arch/powerpc/util/sym-handling.c   |   7 -
 tools/perf/builtin-annotate.c                 |  47 +--
 tools/perf/builtin-buildid-cache.c            |   6 +-
 tools/perf/builtin-c2c.c                      |  58 +--
 tools/perf/builtin-diff.c                     |  22 +-
 tools/perf/builtin-inject.c                   |  19 +-
 tools/perf/builtin-kmem.c                     |  41 ++-
 tools/perf/builtin-kwork.c                    |   9 +-
 tools/perf/builtin-mem.c                      |  16 +-
 tools/perf/builtin-report.c                   |  76 ++--
 tools/perf/builtin-sched.c                    |  50 ++-
 tools/perf/builtin-script.c                   |  89 ++---
 tools/perf/builtin-timechart.c                |  34 +-
 tools/perf/builtin-top.c                      |  85 +++--
 tools/perf/builtin-trace.c                    |  82 ++---
 .../scripts/python/Perf-Trace-Util/Context.c  |  11 +-
 tools/perf/tests/code-reading.c               |   1 +
 tools/perf/tests/hists_cumulate.c             |   1 +
 tools/perf/tests/hists_filter.c               |   1 +
 tools/perf/tests/hists_link.c                 |   1 +
 tools/perf/tests/hists_output.c               |   1 +
 tools/perf/tests/mmap-thread-lookup.c         |   1 +
 tools/perf/tests/pe-file-parsing.c            |   5 +-
 tools/perf/util/Build                         |   6 +-
 tools/perf/util/annotate-data.c               |   7 +-
 tools/perf/util/auxtrace.h                    |   1 +
 tools/perf/util/block-info.c                  |  11 +-
 tools/perf/util/build-id.c                    |  28 +-
 tools/perf/util/callchain.c                   |   1 +
 tools/perf/util/cs-etm.c                      |   1 +
 tools/perf/util/data-convert-json.c           |  30 +-
 tools/perf/util/db-export.c                   |  16 +-
 tools/perf/util/dlfilter.c                    |  11 +-
 tools/perf/util/dlfilter.h                    |   1 +
 tools/perf/util/dso.c                         |  57 +--
 tools/perf/util/event.c                       |  41 ++-
 tools/perf/util/evsel_fprintf.c               |  14 +-
 tools/perf/util/hist.c                        |  29 +-
 tools/perf/util/intel-pt.c                    |  35 +-
 tools/perf/util/kvm-stat.h                    |   5 +-
 tools/perf/util/libbfd.c                      |   9 +-
 tools/perf/util/libbfd.h                      |  12 +-
 tools/perf/util/machine.c                     |  34 +-
 tools/perf/util/perf-libelf.c                 | 235 ++++++++++++
 tools/perf/util/perf-libelf.h                 |  58 +++
 tools/perf/util/print_insn.c                  |  14 +-
 tools/perf/util/probe-event.c                 |   1 +
 tools/perf/util/probe-finder.c                |   1 +
 .../util/scripting-engines/trace-event-perl.c |   1 +
 .../scripting-engines/trace-event-python.c    |   1 +
 tools/perf/util/symbol-elf.c                  | 338 +-----------------
 tools/perf/util/symbol-minimal.c              | 166 +++++----
 tools/perf/util/symbol-minimal.h              |  13 +
 tools/perf/util/symbol.c                      | 107 +++++-
 tools/perf/util/symbol.h                      |  44 +--
 tools/perf/util/symbol_fprintf.c              |   1 +
 tools/perf/util/thread-stack.c                |  12 +-
 tools/perf/util/thread.c                      |  18 +-
 tools/perf/util/unwind-libdw.c                |  15 +-
 59 files changed, 1079 insertions(+), 958 deletions(-)
 create mode 100644 tools/perf/util/perf-libelf.c
 create mode 100644 tools/perf/util/perf-libelf.h
 create mode 100644 tools/perf/util/symbol-minimal.h
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#1 lore raw

PATCH v2 1/10 perf symbol: Reduce scope of elf__needs_adjust_symbols

Function is only used by symsrc__init in symbol-elf.c, make static to reduce scope. Switch to not passing the argument by value but as a pointer.

---
 tools/perf/util/symbol-elf.c | 8 ++++----
 tools/perf/util/symbol.h     | 1 -
 2 files changed, 4 insertions(+), 5 deletions(-)
tools/perf/util/symbol-elf.c +4 -4
index c5c382c3409a..f661ea6c8ac2 100644
@@ -1052,15 +1052,15 @@ void symsrc__destroy(struct symsrc *ss)
 	close(ss->fd);
 }
 
-bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
+static bool elf__needs_adjust_symbols(const GElf_Ehdr *ehdr)
 {
 	/*
 	 * Usually vmlinux is an ELF file with type ET_EXEC for most
 	 * architectures; except Arm64 kernel is linked with option
 	 * '-share', so need to check type ET_DYN.
 	 */
-	return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
-	       ehdr.e_type == ET_DYN;
+	return ehdr->e_type == ET_EXEC || ehdr->e_type == ET_REL ||
+	       ehdr->e_type == ET_DYN;
 }
 
 static Elf *read_gnu_debugdata(struct dso *dso, Elf *elf, const char *name, int *fd_ret)
@@ -1233,7 +1233,7 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 	if (dso__kernel(dso) == DSO_SPACE__USER)
 		ss->adjust_symbols = true;
 	else
-		ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
+		ss->adjust_symbols = elf__needs_adjust_symbols(&ehdr);
 
 	ss->name   = strdup(name);
 	if (!ss->name) {
tools/perf/util/symbol.h +0 -1
index 3fb5d146d9b1..9aa0016937b7 100644
@@ -217,7 +217,6 @@ int setup_intlist(struct intlist **list, const char *list_str,
 		  const char *list_name);
 
 #ifdef HAVE_LIBELF_SUPPORT
-bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
 void arch__sym_update(struct symbol *s, GElf_Sym *sym);
 #endif
 
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#2 lore raw

PATCH v2 2/10 perf symbol: Reduce scope of arch__sym_update

In reply to Ian Rogers

Rather than being a weak arch function, use the e_machine from the ELF header to determine whether to update or not. This should make the function work cross-platform, it also reduces the function's scope.

Note, the previous PPC code would compile both for 32 and 64-bit and when _CALL_ELF==2. Similar testing could be done using the ELF file's e_machine for PPC32 and e_flags for not EF_PPC_EMB. It can also be done on the symbol for a non-zero st_other. Given we're just copying the value, which would otherwise be 0, there's no need to introduce a st_other != 0 test. The value is only used in arch__fix_tev_from_maps on PowerPC in the PPC64_LOCAL_ENTRY_OFFSET macro, where 64-bit is assumed.

---
 tools/perf/arch/powerpc/util/sym-handling.c | 7 -------
 tools/perf/util/symbol-elf.c                | 9 ++++++---
 tools/perf/util/symbol.h                    | 4 ----
 3 files changed, 6 insertions(+), 14 deletions(-)
tools/perf/arch/powerpc/util/sym-handling.c +0 -7
index 947bfad7aa59..afefa9bd0c93 100644
@@ -66,13 +66,6 @@ const char *arch__normalize_symbol_name(const char *name)
 
 #if defined(_CALL_ELF) && _CALL_ELF == 2
 
-#ifdef HAVE_LIBELF_SUPPORT
-void arch__sym_update(struct symbol *s, GElf_Sym *sym)
-{
-	s->arch_sym = sym->st_other;
-}
-#endif
-
 #define PPC64LE_LEP_OFFSET	8
 
 void arch__fix_tev_from_maps(struct perf_probe_event *pev,
tools/perf/util/symbol-elf.c +6 -3
index f661ea6c8ac2..99b1e060acf5 100644
@@ -1338,8 +1338,11 @@ static u64 ref_reloc(struct kmap *kmap)
 	return 0;
 }
 
-void __weak arch__sym_update(struct symbol *s __maybe_unused,
-		GElf_Sym *sym __maybe_unused) { }
+static void arch__sym_update(int e_machine, const GElf_Sym *sym, struct symbol *s)
+{
+	if (e_machine == EM_PPC64)
+		s->arch_sym = sym->st_other;
+}
 
 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
 				      GElf_Sym *sym, GElf_Shdr *shdr,
@@ -1718,7 +1721,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		if (!f)
 			goto out_elf_end;
 
-		arch__sym_update(f, &sym);
+		arch__sym_update(ehdr.e_machine, &sym, f);
 
 		__symbols__insert(dso__symbols(curr_dso), f, dso__kernel(dso));
 		nr++;
tools/perf/util/symbol.h +0 -4
index 9aa0016937b7..f12bb3d0e08b 100644
@@ -216,10 +216,6 @@ int setup_list(struct strlist **list, const char *list_str,
 int setup_intlist(struct intlist **list, const char *list_str,
 		  const char *list_name);
 
-#ifdef HAVE_LIBELF_SUPPORT
-void arch__sym_update(struct symbol *s, GElf_Sym *sym);
-#endif
-
 const char *arch__normalize_symbol_name(const char *name);
 #define SYMBOL_A 0
 #define SYMBOL_B 1
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#3 lore raw

PATCH v2 3/10 perf symbol: Move libelf code to its own file/header

In reply to Ian Rogers

Move perf declarations depending on libelf out of symbol.h into perf-libelf.h. Move the corresponding C code into perf-libelf.c. This is motivated by trying to make it clearer the symbol.[ch] is generic code without library dependencies.

---
 tools/perf/util/Build          |  1 +
 tools/perf/util/perf-libelf.c  | 32 ++++++++++++++++++++++++++++++++
 tools/perf/util/perf-libelf.h  | 25 +++++++++++++++++++++++++
 tools/perf/util/probe-event.c  |  1 +
 tools/perf/util/probe-finder.c |  1 +
 tools/perf/util/symbol-elf.c   | 27 +--------------------------
 tools/perf/util/symbol.h       | 19 -------------------
 7 files changed, 61 insertions(+), 45 deletions(-)
 create mode 100644 tools/perf/util/perf-libelf.c
 create mode 100644 tools/perf/util/perf-libelf.h
tools/perf/util/Build +1 -0
index 1c2a43e1dc68..dcac6f1e1d99 100644
@@ -193,6 +193,7 @@ ifeq ($(CONFIG_LIBTRACEEVENT),y)
   perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_kwork_top.o
 endif
 
+perf-util-$(CONFIG_LIBELF) += perf-libelf.o
 perf-util-$(CONFIG_LIBELF) += symbol-elf.o
 perf-util-$(CONFIG_LIBELF) += probe-file.o
 perf-util-$(CONFIG_LIBELF) += probe-event.o
tools/perf/util/perf-libelf.c new file +32 -0
new file mode 100644
index 000000000000..abd55bd7f14b
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "perf-libelf.h"
+
+#include <stddef.h>
+#include <string.h>
+
+Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
+			     GElf_Shdr *shp, const char *name, size_t *idx)
+{
+	Elf_Scn *sec = NULL;
+	size_t cnt = 1;
+
+	/* ELF is corrupted/truncated, avoid calling elf_strptr. */
+	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
+		return NULL;
+
+	while ((sec = elf_nextscn(elf, sec)) != NULL) {
+		char *str;
+
+		gelf_getshdr(sec, shp);
+		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
+		if (str && !strcmp(name, str)) {
+			if (idx)
+				*idx = cnt;
+			return sec;
+		}
+		++cnt;
+	}
+
+	return NULL;
+}
+
tools/perf/util/perf-libelf.h new file +25 -0
new file mode 100644
index 000000000000..5d7d18daf5ff
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_LIBELF_H
+#define __PERF_LIBELF_H
+
+#ifdef HAVE_LIBELF_SUPPORT
+
+#include <libelf.h>
+#include <gelf.h>
+
+/*
+ * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
+ * for newer versions we can use mmap to reduce memory usage:
+ */
+#ifdef ELF_C_READ_MMAP
+# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
+#else
+# define PERF_ELF_C_READ_MMAP ELF_C_READ
+#endif
+
+Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name,
+			     size_t *idx);
+
+#endif // defined(HAVE_LIBELF_SUPPORT)
+
+#endif /* __PERF_LIBELF_H */
tools/perf/util/probe-event.c +1 -0
index 6ab2eb551b6c..c27ad9121230 100644
@@ -23,6 +23,7 @@
 #include "build-id.h"
 #include "event.h"
 #include "namespaces.h"
+#include "perf-libelf.h"
 #include "strlist.h"
 #include "strfilter.h"
 #include "debug.h"
tools/perf/util/probe-finder.c +1 -0
index 5ffd97ee4898..f94f742a3458 100644
@@ -25,6 +25,7 @@
 #include "debug.h"
 #include "debuginfo.h"
 #include "intlist.h"
+#include "perf-libelf.h"
 #include "strbuf.h"
 #include "strlist.h"
 #include "symbol.h"
tools/perf/util/symbol-elf.c +1 -26
index 99b1e060acf5..72632bf9cc30 100644
@@ -12,6 +12,7 @@
 #include "libbfd.h"
 #include "map.h"
 #include "maps.h"
+#include "perf-libelf.h"
 #include "symbol.h"
 #include "symsrc.h"
 #include "machine.h"
@@ -183,32 +184,6 @@ static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
 	return -1;
 }
 
-Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
-			     GElf_Shdr *shp, const char *name, size_t *idx)
-{
-	Elf_Scn *sec = NULL;
-	size_t cnt = 1;
-
-	/* ELF is corrupted/truncated, avoid calling elf_strptr. */
-	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
-		return NULL;
-
-	while ((sec = elf_nextscn(elf, sec)) != NULL) {
-		char *str;
-
-		gelf_getshdr(sec, shp);
-		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
-		if (str && !strcmp(name, str)) {
-			if (idx)
-				*idx = cnt;
-			return sec;
-		}
-		++cnt;
-	}
-
-	return NULL;
-}
-
 bool filename__has_section(const char *filename, const char *sec)
 {
 	int fd;
tools/perf/util/symbol.h +0 -19
index f12bb3d0e08b..393a528326d5 100644
@@ -14,10 +14,6 @@
 #include "symbol_conf.h"
 #include "spark.h"
 
-#ifdef HAVE_LIBELF_SUPPORT
-#include <libelf.h>
-#include <gelf.h>
-#endif
 #include <elf.h>
 
 struct dso;
@@ -26,21 +22,6 @@ struct maps;
 struct option;
 struct build_id;
 
-/*
- * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
- * for newer versions we can use mmap to reduce memory usage:
- */
-#ifdef ELF_C_READ_MMAP
-# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
-#else
-# define PERF_ELF_C_READ_MMAP ELF_C_READ
-#endif
-
-#ifdef HAVE_LIBELF_SUPPORT
-Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
-			     GElf_Shdr *shp, const char *name, size_t *idx);
-#endif
-
 /**
  * A symtab entry. When allocated this may be preceded by an annotation (see
  * symbol__annotation) and/or a browser_index (see symbol__browser_index).
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#4 lore raw

PATCH v2 4/10 perf symbol: Remove unused includes

In reply to Ian Rogers

Remove the includes of addr_location.h, spark.h, <elf.h>, <linux/refcount.h> and <stdint.h> as they're not used in the file. This caused an issue particularly with addr_location.h where there were lots of transitive dependencies. Resolve those by adding the include to the C file.

When adding the necessary includes, sort the includes and fix any other transitive dependency issues. Also, fix paths to files in util, erroneously in ../, etc.

---
 tools/perf/builtin-annotate.c                 | 47 +++++-----
 tools/perf/builtin-c2c.c                      | 58 ++++++------
 tools/perf/builtin-diff.c                     | 22 ++---
 tools/perf/builtin-inject.c                   | 19 ++--
 tools/perf/builtin-kmem.c                     | 41 +++++----
 tools/perf/builtin-kwork.c                    |  9 +-
 tools/perf/builtin-mem.c                      | 16 ++--
 tools/perf/builtin-report.c                   | 76 ++++++++--------
 tools/perf/builtin-sched.c                    | 50 +++++------
 tools/perf/builtin-script.c                   | 89 ++++++++++---------
 tools/perf/builtin-timechart.c                | 34 +++----
 tools/perf/builtin-top.c                      | 85 +++++++++---------
 tools/perf/builtin-trace.c                    | 82 ++++++++---------
 .../scripts/python/Perf-Trace-Util/Context.c  | 11 +--
 tools/perf/tests/code-reading.c               |  1 +
 tools/perf/tests/hists_cumulate.c             |  1 +
 tools/perf/tests/hists_filter.c               |  1 +
 tools/perf/tests/hists_link.c                 |  1 +
 tools/perf/tests/hists_output.c               |  1 +
 tools/perf/tests/mmap-thread-lookup.c         |  1 +
 tools/perf/util/annotate-data.c               |  7 +-
 tools/perf/util/auxtrace.h                    |  1 +
 tools/perf/util/block-info.c                  | 11 +--
 tools/perf/util/build-id.c                    | 28 +++---
 tools/perf/util/callchain.c                   |  1 +
 tools/perf/util/cs-etm.c                      |  1 +
 tools/perf/util/data-convert-json.c           | 30 ++++---
 tools/perf/util/db-export.c                   | 16 ++--
 tools/perf/util/dlfilter.c                    | 11 +--
 tools/perf/util/dlfilter.h                    |  1 +
 tools/perf/util/event.c                       | 41 +++++----
 tools/perf/util/evsel_fprintf.c               | 14 +--
 tools/perf/util/hist.c                        | 29 +++---
 tools/perf/util/intel-pt.c                    | 35 ++++----
 tools/perf/util/kvm-stat.h                    |  5 +-
 tools/perf/util/machine.c                     | 34 +++----
 tools/perf/util/print_insn.c                  | 14 +--
 .../util/scripting-engines/trace-event-perl.c |  1 +
 .../scripting-engines/trace-event-python.c    |  1 +
 tools/perf/util/symbol.h                      | 14 ++-
 tools/perf/util/symbol_fprintf.c              |  1 +
 tools/perf/util/thread-stack.c                | 12 +--
 tools/perf/util/thread.c                      | 18 ++--
 tools/perf/util/unwind-libdw.c                | 15 ++--
 44 files changed, 518 insertions(+), 468 deletions(-)
tools/perf/builtin-annotate.c +24 -23
index 9c27bb30b708..2967dd085b82 100644
@@ -6,45 +6,46 @@
  * look up and read DSOs and symbol information and display
  * a histogram of results, along various sorting keys.
  */
+
+#include <dlfcn.h>
+#include <errno.h>
+#include <inttypes.h>
+
 #include "builtin.h"
 #include "perf.h"
 
-#include "util/color.h"
-#include <linux/list.h>
+#include "arch/common.h"
+#include "ui/progress.h"
+#include "util/addr_location.h"
+#include "util/annotate-data.h"
+#include "util/annotate.h"
+#include "util/block-range.h"
+#include "util/branch.h"
 #include "util/cache.h"
-#include <linux/rbtree.h>
-#include <linux/zalloc.h>
-#include "util/symbol.h"
-
+#include "util/color.h"
+#include "util/data.h"
 #include "util/debug.h"
-
+#include "util/dso.h"
+#include "util/event.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
-#include "util/annotate.h"
-#include "util/annotate-data.h"
-#include "util/event.h"
-#include <subcmd/parse-options.h>
-#include "util/parse-events.h"
-#include "util/sort.h"
 #include "util/hist.h"
-#include "util/dso.h"
 #include "util/machine.h"
 #include "util/map.h"
+#include "util/map_symbol.h"
+#include "util/parse-events.h"
 #include "util/session.h"
+#include "util/sort.h"
+#include "util/symbol.h"
 #include "util/tool.h"
-#include "util/data.h"
-#include "arch/common.h"
-#include "util/block-range.h"
-#include "util/map_symbol.h"
-#include "util/branch.h"
 #include "util/util.h"
-#include "ui/progress.h"
 
-#include <dlfcn.h>
-#include <errno.h>
 #include <linux/bitmap.h>
 #include <linux/err.h>
-#include <inttypes.h>
+#include <linux/list.h>
+#include <linux/rbtree.h>
+#include <linux/zalloc.h>
+#include <subcmd/parse-options.h>
 
 struct perf_annotate {
 	struct perf_tool tool;
tools/perf/builtin-c2c.c +31 -27

72 lines not on this page · raw message

tools/perf/builtin-diff.c +12 -10
index 53d5ea4a6a4f..9fc8a18647d1 100644
@@ -8,25 +8,27 @@
 #include "builtin.h"
 #include "perf.h"
 
+#include "util/addr_location.h"
+#include "util/annotate.h"
+#include "util/block-info.h"
+#include "util/config.h"
+#include "util/data.h"
 #include "util/debug.h"
 #include "util/event.h"
-#include "util/hist.h"
-#include "util/evsel.h"
 #include "util/evlist.h"
+#include "util/evsel.h"
+#include "util/hist.h"
+#include "util/map.h"
 #include "util/session.h"
-#include "util/tool.h"
 #include "util/sort.h"
+#include "util/spark.h"
 #include "util/srcline.h"
+#include "util/stream.h"
 #include "util/symbol.h"
-#include "util/data.h"
-#include "util/config.h"
 #include "util/time-utils.h"
-#include "util/annotate.h"
-#include "util/map.h"
-#include "util/spark.h"
-#include "util/block-info.h"
-#include "util/stream.h"
+#include "util/tool.h"
 #include "util/util.h"
+
 #include <linux/err.h>
 #include <linux/zalloc.h>
 #include <subcmd/pager.h>
tools/perf/builtin-inject.c +10 -9
index 0e40f2a4bd18..311225944b78 100644
@@ -8,26 +8,27 @@
  */
 #include "builtin.h"
 
+#include "util/addr_location.h"
+#include "util/auxtrace.h"
+#include "util/build-id.h"
 #include "util/color.h"
+#include "util/data.h"
+#include "util/debug.h"
 #include "util/dso.h"
-#include "util/vdso.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
+#include "util/jit.h"
 #include "util/map.h"
+#include "util/namespaces.h"
 #include "util/session.h"
-#include "util/tool.h"
-#include "util/debug.h"
-#include "util/build-id.h"
-#include "util/data.h"
-#include "util/auxtrace.h"
-#include "util/jit.h"
 #include "util/string2.h"
 #include "util/symbol.h"
 #include "util/synthetic-events.h"
 #include "util/thread.h"
-#include "util/namespaces.h"
-#include "util/util.h"
+#include "util/tool.h"
 #include "util/tsc.h"
+#include "util/util.h"
+#include "util/vdso.h"
 
 #include <internal/lib.h>
 
tools/perf/builtin-kmem.c +20 -21

62 lines not on this page · raw message

tools/perf/builtin-kwork.c +5 -4
index 7f3068264568..4a274c550cdf 100644
@@ -8,18 +8,19 @@
 #include "builtin.h"
 #include "perf.h"
 
+#include "util/addr_location.h"
+#include "util/callchain.h"
 #include "util/data.h"
+#include "util/debug.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
+#include "util/evsel_fprintf.h"
 #include "util/header.h"
 #include "util/kwork.h"
-#include "util/debug.h"
 #include "util/session.h"
+#include "util/string2.h"
 #include "util/symbol.h"
 #include "util/thread.h"
-#include "util/string2.h"
-#include "util/callchain.h"
-#include "util/evsel_fprintf.h"
 #include "util/util.h"
 
 #include <subcmd/pager.h>
tools/perf/builtin-mem.c +9 -7
index d43500b92a7b..bad3784bd119 100644
@@ -7,24 +7,26 @@
 #include "builtin.h"
 #include "perf.h"
 
-#include <subcmd/parse-options.h>
+#include "util/addr_location.h"
 #include "util/auxtrace.h"
-#include "util/trace-event.h"
-#include "util/tool.h"
-#include "util/session.h"
 #include "util/data.h"
-#include "util/map_symbol.h"
-#include "util/mem-events.h"
 #include "util/debug.h"
 #include "util/dso.h"
 #include "util/map.h"
-#include "util/symbol.h"
+#include "util/map_symbol.h"
+#include "util/mem-events.h"
 #include "util/pmus.h"
 #include "util/sample.h"
+#include "util/session.h"
 #include "util/sort.h"
 #include "util/string2.h"
+#include "util/symbol.h"
+#include "util/tool.h"
+#include "util/trace-event.h"
 #include "util/util.h"
+
 #include <linux/err.h>
+#include <subcmd/parse-options.h>
 
 #define MEM_OPERATION_LOAD	0x1
 #define MEM_OPERATION_STORE	0x2
tools/perf/builtin-report.c +37 -39

105 lines not on this page · raw message

tools/perf/builtin-sched.c +24 -26

75 lines not on this page · raw message

tools/perf/builtin-script.c +46 -43

119 lines not on this page · raw message

tools/perf/builtin-timechart.c +18 -16
index 22050c640dfa..3f464df723d4 100644
@@ -12,33 +12,35 @@
 #include <inttypes.h>
 
 #include "builtin.h"
+
+#include "util/addr_location.h"
+#include "util/callchain.h"
 #include "util/color.h"
-#include <linux/list.h>
+#include "util/data.h"
+#include "util/debug.h"
+#include "util/event.h"
 #include "util/evlist.h" // for struct evsel_str_handler
 #include "util/evsel.h"
-#include <linux/kernel.h>
-#include <linux/rbtree.h>
-#include <linux/time64.h>
-#include <linux/zalloc.h>
-#include "util/symbol.h"
-#include "util/thread.h"
-#include "util/callchain.h"
-
 #include "util/header.h"
-#include <subcmd/pager.h>
-#include <subcmd/parse-options.h>
 #include "util/parse-events.h"
-#include "util/event.h"
 #include "util/session.h"
+#include "util/string2.h"
 #include "util/svghelper.h"
+#include "util/symbol.h"
+#include "util/thread.h"
 #include "util/tool.h"
-#include "util/data.h"
-#include "util/debug.h"
-#include "util/string2.h"
 #include "util/tracepoint.h"
 #include "util/util.h"
-#include <linux/err.h>
+
 #include <event-parse.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/rbtree.h>
+#include <linux/time64.h>
+#include <linux/zalloc.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
 
 #ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE
 FILE *open_memstream(char **ptr, size_t *sizeloc);
tools/perf/builtin-top.c +40 -45

117 lines not on this page · raw message

tools/perf/builtin-trace.c +42 -40

119 lines not on this page · raw message

tools/perf/scripts/python/Perf-Trace-Util/Context.c +6 -5
index 60dcfe56d4d9..aeafcc71cb08 100644
@@ -12,17 +12,18 @@
 #define PY_SSIZE_T_CLEAN
 
 #include <Python.h>
+#include "../../../util/addr_location.h"
+#include "../../../util/auxtrace.h"
 #include "../../../util/config.h"
-#include "../../../util/trace-event.h"
 #include "../../../util/event.h"
-#include "../../../util/symbol.h"
-#include "../../../util/thread.h"
 #include "../../../util/map.h"
 #include "../../../util/maps.h"
-#include "../../../util/auxtrace.h"
 #include "../../../util/session.h"
-#include "../../../util/srcline.h"
 #include "../../../util/srccode.h"
+#include "../../../util/srcline.h"
+#include "../../../util/symbol.h"
+#include "../../../util/thread.h"
+#include "../../../util/trace-event.h"
 
 #define _PyCapsule_GetPointer(arg1, arg2) \
   PyCapsule_GetPointer((arg1), (arg2))
tools/perf/tests/code-reading.c +1 -0
index 5927d1ea20e2..cff0ecb0a805 100644
@@ -15,6 +15,7 @@
 #include <perf/evlist.h>
 #include <perf/mmap.h>
 
+#include "addr_location.h"
 #include "debug.h"
 #include "dso.h"
 #include "env.h"
tools/perf/tests/hists_cumulate.c +1 -0
index 3eb9ef8d7ec6..c0931f639fb0 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "util/addr_location.h"
 #include "util/debug.h"
 #include "util/dso.h"
 #include "util/event.h"
tools/perf/tests/hists_filter.c +1 -0
index 1cebd20cc91c..24f98c874ba6 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "util/addr_location.h"
 #include "util/debug.h"
 #include "util/map.h"
 #include "util/symbol.h"
tools/perf/tests/hists_link.c +1 -0
index 996f5f0b3bd1..04197fcdfa9b 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "tests.h"
+#include "addr_location.h"
 #include "debug.h"
 #include "symbol.h"
 #include "sort.h"
tools/perf/tests/hists_output.c +1 -0
index ee5ec8bda60e..5e121f2181de 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "util/addr_location.h"
 #include "util/debug.h"
 #include "util/dso.h"
 #include "util/event.h"
tools/perf/tests/mmap-thread-lookup.c +1 -0
index 0c5619c6e6e9..f39dc0b28810 100644
@@ -7,6 +7,7 @@
 #include <pthread.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include "addr_location.h"
 #include "debug.h"
 #include "env.h"
 #include "event.h"
tools/perf/util/annotate-data.c +4 -3
index 07cf9c334be0..85ab4330da0b 100644
@@ -10,14 +10,15 @@
 #include <inttypes.h>
 #include <linux/zalloc.h>
 
-#include "annotate.h"
+#include "addr_location.h"
 #include "annotate-data.h"
-#include "debuginfo.h"
+#include "annotate.h"
 #include "debug.h"
+#include "debuginfo.h"
 #include "dso.h"
 #include "dwarf-regs.h"
-#include "evsel.h"
 #include "evlist.h"
+#include "evsel.h"
 #include "map.h"
 #include "map_symbol.h"
 #include "sort.h"
tools/perf/util/auxtrace.h +1 -0
index eee2c11f7666..a07e9b6ab24d 100644
@@ -8,6 +8,7 @@
 #define __PERF_AUXTRACE_H
 
 #include <stdio.h> // FILE
+#include <unistd.h> // pid_t
 #include <linux/perf_event.h>
 #include <linux/types.h>
 #include <asm/barrier.h>
tools/perf/util/block-info.c +6 -5
index 649392bee7ed..15e42e9fb2e8 100644
@@ -2,15 +2,16 @@
 #include <stdlib.h>
 #include <string.h>
 #include <linux/zalloc.h>
-#include "block-info.h"
-#include "sort.h"
+#include "addr_location.h"
 #include "annotate.h"
-#include "symbol.h"
+#include "block-info.h"
 #include "dso.h"
-#include "map.h"
-#include "srcline.h"
 #include "evlist.h"
 #include "hist.h"
+#include "map.h"
+#include "sort.h"
+#include "srcline.h"
+#include "symbol.h"
 #include "ui/browsers/hists.h"
 
 static struct block_header_column {
tools/perf/util/build-id.c +15 -13
index fdb35133fde4..9e1f8d5f6d51 100644
@@ -7,38 +7,40 @@
  * Copyright (C) 2009, 2010 Red Hat Inc.
  * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <[email protected]>
  */
-#include "util.h" // lsdir(), mkdir_p(), rm_rf()
 #include <dirent.h>
 #include <errno.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include "util/copyfile.h"
-#include "dso.h"
+
+#include "addr_location.h"
 #include "build-id.h"
+#include "copyfile.h"
+#include "debug.h"
+#include "dso.h"
 #include "event.h"
-#include "namespaces.h"
+#include "header.h"
 #include "map.h"
+#include "namespaces.h"
+#include "path.h"
+#include "probe-file.h"
+#include "session.h"
+#include "strlist.h"
 #include "symbol.h"
 #include "thread.h"
-#include <linux/kernel.h>
-#include "debug.h"
-#include "session.h"
 #include "tool.h"
-#include "header.h"
+#include "util.h" // lsdir(), mkdir_p(), rm_rf()
 #include "vdso.h"
-#include "path.h"
-#include "probe-file.h"
-#include "strlist.h"
 
 #ifdef HAVE_DEBUGINFOD_SUPPORT
 #include <elfutils/debuginfod.h>
 #endif
 
+#include <asm/bug.h>
 #include <linux/ctype.h>
-#include <linux/zalloc.h>
+#include <linux/kernel.h>
 #include <linux/string.h>
-#include <asm/bug.h>
+#include <linux/zalloc.h>
 
 static bool no_buildid_cache;
 
tools/perf/util/callchain.c +1 -0
index d7b7eef740b9..9c295b38d1e1 100644
@@ -21,6 +21,7 @@
 
 #include "asm/bug.h"
 
+#include "addr_location.h"
 #include "debug.h"
 #include "dso.h"
 #include "event.h"
tools/perf/util/cs-etm.c +1 -0
index 25d56e0f1c07..cb499a81625a 100644
@@ -17,6 +17,7 @@
 
 #include <stdlib.h>
 
+#include "addr_location.h"
 #include "auxtrace.h"
 #include "color.h"
 #include "cs-etm.h"
tools/perf/util/data-convert-json.c +16 -14
index 9dc1e184cf3c..aec1302e599b 100644
@@ -12,20 +12,22 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "linux/compiler.h"
-#include "linux/err.h"
-#include "util/auxtrace.h"
-#include "util/debug.h"
-#include "util/dso.h"
-#include "util/event.h"
-#include "util/evsel.h"
-#include "util/evlist.h"
-#include "util/header.h"
-#include "util/map.h"
-#include "util/session.h"
-#include "util/symbol.h"
-#include "util/thread.h"
-#include "util/tool.h"
+#include "addr_location.h"
+#include "auxtrace.h"
+#include "debug.h"
+#include "dso.h"
+#include "event.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "header.h"
+#include "map.h"
+#include "session.h"
+#include "symbol.h"
+#include "thread.h"
+#include "tool.h"
+
+#include <linux/compiler.h>
+#include <linux/err.h>
 
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
tools/perf/util/db-export.c +9 -7
index 8f52e8cefcf3..5c23b1b2eafb 100644
@@ -7,18 +7,20 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include "addr_location.h"
+#include "call-path.h"
+#include "callchain.h"
+#include "comm.h"
+#include "db-export.h"
 #include "dso.h"
+#include "event.h"
 #include "evsel.h"
 #include "machine.h"
-#include "thread.h"
-#include "comm.h"
-#include "symbol.h"
 #include "map.h"
-#include "event.h"
+#include "symbol.h"
 #include "thread-stack.h"
-#include "callchain.h"
-#include "call-path.h"
-#include "db-export.h"
+#include "thread.h"
+
 #include <linux/zalloc.h>
 
 int db_export__init(struct db_export *dbe)
tools/perf/util/dlfilter.c +6 -5
index c0afcbd954f8..70e322de711f 100644
@@ -13,17 +13,18 @@
 #include <linux/kernel.h>
 #include <linux/string.h>
 
+#include "../include/perf/perf_dlfilter.h"
+#include "addr_location.h"
 #include "debug.h"
+#include "dlfilter.h"
+#include "dso.h"
 #include "event.h"
 #include "evsel.h"
-#include "dso.h"
 #include "map.h"
+#include "srcline.h"
+#include "symbol.h"
 #include "thread.h"
 #include "trace-event.h"
-#include "symbol.h"
-#include "srcline.h"
-#include "dlfilter.h"
-#include "../include/perf/perf_dlfilter.h"
 
 static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
 {
tools/perf/util/dlfilter.h +1 -0
index cc4bb9657d05..522d8e707db0 100644
@@ -13,6 +13,7 @@ struct perf_sample;
 struct evsel;
 struct machine;
 struct addr_location;
+struct option;
 struct perf_dlfilter_fns;
 struct perf_dlfilter_sample;
 struct perf_dlfilter_al;
tools/perf/util/event.c +22 -19

64 lines not on this page · raw message

tools/perf/util/evsel_fprintf.c +8 -6
index 103984b29b1e..abdd9985c4e3 100644
@@ -2,16 +2,18 @@
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdbool.h>
-#include "util/evlist.h"
-#include "evsel.h"
-#include "util/evsel_fprintf.h"
-#include "util/event.h"
+
+#include "addr_location.h"
 #include "callchain.h"
+#include "dso.h"
+#include "event.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "evsel_fprintf.h"
 #include "map.h"
+#include "srcline.h"
 #include "strlist.h"
 #include "symbol.h"
-#include "srcline.h"
-#include "dso.h"
 
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
tools/perf/util/hist.c +16 -13
index 64ff427040c3..bd71e48c1183 100644
@@ -1,32 +1,35 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <math.h>
+#include <inttypes.h>
+#include <sys/param.h>
+
+#include "addr_location.h"
+#include "annotate.h"
+#include "block-info.h"
+#include "branch.h"
+#include "build-id.h"
 #include "callchain.h"
+#include "cgroup.h"
 #include "debug.h"
 #include "dso.h"
-#include "build-id.h"
+#include "evlist.h"
+#include "evsel.h"
 #include "hist.h"
 #include "kvm-stat.h"
 #include "map.h"
 #include "map_symbol.h"
-#include "branch.h"
 #include "mem-events.h"
 #include "mem-info.h"
-#include "session.h"
 #include "namespaces.h"
-#include "cgroup.h"
+#include "session.h"
 #include "sort.h"
-#include "units.h"
-#include "evlist.h"
-#include "evsel.h"
-#include "annotate.h"
 #include "srcline.h"
 #include "symbol.h"
 #include "thread.h"
-#include "block-info.h"
 #include "ui/progress.h"
-#include <errno.h>
-#include <math.h>
-#include <inttypes.h>
-#include <sys/param.h>
+#include "units.h"
+
 #include <linux/rbtree.h>
 #include <linux/string.h>
 #include <linux/time64.h>
tools/perf/util/intel-pt.c +18 -17
index fc9eec8b54b8..244749c89770 100644
@@ -14,29 +14,30 @@
 #include <linux/types.h>
 #include <linux/zalloc.h>
 
-#include "session.h"
-#include "machine.h"
-#include "memswap.h"
-#include "sort.h"
-#include "tool.h"
+#include "addr_location.h"
+#include "auxtrace.h"
+#include "callchain.h"
+#include "color.h"
+#include "config.h"
+#include "debug.h"
+#include "dso.h"
 #include "event.h"
 #include "evlist.h"
 #include "evsel.h"
+#include "intel-pt.h"
+#include "machine.h"
 #include "map.h"
-#include "color.h"
-#include "thread.h"
-#include "thread-stack.h"
+#include "memswap.h"
+#include "perf_api_probe.h"
+#include "session.h"
+#include "sort.h"
 #include "symbol.h"
-#include "callchain.h"
-#include "dso.h"
-#include "debug.h"
-#include "auxtrace.h"
-#include "tsc.h"
-#include "intel-pt.h"
-#include "config.h"
-#include "util/perf_api_probe.h"
-#include "util/synthetic-events.h"
+#include "synthetic-events.h"
+#include "thread-stack.h"
+#include "thread.h"
 #include "time-utils.h"
+#include "tool.h"
+#include "tsc.h"
 
 #include "../arch/x86/include/uapi/asm/perf_regs.h"
 
tools/perf/util/kvm-stat.h +3 -2
index a356b839c2ee..7b63b1ba1b8a 100644
@@ -4,11 +4,12 @@
 
 #ifdef HAVE_KVM_STAT_SUPPORT
 
-#include "tool.h"
+#include "addr_location.h"
+#include "record.h"
 #include "sort.h"
 #include "stat.h"
 #include "symbol.h"
-#include "record.h"
+#include "tool.h"
 
 #include <errno.h>
 #include <stdlib.h>
tools/perf/util/machine.c +18 -16
index b5dd42588c91..db8052443259 100644
@@ -3,8 +3,19 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <regex.h>
+#include <stdbool.h>
 #include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "addr_location.h"
+#include "arm64-frame-pointer-unwind-support.h"
+#include "asm/bug.h"
+#include "bpf-event.h"
+#include "branch.h"
 #include "callchain.h"
+#include "cgroup.h"
 #include "debug.h"
 #include "dso.h"
 #include "env.h"
@@ -14,37 +25,28 @@
 #include "machine.h"
 #include "map.h"
 #include "map_symbol.h"
-#include "branch.h"
 #include "mem-events.h"
 #include "mem-info.h"
 #include "path.h"
+#include "sort.h"
 #include "srcline.h"
+#include "strlist.h"
 #include "symbol.h"
 #include "synthetic-events.h"
-#include "sort.h"
-#include "strlist.h"
 #include "target.h"
 #include "thread.h"
+#include "unwind.h"
 #include "util.h"
 #include "vdso.h"
-#include <stdbool.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include "unwind.h"
-#include "linux/hash.h"
-#include "asm/bug.h"
-#include "bpf-event.h"
-#include <internal/lib.h> // page_size
-#include "cgroup.h"
-#include "arm64-frame-pointer-unwind-support.h"
-#include <api/io_dir.h>
 
+#include <api/io_dir.h>
+#include <internal/lib.h> // page_size
 #include <linux/ctype.h>
-#include <symbol/kallsyms.h>
+#include <linux/hash.h>
 #include <linux/mman.h>
 #include <linux/string.h>
 #include <linux/zalloc.h>
+#include <symbol/kallsyms.h>
 
 static struct dso *machine__kernel_dso(struct machine *machine)
 {
tools/perf/util/print_insn.c +8 -6
index 02e6fbb8ca04..07f9c9f6e244 100644
@@ -5,18 +5,20 @@
  * Author(s): Changbin Du <[email protected]>
  */
 #include <inttypes.h>
-#include <string.h>
 #include <stdbool.h>
+#include <string.h>
+
+#include "addr_location.h"
 #include "capstone.h"
 #include "debug.h"
+#include "dso.h"
+#include "dump-insn.h"
+#include "machine.h"
+#include "map.h"
+#include "print_insn.h"
 #include "sample.h"
 #include "symbol.h"
-#include "machine.h"
 #include "thread.h"
-#include "print_insn.h"
-#include "dump-insn.h"
-#include "map.h"
-#include "dso.h"
 
 size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp)
 {
tools/perf/util/scripting-engines/trace-event-perl.c +1 -0
index e261a57b87d4..9116ffa252b4 100644
@@ -35,6 +35,7 @@
 #include <EXTERN.h>
 #include <perl.h>
 
+#include "../addr_location.h"
 #include "../callchain.h"
 #include "../dso.h"
 #include "../machine.h"
tools/perf/util/scripting-engines/trace-event-python.c +1 -0
index 6655c0bbe0d8..aacb4dc3cb90 100644
@@ -34,6 +34,7 @@
 #include <event-parse.h>
 #endif
 
+#include "../addr_location.h"
 #include "../build-id.h"
 #include "../counts.h"
 #include "../debug.h"
tools/perf/util/symbol.h +6 -8
index 393a528326d5..2b2bbb5914b9 100644
@@ -2,20 +2,18 @@
 #ifndef __PERF_SYMBOL
 #define __PERF_SYMBOL 1
 
-#include <linux/types.h>
-#include <linux/refcount.h>
 #include <stdbool.h>
-#include <stdint.h>
+#include <stdio.h>
+#include <elf.h>
+
+#include <linux/types.h>
 #include <linux/list.h>
 #include <linux/rbtree.h>
-#include <stdio.h>
-#include "addr_location.h"
+
 #include "path.h"
 #include "symbol_conf.h"
-#include "spark.h"
-
-#include <elf.h>
 
+struct addr_location;
 struct dso;
 struct map;
 struct maps;
tools/perf/util/symbol_fprintf.c +1 -0
index 53e1af4ed9ac..59aa97c0a997 100644
@@ -3,6 +3,7 @@
 #include <inttypes.h>
 #include <stdio.h>
 
+#include "addr_location.h"
 #include "dso.h"
 #include "map.h"
 #include "symbol.h"
tools/perf/util/thread-stack.c +7 -5
index c6a0a27b12c2..0d0cbc1250e9 100644
@@ -11,15 +11,17 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include "thread.h"
+
+#include "addr_location.h"
+#include "call-path.h"
+#include "comm.h"
+#include "debug.h"
+#include "env.h"
 #include "event.h"
 #include "machine.h"
-#include "env.h"
-#include "debug.h"
 #include "symbol.h"
-#include "comm.h"
-#include "call-path.h"
 #include "thread-stack.h"
+#include "thread.h"
 
 #define STACK_GROWTH 2048
 
tools/perf/util/thread.c +10 -8
index aa9c58bbf9d3..bf051074d66c 100644
@@ -7,18 +7,20 @@
 #include <string.h>
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
-#include "dso.h"
-#include "session.h"
-#include "thread.h"
-#include "thread-stack.h"
-#include "debug.h"
-#include "namespaces.h"
+
+#include "addr_location.h"
+#include "callchain.h"
 #include "comm.h"
+#include "debug.h"
+#include "dso.h"
+#include "dwarf-regs.h"
 #include "map.h"
+#include "namespaces.h"
+#include "session.h"
 #include "symbol.h"
+#include "thread-stack.h"
+#include "thread.h"
 #include "unwind.h"
-#include "callchain.h"
-#include "dwarf-regs.h"
 
 #include <api/fs/fs.h>
 
tools/perf/util/unwind-libdw.c +9 -6
index ae70fb56a057..552cffb4bb92 100644
@@ -4,20 +4,23 @@
 #include <elfutils/libdwfl.h>
 #include <inttypes.h>
 #include <errno.h>
+
+#include "addr_location.h"
+#include "callchain.h"
 #include "debug.h"
 #include "dso.h"
-#include "unwind.h"
-#include "unwind-libdw.h"
+#include "env.h"
+#include "event.h"
 #include "machine.h"
 #include "map.h"
+#include "perf_regs.h"
 #include "symbol.h"
 #include "thread.h"
+#include "unwind-libdw.h"
+#include "unwind.h"
+
 #include <linux/types.h>
 #include <linux/zalloc.h>
-#include "event.h"
-#include "perf_regs.h"
-#include "callchain.h"
-#include "util/env.h"
 
 static char *debuginfo_path;
 
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#5 lore raw

PATCH v2 5/10 perf symbol: Move dso__load_bfd_symbols out of symbol.h

In reply to Ian Rogers

Move into libbfd.h where the code is and to make it clear that symbol.h and symbol.c are generic code. Add libbfd.h to symbol.c includes and organize the includes.

---
 tools/perf/tests/pe-file-parsing.c |  5 ++--
 tools/perf/util/libbfd.h           |  7 ++++++
 tools/perf/util/symbol.c           | 38 +++++++++++++++++-------------
 tools/perf/util/symbol.h           |  4 ----
 4 files changed, 31 insertions(+), 23 deletions(-)
tools/perf/tests/pe-file-parsing.c +3 -2
index 30c7da79e109..e97c7662664e 100644
@@ -11,10 +11,11 @@
 #include <unistd.h>
 #include <subcmd/exec-cmd.h>
 
-#include "debug.h"
 #include "util/build-id.h"
-#include "util/symbol.h"
+#include "util/debug.h"
 #include "util/dso.h"
+#include "util/libbfd.h"
+#include "util/symbol.h"
 
 #include "tests.h"
 
tools/perf/util/libbfd.h +7 -0
index 953886f3d62f..086c5f12cc34 100644
@@ -21,6 +21,7 @@ int libbfd__addr2line(const char *dso_name, u64 addr,
 
 
 void dso__free_a2l_libbfd(struct dso *dso);
+int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
 
 int symbol__disassemble_libbfd(const char *filename, struct symbol *sym,
 			     struct annotate_args *args);
@@ -51,6 +52,12 @@ static inline void dso__free_a2l_libbfd(struct dso *dso __always_unused)
 {
 }
 
+static inline int dso__load_bfd_symbols(struct dso *dso __always_unused,
+					const char *debugfile __always_unused)
+{
+	return -1;
+}
+
 static inline int symbol__disassemble_libbfd(const char *filename __always_unused,
 					struct symbol *sym __always_unused,
 					struct annotate_args *args __always_unused)
tools/perf/util/symbol.c +21 -17
index d8fc5ea77f84..f8c6a37464ca 100644
@@ -1,47 +1,51 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <dirent.h>
 #include <errno.h>
-#include <stdlib.h>
+#include <fcntl.h>
+#include <inttypes.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <linux/capability.h>
-#include <linux/kernel.h>
-#include <linux/mman.h>
-#include <linux/string.h>
-#include <linux/time64.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/param.h>
-#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
-#include <inttypes.h>
+
 #include "annotate.h"
 #include "build-id.h"
 #include "cap.h"
 #include "cpumap.h"
 #include "debug.h"
+#include "debug.h"
 #include "demangle-cxx.h"
 #include "demangle-java.h"
 #include "demangle-ocaml.h"
 #include "demangle-rust-v0.h"
 #include "dso.h"
-#include "util.h" // lsdir()
-#include "debug.h"
 #include "event.h"
+#include "header.h"
+#include "intlist.h"
+#include "libbfd.h"
 #include "machine.h"
 #include "map.h"
-#include "symbol.h"
 #include "map_symbol.h"
 #include "mem-events.h"
 #include "mem-info.h"
-#include "symsrc.h"
-#include "strlist.h"
-#include "intlist.h"
 #include "namespaces.h"
-#include "header.h"
 #include "path.h"
+#include "perf-libelf.h"
+#include "strlist.h"
+#include "symbol.h"
+#include "symsrc.h"
+#include "util.h" // lsdir()
+
+#include <linux/capability.h>
 #include <linux/ctype.h>
+#include <linux/kernel.h>
 #include <linux/log2.h>
+#include <linux/mman.h>
+#include <linux/string.h>
+#include <linux/time64.h>
 #include <linux/zalloc.h>
 
 #include <elf.h>
tools/perf/util/symbol.h +0 -4
index 2b2bbb5914b9..e265e1f028be 100644
@@ -153,10 +153,6 @@ 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.52.0.158.g65b55ccf14-goog
author reviewer
#6 lore raw

PATCH v2 6/10 perf symbol: Use fallbacks with filename__read_build_id

In reply to Ian Rogers

Try libbfd, libelf and then symbol-minimal when trying to read a build ID. Always compile in symbol-minimal so that it is the fallback of last resort. Rename libelf reading code. Move the decompression code into symbol.c and allow its use with the symbol-minimal version.

---
 tools/perf/util/Build            |   5 +-
 tools/perf/util/libbfd.c         |   9 +-
 tools/perf/util/libbfd.h         |   5 +-
 tools/perf/util/perf-libelf.c    | 113 ++++++++++++++++++++++
 tools/perf/util/perf-libelf.h    |  20 ++++
 tools/perf/util/symbol-elf.c     | 155 +------------------------------
 tools/perf/util/symbol-minimal.c |  17 ++--
 tools/perf/util/symbol-minimal.h |   9 ++
 tools/perf/util/symbol.c         |  52 +++++++++++
 9 files changed, 211 insertions(+), 174 deletions(-)
 create mode 100644 tools/perf/util/symbol-minimal.h
tools/perf/util/Build +1 -4
index dcac6f1e1d99..c40053db2212 100644
@@ -54,6 +54,7 @@ perf-util-y += usage.o
 perf-util-y += dso.o
 perf-util-y += dsos.o
 perf-util-y += symbol.o
+perf-util-y += symbol-minimal.o
 perf-util-y += symbol_fprintf.o
 perf-util-y += map_symbol.o
 perf-util-y += color.o
@@ -209,10 +210,6 @@ ifdef hashmap
 perf-util-y += hashmap.o
 endif
 
-ifndef CONFIG_LIBELF
-perf-util-y += symbol-minimal.o
-endif
-
 ifndef CONFIG_SETNS
 perf-util-y += setns.o
 endif
tools/perf/util/libbfd.c +2 -7
index cc0c474cbfaa..c438fbd03b24 100644
@@ -418,18 +418,13 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
 	return err;
 }
 
-int libbfd__read_build_id(const char *filename, struct build_id *bid)
+int libbfd__read_build_id(int _fd, const char *filename, struct build_id *bid)
 {
 	size_t size = sizeof(bid->data);
 	int err = -1, fd;
 	bfd *abfd;
 
-	if (!filename)
-		return -EFAULT;
-	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
-
-	fd = open(filename, O_RDONLY);
+	fd = dup(_fd);
 	if (fd < 0)
 		return -1;
 
tools/perf/util/libbfd.h +3 -2
index 086c5f12cc34..72f1ad9a7578 100644
@@ -26,7 +26,7 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
 int symbol__disassemble_libbfd(const char *filename, struct symbol *sym,
 			     struct annotate_args *args);
 
-int libbfd__read_build_id(const char *filename, struct build_id *bid);
+int libbfd__read_build_id(int fd, const char *filename, struct build_id *bid);
 
 int libbfd_filename__read_debuglink(const char *filename, char *debuglink, size_t size);
 
@@ -65,7 +65,8 @@ static inline int symbol__disassemble_libbfd(const char *filename __always_unuse
 	return -1;
 }
 
-static inline int libbfd__read_build_id(const char *filename __always_unused,
+static inline int libbfd__read_build_id(int fd __always_unused,
+					const char *filename __always_unused,
 					struct build_id *bid __always_unused)
 {
 	return -1;
tools/perf/util/perf-libelf.c +113 -0

122 lines not on this page · raw message

tools/perf/util/perf-libelf.h +20 -0
index 5d7d18daf5ff..931597493b1f 100644
@@ -2,6 +2,8 @@
 #ifndef __PERF_LIBELF_H
 #define __PERF_LIBELF_H
 
+struct build_id;
+
 #ifdef HAVE_LIBELF_SUPPORT
 
 #include <libelf.h>
@@ -17,9 +19,27 @@
 # define PERF_ELF_C_READ_MMAP ELF_C_READ
 #endif
 
+/*
+ * Align offset to 4 bytes as needed for note name and descriptor data.
+ */
+#define NOTE_ALIGN(n) (((n) + 3) & -4U)
+
 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name,
 			     size_t *idx);
 
+int __libelf__read_build_id(Elf *elf, void *bf, size_t size);
+
+int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid);
+
+#else // !defined(HAVE_LIBELF_SUPPORT)
+
+static inline int libelf__read_build_id(int fd __always_unused,
+					const char *filename __always_unused,
+					struct build_id *bid __always_unused)
+{
+	return -1;
+}
+
 #endif // defined(HAVE_LIBELF_SUPPORT)
 
 #endif /* __PERF_LIBELF_H */
tools/perf/util/symbol-elf.c +1 -154

167 lines not on this page · raw message

tools/perf/util/symbol-minimal.c +10 -7
index c6b17c14a2e9..b71cf8caf282 100644
@@ -1,5 +1,7 @@
+#include "debug.h"
 #include "dso.h"
 #include "symbol.h"
+#include "symbol-minimal.h"
 #include "symsrc.h"
 
 #include <errno.h>
@@ -75,17 +77,19 @@ static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
 	return -1;
 }
 
+#ifndef HAVE_LIBELF_SUPPORT
 int filename__read_debuglink(const char *filename __maybe_unused,
 			     char *debuglink __maybe_unused,
 			     size_t size __maybe_unused)
 {
 	return -1;
 }
+#endif
 
 /*
  * Just try PT_NOTE header otherwise fails
  */
-int filename__read_build_id(const char *filename, struct build_id *bid)
+int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid)
 {
 	int fd, ret = -1;
 	bool need_swap = false, elf32;
@@ -102,12 +106,7 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 	void *phdr, *buf = NULL;
 	ssize_t phdr_size, ehdr_size, buf_size = 0;
 
-	if (!filename)
-		return -EFAULT;
-	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
-
-	fd = open(filename, O_RDONLY);
+	fd = dup(_fd);
 	if (fd < 0)
 		return -1;
 
@@ -199,9 +198,12 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 	free(phdr);
 out:
 	close(fd);
+	if (ret)
+		pr_debug("Error reading build-id from %s\n", filename);
 	return ret;
 }
 
+#ifndef HAVE_LIBELF_SUPPORT
 int sysfs__read_build_id(const char *filename, struct build_id *bid)
 {
 	int fd;
@@ -363,3 +365,4 @@ bool filename__has_section(const char *filename __maybe_unused, const char *sec
 {
 	return false;
 }
+#endif // HAVE_LIBELF_SUPPORT
tools/perf/util/symbol-minimal.h new file +9 -0
new file mode 100644
index 000000000000..185d98968212
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_SYMBOL_MINIMAL_H
+#define __PERF_SYMBOL_MINIMAL_H
+
+struct build_id;
+
+int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid);
+
+#endif /* __PERF_SYMBOL_MINIMAL_H */
tools/perf/util/symbol.c +52 -0

64 lines not on this page · raw message

Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#7 lore raw

PATCH v2 7/10 perf symbol: Use fallbacks for filename__read_debuglink

In reply to Ian Rogers

Try libbfd and then libelf in symbol.c. Remove the non-existent symbol-minimal version.

---
 tools/perf/util/perf-libelf.c    | 52 +++++++++++++++++++++++++++++
 tools/perf/util/perf-libelf.h    | 10 ++++++
 tools/perf/util/symbol-elf.c     | 56 --------------------------------
 tools/perf/util/symbol-minimal.c |  9 -----
 tools/perf/util/symbol.c         | 10 ++++++
 5 files changed, 72 insertions(+), 65 deletions(-)
tools/perf/util/perf-libelf.c +52 -0
index c7fa08a177d0..a8a8c39056da 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "perf-libelf.h"
 
+#include <fcntl.h>
 #include <stddef.h>
 #include <string.h>
 #include <unistd.h>
@@ -143,3 +144,54 @@ int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid)
 out:
 	return err;
 }
+
+int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_t size)
+{
+	int fd, err = -1;
+	Elf *elf;
+	GElf_Ehdr ehdr;
+	GElf_Shdr shdr;
+	Elf_Data *data;
+	Elf_Scn *sec;
+	Elf_Kind ek;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0)
+		goto out;
+
+	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
+	if (elf == NULL) {
+		pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
+		goto out_close;
+	}
+
+	ek = elf_kind(elf);
+	if (ek != ELF_K_ELF)
+		goto out_elf_end;
+
+	if (gelf_getehdr(elf, &ehdr) == NULL) {
+		pr_err("%s: cannot get elf header.\n", __func__);
+		goto out_elf_end;
+	}
+
+	sec = elf_section_by_name(elf, &ehdr, &shdr,
+				  ".gnu_debuglink", NULL);
+	if (sec == NULL)
+		goto out_elf_end;
+
+	data = elf_getdata(sec, NULL);
+	if (data == NULL)
+		goto out_elf_end;
+
+	/* the start of this section is a zero-terminated string */
+	strncpy(debuglink, data->d_buf, size);
+
+	err = 0;
+
+out_elf_end:
+	elf_end(elf);
+out_close:
+	close(fd);
+out:
+	return err;
+}
tools/perf/util/perf-libelf.h +10 -0
index 931597493b1f..167679f9fc9b 100644
@@ -2,6 +2,8 @@
 #ifndef __PERF_LIBELF_H
 #define __PERF_LIBELF_H
 
+#include <stddef.h>
+
 struct build_id;
 
 #ifdef HAVE_LIBELF_SUPPORT
@@ -30,6 +32,7 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char
 int __libelf__read_build_id(Elf *elf, void *bf, size_t size);
 
 int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid);
+int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_t size);
 
 #else // !defined(HAVE_LIBELF_SUPPORT)
 
@@ -40,6 +43,13 @@ static inline int libelf__read_build_id(int fd __always_unused,
 	return -1;
 }
 
+static inline int libelf_filename__read_debuglink(const char *filename __always_unused,
+						  char *debuglink __always_unused,
+						  size_t size __always_unused)
+{
+	return -1;
+}
+
 #endif // defined(HAVE_LIBELF_SUPPORT)
 
 #endif /* __PERF_LIBELF_H */
tools/perf/util/symbol-elf.c +0 -56

62 lines not on this page · raw message

tools/perf/util/symbol-minimal.c +0 -9
index b71cf8caf282..445307f230af 100644
@@ -77,15 +77,6 @@ static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
 	return -1;
 }
 
-#ifndef HAVE_LIBELF_SUPPORT
-int filename__read_debuglink(const char *filename __maybe_unused,
-			     char *debuglink __maybe_unused,
-			     size_t size __maybe_unused)
-{
-	return -1;
-}
-#endif
-
 /*
  * Just try PT_NOTE header otherwise fails
  */
tools/perf/util/symbol.c +10 -0
index 8d461974a69f..da75a1c159f2 100644
@@ -2050,6 +2050,16 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 	return err;
 }
 
+int filename__read_debuglink(const char *filename, char *debuglink, size_t size)
+{
+	int err = libbfd_filename__read_debuglink(filename, debuglink, size);
+
+	if (err == 0)
+		return 0;
+
+	return libelf_filename__read_debuglink(filename, debuglink, size);
+}
+
 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
 {
 	struct build_id bid = { .size = 0, };
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#8 lore raw

PATCH v2 8/10 perf symbol: Make a common sysfs__read_build_id

In reply to Ian Rogers

The libelf version of sysfs__read_build_id would read through the file, the symbol-minimal version would read the whole file and then use regular build-id parsing. Given we prefer the libelf code, make both implementations use the libelf implementation and not require reading the whole file. As the libelf implementation uses libelf structs, move the code to symbol-minimal and use the style/implementation that exists there already in the in memory read_build_id code. Note, this means we could avoid reading phdrs where read_build_id is used and switch to read_build_id_fd to potentially avoid some memory allocation.

Clean up how bf was being used as it bound checked bf when a build ID note was found but not when the name wasn't "GNU". Switch to just lseek-ing forward the file to avoid any buffer overrun problems and unnecessary reading.

Reduce scope of NOTE_ALIGN macro in perf-libelf.h.

---
 tools/perf/util/perf-libelf.c    |  5 ++
 tools/perf/util/perf-libelf.h    |  5 --
 tools/perf/util/symbol-elf.c     | 50 --------------------
 tools/perf/util/symbol-minimal.c | 80 +++++++++++++++++++++++---------
 tools/perf/util/symbol-minimal.h |  1 +
 tools/perf/util/symbol.c         |  6 +++
 6 files changed, 69 insertions(+), 78 deletions(-)
tools/perf/util/perf-libelf.c +5 -0
index a8a8c39056da..4198f4dfad9f 100644
@@ -9,6 +9,11 @@
 #include "build-id.h"
 #include "debug.h"
 
+/*
+ * Align offset to 4 bytes as needed for note name and descriptor data.
+ */
+#define NOTE_ALIGN(n) (((n) + 3) & -4U)
+
 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
 			     GElf_Shdr *shp, const char *name, size_t *idx)
 {
tools/perf/util/perf-libelf.h +0 -5
index 167679f9fc9b..2118325c04e5 100644
@@ -21,11 +21,6 @@ struct build_id;
 # define PERF_ELF_C_READ_MMAP ELF_C_READ
 #endif
 
-/*
- * Align offset to 4 bytes as needed for note name and descriptor data.
- */
-#define NOTE_ALIGN(n) (((n) + 3) & -4U)
-
 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name,
 			     size_t *idx);
 
tools/perf/util/symbol-elf.c +0 -50

56 lines not on this page · raw message

tools/perf/util/symbol-minimal.c +57 -23

101 lines not on this page · raw message

tools/perf/util/symbol-minimal.h +1 -0
index 185d98968212..5cce1f1f0f16 100644
@@ -5,5 +5,6 @@
 struct build_id;
 
 int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid);
+int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bid);
 
 #endif /* __PERF_SYMBOL_MINIMAL_H */
tools/perf/util/symbol.c +6 -0
index da75a1c159f2..76dc5b70350a 100644
@@ -2050,6 +2050,12 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 	return err;
 }
 
+int sysfs__read_build_id(const char *filename, struct build_id *bid)
+{
+	/* Doesn't mmap file into memory. */
+	return sym_min_sysfs__read_build_id(filename, bid);
+}
+
 int filename__read_debuglink(const char *filename, char *debuglink, size_t size)
 {
 	int err = libbfd_filename__read_debuglink(filename, debuglink, size);
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#9 lore raw

PATCH v2 9/10 perf dso: Move type helpers out of symbol and use fallbacks

In reply to Ian Rogers

Fallback between libelf and symbol-minimal versions. Remove the symbol.c code and just directly inline into the only caller in dso.c.

---
 tools/perf/util/dso.c            | 57 ++++++++++++++----------
 tools/perf/util/perf-libelf.c    | 33 ++++++++++++++
 tools/perf/util/perf-libelf.h    |  8 ++++
 tools/perf/util/symbol-elf.c     | 33 --------------
 tools/perf/util/symbol-minimal.c | 76 ++++++++++++++++----------------
 tools/perf/util/symbol-minimal.h |  3 ++
 tools/perf/util/symbol.h         |  2 -
 7 files changed, 115 insertions(+), 97 deletions(-)
tools/perf/util/dso.c +33 -24
index 344e689567ee..421fa4d27d2a 100644
@@ -1,37 +1,42 @@
 // SPDX-License-Identifier: GPL-2.0
-#include <asm/bug.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 <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 <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "annotate-data.h"
+#include "auxtrace.h"
 #include "compress.h"
+#include "debug.h"
+#include "dso.h"
+#include "dsos.h"
 #include "env.h"
+#include "machine.h"
+#include "map.h"
 #include "namespaces.h"
 #include "path.h"
-#include "map.h"
-#include "symbol.h"
+#include "perf-libelf.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-minimal.h"
+#include "symbol.h"
+#include "util.h" /* O_CLOEXEC for older systems */
 #include "vdso.h"
-#include "annotate-data.h"
+
+#include <asm/bug.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/zalloc.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",
@@ -1747,7 +1752,11 @@ enum dso_type dso__type(struct dso *dso, struct machine *machine)
 	enum dso_type type = DSO__TYPE_UNKNOWN;
 
 	if (dso__data_get_fd(dso, machine, &fd)) {
-		type = dso__type_fd(fd);
+		type = libelf_dso__type_fd(fd);
+
+		if (type == DSO__TYPE_UNKNOWN)
+			type = sym_min_dso__type_fd(fd);
+
 		dso__data_put_fd(dso);
 	}
 
tools/perf/util/perf-libelf.c +33 -0
index 4198f4dfad9f..eed5c0317f8b 100644
@@ -200,3 +200,36 @@ int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_
 out:
 	return err;
 }
+
+enum dso_type libelf_dso__type_fd(int fd)
+{
+	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
+	GElf_Ehdr ehdr;
+	Elf_Kind ek;
+	Elf *elf;
+
+	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
+	if (elf == NULL)
+		goto out;
+
+	ek = elf_kind(elf);
+	if (ek != ELF_K_ELF)
+		goto out_end;
+
+	if (gelf_getclass(elf) == ELFCLASS64) {
+		dso_type = DSO__TYPE_64BIT;
+		goto out_end;
+	}
+
+	if (gelf_getehdr(elf, &ehdr) == NULL)
+		goto out_end;
+
+	if (ehdr.e_machine == EM_X86_64)
+		dso_type = DSO__TYPE_X32BIT;
+	else
+		dso_type = DSO__TYPE_32BIT;
+out_end:
+	elf_end(elf);
+out:
+	return dso_type;
+}
tools/perf/util/perf-libelf.h +8 -0
index 2118325c04e5..5ca5641b9dc9 100644
@@ -4,6 +4,8 @@
 
 #include <stddef.h>
 
+#include "dso.h"
+
 struct build_id;
 
 #ifdef HAVE_LIBELF_SUPPORT
@@ -28,6 +30,7 @@ int __libelf__read_build_id(Elf *elf, void *bf, size_t size);
 
 int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid);
 int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_t size);
+enum dso_type libelf_dso__type_fd(int fd);
 
 #else // !defined(HAVE_LIBELF_SUPPORT)
 
@@ -45,6 +48,11 @@ static inline int libelf_filename__read_debuglink(const char *filename __always_
 	return -1;
 }
 
+enum dso_type libelf_dso__type_fd(int fd __always_unused)
+{
+	return DSO__TYPE_UNKNOWN;
+}
+
 #endif // defined(HAVE_LIBELF_SUPPORT)
 
 #endif /* __PERF_LIBELF_H */
tools/perf/util/symbol-elf.c +0 -33
index cb890de31044..f483aa67f69b 100644
@@ -1572,39 +1572,6 @@ int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
 	return err;
 }
 
-enum dso_type dso__type_fd(int fd)
-{
-	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
-	GElf_Ehdr ehdr;
-	Elf_Kind ek;
-	Elf *elf;
-
-	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-	if (elf == NULL)
-		goto out;
-
-	ek = elf_kind(elf);
-	if (ek != ELF_K_ELF)
-		goto out_end;
-
-	if (gelf_getclass(elf) == ELFCLASS64) {
-		dso_type = DSO__TYPE_64BIT;
-		goto out_end;
-	}
-
-	if (gelf_getehdr(elf, &ehdr) == NULL)
-		goto out_end;
-
-	if (ehdr.e_machine == EM_X86_64)
-		dso_type = DSO__TYPE_X32BIT;
-	else
-		dso_type = DSO__TYPE_32BIT;
-out_end:
-	elf_end(elf);
-out:
-	return dso_type;
-}
-
 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
 {
 	ssize_t r;
tools/perf/util/symbol-minimal.c +38 -38

88 lines not on this page · raw message

tools/perf/util/symbol-minimal.h +3 -0
index 5cce1f1f0f16..282466aca7d9 100644
@@ -2,9 +2,12 @@
 #ifndef __PERF_SYMBOL_MINIMAL_H
 #define __PERF_SYMBOL_MINIMAL_H
 
+#include "dso.h"
+
 struct build_id;
 
 int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid);
 int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bid);
+enum dso_type sym_min_dso__type_fd(int fd);
 
 #endif /* __PERF_SYMBOL_MINIMAL_H */
tools/perf/util/symbol.h +0 -2
index e265e1f028be..4cc557981724 100644
@@ -117,8 +117,6 @@ struct symbol *dso__first_symbol(struct dso *dso);
 struct symbol *dso__last_symbol(struct dso *dso);
 struct symbol *dso__next_symbol(struct symbol *sym);
 
-enum dso_type dso__type_fd(int fd);
-
 int filename__read_build_id(const char *filename, struct build_id *id);
 int sysfs__read_build_id(const char *filename, struct build_id *bid);
 int modules__parse(const char *filename, void *arg,
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#10 lore raw

PATCH v2 10/10 perf symbol: Fix ENOENT case for filename__read_build_id

In reply to Ian Rogers

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

Fixes 834ebb5678d7 ("perf tools: Don't read build-ids from non-regular files")
---
 tools/perf/builtin-buildid-cache.c | 6 ++++--
 tools/perf/util/symbol.c           | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/builtin-buildid-cache.c +4 -2
index c98104481c8a..539e779e3268 100644
@@ -276,12 +276,14 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
 {
 	char filename[PATH_MAX];
 	struct build_id bid = { .size = 0, };
+	int err;
 
 	if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
 		return true;
 
-	if (filename__read_build_id(filename, &bid) == -1) {
-		if (errno == ENOENT)
+	err = filename__read_build_id(filename, &bid);
+	if (err < 0) {
+		if (err == -ENOENT)
 			return false;
 
 		pr_warning("Problems with %s file, consider removing it from the cache\n",
tools/perf/util/symbol.c +2 -1
index 76dc5b70350a..f43e30019e21 100644
@@ -2008,8 +2008,9 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 	if (!filename)
 		return -EFAULT;
 
+	errno = 0;
 	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
 	err = kmod_path__parse(&m, filename);
 	if (err)
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
author reviewer
#11 lore raw

Re: [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id

Quoted 10 lines · Ian Rogers

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

Fixes 834ebb5678d7 ("perf tools: Don't read build-ids from non-regular files")

We might want to prioritize this fix.

Quoted 11 lines
---
 tools/perf/builtin-buildid-cache.c | 6 ++++--
 tools/perf/util/symbol.c           | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/builtin-buildid-cache.c +0 -0
index c98104481c8a..539e779e3268 100644

tools/perf/builtin-buildid-cache.c static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused) · line 283

Quoted 9 lines
tools/perf/builtin-buildid-cache.c +1 -1
@@ -276,12 +276,14 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
 {
        char filename[PATH_MAX];
        struct build_id bid = { .size = 0, };
+       int err;
 
        if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
                return true;
 
-       if (filename__read_build_id(filename, &bid) == -1) {

This check here is clearly wrong when -EWOULDBLOCK is returned from James' change.

Quoted 12 lines
tools/perf/builtin-buildid-cache.c +3 -1
-               if (errno == ENOENT)
+       err = filename__read_build_id(filename, &bid);
+       if (err < 0) {
+               if (err == -ENOENT)
                        return false;
 
                pr_warning("Problems with %s file, consider removing it from the cache\n",
tools/perf/util/symbol.c +0 -0
index 76dc5b70350a..f43e30019e21 100644

tools/perf/util/symbol.c int filename__read_build_id(const char *filename, struct build_id *bid) · line 2013

Quoted 7 lines
tools/perf/util/symbol.c +2 -1
@@ -2008,8 +2008,9 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
        if (!filename)
                return -EFAULT;
 
+       errno = 0;
        if (!is_regular_file(filename))
-               return -EWOULDBLOCK;
+               return errno == 0 ? -EWOULDBLOCK : -errno;

I've made the fix after the other changes as it is simpler to fix in one filename__read_build_id rather than all the libbfd, .. variants. If we don't want the series in the short-term perhaps we still want to carry some parts of this fix.

Thanks,
Ian

Quoted 6 lines
tools/perf/util/symbol.c +0 -1
        err = kmod_path__parse(&m, filename);
        if (err)
--

2.52.0.158.g65b55ccf14-goog

maintainer
#12 lore raw

Re: [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id

Hi Ian,

Quoted 5 lines · Ian Rogers
Quoted 4 lines · Ian Rogers

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

Quoted 8 lines
Quoted 6 lines

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

Fixes 834ebb5678d7 ("perf tools: Don't read build-ids from non-regular files")

We might want to prioritize this fix.

Sure.

Quoted 40 lines
Quoted 12 lines
---
 tools/perf/builtin-buildid-cache.c | 6 ++++--
 tools/perf/util/symbol.c           | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/builtin-buildid-cache.c +0 -0
index c98104481c8a..539e779e3268 100644

tools/perf/builtin-buildid-cache.c static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused) · line 283

Quoted 9 lines
tools/perf/builtin-buildid-cache.c +1 -1
@@ -276,12 +276,14 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
 {
        char filename[PATH_MAX];
        struct build_id bid = { .size = 0, };
+       int err;
 
        if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
                return true;
 
-       if (filename__read_build_id(filename, &bid) == -1) {

This check here is clearly wrong when -EWOULDBLOCK is returned from James' change.

Quoted 15 lines
tools/perf/builtin-buildid-cache.c +3 -1
-               if (errno == ENOENT)
+       err = filename__read_build_id(filename, &bid);
+       if (err < 0) {
+               if (err == -ENOENT)
                        return false;
 
                pr_warning("Problems with %s file, consider removing it from the cache\n",
tools/perf/util/symbol.c +0 -0
index 76dc5b70350a..f43e30019e21 100644
@@ -2008,8 +2008,9 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
        if (!filename)
                return -EFAULT;
 
Quoted 9 lines

tools/perf/util/symbol.c int filename__read_build_id(const char *filename, struct build_id *bid) · line 2013

Quoted 4 lines
tools/perf/util/symbol.c +2 -1
+       errno = 0;
        if (!is_regular_file(filename))
-               return -EWOULDBLOCK;
+               return errno == 0 ? -EWOULDBLOCK : -errno;

I've made the fix after the other changes as it is simpler to fix in one filename__read_build_id rather than all the libbfd, .. variants. If we don't want the series in the short-term perhaps we still want to carry some parts of this fix.

Yeah, I hesitate to merge a big series at this moment unless it's a critical and obvious fix. Can you please separate this change and send it out?

Thanks,
Namhyung

Quoted 7 lines
Quoted 7 lines
tools/perf/util/symbol.c +0 -1
        err = kmod_path__parse(&m, filename);
        if (err)
--

2.52.0.158.g65b55ccf14-goog

author reviewer
#13 lore raw

Re: [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id

Quoted 70 lines · Namhyung Kim

Hi Ian,

Quoted 5 lines · Ian Rogers
Quoted 4 lines · Ian Rogers

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

Quoted 8 lines
Quoted 6 lines

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

Fixes 834ebb5678d7 ("perf tools: Don't read build-ids from non-regular files")

We might want to prioritize this fix.

Sure.

Quoted 40 lines
Quoted 12 lines
---
 tools/perf/builtin-buildid-cache.c | 6 ++++--
 tools/perf/util/symbol.c           | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/builtin-buildid-cache.c +0 -0
index c98104481c8a..539e779e3268 100644

tools/perf/builtin-buildid-cache.c static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused) · line 283

Quoted 9 lines
tools/perf/builtin-buildid-cache.c +1 -1
@@ -276,12 +276,14 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
 {
        char filename[PATH_MAX];
        struct build_id bid = { .size = 0, };
+       int err;
 
        if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
                return true;
 
-       if (filename__read_build_id(filename, &bid) == -1) {

This check here is clearly wrong when -EWOULDBLOCK is returned from James' change.

Quoted 15 lines
tools/perf/builtin-buildid-cache.c +3 -1
-               if (errno == ENOENT)
+       err = filename__read_build_id(filename, &bid);
+       if (err < 0) {
+               if (err == -ENOENT)
                        return false;
 
                pr_warning("Problems with %s file, consider removing it from the cache\n",
tools/perf/util/symbol.c +0 -0
index 76dc5b70350a..f43e30019e21 100644
@@ -2008,8 +2008,9 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
        if (!filename)
                return -EFAULT;
 
Quoted 9 lines

tools/perf/util/symbol.c int filename__read_build_id(const char *filename, struct build_id *bid) · line 2013

Quoted 4 lines
tools/perf/util/symbol.c +2 -1
+       errno = 0;
        if (!is_regular_file(filename))
-               return -EWOULDBLOCK;
+               return errno == 0 ? -EWOULDBLOCK : -errno;

I've made the fix after the other changes as it is simpler to fix in one filename__read_build_id rather than all the libbfd, .. variants. If we don't want the series in the short-term perhaps we still want to carry some parts of this fix.

Quoted 3 lines

Yeah, I hesitate to merge a big series at this moment unless it's a critical and obvious fix. Can you please separate this change and send it out?

Sent in:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian

Quoted 11 lines

Thanks,
Namhyung

Quoted 7 lines
Quoted 7 lines
tools/perf/util/symbol.c +0 -1
        err = kmod_path__parse(&m, filename);
        if (err)
--

2.52.0.158.g65b55ccf14-goog