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