perf tools: Apply tracepoint event definition options to BPF script
[deliverable/linux.git] / tools / perf / util / evsel.c
CommitLineData
f8a95309
ACM
1/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
936be503 10#include <byteswap.h>
0f6a3015 11#include <linux/bitops.h>
4605eab3 12#include <api/fs/tracing_path.h>
4e319027
RR
13#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
8dd2a131 16#include <linux/err.h>
bec19672 17#include <sys/resource.h>
4e319027 18#include "asm/bug.h"
8f651eae 19#include "callchain.h"
f14d5707 20#include "cgroup.h"
69aad6f1 21#include "evsel.h"
70082dd9 22#include "evlist.h"
69aad6f1 23#include "util.h"
86bd5e86 24#include "cpumap.h"
fd78260b 25#include "thread_map.h"
12864b31 26#include "target.h"
26d33022 27#include "perf_regs.h"
e3e1a54f 28#include "debug.h"
97978b3e 29#include "trace-event.h"
a9a3a4d9 30#include "stat.h"
69aad6f1 31
594ac61a
ACM
32static struct {
33 bool sample_id_all;
34 bool exclude_guest;
5c5e854b 35 bool mmap2;
57480d2c 36 bool cloexec;
814c8c38
PZ
37 bool clockid;
38 bool clockid_wrong;
bd0f8895 39 bool lbr_flags;
594ac61a
ACM
40} perf_missing_features;
41
814c8c38
PZ
42static clockid_t clockid;
43
ce8ccff5
ACM
44static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
45{
46 return 0;
47}
48
49static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
50{
51}
52
53static struct {
54 size_t size;
55 int (*init)(struct perf_evsel *evsel);
56 void (*fini)(struct perf_evsel *evsel);
57} perf_evsel__object = {
58 .size = sizeof(struct perf_evsel),
59 .init = perf_evsel__no_extra_init,
60 .fini = perf_evsel__no_extra_fini,
61};
62
63int perf_evsel__object_config(size_t object_size,
64 int (*init)(struct perf_evsel *evsel),
65 void (*fini)(struct perf_evsel *evsel))
66{
67
68 if (object_size == 0)
69 goto set_methods;
70
71 if (perf_evsel__object.size > object_size)
72 return -EINVAL;
73
74 perf_evsel__object.size = object_size;
75
76set_methods:
77 if (init != NULL)
78 perf_evsel__object.init = init;
79
80 if (fini != NULL)
81 perf_evsel__object.fini = fini;
82
83 return 0;
84}
85
c52b12ed
ACM
86#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
87
75562573 88int __perf_evsel__sample_size(u64 sample_type)
c2a70653
ACM
89{
90 u64 mask = sample_type & PERF_SAMPLE_MASK;
91 int size = 0;
92 int i;
93
94 for (i = 0; i < 64; i++) {
95 if (mask & (1ULL << i))
96 size++;
97 }
98
99 size *= sizeof(u64);
100
101 return size;
102}
103
75562573
AH
104/**
105 * __perf_evsel__calc_id_pos - calculate id_pos.
106 * @sample_type: sample type
107 *
108 * This function returns the position of the event id (PERF_SAMPLE_ID or
109 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
110 * sample_event.
111 */
112static int __perf_evsel__calc_id_pos(u64 sample_type)
113{
114 int idx = 0;
115
116 if (sample_type & PERF_SAMPLE_IDENTIFIER)
117 return 0;
118
119 if (!(sample_type & PERF_SAMPLE_ID))
120 return -1;
121
122 if (sample_type & PERF_SAMPLE_IP)
123 idx += 1;
124
125 if (sample_type & PERF_SAMPLE_TID)
126 idx += 1;
127
128 if (sample_type & PERF_SAMPLE_TIME)
129 idx += 1;
130
131 if (sample_type & PERF_SAMPLE_ADDR)
132 idx += 1;
133
134 return idx;
135}
136
137/**
138 * __perf_evsel__calc_is_pos - calculate is_pos.
139 * @sample_type: sample type
140 *
141 * This function returns the position (counting backwards) of the event id
142 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
143 * sample_id_all is used there is an id sample appended to non-sample events.
144 */
145static int __perf_evsel__calc_is_pos(u64 sample_type)
146{
147 int idx = 1;
148
149 if (sample_type & PERF_SAMPLE_IDENTIFIER)
150 return 1;
151
152 if (!(sample_type & PERF_SAMPLE_ID))
153 return -1;
154
155 if (sample_type & PERF_SAMPLE_CPU)
156 idx += 1;
157
158 if (sample_type & PERF_SAMPLE_STREAM_ID)
159 idx += 1;
160
161 return idx;
162}
163
164void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
165{
166 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
167 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
168}
169
7be5ebe8
ACM
170void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
171 enum perf_event_sample_format bit)
172{
173 if (!(evsel->attr.sample_type & bit)) {
174 evsel->attr.sample_type |= bit;
175 evsel->sample_size += sizeof(u64);
75562573 176 perf_evsel__calc_id_pos(evsel);
7be5ebe8
ACM
177 }
178}
179
180void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
181 enum perf_event_sample_format bit)
182{
183 if (evsel->attr.sample_type & bit) {
184 evsel->attr.sample_type &= ~bit;
185 evsel->sample_size -= sizeof(u64);
75562573 186 perf_evsel__calc_id_pos(evsel);
7be5ebe8
ACM
187 }
188}
189
75562573
AH
190void perf_evsel__set_sample_id(struct perf_evsel *evsel,
191 bool can_sample_identifier)
7a5a5ca5 192{
75562573
AH
193 if (can_sample_identifier) {
194 perf_evsel__reset_sample_bit(evsel, ID);
195 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
196 } else {
197 perf_evsel__set_sample_bit(evsel, ID);
198 }
7a5a5ca5
ACM
199 evsel->attr.read_format |= PERF_FORMAT_ID;
200}
201
ef1d1af2
ACM
202void perf_evsel__init(struct perf_evsel *evsel,
203 struct perf_event_attr *attr, int idx)
204{
205 evsel->idx = idx;
60b0896c 206 evsel->tracking = !idx;
ef1d1af2 207 evsel->attr = *attr;
2cfda562 208 evsel->leader = evsel;
410136f5
SE
209 evsel->unit = "";
210 evsel->scale = 1.0;
d49e4695 211 evsel->evlist = NULL;
1f45b1d4 212 evsel->bpf_fd = -1;
ef1d1af2 213 INIT_LIST_HEAD(&evsel->node);
930a2e29 214 INIT_LIST_HEAD(&evsel->config_terms);
ce8ccff5 215 perf_evsel__object.init(evsel);
bde09467 216 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
75562573 217 perf_evsel__calc_id_pos(evsel);
15bfd2cc 218 evsel->cmdline_group_boundary = false;
ef1d1af2
ACM
219}
220
ef503831 221struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
69aad6f1 222{
ce8ccff5 223 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
69aad6f1 224
ef1d1af2
ACM
225 if (evsel != NULL)
226 perf_evsel__init(evsel, attr, idx);
69aad6f1
ACM
227
228 return evsel;
229}
230
8dd2a131
JO
231/*
232 * Returns pointer with encoded error via <linux/err.h> interface.
233 */
ef503831 234struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
efd2b924 235{
ce8ccff5 236 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
8dd2a131 237 int err = -ENOMEM;
efd2b924 238
8dd2a131
JO
239 if (evsel == NULL) {
240 goto out_err;
241 } else {
efd2b924 242 struct perf_event_attr attr = {
0b80f8b3
ACM
243 .type = PERF_TYPE_TRACEPOINT,
244 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
245 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
efd2b924
ACM
246 };
247
e48ffe2b
ACM
248 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
249 goto out_free;
250
97978b3e 251 evsel->tp_format = trace_event__tp_format(sys, name);
8dd2a131
JO
252 if (IS_ERR(evsel->tp_format)) {
253 err = PTR_ERR(evsel->tp_format);
efd2b924 254 goto out_free;
8dd2a131 255 }
efd2b924 256
0b80f8b3 257 event_attr_init(&attr);
efd2b924 258 attr.config = evsel->tp_format->id;
0b80f8b3 259 attr.sample_period = 1;
efd2b924 260 perf_evsel__init(evsel, &attr, idx);
efd2b924
ACM
261 }
262
263 return evsel;
264
265out_free:
74cf249d 266 zfree(&evsel->name);
efd2b924 267 free(evsel);
8dd2a131
JO
268out_err:
269 return ERR_PTR(err);
efd2b924
ACM
270}
271
8ad7013b 272const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
c410431c
ACM
273 "cycles",
274 "instructions",
275 "cache-references",
276 "cache-misses",
277 "branches",
278 "branch-misses",
279 "bus-cycles",
280 "stalled-cycles-frontend",
281 "stalled-cycles-backend",
282 "ref-cycles",
283};
284
dd4f5223 285static const char *__perf_evsel__hw_name(u64 config)
c410431c
ACM
286{
287 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
288 return perf_evsel__hw_names[config];
289
290 return "unknown-hardware";
291}
292
27f18617 293static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
c410431c 294{
27f18617 295 int colon = 0, r = 0;
c410431c 296 struct perf_event_attr *attr = &evsel->attr;
c410431c
ACM
297 bool exclude_guest_default = false;
298
299#define MOD_PRINT(context, mod) do { \
300 if (!attr->exclude_##context) { \
27f18617 301 if (!colon) colon = ++r; \
c410431c
ACM
302 r += scnprintf(bf + r, size - r, "%c", mod); \
303 } } while(0)
304
305 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
306 MOD_PRINT(kernel, 'k');
307 MOD_PRINT(user, 'u');
308 MOD_PRINT(hv, 'h');
309 exclude_guest_default = true;
310 }
311
312 if (attr->precise_ip) {
313 if (!colon)
27f18617 314 colon = ++r;
c410431c
ACM
315 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
316 exclude_guest_default = true;
317 }
318
319 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
320 MOD_PRINT(host, 'H');
321 MOD_PRINT(guest, 'G');
322 }
323#undef MOD_PRINT
324 if (colon)
27f18617 325 bf[colon - 1] = ':';
c410431c
ACM
326 return r;
327}
328
27f18617
ACM
329static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
330{
331 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
332 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
333}
334
8ad7013b 335const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
335c2f5d
ACM
336 "cpu-clock",
337 "task-clock",
338 "page-faults",
339 "context-switches",
8ad7013b 340 "cpu-migrations",
335c2f5d
ACM
341 "minor-faults",
342 "major-faults",
343 "alignment-faults",
344 "emulation-faults",
d22d1a2a 345 "dummy",
335c2f5d
ACM
346};
347
dd4f5223 348static const char *__perf_evsel__sw_name(u64 config)
335c2f5d
ACM
349{
350 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
351 return perf_evsel__sw_names[config];
352 return "unknown-software";
353}
354
355static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
356{
357 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
358 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
359}
360
287e74aa
JO
361static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
362{
363 int r;
364
365 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
366
367 if (type & HW_BREAKPOINT_R)
368 r += scnprintf(bf + r, size - r, "r");
369
370 if (type & HW_BREAKPOINT_W)
371 r += scnprintf(bf + r, size - r, "w");
372
373 if (type & HW_BREAKPOINT_X)
374 r += scnprintf(bf + r, size - r, "x");
375
376 return r;
377}
378
379static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
380{
381 struct perf_event_attr *attr = &evsel->attr;
382 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
383 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
384}
385
0b668bc9
ACM
386const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
387 [PERF_EVSEL__MAX_ALIASES] = {
388 { "L1-dcache", "l1-d", "l1d", "L1-data", },
389 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
390 { "LLC", "L2", },
391 { "dTLB", "d-tlb", "Data-TLB", },
392 { "iTLB", "i-tlb", "Instruction-TLB", },
393 { "branch", "branches", "bpu", "btb", "bpc", },
394 { "node", },
395};
396
397const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
398 [PERF_EVSEL__MAX_ALIASES] = {
399 { "load", "loads", "read", },
400 { "store", "stores", "write", },
401 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
402};
403
404const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
405 [PERF_EVSEL__MAX_ALIASES] = {
406 { "refs", "Reference", "ops", "access", },
407 { "misses", "miss", },
408};
409
410#define C(x) PERF_COUNT_HW_CACHE_##x
411#define CACHE_READ (1 << C(OP_READ))
412#define CACHE_WRITE (1 << C(OP_WRITE))
413#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
414#define COP(x) (1 << x)
415
416/*
417 * cache operartion stat
418 * L1I : Read and prefetch only
419 * ITLB and BPU : Read-only
420 */
421static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
422 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
423 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
424 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
425 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
426 [C(ITLB)] = (CACHE_READ),
427 [C(BPU)] = (CACHE_READ),
428 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
429};
430
431bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
432{
433 if (perf_evsel__hw_cache_stat[type] & COP(op))
434 return true; /* valid */
435 else
436 return false; /* invalid */
437}
438
439int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
440 char *bf, size_t size)
441{
442 if (result) {
443 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
444 perf_evsel__hw_cache_op[op][0],
445 perf_evsel__hw_cache_result[result][0]);
446 }
447
448 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
449 perf_evsel__hw_cache_op[op][1]);
450}
451
dd4f5223 452static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
0b668bc9
ACM
453{
454 u8 op, result, type = (config >> 0) & 0xff;
455 const char *err = "unknown-ext-hardware-cache-type";
456
457 if (type > PERF_COUNT_HW_CACHE_MAX)
458 goto out_err;
459
460 op = (config >> 8) & 0xff;
461 err = "unknown-ext-hardware-cache-op";
462 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
463 goto out_err;
464
465 result = (config >> 16) & 0xff;
466 err = "unknown-ext-hardware-cache-result";
467 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
468 goto out_err;
469
470 err = "invalid-cache";
471 if (!perf_evsel__is_cache_op_valid(type, op))
472 goto out_err;
473
474 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
475out_err:
476 return scnprintf(bf, size, "%s", err);
477}
478
479static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
480{
481 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
482 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
483}
484
6eef3d9c
ACM
485static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
486{
487 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
488 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
489}
490
7289f83c 491const char *perf_evsel__name(struct perf_evsel *evsel)
a4460836 492{
7289f83c 493 char bf[128];
a4460836 494
7289f83c
ACM
495 if (evsel->name)
496 return evsel->name;
c410431c
ACM
497
498 switch (evsel->attr.type) {
499 case PERF_TYPE_RAW:
6eef3d9c 500 perf_evsel__raw_name(evsel, bf, sizeof(bf));
c410431c
ACM
501 break;
502
503 case PERF_TYPE_HARDWARE:
7289f83c 504 perf_evsel__hw_name(evsel, bf, sizeof(bf));
c410431c 505 break;
0b668bc9
ACM
506
507 case PERF_TYPE_HW_CACHE:
7289f83c 508 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
0b668bc9
ACM
509 break;
510
335c2f5d 511 case PERF_TYPE_SOFTWARE:
7289f83c 512 perf_evsel__sw_name(evsel, bf, sizeof(bf));
335c2f5d
ACM
513 break;
514
a4460836 515 case PERF_TYPE_TRACEPOINT:
7289f83c 516 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
a4460836
ACM
517 break;
518
287e74aa
JO
519 case PERF_TYPE_BREAKPOINT:
520 perf_evsel__bp_name(evsel, bf, sizeof(bf));
521 break;
522
c410431c 523 default:
ca1b1457
RR
524 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
525 evsel->attr.type);
a4460836 526 break;
c410431c
ACM
527 }
528
7289f83c
ACM
529 evsel->name = strdup(bf);
530
531 return evsel->name ?: "unknown";
c410431c
ACM
532}
533
717e263f
NK
534const char *perf_evsel__group_name(struct perf_evsel *evsel)
535{
536 return evsel->group_name ?: "anon group";
537}
538
539int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
540{
541 int ret;
542 struct perf_evsel *pos;
543 const char *group_name = perf_evsel__group_name(evsel);
544
545 ret = scnprintf(buf, size, "%s", group_name);
546
547 ret += scnprintf(buf + ret, size - ret, " { %s",
548 perf_evsel__name(evsel));
549
550 for_each_group_member(pos, evsel)
551 ret += scnprintf(buf + ret, size - ret, ", %s",
552 perf_evsel__name(pos));
553
554 ret += scnprintf(buf + ret, size - ret, " }");
555
556 return ret;
557}
558
6bedfab6 559static void
aad2b21c 560perf_evsel__config_callgraph(struct perf_evsel *evsel,
c3a6a8c4
KL
561 struct record_opts *opts,
562 struct callchain_param *param)
6bedfab6
JO
563{
564 bool function = perf_evsel__is_function_event(evsel);
565 struct perf_event_attr *attr = &evsel->attr;
566
567 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
568
c3a6a8c4 569 if (param->record_mode == CALLCHAIN_LBR) {
aad2b21c
KL
570 if (!opts->branch_stack) {
571 if (attr->exclude_user) {
572 pr_warning("LBR callstack option is only available "
573 "to get user callchain information. "
574 "Falling back to framepointers.\n");
575 } else {
576 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
577 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
bd0f8895
AK
578 PERF_SAMPLE_BRANCH_CALL_STACK |
579 PERF_SAMPLE_BRANCH_NO_CYCLES |
580 PERF_SAMPLE_BRANCH_NO_FLAGS;
aad2b21c
KL
581 }
582 } else
583 pr_warning("Cannot use LBR callstack with branch stack. "
584 "Falling back to framepointers.\n");
585 }
586
c3a6a8c4 587 if (param->record_mode == CALLCHAIN_DWARF) {
6bedfab6
JO
588 if (!function) {
589 perf_evsel__set_sample_bit(evsel, REGS_USER);
590 perf_evsel__set_sample_bit(evsel, STACK_USER);
591 attr->sample_regs_user = PERF_REGS_MASK;
c3a6a8c4 592 attr->sample_stack_user = param->dump_size;
6bedfab6
JO
593 attr->exclude_callchain_user = 1;
594 } else {
595 pr_info("Cannot use DWARF unwind for function trace event,"
596 " falling back to framepointers.\n");
597 }
598 }
599
600 if (function) {
601 pr_info("Disabling user space callchains for function trace event.\n");
602 attr->exclude_callchain_user = 1;
603 }
604}
605
d457c963
KL
606static void
607perf_evsel__reset_callgraph(struct perf_evsel *evsel,
608 struct callchain_param *param)
609{
610 struct perf_event_attr *attr = &evsel->attr;
611
612 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
613 if (param->record_mode == CALLCHAIN_LBR) {
614 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
615 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
616 PERF_SAMPLE_BRANCH_CALL_STACK);
617 }
618 if (param->record_mode == CALLCHAIN_DWARF) {
619 perf_evsel__reset_sample_bit(evsel, REGS_USER);
620 perf_evsel__reset_sample_bit(evsel, STACK_USER);
621 }
622}
623
624static void apply_config_terms(struct perf_evsel *evsel,
625 struct record_opts *opts)
930a2e29
JO
626{
627 struct perf_evsel_config_term *term;
32067712
KL
628 struct list_head *config_terms = &evsel->config_terms;
629 struct perf_event_attr *attr = &evsel->attr;
d457c963
KL
630 struct callchain_param param;
631 u32 dump_size = 0;
632 char *callgraph_buf = NULL;
633
634 /* callgraph default */
635 param.record_mode = callchain_param.record_mode;
930a2e29
JO
636
637 list_for_each_entry(term, config_terms, list) {
638 switch (term->type) {
ee4c7588
JO
639 case PERF_EVSEL__CONFIG_TERM_PERIOD:
640 attr->sample_period = term->val.period;
ab35a7d0 641 attr->freq = 0;
32067712 642 break;
09af2a55
NK
643 case PERF_EVSEL__CONFIG_TERM_FREQ:
644 attr->sample_freq = term->val.freq;
645 attr->freq = 1;
646 break;
32067712
KL
647 case PERF_EVSEL__CONFIG_TERM_TIME:
648 if (term->val.time)
649 perf_evsel__set_sample_bit(evsel, TIME);
650 else
651 perf_evsel__reset_sample_bit(evsel, TIME);
652 break;
d457c963
KL
653 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
654 callgraph_buf = term->val.callgraph;
655 break;
656 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
657 dump_size = term->val.stack_user;
658 break;
374ce938
WN
659 case PERF_EVSEL__CONFIG_TERM_INHERIT:
660 /*
661 * attr->inherit should has already been set by
662 * perf_evsel__config. If user explicitly set
663 * inherit using config terms, override global
664 * opt->no_inherit setting.
665 */
666 attr->inherit = term->val.inherit ? 1 : 0;
667 break;
930a2e29
JO
668 default:
669 break;
670 }
671 }
d457c963
KL
672
673 /* User explicitly set per-event callgraph, clear the old setting and reset. */
674 if ((callgraph_buf != NULL) || (dump_size > 0)) {
675
676 /* parse callgraph parameters */
677 if (callgraph_buf != NULL) {
f9db0d0f
KL
678 if (!strcmp(callgraph_buf, "no")) {
679 param.enabled = false;
680 param.record_mode = CALLCHAIN_NONE;
681 } else {
682 param.enabled = true;
683 if (parse_callchain_record(callgraph_buf, &param)) {
684 pr_err("per-event callgraph setting for %s failed. "
685 "Apply callgraph global setting for it\n",
686 evsel->name);
687 return;
688 }
d457c963
KL
689 }
690 }
691 if (dump_size > 0) {
692 dump_size = round_up(dump_size, sizeof(u64));
693 param.dump_size = dump_size;
694 }
695
696 /* If global callgraph set, clear it */
697 if (callchain_param.enabled)
698 perf_evsel__reset_callgraph(evsel, &callchain_param);
699
700 /* set perf-event callgraph */
701 if (param.enabled)
702 perf_evsel__config_callgraph(evsel, opts, &param);
703 }
930a2e29
JO
704}
705
774cb499
JO
706/*
707 * The enable_on_exec/disabled value strategy:
708 *
709 * 1) For any type of traced program:
710 * - all independent events and group leaders are disabled
711 * - all group members are enabled
712 *
713 * Group members are ruled by group leaders. They need to
714 * be enabled, because the group scheduling relies on that.
715 *
716 * 2) For traced programs executed by perf:
717 * - all independent events and group leaders have
718 * enable_on_exec set
719 * - we don't specifically enable or disable any event during
720 * the record command
721 *
722 * Independent events and group leaders are initially disabled
723 * and get enabled by exec. Group members are ruled by group
724 * leaders as stated in 1).
725 *
726 * 3) For traced programs attached by perf (pid/tid):
727 * - we specifically enable or disable all events during
728 * the record command
729 *
730 * When attaching events to already running traced we
731 * enable/disable events specifically, as there's no
732 * initial traced exec call.
733 */
b4006796 734void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
0f82ebc4 735{
3c176311 736 struct perf_evsel *leader = evsel->leader;
0f82ebc4 737 struct perf_event_attr *attr = &evsel->attr;
60b0896c 738 int track = evsel->tracking;
3aa5939d 739 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
0f82ebc4 740
594ac61a 741 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
0f82ebc4 742 attr->inherit = !opts->no_inherit;
0f82ebc4 743
7be5ebe8
ACM
744 perf_evsel__set_sample_bit(evsel, IP);
745 perf_evsel__set_sample_bit(evsel, TID);
0f82ebc4 746
3c176311
JO
747 if (evsel->sample_read) {
748 perf_evsel__set_sample_bit(evsel, READ);
749
750 /*
751 * We need ID even in case of single event, because
752 * PERF_SAMPLE_READ process ID specific data.
753 */
75562573 754 perf_evsel__set_sample_id(evsel, false);
3c176311
JO
755
756 /*
757 * Apply group format only if we belong to group
758 * with more than one members.
759 */
760 if (leader->nr_members > 1) {
761 attr->read_format |= PERF_FORMAT_GROUP;
762 attr->inherit = 0;
763 }
764 }
765
0f82ebc4 766 /*
17314e23 767 * We default some events to have a default interval. But keep
0f82ebc4
ACM
768 * it a weak assumption overridable by the user.
769 */
17314e23 770 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
0f82ebc4
ACM
771 opts->user_interval != ULLONG_MAX)) {
772 if (opts->freq) {
7be5ebe8 773 perf_evsel__set_sample_bit(evsel, PERIOD);
0f82ebc4
ACM
774 attr->freq = 1;
775 attr->sample_freq = opts->freq;
776 } else {
777 attr->sample_period = opts->default_interval;
778 }
779 }
780
3c176311
JO
781 /*
782 * Disable sampling for all group members other
783 * than leader in case leader 'leads' the sampling.
784 */
785 if ((leader != evsel) && leader->sample_read) {
786 attr->sample_freq = 0;
787 attr->sample_period = 0;
788 }
789
0f82ebc4
ACM
790 if (opts->no_samples)
791 attr->sample_freq = 0;
792
793 if (opts->inherit_stat)
794 attr->inherit_stat = 1;
795
796 if (opts->sample_address) {
7be5ebe8 797 perf_evsel__set_sample_bit(evsel, ADDR);
0f82ebc4
ACM
798 attr->mmap_data = track;
799 }
800
f140373b
JO
801 /*
802 * We don't allow user space callchains for function trace
803 * event, due to issues with page faults while tracing page
804 * fault handler and its overall trickiness nature.
805 */
806 if (perf_evsel__is_function_event(evsel))
807 evsel->attr.exclude_callchain_user = 1;
808
72a128aa 809 if (callchain_param.enabled && !evsel->no_aux_samples)
c3a6a8c4 810 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
26d33022 811
6a21c0b5 812 if (opts->sample_intr_regs) {
bcc84ec6 813 attr->sample_regs_intr = opts->sample_intr_regs;
6a21c0b5
SE
814 perf_evsel__set_sample_bit(evsel, REGS_INTR);
815 }
816
3aa5939d 817 if (target__has_cpu(&opts->target))
7be5ebe8 818 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4 819
3e76ac78 820 if (opts->period)
7be5ebe8 821 perf_evsel__set_sample_bit(evsel, PERIOD);
3e76ac78 822
8affc2b8
AK
823 /*
824 * When the user explicitely disabled time don't force it here.
825 */
826 if (opts->sample_time &&
827 (!perf_missing_features.sample_id_all &&
3abebc55
AH
828 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
829 opts->sample_time_set)))
7be5ebe8 830 perf_evsel__set_sample_bit(evsel, TIME);
0f82ebc4 831
6ff1ce76 832 if (opts->raw_samples && !evsel->no_aux_samples) {
7be5ebe8
ACM
833 perf_evsel__set_sample_bit(evsel, TIME);
834 perf_evsel__set_sample_bit(evsel, RAW);
835 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4
ACM
836 }
837
ccf49bfc 838 if (opts->sample_address)
1e7ed5ec 839 perf_evsel__set_sample_bit(evsel, DATA_SRC);
ccf49bfc 840
509051ea 841 if (opts->no_buffering) {
0f82ebc4
ACM
842 attr->watermark = 0;
843 attr->wakeup_events = 1;
844 }
6ff1ce76 845 if (opts->branch_stack && !evsel->no_aux_samples) {
7be5ebe8 846 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
bdfebd84
RAV
847 attr->branch_sample_type = opts->branch_stack;
848 }
0f82ebc4 849
05484298 850 if (opts->sample_weight)
1e7ed5ec 851 perf_evsel__set_sample_bit(evsel, WEIGHT);
05484298 852
62e503b7 853 attr->task = track;
5c5e854b 854 attr->mmap = track;
a5a5ba72 855 attr->mmap2 = track && !perf_missing_features.mmap2;
5c5e854b 856 attr->comm = track;
0f82ebc4 857
b757bb09
AH
858 if (opts->record_switch_events)
859 attr->context_switch = track;
860
475eeab9 861 if (opts->sample_transaction)
1e7ed5ec 862 perf_evsel__set_sample_bit(evsel, TRANSACTION);
475eeab9 863
85c273d2
AK
864 if (opts->running_time) {
865 evsel->attr.read_format |=
866 PERF_FORMAT_TOTAL_TIME_ENABLED |
867 PERF_FORMAT_TOTAL_TIME_RUNNING;
868 }
869
774cb499
JO
870 /*
871 * XXX see the function comment above
872 *
873 * Disabling only independent events or group leaders,
874 * keeping group members enabled.
875 */
823254ed 876 if (perf_evsel__is_group_leader(evsel))
774cb499
JO
877 attr->disabled = 1;
878
879 /*
880 * Setting enable_on_exec for independent events and
881 * group leaders for traced executed by perf.
882 */
6619a53e
AK
883 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
884 !opts->initial_delay)
0f82ebc4 885 attr->enable_on_exec = 1;
2afd2bcf
AH
886
887 if (evsel->immediate) {
888 attr->disabled = 0;
889 attr->enable_on_exec = 0;
890 }
814c8c38
PZ
891
892 clockid = opts->clockid;
893 if (opts->use_clockid) {
894 attr->use_clockid = 1;
895 attr->clockid = opts->clockid;
896 }
930a2e29 897
7f94af7a
JO
898 if (evsel->precise_max)
899 perf_event_attr__set_max_precise_ip(attr);
900
85723885
JO
901 if (opts->all_user) {
902 attr->exclude_kernel = 1;
903 attr->exclude_user = 0;
904 }
905
906 if (opts->all_kernel) {
907 attr->exclude_kernel = 0;
908 attr->exclude_user = 1;
909 }
910
930a2e29
JO
911 /*
912 * Apply event specific term settings,
913 * it overloads any global configuration.
914 */
d457c963 915 apply_config_terms(evsel, opts);
0f82ebc4
ACM
916}
917
8885846f 918static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
69aad6f1 919{
4af4c955 920 int cpu, thread;
bf8e8f4b
AH
921
922 if (evsel->system_wide)
923 nthreads = 1;
924
69aad6f1 925 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
4af4c955
DA
926
927 if (evsel->fd) {
928 for (cpu = 0; cpu < ncpus; cpu++) {
929 for (thread = 0; thread < nthreads; thread++) {
930 FD(evsel, cpu, thread) = -1;
931 }
932 }
933 }
934
69aad6f1
ACM
935 return evsel->fd != NULL ? 0 : -ENOMEM;
936}
937
e2407bef
AK
938static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
939 int ioc, void *arg)
745cefc5
ACM
940{
941 int cpu, thread;
942
bf8e8f4b
AH
943 if (evsel->system_wide)
944 nthreads = 1;
945
745cefc5
ACM
946 for (cpu = 0; cpu < ncpus; cpu++) {
947 for (thread = 0; thread < nthreads; thread++) {
948 int fd = FD(evsel, cpu, thread),
e2407bef 949 err = ioctl(fd, ioc, arg);
745cefc5
ACM
950
951 if (err)
952 return err;
953 }
954 }
955
956 return 0;
957}
958
f47805a2
ACM
959int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
960 const char *filter)
e2407bef
AK
961{
962 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
963 PERF_EVENT_IOC_SET_FILTER,
964 (void *)filter);
965}
966
12467ae4
ACM
967int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
968{
969 char *new_filter = strdup(filter);
970
971 if (new_filter != NULL) {
972 free(evsel->filter);
973 evsel->filter = new_filter;
974 return 0;
975 }
976
977 return -1;
978}
979
64ec84f5
ACM
980int perf_evsel__append_filter(struct perf_evsel *evsel,
981 const char *op, const char *filter)
982{
983 char *new_filter;
984
985 if (evsel->filter == NULL)
986 return perf_evsel__set_filter(evsel, filter);
987
988 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
989 free(evsel->filter);
990 evsel->filter = new_filter;
991 return 0;
992 }
993
994 return -1;
995}
996
5cd95fc3 997int perf_evsel__enable(struct perf_evsel *evsel)
e2407bef 998{
5cd95fc3
JO
999 int nthreads = thread_map__nr(evsel->threads);
1000 int ncpus = cpu_map__nr(evsel->cpus);
1001
e2407bef
AK
1002 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1003 PERF_EVENT_IOC_ENABLE,
1004 0);
1005}
1006
e98a4cbb
JO
1007int perf_evsel__disable(struct perf_evsel *evsel)
1008{
1009 int nthreads = thread_map__nr(evsel->threads);
1010 int ncpus = cpu_map__nr(evsel->cpus);
1011
1012 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1013 PERF_EVENT_IOC_DISABLE,
1014 0);
1015}
1016
70db7533
ACM
1017int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1018{
8d9cbd8f
VG
1019 if (ncpus == 0 || nthreads == 0)
1020 return 0;
1021
bf8e8f4b
AH
1022 if (evsel->system_wide)
1023 nthreads = 1;
1024
a91e5431
ACM
1025 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1026 if (evsel->sample_id == NULL)
1027 return -ENOMEM;
1028
1029 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1030 if (evsel->id == NULL) {
1031 xyarray__delete(evsel->sample_id);
1032 evsel->sample_id = NULL;
1033 return -ENOMEM;
1034 }
1035
1036 return 0;
70db7533
ACM
1037}
1038
8885846f 1039static void perf_evsel__free_fd(struct perf_evsel *evsel)
69aad6f1
ACM
1040{
1041 xyarray__delete(evsel->fd);
1042 evsel->fd = NULL;
1043}
1044
8885846f 1045static void perf_evsel__free_id(struct perf_evsel *evsel)
70db7533 1046{
a91e5431
ACM
1047 xyarray__delete(evsel->sample_id);
1048 evsel->sample_id = NULL;
04662523 1049 zfree(&evsel->id);
70db7533
ACM
1050}
1051
930a2e29
JO
1052static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1053{
1054 struct perf_evsel_config_term *term, *h;
1055
1056 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1057 list_del(&term->list);
1058 free(term);
1059 }
1060}
1061
c52b12ed
ACM
1062void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1063{
1064 int cpu, thread;
1065
bf8e8f4b
AH
1066 if (evsel->system_wide)
1067 nthreads = 1;
1068
c52b12ed
ACM
1069 for (cpu = 0; cpu < ncpus; cpu++)
1070 for (thread = 0; thread < nthreads; ++thread) {
1071 close(FD(evsel, cpu, thread));
1072 FD(evsel, cpu, thread) = -1;
1073 }
1074}
1075
ef1d1af2 1076void perf_evsel__exit(struct perf_evsel *evsel)
69aad6f1
ACM
1077{
1078 assert(list_empty(&evsel->node));
d49e4695 1079 assert(evsel->evlist == NULL);
736b05a0
NK
1080 perf_evsel__free_fd(evsel);
1081 perf_evsel__free_id(evsel);
930a2e29 1082 perf_evsel__free_config_terms(evsel);
597e48c1 1083 close_cgroup(evsel->cgrp);
f30a79b0 1084 cpu_map__put(evsel->cpus);
fce4d296 1085 cpu_map__put(evsel->own_cpus);
578e91ec 1086 thread_map__put(evsel->threads);
597e48c1 1087 zfree(&evsel->group_name);
597e48c1 1088 zfree(&evsel->name);
ce8ccff5 1089 perf_evsel__object.fini(evsel);
ef1d1af2
ACM
1090}
1091
1092void perf_evsel__delete(struct perf_evsel *evsel)
1093{
1094 perf_evsel__exit(evsel);
69aad6f1
ACM
1095 free(evsel);
1096}
c52b12ed 1097
a6fa0038 1098void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
857a94a2 1099 struct perf_counts_values *count)
c7a79c47
SE
1100{
1101 struct perf_counts_values tmp;
1102
1103 if (!evsel->prev_raw_counts)
1104 return;
1105
1106 if (cpu == -1) {
1107 tmp = evsel->prev_raw_counts->aggr;
1108 evsel->prev_raw_counts->aggr = *count;
1109 } else {
a6fa0038
JO
1110 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1111 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
c7a79c47
SE
1112 }
1113
1114 count->val = count->val - tmp.val;
1115 count->ena = count->ena - tmp.ena;
1116 count->run = count->run - tmp.run;
1117}
1118
13112bbf
JO
1119void perf_counts_values__scale(struct perf_counts_values *count,
1120 bool scale, s8 *pscaled)
1121{
1122 s8 scaled = 0;
1123
1124 if (scale) {
1125 if (count->run == 0) {
1126 scaled = -1;
1127 count->val = 0;
1128 } else if (count->run < count->ena) {
1129 scaled = 1;
1130 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1131 }
1132 } else
1133 count->ena = count->run = 0;
1134
1135 if (pscaled)
1136 *pscaled = scaled;
1137}
1138
f99f4719
JO
1139int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1140 struct perf_counts_values *count)
1141{
1142 memset(count, 0, sizeof(*count));
1143
1144 if (FD(evsel, cpu, thread) < 0)
1145 return -EINVAL;
1146
1147 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1148 return -errno;
1149
1150 return 0;
1151}
1152
c52b12ed
ACM
1153int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1154 int cpu, int thread, bool scale)
1155{
1156 struct perf_counts_values count;
1157 size_t nv = scale ? 3 : 1;
1158
1159 if (FD(evsel, cpu, thread) < 0)
1160 return -EINVAL;
1161
a6fa0038 1162 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
4eed11d5
ACM
1163 return -ENOMEM;
1164
c52b12ed
ACM
1165 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1166 return -errno;
1167
a6fa0038 1168 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
13112bbf 1169 perf_counts_values__scale(&count, scale, NULL);
a6fa0038 1170 *perf_counts(evsel->counts, cpu, thread) = count;
c52b12ed
ACM
1171 return 0;
1172}
1173
6a4bb04c
JO
1174static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1175{
1176 struct perf_evsel *leader = evsel->leader;
1177 int fd;
1178
823254ed 1179 if (perf_evsel__is_group_leader(evsel))
6a4bb04c
JO
1180 return -1;
1181
1182 /*
1183 * Leader must be already processed/open,
1184 * if not it's a bug.
1185 */
1186 BUG_ON(!leader->fd);
1187
1188 fd = FD(leader, cpu, thread);
1189 BUG_ON(fd == -1);
1190
1191 return fd;
1192}
1193
2c5e8c52
PZ
1194struct bit_names {
1195 int bit;
1196 const char *name;
1197};
1198
1199static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1200{
1201 bool first_bit = true;
1202 int i = 0;
1203
1204 do {
1205 if (value & bits[i].bit) {
1206 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1207 first_bit = false;
1208 }
1209 } while (bits[++i].name != NULL);
1210}
1211
1212static void __p_sample_type(char *buf, size_t size, u64 value)
1213{
1214#define bit_name(n) { PERF_SAMPLE_##n, #n }
1215 struct bit_names bits[] = {
1216 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1217 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1218 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1219 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
84422592 1220 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
dcdd184b 1221 bit_name(WEIGHT),
2c5e8c52
PZ
1222 { .name = NULL, }
1223 };
1224#undef bit_name
1225 __p_bits(buf, size, value, bits);
1226}
1227
1228static void __p_read_format(char *buf, size_t size, u64 value)
1229{
1230#define bit_name(n) { PERF_FORMAT_##n, #n }
1231 struct bit_names bits[] = {
1232 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1233 bit_name(ID), bit_name(GROUP),
1234 { .name = NULL, }
1235 };
1236#undef bit_name
1237 __p_bits(buf, size, value, bits);
1238}
1239
1240#define BUF_SIZE 1024
1241
7310aed7 1242#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
2c5e8c52
PZ
1243#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1244#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1245#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1246#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1247
1248#define PRINT_ATTRn(_n, _f, _p) \
1249do { \
1250 if (attr->_f) { \
1251 _p(attr->_f); \
1252 ret += attr__fprintf(fp, _n, buf, priv);\
1253 } \
1254} while (0)
1255
1256#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1257
1258int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1259 attr__fprintf_f attr__fprintf, void *priv)
1260{
1261 char buf[BUF_SIZE];
1262 int ret = 0;
1263
1264 PRINT_ATTRf(type, p_unsigned);
1265 PRINT_ATTRf(size, p_unsigned);
1266 PRINT_ATTRf(config, p_hex);
1267 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1268 PRINT_ATTRf(sample_type, p_sample_type);
1269 PRINT_ATTRf(read_format, p_read_format);
1270
1271 PRINT_ATTRf(disabled, p_unsigned);
1272 PRINT_ATTRf(inherit, p_unsigned);
1273 PRINT_ATTRf(pinned, p_unsigned);
1274 PRINT_ATTRf(exclusive, p_unsigned);
1275 PRINT_ATTRf(exclude_user, p_unsigned);
1276 PRINT_ATTRf(exclude_kernel, p_unsigned);
1277 PRINT_ATTRf(exclude_hv, p_unsigned);
1278 PRINT_ATTRf(exclude_idle, p_unsigned);
1279 PRINT_ATTRf(mmap, p_unsigned);
1280 PRINT_ATTRf(comm, p_unsigned);
1281 PRINT_ATTRf(freq, p_unsigned);
1282 PRINT_ATTRf(inherit_stat, p_unsigned);
1283 PRINT_ATTRf(enable_on_exec, p_unsigned);
1284 PRINT_ATTRf(task, p_unsigned);
1285 PRINT_ATTRf(watermark, p_unsigned);
1286 PRINT_ATTRf(precise_ip, p_unsigned);
1287 PRINT_ATTRf(mmap_data, p_unsigned);
1288 PRINT_ATTRf(sample_id_all, p_unsigned);
1289 PRINT_ATTRf(exclude_host, p_unsigned);
1290 PRINT_ATTRf(exclude_guest, p_unsigned);
1291 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1292 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1293 PRINT_ATTRf(mmap2, p_unsigned);
1294 PRINT_ATTRf(comm_exec, p_unsigned);
1295 PRINT_ATTRf(use_clockid, p_unsigned);
0286039f 1296 PRINT_ATTRf(context_switch, p_unsigned);
2c5e8c52
PZ
1297
1298 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1299 PRINT_ATTRf(bp_type, p_unsigned);
1300 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1301 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
8b8cde49 1302 PRINT_ATTRf(branch_sample_type, p_unsigned);
2c5e8c52
PZ
1303 PRINT_ATTRf(sample_regs_user, p_hex);
1304 PRINT_ATTRf(sample_stack_user, p_unsigned);
1305 PRINT_ATTRf(clockid, p_signed);
1306 PRINT_ATTRf(sample_regs_intr, p_hex);
70d73de4 1307 PRINT_ATTRf(aux_watermark, p_unsigned);
e3e1a54f
AH
1308
1309 return ret;
1310}
1311
2c5e8c52
PZ
1312static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1313 void *priv __attribute__((unused)))
1314{
1315 return fprintf(fp, " %-32s %s\n", name, val);
1316}
1317
0252208e 1318static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 1319 struct thread_map *threads)
48290609 1320{
bf8e8f4b 1321 int cpu, thread, nthreads;
57480d2c 1322 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
727ab04e 1323 int pid = -1, err;
bec19672 1324 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
48290609 1325
bf8e8f4b
AH
1326 if (evsel->system_wide)
1327 nthreads = 1;
1328 else
1329 nthreads = threads->nr;
1330
0252208e 1331 if (evsel->fd == NULL &&
bf8e8f4b 1332 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
727ab04e 1333 return -ENOMEM;
4eed11d5 1334
023695d9 1335 if (evsel->cgrp) {
57480d2c 1336 flags |= PERF_FLAG_PID_CGROUP;
023695d9
SE
1337 pid = evsel->cgrp->fd;
1338 }
1339
594ac61a 1340fallback_missing_features:
814c8c38
PZ
1341 if (perf_missing_features.clockid_wrong)
1342 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1343 if (perf_missing_features.clockid) {
1344 evsel->attr.use_clockid = 0;
1345 evsel->attr.clockid = 0;
1346 }
57480d2c
YD
1347 if (perf_missing_features.cloexec)
1348 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
5c5e854b
SE
1349 if (perf_missing_features.mmap2)
1350 evsel->attr.mmap2 = 0;
594ac61a
ACM
1351 if (perf_missing_features.exclude_guest)
1352 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
bd0f8895
AK
1353 if (perf_missing_features.lbr_flags)
1354 evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1355 PERF_SAMPLE_BRANCH_NO_CYCLES);
594ac61a
ACM
1356retry_sample_id:
1357 if (perf_missing_features.sample_id_all)
1358 evsel->attr.sample_id_all = 0;
1359
2c5e8c52
PZ
1360 if (verbose >= 2) {
1361 fprintf(stderr, "%.60s\n", graph_dotted_line);
1362 fprintf(stderr, "perf_event_attr:\n");
1363 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1364 fprintf(stderr, "%.60s\n", graph_dotted_line);
1365 }
e3e1a54f 1366
86bd5e86 1367 for (cpu = 0; cpu < cpus->nr; cpu++) {
9d04f178 1368
bf8e8f4b 1369 for (thread = 0; thread < nthreads; thread++) {
6a4bb04c 1370 int group_fd;
023695d9 1371
bf8e8f4b 1372 if (!evsel->cgrp && !evsel->system_wide)
e13798c7 1373 pid = thread_map__pid(threads, thread);
023695d9 1374
6a4bb04c 1375 group_fd = get_group_fd(evsel, cpu, thread);
bec19672 1376retry_open:
a33f6efc 1377 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
e3e1a54f
AH
1378 pid, cpus->map[cpu], group_fd, flags);
1379
0252208e 1380 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
023695d9 1381 pid,
f08199d3 1382 cpus->map[cpu],
023695d9 1383 group_fd, flags);
727ab04e
ACM
1384 if (FD(evsel, cpu, thread) < 0) {
1385 err = -errno;
a33f6efc 1386 pr_debug2("sys_perf_event_open failed, error %d\n",
f852fd62 1387 err);
594ac61a 1388 goto try_fallback;
727ab04e 1389 }
1f45b1d4
WN
1390
1391 if (evsel->bpf_fd >= 0) {
1392 int evt_fd = FD(evsel, cpu, thread);
1393 int bpf_fd = evsel->bpf_fd;
1394
1395 err = ioctl(evt_fd,
1396 PERF_EVENT_IOC_SET_BPF,
1397 bpf_fd);
1398 if (err && errno != EEXIST) {
1399 pr_err("failed to attach bpf fd %d: %s\n",
1400 bpf_fd, strerror(errno));
1401 err = -EINVAL;
1402 goto out_close;
1403 }
1404 }
1405
bec19672 1406 set_rlimit = NO_CHANGE;
814c8c38
PZ
1407
1408 /*
1409 * If we succeeded but had to kill clockid, fail and
1410 * have perf_evsel__open_strerror() print us a nice
1411 * error.
1412 */
1413 if (perf_missing_features.clockid ||
1414 perf_missing_features.clockid_wrong) {
1415 err = -EINVAL;
1416 goto out_close;
1417 }
0252208e 1418 }
48290609
ACM
1419 }
1420
1421 return 0;
1422
594ac61a 1423try_fallback:
bec19672
AK
1424 /*
1425 * perf stat needs between 5 and 22 fds per CPU. When we run out
1426 * of them try to increase the limits.
1427 */
1428 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1429 struct rlimit l;
1430 int old_errno = errno;
1431
1432 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1433 if (set_rlimit == NO_CHANGE)
1434 l.rlim_cur = l.rlim_max;
1435 else {
1436 l.rlim_cur = l.rlim_max + 1000;
1437 l.rlim_max = l.rlim_cur;
1438 }
1439 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1440 set_rlimit++;
1441 errno = old_errno;
1442 goto retry_open;
1443 }
1444 }
1445 errno = old_errno;
1446 }
1447
594ac61a
ACM
1448 if (err != -EINVAL || cpu > 0 || thread > 0)
1449 goto out_close;
1450
814c8c38
PZ
1451 /*
1452 * Must probe features in the order they were added to the
1453 * perf_event_attr interface.
1454 */
1455 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1456 perf_missing_features.clockid_wrong = true;
1457 goto fallback_missing_features;
1458 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1459 perf_missing_features.clockid = true;
1460 goto fallback_missing_features;
1461 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
57480d2c
YD
1462 perf_missing_features.cloexec = true;
1463 goto fallback_missing_features;
1464 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
5c5e854b
SE
1465 perf_missing_features.mmap2 = true;
1466 goto fallback_missing_features;
1467 } else if (!perf_missing_features.exclude_guest &&
1468 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
594ac61a
ACM
1469 perf_missing_features.exclude_guest = true;
1470 goto fallback_missing_features;
1471 } else if (!perf_missing_features.sample_id_all) {
1472 perf_missing_features.sample_id_all = true;
1473 goto retry_sample_id;
bd0f8895
AK
1474 } else if (!perf_missing_features.lbr_flags &&
1475 (evsel->attr.branch_sample_type &
1476 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1477 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1478 perf_missing_features.lbr_flags = true;
1479 goto fallback_missing_features;
594ac61a
ACM
1480 }
1481
48290609 1482out_close:
0252208e
ACM
1483 do {
1484 while (--thread >= 0) {
1485 close(FD(evsel, cpu, thread));
1486 FD(evsel, cpu, thread) = -1;
1487 }
bf8e8f4b 1488 thread = nthreads;
0252208e 1489 } while (--cpu >= 0);
727ab04e
ACM
1490 return err;
1491}
1492
1493void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1494{
1495 if (evsel->fd == NULL)
1496 return;
1497
1498 perf_evsel__close_fd(evsel, ncpus, nthreads);
1499 perf_evsel__free_fd(evsel);
48290609
ACM
1500}
1501
0252208e
ACM
1502static struct {
1503 struct cpu_map map;
1504 int cpus[1];
1505} empty_cpu_map = {
1506 .map.nr = 1,
1507 .cpus = { -1, },
1508};
1509
1510static struct {
1511 struct thread_map map;
1512 int threads[1];
1513} empty_thread_map = {
1514 .map.nr = 1,
1515 .threads = { -1, },
1516};
1517
f08199d3 1518int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 1519 struct thread_map *threads)
48290609 1520{
0252208e
ACM
1521 if (cpus == NULL) {
1522 /* Work around old compiler warnings about strict aliasing */
1523 cpus = &empty_cpu_map.map;
48290609
ACM
1524 }
1525
0252208e
ACM
1526 if (threads == NULL)
1527 threads = &empty_thread_map.map;
48290609 1528
6a4bb04c 1529 return __perf_evsel__open(evsel, cpus, threads);
48290609
ACM
1530}
1531
f08199d3 1532int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
6a4bb04c 1533 struct cpu_map *cpus)
48290609 1534{
6a4bb04c 1535 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
0252208e 1536}
48290609 1537
f08199d3 1538int perf_evsel__open_per_thread(struct perf_evsel *evsel,
6a4bb04c 1539 struct thread_map *threads)
0252208e 1540{
6a4bb04c 1541 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
48290609 1542}
70082dd9 1543
0807d2d8
ACM
1544static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1545 const union perf_event *event,
1546 struct perf_sample *sample)
d0dd74e8 1547{
0807d2d8 1548 u64 type = evsel->attr.sample_type;
d0dd74e8 1549 const u64 *array = event->sample.array;
0807d2d8 1550 bool swapped = evsel->needs_swap;
37073f9e 1551 union u64_swap u;
d0dd74e8
ACM
1552
1553 array += ((event->header.size -
1554 sizeof(event->header)) / sizeof(u64)) - 1;
1555
75562573
AH
1556 if (type & PERF_SAMPLE_IDENTIFIER) {
1557 sample->id = *array;
1558 array--;
1559 }
1560
d0dd74e8 1561 if (type & PERF_SAMPLE_CPU) {
37073f9e
JO
1562 u.val64 = *array;
1563 if (swapped) {
1564 /* undo swap of u64, then swap on individual u32s */
1565 u.val64 = bswap_64(u.val64);
1566 u.val32[0] = bswap_32(u.val32[0]);
1567 }
1568
1569 sample->cpu = u.val32[0];
d0dd74e8
ACM
1570 array--;
1571 }
1572
1573 if (type & PERF_SAMPLE_STREAM_ID) {
1574 sample->stream_id = *array;
1575 array--;
1576 }
1577
1578 if (type & PERF_SAMPLE_ID) {
1579 sample->id = *array;
1580 array--;
1581 }
1582
1583 if (type & PERF_SAMPLE_TIME) {
1584 sample->time = *array;
1585 array--;
1586 }
1587
1588 if (type & PERF_SAMPLE_TID) {
37073f9e
JO
1589 u.val64 = *array;
1590 if (swapped) {
1591 /* undo swap of u64, then swap on individual u32s */
1592 u.val64 = bswap_64(u.val64);
1593 u.val32[0] = bswap_32(u.val32[0]);
1594 u.val32[1] = bswap_32(u.val32[1]);
1595 }
1596
1597 sample->pid = u.val32[0];
1598 sample->tid = u.val32[1];
dd44bc6b 1599 array--;
d0dd74e8
ACM
1600 }
1601
1602 return 0;
1603}
1604
03b6ea9b
AH
1605static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1606 u64 size)
98e1da90 1607{
03b6ea9b
AH
1608 return size > max_size || offset + size > endp;
1609}
98e1da90 1610
03b6ea9b
AH
1611#define OVERFLOW_CHECK(offset, size, max_size) \
1612 do { \
1613 if (overflow(endp, (max_size), (offset), (size))) \
1614 return -EFAULT; \
1615 } while (0)
98e1da90 1616
03b6ea9b
AH
1617#define OVERFLOW_CHECK_u64(offset) \
1618 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
98e1da90 1619
a3f698fe 1620int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
0807d2d8 1621 struct perf_sample *data)
d0dd74e8 1622{
a3f698fe 1623 u64 type = evsel->attr.sample_type;
0807d2d8 1624 bool swapped = evsel->needs_swap;
d0dd74e8 1625 const u64 *array;
03b6ea9b
AH
1626 u16 max_size = event->header.size;
1627 const void *endp = (void *)event + max_size;
1628 u64 sz;
d0dd74e8 1629
936be503
DA
1630 /*
1631 * used for cross-endian analysis. See git commit 65014ab3
1632 * for why this goofiness is needed.
1633 */
6a11f92e 1634 union u64_swap u;
936be503 1635
f3bda2c9 1636 memset(data, 0, sizeof(*data));
d0dd74e8
ACM
1637 data->cpu = data->pid = data->tid = -1;
1638 data->stream_id = data->id = data->time = -1ULL;
bc529086 1639 data->period = evsel->attr.sample_period;
05484298 1640 data->weight = 0;
d0dd74e8
ACM
1641
1642 if (event->header.type != PERF_RECORD_SAMPLE) {
a3f698fe 1643 if (!evsel->attr.sample_id_all)
d0dd74e8 1644 return 0;
0807d2d8 1645 return perf_evsel__parse_id_sample(evsel, event, data);
d0dd74e8
ACM
1646 }
1647
1648 array = event->sample.array;
1649
03b6ea9b
AH
1650 /*
1651 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1652 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1653 * check the format does not go past the end of the event.
1654 */
a3f698fe 1655 if (evsel->sample_size + sizeof(event->header) > event->header.size)
a2854124
FW
1656 return -EFAULT;
1657
75562573
AH
1658 data->id = -1ULL;
1659 if (type & PERF_SAMPLE_IDENTIFIER) {
1660 data->id = *array;
1661 array++;
1662 }
1663
d0dd74e8 1664 if (type & PERF_SAMPLE_IP) {
ef89325f 1665 data->ip = *array;
d0dd74e8
ACM
1666 array++;
1667 }
1668
1669 if (type & PERF_SAMPLE_TID) {
936be503
DA
1670 u.val64 = *array;
1671 if (swapped) {
1672 /* undo swap of u64, then swap on individual u32s */
1673 u.val64 = bswap_64(u.val64);
1674 u.val32[0] = bswap_32(u.val32[0]);
1675 u.val32[1] = bswap_32(u.val32[1]);
1676 }
1677
1678 data->pid = u.val32[0];
1679 data->tid = u.val32[1];
d0dd74e8
ACM
1680 array++;
1681 }
1682
1683 if (type & PERF_SAMPLE_TIME) {
1684 data->time = *array;
1685 array++;
1686 }
1687
7cec0922 1688 data->addr = 0;
d0dd74e8
ACM
1689 if (type & PERF_SAMPLE_ADDR) {
1690 data->addr = *array;
1691 array++;
1692 }
1693
d0dd74e8
ACM
1694 if (type & PERF_SAMPLE_ID) {
1695 data->id = *array;
1696 array++;
1697 }
1698
1699 if (type & PERF_SAMPLE_STREAM_ID) {
1700 data->stream_id = *array;
1701 array++;
1702 }
1703
1704 if (type & PERF_SAMPLE_CPU) {
936be503
DA
1705
1706 u.val64 = *array;
1707 if (swapped) {
1708 /* undo swap of u64, then swap on individual u32s */
1709 u.val64 = bswap_64(u.val64);
1710 u.val32[0] = bswap_32(u.val32[0]);
1711 }
1712
1713 data->cpu = u.val32[0];
d0dd74e8
ACM
1714 array++;
1715 }
1716
1717 if (type & PERF_SAMPLE_PERIOD) {
1718 data->period = *array;
1719 array++;
1720 }
1721
1722 if (type & PERF_SAMPLE_READ) {
9ede473c
JO
1723 u64 read_format = evsel->attr.read_format;
1724
03b6ea9b 1725 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1726 if (read_format & PERF_FORMAT_GROUP)
1727 data->read.group.nr = *array;
1728 else
1729 data->read.one.value = *array;
1730
1731 array++;
1732
1733 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
03b6ea9b 1734 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1735 data->read.time_enabled = *array;
1736 array++;
1737 }
1738
1739 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
03b6ea9b 1740 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1741 data->read.time_running = *array;
1742 array++;
1743 }
1744
1745 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1746 if (read_format & PERF_FORMAT_GROUP) {
03b6ea9b
AH
1747 const u64 max_group_nr = UINT64_MAX /
1748 sizeof(struct sample_read_value);
1749
1750 if (data->read.group.nr > max_group_nr)
1751 return -EFAULT;
1752 sz = data->read.group.nr *
1753 sizeof(struct sample_read_value);
1754 OVERFLOW_CHECK(array, sz, max_size);
1755 data->read.group.values =
1756 (struct sample_read_value *)array;
1757 array = (void *)array + sz;
9ede473c 1758 } else {
03b6ea9b 1759 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1760 data->read.one.id = *array;
1761 array++;
1762 }
d0dd74e8
ACM
1763 }
1764
1765 if (type & PERF_SAMPLE_CALLCHAIN) {
03b6ea9b 1766 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
98e1da90 1767
03b6ea9b
AH
1768 OVERFLOW_CHECK_u64(array);
1769 data->callchain = (struct ip_callchain *)array++;
1770 if (data->callchain->nr > max_callchain_nr)
98e1da90 1771 return -EFAULT;
03b6ea9b
AH
1772 sz = data->callchain->nr * sizeof(u64);
1773 OVERFLOW_CHECK(array, sz, max_size);
1774 array = (void *)array + sz;
d0dd74e8
ACM
1775 }
1776
1777 if (type & PERF_SAMPLE_RAW) {
03b6ea9b 1778 OVERFLOW_CHECK_u64(array);
936be503
DA
1779 u.val64 = *array;
1780 if (WARN_ONCE(swapped,
1781 "Endianness of raw data not corrected!\n")) {
1782 /* undo swap of u64, then swap on individual u32s */
1783 u.val64 = bswap_64(u.val64);
1784 u.val32[0] = bswap_32(u.val32[0]);
1785 u.val32[1] = bswap_32(u.val32[1]);
1786 }
936be503 1787 data->raw_size = u.val32[0];
03b6ea9b 1788 array = (void *)array + sizeof(u32);
98e1da90 1789
03b6ea9b
AH
1790 OVERFLOW_CHECK(array, data->raw_size, max_size);
1791 data->raw_data = (void *)array;
1792 array = (void *)array + data->raw_size;
d0dd74e8
ACM
1793 }
1794
b5387528 1795 if (type & PERF_SAMPLE_BRANCH_STACK) {
03b6ea9b
AH
1796 const u64 max_branch_nr = UINT64_MAX /
1797 sizeof(struct branch_entry);
b5387528 1798
03b6ea9b
AH
1799 OVERFLOW_CHECK_u64(array);
1800 data->branch_stack = (struct branch_stack *)array++;
b5387528 1801
03b6ea9b
AH
1802 if (data->branch_stack->nr > max_branch_nr)
1803 return -EFAULT;
b5387528 1804 sz = data->branch_stack->nr * sizeof(struct branch_entry);
03b6ea9b
AH
1805 OVERFLOW_CHECK(array, sz, max_size);
1806 array = (void *)array + sz;
b5387528 1807 }
0f6a3015
JO
1808
1809 if (type & PERF_SAMPLE_REGS_USER) {
03b6ea9b 1810 OVERFLOW_CHECK_u64(array);
5b95a4a3
AH
1811 data->user_regs.abi = *array;
1812 array++;
0f6a3015 1813
5b95a4a3 1814 if (data->user_regs.abi) {
352ea45a 1815 u64 mask = evsel->attr.sample_regs_user;
03b6ea9b 1816
352ea45a 1817 sz = hweight_long(mask) * sizeof(u64);
03b6ea9b 1818 OVERFLOW_CHECK(array, sz, max_size);
352ea45a 1819 data->user_regs.mask = mask;
0f6a3015 1820 data->user_regs.regs = (u64 *)array;
03b6ea9b 1821 array = (void *)array + sz;
0f6a3015
JO
1822 }
1823 }
1824
1825 if (type & PERF_SAMPLE_STACK_USER) {
03b6ea9b
AH
1826 OVERFLOW_CHECK_u64(array);
1827 sz = *array++;
0f6a3015
JO
1828
1829 data->user_stack.offset = ((char *)(array - 1)
1830 - (char *) event);
1831
03b6ea9b 1832 if (!sz) {
0f6a3015
JO
1833 data->user_stack.size = 0;
1834 } else {
03b6ea9b 1835 OVERFLOW_CHECK(array, sz, max_size);
0f6a3015 1836 data->user_stack.data = (char *)array;
03b6ea9b
AH
1837 array = (void *)array + sz;
1838 OVERFLOW_CHECK_u64(array);
54bd2692 1839 data->user_stack.size = *array++;
a65cb4b9
JO
1840 if (WARN_ONCE(data->user_stack.size > sz,
1841 "user stack dump failure\n"))
1842 return -EFAULT;
0f6a3015
JO
1843 }
1844 }
1845
05484298
AK
1846 data->weight = 0;
1847 if (type & PERF_SAMPLE_WEIGHT) {
03b6ea9b 1848 OVERFLOW_CHECK_u64(array);
05484298
AK
1849 data->weight = *array;
1850 array++;
1851 }
1852
98a3b32c
SE
1853 data->data_src = PERF_MEM_DATA_SRC_NONE;
1854 if (type & PERF_SAMPLE_DATA_SRC) {
03b6ea9b 1855 OVERFLOW_CHECK_u64(array);
98a3b32c
SE
1856 data->data_src = *array;
1857 array++;
1858 }
1859
475eeab9
AK
1860 data->transaction = 0;
1861 if (type & PERF_SAMPLE_TRANSACTION) {
87b95524 1862 OVERFLOW_CHECK_u64(array);
475eeab9
AK
1863 data->transaction = *array;
1864 array++;
1865 }
1866
6a21c0b5
SE
1867 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1868 if (type & PERF_SAMPLE_REGS_INTR) {
1869 OVERFLOW_CHECK_u64(array);
1870 data->intr_regs.abi = *array;
1871 array++;
1872
1873 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1874 u64 mask = evsel->attr.sample_regs_intr;
1875
1876 sz = hweight_long(mask) * sizeof(u64);
1877 OVERFLOW_CHECK(array, sz, max_size);
1878 data->intr_regs.mask = mask;
1879 data->intr_regs.regs = (u64 *)array;
1880 array = (void *)array + sz;
1881 }
1882 }
1883
d0dd74e8
ACM
1884 return 0;
1885}
74eec26f 1886
b1cf6f65 1887size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
352ea45a 1888 u64 read_format)
b1cf6f65
AH
1889{
1890 size_t sz, result = sizeof(struct sample_event);
1891
1892 if (type & PERF_SAMPLE_IDENTIFIER)
1893 result += sizeof(u64);
1894
1895 if (type & PERF_SAMPLE_IP)
1896 result += sizeof(u64);
1897
1898 if (type & PERF_SAMPLE_TID)
1899 result += sizeof(u64);
1900
1901 if (type & PERF_SAMPLE_TIME)
1902 result += sizeof(u64);
1903
1904 if (type & PERF_SAMPLE_ADDR)
1905 result += sizeof(u64);
1906
1907 if (type & PERF_SAMPLE_ID)
1908 result += sizeof(u64);
1909
1910 if (type & PERF_SAMPLE_STREAM_ID)
1911 result += sizeof(u64);
1912
1913 if (type & PERF_SAMPLE_CPU)
1914 result += sizeof(u64);
1915
1916 if (type & PERF_SAMPLE_PERIOD)
1917 result += sizeof(u64);
1918
1919 if (type & PERF_SAMPLE_READ) {
1920 result += sizeof(u64);
1921 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1922 result += sizeof(u64);
1923 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1924 result += sizeof(u64);
1925 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1926 if (read_format & PERF_FORMAT_GROUP) {
1927 sz = sample->read.group.nr *
1928 sizeof(struct sample_read_value);
1929 result += sz;
1930 } else {
1931 result += sizeof(u64);
1932 }
1933 }
1934
1935 if (type & PERF_SAMPLE_CALLCHAIN) {
1936 sz = (sample->callchain->nr + 1) * sizeof(u64);
1937 result += sz;
1938 }
1939
1940 if (type & PERF_SAMPLE_RAW) {
1941 result += sizeof(u32);
1942 result += sample->raw_size;
1943 }
1944
1945 if (type & PERF_SAMPLE_BRANCH_STACK) {
1946 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1947 sz += sizeof(u64);
1948 result += sz;
1949 }
1950
1951 if (type & PERF_SAMPLE_REGS_USER) {
1952 if (sample->user_regs.abi) {
1953 result += sizeof(u64);
352ea45a 1954 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
b1cf6f65
AH
1955 result += sz;
1956 } else {
1957 result += sizeof(u64);
1958 }
1959 }
1960
1961 if (type & PERF_SAMPLE_STACK_USER) {
1962 sz = sample->user_stack.size;
1963 result += sizeof(u64);
1964 if (sz) {
1965 result += sz;
1966 result += sizeof(u64);
1967 }
1968 }
1969
1970 if (type & PERF_SAMPLE_WEIGHT)
1971 result += sizeof(u64);
1972
1973 if (type & PERF_SAMPLE_DATA_SRC)
1974 result += sizeof(u64);
1975
42d88910
AH
1976 if (type & PERF_SAMPLE_TRANSACTION)
1977 result += sizeof(u64);
1978
6a21c0b5
SE
1979 if (type & PERF_SAMPLE_REGS_INTR) {
1980 if (sample->intr_regs.abi) {
1981 result += sizeof(u64);
1982 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1983 result += sz;
1984 } else {
1985 result += sizeof(u64);
1986 }
1987 }
1988
b1cf6f65
AH
1989 return result;
1990}
1991
74eec26f 1992int perf_event__synthesize_sample(union perf_event *event, u64 type,
352ea45a 1993 u64 read_format,
74eec26f
AV
1994 const struct perf_sample *sample,
1995 bool swapped)
1996{
1997 u64 *array;
d03f2170 1998 size_t sz;
74eec26f
AV
1999 /*
2000 * used for cross-endian analysis. See git commit 65014ab3
2001 * for why this goofiness is needed.
2002 */
6a11f92e 2003 union u64_swap u;
74eec26f
AV
2004
2005 array = event->sample.array;
2006
75562573
AH
2007 if (type & PERF_SAMPLE_IDENTIFIER) {
2008 *array = sample->id;
2009 array++;
2010 }
2011
74eec26f 2012 if (type & PERF_SAMPLE_IP) {
ef89325f 2013 *array = sample->ip;
74eec26f
AV
2014 array++;
2015 }
2016
2017 if (type & PERF_SAMPLE_TID) {
2018 u.val32[0] = sample->pid;
2019 u.val32[1] = sample->tid;
2020 if (swapped) {
2021 /*
a3f698fe 2022 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
2023 */
2024 u.val32[0] = bswap_32(u.val32[0]);
2025 u.val32[1] = bswap_32(u.val32[1]);
2026 u.val64 = bswap_64(u.val64);
2027 }
2028
2029 *array = u.val64;
2030 array++;
2031 }
2032
2033 if (type & PERF_SAMPLE_TIME) {
2034 *array = sample->time;
2035 array++;
2036 }
2037
2038 if (type & PERF_SAMPLE_ADDR) {
2039 *array = sample->addr;
2040 array++;
2041 }
2042
2043 if (type & PERF_SAMPLE_ID) {
2044 *array = sample->id;
2045 array++;
2046 }
2047
2048 if (type & PERF_SAMPLE_STREAM_ID) {
2049 *array = sample->stream_id;
2050 array++;
2051 }
2052
2053 if (type & PERF_SAMPLE_CPU) {
2054 u.val32[0] = sample->cpu;
2055 if (swapped) {
2056 /*
a3f698fe 2057 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
2058 */
2059 u.val32[0] = bswap_32(u.val32[0]);
2060 u.val64 = bswap_64(u.val64);
2061 }
2062 *array = u.val64;
2063 array++;
2064 }
2065
2066 if (type & PERF_SAMPLE_PERIOD) {
2067 *array = sample->period;
2068 array++;
2069 }
2070
d03f2170
AH
2071 if (type & PERF_SAMPLE_READ) {
2072 if (read_format & PERF_FORMAT_GROUP)
2073 *array = sample->read.group.nr;
2074 else
2075 *array = sample->read.one.value;
2076 array++;
2077
2078 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2079 *array = sample->read.time_enabled;
2080 array++;
2081 }
2082
2083 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2084 *array = sample->read.time_running;
2085 array++;
2086 }
2087
2088 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2089 if (read_format & PERF_FORMAT_GROUP) {
2090 sz = sample->read.group.nr *
2091 sizeof(struct sample_read_value);
2092 memcpy(array, sample->read.group.values, sz);
2093 array = (void *)array + sz;
2094 } else {
2095 *array = sample->read.one.id;
2096 array++;
2097 }
2098 }
2099
2100 if (type & PERF_SAMPLE_CALLCHAIN) {
2101 sz = (sample->callchain->nr + 1) * sizeof(u64);
2102 memcpy(array, sample->callchain, sz);
2103 array = (void *)array + sz;
2104 }
2105
2106 if (type & PERF_SAMPLE_RAW) {
2107 u.val32[0] = sample->raw_size;
2108 if (WARN_ONCE(swapped,
2109 "Endianness of raw data not corrected!\n")) {
2110 /*
2111 * Inverse of what is done in perf_evsel__parse_sample
2112 */
2113 u.val32[0] = bswap_32(u.val32[0]);
2114 u.val32[1] = bswap_32(u.val32[1]);
2115 u.val64 = bswap_64(u.val64);
2116 }
2117 *array = u.val64;
2118 array = (void *)array + sizeof(u32);
2119
2120 memcpy(array, sample->raw_data, sample->raw_size);
2121 array = (void *)array + sample->raw_size;
2122 }
2123
2124 if (type & PERF_SAMPLE_BRANCH_STACK) {
2125 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2126 sz += sizeof(u64);
2127 memcpy(array, sample->branch_stack, sz);
2128 array = (void *)array + sz;
2129 }
2130
2131 if (type & PERF_SAMPLE_REGS_USER) {
2132 if (sample->user_regs.abi) {
2133 *array++ = sample->user_regs.abi;
352ea45a 2134 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
d03f2170
AH
2135 memcpy(array, sample->user_regs.regs, sz);
2136 array = (void *)array + sz;
2137 } else {
2138 *array++ = 0;
2139 }
2140 }
2141
2142 if (type & PERF_SAMPLE_STACK_USER) {
2143 sz = sample->user_stack.size;
2144 *array++ = sz;
2145 if (sz) {
2146 memcpy(array, sample->user_stack.data, sz);
2147 array = (void *)array + sz;
2148 *array++ = sz;
2149 }
2150 }
2151
2152 if (type & PERF_SAMPLE_WEIGHT) {
2153 *array = sample->weight;
2154 array++;
2155 }
2156
2157 if (type & PERF_SAMPLE_DATA_SRC) {
2158 *array = sample->data_src;
2159 array++;
2160 }
2161
42d88910
AH
2162 if (type & PERF_SAMPLE_TRANSACTION) {
2163 *array = sample->transaction;
2164 array++;
2165 }
2166
6a21c0b5
SE
2167 if (type & PERF_SAMPLE_REGS_INTR) {
2168 if (sample->intr_regs.abi) {
2169 *array++ = sample->intr_regs.abi;
2170 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2171 memcpy(array, sample->intr_regs.regs, sz);
2172 array = (void *)array + sz;
2173 } else {
2174 *array++ = 0;
2175 }
2176 }
2177
74eec26f
AV
2178 return 0;
2179}
5555ded4 2180
efd2b924
ACM
2181struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2182{
2183 return pevent_find_field(evsel->tp_format, name);
2184}
2185
5d2074ea 2186void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
5555ded4
ACM
2187 const char *name)
2188{
efd2b924 2189 struct format_field *field = perf_evsel__field(evsel, name);
5555ded4
ACM
2190 int offset;
2191
efd2b924
ACM
2192 if (!field)
2193 return NULL;
5555ded4
ACM
2194
2195 offset = field->offset;
2196
2197 if (field->flags & FIELD_IS_DYNAMIC) {
2198 offset = *(int *)(sample->raw_data + field->offset);
2199 offset &= 0xffff;
2200 }
2201
2202 return sample->raw_data + offset;
2203}
2204
2205u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2206 const char *name)
2207{
efd2b924 2208 struct format_field *field = perf_evsel__field(evsel, name);
e6b6f679
ACM
2209 void *ptr;
2210 u64 value;
5555ded4 2211
efd2b924
ACM
2212 if (!field)
2213 return 0;
5555ded4 2214
e6b6f679 2215 ptr = sample->raw_data + field->offset;
5555ded4 2216
e6b6f679
ACM
2217 switch (field->size) {
2218 case 1:
2219 return *(u8 *)ptr;
2220 case 2:
2221 value = *(u16 *)ptr;
2222 break;
2223 case 4:
2224 value = *(u32 *)ptr;
2225 break;
2226 case 8:
e94eedab 2227 memcpy(&value, ptr, sizeof(u64));
e6b6f679
ACM
2228 break;
2229 default:
2230 return 0;
2231 }
2232
2233 if (!evsel->needs_swap)
2234 return value;
2235
2236 switch (field->size) {
2237 case 2:
2238 return bswap_16(value);
2239 case 4:
2240 return bswap_32(value);
2241 case 8:
2242 return bswap_64(value);
2243 default:
2244 return 0;
2245 }
2246
2247 return 0;
5555ded4 2248}
0698aedd
ACM
2249
2250static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2251{
2252 va_list args;
2253 int ret = 0;
2254
2255 if (!*first) {
2256 ret += fprintf(fp, ",");
2257 } else {
2258 ret += fprintf(fp, ":");
2259 *first = false;
2260 }
2261
2262 va_start(args, fmt);
2263 ret += vfprintf(fp, fmt, args);
2264 va_end(args);
2265 return ret;
2266}
2267
2c5e8c52 2268static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
c79a4393 2269{
2c5e8c52 2270 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
c79a4393
ACM
2271}
2272
0698aedd
ACM
2273int perf_evsel__fprintf(struct perf_evsel *evsel,
2274 struct perf_attr_details *details, FILE *fp)
2275{
2276 bool first = true;
e6ab07d0
NK
2277 int printed = 0;
2278
e35ef355 2279 if (details->event_group) {
e6ab07d0
NK
2280 struct perf_evsel *pos;
2281
2282 if (!perf_evsel__is_group_leader(evsel))
2283 return 0;
2284
2285 if (evsel->nr_members > 1)
2286 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2287
2288 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2289 for_each_group_member(pos, evsel)
2290 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2291
2292 if (evsel->nr_members > 1)
2293 printed += fprintf(fp, "}");
2294 goto out;
2295 }
2296
2297 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
0698aedd 2298
2c5e8c52
PZ
2299 if (details->verbose) {
2300 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2301 __print_attr__fprintf, &first);
2302 } else if (details->freq) {
4605bb55
NK
2303 const char *term = "sample_freq";
2304
2305 if (!evsel->attr.freq)
2306 term = "sample_period";
2307
2308 printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
2309 term, (u64)evsel->attr.sample_freq);
0698aedd 2310 }
775d8a1b
NK
2311
2312 if (details->trace_fields) {
2313 struct format_field *field;
2314
2315 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2316 printed += comma_fprintf(fp, &first, " (not a tracepoint)");
2317 goto out;
2318 }
2319
2320 field = evsel->tp_format->format.fields;
2321 if (field == NULL) {
2322 printed += comma_fprintf(fp, &first, " (no trace field)");
2323 goto out;
2324 }
2325
2326 printed += comma_fprintf(fp, &first, " trace_fields: %s", field->name);
2327
2328 field = field->next;
2329 while (field) {
2330 printed += comma_fprintf(fp, &first, "%s", field->name);
2331 field = field->next;
2332 }
2333 }
e6ab07d0 2334out:
0698aedd
ACM
2335 fputc('\n', fp);
2336 return ++printed;
2337}
c0a54341
ACM
2338
2339bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2340 char *msg, size_t msgsize)
2341{
2b821cce 2342 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
c0a54341
ACM
2343 evsel->attr.type == PERF_TYPE_HARDWARE &&
2344 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2345 /*
2346 * If it's cycles then fall back to hrtimer based
2347 * cpu-clock-tick sw counter, which is always available even if
2348 * no PMU support.
2349 *
2350 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2351 * b0a873e).
2352 */
2353 scnprintf(msg, msgsize, "%s",
2354"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2355
2356 evsel->attr.type = PERF_TYPE_SOFTWARE;
2357 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2358
04662523 2359 zfree(&evsel->name);
c0a54341
ACM
2360 return true;
2361 }
2362
2363 return false;
2364}
56e52e85 2365
602ad878 2366int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
56e52e85
ACM
2367 int err, char *msg, size_t size)
2368{
6e81c74c
MH
2369 char sbuf[STRERR_BUFSIZE];
2370
56e52e85
ACM
2371 switch (err) {
2372 case EPERM:
2373 case EACCES:
b69e63a4 2374 return scnprintf(msg, size,
3379e0c3
BH
2375 "You may not have permission to collect %sstats.\n\n"
2376 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2377 "which controls use of the performance events system by\n"
2378 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2379 "The default value is 1:\n\n"
2380 " -1: Allow use of (almost) all events by all users\n"
2381 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2382 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2383 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
56e52e85
ACM
2384 target->system_wide ? "system-wide " : "");
2385 case ENOENT:
2386 return scnprintf(msg, size, "The %s event is not supported.",
2387 perf_evsel__name(evsel));
2388 case EMFILE:
2389 return scnprintf(msg, size, "%s",
2390 "Too many events are opened.\n"
18ffdfe8
JO
2391 "Probably the maximum number of open file descriptors has been reached.\n"
2392 "Hint: Try again after reducing the number of events.\n"
2393 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
56e52e85
ACM
2394 case ENODEV:
2395 if (target->cpu_list)
2396 return scnprintf(msg, size, "%s",
2397 "No such device - did you specify an out-of-range profile CPU?\n");
2398 break;
2399 case EOPNOTSUPP:
2400 if (evsel->attr.precise_ip)
2401 return scnprintf(msg, size, "%s",
2402 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2403#if defined(__i386__) || defined(__x86_64__)
2404 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2405 return scnprintf(msg, size, "%s",
2406 "No hardware sampling interrupt available.\n"
2407 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2408#endif
2409 break;
63914aca
JO
2410 case EBUSY:
2411 if (find_process("oprofiled"))
2412 return scnprintf(msg, size,
2413 "The PMU counters are busy/taken by another profiler.\n"
2414 "We found oprofile daemon running, please stop it and try again.");
2415 break;
814c8c38
PZ
2416 case EINVAL:
2417 if (perf_missing_features.clockid)
2418 return scnprintf(msg, size, "clockid feature not supported.");
2419 if (perf_missing_features.clockid_wrong)
2420 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2421 break;
56e52e85
ACM
2422 default:
2423 break;
2424 }
2425
2426 return scnprintf(msg, size,
6e81c74c 2427 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
56e52e85
ACM
2428 "/bin/dmesg may provide additional information.\n"
2429 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
6e81c74c
MH
2430 err, strerror_r(err, sbuf, sizeof(sbuf)),
2431 perf_evsel__name(evsel));
56e52e85 2432}
This page took 0.303277 seconds and 5 git commands to generate.