12 #include <symbol/kallsyms.h>
15 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
17 map_groups__init(&machine->kmaps);
18 RB_CLEAR_NODE(&machine->rb_node);
19 INIT_LIST_HEAD(&machine->user_dsos);
20 INIT_LIST_HEAD(&machine->kernel_dsos);
22 machine->threads = RB_ROOT;
23 INIT_LIST_HEAD(&machine->dead_threads);
24 machine->last_match = NULL;
26 machine->kmaps.machine = machine;
29 machine->symbol_filter = NULL;
30 machine->id_hdr_size = 0;
32 machine->root_dir = strdup(root_dir);
33 if (machine->root_dir == NULL)
36 if (pid != HOST_KERNEL_ID) {
37 struct thread *thread = machine__findnew_thread(machine, -1,
44 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
45 thread__set_comm(thread, comm, 0);
48 machine->current_tid = NULL;
53 struct machine *machine__new_host(void)
55 struct machine *machine = malloc(sizeof(*machine));
57 if (machine != NULL) {
58 machine__init(machine, "", HOST_KERNEL_ID);
60 if (machine__create_kernel_maps(machine) < 0)
70 static void dsos__delete(struct list_head *dsos)
74 list_for_each_entry_safe(pos, n, dsos, node) {
80 void machine__delete_dead_threads(struct machine *machine)
84 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
90 void machine__delete_threads(struct machine *machine)
92 struct rb_node *nd = rb_first(&machine->threads);
95 struct thread *t = rb_entry(nd, struct thread, rb_node);
97 rb_erase(&t->rb_node, &machine->threads);
103 void machine__exit(struct machine *machine)
105 map_groups__exit(&machine->kmaps);
106 dsos__delete(&machine->user_dsos);
107 dsos__delete(&machine->kernel_dsos);
108 zfree(&machine->root_dir);
109 zfree(&machine->current_tid);
112 void machine__delete(struct machine *machine)
114 machine__exit(machine);
118 void machines__init(struct machines *machines)
120 machine__init(&machines->host, "", HOST_KERNEL_ID);
121 machines->guests = RB_ROOT;
122 machines->symbol_filter = NULL;
125 void machines__exit(struct machines *machines)
127 machine__exit(&machines->host);
131 struct machine *machines__add(struct machines *machines, pid_t pid,
132 const char *root_dir)
134 struct rb_node **p = &machines->guests.rb_node;
135 struct rb_node *parent = NULL;
136 struct machine *pos, *machine = malloc(sizeof(*machine));
141 if (machine__init(machine, root_dir, pid) != 0) {
146 machine->symbol_filter = machines->symbol_filter;
150 pos = rb_entry(parent, struct machine, rb_node);
157 rb_link_node(&machine->rb_node, parent, p);
158 rb_insert_color(&machine->rb_node, &machines->guests);
163 void machines__set_symbol_filter(struct machines *machines,
164 symbol_filter_t symbol_filter)
168 machines->symbol_filter = symbol_filter;
169 machines->host.symbol_filter = symbol_filter;
171 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
172 struct machine *machine = rb_entry(nd, struct machine, rb_node);
174 machine->symbol_filter = symbol_filter;
178 struct machine *machines__find(struct machines *machines, pid_t pid)
180 struct rb_node **p = &machines->guests.rb_node;
181 struct rb_node *parent = NULL;
182 struct machine *machine;
183 struct machine *default_machine = NULL;
185 if (pid == HOST_KERNEL_ID)
186 return &machines->host;
190 machine = rb_entry(parent, struct machine, rb_node);
191 if (pid < machine->pid)
193 else if (pid > machine->pid)
198 default_machine = machine;
201 return default_machine;
204 struct machine *machines__findnew(struct machines *machines, pid_t pid)
207 const char *root_dir = "";
208 struct machine *machine = machines__find(machines, pid);
210 if (machine && (machine->pid == pid))
213 if ((pid != HOST_KERNEL_ID) &&
214 (pid != DEFAULT_GUEST_KERNEL_ID) &&
215 (symbol_conf.guestmount)) {
216 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
217 if (access(path, R_OK)) {
218 static struct strlist *seen;
221 seen = strlist__new(true, NULL);
223 if (!strlist__has_entry(seen, path)) {
224 pr_err("Can't access file %s\n", path);
225 strlist__add(seen, path);
233 machine = machines__add(machines, pid, root_dir);
238 void machines__process_guests(struct machines *machines,
239 machine__process_t process, void *data)
243 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
244 struct machine *pos = rb_entry(nd, struct machine, rb_node);
249 char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
251 if (machine__is_host(machine))
252 snprintf(bf, size, "[%s]", "kernel.kallsyms");
253 else if (machine__is_default_guest(machine))
254 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
256 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
263 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
265 struct rb_node *node;
266 struct machine *machine;
268 machines->host.id_hdr_size = id_hdr_size;
270 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
271 machine = rb_entry(node, struct machine, rb_node);
272 machine->id_hdr_size = id_hdr_size;
278 static void machine__update_thread_pid(struct machine *machine,
279 struct thread *th, pid_t pid)
281 struct thread *leader;
283 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
288 if (th->pid_ == th->tid)
291 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
296 leader->mg = map_groups__new();
301 if (th->mg == leader->mg)
306 * Maps are created from MMAP events which provide the pid and
307 * tid. Consequently there never should be any maps on a thread
308 * with an unknown pid. Just print an error if there are.
310 if (!map_groups__empty(th->mg))
311 pr_err("Discarding thread maps for %d:%d\n",
313 map_groups__delete(th->mg);
316 th->mg = map_groups__get(leader->mg);
321 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
324 static struct thread *__machine__findnew_thread(struct machine *machine,
325 pid_t pid, pid_t tid,
328 struct rb_node **p = &machine->threads.rb_node;
329 struct rb_node *parent = NULL;
333 * Front-end cache - TID lookups come in blocks,
334 * so most of the time we dont have to look up
337 th = machine->last_match;
338 if (th && th->tid == tid) {
339 machine__update_thread_pid(machine, th, pid);
345 th = rb_entry(parent, struct thread, rb_node);
347 if (th->tid == tid) {
348 machine->last_match = th;
349 machine__update_thread_pid(machine, th, pid);
362 th = thread__new(pid, tid);
364 rb_link_node(&th->rb_node, parent, p);
365 rb_insert_color(&th->rb_node, &machine->threads);
366 machine->last_match = th;
369 * We have to initialize map_groups separately
370 * after rb tree is updated.
372 * The reason is that we call machine__findnew_thread
373 * within thread__init_map_groups to find the thread
374 * leader and that would screwed the rb tree.
376 if (thread__init_map_groups(th, machine)) {
385 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
388 return __machine__findnew_thread(machine, pid, tid, true);
391 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
394 return __machine__findnew_thread(machine, pid, tid, false);
397 int machine__process_comm_event(struct machine *machine, union perf_event *event,
398 struct perf_sample *sample)
400 struct thread *thread = machine__findnew_thread(machine,
405 perf_event__fprintf_comm(event, stdout);
407 if (thread == NULL || thread__set_comm(thread, event->comm.comm, sample->time)) {
408 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
415 int machine__process_lost_event(struct machine *machine __maybe_unused,
416 union perf_event *event, struct perf_sample *sample __maybe_unused)
418 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
419 event->lost.id, event->lost.lost);
423 struct map *machine__new_module(struct machine *machine, u64 start,
424 const char *filename)
427 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
432 map = map__new2(start, dso, MAP__FUNCTION);
436 if (machine__is_host(machine))
437 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
439 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
440 map_groups__insert(&machine->kmaps, map);
444 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
447 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
448 __dsos__fprintf(&machines->host.user_dsos, fp);
450 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
451 struct machine *pos = rb_entry(nd, struct machine, rb_node);
452 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
453 ret += __dsos__fprintf(&pos->user_dsos, fp);
459 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
460 bool (skip)(struct dso *dso, int parm), int parm)
462 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
463 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
466 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
467 bool (skip)(struct dso *dso, int parm), int parm)
470 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
472 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
473 struct machine *pos = rb_entry(nd, struct machine, rb_node);
474 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
479 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
483 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
485 if (kdso->has_build_id) {
486 char filename[PATH_MAX];
487 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
488 printed += fprintf(fp, "[0] %s\n", filename);
491 for (i = 0; i < vmlinux_path__nr_entries; ++i)
492 printed += fprintf(fp, "[%d] %s\n",
493 i + kdso->has_build_id, vmlinux_path[i]);
498 size_t machine__fprintf(struct machine *machine, FILE *fp)
503 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
504 struct thread *pos = rb_entry(nd, struct thread, rb_node);
506 ret += thread__fprintf(pos, fp);
512 static struct dso *machine__get_kernel(struct machine *machine)
514 const char *vmlinux_name = NULL;
517 if (machine__is_host(machine)) {
518 vmlinux_name = symbol_conf.vmlinux_name;
520 vmlinux_name = "[kernel.kallsyms]";
522 kernel = dso__kernel_findnew(machine, vmlinux_name,
528 if (machine__is_default_guest(machine))
529 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
531 vmlinux_name = machine__mmap_name(machine, bf,
534 kernel = dso__kernel_findnew(machine, vmlinux_name,
536 DSO_TYPE_GUEST_KERNEL);
539 if (kernel != NULL && (!kernel->has_build_id))
540 dso__read_running_kernel_build_id(kernel, machine);
545 struct process_args {
549 static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
552 if (machine__is_default_guest(machine))
553 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
555 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
558 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
560 /* Figure out the start address of kernel map from /proc/kallsyms.
561 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
562 * symbol_name if it's not that important.
564 static u64 machine__get_kernel_start_addr(struct machine *machine,
565 const char **symbol_name)
567 char filename[PATH_MAX];
572 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
574 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
577 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
578 addr = kallsyms__get_function_start(filename, name);
589 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
592 u64 start = machine__get_kernel_start_addr(machine, NULL);
594 for (type = 0; type < MAP__NR_TYPES; ++type) {
597 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
598 if (machine->vmlinux_maps[type] == NULL)
601 machine->vmlinux_maps[type]->map_ip =
602 machine->vmlinux_maps[type]->unmap_ip =
604 kmap = map__kmap(machine->vmlinux_maps[type]);
605 kmap->kmaps = &machine->kmaps;
606 map_groups__insert(&machine->kmaps,
607 machine->vmlinux_maps[type]);
613 void machine__destroy_kernel_maps(struct machine *machine)
617 for (type = 0; type < MAP__NR_TYPES; ++type) {
620 if (machine->vmlinux_maps[type] == NULL)
623 kmap = map__kmap(machine->vmlinux_maps[type]);
624 map_groups__remove(&machine->kmaps,
625 machine->vmlinux_maps[type]);
626 if (kmap->ref_reloc_sym) {
628 * ref_reloc_sym is shared among all maps, so free just
631 if (type == MAP__FUNCTION) {
632 zfree((char **)&kmap->ref_reloc_sym->name);
633 zfree(&kmap->ref_reloc_sym);
635 kmap->ref_reloc_sym = NULL;
638 map__delete(machine->vmlinux_maps[type]);
639 machine->vmlinux_maps[type] = NULL;
643 int machines__create_guest_kernel_maps(struct machines *machines)
646 struct dirent **namelist = NULL;
652 if (symbol_conf.default_guest_vmlinux_name ||
653 symbol_conf.default_guest_modules ||
654 symbol_conf.default_guest_kallsyms) {
655 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
658 if (symbol_conf.guestmount) {
659 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
662 for (i = 0; i < items; i++) {
663 if (!isdigit(namelist[i]->d_name[0])) {
664 /* Filter out . and .. */
667 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
668 if ((*endp != '\0') ||
669 (endp == namelist[i]->d_name) ||
671 pr_debug("invalid directory (%s). Skipping.\n",
672 namelist[i]->d_name);
675 sprintf(path, "%s/%s/proc/kallsyms",
676 symbol_conf.guestmount,
677 namelist[i]->d_name);
678 ret = access(path, R_OK);
680 pr_debug("Can't access file %s\n", path);
683 machines__create_kernel_maps(machines, pid);
692 void machines__destroy_kernel_maps(struct machines *machines)
694 struct rb_node *next = rb_first(&machines->guests);
696 machine__destroy_kernel_maps(&machines->host);
699 struct machine *pos = rb_entry(next, struct machine, rb_node);
701 next = rb_next(&pos->rb_node);
702 rb_erase(&pos->rb_node, &machines->guests);
703 machine__delete(pos);
707 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
709 struct machine *machine = machines__findnew(machines, pid);
714 return machine__create_kernel_maps(machine);
717 int machine__load_kallsyms(struct machine *machine, const char *filename,
718 enum map_type type, symbol_filter_t filter)
720 struct map *map = machine->vmlinux_maps[type];
721 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
724 dso__set_loaded(map->dso, type);
726 * Since /proc/kallsyms will have multiple sessions for the
727 * kernel, with modules between them, fixup the end of all
730 __map_groups__fixup_end(&machine->kmaps, type);
736 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
737 symbol_filter_t filter)
739 struct map *map = machine->vmlinux_maps[type];
740 int ret = dso__load_vmlinux_path(map->dso, map, filter);
743 dso__set_loaded(map->dso, type);
748 static void map_groups__fixup_end(struct map_groups *mg)
751 for (i = 0; i < MAP__NR_TYPES; ++i)
752 __map_groups__fixup_end(mg, i);
755 static char *get_kernel_version(const char *root_dir)
757 char version[PATH_MAX];
760 const char *prefix = "Linux version ";
762 sprintf(version, "%s/proc/version", root_dir);
763 file = fopen(version, "r");
768 tmp = fgets(version, sizeof(version), file);
771 name = strstr(version, prefix);
774 name += strlen(prefix);
775 tmp = strchr(name, ' ');
782 static int map_groups__set_modules_path_dir(struct map_groups *mg,
783 const char *dir_name, int depth)
786 DIR *dir = opendir(dir_name);
790 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
794 while ((dent = readdir(dir)) != NULL) {
798 /*sshfs might return bad dent->d_type, so we have to stat*/
799 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
803 if (S_ISDIR(st.st_mode)) {
804 if (!strcmp(dent->d_name, ".") ||
805 !strcmp(dent->d_name, ".."))
808 /* Do not follow top-level source and build symlinks */
810 if (!strcmp(dent->d_name, "source") ||
811 !strcmp(dent->d_name, "build"))
815 ret = map_groups__set_modules_path_dir(mg, path,
820 char *dot = strrchr(dent->d_name, '.'),
825 if (dot == NULL || strcmp(dot, ".ko"))
827 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
828 (int)(dot - dent->d_name), dent->d_name);
830 strxfrchar(dso_name, '-', '_');
831 map = map_groups__find_by_name(mg, MAP__FUNCTION,
836 long_name = strdup(path);
837 if (long_name == NULL) {
841 dso__set_long_name(map->dso, long_name, true);
842 dso__kernel_module_get_build_id(map->dso, "");
851 static int machine__set_modules_path(struct machine *machine)
854 char modules_path[PATH_MAX];
856 version = get_kernel_version(machine->root_dir);
860 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
861 machine->root_dir, version);
864 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
867 static int machine__create_module(void *arg, const char *name, u64 start)
869 struct machine *machine = arg;
872 map = machine__new_module(machine, start, name);
876 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
881 static int machine__create_modules(struct machine *machine)
886 if (machine__is_default_guest(machine)) {
887 modules = symbol_conf.default_guest_modules;
889 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
893 if (symbol__restricted_filename(modules, "/proc/modules"))
896 if (modules__parse(modules, machine, machine__create_module))
899 if (!machine__set_modules_path(machine))
902 pr_debug("Problems setting modules path maps, continuing anyway...\n");
907 int machine__create_kernel_maps(struct machine *machine)
909 struct dso *kernel = machine__get_kernel(machine);
911 u64 addr = machine__get_kernel_start_addr(machine, &name);
915 if (kernel == NULL ||
916 __machine__create_kernel_maps(machine, kernel) < 0)
919 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
920 if (machine__is_host(machine))
921 pr_debug("Problems creating module maps, "
922 "continuing anyway...\n");
924 pr_debug("Problems creating module maps for guest %d, "
925 "continuing anyway...\n", machine->pid);
929 * Now that we have all the maps created, just set the ->end of them:
931 map_groups__fixup_end(&machine->kmaps);
933 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
935 machine__destroy_kernel_maps(machine);
942 static void machine__set_kernel_mmap_len(struct machine *machine,
943 union perf_event *event)
947 for (i = 0; i < MAP__NR_TYPES; i++) {
948 machine->vmlinux_maps[i]->start = event->mmap.start;
949 machine->vmlinux_maps[i]->end = (event->mmap.start +
952 * Be a bit paranoid here, some perf.data file came with
953 * a zero sized synthesized MMAP event for the kernel.
955 if (machine->vmlinux_maps[i]->end == 0)
956 machine->vmlinux_maps[i]->end = ~0ULL;
960 static bool machine__uses_kcore(struct machine *machine)
964 list_for_each_entry(dso, &machine->kernel_dsos, node) {
965 if (dso__is_kcore(dso))
972 static int machine__process_kernel_mmap_event(struct machine *machine,
973 union perf_event *event)
976 char kmmap_prefix[PATH_MAX];
977 enum dso_kernel_type kernel_type;
980 /* If we have maps from kcore then we do not need or want any others */
981 if (machine__uses_kcore(machine))
984 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
985 if (machine__is_host(machine))
986 kernel_type = DSO_TYPE_KERNEL;
988 kernel_type = DSO_TYPE_GUEST_KERNEL;
990 is_kernel_mmap = memcmp(event->mmap.filename,
992 strlen(kmmap_prefix) - 1) == 0;
993 if (event->mmap.filename[0] == '/' ||
994 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
996 char short_module_name[1024];
999 if (event->mmap.filename[0] == '/') {
1000 name = strrchr(event->mmap.filename, '/');
1004 ++name; /* skip / */
1005 dot = strrchr(name, '.');
1008 snprintf(short_module_name, sizeof(short_module_name),
1009 "[%.*s]", (int)(dot - name), name);
1010 strxfrchar(short_module_name, '-', '_');
1012 strcpy(short_module_name, event->mmap.filename);
1014 map = machine__new_module(machine, event->mmap.start,
1015 event->mmap.filename);
1019 name = strdup(short_module_name);
1023 dso__set_short_name(map->dso, name, true);
1024 map->end = map->start + event->mmap.len;
1025 } else if (is_kernel_mmap) {
1026 const char *symbol_name = (event->mmap.filename +
1027 strlen(kmmap_prefix));
1029 * Should be there already, from the build-id table in
1032 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1037 kernel->kernel = kernel_type;
1038 if (__machine__create_kernel_maps(machine, kernel) < 0)
1041 machine__set_kernel_mmap_len(machine, event);
1044 * Avoid using a zero address (kptr_restrict) for the ref reloc
1045 * symbol. Effectively having zero here means that at record
1046 * time /proc/sys/kernel/kptr_restrict was non zero.
1048 if (event->mmap.pgoff != 0) {
1049 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1054 if (machine__is_default_guest(machine)) {
1056 * preload dso of guest kernel and modules
1058 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1067 int machine__process_mmap2_event(struct machine *machine,
1068 union perf_event *event,
1069 struct perf_sample *sample __maybe_unused)
1071 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1072 struct thread *thread;
1078 perf_event__fprintf_mmap2(event, stdout);
1080 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1081 cpumode == PERF_RECORD_MISC_KERNEL) {
1082 ret = machine__process_kernel_mmap_event(machine, event);
1088 thread = machine__findnew_thread(machine, event->mmap2.pid,
1093 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1094 type = MAP__VARIABLE;
1096 type = MAP__FUNCTION;
1098 map = map__new(machine, event->mmap2.start,
1099 event->mmap2.len, event->mmap2.pgoff,
1100 event->mmap2.pid, event->mmap2.maj,
1101 event->mmap2.min, event->mmap2.ino,
1102 event->mmap2.ino_generation,
1105 event->mmap2.filename, type);
1110 thread__insert_map(thread, map);
1114 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1118 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1119 struct perf_sample *sample __maybe_unused)
1121 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1122 struct thread *thread;
1128 perf_event__fprintf_mmap(event, stdout);
1130 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1131 cpumode == PERF_RECORD_MISC_KERNEL) {
1132 ret = machine__process_kernel_mmap_event(machine, event);
1138 thread = machine__findnew_thread(machine, event->mmap.pid,
1143 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1144 type = MAP__VARIABLE;
1146 type = MAP__FUNCTION;
1148 map = map__new(machine, event->mmap.start,
1149 event->mmap.len, event->mmap.pgoff,
1150 event->mmap.pid, 0, 0, 0, 0, 0, 0,
1151 event->mmap.filename,
1157 thread__insert_map(thread, map);
1161 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1165 static void machine__remove_thread(struct machine *machine, struct thread *th)
1167 machine->last_match = NULL;
1168 rb_erase(&th->rb_node, &machine->threads);
1170 * We may have references to this thread, for instance in some hist_entry
1171 * instances, so just move them to a separate list.
1173 list_add_tail(&th->node, &machine->dead_threads);
1176 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1177 struct perf_sample *sample)
1179 struct thread *thread = machine__find_thread(machine,
1182 struct thread *parent = machine__findnew_thread(machine,
1186 /* if a thread currently exists for the thread id remove it */
1188 machine__remove_thread(machine, thread);
1190 thread = machine__findnew_thread(machine, event->fork.pid,
1193 perf_event__fprintf_task(event, stdout);
1195 if (thread == NULL || parent == NULL ||
1196 thread__fork(thread, parent, sample->time) < 0) {
1197 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1204 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1205 struct perf_sample *sample __maybe_unused)
1207 struct thread *thread = machine__find_thread(machine,
1212 perf_event__fprintf_task(event, stdout);
1215 thread__exited(thread);
1220 int machine__process_event(struct machine *machine, union perf_event *event,
1221 struct perf_sample *sample)
1225 switch (event->header.type) {
1226 case PERF_RECORD_COMM:
1227 ret = machine__process_comm_event(machine, event, sample); break;
1228 case PERF_RECORD_MMAP:
1229 ret = machine__process_mmap_event(machine, event, sample); break;
1230 case PERF_RECORD_MMAP2:
1231 ret = machine__process_mmap2_event(machine, event, sample); break;
1232 case PERF_RECORD_FORK:
1233 ret = machine__process_fork_event(machine, event, sample); break;
1234 case PERF_RECORD_EXIT:
1235 ret = machine__process_exit_event(machine, event, sample); break;
1236 case PERF_RECORD_LOST:
1237 ret = machine__process_lost_event(machine, event, sample); break;
1246 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1248 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
1253 static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1254 struct addr_map_symbol *ams,
1257 struct addr_location al;
1259 memset(&al, 0, sizeof(al));
1261 * We cannot use the header.misc hint to determine whether a
1262 * branch stack address is user, kernel, guest, hypervisor.
1263 * Branches may straddle the kernel/user/hypervisor boundaries.
1264 * Thus, we have to try consecutively until we find a match
1265 * or else, the symbol is unknown
1267 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
1270 ams->al_addr = al.addr;
1275 static void ip__resolve_data(struct machine *machine, struct thread *thread,
1276 u8 m, struct addr_map_symbol *ams, u64 addr)
1278 struct addr_location al;
1280 memset(&al, 0, sizeof(al));
1282 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1285 ams->al_addr = al.addr;
1290 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1291 struct addr_location *al)
1293 struct mem_info *mi = zalloc(sizeof(*mi));
1298 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1299 ip__resolve_data(al->machine, al->thread, al->cpumode,
1300 &mi->daddr, sample->addr);
1301 mi->data_src.val = sample->data_src;
1306 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1307 struct addr_location *al)
1310 const struct branch_stack *bs = sample->branch_stack;
1311 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
1316 for (i = 0; i < bs->nr; i++) {
1317 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1318 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
1319 bi[i].flags = bs->entries[i].flags;
1324 static int machine__resolve_callchain_sample(struct machine *machine,
1325 struct thread *thread,
1326 struct ip_callchain *chain,
1327 struct symbol **parent,
1328 struct addr_location *root_al,
1331 u8 cpumode = PERF_RECORD_MISC_USER;
1332 int chain_nr = min(max_stack, (int)chain->nr);
1336 int skip_idx __maybe_unused;
1338 callchain_cursor_reset(&callchain_cursor);
1340 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1341 pr_warning("corrupted callchain. skipping...\n");
1346 * Based on DWARF debug information, some architectures skip
1347 * a callchain entry saved by the kernel.
1349 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1351 for (i = 0; i < chain_nr; i++) {
1353 struct addr_location al;
1355 if (callchain_param.order == ORDER_CALLEE)
1358 j = chain->nr - i - 1;
1360 #ifdef HAVE_SKIP_CALLCHAIN_IDX
1366 if (ip >= PERF_CONTEXT_MAX) {
1368 case PERF_CONTEXT_HV:
1369 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1371 case PERF_CONTEXT_KERNEL:
1372 cpumode = PERF_RECORD_MISC_KERNEL;
1374 case PERF_CONTEXT_USER:
1375 cpumode = PERF_RECORD_MISC_USER;
1378 pr_debug("invalid callchain context: "
1379 "%"PRId64"\n", (s64) ip);
1381 * It seems the callchain is corrupted.
1384 callchain_cursor_reset(&callchain_cursor);
1391 thread__find_addr_location(thread, machine, cpumode,
1392 MAP__FUNCTION, ip, &al);
1393 if (al.sym != NULL) {
1394 if (sort__has_parent && !*parent &&
1395 symbol__match_regex(al.sym, &parent_regex))
1397 else if (have_ignore_callees && root_al &&
1398 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1399 /* Treat this symbol as the root,
1400 forgetting its callees. */
1402 callchain_cursor_reset(&callchain_cursor);
1406 err = callchain_cursor_append(&callchain_cursor,
1407 ip, al.map, al.sym);
1415 static int unwind_entry(struct unwind_entry *entry, void *arg)
1417 struct callchain_cursor *cursor = arg;
1418 return callchain_cursor_append(cursor, entry->ip,
1419 entry->map, entry->sym);
1422 int machine__resolve_callchain(struct machine *machine,
1423 struct perf_evsel *evsel,
1424 struct thread *thread,
1425 struct perf_sample *sample,
1426 struct symbol **parent,
1427 struct addr_location *root_al,
1432 ret = machine__resolve_callchain_sample(machine, thread,
1433 sample->callchain, parent,
1434 root_al, max_stack);
1438 /* Can we do dwarf post unwind? */
1439 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1440 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1443 /* Bail out if nothing was captured. */
1444 if ((!sample->user_regs.regs) ||
1445 (!sample->user_stack.size))
1448 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
1449 thread, sample, max_stack);
1453 int machine__for_each_thread(struct machine *machine,
1454 int (*fn)(struct thread *thread, void *p),
1458 struct thread *thread;
1461 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1462 thread = rb_entry(nd, struct thread, rb_node);
1463 rc = fn(thread, priv);
1468 list_for_each_entry(thread, &machine->dead_threads, node) {
1469 rc = fn(thread, priv);
1476 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1477 struct target *target, struct thread_map *threads,
1478 perf_event__handler_t process, bool data_mmap)
1480 if (target__has_task(target))
1481 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
1482 else if (target__has_cpu(target))
1483 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1484 /* command specified */
1488 pid_t machine__get_current_tid(struct machine *machine, int cpu)
1490 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1493 return machine->current_tid[cpu];
1496 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1499 struct thread *thread;
1504 if (!machine->current_tid) {
1507 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1508 if (!machine->current_tid)
1510 for (i = 0; i < MAX_NR_CPUS; i++)
1511 machine->current_tid[i] = -1;
1514 if (cpu >= MAX_NR_CPUS) {
1515 pr_err("Requested CPU %d too large. ", cpu);
1516 pr_err("Consider raising MAX_NR_CPUS\n");
1520 machine->current_tid[cpu] = tid;
1522 thread = machine__findnew_thread(machine, pid, tid);