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