14 #include <linux/string.h>
16 const char *map_type__name
[MAP__NR_TYPES
] = {
17 [MAP__FUNCTION
] = "Functions",
18 [MAP__VARIABLE
] = "Variables",
21 static inline int is_anon_memory(const char *filename
)
23 return !strcmp(filename
, "//anon") ||
24 !strcmp(filename
, "/anon_hugepage (deleted)");
27 static inline int is_no_dso_memory(const char *filename
)
29 return !strncmp(filename
, "[stack", 6) ||
30 !strcmp(filename
, "[heap]");
33 void map__init(struct map
*map
, enum map_type type
,
34 u64 start
, u64 end
, u64 pgoff
, struct dso
*dso
)
41 map
->map_ip
= map__map_ip
;
42 map
->unmap_ip
= map__unmap_ip
;
43 RB_CLEAR_NODE(&map
->rb_node
);
45 map
->referenced
= false;
46 map
->erange_warned
= false;
49 struct map
*map__new(struct list_head
*dsos__list
, u64 start
, u64 len
,
50 u64 pgoff
, u32 pid
, char *filename
,
53 struct map
*map
= malloc(sizeof(*map
));
56 char newfilename
[PATH_MAX
];
58 int anon
, no_dso
, vdso
;
60 anon
= is_anon_memory(filename
);
61 vdso
= is_vdso_map(filename
);
62 no_dso
= is_no_dso_memory(filename
);
65 snprintf(newfilename
, sizeof(newfilename
), "/tmp/perf-%d.map", pid
);
66 filename
= newfilename
;
71 dso
= vdso__dso_findnew(dsos__list
);
73 dso
= __dsos__findnew(dsos__list
, filename
);
78 map__init(map
, type
, start
, start
+ len
, pgoff
, dso
);
81 map
->map_ip
= map
->unmap_ip
= identity__map_ip
;
84 * Set memory without DSO as loaded. All map__find_*
85 * functions still return NULL, and we avoid the
86 * unnecessary map__load warning.
89 dso__set_loaded(dso
, map
->type
);
99 * Constructor variant for modules (where we know from /proc/modules where
100 * they are loaded) and for vmlinux, where only after we load all the
101 * symbols we'll know where it starts and ends.
103 struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
)
105 struct map
*map
= calloc(1, (sizeof(*map
) +
106 (dso
->kernel
? sizeof(struct kmap
) : 0)));
109 * ->end will be filled after we load all the symbols
111 map__init(map
, type
, start
, 0, 0, dso
);
117 void map__delete(struct map
*map
)
122 void map__fixup_start(struct map
*map
)
124 struct rb_root
*symbols
= &map
->dso
->symbols
[map
->type
];
125 struct rb_node
*nd
= rb_first(symbols
);
127 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
128 map
->start
= sym
->start
;
132 void map__fixup_end(struct map
*map
)
134 struct rb_root
*symbols
= &map
->dso
->symbols
[map
->type
];
135 struct rb_node
*nd
= rb_last(symbols
);
137 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
142 #define DSO__DELETED "(deleted)"
144 int map__load(struct map
*map
, symbol_filter_t filter
)
146 const char *name
= map
->dso
->long_name
;
149 if (dso__loaded(map
->dso
, map
->type
))
152 nr
= dso__load(map
->dso
, map
, filter
);
154 if (map
->dso
->has_build_id
) {
155 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
157 build_id__sprintf(map
->dso
->build_id
,
158 sizeof(map
->dso
->build_id
),
160 pr_warning("%s with build id %s not found",
163 pr_warning("Failed to open %s", name
);
165 pr_warning(", continuing without symbols\n");
167 } else if (nr
== 0) {
168 #ifdef LIBELF_SUPPORT
169 const size_t len
= strlen(name
);
170 const size_t real_len
= len
- sizeof(DSO__DELETED
);
172 if (len
> sizeof(DSO__DELETED
) &&
173 strcmp(name
+ real_len
+ 1, DSO__DELETED
) == 0) {
174 pr_warning("%.*s was updated (is prelink enabled?). "
175 "Restart the long running apps that use it!\n",
176 (int)real_len
, name
);
178 pr_warning("no symbols found in %s, maybe install "
179 "a debug package?\n", name
);
185 * Only applies to the kernel, as its symtabs aren't relative like the
188 if (map
->dso
->kernel
)
189 map__reloc_vmlinux(map
);
194 struct symbol
*map__find_symbol(struct map
*map
, u64 addr
,
195 symbol_filter_t filter
)
197 if (map__load(map
, filter
) < 0)
200 return dso__find_symbol(map
->dso
, map
->type
, addr
);
203 struct symbol
*map__find_symbol_by_name(struct map
*map
, const char *name
,
204 symbol_filter_t filter
)
206 if (map__load(map
, filter
) < 0)
209 if (!dso__sorted_by_name(map
->dso
, map
->type
))
210 dso__sort_by_name(map
->dso
, map
->type
);
212 return dso__find_symbol_by_name(map
->dso
, map
->type
, name
);
215 struct map
*map__clone(struct map
*map
)
217 return memdup(map
, sizeof(*map
));
220 int map__overlap(struct map
*l
, struct map
*r
)
222 if (l
->start
> r
->start
) {
228 if (l
->end
> r
->start
)
234 size_t map__fprintf(struct map
*map
, FILE *fp
)
236 return fprintf(fp
, " %" PRIx64
"-%" PRIx64
" %" PRIx64
" %s\n",
237 map
->start
, map
->end
, map
->pgoff
, map
->dso
->name
);
240 size_t map__fprintf_dsoname(struct map
*map
, FILE *fp
)
242 const char *dsoname
= "[unknown]";
244 if (map
&& map
->dso
&& (map
->dso
->name
|| map
->dso
->long_name
)) {
245 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
246 dsoname
= map
->dso
->long_name
;
247 else if (map
->dso
->name
)
248 dsoname
= map
->dso
->name
;
251 return fprintf(fp
, "%s", dsoname
);
255 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
256 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
258 u64
map__rip_2objdump(struct map
*map
, u64 rip
)
260 u64 addr
= map
->dso
->adjust_symbols
?
261 map
->unmap_ip(map
, rip
) : /* RIP -> IP */
266 void map_groups__init(struct map_groups
*mg
)
269 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
270 mg
->maps
[i
] = RB_ROOT
;
271 INIT_LIST_HEAD(&mg
->removed_maps
[i
]);
276 static void maps__delete(struct rb_root
*maps
)
278 struct rb_node
*next
= rb_first(maps
);
281 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
283 next
= rb_next(&pos
->rb_node
);
284 rb_erase(&pos
->rb_node
, maps
);
289 static void maps__delete_removed(struct list_head
*maps
)
293 list_for_each_entry_safe(pos
, n
, maps
, node
) {
294 list_del(&pos
->node
);
299 void map_groups__exit(struct map_groups
*mg
)
303 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
304 maps__delete(&mg
->maps
[i
]);
305 maps__delete_removed(&mg
->removed_maps
[i
]);
309 void map_groups__flush(struct map_groups
*mg
)
313 for (type
= 0; type
< MAP__NR_TYPES
; type
++) {
314 struct rb_root
*root
= &mg
->maps
[type
];
315 struct rb_node
*next
= rb_first(root
);
318 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
319 next
= rb_next(&pos
->rb_node
);
320 rb_erase(&pos
->rb_node
, root
);
322 * We may have references to this map, for
323 * instance in some hist_entry instances, so
324 * just move them to a separate list.
326 list_add_tail(&pos
->node
, &mg
->removed_maps
[pos
->type
]);
331 struct symbol
*map_groups__find_symbol(struct map_groups
*mg
,
332 enum map_type type
, u64 addr
,
334 symbol_filter_t filter
)
336 struct map
*map
= map_groups__find(mg
, type
, addr
);
341 return map__find_symbol(map
, map
->map_ip(map
, addr
), filter
);
347 struct symbol
*map_groups__find_symbol_by_name(struct map_groups
*mg
,
351 symbol_filter_t filter
)
355 for (nd
= rb_first(&mg
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
356 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
357 struct symbol
*sym
= map__find_symbol_by_name(pos
, name
, filter
);
369 size_t __map_groups__fprintf_maps(struct map_groups
*mg
,
370 enum map_type type
, int verbose
, FILE *fp
)
372 size_t printed
= fprintf(fp
, "%s:\n", map_type__name
[type
]);
375 for (nd
= rb_first(&mg
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
376 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
377 printed
+= fprintf(fp
, "Map:");
378 printed
+= map__fprintf(pos
, fp
);
380 printed
+= dso__fprintf(pos
->dso
, type
, fp
);
381 printed
+= fprintf(fp
, "--\n");
388 size_t map_groups__fprintf_maps(struct map_groups
*mg
, int verbose
, FILE *fp
)
390 size_t printed
= 0, i
;
391 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
392 printed
+= __map_groups__fprintf_maps(mg
, i
, verbose
, fp
);
396 static size_t __map_groups__fprintf_removed_maps(struct map_groups
*mg
,
398 int verbose
, FILE *fp
)
403 list_for_each_entry(pos
, &mg
->removed_maps
[type
], node
) {
404 printed
+= fprintf(fp
, "Map:");
405 printed
+= map__fprintf(pos
, fp
);
407 printed
+= dso__fprintf(pos
->dso
, type
, fp
);
408 printed
+= fprintf(fp
, "--\n");
414 static size_t map_groups__fprintf_removed_maps(struct map_groups
*mg
,
415 int verbose
, FILE *fp
)
417 size_t printed
= 0, i
;
418 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
419 printed
+= __map_groups__fprintf_removed_maps(mg
, i
, verbose
, fp
);
423 size_t map_groups__fprintf(struct map_groups
*mg
, int verbose
, FILE *fp
)
425 size_t printed
= map_groups__fprintf_maps(mg
, verbose
, fp
);
426 printed
+= fprintf(fp
, "Removed maps:\n");
427 return printed
+ map_groups__fprintf_removed_maps(mg
, verbose
, fp
);
430 int map_groups__fixup_overlappings(struct map_groups
*mg
, struct map
*map
,
431 int verbose
, FILE *fp
)
433 struct rb_root
*root
= &mg
->maps
[map
->type
];
434 struct rb_node
*next
= rb_first(root
);
438 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
439 next
= rb_next(&pos
->rb_node
);
441 if (!map__overlap(pos
, map
))
445 fputs("overlapping maps:\n", fp
);
446 map__fprintf(map
, fp
);
447 map__fprintf(pos
, fp
);
450 rb_erase(&pos
->rb_node
, root
);
452 * Now check if we need to create new maps for areas not
453 * overlapped by the new map:
455 if (map
->start
> pos
->start
) {
456 struct map
*before
= map__clone(pos
);
458 if (before
== NULL
) {
463 before
->end
= map
->start
- 1;
464 map_groups__insert(mg
, before
);
466 map__fprintf(before
, fp
);
469 if (map
->end
< pos
->end
) {
470 struct map
*after
= map__clone(pos
);
477 after
->start
= map
->end
+ 1;
478 map_groups__insert(mg
, after
);
480 map__fprintf(after
, fp
);
484 * If we have references, just move them to a separate list.
487 list_add_tail(&pos
->node
, &mg
->removed_maps
[map
->type
]);
499 * XXX This should not really _copy_ te maps, but refcount them.
501 int map_groups__clone(struct map_groups
*mg
,
502 struct map_groups
*parent
, enum map_type type
)
505 for (nd
= rb_first(&parent
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
506 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
507 struct map
*new = map__clone(map
);
510 map_groups__insert(mg
, new);
515 static u64
map__reloc_map_ip(struct map
*map
, u64 ip
)
517 return ip
+ (s64
)map
->pgoff
;
520 static u64
map__reloc_unmap_ip(struct map
*map
, u64 ip
)
522 return ip
- (s64
)map
->pgoff
;
525 void map__reloc_vmlinux(struct map
*map
)
527 struct kmap
*kmap
= map__kmap(map
);
530 if (!kmap
->ref_reloc_sym
|| !kmap
->ref_reloc_sym
->unrelocated_addr
)
533 reloc
= (kmap
->ref_reloc_sym
->unrelocated_addr
-
534 kmap
->ref_reloc_sym
->addr
);
539 map
->map_ip
= map__reloc_map_ip
;
540 map
->unmap_ip
= map__reloc_unmap_ip
;
544 void maps__insert(struct rb_root
*maps
, struct map
*map
)
546 struct rb_node
**p
= &maps
->rb_node
;
547 struct rb_node
*parent
= NULL
;
548 const u64 ip
= map
->start
;
553 m
= rb_entry(parent
, struct map
, rb_node
);
560 rb_link_node(&map
->rb_node
, parent
, p
);
561 rb_insert_color(&map
->rb_node
, maps
);
564 void maps__remove(struct rb_root
*maps
, struct map
*map
)
566 rb_erase(&map
->rb_node
, maps
);
569 struct map
*maps__find(struct rb_root
*maps
, u64 ip
)
571 struct rb_node
**p
= &maps
->rb_node
;
572 struct rb_node
*parent
= NULL
;
577 m
= rb_entry(parent
, struct map
, rb_node
);
580 else if (ip
> m
->end
)
This page took 0.054275 seconds and 5 git commands to generate.