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