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