linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: "Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>,
	"Namhyung Kim" <namhyung@kernel.org>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	"Jiri Olsa" <jolsa@kernel.org>, "Ian Rogers" <irogers@google.com>,
	"Adrian Hunter" <adrian.hunter@intel.com>,
	"Suzuki K Poulose" <suzuki.poulose@arm.com>,
	"Mike Leach" <mike.leach@linaro.org>,
	"James Clark" <james.clark@linaro.org>,
	"John Garry" <john.g.garry@oracle.com>,
	"Will Deacon" <will@kernel.org>, "Leo Yan" <leo.yan@linux.dev>,
	"Athira Rajeev" <atrajeev@linux.ibm.com>,
	tanze <tanze@kylinos.cn>,
	"Stephen Brennan" <stephen.s.brennan@oracle.com>,
	"Andi Kleen" <ak@linux.intel.com>,
	"Chun-Tse Shao" <ctshao@google.com>,
	"Thomas Falcon" <thomas.falcon@intel.com>,
	"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
	"Dr. David Alan Gilbert" <linux@treblig.org>,
	"Christophe Leroy" <christophe.leroy@csgroup.eu>,
	"Krzysztof Łopatowski" <krzysztof.m.lopatowski@gmail.com>,
	"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
	"Alexandre Ghiti" <alexghiti@rivosinc.com>,
	"Haibo Xu" <haibo1.xu@intel.com>,
	"Sergei Trofimovich" <slyich@gmail.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: [PATCH v2 09/10] perf dso: Move type helpers out of symbol and use fallbacks
Date: Mon,  1 Dec 2025 12:55:08 -0800	[thread overview]
Message-ID: <20251201205509.195451-10-irogers@google.com> (raw)
In-Reply-To: <20251201205509.195451-1-irogers@google.com>

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


  parent reply	other threads:[~2025-12-01 20:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Ian Rogers [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251201205509.195451-10-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=alexghiti@rivosinc.com \
    --cc=atrajeev@linux.ibm.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=ctshao@google.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=haibo1.xu@intel.com \
    --cc=james.clark@linaro.org \
    --cc=john.g.garry@oracle.com \
    --cc=jolsa@kernel.org \
    --cc=krzysztof.m.lopatowski@gmail.com \
    --cc=leo.yan@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux@treblig.org \
    --cc=mhiramat@kernel.org \
    --cc=mike.leach@linaro.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=slyich@gmail.com \
    --cc=stephen.s.brennan@oracle.com \
    --cc=suzuki.poulose@arm.com \
    --cc=tanze@kylinos.cn \
    --cc=thomas.falcon@intel.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).