1 // SPDX-License-Identifier: GPL-2.0
11 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
14 #include "map_symbol.h"
20 #include <linux/string.h>
21 #include <linux/zalloc.h>
23 #include "namespaces.h"
28 static void __maps__insert(struct maps *maps, struct map *map);
30 static inline int is_android_lib(const char *filename)
32 return strstarts(filename, "/data/app-lib/") ||
33 strstarts(filename, "/system/lib/");
36 static inline bool replace_android_lib(const char *filename, char *newfilename)
40 size_t app_abi_length, new_length;
41 size_t lib_length = 0;
43 libname = strrchr(filename, '/');
45 lib_length = strlen(libname);
47 app_abi = getenv("APP_ABI");
51 app_abi_length = strlen(app_abi);
53 if (strstarts(filename, "/data/app-lib/")) {
59 new_length = 7 + app_abi_length + lib_length;
61 apk_path = getenv("APK_PATH");
63 new_length += strlen(apk_path) + 1;
64 if (new_length > PATH_MAX)
66 snprintf(newfilename, new_length,
67 "%s/libs/%s/%s", apk_path, app_abi, libname);
69 if (new_length > PATH_MAX)
71 snprintf(newfilename, new_length,
72 "libs/%s/%s", app_abi, libname);
77 if (strstarts(filename, "/system/lib/")) {
83 ndk = getenv("NDK_ROOT");
84 app = getenv("APP_PLATFORM");
89 ndk_length = strlen(ndk);
90 app_length = strlen(app);
92 if (!(ndk_length && app_length && app_abi_length))
95 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
96 !strncmp(app_abi, "mips", 4) ? "mips" :
97 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
102 new_length = 27 + ndk_length +
103 app_length + lib_length
106 if (new_length > PATH_MAX)
108 snprintf(newfilename, new_length,
109 "%s/platforms/%s/arch-%s/usr/lib/%s",
110 ndk, app, arch, libname);
117 void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
123 map->dso = dso__get(dso);
124 map->map_ip = map__map_ip;
125 map->unmap_ip = map__unmap_ip;
126 RB_CLEAR_NODE(&map->rb_node);
127 map->erange_warned = false;
128 refcount_set(&map->refcnt, 1);
131 struct map *map__new(struct machine *machine, u64 start, u64 len,
132 u64 pgoff, struct dso_id *id,
133 u32 prot, u32 flags, struct build_id *bid,
134 char *filename, struct thread *thread)
136 struct map *map = malloc(sizeof(*map));
137 struct nsinfo *nsi = NULL;
141 char newfilename[PATH_MAX];
143 int anon, no_dso, vdso, android;
145 android = is_android_lib(filename);
146 anon = is_anon_memory(filename) || flags & MAP_HUGETLB;
147 vdso = is_vdso_map(filename);
148 no_dso = is_no_dso_memory(filename);
151 nsi = nsinfo__get(thread->nsinfo);
153 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
154 snprintf(newfilename, sizeof(newfilename),
155 "/tmp/perf-%d.map", nsi->pid);
156 filename = newfilename;
160 if (replace_android_lib(filename, newfilename))
161 filename = newfilename;
165 /* The vdso maps are always on the host and not the
166 * container. Ensure that we don't use setns to look
169 nnsi = nsinfo__copy(nsi);
172 nnsi->need_setns = false;
176 dso = machine__findnew_vdso(machine, thread);
178 dso = machine__findnew_dso_id(machine, filename, id);
183 map__init(map, start, start + len, pgoff, dso);
185 if (anon || no_dso) {
186 map->map_ip = map->unmap_ip = identity__map_ip;
189 * Set memory without DSO as loaded. All map__find_*
190 * functions still return NULL, and we avoid the
191 * unnecessary map__load warning.
193 if (!(prot & PROT_EXEC))
194 dso__set_loaded(dso);
198 if (build_id__is_defined(bid))
199 dso__set_build_id(dso, bid);
211 * Constructor variant for modules (where we know from /proc/modules where
212 * they are loaded) and for vmlinux, where only after we load all the
213 * symbols we'll know where it starts and ends.
215 struct map *map__new2(u64 start, struct dso *dso)
217 struct map *map = calloc(1, (sizeof(*map) +
218 (dso->kernel ? sizeof(struct kmap) : 0)));
221 * ->end will be filled after we load all the symbols
223 map__init(map, start, 0, 0, dso);
229 bool __map__is_kernel(const struct map *map)
231 if (!map->dso->kernel)
233 return machine__kernel_map(map__kmaps((struct map *)map)->machine) == map;
236 bool __map__is_extra_kernel_map(const struct map *map)
238 struct kmap *kmap = __map__kmap((struct map *)map);
240 return kmap && kmap->name[0];
243 bool __map__is_bpf_prog(const struct map *map)
247 if (map->dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
251 * If PERF_RECORD_BPF_EVENT is not included, the dso will not have
252 * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can
253 * guess the type based on name.
255 name = map->dso->short_name;
256 return name && (strstr(name, "bpf_prog_") == name);
259 bool __map__is_bpf_image(const struct map *map)
263 if (map->dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE)
267 * If PERF_RECORD_KSYMBOL is not included, the dso will not have
268 * type of DSO_BINARY_TYPE__BPF_IMAGE. In such cases, we can
269 * guess the type based on name.
271 name = map->dso->short_name;
272 return name && is_bpf_image(name);
275 bool __map__is_ool(const struct map *map)
277 return map->dso && map->dso->binary_type == DSO_BINARY_TYPE__OOL;
280 bool map__has_symbols(const struct map *map)
282 return dso__has_symbols(map->dso);
285 static void map__exit(struct map *map)
287 BUG_ON(refcount_read(&map->refcnt) != 0);
291 void map__delete(struct map *map)
297 void map__put(struct map *map)
299 if (map && refcount_dec_and_test(&map->refcnt))
303 void map__fixup_start(struct map *map)
305 struct rb_root_cached *symbols = &map->dso->symbols;
306 struct rb_node *nd = rb_first_cached(symbols);
308 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
309 map->start = sym->start;
313 void map__fixup_end(struct map *map)
315 struct rb_root_cached *symbols = &map->dso->symbols;
316 struct rb_node *nd = rb_last(&symbols->rb_root);
318 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
323 #define DSO__DELETED "(deleted)"
325 int map__load(struct map *map)
327 const char *name = map->dso->long_name;
330 if (dso__loaded(map->dso))
333 nr = dso__load(map->dso, map);
335 if (map->dso->has_build_id) {
336 char sbuild_id[SBUILD_ID_SIZE];
338 build_id__sprintf(&map->dso->bid, sbuild_id);
339 pr_debug("%s with build id %s not found", name, sbuild_id);
341 pr_debug("Failed to open %s", name);
343 pr_debug(", continuing without symbols\n");
345 } else if (nr == 0) {
346 #ifdef HAVE_LIBELF_SUPPORT
347 const size_t len = strlen(name);
348 const size_t real_len = len - sizeof(DSO__DELETED);
350 if (len > sizeof(DSO__DELETED) &&
351 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
352 pr_debug("%.*s was updated (is prelink enabled?). "
353 "Restart the long running apps that use it!\n",
354 (int)real_len, name);
356 pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
365 struct symbol *map__find_symbol(struct map *map, u64 addr)
367 if (map__load(map) < 0)
370 return dso__find_symbol(map->dso, addr);
373 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
375 if (map__load(map) < 0)
378 if (!dso__sorted_by_name(map->dso))
379 dso__sort_by_name(map->dso);
381 return dso__find_symbol_by_name(map->dso, name);
384 struct map *map__clone(struct map *from)
386 size_t size = sizeof(struct map);
389 if (from->dso && from->dso->kernel)
390 size += sizeof(struct kmap);
392 map = memdup(from, size);
394 refcount_set(&map->refcnt, 1);
395 RB_CLEAR_NODE(&map->rb_node);
402 size_t map__fprintf(struct map *map, FILE *fp)
404 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
405 map->start, map->end, map->pgoff, map->dso->name);
408 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
410 char buf[symbol_conf.pad_output_len_dso + 1];
411 const char *dsoname = "[unknown]";
413 if (map && map->dso) {
414 if (symbol_conf.show_kernel_path && map->dso->long_name)
415 dsoname = map->dso->long_name;
417 dsoname = map->dso->name;
420 if (symbol_conf.pad_output_len_dso) {
421 scnprintf_pad(buf, symbol_conf.pad_output_len_dso, "%s", dsoname);
425 return fprintf(fp, "%s", dsoname);
428 char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
431 return SRCLINE_UNKNOWN;
432 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
435 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
440 if (map && map->dso) {
441 char *srcline = map__srcline(map, addr, NULL);
442 if (strncmp(srcline, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)
443 ret = fprintf(fp, "%s%s", prefix, srcline);
444 free_srcline(srcline);
449 void srccode_state_free(struct srccode_state *state)
451 zfree(&state->srcfile);
456 * map__rip_2objdump - convert symbol start address to objdump address.
458 * @rip: symbol start address
460 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
461 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
462 * relative to section start.
464 * Return: Address suitable for passing to "objdump --start-address="
466 u64 map__rip_2objdump(struct map *map, u64 rip)
468 struct kmap *kmap = __map__kmap(map);
471 * vmlinux does not have program headers for PTI entry trampolines and
472 * kcore may not either. However the trampoline object code is on the
473 * main kernel map, so just use that instead.
475 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
476 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
482 if (!map->dso->adjust_symbols)
486 return rip - 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_SPACE__USER)
493 return rip + map->dso->text_offset;
495 return map->unmap_ip(map, rip) - map->reloc;
499 * map__objdump_2mem - convert objdump address to a memory address.
501 * @ip: objdump address
503 * Closely related to map__rip_2objdump(), this function takes an address from
504 * objdump and converts it to a memory address. Note this assumes that @map
505 * contains the address. To be sure the result is valid, check it forwards
506 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
508 * Return: Memory address.
510 u64 map__objdump_2mem(struct map *map, u64 ip)
512 if (!map->dso->adjust_symbols)
513 return map->unmap_ip(map, ip);
516 return map->unmap_ip(map, ip + map->pgoff);
519 * kernel modules also have DSO_TYPE_USER in dso->kernel,
520 * but all kernel modules are ET_REL, so won't get here.
522 if (map->dso->kernel == DSO_SPACE__USER)
523 return map->unmap_ip(map, ip - map->dso->text_offset);
525 return ip + map->reloc;
528 void maps__init(struct maps *maps, struct machine *machine)
530 maps->entries = RB_ROOT;
531 init_rwsem(&maps->lock);
532 maps->machine = machine;
533 maps->last_search_by_name = NULL;
535 maps->maps_by_name = NULL;
536 refcount_set(&maps->refcnt, 1);
539 static void __maps__free_maps_by_name(struct maps *maps)
542 * Free everything to try to do it from the rbtree in the next search
544 zfree(&maps->maps_by_name);
545 maps->nr_maps_allocated = 0;
548 void maps__insert(struct maps *maps, struct map *map)
550 down_write(&maps->lock);
551 __maps__insert(maps, map);
554 if (map->dso && map->dso->kernel) {
555 struct kmap *kmap = map__kmap(map);
560 pr_err("Internal error: kernel dso with non kernel map\n");
565 * If we already performed some search by name, then we need to add the just
566 * inserted map and resort.
568 if (maps->maps_by_name) {
569 if (maps->nr_maps > maps->nr_maps_allocated) {
570 int nr_allocate = maps->nr_maps * 2;
571 struct map **maps_by_name = realloc(maps->maps_by_name, nr_allocate * sizeof(map));
573 if (maps_by_name == NULL) {
574 __maps__free_maps_by_name(maps);
575 up_write(&maps->lock);
579 maps->maps_by_name = maps_by_name;
580 maps->nr_maps_allocated = nr_allocate;
582 maps->maps_by_name[maps->nr_maps - 1] = map;
583 __maps__sort_by_name(maps);
585 up_write(&maps->lock);
588 static void __maps__remove(struct maps *maps, struct map *map)
590 rb_erase_init(&map->rb_node, &maps->entries);
594 void maps__remove(struct maps *maps, struct map *map)
596 down_write(&maps->lock);
597 if (maps->last_search_by_name == map)
598 maps->last_search_by_name = NULL;
600 __maps__remove(maps, map);
602 if (maps->maps_by_name)
603 __maps__free_maps_by_name(maps);
604 up_write(&maps->lock);
607 static void __maps__purge(struct maps *maps)
609 struct map *pos, *next;
611 maps__for_each_entry_safe(maps, pos, next) {
612 rb_erase_init(&pos->rb_node, &maps->entries);
617 void maps__exit(struct maps *maps)
619 down_write(&maps->lock);
621 up_write(&maps->lock);
624 bool maps__empty(struct maps *maps)
626 return !maps__first(maps);
629 struct maps *maps__new(struct machine *machine)
631 struct maps *maps = zalloc(sizeof(*maps));
634 maps__init(maps, machine);
639 void maps__delete(struct maps *maps)
642 unwind__finish_access(maps);
646 void maps__put(struct maps *maps)
648 if (maps && refcount_dec_and_test(&maps->refcnt))
652 struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)
654 struct map *map = maps__find(maps, addr);
656 /* Ensure map is loaded before using map->map_ip */
657 if (map != NULL && map__load(map) >= 0) {
660 return map__find_symbol(map, map->map_ip(map, addr));
666 static bool map__contains_symbol(struct map *map, struct symbol *sym)
668 u64 ip = map->unmap_ip(map, sym->start);
670 return ip >= map->start && ip < map->end;
673 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp)
678 down_read(&maps->lock);
680 maps__for_each_entry(maps, pos) {
681 sym = map__find_symbol_by_name(pos, name);
685 if (!map__contains_symbol(pos, sym)) {
696 up_read(&maps->lock);
700 int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams)
702 if (ams->addr < ams->ms.map->start || ams->addr >= ams->ms.map->end) {
705 ams->ms.map = maps__find(maps, ams->addr);
706 if (ams->ms.map == NULL)
710 ams->al_addr = ams->ms.map->map_ip(ams->ms.map, ams->addr);
711 ams->ms.sym = map__find_symbol(ams->ms.map, ams->al_addr);
713 return ams->ms.sym ? 0 : -1;
716 size_t maps__fprintf(struct maps *maps, FILE *fp)
721 down_read(&maps->lock);
723 maps__for_each_entry(maps, pos) {
724 printed += fprintf(fp, "Map:");
725 printed += map__fprintf(pos, fp);
727 printed += dso__fprintf(pos->dso, fp);
728 printed += fprintf(fp, "--\n");
732 up_read(&maps->lock);
737 int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
739 struct rb_root *root;
740 struct rb_node *next, *first;
743 down_write(&maps->lock);
745 root = &maps->entries;
748 * Find first map where end > map->start.
749 * Same as find_vma() in kernel.
751 next = root->rb_node;
754 struct map *pos = rb_entry(next, struct map, rb_node);
756 if (pos->end > map->start) {
758 if (pos->start <= map->start)
760 next = next->rb_left;
762 next = next->rb_right;
767 struct map *pos = rb_entry(next, struct map, rb_node);
768 next = rb_next(&pos->rb_node);
771 * Stop if current map starts after map->end.
772 * Maps are ordered by start: next will not overlap for sure.
774 if (pos->start >= map->end)
780 pr_debug("overlapping maps in %s (disable tui for more info)\n",
783 fputs("overlapping maps:\n", fp);
784 map__fprintf(map, fp);
785 map__fprintf(pos, fp);
789 rb_erase_init(&pos->rb_node, root);
791 * Now check if we need to create new maps for areas not
792 * overlapped by the new map:
794 if (map->start > pos->start) {
795 struct map *before = map__clone(pos);
797 if (before == NULL) {
802 before->end = map->start;
803 __maps__insert(maps, before);
804 if (verbose >= 2 && !use_browser)
805 map__fprintf(before, fp);
809 if (map->end < pos->end) {
810 struct map *after = map__clone(pos);
817 after->start = map->end;
818 after->pgoff += map->end - pos->start;
819 assert(pos->map_ip(pos, map->end) == after->map_ip(after, map->end));
820 __maps__insert(maps, after);
821 if (verbose >= 2 && !use_browser)
822 map__fprintf(after, fp);
834 up_write(&maps->lock);
839 * XXX This should not really _copy_ te maps, but refcount them.
841 int maps__clone(struct thread *thread, struct maps *parent)
843 struct maps *maps = thread->maps;
847 down_read(&parent->lock);
849 maps__for_each_entry(parent, map) {
850 struct map *new = map__clone(map);
854 err = unwind__prepare_access(maps, new, NULL);
858 maps__insert(maps, new);
864 up_read(&parent->lock);
868 static void __maps__insert(struct maps *maps, struct map *map)
870 struct rb_node **p = &maps->entries.rb_node;
871 struct rb_node *parent = NULL;
872 const u64 ip = map->start;
877 m = rb_entry(parent, struct map, rb_node);
884 rb_link_node(&map->rb_node, parent, p);
885 rb_insert_color(&map->rb_node, &maps->entries);
889 struct map *maps__find(struct maps *maps, u64 ip)
894 down_read(&maps->lock);
896 p = maps->entries.rb_node;
898 m = rb_entry(p, struct map, rb_node);
901 else if (ip >= m->end)
909 up_read(&maps->lock);
913 struct map *maps__first(struct maps *maps)
915 struct rb_node *first = rb_first(&maps->entries);
918 return rb_entry(first, struct map, rb_node);
922 static struct map *__map__next(struct map *map)
924 struct rb_node *next = rb_next(&map->rb_node);
927 return rb_entry(next, struct map, rb_node);
931 struct map *map__next(struct map *map)
933 return map ? __map__next(map) : NULL;
936 struct kmap *__map__kmap(struct map *map)
938 if (!map->dso || !map->dso->kernel)
940 return (struct kmap *)(map + 1);
943 struct kmap *map__kmap(struct map *map)
945 struct kmap *kmap = __map__kmap(map);
948 pr_err("Internal error: map__kmap with a non-kernel map\n");
952 struct maps *map__kmaps(struct map *map)
954 struct kmap *kmap = map__kmap(map);
956 if (!kmap || !kmap->kmaps) {
957 pr_err("Internal error: map__kmaps with a non-kernel map\n");