perf trace: Improve the error messages
[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
98dfd55d 374size_t __map_groups__fprintf_maps(struct map_groups *mg,
c6e718ff
ACM
375 enum map_type type, int verbose, FILE *fp)
376{
377 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
378 struct rb_node *nd;
379
98dfd55d 380 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
c6e718ff
ACM
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");
387 }
388 }
389
390 return printed;
391}
392
98dfd55d 393size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
c6e718ff
ACM
394{
395 size_t printed = 0, i;
396 for (i = 0; i < MAP__NR_TYPES; ++i)
98dfd55d 397 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
c6e718ff
ACM
398 return printed;
399}
400
98dfd55d 401static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
c6e718ff
ACM
402 enum map_type type,
403 int verbose, FILE *fp)
404{
405 struct map *pos;
406 size_t printed = 0;
407
98dfd55d 408 list_for_each_entry(pos, &mg->removed_maps[type], node) {
c6e718ff
ACM
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");
414 }
415 }
416 return printed;
417}
418
98dfd55d 419static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
c6e718ff
ACM
420 int verbose, FILE *fp)
421{
422 size_t printed = 0, i;
423 for (i = 0; i < MAP__NR_TYPES; ++i)
98dfd55d 424 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
c6e718ff
ACM
425 return printed;
426}
427
98dfd55d 428size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
c6e718ff 429{
98dfd55d 430 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
c6e718ff 431 printed += fprintf(fp, "Removed maps:\n");
98dfd55d 432 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
c6e718ff
ACM
433}
434
98dfd55d 435int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
c6e718ff
ACM
436 int verbose, FILE *fp)
437{
98dfd55d 438 struct rb_root *root = &mg->maps[map->type];
c6e718ff 439 struct rb_node *next = rb_first(root);
0a1eae39 440 int err = 0;
c6e718ff
ACM
441
442 while (next) {
443 struct map *pos = rb_entry(next, struct map, rb_node);
444 next = rb_next(&pos->rb_node);
445
446 if (!map__overlap(pos, map))
447 continue;
448
449 if (verbose >= 2) {
450 fputs("overlapping maps:\n", fp);
451 map__fprintf(map, fp);
452 map__fprintf(pos, fp);
453 }
454
455 rb_erase(&pos->rb_node, root);
c6e718ff
ACM
456 /*
457 * Now check if we need to create new maps for areas not
458 * overlapped by the new map:
459 */
460 if (map->start > pos->start) {
461 struct map *before = map__clone(pos);
462
0a1eae39
ACM
463 if (before == NULL) {
464 err = -ENOMEM;
465 goto move_map;
466 }
c6e718ff
ACM
467
468 before->end = map->start - 1;
98dfd55d 469 map_groups__insert(mg, before);
c6e718ff
ACM
470 if (verbose >= 2)
471 map__fprintf(before, fp);
472 }
473
474 if (map->end < pos->end) {
475 struct map *after = map__clone(pos);
476
0a1eae39
ACM
477 if (after == NULL) {
478 err = -ENOMEM;
479 goto move_map;
480 }
c6e718ff
ACM
481
482 after->start = map->end + 1;
98dfd55d 483 map_groups__insert(mg, after);
c6e718ff
ACM
484 if (verbose >= 2)
485 map__fprintf(after, fp);
486 }
0a1eae39
ACM
487move_map:
488 /*
489 * If we have references, just move them to a separate list.
490 */
491 if (pos->referenced)
98dfd55d 492 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
0a1eae39
ACM
493 else
494 map__delete(pos);
495
496 if (err)
497 return err;
c6e718ff
ACM
498 }
499
500 return 0;
501}
502
503/*
504 * XXX This should not really _copy_ te maps, but refcount them.
505 */
98dfd55d 506int map_groups__clone(struct map_groups *mg,
c6e718ff
ACM
507 struct map_groups *parent, enum map_type type)
508{
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;
98dfd55d 515 map_groups__insert(mg, new);
c6e718ff
ACM
516 }
517 return 0;
518}
519
4b8cf846
ACM
520void maps__insert(struct rb_root *maps, struct map *map)
521{
522 struct rb_node **p = &maps->rb_node;
523 struct rb_node *parent = NULL;
524 const u64 ip = map->start;
525 struct map *m;
526
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;
534 }
535
536 rb_link_node(&map->rb_node, parent, p);
537 rb_insert_color(&map->rb_node, maps);
538}
539
237a7e04 540void maps__remove(struct rb_root *maps, struct map *map)
076c6e45 541{
237a7e04 542 rb_erase(&map->rb_node, maps);
076c6e45
ACM
543}
544
4b8cf846
ACM
545struct map *maps__find(struct rb_root *maps, u64 ip)
546{
547 struct rb_node **p = &maps->rb_node;
548 struct rb_node *parent = NULL;
549 struct map *m;
550
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;
560 }
561
562 return NULL;
563}
8e0cf965
AH
564
565struct map *maps__first(struct rb_root *maps)
566{
567 struct rb_node *first = rb_first(maps);
568
569 if (first)
570 return rb_entry(first, struct map, rb_node);
571 return NULL;
572}
573
574struct map *maps__next(struct map *map)
575{
576 struct rb_node *next = rb_next(&map->rb_node);
577
578 if (next)
579 return rb_entry(next, struct map, rb_node);
580 return NULL;
581}