blob: e672f2fef65baaf62dd20d869bca33e027c1cfe6 [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
20static int strcommon(const char *pathname, char *cwd, int cwdlen)
21{
22 int n = 0;
23
24 while (n < cwdlen && pathname[n] == cwd[n])
25 ++n;
26
27 return n;
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 Meloafb7b4f2009-10-30 16:28:23 -020042}
43
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080044struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
45 u64 pgoff, u32 pid, char *filename,
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030046 enum map_type type, char *cwd, int cwdlen)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020047{
48 struct map *self = malloc(sizeof(*self));
49
50 if (self != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020051 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020052 struct dso *dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020053 int anon;
54
55 if (cwd) {
56 int n = strcommon(filename, cwd, cwdlen);
57
58 if (n == cwdlen) {
59 snprintf(newfilename, sizeof(newfilename),
60 ".%s", filename + n);
61 filename = newfilename;
62 }
63 }
64
65 anon = is_anon_memory(filename);
66
67 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030068 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020069 filename = newfilename;
70 }
71
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080072 dso = __dsos__findnew(dsos__list, filename);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020073 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020074 goto out_delete;
75
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030076 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020077
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020078 if (anon) {
79set_identity:
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020080 self->map_ip = self->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020081 } else if (strcmp(filename, "[vdso]") == 0) {
82 dso__set_loaded(dso, self->type);
83 goto set_identity;
84 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020085 }
86 return self;
87out_delete:
88 free(self);
89 return NULL;
90}
91
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020092void map__delete(struct map *self)
93{
94 free(self);
95}
96
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020097void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020098{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020099 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200100 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200101 if (nd != NULL) {
102 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
103 self->start = sym->start;
104 }
105}
106
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200107void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200108{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200109 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200110 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200111 if (nd != NULL) {
112 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
113 self->end = sym->end;
114 }
115}
116
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200117#define DSO__DELETED "(deleted)"
118
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200119int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200120{
121 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500122 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200123
Masami Hiramatsua1281682009-12-15 10:32:33 -0500124 if (dso__loaded(self->dso, self->type))
125 return 0;
126
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200127 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200128 if (nr < 0) {
129 if (self->dso->has_build_id) {
130 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
131
132 build_id__sprintf(self->dso->build_id,
133 sizeof(self->dso->build_id),
134 sbuild_id);
135 pr_warning("%s with build id %s not found",
136 name, sbuild_id);
137 } else
138 pr_warning("Failed to open %s", name);
139
140 pr_warning(", continuing without symbols\n");
141 return -1;
142 } else if (nr == 0) {
143 const size_t len = strlen(name);
144 const size_t real_len = len - sizeof(DSO__DELETED);
145
146 if (len > sizeof(DSO__DELETED) &&
147 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
148 pr_warning("%.*s was updated, restart the long "
149 "running apps that use it!\n",
150 (int)real_len, name);
151 } else {
152 pr_warning("no symbols found in %s, maybe install "
153 "a debug package?\n", name);
154 }
155
156 return -1;
157 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200158 /*
159 * Only applies to the kernel, as its symtabs aren't relative like the
160 * module ones.
161 */
162 if (self->dso->kernel)
163 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200164
165 return 0;
166}
167
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200168struct symbol *map__find_symbol(struct map *self, u64 addr,
169 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200170{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200171 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200172 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200173
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200174 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200175}
176
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200177struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
178 symbol_filter_t filter)
179{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200180 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200181 return NULL;
182
183 if (!dso__sorted_by_name(self->dso, self->type))
184 dso__sort_by_name(self->dso, self->type);
185
186 return dso__find_symbol_by_name(self->dso, self->type, name);
187}
188
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200189struct map *map__clone(struct map *self)
190{
191 struct map *map = malloc(sizeof(*self));
192
193 if (!map)
194 return NULL;
195
196 memcpy(map, self, sizeof(*self));
197
198 return map;
199}
200
201int map__overlap(struct map *l, struct map *r)
202{
203 if (l->start > r->start) {
204 struct map *t = l;
205 l = r;
206 r = t;
207 }
208
209 if (l->end > r->start)
210 return 1;
211
212 return 0;
213}
214
215size_t map__fprintf(struct map *self, FILE *fp)
216{
217 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
218 self->start, self->end, self->pgoff, self->dso->name);
219}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200220
221/*
222 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
223 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
224 */
225u64 map__rip_2objdump(struct map *map, u64 rip)
226{
227 u64 addr = map->dso->adjust_symbols ?
228 map->unmap_ip(map, rip) : /* RIP -> IP */
229 rip;
230 return addr;
231}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200232
233u64 map__objdump_2ip(struct map *map, u64 addr)
234{
235 u64 ip = map->dso->adjust_symbols ?
236 addr :
237 map->unmap_ip(map, addr); /* RIP -> IP */
238 return ip;
239}
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300240
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300241void map_groups__init(struct map_groups *self)
242{
243 int i;
244 for (i = 0; i < MAP__NR_TYPES; ++i) {
245 self->maps[i] = RB_ROOT;
246 INIT_LIST_HEAD(&self->removed_maps[i]);
247 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300248 self->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300249}
250
251void map_groups__flush(struct map_groups *self)
252{
253 int type;
254
255 for (type = 0; type < MAP__NR_TYPES; type++) {
256 struct rb_root *root = &self->maps[type];
257 struct rb_node *next = rb_first(root);
258
259 while (next) {
260 struct map *pos = rb_entry(next, struct map, rb_node);
261 next = rb_next(&pos->rb_node);
262 rb_erase(&pos->rb_node, root);
263 /*
264 * We may have references to this map, for
265 * instance in some hist_entry instances, so
266 * just move them to a separate list.
267 */
268 list_add_tail(&pos->node, &self->removed_maps[pos->type]);
269 }
270 }
271}
272
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300273struct symbol *map_groups__find_symbol(struct map_groups *self,
274 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300275 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300276 symbol_filter_t filter)
277{
278 struct map *map = map_groups__find(self, type, addr);
279
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300280 if (map != NULL) {
281 if (mapp != NULL)
282 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300283 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300284 }
285
286 return NULL;
287}
288
289struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
290 enum map_type type,
291 const char *name,
292 struct map **mapp,
293 symbol_filter_t filter)
294{
295 struct rb_node *nd;
296
297 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
298 struct map *pos = rb_entry(nd, struct map, rb_node);
299 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
300
301 if (sym == NULL)
302 continue;
303 if (mapp != NULL)
304 *mapp = pos;
305 return sym;
306 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300307
308 return NULL;
309}
310
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300311size_t __map_groups__fprintf_maps(struct map_groups *self,
312 enum map_type type, int verbose, FILE *fp)
313{
314 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
315 struct rb_node *nd;
316
317 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
318 struct map *pos = rb_entry(nd, struct map, rb_node);
319 printed += fprintf(fp, "Map:");
320 printed += map__fprintf(pos, fp);
321 if (verbose > 2) {
322 printed += dso__fprintf(pos->dso, type, fp);
323 printed += fprintf(fp, "--\n");
324 }
325 }
326
327 return printed;
328}
329
330size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp)
331{
332 size_t printed = 0, i;
333 for (i = 0; i < MAP__NR_TYPES; ++i)
334 printed += __map_groups__fprintf_maps(self, i, verbose, fp);
335 return printed;
336}
337
338static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
339 enum map_type type,
340 int verbose, FILE *fp)
341{
342 struct map *pos;
343 size_t printed = 0;
344
345 list_for_each_entry(pos, &self->removed_maps[type], node) {
346 printed += fprintf(fp, "Map:");
347 printed += map__fprintf(pos, fp);
348 if (verbose > 1) {
349 printed += dso__fprintf(pos->dso, type, fp);
350 printed += fprintf(fp, "--\n");
351 }
352 }
353 return printed;
354}
355
356static size_t map_groups__fprintf_removed_maps(struct map_groups *self,
357 int verbose, FILE *fp)
358{
359 size_t printed = 0, i;
360 for (i = 0; i < MAP__NR_TYPES; ++i)
361 printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp);
362 return printed;
363}
364
365size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp)
366{
367 size_t printed = map_groups__fprintf_maps(self, verbose, fp);
368 printed += fprintf(fp, "Removed maps:\n");
369 return printed + map_groups__fprintf_removed_maps(self, verbose, fp);
370}
371
372int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
373 int verbose, FILE *fp)
374{
375 struct rb_root *root = &self->maps[map->type];
376 struct rb_node *next = rb_first(root);
377
378 while (next) {
379 struct map *pos = rb_entry(next, struct map, rb_node);
380 next = rb_next(&pos->rb_node);
381
382 if (!map__overlap(pos, map))
383 continue;
384
385 if (verbose >= 2) {
386 fputs("overlapping maps:\n", fp);
387 map__fprintf(map, fp);
388 map__fprintf(pos, fp);
389 }
390
391 rb_erase(&pos->rb_node, root);
392 /*
393 * We may have references to this map, for instance in some
394 * hist_entry instances, so just move them to a separate
395 * list.
396 */
397 list_add_tail(&pos->node, &self->removed_maps[map->type]);
398 /*
399 * Now check if we need to create new maps for areas not
400 * overlapped by the new map:
401 */
402 if (map->start > pos->start) {
403 struct map *before = map__clone(pos);
404
405 if (before == NULL)
406 return -ENOMEM;
407
408 before->end = map->start - 1;
409 map_groups__insert(self, before);
410 if (verbose >= 2)
411 map__fprintf(before, fp);
412 }
413
414 if (map->end < pos->end) {
415 struct map *after = map__clone(pos);
416
417 if (after == NULL)
418 return -ENOMEM;
419
420 after->start = map->end + 1;
421 map_groups__insert(self, after);
422 if (verbose >= 2)
423 map__fprintf(after, fp);
424 }
425 }
426
427 return 0;
428}
429
430/*
431 * XXX This should not really _copy_ te maps, but refcount them.
432 */
433int map_groups__clone(struct map_groups *self,
434 struct map_groups *parent, enum map_type type)
435{
436 struct rb_node *nd;
437 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
438 struct map *map = rb_entry(nd, struct map, rb_node);
439 struct map *new = map__clone(map);
440 if (new == NULL)
441 return -ENOMEM;
442 map_groups__insert(self, new);
443 }
444 return 0;
445}
446
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300447static u64 map__reloc_map_ip(struct map *map, u64 ip)
448{
449 return ip + (s64)map->pgoff;
450}
451
452static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
453{
454 return ip - (s64)map->pgoff;
455}
456
457void map__reloc_vmlinux(struct map *self)
458{
459 struct kmap *kmap = map__kmap(self);
460 s64 reloc;
461
462 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
463 return;
464
465 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
466 kmap->ref_reloc_sym->addr);
467
468 if (!reloc)
469 return;
470
471 self->map_ip = map__reloc_map_ip;
472 self->unmap_ip = map__reloc_unmap_ip;
473 self->pgoff = reloc;
474}
475
476void maps__insert(struct rb_root *maps, struct map *map)
477{
478 struct rb_node **p = &maps->rb_node;
479 struct rb_node *parent = NULL;
480 const u64 ip = map->start;
481 struct map *m;
482
483 while (*p != NULL) {
484 parent = *p;
485 m = rb_entry(parent, struct map, rb_node);
486 if (ip < m->start)
487 p = &(*p)->rb_left;
488 else
489 p = &(*p)->rb_right;
490 }
491
492 rb_link_node(&map->rb_node, parent, p);
493 rb_insert_color(&map->rb_node, maps);
494}
495
496struct map *maps__find(struct rb_root *maps, u64 ip)
497{
498 struct rb_node **p = &maps->rb_node;
499 struct rb_node *parent = NULL;
500 struct map *m;
501
502 while (*p != NULL) {
503 parent = *p;
504 m = rb_entry(parent, struct map, rb_node);
505 if (ip < m->start)
506 p = &(*p)->rb_left;
507 else if (ip > m->end)
508 p = &(*p)->rb_right;
509 else
510 return m;
511 }
512
513 return NULL;
514}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800515
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300516int machine__init(struct machine *self, const char *root_dir, pid_t pid)
517{
518 map_groups__init(&self->kmaps);
519 RB_CLEAR_NODE(&self->rb_node);
520 INIT_LIST_HEAD(&self->user_dsos);
521 INIT_LIST_HEAD(&self->kernel_dsos);
522
523 self->kmaps.machine = self;
524 self->pid = pid;
525 self->root_dir = strdup(root_dir);
526 return self->root_dir == NULL ? -ENOMEM : 0;
527}
528
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300529struct machine *machines__add(struct rb_root *self, pid_t pid,
530 const char *root_dir)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800531{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300532 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800533 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300534 struct machine *pos, *machine = malloc(sizeof(*machine));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800535
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300536 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800537 return NULL;
538
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300539 if (machine__init(machine, root_dir, pid) != 0) {
540 free(machine);
541 return NULL;
542 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800543
544 while (*p != NULL) {
545 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300546 pos = rb_entry(parent, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800547 if (pid < pos->pid)
548 p = &(*p)->rb_left;
549 else
550 p = &(*p)->rb_right;
551 }
552
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300553 rb_link_node(&machine->rb_node, parent, p);
554 rb_insert_color(&machine->rb_node, self);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800555
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300556 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800557}
558
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300559struct machine *machines__find(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800560{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300561 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800562 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300563 struct machine *machine;
564 struct machine *default_machine = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800565
566 while (*p != NULL) {
567 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300568 machine = rb_entry(parent, struct machine, rb_node);
569 if (pid < machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800570 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300571 else if (pid > machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800572 p = &(*p)->rb_right;
573 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300574 return machine;
575 if (!machine->pid)
576 default_machine = machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800577 }
578
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300579 return default_machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800580}
581
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300582struct machine *machines__findnew(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800583{
584 char path[PATH_MAX];
585 const char *root_dir;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300586 struct machine *machine = machines__find(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800587
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300588 if (!machine || machine->pid != pid) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800589 if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID)
590 root_dir = "";
591 else {
592 if (!symbol_conf.guestmount)
593 goto out;
594 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300595 if (access(path, R_OK)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800596 pr_err("Can't access file %s\n", path);
597 goto out;
598 }
599 root_dir = path;
600 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300601 machine = machines__add(self, pid, root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800602 }
603
604out:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300605 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800606}
607
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300608void machines__process(struct rb_root *self, machine__process_t process, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800609{
610 struct rb_node *nd;
611
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300612 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
613 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800614 process(pos, data);
615 }
616}
617
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300618char *machine__mmap_name(struct machine *self, char *bf, size_t size)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800619{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300620 if (machine__is_host(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300621 snprintf(bf, size, "[%s]", "kernel.kallsyms");
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300622 else if (machine__is_default_guest(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300623 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800624 else
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300625 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800626
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300627 return bf;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800628}