1 // SPDX-License-Identifier: GPL-2.0
18 #include <sys/types.h>
22 #include "linux/hash.h"
25 #include "sane_ctype.h"
26 #include <symbol/kallsyms.h>
28 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
30 static void dsos__init(struct dsos *dsos)
32 INIT_LIST_HEAD(&dsos->head);
34 pthread_rwlock_init(&dsos->lock, NULL);
37 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
39 memset(machine, 0, sizeof(*machine));
40 map_groups__init(&machine->kmaps, machine);
41 RB_CLEAR_NODE(&machine->rb_node);
42 dsos__init(&machine->dsos);
44 machine->threads = RB_ROOT;
45 pthread_rwlock_init(&machine->threads_lock, NULL);
46 machine->nr_threads = 0;
47 INIT_LIST_HEAD(&machine->dead_threads);
48 machine->last_match = NULL;
50 machine->vdso_info = NULL;
55 machine->id_hdr_size = 0;
56 machine->kptr_restrict_warned = false;
57 machine->comm_exec = false;
58 machine->kernel_start = 0;
60 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
62 machine->root_dir = strdup(root_dir);
63 if (machine->root_dir == NULL)
66 if (pid != HOST_KERNEL_ID) {
67 struct thread *thread = machine__findnew_thread(machine, -1,
74 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
75 thread__set_comm(thread, comm, 0);
79 machine->current_tid = NULL;
84 struct machine *machine__new_host(void)
86 struct machine *machine = malloc(sizeof(*machine));
88 if (machine != NULL) {
89 machine__init(machine, "", HOST_KERNEL_ID);
91 if (machine__create_kernel_maps(machine) < 0)
101 struct machine *machine__new_kallsyms(void)
103 struct machine *machine = machine__new_host();
106 * 1) MAP__FUNCTION will go away when we stop loading separate maps for
107 * functions and data objects.
108 * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
109 * ask for not using the kcore parsing code, once this one is fixed
110 * to create a map per module.
112 if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) {
113 machine__delete(machine);
120 static void dsos__purge(struct dsos *dsos)
124 pthread_rwlock_wrlock(&dsos->lock);
126 list_for_each_entry_safe(pos, n, &dsos->head, node) {
127 RB_CLEAR_NODE(&pos->rb_node);
129 list_del_init(&pos->node);
133 pthread_rwlock_unlock(&dsos->lock);
136 static void dsos__exit(struct dsos *dsos)
139 pthread_rwlock_destroy(&dsos->lock);
142 void machine__delete_threads(struct machine *machine)
146 pthread_rwlock_wrlock(&machine->threads_lock);
147 nd = rb_first(&machine->threads);
149 struct thread *t = rb_entry(nd, struct thread, rb_node);
152 __machine__remove_thread(machine, t, false);
154 pthread_rwlock_unlock(&machine->threads_lock);
157 void machine__exit(struct machine *machine)
162 machine__destroy_kernel_maps(machine);
163 map_groups__exit(&machine->kmaps);
164 dsos__exit(&machine->dsos);
165 machine__exit_vdso(machine);
166 zfree(&machine->root_dir);
167 zfree(&machine->current_tid);
168 pthread_rwlock_destroy(&machine->threads_lock);
171 void machine__delete(struct machine *machine)
174 machine__exit(machine);
179 void machines__init(struct machines *machines)
181 machine__init(&machines->host, "", HOST_KERNEL_ID);
182 machines->guests = RB_ROOT;
185 void machines__exit(struct machines *machines)
187 machine__exit(&machines->host);
191 struct machine *machines__add(struct machines *machines, pid_t pid,
192 const char *root_dir)
194 struct rb_node **p = &machines->guests.rb_node;
195 struct rb_node *parent = NULL;
196 struct machine *pos, *machine = malloc(sizeof(*machine));
201 if (machine__init(machine, root_dir, pid) != 0) {
208 pos = rb_entry(parent, struct machine, rb_node);
215 rb_link_node(&machine->rb_node, parent, p);
216 rb_insert_color(&machine->rb_node, &machines->guests);
221 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
225 machines->host.comm_exec = comm_exec;
227 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
228 struct machine *machine = rb_entry(nd, struct machine, rb_node);
230 machine->comm_exec = comm_exec;
234 struct machine *machines__find(struct machines *machines, pid_t pid)
236 struct rb_node **p = &machines->guests.rb_node;
237 struct rb_node *parent = NULL;
238 struct machine *machine;
239 struct machine *default_machine = NULL;
241 if (pid == HOST_KERNEL_ID)
242 return &machines->host;
246 machine = rb_entry(parent, struct machine, rb_node);
247 if (pid < machine->pid)
249 else if (pid > machine->pid)
254 default_machine = machine;
257 return default_machine;
260 struct machine *machines__findnew(struct machines *machines, pid_t pid)
263 const char *root_dir = "";
264 struct machine *machine = machines__find(machines, pid);
266 if (machine && (machine->pid == pid))
269 if ((pid != HOST_KERNEL_ID) &&
270 (pid != DEFAULT_GUEST_KERNEL_ID) &&
271 (symbol_conf.guestmount)) {
272 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
273 if (access(path, R_OK)) {
274 static struct strlist *seen;
277 seen = strlist__new(NULL, NULL);
279 if (!strlist__has_entry(seen, path)) {
280 pr_err("Can't access file %s\n", path);
281 strlist__add(seen, path);
289 machine = machines__add(machines, pid, root_dir);
294 void machines__process_guests(struct machines *machines,
295 machine__process_t process, void *data)
299 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
300 struct machine *pos = rb_entry(nd, struct machine, rb_node);
305 char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
307 if (machine__is_host(machine))
308 snprintf(bf, size, "[%s]", "kernel.kallsyms");
309 else if (machine__is_default_guest(machine))
310 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
312 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
319 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
321 struct rb_node *node;
322 struct machine *machine;
324 machines->host.id_hdr_size = id_hdr_size;
326 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
327 machine = rb_entry(node, struct machine, rb_node);
328 machine->id_hdr_size = id_hdr_size;
334 static void machine__update_thread_pid(struct machine *machine,
335 struct thread *th, pid_t pid)
337 struct thread *leader;
339 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
344 if (th->pid_ == th->tid)
347 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
352 leader->mg = map_groups__new(machine);
357 if (th->mg == leader->mg)
362 * Maps are created from MMAP events which provide the pid and
363 * tid. Consequently there never should be any maps on a thread
364 * with an unknown pid. Just print an error if there are.
366 if (!map_groups__empty(th->mg))
367 pr_err("Discarding thread maps for %d:%d\n",
369 map_groups__put(th->mg);
372 th->mg = map_groups__get(leader->mg);
377 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
382 * Caller must eventually drop thread->refcnt returned with a successful
383 * lookup/new thread inserted.
385 static struct thread *____machine__findnew_thread(struct machine *machine,
386 pid_t pid, pid_t tid,
389 struct rb_node **p = &machine->threads.rb_node;
390 struct rb_node *parent = NULL;
394 * Front-end cache - TID lookups come in blocks,
395 * so most of the time we dont have to look up
398 th = machine->last_match;
400 if (th->tid == tid) {
401 machine__update_thread_pid(machine, th, pid);
402 return thread__get(th);
405 machine->last_match = NULL;
410 th = rb_entry(parent, struct thread, rb_node);
412 if (th->tid == tid) {
413 machine->last_match = th;
414 machine__update_thread_pid(machine, th, pid);
415 return thread__get(th);
427 th = thread__new(pid, tid);
429 rb_link_node(&th->rb_node, parent, p);
430 rb_insert_color(&th->rb_node, &machine->threads);
433 * We have to initialize map_groups separately
434 * after rb tree is updated.
436 * The reason is that we call machine__findnew_thread
437 * within thread__init_map_groups to find the thread
438 * leader and that would screwed the rb tree.
440 if (thread__init_map_groups(th, machine)) {
441 rb_erase_init(&th->rb_node, &machine->threads);
442 RB_CLEAR_NODE(&th->rb_node);
447 * It is now in the rbtree, get a ref
450 machine->last_match = th;
451 ++machine->nr_threads;
457 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
459 return ____machine__findnew_thread(machine, pid, tid, true);
462 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
467 pthread_rwlock_wrlock(&machine->threads_lock);
468 th = __machine__findnew_thread(machine, pid, tid);
469 pthread_rwlock_unlock(&machine->threads_lock);
473 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
477 pthread_rwlock_rdlock(&machine->threads_lock);
478 th = ____machine__findnew_thread(machine, pid, tid, false);
479 pthread_rwlock_unlock(&machine->threads_lock);
483 struct comm *machine__thread_exec_comm(struct machine *machine,
484 struct thread *thread)
486 if (machine->comm_exec)
487 return thread__exec_comm(thread);
489 return thread__comm(thread);
492 int machine__process_comm_event(struct machine *machine, union perf_event *event,
493 struct perf_sample *sample)
495 struct thread *thread = machine__findnew_thread(machine,
498 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
502 machine->comm_exec = true;
505 perf_event__fprintf_comm(event, stdout);
507 if (thread == NULL ||
508 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
509 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
518 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
519 union perf_event *event,
520 struct perf_sample *sample __maybe_unused)
522 struct thread *thread = machine__findnew_thread(machine,
523 event->namespaces.pid,
524 event->namespaces.tid);
527 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
528 "\nWARNING: kernel seems to support more namespaces than perf"
529 " tool.\nTry updating the perf tool..\n\n");
531 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
532 "\nWARNING: perf tool seems to support more namespaces than"
533 " the kernel.\nTry updating the kernel..\n\n");
536 perf_event__fprintf_namespaces(event, stdout);
538 if (thread == NULL ||
539 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
540 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
549 int machine__process_lost_event(struct machine *machine __maybe_unused,
550 union perf_event *event, struct perf_sample *sample __maybe_unused)
552 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
553 event->lost.id, event->lost.lost);
557 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
558 union perf_event *event, struct perf_sample *sample)
560 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
561 sample->id, event->lost_samples.lost);
565 static struct dso *machine__findnew_module_dso(struct machine *machine,
567 const char *filename)
571 pthread_rwlock_wrlock(&machine->dsos.lock);
573 dso = __dsos__find(&machine->dsos, m->name, true);
575 dso = __dsos__addnew(&machine->dsos, m->name);
579 dso__set_module_info(dso, m, machine);
580 dso__set_long_name(dso, strdup(filename), true);
585 pthread_rwlock_unlock(&machine->dsos.lock);
589 int machine__process_aux_event(struct machine *machine __maybe_unused,
590 union perf_event *event)
593 perf_event__fprintf_aux(event, stdout);
597 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
598 union perf_event *event)
601 perf_event__fprintf_itrace_start(event, stdout);
605 int machine__process_switch_event(struct machine *machine __maybe_unused,
606 union perf_event *event)
609 perf_event__fprintf_switch(event, stdout);
613 static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
615 const char *dup_filename;
617 if (!filename || !dso || !dso->long_name)
619 if (dso->long_name[0] != '[')
621 if (!strchr(filename, '/'))
624 dup_filename = strdup(filename);
628 dso__set_long_name(dso, dup_filename, true);
631 struct map *machine__findnew_module_map(struct machine *machine, u64 start,
632 const char *filename)
634 struct map *map = NULL;
635 struct dso *dso = NULL;
638 if (kmod_path__parse_name(&m, filename))
641 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
645 * If the map's dso is an offline module, give dso__load()
646 * a chance to find the file path of that module by fixing
649 dso__adjust_kmod_long_name(map->dso, filename);
653 dso = machine__findnew_module_dso(machine, &m, filename);
657 map = map__new2(start, dso, MAP__FUNCTION);
661 map_groups__insert(&machine->kmaps, map);
663 /* Put the map here because map_groups__insert alread got it */
666 /* put the dso here, corresponding to machine__findnew_module_dso */
672 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
675 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
677 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
678 struct machine *pos = rb_entry(nd, struct machine, rb_node);
679 ret += __dsos__fprintf(&pos->dsos.head, fp);
685 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
686 bool (skip)(struct dso *dso, int parm), int parm)
688 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
691 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
692 bool (skip)(struct dso *dso, int parm), int parm)
695 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
697 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
698 struct machine *pos = rb_entry(nd, struct machine, rb_node);
699 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
704 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
708 struct dso *kdso = machine__kernel_map(machine)->dso;
710 if (kdso->has_build_id) {
711 char filename[PATH_MAX];
712 if (dso__build_id_filename(kdso, filename, sizeof(filename),
714 printed += fprintf(fp, "[0] %s\n", filename);
717 for (i = 0; i < vmlinux_path__nr_entries; ++i)
718 printed += fprintf(fp, "[%d] %s\n",
719 i + kdso->has_build_id, vmlinux_path[i]);
724 size_t machine__fprintf(struct machine *machine, FILE *fp)
729 pthread_rwlock_rdlock(&machine->threads_lock);
731 ret = fprintf(fp, "Threads: %u\n", machine->nr_threads);
733 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
734 struct thread *pos = rb_entry(nd, struct thread, rb_node);
736 ret += thread__fprintf(pos, fp);
739 pthread_rwlock_unlock(&machine->threads_lock);
744 static struct dso *machine__get_kernel(struct machine *machine)
746 const char *vmlinux_name = NULL;
749 if (machine__is_host(machine)) {
750 vmlinux_name = symbol_conf.vmlinux_name;
752 vmlinux_name = DSO__NAME_KALLSYMS;
754 kernel = machine__findnew_kernel(machine, vmlinux_name,
755 "[kernel]", DSO_TYPE_KERNEL);
759 if (machine__is_default_guest(machine))
760 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
762 vmlinux_name = machine__mmap_name(machine, bf,
765 kernel = machine__findnew_kernel(machine, vmlinux_name,
767 DSO_TYPE_GUEST_KERNEL);
770 if (kernel != NULL && (!kernel->has_build_id))
771 dso__read_running_kernel_build_id(kernel, machine);
776 struct process_args {
780 static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
783 if (machine__is_default_guest(machine))
784 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
786 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
789 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
791 /* Figure out the start address of kernel map from /proc/kallsyms.
792 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
793 * symbol_name if it's not that important.
795 static int machine__get_running_kernel_start(struct machine *machine,
796 const char **symbol_name, u64 *start)
798 char filename[PATH_MAX];
803 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
805 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
808 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
809 err = kallsyms__get_function_start(filename, name, &addr);
824 /* Kernel-space maps for symbols that are outside the main kernel map and module maps */
825 struct extra_kernel_map {
831 static int machine__create_extra_kernel_map(struct machine *machine,
833 struct extra_kernel_map *xm)
838 map = map__new2(xm->start, kernel, MAP__FUNCTION);
843 map->pgoff = xm->pgoff;
845 kmap = map__kmap(map);
847 kmap->kmaps = &machine->kmaps;
849 map_groups__insert(&machine->kmaps, map);
851 pr_debug2("Added extra kernel map %" PRIx64 "-%" PRIx64 "\n",
852 map->start, map->end);
859 static u64 find_entry_trampoline(struct dso *dso)
861 /* Duplicates are removed so lookup all aliases */
862 const char *syms[] = {
864 "__entry_trampoline_start",
865 "entry_SYSCALL_64_trampoline",
867 struct symbol *sym = dso__first_symbol(dso, MAP__FUNCTION);
870 for (; sym; sym = dso__next_symbol(sym)) {
871 if (sym->binding != STB_GLOBAL)
873 for (i = 0; i < ARRAY_SIZE(syms); i++) {
874 if (!strcmp(sym->name, syms[i]))
883 * These values can be used for kernels that do not have symbols for the entry
884 * trampolines in kallsyms.
886 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
887 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
888 #define X86_64_ENTRY_TRAMPOLINE 0x6000
890 /* Map x86_64 PTI entry trampolines */
891 int machine__map_x86_64_entry_trampolines(struct machine *machine,
894 u64 pgoff = find_entry_trampoline(kernel);
895 int nr_cpus_avail, cpu;
900 nr_cpus_avail = machine__nr_cpus_avail(machine);
902 /* Add a 1 page map for each CPU's entry trampoline */
903 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
904 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
905 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
906 X86_64_ENTRY_TRAMPOLINE;
907 struct extra_kernel_map xm = {
909 .end = va + page_size,
913 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
920 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
925 if (machine__get_running_kernel_start(machine, NULL, &start))
928 /* In case of renewal the kernel map, destroy previous one */
929 machine__destroy_kernel_maps(machine);
931 for (type = 0; type < MAP__NR_TYPES; ++type) {
935 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
936 if (machine->vmlinux_maps[type] == NULL)
939 machine->vmlinux_maps[type]->map_ip =
940 machine->vmlinux_maps[type]->unmap_ip =
942 map = __machine__kernel_map(machine, type);
943 kmap = map__kmap(map);
947 kmap->kmaps = &machine->kmaps;
948 map_groups__insert(&machine->kmaps, map);
954 void machine__destroy_kernel_maps(struct machine *machine)
958 for (type = 0; type < MAP__NR_TYPES; ++type) {
960 struct map *map = __machine__kernel_map(machine, type);
965 kmap = map__kmap(map);
966 map_groups__remove(&machine->kmaps, map);
967 if (kmap && kmap->ref_reloc_sym) {
969 * ref_reloc_sym is shared among all maps, so free just
972 if (type == MAP__FUNCTION) {
973 zfree((char **)&kmap->ref_reloc_sym->name);
974 zfree(&kmap->ref_reloc_sym);
976 kmap->ref_reloc_sym = NULL;
979 map__put(machine->vmlinux_maps[type]);
980 machine->vmlinux_maps[type] = NULL;
984 int machines__create_guest_kernel_maps(struct machines *machines)
987 struct dirent **namelist = NULL;
993 if (symbol_conf.default_guest_vmlinux_name ||
994 symbol_conf.default_guest_modules ||
995 symbol_conf.default_guest_kallsyms) {
996 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
999 if (symbol_conf.guestmount) {
1000 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1003 for (i = 0; i < items; i++) {
1004 if (!isdigit(namelist[i]->d_name[0])) {
1005 /* Filter out . and .. */
1008 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1009 if ((*endp != '\0') ||
1010 (endp == namelist[i]->d_name) ||
1011 (errno == ERANGE)) {
1012 pr_debug("invalid directory (%s). Skipping.\n",
1013 namelist[i]->d_name);
1016 sprintf(path, "%s/%s/proc/kallsyms",
1017 symbol_conf.guestmount,
1018 namelist[i]->d_name);
1019 ret = access(path, R_OK);
1021 pr_debug("Can't access file %s\n", path);
1024 machines__create_kernel_maps(machines, pid);
1033 void machines__destroy_kernel_maps(struct machines *machines)
1035 struct rb_node *next = rb_first(&machines->guests);
1037 machine__destroy_kernel_maps(&machines->host);
1040 struct machine *pos = rb_entry(next, struct machine, rb_node);
1042 next = rb_next(&pos->rb_node);
1043 rb_erase(&pos->rb_node, &machines->guests);
1044 machine__delete(pos);
1048 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1050 struct machine *machine = machines__findnew(machines, pid);
1052 if (machine == NULL)
1055 return machine__create_kernel_maps(machine);
1058 int __machine__load_kallsyms(struct machine *machine, const char *filename,
1059 enum map_type type, bool no_kcore)
1061 struct map *map = machine__kernel_map(machine);
1062 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore);
1065 dso__set_loaded(map->dso, type);
1067 * Since /proc/kallsyms will have multiple sessions for the
1068 * kernel, with modules between them, fixup the end of all
1071 __map_groups__fixup_end(&machine->kmaps, type);
1077 int machine__load_kallsyms(struct machine *machine, const char *filename,
1080 return __machine__load_kallsyms(machine, filename, type, false);
1083 int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
1085 struct map *map = machine__kernel_map(machine);
1086 int ret = dso__load_vmlinux_path(map->dso, map);
1089 dso__set_loaded(map->dso, type);
1094 static void map_groups__fixup_end(struct map_groups *mg)
1097 for (i = 0; i < MAP__NR_TYPES; ++i)
1098 __map_groups__fixup_end(mg, i);
1101 static char *get_kernel_version(const char *root_dir)
1103 char version[PATH_MAX];
1106 const char *prefix = "Linux version ";
1108 sprintf(version, "%s/proc/version", root_dir);
1109 file = fopen(version, "r");
1114 tmp = fgets(version, sizeof(version), file);
1117 name = strstr(version, prefix);
1120 name += strlen(prefix);
1121 tmp = strchr(name, ' ');
1125 return strdup(name);
1128 static bool is_kmod_dso(struct dso *dso)
1130 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1131 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1134 static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1135 struct kmod_path *m)
1140 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1144 long_name = strdup(path);
1145 if (long_name == NULL)
1148 dso__set_long_name(map->dso, long_name, true);
1149 dso__kernel_module_get_build_id(map->dso, "");
1152 * Full name could reveal us kmod compression, so
1153 * we need to update the symtab_type if needed.
1155 if (m->comp && is_kmod_dso(map->dso))
1156 map->dso->symtab_type++;
1161 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1162 const char *dir_name, int depth)
1164 struct dirent *dent;
1165 DIR *dir = opendir(dir_name);
1169 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1173 while ((dent = readdir(dir)) != NULL) {
1174 char path[PATH_MAX];
1177 /*sshfs might return bad dent->d_type, so we have to stat*/
1178 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1179 if (stat(path, &st))
1182 if (S_ISDIR(st.st_mode)) {
1183 if (!strcmp(dent->d_name, ".") ||
1184 !strcmp(dent->d_name, ".."))
1187 /* Do not follow top-level source and build symlinks */
1189 if (!strcmp(dent->d_name, "source") ||
1190 !strcmp(dent->d_name, "build"))
1194 ret = map_groups__set_modules_path_dir(mg, path,
1201 ret = kmod_path__parse_name(&m, dent->d_name);
1206 ret = map_groups__set_module_path(mg, path, &m);
1220 static int machine__set_modules_path(struct machine *machine)
1223 char modules_path[PATH_MAX];
1225 version = get_kernel_version(machine->root_dir);
1229 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1230 machine->root_dir, version);
1233 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
1235 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1236 u64 *size __maybe_unused,
1237 const char *name __maybe_unused)
1242 static int machine__create_module(void *arg, const char *name, u64 start,
1245 struct machine *machine = arg;
1248 if (arch__fix_module_text_start(&start, &size, name) < 0)
1251 map = machine__findnew_module_map(machine, start, name);
1254 map->end = start + size;
1256 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1261 static int machine__create_modules(struct machine *machine)
1263 const char *modules;
1264 char path[PATH_MAX];
1266 if (machine__is_default_guest(machine)) {
1267 modules = symbol_conf.default_guest_modules;
1269 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1273 if (symbol__restricted_filename(modules, "/proc/modules"))
1276 if (modules__parse(modules, machine, machine__create_module))
1279 if (!machine__set_modules_path(machine))
1282 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1287 int machine__create_kernel_maps(struct machine *machine)
1289 struct dso *kernel = machine__get_kernel(machine);
1290 const char *name = NULL;
1297 ret = __machine__create_kernel_maps(machine, kernel);
1302 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1303 if (machine__is_host(machine))
1304 pr_debug("Problems creating module maps, "
1305 "continuing anyway...\n");
1307 pr_debug("Problems creating module maps for guest %d, "
1308 "continuing anyway...\n", machine->pid);
1312 * Now that we have all the maps created, just set the ->end of them:
1314 map_groups__fixup_end(&machine->kmaps);
1316 if (!machine__get_running_kernel_start(machine, &name, &addr)) {
1318 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
1319 machine__destroy_kernel_maps(machine);
1327 static void machine__set_kernel_mmap_len(struct machine *machine,
1328 union perf_event *event)
1332 for (i = 0; i < MAP__NR_TYPES; i++) {
1333 machine->vmlinux_maps[i]->start = event->mmap.start;
1334 machine->vmlinux_maps[i]->end = (event->mmap.start +
1337 * Be a bit paranoid here, some perf.data file came with
1338 * a zero sized synthesized MMAP event for the kernel.
1340 if (machine->vmlinux_maps[i]->end == 0)
1341 machine->vmlinux_maps[i]->end = ~0ULL;
1345 static bool machine__uses_kcore(struct machine *machine)
1349 list_for_each_entry(dso, &machine->dsos.head, node) {
1350 if (dso__is_kcore(dso))
1357 static int machine__process_kernel_mmap_event(struct machine *machine,
1358 union perf_event *event)
1361 char kmmap_prefix[PATH_MAX];
1362 enum dso_kernel_type kernel_type;
1363 bool is_kernel_mmap;
1365 /* If we have maps from kcore then we do not need or want any others */
1366 if (machine__uses_kcore(machine))
1369 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1370 if (machine__is_host(machine))
1371 kernel_type = DSO_TYPE_KERNEL;
1373 kernel_type = DSO_TYPE_GUEST_KERNEL;
1375 is_kernel_mmap = memcmp(event->mmap.filename,
1377 strlen(kmmap_prefix) - 1) == 0;
1378 if (event->mmap.filename[0] == '/' ||
1379 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1380 map = machine__findnew_module_map(machine, event->mmap.start,
1381 event->mmap.filename);
1385 map->end = map->start + event->mmap.len;
1386 } else if (is_kernel_mmap) {
1387 const char *symbol_name = (event->mmap.filename +
1388 strlen(kmmap_prefix));
1390 * Should be there already, from the build-id table in
1393 struct dso *kernel = NULL;
1396 pthread_rwlock_rdlock(&machine->dsos.lock);
1398 list_for_each_entry(dso, &machine->dsos.head, node) {
1401 * The cpumode passed to is_kernel_module is not the
1402 * cpumode of *this* event. If we insist on passing
1403 * correct cpumode to is_kernel_module, we should
1404 * record the cpumode when we adding this dso to the
1407 * However we don't really need passing correct
1408 * cpumode. We know the correct cpumode must be kernel
1409 * mode (if not, we should not link it onto kernel_dsos
1412 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1413 * is_kernel_module() treats it as a kernel cpumode.
1417 is_kernel_module(dso->long_name,
1418 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1426 pthread_rwlock_unlock(&machine->dsos.lock);
1429 kernel = machine__findnew_dso(machine, kmmap_prefix);
1433 kernel->kernel = kernel_type;
1434 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1439 if (strstr(kernel->long_name, "vmlinux"))
1440 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1442 machine__set_kernel_mmap_len(machine, event);
1445 * Avoid using a zero address (kptr_restrict) for the ref reloc
1446 * symbol. Effectively having zero here means that at record
1447 * time /proc/sys/kernel/kptr_restrict was non zero.
1449 if (event->mmap.pgoff != 0) {
1450 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1455 if (machine__is_default_guest(machine)) {
1457 * preload dso of guest kernel and modules
1459 dso__load(kernel, machine__kernel_map(machine));
1467 int machine__process_mmap2_event(struct machine *machine,
1468 union perf_event *event,
1469 struct perf_sample *sample)
1471 struct thread *thread;
1477 perf_event__fprintf_mmap2(event, stdout);
1479 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1480 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1481 ret = machine__process_kernel_mmap_event(machine, event);
1487 thread = machine__findnew_thread(machine, event->mmap2.pid,
1492 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1493 type = MAP__VARIABLE;
1495 type = MAP__FUNCTION;
1497 map = map__new(machine, event->mmap2.start,
1498 event->mmap2.len, event->mmap2.pgoff,
1500 event->mmap2.min, event->mmap2.ino,
1501 event->mmap2.ino_generation,
1504 event->mmap2.filename, type, thread);
1507 goto out_problem_map;
1509 ret = thread__insert_map(thread, map);
1511 goto out_problem_insert;
1513 thread__put(thread);
1520 thread__put(thread);
1522 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1526 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1527 struct perf_sample *sample)
1529 struct thread *thread;
1535 perf_event__fprintf_mmap(event, stdout);
1537 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1538 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1539 ret = machine__process_kernel_mmap_event(machine, event);
1545 thread = machine__findnew_thread(machine, event->mmap.pid,
1550 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1551 type = MAP__VARIABLE;
1553 type = MAP__FUNCTION;
1555 map = map__new(machine, event->mmap.start,
1556 event->mmap.len, event->mmap.pgoff,
1558 event->mmap.filename,
1562 goto out_problem_map;
1564 ret = thread__insert_map(thread, map);
1566 goto out_problem_insert;
1568 thread__put(thread);
1575 thread__put(thread);
1577 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1581 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1583 if (machine->last_match == th)
1584 machine->last_match = NULL;
1586 BUG_ON(refcount_read(&th->refcnt) == 0);
1588 pthread_rwlock_wrlock(&machine->threads_lock);
1589 rb_erase_init(&th->rb_node, &machine->threads);
1590 RB_CLEAR_NODE(&th->rb_node);
1591 --machine->nr_threads;
1593 * Move it first to the dead_threads list, then drop the reference,
1594 * if this is the last reference, then the thread__delete destructor
1595 * will be called and we will remove it from the dead_threads list.
1597 list_add_tail(&th->node, &machine->dead_threads);
1599 pthread_rwlock_unlock(&machine->threads_lock);
1603 void machine__remove_thread(struct machine *machine, struct thread *th)
1605 return __machine__remove_thread(machine, th, true);
1608 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1609 struct perf_sample *sample)
1611 struct thread *thread = machine__find_thread(machine,
1614 struct thread *parent = machine__findnew_thread(machine,
1620 perf_event__fprintf_task(event, stdout);
1623 * There may be an existing thread that is not actually the parent,
1624 * either because we are processing events out of order, or because the
1625 * (fork) event that would have removed the thread was lost. Assume the
1626 * latter case and continue on as best we can.
1628 if (parent->pid_ != (pid_t)event->fork.ppid) {
1629 dump_printf("removing erroneous parent thread %d/%d\n",
1630 parent->pid_, parent->tid);
1631 machine__remove_thread(machine, parent);
1632 thread__put(parent);
1633 parent = machine__findnew_thread(machine, event->fork.ppid,
1637 /* if a thread currently exists for the thread id remove it */
1638 if (thread != NULL) {
1639 machine__remove_thread(machine, thread);
1640 thread__put(thread);
1643 thread = machine__findnew_thread(machine, event->fork.pid,
1646 if (thread == NULL || parent == NULL ||
1647 thread__fork(thread, parent, sample->time) < 0) {
1648 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1651 thread__put(thread);
1652 thread__put(parent);
1657 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1658 struct perf_sample *sample __maybe_unused)
1660 struct thread *thread = machine__find_thread(machine,
1665 perf_event__fprintf_task(event, stdout);
1667 if (thread != NULL) {
1668 thread__exited(thread);
1669 thread__put(thread);
1675 int machine__process_event(struct machine *machine, union perf_event *event,
1676 struct perf_sample *sample)
1680 switch (event->header.type) {
1681 case PERF_RECORD_COMM:
1682 ret = machine__process_comm_event(machine, event, sample); break;
1683 case PERF_RECORD_MMAP:
1684 ret = machine__process_mmap_event(machine, event, sample); break;
1685 case PERF_RECORD_NAMESPACES:
1686 ret = machine__process_namespaces_event(machine, event, sample); break;
1687 case PERF_RECORD_MMAP2:
1688 ret = machine__process_mmap2_event(machine, event, sample); break;
1689 case PERF_RECORD_FORK:
1690 ret = machine__process_fork_event(machine, event, sample); break;
1691 case PERF_RECORD_EXIT:
1692 ret = machine__process_exit_event(machine, event, sample); break;
1693 case PERF_RECORD_LOST:
1694 ret = machine__process_lost_event(machine, event, sample); break;
1695 case PERF_RECORD_AUX:
1696 ret = machine__process_aux_event(machine, event); break;
1697 case PERF_RECORD_ITRACE_START:
1698 ret = machine__process_itrace_start_event(machine, event); break;
1699 case PERF_RECORD_LOST_SAMPLES:
1700 ret = machine__process_lost_samples_event(machine, event, sample); break;
1701 case PERF_RECORD_SWITCH:
1702 case PERF_RECORD_SWITCH_CPU_WIDE:
1703 ret = machine__process_switch_event(machine, event); break;
1712 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1714 if (!regexec(regex, sym->name, 0, NULL, 0))
1719 static void ip__resolve_ams(struct thread *thread,
1720 struct addr_map_symbol *ams,
1723 struct addr_location al;
1725 memset(&al, 0, sizeof(al));
1727 * We cannot use the header.misc hint to determine whether a
1728 * branch stack address is user, kernel, guest, hypervisor.
1729 * Branches may straddle the kernel/user/hypervisor boundaries.
1730 * Thus, we have to try consecutively until we find a match
1731 * or else, the symbol is unknown
1733 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
1736 ams->al_addr = al.addr;
1742 static void ip__resolve_data(struct thread *thread,
1743 u8 m, struct addr_map_symbol *ams,
1744 u64 addr, u64 phys_addr)
1746 struct addr_location al;
1748 memset(&al, 0, sizeof(al));
1750 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
1751 if (al.map == NULL) {
1753 * some shared data regions have execute bit set which puts
1754 * their mapping in the MAP__FUNCTION type array.
1755 * Check there as a fallback option before dropping the sample.
1757 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
1761 ams->al_addr = al.addr;
1764 ams->phys_addr = phys_addr;
1767 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1768 struct addr_location *al)
1770 struct mem_info *mi = zalloc(sizeof(*mi));
1775 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1776 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1777 sample->addr, sample->phys_addr);
1778 mi->data_src.val = sample->data_src;
1788 static int add_callchain_ip(struct thread *thread,
1789 struct callchain_cursor *cursor,
1790 struct symbol **parent,
1791 struct addr_location *root_al,
1795 struct branch_flags *flags,
1796 struct iterations *iter,
1799 struct addr_location al;
1800 int nr_loop_iter = 0;
1801 u64 iter_cycles = 0;
1806 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1809 if (ip >= PERF_CONTEXT_MAX) {
1811 case PERF_CONTEXT_HV:
1812 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
1814 case PERF_CONTEXT_KERNEL:
1815 *cpumode = PERF_RECORD_MISC_KERNEL;
1817 case PERF_CONTEXT_USER:
1818 *cpumode = PERF_RECORD_MISC_USER;
1821 pr_debug("invalid callchain context: "
1822 "%"PRId64"\n", (s64) ip);
1824 * It seems the callchain is corrupted.
1827 callchain_cursor_reset(cursor);
1832 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1836 if (al.sym != NULL) {
1837 if (perf_hpp_list.parent && !*parent &&
1838 symbol__match_regex(al.sym, &parent_regex))
1840 else if (have_ignore_callees && root_al &&
1841 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1842 /* Treat this symbol as the root,
1843 forgetting its callees. */
1845 callchain_cursor_reset(cursor);
1849 if (symbol_conf.hide_unresolved && al.sym == NULL)
1853 nr_loop_iter = iter->nr_loop_iter;
1854 iter_cycles = iter->cycles;
1857 return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
1858 branch, flags, nr_loop_iter,
1859 iter_cycles, branch_from);
1862 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1863 struct addr_location *al)
1866 const struct branch_stack *bs = sample->branch_stack;
1867 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
1872 for (i = 0; i < bs->nr; i++) {
1873 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1874 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
1875 bi[i].flags = bs->entries[i].flags;
1880 static void save_iterations(struct iterations *iter,
1881 struct branch_entry *be, int nr)
1885 iter->nr_loop_iter = nr;
1888 for (i = 0; i < nr; i++)
1889 iter->cycles += be[i].flags.cycles;
1894 #define NO_ENTRY 0xff
1896 #define PERF_MAX_BRANCH_DEPTH 127
1899 static int remove_loops(struct branch_entry *l, int nr,
1900 struct iterations *iter)
1903 unsigned char chash[CHASHSZ];
1905 memset(chash, NO_ENTRY, sizeof(chash));
1907 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1909 for (i = 0; i < nr; i++) {
1910 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1912 /* no collision handling for now */
1913 if (chash[h] == NO_ENTRY) {
1915 } else if (l[chash[h]].from == l[i].from) {
1916 bool is_loop = true;
1917 /* check if it is a real loop */
1919 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1920 if (l[j].from != l[i + off].from) {
1927 save_iterations(iter + i + off,
1930 memmove(iter + i, iter + i + off,
1933 memmove(l + i, l + i + off,
1945 * Recolve LBR callstack chain sample
1947 * 1 on success get LBR callchain information
1948 * 0 no available LBR callchain information, should try fp
1949 * negative error code on other errors.
1951 static int resolve_lbr_callchain_sample(struct thread *thread,
1952 struct callchain_cursor *cursor,
1953 struct perf_sample *sample,
1954 struct symbol **parent,
1955 struct addr_location *root_al,
1958 struct ip_callchain *chain = sample->callchain;
1959 int chain_nr = min(max_stack, (int)chain->nr), i;
1960 u8 cpumode = PERF_RECORD_MISC_USER;
1961 u64 ip, branch_from = 0;
1963 for (i = 0; i < chain_nr; i++) {
1964 if (chain->ips[i] == PERF_CONTEXT_USER)
1968 /* LBR only affects the user callchain */
1969 if (i != chain_nr) {
1970 struct branch_stack *lbr_stack = sample->branch_stack;
1971 int lbr_nr = lbr_stack->nr, j, k;
1973 struct branch_flags *flags;
1975 * LBR callstack can only get user call chain.
1976 * The mix_chain_nr is kernel call chain
1977 * number plus LBR user call chain number.
1978 * i is kernel call chain number,
1979 * 1 is PERF_CONTEXT_USER,
1980 * lbr_nr + 1 is the user call chain number.
1981 * For details, please refer to the comments
1982 * in callchain__printf
1984 int mix_chain_nr = i + 1 + lbr_nr + 1;
1986 for (j = 0; j < mix_chain_nr; j++) {
1991 if (callchain_param.order == ORDER_CALLEE) {
1994 else if (j > i + 1) {
1996 ip = lbr_stack->entries[k].from;
1998 flags = &lbr_stack->entries[k].flags;
2000 ip = lbr_stack->entries[0].to;
2002 flags = &lbr_stack->entries[0].flags;
2004 lbr_stack->entries[0].from;
2009 ip = lbr_stack->entries[k].from;
2011 flags = &lbr_stack->entries[k].flags;
2013 else if (j > lbr_nr)
2014 ip = chain->ips[i + 1 - (j - lbr_nr)];
2016 ip = lbr_stack->entries[0].to;
2018 flags = &lbr_stack->entries[0].flags;
2020 lbr_stack->entries[0].from;
2024 err = add_callchain_ip(thread, cursor, parent,
2025 root_al, &cpumode, ip,
2026 branch, flags, NULL,
2029 return (err < 0) ? err : 0;
2037 static int thread__resolve_callchain_sample(struct thread *thread,
2038 struct callchain_cursor *cursor,
2039 struct perf_evsel *evsel,
2040 struct perf_sample *sample,
2041 struct symbol **parent,
2042 struct addr_location *root_al,
2045 struct branch_stack *branch = sample->branch_stack;
2046 struct ip_callchain *chain = sample->callchain;
2048 u8 cpumode = PERF_RECORD_MISC_USER;
2049 int i, j, err, nr_entries;
2054 chain_nr = chain->nr;
2056 if (perf_evsel__has_branch_callstack(evsel)) {
2057 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2058 root_al, max_stack);
2060 return (err < 0) ? err : 0;
2064 * Based on DWARF debug information, some architectures skip
2065 * a callchain entry saved by the kernel.
2067 skip_idx = arch_skip_callchain_idx(thread, chain);
2070 * Add branches to call stack for easier browsing. This gives
2071 * more context for a sample than just the callers.
2073 * This uses individual histograms of paths compared to the
2074 * aggregated histograms the normal LBR mode uses.
2076 * Limitations for now:
2077 * - No extra filters
2078 * - No annotations (should annotate somehow)
2081 if (branch && callchain_param.branch_callstack) {
2082 int nr = min(max_stack, (int)branch->nr);
2083 struct branch_entry be[nr];
2084 struct iterations iter[nr];
2086 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2087 pr_warning("corrupted branch chain. skipping...\n");
2091 for (i = 0; i < nr; i++) {
2092 if (callchain_param.order == ORDER_CALLEE) {
2093 be[i] = branch->entries[i];
2099 * Check for overlap into the callchain.
2100 * The return address is one off compared to
2101 * the branch entry. To adjust for this
2102 * assume the calling instruction is not longer
2105 if (i == skip_idx ||
2106 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2108 else if (be[i].from < chain->ips[first_call] &&
2109 be[i].from >= chain->ips[first_call] - 8)
2112 be[i] = branch->entries[branch->nr - i - 1];
2115 memset(iter, 0, sizeof(struct iterations) * nr);
2116 nr = remove_loops(be, nr, iter);
2118 for (i = 0; i < nr; i++) {
2119 err = add_callchain_ip(thread, cursor, parent,
2126 err = add_callchain_ip(thread, cursor, parent, root_al,
2143 for (i = first_call, nr_entries = 0;
2144 i < chain_nr && nr_entries < max_stack; i++) {
2147 if (callchain_param.order == ORDER_CALLEE)
2150 j = chain->nr - i - 1;
2152 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2158 if (ip < PERF_CONTEXT_MAX)
2161 err = add_callchain_ip(thread, cursor, parent,
2162 root_al, &cpumode, ip,
2163 false, NULL, NULL, 0);
2166 return (err < 0) ? err : 0;
2172 static int unwind_entry(struct unwind_entry *entry, void *arg)
2174 struct callchain_cursor *cursor = arg;
2176 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2178 return callchain_cursor_append(cursor, entry->ip,
2179 entry->map, entry->sym,
2180 false, NULL, 0, 0, 0);
2183 static int thread__resolve_callchain_unwind(struct thread *thread,
2184 struct callchain_cursor *cursor,
2185 struct perf_evsel *evsel,
2186 struct perf_sample *sample,
2189 /* Can we do dwarf post unwind? */
2190 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2191 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2194 /* Bail out if nothing was captured. */
2195 if ((!sample->user_regs.regs) ||
2196 (!sample->user_stack.size))
2199 return unwind__get_entries(unwind_entry, cursor,
2200 thread, sample, max_stack);
2203 int thread__resolve_callchain(struct thread *thread,
2204 struct callchain_cursor *cursor,
2205 struct perf_evsel *evsel,
2206 struct perf_sample *sample,
2207 struct symbol **parent,
2208 struct addr_location *root_al,
2213 callchain_cursor_reset(&callchain_cursor);
2215 if (callchain_param.order == ORDER_CALLEE) {
2216 ret = thread__resolve_callchain_sample(thread, cursor,
2222 ret = thread__resolve_callchain_unwind(thread, cursor,
2226 ret = thread__resolve_callchain_unwind(thread, cursor,
2231 ret = thread__resolve_callchain_sample(thread, cursor,
2240 int machine__for_each_thread(struct machine *machine,
2241 int (*fn)(struct thread *thread, void *p),
2245 struct thread *thread;
2248 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
2249 thread = rb_entry(nd, struct thread, rb_node);
2250 rc = fn(thread, priv);
2255 list_for_each_entry(thread, &machine->dead_threads, node) {
2256 rc = fn(thread, priv);
2263 int machines__for_each_thread(struct machines *machines,
2264 int (*fn)(struct thread *thread, void *p),
2270 rc = machine__for_each_thread(&machines->host, fn, priv);
2274 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2275 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2277 rc = machine__for_each_thread(machine, fn, priv);
2284 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
2285 struct target *target, struct thread_map *threads,
2286 perf_event__handler_t process, bool data_mmap,
2287 unsigned int proc_map_timeout)
2289 if (target__has_task(target))
2290 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
2291 else if (target__has_cpu(target))
2292 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
2293 /* command specified */
2297 pid_t machine__get_current_tid(struct machine *machine, int cpu)
2299 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2302 return machine->current_tid[cpu];
2305 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2308 struct thread *thread;
2313 if (!machine->current_tid) {
2316 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2317 if (!machine->current_tid)
2319 for (i = 0; i < MAX_NR_CPUS; i++)
2320 machine->current_tid[i] = -1;
2323 if (cpu >= MAX_NR_CPUS) {
2324 pr_err("Requested CPU %d too large. ", cpu);
2325 pr_err("Consider raising MAX_NR_CPUS\n");
2329 machine->current_tid[cpu] = tid;
2331 thread = machine__findnew_thread(machine, pid, tid);
2336 thread__put(thread);
2342 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2343 * normalized arch is needed.
2345 bool machine__is(struct machine *machine, const char *arch)
2347 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2350 int machine__nr_cpus_avail(struct machine *machine)
2352 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2355 int machine__get_kernel_start(struct machine *machine)
2357 struct map *map = machine__kernel_map(machine);
2361 * The only addresses above 2^63 are kernel addresses of a 64-bit
2362 * kernel. Note that addresses are unsigned so that on a 32-bit system
2363 * all addresses including kernel addresses are less than 2^32. In
2364 * that case (32-bit system), if the kernel mapping is unknown, all
2365 * addresses will be assumed to be in user space - see
2366 * machine__kernel_ip().
2368 machine->kernel_start = 1ULL << 63;
2370 err = map__load(map);
2372 * On x86_64, PTI entry trampolines are less than the
2373 * start of kernel text, but still above 2^63. So leave
2374 * kernel_start = 1ULL << 63 for x86_64.
2376 if (!err && !machine__is(machine, "x86_64"))
2377 machine->kernel_start = map->start;
2382 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2384 return dsos__findnew(&machine->dsos, filename);
2387 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2389 struct machine *machine = vmachine;
2391 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
2396 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2397 *addrp = map->unmap_ip(map, sym->start);