1 // SPDX-License-Identifier: GPL-2.0
16 #include "map_symbol.h"
18 #include "mem-events.h"
29 #include <sys/types.h>
33 #include "linux/hash.h"
35 #include "bpf-event.h"
36 #include <internal/lib.h> // page_size
38 #include "arm64-frame-pointer-unwind-support.h"
40 #include <linux/ctype.h>
41 #include <symbol/kallsyms.h>
42 #include <linux/mman.h>
43 #include <linux/string.h>
44 #include <linux/zalloc.h>
46 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
48 static struct dso *machine__kernel_dso(struct machine *machine)
50 return machine->vmlinux_map->dso;
53 static void dsos__init(struct dsos *dsos)
55 INIT_LIST_HEAD(&dsos->head);
57 init_rwsem(&dsos->lock);
60 static void machine__threads_init(struct machine *machine)
64 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
65 struct threads *threads = &machine->threads[i];
66 threads->entries = RB_ROOT_CACHED;
67 init_rwsem(&threads->lock);
69 INIT_LIST_HEAD(&threads->dead);
70 threads->last_match = NULL;
74 static int machine__set_mmap_name(struct machine *machine)
76 if (machine__is_host(machine))
77 machine->mmap_name = strdup("[kernel.kallsyms]");
78 else if (machine__is_default_guest(machine))
79 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
80 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
82 machine->mmap_name = NULL;
84 return machine->mmap_name ? 0 : -ENOMEM;
87 static void thread__set_guest_comm(struct thread *thread, pid_t pid)
91 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
92 thread__set_comm(thread, comm, 0);
95 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
99 memset(machine, 0, sizeof(*machine));
100 machine->kmaps = maps__new(machine);
101 if (machine->kmaps == NULL)
104 RB_CLEAR_NODE(&machine->rb_node);
105 dsos__init(&machine->dsos);
107 machine__threads_init(machine);
109 machine->vdso_info = NULL;
114 machine->id_hdr_size = 0;
115 machine->kptr_restrict_warned = false;
116 machine->comm_exec = false;
117 machine->kernel_start = 0;
118 machine->vmlinux_map = NULL;
120 machine->root_dir = strdup(root_dir);
121 if (machine->root_dir == NULL)
124 if (machine__set_mmap_name(machine))
127 if (pid != HOST_KERNEL_ID) {
128 struct thread *thread = machine__findnew_thread(machine, -1,
134 thread__set_guest_comm(thread, pid);
138 machine->current_tid = NULL;
143 zfree(&machine->kmaps);
144 zfree(&machine->root_dir);
145 zfree(&machine->mmap_name);
150 struct machine *machine__new_host(void)
152 struct machine *machine = malloc(sizeof(*machine));
154 if (machine != NULL) {
155 machine__init(machine, "", HOST_KERNEL_ID);
157 if (machine__create_kernel_maps(machine) < 0)
167 struct machine *machine__new_kallsyms(void)
169 struct machine *machine = machine__new_host();
172 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
173 * ask for not using the kcore parsing code, once this one is fixed
174 * to create a map per module.
176 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
177 machine__delete(machine);
184 static void dsos__purge(struct dsos *dsos)
188 down_write(&dsos->lock);
190 list_for_each_entry_safe(pos, n, &dsos->head, node) {
191 RB_CLEAR_NODE(&pos->rb_node);
193 list_del_init(&pos->node);
197 up_write(&dsos->lock);
200 static void dsos__exit(struct dsos *dsos)
203 exit_rwsem(&dsos->lock);
206 void machine__delete_threads(struct machine *machine)
211 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
212 struct threads *threads = &machine->threads[i];
213 down_write(&threads->lock);
214 nd = rb_first_cached(&threads->entries);
216 struct thread *t = rb_entry(nd, struct thread, rb_node);
219 __machine__remove_thread(machine, t, false);
221 up_write(&threads->lock);
225 void machine__exit(struct machine *machine)
232 machine__destroy_kernel_maps(machine);
233 maps__delete(machine->kmaps);
234 dsos__exit(&machine->dsos);
235 machine__exit_vdso(machine);
236 zfree(&machine->root_dir);
237 zfree(&machine->mmap_name);
238 zfree(&machine->current_tid);
240 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
241 struct threads *threads = &machine->threads[i];
242 struct thread *thread, *n;
244 * Forget about the dead, at this point whatever threads were
245 * left in the dead lists better have a reference count taken
246 * by who is using them, and then, when they drop those references
247 * and it finally hits zero, thread__put() will check and see that
248 * its not in the dead threads list and will not try to remove it
249 * from there, just calling thread__delete() straight away.
251 list_for_each_entry_safe(thread, n, &threads->dead, node)
252 list_del_init(&thread->node);
254 exit_rwsem(&threads->lock);
258 void machine__delete(struct machine *machine)
261 machine__exit(machine);
266 void machines__init(struct machines *machines)
268 machine__init(&machines->host, "", HOST_KERNEL_ID);
269 machines->guests = RB_ROOT_CACHED;
272 void machines__exit(struct machines *machines)
274 machine__exit(&machines->host);
278 struct machine *machines__add(struct machines *machines, pid_t pid,
279 const char *root_dir)
281 struct rb_node **p = &machines->guests.rb_root.rb_node;
282 struct rb_node *parent = NULL;
283 struct machine *pos, *machine = malloc(sizeof(*machine));
284 bool leftmost = true;
289 if (machine__init(machine, root_dir, pid) != 0) {
296 pos = rb_entry(parent, struct machine, rb_node);
305 rb_link_node(&machine->rb_node, parent, p);
306 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
308 machine->machines = machines;
313 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
317 machines->host.comm_exec = comm_exec;
319 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
320 struct machine *machine = rb_entry(nd, struct machine, rb_node);
322 machine->comm_exec = comm_exec;
326 struct machine *machines__find(struct machines *machines, pid_t pid)
328 struct rb_node **p = &machines->guests.rb_root.rb_node;
329 struct rb_node *parent = NULL;
330 struct machine *machine;
331 struct machine *default_machine = NULL;
333 if (pid == HOST_KERNEL_ID)
334 return &machines->host;
338 machine = rb_entry(parent, struct machine, rb_node);
339 if (pid < machine->pid)
341 else if (pid > machine->pid)
346 default_machine = machine;
349 return default_machine;
352 struct machine *machines__findnew(struct machines *machines, pid_t pid)
355 const char *root_dir = "";
356 struct machine *machine = machines__find(machines, pid);
358 if (machine && (machine->pid == pid))
361 if ((pid != HOST_KERNEL_ID) &&
362 (pid != DEFAULT_GUEST_KERNEL_ID) &&
363 (symbol_conf.guestmount)) {
364 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
365 if (access(path, R_OK)) {
366 static struct strlist *seen;
369 seen = strlist__new(NULL, NULL);
371 if (!strlist__has_entry(seen, path)) {
372 pr_err("Can't access file %s\n", path);
373 strlist__add(seen, path);
381 machine = machines__add(machines, pid, root_dir);
386 struct machine *machines__find_guest(struct machines *machines, pid_t pid)
388 struct machine *machine = machines__find(machines, pid);
391 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
396 * A common case for KVM test programs is that the test program acts as the
397 * hypervisor, creating, running and destroying the virtual machine, and
398 * providing the guest object code from its own object code. In this case,
399 * the VM is not running an OS, but only the functions loaded into it by the
400 * hypervisor test program, and conveniently, loaded at the same virtual
403 * Normally to resolve addresses, MMAP events are needed to map addresses
404 * back to the object code and debug symbols for that object code.
406 * Currently, there is no way to get such mapping information from guests
407 * but, in the scenario described above, the guest has the same mappings
408 * as the hypervisor, so support for that scenario can be achieved.
410 * To support that, copy the host thread's maps to the guest thread's maps.
411 * Note, we do not discover the guest until we encounter a guest event,
412 * which works well because it is not until then that we know that the host
413 * thread's maps have been set up.
415 * This function returns the guest thread. Apart from keeping the data
416 * structures sane, using a thread belonging to the guest machine, instead
417 * of the host thread, allows it to have its own comm (refer
418 * thread__set_guest_comm()).
420 static struct thread *findnew_guest_code(struct machine *machine,
421 struct machine *host_machine,
424 struct thread *host_thread;
425 struct thread *thread;
431 thread = machine__findnew_thread(machine, -1, pid);
435 /* Assume maps are set up if there are any */
436 if (thread->maps->nr_maps)
439 host_thread = machine__find_thread(host_machine, -1, pid);
443 thread__set_guest_comm(thread, pid);
446 * Guest code can be found in hypervisor process at the same address
449 err = maps__clone(thread, host_thread->maps);
450 thread__put(host_thread);
457 thread__zput(thread);
461 struct thread *machines__findnew_guest_code(struct machines *machines, pid_t pid)
463 struct machine *host_machine = machines__find(machines, HOST_KERNEL_ID);
464 struct machine *machine = machines__findnew(machines, pid);
466 return findnew_guest_code(machine, host_machine, pid);
469 struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid)
471 struct machines *machines = machine->machines;
472 struct machine *host_machine;
477 host_machine = machines__find(machines, HOST_KERNEL_ID);
479 return findnew_guest_code(machine, host_machine, pid);
482 void machines__process_guests(struct machines *machines,
483 machine__process_t process, void *data)
487 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
488 struct machine *pos = rb_entry(nd, struct machine, rb_node);
493 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
495 struct rb_node *node;
496 struct machine *machine;
498 machines->host.id_hdr_size = id_hdr_size;
500 for (node = rb_first_cached(&machines->guests); node;
501 node = rb_next(node)) {
502 machine = rb_entry(node, struct machine, rb_node);
503 machine->id_hdr_size = id_hdr_size;
509 static void machine__update_thread_pid(struct machine *machine,
510 struct thread *th, pid_t pid)
512 struct thread *leader;
514 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
519 if (th->pid_ == th->tid)
522 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
527 leader->maps = maps__new(machine);
532 if (th->maps == leader->maps)
537 * Maps are created from MMAP events which provide the pid and
538 * tid. Consequently there never should be any maps on a thread
539 * with an unknown pid. Just print an error if there are.
541 if (!maps__empty(th->maps))
542 pr_err("Discarding thread maps for %d:%d\n",
547 th->maps = maps__get(leader->maps);
552 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
557 * Front-end cache - TID lookups come in blocks,
558 * so most of the time we dont have to look up
561 static struct thread*
562 __threads__get_last_match(struct threads *threads, struct machine *machine,
567 th = threads->last_match;
569 if (th->tid == tid) {
570 machine__update_thread_pid(machine, th, pid);
571 return thread__get(th);
574 threads->last_match = NULL;
580 static struct thread*
581 threads__get_last_match(struct threads *threads, struct machine *machine,
584 struct thread *th = NULL;
586 if (perf_singlethreaded)
587 th = __threads__get_last_match(threads, machine, pid, tid);
593 __threads__set_last_match(struct threads *threads, struct thread *th)
595 threads->last_match = th;
599 threads__set_last_match(struct threads *threads, struct thread *th)
601 if (perf_singlethreaded)
602 __threads__set_last_match(threads, th);
606 * Caller must eventually drop thread->refcnt returned with a successful
607 * lookup/new thread inserted.
609 static struct thread *____machine__findnew_thread(struct machine *machine,
610 struct threads *threads,
611 pid_t pid, pid_t tid,
614 struct rb_node **p = &threads->entries.rb_root.rb_node;
615 struct rb_node *parent = NULL;
617 bool leftmost = true;
619 th = threads__get_last_match(threads, machine, pid, tid);
625 th = rb_entry(parent, struct thread, rb_node);
627 if (th->tid == tid) {
628 threads__set_last_match(threads, th);
629 machine__update_thread_pid(machine, th, pid);
630 return thread__get(th);
644 th = thread__new(pid, tid);
646 rb_link_node(&th->rb_node, parent, p);
647 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
650 * We have to initialize maps separately after rb tree is updated.
652 * The reason is that we call machine__findnew_thread
653 * within thread__init_maps to find the thread
654 * leader and that would screwed the rb tree.
656 if (thread__init_maps(th, machine)) {
657 rb_erase_cached(&th->rb_node, &threads->entries);
658 RB_CLEAR_NODE(&th->rb_node);
663 * It is now in the rbtree, get a ref
666 threads__set_last_match(threads, th);
673 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
675 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
678 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
681 struct threads *threads = machine__threads(machine, tid);
684 down_write(&threads->lock);
685 th = __machine__findnew_thread(machine, pid, tid);
686 up_write(&threads->lock);
690 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
693 struct threads *threads = machine__threads(machine, tid);
696 down_read(&threads->lock);
697 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
698 up_read(&threads->lock);
703 * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
704 * So here a single thread is created for that, but actually there is a separate
705 * idle task per cpu, so there should be one 'struct thread' per cpu, but there
706 * is only 1. That causes problems for some tools, requiring workarounds. For
707 * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
709 struct thread *machine__idle_thread(struct machine *machine)
711 struct thread *thread = machine__findnew_thread(machine, 0, 0);
713 if (!thread || thread__set_comm(thread, "swapper", 0) ||
714 thread__set_namespaces(thread, 0, NULL))
715 pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
720 struct comm *machine__thread_exec_comm(struct machine *machine,
721 struct thread *thread)
723 if (machine->comm_exec)
724 return thread__exec_comm(thread);
726 return thread__comm(thread);
729 int machine__process_comm_event(struct machine *machine, union perf_event *event,
730 struct perf_sample *sample)
732 struct thread *thread = machine__findnew_thread(machine,
735 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
739 machine->comm_exec = true;
742 perf_event__fprintf_comm(event, stdout);
744 if (thread == NULL ||
745 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
746 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
755 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
756 union perf_event *event,
757 struct perf_sample *sample __maybe_unused)
759 struct thread *thread = machine__findnew_thread(machine,
760 event->namespaces.pid,
761 event->namespaces.tid);
764 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
765 "\nWARNING: kernel seems to support more namespaces than perf"
766 " tool.\nTry updating the perf tool..\n\n");
768 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
769 "\nWARNING: perf tool seems to support more namespaces than"
770 " the kernel.\nTry updating the kernel..\n\n");
773 perf_event__fprintf_namespaces(event, stdout);
775 if (thread == NULL ||
776 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
777 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
786 int machine__process_cgroup_event(struct machine *machine,
787 union perf_event *event,
788 struct perf_sample *sample __maybe_unused)
793 perf_event__fprintf_cgroup(event, stdout);
795 cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
802 int machine__process_lost_event(struct machine *machine __maybe_unused,
803 union perf_event *event, struct perf_sample *sample __maybe_unused)
805 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
806 event->lost.id, event->lost.lost);
810 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
811 union perf_event *event, struct perf_sample *sample)
813 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
814 sample->id, event->lost_samples.lost);
818 static struct dso *machine__findnew_module_dso(struct machine *machine,
820 const char *filename)
824 down_write(&machine->dsos.lock);
826 dso = __dsos__find(&machine->dsos, m->name, true);
828 dso = __dsos__addnew(&machine->dsos, m->name);
832 dso__set_module_info(dso, m, machine);
833 dso__set_long_name(dso, strdup(filename), true);
834 dso->kernel = DSO_SPACE__KERNEL;
839 up_write(&machine->dsos.lock);
843 int machine__process_aux_event(struct machine *machine __maybe_unused,
844 union perf_event *event)
847 perf_event__fprintf_aux(event, stdout);
851 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
852 union perf_event *event)
855 perf_event__fprintf_itrace_start(event, stdout);
859 int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
860 union perf_event *event)
863 perf_event__fprintf_aux_output_hw_id(event, stdout);
867 int machine__process_switch_event(struct machine *machine __maybe_unused,
868 union perf_event *event)
871 perf_event__fprintf_switch(event, stdout);
875 static int machine__process_ksymbol_register(struct machine *machine,
876 union perf_event *event,
877 struct perf_sample *sample __maybe_unused)
880 struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
883 struct dso *dso = dso__new(event->ksymbol.name);
886 dso->kernel = DSO_SPACE__KERNEL;
887 map = map__new2(0, dso);
895 if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
896 map->dso->binary_type = DSO_BINARY_TYPE__OOL;
897 map->dso->data.file_size = event->ksymbol.len;
898 dso__set_loaded(map->dso);
901 map->start = event->ksymbol.addr;
902 map->end = map->start + event->ksymbol.len;
903 maps__insert(machine__kernel_maps(machine), map);
905 dso__set_loaded(dso);
907 if (is_bpf_image(event->ksymbol.name)) {
908 dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE;
909 dso__set_long_name(dso, "", false);
913 sym = symbol__new(map->map_ip(map, map->start),
915 0, 0, event->ksymbol.name);
918 dso__insert_symbol(map->dso, sym);
922 static int machine__process_ksymbol_unregister(struct machine *machine,
923 union perf_event *event,
924 struct perf_sample *sample __maybe_unused)
929 map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
933 if (map != machine->vmlinux_map)
934 maps__remove(machine__kernel_maps(machine), map);
936 sym = dso__find_symbol(map->dso, map->map_ip(map, map->start));
938 dso__delete_symbol(map->dso, sym);
944 int machine__process_ksymbol(struct machine *machine __maybe_unused,
945 union perf_event *event,
946 struct perf_sample *sample)
949 perf_event__fprintf_ksymbol(event, stdout);
951 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
952 return machine__process_ksymbol_unregister(machine, event,
954 return machine__process_ksymbol_register(machine, event, sample);
957 int machine__process_text_poke(struct machine *machine, union perf_event *event,
958 struct perf_sample *sample __maybe_unused)
960 struct map *map = maps__find(machine__kernel_maps(machine), event->text_poke.addr);
961 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
964 perf_event__fprintf_text_poke(event, machine, stdout);
966 if (!event->text_poke.new_len)
969 if (cpumode != PERF_RECORD_MISC_KERNEL) {
970 pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
974 if (map && map->dso) {
975 u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
979 * Kernel maps might be changed when loading symbols so loading
980 * must be done prior to using kernel maps.
983 ret = dso__data_write_cache_addr(map->dso, map, machine,
984 event->text_poke.addr,
986 event->text_poke.new_len);
987 if (ret != event->text_poke.new_len)
988 pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
989 event->text_poke.addr);
991 pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
992 event->text_poke.addr);
998 static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
999 const char *filename)
1001 struct map *map = NULL;
1005 if (kmod_path__parse_name(&m, filename))
1008 dso = machine__findnew_module_dso(machine, &m, filename);
1012 map = map__new2(start, dso);
1016 maps__insert(machine__kernel_maps(machine), map);
1018 /* Put the map here because maps__insert already got it */
1021 /* put the dso here, corresponding to machine__findnew_module_dso */
1027 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
1030 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
1032 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
1033 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1034 ret += __dsos__fprintf(&pos->dsos.head, fp);
1040 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
1041 bool (skip)(struct dso *dso, int parm), int parm)
1043 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
1046 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
1047 bool (skip)(struct dso *dso, int parm), int parm)
1050 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
1052 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
1053 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1054 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
1059 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1063 struct dso *kdso = machine__kernel_dso(machine);
1065 if (kdso->has_build_id) {
1066 char filename[PATH_MAX];
1067 if (dso__build_id_filename(kdso, filename, sizeof(filename),
1069 printed += fprintf(fp, "[0] %s\n", filename);
1072 for (i = 0; i < vmlinux_path__nr_entries; ++i)
1073 printed += fprintf(fp, "[%d] %s\n",
1074 i + kdso->has_build_id, vmlinux_path[i]);
1079 size_t machine__fprintf(struct machine *machine, FILE *fp)
1085 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
1086 struct threads *threads = &machine->threads[i];
1088 down_read(&threads->lock);
1090 ret = fprintf(fp, "Threads: %u\n", threads->nr);
1092 for (nd = rb_first_cached(&threads->entries); nd;
1094 struct thread *pos = rb_entry(nd, struct thread, rb_node);
1096 ret += thread__fprintf(pos, fp);
1099 up_read(&threads->lock);
1104 static struct dso *machine__get_kernel(struct machine *machine)
1106 const char *vmlinux_name = machine->mmap_name;
1109 if (machine__is_host(machine)) {
1110 if (symbol_conf.vmlinux_name)
1111 vmlinux_name = symbol_conf.vmlinux_name;
1113 kernel = machine__findnew_kernel(machine, vmlinux_name,
1114 "[kernel]", DSO_SPACE__KERNEL);
1116 if (symbol_conf.default_guest_vmlinux_name)
1117 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1119 kernel = machine__findnew_kernel(machine, vmlinux_name,
1121 DSO_SPACE__KERNEL_GUEST);
1124 if (kernel != NULL && (!kernel->has_build_id))
1125 dso__read_running_kernel_build_id(kernel, machine);
1130 struct process_args {
1134 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
1137 if (machine__is_default_guest(machine))
1138 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
1140 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
1143 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
1145 /* Figure out the start address of kernel map from /proc/kallsyms.
1146 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
1147 * symbol_name if it's not that important.
1149 static int machine__get_running_kernel_start(struct machine *machine,
1150 const char **symbol_name,
1151 u64 *start, u64 *end)
1153 char filename[PATH_MAX];
1158 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
1160 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1163 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
1164 err = kallsyms__get_function_start(filename, name, &addr);
1173 *symbol_name = name;
1177 err = kallsyms__get_function_start(filename, "_etext", &addr);
1184 int machine__create_extra_kernel_map(struct machine *machine,
1186 struct extra_kernel_map *xm)
1191 map = map__new2(xm->start, kernel);
1196 map->pgoff = xm->pgoff;
1198 kmap = map__kmap(map);
1200 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
1202 maps__insert(machine__kernel_maps(machine), map);
1204 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1205 kmap->name, map->start, map->end);
1212 static u64 find_entry_trampoline(struct dso *dso)
1214 /* Duplicates are removed so lookup all aliases */
1215 const char *syms[] = {
1216 "_entry_trampoline",
1217 "__entry_trampoline_start",
1218 "entry_SYSCALL_64_trampoline",
1220 struct symbol *sym = dso__first_symbol(dso);
1223 for (; sym; sym = dso__next_symbol(sym)) {
1224 if (sym->binding != STB_GLOBAL)
1226 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1227 if (!strcmp(sym->name, syms[i]))
1236 * These values can be used for kernels that do not have symbols for the entry
1237 * trampolines in kallsyms.
1239 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1240 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1241 #define X86_64_ENTRY_TRAMPOLINE 0x6000
1243 /* Map x86_64 PTI entry trampolines */
1244 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1247 struct maps *kmaps = machine__kernel_maps(machine);
1248 int nr_cpus_avail, cpu;
1254 * In the vmlinux case, pgoff is a virtual address which must now be
1255 * mapped to a vmlinux offset.
1257 maps__for_each_entry(kmaps, map) {
1258 struct kmap *kmap = __map__kmap(map);
1259 struct map *dest_map;
1261 if (!kmap || !is_entry_trampoline(kmap->name))
1264 dest_map = maps__find(kmaps, map->pgoff);
1265 if (dest_map != map)
1266 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1269 if (found || machine->trampolines_mapped)
1272 pgoff = find_entry_trampoline(kernel);
1276 nr_cpus_avail = machine__nr_cpus_avail(machine);
1278 /* Add a 1 page map for each CPU's entry trampoline */
1279 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1280 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1281 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1282 X86_64_ENTRY_TRAMPOLINE;
1283 struct extra_kernel_map xm = {
1285 .end = va + page_size,
1289 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1291 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1295 machine->trampolines_mapped = nr_cpus_avail;
1300 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1301 struct dso *kernel __maybe_unused)
1307 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1309 /* In case of renewal the kernel map, destroy previous one */
1310 machine__destroy_kernel_maps(machine);
1312 machine->vmlinux_map = map__new2(0, kernel);
1313 if (machine->vmlinux_map == NULL)
1316 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1317 maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
1321 void machine__destroy_kernel_maps(struct machine *machine)
1324 struct map *map = machine__kernel_map(machine);
1329 kmap = map__kmap(map);
1330 maps__remove(machine__kernel_maps(machine), map);
1331 if (kmap && kmap->ref_reloc_sym) {
1332 zfree((char **)&kmap->ref_reloc_sym->name);
1333 zfree(&kmap->ref_reloc_sym);
1336 map__zput(machine->vmlinux_map);
1339 int machines__create_guest_kernel_maps(struct machines *machines)
1342 struct dirent **namelist = NULL;
1344 char path[PATH_MAX];
1348 if (symbol_conf.default_guest_vmlinux_name ||
1349 symbol_conf.default_guest_modules ||
1350 symbol_conf.default_guest_kallsyms) {
1351 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1354 if (symbol_conf.guestmount) {
1355 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1358 for (i = 0; i < items; i++) {
1359 if (!isdigit(namelist[i]->d_name[0])) {
1360 /* Filter out . and .. */
1363 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1364 if ((*endp != '\0') ||
1365 (endp == namelist[i]->d_name) ||
1366 (errno == ERANGE)) {
1367 pr_debug("invalid directory (%s). Skipping.\n",
1368 namelist[i]->d_name);
1371 sprintf(path, "%s/%s/proc/kallsyms",
1372 symbol_conf.guestmount,
1373 namelist[i]->d_name);
1374 ret = access(path, R_OK);
1376 pr_debug("Can't access file %s\n", path);
1379 machines__create_kernel_maps(machines, pid);
1388 void machines__destroy_kernel_maps(struct machines *machines)
1390 struct rb_node *next = rb_first_cached(&machines->guests);
1392 machine__destroy_kernel_maps(&machines->host);
1395 struct machine *pos = rb_entry(next, struct machine, rb_node);
1397 next = rb_next(&pos->rb_node);
1398 rb_erase_cached(&pos->rb_node, &machines->guests);
1399 machine__delete(pos);
1403 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1405 struct machine *machine = machines__findnew(machines, pid);
1407 if (machine == NULL)
1410 return machine__create_kernel_maps(machine);
1413 int machine__load_kallsyms(struct machine *machine, const char *filename)
1415 struct map *map = machine__kernel_map(machine);
1416 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
1419 dso__set_loaded(map->dso);
1421 * Since /proc/kallsyms will have multiple sessions for the
1422 * kernel, with modules between them, fixup the end of all
1425 maps__fixup_end(machine__kernel_maps(machine));
1431 int machine__load_vmlinux_path(struct machine *machine)
1433 struct map *map = machine__kernel_map(machine);
1434 int ret = dso__load_vmlinux_path(map->dso, map);
1437 dso__set_loaded(map->dso);
1442 static char *get_kernel_version(const char *root_dir)
1444 char version[PATH_MAX];
1447 const char *prefix = "Linux version ";
1449 sprintf(version, "%s/proc/version", root_dir);
1450 file = fopen(version, "r");
1454 tmp = fgets(version, sizeof(version), file);
1459 name = strstr(version, prefix);
1462 name += strlen(prefix);
1463 tmp = strchr(name, ' ');
1467 return strdup(name);
1470 static bool is_kmod_dso(struct dso *dso)
1472 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1473 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1476 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1479 struct map *map = maps__find_by_name(maps, m->name);
1484 long_name = strdup(path);
1485 if (long_name == NULL)
1488 dso__set_long_name(map->dso, long_name, true);
1489 dso__kernel_module_get_build_id(map->dso, "");
1492 * Full name could reveal us kmod compression, so
1493 * we need to update the symtab_type if needed.
1495 if (m->comp && is_kmod_dso(map->dso)) {
1496 map->dso->symtab_type++;
1497 map->dso->comp = m->comp;
1503 static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
1505 struct dirent *dent;
1506 DIR *dir = opendir(dir_name);
1510 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1514 while ((dent = readdir(dir)) != NULL) {
1515 char path[PATH_MAX];
1518 /*sshfs might return bad dent->d_type, so we have to stat*/
1519 path__join(path, sizeof(path), dir_name, dent->d_name);
1520 if (stat(path, &st))
1523 if (S_ISDIR(st.st_mode)) {
1524 if (!strcmp(dent->d_name, ".") ||
1525 !strcmp(dent->d_name, ".."))
1528 /* Do not follow top-level source and build symlinks */
1530 if (!strcmp(dent->d_name, "source") ||
1531 !strcmp(dent->d_name, "build"))
1535 ret = maps__set_modules_path_dir(maps, path, depth + 1);
1541 ret = kmod_path__parse_name(&m, dent->d_name);
1546 ret = maps__set_module_path(maps, path, &m);
1560 static int machine__set_modules_path(struct machine *machine)
1563 char modules_path[PATH_MAX];
1565 version = get_kernel_version(machine->root_dir);
1569 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1570 machine->root_dir, version);
1573 return maps__set_modules_path_dir(machine__kernel_maps(machine), modules_path, 0);
1575 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1576 u64 *size __maybe_unused,
1577 const char *name __maybe_unused)
1582 static int machine__create_module(void *arg, const char *name, u64 start,
1585 struct machine *machine = arg;
1588 if (arch__fix_module_text_start(&start, &size, name) < 0)
1591 map = machine__addnew_module_map(machine, start, name);
1594 map->end = start + size;
1596 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1601 static int machine__create_modules(struct machine *machine)
1603 const char *modules;
1604 char path[PATH_MAX];
1606 if (machine__is_default_guest(machine)) {
1607 modules = symbol_conf.default_guest_modules;
1609 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1613 if (symbol__restricted_filename(modules, "/proc/modules"))
1616 if (modules__parse(modules, machine, machine__create_module))
1619 if (!machine__set_modules_path(machine))
1622 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1627 static void machine__set_kernel_mmap(struct machine *machine,
1630 machine->vmlinux_map->start = start;
1631 machine->vmlinux_map->end = end;
1633 * Be a bit paranoid here, some perf.data file came with
1634 * a zero sized synthesized MMAP event for the kernel.
1636 if (start == 0 && end == 0)
1637 machine->vmlinux_map->end = ~0ULL;
1640 static void machine__update_kernel_mmap(struct machine *machine,
1643 struct map *map = machine__kernel_map(machine);
1646 maps__remove(machine__kernel_maps(machine), map);
1648 machine__set_kernel_mmap(machine, start, end);
1650 maps__insert(machine__kernel_maps(machine), map);
1654 int machine__create_kernel_maps(struct machine *machine)
1656 struct dso *kernel = machine__get_kernel(machine);
1657 const char *name = NULL;
1659 u64 start = 0, end = ~0ULL;
1665 ret = __machine__create_kernel_maps(machine, kernel);
1669 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1670 if (machine__is_host(machine))
1671 pr_debug("Problems creating module maps, "
1672 "continuing anyway...\n");
1674 pr_debug("Problems creating module maps for guest %d, "
1675 "continuing anyway...\n", machine->pid);
1678 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1680 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1681 machine__destroy_kernel_maps(machine);
1687 * we have a real start address now, so re-order the kmaps
1688 * assume it's the last in the kmaps
1690 machine__update_kernel_mmap(machine, start, end);
1693 if (machine__create_extra_kernel_maps(machine, kernel))
1694 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1697 /* update end address of the kernel map using adjacent module address */
1698 map = map__next(machine__kernel_map(machine));
1700 machine__set_kernel_mmap(machine, start, map->start);
1708 static bool machine__uses_kcore(struct machine *machine)
1712 list_for_each_entry(dso, &machine->dsos.head, node) {
1713 if (dso__is_kcore(dso))
1720 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1721 struct extra_kernel_map *xm)
1723 return machine__is(machine, "x86_64") &&
1724 is_entry_trampoline(xm->name);
1727 static int machine__process_extra_kernel_map(struct machine *machine,
1728 struct extra_kernel_map *xm)
1730 struct dso *kernel = machine__kernel_dso(machine);
1735 return machine__create_extra_kernel_map(machine, kernel, xm);
1738 static int machine__process_kernel_mmap_event(struct machine *machine,
1739 struct extra_kernel_map *xm,
1740 struct build_id *bid)
1743 enum dso_space_type dso_space;
1744 bool is_kernel_mmap;
1746 /* If we have maps from kcore then we do not need or want any others */
1747 if (machine__uses_kcore(machine))
1750 if (machine__is_host(machine))
1751 dso_space = DSO_SPACE__KERNEL;
1753 dso_space = DSO_SPACE__KERNEL_GUEST;
1755 is_kernel_mmap = memcmp(xm->name, machine->mmap_name,
1756 strlen(machine->mmap_name) - 1) == 0;
1757 if (xm->name[0] == '/' ||
1758 (!is_kernel_mmap && xm->name[0] == '[')) {
1759 map = machine__addnew_module_map(machine, xm->start,
1764 map->end = map->start + xm->end - xm->start;
1766 if (build_id__is_defined(bid))
1767 dso__set_build_id(map->dso, bid);
1769 } else if (is_kernel_mmap) {
1770 const char *symbol_name = (xm->name + strlen(machine->mmap_name));
1772 * Should be there already, from the build-id table in
1775 struct dso *kernel = NULL;
1778 down_read(&machine->dsos.lock);
1780 list_for_each_entry(dso, &machine->dsos.head, node) {
1783 * The cpumode passed to is_kernel_module is not the
1784 * cpumode of *this* event. If we insist on passing
1785 * correct cpumode to is_kernel_module, we should
1786 * record the cpumode when we adding this dso to the
1789 * However we don't really need passing correct
1790 * cpumode. We know the correct cpumode must be kernel
1791 * mode (if not, we should not link it onto kernel_dsos
1794 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1795 * is_kernel_module() treats it as a kernel cpumode.
1799 is_kernel_module(dso->long_name,
1800 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1808 up_read(&machine->dsos.lock);
1811 kernel = machine__findnew_dso(machine, machine->mmap_name);
1815 kernel->kernel = dso_space;
1816 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1821 if (strstr(kernel->long_name, "vmlinux"))
1822 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1824 machine__update_kernel_mmap(machine, xm->start, xm->end);
1826 if (build_id__is_defined(bid))
1827 dso__set_build_id(kernel, bid);
1830 * Avoid using a zero address (kptr_restrict) for the ref reloc
1831 * symbol. Effectively having zero here means that at record
1832 * time /proc/sys/kernel/kptr_restrict was non zero.
1834 if (xm->pgoff != 0) {
1835 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1840 if (machine__is_default_guest(machine)) {
1842 * preload dso of guest kernel and modules
1844 dso__load(kernel, machine__kernel_map(machine));
1846 } else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
1847 return machine__process_extra_kernel_map(machine, xm);
1854 int machine__process_mmap2_event(struct machine *machine,
1855 union perf_event *event,
1856 struct perf_sample *sample)
1858 struct thread *thread;
1860 struct dso_id dso_id = {
1861 .maj = event->mmap2.maj,
1862 .min = event->mmap2.min,
1863 .ino = event->mmap2.ino,
1864 .ino_generation = event->mmap2.ino_generation,
1866 struct build_id __bid, *bid = NULL;
1870 perf_event__fprintf_mmap2(event, stdout);
1872 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
1874 build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size);
1877 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1878 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1879 struct extra_kernel_map xm = {
1880 .start = event->mmap2.start,
1881 .end = event->mmap2.start + event->mmap2.len,
1882 .pgoff = event->mmap2.pgoff,
1885 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
1886 ret = machine__process_kernel_mmap_event(machine, &xm, bid);
1892 thread = machine__findnew_thread(machine, event->mmap2.pid,
1897 map = map__new(machine, event->mmap2.start,
1898 event->mmap2.len, event->mmap2.pgoff,
1899 &dso_id, event->mmap2.prot,
1900 event->mmap2.flags, bid,
1901 event->mmap2.filename, thread);
1904 goto out_problem_map;
1906 ret = thread__insert_map(thread, map);
1908 goto out_problem_insert;
1910 thread__put(thread);
1917 thread__put(thread);
1919 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1923 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1924 struct perf_sample *sample)
1926 struct thread *thread;
1932 perf_event__fprintf_mmap(event, stdout);
1934 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1935 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1936 struct extra_kernel_map xm = {
1937 .start = event->mmap.start,
1938 .end = event->mmap.start + event->mmap.len,
1939 .pgoff = event->mmap.pgoff,
1942 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1943 ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
1949 thread = machine__findnew_thread(machine, event->mmap.pid,
1954 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1957 map = map__new(machine, event->mmap.start,
1958 event->mmap.len, event->mmap.pgoff,
1959 NULL, prot, 0, NULL, event->mmap.filename, thread);
1962 goto out_problem_map;
1964 ret = thread__insert_map(thread, map);
1966 goto out_problem_insert;
1968 thread__put(thread);
1975 thread__put(thread);
1977 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1981 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1983 struct threads *threads = machine__threads(machine, th->tid);
1985 if (threads->last_match == th)
1986 threads__set_last_match(threads, NULL);
1989 down_write(&threads->lock);
1991 BUG_ON(refcount_read(&th->refcnt) == 0);
1993 rb_erase_cached(&th->rb_node, &threads->entries);
1994 RB_CLEAR_NODE(&th->rb_node);
1997 * Move it first to the dead_threads list, then drop the reference,
1998 * if this is the last reference, then the thread__delete destructor
1999 * will be called and we will remove it from the dead_threads list.
2001 list_add_tail(&th->node, &threads->dead);
2004 * We need to do the put here because if this is the last refcount,
2005 * then we will be touching the threads->dead head when removing the
2011 up_write(&threads->lock);
2014 void machine__remove_thread(struct machine *machine, struct thread *th)
2016 return __machine__remove_thread(machine, th, true);
2019 int machine__process_fork_event(struct machine *machine, union perf_event *event,
2020 struct perf_sample *sample)
2022 struct thread *thread = machine__find_thread(machine,
2025 struct thread *parent = machine__findnew_thread(machine,
2028 bool do_maps_clone = true;
2032 perf_event__fprintf_task(event, stdout);
2035 * There may be an existing thread that is not actually the parent,
2036 * either because we are processing events out of order, or because the
2037 * (fork) event that would have removed the thread was lost. Assume the
2038 * latter case and continue on as best we can.
2040 if (parent->pid_ != (pid_t)event->fork.ppid) {
2041 dump_printf("removing erroneous parent thread %d/%d\n",
2042 parent->pid_, parent->tid);
2043 machine__remove_thread(machine, parent);
2044 thread__put(parent);
2045 parent = machine__findnew_thread(machine, event->fork.ppid,
2049 /* if a thread currently exists for the thread id remove it */
2050 if (thread != NULL) {
2051 machine__remove_thread(machine, thread);
2052 thread__put(thread);
2055 thread = machine__findnew_thread(machine, event->fork.pid,
2058 * When synthesizing FORK events, we are trying to create thread
2059 * objects for the already running tasks on the machine.
2061 * Normally, for a kernel FORK event, we want to clone the parent's
2062 * maps because that is what the kernel just did.
2064 * But when synthesizing, this should not be done. If we do, we end up
2065 * with overlapping maps as we process the synthesized MMAP2 events that
2066 * get delivered shortly thereafter.
2068 * Use the FORK event misc flags in an internal way to signal this
2069 * situation, so we can elide the map clone when appropriate.
2071 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
2072 do_maps_clone = false;
2074 if (thread == NULL || parent == NULL ||
2075 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
2076 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
2079 thread__put(thread);
2080 thread__put(parent);
2085 int machine__process_exit_event(struct machine *machine, union perf_event *event,
2086 struct perf_sample *sample __maybe_unused)
2088 struct thread *thread = machine__find_thread(machine,
2093 perf_event__fprintf_task(event, stdout);
2095 if (thread != NULL) {
2096 thread__exited(thread);
2097 thread__put(thread);
2103 int machine__process_event(struct machine *machine, union perf_event *event,
2104 struct perf_sample *sample)
2108 switch (event->header.type) {
2109 case PERF_RECORD_COMM:
2110 ret = machine__process_comm_event(machine, event, sample); break;
2111 case PERF_RECORD_MMAP:
2112 ret = machine__process_mmap_event(machine, event, sample); break;
2113 case PERF_RECORD_NAMESPACES:
2114 ret = machine__process_namespaces_event(machine, event, sample); break;
2115 case PERF_RECORD_CGROUP:
2116 ret = machine__process_cgroup_event(machine, event, sample); break;
2117 case PERF_RECORD_MMAP2:
2118 ret = machine__process_mmap2_event(machine, event, sample); break;
2119 case PERF_RECORD_FORK:
2120 ret = machine__process_fork_event(machine, event, sample); break;
2121 case PERF_RECORD_EXIT:
2122 ret = machine__process_exit_event(machine, event, sample); break;
2123 case PERF_RECORD_LOST:
2124 ret = machine__process_lost_event(machine, event, sample); break;
2125 case PERF_RECORD_AUX:
2126 ret = machine__process_aux_event(machine, event); break;
2127 case PERF_RECORD_ITRACE_START:
2128 ret = machine__process_itrace_start_event(machine, event); break;
2129 case PERF_RECORD_LOST_SAMPLES:
2130 ret = machine__process_lost_samples_event(machine, event, sample); break;
2131 case PERF_RECORD_SWITCH:
2132 case PERF_RECORD_SWITCH_CPU_WIDE:
2133 ret = machine__process_switch_event(machine, event); break;
2134 case PERF_RECORD_KSYMBOL:
2135 ret = machine__process_ksymbol(machine, event, sample); break;
2136 case PERF_RECORD_BPF_EVENT:
2137 ret = machine__process_bpf(machine, event, sample); break;
2138 case PERF_RECORD_TEXT_POKE:
2139 ret = machine__process_text_poke(machine, event, sample); break;
2140 case PERF_RECORD_AUX_OUTPUT_HW_ID:
2141 ret = machine__process_aux_output_hw_id_event(machine, event); break;
2150 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
2152 if (!regexec(regex, sym->name, 0, NULL, 0))
2157 static void ip__resolve_ams(struct thread *thread,
2158 struct addr_map_symbol *ams,
2161 struct addr_location al;
2163 memset(&al, 0, sizeof(al));
2165 * We cannot use the header.misc hint to determine whether a
2166 * branch stack address is user, kernel, guest, hypervisor.
2167 * Branches may straddle the kernel/user/hypervisor boundaries.
2168 * Thus, we have to try consecutively until we find a match
2169 * or else, the symbol is unknown
2171 thread__find_cpumode_addr_location(thread, ip, &al);
2174 ams->al_addr = al.addr;
2175 ams->al_level = al.level;
2176 ams->ms.maps = al.maps;
2177 ams->ms.sym = al.sym;
2178 ams->ms.map = al.map;
2180 ams->data_page_size = 0;
2183 static void ip__resolve_data(struct thread *thread,
2184 u8 m, struct addr_map_symbol *ams,
2185 u64 addr, u64 phys_addr, u64 daddr_page_size)
2187 struct addr_location al;
2189 memset(&al, 0, sizeof(al));
2191 thread__find_symbol(thread, m, addr, &al);
2194 ams->al_addr = al.addr;
2195 ams->al_level = al.level;
2196 ams->ms.maps = al.maps;
2197 ams->ms.sym = al.sym;
2198 ams->ms.map = al.map;
2199 ams->phys_addr = phys_addr;
2200 ams->data_page_size = daddr_page_size;
2203 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
2204 struct addr_location *al)
2206 struct mem_info *mi = mem_info__new();
2211 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
2212 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
2213 sample->addr, sample->phys_addr,
2214 sample->data_page_size);
2215 mi->data_src.val = sample->data_src;
2220 static char *callchain_srcline(struct map_symbol *ms, u64 ip)
2222 struct map *map = ms->map;
2223 char *srcline = NULL;
2225 if (!map || callchain_param.key == CCKEY_FUNCTION)
2228 srcline = srcline__tree_find(&map->dso->srclines, ip);
2230 bool show_sym = false;
2231 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2233 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
2234 ms->sym, show_sym, show_addr, ip);
2235 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2246 static int add_callchain_ip(struct thread *thread,
2247 struct callchain_cursor *cursor,
2248 struct symbol **parent,
2249 struct addr_location *root_al,
2253 struct branch_flags *flags,
2254 struct iterations *iter,
2257 struct map_symbol ms;
2258 struct addr_location al;
2259 int nr_loop_iter = 0;
2260 u64 iter_cycles = 0;
2261 const char *srcline = NULL;
2267 thread__find_cpumode_addr_location(thread, ip, &al);
2269 if (ip >= PERF_CONTEXT_MAX) {
2271 case PERF_CONTEXT_HV:
2272 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2274 case PERF_CONTEXT_KERNEL:
2275 *cpumode = PERF_RECORD_MISC_KERNEL;
2277 case PERF_CONTEXT_USER:
2278 *cpumode = PERF_RECORD_MISC_USER;
2281 pr_debug("invalid callchain context: "
2282 "%"PRId64"\n", (s64) ip);
2284 * It seems the callchain is corrupted.
2287 callchain_cursor_reset(cursor);
2292 thread__find_symbol(thread, *cpumode, ip, &al);
2295 if (al.sym != NULL) {
2296 if (perf_hpp_list.parent && !*parent &&
2297 symbol__match_regex(al.sym, &parent_regex))
2299 else if (have_ignore_callees && root_al &&
2300 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2301 /* Treat this symbol as the root,
2302 forgetting its callees. */
2304 callchain_cursor_reset(cursor);
2308 if (symbol_conf.hide_unresolved && al.sym == NULL)
2312 nr_loop_iter = iter->nr_loop_iter;
2313 iter_cycles = iter->cycles;
2319 srcline = callchain_srcline(&ms, al.addr);
2320 return callchain_cursor_append(cursor, ip, &ms,
2321 branch, flags, nr_loop_iter,
2322 iter_cycles, branch_from, srcline);
2325 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2326 struct addr_location *al)
2329 const struct branch_stack *bs = sample->branch_stack;
2330 struct branch_entry *entries = perf_sample__branch_entries(sample);
2331 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2336 for (i = 0; i < bs->nr; i++) {
2337 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2338 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2339 bi[i].flags = entries[i].flags;
2344 static void save_iterations(struct iterations *iter,
2345 struct branch_entry *be, int nr)
2349 iter->nr_loop_iter++;
2352 for (i = 0; i < nr; i++)
2353 iter->cycles += be[i].flags.cycles;
2358 #define NO_ENTRY 0xff
2360 #define PERF_MAX_BRANCH_DEPTH 127
2363 static int remove_loops(struct branch_entry *l, int nr,
2364 struct iterations *iter)
2367 unsigned char chash[CHASHSZ];
2369 memset(chash, NO_ENTRY, sizeof(chash));
2371 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2373 for (i = 0; i < nr; i++) {
2374 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2376 /* no collision handling for now */
2377 if (chash[h] == NO_ENTRY) {
2379 } else if (l[chash[h]].from == l[i].from) {
2380 bool is_loop = true;
2381 /* check if it is a real loop */
2383 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2384 if (l[j].from != l[i + off].from) {
2391 save_iterations(iter + i + off,
2394 memmove(iter + i, iter + i + off,
2397 memmove(l + i, l + i + off,
2408 static int lbr_callchain_add_kernel_ip(struct thread *thread,
2409 struct callchain_cursor *cursor,
2410 struct perf_sample *sample,
2411 struct symbol **parent,
2412 struct addr_location *root_al,
2414 bool callee, int end)
2416 struct ip_callchain *chain = sample->callchain;
2417 u8 cpumode = PERF_RECORD_MISC_USER;
2421 for (i = 0; i < end + 1; i++) {
2422 err = add_callchain_ip(thread, cursor, parent,
2423 root_al, &cpumode, chain->ips[i],
2424 false, NULL, NULL, branch_from);
2431 for (i = end; i >= 0; i--) {
2432 err = add_callchain_ip(thread, cursor, parent,
2433 root_al, &cpumode, chain->ips[i],
2434 false, NULL, NULL, branch_from);
2442 static void save_lbr_cursor_node(struct thread *thread,
2443 struct callchain_cursor *cursor,
2446 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2451 if (cursor->pos == cursor->nr) {
2452 lbr_stitch->prev_lbr_cursor[idx].valid = false;
2457 cursor->curr = cursor->first;
2459 cursor->curr = cursor->curr->next;
2460 memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
2461 sizeof(struct callchain_cursor_node));
2463 lbr_stitch->prev_lbr_cursor[idx].valid = true;
2467 static int lbr_callchain_add_lbr_ip(struct thread *thread,
2468 struct callchain_cursor *cursor,
2469 struct perf_sample *sample,
2470 struct symbol **parent,
2471 struct addr_location *root_al,
2475 struct branch_stack *lbr_stack = sample->branch_stack;
2476 struct branch_entry *entries = perf_sample__branch_entries(sample);
2477 u8 cpumode = PERF_RECORD_MISC_USER;
2478 int lbr_nr = lbr_stack->nr;
2479 struct branch_flags *flags;
2484 * The curr and pos are not used in writing session. They are cleared
2485 * in callchain_cursor_commit() when the writing session is closed.
2486 * Using curr and pos to track the current cursor node.
2488 if (thread->lbr_stitch) {
2489 cursor->curr = NULL;
2490 cursor->pos = cursor->nr;
2492 cursor->curr = cursor->first;
2493 for (i = 0; i < (int)(cursor->nr - 1); i++)
2494 cursor->curr = cursor->curr->next;
2499 /* Add LBR ip from first entries.to */
2501 flags = &entries[0].flags;
2502 *branch_from = entries[0].from;
2503 err = add_callchain_ip(thread, cursor, parent,
2504 root_al, &cpumode, ip,
2511 * The number of cursor node increases.
2512 * Move the current cursor node.
2513 * But does not need to save current cursor node for entry 0.
2514 * It's impossible to stitch the whole LBRs of previous sample.
2516 if (thread->lbr_stitch && (cursor->pos != cursor->nr)) {
2518 cursor->curr = cursor->first;
2520 cursor->curr = cursor->curr->next;
2524 /* Add LBR ip from entries.from one by one. */
2525 for (i = 0; i < lbr_nr; i++) {
2526 ip = entries[i].from;
2527 flags = &entries[i].flags;
2528 err = add_callchain_ip(thread, cursor, parent,
2529 root_al, &cpumode, ip,
2534 save_lbr_cursor_node(thread, cursor, i);
2539 /* Add LBR ip from entries.from one by one. */
2540 for (i = lbr_nr - 1; i >= 0; i--) {
2541 ip = entries[i].from;
2542 flags = &entries[i].flags;
2543 err = add_callchain_ip(thread, cursor, parent,
2544 root_al, &cpumode, ip,
2549 save_lbr_cursor_node(thread, cursor, i);
2552 /* Add LBR ip from first entries.to */
2554 flags = &entries[0].flags;
2555 *branch_from = entries[0].from;
2556 err = add_callchain_ip(thread, cursor, parent,
2557 root_al, &cpumode, ip,
2566 static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
2567 struct callchain_cursor *cursor)
2569 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2570 struct callchain_cursor_node *cnode;
2571 struct stitch_list *stitch_node;
2574 list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
2575 cnode = &stitch_node->cursor;
2577 err = callchain_cursor_append(cursor, cnode->ip,
2580 &cnode->branch_flags,
2581 cnode->nr_loop_iter,
2591 static struct stitch_list *get_stitch_node(struct thread *thread)
2593 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2594 struct stitch_list *stitch_node;
2596 if (!list_empty(&lbr_stitch->free_lists)) {
2597 stitch_node = list_first_entry(&lbr_stitch->free_lists,
2598 struct stitch_list, node);
2599 list_del(&stitch_node->node);
2604 return malloc(sizeof(struct stitch_list));
2607 static bool has_stitched_lbr(struct thread *thread,
2608 struct perf_sample *cur,
2609 struct perf_sample *prev,
2610 unsigned int max_lbr,
2613 struct branch_stack *cur_stack = cur->branch_stack;
2614 struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
2615 struct branch_stack *prev_stack = prev->branch_stack;
2616 struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
2617 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2618 int i, j, nr_identical_branches = 0;
2619 struct stitch_list *stitch_node;
2620 u64 cur_base, distance;
2622 if (!cur_stack || !prev_stack)
2625 /* Find the physical index of the base-of-stack for current sample. */
2626 cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
2628 distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
2629 (max_lbr + prev_stack->hw_idx - cur_base);
2630 /* Previous sample has shorter stack. Nothing can be stitched. */
2631 if (distance + 1 > prev_stack->nr)
2635 * Check if there are identical LBRs between two samples.
2636 * Identical LBRs must have same from, to and flags values. Also,
2637 * they have to be saved in the same LBR registers (same physical
2640 * Starts from the base-of-stack of current sample.
2642 for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
2643 if ((prev_entries[i].from != cur_entries[j].from) ||
2644 (prev_entries[i].to != cur_entries[j].to) ||
2645 (prev_entries[i].flags.value != cur_entries[j].flags.value))
2647 nr_identical_branches++;
2650 if (!nr_identical_branches)
2654 * Save the LBRs between the base-of-stack of previous sample
2655 * and the base-of-stack of current sample into lbr_stitch->lists.
2656 * These LBRs will be stitched later.
2658 for (i = prev_stack->nr - 1; i > (int)distance; i--) {
2660 if (!lbr_stitch->prev_lbr_cursor[i].valid)
2663 stitch_node = get_stitch_node(thread);
2667 memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
2668 sizeof(struct callchain_cursor_node));
2671 list_add(&stitch_node->node, &lbr_stitch->lists);
2673 list_add_tail(&stitch_node->node, &lbr_stitch->lists);
2679 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
2681 if (thread->lbr_stitch)
2684 thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch));
2685 if (!thread->lbr_stitch)
2688 thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
2689 if (!thread->lbr_stitch->prev_lbr_cursor)
2690 goto free_lbr_stitch;
2692 INIT_LIST_HEAD(&thread->lbr_stitch->lists);
2693 INIT_LIST_HEAD(&thread->lbr_stitch->free_lists);
2698 zfree(&thread->lbr_stitch);
2700 pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
2701 thread->lbr_stitch_enable = false;
2706 * Resolve LBR callstack chain sample
2708 * 1 on success get LBR callchain information
2709 * 0 no available LBR callchain information, should try fp
2710 * negative error code on other errors.
2712 static int resolve_lbr_callchain_sample(struct thread *thread,
2713 struct callchain_cursor *cursor,
2714 struct perf_sample *sample,
2715 struct symbol **parent,
2716 struct addr_location *root_al,
2718 unsigned int max_lbr)
2720 bool callee = (callchain_param.order == ORDER_CALLEE);
2721 struct ip_callchain *chain = sample->callchain;
2722 int chain_nr = min(max_stack, (int)chain->nr), i;
2723 struct lbr_stitch *lbr_stitch;
2724 bool stitched_lbr = false;
2725 u64 branch_from = 0;
2728 for (i = 0; i < chain_nr; i++) {
2729 if (chain->ips[i] == PERF_CONTEXT_USER)
2733 /* LBR only affects the user callchain */
2737 if (thread->lbr_stitch_enable && !sample->no_hw_idx &&
2738 (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
2739 lbr_stitch = thread->lbr_stitch;
2741 stitched_lbr = has_stitched_lbr(thread, sample,
2742 &lbr_stitch->prev_sample,
2745 if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2746 list_replace_init(&lbr_stitch->lists,
2747 &lbr_stitch->free_lists);
2749 memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
2754 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2755 parent, root_al, branch_from,
2760 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2761 root_al, &branch_from, true);
2766 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2773 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2777 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2778 root_al, &branch_from, false);
2783 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2784 parent, root_al, branch_from,
2792 return (err < 0) ? err : 0;
2795 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2796 struct callchain_cursor *cursor,
2797 struct symbol **parent,
2798 struct addr_location *root_al,
2799 u8 *cpumode, int ent)
2803 while (--ent >= 0) {
2804 u64 ip = chain->ips[ent];
2806 if (ip >= PERF_CONTEXT_MAX) {
2807 err = add_callchain_ip(thread, cursor, parent,
2808 root_al, cpumode, ip,
2809 false, NULL, NULL, 0);
2816 static u64 get_leaf_frame_caller(struct perf_sample *sample,
2817 struct thread *thread, int usr_idx)
2819 if (machine__normalized_is(thread->maps->machine, "arm64"))
2820 return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
2825 static int thread__resolve_callchain_sample(struct thread *thread,
2826 struct callchain_cursor *cursor,
2827 struct evsel *evsel,
2828 struct perf_sample *sample,
2829 struct symbol **parent,
2830 struct addr_location *root_al,
2833 struct branch_stack *branch = sample->branch_stack;
2834 struct branch_entry *entries = perf_sample__branch_entries(sample);
2835 struct ip_callchain *chain = sample->callchain;
2837 u8 cpumode = PERF_RECORD_MISC_USER;
2838 int i, j, err, nr_entries, usr_idx;
2841 u64 leaf_frame_caller;
2844 chain_nr = chain->nr;
2846 if (evsel__has_branch_callstack(evsel)) {
2847 struct perf_env *env = evsel__env(evsel);
2849 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2851 !env ? 0 : env->max_branches);
2853 return (err < 0) ? err : 0;
2857 * Based on DWARF debug information, some architectures skip
2858 * a callchain entry saved by the kernel.
2860 skip_idx = arch_skip_callchain_idx(thread, chain);
2863 * Add branches to call stack for easier browsing. This gives
2864 * more context for a sample than just the callers.
2866 * This uses individual histograms of paths compared to the
2867 * aggregated histograms the normal LBR mode uses.
2869 * Limitations for now:
2870 * - No extra filters
2871 * - No annotations (should annotate somehow)
2874 if (branch && callchain_param.branch_callstack) {
2875 int nr = min(max_stack, (int)branch->nr);
2876 struct branch_entry be[nr];
2877 struct iterations iter[nr];
2879 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2880 pr_warning("corrupted branch chain. skipping...\n");
2884 for (i = 0; i < nr; i++) {
2885 if (callchain_param.order == ORDER_CALLEE) {
2892 * Check for overlap into the callchain.
2893 * The return address is one off compared to
2894 * the branch entry. To adjust for this
2895 * assume the calling instruction is not longer
2898 if (i == skip_idx ||
2899 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2901 else if (be[i].from < chain->ips[first_call] &&
2902 be[i].from >= chain->ips[first_call] - 8)
2905 be[i] = entries[branch->nr - i - 1];
2908 memset(iter, 0, sizeof(struct iterations) * nr);
2909 nr = remove_loops(be, nr, iter);
2911 for (i = 0; i < nr; i++) {
2912 err = add_callchain_ip(thread, cursor, parent,
2919 err = add_callchain_ip(thread, cursor, parent, root_al,
2936 if (chain && callchain_param.order != ORDER_CALLEE) {
2937 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2938 &cpumode, chain->nr - first_call);
2940 return (err < 0) ? err : 0;
2942 for (i = first_call, nr_entries = 0;
2943 i < chain_nr && nr_entries < max_stack; i++) {
2946 if (callchain_param.order == ORDER_CALLEE)
2949 j = chain->nr - i - 1;
2951 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2956 if (ip < PERF_CONTEXT_MAX)
2958 else if (callchain_param.order != ORDER_CALLEE) {
2959 err = find_prev_cpumode(chain, thread, cursor, parent,
2960 root_al, &cpumode, j);
2962 return (err < 0) ? err : 0;
2967 * PERF_CONTEXT_USER allows us to locate where the user stack ends.
2968 * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
2969 * the index will be different in order to add the missing frame
2970 * at the right place.
2973 usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
2975 if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
2977 leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
2980 * check if leaf_frame_Caller != ip to not add the same
2984 if (leaf_frame_caller && leaf_frame_caller != ip) {
2986 err = add_callchain_ip(thread, cursor, parent,
2987 root_al, &cpumode, leaf_frame_caller,
2988 false, NULL, NULL, 0);
2990 return (err < 0) ? err : 0;
2994 err = add_callchain_ip(thread, cursor, parent,
2995 root_al, &cpumode, ip,
2996 false, NULL, NULL, 0);
2999 return (err < 0) ? err : 0;
3005 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
3007 struct symbol *sym = ms->sym;
3008 struct map *map = ms->map;
3009 struct inline_node *inline_node;
3010 struct inline_list *ilist;
3014 if (!symbol_conf.inline_name || !map || !sym)
3017 addr = map__map_ip(map, ip);
3018 addr = map__rip_2objdump(map, addr);
3020 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
3022 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
3025 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
3028 list_for_each_entry(ilist, &inline_node->val, list) {
3029 struct map_symbol ilist_ms = {
3032 .sym = ilist->symbol,
3034 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
3035 NULL, 0, 0, 0, ilist->srcline);
3044 static int unwind_entry(struct unwind_entry *entry, void *arg)
3046 struct callchain_cursor *cursor = arg;
3047 const char *srcline = NULL;
3048 u64 addr = entry->ip;
3050 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
3053 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
3057 * Convert entry->ip from a virtual address to an offset in
3058 * its corresponding binary.
3061 addr = map__map_ip(entry->ms.map, entry->ip);
3063 srcline = callchain_srcline(&entry->ms, addr);
3064 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
3065 false, NULL, 0, 0, 0, srcline);
3068 static int thread__resolve_callchain_unwind(struct thread *thread,
3069 struct callchain_cursor *cursor,
3070 struct evsel *evsel,
3071 struct perf_sample *sample,
3074 /* Can we do dwarf post unwind? */
3075 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
3076 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
3079 /* Bail out if nothing was captured. */
3080 if ((!sample->user_regs.regs) ||
3081 (!sample->user_stack.size))
3084 return unwind__get_entries(unwind_entry, cursor,
3085 thread, sample, max_stack, false);
3088 int thread__resolve_callchain(struct thread *thread,
3089 struct callchain_cursor *cursor,
3090 struct evsel *evsel,
3091 struct perf_sample *sample,
3092 struct symbol **parent,
3093 struct addr_location *root_al,
3098 callchain_cursor_reset(cursor);
3100 if (callchain_param.order == ORDER_CALLEE) {
3101 ret = thread__resolve_callchain_sample(thread, cursor,
3107 ret = thread__resolve_callchain_unwind(thread, cursor,
3111 ret = thread__resolve_callchain_unwind(thread, cursor,
3116 ret = thread__resolve_callchain_sample(thread, cursor,
3125 int machine__for_each_thread(struct machine *machine,
3126 int (*fn)(struct thread *thread, void *p),
3129 struct threads *threads;
3131 struct thread *thread;
3135 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3136 threads = &machine->threads[i];
3137 for (nd = rb_first_cached(&threads->entries); nd;
3139 thread = rb_entry(nd, struct thread, rb_node);
3140 rc = fn(thread, priv);
3145 list_for_each_entry(thread, &threads->dead, node) {
3146 rc = fn(thread, priv);
3154 int machines__for_each_thread(struct machines *machines,
3155 int (*fn)(struct thread *thread, void *p),
3161 rc = machine__for_each_thread(&machines->host, fn, priv);
3165 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
3166 struct machine *machine = rb_entry(nd, struct machine, rb_node);
3168 rc = machine__for_each_thread(machine, fn, priv);
3175 pid_t machine__get_current_tid(struct machine *machine, int cpu)
3177 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3179 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
3182 return machine->current_tid[cpu];
3185 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
3188 struct thread *thread;
3189 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3194 if (!machine->current_tid) {
3197 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
3198 if (!machine->current_tid)
3200 for (i = 0; i < nr_cpus; i++)
3201 machine->current_tid[i] = -1;
3204 if (cpu >= nr_cpus) {
3205 pr_err("Requested CPU %d too large. ", cpu);
3206 pr_err("Consider raising MAX_NR_CPUS\n");
3210 machine->current_tid[cpu] = tid;
3212 thread = machine__findnew_thread(machine, pid, tid);
3217 thread__put(thread);
3223 * Compares the raw arch string. N.B. see instead perf_env__arch() or
3224 * machine__normalized_is() if a normalized arch is needed.
3226 bool machine__is(struct machine *machine, const char *arch)
3228 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
3231 bool machine__normalized_is(struct machine *machine, const char *arch)
3233 return machine && !strcmp(perf_env__arch(machine->env), arch);
3236 int machine__nr_cpus_avail(struct machine *machine)
3238 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
3241 int machine__get_kernel_start(struct machine *machine)
3243 struct map *map = machine__kernel_map(machine);
3247 * The only addresses above 2^63 are kernel addresses of a 64-bit
3248 * kernel. Note that addresses are unsigned so that on a 32-bit system
3249 * all addresses including kernel addresses are less than 2^32. In
3250 * that case (32-bit system), if the kernel mapping is unknown, all
3251 * addresses will be assumed to be in user space - see
3252 * machine__kernel_ip().
3254 machine->kernel_start = 1ULL << 63;
3256 err = map__load(map);
3258 * On x86_64, PTI entry trampolines are less than the
3259 * start of kernel text, but still above 2^63. So leave
3260 * kernel_start = 1ULL << 63 for x86_64.
3262 if (!err && !machine__is(machine, "x86_64"))
3263 machine->kernel_start = map->start;
3268 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
3270 u8 addr_cpumode = cpumode;
3273 if (!machine->single_address_space)
3276 kernel_ip = machine__kernel_ip(machine, addr);
3278 case PERF_RECORD_MISC_KERNEL:
3279 case PERF_RECORD_MISC_USER:
3280 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
3281 PERF_RECORD_MISC_USER;
3283 case PERF_RECORD_MISC_GUEST_KERNEL:
3284 case PERF_RECORD_MISC_GUEST_USER:
3285 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
3286 PERF_RECORD_MISC_GUEST_USER;
3292 return addr_cpumode;
3295 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
3297 return dsos__findnew_id(&machine->dsos, filename, id);
3300 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
3302 return machine__findnew_dso_id(machine, filename, NULL);
3305 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
3307 struct machine *machine = vmachine;
3309 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
3314 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
3315 *addrp = map->unmap_ip(map, sym->start);
3319 int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
3324 list_for_each_entry(pos, &machine->dsos.head, node) {
3325 if (fn(pos, machine, priv))