blob: b442ee49452bc95b0e00e1f5f95fd16899362b12 [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"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020013
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030014const char *map_type__name[MAP__NR_TYPES] = {
15 [MAP__FUNCTION] = "Functions",
16 [MAP__VARIABLE] = "Variables",
17};
18
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020019static inline int is_anon_memory(const char *filename)
20{
21 return strcmp(filename, "//anon") == 0;
22}
23
Jiri Olsa87ffef72011-08-24 15:18:34 +020024static inline int is_no_dso_memory(const char *filename)
25{
26 return !strcmp(filename, "[stack]") ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020027 !strcmp(filename, "[heap]");
28}
29
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020030void map__init(struct map *self, enum map_type type,
31 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020032{
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020033 self->type = type;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020034 self->start = start;
35 self->end = end;
36 self->pgoff = pgoff;
37 self->dso = dso;
38 self->map_ip = map__map_ip;
39 self->unmap_ip = map__unmap_ip;
40 RB_CLEAR_NODE(&self->rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080041 self->groups = NULL;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -030042 self->referenced = false;
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -030043 self->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020044}
45
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080046struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
47 u64 pgoff, u32 pid, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010048 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020049{
50 struct map *self = malloc(sizeof(*self));
51
52 if (self != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020053 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020054 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020055 int anon, no_dso, vdso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020056
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020057 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020058 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020059 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020060
61 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030062 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020063 filename = newfilename;
64 }
65
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020066 if (vdso) {
67 pgoff = 0;
68 dso = vdso__dso_findnew(dsos__list);
69 } else
70 dso = __dsos__findnew(dsos__list, filename);
71
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020072 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020073 goto out_delete;
74
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030075 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020076
Jiri Olsa87ffef72011-08-24 15:18:34 +020077 if (anon || no_dso) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020078 self->map_ip = self->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020079
80 /*
81 * Set memory without DSO as loaded. All map__find_*
82 * functions still return NULL, and we avoid the
83 * unnecessary map__load warning.
84 */
85 if (no_dso)
86 dso__set_loaded(dso, self->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020087 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020088 }
89 return self;
90out_delete:
91 free(self);
92 return NULL;
93}
94
Namhyung Kime5a18452012-08-06 13:41:20 +090095/*
96 * Constructor variant for modules (where we know from /proc/modules where
97 * they are loaded) and for vmlinux, where only after we load all the
98 * symbols we'll know where it starts and ends.
99 */
100struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
101{
102 struct map *map = calloc(1, (sizeof(*map) +
103 (dso->kernel ? sizeof(struct kmap) : 0)));
104 if (map != NULL) {
105 /*
106 * ->end will be filled after we load all the symbols
107 */
108 map__init(map, type, start, 0, 0, dso);
109 }
110
111 return map;
112}
113
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200114void map__delete(struct map *self)
115{
116 free(self);
117}
118
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200119void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200120{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200121 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200122 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200123 if (nd != NULL) {
124 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
125 self->start = sym->start;
126 }
127}
128
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200129void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200130{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200131 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200132 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200133 if (nd != NULL) {
134 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
135 self->end = sym->end;
136 }
137}
138
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200139#define DSO__DELETED "(deleted)"
140
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200141int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200142{
143 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500144 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200145
Masami Hiramatsua1281682009-12-15 10:32:33 -0500146 if (dso__loaded(self->dso, self->type))
147 return 0;
148
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200149 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200150 if (nr < 0) {
151 if (self->dso->has_build_id) {
152 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
153
154 build_id__sprintf(self->dso->build_id,
155 sizeof(self->dso->build_id),
156 sbuild_id);
157 pr_warning("%s with build id %s not found",
158 name, sbuild_id);
159 } else
160 pr_warning("Failed to open %s", name);
161
162 pr_warning(", continuing without symbols\n");
163 return -1;
164 } else if (nr == 0) {
Namhyung Kim393be2e2012-08-06 13:41:21 +0900165#ifndef NO_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200166 const size_t len = strlen(name);
167 const size_t real_len = len - sizeof(DSO__DELETED);
168
169 if (len > sizeof(DSO__DELETED) &&
170 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600171 pr_warning("%.*s was updated (is prelink enabled?). "
172 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200173 (int)real_len, name);
174 } else {
175 pr_warning("no symbols found in %s, maybe install "
176 "a debug package?\n", name);
177 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900178#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200179 return -1;
180 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200181 /*
182 * Only applies to the kernel, as its symtabs aren't relative like the
183 * module ones.
184 */
185 if (self->dso->kernel)
186 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200187
188 return 0;
189}
190
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200191struct symbol *map__find_symbol(struct map *self, u64 addr,
192 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200193{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200194 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200195 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200196
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200197 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200198}
199
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200200struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
201 symbol_filter_t filter)
202{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200203 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200204 return NULL;
205
206 if (!dso__sorted_by_name(self->dso, self->type))
207 dso__sort_by_name(self->dso, self->type);
208
209 return dso__find_symbol_by_name(self->dso, self->type, name);
210}
211
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200212struct map *map__clone(struct map *self)
213{
214 struct map *map = malloc(sizeof(*self));
215
216 if (!map)
217 return NULL;
218
219 memcpy(map, self, sizeof(*self));
220
221 return map;
222}
223
224int map__overlap(struct map *l, struct map *r)
225{
226 if (l->start > r->start) {
227 struct map *t = l;
228 l = r;
229 r = t;
230 }
231
232 if (l->end > r->start)
233 return 1;
234
235 return 0;
236}
237
238size_t map__fprintf(struct map *self, FILE *fp)
239{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200240 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200241 self->start, self->end, self->pgoff, self->dso->name);
242}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200243
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900244size_t map__fprintf_dsoname(struct map *map, FILE *fp)
245{
246 const char *dsoname;
247
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900248 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
249 if (symbol_conf.show_kernel_path && map->dso->long_name)
250 dsoname = map->dso->long_name;
251 else if (map->dso->name)
252 dsoname = map->dso->name;
253 } else
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900254 dsoname = "[unknown]";
255
256 return fprintf(fp, "%s", dsoname);
257}
258
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200259/*
260 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
261 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
262 */
263u64 map__rip_2objdump(struct map *map, u64 rip)
264{
265 u64 addr = map->dso->adjust_symbols ?
266 map->unmap_ip(map, rip) : /* RIP -> IP */
267 rip;
268 return addr;
269}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200270
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300271void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300272{
273 int i;
274 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300275 mg->maps[i] = RB_ROOT;
276 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300277 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300278 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300279}
280
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300281static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300282{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300283 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300284
285 while (next) {
286 struct map *pos = rb_entry(next, struct map, rb_node);
287
288 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300289 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300290 map__delete(pos);
291 }
292}
293
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300294static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300295{
296 struct map *pos, *n;
297
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300298 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300299 list_del(&pos->node);
300 map__delete(pos);
301 }
302}
303
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300304void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300305{
306 int i;
307
308 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300309 maps__delete(&mg->maps[i]);
310 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300311 }
312}
313
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300314void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300315{
316 int type;
317
318 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300319 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300320 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 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300331 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300332 }
333 }
334}
335
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300336struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300337 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300338 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300339 symbol_filter_t filter)
340{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300341 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300342
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300343 if (map != NULL) {
344 if (mapp != NULL)
345 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300346 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300347 }
348
349 return NULL;
350}
351
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300352struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300353 enum map_type type,
354 const char *name,
355 struct map **mapp,
356 symbol_filter_t filter)
357{
358 struct rb_node *nd;
359
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300360 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300361 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 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300370
371 return NULL;
372}
373
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300374size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300375 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
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300380 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300381 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
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300393size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300394{
395 size_t printed = 0, i;
396 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300397 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300398 return printed;
399}
400
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300401static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300402 enum map_type type,
403 int verbose, FILE *fp)
404{
405 struct map *pos;
406 size_t printed = 0;
407
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300408 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300409 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
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300419static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300420 int verbose, FILE *fp)
421{
422 size_t printed = 0, i;
423 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300424 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300425 return printed;
426}
427
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300428size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300429{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300430 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300431 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300432 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300433}
434
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300435int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300436 int verbose, FILE *fp)
437{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300438 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300439 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300440 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300441
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);
456 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300457 * 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
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300463 if (before == NULL) {
464 err = -ENOMEM;
465 goto move_map;
466 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300467
468 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300469 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300470 if (verbose >= 2)
471 map__fprintf(before, fp);
472 }
473
474 if (map->end < pos->end) {
475 struct map *after = map__clone(pos);
476
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300477 if (after == NULL) {
478 err = -ENOMEM;
479 goto move_map;
480 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300481
482 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300483 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300484 if (verbose >= 2)
485 map__fprintf(after, fp);
486 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300487move_map:
488 /*
489 * If we have references, just move them to a separate list.
490 */
491 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300492 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300493 else
494 map__delete(pos);
495
496 if (err)
497 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300498 }
499
500 return 0;
501}
502
503/*
504 * XXX This should not really _copy_ te maps, but refcount them.
505 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300506int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300507 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;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300515 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300516 }
517 return 0;
518}
519
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300520static u64 map__reloc_map_ip(struct map *map, u64 ip)
521{
522 return ip + (s64)map->pgoff;
523}
524
525static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
526{
527 return ip - (s64)map->pgoff;
528}
529
530void map__reloc_vmlinux(struct map *self)
531{
532 struct kmap *kmap = map__kmap(self);
533 s64 reloc;
534
535 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
536 return;
537
538 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
539 kmap->ref_reloc_sym->addr);
540
541 if (!reloc)
542 return;
543
544 self->map_ip = map__reloc_map_ip;
545 self->unmap_ip = map__reloc_unmap_ip;
546 self->pgoff = reloc;
547}
548
549void maps__insert(struct rb_root *maps, struct map *map)
550{
551 struct rb_node **p = &maps->rb_node;
552 struct rb_node *parent = NULL;
553 const u64 ip = map->start;
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
562 p = &(*p)->rb_right;
563 }
564
565 rb_link_node(&map->rb_node, parent, p);
566 rb_insert_color(&map->rb_node, maps);
567}
568
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300569void maps__remove(struct rb_root *self, struct map *map)
570{
571 rb_erase(&map->rb_node, self);
572}
573
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300574struct map *maps__find(struct rb_root *maps, u64 ip)
575{
576 struct rb_node **p = &maps->rb_node;
577 struct rb_node *parent = NULL;
578 struct map *m;
579
580 while (*p != NULL) {
581 parent = *p;
582 m = rb_entry(parent, struct map, rb_node);
583 if (ip < m->start)
584 p = &(*p)->rb_left;
585 else if (ip > m->end)
586 p = &(*p)->rb_right;
587 else
588 return m;
589 }
590
591 return NULL;
592}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800593
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300594int machine__init(struct machine *self, const char *root_dir, pid_t pid)
595{
596 map_groups__init(&self->kmaps);
597 RB_CLEAR_NODE(&self->rb_node);
598 INIT_LIST_HEAD(&self->user_dsos);
599 INIT_LIST_HEAD(&self->kernel_dsos);
600
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200601 self->threads = RB_ROOT;
602 INIT_LIST_HEAD(&self->dead_threads);
603 self->last_match = NULL;
604
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300605 self->kmaps.machine = self;
606 self->pid = pid;
607 self->root_dir = strdup(root_dir);
David Ahern5cd95c22012-07-20 17:25:47 -0600608 if (self->root_dir == NULL)
609 return -ENOMEM;
610
611 if (pid != HOST_KERNEL_ID) {
612 struct thread *thread = machine__findnew_thread(self, pid);
613 char comm[64];
614
615 if (thread == NULL)
616 return -ENOMEM;
617
618 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
619 thread__set_comm(thread, comm);
620 }
621
622 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300623}
624
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300625static void dsos__delete(struct list_head *self)
626{
627 struct dso *pos, *n;
628
629 list_for_each_entry_safe(pos, n, self, node) {
630 list_del(&pos->node);
631 dso__delete(pos);
632 }
633}
634
635void machine__exit(struct machine *self)
636{
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300637 map_groups__exit(&self->kmaps);
638 dsos__delete(&self->user_dsos);
639 dsos__delete(&self->kernel_dsos);
640 free(self->root_dir);
641 self->root_dir = NULL;
642}
643
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300644void machine__delete(struct machine *self)
645{
646 machine__exit(self);
647 free(self);
648}
649
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300650struct machine *machines__add(struct rb_root *self, pid_t pid,
651 const char *root_dir)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800652{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300653 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800654 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300655 struct machine *pos, *machine = malloc(sizeof(*machine));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300657 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800658 return NULL;
659
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300660 if (machine__init(machine, root_dir, pid) != 0) {
661 free(machine);
662 return NULL;
663 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800664
665 while (*p != NULL) {
666 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300667 pos = rb_entry(parent, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800668 if (pid < pos->pid)
669 p = &(*p)->rb_left;
670 else
671 p = &(*p)->rb_right;
672 }
673
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300674 rb_link_node(&machine->rb_node, parent, p);
675 rb_insert_color(&machine->rb_node, self);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800676
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300677 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800678}
679
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300680struct machine *machines__find(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800681{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300682 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800683 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300684 struct machine *machine;
685 struct machine *default_machine = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800686
687 while (*p != NULL) {
688 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300689 machine = rb_entry(parent, struct machine, rb_node);
690 if (pid < machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800691 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300692 else if (pid > machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800693 p = &(*p)->rb_right;
694 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300695 return machine;
696 if (!machine->pid)
697 default_machine = machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800698 }
699
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300700 return default_machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800701}
702
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300703struct machine *machines__findnew(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800704{
705 char path[PATH_MAX];
David Ahern7ed97ad2012-07-02 09:12:57 -0600706 const char *root_dir = "";
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300707 struct machine *machine = machines__find(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800708
David Ahern7ed97ad2012-07-02 09:12:57 -0600709 if (machine && (machine->pid == pid))
710 goto out;
711
712 if ((pid != HOST_KERNEL_ID) &&
713 (pid != DEFAULT_GUEST_KERNEL_ID) &&
714 (symbol_conf.guestmount)) {
715 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
716 if (access(path, R_OK)) {
David Ahernc80c3c22012-07-20 17:25:51 -0600717 static struct strlist *seen;
718
719 if (!seen)
720 seen = strlist__new(true, NULL);
721
722 if (!strlist__has_entry(seen, path)) {
723 pr_err("Can't access file %s\n", path);
724 strlist__add(seen, path);
725 }
David Ahern7ed97ad2012-07-02 09:12:57 -0600726 machine = NULL;
727 goto out;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800728 }
David Ahern7ed97ad2012-07-02 09:12:57 -0600729 root_dir = path;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800730 }
731
David Ahern7ed97ad2012-07-02 09:12:57 -0600732 machine = machines__add(self, pid, root_dir);
733
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800734out:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300735 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800736}
737
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300738void machines__process(struct rb_root *self, machine__process_t process, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800739{
740 struct rb_node *nd;
741
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300742 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
743 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800744 process(pos, data);
745 }
746}
747
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300748char *machine__mmap_name(struct machine *self, char *bf, size_t size)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800749{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300750 if (machine__is_host(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300751 snprintf(bf, size, "[%s]", "kernel.kallsyms");
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300752 else if (machine__is_default_guest(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300753 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800754 else
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300755 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800756
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300757 return bf;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800758}
David Ahernadb5d2a2012-07-20 17:25:49 -0600759
760void machines__set_id_hdr_size(struct rb_root *machines, u16 id_hdr_size)
761{
762 struct rb_node *node;
763 struct machine *machine;
764
765 for (node = rb_first(machines); node; node = rb_next(node)) {
766 machine = rb_entry(node, struct machine, rb_node);
767 machine->id_hdr_size = id_hdr_size;
768 }
769
770 return;
771}