9 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
18 #include <linux/string.h>
22 static void __maps__insert(struct maps *maps, struct map *map);
24 const char *map_type__name[MAP__NR_TYPES] = {
25 [MAP__FUNCTION] = "Functions",
26 [MAP__VARIABLE] = "Variables",
29 static inline int is_anon_memory(const char *filename, u32 flags)
31 return flags & MAP_HUGETLB ||
32 !strcmp(filename, "//anon") ||
33 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
34 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
37 static inline int is_no_dso_memory(const char *filename)
39 return !strncmp(filename, "[stack", 6) ||
40 !strncmp(filename, "/SYSV",5) ||
41 !strcmp(filename, "[heap]");
44 static inline int is_android_lib(const char *filename)
46 return !strncmp(filename, "/data/app-lib", 13) ||
47 !strncmp(filename, "/system/lib", 11);
50 static inline bool replace_android_lib(const char *filename, char *newfilename)
54 size_t app_abi_length, new_length;
55 size_t lib_length = 0;
57 libname = strrchr(filename, '/');
59 lib_length = strlen(libname);
61 app_abi = getenv("APP_ABI");
65 app_abi_length = strlen(app_abi);
67 if (!strncmp(filename, "/data/app-lib", 13)) {
73 new_length = 7 + app_abi_length + lib_length;
75 apk_path = getenv("APK_PATH");
77 new_length += strlen(apk_path) + 1;
78 if (new_length > PATH_MAX)
80 snprintf(newfilename, new_length,
81 "%s/libs/%s/%s", apk_path, app_abi, libname);
83 if (new_length > PATH_MAX)
85 snprintf(newfilename, new_length,
86 "libs/%s/%s", app_abi, libname);
91 if (!strncmp(filename, "/system/lib/", 11)) {
97 ndk = getenv("NDK_ROOT");
98 app = getenv("APP_PLATFORM");
103 ndk_length = strlen(ndk);
104 app_length = strlen(app);
106 if (!(ndk_length && app_length && app_abi_length))
109 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
110 !strncmp(app_abi, "mips", 4) ? "mips" :
111 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
116 new_length = 27 + ndk_length +
117 app_length + lib_length
120 if (new_length > PATH_MAX)
122 snprintf(newfilename, new_length,
123 "%s/platforms/%s/arch-%s/usr/lib/%s",
124 ndk, app, arch, libname);
131 void map__init(struct map *map, enum map_type type,
132 u64 start, u64 end, u64 pgoff, struct dso *dso)
139 map->dso = dso__get(dso);
140 map->map_ip = map__map_ip;
141 map->unmap_ip = map__unmap_ip;
142 RB_CLEAR_NODE(&map->rb_node);
144 map->erange_warned = false;
145 refcount_set(&map->refcnt, 1);
148 struct map *map__new(struct machine *machine, u64 start, u64 len,
149 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
150 u64 ino_gen, u32 prot, u32 flags, char *filename,
151 enum map_type type, struct thread *thread)
153 struct map *map = malloc(sizeof(*map));
156 char newfilename[PATH_MAX];
158 int anon, no_dso, vdso, android;
160 android = is_android_lib(filename);
161 anon = is_anon_memory(filename, flags);
162 vdso = is_vdso_map(filename);
163 no_dso = is_no_dso_memory(filename);
168 map->ino_generation = ino_gen;
172 if ((anon || no_dso) && type == MAP__FUNCTION) {
173 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
174 filename = newfilename;
178 if (replace_android_lib(filename, newfilename))
179 filename = newfilename;
184 dso = machine__findnew_vdso(machine, thread);
186 dso = machine__findnew_dso(machine, filename);
191 map__init(map, type, start, start + len, pgoff, dso);
193 if (anon || no_dso) {
194 map->map_ip = map->unmap_ip = identity__map_ip;
197 * Set memory without DSO as loaded. All map__find_*
198 * functions still return NULL, and we avoid the
199 * unnecessary map__load warning.
201 if (type != MAP__FUNCTION)
202 dso__set_loaded(dso, map->type);
213 * Constructor variant for modules (where we know from /proc/modules where
214 * they are loaded) and for vmlinux, where only after we load all the
215 * symbols we'll know where it starts and ends.
217 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
219 struct map *map = calloc(1, (sizeof(*map) +
220 (dso->kernel ? sizeof(struct kmap) : 0)));
223 * ->end will be filled after we load all the symbols
225 map__init(map, type, start, 0, 0, dso);
232 * Use this and __map__is_kmodule() for map instances that are in
233 * machine->kmaps, and thus have map->groups->machine all properly set, to
234 * disambiguate between the kernel and modules.
236 * When the need arises, introduce map__is_{kernel,kmodule)() that
237 * checks (map->groups != NULL && map->groups->machine != NULL &&
238 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
240 bool __map__is_kernel(const struct map *map)
242 return __machine__kernel_map(map->groups->machine, map->type) == map;
245 static void map__exit(struct map *map)
247 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
251 void map__delete(struct map *map)
257 void map__put(struct map *map)
259 if (map && refcount_dec_and_test(&map->refcnt))
263 void map__fixup_start(struct map *map)
265 struct rb_root *symbols = &map->dso->symbols[map->type];
266 struct rb_node *nd = rb_first(symbols);
268 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
269 map->start = sym->start;
273 void map__fixup_end(struct map *map)
275 struct rb_root *symbols = &map->dso->symbols[map->type];
276 struct rb_node *nd = rb_last(symbols);
278 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
283 #define DSO__DELETED "(deleted)"
285 int map__load(struct map *map)
287 const char *name = map->dso->long_name;
290 if (dso__loaded(map->dso, map->type))
293 nr = dso__load(map->dso, map);
295 if (map->dso->has_build_id) {
296 char sbuild_id[SBUILD_ID_SIZE];
298 build_id__sprintf(map->dso->build_id,
299 sizeof(map->dso->build_id),
301 pr_warning("%s with build id %s not found",
304 pr_warning("Failed to open %s", name);
306 pr_warning(", continuing without symbols\n");
308 } else if (nr == 0) {
309 #ifdef HAVE_LIBELF_SUPPORT
310 const size_t len = strlen(name);
311 const size_t real_len = len - sizeof(DSO__DELETED);
313 if (len > sizeof(DSO__DELETED) &&
314 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
315 pr_warning("%.*s was updated (is prelink enabled?). "
316 "Restart the long running apps that use it!\n",
317 (int)real_len, name);
319 pr_warning("no symbols found in %s, maybe install "
320 "a debug package?\n", name);
329 int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
331 return strcmp(namea, nameb);
334 struct symbol *map__find_symbol(struct map *map, u64 addr)
336 if (map__load(map) < 0)
339 return dso__find_symbol(map->dso, map->type, addr);
342 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
344 if (map__load(map) < 0)
347 if (!dso__sorted_by_name(map->dso, map->type))
348 dso__sort_by_name(map->dso, map->type);
350 return dso__find_symbol_by_name(map->dso, map->type, name);
353 struct map *map__clone(struct map *from)
355 struct map *map = memdup(from, sizeof(*map));
358 refcount_set(&map->refcnt, 1);
359 RB_CLEAR_NODE(&map->rb_node);
367 int map__overlap(struct map *l, struct map *r)
369 if (l->start > r->start) {
375 if (l->end > r->start)
381 size_t map__fprintf(struct map *map, FILE *fp)
383 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
384 map->start, map->end, map->pgoff, map->dso->name);
387 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
389 const char *dsoname = "[unknown]";
391 if (map && map->dso) {
392 if (symbol_conf.show_kernel_path && map->dso->long_name)
393 dsoname = map->dso->long_name;
395 dsoname = map->dso->name;
398 return fprintf(fp, "%s", dsoname);
401 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
407 if (map && map->dso) {
408 srcline = get_srcline(map->dso,
409 map__rip_2objdump(map, addr), NULL,
411 if (srcline != SRCLINE_UNKNOWN)
412 ret = fprintf(fp, "%s%s", prefix, srcline);
413 free_srcline(srcline);
419 * map__rip_2objdump - convert symbol start address to objdump address.
421 * @rip: symbol start address
423 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
424 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
425 * relative to section start.
427 * Return: Address suitable for passing to "objdump --start-address="
429 u64 map__rip_2objdump(struct map *map, u64 rip)
431 if (!map->dso->adjust_symbols)
435 return rip - map->pgoff;
438 * kernel modules also have DSO_TYPE_USER in dso->kernel,
439 * but all kernel modules are ET_REL, so won't get here.
441 if (map->dso->kernel == DSO_TYPE_USER)
442 return rip + map->dso->text_offset;
444 return map->unmap_ip(map, rip) - map->reloc;
448 * map__objdump_2mem - convert objdump address to a memory address.
450 * @ip: objdump address
452 * Closely related to map__rip_2objdump(), this function takes an address from
453 * objdump and converts it to a memory address. Note this assumes that @map
454 * contains the address. To be sure the result is valid, check it forwards
455 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
457 * Return: Memory address.
459 u64 map__objdump_2mem(struct map *map, u64 ip)
461 if (!map->dso->adjust_symbols)
462 return map->unmap_ip(map, ip);
465 return map->unmap_ip(map, ip + map->pgoff);
468 * kernel modules also have DSO_TYPE_USER in dso->kernel,
469 * but all kernel modules are ET_REL, so won't get here.
471 if (map->dso->kernel == DSO_TYPE_USER)
472 return map->unmap_ip(map, ip - map->dso->text_offset);
474 return ip + map->reloc;
477 static void maps__init(struct maps *maps)
479 maps->entries = RB_ROOT;
480 pthread_rwlock_init(&maps->lock, NULL);
483 void map_groups__init(struct map_groups *mg, struct machine *machine)
486 for (i = 0; i < MAP__NR_TYPES; ++i) {
487 maps__init(&mg->maps[i]);
489 mg->machine = machine;
490 refcount_set(&mg->refcnt, 1);
493 static void __maps__purge(struct maps *maps)
495 struct rb_root *root = &maps->entries;
496 struct rb_node *next = rb_first(root);
499 struct map *pos = rb_entry(next, struct map, rb_node);
501 next = rb_next(&pos->rb_node);
502 rb_erase_init(&pos->rb_node, root);
507 static void maps__exit(struct maps *maps)
509 pthread_rwlock_wrlock(&maps->lock);
511 pthread_rwlock_unlock(&maps->lock);
514 void map_groups__exit(struct map_groups *mg)
518 for (i = 0; i < MAP__NR_TYPES; ++i)
519 maps__exit(&mg->maps[i]);
522 bool map_groups__empty(struct map_groups *mg)
526 for (i = 0; i < MAP__NR_TYPES; ++i) {
527 if (maps__first(&mg->maps[i]))
534 struct map_groups *map_groups__new(struct machine *machine)
536 struct map_groups *mg = malloc(sizeof(*mg));
539 map_groups__init(mg, machine);
544 void map_groups__delete(struct map_groups *mg)
546 map_groups__exit(mg);
550 void map_groups__put(struct map_groups *mg)
552 if (mg && refcount_dec_and_test(&mg->refcnt))
553 map_groups__delete(mg);
556 struct symbol *map_groups__find_symbol(struct map_groups *mg,
557 enum map_type type, u64 addr,
560 struct map *map = map_groups__find(mg, type, addr);
562 /* Ensure map is loaded before using map->map_ip */
563 if (map != NULL && map__load(map) >= 0) {
566 return map__find_symbol(map, map->map_ip(map, addr));
572 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
578 pthread_rwlock_rdlock(&maps->lock);
580 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
581 struct map *pos = rb_entry(nd, struct map, rb_node);
583 sym = map__find_symbol_by_name(pos, name);
594 pthread_rwlock_unlock(&maps->lock);
598 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
603 struct symbol *sym = maps__find_symbol_by_name(&mg->maps[type], name, mapp);
608 int map_groups__find_ams(struct addr_map_symbol *ams)
610 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
611 if (ams->map->groups == NULL)
613 ams->map = map_groups__find(ams->map->groups, ams->map->type,
615 if (ams->map == NULL)
619 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
620 ams->sym = map__find_symbol(ams->map, ams->al_addr);
622 return ams->sym ? 0 : -1;
625 static size_t maps__fprintf(struct maps *maps, FILE *fp)
630 pthread_rwlock_rdlock(&maps->lock);
632 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
633 struct map *pos = rb_entry(nd, struct map, rb_node);
634 printed += fprintf(fp, "Map:");
635 printed += map__fprintf(pos, fp);
637 printed += dso__fprintf(pos->dso, pos->type, fp);
638 printed += fprintf(fp, "--\n");
642 pthread_rwlock_unlock(&maps->lock);
647 size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
650 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
651 return printed += maps__fprintf(&mg->maps[type], fp);
654 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
656 size_t printed = 0, i;
657 for (i = 0; i < MAP__NR_TYPES; ++i)
658 printed += __map_groups__fprintf_maps(mg, i, fp);
662 static void __map_groups__insert(struct map_groups *mg, struct map *map)
664 __maps__insert(&mg->maps[map->type], map);
668 static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
670 struct rb_root *root;
671 struct rb_node *next;
674 pthread_rwlock_wrlock(&maps->lock);
676 root = &maps->entries;
677 next = rb_first(root);
680 struct map *pos = rb_entry(next, struct map, rb_node);
681 next = rb_next(&pos->rb_node);
683 if (!map__overlap(pos, map))
689 pr_warning("overlapping maps in %s "
690 "(disable tui for more info)\n",
693 fputs("overlapping maps:\n", fp);
694 map__fprintf(map, fp);
695 map__fprintf(pos, fp);
699 rb_erase_init(&pos->rb_node, root);
701 * Now check if we need to create new maps for areas not
702 * overlapped by the new map:
704 if (map->start > pos->start) {
705 struct map *before = map__clone(pos);
707 if (before == NULL) {
712 before->end = map->start;
713 __map_groups__insert(pos->groups, before);
714 if (verbose >= 2 && !use_browser)
715 map__fprintf(before, fp);
719 if (map->end < pos->end) {
720 struct map *after = map__clone(pos);
727 after->start = map->end;
728 __map_groups__insert(pos->groups, after);
729 if (verbose >= 2 && !use_browser)
730 map__fprintf(after, fp);
742 pthread_rwlock_unlock(&maps->lock);
746 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
749 return maps__fixup_overlappings(&mg->maps[map->type], map, fp);
753 * XXX This should not really _copy_ te maps, but refcount them.
755 int map_groups__clone(struct thread *thread,
756 struct map_groups *parent, enum map_type type)
758 struct map_groups *mg = thread->mg;
761 struct maps *maps = &parent->maps[type];
763 pthread_rwlock_rdlock(&maps->lock);
765 for (map = maps__first(maps); map; map = map__next(map)) {
766 struct map *new = map__clone(map);
770 err = unwind__prepare_access(thread, new, NULL);
774 map_groups__insert(mg, new);
780 pthread_rwlock_unlock(&maps->lock);
784 static void __maps__insert(struct maps *maps, struct map *map)
786 struct rb_node **p = &maps->entries.rb_node;
787 struct rb_node *parent = NULL;
788 const u64 ip = map->start;
793 m = rb_entry(parent, struct map, rb_node);
800 rb_link_node(&map->rb_node, parent, p);
801 rb_insert_color(&map->rb_node, &maps->entries);
805 void maps__insert(struct maps *maps, struct map *map)
807 pthread_rwlock_wrlock(&maps->lock);
808 __maps__insert(maps, map);
809 pthread_rwlock_unlock(&maps->lock);
812 static void __maps__remove(struct maps *maps, struct map *map)
814 rb_erase_init(&map->rb_node, &maps->entries);
818 void maps__remove(struct maps *maps, struct map *map)
820 pthread_rwlock_wrlock(&maps->lock);
821 __maps__remove(maps, map);
822 pthread_rwlock_unlock(&maps->lock);
825 struct map *maps__find(struct maps *maps, u64 ip)
827 struct rb_node **p, *parent = NULL;
830 pthread_rwlock_rdlock(&maps->lock);
832 p = &maps->entries.rb_node;
835 m = rb_entry(parent, struct map, rb_node);
838 else if (ip >= m->end)
846 pthread_rwlock_unlock(&maps->lock);
850 struct map *maps__first(struct maps *maps)
852 struct rb_node *first = rb_first(&maps->entries);
855 return rb_entry(first, struct map, rb_node);
859 struct map *map__next(struct map *map)
861 struct rb_node *next = rb_next(&map->rb_node);
864 return rb_entry(next, struct map, rb_node);
868 struct kmap *map__kmap(struct map *map)
870 if (!map->dso || !map->dso->kernel) {
871 pr_err("Internal error: map__kmap with a non-kernel map\n");
874 return (struct kmap *)(map + 1);
877 struct map_groups *map__kmaps(struct map *map)
879 struct kmap *kmap = map__kmap(map);
881 if (!kmap || !kmap->kmaps) {
882 pr_err("Internal error: map__kmaps with a non-kernel map\n");