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