9 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
17 #include <linux/string.h>
19 #include "namespaces.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 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));
154 struct nsinfo *nsi = NULL;
158 char newfilename[PATH_MAX];
160 int anon, no_dso, vdso, android;
162 android = is_android_lib(filename);
163 anon = is_anon_memory(filename, flags);
164 vdso = is_vdso_map(filename);
165 no_dso = is_no_dso_memory(filename);
170 map->ino_generation = ino_gen;
173 nsi = nsinfo__get(thread->nsinfo);
175 if ((anon || no_dso) && nsi && type == MAP__FUNCTION) {
176 snprintf(newfilename, sizeof(newfilename),
177 "/tmp/perf-%d.map", nsi->pid);
178 filename = newfilename;
182 if (replace_android_lib(filename, newfilename))
183 filename = newfilename;
187 /* The vdso maps are always on the host and not the
188 * container. Ensure that we don't use setns to look
191 nnsi = nsinfo__copy(nsi);
194 nnsi->need_setns = false;
198 dso = machine__findnew_vdso(machine, thread);
200 dso = machine__findnew_dso(machine, filename);
205 map__init(map, type, start, start + len, pgoff, dso);
207 if (anon || no_dso) {
208 map->map_ip = map->unmap_ip = identity__map_ip;
211 * Set memory without DSO as loaded. All map__find_*
212 * functions still return NULL, and we avoid the
213 * unnecessary map__load warning.
215 if (type != MAP__FUNCTION)
216 dso__set_loaded(dso, map->type);
229 * Constructor variant for modules (where we know from /proc/modules where
230 * they are loaded) and for vmlinux, where only after we load all the
231 * symbols we'll know where it starts and ends.
233 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
235 struct map *map = calloc(1, (sizeof(*map) +
236 (dso->kernel ? sizeof(struct kmap) : 0)));
239 * ->end will be filled after we load all the symbols
241 map__init(map, type, start, 0, 0, dso);
248 * Use this and __map__is_kmodule() for map instances that are in
249 * machine->kmaps, and thus have map->groups->machine all properly set, to
250 * disambiguate between the kernel and modules.
252 * When the need arises, introduce map__is_{kernel,kmodule)() that
253 * checks (map->groups != NULL && map->groups->machine != NULL &&
254 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
256 bool __map__is_kernel(const struct map *map)
258 return __machine__kernel_map(map->groups->machine, map->type) == map;
261 static void map__exit(struct map *map)
263 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
267 void map__delete(struct map *map)
273 void map__put(struct map *map)
275 if (map && refcount_dec_and_test(&map->refcnt))
279 void map__fixup_start(struct map *map)
281 struct rb_root *symbols = &map->dso->symbols[map->type];
282 struct rb_node *nd = rb_first(symbols);
284 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
285 map->start = sym->start;
289 void map__fixup_end(struct map *map)
291 struct rb_root *symbols = &map->dso->symbols[map->type];
292 struct rb_node *nd = rb_last(symbols);
294 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
299 #define DSO__DELETED "(deleted)"
301 int map__load(struct map *map)
303 const char *name = map->dso->long_name;
306 if (dso__loaded(map->dso, map->type))
309 nr = dso__load(map->dso, map);
311 if (map->dso->has_build_id) {
312 char sbuild_id[SBUILD_ID_SIZE];
314 build_id__sprintf(map->dso->build_id,
315 sizeof(map->dso->build_id),
317 pr_warning("%s with build id %s not found",
320 pr_warning("Failed to open %s", name);
322 pr_warning(", continuing without symbols\n");
324 } else if (nr == 0) {
325 #ifdef HAVE_LIBELF_SUPPORT
326 const size_t len = strlen(name);
327 const size_t real_len = len - sizeof(DSO__DELETED);
329 if (len > sizeof(DSO__DELETED) &&
330 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
331 pr_warning("%.*s was updated (is prelink enabled?). "
332 "Restart the long running apps that use it!\n",
333 (int)real_len, name);
335 pr_warning("no symbols found in %s, maybe install "
336 "a debug package?\n", name);
345 struct symbol *map__find_symbol(struct map *map, u64 addr)
347 if (map__load(map) < 0)
350 return dso__find_symbol(map->dso, map->type, addr);
353 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
355 if (map__load(map) < 0)
358 if (!dso__sorted_by_name(map->dso, map->type))
359 dso__sort_by_name(map->dso, map->type);
361 return dso__find_symbol_by_name(map->dso, map->type, name);
364 struct map *map__clone(struct map *from)
366 struct map *map = memdup(from, sizeof(*map));
369 refcount_set(&map->refcnt, 1);
370 RB_CLEAR_NODE(&map->rb_node);
378 int map__overlap(struct map *l, struct map *r)
380 if (l->start > r->start) {
386 if (l->end > r->start)
392 size_t map__fprintf(struct map *map, FILE *fp)
394 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
395 map->start, map->end, map->pgoff, map->dso->name);
398 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
400 const char *dsoname = "[unknown]";
402 if (map && map->dso) {
403 if (symbol_conf.show_kernel_path && map->dso->long_name)
404 dsoname = map->dso->long_name;
406 dsoname = map->dso->name;
409 return fprintf(fp, "%s", dsoname);
412 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
418 if (map && map->dso) {
419 srcline = get_srcline(map->dso,
420 map__rip_2objdump(map, addr), NULL,
422 if (srcline != SRCLINE_UNKNOWN)
423 ret = fprintf(fp, "%s%s", prefix, srcline);
424 free_srcline(srcline);
430 * map__rip_2objdump - convert symbol start address to objdump address.
432 * @rip: symbol start address
434 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
435 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
436 * relative to section start.
438 * Return: Address suitable for passing to "objdump --start-address="
440 u64 map__rip_2objdump(struct map *map, u64 rip)
442 if (!map->dso->adjust_symbols)
446 return rip - map->pgoff;
449 * kernel modules also have DSO_TYPE_USER in dso->kernel,
450 * but all kernel modules are ET_REL, so won't get here.
452 if (map->dso->kernel == DSO_TYPE_USER)
453 return rip + map->dso->text_offset;
455 return map->unmap_ip(map, rip) - map->reloc;
459 * map__objdump_2mem - convert objdump address to a memory address.
461 * @ip: objdump address
463 * Closely related to map__rip_2objdump(), this function takes an address from
464 * objdump and converts it to a memory address. Note this assumes that @map
465 * contains the address. To be sure the result is valid, check it forwards
466 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
468 * Return: Memory address.
470 u64 map__objdump_2mem(struct map *map, u64 ip)
472 if (!map->dso->adjust_symbols)
473 return map->unmap_ip(map, ip);
476 return map->unmap_ip(map, ip + map->pgoff);
479 * kernel modules also have DSO_TYPE_USER in dso->kernel,
480 * but all kernel modules are ET_REL, so won't get here.
482 if (map->dso->kernel == DSO_TYPE_USER)
483 return map->unmap_ip(map, ip - map->dso->text_offset);
485 return ip + map->reloc;
488 static void maps__init(struct maps *maps)
490 maps->entries = RB_ROOT;
491 pthread_rwlock_init(&maps->lock, NULL);
494 void map_groups__init(struct map_groups *mg, struct machine *machine)
497 for (i = 0; i < MAP__NR_TYPES; ++i) {
498 maps__init(&mg->maps[i]);
500 mg->machine = machine;
501 refcount_set(&mg->refcnt, 1);
504 static void __maps__purge(struct maps *maps)
506 struct rb_root *root = &maps->entries;
507 struct rb_node *next = rb_first(root);
510 struct map *pos = rb_entry(next, struct map, rb_node);
512 next = rb_next(&pos->rb_node);
513 rb_erase_init(&pos->rb_node, root);
518 static void maps__exit(struct maps *maps)
520 pthread_rwlock_wrlock(&maps->lock);
522 pthread_rwlock_unlock(&maps->lock);
525 void map_groups__exit(struct map_groups *mg)
529 for (i = 0; i < MAP__NR_TYPES; ++i)
530 maps__exit(&mg->maps[i]);
533 bool map_groups__empty(struct map_groups *mg)
537 for (i = 0; i < MAP__NR_TYPES; ++i) {
538 if (maps__first(&mg->maps[i]))
545 struct map_groups *map_groups__new(struct machine *machine)
547 struct map_groups *mg = malloc(sizeof(*mg));
550 map_groups__init(mg, machine);
555 void map_groups__delete(struct map_groups *mg)
557 map_groups__exit(mg);
561 void map_groups__put(struct map_groups *mg)
563 if (mg && refcount_dec_and_test(&mg->refcnt))
564 map_groups__delete(mg);
567 struct symbol *map_groups__find_symbol(struct map_groups *mg,
568 enum map_type type, u64 addr,
571 struct map *map = map_groups__find(mg, type, addr);
573 /* Ensure map is loaded before using map->map_ip */
574 if (map != NULL && map__load(map) >= 0) {
577 return map__find_symbol(map, map->map_ip(map, addr));
583 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
589 pthread_rwlock_rdlock(&maps->lock);
591 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
592 struct map *pos = rb_entry(nd, struct map, rb_node);
594 sym = map__find_symbol_by_name(pos, name);
605 pthread_rwlock_unlock(&maps->lock);
609 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
614 struct symbol *sym = maps__find_symbol_by_name(&mg->maps[type], name, mapp);
619 int map_groups__find_ams(struct addr_map_symbol *ams)
621 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
622 if (ams->map->groups == NULL)
624 ams->map = map_groups__find(ams->map->groups, ams->map->type,
626 if (ams->map == NULL)
630 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
631 ams->sym = map__find_symbol(ams->map, ams->al_addr);
633 return ams->sym ? 0 : -1;
636 static size_t maps__fprintf(struct maps *maps, FILE *fp)
641 pthread_rwlock_rdlock(&maps->lock);
643 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
644 struct map *pos = rb_entry(nd, struct map, rb_node);
645 printed += fprintf(fp, "Map:");
646 printed += map__fprintf(pos, fp);
648 printed += dso__fprintf(pos->dso, pos->type, fp);
649 printed += fprintf(fp, "--\n");
653 pthread_rwlock_unlock(&maps->lock);
658 size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
661 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
662 return printed += maps__fprintf(&mg->maps[type], fp);
665 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
667 size_t printed = 0, i;
668 for (i = 0; i < MAP__NR_TYPES; ++i)
669 printed += __map_groups__fprintf_maps(mg, i, fp);
673 static void __map_groups__insert(struct map_groups *mg, struct map *map)
675 __maps__insert(&mg->maps[map->type], map);
679 static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
681 struct rb_root *root;
682 struct rb_node *next;
685 pthread_rwlock_wrlock(&maps->lock);
687 root = &maps->entries;
688 next = rb_first(root);
691 struct map *pos = rb_entry(next, struct map, rb_node);
692 next = rb_next(&pos->rb_node);
694 if (!map__overlap(pos, map))
700 pr_warning("overlapping maps in %s "
701 "(disable tui for more info)\n",
704 fputs("overlapping maps:\n", fp);
705 map__fprintf(map, fp);
706 map__fprintf(pos, fp);
710 rb_erase_init(&pos->rb_node, root);
712 * Now check if we need to create new maps for areas not
713 * overlapped by the new map:
715 if (map->start > pos->start) {
716 struct map *before = map__clone(pos);
718 if (before == NULL) {
723 before->end = map->start;
724 __map_groups__insert(pos->groups, before);
725 if (verbose >= 2 && !use_browser)
726 map__fprintf(before, fp);
730 if (map->end < pos->end) {
731 struct map *after = map__clone(pos);
738 after->start = map->end;
739 __map_groups__insert(pos->groups, after);
740 if (verbose >= 2 && !use_browser)
741 map__fprintf(after, fp);
753 pthread_rwlock_unlock(&maps->lock);
757 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
760 return maps__fixup_overlappings(&mg->maps[map->type], map, fp);
764 * XXX This should not really _copy_ te maps, but refcount them.
766 int map_groups__clone(struct thread *thread,
767 struct map_groups *parent, enum map_type type)
769 struct map_groups *mg = thread->mg;
772 struct maps *maps = &parent->maps[type];
774 pthread_rwlock_rdlock(&maps->lock);
776 for (map = maps__first(maps); map; map = map__next(map)) {
777 struct map *new = map__clone(map);
781 err = unwind__prepare_access(thread, new, NULL);
785 map_groups__insert(mg, new);
791 pthread_rwlock_unlock(&maps->lock);
795 static void __maps__insert(struct maps *maps, struct map *map)
797 struct rb_node **p = &maps->entries.rb_node;
798 struct rb_node *parent = NULL;
799 const u64 ip = map->start;
804 m = rb_entry(parent, struct map, rb_node);
811 rb_link_node(&map->rb_node, parent, p);
812 rb_insert_color(&map->rb_node, &maps->entries);
816 void maps__insert(struct maps *maps, struct map *map)
818 pthread_rwlock_wrlock(&maps->lock);
819 __maps__insert(maps, map);
820 pthread_rwlock_unlock(&maps->lock);
823 static void __maps__remove(struct maps *maps, struct map *map)
825 rb_erase_init(&map->rb_node, &maps->entries);
829 void maps__remove(struct maps *maps, struct map *map)
831 pthread_rwlock_wrlock(&maps->lock);
832 __maps__remove(maps, map);
833 pthread_rwlock_unlock(&maps->lock);
836 struct map *maps__find(struct maps *maps, u64 ip)
838 struct rb_node **p, *parent = NULL;
841 pthread_rwlock_rdlock(&maps->lock);
843 p = &maps->entries.rb_node;
846 m = rb_entry(parent, struct map, rb_node);
849 else if (ip >= m->end)
857 pthread_rwlock_unlock(&maps->lock);
861 struct map *maps__first(struct maps *maps)
863 struct rb_node *first = rb_first(&maps->entries);
866 return rb_entry(first, struct map, rb_node);
870 struct map *map__next(struct map *map)
872 struct rb_node *next = rb_next(&map->rb_node);
875 return rb_entry(next, struct map, rb_node);
879 struct kmap *map__kmap(struct map *map)
881 if (!map->dso || !map->dso->kernel) {
882 pr_err("Internal error: map__kmap with a non-kernel map\n");
885 return (struct kmap *)(map + 1);
888 struct map_groups *map__kmaps(struct map *map)
890 struct kmap *kmap = map__kmap(map);
892 if (!kmap || !kmap->kmaps) {
893 pr_err("Internal error: map__kmaps with a non-kernel map\n");