10 const char *map_type__name[MAP__NR_TYPES] = {
11 [MAP__FUNCTION] = "Functions",
12 [MAP__VARIABLE] = "Variables",
15 static inline int is_anon_memory(const char *filename)
17 return strcmp(filename, "//anon") == 0;
20 static int strcommon(const char *pathname, char *cwd, int cwdlen)
24 while (n < cwdlen && pathname[n] == cwd[n])
30 void map__init(struct map *self, enum map_type type,
31 u64 start, u64 end, u64 pgoff, struct dso *dso)
38 self->map_ip = map__map_ip;
39 self->unmap_ip = map__unmap_ip;
40 RB_CLEAR_NODE(&self->rb_node);
44 struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
45 u64 pgoff, u32 pid, char *filename,
46 enum map_type type, char *cwd, int cwdlen)
48 struct map *self = malloc(sizeof(*self));
51 char newfilename[PATH_MAX];
56 int n = strcommon(filename, cwd, cwdlen);
59 snprintf(newfilename, sizeof(newfilename),
61 filename = newfilename;
65 anon = is_anon_memory(filename);
68 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
69 filename = newfilename;
72 dso = __dsos__findnew(dsos__list, filename);
76 map__init(self, type, start, start + len, pgoff, dso);
80 self->map_ip = self->unmap_ip = identity__map_ip;
81 } else if (strcmp(filename, "[vdso]") == 0) {
82 dso__set_loaded(dso, self->type);
92 void map__delete(struct map *self)
97 void map__fixup_start(struct map *self)
99 struct rb_root *symbols = &self->dso->symbols[self->type];
100 struct rb_node *nd = rb_first(symbols);
102 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
103 self->start = sym->start;
107 void map__fixup_end(struct map *self)
109 struct rb_root *symbols = &self->dso->symbols[self->type];
110 struct rb_node *nd = rb_last(symbols);
112 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
113 self->end = sym->end;
117 #define DSO__DELETED "(deleted)"
119 int map__load(struct map *self, symbol_filter_t filter)
121 const char *name = self->dso->long_name;
124 if (dso__loaded(self->dso, self->type))
127 nr = dso__load(self->dso, self, filter);
129 if (self->dso->has_build_id) {
130 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
132 build_id__sprintf(self->dso->build_id,
133 sizeof(self->dso->build_id),
135 pr_warning("%s with build id %s not found",
138 pr_warning("Failed to open %s", name);
140 pr_warning(", continuing without symbols\n");
142 } else if (nr == 0) {
143 const size_t len = strlen(name);
144 const size_t real_len = len - sizeof(DSO__DELETED);
146 if (len > sizeof(DSO__DELETED) &&
147 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
148 pr_warning("%.*s was updated, restart the long "
149 "running apps that use it!\n",
150 (int)real_len, name);
152 pr_warning("no symbols found in %s, maybe install "
153 "a debug package?\n", name);
159 * Only applies to the kernel, as its symtabs aren't relative like the
162 if (self->dso->kernel)
163 map__reloc_vmlinux(self);
168 struct symbol *map__find_symbol(struct map *self, u64 addr,
169 symbol_filter_t filter)
171 if (map__load(self, filter) < 0)
174 return dso__find_symbol(self->dso, self->type, addr);
177 struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
178 symbol_filter_t filter)
180 if (map__load(self, filter) < 0)
183 if (!dso__sorted_by_name(self->dso, self->type))
184 dso__sort_by_name(self->dso, self->type);
186 return dso__find_symbol_by_name(self->dso, self->type, name);
189 struct map *map__clone(struct map *self)
191 struct map *map = malloc(sizeof(*self));
196 memcpy(map, self, sizeof(*self));
201 int map__overlap(struct map *l, struct map *r)
203 if (l->start > r->start) {
209 if (l->end > r->start)
215 size_t map__fprintf(struct map *self, FILE *fp)
217 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
218 self->start, self->end, self->pgoff, self->dso->name);
222 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
223 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
225 u64 map__rip_2objdump(struct map *map, u64 rip)
227 u64 addr = map->dso->adjust_symbols ?
228 map->unmap_ip(map, rip) : /* RIP -> IP */
233 u64 map__objdump_2ip(struct map *map, u64 addr)
235 u64 ip = map->dso->adjust_symbols ?
237 map->unmap_ip(map, addr); /* RIP -> IP */
241 void map_groups__init(struct map_groups *self)
244 for (i = 0; i < MAP__NR_TYPES; ++i) {
245 self->maps[i] = RB_ROOT;
246 INIT_LIST_HEAD(&self->removed_maps[i]);
248 self->machine = NULL;
251 void map_groups__flush(struct map_groups *self)
255 for (type = 0; type < MAP__NR_TYPES; type++) {
256 struct rb_root *root = &self->maps[type];
257 struct rb_node *next = rb_first(root);
260 struct map *pos = rb_entry(next, struct map, rb_node);
261 next = rb_next(&pos->rb_node);
262 rb_erase(&pos->rb_node, root);
264 * We may have references to this map, for
265 * instance in some hist_entry instances, so
266 * just move them to a separate list.
268 list_add_tail(&pos->node, &self->removed_maps[pos->type]);
273 struct symbol *map_groups__find_symbol(struct map_groups *self,
274 enum map_type type, u64 addr,
276 symbol_filter_t filter)
278 struct map *map = map_groups__find(self, type, addr);
283 return map__find_symbol(map, map->map_ip(map, addr), filter);
289 struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
293 symbol_filter_t filter)
297 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
298 struct map *pos = rb_entry(nd, struct map, rb_node);
299 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
311 size_t __map_groups__fprintf_maps(struct map_groups *self,
312 enum map_type type, int verbose, FILE *fp)
314 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
317 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
318 struct map *pos = rb_entry(nd, struct map, rb_node);
319 printed += fprintf(fp, "Map:");
320 printed += map__fprintf(pos, fp);
322 printed += dso__fprintf(pos->dso, type, fp);
323 printed += fprintf(fp, "--\n");
330 size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp)
332 size_t printed = 0, i;
333 for (i = 0; i < MAP__NR_TYPES; ++i)
334 printed += __map_groups__fprintf_maps(self, i, verbose, fp);
338 static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
340 int verbose, FILE *fp)
345 list_for_each_entry(pos, &self->removed_maps[type], node) {
346 printed += fprintf(fp, "Map:");
347 printed += map__fprintf(pos, fp);
349 printed += dso__fprintf(pos->dso, type, fp);
350 printed += fprintf(fp, "--\n");
356 static size_t map_groups__fprintf_removed_maps(struct map_groups *self,
357 int verbose, FILE *fp)
359 size_t printed = 0, i;
360 for (i = 0; i < MAP__NR_TYPES; ++i)
361 printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp);
365 size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp)
367 size_t printed = map_groups__fprintf_maps(self, verbose, fp);
368 printed += fprintf(fp, "Removed maps:\n");
369 return printed + map_groups__fprintf_removed_maps(self, verbose, fp);
372 int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
373 int verbose, FILE *fp)
375 struct rb_root *root = &self->maps[map->type];
376 struct rb_node *next = rb_first(root);
379 struct map *pos = rb_entry(next, struct map, rb_node);
380 next = rb_next(&pos->rb_node);
382 if (!map__overlap(pos, map))
386 fputs("overlapping maps:\n", fp);
387 map__fprintf(map, fp);
388 map__fprintf(pos, fp);
391 rb_erase(&pos->rb_node, root);
393 * We may have references to this map, for instance in some
394 * hist_entry instances, so just move them to a separate
397 list_add_tail(&pos->node, &self->removed_maps[map->type]);
399 * Now check if we need to create new maps for areas not
400 * overlapped by the new map:
402 if (map->start > pos->start) {
403 struct map *before = map__clone(pos);
408 before->end = map->start - 1;
409 map_groups__insert(self, before);
411 map__fprintf(before, fp);
414 if (map->end < pos->end) {
415 struct map *after = map__clone(pos);
420 after->start = map->end + 1;
421 map_groups__insert(self, after);
423 map__fprintf(after, fp);
431 * XXX This should not really _copy_ te maps, but refcount them.
433 int map_groups__clone(struct map_groups *self,
434 struct map_groups *parent, enum map_type type)
437 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
438 struct map *map = rb_entry(nd, struct map, rb_node);
439 struct map *new = map__clone(map);
442 map_groups__insert(self, new);
447 static u64 map__reloc_map_ip(struct map *map, u64 ip)
449 return ip + (s64)map->pgoff;
452 static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
454 return ip - (s64)map->pgoff;
457 void map__reloc_vmlinux(struct map *self)
459 struct kmap *kmap = map__kmap(self);
462 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
465 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
466 kmap->ref_reloc_sym->addr);
471 self->map_ip = map__reloc_map_ip;
472 self->unmap_ip = map__reloc_unmap_ip;
476 void maps__insert(struct rb_root *maps, struct map *map)
478 struct rb_node **p = &maps->rb_node;
479 struct rb_node *parent = NULL;
480 const u64 ip = map->start;
485 m = rb_entry(parent, struct map, rb_node);
492 rb_link_node(&map->rb_node, parent, p);
493 rb_insert_color(&map->rb_node, maps);
496 struct map *maps__find(struct rb_root *maps, u64 ip)
498 struct rb_node **p = &maps->rb_node;
499 struct rb_node *parent = NULL;
504 m = rb_entry(parent, struct map, rb_node);
507 else if (ip > m->end)
516 int machine__init(struct machine *self, const char *root_dir, pid_t pid)
518 map_groups__init(&self->kmaps);
519 RB_CLEAR_NODE(&self->rb_node);
520 INIT_LIST_HEAD(&self->user_dsos);
521 INIT_LIST_HEAD(&self->kernel_dsos);
523 self->kmaps.machine = self;
525 self->root_dir = strdup(root_dir);
526 return self->root_dir == NULL ? -ENOMEM : 0;
529 struct machine *machines__add(struct rb_root *self, pid_t pid,
530 const char *root_dir)
532 struct rb_node **p = &self->rb_node;
533 struct rb_node *parent = NULL;
534 struct machine *pos, *machine = malloc(sizeof(*machine));
539 if (machine__init(machine, root_dir, pid) != 0) {
546 pos = rb_entry(parent, struct machine, rb_node);
553 rb_link_node(&machine->rb_node, parent, p);
554 rb_insert_color(&machine->rb_node, self);
559 struct machine *machines__find(struct rb_root *self, pid_t pid)
561 struct rb_node **p = &self->rb_node;
562 struct rb_node *parent = NULL;
563 struct machine *machine;
564 struct machine *default_machine = NULL;
568 machine = rb_entry(parent, struct machine, rb_node);
569 if (pid < machine->pid)
571 else if (pid > machine->pid)
576 default_machine = machine;
579 return default_machine;
582 struct machine *machines__findnew(struct rb_root *self, pid_t pid)
585 const char *root_dir;
586 struct machine *machine = machines__find(self, pid);
588 if (!machine || machine->pid != pid) {
589 if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID)
592 if (!symbol_conf.guestmount)
594 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
595 if (access(path, R_OK)) {
596 pr_err("Can't access file %s\n", path);
601 machine = machines__add(self, pid, root_dir);
608 void machines__process(struct rb_root *self, machine__process_t process, void *data)
612 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
613 struct machine *pos = rb_entry(nd, struct machine, rb_node);
618 char *machine__mmap_name(struct machine *self, char *bf, size_t size)
620 if (machine__is_host(self))
621 snprintf(bf, size, "[%s]", "kernel.kallsyms");
622 else if (machine__is_default_guest(self))
623 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
625 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);