17 #include <linux/string.h>
20 static void __maps__insert(struct maps
*maps
, struct map
*map
);
22 const char *map_type__name
[MAP__NR_TYPES
] = {
23 [MAP__FUNCTION
] = "Functions",
24 [MAP__VARIABLE
] = "Variables",
27 static inline int is_anon_memory(const char *filename
)
29 return !strcmp(filename
, "//anon") ||
30 !strncmp(filename
, "/dev/zero", sizeof("/dev/zero") - 1) ||
31 !strncmp(filename
, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
34 static inline int is_no_dso_memory(const char *filename
)
36 return !strncmp(filename
, "[stack", 6) ||
37 !strncmp(filename
, "/SYSV",5) ||
38 !strcmp(filename
, "[heap]");
41 static inline int is_android_lib(const char *filename
)
43 return !strncmp(filename
, "/data/app-lib", 13) ||
44 !strncmp(filename
, "/system/lib", 11);
47 static inline bool replace_android_lib(const char *filename
, char *newfilename
)
51 size_t app_abi_length
, new_length
;
52 size_t lib_length
= 0;
54 libname
= strrchr(filename
, '/');
56 lib_length
= strlen(libname
);
58 app_abi
= getenv("APP_ABI");
62 app_abi_length
= strlen(app_abi
);
64 if (!strncmp(filename
, "/data/app-lib", 13)) {
70 new_length
= 7 + app_abi_length
+ lib_length
;
72 apk_path
= getenv("APK_PATH");
74 new_length
+= strlen(apk_path
) + 1;
75 if (new_length
> PATH_MAX
)
77 snprintf(newfilename
, new_length
,
78 "%s/libs/%s/%s", apk_path
, app_abi
, libname
);
80 if (new_length
> PATH_MAX
)
82 snprintf(newfilename
, new_length
,
83 "libs/%s/%s", app_abi
, libname
);
88 if (!strncmp(filename
, "/system/lib/", 11)) {
94 ndk
= getenv("NDK_ROOT");
95 app
= getenv("APP_PLATFORM");
100 ndk_length
= strlen(ndk
);
101 app_length
= strlen(app
);
103 if (!(ndk_length
&& app_length
&& app_abi_length
))
106 arch
= !strncmp(app_abi
, "arm", 3) ? "arm" :
107 !strncmp(app_abi
, "mips", 4) ? "mips" :
108 !strncmp(app_abi
, "x86", 3) ? "x86" : NULL
;
113 new_length
= 27 + ndk_length
+
114 app_length
+ lib_length
117 if (new_length
> PATH_MAX
)
119 snprintf(newfilename
, new_length
,
120 "%s/platforms/%s/arch-%s/usr/lib/%s",
121 ndk
, app
, arch
, libname
);
128 void map__init(struct map
*map
, enum map_type type
,
129 u64 start
, u64 end
, u64 pgoff
, struct dso
*dso
)
136 map
->dso
= dso__get(dso
);
137 map
->map_ip
= map__map_ip
;
138 map
->unmap_ip
= map__unmap_ip
;
139 RB_CLEAR_NODE(&map
->rb_node
);
141 map
->erange_warned
= false;
142 atomic_set(&map
->refcnt
, 1);
145 struct map
*map__new(struct machine
*machine
, u64 start
, u64 len
,
146 u64 pgoff
, u32 pid
, u32 d_maj
, u32 d_min
, u64 ino
,
147 u64 ino_gen
, u32 prot
, u32 flags
, char *filename
,
148 enum map_type type
, struct thread
*thread
)
150 struct map
*map
= malloc(sizeof(*map
));
153 char newfilename
[PATH_MAX
];
155 int anon
, no_dso
, vdso
, android
;
157 android
= is_android_lib(filename
);
158 anon
= is_anon_memory(filename
);
159 vdso
= is_vdso_map(filename
);
160 no_dso
= is_no_dso_memory(filename
);
165 map
->ino_generation
= ino_gen
;
169 if ((anon
|| no_dso
) && type
== MAP__FUNCTION
) {
170 snprintf(newfilename
, sizeof(newfilename
), "/tmp/perf-%d.map", pid
);
171 filename
= newfilename
;
175 if (replace_android_lib(filename
, newfilename
))
176 filename
= newfilename
;
181 dso
= machine__findnew_vdso(machine
, thread
);
183 dso
= machine__findnew_dso(machine
, filename
);
188 map__init(map
, type
, start
, start
+ len
, pgoff
, dso
);
190 if (anon
|| no_dso
) {
191 map
->map_ip
= map
->unmap_ip
= identity__map_ip
;
194 * Set memory without DSO as loaded. All map__find_*
195 * functions still return NULL, and we avoid the
196 * unnecessary map__load warning.
198 if (type
!= MAP__FUNCTION
)
199 dso__set_loaded(dso
, map
->type
);
210 * Constructor variant for modules (where we know from /proc/modules where
211 * they are loaded) and for vmlinux, where only after we load all the
212 * symbols we'll know where it starts and ends.
214 struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
)
216 struct map
*map
= calloc(1, (sizeof(*map
) +
217 (dso
->kernel
? sizeof(struct kmap
) : 0)));
220 * ->end will be filled after we load all the symbols
222 map__init(map
, type
, start
, 0, 0, dso
);
229 * Use this and __map__is_kmodule() for map instances that are in
230 * machine->kmaps, and thus have map->groups->machine all properly set, to
231 * disambiguate between the kernel and modules.
233 * When the need arises, introduce map__is_{kernel,kmodule)() that
234 * checks (map->groups != NULL && map->groups->machine != NULL &&
235 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
237 bool __map__is_kernel(const struct map
*map
)
239 return __machine__kernel_map(map
->groups
->machine
, map
->type
) == map
;
242 static void map__exit(struct map
*map
)
244 BUG_ON(!RB_EMPTY_NODE(&map
->rb_node
));
248 void map__delete(struct map
*map
)
254 void map__put(struct map
*map
)
256 if (map
&& atomic_dec_and_test(&map
->refcnt
))
260 void map__fixup_start(struct map
*map
)
262 struct rb_root
*symbols
= &map
->dso
->symbols
[map
->type
];
263 struct rb_node
*nd
= rb_first(symbols
);
265 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
266 map
->start
= sym
->start
;
270 void map__fixup_end(struct map
*map
)
272 struct rb_root
*symbols
= &map
->dso
->symbols
[map
->type
];
273 struct rb_node
*nd
= rb_last(symbols
);
275 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
280 #define DSO__DELETED "(deleted)"
282 int map__load(struct map
*map
, symbol_filter_t filter
)
284 const char *name
= map
->dso
->long_name
;
287 if (dso__loaded(map
->dso
, map
->type
))
290 nr
= dso__load(map
->dso
, map
, filter
);
292 if (map
->dso
->has_build_id
) {
293 char sbuild_id
[SBUILD_ID_SIZE
];
295 build_id__sprintf(map
->dso
->build_id
,
296 sizeof(map
->dso
->build_id
),
298 pr_warning("%s with build id %s not found",
301 pr_warning("Failed to open %s", name
);
303 pr_warning(", continuing without symbols\n");
305 } else if (nr
== 0) {
306 #ifdef HAVE_LIBELF_SUPPORT
307 const size_t len
= strlen(name
);
308 const size_t real_len
= len
- sizeof(DSO__DELETED
);
310 if (len
> sizeof(DSO__DELETED
) &&
311 strcmp(name
+ real_len
+ 1, DSO__DELETED
) == 0) {
312 pr_warning("%.*s was updated (is prelink enabled?). "
313 "Restart the long running apps that use it!\n",
314 (int)real_len
, name
);
316 pr_warning("no symbols passed the given filter.\n");
317 return -2; /* Empty but maybe by the filter */
319 pr_warning("no symbols found in %s, maybe install "
320 "a debug package?\n", name
);
329 int __weak
arch__compare_symbol_names(const char *namea
, const char *nameb
)
331 return strcmp(namea
, nameb
);
334 struct symbol
*map__find_symbol(struct map
*map
, u64 addr
,
335 symbol_filter_t filter
)
337 if (map__load(map
, filter
) < 0)
340 return dso__find_symbol(map
->dso
, map
->type
, addr
);
343 struct symbol
*map__find_symbol_by_name(struct map
*map
, const char *name
,
344 symbol_filter_t filter
)
346 if (map__load(map
, filter
) < 0)
349 if (!dso__sorted_by_name(map
->dso
, map
->type
))
350 dso__sort_by_name(map
->dso
, map
->type
);
352 return dso__find_symbol_by_name(map
->dso
, map
->type
, name
);
355 struct map
*map__clone(struct map
*from
)
357 struct map
*map
= memdup(from
, sizeof(*map
));
360 atomic_set(&map
->refcnt
, 1);
361 RB_CLEAR_NODE(&map
->rb_node
);
369 int map__overlap(struct map
*l
, struct map
*r
)
371 if (l
->start
> r
->start
) {
377 if (l
->end
> r
->start
)
383 size_t map__fprintf(struct map
*map
, FILE *fp
)
385 return fprintf(fp
, " %" PRIx64
"-%" PRIx64
" %" PRIx64
" %s\n",
386 map
->start
, map
->end
, map
->pgoff
, map
->dso
->name
);
389 size_t map__fprintf_dsoname(struct map
*map
, FILE *fp
)
391 const char *dsoname
= "[unknown]";
393 if (map
&& map
->dso
&& (map
->dso
->name
|| map
->dso
->long_name
)) {
394 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
395 dsoname
= map
->dso
->long_name
;
396 else if (map
->dso
->name
)
397 dsoname
= map
->dso
->name
;
400 return fprintf(fp
, "%s", dsoname
);
403 int map__fprintf_srcline(struct map
*map
, u64 addr
, const char *prefix
,
409 if (map
&& map
->dso
) {
410 srcline
= get_srcline(map
->dso
,
411 map__rip_2objdump(map
, addr
), NULL
, true);
412 if (srcline
!= SRCLINE_UNKNOWN
)
413 ret
= fprintf(fp
, "%s%s", prefix
, srcline
);
414 free_srcline(srcline
);
420 * map__rip_2objdump - convert symbol start address to objdump address.
422 * @rip: symbol start address
424 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
425 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
426 * relative to section start.
428 * Return: Address suitable for passing to "objdump --start-address="
430 u64
map__rip_2objdump(struct map
*map
, u64 rip
)
432 if (!map
->dso
->adjust_symbols
)
436 return rip
- map
->pgoff
;
439 * kernel modules also have DSO_TYPE_USER in dso->kernel,
440 * but all kernel modules are ET_REL, so won't get here.
442 if (map
->dso
->kernel
== DSO_TYPE_USER
)
443 return rip
+ map
->dso
->text_offset
;
445 return map
->unmap_ip(map
, rip
) - map
->reloc
;
449 * map__objdump_2mem - convert objdump address to a memory address.
451 * @ip: objdump address
453 * Closely related to map__rip_2objdump(), this function takes an address from
454 * objdump and converts it to a memory address. Note this assumes that @map
455 * contains the address. To be sure the result is valid, check it forwards
456 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
458 * Return: Memory address.
460 u64
map__objdump_2mem(struct map
*map
, u64 ip
)
462 if (!map
->dso
->adjust_symbols
)
463 return map
->unmap_ip(map
, ip
);
466 return map
->unmap_ip(map
, ip
+ map
->pgoff
);
469 * kernel modules also have DSO_TYPE_USER in dso->kernel,
470 * but all kernel modules are ET_REL, so won't get here.
472 if (map
->dso
->kernel
== DSO_TYPE_USER
)
473 return map
->unmap_ip(map
, ip
- map
->dso
->text_offset
);
475 return ip
+ map
->reloc
;
478 static void maps__init(struct maps
*maps
)
480 maps
->entries
= RB_ROOT
;
481 pthread_rwlock_init(&maps
->lock
, NULL
);
484 void map_groups__init(struct map_groups
*mg
, struct machine
*machine
)
487 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
488 maps__init(&mg
->maps
[i
]);
490 mg
->machine
= machine
;
491 atomic_set(&mg
->refcnt
, 1);
494 static void __maps__purge(struct maps
*maps
)
496 struct rb_root
*root
= &maps
->entries
;
497 struct rb_node
*next
= rb_first(root
);
500 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
502 next
= rb_next(&pos
->rb_node
);
503 rb_erase_init(&pos
->rb_node
, root
);
508 static void maps__exit(struct maps
*maps
)
510 pthread_rwlock_wrlock(&maps
->lock
);
512 pthread_rwlock_unlock(&maps
->lock
);
515 void map_groups__exit(struct map_groups
*mg
)
519 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
520 maps__exit(&mg
->maps
[i
]);
523 bool map_groups__empty(struct map_groups
*mg
)
527 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
528 if (maps__first(&mg
->maps
[i
]))
535 struct map_groups
*map_groups__new(struct machine
*machine
)
537 struct map_groups
*mg
= malloc(sizeof(*mg
));
540 map_groups__init(mg
, machine
);
545 void map_groups__delete(struct map_groups
*mg
)
547 map_groups__exit(mg
);
551 void map_groups__put(struct map_groups
*mg
)
553 if (mg
&& atomic_dec_and_test(&mg
->refcnt
))
554 map_groups__delete(mg
);
557 struct symbol
*map_groups__find_symbol(struct map_groups
*mg
,
558 enum map_type type
, u64 addr
,
560 symbol_filter_t filter
)
562 struct map
*map
= map_groups__find(mg
, type
, addr
);
564 /* Ensure map is loaded before using map->map_ip */
565 if (map
!= NULL
&& map__load(map
, filter
) >= 0) {
568 return map__find_symbol(map
, map
->map_ip(map
, addr
), filter
);
574 struct symbol
*maps__find_symbol_by_name(struct maps
*maps
, const char *name
,
575 struct map
**mapp
, symbol_filter_t filter
)
580 pthread_rwlock_rdlock(&maps
->lock
);
582 for (nd
= rb_first(&maps
->entries
); nd
; nd
= rb_next(nd
)) {
583 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
585 sym
= map__find_symbol_by_name(pos
, name
, filter
);
596 pthread_rwlock_unlock(&maps
->lock
);
600 struct symbol
*map_groups__find_symbol_by_name(struct map_groups
*mg
,
604 symbol_filter_t filter
)
606 struct symbol
*sym
= maps__find_symbol_by_name(&mg
->maps
[type
], name
, mapp
, filter
);
611 int map_groups__find_ams(struct addr_map_symbol
*ams
, symbol_filter_t filter
)
613 if (ams
->addr
< ams
->map
->start
|| ams
->addr
>= ams
->map
->end
) {
614 if (ams
->map
->groups
== NULL
)
616 ams
->map
= map_groups__find(ams
->map
->groups
, ams
->map
->type
,
618 if (ams
->map
== NULL
)
622 ams
->al_addr
= ams
->map
->map_ip(ams
->map
, ams
->addr
);
623 ams
->sym
= map__find_symbol(ams
->map
, ams
->al_addr
, filter
);
625 return ams
->sym
? 0 : -1;
628 static size_t maps__fprintf(struct maps
*maps
, FILE *fp
)
633 pthread_rwlock_rdlock(&maps
->lock
);
635 for (nd
= rb_first(&maps
->entries
); nd
; nd
= rb_next(nd
)) {
636 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
637 printed
+= fprintf(fp
, "Map:");
638 printed
+= map__fprintf(pos
, fp
);
640 printed
+= dso__fprintf(pos
->dso
, pos
->type
, fp
);
641 printed
+= fprintf(fp
, "--\n");
645 pthread_rwlock_unlock(&maps
->lock
);
650 size_t __map_groups__fprintf_maps(struct map_groups
*mg
, enum map_type type
,
653 size_t printed
= fprintf(fp
, "%s:\n", map_type__name
[type
]);
654 return printed
+= maps__fprintf(&mg
->maps
[type
], fp
);
657 size_t map_groups__fprintf(struct map_groups
*mg
, FILE *fp
)
659 size_t printed
= 0, i
;
660 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
661 printed
+= __map_groups__fprintf_maps(mg
, i
, fp
);
665 static void __map_groups__insert(struct map_groups
*mg
, struct map
*map
)
667 __maps__insert(&mg
->maps
[map
->type
], map
);
671 static int maps__fixup_overlappings(struct maps
*maps
, struct map
*map
, FILE *fp
)
673 struct rb_root
*root
;
674 struct rb_node
*next
;
677 pthread_rwlock_wrlock(&maps
->lock
);
679 root
= &maps
->entries
;
680 next
= rb_first(root
);
683 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
684 next
= rb_next(&pos
->rb_node
);
686 if (!map__overlap(pos
, map
))
690 fputs("overlapping maps:\n", fp
);
691 map__fprintf(map
, fp
);
692 map__fprintf(pos
, fp
);
695 rb_erase_init(&pos
->rb_node
, root
);
697 * Now check if we need to create new maps for areas not
698 * overlapped by the new map:
700 if (map
->start
> pos
->start
) {
701 struct map
*before
= map__clone(pos
);
703 if (before
== NULL
) {
708 before
->end
= map
->start
;
709 __map_groups__insert(pos
->groups
, before
);
711 map__fprintf(before
, fp
);
715 if (map
->end
< pos
->end
) {
716 struct map
*after
= map__clone(pos
);
723 after
->start
= map
->end
;
724 __map_groups__insert(pos
->groups
, after
);
726 map__fprintf(after
, fp
);
738 pthread_rwlock_unlock(&maps
->lock
);
742 int map_groups__fixup_overlappings(struct map_groups
*mg
, struct map
*map
,
745 return maps__fixup_overlappings(&mg
->maps
[map
->type
], map
, fp
);
749 * XXX This should not really _copy_ te maps, but refcount them.
751 int map_groups__clone(struct thread
*thread
,
752 struct map_groups
*parent
, enum map_type type
)
754 struct map_groups
*mg
= thread
->mg
;
757 struct maps
*maps
= &parent
->maps
[type
];
759 pthread_rwlock_rdlock(&maps
->lock
);
761 for (map
= maps__first(maps
); map
; map
= map__next(map
)) {
762 struct map
*new = map__clone(map
);
766 err
= unwind__prepare_access(thread
, new, NULL
);
770 map_groups__insert(mg
, new);
776 pthread_rwlock_unlock(&maps
->lock
);
780 static void __maps__insert(struct maps
*maps
, struct map
*map
)
782 struct rb_node
**p
= &maps
->entries
.rb_node
;
783 struct rb_node
*parent
= NULL
;
784 const u64 ip
= map
->start
;
789 m
= rb_entry(parent
, struct map
, rb_node
);
796 rb_link_node(&map
->rb_node
, parent
, p
);
797 rb_insert_color(&map
->rb_node
, &maps
->entries
);
801 void maps__insert(struct maps
*maps
, struct map
*map
)
803 pthread_rwlock_wrlock(&maps
->lock
);
804 __maps__insert(maps
, map
);
805 pthread_rwlock_unlock(&maps
->lock
);
808 static void __maps__remove(struct maps
*maps
, struct map
*map
)
810 rb_erase_init(&map
->rb_node
, &maps
->entries
);
814 void maps__remove(struct maps
*maps
, struct map
*map
)
816 pthread_rwlock_wrlock(&maps
->lock
);
817 __maps__remove(maps
, map
);
818 pthread_rwlock_unlock(&maps
->lock
);
821 struct map
*maps__find(struct maps
*maps
, u64 ip
)
823 struct rb_node
**p
, *parent
= NULL
;
826 pthread_rwlock_rdlock(&maps
->lock
);
828 p
= &maps
->entries
.rb_node
;
831 m
= rb_entry(parent
, struct map
, rb_node
);
834 else if (ip
>= m
->end
)
842 pthread_rwlock_unlock(&maps
->lock
);
846 struct map
*maps__first(struct maps
*maps
)
848 struct rb_node
*first
= rb_first(&maps
->entries
);
851 return rb_entry(first
, struct map
, rb_node
);
855 struct map
*map__next(struct map
*map
)
857 struct rb_node
*next
= rb_next(&map
->rb_node
);
860 return rb_entry(next
, struct map
, rb_node
);
864 struct kmap
*map__kmap(struct map
*map
)
866 if (!map
->dso
|| !map
->dso
->kernel
) {
867 pr_err("Internal error: map__kmap with a non-kernel map\n");
870 return (struct kmap
*)(map
+ 1);
873 struct map_groups
*map__kmaps(struct map
*map
)
875 struct kmap
*kmap
= map__kmap(map
);
877 if (!kmap
|| !kmap
->kmaps
) {
878 pr_err("Internal error: map__kmaps with a non-kernel map\n");
This page took 0.079777 seconds and 5 git commands to generate.