x86/xen: resume timer irqs early
[linux/fpc-iii.git] / tools / perf / util / map.c
blob4f6680d2043b1e68ef7b9dfbc63a37326fcc6f22
1 #include "symbol.h"
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <limits.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include "map.h"
10 #include "thread.h"
11 #include "strlist.h"
12 #include "vdso.h"
13 #include "build-id.h"
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, "/dev/zero (deleted)") ||
25 !strcmp(filename, "/anon_hugepage (deleted)");
28 static inline int is_no_dso_memory(const char *filename)
30 return !strncmp(filename, "[stack", 6) ||
31 !strcmp(filename, "[heap]");
34 void map__init(struct map *map, enum map_type type,
35 u64 start, u64 end, u64 pgoff, struct dso *dso)
37 map->type = type;
38 map->start = start;
39 map->end = end;
40 map->pgoff = pgoff;
41 map->dso = dso;
42 map->map_ip = map__map_ip;
43 map->unmap_ip = map__unmap_ip;
44 RB_CLEAR_NODE(&map->rb_node);
45 map->groups = NULL;
46 map->referenced = false;
47 map->erange_warned = false;
50 struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
51 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
52 u64 ino_gen, char *filename,
53 enum map_type type)
55 struct map *map = malloc(sizeof(*map));
57 if (map != NULL) {
58 char newfilename[PATH_MAX];
59 struct dso *dso;
60 int anon, no_dso, vdso;
62 anon = is_anon_memory(filename);
63 vdso = is_vdso_map(filename);
64 no_dso = is_no_dso_memory(filename);
66 map->maj = d_maj;
67 map->min = d_min;
68 map->ino = ino;
69 map->ino_generation = ino_gen;
71 if (anon) {
72 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
73 filename = newfilename;
76 if (vdso) {
77 pgoff = 0;
78 dso = vdso__dso_findnew(dsos__list);
79 } else
80 dso = __dsos__findnew(dsos__list, filename);
82 if (dso == NULL)
83 goto out_delete;
85 map__init(map, type, start, start + len, pgoff, dso);
87 if (anon || no_dso) {
88 map->map_ip = map->unmap_ip = identity__map_ip;
91 * Set memory without DSO as loaded. All map__find_*
92 * functions still return NULL, and we avoid the
93 * unnecessary map__load warning.
95 if (no_dso)
96 dso__set_loaded(dso, map->type);
99 return map;
100 out_delete:
101 free(map);
102 return NULL;
106 * Constructor variant for modules (where we know from /proc/modules where
107 * they are loaded) and for vmlinux, where only after we load all the
108 * symbols we'll know where it starts and ends.
110 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
112 struct map *map = calloc(1, (sizeof(*map) +
113 (dso->kernel ? sizeof(struct kmap) : 0)));
114 if (map != NULL) {
116 * ->end will be filled after we load all the symbols
118 map__init(map, type, start, 0, 0, dso);
121 return map;
124 void map__delete(struct map *map)
126 free(map);
129 void map__fixup_start(struct map *map)
131 struct rb_root *symbols = &map->dso->symbols[map->type];
132 struct rb_node *nd = rb_first(symbols);
133 if (nd != NULL) {
134 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
135 map->start = sym->start;
139 void map__fixup_end(struct map *map)
141 struct rb_root *symbols = &map->dso->symbols[map->type];
142 struct rb_node *nd = rb_last(symbols);
143 if (nd != NULL) {
144 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
145 map->end = sym->end;
149 #define DSO__DELETED "(deleted)"
151 int map__load(struct map *map, symbol_filter_t filter)
153 const char *name = map->dso->long_name;
154 int nr;
156 if (dso__loaded(map->dso, map->type))
157 return 0;
159 nr = dso__load(map->dso, map, filter);
160 if (nr < 0) {
161 if (map->dso->has_build_id) {
162 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
164 build_id__sprintf(map->dso->build_id,
165 sizeof(map->dso->build_id),
166 sbuild_id);
167 pr_warning("%s with build id %s not found",
168 name, sbuild_id);
169 } else
170 pr_warning("Failed to open %s", name);
172 pr_warning(", continuing without symbols\n");
173 return -1;
174 } else if (nr == 0) {
175 #ifdef LIBELF_SUPPORT
176 const size_t len = strlen(name);
177 const size_t real_len = len - sizeof(DSO__DELETED);
179 if (len > sizeof(DSO__DELETED) &&
180 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
181 pr_warning("%.*s was updated (is prelink enabled?). "
182 "Restart the long running apps that use it!\n",
183 (int)real_len, name);
184 } else {
185 pr_warning("no symbols found in %s, maybe install "
186 "a debug package?\n", name);
188 #endif
189 return -1;
192 return 0;
195 struct symbol *map__find_symbol(struct map *map, u64 addr,
196 symbol_filter_t filter)
198 if (map__load(map, filter) < 0)
199 return NULL;
201 return dso__find_symbol(map->dso, map->type, addr);
204 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
205 symbol_filter_t filter)
207 if (map__load(map, filter) < 0)
208 return NULL;
210 if (!dso__sorted_by_name(map->dso, map->type))
211 dso__sort_by_name(map->dso, map->type);
213 return dso__find_symbol_by_name(map->dso, map->type, name);
216 struct map *map__clone(struct map *map)
218 return memdup(map, sizeof(*map));
221 int map__overlap(struct map *l, struct map *r)
223 if (l->start > r->start) {
224 struct map *t = l;
225 l = r;
226 r = t;
229 if (l->end > r->start)
230 return 1;
232 return 0;
235 size_t map__fprintf(struct map *map, FILE *fp)
237 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
238 map->start, map->end, map->pgoff, map->dso->name);
241 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
243 const char *dsoname = "[unknown]";
245 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
246 if (symbol_conf.show_kernel_path && map->dso->long_name)
247 dsoname = map->dso->long_name;
248 else if (map->dso->name)
249 dsoname = map->dso->name;
252 return fprintf(fp, "%s", dsoname);
256 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
257 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
258 * relative to section start.
260 u64 map__rip_2objdump(struct map *map, u64 rip)
262 if (!map->dso->adjust_symbols)
263 return rip;
265 if (map->dso->rel)
266 return rip - map->pgoff;
268 return map->unmap_ip(map, rip);
271 void map_groups__init(struct map_groups *mg)
273 int i;
274 for (i = 0; i < MAP__NR_TYPES; ++i) {
275 mg->maps[i] = RB_ROOT;
276 INIT_LIST_HEAD(&mg->removed_maps[i]);
278 mg->machine = NULL;
281 static void maps__delete(struct rb_root *maps)
283 struct rb_node *next = rb_first(maps);
285 while (next) {
286 struct map *pos = rb_entry(next, struct map, rb_node);
288 next = rb_next(&pos->rb_node);
289 rb_erase(&pos->rb_node, maps);
290 map__delete(pos);
294 static void maps__delete_removed(struct list_head *maps)
296 struct map *pos, *n;
298 list_for_each_entry_safe(pos, n, maps, node) {
299 list_del(&pos->node);
300 map__delete(pos);
304 void map_groups__exit(struct map_groups *mg)
306 int i;
308 for (i = 0; i < MAP__NR_TYPES; ++i) {
309 maps__delete(&mg->maps[i]);
310 maps__delete_removed(&mg->removed_maps[i]);
314 void map_groups__flush(struct map_groups *mg)
316 int type;
318 for (type = 0; type < MAP__NR_TYPES; type++) {
319 struct rb_root *root = &mg->maps[type];
320 struct rb_node *next = rb_first(root);
322 while (next) {
323 struct map *pos = rb_entry(next, struct map, rb_node);
324 next = rb_next(&pos->rb_node);
325 rb_erase(&pos->rb_node, root);
327 * We may have references to this map, for
328 * instance in some hist_entry instances, so
329 * just move them to a separate list.
331 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
336 struct symbol *map_groups__find_symbol(struct map_groups *mg,
337 enum map_type type, u64 addr,
338 struct map **mapp,
339 symbol_filter_t filter)
341 struct map *map = map_groups__find(mg, type, addr);
343 if (map != NULL) {
344 if (mapp != NULL)
345 *mapp = map;
346 return map__find_symbol(map, map->map_ip(map, addr), filter);
349 return NULL;
352 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
353 enum map_type type,
354 const char *name,
355 struct map **mapp,
356 symbol_filter_t filter)
358 struct rb_node *nd;
360 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
361 struct map *pos = rb_entry(nd, struct map, rb_node);
362 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
364 if (sym == NULL)
365 continue;
366 if (mapp != NULL)
367 *mapp = pos;
368 return sym;
371 return NULL;
374 size_t __map_groups__fprintf_maps(struct map_groups *mg,
375 enum map_type type, int verbose, FILE *fp)
377 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
378 struct rb_node *nd;
380 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
381 struct map *pos = rb_entry(nd, struct map, rb_node);
382 printed += fprintf(fp, "Map:");
383 printed += map__fprintf(pos, fp);
384 if (verbose > 2) {
385 printed += dso__fprintf(pos->dso, type, fp);
386 printed += fprintf(fp, "--\n");
390 return printed;
393 size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
395 size_t printed = 0, i;
396 for (i = 0; i < MAP__NR_TYPES; ++i)
397 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
398 return printed;
401 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
402 enum map_type type,
403 int verbose, FILE *fp)
405 struct map *pos;
406 size_t printed = 0;
408 list_for_each_entry(pos, &mg->removed_maps[type], node) {
409 printed += fprintf(fp, "Map:");
410 printed += map__fprintf(pos, fp);
411 if (verbose > 1) {
412 printed += dso__fprintf(pos->dso, type, fp);
413 printed += fprintf(fp, "--\n");
416 return printed;
419 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
420 int verbose, FILE *fp)
422 size_t printed = 0, i;
423 for (i = 0; i < MAP__NR_TYPES; ++i)
424 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
425 return printed;
428 size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
430 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
431 printed += fprintf(fp, "Removed maps:\n");
432 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
435 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
436 int verbose, FILE *fp)
438 struct rb_root *root = &mg->maps[map->type];
439 struct rb_node *next = rb_first(root);
440 int err = 0;
442 while (next) {
443 struct map *pos = rb_entry(next, struct map, rb_node);
444 next = rb_next(&pos->rb_node);
446 if (!map__overlap(pos, map))
447 continue;
449 if (verbose >= 2) {
450 fputs("overlapping maps:\n", fp);
451 map__fprintf(map, fp);
452 map__fprintf(pos, fp);
455 rb_erase(&pos->rb_node, root);
457 * Now check if we need to create new maps for areas not
458 * overlapped by the new map:
460 if (map->start > pos->start) {
461 struct map *before = map__clone(pos);
463 if (before == NULL) {
464 err = -ENOMEM;
465 goto move_map;
468 before->end = map->start - 1;
469 map_groups__insert(mg, before);
470 if (verbose >= 2)
471 map__fprintf(before, fp);
474 if (map->end < pos->end) {
475 struct map *after = map__clone(pos);
477 if (after == NULL) {
478 err = -ENOMEM;
479 goto move_map;
482 after->start = map->end + 1;
483 map_groups__insert(mg, after);
484 if (verbose >= 2)
485 map__fprintf(after, fp);
487 move_map:
489 * If we have references, just move them to a separate list.
491 if (pos->referenced)
492 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
493 else
494 map__delete(pos);
496 if (err)
497 return err;
500 return 0;
504 * XXX This should not really _copy_ te maps, but refcount them.
506 int map_groups__clone(struct map_groups *mg,
507 struct map_groups *parent, enum map_type type)
509 struct rb_node *nd;
510 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
511 struct map *map = rb_entry(nd, struct map, rb_node);
512 struct map *new = map__clone(map);
513 if (new == NULL)
514 return -ENOMEM;
515 map_groups__insert(mg, new);
517 return 0;
520 void maps__insert(struct rb_root *maps, struct map *map)
522 struct rb_node **p = &maps->rb_node;
523 struct rb_node *parent = NULL;
524 const u64 ip = map->start;
525 struct map *m;
527 while (*p != NULL) {
528 parent = *p;
529 m = rb_entry(parent, struct map, rb_node);
530 if (ip < m->start)
531 p = &(*p)->rb_left;
532 else
533 p = &(*p)->rb_right;
536 rb_link_node(&map->rb_node, parent, p);
537 rb_insert_color(&map->rb_node, maps);
540 void maps__remove(struct rb_root *maps, struct map *map)
542 rb_erase(&map->rb_node, maps);
545 struct map *maps__find(struct rb_root *maps, u64 ip)
547 struct rb_node **p = &maps->rb_node;
548 struct rb_node *parent = NULL;
549 struct map *m;
551 while (*p != NULL) {
552 parent = *p;
553 m = rb_entry(parent, struct map, rb_node);
554 if (ip < m->start)
555 p = &(*p)->rb_left;
556 else if (ip > m->end)
557 p = &(*p)->rb_right;
558 else
559 return m;
562 return NULL;
565 struct map *maps__first(struct rb_root *maps)
567 struct rb_node *first = rb_first(maps);
569 if (first)
570 return rb_entry(first, struct map, rb_node);
571 return NULL;
574 struct map *maps__next(struct map *map)
576 struct rb_node *next = rb_next(&map->rb_node);
578 if (next)
579 return rb_entry(next, struct map, rb_node);
580 return NULL;