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_anon_memory(const char *filename, u32 flags)
32 return flags & MAP_HUGETLB ||
33 !strcmp(filename, "//anon") ||
34 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
35 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
38 static inline int is_no_dso_memory(const char *filename)
40 return !strncmp(filename, "[stack", 6) ||
41 !strncmp(filename, "/SYSV",5) ||
42 !strcmp(filename, "[heap]");
45 static inline int is_android_lib(const char *filename)
47 return !strncmp(filename, "/data/app-lib", 13) ||
48 !strncmp(filename, "/system/lib", 11);
51 static inline bool replace_android_lib(const char *filename, char *newfilename)
55 size_t app_abi_length, new_length;
56 size_t lib_length = 0;
58 libname = strrchr(filename, '/');
60 lib_length = strlen(libname);
62 app_abi = getenv("APP_ABI");
66 app_abi_length = strlen(app_abi);
68 if (!strncmp(filename, "/data/app-lib", 13)) {
74 new_length = 7 + app_abi_length + lib_length;
76 apk_path = getenv("APK_PATH");
78 new_length += strlen(apk_path) + 1;
79 if (new_length > PATH_MAX)
81 snprintf(newfilename, new_length,
82 "%s/libs/%s/%s", apk_path, app_abi, libname);
84 if (new_length > PATH_MAX)
86 snprintf(newfilename, new_length,
87 "libs/%s/%s", app_abi, libname);
92 if (!strncmp(filename, "/system/lib/", 11)) {
98 ndk = getenv("NDK_ROOT");
99 app = getenv("APP_PLATFORM");
104 ndk_length = strlen(ndk);
105 app_length = strlen(app);
107 if (!(ndk_length && app_length && app_abi_length))
110 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
111 !strncmp(app_abi, "mips", 4) ? "mips" :
112 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
117 new_length = 27 + ndk_length +
118 app_length + lib_length
121 if (new_length > PATH_MAX)
123 snprintf(newfilename, new_length,
124 "%s/platforms/%s/arch-%s/usr/lib/%s",
125 ndk, app, arch, libname);
132 void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
138 map->dso = dso__get(dso);
139 map->map_ip = map__map_ip;
140 map->unmap_ip = map__unmap_ip;
141 RB_CLEAR_NODE(&map->rb_node);
142 map->erange_warned = false;
143 refcount_set(&map->refcnt, 1);
146 struct map *map__new(struct machine *machine, u64 start, u64 len,
147 u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
148 u64 ino_gen, u32 prot, u32 flags, char *filename,
149 struct thread *thread)
151 struct map *map = malloc(sizeof(*map));
152 struct nsinfo *nsi = NULL;
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);
165 map->dso_id.maj = d_maj;
166 map->dso_id.min = d_min;
167 map->dso_id.ino = ino;
168 map->dso_id.ino_generation = ino_gen;
171 nsi = nsinfo__get(thread->nsinfo);
173 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
174 snprintf(newfilename, sizeof(newfilename),
175 "/tmp/perf-%d.map", nsi->pid);
176 filename = newfilename;
180 if (replace_android_lib(filename, newfilename))
181 filename = newfilename;
185 /* The vdso maps are always on the host and not the
186 * container. Ensure that we don't use setns to look
189 nnsi = nsinfo__copy(nsi);
192 nnsi->need_setns = false;
196 dso = machine__findnew_vdso(machine, thread);
198 dso = machine__findnew_dso(machine, filename);
203 map__init(map, start, start + len, pgoff, dso);
205 if (anon || no_dso) {
206 map->map_ip = map->unmap_ip = identity__map_ip;
209 * Set memory without DSO as loaded. All map__find_*
210 * functions still return NULL, and we avoid the
211 * unnecessary map__load warning.
213 if (!(prot & PROT_EXEC))
214 dso__set_loaded(dso);
227 * Constructor variant for modules (where we know from /proc/modules where
228 * they are loaded) and for vmlinux, where only after we load all the
229 * symbols we'll know where it starts and ends.
231 struct map *map__new2(u64 start, struct dso *dso)
233 struct map *map = calloc(1, (sizeof(*map) +
234 (dso->kernel ? sizeof(struct kmap) : 0)));
237 * ->end will be filled after we load all the symbols
239 map__init(map, start, 0, 0, dso);
245 bool __map__is_kernel(const struct map *map)
247 if (!map->dso->kernel)
249 return machine__kernel_map(map__kmaps((struct map *)map)->machine) == map;
252 bool __map__is_extra_kernel_map(const struct map *map)
254 struct kmap *kmap = __map__kmap((struct map *)map);
256 return kmap && kmap->name[0];
259 bool __map__is_bpf_prog(const struct map *map)
263 if (map->dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
267 * If PERF_RECORD_BPF_EVENT is not included, the dso will not have
268 * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can
269 * guess the type based on name.
271 name = map->dso->short_name;
272 return name && (strstr(name, "bpf_prog_") == name);
275 bool map__has_symbols(const struct map *map)
277 return dso__has_symbols(map->dso);
280 static void map__exit(struct map *map)
282 BUG_ON(refcount_read(&map->refcnt) != 0);
286 void map__delete(struct map *map)
292 void map__put(struct map *map)
294 if (map && refcount_dec_and_test(&map->refcnt))
298 void map__fixup_start(struct map *map)
300 struct rb_root_cached *symbols = &map->dso->symbols;
301 struct rb_node *nd = rb_first_cached(symbols);
303 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
304 map->start = sym->start;
308 void map__fixup_end(struct map *map)
310 struct rb_root_cached *symbols = &map->dso->symbols;
311 struct rb_node *nd = rb_last(&symbols->rb_root);
313 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
318 #define DSO__DELETED "(deleted)"
320 int map__load(struct map *map)
322 const char *name = map->dso->long_name;
325 if (dso__loaded(map->dso))
328 nr = dso__load(map->dso, map);
330 if (map->dso->has_build_id) {
331 char sbuild_id[SBUILD_ID_SIZE];
333 build_id__sprintf(map->dso->build_id,
334 sizeof(map->dso->build_id),
336 pr_debug("%s with build id %s not found", name, sbuild_id);
338 pr_debug("Failed to open %s", name);
340 pr_debug(", continuing without symbols\n");
342 } else if (nr == 0) {
343 #ifdef HAVE_LIBELF_SUPPORT
344 const size_t len = strlen(name);
345 const size_t real_len = len - sizeof(DSO__DELETED);
347 if (len > sizeof(DSO__DELETED) &&
348 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
349 pr_debug("%.*s was updated (is prelink enabled?). "
350 "Restart the long running apps that use it!\n",
351 (int)real_len, name);
353 pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
362 struct symbol *map__find_symbol(struct map *map, u64 addr)
364 if (map__load(map) < 0)
367 return dso__find_symbol(map->dso, addr);
370 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
372 if (map__load(map) < 0)
375 if (!dso__sorted_by_name(map->dso))
376 dso__sort_by_name(map->dso);
378 return dso__find_symbol_by_name(map->dso, name);
381 struct map *map__clone(struct map *from)
383 struct map *map = memdup(from, sizeof(*map));
386 refcount_set(&map->refcnt, 1);
387 RB_CLEAR_NODE(&map->rb_node);
394 size_t map__fprintf(struct map *map, FILE *fp)
396 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
397 map->start, map->end, map->pgoff, map->dso->name);
400 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
402 char buf[symbol_conf.pad_output_len_dso + 1];
403 const char *dsoname = "[unknown]";
405 if (map && map->dso) {
406 if (symbol_conf.show_kernel_path && map->dso->long_name)
407 dsoname = map->dso->long_name;
409 dsoname = map->dso->name;
412 if (symbol_conf.pad_output_len_dso) {
413 scnprintf_pad(buf, symbol_conf.pad_output_len_dso, "%s", dsoname);
417 return fprintf(fp, "%s", dsoname);
420 char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
423 return SRCLINE_UNKNOWN;
424 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
427 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
432 if (map && map->dso) {
433 char *srcline = map__srcline(map, addr, NULL);
434 if (srcline != SRCLINE_UNKNOWN)
435 ret = fprintf(fp, "%s%s", prefix, srcline);
436 free_srcline(srcline);
441 int map__fprintf_srccode(struct map *map, u64 addr,
443 struct srccode_state *state)
451 if (!map || !map->dso)
453 srcfile = get_srcline_split(map->dso,
454 map__rip_2objdump(map, addr),
459 /* Avoid redundant printing */
462 !strcmp(state->srcfile, srcfile) &&
463 state->line == line) {
468 srccode = find_sourceline(srcfile, line, &len);
472 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
475 state->srcfile = srcfile;
486 void srccode_state_free(struct srccode_state *state)
488 zfree(&state->srcfile);
493 * map__rip_2objdump - convert symbol start address to objdump address.
495 * @rip: symbol start address
497 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
498 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
499 * relative to section start.
501 * Return: Address suitable for passing to "objdump --start-address="
503 u64 map__rip_2objdump(struct map *map, u64 rip)
505 struct kmap *kmap = __map__kmap(map);
508 * vmlinux does not have program headers for PTI entry trampolines and
509 * kcore may not either. However the trampoline object code is on the
510 * main kernel map, so just use that instead.
512 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
513 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
519 if (!map->dso->adjust_symbols)
523 return rip - map->pgoff;
526 * kernel modules also have DSO_TYPE_USER in dso->kernel,
527 * but all kernel modules are ET_REL, so won't get here.
529 if (map->dso->kernel == DSO_TYPE_USER)
530 return rip + map->dso->text_offset;
532 return map->unmap_ip(map, rip) - map->reloc;
536 * map__objdump_2mem - convert objdump address to a memory address.
538 * @ip: objdump address
540 * Closely related to map__rip_2objdump(), this function takes an address from
541 * objdump and converts it to a memory address. Note this assumes that @map
542 * contains the address. To be sure the result is valid, check it forwards
543 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
545 * Return: Memory address.
547 u64 map__objdump_2mem(struct map *map, u64 ip)
549 if (!map->dso->adjust_symbols)
550 return map->unmap_ip(map, ip);
553 return map->unmap_ip(map, ip + map->pgoff);
556 * kernel modules also have DSO_TYPE_USER in dso->kernel,
557 * but all kernel modules are ET_REL, so won't get here.
559 if (map->dso->kernel == DSO_TYPE_USER)
560 return map->unmap_ip(map, ip - map->dso->text_offset);
562 return ip + map->reloc;
565 static void maps__init(struct maps *maps)
567 maps->entries = RB_ROOT;
568 init_rwsem(&maps->lock);
571 void map_groups__init(struct map_groups *mg, struct machine *machine)
573 maps__init(&mg->maps);
574 mg->machine = machine;
575 mg->last_search_by_name = NULL;
577 mg->maps_by_name = NULL;
578 refcount_set(&mg->refcnt, 1);
581 static void __map_groups__free_maps_by_name(struct map_groups *mg)
584 * Free everything to try to do it from the rbtree in the next search
586 zfree(&mg->maps_by_name);
587 mg->nr_maps_allocated = 0;
590 void map_groups__insert(struct map_groups *mg, struct map *map)
592 struct maps *maps = &mg->maps;
594 down_write(&maps->lock);
595 __maps__insert(maps, map);
599 * If we already performed some search by name, then we need to add the just
600 * inserted map and resort.
602 if (mg->maps_by_name) {
603 if (mg->nr_maps > mg->nr_maps_allocated) {
604 int nr_allocate = mg->nr_maps * 2;
605 struct map **maps_by_name = realloc(mg->maps_by_name, nr_allocate * sizeof(map));
607 if (maps_by_name == NULL) {
608 __map_groups__free_maps_by_name(mg);
612 mg->maps_by_name = maps_by_name;
613 mg->nr_maps_allocated = nr_allocate;
615 mg->maps_by_name[mg->nr_maps - 1] = map;
616 __map_groups__sort_by_name(mg);
618 up_write(&maps->lock);
621 void map_groups__remove(struct map_groups *mg, struct map *map)
623 struct maps *maps = &mg->maps;
624 down_write(&maps->lock);
625 if (mg->last_search_by_name == map)
626 mg->last_search_by_name = NULL;
628 __maps__remove(maps, map);
630 if (mg->maps_by_name)
631 __map_groups__free_maps_by_name(mg);
632 up_write(&maps->lock);
635 static void __maps__purge(struct maps *maps)
637 struct map *pos, *next;
639 maps__for_each_entry_safe(maps, pos, next) {
640 rb_erase_init(&pos->rb_node, &maps->entries);
645 static void maps__exit(struct maps *maps)
647 down_write(&maps->lock);
649 up_write(&maps->lock);
652 void map_groups__exit(struct map_groups *mg)
654 maps__exit(&mg->maps);
657 bool map_groups__empty(struct map_groups *mg)
659 return !maps__first(&mg->maps);
662 struct map_groups *map_groups__new(struct machine *machine)
664 struct map_groups *mg = zalloc(sizeof(*mg));
667 map_groups__init(mg, machine);
672 void map_groups__delete(struct map_groups *mg)
674 map_groups__exit(mg);
675 unwind__finish_access(mg);
679 void map_groups__put(struct map_groups *mg)
681 if (mg && refcount_dec_and_test(&mg->refcnt))
682 map_groups__delete(mg);
685 struct symbol *map_groups__find_symbol(struct map_groups *mg,
686 u64 addr, struct map **mapp)
688 struct map *map = map_groups__find(mg, addr);
690 /* Ensure map is loaded before using map->map_ip */
691 if (map != NULL && map__load(map) >= 0) {
694 return map__find_symbol(map, map->map_ip(map, addr));
700 static bool map__contains_symbol(struct map *map, struct symbol *sym)
702 u64 ip = map->unmap_ip(map, sym->start);
704 return ip >= map->start && ip < map->end;
707 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
713 down_read(&maps->lock);
715 maps__for_each_entry(maps, pos) {
716 sym = map__find_symbol_by_name(pos, name);
720 if (!map__contains_symbol(pos, sym)) {
731 up_read(&maps->lock);
735 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
739 return maps__find_symbol_by_name(&mg->maps, name, mapp);
742 int map_groups__find_ams(struct map_groups *mg, struct addr_map_symbol *ams)
744 if (ams->addr < ams->ms.map->start || ams->addr >= ams->ms.map->end) {
747 ams->ms.map = map_groups__find(mg, ams->addr);
748 if (ams->ms.map == NULL)
752 ams->al_addr = ams->ms.map->map_ip(ams->ms.map, ams->addr);
753 ams->ms.sym = map__find_symbol(ams->ms.map, ams->al_addr);
755 return ams->ms.sym ? 0 : -1;
758 static size_t maps__fprintf(struct maps *maps, FILE *fp)
763 down_read(&maps->lock);
765 maps__for_each_entry(maps, pos) {
766 printed += fprintf(fp, "Map:");
767 printed += map__fprintf(pos, fp);
769 printed += dso__fprintf(pos->dso, fp);
770 printed += fprintf(fp, "--\n");
774 up_read(&maps->lock);
779 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
781 return maps__fprintf(&mg->maps, fp);
784 static void __map_groups__insert(struct map_groups *mg, struct map *map)
786 __maps__insert(&mg->maps, map);
789 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, FILE *fp)
791 struct maps *maps = &mg->maps;
792 struct rb_root *root;
793 struct rb_node *next, *first;
796 down_write(&maps->lock);
798 root = &maps->entries;
801 * Find first map where end > map->start.
802 * Same as find_vma() in kernel.
804 next = root->rb_node;
807 struct map *pos = rb_entry(next, struct map, rb_node);
809 if (pos->end > map->start) {
811 if (pos->start <= map->start)
813 next = next->rb_left;
815 next = next->rb_right;
820 struct map *pos = rb_entry(next, struct map, rb_node);
821 next = rb_next(&pos->rb_node);
824 * Stop if current map starts after map->end.
825 * Maps are ordered by start: next will not overlap for sure.
827 if (pos->start >= map->end)
833 pr_debug("overlapping maps in %s (disable tui for more info)\n",
836 fputs("overlapping maps:\n", fp);
837 map__fprintf(map, fp);
838 map__fprintf(pos, fp);
842 rb_erase_init(&pos->rb_node, root);
844 * Now check if we need to create new maps for areas not
845 * overlapped by the new map:
847 if (map->start > pos->start) {
848 struct map *before = map__clone(pos);
850 if (before == NULL) {
855 before->end = map->start;
856 __map_groups__insert(mg, before);
857 if (verbose >= 2 && !use_browser)
858 map__fprintf(before, fp);
862 if (map->end < pos->end) {
863 struct map *after = map__clone(pos);
870 after->start = map->end;
871 after->pgoff += map->end - pos->start;
872 assert(pos->map_ip(pos, map->end) == after->map_ip(after, map->end));
873 __map_groups__insert(mg, after);
874 if (verbose >= 2 && !use_browser)
875 map__fprintf(after, fp);
887 up_write(&maps->lock);
892 * XXX This should not really _copy_ te maps, but refcount them.
894 int map_groups__clone(struct thread *thread, struct map_groups *parent)
896 struct map_groups *mg = thread->mg;
899 struct maps *maps = &parent->maps;
901 down_read(&maps->lock);
903 maps__for_each_entry(maps, map) {
904 struct map *new = map__clone(map);
908 err = unwind__prepare_access(mg, new, NULL);
912 map_groups__insert(mg, new);
918 up_read(&maps->lock);
922 static void __maps__insert(struct maps *maps, struct map *map)
924 struct rb_node **p = &maps->entries.rb_node;
925 struct rb_node *parent = NULL;
926 const u64 ip = map->start;
931 m = rb_entry(parent, struct map, rb_node);
938 rb_link_node(&map->rb_node, parent, p);
939 rb_insert_color(&map->rb_node, &maps->entries);
943 void maps__insert(struct maps *maps, struct map *map)
945 down_write(&maps->lock);
946 __maps__insert(maps, map);
947 up_write(&maps->lock);
950 void __maps__remove(struct maps *maps, struct map *map)
952 rb_erase_init(&map->rb_node, &maps->entries);
956 void maps__remove(struct maps *maps, struct map *map)
958 down_write(&maps->lock);
959 __maps__remove(maps, map);
960 up_write(&maps->lock);
963 struct map *maps__find(struct maps *maps, u64 ip)
968 down_read(&maps->lock);
970 p = maps->entries.rb_node;
972 m = rb_entry(p, struct map, rb_node);
975 else if (ip >= m->end)
983 up_read(&maps->lock);
987 struct map *maps__first(struct maps *maps)
989 struct rb_node *first = rb_first(&maps->entries);
992 return rb_entry(first, struct map, rb_node);
996 static struct map *__map__next(struct map *map)
998 struct rb_node *next = rb_next(&map->rb_node);
1001 return rb_entry(next, struct map, rb_node);
1005 struct map *map__next(struct map *map)
1007 return map ? __map__next(map) : NULL;
1010 struct kmap *__map__kmap(struct map *map)
1012 if (!map->dso || !map->dso->kernel)
1014 return (struct kmap *)(map + 1);
1017 struct kmap *map__kmap(struct map *map)
1019 struct kmap *kmap = __map__kmap(map);
1022 pr_err("Internal error: map__kmap with a non-kernel map\n");
1026 struct map_groups *map__kmaps(struct map *map)
1028 struct kmap *kmap = map__kmap(map);
1030 if (!kmap || !kmap->kmaps) {
1031 pr_err("Internal error: map__kmaps with a non-kernel map\n");