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