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