blob: 3a7eb6ec0eecf2c13dcd2ae098f56b4020d70301 [file] [log] [blame]
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001#include "symbol.h"
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03002#include <errno.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03003#include <limits.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02004#include <stdlib.h>
5#include <string.h>
6#include <stdio.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08007#include <unistd.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03008#include "map.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02009
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030010const char *map_type__name[MAP__NR_TYPES] = {
11 [MAP__FUNCTION] = "Functions",
12 [MAP__VARIABLE] = "Variables",
13};
14
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020015static inline int is_anon_memory(const char *filename)
16{
17 return strcmp(filename, "//anon") == 0;
18}
19
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020020void map__init(struct map *self, enum map_type type,
21 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020022{
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020023 self->type = type;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020024 self->start = start;
25 self->end = end;
26 self->pgoff = pgoff;
27 self->dso = dso;
28 self->map_ip = map__map_ip;
29 self->unmap_ip = map__unmap_ip;
30 RB_CLEAR_NODE(&self->rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080031 self->groups = NULL;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -030032 self->referenced = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020033}
34
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080035struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
36 u64 pgoff, u32 pid, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010037 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020038{
39 struct map *self = malloc(sizeof(*self));
40
41 if (self != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020042 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020043 struct dso *dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020044 int anon;
45
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020046 anon = is_anon_memory(filename);
47
48 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030049 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020050 filename = newfilename;
51 }
52
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080053 dso = __dsos__findnew(dsos__list, filename);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020054 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020055 goto out_delete;
56
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030057 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020058
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020059 if (anon) {
60set_identity:
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020061 self->map_ip = self->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020062 } else if (strcmp(filename, "[vdso]") == 0) {
63 dso__set_loaded(dso, self->type);
64 goto set_identity;
65 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020066 }
67 return self;
68out_delete:
69 free(self);
70 return NULL;
71}
72
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020073void map__delete(struct map *self)
74{
75 free(self);
76}
77
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020078void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020079{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020080 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020081 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020082 if (nd != NULL) {
83 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
84 self->start = sym->start;
85 }
86}
87
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020088void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020089{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020090 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020091 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020092 if (nd != NULL) {
93 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
94 self->end = sym->end;
95 }
96}
97
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -020098#define DSO__DELETED "(deleted)"
99
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200100int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200101{
102 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500103 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200104
Masami Hiramatsua1281682009-12-15 10:32:33 -0500105 if (dso__loaded(self->dso, self->type))
106 return 0;
107
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200108 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200109 if (nr < 0) {
110 if (self->dso->has_build_id) {
111 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
112
113 build_id__sprintf(self->dso->build_id,
114 sizeof(self->dso->build_id),
115 sbuild_id);
116 pr_warning("%s with build id %s not found",
117 name, sbuild_id);
118 } else
119 pr_warning("Failed to open %s", name);
120
121 pr_warning(", continuing without symbols\n");
122 return -1;
123 } else if (nr == 0) {
124 const size_t len = strlen(name);
125 const size_t real_len = len - sizeof(DSO__DELETED);
126
127 if (len > sizeof(DSO__DELETED) &&
128 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
129 pr_warning("%.*s was updated, restart the long "
130 "running apps that use it!\n",
131 (int)real_len, name);
132 } else {
133 pr_warning("no symbols found in %s, maybe install "
134 "a debug package?\n", name);
135 }
136
137 return -1;
138 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200139 /*
140 * Only applies to the kernel, as its symtabs aren't relative like the
141 * module ones.
142 */
143 if (self->dso->kernel)
144 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200145
146 return 0;
147}
148
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200149struct symbol *map__find_symbol(struct map *self, u64 addr,
150 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200151{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200152 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200153 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200154
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200155 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200156}
157
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200158struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
159 symbol_filter_t filter)
160{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200161 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200162 return NULL;
163
164 if (!dso__sorted_by_name(self->dso, self->type))
165 dso__sort_by_name(self->dso, self->type);
166
167 return dso__find_symbol_by_name(self->dso, self->type, name);
168}
169
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200170struct map *map__clone(struct map *self)
171{
172 struct map *map = malloc(sizeof(*self));
173
174 if (!map)
175 return NULL;
176
177 memcpy(map, self, sizeof(*self));
178
179 return map;
180}
181
182int map__overlap(struct map *l, struct map *r)
183{
184 if (l->start > r->start) {
185 struct map *t = l;
186 l = r;
187 r = t;
188 }
189
190 if (l->end > r->start)
191 return 1;
192
193 return 0;
194}
195
196size_t map__fprintf(struct map *self, FILE *fp)
197{
198 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
199 self->start, self->end, self->pgoff, self->dso->name);
200}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200201
202/*
203 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
204 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
205 */
206u64 map__rip_2objdump(struct map *map, u64 rip)
207{
208 u64 addr = map->dso->adjust_symbols ?
209 map->unmap_ip(map, rip) : /* RIP -> IP */
210 rip;
211 return addr;
212}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200213
214u64 map__objdump_2ip(struct map *map, u64 addr)
215{
216 u64 ip = map->dso->adjust_symbols ?
217 addr :
218 map->unmap_ip(map, addr); /* RIP -> IP */
219 return ip;
220}
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300221
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300222void map_groups__init(struct map_groups *self)
223{
224 int i;
225 for (i = 0; i < MAP__NR_TYPES; ++i) {
226 self->maps[i] = RB_ROOT;
227 INIT_LIST_HEAD(&self->removed_maps[i]);
228 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300229 self->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300230}
231
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300232static void maps__delete(struct rb_root *self)
233{
234 struct rb_node *next = rb_first(self);
235
236 while (next) {
237 struct map *pos = rb_entry(next, struct map, rb_node);
238
239 next = rb_next(&pos->rb_node);
240 rb_erase(&pos->rb_node, self);
241 map__delete(pos);
242 }
243}
244
245static void maps__delete_removed(struct list_head *self)
246{
247 struct map *pos, *n;
248
249 list_for_each_entry_safe(pos, n, self, node) {
250 list_del(&pos->node);
251 map__delete(pos);
252 }
253}
254
255void map_groups__exit(struct map_groups *self)
256{
257 int i;
258
259 for (i = 0; i < MAP__NR_TYPES; ++i) {
260 maps__delete(&self->maps[i]);
261 maps__delete_removed(&self->removed_maps[i]);
262 }
263}
264
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300265void map_groups__flush(struct map_groups *self)
266{
267 int type;
268
269 for (type = 0; type < MAP__NR_TYPES; type++) {
270 struct rb_root *root = &self->maps[type];
271 struct rb_node *next = rb_first(root);
272
273 while (next) {
274 struct map *pos = rb_entry(next, struct map, rb_node);
275 next = rb_next(&pos->rb_node);
276 rb_erase(&pos->rb_node, root);
277 /*
278 * We may have references to this map, for
279 * instance in some hist_entry instances, so
280 * just move them to a separate list.
281 */
282 list_add_tail(&pos->node, &self->removed_maps[pos->type]);
283 }
284 }
285}
286
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300287struct symbol *map_groups__find_symbol(struct map_groups *self,
288 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300289 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300290 symbol_filter_t filter)
291{
292 struct map *map = map_groups__find(self, type, addr);
293
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300294 if (map != NULL) {
295 if (mapp != NULL)
296 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300297 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300298 }
299
300 return NULL;
301}
302
303struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
304 enum map_type type,
305 const char *name,
306 struct map **mapp,
307 symbol_filter_t filter)
308{
309 struct rb_node *nd;
310
311 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
312 struct map *pos = rb_entry(nd, struct map, rb_node);
313 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
314
315 if (sym == NULL)
316 continue;
317 if (mapp != NULL)
318 *mapp = pos;
319 return sym;
320 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300321
322 return NULL;
323}
324
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300325size_t __map_groups__fprintf_maps(struct map_groups *self,
326 enum map_type type, int verbose, FILE *fp)
327{
328 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
329 struct rb_node *nd;
330
331 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
332 struct map *pos = rb_entry(nd, struct map, rb_node);
333 printed += fprintf(fp, "Map:");
334 printed += map__fprintf(pos, fp);
335 if (verbose > 2) {
336 printed += dso__fprintf(pos->dso, type, fp);
337 printed += fprintf(fp, "--\n");
338 }
339 }
340
341 return printed;
342}
343
344size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp)
345{
346 size_t printed = 0, i;
347 for (i = 0; i < MAP__NR_TYPES; ++i)
348 printed += __map_groups__fprintf_maps(self, i, verbose, fp);
349 return printed;
350}
351
352static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
353 enum map_type type,
354 int verbose, FILE *fp)
355{
356 struct map *pos;
357 size_t printed = 0;
358
359 list_for_each_entry(pos, &self->removed_maps[type], node) {
360 printed += fprintf(fp, "Map:");
361 printed += map__fprintf(pos, fp);
362 if (verbose > 1) {
363 printed += dso__fprintf(pos->dso, type, fp);
364 printed += fprintf(fp, "--\n");
365 }
366 }
367 return printed;
368}
369
370static size_t map_groups__fprintf_removed_maps(struct map_groups *self,
371 int verbose, FILE *fp)
372{
373 size_t printed = 0, i;
374 for (i = 0; i < MAP__NR_TYPES; ++i)
375 printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp);
376 return printed;
377}
378
379size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp)
380{
381 size_t printed = map_groups__fprintf_maps(self, verbose, fp);
382 printed += fprintf(fp, "Removed maps:\n");
383 return printed + map_groups__fprintf_removed_maps(self, verbose, fp);
384}
385
386int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
387 int verbose, FILE *fp)
388{
389 struct rb_root *root = &self->maps[map->type];
390 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300391 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300392
393 while (next) {
394 struct map *pos = rb_entry(next, struct map, rb_node);
395 next = rb_next(&pos->rb_node);
396
397 if (!map__overlap(pos, map))
398 continue;
399
400 if (verbose >= 2) {
401 fputs("overlapping maps:\n", fp);
402 map__fprintf(map, fp);
403 map__fprintf(pos, fp);
404 }
405
406 rb_erase(&pos->rb_node, root);
407 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300408 * Now check if we need to create new maps for areas not
409 * overlapped by the new map:
410 */
411 if (map->start > pos->start) {
412 struct map *before = map__clone(pos);
413
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300414 if (before == NULL) {
415 err = -ENOMEM;
416 goto move_map;
417 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300418
419 before->end = map->start - 1;
420 map_groups__insert(self, before);
421 if (verbose >= 2)
422 map__fprintf(before, fp);
423 }
424
425 if (map->end < pos->end) {
426 struct map *after = map__clone(pos);
427
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300428 if (after == NULL) {
429 err = -ENOMEM;
430 goto move_map;
431 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300432
433 after->start = map->end + 1;
434 map_groups__insert(self, after);
435 if (verbose >= 2)
436 map__fprintf(after, fp);
437 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300438move_map:
439 /*
440 * If we have references, just move them to a separate list.
441 */
442 if (pos->referenced)
443 list_add_tail(&pos->node, &self->removed_maps[map->type]);
444 else
445 map__delete(pos);
446
447 if (err)
448 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300449 }
450
451 return 0;
452}
453
454/*
455 * XXX This should not really _copy_ te maps, but refcount them.
456 */
457int map_groups__clone(struct map_groups *self,
458 struct map_groups *parent, enum map_type type)
459{
460 struct rb_node *nd;
461 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
462 struct map *map = rb_entry(nd, struct map, rb_node);
463 struct map *new = map__clone(map);
464 if (new == NULL)
465 return -ENOMEM;
466 map_groups__insert(self, new);
467 }
468 return 0;
469}
470
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300471static u64 map__reloc_map_ip(struct map *map, u64 ip)
472{
473 return ip + (s64)map->pgoff;
474}
475
476static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
477{
478 return ip - (s64)map->pgoff;
479}
480
481void map__reloc_vmlinux(struct map *self)
482{
483 struct kmap *kmap = map__kmap(self);
484 s64 reloc;
485
486 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
487 return;
488
489 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
490 kmap->ref_reloc_sym->addr);
491
492 if (!reloc)
493 return;
494
495 self->map_ip = map__reloc_map_ip;
496 self->unmap_ip = map__reloc_unmap_ip;
497 self->pgoff = reloc;
498}
499
500void maps__insert(struct rb_root *maps, struct map *map)
501{
502 struct rb_node **p = &maps->rb_node;
503 struct rb_node *parent = NULL;
504 const u64 ip = map->start;
505 struct map *m;
506
507 while (*p != NULL) {
508 parent = *p;
509 m = rb_entry(parent, struct map, rb_node);
510 if (ip < m->start)
511 p = &(*p)->rb_left;
512 else
513 p = &(*p)->rb_right;
514 }
515
516 rb_link_node(&map->rb_node, parent, p);
517 rb_insert_color(&map->rb_node, maps);
518}
519
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300520void maps__remove(struct rb_root *self, struct map *map)
521{
522 rb_erase(&map->rb_node, self);
523}
524
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300525struct map *maps__find(struct rb_root *maps, u64 ip)
526{
527 struct rb_node **p = &maps->rb_node;
528 struct rb_node *parent = NULL;
529 struct map *m;
530
531 while (*p != NULL) {
532 parent = *p;
533 m = rb_entry(parent, struct map, rb_node);
534 if (ip < m->start)
535 p = &(*p)->rb_left;
536 else if (ip > m->end)
537 p = &(*p)->rb_right;
538 else
539 return m;
540 }
541
542 return NULL;
543}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800544
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300545int machine__init(struct machine *self, const char *root_dir, pid_t pid)
546{
547 map_groups__init(&self->kmaps);
548 RB_CLEAR_NODE(&self->rb_node);
549 INIT_LIST_HEAD(&self->user_dsos);
550 INIT_LIST_HEAD(&self->kernel_dsos);
551
552 self->kmaps.machine = self;
553 self->pid = pid;
554 self->root_dir = strdup(root_dir);
555 return self->root_dir == NULL ? -ENOMEM : 0;
556}
557
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300558static void dsos__delete(struct list_head *self)
559{
560 struct dso *pos, *n;
561
562 list_for_each_entry_safe(pos, n, self, node) {
563 list_del(&pos->node);
564 dso__delete(pos);
565 }
566}
567
568void machine__exit(struct machine *self)
569{
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300570 map_groups__exit(&self->kmaps);
571 dsos__delete(&self->user_dsos);
572 dsos__delete(&self->kernel_dsos);
573 free(self->root_dir);
574 self->root_dir = NULL;
575}
576
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300577void machine__delete(struct machine *self)
578{
579 machine__exit(self);
580 free(self);
581}
582
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300583struct machine *machines__add(struct rb_root *self, pid_t pid,
584 const char *root_dir)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800585{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300586 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800587 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300588 struct machine *pos, *machine = malloc(sizeof(*machine));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800589
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300590 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800591 return NULL;
592
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300593 if (machine__init(machine, root_dir, pid) != 0) {
594 free(machine);
595 return NULL;
596 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800597
598 while (*p != NULL) {
599 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300600 pos = rb_entry(parent, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800601 if (pid < pos->pid)
602 p = &(*p)->rb_left;
603 else
604 p = &(*p)->rb_right;
605 }
606
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300607 rb_link_node(&machine->rb_node, parent, p);
608 rb_insert_color(&machine->rb_node, self);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800609
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300610 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800611}
612
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300613struct machine *machines__find(struct rb_root *self, pid_t pid)
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 *machine;
618 struct machine *default_machine = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800619
620 while (*p != NULL) {
621 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300622 machine = rb_entry(parent, struct machine, rb_node);
623 if (pid < machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800624 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300625 else if (pid > machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800626 p = &(*p)->rb_right;
627 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300628 return machine;
629 if (!machine->pid)
630 default_machine = machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800631 }
632
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300633 return default_machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800634}
635
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300636struct machine *machines__findnew(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800637{
638 char path[PATH_MAX];
639 const char *root_dir;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300640 struct machine *machine = machines__find(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800641
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300642 if (!machine || machine->pid != pid) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800643 if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID)
644 root_dir = "";
645 else {
646 if (!symbol_conf.guestmount)
647 goto out;
648 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300649 if (access(path, R_OK)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800650 pr_err("Can't access file %s\n", path);
651 goto out;
652 }
653 root_dir = path;
654 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300655 machine = machines__add(self, pid, root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656 }
657
658out:
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 -0300662void machines__process(struct rb_root *self, machine__process_t process, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800663{
664 struct rb_node *nd;
665
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300666 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
667 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800668 process(pos, data);
669 }
670}
671
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300672char *machine__mmap_name(struct machine *self, char *bf, size_t size)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800673{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300674 if (machine__is_host(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300675 snprintf(bf, size, "[%s]", "kernel.kallsyms");
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300676 else if (machine__is_default_guest(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300677 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800678 else
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300679 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800680
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300681 return bf;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800682}