17 #include <sys/types.h>
21 #include "linux/hash.h"
24 #include "sane_ctype.h"
25 #include <symbol/kallsyms.h>
27 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
29 static void dsos__init(struct dsos *dsos)
31 INIT_LIST_HEAD(&dsos->head);
33 pthread_rwlock_init(&dsos->lock, NULL);
36 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
38 memset(machine, 0, sizeof(*machine));
39 map_groups__init(&machine->kmaps, machine);
40 RB_CLEAR_NODE(&machine->rb_node);
41 dsos__init(&machine->dsos);
43 machine->threads = RB_ROOT;
44 pthread_rwlock_init(&machine->threads_lock, NULL);
45 machine->nr_threads = 0;
46 INIT_LIST_HEAD(&machine->dead_threads);
47 machine->last_match = NULL;
49 machine->vdso_info = NULL;
54 machine->id_hdr_size = 0;
55 machine->kptr_restrict_warned = false;
56 machine->comm_exec = false;
57 machine->kernel_start = 0;
59 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
61 machine->root_dir = strdup(root_dir);
62 if (machine->root_dir == NULL)
65 if (pid != HOST_KERNEL_ID) {
66 struct thread *thread = machine__findnew_thread(machine, -1,
73 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
74 thread__set_comm(thread, comm, 0);
78 machine->current_tid = NULL;
83 struct machine *machine__new_host(void)
85 struct machine *machine = malloc(sizeof(*machine));
87 if (machine != NULL) {
88 machine__init(machine, "", HOST_KERNEL_ID);
90 if (machine__create_kernel_maps(machine) < 0)
100 struct machine *machine__new_kallsyms(void)
102 struct machine *machine = machine__new_host();
105 * 1) MAP__FUNCTION will go away when we stop loading separate maps for
106 * functions and data objects.
107 * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
108 * ask for not using the kcore parsing code, once this one is fixed
109 * to create a map per module.
111 if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) {
112 machine__delete(machine);
119 static void dsos__purge(struct dsos *dsos)
123 pthread_rwlock_wrlock(&dsos->lock);
125 list_for_each_entry_safe(pos, n, &dsos->head, node) {
126 RB_CLEAR_NODE(&pos->rb_node);
128 list_del_init(&pos->node);
132 pthread_rwlock_unlock(&dsos->lock);
135 static void dsos__exit(struct dsos *dsos)
138 pthread_rwlock_destroy(&dsos->lock);
141 void machine__delete_threads(struct machine *machine)
145 pthread_rwlock_wrlock(&machine->threads_lock);
146 nd = rb_first(&machine->threads);
148 struct thread *t = rb_entry(nd, struct thread, rb_node);
151 __machine__remove_thread(machine, t, false);
153 pthread_rwlock_unlock(&machine->threads_lock);
156 void machine__exit(struct machine *machine)
158 machine__destroy_kernel_maps(machine);
159 map_groups__exit(&machine->kmaps);
160 dsos__exit(&machine->dsos);
161 machine__exit_vdso(machine);
162 zfree(&machine->root_dir);
163 zfree(&machine->current_tid);
164 pthread_rwlock_destroy(&machine->threads_lock);
167 void machine__delete(struct machine *machine)
170 machine__exit(machine);
175 void machines__init(struct machines *machines)
177 machine__init(&machines->host, "", HOST_KERNEL_ID);
178 machines->guests = RB_ROOT;
181 void machines__exit(struct machines *machines)
183 machine__exit(&machines->host);
187 struct machine *machines__add(struct machines *machines, pid_t pid,
188 const char *root_dir)
190 struct rb_node **p = &machines->guests.rb_node;
191 struct rb_node *parent = NULL;
192 struct machine *pos, *machine = malloc(sizeof(*machine));
197 if (machine__init(machine, root_dir, pid) != 0) {
204 pos = rb_entry(parent, struct machine, rb_node);
211 rb_link_node(&machine->rb_node, parent, p);
212 rb_insert_color(&machine->rb_node, &machines->guests);
217 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
221 machines->host.comm_exec = comm_exec;
223 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
224 struct machine *machine = rb_entry(nd, struct machine, rb_node);
226 machine->comm_exec = comm_exec;
230 struct machine *machines__find(struct machines *machines, pid_t pid)
232 struct rb_node **p = &machines->guests.rb_node;
233 struct rb_node *parent = NULL;
234 struct machine *machine;
235 struct machine *default_machine = NULL;
237 if (pid == HOST_KERNEL_ID)
238 return &machines->host;
242 machine = rb_entry(parent, struct machine, rb_node);
243 if (pid < machine->pid)
245 else if (pid > machine->pid)
250 default_machine = machine;
253 return default_machine;
256 struct machine *machines__findnew(struct machines *machines, pid_t pid)
259 const char *root_dir = "";
260 struct machine *machine = machines__find(machines, pid);
262 if (machine && (machine->pid == pid))
265 if ((pid != HOST_KERNEL_ID) &&
266 (pid != DEFAULT_GUEST_KERNEL_ID) &&
267 (symbol_conf.guestmount)) {
268 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
269 if (access(path, R_OK)) {
270 static struct strlist *seen;
273 seen = strlist__new(NULL, NULL);
275 if (!strlist__has_entry(seen, path)) {
276 pr_err("Can't access file %s\n", path);
277 strlist__add(seen, path);
285 machine = machines__add(machines, pid, root_dir);
290 void machines__process_guests(struct machines *machines,
291 machine__process_t process, void *data)
295 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
296 struct machine *pos = rb_entry(nd, struct machine, rb_node);
301 char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
303 if (machine__is_host(machine))
304 snprintf(bf, size, "[%s]", "kernel.kallsyms");
305 else if (machine__is_default_guest(machine))
306 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
308 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
315 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
317 struct rb_node *node;
318 struct machine *machine;
320 machines->host.id_hdr_size = id_hdr_size;
322 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
323 machine = rb_entry(node, struct machine, rb_node);
324 machine->id_hdr_size = id_hdr_size;
330 static void machine__update_thread_pid(struct machine *machine,
331 struct thread *th, pid_t pid)
333 struct thread *leader;
335 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
340 if (th->pid_ == th->tid)
343 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
348 leader->mg = map_groups__new(machine);
353 if (th->mg == leader->mg)
358 * Maps are created from MMAP events which provide the pid and
359 * tid. Consequently there never should be any maps on a thread
360 * with an unknown pid. Just print an error if there are.
362 if (!map_groups__empty(th->mg))
363 pr_err("Discarding thread maps for %d:%d\n",
365 map_groups__put(th->mg);
368 th->mg = map_groups__get(leader->mg);
373 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
378 * Caller must eventually drop thread->refcnt returned with a successful
379 * lookup/new thread inserted.
381 static struct thread *____machine__findnew_thread(struct machine *machine,
382 pid_t pid, pid_t tid,
385 struct rb_node **p = &machine->threads.rb_node;
386 struct rb_node *parent = NULL;
390 * Front-end cache - TID lookups come in blocks,
391 * so most of the time we dont have to look up
394 th = machine->last_match;
396 if (th->tid == tid) {
397 machine__update_thread_pid(machine, th, pid);
398 return thread__get(th);
401 machine->last_match = NULL;
406 th = rb_entry(parent, struct thread, rb_node);
408 if (th->tid == tid) {
409 machine->last_match = th;
410 machine__update_thread_pid(machine, th, pid);
411 return thread__get(th);
423 th = thread__new(pid, tid);
425 rb_link_node(&th->rb_node, parent, p);
426 rb_insert_color(&th->rb_node, &machine->threads);
429 * We have to initialize map_groups separately
430 * after rb tree is updated.
432 * The reason is that we call machine__findnew_thread
433 * within thread__init_map_groups to find the thread
434 * leader and that would screwed the rb tree.
436 if (thread__init_map_groups(th, machine)) {
437 rb_erase_init(&th->rb_node, &machine->threads);
438 RB_CLEAR_NODE(&th->rb_node);
443 * It is now in the rbtree, get a ref
446 machine->last_match = th;
447 ++machine->nr_threads;
453 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
455 return ____machine__findnew_thread(machine, pid, tid, true);
458 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
463 pthread_rwlock_wrlock(&machine->threads_lock);
464 th = __machine__findnew_thread(machine, pid, tid);
465 pthread_rwlock_unlock(&machine->threads_lock);
469 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
473 pthread_rwlock_rdlock(&machine->threads_lock);
474 th = ____machine__findnew_thread(machine, pid, tid, false);
475 pthread_rwlock_unlock(&machine->threads_lock);
479 struct comm *machine__thread_exec_comm(struct machine *machine,
480 struct thread *thread)
482 if (machine->comm_exec)
483 return thread__exec_comm(thread);
485 return thread__comm(thread);
488 int machine__process_comm_event(struct machine *machine, union perf_event *event,
489 struct perf_sample *sample)
491 struct thread *thread = machine__findnew_thread(machine,
494 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
498 machine->comm_exec = true;
501 perf_event__fprintf_comm(event, stdout);
503 if (thread == NULL ||
504 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
505 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
514 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
515 union perf_event *event,
516 struct perf_sample *sample __maybe_unused)
518 struct thread *thread = machine__findnew_thread(machine,
519 event->namespaces.pid,
520 event->namespaces.tid);
523 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
524 "\nWARNING: kernel seems to support more namespaces than perf"
525 " tool.\nTry updating the perf tool..\n\n");
527 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
528 "\nWARNING: perf tool seems to support more namespaces than"
529 " the kernel.\nTry updating the kernel..\n\n");
532 perf_event__fprintf_namespaces(event, stdout);
534 if (thread == NULL ||
535 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
536 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
545 int machine__process_lost_event(struct machine *machine __maybe_unused,
546 union perf_event *event, struct perf_sample *sample __maybe_unused)
548 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
549 event->lost.id, event->lost.lost);
553 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
554 union perf_event *event, struct perf_sample *sample)
556 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
557 sample->id, event->lost_samples.lost);
561 static struct dso *machine__findnew_module_dso(struct machine *machine,
563 const char *filename)
567 pthread_rwlock_wrlock(&machine->dsos.lock);
569 dso = __dsos__find(&machine->dsos, m->name, true);
571 dso = __dsos__addnew(&machine->dsos, m->name);
575 if (machine__is_host(machine))
576 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
578 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
580 /* _KMODULE_COMP should be next to _KMODULE */
581 if (m->kmod && m->comp)
584 dso__set_short_name(dso, strdup(m->name), true);
585 dso__set_long_name(dso, strdup(filename), true);
590 pthread_rwlock_unlock(&machine->dsos.lock);
594 int machine__process_aux_event(struct machine *machine __maybe_unused,
595 union perf_event *event)
598 perf_event__fprintf_aux(event, stdout);
602 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
603 union perf_event *event)
606 perf_event__fprintf_itrace_start(event, stdout);
610 int machine__process_switch_event(struct machine *machine __maybe_unused,
611 union perf_event *event)
614 perf_event__fprintf_switch(event, stdout);
618 static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
620 const char *dup_filename;
622 if (!filename || !dso || !dso->long_name)
624 if (dso->long_name[0] != '[')
626 if (!strchr(filename, '/'))
629 dup_filename = strdup(filename);
633 dso__set_long_name(dso, dup_filename, true);
636 struct map *machine__findnew_module_map(struct machine *machine, u64 start,
637 const char *filename)
639 struct map *map = NULL;
640 struct dso *dso = NULL;
643 if (kmod_path__parse_name(&m, filename))
646 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
650 * If the map's dso is an offline module, give dso__load()
651 * a chance to find the file path of that module by fixing
654 dso__adjust_kmod_long_name(map->dso, filename);
658 dso = machine__findnew_module_dso(machine, &m, filename);
662 map = map__new2(start, dso, MAP__FUNCTION);
666 map_groups__insert(&machine->kmaps, map);
668 /* Put the map here because map_groups__insert alread got it */
671 /* put the dso here, corresponding to machine__findnew_module_dso */
677 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
680 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
682 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
683 struct machine *pos = rb_entry(nd, struct machine, rb_node);
684 ret += __dsos__fprintf(&pos->dsos.head, fp);
690 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
691 bool (skip)(struct dso *dso, int parm), int parm)
693 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
696 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
697 bool (skip)(struct dso *dso, int parm), int parm)
700 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
702 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
703 struct machine *pos = rb_entry(nd, struct machine, rb_node);
704 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
709 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
713 struct dso *kdso = machine__kernel_map(machine)->dso;
715 if (kdso->has_build_id) {
716 char filename[PATH_MAX];
717 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
718 printed += fprintf(fp, "[0] %s\n", filename);
721 for (i = 0; i < vmlinux_path__nr_entries; ++i)
722 printed += fprintf(fp, "[%d] %s\n",
723 i + kdso->has_build_id, vmlinux_path[i]);
728 size_t machine__fprintf(struct machine *machine, FILE *fp)
733 pthread_rwlock_rdlock(&machine->threads_lock);
735 ret = fprintf(fp, "Threads: %u\n", machine->nr_threads);
737 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
738 struct thread *pos = rb_entry(nd, struct thread, rb_node);
740 ret += thread__fprintf(pos, fp);
743 pthread_rwlock_unlock(&machine->threads_lock);
748 static struct dso *machine__get_kernel(struct machine *machine)
750 const char *vmlinux_name = NULL;
753 if (machine__is_host(machine)) {
754 vmlinux_name = symbol_conf.vmlinux_name;
756 vmlinux_name = DSO__NAME_KALLSYMS;
758 kernel = machine__findnew_kernel(machine, vmlinux_name,
759 "[kernel]", DSO_TYPE_KERNEL);
763 if (machine__is_default_guest(machine))
764 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
766 vmlinux_name = machine__mmap_name(machine, bf,
769 kernel = machine__findnew_kernel(machine, vmlinux_name,
771 DSO_TYPE_GUEST_KERNEL);
774 if (kernel != NULL && (!kernel->has_build_id))
775 dso__read_running_kernel_build_id(kernel, machine);
780 struct process_args {
784 static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
787 if (machine__is_default_guest(machine))
788 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
790 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
793 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
795 /* Figure out the start address of kernel map from /proc/kallsyms.
796 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
797 * symbol_name if it's not that important.
799 static u64 machine__get_running_kernel_start(struct machine *machine,
800 const char **symbol_name)
802 char filename[PATH_MAX];
807 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
809 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
812 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
813 addr = kallsyms__get_function_start(filename, name);
824 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
827 u64 start = machine__get_running_kernel_start(machine, NULL);
829 /* In case of renewal the kernel map, destroy previous one */
830 machine__destroy_kernel_maps(machine);
832 for (type = 0; type < MAP__NR_TYPES; ++type) {
836 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
837 if (machine->vmlinux_maps[type] == NULL)
840 machine->vmlinux_maps[type]->map_ip =
841 machine->vmlinux_maps[type]->unmap_ip =
843 map = __machine__kernel_map(machine, type);
844 kmap = map__kmap(map);
848 kmap->kmaps = &machine->kmaps;
849 map_groups__insert(&machine->kmaps, map);
855 void machine__destroy_kernel_maps(struct machine *machine)
859 for (type = 0; type < MAP__NR_TYPES; ++type) {
861 struct map *map = __machine__kernel_map(machine, type);
866 kmap = map__kmap(map);
867 map_groups__remove(&machine->kmaps, map);
868 if (kmap && kmap->ref_reloc_sym) {
870 * ref_reloc_sym is shared among all maps, so free just
873 if (type == MAP__FUNCTION) {
874 zfree((char **)&kmap->ref_reloc_sym->name);
875 zfree(&kmap->ref_reloc_sym);
877 kmap->ref_reloc_sym = NULL;
880 map__put(machine->vmlinux_maps[type]);
881 machine->vmlinux_maps[type] = NULL;
885 int machines__create_guest_kernel_maps(struct machines *machines)
888 struct dirent **namelist = NULL;
894 if (symbol_conf.default_guest_vmlinux_name ||
895 symbol_conf.default_guest_modules ||
896 symbol_conf.default_guest_kallsyms) {
897 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
900 if (symbol_conf.guestmount) {
901 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
904 for (i = 0; i < items; i++) {
905 if (!isdigit(namelist[i]->d_name[0])) {
906 /* Filter out . and .. */
909 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
910 if ((*endp != '\0') ||
911 (endp == namelist[i]->d_name) ||
913 pr_debug("invalid directory (%s). Skipping.\n",
914 namelist[i]->d_name);
917 sprintf(path, "%s/%s/proc/kallsyms",
918 symbol_conf.guestmount,
919 namelist[i]->d_name);
920 ret = access(path, R_OK);
922 pr_debug("Can't access file %s\n", path);
925 machines__create_kernel_maps(machines, pid);
934 void machines__destroy_kernel_maps(struct machines *machines)
936 struct rb_node *next = rb_first(&machines->guests);
938 machine__destroy_kernel_maps(&machines->host);
941 struct machine *pos = rb_entry(next, struct machine, rb_node);
943 next = rb_next(&pos->rb_node);
944 rb_erase(&pos->rb_node, &machines->guests);
945 machine__delete(pos);
949 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
951 struct machine *machine = machines__findnew(machines, pid);
956 return machine__create_kernel_maps(machine);
959 int __machine__load_kallsyms(struct machine *machine, const char *filename,
960 enum map_type type, bool no_kcore)
962 struct map *map = machine__kernel_map(machine);
963 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore);
966 dso__set_loaded(map->dso, type);
968 * Since /proc/kallsyms will have multiple sessions for the
969 * kernel, with modules between them, fixup the end of all
972 __map_groups__fixup_end(&machine->kmaps, type);
978 int machine__load_kallsyms(struct machine *machine, const char *filename,
981 return __machine__load_kallsyms(machine, filename, type, false);
984 int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
986 struct map *map = machine__kernel_map(machine);
987 int ret = dso__load_vmlinux_path(map->dso, map);
990 dso__set_loaded(map->dso, type);
995 static void map_groups__fixup_end(struct map_groups *mg)
998 for (i = 0; i < MAP__NR_TYPES; ++i)
999 __map_groups__fixup_end(mg, i);
1002 static char *get_kernel_version(const char *root_dir)
1004 char version[PATH_MAX];
1007 const char *prefix = "Linux version ";
1009 sprintf(version, "%s/proc/version", root_dir);
1010 file = fopen(version, "r");
1015 tmp = fgets(version, sizeof(version), file);
1018 name = strstr(version, prefix);
1021 name += strlen(prefix);
1022 tmp = strchr(name, ' ');
1026 return strdup(name);
1029 static bool is_kmod_dso(struct dso *dso)
1031 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1032 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1035 static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1036 struct kmod_path *m)
1041 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1045 long_name = strdup(path);
1046 if (long_name == NULL)
1049 dso__set_long_name(map->dso, long_name, true);
1050 dso__kernel_module_get_build_id(map->dso, "");
1053 * Full name could reveal us kmod compression, so
1054 * we need to update the symtab_type if needed.
1056 if (m->comp && is_kmod_dso(map->dso))
1057 map->dso->symtab_type++;
1062 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1063 const char *dir_name, int depth)
1065 struct dirent *dent;
1066 DIR *dir = opendir(dir_name);
1070 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1074 while ((dent = readdir(dir)) != NULL) {
1075 char path[PATH_MAX];
1078 /*sshfs might return bad dent->d_type, so we have to stat*/
1079 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1080 if (stat(path, &st))
1083 if (S_ISDIR(st.st_mode)) {
1084 if (!strcmp(dent->d_name, ".") ||
1085 !strcmp(dent->d_name, ".."))
1088 /* Do not follow top-level source and build symlinks */
1090 if (!strcmp(dent->d_name, "source") ||
1091 !strcmp(dent->d_name, "build"))
1095 ret = map_groups__set_modules_path_dir(mg, path,
1102 ret = kmod_path__parse_name(&m, dent->d_name);
1107 ret = map_groups__set_module_path(mg, path, &m);
1121 static int machine__set_modules_path(struct machine *machine)
1124 char modules_path[PATH_MAX];
1126 version = get_kernel_version(machine->root_dir);
1130 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1131 machine->root_dir, version);
1134 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
1136 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1137 const char *name __maybe_unused)
1142 static int machine__create_module(void *arg, const char *name, u64 start)
1144 struct machine *machine = arg;
1147 if (arch__fix_module_text_start(&start, name) < 0)
1150 map = machine__findnew_module_map(machine, start, name);
1154 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1159 static int machine__create_modules(struct machine *machine)
1161 const char *modules;
1162 char path[PATH_MAX];
1164 if (machine__is_default_guest(machine)) {
1165 modules = symbol_conf.default_guest_modules;
1167 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1171 if (symbol__restricted_filename(modules, "/proc/modules"))
1174 if (modules__parse(modules, machine, machine__create_module))
1177 if (!machine__set_modules_path(machine))
1180 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1185 int machine__create_kernel_maps(struct machine *machine)
1187 struct dso *kernel = machine__get_kernel(machine);
1195 ret = __machine__create_kernel_maps(machine, kernel);
1200 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1201 if (machine__is_host(machine))
1202 pr_debug("Problems creating module maps, "
1203 "continuing anyway...\n");
1205 pr_debug("Problems creating module maps for guest %d, "
1206 "continuing anyway...\n", machine->pid);
1210 * Now that we have all the maps created, just set the ->end of them:
1212 map_groups__fixup_end(&machine->kmaps);
1214 addr = machine__get_running_kernel_start(machine, &name);
1216 } else if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
1217 machine__destroy_kernel_maps(machine);
1224 static void machine__set_kernel_mmap_len(struct machine *machine,
1225 union perf_event *event)
1229 for (i = 0; i < MAP__NR_TYPES; i++) {
1230 machine->vmlinux_maps[i]->start = event->mmap.start;
1231 machine->vmlinux_maps[i]->end = (event->mmap.start +
1234 * Be a bit paranoid here, some perf.data file came with
1235 * a zero sized synthesized MMAP event for the kernel.
1237 if (machine->vmlinux_maps[i]->end == 0)
1238 machine->vmlinux_maps[i]->end = ~0ULL;
1242 static bool machine__uses_kcore(struct machine *machine)
1246 list_for_each_entry(dso, &machine->dsos.head, node) {
1247 if (dso__is_kcore(dso))
1254 static int machine__process_kernel_mmap_event(struct machine *machine,
1255 union perf_event *event)
1258 char kmmap_prefix[PATH_MAX];
1259 enum dso_kernel_type kernel_type;
1260 bool is_kernel_mmap;
1262 /* If we have maps from kcore then we do not need or want any others */
1263 if (machine__uses_kcore(machine))
1266 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1267 if (machine__is_host(machine))
1268 kernel_type = DSO_TYPE_KERNEL;
1270 kernel_type = DSO_TYPE_GUEST_KERNEL;
1272 is_kernel_mmap = memcmp(event->mmap.filename,
1274 strlen(kmmap_prefix) - 1) == 0;
1275 if (event->mmap.filename[0] == '/' ||
1276 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1277 map = machine__findnew_module_map(machine, event->mmap.start,
1278 event->mmap.filename);
1282 map->end = map->start + event->mmap.len;
1283 } else if (is_kernel_mmap) {
1284 const char *symbol_name = (event->mmap.filename +
1285 strlen(kmmap_prefix));
1287 * Should be there already, from the build-id table in
1290 struct dso *kernel = NULL;
1293 pthread_rwlock_rdlock(&machine->dsos.lock);
1295 list_for_each_entry(dso, &machine->dsos.head, node) {
1298 * The cpumode passed to is_kernel_module is not the
1299 * cpumode of *this* event. If we insist on passing
1300 * correct cpumode to is_kernel_module, we should
1301 * record the cpumode when we adding this dso to the
1304 * However we don't really need passing correct
1305 * cpumode. We know the correct cpumode must be kernel
1306 * mode (if not, we should not link it onto kernel_dsos
1309 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1310 * is_kernel_module() treats it as a kernel cpumode.
1314 is_kernel_module(dso->long_name,
1315 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1323 pthread_rwlock_unlock(&machine->dsos.lock);
1326 kernel = machine__findnew_dso(machine, kmmap_prefix);
1330 kernel->kernel = kernel_type;
1331 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1336 if (strstr(kernel->long_name, "vmlinux"))
1337 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1339 machine__set_kernel_mmap_len(machine, event);
1342 * Avoid using a zero address (kptr_restrict) for the ref reloc
1343 * symbol. Effectively having zero here means that at record
1344 * time /proc/sys/kernel/kptr_restrict was non zero.
1346 if (event->mmap.pgoff != 0) {
1347 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1352 if (machine__is_default_guest(machine)) {
1354 * preload dso of guest kernel and modules
1356 dso__load(kernel, machine__kernel_map(machine));
1364 int machine__process_mmap2_event(struct machine *machine,
1365 union perf_event *event,
1366 struct perf_sample *sample)
1368 struct thread *thread;
1374 perf_event__fprintf_mmap2(event, stdout);
1376 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1377 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1378 ret = machine__process_kernel_mmap_event(machine, event);
1384 thread = machine__findnew_thread(machine, event->mmap2.pid,
1389 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1390 type = MAP__VARIABLE;
1392 type = MAP__FUNCTION;
1394 map = map__new(machine, event->mmap2.start,
1395 event->mmap2.len, event->mmap2.pgoff,
1396 event->mmap2.pid, event->mmap2.maj,
1397 event->mmap2.min, event->mmap2.ino,
1398 event->mmap2.ino_generation,
1401 event->mmap2.filename, type, thread);
1404 goto out_problem_map;
1406 ret = thread__insert_map(thread, map);
1408 goto out_problem_insert;
1410 thread__put(thread);
1417 thread__put(thread);
1419 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1423 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1424 struct perf_sample *sample)
1426 struct thread *thread;
1432 perf_event__fprintf_mmap(event, stdout);
1434 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1435 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1436 ret = machine__process_kernel_mmap_event(machine, event);
1442 thread = machine__findnew_thread(machine, event->mmap.pid,
1447 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1448 type = MAP__VARIABLE;
1450 type = MAP__FUNCTION;
1452 map = map__new(machine, event->mmap.start,
1453 event->mmap.len, event->mmap.pgoff,
1454 event->mmap.pid, 0, 0, 0, 0, 0, 0,
1455 event->mmap.filename,
1459 goto out_problem_map;
1461 ret = thread__insert_map(thread, map);
1463 goto out_problem_insert;
1465 thread__put(thread);
1472 thread__put(thread);
1474 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1478 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1480 if (machine->last_match == th)
1481 machine->last_match = NULL;
1483 BUG_ON(refcount_read(&th->refcnt) == 0);
1485 pthread_rwlock_wrlock(&machine->threads_lock);
1486 rb_erase_init(&th->rb_node, &machine->threads);
1487 RB_CLEAR_NODE(&th->rb_node);
1488 --machine->nr_threads;
1490 * Move it first to the dead_threads list, then drop the reference,
1491 * if this is the last reference, then the thread__delete destructor
1492 * will be called and we will remove it from the dead_threads list.
1494 list_add_tail(&th->node, &machine->dead_threads);
1496 pthread_rwlock_unlock(&machine->threads_lock);
1500 void machine__remove_thread(struct machine *machine, struct thread *th)
1502 return __machine__remove_thread(machine, th, true);
1505 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1506 struct perf_sample *sample)
1508 struct thread *thread = machine__find_thread(machine,
1511 struct thread *parent = machine__findnew_thread(machine,
1517 perf_event__fprintf_task(event, stdout);
1520 * There may be an existing thread that is not actually the parent,
1521 * either because we are processing events out of order, or because the
1522 * (fork) event that would have removed the thread was lost. Assume the
1523 * latter case and continue on as best we can.
1525 if (parent->pid_ != (pid_t)event->fork.ppid) {
1526 dump_printf("removing erroneous parent thread %d/%d\n",
1527 parent->pid_, parent->tid);
1528 machine__remove_thread(machine, parent);
1529 thread__put(parent);
1530 parent = machine__findnew_thread(machine, event->fork.ppid,
1534 /* if a thread currently exists for the thread id remove it */
1535 if (thread != NULL) {
1536 machine__remove_thread(machine, thread);
1537 thread__put(thread);
1540 thread = machine__findnew_thread(machine, event->fork.pid,
1543 if (thread == NULL || parent == NULL ||
1544 thread__fork(thread, parent, sample->time) < 0) {
1545 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1548 thread__put(thread);
1549 thread__put(parent);
1554 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1555 struct perf_sample *sample __maybe_unused)
1557 struct thread *thread = machine__find_thread(machine,
1562 perf_event__fprintf_task(event, stdout);
1564 if (thread != NULL) {
1565 thread__exited(thread);
1566 thread__put(thread);
1572 int machine__process_event(struct machine *machine, union perf_event *event,
1573 struct perf_sample *sample)
1577 switch (event->header.type) {
1578 case PERF_RECORD_COMM:
1579 ret = machine__process_comm_event(machine, event, sample); break;
1580 case PERF_RECORD_MMAP:
1581 ret = machine__process_mmap_event(machine, event, sample); break;
1582 case PERF_RECORD_NAMESPACES:
1583 ret = machine__process_namespaces_event(machine, event, sample); break;
1584 case PERF_RECORD_MMAP2:
1585 ret = machine__process_mmap2_event(machine, event, sample); break;
1586 case PERF_RECORD_FORK:
1587 ret = machine__process_fork_event(machine, event, sample); break;
1588 case PERF_RECORD_EXIT:
1589 ret = machine__process_exit_event(machine, event, sample); break;
1590 case PERF_RECORD_LOST:
1591 ret = machine__process_lost_event(machine, event, sample); break;
1592 case PERF_RECORD_AUX:
1593 ret = machine__process_aux_event(machine, event); break;
1594 case PERF_RECORD_ITRACE_START:
1595 ret = machine__process_itrace_start_event(machine, event); break;
1596 case PERF_RECORD_LOST_SAMPLES:
1597 ret = machine__process_lost_samples_event(machine, event, sample); break;
1598 case PERF_RECORD_SWITCH:
1599 case PERF_RECORD_SWITCH_CPU_WIDE:
1600 ret = machine__process_switch_event(machine, event); break;
1609 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1611 if (!regexec(regex, sym->name, 0, NULL, 0))
1616 static void ip__resolve_ams(struct thread *thread,
1617 struct addr_map_symbol *ams,
1620 struct addr_location al;
1622 memset(&al, 0, sizeof(al));
1624 * We cannot use the header.misc hint to determine whether a
1625 * branch stack address is user, kernel, guest, hypervisor.
1626 * Branches may straddle the kernel/user/hypervisor boundaries.
1627 * Thus, we have to try consecutively until we find a match
1628 * or else, the symbol is unknown
1630 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
1633 ams->al_addr = al.addr;
1638 static void ip__resolve_data(struct thread *thread,
1639 u8 m, struct addr_map_symbol *ams, u64 addr)
1641 struct addr_location al;
1643 memset(&al, 0, sizeof(al));
1645 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
1646 if (al.map == NULL) {
1648 * some shared data regions have execute bit set which puts
1649 * their mapping in the MAP__FUNCTION type array.
1650 * Check there as a fallback option before dropping the sample.
1652 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
1656 ams->al_addr = al.addr;
1661 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1662 struct addr_location *al)
1664 struct mem_info *mi = zalloc(sizeof(*mi));
1669 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1670 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
1671 mi->data_src.val = sample->data_src;
1676 static int add_callchain_ip(struct thread *thread,
1677 struct callchain_cursor *cursor,
1678 struct symbol **parent,
1679 struct addr_location *root_al,
1683 struct branch_flags *flags,
1687 struct addr_location al;
1692 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1695 if (ip >= PERF_CONTEXT_MAX) {
1697 case PERF_CONTEXT_HV:
1698 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
1700 case PERF_CONTEXT_KERNEL:
1701 *cpumode = PERF_RECORD_MISC_KERNEL;
1703 case PERF_CONTEXT_USER:
1704 *cpumode = PERF_RECORD_MISC_USER;
1707 pr_debug("invalid callchain context: "
1708 "%"PRId64"\n", (s64) ip);
1710 * It seems the callchain is corrupted.
1713 callchain_cursor_reset(cursor);
1718 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1722 if (al.sym != NULL) {
1723 if (perf_hpp_list.parent && !*parent &&
1724 symbol__match_regex(al.sym, &parent_regex))
1726 else if (have_ignore_callees && root_al &&
1727 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1728 /* Treat this symbol as the root,
1729 forgetting its callees. */
1731 callchain_cursor_reset(cursor);
1735 if (symbol_conf.hide_unresolved && al.sym == NULL)
1737 return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
1738 branch, flags, nr_loop_iter, samples);
1741 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1742 struct addr_location *al)
1745 const struct branch_stack *bs = sample->branch_stack;
1746 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
1751 for (i = 0; i < bs->nr; i++) {
1752 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1753 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
1754 bi[i].flags = bs->entries[i].flags;
1761 #define NO_ENTRY 0xff
1763 #define PERF_MAX_BRANCH_DEPTH 127
1766 static int remove_loops(struct branch_entry *l, int nr)
1769 unsigned char chash[CHASHSZ];
1771 memset(chash, NO_ENTRY, sizeof(chash));
1773 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1775 for (i = 0; i < nr; i++) {
1776 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1778 /* no collision handling for now */
1779 if (chash[h] == NO_ENTRY) {
1781 } else if (l[chash[h]].from == l[i].from) {
1782 bool is_loop = true;
1783 /* check if it is a real loop */
1785 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1786 if (l[j].from != l[i + off].from) {
1791 memmove(l + i, l + i + off,
1792 (nr - (i + off)) * sizeof(*l));
1801 * Recolve LBR callstack chain sample
1803 * 1 on success get LBR callchain information
1804 * 0 no available LBR callchain information, should try fp
1805 * negative error code on other errors.
1807 static int resolve_lbr_callchain_sample(struct thread *thread,
1808 struct callchain_cursor *cursor,
1809 struct perf_sample *sample,
1810 struct symbol **parent,
1811 struct addr_location *root_al,
1814 struct ip_callchain *chain = sample->callchain;
1815 int chain_nr = min(max_stack, (int)chain->nr), i;
1816 u8 cpumode = PERF_RECORD_MISC_USER;
1819 for (i = 0; i < chain_nr; i++) {
1820 if (chain->ips[i] == PERF_CONTEXT_USER)
1824 /* LBR only affects the user callchain */
1825 if (i != chain_nr) {
1826 struct branch_stack *lbr_stack = sample->branch_stack;
1827 int lbr_nr = lbr_stack->nr, j, k;
1829 struct branch_flags *flags;
1831 * LBR callstack can only get user call chain.
1832 * The mix_chain_nr is kernel call chain
1833 * number plus LBR user call chain number.
1834 * i is kernel call chain number,
1835 * 1 is PERF_CONTEXT_USER,
1836 * lbr_nr + 1 is the user call chain number.
1837 * For details, please refer to the comments
1838 * in callchain__printf
1840 int mix_chain_nr = i + 1 + lbr_nr + 1;
1842 for (j = 0; j < mix_chain_nr; j++) {
1847 if (callchain_param.order == ORDER_CALLEE) {
1850 else if (j > i + 1) {
1852 ip = lbr_stack->entries[k].from;
1854 flags = &lbr_stack->entries[k].flags;
1856 ip = lbr_stack->entries[0].to;
1858 flags = &lbr_stack->entries[0].flags;
1863 ip = lbr_stack->entries[k].from;
1865 flags = &lbr_stack->entries[k].flags;
1867 else if (j > lbr_nr)
1868 ip = chain->ips[i + 1 - (j - lbr_nr)];
1870 ip = lbr_stack->entries[0].to;
1872 flags = &lbr_stack->entries[0].flags;
1876 err = add_callchain_ip(thread, cursor, parent,
1877 root_al, &cpumode, ip,
1878 branch, flags, 0, 0);
1880 return (err < 0) ? err : 0;
1888 static int thread__resolve_callchain_sample(struct thread *thread,
1889 struct callchain_cursor *cursor,
1890 struct perf_evsel *evsel,
1891 struct perf_sample *sample,
1892 struct symbol **parent,
1893 struct addr_location *root_al,
1896 struct branch_stack *branch = sample->branch_stack;
1897 struct ip_callchain *chain = sample->callchain;
1898 int chain_nr = chain->nr;
1899 u8 cpumode = PERF_RECORD_MISC_USER;
1900 int i, j, err, nr_entries;
1905 if (perf_evsel__has_branch_callstack(evsel)) {
1906 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
1907 root_al, max_stack);
1909 return (err < 0) ? err : 0;
1913 * Based on DWARF debug information, some architectures skip
1914 * a callchain entry saved by the kernel.
1916 skip_idx = arch_skip_callchain_idx(thread, chain);
1919 * Add branches to call stack for easier browsing. This gives
1920 * more context for a sample than just the callers.
1922 * This uses individual histograms of paths compared to the
1923 * aggregated histograms the normal LBR mode uses.
1925 * Limitations for now:
1926 * - No extra filters
1927 * - No annotations (should annotate somehow)
1930 if (branch && callchain_param.branch_callstack) {
1931 int nr = min(max_stack, (int)branch->nr);
1932 struct branch_entry be[nr];
1934 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1935 pr_warning("corrupted branch chain. skipping...\n");
1939 for (i = 0; i < nr; i++) {
1940 if (callchain_param.order == ORDER_CALLEE) {
1941 be[i] = branch->entries[i];
1943 * Check for overlap into the callchain.
1944 * The return address is one off compared to
1945 * the branch entry. To adjust for this
1946 * assume the calling instruction is not longer
1949 if (i == skip_idx ||
1950 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1952 else if (be[i].from < chain->ips[first_call] &&
1953 be[i].from >= chain->ips[first_call] - 8)
1956 be[i] = branch->entries[branch->nr - i - 1];
1960 nr = remove_loops(be, nr);
1963 * Get the number of iterations.
1964 * It's only approximation, but good enough in practice.
1966 if (nr_loop_iter > nr)
1967 nr_loop_iter = nr_loop_iter - nr + 1;
1971 for (i = 0; i < nr; i++) {
1973 err = add_callchain_ip(thread, cursor, parent,
1979 err = add_callchain_ip(thread, cursor, parent,
1986 err = add_callchain_ip(thread, cursor, parent, root_al,
1999 for (i = first_call, nr_entries = 0;
2000 i < chain_nr && nr_entries < max_stack; i++) {
2003 if (callchain_param.order == ORDER_CALLEE)
2006 j = chain->nr - i - 1;
2008 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2014 if (ip < PERF_CONTEXT_MAX)
2017 err = add_callchain_ip(thread, cursor, parent,
2018 root_al, &cpumode, ip,
2022 return (err < 0) ? err : 0;
2028 static int unwind_entry(struct unwind_entry *entry, void *arg)
2030 struct callchain_cursor *cursor = arg;
2032 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2034 return callchain_cursor_append(cursor, entry->ip,
2035 entry->map, entry->sym,
2039 static int thread__resolve_callchain_unwind(struct thread *thread,
2040 struct callchain_cursor *cursor,
2041 struct perf_evsel *evsel,
2042 struct perf_sample *sample,
2045 /* Can we do dwarf post unwind? */
2046 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2047 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2050 /* Bail out if nothing was captured. */
2051 if ((!sample->user_regs.regs) ||
2052 (!sample->user_stack.size))
2055 return unwind__get_entries(unwind_entry, cursor,
2056 thread, sample, max_stack);
2059 int thread__resolve_callchain(struct thread *thread,
2060 struct callchain_cursor *cursor,
2061 struct perf_evsel *evsel,
2062 struct perf_sample *sample,
2063 struct symbol **parent,
2064 struct addr_location *root_al,
2069 callchain_cursor_reset(&callchain_cursor);
2071 if (callchain_param.order == ORDER_CALLEE) {
2072 ret = thread__resolve_callchain_sample(thread, cursor,
2078 ret = thread__resolve_callchain_unwind(thread, cursor,
2082 ret = thread__resolve_callchain_unwind(thread, cursor,
2087 ret = thread__resolve_callchain_sample(thread, cursor,
2096 int machine__for_each_thread(struct machine *machine,
2097 int (*fn)(struct thread *thread, void *p),
2101 struct thread *thread;
2104 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
2105 thread = rb_entry(nd, struct thread, rb_node);
2106 rc = fn(thread, priv);
2111 list_for_each_entry(thread, &machine->dead_threads, node) {
2112 rc = fn(thread, priv);
2119 int machines__for_each_thread(struct machines *machines,
2120 int (*fn)(struct thread *thread, void *p),
2126 rc = machine__for_each_thread(&machines->host, fn, priv);
2130 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2131 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2133 rc = machine__for_each_thread(machine, fn, priv);
2140 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
2141 struct target *target, struct thread_map *threads,
2142 perf_event__handler_t process, bool data_mmap,
2143 unsigned int proc_map_timeout)
2145 if (target__has_task(target))
2146 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
2147 else if (target__has_cpu(target))
2148 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
2149 /* command specified */
2153 pid_t machine__get_current_tid(struct machine *machine, int cpu)
2155 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2158 return machine->current_tid[cpu];
2161 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2164 struct thread *thread;
2169 if (!machine->current_tid) {
2172 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2173 if (!machine->current_tid)
2175 for (i = 0; i < MAX_NR_CPUS; i++)
2176 machine->current_tid[i] = -1;
2179 if (cpu >= MAX_NR_CPUS) {
2180 pr_err("Requested CPU %d too large. ", cpu);
2181 pr_err("Consider raising MAX_NR_CPUS\n");
2185 machine->current_tid[cpu] = tid;
2187 thread = machine__findnew_thread(machine, pid, tid);
2192 thread__put(thread);
2197 int machine__get_kernel_start(struct machine *machine)
2199 struct map *map = machine__kernel_map(machine);
2203 * The only addresses above 2^63 are kernel addresses of a 64-bit
2204 * kernel. Note that addresses are unsigned so that on a 32-bit system
2205 * all addresses including kernel addresses are less than 2^32. In
2206 * that case (32-bit system), if the kernel mapping is unknown, all
2207 * addresses will be assumed to be in user space - see
2208 * machine__kernel_ip().
2210 machine->kernel_start = 1ULL << 63;
2212 err = map__load(map);
2214 machine->kernel_start = map->start;
2219 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2221 return dsos__findnew(&machine->dsos, filename);
2224 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2226 struct machine *machine = vmachine;
2228 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
2233 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2234 *addrp = map->unmap_ip(map, sym->start);