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