10 const char *map_type__name
[MAP__NR_TYPES
] = {
11 [MAP__FUNCTION
] = "Functions",
12 [MAP__VARIABLE
] = "Variables",
15 static inline int is_anon_memory(const char *filename
)
17 return strcmp(filename
, "//anon") == 0;
20 void map__init(struct map
*self
, enum map_type type
,
21 u64 start
, u64 end
, u64 pgoff
, struct dso
*dso
)
28 self
->map_ip
= map__map_ip
;
29 self
->unmap_ip
= map__unmap_ip
;
30 RB_CLEAR_NODE(&self
->rb_node
);
32 self
->referenced
= false;
35 struct map
*map__new(struct list_head
*dsos__list
, u64 start
, u64 len
,
36 u64 pgoff
, u32 pid
, char *filename
,
39 struct map
*self
= malloc(sizeof(*self
));
42 char newfilename
[PATH_MAX
];
46 anon
= is_anon_memory(filename
);
49 snprintf(newfilename
, sizeof(newfilename
), "/tmp/perf-%d.map", pid
);
50 filename
= newfilename
;
53 dso
= __dsos__findnew(dsos__list
, filename
);
57 map__init(self
, type
, start
, start
+ len
, pgoff
, dso
);
61 self
->map_ip
= self
->unmap_ip
= identity__map_ip
;
62 } else if (strcmp(filename
, "[vdso]") == 0) {
63 dso__set_loaded(dso
, self
->type
);
73 void map__delete(struct map
*self
)
78 void map__fixup_start(struct map
*self
)
80 struct rb_root
*symbols
= &self
->dso
->symbols
[self
->type
];
81 struct rb_node
*nd
= rb_first(symbols
);
83 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
84 self
->start
= sym
->start
;
88 void map__fixup_end(struct map
*self
)
90 struct rb_root
*symbols
= &self
->dso
->symbols
[self
->type
];
91 struct rb_node
*nd
= rb_last(symbols
);
93 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
98 #define DSO__DELETED "(deleted)"
100 int map__load(struct map
*self
, symbol_filter_t filter
)
102 const char *name
= self
->dso
->long_name
;
105 if (dso__loaded(self
->dso
, self
->type
))
108 nr
= dso__load(self
->dso
, self
, filter
);
110 if (self
->dso
->has_build_id
) {
111 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
113 build_id__sprintf(self
->dso
->build_id
,
114 sizeof(self
->dso
->build_id
),
116 pr_warning("%s with build id %s not found",
119 pr_warning("Failed to open %s", name
);
121 pr_warning(", continuing without symbols\n");
123 } else if (nr
== 0) {
124 const size_t len
= strlen(name
);
125 const size_t real_len
= len
- sizeof(DSO__DELETED
);
127 if (len
> sizeof(DSO__DELETED
) &&
128 strcmp(name
+ real_len
+ 1, DSO__DELETED
) == 0) {
129 pr_warning("%.*s was updated, restart the long "
130 "running apps that use it!\n",
131 (int)real_len
, name
);
133 pr_warning("no symbols found in %s, maybe install "
134 "a debug package?\n", name
);
140 * Only applies to the kernel, as its symtabs aren't relative like the
143 if (self
->dso
->kernel
)
144 map__reloc_vmlinux(self
);
149 struct symbol
*map__find_symbol(struct map
*self
, u64 addr
,
150 symbol_filter_t filter
)
152 if (map__load(self
, filter
) < 0)
155 return dso__find_symbol(self
->dso
, self
->type
, addr
);
158 struct symbol
*map__find_symbol_by_name(struct map
*self
, const char *name
,
159 symbol_filter_t filter
)
161 if (map__load(self
, filter
) < 0)
164 if (!dso__sorted_by_name(self
->dso
, self
->type
))
165 dso__sort_by_name(self
->dso
, self
->type
);
167 return dso__find_symbol_by_name(self
->dso
, self
->type
, name
);
170 struct map
*map__clone(struct map
*self
)
172 struct map
*map
= malloc(sizeof(*self
));
177 memcpy(map
, self
, sizeof(*self
));
182 int map__overlap(struct map
*l
, struct map
*r
)
184 if (l
->start
> r
->start
) {
190 if (l
->end
> r
->start
)
196 size_t map__fprintf(struct map
*self
, FILE *fp
)
198 return fprintf(fp
, " %Lx-%Lx %Lx %s\n",
199 self
->start
, self
->end
, self
->pgoff
, self
->dso
->name
);
203 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
204 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
206 u64
map__rip_2objdump(struct map
*map
, u64 rip
)
208 u64 addr
= map
->dso
->adjust_symbols
?
209 map
->unmap_ip(map
, rip
) : /* RIP -> IP */
214 u64
map__objdump_2ip(struct map
*map
, u64 addr
)
216 u64 ip
= map
->dso
->adjust_symbols
?
218 map
->unmap_ip(map
, addr
); /* RIP -> IP */
222 void map_groups__init(struct map_groups
*self
)
225 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
226 self
->maps
[i
] = RB_ROOT
;
227 INIT_LIST_HEAD(&self
->removed_maps
[i
]);
229 self
->machine
= NULL
;
232 static void maps__delete(struct rb_root
*self
)
234 struct rb_node
*next
= rb_first(self
);
237 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
239 next
= rb_next(&pos
->rb_node
);
240 rb_erase(&pos
->rb_node
, self
);
245 static void maps__delete_removed(struct list_head
*self
)
249 list_for_each_entry_safe(pos
, n
, self
, node
) {
250 list_del(&pos
->node
);
255 void map_groups__exit(struct map_groups
*self
)
259 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
260 maps__delete(&self
->maps
[i
]);
261 maps__delete_removed(&self
->removed_maps
[i
]);
265 void map_groups__flush(struct map_groups
*self
)
269 for (type
= 0; type
< MAP__NR_TYPES
; type
++) {
270 struct rb_root
*root
= &self
->maps
[type
];
271 struct rb_node
*next
= rb_first(root
);
274 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
275 next
= rb_next(&pos
->rb_node
);
276 rb_erase(&pos
->rb_node
, root
);
278 * We may have references to this map, for
279 * instance in some hist_entry instances, so
280 * just move them to a separate list.
282 list_add_tail(&pos
->node
, &self
->removed_maps
[pos
->type
]);
287 struct symbol
*map_groups__find_symbol(struct map_groups
*self
,
288 enum map_type type
, u64 addr
,
290 symbol_filter_t filter
)
292 struct map
*map
= map_groups__find(self
, type
, addr
);
297 return map__find_symbol(map
, map
->map_ip(map
, addr
), filter
);
303 struct symbol
*map_groups__find_symbol_by_name(struct map_groups
*self
,
307 symbol_filter_t filter
)
311 for (nd
= rb_first(&self
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
312 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
313 struct symbol
*sym
= map__find_symbol_by_name(pos
, name
, filter
);
325 size_t __map_groups__fprintf_maps(struct map_groups
*self
,
326 enum map_type type
, int verbose
, FILE *fp
)
328 size_t printed
= fprintf(fp
, "%s:\n", map_type__name
[type
]);
331 for (nd
= rb_first(&self
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
332 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
333 printed
+= fprintf(fp
, "Map:");
334 printed
+= map__fprintf(pos
, fp
);
336 printed
+= dso__fprintf(pos
->dso
, type
, fp
);
337 printed
+= fprintf(fp
, "--\n");
344 size_t map_groups__fprintf_maps(struct map_groups
*self
, int verbose
, FILE *fp
)
346 size_t printed
= 0, i
;
347 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
348 printed
+= __map_groups__fprintf_maps(self
, i
, verbose
, fp
);
352 static size_t __map_groups__fprintf_removed_maps(struct map_groups
*self
,
354 int verbose
, FILE *fp
)
359 list_for_each_entry(pos
, &self
->removed_maps
[type
], node
) {
360 printed
+= fprintf(fp
, "Map:");
361 printed
+= map__fprintf(pos
, fp
);
363 printed
+= dso__fprintf(pos
->dso
, type
, fp
);
364 printed
+= fprintf(fp
, "--\n");
370 static size_t map_groups__fprintf_removed_maps(struct map_groups
*self
,
371 int verbose
, FILE *fp
)
373 size_t printed
= 0, i
;
374 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
375 printed
+= __map_groups__fprintf_removed_maps(self
, i
, verbose
, fp
);
379 size_t map_groups__fprintf(struct map_groups
*self
, int verbose
, FILE *fp
)
381 size_t printed
= map_groups__fprintf_maps(self
, verbose
, fp
);
382 printed
+= fprintf(fp
, "Removed maps:\n");
383 return printed
+ map_groups__fprintf_removed_maps(self
, verbose
, fp
);
386 int map_groups__fixup_overlappings(struct map_groups
*self
, struct map
*map
,
387 int verbose
, FILE *fp
)
389 struct rb_root
*root
= &self
->maps
[map
->type
];
390 struct rb_node
*next
= rb_first(root
);
394 struct map
*pos
= rb_entry(next
, struct map
, rb_node
);
395 next
= rb_next(&pos
->rb_node
);
397 if (!map__overlap(pos
, map
))
401 fputs("overlapping maps:\n", fp
);
402 map__fprintf(map
, fp
);
403 map__fprintf(pos
, fp
);
406 rb_erase(&pos
->rb_node
, root
);
408 * Now check if we need to create new maps for areas not
409 * overlapped by the new map:
411 if (map
->start
> pos
->start
) {
412 struct map
*before
= map__clone(pos
);
414 if (before
== NULL
) {
419 before
->end
= map
->start
- 1;
420 map_groups__insert(self
, before
);
422 map__fprintf(before
, fp
);
425 if (map
->end
< pos
->end
) {
426 struct map
*after
= map__clone(pos
);
433 after
->start
= map
->end
+ 1;
434 map_groups__insert(self
, after
);
436 map__fprintf(after
, fp
);
440 * If we have references, just move them to a separate list.
443 list_add_tail(&pos
->node
, &self
->removed_maps
[map
->type
]);
455 * XXX This should not really _copy_ te maps, but refcount them.
457 int map_groups__clone(struct map_groups
*self
,
458 struct map_groups
*parent
, enum map_type type
)
461 for (nd
= rb_first(&parent
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
462 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
463 struct map
*new = map__clone(map
);
466 map_groups__insert(self
, new);
471 static u64
map__reloc_map_ip(struct map
*map
, u64 ip
)
473 return ip
+ (s64
)map
->pgoff
;
476 static u64
map__reloc_unmap_ip(struct map
*map
, u64 ip
)
478 return ip
- (s64
)map
->pgoff
;
481 void map__reloc_vmlinux(struct map
*self
)
483 struct kmap
*kmap
= map__kmap(self
);
486 if (!kmap
->ref_reloc_sym
|| !kmap
->ref_reloc_sym
->unrelocated_addr
)
489 reloc
= (kmap
->ref_reloc_sym
->unrelocated_addr
-
490 kmap
->ref_reloc_sym
->addr
);
495 self
->map_ip
= map__reloc_map_ip
;
496 self
->unmap_ip
= map__reloc_unmap_ip
;
500 void maps__insert(struct rb_root
*maps
, struct map
*map
)
502 struct rb_node
**p
= &maps
->rb_node
;
503 struct rb_node
*parent
= NULL
;
504 const u64 ip
= map
->start
;
509 m
= rb_entry(parent
, struct map
, rb_node
);
516 rb_link_node(&map
->rb_node
, parent
, p
);
517 rb_insert_color(&map
->rb_node
, maps
);
520 void maps__remove(struct rb_root
*self
, struct map
*map
)
522 rb_erase(&map
->rb_node
, self
);
525 struct map
*maps__find(struct rb_root
*maps
, u64 ip
)
527 struct rb_node
**p
= &maps
->rb_node
;
528 struct rb_node
*parent
= NULL
;
533 m
= rb_entry(parent
, struct map
, rb_node
);
536 else if (ip
> m
->end
)
545 int machine__init(struct machine
*self
, const char *root_dir
, pid_t pid
)
547 map_groups__init(&self
->kmaps
);
548 RB_CLEAR_NODE(&self
->rb_node
);
549 INIT_LIST_HEAD(&self
->user_dsos
);
550 INIT_LIST_HEAD(&self
->kernel_dsos
);
552 self
->kmaps
.machine
= self
;
554 self
->root_dir
= strdup(root_dir
);
555 return self
->root_dir
== NULL
? -ENOMEM
: 0;
558 static void dsos__delete(struct list_head
*self
)
562 list_for_each_entry_safe(pos
, n
, self
, node
) {
563 list_del(&pos
->node
);
568 void machine__exit(struct machine
*self
)
570 map_groups__exit(&self
->kmaps
);
571 dsos__delete(&self
->user_dsos
);
572 dsos__delete(&self
->kernel_dsos
);
573 free(self
->root_dir
);
574 self
->root_dir
= NULL
;
577 void machine__delete(struct machine
*self
)
583 struct machine
*machines__add(struct rb_root
*self
, pid_t pid
,
584 const char *root_dir
)
586 struct rb_node
**p
= &self
->rb_node
;
587 struct rb_node
*parent
= NULL
;
588 struct machine
*pos
, *machine
= malloc(sizeof(*machine
));
593 if (machine__init(machine
, root_dir
, pid
) != 0) {
600 pos
= rb_entry(parent
, struct machine
, rb_node
);
607 rb_link_node(&machine
->rb_node
, parent
, p
);
608 rb_insert_color(&machine
->rb_node
, self
);
613 struct machine
*machines__find(struct rb_root
*self
, pid_t pid
)
615 struct rb_node
**p
= &self
->rb_node
;
616 struct rb_node
*parent
= NULL
;
617 struct machine
*machine
;
618 struct machine
*default_machine
= NULL
;
622 machine
= rb_entry(parent
, struct machine
, rb_node
);
623 if (pid
< machine
->pid
)
625 else if (pid
> machine
->pid
)
630 default_machine
= machine
;
633 return default_machine
;
636 struct machine
*machines__findnew(struct rb_root
*self
, pid_t pid
)
639 const char *root_dir
;
640 struct machine
*machine
= machines__find(self
, pid
);
642 if (!machine
|| machine
->pid
!= pid
) {
643 if (pid
== HOST_KERNEL_ID
|| pid
== DEFAULT_GUEST_KERNEL_ID
)
646 if (!symbol_conf
.guestmount
)
648 sprintf(path
, "%s/%d", symbol_conf
.guestmount
, pid
);
649 if (access(path
, R_OK
)) {
650 pr_err("Can't access file %s\n", path
);
655 machine
= machines__add(self
, pid
, root_dir
);
662 void machines__process(struct rb_root
*self
, machine__process_t process
, void *data
)
666 for (nd
= rb_first(self
); nd
; nd
= rb_next(nd
)) {
667 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
672 char *machine__mmap_name(struct machine
*self
, char *bf
, size_t size
)
674 if (machine__is_host(self
))
675 snprintf(bf
, size
, "[%s]", "kernel.kallsyms");
676 else if (machine__is_default_guest(self
))
677 snprintf(bf
, size
, "[%s]", "guest.kernel.kallsyms");
679 snprintf(bf
, size
, "[%s.%d]", "guest.kernel.kallsyms", self
->pid
);