perf symbols: Add map_groups__find_ams()
[linux-2.6-block.git] / tools / perf / util / map.c
CommitLineData
66e274f3 1#include "symbol.h"
c6e718ff 2#include <errno.h>
9486aa38 3#include <inttypes.h>
4b8cf846 4#include <limits.h>
66e274f3
FW
5#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
a1645ce1 8#include <unistd.h>
4b8cf846 9#include "map.h"
5cd95c2d 10#include "thread.h"
c80c3c26 11#include "strlist.h"
7dbf4dcf 12#include "vdso.h"
ebb296c2 13#include "build-id.h"
8e16017d 14#include <linux/string.h>
66e274f3 15
3846df2e
ACM
16const char *map_type__name[MAP__NR_TYPES] = {
17 [MAP__FUNCTION] = "Functions",
18 [MAP__VARIABLE] = "Variables",
19};
20
66e274f3
FW
21static inline int is_anon_memory(const char *filename)
22{
d0528b5d 23 return !strcmp(filename, "//anon") ||
89365e6c 24 !strcmp(filename, "/dev/zero (deleted)") ||
d0528b5d 25 !strcmp(filename, "/anon_hugepage (deleted)");
66e274f3
FW
26}
27
87ffef79
JO
28static inline int is_no_dso_memory(const char *filename)
29{
1e82574d 30 return !strncmp(filename, "[stack", 6) ||
87ffef79
JO
31 !strcmp(filename, "[heap]");
32}
33
237a7e04 34void map__init(struct map *map, enum map_type type,
3610583c 35 u64 start, u64 end, u64 pgoff, struct dso *dso)
afb7b4f0 36{
237a7e04
ACM
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;
afb7b4f0
ACM
48}
49
a1645ce1 50struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
5c5e854b
SE
51 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
52 u64 ino_gen, char *filename,
361d1346 53 enum map_type type)
66e274f3 54{
237a7e04 55 struct map *map = malloc(sizeof(*map));
66e274f3 56
237a7e04 57 if (map != NULL) {
66e274f3 58 char newfilename[PATH_MAX];
afb7b4f0 59 struct dso *dso;
7dbf4dcf 60 int anon, no_dso, vdso;
66e274f3 61
66e274f3 62 anon = is_anon_memory(filename);
7dbf4dcf 63 vdso = is_vdso_map(filename);
87ffef79 64 no_dso = is_no_dso_memory(filename);
66e274f3 65
5c5e854b
SE
66 map->maj = d_maj;
67 map->min = d_min;
68 map->ino = ino;
69 map->ino_generation = ino_gen;
70
66e274f3 71 if (anon) {
b177f63f 72 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
66e274f3
FW
73 filename = newfilename;
74 }
75
7dbf4dcf
JO
76 if (vdso) {
77 pgoff = 0;
78 dso = vdso__dso_findnew(dsos__list);
79 } else
80 dso = __dsos__findnew(dsos__list, filename);
81
afb7b4f0 82 if (dso == NULL)
66e274f3
FW
83 goto out_delete;
84
237a7e04 85 map__init(map, type, start, start + len, pgoff, dso);
afb7b4f0 86
87ffef79 87 if (anon || no_dso) {
237a7e04 88 map->map_ip = map->unmap_ip = identity__map_ip;
87ffef79
JO
89
90 /*
91 * Set memory without DSO as loaded. All map__find_*
92 * functions still return NULL, and we avoid the
93 * unnecessary map__load warning.
94 */
95 if (no_dso)
237a7e04 96 dso__set_loaded(dso, map->type);
8d92c02a 97 }
66e274f3 98 }
237a7e04 99 return map;
66e274f3 100out_delete:
237a7e04 101 free(map);
66e274f3
FW
102 return NULL;
103}
104
e5a1845f
NK
105/*
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.
109 */
110struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
111{
112 struct map *map = calloc(1, (sizeof(*map) +
113 (dso->kernel ? sizeof(struct kmap) : 0)));
114 if (map != NULL) {
115 /*
116 * ->end will be filled after we load all the symbols
117 */
118 map__init(map, type, start, 0, 0, dso);
119 }
120
121 return map;
122}
123
237a7e04 124void map__delete(struct map *map)
c338aee8 125{
237a7e04 126 free(map);
c338aee8
ACM
127}
128
237a7e04 129void map__fixup_start(struct map *map)
c338aee8 130{
237a7e04 131 struct rb_root *symbols = &map->dso->symbols[map->type];
fcf1203a 132 struct rb_node *nd = rb_first(symbols);
c338aee8
ACM
133 if (nd != NULL) {
134 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
237a7e04 135 map->start = sym->start;
c338aee8
ACM
136 }
137}
138
237a7e04 139void map__fixup_end(struct map *map)
c338aee8 140{
237a7e04 141 struct rb_root *symbols = &map->dso->symbols[map->type];
fcf1203a 142 struct rb_node *nd = rb_last(symbols);
c338aee8
ACM
143 if (nd != NULL) {
144 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
237a7e04 145 map->end = sym->end;
c338aee8
ACM
146 }
147}
148
d70a5402
ACM
149#define DSO__DELETED "(deleted)"
150
237a7e04 151int map__load(struct map *map, symbol_filter_t filter)
66bd8424 152{
237a7e04 153 const char *name = map->dso->long_name;
a128168d 154 int nr;
79406cd7 155
237a7e04 156 if (dso__loaded(map->dso, map->type))
a128168d
MH
157 return 0;
158
237a7e04 159 nr = dso__load(map->dso, map, filter);
79406cd7 160 if (nr < 0) {
237a7e04 161 if (map->dso->has_build_id) {
79406cd7
ACM
162 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
163
237a7e04
ACM
164 build_id__sprintf(map->dso->build_id,
165 sizeof(map->dso->build_id),
79406cd7
ACM
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);
171
172 pr_warning(", continuing without symbols\n");
173 return -1;
174 } else if (nr == 0) {
89fe808a 175#ifdef HAVE_LIBELF_SUPPORT
79406cd7
ACM
176 const size_t len = strlen(name);
177 const size_t real_len = len - sizeof(DSO__DELETED);
178
179 if (len > sizeof(DSO__DELETED) &&
180 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
e77b15bd
DA
181 pr_warning("%.*s was updated (is prelink enabled?). "
182 "Restart the long running apps that use it!\n",
79406cd7
ACM
183 (int)real_len, name);
184 } else {
185 pr_warning("no symbols found in %s, maybe install "
186 "a debug package?\n", name);
66bd8424 187 }
393be2e3 188#endif
79406cd7 189 return -1;
66bd8424
ACM
190 }
191
79406cd7
ACM
192 return 0;
193}
194
237a7e04 195struct symbol *map__find_symbol(struct map *map, u64 addr,
9de89fe7 196 symbol_filter_t filter)
79406cd7 197{
237a7e04 198 if (map__load(map, filter) < 0)
79406cd7
ACM
199 return NULL;
200
237a7e04 201 return dso__find_symbol(map->dso, map->type, addr);
66bd8424
ACM
202}
203
237a7e04 204struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
79406cd7
ACM
205 symbol_filter_t filter)
206{
237a7e04 207 if (map__load(map, filter) < 0)
79406cd7
ACM
208 return NULL;
209
237a7e04
ACM
210 if (!dso__sorted_by_name(map->dso, map->type))
211 dso__sort_by_name(map->dso, map->type);
79406cd7 212
237a7e04 213 return dso__find_symbol_by_name(map->dso, map->type, name);
79406cd7
ACM
214}
215
237a7e04 216struct map *map__clone(struct map *map)
66e274f3 217{
8e16017d 218 return memdup(map, sizeof(*map));
66e274f3
FW
219}
220
221int map__overlap(struct map *l, struct map *r)
222{
223 if (l->start > r->start) {
224 struct map *t = l;
225 l = r;
226 r = t;
227 }
228
229 if (l->end > r->start)
230 return 1;
231
232 return 0;
233}
234
237a7e04 235size_t map__fprintf(struct map *map, FILE *fp)
66e274f3 236{
9486aa38 237 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
237a7e04 238 map->start, map->end, map->pgoff, map->dso->name);
66e274f3 239}
7a2b6209 240
547a92e0
AN
241size_t map__fprintf_dsoname(struct map *map, FILE *fp)
242{
8f28f19a 243 const char *dsoname = "[unknown]";
547a92e0 244
0bc8d205
AN
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;
8f28f19a 250 }
547a92e0
AN
251
252 return fprintf(fp, "%s", dsoname);
253}
254
7a2b6209
KS
255/*
256 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
0131c4ec
AH
257 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
258 * relative to section start.
7a2b6209
KS
259 */
260u64 map__rip_2objdump(struct map *map, u64 rip)
261{
0131c4ec
AH
262 if (!map->dso->adjust_symbols)
263 return rip;
264
265 if (map->dso->rel)
266 return rip - map->pgoff;
267
268 return map->unmap_ip(map, rip);
7a2b6209 269}
ee11b90b 270
98dfd55d 271void map_groups__init(struct map_groups *mg)
c6e718ff
ACM
272{
273 int i;
274 for (i = 0; i < MAP__NR_TYPES; ++i) {
98dfd55d
ACM
275 mg->maps[i] = RB_ROOT;
276 INIT_LIST_HEAD(&mg->removed_maps[i]);
c6e718ff 277 }
98dfd55d 278 mg->machine = NULL;
c6e718ff
ACM
279}
280
98dfd55d 281static void maps__delete(struct rb_root *maps)
591765fd 282{
98dfd55d 283 struct rb_node *next = rb_first(maps);
591765fd
ACM
284
285 while (next) {
286 struct map *pos = rb_entry(next, struct map, rb_node);
287
288 next = rb_next(&pos->rb_node);
98dfd55d 289 rb_erase(&pos->rb_node, maps);
591765fd
ACM
290 map__delete(pos);
291 }
292}
293
98dfd55d 294static void maps__delete_removed(struct list_head *maps)
591765fd
ACM
295{
296 struct map *pos, *n;
297
98dfd55d 298 list_for_each_entry_safe(pos, n, maps, node) {
591765fd
ACM
299 list_del(&pos->node);
300 map__delete(pos);
301 }
302}
303
98dfd55d 304void map_groups__exit(struct map_groups *mg)
591765fd
ACM
305{
306 int i;
307
308 for (i = 0; i < MAP__NR_TYPES; ++i) {
98dfd55d
ACM
309 maps__delete(&mg->maps[i]);
310 maps__delete_removed(&mg->removed_maps[i]);
591765fd
ACM
311 }
312}
313
98dfd55d 314void map_groups__flush(struct map_groups *mg)
c6e718ff
ACM
315{
316 int type;
317
318 for (type = 0; type < MAP__NR_TYPES; type++) {
98dfd55d 319 struct rb_root *root = &mg->maps[type];
c6e718ff
ACM
320 struct rb_node *next = rb_first(root);
321
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);
326 /*
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.
330 */
98dfd55d 331 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
c6e718ff
ACM
332 }
333 }
334}
335
98dfd55d 336struct symbol *map_groups__find_symbol(struct map_groups *mg,
4b8cf846 337 enum map_type type, u64 addr,
7e5e1b14 338 struct map **mapp,
4b8cf846
ACM
339 symbol_filter_t filter)
340{
98dfd55d 341 struct map *map = map_groups__find(mg, type, addr);
4b8cf846 342
7e5e1b14
ACM
343 if (map != NULL) {
344 if (mapp != NULL)
345 *mapp = map;
4b8cf846 346 return map__find_symbol(map, map->map_ip(map, addr), filter);
7e5e1b14
ACM
347 }
348
349 return NULL;
350}
351
98dfd55d 352struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
7e5e1b14
ACM
353 enum map_type type,
354 const char *name,
355 struct map **mapp,
356 symbol_filter_t filter)
357{
358 struct rb_node *nd;
359
98dfd55d 360 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
7e5e1b14
ACM
361 struct map *pos = rb_entry(nd, struct map, rb_node);
362 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
363
364 if (sym == NULL)
365 continue;
366 if (mapp != NULL)
367 *mapp = pos;
368 return sym;
369 }
4b8cf846
ACM
370
371 return NULL;
372}
373
4e987712
ACM
374int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
375{
376 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
377 if (ams->map->groups == NULL)
378 return -1;
379 ams->map = map_groups__find(ams->map->groups, ams->map->type,
380 ams->addr);
381 if (ams->map == NULL)
382 return -1;
383 }
384
385 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
386 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
387
388 return ams->sym ? 0 : -1;
389}
390
98dfd55d 391size_t __map_groups__fprintf_maps(struct map_groups *mg,
c6e718ff
ACM
392 enum map_type type, int verbose, FILE *fp)
393{
394 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
395 struct rb_node *nd;
396
98dfd55d 397 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
c6e718ff
ACM
398 struct map *pos = rb_entry(nd, struct map, rb_node);
399 printed += fprintf(fp, "Map:");
400 printed += map__fprintf(pos, fp);
401 if (verbose > 2) {
402 printed += dso__fprintf(pos->dso, type, fp);
403 printed += fprintf(fp, "--\n");
404 }
405 }
406
407 return printed;
408}
409
98dfd55d 410size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
c6e718ff
ACM
411{
412 size_t printed = 0, i;
413 for (i = 0; i < MAP__NR_TYPES; ++i)
98dfd55d 414 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
c6e718ff
ACM
415 return printed;
416}
417
98dfd55d 418static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
c6e718ff
ACM
419 enum map_type type,
420 int verbose, FILE *fp)
421{
422 struct map *pos;
423 size_t printed = 0;
424
98dfd55d 425 list_for_each_entry(pos, &mg->removed_maps[type], node) {
c6e718ff
ACM
426 printed += fprintf(fp, "Map:");
427 printed += map__fprintf(pos, fp);
428 if (verbose > 1) {
429 printed += dso__fprintf(pos->dso, type, fp);
430 printed += fprintf(fp, "--\n");
431 }
432 }
433 return printed;
434}
435
98dfd55d 436static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
c6e718ff
ACM
437 int verbose, FILE *fp)
438{
439 size_t printed = 0, i;
440 for (i = 0; i < MAP__NR_TYPES; ++i)
98dfd55d 441 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
c6e718ff
ACM
442 return printed;
443}
444
98dfd55d 445size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
c6e718ff 446{
98dfd55d 447 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
c6e718ff 448 printed += fprintf(fp, "Removed maps:\n");
98dfd55d 449 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
c6e718ff
ACM
450}
451
98dfd55d 452int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
c6e718ff
ACM
453 int verbose, FILE *fp)
454{
98dfd55d 455 struct rb_root *root = &mg->maps[map->type];
c6e718ff 456 struct rb_node *next = rb_first(root);
0a1eae39 457 int err = 0;
c6e718ff
ACM
458
459 while (next) {
460 struct map *pos = rb_entry(next, struct map, rb_node);
461 next = rb_next(&pos->rb_node);
462
463 if (!map__overlap(pos, map))
464 continue;
465
466 if (verbose >= 2) {
467 fputs("overlapping maps:\n", fp);
468 map__fprintf(map, fp);
469 map__fprintf(pos, fp);
470 }
471
472 rb_erase(&pos->rb_node, root);
c6e718ff
ACM
473 /*
474 * Now check if we need to create new maps for areas not
475 * overlapped by the new map:
476 */
477 if (map->start > pos->start) {
478 struct map *before = map__clone(pos);
479
0a1eae39
ACM
480 if (before == NULL) {
481 err = -ENOMEM;
482 goto move_map;
483 }
c6e718ff
ACM
484
485 before->end = map->start - 1;
98dfd55d 486 map_groups__insert(mg, before);
c6e718ff
ACM
487 if (verbose >= 2)
488 map__fprintf(before, fp);
489 }
490
491 if (map->end < pos->end) {
492 struct map *after = map__clone(pos);
493
0a1eae39
ACM
494 if (after == NULL) {
495 err = -ENOMEM;
496 goto move_map;
497 }
c6e718ff
ACM
498
499 after->start = map->end + 1;
98dfd55d 500 map_groups__insert(mg, after);
c6e718ff
ACM
501 if (verbose >= 2)
502 map__fprintf(after, fp);
503 }
0a1eae39
ACM
504move_map:
505 /*
506 * If we have references, just move them to a separate list.
507 */
508 if (pos->referenced)
98dfd55d 509 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
0a1eae39
ACM
510 else
511 map__delete(pos);
512
513 if (err)
514 return err;
c6e718ff
ACM
515 }
516
517 return 0;
518}
519
520/*
521 * XXX This should not really _copy_ te maps, but refcount them.
522 */
98dfd55d 523int map_groups__clone(struct map_groups *mg,
c6e718ff
ACM
524 struct map_groups *parent, enum map_type type)
525{
526 struct rb_node *nd;
527 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
528 struct map *map = rb_entry(nd, struct map, rb_node);
529 struct map *new = map__clone(map);
530 if (new == NULL)
531 return -ENOMEM;
98dfd55d 532 map_groups__insert(mg, new);
c6e718ff
ACM
533 }
534 return 0;
535}
536
4b8cf846
ACM
537void maps__insert(struct rb_root *maps, struct map *map)
538{
539 struct rb_node **p = &maps->rb_node;
540 struct rb_node *parent = NULL;
541 const u64 ip = map->start;
542 struct map *m;
543
544 while (*p != NULL) {
545 parent = *p;
546 m = rb_entry(parent, struct map, rb_node);
547 if (ip < m->start)
548 p = &(*p)->rb_left;
549 else
550 p = &(*p)->rb_right;
551 }
552
553 rb_link_node(&map->rb_node, parent, p);
554 rb_insert_color(&map->rb_node, maps);
555}
556
237a7e04 557void maps__remove(struct rb_root *maps, struct map *map)
076c6e45 558{
237a7e04 559 rb_erase(&map->rb_node, maps);
076c6e45
ACM
560}
561
4b8cf846
ACM
562struct map *maps__find(struct rb_root *maps, u64 ip)
563{
564 struct rb_node **p = &maps->rb_node;
565 struct rb_node *parent = NULL;
566 struct map *m;
567
568 while (*p != NULL) {
569 parent = *p;
570 m = rb_entry(parent, struct map, rb_node);
571 if (ip < m->start)
572 p = &(*p)->rb_left;
573 else if (ip > m->end)
574 p = &(*p)->rb_right;
575 else
576 return m;
577 }
578
579 return NULL;
580}
8e0cf965
AH
581
582struct map *maps__first(struct rb_root *maps)
583{
584 struct rb_node *first = rb_first(maps);
585
586 if (first)
587 return rb_entry(first, struct map, rb_node);
588 return NULL;
589}
590
591struct map *maps__next(struct map *map)
592{
593 struct rb_node *next = rb_next(&map->rb_node);
594
595 if (next)
596 return rb_entry(next, struct map, rb_node);
597 return NULL;
598}