14 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
16 map_groups__init(&machine->kmaps);
17 RB_CLEAR_NODE(&machine->rb_node);
18 INIT_LIST_HEAD(&machine->user_dsos);
19 INIT_LIST_HEAD(&machine->kernel_dsos);
21 machine->threads = RB_ROOT;
22 INIT_LIST_HEAD(&machine->dead_threads);
23 machine->last_match = NULL;
25 machine->kmaps.machine = machine;
28 machine->root_dir = strdup(root_dir);
29 if (machine->root_dir == NULL)
32 if (pid != HOST_KERNEL_ID) {
33 struct thread *thread = machine__findnew_thread(machine, pid);
39 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
40 thread__set_comm(thread, comm);
46 static void dsos__delete(struct list_head *dsos)
50 list_for_each_entry_safe(pos, n, dsos, node) {
56 void machine__delete_dead_threads(struct machine *machine)
60 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
66 void machine__delete_threads(struct machine *machine)
68 struct rb_node *nd = rb_first(&machine->threads);
71 struct thread *t = rb_entry(nd, struct thread, rb_node);
73 rb_erase(&t->rb_node, &machine->threads);
79 void machine__exit(struct machine *machine)
81 map_groups__exit(&machine->kmaps);
82 dsos__delete(&machine->user_dsos);
83 dsos__delete(&machine->kernel_dsos);
84 free(machine->root_dir);
85 machine->root_dir = NULL;
88 void machine__delete(struct machine *machine)
90 machine__exit(machine);
94 void machines__init(struct machines *machines)
96 machine__init(&machines->host, "", HOST_KERNEL_ID);
97 machines->guests = RB_ROOT;
100 void machines__exit(struct machines *machines)
102 machine__exit(&machines->host);
106 struct machine *machines__add(struct machines *machines, pid_t pid,
107 const char *root_dir)
109 struct rb_node **p = &machines->guests.rb_node;
110 struct rb_node *parent = NULL;
111 struct machine *pos, *machine = malloc(sizeof(*machine));
116 if (machine__init(machine, root_dir, pid) != 0) {
123 pos = rb_entry(parent, struct machine, rb_node);
130 rb_link_node(&machine->rb_node, parent, p);
131 rb_insert_color(&machine->rb_node, &machines->guests);
136 struct machine *machines__find(struct machines *machines, pid_t pid)
138 struct rb_node **p = &machines->guests.rb_node;
139 struct rb_node *parent = NULL;
140 struct machine *machine;
141 struct machine *default_machine = NULL;
143 if (pid == HOST_KERNEL_ID)
144 return &machines->host;
148 machine = rb_entry(parent, struct machine, rb_node);
149 if (pid < machine->pid)
151 else if (pid > machine->pid)
156 default_machine = machine;
159 return default_machine;
162 struct machine *machines__findnew(struct machines *machines, pid_t pid)
165 const char *root_dir = "";
166 struct machine *machine = machines__find(machines, pid);
168 if (machine && (machine->pid == pid))
171 if ((pid != HOST_KERNEL_ID) &&
172 (pid != DEFAULT_GUEST_KERNEL_ID) &&
173 (symbol_conf.guestmount)) {
174 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
175 if (access(path, R_OK)) {
176 static struct strlist *seen;
179 seen = strlist__new(true, NULL);
181 if (!strlist__has_entry(seen, path)) {
182 pr_err("Can't access file %s\n", path);
183 strlist__add(seen, path);
191 machine = machines__add(machines, pid, root_dir);
196 void machines__process_guests(struct machines *machines,
197 machine__process_t process, void *data)
201 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
202 struct machine *pos = rb_entry(nd, struct machine, rb_node);
207 char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
209 if (machine__is_host(machine))
210 snprintf(bf, size, "[%s]", "kernel.kallsyms");
211 else if (machine__is_default_guest(machine))
212 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
214 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
221 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
223 struct rb_node *node;
224 struct machine *machine;
226 machines->host.id_hdr_size = id_hdr_size;
228 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
229 machine = rb_entry(node, struct machine, rb_node);
230 machine->id_hdr_size = id_hdr_size;
236 static struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid,
239 struct rb_node **p = &machine->threads.rb_node;
240 struct rb_node *parent = NULL;
244 * Font-end cache - PID lookups come in blocks,
245 * so most of the time we dont have to look up
248 if (machine->last_match && machine->last_match->pid == pid)
249 return machine->last_match;
253 th = rb_entry(parent, struct thread, rb_node);
255 if (th->pid == pid) {
256 machine->last_match = th;
269 th = thread__new(pid);
271 rb_link_node(&th->rb_node, parent, p);
272 rb_insert_color(&th->rb_node, &machine->threads);
273 machine->last_match = th;
279 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid)
281 return __machine__findnew_thread(machine, pid, true);
284 struct thread *machine__find_thread(struct machine *machine, pid_t pid)
286 return __machine__findnew_thread(machine, pid, false);
289 int machine__process_comm_event(struct machine *machine, union perf_event *event)
291 struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
294 perf_event__fprintf_comm(event, stdout);
296 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
297 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
304 int machine__process_lost_event(struct machine *machine __maybe_unused,
305 union perf_event *event)
307 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
308 event->lost.id, event->lost.lost);
312 struct map *machine__new_module(struct machine *machine, u64 start,
313 const char *filename)
316 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
321 map = map__new2(start, dso, MAP__FUNCTION);
325 if (machine__is_host(machine))
326 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
328 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
329 map_groups__insert(&machine->kmaps, map);
333 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
336 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
337 __dsos__fprintf(&machines->host.user_dsos, fp);
339 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
340 struct machine *pos = rb_entry(nd, struct machine, rb_node);
341 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
342 ret += __dsos__fprintf(&pos->user_dsos, fp);
348 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
349 bool (skip)(struct dso *dso, int parm), int parm)
351 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
352 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
355 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
356 bool (skip)(struct dso *dso, int parm), int parm)
359 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
361 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
362 struct machine *pos = rb_entry(nd, struct machine, rb_node);
363 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
368 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
372 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
374 if (kdso->has_build_id) {
375 char filename[PATH_MAX];
376 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
377 printed += fprintf(fp, "[0] %s\n", filename);
380 for (i = 0; i < vmlinux_path__nr_entries; ++i)
381 printed += fprintf(fp, "[%d] %s\n",
382 i + kdso->has_build_id, vmlinux_path[i]);
387 size_t machine__fprintf(struct machine *machine, FILE *fp)
392 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
393 struct thread *pos = rb_entry(nd, struct thread, rb_node);
395 ret += thread__fprintf(pos, fp);
401 static struct dso *machine__get_kernel(struct machine *machine)
403 const char *vmlinux_name = NULL;
406 if (machine__is_host(machine)) {
407 vmlinux_name = symbol_conf.vmlinux_name;
409 vmlinux_name = "[kernel.kallsyms]";
411 kernel = dso__kernel_findnew(machine, vmlinux_name,
417 if (machine__is_default_guest(machine))
418 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
420 vmlinux_name = machine__mmap_name(machine, bf,
423 kernel = dso__kernel_findnew(machine, vmlinux_name,
425 DSO_TYPE_GUEST_KERNEL);
428 if (kernel != NULL && (!kernel->has_build_id))
429 dso__read_running_kernel_build_id(kernel, machine);
434 struct process_args {
438 static int symbol__in_kernel(void *arg, const char *name,
439 char type __maybe_unused, u64 start)
441 struct process_args *args = arg;
443 if (strchr(name, '['))
450 /* Figure out the start address of kernel map from /proc/kallsyms */
451 static u64 machine__get_kernel_start_addr(struct machine *machine)
453 const char *filename;
455 struct process_args args;
457 if (machine__is_host(machine)) {
458 filename = "/proc/kallsyms";
460 if (machine__is_default_guest(machine))
461 filename = (char *)symbol_conf.default_guest_kallsyms;
463 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
468 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
471 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
477 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
480 u64 start = machine__get_kernel_start_addr(machine);
482 for (type = 0; type < MAP__NR_TYPES; ++type) {
485 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
486 if (machine->vmlinux_maps[type] == NULL)
489 machine->vmlinux_maps[type]->map_ip =
490 machine->vmlinux_maps[type]->unmap_ip =
492 kmap = map__kmap(machine->vmlinux_maps[type]);
493 kmap->kmaps = &machine->kmaps;
494 map_groups__insert(&machine->kmaps,
495 machine->vmlinux_maps[type]);
501 void machine__destroy_kernel_maps(struct machine *machine)
505 for (type = 0; type < MAP__NR_TYPES; ++type) {
508 if (machine->vmlinux_maps[type] == NULL)
511 kmap = map__kmap(machine->vmlinux_maps[type]);
512 map_groups__remove(&machine->kmaps,
513 machine->vmlinux_maps[type]);
514 if (kmap->ref_reloc_sym) {
516 * ref_reloc_sym is shared among all maps, so free just
519 if (type == MAP__FUNCTION) {
520 free((char *)kmap->ref_reloc_sym->name);
521 kmap->ref_reloc_sym->name = NULL;
522 free(kmap->ref_reloc_sym);
524 kmap->ref_reloc_sym = NULL;
527 map__delete(machine->vmlinux_maps[type]);
528 machine->vmlinux_maps[type] = NULL;
532 int machines__create_guest_kernel_maps(struct machines *machines)
535 struct dirent **namelist = NULL;
541 if (symbol_conf.default_guest_vmlinux_name ||
542 symbol_conf.default_guest_modules ||
543 symbol_conf.default_guest_kallsyms) {
544 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
547 if (symbol_conf.guestmount) {
548 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
551 for (i = 0; i < items; i++) {
552 if (!isdigit(namelist[i]->d_name[0])) {
553 /* Filter out . and .. */
556 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
557 if ((*endp != '\0') ||
558 (endp == namelist[i]->d_name) ||
560 pr_debug("invalid directory (%s). Skipping.\n",
561 namelist[i]->d_name);
564 sprintf(path, "%s/%s/proc/kallsyms",
565 symbol_conf.guestmount,
566 namelist[i]->d_name);
567 ret = access(path, R_OK);
569 pr_debug("Can't access file %s\n", path);
572 machines__create_kernel_maps(machines, pid);
581 void machines__destroy_kernel_maps(struct machines *machines)
583 struct rb_node *next = rb_first(&machines->guests);
585 machine__destroy_kernel_maps(&machines->host);
588 struct machine *pos = rb_entry(next, struct machine, rb_node);
590 next = rb_next(&pos->rb_node);
591 rb_erase(&pos->rb_node, &machines->guests);
592 machine__delete(pos);
596 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
598 struct machine *machine = machines__findnew(machines, pid);
603 return machine__create_kernel_maps(machine);
606 int machine__load_kallsyms(struct machine *machine, const char *filename,
607 enum map_type type, symbol_filter_t filter)
609 struct map *map = machine->vmlinux_maps[type];
610 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
613 dso__set_loaded(map->dso, type);
615 * Since /proc/kallsyms will have multiple sessions for the
616 * kernel, with modules between them, fixup the end of all
619 __map_groups__fixup_end(&machine->kmaps, type);
625 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
626 symbol_filter_t filter)
628 struct map *map = machine->vmlinux_maps[type];
629 int ret = dso__load_vmlinux_path(map->dso, map, filter);
632 dso__set_loaded(map->dso, type);
633 map__reloc_vmlinux(map);
639 static void map_groups__fixup_end(struct map_groups *mg)
642 for (i = 0; i < MAP__NR_TYPES; ++i)
643 __map_groups__fixup_end(mg, i);
646 static char *get_kernel_version(const char *root_dir)
648 char version[PATH_MAX];
651 const char *prefix = "Linux version ";
653 sprintf(version, "%s/proc/version", root_dir);
654 file = fopen(version, "r");
659 tmp = fgets(version, sizeof(version), file);
662 name = strstr(version, prefix);
665 name += strlen(prefix);
666 tmp = strchr(name, ' ');
673 static int map_groups__set_modules_path_dir(struct map_groups *mg,
674 const char *dir_name)
677 DIR *dir = opendir(dir_name);
681 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
685 while ((dent = readdir(dir)) != NULL) {
689 /*sshfs might return bad dent->d_type, so we have to stat*/
690 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
694 if (S_ISDIR(st.st_mode)) {
695 if (!strcmp(dent->d_name, ".") ||
696 !strcmp(dent->d_name, ".."))
699 ret = map_groups__set_modules_path_dir(mg, path);
703 char *dot = strrchr(dent->d_name, '.'),
708 if (dot == NULL || strcmp(dot, ".ko"))
710 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
711 (int)(dot - dent->d_name), dent->d_name);
713 strxfrchar(dso_name, '-', '_');
714 map = map_groups__find_by_name(mg, MAP__FUNCTION,
719 long_name = strdup(path);
720 if (long_name == NULL) {
724 dso__set_long_name(map->dso, long_name);
725 map->dso->lname_alloc = 1;
726 dso__kernel_module_get_build_id(map->dso, "");
735 static int machine__set_modules_path(struct machine *machine)
738 char modules_path[PATH_MAX];
740 version = get_kernel_version(machine->root_dir);
744 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
745 machine->root_dir, version);
748 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
751 static int machine__create_modules(struct machine *machine)
760 if (machine__is_default_guest(machine))
761 modules = symbol_conf.default_guest_modules;
763 sprintf(path, "%s/proc/modules", machine->root_dir);
767 if (symbol__restricted_filename(path, "/proc/modules"))
770 file = fopen(modules, "r");
774 while (!feof(file)) {
780 line_len = getline(&line, &n, file);
787 line[--line_len] = '\0'; /* \n */
789 sep = strrchr(line, 'x');
793 hex2u64(sep + 1, &start);
795 sep = strchr(line, ' ');
801 snprintf(name, sizeof(name), "[%s]", line);
802 map = machine__new_module(machine, start, name);
804 goto out_delete_line;
805 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
811 return machine__set_modules_path(machine);
819 int machine__create_kernel_maps(struct machine *machine)
821 struct dso *kernel = machine__get_kernel(machine);
823 if (kernel == NULL ||
824 __machine__create_kernel_maps(machine, kernel) < 0)
827 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
828 if (machine__is_host(machine))
829 pr_debug("Problems creating module maps, "
830 "continuing anyway...\n");
832 pr_debug("Problems creating module maps for guest %d, "
833 "continuing anyway...\n", machine->pid);
837 * Now that we have all the maps created, just set the ->end of them:
839 map_groups__fixup_end(&machine->kmaps);
843 static void machine__set_kernel_mmap_len(struct machine *machine,
844 union perf_event *event)
848 for (i = 0; i < MAP__NR_TYPES; i++) {
849 machine->vmlinux_maps[i]->start = event->mmap.start;
850 machine->vmlinux_maps[i]->end = (event->mmap.start +
853 * Be a bit paranoid here, some perf.data file came with
854 * a zero sized synthesized MMAP event for the kernel.
856 if (machine->vmlinux_maps[i]->end == 0)
857 machine->vmlinux_maps[i]->end = ~0ULL;
861 static int machine__process_kernel_mmap_event(struct machine *machine,
862 union perf_event *event)
865 char kmmap_prefix[PATH_MAX];
866 enum dso_kernel_type kernel_type;
869 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
870 if (machine__is_host(machine))
871 kernel_type = DSO_TYPE_KERNEL;
873 kernel_type = DSO_TYPE_GUEST_KERNEL;
875 is_kernel_mmap = memcmp(event->mmap.filename,
877 strlen(kmmap_prefix) - 1) == 0;
878 if (event->mmap.filename[0] == '/' ||
879 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
881 char short_module_name[1024];
884 if (event->mmap.filename[0] == '/') {
885 name = strrchr(event->mmap.filename, '/');
890 dot = strrchr(name, '.');
893 snprintf(short_module_name, sizeof(short_module_name),
894 "[%.*s]", (int)(dot - name), name);
895 strxfrchar(short_module_name, '-', '_');
897 strcpy(short_module_name, event->mmap.filename);
899 map = machine__new_module(machine, event->mmap.start,
900 event->mmap.filename);
904 name = strdup(short_module_name);
908 map->dso->short_name = name;
909 map->dso->sname_alloc = 1;
910 map->end = map->start + event->mmap.len;
911 } else if (is_kernel_mmap) {
912 const char *symbol_name = (event->mmap.filename +
913 strlen(kmmap_prefix));
915 * Should be there already, from the build-id table in
918 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
923 kernel->kernel = kernel_type;
924 if (__machine__create_kernel_maps(machine, kernel) < 0)
927 machine__set_kernel_mmap_len(machine, event);
930 * Avoid using a zero address (kptr_restrict) for the ref reloc
931 * symbol. Effectively having zero here means that at record
932 * time /proc/sys/kernel/kptr_restrict was non zero.
934 if (event->mmap.pgoff != 0) {
935 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
940 if (machine__is_default_guest(machine)) {
942 * preload dso of guest kernel and modules
944 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
953 int machine__process_mmap_event(struct machine *machine, union perf_event *event)
955 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
956 struct thread *thread;
961 perf_event__fprintf_mmap(event, stdout);
963 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
964 cpumode == PERF_RECORD_MISC_KERNEL) {
965 ret = machine__process_kernel_mmap_event(machine, event);
971 thread = machine__findnew_thread(machine, event->mmap.pid);
974 map = map__new(&machine->user_dsos, event->mmap.start,
975 event->mmap.len, event->mmap.pgoff,
976 event->mmap.pid, event->mmap.filename,
981 thread__insert_map(thread, map);
985 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
989 int machine__process_fork_event(struct machine *machine, union perf_event *event)
991 struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
992 struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
995 perf_event__fprintf_task(event, stdout);
997 if (thread == NULL || parent == NULL ||
998 thread__fork(thread, parent) < 0) {
999 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1006 int machine__process_exit_event(struct machine *machine, union perf_event *event)
1008 struct thread *thread = machine__find_thread(machine, event->fork.tid);
1011 perf_event__fprintf_task(event, stdout);
1014 machine__remove_thread(machine, thread);
1019 int machine__process_event(struct machine *machine, union perf_event *event)
1023 switch (event->header.type) {
1024 case PERF_RECORD_COMM:
1025 ret = machine__process_comm_event(machine, event); break;
1026 case PERF_RECORD_MMAP:
1027 ret = machine__process_mmap_event(machine, event); break;
1028 case PERF_RECORD_FORK:
1029 ret = machine__process_fork_event(machine, event); break;
1030 case PERF_RECORD_EXIT:
1031 ret = machine__process_exit_event(machine, event); break;
1032 case PERF_RECORD_LOST:
1033 ret = machine__process_lost_event(machine, event); break;
1042 void machine__remove_thread(struct machine *machine, struct thread *th)
1044 machine->last_match = NULL;
1045 rb_erase(&th->rb_node, &machine->threads);
1047 * We may have references to this thread, for instance in some hist_entry
1048 * instances, so just move them to a separate list.
1050 list_add_tail(&th->node, &machine->dead_threads);
1053 static bool symbol__match_parent_regex(struct symbol *sym)
1055 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1061 static const u8 cpumodes[] = {
1062 PERF_RECORD_MISC_USER,
1063 PERF_RECORD_MISC_KERNEL,
1064 PERF_RECORD_MISC_GUEST_USER,
1065 PERF_RECORD_MISC_GUEST_KERNEL
1067 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
1069 static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1070 struct addr_map_symbol *ams,
1073 struct addr_location al;
1077 memset(&al, 0, sizeof(al));
1079 for (i = 0; i < NCPUMODES; i++) {
1082 * We cannot use the header.misc hint to determine whether a
1083 * branch stack address is user, kernel, guest, hypervisor.
1084 * Branches may straddle the kernel/user/hypervisor boundaries.
1085 * Thus, we have to try consecutively until we find a match
1086 * or else, the symbol is unknown
1088 thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
1095 ams->al_addr = al.addr;
1100 struct branch_info *machine__resolve_bstack(struct machine *machine,
1102 struct branch_stack *bs)
1104 struct branch_info *bi;
1107 bi = calloc(bs->nr, sizeof(struct branch_info));
1111 for (i = 0; i < bs->nr; i++) {
1112 ip__resolve_ams(machine, thr, &bi[i].to, bs->entries[i].to);
1113 ip__resolve_ams(machine, thr, &bi[i].from, bs->entries[i].from);
1114 bi[i].flags = bs->entries[i].flags;
1119 static int machine__resolve_callchain_sample(struct machine *machine,
1120 struct thread *thread,
1121 struct ip_callchain *chain,
1122 struct symbol **parent)
1125 u8 cpumode = PERF_RECORD_MISC_USER;
1129 callchain_cursor_reset(&callchain_cursor);
1131 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1132 pr_warning("corrupted callchain. skipping...\n");
1136 for (i = 0; i < chain->nr; i++) {
1138 struct addr_location al;
1140 if (callchain_param.order == ORDER_CALLEE)
1143 ip = chain->ips[chain->nr - i - 1];
1145 if (ip >= PERF_CONTEXT_MAX) {
1147 case PERF_CONTEXT_HV:
1148 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1150 case PERF_CONTEXT_KERNEL:
1151 cpumode = PERF_RECORD_MISC_KERNEL;
1153 case PERF_CONTEXT_USER:
1154 cpumode = PERF_RECORD_MISC_USER;
1157 pr_debug("invalid callchain context: "
1158 "%"PRId64"\n", (s64) ip);
1160 * It seems the callchain is corrupted.
1163 callchain_cursor_reset(&callchain_cursor);
1169 al.filtered = false;
1170 thread__find_addr_location(thread, machine, cpumode,
1171 MAP__FUNCTION, ip, &al, NULL);
1172 if (al.sym != NULL) {
1173 if (sort__has_parent && !*parent &&
1174 symbol__match_parent_regex(al.sym))
1176 if (!symbol_conf.use_callchain)
1180 err = callchain_cursor_append(&callchain_cursor,
1181 ip, al.map, al.sym);
1189 static int unwind_entry(struct unwind_entry *entry, void *arg)
1191 struct callchain_cursor *cursor = arg;
1192 return callchain_cursor_append(cursor, entry->ip,
1193 entry->map, entry->sym);
1196 int machine__resolve_callchain(struct machine *machine,
1197 struct perf_evsel *evsel,
1198 struct thread *thread,
1199 struct perf_sample *sample,
1200 struct symbol **parent)
1205 callchain_cursor_reset(&callchain_cursor);
1207 ret = machine__resolve_callchain_sample(machine, thread,
1208 sample->callchain, parent);
1212 /* Can we do dwarf post unwind? */
1213 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1214 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1217 /* Bail out if nothing was captured. */
1218 if ((!sample->user_regs.regs) ||
1219 (!sample->user_stack.size))
1222 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
1223 thread, evsel->attr.sample_regs_user,