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>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Leo Yan <leo.yan@linux.dev>, Dmitry Vyukov <dvyukov@google.com>,
"Steinar H . Gunderson" <sesse@google.com>,
Stephen Brennan <stephen.s.brennan@oracle.com>,
Thomas Falcon <thomas.falcon@intel.com>,
linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v4 13/19] perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id()
Date: Tue, 24 Nov 2026 16:51:02 -0800 [thread overview]
Message-ID: <20261125005108.1020003-14-irogers@google.com> (raw)
In-Reply-To: <20261125005108.1020003-1-irogers@google.com>
Add helpers to look up a DSO in struct dsos by its build ID and to find
(or lazily populate from machine->dsos or the build-id cache) a struct
map in struct maps matching a given build ID. This prepares for
resolving PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET samples without virtual-address
MMAP records.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dsos.c | 68 +++++++++++++++++++++++++++++++++++++++
tools/perf/util/dsos.h | 6 ++++
tools/perf/util/maps.c | 73 ++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/maps.h | 2 ++
4 files changed, 149 insertions(+)
diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index 7a56577f4ec8..5dca53c5b258 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -304,6 +304,40 @@ struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
return res;
}
+struct dsos__find_build_id_cb_args {
+ const struct build_id *bid;
+ enum dso_space_type dso_space;
+ struct dso *res;
+};
+
+static int dsos__find_build_id_cb(struct dso *dso, void *data)
+{
+ struct dsos__find_build_id_cb_args *args = data;
+
+ if (dso__kernel(dso) == args->dso_space &&
+ dso__has_build_id(dso) && dso__build_id_equal(dso, args->bid)) {
+ args->res = dso__get(dso);
+ return 1;
+ }
+ return 0;
+}
+
+struct dso *dsos__find_by_build_id(struct dsos *dsos, const struct build_id *bid,
+ int dso_space)
+{
+ struct dsos__find_build_id_cb_args args = {
+ .bid = bid,
+ .dso_space = dso_space,
+ .res = NULL,
+ };
+
+ if (!build_id__is_defined(bid))
+ return NULL;
+
+ dsos__for_each_dso(dsos, dsos__find_build_id_cb, &args);
+ return args.res;
+}
+
static void dso__set_basename(struct dso *dso)
{
bool allocated = false;
@@ -370,6 +404,40 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct d
return dso;
}
+struct dso *dsos__findnew_id_space(struct dsos *dsos, struct machine *machine,
+ const char *name, const struct dso_id *id,
+ int dso_space)
+{
+ enum dso_space_type space = dso_space;
+ struct dso *dso;
+
+ down_write(&dsos->lock);
+ dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/true);
+ if (dso) {
+ if (dso__kernel(dso) == space)
+ __dso__improve_id(dso, id);
+ else
+ dso__zput(dso);
+ } else {
+ dso = dso__new_id(name, id);
+ if (dso) {
+ dso__set_basename(dso);
+ if (space != DSO_SPACE__USER) {
+ struct kmod_path m = { .name = NULL };
+
+ if (!kmod_path__parse_name(&m, name) && m.kmod)
+ dso__set_module_info(dso, &m, machine);
+ dso__set_kernel(dso, space);
+ free(m.name);
+ }
+ if (__dsos__add(dsos, dso))
+ dso__zput(dso);
+ }
+ }
+ up_write(&dsos->lock);
+ return dso;
+}
+
struct dsos__fprintf_buildid_cb_args {
FILE *fp;
bool (*skip)(struct dso *dso, int parm);
diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h
index cc7e3a42f9cb..16b93cd5105f 100644
--- a/tools/perf/util/dsos.h
+++ b/tools/perf/util/dsos.h
@@ -8,6 +8,7 @@
#include <linux/rbtree.h>
#include "rwsem.h"
+struct build_id;
struct dso;
struct dso_id;
struct kmod_path;
@@ -33,8 +34,13 @@ int dsos__add(struct dsos *dsos, struct dso *dso);
struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked)
__must_hold_shared(&dsos->lock);
struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
+struct dso *dsos__find_by_build_id(struct dsos *dsos, const struct build_id *bid,
+ int dso_space);
struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id);
+struct dso *dsos__findnew_id_space(struct dsos *dsos, struct machine *machine,
+ const char *name, const struct dso_id *id,
+ int dso_space);
bool dsos__read_build_ids(struct dsos *dsos, bool with_hits);
diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
index 770ce43a4ce3..c6670fccace4 100644
--- a/tools/perf/util/maps.c
+++ b/tools/perf/util/maps.c
@@ -3,8 +3,11 @@
#include <stdlib.h>
#include <string.h>
#include <linux/zalloc.h>
+#include "build-id.h"
#include "debug.h"
#include "dso.h"
+#include "dsos.h"
+#include "machine.h"
#include "map.h"
#include "maps.h"
#include "rwsem.h"
@@ -1307,6 +1310,76 @@ static int map__strcmp_name(const void *name, const void *b)
return strcmp(name, dso__short_name(dso));
}
+struct map *maps__find_by_build_id(struct maps *maps, struct build_id *bid)
+{
+ struct machine *machine = maps__machine(maps);
+ bool is_kmaps = RC_CHK_EQUAL(maps, machine__kernel_maps(machine));
+ enum dso_space_type dso_space = is_kmaps
+ ? (machine__is_host(machine) ? DSO_SPACE__KERNEL : DSO_SPACE__KERNEL_GUEST)
+ : DSO_SPACE__USER;
+ struct map *result = NULL;
+
+ if (!build_id__is_defined(bid))
+ return NULL;
+
+ down_read(maps__lock(maps));
+ for (unsigned int i = 0, n = __maps__nr_maps(maps); i < n; i++) {
+ struct map *map = maps__maps_by_address(maps)[i];
+ struct dso *dso = map__dso(map);
+
+ if (dso && dso__kernel(dso) == dso_space &&
+ dso__has_build_id(dso) &&
+ dso__build_id_equal(dso, bid)) {
+ result = map__get(map);
+ break;
+ }
+ }
+ up_read(maps__lock(maps));
+
+ if (!result) {
+ struct dso *dso = dsos__find_by_build_id(&machine->dsos, bid, dso_space);
+
+ if (!dso) {
+ char sbuild_id[SBUILD_ID_SIZE];
+ char *origname;
+
+ build_id__snprintf(bid, sbuild_id, sizeof(sbuild_id));
+ origname = build_id_cache__origname(sbuild_id);
+ if (origname) {
+ struct dso_id id = { .build_id = *bid };
+
+ dso = dsos__findnew_id_space(&machine->dsos, machine,
+ origname, &id, dso_space);
+ free(origname);
+ }
+ }
+ if (dso) {
+ down_write(maps__lock(maps));
+ for (unsigned int i = 0, n = __maps__nr_maps(maps); i < n; i++) {
+ struct map *map = maps__maps_by_address(maps)[i];
+ struct dso *map_dso = map__dso(map);
+
+ if (map_dso && dso__kernel(map_dso) == dso_space &&
+ dso__has_build_id(map_dso) &&
+ dso__build_id_equal(map_dso, bid)) {
+ result = map__get(map);
+ break;
+ }
+ }
+ if (!result) {
+ result = map__new2(0, dso);
+ if (result && __maps__insert(maps, result)) {
+ map__put(result);
+ result = NULL;
+ }
+ }
+ up_write(maps__lock(maps));
+ dso__put(dso);
+ }
+ }
+ return result;
+}
+
struct map *maps__find_by_name(struct maps *maps, const char *name)
{
struct map *result = NULL;
diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h
index 4e6eff244125..fbe5b6f47959 100644
--- a/tools/perf/util/maps.h
+++ b/tools/perf/util/maps.h
@@ -11,6 +11,7 @@ struct ref_reloc_sym;
struct machine;
struct map;
struct maps;
+struct build_id;
#define KMAP_NAME_LEN 256
@@ -76,6 +77,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams);
int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new);
struct map *maps__find_by_name(struct maps *maps, const char *name);
+struct map *maps__find_by_build_id(struct maps *maps, struct build_id *bid);
struct map *maps__find_next_entry(struct maps *maps, struct map *map);
--
2.56.1.831.g720f3f5937-goog
next prev parent reply other threads:[~2026-11-25 0:51 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 0:50 [PATCH v4 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
2026-11-25 0:50 ` [PATCH v4 01/19] perf/core: Protect per-CPU callchain buffer across bpf_overflow_handler() Ian Rogers
2026-11-25 0:50 ` [PATCH v4 02/19] perf/uapi: Factor perf_build_id out into its own top-level struct Ian Rogers
2026-11-25 13:17 ` Peter Zijlstra
2026-11-30 19:34 ` Ian Rogers
2026-11-25 0:50 ` [PATCH v4 03/19] perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI Ian Rogers
2026-11-25 13:25 ` Peter Zijlstra
2026-11-25 21:45 ` Namhyung Kim
2026-11-30 19:38 ` Ian Rogers
2026-12-01 8:14 ` Peter Zijlstra
2026-12-01 17:10 ` Ian Rogers
2026-12-01 8:10 ` Peter Zijlstra
2026-11-25 0:50 ` [PATCH v4 04/19] bpf: Export stack_map_get_build_id_offset() for perf events Ian Rogers
2026-11-25 0:50 ` [PATCH v4 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types Ian Rogers
2026-11-25 1:19 ` sashiko-bot
2026-11-25 0:50 ` [PATCH v4 06/19] perf/core: Support deferred unwinding for BUILD_ID_OFFSET " Ian Rogers
2026-11-25 0:50 ` [PATCH v4 07/19] perf inject: Set ordered_events and ordering_requires_timestamps for --aslr Ian Rogers
2026-11-25 0:50 ` [PATCH v4 08/19] perf session: Avoid double byte-swapping re-parsed deferred samples Ian Rogers
2026-11-25 1:12 ` sashiko-bot
2026-11-25 0:50 ` [PATCH v4 09/19] perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in aslr_tool__process_sample() Ian Rogers
2026-11-25 0:50 ` [PATCH v4 10/19] tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id Ian Rogers
2026-11-25 0:51 ` [PATCH v4 11/19] tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET Ian Rogers
2026-11-25 0:51 ` [PATCH v4 12/19] perf: Refactor thread map and symbol APIs to take perf_sample Ian Rogers
2026-11-25 0:51 ` Ian Rogers [this message]
2026-11-25 0:51 ` [PATCH v4 14/19] perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types Ian Rogers
2026-11-25 0:51 ` [PATCH v4 15/19] perf symbol: Resolve symbols and callchains using build ID and offset Ian Rogers
2026-11-25 0:51 ` [PATCH v4 16/19] perf inject: Add --sample-buildids to convert samples to build ID + offset Ian Rogers
2026-11-25 1:18 ` sashiko-bot
2026-11-25 0:51 ` [PATCH v4 17/19] perf record: Add --buildid-offset option Ian Rogers
2026-11-25 1:19 ` sashiko-bot
2026-11-25 0:51 ` [PATCH v4 18/19] perf test: Add BUILD_ID_OFFSET C unit tests Ian Rogers
2026-11-25 0:51 ` [PATCH v4 19/19] perf test: Add inject --sample-buildids and record --buildid-offset shell tests Ian Rogers
2026-11-25 1:19 ` [PATCH v4 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support sashiko-bot
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=20261125005108.1020003-14-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=atrajeev@linux.ibm.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dvyukov@google.com \
--cc=eddyz87@gmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=leo.yan@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=martin.lau@linux.dev \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=sesse@google.com \
--cc=song@kernel.org \
--cc=stephen.s.brennan@oracle.com \
--cc=thomas.falcon@intel.com \
--cc=yonghong.song@linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.