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, u32 prot, u32 flags, 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;
163 if ((anon || no_dso) && type == MAP__FUNCTION) {
164 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
165 filename = newfilename;
169 if (replace_android_lib(filename, newfilename))
170 filename = newfilename;
175 dso = vdso__dso_findnew(dsos__list);
177 dso = __dsos__findnew(dsos__list, filename);
182 map__init(map, type, start, start + len, pgoff, dso);
184 if (anon || no_dso) {
185 map->map_ip = map->unmap_ip = identity__map_ip;
188 * Set memory without DSO as loaded. All map__find_*
189 * functions still return NULL, and we avoid the
190 * unnecessary map__load warning.
192 if (type != MAP__FUNCTION)
193 dso__set_loaded(dso, map->type);
203 * Constructor variant for modules (where we know from /proc/modules where
204 * they are loaded) and for vmlinux, where only after we load all the
205 * symbols we'll know where it starts and ends.
207 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
209 struct map *map = calloc(1, (sizeof(*map) +
210 (dso->kernel ? sizeof(struct kmap) : 0)));
213 * ->end will be filled after we load all the symbols
215 map__init(map, type, start, 0, 0, dso);
221 void map__delete(struct map *map)
226 void map__fixup_start(struct map *map)
228 struct rb_root *symbols = &map->dso->symbols[map->type];
229 struct rb_node *nd = rb_first(symbols);
231 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
232 map->start = sym->start;
236 void map__fixup_end(struct map *map)
238 struct rb_root *symbols = &map->dso->symbols[map->type];
239 struct rb_node *nd = rb_last(symbols);
241 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
246 #define DSO__DELETED "(deleted)"
248 int map__load(struct map *map, symbol_filter_t filter)
250 const char *name = map->dso->long_name;
253 if (dso__loaded(map->dso, map->type))
256 nr = dso__load(map->dso, map, filter);
258 if (map->dso->has_build_id) {
259 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
261 build_id__sprintf(map->dso->build_id,
262 sizeof(map->dso->build_id),
264 pr_warning("%s with build id %s not found",
267 pr_warning("Failed to open %s", name);
269 pr_warning(", continuing without symbols\n");
271 } else if (nr == 0) {
272 #ifdef HAVE_LIBELF_SUPPORT
273 const size_t len = strlen(name);
274 const size_t real_len = len - sizeof(DSO__DELETED);
276 if (len > sizeof(DSO__DELETED) &&
277 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
278 pr_warning("%.*s was updated (is prelink enabled?). "
279 "Restart the long running apps that use it!\n",
280 (int)real_len, name);
282 pr_warning("no symbols found in %s, maybe install "
283 "a debug package?\n", name);
292 struct symbol *map__find_symbol(struct map *map, u64 addr,
293 symbol_filter_t filter)
295 if (map__load(map, filter) < 0)
298 return dso__find_symbol(map->dso, map->type, addr);
301 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
302 symbol_filter_t filter)
304 if (map__load(map, filter) < 0)
307 if (!dso__sorted_by_name(map->dso, map->type))
308 dso__sort_by_name(map->dso, map->type);
310 return dso__find_symbol_by_name(map->dso, map->type, name);
313 struct map *map__clone(struct map *map)
315 return memdup(map, sizeof(*map));
318 int map__overlap(struct map *l, struct map *r)
320 if (l->start > r->start) {
326 if (l->end > r->start)
332 size_t map__fprintf(struct map *map, FILE *fp)
334 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
335 map->start, map->end, map->pgoff, map->dso->name);
338 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
340 const char *dsoname = "[unknown]";
342 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
343 if (symbol_conf.show_kernel_path && map->dso->long_name)
344 dsoname = map->dso->long_name;
345 else if (map->dso->name)
346 dsoname = map->dso->name;
349 return fprintf(fp, "%s", dsoname);
352 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
358 if (map && map->dso) {
359 srcline = get_srcline(map->dso,
360 map__rip_2objdump(map, addr));
361 if (srcline != SRCLINE_UNKNOWN)
362 ret = fprintf(fp, "%s%s", prefix, srcline);
363 free_srcline(srcline);
369 * map__rip_2objdump - convert symbol start address to objdump address.
371 * @rip: symbol start address
373 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
374 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
375 * relative to section start.
377 * Return: Address suitable for passing to "objdump --start-address="
379 u64 map__rip_2objdump(struct map *map, u64 rip)
381 if (!map->dso->adjust_symbols)
385 return rip - map->pgoff;
387 return map->unmap_ip(map, rip) - map->reloc;
391 * map__objdump_2mem - convert objdump address to a memory address.
393 * @ip: objdump address
395 * Closely related to map__rip_2objdump(), this function takes an address from
396 * objdump and converts it to a memory address. Note this assumes that @map
397 * contains the address. To be sure the result is valid, check it forwards
398 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
400 * Return: Memory address.
402 u64 map__objdump_2mem(struct map *map, u64 ip)
404 if (!map->dso->adjust_symbols)
405 return map->unmap_ip(map, ip);
408 return map->unmap_ip(map, ip + map->pgoff);
410 return ip + map->reloc;
413 void map_groups__init(struct map_groups *mg)
416 for (i = 0; i < MAP__NR_TYPES; ++i) {
417 mg->maps[i] = RB_ROOT;
418 INIT_LIST_HEAD(&mg->removed_maps[i]);
424 static void maps__delete(struct rb_root *maps)
426 struct rb_node *next = rb_first(maps);
429 struct map *pos = rb_entry(next, struct map, rb_node);
431 next = rb_next(&pos->rb_node);
432 rb_erase(&pos->rb_node, maps);
437 static void maps__delete_removed(struct list_head *maps)
441 list_for_each_entry_safe(pos, n, maps, node) {
442 list_del(&pos->node);
447 void map_groups__exit(struct map_groups *mg)
451 for (i = 0; i < MAP__NR_TYPES; ++i) {
452 maps__delete(&mg->maps[i]);
453 maps__delete_removed(&mg->removed_maps[i]);
457 struct map_groups *map_groups__new(void)
459 struct map_groups *mg = malloc(sizeof(*mg));
462 map_groups__init(mg);
467 void map_groups__delete(struct map_groups *mg)
469 map_groups__exit(mg);
473 void map_groups__put(struct map_groups *mg)
475 if (--mg->refcnt == 0)
476 map_groups__delete(mg);
479 void map_groups__flush(struct map_groups *mg)
483 for (type = 0; type < MAP__NR_TYPES; type++) {
484 struct rb_root *root = &mg->maps[type];
485 struct rb_node *next = rb_first(root);
488 struct map *pos = rb_entry(next, struct map, rb_node);
489 next = rb_next(&pos->rb_node);
490 rb_erase(&pos->rb_node, root);
492 * We may have references to this map, for
493 * instance in some hist_entry instances, so
494 * just move them to a separate list.
496 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
501 struct symbol *map_groups__find_symbol(struct map_groups *mg,
502 enum map_type type, u64 addr,
504 symbol_filter_t filter)
506 struct map *map = map_groups__find(mg, type, addr);
508 /* Ensure map is loaded before using map->map_ip */
509 if (map != NULL && map__load(map, filter) >= 0) {
512 return map__find_symbol(map, map->map_ip(map, addr), filter);
518 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
522 symbol_filter_t filter)
526 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
527 struct map *pos = rb_entry(nd, struct map, rb_node);
528 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
540 int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
542 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
543 if (ams->map->groups == NULL)
545 ams->map = map_groups__find(ams->map->groups, ams->map->type,
547 if (ams->map == NULL)
551 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
552 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
554 return ams->sym ? 0 : -1;
557 size_t __map_groups__fprintf_maps(struct map_groups *mg,
558 enum map_type type, int verbose, FILE *fp)
560 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
563 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
564 struct map *pos = rb_entry(nd, struct map, rb_node);
565 printed += fprintf(fp, "Map:");
566 printed += map__fprintf(pos, fp);
568 printed += dso__fprintf(pos->dso, type, fp);
569 printed += fprintf(fp, "--\n");
576 size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
578 size_t printed = 0, i;
579 for (i = 0; i < MAP__NR_TYPES; ++i)
580 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
584 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
586 int verbose, FILE *fp)
591 list_for_each_entry(pos, &mg->removed_maps[type], node) {
592 printed += fprintf(fp, "Map:");
593 printed += map__fprintf(pos, fp);
595 printed += dso__fprintf(pos->dso, type, fp);
596 printed += fprintf(fp, "--\n");
602 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
603 int verbose, FILE *fp)
605 size_t printed = 0, i;
606 for (i = 0; i < MAP__NR_TYPES; ++i)
607 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
611 size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
613 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
614 printed += fprintf(fp, "Removed maps:\n");
615 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
618 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
619 int verbose, FILE *fp)
621 struct rb_root *root = &mg->maps[map->type];
622 struct rb_node *next = rb_first(root);
626 struct map *pos = rb_entry(next, struct map, rb_node);
627 next = rb_next(&pos->rb_node);
629 if (!map__overlap(pos, map))
633 fputs("overlapping maps:\n", fp);
634 map__fprintf(map, fp);
635 map__fprintf(pos, fp);
638 rb_erase(&pos->rb_node, root);
640 * Now check if we need to create new maps for areas not
641 * overlapped by the new map:
643 if (map->start > pos->start) {
644 struct map *before = map__clone(pos);
646 if (before == NULL) {
651 before->end = map->start - 1;
652 map_groups__insert(mg, before);
654 map__fprintf(before, fp);
657 if (map->end < pos->end) {
658 struct map *after = map__clone(pos);
665 after->start = map->end + 1;
666 map_groups__insert(mg, after);
668 map__fprintf(after, fp);
672 * If we have references, just move them to a separate list.
675 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
687 * XXX This should not really _copy_ te maps, but refcount them.
689 int map_groups__clone(struct map_groups *mg,
690 struct map_groups *parent, enum map_type type)
693 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
694 struct map *map = rb_entry(nd, struct map, rb_node);
695 struct map *new = map__clone(map);
698 map_groups__insert(mg, new);
703 void maps__insert(struct rb_root *maps, struct map *map)
705 struct rb_node **p = &maps->rb_node;
706 struct rb_node *parent = NULL;
707 const u64 ip = map->start;
712 m = rb_entry(parent, struct map, rb_node);
719 rb_link_node(&map->rb_node, parent, p);
720 rb_insert_color(&map->rb_node, maps);
723 void maps__remove(struct rb_root *maps, struct map *map)
725 rb_erase(&map->rb_node, maps);
728 struct map *maps__find(struct rb_root *maps, u64 ip)
730 struct rb_node **p = &maps->rb_node;
731 struct rb_node *parent = NULL;
736 m = rb_entry(parent, struct map, rb_node);
739 else if (ip > m->end)
748 struct map *maps__first(struct rb_root *maps)
750 struct rb_node *first = rb_first(maps);
753 return rb_entry(first, struct map, rb_node);
757 struct map *maps__next(struct map *map)
759 struct rb_node *next = rb_next(&map->rb_node);
762 return rb_entry(next, struct map, rb_node);