perf record: collect BPF metadata from existing BPF programs
authorBlake Jones <blakejones@google.com>
Thu, 12 Jun 2025 19:49:36 +0000 (12:49 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Fri, 20 Jun 2025 21:48:35 +0000 (14:48 -0700)
commitab38e84ba9a80581e055408e0f8c0158998fa4b9
tree7ff430fed80abb03849ec456f826bedeb2ebaee8
parent1d0654b7fdc5431b85035f6e76b4bc57679575d8
perf record: collect BPF metadata from existing BPF programs

Look for .rodata maps, find ones with 'bpf_metadata_' variables, extract
their values as strings, and create a new PERF_RECORD_BPF_METADATA
synthetic event using that data. The code gets invoked from the existing
routine perf_event__synthesize_one_bpf_prog().

For example, a BPF program with the following variables:

    const char bpf_metadata_version[] SEC(".rodata") = "3.14159";
    int bpf_metadata_value[] SEC(".rodata") = 42;

would generate a PERF_RECORD_BPF_METADATA record with:

    .prog_name        = <BPF program name, e.g. "bpf_prog_a1b2c3_foo">
    .nr_entries       = 2
    .entries[0].key   = "version"
    .entries[0].value = "3.14159"
    .entries[1].key   = "value"
    .entries[1].value = "42"

Each of the BPF programs and subprograms that share those variables would
get a distinct PERF_RECORD_BPF_METADATA record, with the ".prog_name"
showing the name of each program or subprogram. The prog_name is
deliberately the same as the ".name" field in the corresponding
PERF_RECORD_KSYMBOL record.

This code only gets invoked if support for displaying BTF char arrays
as strings is detected.

Signed-off-by: Blake Jones <blakejones@google.com>
Link: https://lore.kernel.org/r/20250612194939.162730-3-blakejones@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/lib/perf/include/perf/event.h
tools/perf/util/bpf-event.c
tools/perf/util/bpf-event.h