[PATCH v3 17/19] perf record: Add --buildid-offset option
From: Ian Rogers
Date: Mon Nov 16 2026 - 02:46:28 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/Documentation/perf-record.txt | 6 ++++++
tools/perf/builtin-record.c | 22 ++++++++++++++++++++--
tools/perf/util/evsel.c | 17 ++++++++++++++++-
tools/perf/util/record.c | 22 ++++++++++++++++++++++
tools/perf/util/record.h | 1 +
5 files changed, 65 insertions(+), 3 deletions(-)
diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 2351f7b4f5fc..d821303c923c 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -578,6 +578,12 @@ Specify vmlinux path which has debuginfo.
--buildid-all::
Record build-id of all DSOs regardless whether it's actually hit or not.
+--buildid-offset::
+Record build-id and DSO file offset instead of virtual instruction pointer
+addresses in samples and callchains (using PERF_SAMPLE_BUILD_ID_OFFSET and
+PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET). Implies --buildid-mmap and cannot be
+used with --no-buildid-mmap.
+
--buildid-mmap::
Legacy record build-id in map events option which is now the
default. Behaves indentically to --no-buildid. Disable with
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 085d3648a30a..5796f13ffe8c 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -936,7 +936,8 @@ static int record__config_tracking_events(struct record *rec)
* delay of waiting or event synthesis.
*/
if (opts->target.initial_delay || target__has_cpu(&opts->target) ||
- perf_pmus__num_core_pmus() > 1) {
+ perf_pmus__num_core_pmus() > 1 ||
+ (opts->buildid_offset && evlist__nr_entries(evlist) > 1)) {
/*
* User space tasks can migrate between CPUs, so when tracing
* selected CPUs, sideband for all CPUs is still needed.
@@ -3625,6 +3626,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,
@@ -4162,6 +4165,20 @@ int cmd_record(int argc, const char **argv)
record.opts.record_switch_events = true;
}
+ if (rec->opts.buildid_offset) {
+ if (rec->buildid_mmap_set && !rec->buildid_mmap) {
+ pr_err("--buildid-offset cannot be used with --no-buildid-mmap.\n");
+ err = -EINVAL;
+ goto out_opts;
+ }
+ if (!perf_can_record_build_id()) {
+ pr_err("Missing support for build id in kernel mmap events required by --buildid-offset.\n");
+ err = -EINVAL;
+ goto out_opts;
+ }
+ rec->buildid_mmap = true;
+ }
+
if (rec->buildid_mmap && !perf_can_record_build_id()) {
pr_warning("Missing support for build id in kernel mmap events.\n"
"Disable this warning with --no-buildid-mmap\n");
@@ -4352,7 +4369,8 @@ int cmd_record(int argc, const char **argv)
evlist__warn_user_requested_cpus(rec->evlist, rec->opts.target.cpu_list);
- if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP) {
+ if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP &&
+ !rec->opts.buildid_offset) {
if (EM_HOST == EM_AARCH64)
add_leaf_frame_caller_opts_aarch64(&rec->opts);
}
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 49f6bbf08160..32c258764db9 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1745,7 +1745,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 +1831,21 @@ 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 (!evsel->no_aux_samples && !attr->exclude_user)
+ attr->defer_callchain = 1;
+ }
+ if ((evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN) &&
+ !attr->exclude_callchain_user) {
+ evsel__reset_sample_bit(evsel, CALLCHAIN);
+ evsel__set_sample_bit(evsel, CALLCHAIN_BUILD_ID_OFFSET);
+ 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.c b/tools/perf/util/record.c
index 38e8aee3106b..77337e1318ab 100644
--- a/tools/perf/util/record.c
+++ b/tools/perf/util/record.c
@@ -110,6 +110,28 @@ void evlist__config(struct evlist *evlist, struct record_opts *opts, struct call
evsel->core.attr.comm_exec = 1;
}
+ if (opts->buildid_offset) {
+ bool has_callchain_bid = false;
+
+ evlist__for_each_entry(evlist, evsel) {
+ if ((evsel->core.attr.sample_type &
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) &&
+ !evsel->core.attr.exclude_callchain_user) {
+ has_callchain_bid = true;
+ break;
+ }
+ }
+ if (has_callchain_bid) {
+ evlist__for_each_entry(evlist, evsel) {
+ if (evsel->core.attr.defer_output &&
+ evsel__is_dummy_event(evsel)) {
+ evsel__set_sample_bit(evsel, CALLCHAIN_BUILD_ID_OFFSET);
+ evsel->core.attr.exclude_callchain_kernel = 1;
+ }
+ }
+ }
+ }
+
/* Configure leader sampling here now that the sample type is known */
evlist__for_each_entry(evlist, evsel)
evsel__config_leader_sampling(evsel, evlist);
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.1.703.gf848ed807c-goog