perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support
Subsystems (8)
Ian Rogers
reviewerv3 cover perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support
This patch series introduces PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET to the perf_event UAPI and implements full support across both the kernel and perf tools. It builds on earlier work making build IDs the default and improving build ID handling in perf [1].
Based on linux-next (next-20261113) merged with perf-tools-next (b7ff10b9ddec), which combines the tip, bpf-next, and perf-tools-next trees including the prerequisite deferred-output perf/core fixes [4] and standalone perf tool fixes.
Background & Motivation:
In order for perf to translate virtual addresses of samples into symbols a file and offset within the file are needed. During event synthesis perf will create mmap events to facilitate the translation of a virtual address to a file and offset by modelling the address space of a process. By directly recording in a sample the Build ID of a file and the offset within it, no synthesis is necessary. The Build ID and offset as a pair are larger than a virtual address, so there is a trade-off between synthesis cost and extra size for samples. These changes facilitate Build ID and offset as a choice for perf samples so the user can choose to use it when they believe it is advantageous.
In practice perf still needs to map a build ID to a file, so by default this change keeps synthesis to allow this. It is expected a user that knows their build IDs, say through debuginfod, will disable this option with --synth=no.
For callchains (PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET and PERF_RECORD_CALLCHAIN_DEFERRED with PERF_RECORD_MISC_MMAP_BUILD_ID), unique build IDs are deduplicated into a compact variable-length table (up to 254 unique build IDs per record) and referenced by a 1-based index in the top byte alongside a 56-bit file offset for user frames, while non-user callchain addresses and context markers are preserved unmodified across architectures.
The kernel support uses the existing build ID and offset support used by BPF stack traces (stack_map_get_build_id_offset()). When attr.defer_callchain is enabled (the default for perf record --buildid-offset), user-space build ID and offset resolution is deferred to a task_work callback where page faults are permitted, with resolved entries emitted via PERF_RECORD_CALLCHAIN_DEFERRED.
The Build ID and offset samples inherently don't leak ASLR information. As with the ASLR remapping work in perf inject, support is added to migrate virtual addresses to Build ID and offset (perf inject --sample-buildids) for testing and conversion without kernel support.
Series Organization:
- Patch 1: Pre-existing kernel perf/core fix for per-CPU callchain buffer protection across bpf_overflow_handler().
- Patches 2-6: Kernel UAPI, BPF helper export, and perf/core synchronous and deferred BUILD_ID_OFFSET / CALLCHAIN_BUILD_ID_OFFSET support.
- Patches 7-9: Pre-existing perf tool fixes uncovered during development and review.
- Patches 10-19: Tool UAPI header sync, perf parsing/synthesis/symbol resolution, perf inject --sample-buildids, perf record --buildid-offset, man page updates, and C/shell test coverage.
v3:
- Rebased onto linux-next (next-20261113) + perf-tools-next, dropping the prerequisite perf/core [4] and perf tool fixes that were merged upstream, and addressed review feedback on v2 [3] as well as findings from Sashiko AI review (with AI coding assistant help refactoring and verifying fixes against kernel/perf builds, checkpatch.pl, and perf test):
- Split larger kernel and tool patches into smaller logical commits and separated pre-existing kernel and perf tool bug fixes into standalone commits with Fixes: tags at the start of the kernel and tool sections.
- Changed PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET and build-ID PERF_RECORD_CALLCHAIN_DEFERRED to deduplicate build IDs into a compact packed table + 8-bit index / 56-bit file offset array, keeping non-user addresses lossless across all architectures.
- Preserved source-level API compatibility in libperf's struct perf_record_mmap2 union by retaining build_id_size and build_id[] alongside struct perf_build_id bid.
- Fixed per-CPU callchain buffer protection across bpf_overflow_handler(), deferred output build-ID accounting and single-IP clamping, and deferred callchain u16 header.size clamping.
- Isolated user vs kernel DSO/map build-ID lookups, handled vDSO and kcore names when synthesizing build IDs in perf inject --sample-buildids, aligned sample payload walking with __evsel__parse_sample(), and fixed cross-endian deferred sample re-parsing and Python event copying.
- Documented --sample-buildids in perf-inject.txt and --buildid-offset in perf-record.txt.
v2:
- Addressed review feedback from Namhyung Kim and Sashiko AI on v1 [2]:
- Extended the deferred callchain unwind infrastructure (attr.defer_callchain / PERF_RECORD_CALLCHAIN_DEFERRED) to support deferred user-space build ID and offset resolution in task_work context where page faults are allowed, avoiding NMI-time page fault limitations and per-CPU irq_work contention.
- Batched in-place user callchain resolution into a single stack_map_get_build_id_offset() call and skipped kernel frames.
- Defined struct perf_build_id and struct perf_sample_build_id_offset consistently across UAPI and libperf headers, with a fallback for __DECLARE_FLEX_ARRAY.
- Added reference-counted tracking (nr_build_id_offset_events and nr_callchain_build_id_offset_events) and clamped callchain frame counts in perf_prepare_sample() and perf_callchain_deferred_output() to prevent u16 header.size overflows.
- Added cross-endian byte-swapping support for PERF_SAMPLE_BUILD_ID_OFFSET, PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET, and PERF_RECORD_CALLCHAIN_DEFERRED.
- Fixed perf_record_mmap2 union field preservation when build ID synthesis fails, callchain context marker tracking in perf inject, and per-event callgraph configuration in perf record --buildid-offset.
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/linux-perf-users/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (19):
perf/core: Protect per-CPU callchain buffer across
bpf_overflow_handler()
perf/uapi: Factor perf_build_id out into its own top-level struct perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI
bpf: Export stack_map_get_build_id_offset() for perf events perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET
sample types
perf/core: Support deferred unwinding for BUILD_ID_OFFSET sample types perf inject: Set ordered_events and ordering_requires_timestamps for
--aslr
perf session: Avoid double byte-swapping re-parsed deferred samples perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in
aslr_tool__process_sample()
tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET
perf: Refactor thread map and symbol APIs to take perf_sample perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id() perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types perf symbol: Resolve symbols and callchains using build ID and offset perf inject: Add --sample-buildids to convert samples to build ID +
offset
perf record: Add --buildid-offset option
perf test: Add BUILD_ID_OFFSET C unit tests
perf test: Add inject --sample-buildids and record --buildid-offset
shell tests
arch/powerpc/perf/core-book3s.c | 10 +- arch/s390/kernel/perf_cpum_sf.c | 7 +- include/linux/bpf.h | 14 + include/linux/perf_event.h | 43 +- include/uapi/linux/perf_event.h | 72 +- kernel/bpf/stackmap.c | 10 +- kernel/events/callchain.c | 206 +++- kernel/events/core.c | 310 +++++- tools/include/uapi/linux/perf_event.h | 72 +- tools/lib/perf/include/perf/event.h | 43 +- tools/perf/Documentation/perf-inject.txt | 8 + tools/perf/Documentation/perf-record.txt | 6 + .../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/builtin-inject.c | 80 +- tools/perf/builtin-record.c | 22 +- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-script.c | 75 +- tools/perf/builtin-timechart.c | 5 +- tools/perf/builtin-trace.c | 10 +- tools/perf/tests/Build | 1 + tools/perf/tests/bid-offset.c | 61 ++ tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/code-reading.c | 6 +- tools/perf/tests/mmap-thread-lookup.c | 58 +- tools/perf/tests/sample-parsing.c | 60 +- tools/perf/tests/shell/inject_bid_offset.sh | 179 ++++ tools/perf/tests/shell/record.sh | 74 ++ tools/perf/tests/tests.h | 1 + tools/perf/util/Build | 1 + tools/perf/util/annotate-data.c | 5 +- tools/perf/util/aslr.c | 142 ++- tools/perf/util/build-id.c | 2 +- tools/perf/util/callchain.c | 153 ++- tools/perf/util/capstone.c | 4 +- tools/perf/util/cs-etm.c | 10 +- tools/perf/util/data-convert-json.c | 4 +- tools/perf/util/debug.c | 6 +- tools/perf/util/dlfilter.c | 8 +- tools/perf/util/dsos.c | 68 ++ tools/perf/util/dsos.h | 6 + tools/perf/util/event.c | 89 +- tools/perf/util/evlist.c | 38 +- tools/perf/util/evlist.h | 2 + tools/perf/util/evsel.c | 180 +++- tools/perf/util/evsel.h | 3 +- tools/perf/util/evsel_fprintf.c | 9 +- tools/perf/util/inject_bid_offset.c | 882 ++++++++++++++++++ tools/perf/util/inject_bid_offset.h | 15 + tools/perf/util/intel-pt.c | 24 +- tools/perf/util/machine.c | 194 +++- tools/perf/util/maps.c | 73 ++ tools/perf/util/maps.h | 2 + tools/perf/util/perf_event_attr_fprintf.c | 3 +- tools/perf/util/python.c | 43 +- tools/perf/util/record.c | 22 + tools/perf/util/record.h | 1 + tools/perf/util/sample.c | 84 ++ tools/perf/util/sample.h | 39 + tools/perf/util/session.c | 140 ++- tools/perf/util/synthetic-events.c | 77 +- tools/perf/util/thread.c | 11 +- tools/perf/util/thread.h | 14 +- tools/perf/util/unwind-libdw.c | 7 +- tools/perf/util/unwind-libunwind.c | 8 +- 64 files changed, 3433 insertions(+), 380 deletions(-) create mode 100644 tools/perf/tests/bid-offset.c create mode 100755 tools/perf/tests/shell/inject_bid_offset.sh create mode 100644 tools/perf/util/inject_bid_offset.c create mode 100644 tools/perf/util/inject_bid_offset.h
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 1/19 perf/core: Protect per-CPU callchain buffer across bpf_overflow_handler()
In bpf_overflow_handler(), perf_prepare_sample() runs before bpf_prog_run() and saves data->callchain pointing into the current CPU's callchain buffer for the active interrupt_context_level(), after get_perf_callchain() has already released its callchain_recursion slot.
If the attached BPF_PROG_TYPE_PERF_EVENT program invokes a helper at the same interrupt_context_level() that calls get_callchain_entry() (such as bpf_perf_event_output() on a PERF_COUNT_SW_BPF_OUTPUT event configured with PERF_SAMPLE_CALLCHAIN and a larger stack, or bpf_get_task_stack()), it re-acquires the same per-CPU callchain buffer and overwrites data->callchain->nr in place. When bpf_prog_run() returns non-zero and __perf_event_output() emits the outer event's sample, perf_output_begin() reserves only the smaller dyn_size computed earlier while perf_output_sample() copies the overwritten larger data->callchain->nr entries, overflowing the ring-buffer reservation.
Hold the callchain recursion slot via get_callchain_entry() across bpf_prog_run() when PERF_SAMPLE_CALLCHAIN has been prepared so nested callers at the same context level cannot overwrite the active per-CPU callchain buffer.
--- kernel/events/core.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/events/core.c +7 -0
@@ -10818,8 +10818,15 @@ static int bpf_overflow_handler(struct perf_event *event,rcu_read_lock (); prog =READ_ONCE (event->prog);if (prog) { +struct perf_callchain_entry *callchain =NULL ; +int callchain_rctx; +perf_prepare_sample (data, event, regs); +if (data->sample_flags &PERF_SAMPLE_CALLCHAIN ) + callchain =get_callchain_entry (&callchain_rctx); ret =bpf_prog_run (prog, &ctx); +if (callchain) +put_callchain_entry (callchain_rctx); }rcu_read_unlock (); out:
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 2/19 perf/uapi: Factor perf_build_id out into its own top-level struct
In reply to Ian RogersDocument struct perf_build_id as a top-level structure in the perf_event UAPI comment and add it to the PERF_RECORD_MMAP2 union comment so future sample types can reference struct perf_build_id directly.
--- include/uapi/linux/perf_event.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/uapi/linux/perf_event.h +13 -0
@@ -1099,6 +1099,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 perf_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
@@ -1123,6 +1135,7 @@ enum perf_event_type {* u16 __reserved_2; * u8 build_id[20]; * }; +* struct perf_build_id bid; * }; * u32 prot, flags; * char filename[];
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 3/19 perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI
In reply to Ian RogersIntroduce PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET in enum perf_event_sample_format.
For PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET (and PERF_RECORD_CALLCHAIN_DEFERRED when PERF_RECORD_MISC_MMAP_BUILD_ID is set), encode a deduplicated table of variable-length build IDs (u32 nr_build_ids, u32 nr_offsets, followed by packed { u8 size; u8 data[size]; } entries padded to 8-byte alignment) and an array of 64-bit offsets whose top 8 bits encode the 1-based build ID index (or 0x00 for unresolved user addresses and 0xff for kernel/context entries). In common cases where multiple callchain frames belong to the same binary, each unique build ID is encoded only once.
--- include/uapi/linux/perf_event.h | 59 ++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 5 deletions(-)
include/uapi/linux/perf_event.h +54 -5
@@ -165,8 +165,10 @@ enum perf_event_sample_format {PERF_SAMPLE_DATA_PAGE_SIZE =1U <<22 ,PERF_SAMPLE_CODE_PAGE_SIZE =1U <<23 ,PERF_SAMPLE_WEIGHT_STRUCT =1U <<24 , +PERF_SAMPLE_BUILD_ID_OFFSET =1U <<25 , +PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET =1U <<26 , - PERF_SAMPLE_MAX = 1U << 25, /* non-ABI */ + PERF_SAMPLE_MAX = 1U << 27, /* non-ABI */ };#define PERF_SAMPLE_WEIGHT_TYPE (PERF_SAMPLE_WEIGHT | PERF_SAMPLE_WEIGHT_STRUCT)
@@ -840,7 +842,8 @@ struct perf_event_mmap_page {* * PERF_RECORD_MISC_EXACT_IP - PERF_RECORD_SAMPLE of precise events * PERF_RECORD_MISC_SWITCH_OUT_PREEMPT - PERF_RECORD_SWITCH* events -* PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 event +* PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 and +* PERF_RECORD_CALLCHAIN_DEFERRED events * * * PERF_RECORD_MISC_EXACT_IP:
@@ -852,7 +855,7 @@ struct perf_event_mmap_page {* Indicates that thread was preempted in TASK_RUNNING state. * * PERF_RECORD_MISC_MMAP_BUILD_ID: -* Indicates that mmap2 event carries build ID data. +* Indicates that mmap2 or deferred callchain event carries build ID data. */ #define PERF_RECORD_MISC_EXACT_IP (1 << 14)#define PERF_RECORD_MISC_SWITCH_OUT_PREEMPT (1 << 14)
@@ -1095,6 +1098,38 @@ enum perf_event_type {* { u64 code_page_size;} && PERF_SAMPLE_CODE_PAGE_SIZE * { u64 size; * char data[size]; } && PERF_SAMPLE_AUX +* { union { +* struct { +* struct perf_build_id bid; +* u64 offset; +* }; +* struct { +* u64 zero; +* u64 context; +* u64 cookie; +* u64 ip; +* }; && attr.defer_callchain +* }; } && PERF_SAMPLE_BUILD_ID_OFFSET +* { u32 nr_build_ids; +* u32 nr_offsets; +* struct { +* u8 size; +* u8 data[size]; +* }[nr_build_ids]; +* u8 pad[]; +* u64 offsets[nr_offsets]; +* # +* # Entries in offsets[] include PERF_CONTEXT_* markers (which +* # have top 8 bits 0xff). Kernel/hypervisor entries (after +* # PERF_CONTEXT_KERNEL, PERF_CONTEXT_HV, PERF_CONTEXT_GUEST, +* # or PERF_CONTEXT_GUEST_KERNEL) and the cookie word following +* # PERF_CONTEXT_USER_DEFERRED are raw 64-bit values. User +* # entries (after PERF_CONTEXT_USER or PERF_CONTEXT_GUEST_USER, +* # or in PERF_RECORD_CALLCHAIN_DEFERRED) use the top 8 bits to +* # encode a 1-based index (1..254) into the build_ids table and +* # the lower 56 bits for the DSO file offset, or 0x00 for an +* # unresolved user virtual address. +* } && PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET * }; */ PERF_RECORD_SAMPLE = 9,
@@ -1306,8 +1341,22 @@ enum perf_event_type {* struct { * struct perf_event_header header; * u64 cookie; -* u64 nr; -* u64 ips[nr]; +* union { +* struct { +* u64 nr; +* u64 ips[nr]; +* }; +* struct { +* u32 nr_build_ids; +* u32 nr_offsets; +* struct { +* u8 size; +* u8 data[size]; +* }[nr_build_ids]; +* u8 pad[]; +* u64 offsets[nr_offsets]; +* }; && (header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) +* }; * struct sample_id sample_id; * }; */
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 4/19 bpf: Export stack_map_get_build_id_offset() for perf events
In reply to Ian RogersMake stack_map_get_build_id_offset() non-static and declare it in include/linux/bpf.h (with an inline fallback when CONFIG_BPF_SYSCALL or CONFIG_PERF_EVENTS is disabled) so that the perf events core can reuse it to resolve user-space instruction pointers to build IDs and file offsets.
--- include/linux/bpf.h | 14 ++++++++++++++ kernel/bpf/stackmap.c | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-)
include/linux/bpf.h +14 -0
@@ -4503,4 +4503,18 @@ static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 all return 0; } +#if defined(CONFIG_BPF_SYSCALL ) && defined(CONFIG_PERF_EVENTS ) +void stack_map_get_build_id_offset (struct bpf_stack_build_id *id_offs, +u32 trace_nr,bool user,bool may_fault); +#else +static inline void stack_map_get_build_id_offset (struct bpf_stack_build_id *id_offs, +u32 trace_nr,bool user,bool may_fault) +{ +for (u32 i =0 ; i < trace_nr; i++) { + id_offs[i].status =BPF_STACK_BUILD_ID_IP ; +memset (id_offs[i].build_id,0 ,BPF_BUILD_ID_SIZE ); + } +} +#endif +#endif /* _LINUX_BPF_H */
kernel/bpf/stackmap.c +2 -2
@@ -406,8 +406,8 @@ static void stack_map_get_build_id_offset_sleepable(struct bpf_stack_build_id *i* id_offs[i].build_id is zeroed out and id_offs[i].status is set to * BPF_STACK_BUILD_ID_IP. */ -static void stack_map_get_build_id_offset (struct bpf_stack_build_id *id_offs, -u32 trace_nr,bool user,bool may_fault) +void stack_map_get_build_id_offset (struct bpf_stack_build_id *id_offs, +u32 trace_nr,bool user,bool may_fault) {struct mmap_unlock_irq_work *work;bool has_user_ctx = user && current && current->mm;
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 5/19 perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types
In reply to Ian RogersImplement synchronous PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET payload writing in the perf event core using stack_map_get_build_id_offset(..., may_fault=false).
For callchains, extend the per-CPU callchain buffers in kernel/events/callchain.c to provide scratch space to deduplicate build IDs and encode the compressed build ID table and tagged 64-bit offsets (with the top 8 bits storing the 1-based build ID index).
--- arch/powerpc/perf/core-book3s.c | 10 +- arch/s390/kernel/perf_cpum_sf.c | 7 +- include/linux/perf_event.h | 43 ++++++- kernel/bpf/stackmap.c | 6 +- kernel/events/callchain.c | 206 +++++++++++++++++++++++++++++++- kernel/events/core.c | 132 ++++++++++++++++++-- 6 files changed, 385 insertions(+), 19 deletions(-)
arch/powerpc/perf/core-book3s.c +6 -4
@@ -2271,10 +2271,11 @@ static void record_and_restart(struct perf_event *event, unsigned long val,/* * If address is not requested in the sample via -* PERF_SAMPLE_IP, just record that sample irrespective -* of SIAR valid check. +* PERF_SAMPLE_IP or PERF_SAMPLE_BUILD_ID_OFFSET, just +* record that sample irrespective of SIAR valid check. */ -if (event->attr.sample_type &PERF_SAMPLE_IP ) +if (event->attr.sample_type & + (PERF_SAMPLE_IP |PERF_SAMPLE_BUILD_ID_OFFSET )) record =siar_valid (regs);else record =1 ;
@@ -2297,7 +2298,8 @@ static void record_and_restart(struct perf_event *event, unsigned long val,* these cases. */ if (event->attr.exclude_kernel && - (event->attr.sample_type &PERF_SAMPLE_IP ) && + (event->attr.sample_type & + (PERF_SAMPLE_IP |PERF_SAMPLE_BUILD_ID_OFFSET )) &&is_kernel_addr (mfspr (SPRN_SIAR ))) record =0 ;
arch/s390/kernel/perf_cpum_sf.c +5 -2
@@ -840,8 +840,11 @@ static bool is_callchain_event(struct perf_event *event) {u64 sample_type = event->attr.sample_type; -return sample_type & (PERF_SAMPLE_CALLCHAIN |PERF_SAMPLE_REGS_USER | -PERF_SAMPLE_REGS_INTR |PERF_SAMPLE_STACK_USER ); +return sample_type & (PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET | +PERF_SAMPLE_BUILD_ID_OFFSET | +PERF_SAMPLE_REGS_USER |PERF_SAMPLE_REGS_INTR | +PERF_SAMPLE_STACK_USER ); }static int cpumsf_pmu_event_init (struct perf_event *event)
include/linux/perf_event.h +41 -2
73 lines not on this page · raw message
kernel/bpf/stackmap.c +4 -2
@@ -691,7 +691,8 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,int ret;/* perf_sample_data doesn't have callchain, use bpf_get_stackid */ -if (!(event->attr.sample_type &PERF_SAMPLE_CALLCHAIN )) +if (!(event->attr.sample_type & + (PERF_SAMPLE_CALLCHAIN |PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET )))return bpf_get_stackid ((unsigned long )(ctx->regs), (unsigned long ) map, flags,0 ,0 );
@@ -982,7 +983,8 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,int err = -EINVAL ;__u64 nr_kernel; -if (!(event->attr.sample_type &PERF_SAMPLE_CALLCHAIN )) +if (!(event->attr.sample_type & + (PERF_SAMPLE_CALLCHAIN |PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET )))return __bpf_get_stack (regs,NULL , buf, size, flags, false/* !may_fault */ ); if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
kernel/events/callchain.c +204 -2
236 lines not on this page · raw message
kernel/events/core.c +125 -7
198 lines not on this page · raw message
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 6/19 perf/core: Support deferred unwinding for BUILD_ID_OFFSET sample types
In reply to Ian RogersWhen attr.defer_callchain is set on events using PERF_SAMPLE_BUILD_ID_OFFSET or PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET, request a deferred user-space unwind via unwind_deferred_request() and emit a PERF_CONTEXT_USER_DEFERRED marker and cookie in the sample.
In the faultable task_work callback (perf_unwind_deferred_callback()), resolve the unwound user IPs to build IDs and file offsets using stack_map_get_build_id_offset(..., may_fault=true), encode them into the compressed build ID + offset format, and emit PERF_RECORD_CALLCHAIN_DEFERRED with PERF_RECORD_MISC_MMAP_BUILD_ID set.
--- kernel/events/core.c | 175 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 164 insertions(+), 11 deletions(-)
kernel/events/core.c +164 -11
@@ -460,6 +460,8 @@ static atomic_t nr_bpf_events __read_mostly;static atomic_t nr_cgroup_events__read_mostly ;static atomic_t nr_text_poke_events__read_mostly ;static atomic_t nr_build_id_events__read_mostly ; +static atomic_t nr_defer_output_build_id_events__read_mostly ; +static atomic_t nr_defer_output_callchain_build_id_events__read_mostly ;static LIST_HEAD (pmus);static DEFINE_MUTEX (pmus_lock);
@@ -5639,6 +5641,25 @@ static void detach_perf_ctx_data(struct perf_event *event) } } +static bool has_defer_output_callchain_build_id_offset (const struct perf_event *event) +{ +return event->attr.defer_output && + (event->attr.sample_type &PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET ) && + !(event->attr.sample_type &PERF_SAMPLE_CALLCHAIN ) && + !event->attr.exclude_callchain_user; +} + +static bool has_defer_output_build_id_offset (const struct perf_event *event) +{ +if (!event->attr.defer_output) +return false; +if (has_defer_output_callchain_build_id_offset (event)) +return true; +return (event->attr.sample_type &PERF_SAMPLE_BUILD_ID_OFFSET ) && + (!(event->attr.sample_type &PERF_SAMPLE_CALLCHAIN ) || + event->attr.exclude_callchain_user); +} +static void unaccount_event (struct perf_event *event) {bool dec = false;
@@ -5652,6 +5673,10 @@ static void unaccount_event(struct perf_event *event)atomic_dec (&nr_mmap_events);if (event->attr.build_id)atomic_dec (&nr_build_id_events); +if (has_defer_output_build_id_offset (event)) +atomic_dec (&nr_defer_output_build_id_events); +if (has_defer_output_callchain_build_id_offset (event)) +atomic_dec (&nr_defer_output_callchain_build_id_events);if (event->attr.comm)atomic_dec (&nr_comm_events);if (event->attr.namespaces)
@@ -8399,6 +8424,8 @@ static void perf_output_read(struct perf_output_handle *handle,perf_output_read_one (handle, event, enabled, running); } +static struct unwind_work perf_unwind_work; +static void perf_bpf_to_sample_bid_offset (struct bpf_stack_build_id *bpf_bid) {struct perf_sample_build_id_offset *bid = (void *)bpf_bid;
@@ -8434,7 +8461,28 @@ static void perf_output_sample_build_id(struct perf_output_handle *handle,PERF_RECORD_MISC_USER ;if (sample_bid) { -if (is_user && has_user_ctx) { +u64 defer_cookie =0 ; +bool defer_user =IS_ENABLED (CONFIG_UNWIND_USER ) && + is_user && has_user_ctx && + event->attr.defer_callchain && + (!(sample_type &PERF_SAMPLE_CALLCHAIN ) || + event->attr.exclude_callchain_user) && + data->ip ==instruction_pointer (task_pt_regs (current)); + +if (defer_user && +unwind_deferred_request (&perf_unwind_work, &defer_cookie) <0 ) + defer_cookie =0 ; + +if (defer_cookie) { +u64 deferred_bid[4 ] = { +0 , +PERF_CONTEXT_USER_DEFERRED , + defer_cookie, + data->ip, + }; + +perf_output_put (handle, deferred_bid); + }else if (is_user && has_user_ctx) {if (callchain_bid && data->callchain_build_id && data->callchain_build_id->has_ip_bid) { perf_output_put(handle,
@@ -8811,8 +8859,6 @@ static u64 perf_get_page_size(unsigned long addr)static struct perf_callchain_entry __empty_callchain = { .nr =0 , }; -static struct unwind_work perf_unwind_work; -struct perf_callchain_entry *perf_callchain (struct perf_event *event,struct pt_regs *regs) {
@@ -10635,34 +10681,117 @@ void perf_event_bpf_event(struct bpf_prog *prog,struct perf_callchain_deferred_event {struct unwind_stacktrace *trace; +struct perf_callchain_build_id_entry *bid_entry;struct {struct perf_event_header header;u64 cookie; } event; }; +static struct perf_callchain_build_id_entry * +perf_resolve_build_id_offsets (struct unwind_stacktrace *trace) +{ +struct perf_callchain_build_id_entry *entry; +struct bpf_stack_build_id *bpf_bids; +u16 nr = trace->nr; +u32 max_size; + +if (!nr || !current->mm || + !atomic_read (&nr_defer_output_build_id_events)) +return NULL ; + +if (!atomic_read (&nr_defer_output_callchain_build_id_events)) + nr =1 ; + + entry =kzalloc (sizeof (*entry) + nr *sizeof (u64),GFP_KERNEL ); +if (!entry) +return NULL ; + + bpf_bids =kcalloc (nr,sizeof (*bpf_bids),GFP_KERNEL ); +if (!bpf_bids) { +kfree (entry); +return NULL ; + } + +for (u16 i =0 ; i < nr; i++) { + entry->offsets[i] = trace->entries[i]; + bpf_bids[i].ip = trace->entries[i]; + } + +stack_map_get_build_id_offset (bpf_bids, nr, true, true); + + max_size =round_down (U16_MAX -sizeof (struct perf_event_header ) - +2 *sizeof (u64),sizeof (u64)); +perf_callchain_encode_build_ids (entry, entry->offsets, nr,0 , + bpf_bids, nr, max_size); +kfree (bpf_bids); +return entry; +} +static void perf_callchain_deferred_output (struct perf_event *event,void *data) {struct perf_callchain_deferred_event *deferred_event = data; +struct perf_callchain_build_id_entry *bid_entry = deferred_event->bid_entry;struct perf_output_handle handle;struct perf_sample_data sample;u64 nr = deferred_event->trace->nr; -u16 size, id_size, max_payload; +u32 max_offsets =0 , nr_build_ids =0 , nr_offsets =0 ; +u32 raw_bids_len =0 , aligned_bids_len =0 ; +bool use_bid; +u16 size, misc, id_size, max_payload;int ret;if (!event->attr.defer_output)return ; + use_bid =has_defer_output_build_id_offset (event); +if (use_bid && bid_entry) { +if (has_defer_output_callchain_build_id_offset (event)) + max_offsets = bid_entry->nr_offsets; +else + max_offsets =min_t (u32, bid_entry->nr_offsets,1 ); + } + id_size = event->attr.sample_id_all ? event->id_header_size :0 ; max_payload =round_down (U16_MAX -sizeof (deferred_event->event) -sizeof (u64) - id_size,sizeof (u64)); - nr =min_t (u64, nr, max_payload /sizeof (u64)); - size =sizeof (deferred_event->event) +sizeof (u64) + nr *sizeof (u64); + +if (use_bid) { +for (u32 i =0 ; i < max_offsets; i++) { +u8 bid_idx = bid_entry->offsets[i] >>56 ; +u32 next_nr_bids = nr_build_ids; +u32 next_raw_len = raw_bids_len; +u32 next_aligned_len; + +if (bid_idx >0 && bid_idx <= bid_entry->nr_build_ids && + bid_idx > next_nr_bids) { + next_nr_bids = bid_idx; + next_raw_len = next_nr_bids * + (1 +BUILD_ID_SIZE_MAX ); + } + next_aligned_len =ALIGN (next_raw_len,sizeof (u64)); +if (next_aligned_len + (i +1 ) *sizeof (u64) > max_payload) +break ; + nr_build_ids = next_nr_bids; + raw_bids_len = next_raw_len; + aligned_bids_len = next_aligned_len; + nr_offsets = i +1 ; + } + size =sizeof (deferred_event->event) +sizeof (u64) + + aligned_bids_len + nr_offsets *sizeof (u64); + }else { + nr =min_t (u64, nr, max_payload /sizeof (u64)); + size =sizeof (deferred_event->event) +sizeof (u64) + + nr *sizeof (u64); + } + + misc =PERF_RECORD_MISC_USER | + (use_bid ?PERF_RECORD_MISC_MMAP_BUILD_ID :0 );/* XXX do we really need sample_id_all for this ??? */ perf_event_header__init (&deferred_event->event.header, &sample,PERF_RECORD_CALLCHAIN_DEFERRED , -PERF_RECORD_MISC_USER , + misc, size, event);
@@ -10672,11 +10801,27 @@ static void perf_callchain_deferred_output(struct perf_event *event, void *data)return ;perf_output_put (&handle, deferred_event->event); -perf_output_put (&handle, nr); -for (u64 i =0 ; i < nr; i++) { -u64 entry = deferred_event->trace->entries[i]; +if (use_bid) { +u64 pad_zero =0 ; +u32 pad = aligned_bids_len - raw_bids_len; + +perf_output_put (&handle, nr_build_ids); +perf_output_put (&handle, nr_offsets); +if (raw_bids_len) +__output_copy (&handle, bid_entry->build_ids, raw_bids_len); +if (pad) +__output_copy (&handle, &pad_zero, pad); +if (nr_offsets) { +__output_copy (&handle, bid_entry->offsets, + nr_offsets *sizeof (u64)); + } + }else { +perf_output_put (&handle, nr); +for (u64 i =0 ; i < nr; i++) { +u64 entry = deferred_event->trace->entries[i]; -perf_output_put (&handle, entry); +perf_output_put (&handle, entry); + } }perf_event__output_id_sample (event, &handle, &sample);
@@ -10686,14 +10831,18 @@ static void perf_callchain_deferred_output(struct perf_event *event, void *data)static void perf_unwind_deferred_callback (struct unwind_work *work,struct unwind_stacktrace *trace,u64 cookie) { +struct perf_callchain_build_id_entry *bid_entry = +perf_resolve_build_id_offsets (trace);struct perf_callchain_deferred_event deferred_event = { .trace = trace, + .bid_entry = bid_entry, .event = { .cookie = cookie, }, };perf_iterate_sb (perf_callchain_deferred_output, &deferred_event,NULL ); +kfree (bid_entry); }struct perf_text_poke_event {
@@ -13552,6 +13701,10 @@ static void account_event(struct perf_event *event)atomic_inc (&nr_mmap_events);if (event->attr.build_id)atomic_inc (&nr_build_id_events); +if (has_defer_output_build_id_offset (event)) +atomic_inc (&nr_defer_output_build_id_events); +if (has_defer_output_callchain_build_id_offset (event)) +atomic_inc (&nr_defer_output_callchain_build_id_events);if (event->attr.comm)atomic_inc (&nr_comm_events);if (event->attr.namespaces)
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 7/19 perf inject: Set ordered_events and ordering_requires_timestamps for --aslr
In reply to Ian RogersCommit 0ce6cf53db5b ("perf inject: Set ordering_requires_timestamps before creating session") initialized ordered_events and ordering_requires_timestamps on inject.tool before creating delegate tools and the session, but omitted inject.aslr. Also set ordering_requires_timestamps on aslr->tool.tool alongside ordered_events in aslr_tool__init() so --aslr orders MMAP and sample events by timestamp consistently.
--- tools/perf/builtin-inject.c | 4 ++-- tools/perf/util/aslr.c | 1 + 2 files changed, 3 insertions(+), 2 deletions(-)
tools/perf/builtin-inject.c +2 -2
@@ -2810,7 +2810,7 @@ int cmd_inject(int argc, const char **argv) data.path = inject.input_name; - ordered_events = inject.jit_mode || inject.sched_stat || + ordered_events = inject.jit_mode || inject.sched_stat || inject.aslr || inject.itrace_synth_opts.vm_time_correlation || inject.itrace_synth_opts.set || inject.guest_session.perf_data_file ||
@@ -2861,7 +2861,7 @@ int cmd_inject(int argc, const char **argv) inject.tool.schedstat_domain = perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group = true; inject.tool.merge_deferred_callchains = false; - if (inject.jit_mode || + if (inject.jit_mode || inject.aslr || inject.itrace_synth_opts.vm_time_correlation || inject.itrace_synth_opts.set || inject.guest_session.perf_data_file ||
tools/perf/util/aslr.c +1 -0
@@ -1273,6 +1273,7 @@ static int aslr_tool__init(struct aslr_tool *aslr, struct perf_tool *delegate) {delegate_tool__init (&aslr->tool, delegate); aslr->tool.tool.ordered_events = true; + aslr->tool.tool.ordering_requires_timestamps = true;if (machines__init (&aslr->machines,/*session=*/ NULL ))return -ENOMEM ;
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 8/19 perf session: Avoid double byte-swapping re-parsed deferred samples
In reply to Ian RogersWhen perf_session__deliver_event() processes a cross-endian sample, __evsel__parse_sample() is invoked with needs_swap=true and byte-swaps payload regions such as PERF_SAMPLE_RAW, PERF_SAMPLE_BRANCH_STACK, and PERF_SAMPLE_AUX in-place in the event buffer.
If the sample has a deferred callchain and merge_deferred_callchains is enabled, machines__deliver_event() copies that already-parsed event into de->event. Later, evlist__deliver_deferred_callchain() (potentially on multiple iterations before finding a matching tid) and session__flush_deferred_samples() re-parse de->event via evlist__parse_sample(), which runs __evsel__parse_sample() again with evsel->needs_swap still true, double-swapping in-place fields. Meanwhile, pyrf_event__copy() re-parses copied events with needs_swap=false, which misses PERF_SAMPLE_TID, PERF_SAMPLE_CPU, and PERF_SAMPLE_RAW's size word because they were only unswapped into local variables.
Finish unswapping PERF_SAMPLE_TID, PERF_SAMPLE_CPU, and PERF_SAMPLE_RAW's size prefix in-place during the initial cross-endian parse so the entire sample buffer is in host layout afterwards, filter deferred samples by stored tid before re-parsing, and re-parse de->event via evlist__parse_sample_unswapped().
--- tools/perf/util/evlist.c | 38 +++++++++++++++++++++++++++++--------- tools/perf/util/evlist.h | 2 ++ tools/perf/util/evsel.c | 10 +++++++++- tools/perf/util/session.c | 14 +++++++------- 4 files changed, 47 insertions(+), 17 deletions(-)
tools/perf/util/evlist.c +29 -9
@@ -1935,17 +1935,12 @@ void evlist__cancel_workload(struct evlist *evlist) } } -int evlist__parse_sample (struct evlist *evlist,union perf_event *event,struct perf_sample *sample) +static int __evlist__parse_sample (struct evlist *evlist,struct evsel *evsel, +union perf_event *event, +struct perf_sample *sample,bool needs_swap) { -struct evsel *evsel =evlist__event2evsel (evlist, event); -int ret; +int ret =__evsel__parse_sample (evsel, event, sample, needs_swap); -if (!evsel) { -/* Ensure the sample is okay for perf_sample__exit. */ -perf_sample__init (sample,/*all=*/ false); -return -EFAULT ; - } - ret =evsel__parse_sample (evsel, event, sample);if (ret)return ret;if (perf_guest && sample->id) {
@@ -1959,6 +1954,31 @@ int evlist__parse_sample(struct evlist *evlist, union perf_event *event, structreturn 0 ; } +int evlist__parse_sample (struct evlist *evlist,union perf_event *event,struct perf_sample *sample) +{ +struct evsel *evsel =evlist__event2evsel (evlist, event); + +if (!evsel) { +/* Ensure the sample is okay for perf_sample__exit. */ +perf_sample__init (sample,/*all=*/ false); +return -EFAULT ; + } +return __evlist__parse_sample (evlist, evsel, event, sample, evsel->needs_swap); +} + +int evlist__parse_sample_unswapped (struct evlist *evlist,union perf_event *event, +struct perf_sample *sample) +{ +struct evsel *evsel =evlist__event2evsel (evlist, event); + +if (!evsel) { +/* Ensure the sample is okay for perf_sample__exit. */ +perf_sample__init (sample,/*all=*/ false); +return -EFAULT ; + } +return __evlist__parse_sample (evlist, evsel, event, sample,/*needs_swap=*/ false); +} +int evlist__parse_sample_timestamp (struct evlist *evlist,union perf_event *event,u64 *timestamp) {struct evsel *evsel =evlist__event2evsel (evlist, event);
tools/perf/util/evlist.h +2 -0
@@ -450,6 +450,8 @@ bool evlist__sample_id_all(struct evlist *evlist);u16 evlist__id_hdr_size (struct evlist *evlist);int evlist__parse_sample (struct evlist *evlist,union perf_event *event,struct perf_sample *sample); +int evlist__parse_sample_unswapped (struct evlist *evlist,union perf_event *event, +struct perf_sample *sample);int evlist__parse_sample_timestamp (struct evlist *evlist,union perf_event *event,u64 *timestamp);bool evlist__valid_sample_type (struct evlist *evlist);
tools/perf/util/evsel.c +9 -1
@@ -3520,6 +3520,7 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, u.val64 =bswap_64 (u.val64); u.val32[0 ] =bswap_32 (u.val32[0 ]); u.val32[1 ] =bswap_32 (u.val32[1 ]); + *(__u64 *)array = u.val64; } data->pid = u.val32[0 ];
@@ -3554,6 +3555,8 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,/* undo swap of u64, then swap on individual u32s */ u.val64 =bswap_64 (u.val64); u.val32[0 ] =bswap_32 (u.val32[0 ]); + u.val32[1 ] =bswap_32 (u.val32[1 ]); + *(__u64 *)array = u.val64; } data->cpu = u.val32[0 ];
@@ -3665,7 +3668,12 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,/* mem_bswap_64() accesses complete 64-bit words. */ sz =roundup ((u64 )data->raw_size,sizeof (u64));OVERFLOW_CHECK (raw, sz, max_size); -mem_bswap_64 ((void *)raw, data->raw_size); +if (data->raw_size) { +mem_bswap_64 ((void *)raw, data->raw_size); + *(u32 *)raw = data->raw_size; + }else { + *(__u64 *)raw = u.val64; + } } data->raw_data = (void *)array; array = (void *)array + data->raw_size;
tools/perf/util/session.c +7 -7
@@ -1933,6 +1933,7 @@ struct deferred_event {struct list_head list;union perf_event *event;u64 file_offset; +u32 tid; };/*
@@ -1964,8 +1965,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,struct perf_sample orig_sample;struct evsel *new_evsel; +if (sample->tid != de->tid) +continue ; +perf_sample__init (&orig_sample,/*all=*/ false); - ret =evlist__parse_sample (evlist, de->event, &orig_sample); + ret =evlist__parse_sample_unswapped (evlist, de->event, &orig_sample);if (ret <0 ) {pr_err ("failed to parse original sample\n" );perf_sample__exit (&orig_sample);
@@ -1973,11 +1977,6 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist, } orig_sample.file_offset = de->file_offset; -if (sample->tid != orig_sample.tid) { -perf_sample__exit (&orig_sample); -continue ; - } -if (event->callchain_deferred.cookie == orig_sample.deferred_cookie)sample__merge_deferred_callchain (&orig_sample, sample);else
@@ -2027,7 +2026,7 @@ static int session__flush_deferred_samples(struct perf_session *session, }perf_sample__init (&sample,/*all=*/ false); - ret =evlist__parse_sample (evlist, de->event, &sample); + ret =evlist__parse_sample_unswapped (evlist, de->event, &sample);if (ret <0 ) {pr_err ("failed to parse original sample\n" );perf_sample__exit (&sample);
@@ -2120,6 +2119,7 @@ static int machines__deliver_event(struct machines *machines, }memcpy (de->event, event, sz); de->file_offset = sample->file_offset; + de->tid = sample->tid;list_add_tail (&de->list,evlist__deferred_samples (evlist));return 0 ; }
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 9/19 perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in aslr_tool__process_sample()
In reply to Ian RogersIn aslr_tool__process_sample(), PERF_SAMPLE_READ only copied the id word when evsel->core.attr.read_format had PERF_FORMAT_ID set, whereas the kernel and __evsel__parse_sample() unconditionally include the id word in both single-counter and PERF_FORMAT_GROUP modes. When PERF_FORMAT_ID was not explicitly set in attr.read_format, in_array[i] became desynchronized with subsequent sample fields.
In addition, CHECK_BOUNDS() used addition (i + required_i > max_i) which could overflow, and the dropped PERF_SAMPLE_RAW, PERF_SAMPLE_STACK_USER, and PERF_SAMPLE_AUX handlers copied word-rounded lengths into out_array before discarding the sample.
Unconditionally copy the id word in PERF_SAMPLE_READ using sample_read_value_size() with division-based bounds checks, make CHECK_BOUNDS() subtraction-based, and drop RAW/STACK_USER/AUX samples directly without dead copies.
--- tools/perf/util/aslr.c | 95 +++++++++--------------------------------- 1 file changed, 20 insertions(+), 75 deletions(-)
tools/perf/util/aslr.c +20 -75
@@ -8,6 +8,7 @@#include "evlist.h" #include "machine.h" #include "map.h" +#include "sample.h" #include "thread.h" #include "tool.h" #include "session.h"
@@ -780,7 +781,7 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, out_array = &new_event->sample.array[0 ];#define CHECK_BOUNDS (required_i, required_j) \ - (i + (required_i) > max_i || j + (required_j) > max_j) + ((required_i) > max_i - i || (required_j) > max_j - j)#define COPY_U64 () \ do { \
@@ -835,18 +836,20 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,if (orig_sample_type &PERF_SAMPLE_PERIOD )COPY_U64 ();/* period */ if (orig_sample_type &PERF_SAMPLE_READ ) { -if ((evsel->core.attr.read_format &PERF_FORMAT_GROUP ) ==0 ) { +u64 read_format = evsel->core.attr.read_format; + +if ((read_format &PERF_FORMAT_GROUP ) ==0 ) {COPY_U64 ();/* value */ -if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +if (read_format &PERF_FORMAT_TOTAL_TIME_ENABLED )COPY_U64 ();/* time_enabled */ -if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_RUNNING ) +if (read_format &PERF_FORMAT_TOTAL_TIME_RUNNING )COPY_U64 ();/* time_running */ -if (evsel->core.attr.read_format &PERF_FORMAT_ID ) -COPY_U64 ();/* id */ -if (evsel->core.attr.read_format &PERF_FORMAT_LOST ) +/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ +COPY_U64 ();/* id */ +if (read_format &PERF_FORMAT_LOST )COPY_U64 ();/* lost */ }else { -u64 nr; +u64 nr, entry_words;if (CHECK_BOUNDS (1 ,1 )) { ret = -EFAULT ;
@@ -854,17 +857,18 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, } nr = in_array[i];COPY_U64 (); -if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +if (read_format &PERF_FORMAT_TOTAL_TIME_ENABLED )COPY_U64 ();/* time_enabled */ -if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_RUNNING ) +if (read_format &PERF_FORMAT_TOTAL_TIME_RUNNING )COPY_U64 ();/* time_running */ -for (u64 cntr =0 ; cntr < nr; cntr++) { -COPY_U64 ();/* value */ -if (evsel->core.attr.read_format &PERF_FORMAT_ID ) -COPY_U64 ();/* id */ -if (evsel->core.attr.read_format &PERF_FORMAT_LOST ) -COPY_U64 ();/* lost */ + entry_words =sample_read_value_size (read_format) /sizeof (u64); +if (nr > (max_i - i) / entry_words || + nr > (max_j - j) / entry_words) { + ret = -EFAULT ; + goto out_put; } +for (u64 cntr =0 ; cntr < nr * entry_words; cntr++) + out_array[j++] = in_array[i++]; } }if (orig_sample_type &PERF_SAMPLE_CALLCHAIN ) {
@@ -934,16 +938,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, } }if (orig_sample_type &PERF_SAMPLE_RAW ) { -size_t bytes =sizeof (u32) + sample->raw_size; -size_t u64_words = (bytes +7 ) /8 ; - -if (i + u64_words > max_i || j + u64_words > max_j) { - ret = -EFAULT ; - goto out_put; - } -memcpy (&out_array[j], &in_array[i], bytes); - i += u64_words; - j += u64_words;/* * TODO: certain raw samples can be remapped, such as * tracepoints by examining their fields.
@@ -1041,30 +1035,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, } }if (orig_sample_type &PERF_SAMPLE_STACK_USER ) { -u64 size; - -if (CHECK_BOUNDS (1 ,1 )) { - ret = -EFAULT ; - goto out_put; - } - size = in_array[i]; -COPY_U64 (); -if (size >0 ) { -size_t u64_words = size /8 + (size %8 ?1 :0 ); - -if (u64_words > max_i - i || u64_words > max_j - j) { - ret = -EFAULT ; - goto out_put; - } -memcpy (&out_array[j], &in_array[i], size); -if (size %8 ) { -size_t pad =8 - (size %8 ); - -memset (((char *)&out_array[j]) + size,0 , pad); - } - i += u64_words; - j += u64_words; - }/* TODO: can this be less conservative? */ pr_debug ("Dropping stack user sample as possible ASLR leak\n" ); ret =0 ;
@@ -1110,31 +1080,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,COPY_U64 ();/* code_page_size */ if (orig_sample_type &PERF_SAMPLE_AUX ) { -u64 size; - -if (CHECK_BOUNDS (1 ,1 )) { - ret = -EFAULT ; - goto out_put; - } - out_array[j] = in_array[i]; - size = out_array[j++]; - i++; -if (size >0 ) { -size_t u64_words = size /8 + (size %8 ?1 :0 ); - -if (u64_words > max_i - i || u64_words > max_j - j) { - ret = -EFAULT ; - goto out_put; - } -memcpy (&out_array[j], &in_array[i], size); -if (size %8 ) { -size_t pad =8 - (size %8 ); - -memset (((char *)&out_array[j]) + size,0 , pad); - } - i += u64_words; - j += u64_words; - }/* TODO: can this be less conservative? */ pr_debug ("Dropping aux sample as possible ASLR leak\n" ); ret =0 ;
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 10/19 tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id
In reply to Ian RogersSynchronize tools/include/uapi/linux/perf_event.h, define struct perf_build_id in tools/lib/perf/include/perf/event.h (keeping the existing anonymous build_id_size/build_id fields in struct perf_record_mmap2 for source compatibility), and update perf tools to use struct perf_build_id.
--- tools/include/uapi/linux/perf_event.h | 13 ++++++++++ tools/lib/perf/include/perf/event.h | 14 +++++++++-- 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 | 36 ++++++++++++++------------- 7 files changed, 54 insertions(+), 27 deletions(-)
tools/include/uapi/linux/perf_event.h +13 -0
@@ -1099,6 +1099,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 perf_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
@@ -1123,6 +1135,7 @@ enum perf_event_type {* u16 __reserved_2; * u8 build_id[20]; * }; +* struct perf_build_id bid; * }; * u32 prot, flags; * char filename[];
tools/lib/perf/include/perf/event.h +12 -2
@@ -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;
@@ -43,8 +52,9 @@ struct perf_record_mmap2 {__u8 build_id_size;__u8 __reserved_1;__u16 __reserved_2; -__u8 build_id[20 ]; +__u8 build_id[PERF_BUILD_ID_SIZE ]; }; +struct perf_build_id bid; };__u32 prot;__u32 flags;
@@ -321,7 +331,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__;
tools/perf/builtin-inject.c +2 -1
@@ -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.bid.data, + event->mmap2.bid.size); }else { id.maj = event->mmap2.maj; id.min = event->mmap2.min;
tools/perf/util/event.c +2 -2
@@ -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.bid.data, + event->mmap2.bid.size);build_id__snprintf (&bid, sbuild_id,sizeof (sbuild_id)); return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
tools/perf/util/machine.c +2 -1
@@ -1828,7 +1828,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.bid.data, + event->mmap2.bid.size); }else { dso_id.maj = event->mmap2.maj; dso_id.min = event->mmap2.min;
tools/perf/util/python.c +4 -4
@@ -302,12 +302,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.bid.size; -if (size >20 ) - size =20 ; +if (size >sizeof (pevent->event->mmap2.bid.data)) + size =sizeof (pevent->event->mmap2.bid.data); -return PyBytes_FromStringAndSize ((const char *)pevent->event->mmap2.build_id, size); +return PyBytes_FromStringAndSize ((const char *)pevent->event->mmap2.bid.data, size); }static PyGetSetDef pyrf_mmap2_event__getset[] = {
tools/perf/util/synthetic-events.c +19 -17
@@ -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->bid.data, event->bid.size); }else { dso_id.maj = event->maj; dso_id.min = event->min;
@@ -479,11 +479,11 @@ static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event, out:if (rc ==0 ) { -memcpy (event->build_id, bid.data,sizeof (bid.data)); - event->build_id_size = (u8 ) bid.size; +memcpy (event->bid.data, bid.data,sizeof (bid.data)); + event->bid.size = (u8 ) bid.size; event->header.misc |=PERF_RECORD_MISC_MMAP_BUILD_ID ; - event->__reserved_1 =0 ; - event->__reserved_2 =0 ; + event->bid.__reserved_1 =0 ; + event->bid.__reserved_2 =0 ;if (dso && !dso__has_build_id (dso))dso__set_build_id (dso, &bid);
@@ -611,8 +611,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 +809,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.bid.data,0 ,sizeof (event->mmap2.bid.data)); + event->mmap2.bid.size =0 ; + event->mmap2.bid.__reserved_1 =0 ; + event->mmap2.bid.__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);
@@ -1299,7 +1301,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 ;
@@ -2530,10 +2532,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.bid.size = bid->size; +if (ev.mmap2.bid.size >sizeof (ev.mmap2.bid.data)) + ev.mmap2.bid.size =sizeof (ev.mmap2.bid.data); +memcpy (ev.mmap2.bid.data, bid->data, ev.mmap2.bid.size); ev.mmap2.prot = prot; ev.mmap2.flags = flags;
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 11/19 tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET
In reply to Ian RogersSynchronize tools/include/uapi/linux/perf_event.h with the kernel UAPI header for PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET, and add struct perf_sample_build_id_offset and the struct perf_record_callchain_deferred build ID union fields to tools/lib/perf/include/perf/event.h.
--- tools/include/uapi/linux/perf_event.h | 59 ++++++++++++++++++++++++--- tools/lib/perf/include/perf/event.h | 29 ++++++++++++- 2 files changed, 81 insertions(+), 7 deletions(-)
tools/include/uapi/linux/perf_event.h +54 -5
@@ -165,8 +165,10 @@ enum perf_event_sample_format {PERF_SAMPLE_DATA_PAGE_SIZE =1U <<22 ,PERF_SAMPLE_CODE_PAGE_SIZE =1U <<23 ,PERF_SAMPLE_WEIGHT_STRUCT =1U <<24 , +PERF_SAMPLE_BUILD_ID_OFFSET =1U <<25 , +PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET =1U <<26 , - PERF_SAMPLE_MAX = 1U << 25, /* non-ABI */ + PERF_SAMPLE_MAX = 1U << 27, /* non-ABI */ };#define PERF_SAMPLE_WEIGHT_TYPE (PERF_SAMPLE_WEIGHT | PERF_SAMPLE_WEIGHT_STRUCT)
@@ -840,7 +842,8 @@ struct perf_event_mmap_page {* * PERF_RECORD_MISC_EXACT_IP - PERF_RECORD_SAMPLE of precise events * PERF_RECORD_MISC_SWITCH_OUT_PREEMPT - PERF_RECORD_SWITCH* events -* PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 event +* PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 and +* PERF_RECORD_CALLCHAIN_DEFERRED events * * * PERF_RECORD_MISC_EXACT_IP:
@@ -852,7 +855,7 @@ struct perf_event_mmap_page {* Indicates that thread was preempted in TASK_RUNNING state. * * PERF_RECORD_MISC_MMAP_BUILD_ID: -* Indicates that mmap2 event carries build ID data. +* Indicates that mmap2 or deferred callchain event carries build ID data. */ #define PERF_RECORD_MISC_EXACT_IP (1 << 14)#define PERF_RECORD_MISC_SWITCH_OUT_PREEMPT (1 << 14)
@@ -1095,6 +1098,38 @@ enum perf_event_type {* { u64 code_page_size;} && PERF_SAMPLE_CODE_PAGE_SIZE * { u64 size; * char data[size]; } && PERF_SAMPLE_AUX +* { union { +* struct { +* struct perf_build_id bid; +* u64 offset; +* }; +* struct { +* u64 zero; +* u64 context; +* u64 cookie; +* u64 ip; +* }; && attr.defer_callchain +* }; } && PERF_SAMPLE_BUILD_ID_OFFSET +* { u32 nr_build_ids; +* u32 nr_offsets; +* struct { +* u8 size; +* u8 data[size]; +* }[nr_build_ids]; +* u8 pad[]; +* u64 offsets[nr_offsets]; +* # +* # Entries in offsets[] include PERF_CONTEXT_* markers (which +* # have top 8 bits 0xff). Kernel/hypervisor entries (after +* # PERF_CONTEXT_KERNEL, PERF_CONTEXT_HV, PERF_CONTEXT_GUEST, +* # or PERF_CONTEXT_GUEST_KERNEL) and the cookie word following +* # PERF_CONTEXT_USER_DEFERRED are raw 64-bit values. User +* # entries (after PERF_CONTEXT_USER or PERF_CONTEXT_GUEST_USER, +* # or in PERF_RECORD_CALLCHAIN_DEFERRED) use the top 8 bits to +* # encode a 1-based index (1..254) into the build_ids table and +* # the lower 56 bits for the DSO file offset, or 0x00 for an +* # unresolved user virtual address. +* } && PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET * }; */ PERF_RECORD_SAMPLE = 9,
@@ -1306,8 +1341,22 @@ enum perf_event_type {* struct { * struct perf_event_header header; * u64 cookie; -* u64 nr; -* u64 ips[nr]; +* union { +* struct { +* u64 nr; +* u64 ips[nr]; +* }; +* struct { +* u32 nr_build_ids; +* u32 nr_offsets; +* struct { +* u8 size; +* u8 data[size]; +* }[nr_build_ids]; +* u8 pad[]; +* u64 offsets[nr_offsets]; +* }; && (header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) +* }; * struct sample_id sample_id; * }; */
tools/lib/perf/include/perf/event.h +27 -2
@@ -3,6 +3,7 @@#define __LIBPERF_EVENT_H#include <linux/perf_event.h> +#include <linux/stddef.h> #include <linux/types.h> #include <linux/limits.h> #include <linux/bpf.h>
@@ -168,6 +169,21 @@ struct perf_record_switch {__u32 next_prev_tid; }; +struct perf_sample_build_id_offset { +union { +struct perf_build_id bid; +struct { +__u64 zero; +__u64 context; +__u64 cookie; + }; + }; +union { +__u64 offset; +__u64 ip; + }; +}; +struct perf_record_callchain_deferred {struct perf_event_header header;/*
@@ -176,8 +192,17 @@ struct perf_record_callchain_deferred {* the PERF_CONTEXT_USER_DEFERRED entry. */ __u64 cookie; -__u64 nr; -__u64 ips[]; +union { +struct { +__u64 nr; +__u64 ips[]; + }; +struct { +__u32 nr_build_ids; +__u32 nr_offsets; +__u8 build_ids[]; + }; + }; };struct perf_record_header_attr {
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 12/19 perf: Refactor thread map and symbol APIs to take perf_sample
In reply to Ian RogersRefactor thread__find_map and thread__find_symbol family of APIs to take a struct perf_sample * instead of cpumode and addr. This prepares for looking up symbols using build ID and offset, which is contained in the sample payload instead of the address.
--- .../arch/powerpc/util/skip-callchain-idx.c | 4 +- tools/perf/builtin-inject.c | 2 +- tools/perf/builtin-script.c | 48 +++++++++++++---- tools/perf/builtin-timechart.c | 5 +- tools/perf/builtin-trace.c | 10 ++-- tools/perf/tests/code-reading.c | 6 ++- tools/perf/tests/mmap-thread-lookup.c | 9 +++- tools/perf/util/annotate-data.c | 5 +- tools/perf/util/aslr.c | 46 +++++++++++------ tools/perf/util/build-id.c | 2 +- tools/perf/util/capstone.c | 4 +- tools/perf/util/cs-etm.c | 10 +++- tools/perf/util/data-convert-json.c | 4 +- tools/perf/util/debug.c | 6 ++- tools/perf/util/dlfilter.c | 8 ++- tools/perf/util/event.c | 51 ++++++++++--------- tools/perf/util/intel-pt.c | 24 +++++++-- tools/perf/util/machine.c | 18 +++++-- tools/perf/util/python.c | 18 +++++-- tools/perf/util/thread.c | 11 ++-- tools/perf/util/thread.h | 14 ++--- tools/perf/util/unwind-libdw.c | 7 ++- tools/perf/util/unwind-libunwind.c | 8 ++- 23 files changed, 224 insertions(+), 96 deletions(-)
tools/perf/arch/powerpc/util/skip-callchain-idx.c +3 -1
@@ -225,7 +225,9 @@ int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)addr_location__init (&al); ip = chain->ips[1 ]; -thread__find_symbol (thread,PERF_RECORD_MISC_USER , ip, &al); +thread__find_symbol (thread, + &(struct perf_sample ){.cpumode =PERF_RECORD_MISC_USER , .ip = ip}, + &al);if (al.map) dso =map__dso (al.map);
tools/perf/builtin-inject.c +1 -1
@@ -1167,7 +1167,7 @@ static int perf_event__inject_buildid(const struct perf_tool *tool, union perf_e goto repipe; } -if (thread__find_map (thread, sample->cpumode, sample->ip, &al)) { +if (thread__find_map (thread, sample, &al)) {mark_dso_hit (inject, tool, sample, machine, args.mmap_evsel, al.map,/*sample_in_dso=*/ true); }
tools/perf/builtin-script.c +38 -10
111 lines not on this page · raw message
tools/perf/builtin-timechart.c +4 -1
@@ -517,6 +517,7 @@ static char *cat_backtrace(struct perf_sample *sample,size_t p_len;u8 cpumode =PERF_RECORD_MISC_USER ;struct ip_callchain *chain = sample->callchain; +struct perf_sample ip_sample = {};FILE *f =open_memstream (&p, &p_len);bool corrupted = false;
@@ -565,7 +566,9 @@ static char *cat_backtrace(struct perf_sample *sample,addr_location__init (&tal); tal.filtered =0 ; -if (thread__find_symbol (al.thread, cpumode, ip, &tal)) + ip_sample.cpumode = cpumode; + ip_sample.ip = ip; +if (thread__find_symbol (al.thread, &ip_sample, &tal))fprintf (f,"..... %016" PRIx64" %s\n" , ip, tal.sym->name);else fprintf (f,"..... %016" PRIx64"\n" , ip);
tools/perf/builtin-trace.c +7 -3
@@ -3718,7 +3718,7 @@ static int trace__pgfault(struct trace *trace,if (trace->summary_only) goto out; -thread__find_symbol (thread, sample->cpumode, sample->ip, &al); +thread__find_symbol (thread, sample, &al);trace__fprintf_entry_head (trace, thread,0 , true, sample->time, sample->cpu, trace->output);
@@ -3731,10 +3731,14 @@ static int trace__pgfault(struct trace *trace,fprintf (trace->output,"] => " ); -thread__find_symbol (thread, sample->cpumode, sample->addr, &al); +thread__find_symbol (thread, + &(struct perf_sample ){.cpumode = sample->cpumode, + .ip = sample->addr}, &al);if (!al.map) { -thread__find_symbol (thread, sample->cpumode, sample->addr, &al); +thread__find_symbol (thread, + &(struct perf_sample ){.cpumode = sample->cpumode, + .ip = sample->addr}, &al);if (al.map) map_type ='x' ;
tools/perf/tests/code-reading.c +5 -1
@@ -387,6 +387,10 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,u64 skip_addr;const char *objdump_name;char decomp_name[KMOD_DECOMP_LEN ]; +struct perf_sample sample = { + .cpumode = cpumode, + .ip = addr, + };bool decomp = false;int ret, err =0 ;struct dso *dso;
@@ -394,7 +398,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,pr_debug ("Reading object code for memory address: %#" PRIx64"\n" , addr);addr_location__init (&al); -if (!thread__find_map (thread, cpumode, addr, &al) || !map__dso (al.map)) { +if (!thread__find_map (thread, &sample, &al) || !map__dso (al.map)) {if (cpumode ==PERF_RECORD_MISC_HYPERVISOR ) {pr_debug ("Hypervisor address can not be resolved - skipping\n" ); goto out;
tools/perf/tests/mmap-thread-lookup.c +7 -2
@@ -195,8 +195,10 @@ static int mmap_events(synth_cb synth)pr_debug ("looking for map %p\n" , td->map); -thread__find_map (thread,PERF_RECORD_MISC_USER , - (unsigned long ) (td->map +1 ), &al); +thread__find_map (thread, &(struct perf_sample ){ + .cpumode =PERF_RECORD_MISC_USER , + .ip = (unsigned long ) (td->map +1 ), + }, &al);thread__put (thread);
@@ -208,7 +210,10 @@ static int mmap_events(synth_cb synth) }pr_debug ("map %p, addr %" PRIx64"\n" , al.map,map__start (al.map)); +addr_location__exit (&al); +if (err) +break ; }machine__delete (machine);
tools/perf/util/annotate-data.c +3 -2
@@ -882,8 +882,9 @@ bool get_global_var_info(struct data_loc_info *dloc, u64 addr, mem_addr = addr +map__reloc (dloc->ms->map);addr_location__init (&al); - sym =thread__find_symbol_fb (dloc->thread, dloc->cpumode, - mem_addr, &al); + sym =thread__find_symbol_fb (dloc->thread, + &(struct perf_sample ){.cpumode = dloc->cpumode, + .ip = mem_addr}, &al);if (sym) { *var_name = sym->name;/* Calculate type offset from the start of variable */
tools/perf/util/aslr.c +31 -15
82 lines not on this page · raw message
tools/perf/util/build-id.c +1 -1
@@ -69,7 +69,7 @@ int build_id__mark_dso_hit(const struct perf_tool *tool __maybe_unused, }addr_location__init (&al); -if (thread__find_map (thread, sample->cpumode, sample->ip, &al)) +if (thread__find_map (thread, sample, &al))dso__set_hit (map__dso (al.map));addr_location__exit (&al);
tools/perf/util/capstone.c +3 -1
@@ -252,7 +252,9 @@ static size_t print_insn_x86(struct thread *thread, u8 cpumode, struct cs_insn *addr_location__init (&al);if (op->type ==X86_OP_IMM && -thread__find_symbol (thread, cpumode, op->imm, &al)) { +thread__find_symbol (thread, + &(struct perf_sample ){.cpumode = cpumode, + .ip = op->imm}, &al)) { printed +=fprintf (fp,"%s " , insn[0 ].mnemonic); printed +=symbol__fprintf_symname_offs (al.sym, &al, fp);if (print_opts &PRINT_INSN_IMM_HEX )
tools/perf/util/cs-etm.c +8 -2
@@ -1182,9 +1182,15 @@ static u32 __cs_etm__mem_access(struct cs_etm_queue *etmq, } cpumode =cs_etm__cpu_mode (etmq, address, el); + { +struct perf_sample sample = { + .cpumode = cpumode, + .ip = address, + }; -if (!thread__find_map (thread, cpumode, address, &al)) - goto out; +if (!thread__find_map (thread, &sample, &al)) + goto out; + } dso =map__dso (al.map);if (!dso)
tools/perf/util/data-convert-json.c +3 -1
@@ -235,7 +235,9 @@ static int process_sample_event(const struct perf_tool *tool,fputc (',' , out);addr_location__init (&tal); - ok =thread__find_symbol (al.thread, cpumode, ip, &tal); + ok =thread__find_symbol (al.thread, + &(struct perf_sample ){.cpumode = cpumode, + .ip = ip}, &tal);output_sample_callchain_entry (tool, ip, ok ? &tal :NULL );addr_location__exit (&tal); }
tools/perf/util/debug.c +5 -1
@@ -336,10 +336,14 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)for (size_t i =0 ; i < stackdump_size; i++) {struct addr_location al;u64 addr = (u64 )(uintptr_t )stackdump[i]; +struct perf_sample sample = { + .cpumode =PERF_RECORD_MISC_USER , + .ip = addr, + };bool printed = false;addr_location__init (&al); -if (thread &&thread__find_map (thread,PERF_RECORD_MISC_USER , addr, &al)) { +if (thread &&thread__find_map (thread, &sample, &al)) { al.sym =map__find_symbol (al.map, al.addr);if (al.sym) {fprintf (file," #%zd %p in %s " , i, stackdump[i], al.sym->name);
tools/perf/util/dlfilter.c +6 -2
@@ -177,7 +177,9 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlfreturn -1 ;addr_location__init (&al); -thread__find_symbol_fb (thread, d->sample->cpumode, address, &al); +thread__find_symbol_fb (thread, + &(struct perf_sample ){.cpumode = d->sample->cpumode, + .ip = address}, &al);al_to_d_al (&al, &d_al);
@@ -313,7 +315,9 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)addr_location__init (&a); -thread__find_map_fb (al->thread, d->sample->cpumode, ip, &a); +thread__find_map_fb (al->thread, + &(struct perf_sample ){.cpumode = d->sample->cpumode, + .ip = ip}, &a); ret = a.map ?code_read (ip, a.map, d->machine, buf, len) :-1 ;addr_location__exit (&a);
tools/perf/util/event.c +27 -24
112 lines not on this page · raw message
tools/perf/util/intel-pt.c +20 -4
@@ -752,6 +752,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, {struct intel_pt_queue *ptq = data;struct machine *machine = ptq->pt->machine; +struct perf_sample sample = {};struct thread *thread;struct addr_location al;unsigned char buf[INTEL_PT_INSN_BUF_SZ ];
@@ -809,10 +810,13 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, } } + sample.cpumode = cpumode; +while (1 ) {struct dso *dso; -if (!thread__find_map (thread, cpumode, *ip, &al) || !map__dso (al.map)) { + sample.ip = *ip; +if (!thread__find_map (thread, &sample, &al) || !map__dso (al.map)) {if (al.map)intel_pt_log ("ERROR: thread has no dso for %#" PRIx64"\n" , *ip);else
@@ -1007,8 +1011,15 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data)return -EINVAL ;addr_location__init (&al); -if (!thread__find_map (thread, cpumode, ip, &al) || !map__dso (al.map)) -return -EINVAL ; + { +struct perf_sample sample = { + .cpumode = cpumode, + .ip = ip, + }; + +if (!thread__find_map (thread, &sample, &al) || !map__dso (al.map)) +return -EINVAL ; + } offset =map__map_ip (al.map, ip);
@@ -3663,7 +3674,12 @@ static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr, struct addr_location *al) {if (!al->map || addr <map__start (al->map) || addr >=map__end (al->map)) { -if (!thread__find_map (thread, cpumode, addr, al)) +struct perf_sample sample = { + .cpumode = cpumode, + .ip = addr, + }; + +if (!thread__find_map (thread, &sample, al))return -1 ; }
tools/perf/util/machine.c +13 -5
@@ -2093,6 +2093,7 @@ static void ip__resolve_ams(struct thread *thread, struct addr_map_symbol *ams, u64 ip) { +static __threadstruct perf_sample sample;struct addr_location al;addr_location__init (&al);
@@ -2103,7 +2104,8 @@ static void ip__resolve_ams(struct thread *thread,* Thus, we have to try consecutively until we find a match * or else, the symbol is unknown */ -thread__find_cpumode_addr_location (thread, ip,/*symbols=*/ true, &al); + sample.ip = ip; +thread__find_cpumode_addr_location (thread, &sample,/*symbols=*/ true, &al); ams->addr = ip; ams->al_addr = al.addr;
@@ -2120,11 +2122,14 @@ static void ip__resolve_data(struct thread *thread, u8 m, struct addr_map_symbol *ams, u64 addr, u64 phys_addr, u64 daddr_page_size) { + static __thread struct perf_sample sample;struct addr_location al;addr_location__init (&al); -thread__find_symbol (thread, m, addr, &al); + sample.cpumode = m; + sample.ip = addr; +thread__find_symbol (thread, &sample, &al); ams->addr = addr; ams->al_addr = al.addr;
@@ -2247,6 +2252,7 @@ static int add_callchain_ip(struct thread *thread, u64 branch_from, bool symbols) { +static __threadstruct perf_sample sample;struct map_symbol ms = {};struct addr_location al;int nr_loop_iter =0 , err =0 ;
@@ -2257,8 +2263,9 @@ static int add_callchain_ip(struct thread *thread, al.filtered =0 ; al.sym =NULL ; al.srcline =NULL ; + sample.ip = ip;if (!cpumode) { -thread__find_cpumode_addr_location (thread, ip, symbols, &al); +thread__find_cpumode_addr_location (thread, &sample, symbols, &al); }else {if (ip >=PERF_CONTEXT_MAX ) {switch (ip) {
@@ -2287,10 +2294,11 @@ static int add_callchain_ip(struct thread *thread, } goto out; } + sample.cpumode = *cpumode;if (symbols) -thread__find_symbol (thread, *cpumode, ip, &al); +thread__find_symbol (thread, &sample, &al);else -thread__find_map (thread, *cpumode, ip, &al); +thread__find_map (thread, &sample, &al); }if (al.sym !=NULL ) {
tools/perf/util/python.c +14 -4
@@ -877,7 +877,10 @@ static PyObject *pyrf_sample_event__srccode(PyObject *self, PyObject *args)if (addr != pevent->sample->ip) {addr_location__init (&al); -thread__find_symbol_fb (pevent->al.thread, pevent->sample->cpumode, addr, &al); +thread__find_symbol_fb (pevent->al.thread, + &(struct perf_sample ){ + .cpumode = pevent->sample->cpumode, + .ip = addr}, &al); }else {addr_location__init (&al); al.thread =thread__get (pevent->al.thread);
@@ -1303,8 +1306,12 @@ static int pyrf_sample_event__resolve_addr_al(struct pyrf_event *pevent,if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.thread)return -1 ; -thread__find_symbol_fb (pevent->al.thread, pevent->sample->cpumode, - pevent->sample->addr, addr_al); +thread__find_symbol_fb (pevent->al.thread, + &(struct perf_sample ){ + .cpumode = pevent->sample->cpumode, + .ip = pevent->sample->addr, + }, + addr_al);return 0 ; }
@@ -4481,6 +4488,7 @@ static PyObject *pyrf_call_path__to_callchain(const struct call_path *cp, struct thread *thread) {struct pyrf_callchain *pchain; +struct perf_sample sample = {};const struct call_path *pos;u64 nr_frames =0 ;
@@ -4513,7 +4521,9 @@ static PyObject *pyrf_call_path__to_callchain(const struct call_path *cp, : PERF_RECORD_MISC_USER;addr_location__init (&al); -thread__find_map (thread, cpumode, pos->ip, &al); + sample.cpumode = cpumode; + sample.ip = pos->ip; +thread__find_map (thread, &sample, &al); frame->map =map__get (al.map);if (frame->map) frame->sym = pos->sym;
tools/perf/util/thread.c +7 -4
@@ -413,7 +413,7 @@ int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, boreturn thread__clone_maps (thread, parent, do_maps_clone); } -void thread__find_cpumode_addr_location (struct thread *thread,u64 addr, +void thread__find_cpumode_addr_location (struct thread *thread,struct perf_sample *sample,bool symbols,struct addr_location *al) {size_t i;
@@ -425,10 +425,11 @@ void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, };for (i =0 ; i <ARRAY_SIZE (cpumodes); i++) { + sample->cpumode = cpumodes[i];if (symbols) -thread__find_symbol (thread, cpumodes[i], addr, al); +thread__find_symbol (thread, sample, al);else -thread__find_map (thread, cpumodes[i], addr, al); +thread__find_map (thread, sample, al);if (al->map)break ;
@@ -579,6 +580,7 @@ int thread__memcpy(struct thread *thread, struct machine *machine, void *buf, u64 ip, int len, bool *is64bit) { u8 cpumode = PERF_RECORD_MISC_USER; +struct perf_sample sample = { .ip = ip };struct addr_location al;struct dso *dso;long offset;
@@ -587,7 +589,8 @@ int thread__memcpy(struct thread *thread, struct machine *machine, cpumode =PERF_RECORD_MISC_KERNEL ;addr_location__init (&al); -if (!thread__find_map (thread, cpumode, ip, &al)) { + sample.cpumode = cpumode; +if (!thread__find_map (thread, &sample, &al)) {addr_location__exit (&al);return -1 ; }
tools/perf/util/thread.h +7 -7
@@ -124,17 +124,17 @@ size_t thread__fprintf(struct thread *thread, FILE *fp);struct thread *thread__main_thread (struct machine *machine,struct thread *thread); -struct map *thread__find_map (struct thread *thread,u8 cpumode,u64 addr, +struct map *thread__find_map (struct thread *thread,struct perf_sample *sample,struct addr_location *al); -struct map *thread__find_map_fb (struct thread *thread,u8 cpumode,u64 addr, +struct map *thread__find_map_fb (struct thread *thread,struct perf_sample *sample,struct addr_location *al); -struct symbol *thread__find_symbol (struct thread *thread,u8 cpumode, -u64 addr,struct addr_location *al); -struct symbol *thread__find_symbol_fb (struct thread *thread,u8 cpumode, -u64 addr,struct addr_location *al); +struct symbol *thread__find_symbol (struct thread *thread,struct perf_sample *sample, +struct addr_location *al); +struct symbol *thread__find_symbol_fb (struct thread *thread,struct perf_sample *sample, +struct addr_location *al); -void thread__find_cpumode_addr_location (struct thread *thread,u64 addr, +void thread__find_cpumode_addr_location (struct thread *thread,struct perf_sample *sample,bool symbols,struct addr_location *al); int thread__memcpy(struct thread *thread, struct machine *machine,
tools/perf/util/unwind-libdw.c +5 -2
@@ -82,7 +82,9 @@ static int __report_module(struct addr_location *al, u64 ip,* Some callers will use al->sym, so we can't just use the * cheaper thread__find_map() here. */ -thread__find_symbol (ui->thread,PERF_RECORD_MISC_USER , ip, al); +thread__find_symbol (ui->thread, + &(struct perf_sample ){.cpumode =PERF_RECORD_MISC_USER , .ip = ip}, + al);if (al->map) dso =map__dso (al->map);
@@ -208,13 +210,14 @@ static bool get_thread(Dwfl *dwfl __maybe_unused, pid_t tid, void *arg,static int access_dso_mem (struct unwind_info *ui,Dwarf_Addr addr,Dwarf_Word *data,size_t len) { +struct perf_sample sample = { .cpumode =PERF_RECORD_MISC_USER , .ip = addr };struct addr_location al;union u64_swap u;ssize_t size;struct dso *dso;addr_location__init (&al); -if (!thread__find_map (ui->thread,PERF_RECORD_MISC_USER , addr, &al)) { +if (!thread__find_map (ui->thread, &sample, &al)) {pr_debug ("unwind: no map for %lx\n" , (unsigned long )addr); goto out_fail; }
tools/perf/util/unwind-libunwind.c +6 -2
@@ -370,7 +370,9 @@ static struct map *find_map(uint64_t ip, struct unwind_info *ui)struct map *ret;addr_location__init (&al); -thread__find_map (ui->thread,PERF_RECORD_MISC_USER , ip, &al); +thread__find_map (ui->thread, + &(struct perf_sample ){.cpumode =PERF_RECORD_MISC_USER , .ip = ip}, + &al); ret =map__get (al.map);addr_location__exit (&al);return ret;
@@ -626,7 +628,9 @@ static int entry(uint64_t ip, struct thread *thread, unwind_entry_cb_t cb, voidint ret;addr_location__init (&al); - e.ms.sym =thread__find_symbol (thread,PERF_RECORD_MISC_USER , ip, &al); + e.ms.sym =thread__find_symbol (thread, + &(struct perf_sample ){.cpumode =PERF_RECORD_MISC_USER , + .ip = ip}, &al); e.ip = ip; e.ms.map = al.map; e.ms.thread =thread__get (al.thread);
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 13/19 perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id()
In reply to Ian RogersAdd 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.
--- 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(+)
tools/perf/util/dsos.c +68 -0
@@ -294,6 +294,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;
@@ -349,6 +383,40 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dreturn 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);
tools/perf/util/dsos.h +6 -0
@@ -8,6 +8,7 @@#include <linux/rbtree.h> #include "rwsem.h" +struct build_id ;struct dso ;struct dso_id ;struct kmod_path ;
@@ -31,8 +32,13 @@ void dsos__exit(struct dsos *dsos);int __dsos__add (struct dsos *dsos,struct dso *dso);int dsos__add (struct dsos *dsos,struct dso *dso);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);
tools/perf/util/maps.c +73 -0
87 lines not on this page · raw message
tools/perf/util/maps.h +2 -0
@@ -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);
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 14/19 perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types
In reply to Ian RogersAdd fields to struct perf_sample and implement parsing, synthesis, cross-endian byte-swapping, and raw trace dumping ('perf report -D') for PERF_SAMPLE_BUILD_ID_OFFSET, PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET, and PERF_RECORD_CALLCHAIN_DEFERRED with PERF_RECORD_MISC_MMAP_BUILD_ID.
--- tools/perf/util/evsel.c | 153 +++++++++++++++++++++- tools/perf/util/evsel.h | 3 +- tools/perf/util/perf_event_attr_fprintf.c | 3 +- tools/perf/util/python.c | 17 +++ tools/perf/util/sample.c | 84 ++++++++++++ tools/perf/util/sample.h | 39 ++++++ tools/perf/util/session.c | 126 ++++++++++++++++-- tools/perf/util/synthetic-events.c | 41 ++++++ 8 files changed, 444 insertions(+), 22 deletions(-)
tools/perf/util/evsel.c +146 -7
@@ -3445,6 +3445,89 @@ static int __set_offcpu_sample(struct perf_sample *data)return -EFAULT ; } +/* +* Before __evsel__parse_sample() is called on a cross-endian perf.data file, +* perf_event__all64_swap() has already byte-swapped the entire PERF_RECORD_SAMPLE +* payload in 64-bit chunks. That leaves 64-bit fields (like offset, ip, cookie) +* in host endianness, but scrambles the 24-byte struct perf_build_id (which +* contains a 16-bit reserved field, a 1-byte size, and a 20-byte byte array). +* Undo the 64-bit chunk swap on struct perf_build_id and byte-swap its 16-bit +* field. +*/ +static void perf_build_id__undo_bswap_64 (struct perf_build_id *bid) +{ +mem_bswap_64 (bid,sizeof (*bid)); + bid->__reserved_2 =bswap_16 (bid->__reserved_2); +} + +static int parse_callchain_build_id_offset (const __u64 **arrayp,const void *endp, +struct perf_sample *data,bool swapped) +{ +const __u64 *array = *arrayp; +__u64 *hdr; +union u64_swap u; +u32 nr_build_ids, nr_offsets; +u32 pos =0 , aligned_bids_len; +u8 *raw_bytes; +u8 *build_ids =NULL ; +u64 *offsets =NULL ; + +if ((const void *)(array +1 ) > endp) +return -EFAULT ; + + hdr = (__u64 *)array++; + u.val64 = *hdr; +if (swapped) { +/* undo swap of words, then swap_32 */ + u.val64 =bswap_64 (u.val64); + u.val32[0 ] =bswap_32 (u.val32[0 ]); + u.val32[1 ] =bswap_32 (u.val32[1 ]); + } + nr_build_ids = u.val32[0 ]; + nr_offsets = u.val32[1 ]; +if (nr_build_ids >254 ) +return -EFAULT ; + + raw_bytes = (u8 *)array; +for (u32 i =0 ; i < nr_build_ids; i++) { +u32 byte_idx = swapped ? (pos ^7 ) : pos; +u8 bid_size; + +if ((const void *)(raw_bytes + (pos |7 ) +1 ) > endp) +return -EFAULT ; + bid_size = raw_bytes[byte_idx]; +if (bid_size ==0 || bid_size >20 ) +return -EFAULT ; + pos +=1 + bid_size; + } + aligned_bids_len =ALIGN (pos,sizeof (u64)); +if ((const void *)(raw_bytes + aligned_bids_len) > endp) +return -EFAULT ; + + array += aligned_bids_len /sizeof (u64); +if (nr_offsets > (size_t )((const u8 *)endp - (const u8 *)array) /sizeof (u64)) +return -EFAULT ; + +if (swapped) { + *hdr = u.val64; +if (aligned_bids_len >0 ) +mem_bswap_64 (raw_bytes, aligned_bids_len); + } +if (aligned_bids_len >0 ) + build_ids = raw_bytes; +if (nr_offsets >0 ) + offsets = (u64 *)array; + array += nr_offsets; + + data->callchain_bid.nr_build_ids = nr_build_ids; + data->callchain_bid.nr_offsets = nr_offsets; + data->callchain_bid.build_ids_len = aligned_bids_len; + data->callchain_bid.build_ids = build_ids; + data->callchain_bid.offsets = offsets; + *arrayp = array; +return 0 ; +} +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event,struct perf_sample *data,bool needs_swap) {
@@ -3472,14 +3555,21 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, data->vcpu =-1 ;if (event->header.type ==PERF_RECORD_CALLCHAIN_DEFERRED ) { -const u64 max_callchain_nr =UINT64_MAX /sizeof (u64); -OVERFLOW_CHECK_u64 (&event->callchain_deferred.nr); - data->callchain = (struct ip_callchain *)&event->callchain_deferred.nr; -if (data->callchain->nr > max_callchain_nr) - goto out_efault; -OVERFLOW_CHECK (event->callchain_deferred.ips, - data->callchain->nr *sizeof (u64), max_size); +if (event->header.misc &PERF_RECORD_MISC_MMAP_BUILD_ID ) { + array = (const __u64 *)&event->callchain_deferred.nr_build_ids; +if (parse_callchain_build_id_offset (&array, endp, data, +/*swapped=*/ false)) + goto out_efault; + }else { +const u64 max_callchain_nr =UINT64_MAX /sizeof (u64); + + data->callchain = (struct ip_callchain *)&event->callchain_deferred.nr; +if (data->callchain->nr > max_callchain_nr) + goto out_efault; +OVERFLOW_CHECK (event->callchain_deferred.ips, + data->callchain->nr *sizeof (u64), max_size); + } data->deferred_cookie = event->callchain_deferred.cookie;
@@ -3918,6 +4008,55 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, array = (void *)array + sz; } +if (type &PERF_SAMPLE_BUILD_ID_OFFSET ) { +const struct perf_sample_build_id_offset *entry = + (const struct perf_sample_build_id_offset *)array; + +OVERFLOW_CHECK (array,sizeof (*entry), max_size); +if (evsel->core.attr.defer_callchain && entry->zero ==0 && + entry->context ==PERF_CONTEXT_USER_DEFERRED ) { + data->deferred_bid = true; + data->deferred_callchain = true; + data->deferred_cookie = entry->cookie; + data->bid.bid = (struct perf_build_id *)&entry->bid; + data->bid.offset = entry->ip; + }else { +struct perf_build_id *bid = + (struct perf_build_id *)&entry->bid; + +/* +* perf_event__all64_swap() already swapped the sample +* in 64-bit chunks, so entry->offset is already in host +* endianness; undo the 64-bit swap on the build ID +* bytes in-place so re-parsing with needs_swap=false +* sees host layout. +*/ +if (swapped) +perf_build_id__undo_bswap_64 (bid); + data->bid.bid = bid; + data->bid.offset = entry->offset; + } +if (!(type &PERF_SAMPLE_IP )) + data->ip = data->bid.offset; + array +=sizeof (*entry) /sizeof (u64); + }else { + data->bid.bid =NULL ; + data->bid.offset =0 ; + } + +if (type &PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET ) { +if (parse_callchain_build_id_offset (&array, endp, data, swapped)) + goto out_efault; +if (evsel->core.attr.defer_callchain && + data->callchain_bid.nr_offsets >=2 && + data->callchain_bid.offsets[data->callchain_bid.nr_offsets -2 ] == +PERF_CONTEXT_USER_DEFERRED ) { + data->deferred_cookie = + data->callchain_bid.offsets[data->callchain_bid.nr_offsets -1 ]; + data->deferred_callchain = true; + } + } +if (evsel__is_offcpu_event (evsel)) {if (__set_offcpu_sample (data)) goto out_efault;
tools/perf/util/evsel.h +2 -1
@@ -570,7 +570,8 @@ static inline bool evsel__has_callchain(const struct evsel *evsel)* For reporting purposes, an evsel sample can have a recorded callchain * or a callchain synthesized from AUX area data. */ -return evsel->core.attr.sample_type &PERF_SAMPLE_CALLCHAIN || +return evsel->core.attr.sample_type & (PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET ) || evsel->synth_sample_type &PERF_SAMPLE_CALLCHAIN ; }
tools/perf/util/perf_event_attr_fprintf.c +2 -1
@@ -40,7 +40,8 @@ static void __p_sample_type(char *buf, size_t size, u64 value)bit_name (IDENTIFIER ),bit_name (REGS_INTR ),bit_name (DATA_SRC ),bit_name (WEIGHT ),bit_name (PHYS_ADDR ),bit_name (AUX ),bit_name (CGROUP ),bit_name (DATA_PAGE_SIZE ),bit_name (CODE_PAGE_SIZE ), -bit_name (WEIGHT_STRUCT ), +bit_name (WEIGHT_STRUCT ),bit_name (BUILD_ID_OFFSET ), +bit_name (CALLCHAIN_BUILD_ID_OFFSET ), { .name = NULL, } };#undef bit_name
tools/perf/util/python.c +17 -0
@@ -1747,6 +1747,18 @@ static int pyrf_event__copy(struct pyrf_event *pevent) sizeof(pevent->sample_storage.insn)); pevent->sample_storage.insn_len = orig_sample->insn_len; } +if (orig_sample->bid.bid == + (const struct perf_build_id *)orig_sample->bid.deferred_storage) { +memcpy (pevent->sample_storage.bid.deferred_storage, + orig_sample->bid.deferred_storage, +sizeof (pevent->sample_storage.bid.deferred_storage)); + pevent->sample_storage.bid.bid = + (struct perf_build_id *) + pevent->sample_storage.bid.deferred_storage; + pevent->sample_storage.bid.offset = orig_sample->bid.offset; + pevent->sample_storage.ip = orig_sample->ip; + pevent->sample_storage.deferred_bid = false; + } } pevent->sample = &pevent->sample_storage; }else if (orig_sample != &pevent->sample_storage) {
@@ -1763,6 +1775,11 @@ static int pyrf_event__copy(struct pyrf_event *pevent)evsel__get (pevent->sample_storage.evsel); pevent->sample_storage.callchain =NULL ; pevent->sample_storage.merged_callchain = false; + pevent->sample_storage.bid.bid =NULL ; +memset (&pevent->sample_storage.callchain_bid,0 , +sizeof (pevent->sample_storage.callchain_bid)); + pevent->sample_storage.allocated_callchain_bids = false; + pevent->sample_storage.merged_callchain_bids = false; pevent->sample_storage.raw_data =NULL ; pevent->sample_storage.raw_size =0 ; pevent->sample_storage.branch_stack =NULL ;
tools/perf/util/sample.c +84 -0
109 lines not on this page · raw message
tools/perf/util/sample.h +39 -0
@@ -8,6 +8,7 @@struct evsel ;struct machine ;struct thread ; +struct perf_build_id ;/* number of register is bound by the number of bits in regs_dump::mask (64) */ #define PERF_SAMPLE_REGS_CACHE_SIZE (8 * sizeof(u64))
@@ -168,6 +169,26 @@ struct perf_sample {u64 code_page_size;/** @cgroup: The sample event PERF_SAMPLE_CGROUP value. */ u64 cgroup; +/** @bid: The sample event PERF_SAMPLE_BUILD_ID_OFFSET value. */ +struct { +struct perf_build_id *bid; +union { +u64 offset; +u64 ip; + }; +u64 deferred_storage[3 ]; + } bid; +/** +* @callchain_bid: Build IDs and offsets for the callchain when +* PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET is set. +*/ +struct { +u32 nr_build_ids; +u32 nr_offsets; +u32 build_ids_len; +u8 *build_ids; +u64 *offsets; + } callchain_bid;/** @file_offset: Byte offset of this event in the perf.data file. */ u64 file_offset;/** @flags: Extra flag data from auxiliary events like intel-pt. */
@@ -210,11 +231,26 @@ struct perf_sample {* user callchain marker was encountered. */ bool deferred_callchain; +/** +* @deferred_bid: When processing PERF_SAMPLE_BUILD_ID_OFFSET a deferred +* user marker was encountered. +*/ +bool deferred_bid;/** * @merged_callchain: A synthesized merged callchain that is allocated * and needs freeing. */ bool merged_callchain; +/** +* @allocated_callchain_bids: callchain_bid.build_ids is heap-allocated +* and needs freeing. +*/ +bool allocated_callchain_bids; +/** +* @merged_callchain_bids: PERF_CONTEXT_USER_DEFERRED in callchain_bid +* has been merged with its deferred callchain. +*/ +bool merged_callchain_bids;/** * @deferred_cookie: Identifier of the deferred callchain in the later * PERF_RECORD_CALLCHAIN_DEFERRED event.
@@ -261,6 +297,9 @@ void perf_sample__init(struct perf_sample *sample, bool all);void perf_sample__exit (struct perf_sample *sample);struct regs_dump *perf_sample__user_regs (struct perf_sample *sample);struct regs_dump *perf_sample__intr_regs (struct perf_sample *sample); +void perf_sample__callchain_bid_entry (const struct perf_sample *sample, +u32 idx,struct perf_build_id *bid_out, +u64 *offset_out); void perf_sample__fetch_insn(struct perf_sample *sample, struct thread *thread,
tools/perf/util/session.c +113 -13
177 lines not on this page · raw message
tools/perf/util/synthetic-events.c +41 -0
53 lines not on this page · raw message
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 15/19 perf symbol: Resolve symbols and callchains using build ID and offset
In reply to Ian RogersUpdate thread__find_map(), callchain resolution in machine.c, deferred callchain merging in sample__merge_deferred_callchain(), and perf report/script to resolve symbols and callchains using the build ID and file offset recorded in samples.
--- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-script.c | 27 +++-- tools/perf/util/callchain.c | 153 +++++++++++++++++++++++----- tools/perf/util/event.c | 36 +++++-- tools/perf/util/evsel_fprintf.c | 9 +- tools/perf/util/machine.c | 173 ++++++++++++++++++++++++-------- 6 files changed, 317 insertions(+), 87 deletions(-)
tools/perf/builtin-report.c +4 -2
@@ -384,7 +384,8 @@ static int report__setup_sample_type(struct report *rep) session->itrace_synth_opts->add_last_branch) sample_type |=PERF_SAMPLE_BRANCH_STACK ; -if (!is_pipe && !(sample_type &PERF_SAMPLE_CALLCHAIN )) { +if (!is_pipe && !(sample_type & (PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET ))) {if (perf_hpp_list.parent) {ui__error ("Selected --sort parent, but no " "callchain data. Did you call "
@@ -410,7 +411,8 @@ static int report__setup_sample_type(struct report *rep)if (symbol_conf.cumulate_callchain) {/* Silently ignore if callchain is missing */ -if (!(sample_type &PERF_SAMPLE_CALLCHAIN )) { +if (!(sample_type & (PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET ))) { symbol_conf.cumulate_callchain = false;perf_hpp__cancel_cumulate (session->evlist); }
tools/perf/builtin-script.c +19 -8
@@ -498,7 +498,8 @@ static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)return -EINVAL ;if (PRINT_FIELD (IP )) { -if (evsel__check_stype (evsel,PERF_SAMPLE_IP ,"IP" ,PERF_OUTPUT_IP )) +if (evsel__check_stype (evsel,PERF_SAMPLE_IP |PERF_SAMPLE_BUILD_ID_OFFSET , +"IP" ,PERF_OUTPUT_IP ))return -EINVAL ; }
@@ -515,7 +516,8 @@ static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)return -EINVAL ;if (PRINT_FIELD (SYM ) && - !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP |PERF_SAMPLE_ADDR ))) { + !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP |PERF_SAMPLE_ADDR | +PERF_SAMPLE_BUILD_ID_OFFSET ))) {pr_err ("Display of symbols requested but neither sample IP nor " "sample address\navailable. Hence, no addresses to convert " "to symbols.\n" );
@@ -527,7 +529,8 @@ static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)return -EINVAL ; }if (PRINT_FIELD (DSO ) && - !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP |PERF_SAMPLE_ADDR ))) { + !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP |PERF_SAMPLE_ADDR | +PERF_SAMPLE_BUILD_ID_OFFSET ))) {pr_err ("Display of DSO requested but no address to convert.\n" );return -EINVAL ; }
@@ -1796,7 +1799,8 @@ static int perf_sample__fprintf_bts(struct perf_sample *sample,unsigned int print_opts = output[type].print_ip_opts;struct callchain_cursor *cursor =NULL ; -if (symbol_conf.use_callchain && sample->callchain) { +if (symbol_conf.use_callchain && + (sample->callchain || sample->callchain_bid.nr_offsets)) { cursor =get_tls_callchain_cursor (); if (thread__resolve_callchain(al->thread, cursor, sample, NULL, NULL,
@@ -2755,13 +2759,18 @@ static void process_event(struct perf_script *script,fprintf (fp," %s" , cgrp_name); } -if (PRINT_FIELD (IP )) { +if (PRINT_FIELD (IP ) || + ((attr->sample_type & (PERF_SAMPLE_IP |PERF_SAMPLE_BUILD_ID_OFFSET )) && + output[type].print_ip_opts && + !(PRINT_FIELD (ADDR ) ||PRINT_FIELD (BRSTACK ) || +PRINT_FIELD (BRSTACKSYM ) ||PRINT_FIELD (BRSTACKOFF )))) {struct callchain_cursor *cursor =NULL ;if (script->stitch_lbr)thread__set_lbr_stitch_enable (al->thread, true); -if (symbol_conf.use_callchain && sample->callchain) { +if (symbol_conf.use_callchain && + (sample->callchain || sample->callchain_bid.nr_offsets)) { cursor =get_tls_callchain_cursor (); if (thread__resolve_callchain(al->thread, cursor, sample, NULL, NULL,
@@ -3005,10 +3014,11 @@ static int process_deferred_sample_event(const struct perf_tool *tool,fprintf (fp,"DEFERRED CALLCHAIN [cookie: %llx]" , (unsigned long long )event->callchain_deferred.cookie); -if (PRINT_FIELD (IP )) { +if (output[type].print_ip_opts) {struct callchain_cursor *cursor =NULL ; -if (symbol_conf.use_callchain && sample->callchain) { +if (symbol_conf.use_callchain && + (sample->callchain || sample->callchain_bid.nr_offsets)) { cursor =get_tls_callchain_cursor (); if (thread__resolve_callchain(al.thread, cursor, sample, NULL, NULL,
@@ -3090,6 +3100,7 @@ static int process_attr(const struct perf_tool *tool, union perf_event *event,/* Enable fields for callchain entries */ if (symbol_conf.use_callchain && (sample_type &PERF_SAMPLE_CALLCHAIN || + sample_type &PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET || sample_type &PERF_SAMPLE_BRANCH_STACK || (sample_type &PERF_SAMPLE_REGS_USER && sample_type &PERF_SAMPLE_STACK_USER ))) {
tools/perf/util/callchain.c +130 -23
176 lines not on this page · raw message
tools/perf/util/event.c +26 -10
@@ -702,8 +702,8 @@ struct map *thread__find_map(struct thread *thread, struct perf_sample *sample,thread__zput (al->thread); al->thread =thread__get (thread); - al->addr = sample->ip; al->cpumode = sample->cpumode; + al->addr = sample->bid.bid ? sample->bid.offset : sample->ip; al->filtered =0 ;if (sample->cpumode ==PERF_RECORD_MISC_KERNEL && perf_host) {
@@ -732,15 +732,31 @@ struct map *thread__find_map(struct thread *thread, struct perf_sample *sample,return NULL ; } - al->map =maps__find (maps, al->addr); -if (al->map !=NULL ) { -/* -* Kernel maps might be changed when loading symbols so loading -* must be done prior to using kernel maps. -*/ -if (load_map) -map__load (al->map); - al->addr =map__map_ip (al->map, al->addr); + +if (sample->bid.bid && sample->bid.bid->size >0 ) { +struct build_id bid; + +build_id__init (&bid, sample->bid.bid->data, sample->bid.bid->size); + al->addr = sample->bid.offset; + al->map =maps__find_by_build_id (maps, &bid); +if (al->map !=NULL ) { +if (load_map) +map__load (al->map); +/* al->addr is already a file offset */ + } + }else { +/* When bid.bid->size == 0, bid.ip holds the fallback virtual address. */ + al->addr = sample->bid.bid ? sample->bid.ip : sample->ip; + al->map =maps__find (maps, al->addr); +if (al->map !=NULL ) { +/* +* Kernel maps might be changed when loading symbols so loading +* must be done prior to using kernel maps. +*/ +if (load_map) +map__load (al->map); + al->addr =map__map_ip (al->map, al->addr); + } }return al->map;
tools/perf/util/evsel_fprintf.c +6 -3
@@ -131,7 +131,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,if (cursor ==NULL )return fprintf (fp,"<not enough memory for the callchain cursor>%s" , print_oneline ?"" :"\n" ); -if (sample->callchain) { +if (sample->callchain || sample->callchain_bid.nr_offsets) {callchain_cursor_commit (cursor);while (1 ) {
@@ -232,8 +232,11 @@ int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al, }else { printed +=fprintf (fp,"%-*.*s" , left_alignment, left_alignment," " ); -if (print_ip) - printed +=fprintf (fp,"%16" PRIx64, sample->ip); +if (print_ip) { +u64 ip = sample->bid.bid ? sample->bid.offset : sample->ip; + + printed +=fprintf (fp,"%16" PRIx64, ip); + }if (print_sym) { printed +=fprintf (fp," " );
tools/perf/util/machine.c +132 -41
324 lines not on this page · raw message
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 16/19 perf inject: Add --sample-buildids to convert samples to build ID + offset
In reply to Ian RogersAdd the --sample-buildids (-S) option to perf inject, allowing it to drop user-space MMAP events and rewrite PERF_SAMPLE_IP, PERF_SAMPLE_CALLCHAIN, and PERF_RECORD_CALLCHAIN_DEFERRED records to use build IDs and file offsets instead of virtual addresses.
--- tools/perf/Documentation/perf-inject.txt | 8 + tools/perf/builtin-inject.c | 71 +- tools/perf/util/Build | 1 + tools/perf/util/inject_bid_offset.c | 882 +++++++++++++++++++++++ tools/perf/util/inject_bid_offset.h | 15 + 5 files changed, 970 insertions(+), 7 deletions(-) create mode 100644 tools/perf/util/inject_bid_offset.c create mode 100644 tools/perf/util/inject_bid_offset.h
tools/perf/Documentation/perf-inject.txt +8 -0
@@ -32,6 +32,14 @@ OPTIONS Inject build-ids of all DSOs into the output stream regardless of hits and skip SAMPLE processing. +-S:: +--sample-buildids:: + Drop user-space MMAP and MMAP2 events, inject build-id records for DSOs + hit by samples, and rewrite PERF_SAMPLE_IP, PERF_SAMPLE_CALLCHAIN, and + PERF_RECORD_CALLCHAIN_DEFERRED records to use build IDs and DSO file + offsets instead of virtual addresses. Mutually exclusive with --aslr + and --convert-callchain. + --known-build-ids=:: Override build-ids to inject using these comma-separated pairs of build-id and path. Understands file://filename to read these pairs
tools/perf/builtin-inject.c +64 -7
191 lines not on this page · raw message
tools/perf/util/Build +1 -0
@@ -7,6 +7,7 @@ perf-util-y += addr2line.o perf-util-y += addr_location.o perf-util-y += annotate.o perf-util-y += aslr.o +perf-util-y += inject_bid_offset.o perf-util-y += blake2s.o perf-util-y += block-info.o perf-util-y += block-range.o
tools/perf/util/inject_bid_offset.c new file +882 -0
882 lines not on this page · raw message
tools/perf/util/inject_bid_offset.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_INJECT_BID_OFFSET_H +#define __PERF_INJECT_BID_OFFSET_H + +#include <linux/perf_event.h> + +struct perf_tool ; +struct evlist ; + +int perf_event__rewrite_attr_for_build_id_offset (struct perf_event_attr *attr); +int perf_event__rewrite_attr_for_sample_ip (struct perf_event_attr *attr); +struct perf_tool *inject_bid_offset_tool__new (struct perf_tool *delegate); +void inject_bid_offset_tool__delete (struct perf_tool *tool); + +#endif /* __PERF_INJECT_BID_OFFSET_H */
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 17/19 perf record: Add --buildid-offset option
In reply to Ian RogersAdd 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.
--- 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(-)
tools/perf/Documentation/perf-record.txt +6 -0
@@ -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
tools/perf/builtin-record.c +20 -2
@@ -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); }
tools/perf/util/evsel.c +16 -1
@@ -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. */
tools/perf/util/record.c +22 -0
@@ -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);
tools/perf/util/record.h +1 -0
@@ -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;
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 18/19 perf test: Add BUILD_ID_OFFSET C unit tests
In reply to Ian RogersAdd C unit test coverage for PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET:
- sample-parsing: round-trip synthesis and parsing
- mmap-thread-lookup: map lookup by build ID and DSO file offset
- bid-offset: sys_perf_event_open() attribute probing
--- tools/perf/tests/Build | 1 + tools/perf/tests/bid-offset.c | 61 +++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/mmap-thread-lookup.c | 49 ++++++++++++++++++++- tools/perf/tests/sample-parsing.c | 60 +++++++++++++++++++++++++- tools/perf/tests/tests.h | 1 + 6 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 tools/perf/tests/bid-offset.c
tools/perf/tests/Build +1 -0
@@ -66,6 +66,7 @@ perf-test-y += expand-cgroup.o perf-test-y += perf-time-to-tsc.o perf-test-y += dlfilter-test.o perf-test-y += sigtrap.o +perf-test-y += bid-offset.o perf-test-y += event_groups.o perf-test-y += hybrid-merge.o perf-test-y += symbols.o
tools/perf/tests/bid-offset.c new file +61 -0
61 lines not on this page · raw message
tools/perf/tests/builtin-test.c +1 -0
@@ -186,6 +186,7 @@ static struct test_suite *generic_tests[] = { &suite__perf_time_to_tsc, &suite__dlfilter, &suite__sigtrap, + &suite__bid_offset, &suite__event_groups, &suite__hybrid_merge, &suite__symbols,
tools/perf/tests/mmap-thread-lookup.c +47 -2
77 lines not on this page · raw message
tools/perf/tests/sample-parsing.c +59 -1
84 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -196,6 +196,7 @@ DECLARE_SUITE(expand_cgroup_events);DECLARE_SUITE (perf_time_to_tsc);DECLARE_SUITE (dlfilter);DECLARE_SUITE (sigtrap); +DECLARE_SUITE (bid_offset);DECLARE_SUITE (event_groups);DECLARE_SUITE (hybrid_merge);DECLARE_SUITE (symbols);
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH v3 19/19 perf test: Add inject --sample-buildids and record --buildid-offset shell tests
In reply to Ian RogersAdd shell tests verifying perf inject --sample-buildids (with basic samples, callchains, pipe mode, and kernel samples) and perf record --buildid-offset across per-thread, system-wide, workload, and callgraph modes.
--- tools/perf/tests/shell/inject_bid_offset.sh | 179 ++++++++++++++++++++ tools/perf/tests/shell/record.sh | 74 ++++++++ 2 files changed, 253 insertions(+) create mode 100755 tools/perf/tests/shell/inject_bid_offset.sh
tools/perf/tests/shell/inject_bid_offset.sh new file +179 -0
@@ -0,0 +1,179 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# perf inject --sample-buildids test + +set -e +set -o pipefail + +shelldir=$(dirname "$0") +# shellcheck source=lib/perf_has_symbol.sh +. "${shelldir}"/lib/perf_has_symbol.sh + +sym="noploop" + +skip_test_missing_symbol ${sym} + +temp_dir=$(mktemp -d /tmp/perf-test-bid-offset.XXXXXXXXXX) + +prog="perf test -w noploop" +[ "$(uname -m)" = "s390x" ] && prog="$prog 3" +err=0 + +cleanup() { + local exit_code=${1:-$?} + trap - EXIT TERM INT + if [ "${exit_code}" -ne 0 ] || [ "${err}" -ne 0 ]; then + echo "Test failed! Preserving temp directory: ${temp_dir}" + exit 1 + fi + if [[ "${temp_dir}" =~ ^/tmp/perf-test-bid-offset\. ]]; then + rm -rf "${temp_dir}" + fi + exit 0 +} + +trap_cleanup() { + local exit_code=$? + echo "Unexpected signal in ${FUNCNAME[1]}" + cleanup ${exit_code} +} +trap trap_cleanup EXIT TERM INT + +compare_script() { + local orig=$1 + local bid=$2 + local msg=$3 + + perf script -F -ip -i "${orig}" > "${temp_dir}/script_orig.txt" + perf script -F -ip -i "${bid}" > "${temp_dir}/script_bid.txt" + + if [ ! -s "${temp_dir}/script_orig.txt" ]; then + echo "${msg} [Failed - empty perf script output]" + err=1 + elif ! diff -q "${temp_dir}/script_orig.txt" "${temp_dir}/script_bid.txt"; then + echo "${msg} [Failed - perf script output differs]" + err=1 + else + echo "${msg} [Success]" + fi +} + +compare_report() { + local orig=$1 + local bid=$2 + local msg=$3 + + perf report -i "${orig}" --stdio | grep -v '^#' > "${temp_dir}/report_orig.txt" + perf report -i "${bid}" --stdio | grep -v '^#' > "${temp_dir}/report_bid.txt" + + if ! diff -q "${temp_dir}/report_orig.txt" "${temp_dir}/report_bid.txt"; then + echo "${msg} [Failed - perf report output differs]" + err=1 + else + echo "${msg} [Success]" + fi +} + +test_basic() { + echo "Test basic build id offset injection" + local data="${temp_dir}/perf.data.basic" + local data2="${temp_dir}/perf.data2.basic" + + perf record -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1 + perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1 + + compare_script "${data}" "${data2}" "Basic injection" +} + +test_callchain() { + echo "Test Callchain build id offset injection" + local data="${temp_dir}/perf.data.callchain" + local data2="${temp_dir}/perf.data2.callchain" + + perf record --call-graph fp -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1 + perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1 + + compare_script "${data}" "${data2}" "Callchain injection" +} + +test_pipe() { + echo "Test pipe mode build id offset injection" + local data="${temp_dir}/perf.data.pipe" + local data2="${temp_dir}/perf.data2.pipe" + + perf record -e task-clock:u -o - ${prog} 2>/dev/null | tee "${data}" | \ + perf inject --sample-buildids -o "${data2}" >/dev/null 2>&1 + + compare_script "${data}" "${data2}" "Pipe mode injection" +} + +test_report() { + echo "Test perf report consistency" + local data="${temp_dir}/perf.data.report" + local data2="${temp_dir}/perf.data2.report" + + perf record -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1 + perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1 + + compare_report "${data}" "${data2}" "Report consistency" +} + +test_pipe_report() { + echo "Test pipe mode perf report consistency" + local data="${temp_dir}/perf.data.pipe_report" + local data2="${temp_dir}/perf.data2.pipe_report" + + perf record -e task-clock:u -o - ${prog} 2>/dev/null | tee "${data}" | \ + perf inject --sample-buildids -o "${data2}" >/dev/null 2>&1 + + compare_report "${data}" "${data2}" "Pipe Report consistency" +} + +test_kernel() { + echo "Test kernel build id offset injection" + local data="${temp_dir}/perf.data.kernel" + local data2="${temp_dir}/perf.data2.kernel" + local kprog="dd if=/dev/urandom of=/dev/null bs=1M count=50" + + # Not all systems allow kernel profiling, so we check if it succeeds first + if ! perf record -e task-clock:k -o "${data}" ${kprog} >/dev/null 2>&1; then + echo "Kernel injection [Skipped - permissions or support missing]" + return + fi + perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1 + + compare_script "${data}" "${data2}" "Kernel injection" +} + +test_record_buildid_offset() { + echo "Test perf record --buildid-offset consistency" + local data="${temp_dir}/perf.data.record_orig" + local data2="${temp_dir}/perf.data2.record_bid" + + perf record -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1 + # We must have the binaries in .build-id cache, which the first record does natively. + # Now record with --buildid-offset. + if ! perf record --buildid-offset -e task-clock:u -o "${data2}" \ + ${prog} >/dev/null 2>&1; then + echo "Record --buildid-offset consistency [Skipped - not supported by kernel]" + return + fi + + # Verify symbolization resolves the workload symbol + if ! perf report -i "${data2}" --stdio | grep -q "${sym}"; then + echo "Record --buildid-offset consistency [Failed - missing ${sym} in perf report]" + err=1 + else + echo "Record --buildid-offset consistency [Success]" + fi +} + +test_basic +test_callchain +test_pipe +test_report +test_pipe_report +test_kernel +test_record_buildid_offset + +cleanup ${err}
tools/perf/tests/shell/record.sh +74 -0
86 lines not on this page · raw message
Signature · 1 line
2.56.1.703.gf848ed807c-goog