1 // SPDX-License-Identifier: GPL-2.0
11 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
19 #include <linux/string.h>
21 #include "namespaces.h"
24 static void __maps__insert(struct maps *maps, struct map *map);
26 static inline int is_anon_memory(const char *filename, u32 flags)
28 return flags & MAP_HUGETLB ||
29 !strcmp(filename, "//anon") ||
30 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
31 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
34 static inline int is_no_dso_memory(const char *filename)
36 return !strncmp(filename, "[stack", 6) ||
37 !strncmp(filename, "/SYSV",5) ||
38 !strcmp(filename, "[heap]");
41 static inline int is_android_lib(const char *filename)
43 return !strncmp(filename, "/data/app-lib", 13) ||
44 !strncmp(filename, "/system/lib", 11);
47 static inline bool replace_android_lib(const char *filename, char *newfilename)
51 size_t app_abi_length, new_length;
52 size_t lib_length = 0;
54 libname = strrchr(filename, '/');
56 lib_length = strlen(libname);
58 app_abi = getenv("APP_ABI");
62 app_abi_length = strlen(app_abi);
64 if (!strncmp(filename, "/data/app-lib", 13)) {
70 new_length = 7 + app_abi_length + lib_length;
72 apk_path = getenv("APK_PATH");
74 new_length += strlen(apk_path) + 1;
75 if (new_length > PATH_MAX)
77 snprintf(newfilename, new_length,
78 "%s/libs/%s/%s", apk_path, app_abi, libname);
80 if (new_length > PATH_MAX)
82 snprintf(newfilename, new_length,
83 "libs/%s/%s", app_abi, libname);
88 if (!strncmp(filename, "/system/lib/", 12)) {
91 int ndk_length, app_length;
93 ndk = getenv("NDK_ROOT");
94 app = getenv("APP_PLATFORM");
99 ndk_length = strlen(ndk);
100 app_length = strlen(app);
102 if (!(ndk_length && app_length && app_abi_length))
105 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
106 !strncmp(app_abi, "mips", 4) ? "mips" :
107 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
112 new_length = 27 + ndk_length +
113 app_length + lib_length
116 if (new_length > PATH_MAX)
118 snprintf(newfilename, new_length,
119 "%.*s/platforms/%.*s/arch-%s/usr/lib/%s",
120 ndk_length, ndk, app_length, app, arch, libname);
127 void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
133 map->dso = dso__get(dso);
134 map->map_ip = map__map_ip;
135 map->unmap_ip = map__unmap_ip;
136 RB_CLEAR_NODE(&map->rb_node);
138 map->erange_warned = false;
139 refcount_set(&map->refcnt, 1);
142 struct map *map__new(struct machine *machine, u64 start, u64 len,
143 u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
144 u64 ino_gen, u32 prot, u32 flags, char *filename,
145 struct thread *thread)
147 struct map *map = malloc(sizeof(*map));
148 struct nsinfo *nsi = NULL;
152 char newfilename[PATH_MAX];
154 int anon, no_dso, vdso, android;
156 android = is_android_lib(filename);
157 anon = is_anon_memory(filename, flags);
158 vdso = is_vdso_map(filename);
159 no_dso = is_no_dso_memory(filename);
164 map->ino_generation = ino_gen;
167 nsi = nsinfo__get(thread->nsinfo);
169 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
170 snprintf(newfilename, sizeof(newfilename),
171 "/tmp/perf-%d.map", nsi->pid);
172 filename = newfilename;
176 if (replace_android_lib(filename, newfilename))
177 filename = newfilename;
181 /* The vdso maps are always on the host and not the
182 * container. Ensure that we don't use setns to look
185 nnsi = nsinfo__copy(nsi);
188 nnsi->need_setns = false;
192 dso = machine__findnew_vdso(machine, thread);
194 dso = machine__findnew_dso(machine, filename);
199 map__init(map, start, start + len, pgoff, dso);
201 if (anon || no_dso) {
202 map->map_ip = map->unmap_ip = identity__map_ip;
205 * Set memory without DSO as loaded. All map__find_*
206 * functions still return NULL, and we avoid the
207 * unnecessary map__load warning.
209 if (!(prot & PROT_EXEC))
210 dso__set_loaded(dso);
223 * Constructor variant for modules (where we know from /proc/modules where
224 * they are loaded) and for vmlinux, where only after we load all the
225 * symbols we'll know where it starts and ends.
227 struct map *map__new2(u64 start, struct dso *dso)
229 struct map *map = calloc(1, (sizeof(*map) +
230 (dso->kernel ? sizeof(struct kmap) : 0)));
233 * ->end will be filled after we load all the symbols
235 map__init(map, start, 0, 0, dso);
242 * Use this and __map__is_kmodule() for map instances that are in
243 * machine->kmaps, and thus have map->groups->machine all properly set, to
244 * disambiguate between the kernel and modules.
246 * When the need arises, introduce map__is_{kernel,kmodule)() that
247 * checks (map->groups != NULL && map->groups->machine != NULL &&
248 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
250 bool __map__is_kernel(const struct map *map)
252 return machine__kernel_map(map->groups->machine) == map;
255 bool __map__is_extra_kernel_map(const struct map *map)
257 struct kmap *kmap = __map__kmap((struct map *)map);
259 return kmap && kmap->name[0];
262 bool map__has_symbols(const struct map *map)
264 return dso__has_symbols(map->dso);
267 static void map__exit(struct map *map)
269 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
273 void map__delete(struct map *map)
279 void map__put(struct map *map)
281 if (map && refcount_dec_and_test(&map->refcnt))
285 void map__fixup_start(struct map *map)
287 struct rb_root *symbols = &map->dso->symbols;
288 struct rb_node *nd = rb_first(symbols);
290 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
291 map->start = sym->start;
295 void map__fixup_end(struct map *map)
297 struct rb_root *symbols = &map->dso->symbols;
298 struct rb_node *nd = rb_last(symbols);
300 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
305 #define DSO__DELETED "(deleted)"
307 int map__load(struct map *map)
309 const char *name = map->dso->long_name;
312 if (dso__loaded(map->dso))
315 nr = dso__load(map->dso, map);
317 if (map->dso->has_build_id) {
318 char sbuild_id[SBUILD_ID_SIZE];
320 build_id__sprintf(map->dso->build_id,
321 sizeof(map->dso->build_id),
323 pr_warning("%s with build id %s not found",
326 pr_warning("Failed to open %s", name);
328 pr_warning(", continuing without symbols\n");
330 } else if (nr == 0) {
331 #ifdef HAVE_LIBELF_SUPPORT
332 const size_t len = strlen(name);
333 const size_t real_len = len - sizeof(DSO__DELETED);
335 if (len > sizeof(DSO__DELETED) &&
336 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
337 pr_warning("%.*s was updated (is prelink enabled?). "
338 "Restart the long running apps that use it!\n",
339 (int)real_len, name);
341 pr_warning("no symbols found in %s, maybe install "
342 "a debug package?\n", name);
351 struct symbol *map__find_symbol(struct map *map, u64 addr)
353 if (map__load(map) < 0)
356 return dso__find_symbol(map->dso, addr);
359 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
361 if (map__load(map) < 0)
364 if (!dso__sorted_by_name(map->dso))
365 dso__sort_by_name(map->dso);
367 return dso__find_symbol_by_name(map->dso, name);
370 struct map *map__clone(struct map *from)
372 struct map *map = memdup(from, sizeof(*map));
375 refcount_set(&map->refcnt, 1);
376 RB_CLEAR_NODE(&map->rb_node);
384 size_t map__fprintf(struct map *map, FILE *fp)
386 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
387 map->start, map->end, map->pgoff, map->dso->name);
390 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
392 const char *dsoname = "[unknown]";
394 if (map && map->dso) {
395 if (symbol_conf.show_kernel_path && map->dso->long_name)
396 dsoname = map->dso->long_name;
398 dsoname = map->dso->name;
401 return fprintf(fp, "%s", dsoname);
404 char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
407 return SRCLINE_UNKNOWN;
408 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
411 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
416 if (map && map->dso) {
417 char *srcline = map__srcline(map, addr, NULL);
418 if (srcline != SRCLINE_UNKNOWN)
419 ret = fprintf(fp, "%s%s", prefix, srcline);
420 free_srcline(srcline);
426 * map__rip_2objdump - convert symbol start address to objdump address.
428 * @rip: symbol start address
430 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
431 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
432 * relative to section start.
434 * Return: Address suitable for passing to "objdump --start-address="
436 u64 map__rip_2objdump(struct map *map, u64 rip)
438 struct kmap *kmap = __map__kmap(map);
441 * vmlinux does not have program headers for PTI entry trampolines and
442 * kcore may not either. However the trampoline object code is on the
443 * main kernel map, so just use that instead.
445 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
446 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
452 if (!map->dso->adjust_symbols)
456 return rip - map->pgoff;
459 * kernel modules also have DSO_TYPE_USER in dso->kernel,
460 * but all kernel modules are ET_REL, so won't get here.
462 if (map->dso->kernel == DSO_TYPE_USER)
463 return rip + map->dso->text_offset;
465 return map->unmap_ip(map, rip) - map->reloc;
469 * map__objdump_2mem - convert objdump address to a memory address.
471 * @ip: objdump address
473 * Closely related to map__rip_2objdump(), this function takes an address from
474 * objdump and converts it to a memory address. Note this assumes that @map
475 * contains the address. To be sure the result is valid, check it forwards
476 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
478 * Return: Memory address.
480 u64 map__objdump_2mem(struct map *map, u64 ip)
482 if (!map->dso->adjust_symbols)
483 return map->unmap_ip(map, ip);
486 return map->unmap_ip(map, ip + map->pgoff);
489 * kernel modules also have DSO_TYPE_USER in dso->kernel,
490 * but all kernel modules are ET_REL, so won't get here.
492 if (map->dso->kernel == DSO_TYPE_USER)
493 return map->unmap_ip(map, ip - map->dso->text_offset);
495 return ip + map->reloc;
498 static void maps__init(struct maps *maps)
500 maps->entries = RB_ROOT;
501 init_rwsem(&maps->lock);
504 void map_groups__init(struct map_groups *mg, struct machine *machine)
506 maps__init(&mg->maps);
507 mg->machine = machine;
508 refcount_set(&mg->refcnt, 1);
511 static void __maps__purge(struct maps *maps)
513 struct rb_root *root = &maps->entries;
514 struct rb_node *next = rb_first(root);
517 struct map *pos = rb_entry(next, struct map, rb_node);
519 next = rb_next(&pos->rb_node);
520 rb_erase_init(&pos->rb_node, root);
525 static void maps__exit(struct maps *maps)
527 down_write(&maps->lock);
529 up_write(&maps->lock);
532 void map_groups__exit(struct map_groups *mg)
534 maps__exit(&mg->maps);
537 bool map_groups__empty(struct map_groups *mg)
539 return !maps__first(&mg->maps);
542 struct map_groups *map_groups__new(struct machine *machine)
544 struct map_groups *mg = malloc(sizeof(*mg));
547 map_groups__init(mg, machine);
552 void map_groups__delete(struct map_groups *mg)
554 map_groups__exit(mg);
558 void map_groups__put(struct map_groups *mg)
560 if (mg && refcount_dec_and_test(&mg->refcnt))
561 map_groups__delete(mg);
564 struct symbol *map_groups__find_symbol(struct map_groups *mg,
565 u64 addr, struct map **mapp)
567 struct map *map = map_groups__find(mg, addr);
569 /* Ensure map is loaded before using map->map_ip */
570 if (map != NULL && map__load(map) >= 0) {
573 return map__find_symbol(map, map->map_ip(map, addr));
579 static bool map__contains_symbol(struct map *map, struct symbol *sym)
581 u64 ip = map->unmap_ip(map, sym->start);
583 return ip >= map->start && ip < map->end;
586 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
592 down_read(&maps->lock);
594 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
595 struct map *pos = rb_entry(nd, struct map, rb_node);
597 sym = map__find_symbol_by_name(pos, name);
601 if (!map__contains_symbol(pos, sym)) {
612 up_read(&maps->lock);
616 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
620 return maps__find_symbol_by_name(&mg->maps, name, mapp);
623 int map_groups__find_ams(struct addr_map_symbol *ams)
625 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
626 if (ams->map->groups == NULL)
628 ams->map = map_groups__find(ams->map->groups, ams->addr);
629 if (ams->map == NULL)
633 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
634 ams->sym = map__find_symbol(ams->map, ams->al_addr);
636 return ams->sym ? 0 : -1;
639 static size_t maps__fprintf(struct maps *maps, FILE *fp)
644 down_read(&maps->lock);
646 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
647 struct map *pos = rb_entry(nd, struct map, rb_node);
648 printed += fprintf(fp, "Map:");
649 printed += map__fprintf(pos, fp);
651 printed += dso__fprintf(pos->dso, fp);
652 printed += fprintf(fp, "--\n");
656 up_read(&maps->lock);
661 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
663 return maps__fprintf(&mg->maps, fp);
666 static void __map_groups__insert(struct map_groups *mg, struct map *map)
668 __maps__insert(&mg->maps, map);
672 static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
674 struct rb_root *root;
675 struct rb_node *next, *first;
678 down_write(&maps->lock);
680 root = &maps->entries;
683 * Find first map where end > map->start.
684 * Same as find_vma() in kernel.
686 next = root->rb_node;
689 struct map *pos = rb_entry(next, struct map, rb_node);
691 if (pos->end > map->start) {
693 if (pos->start <= map->start)
695 next = next->rb_left;
697 next = next->rb_right;
702 struct map *pos = rb_entry(next, struct map, rb_node);
703 next = rb_next(&pos->rb_node);
706 * Stop if current map starts after map->end.
707 * Maps are ordered by start: next will not overlap for sure.
709 if (pos->start >= map->end)
715 pr_warning("overlapping maps in %s "
716 "(disable tui for more info)\n",
719 fputs("overlapping maps:\n", fp);
720 map__fprintf(map, fp);
721 map__fprintf(pos, fp);
725 rb_erase_init(&pos->rb_node, root);
727 * Now check if we need to create new maps for areas not
728 * overlapped by the new map:
730 if (map->start > pos->start) {
731 struct map *before = map__clone(pos);
733 if (before == NULL) {
738 before->end = map->start;
739 __map_groups__insert(pos->groups, before);
740 if (verbose >= 2 && !use_browser)
741 map__fprintf(before, fp);
745 if (map->end < pos->end) {
746 struct map *after = map__clone(pos);
753 after->start = map->end;
754 after->pgoff += map->end - pos->start;
755 assert(pos->map_ip(pos, map->end) == after->map_ip(after, map->end));
756 __map_groups__insert(pos->groups, after);
757 if (verbose >= 2 && !use_browser)
758 map__fprintf(after, fp);
770 up_write(&maps->lock);
774 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
777 return maps__fixup_overlappings(&mg->maps, map, fp);
781 * XXX This should not really _copy_ te maps, but refcount them.
783 int map_groups__clone(struct thread *thread, struct map_groups *parent)
785 struct map_groups *mg = thread->mg;
788 struct maps *maps = &parent->maps;
790 down_read(&maps->lock);
792 for (map = maps__first(maps); map; map = map__next(map)) {
793 struct map *new = map__clone(map);
797 err = unwind__prepare_access(thread, new, NULL);
801 map_groups__insert(mg, new);
807 up_read(&maps->lock);
811 static void __maps__insert(struct maps *maps, struct map *map)
813 struct rb_node **p = &maps->entries.rb_node;
814 struct rb_node *parent = NULL;
815 const u64 ip = map->start;
820 m = rb_entry(parent, struct map, rb_node);
827 rb_link_node(&map->rb_node, parent, p);
828 rb_insert_color(&map->rb_node, &maps->entries);
832 void maps__insert(struct maps *maps, struct map *map)
834 down_write(&maps->lock);
835 __maps__insert(maps, map);
836 up_write(&maps->lock);
839 static void __maps__remove(struct maps *maps, struct map *map)
841 rb_erase_init(&map->rb_node, &maps->entries);
845 void maps__remove(struct maps *maps, struct map *map)
847 down_write(&maps->lock);
848 __maps__remove(maps, map);
849 up_write(&maps->lock);
852 struct map *maps__find(struct maps *maps, u64 ip)
854 struct rb_node **p, *parent = NULL;
857 down_read(&maps->lock);
859 p = &maps->entries.rb_node;
862 m = rb_entry(parent, struct map, rb_node);
865 else if (ip >= m->end)
873 up_read(&maps->lock);
877 struct map *maps__first(struct maps *maps)
879 struct rb_node *first = rb_first(&maps->entries);
882 return rb_entry(first, struct map, rb_node);
886 struct map *map__next(struct map *map)
888 struct rb_node *next = rb_next(&map->rb_node);
891 return rb_entry(next, struct map, rb_node);
895 struct kmap *__map__kmap(struct map *map)
897 if (!map->dso || !map->dso->kernel)
899 return (struct kmap *)(map + 1);
902 struct kmap *map__kmap(struct map *map)
904 struct kmap *kmap = __map__kmap(map);
907 pr_err("Internal error: map__kmap with a non-kernel map\n");
911 struct map_groups *map__kmaps(struct map *map)
913 struct kmap *kmap = map__kmap(map);
915 if (!kmap || !kmap->kmaps) {
916 pr_err("Internal error: map__kmaps with a non-kernel map\n");