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