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);
47 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip);
49 static struct dso *machine__kernel_dso(struct machine *machine)
51 return map__dso(machine->vmlinux_map);
54 static void dsos__init(struct dsos *dsos)
56 INIT_LIST_HEAD(&dsos->head);
58 init_rwsem(&dsos->lock);
61 static void machine__threads_init(struct machine *machine)
65 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
66 struct threads *threads = &machine->threads[i];
67 threads->entries = RB_ROOT_CACHED;
68 init_rwsem(&threads->lock);
70 INIT_LIST_HEAD(&threads->dead);
71 threads->last_match = NULL;
75 static int machine__set_mmap_name(struct machine *machine)
77 if (machine__is_host(machine))
78 machine->mmap_name = strdup("[kernel.kallsyms]");
79 else if (machine__is_default_guest(machine))
80 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
81 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
83 machine->mmap_name = NULL;
85 return machine->mmap_name ? 0 : -ENOMEM;
88 static void thread__set_guest_comm(struct thread *thread, pid_t pid)
92 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
93 thread__set_comm(thread, comm, 0);
96 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
100 memset(machine, 0, sizeof(*machine));
101 machine->kmaps = maps__new(machine);
102 if (machine->kmaps == NULL)
105 RB_CLEAR_NODE(&machine->rb_node);
106 dsos__init(&machine->dsos);
108 machine__threads_init(machine);
110 machine->vdso_info = NULL;
115 machine->id_hdr_size = 0;
116 machine->kptr_restrict_warned = false;
117 machine->comm_exec = false;
118 machine->kernel_start = 0;
119 machine->vmlinux_map = NULL;
121 machine->root_dir = strdup(root_dir);
122 if (machine->root_dir == NULL)
125 if (machine__set_mmap_name(machine))
128 if (pid != HOST_KERNEL_ID) {
129 struct thread *thread = machine__findnew_thread(machine, -1,
135 thread__set_guest_comm(thread, pid);
139 machine->current_tid = NULL;
144 zfree(&machine->kmaps);
145 zfree(&machine->root_dir);
146 zfree(&machine->mmap_name);
151 struct machine *machine__new_host(void)
153 struct machine *machine = malloc(sizeof(*machine));
155 if (machine != NULL) {
156 machine__init(machine, "", HOST_KERNEL_ID);
158 if (machine__create_kernel_maps(machine) < 0)
168 struct machine *machine__new_kallsyms(void)
170 struct machine *machine = machine__new_host();
173 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
174 * ask for not using the kcore parsing code, once this one is fixed
175 * to create a map per module.
177 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
178 machine__delete(machine);
185 static void dsos__purge(struct dsos *dsos)
189 down_write(&dsos->lock);
191 list_for_each_entry_safe(pos, n, &dsos->head, node) {
192 RB_CLEAR_NODE(&pos->rb_node);
194 list_del_init(&pos->node);
198 up_write(&dsos->lock);
201 static void dsos__exit(struct dsos *dsos)
204 exit_rwsem(&dsos->lock);
207 void machine__delete_threads(struct machine *machine)
212 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
213 struct threads *threads = &machine->threads[i];
214 down_write(&threads->lock);
215 nd = rb_first_cached(&threads->entries);
217 struct thread *t = rb_entry(nd, struct thread, rb_node);
220 __machine__remove_thread(machine, t, false);
222 up_write(&threads->lock);
226 void machine__exit(struct machine *machine)
233 machine__destroy_kernel_maps(machine);
234 maps__delete(machine->kmaps);
235 dsos__exit(&machine->dsos);
236 machine__exit_vdso(machine);
237 zfree(&machine->root_dir);
238 zfree(&machine->mmap_name);
239 zfree(&machine->current_tid);
240 zfree(&machine->kallsyms_filename);
242 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
243 struct threads *threads = &machine->threads[i];
244 struct thread *thread, *n;
246 * Forget about the dead, at this point whatever threads were
247 * left in the dead lists better have a reference count taken
248 * by who is using them, and then, when they drop those references
249 * and it finally hits zero, thread__put() will check and see that
250 * its not in the dead threads list and will not try to remove it
251 * from there, just calling thread__delete() straight away.
253 list_for_each_entry_safe(thread, n, &threads->dead, node)
254 list_del_init(&thread->node);
256 exit_rwsem(&threads->lock);
260 void machine__delete(struct machine *machine)
263 machine__exit(machine);
268 void machines__init(struct machines *machines)
270 machine__init(&machines->host, "", HOST_KERNEL_ID);
271 machines->guests = RB_ROOT_CACHED;
274 void machines__exit(struct machines *machines)
276 machine__exit(&machines->host);
280 struct machine *machines__add(struct machines *machines, pid_t pid,
281 const char *root_dir)
283 struct rb_node **p = &machines->guests.rb_root.rb_node;
284 struct rb_node *parent = NULL;
285 struct machine *pos, *machine = malloc(sizeof(*machine));
286 bool leftmost = true;
291 if (machine__init(machine, root_dir, pid) != 0) {
298 pos = rb_entry(parent, struct machine, rb_node);
307 rb_link_node(&machine->rb_node, parent, p);
308 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
310 machine->machines = machines;
315 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
319 machines->host.comm_exec = comm_exec;
321 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
322 struct machine *machine = rb_entry(nd, struct machine, rb_node);
324 machine->comm_exec = comm_exec;
328 struct machine *machines__find(struct machines *machines, pid_t pid)
330 struct rb_node **p = &machines->guests.rb_root.rb_node;
331 struct rb_node *parent = NULL;
332 struct machine *machine;
333 struct machine *default_machine = NULL;
335 if (pid == HOST_KERNEL_ID)
336 return &machines->host;
340 machine = rb_entry(parent, struct machine, rb_node);
341 if (pid < machine->pid)
343 else if (pid > machine->pid)
348 default_machine = machine;
351 return default_machine;
354 struct machine *machines__findnew(struct machines *machines, pid_t pid)
357 const char *root_dir = "";
358 struct machine *machine = machines__find(machines, pid);
360 if (machine && (machine->pid == pid))
363 if ((pid != HOST_KERNEL_ID) &&
364 (pid != DEFAULT_GUEST_KERNEL_ID) &&
365 (symbol_conf.guestmount)) {
366 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
367 if (access(path, R_OK)) {
368 static struct strlist *seen;
371 seen = strlist__new(NULL, NULL);
373 if (!strlist__has_entry(seen, path)) {
374 pr_err("Can't access file %s\n", path);
375 strlist__add(seen, path);
383 machine = machines__add(machines, pid, root_dir);
388 struct machine *machines__find_guest(struct machines *machines, pid_t pid)
390 struct machine *machine = machines__find(machines, pid);
393 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
398 * A common case for KVM test programs is that the test program acts as the
399 * hypervisor, creating, running and destroying the virtual machine, and
400 * providing the guest object code from its own object code. In this case,
401 * the VM is not running an OS, but only the functions loaded into it by the
402 * hypervisor test program, and conveniently, loaded at the same virtual
405 * Normally to resolve addresses, MMAP events are needed to map addresses
406 * back to the object code and debug symbols for that object code.
408 * Currently, there is no way to get such mapping information from guests
409 * but, in the scenario described above, the guest has the same mappings
410 * as the hypervisor, so support for that scenario can be achieved.
412 * To support that, copy the host thread's maps to the guest thread's maps.
413 * Note, we do not discover the guest until we encounter a guest event,
414 * which works well because it is not until then that we know that the host
415 * thread's maps have been set up.
417 * This function returns the guest thread. Apart from keeping the data
418 * structures sane, using a thread belonging to the guest machine, instead
419 * of the host thread, allows it to have its own comm (refer
420 * thread__set_guest_comm()).
422 static struct thread *findnew_guest_code(struct machine *machine,
423 struct machine *host_machine,
426 struct thread *host_thread;
427 struct thread *thread;
433 thread = machine__findnew_thread(machine, -1, pid);
437 /* Assume maps are set up if there are any */
438 if (maps__nr_maps(thread->maps))
441 host_thread = machine__find_thread(host_machine, -1, pid);
445 thread__set_guest_comm(thread, pid);
448 * Guest code can be found in hypervisor process at the same address
451 err = maps__clone(thread, host_thread->maps);
452 thread__put(host_thread);
459 thread__zput(thread);
463 struct thread *machines__findnew_guest_code(struct machines *machines, pid_t pid)
465 struct machine *host_machine = machines__find(machines, HOST_KERNEL_ID);
466 struct machine *machine = machines__findnew(machines, pid);
468 return findnew_guest_code(machine, host_machine, pid);
471 struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid)
473 struct machines *machines = machine->machines;
474 struct machine *host_machine;
479 host_machine = machines__find(machines, HOST_KERNEL_ID);
481 return findnew_guest_code(machine, host_machine, pid);
484 void machines__process_guests(struct machines *machines,
485 machine__process_t process, void *data)
489 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
490 struct machine *pos = rb_entry(nd, struct machine, rb_node);
495 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
497 struct rb_node *node;
498 struct machine *machine;
500 machines->host.id_hdr_size = id_hdr_size;
502 for (node = rb_first_cached(&machines->guests); node;
503 node = rb_next(node)) {
504 machine = rb_entry(node, struct machine, rb_node);
505 machine->id_hdr_size = id_hdr_size;
511 static void machine__update_thread_pid(struct machine *machine,
512 struct thread *th, pid_t pid)
514 struct thread *leader;
516 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
521 if (th->pid_ == th->tid)
524 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
529 leader->maps = maps__new(machine);
534 if (th->maps == leader->maps)
539 * Maps are created from MMAP events which provide the pid and
540 * tid. Consequently there never should be any maps on a thread
541 * with an unknown pid. Just print an error if there are.
543 if (!maps__empty(th->maps))
544 pr_err("Discarding thread maps for %d:%d\n",
549 th->maps = maps__get(leader->maps);
554 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
559 * Front-end cache - TID lookups come in blocks,
560 * so most of the time we dont have to look up
563 static struct thread*
564 __threads__get_last_match(struct threads *threads, struct machine *machine,
569 th = threads->last_match;
571 if (th->tid == tid) {
572 machine__update_thread_pid(machine, th, pid);
573 return thread__get(th);
576 threads->last_match = NULL;
582 static struct thread*
583 threads__get_last_match(struct threads *threads, struct machine *machine,
586 struct thread *th = NULL;
588 if (perf_singlethreaded)
589 th = __threads__get_last_match(threads, machine, pid, tid);
595 __threads__set_last_match(struct threads *threads, struct thread *th)
597 threads->last_match = th;
601 threads__set_last_match(struct threads *threads, struct thread *th)
603 if (perf_singlethreaded)
604 __threads__set_last_match(threads, th);
608 * Caller must eventually drop thread->refcnt returned with a successful
609 * lookup/new thread inserted.
611 static struct thread *____machine__findnew_thread(struct machine *machine,
612 struct threads *threads,
613 pid_t pid, pid_t tid,
616 struct rb_node **p = &threads->entries.rb_root.rb_node;
617 struct rb_node *parent = NULL;
619 bool leftmost = true;
621 th = threads__get_last_match(threads, machine, pid, tid);
627 th = rb_entry(parent, struct thread, rb_node);
629 if (th->tid == tid) {
630 threads__set_last_match(threads, th);
631 machine__update_thread_pid(machine, th, pid);
632 return thread__get(th);
646 th = thread__new(pid, tid);
648 rb_link_node(&th->rb_node, parent, p);
649 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
652 * We have to initialize maps separately after rb tree is updated.
654 * The reason is that we call machine__findnew_thread
655 * within thread__init_maps to find the thread
656 * leader and that would screwed the rb tree.
658 if (thread__init_maps(th, machine)) {
659 rb_erase_cached(&th->rb_node, &threads->entries);
660 RB_CLEAR_NODE(&th->rb_node);
665 * It is now in the rbtree, get a ref
668 threads__set_last_match(threads, th);
675 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
677 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
680 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
683 struct threads *threads = machine__threads(machine, tid);
686 down_write(&threads->lock);
687 th = __machine__findnew_thread(machine, pid, tid);
688 up_write(&threads->lock);
692 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
695 struct threads *threads = machine__threads(machine, tid);
698 down_read(&threads->lock);
699 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
700 up_read(&threads->lock);
705 * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
706 * So here a single thread is created for that, but actually there is a separate
707 * idle task per cpu, so there should be one 'struct thread' per cpu, but there
708 * is only 1. That causes problems for some tools, requiring workarounds. For
709 * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
711 struct thread *machine__idle_thread(struct machine *machine)
713 struct thread *thread = machine__findnew_thread(machine, 0, 0);
715 if (!thread || thread__set_comm(thread, "swapper", 0) ||
716 thread__set_namespaces(thread, 0, NULL))
717 pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
722 struct comm *machine__thread_exec_comm(struct machine *machine,
723 struct thread *thread)
725 if (machine->comm_exec)
726 return thread__exec_comm(thread);
728 return thread__comm(thread);
731 int machine__process_comm_event(struct machine *machine, union perf_event *event,
732 struct perf_sample *sample)
734 struct thread *thread = machine__findnew_thread(machine,
737 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
741 machine->comm_exec = true;
744 perf_event__fprintf_comm(event, stdout);
746 if (thread == NULL ||
747 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
748 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
757 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
758 union perf_event *event,
759 struct perf_sample *sample __maybe_unused)
761 struct thread *thread = machine__findnew_thread(machine,
762 event->namespaces.pid,
763 event->namespaces.tid);
766 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
767 "\nWARNING: kernel seems to support more namespaces than perf"
768 " tool.\nTry updating the perf tool..\n\n");
770 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
771 "\nWARNING: perf tool seems to support more namespaces than"
772 " the kernel.\nTry updating the kernel..\n\n");
775 perf_event__fprintf_namespaces(event, stdout);
777 if (thread == NULL ||
778 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
779 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
788 int machine__process_cgroup_event(struct machine *machine,
789 union perf_event *event,
790 struct perf_sample *sample __maybe_unused)
795 perf_event__fprintf_cgroup(event, stdout);
797 cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
804 int machine__process_lost_event(struct machine *machine __maybe_unused,
805 union perf_event *event, struct perf_sample *sample __maybe_unused)
807 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
808 event->lost.id, event->lost.lost);
812 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
813 union perf_event *event, struct perf_sample *sample)
815 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
816 sample->id, event->lost_samples.lost);
820 static struct dso *machine__findnew_module_dso(struct machine *machine,
822 const char *filename)
826 down_write(&machine->dsos.lock);
828 dso = __dsos__find(&machine->dsos, m->name, true);
830 dso = __dsos__addnew(&machine->dsos, m->name);
834 dso__set_module_info(dso, m, machine);
835 dso__set_long_name(dso, strdup(filename), true);
836 dso->kernel = DSO_SPACE__KERNEL;
841 up_write(&machine->dsos.lock);
845 int machine__process_aux_event(struct machine *machine __maybe_unused,
846 union perf_event *event)
849 perf_event__fprintf_aux(event, stdout);
853 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
854 union perf_event *event)
857 perf_event__fprintf_itrace_start(event, stdout);
861 int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
862 union perf_event *event)
865 perf_event__fprintf_aux_output_hw_id(event, stdout);
869 int machine__process_switch_event(struct machine *machine __maybe_unused,
870 union perf_event *event)
873 perf_event__fprintf_switch(event, stdout);
877 static int machine__process_ksymbol_register(struct machine *machine,
878 union perf_event *event,
879 struct perf_sample *sample __maybe_unused)
883 struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
884 bool put_map = false;
888 dso = dso__new(event->ksymbol.name);
894 dso->kernel = DSO_SPACE__KERNEL;
895 map = map__new2(0, dso);
902 * The inserted map has a get on it, we need to put to release
903 * the reference count here, but do it after all accesses are
907 if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
908 dso->binary_type = DSO_BINARY_TYPE__OOL;
909 dso->data.file_size = event->ksymbol.len;
910 dso__set_loaded(dso);
913 map__set_start(map, event->ksymbol.addr);
914 map__set_end(map, map__start(map) + event->ksymbol.len);
915 err = maps__insert(machine__kernel_maps(machine), map);
921 dso__set_loaded(dso);
923 if (is_bpf_image(event->ksymbol.name)) {
924 dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE;
925 dso__set_long_name(dso, "", false);
931 sym = symbol__new(map__map_ip(map, map__start(map)),
933 0, 0, event->ksymbol.name);
938 dso__insert_symbol(dso, sym);
945 static int machine__process_ksymbol_unregister(struct machine *machine,
946 union perf_event *event,
947 struct perf_sample *sample __maybe_unused)
952 map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
956 if (RC_CHK_ACCESS(map) != RC_CHK_ACCESS(machine->vmlinux_map))
957 maps__remove(machine__kernel_maps(machine), map);
959 struct dso *dso = map__dso(map);
961 sym = dso__find_symbol(dso, map__map_ip(map, map__start(map)));
963 dso__delete_symbol(dso, sym);
969 int machine__process_ksymbol(struct machine *machine __maybe_unused,
970 union perf_event *event,
971 struct perf_sample *sample)
974 perf_event__fprintf_ksymbol(event, stdout);
976 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
977 return machine__process_ksymbol_unregister(machine, event,
979 return machine__process_ksymbol_register(machine, event, sample);
982 int machine__process_text_poke(struct machine *machine, union perf_event *event,
983 struct perf_sample *sample __maybe_unused)
985 struct map *map = maps__find(machine__kernel_maps(machine), event->text_poke.addr);
986 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
987 struct dso *dso = map ? map__dso(map) : NULL;
990 perf_event__fprintf_text_poke(event, machine, stdout);
992 if (!event->text_poke.new_len)
995 if (cpumode != PERF_RECORD_MISC_KERNEL) {
996 pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
1001 u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
1005 * Kernel maps might be changed when loading symbols so loading
1006 * must be done prior to using kernel maps.
1009 ret = dso__data_write_cache_addr(dso, map, machine,
1010 event->text_poke.addr,
1012 event->text_poke.new_len);
1013 if (ret != event->text_poke.new_len)
1014 pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
1015 event->text_poke.addr);
1017 pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
1018 event->text_poke.addr);
1024 static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
1025 const char *filename)
1027 struct map *map = NULL;
1032 if (kmod_path__parse_name(&m, filename))
1035 dso = machine__findnew_module_dso(machine, &m, filename);
1039 map = map__new2(start, dso);
1043 err = maps__insert(machine__kernel_maps(machine), map);
1044 /* If maps__insert failed, return NULL. */
1050 /* put the dso here, corresponding to machine__findnew_module_dso */
1056 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
1059 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
1061 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
1062 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1063 ret += __dsos__fprintf(&pos->dsos.head, fp);
1069 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
1070 bool (skip)(struct dso *dso, int parm), int parm)
1072 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
1075 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
1076 bool (skip)(struct dso *dso, int parm), int parm)
1079 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
1081 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
1082 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1083 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
1088 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1092 struct dso *kdso = machine__kernel_dso(machine);
1094 if (kdso->has_build_id) {
1095 char filename[PATH_MAX];
1096 if (dso__build_id_filename(kdso, filename, sizeof(filename),
1098 printed += fprintf(fp, "[0] %s\n", filename);
1101 for (i = 0; i < vmlinux_path__nr_entries; ++i)
1102 printed += fprintf(fp, "[%d] %s\n",
1103 i + kdso->has_build_id, vmlinux_path[i]);
1108 size_t machine__fprintf(struct machine *machine, FILE *fp)
1114 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
1115 struct threads *threads = &machine->threads[i];
1117 down_read(&threads->lock);
1119 ret = fprintf(fp, "Threads: %u\n", threads->nr);
1121 for (nd = rb_first_cached(&threads->entries); nd;
1123 struct thread *pos = rb_entry(nd, struct thread, rb_node);
1125 ret += thread__fprintf(pos, fp);
1128 up_read(&threads->lock);
1133 static struct dso *machine__get_kernel(struct machine *machine)
1135 const char *vmlinux_name = machine->mmap_name;
1138 if (machine__is_host(machine)) {
1139 if (symbol_conf.vmlinux_name)
1140 vmlinux_name = symbol_conf.vmlinux_name;
1142 kernel = machine__findnew_kernel(machine, vmlinux_name,
1143 "[kernel]", DSO_SPACE__KERNEL);
1145 if (symbol_conf.default_guest_vmlinux_name)
1146 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1148 kernel = machine__findnew_kernel(machine, vmlinux_name,
1150 DSO_SPACE__KERNEL_GUEST);
1153 if (kernel != NULL && (!kernel->has_build_id))
1154 dso__read_running_kernel_build_id(kernel, machine);
1159 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
1162 if (machine__is_default_guest(machine))
1163 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
1165 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
1168 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
1170 /* Figure out the start address of kernel map from /proc/kallsyms.
1171 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
1172 * symbol_name if it's not that important.
1174 static int machine__get_running_kernel_start(struct machine *machine,
1175 const char **symbol_name,
1176 u64 *start, u64 *end)
1178 char filename[PATH_MAX];
1183 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
1185 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1188 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
1189 err = kallsyms__get_function_start(filename, name, &addr);
1198 *symbol_name = name;
1202 err = kallsyms__get_function_start(filename, "_etext", &addr);
1209 int machine__create_extra_kernel_map(struct machine *machine,
1211 struct extra_kernel_map *xm)
1217 map = map__new2(xm->start, kernel);
1221 map__set_end(map, xm->end);
1222 map__set_pgoff(map, xm->pgoff);
1224 kmap = map__kmap(map);
1226 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
1228 err = maps__insert(machine__kernel_maps(machine), map);
1231 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1232 kmap->name, map__start(map), map__end(map));
1240 static u64 find_entry_trampoline(struct dso *dso)
1242 /* Duplicates are removed so lookup all aliases */
1243 const char *syms[] = {
1244 "_entry_trampoline",
1245 "__entry_trampoline_start",
1246 "entry_SYSCALL_64_trampoline",
1248 struct symbol *sym = dso__first_symbol(dso);
1251 for (; sym; sym = dso__next_symbol(sym)) {
1252 if (sym->binding != STB_GLOBAL)
1254 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1255 if (!strcmp(sym->name, syms[i]))
1264 * These values can be used for kernels that do not have symbols for the entry
1265 * trampolines in kallsyms.
1267 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1268 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1269 #define X86_64_ENTRY_TRAMPOLINE 0x6000
1271 /* Map x86_64 PTI entry trampolines */
1272 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1275 struct maps *kmaps = machine__kernel_maps(machine);
1276 int nr_cpus_avail, cpu;
1278 struct map_rb_node *rb_node;
1282 * In the vmlinux case, pgoff is a virtual address which must now be
1283 * mapped to a vmlinux offset.
1285 maps__for_each_entry(kmaps, rb_node) {
1286 struct map *dest_map, *map = rb_node->map;
1287 struct kmap *kmap = __map__kmap(map);
1289 if (!kmap || !is_entry_trampoline(kmap->name))
1292 dest_map = maps__find(kmaps, map__pgoff(map));
1293 if (dest_map != map)
1294 map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map)));
1297 if (found || machine->trampolines_mapped)
1300 pgoff = find_entry_trampoline(kernel);
1304 nr_cpus_avail = machine__nr_cpus_avail(machine);
1306 /* Add a 1 page map for each CPU's entry trampoline */
1307 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1308 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1309 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1310 X86_64_ENTRY_TRAMPOLINE;
1311 struct extra_kernel_map xm = {
1313 .end = va + page_size,
1317 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1319 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1323 machine->trampolines_mapped = nr_cpus_avail;
1328 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1329 struct dso *kernel __maybe_unused)
1335 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1337 /* In case of renewal the kernel map, destroy previous one */
1338 machine__destroy_kernel_maps(machine);
1340 map__put(machine->vmlinux_map);
1341 machine->vmlinux_map = map__new2(0, kernel);
1342 if (machine->vmlinux_map == NULL)
1345 map__set_map_ip(machine->vmlinux_map, identity__map_ip);
1346 map__set_unmap_ip(machine->vmlinux_map, identity__map_ip);
1347 return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
1350 void machine__destroy_kernel_maps(struct machine *machine)
1353 struct map *map = machine__kernel_map(machine);
1358 kmap = map__kmap(map);
1359 maps__remove(machine__kernel_maps(machine), map);
1360 if (kmap && kmap->ref_reloc_sym) {
1361 zfree((char **)&kmap->ref_reloc_sym->name);
1362 zfree(&kmap->ref_reloc_sym);
1365 map__zput(machine->vmlinux_map);
1368 int machines__create_guest_kernel_maps(struct machines *machines)
1371 struct dirent **namelist = NULL;
1373 char path[PATH_MAX];
1377 if (symbol_conf.default_guest_vmlinux_name ||
1378 symbol_conf.default_guest_modules ||
1379 symbol_conf.default_guest_kallsyms) {
1380 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1383 if (symbol_conf.guestmount) {
1384 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1387 for (i = 0; i < items; i++) {
1388 if (!isdigit(namelist[i]->d_name[0])) {
1389 /* Filter out . and .. */
1392 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1393 if ((*endp != '\0') ||
1394 (endp == namelist[i]->d_name) ||
1395 (errno == ERANGE)) {
1396 pr_debug("invalid directory (%s). Skipping.\n",
1397 namelist[i]->d_name);
1400 sprintf(path, "%s/%s/proc/kallsyms",
1401 symbol_conf.guestmount,
1402 namelist[i]->d_name);
1403 ret = access(path, R_OK);
1405 pr_debug("Can't access file %s\n", path);
1408 machines__create_kernel_maps(machines, pid);
1417 void machines__destroy_kernel_maps(struct machines *machines)
1419 struct rb_node *next = rb_first_cached(&machines->guests);
1421 machine__destroy_kernel_maps(&machines->host);
1424 struct machine *pos = rb_entry(next, struct machine, rb_node);
1426 next = rb_next(&pos->rb_node);
1427 rb_erase_cached(&pos->rb_node, &machines->guests);
1428 machine__delete(pos);
1432 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1434 struct machine *machine = machines__findnew(machines, pid);
1436 if (machine == NULL)
1439 return machine__create_kernel_maps(machine);
1442 int machine__load_kallsyms(struct machine *machine, const char *filename)
1444 struct map *map = machine__kernel_map(machine);
1445 struct dso *dso = map__dso(map);
1446 int ret = __dso__load_kallsyms(dso, filename, map, true);
1449 dso__set_loaded(dso);
1451 * Since /proc/kallsyms will have multiple sessions for the
1452 * kernel, with modules between them, fixup the end of all
1455 maps__fixup_end(machine__kernel_maps(machine));
1461 int machine__load_vmlinux_path(struct machine *machine)
1463 struct map *map = machine__kernel_map(machine);
1464 struct dso *dso = map__dso(map);
1465 int ret = dso__load_vmlinux_path(dso, map);
1468 dso__set_loaded(dso);
1473 static char *get_kernel_version(const char *root_dir)
1475 char version[PATH_MAX];
1478 const char *prefix = "Linux version ";
1480 sprintf(version, "%s/proc/version", root_dir);
1481 file = fopen(version, "r");
1485 tmp = fgets(version, sizeof(version), file);
1490 name = strstr(version, prefix);
1493 name += strlen(prefix);
1494 tmp = strchr(name, ' ');
1498 return strdup(name);
1501 static bool is_kmod_dso(struct dso *dso)
1503 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1504 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1507 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1511 struct map *map = maps__find_by_name(maps, m->name);
1516 long_name = strdup(path);
1517 if (long_name == NULL)
1520 dso = map__dso(map);
1521 dso__set_long_name(dso, long_name, true);
1522 dso__kernel_module_get_build_id(dso, "");
1525 * Full name could reveal us kmod compression, so
1526 * we need to update the symtab_type if needed.
1528 if (m->comp && is_kmod_dso(dso)) {
1530 dso->comp = m->comp;
1536 static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
1538 struct dirent *dent;
1539 DIR *dir = opendir(dir_name);
1543 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1547 while ((dent = readdir(dir)) != NULL) {
1548 char path[PATH_MAX];
1551 /*sshfs might return bad dent->d_type, so we have to stat*/
1552 path__join(path, sizeof(path), dir_name, dent->d_name);
1553 if (stat(path, &st))
1556 if (S_ISDIR(st.st_mode)) {
1557 if (!strcmp(dent->d_name, ".") ||
1558 !strcmp(dent->d_name, ".."))
1561 /* Do not follow top-level source and build symlinks */
1563 if (!strcmp(dent->d_name, "source") ||
1564 !strcmp(dent->d_name, "build"))
1568 ret = maps__set_modules_path_dir(maps, path, depth + 1);
1574 ret = kmod_path__parse_name(&m, dent->d_name);
1579 ret = maps__set_module_path(maps, path, &m);
1593 static int machine__set_modules_path(struct machine *machine)
1596 char modules_path[PATH_MAX];
1598 version = get_kernel_version(machine->root_dir);
1602 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1603 machine->root_dir, version);
1606 return maps__set_modules_path_dir(machine__kernel_maps(machine), modules_path, 0);
1608 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1609 u64 *size __maybe_unused,
1610 const char *name __maybe_unused)
1615 static int machine__create_module(void *arg, const char *name, u64 start,
1618 struct machine *machine = arg;
1621 if (arch__fix_module_text_start(&start, &size, name) < 0)
1624 map = machine__addnew_module_map(machine, start, name);
1627 map__set_end(map, start + size);
1629 dso__kernel_module_get_build_id(map__dso(map), machine->root_dir);
1634 static int machine__create_modules(struct machine *machine)
1636 const char *modules;
1637 char path[PATH_MAX];
1639 if (machine__is_default_guest(machine)) {
1640 modules = symbol_conf.default_guest_modules;
1642 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1646 if (symbol__restricted_filename(modules, "/proc/modules"))
1649 if (modules__parse(modules, machine, machine__create_module))
1652 if (!machine__set_modules_path(machine))
1655 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1660 static void machine__set_kernel_mmap(struct machine *machine,
1663 map__set_start(machine->vmlinux_map, start);
1664 map__set_end(machine->vmlinux_map, end);
1666 * Be a bit paranoid here, some perf.data file came with
1667 * a zero sized synthesized MMAP event for the kernel.
1669 if (start == 0 && end == 0)
1670 map__set_end(machine->vmlinux_map, ~0ULL);
1673 static int machine__update_kernel_mmap(struct machine *machine,
1676 struct map *orig, *updated;
1679 orig = machine->vmlinux_map;
1680 updated = map__get(orig);
1682 machine->vmlinux_map = updated;
1683 machine__set_kernel_mmap(machine, start, end);
1684 maps__remove(machine__kernel_maps(machine), orig);
1685 err = maps__insert(machine__kernel_maps(machine), updated);
1691 int machine__create_kernel_maps(struct machine *machine)
1693 struct dso *kernel = machine__get_kernel(machine);
1694 const char *name = NULL;
1695 u64 start = 0, end = ~0ULL;
1701 ret = __machine__create_kernel_maps(machine, kernel);
1705 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1706 if (machine__is_host(machine))
1707 pr_debug("Problems creating module maps, "
1708 "continuing anyway...\n");
1710 pr_debug("Problems creating module maps for guest %d, "
1711 "continuing anyway...\n", machine->pid);
1714 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1716 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1717 machine__destroy_kernel_maps(machine);
1723 * we have a real start address now, so re-order the kmaps
1724 * assume it's the last in the kmaps
1726 ret = machine__update_kernel_mmap(machine, start, end);
1731 if (machine__create_extra_kernel_maps(machine, kernel))
1732 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1735 /* update end address of the kernel map using adjacent module address */
1736 struct map_rb_node *rb_node = maps__find_node(machine__kernel_maps(machine),
1737 machine__kernel_map(machine));
1738 struct map_rb_node *next = map_rb_node__next(rb_node);
1741 machine__set_kernel_mmap(machine, start, map__start(next->map));
1749 static bool machine__uses_kcore(struct machine *machine)
1753 list_for_each_entry(dso, &machine->dsos.head, node) {
1754 if (dso__is_kcore(dso))
1761 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1762 struct extra_kernel_map *xm)
1764 return machine__is(machine, "x86_64") &&
1765 is_entry_trampoline(xm->name);
1768 static int machine__process_extra_kernel_map(struct machine *machine,
1769 struct extra_kernel_map *xm)
1771 struct dso *kernel = machine__kernel_dso(machine);
1776 return machine__create_extra_kernel_map(machine, kernel, xm);
1779 static int machine__process_kernel_mmap_event(struct machine *machine,
1780 struct extra_kernel_map *xm,
1781 struct build_id *bid)
1784 enum dso_space_type dso_space;
1785 bool is_kernel_mmap;
1786 const char *mmap_name = machine->mmap_name;
1788 /* If we have maps from kcore then we do not need or want any others */
1789 if (machine__uses_kcore(machine))
1792 if (machine__is_host(machine))
1793 dso_space = DSO_SPACE__KERNEL;
1795 dso_space = DSO_SPACE__KERNEL_GUEST;
1797 is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0;
1798 if (!is_kernel_mmap && !machine__is_host(machine)) {
1800 * If the event was recorded inside the guest and injected into
1801 * the host perf.data file, then it will match a host mmap_name,
1802 * so try that - see machine__set_mmap_name().
1804 mmap_name = "[kernel.kallsyms]";
1805 is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0;
1807 if (xm->name[0] == '/' ||
1808 (!is_kernel_mmap && xm->name[0] == '[')) {
1809 map = machine__addnew_module_map(machine, xm->start,
1814 map__set_end(map, map__start(map) + xm->end - xm->start);
1816 if (build_id__is_defined(bid))
1817 dso__set_build_id(map__dso(map), bid);
1819 } else if (is_kernel_mmap) {
1820 const char *symbol_name = xm->name + strlen(mmap_name);
1822 * Should be there already, from the build-id table in
1825 struct dso *kernel = NULL;
1828 down_read(&machine->dsos.lock);
1830 list_for_each_entry(dso, &machine->dsos.head, node) {
1833 * The cpumode passed to is_kernel_module is not the
1834 * cpumode of *this* event. If we insist on passing
1835 * correct cpumode to is_kernel_module, we should
1836 * record the cpumode when we adding this dso to the
1839 * However we don't really need passing correct
1840 * cpumode. We know the correct cpumode must be kernel
1841 * mode (if not, we should not link it onto kernel_dsos
1844 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1845 * is_kernel_module() treats it as a kernel cpumode.
1849 is_kernel_module(dso->long_name,
1850 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1858 up_read(&machine->dsos.lock);
1861 kernel = machine__findnew_dso(machine, machine->mmap_name);
1865 kernel->kernel = dso_space;
1866 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1871 if (strstr(kernel->long_name, "vmlinux"))
1872 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1874 if (machine__update_kernel_mmap(machine, xm->start, xm->end) < 0) {
1879 if (build_id__is_defined(bid))
1880 dso__set_build_id(kernel, bid);
1883 * Avoid using a zero address (kptr_restrict) for the ref reloc
1884 * symbol. Effectively having zero here means that at record
1885 * time /proc/sys/kernel/kptr_restrict was non zero.
1887 if (xm->pgoff != 0) {
1888 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1893 if (machine__is_default_guest(machine)) {
1895 * preload dso of guest kernel and modules
1897 dso__load(kernel, machine__kernel_map(machine));
1899 } else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
1900 return machine__process_extra_kernel_map(machine, xm);
1907 int machine__process_mmap2_event(struct machine *machine,
1908 union perf_event *event,
1909 struct perf_sample *sample)
1911 struct thread *thread;
1913 struct dso_id dso_id = {
1914 .maj = event->mmap2.maj,
1915 .min = event->mmap2.min,
1916 .ino = event->mmap2.ino,
1917 .ino_generation = event->mmap2.ino_generation,
1919 struct build_id __bid, *bid = NULL;
1923 perf_event__fprintf_mmap2(event, stdout);
1925 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
1927 build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size);
1930 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1931 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1932 struct extra_kernel_map xm = {
1933 .start = event->mmap2.start,
1934 .end = event->mmap2.start + event->mmap2.len,
1935 .pgoff = event->mmap2.pgoff,
1938 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
1939 ret = machine__process_kernel_mmap_event(machine, &xm, bid);
1945 thread = machine__findnew_thread(machine, event->mmap2.pid,
1950 map = map__new(machine, event->mmap2.start,
1951 event->mmap2.len, event->mmap2.pgoff,
1952 &dso_id, event->mmap2.prot,
1953 event->mmap2.flags, bid,
1954 event->mmap2.filename, thread);
1957 goto out_problem_map;
1959 ret = thread__insert_map(thread, map);
1961 goto out_problem_insert;
1963 thread__put(thread);
1970 thread__put(thread);
1972 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1976 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1977 struct perf_sample *sample)
1979 struct thread *thread;
1985 perf_event__fprintf_mmap(event, stdout);
1987 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1988 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1989 struct extra_kernel_map xm = {
1990 .start = event->mmap.start,
1991 .end = event->mmap.start + event->mmap.len,
1992 .pgoff = event->mmap.pgoff,
1995 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1996 ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
2002 thread = machine__findnew_thread(machine, event->mmap.pid,
2007 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
2010 map = map__new(machine, event->mmap.start,
2011 event->mmap.len, event->mmap.pgoff,
2012 NULL, prot, 0, NULL, event->mmap.filename, thread);
2015 goto out_problem_map;
2017 ret = thread__insert_map(thread, map);
2019 goto out_problem_insert;
2021 thread__put(thread);
2028 thread__put(thread);
2030 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
2034 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
2036 struct threads *threads = machine__threads(machine, th->tid);
2038 if (threads->last_match == th)
2039 threads__set_last_match(threads, NULL);
2042 down_write(&threads->lock);
2044 BUG_ON(refcount_read(&th->refcnt) == 0);
2046 rb_erase_cached(&th->rb_node, &threads->entries);
2047 RB_CLEAR_NODE(&th->rb_node);
2050 * Move it first to the dead_threads list, then drop the reference,
2051 * if this is the last reference, then the thread__delete destructor
2052 * will be called and we will remove it from the dead_threads list.
2054 list_add_tail(&th->node, &threads->dead);
2057 * We need to do the put here because if this is the last refcount,
2058 * then we will be touching the threads->dead head when removing the
2064 up_write(&threads->lock);
2067 void machine__remove_thread(struct machine *machine, struct thread *th)
2069 return __machine__remove_thread(machine, th, true);
2072 int machine__process_fork_event(struct machine *machine, union perf_event *event,
2073 struct perf_sample *sample)
2075 struct thread *thread = machine__find_thread(machine,
2078 struct thread *parent = machine__findnew_thread(machine,
2081 bool do_maps_clone = true;
2085 perf_event__fprintf_task(event, stdout);
2088 * There may be an existing thread that is not actually the parent,
2089 * either because we are processing events out of order, or because the
2090 * (fork) event that would have removed the thread was lost. Assume the
2091 * latter case and continue on as best we can.
2093 if (parent->pid_ != (pid_t)event->fork.ppid) {
2094 dump_printf("removing erroneous parent thread %d/%d\n",
2095 parent->pid_, parent->tid);
2096 machine__remove_thread(machine, parent);
2097 thread__put(parent);
2098 parent = machine__findnew_thread(machine, event->fork.ppid,
2102 /* if a thread currently exists for the thread id remove it */
2103 if (thread != NULL) {
2104 machine__remove_thread(machine, thread);
2105 thread__put(thread);
2108 thread = machine__findnew_thread(machine, event->fork.pid,
2111 * When synthesizing FORK events, we are trying to create thread
2112 * objects for the already running tasks on the machine.
2114 * Normally, for a kernel FORK event, we want to clone the parent's
2115 * maps because that is what the kernel just did.
2117 * But when synthesizing, this should not be done. If we do, we end up
2118 * with overlapping maps as we process the synthesized MMAP2 events that
2119 * get delivered shortly thereafter.
2121 * Use the FORK event misc flags in an internal way to signal this
2122 * situation, so we can elide the map clone when appropriate.
2124 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
2125 do_maps_clone = false;
2127 if (thread == NULL || parent == NULL ||
2128 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
2129 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
2132 thread__put(thread);
2133 thread__put(parent);
2138 int machine__process_exit_event(struct machine *machine, union perf_event *event,
2139 struct perf_sample *sample __maybe_unused)
2141 struct thread *thread = machine__find_thread(machine,
2146 perf_event__fprintf_task(event, stdout);
2148 if (thread != NULL) {
2149 thread__exited(thread);
2150 thread__put(thread);
2156 int machine__process_event(struct machine *machine, union perf_event *event,
2157 struct perf_sample *sample)
2161 switch (event->header.type) {
2162 case PERF_RECORD_COMM:
2163 ret = machine__process_comm_event(machine, event, sample); break;
2164 case PERF_RECORD_MMAP:
2165 ret = machine__process_mmap_event(machine, event, sample); break;
2166 case PERF_RECORD_NAMESPACES:
2167 ret = machine__process_namespaces_event(machine, event, sample); break;
2168 case PERF_RECORD_CGROUP:
2169 ret = machine__process_cgroup_event(machine, event, sample); break;
2170 case PERF_RECORD_MMAP2:
2171 ret = machine__process_mmap2_event(machine, event, sample); break;
2172 case PERF_RECORD_FORK:
2173 ret = machine__process_fork_event(machine, event, sample); break;
2174 case PERF_RECORD_EXIT:
2175 ret = machine__process_exit_event(machine, event, sample); break;
2176 case PERF_RECORD_LOST:
2177 ret = machine__process_lost_event(machine, event, sample); break;
2178 case PERF_RECORD_AUX:
2179 ret = machine__process_aux_event(machine, event); break;
2180 case PERF_RECORD_ITRACE_START:
2181 ret = machine__process_itrace_start_event(machine, event); break;
2182 case PERF_RECORD_LOST_SAMPLES:
2183 ret = machine__process_lost_samples_event(machine, event, sample); break;
2184 case PERF_RECORD_SWITCH:
2185 case PERF_RECORD_SWITCH_CPU_WIDE:
2186 ret = machine__process_switch_event(machine, event); break;
2187 case PERF_RECORD_KSYMBOL:
2188 ret = machine__process_ksymbol(machine, event, sample); break;
2189 case PERF_RECORD_BPF_EVENT:
2190 ret = machine__process_bpf(machine, event, sample); break;
2191 case PERF_RECORD_TEXT_POKE:
2192 ret = machine__process_text_poke(machine, event, sample); break;
2193 case PERF_RECORD_AUX_OUTPUT_HW_ID:
2194 ret = machine__process_aux_output_hw_id_event(machine, event); break;
2203 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
2205 if (!regexec(regex, sym->name, 0, NULL, 0))
2210 static void ip__resolve_ams(struct thread *thread,
2211 struct addr_map_symbol *ams,
2214 struct addr_location al;
2216 memset(&al, 0, sizeof(al));
2218 * We cannot use the header.misc hint to determine whether a
2219 * branch stack address is user, kernel, guest, hypervisor.
2220 * Branches may straddle the kernel/user/hypervisor boundaries.
2221 * Thus, we have to try consecutively until we find a match
2222 * or else, the symbol is unknown
2224 thread__find_cpumode_addr_location(thread, ip, &al);
2227 ams->al_addr = al.addr;
2228 ams->al_level = al.level;
2229 ams->ms.maps = al.maps;
2230 ams->ms.sym = al.sym;
2231 ams->ms.map = al.map;
2233 ams->data_page_size = 0;
2236 static void ip__resolve_data(struct thread *thread,
2237 u8 m, struct addr_map_symbol *ams,
2238 u64 addr, u64 phys_addr, u64 daddr_page_size)
2240 struct addr_location al;
2242 memset(&al, 0, sizeof(al));
2244 thread__find_symbol(thread, m, addr, &al);
2247 ams->al_addr = al.addr;
2248 ams->al_level = al.level;
2249 ams->ms.maps = al.maps;
2250 ams->ms.sym = al.sym;
2251 ams->ms.map = al.map;
2252 ams->phys_addr = phys_addr;
2253 ams->data_page_size = daddr_page_size;
2256 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
2257 struct addr_location *al)
2259 struct mem_info *mi = mem_info__new();
2264 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
2265 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
2266 sample->addr, sample->phys_addr,
2267 sample->data_page_size);
2268 mi->data_src.val = sample->data_src;
2273 static char *callchain_srcline(struct map_symbol *ms, u64 ip)
2275 struct map *map = ms->map;
2276 char *srcline = NULL;
2279 if (!map || callchain_param.key == CCKEY_FUNCTION)
2282 dso = map__dso(map);
2283 srcline = srcline__tree_find(&dso->srclines, ip);
2285 bool show_sym = false;
2286 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2288 srcline = get_srcline(dso, map__rip_2objdump(map, ip),
2289 ms->sym, show_sym, show_addr, ip);
2290 srcline__tree_insert(&dso->srclines, ip, srcline);
2301 static int add_callchain_ip(struct thread *thread,
2302 struct callchain_cursor *cursor,
2303 struct symbol **parent,
2304 struct addr_location *root_al,
2308 struct branch_flags *flags,
2309 struct iterations *iter,
2312 struct map_symbol ms;
2313 struct addr_location al;
2314 int nr_loop_iter = 0, err;
2315 u64 iter_cycles = 0;
2316 const char *srcline = NULL;
2322 thread__find_cpumode_addr_location(thread, ip, &al);
2324 if (ip >= PERF_CONTEXT_MAX) {
2326 case PERF_CONTEXT_HV:
2327 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2329 case PERF_CONTEXT_KERNEL:
2330 *cpumode = PERF_RECORD_MISC_KERNEL;
2332 case PERF_CONTEXT_USER:
2333 *cpumode = PERF_RECORD_MISC_USER;
2336 pr_debug("invalid callchain context: "
2337 "%"PRId64"\n", (s64) ip);
2339 * It seems the callchain is corrupted.
2342 callchain_cursor_reset(cursor);
2347 thread__find_symbol(thread, *cpumode, ip, &al);
2350 if (al.sym != NULL) {
2351 if (perf_hpp_list.parent && !*parent &&
2352 symbol__match_regex(al.sym, &parent_regex))
2354 else if (have_ignore_callees && root_al &&
2355 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2356 /* Treat this symbol as the root,
2357 forgetting its callees. */
2359 callchain_cursor_reset(cursor);
2363 if (symbol_conf.hide_unresolved && al.sym == NULL)
2367 nr_loop_iter = iter->nr_loop_iter;
2368 iter_cycles = iter->cycles;
2375 if (!branch && append_inlines(cursor, &ms, ip) == 0)
2378 srcline = callchain_srcline(&ms, al.addr);
2379 err = callchain_cursor_append(cursor, ip, &ms,
2380 branch, flags, nr_loop_iter,
2381 iter_cycles, branch_from, srcline);
2386 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2387 struct addr_location *al)
2390 const struct branch_stack *bs = sample->branch_stack;
2391 struct branch_entry *entries = perf_sample__branch_entries(sample);
2392 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2397 for (i = 0; i < bs->nr; i++) {
2398 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2399 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2400 bi[i].flags = entries[i].flags;
2405 static void save_iterations(struct iterations *iter,
2406 struct branch_entry *be, int nr)
2410 iter->nr_loop_iter++;
2413 for (i = 0; i < nr; i++)
2414 iter->cycles += be[i].flags.cycles;
2419 #define NO_ENTRY 0xff
2421 #define PERF_MAX_BRANCH_DEPTH 127
2424 static int remove_loops(struct branch_entry *l, int nr,
2425 struct iterations *iter)
2428 unsigned char chash[CHASHSZ];
2430 memset(chash, NO_ENTRY, sizeof(chash));
2432 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2434 for (i = 0; i < nr; i++) {
2435 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2437 /* no collision handling for now */
2438 if (chash[h] == NO_ENTRY) {
2440 } else if (l[chash[h]].from == l[i].from) {
2441 bool is_loop = true;
2442 /* check if it is a real loop */
2444 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2445 if (l[j].from != l[i + off].from) {
2452 save_iterations(iter + i + off,
2455 memmove(iter + i, iter + i + off,
2458 memmove(l + i, l + i + off,
2469 static int lbr_callchain_add_kernel_ip(struct thread *thread,
2470 struct callchain_cursor *cursor,
2471 struct perf_sample *sample,
2472 struct symbol **parent,
2473 struct addr_location *root_al,
2475 bool callee, int end)
2477 struct ip_callchain *chain = sample->callchain;
2478 u8 cpumode = PERF_RECORD_MISC_USER;
2482 for (i = 0; i < end + 1; i++) {
2483 err = add_callchain_ip(thread, cursor, parent,
2484 root_al, &cpumode, chain->ips[i],
2485 false, NULL, NULL, branch_from);
2492 for (i = end; i >= 0; i--) {
2493 err = add_callchain_ip(thread, cursor, parent,
2494 root_al, &cpumode, chain->ips[i],
2495 false, NULL, NULL, branch_from);
2503 static void save_lbr_cursor_node(struct thread *thread,
2504 struct callchain_cursor *cursor,
2507 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2512 if (cursor->pos == cursor->nr) {
2513 lbr_stitch->prev_lbr_cursor[idx].valid = false;
2518 cursor->curr = cursor->first;
2520 cursor->curr = cursor->curr->next;
2521 memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
2522 sizeof(struct callchain_cursor_node));
2524 lbr_stitch->prev_lbr_cursor[idx].valid = true;
2528 static int lbr_callchain_add_lbr_ip(struct thread *thread,
2529 struct callchain_cursor *cursor,
2530 struct perf_sample *sample,
2531 struct symbol **parent,
2532 struct addr_location *root_al,
2536 struct branch_stack *lbr_stack = sample->branch_stack;
2537 struct branch_entry *entries = perf_sample__branch_entries(sample);
2538 u8 cpumode = PERF_RECORD_MISC_USER;
2539 int lbr_nr = lbr_stack->nr;
2540 struct branch_flags *flags;
2545 * The curr and pos are not used in writing session. They are cleared
2546 * in callchain_cursor_commit() when the writing session is closed.
2547 * Using curr and pos to track the current cursor node.
2549 if (thread->lbr_stitch) {
2550 cursor->curr = NULL;
2551 cursor->pos = cursor->nr;
2553 cursor->curr = cursor->first;
2554 for (i = 0; i < (int)(cursor->nr - 1); i++)
2555 cursor->curr = cursor->curr->next;
2560 /* Add LBR ip from first entries.to */
2562 flags = &entries[0].flags;
2563 *branch_from = entries[0].from;
2564 err = add_callchain_ip(thread, cursor, parent,
2565 root_al, &cpumode, ip,
2572 * The number of cursor node increases.
2573 * Move the current cursor node.
2574 * But does not need to save current cursor node for entry 0.
2575 * It's impossible to stitch the whole LBRs of previous sample.
2577 if (thread->lbr_stitch && (cursor->pos != cursor->nr)) {
2579 cursor->curr = cursor->first;
2581 cursor->curr = cursor->curr->next;
2585 /* Add LBR ip from entries.from one by one. */
2586 for (i = 0; i < lbr_nr; i++) {
2587 ip = entries[i].from;
2588 flags = &entries[i].flags;
2589 err = add_callchain_ip(thread, cursor, parent,
2590 root_al, &cpumode, ip,
2595 save_lbr_cursor_node(thread, cursor, i);
2600 /* Add LBR ip from entries.from one by one. */
2601 for (i = lbr_nr - 1; i >= 0; i--) {
2602 ip = entries[i].from;
2603 flags = &entries[i].flags;
2604 err = add_callchain_ip(thread, cursor, parent,
2605 root_al, &cpumode, ip,
2610 save_lbr_cursor_node(thread, cursor, i);
2613 /* Add LBR ip from first entries.to */
2615 flags = &entries[0].flags;
2616 *branch_from = entries[0].from;
2617 err = add_callchain_ip(thread, cursor, parent,
2618 root_al, &cpumode, ip,
2627 static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
2628 struct callchain_cursor *cursor)
2630 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2631 struct callchain_cursor_node *cnode;
2632 struct stitch_list *stitch_node;
2635 list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
2636 cnode = &stitch_node->cursor;
2638 err = callchain_cursor_append(cursor, cnode->ip,
2641 &cnode->branch_flags,
2642 cnode->nr_loop_iter,
2652 static struct stitch_list *get_stitch_node(struct thread *thread)
2654 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2655 struct stitch_list *stitch_node;
2657 if (!list_empty(&lbr_stitch->free_lists)) {
2658 stitch_node = list_first_entry(&lbr_stitch->free_lists,
2659 struct stitch_list, node);
2660 list_del(&stitch_node->node);
2665 return malloc(sizeof(struct stitch_list));
2668 static bool has_stitched_lbr(struct thread *thread,
2669 struct perf_sample *cur,
2670 struct perf_sample *prev,
2671 unsigned int max_lbr,
2674 struct branch_stack *cur_stack = cur->branch_stack;
2675 struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
2676 struct branch_stack *prev_stack = prev->branch_stack;
2677 struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
2678 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2679 int i, j, nr_identical_branches = 0;
2680 struct stitch_list *stitch_node;
2681 u64 cur_base, distance;
2683 if (!cur_stack || !prev_stack)
2686 /* Find the physical index of the base-of-stack for current sample. */
2687 cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
2689 distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
2690 (max_lbr + prev_stack->hw_idx - cur_base);
2691 /* Previous sample has shorter stack. Nothing can be stitched. */
2692 if (distance + 1 > prev_stack->nr)
2696 * Check if there are identical LBRs between two samples.
2697 * Identical LBRs must have same from, to and flags values. Also,
2698 * they have to be saved in the same LBR registers (same physical
2701 * Starts from the base-of-stack of current sample.
2703 for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
2704 if ((prev_entries[i].from != cur_entries[j].from) ||
2705 (prev_entries[i].to != cur_entries[j].to) ||
2706 (prev_entries[i].flags.value != cur_entries[j].flags.value))
2708 nr_identical_branches++;
2711 if (!nr_identical_branches)
2715 * Save the LBRs between the base-of-stack of previous sample
2716 * and the base-of-stack of current sample into lbr_stitch->lists.
2717 * These LBRs will be stitched later.
2719 for (i = prev_stack->nr - 1; i > (int)distance; i--) {
2721 if (!lbr_stitch->prev_lbr_cursor[i].valid)
2724 stitch_node = get_stitch_node(thread);
2728 memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
2729 sizeof(struct callchain_cursor_node));
2732 list_add(&stitch_node->node, &lbr_stitch->lists);
2734 list_add_tail(&stitch_node->node, &lbr_stitch->lists);
2740 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
2742 if (thread->lbr_stitch)
2745 thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch));
2746 if (!thread->lbr_stitch)
2749 thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
2750 if (!thread->lbr_stitch->prev_lbr_cursor)
2751 goto free_lbr_stitch;
2753 INIT_LIST_HEAD(&thread->lbr_stitch->lists);
2754 INIT_LIST_HEAD(&thread->lbr_stitch->free_lists);
2759 zfree(&thread->lbr_stitch);
2761 pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
2762 thread->lbr_stitch_enable = false;
2767 * Resolve LBR callstack chain sample
2769 * 1 on success get LBR callchain information
2770 * 0 no available LBR callchain information, should try fp
2771 * negative error code on other errors.
2773 static int resolve_lbr_callchain_sample(struct thread *thread,
2774 struct callchain_cursor *cursor,
2775 struct perf_sample *sample,
2776 struct symbol **parent,
2777 struct addr_location *root_al,
2779 unsigned int max_lbr)
2781 bool callee = (callchain_param.order == ORDER_CALLEE);
2782 struct ip_callchain *chain = sample->callchain;
2783 int chain_nr = min(max_stack, (int)chain->nr), i;
2784 struct lbr_stitch *lbr_stitch;
2785 bool stitched_lbr = false;
2786 u64 branch_from = 0;
2789 for (i = 0; i < chain_nr; i++) {
2790 if (chain->ips[i] == PERF_CONTEXT_USER)
2794 /* LBR only affects the user callchain */
2798 if (thread->lbr_stitch_enable && !sample->no_hw_idx &&
2799 (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
2800 lbr_stitch = thread->lbr_stitch;
2802 stitched_lbr = has_stitched_lbr(thread, sample,
2803 &lbr_stitch->prev_sample,
2806 if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2807 list_replace_init(&lbr_stitch->lists,
2808 &lbr_stitch->free_lists);
2810 memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
2815 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2816 parent, root_al, branch_from,
2821 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2822 root_al, &branch_from, true);
2827 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2834 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2838 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2839 root_al, &branch_from, false);
2844 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2845 parent, root_al, branch_from,
2853 return (err < 0) ? err : 0;
2856 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2857 struct callchain_cursor *cursor,
2858 struct symbol **parent,
2859 struct addr_location *root_al,
2860 u8 *cpumode, int ent)
2864 while (--ent >= 0) {
2865 u64 ip = chain->ips[ent];
2867 if (ip >= PERF_CONTEXT_MAX) {
2868 err = add_callchain_ip(thread, cursor, parent,
2869 root_al, cpumode, ip,
2870 false, NULL, NULL, 0);
2877 static u64 get_leaf_frame_caller(struct perf_sample *sample,
2878 struct thread *thread, int usr_idx)
2880 if (machine__normalized_is(maps__machine(thread->maps), "arm64"))
2881 return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
2886 static int thread__resolve_callchain_sample(struct thread *thread,
2887 struct callchain_cursor *cursor,
2888 struct evsel *evsel,
2889 struct perf_sample *sample,
2890 struct symbol **parent,
2891 struct addr_location *root_al,
2894 struct branch_stack *branch = sample->branch_stack;
2895 struct branch_entry *entries = perf_sample__branch_entries(sample);
2896 struct ip_callchain *chain = sample->callchain;
2898 u8 cpumode = PERF_RECORD_MISC_USER;
2899 int i, j, err, nr_entries, usr_idx;
2902 u64 leaf_frame_caller;
2905 chain_nr = chain->nr;
2907 if (evsel__has_branch_callstack(evsel)) {
2908 struct perf_env *env = evsel__env(evsel);
2910 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2912 !env ? 0 : env->max_branches);
2914 return (err < 0) ? err : 0;
2918 * Based on DWARF debug information, some architectures skip
2919 * a callchain entry saved by the kernel.
2921 skip_idx = arch_skip_callchain_idx(thread, chain);
2924 * Add branches to call stack for easier browsing. This gives
2925 * more context for a sample than just the callers.
2927 * This uses individual histograms of paths compared to the
2928 * aggregated histograms the normal LBR mode uses.
2930 * Limitations for now:
2931 * - No extra filters
2932 * - No annotations (should annotate somehow)
2935 if (branch && callchain_param.branch_callstack) {
2936 int nr = min(max_stack, (int)branch->nr);
2937 struct branch_entry be[nr];
2938 struct iterations iter[nr];
2940 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2941 pr_warning("corrupted branch chain. skipping...\n");
2945 for (i = 0; i < nr; i++) {
2946 if (callchain_param.order == ORDER_CALLEE) {
2953 * Check for overlap into the callchain.
2954 * The return address is one off compared to
2955 * the branch entry. To adjust for this
2956 * assume the calling instruction is not longer
2959 if (i == skip_idx ||
2960 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2962 else if (be[i].from < chain->ips[first_call] &&
2963 be[i].from >= chain->ips[first_call] - 8)
2966 be[i] = entries[branch->nr - i - 1];
2969 memset(iter, 0, sizeof(struct iterations) * nr);
2970 nr = remove_loops(be, nr, iter);
2972 for (i = 0; i < nr; i++) {
2973 err = add_callchain_ip(thread, cursor, parent,
2980 err = add_callchain_ip(thread, cursor, parent, root_al,
2997 if (chain && callchain_param.order != ORDER_CALLEE) {
2998 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2999 &cpumode, chain->nr - first_call);
3001 return (err < 0) ? err : 0;
3003 for (i = first_call, nr_entries = 0;
3004 i < chain_nr && nr_entries < max_stack; i++) {
3007 if (callchain_param.order == ORDER_CALLEE)
3010 j = chain->nr - i - 1;
3012 #ifdef HAVE_SKIP_CALLCHAIN_IDX
3017 if (ip < PERF_CONTEXT_MAX)
3019 else if (callchain_param.order != ORDER_CALLEE) {
3020 err = find_prev_cpumode(chain, thread, cursor, parent,
3021 root_al, &cpumode, j);
3023 return (err < 0) ? err : 0;
3028 * PERF_CONTEXT_USER allows us to locate where the user stack ends.
3029 * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
3030 * the index will be different in order to add the missing frame
3031 * at the right place.
3034 usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
3036 if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
3038 leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
3041 * check if leaf_frame_Caller != ip to not add the same
3045 if (leaf_frame_caller && leaf_frame_caller != ip) {
3047 err = add_callchain_ip(thread, cursor, parent,
3048 root_al, &cpumode, leaf_frame_caller,
3049 false, NULL, NULL, 0);
3051 return (err < 0) ? err : 0;
3055 err = add_callchain_ip(thread, cursor, parent,
3056 root_al, &cpumode, ip,
3057 false, NULL, NULL, 0);
3060 return (err < 0) ? err : 0;
3066 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
3068 struct symbol *sym = ms->sym;
3069 struct map *map = ms->map;
3070 struct inline_node *inline_node;
3071 struct inline_list *ilist;
3076 if (!symbol_conf.inline_name || !map || !sym)
3079 addr = map__dso_map_ip(map, ip);
3080 addr = map__rip_2objdump(map, addr);
3081 dso = map__dso(map);
3083 inline_node = inlines__tree_find(&dso->inlined_nodes, addr);
3085 inline_node = dso__parse_addr_inlines(dso, addr, sym);
3088 inlines__tree_insert(&dso->inlined_nodes, inline_node);
3091 list_for_each_entry(ilist, &inline_node->val, list) {
3092 struct map_symbol ilist_ms = {
3095 .sym = ilist->symbol,
3097 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
3098 NULL, 0, 0, 0, ilist->srcline);
3107 static int unwind_entry(struct unwind_entry *entry, void *arg)
3109 struct callchain_cursor *cursor = arg;
3110 const char *srcline = NULL;
3111 u64 addr = entry->ip;
3113 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
3116 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
3120 * Convert entry->ip from a virtual address to an offset in
3121 * its corresponding binary.
3124 addr = map__dso_map_ip(entry->ms.map, entry->ip);
3126 srcline = callchain_srcline(&entry->ms, addr);
3127 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
3128 false, NULL, 0, 0, 0, srcline);
3131 static int thread__resolve_callchain_unwind(struct thread *thread,
3132 struct callchain_cursor *cursor,
3133 struct evsel *evsel,
3134 struct perf_sample *sample,
3137 /* Can we do dwarf post unwind? */
3138 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
3139 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
3142 /* Bail out if nothing was captured. */
3143 if ((!sample->user_regs.regs) ||
3144 (!sample->user_stack.size))
3147 return unwind__get_entries(unwind_entry, cursor,
3148 thread, sample, max_stack, false);
3151 int thread__resolve_callchain(struct thread *thread,
3152 struct callchain_cursor *cursor,
3153 struct evsel *evsel,
3154 struct perf_sample *sample,
3155 struct symbol **parent,
3156 struct addr_location *root_al,
3161 callchain_cursor_reset(cursor);
3163 if (callchain_param.order == ORDER_CALLEE) {
3164 ret = thread__resolve_callchain_sample(thread, cursor,
3170 ret = thread__resolve_callchain_unwind(thread, cursor,
3174 ret = thread__resolve_callchain_unwind(thread, cursor,
3179 ret = thread__resolve_callchain_sample(thread, cursor,
3188 int machine__for_each_thread(struct machine *machine,
3189 int (*fn)(struct thread *thread, void *p),
3192 struct threads *threads;
3194 struct thread *thread;
3198 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3199 threads = &machine->threads[i];
3200 for (nd = rb_first_cached(&threads->entries); nd;
3202 thread = rb_entry(nd, struct thread, rb_node);
3203 rc = fn(thread, priv);
3208 list_for_each_entry(thread, &threads->dead, node) {
3209 rc = fn(thread, priv);
3217 int machines__for_each_thread(struct machines *machines,
3218 int (*fn)(struct thread *thread, void *p),
3224 rc = machine__for_each_thread(&machines->host, fn, priv);
3228 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
3229 struct machine *machine = rb_entry(nd, struct machine, rb_node);
3231 rc = machine__for_each_thread(machine, fn, priv);
3238 pid_t machine__get_current_tid(struct machine *machine, int cpu)
3240 if (cpu < 0 || (size_t)cpu >= machine->current_tid_sz)
3243 return machine->current_tid[cpu];
3246 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
3249 struct thread *thread;
3250 const pid_t init_val = -1;
3255 if (realloc_array_as_needed(machine->current_tid,
3256 machine->current_tid_sz,
3261 machine->current_tid[cpu] = tid;
3263 thread = machine__findnew_thread(machine, pid, tid);
3268 thread__put(thread);
3274 * Compares the raw arch string. N.B. see instead perf_env__arch() or
3275 * machine__normalized_is() if a normalized arch is needed.
3277 bool machine__is(struct machine *machine, const char *arch)
3279 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
3282 bool machine__normalized_is(struct machine *machine, const char *arch)
3284 return machine && !strcmp(perf_env__arch(machine->env), arch);
3287 int machine__nr_cpus_avail(struct machine *machine)
3289 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
3292 int machine__get_kernel_start(struct machine *machine)
3294 struct map *map = machine__kernel_map(machine);
3298 * The only addresses above 2^63 are kernel addresses of a 64-bit
3299 * kernel. Note that addresses are unsigned so that on a 32-bit system
3300 * all addresses including kernel addresses are less than 2^32. In
3301 * that case (32-bit system), if the kernel mapping is unknown, all
3302 * addresses will be assumed to be in user space - see
3303 * machine__kernel_ip().
3305 machine->kernel_start = 1ULL << 63;
3307 err = map__load(map);
3309 * On x86_64, PTI entry trampolines are less than the
3310 * start of kernel text, but still above 2^63. So leave
3311 * kernel_start = 1ULL << 63 for x86_64.
3313 if (!err && !machine__is(machine, "x86_64"))
3314 machine->kernel_start = map__start(map);
3319 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
3321 u8 addr_cpumode = cpumode;
3324 if (!machine->single_address_space)
3327 kernel_ip = machine__kernel_ip(machine, addr);
3329 case PERF_RECORD_MISC_KERNEL:
3330 case PERF_RECORD_MISC_USER:
3331 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
3332 PERF_RECORD_MISC_USER;
3334 case PERF_RECORD_MISC_GUEST_KERNEL:
3335 case PERF_RECORD_MISC_GUEST_USER:
3336 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
3337 PERF_RECORD_MISC_GUEST_USER;
3343 return addr_cpumode;
3346 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
3348 return dsos__findnew_id(&machine->dsos, filename, id);
3351 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
3353 return machine__findnew_dso_id(machine, filename, NULL);
3356 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
3358 struct machine *machine = vmachine;
3360 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
3365 *modp = __map__is_kmodule(map) ? (char *)map__dso(map)->short_name : NULL;
3366 *addrp = map__unmap_ip(map, sym->start);
3370 int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
3375 list_for_each_entry(pos, &machine->dsos.head, node) {
3376 if (fn(pos, machine, priv))
3382 int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn, void *priv)
3384 struct maps *maps = machine__kernel_maps(machine);
3385 struct map_rb_node *pos;
3388 maps__for_each_entry(maps, pos) {
3389 err = fn(pos->map, priv);
3397 bool machine__is_lock_function(struct machine *machine, u64 addr)
3399 if (!machine->sched.text_start) {
3401 struct symbol *sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_start", &kmap);
3404 /* to avoid retry */
3405 machine->sched.text_start = 1;
3409 machine->sched.text_start = map__unmap_ip(kmap, sym->start);
3411 /* should not fail from here */
3412 sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_end", &kmap);
3413 machine->sched.text_end = map__unmap_ip(kmap, sym->start);
3415 sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_start", &kmap);
3416 machine->lock.text_start = map__unmap_ip(kmap, sym->start);
3418 sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_end", &kmap);
3419 machine->lock.text_end = map__unmap_ip(kmap, sym->start);
3422 /* failed to get kernel symbols */
3423 if (machine->sched.text_start == 1)
3426 /* mutex and rwsem functions are in sched text section */
3427 if (machine->sched.text_start <= addr && addr < machine->sched.text_end)
3430 /* spinlock functions are in lock text section */
3431 if (machine->lock.text_start <= addr && addr < machine->lock.text_end)