xref: /openbmc/linux/tools/perf/util/evsel.c (revision 260ea95c)
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 <errno.h>
12 #include <inttypes.h>
13 #include <linux/bitops.h>
14 #include <api/fs/fs.h>
15 #include <api/fs/tracing_path.h>
16 #include <traceevent/event-parse.h>
17 #include <linux/hw_breakpoint.h>
18 #include <linux/perf_event.h>
19 #include <linux/compiler.h>
20 #include <linux/err.h>
21 #include <sys/ioctl.h>
22 #include <sys/resource.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include "asm/bug.h"
26 #include "callchain.h"
27 #include "cgroup.h"
28 #include "event.h"
29 #include "evsel.h"
30 #include "evlist.h"
31 #include "util.h"
32 #include "cpumap.h"
33 #include "thread_map.h"
34 #include "target.h"
35 #include "perf_regs.h"
36 #include "debug.h"
37 #include "trace-event.h"
38 #include "stat.h"
39 #include "util/parse-branch-options.h"
40 
41 #include "sane_ctype.h"
42 
43 static struct {
44 	bool sample_id_all;
45 	bool exclude_guest;
46 	bool mmap2;
47 	bool cloexec;
48 	bool clockid;
49 	bool clockid_wrong;
50 	bool lbr_flags;
51 	bool write_backward;
52 } perf_missing_features;
53 
54 static clockid_t clockid;
55 
56 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
57 {
58 	return 0;
59 }
60 
61 static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
62 {
63 }
64 
65 static struct {
66 	size_t	size;
67 	int	(*init)(struct perf_evsel *evsel);
68 	void	(*fini)(struct perf_evsel *evsel);
69 } perf_evsel__object = {
70 	.size = sizeof(struct perf_evsel),
71 	.init = perf_evsel__no_extra_init,
72 	.fini = perf_evsel__no_extra_fini,
73 };
74 
75 int perf_evsel__object_config(size_t object_size,
76 			      int (*init)(struct perf_evsel *evsel),
77 			      void (*fini)(struct perf_evsel *evsel))
78 {
79 
80 	if (object_size == 0)
81 		goto set_methods;
82 
83 	if (perf_evsel__object.size > object_size)
84 		return -EINVAL;
85 
86 	perf_evsel__object.size = object_size;
87 
88 set_methods:
89 	if (init != NULL)
90 		perf_evsel__object.init = init;
91 
92 	if (fini != NULL)
93 		perf_evsel__object.fini = fini;
94 
95 	return 0;
96 }
97 
98 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
99 
100 int __perf_evsel__sample_size(u64 sample_type)
101 {
102 	u64 mask = sample_type & PERF_SAMPLE_MASK;
103 	int size = 0;
104 	int i;
105 
106 	for (i = 0; i < 64; i++) {
107 		if (mask & (1ULL << i))
108 			size++;
109 	}
110 
111 	size *= sizeof(u64);
112 
113 	return size;
114 }
115 
116 /**
117  * __perf_evsel__calc_id_pos - calculate id_pos.
118  * @sample_type: sample type
119  *
120  * This function returns the position of the event id (PERF_SAMPLE_ID or
121  * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
122  * sample_event.
123  */
124 static int __perf_evsel__calc_id_pos(u64 sample_type)
125 {
126 	int idx = 0;
127 
128 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
129 		return 0;
130 
131 	if (!(sample_type & PERF_SAMPLE_ID))
132 		return -1;
133 
134 	if (sample_type & PERF_SAMPLE_IP)
135 		idx += 1;
136 
137 	if (sample_type & PERF_SAMPLE_TID)
138 		idx += 1;
139 
140 	if (sample_type & PERF_SAMPLE_TIME)
141 		idx += 1;
142 
143 	if (sample_type & PERF_SAMPLE_ADDR)
144 		idx += 1;
145 
146 	return idx;
147 }
148 
149 /**
150  * __perf_evsel__calc_is_pos - calculate is_pos.
151  * @sample_type: sample type
152  *
153  * This function returns the position (counting backwards) of the event id
154  * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
155  * sample_id_all is used there is an id sample appended to non-sample events.
156  */
157 static int __perf_evsel__calc_is_pos(u64 sample_type)
158 {
159 	int idx = 1;
160 
161 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
162 		return 1;
163 
164 	if (!(sample_type & PERF_SAMPLE_ID))
165 		return -1;
166 
167 	if (sample_type & PERF_SAMPLE_CPU)
168 		idx += 1;
169 
170 	if (sample_type & PERF_SAMPLE_STREAM_ID)
171 		idx += 1;
172 
173 	return idx;
174 }
175 
176 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
177 {
178 	evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
179 	evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
180 }
181 
182 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
183 				  enum perf_event_sample_format bit)
184 {
185 	if (!(evsel->attr.sample_type & bit)) {
186 		evsel->attr.sample_type |= bit;
187 		evsel->sample_size += sizeof(u64);
188 		perf_evsel__calc_id_pos(evsel);
189 	}
190 }
191 
192 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
193 				    enum perf_event_sample_format bit)
194 {
195 	if (evsel->attr.sample_type & bit) {
196 		evsel->attr.sample_type &= ~bit;
197 		evsel->sample_size -= sizeof(u64);
198 		perf_evsel__calc_id_pos(evsel);
199 	}
200 }
201 
202 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
203 			       bool can_sample_identifier)
204 {
205 	if (can_sample_identifier) {
206 		perf_evsel__reset_sample_bit(evsel, ID);
207 		perf_evsel__set_sample_bit(evsel, IDENTIFIER);
208 	} else {
209 		perf_evsel__set_sample_bit(evsel, ID);
210 	}
211 	evsel->attr.read_format |= PERF_FORMAT_ID;
212 }
213 
214 /**
215  * perf_evsel__is_function_event - Return whether given evsel is a function
216  * trace event
217  *
218  * @evsel - evsel selector to be tested
219  *
220  * Return %true if event is function trace event
221  */
222 bool perf_evsel__is_function_event(struct perf_evsel *evsel)
223 {
224 #define FUNCTION_EVENT "ftrace:function"
225 
226 	return evsel->name &&
227 	       !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
228 
229 #undef FUNCTION_EVENT
230 }
231 
232 void perf_evsel__init(struct perf_evsel *evsel,
233 		      struct perf_event_attr *attr, int idx)
234 {
235 	evsel->idx	   = idx;
236 	evsel->tracking	   = !idx;
237 	evsel->attr	   = *attr;
238 	evsel->leader	   = evsel;
239 	evsel->unit	   = "";
240 	evsel->scale	   = 1.0;
241 	evsel->evlist	   = NULL;
242 	evsel->bpf_fd	   = -1;
243 	INIT_LIST_HEAD(&evsel->node);
244 	INIT_LIST_HEAD(&evsel->config_terms);
245 	perf_evsel__object.init(evsel);
246 	evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
247 	perf_evsel__calc_id_pos(evsel);
248 	evsel->cmdline_group_boundary = false;
249 	evsel->metric_expr   = NULL;
250 	evsel->metric_name   = NULL;
251 	evsel->metric_events = NULL;
252 	evsel->collect_stat  = false;
253 }
254 
255 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
256 {
257 	struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
258 
259 	if (evsel != NULL)
260 		perf_evsel__init(evsel, attr, idx);
261 
262 	if (perf_evsel__is_bpf_output(evsel)) {
263 		evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
264 					    PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
265 		evsel->attr.sample_period = 1;
266 	}
267 
268 	return evsel;
269 }
270 
271 struct perf_evsel *perf_evsel__new_cycles(void)
272 {
273 	struct perf_event_attr attr = {
274 		.type	= PERF_TYPE_HARDWARE,
275 		.config	= PERF_COUNT_HW_CPU_CYCLES,
276 		.exclude_kernel	= 1,
277 	};
278 	struct perf_evsel *evsel;
279 
280 	event_attr_init(&attr);
281 	/*
282 	 * Unnamed union member, not supported as struct member named
283 	 * initializer in older compilers such as gcc 4.4.7
284 	 *
285 	 * Just for probing the precise_ip:
286 	 */
287 	attr.sample_period = 1;
288 
289 	perf_event_attr__set_max_precise_ip(&attr);
290 	/*
291 	 * Now let the usual logic to set up the perf_event_attr defaults
292 	 * to kick in when we return and before perf_evsel__open() is called.
293 	 */
294 	attr.sample_period = 0;
295 
296 	evsel = perf_evsel__new(&attr);
297 	if (evsel == NULL)
298 		goto out;
299 
300 	/* use asprintf() because free(evsel) assumes name is allocated */
301 	if (asprintf(&evsel->name, "cycles%.*s",
302 		     attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
303 		goto error_free;
304 out:
305 	return evsel;
306 error_free:
307 	perf_evsel__delete(evsel);
308 	evsel = NULL;
309 	goto out;
310 }
311 
312 /*
313  * Returns pointer with encoded error via <linux/err.h> interface.
314  */
315 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
316 {
317 	struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
318 	int err = -ENOMEM;
319 
320 	if (evsel == NULL) {
321 		goto out_err;
322 	} else {
323 		struct perf_event_attr attr = {
324 			.type	       = PERF_TYPE_TRACEPOINT,
325 			.sample_type   = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
326 					  PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
327 		};
328 
329 		if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
330 			goto out_free;
331 
332 		evsel->tp_format = trace_event__tp_format(sys, name);
333 		if (IS_ERR(evsel->tp_format)) {
334 			err = PTR_ERR(evsel->tp_format);
335 			goto out_free;
336 		}
337 
338 		event_attr_init(&attr);
339 		attr.config = evsel->tp_format->id;
340 		attr.sample_period = 1;
341 		perf_evsel__init(evsel, &attr, idx);
342 	}
343 
344 	return evsel;
345 
346 out_free:
347 	zfree(&evsel->name);
348 	free(evsel);
349 out_err:
350 	return ERR_PTR(err);
351 }
352 
353 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
354 	"cycles",
355 	"instructions",
356 	"cache-references",
357 	"cache-misses",
358 	"branches",
359 	"branch-misses",
360 	"bus-cycles",
361 	"stalled-cycles-frontend",
362 	"stalled-cycles-backend",
363 	"ref-cycles",
364 };
365 
366 static const char *__perf_evsel__hw_name(u64 config)
367 {
368 	if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
369 		return perf_evsel__hw_names[config];
370 
371 	return "unknown-hardware";
372 }
373 
374 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
375 {
376 	int colon = 0, r = 0;
377 	struct perf_event_attr *attr = &evsel->attr;
378 	bool exclude_guest_default = false;
379 
380 #define MOD_PRINT(context, mod)	do {					\
381 		if (!attr->exclude_##context) {				\
382 			if (!colon) colon = ++r;			\
383 			r += scnprintf(bf + r, size - r, "%c", mod);	\
384 		} } while(0)
385 
386 	if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
387 		MOD_PRINT(kernel, 'k');
388 		MOD_PRINT(user, 'u');
389 		MOD_PRINT(hv, 'h');
390 		exclude_guest_default = true;
391 	}
392 
393 	if (attr->precise_ip) {
394 		if (!colon)
395 			colon = ++r;
396 		r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
397 		exclude_guest_default = true;
398 	}
399 
400 	if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
401 		MOD_PRINT(host, 'H');
402 		MOD_PRINT(guest, 'G');
403 	}
404 #undef MOD_PRINT
405 	if (colon)
406 		bf[colon - 1] = ':';
407 	return r;
408 }
409 
410 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
411 {
412 	int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
413 	return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
414 }
415 
416 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
417 	"cpu-clock",
418 	"task-clock",
419 	"page-faults",
420 	"context-switches",
421 	"cpu-migrations",
422 	"minor-faults",
423 	"major-faults",
424 	"alignment-faults",
425 	"emulation-faults",
426 	"dummy",
427 };
428 
429 static const char *__perf_evsel__sw_name(u64 config)
430 {
431 	if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
432 		return perf_evsel__sw_names[config];
433 	return "unknown-software";
434 }
435 
436 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
437 {
438 	int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
439 	return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
440 }
441 
442 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
443 {
444 	int r;
445 
446 	r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
447 
448 	if (type & HW_BREAKPOINT_R)
449 		r += scnprintf(bf + r, size - r, "r");
450 
451 	if (type & HW_BREAKPOINT_W)
452 		r += scnprintf(bf + r, size - r, "w");
453 
454 	if (type & HW_BREAKPOINT_X)
455 		r += scnprintf(bf + r, size - r, "x");
456 
457 	return r;
458 }
459 
460 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
461 {
462 	struct perf_event_attr *attr = &evsel->attr;
463 	int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
464 	return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
465 }
466 
467 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
468 				[PERF_EVSEL__MAX_ALIASES] = {
469  { "L1-dcache",	"l1-d",		"l1d",		"L1-data",		},
470  { "L1-icache",	"l1-i",		"l1i",		"L1-instruction",	},
471  { "LLC",	"L2",							},
472  { "dTLB",	"d-tlb",	"Data-TLB",				},
473  { "iTLB",	"i-tlb",	"Instruction-TLB",			},
474  { "branch",	"branches",	"bpu",		"btb",		"bpc",	},
475  { "node",								},
476 };
477 
478 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
479 				   [PERF_EVSEL__MAX_ALIASES] = {
480  { "load",	"loads",	"read",					},
481  { "store",	"stores",	"write",				},
482  { "prefetch",	"prefetches",	"speculative-read", "speculative-load",	},
483 };
484 
485 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
486 				       [PERF_EVSEL__MAX_ALIASES] = {
487  { "refs",	"Reference",	"ops",		"access",		},
488  { "misses",	"miss",							},
489 };
490 
491 #define C(x)		PERF_COUNT_HW_CACHE_##x
492 #define CACHE_READ	(1 << C(OP_READ))
493 #define CACHE_WRITE	(1 << C(OP_WRITE))
494 #define CACHE_PREFETCH	(1 << C(OP_PREFETCH))
495 #define COP(x)		(1 << x)
496 
497 /*
498  * cache operartion stat
499  * L1I : Read and prefetch only
500  * ITLB and BPU : Read-only
501  */
502 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
503  [C(L1D)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
504  [C(L1I)]	= (CACHE_READ | CACHE_PREFETCH),
505  [C(LL)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
506  [C(DTLB)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
507  [C(ITLB)]	= (CACHE_READ),
508  [C(BPU)]	= (CACHE_READ),
509  [C(NODE)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
510 };
511 
512 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
513 {
514 	if (perf_evsel__hw_cache_stat[type] & COP(op))
515 		return true;	/* valid */
516 	else
517 		return false;	/* invalid */
518 }
519 
520 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
521 					    char *bf, size_t size)
522 {
523 	if (result) {
524 		return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
525 				 perf_evsel__hw_cache_op[op][0],
526 				 perf_evsel__hw_cache_result[result][0]);
527 	}
528 
529 	return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
530 			 perf_evsel__hw_cache_op[op][1]);
531 }
532 
533 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
534 {
535 	u8 op, result, type = (config >>  0) & 0xff;
536 	const char *err = "unknown-ext-hardware-cache-type";
537 
538 	if (type >= PERF_COUNT_HW_CACHE_MAX)
539 		goto out_err;
540 
541 	op = (config >>  8) & 0xff;
542 	err = "unknown-ext-hardware-cache-op";
543 	if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
544 		goto out_err;
545 
546 	result = (config >> 16) & 0xff;
547 	err = "unknown-ext-hardware-cache-result";
548 	if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
549 		goto out_err;
550 
551 	err = "invalid-cache";
552 	if (!perf_evsel__is_cache_op_valid(type, op))
553 		goto out_err;
554 
555 	return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
556 out_err:
557 	return scnprintf(bf, size, "%s", err);
558 }
559 
560 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
561 {
562 	int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
563 	return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
564 }
565 
566 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
567 {
568 	int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
569 	return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
570 }
571 
572 const char *perf_evsel__name(struct perf_evsel *evsel)
573 {
574 	char bf[128];
575 
576 	if (evsel->name)
577 		return evsel->name;
578 
579 	switch (evsel->attr.type) {
580 	case PERF_TYPE_RAW:
581 		perf_evsel__raw_name(evsel, bf, sizeof(bf));
582 		break;
583 
584 	case PERF_TYPE_HARDWARE:
585 		perf_evsel__hw_name(evsel, bf, sizeof(bf));
586 		break;
587 
588 	case PERF_TYPE_HW_CACHE:
589 		perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
590 		break;
591 
592 	case PERF_TYPE_SOFTWARE:
593 		perf_evsel__sw_name(evsel, bf, sizeof(bf));
594 		break;
595 
596 	case PERF_TYPE_TRACEPOINT:
597 		scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
598 		break;
599 
600 	case PERF_TYPE_BREAKPOINT:
601 		perf_evsel__bp_name(evsel, bf, sizeof(bf));
602 		break;
603 
604 	default:
605 		scnprintf(bf, sizeof(bf), "unknown attr type: %d",
606 			  evsel->attr.type);
607 		break;
608 	}
609 
610 	evsel->name = strdup(bf);
611 
612 	return evsel->name ?: "unknown";
613 }
614 
615 const char *perf_evsel__group_name(struct perf_evsel *evsel)
616 {
617 	return evsel->group_name ?: "anon group";
618 }
619 
620 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
621 {
622 	int ret;
623 	struct perf_evsel *pos;
624 	const char *group_name = perf_evsel__group_name(evsel);
625 
626 	ret = scnprintf(buf, size, "%s", group_name);
627 
628 	ret += scnprintf(buf + ret, size - ret, " { %s",
629 			 perf_evsel__name(evsel));
630 
631 	for_each_group_member(pos, evsel)
632 		ret += scnprintf(buf + ret, size - ret, ", %s",
633 				 perf_evsel__name(pos));
634 
635 	ret += scnprintf(buf + ret, size - ret, " }");
636 
637 	return ret;
638 }
639 
640 void perf_evsel__config_callchain(struct perf_evsel *evsel,
641 				  struct record_opts *opts,
642 				  struct callchain_param *param)
643 {
644 	bool function = perf_evsel__is_function_event(evsel);
645 	struct perf_event_attr *attr = &evsel->attr;
646 
647 	perf_evsel__set_sample_bit(evsel, CALLCHAIN);
648 
649 	attr->sample_max_stack = param->max_stack;
650 
651 	if (param->record_mode == CALLCHAIN_LBR) {
652 		if (!opts->branch_stack) {
653 			if (attr->exclude_user) {
654 				pr_warning("LBR callstack option is only available "
655 					   "to get user callchain information. "
656 					   "Falling back to framepointers.\n");
657 			} else {
658 				perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
659 				attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
660 							PERF_SAMPLE_BRANCH_CALL_STACK |
661 							PERF_SAMPLE_BRANCH_NO_CYCLES |
662 							PERF_SAMPLE_BRANCH_NO_FLAGS;
663 			}
664 		} else
665 			 pr_warning("Cannot use LBR callstack with branch stack. "
666 				    "Falling back to framepointers.\n");
667 	}
668 
669 	if (param->record_mode == CALLCHAIN_DWARF) {
670 		if (!function) {
671 			perf_evsel__set_sample_bit(evsel, REGS_USER);
672 			perf_evsel__set_sample_bit(evsel, STACK_USER);
673 			attr->sample_regs_user = PERF_REGS_MASK;
674 			attr->sample_stack_user = param->dump_size;
675 			attr->exclude_callchain_user = 1;
676 		} else {
677 			pr_info("Cannot use DWARF unwind for function trace event,"
678 				" falling back to framepointers.\n");
679 		}
680 	}
681 
682 	if (function) {
683 		pr_info("Disabling user space callchains for function trace event.\n");
684 		attr->exclude_callchain_user = 1;
685 	}
686 }
687 
688 static void
689 perf_evsel__reset_callgraph(struct perf_evsel *evsel,
690 			    struct callchain_param *param)
691 {
692 	struct perf_event_attr *attr = &evsel->attr;
693 
694 	perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
695 	if (param->record_mode == CALLCHAIN_LBR) {
696 		perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
697 		attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
698 					      PERF_SAMPLE_BRANCH_CALL_STACK);
699 	}
700 	if (param->record_mode == CALLCHAIN_DWARF) {
701 		perf_evsel__reset_sample_bit(evsel, REGS_USER);
702 		perf_evsel__reset_sample_bit(evsel, STACK_USER);
703 	}
704 }
705 
706 static void apply_config_terms(struct perf_evsel *evsel,
707 			       struct record_opts *opts)
708 {
709 	struct perf_evsel_config_term *term;
710 	struct list_head *config_terms = &evsel->config_terms;
711 	struct perf_event_attr *attr = &evsel->attr;
712 	struct callchain_param param;
713 	u32 dump_size = 0;
714 	int max_stack = 0;
715 	const char *callgraph_buf = NULL;
716 
717 	/* callgraph default */
718 	param.record_mode = callchain_param.record_mode;
719 
720 	list_for_each_entry(term, config_terms, list) {
721 		switch (term->type) {
722 		case PERF_EVSEL__CONFIG_TERM_PERIOD:
723 			attr->sample_period = term->val.period;
724 			attr->freq = 0;
725 			break;
726 		case PERF_EVSEL__CONFIG_TERM_FREQ:
727 			attr->sample_freq = term->val.freq;
728 			attr->freq = 1;
729 			break;
730 		case PERF_EVSEL__CONFIG_TERM_TIME:
731 			if (term->val.time)
732 				perf_evsel__set_sample_bit(evsel, TIME);
733 			else
734 				perf_evsel__reset_sample_bit(evsel, TIME);
735 			break;
736 		case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
737 			callgraph_buf = term->val.callgraph;
738 			break;
739 		case PERF_EVSEL__CONFIG_TERM_BRANCH:
740 			if (term->val.branch && strcmp(term->val.branch, "no")) {
741 				perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
742 				parse_branch_str(term->val.branch,
743 						 &attr->branch_sample_type);
744 			} else
745 				perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
746 			break;
747 		case PERF_EVSEL__CONFIG_TERM_STACK_USER:
748 			dump_size = term->val.stack_user;
749 			break;
750 		case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
751 			max_stack = term->val.max_stack;
752 			break;
753 		case PERF_EVSEL__CONFIG_TERM_INHERIT:
754 			/*
755 			 * attr->inherit should has already been set by
756 			 * perf_evsel__config. If user explicitly set
757 			 * inherit using config terms, override global
758 			 * opt->no_inherit setting.
759 			 */
760 			attr->inherit = term->val.inherit ? 1 : 0;
761 			break;
762 		case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
763 			attr->write_backward = term->val.overwrite ? 1 : 0;
764 			break;
765 		default:
766 			break;
767 		}
768 	}
769 
770 	/* User explicitly set per-event callgraph, clear the old setting and reset. */
771 	if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
772 		if (max_stack) {
773 			param.max_stack = max_stack;
774 			if (callgraph_buf == NULL)
775 				callgraph_buf = "fp";
776 		}
777 
778 		/* parse callgraph parameters */
779 		if (callgraph_buf != NULL) {
780 			if (!strcmp(callgraph_buf, "no")) {
781 				param.enabled = false;
782 				param.record_mode = CALLCHAIN_NONE;
783 			} else {
784 				param.enabled = true;
785 				if (parse_callchain_record(callgraph_buf, &param)) {
786 					pr_err("per-event callgraph setting for %s failed. "
787 					       "Apply callgraph global setting for it\n",
788 					       evsel->name);
789 					return;
790 				}
791 			}
792 		}
793 		if (dump_size > 0) {
794 			dump_size = round_up(dump_size, sizeof(u64));
795 			param.dump_size = dump_size;
796 		}
797 
798 		/* If global callgraph set, clear it */
799 		if (callchain_param.enabled)
800 			perf_evsel__reset_callgraph(evsel, &callchain_param);
801 
802 		/* set perf-event callgraph */
803 		if (param.enabled)
804 			perf_evsel__config_callchain(evsel, opts, &param);
805 	}
806 }
807 
808 /*
809  * The enable_on_exec/disabled value strategy:
810  *
811  *  1) For any type of traced program:
812  *    - all independent events and group leaders are disabled
813  *    - all group members are enabled
814  *
815  *     Group members are ruled by group leaders. They need to
816  *     be enabled, because the group scheduling relies on that.
817  *
818  *  2) For traced programs executed by perf:
819  *     - all independent events and group leaders have
820  *       enable_on_exec set
821  *     - we don't specifically enable or disable any event during
822  *       the record command
823  *
824  *     Independent events and group leaders are initially disabled
825  *     and get enabled by exec. Group members are ruled by group
826  *     leaders as stated in 1).
827  *
828  *  3) For traced programs attached by perf (pid/tid):
829  *     - we specifically enable or disable all events during
830  *       the record command
831  *
832  *     When attaching events to already running traced we
833  *     enable/disable events specifically, as there's no
834  *     initial traced exec call.
835  */
836 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
837 			struct callchain_param *callchain)
838 {
839 	struct perf_evsel *leader = evsel->leader;
840 	struct perf_event_attr *attr = &evsel->attr;
841 	int track = evsel->tracking;
842 	bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
843 
844 	attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
845 	attr->inherit	    = !opts->no_inherit;
846 	attr->write_backward = opts->overwrite ? 1 : 0;
847 
848 	perf_evsel__set_sample_bit(evsel, IP);
849 	perf_evsel__set_sample_bit(evsel, TID);
850 
851 	if (evsel->sample_read) {
852 		perf_evsel__set_sample_bit(evsel, READ);
853 
854 		/*
855 		 * We need ID even in case of single event, because
856 		 * PERF_SAMPLE_READ process ID specific data.
857 		 */
858 		perf_evsel__set_sample_id(evsel, false);
859 
860 		/*
861 		 * Apply group format only if we belong to group
862 		 * with more than one members.
863 		 */
864 		if (leader->nr_members > 1) {
865 			attr->read_format |= PERF_FORMAT_GROUP;
866 			attr->inherit = 0;
867 		}
868 	}
869 
870 	/*
871 	 * We default some events to have a default interval. But keep
872 	 * it a weak assumption overridable by the user.
873 	 */
874 	if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
875 				     opts->user_interval != ULLONG_MAX)) {
876 		if (opts->freq) {
877 			perf_evsel__set_sample_bit(evsel, PERIOD);
878 			attr->freq		= 1;
879 			attr->sample_freq	= opts->freq;
880 		} else {
881 			attr->sample_period = opts->default_interval;
882 		}
883 	}
884 
885 	/*
886 	 * Disable sampling for all group members other
887 	 * than leader in case leader 'leads' the sampling.
888 	 */
889 	if ((leader != evsel) && leader->sample_read) {
890 		attr->sample_freq   = 0;
891 		attr->sample_period = 0;
892 	}
893 
894 	if (opts->no_samples)
895 		attr->sample_freq = 0;
896 
897 	if (opts->inherit_stat)
898 		attr->inherit_stat = 1;
899 
900 	if (opts->sample_address) {
901 		perf_evsel__set_sample_bit(evsel, ADDR);
902 		attr->mmap_data = track;
903 	}
904 
905 	/*
906 	 * We don't allow user space callchains for  function trace
907 	 * event, due to issues with page faults while tracing page
908 	 * fault handler and its overall trickiness nature.
909 	 */
910 	if (perf_evsel__is_function_event(evsel))
911 		evsel->attr.exclude_callchain_user = 1;
912 
913 	if (callchain && callchain->enabled && !evsel->no_aux_samples)
914 		perf_evsel__config_callchain(evsel, opts, callchain);
915 
916 	if (opts->sample_intr_regs) {
917 		attr->sample_regs_intr = opts->sample_intr_regs;
918 		perf_evsel__set_sample_bit(evsel, REGS_INTR);
919 	}
920 
921 	if (target__has_cpu(&opts->target) || opts->sample_cpu)
922 		perf_evsel__set_sample_bit(evsel, CPU);
923 
924 	if (opts->period)
925 		perf_evsel__set_sample_bit(evsel, PERIOD);
926 
927 	/*
928 	 * When the user explicitly disabled time don't force it here.
929 	 */
930 	if (opts->sample_time &&
931 	    (!perf_missing_features.sample_id_all &&
932 	    (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
933 	     opts->sample_time_set)))
934 		perf_evsel__set_sample_bit(evsel, TIME);
935 
936 	if (opts->raw_samples && !evsel->no_aux_samples) {
937 		perf_evsel__set_sample_bit(evsel, TIME);
938 		perf_evsel__set_sample_bit(evsel, RAW);
939 		perf_evsel__set_sample_bit(evsel, CPU);
940 	}
941 
942 	if (opts->sample_address)
943 		perf_evsel__set_sample_bit(evsel, DATA_SRC);
944 
945 	if (opts->no_buffering) {
946 		attr->watermark = 0;
947 		attr->wakeup_events = 1;
948 	}
949 	if (opts->branch_stack && !evsel->no_aux_samples) {
950 		perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
951 		attr->branch_sample_type = opts->branch_stack;
952 	}
953 
954 	if (opts->sample_weight)
955 		perf_evsel__set_sample_bit(evsel, WEIGHT);
956 
957 	attr->task  = track;
958 	attr->mmap  = track;
959 	attr->mmap2 = track && !perf_missing_features.mmap2;
960 	attr->comm  = track;
961 
962 	if (opts->record_namespaces)
963 		attr->namespaces  = track;
964 
965 	if (opts->record_switch_events)
966 		attr->context_switch = track;
967 
968 	if (opts->sample_transaction)
969 		perf_evsel__set_sample_bit(evsel, TRANSACTION);
970 
971 	if (opts->running_time) {
972 		evsel->attr.read_format |=
973 			PERF_FORMAT_TOTAL_TIME_ENABLED |
974 			PERF_FORMAT_TOTAL_TIME_RUNNING;
975 	}
976 
977 	/*
978 	 * XXX see the function comment above
979 	 *
980 	 * Disabling only independent events or group leaders,
981 	 * keeping group members enabled.
982 	 */
983 	if (perf_evsel__is_group_leader(evsel))
984 		attr->disabled = 1;
985 
986 	/*
987 	 * Setting enable_on_exec for independent events and
988 	 * group leaders for traced executed by perf.
989 	 */
990 	if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
991 		!opts->initial_delay)
992 		attr->enable_on_exec = 1;
993 
994 	if (evsel->immediate) {
995 		attr->disabled = 0;
996 		attr->enable_on_exec = 0;
997 	}
998 
999 	clockid = opts->clockid;
1000 	if (opts->use_clockid) {
1001 		attr->use_clockid = 1;
1002 		attr->clockid = opts->clockid;
1003 	}
1004 
1005 	if (evsel->precise_max)
1006 		perf_event_attr__set_max_precise_ip(attr);
1007 
1008 	if (opts->all_user) {
1009 		attr->exclude_kernel = 1;
1010 		attr->exclude_user   = 0;
1011 	}
1012 
1013 	if (opts->all_kernel) {
1014 		attr->exclude_kernel = 0;
1015 		attr->exclude_user   = 1;
1016 	}
1017 
1018 	/*
1019 	 * Apply event specific term settings,
1020 	 * it overloads any global configuration.
1021 	 */
1022 	apply_config_terms(evsel, opts);
1023 
1024 	evsel->ignore_missing_thread = opts->ignore_missing_thread;
1025 }
1026 
1027 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1028 {
1029 	if (evsel->system_wide)
1030 		nthreads = 1;
1031 
1032 	evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
1033 
1034 	if (evsel->fd) {
1035 		int cpu, thread;
1036 		for (cpu = 0; cpu < ncpus; cpu++) {
1037 			for (thread = 0; thread < nthreads; thread++) {
1038 				FD(evsel, cpu, thread) = -1;
1039 			}
1040 		}
1041 	}
1042 
1043 	return evsel->fd != NULL ? 0 : -ENOMEM;
1044 }
1045 
1046 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
1047 			  int ioc,  void *arg)
1048 {
1049 	int cpu, thread;
1050 
1051 	if (evsel->system_wide)
1052 		nthreads = 1;
1053 
1054 	for (cpu = 0; cpu < ncpus; cpu++) {
1055 		for (thread = 0; thread < nthreads; thread++) {
1056 			int fd = FD(evsel, cpu, thread),
1057 			    err = ioctl(fd, ioc, arg);
1058 
1059 			if (err)
1060 				return err;
1061 		}
1062 	}
1063 
1064 	return 0;
1065 }
1066 
1067 int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
1068 			     const char *filter)
1069 {
1070 	return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1071 				     PERF_EVENT_IOC_SET_FILTER,
1072 				     (void *)filter);
1073 }
1074 
1075 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
1076 {
1077 	char *new_filter = strdup(filter);
1078 
1079 	if (new_filter != NULL) {
1080 		free(evsel->filter);
1081 		evsel->filter = new_filter;
1082 		return 0;
1083 	}
1084 
1085 	return -1;
1086 }
1087 
1088 static int perf_evsel__append_filter(struct perf_evsel *evsel,
1089 				     const char *fmt, const char *filter)
1090 {
1091 	char *new_filter;
1092 
1093 	if (evsel->filter == NULL)
1094 		return perf_evsel__set_filter(evsel, filter);
1095 
1096 	if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
1097 		free(evsel->filter);
1098 		evsel->filter = new_filter;
1099 		return 0;
1100 	}
1101 
1102 	return -1;
1103 }
1104 
1105 int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter)
1106 {
1107 	return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
1108 }
1109 
1110 int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter)
1111 {
1112 	return perf_evsel__append_filter(evsel, "%s,%s", filter);
1113 }
1114 
1115 int perf_evsel__enable(struct perf_evsel *evsel)
1116 {
1117 	int nthreads = thread_map__nr(evsel->threads);
1118 	int ncpus = cpu_map__nr(evsel->cpus);
1119 
1120 	return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1121 				     PERF_EVENT_IOC_ENABLE,
1122 				     0);
1123 }
1124 
1125 int perf_evsel__disable(struct perf_evsel *evsel)
1126 {
1127 	int nthreads = thread_map__nr(evsel->threads);
1128 	int ncpus = cpu_map__nr(evsel->cpus);
1129 
1130 	return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1131 				     PERF_EVENT_IOC_DISABLE,
1132 				     0);
1133 }
1134 
1135 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1136 {
1137 	if (ncpus == 0 || nthreads == 0)
1138 		return 0;
1139 
1140 	if (evsel->system_wide)
1141 		nthreads = 1;
1142 
1143 	evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1144 	if (evsel->sample_id == NULL)
1145 		return -ENOMEM;
1146 
1147 	evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1148 	if (evsel->id == NULL) {
1149 		xyarray__delete(evsel->sample_id);
1150 		evsel->sample_id = NULL;
1151 		return -ENOMEM;
1152 	}
1153 
1154 	return 0;
1155 }
1156 
1157 static void perf_evsel__free_fd(struct perf_evsel *evsel)
1158 {
1159 	xyarray__delete(evsel->fd);
1160 	evsel->fd = NULL;
1161 }
1162 
1163 static void perf_evsel__free_id(struct perf_evsel *evsel)
1164 {
1165 	xyarray__delete(evsel->sample_id);
1166 	evsel->sample_id = NULL;
1167 	zfree(&evsel->id);
1168 }
1169 
1170 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1171 {
1172 	struct perf_evsel_config_term *term, *h;
1173 
1174 	list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1175 		list_del(&term->list);
1176 		free(term);
1177 	}
1178 }
1179 
1180 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1181 {
1182 	int cpu, thread;
1183 
1184 	if (evsel->system_wide)
1185 		nthreads = 1;
1186 
1187 	for (cpu = 0; cpu < ncpus; cpu++)
1188 		for (thread = 0; thread < nthreads; ++thread) {
1189 			close(FD(evsel, cpu, thread));
1190 			FD(evsel, cpu, thread) = -1;
1191 		}
1192 }
1193 
1194 void perf_evsel__exit(struct perf_evsel *evsel)
1195 {
1196 	assert(list_empty(&evsel->node));
1197 	assert(evsel->evlist == NULL);
1198 	perf_evsel__free_fd(evsel);
1199 	perf_evsel__free_id(evsel);
1200 	perf_evsel__free_config_terms(evsel);
1201 	close_cgroup(evsel->cgrp);
1202 	cpu_map__put(evsel->cpus);
1203 	cpu_map__put(evsel->own_cpus);
1204 	thread_map__put(evsel->threads);
1205 	zfree(&evsel->group_name);
1206 	zfree(&evsel->name);
1207 	perf_evsel__object.fini(evsel);
1208 }
1209 
1210 void perf_evsel__delete(struct perf_evsel *evsel)
1211 {
1212 	perf_evsel__exit(evsel);
1213 	free(evsel);
1214 }
1215 
1216 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
1217 				struct perf_counts_values *count)
1218 {
1219 	struct perf_counts_values tmp;
1220 
1221 	if (!evsel->prev_raw_counts)
1222 		return;
1223 
1224 	if (cpu == -1) {
1225 		tmp = evsel->prev_raw_counts->aggr;
1226 		evsel->prev_raw_counts->aggr = *count;
1227 	} else {
1228 		tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1229 		*perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1230 	}
1231 
1232 	count->val = count->val - tmp.val;
1233 	count->ena = count->ena - tmp.ena;
1234 	count->run = count->run - tmp.run;
1235 }
1236 
1237 void perf_counts_values__scale(struct perf_counts_values *count,
1238 			       bool scale, s8 *pscaled)
1239 {
1240 	s8 scaled = 0;
1241 
1242 	if (scale) {
1243 		if (count->run == 0) {
1244 			scaled = -1;
1245 			count->val = 0;
1246 		} else if (count->run < count->ena) {
1247 			scaled = 1;
1248 			count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1249 		}
1250 	} else
1251 		count->ena = count->run = 0;
1252 
1253 	if (pscaled)
1254 		*pscaled = scaled;
1255 }
1256 
1257 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1258 		     struct perf_counts_values *count)
1259 {
1260 	memset(count, 0, sizeof(*count));
1261 
1262 	if (FD(evsel, cpu, thread) < 0)
1263 		return -EINVAL;
1264 
1265 	if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) <= 0)
1266 		return -errno;
1267 
1268 	return 0;
1269 }
1270 
1271 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1272 			      int cpu, int thread, bool scale)
1273 {
1274 	struct perf_counts_values count;
1275 	size_t nv = scale ? 3 : 1;
1276 
1277 	if (FD(evsel, cpu, thread) < 0)
1278 		return -EINVAL;
1279 
1280 	if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1281 		return -ENOMEM;
1282 
1283 	if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
1284 		return -errno;
1285 
1286 	perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1287 	perf_counts_values__scale(&count, scale, NULL);
1288 	*perf_counts(evsel->counts, cpu, thread) = count;
1289 	return 0;
1290 }
1291 
1292 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1293 {
1294 	struct perf_evsel *leader = evsel->leader;
1295 	int fd;
1296 
1297 	if (perf_evsel__is_group_leader(evsel))
1298 		return -1;
1299 
1300 	/*
1301 	 * Leader must be already processed/open,
1302 	 * if not it's a bug.
1303 	 */
1304 	BUG_ON(!leader->fd);
1305 
1306 	fd = FD(leader, cpu, thread);
1307 	BUG_ON(fd == -1);
1308 
1309 	return fd;
1310 }
1311 
1312 struct bit_names {
1313 	int bit;
1314 	const char *name;
1315 };
1316 
1317 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1318 {
1319 	bool first_bit = true;
1320 	int i = 0;
1321 
1322 	do {
1323 		if (value & bits[i].bit) {
1324 			buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1325 			first_bit = false;
1326 		}
1327 	} while (bits[++i].name != NULL);
1328 }
1329 
1330 static void __p_sample_type(char *buf, size_t size, u64 value)
1331 {
1332 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1333 	struct bit_names bits[] = {
1334 		bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1335 		bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1336 		bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1337 		bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1338 		bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1339 		bit_name(WEIGHT),
1340 		{ .name = NULL, }
1341 	};
1342 #undef bit_name
1343 	__p_bits(buf, size, value, bits);
1344 }
1345 
1346 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1347 {
1348 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1349 	struct bit_names bits[] = {
1350 		bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1351 		bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1352 		bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1353 		bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1354 		bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1355 		{ .name = NULL, }
1356 	};
1357 #undef bit_name
1358 	__p_bits(buf, size, value, bits);
1359 }
1360 
1361 static void __p_read_format(char *buf, size_t size, u64 value)
1362 {
1363 #define bit_name(n) { PERF_FORMAT_##n, #n }
1364 	struct bit_names bits[] = {
1365 		bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1366 		bit_name(ID), bit_name(GROUP),
1367 		{ .name = NULL, }
1368 	};
1369 #undef bit_name
1370 	__p_bits(buf, size, value, bits);
1371 }
1372 
1373 #define BUF_SIZE		1024
1374 
1375 #define p_hex(val)		snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1376 #define p_unsigned(val)		snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1377 #define p_signed(val)		snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1378 #define p_sample_type(val)	__p_sample_type(buf, BUF_SIZE, val)
1379 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1380 #define p_read_format(val)	__p_read_format(buf, BUF_SIZE, val)
1381 
1382 #define PRINT_ATTRn(_n, _f, _p)				\
1383 do {							\
1384 	if (attr->_f) {					\
1385 		_p(attr->_f);				\
1386 		ret += attr__fprintf(fp, _n, buf, priv);\
1387 	}						\
1388 } while (0)
1389 
1390 #define PRINT_ATTRf(_f, _p)	PRINT_ATTRn(#_f, _f, _p)
1391 
1392 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1393 			     attr__fprintf_f attr__fprintf, void *priv)
1394 {
1395 	char buf[BUF_SIZE];
1396 	int ret = 0;
1397 
1398 	PRINT_ATTRf(type, p_unsigned);
1399 	PRINT_ATTRf(size, p_unsigned);
1400 	PRINT_ATTRf(config, p_hex);
1401 	PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1402 	PRINT_ATTRf(sample_type, p_sample_type);
1403 	PRINT_ATTRf(read_format, p_read_format);
1404 
1405 	PRINT_ATTRf(disabled, p_unsigned);
1406 	PRINT_ATTRf(inherit, p_unsigned);
1407 	PRINT_ATTRf(pinned, p_unsigned);
1408 	PRINT_ATTRf(exclusive, p_unsigned);
1409 	PRINT_ATTRf(exclude_user, p_unsigned);
1410 	PRINT_ATTRf(exclude_kernel, p_unsigned);
1411 	PRINT_ATTRf(exclude_hv, p_unsigned);
1412 	PRINT_ATTRf(exclude_idle, p_unsigned);
1413 	PRINT_ATTRf(mmap, p_unsigned);
1414 	PRINT_ATTRf(comm, p_unsigned);
1415 	PRINT_ATTRf(freq, p_unsigned);
1416 	PRINT_ATTRf(inherit_stat, p_unsigned);
1417 	PRINT_ATTRf(enable_on_exec, p_unsigned);
1418 	PRINT_ATTRf(task, p_unsigned);
1419 	PRINT_ATTRf(watermark, p_unsigned);
1420 	PRINT_ATTRf(precise_ip, p_unsigned);
1421 	PRINT_ATTRf(mmap_data, p_unsigned);
1422 	PRINT_ATTRf(sample_id_all, p_unsigned);
1423 	PRINT_ATTRf(exclude_host, p_unsigned);
1424 	PRINT_ATTRf(exclude_guest, p_unsigned);
1425 	PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1426 	PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1427 	PRINT_ATTRf(mmap2, p_unsigned);
1428 	PRINT_ATTRf(comm_exec, p_unsigned);
1429 	PRINT_ATTRf(use_clockid, p_unsigned);
1430 	PRINT_ATTRf(context_switch, p_unsigned);
1431 	PRINT_ATTRf(write_backward, p_unsigned);
1432 
1433 	PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1434 	PRINT_ATTRf(bp_type, p_unsigned);
1435 	PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1436 	PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1437 	PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
1438 	PRINT_ATTRf(sample_regs_user, p_hex);
1439 	PRINT_ATTRf(sample_stack_user, p_unsigned);
1440 	PRINT_ATTRf(clockid, p_signed);
1441 	PRINT_ATTRf(sample_regs_intr, p_hex);
1442 	PRINT_ATTRf(aux_watermark, p_unsigned);
1443 	PRINT_ATTRf(sample_max_stack, p_unsigned);
1444 
1445 	return ret;
1446 }
1447 
1448 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1449 				void *priv __maybe_unused)
1450 {
1451 	return fprintf(fp, "  %-32s %s\n", name, val);
1452 }
1453 
1454 static bool ignore_missing_thread(struct perf_evsel *evsel,
1455 				  struct thread_map *threads,
1456 				  int thread, int err)
1457 {
1458 	if (!evsel->ignore_missing_thread)
1459 		return false;
1460 
1461 	/* The system wide setup does not work with threads. */
1462 	if (evsel->system_wide)
1463 		return false;
1464 
1465 	/* The -ESRCH is perf event syscall errno for pid's not found. */
1466 	if (err != -ESRCH)
1467 		return false;
1468 
1469 	/* If there's only one thread, let it fail. */
1470 	if (threads->nr == 1)
1471 		return false;
1472 
1473 	if (thread_map__remove(threads, thread))
1474 		return false;
1475 
1476 	pr_warning("WARNING: Ignored open failure for pid %d\n",
1477 		   thread_map__pid(threads, thread));
1478 	return true;
1479 }
1480 
1481 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1482 		     struct thread_map *threads)
1483 {
1484 	int cpu, thread, nthreads;
1485 	unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1486 	int pid = -1, err;
1487 	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1488 
1489 	if (perf_missing_features.write_backward && evsel->attr.write_backward)
1490 		return -EINVAL;
1491 
1492 	if (cpus == NULL) {
1493 		static struct cpu_map *empty_cpu_map;
1494 
1495 		if (empty_cpu_map == NULL) {
1496 			empty_cpu_map = cpu_map__dummy_new();
1497 			if (empty_cpu_map == NULL)
1498 				return -ENOMEM;
1499 		}
1500 
1501 		cpus = empty_cpu_map;
1502 	}
1503 
1504 	if (threads == NULL) {
1505 		static struct thread_map *empty_thread_map;
1506 
1507 		if (empty_thread_map == NULL) {
1508 			empty_thread_map = thread_map__new_by_tid(-1);
1509 			if (empty_thread_map == NULL)
1510 				return -ENOMEM;
1511 		}
1512 
1513 		threads = empty_thread_map;
1514 	}
1515 
1516 	if (evsel->system_wide)
1517 		nthreads = 1;
1518 	else
1519 		nthreads = threads->nr;
1520 
1521 	if (evsel->fd == NULL &&
1522 	    perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1523 		return -ENOMEM;
1524 
1525 	if (evsel->cgrp) {
1526 		flags |= PERF_FLAG_PID_CGROUP;
1527 		pid = evsel->cgrp->fd;
1528 	}
1529 
1530 fallback_missing_features:
1531 	if (perf_missing_features.clockid_wrong)
1532 		evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1533 	if (perf_missing_features.clockid) {
1534 		evsel->attr.use_clockid = 0;
1535 		evsel->attr.clockid = 0;
1536 	}
1537 	if (perf_missing_features.cloexec)
1538 		flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1539 	if (perf_missing_features.mmap2)
1540 		evsel->attr.mmap2 = 0;
1541 	if (perf_missing_features.exclude_guest)
1542 		evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1543 	if (perf_missing_features.lbr_flags)
1544 		evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1545 				     PERF_SAMPLE_BRANCH_NO_CYCLES);
1546 retry_sample_id:
1547 	if (perf_missing_features.sample_id_all)
1548 		evsel->attr.sample_id_all = 0;
1549 
1550 	if (verbose >= 2) {
1551 		fprintf(stderr, "%.60s\n", graph_dotted_line);
1552 		fprintf(stderr, "perf_event_attr:\n");
1553 		perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1554 		fprintf(stderr, "%.60s\n", graph_dotted_line);
1555 	}
1556 
1557 	for (cpu = 0; cpu < cpus->nr; cpu++) {
1558 
1559 		for (thread = 0; thread < nthreads; thread++) {
1560 			int fd, group_fd;
1561 
1562 			if (!evsel->cgrp && !evsel->system_wide)
1563 				pid = thread_map__pid(threads, thread);
1564 
1565 			group_fd = get_group_fd(evsel, cpu, thread);
1566 retry_open:
1567 			pr_debug2("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
1568 				  pid, cpus->map[cpu], group_fd, flags);
1569 
1570 			fd = sys_perf_event_open(&evsel->attr, pid, cpus->map[cpu],
1571 						 group_fd, flags);
1572 
1573 			FD(evsel, cpu, thread) = fd;
1574 
1575 			if (fd < 0) {
1576 				err = -errno;
1577 
1578 				if (ignore_missing_thread(evsel, threads, thread, err)) {
1579 					/*
1580 					 * We just removed 1 thread, so take a step
1581 					 * back on thread index and lower the upper
1582 					 * nthreads limit.
1583 					 */
1584 					nthreads--;
1585 					thread--;
1586 
1587 					/* ... and pretend like nothing have happened. */
1588 					err = 0;
1589 					continue;
1590 				}
1591 
1592 				pr_debug2("\nsys_perf_event_open failed, error %d\n",
1593 					  err);
1594 				goto try_fallback;
1595 			}
1596 
1597 			pr_debug2(" = %d\n", fd);
1598 
1599 			if (evsel->bpf_fd >= 0) {
1600 				int evt_fd = fd;
1601 				int bpf_fd = evsel->bpf_fd;
1602 
1603 				err = ioctl(evt_fd,
1604 					    PERF_EVENT_IOC_SET_BPF,
1605 					    bpf_fd);
1606 				if (err && errno != EEXIST) {
1607 					pr_err("failed to attach bpf fd %d: %s\n",
1608 					       bpf_fd, strerror(errno));
1609 					err = -EINVAL;
1610 					goto out_close;
1611 				}
1612 			}
1613 
1614 			set_rlimit = NO_CHANGE;
1615 
1616 			/*
1617 			 * If we succeeded but had to kill clockid, fail and
1618 			 * have perf_evsel__open_strerror() print us a nice
1619 			 * error.
1620 			 */
1621 			if (perf_missing_features.clockid ||
1622 			    perf_missing_features.clockid_wrong) {
1623 				err = -EINVAL;
1624 				goto out_close;
1625 			}
1626 		}
1627 	}
1628 
1629 	return 0;
1630 
1631 try_fallback:
1632 	/*
1633 	 * perf stat needs between 5 and 22 fds per CPU. When we run out
1634 	 * of them try to increase the limits.
1635 	 */
1636 	if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1637 		struct rlimit l;
1638 		int old_errno = errno;
1639 
1640 		if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1641 			if (set_rlimit == NO_CHANGE)
1642 				l.rlim_cur = l.rlim_max;
1643 			else {
1644 				l.rlim_cur = l.rlim_max + 1000;
1645 				l.rlim_max = l.rlim_cur;
1646 			}
1647 			if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1648 				set_rlimit++;
1649 				errno = old_errno;
1650 				goto retry_open;
1651 			}
1652 		}
1653 		errno = old_errno;
1654 	}
1655 
1656 	if (err != -EINVAL || cpu > 0 || thread > 0)
1657 		goto out_close;
1658 
1659 	/*
1660 	 * Must probe features in the order they were added to the
1661 	 * perf_event_attr interface.
1662 	 */
1663 	if (!perf_missing_features.write_backward && evsel->attr.write_backward) {
1664 		perf_missing_features.write_backward = true;
1665 		goto out_close;
1666 	} else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1667 		perf_missing_features.clockid_wrong = true;
1668 		goto fallback_missing_features;
1669 	} else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1670 		perf_missing_features.clockid = true;
1671 		goto fallback_missing_features;
1672 	} else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1673 		perf_missing_features.cloexec = true;
1674 		goto fallback_missing_features;
1675 	} else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1676 		perf_missing_features.mmap2 = true;
1677 		goto fallback_missing_features;
1678 	} else if (!perf_missing_features.exclude_guest &&
1679 		   (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1680 		perf_missing_features.exclude_guest = true;
1681 		goto fallback_missing_features;
1682 	} else if (!perf_missing_features.sample_id_all) {
1683 		perf_missing_features.sample_id_all = true;
1684 		goto retry_sample_id;
1685 	} else if (!perf_missing_features.lbr_flags &&
1686 			(evsel->attr.branch_sample_type &
1687 			 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1688 			  PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1689 		perf_missing_features.lbr_flags = true;
1690 		goto fallback_missing_features;
1691 	}
1692 out_close:
1693 	do {
1694 		while (--thread >= 0) {
1695 			close(FD(evsel, cpu, thread));
1696 			FD(evsel, cpu, thread) = -1;
1697 		}
1698 		thread = nthreads;
1699 	} while (--cpu >= 0);
1700 	return err;
1701 }
1702 
1703 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1704 {
1705 	if (evsel->fd == NULL)
1706 		return;
1707 
1708 	perf_evsel__close_fd(evsel, ncpus, nthreads);
1709 	perf_evsel__free_fd(evsel);
1710 }
1711 
1712 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1713 			     struct cpu_map *cpus)
1714 {
1715 	return perf_evsel__open(evsel, cpus, NULL);
1716 }
1717 
1718 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1719 				struct thread_map *threads)
1720 {
1721 	return perf_evsel__open(evsel, NULL, threads);
1722 }
1723 
1724 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1725 				       const union perf_event *event,
1726 				       struct perf_sample *sample)
1727 {
1728 	u64 type = evsel->attr.sample_type;
1729 	const u64 *array = event->sample.array;
1730 	bool swapped = evsel->needs_swap;
1731 	union u64_swap u;
1732 
1733 	array += ((event->header.size -
1734 		   sizeof(event->header)) / sizeof(u64)) - 1;
1735 
1736 	if (type & PERF_SAMPLE_IDENTIFIER) {
1737 		sample->id = *array;
1738 		array--;
1739 	}
1740 
1741 	if (type & PERF_SAMPLE_CPU) {
1742 		u.val64 = *array;
1743 		if (swapped) {
1744 			/* undo swap of u64, then swap on individual u32s */
1745 			u.val64 = bswap_64(u.val64);
1746 			u.val32[0] = bswap_32(u.val32[0]);
1747 		}
1748 
1749 		sample->cpu = u.val32[0];
1750 		array--;
1751 	}
1752 
1753 	if (type & PERF_SAMPLE_STREAM_ID) {
1754 		sample->stream_id = *array;
1755 		array--;
1756 	}
1757 
1758 	if (type & PERF_SAMPLE_ID) {
1759 		sample->id = *array;
1760 		array--;
1761 	}
1762 
1763 	if (type & PERF_SAMPLE_TIME) {
1764 		sample->time = *array;
1765 		array--;
1766 	}
1767 
1768 	if (type & PERF_SAMPLE_TID) {
1769 		u.val64 = *array;
1770 		if (swapped) {
1771 			/* undo swap of u64, then swap on individual u32s */
1772 			u.val64 = bswap_64(u.val64);
1773 			u.val32[0] = bswap_32(u.val32[0]);
1774 			u.val32[1] = bswap_32(u.val32[1]);
1775 		}
1776 
1777 		sample->pid = u.val32[0];
1778 		sample->tid = u.val32[1];
1779 		array--;
1780 	}
1781 
1782 	return 0;
1783 }
1784 
1785 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1786 			    u64 size)
1787 {
1788 	return size > max_size || offset + size > endp;
1789 }
1790 
1791 #define OVERFLOW_CHECK(offset, size, max_size)				\
1792 	do {								\
1793 		if (overflow(endp, (max_size), (offset), (size)))	\
1794 			return -EFAULT;					\
1795 	} while (0)
1796 
1797 #define OVERFLOW_CHECK_u64(offset) \
1798 	OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1799 
1800 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1801 			     struct perf_sample *data)
1802 {
1803 	u64 type = evsel->attr.sample_type;
1804 	bool swapped = evsel->needs_swap;
1805 	const u64 *array;
1806 	u16 max_size = event->header.size;
1807 	const void *endp = (void *)event + max_size;
1808 	u64 sz;
1809 
1810 	/*
1811 	 * used for cross-endian analysis. See git commit 65014ab3
1812 	 * for why this goofiness is needed.
1813 	 */
1814 	union u64_swap u;
1815 
1816 	memset(data, 0, sizeof(*data));
1817 	data->cpu = data->pid = data->tid = -1;
1818 	data->stream_id = data->id = data->time = -1ULL;
1819 	data->period = evsel->attr.sample_period;
1820 	data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1821 
1822 	if (event->header.type != PERF_RECORD_SAMPLE) {
1823 		if (!evsel->attr.sample_id_all)
1824 			return 0;
1825 		return perf_evsel__parse_id_sample(evsel, event, data);
1826 	}
1827 
1828 	array = event->sample.array;
1829 
1830 	/*
1831 	 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1832 	 * up to PERF_SAMPLE_PERIOD.  After that overflow() must be used to
1833 	 * check the format does not go past the end of the event.
1834 	 */
1835 	if (evsel->sample_size + sizeof(event->header) > event->header.size)
1836 		return -EFAULT;
1837 
1838 	data->id = -1ULL;
1839 	if (type & PERF_SAMPLE_IDENTIFIER) {
1840 		data->id = *array;
1841 		array++;
1842 	}
1843 
1844 	if (type & PERF_SAMPLE_IP) {
1845 		data->ip = *array;
1846 		array++;
1847 	}
1848 
1849 	if (type & PERF_SAMPLE_TID) {
1850 		u.val64 = *array;
1851 		if (swapped) {
1852 			/* undo swap of u64, then swap on individual u32s */
1853 			u.val64 = bswap_64(u.val64);
1854 			u.val32[0] = bswap_32(u.val32[0]);
1855 			u.val32[1] = bswap_32(u.val32[1]);
1856 		}
1857 
1858 		data->pid = u.val32[0];
1859 		data->tid = u.val32[1];
1860 		array++;
1861 	}
1862 
1863 	if (type & PERF_SAMPLE_TIME) {
1864 		data->time = *array;
1865 		array++;
1866 	}
1867 
1868 	data->addr = 0;
1869 	if (type & PERF_SAMPLE_ADDR) {
1870 		data->addr = *array;
1871 		array++;
1872 	}
1873 
1874 	if (type & PERF_SAMPLE_ID) {
1875 		data->id = *array;
1876 		array++;
1877 	}
1878 
1879 	if (type & PERF_SAMPLE_STREAM_ID) {
1880 		data->stream_id = *array;
1881 		array++;
1882 	}
1883 
1884 	if (type & PERF_SAMPLE_CPU) {
1885 
1886 		u.val64 = *array;
1887 		if (swapped) {
1888 			/* undo swap of u64, then swap on individual u32s */
1889 			u.val64 = bswap_64(u.val64);
1890 			u.val32[0] = bswap_32(u.val32[0]);
1891 		}
1892 
1893 		data->cpu = u.val32[0];
1894 		array++;
1895 	}
1896 
1897 	if (type & PERF_SAMPLE_PERIOD) {
1898 		data->period = *array;
1899 		array++;
1900 	}
1901 
1902 	if (type & PERF_SAMPLE_READ) {
1903 		u64 read_format = evsel->attr.read_format;
1904 
1905 		OVERFLOW_CHECK_u64(array);
1906 		if (read_format & PERF_FORMAT_GROUP)
1907 			data->read.group.nr = *array;
1908 		else
1909 			data->read.one.value = *array;
1910 
1911 		array++;
1912 
1913 		if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1914 			OVERFLOW_CHECK_u64(array);
1915 			data->read.time_enabled = *array;
1916 			array++;
1917 		}
1918 
1919 		if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1920 			OVERFLOW_CHECK_u64(array);
1921 			data->read.time_running = *array;
1922 			array++;
1923 		}
1924 
1925 		/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1926 		if (read_format & PERF_FORMAT_GROUP) {
1927 			const u64 max_group_nr = UINT64_MAX /
1928 					sizeof(struct sample_read_value);
1929 
1930 			if (data->read.group.nr > max_group_nr)
1931 				return -EFAULT;
1932 			sz = data->read.group.nr *
1933 			     sizeof(struct sample_read_value);
1934 			OVERFLOW_CHECK(array, sz, max_size);
1935 			data->read.group.values =
1936 					(struct sample_read_value *)array;
1937 			array = (void *)array + sz;
1938 		} else {
1939 			OVERFLOW_CHECK_u64(array);
1940 			data->read.one.id = *array;
1941 			array++;
1942 		}
1943 	}
1944 
1945 	if (type & PERF_SAMPLE_CALLCHAIN) {
1946 		const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1947 
1948 		OVERFLOW_CHECK_u64(array);
1949 		data->callchain = (struct ip_callchain *)array++;
1950 		if (data->callchain->nr > max_callchain_nr)
1951 			return -EFAULT;
1952 		sz = data->callchain->nr * sizeof(u64);
1953 		OVERFLOW_CHECK(array, sz, max_size);
1954 		array = (void *)array + sz;
1955 	}
1956 
1957 	if (type & PERF_SAMPLE_RAW) {
1958 		OVERFLOW_CHECK_u64(array);
1959 		u.val64 = *array;
1960 		if (WARN_ONCE(swapped,
1961 			      "Endianness of raw data not corrected!\n")) {
1962 			/* undo swap of u64, then swap on individual u32s */
1963 			u.val64 = bswap_64(u.val64);
1964 			u.val32[0] = bswap_32(u.val32[0]);
1965 			u.val32[1] = bswap_32(u.val32[1]);
1966 		}
1967 		data->raw_size = u.val32[0];
1968 		array = (void *)array + sizeof(u32);
1969 
1970 		OVERFLOW_CHECK(array, data->raw_size, max_size);
1971 		data->raw_data = (void *)array;
1972 		array = (void *)array + data->raw_size;
1973 	}
1974 
1975 	if (type & PERF_SAMPLE_BRANCH_STACK) {
1976 		const u64 max_branch_nr = UINT64_MAX /
1977 					  sizeof(struct branch_entry);
1978 
1979 		OVERFLOW_CHECK_u64(array);
1980 		data->branch_stack = (struct branch_stack *)array++;
1981 
1982 		if (data->branch_stack->nr > max_branch_nr)
1983 			return -EFAULT;
1984 		sz = data->branch_stack->nr * sizeof(struct branch_entry);
1985 		OVERFLOW_CHECK(array, sz, max_size);
1986 		array = (void *)array + sz;
1987 	}
1988 
1989 	if (type & PERF_SAMPLE_REGS_USER) {
1990 		OVERFLOW_CHECK_u64(array);
1991 		data->user_regs.abi = *array;
1992 		array++;
1993 
1994 		if (data->user_regs.abi) {
1995 			u64 mask = evsel->attr.sample_regs_user;
1996 
1997 			sz = hweight_long(mask) * sizeof(u64);
1998 			OVERFLOW_CHECK(array, sz, max_size);
1999 			data->user_regs.mask = mask;
2000 			data->user_regs.regs = (u64 *)array;
2001 			array = (void *)array + sz;
2002 		}
2003 	}
2004 
2005 	if (type & PERF_SAMPLE_STACK_USER) {
2006 		OVERFLOW_CHECK_u64(array);
2007 		sz = *array++;
2008 
2009 		data->user_stack.offset = ((char *)(array - 1)
2010 					  - (char *) event);
2011 
2012 		if (!sz) {
2013 			data->user_stack.size = 0;
2014 		} else {
2015 			OVERFLOW_CHECK(array, sz, max_size);
2016 			data->user_stack.data = (char *)array;
2017 			array = (void *)array + sz;
2018 			OVERFLOW_CHECK_u64(array);
2019 			data->user_stack.size = *array++;
2020 			if (WARN_ONCE(data->user_stack.size > sz,
2021 				      "user stack dump failure\n"))
2022 				return -EFAULT;
2023 		}
2024 	}
2025 
2026 	if (type & PERF_SAMPLE_WEIGHT) {
2027 		OVERFLOW_CHECK_u64(array);
2028 		data->weight = *array;
2029 		array++;
2030 	}
2031 
2032 	data->data_src = PERF_MEM_DATA_SRC_NONE;
2033 	if (type & PERF_SAMPLE_DATA_SRC) {
2034 		OVERFLOW_CHECK_u64(array);
2035 		data->data_src = *array;
2036 		array++;
2037 	}
2038 
2039 	data->transaction = 0;
2040 	if (type & PERF_SAMPLE_TRANSACTION) {
2041 		OVERFLOW_CHECK_u64(array);
2042 		data->transaction = *array;
2043 		array++;
2044 	}
2045 
2046 	data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2047 	if (type & PERF_SAMPLE_REGS_INTR) {
2048 		OVERFLOW_CHECK_u64(array);
2049 		data->intr_regs.abi = *array;
2050 		array++;
2051 
2052 		if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2053 			u64 mask = evsel->attr.sample_regs_intr;
2054 
2055 			sz = hweight_long(mask) * sizeof(u64);
2056 			OVERFLOW_CHECK(array, sz, max_size);
2057 			data->intr_regs.mask = mask;
2058 			data->intr_regs.regs = (u64 *)array;
2059 			array = (void *)array + sz;
2060 		}
2061 	}
2062 
2063 	return 0;
2064 }
2065 
2066 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
2067 				     u64 read_format)
2068 {
2069 	size_t sz, result = sizeof(struct sample_event);
2070 
2071 	if (type & PERF_SAMPLE_IDENTIFIER)
2072 		result += sizeof(u64);
2073 
2074 	if (type & PERF_SAMPLE_IP)
2075 		result += sizeof(u64);
2076 
2077 	if (type & PERF_SAMPLE_TID)
2078 		result += sizeof(u64);
2079 
2080 	if (type & PERF_SAMPLE_TIME)
2081 		result += sizeof(u64);
2082 
2083 	if (type & PERF_SAMPLE_ADDR)
2084 		result += sizeof(u64);
2085 
2086 	if (type & PERF_SAMPLE_ID)
2087 		result += sizeof(u64);
2088 
2089 	if (type & PERF_SAMPLE_STREAM_ID)
2090 		result += sizeof(u64);
2091 
2092 	if (type & PERF_SAMPLE_CPU)
2093 		result += sizeof(u64);
2094 
2095 	if (type & PERF_SAMPLE_PERIOD)
2096 		result += sizeof(u64);
2097 
2098 	if (type & PERF_SAMPLE_READ) {
2099 		result += sizeof(u64);
2100 		if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2101 			result += sizeof(u64);
2102 		if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2103 			result += sizeof(u64);
2104 		/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2105 		if (read_format & PERF_FORMAT_GROUP) {
2106 			sz = sample->read.group.nr *
2107 			     sizeof(struct sample_read_value);
2108 			result += sz;
2109 		} else {
2110 			result += sizeof(u64);
2111 		}
2112 	}
2113 
2114 	if (type & PERF_SAMPLE_CALLCHAIN) {
2115 		sz = (sample->callchain->nr + 1) * sizeof(u64);
2116 		result += sz;
2117 	}
2118 
2119 	if (type & PERF_SAMPLE_RAW) {
2120 		result += sizeof(u32);
2121 		result += sample->raw_size;
2122 	}
2123 
2124 	if (type & PERF_SAMPLE_BRANCH_STACK) {
2125 		sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2126 		sz += sizeof(u64);
2127 		result += sz;
2128 	}
2129 
2130 	if (type & PERF_SAMPLE_REGS_USER) {
2131 		if (sample->user_regs.abi) {
2132 			result += sizeof(u64);
2133 			sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
2134 			result += sz;
2135 		} else {
2136 			result += sizeof(u64);
2137 		}
2138 	}
2139 
2140 	if (type & PERF_SAMPLE_STACK_USER) {
2141 		sz = sample->user_stack.size;
2142 		result += sizeof(u64);
2143 		if (sz) {
2144 			result += sz;
2145 			result += sizeof(u64);
2146 		}
2147 	}
2148 
2149 	if (type & PERF_SAMPLE_WEIGHT)
2150 		result += sizeof(u64);
2151 
2152 	if (type & PERF_SAMPLE_DATA_SRC)
2153 		result += sizeof(u64);
2154 
2155 	if (type & PERF_SAMPLE_TRANSACTION)
2156 		result += sizeof(u64);
2157 
2158 	if (type & PERF_SAMPLE_REGS_INTR) {
2159 		if (sample->intr_regs.abi) {
2160 			result += sizeof(u64);
2161 			sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2162 			result += sz;
2163 		} else {
2164 			result += sizeof(u64);
2165 		}
2166 	}
2167 
2168 	return result;
2169 }
2170 
2171 int perf_event__synthesize_sample(union perf_event *event, u64 type,
2172 				  u64 read_format,
2173 				  const struct perf_sample *sample,
2174 				  bool swapped)
2175 {
2176 	u64 *array;
2177 	size_t sz;
2178 	/*
2179 	 * used for cross-endian analysis. See git commit 65014ab3
2180 	 * for why this goofiness is needed.
2181 	 */
2182 	union u64_swap u;
2183 
2184 	array = event->sample.array;
2185 
2186 	if (type & PERF_SAMPLE_IDENTIFIER) {
2187 		*array = sample->id;
2188 		array++;
2189 	}
2190 
2191 	if (type & PERF_SAMPLE_IP) {
2192 		*array = sample->ip;
2193 		array++;
2194 	}
2195 
2196 	if (type & PERF_SAMPLE_TID) {
2197 		u.val32[0] = sample->pid;
2198 		u.val32[1] = sample->tid;
2199 		if (swapped) {
2200 			/*
2201 			 * Inverse of what is done in perf_evsel__parse_sample
2202 			 */
2203 			u.val32[0] = bswap_32(u.val32[0]);
2204 			u.val32[1] = bswap_32(u.val32[1]);
2205 			u.val64 = bswap_64(u.val64);
2206 		}
2207 
2208 		*array = u.val64;
2209 		array++;
2210 	}
2211 
2212 	if (type & PERF_SAMPLE_TIME) {
2213 		*array = sample->time;
2214 		array++;
2215 	}
2216 
2217 	if (type & PERF_SAMPLE_ADDR) {
2218 		*array = sample->addr;
2219 		array++;
2220 	}
2221 
2222 	if (type & PERF_SAMPLE_ID) {
2223 		*array = sample->id;
2224 		array++;
2225 	}
2226 
2227 	if (type & PERF_SAMPLE_STREAM_ID) {
2228 		*array = sample->stream_id;
2229 		array++;
2230 	}
2231 
2232 	if (type & PERF_SAMPLE_CPU) {
2233 		u.val32[0] = sample->cpu;
2234 		if (swapped) {
2235 			/*
2236 			 * Inverse of what is done in perf_evsel__parse_sample
2237 			 */
2238 			u.val32[0] = bswap_32(u.val32[0]);
2239 			u.val64 = bswap_64(u.val64);
2240 		}
2241 		*array = u.val64;
2242 		array++;
2243 	}
2244 
2245 	if (type & PERF_SAMPLE_PERIOD) {
2246 		*array = sample->period;
2247 		array++;
2248 	}
2249 
2250 	if (type & PERF_SAMPLE_READ) {
2251 		if (read_format & PERF_FORMAT_GROUP)
2252 			*array = sample->read.group.nr;
2253 		else
2254 			*array = sample->read.one.value;
2255 		array++;
2256 
2257 		if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2258 			*array = sample->read.time_enabled;
2259 			array++;
2260 		}
2261 
2262 		if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2263 			*array = sample->read.time_running;
2264 			array++;
2265 		}
2266 
2267 		/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2268 		if (read_format & PERF_FORMAT_GROUP) {
2269 			sz = sample->read.group.nr *
2270 			     sizeof(struct sample_read_value);
2271 			memcpy(array, sample->read.group.values, sz);
2272 			array = (void *)array + sz;
2273 		} else {
2274 			*array = sample->read.one.id;
2275 			array++;
2276 		}
2277 	}
2278 
2279 	if (type & PERF_SAMPLE_CALLCHAIN) {
2280 		sz = (sample->callchain->nr + 1) * sizeof(u64);
2281 		memcpy(array, sample->callchain, sz);
2282 		array = (void *)array + sz;
2283 	}
2284 
2285 	if (type & PERF_SAMPLE_RAW) {
2286 		u.val32[0] = sample->raw_size;
2287 		if (WARN_ONCE(swapped,
2288 			      "Endianness of raw data not corrected!\n")) {
2289 			/*
2290 			 * Inverse of what is done in perf_evsel__parse_sample
2291 			 */
2292 			u.val32[0] = bswap_32(u.val32[0]);
2293 			u.val32[1] = bswap_32(u.val32[1]);
2294 			u.val64 = bswap_64(u.val64);
2295 		}
2296 		*array = u.val64;
2297 		array = (void *)array + sizeof(u32);
2298 
2299 		memcpy(array, sample->raw_data, sample->raw_size);
2300 		array = (void *)array + sample->raw_size;
2301 	}
2302 
2303 	if (type & PERF_SAMPLE_BRANCH_STACK) {
2304 		sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2305 		sz += sizeof(u64);
2306 		memcpy(array, sample->branch_stack, sz);
2307 		array = (void *)array + sz;
2308 	}
2309 
2310 	if (type & PERF_SAMPLE_REGS_USER) {
2311 		if (sample->user_regs.abi) {
2312 			*array++ = sample->user_regs.abi;
2313 			sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
2314 			memcpy(array, sample->user_regs.regs, sz);
2315 			array = (void *)array + sz;
2316 		} else {
2317 			*array++ = 0;
2318 		}
2319 	}
2320 
2321 	if (type & PERF_SAMPLE_STACK_USER) {
2322 		sz = sample->user_stack.size;
2323 		*array++ = sz;
2324 		if (sz) {
2325 			memcpy(array, sample->user_stack.data, sz);
2326 			array = (void *)array + sz;
2327 			*array++ = sz;
2328 		}
2329 	}
2330 
2331 	if (type & PERF_SAMPLE_WEIGHT) {
2332 		*array = sample->weight;
2333 		array++;
2334 	}
2335 
2336 	if (type & PERF_SAMPLE_DATA_SRC) {
2337 		*array = sample->data_src;
2338 		array++;
2339 	}
2340 
2341 	if (type & PERF_SAMPLE_TRANSACTION) {
2342 		*array = sample->transaction;
2343 		array++;
2344 	}
2345 
2346 	if (type & PERF_SAMPLE_REGS_INTR) {
2347 		if (sample->intr_regs.abi) {
2348 			*array++ = sample->intr_regs.abi;
2349 			sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2350 			memcpy(array, sample->intr_regs.regs, sz);
2351 			array = (void *)array + sz;
2352 		} else {
2353 			*array++ = 0;
2354 		}
2355 	}
2356 
2357 	return 0;
2358 }
2359 
2360 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2361 {
2362 	return pevent_find_field(evsel->tp_format, name);
2363 }
2364 
2365 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2366 			 const char *name)
2367 {
2368 	struct format_field *field = perf_evsel__field(evsel, name);
2369 	int offset;
2370 
2371 	if (!field)
2372 		return NULL;
2373 
2374 	offset = field->offset;
2375 
2376 	if (field->flags & FIELD_IS_DYNAMIC) {
2377 		offset = *(int *)(sample->raw_data + field->offset);
2378 		offset &= 0xffff;
2379 	}
2380 
2381 	return sample->raw_data + offset;
2382 }
2383 
2384 u64 format_field__intval(struct format_field *field, struct perf_sample *sample,
2385 			 bool needs_swap)
2386 {
2387 	u64 value;
2388 	void *ptr = sample->raw_data + field->offset;
2389 
2390 	switch (field->size) {
2391 	case 1:
2392 		return *(u8 *)ptr;
2393 	case 2:
2394 		value = *(u16 *)ptr;
2395 		break;
2396 	case 4:
2397 		value = *(u32 *)ptr;
2398 		break;
2399 	case 8:
2400 		memcpy(&value, ptr, sizeof(u64));
2401 		break;
2402 	default:
2403 		return 0;
2404 	}
2405 
2406 	if (!needs_swap)
2407 		return value;
2408 
2409 	switch (field->size) {
2410 	case 2:
2411 		return bswap_16(value);
2412 	case 4:
2413 		return bswap_32(value);
2414 	case 8:
2415 		return bswap_64(value);
2416 	default:
2417 		return 0;
2418 	}
2419 
2420 	return 0;
2421 }
2422 
2423 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2424 		       const char *name)
2425 {
2426 	struct format_field *field = perf_evsel__field(evsel, name);
2427 
2428 	if (!field)
2429 		return 0;
2430 
2431 	return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2432 }
2433 
2434 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2435 			  char *msg, size_t msgsize)
2436 {
2437 	int paranoid;
2438 
2439 	if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2440 	    evsel->attr.type   == PERF_TYPE_HARDWARE &&
2441 	    evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2442 		/*
2443 		 * If it's cycles then fall back to hrtimer based
2444 		 * cpu-clock-tick sw counter, which is always available even if
2445 		 * no PMU support.
2446 		 *
2447 		 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2448 		 * b0a873e).
2449 		 */
2450 		scnprintf(msg, msgsize, "%s",
2451 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2452 
2453 		evsel->attr.type   = PERF_TYPE_SOFTWARE;
2454 		evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2455 
2456 		zfree(&evsel->name);
2457 		return true;
2458 	} else if (err == EACCES && !evsel->attr.exclude_kernel &&
2459 		   (paranoid = perf_event_paranoid()) > 1) {
2460 		const char *name = perf_evsel__name(evsel);
2461 		char *new_name;
2462 
2463 		if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0)
2464 			return false;
2465 
2466 		if (evsel->name)
2467 			free(evsel->name);
2468 		evsel->name = new_name;
2469 		scnprintf(msg, msgsize,
2470 "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2471 		evsel->attr.exclude_kernel = 1;
2472 
2473 		return true;
2474 	}
2475 
2476 	return false;
2477 }
2478 
2479 static bool find_process(const char *name)
2480 {
2481 	size_t len = strlen(name);
2482 	DIR *dir;
2483 	struct dirent *d;
2484 	int ret = -1;
2485 
2486 	dir = opendir(procfs__mountpoint());
2487 	if (!dir)
2488 		return false;
2489 
2490 	/* Walk through the directory. */
2491 	while (ret && (d = readdir(dir)) != NULL) {
2492 		char path[PATH_MAX];
2493 		char *data;
2494 		size_t size;
2495 
2496 		if ((d->d_type != DT_DIR) ||
2497 		     !strcmp(".", d->d_name) ||
2498 		     !strcmp("..", d->d_name))
2499 			continue;
2500 
2501 		scnprintf(path, sizeof(path), "%s/%s/comm",
2502 			  procfs__mountpoint(), d->d_name);
2503 
2504 		if (filename__read_str(path, &data, &size))
2505 			continue;
2506 
2507 		ret = strncmp(name, data, len);
2508 		free(data);
2509 	}
2510 
2511 	closedir(dir);
2512 	return ret ? false : true;
2513 }
2514 
2515 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2516 			      int err, char *msg, size_t size)
2517 {
2518 	char sbuf[STRERR_BUFSIZE];
2519 	int printed = 0;
2520 
2521 	switch (err) {
2522 	case EPERM:
2523 	case EACCES:
2524 		if (err == EPERM)
2525 			printed = scnprintf(msg, size,
2526 				"No permission to enable %s event.\n\n",
2527 				perf_evsel__name(evsel));
2528 
2529 		return scnprintf(msg + printed, size - printed,
2530 		 "You may not have permission to collect %sstats.\n\n"
2531 		 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2532 		 "which controls use of the performance events system by\n"
2533 		 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2534 		 "The current value is %d:\n\n"
2535 		 "  -1: Allow use of (almost) all events by all users\n"
2536 		 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2537 		 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2538 		 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2539 		 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2540 		 "	kernel.perf_event_paranoid = -1\n" ,
2541 				 target->system_wide ? "system-wide " : "",
2542 				 perf_event_paranoid());
2543 	case ENOENT:
2544 		return scnprintf(msg, size, "The %s event is not supported.",
2545 				 perf_evsel__name(evsel));
2546 	case EMFILE:
2547 		return scnprintf(msg, size, "%s",
2548 			 "Too many events are opened.\n"
2549 			 "Probably the maximum number of open file descriptors has been reached.\n"
2550 			 "Hint: Try again after reducing the number of events.\n"
2551 			 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2552 	case ENOMEM:
2553 		if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 &&
2554 		    access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2555 			return scnprintf(msg, size,
2556 					 "Not enough memory to setup event with callchain.\n"
2557 					 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2558 					 "Hint: Current value: %d", sysctl_perf_event_max_stack);
2559 		break;
2560 	case ENODEV:
2561 		if (target->cpu_list)
2562 			return scnprintf(msg, size, "%s",
2563 	 "No such device - did you specify an out-of-range profile CPU?");
2564 		break;
2565 	case EOPNOTSUPP:
2566 		if (evsel->attr.sample_period != 0)
2567 			return scnprintf(msg, size, "%s",
2568 	"PMU Hardware doesn't support sampling/overflow-interrupts.");
2569 		if (evsel->attr.precise_ip)
2570 			return scnprintf(msg, size, "%s",
2571 	"\'precise\' request may not be supported. Try removing 'p' modifier.");
2572 #if defined(__i386__) || defined(__x86_64__)
2573 		if (evsel->attr.type == PERF_TYPE_HARDWARE)
2574 			return scnprintf(msg, size, "%s",
2575 	"No hardware sampling interrupt available.\n"
2576 	"No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2577 #endif
2578 		break;
2579 	case EBUSY:
2580 		if (find_process("oprofiled"))
2581 			return scnprintf(msg, size,
2582 	"The PMU counters are busy/taken by another profiler.\n"
2583 	"We found oprofile daemon running, please stop it and try again.");
2584 		break;
2585 	case EINVAL:
2586 		if (evsel->attr.write_backward && perf_missing_features.write_backward)
2587 			return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
2588 		if (perf_missing_features.clockid)
2589 			return scnprintf(msg, size, "clockid feature not supported.");
2590 		if (perf_missing_features.clockid_wrong)
2591 			return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2592 		break;
2593 	default:
2594 		break;
2595 	}
2596 
2597 	return scnprintf(msg, size,
2598 	"The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2599 	"/bin/dmesg may provide additional information.\n"
2600 	"No CONFIG_PERF_EVENTS=y kernel support configured?",
2601 			 err, str_error_r(err, sbuf, sizeof(sbuf)),
2602 			 perf_evsel__name(evsel));
2603 }
2604 
2605 char *perf_evsel__env_arch(struct perf_evsel *evsel)
2606 {
2607 	if (evsel && evsel->evlist && evsel->evlist->env)
2608 		return evsel->evlist->env->arch;
2609 	return NULL;
2610 }
2611