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 maps__init(&machine->kmaps, machine);
93 RB_CLEAR_NODE(&machine->rb_node);
94 dsos__init(&machine->dsos);
96 machine__threads_init(machine);
98 machine->vdso_info = NULL;
103 machine->id_hdr_size = 0;
104 machine->kptr_restrict_warned = false;
105 machine->comm_exec = false;
106 machine->kernel_start = 0;
107 machine->vmlinux_map = NULL;
109 machine->root_dir = strdup(root_dir);
110 if (machine->root_dir == NULL)
113 if (machine__set_mmap_name(machine))
116 if (pid != HOST_KERNEL_ID) {
117 struct thread *thread = machine__findnew_thread(machine, -1,
124 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
125 thread__set_comm(thread, comm, 0);
129 machine->current_tid = NULL;
134 zfree(&machine->root_dir);
135 zfree(&machine->mmap_name);
140 struct machine *machine__new_host(void)
142 struct machine *machine = malloc(sizeof(*machine));
144 if (machine != NULL) {
145 machine__init(machine, "", HOST_KERNEL_ID);
147 if (machine__create_kernel_maps(machine) < 0)
157 struct machine *machine__new_kallsyms(void)
159 struct machine *machine = machine__new_host();
162 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
163 * ask for not using the kcore parsing code, once this one is fixed
164 * to create a map per module.
166 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
167 machine__delete(machine);
174 static void dsos__purge(struct dsos *dsos)
178 down_write(&dsos->lock);
180 list_for_each_entry_safe(pos, n, &dsos->head, node) {
181 RB_CLEAR_NODE(&pos->rb_node);
183 list_del_init(&pos->node);
187 up_write(&dsos->lock);
190 static void dsos__exit(struct dsos *dsos)
193 exit_rwsem(&dsos->lock);
196 void machine__delete_threads(struct machine *machine)
201 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
202 struct threads *threads = &machine->threads[i];
203 down_write(&threads->lock);
204 nd = rb_first_cached(&threads->entries);
206 struct thread *t = rb_entry(nd, struct thread, rb_node);
209 __machine__remove_thread(machine, t, false);
211 up_write(&threads->lock);
215 void machine__exit(struct machine *machine)
222 machine__destroy_kernel_maps(machine);
223 maps__exit(&machine->kmaps);
224 dsos__exit(&machine->dsos);
225 machine__exit_vdso(machine);
226 zfree(&machine->root_dir);
227 zfree(&machine->mmap_name);
228 zfree(&machine->current_tid);
230 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
231 struct threads *threads = &machine->threads[i];
232 struct thread *thread, *n;
234 * Forget about the dead, at this point whatever threads were
235 * left in the dead lists better have a reference count taken
236 * by who is using them, and then, when they drop those references
237 * and it finally hits zero, thread__put() will check and see that
238 * its not in the dead threads list and will not try to remove it
239 * from there, just calling thread__delete() straight away.
241 list_for_each_entry_safe(thread, n, &threads->dead, node)
242 list_del_init(&thread->node);
244 exit_rwsem(&threads->lock);
248 void machine__delete(struct machine *machine)
251 machine__exit(machine);
256 void machines__init(struct machines *machines)
258 machine__init(&machines->host, "", HOST_KERNEL_ID);
259 machines->guests = RB_ROOT_CACHED;
262 void machines__exit(struct machines *machines)
264 machine__exit(&machines->host);
268 struct machine *machines__add(struct machines *machines, pid_t pid,
269 const char *root_dir)
271 struct rb_node **p = &machines->guests.rb_root.rb_node;
272 struct rb_node *parent = NULL;
273 struct machine *pos, *machine = malloc(sizeof(*machine));
274 bool leftmost = true;
279 if (machine__init(machine, root_dir, pid) != 0) {
286 pos = rb_entry(parent, struct machine, rb_node);
295 rb_link_node(&machine->rb_node, parent, p);
296 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
301 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
305 machines->host.comm_exec = comm_exec;
307 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
308 struct machine *machine = rb_entry(nd, struct machine, rb_node);
310 machine->comm_exec = comm_exec;
314 struct machine *machines__find(struct machines *machines, pid_t pid)
316 struct rb_node **p = &machines->guests.rb_root.rb_node;
317 struct rb_node *parent = NULL;
318 struct machine *machine;
319 struct machine *default_machine = NULL;
321 if (pid == HOST_KERNEL_ID)
322 return &machines->host;
326 machine = rb_entry(parent, struct machine, rb_node);
327 if (pid < machine->pid)
329 else if (pid > machine->pid)
334 default_machine = machine;
337 return default_machine;
340 struct machine *machines__findnew(struct machines *machines, pid_t pid)
343 const char *root_dir = "";
344 struct machine *machine = machines__find(machines, pid);
346 if (machine && (machine->pid == pid))
349 if ((pid != HOST_KERNEL_ID) &&
350 (pid != DEFAULT_GUEST_KERNEL_ID) &&
351 (symbol_conf.guestmount)) {
352 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
353 if (access(path, R_OK)) {
354 static struct strlist *seen;
357 seen = strlist__new(NULL, NULL);
359 if (!strlist__has_entry(seen, path)) {
360 pr_err("Can't access file %s\n", path);
361 strlist__add(seen, path);
369 machine = machines__add(machines, pid, root_dir);
374 struct machine *machines__find_guest(struct machines *machines, pid_t pid)
376 struct machine *machine = machines__find(machines, pid);
379 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
383 void machines__process_guests(struct machines *machines,
384 machine__process_t process, void *data)
388 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
389 struct machine *pos = rb_entry(nd, struct machine, rb_node);
394 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
396 struct rb_node *node;
397 struct machine *machine;
399 machines->host.id_hdr_size = id_hdr_size;
401 for (node = rb_first_cached(&machines->guests); node;
402 node = rb_next(node)) {
403 machine = rb_entry(node, struct machine, rb_node);
404 machine->id_hdr_size = id_hdr_size;
410 static void machine__update_thread_pid(struct machine *machine,
411 struct thread *th, pid_t pid)
413 struct thread *leader;
415 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
420 if (th->pid_ == th->tid)
423 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
428 leader->maps = maps__new(machine);
433 if (th->maps == leader->maps)
438 * Maps are created from MMAP events which provide the pid and
439 * tid. Consequently there never should be any maps on a thread
440 * with an unknown pid. Just print an error if there are.
442 if (!maps__empty(th->maps))
443 pr_err("Discarding thread maps for %d:%d\n",
448 th->maps = maps__get(leader->maps);
453 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
458 * Front-end cache - TID lookups come in blocks,
459 * so most of the time we dont have to look up
462 static struct thread*
463 __threads__get_last_match(struct threads *threads, struct machine *machine,
468 th = threads->last_match;
470 if (th->tid == tid) {
471 machine__update_thread_pid(machine, th, pid);
472 return thread__get(th);
475 threads->last_match = NULL;
481 static struct thread*
482 threads__get_last_match(struct threads *threads, struct machine *machine,
485 struct thread *th = NULL;
487 if (perf_singlethreaded)
488 th = __threads__get_last_match(threads, machine, pid, tid);
494 __threads__set_last_match(struct threads *threads, struct thread *th)
496 threads->last_match = th;
500 threads__set_last_match(struct threads *threads, struct thread *th)
502 if (perf_singlethreaded)
503 __threads__set_last_match(threads, th);
507 * Caller must eventually drop thread->refcnt returned with a successful
508 * lookup/new thread inserted.
510 static struct thread *____machine__findnew_thread(struct machine *machine,
511 struct threads *threads,
512 pid_t pid, pid_t tid,
515 struct rb_node **p = &threads->entries.rb_root.rb_node;
516 struct rb_node *parent = NULL;
518 bool leftmost = true;
520 th = threads__get_last_match(threads, machine, pid, tid);
526 th = rb_entry(parent, struct thread, rb_node);
528 if (th->tid == tid) {
529 threads__set_last_match(threads, th);
530 machine__update_thread_pid(machine, th, pid);
531 return thread__get(th);
545 th = thread__new(pid, tid);
547 rb_link_node(&th->rb_node, parent, p);
548 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
551 * We have to initialize maps separately after rb tree is updated.
553 * The reason is that we call machine__findnew_thread
554 * within thread__init_maps to find the thread
555 * leader and that would screwed the rb tree.
557 if (thread__init_maps(th, machine)) {
558 rb_erase_cached(&th->rb_node, &threads->entries);
559 RB_CLEAR_NODE(&th->rb_node);
564 * It is now in the rbtree, get a ref
567 threads__set_last_match(threads, th);
574 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
576 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
579 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
582 struct threads *threads = machine__threads(machine, tid);
585 down_write(&threads->lock);
586 th = __machine__findnew_thread(machine, pid, tid);
587 up_write(&threads->lock);
591 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
594 struct threads *threads = machine__threads(machine, tid);
597 down_read(&threads->lock);
598 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
599 up_read(&threads->lock);
604 * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
605 * So here a single thread is created for that, but actually there is a separate
606 * idle task per cpu, so there should be one 'struct thread' per cpu, but there
607 * is only 1. That causes problems for some tools, requiring workarounds. For
608 * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
610 struct thread *machine__idle_thread(struct machine *machine)
612 struct thread *thread = machine__findnew_thread(machine, 0, 0);
614 if (!thread || thread__set_comm(thread, "swapper", 0) ||
615 thread__set_namespaces(thread, 0, NULL))
616 pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
621 struct comm *machine__thread_exec_comm(struct machine *machine,
622 struct thread *thread)
624 if (machine->comm_exec)
625 return thread__exec_comm(thread);
627 return thread__comm(thread);
630 int machine__process_comm_event(struct machine *machine, union perf_event *event,
631 struct perf_sample *sample)
633 struct thread *thread = machine__findnew_thread(machine,
636 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
640 machine->comm_exec = true;
643 perf_event__fprintf_comm(event, stdout);
645 if (thread == NULL ||
646 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
647 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
656 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
657 union perf_event *event,
658 struct perf_sample *sample __maybe_unused)
660 struct thread *thread = machine__findnew_thread(machine,
661 event->namespaces.pid,
662 event->namespaces.tid);
665 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
666 "\nWARNING: kernel seems to support more namespaces than perf"
667 " tool.\nTry updating the perf tool..\n\n");
669 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
670 "\nWARNING: perf tool seems to support more namespaces than"
671 " the kernel.\nTry updating the kernel..\n\n");
674 perf_event__fprintf_namespaces(event, stdout);
676 if (thread == NULL ||
677 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
678 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
687 int machine__process_cgroup_event(struct machine *machine,
688 union perf_event *event,
689 struct perf_sample *sample __maybe_unused)
694 perf_event__fprintf_cgroup(event, stdout);
696 cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
703 int machine__process_lost_event(struct machine *machine __maybe_unused,
704 union perf_event *event, struct perf_sample *sample __maybe_unused)
706 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
707 event->lost.id, event->lost.lost);
711 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
712 union perf_event *event, struct perf_sample *sample)
714 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
715 sample->id, event->lost_samples.lost);
719 static struct dso *machine__findnew_module_dso(struct machine *machine,
721 const char *filename)
725 down_write(&machine->dsos.lock);
727 dso = __dsos__find(&machine->dsos, m->name, true);
729 dso = __dsos__addnew(&machine->dsos, m->name);
733 dso__set_module_info(dso, m, machine);
734 dso__set_long_name(dso, strdup(filename), true);
735 dso->kernel = DSO_SPACE__KERNEL;
740 up_write(&machine->dsos.lock);
744 int machine__process_aux_event(struct machine *machine __maybe_unused,
745 union perf_event *event)
748 perf_event__fprintf_aux(event, stdout);
752 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
753 union perf_event *event)
756 perf_event__fprintf_itrace_start(event, stdout);
760 int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
761 union perf_event *event)
764 perf_event__fprintf_aux_output_hw_id(event, stdout);
768 int machine__process_switch_event(struct machine *machine __maybe_unused,
769 union perf_event *event)
772 perf_event__fprintf_switch(event, stdout);
776 static int machine__process_ksymbol_register(struct machine *machine,
777 union perf_event *event,
778 struct perf_sample *sample __maybe_unused)
781 struct map *map = maps__find(&machine->kmaps, event->ksymbol.addr);
784 struct dso *dso = dso__new(event->ksymbol.name);
787 dso->kernel = DSO_SPACE__KERNEL;
788 map = map__new2(0, dso);
796 if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
797 map->dso->binary_type = DSO_BINARY_TYPE__OOL;
798 map->dso->data.file_size = event->ksymbol.len;
799 dso__set_loaded(map->dso);
802 map->start = event->ksymbol.addr;
803 map->end = map->start + event->ksymbol.len;
804 maps__insert(&machine->kmaps, map);
806 dso__set_loaded(dso);
808 if (is_bpf_image(event->ksymbol.name)) {
809 dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE;
810 dso__set_long_name(dso, "", false);
814 sym = symbol__new(map->map_ip(map, map->start),
816 0, 0, event->ksymbol.name);
819 dso__insert_symbol(map->dso, sym);
823 static int machine__process_ksymbol_unregister(struct machine *machine,
824 union perf_event *event,
825 struct perf_sample *sample __maybe_unused)
830 map = maps__find(&machine->kmaps, event->ksymbol.addr);
834 if (map != machine->vmlinux_map)
835 maps__remove(&machine->kmaps, map);
837 sym = dso__find_symbol(map->dso, map->map_ip(map, map->start));
839 dso__delete_symbol(map->dso, sym);
845 int machine__process_ksymbol(struct machine *machine __maybe_unused,
846 union perf_event *event,
847 struct perf_sample *sample)
850 perf_event__fprintf_ksymbol(event, stdout);
852 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
853 return machine__process_ksymbol_unregister(machine, event,
855 return machine__process_ksymbol_register(machine, event, sample);
858 int machine__process_text_poke(struct machine *machine, union perf_event *event,
859 struct perf_sample *sample __maybe_unused)
861 struct map *map = maps__find(&machine->kmaps, event->text_poke.addr);
862 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
865 perf_event__fprintf_text_poke(event, machine, stdout);
867 if (!event->text_poke.new_len)
870 if (cpumode != PERF_RECORD_MISC_KERNEL) {
871 pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
875 if (map && map->dso) {
876 u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
880 * Kernel maps might be changed when loading symbols so loading
881 * must be done prior to using kernel maps.
884 ret = dso__data_write_cache_addr(map->dso, map, machine,
885 event->text_poke.addr,
887 event->text_poke.new_len);
888 if (ret != event->text_poke.new_len)
889 pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
890 event->text_poke.addr);
892 pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
893 event->text_poke.addr);
899 static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
900 const char *filename)
902 struct map *map = NULL;
906 if (kmod_path__parse_name(&m, filename))
909 dso = machine__findnew_module_dso(machine, &m, filename);
913 map = map__new2(start, dso);
917 maps__insert(&machine->kmaps, map);
919 /* Put the map here because maps__insert already got it */
922 /* put the dso here, corresponding to machine__findnew_module_dso */
928 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
931 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
933 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
934 struct machine *pos = rb_entry(nd, struct machine, rb_node);
935 ret += __dsos__fprintf(&pos->dsos.head, fp);
941 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
942 bool (skip)(struct dso *dso, int parm), int parm)
944 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
947 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
948 bool (skip)(struct dso *dso, int parm), int parm)
951 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
953 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
954 struct machine *pos = rb_entry(nd, struct machine, rb_node);
955 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
960 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
964 struct dso *kdso = machine__kernel_dso(machine);
966 if (kdso->has_build_id) {
967 char filename[PATH_MAX];
968 if (dso__build_id_filename(kdso, filename, sizeof(filename),
970 printed += fprintf(fp, "[0] %s\n", filename);
973 for (i = 0; i < vmlinux_path__nr_entries; ++i)
974 printed += fprintf(fp, "[%d] %s\n",
975 i + kdso->has_build_id, vmlinux_path[i]);
980 size_t machine__fprintf(struct machine *machine, FILE *fp)
986 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
987 struct threads *threads = &machine->threads[i];
989 down_read(&threads->lock);
991 ret = fprintf(fp, "Threads: %u\n", threads->nr);
993 for (nd = rb_first_cached(&threads->entries); nd;
995 struct thread *pos = rb_entry(nd, struct thread, rb_node);
997 ret += thread__fprintf(pos, fp);
1000 up_read(&threads->lock);
1005 static struct dso *machine__get_kernel(struct machine *machine)
1007 const char *vmlinux_name = machine->mmap_name;
1010 if (machine__is_host(machine)) {
1011 if (symbol_conf.vmlinux_name)
1012 vmlinux_name = symbol_conf.vmlinux_name;
1014 kernel = machine__findnew_kernel(machine, vmlinux_name,
1015 "[kernel]", DSO_SPACE__KERNEL);
1017 if (symbol_conf.default_guest_vmlinux_name)
1018 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1020 kernel = machine__findnew_kernel(machine, vmlinux_name,
1022 DSO_SPACE__KERNEL_GUEST);
1025 if (kernel != NULL && (!kernel->has_build_id))
1026 dso__read_running_kernel_build_id(kernel, machine);
1031 struct process_args {
1035 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
1038 if (machine__is_default_guest(machine))
1039 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
1041 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
1044 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
1046 /* Figure out the start address of kernel map from /proc/kallsyms.
1047 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
1048 * symbol_name if it's not that important.
1050 static int machine__get_running_kernel_start(struct machine *machine,
1051 const char **symbol_name,
1052 u64 *start, u64 *end)
1054 char filename[PATH_MAX];
1059 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
1061 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1064 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
1065 err = kallsyms__get_function_start(filename, name, &addr);
1074 *symbol_name = name;
1078 err = kallsyms__get_function_start(filename, "_etext", &addr);
1085 int machine__create_extra_kernel_map(struct machine *machine,
1087 struct extra_kernel_map *xm)
1092 map = map__new2(xm->start, kernel);
1097 map->pgoff = xm->pgoff;
1099 kmap = map__kmap(map);
1101 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
1103 maps__insert(&machine->kmaps, map);
1105 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1106 kmap->name, map->start, map->end);
1113 static u64 find_entry_trampoline(struct dso *dso)
1115 /* Duplicates are removed so lookup all aliases */
1116 const char *syms[] = {
1117 "_entry_trampoline",
1118 "__entry_trampoline_start",
1119 "entry_SYSCALL_64_trampoline",
1121 struct symbol *sym = dso__first_symbol(dso);
1124 for (; sym; sym = dso__next_symbol(sym)) {
1125 if (sym->binding != STB_GLOBAL)
1127 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1128 if (!strcmp(sym->name, syms[i]))
1137 * These values can be used for kernels that do not have symbols for the entry
1138 * trampolines in kallsyms.
1140 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1141 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1142 #define X86_64_ENTRY_TRAMPOLINE 0x6000
1144 /* Map x86_64 PTI entry trampolines */
1145 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1148 struct maps *kmaps = &machine->kmaps;
1149 int nr_cpus_avail, cpu;
1155 * In the vmlinux case, pgoff is a virtual address which must now be
1156 * mapped to a vmlinux offset.
1158 maps__for_each_entry(kmaps, map) {
1159 struct kmap *kmap = __map__kmap(map);
1160 struct map *dest_map;
1162 if (!kmap || !is_entry_trampoline(kmap->name))
1165 dest_map = maps__find(kmaps, map->pgoff);
1166 if (dest_map != map)
1167 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1170 if (found || machine->trampolines_mapped)
1173 pgoff = find_entry_trampoline(kernel);
1177 nr_cpus_avail = machine__nr_cpus_avail(machine);
1179 /* Add a 1 page map for each CPU's entry trampoline */
1180 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1181 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1182 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1183 X86_64_ENTRY_TRAMPOLINE;
1184 struct extra_kernel_map xm = {
1186 .end = va + page_size,
1190 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1192 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1196 machine->trampolines_mapped = nr_cpus_avail;
1201 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1202 struct dso *kernel __maybe_unused)
1208 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1210 /* In case of renewal the kernel map, destroy previous one */
1211 machine__destroy_kernel_maps(machine);
1213 machine->vmlinux_map = map__new2(0, kernel);
1214 if (machine->vmlinux_map == NULL)
1217 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1218 maps__insert(&machine->kmaps, machine->vmlinux_map);
1222 void machine__destroy_kernel_maps(struct machine *machine)
1225 struct map *map = machine__kernel_map(machine);
1230 kmap = map__kmap(map);
1231 maps__remove(&machine->kmaps, map);
1232 if (kmap && kmap->ref_reloc_sym) {
1233 zfree((char **)&kmap->ref_reloc_sym->name);
1234 zfree(&kmap->ref_reloc_sym);
1237 map__zput(machine->vmlinux_map);
1240 int machines__create_guest_kernel_maps(struct machines *machines)
1243 struct dirent **namelist = NULL;
1245 char path[PATH_MAX];
1249 if (symbol_conf.default_guest_vmlinux_name ||
1250 symbol_conf.default_guest_modules ||
1251 symbol_conf.default_guest_kallsyms) {
1252 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1255 if (symbol_conf.guestmount) {
1256 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1259 for (i = 0; i < items; i++) {
1260 if (!isdigit(namelist[i]->d_name[0])) {
1261 /* Filter out . and .. */
1264 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1265 if ((*endp != '\0') ||
1266 (endp == namelist[i]->d_name) ||
1267 (errno == ERANGE)) {
1268 pr_debug("invalid directory (%s). Skipping.\n",
1269 namelist[i]->d_name);
1272 sprintf(path, "%s/%s/proc/kallsyms",
1273 symbol_conf.guestmount,
1274 namelist[i]->d_name);
1275 ret = access(path, R_OK);
1277 pr_debug("Can't access file %s\n", path);
1280 machines__create_kernel_maps(machines, pid);
1289 void machines__destroy_kernel_maps(struct machines *machines)
1291 struct rb_node *next = rb_first_cached(&machines->guests);
1293 machine__destroy_kernel_maps(&machines->host);
1296 struct machine *pos = rb_entry(next, struct machine, rb_node);
1298 next = rb_next(&pos->rb_node);
1299 rb_erase_cached(&pos->rb_node, &machines->guests);
1300 machine__delete(pos);
1304 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1306 struct machine *machine = machines__findnew(machines, pid);
1308 if (machine == NULL)
1311 return machine__create_kernel_maps(machine);
1314 int machine__load_kallsyms(struct machine *machine, const char *filename)
1316 struct map *map = machine__kernel_map(machine);
1317 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
1320 dso__set_loaded(map->dso);
1322 * Since /proc/kallsyms will have multiple sessions for the
1323 * kernel, with modules between them, fixup the end of all
1326 maps__fixup_end(&machine->kmaps);
1332 int machine__load_vmlinux_path(struct machine *machine)
1334 struct map *map = machine__kernel_map(machine);
1335 int ret = dso__load_vmlinux_path(map->dso, map);
1338 dso__set_loaded(map->dso);
1343 static char *get_kernel_version(const char *root_dir)
1345 char version[PATH_MAX];
1348 const char *prefix = "Linux version ";
1350 sprintf(version, "%s/proc/version", root_dir);
1351 file = fopen(version, "r");
1355 tmp = fgets(version, sizeof(version), file);
1360 name = strstr(version, prefix);
1363 name += strlen(prefix);
1364 tmp = strchr(name, ' ');
1368 return strdup(name);
1371 static bool is_kmod_dso(struct dso *dso)
1373 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1374 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1377 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1380 struct map *map = maps__find_by_name(maps, m->name);
1385 long_name = strdup(path);
1386 if (long_name == NULL)
1389 dso__set_long_name(map->dso, long_name, true);
1390 dso__kernel_module_get_build_id(map->dso, "");
1393 * Full name could reveal us kmod compression, so
1394 * we need to update the symtab_type if needed.
1396 if (m->comp && is_kmod_dso(map->dso)) {
1397 map->dso->symtab_type++;
1398 map->dso->comp = m->comp;
1404 static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
1406 struct dirent *dent;
1407 DIR *dir = opendir(dir_name);
1411 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1415 while ((dent = readdir(dir)) != NULL) {
1416 char path[PATH_MAX];
1419 /*sshfs might return bad dent->d_type, so we have to stat*/
1420 path__join(path, sizeof(path), dir_name, dent->d_name);
1421 if (stat(path, &st))
1424 if (S_ISDIR(st.st_mode)) {
1425 if (!strcmp(dent->d_name, ".") ||
1426 !strcmp(dent->d_name, ".."))
1429 /* Do not follow top-level source and build symlinks */
1431 if (!strcmp(dent->d_name, "source") ||
1432 !strcmp(dent->d_name, "build"))
1436 ret = maps__set_modules_path_dir(maps, path, depth + 1);
1442 ret = kmod_path__parse_name(&m, dent->d_name);
1447 ret = maps__set_module_path(maps, path, &m);
1461 static int machine__set_modules_path(struct machine *machine)
1464 char modules_path[PATH_MAX];
1466 version = get_kernel_version(machine->root_dir);
1470 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1471 machine->root_dir, version);
1474 return maps__set_modules_path_dir(&machine->kmaps, modules_path, 0);
1476 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1477 u64 *size __maybe_unused,
1478 const char *name __maybe_unused)
1483 static int machine__create_module(void *arg, const char *name, u64 start,
1486 struct machine *machine = arg;
1489 if (arch__fix_module_text_start(&start, &size, name) < 0)
1492 map = machine__addnew_module_map(machine, start, name);
1495 map->end = start + size;
1497 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1502 static int machine__create_modules(struct machine *machine)
1504 const char *modules;
1505 char path[PATH_MAX];
1507 if (machine__is_default_guest(machine)) {
1508 modules = symbol_conf.default_guest_modules;
1510 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1514 if (symbol__restricted_filename(modules, "/proc/modules"))
1517 if (modules__parse(modules, machine, machine__create_module))
1520 if (!machine__set_modules_path(machine))
1523 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1528 static void machine__set_kernel_mmap(struct machine *machine,
1531 machine->vmlinux_map->start = start;
1532 machine->vmlinux_map->end = end;
1534 * Be a bit paranoid here, some perf.data file came with
1535 * a zero sized synthesized MMAP event for the kernel.
1537 if (start == 0 && end == 0)
1538 machine->vmlinux_map->end = ~0ULL;
1541 static void machine__update_kernel_mmap(struct machine *machine,
1544 struct map *map = machine__kernel_map(machine);
1547 maps__remove(&machine->kmaps, map);
1549 machine__set_kernel_mmap(machine, start, end);
1551 maps__insert(&machine->kmaps, map);
1555 int machine__create_kernel_maps(struct machine *machine)
1557 struct dso *kernel = machine__get_kernel(machine);
1558 const char *name = NULL;
1560 u64 start = 0, end = ~0ULL;
1566 ret = __machine__create_kernel_maps(machine, kernel);
1570 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1571 if (machine__is_host(machine))
1572 pr_debug("Problems creating module maps, "
1573 "continuing anyway...\n");
1575 pr_debug("Problems creating module maps for guest %d, "
1576 "continuing anyway...\n", machine->pid);
1579 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1581 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1582 machine__destroy_kernel_maps(machine);
1588 * we have a real start address now, so re-order the kmaps
1589 * assume it's the last in the kmaps
1591 machine__update_kernel_mmap(machine, start, end);
1594 if (machine__create_extra_kernel_maps(machine, kernel))
1595 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1598 /* update end address of the kernel map using adjacent module address */
1599 map = map__next(machine__kernel_map(machine));
1601 machine__set_kernel_mmap(machine, start, map->start);
1609 static bool machine__uses_kcore(struct machine *machine)
1613 list_for_each_entry(dso, &machine->dsos.head, node) {
1614 if (dso__is_kcore(dso))
1621 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1622 struct extra_kernel_map *xm)
1624 return machine__is(machine, "x86_64") &&
1625 is_entry_trampoline(xm->name);
1628 static int machine__process_extra_kernel_map(struct machine *machine,
1629 struct extra_kernel_map *xm)
1631 struct dso *kernel = machine__kernel_dso(machine);
1636 return machine__create_extra_kernel_map(machine, kernel, xm);
1639 static int machine__process_kernel_mmap_event(struct machine *machine,
1640 struct extra_kernel_map *xm,
1641 struct build_id *bid)
1644 enum dso_space_type dso_space;
1645 bool is_kernel_mmap;
1647 /* If we have maps from kcore then we do not need or want any others */
1648 if (machine__uses_kcore(machine))
1651 if (machine__is_host(machine))
1652 dso_space = DSO_SPACE__KERNEL;
1654 dso_space = DSO_SPACE__KERNEL_GUEST;
1656 is_kernel_mmap = memcmp(xm->name, machine->mmap_name,
1657 strlen(machine->mmap_name) - 1) == 0;
1658 if (xm->name[0] == '/' ||
1659 (!is_kernel_mmap && xm->name[0] == '[')) {
1660 map = machine__addnew_module_map(machine, xm->start,
1665 map->end = map->start + xm->end - xm->start;
1667 if (build_id__is_defined(bid))
1668 dso__set_build_id(map->dso, bid);
1670 } else if (is_kernel_mmap) {
1671 const char *symbol_name = (xm->name + strlen(machine->mmap_name));
1673 * Should be there already, from the build-id table in
1676 struct dso *kernel = NULL;
1679 down_read(&machine->dsos.lock);
1681 list_for_each_entry(dso, &machine->dsos.head, node) {
1684 * The cpumode passed to is_kernel_module is not the
1685 * cpumode of *this* event. If we insist on passing
1686 * correct cpumode to is_kernel_module, we should
1687 * record the cpumode when we adding this dso to the
1690 * However we don't really need passing correct
1691 * cpumode. We know the correct cpumode must be kernel
1692 * mode (if not, we should not link it onto kernel_dsos
1695 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1696 * is_kernel_module() treats it as a kernel cpumode.
1700 is_kernel_module(dso->long_name,
1701 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1709 up_read(&machine->dsos.lock);
1712 kernel = machine__findnew_dso(machine, machine->mmap_name);
1716 kernel->kernel = dso_space;
1717 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1722 if (strstr(kernel->long_name, "vmlinux"))
1723 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1725 machine__update_kernel_mmap(machine, xm->start, xm->end);
1727 if (build_id__is_defined(bid))
1728 dso__set_build_id(kernel, bid);
1731 * Avoid using a zero address (kptr_restrict) for the ref reloc
1732 * symbol. Effectively having zero here means that at record
1733 * time /proc/sys/kernel/kptr_restrict was non zero.
1735 if (xm->pgoff != 0) {
1736 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1741 if (machine__is_default_guest(machine)) {
1743 * preload dso of guest kernel and modules
1745 dso__load(kernel, machine__kernel_map(machine));
1747 } else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
1748 return machine__process_extra_kernel_map(machine, xm);
1755 int machine__process_mmap2_event(struct machine *machine,
1756 union perf_event *event,
1757 struct perf_sample *sample)
1759 struct thread *thread;
1761 struct dso_id dso_id = {
1762 .maj = event->mmap2.maj,
1763 .min = event->mmap2.min,
1764 .ino = event->mmap2.ino,
1765 .ino_generation = event->mmap2.ino_generation,
1767 struct build_id __bid, *bid = NULL;
1771 perf_event__fprintf_mmap2(event, stdout);
1773 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
1775 build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size);
1778 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1779 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1780 struct extra_kernel_map xm = {
1781 .start = event->mmap2.start,
1782 .end = event->mmap2.start + event->mmap2.len,
1783 .pgoff = event->mmap2.pgoff,
1786 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
1787 ret = machine__process_kernel_mmap_event(machine, &xm, bid);
1793 thread = machine__findnew_thread(machine, event->mmap2.pid,
1798 map = map__new(machine, event->mmap2.start,
1799 event->mmap2.len, event->mmap2.pgoff,
1800 &dso_id, event->mmap2.prot,
1801 event->mmap2.flags, bid,
1802 event->mmap2.filename, thread);
1805 goto out_problem_map;
1807 ret = thread__insert_map(thread, map);
1809 goto out_problem_insert;
1811 thread__put(thread);
1818 thread__put(thread);
1820 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1824 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1825 struct perf_sample *sample)
1827 struct thread *thread;
1833 perf_event__fprintf_mmap(event, stdout);
1835 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1836 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1837 struct extra_kernel_map xm = {
1838 .start = event->mmap.start,
1839 .end = event->mmap.start + event->mmap.len,
1840 .pgoff = event->mmap.pgoff,
1843 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1844 ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
1850 thread = machine__findnew_thread(machine, event->mmap.pid,
1855 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1858 map = map__new(machine, event->mmap.start,
1859 event->mmap.len, event->mmap.pgoff,
1860 NULL, prot, 0, NULL, event->mmap.filename, thread);
1863 goto out_problem_map;
1865 ret = thread__insert_map(thread, map);
1867 goto out_problem_insert;
1869 thread__put(thread);
1876 thread__put(thread);
1878 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1882 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1884 struct threads *threads = machine__threads(machine, th->tid);
1886 if (threads->last_match == th)
1887 threads__set_last_match(threads, NULL);
1890 down_write(&threads->lock);
1892 BUG_ON(refcount_read(&th->refcnt) == 0);
1894 rb_erase_cached(&th->rb_node, &threads->entries);
1895 RB_CLEAR_NODE(&th->rb_node);
1898 * Move it first to the dead_threads list, then drop the reference,
1899 * if this is the last reference, then the thread__delete destructor
1900 * will be called and we will remove it from the dead_threads list.
1902 list_add_tail(&th->node, &threads->dead);
1905 * We need to do the put here because if this is the last refcount,
1906 * then we will be touching the threads->dead head when removing the
1912 up_write(&threads->lock);
1915 void machine__remove_thread(struct machine *machine, struct thread *th)
1917 return __machine__remove_thread(machine, th, true);
1920 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1921 struct perf_sample *sample)
1923 struct thread *thread = machine__find_thread(machine,
1926 struct thread *parent = machine__findnew_thread(machine,
1929 bool do_maps_clone = true;
1933 perf_event__fprintf_task(event, stdout);
1936 * There may be an existing thread that is not actually the parent,
1937 * either because we are processing events out of order, or because the
1938 * (fork) event that would have removed the thread was lost. Assume the
1939 * latter case and continue on as best we can.
1941 if (parent->pid_ != (pid_t)event->fork.ppid) {
1942 dump_printf("removing erroneous parent thread %d/%d\n",
1943 parent->pid_, parent->tid);
1944 machine__remove_thread(machine, parent);
1945 thread__put(parent);
1946 parent = machine__findnew_thread(machine, event->fork.ppid,
1950 /* if a thread currently exists for the thread id remove it */
1951 if (thread != NULL) {
1952 machine__remove_thread(machine, thread);
1953 thread__put(thread);
1956 thread = machine__findnew_thread(machine, event->fork.pid,
1959 * When synthesizing FORK events, we are trying to create thread
1960 * objects for the already running tasks on the machine.
1962 * Normally, for a kernel FORK event, we want to clone the parent's
1963 * maps because that is what the kernel just did.
1965 * But when synthesizing, this should not be done. If we do, we end up
1966 * with overlapping maps as we process the synthesized MMAP2 events that
1967 * get delivered shortly thereafter.
1969 * Use the FORK event misc flags in an internal way to signal this
1970 * situation, so we can elide the map clone when appropriate.
1972 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1973 do_maps_clone = false;
1975 if (thread == NULL || parent == NULL ||
1976 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
1977 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1980 thread__put(thread);
1981 thread__put(parent);
1986 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1987 struct perf_sample *sample __maybe_unused)
1989 struct thread *thread = machine__find_thread(machine,
1994 perf_event__fprintf_task(event, stdout);
1996 if (thread != NULL) {
1997 thread__exited(thread);
1998 thread__put(thread);
2004 int machine__process_event(struct machine *machine, union perf_event *event,
2005 struct perf_sample *sample)
2009 switch (event->header.type) {
2010 case PERF_RECORD_COMM:
2011 ret = machine__process_comm_event(machine, event, sample); break;
2012 case PERF_RECORD_MMAP:
2013 ret = machine__process_mmap_event(machine, event, sample); break;
2014 case PERF_RECORD_NAMESPACES:
2015 ret = machine__process_namespaces_event(machine, event, sample); break;
2016 case PERF_RECORD_CGROUP:
2017 ret = machine__process_cgroup_event(machine, event, sample); break;
2018 case PERF_RECORD_MMAP2:
2019 ret = machine__process_mmap2_event(machine, event, sample); break;
2020 case PERF_RECORD_FORK:
2021 ret = machine__process_fork_event(machine, event, sample); break;
2022 case PERF_RECORD_EXIT:
2023 ret = machine__process_exit_event(machine, event, sample); break;
2024 case PERF_RECORD_LOST:
2025 ret = machine__process_lost_event(machine, event, sample); break;
2026 case PERF_RECORD_AUX:
2027 ret = machine__process_aux_event(machine, event); break;
2028 case PERF_RECORD_ITRACE_START:
2029 ret = machine__process_itrace_start_event(machine, event); break;
2030 case PERF_RECORD_LOST_SAMPLES:
2031 ret = machine__process_lost_samples_event(machine, event, sample); break;
2032 case PERF_RECORD_SWITCH:
2033 case PERF_RECORD_SWITCH_CPU_WIDE:
2034 ret = machine__process_switch_event(machine, event); break;
2035 case PERF_RECORD_KSYMBOL:
2036 ret = machine__process_ksymbol(machine, event, sample); break;
2037 case PERF_RECORD_BPF_EVENT:
2038 ret = machine__process_bpf(machine, event, sample); break;
2039 case PERF_RECORD_TEXT_POKE:
2040 ret = machine__process_text_poke(machine, event, sample); break;
2041 case PERF_RECORD_AUX_OUTPUT_HW_ID:
2042 ret = machine__process_aux_output_hw_id_event(machine, event); break;
2051 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
2053 if (!regexec(regex, sym->name, 0, NULL, 0))
2058 static void ip__resolve_ams(struct thread *thread,
2059 struct addr_map_symbol *ams,
2062 struct addr_location al;
2064 memset(&al, 0, sizeof(al));
2066 * We cannot use the header.misc hint to determine whether a
2067 * branch stack address is user, kernel, guest, hypervisor.
2068 * Branches may straddle the kernel/user/hypervisor boundaries.
2069 * Thus, we have to try consecutively until we find a match
2070 * or else, the symbol is unknown
2072 thread__find_cpumode_addr_location(thread, ip, &al);
2075 ams->al_addr = al.addr;
2076 ams->al_level = al.level;
2077 ams->ms.maps = al.maps;
2078 ams->ms.sym = al.sym;
2079 ams->ms.map = al.map;
2081 ams->data_page_size = 0;
2084 static void ip__resolve_data(struct thread *thread,
2085 u8 m, struct addr_map_symbol *ams,
2086 u64 addr, u64 phys_addr, u64 daddr_page_size)
2088 struct addr_location al;
2090 memset(&al, 0, sizeof(al));
2092 thread__find_symbol(thread, m, addr, &al);
2095 ams->al_addr = al.addr;
2096 ams->al_level = al.level;
2097 ams->ms.maps = al.maps;
2098 ams->ms.sym = al.sym;
2099 ams->ms.map = al.map;
2100 ams->phys_addr = phys_addr;
2101 ams->data_page_size = daddr_page_size;
2104 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
2105 struct addr_location *al)
2107 struct mem_info *mi = mem_info__new();
2112 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
2113 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
2114 sample->addr, sample->phys_addr,
2115 sample->data_page_size);
2116 mi->data_src.val = sample->data_src;
2121 static char *callchain_srcline(struct map_symbol *ms, u64 ip)
2123 struct map *map = ms->map;
2124 char *srcline = NULL;
2126 if (!map || callchain_param.key == CCKEY_FUNCTION)
2129 srcline = srcline__tree_find(&map->dso->srclines, ip);
2131 bool show_sym = false;
2132 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2134 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
2135 ms->sym, show_sym, show_addr, ip);
2136 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2147 static int add_callchain_ip(struct thread *thread,
2148 struct callchain_cursor *cursor,
2149 struct symbol **parent,
2150 struct addr_location *root_al,
2154 struct branch_flags *flags,
2155 struct iterations *iter,
2158 struct map_symbol ms;
2159 struct addr_location al;
2160 int nr_loop_iter = 0;
2161 u64 iter_cycles = 0;
2162 const char *srcline = NULL;
2168 thread__find_cpumode_addr_location(thread, ip, &al);
2170 if (ip >= PERF_CONTEXT_MAX) {
2172 case PERF_CONTEXT_HV:
2173 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2175 case PERF_CONTEXT_KERNEL:
2176 *cpumode = PERF_RECORD_MISC_KERNEL;
2178 case PERF_CONTEXT_USER:
2179 *cpumode = PERF_RECORD_MISC_USER;
2182 pr_debug("invalid callchain context: "
2183 "%"PRId64"\n", (s64) ip);
2185 * It seems the callchain is corrupted.
2188 callchain_cursor_reset(cursor);
2193 thread__find_symbol(thread, *cpumode, ip, &al);
2196 if (al.sym != NULL) {
2197 if (perf_hpp_list.parent && !*parent &&
2198 symbol__match_regex(al.sym, &parent_regex))
2200 else if (have_ignore_callees && root_al &&
2201 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2202 /* Treat this symbol as the root,
2203 forgetting its callees. */
2205 callchain_cursor_reset(cursor);
2209 if (symbol_conf.hide_unresolved && al.sym == NULL)
2213 nr_loop_iter = iter->nr_loop_iter;
2214 iter_cycles = iter->cycles;
2220 srcline = callchain_srcline(&ms, al.addr);
2221 return callchain_cursor_append(cursor, ip, &ms,
2222 branch, flags, nr_loop_iter,
2223 iter_cycles, branch_from, srcline);
2226 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2227 struct addr_location *al)
2230 const struct branch_stack *bs = sample->branch_stack;
2231 struct branch_entry *entries = perf_sample__branch_entries(sample);
2232 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2237 for (i = 0; i < bs->nr; i++) {
2238 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2239 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2240 bi[i].flags = entries[i].flags;
2245 static void save_iterations(struct iterations *iter,
2246 struct branch_entry *be, int nr)
2250 iter->nr_loop_iter++;
2253 for (i = 0; i < nr; i++)
2254 iter->cycles += be[i].flags.cycles;
2259 #define NO_ENTRY 0xff
2261 #define PERF_MAX_BRANCH_DEPTH 127
2264 static int remove_loops(struct branch_entry *l, int nr,
2265 struct iterations *iter)
2268 unsigned char chash[CHASHSZ];
2270 memset(chash, NO_ENTRY, sizeof(chash));
2272 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2274 for (i = 0; i < nr; i++) {
2275 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2277 /* no collision handling for now */
2278 if (chash[h] == NO_ENTRY) {
2280 } else if (l[chash[h]].from == l[i].from) {
2281 bool is_loop = true;
2282 /* check if it is a real loop */
2284 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2285 if (l[j].from != l[i + off].from) {
2292 save_iterations(iter + i + off,
2295 memmove(iter + i, iter + i + off,
2298 memmove(l + i, l + i + off,
2309 static int lbr_callchain_add_kernel_ip(struct thread *thread,
2310 struct callchain_cursor *cursor,
2311 struct perf_sample *sample,
2312 struct symbol **parent,
2313 struct addr_location *root_al,
2315 bool callee, int end)
2317 struct ip_callchain *chain = sample->callchain;
2318 u8 cpumode = PERF_RECORD_MISC_USER;
2322 for (i = 0; i < end + 1; i++) {
2323 err = add_callchain_ip(thread, cursor, parent,
2324 root_al, &cpumode, chain->ips[i],
2325 false, NULL, NULL, branch_from);
2332 for (i = end; i >= 0; i--) {
2333 err = add_callchain_ip(thread, cursor, parent,
2334 root_al, &cpumode, chain->ips[i],
2335 false, NULL, NULL, branch_from);
2343 static void save_lbr_cursor_node(struct thread *thread,
2344 struct callchain_cursor *cursor,
2347 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2352 if (cursor->pos == cursor->nr) {
2353 lbr_stitch->prev_lbr_cursor[idx].valid = false;
2358 cursor->curr = cursor->first;
2360 cursor->curr = cursor->curr->next;
2361 memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
2362 sizeof(struct callchain_cursor_node));
2364 lbr_stitch->prev_lbr_cursor[idx].valid = true;
2368 static int lbr_callchain_add_lbr_ip(struct thread *thread,
2369 struct callchain_cursor *cursor,
2370 struct perf_sample *sample,
2371 struct symbol **parent,
2372 struct addr_location *root_al,
2376 struct branch_stack *lbr_stack = sample->branch_stack;
2377 struct branch_entry *entries = perf_sample__branch_entries(sample);
2378 u8 cpumode = PERF_RECORD_MISC_USER;
2379 int lbr_nr = lbr_stack->nr;
2380 struct branch_flags *flags;
2385 * The curr and pos are not used in writing session. They are cleared
2386 * in callchain_cursor_commit() when the writing session is closed.
2387 * Using curr and pos to track the current cursor node.
2389 if (thread->lbr_stitch) {
2390 cursor->curr = NULL;
2391 cursor->pos = cursor->nr;
2393 cursor->curr = cursor->first;
2394 for (i = 0; i < (int)(cursor->nr - 1); i++)
2395 cursor->curr = cursor->curr->next;
2400 /* Add LBR ip from first entries.to */
2402 flags = &entries[0].flags;
2403 *branch_from = entries[0].from;
2404 err = add_callchain_ip(thread, cursor, parent,
2405 root_al, &cpumode, ip,
2412 * The number of cursor node increases.
2413 * Move the current cursor node.
2414 * But does not need to save current cursor node for entry 0.
2415 * It's impossible to stitch the whole LBRs of previous sample.
2417 if (thread->lbr_stitch && (cursor->pos != cursor->nr)) {
2419 cursor->curr = cursor->first;
2421 cursor->curr = cursor->curr->next;
2425 /* Add LBR ip from entries.from one by one. */
2426 for (i = 0; i < lbr_nr; i++) {
2427 ip = entries[i].from;
2428 flags = &entries[i].flags;
2429 err = add_callchain_ip(thread, cursor, parent,
2430 root_al, &cpumode, ip,
2435 save_lbr_cursor_node(thread, cursor, i);
2440 /* Add LBR ip from entries.from one by one. */
2441 for (i = lbr_nr - 1; i >= 0; i--) {
2442 ip = entries[i].from;
2443 flags = &entries[i].flags;
2444 err = add_callchain_ip(thread, cursor, parent,
2445 root_al, &cpumode, ip,
2450 save_lbr_cursor_node(thread, cursor, i);
2453 /* Add LBR ip from first entries.to */
2455 flags = &entries[0].flags;
2456 *branch_from = entries[0].from;
2457 err = add_callchain_ip(thread, cursor, parent,
2458 root_al, &cpumode, ip,
2467 static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
2468 struct callchain_cursor *cursor)
2470 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2471 struct callchain_cursor_node *cnode;
2472 struct stitch_list *stitch_node;
2475 list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
2476 cnode = &stitch_node->cursor;
2478 err = callchain_cursor_append(cursor, cnode->ip,
2481 &cnode->branch_flags,
2482 cnode->nr_loop_iter,
2492 static struct stitch_list *get_stitch_node(struct thread *thread)
2494 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2495 struct stitch_list *stitch_node;
2497 if (!list_empty(&lbr_stitch->free_lists)) {
2498 stitch_node = list_first_entry(&lbr_stitch->free_lists,
2499 struct stitch_list, node);
2500 list_del(&stitch_node->node);
2505 return malloc(sizeof(struct stitch_list));
2508 static bool has_stitched_lbr(struct thread *thread,
2509 struct perf_sample *cur,
2510 struct perf_sample *prev,
2511 unsigned int max_lbr,
2514 struct branch_stack *cur_stack = cur->branch_stack;
2515 struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
2516 struct branch_stack *prev_stack = prev->branch_stack;
2517 struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
2518 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2519 int i, j, nr_identical_branches = 0;
2520 struct stitch_list *stitch_node;
2521 u64 cur_base, distance;
2523 if (!cur_stack || !prev_stack)
2526 /* Find the physical index of the base-of-stack for current sample. */
2527 cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
2529 distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
2530 (max_lbr + prev_stack->hw_idx - cur_base);
2531 /* Previous sample has shorter stack. Nothing can be stitched. */
2532 if (distance + 1 > prev_stack->nr)
2536 * Check if there are identical LBRs between two samples.
2537 * Identical LBRs must have same from, to and flags values. Also,
2538 * they have to be saved in the same LBR registers (same physical
2541 * Starts from the base-of-stack of current sample.
2543 for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
2544 if ((prev_entries[i].from != cur_entries[j].from) ||
2545 (prev_entries[i].to != cur_entries[j].to) ||
2546 (prev_entries[i].flags.value != cur_entries[j].flags.value))
2548 nr_identical_branches++;
2551 if (!nr_identical_branches)
2555 * Save the LBRs between the base-of-stack of previous sample
2556 * and the base-of-stack of current sample into lbr_stitch->lists.
2557 * These LBRs will be stitched later.
2559 for (i = prev_stack->nr - 1; i > (int)distance; i--) {
2561 if (!lbr_stitch->prev_lbr_cursor[i].valid)
2564 stitch_node = get_stitch_node(thread);
2568 memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
2569 sizeof(struct callchain_cursor_node));
2572 list_add(&stitch_node->node, &lbr_stitch->lists);
2574 list_add_tail(&stitch_node->node, &lbr_stitch->lists);
2580 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
2582 if (thread->lbr_stitch)
2585 thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch));
2586 if (!thread->lbr_stitch)
2589 thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
2590 if (!thread->lbr_stitch->prev_lbr_cursor)
2591 goto free_lbr_stitch;
2593 INIT_LIST_HEAD(&thread->lbr_stitch->lists);
2594 INIT_LIST_HEAD(&thread->lbr_stitch->free_lists);
2599 zfree(&thread->lbr_stitch);
2601 pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
2602 thread->lbr_stitch_enable = false;
2607 * Resolve LBR callstack chain sample
2609 * 1 on success get LBR callchain information
2610 * 0 no available LBR callchain information, should try fp
2611 * negative error code on other errors.
2613 static int resolve_lbr_callchain_sample(struct thread *thread,
2614 struct callchain_cursor *cursor,
2615 struct perf_sample *sample,
2616 struct symbol **parent,
2617 struct addr_location *root_al,
2619 unsigned int max_lbr)
2621 bool callee = (callchain_param.order == ORDER_CALLEE);
2622 struct ip_callchain *chain = sample->callchain;
2623 int chain_nr = min(max_stack, (int)chain->nr), i;
2624 struct lbr_stitch *lbr_stitch;
2625 bool stitched_lbr = false;
2626 u64 branch_from = 0;
2629 for (i = 0; i < chain_nr; i++) {
2630 if (chain->ips[i] == PERF_CONTEXT_USER)
2634 /* LBR only affects the user callchain */
2638 if (thread->lbr_stitch_enable && !sample->no_hw_idx &&
2639 (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
2640 lbr_stitch = thread->lbr_stitch;
2642 stitched_lbr = has_stitched_lbr(thread, sample,
2643 &lbr_stitch->prev_sample,
2646 if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2647 list_replace_init(&lbr_stitch->lists,
2648 &lbr_stitch->free_lists);
2650 memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
2655 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2656 parent, root_al, branch_from,
2661 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2662 root_al, &branch_from, true);
2667 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2674 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2678 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2679 root_al, &branch_from, false);
2684 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2685 parent, root_al, branch_from,
2693 return (err < 0) ? err : 0;
2696 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2697 struct callchain_cursor *cursor,
2698 struct symbol **parent,
2699 struct addr_location *root_al,
2700 u8 *cpumode, int ent)
2704 while (--ent >= 0) {
2705 u64 ip = chain->ips[ent];
2707 if (ip >= PERF_CONTEXT_MAX) {
2708 err = add_callchain_ip(thread, cursor, parent,
2709 root_al, cpumode, ip,
2710 false, NULL, NULL, 0);
2717 static u64 get_leaf_frame_caller(struct perf_sample *sample,
2718 struct thread *thread, int usr_idx)
2720 if (machine__normalized_is(thread->maps->machine, "arm64"))
2721 return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
2726 static int thread__resolve_callchain_sample(struct thread *thread,
2727 struct callchain_cursor *cursor,
2728 struct evsel *evsel,
2729 struct perf_sample *sample,
2730 struct symbol **parent,
2731 struct addr_location *root_al,
2734 struct branch_stack *branch = sample->branch_stack;
2735 struct branch_entry *entries = perf_sample__branch_entries(sample);
2736 struct ip_callchain *chain = sample->callchain;
2738 u8 cpumode = PERF_RECORD_MISC_USER;
2739 int i, j, err, nr_entries, usr_idx;
2742 u64 leaf_frame_caller;
2745 chain_nr = chain->nr;
2747 if (evsel__has_branch_callstack(evsel)) {
2748 struct perf_env *env = evsel__env(evsel);
2750 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2752 !env ? 0 : env->max_branches);
2754 return (err < 0) ? err : 0;
2758 * Based on DWARF debug information, some architectures skip
2759 * a callchain entry saved by the kernel.
2761 skip_idx = arch_skip_callchain_idx(thread, chain);
2764 * Add branches to call stack for easier browsing. This gives
2765 * more context for a sample than just the callers.
2767 * This uses individual histograms of paths compared to the
2768 * aggregated histograms the normal LBR mode uses.
2770 * Limitations for now:
2771 * - No extra filters
2772 * - No annotations (should annotate somehow)
2775 if (branch && callchain_param.branch_callstack) {
2776 int nr = min(max_stack, (int)branch->nr);
2777 struct branch_entry be[nr];
2778 struct iterations iter[nr];
2780 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2781 pr_warning("corrupted branch chain. skipping...\n");
2785 for (i = 0; i < nr; i++) {
2786 if (callchain_param.order == ORDER_CALLEE) {
2793 * Check for overlap into the callchain.
2794 * The return address is one off compared to
2795 * the branch entry. To adjust for this
2796 * assume the calling instruction is not longer
2799 if (i == skip_idx ||
2800 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2802 else if (be[i].from < chain->ips[first_call] &&
2803 be[i].from >= chain->ips[first_call] - 8)
2806 be[i] = entries[branch->nr - i - 1];
2809 memset(iter, 0, sizeof(struct iterations) * nr);
2810 nr = remove_loops(be, nr, iter);
2812 for (i = 0; i < nr; i++) {
2813 err = add_callchain_ip(thread, cursor, parent,
2820 err = add_callchain_ip(thread, cursor, parent, root_al,
2837 if (chain && callchain_param.order != ORDER_CALLEE) {
2838 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2839 &cpumode, chain->nr - first_call);
2841 return (err < 0) ? err : 0;
2843 for (i = first_call, nr_entries = 0;
2844 i < chain_nr && nr_entries < max_stack; i++) {
2847 if (callchain_param.order == ORDER_CALLEE)
2850 j = chain->nr - i - 1;
2852 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2857 if (ip < PERF_CONTEXT_MAX)
2859 else if (callchain_param.order != ORDER_CALLEE) {
2860 err = find_prev_cpumode(chain, thread, cursor, parent,
2861 root_al, &cpumode, j);
2863 return (err < 0) ? err : 0;
2868 * PERF_CONTEXT_USER allows us to locate where the user stack ends.
2869 * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
2870 * the index will be different in order to add the missing frame
2871 * at the right place.
2874 usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
2876 if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
2878 leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
2881 * check if leaf_frame_Caller != ip to not add the same
2885 if (leaf_frame_caller && leaf_frame_caller != ip) {
2887 err = add_callchain_ip(thread, cursor, parent,
2888 root_al, &cpumode, leaf_frame_caller,
2889 false, NULL, NULL, 0);
2891 return (err < 0) ? err : 0;
2895 err = add_callchain_ip(thread, cursor, parent,
2896 root_al, &cpumode, ip,
2897 false, NULL, NULL, 0);
2900 return (err < 0) ? err : 0;
2906 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
2908 struct symbol *sym = ms->sym;
2909 struct map *map = ms->map;
2910 struct inline_node *inline_node;
2911 struct inline_list *ilist;
2915 if (!symbol_conf.inline_name || !map || !sym)
2918 addr = map__map_ip(map, ip);
2919 addr = map__rip_2objdump(map, addr);
2921 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2923 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2926 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2929 list_for_each_entry(ilist, &inline_node->val, list) {
2930 struct map_symbol ilist_ms = {
2933 .sym = ilist->symbol,
2935 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
2936 NULL, 0, 0, 0, ilist->srcline);
2945 static int unwind_entry(struct unwind_entry *entry, void *arg)
2947 struct callchain_cursor *cursor = arg;
2948 const char *srcline = NULL;
2949 u64 addr = entry->ip;
2951 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
2954 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
2958 * Convert entry->ip from a virtual address to an offset in
2959 * its corresponding binary.
2962 addr = map__map_ip(entry->ms.map, entry->ip);
2964 srcline = callchain_srcline(&entry->ms, addr);
2965 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
2966 false, NULL, 0, 0, 0, srcline);
2969 static int thread__resolve_callchain_unwind(struct thread *thread,
2970 struct callchain_cursor *cursor,
2971 struct evsel *evsel,
2972 struct perf_sample *sample,
2975 /* Can we do dwarf post unwind? */
2976 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2977 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
2980 /* Bail out if nothing was captured. */
2981 if ((!sample->user_regs.regs) ||
2982 (!sample->user_stack.size))
2985 return unwind__get_entries(unwind_entry, cursor,
2986 thread, sample, max_stack);
2989 int thread__resolve_callchain(struct thread *thread,
2990 struct callchain_cursor *cursor,
2991 struct evsel *evsel,
2992 struct perf_sample *sample,
2993 struct symbol **parent,
2994 struct addr_location *root_al,
2999 callchain_cursor_reset(cursor);
3001 if (callchain_param.order == ORDER_CALLEE) {
3002 ret = thread__resolve_callchain_sample(thread, cursor,
3008 ret = thread__resolve_callchain_unwind(thread, cursor,
3012 ret = thread__resolve_callchain_unwind(thread, cursor,
3017 ret = thread__resolve_callchain_sample(thread, cursor,
3026 int machine__for_each_thread(struct machine *machine,
3027 int (*fn)(struct thread *thread, void *p),
3030 struct threads *threads;
3032 struct thread *thread;
3036 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3037 threads = &machine->threads[i];
3038 for (nd = rb_first_cached(&threads->entries); nd;
3040 thread = rb_entry(nd, struct thread, rb_node);
3041 rc = fn(thread, priv);
3046 list_for_each_entry(thread, &threads->dead, node) {
3047 rc = fn(thread, priv);
3055 int machines__for_each_thread(struct machines *machines,
3056 int (*fn)(struct thread *thread, void *p),
3062 rc = machine__for_each_thread(&machines->host, fn, priv);
3066 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
3067 struct machine *machine = rb_entry(nd, struct machine, rb_node);
3069 rc = machine__for_each_thread(machine, fn, priv);
3076 pid_t machine__get_current_tid(struct machine *machine, int cpu)
3078 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3080 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
3083 return machine->current_tid[cpu];
3086 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
3089 struct thread *thread;
3090 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3095 if (!machine->current_tid) {
3098 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
3099 if (!machine->current_tid)
3101 for (i = 0; i < nr_cpus; i++)
3102 machine->current_tid[i] = -1;
3105 if (cpu >= nr_cpus) {
3106 pr_err("Requested CPU %d too large. ", cpu);
3107 pr_err("Consider raising MAX_NR_CPUS\n");
3111 machine->current_tid[cpu] = tid;
3113 thread = machine__findnew_thread(machine, pid, tid);
3118 thread__put(thread);
3124 * Compares the raw arch string. N.B. see instead perf_env__arch() or
3125 * machine__normalized_is() if a normalized arch is needed.
3127 bool machine__is(struct machine *machine, const char *arch)
3129 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
3132 bool machine__normalized_is(struct machine *machine, const char *arch)
3134 return machine && !strcmp(perf_env__arch(machine->env), arch);
3137 int machine__nr_cpus_avail(struct machine *machine)
3139 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
3142 int machine__get_kernel_start(struct machine *machine)
3144 struct map *map = machine__kernel_map(machine);
3148 * The only addresses above 2^63 are kernel addresses of a 64-bit
3149 * kernel. Note that addresses are unsigned so that on a 32-bit system
3150 * all addresses including kernel addresses are less than 2^32. In
3151 * that case (32-bit system), if the kernel mapping is unknown, all
3152 * addresses will be assumed to be in user space - see
3153 * machine__kernel_ip().
3155 machine->kernel_start = 1ULL << 63;
3157 err = map__load(map);
3159 * On x86_64, PTI entry trampolines are less than the
3160 * start of kernel text, but still above 2^63. So leave
3161 * kernel_start = 1ULL << 63 for x86_64.
3163 if (!err && !machine__is(machine, "x86_64"))
3164 machine->kernel_start = map->start;
3169 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
3171 u8 addr_cpumode = cpumode;
3174 if (!machine->single_address_space)
3177 kernel_ip = machine__kernel_ip(machine, addr);
3179 case PERF_RECORD_MISC_KERNEL:
3180 case PERF_RECORD_MISC_USER:
3181 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
3182 PERF_RECORD_MISC_USER;
3184 case PERF_RECORD_MISC_GUEST_KERNEL:
3185 case PERF_RECORD_MISC_GUEST_USER:
3186 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
3187 PERF_RECORD_MISC_GUEST_USER;
3193 return addr_cpumode;
3196 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
3198 return dsos__findnew_id(&machine->dsos, filename, id);
3201 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
3203 return machine__findnew_dso_id(machine, filename, NULL);
3206 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
3208 struct machine *machine = vmachine;
3210 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
3215 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
3216 *addrp = map->unmap_ip(map, sym->start);
3220 int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
3225 list_for_each_entry(pos, &machine->dsos.head, node) {
3226 if (fn(pos, machine, priv))