1 // SPDX-License-Identifier: GPL-2.0
16 #include "map_symbol.h"
18 #include "mem-events.h"
28 #include <sys/types.h>
32 #include "linux/hash.h"
34 #include "bpf-event.h"
36 #include <linux/ctype.h>
37 #include <symbol/kallsyms.h>
38 #include <linux/mman.h>
39 #include <linux/string.h>
40 #include <linux/zalloc.h>
42 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
44 static void dsos__init(struct dsos *dsos)
46 INIT_LIST_HEAD(&dsos->head);
48 init_rwsem(&dsos->lock);
51 static void machine__threads_init(struct machine *machine)
55 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
56 struct threads *threads = &machine->threads[i];
57 threads->entries = RB_ROOT_CACHED;
58 init_rwsem(&threads->lock);
60 INIT_LIST_HEAD(&threads->dead);
61 threads->last_match = NULL;
65 static int machine__set_mmap_name(struct machine *machine)
67 if (machine__is_host(machine))
68 machine->mmap_name = strdup("[kernel.kallsyms]");
69 else if (machine__is_default_guest(machine))
70 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
71 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
73 machine->mmap_name = NULL;
75 return machine->mmap_name ? 0 : -ENOMEM;
78 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
82 memset(machine, 0, sizeof(*machine));
83 map_groups__init(&machine->kmaps, machine);
84 RB_CLEAR_NODE(&machine->rb_node);
85 dsos__init(&machine->dsos);
87 machine__threads_init(machine);
89 machine->vdso_info = NULL;
94 machine->id_hdr_size = 0;
95 machine->kptr_restrict_warned = false;
96 machine->comm_exec = false;
97 machine->kernel_start = 0;
98 machine->vmlinux_map = NULL;
100 machine->root_dir = strdup(root_dir);
101 if (machine->root_dir == NULL)
104 if (machine__set_mmap_name(machine))
107 if (pid != HOST_KERNEL_ID) {
108 struct thread *thread = machine__findnew_thread(machine, -1,
115 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
116 thread__set_comm(thread, comm, 0);
120 machine->current_tid = NULL;
125 zfree(&machine->root_dir);
126 zfree(&machine->mmap_name);
131 struct machine *machine__new_host(void)
133 struct machine *machine = malloc(sizeof(*machine));
135 if (machine != NULL) {
136 machine__init(machine, "", HOST_KERNEL_ID);
138 if (machine__create_kernel_maps(machine) < 0)
148 struct machine *machine__new_kallsyms(void)
150 struct machine *machine = machine__new_host();
153 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
154 * ask for not using the kcore parsing code, once this one is fixed
155 * to create a map per module.
157 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
158 machine__delete(machine);
165 static void dsos__purge(struct dsos *dsos)
169 down_write(&dsos->lock);
171 list_for_each_entry_safe(pos, n, &dsos->head, node) {
172 RB_CLEAR_NODE(&pos->rb_node);
174 list_del_init(&pos->node);
178 up_write(&dsos->lock);
181 static void dsos__exit(struct dsos *dsos)
184 exit_rwsem(&dsos->lock);
187 void machine__delete_threads(struct machine *machine)
192 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
193 struct threads *threads = &machine->threads[i];
194 down_write(&threads->lock);
195 nd = rb_first_cached(&threads->entries);
197 struct thread *t = rb_entry(nd, struct thread, rb_node);
200 __machine__remove_thread(machine, t, false);
202 up_write(&threads->lock);
206 void machine__exit(struct machine *machine)
213 machine__destroy_kernel_maps(machine);
214 map_groups__exit(&machine->kmaps);
215 dsos__exit(&machine->dsos);
216 machine__exit_vdso(machine);
217 zfree(&machine->root_dir);
218 zfree(&machine->mmap_name);
219 zfree(&machine->current_tid);
221 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
222 struct threads *threads = &machine->threads[i];
223 struct thread *thread, *n;
225 * Forget about the dead, at this point whatever threads were
226 * left in the dead lists better have a reference count taken
227 * by who is using them, and then, when they drop those references
228 * and it finally hits zero, thread__put() will check and see that
229 * its not in the dead threads list and will not try to remove it
230 * from there, just calling thread__delete() straight away.
232 list_for_each_entry_safe(thread, n, &threads->dead, node)
233 list_del_init(&thread->node);
235 exit_rwsem(&threads->lock);
239 void machine__delete(struct machine *machine)
242 machine__exit(machine);
247 void machines__init(struct machines *machines)
249 machine__init(&machines->host, "", HOST_KERNEL_ID);
250 machines->guests = RB_ROOT_CACHED;
253 void machines__exit(struct machines *machines)
255 machine__exit(&machines->host);
259 struct machine *machines__add(struct machines *machines, pid_t pid,
260 const char *root_dir)
262 struct rb_node **p = &machines->guests.rb_root.rb_node;
263 struct rb_node *parent = NULL;
264 struct machine *pos, *machine = malloc(sizeof(*machine));
265 bool leftmost = true;
270 if (machine__init(machine, root_dir, pid) != 0) {
277 pos = rb_entry(parent, struct machine, rb_node);
286 rb_link_node(&machine->rb_node, parent, p);
287 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
292 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
296 machines->host.comm_exec = comm_exec;
298 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
299 struct machine *machine = rb_entry(nd, struct machine, rb_node);
301 machine->comm_exec = comm_exec;
305 struct machine *machines__find(struct machines *machines, pid_t pid)
307 struct rb_node **p = &machines->guests.rb_root.rb_node;
308 struct rb_node *parent = NULL;
309 struct machine *machine;
310 struct machine *default_machine = NULL;
312 if (pid == HOST_KERNEL_ID)
313 return &machines->host;
317 machine = rb_entry(parent, struct machine, rb_node);
318 if (pid < machine->pid)
320 else if (pid > machine->pid)
325 default_machine = machine;
328 return default_machine;
331 struct machine *machines__findnew(struct machines *machines, pid_t pid)
334 const char *root_dir = "";
335 struct machine *machine = machines__find(machines, pid);
337 if (machine && (machine->pid == pid))
340 if ((pid != HOST_KERNEL_ID) &&
341 (pid != DEFAULT_GUEST_KERNEL_ID) &&
342 (symbol_conf.guestmount)) {
343 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
344 if (access(path, R_OK)) {
345 static struct strlist *seen;
348 seen = strlist__new(NULL, NULL);
350 if (!strlist__has_entry(seen, path)) {
351 pr_err("Can't access file %s\n", path);
352 strlist__add(seen, path);
360 machine = machines__add(machines, pid, root_dir);
365 void machines__process_guests(struct machines *machines,
366 machine__process_t process, void *data)
370 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
371 struct machine *pos = rb_entry(nd, struct machine, rb_node);
376 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
378 struct rb_node *node;
379 struct machine *machine;
381 machines->host.id_hdr_size = id_hdr_size;
383 for (node = rb_first_cached(&machines->guests); node;
384 node = rb_next(node)) {
385 machine = rb_entry(node, struct machine, rb_node);
386 machine->id_hdr_size = id_hdr_size;
392 static void machine__update_thread_pid(struct machine *machine,
393 struct thread *th, pid_t pid)
395 struct thread *leader;
397 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
402 if (th->pid_ == th->tid)
405 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
410 leader->mg = map_groups__new(machine);
415 if (th->mg == leader->mg)
420 * Maps are created from MMAP events which provide the pid and
421 * tid. Consequently there never should be any maps on a thread
422 * with an unknown pid. Just print an error if there are.
424 if (!map_groups__empty(th->mg))
425 pr_err("Discarding thread maps for %d:%d\n",
427 map_groups__put(th->mg);
430 th->mg = map_groups__get(leader->mg);
435 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
440 * Front-end cache - TID lookups come in blocks,
441 * so most of the time we dont have to look up
444 static struct thread*
445 __threads__get_last_match(struct threads *threads, struct machine *machine,
450 th = threads->last_match;
452 if (th->tid == tid) {
453 machine__update_thread_pid(machine, th, pid);
454 return thread__get(th);
457 threads->last_match = NULL;
463 static struct thread*
464 threads__get_last_match(struct threads *threads, struct machine *machine,
467 struct thread *th = NULL;
469 if (perf_singlethreaded)
470 th = __threads__get_last_match(threads, machine, pid, tid);
476 __threads__set_last_match(struct threads *threads, struct thread *th)
478 threads->last_match = th;
482 threads__set_last_match(struct threads *threads, struct thread *th)
484 if (perf_singlethreaded)
485 __threads__set_last_match(threads, th);
489 * Caller must eventually drop thread->refcnt returned with a successful
490 * lookup/new thread inserted.
492 static struct thread *____machine__findnew_thread(struct machine *machine,
493 struct threads *threads,
494 pid_t pid, pid_t tid,
497 struct rb_node **p = &threads->entries.rb_root.rb_node;
498 struct rb_node *parent = NULL;
500 bool leftmost = true;
502 th = threads__get_last_match(threads, machine, pid, tid);
508 th = rb_entry(parent, struct thread, rb_node);
510 if (th->tid == tid) {
511 threads__set_last_match(threads, th);
512 machine__update_thread_pid(machine, th, pid);
513 return thread__get(th);
527 th = thread__new(pid, tid);
529 rb_link_node(&th->rb_node, parent, p);
530 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
533 * We have to initialize map_groups separately
534 * after rb tree is updated.
536 * The reason is that we call machine__findnew_thread
537 * within thread__init_map_groups to find the thread
538 * leader and that would screwed the rb tree.
540 if (thread__init_map_groups(th, machine)) {
541 rb_erase_cached(&th->rb_node, &threads->entries);
542 RB_CLEAR_NODE(&th->rb_node);
547 * It is now in the rbtree, get a ref
550 threads__set_last_match(threads, th);
557 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
559 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
562 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
565 struct threads *threads = machine__threads(machine, tid);
568 down_write(&threads->lock);
569 th = __machine__findnew_thread(machine, pid, tid);
570 up_write(&threads->lock);
574 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
577 struct threads *threads = machine__threads(machine, tid);
580 down_read(&threads->lock);
581 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
582 up_read(&threads->lock);
586 struct comm *machine__thread_exec_comm(struct machine *machine,
587 struct thread *thread)
589 if (machine->comm_exec)
590 return thread__exec_comm(thread);
592 return thread__comm(thread);
595 int machine__process_comm_event(struct machine *machine, union perf_event *event,
596 struct perf_sample *sample)
598 struct thread *thread = machine__findnew_thread(machine,
601 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
605 machine->comm_exec = true;
608 perf_event__fprintf_comm(event, stdout);
610 if (thread == NULL ||
611 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
612 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
621 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
622 union perf_event *event,
623 struct perf_sample *sample __maybe_unused)
625 struct thread *thread = machine__findnew_thread(machine,
626 event->namespaces.pid,
627 event->namespaces.tid);
630 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
631 "\nWARNING: kernel seems to support more namespaces than perf"
632 " tool.\nTry updating the perf tool..\n\n");
634 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
635 "\nWARNING: perf tool seems to support more namespaces than"
636 " the kernel.\nTry updating the kernel..\n\n");
639 perf_event__fprintf_namespaces(event, stdout);
641 if (thread == NULL ||
642 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
643 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
652 int machine__process_lost_event(struct machine *machine __maybe_unused,
653 union perf_event *event, struct perf_sample *sample __maybe_unused)
655 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
656 event->lost.id, event->lost.lost);
660 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
661 union perf_event *event, struct perf_sample *sample)
663 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
664 sample->id, event->lost_samples.lost);
668 static struct dso *machine__findnew_module_dso(struct machine *machine,
670 const char *filename)
674 down_write(&machine->dsos.lock);
676 dso = __dsos__find(&machine->dsos, m->name, true);
678 dso = __dsos__addnew(&machine->dsos, m->name);
682 dso__set_module_info(dso, m, machine);
683 dso__set_long_name(dso, strdup(filename), true);
688 up_write(&machine->dsos.lock);
692 int machine__process_aux_event(struct machine *machine __maybe_unused,
693 union perf_event *event)
696 perf_event__fprintf_aux(event, stdout);
700 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
701 union perf_event *event)
704 perf_event__fprintf_itrace_start(event, stdout);
708 int machine__process_switch_event(struct machine *machine __maybe_unused,
709 union perf_event *event)
712 perf_event__fprintf_switch(event, stdout);
716 static int machine__process_ksymbol_register(struct machine *machine,
717 union perf_event *event,
718 struct perf_sample *sample __maybe_unused)
723 map = map_groups__find(&machine->kmaps, event->ksymbol.addr);
725 map = dso__new_map(event->ksymbol.name);
729 map->start = event->ksymbol.addr;
730 map->end = map->start + event->ksymbol.len;
731 map_groups__insert(&machine->kmaps, map);
734 sym = symbol__new(map->map_ip(map, map->start),
736 0, 0, event->ksymbol.name);
739 dso__insert_symbol(map->dso, sym);
743 static int machine__process_ksymbol_unregister(struct machine *machine,
744 union perf_event *event,
745 struct perf_sample *sample __maybe_unused)
749 map = map_groups__find(&machine->kmaps, event->ksymbol.addr);
751 map_groups__remove(&machine->kmaps, map);
756 int machine__process_ksymbol(struct machine *machine __maybe_unused,
757 union perf_event *event,
758 struct perf_sample *sample)
761 perf_event__fprintf_ksymbol(event, stdout);
763 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
764 return machine__process_ksymbol_unregister(machine, event,
766 return machine__process_ksymbol_register(machine, event, sample);
769 static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
771 const char *dup_filename;
773 if (!filename || !dso || !dso->long_name)
775 if (dso->long_name[0] != '[')
777 if (!strchr(filename, '/'))
780 dup_filename = strdup(filename);
784 dso__set_long_name(dso, dup_filename, true);
787 struct map *machine__findnew_module_map(struct machine *machine, u64 start,
788 const char *filename)
790 struct map *map = NULL;
791 struct dso *dso = NULL;
794 if (kmod_path__parse_name(&m, filename))
797 map = map_groups__find_by_name(&machine->kmaps, m.name);
800 * If the map's dso is an offline module, give dso__load()
801 * a chance to find the file path of that module by fixing
804 dso__adjust_kmod_long_name(map->dso, filename);
808 dso = machine__findnew_module_dso(machine, &m, filename);
812 map = map__new2(start, dso);
816 map_groups__insert(&machine->kmaps, map);
818 /* Put the map here because map_groups__insert alread got it */
821 /* put the dso here, corresponding to machine__findnew_module_dso */
827 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
830 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
832 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
833 struct machine *pos = rb_entry(nd, struct machine, rb_node);
834 ret += __dsos__fprintf(&pos->dsos.head, fp);
840 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
841 bool (skip)(struct dso *dso, int parm), int parm)
843 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
846 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
847 bool (skip)(struct dso *dso, int parm), int parm)
850 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
852 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
853 struct machine *pos = rb_entry(nd, struct machine, rb_node);
854 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
859 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
863 struct dso *kdso = machine__kernel_map(machine)->dso;
865 if (kdso->has_build_id) {
866 char filename[PATH_MAX];
867 if (dso__build_id_filename(kdso, filename, sizeof(filename),
869 printed += fprintf(fp, "[0] %s\n", filename);
872 for (i = 0; i < vmlinux_path__nr_entries; ++i)
873 printed += fprintf(fp, "[%d] %s\n",
874 i + kdso->has_build_id, vmlinux_path[i]);
879 size_t machine__fprintf(struct machine *machine, FILE *fp)
885 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
886 struct threads *threads = &machine->threads[i];
888 down_read(&threads->lock);
890 ret = fprintf(fp, "Threads: %u\n", threads->nr);
892 for (nd = rb_first_cached(&threads->entries); nd;
894 struct thread *pos = rb_entry(nd, struct thread, rb_node);
896 ret += thread__fprintf(pos, fp);
899 up_read(&threads->lock);
904 static struct dso *machine__get_kernel(struct machine *machine)
906 const char *vmlinux_name = machine->mmap_name;
909 if (machine__is_host(machine)) {
910 if (symbol_conf.vmlinux_name)
911 vmlinux_name = symbol_conf.vmlinux_name;
913 kernel = machine__findnew_kernel(machine, vmlinux_name,
914 "[kernel]", DSO_TYPE_KERNEL);
916 if (symbol_conf.default_guest_vmlinux_name)
917 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
919 kernel = machine__findnew_kernel(machine, vmlinux_name,
921 DSO_TYPE_GUEST_KERNEL);
924 if (kernel != NULL && (!kernel->has_build_id))
925 dso__read_running_kernel_build_id(kernel, machine);
930 struct process_args {
934 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
937 if (machine__is_default_guest(machine))
938 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
940 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
943 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
945 /* Figure out the start address of kernel map from /proc/kallsyms.
946 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
947 * symbol_name if it's not that important.
949 static int machine__get_running_kernel_start(struct machine *machine,
950 const char **symbol_name,
951 u64 *start, u64 *end)
953 char filename[PATH_MAX];
958 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
960 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
963 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
964 err = kallsyms__get_function_start(filename, name, &addr);
977 err = kallsyms__get_function_start(filename, "_etext", &addr);
984 int machine__create_extra_kernel_map(struct machine *machine,
986 struct extra_kernel_map *xm)
991 map = map__new2(xm->start, kernel);
996 map->pgoff = xm->pgoff;
998 kmap = map__kmap(map);
1000 kmap->kmaps = &machine->kmaps;
1001 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
1003 map_groups__insert(&machine->kmaps, map);
1005 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1006 kmap->name, map->start, map->end);
1013 static u64 find_entry_trampoline(struct dso *dso)
1015 /* Duplicates are removed so lookup all aliases */
1016 const char *syms[] = {
1017 "_entry_trampoline",
1018 "__entry_trampoline_start",
1019 "entry_SYSCALL_64_trampoline",
1021 struct symbol *sym = dso__first_symbol(dso);
1024 for (; sym; sym = dso__next_symbol(sym)) {
1025 if (sym->binding != STB_GLOBAL)
1027 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1028 if (!strcmp(sym->name, syms[i]))
1037 * These values can be used for kernels that do not have symbols for the entry
1038 * trampolines in kallsyms.
1040 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1041 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1042 #define X86_64_ENTRY_TRAMPOLINE 0x6000
1044 /* Map x86_64 PTI entry trampolines */
1045 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1048 struct map_groups *kmaps = &machine->kmaps;
1049 struct maps *maps = &kmaps->maps;
1050 int nr_cpus_avail, cpu;
1056 * In the vmlinux case, pgoff is a virtual address which must now be
1057 * mapped to a vmlinux offset.
1059 for (map = maps__first(maps); map; map = map__next(map)) {
1060 struct kmap *kmap = __map__kmap(map);
1061 struct map *dest_map;
1063 if (!kmap || !is_entry_trampoline(kmap->name))
1066 dest_map = map_groups__find(kmaps, map->pgoff);
1067 if (dest_map != map)
1068 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1071 if (found || machine->trampolines_mapped)
1074 pgoff = find_entry_trampoline(kernel);
1078 nr_cpus_avail = machine__nr_cpus_avail(machine);
1080 /* Add a 1 page map for each CPU's entry trampoline */
1081 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1082 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1083 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1084 X86_64_ENTRY_TRAMPOLINE;
1085 struct extra_kernel_map xm = {
1087 .end = va + page_size,
1091 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1093 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1097 machine->trampolines_mapped = nr_cpus_avail;
1102 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1103 struct dso *kernel __maybe_unused)
1109 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1114 /* In case of renewal the kernel map, destroy previous one */
1115 machine__destroy_kernel_maps(machine);
1117 machine->vmlinux_map = map__new2(0, kernel);
1118 if (machine->vmlinux_map == NULL)
1121 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1122 map = machine__kernel_map(machine);
1123 kmap = map__kmap(map);
1127 kmap->kmaps = &machine->kmaps;
1128 map_groups__insert(&machine->kmaps, map);
1133 void machine__destroy_kernel_maps(struct machine *machine)
1136 struct map *map = machine__kernel_map(machine);
1141 kmap = map__kmap(map);
1142 map_groups__remove(&machine->kmaps, map);
1143 if (kmap && kmap->ref_reloc_sym) {
1144 zfree((char **)&kmap->ref_reloc_sym->name);
1145 zfree(&kmap->ref_reloc_sym);
1148 map__zput(machine->vmlinux_map);
1151 int machines__create_guest_kernel_maps(struct machines *machines)
1154 struct dirent **namelist = NULL;
1156 char path[PATH_MAX];
1160 if (symbol_conf.default_guest_vmlinux_name ||
1161 symbol_conf.default_guest_modules ||
1162 symbol_conf.default_guest_kallsyms) {
1163 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1166 if (symbol_conf.guestmount) {
1167 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1170 for (i = 0; i < items; i++) {
1171 if (!isdigit(namelist[i]->d_name[0])) {
1172 /* Filter out . and .. */
1175 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1176 if ((*endp != '\0') ||
1177 (endp == namelist[i]->d_name) ||
1178 (errno == ERANGE)) {
1179 pr_debug("invalid directory (%s). Skipping.\n",
1180 namelist[i]->d_name);
1183 sprintf(path, "%s/%s/proc/kallsyms",
1184 symbol_conf.guestmount,
1185 namelist[i]->d_name);
1186 ret = access(path, R_OK);
1188 pr_debug("Can't access file %s\n", path);
1191 machines__create_kernel_maps(machines, pid);
1200 void machines__destroy_kernel_maps(struct machines *machines)
1202 struct rb_node *next = rb_first_cached(&machines->guests);
1204 machine__destroy_kernel_maps(&machines->host);
1207 struct machine *pos = rb_entry(next, struct machine, rb_node);
1209 next = rb_next(&pos->rb_node);
1210 rb_erase_cached(&pos->rb_node, &machines->guests);
1211 machine__delete(pos);
1215 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1217 struct machine *machine = machines__findnew(machines, pid);
1219 if (machine == NULL)
1222 return machine__create_kernel_maps(machine);
1225 int machine__load_kallsyms(struct machine *machine, const char *filename)
1227 struct map *map = machine__kernel_map(machine);
1228 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
1231 dso__set_loaded(map->dso);
1233 * Since /proc/kallsyms will have multiple sessions for the
1234 * kernel, with modules between them, fixup the end of all
1237 map_groups__fixup_end(&machine->kmaps);
1243 int machine__load_vmlinux_path(struct machine *machine)
1245 struct map *map = machine__kernel_map(machine);
1246 int ret = dso__load_vmlinux_path(map->dso, map);
1249 dso__set_loaded(map->dso);
1254 static char *get_kernel_version(const char *root_dir)
1256 char version[PATH_MAX];
1259 const char *prefix = "Linux version ";
1261 sprintf(version, "%s/proc/version", root_dir);
1262 file = fopen(version, "r");
1266 tmp = fgets(version, sizeof(version), file);
1271 name = strstr(version, prefix);
1274 name += strlen(prefix);
1275 tmp = strchr(name, ' ');
1279 return strdup(name);
1282 static bool is_kmod_dso(struct dso *dso)
1284 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1285 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1288 static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1289 struct kmod_path *m)
1292 struct map *map = map_groups__find_by_name(mg, m->name);
1297 long_name = strdup(path);
1298 if (long_name == NULL)
1301 dso__set_long_name(map->dso, long_name, true);
1302 dso__kernel_module_get_build_id(map->dso, "");
1305 * Full name could reveal us kmod compression, so
1306 * we need to update the symtab_type if needed.
1308 if (m->comp && is_kmod_dso(map->dso)) {
1309 map->dso->symtab_type++;
1310 map->dso->comp = m->comp;
1316 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1317 const char *dir_name, int depth)
1319 struct dirent *dent;
1320 DIR *dir = opendir(dir_name);
1324 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1328 while ((dent = readdir(dir)) != NULL) {
1329 char path[PATH_MAX];
1332 /*sshfs might return bad dent->d_type, so we have to stat*/
1333 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1334 if (stat(path, &st))
1337 if (S_ISDIR(st.st_mode)) {
1338 if (!strcmp(dent->d_name, ".") ||
1339 !strcmp(dent->d_name, ".."))
1342 /* Do not follow top-level source and build symlinks */
1344 if (!strcmp(dent->d_name, "source") ||
1345 !strcmp(dent->d_name, "build"))
1349 ret = map_groups__set_modules_path_dir(mg, path,
1356 ret = kmod_path__parse_name(&m, dent->d_name);
1361 ret = map_groups__set_module_path(mg, path, &m);
1375 static int machine__set_modules_path(struct machine *machine)
1378 char modules_path[PATH_MAX];
1380 version = get_kernel_version(machine->root_dir);
1384 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1385 machine->root_dir, version);
1388 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
1390 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1391 u64 *size __maybe_unused,
1392 const char *name __maybe_unused)
1397 static int machine__create_module(void *arg, const char *name, u64 start,
1400 struct machine *machine = arg;
1403 if (arch__fix_module_text_start(&start, &size, name) < 0)
1406 map = machine__findnew_module_map(machine, start, name);
1409 map->end = start + size;
1411 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1416 static int machine__create_modules(struct machine *machine)
1418 const char *modules;
1419 char path[PATH_MAX];
1421 if (machine__is_default_guest(machine)) {
1422 modules = symbol_conf.default_guest_modules;
1424 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1428 if (symbol__restricted_filename(modules, "/proc/modules"))
1431 if (modules__parse(modules, machine, machine__create_module))
1434 if (!machine__set_modules_path(machine))
1437 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1442 static void machine__set_kernel_mmap(struct machine *machine,
1445 machine->vmlinux_map->start = start;
1446 machine->vmlinux_map->end = end;
1448 * Be a bit paranoid here, some perf.data file came with
1449 * a zero sized synthesized MMAP event for the kernel.
1451 if (start == 0 && end == 0)
1452 machine->vmlinux_map->end = ~0ULL;
1455 static void machine__update_kernel_mmap(struct machine *machine,
1458 struct map *map = machine__kernel_map(machine);
1461 map_groups__remove(&machine->kmaps, map);
1463 machine__set_kernel_mmap(machine, start, end);
1465 map_groups__insert(&machine->kmaps, map);
1469 int machine__create_kernel_maps(struct machine *machine)
1471 struct dso *kernel = machine__get_kernel(machine);
1472 const char *name = NULL;
1474 u64 start = 0, end = ~0ULL;
1480 ret = __machine__create_kernel_maps(machine, kernel);
1484 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1485 if (machine__is_host(machine))
1486 pr_debug("Problems creating module maps, "
1487 "continuing anyway...\n");
1489 pr_debug("Problems creating module maps for guest %d, "
1490 "continuing anyway...\n", machine->pid);
1493 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1495 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1496 machine__destroy_kernel_maps(machine);
1502 * we have a real start address now, so re-order the kmaps
1503 * assume it's the last in the kmaps
1505 machine__update_kernel_mmap(machine, start, end);
1508 if (machine__create_extra_kernel_maps(machine, kernel))
1509 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1512 /* update end address of the kernel map using adjacent module address */
1513 map = map__next(machine__kernel_map(machine));
1515 machine__set_kernel_mmap(machine, start, map->start);
1523 static bool machine__uses_kcore(struct machine *machine)
1527 list_for_each_entry(dso, &machine->dsos.head, node) {
1528 if (dso__is_kcore(dso))
1535 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1536 union perf_event *event)
1538 return machine__is(machine, "x86_64") &&
1539 is_entry_trampoline(event->mmap.filename);
1542 static int machine__process_extra_kernel_map(struct machine *machine,
1543 union perf_event *event)
1545 struct map *kernel_map = machine__kernel_map(machine);
1546 struct dso *kernel = kernel_map ? kernel_map->dso : NULL;
1547 struct extra_kernel_map xm = {
1548 .start = event->mmap.start,
1549 .end = event->mmap.start + event->mmap.len,
1550 .pgoff = event->mmap.pgoff,
1556 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1558 return machine__create_extra_kernel_map(machine, kernel, &xm);
1561 static int machine__process_kernel_mmap_event(struct machine *machine,
1562 union perf_event *event)
1565 enum dso_kernel_type kernel_type;
1566 bool is_kernel_mmap;
1568 /* If we have maps from kcore then we do not need or want any others */
1569 if (machine__uses_kcore(machine))
1572 if (machine__is_host(machine))
1573 kernel_type = DSO_TYPE_KERNEL;
1575 kernel_type = DSO_TYPE_GUEST_KERNEL;
1577 is_kernel_mmap = memcmp(event->mmap.filename,
1579 strlen(machine->mmap_name) - 1) == 0;
1580 if (event->mmap.filename[0] == '/' ||
1581 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1582 map = machine__findnew_module_map(machine, event->mmap.start,
1583 event->mmap.filename);
1587 map->end = map->start + event->mmap.len;
1588 } else if (is_kernel_mmap) {
1589 const char *symbol_name = (event->mmap.filename +
1590 strlen(machine->mmap_name));
1592 * Should be there already, from the build-id table in
1595 struct dso *kernel = NULL;
1598 down_read(&machine->dsos.lock);
1600 list_for_each_entry(dso, &machine->dsos.head, node) {
1603 * The cpumode passed to is_kernel_module is not the
1604 * cpumode of *this* event. If we insist on passing
1605 * correct cpumode to is_kernel_module, we should
1606 * record the cpumode when we adding this dso to the
1609 * However we don't really need passing correct
1610 * cpumode. We know the correct cpumode must be kernel
1611 * mode (if not, we should not link it onto kernel_dsos
1614 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1615 * is_kernel_module() treats it as a kernel cpumode.
1619 is_kernel_module(dso->long_name,
1620 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1628 up_read(&machine->dsos.lock);
1631 kernel = machine__findnew_dso(machine, machine->mmap_name);
1635 kernel->kernel = kernel_type;
1636 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1641 if (strstr(kernel->long_name, "vmlinux"))
1642 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1644 machine__update_kernel_mmap(machine, event->mmap.start,
1645 event->mmap.start + event->mmap.len);
1648 * Avoid using a zero address (kptr_restrict) for the ref reloc
1649 * symbol. Effectively having zero here means that at record
1650 * time /proc/sys/kernel/kptr_restrict was non zero.
1652 if (event->mmap.pgoff != 0) {
1653 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1658 if (machine__is_default_guest(machine)) {
1660 * preload dso of guest kernel and modules
1662 dso__load(kernel, machine__kernel_map(machine));
1664 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1665 return machine__process_extra_kernel_map(machine, event);
1672 int machine__process_mmap2_event(struct machine *machine,
1673 union perf_event *event,
1674 struct perf_sample *sample)
1676 struct thread *thread;
1681 perf_event__fprintf_mmap2(event, stdout);
1683 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1684 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1685 ret = machine__process_kernel_mmap_event(machine, event);
1691 thread = machine__findnew_thread(machine, event->mmap2.pid,
1696 map = map__new(machine, event->mmap2.start,
1697 event->mmap2.len, event->mmap2.pgoff,
1699 event->mmap2.min, event->mmap2.ino,
1700 event->mmap2.ino_generation,
1703 event->mmap2.filename, thread);
1706 goto out_problem_map;
1708 ret = thread__insert_map(thread, map);
1710 goto out_problem_insert;
1712 thread__put(thread);
1719 thread__put(thread);
1721 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1725 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1726 struct perf_sample *sample)
1728 struct thread *thread;
1734 perf_event__fprintf_mmap(event, stdout);
1736 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1737 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1738 ret = machine__process_kernel_mmap_event(machine, event);
1744 thread = machine__findnew_thread(machine, event->mmap.pid,
1749 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1752 map = map__new(machine, event->mmap.start,
1753 event->mmap.len, event->mmap.pgoff,
1754 0, 0, 0, 0, prot, 0,
1755 event->mmap.filename,
1759 goto out_problem_map;
1761 ret = thread__insert_map(thread, map);
1763 goto out_problem_insert;
1765 thread__put(thread);
1772 thread__put(thread);
1774 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1778 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1780 struct threads *threads = machine__threads(machine, th->tid);
1782 if (threads->last_match == th)
1783 threads__set_last_match(threads, NULL);
1786 down_write(&threads->lock);
1788 BUG_ON(refcount_read(&th->refcnt) == 0);
1790 rb_erase_cached(&th->rb_node, &threads->entries);
1791 RB_CLEAR_NODE(&th->rb_node);
1794 * Move it first to the dead_threads list, then drop the reference,
1795 * if this is the last reference, then the thread__delete destructor
1796 * will be called and we will remove it from the dead_threads list.
1798 list_add_tail(&th->node, &threads->dead);
1801 * We need to do the put here because if this is the last refcount,
1802 * then we will be touching the threads->dead head when removing the
1808 up_write(&threads->lock);
1811 void machine__remove_thread(struct machine *machine, struct thread *th)
1813 return __machine__remove_thread(machine, th, true);
1816 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1817 struct perf_sample *sample)
1819 struct thread *thread = machine__find_thread(machine,
1822 struct thread *parent = machine__findnew_thread(machine,
1825 bool do_maps_clone = true;
1829 perf_event__fprintf_task(event, stdout);
1832 * There may be an existing thread that is not actually the parent,
1833 * either because we are processing events out of order, or because the
1834 * (fork) event that would have removed the thread was lost. Assume the
1835 * latter case and continue on as best we can.
1837 if (parent->pid_ != (pid_t)event->fork.ppid) {
1838 dump_printf("removing erroneous parent thread %d/%d\n",
1839 parent->pid_, parent->tid);
1840 machine__remove_thread(machine, parent);
1841 thread__put(parent);
1842 parent = machine__findnew_thread(machine, event->fork.ppid,
1846 /* if a thread currently exists for the thread id remove it */
1847 if (thread != NULL) {
1848 machine__remove_thread(machine, thread);
1849 thread__put(thread);
1852 thread = machine__findnew_thread(machine, event->fork.pid,
1855 * When synthesizing FORK events, we are trying to create thread
1856 * objects for the already running tasks on the machine.
1858 * Normally, for a kernel FORK event, we want to clone the parent's
1859 * maps because that is what the kernel just did.
1861 * But when synthesizing, this should not be done. If we do, we end up
1862 * with overlapping maps as we process the sythesized MMAP2 events that
1863 * get delivered shortly thereafter.
1865 * Use the FORK event misc flags in an internal way to signal this
1866 * situation, so we can elide the map clone when appropriate.
1868 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1869 do_maps_clone = false;
1871 if (thread == NULL || parent == NULL ||
1872 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
1873 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1876 thread__put(thread);
1877 thread__put(parent);
1882 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1883 struct perf_sample *sample __maybe_unused)
1885 struct thread *thread = machine__find_thread(machine,
1890 perf_event__fprintf_task(event, stdout);
1892 if (thread != NULL) {
1893 thread__exited(thread);
1894 thread__put(thread);
1900 int machine__process_event(struct machine *machine, union perf_event *event,
1901 struct perf_sample *sample)
1905 switch (event->header.type) {
1906 case PERF_RECORD_COMM:
1907 ret = machine__process_comm_event(machine, event, sample); break;
1908 case PERF_RECORD_MMAP:
1909 ret = machine__process_mmap_event(machine, event, sample); break;
1910 case PERF_RECORD_NAMESPACES:
1911 ret = machine__process_namespaces_event(machine, event, sample); break;
1912 case PERF_RECORD_MMAP2:
1913 ret = machine__process_mmap2_event(machine, event, sample); break;
1914 case PERF_RECORD_FORK:
1915 ret = machine__process_fork_event(machine, event, sample); break;
1916 case PERF_RECORD_EXIT:
1917 ret = machine__process_exit_event(machine, event, sample); break;
1918 case PERF_RECORD_LOST:
1919 ret = machine__process_lost_event(machine, event, sample); break;
1920 case PERF_RECORD_AUX:
1921 ret = machine__process_aux_event(machine, event); break;
1922 case PERF_RECORD_ITRACE_START:
1923 ret = machine__process_itrace_start_event(machine, event); break;
1924 case PERF_RECORD_LOST_SAMPLES:
1925 ret = machine__process_lost_samples_event(machine, event, sample); break;
1926 case PERF_RECORD_SWITCH:
1927 case PERF_RECORD_SWITCH_CPU_WIDE:
1928 ret = machine__process_switch_event(machine, event); break;
1929 case PERF_RECORD_KSYMBOL:
1930 ret = machine__process_ksymbol(machine, event, sample); break;
1931 case PERF_RECORD_BPF_EVENT:
1932 ret = machine__process_bpf(machine, event, sample); break;
1941 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1943 if (!regexec(regex, sym->name, 0, NULL, 0))
1948 static void ip__resolve_ams(struct thread *thread,
1949 struct addr_map_symbol *ams,
1952 struct addr_location al;
1954 memset(&al, 0, sizeof(al));
1956 * We cannot use the header.misc hint to determine whether a
1957 * branch stack address is user, kernel, guest, hypervisor.
1958 * Branches may straddle the kernel/user/hypervisor boundaries.
1959 * Thus, we have to try consecutively until we find a match
1960 * or else, the symbol is unknown
1962 thread__find_cpumode_addr_location(thread, ip, &al);
1965 ams->al_addr = al.addr;
1971 static void ip__resolve_data(struct thread *thread,
1972 u8 m, struct addr_map_symbol *ams,
1973 u64 addr, u64 phys_addr)
1975 struct addr_location al;
1977 memset(&al, 0, sizeof(al));
1979 thread__find_symbol(thread, m, addr, &al);
1982 ams->al_addr = al.addr;
1985 ams->phys_addr = phys_addr;
1988 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1989 struct addr_location *al)
1991 struct mem_info *mi = mem_info__new();
1996 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1997 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1998 sample->addr, sample->phys_addr);
1999 mi->data_src.val = sample->data_src;
2004 static char *callchain_srcline(struct map *map, struct symbol *sym, u64 ip)
2006 char *srcline = NULL;
2008 if (!map || callchain_param.key == CCKEY_FUNCTION)
2011 srcline = srcline__tree_find(&map->dso->srclines, ip);
2013 bool show_sym = false;
2014 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2016 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
2017 sym, show_sym, show_addr, ip);
2018 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2029 static int add_callchain_ip(struct thread *thread,
2030 struct callchain_cursor *cursor,
2031 struct symbol **parent,
2032 struct addr_location *root_al,
2036 struct branch_flags *flags,
2037 struct iterations *iter,
2040 struct addr_location al;
2041 int nr_loop_iter = 0;
2042 u64 iter_cycles = 0;
2043 const char *srcline = NULL;
2048 thread__find_cpumode_addr_location(thread, ip, &al);
2050 if (ip >= PERF_CONTEXT_MAX) {
2052 case PERF_CONTEXT_HV:
2053 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2055 case PERF_CONTEXT_KERNEL:
2056 *cpumode = PERF_RECORD_MISC_KERNEL;
2058 case PERF_CONTEXT_USER:
2059 *cpumode = PERF_RECORD_MISC_USER;
2062 pr_debug("invalid callchain context: "
2063 "%"PRId64"\n", (s64) ip);
2065 * It seems the callchain is corrupted.
2068 callchain_cursor_reset(cursor);
2073 thread__find_symbol(thread, *cpumode, ip, &al);
2076 if (al.sym != NULL) {
2077 if (perf_hpp_list.parent && !*parent &&
2078 symbol__match_regex(al.sym, &parent_regex))
2080 else if (have_ignore_callees && root_al &&
2081 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2082 /* Treat this symbol as the root,
2083 forgetting its callees. */
2085 callchain_cursor_reset(cursor);
2089 if (symbol_conf.hide_unresolved && al.sym == NULL)
2093 nr_loop_iter = iter->nr_loop_iter;
2094 iter_cycles = iter->cycles;
2097 srcline = callchain_srcline(al.map, al.sym, al.addr);
2098 return callchain_cursor_append(cursor, ip, al.map, al.sym,
2099 branch, flags, nr_loop_iter,
2100 iter_cycles, branch_from, srcline);
2103 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2104 struct addr_location *al)
2107 const struct branch_stack *bs = sample->branch_stack;
2108 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2113 for (i = 0; i < bs->nr; i++) {
2114 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
2115 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
2116 bi[i].flags = bs->entries[i].flags;
2121 static void save_iterations(struct iterations *iter,
2122 struct branch_entry *be, int nr)
2126 iter->nr_loop_iter++;
2129 for (i = 0; i < nr; i++)
2130 iter->cycles += be[i].flags.cycles;
2135 #define NO_ENTRY 0xff
2137 #define PERF_MAX_BRANCH_DEPTH 127
2140 static int remove_loops(struct branch_entry *l, int nr,
2141 struct iterations *iter)
2144 unsigned char chash[CHASHSZ];
2146 memset(chash, NO_ENTRY, sizeof(chash));
2148 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2150 for (i = 0; i < nr; i++) {
2151 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2153 /* no collision handling for now */
2154 if (chash[h] == NO_ENTRY) {
2156 } else if (l[chash[h]].from == l[i].from) {
2157 bool is_loop = true;
2158 /* check if it is a real loop */
2160 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2161 if (l[j].from != l[i + off].from) {
2168 save_iterations(iter + i + off,
2171 memmove(iter + i, iter + i + off,
2174 memmove(l + i, l + i + off,
2186 * Recolve LBR callstack chain sample
2188 * 1 on success get LBR callchain information
2189 * 0 no available LBR callchain information, should try fp
2190 * negative error code on other errors.
2192 static int resolve_lbr_callchain_sample(struct thread *thread,
2193 struct callchain_cursor *cursor,
2194 struct perf_sample *sample,
2195 struct symbol **parent,
2196 struct addr_location *root_al,
2199 struct ip_callchain *chain = sample->callchain;
2200 int chain_nr = min(max_stack, (int)chain->nr), i;
2201 u8 cpumode = PERF_RECORD_MISC_USER;
2202 u64 ip, branch_from = 0;
2204 for (i = 0; i < chain_nr; i++) {
2205 if (chain->ips[i] == PERF_CONTEXT_USER)
2209 /* LBR only affects the user callchain */
2210 if (i != chain_nr) {
2211 struct branch_stack *lbr_stack = sample->branch_stack;
2212 int lbr_nr = lbr_stack->nr, j, k;
2214 struct branch_flags *flags;
2216 * LBR callstack can only get user call chain.
2217 * The mix_chain_nr is kernel call chain
2218 * number plus LBR user call chain number.
2219 * i is kernel call chain number,
2220 * 1 is PERF_CONTEXT_USER,
2221 * lbr_nr + 1 is the user call chain number.
2222 * For details, please refer to the comments
2223 * in callchain__printf
2225 int mix_chain_nr = i + 1 + lbr_nr + 1;
2227 for (j = 0; j < mix_chain_nr; j++) {
2232 if (callchain_param.order == ORDER_CALLEE) {
2235 else if (j > i + 1) {
2237 ip = lbr_stack->entries[k].from;
2239 flags = &lbr_stack->entries[k].flags;
2241 ip = lbr_stack->entries[0].to;
2243 flags = &lbr_stack->entries[0].flags;
2245 lbr_stack->entries[0].from;
2250 ip = lbr_stack->entries[k].from;
2252 flags = &lbr_stack->entries[k].flags;
2254 else if (j > lbr_nr)
2255 ip = chain->ips[i + 1 - (j - lbr_nr)];
2257 ip = lbr_stack->entries[0].to;
2259 flags = &lbr_stack->entries[0].flags;
2261 lbr_stack->entries[0].from;
2265 err = add_callchain_ip(thread, cursor, parent,
2266 root_al, &cpumode, ip,
2267 branch, flags, NULL,
2270 return (err < 0) ? err : 0;
2278 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2279 struct callchain_cursor *cursor,
2280 struct symbol **parent,
2281 struct addr_location *root_al,
2282 u8 *cpumode, int ent)
2286 while (--ent >= 0) {
2287 u64 ip = chain->ips[ent];
2289 if (ip >= PERF_CONTEXT_MAX) {
2290 err = add_callchain_ip(thread, cursor, parent,
2291 root_al, cpumode, ip,
2292 false, NULL, NULL, 0);
2299 static int thread__resolve_callchain_sample(struct thread *thread,
2300 struct callchain_cursor *cursor,
2301 struct evsel *evsel,
2302 struct perf_sample *sample,
2303 struct symbol **parent,
2304 struct addr_location *root_al,
2307 struct branch_stack *branch = sample->branch_stack;
2308 struct ip_callchain *chain = sample->callchain;
2310 u8 cpumode = PERF_RECORD_MISC_USER;
2311 int i, j, err, nr_entries;
2316 chain_nr = chain->nr;
2318 if (perf_evsel__has_branch_callstack(evsel)) {
2319 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2320 root_al, max_stack);
2322 return (err < 0) ? err : 0;
2326 * Based on DWARF debug information, some architectures skip
2327 * a callchain entry saved by the kernel.
2329 skip_idx = arch_skip_callchain_idx(thread, chain);
2332 * Add branches to call stack for easier browsing. This gives
2333 * more context for a sample than just the callers.
2335 * This uses individual histograms of paths compared to the
2336 * aggregated histograms the normal LBR mode uses.
2338 * Limitations for now:
2339 * - No extra filters
2340 * - No annotations (should annotate somehow)
2343 if (branch && callchain_param.branch_callstack) {
2344 int nr = min(max_stack, (int)branch->nr);
2345 struct branch_entry be[nr];
2346 struct iterations iter[nr];
2348 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2349 pr_warning("corrupted branch chain. skipping...\n");
2353 for (i = 0; i < nr; i++) {
2354 if (callchain_param.order == ORDER_CALLEE) {
2355 be[i] = branch->entries[i];
2361 * Check for overlap into the callchain.
2362 * The return address is one off compared to
2363 * the branch entry. To adjust for this
2364 * assume the calling instruction is not longer
2367 if (i == skip_idx ||
2368 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2370 else if (be[i].from < chain->ips[first_call] &&
2371 be[i].from >= chain->ips[first_call] - 8)
2374 be[i] = branch->entries[branch->nr - i - 1];
2377 memset(iter, 0, sizeof(struct iterations) * nr);
2378 nr = remove_loops(be, nr, iter);
2380 for (i = 0; i < nr; i++) {
2381 err = add_callchain_ip(thread, cursor, parent,
2388 err = add_callchain_ip(thread, cursor, parent, root_al,
2405 if (callchain_param.order != ORDER_CALLEE) {
2406 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2407 &cpumode, chain->nr - first_call);
2409 return (err < 0) ? err : 0;
2411 for (i = first_call, nr_entries = 0;
2412 i < chain_nr && nr_entries < max_stack; i++) {
2415 if (callchain_param.order == ORDER_CALLEE)
2418 j = chain->nr - i - 1;
2420 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2425 if (ip < PERF_CONTEXT_MAX)
2427 else if (callchain_param.order != ORDER_CALLEE) {
2428 err = find_prev_cpumode(chain, thread, cursor, parent,
2429 root_al, &cpumode, j);
2431 return (err < 0) ? err : 0;
2435 err = add_callchain_ip(thread, cursor, parent,
2436 root_al, &cpumode, ip,
2437 false, NULL, NULL, 0);
2440 return (err < 0) ? err : 0;
2446 static int append_inlines(struct callchain_cursor *cursor,
2447 struct map *map, struct symbol *sym, u64 ip)
2449 struct inline_node *inline_node;
2450 struct inline_list *ilist;
2454 if (!symbol_conf.inline_name || !map || !sym)
2457 addr = map__map_ip(map, ip);
2458 addr = map__rip_2objdump(map, addr);
2460 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2462 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2465 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2468 list_for_each_entry(ilist, &inline_node->val, list) {
2469 ret = callchain_cursor_append(cursor, ip, map,
2470 ilist->symbol, false,
2471 NULL, 0, 0, 0, ilist->srcline);
2480 static int unwind_entry(struct unwind_entry *entry, void *arg)
2482 struct callchain_cursor *cursor = arg;
2483 const char *srcline = NULL;
2484 u64 addr = entry->ip;
2486 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2489 if (append_inlines(cursor, entry->map, entry->sym, entry->ip) == 0)
2493 * Convert entry->ip from a virtual address to an offset in
2494 * its corresponding binary.
2497 addr = map__map_ip(entry->map, entry->ip);
2499 srcline = callchain_srcline(entry->map, entry->sym, addr);
2500 return callchain_cursor_append(cursor, entry->ip,
2501 entry->map, entry->sym,
2502 false, NULL, 0, 0, 0, srcline);
2505 static int thread__resolve_callchain_unwind(struct thread *thread,
2506 struct callchain_cursor *cursor,
2507 struct evsel *evsel,
2508 struct perf_sample *sample,
2511 /* Can we do dwarf post unwind? */
2512 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2513 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
2516 /* Bail out if nothing was captured. */
2517 if ((!sample->user_regs.regs) ||
2518 (!sample->user_stack.size))
2521 return unwind__get_entries(unwind_entry, cursor,
2522 thread, sample, max_stack);
2525 int thread__resolve_callchain(struct thread *thread,
2526 struct callchain_cursor *cursor,
2527 struct evsel *evsel,
2528 struct perf_sample *sample,
2529 struct symbol **parent,
2530 struct addr_location *root_al,
2535 callchain_cursor_reset(cursor);
2537 if (callchain_param.order == ORDER_CALLEE) {
2538 ret = thread__resolve_callchain_sample(thread, cursor,
2544 ret = thread__resolve_callchain_unwind(thread, cursor,
2548 ret = thread__resolve_callchain_unwind(thread, cursor,
2553 ret = thread__resolve_callchain_sample(thread, cursor,
2562 int machine__for_each_thread(struct machine *machine,
2563 int (*fn)(struct thread *thread, void *p),
2566 struct threads *threads;
2568 struct thread *thread;
2572 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2573 threads = &machine->threads[i];
2574 for (nd = rb_first_cached(&threads->entries); nd;
2576 thread = rb_entry(nd, struct thread, rb_node);
2577 rc = fn(thread, priv);
2582 list_for_each_entry(thread, &threads->dead, node) {
2583 rc = fn(thread, priv);
2591 int machines__for_each_thread(struct machines *machines,
2592 int (*fn)(struct thread *thread, void *p),
2598 rc = machine__for_each_thread(&machines->host, fn, priv);
2602 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
2603 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2605 rc = machine__for_each_thread(machine, fn, priv);
2612 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
2613 struct target *target, struct perf_thread_map *threads,
2614 perf_event__handler_t process, bool data_mmap,
2615 unsigned int nr_threads_synthesize)
2617 if (target__has_task(target))
2618 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
2619 else if (target__has_cpu(target))
2620 return perf_event__synthesize_threads(tool, process,
2622 nr_threads_synthesize);
2623 /* command specified */
2627 pid_t machine__get_current_tid(struct machine *machine, int cpu)
2629 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2631 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
2634 return machine->current_tid[cpu];
2637 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2640 struct thread *thread;
2641 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2646 if (!machine->current_tid) {
2649 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
2650 if (!machine->current_tid)
2652 for (i = 0; i < nr_cpus; i++)
2653 machine->current_tid[i] = -1;
2656 if (cpu >= nr_cpus) {
2657 pr_err("Requested CPU %d too large. ", cpu);
2658 pr_err("Consider raising MAX_NR_CPUS\n");
2662 machine->current_tid[cpu] = tid;
2664 thread = machine__findnew_thread(machine, pid, tid);
2669 thread__put(thread);
2675 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2676 * normalized arch is needed.
2678 bool machine__is(struct machine *machine, const char *arch)
2680 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2683 int machine__nr_cpus_avail(struct machine *machine)
2685 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2688 int machine__get_kernel_start(struct machine *machine)
2690 struct map *map = machine__kernel_map(machine);
2694 * The only addresses above 2^63 are kernel addresses of a 64-bit
2695 * kernel. Note that addresses are unsigned so that on a 32-bit system
2696 * all addresses including kernel addresses are less than 2^32. In
2697 * that case (32-bit system), if the kernel mapping is unknown, all
2698 * addresses will be assumed to be in user space - see
2699 * machine__kernel_ip().
2701 machine->kernel_start = 1ULL << 63;
2703 err = map__load(map);
2705 * On x86_64, PTI entry trampolines are less than the
2706 * start of kernel text, but still above 2^63. So leave
2707 * kernel_start = 1ULL << 63 for x86_64.
2709 if (!err && !machine__is(machine, "x86_64"))
2710 machine->kernel_start = map->start;
2715 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2717 u8 addr_cpumode = cpumode;
2720 if (!machine->single_address_space)
2723 kernel_ip = machine__kernel_ip(machine, addr);
2725 case PERF_RECORD_MISC_KERNEL:
2726 case PERF_RECORD_MISC_USER:
2727 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2728 PERF_RECORD_MISC_USER;
2730 case PERF_RECORD_MISC_GUEST_KERNEL:
2731 case PERF_RECORD_MISC_GUEST_USER:
2732 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2733 PERF_RECORD_MISC_GUEST_USER;
2739 return addr_cpumode;
2742 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2744 return dsos__findnew(&machine->dsos, filename);
2747 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2749 struct machine *machine = vmachine;
2751 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
2756 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2757 *addrp = map->unmap_ip(map, sym->start);