blob: ff94425779a2b291b6d2ea19600f19f34de815e6 [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 Melo9486aa32011-01-22 20:37:02 -02003#include <inttypes.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03004#include <limits.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02005#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08008#include <unistd.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03009#include "map.h"
David Ahern5cd95c22012-07-20 17:25:47 -060010#include "thread.h"
David Ahernc80c3c22012-07-20 17:25:51 -060011#include "strlist.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020012#include "vdso.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020013#include "build-id.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020014
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030015const char *map_type__name[MAP__NR_TYPES] = {
16 [MAP__FUNCTION] = "Functions",
17 [MAP__VARIABLE] = "Variables",
18};
19
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020020static inline int is_anon_memory(const char *filename)
21{
Joshua Zhud0528b52013-01-05 13:29:57 +080022 return !strcmp(filename, "//anon") ||
23 !strcmp(filename, "/anon_hugepage (deleted)");
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020024}
25
Jiri Olsa87ffef72011-08-24 15:18:34 +020026static inline int is_no_dso_memory(const char *filename)
27{
Namhyung Kim1e825742012-11-07 16:27:11 +090028 return !strncmp(filename, "[stack", 6) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020029 !strcmp(filename, "[heap]");
30}
31
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020032void map__init(struct map *self, enum map_type type,
33 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020034{
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020035 self->type = type;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020036 self->start = start;
37 self->end = end;
38 self->pgoff = pgoff;
39 self->dso = dso;
40 self->map_ip = map__map_ip;
41 self->unmap_ip = map__unmap_ip;
42 RB_CLEAR_NODE(&self->rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080043 self->groups = NULL;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -030044 self->referenced = false;
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -030045 self->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020046}
47
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080048struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
49 u64 pgoff, u32 pid, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010050 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020051{
52 struct map *self = malloc(sizeof(*self));
53
54 if (self != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020055 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020056 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020057 int anon, no_dso, vdso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020058
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020059 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020060 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020061 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020062
63 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030064 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020065 filename = newfilename;
66 }
67
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020068 if (vdso) {
69 pgoff = 0;
70 dso = vdso__dso_findnew(dsos__list);
71 } else
72 dso = __dsos__findnew(dsos__list, filename);
73
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020074 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020075 goto out_delete;
76
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030077 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020078
Jiri Olsa87ffef72011-08-24 15:18:34 +020079 if (anon || no_dso) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020080 self->map_ip = self->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020081
82 /*
83 * Set memory without DSO as loaded. All map__find_*
84 * functions still return NULL, and we avoid the
85 * unnecessary map__load warning.
86 */
87 if (no_dso)
88 dso__set_loaded(dso, self->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020089 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020090 }
91 return self;
92out_delete:
93 free(self);
94 return NULL;
95}
96
Namhyung Kime5a18452012-08-06 13:41:20 +090097/*
98 * Constructor variant for modules (where we know from /proc/modules where
99 * they are loaded) and for vmlinux, where only after we load all the
100 * symbols we'll know where it starts and ends.
101 */
102struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
103{
104 struct map *map = calloc(1, (sizeof(*map) +
105 (dso->kernel ? sizeof(struct kmap) : 0)));
106 if (map != NULL) {
107 /*
108 * ->end will be filled after we load all the symbols
109 */
110 map__init(map, type, start, 0, 0, dso);
111 }
112
113 return map;
114}
115
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200116void map__delete(struct map *self)
117{
118 free(self);
119}
120
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200121void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200122{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200123 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200124 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200125 if (nd != NULL) {
126 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
127 self->start = sym->start;
128 }
129}
130
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200131void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200132{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200133 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200134 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200135 if (nd != NULL) {
136 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
137 self->end = sym->end;
138 }
139}
140
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200141#define DSO__DELETED "(deleted)"
142
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200143int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200144{
145 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500146 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200147
Masami Hiramatsua1281682009-12-15 10:32:33 -0500148 if (dso__loaded(self->dso, self->type))
149 return 0;
150
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200151 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200152 if (nr < 0) {
153 if (self->dso->has_build_id) {
154 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
155
156 build_id__sprintf(self->dso->build_id,
157 sizeof(self->dso->build_id),
158 sbuild_id);
159 pr_warning("%s with build id %s not found",
160 name, sbuild_id);
161 } else
162 pr_warning("Failed to open %s", name);
163
164 pr_warning(", continuing without symbols\n");
165 return -1;
166 } else if (nr == 0) {
Namhyung Kim29a0fc92012-09-28 18:31:59 +0900167#ifdef LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200168 const size_t len = strlen(name);
169 const size_t real_len = len - sizeof(DSO__DELETED);
170
171 if (len > sizeof(DSO__DELETED) &&
172 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600173 pr_warning("%.*s was updated (is prelink enabled?). "
174 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200175 (int)real_len, name);
176 } else {
177 pr_warning("no symbols found in %s, maybe install "
178 "a debug package?\n", name);
179 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900180#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200181 return -1;
182 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200183 /*
184 * Only applies to the kernel, as its symtabs aren't relative like the
185 * module ones.
186 */
187 if (self->dso->kernel)
188 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200189
190 return 0;
191}
192
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200193struct symbol *map__find_symbol(struct map *self, u64 addr,
194 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200195{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200196 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200197 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200198
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200199 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200200}
201
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200202struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
203 symbol_filter_t filter)
204{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200205 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200206 return NULL;
207
208 if (!dso__sorted_by_name(self->dso, self->type))
209 dso__sort_by_name(self->dso, self->type);
210
211 return dso__find_symbol_by_name(self->dso, self->type, name);
212}
213
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200214struct map *map__clone(struct map *self)
215{
216 struct map *map = malloc(sizeof(*self));
217
218 if (!map)
219 return NULL;
220
221 memcpy(map, self, sizeof(*self));
222
223 return map;
224}
225
226int map__overlap(struct map *l, struct map *r)
227{
228 if (l->start > r->start) {
229 struct map *t = l;
230 l = r;
231 r = t;
232 }
233
234 if (l->end > r->start)
235 return 1;
236
237 return 0;
238}
239
240size_t map__fprintf(struct map *self, FILE *fp)
241{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200242 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200243 self->start, self->end, self->pgoff, self->dso->name);
244}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200245
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900246size_t map__fprintf_dsoname(struct map *map, FILE *fp)
247{
Feng Tang8f28f192012-08-27 15:38:26 +0800248 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900249
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900250 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
251 if (symbol_conf.show_kernel_path && map->dso->long_name)
252 dsoname = map->dso->long_name;
253 else if (map->dso->name)
254 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800255 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900256
257 return fprintf(fp, "%s", dsoname);
258}
259
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200260/*
261 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
262 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
263 */
264u64 map__rip_2objdump(struct map *map, u64 rip)
265{
266 u64 addr = map->dso->adjust_symbols ?
267 map->unmap_ip(map, rip) : /* RIP -> IP */
268 rip;
269 return addr;
270}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200271
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300272void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300273{
274 int i;
275 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300276 mg->maps[i] = RB_ROOT;
277 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300278 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300279 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300280}
281
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300282static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300283{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300284 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300285
286 while (next) {
287 struct map *pos = rb_entry(next, struct map, rb_node);
288
289 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300290 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300291 map__delete(pos);
292 }
293}
294
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300295static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300296{
297 struct map *pos, *n;
298
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300299 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300300 list_del(&pos->node);
301 map__delete(pos);
302 }
303}
304
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300305void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300306{
307 int i;
308
309 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300310 maps__delete(&mg->maps[i]);
311 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300312 }
313}
314
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300315void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300316{
317 int type;
318
319 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300320 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300321 struct rb_node *next = rb_first(root);
322
323 while (next) {
324 struct map *pos = rb_entry(next, struct map, rb_node);
325 next = rb_next(&pos->rb_node);
326 rb_erase(&pos->rb_node, root);
327 /*
328 * We may have references to this map, for
329 * instance in some hist_entry instances, so
330 * just move them to a separate list.
331 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300332 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300333 }
334 }
335}
336
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300337struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300338 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300339 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300340 symbol_filter_t filter)
341{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300342 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300343
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300344 if (map != NULL) {
345 if (mapp != NULL)
346 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300347 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300348 }
349
350 return NULL;
351}
352
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300353struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300354 enum map_type type,
355 const char *name,
356 struct map **mapp,
357 symbol_filter_t filter)
358{
359 struct rb_node *nd;
360
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300361 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300362 struct map *pos = rb_entry(nd, struct map, rb_node);
363 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
364
365 if (sym == NULL)
366 continue;
367 if (mapp != NULL)
368 *mapp = pos;
369 return sym;
370 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300371
372 return NULL;
373}
374
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300375size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300376 enum map_type type, int verbose, FILE *fp)
377{
378 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
379 struct rb_node *nd;
380
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300381 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300382 struct map *pos = rb_entry(nd, struct map, rb_node);
383 printed += fprintf(fp, "Map:");
384 printed += map__fprintf(pos, fp);
385 if (verbose > 2) {
386 printed += dso__fprintf(pos->dso, type, fp);
387 printed += fprintf(fp, "--\n");
388 }
389 }
390
391 return printed;
392}
393
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300394size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300395{
396 size_t printed = 0, i;
397 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300398 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300399 return printed;
400}
401
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300402static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300403 enum map_type type,
404 int verbose, FILE *fp)
405{
406 struct map *pos;
407 size_t printed = 0;
408
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300409 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300410 printed += fprintf(fp, "Map:");
411 printed += map__fprintf(pos, fp);
412 if (verbose > 1) {
413 printed += dso__fprintf(pos->dso, type, fp);
414 printed += fprintf(fp, "--\n");
415 }
416 }
417 return printed;
418}
419
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300420static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300421 int verbose, FILE *fp)
422{
423 size_t printed = 0, i;
424 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300425 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300426 return printed;
427}
428
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300429size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300430{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300431 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300432 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300433 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300434}
435
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300436int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300437 int verbose, FILE *fp)
438{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300439 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300440 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300441 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300442
443 while (next) {
444 struct map *pos = rb_entry(next, struct map, rb_node);
445 next = rb_next(&pos->rb_node);
446
447 if (!map__overlap(pos, map))
448 continue;
449
450 if (verbose >= 2) {
451 fputs("overlapping maps:\n", fp);
452 map__fprintf(map, fp);
453 map__fprintf(pos, fp);
454 }
455
456 rb_erase(&pos->rb_node, root);
457 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300458 * Now check if we need to create new maps for areas not
459 * overlapped by the new map:
460 */
461 if (map->start > pos->start) {
462 struct map *before = map__clone(pos);
463
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300464 if (before == NULL) {
465 err = -ENOMEM;
466 goto move_map;
467 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300468
469 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300470 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300471 if (verbose >= 2)
472 map__fprintf(before, fp);
473 }
474
475 if (map->end < pos->end) {
476 struct map *after = map__clone(pos);
477
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300478 if (after == NULL) {
479 err = -ENOMEM;
480 goto move_map;
481 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300482
483 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300484 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300485 if (verbose >= 2)
486 map__fprintf(after, fp);
487 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300488move_map:
489 /*
490 * If we have references, just move them to a separate list.
491 */
492 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300493 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300494 else
495 map__delete(pos);
496
497 if (err)
498 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300499 }
500
501 return 0;
502}
503
504/*
505 * XXX This should not really _copy_ te maps, but refcount them.
506 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300507int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300508 struct map_groups *parent, enum map_type type)
509{
510 struct rb_node *nd;
511 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
512 struct map *map = rb_entry(nd, struct map, rb_node);
513 struct map *new = map__clone(map);
514 if (new == NULL)
515 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300516 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300517 }
518 return 0;
519}
520
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300521static u64 map__reloc_map_ip(struct map *map, u64 ip)
522{
523 return ip + (s64)map->pgoff;
524}
525
526static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
527{
528 return ip - (s64)map->pgoff;
529}
530
531void map__reloc_vmlinux(struct map *self)
532{
533 struct kmap *kmap = map__kmap(self);
534 s64 reloc;
535
536 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
537 return;
538
539 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
540 kmap->ref_reloc_sym->addr);
541
542 if (!reloc)
543 return;
544
545 self->map_ip = map__reloc_map_ip;
546 self->unmap_ip = map__reloc_unmap_ip;
547 self->pgoff = reloc;
548}
549
550void maps__insert(struct rb_root *maps, struct map *map)
551{
552 struct rb_node **p = &maps->rb_node;
553 struct rb_node *parent = NULL;
554 const u64 ip = map->start;
555 struct map *m;
556
557 while (*p != NULL) {
558 parent = *p;
559 m = rb_entry(parent, struct map, rb_node);
560 if (ip < m->start)
561 p = &(*p)->rb_left;
562 else
563 p = &(*p)->rb_right;
564 }
565
566 rb_link_node(&map->rb_node, parent, p);
567 rb_insert_color(&map->rb_node, maps);
568}
569
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300570void maps__remove(struct rb_root *self, struct map *map)
571{
572 rb_erase(&map->rb_node, self);
573}
574
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300575struct map *maps__find(struct rb_root *maps, u64 ip)
576{
577 struct rb_node **p = &maps->rb_node;
578 struct rb_node *parent = NULL;
579 struct map *m;
580
581 while (*p != NULL) {
582 parent = *p;
583 m = rb_entry(parent, struct map, rb_node);
584 if (ip < m->start)
585 p = &(*p)->rb_left;
586 else if (ip > m->end)
587 p = &(*p)->rb_right;
588 else
589 return m;
590 }
591
592 return NULL;
593}