15 #include <linux/string.h>
17 const char *map_type__name
[MAP__NR_TYPES
] = {
18 [MAP__FUNCTION
] = "Functions",
19 [MAP__VARIABLE
] = "Variables",
22 static inline int is_anon_memory(const char *filename
)
24 return !strcmp(filename
, "//anon") ||
25 !strcmp(filename
, "/dev/zero (deleted)") ||
26 !strcmp(filename
, "/anon_hugepage (deleted)");
29 static inline int is_no_dso_memory(const char *filename
)
31 return !strncmp(filename
, "[stack", 6) ||
32 !strcmp(filename
, "[heap]");
35 void map__init(struct map
*map
, enum map_type type
,
36 u64 start
, u64 end
, u64 pgoff
, struct dso
*dso
)
43 map
->map_ip
= map__map_ip
;
44 map
->unmap_ip
= map__unmap_ip
;
45 RB_CLEAR_NODE(&map
->rb_node
);
47 map
->referenced
= false;
48 map
->erange_warned
= false;
51 struct map
*map__new(struct list_head
*dsos__list
, u64 start
, u64 len
,
52 u64 pgoff
, u32 pid
, u32 d_maj
, u32 d_min
, u64 ino
,
53 u64 ino_gen
, char *filename
,
56 struct map
*map
= malloc(sizeof(*map
));
59 char newfilename
[PATH_MAX
];
61 int anon
, no_dso
, vdso
;
63 anon
= is_anon_memory(filename
);
64 vdso
= is_vdso_map(filename
);
65 no_dso
= is_no_dso_memory(filename
);
70 map
->ino_generation
= ino_gen
;
72 if ((anon
|| no_dso
) && type
== MAP__FUNCTION
) {
73 snprintf(newfilename
, sizeof(newfilename
), "/tmp/perf-%d.map", pid
);
74 filename
= newfilename
;
79 dso
= vdso__dso_findnew(dsos__list
);
81 dso
= __dsos__findnew(dsos__list
, filename
);
86 map__init(map
, type
, start
, start
+ len
, pgoff
, dso
);
89 map
->map_ip
= map
->unmap_ip
= identity__map_ip
;
92 * Set memory without DSO as loaded. All map__find_*
93 * functions still return NULL, and we avoid the
94 * unnecessary map__load warning.
96 if (type
!= MAP__FUNCTION
)
97 dso__set_loaded(dso
, map
->type
);
107 * Constructor variant for modules (where we know from /proc/modules where
108 * they are loaded) and for vmlinux, where only after we load all the
109 * symbols we'll know where it starts and ends.
111 struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
)
113 struct map
*map
= calloc(1, (sizeof(*map
) +
114 (dso
->kernel
? sizeof(struct kmap
) : 0)));
117 * ->end will be filled after we load all the symbols
119 map__init(map
, type
, start
, 0, 0, dso
);
125 void map__delete(struct map
*map
)
130 void map__fixup_start(struct map
*map
)
132 struct rb_root
*symbols
= &map
->dso
->symbols
[map
->type
];
133 struct rb_node
*nd
= rb_first(symbols
);
135 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
136 map
->start
= sym
->start
;
140 void map__fixup_end(struct map
*map
)
142 struct rb_root
*symbols
= &map
->dso
->symbols
[map
->type
];
143 struct rb_node
*nd
= rb_last(symbols
);
145 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
150 #define DSO__DELETED "(deleted)"
152 int map__load(struct map
*map
, symbol_filter_t filter
)
154 const char *name
= map
->dso
->long_name
;
157 if (dso__loaded(map
->dso
, map
->type
))
160 nr
= dso__load(map
->dso
, map
, filter
);
162 if (map
->dso
->has_build_id
) {
163 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
165 build_id__sprintf(map
->dso
->build_id
,
166 sizeof(map
->dso
->build_id
),
168 pr_warning("%s with build id %s not found",
171 pr_warning("Failed to open %s", name
);
173 pr_warning(", continuing without symbols\n");
175 } else if (nr
== 0) {
176 #ifdef HAVE_LIBELF_SUPPORT
177 const size_t len
= strlen(name
);
178 const size_t real_len
= len
- sizeof(DSO__DELETED
);
180 if (len
> sizeof(DSO__DELETED
) &&
181 strcmp(name
+ real_len
+ 1, DSO__DELETED
) == 0) {
182 pr_warning("%.*s was updated (is prelink enabled?). "
183 "Restart the long running apps that use it!\n",
184 (int)real_len
, name
);
186 pr_warning("no symbols found in %s, maybe install "
187 "a debug package?\n", name
);
196 struct symbol
*map__find_symbol(struct map
*map
, u64 addr
,
197 symbol_filter_t filter
)
199 if (map__load(map
, filter
) < 0)
202 return dso__find_symbol(map
->dso
, map
->type
, addr
);
205 struct symbol
*map__find_symbol_by_name(struct map
*map
, const char *name
,
206 symbol_filter_t filter
)
208 if (map__load(map
, filter
) < 0)
211 if (!dso__sorted_by_name(map
->dso
, map
->type
))
212 dso__sort_by_name(map
->dso
, map
->type
);
214 return dso__find_symbol_by_name(map
->dso
, map
->type
, name
);
217 struct map
*map__clone(struct map
*map
)
219 return memdup(map
, sizeof(*map
));
222 int map__overlap(struct map
*l
, struct map
*r
)
224 if (l
->start
> r
->start
) {
230 if (l
->end
> r
->start
)
236 size_t map__fprintf(struct map
*map
, FILE *fp
)
238 return fprintf(fp
, " %" PRIx64
"-%" PRIx64
" %" PRIx64
" %s\n",
239 map
->start
, map
->end
, map
->pgoff
, map
->dso
->name
);
242 size_t map__fprintf_dsoname(struct map
*map
, FILE *fp
)
244 const char *dsoname
= "[unknown]";
246 if (map
&& map
->dso
&& (map
->dso
->name
|| map
->dso
->long_name
)) {
247 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
248 dsoname
= map
->dso
->long_name
;
249 else if (map
->dso
->name
)
250 dsoname
= map
->dso
->name
;
253 return fprintf(fp
, "%s", dsoname
);
256 int map__fprintf_srcline(struct map
*map
, u64 addr
, const char *prefix
,
262 if (map
&& map
->dso
) {
263 srcline
= get_srcline(map
->dso
,
264 map__rip_2objdump(map
, addr
));
265 if (srcline
!= SRCLINE_UNKNOWN
)
266 ret
= fprintf(fp
, "%s%s", prefix
, srcline
);
267 free_srcline(srcline
);
273 * map__rip_2objdump - convert symbol start address to objdump address.
275 * @rip: symbol start address
277 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
278 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
279 * relative to section start.
281 * Return: Address suitable for passing to "objdump --start-address="
283 u64
map__rip_2objdump(struct map
*map
, u64 rip
)
285 if (!map
->dso
->adjust_symbols
)
289 return rip
- map
->pgoff
;
291 return map
->unmap_ip(map
, rip
);
295 * map__objdump_2mem - convert objdump address to a memory address.
297 * @ip: objdump address
299 * Closely related to map__rip_2objdump(), this function takes an address from
300 * objdump and converts it to a memory address. Note this assumes that @map
301 * contains the address. To be sure the result is valid, check it forwards
302 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
304 * Return: Memory address.
306 u64
map__objdump_2mem(struct map
*map
, u64 ip
)
308 if (!map
->dso
->adjust_symbols
)
309 return map
->unmap_ip(map
, ip
);
312 return map
->unmap_ip(map
, ip
+ map
->pgoff
);
317 void map_groups__init(struct map_groups
*mg
)
320 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
321 mg
->maps
[i
] = RB_ROOT
;
322 INIT_LIST_HEAD(&mg
->removed_maps
[i
]);
327 static void maps__delete(struct rb_root
*maps
)
329 struct rb_node
*next
= rb_first(maps
);
332 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
334 next
= rb_next(&pos
->rb_node
);
335 rb_erase(&pos
->rb_node
, maps
);
340 static void maps__delete_removed(struct list_head
*maps
)
344 list_for_each_entry_safe(pos
, n
, maps
, node
) {
345 list_del(&pos
->node
);
350 void map_groups__exit(struct map_groups
*mg
)
354 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
355 maps__delete(&mg
->maps
[i
]);
356 maps__delete_removed(&mg
->removed_maps
[i
]);
360 void map_groups__flush(struct map_groups
*mg
)
364 for (type
= 0; type
< MAP__NR_TYPES
; type
++) {
365 struct rb_root
*root
= &mg
->maps
[type
];
366 struct rb_node
*next
= rb_first(root
);
369 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
370 next
= rb_next(&pos
->rb_node
);
371 rb_erase(&pos
->rb_node
, root
);
373 * We may have references to this map, for
374 * instance in some hist_entry instances, so
375 * just move them to a separate list.
377 list_add_tail(&pos
->node
, &mg
->removed_maps
[pos
->type
]);
382 struct symbol
*map_groups__find_symbol(struct map_groups
*mg
,
383 enum map_type type
, u64 addr
,
385 symbol_filter_t filter
)
387 struct map
*map
= map_groups__find(mg
, type
, addr
);
389 /* Ensure map is loaded before using map->map_ip */
390 if (map
!= NULL
&& map__load(map
, filter
) >= 0) {
393 return map__find_symbol(map
, map
->map_ip(map
, addr
), filter
);
399 struct symbol
*map_groups__find_symbol_by_name(struct map_groups
*mg
,
403 symbol_filter_t filter
)
407 for (nd
= rb_first(&mg
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
408 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
409 struct symbol
*sym
= map__find_symbol_by_name(pos
, name
, filter
);
421 int map_groups__find_ams(struct addr_map_symbol
*ams
, symbol_filter_t filter
)
423 if (ams
->addr
< ams
->map
->start
|| ams
->addr
> ams
->map
->end
) {
424 if (ams
->map
->groups
== NULL
)
426 ams
->map
= map_groups__find(ams
->map
->groups
, ams
->map
->type
,
428 if (ams
->map
== NULL
)
432 ams
->al_addr
= ams
->map
->map_ip(ams
->map
, ams
->addr
);
433 ams
->sym
= map__find_symbol(ams
->map
, ams
->al_addr
, filter
);
435 return ams
->sym
? 0 : -1;
438 size_t __map_groups__fprintf_maps(struct map_groups
*mg
,
439 enum map_type type
, int verbose
, FILE *fp
)
441 size_t printed
= fprintf(fp
, "%s:\n", map_type__name
[type
]);
444 for (nd
= rb_first(&mg
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
445 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
446 printed
+= fprintf(fp
, "Map:");
447 printed
+= map__fprintf(pos
, fp
);
449 printed
+= dso__fprintf(pos
->dso
, type
, fp
);
450 printed
+= fprintf(fp
, "--\n");
457 size_t map_groups__fprintf_maps(struct map_groups
*mg
, int verbose
, FILE *fp
)
459 size_t printed
= 0, i
;
460 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
461 printed
+= __map_groups__fprintf_maps(mg
, i
, verbose
, fp
);
465 static size_t __map_groups__fprintf_removed_maps(struct map_groups
*mg
,
467 int verbose
, FILE *fp
)
472 list_for_each_entry(pos
, &mg
->removed_maps
[type
], node
) {
473 printed
+= fprintf(fp
, "Map:");
474 printed
+= map__fprintf(pos
, fp
);
476 printed
+= dso__fprintf(pos
->dso
, type
, fp
);
477 printed
+= fprintf(fp
, "--\n");
483 static size_t map_groups__fprintf_removed_maps(struct map_groups
*mg
,
484 int verbose
, FILE *fp
)
486 size_t printed
= 0, i
;
487 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
488 printed
+= __map_groups__fprintf_removed_maps(mg
, i
, verbose
, fp
);
492 size_t map_groups__fprintf(struct map_groups
*mg
, int verbose
, FILE *fp
)
494 size_t printed
= map_groups__fprintf_maps(mg
, verbose
, fp
);
495 printed
+= fprintf(fp
, "Removed maps:\n");
496 return printed
+ map_groups__fprintf_removed_maps(mg
, verbose
, fp
);
499 int map_groups__fixup_overlappings(struct map_groups
*mg
, struct map
*map
,
500 int verbose
, FILE *fp
)
502 struct rb_root
*root
= &mg
->maps
[map
->type
];
503 struct rb_node
*next
= rb_first(root
);
507 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
508 next
= rb_next(&pos
->rb_node
);
510 if (!map__overlap(pos
, map
))
514 fputs("overlapping maps:\n", fp
);
515 map__fprintf(map
, fp
);
516 map__fprintf(pos
, fp
);
519 rb_erase(&pos
->rb_node
, root
);
521 * Now check if we need to create new maps for areas not
522 * overlapped by the new map:
524 if (map
->start
> pos
->start
) {
525 struct map
*before
= map__clone(pos
);
527 if (before
== NULL
) {
532 before
->end
= map
->start
- 1;
533 map_groups__insert(mg
, before
);
535 map__fprintf(before
, fp
);
538 if (map
->end
< pos
->end
) {
539 struct map
*after
= map__clone(pos
);
546 after
->start
= map
->end
+ 1;
547 map_groups__insert(mg
, after
);
549 map__fprintf(after
, fp
);
553 * If we have references, just move them to a separate list.
556 list_add_tail(&pos
->node
, &mg
->removed_maps
[map
->type
]);
568 * XXX This should not really _copy_ te maps, but refcount them.
570 int map_groups__clone(struct map_groups
*mg
,
571 struct map_groups
*parent
, enum map_type type
)
574 for (nd
= rb_first(&parent
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
575 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
576 struct map
*new = map__clone(map
);
579 map_groups__insert(mg
, new);
584 void maps__insert(struct rb_root
*maps
, struct map
*map
)
586 struct rb_node
**p
= &maps
->rb_node
;
587 struct rb_node
*parent
= NULL
;
588 const u64 ip
= map
->start
;
593 m
= rb_entry(parent
, struct map
, rb_node
);
600 rb_link_node(&map
->rb_node
, parent
, p
);
601 rb_insert_color(&map
->rb_node
, maps
);
604 void maps__remove(struct rb_root
*maps
, struct map
*map
)
606 rb_erase(&map
->rb_node
, maps
);
609 struct map
*maps__find(struct rb_root
*maps
, u64 ip
)
611 struct rb_node
**p
= &maps
->rb_node
;
612 struct rb_node
*parent
= NULL
;
617 m
= rb_entry(parent
, struct map
, rb_node
);
620 else if (ip
> m
->end
)
629 struct map
*maps__first(struct rb_root
*maps
)
631 struct rb_node
*first
= rb_first(maps
);
634 return rb_entry(first
, struct map
, rb_node
);
638 struct map
*maps__next(struct map
*map
)
640 struct rb_node
*next
= rb_next(&map
->rb_node
);
643 return rb_entry(next
, struct map
, rb_node
);