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);
168 map->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 refcount_set(&mg->refcnt, 1);
578 void map_groups__insert(struct map_groups *mg, struct map *map)
580 maps__insert(&mg->maps, map);
583 static void __maps__purge(struct maps *maps)
585 struct map *pos, *next;
587 maps__for_each_entry_safe(maps, pos, next) {
588 rb_erase_init(&pos->rb_node, &maps->entries);
593 static void maps__exit(struct maps *maps)
595 down_write(&maps->lock);
597 up_write(&maps->lock);
600 void map_groups__exit(struct map_groups *mg)
602 maps__exit(&mg->maps);
605 bool map_groups__empty(struct map_groups *mg)
607 return !maps__first(&mg->maps);
610 struct map_groups *map_groups__new(struct machine *machine)
612 struct map_groups *mg = zalloc(sizeof(*mg));
615 map_groups__init(mg, machine);
620 void map_groups__delete(struct map_groups *mg)
622 map_groups__exit(mg);
623 unwind__finish_access(mg);
627 void map_groups__put(struct map_groups *mg)
629 if (mg && refcount_dec_and_test(&mg->refcnt))
630 map_groups__delete(mg);
633 struct symbol *map_groups__find_symbol(struct map_groups *mg,
634 u64 addr, struct map **mapp)
636 struct map *map = map_groups__find(mg, addr);
638 /* Ensure map is loaded before using map->map_ip */
639 if (map != NULL && map__load(map) >= 0) {
642 return map__find_symbol(map, map->map_ip(map, addr));
648 static bool map__contains_symbol(struct map *map, struct symbol *sym)
650 u64 ip = map->unmap_ip(map, sym->start);
652 return ip >= map->start && ip < map->end;
655 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
661 down_read(&maps->lock);
663 maps__for_each_entry(maps, pos) {
664 sym = map__find_symbol_by_name(pos, name);
668 if (!map__contains_symbol(pos, sym)) {
679 up_read(&maps->lock);
683 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
687 return maps__find_symbol_by_name(&mg->maps, name, mapp);
690 int map_groups__find_ams(struct map_groups *mg, struct addr_map_symbol *ams)
692 if (ams->addr < ams->ms.map->start || ams->addr >= ams->ms.map->end) {
695 ams->ms.map = map_groups__find(mg, ams->addr);
696 if (ams->ms.map == NULL)
700 ams->al_addr = ams->ms.map->map_ip(ams->ms.map, ams->addr);
701 ams->ms.sym = map__find_symbol(ams->ms.map, ams->al_addr);
703 return ams->ms.sym ? 0 : -1;
706 static size_t maps__fprintf(struct maps *maps, FILE *fp)
711 down_read(&maps->lock);
713 maps__for_each_entry(maps, pos) {
714 printed += fprintf(fp, "Map:");
715 printed += map__fprintf(pos, fp);
717 printed += dso__fprintf(pos->dso, fp);
718 printed += fprintf(fp, "--\n");
722 up_read(&maps->lock);
727 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
729 return maps__fprintf(&mg->maps, fp);
732 static void __map_groups__insert(struct map_groups *mg, struct map *map)
734 __maps__insert(&mg->maps, map);
737 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, FILE *fp)
739 struct maps *maps = &mg->maps;
740 struct rb_root *root;
741 struct rb_node *next, *first;
744 down_write(&maps->lock);
746 root = &maps->entries;
749 * Find first map where end > map->start.
750 * Same as find_vma() in kernel.
752 next = root->rb_node;
755 struct map *pos = rb_entry(next, struct map, rb_node);
757 if (pos->end > map->start) {
759 if (pos->start <= map->start)
761 next = next->rb_left;
763 next = next->rb_right;
768 struct map *pos = rb_entry(next, struct map, rb_node);
769 next = rb_next(&pos->rb_node);
772 * Stop if current map starts after map->end.
773 * Maps are ordered by start: next will not overlap for sure.
775 if (pos->start >= map->end)
781 pr_debug("overlapping maps in %s (disable tui for more info)\n",
784 fputs("overlapping maps:\n", fp);
785 map__fprintf(map, fp);
786 map__fprintf(pos, fp);
790 rb_erase_init(&pos->rb_node, root);
792 * Now check if we need to create new maps for areas not
793 * overlapped by the new map:
795 if (map->start > pos->start) {
796 struct map *before = map__clone(pos);
798 if (before == NULL) {
803 before->end = map->start;
804 __map_groups__insert(mg, before);
805 if (verbose >= 2 && !use_browser)
806 map__fprintf(before, fp);
810 if (map->end < pos->end) {
811 struct map *after = map__clone(pos);
818 after->start = map->end;
819 after->pgoff += map->end - pos->start;
820 assert(pos->map_ip(pos, map->end) == after->map_ip(after, map->end));
821 __map_groups__insert(mg, after);
822 if (verbose >= 2 && !use_browser)
823 map__fprintf(after, fp);
835 up_write(&maps->lock);
840 * XXX This should not really _copy_ te maps, but refcount them.
842 int map_groups__clone(struct thread *thread, struct map_groups *parent)
844 struct map_groups *mg = thread->mg;
847 struct maps *maps = &parent->maps;
849 down_read(&maps->lock);
851 maps__for_each_entry(maps, map) {
852 struct map *new = map__clone(map);
856 err = unwind__prepare_access(mg, new, NULL);
860 map_groups__insert(mg, new);
866 up_read(&maps->lock);
870 static void __maps__insert(struct maps *maps, struct map *map)
872 struct rb_node **p = &maps->entries.rb_node;
873 struct rb_node *parent = NULL;
874 const u64 ip = map->start;
879 m = rb_entry(parent, struct map, rb_node);
886 rb_link_node(&map->rb_node, parent, p);
887 rb_insert_color(&map->rb_node, &maps->entries);
891 void maps__insert(struct maps *maps, struct map *map)
893 down_write(&maps->lock);
894 __maps__insert(maps, map);
895 up_write(&maps->lock);
898 static void __maps__remove(struct maps *maps, struct map *map)
900 rb_erase_init(&map->rb_node, &maps->entries);
904 void maps__remove(struct maps *maps, struct map *map)
906 down_write(&maps->lock);
907 __maps__remove(maps, map);
908 up_write(&maps->lock);
911 struct map *maps__find(struct maps *maps, u64 ip)
916 down_read(&maps->lock);
918 p = maps->entries.rb_node;
920 m = rb_entry(p, struct map, rb_node);
923 else if (ip >= m->end)
931 up_read(&maps->lock);
935 struct map *maps__first(struct maps *maps)
937 struct rb_node *first = rb_first(&maps->entries);
940 return rb_entry(first, struct map, rb_node);
944 static struct map *__map__next(struct map *map)
946 struct rb_node *next = rb_next(&map->rb_node);
949 return rb_entry(next, struct map, rb_node);
953 struct map *map__next(struct map *map)
955 return map ? __map__next(map) : NULL;
958 struct kmap *__map__kmap(struct map *map)
960 if (!map->dso || !map->dso->kernel)
962 return (struct kmap *)(map + 1);
965 struct kmap *map__kmap(struct map *map)
967 struct kmap *kmap = __map__kmap(map);
970 pr_err("Internal error: map__kmap with a non-kernel map\n");
974 struct map_groups *map__kmaps(struct map *map)
976 struct kmap *kmap = map__kmap(map);
978 if (!kmap || !kmap->kmaps) {
979 pr_err("Internal error: map__kmaps with a non-kernel map\n");