blob: 11079607105bb8e2c364ae0ee2f3ce9beb643992 [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"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020010
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030011const char *map_type__name[MAP__NR_TYPES] = {
12 [MAP__FUNCTION] = "Functions",
13 [MAP__VARIABLE] = "Variables",
14};
15
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020016static inline int is_anon_memory(const char *filename)
17{
18 return strcmp(filename, "//anon") == 0;
19}
20
Jiri Olsa87ffef72011-08-24 15:18:34 +020021static inline int is_no_dso_memory(const char *filename)
22{
23 return !strcmp(filename, "[stack]") ||
24 !strcmp(filename, "[vdso]") ||
25 !strcmp(filename, "[heap]");
26}
27
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020028void map__init(struct map *self, enum map_type type,
29 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020030{
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020031 self->type = type;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020032 self->start = start;
33 self->end = end;
34 self->pgoff = pgoff;
35 self->dso = dso;
36 self->map_ip = map__map_ip;
37 self->unmap_ip = map__unmap_ip;
38 RB_CLEAR_NODE(&self->rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080039 self->groups = NULL;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -030040 self->referenced = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020041}
42
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080043struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
44 u64 pgoff, u32 pid, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010045 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020046{
47 struct map *self = malloc(sizeof(*self));
48
49 if (self != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020050 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020051 struct dso *dso;
Jiri Olsa87ffef72011-08-24 15:18:34 +020052 int anon, no_dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020053
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020054 anon = is_anon_memory(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020055 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020056
57 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030058 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020059 filename = newfilename;
60 }
61
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080062 dso = __dsos__findnew(dsos__list, filename);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020063 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020064 goto out_delete;
65
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030066 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020067
Jiri Olsa87ffef72011-08-24 15:18:34 +020068 if (anon || no_dso) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020069 self->map_ip = self->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020070
71 /*
72 * Set memory without DSO as loaded. All map__find_*
73 * functions still return NULL, and we avoid the
74 * unnecessary map__load warning.
75 */
76 if (no_dso)
77 dso__set_loaded(dso, self->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020078 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020079 }
80 return self;
81out_delete:
82 free(self);
83 return NULL;
84}
85
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020086void map__delete(struct map *self)
87{
88 free(self);
89}
90
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020091void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020092{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020093 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020094 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020095 if (nd != NULL) {
96 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
97 self->start = sym->start;
98 }
99}
100
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200101void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200102{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200103 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200104 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200105 if (nd != NULL) {
106 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
107 self->end = sym->end;
108 }
109}
110
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200111#define DSO__DELETED "(deleted)"
112
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200113int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200114{
115 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500116 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200117
Masami Hiramatsua1281682009-12-15 10:32:33 -0500118 if (dso__loaded(self->dso, self->type))
119 return 0;
120
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200121 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200122 if (nr < 0) {
123 if (self->dso->has_build_id) {
124 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
125
126 build_id__sprintf(self->dso->build_id,
127 sizeof(self->dso->build_id),
128 sbuild_id);
129 pr_warning("%s with build id %s not found",
130 name, sbuild_id);
131 } else
132 pr_warning("Failed to open %s", name);
133
134 pr_warning(", continuing without symbols\n");
135 return -1;
136 } else if (nr == 0) {
137 const size_t len = strlen(name);
138 const size_t real_len = len - sizeof(DSO__DELETED);
139
140 if (len > sizeof(DSO__DELETED) &&
141 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600142 pr_warning("%.*s was updated (is prelink enabled?). "
143 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200144 (int)real_len, name);
145 } else {
146 pr_warning("no symbols found in %s, maybe install "
147 "a debug package?\n", name);
148 }
149
150 return -1;
151 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200152 /*
153 * Only applies to the kernel, as its symtabs aren't relative like the
154 * module ones.
155 */
156 if (self->dso->kernel)
157 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200158
159 return 0;
160}
161
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200162struct symbol *map__find_symbol(struct map *self, u64 addr,
163 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200164{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200165 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200166 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200167
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200168 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200169}
170
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200171struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
172 symbol_filter_t filter)
173{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200174 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200175 return NULL;
176
177 if (!dso__sorted_by_name(self->dso, self->type))
178 dso__sort_by_name(self->dso, self->type);
179
180 return dso__find_symbol_by_name(self->dso, self->type, name);
181}
182
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200183struct map *map__clone(struct map *self)
184{
185 struct map *map = malloc(sizeof(*self));
186
187 if (!map)
188 return NULL;
189
190 memcpy(map, self, sizeof(*self));
191
192 return map;
193}
194
195int map__overlap(struct map *l, struct map *r)
196{
197 if (l->start > r->start) {
198 struct map *t = l;
199 l = r;
200 r = t;
201 }
202
203 if (l->end > r->start)
204 return 1;
205
206 return 0;
207}
208
209size_t map__fprintf(struct map *self, FILE *fp)
210{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200211 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200212 self->start, self->end, self->pgoff, self->dso->name);
213}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200214
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900215size_t map__fprintf_dsoname(struct map *map, FILE *fp)
216{
217 const char *dsoname;
218
219 if (map && map->dso && map->dso->name)
220 dsoname = map->dso->name;
221 else
222 dsoname = "[unknown]";
223
224 return fprintf(fp, "%s", dsoname);
225}
226
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200227/*
228 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
229 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
230 */
231u64 map__rip_2objdump(struct map *map, u64 rip)
232{
233 u64 addr = map->dso->adjust_symbols ?
234 map->unmap_ip(map, rip) : /* RIP -> IP */
235 rip;
236 return addr;
237}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200238
239u64 map__objdump_2ip(struct map *map, u64 addr)
240{
241 u64 ip = map->dso->adjust_symbols ?
242 addr :
243 map->unmap_ip(map, addr); /* RIP -> IP */
244 return ip;
245}
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300246
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300247void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300248{
249 int i;
250 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300251 mg->maps[i] = RB_ROOT;
252 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300253 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300254 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300255}
256
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300257static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300258{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300259 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300260
261 while (next) {
262 struct map *pos = rb_entry(next, struct map, rb_node);
263
264 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300265 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300266 map__delete(pos);
267 }
268}
269
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300270static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300271{
272 struct map *pos, *n;
273
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300274 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300275 list_del(&pos->node);
276 map__delete(pos);
277 }
278}
279
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300280void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300281{
282 int i;
283
284 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300285 maps__delete(&mg->maps[i]);
286 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300287 }
288}
289
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300290void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300291{
292 int type;
293
294 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300295 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300296 struct rb_node *next = rb_first(root);
297
298 while (next) {
299 struct map *pos = rb_entry(next, struct map, rb_node);
300 next = rb_next(&pos->rb_node);
301 rb_erase(&pos->rb_node, root);
302 /*
303 * We may have references to this map, for
304 * instance in some hist_entry instances, so
305 * just move them to a separate list.
306 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300307 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300308 }
309 }
310}
311
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300312struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300313 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300314 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300315 symbol_filter_t filter)
316{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300317 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300318
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300319 if (map != NULL) {
320 if (mapp != NULL)
321 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300322 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300323 }
324
325 return NULL;
326}
327
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300328struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300329 enum map_type type,
330 const char *name,
331 struct map **mapp,
332 symbol_filter_t filter)
333{
334 struct rb_node *nd;
335
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300336 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300337 struct map *pos = rb_entry(nd, struct map, rb_node);
338 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
339
340 if (sym == NULL)
341 continue;
342 if (mapp != NULL)
343 *mapp = pos;
344 return sym;
345 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300346
347 return NULL;
348}
349
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300350size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300351 enum map_type type, int verbose, FILE *fp)
352{
353 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
354 struct rb_node *nd;
355
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300356 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300357 struct map *pos = rb_entry(nd, struct map, rb_node);
358 printed += fprintf(fp, "Map:");
359 printed += map__fprintf(pos, fp);
360 if (verbose > 2) {
361 printed += dso__fprintf(pos->dso, type, fp);
362 printed += fprintf(fp, "--\n");
363 }
364 }
365
366 return printed;
367}
368
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300369size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300370{
371 size_t printed = 0, i;
372 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300373 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300374 return printed;
375}
376
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300377static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300378 enum map_type type,
379 int verbose, FILE *fp)
380{
381 struct map *pos;
382 size_t printed = 0;
383
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300384 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300385 printed += fprintf(fp, "Map:");
386 printed += map__fprintf(pos, fp);
387 if (verbose > 1) {
388 printed += dso__fprintf(pos->dso, type, fp);
389 printed += fprintf(fp, "--\n");
390 }
391 }
392 return printed;
393}
394
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300395static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300396 int verbose, FILE *fp)
397{
398 size_t printed = 0, i;
399 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300400 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300401 return printed;
402}
403
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300404size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300405{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300406 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300407 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300408 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300409}
410
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300411int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300412 int verbose, FILE *fp)
413{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300414 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300415 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300416 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300417
418 while (next) {
419 struct map *pos = rb_entry(next, struct map, rb_node);
420 next = rb_next(&pos->rb_node);
421
422 if (!map__overlap(pos, map))
423 continue;
424
425 if (verbose >= 2) {
426 fputs("overlapping maps:\n", fp);
427 map__fprintf(map, fp);
428 map__fprintf(pos, fp);
429 }
430
431 rb_erase(&pos->rb_node, root);
432 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300433 * Now check if we need to create new maps for areas not
434 * overlapped by the new map:
435 */
436 if (map->start > pos->start) {
437 struct map *before = map__clone(pos);
438
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300439 if (before == NULL) {
440 err = -ENOMEM;
441 goto move_map;
442 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300443
444 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300445 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300446 if (verbose >= 2)
447 map__fprintf(before, fp);
448 }
449
450 if (map->end < pos->end) {
451 struct map *after = map__clone(pos);
452
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300453 if (after == NULL) {
454 err = -ENOMEM;
455 goto move_map;
456 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300457
458 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300459 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300460 if (verbose >= 2)
461 map__fprintf(after, fp);
462 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300463move_map:
464 /*
465 * If we have references, just move them to a separate list.
466 */
467 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300468 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300469 else
470 map__delete(pos);
471
472 if (err)
473 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300474 }
475
476 return 0;
477}
478
479/*
480 * XXX This should not really _copy_ te maps, but refcount them.
481 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300482int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300483 struct map_groups *parent, enum map_type type)
484{
485 struct rb_node *nd;
486 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
487 struct map *map = rb_entry(nd, struct map, rb_node);
488 struct map *new = map__clone(map);
489 if (new == NULL)
490 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300491 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300492 }
493 return 0;
494}
495
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300496static u64 map__reloc_map_ip(struct map *map, u64 ip)
497{
498 return ip + (s64)map->pgoff;
499}
500
501static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
502{
503 return ip - (s64)map->pgoff;
504}
505
506void map__reloc_vmlinux(struct map *self)
507{
508 struct kmap *kmap = map__kmap(self);
509 s64 reloc;
510
511 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
512 return;
513
514 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
515 kmap->ref_reloc_sym->addr);
516
517 if (!reloc)
518 return;
519
520 self->map_ip = map__reloc_map_ip;
521 self->unmap_ip = map__reloc_unmap_ip;
522 self->pgoff = reloc;
523}
524
525void maps__insert(struct rb_root *maps, struct map *map)
526{
527 struct rb_node **p = &maps->rb_node;
528 struct rb_node *parent = NULL;
529 const u64 ip = map->start;
530 struct map *m;
531
532 while (*p != NULL) {
533 parent = *p;
534 m = rb_entry(parent, struct map, rb_node);
535 if (ip < m->start)
536 p = &(*p)->rb_left;
537 else
538 p = &(*p)->rb_right;
539 }
540
541 rb_link_node(&map->rb_node, parent, p);
542 rb_insert_color(&map->rb_node, maps);
543}
544
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300545void maps__remove(struct rb_root *self, struct map *map)
546{
547 rb_erase(&map->rb_node, self);
548}
549
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300550struct map *maps__find(struct rb_root *maps, u64 ip)
551{
552 struct rb_node **p = &maps->rb_node;
553 struct rb_node *parent = NULL;
554 struct map *m;
555
556 while (*p != NULL) {
557 parent = *p;
558 m = rb_entry(parent, struct map, rb_node);
559 if (ip < m->start)
560 p = &(*p)->rb_left;
561 else if (ip > m->end)
562 p = &(*p)->rb_right;
563 else
564 return m;
565 }
566
567 return NULL;
568}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800569
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300570int machine__init(struct machine *self, const char *root_dir, pid_t pid)
571{
572 map_groups__init(&self->kmaps);
573 RB_CLEAR_NODE(&self->rb_node);
574 INIT_LIST_HEAD(&self->user_dsos);
575 INIT_LIST_HEAD(&self->kernel_dsos);
576
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200577 self->threads = RB_ROOT;
578 INIT_LIST_HEAD(&self->dead_threads);
579 self->last_match = NULL;
580
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300581 self->kmaps.machine = self;
582 self->pid = pid;
583 self->root_dir = strdup(root_dir);
584 return self->root_dir == NULL ? -ENOMEM : 0;
585}
586
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300587static void dsos__delete(struct list_head *self)
588{
589 struct dso *pos, *n;
590
591 list_for_each_entry_safe(pos, n, self, node) {
592 list_del(&pos->node);
593 dso__delete(pos);
594 }
595}
596
597void machine__exit(struct machine *self)
598{
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300599 map_groups__exit(&self->kmaps);
600 dsos__delete(&self->user_dsos);
601 dsos__delete(&self->kernel_dsos);
602 free(self->root_dir);
603 self->root_dir = NULL;
604}
605
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300606void machine__delete(struct machine *self)
607{
608 machine__exit(self);
609 free(self);
610}
611
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300612struct machine *machines__add(struct rb_root *self, pid_t pid,
613 const char *root_dir)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800614{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300615 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800616 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300617 struct machine *pos, *machine = malloc(sizeof(*machine));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800618
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300619 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800620 return NULL;
621
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300622 if (machine__init(machine, root_dir, pid) != 0) {
623 free(machine);
624 return NULL;
625 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800626
627 while (*p != NULL) {
628 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300629 pos = rb_entry(parent, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800630 if (pid < pos->pid)
631 p = &(*p)->rb_left;
632 else
633 p = &(*p)->rb_right;
634 }
635
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300636 rb_link_node(&machine->rb_node, parent, p);
637 rb_insert_color(&machine->rb_node, self);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800638
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300639 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800640}
641
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300642struct machine *machines__find(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800643{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300644 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800645 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300646 struct machine *machine;
647 struct machine *default_machine = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800648
649 while (*p != NULL) {
650 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300651 machine = rb_entry(parent, struct machine, rb_node);
652 if (pid < machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800653 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300654 else if (pid > machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800655 p = &(*p)->rb_right;
656 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300657 return machine;
658 if (!machine->pid)
659 default_machine = machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800660 }
661
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300662 return default_machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800663}
664
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300665struct machine *machines__findnew(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800666{
667 char path[PATH_MAX];
668 const char *root_dir;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300669 struct machine *machine = machines__find(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800670
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300671 if (!machine || machine->pid != pid) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800672 if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID)
673 root_dir = "";
674 else {
675 if (!symbol_conf.guestmount)
676 goto out;
677 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300678 if (access(path, R_OK)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800679 pr_err("Can't access file %s\n", path);
680 goto out;
681 }
682 root_dir = path;
683 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300684 machine = machines__add(self, pid, root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800685 }
686
687out:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300688 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800689}
690
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300691void machines__process(struct rb_root *self, machine__process_t process, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800692{
693 struct rb_node *nd;
694
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300695 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
696 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800697 process(pos, data);
698 }
699}
700
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300701char *machine__mmap_name(struct machine *self, char *bf, size_t size)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800702{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300703 if (machine__is_host(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300704 snprintf(bf, size, "[%s]", "kernel.kallsyms");
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300705 else if (machine__is_default_guest(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300706 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800707 else
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300708 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800709
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300710 return bf;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800711}