[PATCH v2 1/3] perf dso: Move dso__read_build_id() from perf inject to dso.c
From: Matthew Wood
Date: Fri Oct 16 2026 - 20:02:14 EST
dso__read_build_id() reads a DSO's build-id from inside its mount
namespace when it isn't known yet. The libdw unwinder is going to need
it, so move it out of builtin-inject.c and make it available to the rest
of perf.
No functional change.
Signed-off-by: Matthew Wood <thepacketgeek@xxxxxxxxx>
---
tools/perf/builtin-inject.c | 25 -------------------------
tools/perf/util/dso.c | 29 +++++++++++++++++++++++++++++
tools/perf/util/dso.h | 1 +
3 files changed, 30 insertions(+), 25 deletions(-)
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 70bbfad5653e..7e4b45a5062c 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -892,31 +892,6 @@ static int perf_event__repipe_tracing_data(const struct perf_tool *tool,
}
#endif
-static int dso__read_build_id(struct dso *dso)
-{
- struct nscookie nsc;
- struct build_id bid = { .size = 0, };
-
- if (dso__has_build_id(dso))
- return 0;
-
- mutex_lock(dso__lock(dso));
- nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
- if (filename__read_build_id(dso__long_name(dso), &bid) > 0)
- dso__set_build_id(dso, &bid);
- else if (dso__nsinfo(dso)) {
- char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso));
-
- if (new_name && filename__read_build_id(new_name, &bid) > 0)
- dso__set_build_id(dso, &bid);
- free(new_name);
- }
- nsinfo__mountns_exit(&nsc);
- mutex_unlock(dso__lock(dso));
-
- return dso__has_build_id(dso) ? 0 : -1;
-}
-
static struct strlist *perf_inject__parse_known_build_ids(
const char *known_build_ids_string)
{
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 42bfe30a3b51..e03e2cb9212b 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -569,6 +569,35 @@ char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
return filename_with_chroot(nsinfo__pid(dso__nsinfo_const(dso)), filename);
}
+/*
+ * Read the build-id from the DSO's file, inside its mount namespace, if it
+ * isn't known yet.
+ */
+int dso__read_build_id(struct dso *dso)
+{
+ struct nscookie nsc;
+ struct build_id bid = { .size = 0, };
+
+ if (dso__has_build_id(dso))
+ return 0;
+
+ mutex_lock(dso__lock(dso));
+ nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
+ if (filename__read_build_id(dso__long_name(dso), &bid) > 0)
+ dso__set_build_id(dso, &bid);
+ else if (dso__nsinfo(dso)) {
+ char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso));
+
+ if (new_name && filename__read_build_id(new_name, &bid) > 0)
+ dso__set_build_id(dso, &bid);
+ free(new_name);
+ }
+ nsinfo__mountns_exit(&nsc);
+ mutex_unlock(dso__lock(dso));
+
+ return dso__has_build_id(dso) ? 0 : -1;
+}
+
static char *dso__get_filename(struct dso *dso, const char *root_dir,
bool *decomp)
{
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 55c4aaa53c38..ece54b9b8552 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -805,6 +805,7 @@ static inline bool dso__has_symbols(const struct dso *dso)
}
char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
+int dso__read_build_id(struct dso *dso);
bool dso__sorted_by_name(const struct dso *dso);
void dso__set_sorted_by_name(struct dso *dso);
--
2.53.0-Meta