xref: /linux/tools/perf/util/event.c (revision 55657d7ac8caa98c7c0ef241bf64e176db899b4d)
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <inttypes.h>
4 #include <linux/compiler.h>
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <perf/cpumap.h>
8 #include <perf/event.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
14 #include <linux/perf_event.h>
15 #include <linux/zalloc.h>
16 #include "cpumap.h"
17 #include "dso.h"
18 #include "event.h"
19 #include "debug.h"
20 #include "hist.h"
21 #include "machine.h"
22 #include "sort.h"
23 #include "string2.h"
24 #include "strlist.h"
25 #include "thread.h"
26 #include "thread_map.h"
27 #include "time-utils.h"
28 #include <linux/ctype.h>
29 #include "map.h"
30 #include "util/namespaces.h"
31 #include "symbol.h"
32 #include "symbol/kallsyms.h"
33 #include "asm/bug.h"
34 #include "stat.h"
35 #include "session.h"
36 #include "bpf-event.h"
37 #include "print_binary.h"
38 #include "tool.h"
39 #include "util.h"
40 
41 static const char *perf_event__names[] = {
42 	[0]					= "TOTAL",
43 	[PERF_RECORD_MMAP]			= "MMAP",
44 	[PERF_RECORD_MMAP2]			= "MMAP2",
45 	[PERF_RECORD_LOST]			= "LOST",
46 	[PERF_RECORD_COMM]			= "COMM",
47 	[PERF_RECORD_EXIT]			= "EXIT",
48 	[PERF_RECORD_THROTTLE]			= "THROTTLE",
49 	[PERF_RECORD_UNTHROTTLE]		= "UNTHROTTLE",
50 	[PERF_RECORD_FORK]			= "FORK",
51 	[PERF_RECORD_READ]			= "READ",
52 	[PERF_RECORD_SAMPLE]			= "SAMPLE",
53 	[PERF_RECORD_AUX]			= "AUX",
54 	[PERF_RECORD_ITRACE_START]		= "ITRACE_START",
55 	[PERF_RECORD_LOST_SAMPLES]		= "LOST_SAMPLES",
56 	[PERF_RECORD_SWITCH]			= "SWITCH",
57 	[PERF_RECORD_SWITCH_CPU_WIDE]		= "SWITCH_CPU_WIDE",
58 	[PERF_RECORD_NAMESPACES]		= "NAMESPACES",
59 	[PERF_RECORD_KSYMBOL]			= "KSYMBOL",
60 	[PERF_RECORD_BPF_EVENT]			= "BPF_EVENT",
61 	[PERF_RECORD_CGROUP]			= "CGROUP",
62 	[PERF_RECORD_TEXT_POKE]			= "TEXT_POKE",
63 	[PERF_RECORD_AUX_OUTPUT_HW_ID]		= "AUX_OUTPUT_HW_ID",
64 	[PERF_RECORD_CALLCHAIN_DEFERRED]	= "CALLCHAIN_DEFERRED",
65 	[PERF_RECORD_HEADER_ATTR]		= "ATTR",
66 	[PERF_RECORD_HEADER_EVENT_TYPE]		= "EVENT_TYPE",
67 	[PERF_RECORD_HEADER_TRACING_DATA]	= "TRACING_DATA",
68 	[PERF_RECORD_HEADER_BUILD_ID]		= "BUILD_ID",
69 	[PERF_RECORD_FINISHED_ROUND]		= "FINISHED_ROUND",
70 	[PERF_RECORD_ID_INDEX]			= "ID_INDEX",
71 	[PERF_RECORD_AUXTRACE_INFO]		= "AUXTRACE_INFO",
72 	[PERF_RECORD_AUXTRACE]			= "AUXTRACE",
73 	[PERF_RECORD_AUXTRACE_ERROR]		= "AUXTRACE_ERROR",
74 	[PERF_RECORD_THREAD_MAP]		= "THREAD_MAP",
75 	[PERF_RECORD_CPU_MAP]			= "CPU_MAP",
76 	[PERF_RECORD_STAT_CONFIG]		= "STAT_CONFIG",
77 	[PERF_RECORD_STAT]			= "STAT",
78 	[PERF_RECORD_STAT_ROUND]		= "STAT_ROUND",
79 	[PERF_RECORD_EVENT_UPDATE]		= "EVENT_UPDATE",
80 	[PERF_RECORD_TIME_CONV]			= "TIME_CONV",
81 	[PERF_RECORD_HEADER_FEATURE]		= "FEATURE",
82 	[PERF_RECORD_COMPRESSED]		= "COMPRESSED",
83 	[PERF_RECORD_FINISHED_INIT]		= "FINISHED_INIT",
84 	[PERF_RECORD_COMPRESSED2]		= "COMPRESSED2",
85 	[PERF_RECORD_BPF_METADATA]		= "BPF_METADATA",
86 	[PERF_RECORD_SCHEDSTAT_CPU]		= "SCHEDSTAT_CPU",
87 	[PERF_RECORD_SCHEDSTAT_DOMAIN]		= "SCHEDSTAT_DOMAIN",
88 };
89 
90 const char *perf_event__name(unsigned int id)
91 {
92 	if (id >= ARRAY_SIZE(perf_event__names))
93 		return "INVALID";
94 	if (!perf_event__names[id])
95 		return "UNKNOWN";
96 	return perf_event__names[id];
97 }
98 
99 struct process_symbol_args {
100 	const char *name;
101 	u64	   start;
102 };
103 
104 static int find_func_symbol_cb(void *arg, const char *name, char type,
105 			       u64 start)
106 {
107 	struct process_symbol_args *args = arg;
108 
109 	/*
110 	 * Must be a function or at least an alias, as in PARISC64, where "_text" is
111 	 * an 'A' to the same address as "_stext".
112 	 */
113 	if (!(kallsyms__is_function(type) ||
114 	      type == 'A') || strcmp(name, args->name))
115 		return 0;
116 
117 	args->start = start;
118 	return 1;
119 }
120 
121 static int find_any_symbol_cb(void *arg, const char *name,
122 			      char type __maybe_unused, u64 start)
123 {
124 	struct process_symbol_args *args = arg;
125 
126 	if (strcmp(name, args->name))
127 		return 0;
128 
129 	args->start = start;
130 	return 1;
131 }
132 
133 int kallsyms__get_function_start(const char *kallsyms_filename,
134 				 const char *symbol_name, u64 *addr)
135 {
136 	struct process_symbol_args args = { .name = symbol_name, };
137 
138 	if (kallsyms__parse(kallsyms_filename, &args, find_func_symbol_cb) <= 0)
139 		return -1;
140 
141 	*addr = args.start;
142 	return 0;
143 }
144 
145 int kallsyms__get_symbol_start(const char *kallsyms_filename,
146 			       const char *symbol_name, u64 *addr)
147 {
148 	struct process_symbol_args args = { .name = symbol_name, };
149 
150 	if (kallsyms__parse(kallsyms_filename, &args, find_any_symbol_cb) <= 0)
151 		return -1;
152 
153 	*addr = args.start;
154 	return 0;
155 }
156 
157 void perf_event__read_stat_config(struct perf_stat_config *config,
158 				  struct perf_record_stat_config *event)
159 {
160 	unsigned i;
161 
162 	for (i = 0; i < event->nr; i++) {
163 
164 		switch (event->data[i].tag) {
165 #define CASE(__term, __val)					\
166 		case PERF_STAT_CONFIG_TERM__##__term:		\
167 			config->__val = event->data[i].val;	\
168 			break;
169 
170 		CASE(AGGR_MODE,  aggr_mode)
171 		CASE(SCALE,      scale)
172 		CASE(INTERVAL,   interval)
173 		CASE(AGGR_LEVEL, aggr_level)
174 #undef CASE
175 		default:
176 			pr_warning("unknown stat config term %" PRI_lu64 "\n",
177 				   event->data[i].tag);
178 		}
179 	}
180 }
181 
182 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
183 {
184 	const char *s;
185 
186 	if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
187 		s = " exec";
188 	else
189 		s = "";
190 
191 	return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
192 }
193 
194 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
195 {
196 	size_t ret = 0;
197 	struct perf_ns_link_info *ns_link_info;
198 	u32 nr_namespaces, idx;
199 
200 	ns_link_info = event->namespaces.link_info;
201 	nr_namespaces = event->namespaces.nr_namespaces;
202 
203 	ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
204 		       event->namespaces.pid,
205 		       event->namespaces.tid,
206 		       nr_namespaces);
207 
208 	for (idx = 0; idx < nr_namespaces; idx++) {
209 		if (idx && (idx % 4 == 0))
210 			ret += fprintf(fp, "\n\t\t ");
211 
212 		ret  += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
213 				perf_ns__name(idx), (u64)ns_link_info[idx].dev,
214 				(u64)ns_link_info[idx].ino,
215 				((idx + 1) != nr_namespaces) ? ", " : "]\n");
216 	}
217 
218 	return ret;
219 }
220 
221 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp)
222 {
223 	return fprintf(fp, " cgroup: %" PRI_lu64 " %s\n",
224 		       event->cgroup.id, event->cgroup.path);
225 }
226 
227 int perf_event__process_comm(const struct perf_tool *tool __maybe_unused,
228 			     union perf_event *event,
229 			     struct perf_sample *sample,
230 			     struct machine *machine)
231 {
232 	return machine__process_comm_event(machine, event, sample);
233 }
234 
235 int perf_event__process_namespaces(const struct perf_tool *tool __maybe_unused,
236 				   union perf_event *event,
237 				   struct perf_sample *sample,
238 				   struct machine *machine)
239 {
240 	return machine__process_namespaces_event(machine, event, sample);
241 }
242 
243 int perf_event__process_cgroup(const struct perf_tool *tool __maybe_unused,
244 			       union perf_event *event,
245 			       struct perf_sample *sample,
246 			       struct machine *machine)
247 {
248 	return machine__process_cgroup_event(machine, event, sample);
249 }
250 
251 int perf_event__process_lost(const struct perf_tool *tool __maybe_unused,
252 			     union perf_event *event,
253 			     struct perf_sample *sample,
254 			     struct machine *machine)
255 {
256 	return machine__process_lost_event(machine, event, sample);
257 }
258 
259 int perf_event__process_aux(const struct perf_tool *tool __maybe_unused,
260 			    union perf_event *event,
261 			    struct perf_sample *sample __maybe_unused,
262 			    struct machine *machine)
263 {
264 	return machine__process_aux_event(machine, event);
265 }
266 
267 int perf_event__process_itrace_start(const struct perf_tool *tool __maybe_unused,
268 				     union perf_event *event,
269 				     struct perf_sample *sample __maybe_unused,
270 				     struct machine *machine)
271 {
272 	return machine__process_itrace_start_event(machine, event);
273 }
274 
275 int perf_event__process_aux_output_hw_id(const struct perf_tool *tool __maybe_unused,
276 					 union perf_event *event,
277 					 struct perf_sample *sample __maybe_unused,
278 					 struct machine *machine)
279 {
280 	return machine__process_aux_output_hw_id_event(machine, event);
281 }
282 
283 int perf_event__process_lost_samples(const struct perf_tool *tool __maybe_unused,
284 				     union perf_event *event,
285 				     struct perf_sample *sample,
286 				     struct machine *machine)
287 {
288 	return machine__process_lost_samples_event(machine, event, sample);
289 }
290 
291 int perf_event__process_switch(const struct perf_tool *tool __maybe_unused,
292 			       union perf_event *event,
293 			       struct perf_sample *sample __maybe_unused,
294 			       struct machine *machine)
295 {
296 	return machine__process_switch_event(machine, event);
297 }
298 
299 int perf_event__process_ksymbol(const struct perf_tool *tool __maybe_unused,
300 				union perf_event *event,
301 				struct perf_sample *sample __maybe_unused,
302 				struct machine *machine)
303 {
304 	return machine__process_ksymbol(machine, event, sample);
305 }
306 
307 int perf_event__process_bpf(const struct perf_tool *tool __maybe_unused,
308 			    union perf_event *event,
309 			    struct perf_sample *sample,
310 			    struct machine *machine)
311 {
312 	return machine__process_bpf(machine, event, sample);
313 }
314 
315 int perf_event__process_text_poke(const struct perf_tool *tool __maybe_unused,
316 				  union perf_event *event,
317 				  struct perf_sample *sample,
318 				  struct machine *machine)
319 {
320 	return machine__process_text_poke(machine, event, sample);
321 }
322 
323 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
324 {
325 	return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n",
326 		       event->mmap.pid, event->mmap.tid, event->mmap.start,
327 		       event->mmap.len, event->mmap.pgoff,
328 		       (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
329 		       event->mmap.filename);
330 }
331 
332 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
333 {
334 	if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
335 		char sbuild_id[SBUILD_ID_SIZE];
336 		struct build_id bid;
337 
338 		build_id__init(&bid, event->mmap2.build_id,
339 			       event->mmap2.build_id_size);
340 		build_id__snprintf(&bid, sbuild_id, sizeof(sbuild_id));
341 
342 		return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
343 				   " <%s>]: %c%c%c%c %s\n",
344 			       event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
345 			       event->mmap2.len, event->mmap2.pgoff, sbuild_id,
346 			       (event->mmap2.prot & PROT_READ) ? 'r' : '-',
347 			       (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
348 			       (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
349 			       (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
350 			       event->mmap2.filename);
351 	} else {
352 		return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
353 				   " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n",
354 			       event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
355 			       event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
356 			       event->mmap2.min, event->mmap2.ino,
357 			       event->mmap2.ino_generation,
358 			       (event->mmap2.prot & PROT_READ) ? 'r' : '-',
359 			       (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
360 			       (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
361 			       (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
362 			       event->mmap2.filename);
363 	}
364 }
365 
366 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
367 {
368 	struct perf_thread_map *threads = thread_map__new_event(&event->thread_map);
369 	size_t ret;
370 
371 	ret = fprintf(fp, " nr: ");
372 
373 	if (threads)
374 		ret += thread_map__fprintf(threads, fp);
375 	else
376 		ret += fprintf(fp, "failed to get threads from event\n");
377 
378 	perf_thread_map__put(threads);
379 	return ret;
380 }
381 
382 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
383 {
384 	struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
385 	size_t ret;
386 
387 	ret = fprintf(fp, ": ");
388 
389 	if (cpus)
390 		ret += cpu_map__fprintf(cpus, fp);
391 	else
392 		ret += fprintf(fp, "failed to get cpumap from event\n");
393 
394 	perf_cpu_map__put(cpus);
395 	return ret;
396 }
397 
398 int perf_event__process_mmap(const struct perf_tool *tool __maybe_unused,
399 			     union perf_event *event,
400 			     struct perf_sample *sample,
401 			     struct machine *machine)
402 {
403 	return machine__process_mmap_event(machine, event, sample);
404 }
405 
406 int perf_event__process_mmap2(const struct perf_tool *tool __maybe_unused,
407 			     union perf_event *event,
408 			     struct perf_sample *sample,
409 			     struct machine *machine)
410 {
411 	return machine__process_mmap2_event(machine, event, sample);
412 }
413 
414 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
415 {
416 	return fprintf(fp, "(%d:%d):(%d:%d)\n",
417 		       event->fork.pid, event->fork.tid,
418 		       event->fork.ppid, event->fork.ptid);
419 }
420 
421 int perf_event__process_fork(const struct perf_tool *tool __maybe_unused,
422 			     union perf_event *event,
423 			     struct perf_sample *sample,
424 			     struct machine *machine)
425 {
426 	return machine__process_fork_event(machine, event, sample);
427 }
428 
429 int perf_event__process_exit(const struct perf_tool *tool __maybe_unused,
430 			     union perf_event *event,
431 			     struct perf_sample *sample,
432 			     struct machine *machine)
433 {
434 	return machine__process_exit_event(machine, event, sample);
435 }
436 
437 int perf_event__exit_del_thread(const struct perf_tool *tool __maybe_unused,
438 				union perf_event *event,
439 				struct perf_sample *sample __maybe_unused,
440 				struct machine *machine)
441 {
442 	struct thread *thread = machine__findnew_thread(machine,
443 							event->fork.pid,
444 							event->fork.tid);
445 
446 	dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
447 		    event->fork.ppid, event->fork.ptid);
448 
449 	if (thread) {
450 		machine__remove_thread(machine, thread);
451 		thread__put(thread);
452 	}
453 
454 	return 0;
455 }
456 
457 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
458 {
459 	return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s%s]\n",
460 		       event->aux.aux_offset, event->aux.aux_size,
461 		       event->aux.flags,
462 		       event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
463 		       event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
464 		       event->aux.flags & PERF_AUX_FLAG_PARTIAL   ? "P" : "",
465 		       event->aux.flags & PERF_AUX_FLAG_COLLISION ? "C" : "");
466 }
467 
468 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
469 {
470 	return fprintf(fp, " pid: %u tid: %u\n",
471 		       event->itrace_start.pid, event->itrace_start.tid);
472 }
473 
474 size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp)
475 {
476 	return fprintf(fp, " hw_id: %#"PRI_lx64"\n",
477 		       event->aux_output_hw_id.hw_id);
478 }
479 
480 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
481 {
482 	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
483 	const char *in_out = !out ? "IN         " :
484 		!(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ?
485 				    "OUT        " : "OUT preempt";
486 
487 	if (event->header.type == PERF_RECORD_SWITCH)
488 		return fprintf(fp, " %s\n", in_out);
489 
490 	return fprintf(fp, " %s  %s pid/tid: %5d/%-5d\n",
491 		       in_out, out ? "next" : "prev",
492 		       event->context_switch.next_prev_pid,
493 		       event->context_switch.next_prev_tid);
494 }
495 
496 static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp)
497 {
498 	return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost);
499 }
500 
501 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp)
502 {
503 	return fprintf(fp, " addr %" PRI_lx64 " len %u type %u flags 0x%x name %s\n",
504 		       event->ksymbol.addr, event->ksymbol.len,
505 		       event->ksymbol.ksym_type,
506 		       event->ksymbol.flags, event->ksymbol.name);
507 }
508 
509 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp)
510 {
511 	return fprintf(fp, " type %u, flags %u, id %u\n",
512 		       event->bpf.type, event->bpf.flags, event->bpf.id);
513 }
514 
515 size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp)
516 {
517 	struct perf_record_bpf_metadata *metadata = &event->bpf_metadata;
518 	size_t ret;
519 
520 	ret = fprintf(fp, " prog %s\n", metadata->prog_name);
521 	for (__u32 i = 0; i < metadata->nr_entries; i++) {
522 		ret += fprintf(fp, "  entry %d: %20s = %s\n", i,
523 			       metadata->entries[i].key,
524 			       metadata->entries[i].value);
525 	}
526 	return ret;
527 }
528 
529 static int text_poke_printer(enum binary_printer_ops op, unsigned int val,
530 			     void *extra, FILE *fp)
531 {
532 	bool old = *(bool *)extra;
533 
534 	switch ((int)op) {
535 	case BINARY_PRINT_LINE_BEGIN:
536 		return fprintf(fp, "            %s bytes:", old ? "Old" : "New");
537 	case BINARY_PRINT_NUM_DATA:
538 		return fprintf(fp, " %02x", val);
539 	case BINARY_PRINT_LINE_END:
540 		return fprintf(fp, "\n");
541 	default:
542 		return 0;
543 	}
544 }
545 
546 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine, FILE *fp)
547 {
548 	struct perf_record_text_poke_event *tp = &event->text_poke;
549 	size_t ret;
550 	bool old;
551 
552 	ret = fprintf(fp, " %" PRI_lx64 " ", tp->addr);
553 	if (machine) {
554 		struct addr_location al;
555 
556 		addr_location__init(&al);
557 		al.map = maps__find(machine__kernel_maps(machine), tp->addr);
558 		if (al.map && map__load(al.map) >= 0) {
559 			al.addr = map__map_ip(al.map, tp->addr);
560 			al.sym = map__find_symbol(al.map, al.addr);
561 			if (al.sym)
562 				ret += symbol__fprintf_symname_offs(al.sym, &al, fp);
563 		}
564 		addr_location__exit(&al);
565 	}
566 	ret += fprintf(fp, " old len %u new len %u\n", tp->old_len, tp->new_len);
567 	old = true;
568 	ret += binary__fprintf(tp->bytes, tp->old_len, 16, text_poke_printer,
569 			       &old, fp);
570 	old = false;
571 	ret += binary__fprintf(tp->bytes + tp->old_len, tp->new_len, 16,
572 			       text_poke_printer, &old, fp);
573 	return ret;
574 }
575 
576 size_t perf_event__fprintf_schedstat_cpu(union perf_event *event, FILE *fp)
577 {
578 	struct perf_record_schedstat_cpu *cs = &event->schedstat_cpu;
579 	size_t size = fprintf(fp, "\ncpu%u ", cs->cpu);
580 	__u16 version = cs->version;
581 
582 #define CPU_FIELD(_type, _name, _desc, _format, _is_pct, _pct_of, _ver)		\
583 	size += fprintf(fp, "%" PRIu64 " ", (uint64_t)cs->_ver._name)
584 
585 	if (version == 15) {
586 #include <perf/schedstat-v15.h>
587 		return size;
588 	} else if (version == 16) {
589 #include <perf/schedstat-v16.h>
590 		return size;
591 	}
592 #undef CPU_FIELD
593 
594 	return fprintf(fp, "Unsupported /proc/schedstat version %d.\n",
595 		       event->schedstat_cpu.version);
596 }
597 
598 size_t perf_event__fprintf_schedstat_domain(union perf_event *event, FILE *fp)
599 {
600 	struct perf_record_schedstat_domain *ds = &event->schedstat_domain;
601 	__u16 version = ds->version;
602 	size_t size = fprintf(fp, "\ndomain%u ", ds->domain);
603 
604 #define DOMAIN_FIELD(_type, _name, _desc, _format, _is_jiffies, _ver)		\
605 	size += fprintf(fp, "%" PRIu64 " ", (uint64_t)ds->_ver._name)
606 
607 	if (version == 15) {
608 #include <perf/schedstat-v15.h>
609 		return size;
610 	} else if (version == 16) {
611 #include <perf/schedstat-v16.h>
612 		return size;
613 	}
614 #undef DOMAIN_FIELD
615 
616 	return fprintf(fp, "Unsupported /proc/schedstat version %d.\n",
617 		       event->schedstat_domain.version);
618 }
619 
620 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp)
621 {
622 	size_t ret = fprintf(fp, "PERF_RECORD_%s",
623 			     perf_event__name(event->header.type));
624 
625 	switch (event->header.type) {
626 	case PERF_RECORD_COMM:
627 		ret += perf_event__fprintf_comm(event, fp);
628 		break;
629 	case PERF_RECORD_FORK:
630 	case PERF_RECORD_EXIT:
631 		ret += perf_event__fprintf_task(event, fp);
632 		break;
633 	case PERF_RECORD_MMAP:
634 		ret += perf_event__fprintf_mmap(event, fp);
635 		break;
636 	case PERF_RECORD_NAMESPACES:
637 		ret += perf_event__fprintf_namespaces(event, fp);
638 		break;
639 	case PERF_RECORD_CGROUP:
640 		ret += perf_event__fprintf_cgroup(event, fp);
641 		break;
642 	case PERF_RECORD_MMAP2:
643 		ret += perf_event__fprintf_mmap2(event, fp);
644 		break;
645 	case PERF_RECORD_AUX:
646 		ret += perf_event__fprintf_aux(event, fp);
647 		break;
648 	case PERF_RECORD_ITRACE_START:
649 		ret += perf_event__fprintf_itrace_start(event, fp);
650 		break;
651 	case PERF_RECORD_SWITCH:
652 	case PERF_RECORD_SWITCH_CPU_WIDE:
653 		ret += perf_event__fprintf_switch(event, fp);
654 		break;
655 	case PERF_RECORD_LOST:
656 		ret += perf_event__fprintf_lost(event, fp);
657 		break;
658 	case PERF_RECORD_KSYMBOL:
659 		ret += perf_event__fprintf_ksymbol(event, fp);
660 		break;
661 	case PERF_RECORD_BPF_EVENT:
662 		ret += perf_event__fprintf_bpf(event, fp);
663 		break;
664 	case PERF_RECORD_TEXT_POKE:
665 		ret += perf_event__fprintf_text_poke(event, machine, fp);
666 		break;
667 	case PERF_RECORD_AUX_OUTPUT_HW_ID:
668 		ret += perf_event__fprintf_aux_output_hw_id(event, fp);
669 		break;
670 	case PERF_RECORD_BPF_METADATA:
671 		ret += perf_event__fprintf_bpf_metadata(event, fp);
672 		break;
673 	default:
674 		ret += fprintf(fp, "\n");
675 	}
676 
677 	return ret;
678 }
679 
680 int perf_event__process(const struct perf_tool *tool __maybe_unused,
681 			union perf_event *event,
682 			struct perf_sample *sample,
683 			struct machine *machine)
684 {
685 	return machine__process_event(machine, event, sample);
686 }
687 
688 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
689 			     struct addr_location *al)
690 {
691 	struct maps *maps = thread__maps(thread);
692 	struct machine *machine = maps__machine(maps);
693 	bool load_map = false;
694 
695 	maps__zput(al->maps);
696 	map__zput(al->map);
697 	thread__zput(al->thread);
698 	al->thread = thread__get(thread);
699 
700 	al->addr = addr;
701 	al->cpumode = cpumode;
702 	al->filtered = 0;
703 
704 	if (machine == NULL)
705 		return NULL;
706 
707 	if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
708 		al->level = 'k';
709 		maps = machine__kernel_maps(machine);
710 		load_map = !symbol_conf.lazy_load_kernel_maps;
711 	} else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
712 		al->level = '.';
713 	} else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
714 		al->level = 'g';
715 		maps = machine__kernel_maps(machine);
716 		load_map = !symbol_conf.lazy_load_kernel_maps;
717 	} else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
718 		al->level = 'u';
719 	} else {
720 		al->level = 'H';
721 
722 		if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
723 			cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
724 			!perf_guest)
725 			al->filtered |= (1 << HIST_FILTER__GUEST);
726 		if ((cpumode == PERF_RECORD_MISC_USER ||
727 			cpumode == PERF_RECORD_MISC_KERNEL) &&
728 			!perf_host)
729 			al->filtered |= (1 << HIST_FILTER__HOST);
730 
731 		return NULL;
732 	}
733 	al->maps = maps__get(maps);
734 	al->map = maps__find(maps, al->addr);
735 	if (al->map != NULL) {
736 		/*
737 		 * Kernel maps might be changed when loading symbols so loading
738 		 * must be done prior to using kernel maps.
739 		 */
740 		if (load_map)
741 			map__load(al->map);
742 		al->addr = map__map_ip(al->map, al->addr);
743 	}
744 
745 	return al->map;
746 }
747 
748 /*
749  * For branch stacks or branch samples, the sample cpumode might not be correct
750  * because it applies only to the sample 'ip' and not necessary to 'addr' or
751  * branch stack addresses. If possible, use a fallback to deal with those cases.
752  */
753 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
754 				struct addr_location *al)
755 {
756 	struct map *map = thread__find_map(thread, cpumode, addr, al);
757 	struct machine *machine = maps__machine(thread__maps(thread));
758 	u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr);
759 
760 	if (map || addr_cpumode == cpumode)
761 		return map;
762 
763 	return thread__find_map(thread, addr_cpumode, addr, al);
764 }
765 
766 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
767 				   u64 addr, struct addr_location *al)
768 {
769 	al->sym = NULL;
770 	if (thread__find_map(thread, cpumode, addr, al))
771 		al->sym = map__find_symbol(al->map, al->addr);
772 	return al->sym;
773 }
774 
775 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
776 				      u64 addr, struct addr_location *al)
777 {
778 	al->sym = NULL;
779 	if (thread__find_map_fb(thread, cpumode, addr, al))
780 		al->sym = map__find_symbol(al->map, al->addr);
781 	return al->sym;
782 }
783 
784 static bool check_address_range(struct intlist *addr_list, int addr_range,
785 				unsigned long addr)
786 {
787 	struct int_node *pos;
788 
789 	intlist__for_each_entry(pos, addr_list) {
790 		if (addr >= pos->i && addr < pos->i + addr_range)
791 			return true;
792 	}
793 
794 	return false;
795 }
796 
797 /*
798  * Callers need to drop the reference to al->thread, obtained in
799  * machine__findnew_thread()
800  */
801 int machine__resolve(struct machine *machine, struct addr_location *al,
802 		     struct perf_sample *sample)
803 {
804 	struct thread *thread;
805 	struct dso *dso;
806 
807 	if (symbol_conf.guest_code && !machine__is_host(machine))
808 		thread = machine__findnew_guest_code(machine, sample->pid);
809 	else
810 		thread = machine__findnew_thread(machine, sample->pid, sample->tid);
811 	if (thread == NULL)
812 		return -1;
813 
814 	dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread__tid(thread));
815 	thread__find_map(thread, sample->cpumode, sample->ip, al);
816 	dso = al->map ? map__dso(al->map) : NULL;
817 	dump_printf(" ...... dso: %s\n",
818 		dso
819 		? dso__long_name(dso)
820 		: (al->level == 'H' ? "[hypervisor]" : "<not found>"));
821 
822 	if (thread__is_filtered(thread))
823 		al->filtered |= (1 << HIST_FILTER__THREAD);
824 
825 	thread__put(thread);
826 	thread = NULL;
827 
828 	al->sym = NULL;
829 	al->cpu = sample->cpu;
830 	al->socket = -1;
831 	al->srcline = NULL;
832 
833 	if (al->cpu >= 0) {
834 		struct perf_env *env = machine->env;
835 
836 		if (env && env->cpu)
837 			al->socket = env->cpu[al->cpu].socket_id;
838 	}
839 
840 	/* Account for possible out-of-order switch events. */
841 	al->parallelism = max(1, min(machine->parallelism, machine__nr_cpus_avail(machine)));
842 	if (test_bit(al->parallelism, symbol_conf.parallelism_filter))
843 		al->filtered |= (1 << HIST_FILTER__PARALLELISM);
844 	/*
845 	 * Multiply it by some const to avoid precision loss or dealing
846 	 * with floats. The multiplier does not matter otherwise since
847 	 * we only print it as percents.
848 	 */
849 	al->latency = sample->period * 1000 / al->parallelism;
850 
851 	if (al->map) {
852 		if (symbol_conf.dso_list &&
853 		    (!dso || !(strlist__has_entry(symbol_conf.dso_list,
854 						  dso__short_name(dso)) ||
855 			       (dso__short_name(dso) != dso__long_name(dso) &&
856 				strlist__has_entry(symbol_conf.dso_list,
857 						   dso__long_name(dso)))))) {
858 			al->filtered |= (1 << HIST_FILTER__DSO);
859 		}
860 
861 		al->sym = map__find_symbol(al->map, al->addr);
862 	} else if (symbol_conf.dso_list) {
863 		al->filtered |= (1 << HIST_FILTER__DSO);
864 	}
865 
866 	if (symbol_conf.sym_list) {
867 		int ret = 0;
868 		char al_addr_str[32];
869 		size_t sz = sizeof(al_addr_str);
870 
871 		if (al->sym) {
872 			ret = strlist__has_entry(symbol_conf.sym_list,
873 						al->sym->name);
874 		}
875 		if (!ret && al->sym) {
876 			snprintf(al_addr_str, sz, "0x%"PRIx64,
877 				 map__unmap_ip(al->map, al->sym->start));
878 			ret = strlist__has_entry(symbol_conf.sym_list,
879 						al_addr_str);
880 		}
881 		if (!ret && symbol_conf.addr_list && al->map) {
882 			unsigned long addr = map__unmap_ip(al->map, al->addr);
883 
884 			ret = intlist__has_entry(symbol_conf.addr_list, addr);
885 			if (!ret && symbol_conf.addr_range) {
886 				ret = check_address_range(symbol_conf.addr_list,
887 							  symbol_conf.addr_range,
888 							  addr);
889 			}
890 		}
891 
892 		if (!ret)
893 			al->filtered |= (1 << HIST_FILTER__SYMBOL);
894 	}
895 
896 	return 0;
897 }
898 
899 bool is_bts_event(struct perf_event_attr *attr)
900 {
901 	return attr->type == PERF_TYPE_HARDWARE &&
902 	       (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
903 	       attr->sample_period == 1;
904 }
905 
906 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
907 {
908 	if (attr->type == PERF_TYPE_SOFTWARE &&
909 	    (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
910 	     attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
911 	     attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
912 		return true;
913 
914 	if (is_bts_event(attr))
915 		return true;
916 
917 	return false;
918 }
919 
920 void thread__resolve(struct thread *thread, struct addr_location *al,
921 		     struct perf_sample *sample)
922 {
923 	thread__find_map_fb(thread, sample->cpumode, sample->addr, al);
924 
925 	al->cpu = sample->cpu;
926 	al->sym = NULL;
927 
928 	if (al->map)
929 		al->sym = map__find_symbol(al->map, al->addr);
930 }
931