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 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
91 memset(machine, 0, sizeof(*machine));
92 machine->kmaps = maps__new(machine);
93 if (machine->kmaps == NULL)
96 RB_CLEAR_NODE(&machine->rb_node);
97 dsos__init(&machine->dsos);
99 machine__threads_init(machine);
101 machine->vdso_info = NULL;
106 machine->id_hdr_size = 0;
107 machine->kptr_restrict_warned = false;
108 machine->comm_exec = false;
109 machine->kernel_start = 0;
110 machine->vmlinux_map = NULL;
112 machine->root_dir = strdup(root_dir);
113 if (machine->root_dir == NULL)
116 if (machine__set_mmap_name(machine))
119 if (pid != HOST_KERNEL_ID) {
120 struct thread *thread = machine__findnew_thread(machine, -1,
127 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
128 thread__set_comm(thread, comm, 0);
132 machine->current_tid = NULL;
137 zfree(&machine->kmaps);
138 zfree(&machine->root_dir);
139 zfree(&machine->mmap_name);
144 struct machine *machine__new_host(void)
146 struct machine *machine = malloc(sizeof(*machine));
148 if (machine != NULL) {
149 machine__init(machine, "", HOST_KERNEL_ID);
151 if (machine__create_kernel_maps(machine) < 0)
161 struct machine *machine__new_kallsyms(void)
163 struct machine *machine = machine__new_host();
166 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
167 * ask for not using the kcore parsing code, once this one is fixed
168 * to create a map per module.
170 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
171 machine__delete(machine);
178 static void dsos__purge(struct dsos *dsos)
182 down_write(&dsos->lock);
184 list_for_each_entry_safe(pos, n, &dsos->head, node) {
185 RB_CLEAR_NODE(&pos->rb_node);
187 list_del_init(&pos->node);
191 up_write(&dsos->lock);
194 static void dsos__exit(struct dsos *dsos)
197 exit_rwsem(&dsos->lock);
200 void machine__delete_threads(struct machine *machine)
205 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
206 struct threads *threads = &machine->threads[i];
207 down_write(&threads->lock);
208 nd = rb_first_cached(&threads->entries);
210 struct thread *t = rb_entry(nd, struct thread, rb_node);
213 __machine__remove_thread(machine, t, false);
215 up_write(&threads->lock);
219 void machine__exit(struct machine *machine)
226 machine__destroy_kernel_maps(machine);
227 maps__delete(machine->kmaps);
228 dsos__exit(&machine->dsos);
229 machine__exit_vdso(machine);
230 zfree(&machine->root_dir);
231 zfree(&machine->mmap_name);
232 zfree(&machine->current_tid);
234 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
235 struct threads *threads = &machine->threads[i];
236 struct thread *thread, *n;
238 * Forget about the dead, at this point whatever threads were
239 * left in the dead lists better have a reference count taken
240 * by who is using them, and then, when they drop those references
241 * and it finally hits zero, thread__put() will check and see that
242 * its not in the dead threads list and will not try to remove it
243 * from there, just calling thread__delete() straight away.
245 list_for_each_entry_safe(thread, n, &threads->dead, node)
246 list_del_init(&thread->node);
248 exit_rwsem(&threads->lock);
252 void machine__delete(struct machine *machine)
255 machine__exit(machine);
260 void machines__init(struct machines *machines)
262 machine__init(&machines->host, "", HOST_KERNEL_ID);
263 machines->guests = RB_ROOT_CACHED;
266 void machines__exit(struct machines *machines)
268 machine__exit(&machines->host);
272 struct machine *machines__add(struct machines *machines, pid_t pid,
273 const char *root_dir)
275 struct rb_node **p = &machines->guests.rb_root.rb_node;
276 struct rb_node *parent = NULL;
277 struct machine *pos, *machine = malloc(sizeof(*machine));
278 bool leftmost = true;
283 if (machine__init(machine, root_dir, pid) != 0) {
290 pos = rb_entry(parent, struct machine, rb_node);
299 rb_link_node(&machine->rb_node, parent, p);
300 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
305 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
309 machines->host.comm_exec = comm_exec;
311 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
312 struct machine *machine = rb_entry(nd, struct machine, rb_node);
314 machine->comm_exec = comm_exec;
318 struct machine *machines__find(struct machines *machines, pid_t pid)
320 struct rb_node **p = &machines->guests.rb_root.rb_node;
321 struct rb_node *parent = NULL;
322 struct machine *machine;
323 struct machine *default_machine = NULL;
325 if (pid == HOST_KERNEL_ID)
326 return &machines->host;
330 machine = rb_entry(parent, struct machine, rb_node);
331 if (pid < machine->pid)
333 else if (pid > machine->pid)
338 default_machine = machine;
341 return default_machine;
344 struct machine *machines__findnew(struct machines *machines, pid_t pid)
347 const char *root_dir = "";
348 struct machine *machine = machines__find(machines, pid);
350 if (machine && (machine->pid == pid))
353 if ((pid != HOST_KERNEL_ID) &&
354 (pid != DEFAULT_GUEST_KERNEL_ID) &&
355 (symbol_conf.guestmount)) {
356 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
357 if (access(path, R_OK)) {
358 static struct strlist *seen;
361 seen = strlist__new(NULL, NULL);
363 if (!strlist__has_entry(seen, path)) {
364 pr_err("Can't access file %s\n", path);
365 strlist__add(seen, path);
373 machine = machines__add(machines, pid, root_dir);
378 struct machine *machines__find_guest(struct machines *machines, pid_t pid)
380 struct machine *machine = machines__find(machines, pid);
383 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
387 void machines__process_guests(struct machines *machines,
388 machine__process_t process, void *data)
392 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
393 struct machine *pos = rb_entry(nd, struct machine, rb_node);
398 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
400 struct rb_node *node;
401 struct machine *machine;
403 machines->host.id_hdr_size = id_hdr_size;
405 for (node = rb_first_cached(&machines->guests); node;
406 node = rb_next(node)) {
407 machine = rb_entry(node, struct machine, rb_node);
408 machine->id_hdr_size = id_hdr_size;
414 static void machine__update_thread_pid(struct machine *machine,
415 struct thread *th, pid_t pid)
417 struct thread *leader;
419 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
424 if (th->pid_ == th->tid)
427 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
432 leader->maps = maps__new(machine);
437 if (th->maps == leader->maps)
442 * Maps are created from MMAP events which provide the pid and
443 * tid. Consequently there never should be any maps on a thread
444 * with an unknown pid. Just print an error if there are.
446 if (!maps__empty(th->maps))
447 pr_err("Discarding thread maps for %d:%d\n",
452 th->maps = maps__get(leader->maps);
457 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
462 * Front-end cache - TID lookups come in blocks,
463 * so most of the time we dont have to look up
466 static struct thread*
467 __threads__get_last_match(struct threads *threads, struct machine *machine,
472 th = threads->last_match;
474 if (th->tid == tid) {
475 machine__update_thread_pid(machine, th, pid);
476 return thread__get(th);
479 threads->last_match = NULL;
485 static struct thread*
486 threads__get_last_match(struct threads *threads, struct machine *machine,
489 struct thread *th = NULL;
491 if (perf_singlethreaded)
492 th = __threads__get_last_match(threads, machine, pid, tid);
498 __threads__set_last_match(struct threads *threads, struct thread *th)
500 threads->last_match = th;
504 threads__set_last_match(struct threads *threads, struct thread *th)
506 if (perf_singlethreaded)
507 __threads__set_last_match(threads, th);
511 * Caller must eventually drop thread->refcnt returned with a successful
512 * lookup/new thread inserted.
514 static struct thread *____machine__findnew_thread(struct machine *machine,
515 struct threads *threads,
516 pid_t pid, pid_t tid,
519 struct rb_node **p = &threads->entries.rb_root.rb_node;
520 struct rb_node *parent = NULL;
522 bool leftmost = true;
524 th = threads__get_last_match(threads, machine, pid, tid);
530 th = rb_entry(parent, struct thread, rb_node);
532 if (th->tid == tid) {
533 threads__set_last_match(threads, th);
534 machine__update_thread_pid(machine, th, pid);
535 return thread__get(th);
549 th = thread__new(pid, tid);
551 rb_link_node(&th->rb_node, parent, p);
552 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
555 * We have to initialize maps separately after rb tree is updated.
557 * The reason is that we call machine__findnew_thread
558 * within thread__init_maps to find the thread
559 * leader and that would screwed the rb tree.
561 if (thread__init_maps(th, machine)) {
562 rb_erase_cached(&th->rb_node, &threads->entries);
563 RB_CLEAR_NODE(&th->rb_node);
568 * It is now in the rbtree, get a ref
571 threads__set_last_match(threads, th);
578 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
580 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
583 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
586 struct threads *threads = machine__threads(machine, tid);
589 down_write(&threads->lock);
590 th = __machine__findnew_thread(machine, pid, tid);
591 up_write(&threads->lock);
595 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
598 struct threads *threads = machine__threads(machine, tid);
601 down_read(&threads->lock);
602 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
603 up_read(&threads->lock);
608 * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
609 * So here a single thread is created for that, but actually there is a separate
610 * idle task per cpu, so there should be one 'struct thread' per cpu, but there
611 * is only 1. That causes problems for some tools, requiring workarounds. For
612 * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
614 struct thread *machine__idle_thread(struct machine *machine)
616 struct thread *thread = machine__findnew_thread(machine, 0, 0);
618 if (!thread || thread__set_comm(thread, "swapper", 0) ||
619 thread__set_namespaces(thread, 0, NULL))
620 pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
625 struct comm *machine__thread_exec_comm(struct machine *machine,
626 struct thread *thread)
628 if (machine->comm_exec)
629 return thread__exec_comm(thread);
631 return thread__comm(thread);
634 int machine__process_comm_event(struct machine *machine, union perf_event *event,
635 struct perf_sample *sample)
637 struct thread *thread = machine__findnew_thread(machine,
640 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
644 machine->comm_exec = true;
647 perf_event__fprintf_comm(event, stdout);
649 if (thread == NULL ||
650 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
651 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
660 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
661 union perf_event *event,
662 struct perf_sample *sample __maybe_unused)
664 struct thread *thread = machine__findnew_thread(machine,
665 event->namespaces.pid,
666 event->namespaces.tid);
669 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
670 "\nWARNING: kernel seems to support more namespaces than perf"
671 " tool.\nTry updating the perf tool..\n\n");
673 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
674 "\nWARNING: perf tool seems to support more namespaces than"
675 " the kernel.\nTry updating the kernel..\n\n");
678 perf_event__fprintf_namespaces(event, stdout);
680 if (thread == NULL ||
681 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
682 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
691 int machine__process_cgroup_event(struct machine *machine,
692 union perf_event *event,
693 struct perf_sample *sample __maybe_unused)
698 perf_event__fprintf_cgroup(event, stdout);
700 cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
707 int machine__process_lost_event(struct machine *machine __maybe_unused,
708 union perf_event *event, struct perf_sample *sample __maybe_unused)
710 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
711 event->lost.id, event->lost.lost);
715 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
716 union perf_event *event, struct perf_sample *sample)
718 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
719 sample->id, event->lost_samples.lost);
723 static struct dso *machine__findnew_module_dso(struct machine *machine,
725 const char *filename)
729 down_write(&machine->dsos.lock);
731 dso = __dsos__find(&machine->dsos, m->name, true);
733 dso = __dsos__addnew(&machine->dsos, m->name);
737 dso__set_module_info(dso, m, machine);
738 dso__set_long_name(dso, strdup(filename), true);
739 dso->kernel = DSO_SPACE__KERNEL;
744 up_write(&machine->dsos.lock);
748 int machine__process_aux_event(struct machine *machine __maybe_unused,
749 union perf_event *event)
752 perf_event__fprintf_aux(event, stdout);
756 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
757 union perf_event *event)
760 perf_event__fprintf_itrace_start(event, stdout);
764 int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
765 union perf_event *event)
768 perf_event__fprintf_aux_output_hw_id(event, stdout);
772 int machine__process_switch_event(struct machine *machine __maybe_unused,
773 union perf_event *event)
776 perf_event__fprintf_switch(event, stdout);
780 static int machine__process_ksymbol_register(struct machine *machine,
781 union perf_event *event,
782 struct perf_sample *sample __maybe_unused)
785 struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
788 struct dso *dso = dso__new(event->ksymbol.name);
791 dso->kernel = DSO_SPACE__KERNEL;
792 map = map__new2(0, dso);
800 if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
801 map->dso->binary_type = DSO_BINARY_TYPE__OOL;
802 map->dso->data.file_size = event->ksymbol.len;
803 dso__set_loaded(map->dso);
806 map->start = event->ksymbol.addr;
807 map->end = map->start + event->ksymbol.len;
808 maps__insert(machine__kernel_maps(machine), map);
810 dso__set_loaded(dso);
812 if (is_bpf_image(event->ksymbol.name)) {
813 dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE;
814 dso__set_long_name(dso, "", false);
818 sym = symbol__new(map->map_ip(map, map->start),
820 0, 0, event->ksymbol.name);
823 dso__insert_symbol(map->dso, sym);
827 static int machine__process_ksymbol_unregister(struct machine *machine,
828 union perf_event *event,
829 struct perf_sample *sample __maybe_unused)
834 map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
838 if (map != machine->vmlinux_map)
839 maps__remove(machine__kernel_maps(machine), map);
841 sym = dso__find_symbol(map->dso, map->map_ip(map, map->start));
843 dso__delete_symbol(map->dso, sym);
849 int machine__process_ksymbol(struct machine *machine __maybe_unused,
850 union perf_event *event,
851 struct perf_sample *sample)
854 perf_event__fprintf_ksymbol(event, stdout);
856 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
857 return machine__process_ksymbol_unregister(machine, event,
859 return machine__process_ksymbol_register(machine, event, sample);
862 int machine__process_text_poke(struct machine *machine, union perf_event *event,
863 struct perf_sample *sample __maybe_unused)
865 struct map *map = maps__find(machine__kernel_maps(machine), event->text_poke.addr);
866 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
869 perf_event__fprintf_text_poke(event, machine, stdout);
871 if (!event->text_poke.new_len)
874 if (cpumode != PERF_RECORD_MISC_KERNEL) {
875 pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
879 if (map && map->dso) {
880 u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
884 * Kernel maps might be changed when loading symbols so loading
885 * must be done prior to using kernel maps.
888 ret = dso__data_write_cache_addr(map->dso, map, machine,
889 event->text_poke.addr,
891 event->text_poke.new_len);
892 if (ret != event->text_poke.new_len)
893 pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
894 event->text_poke.addr);
896 pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
897 event->text_poke.addr);
903 static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
904 const char *filename)
906 struct map *map = NULL;
910 if (kmod_path__parse_name(&m, filename))
913 dso = machine__findnew_module_dso(machine, &m, filename);
917 map = map__new2(start, dso);
921 maps__insert(machine__kernel_maps(machine), map);
923 /* Put the map here because maps__insert already got it */
926 /* put the dso here, corresponding to machine__findnew_module_dso */
932 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
935 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
937 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
938 struct machine *pos = rb_entry(nd, struct machine, rb_node);
939 ret += __dsos__fprintf(&pos->dsos.head, fp);
945 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
946 bool (skip)(struct dso *dso, int parm), int parm)
948 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
951 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
952 bool (skip)(struct dso *dso, int parm), int parm)
955 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
957 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
958 struct machine *pos = rb_entry(nd, struct machine, rb_node);
959 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
964 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
968 struct dso *kdso = machine__kernel_dso(machine);
970 if (kdso->has_build_id) {
971 char filename[PATH_MAX];
972 if (dso__build_id_filename(kdso, filename, sizeof(filename),
974 printed += fprintf(fp, "[0] %s\n", filename);
977 for (i = 0; i < vmlinux_path__nr_entries; ++i)
978 printed += fprintf(fp, "[%d] %s\n",
979 i + kdso->has_build_id, vmlinux_path[i]);
984 size_t machine__fprintf(struct machine *machine, FILE *fp)
990 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
991 struct threads *threads = &machine->threads[i];
993 down_read(&threads->lock);
995 ret = fprintf(fp, "Threads: %u\n", threads->nr);
997 for (nd = rb_first_cached(&threads->entries); nd;
999 struct thread *pos = rb_entry(nd, struct thread, rb_node);
1001 ret += thread__fprintf(pos, fp);
1004 up_read(&threads->lock);
1009 static struct dso *machine__get_kernel(struct machine *machine)
1011 const char *vmlinux_name = machine->mmap_name;
1014 if (machine__is_host(machine)) {
1015 if (symbol_conf.vmlinux_name)
1016 vmlinux_name = symbol_conf.vmlinux_name;
1018 kernel = machine__findnew_kernel(machine, vmlinux_name,
1019 "[kernel]", DSO_SPACE__KERNEL);
1021 if (symbol_conf.default_guest_vmlinux_name)
1022 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1024 kernel = machine__findnew_kernel(machine, vmlinux_name,
1026 DSO_SPACE__KERNEL_GUEST);
1029 if (kernel != NULL && (!kernel->has_build_id))
1030 dso__read_running_kernel_build_id(kernel, machine);
1035 struct process_args {
1039 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
1042 if (machine__is_default_guest(machine))
1043 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
1045 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
1048 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
1050 /* Figure out the start address of kernel map from /proc/kallsyms.
1051 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
1052 * symbol_name if it's not that important.
1054 static int machine__get_running_kernel_start(struct machine *machine,
1055 const char **symbol_name,
1056 u64 *start, u64 *end)
1058 char filename[PATH_MAX];
1063 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
1065 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1068 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
1069 err = kallsyms__get_function_start(filename, name, &addr);
1078 *symbol_name = name;
1082 err = kallsyms__get_function_start(filename, "_etext", &addr);
1089 int machine__create_extra_kernel_map(struct machine *machine,
1091 struct extra_kernel_map *xm)
1096 map = map__new2(xm->start, kernel);
1101 map->pgoff = xm->pgoff;
1103 kmap = map__kmap(map);
1105 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
1107 maps__insert(machine__kernel_maps(machine), map);
1109 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1110 kmap->name, map->start, map->end);
1117 static u64 find_entry_trampoline(struct dso *dso)
1119 /* Duplicates are removed so lookup all aliases */
1120 const char *syms[] = {
1121 "_entry_trampoline",
1122 "__entry_trampoline_start",
1123 "entry_SYSCALL_64_trampoline",
1125 struct symbol *sym = dso__first_symbol(dso);
1128 for (; sym; sym = dso__next_symbol(sym)) {
1129 if (sym->binding != STB_GLOBAL)
1131 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1132 if (!strcmp(sym->name, syms[i]))
1141 * These values can be used for kernels that do not have symbols for the entry
1142 * trampolines in kallsyms.
1144 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1145 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1146 #define X86_64_ENTRY_TRAMPOLINE 0x6000
1148 /* Map x86_64 PTI entry trampolines */
1149 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1152 struct maps *kmaps = machine__kernel_maps(machine);
1153 int nr_cpus_avail, cpu;
1159 * In the vmlinux case, pgoff is a virtual address which must now be
1160 * mapped to a vmlinux offset.
1162 maps__for_each_entry(kmaps, map) {
1163 struct kmap *kmap = __map__kmap(map);
1164 struct map *dest_map;
1166 if (!kmap || !is_entry_trampoline(kmap->name))
1169 dest_map = maps__find(kmaps, map->pgoff);
1170 if (dest_map != map)
1171 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1174 if (found || machine->trampolines_mapped)
1177 pgoff = find_entry_trampoline(kernel);
1181 nr_cpus_avail = machine__nr_cpus_avail(machine);
1183 /* Add a 1 page map for each CPU's entry trampoline */
1184 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1185 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1186 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1187 X86_64_ENTRY_TRAMPOLINE;
1188 struct extra_kernel_map xm = {
1190 .end = va + page_size,
1194 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1196 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1200 machine->trampolines_mapped = nr_cpus_avail;
1205 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1206 struct dso *kernel __maybe_unused)
1212 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1214 /* In case of renewal the kernel map, destroy previous one */
1215 machine__destroy_kernel_maps(machine);
1217 machine->vmlinux_map = map__new2(0, kernel);
1218 if (machine->vmlinux_map == NULL)
1221 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1222 maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
1226 void machine__destroy_kernel_maps(struct machine *machine)
1229 struct map *map = machine__kernel_map(machine);
1234 kmap = map__kmap(map);
1235 maps__remove(machine__kernel_maps(machine), map);
1236 if (kmap && kmap->ref_reloc_sym) {
1237 zfree((char **)&kmap->ref_reloc_sym->name);
1238 zfree(&kmap->ref_reloc_sym);
1241 map__zput(machine->vmlinux_map);
1244 int machines__create_guest_kernel_maps(struct machines *machines)
1247 struct dirent **namelist = NULL;
1249 char path[PATH_MAX];
1253 if (symbol_conf.default_guest_vmlinux_name ||
1254 symbol_conf.default_guest_modules ||
1255 symbol_conf.default_guest_kallsyms) {
1256 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1259 if (symbol_conf.guestmount) {
1260 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1263 for (i = 0; i < items; i++) {
1264 if (!isdigit(namelist[i]->d_name[0])) {
1265 /* Filter out . and .. */
1268 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1269 if ((*endp != '\0') ||
1270 (endp == namelist[i]->d_name) ||
1271 (errno == ERANGE)) {
1272 pr_debug("invalid directory (%s). Skipping.\n",
1273 namelist[i]->d_name);
1276 sprintf(path, "%s/%s/proc/kallsyms",
1277 symbol_conf.guestmount,
1278 namelist[i]->d_name);
1279 ret = access(path, R_OK);
1281 pr_debug("Can't access file %s\n", path);
1284 machines__create_kernel_maps(machines, pid);
1293 void machines__destroy_kernel_maps(struct machines *machines)
1295 struct rb_node *next = rb_first_cached(&machines->guests);
1297 machine__destroy_kernel_maps(&machines->host);
1300 struct machine *pos = rb_entry(next, struct machine, rb_node);
1302 next = rb_next(&pos->rb_node);
1303 rb_erase_cached(&pos->rb_node, &machines->guests);
1304 machine__delete(pos);
1308 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1310 struct machine *machine = machines__findnew(machines, pid);
1312 if (machine == NULL)
1315 return machine__create_kernel_maps(machine);
1318 int machine__load_kallsyms(struct machine *machine, const char *filename)
1320 struct map *map = machine__kernel_map(machine);
1321 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
1324 dso__set_loaded(map->dso);
1326 * Since /proc/kallsyms will have multiple sessions for the
1327 * kernel, with modules between them, fixup the end of all
1330 maps__fixup_end(machine__kernel_maps(machine));
1336 int machine__load_vmlinux_path(struct machine *machine)
1338 struct map *map = machine__kernel_map(machine);
1339 int ret = dso__load_vmlinux_path(map->dso, map);
1342 dso__set_loaded(map->dso);
1347 static char *get_kernel_version(const char *root_dir)
1349 char version[PATH_MAX];
1352 const char *prefix = "Linux version ";
1354 sprintf(version, "%s/proc/version", root_dir);
1355 file = fopen(version, "r");
1359 tmp = fgets(version, sizeof(version), file);
1364 name = strstr(version, prefix);
1367 name += strlen(prefix);
1368 tmp = strchr(name, ' ');
1372 return strdup(name);
1375 static bool is_kmod_dso(struct dso *dso)
1377 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1378 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1381 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1384 struct map *map = maps__find_by_name(maps, m->name);
1389 long_name = strdup(path);
1390 if (long_name == NULL)
1393 dso__set_long_name(map->dso, long_name, true);
1394 dso__kernel_module_get_build_id(map->dso, "");
1397 * Full name could reveal us kmod compression, so
1398 * we need to update the symtab_type if needed.
1400 if (m->comp && is_kmod_dso(map->dso)) {
1401 map->dso->symtab_type++;
1402 map->dso->comp = m->comp;
1408 static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
1410 struct dirent *dent;
1411 DIR *dir = opendir(dir_name);
1415 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1419 while ((dent = readdir(dir)) != NULL) {
1420 char path[PATH_MAX];
1423 /*sshfs might return bad dent->d_type, so we have to stat*/
1424 path__join(path, sizeof(path), dir_name, dent->d_name);
1425 if (stat(path, &st))
1428 if (S_ISDIR(st.st_mode)) {
1429 if (!strcmp(dent->d_name, ".") ||
1430 !strcmp(dent->d_name, ".."))
1433 /* Do not follow top-level source and build symlinks */
1435 if (!strcmp(dent->d_name, "source") ||
1436 !strcmp(dent->d_name, "build"))
1440 ret = maps__set_modules_path_dir(maps, path, depth + 1);
1446 ret = kmod_path__parse_name(&m, dent->d_name);
1451 ret = maps__set_module_path(maps, path, &m);
1465 static int machine__set_modules_path(struct machine *machine)
1468 char modules_path[PATH_MAX];
1470 version = get_kernel_version(machine->root_dir);
1474 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1475 machine->root_dir, version);
1478 return maps__set_modules_path_dir(machine__kernel_maps(machine), modules_path, 0);
1480 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1481 u64 *size __maybe_unused,
1482 const char *name __maybe_unused)
1487 static int machine__create_module(void *arg, const char *name, u64 start,
1490 struct machine *machine = arg;
1493 if (arch__fix_module_text_start(&start, &size, name) < 0)
1496 map = machine__addnew_module_map(machine, start, name);
1499 map->end = start + size;
1501 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1506 static int machine__create_modules(struct machine *machine)
1508 const char *modules;
1509 char path[PATH_MAX];
1511 if (machine__is_default_guest(machine)) {
1512 modules = symbol_conf.default_guest_modules;
1514 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1518 if (symbol__restricted_filename(modules, "/proc/modules"))
1521 if (modules__parse(modules, machine, machine__create_module))
1524 if (!machine__set_modules_path(machine))
1527 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1532 static void machine__set_kernel_mmap(struct machine *machine,
1535 machine->vmlinux_map->start = start;
1536 machine->vmlinux_map->end = end;
1538 * Be a bit paranoid here, some perf.data file came with
1539 * a zero sized synthesized MMAP event for the kernel.
1541 if (start == 0 && end == 0)
1542 machine->vmlinux_map->end = ~0ULL;
1545 static void machine__update_kernel_mmap(struct machine *machine,
1548 struct map *map = machine__kernel_map(machine);
1551 maps__remove(machine__kernel_maps(machine), map);
1553 machine__set_kernel_mmap(machine, start, end);
1555 maps__insert(machine__kernel_maps(machine), map);
1559 int machine__create_kernel_maps(struct machine *machine)
1561 struct dso *kernel = machine__get_kernel(machine);
1562 const char *name = NULL;
1564 u64 start = 0, end = ~0ULL;
1570 ret = __machine__create_kernel_maps(machine, kernel);
1574 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1575 if (machine__is_host(machine))
1576 pr_debug("Problems creating module maps, "
1577 "continuing anyway...\n");
1579 pr_debug("Problems creating module maps for guest %d, "
1580 "continuing anyway...\n", machine->pid);
1583 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1585 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1586 machine__destroy_kernel_maps(machine);
1592 * we have a real start address now, so re-order the kmaps
1593 * assume it's the last in the kmaps
1595 machine__update_kernel_mmap(machine, start, end);
1598 if (machine__create_extra_kernel_maps(machine, kernel))
1599 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1602 /* update end address of the kernel map using adjacent module address */
1603 map = map__next(machine__kernel_map(machine));
1605 machine__set_kernel_mmap(machine, start, map->start);
1613 static bool machine__uses_kcore(struct machine *machine)
1617 list_for_each_entry(dso, &machine->dsos.head, node) {
1618 if (dso__is_kcore(dso))
1625 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1626 struct extra_kernel_map *xm)
1628 return machine__is(machine, "x86_64") &&
1629 is_entry_trampoline(xm->name);
1632 static int machine__process_extra_kernel_map(struct machine *machine,
1633 struct extra_kernel_map *xm)
1635 struct dso *kernel = machine__kernel_dso(machine);
1640 return machine__create_extra_kernel_map(machine, kernel, xm);
1643 static int machine__process_kernel_mmap_event(struct machine *machine,
1644 struct extra_kernel_map *xm,
1645 struct build_id *bid)
1648 enum dso_space_type dso_space;
1649 bool is_kernel_mmap;
1651 /* If we have maps from kcore then we do not need or want any others */
1652 if (machine__uses_kcore(machine))
1655 if (machine__is_host(machine))
1656 dso_space = DSO_SPACE__KERNEL;
1658 dso_space = DSO_SPACE__KERNEL_GUEST;
1660 is_kernel_mmap = memcmp(xm->name, machine->mmap_name,
1661 strlen(machine->mmap_name) - 1) == 0;
1662 if (xm->name[0] == '/' ||
1663 (!is_kernel_mmap && xm->name[0] == '[')) {
1664 map = machine__addnew_module_map(machine, xm->start,
1669 map->end = map->start + xm->end - xm->start;
1671 if (build_id__is_defined(bid))
1672 dso__set_build_id(map->dso, bid);
1674 } else if (is_kernel_mmap) {
1675 const char *symbol_name = (xm->name + strlen(machine->mmap_name));
1677 * Should be there already, from the build-id table in
1680 struct dso *kernel = NULL;
1683 down_read(&machine->dsos.lock);
1685 list_for_each_entry(dso, &machine->dsos.head, node) {
1688 * The cpumode passed to is_kernel_module is not the
1689 * cpumode of *this* event. If we insist on passing
1690 * correct cpumode to is_kernel_module, we should
1691 * record the cpumode when we adding this dso to the
1694 * However we don't really need passing correct
1695 * cpumode. We know the correct cpumode must be kernel
1696 * mode (if not, we should not link it onto kernel_dsos
1699 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1700 * is_kernel_module() treats it as a kernel cpumode.
1704 is_kernel_module(dso->long_name,
1705 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1713 up_read(&machine->dsos.lock);
1716 kernel = machine__findnew_dso(machine, machine->mmap_name);
1720 kernel->kernel = dso_space;
1721 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1726 if (strstr(kernel->long_name, "vmlinux"))
1727 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1729 machine__update_kernel_mmap(machine, xm->start, xm->end);
1731 if (build_id__is_defined(bid))
1732 dso__set_build_id(kernel, bid);
1735 * Avoid using a zero address (kptr_restrict) for the ref reloc
1736 * symbol. Effectively having zero here means that at record
1737 * time /proc/sys/kernel/kptr_restrict was non zero.
1739 if (xm->pgoff != 0) {
1740 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1745 if (machine__is_default_guest(machine)) {
1747 * preload dso of guest kernel and modules
1749 dso__load(kernel, machine__kernel_map(machine));
1751 } else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
1752 return machine__process_extra_kernel_map(machine, xm);
1759 int machine__process_mmap2_event(struct machine *machine,
1760 union perf_event *event,
1761 struct perf_sample *sample)
1763 struct thread *thread;
1765 struct dso_id dso_id = {
1766 .maj = event->mmap2.maj,
1767 .min = event->mmap2.min,
1768 .ino = event->mmap2.ino,
1769 .ino_generation = event->mmap2.ino_generation,
1771 struct build_id __bid, *bid = NULL;
1775 perf_event__fprintf_mmap2(event, stdout);
1777 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
1779 build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size);
1782 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1783 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1784 struct extra_kernel_map xm = {
1785 .start = event->mmap2.start,
1786 .end = event->mmap2.start + event->mmap2.len,
1787 .pgoff = event->mmap2.pgoff,
1790 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
1791 ret = machine__process_kernel_mmap_event(machine, &xm, bid);
1797 thread = machine__findnew_thread(machine, event->mmap2.pid,
1802 map = map__new(machine, event->mmap2.start,
1803 event->mmap2.len, event->mmap2.pgoff,
1804 &dso_id, event->mmap2.prot,
1805 event->mmap2.flags, bid,
1806 event->mmap2.filename, thread);
1809 goto out_problem_map;
1811 ret = thread__insert_map(thread, map);
1813 goto out_problem_insert;
1815 thread__put(thread);
1822 thread__put(thread);
1824 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1828 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1829 struct perf_sample *sample)
1831 struct thread *thread;
1837 perf_event__fprintf_mmap(event, stdout);
1839 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1840 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1841 struct extra_kernel_map xm = {
1842 .start = event->mmap.start,
1843 .end = event->mmap.start + event->mmap.len,
1844 .pgoff = event->mmap.pgoff,
1847 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1848 ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
1854 thread = machine__findnew_thread(machine, event->mmap.pid,
1859 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1862 map = map__new(machine, event->mmap.start,
1863 event->mmap.len, event->mmap.pgoff,
1864 NULL, prot, 0, NULL, event->mmap.filename, thread);
1867 goto out_problem_map;
1869 ret = thread__insert_map(thread, map);
1871 goto out_problem_insert;
1873 thread__put(thread);
1880 thread__put(thread);
1882 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1886 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1888 struct threads *threads = machine__threads(machine, th->tid);
1890 if (threads->last_match == th)
1891 threads__set_last_match(threads, NULL);
1894 down_write(&threads->lock);
1896 BUG_ON(refcount_read(&th->refcnt) == 0);
1898 rb_erase_cached(&th->rb_node, &threads->entries);
1899 RB_CLEAR_NODE(&th->rb_node);
1902 * Move it first to the dead_threads list, then drop the reference,
1903 * if this is the last reference, then the thread__delete destructor
1904 * will be called and we will remove it from the dead_threads list.
1906 list_add_tail(&th->node, &threads->dead);
1909 * We need to do the put here because if this is the last refcount,
1910 * then we will be touching the threads->dead head when removing the
1916 up_write(&threads->lock);
1919 void machine__remove_thread(struct machine *machine, struct thread *th)
1921 return __machine__remove_thread(machine, th, true);
1924 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1925 struct perf_sample *sample)
1927 struct thread *thread = machine__find_thread(machine,
1930 struct thread *parent = machine__findnew_thread(machine,
1933 bool do_maps_clone = true;
1937 perf_event__fprintf_task(event, stdout);
1940 * There may be an existing thread that is not actually the parent,
1941 * either because we are processing events out of order, or because the
1942 * (fork) event that would have removed the thread was lost. Assume the
1943 * latter case and continue on as best we can.
1945 if (parent->pid_ != (pid_t)event->fork.ppid) {
1946 dump_printf("removing erroneous parent thread %d/%d\n",
1947 parent->pid_, parent->tid);
1948 machine__remove_thread(machine, parent);
1949 thread__put(parent);
1950 parent = machine__findnew_thread(machine, event->fork.ppid,
1954 /* if a thread currently exists for the thread id remove it */
1955 if (thread != NULL) {
1956 machine__remove_thread(machine, thread);
1957 thread__put(thread);
1960 thread = machine__findnew_thread(machine, event->fork.pid,
1963 * When synthesizing FORK events, we are trying to create thread
1964 * objects for the already running tasks on the machine.
1966 * Normally, for a kernel FORK event, we want to clone the parent's
1967 * maps because that is what the kernel just did.
1969 * But when synthesizing, this should not be done. If we do, we end up
1970 * with overlapping maps as we process the synthesized MMAP2 events that
1971 * get delivered shortly thereafter.
1973 * Use the FORK event misc flags in an internal way to signal this
1974 * situation, so we can elide the map clone when appropriate.
1976 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1977 do_maps_clone = false;
1979 if (thread == NULL || parent == NULL ||
1980 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
1981 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1984 thread__put(thread);
1985 thread__put(parent);
1990 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1991 struct perf_sample *sample __maybe_unused)
1993 struct thread *thread = machine__find_thread(machine,
1998 perf_event__fprintf_task(event, stdout);
2000 if (thread != NULL) {
2001 thread__exited(thread);
2002 thread__put(thread);
2008 int machine__process_event(struct machine *machine, union perf_event *event,
2009 struct perf_sample *sample)
2013 switch (event->header.type) {
2014 case PERF_RECORD_COMM:
2015 ret = machine__process_comm_event(machine, event, sample); break;
2016 case PERF_RECORD_MMAP:
2017 ret = machine__process_mmap_event(machine, event, sample); break;
2018 case PERF_RECORD_NAMESPACES:
2019 ret = machine__process_namespaces_event(machine, event, sample); break;
2020 case PERF_RECORD_CGROUP:
2021 ret = machine__process_cgroup_event(machine, event, sample); break;
2022 case PERF_RECORD_MMAP2:
2023 ret = machine__process_mmap2_event(machine, event, sample); break;
2024 case PERF_RECORD_FORK:
2025 ret = machine__process_fork_event(machine, event, sample); break;
2026 case PERF_RECORD_EXIT:
2027 ret = machine__process_exit_event(machine, event, sample); break;
2028 case PERF_RECORD_LOST:
2029 ret = machine__process_lost_event(machine, event, sample); break;
2030 case PERF_RECORD_AUX:
2031 ret = machine__process_aux_event(machine, event); break;
2032 case PERF_RECORD_ITRACE_START:
2033 ret = machine__process_itrace_start_event(machine, event); break;
2034 case PERF_RECORD_LOST_SAMPLES:
2035 ret = machine__process_lost_samples_event(machine, event, sample); break;
2036 case PERF_RECORD_SWITCH:
2037 case PERF_RECORD_SWITCH_CPU_WIDE:
2038 ret = machine__process_switch_event(machine, event); break;
2039 case PERF_RECORD_KSYMBOL:
2040 ret = machine__process_ksymbol(machine, event, sample); break;
2041 case PERF_RECORD_BPF_EVENT:
2042 ret = machine__process_bpf(machine, event, sample); break;
2043 case PERF_RECORD_TEXT_POKE:
2044 ret = machine__process_text_poke(machine, event, sample); break;
2045 case PERF_RECORD_AUX_OUTPUT_HW_ID:
2046 ret = machine__process_aux_output_hw_id_event(machine, event); break;
2055 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
2057 if (!regexec(regex, sym->name, 0, NULL, 0))
2062 static void ip__resolve_ams(struct thread *thread,
2063 struct addr_map_symbol *ams,
2066 struct addr_location al;
2068 memset(&al, 0, sizeof(al));
2070 * We cannot use the header.misc hint to determine whether a
2071 * branch stack address is user, kernel, guest, hypervisor.
2072 * Branches may straddle the kernel/user/hypervisor boundaries.
2073 * Thus, we have to try consecutively until we find a match
2074 * or else, the symbol is unknown
2076 thread__find_cpumode_addr_location(thread, ip, &al);
2079 ams->al_addr = al.addr;
2080 ams->al_level = al.level;
2081 ams->ms.maps = al.maps;
2082 ams->ms.sym = al.sym;
2083 ams->ms.map = al.map;
2085 ams->data_page_size = 0;
2088 static void ip__resolve_data(struct thread *thread,
2089 u8 m, struct addr_map_symbol *ams,
2090 u64 addr, u64 phys_addr, u64 daddr_page_size)
2092 struct addr_location al;
2094 memset(&al, 0, sizeof(al));
2096 thread__find_symbol(thread, m, addr, &al);
2099 ams->al_addr = al.addr;
2100 ams->al_level = al.level;
2101 ams->ms.maps = al.maps;
2102 ams->ms.sym = al.sym;
2103 ams->ms.map = al.map;
2104 ams->phys_addr = phys_addr;
2105 ams->data_page_size = daddr_page_size;
2108 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
2109 struct addr_location *al)
2111 struct mem_info *mi = mem_info__new();
2116 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
2117 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
2118 sample->addr, sample->phys_addr,
2119 sample->data_page_size);
2120 mi->data_src.val = sample->data_src;
2125 static char *callchain_srcline(struct map_symbol *ms, u64 ip)
2127 struct map *map = ms->map;
2128 char *srcline = NULL;
2130 if (!map || callchain_param.key == CCKEY_FUNCTION)
2133 srcline = srcline__tree_find(&map->dso->srclines, ip);
2135 bool show_sym = false;
2136 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2138 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
2139 ms->sym, show_sym, show_addr, ip);
2140 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2151 static int add_callchain_ip(struct thread *thread,
2152 struct callchain_cursor *cursor,
2153 struct symbol **parent,
2154 struct addr_location *root_al,
2158 struct branch_flags *flags,
2159 struct iterations *iter,
2162 struct map_symbol ms;
2163 struct addr_location al;
2164 int nr_loop_iter = 0;
2165 u64 iter_cycles = 0;
2166 const char *srcline = NULL;
2172 thread__find_cpumode_addr_location(thread, ip, &al);
2174 if (ip >= PERF_CONTEXT_MAX) {
2176 case PERF_CONTEXT_HV:
2177 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2179 case PERF_CONTEXT_KERNEL:
2180 *cpumode = PERF_RECORD_MISC_KERNEL;
2182 case PERF_CONTEXT_USER:
2183 *cpumode = PERF_RECORD_MISC_USER;
2186 pr_debug("invalid callchain context: "
2187 "%"PRId64"\n", (s64) ip);
2189 * It seems the callchain is corrupted.
2192 callchain_cursor_reset(cursor);
2197 thread__find_symbol(thread, *cpumode, ip, &al);
2200 if (al.sym != NULL) {
2201 if (perf_hpp_list.parent && !*parent &&
2202 symbol__match_regex(al.sym, &parent_regex))
2204 else if (have_ignore_callees && root_al &&
2205 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2206 /* Treat this symbol as the root,
2207 forgetting its callees. */
2209 callchain_cursor_reset(cursor);
2213 if (symbol_conf.hide_unresolved && al.sym == NULL)
2217 nr_loop_iter = iter->nr_loop_iter;
2218 iter_cycles = iter->cycles;
2224 srcline = callchain_srcline(&ms, al.addr);
2225 return callchain_cursor_append(cursor, ip, &ms,
2226 branch, flags, nr_loop_iter,
2227 iter_cycles, branch_from, srcline);
2230 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2231 struct addr_location *al)
2234 const struct branch_stack *bs = sample->branch_stack;
2235 struct branch_entry *entries = perf_sample__branch_entries(sample);
2236 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2241 for (i = 0; i < bs->nr; i++) {
2242 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2243 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2244 bi[i].flags = entries[i].flags;
2249 static void save_iterations(struct iterations *iter,
2250 struct branch_entry *be, int nr)
2254 iter->nr_loop_iter++;
2257 for (i = 0; i < nr; i++)
2258 iter->cycles += be[i].flags.cycles;
2263 #define NO_ENTRY 0xff
2265 #define PERF_MAX_BRANCH_DEPTH 127
2268 static int remove_loops(struct branch_entry *l, int nr,
2269 struct iterations *iter)
2272 unsigned char chash[CHASHSZ];
2274 memset(chash, NO_ENTRY, sizeof(chash));
2276 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2278 for (i = 0; i < nr; i++) {
2279 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2281 /* no collision handling for now */
2282 if (chash[h] == NO_ENTRY) {
2284 } else if (l[chash[h]].from == l[i].from) {
2285 bool is_loop = true;
2286 /* check if it is a real loop */
2288 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2289 if (l[j].from != l[i + off].from) {
2296 save_iterations(iter + i + off,
2299 memmove(iter + i, iter + i + off,
2302 memmove(l + i, l + i + off,
2313 static int lbr_callchain_add_kernel_ip(struct thread *thread,
2314 struct callchain_cursor *cursor,
2315 struct perf_sample *sample,
2316 struct symbol **parent,
2317 struct addr_location *root_al,
2319 bool callee, int end)
2321 struct ip_callchain *chain = sample->callchain;
2322 u8 cpumode = PERF_RECORD_MISC_USER;
2326 for (i = 0; i < end + 1; i++) {
2327 err = add_callchain_ip(thread, cursor, parent,
2328 root_al, &cpumode, chain->ips[i],
2329 false, NULL, NULL, branch_from);
2336 for (i = end; i >= 0; i--) {
2337 err = add_callchain_ip(thread, cursor, parent,
2338 root_al, &cpumode, chain->ips[i],
2339 false, NULL, NULL, branch_from);
2347 static void save_lbr_cursor_node(struct thread *thread,
2348 struct callchain_cursor *cursor,
2351 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2356 if (cursor->pos == cursor->nr) {
2357 lbr_stitch->prev_lbr_cursor[idx].valid = false;
2362 cursor->curr = cursor->first;
2364 cursor->curr = cursor->curr->next;
2365 memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
2366 sizeof(struct callchain_cursor_node));
2368 lbr_stitch->prev_lbr_cursor[idx].valid = true;
2372 static int lbr_callchain_add_lbr_ip(struct thread *thread,
2373 struct callchain_cursor *cursor,
2374 struct perf_sample *sample,
2375 struct symbol **parent,
2376 struct addr_location *root_al,
2380 struct branch_stack *lbr_stack = sample->branch_stack;
2381 struct branch_entry *entries = perf_sample__branch_entries(sample);
2382 u8 cpumode = PERF_RECORD_MISC_USER;
2383 int lbr_nr = lbr_stack->nr;
2384 struct branch_flags *flags;
2389 * The curr and pos are not used in writing session. They are cleared
2390 * in callchain_cursor_commit() when the writing session is closed.
2391 * Using curr and pos to track the current cursor node.
2393 if (thread->lbr_stitch) {
2394 cursor->curr = NULL;
2395 cursor->pos = cursor->nr;
2397 cursor->curr = cursor->first;
2398 for (i = 0; i < (int)(cursor->nr - 1); i++)
2399 cursor->curr = cursor->curr->next;
2404 /* Add LBR ip from first entries.to */
2406 flags = &entries[0].flags;
2407 *branch_from = entries[0].from;
2408 err = add_callchain_ip(thread, cursor, parent,
2409 root_al, &cpumode, ip,
2416 * The number of cursor node increases.
2417 * Move the current cursor node.
2418 * But does not need to save current cursor node for entry 0.
2419 * It's impossible to stitch the whole LBRs of previous sample.
2421 if (thread->lbr_stitch && (cursor->pos != cursor->nr)) {
2423 cursor->curr = cursor->first;
2425 cursor->curr = cursor->curr->next;
2429 /* Add LBR ip from entries.from one by one. */
2430 for (i = 0; i < lbr_nr; i++) {
2431 ip = entries[i].from;
2432 flags = &entries[i].flags;
2433 err = add_callchain_ip(thread, cursor, parent,
2434 root_al, &cpumode, ip,
2439 save_lbr_cursor_node(thread, cursor, i);
2444 /* Add LBR ip from entries.from one by one. */
2445 for (i = lbr_nr - 1; i >= 0; i--) {
2446 ip = entries[i].from;
2447 flags = &entries[i].flags;
2448 err = add_callchain_ip(thread, cursor, parent,
2449 root_al, &cpumode, ip,
2454 save_lbr_cursor_node(thread, cursor, i);
2457 /* Add LBR ip from first entries.to */
2459 flags = &entries[0].flags;
2460 *branch_from = entries[0].from;
2461 err = add_callchain_ip(thread, cursor, parent,
2462 root_al, &cpumode, ip,
2471 static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
2472 struct callchain_cursor *cursor)
2474 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2475 struct callchain_cursor_node *cnode;
2476 struct stitch_list *stitch_node;
2479 list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
2480 cnode = &stitch_node->cursor;
2482 err = callchain_cursor_append(cursor, cnode->ip,
2485 &cnode->branch_flags,
2486 cnode->nr_loop_iter,
2496 static struct stitch_list *get_stitch_node(struct thread *thread)
2498 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2499 struct stitch_list *stitch_node;
2501 if (!list_empty(&lbr_stitch->free_lists)) {
2502 stitch_node = list_first_entry(&lbr_stitch->free_lists,
2503 struct stitch_list, node);
2504 list_del(&stitch_node->node);
2509 return malloc(sizeof(struct stitch_list));
2512 static bool has_stitched_lbr(struct thread *thread,
2513 struct perf_sample *cur,
2514 struct perf_sample *prev,
2515 unsigned int max_lbr,
2518 struct branch_stack *cur_stack = cur->branch_stack;
2519 struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
2520 struct branch_stack *prev_stack = prev->branch_stack;
2521 struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
2522 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2523 int i, j, nr_identical_branches = 0;
2524 struct stitch_list *stitch_node;
2525 u64 cur_base, distance;
2527 if (!cur_stack || !prev_stack)
2530 /* Find the physical index of the base-of-stack for current sample. */
2531 cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
2533 distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
2534 (max_lbr + prev_stack->hw_idx - cur_base);
2535 /* Previous sample has shorter stack. Nothing can be stitched. */
2536 if (distance + 1 > prev_stack->nr)
2540 * Check if there are identical LBRs between two samples.
2541 * Identical LBRs must have same from, to and flags values. Also,
2542 * they have to be saved in the same LBR registers (same physical
2545 * Starts from the base-of-stack of current sample.
2547 for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
2548 if ((prev_entries[i].from != cur_entries[j].from) ||
2549 (prev_entries[i].to != cur_entries[j].to) ||
2550 (prev_entries[i].flags.value != cur_entries[j].flags.value))
2552 nr_identical_branches++;
2555 if (!nr_identical_branches)
2559 * Save the LBRs between the base-of-stack of previous sample
2560 * and the base-of-stack of current sample into lbr_stitch->lists.
2561 * These LBRs will be stitched later.
2563 for (i = prev_stack->nr - 1; i > (int)distance; i--) {
2565 if (!lbr_stitch->prev_lbr_cursor[i].valid)
2568 stitch_node = get_stitch_node(thread);
2572 memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
2573 sizeof(struct callchain_cursor_node));
2576 list_add(&stitch_node->node, &lbr_stitch->lists);
2578 list_add_tail(&stitch_node->node, &lbr_stitch->lists);
2584 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
2586 if (thread->lbr_stitch)
2589 thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch));
2590 if (!thread->lbr_stitch)
2593 thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
2594 if (!thread->lbr_stitch->prev_lbr_cursor)
2595 goto free_lbr_stitch;
2597 INIT_LIST_HEAD(&thread->lbr_stitch->lists);
2598 INIT_LIST_HEAD(&thread->lbr_stitch->free_lists);
2603 zfree(&thread->lbr_stitch);
2605 pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
2606 thread->lbr_stitch_enable = false;
2611 * Resolve LBR callstack chain sample
2613 * 1 on success get LBR callchain information
2614 * 0 no available LBR callchain information, should try fp
2615 * negative error code on other errors.
2617 static int resolve_lbr_callchain_sample(struct thread *thread,
2618 struct callchain_cursor *cursor,
2619 struct perf_sample *sample,
2620 struct symbol **parent,
2621 struct addr_location *root_al,
2623 unsigned int max_lbr)
2625 bool callee = (callchain_param.order == ORDER_CALLEE);
2626 struct ip_callchain *chain = sample->callchain;
2627 int chain_nr = min(max_stack, (int)chain->nr), i;
2628 struct lbr_stitch *lbr_stitch;
2629 bool stitched_lbr = false;
2630 u64 branch_from = 0;
2633 for (i = 0; i < chain_nr; i++) {
2634 if (chain->ips[i] == PERF_CONTEXT_USER)
2638 /* LBR only affects the user callchain */
2642 if (thread->lbr_stitch_enable && !sample->no_hw_idx &&
2643 (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
2644 lbr_stitch = thread->lbr_stitch;
2646 stitched_lbr = has_stitched_lbr(thread, sample,
2647 &lbr_stitch->prev_sample,
2650 if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2651 list_replace_init(&lbr_stitch->lists,
2652 &lbr_stitch->free_lists);
2654 memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
2659 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2660 parent, root_al, branch_from,
2665 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2666 root_al, &branch_from, true);
2671 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2678 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2682 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2683 root_al, &branch_from, false);
2688 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2689 parent, root_al, branch_from,
2697 return (err < 0) ? err : 0;
2700 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2701 struct callchain_cursor *cursor,
2702 struct symbol **parent,
2703 struct addr_location *root_al,
2704 u8 *cpumode, int ent)
2708 while (--ent >= 0) {
2709 u64 ip = chain->ips[ent];
2711 if (ip >= PERF_CONTEXT_MAX) {
2712 err = add_callchain_ip(thread, cursor, parent,
2713 root_al, cpumode, ip,
2714 false, NULL, NULL, 0);
2721 static u64 get_leaf_frame_caller(struct perf_sample *sample,
2722 struct thread *thread, int usr_idx)
2724 if (machine__normalized_is(thread->maps->machine, "arm64"))
2725 return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
2730 static int thread__resolve_callchain_sample(struct thread *thread,
2731 struct callchain_cursor *cursor,
2732 struct evsel *evsel,
2733 struct perf_sample *sample,
2734 struct symbol **parent,
2735 struct addr_location *root_al,
2738 struct branch_stack *branch = sample->branch_stack;
2739 struct branch_entry *entries = perf_sample__branch_entries(sample);
2740 struct ip_callchain *chain = sample->callchain;
2742 u8 cpumode = PERF_RECORD_MISC_USER;
2743 int i, j, err, nr_entries, usr_idx;
2746 u64 leaf_frame_caller;
2749 chain_nr = chain->nr;
2751 if (evsel__has_branch_callstack(evsel)) {
2752 struct perf_env *env = evsel__env(evsel);
2754 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2756 !env ? 0 : env->max_branches);
2758 return (err < 0) ? err : 0;
2762 * Based on DWARF debug information, some architectures skip
2763 * a callchain entry saved by the kernel.
2765 skip_idx = arch_skip_callchain_idx(thread, chain);
2768 * Add branches to call stack for easier browsing. This gives
2769 * more context for a sample than just the callers.
2771 * This uses individual histograms of paths compared to the
2772 * aggregated histograms the normal LBR mode uses.
2774 * Limitations for now:
2775 * - No extra filters
2776 * - No annotations (should annotate somehow)
2779 if (branch && callchain_param.branch_callstack) {
2780 int nr = min(max_stack, (int)branch->nr);
2781 struct branch_entry be[nr];
2782 struct iterations iter[nr];
2784 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2785 pr_warning("corrupted branch chain. skipping...\n");
2789 for (i = 0; i < nr; i++) {
2790 if (callchain_param.order == ORDER_CALLEE) {
2797 * Check for overlap into the callchain.
2798 * The return address is one off compared to
2799 * the branch entry. To adjust for this
2800 * assume the calling instruction is not longer
2803 if (i == skip_idx ||
2804 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2806 else if (be[i].from < chain->ips[first_call] &&
2807 be[i].from >= chain->ips[first_call] - 8)
2810 be[i] = entries[branch->nr - i - 1];
2813 memset(iter, 0, sizeof(struct iterations) * nr);
2814 nr = remove_loops(be, nr, iter);
2816 for (i = 0; i < nr; i++) {
2817 err = add_callchain_ip(thread, cursor, parent,
2824 err = add_callchain_ip(thread, cursor, parent, root_al,
2841 if (chain && callchain_param.order != ORDER_CALLEE) {
2842 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2843 &cpumode, chain->nr - first_call);
2845 return (err < 0) ? err : 0;
2847 for (i = first_call, nr_entries = 0;
2848 i < chain_nr && nr_entries < max_stack; i++) {
2851 if (callchain_param.order == ORDER_CALLEE)
2854 j = chain->nr - i - 1;
2856 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2861 if (ip < PERF_CONTEXT_MAX)
2863 else if (callchain_param.order != ORDER_CALLEE) {
2864 err = find_prev_cpumode(chain, thread, cursor, parent,
2865 root_al, &cpumode, j);
2867 return (err < 0) ? err : 0;
2872 * PERF_CONTEXT_USER allows us to locate where the user stack ends.
2873 * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
2874 * the index will be different in order to add the missing frame
2875 * at the right place.
2878 usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
2880 if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
2882 leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
2885 * check if leaf_frame_Caller != ip to not add the same
2889 if (leaf_frame_caller && leaf_frame_caller != ip) {
2891 err = add_callchain_ip(thread, cursor, parent,
2892 root_al, &cpumode, leaf_frame_caller,
2893 false, NULL, NULL, 0);
2895 return (err < 0) ? err : 0;
2899 err = add_callchain_ip(thread, cursor, parent,
2900 root_al, &cpumode, ip,
2901 false, NULL, NULL, 0);
2904 return (err < 0) ? err : 0;
2910 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
2912 struct symbol *sym = ms->sym;
2913 struct map *map = ms->map;
2914 struct inline_node *inline_node;
2915 struct inline_list *ilist;
2919 if (!symbol_conf.inline_name || !map || !sym)
2922 addr = map__map_ip(map, ip);
2923 addr = map__rip_2objdump(map, addr);
2925 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2927 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2930 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2933 list_for_each_entry(ilist, &inline_node->val, list) {
2934 struct map_symbol ilist_ms = {
2937 .sym = ilist->symbol,
2939 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
2940 NULL, 0, 0, 0, ilist->srcline);
2949 static int unwind_entry(struct unwind_entry *entry, void *arg)
2951 struct callchain_cursor *cursor = arg;
2952 const char *srcline = NULL;
2953 u64 addr = entry->ip;
2955 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
2958 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
2962 * Convert entry->ip from a virtual address to an offset in
2963 * its corresponding binary.
2966 addr = map__map_ip(entry->ms.map, entry->ip);
2968 srcline = callchain_srcline(&entry->ms, addr);
2969 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
2970 false, NULL, 0, 0, 0, srcline);
2973 static int thread__resolve_callchain_unwind(struct thread *thread,
2974 struct callchain_cursor *cursor,
2975 struct evsel *evsel,
2976 struct perf_sample *sample,
2979 /* Can we do dwarf post unwind? */
2980 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2981 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
2984 /* Bail out if nothing was captured. */
2985 if ((!sample->user_regs.regs) ||
2986 (!sample->user_stack.size))
2989 return unwind__get_entries(unwind_entry, cursor,
2990 thread, sample, max_stack);
2993 int thread__resolve_callchain(struct thread *thread,
2994 struct callchain_cursor *cursor,
2995 struct evsel *evsel,
2996 struct perf_sample *sample,
2997 struct symbol **parent,
2998 struct addr_location *root_al,
3003 callchain_cursor_reset(cursor);
3005 if (callchain_param.order == ORDER_CALLEE) {
3006 ret = thread__resolve_callchain_sample(thread, cursor,
3012 ret = thread__resolve_callchain_unwind(thread, cursor,
3016 ret = thread__resolve_callchain_unwind(thread, cursor,
3021 ret = thread__resolve_callchain_sample(thread, cursor,
3030 int machine__for_each_thread(struct machine *machine,
3031 int (*fn)(struct thread *thread, void *p),
3034 struct threads *threads;
3036 struct thread *thread;
3040 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3041 threads = &machine->threads[i];
3042 for (nd = rb_first_cached(&threads->entries); nd;
3044 thread = rb_entry(nd, struct thread, rb_node);
3045 rc = fn(thread, priv);
3050 list_for_each_entry(thread, &threads->dead, node) {
3051 rc = fn(thread, priv);
3059 int machines__for_each_thread(struct machines *machines,
3060 int (*fn)(struct thread *thread, void *p),
3066 rc = machine__for_each_thread(&machines->host, fn, priv);
3070 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
3071 struct machine *machine = rb_entry(nd, struct machine, rb_node);
3073 rc = machine__for_each_thread(machine, fn, priv);
3080 pid_t machine__get_current_tid(struct machine *machine, int cpu)
3082 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3084 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
3087 return machine->current_tid[cpu];
3090 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
3093 struct thread *thread;
3094 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3099 if (!machine->current_tid) {
3102 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
3103 if (!machine->current_tid)
3105 for (i = 0; i < nr_cpus; i++)
3106 machine->current_tid[i] = -1;
3109 if (cpu >= nr_cpus) {
3110 pr_err("Requested CPU %d too large. ", cpu);
3111 pr_err("Consider raising MAX_NR_CPUS\n");
3115 machine->current_tid[cpu] = tid;
3117 thread = machine__findnew_thread(machine, pid, tid);
3122 thread__put(thread);
3128 * Compares the raw arch string. N.B. see instead perf_env__arch() or
3129 * machine__normalized_is() if a normalized arch is needed.
3131 bool machine__is(struct machine *machine, const char *arch)
3133 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
3136 bool machine__normalized_is(struct machine *machine, const char *arch)
3138 return machine && !strcmp(perf_env__arch(machine->env), arch);
3141 int machine__nr_cpus_avail(struct machine *machine)
3143 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
3146 int machine__get_kernel_start(struct machine *machine)
3148 struct map *map = machine__kernel_map(machine);
3152 * The only addresses above 2^63 are kernel addresses of a 64-bit
3153 * kernel. Note that addresses are unsigned so that on a 32-bit system
3154 * all addresses including kernel addresses are less than 2^32. In
3155 * that case (32-bit system), if the kernel mapping is unknown, all
3156 * addresses will be assumed to be in user space - see
3157 * machine__kernel_ip().
3159 machine->kernel_start = 1ULL << 63;
3161 err = map__load(map);
3163 * On x86_64, PTI entry trampolines are less than the
3164 * start of kernel text, but still above 2^63. So leave
3165 * kernel_start = 1ULL << 63 for x86_64.
3167 if (!err && !machine__is(machine, "x86_64"))
3168 machine->kernel_start = map->start;
3173 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
3175 u8 addr_cpumode = cpumode;
3178 if (!machine->single_address_space)
3181 kernel_ip = machine__kernel_ip(machine, addr);
3183 case PERF_RECORD_MISC_KERNEL:
3184 case PERF_RECORD_MISC_USER:
3185 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
3186 PERF_RECORD_MISC_USER;
3188 case PERF_RECORD_MISC_GUEST_KERNEL:
3189 case PERF_RECORD_MISC_GUEST_USER:
3190 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
3191 PERF_RECORD_MISC_GUEST_USER;
3197 return addr_cpumode;
3200 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
3202 return dsos__findnew_id(&machine->dsos, filename, id);
3205 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
3207 return machine__findnew_dso_id(machine, filename, NULL);
3210 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
3212 struct machine *machine = vmachine;
3214 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
3219 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
3220 *addrp = map->unmap_ip(map, sym->start);
3224 int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
3229 list_for_each_entry(pos, &machine->dsos.head, node) {
3230 if (fn(pos, machine, priv))