15 #include <linux/string.h>
17 const char *map_type__name[MAP__NR_TYPES] = {
18 [MAP__FUNCTION] = "Functions",
19 [MAP__VARIABLE] = "Variables",
22 static inline int is_anon_memory(const char *filename)
24 return !strcmp(filename, "//anon") ||
25 !strcmp(filename, "/dev/zero (deleted)") ||
26 !strcmp(filename, "/anon_hugepage (deleted)");
29 static inline int is_no_dso_memory(const char *filename)
31 return !strncmp(filename, "[stack", 6) ||
32 !strcmp(filename, "[heap]");
35 static inline int is_android_lib(const char *filename)
37 return !strncmp(filename, "/data/app-lib", 13) ||
38 !strncmp(filename, "/system/lib", 11);
41 static inline bool replace_android_lib(const char *filename, char *newfilename)
45 size_t app_abi_length, new_length;
46 size_t lib_length = 0;
48 libname = strrchr(filename, '/');
50 lib_length = strlen(libname);
52 app_abi = getenv("APP_ABI");
56 app_abi_length = strlen(app_abi);
58 if (!strncmp(filename, "/data/app-lib", 13)) {
64 new_length = 7 + app_abi_length + lib_length;
66 apk_path = getenv("APK_PATH");
68 new_length += strlen(apk_path) + 1;
69 if (new_length > PATH_MAX)
71 snprintf(newfilename, new_length,
72 "%s/libs/%s/%s", apk_path, app_abi, libname);
74 if (new_length > PATH_MAX)
76 snprintf(newfilename, new_length,
77 "libs/%s/%s", app_abi, libname);
82 if (!strncmp(filename, "/system/lib/", 11)) {
88 ndk = getenv("NDK_ROOT");
89 app = getenv("APP_PLATFORM");
94 ndk_length = strlen(ndk);
95 app_length = strlen(app);
97 if (!(ndk_length && app_length && app_abi_length))
100 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
101 !strncmp(app_abi, "mips", 4) ? "mips" :
102 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
107 new_length = 27 + ndk_length +
108 app_length + lib_length
111 if (new_length > PATH_MAX)
113 snprintf(newfilename, new_length,
114 "%s/platforms/%s/arch-%s/usr/lib/%s",
115 ndk, app, arch, libname);
122 void map__init(struct map *map, enum map_type type,
123 u64 start, u64 end, u64 pgoff, struct dso *dso)
131 map->map_ip = map__map_ip;
132 map->unmap_ip = map__unmap_ip;
133 RB_CLEAR_NODE(&map->rb_node);
135 map->referenced = false;
136 map->erange_warned = false;
139 struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
140 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
141 u64 ino_gen, char *filename,
144 struct map *map = malloc(sizeof(*map));
147 char newfilename[PATH_MAX];
149 int anon, no_dso, vdso, android;
151 android = is_android_lib(filename);
152 anon = is_anon_memory(filename);
153 vdso = is_vdso_map(filename);
154 no_dso = is_no_dso_memory(filename);
159 map->ino_generation = ino_gen;
161 if ((anon || no_dso) && type == MAP__FUNCTION) {
162 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
163 filename = newfilename;
167 if (replace_android_lib(filename, newfilename))
168 filename = newfilename;
173 dso = vdso__dso_findnew(dsos__list);
175 dso = __dsos__findnew(dsos__list, filename);
180 map__init(map, type, start, start + len, pgoff, dso);
182 if (anon || no_dso) {
183 map->map_ip = map->unmap_ip = identity__map_ip;
186 * Set memory without DSO as loaded. All map__find_*
187 * functions still return NULL, and we avoid the
188 * unnecessary map__load warning.
190 if (type != MAP__FUNCTION)
191 dso__set_loaded(dso, map->type);
201 * Constructor variant for modules (where we know from /proc/modules where
202 * they are loaded) and for vmlinux, where only after we load all the
203 * symbols we'll know where it starts and ends.
205 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
207 struct map *map = calloc(1, (sizeof(*map) +
208 (dso->kernel ? sizeof(struct kmap) : 0)));
211 * ->end will be filled after we load all the symbols
213 map__init(map, type, start, 0, 0, dso);
219 void map__delete(struct map *map)
224 void map__fixup_start(struct map *map)
226 struct rb_root *symbols = &map->dso->symbols[map->type];
227 struct rb_node *nd = rb_first(symbols);
229 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
230 map->start = sym->start;
234 void map__fixup_end(struct map *map)
236 struct rb_root *symbols = &map->dso->symbols[map->type];
237 struct rb_node *nd = rb_last(symbols);
239 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
244 #define DSO__DELETED "(deleted)"
246 int map__load(struct map *map, symbol_filter_t filter)
248 const char *name = map->dso->long_name;
251 if (dso__loaded(map->dso, map->type))
254 nr = dso__load(map->dso, map, filter);
256 if (map->dso->has_build_id) {
257 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
259 build_id__sprintf(map->dso->build_id,
260 sizeof(map->dso->build_id),
262 pr_warning("%s with build id %s not found",
265 pr_warning("Failed to open %s", name);
267 pr_warning(", continuing without symbols\n");
269 } else if (nr == 0) {
270 #ifdef HAVE_LIBELF_SUPPORT
271 const size_t len = strlen(name);
272 const size_t real_len = len - sizeof(DSO__DELETED);
274 if (len > sizeof(DSO__DELETED) &&
275 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
276 pr_warning("%.*s was updated (is prelink enabled?). "
277 "Restart the long running apps that use it!\n",
278 (int)real_len, name);
280 pr_warning("no symbols found in %s, maybe install "
281 "a debug package?\n", name);
290 struct symbol *map__find_symbol(struct map *map, u64 addr,
291 symbol_filter_t filter)
293 if (map__load(map, filter) < 0)
296 return dso__find_symbol(map->dso, map->type, addr);
299 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
300 symbol_filter_t filter)
302 if (map__load(map, filter) < 0)
305 if (!dso__sorted_by_name(map->dso, map->type))
306 dso__sort_by_name(map->dso, map->type);
308 return dso__find_symbol_by_name(map->dso, map->type, name);
311 struct map *map__clone(struct map *map)
313 return memdup(map, sizeof(*map));
316 int map__overlap(struct map *l, struct map *r)
318 if (l->start > r->start) {
324 if (l->end > r->start)
330 size_t map__fprintf(struct map *map, FILE *fp)
332 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
333 map->start, map->end, map->pgoff, map->dso->name);
336 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
338 const char *dsoname = "[unknown]";
340 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
341 if (symbol_conf.show_kernel_path && map->dso->long_name)
342 dsoname = map->dso->long_name;
343 else if (map->dso->name)
344 dsoname = map->dso->name;
347 return fprintf(fp, "%s", dsoname);
350 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
356 if (map && map->dso) {
357 srcline = get_srcline(map->dso,
358 map__rip_2objdump(map, addr));
359 if (srcline != SRCLINE_UNKNOWN)
360 ret = fprintf(fp, "%s%s", prefix, srcline);
361 free_srcline(srcline);
367 * map__rip_2objdump - convert symbol start address to objdump address.
369 * @rip: symbol start address
371 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
372 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
373 * relative to section start.
375 * Return: Address suitable for passing to "objdump --start-address="
377 u64 map__rip_2objdump(struct map *map, u64 rip)
379 if (!map->dso->adjust_symbols)
383 return rip - map->pgoff;
385 return map->unmap_ip(map, rip) - map->reloc;
389 * map__objdump_2mem - convert objdump address to a memory address.
391 * @ip: objdump address
393 * Closely related to map__rip_2objdump(), this function takes an address from
394 * objdump and converts it to a memory address. Note this assumes that @map
395 * contains the address. To be sure the result is valid, check it forwards
396 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
398 * Return: Memory address.
400 u64 map__objdump_2mem(struct map *map, u64 ip)
402 if (!map->dso->adjust_symbols)
403 return map->unmap_ip(map, ip);
406 return map->unmap_ip(map, ip + map->pgoff);
408 return ip + map->reloc;
411 void map_groups__init(struct map_groups *mg)
414 for (i = 0; i < MAP__NR_TYPES; ++i) {
415 mg->maps[i] = RB_ROOT;
416 INIT_LIST_HEAD(&mg->removed_maps[i]);
422 static void maps__delete(struct rb_root *maps)
424 struct rb_node *next = rb_first(maps);
427 struct map *pos = rb_entry(next, struct map, rb_node);
429 next = rb_next(&pos->rb_node);
430 rb_erase(&pos->rb_node, maps);
435 static void maps__delete_removed(struct list_head *maps)
439 list_for_each_entry_safe(pos, n, maps, node) {
440 list_del(&pos->node);
445 void map_groups__exit(struct map_groups *mg)
449 for (i = 0; i < MAP__NR_TYPES; ++i) {
450 maps__delete(&mg->maps[i]);
451 maps__delete_removed(&mg->removed_maps[i]);
455 struct map_groups *map_groups__new(void)
457 struct map_groups *mg = malloc(sizeof(*mg));
460 map_groups__init(mg);
465 void map_groups__delete(struct map_groups *mg)
467 map_groups__exit(mg);
471 void map_groups__put(struct map_groups *mg)
473 if (--mg->refcnt == 0)
474 map_groups__delete(mg);
477 void map_groups__flush(struct map_groups *mg)
481 for (type = 0; type < MAP__NR_TYPES; type++) {
482 struct rb_root *root = &mg->maps[type];
483 struct rb_node *next = rb_first(root);
486 struct map *pos = rb_entry(next, struct map, rb_node);
487 next = rb_next(&pos->rb_node);
488 rb_erase(&pos->rb_node, root);
490 * We may have references to this map, for
491 * instance in some hist_entry instances, so
492 * just move them to a separate list.
494 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
499 struct symbol *map_groups__find_symbol(struct map_groups *mg,
500 enum map_type type, u64 addr,
502 symbol_filter_t filter)
504 struct map *map = map_groups__find(mg, type, addr);
506 /* Ensure map is loaded before using map->map_ip */
507 if (map != NULL && map__load(map, filter) >= 0) {
510 return map__find_symbol(map, map->map_ip(map, addr), filter);
516 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
520 symbol_filter_t filter)
524 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
525 struct map *pos = rb_entry(nd, struct map, rb_node);
526 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
538 int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
540 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
541 if (ams->map->groups == NULL)
543 ams->map = map_groups__find(ams->map->groups, ams->map->type,
545 if (ams->map == NULL)
549 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
550 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
552 return ams->sym ? 0 : -1;
555 size_t __map_groups__fprintf_maps(struct map_groups *mg,
556 enum map_type type, int verbose, FILE *fp)
558 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
561 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
562 struct map *pos = rb_entry(nd, struct map, rb_node);
563 printed += fprintf(fp, "Map:");
564 printed += map__fprintf(pos, fp);
566 printed += dso__fprintf(pos->dso, type, fp);
567 printed += fprintf(fp, "--\n");
574 size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
576 size_t printed = 0, i;
577 for (i = 0; i < MAP__NR_TYPES; ++i)
578 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
582 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
584 int verbose, FILE *fp)
589 list_for_each_entry(pos, &mg->removed_maps[type], node) {
590 printed += fprintf(fp, "Map:");
591 printed += map__fprintf(pos, fp);
593 printed += dso__fprintf(pos->dso, type, fp);
594 printed += fprintf(fp, "--\n");
600 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
601 int verbose, FILE *fp)
603 size_t printed = 0, i;
604 for (i = 0; i < MAP__NR_TYPES; ++i)
605 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
609 size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
611 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
612 printed += fprintf(fp, "Removed maps:\n");
613 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
616 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
617 int verbose, FILE *fp)
619 struct rb_root *root = &mg->maps[map->type];
620 struct rb_node *next = rb_first(root);
624 struct map *pos = rb_entry(next, struct map, rb_node);
625 next = rb_next(&pos->rb_node);
627 if (!map__overlap(pos, map))
631 fputs("overlapping maps:\n", fp);
632 map__fprintf(map, fp);
633 map__fprintf(pos, fp);
636 rb_erase(&pos->rb_node, root);
638 * Now check if we need to create new maps for areas not
639 * overlapped by the new map:
641 if (map->start > pos->start) {
642 struct map *before = map__clone(pos);
644 if (before == NULL) {
649 before->end = map->start - 1;
650 map_groups__insert(mg, before);
652 map__fprintf(before, fp);
655 if (map->end < pos->end) {
656 struct map *after = map__clone(pos);
663 after->start = map->end + 1;
664 map_groups__insert(mg, after);
666 map__fprintf(after, fp);
670 * If we have references, just move them to a separate list.
673 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
685 * XXX This should not really _copy_ te maps, but refcount them.
687 int map_groups__clone(struct map_groups *mg,
688 struct map_groups *parent, enum map_type type)
691 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
692 struct map *map = rb_entry(nd, struct map, rb_node);
693 struct map *new = map__clone(map);
696 map_groups__insert(mg, new);
701 void maps__insert(struct rb_root *maps, struct map *map)
703 struct rb_node **p = &maps->rb_node;
704 struct rb_node *parent = NULL;
705 const u64 ip = map->start;
710 m = rb_entry(parent, struct map, rb_node);
717 rb_link_node(&map->rb_node, parent, p);
718 rb_insert_color(&map->rb_node, maps);
721 void maps__remove(struct rb_root *maps, struct map *map)
723 rb_erase(&map->rb_node, maps);
726 struct map *maps__find(struct rb_root *maps, u64 ip)
728 struct rb_node **p = &maps->rb_node;
729 struct rb_node *parent = NULL;
734 m = rb_entry(parent, struct map, rb_node);
737 else if (ip > m->end)
746 struct map *maps__first(struct rb_root *maps)
748 struct rb_node *first = rb_first(maps);
751 return rb_entry(first, struct map, rb_node);
755 struct map *maps__next(struct map *map)
757 struct rb_node *next = rb_next(&map->rb_node);
760 return rb_entry(next, struct map, rb_node);