blob: cc33486ad9e25b80e8486a4c60fce41d03a60b15 [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"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020012
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030013const char *map_type__name[MAP__NR_TYPES] = {
14 [MAP__FUNCTION] = "Functions",
15 [MAP__VARIABLE] = "Variables",
16};
17
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020018static inline int is_anon_memory(const char *filename)
19{
20 return strcmp(filename, "//anon") == 0;
21}
22
Jiri Olsa87ffef72011-08-24 15:18:34 +020023static inline int is_no_dso_memory(const char *filename)
24{
25 return !strcmp(filename, "[stack]") ||
26 !strcmp(filename, "[vdso]") ||
27 !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 Olsa87ffef72011-08-24 15:18:34 +020055 int anon, no_dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020056
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020057 anon = is_anon_memory(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020058 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020059
60 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030061 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020062 filename = newfilename;
63 }
64
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080065 dso = __dsos__findnew(dsos__list, filename);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020066 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020067 goto out_delete;
68
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030069 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020070
Jiri Olsa87ffef72011-08-24 15:18:34 +020071 if (anon || no_dso) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020072 self->map_ip = self->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020073
74 /*
75 * Set memory without DSO as loaded. All map__find_*
76 * functions still return NULL, and we avoid the
77 * unnecessary map__load warning.
78 */
79 if (no_dso)
80 dso__set_loaded(dso, self->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020081 }
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) {
David Aherne77b15b2011-10-18 18:44:45 -0600145 pr_warning("%.*s was updated (is prelink enabled?). "
146 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200147 (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{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200214 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200215 self->start, self->end, self->pgoff, self->dso->name);
216}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200217
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900218size_t map__fprintf_dsoname(struct map *map, FILE *fp)
219{
220 const char *dsoname;
221
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900222 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
223 if (symbol_conf.show_kernel_path && map->dso->long_name)
224 dsoname = map->dso->long_name;
225 else if (map->dso->name)
226 dsoname = map->dso->name;
227 } else
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900228 dsoname = "[unknown]";
229
230 return fprintf(fp, "%s", dsoname);
231}
232
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200233/*
234 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
235 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
236 */
237u64 map__rip_2objdump(struct map *map, u64 rip)
238{
239 u64 addr = map->dso->adjust_symbols ?
240 map->unmap_ip(map, rip) : /* RIP -> IP */
241 rip;
242 return addr;
243}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200244
245u64 map__objdump_2ip(struct map *map, u64 addr)
246{
247 u64 ip = map->dso->adjust_symbols ?
248 addr :
249 map->unmap_ip(map, addr); /* RIP -> IP */
250 return ip;
251}
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300252
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300253void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300254{
255 int i;
256 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300257 mg->maps[i] = RB_ROOT;
258 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300259 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300260 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300261}
262
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300263static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300264{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300265 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300266
267 while (next) {
268 struct map *pos = rb_entry(next, struct map, rb_node);
269
270 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300271 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300272 map__delete(pos);
273 }
274}
275
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300276static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300277{
278 struct map *pos, *n;
279
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300280 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300281 list_del(&pos->node);
282 map__delete(pos);
283 }
284}
285
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300286void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300287{
288 int i;
289
290 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300291 maps__delete(&mg->maps[i]);
292 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300293 }
294}
295
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300296void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300297{
298 int type;
299
300 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300301 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300302 struct rb_node *next = rb_first(root);
303
304 while (next) {
305 struct map *pos = rb_entry(next, struct map, rb_node);
306 next = rb_next(&pos->rb_node);
307 rb_erase(&pos->rb_node, root);
308 /*
309 * We may have references to this map, for
310 * instance in some hist_entry instances, so
311 * just move them to a separate list.
312 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300313 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300314 }
315 }
316}
317
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300318struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300319 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300320 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300321 symbol_filter_t filter)
322{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300323 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300324
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300325 if (map != NULL) {
326 if (mapp != NULL)
327 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300328 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300329 }
330
331 return NULL;
332}
333
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300334struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300335 enum map_type type,
336 const char *name,
337 struct map **mapp,
338 symbol_filter_t filter)
339{
340 struct rb_node *nd;
341
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300342 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300343 struct map *pos = rb_entry(nd, struct map, rb_node);
344 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
345
346 if (sym == NULL)
347 continue;
348 if (mapp != NULL)
349 *mapp = pos;
350 return sym;
351 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300352
353 return NULL;
354}
355
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300356size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300357 enum map_type type, int verbose, FILE *fp)
358{
359 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
360 struct rb_node *nd;
361
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300362 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300363 struct map *pos = rb_entry(nd, struct map, rb_node);
364 printed += fprintf(fp, "Map:");
365 printed += map__fprintf(pos, fp);
366 if (verbose > 2) {
367 printed += dso__fprintf(pos->dso, type, fp);
368 printed += fprintf(fp, "--\n");
369 }
370 }
371
372 return printed;
373}
374
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300375size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300376{
377 size_t printed = 0, i;
378 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300379 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300380 return printed;
381}
382
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300383static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300384 enum map_type type,
385 int verbose, FILE *fp)
386{
387 struct map *pos;
388 size_t printed = 0;
389
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300390 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300391 printed += fprintf(fp, "Map:");
392 printed += map__fprintf(pos, fp);
393 if (verbose > 1) {
394 printed += dso__fprintf(pos->dso, type, fp);
395 printed += fprintf(fp, "--\n");
396 }
397 }
398 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 int verbose, FILE *fp)
403{
404 size_t printed = 0, i;
405 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300406 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300407 return printed;
408}
409
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300410size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300411{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300412 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300413 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300414 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300415}
416
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300417int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300418 int verbose, FILE *fp)
419{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300420 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300421 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300422 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300423
424 while (next) {
425 struct map *pos = rb_entry(next, struct map, rb_node);
426 next = rb_next(&pos->rb_node);
427
428 if (!map__overlap(pos, map))
429 continue;
430
431 if (verbose >= 2) {
432 fputs("overlapping maps:\n", fp);
433 map__fprintf(map, fp);
434 map__fprintf(pos, fp);
435 }
436
437 rb_erase(&pos->rb_node, root);
438 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300439 * Now check if we need to create new maps for areas not
440 * overlapped by the new map:
441 */
442 if (map->start > pos->start) {
443 struct map *before = map__clone(pos);
444
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300445 if (before == NULL) {
446 err = -ENOMEM;
447 goto move_map;
448 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300449
450 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300451 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300452 if (verbose >= 2)
453 map__fprintf(before, fp);
454 }
455
456 if (map->end < pos->end) {
457 struct map *after = map__clone(pos);
458
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300459 if (after == NULL) {
460 err = -ENOMEM;
461 goto move_map;
462 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300463
464 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300465 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300466 if (verbose >= 2)
467 map__fprintf(after, fp);
468 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300469move_map:
470 /*
471 * If we have references, just move them to a separate list.
472 */
473 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300474 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300475 else
476 map__delete(pos);
477
478 if (err)
479 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300480 }
481
482 return 0;
483}
484
485/*
486 * XXX This should not really _copy_ te maps, but refcount them.
487 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300488int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300489 struct map_groups *parent, enum map_type type)
490{
491 struct rb_node *nd;
492 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
493 struct map *map = rb_entry(nd, struct map, rb_node);
494 struct map *new = map__clone(map);
495 if (new == NULL)
496 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300497 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300498 }
499 return 0;
500}
501
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300502static u64 map__reloc_map_ip(struct map *map, u64 ip)
503{
504 return ip + (s64)map->pgoff;
505}
506
507static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
508{
509 return ip - (s64)map->pgoff;
510}
511
512void map__reloc_vmlinux(struct map *self)
513{
514 struct kmap *kmap = map__kmap(self);
515 s64 reloc;
516
517 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
518 return;
519
520 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
521 kmap->ref_reloc_sym->addr);
522
523 if (!reloc)
524 return;
525
526 self->map_ip = map__reloc_map_ip;
527 self->unmap_ip = map__reloc_unmap_ip;
528 self->pgoff = reloc;
529}
530
531void maps__insert(struct rb_root *maps, struct map *map)
532{
533 struct rb_node **p = &maps->rb_node;
534 struct rb_node *parent = NULL;
535 const u64 ip = map->start;
536 struct map *m;
537
538 while (*p != NULL) {
539 parent = *p;
540 m = rb_entry(parent, struct map, rb_node);
541 if (ip < m->start)
542 p = &(*p)->rb_left;
543 else
544 p = &(*p)->rb_right;
545 }
546
547 rb_link_node(&map->rb_node, parent, p);
548 rb_insert_color(&map->rb_node, maps);
549}
550
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300551void maps__remove(struct rb_root *self, struct map *map)
552{
553 rb_erase(&map->rb_node, self);
554}
555
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300556struct map *maps__find(struct rb_root *maps, u64 ip)
557{
558 struct rb_node **p = &maps->rb_node;
559 struct rb_node *parent = NULL;
560 struct map *m;
561
562 while (*p != NULL) {
563 parent = *p;
564 m = rb_entry(parent, struct map, rb_node);
565 if (ip < m->start)
566 p = &(*p)->rb_left;
567 else if (ip > m->end)
568 p = &(*p)->rb_right;
569 else
570 return m;
571 }
572
573 return NULL;
574}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800575
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300576int machine__init(struct machine *self, const char *root_dir, pid_t pid)
577{
578 map_groups__init(&self->kmaps);
579 RB_CLEAR_NODE(&self->rb_node);
580 INIT_LIST_HEAD(&self->user_dsos);
581 INIT_LIST_HEAD(&self->kernel_dsos);
582
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200583 self->threads = RB_ROOT;
584 INIT_LIST_HEAD(&self->dead_threads);
585 self->last_match = NULL;
586
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300587 self->kmaps.machine = self;
588 self->pid = pid;
589 self->root_dir = strdup(root_dir);
David Ahern5cd95c22012-07-20 17:25:47 -0600590 if (self->root_dir == NULL)
591 return -ENOMEM;
592
593 if (pid != HOST_KERNEL_ID) {
594 struct thread *thread = machine__findnew_thread(self, pid);
595 char comm[64];
596
597 if (thread == NULL)
598 return -ENOMEM;
599
600 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
601 thread__set_comm(thread, comm);
602 }
603
604 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300605}
606
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300607static void dsos__delete(struct list_head *self)
608{
609 struct dso *pos, *n;
610
611 list_for_each_entry_safe(pos, n, self, node) {
612 list_del(&pos->node);
613 dso__delete(pos);
614 }
615}
616
617void machine__exit(struct machine *self)
618{
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300619 map_groups__exit(&self->kmaps);
620 dsos__delete(&self->user_dsos);
621 dsos__delete(&self->kernel_dsos);
622 free(self->root_dir);
623 self->root_dir = NULL;
624}
625
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300626void machine__delete(struct machine *self)
627{
628 machine__exit(self);
629 free(self);
630}
631
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300632struct machine *machines__add(struct rb_root *self, pid_t pid,
633 const char *root_dir)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800634{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300635 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800636 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300637 struct machine *pos, *machine = malloc(sizeof(*machine));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800638
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300639 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800640 return NULL;
641
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300642 if (machine__init(machine, root_dir, pid) != 0) {
643 free(machine);
644 return NULL;
645 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800646
647 while (*p != NULL) {
648 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300649 pos = rb_entry(parent, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800650 if (pid < pos->pid)
651 p = &(*p)->rb_left;
652 else
653 p = &(*p)->rb_right;
654 }
655
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300656 rb_link_node(&machine->rb_node, parent, p);
657 rb_insert_color(&machine->rb_node, self);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800658
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300659 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800660}
661
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300662struct machine *machines__find(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800663{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300664 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800665 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300666 struct machine *machine;
667 struct machine *default_machine = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800668
669 while (*p != NULL) {
670 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300671 machine = rb_entry(parent, struct machine, rb_node);
672 if (pid < machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800673 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300674 else if (pid > machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800675 p = &(*p)->rb_right;
676 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300677 return machine;
678 if (!machine->pid)
679 default_machine = machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800680 }
681
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300682 return default_machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800683}
684
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300685struct machine *machines__findnew(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800686{
687 char path[PATH_MAX];
David Ahern7ed97ad2012-07-02 09:12:57 -0600688 const char *root_dir = "";
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300689 struct machine *machine = machines__find(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800690
David Ahern7ed97ad2012-07-02 09:12:57 -0600691 if (machine && (machine->pid == pid))
692 goto out;
693
694 if ((pid != HOST_KERNEL_ID) &&
695 (pid != DEFAULT_GUEST_KERNEL_ID) &&
696 (symbol_conf.guestmount)) {
697 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
698 if (access(path, R_OK)) {
David Ahernc80c3c22012-07-20 17:25:51 -0600699 static struct strlist *seen;
700
701 if (!seen)
702 seen = strlist__new(true, NULL);
703
704 if (!strlist__has_entry(seen, path)) {
705 pr_err("Can't access file %s\n", path);
706 strlist__add(seen, path);
707 }
David Ahern7ed97ad2012-07-02 09:12:57 -0600708 machine = NULL;
709 goto out;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800710 }
David Ahern7ed97ad2012-07-02 09:12:57 -0600711 root_dir = path;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800712 }
713
David Ahern7ed97ad2012-07-02 09:12:57 -0600714 machine = machines__add(self, pid, root_dir);
715
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800716out:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300717 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800718}
719
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300720void machines__process(struct rb_root *self, machine__process_t process, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800721{
722 struct rb_node *nd;
723
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300724 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
725 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800726 process(pos, data);
727 }
728}
729
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300730char *machine__mmap_name(struct machine *self, char *bf, size_t size)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800731{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300732 if (machine__is_host(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300733 snprintf(bf, size, "[%s]", "kernel.kallsyms");
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300734 else if (machine__is_default_guest(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300735 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800736 else
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300737 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800738
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300739 return bf;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800740}
David Ahernadb5d2a2012-07-20 17:25:49 -0600741
742void machines__set_id_hdr_size(struct rb_root *machines, u16 id_hdr_size)
743{
744 struct rb_node *node;
745 struct machine *machine;
746
747 for (node = rb_first(machines); node; node = rb_next(node)) {
748 machine = rb_entry(node, struct machine, rb_node);
749 machine->id_hdr_size = id_hdr_size;
750 }
751
752 return;
753}