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>
15
16 const char *map_type__name[MAP__NR_TYPES] = {
17 [MAP__FUNCTION] = "Functions",
18 [MAP__VARIABLE] = "Variables",
19 };
20
is_anon_memory(const char * filename)21 static inline int is_anon_memory(const char *filename)
22 {
23 return !strcmp(filename, "//anon") ||
24 !strcmp(filename, "/anon_hugepage (deleted)");
25 }
26
is_no_dso_memory(const char * filename)27 static inline int is_no_dso_memory(const char *filename)
28 {
29 return !strncmp(filename, "[stack", 6) ||
30 !strcmp(filename, "[heap]");
31 }
32
map__init(struct map * map,enum map_type type,u64 start,u64 end,u64 pgoff,struct dso * dso)33 void map__init(struct map *map, enum map_type type,
34 u64 start, u64 end, u64 pgoff, struct dso *dso)
35 {
36 map->type = type;
37 map->start = start;
38 map->end = end;
39 map->pgoff = pgoff;
40 map->dso = dso;
41 map->map_ip = map__map_ip;
42 map->unmap_ip = map__unmap_ip;
43 RB_CLEAR_NODE(&map->rb_node);
44 map->groups = NULL;
45 map->referenced = false;
46 map->erange_warned = false;
47 }
48
map__new(struct list_head * dsos__list,u64 start,u64 len,u64 pgoff,u32 pid,char * filename,enum map_type type)49 struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
50 u64 pgoff, u32 pid, char *filename,
51 enum map_type type)
52 {
53 struct map *map = malloc(sizeof(*map));
54
55 if (map != NULL) {
56 char newfilename[PATH_MAX];
57 struct dso *dso;
58 int anon, no_dso, vdso;
59
60 anon = is_anon_memory(filename);
61 vdso = is_vdso_map(filename);
62 no_dso = is_no_dso_memory(filename);
63
64 if (anon) {
65 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
66 filename = newfilename;
67 }
68
69 if (vdso) {
70 pgoff = 0;
71 dso = vdso__dso_findnew(dsos__list);
72 } else
73 dso = __dsos__findnew(dsos__list, filename);
74
75 if (dso == NULL)
76 goto out_delete;
77
78 map__init(map, type, start, start + len, pgoff, dso);
79
80 if (anon || no_dso) {
81 map->map_ip = map->unmap_ip = identity__map_ip;
82
83 /*
84 * Set memory without DSO as loaded. All map__find_*
85 * functions still return NULL, and we avoid the
86 * unnecessary map__load warning.
87 */
88 if (no_dso)
89 dso__set_loaded(dso, map->type);
90 }
91 }
92 return map;
93 out_delete:
94 free(map);
95 return NULL;
96 }
97
98 /*
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.
102 */
map__new2(u64 start,struct dso * dso,enum map_type type)103 struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
104 {
105 struct map *map = calloc(1, (sizeof(*map) +
106 (dso->kernel ? sizeof(struct kmap) : 0)));
107 if (map != NULL) {
108 /*
109 * ->end will be filled after we load all the symbols
110 */
111 map__init(map, type, start, 0, 0, dso);
112 }
113
114 return map;
115 }
116
map__delete(struct map * map)117 void map__delete(struct map *map)
118 {
119 free(map);
120 }
121
map__fixup_start(struct map * map)122 void map__fixup_start(struct map *map)
123 {
124 struct rb_root *symbols = &map->dso->symbols[map->type];
125 struct rb_node *nd = rb_first(symbols);
126 if (nd != NULL) {
127 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
128 map->start = sym->start;
129 }
130 }
131
map__fixup_end(struct map * map)132 void map__fixup_end(struct map *map)
133 {
134 struct rb_root *symbols = &map->dso->symbols[map->type];
135 struct rb_node *nd = rb_last(symbols);
136 if (nd != NULL) {
137 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
138 map->end = sym->end;
139 }
140 }
141
142 #define DSO__DELETED "(deleted)"
143
map__load(struct map * map,symbol_filter_t filter)144 int map__load(struct map *map, symbol_filter_t filter)
145 {
146 const char *name = map->dso->long_name;
147 int nr;
148
149 if (dso__loaded(map->dso, map->type))
150 return 0;
151
152 nr = dso__load(map->dso, map, filter);
153 if (nr < 0) {
154 if (map->dso->has_build_id) {
155 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
156
157 build_id__sprintf(map->dso->build_id,
158 sizeof(map->dso->build_id),
159 sbuild_id);
160 pr_warning("%s with build id %s not found",
161 name, sbuild_id);
162 } else
163 pr_warning("Failed to open %s", name);
164
165 pr_warning(", continuing without symbols\n");
166 return -1;
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);
171
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);
177 } else {
178 pr_warning("no symbols found in %s, maybe install "
179 "a debug package?\n", name);
180 }
181 #endif
182 return -1;
183 }
184 /*
185 * Only applies to the kernel, as its symtabs aren't relative like the
186 * module ones.
187 */
188 if (map->dso->kernel)
189 map__reloc_vmlinux(map);
190
191 return 0;
192 }
193
map__find_symbol(struct map * map,u64 addr,symbol_filter_t filter)194 struct symbol *map__find_symbol(struct map *map, u64 addr,
195 symbol_filter_t filter)
196 {
197 if (map__load(map, filter) < 0)
198 return NULL;
199
200 return dso__find_symbol(map->dso, map->type, addr);
201 }
202
map__find_symbol_by_name(struct map * map,const char * name,symbol_filter_t filter)203 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
204 symbol_filter_t filter)
205 {
206 if (map__load(map, filter) < 0)
207 return NULL;
208
209 if (!dso__sorted_by_name(map->dso, map->type))
210 dso__sort_by_name(map->dso, map->type);
211
212 return dso__find_symbol_by_name(map->dso, map->type, name);
213 }
214
map__clone(struct map * map)215 struct map *map__clone(struct map *map)
216 {
217 return memdup(map, sizeof(*map));
218 }
219
map__overlap(struct map * l,struct map * r)220 int map__overlap(struct map *l, struct map *r)
221 {
222 if (l->start > r->start) {
223 struct map *t = l;
224 l = r;
225 r = t;
226 }
227
228 if (l->end > r->start)
229 return 1;
230
231 return 0;
232 }
233
map__fprintf(struct map * map,FILE * fp)234 size_t map__fprintf(struct map *map, FILE *fp)
235 {
236 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
237 map->start, map->end, map->pgoff, map->dso->name);
238 }
239
map__fprintf_dsoname(struct map * map,FILE * fp)240 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
241 {
242 const char *dsoname = "[unknown]";
243
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;
249 }
250
251 return fprintf(fp, "%s", dsoname);
252 }
253
254 /*
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.
257 */
map__rip_2objdump(struct map * map,u64 rip)258 u64 map__rip_2objdump(struct map *map, u64 rip)
259 {
260 u64 addr = map->dso->adjust_symbols ?
261 map->unmap_ip(map, rip) : /* RIP -> IP */
262 rip;
263 return addr;
264 }
265
map_groups__init(struct map_groups * mg)266 void map_groups__init(struct map_groups *mg)
267 {
268 int i;
269 for (i = 0; i < MAP__NR_TYPES; ++i) {
270 mg->maps[i] = RB_ROOT;
271 INIT_LIST_HEAD(&mg->removed_maps[i]);
272 }
273 mg->machine = NULL;
274 }
275
maps__delete(struct rb_root * maps)276 static void maps__delete(struct rb_root *maps)
277 {
278 struct rb_node *next = rb_first(maps);
279
280 while (next) {
281 struct map *pos = rb_entry(next, struct map, rb_node);
282
283 next = rb_next(&pos->rb_node);
284 rb_erase(&pos->rb_node, maps);
285 map__delete(pos);
286 }
287 }
288
maps__delete_removed(struct list_head * maps)289 static void maps__delete_removed(struct list_head *maps)
290 {
291 struct map *pos, *n;
292
293 list_for_each_entry_safe(pos, n, maps, node) {
294 list_del(&pos->node);
295 map__delete(pos);
296 }
297 }
298
map_groups__exit(struct map_groups * mg)299 void map_groups__exit(struct map_groups *mg)
300 {
301 int i;
302
303 for (i = 0; i < MAP__NR_TYPES; ++i) {
304 maps__delete(&mg->maps[i]);
305 maps__delete_removed(&mg->removed_maps[i]);
306 }
307 }
308
map_groups__flush(struct map_groups * mg)309 void map_groups__flush(struct map_groups *mg)
310 {
311 int type;
312
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);
316
317 while (next) {
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);
321 /*
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.
325 */
326 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
327 }
328 }
329 }
330
map_groups__find_symbol(struct map_groups * mg,enum map_type type,u64 addr,struct map ** mapp,symbol_filter_t filter)331 struct symbol *map_groups__find_symbol(struct map_groups *mg,
332 enum map_type type, u64 addr,
333 struct map **mapp,
334 symbol_filter_t filter)
335 {
336 struct map *map = map_groups__find(mg, type, addr);
337
338 if (map != NULL) {
339 if (mapp != NULL)
340 *mapp = map;
341 return map__find_symbol(map, map->map_ip(map, addr), filter);
342 }
343
344 return NULL;
345 }
346
map_groups__find_symbol_by_name(struct map_groups * mg,enum map_type type,const char * name,struct map ** mapp,symbol_filter_t filter)347 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
348 enum map_type type,
349 const char *name,
350 struct map **mapp,
351 symbol_filter_t filter)
352 {
353 struct rb_node *nd;
354
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);
358
359 if (sym == NULL)
360 continue;
361 if (mapp != NULL)
362 *mapp = pos;
363 return sym;
364 }
365
366 return NULL;
367 }
368
__map_groups__fprintf_maps(struct map_groups * mg,enum map_type type,int verbose,FILE * fp)369 size_t __map_groups__fprintf_maps(struct map_groups *mg,
370 enum map_type type, int verbose, FILE *fp)
371 {
372 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
373 struct rb_node *nd;
374
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);
379 if (verbose > 2) {
380 printed += dso__fprintf(pos->dso, type, fp);
381 printed += fprintf(fp, "--\n");
382 }
383 }
384
385 return printed;
386 }
387
map_groups__fprintf_maps(struct map_groups * mg,int verbose,FILE * fp)388 size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
389 {
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);
393 return printed;
394 }
395
__map_groups__fprintf_removed_maps(struct map_groups * mg,enum map_type type,int verbose,FILE * fp)396 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
397 enum map_type type,
398 int verbose, FILE *fp)
399 {
400 struct map *pos;
401 size_t printed = 0;
402
403 list_for_each_entry(pos, &mg->removed_maps[type], node) {
404 printed += fprintf(fp, "Map:");
405 printed += map__fprintf(pos, fp);
406 if (verbose > 1) {
407 printed += dso__fprintf(pos->dso, type, fp);
408 printed += fprintf(fp, "--\n");
409 }
410 }
411 return printed;
412 }
413
map_groups__fprintf_removed_maps(struct map_groups * mg,int verbose,FILE * fp)414 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
415 int verbose, FILE *fp)
416 {
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);
420 return printed;
421 }
422
map_groups__fprintf(struct map_groups * mg,int verbose,FILE * fp)423 size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
424 {
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);
428 }
429
map_groups__fixup_overlappings(struct map_groups * mg,struct map * map,int verbose,FILE * fp)430 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
431 int verbose, FILE *fp)
432 {
433 struct rb_root *root = &mg->maps[map->type];
434 struct rb_node *next = rb_first(root);
435 int err = 0;
436
437 while (next) {
438 struct map *pos = rb_entry(next, struct map, rb_node);
439 next = rb_next(&pos->rb_node);
440
441 if (!map__overlap(pos, map))
442 continue;
443
444 if (verbose >= 2) {
445 fputs("overlapping maps:\n", fp);
446 map__fprintf(map, fp);
447 map__fprintf(pos, fp);
448 }
449
450 rb_erase(&pos->rb_node, root);
451 /*
452 * Now check if we need to create new maps for areas not
453 * overlapped by the new map:
454 */
455 if (map->start > pos->start) {
456 struct map *before = map__clone(pos);
457
458 if (before == NULL) {
459 err = -ENOMEM;
460 goto move_map;
461 }
462
463 before->end = map->start - 1;
464 map_groups__insert(mg, before);
465 if (verbose >= 2)
466 map__fprintf(before, fp);
467 }
468
469 if (map->end < pos->end) {
470 struct map *after = map__clone(pos);
471
472 if (after == NULL) {
473 err = -ENOMEM;
474 goto move_map;
475 }
476
477 after->start = map->end + 1;
478 map_groups__insert(mg, after);
479 if (verbose >= 2)
480 map__fprintf(after, fp);
481 }
482 move_map:
483 /*
484 * If we have references, just move them to a separate list.
485 */
486 if (pos->referenced)
487 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
488 else
489 map__delete(pos);
490
491 if (err)
492 return err;
493 }
494
495 return 0;
496 }
497
498 /*
499 * XXX This should not really _copy_ te maps, but refcount them.
500 */
map_groups__clone(struct map_groups * mg,struct map_groups * parent,enum map_type type)501 int map_groups__clone(struct map_groups *mg,
502 struct map_groups *parent, enum map_type type)
503 {
504 struct rb_node *nd;
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);
508 if (new == NULL)
509 return -ENOMEM;
510 map_groups__insert(mg, new);
511 }
512 return 0;
513 }
514
map__reloc_map_ip(struct map * map,u64 ip)515 static u64 map__reloc_map_ip(struct map *map, u64 ip)
516 {
517 return ip + (s64)map->pgoff;
518 }
519
map__reloc_unmap_ip(struct map * map,u64 ip)520 static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
521 {
522 return ip - (s64)map->pgoff;
523 }
524
map__reloc_vmlinux(struct map * map)525 void map__reloc_vmlinux(struct map *map)
526 {
527 struct kmap *kmap = map__kmap(map);
528 s64 reloc;
529
530 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
531 return;
532
533 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
534 kmap->ref_reloc_sym->addr);
535
536 if (!reloc)
537 return;
538
539 map->map_ip = map__reloc_map_ip;
540 map->unmap_ip = map__reloc_unmap_ip;
541 map->pgoff = reloc;
542 }
543
maps__insert(struct rb_root * maps,struct map * map)544 void maps__insert(struct rb_root *maps, struct map *map)
545 {
546 struct rb_node **p = &maps->rb_node;
547 struct rb_node *parent = NULL;
548 const u64 ip = map->start;
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
557 p = &(*p)->rb_right;
558 }
559
560 rb_link_node(&map->rb_node, parent, p);
561 rb_insert_color(&map->rb_node, maps);
562 }
563
maps__remove(struct rb_root * maps,struct map * map)564 void maps__remove(struct rb_root *maps, struct map *map)
565 {
566 rb_erase(&map->rb_node, maps);
567 }
568
maps__find(struct rb_root * maps,u64 ip)569 struct map *maps__find(struct rb_root *maps, u64 ip)
570 {
571 struct rb_node **p = &maps->rb_node;
572 struct rb_node *parent = NULL;
573 struct map *m;
574
575 while (*p != NULL) {
576 parent = *p;
577 m = rb_entry(parent, struct map, rb_node);
578 if (ip < m->start)
579 p = &(*p)->rb_left;
580 else if (ip > m->end)
581 p = &(*p)->rb_right;
582 else
583 return m;
584 }
585
586 return NULL;
587 }
588