* [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink
@ 2025-12-01 20:54 Ian Rogers
2025-12-01 20:55 ` [PATCH v2 01/10] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
` (9 more replies)
0 siblings, 10 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:54 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
This cleans up some tech debt mentioned in the thread:
https://lore.kernel.org/lkml/aQt66zhfxSA80xwt@gentoo.org/
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/20251121-james-perf-non-block-v1-1-6ab2702ab573@linaro.org/
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/20251123023225.8069-1-irogers@google.com/
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
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 01/10] perf symbol: Reduce scope of elf__needs_adjust_symbols
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 02/10] perf symbol: Reduce scope of arch__sym_update Ian Rogers
` (8 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/symbol-elf.c | 8 ++++----
tools/perf/util/symbol.h | 1 -
2 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index c5c382c3409a..f661ea6c8ac2 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -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) {
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 3fb5d146d9b1..9aa0016937b7 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -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
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 02/10] perf symbol: Reduce scope of arch__sym_update
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
2025-12-01 20:55 ` [PATCH v2 01/10] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 03/10] perf symbol: Move libelf code to its own file/header Ian Rogers
` (7 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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(-)
diff --git a/tools/perf/arch/powerpc/util/sym-handling.c b/tools/perf/arch/powerpc/util/sym-handling.c
index 947bfad7aa59..afefa9bd0c93 100644
--- a/tools/perf/arch/powerpc/util/sym-handling.c
+++ b/tools/perf/arch/powerpc/util/sym-handling.c
@@ -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,
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index f661ea6c8ac2..99b1e060acf5 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -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++;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 9aa0016937b7..f12bb3d0e08b 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -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
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 03/10] perf symbol: Move libelf code to its own file/header
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
2025-12-01 20:55 ` [PATCH v2 01/10] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
2025-12-01 20:55 ` [PATCH v2 02/10] perf symbol: Reduce scope of arch__sym_update Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 04/10] perf symbol: Remove unused includes Ian Rogers
` (6 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 1c2a43e1dc68..dcac6f1e1d99 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -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
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
new file mode 100644
index 000000000000..abd55bd7f14b
--- /dev/null
+++ b/tools/perf/util/perf-libelf.c
@@ -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;
+}
+
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
new file mode 100644
index 000000000000..5d7d18daf5ff
--- /dev/null
+++ b/tools/perf/util/perf-libelf.h
@@ -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 */
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index 6ab2eb551b6c..c27ad9121230 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -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"
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 5ffd97ee4898..f94f742a3458 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -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"
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 99b1e060acf5..72632bf9cc30 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -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;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index f12bb3d0e08b..393a528326d5 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -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).
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 04/10] perf symbol: Remove unused includes
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
` (2 preceding siblings ...)
2025-12-01 20:55 ` [PATCH v2 03/10] perf symbol: Move libelf code to its own file/header Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 05/10] perf symbol: Move dso__load_bfd_symbols out of symbol.h Ian Rogers
` (5 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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(-)
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 9c27bb30b708..2967dd085b82 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -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;
diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index 14c3823f8fed..6cc0060cacca 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -12,41 +12,45 @@
*/
#include <errno.h>
#include <inttypes.h>
+#include <sys/param.h>
+
+#include "builtin.h"
+
+#include "ui/browsers/hists.h"
+#include "ui/progress.h"
+#include "ui/ui.h"
+#include "util/addr_location.h"
+#include "util/annotate.h"
+#include "util/cacheline.h"
+#include "util/data.h"
+#include "util/debug.h"
+#include "util/event.h"
+#include "util/evlist.h"
+#include "util/evsel.h"
+#include "util/hist.h"
+#include "util/map_symbol.h"
+#include "util/mem-events.h"
+#include "util/mem-info.h"
+#include "util/mem2node.h"
+#include "util/pmus.h"
+#include "util/session.h"
+#include "util/sort.h"
+#include "util/string2.h"
+#include "util/symbol.h"
+#include "util/symbol.h"
+#include "util/thread.h"
+#include "util/tool.h"
+#include "util/util.h"
+
+#include <asm/bug.h>
#include <linux/compiler.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/stringify.h>
#include <linux/zalloc.h>
-#include <asm/bug.h>
-#include <sys/param.h>
-#include "debug.h"
-#include "builtin.h"
#include <perf/cpumap.h>
#include <subcmd/pager.h>
#include <subcmd/parse-options.h>
-#include "map_symbol.h"
-#include "mem-events.h"
-#include "session.h"
-#include "hist.h"
-#include "sort.h"
-#include "tool.h"
-#include "cacheline.h"
-#include "data.h"
-#include "event.h"
-#include "evlist.h"
-#include "evsel.h"
-#include "ui/browsers/hists.h"
-#include "thread.h"
-#include "mem2node.h"
-#include "mem-info.h"
-#include "symbol.h"
-#include "ui/ui.h"
-#include "ui/progress.h"
-#include "pmus.h"
-#include "string2.h"
-#include "util/util.h"
-#include "util/symbol.h"
-#include "util/annotate.h"
struct c2c_hists {
struct hists hists;
diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 53d5ea4a6a4f..9fc8a18647d1 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -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>
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 0e40f2a4bd18..311225944b78 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -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>
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 7929a5fa5f46..e9f5b3ff24e7 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -1,42 +1,41 @@
// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <inttypes.h>
+#include <locale.h>
+#include <regex.h>
+
#include "builtin.h"
+#include "util/addr_location.h"
+#include "util/callchain.h"
+#include "util/config.h"
+#include "util/cpumap.h"
+#include "util/data.h"
+#include "util/debug.h"
#include "util/dso.h"
#include "util/evlist.h"
#include "util/evsel.h"
-#include "util/config.h"
+#include "util/header.h"
#include "util/map.h"
+#include "util/session.h"
+#include "util/string2.h"
#include "util/symbol.h"
#include "util/thread.h"
-#include "util/header.h"
-#include "util/session.h"
-#include "util/tool.h"
-#include "util/callchain.h"
#include "util/time-utils.h"
-#include <linux/err.h>
-
-#include <subcmd/pager.h>
-#include <subcmd/parse-options.h>
+#include "util/tool.h"
#include "util/trace-event.h"
-#include "util/data.h"
-#include "util/cpumap.h"
-
-#include "util/debug.h"
-#include "util/string2.h"
#include "util/util.h"
+#include <event-parse.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/numa.h>
#include <linux/rbtree.h>
#include <linux/string.h>
#include <linux/zalloc.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <locale.h>
-#include <regex.h>
-
-#include <linux/ctype.h>
-#include <event-parse.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
static int kmem_slab;
static int kmem_page;
diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
index 7f3068264568..4a274c550cdf 100644
--- a/tools/perf/builtin-kwork.c
+++ b/tools/perf/builtin-kwork.c
@@ -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>
diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c
index d43500b92a7b..bad3784bd119 100644
--- a/tools/perf/builtin-mem.c
+++ b/tools/perf/builtin-mem.c
@@ -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
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 2bc269f5fcef..cdc7dd8d6320 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -6,68 +6,66 @@
* look up and read DSOs and symbol information and display
* a histogram of results, along various sorting keys.
*/
-#include "builtin.h"
+#include <dlfcn.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <regex.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
-#include "util/config.h"
+#include "builtin.h"
+#include "perf.h"
+#include "arch/common.h"
+#include "ui/progress.h"
+#include "ui/ui.h"
+#include "util/addr_location.h"
#include "util/annotate.h"
-#include "util/color.h"
-#include "util/dso.h"
-#include <linux/list.h>
-#include <linux/rbtree.h>
-#include <linux/err.h>
-#include <linux/zalloc.h>
-#include "util/map.h"
-#include "util/symbol.h"
-#include "util/map_symbol.h"
-#include "util/mem-events.h"
+#include "util/auxtrace.h"
+#include "util/block-info.h"
#include "util/branch.h"
#include "util/callchain.h"
-#include "util/values.h"
-
-#include "perf.h"
+#include "util/color.h"
+#include "util/config.h"
+#include "util/data.h"
#include "util/debug.h"
+#include "util/dso.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/evswitch.h"
#include "util/header.h"
+#include "util/hist.h"
+#include "util/map.h"
+#include "util/map_symbol.h"
+#include "util/mem-events.h"
#include "util/mem-info.h"
+#include "util/parse-events.h"
#include "util/session.h"
+#include "util/sort.h"
#include "util/srcline.h"
-#include "util/tool.h"
-
-#include <subcmd/parse-options.h>
-#include <subcmd/exec-cmd.h>
-#include "util/parse-events.h"
-
+#include "util/symbol.h"
#include "util/thread.h"
-#include "util/sort.h"
-#include "util/hist.h"
-#include "util/data.h"
-#include "arch/common.h"
#include "util/time-utils.h"
-#include "util/auxtrace.h"
+#include "util/tool.h"
#include "util/units.h"
#include "util/util.h" // perf_tip()
-#include "ui/ui.h"
-#include "ui/progress.h"
-#include "util/block-info.h"
+#include "util/values.h"
-#include <dlfcn.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <regex.h>
-#include <linux/ctype.h>
-#include <signal.h>
#include <linux/bitmap.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
+#include <linux/list.h>
#include <linux/list_sort.h>
+#include <linux/mman.h>
+#include <linux/rbtree.h>
#include <linux/string.h>
#include <linux/stringify.h>
#include <linux/time64.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <linux/mman.h>
+#include <linux/zalloc.h>
+#include <subcmd/exec-cmd.h>
+#include <subcmd/parse-options.h>
#ifdef HAVE_LIBTRACEEVENT
#include <event-parse.h>
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index eca3b1c58c4b..bc9e098642df 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1,51 +1,49 @@
// SPDX-License-Identifier: GPL-2.0
+#include <inttypes.h>
+#include <errno.h>
+#include <semaphore.h>
+#include <pthread.h>
+#include <math.h>
+
#include "builtin.h"
#include "perf.h"
#include "perf-sys.h"
+#include "util/addr_location.h"
+#include "util/callchain.h"
+#include "util/cloexec.h"
+#include "util/color.h"
#include "util/cpumap.h"
+#include "util/debug.h"
+#include "util/event.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/evsel_fprintf.h"
-#include "util/mutex.h"
-#include "util/symbol.h"
-#include "util/thread.h"
#include "util/header.h"
+#include "util/mutex.h"
#include "util/session.h"
-#include "util/tool.h"
-#include "util/cloexec.h"
-#include "util/thread_map.h"
-#include "util/color.h"
#include "util/stat.h"
#include "util/string2.h"
-#include "util/callchain.h"
+#include "util/symbol.h"
+#include "util/thread.h"
+#include "util/thread_map.h"
#include "util/time-utils.h"
-
-#include <subcmd/pager.h>
-#include <subcmd/parse-options.h>
+#include "util/tool.h"
#include "util/trace-event.h"
-
-#include "util/debug.h"
-#include "util/event.h"
#include "util/util.h"
+#include <api/fs/fs.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/log2.h>
+#include <linux/time64.h>
#include <linux/zalloc.h>
+#include <perf/cpumap.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
#include <sys/prctl.h>
#include <sys/resource.h>
-#include <inttypes.h>
-
-#include <errno.h>
-#include <semaphore.h>
-#include <pthread.h>
-#include <math.h>
-#include <api/fs/fs.h>
-#include <perf/cpumap.h>
-#include <linux/time64.h>
-#include <linux/err.h>
-
-#include <linux/ctype.h>
#define PR_SET_NAME 15 /* Set process name */
#define MAX_CPUS 4096
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 011962e1ee0f..ba363884a79d 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -1,73 +1,76 @@
// SPDX-License-Identifier: GPL-2.0
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+
#include "builtin.h"
+#include "perf.h"
+#include "ui/ui.h"
+#include "util/addr_location.h"
+#include "util/annotate.h"
+#include "util/archinsn.h"
+#include "util/auxtrace.h"
+#include "util/cgroup.h"
+#include "util/color.h"
#include "util/counts.h"
+#include "util/cpumap.h"
+#include "util/data.h"
#include "util/debug.h"
+#include "util/dlfilter.h"
#include "util/dso.h"
-#include <subcmd/exec-cmd.h>
-#include "util/header.h"
-#include <subcmd/parse-options.h>
-#include "util/perf_regs.h"
-#include "util/session.h"
-#include "util/tool.h"
-#include "util/map.h"
-#include "util/srcline.h"
-#include "util/symbol.h"
-#include "util/thread.h"
-#include "util/trace-event.h"
+#include "util/dump-insn.h"
#include "util/env.h"
+#include "util/event.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/evsel_fprintf.h"
#include "util/evswitch.h"
+#include "util/header.h"
+#include "util/map.h"
+#include "util/mem-events.h"
+#include "util/mem-info.h"
+#include "util/metricgroup.h"
+#include "util/path.h"
+#include "util/perf_regs.h"
+#include "util/print_binary.h"
+#include "util/print_insn.h"
+#include "util/record.h"
+#include "util/session.h"
#include "util/sort.h"
-#include "util/data.h"
-#include "util/auxtrace.h"
-#include "util/cpumap.h"
-#include "util/thread_map.h"
+#include "util/srcline.h"
#include "util/stat.h"
-#include "util/color.h"
#include "util/string2.h"
+#include "util/symbol.h"
#include "util/thread-stack.h"
+#include "util/thread.h"
+#include "util/thread_map.h"
#include "util/time-utils.h"
-#include "util/path.h"
-#include "util/event.h"
-#include "util/mem-info.h"
-#include "util/metricgroup.h"
-#include "ui/ui.h"
-#include "print_binary.h"
-#include "print_insn.h"
-#include "archinsn.h"
+#include "util/tool.h"
+#include "util/trace-event.h"
+#include "util/util.h"
+
+#include <asm/bug.h>
#include <linux/bitmap.h>
#include <linux/compiler.h>
+#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/stringify.h>
#include <linux/time64.h>
-#include <linux/zalloc.h>
#include <linux/unaligned.h>
-#include <sys/utsname.h>
-#include "asm/bug.h"
-#include "util/mem-events.h"
-#include "util/dump-insn.h"
-#include <dirent.h>
-#include <errno.h>
-#include <inttypes.h>
+#include <linux/zalloc.h>
+#include <perf/evlist.h>
#include <signal.h>
#include <stdio.h>
+#include <subcmd/exec-cmd.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
#include <sys/param.h>
-#include <sys/types.h>
#include <sys/stat.h>
-#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
#include <unistd.h>
-#include <subcmd/pager.h>
-#include <perf/evlist.h>
-#include <linux/err.h>
-#include "util/dlfilter.h"
-#include "util/record.h"
-#include "util/util.h"
-#include "util/cgroup.h"
-#include "util/annotate.h"
-#include "perf.h"
#include <linux/ctype.h>
#ifdef HAVE_LIBTRACEEVENT
diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index 22050c640dfa..3f464df723d4 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -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);
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 710604c4f6f6..60cb08783457 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -16,77 +16,72 @@
* Mike Galbraith <efault@gmx.de>
* Paul Mackerras <paulus@samba.org>
*/
-#include "builtin.h"
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <poll.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <sys/uio.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
+#include "builtin.h"
#include "perf.h"
+#include "arch/common.h"
+#include "ui/ui.h"
+#include "util/addr_location.h"
#include "util/annotate.h"
#include "util/bpf-event.h"
+#include "util/callchain.h"
#include "util/cgroup.h"
-#include "util/config.h"
#include "util/color.h"
+#include "util/config.h"
+#include "util/cpumap.h"
+#include "util/debug.h"
#include "util/dso.h"
+#include "util/event.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/evsel_config.h"
-#include "util/event.h"
+#include "util/intlist.h"
#include "util/machine.h"
#include "util/map.h"
#include "util/mmap.h"
+#include "util/ordered-events.h"
+#include "util/parse-branch-options.h"
+#include "util/parse-events.h"
+#include "util/pfm.h"
#include "util/session.h"
-#include "util/thread.h"
+#include "util/sort.h"
#include "util/stat.h"
+#include "util/string2.h"
#include "util/symbol.h"
#include "util/synthetic-events.h"
+#include "util/term.h"
+#include "util/thread.h"
#include "util/top.h"
#include "util/util.h"
-#include <linux/rbtree.h>
-#include <subcmd/parse-options.h>
-#include "util/parse-events.h"
-#include "util/callchain.h"
-#include "util/cpumap.h"
-#include "util/sort.h"
-#include "util/string2.h"
-#include "util/term.h"
-#include "util/intlist.h"
-#include "util/parse-branch-options.h"
-#include "arch/common.h"
-#include "ui/ui.h"
-#include "util/debug.h"
-#include "util/ordered-events.h"
-#include "util/pfm.h"
-
-#include <assert.h>
#include <elf.h>
-#include <fcntl.h>
-
-#include <stdio.h>
-#include <termios.h>
-#include <unistd.h>
-#include <inttypes.h>
-
-#include <errno.h>
-#include <time.h>
-#include <sched.h>
-#include <signal.h>
-
-#include <sys/syscall.h>
-#include <sys/ioctl.h>
-#include <poll.h>
-#include <sys/prctl.h>
-#include <sys/wait.h>
-#include <sys/uio.h>
-#include <sys/utsname.h>
-#include <sys/mman.h>
-
+#include <linux/ctype.h>
+#include <linux/err.h>
+#include <linux/rbtree.h>
#include <linux/stringify.h>
#include <linux/time64.h>
#include <linux/types.h>
-#include <linux/err.h>
-
-#include <linux/ctype.h>
#include <perf/mmap.h>
+#include <subcmd/parse-options.h>
static volatile sig_atomic_t done;
static volatile sig_atomic_t resize;
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index a743bda294bd..f4791b9b8b3b 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -13,17 +13,22 @@
*
* http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
*/
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/sysmacros.h>
-#include "util/record.h"
-#include <api/fs/tracing_path.h>
-#ifdef HAVE_LIBBPF_SUPPORT
-#include <bpf/bpf.h>
-#include <bpf/libbpf.h>
-#include <bpf/btf.h>
-#endif
-#include "util/bpf_map.h"
-#include "util/rlimit.h"
#include "builtin.h"
+#include "perf.h"
+
+#include "trace/beauty/beauty.h"
+#include "util/addr_location.h"
+#include "util/bpf_map.h"
+#include "util/callchain.h"
#include "util/cgroup.h"
#include "util/color.h"
#include "util/config.h"
@@ -31,47 +36,39 @@
#include "util/dso.h"
#include "util/env.h"
#include "util/event.h"
+#include "util/evlist.h"
#include "util/evsel.h"
#include "util/evsel_fprintf.h"
-#include "util/synthetic-events.h"
-#include "util/evlist.h"
#include "util/evswitch.h"
#include "util/hashmap.h"
-#include "util/mmap.h"
-#include <subcmd/pager.h>
-#include <subcmd/exec-cmd.h>
+#include "util/include/dwarf-regs.h"
+#include "util/intlist.h"
#include "util/machine.h"
#include "util/map.h"
-#include "util/symbol.h"
+#include "util/mmap.h"
+#include "util/parse-events.h"
#include "util/path.h"
+#include "util/print_binary.h"
+#include "util/record.h"
+#include "util/rlimit.h"
#include "util/session.h"
-#include "util/thread.h"
-#include <subcmd/parse-options.h>
+#include "util/stat.h"
+#include "util/string2.h"
#include "util/strlist.h"
-#include "util/intlist.h"
+#include "util/symbol.h"
+#include "util/synthetic-events.h"
+#include "util/syscalltbl.h"
+#include "util/thread.h"
#include "util/thread_map.h"
-#include "util/stat.h"
#include "util/tool.h"
+#include "util/trace-event.h"
#include "util/trace.h"
-#include "util/util.h"
-#include "trace/beauty/beauty.h"
-#include "trace-event.h"
-#include "util/parse-events.h"
+#include "util/trace_augment.h"
#include "util/tracepoint.h"
-#include "callchain.h"
-#include "print_binary.h"
-#include "string2.h"
-#include "syscalltbl.h"
-#include "../perf.h"
-#include "trace_augment.h"
-#include "dwarf-regs.h"
+#include "util/util.h"
-#include <errno.h>
-#include <inttypes.h>
-#include <poll.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <string.h>
+#include <api/fs/tracing_path.h>
+#include <linux/ctype.h>
#include <linux/err.h>
#include <linux/filter.h>
#include <linux/kernel.h>
@@ -80,13 +77,18 @@
#include <linux/stringify.h>
#include <linux/time64.h>
#include <linux/zalloc.h>
-#include <fcntl.h>
-#include <sys/sysmacros.h>
-
-#include <linux/ctype.h>
#include <perf/mmap.h>
+#include <subcmd/exec-cmd.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
#include <tools/libc_compat.h>
+#ifdef HAVE_LIBBPF_SUPPORT
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+#include <bpf/btf.h>
+#endif
+
#ifdef HAVE_LIBTRACEEVENT
#include <event-parse.h>
#endif
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
index 60dcfe56d4d9..aeafcc71cb08 100644
--- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c
+++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
@@ -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))
diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c
index 5927d1ea20e2..cff0ecb0a805 100644
--- a/tools/perf/tests/code-reading.c
+++ b/tools/perf/tests/code-reading.c
@@ -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"
diff --git a/tools/perf/tests/hists_cumulate.c b/tools/perf/tests/hists_cumulate.c
index 3eb9ef8d7ec6..c0931f639fb0 100644
--- a/tools/perf/tests/hists_cumulate.c
+++ b/tools/perf/tests/hists_cumulate.c
@@ -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"
diff --git a/tools/perf/tests/hists_filter.c b/tools/perf/tests/hists_filter.c
index 1cebd20cc91c..24f98c874ba6 100644
--- a/tools/perf/tests/hists_filter.c
+++ b/tools/perf/tests/hists_filter.c
@@ -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"
diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c
index 996f5f0b3bd1..04197fcdfa9b 100644
--- a/tools/perf/tests/hists_link.c
+++ b/tools/perf/tests/hists_link.c
@@ -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"
diff --git a/tools/perf/tests/hists_output.c b/tools/perf/tests/hists_output.c
index ee5ec8bda60e..5e121f2181de 100644
--- a/tools/perf/tests/hists_output.c
+++ b/tools/perf/tests/hists_output.c
@@ -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"
diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-thread-lookup.c
index 0c5619c6e6e9..f39dc0b28810 100644
--- a/tools/perf/tests/mmap-thread-lookup.c
+++ b/tools/perf/tests/mmap-thread-lookup.c
@@ -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"
diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c
index 07cf9c334be0..85ab4330da0b 100644
--- a/tools/perf/util/annotate-data.c
+++ b/tools/perf/util/annotate-data.c
@@ -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"
diff --git a/tools/perf/util/auxtrace.h b/tools/perf/util/auxtrace.h
index eee2c11f7666..a07e9b6ab24d 100644
--- a/tools/perf/util/auxtrace.h
+++ b/tools/perf/util/auxtrace.h
@@ -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>
diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c
index 649392bee7ed..15e42e9fb2e8 100644
--- a/tools/perf/util/block-info.c
+++ b/tools/perf/util/block-info.c
@@ -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 {
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index fdb35133fde4..9e1f8d5f6d51 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -7,38 +7,40 @@
* Copyright (C) 2009, 2010 Red Hat Inc.
* Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
*/
-#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;
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index d7b7eef740b9..9c295b38d1e1 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -21,6 +21,7 @@
#include "asm/bug.h"
+#include "addr_location.h"
#include "debug.h"
#include "dso.h"
#include "event.h"
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 25d56e0f1c07..cb499a81625a 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -17,6 +17,7 @@
#include <stdlib.h>
+#include "addr_location.h"
#include "auxtrace.h"
#include "color.h"
#include "cs-etm.h"
diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
index 9dc1e184cf3c..aec1302e599b 100644
--- a/tools/perf/util/data-convert-json.c
+++ b/tools/perf/util/data-convert-json.c
@@ -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>
diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c
index 8f52e8cefcf3..5c23b1b2eafb 100644
--- a/tools/perf/util/db-export.c
+++ b/tools/perf/util/db-export.c
@@ -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)
diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
index c0afcbd954f8..70e322de711f 100644
--- a/tools/perf/util/dlfilter.c
+++ b/tools/perf/util/dlfilter.c
@@ -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)
{
diff --git a/tools/perf/util/dlfilter.h b/tools/perf/util/dlfilter.h
index cc4bb9657d05..522d8e707db0 100644
--- a/tools/perf/util/dlfilter.h
+++ b/tools/perf/util/dlfilter.h
@@ -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;
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index fcf44149feb2..f4a3f76edad2 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -1,42 +1,45 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
-#include <linux/compiler.h>
-#include <linux/kernel.h>
-#include <linux/types.h>
-#include <perf/cpumap.h>
-#include <perf/event.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
-#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
-#include <linux/perf_event.h>
-#include <linux/zalloc.h>
+
+#include "addr_location.h"
+#include "bpf-event.h"
#include "cpumap.h"
+#include "debug.h"
#include "dso.h"
#include "event.h"
-#include "debug.h"
#include "hist.h"
#include "machine.h"
+#include "map.h"
+#include "print_binary.h"
+#include "session.h"
#include "sort.h"
+#include "stat.h"
#include "string2.h"
#include "strlist.h"
+#include "symbol.h"
+#include "symbol/kallsyms.h"
#include "thread.h"
#include "thread_map.h"
#include "time-utils.h"
-#include <linux/ctype.h>
-#include "map.h"
-#include "util/namespaces.h"
-#include "symbol.h"
-#include "symbol/kallsyms.h"
-#include "asm/bug.h"
-#include "stat.h"
-#include "session.h"
-#include "bpf-event.h"
-#include "print_binary.h"
#include "tool.h"
#include "util.h"
+#include "namespaces.h"
+
+#include <asm/bug.h>
+#include <linux/compiler.h>
+#include <linux/ctype.h>
+#include <linux/kernel.h>
+#include <linux/perf_event.h>
+#include <linux/types.h>
+#include <linux/zalloc.h>
+#include <perf/cpumap.h>
+#include <perf/event.h>
+#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
static const char *perf_event__names[] = {
[0] = "TOTAL",
diff --git a/tools/perf/util/evsel_fprintf.c b/tools/perf/util/evsel_fprintf.c
index 103984b29b1e..abdd9985c4e3 100644
--- a/tools/perf/util/evsel_fprintf.c
+++ b/tools/perf/util/evsel_fprintf.c
@@ -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>
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 64ff427040c3..bd71e48c1183 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -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>
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index fc9eec8b54b8..244749c89770 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -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"
diff --git a/tools/perf/util/kvm-stat.h b/tools/perf/util/kvm-stat.h
index a356b839c2ee..7b63b1ba1b8a 100644
--- a/tools/perf/util/kvm-stat.h
+++ b/tools/perf/util/kvm-stat.h
@@ -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>
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index b5dd42588c91..db8052443259 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -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)
{
diff --git a/tools/perf/util/print_insn.c b/tools/perf/util/print_insn.c
index 02e6fbb8ca04..07f9c9f6e244 100644
--- a/tools/perf/util/print_insn.c
+++ b/tools/perf/util/print_insn.c
@@ -5,18 +5,20 @@
* Author(s): Changbin Du <changbin.du@huawei.com>
*/
#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)
{
diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/perf/util/scripting-engines/trace-event-perl.c
index e261a57b87d4..9116ffa252b4 100644
--- a/tools/perf/util/scripting-engines/trace-event-perl.c
+++ b/tools/perf/util/scripting-engines/trace-event-perl.c
@@ -35,6 +35,7 @@
#include <EXTERN.h>
#include <perl.h>
+#include "../addr_location.h"
#include "../callchain.h"
#include "../dso.h"
#include "../machine.h"
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 6655c0bbe0d8..aacb4dc3cb90 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -34,6 +34,7 @@
#include <event-parse.h>
#endif
+#include "../addr_location.h"
#include "../build-id.h"
#include "../counts.h"
#include "../debug.h"
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 393a528326d5..2b2bbb5914b9 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -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;
diff --git a/tools/perf/util/symbol_fprintf.c b/tools/perf/util/symbol_fprintf.c
index 53e1af4ed9ac..59aa97c0a997 100644
--- a/tools/perf/util/symbol_fprintf.c
+++ b/tools/perf/util/symbol_fprintf.c
@@ -3,6 +3,7 @@
#include <inttypes.h>
#include <stdio.h>
+#include "addr_location.h"
#include "dso.h"
#include "map.h"
#include "symbol.h"
diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c
index c6a0a27b12c2..0d0cbc1250e9 100644
--- a/tools/perf/util/thread-stack.c
+++ b/tools/perf/util/thread-stack.c
@@ -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
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index aa9c58bbf9d3..bf051074d66c 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -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>
diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c
index ae70fb56a057..552cffb4bb92 100644
--- a/tools/perf/util/unwind-libdw.c
+++ b/tools/perf/util/unwind-libdw.c
@@ -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;
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 05/10] perf symbol: Move dso__load_bfd_symbols out of symbol.h
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
` (3 preceding siblings ...)
2025-12-01 20:55 ` [PATCH v2 04/10] perf symbol: Remove unused includes Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 06/10] perf symbol: Use fallbacks with filename__read_build_id Ian Rogers
` (4 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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(-)
diff --git a/tools/perf/tests/pe-file-parsing.c b/tools/perf/tests/pe-file-parsing.c
index 30c7da79e109..e97c7662664e 100644
--- a/tools/perf/tests/pe-file-parsing.c
+++ b/tools/perf/tests/pe-file-parsing.c
@@ -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"
diff --git a/tools/perf/util/libbfd.h b/tools/perf/util/libbfd.h
index 953886f3d62f..086c5f12cc34 100644
--- a/tools/perf/util/libbfd.h
+++ b/tools/perf/util/libbfd.h
@@ -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)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index d8fc5ea77f84..f8c6a37464ca 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -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>
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 2b2bbb5914b9..e265e1f028be 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -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);
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 06/10] perf symbol: Use fallbacks with filename__read_build_id
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
` (4 preceding siblings ...)
2025-12-01 20:55 ` [PATCH v2 05/10] perf symbol: Move dso__load_bfd_symbols out of symbol.h Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 07/10] perf symbol: Use fallbacks for filename__read_debuglink Ian Rogers
` (3 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index dcac6f1e1d99..c40053db2212 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -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
diff --git a/tools/perf/util/libbfd.c b/tools/perf/util/libbfd.c
index cc0c474cbfaa..c438fbd03b24 100644
--- a/tools/perf/util/libbfd.c
+++ b/tools/perf/util/libbfd.c
@@ -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;
diff --git a/tools/perf/util/libbfd.h b/tools/perf/util/libbfd.h
index 086c5f12cc34..72f1ad9a7578 100644
--- a/tools/perf/util/libbfd.h
+++ b/tools/perf/util/libbfd.h
@@ -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;
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index abd55bd7f14b..c7fa08a177d0 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -3,6 +3,10 @@
#include <stddef.h>
#include <string.h>
+#include <unistd.h>
+
+#include "build-id.h"
+#include "debug.h"
Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
GElf_Shdr *shp, const char *name, size_t *idx)
@@ -30,3 +34,112 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
return NULL;
}
+int __libelf__read_build_id(Elf *elf, void *bf, size_t size)
+{
+ int err = -1;
+ GElf_Ehdr ehdr;
+ GElf_Shdr shdr;
+ Elf_Data *data;
+ Elf_Scn *sec;
+ Elf_Kind ek;
+ void *ptr;
+
+ if (size < BUILD_ID_SIZE)
+ goto out;
+
+ ek = elf_kind(elf);
+ if (ek != ELF_K_ELF)
+ goto out;
+
+ if (gelf_getehdr(elf, &ehdr) == NULL) {
+ pr_err("%s: cannot get elf header.\n", __func__);
+ goto out;
+ }
+
+ /*
+ * Check following sections for notes:
+ * '.note.gnu.build-id'
+ * '.notes'
+ * '.note' (VDSO specific)
+ */
+ do {
+ sec = elf_section_by_name(elf, &ehdr, &shdr,
+ ".note.gnu.build-id", NULL);
+ if (sec)
+ break;
+
+ sec = elf_section_by_name(elf, &ehdr, &shdr,
+ ".notes", NULL);
+ if (sec)
+ break;
+
+ sec = elf_section_by_name(elf, &ehdr, &shdr,
+ ".note", NULL);
+ if (sec)
+ break;
+
+ return err;
+
+ } while (0);
+
+ data = elf_getdata(sec, NULL);
+ if (data == NULL)
+ goto out;
+
+ ptr = data->d_buf;
+ while (ptr < (data->d_buf + data->d_size)) {
+ GElf_Nhdr *nhdr = ptr;
+ size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
+ descsz = NOTE_ALIGN(nhdr->n_descsz);
+ const char *name;
+
+ ptr += sizeof(*nhdr);
+ name = ptr;
+ ptr += namesz;
+ if (nhdr->n_type == NT_GNU_BUILD_ID &&
+ nhdr->n_namesz == sizeof("GNU")) {
+ if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
+ size_t sz = min(size, descsz);
+
+ memcpy(bf, ptr, sz);
+ memset(bf + sz, 0, size - sz);
+ err = sz;
+ break;
+ }
+ }
+ ptr += descsz;
+ }
+
+out:
+ return err;
+}
+
+int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid)
+{
+ size_t size = sizeof(bid->data);
+ int fd, err = -1;
+ Elf *elf;
+
+ if (size < BUILD_ID_SIZE)
+ goto out;
+
+ fd = dup(_fd);
+ 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;
+ }
+
+ err = __libelf__read_build_id(elf, bid->data, size);
+ if (err > 0)
+ bid->size = err;
+
+ elf_end(elf);
+out_close:
+ close(fd);
+out:
+ return err;
+}
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 5d7d18daf5ff..931597493b1f 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -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 */
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 72632bf9cc30..8fb7bf743ca6 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -751,159 +751,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
return 0;
}
-/*
- * Align offset to 4 bytes as needed for note name and descriptor data.
- */
-#define NOTE_ALIGN(n) (((n) + 3) & -4U)
-
-static int elf_read_build_id(Elf *elf, void *bf, size_t size)
-{
- int err = -1;
- GElf_Ehdr ehdr;
- GElf_Shdr shdr;
- Elf_Data *data;
- Elf_Scn *sec;
- Elf_Kind ek;
- void *ptr;
-
- if (size < BUILD_ID_SIZE)
- goto out;
-
- ek = elf_kind(elf);
- if (ek != ELF_K_ELF)
- goto out;
-
- if (gelf_getehdr(elf, &ehdr) == NULL) {
- pr_err("%s: cannot get elf header.\n", __func__);
- goto out;
- }
-
- /*
- * Check following sections for notes:
- * '.note.gnu.build-id'
- * '.notes'
- * '.note' (VDSO specific)
- */
- do {
- sec = elf_section_by_name(elf, &ehdr, &shdr,
- ".note.gnu.build-id", NULL);
- if (sec)
- break;
-
- sec = elf_section_by_name(elf, &ehdr, &shdr,
- ".notes", NULL);
- if (sec)
- break;
-
- sec = elf_section_by_name(elf, &ehdr, &shdr,
- ".note", NULL);
- if (sec)
- break;
-
- return err;
-
- } while (0);
-
- data = elf_getdata(sec, NULL);
- if (data == NULL)
- goto out;
-
- ptr = data->d_buf;
- while (ptr < (data->d_buf + data->d_size)) {
- GElf_Nhdr *nhdr = ptr;
- size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
- descsz = NOTE_ALIGN(nhdr->n_descsz);
- const char *name;
-
- ptr += sizeof(*nhdr);
- name = ptr;
- ptr += namesz;
- if (nhdr->n_type == NT_GNU_BUILD_ID &&
- nhdr->n_namesz == sizeof("GNU")) {
- if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
- size_t sz = min(size, descsz);
- memcpy(bf, ptr, sz);
- memset(bf + sz, 0, size - sz);
- err = sz;
- break;
- }
- }
- ptr += descsz;
- }
-
-out:
- return err;
-}
-
-static int read_build_id(const char *filename, struct build_id *bid)
-{
- size_t size = sizeof(bid->data);
- int fd, err;
- Elf *elf;
-
- err = libbfd__read_build_id(filename, bid);
- if (err >= 0)
- goto out;
-
- if (size < BUILD_ID_SIZE)
- goto out;
-
- 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;
- }
-
- err = elf_read_build_id(elf, bid->data, size);
- if (err > 0)
- bid->size = err;
-
- elf_end(elf);
-out_close:
- close(fd);
-out:
- return err;
-}
-
-int filename__read_build_id(const char *filename, struct build_id *bid)
-{
- struct kmod_path m = { .name = NULL, };
- char path[PATH_MAX];
- int err;
-
- 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;
-
- 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;
- }
- close(fd);
- filename = path;
- }
-
- err = read_build_id(filename, bid);
-
- if (m.comp)
- unlink(filename);
- return err;
-}
-
int sysfs__read_build_id(const char *filename, struct build_id *bid)
{
size_t size = sizeof(bid->data);
@@ -1171,7 +1018,7 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
struct build_id bid;
int size;
- size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
+ size = __libelf__read_build_id(elf, build_id, BUILD_ID_SIZE);
if (size <= 0) {
*dso__load_errno(dso) = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
goto out_elf_end;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index c6b17c14a2e9..b71cf8caf282 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -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
diff --git a/tools/perf/util/symbol-minimal.h b/tools/perf/util/symbol-minimal.h
new file mode 100644
index 000000000000..185d98968212
--- /dev/null
+++ b/tools/perf/util/symbol-minimal.h
@@ -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 */
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index f8c6a37464ca..8d461974a69f 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -35,6 +35,7 @@
#include "path.h"
#include "perf-libelf.h"
#include "strlist.h"
+#include "symbol-minimal.h"
#include "symbol.h"
#include "symsrc.h"
#include "util.h" // lsdir()
@@ -1998,6 +1999,57 @@ static bool filename__readable(const char *file)
return true;
}
+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;
+}
+
static char *dso__find_kallsyms(struct dso *dso, struct map *map)
{
struct build_id bid = { .size = 0, };
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 07/10] perf symbol: Use fallbacks for filename__read_debuglink
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
` (5 preceding siblings ...)
2025-12-01 20:55 ` [PATCH v2 06/10] perf symbol: Use fallbacks with filename__read_build_id Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 08/10] perf symbol: Make a common sysfs__read_build_id Ian Rogers
` (2 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
Try libbfd and then libelf in symbol.c. Remove the non-existent
symbol-minimal version.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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(-)
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index c7fa08a177d0..a8a8c39056da 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -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;
+}
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 931597493b1f..167679f9fc9b 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -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 */
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 8fb7bf743ca6..9ceb746b6c59 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -801,62 +801,6 @@ int sysfs__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 fd, err = -1;
- Elf *elf;
- GElf_Ehdr ehdr;
- GElf_Shdr shdr;
- Elf_Data *data;
- Elf_Scn *sec;
- Elf_Kind ek;
-
- err = libbfd_filename__read_debuglink(filename, debuglink, size);
- if (err >= 0)
- goto out;
-
- 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;
-}
-
bool symsrc__possibly_runtime(struct symsrc *ss)
{
return ss->dynsym || ss->opdsec;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index b71cf8caf282..445307f230af 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -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
*/
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 8d461974a69f..da75a1c159f2 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -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, };
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 08/10] perf symbol: Make a common sysfs__read_build_id
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
` (6 preceding siblings ...)
2025-12-01 20:55 ` [PATCH v2 07/10] perf symbol: Use fallbacks for filename__read_debuglink Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 09/10] perf dso: Move type helpers out of symbol and use fallbacks Ian Rogers
2025-12-01 20:55 ` [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id Ian Rogers
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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(-)
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index a8a8c39056da..4198f4dfad9f 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -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)
{
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 167679f9fc9b..2118325c04e5 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -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);
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 9ceb746b6c59..cb890de31044 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -751,56 +751,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
return 0;
}
-int sysfs__read_build_id(const char *filename, struct build_id *bid)
-{
- size_t size = sizeof(bid->data);
- int fd, err = -1;
-
- fd = open(filename, O_RDONLY);
- if (fd < 0)
- goto out;
-
- while (1) {
- char bf[BUFSIZ];
- GElf_Nhdr nhdr;
- size_t namesz, descsz;
-
- if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
- break;
-
- namesz = NOTE_ALIGN(nhdr.n_namesz);
- descsz = NOTE_ALIGN(nhdr.n_descsz);
- if (nhdr.n_type == NT_GNU_BUILD_ID &&
- nhdr.n_namesz == sizeof("GNU")) {
- if (read(fd, bf, namesz) != (ssize_t)namesz)
- break;
- if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
- size_t sz = min(descsz, size);
- if (read(fd, bid->data, sz) == (ssize_t)sz) {
- memset(bid->data + sz, 0, size - sz);
- bid->size = sz;
- err = 0;
- break;
- }
- } else if (read(fd, bf, descsz) != (ssize_t)descsz)
- break;
- } else {
- int n = namesz + descsz;
-
- if (n > (int)sizeof(bf)) {
- n = sizeof(bf);
- pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
- __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
- }
- if (read(fd, bf, n) != n)
- break;
- }
- }
- close(fd);
-out:
- return err;
-}
-
bool symsrc__possibly_runtime(struct symsrc *ss)
{
return ss->dynsym || ss->opdsec;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index 445307f230af..0e3a27dae9d6 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -77,6 +77,58 @@ static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
return -1;
}
+static int read_build_id_fd(int fd, struct build_id *bid, bool need_swap)
+{
+ size_t size = sizeof(bid->data);
+ struct {
+ u32 n_namesz;
+ u32 n_descsz;
+ u32 n_type;
+ } nhdr;
+
+ while (true) {
+ size_t namesz, descsz, n;
+ ssize_t err;
+
+ err = read(fd, &nhdr, sizeof(nhdr));
+ if (err != sizeof(nhdr))
+ return err == -1 ? -errno : -EIO;
+
+ if (need_swap) {
+ nhdr.n_namesz = bswap_32(nhdr.n_namesz);
+ nhdr.n_descsz = bswap_32(nhdr.n_descsz);
+ nhdr.n_type = bswap_32(nhdr.n_type);
+ }
+ namesz = NOTE_ALIGN(nhdr.n_namesz);
+ descsz = NOTE_ALIGN(nhdr.n_descsz);
+ n = namesz + descsz; /* Remaining bytes of this note. */
+
+ if (nhdr.n_type == NT_GNU_BUILD_ID && nhdr.n_namesz == sizeof("GNU")) {
+ char name[NOTE_ALIGN(sizeof("GNU"))];
+
+ err = read(fd, name, sizeof(name));
+ if (err != sizeof(name))
+ return err == -1 ? -errno : -EIO;
+
+ n = descsz;
+ if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
+ /* Successfully found build ID. */
+ ssize_t sz = min(descsz, size);
+
+ err = read(fd, bid->data, sz);
+ if (err != sz)
+ return err == -1 ? -errno : -EIO;
+
+ memset(bid->data + sz, 0, size - sz);
+ bid->size = sz;
+ return 0;
+ }
+ }
+ if (lseek(fd, n, SEEK_CUR) == -1)
+ return -errno;
+ }
+}
+
/*
* Just try PT_NOTE header otherwise fails
*/
@@ -194,38 +246,20 @@ int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid)
return ret;
}
-#ifndef HAVE_LIBELF_SUPPORT
-int sysfs__read_build_id(const char *filename, struct build_id *bid)
+int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bid)
{
- int fd;
- int ret = -1;
- struct stat stbuf;
- size_t buf_size;
- void *buf;
+ int fd, ret;
fd = open(filename, O_RDONLY);
if (fd < 0)
- return -1;
-
- if (fstat(fd, &stbuf) < 0)
- goto out;
-
- buf_size = stbuf.st_size;
- buf = malloc(buf_size);
- if (buf == NULL)
- goto out;
-
- if (read(fd, buf, buf_size) != (ssize_t) buf_size)
- goto out_free;
+ return -errno;
- ret = read_build_id(buf, buf_size, bid, false);
-out_free:
- free(buf);
-out:
+ ret = read_build_id_fd(fd, bid, /*need_swap=*/false);
close(fd);
return ret;
}
+#ifndef HAVE_LIBELF_SUPPORT
int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
enum dso_binary_type type)
{
diff --git a/tools/perf/util/symbol-minimal.h b/tools/perf/util/symbol-minimal.h
index 185d98968212..5cce1f1f0f16 100644
--- a/tools/perf/util/symbol-minimal.h
+++ b/tools/perf/util/symbol-minimal.h
@@ -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 */
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index da75a1c159f2..76dc5b70350a 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -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);
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 09/10] perf dso: Move type helpers out of symbol and use fallbacks
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
` (7 preceding siblings ...)
2025-12-01 20:55 ` [PATCH v2 08/10] perf symbol: Make a common sysfs__read_build_id Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-01 20:55 ` [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id Ian Rogers
9 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
Fallback between libelf and symbol-minimal versions. Remove the
symbol.c code and just directly inline into the only caller in dso.c.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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(-)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 344e689567ee..421fa4d27d2a 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -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);
}
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index 4198f4dfad9f..eed5c0317f8b 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -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;
+}
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 2118325c04e5..5ca5641b9dc9 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -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 */
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index cb890de31044..f483aa67f69b 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -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;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index 0e3a27dae9d6..f9724448fb64 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -259,6 +259,44 @@ int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bid)
return ret;
}
+static int fd__is_64_bit(int fd)
+{
+ u8 e_ident[EI_NIDENT];
+
+ if (lseek(fd, 0, SEEK_SET))
+ return -1;
+
+ if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
+ return -1;
+
+ if (memcmp(e_ident, ELFMAG, SELFMAG) ||
+ e_ident[EI_VERSION] != EV_CURRENT)
+ return -1;
+
+ return e_ident[EI_CLASS] == ELFCLASS64;
+}
+
+enum dso_type sym_min_dso__type_fd(int fd)
+{
+ Elf64_Ehdr ehdr;
+ int ret;
+
+ ret = fd__is_64_bit(fd);
+ if (ret < 0)
+ return DSO__TYPE_UNKNOWN;
+
+ if (ret)
+ return DSO__TYPE_64BIT;
+
+ if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
+ return DSO__TYPE_UNKNOWN;
+
+ if (ehdr.e_machine == EM_X86_64)
+ return DSO__TYPE_X32BIT;
+
+ return DSO__TYPE_32BIT;
+}
+
#ifndef HAVE_LIBELF_SUPPORT
int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
enum dso_binary_type type)
@@ -305,44 +343,6 @@ int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
return 0;
}
-static int fd__is_64_bit(int fd)
-{
- u8 e_ident[EI_NIDENT];
-
- if (lseek(fd, 0, SEEK_SET))
- return -1;
-
- if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
- return -1;
-
- if (memcmp(e_ident, ELFMAG, SELFMAG) ||
- e_ident[EI_VERSION] != EV_CURRENT)
- return -1;
-
- return e_ident[EI_CLASS] == ELFCLASS64;
-}
-
-enum dso_type dso__type_fd(int fd)
-{
- Elf64_Ehdr ehdr;
- int ret;
-
- ret = fd__is_64_bit(fd);
- if (ret < 0)
- return DSO__TYPE_UNKNOWN;
-
- if (ret)
- return DSO__TYPE_64BIT;
-
- if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
- return DSO__TYPE_UNKNOWN;
-
- if (ehdr.e_machine == EM_X86_64)
- return DSO__TYPE_X32BIT;
-
- return DSO__TYPE_32BIT;
-}
-
int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
struct symsrc *ss,
struct symsrc *runtime_ss __maybe_unused,
diff --git a/tools/perf/util/symbol-minimal.h b/tools/perf/util/symbol-minimal.h
index 5cce1f1f0f16..282466aca7d9 100644
--- a/tools/perf/util/symbol-minimal.h
+++ b/tools/perf/util/symbol-minimal.h
@@ -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 */
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index e265e1f028be..4cc557981724 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -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,
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
` (8 preceding siblings ...)
2025-12-01 20:55 ` [PATCH v2 09/10] perf dso: Move type helpers out of symbol and use fallbacks Ian Rogers
@ 2025-12-01 20:55 ` Ian Rogers
2025-12-05 19:40 ` Ian Rogers
9 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2025-12-01 20:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
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")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-buildid-cache.c | 6 ++++--
tools/perf/util/symbol.c | 3 ++-
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
index c98104481c8a..539e779e3268 100644
--- a/tools/perf/builtin-buildid-cache.c
+++ b/tools/perf/builtin-buildid-cache.c
@@ -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",
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 76dc5b70350a..f43e30019e21 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -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)
--
2.52.0.158.g65b55ccf14-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id
2025-12-01 20:55 ` [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id Ian Rogers
@ 2025-12-05 19:40 ` Ian Rogers
2025-12-05 21:18 ` Namhyung Kim
0 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2025-12-05 19:40 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
Stephen Brennan, Andi Kleen, Chun-Tse Shao, Thomas Falcon,
Dapeng Mi, Dr. David Alan Gilbert, Christophe Leroy,
Krzysztof Łopatowski, Masami Hiramatsu (Google),
Alexandre Ghiti, Haibo Xu, Sergei Trofimovich, linux-kernel,
linux-perf-users
On Mon, Dec 1, 2025 at 12:55 PM Ian Rogers <irogers@google.com> wrote:
>
> 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.
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> tools/perf/builtin-buildid-cache.c | 6 ++++--
> tools/perf/util/symbol.c | 3 ++-
> 2 files changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
> index c98104481c8a..539e779e3268 100644
> --- a/tools/perf/builtin-buildid-cache.c
> +++ b/tools/perf/builtin-buildid-cache.c
> @@ -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.
> - 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",
> diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
> index 76dc5b70350a..f43e30019e21 100644
> --- a/tools/perf/util/symbol.c
> +++ b/tools/perf/util/symbol.c
> @@ -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
>
> err = kmod_path__parse(&m, filename);
> if (err)
> --
> 2.52.0.158.g65b55ccf14-goog
>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id
2025-12-05 19:40 ` Ian Rogers
@ 2025-12-05 21:18 ` Namhyung Kim
2025-12-07 2:28 ` Ian Rogers
0 siblings, 1 reply; 14+ messages in thread
From: Namhyung Kim @ 2025-12-05 21:18 UTC (permalink / raw)
To: Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Alexander Shishkin, Jiri Olsa, Adrian Hunter, Suzuki K Poulose,
Mike Leach, James Clark, John Garry, Will Deacon, Leo Yan,
Athira Rajeev, tanze, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
Christophe Leroy, Krzysztof Łopatowski,
Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
Sergei Trofimovich, linux-kernel, linux-perf-users
Hi Ian,
On Fri, Dec 05, 2025 at 11:40:12AM -0800, Ian Rogers wrote:
> On Mon, Dec 1, 2025 at 12:55 PM Ian Rogers <irogers@google.com> wrote:
> >
> > 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.
Sure.
>
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> > tools/perf/builtin-buildid-cache.c | 6 ++++--
> > tools/perf/util/symbol.c | 3 ++-
> > 2 files changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
> > index c98104481c8a..539e779e3268 100644
> > --- a/tools/perf/builtin-buildid-cache.c
> > +++ b/tools/perf/builtin-buildid-cache.c
> > @@ -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.
>
> > - 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",
> > diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
> > index 76dc5b70350a..f43e30019e21 100644
> > --- a/tools/perf/util/symbol.c
> > +++ b/tools/perf/util/symbol.c
> > @@ -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.
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
>
> >
> > err = kmod_path__parse(&m, filename);
> > if (err)
> > --
> > 2.52.0.158.g65b55ccf14-goog
> >
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id
2025-12-05 21:18 ` Namhyung Kim
@ 2025-12-07 2:28 ` Ian Rogers
0 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2025-12-07 2:28 UTC (permalink / raw)
To: Namhyung Kim
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Alexander Shishkin, Jiri Olsa, Adrian Hunter, Suzuki K Poulose,
Mike Leach, James Clark, John Garry, Will Deacon, Leo Yan,
Athira Rajeev, tanze, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
Christophe Leroy, Krzysztof Łopatowski,
Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
Sergei Trofimovich, linux-kernel, linux-perf-users
On Fri, Dec 5, 2025 at 1:18 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> Hi Ian,
>
> On Fri, Dec 05, 2025 at 11:40:12AM -0800, Ian Rogers wrote:
> > On Mon, Dec 1, 2025 at 12:55 PM Ian Rogers <irogers@google.com> wrote:
> > >
> > > 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.
>
> Sure.
>
> >
> > > Signed-off-by: Ian Rogers <irogers@google.com>
> > > ---
> > > tools/perf/builtin-buildid-cache.c | 6 ++++--
> > > tools/perf/util/symbol.c | 3 ++-
> > > 2 files changed, 6 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
> > > index c98104481c8a..539e779e3268 100644
> > > --- a/tools/perf/builtin-buildid-cache.c
> > > +++ b/tools/perf/builtin-buildid-cache.c
> > > @@ -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.
> >
> > > - 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",
> > > diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
> > > index 76dc5b70350a..f43e30019e21 100644
> > > --- a/tools/perf/util/symbol.c
> > > +++ b/tools/perf/util/symbol.c
> > > @@ -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.
>
> 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/20251207022345.909535-1-irogers@google.com/
Thanks,
Ian
> Thanks,
> Namhyung
>
>
> >
> > >
> > > err = kmod_path__parse(&m, filename);
> > > if (err)
> > > --
> > > 2.52.0.158.g65b55ccf14-goog
> > >
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2025-12-07 2:28 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
2025-12-01 20:55 ` [PATCH v2 01/10] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
2025-12-01 20:55 ` [PATCH v2 02/10] perf symbol: Reduce scope of arch__sym_update Ian Rogers
2025-12-01 20:55 ` [PATCH v2 03/10] perf symbol: Move libelf code to its own file/header Ian Rogers
2025-12-01 20:55 ` [PATCH v2 04/10] perf symbol: Remove unused includes Ian Rogers
2025-12-01 20:55 ` [PATCH v2 05/10] perf symbol: Move dso__load_bfd_symbols out of symbol.h Ian Rogers
2025-12-01 20:55 ` [PATCH v2 06/10] perf symbol: Use fallbacks with filename__read_build_id Ian Rogers
2025-12-01 20:55 ` [PATCH v2 07/10] perf symbol: Use fallbacks for filename__read_debuglink Ian Rogers
2025-12-01 20:55 ` [PATCH v2 08/10] perf symbol: Make a common sysfs__read_build_id Ian Rogers
2025-12-01 20:55 ` [PATCH v2 09/10] perf dso: Move type helpers out of symbol and use fallbacks Ian Rogers
2025-12-01 20:55 ` [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id Ian Rogers
2025-12-05 19:40 ` Ian Rogers
2025-12-05 21:18 ` Namhyung Kim
2025-12-07 2:28 ` Ian Rogers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).