1 // SPDX-License-Identifier: GPL-2.0
10 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
18 #include <linux/string.h>
20 #include "namespaces.h"
23 static void __maps__insert(struct maps *maps, struct map *map);
25 static inline int is_anon_memory(const char *filename, u32 flags)
27 return flags & MAP_HUGETLB ||
28 !strcmp(filename, "//anon") ||
29 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
30 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
33 static inline int is_no_dso_memory(const char *filename)
35 return !strncmp(filename, "[stack", 6) ||
36 !strncmp(filename, "/SYSV",5) ||
37 !strcmp(filename, "[heap]");
40 static inline int is_android_lib(const char *filename)
42 return !strncmp(filename, "/data/app-lib", 13) ||
43 !strncmp(filename, "/system/lib", 11);
46 static inline bool replace_android_lib(const char *filename, char *newfilename)
50 size_t app_abi_length, new_length;
51 size_t lib_length = 0;
53 libname = strrchr(filename, '/');
55 lib_length = strlen(libname);
57 app_abi = getenv("APP_ABI");
61 app_abi_length = strlen(app_abi);
63 if (!strncmp(filename, "/data/app-lib", 13)) {
69 new_length = 7 + app_abi_length + lib_length;
71 apk_path = getenv("APK_PATH");
73 new_length += strlen(apk_path) + 1;
74 if (new_length > PATH_MAX)
76 snprintf(newfilename, new_length,
77 "%s/libs/%s/%s", apk_path, app_abi, libname);
79 if (new_length > PATH_MAX)
81 snprintf(newfilename, new_length,
82 "libs/%s/%s", app_abi, libname);
87 if (!strncmp(filename, "/system/lib/", 11)) {
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, 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 int map__overlap(struct map *l, struct map *r)
386 if (l->start > r->start) {
392 if (l->end > r->start)
398 size_t map__fprintf(struct map *map, FILE *fp)
400 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
401 map->start, map->end, map->pgoff, map->dso->name);
404 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
406 const char *dsoname = "[unknown]";
408 if (map && map->dso) {
409 if (symbol_conf.show_kernel_path && map->dso->long_name)
410 dsoname = map->dso->long_name;
412 dsoname = map->dso->name;
415 return fprintf(fp, "%s", dsoname);
418 char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
421 return SRCLINE_UNKNOWN;
422 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
425 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
430 if (map && map->dso) {
431 char *srcline = map__srcline(map, addr, NULL);
432 if (srcline != SRCLINE_UNKNOWN)
433 ret = fprintf(fp, "%s%s", prefix, srcline);
434 free_srcline(srcline);
440 * map__rip_2objdump - convert symbol start address to objdump address.
442 * @rip: symbol start address
444 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
445 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
446 * relative to section start.
448 * Return: Address suitable for passing to "objdump --start-address="
450 u64 map__rip_2objdump(struct map *map, u64 rip)
452 struct kmap *kmap = __map__kmap(map);
455 * vmlinux does not have program headers for PTI entry trampolines and
456 * kcore may not either. However the trampoline object code is on the
457 * main kernel map, so just use that instead.
459 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
460 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
466 if (!map->dso->adjust_symbols)
470 return rip - map->pgoff;
473 * kernel modules also have DSO_TYPE_USER in dso->kernel,
474 * but all kernel modules are ET_REL, so won't get here.
476 if (map->dso->kernel == DSO_TYPE_USER)
477 return rip + map->dso->text_offset;
479 return map->unmap_ip(map, rip) - map->reloc;
483 * map__objdump_2mem - convert objdump address to a memory address.
485 * @ip: objdump address
487 * Closely related to map__rip_2objdump(), this function takes an address from
488 * objdump and converts it to a memory address. Note this assumes that @map
489 * contains the address. To be sure the result is valid, check it forwards
490 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
492 * Return: Memory address.
494 u64 map__objdump_2mem(struct map *map, u64 ip)
496 if (!map->dso->adjust_symbols)
497 return map->unmap_ip(map, ip);
500 return map->unmap_ip(map, ip + map->pgoff);
503 * kernel modules also have DSO_TYPE_USER in dso->kernel,
504 * but all kernel modules are ET_REL, so won't get here.
506 if (map->dso->kernel == DSO_TYPE_USER)
507 return map->unmap_ip(map, ip - map->dso->text_offset);
509 return ip + map->reloc;
512 static void maps__init(struct maps *maps)
514 maps->entries = RB_ROOT;
515 init_rwsem(&maps->lock);
518 void map_groups__init(struct map_groups *mg, struct machine *machine)
520 maps__init(&mg->maps);
521 mg->machine = machine;
522 refcount_set(&mg->refcnt, 1);
525 static void __maps__purge(struct maps *maps)
527 struct rb_root *root = &maps->entries;
528 struct rb_node *next = rb_first(root);
531 struct map *pos = rb_entry(next, struct map, rb_node);
533 next = rb_next(&pos->rb_node);
534 rb_erase_init(&pos->rb_node, root);
539 static void maps__exit(struct maps *maps)
541 down_write(&maps->lock);
543 up_write(&maps->lock);
546 void map_groups__exit(struct map_groups *mg)
548 maps__exit(&mg->maps);
551 bool map_groups__empty(struct map_groups *mg)
553 return !maps__first(&mg->maps);
556 struct map_groups *map_groups__new(struct machine *machine)
558 struct map_groups *mg = malloc(sizeof(*mg));
561 map_groups__init(mg, machine);
566 void map_groups__delete(struct map_groups *mg)
568 map_groups__exit(mg);
572 void map_groups__put(struct map_groups *mg)
574 if (mg && refcount_dec_and_test(&mg->refcnt))
575 map_groups__delete(mg);
578 struct symbol *map_groups__find_symbol(struct map_groups *mg,
579 u64 addr, struct map **mapp)
581 struct map *map = map_groups__find(mg, addr);
583 /* Ensure map is loaded before using map->map_ip */
584 if (map != NULL && map__load(map) >= 0) {
587 return map__find_symbol(map, map->map_ip(map, addr));
593 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
599 down_read(&maps->lock);
601 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
602 struct map *pos = rb_entry(nd, struct map, rb_node);
604 sym = map__find_symbol_by_name(pos, name);
615 up_read(&maps->lock);
619 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
623 return maps__find_symbol_by_name(&mg->maps, name, mapp);
626 int map_groups__find_ams(struct addr_map_symbol *ams)
628 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
629 if (ams->map->groups == NULL)
631 ams->map = map_groups__find(ams->map->groups, ams->addr);
632 if (ams->map == NULL)
636 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
637 ams->sym = map__find_symbol(ams->map, ams->al_addr);
639 return ams->sym ? 0 : -1;
642 static size_t maps__fprintf(struct maps *maps, FILE *fp)
647 down_read(&maps->lock);
649 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
650 struct map *pos = rb_entry(nd, struct map, rb_node);
651 printed += fprintf(fp, "Map:");
652 printed += map__fprintf(pos, fp);
654 printed += dso__fprintf(pos->dso, fp);
655 printed += fprintf(fp, "--\n");
659 up_read(&maps->lock);
664 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
666 return maps__fprintf(&mg->maps, fp);
669 static void __map_groups__insert(struct map_groups *mg, struct map *map)
671 __maps__insert(&mg->maps, map);
675 static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
677 struct rb_root *root;
678 struct rb_node *next;
681 down_write(&maps->lock);
683 root = &maps->entries;
684 next = rb_first(root);
687 struct map *pos = rb_entry(next, struct map, rb_node);
688 next = rb_next(&pos->rb_node);
690 if (!map__overlap(pos, map))
696 pr_warning("overlapping maps in %s "
697 "(disable tui for more info)\n",
700 fputs("overlapping maps:\n", fp);
701 map__fprintf(map, fp);
702 map__fprintf(pos, fp);
706 rb_erase_init(&pos->rb_node, root);
708 * Now check if we need to create new maps for areas not
709 * overlapped by the new map:
711 if (map->start > pos->start) {
712 struct map *before = map__clone(pos);
714 if (before == NULL) {
719 before->end = map->start;
720 __map_groups__insert(pos->groups, before);
721 if (verbose >= 2 && !use_browser)
722 map__fprintf(before, fp);
726 if (map->end < pos->end) {
727 struct map *after = map__clone(pos);
734 after->start = map->end;
735 __map_groups__insert(pos->groups, after);
736 if (verbose >= 2 && !use_browser)
737 map__fprintf(after, fp);
749 up_write(&maps->lock);
753 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
756 return maps__fixup_overlappings(&mg->maps, map, fp);
760 * XXX This should not really _copy_ te maps, but refcount them.
762 int map_groups__clone(struct thread *thread, struct map_groups *parent)
764 struct map_groups *mg = thread->mg;
767 struct maps *maps = &parent->maps;
769 down_read(&maps->lock);
771 for (map = maps__first(maps); map; map = map__next(map)) {
772 struct map *new = map__clone(map);
776 err = unwind__prepare_access(thread, new, NULL);
780 map_groups__insert(mg, new);
786 up_read(&maps->lock);
790 static void __maps__insert(struct maps *maps, struct map *map)
792 struct rb_node **p = &maps->entries.rb_node;
793 struct rb_node *parent = NULL;
794 const u64 ip = map->start;
799 m = rb_entry(parent, struct map, rb_node);
806 rb_link_node(&map->rb_node, parent, p);
807 rb_insert_color(&map->rb_node, &maps->entries);
811 void maps__insert(struct maps *maps, struct map *map)
813 down_write(&maps->lock);
814 __maps__insert(maps, map);
815 up_write(&maps->lock);
818 static void __maps__remove(struct maps *maps, struct map *map)
820 rb_erase_init(&map->rb_node, &maps->entries);
824 void maps__remove(struct maps *maps, struct map *map)
826 down_write(&maps->lock);
827 __maps__remove(maps, map);
828 up_write(&maps->lock);
831 struct map *maps__find(struct maps *maps, u64 ip)
833 struct rb_node **p, *parent = NULL;
836 down_read(&maps->lock);
838 p = &maps->entries.rb_node;
841 m = rb_entry(parent, struct map, rb_node);
844 else if (ip >= m->end)
852 up_read(&maps->lock);
856 struct map *maps__first(struct maps *maps)
858 struct rb_node *first = rb_first(&maps->entries);
861 return rb_entry(first, struct map, rb_node);
865 struct map *map__next(struct map *map)
867 struct rb_node *next = rb_next(&map->rb_node);
870 return rb_entry(next, struct map, rb_node);
874 struct kmap *__map__kmap(struct map *map)
876 if (!map->dso || !map->dso->kernel)
878 return (struct kmap *)(map + 1);
881 struct kmap *map__kmap(struct map *map)
883 struct kmap *kmap = __map__kmap(map);
886 pr_err("Internal error: map__kmap with a non-kernel map\n");
890 struct map_groups *map__kmaps(struct map *map)
892 struct kmap *kmap = map__kmap(map);
894 if (!kmap || !kmap->kmaps) {
895 pr_err("Internal error: map__kmaps with a non-kernel map\n");