xref: /linux/tools/perf/util/machine.c (revision 390d5ea26622f794c2d29cefd5a01ef116b4fe1d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <dirent.h>
3 #include <errno.h>
4 #include <inttypes.h>
5 #include <regex.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "callchain.h"
9 #include "debug.h"
10 #include "dso.h"
11 #include "env.h"
12 #include "event.h"
13 #include "evsel.h"
14 #include "hist.h"
15 #include "machine.h"
16 #include "map.h"
17 #include "map_symbol.h"
18 #include "branch.h"
19 #include "mem-events.h"
20 #include "mem-info.h"
21 #include "path.h"
22 #include "srcline.h"
23 #include "symbol.h"
24 #include "synthetic-events.h"
25 #include "sort.h"
26 #include "strlist.h"
27 #include "target.h"
28 #include "thread.h"
29 #include "util.h"
30 #include "vdso.h"
31 #include <stdbool.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include "unwind.h"
36 #include "linux/hash.h"
37 #include "asm/bug.h"
38 #include "bpf-event.h"
39 #include <internal/lib.h> // page_size
40 #include "cgroup.h"
41 #include "arm64-frame-pointer-unwind-support.h"
42 #include <api/io_dir.h>
43 
44 #include <linux/ctype.h>
45 #include <symbol/kallsyms.h>
46 #include <linux/mman.h>
47 #include <linux/string.h>
48 #include <linux/zalloc.h>
49 
50 static struct dso *machine__kernel_dso(struct machine *machine)
51 {
52 	return map__dso(machine->vmlinux_map);
53 }
54 
55 static int machine__set_mmap_name(struct machine *machine)
56 {
57 	if (machine__is_host(machine))
58 		machine->mmap_name = strdup("[kernel.kallsyms]");
59 	else if (machine__is_default_guest(machine))
60 		machine->mmap_name = strdup("[guest.kernel.kallsyms]");
61 	else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
62 			  machine->pid) < 0)
63 		machine->mmap_name = NULL;
64 
65 	return machine->mmap_name ? 0 : -ENOMEM;
66 }
67 
68 static void thread__set_guest_comm(struct thread *thread, pid_t pid)
69 {
70 	char comm[64];
71 
72 	snprintf(comm, sizeof(comm), "[guest/%d]", pid);
73 	thread__set_comm(thread, comm, 0);
74 }
75 
76 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
77 {
78 	int err = -ENOMEM;
79 
80 	memset(machine, 0, sizeof(*machine));
81 	machine->kmaps = maps__new(machine);
82 	if (machine->kmaps == NULL)
83 		return -ENOMEM;
84 
85 	RB_CLEAR_NODE(&machine->rb_node);
86 	dsos__init(&machine->dsos);
87 
88 	threads__init(&machine->threads);
89 
90 	machine->vdso_info = NULL;
91 	machine->env = NULL;
92 
93 	machine->pid = pid;
94 
95 	machine->id_hdr_size = 0;
96 	machine->kptr_restrict_warned = false;
97 	machine->comm_exec = false;
98 	machine->kernel_start = 0;
99 	machine->vmlinux_map = NULL;
100 	/* There is no initial context switch in, so we start at 1. */
101 	machine->parallelism = 1;
102 
103 	machine->root_dir = strdup(root_dir);
104 	if (machine->root_dir == NULL)
105 		goto out;
106 
107 	if (machine__set_mmap_name(machine))
108 		goto out;
109 
110 	if (pid != HOST_KERNEL_ID) {
111 		struct thread *thread = machine__findnew_thread(machine, -1,
112 								pid);
113 
114 		if (thread == NULL)
115 			goto out;
116 
117 		thread__set_guest_comm(thread, pid);
118 		thread__put(thread);
119 	}
120 
121 	machine->current_tid = NULL;
122 	err = 0;
123 
124 out:
125 	if (err) {
126 		zfree(&machine->kmaps);
127 		zfree(&machine->root_dir);
128 		zfree(&machine->mmap_name);
129 	}
130 	return 0;
131 }
132 
133 static struct machine *__machine__new_host(struct perf_env *host_env, bool kernel_maps)
134 {
135 	struct machine *machine = malloc(sizeof(*machine));
136 
137 	if (!machine)
138 		return NULL;
139 
140 	machine__init(machine, "", HOST_KERNEL_ID);
141 
142 	if (kernel_maps && machine__create_kernel_maps(machine) < 0) {
143 		free(machine);
144 		return NULL;
145 	}
146 	machine->env = host_env;
147 	return machine;
148 }
149 
150 struct machine *machine__new_host(struct perf_env *host_env)
151 {
152 	return __machine__new_host(host_env, /*kernel_maps=*/true);
153 }
154 
155 static int mmap_handler(const struct perf_tool *tool __maybe_unused,
156 			union perf_event *event,
157 			struct perf_sample *sample,
158 			struct machine *machine)
159 {
160 	return machine__process_mmap2_event(machine, event, sample);
161 }
162 
163 static int machine__init_live(struct machine *machine, pid_t pid)
164 {
165 	union perf_event event;
166 
167 	memset(&event, 0, sizeof(event));
168 	return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
169 						  mmap_handler, machine, true);
170 }
171 
172 struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid)
173 {
174 	struct machine *machine = __machine__new_host(host_env, kernel_maps);
175 
176 	if (!machine)
177 		return NULL;
178 
179 	if (machine__init_live(machine, pid)) {
180 		machine__delete(machine);
181 		return NULL;
182 	}
183 	return machine;
184 }
185 
186 struct machine *machine__new_kallsyms(struct perf_env *host_env)
187 {
188 	struct machine *machine = machine__new_host(host_env);
189 	/*
190 	 * FIXME:
191 	 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
192 	 *    ask for not using the kcore parsing code, once this one is fixed
193 	 *    to create a map per module.
194 	 */
195 	if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
196 		machine__delete(machine);
197 		machine = NULL;
198 	}
199 
200 	return machine;
201 }
202 
203 void machine__delete_threads(struct machine *machine)
204 {
205 	threads__remove_all_threads(&machine->threads);
206 }
207 
208 void machine__exit(struct machine *machine)
209 {
210 	if (machine == NULL)
211 		return;
212 
213 	machine__destroy_kernel_maps(machine);
214 	maps__zput(machine->kmaps);
215 	dsos__exit(&machine->dsos);
216 	machine__exit_vdso(machine);
217 	zfree(&machine->root_dir);
218 	zfree(&machine->mmap_name);
219 	zfree(&machine->current_tid);
220 	zfree(&machine->kallsyms_filename);
221 
222 	threads__exit(&machine->threads);
223 }
224 
225 void machine__delete(struct machine *machine)
226 {
227 	if (machine) {
228 		machine__exit(machine);
229 		free(machine);
230 	}
231 }
232 
233 void machines__init(struct machines *machines)
234 {
235 	machine__init(&machines->host, "", HOST_KERNEL_ID);
236 	machines->guests = RB_ROOT_CACHED;
237 }
238 
239 void machines__exit(struct machines *machines)
240 {
241 	machine__exit(&machines->host);
242 	/* XXX exit guest */
243 }
244 
245 struct machine *machines__add(struct machines *machines, pid_t pid,
246 			      const char *root_dir)
247 {
248 	struct rb_node **p = &machines->guests.rb_root.rb_node;
249 	struct rb_node *parent = NULL;
250 	struct machine *pos, *machine = malloc(sizeof(*machine));
251 	bool leftmost = true;
252 
253 	if (machine == NULL)
254 		return NULL;
255 
256 	if (machine__init(machine, root_dir, pid) != 0) {
257 		free(machine);
258 		return NULL;
259 	}
260 
261 	while (*p != NULL) {
262 		parent = *p;
263 		pos = rb_entry(parent, struct machine, rb_node);
264 		if (pid < pos->pid)
265 			p = &(*p)->rb_left;
266 		else {
267 			p = &(*p)->rb_right;
268 			leftmost = false;
269 		}
270 	}
271 
272 	rb_link_node(&machine->rb_node, parent, p);
273 	rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
274 
275 	machine->machines = machines;
276 
277 	return machine;
278 }
279 
280 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
281 {
282 	struct rb_node *nd;
283 
284 	machines->host.comm_exec = comm_exec;
285 
286 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
287 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
288 
289 		machine->comm_exec = comm_exec;
290 	}
291 }
292 
293 struct machine *machines__find(struct machines *machines, pid_t pid)
294 {
295 	struct rb_node **p = &machines->guests.rb_root.rb_node;
296 	struct rb_node *parent = NULL;
297 	struct machine *machine;
298 	struct machine *default_machine = NULL;
299 
300 	if (pid == HOST_KERNEL_ID)
301 		return &machines->host;
302 
303 	while (*p != NULL) {
304 		parent = *p;
305 		machine = rb_entry(parent, struct machine, rb_node);
306 		if (pid < machine->pid)
307 			p = &(*p)->rb_left;
308 		else if (pid > machine->pid)
309 			p = &(*p)->rb_right;
310 		else
311 			return machine;
312 		if (!machine->pid)
313 			default_machine = machine;
314 	}
315 
316 	return default_machine;
317 }
318 
319 struct machine *machines__findnew(struct machines *machines, pid_t pid)
320 {
321 	char path[PATH_MAX];
322 	const char *root_dir = "";
323 	struct machine *machine = machines__find(machines, pid);
324 
325 	if (machine && (machine->pid == pid))
326 		goto out;
327 
328 	if ((pid != HOST_KERNEL_ID) &&
329 	    (pid != DEFAULT_GUEST_KERNEL_ID) &&
330 	    (symbol_conf.guestmount)) {
331 		sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
332 		if (access(path, R_OK)) {
333 			static struct strlist *seen;
334 
335 			if (!seen)
336 				seen = strlist__new(NULL, NULL);
337 
338 			if (!strlist__has_entry(seen, path)) {
339 				pr_err("Can't access file %s\n", path);
340 				strlist__add(seen, path);
341 			}
342 			machine = NULL;
343 			goto out;
344 		}
345 		root_dir = path;
346 	}
347 
348 	machine = machines__add(machines, pid, root_dir);
349 out:
350 	return machine;
351 }
352 
353 struct machine *machines__find_guest(struct machines *machines, pid_t pid)
354 {
355 	struct machine *machine = machines__find(machines, pid);
356 
357 	if (!machine)
358 		machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
359 	return machine;
360 }
361 
362 /*
363  * A common case for KVM test programs is that the test program acts as the
364  * hypervisor, creating, running and destroying the virtual machine, and
365  * providing the guest object code from its own object code. In this case,
366  * the VM is not running an OS, but only the functions loaded into it by the
367  * hypervisor test program, and conveniently, loaded at the same virtual
368  * addresses.
369  *
370  * Normally to resolve addresses, MMAP events are needed to map addresses
371  * back to the object code and debug symbols for that object code.
372  *
373  * Currently, there is no way to get such mapping information from guests
374  * but, in the scenario described above, the guest has the same mappings
375  * as the hypervisor, so support for that scenario can be achieved.
376  *
377  * To support that, copy the host thread's maps to the guest thread's maps.
378  * Note, we do not discover the guest until we encounter a guest event,
379  * which works well because it is not until then that we know that the host
380  * thread's maps have been set up.
381  *
382  * This function returns the guest thread. Apart from keeping the data
383  * structures sane, using a thread belonging to the guest machine, instead
384  * of the host thread, allows it to have its own comm (refer
385  * thread__set_guest_comm()).
386  */
387 static struct thread *findnew_guest_code(struct machine *machine,
388 					 struct machine *host_machine,
389 					 pid_t pid)
390 {
391 	struct thread *host_thread;
392 	struct thread *thread;
393 	int err;
394 
395 	if (!machine)
396 		return NULL;
397 
398 	thread = machine__findnew_thread(machine, -1, pid);
399 	if (!thread)
400 		return NULL;
401 
402 	/* Assume maps are set up if there are any */
403 	if (!maps__empty(thread__maps(thread)))
404 		return thread;
405 
406 	host_thread = machine__find_thread(host_machine, -1, pid);
407 	if (!host_thread)
408 		goto out_err;
409 
410 	thread__set_guest_comm(thread, pid);
411 
412 	/*
413 	 * Guest code can be found in hypervisor process at the same address
414 	 * so copy host maps.
415 	 */
416 	err = maps__copy_from(thread__maps(thread), thread__maps(host_thread));
417 	thread__put(host_thread);
418 	if (err)
419 		goto out_err;
420 
421 	return thread;
422 
423 out_err:
424 	thread__zput(thread);
425 	return NULL;
426 }
427 
428 struct thread *machines__findnew_guest_code(struct machines *machines, pid_t pid)
429 {
430 	struct machine *host_machine = machines__find(machines, HOST_KERNEL_ID);
431 	struct machine *machine = machines__findnew(machines, pid);
432 
433 	return findnew_guest_code(machine, host_machine, pid);
434 }
435 
436 struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid)
437 {
438 	struct machines *machines = machine->machines;
439 	struct machine *host_machine;
440 
441 	if (!machines)
442 		return NULL;
443 
444 	host_machine = machines__find(machines, HOST_KERNEL_ID);
445 
446 	return findnew_guest_code(machine, host_machine, pid);
447 }
448 
449 void machines__process_guests(struct machines *machines,
450 			      machine__process_t process, void *data)
451 {
452 	struct rb_node *nd;
453 
454 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
455 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
456 		process(pos, data);
457 	}
458 }
459 
460 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
461 {
462 	struct rb_node *node;
463 	struct machine *machine;
464 
465 	machines->host.id_hdr_size = id_hdr_size;
466 
467 	for (node = rb_first_cached(&machines->guests); node;
468 	     node = rb_next(node)) {
469 		machine = rb_entry(node, struct machine, rb_node);
470 		machine->id_hdr_size = id_hdr_size;
471 	}
472 
473 	return;
474 }
475 
476 static void machine__update_thread_pid(struct machine *machine,
477 				       struct thread *th, pid_t pid)
478 {
479 	struct thread *leader;
480 
481 	if (pid == thread__pid(th) || pid == -1 || thread__pid(th) != -1)
482 		return;
483 
484 	thread__set_pid(th, pid);
485 
486 	if (thread__pid(th) == thread__tid(th))
487 		return;
488 
489 	leader = machine__findnew_thread(machine, thread__pid(th), thread__pid(th));
490 	if (!leader)
491 		goto out_err;
492 
493 	if (!thread__maps(leader))
494 		thread__set_maps(leader, maps__new(machine));
495 
496 	if (!thread__maps(leader))
497 		goto out_err;
498 
499 	if (thread__maps(th) == thread__maps(leader))
500 		goto out_put;
501 
502 	if (thread__maps(th)) {
503 		/*
504 		 * Maps are created from MMAP events which provide the pid and
505 		 * tid.  Consequently there never should be any maps on a thread
506 		 * with an unknown pid.  Just print an error if there are.
507 		 */
508 		if (!maps__empty(thread__maps(th)))
509 			pr_err("Discarding thread maps for %d:%d\n",
510 				thread__pid(th), thread__tid(th));
511 		maps__put(thread__maps(th));
512 	}
513 
514 	thread__set_maps(th, maps__get(thread__maps(leader)));
515 out_put:
516 	thread__put(leader);
517 	return;
518 out_err:
519 	pr_err("Failed to join map groups for %d:%d\n", thread__pid(th), thread__tid(th));
520 	goto out_put;
521 }
522 
523 /*
524  * Caller must eventually drop thread->refcnt returned with a successful
525  * lookup/new thread inserted.
526  */
527 static struct thread *__machine__findnew_thread(struct machine *machine,
528 						pid_t pid,
529 						pid_t tid,
530 						bool create)
531 {
532 	struct thread *th = threads__find(&machine->threads, tid);
533 	bool created;
534 
535 	if (th) {
536 		machine__update_thread_pid(machine, th, pid);
537 		return th;
538 	}
539 	if (!create)
540 		return NULL;
541 
542 	th = threads__findnew(&machine->threads, pid, tid, &created);
543 	if (created) {
544 		/*
545 		 * We have to initialize maps separately after rb tree is
546 		 * updated.
547 		 *
548 		 * The reason is that we call machine__findnew_thread within
549 		 * thread__init_maps to find the thread leader and that would
550 		 * screwed the rb tree.
551 		 */
552 		if (thread__init_maps(th, machine)) {
553 			pr_err("Thread init failed thread %d\n", pid);
554 			threads__remove(&machine->threads, th);
555 			thread__put(th);
556 			return NULL;
557 		}
558 	} else
559 		machine__update_thread_pid(machine, th, pid);
560 
561 	return th;
562 }
563 
564 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
565 {
566 	return __machine__findnew_thread(machine, pid, tid, /*create=*/true);
567 }
568 
569 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
570 				    pid_t tid)
571 {
572 	return __machine__findnew_thread(machine, pid, tid, /*create=*/false);
573 }
574 
575 /*
576  * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
577  * So here a single thread is created for that, but actually there is a separate
578  * idle task per cpu, so there should be one 'struct thread' per cpu, but there
579  * is only 1. That causes problems for some tools, requiring workarounds. For
580  * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
581  */
582 struct thread *machine__idle_thread(struct machine *machine)
583 {
584 	struct thread *thread = machine__findnew_thread(machine, 0, 0);
585 
586 	if (!thread || thread__set_comm(thread, "swapper", 0) ||
587 	    thread__set_namespaces(thread, 0, NULL))
588 		pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
589 
590 	return thread;
591 }
592 
593 struct comm *machine__thread_exec_comm(struct machine *machine,
594 				       struct thread *thread)
595 {
596 	if (machine->comm_exec)
597 		return thread__exec_comm(thread);
598 	else
599 		return thread__comm(thread);
600 }
601 
602 int machine__process_comm_event(struct machine *machine, union perf_event *event,
603 				struct perf_sample *sample)
604 {
605 	struct thread *thread = machine__findnew_thread(machine,
606 							event->comm.pid,
607 							event->comm.tid);
608 	bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
609 	int err = 0;
610 
611 	if (exec)
612 		machine->comm_exec = true;
613 
614 	if (dump_trace)
615 		perf_event__fprintf_comm(event, stdout);
616 
617 	if (thread == NULL ||
618 	    __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
619 		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
620 		err = -1;
621 	}
622 
623 	thread__put(thread);
624 
625 	return err;
626 }
627 
628 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
629 				      union perf_event *event,
630 				      struct perf_sample *sample __maybe_unused)
631 {
632 	struct thread *thread = machine__findnew_thread(machine,
633 							event->namespaces.pid,
634 							event->namespaces.tid);
635 	int err = 0;
636 
637 	WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
638 		  "\nWARNING: kernel seems to support more namespaces than perf"
639 		  " tool.\nTry updating the perf tool..\n\n");
640 
641 	WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
642 		  "\nWARNING: perf tool seems to support more namespaces than"
643 		  " the kernel.\nTry updating the kernel..\n\n");
644 
645 	if (dump_trace)
646 		perf_event__fprintf_namespaces(event, stdout);
647 
648 	if (thread == NULL ||
649 	    thread__set_namespaces(thread, sample->time, &event->namespaces)) {
650 		dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
651 		err = -1;
652 	}
653 
654 	thread__put(thread);
655 
656 	return err;
657 }
658 
659 int machine__process_cgroup_event(struct machine *machine,
660 				  union perf_event *event,
661 				  struct perf_sample *sample __maybe_unused)
662 {
663 	struct cgroup *cgrp;
664 
665 	if (dump_trace)
666 		perf_event__fprintf_cgroup(event, stdout);
667 
668 	cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
669 	if (cgrp == NULL)
670 		return -ENOMEM;
671 
672 	return 0;
673 }
674 
675 int machine__process_lost_event(struct machine *machine __maybe_unused,
676 				union perf_event *event, struct perf_sample *sample __maybe_unused)
677 {
678 	dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
679 		    event->lost.id, event->lost.lost);
680 	return 0;
681 }
682 
683 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
684 					union perf_event *event, struct perf_sample *sample)
685 {
686 	dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "%s\n",
687 		    sample->id, event->lost_samples.lost,
688 		    event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF ? " (BPF)" : "");
689 	return 0;
690 }
691 
692 int machine__process_aux_event(struct machine *machine __maybe_unused,
693 			       union perf_event *event)
694 {
695 	if (dump_trace)
696 		perf_event__fprintf_aux(event, stdout);
697 	return 0;
698 }
699 
700 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
701 					union perf_event *event)
702 {
703 	if (dump_trace)
704 		perf_event__fprintf_itrace_start(event, stdout);
705 	return 0;
706 }
707 
708 int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
709 					    union perf_event *event)
710 {
711 	if (dump_trace)
712 		perf_event__fprintf_aux_output_hw_id(event, stdout);
713 	return 0;
714 }
715 
716 int machine__process_switch_event(struct machine *machine __maybe_unused,
717 				  union perf_event *event)
718 {
719 	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
720 
721 	if (dump_trace)
722 		perf_event__fprintf_switch(event, stdout);
723 	machine->parallelism += out ? -1 : 1;
724 	return 0;
725 }
726 
727 static int machine__process_ksymbol_register(struct machine *machine,
728 					     union perf_event *event,
729 					     struct perf_sample *sample __maybe_unused)
730 {
731 	struct symbol *sym;
732 	struct dso *dso = NULL;
733 	struct map *map;
734 	int err = 0;
735 
736 	/* Ignore mapping symbols in ksymbol events - check early before any state mutation */
737 	if (is_ignored_kernel_symbol(event->ksymbol.name))
738 		return 0;
739 
740 	map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
741 
742 	if (!map) {
743 		dso = dso__new(event->ksymbol.name);
744 
745 		if (!dso) {
746 			err = -ENOMEM;
747 			goto out;
748 		}
749 		dso__set_kernel(dso, DSO_SPACE__KERNEL);
750 		map = map__new2(0, dso);
751 		if (!map) {
752 			err = -ENOMEM;
753 			goto out;
754 		}
755 		if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
756 			dso__set_binary_type(dso, DSO_BINARY_TYPE__OOL);
757 			dso__data(dso)->file_size = event->ksymbol.len;
758 			dso__set_loaded(dso);
759 		}
760 
761 		map__set_start(map, event->ksymbol.addr);
762 		map__set_end(map, map__start(map) + event->ksymbol.len);
763 		err = maps__fixup_overlap_and_insert(machine__kernel_maps(machine), map);
764 		if (err) {
765 			err = -ENOMEM;
766 			goto out;
767 		}
768 
769 		dso__set_loaded(dso);
770 
771 		if (is_bpf_image(event->ksymbol.name)) {
772 			dso__set_binary_type(dso, DSO_BINARY_TYPE__BPF_IMAGE);
773 			dso__set_long_name(dso, "", false);
774 		}
775 	} else {
776 		dso = dso__get(map__dso(map));
777 	}
778 
779 	sym = symbol__new(map__map_ip(map, map__start(map)),
780 			  event->ksymbol.len,
781 			  0, 0, event->ksymbol.name);
782 	if (!sym) {
783 		err = -ENOMEM;
784 		goto out;
785 	}
786 	dso__insert_symbol(dso, sym);
787 out:
788 	map__put(map);
789 	dso__put(dso);
790 	return err;
791 }
792 
793 static int machine__process_ksymbol_unregister(struct machine *machine,
794 					       union perf_event *event,
795 					       struct perf_sample *sample __maybe_unused)
796 {
797 	struct symbol *sym;
798 	struct map *map;
799 
800 	/* Ignore mapping symbols in ksymbol events */
801 	if (is_ignored_kernel_symbol(event->ksymbol.name))
802 		return 0;
803 
804 	map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
805 	if (!map)
806 		return 0;
807 
808 	if (!RC_CHK_EQUAL(map, machine->vmlinux_map))
809 		maps__remove(machine__kernel_maps(machine), map);
810 	else {
811 		struct dso *dso = map__dso(map);
812 
813 		sym = dso__find_symbol(dso, map__map_ip(map, map__start(map)));
814 		if (sym)
815 			dso__delete_symbol(dso, sym);
816 	}
817 	map__put(map);
818 	return 0;
819 }
820 
821 int machine__process_ksymbol(struct machine *machine __maybe_unused,
822 			     union perf_event *event,
823 			     struct perf_sample *sample)
824 {
825 	if (dump_trace)
826 		perf_event__fprintf_ksymbol(event, stdout);
827 
828 	if (event->header.size < offsetof(struct perf_record_ksymbol, name) + 2 ||
829 	    !memchr(event->ksymbol.name, '\0',
830 		    event->header.size - offsetof(struct perf_record_ksymbol, name)))
831 		return -EINVAL;
832 
833 	/* no need to process non-JIT BPF as it cannot get samples */
834 	if (event->ksymbol.len == 0)
835 		return 0;
836 
837 	if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
838 		return machine__process_ksymbol_unregister(machine, event,
839 							   sample);
840 	return machine__process_ksymbol_register(machine, event, sample);
841 }
842 
843 int machine__process_text_poke(struct machine *machine, union perf_event *event,
844 			       struct perf_sample *sample __maybe_unused)
845 {
846 	struct map *map = maps__find(machine__kernel_maps(machine), event->text_poke.addr);
847 	u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
848 	struct dso *dso = map ? map__dso(map) : NULL;
849 
850 	if (dump_trace)
851 		perf_event__fprintf_text_poke(event, machine, stdout);
852 
853 	if (!event->text_poke.new_len)
854 		goto out;
855 
856 	if (cpumode != PERF_RECORD_MISC_KERNEL) {
857 		pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
858 		goto out;
859 	}
860 
861 	if (dso) {
862 		u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
863 		int ret;
864 
865 		/*
866 		 * Kernel maps might be changed when loading symbols so loading
867 		 * must be done prior to using kernel maps.
868 		 */
869 		map__load(map);
870 		ret = dso__data_write_cache_addr(dso, map, machine,
871 						 event->text_poke.addr,
872 						 new_bytes,
873 						 event->text_poke.new_len);
874 		if (ret != event->text_poke.new_len)
875 			pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
876 				 event->text_poke.addr);
877 	} else {
878 		pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
879 			 event->text_poke.addr);
880 	}
881 out:
882 	map__put(map);
883 	return 0;
884 }
885 
886 static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
887 					      const char *filename)
888 {
889 	struct map *map = NULL;
890 	struct kmod_path m;
891 	struct dso *dso;
892 	int err;
893 
894 	if (kmod_path__parse_name(&m, filename))
895 		return NULL;
896 
897 	dso = dsos__findnew_module_dso(&machine->dsos, machine, &m, filename);
898 	if (dso == NULL)
899 		goto out;
900 
901 	map = map__new2(start, dso);
902 	if (map == NULL)
903 		goto out;
904 
905 	err = maps__insert(machine__kernel_maps(machine), map);
906 	/* If maps__insert failed, return NULL. */
907 	if (err) {
908 		map__put(map);
909 		map = NULL;
910 	}
911 out:
912 	/* put the dso here, corresponding to  machine__findnew_module_dso */
913 	dso__put(dso);
914 	zfree(&m.name);
915 	return map;
916 }
917 
918 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
919 {
920 	struct rb_node *nd;
921 	size_t ret = dsos__fprintf(&machines->host.dsos, fp);
922 
923 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
924 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
925 		ret += dsos__fprintf(&pos->dsos, fp);
926 	}
927 
928 	return ret;
929 }
930 
931 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
932 				     bool (skip)(struct dso *dso, int parm), int parm)
933 {
934 	return dsos__fprintf_buildid(&m->dsos, fp, skip, parm);
935 }
936 
937 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
938 				     bool (skip)(struct dso *dso, int parm), int parm)
939 {
940 	struct rb_node *nd;
941 	size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
942 
943 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
944 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
945 		ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
946 	}
947 	return ret;
948 }
949 
950 struct machine_fprintf_cb_args {
951 	FILE *fp;
952 	size_t printed;
953 };
954 
955 static int machine_fprintf_cb(struct thread *thread, void *data)
956 {
957 	struct machine_fprintf_cb_args *args = data;
958 
959 	/* TODO: handle fprintf errors. */
960 	args->printed += thread__fprintf(thread, args->fp);
961 	return 0;
962 }
963 
964 size_t machine__fprintf(struct machine *machine, FILE *fp)
965 {
966 	struct machine_fprintf_cb_args args = {
967 		.fp = fp,
968 		.printed = 0,
969 	};
970 	size_t ret = fprintf(fp, "Threads: %zu\n", threads__nr(&machine->threads));
971 
972 	machine__for_each_thread(machine, machine_fprintf_cb, &args);
973 	return ret + args.printed;
974 }
975 
976 static struct dso *machine__get_kernel(struct machine *machine)
977 {
978 	const char *vmlinux_name = machine->mmap_name;
979 	struct dso *kernel;
980 
981 	if (machine__is_host(machine)) {
982 		if (symbol_conf.vmlinux_name)
983 			vmlinux_name = symbol_conf.vmlinux_name;
984 
985 		kernel = machine__findnew_kernel(machine, vmlinux_name,
986 						 "[kernel]", DSO_SPACE__KERNEL);
987 	} else {
988 		if (symbol_conf.default_guest_vmlinux_name)
989 			vmlinux_name = symbol_conf.default_guest_vmlinux_name;
990 
991 		kernel = machine__findnew_kernel(machine, vmlinux_name,
992 						 "[guest.kernel]",
993 						 DSO_SPACE__KERNEL_GUEST);
994 	}
995 
996 	if (kernel != NULL && (!dso__has_build_id(kernel)))
997 		dso__read_running_kernel_build_id(kernel, machine);
998 
999 	return kernel;
1000 }
1001 
1002 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
1003 				    size_t bufsz)
1004 {
1005 	if (machine__is_default_guest(machine))
1006 		scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
1007 	else
1008 		scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
1009 }
1010 
1011 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
1012 
1013 /* Figure out the start address of kernel map from /proc/kallsyms.
1014  * Returns the name of the start symbol in *symbol_name. Pass in NULL as
1015  * symbol_name if it's not that important.
1016  */
1017 static int machine__get_running_kernel_start(struct machine *machine,
1018 					     const char **symbol_name,
1019 					     u64 *start, u64 *end)
1020 {
1021 	char filename[PATH_MAX];
1022 	int i, err = -1;
1023 	const char *name;
1024 	u64 addr = 0;
1025 
1026 	machine__get_kallsyms_filename(machine, filename, PATH_MAX);
1027 
1028 	if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1029 		return 0;
1030 
1031 	for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
1032 		err = kallsyms__get_function_start(filename, name, &addr);
1033 		if (!err)
1034 			break;
1035 	}
1036 
1037 	if (err)
1038 		return -1;
1039 
1040 	if (symbol_name)
1041 		*symbol_name = name;
1042 
1043 	*start = addr;
1044 
1045 	err = kallsyms__get_symbol_start(filename, "_edata", &addr);
1046 	if (err)
1047 		err = kallsyms__get_symbol_start(filename, "_etext", &addr);
1048 	if (!err)
1049 		*end = addr;
1050 
1051 	return 0;
1052 }
1053 
1054 int machine__create_extra_kernel_map(struct machine *machine,
1055 				     struct dso *kernel,
1056 				     struct extra_kernel_map *xm)
1057 {
1058 	struct kmap *kmap;
1059 	struct map *map;
1060 	int err;
1061 
1062 	map = map__new2(xm->start, kernel);
1063 	if (!map)
1064 		return -ENOMEM;
1065 
1066 	map__set_end(map, xm->end);
1067 	map__set_pgoff(map, xm->pgoff);
1068 
1069 	kmap = map__kmap(map);
1070 
1071 	strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
1072 
1073 	err = maps__insert(machine__kernel_maps(machine), map);
1074 
1075 	if (!err) {
1076 		pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1077 			kmap->name, map__start(map), map__end(map));
1078 	}
1079 
1080 	map__put(map);
1081 
1082 	return err;
1083 }
1084 
1085 static u64 find_entry_trampoline(struct dso *dso)
1086 {
1087 	/* Duplicates are removed so lookup all aliases */
1088 	const char *syms[] = {
1089 		"_entry_trampoline",
1090 		"__entry_trampoline_start",
1091 		"entry_SYSCALL_64_trampoline",
1092 	};
1093 	struct symbol *sym = dso__first_symbol(dso);
1094 	unsigned int i;
1095 
1096 	for (; sym; sym = dso__next_symbol(sym)) {
1097 		if (sym->binding != STB_GLOBAL)
1098 			continue;
1099 		for (i = 0; i < ARRAY_SIZE(syms); i++) {
1100 			if (!strcmp(sym->name, syms[i]))
1101 				return sym->start;
1102 		}
1103 	}
1104 
1105 	return 0;
1106 }
1107 
1108 /*
1109  * These values can be used for kernels that do not have symbols for the entry
1110  * trampolines in kallsyms.
1111  */
1112 #define X86_64_CPU_ENTRY_AREA_PER_CPU	0xfffffe0000000000ULL
1113 #define X86_64_CPU_ENTRY_AREA_SIZE	0x2c000
1114 #define X86_64_ENTRY_TRAMPOLINE		0x6000
1115 
1116 struct machine__map_x86_64_entry_trampolines_args {
1117 	struct maps *kmaps;
1118 	bool found;
1119 };
1120 
1121 static int machine__map_x86_64_entry_trampolines_cb(struct map *map, void *data)
1122 {
1123 	struct machine__map_x86_64_entry_trampolines_args *args = data;
1124 	struct map *dest_map;
1125 	struct kmap *kmap = __map__kmap(map);
1126 
1127 	if (!kmap || !is_entry_trampoline(kmap->name))
1128 		return 0;
1129 
1130 	dest_map = maps__find(args->kmaps, map__pgoff(map));
1131 	if (RC_CHK_ACCESS(dest_map) != RC_CHK_ACCESS(map))
1132 		map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map)));
1133 
1134 	map__put(dest_map);
1135 	args->found = true;
1136 	return 0;
1137 }
1138 
1139 /* Map x86_64 PTI entry trampolines */
1140 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1141 					  struct dso *kernel)
1142 {
1143 	struct machine__map_x86_64_entry_trampolines_args args = {
1144 		.kmaps = machine__kernel_maps(machine),
1145 		.found = false,
1146 	};
1147 	int nr_cpus_avail, cpu;
1148 	u64 pgoff;
1149 
1150 	/*
1151 	 * In the vmlinux case, pgoff is a virtual address which must now be
1152 	 * mapped to a vmlinux offset.
1153 	 */
1154 	maps__for_each_map(args.kmaps, machine__map_x86_64_entry_trampolines_cb, &args);
1155 
1156 	if (args.found || machine->trampolines_mapped)
1157 		return 0;
1158 
1159 	pgoff = find_entry_trampoline(kernel);
1160 	if (!pgoff)
1161 		return 0;
1162 
1163 	nr_cpus_avail = machine__nr_cpus_avail(machine);
1164 
1165 	/* Add a 1 page map for each CPU's entry trampoline */
1166 	for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1167 		u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1168 			 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1169 			 X86_64_ENTRY_TRAMPOLINE;
1170 		struct extra_kernel_map xm = {
1171 			.start = va,
1172 			.end   = va + page_size,
1173 			.pgoff = pgoff,
1174 		};
1175 
1176 		strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1177 
1178 		if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1179 			return -1;
1180 	}
1181 
1182 	machine->trampolines_mapped = nr_cpus_avail;
1183 
1184 	return 0;
1185 }
1186 
1187 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1188 					     struct dso *kernel __maybe_unused)
1189 {
1190 	return 0;
1191 }
1192 
1193 static int
1194 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1195 {
1196 	/* In case of renewal the kernel map, destroy previous one */
1197 	machine__destroy_kernel_maps(machine);
1198 
1199 	map__put(machine->vmlinux_map);
1200 	machine->vmlinux_map = map__new2(0, kernel);
1201 	if (machine->vmlinux_map == NULL)
1202 		return -ENOMEM;
1203 
1204 	map__set_mapping_type(machine->vmlinux_map, MAPPING_TYPE__IDENTITY);
1205 	return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
1206 }
1207 
1208 void machine__destroy_kernel_maps(struct machine *machine)
1209 {
1210 	struct kmap *kmap;
1211 	struct map *map = machine__kernel_map(machine);
1212 
1213 	if (map == NULL)
1214 		return;
1215 
1216 	kmap = map__kmap(map);
1217 	maps__remove(machine__kernel_maps(machine), map);
1218 	if (kmap && kmap->ref_reloc_sym) {
1219 		zfree((char **)&kmap->ref_reloc_sym->name);
1220 		zfree(&kmap->ref_reloc_sym);
1221 	}
1222 
1223 	map__zput(machine->vmlinux_map);
1224 }
1225 
1226 int machines__create_guest_kernel_maps(struct machines *machines)
1227 {
1228 	int ret = 0;
1229 	struct dirent **namelist = NULL;
1230 	int i, items = 0;
1231 	char path[PATH_MAX];
1232 	pid_t pid;
1233 	char *endp;
1234 
1235 	if (symbol_conf.default_guest_vmlinux_name ||
1236 	    symbol_conf.default_guest_modules ||
1237 	    symbol_conf.default_guest_kallsyms) {
1238 		machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1239 	}
1240 
1241 	if (symbol_conf.guestmount) {
1242 		items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1243 		if (items <= 0)
1244 			return -ENOENT;
1245 		for (i = 0; i < items; i++) {
1246 			if (!isdigit(namelist[i]->d_name[0])) {
1247 				/* Filter out . and .. */
1248 				continue;
1249 			}
1250 			pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1251 			if ((*endp != '\0') ||
1252 			    (endp == namelist[i]->d_name) ||
1253 			    (errno == ERANGE)) {
1254 				pr_debug("invalid directory (%s). Skipping.\n",
1255 					 namelist[i]->d_name);
1256 				continue;
1257 			}
1258 			sprintf(path, "%s/%s/proc/kallsyms",
1259 				symbol_conf.guestmount,
1260 				namelist[i]->d_name);
1261 			ret = access(path, R_OK);
1262 			if (ret) {
1263 				pr_debug("Can't access file %s\n", path);
1264 				goto failure;
1265 			}
1266 			machines__create_kernel_maps(machines, pid);
1267 		}
1268 failure:
1269 		free(namelist);
1270 	}
1271 
1272 	return ret;
1273 }
1274 
1275 void machines__destroy_kernel_maps(struct machines *machines)
1276 {
1277 	struct rb_node *next = rb_first_cached(&machines->guests);
1278 
1279 	machine__destroy_kernel_maps(&machines->host);
1280 
1281 	while (next) {
1282 		struct machine *pos = rb_entry(next, struct machine, rb_node);
1283 
1284 		next = rb_next(&pos->rb_node);
1285 		rb_erase_cached(&pos->rb_node, &machines->guests);
1286 		machine__delete(pos);
1287 	}
1288 }
1289 
1290 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1291 {
1292 	struct machine *machine = machines__findnew(machines, pid);
1293 
1294 	if (machine == NULL)
1295 		return -1;
1296 
1297 	return machine__create_kernel_maps(machine);
1298 }
1299 
1300 int machine__load_kallsyms(struct machine *machine, const char *filename)
1301 {
1302 	struct map *map = machine__kernel_map(machine);
1303 	struct dso *dso = map__dso(map);
1304 	int ret = __dso__load_kallsyms(dso, filename, map, true);
1305 
1306 	if (ret > 0) {
1307 		dso__set_loaded(dso);
1308 		/*
1309 		 * Since /proc/kallsyms will have multiple sessions for the
1310 		 * kernel, with modules between them, fixup the end of all
1311 		 * sections.
1312 		 */
1313 		maps__fixup_end(machine__kernel_maps(machine));
1314 	}
1315 
1316 	return ret;
1317 }
1318 
1319 int machine__load_vmlinux_path(struct machine *machine)
1320 {
1321 	struct map *map = machine__kernel_map(machine);
1322 	struct dso *dso = map__dso(map);
1323 	int ret = dso__load_vmlinux_path(dso, map);
1324 
1325 	if (ret > 0)
1326 		dso__set_loaded(dso);
1327 
1328 	return ret;
1329 }
1330 
1331 static char *get_kernel_version(const char *root_dir)
1332 {
1333 	char version[PATH_MAX];
1334 	FILE *file;
1335 	char *name, *tmp;
1336 	const char *prefix = "Linux version ";
1337 
1338 	sprintf(version, "%s/proc/version", root_dir);
1339 	file = fopen(version, "r");
1340 	if (!file)
1341 		return NULL;
1342 
1343 	tmp = fgets(version, sizeof(version), file);
1344 	fclose(file);
1345 	if (!tmp)
1346 		return NULL;
1347 
1348 	name = strstr(version, prefix);
1349 	if (!name)
1350 		return NULL;
1351 	name += strlen(prefix);
1352 	tmp = strchr(name, ' ');
1353 	if (tmp)
1354 		*tmp = '\0';
1355 
1356 	return strdup(name);
1357 }
1358 
1359 static bool is_kmod_dso(struct dso *dso)
1360 {
1361 	return dso__symtab_type(dso) == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1362 	       dso__symtab_type(dso) == DSO_BINARY_TYPE__GUEST_KMODULE;
1363 }
1364 
1365 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1366 {
1367 	char *long_name;
1368 	struct dso *dso;
1369 	struct map *map = maps__find_by_name(maps, m->name);
1370 
1371 	if (map == NULL)
1372 		return 0;
1373 
1374 	long_name = strdup(path);
1375 	if (long_name == NULL) {
1376 		map__put(map);
1377 		return -ENOMEM;
1378 	}
1379 
1380 	dso = map__dso(map);
1381 	dso__set_long_name(dso, long_name, true);
1382 	dso__kernel_module_get_build_id(dso, "");
1383 
1384 	/*
1385 	 * Full name could reveal us kmod compression, so
1386 	 * we need to update the symtab_type if needed.
1387 	 */
1388 	if (m->comp && is_kmod_dso(dso)) {
1389 		dso__set_symtab_type(dso, dso__symtab_type(dso)+1);
1390 		dso__set_comp(dso, m->comp);
1391 	}
1392 	map__put(map);
1393 	return 0;
1394 }
1395 
1396 static int maps__set_modules_path_dir(struct maps *maps, char *path, size_t path_size, int depth)
1397 {
1398 	struct io_dirent64 *dent;
1399 	struct io_dir iod;
1400 	size_t root_len = strlen(path);
1401 	int ret = 0;
1402 
1403 	io_dir__init(&iod, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY));
1404 	if (iod.dirfd < 0) {
1405 		pr_debug("%s: cannot open %s dir\n", __func__, path);
1406 		return -1;
1407 	}
1408 	/* Bounds check, should never happen. */
1409 	if (root_len >= path_size)
1410 		return -1;
1411 	path[root_len++] = '/';
1412 	while ((dent = io_dir__readdir(&iod)) != NULL) {
1413 		if (io_dir__is_dir(&iod, dent)) {
1414 			if (!strcmp(dent->d_name, ".") ||
1415 			    !strcmp(dent->d_name, ".."))
1416 				continue;
1417 
1418 			/* Do not follow top-level source and build symlinks */
1419 			if (depth == 0) {
1420 				if (!strcmp(dent->d_name, "source") ||
1421 				    !strcmp(dent->d_name, "build"))
1422 					continue;
1423 			}
1424 
1425 			/* Bounds check, should never happen. */
1426 			if (root_len + strlen(dent->d_name) >= path_size)
1427 				continue;
1428 
1429 			strcpy(path + root_len, dent->d_name);
1430 			ret = maps__set_modules_path_dir(maps, path, path_size, depth + 1);
1431 			if (ret < 0)
1432 				goto out;
1433 		} else {
1434 			struct kmod_path m;
1435 
1436 			ret = kmod_path__parse_name(&m, dent->d_name);
1437 			if (ret)
1438 				goto out;
1439 
1440 			if (m.kmod) {
1441 				/* Bounds check, should never happen. */
1442 				if (root_len + strlen(dent->d_name) < path_size) {
1443 					strcpy(path + root_len, dent->d_name);
1444 					ret = maps__set_module_path(maps, path, &m);
1445 
1446 				}
1447 			}
1448 			zfree(&m.name);
1449 
1450 			if (ret)
1451 				goto out;
1452 		}
1453 	}
1454 
1455 out:
1456 	close(iod.dirfd);
1457 	return ret;
1458 }
1459 
1460 static int machine__set_modules_path(struct machine *machine)
1461 {
1462 	char *version;
1463 	char modules_path[PATH_MAX];
1464 
1465 	version = get_kernel_version(machine->root_dir);
1466 	if (!version)
1467 		return -1;
1468 
1469 	snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1470 		 machine->root_dir, version);
1471 	free(version);
1472 
1473 	return maps__set_modules_path_dir(machine__kernel_maps(machine),
1474 					  modules_path, sizeof(modules_path), 0);
1475 }
1476 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1477 				u64 *size __maybe_unused,
1478 				const char *name __maybe_unused)
1479 {
1480 	return 0;
1481 }
1482 
1483 static int machine__create_module(void *arg, const char *name, u64 start,
1484 				  u64 size)
1485 {
1486 	struct machine *machine = arg;
1487 	struct map *map;
1488 
1489 	if (arch__fix_module_text_start(&start, &size, name) < 0)
1490 		return -1;
1491 
1492 	map = machine__addnew_module_map(machine, start, name);
1493 	if (map == NULL)
1494 		return -1;
1495 	map__set_end(map, start + size);
1496 
1497 	dso__kernel_module_get_build_id(map__dso(map), machine->root_dir);
1498 	map__put(map);
1499 	return 0;
1500 }
1501 
1502 static int machine__create_modules(struct machine *machine)
1503 {
1504 	const char *modules;
1505 	char path[PATH_MAX];
1506 
1507 	if (machine__is_default_guest(machine)) {
1508 		modules = symbol_conf.default_guest_modules;
1509 	} else {
1510 		snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1511 		modules = path;
1512 	}
1513 
1514 	if (symbol__restricted_filename(modules, "/proc/modules"))
1515 		return -1;
1516 
1517 	if (modules__parse(modules, machine, machine__create_module))
1518 		return -1;
1519 
1520 	if (!machine__set_modules_path(machine))
1521 		return 0;
1522 
1523 	pr_debug("Problems setting modules path maps, continuing anyway...\n");
1524 
1525 	return 0;
1526 }
1527 
1528 static void machine__set_kernel_mmap(struct machine *machine,
1529 				     u64 start, u64 end)
1530 {
1531 	map__set_start(machine->vmlinux_map, start);
1532 	map__set_end(machine->vmlinux_map, end);
1533 	/*
1534 	 * Be a bit paranoid here, some perf.data file came with
1535 	 * a zero sized synthesized MMAP event for the kernel.
1536 	 */
1537 	if (start == 0 && end == 0)
1538 		map__set_end(machine->vmlinux_map, ~0ULL);
1539 }
1540 
1541 static int machine__update_kernel_mmap(struct machine *machine,
1542 				     u64 start, u64 end)
1543 {
1544 	struct map *orig, *updated;
1545 	int err;
1546 
1547 	orig = machine->vmlinux_map;
1548 	updated = map__get(orig);
1549 
1550 	machine->vmlinux_map = updated;
1551 	maps__remove(machine__kernel_maps(machine), orig);
1552 	machine__set_kernel_mmap(machine, start, end);
1553 	err = maps__insert(machine__kernel_maps(machine), updated);
1554 	map__put(orig);
1555 
1556 	return err;
1557 }
1558 
1559 int machine__create_kernel_maps(struct machine *machine)
1560 {
1561 	struct dso *kernel = machine__get_kernel(machine);
1562 	const char *name = NULL;
1563 	u64 start = 0, end = ~0ULL;
1564 	int ret;
1565 
1566 	if (kernel == NULL)
1567 		return -1;
1568 
1569 	ret = __machine__create_kernel_maps(machine, kernel);
1570 	if (ret < 0)
1571 		goto out_put;
1572 
1573 	if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1574 		if (machine__is_host(machine))
1575 			pr_debug("Problems creating module maps, "
1576 				 "continuing anyway...\n");
1577 		else
1578 			pr_debug("Problems creating module maps for guest %d, "
1579 				 "continuing anyway...\n", machine->pid);
1580 	}
1581 
1582 	if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1583 		if (name &&
1584 		    map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1585 			machine__destroy_kernel_maps(machine);
1586 			ret = -1;
1587 			goto out_put;
1588 		}
1589 
1590 		/*
1591 		 * we have a real start address now, so re-order the kmaps
1592 		 * assume it's the last in the kmaps
1593 		 */
1594 		ret = machine__update_kernel_mmap(machine, start, end);
1595 		if (ret < 0)
1596 			goto out_put;
1597 	}
1598 
1599 	if (machine__create_extra_kernel_maps(machine, kernel))
1600 		pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1601 
1602 	if (end == ~0ULL) {
1603 		/* update end address of the kernel map using adjacent module address */
1604 		struct map *next = maps__find_next_entry(machine__kernel_maps(machine),
1605 							 machine__kernel_map(machine));
1606 
1607 		if (next) {
1608 			machine__set_kernel_mmap(machine, start, map__start(next));
1609 			map__put(next);
1610 		}
1611 	}
1612 
1613 	maps__fixup_end(machine__kernel_maps(machine));
1614 
1615 out_put:
1616 	dso__put(kernel);
1617 	return ret;
1618 }
1619 
1620 static int machine__uses_kcore_cb(struct dso *dso, void *data __maybe_unused)
1621 {
1622 	return dso__is_kcore(dso) ? 1 : 0;
1623 }
1624 
1625 static bool machine__uses_kcore(struct machine *machine)
1626 {
1627 	return dsos__for_each_dso(&machine->dsos, machine__uses_kcore_cb, NULL) != 0 ? true : false;
1628 }
1629 
1630 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1631 					     struct extra_kernel_map *xm)
1632 {
1633 	return machine__is(machine, "x86_64") &&
1634 	       is_entry_trampoline(xm->name);
1635 }
1636 
1637 static int machine__process_extra_kernel_map(struct machine *machine,
1638 					     struct extra_kernel_map *xm)
1639 {
1640 	struct dso *kernel = machine__kernel_dso(machine);
1641 
1642 	if (kernel == NULL)
1643 		return -1;
1644 
1645 	return machine__create_extra_kernel_map(machine, kernel, xm);
1646 }
1647 
1648 static int machine__process_kernel_mmap_event(struct machine *machine,
1649 					      struct extra_kernel_map *xm,
1650 					      struct build_id *bid)
1651 {
1652 	enum dso_space_type dso_space;
1653 	bool is_kernel_mmap;
1654 	const char *mmap_name = machine->mmap_name;
1655 
1656 	/* If we have maps from kcore then we do not need or want any others */
1657 	if (machine__uses_kcore(machine))
1658 		return 0;
1659 
1660 	if (machine__is_host(machine))
1661 		dso_space = DSO_SPACE__KERNEL;
1662 	else
1663 		dso_space = DSO_SPACE__KERNEL_GUEST;
1664 
1665 	is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0;
1666 	if (!is_kernel_mmap && !machine__is_host(machine)) {
1667 		/*
1668 		 * If the event was recorded inside the guest and injected into
1669 		 * the host perf.data file, then it will match a host mmap_name,
1670 		 * so try that - see machine__set_mmap_name().
1671 		 */
1672 		mmap_name = "[kernel.kallsyms]";
1673 		is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0;
1674 	}
1675 	if (xm->name[0] == '/' ||
1676 	    (!is_kernel_mmap && xm->name[0] == '[')) {
1677 		struct map *map = machine__addnew_module_map(machine, xm->start, xm->name);
1678 
1679 		if (map == NULL)
1680 			goto out_problem;
1681 
1682 		map__set_end(map, map__start(map) + xm->end - xm->start);
1683 
1684 		if (build_id__is_defined(bid))
1685 			dso__set_build_id(map__dso(map), bid);
1686 
1687 		map__put(map);
1688 	} else if (is_kernel_mmap) {
1689 		const char *symbol_name = xm->name + strlen(mmap_name);
1690 		/*
1691 		 * Should be there already, from the build-id table in
1692 		 * the header.
1693 		 */
1694 		struct dso *kernel = dsos__find_kernel_dso(&machine->dsos);
1695 
1696 		if (kernel == NULL)
1697 			kernel = machine__findnew_dso(machine, machine->mmap_name);
1698 		if (kernel == NULL)
1699 			goto out_problem;
1700 
1701 		dso__set_kernel(kernel, dso_space);
1702 		if (__machine__create_kernel_maps(machine, kernel) < 0) {
1703 			dso__put(kernel);
1704 			goto out_problem;
1705 		}
1706 
1707 		if (strstr(dso__long_name(kernel), "vmlinux"))
1708 			dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1709 
1710 		if (machine__update_kernel_mmap(machine, xm->start, xm->end) < 0) {
1711 			dso__put(kernel);
1712 			goto out_problem;
1713 		}
1714 
1715 		if (build_id__is_defined(bid))
1716 			dso__set_build_id(kernel, bid);
1717 
1718 		/*
1719 		 * Avoid using a zero address (kptr_restrict) for the ref reloc
1720 		 * symbol. Effectively having zero here means that at record
1721 		 * time /proc/sys/kernel/kptr_restrict was non zero.
1722 		 */
1723 		if (xm->pgoff != 0) {
1724 			map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1725 							symbol_name,
1726 							xm->pgoff);
1727 		}
1728 
1729 		if (machine__is_default_guest(machine)) {
1730 			/*
1731 			 * preload dso of guest kernel and modules
1732 			 */
1733 			dso__load(kernel, machine__kernel_map(machine));
1734 		}
1735 		dso__put(kernel);
1736 	} else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
1737 		return machine__process_extra_kernel_map(machine, xm);
1738 	}
1739 	return 0;
1740 out_problem:
1741 	return -1;
1742 }
1743 
1744 int machine__process_mmap2_event(struct machine *machine,
1745 				 union perf_event *event,
1746 				 struct perf_sample *sample)
1747 {
1748 	struct thread *thread;
1749 	struct map *map;
1750 	struct dso_id dso_id = dso_id_empty;
1751 	int ret = 0;
1752 
1753 	if (dump_trace)
1754 		perf_event__fprintf_mmap2(event, stdout);
1755 
1756 	if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
1757 		build_id__init(&dso_id.build_id, event->mmap2.build_id, event->mmap2.build_id_size);
1758 	} else {
1759 		dso_id.maj = event->mmap2.maj;
1760 		dso_id.min = event->mmap2.min;
1761 		dso_id.ino = event->mmap2.ino;
1762 		dso_id.ino_generation = event->mmap2.ino_generation;
1763 		dso_id.mmap2_valid = true;
1764 		dso_id.mmap2_ino_generation_valid = true;
1765 	}
1766 
1767 	if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1768 	    sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1769 		struct extra_kernel_map xm = {
1770 			.start = event->mmap2.start,
1771 			.end   = event->mmap2.start + event->mmap2.len,
1772 			.pgoff = event->mmap2.pgoff,
1773 		};
1774 
1775 		strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
1776 		ret = machine__process_kernel_mmap_event(machine, &xm, &dso_id.build_id);
1777 		if (ret < 0)
1778 			goto out_problem;
1779 		return 0;
1780 	}
1781 
1782 	thread = machine__findnew_thread(machine, event->mmap2.pid,
1783 					event->mmap2.tid);
1784 	if (thread == NULL)
1785 		goto out_problem;
1786 
1787 	map = map__new(machine, event->mmap2.start,
1788 			event->mmap2.len, event->mmap2.pgoff,
1789 			&dso_id, event->mmap2.prot,
1790 			event->mmap2.flags,
1791 			event->mmap2.filename, thread);
1792 
1793 	if (map == NULL)
1794 		goto out_problem_map;
1795 
1796 	ret = thread__insert_map(thread, map);
1797 	if (ret)
1798 		goto out_problem_insert;
1799 
1800 	thread__put(thread);
1801 	map__put(map);
1802 	return 0;
1803 
1804 out_problem_insert:
1805 	map__put(map);
1806 out_problem_map:
1807 	thread__put(thread);
1808 out_problem:
1809 	dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1810 	return 0;
1811 }
1812 
1813 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1814 				struct perf_sample *sample)
1815 {
1816 	struct thread *thread;
1817 	struct map *map;
1818 	u32 prot = 0;
1819 	int ret = 0;
1820 
1821 	if (dump_trace)
1822 		perf_event__fprintf_mmap(event, stdout);
1823 
1824 	if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1825 	    sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1826 		struct extra_kernel_map xm = {
1827 			.start = event->mmap.start,
1828 			.end   = event->mmap.start + event->mmap.len,
1829 			.pgoff = event->mmap.pgoff,
1830 		};
1831 
1832 		strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1833 		ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
1834 		if (ret < 0)
1835 			goto out_problem;
1836 		return 0;
1837 	}
1838 
1839 	thread = machine__findnew_thread(machine, event->mmap.pid,
1840 					 event->mmap.tid);
1841 	if (thread == NULL)
1842 		goto out_problem;
1843 
1844 	if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1845 		prot = PROT_EXEC;
1846 
1847 	map = map__new(machine, event->mmap.start,
1848 		       event->mmap.len, event->mmap.pgoff,
1849 		       &dso_id_empty, prot, /*flags=*/0, event->mmap.filename, thread);
1850 
1851 	if (map == NULL)
1852 		goto out_problem_map;
1853 
1854 	ret = thread__insert_map(thread, map);
1855 	if (ret)
1856 		goto out_problem_insert;
1857 
1858 	thread__put(thread);
1859 	map__put(map);
1860 	return 0;
1861 
1862 out_problem_insert:
1863 	map__put(map);
1864 out_problem_map:
1865 	thread__put(thread);
1866 out_problem:
1867 	dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1868 	return 0;
1869 }
1870 
1871 void machine__remove_thread(struct machine *machine, struct thread *th)
1872 {
1873 	return threads__remove(&machine->threads, th);
1874 }
1875 
1876 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1877 				struct perf_sample *sample)
1878 {
1879 	struct thread *thread = machine__find_thread(machine,
1880 						     event->fork.pid,
1881 						     event->fork.tid);
1882 	struct thread *parent = machine__findnew_thread(machine,
1883 							event->fork.ppid,
1884 							event->fork.ptid);
1885 	bool do_maps_clone = true;
1886 	int err = 0;
1887 
1888 	if (dump_trace)
1889 		perf_event__fprintf_task(event, stdout);
1890 
1891 	/*
1892 	 * There may be an existing thread that is not actually the parent,
1893 	 * either because we are processing events out of order, or because the
1894 	 * (fork) event that would have removed the thread was lost. Assume the
1895 	 * latter case and continue on as best we can.
1896 	 */
1897 	if (thread__pid(parent) != (pid_t)event->fork.ppid) {
1898 		dump_printf("removing erroneous parent thread %d/%d\n",
1899 			    thread__pid(parent), thread__tid(parent));
1900 		machine__remove_thread(machine, parent);
1901 		thread__put(parent);
1902 		parent = machine__findnew_thread(machine, event->fork.ppid,
1903 						 event->fork.ptid);
1904 	}
1905 
1906 	/* if a thread currently exists for the thread id remove it */
1907 	if (thread != NULL) {
1908 		machine__remove_thread(machine, thread);
1909 		thread__put(thread);
1910 	}
1911 
1912 	thread = machine__findnew_thread(machine, event->fork.pid,
1913 					 event->fork.tid);
1914 	/*
1915 	 * When synthesizing FORK events, we are trying to create thread
1916 	 * objects for the already running tasks on the machine.
1917 	 *
1918 	 * Normally, for a kernel FORK event, we want to clone the parent's
1919 	 * maps because that is what the kernel just did.
1920 	 *
1921 	 * But when synthesizing, this should not be done.  If we do, we end up
1922 	 * with overlapping maps as we process the synthesized MMAP2 events that
1923 	 * get delivered shortly thereafter.
1924 	 *
1925 	 * Use the FORK event misc flags in an internal way to signal this
1926 	 * situation, so we can elide the map clone when appropriate.
1927 	 */
1928 	if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1929 		do_maps_clone = false;
1930 
1931 	if (thread == NULL || parent == NULL ||
1932 	    thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
1933 		dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1934 		err = -1;
1935 	}
1936 	thread__put(thread);
1937 	thread__put(parent);
1938 
1939 	return err;
1940 }
1941 
1942 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1943 				struct perf_sample *sample __maybe_unused)
1944 {
1945 	struct thread *thread = machine__find_thread(machine,
1946 						     event->fork.pid,
1947 						     event->fork.tid);
1948 
1949 	if (dump_trace)
1950 		perf_event__fprintf_task(event, stdout);
1951 
1952 	/* There is no context switch out before exit, so we decrement here. */
1953 	machine->parallelism--;
1954 	if (thread != NULL) {
1955 		if (symbol_conf.keep_exited_threads)
1956 			thread__set_exited(thread, /*exited=*/true);
1957 		else
1958 			machine__remove_thread(machine, thread);
1959 	}
1960 	thread__put(thread);
1961 	return 0;
1962 }
1963 
1964 int machine__process_event(struct machine *machine, union perf_event *event,
1965 			   struct perf_sample *sample)
1966 {
1967 	int ret;
1968 
1969 	switch (event->header.type) {
1970 	case PERF_RECORD_COMM:
1971 		ret = machine__process_comm_event(machine, event, sample); break;
1972 	case PERF_RECORD_MMAP:
1973 		ret = machine__process_mmap_event(machine, event, sample); break;
1974 	case PERF_RECORD_NAMESPACES:
1975 		ret = machine__process_namespaces_event(machine, event, sample); break;
1976 	case PERF_RECORD_CGROUP:
1977 		ret = machine__process_cgroup_event(machine, event, sample); break;
1978 	case PERF_RECORD_MMAP2:
1979 		ret = machine__process_mmap2_event(machine, event, sample); break;
1980 	case PERF_RECORD_FORK:
1981 		ret = machine__process_fork_event(machine, event, sample); break;
1982 	case PERF_RECORD_EXIT:
1983 		ret = machine__process_exit_event(machine, event, sample); break;
1984 	case PERF_RECORD_LOST:
1985 		ret = machine__process_lost_event(machine, event, sample); break;
1986 	case PERF_RECORD_AUX:
1987 		ret = machine__process_aux_event(machine, event); break;
1988 	case PERF_RECORD_ITRACE_START:
1989 		ret = machine__process_itrace_start_event(machine, event); break;
1990 	case PERF_RECORD_LOST_SAMPLES:
1991 		ret = machine__process_lost_samples_event(machine, event, sample); break;
1992 	case PERF_RECORD_SWITCH:
1993 	case PERF_RECORD_SWITCH_CPU_WIDE:
1994 		ret = machine__process_switch_event(machine, event); break;
1995 	case PERF_RECORD_KSYMBOL:
1996 		ret = machine__process_ksymbol(machine, event, sample); break;
1997 	case PERF_RECORD_BPF_EVENT:
1998 		ret = machine__process_bpf(machine, event, sample); break;
1999 	case PERF_RECORD_TEXT_POKE:
2000 		ret = machine__process_text_poke(machine, event, sample); break;
2001 	case PERF_RECORD_AUX_OUTPUT_HW_ID:
2002 		ret = machine__process_aux_output_hw_id_event(machine, event); break;
2003 	default:
2004 		ret = -1;
2005 		break;
2006 	}
2007 
2008 	return ret;
2009 }
2010 
2011 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
2012 {
2013 	return regexec(regex, sym->name, 0, NULL, 0) == 0;
2014 }
2015 
2016 static void ip__resolve_ams(struct thread *thread,
2017 			    struct addr_map_symbol *ams,
2018 			    u64 ip)
2019 {
2020 	struct addr_location al;
2021 
2022 	addr_location__init(&al);
2023 	/*
2024 	 * We cannot use the header.misc hint to determine whether a
2025 	 * branch stack address is user, kernel, guest, hypervisor.
2026 	 * Branches may straddle the kernel/user/hypervisor boundaries.
2027 	 * Thus, we have to try consecutively until we find a match
2028 	 * or else, the symbol is unknown
2029 	 */
2030 	thread__find_cpumode_addr_location(thread, ip, /*symbols=*/true, &al);
2031 
2032 	ams->addr = ip;
2033 	ams->al_addr = al.addr;
2034 	ams->al_level = al.level;
2035 	ams->ms.thread = thread__get(al.thread);
2036 	ams->ms.sym = al.sym;
2037 	ams->ms.map = map__get(al.map);
2038 	ams->phys_addr = 0;
2039 	ams->data_page_size = 0;
2040 	addr_location__exit(&al);
2041 }
2042 
2043 static void ip__resolve_data(struct thread *thread,
2044 			     u8 m, struct addr_map_symbol *ams,
2045 			     u64 addr, u64 phys_addr, u64 daddr_page_size)
2046 {
2047 	struct addr_location al;
2048 
2049 	addr_location__init(&al);
2050 
2051 	thread__find_symbol(thread, m, addr, &al);
2052 
2053 	ams->addr = addr;
2054 	ams->al_addr = al.addr;
2055 	ams->al_level = al.level;
2056 	ams->ms.thread = thread__get(al.thread);
2057 	ams->ms.sym = al.sym;
2058 	ams->ms.map = map__get(al.map);
2059 	ams->phys_addr = phys_addr;
2060 	ams->data_page_size = daddr_page_size;
2061 	addr_location__exit(&al);
2062 }
2063 
2064 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
2065 				     struct addr_location *al)
2066 {
2067 	struct mem_info *mi = mem_info__new();
2068 
2069 	if (!mi)
2070 		return NULL;
2071 
2072 	ip__resolve_ams(al->thread, mem_info__iaddr(mi), sample->ip);
2073 	ip__resolve_data(al->thread, al->cpumode, mem_info__daddr(mi),
2074 			 sample->addr, sample->phys_addr,
2075 			 sample->data_page_size);
2076 	mem_info__data_src(mi)->val = sample->data_src;
2077 
2078 	return mi;
2079 }
2080 
2081 static char *callchain_srcline(struct map_symbol *ms, u64 ip)
2082 {
2083 	struct map *map = ms->map;
2084 	char *srcline = NULL;
2085 	struct dso *dso;
2086 
2087 	if (!map || callchain_param.key == CCKEY_FUNCTION)
2088 		return srcline;
2089 
2090 	dso = map__dso(map);
2091 	srcline = srcline__tree_find(dso__srclines(dso), ip);
2092 	if (!srcline) {
2093 		bool show_sym = false;
2094 		bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2095 
2096 		srcline = get_srcline(dso, map__rip_2objdump(map, ip),
2097 				      ms->sym, show_sym, show_addr, ip);
2098 		srcline__tree_insert(dso__srclines(dso), ip, srcline);
2099 	}
2100 
2101 	return srcline;
2102 }
2103 
2104 struct iterations {
2105 	int nr_loop_iter;
2106 	u64 cycles;
2107 };
2108 
2109 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip,
2110 			bool branch, struct branch_flags *flags, int nr_loop_iter,
2111 			u64 iter_cycles, u64 branch_from)
2112 {
2113 	struct symbol *sym = ms->sym;
2114 	struct map *map = ms->map;
2115 	struct inline_node *inline_node;
2116 	struct inline_list *ilist;
2117 	struct dso *dso;
2118 	u64 addr;
2119 	int ret = 1;
2120 	struct map_symbol ilist_ms;
2121 	bool first = true;
2122 
2123 	if (!symbol_conf.inline_name || !map || !sym)
2124 		return ret;
2125 
2126 	addr = map__dso_map_ip(map, ip);
2127 	addr = map__rip_2objdump(map, addr);
2128 	dso = map__dso(map);
2129 
2130 	inline_node = inlines__tree_find(dso__inlined_nodes(dso), addr);
2131 	if (!inline_node) {
2132 		inline_node = dso__parse_addr_inlines(dso, addr, sym);
2133 		if (!inline_node)
2134 			return ret;
2135 		inlines__tree_insert(dso__inlined_nodes(dso), inline_node);
2136 	}
2137 
2138 	ilist_ms = (struct map_symbol) {
2139 		.thread = thread__get(ms->thread),
2140 		.map = map__get(map),
2141 	};
2142 	list_for_each_entry(ilist, &inline_node->val, list) {
2143 		ilist_ms.sym = ilist->symbol;
2144 		if (first) {
2145 			ret = callchain_cursor_append(cursor, ip, &ilist_ms,
2146 						      branch, flags, nr_loop_iter,
2147 						      iter_cycles, branch_from, ilist->srcline);
2148 		} else {
2149 			ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
2150 						      NULL, 0, 0, 0, ilist->srcline);
2151 		}
2152 		first = false;
2153 
2154 		if (ret != 0)
2155 			return ret;
2156 	}
2157 	map_symbol__exit(&ilist_ms);
2158 
2159 	return ret;
2160 }
2161 
2162 static int add_callchain_ip(struct thread *thread,
2163 			    struct callchain_cursor *cursor,
2164 			    struct symbol **parent,
2165 			    struct addr_location *root_al,
2166 			    u8 *cpumode,
2167 			    u64 ip,
2168 			    bool branch,
2169 			    struct branch_flags *flags,
2170 			    struct iterations *iter,
2171 			    u64 branch_from,
2172 			    bool symbols)
2173 {
2174 	struct map_symbol ms = {};
2175 	struct addr_location al;
2176 	int nr_loop_iter = 0, err = 0;
2177 	u64 iter_cycles = 0;
2178 	const char *srcline = NULL;
2179 
2180 	addr_location__init(&al);
2181 	al.filtered = 0;
2182 	al.sym = NULL;
2183 	al.srcline = NULL;
2184 	if (!cpumode) {
2185 		thread__find_cpumode_addr_location(thread, ip, symbols, &al);
2186 	} else {
2187 		if (ip >= PERF_CONTEXT_MAX) {
2188 			switch (ip) {
2189 			case PERF_CONTEXT_HV:
2190 				*cpumode = PERF_RECORD_MISC_HYPERVISOR;
2191 				break;
2192 			case PERF_CONTEXT_KERNEL:
2193 				*cpumode = PERF_RECORD_MISC_KERNEL;
2194 				break;
2195 			case PERF_CONTEXT_USER:
2196 			case PERF_CONTEXT_USER_DEFERRED:
2197 				*cpumode = PERF_RECORD_MISC_USER;
2198 				break;
2199 			default:
2200 				pr_debug("invalid callchain context: "
2201 					 "%"PRId64"\n", (s64) ip);
2202 				/*
2203 				 * It seems the callchain is corrupted.
2204 				 * Discard all.
2205 				 */
2206 				callchain_cursor_reset(cursor);
2207 				err = 1;
2208 				goto out;
2209 			}
2210 			goto out;
2211 		}
2212 		if (symbols)
2213 			thread__find_symbol(thread, *cpumode, ip, &al);
2214 		else
2215 			thread__find_map(thread, *cpumode, ip, &al);
2216 	}
2217 
2218 	if (al.sym != NULL) {
2219 		if (perf_hpp_list.parent && !*parent &&
2220 		    symbol__match_regex(al.sym, &parent_regex))
2221 			*parent = al.sym;
2222 		else if (have_ignore_callees && root_al &&
2223 		  symbol__match_regex(al.sym, &ignore_callees_regex)) {
2224 			/* Treat this symbol as the root,
2225 			   forgetting its callees. */
2226 			addr_location__copy(root_al, &al);
2227 			callchain_cursor_reset(cursor);
2228 		}
2229 	}
2230 
2231 	if (symbol_conf.hide_unresolved && al.sym == NULL)
2232 		goto out;
2233 
2234 	if (iter) {
2235 		nr_loop_iter = iter->nr_loop_iter;
2236 		iter_cycles = iter->cycles;
2237 	}
2238 
2239 	ms.thread = thread__get(al.thread);
2240 	ms.map = map__get(al.map);
2241 	ms.sym = al.sym;
2242 
2243 	if (append_inlines(cursor, &ms, ip, branch, flags, nr_loop_iter,
2244 			   iter_cycles, branch_from) == 0)
2245 		goto out;
2246 
2247 	srcline = callchain_srcline(&ms, al.addr);
2248 	err = callchain_cursor_append(cursor, ip, &ms,
2249 				      branch, flags, nr_loop_iter,
2250 				      iter_cycles, branch_from, srcline);
2251 out:
2252 	addr_location__exit(&al);
2253 	map_symbol__exit(&ms);
2254 	return err;
2255 }
2256 
2257 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2258 					   struct addr_location *al)
2259 {
2260 	unsigned int i;
2261 	const struct branch_stack *bs = sample->branch_stack;
2262 	struct branch_entry *entries = perf_sample__branch_entries(sample);
2263 	u64 *branch_stack_cntr = sample->branch_stack_cntr;
2264 	struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2265 
2266 	if (!bi)
2267 		return NULL;
2268 
2269 	for (i = 0; i < bs->nr; i++) {
2270 		ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2271 		ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2272 		bi[i].flags = entries[i].flags;
2273 		if (branch_stack_cntr)
2274 			bi[i].branch_stack_cntr  = branch_stack_cntr[i];
2275 	}
2276 	return bi;
2277 }
2278 
2279 static void save_iterations(struct iterations *iter,
2280 			    struct branch_entry *be, int nr)
2281 {
2282 	int i;
2283 
2284 	iter->nr_loop_iter++;
2285 	iter->cycles = 0;
2286 
2287 	for (i = 0; i < nr; i++)
2288 		iter->cycles += be[i].flags.cycles;
2289 }
2290 
2291 #define CHASHSZ 127
2292 #define CHASHBITS 7
2293 #define NO_ENTRY 0xff
2294 
2295 #define PERF_MAX_BRANCH_DEPTH 127
2296 
2297 /* Remove loops. */
2298 static int remove_loops(struct branch_entry *l, int nr,
2299 			struct iterations *iter)
2300 {
2301 	int i, j, off;
2302 	unsigned char chash[CHASHSZ];
2303 
2304 	memset(chash, NO_ENTRY, sizeof(chash));
2305 
2306 	BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2307 
2308 	for (i = 0; i < nr; i++) {
2309 		int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2310 
2311 		/* no collision handling for now */
2312 		if (chash[h] == NO_ENTRY) {
2313 			chash[h] = i;
2314 		} else if (l[chash[h]].from == l[i].from) {
2315 			bool is_loop = true;
2316 			/* check if it is a real loop */
2317 			off = 0;
2318 			for (j = chash[h]; j < i && i + off < nr; j++, off++)
2319 				if (l[j].from != l[i + off].from) {
2320 					is_loop = false;
2321 					break;
2322 				}
2323 			if (is_loop) {
2324 				j = nr - (i + off);
2325 				if (j > 0) {
2326 					save_iterations(iter + i + off,
2327 						l + i, off);
2328 
2329 					memmove(iter + i, iter + i + off,
2330 						j * sizeof(*iter));
2331 
2332 					memmove(l + i, l + i + off,
2333 						j * sizeof(*l));
2334 				}
2335 
2336 				nr -= off;
2337 			}
2338 		}
2339 	}
2340 	return nr;
2341 }
2342 
2343 static int lbr_callchain_add_kernel_ip(struct thread *thread,
2344 				       struct callchain_cursor *cursor,
2345 				       struct perf_sample *sample,
2346 				       struct symbol **parent,
2347 				       struct addr_location *root_al,
2348 				       u64 branch_from,
2349 				       bool callee, int end,
2350 				       bool symbols)
2351 {
2352 	struct ip_callchain *chain = sample->callchain;
2353 	u8 cpumode = PERF_RECORD_MISC_USER;
2354 	int err, i;
2355 
2356 	if (callee) {
2357 		for (i = 0; i < end + 1; i++) {
2358 			err = add_callchain_ip(thread, cursor, parent,
2359 					       root_al, &cpumode, chain->ips[i],
2360 					       false, NULL, NULL, branch_from,
2361 					       symbols);
2362 			if (err)
2363 				return err;
2364 		}
2365 		return 0;
2366 	}
2367 
2368 	for (i = end; i >= 0; i--) {
2369 		err = add_callchain_ip(thread, cursor, parent,
2370 				       root_al, &cpumode, chain->ips[i],
2371 				       false, NULL, NULL, branch_from,
2372 				       symbols);
2373 		if (err)
2374 			return err;
2375 	}
2376 
2377 	return 0;
2378 }
2379 
2380 static void save_lbr_cursor_node(struct thread *thread,
2381 				 struct callchain_cursor *cursor,
2382 				 int idx)
2383 {
2384 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
2385 
2386 	if (!lbr_stitch)
2387 		return;
2388 
2389 	if (cursor->pos == cursor->nr) {
2390 		lbr_stitch->prev_lbr_cursor[idx].valid = false;
2391 		return;
2392 	}
2393 
2394 	if (!cursor->curr)
2395 		cursor->curr = cursor->first;
2396 	else
2397 		cursor->curr = cursor->curr->next;
2398 
2399 	map_symbol__exit(&lbr_stitch->prev_lbr_cursor[idx].ms);
2400 	memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
2401 	       sizeof(struct callchain_cursor_node));
2402 	lbr_stitch->prev_lbr_cursor[idx].ms.thread = thread__get(cursor->curr->ms.thread);
2403 	lbr_stitch->prev_lbr_cursor[idx].ms.map = map__get(cursor->curr->ms.map);
2404 
2405 	lbr_stitch->prev_lbr_cursor[idx].valid = true;
2406 	cursor->pos++;
2407 }
2408 
2409 static int lbr_callchain_add_lbr_ip(struct thread *thread,
2410 				    struct callchain_cursor *cursor,
2411 				    struct perf_sample *sample,
2412 				    struct symbol **parent,
2413 				    struct addr_location *root_al,
2414 				    u64 *branch_from,
2415 				    bool callee,
2416 				    bool symbols)
2417 {
2418 	struct branch_stack *lbr_stack = sample->branch_stack;
2419 	struct branch_entry *entries = perf_sample__branch_entries(sample);
2420 	u8 cpumode = PERF_RECORD_MISC_USER;
2421 	int lbr_nr = lbr_stack->nr;
2422 	struct branch_flags *flags;
2423 	int err, i;
2424 	u64 ip;
2425 
2426 	/*
2427 	 * The curr and pos are not used in writing session. They are cleared
2428 	 * in callchain_cursor_commit() when the writing session is closed.
2429 	 * Using curr and pos to track the current cursor node.
2430 	 */
2431 	if (thread__lbr_stitch(thread)) {
2432 		cursor->curr = NULL;
2433 		cursor->pos = cursor->nr;
2434 		if (cursor->nr) {
2435 			cursor->curr = cursor->first;
2436 			for (i = 0; i < (int)(cursor->nr - 1); i++)
2437 				cursor->curr = cursor->curr->next;
2438 		}
2439 	}
2440 
2441 	if (callee) {
2442 		/*
2443 		 * Set the (first) leaf function's IP to sample->ip (the
2444 		 * location of the sample) but if not recorded use entries.to
2445 		 */
2446 		if (sample->ip)
2447 			ip = sample->ip;
2448 		else
2449 			ip = entries[0].to;
2450 		flags = &entries[0].flags;
2451 		*branch_from = entries[0].from;
2452 		err = add_callchain_ip(thread, cursor, parent,
2453 				       root_al, &cpumode, ip,
2454 				       true, flags, NULL,
2455 				       *branch_from, symbols);
2456 		if (err)
2457 			return err;
2458 
2459 		/*
2460 		 * The number of cursor node increases.
2461 		 * Move the current cursor node.
2462 		 * But does not need to save current cursor node for entry 0.
2463 		 * It's impossible to stitch the whole LBRs of previous sample.
2464 		 */
2465 		if (thread__lbr_stitch(thread) && (cursor->pos != cursor->nr)) {
2466 			if (!cursor->curr)
2467 				cursor->curr = cursor->first;
2468 			else
2469 				cursor->curr = cursor->curr->next;
2470 			cursor->pos++;
2471 		}
2472 
2473 		/* Add LBR ip from entries.from one by one. */
2474 		for (i = 0; i < lbr_nr; i++) {
2475 			ip = entries[i].from;
2476 			flags = &entries[i].flags;
2477 			err = add_callchain_ip(thread, cursor, parent,
2478 					       root_al, &cpumode, ip,
2479 					       true, flags, NULL,
2480 					       *branch_from, symbols);
2481 			if (err)
2482 				return err;
2483 			save_lbr_cursor_node(thread, cursor, i);
2484 		}
2485 		return 0;
2486 	}
2487 
2488 	/* Add LBR ip from entries.from one by one. */
2489 	for (i = lbr_nr - 1; i >= 0; i--) {
2490 		ip = entries[i].from;
2491 		flags = &entries[i].flags;
2492 		err = add_callchain_ip(thread, cursor, parent,
2493 				       root_al, &cpumode, ip,
2494 				       true, flags, NULL,
2495 				       *branch_from, symbols);
2496 		if (err)
2497 			return err;
2498 		save_lbr_cursor_node(thread, cursor, i);
2499 	}
2500 
2501 	if (lbr_nr > 0) {
2502 		/*
2503 		 * Set the (first) leaf function's IP to sample->ip (the
2504 		 * location of the sample) but if not recorded use entries.to
2505 		 */
2506 		if (sample->ip)
2507 			ip = sample->ip;
2508 		else
2509 			ip = entries[0].to;
2510 		flags = &entries[0].flags;
2511 		*branch_from = entries[0].from;
2512 		err = add_callchain_ip(thread, cursor, parent,
2513 				root_al, &cpumode, ip,
2514 				true, flags, NULL,
2515 				*branch_from, symbols);
2516 		if (err)
2517 			return err;
2518 	}
2519 
2520 	return 0;
2521 }
2522 
2523 static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
2524 					     struct callchain_cursor *cursor)
2525 {
2526 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
2527 	struct callchain_cursor_node *cnode;
2528 	struct stitch_list *stitch_node;
2529 	int err;
2530 
2531 	list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
2532 		cnode = &stitch_node->cursor;
2533 
2534 		err = callchain_cursor_append(cursor, cnode->ip,
2535 					      &cnode->ms,
2536 					      cnode->branch,
2537 					      &cnode->branch_flags,
2538 					      cnode->nr_loop_iter,
2539 					      cnode->iter_cycles,
2540 					      cnode->branch_from,
2541 					      cnode->srcline);
2542 		if (err)
2543 			return err;
2544 	}
2545 	return 0;
2546 }
2547 
2548 static struct stitch_list *get_stitch_node(struct thread *thread)
2549 {
2550 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
2551 	struct stitch_list *stitch_node;
2552 
2553 	if (!list_empty(&lbr_stitch->free_lists)) {
2554 		stitch_node = list_first_entry(&lbr_stitch->free_lists,
2555 					       struct stitch_list, node);
2556 		list_del(&stitch_node->node);
2557 
2558 		return stitch_node;
2559 	}
2560 
2561 	return malloc(sizeof(struct stitch_list));
2562 }
2563 
2564 static bool has_stitched_lbr(struct thread *thread,
2565 			     struct perf_sample *cur,
2566 			     struct perf_sample *prev,
2567 			     unsigned int max_lbr,
2568 			     bool callee)
2569 {
2570 	struct branch_stack *cur_stack = cur->branch_stack;
2571 	struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
2572 	struct branch_stack *prev_stack = prev->branch_stack;
2573 	struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
2574 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
2575 	int i, j, nr_identical_branches = 0;
2576 	struct stitch_list *stitch_node;
2577 	u64 cur_base, distance;
2578 
2579 	if (!cur_stack || !prev_stack)
2580 		return false;
2581 
2582 	/* Find the physical index of the base-of-stack for current sample. */
2583 	cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
2584 
2585 	distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
2586 						     (max_lbr + prev_stack->hw_idx - cur_base);
2587 	/* Previous sample has shorter stack. Nothing can be stitched. */
2588 	if (distance + 1 > prev_stack->nr)
2589 		return false;
2590 
2591 	/*
2592 	 * Check if there are identical LBRs between two samples.
2593 	 * Identical LBRs must have same from, to and flags values. Also,
2594 	 * they have to be saved in the same LBR registers (same physical
2595 	 * index).
2596 	 *
2597 	 * Starts from the base-of-stack of current sample.
2598 	 */
2599 	for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
2600 		if ((prev_entries[i].from != cur_entries[j].from) ||
2601 		    (prev_entries[i].to != cur_entries[j].to) ||
2602 		    (prev_entries[i].flags.value != cur_entries[j].flags.value))
2603 			break;
2604 		nr_identical_branches++;
2605 	}
2606 
2607 	if (!nr_identical_branches)
2608 		return false;
2609 
2610 	/*
2611 	 * Save the LBRs between the base-of-stack of previous sample
2612 	 * and the base-of-stack of current sample into lbr_stitch->lists.
2613 	 * These LBRs will be stitched later.
2614 	 */
2615 	for (i = prev_stack->nr - 1; i > (int)distance; i--) {
2616 
2617 		if (!lbr_stitch->prev_lbr_cursor[i].valid)
2618 			continue;
2619 
2620 		stitch_node = get_stitch_node(thread);
2621 		if (!stitch_node)
2622 			return false;
2623 
2624 		memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
2625 		       sizeof(struct callchain_cursor_node));
2626 
2627 		stitch_node->cursor.ms.thread =
2628 			thread__get(lbr_stitch->prev_lbr_cursor[i].ms.thread);
2629 		stitch_node->cursor.ms.map = map__get(lbr_stitch->prev_lbr_cursor[i].ms.map);
2630 
2631 		if (callee)
2632 			list_add(&stitch_node->node, &lbr_stitch->lists);
2633 		else
2634 			list_add_tail(&stitch_node->node, &lbr_stitch->lists);
2635 	}
2636 
2637 	return true;
2638 }
2639 
2640 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
2641 {
2642 	if (thread__lbr_stitch(thread))
2643 		return true;
2644 
2645 	thread__set_lbr_stitch(thread, zalloc(sizeof(struct lbr_stitch)));
2646 	if (!thread__lbr_stitch(thread))
2647 		goto err;
2648 
2649 	thread__lbr_stitch(thread)->prev_lbr_cursor =
2650 		calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
2651 	if (!thread__lbr_stitch(thread)->prev_lbr_cursor)
2652 		goto free_lbr_stitch;
2653 
2654 	thread__lbr_stitch(thread)->prev_lbr_cursor_size = max_lbr + 1;
2655 
2656 	INIT_LIST_HEAD(&thread__lbr_stitch(thread)->lists);
2657 	INIT_LIST_HEAD(&thread__lbr_stitch(thread)->free_lists);
2658 
2659 	return true;
2660 
2661 free_lbr_stitch:
2662 	free(thread__lbr_stitch(thread));
2663 	thread__set_lbr_stitch(thread, NULL);
2664 err:
2665 	pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
2666 	thread__set_lbr_stitch_enable(thread, false);
2667 	return false;
2668 }
2669 
2670 /*
2671  * Resolve LBR callstack chain sample
2672  * Return:
2673  * 1 on success get LBR callchain information
2674  * 0 no available LBR callchain information, should try fp
2675  * negative error code on other errors.
2676  */
2677 static int resolve_lbr_callchain_sample(struct thread *thread,
2678 					struct callchain_cursor *cursor,
2679 					struct perf_sample *sample,
2680 					struct symbol **parent,
2681 					struct addr_location *root_al,
2682 					int max_stack,
2683 					unsigned int max_lbr,
2684 					bool symbols)
2685 {
2686 	bool callee = (callchain_param.order == ORDER_CALLEE);
2687 	struct ip_callchain *chain = sample->callchain;
2688 	int chain_nr = min(max_stack, (int)chain->nr), i;
2689 	struct lbr_stitch *lbr_stitch;
2690 	bool stitched_lbr = false;
2691 	u64 branch_from = 0;
2692 	int err;
2693 
2694 	for (i = 0; i < chain_nr; i++) {
2695 		if (chain->ips[i] == PERF_CONTEXT_USER)
2696 			break;
2697 	}
2698 
2699 	/* LBR only affects the user callchain */
2700 	if (i == chain_nr)
2701 		return 0;
2702 
2703 	if (thread__lbr_stitch_enable(thread) && !sample->no_hw_idx &&
2704 	    (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
2705 		lbr_stitch = thread__lbr_stitch(thread);
2706 
2707 		stitched_lbr = has_stitched_lbr(thread, sample,
2708 						&lbr_stitch->prev_sample,
2709 						max_lbr, callee);
2710 
2711 		if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2712 			struct stitch_list *stitch_node;
2713 
2714 			list_for_each_entry(stitch_node, &lbr_stitch->lists, node)
2715 				map_symbol__exit(&stitch_node->cursor.ms);
2716 
2717 			list_splice_init(&lbr_stitch->lists, &lbr_stitch->free_lists);
2718 		}
2719 		memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
2720 	}
2721 
2722 	if (callee) {
2723 		/* Add kernel ip */
2724 		err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2725 						  parent, root_al, branch_from,
2726 						  true, i, symbols);
2727 		if (err)
2728 			goto error;
2729 
2730 		err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2731 					       root_al, &branch_from, true, symbols);
2732 		if (err)
2733 			goto error;
2734 
2735 		if (stitched_lbr) {
2736 			err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2737 			if (err)
2738 				goto error;
2739 		}
2740 
2741 	} else {
2742 		if (stitched_lbr) {
2743 			err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2744 			if (err)
2745 				goto error;
2746 		}
2747 		err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2748 					       root_al, &branch_from, false, symbols);
2749 		if (err)
2750 			goto error;
2751 
2752 		/* Add kernel ip */
2753 		err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2754 						  parent, root_al, branch_from,
2755 						  false, i, symbols);
2756 		if (err)
2757 			goto error;
2758 	}
2759 	return 1;
2760 
2761 error:
2762 	return (err < 0) ? err : 0;
2763 }
2764 
2765 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2766 			     struct callchain_cursor *cursor,
2767 			     struct symbol **parent,
2768 			     struct addr_location *root_al,
2769 			     u8 *cpumode, int ent, bool symbols)
2770 {
2771 	int err = 0;
2772 
2773 	while (--ent >= 0) {
2774 		u64 ip = chain->ips[ent];
2775 
2776 		if (ip >= PERF_CONTEXT_MAX) {
2777 			err = add_callchain_ip(thread, cursor, parent,
2778 					       root_al, cpumode, ip,
2779 					       false, NULL, NULL, 0, symbols);
2780 			break;
2781 		}
2782 	}
2783 	return err;
2784 }
2785 
2786 static u64 get_leaf_frame_caller(struct perf_sample *sample,
2787 		struct thread *thread, int usr_idx)
2788 {
2789 	if (machine__normalized_is(maps__machine(thread__maps(thread)), "arm64"))
2790 		return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
2791 	else
2792 		return 0;
2793 }
2794 
2795 static int thread__resolve_callchain_sample(struct thread *thread,
2796 					    struct callchain_cursor *cursor,
2797 					    struct perf_sample *sample,
2798 					    struct symbol **parent,
2799 					    struct addr_location *root_al,
2800 					    int max_stack,
2801 					    bool symbols)
2802 {
2803 	struct evsel *evsel = sample->evsel;
2804 	struct branch_stack *branch = sample->branch_stack;
2805 	struct branch_entry *entries = perf_sample__branch_entries(sample);
2806 	struct ip_callchain *chain = sample->callchain;
2807 	int chain_nr = 0;
2808 	u8 cpumode = PERF_RECORD_MISC_USER;
2809 	int i, j, err, nr_entries, usr_idx;
2810 	int skip_idx = -1;
2811 	int first_call = 0;
2812 	u64 leaf_frame_caller;
2813 
2814 	if (chain)
2815 		chain_nr = chain->nr;
2816 
2817 	if (evsel__has_branch_callstack(evsel)) {
2818 		struct perf_env *env = evsel__env(evsel);
2819 
2820 		err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2821 						   root_al, max_stack,
2822 						   !env ? 0 : env->max_branches,
2823 						   symbols);
2824 		if (err)
2825 			return (err < 0) ? err : 0;
2826 	}
2827 
2828 	/*
2829 	 * Based on DWARF debug information, some architectures skip
2830 	 * a callchain entry saved by the kernel.
2831 	 */
2832 	skip_idx = arch_skip_callchain_idx(thread, chain);
2833 
2834 	/*
2835 	 * Add branches to call stack for easier browsing. This gives
2836 	 * more context for a sample than just the callers.
2837 	 *
2838 	 * This uses individual histograms of paths compared to the
2839 	 * aggregated histograms the normal LBR mode uses.
2840 	 *
2841 	 * Limitations for now:
2842 	 * - No extra filters
2843 	 * - No annotations (should annotate somehow)
2844 	 */
2845 
2846 	if (branch && callchain_param.branch_callstack) {
2847 		int nr = min(max_stack, (int)branch->nr);
2848 		struct branch_entry be[nr];
2849 		struct iterations iter[nr];
2850 
2851 		if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2852 			pr_warning("corrupted branch chain. skipping...\n");
2853 			goto check_calls;
2854 		}
2855 
2856 		for (i = 0; i < nr; i++) {
2857 			if (callchain_param.order == ORDER_CALLEE) {
2858 				be[i] = entries[i];
2859 
2860 				if (chain == NULL)
2861 					continue;
2862 
2863 				/*
2864 				 * Check for overlap into the callchain.
2865 				 * The return address is one off compared to
2866 				 * the branch entry. To adjust for this
2867 				 * assume the calling instruction is not longer
2868 				 * than 8 bytes.
2869 				 */
2870 				if (i == skip_idx ||
2871 				    chain->ips[first_call] >= PERF_CONTEXT_MAX)
2872 					first_call++;
2873 				else if (be[i].from < chain->ips[first_call] &&
2874 				    be[i].from >= chain->ips[first_call] - 8)
2875 					first_call++;
2876 			} else
2877 				be[i] = entries[branch->nr - i - 1];
2878 		}
2879 
2880 		memset(iter, 0, sizeof(struct iterations) * nr);
2881 		nr = remove_loops(be, nr, iter);
2882 
2883 		for (i = 0; i < nr; i++) {
2884 			err = add_callchain_ip(thread, cursor, parent,
2885 					       root_al,
2886 					       NULL, be[i].to,
2887 					       true, &be[i].flags,
2888 					       NULL, be[i].from, symbols);
2889 
2890 			if (!err) {
2891 				err = add_callchain_ip(thread, cursor, parent, root_al,
2892 						       NULL, be[i].from,
2893 						       true, &be[i].flags,
2894 						       &iter[i], 0, symbols);
2895 			}
2896 			if (err == -EINVAL)
2897 				break;
2898 			if (err)
2899 				return err;
2900 		}
2901 
2902 		if (chain_nr == 0)
2903 			return 0;
2904 
2905 		chain_nr -= nr;
2906 	}
2907 
2908 check_calls:
2909 	if (chain && callchain_param.order != ORDER_CALLEE) {
2910 		err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2911 					&cpumode, chain->nr - first_call, symbols);
2912 		if (err)
2913 			return (err < 0) ? err : 0;
2914 	}
2915 	for (i = first_call, nr_entries = 0;
2916 	     i < chain_nr && nr_entries < max_stack; i++) {
2917 		u64 ip;
2918 
2919 		if (callchain_param.order == ORDER_CALLEE)
2920 			j = i;
2921 		else
2922 			j = chain->nr - i - 1;
2923 
2924 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2925 		if (j == skip_idx)
2926 			continue;
2927 #endif
2928 		ip = chain->ips[j];
2929 		if (ip < PERF_CONTEXT_MAX)
2930                        ++nr_entries;
2931 		else if (callchain_param.order != ORDER_CALLEE) {
2932 			err = find_prev_cpumode(chain, thread, cursor, parent,
2933 						root_al, &cpumode, j, symbols);
2934 			if (err)
2935 				return (err < 0) ? err : 0;
2936 			continue;
2937 		}
2938 
2939 		/*
2940 		 * PERF_CONTEXT_USER allows us to locate where the user stack ends.
2941 		 * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
2942 		 * the index will be different in order to add the missing frame
2943 		 * at the right place.
2944 		 */
2945 
2946 		usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
2947 
2948 		if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
2949 
2950 			leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
2951 
2952 			/*
2953 			 * check if leaf_frame_Caller != ip to not add the same
2954 			 * value twice.
2955 			 */
2956 
2957 			if (leaf_frame_caller && leaf_frame_caller != ip) {
2958 
2959 				err = add_callchain_ip(thread, cursor, parent,
2960 						root_al, &cpumode, leaf_frame_caller,
2961 						false, NULL, NULL, 0, symbols);
2962 				if (err)
2963 					return (err < 0) ? err : 0;
2964 			}
2965 		}
2966 
2967 		err = add_callchain_ip(thread, cursor, parent,
2968 				       root_al, &cpumode, ip,
2969 				       false, NULL, NULL, 0, symbols);
2970 
2971 		if (err)
2972 			return (err < 0) ? err : 0;
2973 	}
2974 
2975 	return 0;
2976 }
2977 
2978 static int unwind_entry(struct unwind_entry *entry, void *arg)
2979 {
2980 	struct callchain_cursor *cursor = arg;
2981 	const char *srcline = NULL;
2982 	u64 addr = entry->ip;
2983 
2984 	if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
2985 		return 0;
2986 
2987 	if (append_inlines(cursor, &entry->ms, entry->ip, /*branch=*/false, /*branch_flags=*/NULL,
2988 			   /*nr_loop_iter=*/0, /*iter_cycles=*/0, /*branch_from=*/0) == 0)
2989 		return 0;
2990 
2991 	/*
2992 	 * Convert entry->ip from a virtual address to an offset in
2993 	 * its corresponding binary.
2994 	 */
2995 	if (entry->ms.map)
2996 		addr = map__dso_map_ip(entry->ms.map, entry->ip);
2997 
2998 	srcline = callchain_srcline(&entry->ms, addr);
2999 	return callchain_cursor_append(cursor, entry->ip, &entry->ms,
3000 				       false, NULL, 0, 0, 0, srcline);
3001 }
3002 
3003 static int thread__resolve_callchain_unwind(struct thread *thread,
3004 					    struct callchain_cursor *cursor,
3005 					    struct perf_sample *sample,
3006 					    int max_stack, bool symbols)
3007 {
3008 	struct evsel *evsel = sample->evsel;
3009 
3010 	/* Can we do dwarf post unwind? */
3011 	if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
3012 	      (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
3013 		return 0;
3014 
3015 	/* Bail out if nothing was captured. */
3016 	if (!sample->user_regs || !sample->user_regs->regs ||
3017 	    !sample->user_stack.size)
3018 		return 0;
3019 
3020 	if (!symbols)
3021 		pr_debug("Not resolving symbols with an unwinder isn't currently supported\n");
3022 
3023 	return unwind__get_entries(unwind_entry, cursor,
3024 				   thread, sample, max_stack, false);
3025 }
3026 
3027 int __thread__resolve_callchain(struct thread *thread,
3028 				struct callchain_cursor *cursor,
3029 				struct perf_sample *sample,
3030 				struct symbol **parent,
3031 				struct addr_location *root_al,
3032 				int max_stack,
3033 				bool symbols)
3034 {
3035 	int ret = 0;
3036 
3037 	if (cursor == NULL)
3038 		return -ENOMEM;
3039 
3040 	callchain_cursor_reset(cursor);
3041 
3042 	if (callchain_param.order == ORDER_CALLEE) {
3043 		ret = thread__resolve_callchain_sample(thread, cursor,
3044 						       sample,
3045 						       parent, root_al,
3046 						       max_stack, symbols);
3047 		if (ret)
3048 			return ret;
3049 		ret = thread__resolve_callchain_unwind(thread, cursor,
3050 						       sample,
3051 						       max_stack, symbols);
3052 	} else {
3053 		ret = thread__resolve_callchain_unwind(thread, cursor,
3054 						       sample,
3055 						       max_stack, symbols);
3056 		if (ret)
3057 			return ret;
3058 		ret = thread__resolve_callchain_sample(thread, cursor,
3059 						       sample,
3060 						       parent, root_al,
3061 						       max_stack, symbols);
3062 	}
3063 
3064 	return ret;
3065 }
3066 
3067 int machine__for_each_thread(struct machine *machine,
3068 			     int (*fn)(struct thread *thread, void *p),
3069 			     void *priv)
3070 {
3071 	return threads__for_each_thread(&machine->threads, fn, priv);
3072 }
3073 
3074 int machines__for_each_thread(struct machines *machines,
3075 			      int (*fn)(struct thread *thread, void *p),
3076 			      void *priv)
3077 {
3078 	struct rb_node *nd;
3079 	int rc = 0;
3080 
3081 	rc = machine__for_each_thread(&machines->host, fn, priv);
3082 	if (rc != 0)
3083 		return rc;
3084 
3085 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
3086 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
3087 
3088 		rc = machine__for_each_thread(machine, fn, priv);
3089 		if (rc != 0)
3090 			return rc;
3091 	}
3092 	return rc;
3093 }
3094 
3095 
3096 static int thread_list_cb(struct thread *thread, void *data)
3097 {
3098 	struct list_head *list = data;
3099 	struct thread_list *entry = malloc(sizeof(*entry));
3100 
3101 	if (!entry)
3102 		return -ENOMEM;
3103 
3104 	entry->thread = thread__get(thread);
3105 	list_add_tail(&entry->list, list);
3106 	return 0;
3107 }
3108 
3109 int machine__thread_list(struct machine *machine, struct list_head *list)
3110 {
3111 	return machine__for_each_thread(machine, thread_list_cb, list);
3112 }
3113 
3114 void thread_list__delete(struct list_head *list)
3115 {
3116 	struct thread_list *pos, *next;
3117 
3118 	list_for_each_entry_safe(pos, next, list, list) {
3119 		thread__zput(pos->thread);
3120 		list_del(&pos->list);
3121 		free(pos);
3122 	}
3123 }
3124 
3125 pid_t machine__get_current_tid(struct machine *machine, int cpu)
3126 {
3127 	if (cpu < 0 || (size_t)cpu >= machine->current_tid_sz)
3128 		return -1;
3129 
3130 	return machine->current_tid[cpu];
3131 }
3132 
3133 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
3134 			     pid_t tid)
3135 {
3136 	struct thread *thread;
3137 	const pid_t init_val = -1;
3138 
3139 	if (cpu < 0)
3140 		return -EINVAL;
3141 
3142 	if (realloc_array_as_needed(machine->current_tid,
3143 				    machine->current_tid_sz,
3144 				    (unsigned int)cpu,
3145 				    &init_val))
3146 		return -ENOMEM;
3147 
3148 	machine->current_tid[cpu] = tid;
3149 
3150 	thread = machine__findnew_thread(machine, pid, tid);
3151 	if (!thread)
3152 		return -ENOMEM;
3153 
3154 	thread__set_cpu(thread, cpu);
3155 	thread__put(thread);
3156 
3157 	return 0;
3158 }
3159 
3160 /*
3161  * Compares the raw arch string. N.B. see instead perf_env__arch() or
3162  * machine__normalized_is() if a normalized arch is needed.
3163  */
3164 bool machine__is(struct machine *machine, const char *arch)
3165 {
3166 	return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
3167 }
3168 
3169 bool machine__normalized_is(struct machine *machine, const char *arch)
3170 {
3171 	return machine && !strcmp(perf_env__arch(machine->env), arch);
3172 }
3173 
3174 int machine__nr_cpus_avail(struct machine *machine)
3175 {
3176 	return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
3177 }
3178 
3179 int machine__get_kernel_start(struct machine *machine)
3180 {
3181 	struct map *map = machine__kernel_map(machine);
3182 	int err = 0;
3183 
3184 	/*
3185 	 * The only addresses above 2^63 are kernel addresses of a 64-bit
3186 	 * kernel.  Note that addresses are unsigned so that on a 32-bit system
3187 	 * all addresses including kernel addresses are less than 2^32.  In
3188 	 * that case (32-bit system), if the kernel mapping is unknown, all
3189 	 * addresses will be assumed to be in user space - see
3190 	 * machine__kernel_ip().
3191 	 */
3192 	machine->kernel_start = 1ULL << 63;
3193 	if (map) {
3194 		err = map__load(map);
3195 		/*
3196 		 * On x86_64, PTI entry trampolines are less than the
3197 		 * start of kernel text, but still above 2^63. So leave
3198 		 * kernel_start = 1ULL << 63 for x86_64.
3199 		 */
3200 		if (!err && !machine__is(machine, "x86_64"))
3201 			machine->kernel_start = map__start(map);
3202 	}
3203 	return err;
3204 }
3205 
3206 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
3207 {
3208 	u8 addr_cpumode = cpumode;
3209 	bool kernel_ip;
3210 
3211 	if (!machine->single_address_space)
3212 		goto out;
3213 
3214 	kernel_ip = machine__kernel_ip(machine, addr);
3215 	switch (cpumode) {
3216 	case PERF_RECORD_MISC_KERNEL:
3217 	case PERF_RECORD_MISC_USER:
3218 		addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
3219 					   PERF_RECORD_MISC_USER;
3220 		break;
3221 	case PERF_RECORD_MISC_GUEST_KERNEL:
3222 	case PERF_RECORD_MISC_GUEST_USER:
3223 		addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
3224 					   PERF_RECORD_MISC_GUEST_USER;
3225 		break;
3226 	default:
3227 		break;
3228 	}
3229 out:
3230 	return addr_cpumode;
3231 }
3232 
3233 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename,
3234 				    const struct dso_id *id)
3235 {
3236 	return dsos__findnew_id(&machine->dsos, filename, id);
3237 }
3238 
3239 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
3240 {
3241 	return machine__findnew_dso_id(machine, filename, &dso_id_empty);
3242 }
3243 
3244 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
3245 {
3246 	struct machine *machine = vmachine;
3247 	struct map *map;
3248 	struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
3249 
3250 	if (sym == NULL)
3251 		return NULL;
3252 
3253 	*modp = __map__is_kmodule(map) ? (char *)dso__short_name(map__dso(map)) : NULL;
3254 	*addrp = map__unmap_ip(map, sym->start);
3255 	return sym->name;
3256 }
3257 
3258 struct machine__for_each_dso_cb_args {
3259 	struct machine *machine;
3260 	machine__dso_t fn;
3261 	void *priv;
3262 };
3263 
3264 static int machine__for_each_dso_cb(struct dso *dso, void *data)
3265 {
3266 	struct machine__for_each_dso_cb_args *args = data;
3267 
3268 	return args->fn(dso, args->machine, args->priv);
3269 }
3270 
3271 int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
3272 {
3273 	struct machine__for_each_dso_cb_args args = {
3274 		.machine = machine,
3275 		.fn = fn,
3276 		.priv = priv,
3277 	};
3278 
3279 	return dsos__for_each_dso(&machine->dsos, machine__for_each_dso_cb, &args);
3280 }
3281 
3282 int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn, void *priv)
3283 {
3284 	struct maps *maps = machine__kernel_maps(machine);
3285 
3286 	return maps__for_each_map(maps, fn, priv);
3287 }
3288 
3289 bool machine__is_lock_function(struct machine *machine, u64 addr)
3290 {
3291 	if (!machine->sched.text_start) {
3292 		struct map *kmap;
3293 		struct symbol *sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_start", &kmap);
3294 
3295 		if (!sym) {
3296 			/* to avoid retry */
3297 			machine->sched.text_start = 1;
3298 			return false;
3299 		}
3300 
3301 		machine->sched.text_start = map__unmap_ip(kmap, sym->start);
3302 
3303 		/* should not fail from here */
3304 		sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_end", &kmap);
3305 		machine->sched.text_end = map__unmap_ip(kmap, sym->start);
3306 
3307 		sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_start", &kmap);
3308 		machine->lock.text_start = map__unmap_ip(kmap, sym->start);
3309 
3310 		sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_end", &kmap);
3311 		machine->lock.text_end = map__unmap_ip(kmap, sym->start);
3312 
3313 		sym = machine__find_kernel_symbol_by_name(machine, "__traceiter_contention_begin", &kmap);
3314 		if (sym) {
3315 			machine->traceiter.text_start = map__unmap_ip(kmap, sym->start);
3316 			machine->traceiter.text_end = map__unmap_ip(kmap, sym->end);
3317 		}
3318 		sym = machine__find_kernel_symbol_by_name(machine, "trace_contention_begin", &kmap);
3319 		if (sym) {
3320 			machine->trace.text_start = map__unmap_ip(kmap, sym->start);
3321 			machine->trace.text_end = map__unmap_ip(kmap, sym->end);
3322 		}
3323 	}
3324 
3325 	/* failed to get kernel symbols */
3326 	if (machine->sched.text_start == 1)
3327 		return false;
3328 
3329 	/* mutex and rwsem functions are in sched text section */
3330 	if (machine->sched.text_start <= addr && addr < machine->sched.text_end)
3331 		return true;
3332 
3333 	/* spinlock functions are in lock text section */
3334 	if (machine->lock.text_start <= addr && addr < machine->lock.text_end)
3335 		return true;
3336 
3337 	/* traceiter functions currently don't have their own section
3338 	 * but we consider them lock functions
3339 	 */
3340 	if (machine->traceiter.text_start != 0) {
3341 		if (machine->traceiter.text_start <= addr && addr < machine->traceiter.text_end)
3342 			return true;
3343 	}
3344 
3345 	if (machine->trace.text_start != 0) {
3346 		if (machine->trace.text_start <= addr && addr < machine->trace.text_end)
3347 			return true;
3348 	}
3349 
3350 	return false;
3351 }
3352 
3353 int machine__hit_all_dsos(struct machine *machine)
3354 {
3355 	return dsos__hit_all(&machine->dsos);
3356 }
3357