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