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