blob: 2ddbae319de592889fa63c2e32bd06f8f7a0621e [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 Meloafb7b4f2009-10-30 16:28:23 -020032}
33
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080034struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
35 u64 pgoff, u32 pid, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010036 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020037{
38 struct map *self = malloc(sizeof(*self));
39
40 if (self != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020041 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020042 struct dso *dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020043 int anon;
44
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020045 anon = is_anon_memory(filename);
46
47 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030048 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020049 filename = newfilename;
50 }
51
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080052 dso = __dsos__findnew(dsos__list, filename);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020053 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020054 goto out_delete;
55
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030056 map__init(self, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020057
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020058 if (anon) {
59set_identity:
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020060 self->map_ip = self->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020061 } else if (strcmp(filename, "[vdso]") == 0) {
62 dso__set_loaded(dso, self->type);
63 goto set_identity;
64 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020065 }
66 return self;
67out_delete:
68 free(self);
69 return NULL;
70}
71
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020072void map__delete(struct map *self)
73{
74 free(self);
75}
76
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020077void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020078{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020079 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020080 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020081 if (nd != NULL) {
82 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
83 self->start = sym->start;
84 }
85}
86
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020087void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020088{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020089 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020090 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020091 if (nd != NULL) {
92 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
93 self->end = sym->end;
94 }
95}
96
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -020097#define DSO__DELETED "(deleted)"
98
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020099int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200100{
101 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500102 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200103
Masami Hiramatsua1281682009-12-15 10:32:33 -0500104 if (dso__loaded(self->dso, self->type))
105 return 0;
106
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200107 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200108 if (nr < 0) {
109 if (self->dso->has_build_id) {
110 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
111
112 build_id__sprintf(self->dso->build_id,
113 sizeof(self->dso->build_id),
114 sbuild_id);
115 pr_warning("%s with build id %s not found",
116 name, sbuild_id);
117 } else
118 pr_warning("Failed to open %s", name);
119
120 pr_warning(", continuing without symbols\n");
121 return -1;
122 } else if (nr == 0) {
123 const size_t len = strlen(name);
124 const size_t real_len = len - sizeof(DSO__DELETED);
125
126 if (len > sizeof(DSO__DELETED) &&
127 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
128 pr_warning("%.*s was updated, restart the long "
129 "running apps that use it!\n",
130 (int)real_len, name);
131 } else {
132 pr_warning("no symbols found in %s, maybe install "
133 "a debug package?\n", name);
134 }
135
136 return -1;
137 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200138 /*
139 * Only applies to the kernel, as its symtabs aren't relative like the
140 * module ones.
141 */
142 if (self->dso->kernel)
143 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200144
145 return 0;
146}
147
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200148struct symbol *map__find_symbol(struct map *self, u64 addr,
149 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200150{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200151 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200152 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200153
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200154 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200155}
156
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200157struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
158 symbol_filter_t filter)
159{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200160 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200161 return NULL;
162
163 if (!dso__sorted_by_name(self->dso, self->type))
164 dso__sort_by_name(self->dso, self->type);
165
166 return dso__find_symbol_by_name(self->dso, self->type, name);
167}
168
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200169struct map *map__clone(struct map *self)
170{
171 struct map *map = malloc(sizeof(*self));
172
173 if (!map)
174 return NULL;
175
176 memcpy(map, self, sizeof(*self));
177
178 return map;
179}
180
181int map__overlap(struct map *l, struct map *r)
182{
183 if (l->start > r->start) {
184 struct map *t = l;
185 l = r;
186 r = t;
187 }
188
189 if (l->end > r->start)
190 return 1;
191
192 return 0;
193}
194
195size_t map__fprintf(struct map *self, FILE *fp)
196{
197 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
198 self->start, self->end, self->pgoff, self->dso->name);
199}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200200
201/*
202 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
203 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
204 */
205u64 map__rip_2objdump(struct map *map, u64 rip)
206{
207 u64 addr = map->dso->adjust_symbols ?
208 map->unmap_ip(map, rip) : /* RIP -> IP */
209 rip;
210 return addr;
211}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200212
213u64 map__objdump_2ip(struct map *map, u64 addr)
214{
215 u64 ip = map->dso->adjust_symbols ?
216 addr :
217 map->unmap_ip(map, addr); /* RIP -> IP */
218 return ip;
219}
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300220
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300221void map_groups__init(struct map_groups *self)
222{
223 int i;
224 for (i = 0; i < MAP__NR_TYPES; ++i) {
225 self->maps[i] = RB_ROOT;
226 INIT_LIST_HEAD(&self->removed_maps[i]);
227 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300228 self->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300229}
230
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300231static void maps__delete(struct rb_root *self)
232{
233 struct rb_node *next = rb_first(self);
234
235 while (next) {
236 struct map *pos = rb_entry(next, struct map, rb_node);
237
238 next = rb_next(&pos->rb_node);
239 rb_erase(&pos->rb_node, self);
240 map__delete(pos);
241 }
242}
243
244static void maps__delete_removed(struct list_head *self)
245{
246 struct map *pos, *n;
247
248 list_for_each_entry_safe(pos, n, self, node) {
249 list_del(&pos->node);
250 map__delete(pos);
251 }
252}
253
254void map_groups__exit(struct map_groups *self)
255{
256 int i;
257
258 for (i = 0; i < MAP__NR_TYPES; ++i) {
259 maps__delete(&self->maps[i]);
260 maps__delete_removed(&self->removed_maps[i]);
261 }
262}
263
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300264void map_groups__flush(struct map_groups *self)
265{
266 int type;
267
268 for (type = 0; type < MAP__NR_TYPES; type++) {
269 struct rb_root *root = &self->maps[type];
270 struct rb_node *next = rb_first(root);
271
272 while (next) {
273 struct map *pos = rb_entry(next, struct map, rb_node);
274 next = rb_next(&pos->rb_node);
275 rb_erase(&pos->rb_node, root);
276 /*
277 * We may have references to this map, for
278 * instance in some hist_entry instances, so
279 * just move them to a separate list.
280 */
281 list_add_tail(&pos->node, &self->removed_maps[pos->type]);
282 }
283 }
284}
285
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300286struct symbol *map_groups__find_symbol(struct map_groups *self,
287 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300288 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300289 symbol_filter_t filter)
290{
291 struct map *map = map_groups__find(self, type, addr);
292
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300293 if (map != NULL) {
294 if (mapp != NULL)
295 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300296 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300297 }
298
299 return NULL;
300}
301
302struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
303 enum map_type type,
304 const char *name,
305 struct map **mapp,
306 symbol_filter_t filter)
307{
308 struct rb_node *nd;
309
310 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
311 struct map *pos = rb_entry(nd, struct map, rb_node);
312 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
313
314 if (sym == NULL)
315 continue;
316 if (mapp != NULL)
317 *mapp = pos;
318 return sym;
319 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300320
321 return NULL;
322}
323
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300324size_t __map_groups__fprintf_maps(struct map_groups *self,
325 enum map_type type, int verbose, FILE *fp)
326{
327 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
328 struct rb_node *nd;
329
330 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
331 struct map *pos = rb_entry(nd, struct map, rb_node);
332 printed += fprintf(fp, "Map:");
333 printed += map__fprintf(pos, fp);
334 if (verbose > 2) {
335 printed += dso__fprintf(pos->dso, type, fp);
336 printed += fprintf(fp, "--\n");
337 }
338 }
339
340 return printed;
341}
342
343size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp)
344{
345 size_t printed = 0, i;
346 for (i = 0; i < MAP__NR_TYPES; ++i)
347 printed += __map_groups__fprintf_maps(self, i, verbose, fp);
348 return printed;
349}
350
351static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
352 enum map_type type,
353 int verbose, FILE *fp)
354{
355 struct map *pos;
356 size_t printed = 0;
357
358 list_for_each_entry(pos, &self->removed_maps[type], node) {
359 printed += fprintf(fp, "Map:");
360 printed += map__fprintf(pos, fp);
361 if (verbose > 1) {
362 printed += dso__fprintf(pos->dso, type, fp);
363 printed += fprintf(fp, "--\n");
364 }
365 }
366 return printed;
367}
368
369static size_t map_groups__fprintf_removed_maps(struct map_groups *self,
370 int verbose, FILE *fp)
371{
372 size_t printed = 0, i;
373 for (i = 0; i < MAP__NR_TYPES; ++i)
374 printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp);
375 return printed;
376}
377
378size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp)
379{
380 size_t printed = map_groups__fprintf_maps(self, verbose, fp);
381 printed += fprintf(fp, "Removed maps:\n");
382 return printed + map_groups__fprintf_removed_maps(self, verbose, fp);
383}
384
385int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
386 int verbose, FILE *fp)
387{
388 struct rb_root *root = &self->maps[map->type];
389 struct rb_node *next = rb_first(root);
390
391 while (next) {
392 struct map *pos = rb_entry(next, struct map, rb_node);
393 next = rb_next(&pos->rb_node);
394
395 if (!map__overlap(pos, map))
396 continue;
397
398 if (verbose >= 2) {
399 fputs("overlapping maps:\n", fp);
400 map__fprintf(map, fp);
401 map__fprintf(pos, fp);
402 }
403
404 rb_erase(&pos->rb_node, root);
405 /*
406 * We may have references to this map, for instance in some
407 * hist_entry instances, so just move them to a separate
408 * list.
409 */
410 list_add_tail(&pos->node, &self->removed_maps[map->type]);
411 /*
412 * Now check if we need to create new maps for areas not
413 * overlapped by the new map:
414 */
415 if (map->start > pos->start) {
416 struct map *before = map__clone(pos);
417
418 if (before == NULL)
419 return -ENOMEM;
420
421 before->end = map->start - 1;
422 map_groups__insert(self, before);
423 if (verbose >= 2)
424 map__fprintf(before, fp);
425 }
426
427 if (map->end < pos->end) {
428 struct map *after = map__clone(pos);
429
430 if (after == NULL)
431 return -ENOMEM;
432
433 after->start = map->end + 1;
434 map_groups__insert(self, after);
435 if (verbose >= 2)
436 map__fprintf(after, fp);
437 }
438 }
439
440 return 0;
441}
442
443/*
444 * XXX This should not really _copy_ te maps, but refcount them.
445 */
446int map_groups__clone(struct map_groups *self,
447 struct map_groups *parent, enum map_type type)
448{
449 struct rb_node *nd;
450 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
451 struct map *map = rb_entry(nd, struct map, rb_node);
452 struct map *new = map__clone(map);
453 if (new == NULL)
454 return -ENOMEM;
455 map_groups__insert(self, new);
456 }
457 return 0;
458}
459
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300460static u64 map__reloc_map_ip(struct map *map, u64 ip)
461{
462 return ip + (s64)map->pgoff;
463}
464
465static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
466{
467 return ip - (s64)map->pgoff;
468}
469
470void map__reloc_vmlinux(struct map *self)
471{
472 struct kmap *kmap = map__kmap(self);
473 s64 reloc;
474
475 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
476 return;
477
478 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
479 kmap->ref_reloc_sym->addr);
480
481 if (!reloc)
482 return;
483
484 self->map_ip = map__reloc_map_ip;
485 self->unmap_ip = map__reloc_unmap_ip;
486 self->pgoff = reloc;
487}
488
489void maps__insert(struct rb_root *maps, struct map *map)
490{
491 struct rb_node **p = &maps->rb_node;
492 struct rb_node *parent = NULL;
493 const u64 ip = map->start;
494 struct map *m;
495
496 while (*p != NULL) {
497 parent = *p;
498 m = rb_entry(parent, struct map, rb_node);
499 if (ip < m->start)
500 p = &(*p)->rb_left;
501 else
502 p = &(*p)->rb_right;
503 }
504
505 rb_link_node(&map->rb_node, parent, p);
506 rb_insert_color(&map->rb_node, maps);
507}
508
509struct map *maps__find(struct rb_root *maps, u64 ip)
510{
511 struct rb_node **p = &maps->rb_node;
512 struct rb_node *parent = NULL;
513 struct map *m;
514
515 while (*p != NULL) {
516 parent = *p;
517 m = rb_entry(parent, struct map, rb_node);
518 if (ip < m->start)
519 p = &(*p)->rb_left;
520 else if (ip > m->end)
521 p = &(*p)->rb_right;
522 else
523 return m;
524 }
525
526 return NULL;
527}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800528
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300529int machine__init(struct machine *self, const char *root_dir, pid_t pid)
530{
531 map_groups__init(&self->kmaps);
532 RB_CLEAR_NODE(&self->rb_node);
533 INIT_LIST_HEAD(&self->user_dsos);
534 INIT_LIST_HEAD(&self->kernel_dsos);
535
536 self->kmaps.machine = self;
537 self->pid = pid;
538 self->root_dir = strdup(root_dir);
539 return self->root_dir == NULL ? -ENOMEM : 0;
540}
541
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300542struct machine *machines__add(struct rb_root *self, pid_t pid,
543 const char *root_dir)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800544{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300545 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800546 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300547 struct machine *pos, *machine = malloc(sizeof(*machine));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800548
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300549 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800550 return NULL;
551
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300552 if (machine__init(machine, root_dir, pid) != 0) {
553 free(machine);
554 return NULL;
555 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800556
557 while (*p != NULL) {
558 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300559 pos = rb_entry(parent, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800560 if (pid < pos->pid)
561 p = &(*p)->rb_left;
562 else
563 p = &(*p)->rb_right;
564 }
565
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300566 rb_link_node(&machine->rb_node, parent, p);
567 rb_insert_color(&machine->rb_node, self);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800568
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300569 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800570}
571
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300572struct machine *machines__find(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800573{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300574 struct rb_node **p = &self->rb_node;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800575 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300576 struct machine *machine;
577 struct machine *default_machine = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800578
579 while (*p != NULL) {
580 parent = *p;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300581 machine = rb_entry(parent, struct machine, rb_node);
582 if (pid < machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800583 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300584 else if (pid > machine->pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800585 p = &(*p)->rb_right;
586 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300587 return machine;
588 if (!machine->pid)
589 default_machine = machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800590 }
591
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300592 return default_machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800593}
594
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300595struct machine *machines__findnew(struct rb_root *self, pid_t pid)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800596{
597 char path[PATH_MAX];
598 const char *root_dir;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300599 struct machine *machine = machines__find(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800600
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300601 if (!machine || machine->pid != pid) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800602 if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID)
603 root_dir = "";
604 else {
605 if (!symbol_conf.guestmount)
606 goto out;
607 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300608 if (access(path, R_OK)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800609 pr_err("Can't access file %s\n", path);
610 goto out;
611 }
612 root_dir = path;
613 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300614 machine = machines__add(self, pid, root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800615 }
616
617out:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300618 return machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800619}
620
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300621void machines__process(struct rb_root *self, machine__process_t process, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800622{
623 struct rb_node *nd;
624
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300625 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
626 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800627 process(pos, data);
628 }
629}
630
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300631char *machine__mmap_name(struct machine *self, char *bf, size_t size)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800632{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300633 if (machine__is_host(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300634 snprintf(bf, size, "[%s]", "kernel.kallsyms");
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300635 else if (machine__is_default_guest(self))
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300636 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800637 else
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300638 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800639
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300640 return bf;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800641}