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_debug("%s with build id %s not found", name, sbuild_id);
325 pr_debug("Failed to open %s", name);
327 pr_debug(", continuing without symbols\n");
329 } else if (nr == 0) {
330 #ifdef HAVE_LIBELF_SUPPORT
331 const size_t len = strlen(name);
332 const size_t real_len = len - sizeof(DSO__DELETED);
334 if (len > sizeof(DSO__DELETED) &&
335 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
336 pr_debug("%.*s was updated (is prelink enabled?). "
337 "Restart the long running apps that use it!\n",
338 (int)real_len, name);
340 pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
349 struct symbol *map__find_symbol(struct map *map, u64 addr)
351 if (map__load(map) < 0)
354 return dso__find_symbol(map->dso, addr);
357 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
359 if (map__load(map) < 0)
362 if (!dso__sorted_by_name(map->dso))
363 dso__sort_by_name(map->dso);
365 return dso__find_symbol_by_name(map->dso, name);
368 struct map *map__clone(struct map *from)
370 struct map *map = memdup(from, sizeof(*map));
373 refcount_set(&map->refcnt, 1);
374 RB_CLEAR_NODE(&map->rb_node);
382 size_t map__fprintf(struct map *map, FILE *fp)
384 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
385 map->start, map->end, map->pgoff, map->dso->name);
388 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
390 const char *dsoname = "[unknown]";
392 if (map && map->dso) {
393 if (symbol_conf.show_kernel_path && map->dso->long_name)
394 dsoname = map->dso->long_name;
396 dsoname = map->dso->name;
399 return fprintf(fp, "%s", dsoname);
402 char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
405 return SRCLINE_UNKNOWN;
406 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
409 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
414 if (map && map->dso) {
415 char *srcline = map__srcline(map, addr, NULL);
416 if (srcline != SRCLINE_UNKNOWN)
417 ret = fprintf(fp, "%s%s", prefix, srcline);
418 free_srcline(srcline);
424 * map__rip_2objdump - convert symbol start address to objdump address.
426 * @rip: symbol start address
428 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
429 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
430 * relative to section start.
432 * Return: Address suitable for passing to "objdump --start-address="
434 u64 map__rip_2objdump(struct map *map, u64 rip)
436 struct kmap *kmap = __map__kmap(map);
439 * vmlinux does not have program headers for PTI entry trampolines and
440 * kcore may not either. However the trampoline object code is on the
441 * main kernel map, so just use that instead.
443 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
444 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
450 if (!map->dso->adjust_symbols)
454 return rip - map->pgoff;
457 * kernel modules also have DSO_TYPE_USER in dso->kernel,
458 * but all kernel modules are ET_REL, so won't get here.
460 if (map->dso->kernel == DSO_TYPE_USER)
461 return rip + map->dso->text_offset;
463 return map->unmap_ip(map, rip) - map->reloc;
467 * map__objdump_2mem - convert objdump address to a memory address.
469 * @ip: objdump address
471 * Closely related to map__rip_2objdump(), this function takes an address from
472 * objdump and converts it to a memory address. Note this assumes that @map
473 * contains the address. To be sure the result is valid, check it forwards
474 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
476 * Return: Memory address.
478 u64 map__objdump_2mem(struct map *map, u64 ip)
480 if (!map->dso->adjust_symbols)
481 return map->unmap_ip(map, ip);
484 return map->unmap_ip(map, ip + map->pgoff);
487 * kernel modules also have DSO_TYPE_USER in dso->kernel,
488 * but all kernel modules are ET_REL, so won't get here.
490 if (map->dso->kernel == DSO_TYPE_USER)
491 return map->unmap_ip(map, ip - map->dso->text_offset);
493 return ip + map->reloc;
496 static void maps__init(struct maps *maps)
498 maps->entries = RB_ROOT;
499 init_rwsem(&maps->lock);
502 void map_groups__init(struct map_groups *mg, struct machine *machine)
504 maps__init(&mg->maps);
505 mg->machine = machine;
506 refcount_set(&mg->refcnt, 1);
509 static void __maps__purge(struct maps *maps)
511 struct rb_root *root = &maps->entries;
512 struct rb_node *next = rb_first(root);
515 struct map *pos = rb_entry(next, struct map, rb_node);
517 next = rb_next(&pos->rb_node);
518 rb_erase_init(&pos->rb_node, root);
523 static void maps__exit(struct maps *maps)
525 down_write(&maps->lock);
527 up_write(&maps->lock);
530 void map_groups__exit(struct map_groups *mg)
532 maps__exit(&mg->maps);
535 bool map_groups__empty(struct map_groups *mg)
537 return !maps__first(&mg->maps);
540 struct map_groups *map_groups__new(struct machine *machine)
542 struct map_groups *mg = malloc(sizeof(*mg));
545 map_groups__init(mg, machine);
550 void map_groups__delete(struct map_groups *mg)
552 map_groups__exit(mg);
556 void map_groups__put(struct map_groups *mg)
558 if (mg && refcount_dec_and_test(&mg->refcnt))
559 map_groups__delete(mg);
562 struct symbol *map_groups__find_symbol(struct map_groups *mg,
563 u64 addr, struct map **mapp)
565 struct map *map = map_groups__find(mg, addr);
567 /* Ensure map is loaded before using map->map_ip */
568 if (map != NULL && map__load(map) >= 0) {
571 return map__find_symbol(map, map->map_ip(map, addr));
577 static bool map__contains_symbol(struct map *map, struct symbol *sym)
579 u64 ip = map->unmap_ip(map, sym->start);
581 return ip >= map->start && ip < map->end;
584 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
590 down_read(&maps->lock);
592 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
593 struct map *pos = rb_entry(nd, struct map, rb_node);
595 sym = map__find_symbol_by_name(pos, name);
599 if (!map__contains_symbol(pos, sym)) {
610 up_read(&maps->lock);
614 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
618 return maps__find_symbol_by_name(&mg->maps, name, mapp);
621 int map_groups__find_ams(struct addr_map_symbol *ams)
623 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
624 if (ams->map->groups == NULL)
626 ams->map = map_groups__find(ams->map->groups, ams->addr);
627 if (ams->map == NULL)
631 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
632 ams->sym = map__find_symbol(ams->map, ams->al_addr);
634 return ams->sym ? 0 : -1;
637 static size_t maps__fprintf(struct maps *maps, FILE *fp)
642 down_read(&maps->lock);
644 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
645 struct map *pos = rb_entry(nd, struct map, rb_node);
646 printed += fprintf(fp, "Map:");
647 printed += map__fprintf(pos, fp);
649 printed += dso__fprintf(pos->dso, fp);
650 printed += fprintf(fp, "--\n");
654 up_read(&maps->lock);
659 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
661 return maps__fprintf(&mg->maps, fp);
664 static void __map_groups__insert(struct map_groups *mg, struct map *map)
666 __maps__insert(&mg->maps, map);
670 static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
672 struct rb_root *root;
673 struct rb_node *next, *first;
676 down_write(&maps->lock);
678 root = &maps->entries;
681 * Find first map where end > map->start.
682 * Same as find_vma() in kernel.
684 next = root->rb_node;
687 struct map *pos = rb_entry(next, struct map, rb_node);
689 if (pos->end > map->start) {
691 if (pos->start <= map->start)
693 next = next->rb_left;
695 next = next->rb_right;
700 struct map *pos = rb_entry(next, struct map, rb_node);
701 next = rb_next(&pos->rb_node);
704 * Stop if current map starts after map->end.
705 * Maps are ordered by start: next will not overlap for sure.
707 if (pos->start >= map->end)
713 pr_debug("overlapping maps in %s (disable tui for more info)\n",
716 fputs("overlapping maps:\n", fp);
717 map__fprintf(map, fp);
718 map__fprintf(pos, fp);
722 rb_erase_init(&pos->rb_node, root);
724 * Now check if we need to create new maps for areas not
725 * overlapped by the new map:
727 if (map->start > pos->start) {
728 struct map *before = map__clone(pos);
730 if (before == NULL) {
735 before->end = map->start;
736 __map_groups__insert(pos->groups, before);
737 if (verbose >= 2 && !use_browser)
738 map__fprintf(before, fp);
742 if (map->end < pos->end) {
743 struct map *after = map__clone(pos);
750 after->start = map->end;
751 __map_groups__insert(pos->groups, after);
752 if (verbose >= 2 && !use_browser)
753 map__fprintf(after, fp);
765 up_write(&maps->lock);
769 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
772 return maps__fixup_overlappings(&mg->maps, map, fp);
776 * XXX This should not really _copy_ te maps, but refcount them.
778 int map_groups__clone(struct thread *thread, struct map_groups *parent)
780 struct map_groups *mg = thread->mg;
783 struct maps *maps = &parent->maps;
785 down_read(&maps->lock);
787 for (map = maps__first(maps); map; map = map__next(map)) {
788 struct map *new = map__clone(map);
792 err = unwind__prepare_access(thread, new, NULL);
796 map_groups__insert(mg, new);
802 up_read(&maps->lock);
806 static void __maps__insert(struct maps *maps, struct map *map)
808 struct rb_node **p = &maps->entries.rb_node;
809 struct rb_node *parent = NULL;
810 const u64 ip = map->start;
815 m = rb_entry(parent, struct map, rb_node);
822 rb_link_node(&map->rb_node, parent, p);
823 rb_insert_color(&map->rb_node, &maps->entries);
827 void maps__insert(struct maps *maps, struct map *map)
829 down_write(&maps->lock);
830 __maps__insert(maps, map);
831 up_write(&maps->lock);
834 static void __maps__remove(struct maps *maps, struct map *map)
836 rb_erase_init(&map->rb_node, &maps->entries);
840 void maps__remove(struct maps *maps, struct map *map)
842 down_write(&maps->lock);
843 __maps__remove(maps, map);
844 up_write(&maps->lock);
847 struct map *maps__find(struct maps *maps, u64 ip)
849 struct rb_node **p, *parent = NULL;
852 down_read(&maps->lock);
854 p = &maps->entries.rb_node;
857 m = rb_entry(parent, struct map, rb_node);
860 else if (ip >= m->end)
868 up_read(&maps->lock);
872 struct map *maps__first(struct maps *maps)
874 struct rb_node *first = rb_first(&maps->entries);
877 return rb_entry(first, struct map, rb_node);
881 struct map *map__next(struct map *map)
883 struct rb_node *next = rb_next(&map->rb_node);
886 return rb_entry(next, struct map, rb_node);
890 struct kmap *__map__kmap(struct map *map)
892 if (!map->dso || !map->dso->kernel)
894 return (struct kmap *)(map + 1);
897 struct kmap *map__kmap(struct map *map)
899 struct kmap *kmap = __map__kmap(map);
902 pr_err("Internal error: map__kmap with a non-kernel map\n");
906 struct map_groups *map__kmaps(struct map *map)
908 struct kmap *kmap = map__kmap(map);
910 if (!kmap || !kmap->kmaps) {
911 pr_err("Internal error: map__kmaps with a non-kernel map\n");