17 #include <linux/string.h>
19 const char *map_type__name[MAP__NR_TYPES] = {
20 [MAP__FUNCTION] = "Functions",
21 [MAP__VARIABLE] = "Variables",
24 static inline int is_anon_memory(const char *filename)
26 return !strcmp(filename, "//anon") ||
27 !strcmp(filename, "/dev/zero (deleted)") ||
28 !strcmp(filename, "/anon_hugepage (deleted)");
31 static inline int is_no_dso_memory(const char *filename)
33 return !strncmp(filename, "[stack", 6) ||
34 !strcmp(filename, "[heap]");
37 static inline int is_android_lib(const char *filename)
39 return !strncmp(filename, "/data/app-lib", 13) ||
40 !strncmp(filename, "/system/lib", 11);
43 static inline bool replace_android_lib(const char *filename, char *newfilename)
47 size_t app_abi_length, new_length;
48 size_t lib_length = 0;
50 libname = strrchr(filename, '/');
52 lib_length = strlen(libname);
54 app_abi = getenv("APP_ABI");
58 app_abi_length = strlen(app_abi);
60 if (!strncmp(filename, "/data/app-lib", 13)) {
66 new_length = 7 + app_abi_length + lib_length;
68 apk_path = getenv("APK_PATH");
70 new_length += strlen(apk_path) + 1;
71 if (new_length > PATH_MAX)
73 snprintf(newfilename, new_length,
74 "%s/libs/%s/%s", apk_path, app_abi, libname);
76 if (new_length > PATH_MAX)
78 snprintf(newfilename, new_length,
79 "libs/%s/%s", app_abi, libname);
84 if (!strncmp(filename, "/system/lib/", 11)) {
90 ndk = getenv("NDK_ROOT");
91 app = getenv("APP_PLATFORM");
96 ndk_length = strlen(ndk);
97 app_length = strlen(app);
99 if (!(ndk_length && app_length && app_abi_length))
102 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
103 !strncmp(app_abi, "mips", 4) ? "mips" :
104 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
109 new_length = 27 + ndk_length +
110 app_length + lib_length
113 if (new_length > PATH_MAX)
115 snprintf(newfilename, new_length,
116 "%s/platforms/%s/arch-%s/usr/lib/%s",
117 ndk, app, arch, libname);
124 void map__init(struct map *map, enum map_type type,
125 u64 start, u64 end, u64 pgoff, struct dso *dso)
133 map->map_ip = map__map_ip;
134 map->unmap_ip = map__unmap_ip;
135 RB_CLEAR_NODE(&map->rb_node);
137 map->referenced = false;
138 map->erange_warned = false;
141 struct map *map__new(struct machine *machine, u64 start, u64 len,
142 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
143 u64 ino_gen, u32 prot, u32 flags, char *filename,
144 enum map_type type, struct thread *thread)
146 struct map *map = malloc(sizeof(*map));
149 char newfilename[PATH_MAX];
151 int anon, no_dso, vdso, android;
153 android = is_android_lib(filename);
154 anon = is_anon_memory(filename);
155 vdso = is_vdso_map(filename);
156 no_dso = is_no_dso_memory(filename);
161 map->ino_generation = ino_gen;
165 if ((anon || no_dso) && type == MAP__FUNCTION) {
166 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
167 filename = newfilename;
171 if (replace_android_lib(filename, newfilename))
172 filename = newfilename;
177 dso = vdso__dso_findnew(machine, thread);
179 dso = __dsos__findnew(&machine->user_dsos, filename);
184 map__init(map, type, start, start + len, pgoff, dso);
186 if (anon || no_dso) {
187 map->map_ip = map->unmap_ip = identity__map_ip;
190 * Set memory without DSO as loaded. All map__find_*
191 * functions still return NULL, and we avoid the
192 * unnecessary map__load warning.
194 if (type != MAP__FUNCTION)
195 dso__set_loaded(dso, map->type);
205 * Constructor variant for modules (where we know from /proc/modules where
206 * they are loaded) and for vmlinux, where only after we load all the
207 * symbols we'll know where it starts and ends.
209 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
211 struct map *map = calloc(1, (sizeof(*map) +
212 (dso->kernel ? sizeof(struct kmap) : 0)));
215 * ->end will be filled after we load all the symbols
217 map__init(map, type, start, 0, 0, dso);
223 void map__delete(struct map *map)
228 void map__fixup_start(struct map *map)
230 struct rb_root *symbols = &map->dso->symbols[map->type];
231 struct rb_node *nd = rb_first(symbols);
233 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
234 map->start = sym->start;
238 void map__fixup_end(struct map *map)
240 struct rb_root *symbols = &map->dso->symbols[map->type];
241 struct rb_node *nd = rb_last(symbols);
243 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
248 #define DSO__DELETED "(deleted)"
250 int map__load(struct map *map, symbol_filter_t filter)
252 const char *name = map->dso->long_name;
255 if (dso__loaded(map->dso, map->type))
258 nr = dso__load(map->dso, map, filter);
260 if (map->dso->has_build_id) {
261 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
263 build_id__sprintf(map->dso->build_id,
264 sizeof(map->dso->build_id),
266 pr_warning("%s with build id %s not found",
269 pr_warning("Failed to open %s", name);
271 pr_warning(", continuing without symbols\n");
273 } else if (nr == 0) {
274 #ifdef HAVE_LIBELF_SUPPORT
275 const size_t len = strlen(name);
276 const size_t real_len = len - sizeof(DSO__DELETED);
278 if (len > sizeof(DSO__DELETED) &&
279 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
280 pr_warning("%.*s was updated (is prelink enabled?). "
281 "Restart the long running apps that use it!\n",
282 (int)real_len, name);
284 pr_warning("no symbols found in %s, maybe install "
285 "a debug package?\n", name);
294 struct symbol *map__find_symbol(struct map *map, u64 addr,
295 symbol_filter_t filter)
297 if (map__load(map, filter) < 0)
300 return dso__find_symbol(map->dso, map->type, addr);
303 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
304 symbol_filter_t filter)
306 if (map__load(map, filter) < 0)
309 if (!dso__sorted_by_name(map->dso, map->type))
310 dso__sort_by_name(map->dso, map->type);
312 return dso__find_symbol_by_name(map->dso, map->type, name);
315 struct map *map__clone(struct map *map)
317 return memdup(map, sizeof(*map));
320 int map__overlap(struct map *l, struct map *r)
322 if (l->start > r->start) {
328 if (l->end > r->start)
334 size_t map__fprintf(struct map *map, FILE *fp)
336 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
337 map->start, map->end, map->pgoff, map->dso->name);
340 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
342 const char *dsoname = "[unknown]";
344 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
345 if (symbol_conf.show_kernel_path && map->dso->long_name)
346 dsoname = map->dso->long_name;
347 else if (map->dso->name)
348 dsoname = map->dso->name;
351 return fprintf(fp, "%s", dsoname);
354 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
360 if (map && map->dso) {
361 srcline = get_srcline(map->dso,
362 map__rip_2objdump(map, addr));
363 if (srcline != SRCLINE_UNKNOWN)
364 ret = fprintf(fp, "%s%s", prefix, srcline);
365 free_srcline(srcline);
371 * map__rip_2objdump - convert symbol start address to objdump address.
373 * @rip: symbol start address
375 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
376 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
377 * relative to section start.
379 * Return: Address suitable for passing to "objdump --start-address="
381 u64 map__rip_2objdump(struct map *map, u64 rip)
383 if (!map->dso->adjust_symbols)
387 return rip - map->pgoff;
389 return map->unmap_ip(map, rip) - map->reloc;
393 * map__objdump_2mem - convert objdump address to a memory address.
395 * @ip: objdump address
397 * Closely related to map__rip_2objdump(), this function takes an address from
398 * objdump and converts it to a memory address. Note this assumes that @map
399 * contains the address. To be sure the result is valid, check it forwards
400 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
402 * Return: Memory address.
404 u64 map__objdump_2mem(struct map *map, u64 ip)
406 if (!map->dso->adjust_symbols)
407 return map->unmap_ip(map, ip);
410 return map->unmap_ip(map, ip + map->pgoff);
412 return ip + map->reloc;
415 void map_groups__init(struct map_groups *mg)
418 for (i = 0; i < MAP__NR_TYPES; ++i) {
419 mg->maps[i] = RB_ROOT;
420 INIT_LIST_HEAD(&mg->removed_maps[i]);
426 static void maps__delete(struct rb_root *maps)
428 struct rb_node *next = rb_first(maps);
431 struct map *pos = rb_entry(next, struct map, rb_node);
433 next = rb_next(&pos->rb_node);
434 rb_erase(&pos->rb_node, maps);
439 static void maps__delete_removed(struct list_head *maps)
443 list_for_each_entry_safe(pos, n, maps, node) {
444 list_del(&pos->node);
449 void map_groups__exit(struct map_groups *mg)
453 for (i = 0; i < MAP__NR_TYPES; ++i) {
454 maps__delete(&mg->maps[i]);
455 maps__delete_removed(&mg->removed_maps[i]);
459 bool map_groups__empty(struct map_groups *mg)
463 for (i = 0; i < MAP__NR_TYPES; ++i) {
464 if (maps__first(&mg->maps[i]))
466 if (!list_empty(&mg->removed_maps[i]))
473 struct map_groups *map_groups__new(void)
475 struct map_groups *mg = malloc(sizeof(*mg));
478 map_groups__init(mg);
483 void map_groups__delete(struct map_groups *mg)
485 map_groups__exit(mg);
489 void map_groups__put(struct map_groups *mg)
491 if (--mg->refcnt == 0)
492 map_groups__delete(mg);
495 void map_groups__flush(struct map_groups *mg)
499 for (type = 0; type < MAP__NR_TYPES; type++) {
500 struct rb_root *root = &mg->maps[type];
501 struct rb_node *next = rb_first(root);
504 struct map *pos = rb_entry(next, struct map, rb_node);
505 next = rb_next(&pos->rb_node);
506 rb_erase(&pos->rb_node, root);
508 * We may have references to this map, for
509 * instance in some hist_entry instances, so
510 * just move them to a separate list.
512 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
517 struct symbol *map_groups__find_symbol(struct map_groups *mg,
518 enum map_type type, u64 addr,
520 symbol_filter_t filter)
522 struct map *map = map_groups__find(mg, type, addr);
524 /* Ensure map is loaded before using map->map_ip */
525 if (map != NULL && map__load(map, filter) >= 0) {
528 return map__find_symbol(map, map->map_ip(map, addr), filter);
534 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
538 symbol_filter_t filter)
542 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
543 struct map *pos = rb_entry(nd, struct map, rb_node);
544 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
556 int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
558 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
559 if (ams->map->groups == NULL)
561 ams->map = map_groups__find(ams->map->groups, ams->map->type,
563 if (ams->map == NULL)
567 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
568 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
570 return ams->sym ? 0 : -1;
573 size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
576 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
579 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
580 struct map *pos = rb_entry(nd, struct map, rb_node);
581 printed += fprintf(fp, "Map:");
582 printed += map__fprintf(pos, fp);
584 printed += dso__fprintf(pos->dso, type, fp);
585 printed += fprintf(fp, "--\n");
592 static size_t map_groups__fprintf_maps(struct map_groups *mg, FILE *fp)
594 size_t printed = 0, i;
595 for (i = 0; i < MAP__NR_TYPES; ++i)
596 printed += __map_groups__fprintf_maps(mg, i, fp);
600 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
601 enum map_type type, FILE *fp)
606 list_for_each_entry(pos, &mg->removed_maps[type], node) {
607 printed += fprintf(fp, "Map:");
608 printed += map__fprintf(pos, fp);
610 printed += dso__fprintf(pos->dso, type, fp);
611 printed += fprintf(fp, "--\n");
617 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
620 size_t printed = 0, i;
621 for (i = 0; i < MAP__NR_TYPES; ++i)
622 printed += __map_groups__fprintf_removed_maps(mg, i, fp);
626 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
628 size_t printed = map_groups__fprintf_maps(mg, fp);
629 printed += fprintf(fp, "Removed maps:\n");
630 return printed + map_groups__fprintf_removed_maps(mg, fp);
633 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
636 struct rb_root *root = &mg->maps[map->type];
637 struct rb_node *next = rb_first(root);
641 struct map *pos = rb_entry(next, struct map, rb_node);
642 next = rb_next(&pos->rb_node);
644 if (!map__overlap(pos, map))
648 fputs("overlapping maps:\n", fp);
649 map__fprintf(map, fp);
650 map__fprintf(pos, fp);
653 rb_erase(&pos->rb_node, root);
655 * Now check if we need to create new maps for areas not
656 * overlapped by the new map:
658 if (map->start > pos->start) {
659 struct map *before = map__clone(pos);
661 if (before == NULL) {
666 before->end = map->start - 1;
667 map_groups__insert(mg, before);
669 map__fprintf(before, fp);
672 if (map->end < pos->end) {
673 struct map *after = map__clone(pos);
680 after->start = map->end + 1;
681 map_groups__insert(mg, after);
683 map__fprintf(after, fp);
687 * If we have references, just move them to a separate list.
690 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
702 * XXX This should not really _copy_ te maps, but refcount them.
704 int map_groups__clone(struct map_groups *mg,
705 struct map_groups *parent, enum map_type type)
708 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
709 struct map *map = rb_entry(nd, struct map, rb_node);
710 struct map *new = map__clone(map);
713 map_groups__insert(mg, new);
718 void maps__insert(struct rb_root *maps, struct map *map)
720 struct rb_node **p = &maps->rb_node;
721 struct rb_node *parent = NULL;
722 const u64 ip = map->start;
727 m = rb_entry(parent, struct map, rb_node);
734 rb_link_node(&map->rb_node, parent, p);
735 rb_insert_color(&map->rb_node, maps);
738 void maps__remove(struct rb_root *maps, struct map *map)
740 rb_erase(&map->rb_node, maps);
743 struct map *maps__find(struct rb_root *maps, u64 ip)
745 struct rb_node **p = &maps->rb_node;
746 struct rb_node *parent = NULL;
751 m = rb_entry(parent, struct map, rb_node);
754 else if (ip > m->end)
763 struct map *maps__first(struct rb_root *maps)
765 struct rb_node *first = rb_first(maps);
768 return rb_entry(first, struct map, rb_node);
772 struct map *maps__next(struct map *map)
774 struct rb_node *next = rb_next(&map->rb_node);
777 return rb_entry(next, struct map, rb_node);