blob: e21f98001734a2dcc92c464244caa6e97633ee78 [file] [log] [blame]
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001#include "symbol.h"
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03002#include <errno.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03003#include <limits.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02004#include <stdlib.h>
5#include <string.h>
6#include <stdio.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03007#include "map.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02008
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03009const char *map_type__name[MAP__NR_TYPES] = {
10 [MAP__FUNCTION] = "Functions",
11 [MAP__VARIABLE] = "Variables",
12};
13
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020014static inline int is_anon_memory(const char *filename)
15{
16 return strcmp(filename, "//anon") == 0;
17}
18
19static int strcommon(const char *pathname, char *cwd, int cwdlen)
20{
21 int n = 0;
22
23 while (n < cwdlen && pathname[n] == cwd[n])
24 ++n;
25
26 return n;
27}
28
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020029void map__init(struct map *self, enum map_type type,
30 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020031{
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020032 self->type = type;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020033 self->start = start;
34 self->end = end;
35 self->pgoff = pgoff;
36 self->dso = dso;
37 self->map_ip = map__map_ip;
38 self->unmap_ip = map__unmap_ip;
39 RB_CLEAR_NODE(&self->rb_node);
40}
41
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030042struct map *map__new(u64 start, u64 len, u64 pgoff, u32 pid, char *filename,
43 enum map_type type, char *cwd, int cwdlen)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020044{
45 struct map *self = malloc(sizeof(*self));
46
47 if (self != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020048 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020049 struct dso *dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020050 int anon;
51
52 if (cwd) {
53 int n = strcommon(filename, cwd, cwdlen);
54
55 if (n == cwdlen) {
56 snprintf(newfilename, sizeof(newfilename),
57 ".%s", filename + n);
58 filename = newfilename;
59 }
60 }
61
62 anon = is_anon_memory(filename);
63
64 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030065 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020066 filename = newfilename;
67 }
68
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020069 dso = dsos__findnew(filename);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020070 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020071 goto out_delete;
72
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030073 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020074
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020075 if (anon) {
76set_identity:
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020077 self->map_ip = self->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020078 } else if (strcmp(filename, "[vdso]") == 0) {
79 dso__set_loaded(dso, self->type);
80 goto set_identity;
81 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020082 }
83 return self;
84out_delete:
85 free(self);
86 return NULL;
87}
88
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020089void map__delete(struct map *self)
90{
91 free(self);
92}
93
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020094void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020095{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020096 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020097 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020098 if (nd != NULL) {
99 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
100 self->start = sym->start;
101 }
102}
103
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200104void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200105{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200106 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200107 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200108 if (nd != NULL) {
109 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
110 self->end = sym->end;
111 }
112}
113
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200114#define DSO__DELETED "(deleted)"
115
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200116int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200117{
118 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500119 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200120
Masami Hiramatsua1281682009-12-15 10:32:33 -0500121 if (dso__loaded(self->dso, self->type))
122 return 0;
123
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200124 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200125 if (nr < 0) {
126 if (self->dso->has_build_id) {
127 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
128
129 build_id__sprintf(self->dso->build_id,
130 sizeof(self->dso->build_id),
131 sbuild_id);
132 pr_warning("%s with build id %s not found",
133 name, sbuild_id);
134 } else
135 pr_warning("Failed to open %s", name);
136
137 pr_warning(", continuing without symbols\n");
138 return -1;
139 } else if (nr == 0) {
140 const size_t len = strlen(name);
141 const size_t real_len = len - sizeof(DSO__DELETED);
142
143 if (len > sizeof(DSO__DELETED) &&
144 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
145 pr_warning("%.*s was updated, restart the long "
146 "running apps that use it!\n",
147 (int)real_len, name);
148 } else {
149 pr_warning("no symbols found in %s, maybe install "
150 "a debug package?\n", name);
151 }
152
153 return -1;
154 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200155 /*
156 * Only applies to the kernel, as its symtabs aren't relative like the
157 * module ones.
158 */
159 if (self->dso->kernel)
160 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200161
162 return 0;
163}
164
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200165struct symbol *map__find_symbol(struct map *self, u64 addr,
166 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200167{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200168 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200169 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200170
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200171 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200172}
173
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200174struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
175 symbol_filter_t filter)
176{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200177 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200178 return NULL;
179
180 if (!dso__sorted_by_name(self->dso, self->type))
181 dso__sort_by_name(self->dso, self->type);
182
183 return dso__find_symbol_by_name(self->dso, self->type, name);
184}
185
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200186struct map *map__clone(struct map *self)
187{
188 struct map *map = malloc(sizeof(*self));
189
190 if (!map)
191 return NULL;
192
193 memcpy(map, self, sizeof(*self));
194
195 return map;
196}
197
198int map__overlap(struct map *l, struct map *r)
199{
200 if (l->start > r->start) {
201 struct map *t = l;
202 l = r;
203 r = t;
204 }
205
206 if (l->end > r->start)
207 return 1;
208
209 return 0;
210}
211
212size_t map__fprintf(struct map *self, FILE *fp)
213{
214 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
215 self->start, self->end, self->pgoff, self->dso->name);
216}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200217
218/*
219 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
220 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
221 */
222u64 map__rip_2objdump(struct map *map, u64 rip)
223{
224 u64 addr = map->dso->adjust_symbols ?
225 map->unmap_ip(map, rip) : /* RIP -> IP */
226 rip;
227 return addr;
228}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200229
230u64 map__objdump_2ip(struct map *map, u64 addr)
231{
232 u64 ip = map->dso->adjust_symbols ?
233 addr :
234 map->unmap_ip(map, addr); /* RIP -> IP */
235 return ip;
236}
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300237
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300238void map_groups__init(struct map_groups *self)
239{
240 int i;
241 for (i = 0; i < MAP__NR_TYPES; ++i) {
242 self->maps[i] = RB_ROOT;
243 INIT_LIST_HEAD(&self->removed_maps[i]);
244 }
245}
246
247void map_groups__flush(struct map_groups *self)
248{
249 int type;
250
251 for (type = 0; type < MAP__NR_TYPES; type++) {
252 struct rb_root *root = &self->maps[type];
253 struct rb_node *next = rb_first(root);
254
255 while (next) {
256 struct map *pos = rb_entry(next, struct map, rb_node);
257 next = rb_next(&pos->rb_node);
258 rb_erase(&pos->rb_node, root);
259 /*
260 * We may have references to this map, for
261 * instance in some hist_entry instances, so
262 * just move them to a separate list.
263 */
264 list_add_tail(&pos->node, &self->removed_maps[pos->type]);
265 }
266 }
267}
268
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300269struct symbol *map_groups__find_symbol(struct map_groups *self,
270 enum map_type type, u64 addr,
271 symbol_filter_t filter)
272{
273 struct map *map = map_groups__find(self, type, addr);
274
275 if (map != NULL)
276 return map__find_symbol(map, map->map_ip(map, addr), filter);
277
278 return NULL;
279}
280
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300281size_t __map_groups__fprintf_maps(struct map_groups *self,
282 enum map_type type, int verbose, FILE *fp)
283{
284 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
285 struct rb_node *nd;
286
287 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
288 struct map *pos = rb_entry(nd, struct map, rb_node);
289 printed += fprintf(fp, "Map:");
290 printed += map__fprintf(pos, fp);
291 if (verbose > 2) {
292 printed += dso__fprintf(pos->dso, type, fp);
293 printed += fprintf(fp, "--\n");
294 }
295 }
296
297 return printed;
298}
299
300size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp)
301{
302 size_t printed = 0, i;
303 for (i = 0; i < MAP__NR_TYPES; ++i)
304 printed += __map_groups__fprintf_maps(self, i, verbose, fp);
305 return printed;
306}
307
308static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
309 enum map_type type,
310 int verbose, FILE *fp)
311{
312 struct map *pos;
313 size_t printed = 0;
314
315 list_for_each_entry(pos, &self->removed_maps[type], node) {
316 printed += fprintf(fp, "Map:");
317 printed += map__fprintf(pos, fp);
318 if (verbose > 1) {
319 printed += dso__fprintf(pos->dso, type, fp);
320 printed += fprintf(fp, "--\n");
321 }
322 }
323 return printed;
324}
325
326static size_t map_groups__fprintf_removed_maps(struct map_groups *self,
327 int verbose, FILE *fp)
328{
329 size_t printed = 0, i;
330 for (i = 0; i < MAP__NR_TYPES; ++i)
331 printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp);
332 return printed;
333}
334
335size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp)
336{
337 size_t printed = map_groups__fprintf_maps(self, verbose, fp);
338 printed += fprintf(fp, "Removed maps:\n");
339 return printed + map_groups__fprintf_removed_maps(self, verbose, fp);
340}
341
342int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
343 int verbose, FILE *fp)
344{
345 struct rb_root *root = &self->maps[map->type];
346 struct rb_node *next = rb_first(root);
347
348 while (next) {
349 struct map *pos = rb_entry(next, struct map, rb_node);
350 next = rb_next(&pos->rb_node);
351
352 if (!map__overlap(pos, map))
353 continue;
354
355 if (verbose >= 2) {
356 fputs("overlapping maps:\n", fp);
357 map__fprintf(map, fp);
358 map__fprintf(pos, fp);
359 }
360
361 rb_erase(&pos->rb_node, root);
362 /*
363 * We may have references to this map, for instance in some
364 * hist_entry instances, so just move them to a separate
365 * list.
366 */
367 list_add_tail(&pos->node, &self->removed_maps[map->type]);
368 /*
369 * Now check if we need to create new maps for areas not
370 * overlapped by the new map:
371 */
372 if (map->start > pos->start) {
373 struct map *before = map__clone(pos);
374
375 if (before == NULL)
376 return -ENOMEM;
377
378 before->end = map->start - 1;
379 map_groups__insert(self, before);
380 if (verbose >= 2)
381 map__fprintf(before, fp);
382 }
383
384 if (map->end < pos->end) {
385 struct map *after = map__clone(pos);
386
387 if (after == NULL)
388 return -ENOMEM;
389
390 after->start = map->end + 1;
391 map_groups__insert(self, after);
392 if (verbose >= 2)
393 map__fprintf(after, fp);
394 }
395 }
396
397 return 0;
398}
399
400/*
401 * XXX This should not really _copy_ te maps, but refcount them.
402 */
403int map_groups__clone(struct map_groups *self,
404 struct map_groups *parent, enum map_type type)
405{
406 struct rb_node *nd;
407 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
408 struct map *map = rb_entry(nd, struct map, rb_node);
409 struct map *new = map__clone(map);
410 if (new == NULL)
411 return -ENOMEM;
412 map_groups__insert(self, new);
413 }
414 return 0;
415}
416
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300417static u64 map__reloc_map_ip(struct map *map, u64 ip)
418{
419 return ip + (s64)map->pgoff;
420}
421
422static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
423{
424 return ip - (s64)map->pgoff;
425}
426
427void map__reloc_vmlinux(struct map *self)
428{
429 struct kmap *kmap = map__kmap(self);
430 s64 reloc;
431
432 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
433 return;
434
435 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
436 kmap->ref_reloc_sym->addr);
437
438 if (!reloc)
439 return;
440
441 self->map_ip = map__reloc_map_ip;
442 self->unmap_ip = map__reloc_unmap_ip;
443 self->pgoff = reloc;
444}
445
446void maps__insert(struct rb_root *maps, struct map *map)
447{
448 struct rb_node **p = &maps->rb_node;
449 struct rb_node *parent = NULL;
450 const u64 ip = map->start;
451 struct map *m;
452
453 while (*p != NULL) {
454 parent = *p;
455 m = rb_entry(parent, struct map, rb_node);
456 if (ip < m->start)
457 p = &(*p)->rb_left;
458 else
459 p = &(*p)->rb_right;
460 }
461
462 rb_link_node(&map->rb_node, parent, p);
463 rb_insert_color(&map->rb_node, maps);
464}
465
466struct map *maps__find(struct rb_root *maps, u64 ip)
467{
468 struct rb_node **p = &maps->rb_node;
469 struct rb_node *parent = NULL;
470 struct map *m;
471
472 while (*p != NULL) {
473 parent = *p;
474 m = rb_entry(parent, struct map, rb_node);
475 if (ip < m->start)
476 p = &(*p)->rb_left;
477 else if (ip > m->end)
478 p = &(*p)->rb_right;
479 else
480 return m;
481 }
482
483 return NULL;
484}