1 // SPDX-License-Identifier: GPL-2.0
16 #include "map_symbol.h"
18 #include "mem-events.h"
28 #include <sys/types.h>
32 #include "linux/hash.h"
34 #include "bpf-event.h"
35 #include <internal/lib.h> // page_size
37 #include <linux/ctype.h>
38 #include <symbol/kallsyms.h>
39 #include <linux/mman.h>
40 #include <linux/string.h>
41 #include <linux/zalloc.h>
43 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
45 static struct dso *machine__kernel_dso(struct machine *machine)
47 return machine->vmlinux_map->dso;
50 static void dsos__init(struct dsos *dsos)
52 INIT_LIST_HEAD(&dsos->head);
54 init_rwsem(&dsos->lock);
57 static void machine__threads_init(struct machine *machine)
61 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
62 struct threads *threads = &machine->threads[i];
63 threads->entries = RB_ROOT_CACHED;
64 init_rwsem(&threads->lock);
66 INIT_LIST_HEAD(&threads->dead);
67 threads->last_match = NULL;
71 static int machine__set_mmap_name(struct machine *machine)
73 if (machine__is_host(machine))
74 machine->mmap_name = strdup("[kernel.kallsyms]");
75 else if (machine__is_default_guest(machine))
76 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
77 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
79 machine->mmap_name = NULL;
81 return machine->mmap_name ? 0 : -ENOMEM;
84 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
88 memset(machine, 0, sizeof(*machine));
89 maps__init(&machine->kmaps, machine);
90 RB_CLEAR_NODE(&machine->rb_node);
91 dsos__init(&machine->dsos);
93 machine__threads_init(machine);
95 machine->vdso_info = NULL;
100 machine->id_hdr_size = 0;
101 machine->kptr_restrict_warned = false;
102 machine->comm_exec = false;
103 machine->kernel_start = 0;
104 machine->vmlinux_map = NULL;
106 machine->root_dir = strdup(root_dir);
107 if (machine->root_dir == NULL)
110 if (machine__set_mmap_name(machine))
113 if (pid != HOST_KERNEL_ID) {
114 struct thread *thread = machine__findnew_thread(machine, -1,
121 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
122 thread__set_comm(thread, comm, 0);
126 machine->current_tid = NULL;
131 zfree(&machine->root_dir);
132 zfree(&machine->mmap_name);
137 struct machine *machine__new_host(void)
139 struct machine *machine = malloc(sizeof(*machine));
141 if (machine != NULL) {
142 machine__init(machine, "", HOST_KERNEL_ID);
144 if (machine__create_kernel_maps(machine) < 0)
154 struct machine *machine__new_kallsyms(void)
156 struct machine *machine = machine__new_host();
159 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
160 * ask for not using the kcore parsing code, once this one is fixed
161 * to create a map per module.
163 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
164 machine__delete(machine);
171 static void dsos__purge(struct dsos *dsos)
175 down_write(&dsos->lock);
177 list_for_each_entry_safe(pos, n, &dsos->head, node) {
178 RB_CLEAR_NODE(&pos->rb_node);
180 list_del_init(&pos->node);
184 up_write(&dsos->lock);
187 static void dsos__exit(struct dsos *dsos)
190 exit_rwsem(&dsos->lock);
193 void machine__delete_threads(struct machine *machine)
198 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
199 struct threads *threads = &machine->threads[i];
200 down_write(&threads->lock);
201 nd = rb_first_cached(&threads->entries);
203 struct thread *t = rb_entry(nd, struct thread, rb_node);
206 __machine__remove_thread(machine, t, false);
208 up_write(&threads->lock);
212 void machine__exit(struct machine *machine)
219 machine__destroy_kernel_maps(machine);
220 maps__exit(&machine->kmaps);
221 dsos__exit(&machine->dsos);
222 machine__exit_vdso(machine);
223 zfree(&machine->root_dir);
224 zfree(&machine->mmap_name);
225 zfree(&machine->current_tid);
227 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
228 struct threads *threads = &machine->threads[i];
229 struct thread *thread, *n;
231 * Forget about the dead, at this point whatever threads were
232 * left in the dead lists better have a reference count taken
233 * by who is using them, and then, when they drop those references
234 * and it finally hits zero, thread__put() will check and see that
235 * its not in the dead threads list and will not try to remove it
236 * from there, just calling thread__delete() straight away.
238 list_for_each_entry_safe(thread, n, &threads->dead, node)
239 list_del_init(&thread->node);
241 exit_rwsem(&threads->lock);
245 void machine__delete(struct machine *machine)
248 machine__exit(machine);
253 void machines__init(struct machines *machines)
255 machine__init(&machines->host, "", HOST_KERNEL_ID);
256 machines->guests = RB_ROOT_CACHED;
259 void machines__exit(struct machines *machines)
261 machine__exit(&machines->host);
265 struct machine *machines__add(struct machines *machines, pid_t pid,
266 const char *root_dir)
268 struct rb_node **p = &machines->guests.rb_root.rb_node;
269 struct rb_node *parent = NULL;
270 struct machine *pos, *machine = malloc(sizeof(*machine));
271 bool leftmost = true;
276 if (machine__init(machine, root_dir, pid) != 0) {
283 pos = rb_entry(parent, struct machine, rb_node);
292 rb_link_node(&machine->rb_node, parent, p);
293 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
298 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
302 machines->host.comm_exec = comm_exec;
304 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
305 struct machine *machine = rb_entry(nd, struct machine, rb_node);
307 machine->comm_exec = comm_exec;
311 struct machine *machines__find(struct machines *machines, pid_t pid)
313 struct rb_node **p = &machines->guests.rb_root.rb_node;
314 struct rb_node *parent = NULL;
315 struct machine *machine;
316 struct machine *default_machine = NULL;
318 if (pid == HOST_KERNEL_ID)
319 return &machines->host;
323 machine = rb_entry(parent, struct machine, rb_node);
324 if (pid < machine->pid)
326 else if (pid > machine->pid)
331 default_machine = machine;
334 return default_machine;
337 struct machine *machines__findnew(struct machines *machines, pid_t pid)
340 const char *root_dir = "";
341 struct machine *machine = machines__find(machines, pid);
343 if (machine && (machine->pid == pid))
346 if ((pid != HOST_KERNEL_ID) &&
347 (pid != DEFAULT_GUEST_KERNEL_ID) &&
348 (symbol_conf.guestmount)) {
349 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
350 if (access(path, R_OK)) {
351 static struct strlist *seen;
354 seen = strlist__new(NULL, NULL);
356 if (!strlist__has_entry(seen, path)) {
357 pr_err("Can't access file %s\n", path);
358 strlist__add(seen, path);
366 machine = machines__add(machines, pid, root_dir);
371 void machines__process_guests(struct machines *machines,
372 machine__process_t process, void *data)
376 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
377 struct machine *pos = rb_entry(nd, struct machine, rb_node);
382 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
384 struct rb_node *node;
385 struct machine *machine;
387 machines->host.id_hdr_size = id_hdr_size;
389 for (node = rb_first_cached(&machines->guests); node;
390 node = rb_next(node)) {
391 machine = rb_entry(node, struct machine, rb_node);
392 machine->id_hdr_size = id_hdr_size;
398 static void machine__update_thread_pid(struct machine *machine,
399 struct thread *th, pid_t pid)
401 struct thread *leader;
403 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
408 if (th->pid_ == th->tid)
411 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
416 leader->maps = maps__new(machine);
421 if (th->maps == leader->maps)
426 * Maps are created from MMAP events which provide the pid and
427 * tid. Consequently there never should be any maps on a thread
428 * with an unknown pid. Just print an error if there are.
430 if (!maps__empty(th->maps))
431 pr_err("Discarding thread maps for %d:%d\n",
436 th->maps = maps__get(leader->maps);
441 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
446 * Front-end cache - TID lookups come in blocks,
447 * so most of the time we dont have to look up
450 static struct thread*
451 __threads__get_last_match(struct threads *threads, struct machine *machine,
456 th = threads->last_match;
458 if (th->tid == tid) {
459 machine__update_thread_pid(machine, th, pid);
460 return thread__get(th);
463 threads->last_match = NULL;
469 static struct thread*
470 threads__get_last_match(struct threads *threads, struct machine *machine,
473 struct thread *th = NULL;
475 if (perf_singlethreaded)
476 th = __threads__get_last_match(threads, machine, pid, tid);
482 __threads__set_last_match(struct threads *threads, struct thread *th)
484 threads->last_match = th;
488 threads__set_last_match(struct threads *threads, struct thread *th)
490 if (perf_singlethreaded)
491 __threads__set_last_match(threads, th);
495 * Caller must eventually drop thread->refcnt returned with a successful
496 * lookup/new thread inserted.
498 static struct thread *____machine__findnew_thread(struct machine *machine,
499 struct threads *threads,
500 pid_t pid, pid_t tid,
503 struct rb_node **p = &threads->entries.rb_root.rb_node;
504 struct rb_node *parent = NULL;
506 bool leftmost = true;
508 th = threads__get_last_match(threads, machine, pid, tid);
514 th = rb_entry(parent, struct thread, rb_node);
516 if (th->tid == tid) {
517 threads__set_last_match(threads, th);
518 machine__update_thread_pid(machine, th, pid);
519 return thread__get(th);
533 th = thread__new(pid, tid);
535 rb_link_node(&th->rb_node, parent, p);
536 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
539 * We have to initialize maps separately after rb tree is updated.
541 * The reason is that we call machine__findnew_thread
542 * within thread__init_maps to find the thread
543 * leader and that would screwed the rb tree.
545 if (thread__init_maps(th, machine)) {
546 rb_erase_cached(&th->rb_node, &threads->entries);
547 RB_CLEAR_NODE(&th->rb_node);
552 * It is now in the rbtree, get a ref
555 threads__set_last_match(threads, th);
562 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
564 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
567 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
570 struct threads *threads = machine__threads(machine, tid);
573 down_write(&threads->lock);
574 th = __machine__findnew_thread(machine, pid, tid);
575 up_write(&threads->lock);
579 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
582 struct threads *threads = machine__threads(machine, tid);
585 down_read(&threads->lock);
586 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
587 up_read(&threads->lock);
591 struct comm *machine__thread_exec_comm(struct machine *machine,
592 struct thread *thread)
594 if (machine->comm_exec)
595 return thread__exec_comm(thread);
597 return thread__comm(thread);
600 int machine__process_comm_event(struct machine *machine, union perf_event *event,
601 struct perf_sample *sample)
603 struct thread *thread = machine__findnew_thread(machine,
606 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
610 machine->comm_exec = true;
613 perf_event__fprintf_comm(event, stdout);
615 if (thread == NULL ||
616 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
617 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
626 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
627 union perf_event *event,
628 struct perf_sample *sample __maybe_unused)
630 struct thread *thread = machine__findnew_thread(machine,
631 event->namespaces.pid,
632 event->namespaces.tid);
635 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
636 "\nWARNING: kernel seems to support more namespaces than perf"
637 " tool.\nTry updating the perf tool..\n\n");
639 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
640 "\nWARNING: perf tool seems to support more namespaces than"
641 " the kernel.\nTry updating the kernel..\n\n");
644 perf_event__fprintf_namespaces(event, stdout);
646 if (thread == NULL ||
647 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
648 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
657 int machine__process_lost_event(struct machine *machine __maybe_unused,
658 union perf_event *event, struct perf_sample *sample __maybe_unused)
660 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
661 event->lost.id, event->lost.lost);
665 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
666 union perf_event *event, struct perf_sample *sample)
668 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
669 sample->id, event->lost_samples.lost);
673 static struct dso *machine__findnew_module_dso(struct machine *machine,
675 const char *filename)
679 down_write(&machine->dsos.lock);
681 dso = __dsos__find(&machine->dsos, m->name, true);
683 dso = __dsos__addnew(&machine->dsos, m->name);
687 dso__set_module_info(dso, m, machine);
688 dso__set_long_name(dso, strdup(filename), true);
689 dso->kernel = DSO_TYPE_KERNEL;
694 up_write(&machine->dsos.lock);
698 int machine__process_aux_event(struct machine *machine __maybe_unused,
699 union perf_event *event)
702 perf_event__fprintf_aux(event, stdout);
706 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
707 union perf_event *event)
710 perf_event__fprintf_itrace_start(event, stdout);
714 int machine__process_switch_event(struct machine *machine __maybe_unused,
715 union perf_event *event)
718 perf_event__fprintf_switch(event, stdout);
722 static int machine__process_ksymbol_register(struct machine *machine,
723 union perf_event *event,
724 struct perf_sample *sample __maybe_unused)
727 struct map *map = maps__find(&machine->kmaps, event->ksymbol.addr);
730 struct dso *dso = dso__new(event->ksymbol.name);
733 dso->kernel = DSO_TYPE_KERNEL;
734 map = map__new2(0, dso);
742 map->start = event->ksymbol.addr;
743 map->end = map->start + event->ksymbol.len;
744 maps__insert(&machine->kmaps, map);
747 sym = symbol__new(map->map_ip(map, map->start),
749 0, 0, event->ksymbol.name);
752 dso__insert_symbol(map->dso, sym);
756 static int machine__process_ksymbol_unregister(struct machine *machine,
757 union perf_event *event,
758 struct perf_sample *sample __maybe_unused)
762 map = maps__find(&machine->kmaps, event->ksymbol.addr);
764 maps__remove(&machine->kmaps, map);
769 int machine__process_ksymbol(struct machine *machine __maybe_unused,
770 union perf_event *event,
771 struct perf_sample *sample)
774 perf_event__fprintf_ksymbol(event, stdout);
776 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
777 return machine__process_ksymbol_unregister(machine, event,
779 return machine__process_ksymbol_register(machine, event, sample);
782 static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
783 const char *filename)
785 struct map *map = NULL;
789 if (kmod_path__parse_name(&m, filename))
792 dso = machine__findnew_module_dso(machine, &m, filename);
796 map = map__new2(start, dso);
800 maps__insert(&machine->kmaps, map);
802 /* Put the map here because maps__insert alread got it */
805 /* put the dso here, corresponding to machine__findnew_module_dso */
811 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
814 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
816 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
817 struct machine *pos = rb_entry(nd, struct machine, rb_node);
818 ret += __dsos__fprintf(&pos->dsos.head, fp);
824 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
825 bool (skip)(struct dso *dso, int parm), int parm)
827 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
830 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
831 bool (skip)(struct dso *dso, int parm), int parm)
834 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
836 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
837 struct machine *pos = rb_entry(nd, struct machine, rb_node);
838 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
843 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
847 struct dso *kdso = machine__kernel_dso(machine);
849 if (kdso->has_build_id) {
850 char filename[PATH_MAX];
851 if (dso__build_id_filename(kdso, filename, sizeof(filename),
853 printed += fprintf(fp, "[0] %s\n", filename);
856 for (i = 0; i < vmlinux_path__nr_entries; ++i)
857 printed += fprintf(fp, "[%d] %s\n",
858 i + kdso->has_build_id, vmlinux_path[i]);
863 size_t machine__fprintf(struct machine *machine, FILE *fp)
869 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
870 struct threads *threads = &machine->threads[i];
872 down_read(&threads->lock);
874 ret = fprintf(fp, "Threads: %u\n", threads->nr);
876 for (nd = rb_first_cached(&threads->entries); nd;
878 struct thread *pos = rb_entry(nd, struct thread, rb_node);
880 ret += thread__fprintf(pos, fp);
883 up_read(&threads->lock);
888 static struct dso *machine__get_kernel(struct machine *machine)
890 const char *vmlinux_name = machine->mmap_name;
893 if (machine__is_host(machine)) {
894 if (symbol_conf.vmlinux_name)
895 vmlinux_name = symbol_conf.vmlinux_name;
897 kernel = machine__findnew_kernel(machine, vmlinux_name,
898 "[kernel]", DSO_TYPE_KERNEL);
900 if (symbol_conf.default_guest_vmlinux_name)
901 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
903 kernel = machine__findnew_kernel(machine, vmlinux_name,
905 DSO_TYPE_GUEST_KERNEL);
908 if (kernel != NULL && (!kernel->has_build_id))
909 dso__read_running_kernel_build_id(kernel, machine);
914 struct process_args {
918 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
921 if (machine__is_default_guest(machine))
922 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
924 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
927 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
929 /* Figure out the start address of kernel map from /proc/kallsyms.
930 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
931 * symbol_name if it's not that important.
933 static int machine__get_running_kernel_start(struct machine *machine,
934 const char **symbol_name,
935 u64 *start, u64 *end)
937 char filename[PATH_MAX];
942 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
944 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
947 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
948 err = kallsyms__get_function_start(filename, name, &addr);
961 err = kallsyms__get_function_start(filename, "_etext", &addr);
968 int machine__create_extra_kernel_map(struct machine *machine,
970 struct extra_kernel_map *xm)
975 map = map__new2(xm->start, kernel);
980 map->pgoff = xm->pgoff;
982 kmap = map__kmap(map);
984 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
986 maps__insert(&machine->kmaps, map);
988 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
989 kmap->name, map->start, map->end);
996 static u64 find_entry_trampoline(struct dso *dso)
998 /* Duplicates are removed so lookup all aliases */
999 const char *syms[] = {
1000 "_entry_trampoline",
1001 "__entry_trampoline_start",
1002 "entry_SYSCALL_64_trampoline",
1004 struct symbol *sym = dso__first_symbol(dso);
1007 for (; sym; sym = dso__next_symbol(sym)) {
1008 if (sym->binding != STB_GLOBAL)
1010 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1011 if (!strcmp(sym->name, syms[i]))
1020 * These values can be used for kernels that do not have symbols for the entry
1021 * trampolines in kallsyms.
1023 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1024 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1025 #define X86_64_ENTRY_TRAMPOLINE 0x6000
1027 /* Map x86_64 PTI entry trampolines */
1028 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1031 struct maps *kmaps = &machine->kmaps;
1032 int nr_cpus_avail, cpu;
1038 * In the vmlinux case, pgoff is a virtual address which must now be
1039 * mapped to a vmlinux offset.
1041 maps__for_each_entry(kmaps, map) {
1042 struct kmap *kmap = __map__kmap(map);
1043 struct map *dest_map;
1045 if (!kmap || !is_entry_trampoline(kmap->name))
1048 dest_map = maps__find(kmaps, map->pgoff);
1049 if (dest_map != map)
1050 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1053 if (found || machine->trampolines_mapped)
1056 pgoff = find_entry_trampoline(kernel);
1060 nr_cpus_avail = machine__nr_cpus_avail(machine);
1062 /* Add a 1 page map for each CPU's entry trampoline */
1063 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1064 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1065 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1066 X86_64_ENTRY_TRAMPOLINE;
1067 struct extra_kernel_map xm = {
1069 .end = va + page_size,
1073 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1075 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1079 machine->trampolines_mapped = nr_cpus_avail;
1084 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1085 struct dso *kernel __maybe_unused)
1091 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1093 /* In case of renewal the kernel map, destroy previous one */
1094 machine__destroy_kernel_maps(machine);
1096 machine->vmlinux_map = map__new2(0, kernel);
1097 if (machine->vmlinux_map == NULL)
1100 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1101 maps__insert(&machine->kmaps, machine->vmlinux_map);
1105 void machine__destroy_kernel_maps(struct machine *machine)
1108 struct map *map = machine__kernel_map(machine);
1113 kmap = map__kmap(map);
1114 maps__remove(&machine->kmaps, map);
1115 if (kmap && kmap->ref_reloc_sym) {
1116 zfree((char **)&kmap->ref_reloc_sym->name);
1117 zfree(&kmap->ref_reloc_sym);
1120 map__zput(machine->vmlinux_map);
1123 int machines__create_guest_kernel_maps(struct machines *machines)
1126 struct dirent **namelist = NULL;
1128 char path[PATH_MAX];
1132 if (symbol_conf.default_guest_vmlinux_name ||
1133 symbol_conf.default_guest_modules ||
1134 symbol_conf.default_guest_kallsyms) {
1135 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1138 if (symbol_conf.guestmount) {
1139 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1142 for (i = 0; i < items; i++) {
1143 if (!isdigit(namelist[i]->d_name[0])) {
1144 /* Filter out . and .. */
1147 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1148 if ((*endp != '\0') ||
1149 (endp == namelist[i]->d_name) ||
1150 (errno == ERANGE)) {
1151 pr_debug("invalid directory (%s). Skipping.\n",
1152 namelist[i]->d_name);
1155 sprintf(path, "%s/%s/proc/kallsyms",
1156 symbol_conf.guestmount,
1157 namelist[i]->d_name);
1158 ret = access(path, R_OK);
1160 pr_debug("Can't access file %s\n", path);
1163 machines__create_kernel_maps(machines, pid);
1172 void machines__destroy_kernel_maps(struct machines *machines)
1174 struct rb_node *next = rb_first_cached(&machines->guests);
1176 machine__destroy_kernel_maps(&machines->host);
1179 struct machine *pos = rb_entry(next, struct machine, rb_node);
1181 next = rb_next(&pos->rb_node);
1182 rb_erase_cached(&pos->rb_node, &machines->guests);
1183 machine__delete(pos);
1187 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1189 struct machine *machine = machines__findnew(machines, pid);
1191 if (machine == NULL)
1194 return machine__create_kernel_maps(machine);
1197 int machine__load_kallsyms(struct machine *machine, const char *filename)
1199 struct map *map = machine__kernel_map(machine);
1200 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
1203 dso__set_loaded(map->dso);
1205 * Since /proc/kallsyms will have multiple sessions for the
1206 * kernel, with modules between them, fixup the end of all
1209 maps__fixup_end(&machine->kmaps);
1215 int machine__load_vmlinux_path(struct machine *machine)
1217 struct map *map = machine__kernel_map(machine);
1218 int ret = dso__load_vmlinux_path(map->dso, map);
1221 dso__set_loaded(map->dso);
1226 static char *get_kernel_version(const char *root_dir)
1228 char version[PATH_MAX];
1231 const char *prefix = "Linux version ";
1233 sprintf(version, "%s/proc/version", root_dir);
1234 file = fopen(version, "r");
1238 tmp = fgets(version, sizeof(version), file);
1243 name = strstr(version, prefix);
1246 name += strlen(prefix);
1247 tmp = strchr(name, ' ');
1251 return strdup(name);
1254 static bool is_kmod_dso(struct dso *dso)
1256 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1257 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1260 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1263 struct map *map = maps__find_by_name(maps, m->name);
1268 long_name = strdup(path);
1269 if (long_name == NULL)
1272 dso__set_long_name(map->dso, long_name, true);
1273 dso__kernel_module_get_build_id(map->dso, "");
1276 * Full name could reveal us kmod compression, so
1277 * we need to update the symtab_type if needed.
1279 if (m->comp && is_kmod_dso(map->dso)) {
1280 map->dso->symtab_type++;
1281 map->dso->comp = m->comp;
1287 static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
1289 struct dirent *dent;
1290 DIR *dir = opendir(dir_name);
1294 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1298 while ((dent = readdir(dir)) != NULL) {
1299 char path[PATH_MAX];
1302 /*sshfs might return bad dent->d_type, so we have to stat*/
1303 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1304 if (stat(path, &st))
1307 if (S_ISDIR(st.st_mode)) {
1308 if (!strcmp(dent->d_name, ".") ||
1309 !strcmp(dent->d_name, ".."))
1312 /* Do not follow top-level source and build symlinks */
1314 if (!strcmp(dent->d_name, "source") ||
1315 !strcmp(dent->d_name, "build"))
1319 ret = maps__set_modules_path_dir(maps, path, depth + 1);
1325 ret = kmod_path__parse_name(&m, dent->d_name);
1330 ret = maps__set_module_path(maps, path, &m);
1344 static int machine__set_modules_path(struct machine *machine)
1347 char modules_path[PATH_MAX];
1349 version = get_kernel_version(machine->root_dir);
1353 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1354 machine->root_dir, version);
1357 return maps__set_modules_path_dir(&machine->kmaps, modules_path, 0);
1359 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1360 u64 *size __maybe_unused,
1361 const char *name __maybe_unused)
1366 static int machine__create_module(void *arg, const char *name, u64 start,
1369 struct machine *machine = arg;
1372 if (arch__fix_module_text_start(&start, &size, name) < 0)
1375 map = machine__addnew_module_map(machine, start, name);
1378 map->end = start + size;
1380 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1385 static int machine__create_modules(struct machine *machine)
1387 const char *modules;
1388 char path[PATH_MAX];
1390 if (machine__is_default_guest(machine)) {
1391 modules = symbol_conf.default_guest_modules;
1393 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1397 if (symbol__restricted_filename(modules, "/proc/modules"))
1400 if (modules__parse(modules, machine, machine__create_module))
1403 if (!machine__set_modules_path(machine))
1406 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1411 static void machine__set_kernel_mmap(struct machine *machine,
1414 machine->vmlinux_map->start = start;
1415 machine->vmlinux_map->end = end;
1417 * Be a bit paranoid here, some perf.data file came with
1418 * a zero sized synthesized MMAP event for the kernel.
1420 if (start == 0 && end == 0)
1421 machine->vmlinux_map->end = ~0ULL;
1424 static void machine__update_kernel_mmap(struct machine *machine,
1427 struct map *map = machine__kernel_map(machine);
1430 maps__remove(&machine->kmaps, map);
1432 machine__set_kernel_mmap(machine, start, end);
1434 maps__insert(&machine->kmaps, map);
1438 int machine__create_kernel_maps(struct machine *machine)
1440 struct dso *kernel = machine__get_kernel(machine);
1441 const char *name = NULL;
1443 u64 start = 0, end = ~0ULL;
1449 ret = __machine__create_kernel_maps(machine, kernel);
1453 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1454 if (machine__is_host(machine))
1455 pr_debug("Problems creating module maps, "
1456 "continuing anyway...\n");
1458 pr_debug("Problems creating module maps for guest %d, "
1459 "continuing anyway...\n", machine->pid);
1462 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1464 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1465 machine__destroy_kernel_maps(machine);
1471 * we have a real start address now, so re-order the kmaps
1472 * assume it's the last in the kmaps
1474 machine__update_kernel_mmap(machine, start, end);
1477 if (machine__create_extra_kernel_maps(machine, kernel))
1478 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1481 /* update end address of the kernel map using adjacent module address */
1482 map = map__next(machine__kernel_map(machine));
1484 machine__set_kernel_mmap(machine, start, map->start);
1492 static bool machine__uses_kcore(struct machine *machine)
1496 list_for_each_entry(dso, &machine->dsos.head, node) {
1497 if (dso__is_kcore(dso))
1504 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1505 union perf_event *event)
1507 return machine__is(machine, "x86_64") &&
1508 is_entry_trampoline(event->mmap.filename);
1511 static int machine__process_extra_kernel_map(struct machine *machine,
1512 union perf_event *event)
1514 struct dso *kernel = machine__kernel_dso(machine);
1515 struct extra_kernel_map xm = {
1516 .start = event->mmap.start,
1517 .end = event->mmap.start + event->mmap.len,
1518 .pgoff = event->mmap.pgoff,
1524 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1526 return machine__create_extra_kernel_map(machine, kernel, &xm);
1529 static int machine__process_kernel_mmap_event(struct machine *machine,
1530 union perf_event *event)
1533 enum dso_kernel_type kernel_type;
1534 bool is_kernel_mmap;
1536 /* If we have maps from kcore then we do not need or want any others */
1537 if (machine__uses_kcore(machine))
1540 if (machine__is_host(machine))
1541 kernel_type = DSO_TYPE_KERNEL;
1543 kernel_type = DSO_TYPE_GUEST_KERNEL;
1545 is_kernel_mmap = memcmp(event->mmap.filename,
1547 strlen(machine->mmap_name) - 1) == 0;
1548 if (event->mmap.filename[0] == '/' ||
1549 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1550 map = machine__addnew_module_map(machine, event->mmap.start,
1551 event->mmap.filename);
1555 map->end = map->start + event->mmap.len;
1556 } else if (is_kernel_mmap) {
1557 const char *symbol_name = (event->mmap.filename +
1558 strlen(machine->mmap_name));
1560 * Should be there already, from the build-id table in
1563 struct dso *kernel = NULL;
1566 down_read(&machine->dsos.lock);
1568 list_for_each_entry(dso, &machine->dsos.head, node) {
1571 * The cpumode passed to is_kernel_module is not the
1572 * cpumode of *this* event. If we insist on passing
1573 * correct cpumode to is_kernel_module, we should
1574 * record the cpumode when we adding this dso to the
1577 * However we don't really need passing correct
1578 * cpumode. We know the correct cpumode must be kernel
1579 * mode (if not, we should not link it onto kernel_dsos
1582 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1583 * is_kernel_module() treats it as a kernel cpumode.
1587 is_kernel_module(dso->long_name,
1588 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1596 up_read(&machine->dsos.lock);
1599 kernel = machine__findnew_dso(machine, machine->mmap_name);
1603 kernel->kernel = kernel_type;
1604 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1609 if (strstr(kernel->long_name, "vmlinux"))
1610 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1612 machine__update_kernel_mmap(machine, event->mmap.start,
1613 event->mmap.start + event->mmap.len);
1616 * Avoid using a zero address (kptr_restrict) for the ref reloc
1617 * symbol. Effectively having zero here means that at record
1618 * time /proc/sys/kernel/kptr_restrict was non zero.
1620 if (event->mmap.pgoff != 0) {
1621 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1626 if (machine__is_default_guest(machine)) {
1628 * preload dso of guest kernel and modules
1630 dso__load(kernel, machine__kernel_map(machine));
1632 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1633 return machine__process_extra_kernel_map(machine, event);
1640 int machine__process_mmap2_event(struct machine *machine,
1641 union perf_event *event,
1642 struct perf_sample *sample)
1644 struct thread *thread;
1646 struct dso_id dso_id = {
1647 .maj = event->mmap2.maj,
1648 .min = event->mmap2.min,
1649 .ino = event->mmap2.ino,
1650 .ino_generation = event->mmap2.ino_generation,
1655 perf_event__fprintf_mmap2(event, stdout);
1657 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1658 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1659 ret = machine__process_kernel_mmap_event(machine, event);
1665 thread = machine__findnew_thread(machine, event->mmap2.pid,
1670 map = map__new(machine, event->mmap2.start,
1671 event->mmap2.len, event->mmap2.pgoff,
1672 &dso_id, event->mmap2.prot,
1674 event->mmap2.filename, thread);
1677 goto out_problem_map;
1679 ret = thread__insert_map(thread, map);
1681 goto out_problem_insert;
1683 thread__put(thread);
1690 thread__put(thread);
1692 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1696 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1697 struct perf_sample *sample)
1699 struct thread *thread;
1705 perf_event__fprintf_mmap(event, stdout);
1707 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1708 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1709 ret = machine__process_kernel_mmap_event(machine, event);
1715 thread = machine__findnew_thread(machine, event->mmap.pid,
1720 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1723 map = map__new(machine, event->mmap.start,
1724 event->mmap.len, event->mmap.pgoff,
1725 NULL, prot, 0, event->mmap.filename, thread);
1728 goto out_problem_map;
1730 ret = thread__insert_map(thread, map);
1732 goto out_problem_insert;
1734 thread__put(thread);
1741 thread__put(thread);
1743 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1747 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1749 struct threads *threads = machine__threads(machine, th->tid);
1751 if (threads->last_match == th)
1752 threads__set_last_match(threads, NULL);
1755 down_write(&threads->lock);
1757 BUG_ON(refcount_read(&th->refcnt) == 0);
1759 rb_erase_cached(&th->rb_node, &threads->entries);
1760 RB_CLEAR_NODE(&th->rb_node);
1763 * Move it first to the dead_threads list, then drop the reference,
1764 * if this is the last reference, then the thread__delete destructor
1765 * will be called and we will remove it from the dead_threads list.
1767 list_add_tail(&th->node, &threads->dead);
1770 * We need to do the put here because if this is the last refcount,
1771 * then we will be touching the threads->dead head when removing the
1777 up_write(&threads->lock);
1780 void machine__remove_thread(struct machine *machine, struct thread *th)
1782 return __machine__remove_thread(machine, th, true);
1785 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1786 struct perf_sample *sample)
1788 struct thread *thread = machine__find_thread(machine,
1791 struct thread *parent = machine__findnew_thread(machine,
1794 bool do_maps_clone = true;
1798 perf_event__fprintf_task(event, stdout);
1801 * There may be an existing thread that is not actually the parent,
1802 * either because we are processing events out of order, or because the
1803 * (fork) event that would have removed the thread was lost. Assume the
1804 * latter case and continue on as best we can.
1806 if (parent->pid_ != (pid_t)event->fork.ppid) {
1807 dump_printf("removing erroneous parent thread %d/%d\n",
1808 parent->pid_, parent->tid);
1809 machine__remove_thread(machine, parent);
1810 thread__put(parent);
1811 parent = machine__findnew_thread(machine, event->fork.ppid,
1815 /* if a thread currently exists for the thread id remove it */
1816 if (thread != NULL) {
1817 machine__remove_thread(machine, thread);
1818 thread__put(thread);
1821 thread = machine__findnew_thread(machine, event->fork.pid,
1824 * When synthesizing FORK events, we are trying to create thread
1825 * objects for the already running tasks on the machine.
1827 * Normally, for a kernel FORK event, we want to clone the parent's
1828 * maps because that is what the kernel just did.
1830 * But when synthesizing, this should not be done. If we do, we end up
1831 * with overlapping maps as we process the sythesized MMAP2 events that
1832 * get delivered shortly thereafter.
1834 * Use the FORK event misc flags in an internal way to signal this
1835 * situation, so we can elide the map clone when appropriate.
1837 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1838 do_maps_clone = false;
1840 if (thread == NULL || parent == NULL ||
1841 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
1842 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1845 thread__put(thread);
1846 thread__put(parent);
1851 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1852 struct perf_sample *sample __maybe_unused)
1854 struct thread *thread = machine__find_thread(machine,
1859 perf_event__fprintf_task(event, stdout);
1861 if (thread != NULL) {
1862 thread__exited(thread);
1863 thread__put(thread);
1869 int machine__process_event(struct machine *machine, union perf_event *event,
1870 struct perf_sample *sample)
1874 switch (event->header.type) {
1875 case PERF_RECORD_COMM:
1876 ret = machine__process_comm_event(machine, event, sample); break;
1877 case PERF_RECORD_MMAP:
1878 ret = machine__process_mmap_event(machine, event, sample); break;
1879 case PERF_RECORD_NAMESPACES:
1880 ret = machine__process_namespaces_event(machine, event, sample); break;
1881 case PERF_RECORD_MMAP2:
1882 ret = machine__process_mmap2_event(machine, event, sample); break;
1883 case PERF_RECORD_FORK:
1884 ret = machine__process_fork_event(machine, event, sample); break;
1885 case PERF_RECORD_EXIT:
1886 ret = machine__process_exit_event(machine, event, sample); break;
1887 case PERF_RECORD_LOST:
1888 ret = machine__process_lost_event(machine, event, sample); break;
1889 case PERF_RECORD_AUX:
1890 ret = machine__process_aux_event(machine, event); break;
1891 case PERF_RECORD_ITRACE_START:
1892 ret = machine__process_itrace_start_event(machine, event); break;
1893 case PERF_RECORD_LOST_SAMPLES:
1894 ret = machine__process_lost_samples_event(machine, event, sample); break;
1895 case PERF_RECORD_SWITCH:
1896 case PERF_RECORD_SWITCH_CPU_WIDE:
1897 ret = machine__process_switch_event(machine, event); break;
1898 case PERF_RECORD_KSYMBOL:
1899 ret = machine__process_ksymbol(machine, event, sample); break;
1900 case PERF_RECORD_BPF_EVENT:
1901 ret = machine__process_bpf(machine, event, sample); break;
1910 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1912 if (!regexec(regex, sym->name, 0, NULL, 0))
1917 static void ip__resolve_ams(struct thread *thread,
1918 struct addr_map_symbol *ams,
1921 struct addr_location al;
1923 memset(&al, 0, sizeof(al));
1925 * We cannot use the header.misc hint to determine whether a
1926 * branch stack address is user, kernel, guest, hypervisor.
1927 * Branches may straddle the kernel/user/hypervisor boundaries.
1928 * Thus, we have to try consecutively until we find a match
1929 * or else, the symbol is unknown
1931 thread__find_cpumode_addr_location(thread, ip, &al);
1934 ams->al_addr = al.addr;
1935 ams->ms.maps = al.maps;
1936 ams->ms.sym = al.sym;
1937 ams->ms.map = al.map;
1941 static void ip__resolve_data(struct thread *thread,
1942 u8 m, struct addr_map_symbol *ams,
1943 u64 addr, u64 phys_addr)
1945 struct addr_location al;
1947 memset(&al, 0, sizeof(al));
1949 thread__find_symbol(thread, m, addr, &al);
1952 ams->al_addr = al.addr;
1953 ams->ms.maps = al.maps;
1954 ams->ms.sym = al.sym;
1955 ams->ms.map = al.map;
1956 ams->phys_addr = phys_addr;
1959 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1960 struct addr_location *al)
1962 struct mem_info *mi = mem_info__new();
1967 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1968 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1969 sample->addr, sample->phys_addr);
1970 mi->data_src.val = sample->data_src;
1975 static char *callchain_srcline(struct map_symbol *ms, u64 ip)
1977 struct map *map = ms->map;
1978 char *srcline = NULL;
1980 if (!map || callchain_param.key == CCKEY_FUNCTION)
1983 srcline = srcline__tree_find(&map->dso->srclines, ip);
1985 bool show_sym = false;
1986 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1988 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
1989 ms->sym, show_sym, show_addr, ip);
1990 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2001 static int add_callchain_ip(struct thread *thread,
2002 struct callchain_cursor *cursor,
2003 struct symbol **parent,
2004 struct addr_location *root_al,
2008 struct branch_flags *flags,
2009 struct iterations *iter,
2012 struct map_symbol ms;
2013 struct addr_location al;
2014 int nr_loop_iter = 0;
2015 u64 iter_cycles = 0;
2016 const char *srcline = NULL;
2021 thread__find_cpumode_addr_location(thread, ip, &al);
2023 if (ip >= PERF_CONTEXT_MAX) {
2025 case PERF_CONTEXT_HV:
2026 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2028 case PERF_CONTEXT_KERNEL:
2029 *cpumode = PERF_RECORD_MISC_KERNEL;
2031 case PERF_CONTEXT_USER:
2032 *cpumode = PERF_RECORD_MISC_USER;
2035 pr_debug("invalid callchain context: "
2036 "%"PRId64"\n", (s64) ip);
2038 * It seems the callchain is corrupted.
2041 callchain_cursor_reset(cursor);
2046 thread__find_symbol(thread, *cpumode, ip, &al);
2049 if (al.sym != NULL) {
2050 if (perf_hpp_list.parent && !*parent &&
2051 symbol__match_regex(al.sym, &parent_regex))
2053 else if (have_ignore_callees && root_al &&
2054 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2055 /* Treat this symbol as the root,
2056 forgetting its callees. */
2058 callchain_cursor_reset(cursor);
2062 if (symbol_conf.hide_unresolved && al.sym == NULL)
2066 nr_loop_iter = iter->nr_loop_iter;
2067 iter_cycles = iter->cycles;
2073 srcline = callchain_srcline(&ms, al.addr);
2074 return callchain_cursor_append(cursor, ip, &ms,
2075 branch, flags, nr_loop_iter,
2076 iter_cycles, branch_from, srcline);
2079 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2080 struct addr_location *al)
2083 const struct branch_stack *bs = sample->branch_stack;
2084 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2089 for (i = 0; i < bs->nr; i++) {
2090 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
2091 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
2092 bi[i].flags = bs->entries[i].flags;
2097 static void save_iterations(struct iterations *iter,
2098 struct branch_entry *be, int nr)
2102 iter->nr_loop_iter++;
2105 for (i = 0; i < nr; i++)
2106 iter->cycles += be[i].flags.cycles;
2111 #define NO_ENTRY 0xff
2113 #define PERF_MAX_BRANCH_DEPTH 127
2116 static int remove_loops(struct branch_entry *l, int nr,
2117 struct iterations *iter)
2120 unsigned char chash[CHASHSZ];
2122 memset(chash, NO_ENTRY, sizeof(chash));
2124 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2126 for (i = 0; i < nr; i++) {
2127 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2129 /* no collision handling for now */
2130 if (chash[h] == NO_ENTRY) {
2132 } else if (l[chash[h]].from == l[i].from) {
2133 bool is_loop = true;
2134 /* check if it is a real loop */
2136 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2137 if (l[j].from != l[i + off].from) {
2144 save_iterations(iter + i + off,
2147 memmove(iter + i, iter + i + off,
2150 memmove(l + i, l + i + off,
2162 * Recolve LBR callstack chain sample
2164 * 1 on success get LBR callchain information
2165 * 0 no available LBR callchain information, should try fp
2166 * negative error code on other errors.
2168 static int resolve_lbr_callchain_sample(struct thread *thread,
2169 struct callchain_cursor *cursor,
2170 struct perf_sample *sample,
2171 struct symbol **parent,
2172 struct addr_location *root_al,
2175 struct ip_callchain *chain = sample->callchain;
2176 int chain_nr = min(max_stack, (int)chain->nr), i;
2177 u8 cpumode = PERF_RECORD_MISC_USER;
2178 u64 ip, branch_from = 0;
2180 for (i = 0; i < chain_nr; i++) {
2181 if (chain->ips[i] == PERF_CONTEXT_USER)
2185 /* LBR only affects the user callchain */
2186 if (i != chain_nr) {
2187 struct branch_stack *lbr_stack = sample->branch_stack;
2188 int lbr_nr = lbr_stack->nr, j, k;
2190 struct branch_flags *flags;
2192 * LBR callstack can only get user call chain.
2193 * The mix_chain_nr is kernel call chain
2194 * number plus LBR user call chain number.
2195 * i is kernel call chain number,
2196 * 1 is PERF_CONTEXT_USER,
2197 * lbr_nr + 1 is the user call chain number.
2198 * For details, please refer to the comments
2199 * in callchain__printf
2201 int mix_chain_nr = i + 1 + lbr_nr + 1;
2203 for (j = 0; j < mix_chain_nr; j++) {
2208 if (callchain_param.order == ORDER_CALLEE) {
2211 else if (j > i + 1) {
2213 ip = lbr_stack->entries[k].from;
2215 flags = &lbr_stack->entries[k].flags;
2217 ip = lbr_stack->entries[0].to;
2219 flags = &lbr_stack->entries[0].flags;
2221 lbr_stack->entries[0].from;
2226 ip = lbr_stack->entries[k].from;
2228 flags = &lbr_stack->entries[k].flags;
2230 else if (j > lbr_nr)
2231 ip = chain->ips[i + 1 - (j - lbr_nr)];
2233 ip = lbr_stack->entries[0].to;
2235 flags = &lbr_stack->entries[0].flags;
2237 lbr_stack->entries[0].from;
2241 err = add_callchain_ip(thread, cursor, parent,
2242 root_al, &cpumode, ip,
2243 branch, flags, NULL,
2246 return (err < 0) ? err : 0;
2254 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2255 struct callchain_cursor *cursor,
2256 struct symbol **parent,
2257 struct addr_location *root_al,
2258 u8 *cpumode, int ent)
2262 while (--ent >= 0) {
2263 u64 ip = chain->ips[ent];
2265 if (ip >= PERF_CONTEXT_MAX) {
2266 err = add_callchain_ip(thread, cursor, parent,
2267 root_al, cpumode, ip,
2268 false, NULL, NULL, 0);
2275 static int thread__resolve_callchain_sample(struct thread *thread,
2276 struct callchain_cursor *cursor,
2277 struct evsel *evsel,
2278 struct perf_sample *sample,
2279 struct symbol **parent,
2280 struct addr_location *root_al,
2283 struct branch_stack *branch = sample->branch_stack;
2284 struct ip_callchain *chain = sample->callchain;
2286 u8 cpumode = PERF_RECORD_MISC_USER;
2287 int i, j, err, nr_entries;
2292 chain_nr = chain->nr;
2294 if (perf_evsel__has_branch_callstack(evsel)) {
2295 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2296 root_al, max_stack);
2298 return (err < 0) ? err : 0;
2302 * Based on DWARF debug information, some architectures skip
2303 * a callchain entry saved by the kernel.
2305 skip_idx = arch_skip_callchain_idx(thread, chain);
2308 * Add branches to call stack for easier browsing. This gives
2309 * more context for a sample than just the callers.
2311 * This uses individual histograms of paths compared to the
2312 * aggregated histograms the normal LBR mode uses.
2314 * Limitations for now:
2315 * - No extra filters
2316 * - No annotations (should annotate somehow)
2319 if (branch && callchain_param.branch_callstack) {
2320 int nr = min(max_stack, (int)branch->nr);
2321 struct branch_entry be[nr];
2322 struct iterations iter[nr];
2324 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2325 pr_warning("corrupted branch chain. skipping...\n");
2329 for (i = 0; i < nr; i++) {
2330 if (callchain_param.order == ORDER_CALLEE) {
2331 be[i] = branch->entries[i];
2337 * Check for overlap into the callchain.
2338 * The return address is one off compared to
2339 * the branch entry. To adjust for this
2340 * assume the calling instruction is not longer
2343 if (i == skip_idx ||
2344 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2346 else if (be[i].from < chain->ips[first_call] &&
2347 be[i].from >= chain->ips[first_call] - 8)
2350 be[i] = branch->entries[branch->nr - i - 1];
2353 memset(iter, 0, sizeof(struct iterations) * nr);
2354 nr = remove_loops(be, nr, iter);
2356 for (i = 0; i < nr; i++) {
2357 err = add_callchain_ip(thread, cursor, parent,
2364 err = add_callchain_ip(thread, cursor, parent, root_al,
2381 if (chain && callchain_param.order != ORDER_CALLEE) {
2382 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2383 &cpumode, chain->nr - first_call);
2385 return (err < 0) ? err : 0;
2387 for (i = first_call, nr_entries = 0;
2388 i < chain_nr && nr_entries < max_stack; i++) {
2391 if (callchain_param.order == ORDER_CALLEE)
2394 j = chain->nr - i - 1;
2396 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2401 if (ip < PERF_CONTEXT_MAX)
2403 else if (callchain_param.order != ORDER_CALLEE) {
2404 err = find_prev_cpumode(chain, thread, cursor, parent,
2405 root_al, &cpumode, j);
2407 return (err < 0) ? err : 0;
2411 err = add_callchain_ip(thread, cursor, parent,
2412 root_al, &cpumode, ip,
2413 false, NULL, NULL, 0);
2416 return (err < 0) ? err : 0;
2422 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
2424 struct symbol *sym = ms->sym;
2425 struct map *map = ms->map;
2426 struct inline_node *inline_node;
2427 struct inline_list *ilist;
2431 if (!symbol_conf.inline_name || !map || !sym)
2434 addr = map__map_ip(map, ip);
2435 addr = map__rip_2objdump(map, addr);
2437 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2439 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2442 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2445 list_for_each_entry(ilist, &inline_node->val, list) {
2446 struct map_symbol ilist_ms = {
2449 .sym = ilist->symbol,
2451 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
2452 NULL, 0, 0, 0, ilist->srcline);
2461 static int unwind_entry(struct unwind_entry *entry, void *arg)
2463 struct callchain_cursor *cursor = arg;
2464 const char *srcline = NULL;
2465 u64 addr = entry->ip;
2467 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
2470 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
2474 * Convert entry->ip from a virtual address to an offset in
2475 * its corresponding binary.
2478 addr = map__map_ip(entry->ms.map, entry->ip);
2480 srcline = callchain_srcline(&entry->ms, addr);
2481 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
2482 false, NULL, 0, 0, 0, srcline);
2485 static int thread__resolve_callchain_unwind(struct thread *thread,
2486 struct callchain_cursor *cursor,
2487 struct evsel *evsel,
2488 struct perf_sample *sample,
2491 /* Can we do dwarf post unwind? */
2492 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2493 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
2496 /* Bail out if nothing was captured. */
2497 if ((!sample->user_regs.regs) ||
2498 (!sample->user_stack.size))
2501 return unwind__get_entries(unwind_entry, cursor,
2502 thread, sample, max_stack);
2505 int thread__resolve_callchain(struct thread *thread,
2506 struct callchain_cursor *cursor,
2507 struct evsel *evsel,
2508 struct perf_sample *sample,
2509 struct symbol **parent,
2510 struct addr_location *root_al,
2515 callchain_cursor_reset(cursor);
2517 if (callchain_param.order == ORDER_CALLEE) {
2518 ret = thread__resolve_callchain_sample(thread, cursor,
2524 ret = thread__resolve_callchain_unwind(thread, cursor,
2528 ret = thread__resolve_callchain_unwind(thread, cursor,
2533 ret = thread__resolve_callchain_sample(thread, cursor,
2542 int machine__for_each_thread(struct machine *machine,
2543 int (*fn)(struct thread *thread, void *p),
2546 struct threads *threads;
2548 struct thread *thread;
2552 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2553 threads = &machine->threads[i];
2554 for (nd = rb_first_cached(&threads->entries); nd;
2556 thread = rb_entry(nd, struct thread, rb_node);
2557 rc = fn(thread, priv);
2562 list_for_each_entry(thread, &threads->dead, node) {
2563 rc = fn(thread, priv);
2571 int machines__for_each_thread(struct machines *machines,
2572 int (*fn)(struct thread *thread, void *p),
2578 rc = machine__for_each_thread(&machines->host, fn, priv);
2582 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
2583 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2585 rc = machine__for_each_thread(machine, fn, priv);
2592 pid_t machine__get_current_tid(struct machine *machine, int cpu)
2594 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2596 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
2599 return machine->current_tid[cpu];
2602 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2605 struct thread *thread;
2606 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2611 if (!machine->current_tid) {
2614 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
2615 if (!machine->current_tid)
2617 for (i = 0; i < nr_cpus; i++)
2618 machine->current_tid[i] = -1;
2621 if (cpu >= nr_cpus) {
2622 pr_err("Requested CPU %d too large. ", cpu);
2623 pr_err("Consider raising MAX_NR_CPUS\n");
2627 machine->current_tid[cpu] = tid;
2629 thread = machine__findnew_thread(machine, pid, tid);
2634 thread__put(thread);
2640 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2641 * normalized arch is needed.
2643 bool machine__is(struct machine *machine, const char *arch)
2645 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2648 int machine__nr_cpus_avail(struct machine *machine)
2650 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2653 int machine__get_kernel_start(struct machine *machine)
2655 struct map *map = machine__kernel_map(machine);
2659 * The only addresses above 2^63 are kernel addresses of a 64-bit
2660 * kernel. Note that addresses are unsigned so that on a 32-bit system
2661 * all addresses including kernel addresses are less than 2^32. In
2662 * that case (32-bit system), if the kernel mapping is unknown, all
2663 * addresses will be assumed to be in user space - see
2664 * machine__kernel_ip().
2666 machine->kernel_start = 1ULL << 63;
2668 err = map__load(map);
2670 * On x86_64, PTI entry trampolines are less than the
2671 * start of kernel text, but still above 2^63. So leave
2672 * kernel_start = 1ULL << 63 for x86_64.
2674 if (!err && !machine__is(machine, "x86_64"))
2675 machine->kernel_start = map->start;
2680 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2682 u8 addr_cpumode = cpumode;
2685 if (!machine->single_address_space)
2688 kernel_ip = machine__kernel_ip(machine, addr);
2690 case PERF_RECORD_MISC_KERNEL:
2691 case PERF_RECORD_MISC_USER:
2692 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2693 PERF_RECORD_MISC_USER;
2695 case PERF_RECORD_MISC_GUEST_KERNEL:
2696 case PERF_RECORD_MISC_GUEST_USER:
2697 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2698 PERF_RECORD_MISC_GUEST_USER;
2704 return addr_cpumode;
2707 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
2709 return dsos__findnew_id(&machine->dsos, filename, id);
2712 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2714 return machine__findnew_dso_id(machine, filename, NULL);
2717 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2719 struct machine *machine = vmachine;
2721 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
2726 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2727 *addrp = map->unmap_ip(map, sym->start);