projects/linux·blob master · refs

410 lines · 13.3 KiB

1// SPDX-License-Identifier: GPL-2.0
2#include <inttypes.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <stdbool.h>
6#include <linux/kernel.h>
7#include <linux/types.h>
8#include <linux/perf_event.h>
9#include "util/evsel_fprintf.h"
10#include "util/pmu.h"
11#include "util/pmus.h"
12#include "trace-event.h"
13
14struct bit_names {
15 int bit;
16 const char *name;
17};
18
19static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
20{
21 bool first_bit = true;
22 int i = 0;
23
24 do {
25 if (value & bits[i].bit) {
26 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
27 first_bit = false;
28 }
29 } while (bits[++i].name != NULL);
30}
31
32static void __p_sample_type(char *buf, size_t size, u64 value)
33{
34#define bit_name(n) { PERF_SAMPLE_##n, #n }
35 struct bit_names bits[] = {
36 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
37 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
38 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
39 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
40 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
41 bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(AUX),
42 bit_name(CGROUP), bit_name(DATA_PAGE_SIZE), bit_name(CODE_PAGE_SIZE),
43 bit_name(WEIGHT_STRUCT),
44 { .name = NULL, }
45 };
46#undef bit_name
47 __p_bits(buf, size, value, bits);
48}
49
50static void __p_branch_sample_type(char *buf, size_t size, u64 value)
51{
52#define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
53 struct bit_names bits[] = {
54 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
55 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
56 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
57 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
58 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
59 bit_name(TYPE_SAVE), bit_name(HW_INDEX), bit_name(PRIV_SAVE),
60 bit_name(COUNTERS),
61 { .name = NULL, }
62 };
63#undef bit_name
64 __p_bits(buf, size, value, bits);
65}
66
67static void __p_read_format(char *buf, size_t size, u64 value)
68{
69#define bit_name(n) { PERF_FORMAT_##n, #n }
70 struct bit_names bits[] = {
71 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
72 bit_name(ID), bit_name(GROUP), bit_name(LOST),
73 { .name = NULL, }
74 };
75#undef bit_name
76 __p_bits(buf, size, value, bits);
77}
78
79#define ENUM_ID_TO_STR_CASE(x) case x: return (#x);
80static const char *stringify_perf_type_id(struct perf_pmu *pmu, u32 type)
81{
82 switch (type) {
83 ENUM_ID_TO_STR_CASE(PERF_TYPE_HARDWARE)
84 ENUM_ID_TO_STR_CASE(PERF_TYPE_SOFTWARE)
85 ENUM_ID_TO_STR_CASE(PERF_TYPE_TRACEPOINT)
86 ENUM_ID_TO_STR_CASE(PERF_TYPE_HW_CACHE)
87 ENUM_ID_TO_STR_CASE(PERF_TYPE_BREAKPOINT)
88 case PERF_TYPE_RAW:
89 return pmu ? pmu->name : "PERF_TYPE_RAW";
90 default:
91 return pmu ? pmu->name : NULL;
92 }
93}
94
95static const char *stringify_perf_hw_id(u64 value)
96{
97 switch (value & PERF_HW_EVENT_MASK) {
98 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CPU_CYCLES)
99 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_INSTRUCTIONS)
100 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_REFERENCES)
101 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_MISSES)
102 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)
103 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_BRANCH_MISSES)
104 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_BUS_CYCLES)
105 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_STALLED_CYCLES_FRONTEND)
106 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_STALLED_CYCLES_BACKEND)
107 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_REF_CPU_CYCLES)
108 default:
109 return NULL;
110 }
111}
112
113static const char *stringify_perf_hw_cache_id(u64 value)
114{
115 switch (value) {
116 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_L1D)
117 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_L1I)
118 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_LL)
119 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_DTLB)
120 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_ITLB)
121 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_BPU)
122 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_NODE)
123 default:
124 return NULL;
125 }
126}
127
128static const char *stringify_perf_hw_cache_op_id(u64 value)
129{
130 switch (value) {
131 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_OP_READ)
132 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_OP_WRITE)
133 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_OP_PREFETCH)
134 default:
135 return NULL;
136 }
137}
138
139static const char *stringify_perf_hw_cache_op_result_id(u64 value)
140{
141 switch (value) {
142 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_RESULT_ACCESS)
143 ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_RESULT_MISS)
144 default:
145 return NULL;
146 }
147}
148
149static const char *stringify_perf_sw_id(u64 value)
150{
151 switch (value) {
152 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CPU_CLOCK)
153 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_TASK_CLOCK)
154 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_PAGE_FAULTS)
155 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CONTEXT_SWITCHES)
156 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CPU_MIGRATIONS)
157 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_PAGE_FAULTS_MIN)
158 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_PAGE_FAULTS_MAJ)
159 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_ALIGNMENT_FAULTS)
160 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_EMULATION_FAULTS)
161 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_DUMMY)
162 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_BPF_OUTPUT)
163 ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CGROUP_SWITCHES)
164 default:
165 return NULL;
166 }
167}
168#undef ENUM_ID_TO_STR_CASE
169
170static void print_id_unsigned(char *buf, size_t size, u64 value, const char *s)
171{
172 if (s == NULL)
173 snprintf(buf, size, "%"PRIu64, value);
174 else
175 snprintf(buf, size, "%"PRIu64" (%s)", value, s);
176}
177
178static void print_id_hex(char *buf, size_t size, u64 value, const char *s)
179{
180 if (s == NULL)
181 snprintf(buf, size, "%#"PRIx64, value);
182 else
183 snprintf(buf, size, "%#"PRIx64" (%s)", value, s);
184}
185
186static void __p_type_id(char *buf, size_t size, struct perf_pmu *pmu, u32 type)
187{
188 print_id_unsigned(buf, size, type, stringify_perf_type_id(pmu, type));
189}
190
191static void __p_config_hw_id(char *buf, size_t size, struct perf_pmu *pmu, u64 config)
192{
193 const char *name = stringify_perf_hw_id(config);
194
195 if (name == NULL) {
196 if (pmu == NULL) {
197 snprintf(buf, size, "%#"PRIx64, config);
198 } else {
199 snprintf(buf, size, "%#"PRIx64" (%s/config=%#"PRIx64"/)", config, pmu->name,
200 config);
201 }
202 } else {
203 if (pmu == NULL)
204 snprintf(buf, size, "%#"PRIx64" (%s)", config, name);
205 else
206 snprintf(buf, size, "%#"PRIx64" (%s/%s/)", config, pmu->name, name);
207 }
208}
209
210static void __p_config_sw_id(char *buf, size_t size, u64 id)
211{
212 print_id_hex(buf, size, id, stringify_perf_sw_id(id));
213}
214
215static void __p_config_hw_cache_id(char *buf, size_t size, struct perf_pmu *pmu, u64 config)
216{
217 const char *hw_cache_str = stringify_perf_hw_cache_id(config & 0xff);
218 const char *hw_cache_op_str =
219 stringify_perf_hw_cache_op_id((config & 0xff00) >> 8);
220 const char *hw_cache_op_result_str =
221 stringify_perf_hw_cache_op_result_id((config & 0xff0000) >> 16);
222
223 if (hw_cache_str == NULL || hw_cache_op_str == NULL || hw_cache_op_result_str == NULL) {
224 if (pmu == NULL) {
225 snprintf(buf, size, "%#"PRIx64, config);
226 } else {
227 snprintf(buf, size, "%#"PRIx64" (%s/config=%#"PRIx64"/)", config, pmu->name,
228 config);
229 }
230 } else {
231 if (pmu == NULL) {
232 snprintf(buf, size, "%#"PRIx64" (%s | %s | %s)", config,
233 hw_cache_op_result_str, hw_cache_op_str, hw_cache_str);
234 } else {
235 snprintf(buf, size, "%#"PRIx64" (%s/%s | %s | %s/)", config, pmu->name,
236 hw_cache_op_result_str, hw_cache_op_str, hw_cache_str);
237 }
238 }
239}
240
241static void __p_config_tracepoint_id(char *buf, size_t size, u64 id)
242{
243 char *str = tracepoint_id_to_name(id);
244
245 print_id_hex(buf, size, id, str);
246 free(str);
247}
248
249static void __p_config_id(struct perf_pmu *pmu, char *buf, size_t size, u32 type, u64 config)
250{
251 switch (type) {
252 case PERF_TYPE_HARDWARE:
253 return __p_config_hw_id(buf, size, pmu, config);
254 case PERF_TYPE_SOFTWARE:
255 return __p_config_sw_id(buf, size, config);
256 case PERF_TYPE_HW_CACHE:
257 return __p_config_hw_cache_id(buf, size, pmu, config);
258 case PERF_TYPE_TRACEPOINT:
259 return __p_config_tracepoint_id(buf, size, config);
260 case PERF_TYPE_RAW:
261 case PERF_TYPE_BREAKPOINT:
262 default:
263 return print_id_hex(buf, size, config, perf_pmu__name_from_config(pmu, config));
264 }
265}
266
267#define BUF_SIZE 1024
268
269#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
270#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
271#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
272#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
273#define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
274#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
275#define p_type_id(val) __p_type_id(buf, BUF_SIZE, pmu, val)
276#define p_config_id(val) __p_config_id(pmu, buf, BUF_SIZE, attr->type, val)
277
278#define PRINT_ATTRn(_n, _f, _p, _a) \
279do { \
280 if (attr_size >= offsetof(struct perf_event_attr, _f) + \
281 sizeof(attr->_f) && \
282 (_a || attr->_f)) { \
283 _p(attr->_f); \
284 ret += attr__fprintf(fp, _n, buf, priv); \
285 } \
286} while (0)
287
288/* bitfield members share an offset; most are within PERF_ATTR_SIZE_VER0 */
289#define PRINT_ATTRn_bf(_n, _f, _p, _a) \
290do { \
291 if (_a || attr->_f) { \
292 _p(attr->_f); \
293 ret += attr__fprintf(fp, _n, buf, priv); \
294 } \
295} while (0)
296
297#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p, false)
298#define PRINT_ATTRf_bf(_f, _p) PRINT_ATTRn_bf(#_f, _f, _p, false)
299
300int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
301 attr__fprintf_f attr__fprintf, void *priv)
302{
303 struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
304 /*
305 * size == 0 means ABI0 — the producer didn't set attr.size.
306 * perf_event__fprintf_attr() may pass the raw mmap'd event
307 * before the local copy, so default to PERF_ATTR_SIZE_VER0
308 * (the ABI0 footprint) to avoid reading past the attr into
309 * the ID array that follows it in HEADER_ATTR events.
310 */
311 u32 attr_size = attr->size ?: PERF_ATTR_SIZE_VER0;
312 char buf[BUF_SIZE];
313 int ret = 0;
314
315 /*
316 * Cap to what we understand: all callers store the attr in a
317 * buffer of sizeof(*attr) bytes (perf.data read path copies
318 * min(attr.size, sizeof), BPF augmented path copies into a
319 * fixed-size value[] array). A spoofed attr->size larger
320 * than sizeof would cause PRINT_ATTRn to read past the
321 * actual buffer.
322 */
323 if (attr_size > sizeof(*attr))
324 attr_size = sizeof(*attr);
325
326 if (!pmu && attr_size >= offsetof(struct perf_event_attr, config) + sizeof(attr->config) &&
327 (attr->type == PERF_TYPE_HARDWARE || attr->type == PERF_TYPE_HW_CACHE)) {
328 u32 extended_type = attr->config >> PERF_PMU_TYPE_SHIFT;
329
330 if (extended_type)
331 pmu = perf_pmus__find_by_type(extended_type);
332 }
333
334 PRINT_ATTRn("type", type, p_type_id, true);
335 PRINT_ATTRf(size, p_unsigned);
336 PRINT_ATTRn("config", config, p_config_id, true);
337 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned, false);
338 PRINT_ATTRf(sample_type, p_sample_type);
339 PRINT_ATTRf(read_format, p_read_format);
340
341 /*
342 * All bitfields share a single __u64 right after read_format.
343 * BPF-captured attrs from perf trace may have a small size
344 * when the tracee passes a minimal struct, so skip the
345 * entire block when it's not covered.
346 */
347 if (attr_size >= offsetof(struct perf_event_attr, wakeup_events)) {
348 PRINT_ATTRf_bf(disabled, p_unsigned);
349 PRINT_ATTRf_bf(inherit, p_unsigned);
350 PRINT_ATTRf_bf(pinned, p_unsigned);
351 PRINT_ATTRf_bf(exclusive, p_unsigned);
352 PRINT_ATTRf_bf(exclude_user, p_unsigned);
353 PRINT_ATTRf_bf(exclude_kernel, p_unsigned);
354 PRINT_ATTRf_bf(exclude_hv, p_unsigned);
355 PRINT_ATTRf_bf(exclude_idle, p_unsigned);
356 PRINT_ATTRf_bf(mmap, p_unsigned);
357 PRINT_ATTRf_bf(comm, p_unsigned);
358 PRINT_ATTRf_bf(freq, p_unsigned);
359 PRINT_ATTRf_bf(inherit_stat, p_unsigned);
360 PRINT_ATTRf_bf(enable_on_exec, p_unsigned);
361 PRINT_ATTRf_bf(task, p_unsigned);
362 PRINT_ATTRf_bf(watermark, p_unsigned);
363 PRINT_ATTRf_bf(precise_ip, p_unsigned);
364 PRINT_ATTRf_bf(mmap_data, p_unsigned);
365 PRINT_ATTRf_bf(sample_id_all, p_unsigned);
366 PRINT_ATTRf_bf(exclude_host, p_unsigned);
367 PRINT_ATTRf_bf(exclude_guest, p_unsigned);
368 PRINT_ATTRf_bf(exclude_callchain_kernel, p_unsigned);
369 PRINT_ATTRf_bf(exclude_callchain_user, p_unsigned);
370 PRINT_ATTRf_bf(mmap2, p_unsigned);
371 PRINT_ATTRf_bf(comm_exec, p_unsigned);
372 PRINT_ATTRf_bf(use_clockid, p_unsigned);
373 PRINT_ATTRf_bf(context_switch, p_unsigned);
374 PRINT_ATTRf_bf(write_backward, p_unsigned);
375 PRINT_ATTRf_bf(namespaces, p_unsigned);
376 PRINT_ATTRf_bf(ksymbol, p_unsigned);
377 PRINT_ATTRf_bf(bpf_event, p_unsigned);
378 PRINT_ATTRf_bf(aux_output, p_unsigned);
379 PRINT_ATTRf_bf(cgroup, p_unsigned);
380 PRINT_ATTRf_bf(text_poke, p_unsigned);
381 PRINT_ATTRf_bf(build_id, p_unsigned);
382 PRINT_ATTRf_bf(inherit_thread, p_unsigned);
383 PRINT_ATTRf_bf(remove_on_exec, p_unsigned);
384 PRINT_ATTRf_bf(sigtrap, p_unsigned);
385 PRINT_ATTRf_bf(defer_callchain, p_unsigned);
386 PRINT_ATTRf_bf(defer_output, p_unsigned);
387 }
388
389 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned, false);
390 PRINT_ATTRf(bp_type, p_unsigned);
391 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex, false);
392 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex, false);
393 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
394 PRINT_ATTRf(sample_regs_user, p_hex);
395 PRINT_ATTRf(sample_stack_user, p_unsigned);
396 PRINT_ATTRf(clockid, p_signed);
397 PRINT_ATTRf(sample_regs_intr, p_hex);
398 PRINT_ATTRf(aux_watermark, p_unsigned);
399 PRINT_ATTRf(sample_max_stack, p_unsigned);
400 PRINT_ATTRf(aux_sample_size, p_unsigned);
401 PRINT_ATTRf(sig_data, p_unsigned);
402 /* aux_{start_paused,pause,resume} are at byte 116, past VER0 */
403 if (attr_size >= offsetof(struct perf_event_attr, sig_data)) {
404 PRINT_ATTRf_bf(aux_start_paused, p_unsigned);
405 PRINT_ATTRf_bf(aux_pause, p_unsigned);
406 PRINT_ATTRf_bf(aux_resume, p_unsigned);
407 }
408
409 return ret;
410}