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, char *filename,
54 struct map *map = malloc(sizeof(*map));
57 char newfilename[PATH_MAX];
59 int anon, no_dso, vdso;
61 anon = is_anon_memory(filename);
62 vdso = is_vdso_map(filename);
63 no_dso = is_no_dso_memory(filename);
66 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
67 filename = newfilename;
72 dso = vdso__dso_findnew(dsos__list);
74 dso = __dsos__findnew(dsos__list, filename);
79 map__init(map, type, start, start + len, pgoff, dso);
82 map->map_ip = map->unmap_ip = identity__map_ip;
85 * Set memory without DSO as loaded. All map__find_*
86 * functions still return NULL, and we avoid the
87 * unnecessary map__load warning.
90 dso__set_loaded(dso, map->type);
100 * Constructor variant for modules (where we know from /proc/modules where
101 * they are loaded) and for vmlinux, where only after we load all the
102 * symbols we'll know where it starts and ends.
104 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
106 struct map *map = calloc(1, (sizeof(*map) +
107 (dso->kernel ? sizeof(struct kmap) : 0)));
110 * ->end will be filled after we load all the symbols
112 map__init(map, type, start, 0, 0, dso);
118 void map__delete(struct map *map)
123 void map__fixup_start(struct map *map)
125 struct rb_root *symbols = &map->dso->symbols[map->type];
126 struct rb_node *nd = rb_first(symbols);
128 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
129 map->start = sym->start;
133 void map__fixup_end(struct map *map)
135 struct rb_root *symbols = &map->dso->symbols[map->type];
136 struct rb_node *nd = rb_last(symbols);
138 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
143 #define DSO__DELETED "(deleted)"
145 int map__load(struct map *map, symbol_filter_t filter)
147 const char *name = map->dso->long_name;
150 if (dso__loaded(map->dso, map->type))
153 nr = dso__load(map->dso, map, filter);
155 if (map->dso->has_build_id) {
156 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
158 build_id__sprintf(map->dso->build_id,
159 sizeof(map->dso->build_id),
161 pr_warning("%s with build id %s not found",
164 pr_warning("Failed to open %s", name);
166 pr_warning(", continuing without symbols\n");
168 } else if (nr == 0) {
169 #ifdef LIBELF_SUPPORT
170 const size_t len = strlen(name);
171 const size_t real_len = len - sizeof(DSO__DELETED);
173 if (len > sizeof(DSO__DELETED) &&
174 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
175 pr_warning("%.*s was updated (is prelink enabled?). "
176 "Restart the long running apps that use it!\n",
177 (int)real_len, name);
179 pr_warning("no symbols found in %s, maybe install "
180 "a debug package?\n", name);
186 * Only applies to the kernel, as its symtabs aren't relative like the
189 if (map->dso->kernel)
190 map__reloc_vmlinux(map);
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.
259 u64 map__rip_2objdump(struct map *map, u64 rip)
261 u64 addr = map->dso->adjust_symbols ?
262 map->unmap_ip(map, rip) : /* RIP -> IP */
267 void map_groups__init(struct map_groups *mg)
270 for (i = 0; i < MAP__NR_TYPES; ++i) {
271 mg->maps[i] = RB_ROOT;
272 INIT_LIST_HEAD(&mg->removed_maps[i]);
277 static void maps__delete(struct rb_root *maps)
279 struct rb_node *next = rb_first(maps);
282 struct map *pos = rb_entry(next, struct map, rb_node);
284 next = rb_next(&pos->rb_node);
285 rb_erase(&pos->rb_node, maps);
290 static void maps__delete_removed(struct list_head *maps)
294 list_for_each_entry_safe(pos, n, maps, node) {
295 list_del(&pos->node);
300 void map_groups__exit(struct map_groups *mg)
304 for (i = 0; i < MAP__NR_TYPES; ++i) {
305 maps__delete(&mg->maps[i]);
306 maps__delete_removed(&mg->removed_maps[i]);
310 void map_groups__flush(struct map_groups *mg)
314 for (type = 0; type < MAP__NR_TYPES; type++) {
315 struct rb_root *root = &mg->maps[type];
316 struct rb_node *next = rb_first(root);
319 struct map *pos = rb_entry(next, struct map, rb_node);
320 next = rb_next(&pos->rb_node);
321 rb_erase(&pos->rb_node, root);
323 * We may have references to this map, for
324 * instance in some hist_entry instances, so
325 * just move them to a separate list.
327 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
332 struct symbol *map_groups__find_symbol(struct map_groups *mg,
333 enum map_type type, u64 addr,
335 symbol_filter_t filter)
337 struct map *map = map_groups__find(mg, type, addr);
342 return map__find_symbol(map, map->map_ip(map, addr), filter);
348 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
352 symbol_filter_t filter)
356 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
357 struct map *pos = rb_entry(nd, struct map, rb_node);
358 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
370 size_t __map_groups__fprintf_maps(struct map_groups *mg,
371 enum map_type type, int verbose, FILE *fp)
373 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
376 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
377 struct map *pos = rb_entry(nd, struct map, rb_node);
378 printed += fprintf(fp, "Map:");
379 printed += map__fprintf(pos, fp);
381 printed += dso__fprintf(pos->dso, type, fp);
382 printed += fprintf(fp, "--\n");
389 size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
391 size_t printed = 0, i;
392 for (i = 0; i < MAP__NR_TYPES; ++i)
393 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
397 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
399 int verbose, FILE *fp)
404 list_for_each_entry(pos, &mg->removed_maps[type], node) {
405 printed += fprintf(fp, "Map:");
406 printed += map__fprintf(pos, fp);
408 printed += dso__fprintf(pos->dso, type, fp);
409 printed += fprintf(fp, "--\n");
415 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
416 int verbose, FILE *fp)
418 size_t printed = 0, i;
419 for (i = 0; i < MAP__NR_TYPES; ++i)
420 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
424 size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
426 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
427 printed += fprintf(fp, "Removed maps:\n");
428 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
431 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
432 int verbose, FILE *fp)
434 struct rb_root *root = &mg->maps[map->type];
435 struct rb_node *next = rb_first(root);
439 struct map *pos = rb_entry(next, struct map, rb_node);
440 next = rb_next(&pos->rb_node);
442 if (!map__overlap(pos, map))
446 fputs("overlapping maps:\n", fp);
447 map__fprintf(map, fp);
448 map__fprintf(pos, fp);
451 rb_erase(&pos->rb_node, root);
453 * Now check if we need to create new maps for areas not
454 * overlapped by the new map:
456 if (map->start > pos->start) {
457 struct map *before = map__clone(pos);
459 if (before == NULL) {
464 before->end = map->start - 1;
465 map_groups__insert(mg, before);
467 map__fprintf(before, fp);
470 if (map->end < pos->end) {
471 struct map *after = map__clone(pos);
478 after->start = map->end + 1;
479 map_groups__insert(mg, after);
481 map__fprintf(after, fp);
485 * If we have references, just move them to a separate list.
488 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
500 * XXX This should not really _copy_ te maps, but refcount them.
502 int map_groups__clone(struct map_groups *mg,
503 struct map_groups *parent, enum map_type type)
506 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
507 struct map *map = rb_entry(nd, struct map, rb_node);
508 struct map *new = map__clone(map);
511 map_groups__insert(mg, new);
516 static u64 map__reloc_map_ip(struct map *map, u64 ip)
518 return ip + (s64)map->pgoff;
521 static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
523 return ip - (s64)map->pgoff;
526 void map__reloc_vmlinux(struct map *map)
528 struct kmap *kmap = map__kmap(map);
531 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
534 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
535 kmap->ref_reloc_sym->addr);
540 map->map_ip = map__reloc_map_ip;
541 map->unmap_ip = map__reloc_unmap_ip;
545 void maps__insert(struct rb_root *maps, struct map *map)
547 struct rb_node **p = &maps->rb_node;
548 struct rb_node *parent = NULL;
549 const u64 ip = map->start;
554 m = rb_entry(parent, struct map, rb_node);
561 rb_link_node(&map->rb_node, parent, p);
562 rb_insert_color(&map->rb_node, maps);
565 void maps__remove(struct rb_root *maps, struct map *map)
567 rb_erase(&map->rb_node, maps);
570 struct map *maps__find(struct rb_root *maps, u64 ip)
572 struct rb_node **p = &maps->rb_node;
573 struct rb_node *parent = NULL;
578 m = rb_entry(parent, struct map, rb_node);
581 else if (ip > m->end)