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