[RFC PATCH v1 1/8] perf event: Factor build_id out into its own top-level struct
From: Ian Rogers
Date: Fri Aug 07 2026 - 03:32:11 EST
Future changes will use build_id in more contexts. For consistency make
the struct its own type and update usage to be of this type.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/include/uapi/linux/perf_event.h | 19 +++++++++----
tools/lib/perf/include/perf/event.h | 18 +++++++-----
tools/perf/builtin-inject.c | 3 +-
tools/perf/util/event.c | 4 +--
tools/perf/util/machine.c | 3 +-
tools/perf/util/python.c | 8 +++---
tools/perf/util/synthetic-events.c | 40 +++++++++++++++------------
7 files changed, 57 insertions(+), 38 deletions(-)
diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
index fd10aa8d697f..2479a38883d0 100644
--- a/tools/include/uapi/linux/perf_event.h
+++ b/tools/include/uapi/linux/perf_event.h
@@ -1058,6 +1058,18 @@ enum perf_event_type {
*/
PERF_RECORD_SAMPLE = 9,
+ /*
+ * Build IDs may be present in a number of events. They have a
+ * consistent encoding of:
+ *
+ * struct build_id {
+ * u8 size;
+ * u8 __reserved_1;
+ * u16 __reserved_2;
+ * u8 data[20];
+ * };
+ */
+
/*
* The MMAP2 records are an augmented version of MMAP, they add
* maj, min, ino numbers to be used to uniquely identify each mapping
@@ -1076,12 +1088,7 @@ enum perf_event_type {
* u64 ino;
* u64 ino_generation;
* };
- * struct {
- * u8 build_id_size;
- * u8 __reserved_1;
- * u16 __reserved_2;
- * u8 build_id[20];
- * };
+ * struct build_id build_id;
* };
* u32 prot, flags;
* char filename[];
diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/perf/event.h
index fdced574c889..173eab43c148 100644
--- a/tools/lib/perf/include/perf/event.h
+++ b/tools/lib/perf/include/perf/event.h
@@ -26,6 +26,15 @@ struct perf_record_mmap {
char filename[PATH_MAX];
};
+#define PERF_BUILD_ID_SIZE 20
+
+struct perf_build_id {
+ __u8 size;
+ __u8 __reserved_1;
+ __u16 __reserved_2;
+ __u8 data[PERF_BUILD_ID_SIZE];
+};
+
struct perf_record_mmap2 {
struct perf_event_header header;
__u32 pid, tid;
@@ -39,12 +48,7 @@ struct perf_record_mmap2 {
__u64 ino;
__u64 ino_generation;
};
- struct {
- __u8 build_id_size;
- __u8 __reserved_1;
- __u16 __reserved_2;
- __u8 build_id[20];
- };
+ struct perf_build_id build_id;
};
__u32 prot;
__u32 flags;
@@ -321,7 +325,7 @@ struct perf_record_header_build_id {
union {
__u8 build_id[24];
struct {
- __u8 data[20];
+ __u8 data[PERF_BUILD_ID_SIZE];
__u8 size;
__u8 reserved1__;
__u16 reserved2__;
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 70bbfad5653e..8559c0eab5f1 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -811,7 +811,8 @@ static int perf_event__repipe_mmap2(const struct perf_tool *tool,
struct dso_id id = dso_id_empty;
if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
- build_id__init(&id.build_id, event->mmap2.build_id, event->mmap2.build_id_size);
+ build_id__init(&id.build_id, event->mmap2.build_id.data,
+ event->mmap2.build_id.size);
} else {
id.maj = event->mmap2.maj;
id.min = event->mmap2.min;
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index ea75816d126a..c69ae57ce679 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -335,8 +335,8 @@ size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
char sbuild_id[SBUILD_ID_SIZE];
struct build_id bid;
- build_id__init(&bid, event->mmap2.build_id,
- event->mmap2.build_id_size);
+ build_id__init(&bid, event->mmap2.build_id.data,
+ event->mmap2.build_id.size);
build_id__snprintf(&bid, sbuild_id, sizeof(sbuild_id));
return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index f86b3b7df742..21d54ebc866b 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -1797,7 +1797,8 @@ int machine__process_mmap2_event(struct machine *machine,
perf_event__fprintf_mmap2(event, stdout);
if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
- build_id__init(&dso_id.build_id, event->mmap2.build_id, event->mmap2.build_id_size);
+ build_id__init(&dso_id.build_id, event->mmap2.build_id.data,
+ event->mmap2.build_id.size);
} else {
dso_id.maj = event->mmap2.maj;
dso_id.min = event->mmap2.min;
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index af85c0771c44..d35190052d97 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -233,12 +233,12 @@ static PyObject *pyrf_mmap2_event__get_build_id(PyObject *self, void *closure __
if (!(pevent->event.header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID))
Py_RETURN_NONE;
- int size = pevent->event.mmap2.build_id_size;
+ size_t size = pevent->event.mmap2.build_id.size;
- if (size > 20)
- size = 20;
+ if (size > sizeof(pevent->event.mmap2.build_id.data))
+ size = sizeof(pevent->event.mmap2.build_id.data);
- return PyBytes_FromStringAndSize((const char *)pevent->event.mmap2.build_id, size);
+ return PyBytes_FromStringAndSize((const char *)pevent->event.mmap2.build_id.data, size);
}
static PyGetSetDef pyrf_mmap2_event__getset[] = {
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 75a32ae8ef62..f7dedfb6bab8 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -452,7 +452,7 @@ static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
}
if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
- build_id__init(&dso_id.build_id, event->build_id, event->build_id_size);
+ build_id__init(&dso_id.build_id, event->build_id.data, event->build_id.size);
} else {
dso_id.maj = event->maj;
dso_id.min = event->min;
@@ -478,16 +478,20 @@ static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
nsinfo__put(nsi);
out:
+ event->build_id.__reserved_1 = 0;
+ event->build_id.__reserved_2 = 0;
if (rc == 0) {
- memcpy(event->build_id, bid.data, sizeof(bid.data));
- event->build_id_size = (u8) bid.size;
+ memcpy(event->build_id.data, bid.data, sizeof(bid.data));
+ event->build_id.size = (u8) bid.size;
event->header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
- event->__reserved_1 = 0;
- event->__reserved_2 = 0;
if (dso && !dso__has_build_id(dso))
dso__set_build_id(dso, &bid);
} else {
+ memset(&event->build_id, 0, sizeof(event->build_id));
+ event->build_id.size = 0;
+ event->header.misc &= ~PERF_RECORD_MISC_MMAP_BUILD_ID;
+
if (event->filename[0] == '/') {
pr_debug2("Failed to read build ID for %s\n",
event->filename);
@@ -611,8 +615,10 @@ int perf_event__synthesize_mmap_events(const struct perf_tool *tool,
event->mmap2.prot = prot;
event->mmap2.flags = flags;
- if (!symbol_conf.no_buildid_mmap2)
- perf_record_mmap2__read_build_id(&event->mmap2, machine, false);
+ if (!symbol_conf.no_buildid_mmap2) {
+ perf_record_mmap2__read_build_id(&event->mmap2, machine,
+ /*is_kernel=*/false);
+ }
if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
rc = -1;
@@ -807,12 +813,12 @@ static int perf_event__synthesize_modules_maps_cb(struct map *map, void *data)
/* Clear stale build ID and entire union from previous module iteration */
event->mmap2.header.misc &= ~PERF_RECORD_MISC_MMAP_BUILD_ID;
- memset(event->mmap2.build_id, 0, sizeof(event->mmap2.build_id));
- event->mmap2.build_id_size = 0;
- event->mmap2.__reserved_1 = 0;
- event->mmap2.__reserved_2 = 0;
+ memset(event->mmap2.build_id.data, 0, sizeof(event->mmap2.build_id.data));
+ event->mmap2.build_id.size = 0;
+ event->mmap2.build_id.__reserved_1 = 0;
+ event->mmap2.build_id.__reserved_2 = 0;
- perf_record_mmap2__read_build_id(&event->mmap2, args->machine, false);
+ perf_record_mmap2__read_build_id(&event->mmap2, args->machine, /*is_kernel=*/false);
} else {
const char *long_name = dso__long_name(dso);
@@ -1293,7 +1299,7 @@ static int __perf_event__synthesize_kernel_mmap(const struct perf_tool *tool,
event->mmap2.len = map__end(map) - event->mmap.start;
event->mmap2.pid = machine->pid;
- perf_record_mmap2__read_build_id(&event->mmap2, machine, true);
+ perf_record_mmap2__read_build_id(&event->mmap2, machine, /*is_kernel=*/true);
} else {
size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
"%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
@@ -2486,10 +2492,10 @@ int perf_event__synthesize_mmap2_build_id(const struct perf_tool *tool,
ev.mmap2.len = len;
ev.mmap2.pgoff = pgoff;
- ev.mmap2.build_id_size = bid->size;
- if (ev.mmap2.build_id_size > sizeof(ev.mmap2.build_id))
- ev.mmap2.build_id_size = sizeof(ev.mmap2.build_id);
- memcpy(ev.mmap2.build_id, bid->data, ev.mmap2.build_id_size);
+ ev.mmap2.build_id.size = bid->size;
+ if (ev.mmap2.build_id.size > sizeof(ev.mmap2.build_id.data))
+ ev.build_id.size = sizeof(ev.mmap2.build_id.data);
+ memcpy(ev.mmap2.build_id.data, bid->data, ev.mmap2.build_id.size);
ev.mmap2.prot = prot;
ev.mmap2.flags = flags;
--
2.55.0.679.g6767b8d81c-goog