14 #include <linux/string.h>
16 const char *map_type__name[MAP__NR_TYPES] = {
17 [MAP__FUNCTION] = "Functions",
18 [MAP__VARIABLE] = "Variables",
21 static inline int is_anon_memory(const char *filename)
23 return !strcmp(filename, "//anon") ||
24 !strcmp(filename, "/dev/zero (deleted)") ||
25 !strcmp(filename, "/anon_hugepage (deleted)");
28 static inline int is_no_dso_memory(const char *filename)
30 return !strncmp(filename, "[stack", 6) ||
31 !strcmp(filename, "[heap]");
34 void map__init(struct map *map, enum map_type type,
35 u64 start, u64 end, u64 pgoff, struct dso *dso)
42 map->map_ip = map__map_ip;
43 map->unmap_ip = map__unmap_ip;
44 RB_CLEAR_NODE(&map->rb_node);
46 map->referenced = false;
47 map->erange_warned = false;
50 struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
51 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
52 u64 ino_gen, char *filename,
55 struct map *map = malloc(sizeof(*map));
58 char newfilename[PATH_MAX];
60 int anon, no_dso, vdso;
62 anon = is_anon_memory(filename);
63 vdso = is_vdso_map(filename);
64 no_dso = is_no_dso_memory(filename);
69 map->ino_generation = ino_gen;
72 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
73 filename = newfilename;
78 dso = vdso__dso_findnew(dsos__list);
80 dso = __dsos__findnew(dsos__list, filename);
85 map__init(map, type, start, start + len, pgoff, dso);
88 map->map_ip = map->unmap_ip = identity__map_ip;
91 * Set memory without DSO as loaded. All map__find_*
92 * functions still return NULL, and we avoid the
93 * unnecessary map__load warning.
96 dso__set_loaded(dso, map->type);
106 * Constructor variant for modules (where we know from /proc/modules where
107 * they are loaded) and for vmlinux, where only after we load all the
108 * symbols we'll know where it starts and ends.
110 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
112 struct map *map = calloc(1, (sizeof(*map) +
113 (dso->kernel ? sizeof(struct kmap) : 0)));
116 * ->end will be filled after we load all the symbols
118 map__init(map, type, start, 0, 0, dso);
124 void map__delete(struct map *map)
129 void map__fixup_start(struct map *map)
131 struct rb_root *symbols = &map->dso->symbols[map->type];
132 struct rb_node *nd = rb_first(symbols);
134 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
135 map->start = sym->start;
139 void map__fixup_end(struct map *map)
141 struct rb_root *symbols = &map->dso->symbols[map->type];
142 struct rb_node *nd = rb_last(symbols);
144 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
149 #define DSO__DELETED "(deleted)"
151 int map__load(struct map *map, symbol_filter_t filter)
153 const char *name = map->dso->long_name;
156 if (dso__loaded(map->dso, map->type))
159 nr = dso__load(map->dso, map, filter);
161 if (map->dso->has_build_id) {
162 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
164 build_id__sprintf(map->dso->build_id,
165 sizeof(map->dso->build_id),
167 pr_warning("%s with build id %s not found",
170 pr_warning("Failed to open %s", name);
172 pr_warning(", continuing without symbols\n");
174 } else if (nr == 0) {
175 #ifdef LIBELF_SUPPORT
176 const size_t len = strlen(name);
177 const size_t real_len = len - sizeof(DSO__DELETED);
179 if (len > sizeof(DSO__DELETED) &&
180 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
181 pr_warning("%.*s was updated (is prelink enabled?). "
182 "Restart the long running apps that use it!\n",
183 (int)real_len, name);
185 pr_warning("no symbols found in %s, maybe install "
186 "a debug package?\n", name);
195 struct symbol *map__find_symbol(struct map *map, u64 addr,
196 symbol_filter_t filter)
198 if (map__load(map, filter) < 0)
201 return dso__find_symbol(map->dso, map->type, addr);
204 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
205 symbol_filter_t filter)
207 if (map__load(map, filter) < 0)
210 if (!dso__sorted_by_name(map->dso, map->type))
211 dso__sort_by_name(map->dso, map->type);
213 return dso__find_symbol_by_name(map->dso, map->type, name);
216 struct map *map__clone(struct map *map)
218 return memdup(map, sizeof(*map));
221 int map__overlap(struct map *l, struct map *r)
223 if (l->start > r->start) {
229 if (l->end > r->start)
235 size_t map__fprintf(struct map *map, FILE *fp)
237 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
238 map->start, map->end, map->pgoff, map->dso->name);
241 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
243 const char *dsoname = "[unknown]";
245 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
246 if (symbol_conf.show_kernel_path && map->dso->long_name)
247 dsoname = map->dso->long_name;
248 else if (map->dso->name)
249 dsoname = map->dso->name;
252 return fprintf(fp, "%s", dsoname);
256 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
257 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
258 * relative to section start.
260 u64 map__rip_2objdump(struct map *map, u64 rip)
262 if (!map->dso->adjust_symbols)
266 return rip - map->pgoff;
268 return map->unmap_ip(map, rip);
271 void map_groups__init(struct map_groups *mg)
274 for (i = 0; i < MAP__NR_TYPES; ++i) {
275 mg->maps[i] = RB_ROOT;
276 INIT_LIST_HEAD(&mg->removed_maps[i]);
281 static void maps__delete(struct rb_root *maps)
283 struct rb_node *next = rb_first(maps);
286 struct map *pos = rb_entry(next, struct map, rb_node);
288 next = rb_next(&pos->rb_node);
289 rb_erase(&pos->rb_node, maps);
294 static void maps__delete_removed(struct list_head *maps)
298 list_for_each_entry_safe(pos, n, maps, node) {
299 list_del(&pos->node);
304 void map_groups__exit(struct map_groups *mg)
308 for (i = 0; i < MAP__NR_TYPES; ++i) {
309 maps__delete(&mg->maps[i]);
310 maps__delete_removed(&mg->removed_maps[i]);
314 void map_groups__flush(struct map_groups *mg)
318 for (type = 0; type < MAP__NR_TYPES; type++) {
319 struct rb_root *root = &mg->maps[type];
320 struct rb_node *next = rb_first(root);
323 struct map *pos = rb_entry(next, struct map, rb_node);
324 next = rb_next(&pos->rb_node);
325 rb_erase(&pos->rb_node, root);
327 * We may have references to this map, for
328 * instance in some hist_entry instances, so
329 * just move them to a separate list.
331 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
336 struct symbol *map_groups__find_symbol(struct map_groups *mg,
337 enum map_type type, u64 addr,
339 symbol_filter_t filter)
341 struct map *map = map_groups__find(mg, type, addr);
346 return map__find_symbol(map, map->map_ip(map, addr), filter);
352 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
356 symbol_filter_t filter)
360 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
361 struct map *pos = rb_entry(nd, struct map, rb_node);
362 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
374 size_t __map_groups__fprintf_maps(struct map_groups *mg,
375 enum map_type type, int verbose, FILE *fp)
377 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
380 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
381 struct map *pos = rb_entry(nd, struct map, rb_node);
382 printed += fprintf(fp, "Map:");
383 printed += map__fprintf(pos, fp);
385 printed += dso__fprintf(pos->dso, type, fp);
386 printed += fprintf(fp, "--\n");
393 size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
395 size_t printed = 0, i;
396 for (i = 0; i < MAP__NR_TYPES; ++i)
397 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
401 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
403 int verbose, FILE *fp)
408 list_for_each_entry(pos, &mg->removed_maps[type], node) {
409 printed += fprintf(fp, "Map:");
410 printed += map__fprintf(pos, fp);
412 printed += dso__fprintf(pos->dso, type, fp);
413 printed += fprintf(fp, "--\n");
419 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
420 int verbose, FILE *fp)
422 size_t printed = 0, i;
423 for (i = 0; i < MAP__NR_TYPES; ++i)
424 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
428 size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
430 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
431 printed += fprintf(fp, "Removed maps:\n");
432 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
435 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
436 int verbose, FILE *fp)
438 struct rb_root *root = &mg->maps[map->type];
439 struct rb_node *next = rb_first(root);
443 struct map *pos = rb_entry(next, struct map, rb_node);
444 next = rb_next(&pos->rb_node);
446 if (!map__overlap(pos, map))
450 fputs("overlapping maps:\n", fp);
451 map__fprintf(map, fp);
452 map__fprintf(pos, fp);
455 rb_erase(&pos->rb_node, root);
457 * Now check if we need to create new maps for areas not
458 * overlapped by the new map:
460 if (map->start > pos->start) {
461 struct map *before = map__clone(pos);
463 if (before == NULL) {
468 before->end = map->start - 1;
469 map_groups__insert(mg, before);
471 map__fprintf(before, fp);
474 if (map->end < pos->end) {
475 struct map *after = map__clone(pos);
482 after->start = map->end + 1;
483 map_groups__insert(mg, after);
485 map__fprintf(after, fp);
489 * If we have references, just move them to a separate list.
492 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
504 * XXX This should not really _copy_ te maps, but refcount them.
506 int map_groups__clone(struct map_groups *mg,
507 struct map_groups *parent, enum map_type type)
510 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
511 struct map *map = rb_entry(nd, struct map, rb_node);
512 struct map *new = map__clone(map);
515 map_groups__insert(mg, new);
520 void maps__insert(struct rb_root *maps, struct map *map)
522 struct rb_node **p = &maps->rb_node;
523 struct rb_node *parent = NULL;
524 const u64 ip = map->start;
529 m = rb_entry(parent, struct map, rb_node);
536 rb_link_node(&map->rb_node, parent, p);
537 rb_insert_color(&map->rb_node, maps);
540 void maps__remove(struct rb_root *maps, struct map *map)
542 rb_erase(&map->rb_node, maps);
545 struct map *maps__find(struct rb_root *maps, u64 ip)
547 struct rb_node **p = &maps->rb_node;
548 struct rb_node *parent = NULL;
553 m = rb_entry(parent, struct map, rb_node);
556 else if (ip > m->end)
565 struct map *maps__first(struct rb_root *maps)
567 struct rb_node *first = rb_first(maps);
570 return rb_entry(first, struct map, rb_node);
574 struct map *maps__next(struct map *map)
576 struct rb_node *next = rb_next(&map->rb_node);
579 return rb_entry(next, struct map, rb_node);