1 // SPDX-License-Identifier: GPL-2.0
10 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
13 #include "map_symbol.h"
19 #include <linux/string.h>
20 #include <linux/zalloc.h>
22 #include "namespaces.h"
27 static void __maps__insert(struct maps *maps, struct map *map);
28 static void __maps__insert_name(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);
143 map->erange_warned = false;
144 refcount_set(&map->refcnt, 1);
147 struct map *map__new(struct machine *machine, u64 start, u64 len,
148 u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
149 u64 ino_gen, u32 prot, u32 flags, char *filename,
150 struct thread *thread)
152 struct map *map = malloc(sizeof(*map));
153 struct nsinfo *nsi = NULL;
157 char newfilename[PATH_MAX];
159 int anon, no_dso, vdso, android;
161 android = is_android_lib(filename);
162 anon = is_anon_memory(filename, flags);
163 vdso = is_vdso_map(filename);
164 no_dso = is_no_dso_memory(filename);
169 map->ino_generation = ino_gen;
172 nsi = nsinfo__get(thread->nsinfo);
174 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
175 snprintf(newfilename, sizeof(newfilename),
176 "/tmp/perf-%d.map", nsi->pid);
177 filename = newfilename;
181 if (replace_android_lib(filename, newfilename))
182 filename = newfilename;
186 /* The vdso maps are always on the host and not the
187 * container. Ensure that we don't use setns to look
190 nnsi = nsinfo__copy(nsi);
193 nnsi->need_setns = false;
197 dso = machine__findnew_vdso(machine, thread);
199 dso = machine__findnew_dso(machine, filename);
204 map__init(map, start, start + len, pgoff, dso);
206 if (anon || no_dso) {
207 map->map_ip = map->unmap_ip = identity__map_ip;
210 * Set memory without DSO as loaded. All map__find_*
211 * functions still return NULL, and we avoid the
212 * unnecessary map__load warning.
214 if (!(prot & PROT_EXEC))
215 dso__set_loaded(dso);
228 * Constructor variant for modules (where we know from /proc/modules where
229 * they are loaded) and for vmlinux, where only after we load all the
230 * symbols we'll know where it starts and ends.
232 struct map *map__new2(u64 start, struct dso *dso)
234 struct map *map = calloc(1, (sizeof(*map) +
235 (dso->kernel ? sizeof(struct kmap) : 0)));
238 * ->end will be filled after we load all the symbols
240 map__init(map, start, 0, 0, dso);
247 * Use this and __map__is_kmodule() for map instances that are in
248 * machine->kmaps, and thus have map->groups->machine all properly set, to
249 * disambiguate between the kernel and modules.
251 * When the need arises, introduce map__is_{kernel,kmodule)() that
252 * checks (map->groups != NULL && map->groups->machine != NULL &&
253 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
255 bool __map__is_kernel(const struct map *map)
257 return machine__kernel_map(map->groups->machine) == map;
260 bool __map__is_extra_kernel_map(const struct map *map)
262 struct kmap *kmap = __map__kmap((struct map *)map);
264 return kmap && kmap->name[0];
267 bool __map__is_bpf_prog(const struct map *map)
271 if (map->dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
275 * If PERF_RECORD_BPF_EVENT is not included, the dso will not have
276 * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can
277 * guess the type based on name.
279 name = map->dso->short_name;
280 return name && (strstr(name, "bpf_prog_") == name);
283 bool map__has_symbols(const struct map *map)
285 return dso__has_symbols(map->dso);
288 static void map__exit(struct map *map)
290 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
294 void map__delete(struct map *map)
300 void map__put(struct map *map)
302 if (map && refcount_dec_and_test(&map->refcnt))
306 void map__fixup_start(struct map *map)
308 struct rb_root_cached *symbols = &map->dso->symbols;
309 struct rb_node *nd = rb_first_cached(symbols);
311 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
312 map->start = sym->start;
316 void map__fixup_end(struct map *map)
318 struct rb_root_cached *symbols = &map->dso->symbols;
319 struct rb_node *nd = rb_last(&symbols->rb_root);
321 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
326 #define DSO__DELETED "(deleted)"
328 int map__load(struct map *map)
330 const char *name = map->dso->long_name;
333 if (dso__loaded(map->dso))
336 nr = dso__load(map->dso, map);
338 if (map->dso->has_build_id) {
339 char sbuild_id[SBUILD_ID_SIZE];
341 build_id__sprintf(map->dso->build_id,
342 sizeof(map->dso->build_id),
344 pr_debug("%s with build id %s not found", name, sbuild_id);
346 pr_debug("Failed to open %s", name);
348 pr_debug(", continuing without symbols\n");
350 } else if (nr == 0) {
351 #ifdef HAVE_LIBELF_SUPPORT
352 const size_t len = strlen(name);
353 const size_t real_len = len - sizeof(DSO__DELETED);
355 if (len > sizeof(DSO__DELETED) &&
356 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
357 pr_debug("%.*s was updated (is prelink enabled?). "
358 "Restart the long running apps that use it!\n",
359 (int)real_len, name);
361 pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
370 struct symbol *map__find_symbol(struct map *map, u64 addr)
372 if (map__load(map) < 0)
375 return dso__find_symbol(map->dso, addr);
378 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
380 if (map__load(map) < 0)
383 if (!dso__sorted_by_name(map->dso))
384 dso__sort_by_name(map->dso);
386 return dso__find_symbol_by_name(map->dso, name);
389 struct map *map__clone(struct map *from)
391 struct map *map = memdup(from, sizeof(*map));
394 refcount_set(&map->refcnt, 1);
395 RB_CLEAR_NODE(&map->rb_node);
403 size_t map__fprintf(struct map *map, FILE *fp)
405 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
406 map->start, map->end, map->pgoff, map->dso->name);
409 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
411 char buf[symbol_conf.pad_output_len_dso + 1];
412 const char *dsoname = "[unknown]";
414 if (map && map->dso) {
415 if (symbol_conf.show_kernel_path && map->dso->long_name)
416 dsoname = map->dso->long_name;
418 dsoname = map->dso->name;
421 if (symbol_conf.pad_output_len_dso) {
422 scnprintf_pad(buf, symbol_conf.pad_output_len_dso, "%s", dsoname);
426 return fprintf(fp, "%s", dsoname);
429 char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
432 return SRCLINE_UNKNOWN;
433 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
436 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
441 if (map && map->dso) {
442 char *srcline = map__srcline(map, addr, NULL);
443 if (srcline != SRCLINE_UNKNOWN)
444 ret = fprintf(fp, "%s%s", prefix, srcline);
445 free_srcline(srcline);
450 int map__fprintf_srccode(struct map *map, u64 addr,
452 struct srccode_state *state)
460 if (!map || !map->dso)
462 srcfile = get_srcline_split(map->dso,
463 map__rip_2objdump(map, addr),
468 /* Avoid redundant printing */
471 !strcmp(state->srcfile, srcfile) &&
472 state->line == line) {
477 srccode = find_sourceline(srcfile, line, &len);
481 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
484 state->srcfile = srcfile;
495 void srccode_state_free(struct srccode_state *state)
497 zfree(&state->srcfile);
502 * map__rip_2objdump - convert symbol start address to objdump address.
504 * @rip: symbol start address
506 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
507 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
508 * relative to section start.
510 * Return: Address suitable for passing to "objdump --start-address="
512 u64 map__rip_2objdump(struct map *map, u64 rip)
514 struct kmap *kmap = __map__kmap(map);
517 * vmlinux does not have program headers for PTI entry trampolines and
518 * kcore may not either. However the trampoline object code is on the
519 * main kernel map, so just use that instead.
521 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
522 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
528 if (!map->dso->adjust_symbols)
532 return rip - map->pgoff;
535 * kernel modules also have DSO_TYPE_USER in dso->kernel,
536 * but all kernel modules are ET_REL, so won't get here.
538 if (map->dso->kernel == DSO_TYPE_USER)
539 return rip + map->dso->text_offset;
541 return map->unmap_ip(map, rip) - map->reloc;
545 * map__objdump_2mem - convert objdump address to a memory address.
547 * @ip: objdump address
549 * Closely related to map__rip_2objdump(), this function takes an address from
550 * objdump and converts it to a memory address. Note this assumes that @map
551 * contains the address. To be sure the result is valid, check it forwards
552 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
554 * Return: Memory address.
556 u64 map__objdump_2mem(struct map *map, u64 ip)
558 if (!map->dso->adjust_symbols)
559 return map->unmap_ip(map, ip);
562 return map->unmap_ip(map, ip + map->pgoff);
565 * kernel modules also have DSO_TYPE_USER in dso->kernel,
566 * but all kernel modules are ET_REL, so won't get here.
568 if (map->dso->kernel == DSO_TYPE_USER)
569 return map->unmap_ip(map, ip - map->dso->text_offset);
571 return ip + map->reloc;
574 static void maps__init(struct maps *maps)
576 maps->entries = RB_ROOT;
577 maps->names = RB_ROOT;
578 init_rwsem(&maps->lock);
581 void map_groups__init(struct map_groups *mg, struct machine *machine)
583 maps__init(&mg->maps);
584 mg->machine = machine;
585 refcount_set(&mg->refcnt, 1);
588 void map_groups__insert(struct map_groups *mg, struct map *map)
590 maps__insert(&mg->maps, map);
594 static void __maps__purge(struct maps *maps)
596 struct rb_root *root = &maps->entries;
597 struct rb_node *next = rb_first(root);
600 struct map *pos = rb_entry(next, struct map, rb_node);
602 next = rb_next(&pos->rb_node);
603 rb_erase_init(&pos->rb_node, root);
608 static void __maps__purge_names(struct maps *maps)
610 struct rb_root *root = &maps->names;
611 struct rb_node *next = rb_first(root);
614 struct map *pos = rb_entry(next, struct map, rb_node_name);
616 next = rb_next(&pos->rb_node_name);
617 rb_erase_init(&pos->rb_node_name, root);
622 static void maps__exit(struct maps *maps)
624 down_write(&maps->lock);
626 __maps__purge_names(maps);
627 up_write(&maps->lock);
630 void map_groups__exit(struct map_groups *mg)
632 maps__exit(&mg->maps);
635 bool map_groups__empty(struct map_groups *mg)
637 return !maps__first(&mg->maps);
640 struct map_groups *map_groups__new(struct machine *machine)
642 struct map_groups *mg = zalloc(sizeof(*mg));
645 map_groups__init(mg, machine);
650 void map_groups__delete(struct map_groups *mg)
652 map_groups__exit(mg);
653 unwind__finish_access(mg);
657 void map_groups__put(struct map_groups *mg)
659 if (mg && refcount_dec_and_test(&mg->refcnt))
660 map_groups__delete(mg);
663 struct symbol *map_groups__find_symbol(struct map_groups *mg,
664 u64 addr, struct map **mapp)
666 struct map *map = map_groups__find(mg, addr);
668 /* Ensure map is loaded before using map->map_ip */
669 if (map != NULL && map__load(map) >= 0) {
672 return map__find_symbol(map, map->map_ip(map, addr));
678 static bool map__contains_symbol(struct map *map, struct symbol *sym)
680 u64 ip = map->unmap_ip(map, sym->start);
682 return ip >= map->start && ip < map->end;
685 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
691 down_read(&maps->lock);
693 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
694 struct map *pos = rb_entry(nd, struct map, rb_node);
696 sym = map__find_symbol_by_name(pos, name);
700 if (!map__contains_symbol(pos, sym)) {
711 up_read(&maps->lock);
715 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
719 return maps__find_symbol_by_name(&mg->maps, name, mapp);
722 int map_groups__find_ams(struct addr_map_symbol *ams)
724 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
725 if (ams->map->groups == NULL)
727 ams->map = map_groups__find(ams->map->groups, ams->addr);
728 if (ams->map == NULL)
732 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
733 ams->sym = map__find_symbol(ams->map, ams->al_addr);
735 return ams->sym ? 0 : -1;
738 static size_t maps__fprintf(struct maps *maps, FILE *fp)
743 down_read(&maps->lock);
745 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
746 struct map *pos = rb_entry(nd, struct map, rb_node);
747 printed += fprintf(fp, "Map:");
748 printed += map__fprintf(pos, fp);
750 printed += dso__fprintf(pos->dso, fp);
751 printed += fprintf(fp, "--\n");
755 up_read(&maps->lock);
760 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
762 return maps__fprintf(&mg->maps, fp);
765 static void __map_groups__insert(struct map_groups *mg, struct map *map)
767 __maps__insert(&mg->maps, map);
768 __maps__insert_name(&mg->maps, map);
772 static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
774 struct rb_root *root;
775 struct rb_node *next, *first;
778 down_write(&maps->lock);
780 root = &maps->entries;
783 * Find first map where end > map->start.
784 * Same as find_vma() in kernel.
786 next = root->rb_node;
789 struct map *pos = rb_entry(next, struct map, rb_node);
791 if (pos->end > map->start) {
793 if (pos->start <= map->start)
795 next = next->rb_left;
797 next = next->rb_right;
802 struct map *pos = rb_entry(next, struct map, rb_node);
803 next = rb_next(&pos->rb_node);
806 * Stop if current map starts after map->end.
807 * Maps are ordered by start: next will not overlap for sure.
809 if (pos->start >= map->end)
815 pr_debug("overlapping maps in %s (disable tui for more info)\n",
818 fputs("overlapping maps:\n", fp);
819 map__fprintf(map, fp);
820 map__fprintf(pos, fp);
824 rb_erase_init(&pos->rb_node, root);
826 * Now check if we need to create new maps for areas not
827 * overlapped by the new map:
829 if (map->start > pos->start) {
830 struct map *before = map__clone(pos);
832 if (before == NULL) {
837 before->end = map->start;
838 __map_groups__insert(pos->groups, before);
839 if (verbose >= 2 && !use_browser)
840 map__fprintf(before, fp);
844 if (map->end < pos->end) {
845 struct map *after = map__clone(pos);
852 after->start = map->end;
853 __map_groups__insert(pos->groups, after);
854 if (verbose >= 2 && !use_browser)
855 map__fprintf(after, fp);
867 up_write(&maps->lock);
871 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
874 return maps__fixup_overlappings(&mg->maps, map, fp);
878 * XXX This should not really _copy_ te maps, but refcount them.
880 int map_groups__clone(struct thread *thread, struct map_groups *parent)
882 struct map_groups *mg = thread->mg;
885 struct maps *maps = &parent->maps;
887 down_read(&maps->lock);
889 for (map = maps__first(maps); map; map = map__next(map)) {
890 struct map *new = map__clone(map);
894 err = unwind__prepare_access(mg, new, NULL);
898 map_groups__insert(mg, new);
904 up_read(&maps->lock);
908 static void __maps__insert(struct maps *maps, struct map *map)
910 struct rb_node **p = &maps->entries.rb_node;
911 struct rb_node *parent = NULL;
912 const u64 ip = map->start;
917 m = rb_entry(parent, struct map, rb_node);
924 rb_link_node(&map->rb_node, parent, p);
925 rb_insert_color(&map->rb_node, &maps->entries);
929 static void __maps__insert_name(struct maps *maps, struct map *map)
931 struct rb_node **p = &maps->names.rb_node;
932 struct rb_node *parent = NULL;
938 m = rb_entry(parent, struct map, rb_node_name);
939 rc = strcmp(m->dso->short_name, map->dso->short_name);
945 rb_link_node(&map->rb_node_name, parent, p);
946 rb_insert_color(&map->rb_node_name, &maps->names);
950 void maps__insert(struct maps *maps, struct map *map)
952 down_write(&maps->lock);
953 __maps__insert(maps, map);
954 __maps__insert_name(maps, map);
955 up_write(&maps->lock);
958 static void __maps__remove(struct maps *maps, struct map *map)
960 rb_erase_init(&map->rb_node, &maps->entries);
963 rb_erase_init(&map->rb_node_name, &maps->names);
967 void maps__remove(struct maps *maps, struct map *map)
969 down_write(&maps->lock);
970 __maps__remove(maps, map);
971 up_write(&maps->lock);
974 struct map *maps__find(struct maps *maps, u64 ip)
979 down_read(&maps->lock);
981 p = maps->entries.rb_node;
983 m = rb_entry(p, struct map, rb_node);
986 else if (ip >= m->end)
994 up_read(&maps->lock);
998 struct map *maps__first(struct maps *maps)
1000 struct rb_node *first = rb_first(&maps->entries);
1003 return rb_entry(first, struct map, rb_node);
1007 struct map *map__next(struct map *map)
1009 struct rb_node *next = rb_next(&map->rb_node);
1012 return rb_entry(next, struct map, rb_node);
1016 struct kmap *__map__kmap(struct map *map)
1018 if (!map->dso || !map->dso->kernel)
1020 return (struct kmap *)(map + 1);
1023 struct kmap *map__kmap(struct map *map)
1025 struct kmap *kmap = __map__kmap(map);
1028 pr_err("Internal error: map__kmap with a non-kernel map\n");
1032 struct map_groups *map__kmaps(struct map *map)
1034 struct kmap *kmap = map__kmap(map);
1036 if (!kmap || !kmap->kmaps) {
1037 pr_err("Internal error: map__kmaps with a non-kernel map\n");