]>
git.ipfire.org Git - thirdparty/kernel/linux.git/blob - tools/perf/util/map.c
e32628cd20a7f36e0e06efb3ee5c2e6cae09dabb
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"
24 static void __maps__insert(struct maps
*maps
, struct map
*map
);
25 static void __maps__insert_name(struct maps
*maps
, struct map
*map
);
27 static inline int is_anon_memory(const char *filename
, u32 flags
)
29 return flags
& MAP_HUGETLB
||
30 !strcmp(filename
, "//anon") ||
31 !strncmp(filename
, "/dev/zero", sizeof("/dev/zero") - 1) ||
32 !strncmp(filename
, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
35 static inline int is_no_dso_memory(const char *filename
)
37 return !strncmp(filename
, "[stack", 6) ||
38 !strncmp(filename
, "/SYSV",5) ||
39 !strcmp(filename
, "[heap]");
42 static inline int is_android_lib(const char *filename
)
44 return !strncmp(filename
, "/data/app-lib", 13) ||
45 !strncmp(filename
, "/system/lib", 11);
48 static inline bool replace_android_lib(const char *filename
, char *newfilename
)
52 size_t app_abi_length
, new_length
;
53 size_t lib_length
= 0;
55 libname
= strrchr(filename
, '/');
57 lib_length
= strlen(libname
);
59 app_abi
= getenv("APP_ABI");
63 app_abi_length
= strlen(app_abi
);
65 if (!strncmp(filename
, "/data/app-lib", 13)) {
71 new_length
= 7 + app_abi_length
+ lib_length
;
73 apk_path
= getenv("APK_PATH");
75 new_length
+= strlen(apk_path
) + 1;
76 if (new_length
> PATH_MAX
)
78 snprintf(newfilename
, new_length
,
79 "%s/libs/%s/%s", apk_path
, app_abi
, libname
);
81 if (new_length
> PATH_MAX
)
83 snprintf(newfilename
, new_length
,
84 "libs/%s/%s", app_abi
, libname
);
89 if (!strncmp(filename
, "/system/lib/", 11)) {
95 ndk
= getenv("NDK_ROOT");
96 app
= getenv("APP_PLATFORM");
101 ndk_length
= strlen(ndk
);
102 app_length
= strlen(app
);
104 if (!(ndk_length
&& app_length
&& app_abi_length
))
107 arch
= !strncmp(app_abi
, "arm", 3) ? "arm" :
108 !strncmp(app_abi
, "mips", 4) ? "mips" :
109 !strncmp(app_abi
, "x86", 3) ? "x86" : NULL
;
114 new_length
= 27 + ndk_length
+
115 app_length
+ lib_length
118 if (new_length
> PATH_MAX
)
120 snprintf(newfilename
, new_length
,
121 "%s/platforms/%s/arch-%s/usr/lib/%s",
122 ndk
, app
, arch
, libname
);
129 void map__init(struct map
*map
, u64 start
, u64 end
, u64 pgoff
, struct dso
*dso
)
135 map
->dso
= dso__get(dso
);
136 map
->map_ip
= map__map_ip
;
137 map
->unmap_ip
= map__unmap_ip
;
138 RB_CLEAR_NODE(&map
->rb_node
);
140 map
->erange_warned
= false;
141 refcount_set(&map
->refcnt
, 1);
144 struct map
*map__new(struct machine
*machine
, u64 start
, u64 len
,
145 u64 pgoff
, u32 d_maj
, u32 d_min
, u64 ino
,
146 u64 ino_gen
, u32 prot
, u32 flags
, char *filename
,
147 struct thread
*thread
)
149 struct map
*map
= malloc(sizeof(*map
));
150 struct nsinfo
*nsi
= NULL
;
154 char newfilename
[PATH_MAX
];
156 int anon
, no_dso
, vdso
, android
;
158 android
= is_android_lib(filename
);
159 anon
= is_anon_memory(filename
, flags
);
160 vdso
= is_vdso_map(filename
);
161 no_dso
= is_no_dso_memory(filename
);
166 map
->ino_generation
= ino_gen
;
169 nsi
= nsinfo__get(thread
->nsinfo
);
171 if ((anon
|| no_dso
) && nsi
&& (prot
& PROT_EXEC
)) {
172 snprintf(newfilename
, sizeof(newfilename
),
173 "/tmp/perf-%d.map", nsi
->pid
);
174 filename
= newfilename
;
178 if (replace_android_lib(filename
, newfilename
))
179 filename
= newfilename
;
183 /* The vdso maps are always on the host and not the
184 * container. Ensure that we don't use setns to look
187 nnsi
= nsinfo__copy(nsi
);
190 nnsi
->need_setns
= false;
194 dso
= machine__findnew_vdso(machine
, thread
);
196 dso
= machine__findnew_dso(machine
, filename
);
201 map__init(map
, start
, start
+ len
, pgoff
, dso
);
203 if (anon
|| no_dso
) {
204 map
->map_ip
= map
->unmap_ip
= identity__map_ip
;
207 * Set memory without DSO as loaded. All map__find_*
208 * functions still return NULL, and we avoid the
209 * unnecessary map__load warning.
211 if (!(prot
& PROT_EXEC
))
212 dso__set_loaded(dso
);
225 * Constructor variant for modules (where we know from /proc/modules where
226 * they are loaded) and for vmlinux, where only after we load all the
227 * symbols we'll know where it starts and ends.
229 struct map
*map__new2(u64 start
, struct dso
*dso
)
231 struct map
*map
= calloc(1, (sizeof(*map
) +
232 (dso
->kernel
? sizeof(struct kmap
) : 0)));
235 * ->end will be filled after we load all the symbols
237 map__init(map
, start
, 0, 0, dso
);
244 * Use this and __map__is_kmodule() for map instances that are in
245 * machine->kmaps, and thus have map->groups->machine all properly set, to
246 * disambiguate between the kernel and modules.
248 * When the need arises, introduce map__is_{kernel,kmodule)() that
249 * checks (map->groups != NULL && map->groups->machine != NULL &&
250 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
252 bool __map__is_kernel(const struct map
*map
)
254 return machine__kernel_map(map
->groups
->machine
) == map
;
257 bool __map__is_extra_kernel_map(const struct map
*map
)
259 struct kmap
*kmap
= __map__kmap((struct map
*)map
);
261 return kmap
&& kmap
->name
[0];
264 bool map__has_symbols(const struct map
*map
)
266 return dso__has_symbols(map
->dso
);
269 static void map__exit(struct map
*map
)
271 BUG_ON(!RB_EMPTY_NODE(&map
->rb_node
));
275 void map__delete(struct map
*map
)
281 void map__put(struct map
*map
)
283 if (map
&& refcount_dec_and_test(&map
->refcnt
))
287 void map__fixup_start(struct map
*map
)
289 struct rb_root_cached
*symbols
= &map
->dso
->symbols
;
290 struct rb_node
*nd
= rb_first_cached(symbols
);
292 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
293 map
->start
= sym
->start
;
297 void map__fixup_end(struct map
*map
)
299 struct rb_root_cached
*symbols
= &map
->dso
->symbols
;
300 struct rb_node
*nd
= rb_last(&symbols
->rb_root
);
302 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
307 #define DSO__DELETED "(deleted)"
309 int map__load(struct map
*map
)
311 const char *name
= map
->dso
->long_name
;
314 if (dso__loaded(map
->dso
))
317 nr
= dso__load(map
->dso
, map
);
319 if (map
->dso
->has_build_id
) {
320 char sbuild_id
[SBUILD_ID_SIZE
];
322 build_id__sprintf(map
->dso
->build_id
,
323 sizeof(map
->dso
->build_id
),
325 pr_debug("%s with build id %s not found", name
, sbuild_id
);
327 pr_debug("Failed to open %s", name
);
329 pr_debug(", continuing without symbols\n");
331 } else if (nr
== 0) {
332 #ifdef HAVE_LIBELF_SUPPORT
333 const size_t len
= strlen(name
);
334 const size_t real_len
= len
- sizeof(DSO__DELETED
);
336 if (len
> sizeof(DSO__DELETED
) &&
337 strcmp(name
+ real_len
+ 1, DSO__DELETED
) == 0) {
338 pr_debug("%.*s was updated (is prelink enabled?). "
339 "Restart the long running apps that use it!\n",
340 (int)real_len
, name
);
342 pr_debug("no symbols found in %s, maybe install a debug package?\n", name
);
351 struct symbol
*map__find_symbol(struct map
*map
, u64 addr
)
353 if (map__load(map
) < 0)
356 return dso__find_symbol(map
->dso
, addr
);
359 struct symbol
*map__find_symbol_by_name(struct map
*map
, const char *name
)
361 if (map__load(map
) < 0)
364 if (!dso__sorted_by_name(map
->dso
))
365 dso__sort_by_name(map
->dso
);
367 return dso__find_symbol_by_name(map
->dso
, name
);
370 struct map
*map__clone(struct map
*from
)
372 struct map
*map
= memdup(from
, sizeof(*map
));
375 refcount_set(&map
->refcnt
, 1);
376 RB_CLEAR_NODE(&map
->rb_node
);
384 size_t map__fprintf(struct map
*map
, FILE *fp
)
386 return fprintf(fp
, " %" PRIx64
"-%" PRIx64
" %" PRIx64
" %s\n",
387 map
->start
, map
->end
, map
->pgoff
, map
->dso
->name
);
390 size_t map__fprintf_dsoname(struct map
*map
, FILE *fp
)
392 const char *dsoname
= "[unknown]";
394 if (map
&& map
->dso
) {
395 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
396 dsoname
= map
->dso
->long_name
;
398 dsoname
= map
->dso
->name
;
401 return fprintf(fp
, "%s", dsoname
);
404 char *map__srcline(struct map
*map
, u64 addr
, struct symbol
*sym
)
407 return SRCLINE_UNKNOWN
;
408 return get_srcline(map
->dso
, map__rip_2objdump(map
, addr
), sym
, true, true, addr
);
411 int map__fprintf_srcline(struct map
*map
, u64 addr
, const char *prefix
,
416 if (map
&& map
->dso
) {
417 char *srcline
= map__srcline(map
, addr
, NULL
);
418 if (srcline
!= SRCLINE_UNKNOWN
)
419 ret
= fprintf(fp
, "%s%s", prefix
, srcline
);
420 free_srcline(srcline
);
425 int map__fprintf_srccode(struct map
*map
, u64 addr
,
427 struct srccode_state
*state
)
435 if (!map
|| !map
->dso
)
437 srcfile
= get_srcline_split(map
->dso
,
438 map__rip_2objdump(map
, addr
),
443 /* Avoid redundant printing */
446 !strcmp(state
->srcfile
, srcfile
) &&
447 state
->line
== line
) {
452 srccode
= find_sourceline(srcfile
, line
, &len
);
456 ret
= fprintf(fp
, "|%-8d %.*s", line
, len
, srccode
);
457 state
->srcfile
= srcfile
;
467 void srccode_state_free(struct srccode_state
*state
)
469 zfree(&state
->srcfile
);
474 * map__rip_2objdump - convert symbol start address to objdump address.
476 * @rip: symbol start address
478 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
479 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
480 * relative to section start.
482 * Return: Address suitable for passing to "objdump --start-address="
484 u64
map__rip_2objdump(struct map
*map
, u64 rip
)
486 struct kmap
*kmap
= __map__kmap(map
);
489 * vmlinux does not have program headers for PTI entry trampolines and
490 * kcore may not either. However the trampoline object code is on the
491 * main kernel map, so just use that instead.
493 if (kmap
&& is_entry_trampoline(kmap
->name
) && kmap
->kmaps
&& kmap
->kmaps
->machine
) {
494 struct map
*kernel_map
= machine__kernel_map(kmap
->kmaps
->machine
);
500 if (!map
->dso
->adjust_symbols
)
504 return rip
- map
->pgoff
;
507 * kernel modules also have DSO_TYPE_USER in dso->kernel,
508 * but all kernel modules are ET_REL, so won't get here.
510 if (map
->dso
->kernel
== DSO_TYPE_USER
)
511 return rip
+ map
->dso
->text_offset
;
513 return map
->unmap_ip(map
, rip
) - map
->reloc
;
517 * map__objdump_2mem - convert objdump address to a memory address.
519 * @ip: objdump address
521 * Closely related to map__rip_2objdump(), this function takes an address from
522 * objdump and converts it to a memory address. Note this assumes that @map
523 * contains the address. To be sure the result is valid, check it forwards
524 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
526 * Return: Memory address.
528 u64
map__objdump_2mem(struct map
*map
, u64 ip
)
530 if (!map
->dso
->adjust_symbols
)
531 return map
->unmap_ip(map
, ip
);
534 return map
->unmap_ip(map
, ip
+ map
->pgoff
);
537 * kernel modules also have DSO_TYPE_USER in dso->kernel,
538 * but all kernel modules are ET_REL, so won't get here.
540 if (map
->dso
->kernel
== DSO_TYPE_USER
)
541 return map
->unmap_ip(map
, ip
- map
->dso
->text_offset
);
543 return ip
+ map
->reloc
;
546 static void maps__init(struct maps
*maps
)
548 maps
->entries
= RB_ROOT
;
549 maps
->names
= RB_ROOT
;
550 init_rwsem(&maps
->lock
);
553 void map_groups__init(struct map_groups
*mg
, struct machine
*machine
)
555 maps__init(&mg
->maps
);
556 mg
->machine
= machine
;
557 refcount_set(&mg
->refcnt
, 1);
560 void map_groups__insert(struct map_groups
*mg
, struct map
*map
)
562 maps__insert(&mg
->maps
, map
);
566 static void __maps__purge(struct maps
*maps
)
568 struct rb_root
*root
= &maps
->entries
;
569 struct rb_node
*next
= rb_first(root
);
572 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
574 next
= rb_next(&pos
->rb_node
);
575 rb_erase_init(&pos
->rb_node
, root
);
580 static void __maps__purge_names(struct maps
*maps
)
582 struct rb_root
*root
= &maps
->names
;
583 struct rb_node
*next
= rb_first(root
);
586 struct map
*pos
= rb_entry(next
, struct map
, rb_node_name
);
588 next
= rb_next(&pos
->rb_node_name
);
589 rb_erase_init(&pos
->rb_node_name
, root
);
594 static void maps__exit(struct maps
*maps
)
596 down_write(&maps
->lock
);
598 __maps__purge_names(maps
);
599 up_write(&maps
->lock
);
602 void map_groups__exit(struct map_groups
*mg
)
604 maps__exit(&mg
->maps
);
607 bool map_groups__empty(struct map_groups
*mg
)
609 return !maps__first(&mg
->maps
);
612 struct map_groups
*map_groups__new(struct machine
*machine
)
614 struct map_groups
*mg
= malloc(sizeof(*mg
));
617 map_groups__init(mg
, machine
);
622 void map_groups__delete(struct map_groups
*mg
)
624 map_groups__exit(mg
);
628 void map_groups__put(struct map_groups
*mg
)
630 if (mg
&& refcount_dec_and_test(&mg
->refcnt
))
631 map_groups__delete(mg
);
634 struct symbol
*map_groups__find_symbol(struct map_groups
*mg
,
635 u64 addr
, struct map
**mapp
)
637 struct map
*map
= map_groups__find(mg
, addr
);
639 /* Ensure map is loaded before using map->map_ip */
640 if (map
!= NULL
&& map__load(map
) >= 0) {
643 return map__find_symbol(map
, map
->map_ip(map
, addr
));
649 static bool map__contains_symbol(struct map
*map
, struct symbol
*sym
)
651 u64 ip
= map
->unmap_ip(map
, sym
->start
);
653 return ip
>= map
->start
&& ip
< map
->end
;
656 struct symbol
*maps__find_symbol_by_name(struct maps
*maps
, const char *name
,
662 down_read(&maps
->lock
);
664 for (nd
= rb_first(&maps
->entries
); nd
; nd
= rb_next(nd
)) {
665 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
667 sym
= map__find_symbol_by_name(pos
, name
);
671 if (!map__contains_symbol(pos
, sym
)) {
682 up_read(&maps
->lock
);
686 struct symbol
*map_groups__find_symbol_by_name(struct map_groups
*mg
,
690 return maps__find_symbol_by_name(&mg
->maps
, name
, mapp
);
693 int map_groups__find_ams(struct addr_map_symbol
*ams
)
695 if (ams
->addr
< ams
->map
->start
|| ams
->addr
>= ams
->map
->end
) {
696 if (ams
->map
->groups
== NULL
)
698 ams
->map
= map_groups__find(ams
->map
->groups
, ams
->addr
);
699 if (ams
->map
== NULL
)
703 ams
->al_addr
= ams
->map
->map_ip(ams
->map
, ams
->addr
);
704 ams
->sym
= map__find_symbol(ams
->map
, ams
->al_addr
);
706 return ams
->sym
? 0 : -1;
709 static size_t maps__fprintf(struct maps
*maps
, FILE *fp
)
714 down_read(&maps
->lock
);
716 for (nd
= rb_first(&maps
->entries
); nd
; nd
= rb_next(nd
)) {
717 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
718 printed
+= fprintf(fp
, "Map:");
719 printed
+= map__fprintf(pos
, fp
);
721 printed
+= dso__fprintf(pos
->dso
, fp
);
722 printed
+= fprintf(fp
, "--\n");
726 up_read(&maps
->lock
);
731 size_t map_groups__fprintf(struct map_groups
*mg
, FILE *fp
)
733 return maps__fprintf(&mg
->maps
, fp
);
736 static void __map_groups__insert(struct map_groups
*mg
, struct map
*map
)
738 __maps__insert(&mg
->maps
, map
);
739 __maps__insert_name(&mg
->maps
, map
);
743 static int maps__fixup_overlappings(struct maps
*maps
, struct map
*map
, FILE *fp
)
745 struct rb_root
*root
;
746 struct rb_node
*next
, *first
;
749 down_write(&maps
->lock
);
751 root
= &maps
->entries
;
754 * Find first map where end > map->start.
755 * Same as find_vma() in kernel.
757 next
= root
->rb_node
;
760 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
762 if (pos
->end
> map
->start
) {
764 if (pos
->start
<= map
->start
)
766 next
= next
->rb_left
;
768 next
= next
->rb_right
;
773 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
774 next
= rb_next(&pos
->rb_node
);
777 * Stop if current map starts after map->end.
778 * Maps are ordered by start: next will not overlap for sure.
780 if (pos
->start
>= map
->end
)
786 pr_debug("overlapping maps in %s (disable tui for more info)\n",
789 fputs("overlapping maps:\n", fp
);
790 map__fprintf(map
, fp
);
791 map__fprintf(pos
, fp
);
795 rb_erase_init(&pos
->rb_node
, root
);
797 * Now check if we need to create new maps for areas not
798 * overlapped by the new map:
800 if (map
->start
> pos
->start
) {
801 struct map
*before
= map__clone(pos
);
803 if (before
== NULL
) {
808 before
->end
= map
->start
;
809 __map_groups__insert(pos
->groups
, before
);
810 if (verbose
>= 2 && !use_browser
)
811 map__fprintf(before
, fp
);
815 if (map
->end
< pos
->end
) {
816 struct map
*after
= map__clone(pos
);
823 after
->start
= map
->end
;
824 __map_groups__insert(pos
->groups
, after
);
825 if (verbose
>= 2 && !use_browser
)
826 map__fprintf(after
, fp
);
838 up_write(&maps
->lock
);
842 int map_groups__fixup_overlappings(struct map_groups
*mg
, struct map
*map
,
845 return maps__fixup_overlappings(&mg
->maps
, map
, fp
);
849 * XXX This should not really _copy_ te maps, but refcount them.
851 int map_groups__clone(struct thread
*thread
, struct map_groups
*parent
)
853 struct map_groups
*mg
= thread
->mg
;
856 struct maps
*maps
= &parent
->maps
;
858 down_read(&maps
->lock
);
860 for (map
= maps__first(maps
); map
; map
= map__next(map
)) {
861 struct map
*new = map__clone(map
);
865 err
= unwind__prepare_access(thread
, new, NULL
);
869 map_groups__insert(mg
, new);
875 up_read(&maps
->lock
);
879 static void __maps__insert(struct maps
*maps
, struct map
*map
)
881 struct rb_node
**p
= &maps
->entries
.rb_node
;
882 struct rb_node
*parent
= NULL
;
883 const u64 ip
= map
->start
;
888 m
= rb_entry(parent
, struct map
, rb_node
);
895 rb_link_node(&map
->rb_node
, parent
, p
);
896 rb_insert_color(&map
->rb_node
, &maps
->entries
);
900 static void __maps__insert_name(struct maps
*maps
, struct map
*map
)
902 struct rb_node
**p
= &maps
->names
.rb_node
;
903 struct rb_node
*parent
= NULL
;
909 m
= rb_entry(parent
, struct map
, rb_node_name
);
910 rc
= strcmp(m
->dso
->short_name
, map
->dso
->short_name
);
918 rb_link_node(&map
->rb_node_name
, parent
, p
);
919 rb_insert_color(&map
->rb_node_name
, &maps
->names
);
923 void maps__insert(struct maps
*maps
, struct map
*map
)
925 down_write(&maps
->lock
);
926 __maps__insert(maps
, map
);
927 __maps__insert_name(maps
, map
);
928 up_write(&maps
->lock
);
931 static void __maps__remove(struct maps
*maps
, struct map
*map
)
933 rb_erase_init(&map
->rb_node
, &maps
->entries
);
936 rb_erase_init(&map
->rb_node_name
, &maps
->names
);
940 void maps__remove(struct maps
*maps
, struct map
*map
)
942 down_write(&maps
->lock
);
943 __maps__remove(maps
, map
);
944 up_write(&maps
->lock
);
947 struct map
*maps__find(struct maps
*maps
, u64 ip
)
952 down_read(&maps
->lock
);
954 p
= maps
->entries
.rb_node
;
956 m
= rb_entry(p
, struct map
, rb_node
);
959 else if (ip
>= m
->end
)
967 up_read(&maps
->lock
);
971 struct map
*maps__first(struct maps
*maps
)
973 struct rb_node
*first
= rb_first(&maps
->entries
);
976 return rb_entry(first
, struct map
, rb_node
);
980 struct map
*map__next(struct map
*map
)
982 struct rb_node
*next
= rb_next(&map
->rb_node
);
985 return rb_entry(next
, struct map
, rb_node
);
989 struct kmap
*__map__kmap(struct map
*map
)
991 if (!map
->dso
|| !map
->dso
->kernel
)
993 return (struct kmap
*)(map
+ 1);
996 struct kmap
*map__kmap(struct map
*map
)
998 struct kmap
*kmap
= __map__kmap(map
);
1001 pr_err("Internal error: map__kmap with a non-kernel map\n");
1005 struct map_groups
*map__kmaps(struct map
*map
)
1007 struct kmap
*kmap
= map__kmap(map
);
1009 if (!kmap
|| !kmap
->kmaps
) {
1010 pr_err("Internal error: map__kmaps with a non-kernel map\n");