[PATCH v2 7/8] perf record: Add --buildid-offset option

From: Ian Rogers

Date: Fri Oct 02 2026 - 13:40:13 EST


Add the --buildid-offset flag to perf record to natively request
PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET
from the kernel. This configures the evsel attributes by replacing
the standard IP and CALLCHAIN bits with the build ID offset equivalents.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-record.c | 2 ++
tools/perf/util/evsel.c | 55 ++++++++++++++++++++++++++++++++++++-
tools/perf/util/record.h | 1 +
3 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index ad81458989b5..0bd051bf95e1 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -3625,6 +3625,8 @@ static struct option __record_options[] = {
"file", "vmlinux pathname"),
OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
"Record build-id of all DSOs regardless of hits"),
+ OPT_BOOLEAN(0, "buildid-offset", &record.opts.buildid_offset,
+ "Record build id offset instead of IP"),
OPT_BOOLEAN_SET(0, "buildid-mmap", &record.buildid_mmap, &record.buildid_mmap_set,
"Record build-id in mmap events and skip build-id processing."),
OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 08e9e6567bed..7517e960c58e 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1549,6 +1549,43 @@ bool evsel__is_offcpu_event(struct evsel *evsel)
evsel->core.attr.sample_type & PERF_SAMPLE_RAW;
}

+static bool evlist__has_callchain_config(struct evlist *evlist,
+ const struct callchain_param *callchain)
+{
+ struct evsel *pos;
+
+ if (callchain && callchain->enabled)
+ return true;
+ if (!evlist)
+ return false;
+
+ evlist__for_each_entry(evlist, pos) {
+ struct evsel_config_term *term;
+ const char *callgraph_buf = NULL;
+ u32 dump_size = 0;
+ int max_stack = 0;
+
+ if (evsel__has_callchain(pos))
+ return true;
+
+ list_for_each_entry(term, &pos->config_terms, list) {
+ if (term->type == EVSEL__CONFIG_TERM_CALLGRAPH)
+ callgraph_buf = term->val.str;
+ else if (term->type == EVSEL__CONFIG_TERM_STACK_USER)
+ dump_size = term->val.stack_user;
+ else if (term->type == EVSEL__CONFIG_TERM_MAX_STACK)
+ max_stack = term->val.max_stack;
+ }
+ if (callgraph_buf) {
+ if (strcmp(callgraph_buf, "no") != 0)
+ return true;
+ } else if (dump_size > 0 || max_stack > 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
/*
* The enable_on_exec/disabled value strategy:
*
@@ -1745,7 +1782,7 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts,
attr->mmap2 = track && !perf_missing_features.mmap2;
attr->comm = track;
attr->build_id = track && opts->build_id;
- attr->defer_output = track && callchain && callchain->defer;
+ attr->defer_output = track && ((callchain && callchain->defer) || opts->buildid_offset);

/*
* ksymbol is tracked separately with text poke because it needs to be
@@ -1831,6 +1868,22 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts,
*/
evsel__apply_config_terms(evsel, opts, track);

+ if (opts->buildid_offset && !evsel__is_offcpu_event(evsel)) {
+ if ((evsel->core.attr.sample_type & PERF_SAMPLE_IP) || track) {
+ evsel__reset_sample_bit(evsel, IP);
+ evsel__set_sample_bit(evsel, BUILD_ID_OFFSET);
+ if (!attr->exclude_user)
+ attr->defer_callchain = 1;
+ }
+ if ((evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN) ||
+ (track && evlist__has_callchain_config(evsel->evlist, callchain))) {
+ evsel__reset_sample_bit(evsel, CALLCHAIN);
+ evsel__set_sample_bit(evsel, CALLCHAIN_BUILD_ID_OFFSET);
+ if (!attr->exclude_callchain_user)
+ attr->defer_callchain = 1;
+ }
+ }
+
evsel->ignore_missing_thread = opts->ignore_missing_thread;

/* The --period option takes the precedence. */
diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h
index 411bb7276ad7..6c9568616177 100644
--- a/tools/perf/util/record.h
+++ b/tools/perf/util/record.h
@@ -55,6 +55,7 @@ struct record_opts {
bool kcore;
bool text_poke;
bool build_id;
+ bool buildid_offset;
unsigned int freq;
unsigned int mmap_pages;
unsigned int auxtrace_mmap_pages;
--
2.56.0.rc1.315.gc6ed9934b7-goog