13 #include <symbol/kallsyms.h>
16 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
18 map_groups__init(&machine->kmaps);
19 RB_CLEAR_NODE(&machine->rb_node);
20 INIT_LIST_HEAD(&machine->user_dsos.head);
21 INIT_LIST_HEAD(&machine->kernel_dsos.head);
23 machine->threads = RB_ROOT;
24 INIT_LIST_HEAD(&machine->dead_threads);
25 machine->last_match = NULL;
27 machine->vdso_info = NULL;
29 machine->kmaps.machine = machine;
32 machine->symbol_filter = NULL;
33 machine->id_hdr_size = 0;
34 machine->comm_exec = false;
35 machine->kernel_start = 0;
37 machine->root_dir = strdup(root_dir);
38 if (machine->root_dir == NULL)
41 if (pid != HOST_KERNEL_ID) {
42 struct thread *thread = machine__findnew_thread(machine, -1,
49 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
50 thread__set_comm(thread, comm, 0);
53 machine->current_tid = NULL;
58 struct machine *machine__new_host(void)
60 struct machine *machine = malloc(sizeof(*machine));
62 if (machine != NULL) {
63 machine__init(machine, "", HOST_KERNEL_ID);
65 if (machine__create_kernel_maps(machine) < 0)
75 static void dsos__delete(struct dsos *dsos)
79 list_for_each_entry_safe(pos, n, &dsos->head, node) {
80 RB_CLEAR_NODE(&pos->rb_node);
86 void machine__delete_dead_threads(struct machine *machine)
90 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
96 void machine__delete_threads(struct machine *machine)
98 struct rb_node *nd = rb_first(&machine->threads);
101 struct thread *t = rb_entry(nd, struct thread, rb_node);
103 rb_erase(&t->rb_node, &machine->threads);
109 void machine__exit(struct machine *machine)
111 map_groups__exit(&machine->kmaps);
112 dsos__delete(&machine->user_dsos);
113 dsos__delete(&machine->kernel_dsos);
115 zfree(&machine->root_dir);
116 zfree(&machine->current_tid);
119 void machine__delete(struct machine *machine)
121 machine__exit(machine);
125 void machines__init(struct machines *machines)
127 machine__init(&machines->host, "", HOST_KERNEL_ID);
128 machines->guests = RB_ROOT;
129 machines->symbol_filter = NULL;
132 void machines__exit(struct machines *machines)
134 machine__exit(&machines->host);
138 struct machine *machines__add(struct machines *machines, pid_t pid,
139 const char *root_dir)
141 struct rb_node **p = &machines->guests.rb_node;
142 struct rb_node *parent = NULL;
143 struct machine *pos, *machine = malloc(sizeof(*machine));
148 if (machine__init(machine, root_dir, pid) != 0) {
153 machine->symbol_filter = machines->symbol_filter;
157 pos = rb_entry(parent, struct machine, rb_node);
164 rb_link_node(&machine->rb_node, parent, p);
165 rb_insert_color(&machine->rb_node, &machines->guests);
170 void machines__set_symbol_filter(struct machines *machines,
171 symbol_filter_t symbol_filter)
175 machines->symbol_filter = symbol_filter;
176 machines->host.symbol_filter = symbol_filter;
178 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
179 struct machine *machine = rb_entry(nd, struct machine, rb_node);
181 machine->symbol_filter = symbol_filter;
185 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
189 machines->host.comm_exec = comm_exec;
191 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
192 struct machine *machine = rb_entry(nd, struct machine, rb_node);
194 machine->comm_exec = comm_exec;
198 struct machine *machines__find(struct machines *machines, pid_t pid)
200 struct rb_node **p = &machines->guests.rb_node;
201 struct rb_node *parent = NULL;
202 struct machine *machine;
203 struct machine *default_machine = NULL;
205 if (pid == HOST_KERNEL_ID)
206 return &machines->host;
210 machine = rb_entry(parent, struct machine, rb_node);
211 if (pid < machine->pid)
213 else if (pid > machine->pid)
218 default_machine = machine;
221 return default_machine;
224 struct machine *machines__findnew(struct machines *machines, pid_t pid)
227 const char *root_dir = "";
228 struct machine *machine = machines__find(machines, pid);
230 if (machine && (machine->pid == pid))
233 if ((pid != HOST_KERNEL_ID) &&
234 (pid != DEFAULT_GUEST_KERNEL_ID) &&
235 (symbol_conf.guestmount)) {
236 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
237 if (access(path, R_OK)) {
238 static struct strlist *seen;
241 seen = strlist__new(true, NULL);
243 if (!strlist__has_entry(seen, path)) {
244 pr_err("Can't access file %s\n", path);
245 strlist__add(seen, path);
253 machine = machines__add(machines, pid, root_dir);
258 void machines__process_guests(struct machines *machines,
259 machine__process_t process, void *data)
263 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
264 struct machine *pos = rb_entry(nd, struct machine, rb_node);
269 char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
271 if (machine__is_host(machine))
272 snprintf(bf, size, "[%s]", "kernel.kallsyms");
273 else if (machine__is_default_guest(machine))
274 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
276 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
283 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
285 struct rb_node *node;
286 struct machine *machine;
288 machines->host.id_hdr_size = id_hdr_size;
290 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
291 machine = rb_entry(node, struct machine, rb_node);
292 machine->id_hdr_size = id_hdr_size;
298 static void machine__update_thread_pid(struct machine *machine,
299 struct thread *th, pid_t pid)
301 struct thread *leader;
303 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
308 if (th->pid_ == th->tid)
311 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
316 leader->mg = map_groups__new();
321 if (th->mg == leader->mg)
326 * Maps are created from MMAP events which provide the pid and
327 * tid. Consequently there never should be any maps on a thread
328 * with an unknown pid. Just print an error if there are.
330 if (!map_groups__empty(th->mg))
331 pr_err("Discarding thread maps for %d:%d\n",
333 map_groups__delete(th->mg);
336 th->mg = map_groups__get(leader->mg);
341 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
344 static struct thread *__machine__findnew_thread(struct machine *machine,
345 pid_t pid, pid_t tid,
348 struct rb_node **p = &machine->threads.rb_node;
349 struct rb_node *parent = NULL;
353 * Front-end cache - TID lookups come in blocks,
354 * so most of the time we dont have to look up
357 th = machine->last_match;
358 if (th && th->tid == tid) {
359 machine__update_thread_pid(machine, th, pid);
365 th = rb_entry(parent, struct thread, rb_node);
367 if (th->tid == tid) {
368 machine->last_match = th;
369 machine__update_thread_pid(machine, th, pid);
382 th = thread__new(pid, tid);
384 rb_link_node(&th->rb_node, parent, p);
385 rb_insert_color(&th->rb_node, &machine->threads);
386 machine->last_match = th;
389 * We have to initialize map_groups separately
390 * after rb tree is updated.
392 * The reason is that we call machine__findnew_thread
393 * within thread__init_map_groups to find the thread
394 * leader and that would screwed the rb tree.
396 if (thread__init_map_groups(th, machine)) {
405 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
408 return __machine__findnew_thread(machine, pid, tid, true);
411 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
414 return __machine__findnew_thread(machine, pid, tid, false);
417 struct comm *machine__thread_exec_comm(struct machine *machine,
418 struct thread *thread)
420 if (machine->comm_exec)
421 return thread__exec_comm(thread);
423 return thread__comm(thread);
426 int machine__process_comm_event(struct machine *machine, union perf_event *event,
427 struct perf_sample *sample)
429 struct thread *thread = machine__findnew_thread(machine,
432 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
435 machine->comm_exec = true;
438 perf_event__fprintf_comm(event, stdout);
440 if (thread == NULL ||
441 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
442 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
449 int machine__process_lost_event(struct machine *machine __maybe_unused,
450 union perf_event *event, struct perf_sample *sample __maybe_unused)
452 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
453 event->lost.id, event->lost.lost);
457 struct map *machine__new_module(struct machine *machine, u64 start,
458 const char *filename)
461 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
466 map = map__new2(start, dso, MAP__FUNCTION);
470 if (machine__is_host(machine))
471 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
473 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
474 map_groups__insert(&machine->kmaps, map);
478 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
481 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos.head, fp) +
482 __dsos__fprintf(&machines->host.user_dsos.head, fp);
484 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
485 struct machine *pos = rb_entry(nd, struct machine, rb_node);
486 ret += __dsos__fprintf(&pos->kernel_dsos.head, fp);
487 ret += __dsos__fprintf(&pos->user_dsos.head, fp);
493 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
494 bool (skip)(struct dso *dso, int parm), int parm)
496 return __dsos__fprintf_buildid(&m->kernel_dsos.head, fp, skip, parm) +
497 __dsos__fprintf_buildid(&m->user_dsos.head, fp, skip, parm);
500 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
501 bool (skip)(struct dso *dso, int parm), int parm)
504 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
506 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
507 struct machine *pos = rb_entry(nd, struct machine, rb_node);
508 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
513 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
517 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
519 if (kdso->has_build_id) {
520 char filename[PATH_MAX];
521 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
522 printed += fprintf(fp, "[0] %s\n", filename);
525 for (i = 0; i < vmlinux_path__nr_entries; ++i)
526 printed += fprintf(fp, "[%d] %s\n",
527 i + kdso->has_build_id, vmlinux_path[i]);
532 size_t machine__fprintf(struct machine *machine, FILE *fp)
537 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
538 struct thread *pos = rb_entry(nd, struct thread, rb_node);
540 ret += thread__fprintf(pos, fp);
546 static struct dso *machine__get_kernel(struct machine *machine)
548 const char *vmlinux_name = NULL;
551 if (machine__is_host(machine)) {
552 vmlinux_name = symbol_conf.vmlinux_name;
554 vmlinux_name = "[kernel.kallsyms]";
556 kernel = dso__kernel_findnew(machine, vmlinux_name,
562 if (machine__is_default_guest(machine))
563 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
565 vmlinux_name = machine__mmap_name(machine, bf,
568 kernel = dso__kernel_findnew(machine, vmlinux_name,
570 DSO_TYPE_GUEST_KERNEL);
573 if (kernel != NULL && (!kernel->has_build_id))
574 dso__read_running_kernel_build_id(kernel, machine);
579 struct process_args {
583 static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
586 if (machine__is_default_guest(machine))
587 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
589 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
592 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
594 /* Figure out the start address of kernel map from /proc/kallsyms.
595 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
596 * symbol_name if it's not that important.
598 static u64 machine__get_running_kernel_start(struct machine *machine,
599 const char **symbol_name)
601 char filename[PATH_MAX];
606 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
608 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
611 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
612 addr = kallsyms__get_function_start(filename, name);
623 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
626 u64 start = machine__get_running_kernel_start(machine, NULL);
628 for (type = 0; type < MAP__NR_TYPES; ++type) {
631 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
632 if (machine->vmlinux_maps[type] == NULL)
635 machine->vmlinux_maps[type]->map_ip =
636 machine->vmlinux_maps[type]->unmap_ip =
638 kmap = map__kmap(machine->vmlinux_maps[type]);
639 kmap->kmaps = &machine->kmaps;
640 map_groups__insert(&machine->kmaps,
641 machine->vmlinux_maps[type]);
647 void machine__destroy_kernel_maps(struct machine *machine)
651 for (type = 0; type < MAP__NR_TYPES; ++type) {
654 if (machine->vmlinux_maps[type] == NULL)
657 kmap = map__kmap(machine->vmlinux_maps[type]);
658 map_groups__remove(&machine->kmaps,
659 machine->vmlinux_maps[type]);
660 if (kmap->ref_reloc_sym) {
662 * ref_reloc_sym is shared among all maps, so free just
665 if (type == MAP__FUNCTION) {
666 zfree((char **)&kmap->ref_reloc_sym->name);
667 zfree(&kmap->ref_reloc_sym);
669 kmap->ref_reloc_sym = NULL;
672 map__delete(machine->vmlinux_maps[type]);
673 machine->vmlinux_maps[type] = NULL;
677 int machines__create_guest_kernel_maps(struct machines *machines)
680 struct dirent **namelist = NULL;
686 if (symbol_conf.default_guest_vmlinux_name ||
687 symbol_conf.default_guest_modules ||
688 symbol_conf.default_guest_kallsyms) {
689 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
692 if (symbol_conf.guestmount) {
693 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
696 for (i = 0; i < items; i++) {
697 if (!isdigit(namelist[i]->d_name[0])) {
698 /* Filter out . and .. */
701 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
702 if ((*endp != '\0') ||
703 (endp == namelist[i]->d_name) ||
705 pr_debug("invalid directory (%s). Skipping.\n",
706 namelist[i]->d_name);
709 sprintf(path, "%s/%s/proc/kallsyms",
710 symbol_conf.guestmount,
711 namelist[i]->d_name);
712 ret = access(path, R_OK);
714 pr_debug("Can't access file %s\n", path);
717 machines__create_kernel_maps(machines, pid);
726 void machines__destroy_kernel_maps(struct machines *machines)
728 struct rb_node *next = rb_first(&machines->guests);
730 machine__destroy_kernel_maps(&machines->host);
733 struct machine *pos = rb_entry(next, struct machine, rb_node);
735 next = rb_next(&pos->rb_node);
736 rb_erase(&pos->rb_node, &machines->guests);
737 machine__delete(pos);
741 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
743 struct machine *machine = machines__findnew(machines, pid);
748 return machine__create_kernel_maps(machine);
751 int machine__load_kallsyms(struct machine *machine, const char *filename,
752 enum map_type type, symbol_filter_t filter)
754 struct map *map = machine->vmlinux_maps[type];
755 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
758 dso__set_loaded(map->dso, type);
760 * Since /proc/kallsyms will have multiple sessions for the
761 * kernel, with modules between them, fixup the end of all
764 __map_groups__fixup_end(&machine->kmaps, type);
770 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
771 symbol_filter_t filter)
773 struct map *map = machine->vmlinux_maps[type];
774 int ret = dso__load_vmlinux_path(map->dso, map, filter);
777 dso__set_loaded(map->dso, type);
782 static void map_groups__fixup_end(struct map_groups *mg)
785 for (i = 0; i < MAP__NR_TYPES; ++i)
786 __map_groups__fixup_end(mg, i);
789 static char *get_kernel_version(const char *root_dir)
791 char version[PATH_MAX];
794 const char *prefix = "Linux version ";
796 sprintf(version, "%s/proc/version", root_dir);
797 file = fopen(version, "r");
802 tmp = fgets(version, sizeof(version), file);
805 name = strstr(version, prefix);
808 name += strlen(prefix);
809 tmp = strchr(name, ' ');
816 static int map_groups__set_modules_path_dir(struct map_groups *mg,
817 const char *dir_name, int depth)
820 DIR *dir = opendir(dir_name);
824 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
828 while ((dent = readdir(dir)) != NULL) {
832 /*sshfs might return bad dent->d_type, so we have to stat*/
833 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
837 if (S_ISDIR(st.st_mode)) {
838 if (!strcmp(dent->d_name, ".") ||
839 !strcmp(dent->d_name, ".."))
842 /* Do not follow top-level source and build symlinks */
844 if (!strcmp(dent->d_name, "source") ||
845 !strcmp(dent->d_name, "build"))
849 ret = map_groups__set_modules_path_dir(mg, path,
854 char *dot = strrchr(dent->d_name, '.'),
859 if (dot == NULL || strcmp(dot, ".ko"))
861 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
862 (int)(dot - dent->d_name), dent->d_name);
864 strxfrchar(dso_name, '-', '_');
865 map = map_groups__find_by_name(mg, MAP__FUNCTION,
870 long_name = strdup(path);
871 if (long_name == NULL) {
875 dso__set_long_name(map->dso, long_name, true);
876 dso__kernel_module_get_build_id(map->dso, "");
885 static int machine__set_modules_path(struct machine *machine)
888 char modules_path[PATH_MAX];
890 version = get_kernel_version(machine->root_dir);
894 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
895 machine->root_dir, version);
898 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
901 static int machine__create_module(void *arg, const char *name, u64 start)
903 struct machine *machine = arg;
906 map = machine__new_module(machine, start, name);
910 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
915 static int machine__create_modules(struct machine *machine)
920 if (machine__is_default_guest(machine)) {
921 modules = symbol_conf.default_guest_modules;
923 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
927 if (symbol__restricted_filename(modules, "/proc/modules"))
930 if (modules__parse(modules, machine, machine__create_module))
933 if (!machine__set_modules_path(machine))
936 pr_debug("Problems setting modules path maps, continuing anyway...\n");
941 int machine__create_kernel_maps(struct machine *machine)
943 struct dso *kernel = machine__get_kernel(machine);
945 u64 addr = machine__get_running_kernel_start(machine, &name);
949 if (kernel == NULL ||
950 __machine__create_kernel_maps(machine, kernel) < 0)
953 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
954 if (machine__is_host(machine))
955 pr_debug("Problems creating module maps, "
956 "continuing anyway...\n");
958 pr_debug("Problems creating module maps for guest %d, "
959 "continuing anyway...\n", machine->pid);
963 * Now that we have all the maps created, just set the ->end of them:
965 map_groups__fixup_end(&machine->kmaps);
967 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
969 machine__destroy_kernel_maps(machine);
976 static void machine__set_kernel_mmap_len(struct machine *machine,
977 union perf_event *event)
981 for (i = 0; i < MAP__NR_TYPES; i++) {
982 machine->vmlinux_maps[i]->start = event->mmap.start;
983 machine->vmlinux_maps[i]->end = (event->mmap.start +
986 * Be a bit paranoid here, some perf.data file came with
987 * a zero sized synthesized MMAP event for the kernel.
989 if (machine->vmlinux_maps[i]->end == 0)
990 machine->vmlinux_maps[i]->end = ~0ULL;
994 static bool machine__uses_kcore(struct machine *machine)
998 list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
999 if (dso__is_kcore(dso))
1006 static int machine__process_kernel_mmap_event(struct machine *machine,
1007 union perf_event *event)
1010 char kmmap_prefix[PATH_MAX];
1011 enum dso_kernel_type kernel_type;
1012 bool is_kernel_mmap;
1014 /* If we have maps from kcore then we do not need or want any others */
1015 if (machine__uses_kcore(machine))
1018 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1019 if (machine__is_host(machine))
1020 kernel_type = DSO_TYPE_KERNEL;
1022 kernel_type = DSO_TYPE_GUEST_KERNEL;
1024 is_kernel_mmap = memcmp(event->mmap.filename,
1026 strlen(kmmap_prefix) - 1) == 0;
1027 if (event->mmap.filename[0] == '/' ||
1028 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1030 char short_module_name[1024];
1033 if (event->mmap.filename[0] == '/') {
1034 name = strrchr(event->mmap.filename, '/');
1038 ++name; /* skip / */
1039 dot = strrchr(name, '.');
1042 snprintf(short_module_name, sizeof(short_module_name),
1043 "[%.*s]", (int)(dot - name), name);
1044 strxfrchar(short_module_name, '-', '_');
1046 strcpy(short_module_name, event->mmap.filename);
1048 map = machine__new_module(machine, event->mmap.start,
1049 event->mmap.filename);
1053 name = strdup(short_module_name);
1057 dso__set_short_name(map->dso, name, true);
1058 map->end = map->start + event->mmap.len;
1059 } else if (is_kernel_mmap) {
1060 const char *symbol_name = (event->mmap.filename +
1061 strlen(kmmap_prefix));
1063 * Should be there already, from the build-id table in
1066 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1071 kernel->kernel = kernel_type;
1072 if (__machine__create_kernel_maps(machine, kernel) < 0)
1075 machine__set_kernel_mmap_len(machine, event);
1078 * Avoid using a zero address (kptr_restrict) for the ref reloc
1079 * symbol. Effectively having zero here means that at record
1080 * time /proc/sys/kernel/kptr_restrict was non zero.
1082 if (event->mmap.pgoff != 0) {
1083 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1088 if (machine__is_default_guest(machine)) {
1090 * preload dso of guest kernel and modules
1092 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1101 int machine__process_mmap2_event(struct machine *machine,
1102 union perf_event *event,
1103 struct perf_sample *sample __maybe_unused)
1105 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1106 struct thread *thread;
1112 perf_event__fprintf_mmap2(event, stdout);
1114 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1115 cpumode == PERF_RECORD_MISC_KERNEL) {
1116 ret = machine__process_kernel_mmap_event(machine, event);
1122 thread = machine__findnew_thread(machine, event->mmap2.pid,
1127 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1128 type = MAP__VARIABLE;
1130 type = MAP__FUNCTION;
1132 map = map__new(machine, event->mmap2.start,
1133 event->mmap2.len, event->mmap2.pgoff,
1134 event->mmap2.pid, event->mmap2.maj,
1135 event->mmap2.min, event->mmap2.ino,
1136 event->mmap2.ino_generation,
1139 event->mmap2.filename, type, thread);
1144 thread__insert_map(thread, map);
1148 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1152 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1153 struct perf_sample *sample __maybe_unused)
1155 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1156 struct thread *thread;
1162 perf_event__fprintf_mmap(event, stdout);
1164 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1165 cpumode == PERF_RECORD_MISC_KERNEL) {
1166 ret = machine__process_kernel_mmap_event(machine, event);
1172 thread = machine__findnew_thread(machine, event->mmap.pid,
1177 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1178 type = MAP__VARIABLE;
1180 type = MAP__FUNCTION;
1182 map = map__new(machine, event->mmap.start,
1183 event->mmap.len, event->mmap.pgoff,
1184 event->mmap.pid, 0, 0, 0, 0, 0, 0,
1185 event->mmap.filename,
1191 thread__insert_map(thread, map);
1195 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1199 static void machine__remove_thread(struct machine *machine, struct thread *th)
1201 machine->last_match = NULL;
1202 rb_erase(&th->rb_node, &machine->threads);
1204 * We may have references to this thread, for instance in some hist_entry
1205 * instances, so just move them to a separate list.
1207 list_add_tail(&th->node, &machine->dead_threads);
1210 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1211 struct perf_sample *sample)
1213 struct thread *thread = machine__find_thread(machine,
1216 struct thread *parent = machine__findnew_thread(machine,
1220 /* if a thread currently exists for the thread id remove it */
1222 machine__remove_thread(machine, thread);
1224 thread = machine__findnew_thread(machine, event->fork.pid,
1227 perf_event__fprintf_task(event, stdout);
1229 if (thread == NULL || parent == NULL ||
1230 thread__fork(thread, parent, sample->time) < 0) {
1231 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1238 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1239 struct perf_sample *sample __maybe_unused)
1241 struct thread *thread = machine__find_thread(machine,
1246 perf_event__fprintf_task(event, stdout);
1249 thread__exited(thread);
1254 int machine__process_event(struct machine *machine, union perf_event *event,
1255 struct perf_sample *sample)
1259 switch (event->header.type) {
1260 case PERF_RECORD_COMM:
1261 ret = machine__process_comm_event(machine, event, sample); break;
1262 case PERF_RECORD_MMAP:
1263 ret = machine__process_mmap_event(machine, event, sample); break;
1264 case PERF_RECORD_MMAP2:
1265 ret = machine__process_mmap2_event(machine, event, sample); break;
1266 case PERF_RECORD_FORK:
1267 ret = machine__process_fork_event(machine, event, sample); break;
1268 case PERF_RECORD_EXIT:
1269 ret = machine__process_exit_event(machine, event, sample); break;
1270 case PERF_RECORD_LOST:
1271 ret = machine__process_lost_event(machine, event, sample); break;
1280 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1282 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
1287 static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1288 struct addr_map_symbol *ams,
1291 struct addr_location al;
1293 memset(&al, 0, sizeof(al));
1295 * We cannot use the header.misc hint to determine whether a
1296 * branch stack address is user, kernel, guest, hypervisor.
1297 * Branches may straddle the kernel/user/hypervisor boundaries.
1298 * Thus, we have to try consecutively until we find a match
1299 * or else, the symbol is unknown
1301 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
1304 ams->al_addr = al.addr;
1309 static void ip__resolve_data(struct machine *machine, struct thread *thread,
1310 u8 m, struct addr_map_symbol *ams, u64 addr)
1312 struct addr_location al;
1314 memset(&al, 0, sizeof(al));
1316 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1318 if (al.map == NULL) {
1320 * some shared data regions have execute bit set which puts
1321 * their mapping in the MAP__FUNCTION type array.
1322 * Check there as a fallback option before dropping the sample.
1324 thread__find_addr_location(thread, machine, m, MAP__FUNCTION, addr,
1329 ams->al_addr = al.addr;
1334 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1335 struct addr_location *al)
1337 struct mem_info *mi = zalloc(sizeof(*mi));
1342 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1343 ip__resolve_data(al->machine, al->thread, al->cpumode,
1344 &mi->daddr, sample->addr);
1345 mi->data_src.val = sample->data_src;
1350 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1351 struct addr_location *al)
1354 const struct branch_stack *bs = sample->branch_stack;
1355 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
1360 for (i = 0; i < bs->nr; i++) {
1361 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1362 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
1363 bi[i].flags = bs->entries[i].flags;
1368 static int machine__resolve_callchain_sample(struct machine *machine,
1369 struct thread *thread,
1370 struct ip_callchain *chain,
1371 struct symbol **parent,
1372 struct addr_location *root_al,
1375 u8 cpumode = PERF_RECORD_MISC_USER;
1376 int chain_nr = min(max_stack, (int)chain->nr);
1380 int skip_idx __maybe_unused;
1382 callchain_cursor_reset(&callchain_cursor);
1384 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1385 pr_warning("corrupted callchain. skipping...\n");
1390 * Based on DWARF debug information, some architectures skip
1391 * a callchain entry saved by the kernel.
1393 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1395 for (i = 0; i < chain_nr; i++) {
1397 struct addr_location al;
1399 if (callchain_param.order == ORDER_CALLEE)
1402 j = chain->nr - i - 1;
1404 #ifdef HAVE_SKIP_CALLCHAIN_IDX
1410 if (ip >= PERF_CONTEXT_MAX) {
1412 case PERF_CONTEXT_HV:
1413 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1415 case PERF_CONTEXT_KERNEL:
1416 cpumode = PERF_RECORD_MISC_KERNEL;
1418 case PERF_CONTEXT_USER:
1419 cpumode = PERF_RECORD_MISC_USER;
1422 pr_debug("invalid callchain context: "
1423 "%"PRId64"\n", (s64) ip);
1425 * It seems the callchain is corrupted.
1428 callchain_cursor_reset(&callchain_cursor);
1435 thread__find_addr_location(thread, machine, cpumode,
1436 MAP__FUNCTION, ip, &al);
1437 if (al.sym != NULL) {
1438 if (sort__has_parent && !*parent &&
1439 symbol__match_regex(al.sym, &parent_regex))
1441 else if (have_ignore_callees && root_al &&
1442 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1443 /* Treat this symbol as the root,
1444 forgetting its callees. */
1446 callchain_cursor_reset(&callchain_cursor);
1450 err = callchain_cursor_append(&callchain_cursor,
1451 ip, al.map, al.sym);
1459 static int unwind_entry(struct unwind_entry *entry, void *arg)
1461 struct callchain_cursor *cursor = arg;
1462 return callchain_cursor_append(cursor, entry->ip,
1463 entry->map, entry->sym);
1466 int machine__resolve_callchain(struct machine *machine,
1467 struct perf_evsel *evsel,
1468 struct thread *thread,
1469 struct perf_sample *sample,
1470 struct symbol **parent,
1471 struct addr_location *root_al,
1476 ret = machine__resolve_callchain_sample(machine, thread,
1477 sample->callchain, parent,
1478 root_al, max_stack);
1482 /* Can we do dwarf post unwind? */
1483 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1484 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1487 /* Bail out if nothing was captured. */
1488 if ((!sample->user_regs.regs) ||
1489 (!sample->user_stack.size))
1492 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
1493 thread, sample, max_stack);
1497 int machine__for_each_thread(struct machine *machine,
1498 int (*fn)(struct thread *thread, void *p),
1502 struct thread *thread;
1505 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1506 thread = rb_entry(nd, struct thread, rb_node);
1507 rc = fn(thread, priv);
1512 list_for_each_entry(thread, &machine->dead_threads, node) {
1513 rc = fn(thread, priv);
1520 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1521 struct target *target, struct thread_map *threads,
1522 perf_event__handler_t process, bool data_mmap)
1524 if (target__has_task(target))
1525 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
1526 else if (target__has_cpu(target))
1527 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1528 /* command specified */
1532 pid_t machine__get_current_tid(struct machine *machine, int cpu)
1534 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1537 return machine->current_tid[cpu];
1540 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1543 struct thread *thread;
1548 if (!machine->current_tid) {
1551 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1552 if (!machine->current_tid)
1554 for (i = 0; i < MAX_NR_CPUS; i++)
1555 machine->current_tid[i] = -1;
1558 if (cpu >= MAX_NR_CPUS) {
1559 pr_err("Requested CPU %d too large. ", cpu);
1560 pr_err("Consider raising MAX_NR_CPUS\n");
1564 machine->current_tid[cpu] = tid;
1566 thread = machine__findnew_thread(machine, pid, tid);
1575 int machine__get_kernel_start(struct machine *machine)
1577 struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
1581 * The only addresses above 2^63 are kernel addresses of a 64-bit
1582 * kernel. Note that addresses are unsigned so that on a 32-bit system
1583 * all addresses including kernel addresses are less than 2^32. In
1584 * that case (32-bit system), if the kernel mapping is unknown, all
1585 * addresses will be assumed to be in user space - see
1586 * machine__kernel_ip().
1588 machine->kernel_start = 1ULL << 63;
1590 err = map__load(map, machine->symbol_filter);
1592 machine->kernel_start = map->start;