9 const char *map_type__name[MAP__NR_TYPES] = {
10 [MAP__FUNCTION] = "Functions",
11 [MAP__VARIABLE] = "Variables",
14 static inline int is_anon_memory(const char *filename)
16 return strcmp(filename, "//anon") == 0;
19 static int strcommon(const char *pathname, char *cwd, int cwdlen)
23 while (n < cwdlen && pathname[n] == cwd[n])
29 void map__init(struct map *self, enum map_type type,
30 u64 start, u64 end, u64 pgoff, struct dso *dso)
37 self->map_ip = map__map_ip;
38 self->unmap_ip = map__unmap_ip;
39 RB_CLEAR_NODE(&self->rb_node);
42 struct map *map__new(u64 start, u64 len, u64 pgoff, u32 pid, char *filename,
43 enum map_type type, char *cwd, int cwdlen)
45 struct map *self = malloc(sizeof(*self));
48 char newfilename[PATH_MAX];
53 int n = strcommon(filename, cwd, cwdlen);
56 snprintf(newfilename, sizeof(newfilename),
58 filename = newfilename;
62 anon = is_anon_memory(filename);
65 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
66 filename = newfilename;
69 dso = dsos__findnew(filename);
73 map__init(self, type, start, start + len, pgoff, dso);
77 self->map_ip = self->unmap_ip = identity__map_ip;
78 } else if (strcmp(filename, "[vdso]") == 0) {
79 dso__set_loaded(dso, self->type);
89 void map__delete(struct map *self)
94 void map__fixup_start(struct map *self)
96 struct rb_root *symbols = &self->dso->symbols[self->type];
97 struct rb_node *nd = rb_first(symbols);
99 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
100 self->start = sym->start;
104 void map__fixup_end(struct map *self)
106 struct rb_root *symbols = &self->dso->symbols[self->type];
107 struct rb_node *nd = rb_last(symbols);
109 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
110 self->end = sym->end;
114 #define DSO__DELETED "(deleted)"
116 int map__load(struct map *self, symbol_filter_t filter)
118 const char *name = self->dso->long_name;
121 if (dso__loaded(self->dso, self->type))
124 nr = dso__load(self->dso, self, filter);
126 if (self->dso->has_build_id) {
127 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
129 build_id__sprintf(self->dso->build_id,
130 sizeof(self->dso->build_id),
132 pr_warning("%s with build id %s not found",
135 pr_warning("Failed to open %s", name);
137 pr_warning(", continuing without symbols\n");
139 } else if (nr == 0) {
140 const size_t len = strlen(name);
141 const size_t real_len = len - sizeof(DSO__DELETED);
143 if (len > sizeof(DSO__DELETED) &&
144 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
145 pr_warning("%.*s was updated, restart the long "
146 "running apps that use it!\n",
147 (int)real_len, name);
149 pr_warning("no symbols found in %s, maybe install "
150 "a debug package?\n", name);
156 * Only applies to the kernel, as its symtabs aren't relative like the
159 if (self->dso->kernel)
160 map__reloc_vmlinux(self);
165 struct symbol *map__find_symbol(struct map *self, u64 addr,
166 symbol_filter_t filter)
168 if (map__load(self, filter) < 0)
171 return dso__find_symbol(self->dso, self->type, addr);
174 struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
175 symbol_filter_t filter)
177 if (map__load(self, filter) < 0)
180 if (!dso__sorted_by_name(self->dso, self->type))
181 dso__sort_by_name(self->dso, self->type);
183 return dso__find_symbol_by_name(self->dso, self->type, name);
186 struct map *map__clone(struct map *self)
188 struct map *map = malloc(sizeof(*self));
193 memcpy(map, self, sizeof(*self));
198 int map__overlap(struct map *l, struct map *r)
200 if (l->start > r->start) {
206 if (l->end > r->start)
212 size_t map__fprintf(struct map *self, FILE *fp)
214 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
215 self->start, self->end, self->pgoff, self->dso->name);
219 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
220 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
222 u64 map__rip_2objdump(struct map *map, u64 rip)
224 u64 addr = map->dso->adjust_symbols ?
225 map->unmap_ip(map, rip) : /* RIP -> IP */
230 u64 map__objdump_2ip(struct map *map, u64 addr)
232 u64 ip = map->dso->adjust_symbols ?
234 map->unmap_ip(map, addr); /* RIP -> IP */
238 void map_groups__init(struct map_groups *self)
241 for (i = 0; i < MAP__NR_TYPES; ++i) {
242 self->maps[i] = RB_ROOT;
243 INIT_LIST_HEAD(&self->removed_maps[i]);
247 void map_groups__flush(struct map_groups *self)
251 for (type = 0; type < MAP__NR_TYPES; type++) {
252 struct rb_root *root = &self->maps[type];
253 struct rb_node *next = rb_first(root);
256 struct map *pos = rb_entry(next, struct map, rb_node);
257 next = rb_next(&pos->rb_node);
258 rb_erase(&pos->rb_node, root);
260 * We may have references to this map, for
261 * instance in some hist_entry instances, so
262 * just move them to a separate list.
264 list_add_tail(&pos->node, &self->removed_maps[pos->type]);
269 struct symbol *map_groups__find_symbol(struct map_groups *self,
270 enum map_type type, u64 addr,
272 symbol_filter_t filter)
274 struct map *map = map_groups__find(self, type, addr);
279 return map__find_symbol(map, map->map_ip(map, addr), filter);
285 struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
289 symbol_filter_t filter)
293 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
294 struct map *pos = rb_entry(nd, struct map, rb_node);
295 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
307 size_t __map_groups__fprintf_maps(struct map_groups *self,
308 enum map_type type, int verbose, FILE *fp)
310 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
313 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
314 struct map *pos = rb_entry(nd, struct map, rb_node);
315 printed += fprintf(fp, "Map:");
316 printed += map__fprintf(pos, fp);
318 printed += dso__fprintf(pos->dso, type, fp);
319 printed += fprintf(fp, "--\n");
326 size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp)
328 size_t printed = 0, i;
329 for (i = 0; i < MAP__NR_TYPES; ++i)
330 printed += __map_groups__fprintf_maps(self, i, verbose, fp);
334 static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
336 int verbose, FILE *fp)
341 list_for_each_entry(pos, &self->removed_maps[type], node) {
342 printed += fprintf(fp, "Map:");
343 printed += map__fprintf(pos, fp);
345 printed += dso__fprintf(pos->dso, type, fp);
346 printed += fprintf(fp, "--\n");
352 static size_t map_groups__fprintf_removed_maps(struct map_groups *self,
353 int verbose, FILE *fp)
355 size_t printed = 0, i;
356 for (i = 0; i < MAP__NR_TYPES; ++i)
357 printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp);
361 size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp)
363 size_t printed = map_groups__fprintf_maps(self, verbose, fp);
364 printed += fprintf(fp, "Removed maps:\n");
365 return printed + map_groups__fprintf_removed_maps(self, verbose, fp);
368 int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
369 int verbose, FILE *fp)
371 struct rb_root *root = &self->maps[map->type];
372 struct rb_node *next = rb_first(root);
375 struct map *pos = rb_entry(next, struct map, rb_node);
376 next = rb_next(&pos->rb_node);
378 if (!map__overlap(pos, map))
382 fputs("overlapping maps:\n", fp);
383 map__fprintf(map, fp);
384 map__fprintf(pos, fp);
387 rb_erase(&pos->rb_node, root);
389 * We may have references to this map, for instance in some
390 * hist_entry instances, so just move them to a separate
393 list_add_tail(&pos->node, &self->removed_maps[map->type]);
395 * Now check if we need to create new maps for areas not
396 * overlapped by the new map:
398 if (map->start > pos->start) {
399 struct map *before = map__clone(pos);
404 before->end = map->start - 1;
405 map_groups__insert(self, before);
407 map__fprintf(before, fp);
410 if (map->end < pos->end) {
411 struct map *after = map__clone(pos);
416 after->start = map->end + 1;
417 map_groups__insert(self, after);
419 map__fprintf(after, fp);
427 * XXX This should not really _copy_ te maps, but refcount them.
429 int map_groups__clone(struct map_groups *self,
430 struct map_groups *parent, enum map_type type)
433 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
434 struct map *map = rb_entry(nd, struct map, rb_node);
435 struct map *new = map__clone(map);
438 map_groups__insert(self, new);
443 static u64 map__reloc_map_ip(struct map *map, u64 ip)
445 return ip + (s64)map->pgoff;
448 static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
450 return ip - (s64)map->pgoff;
453 void map__reloc_vmlinux(struct map *self)
455 struct kmap *kmap = map__kmap(self);
458 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
461 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
462 kmap->ref_reloc_sym->addr);
467 self->map_ip = map__reloc_map_ip;
468 self->unmap_ip = map__reloc_unmap_ip;
472 void maps__insert(struct rb_root *maps, struct map *map)
474 struct rb_node **p = &maps->rb_node;
475 struct rb_node *parent = NULL;
476 const u64 ip = map->start;
481 m = rb_entry(parent, struct map, rb_node);
488 rb_link_node(&map->rb_node, parent, p);
489 rb_insert_color(&map->rb_node, maps);
492 struct map *maps__find(struct rb_root *maps, u64 ip)
494 struct rb_node **p = &maps->rb_node;
495 struct rb_node *parent = NULL;
500 m = rb_entry(parent, struct map, rb_node);
503 else if (ip > m->end)