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