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