Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 1 | #include "symbol.h" |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 3 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 4 | #include <limits.h> |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <stdio.h> |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 8 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 9 | #include "map.h" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 10 | |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 11 | const char *map_type__name[MAP__NR_TYPES] = { |
| 12 | [MAP__FUNCTION] = "Functions", |
| 13 | [MAP__VARIABLE] = "Variables", |
| 14 | }; |
| 15 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 16 | static inline int is_anon_memory(const char *filename) |
| 17 | { |
| 18 | return strcmp(filename, "//anon") == 0; |
| 19 | } |
| 20 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 21 | static inline int is_no_dso_memory(const char *filename) |
| 22 | { |
| 23 | return !strcmp(filename, "[stack]") || |
| 24 | !strcmp(filename, "[vdso]") || |
| 25 | !strcmp(filename, "[heap]"); |
| 26 | } |
| 27 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 28 | void map__init(struct map *self, enum map_type type, |
| 29 | u64 start, u64 end, u64 pgoff, struct dso *dso) |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 30 | { |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 31 | self->type = type; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 32 | self->start = start; |
| 33 | self->end = end; |
| 34 | self->pgoff = pgoff; |
| 35 | self->dso = dso; |
| 36 | self->map_ip = map__map_ip; |
| 37 | self->unmap_ip = map__unmap_ip; |
| 38 | RB_CLEAR_NODE(&self->rb_node); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 39 | self->groups = NULL; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 40 | self->referenced = false; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 41 | } |
| 42 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 43 | struct map *map__new(struct list_head *dsos__list, u64 start, u64 len, |
| 44 | u64 pgoff, u32 pid, char *filename, |
Dave Martin | 361d134 | 2010-07-27 16:40:02 +0100 | [diff] [blame] | 45 | enum map_type type) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 46 | { |
| 47 | struct map *self = malloc(sizeof(*self)); |
| 48 | |
| 49 | if (self != NULL) { |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 50 | char newfilename[PATH_MAX]; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 51 | struct dso *dso; |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 52 | int anon, no_dso; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 53 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 54 | anon = is_anon_memory(filename); |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 55 | no_dso = is_no_dso_memory(filename); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 56 | |
| 57 | if (anon) { |
Arnaldo Carvalho de Melo | b177f63 | 2010-03-25 19:58:57 -0300 | [diff] [blame] | 58 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 59 | filename = newfilename; |
| 60 | } |
| 61 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 62 | dso = __dsos__findnew(dsos__list, filename); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 63 | if (dso == NULL) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 64 | goto out_delete; |
| 65 | |
Arnaldo Carvalho de Melo | b177f63 | 2010-03-25 19:58:57 -0300 | [diff] [blame] | 66 | map__init(self, type, start, start + len, pgoff, dso); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 67 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 68 | if (anon || no_dso) { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 69 | self->map_ip = self->unmap_ip = identity__map_ip; |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Set memory without DSO as loaded. All map__find_* |
| 73 | * functions still return NULL, and we avoid the |
| 74 | * unnecessary map__load warning. |
| 75 | */ |
| 76 | if (no_dso) |
| 77 | dso__set_loaded(dso, self->type); |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame] | 78 | } |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 79 | } |
| 80 | return self; |
| 81 | out_delete: |
| 82 | free(self); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 86 | void map__delete(struct map *self) |
| 87 | { |
| 88 | free(self); |
| 89 | } |
| 90 | |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 91 | void map__fixup_start(struct map *self) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 92 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 93 | struct rb_root *symbols = &self->dso->symbols[self->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 94 | struct rb_node *nd = rb_first(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 95 | if (nd != NULL) { |
| 96 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 97 | self->start = sym->start; |
| 98 | } |
| 99 | } |
| 100 | |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 101 | void map__fixup_end(struct map *self) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 102 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 103 | struct rb_root *symbols = &self->dso->symbols[self->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 104 | struct rb_node *nd = rb_last(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 105 | if (nd != NULL) { |
| 106 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 107 | self->end = sym->end; |
| 108 | } |
| 109 | } |
| 110 | |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 111 | #define DSO__DELETED "(deleted)" |
| 112 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 113 | int map__load(struct map *self, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 114 | { |
| 115 | const char *name = self->dso->long_name; |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 116 | int nr; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 117 | |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 118 | if (dso__loaded(self->dso, self->type)) |
| 119 | return 0; |
| 120 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 121 | nr = dso__load(self->dso, self, filter); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 122 | if (nr < 0) { |
| 123 | if (self->dso->has_build_id) { |
| 124 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 125 | |
| 126 | build_id__sprintf(self->dso->build_id, |
| 127 | sizeof(self->dso->build_id), |
| 128 | sbuild_id); |
| 129 | pr_warning("%s with build id %s not found", |
| 130 | name, sbuild_id); |
| 131 | } else |
| 132 | pr_warning("Failed to open %s", name); |
| 133 | |
| 134 | pr_warning(", continuing without symbols\n"); |
| 135 | return -1; |
| 136 | } else if (nr == 0) { |
| 137 | const size_t len = strlen(name); |
| 138 | const size_t real_len = len - sizeof(DSO__DELETED); |
| 139 | |
| 140 | if (len > sizeof(DSO__DELETED) && |
| 141 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { |
David Ahern | e77b15b | 2011-10-18 18:44:45 -0600 | [diff] [blame] | 142 | pr_warning("%.*s was updated (is prelink enabled?). " |
| 143 | "Restart the long running apps that use it!\n", |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 144 | (int)real_len, name); |
| 145 | } else { |
| 146 | pr_warning("no symbols found in %s, maybe install " |
| 147 | "a debug package?\n", name); |
| 148 | } |
| 149 | |
| 150 | return -1; |
| 151 | } |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 152 | /* |
| 153 | * Only applies to the kernel, as its symtabs aren't relative like the |
| 154 | * module ones. |
| 155 | */ |
| 156 | if (self->dso->kernel) |
| 157 | map__reloc_vmlinux(self); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 162 | struct symbol *map__find_symbol(struct map *self, u64 addr, |
| 163 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 164 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 165 | if (map__load(self, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 166 | return NULL; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 167 | |
Arnaldo Carvalho de Melo | ea08d8c | 2009-12-11 18:56:39 -0200 | [diff] [blame] | 168 | return dso__find_symbol(self->dso, self->type, addr); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 169 | } |
| 170 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 171 | struct symbol *map__find_symbol_by_name(struct map *self, const char *name, |
| 172 | symbol_filter_t filter) |
| 173 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 174 | if (map__load(self, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 175 | return NULL; |
| 176 | |
| 177 | if (!dso__sorted_by_name(self->dso, self->type)) |
| 178 | dso__sort_by_name(self->dso, self->type); |
| 179 | |
| 180 | return dso__find_symbol_by_name(self->dso, self->type, name); |
| 181 | } |
| 182 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 183 | struct map *map__clone(struct map *self) |
| 184 | { |
| 185 | struct map *map = malloc(sizeof(*self)); |
| 186 | |
| 187 | if (!map) |
| 188 | return NULL; |
| 189 | |
| 190 | memcpy(map, self, sizeof(*self)); |
| 191 | |
| 192 | return map; |
| 193 | } |
| 194 | |
| 195 | int map__overlap(struct map *l, struct map *r) |
| 196 | { |
| 197 | if (l->start > r->start) { |
| 198 | struct map *t = l; |
| 199 | l = r; |
| 200 | r = t; |
| 201 | } |
| 202 | |
| 203 | if (l->end > r->start) |
| 204 | return 1; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | size_t map__fprintf(struct map *self, FILE *fp) |
| 210 | { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 211 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 212 | self->start, self->end, self->pgoff, self->dso->name); |
| 213 | } |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 214 | |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 215 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
| 216 | { |
| 217 | const char *dsoname; |
| 218 | |
Akihiro Nagai | 0bc8d20 | 2012-01-30 13:43:20 +0900 | [diff] [blame] | 219 | if (map && map->dso && (map->dso->name || map->dso->long_name)) { |
| 220 | if (symbol_conf.show_kernel_path && map->dso->long_name) |
| 221 | dsoname = map->dso->long_name; |
| 222 | else if (map->dso->name) |
| 223 | dsoname = map->dso->name; |
| 224 | } else |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 225 | dsoname = "[unknown]"; |
| 226 | |
| 227 | return fprintf(fp, "%s", dsoname); |
| 228 | } |
| 229 | |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 230 | /* |
| 231 | * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN. |
| 232 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases. |
| 233 | */ |
| 234 | u64 map__rip_2objdump(struct map *map, u64 rip) |
| 235 | { |
| 236 | u64 addr = map->dso->adjust_symbols ? |
| 237 | map->unmap_ip(map, rip) : /* RIP -> IP */ |
| 238 | rip; |
| 239 | return addr; |
| 240 | } |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 241 | |
| 242 | u64 map__objdump_2ip(struct map *map, u64 addr) |
| 243 | { |
| 244 | u64 ip = map->dso->adjust_symbols ? |
| 245 | addr : |
| 246 | map->unmap_ip(map, addr); /* RIP -> IP */ |
| 247 | return ip; |
| 248 | } |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 249 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 250 | void map_groups__init(struct map_groups *mg) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 251 | { |
| 252 | int i; |
| 253 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 254 | mg->maps[i] = RB_ROOT; |
| 255 | INIT_LIST_HEAD(&mg->removed_maps[i]); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 256 | } |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 257 | mg->machine = NULL; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 258 | } |
| 259 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 260 | static void maps__delete(struct rb_root *maps) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 261 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 262 | struct rb_node *next = rb_first(maps); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 263 | |
| 264 | while (next) { |
| 265 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 266 | |
| 267 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 268 | rb_erase(&pos->rb_node, maps); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 269 | map__delete(pos); |
| 270 | } |
| 271 | } |
| 272 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 273 | static void maps__delete_removed(struct list_head *maps) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 274 | { |
| 275 | struct map *pos, *n; |
| 276 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 277 | list_for_each_entry_safe(pos, n, maps, node) { |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 278 | list_del(&pos->node); |
| 279 | map__delete(pos); |
| 280 | } |
| 281 | } |
| 282 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 283 | void map_groups__exit(struct map_groups *mg) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 284 | { |
| 285 | int i; |
| 286 | |
| 287 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 288 | maps__delete(&mg->maps[i]); |
| 289 | maps__delete_removed(&mg->removed_maps[i]); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 293 | void map_groups__flush(struct map_groups *mg) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 294 | { |
| 295 | int type; |
| 296 | |
| 297 | for (type = 0; type < MAP__NR_TYPES; type++) { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 298 | struct rb_root *root = &mg->maps[type]; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 299 | struct rb_node *next = rb_first(root); |
| 300 | |
| 301 | while (next) { |
| 302 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 303 | next = rb_next(&pos->rb_node); |
| 304 | rb_erase(&pos->rb_node, root); |
| 305 | /* |
| 306 | * We may have references to this map, for |
| 307 | * instance in some hist_entry instances, so |
| 308 | * just move them to a separate list. |
| 309 | */ |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 310 | list_add_tail(&pos->node, &mg->removed_maps[pos->type]); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 315 | struct symbol *map_groups__find_symbol(struct map_groups *mg, |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 316 | enum map_type type, u64 addr, |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 317 | struct map **mapp, |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 318 | symbol_filter_t filter) |
| 319 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 320 | struct map *map = map_groups__find(mg, type, addr); |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 321 | |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 322 | if (map != NULL) { |
| 323 | if (mapp != NULL) |
| 324 | *mapp = map; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 325 | return map__find_symbol(map, map->map_ip(map, addr), filter); |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | return NULL; |
| 329 | } |
| 330 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 331 | struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg, |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 332 | enum map_type type, |
| 333 | const char *name, |
| 334 | struct map **mapp, |
| 335 | symbol_filter_t filter) |
| 336 | { |
| 337 | struct rb_node *nd; |
| 338 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 339 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 340 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 341 | struct symbol *sym = map__find_symbol_by_name(pos, name, filter); |
| 342 | |
| 343 | if (sym == NULL) |
| 344 | continue; |
| 345 | if (mapp != NULL) |
| 346 | *mapp = pos; |
| 347 | return sym; |
| 348 | } |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 349 | |
| 350 | return NULL; |
| 351 | } |
| 352 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 353 | size_t __map_groups__fprintf_maps(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 354 | enum map_type type, int verbose, FILE *fp) |
| 355 | { |
| 356 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); |
| 357 | struct rb_node *nd; |
| 358 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 359 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 360 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 361 | printed += fprintf(fp, "Map:"); |
| 362 | printed += map__fprintf(pos, fp); |
| 363 | if (verbose > 2) { |
| 364 | printed += dso__fprintf(pos->dso, type, fp); |
| 365 | printed += fprintf(fp, "--\n"); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return printed; |
| 370 | } |
| 371 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 372 | size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 373 | { |
| 374 | size_t printed = 0, i; |
| 375 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 376 | printed += __map_groups__fprintf_maps(mg, i, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 377 | return printed; |
| 378 | } |
| 379 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 380 | static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 381 | enum map_type type, |
| 382 | int verbose, FILE *fp) |
| 383 | { |
| 384 | struct map *pos; |
| 385 | size_t printed = 0; |
| 386 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 387 | list_for_each_entry(pos, &mg->removed_maps[type], node) { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 388 | printed += fprintf(fp, "Map:"); |
| 389 | printed += map__fprintf(pos, fp); |
| 390 | if (verbose > 1) { |
| 391 | printed += dso__fprintf(pos->dso, type, fp); |
| 392 | printed += fprintf(fp, "--\n"); |
| 393 | } |
| 394 | } |
| 395 | return printed; |
| 396 | } |
| 397 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 398 | static size_t map_groups__fprintf_removed_maps(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 399 | int verbose, FILE *fp) |
| 400 | { |
| 401 | size_t printed = 0, i; |
| 402 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 403 | printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 404 | return printed; |
| 405 | } |
| 406 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 407 | size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 408 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 409 | size_t printed = map_groups__fprintf_maps(mg, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 410 | printed += fprintf(fp, "Removed maps:\n"); |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 411 | return printed + map_groups__fprintf_removed_maps(mg, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 412 | } |
| 413 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 414 | int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 415 | int verbose, FILE *fp) |
| 416 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 417 | struct rb_root *root = &mg->maps[map->type]; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 418 | struct rb_node *next = rb_first(root); |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 419 | int err = 0; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 420 | |
| 421 | while (next) { |
| 422 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 423 | next = rb_next(&pos->rb_node); |
| 424 | |
| 425 | if (!map__overlap(pos, map)) |
| 426 | continue; |
| 427 | |
| 428 | if (verbose >= 2) { |
| 429 | fputs("overlapping maps:\n", fp); |
| 430 | map__fprintf(map, fp); |
| 431 | map__fprintf(pos, fp); |
| 432 | } |
| 433 | |
| 434 | rb_erase(&pos->rb_node, root); |
| 435 | /* |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 436 | * Now check if we need to create new maps for areas not |
| 437 | * overlapped by the new map: |
| 438 | */ |
| 439 | if (map->start > pos->start) { |
| 440 | struct map *before = map__clone(pos); |
| 441 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 442 | if (before == NULL) { |
| 443 | err = -ENOMEM; |
| 444 | goto move_map; |
| 445 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 446 | |
| 447 | before->end = map->start - 1; |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 448 | map_groups__insert(mg, before); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 449 | if (verbose >= 2) |
| 450 | map__fprintf(before, fp); |
| 451 | } |
| 452 | |
| 453 | if (map->end < pos->end) { |
| 454 | struct map *after = map__clone(pos); |
| 455 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 456 | if (after == NULL) { |
| 457 | err = -ENOMEM; |
| 458 | goto move_map; |
| 459 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 460 | |
| 461 | after->start = map->end + 1; |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 462 | map_groups__insert(mg, after); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 463 | if (verbose >= 2) |
| 464 | map__fprintf(after, fp); |
| 465 | } |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 466 | move_map: |
| 467 | /* |
| 468 | * If we have references, just move them to a separate list. |
| 469 | */ |
| 470 | if (pos->referenced) |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 471 | list_add_tail(&pos->node, &mg->removed_maps[map->type]); |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 472 | else |
| 473 | map__delete(pos); |
| 474 | |
| 475 | if (err) |
| 476 | return err; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * XXX This should not really _copy_ te maps, but refcount them. |
| 484 | */ |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 485 | int map_groups__clone(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 486 | struct map_groups *parent, enum map_type type) |
| 487 | { |
| 488 | struct rb_node *nd; |
| 489 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { |
| 490 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 491 | struct map *new = map__clone(map); |
| 492 | if (new == NULL) |
| 493 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 494 | map_groups__insert(mg, new); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 499 | static u64 map__reloc_map_ip(struct map *map, u64 ip) |
| 500 | { |
| 501 | return ip + (s64)map->pgoff; |
| 502 | } |
| 503 | |
| 504 | static u64 map__reloc_unmap_ip(struct map *map, u64 ip) |
| 505 | { |
| 506 | return ip - (s64)map->pgoff; |
| 507 | } |
| 508 | |
| 509 | void map__reloc_vmlinux(struct map *self) |
| 510 | { |
| 511 | struct kmap *kmap = map__kmap(self); |
| 512 | s64 reloc; |
| 513 | |
| 514 | if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr) |
| 515 | return; |
| 516 | |
| 517 | reloc = (kmap->ref_reloc_sym->unrelocated_addr - |
| 518 | kmap->ref_reloc_sym->addr); |
| 519 | |
| 520 | if (!reloc) |
| 521 | return; |
| 522 | |
| 523 | self->map_ip = map__reloc_map_ip; |
| 524 | self->unmap_ip = map__reloc_unmap_ip; |
| 525 | self->pgoff = reloc; |
| 526 | } |
| 527 | |
| 528 | void maps__insert(struct rb_root *maps, struct map *map) |
| 529 | { |
| 530 | struct rb_node **p = &maps->rb_node; |
| 531 | struct rb_node *parent = NULL; |
| 532 | const u64 ip = map->start; |
| 533 | struct map *m; |
| 534 | |
| 535 | while (*p != NULL) { |
| 536 | parent = *p; |
| 537 | m = rb_entry(parent, struct map, rb_node); |
| 538 | if (ip < m->start) |
| 539 | p = &(*p)->rb_left; |
| 540 | else |
| 541 | p = &(*p)->rb_right; |
| 542 | } |
| 543 | |
| 544 | rb_link_node(&map->rb_node, parent, p); |
| 545 | rb_insert_color(&map->rb_node, maps); |
| 546 | } |
| 547 | |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 548 | void maps__remove(struct rb_root *self, struct map *map) |
| 549 | { |
| 550 | rb_erase(&map->rb_node, self); |
| 551 | } |
| 552 | |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 553 | struct map *maps__find(struct rb_root *maps, u64 ip) |
| 554 | { |
| 555 | struct rb_node **p = &maps->rb_node; |
| 556 | struct rb_node *parent = NULL; |
| 557 | struct map *m; |
| 558 | |
| 559 | while (*p != NULL) { |
| 560 | parent = *p; |
| 561 | m = rb_entry(parent, struct map, rb_node); |
| 562 | if (ip < m->start) |
| 563 | p = &(*p)->rb_left; |
| 564 | else if (ip > m->end) |
| 565 | p = &(*p)->rb_right; |
| 566 | else |
| 567 | return m; |
| 568 | } |
| 569 | |
| 570 | return NULL; |
| 571 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 572 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 573 | int machine__init(struct machine *self, const char *root_dir, pid_t pid) |
| 574 | { |
| 575 | map_groups__init(&self->kmaps); |
| 576 | RB_CLEAR_NODE(&self->rb_node); |
| 577 | INIT_LIST_HEAD(&self->user_dsos); |
| 578 | INIT_LIST_HEAD(&self->kernel_dsos); |
| 579 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 580 | self->threads = RB_ROOT; |
| 581 | INIT_LIST_HEAD(&self->dead_threads); |
| 582 | self->last_match = NULL; |
| 583 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 584 | self->kmaps.machine = self; |
| 585 | self->pid = pid; |
| 586 | self->root_dir = strdup(root_dir); |
| 587 | return self->root_dir == NULL ? -ENOMEM : 0; |
| 588 | } |
| 589 | |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 590 | static void dsos__delete(struct list_head *self) |
| 591 | { |
| 592 | struct dso *pos, *n; |
| 593 | |
| 594 | list_for_each_entry_safe(pos, n, self, node) { |
| 595 | list_del(&pos->node); |
| 596 | dso__delete(pos); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void machine__exit(struct machine *self) |
| 601 | { |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 602 | map_groups__exit(&self->kmaps); |
| 603 | dsos__delete(&self->user_dsos); |
| 604 | dsos__delete(&self->kernel_dsos); |
| 605 | free(self->root_dir); |
| 606 | self->root_dir = NULL; |
| 607 | } |
| 608 | |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 609 | void machine__delete(struct machine *self) |
| 610 | { |
| 611 | machine__exit(self); |
| 612 | free(self); |
| 613 | } |
| 614 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 615 | struct machine *machines__add(struct rb_root *self, pid_t pid, |
| 616 | const char *root_dir) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 617 | { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 618 | struct rb_node **p = &self->rb_node; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 619 | struct rb_node *parent = NULL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 620 | struct machine *pos, *machine = malloc(sizeof(*machine)); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 621 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 622 | if (!machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 623 | return NULL; |
| 624 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 625 | if (machine__init(machine, root_dir, pid) != 0) { |
| 626 | free(machine); |
| 627 | return NULL; |
| 628 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 629 | |
| 630 | while (*p != NULL) { |
| 631 | parent = *p; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 632 | pos = rb_entry(parent, struct machine, rb_node); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 633 | if (pid < pos->pid) |
| 634 | p = &(*p)->rb_left; |
| 635 | else |
| 636 | p = &(*p)->rb_right; |
| 637 | } |
| 638 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 639 | rb_link_node(&machine->rb_node, parent, p); |
| 640 | rb_insert_color(&machine->rb_node, self); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 641 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 642 | return machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 643 | } |
| 644 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 645 | struct machine *machines__find(struct rb_root *self, pid_t pid) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 646 | { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 647 | struct rb_node **p = &self->rb_node; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 648 | struct rb_node *parent = NULL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 649 | struct machine *machine; |
| 650 | struct machine *default_machine = NULL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 651 | |
| 652 | while (*p != NULL) { |
| 653 | parent = *p; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 654 | machine = rb_entry(parent, struct machine, rb_node); |
| 655 | if (pid < machine->pid) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 656 | p = &(*p)->rb_left; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 657 | else if (pid > machine->pid) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 658 | p = &(*p)->rb_right; |
| 659 | else |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 660 | return machine; |
| 661 | if (!machine->pid) |
| 662 | default_machine = machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 663 | } |
| 664 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 665 | return default_machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 666 | } |
| 667 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 668 | struct machine *machines__findnew(struct rb_root *self, pid_t pid) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 669 | { |
| 670 | char path[PATH_MAX]; |
| 671 | const char *root_dir; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 672 | struct machine *machine = machines__find(self, pid); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 673 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 674 | if (!machine || machine->pid != pid) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 675 | if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID) |
| 676 | root_dir = ""; |
| 677 | else { |
| 678 | if (!symbol_conf.guestmount) |
| 679 | goto out; |
| 680 | sprintf(path, "%s/%d", symbol_conf.guestmount, pid); |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 681 | if (access(path, R_OK)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 682 | pr_err("Can't access file %s\n", path); |
| 683 | goto out; |
| 684 | } |
| 685 | root_dir = path; |
| 686 | } |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 687 | machine = machines__add(self, pid, root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | out: |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 691 | return machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 692 | } |
| 693 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 694 | void machines__process(struct rb_root *self, machine__process_t process, void *data) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 695 | { |
| 696 | struct rb_node *nd; |
| 697 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 698 | for (nd = rb_first(self); nd; nd = rb_next(nd)) { |
| 699 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 700 | process(pos, data); |
| 701 | } |
| 702 | } |
| 703 | |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 704 | char *machine__mmap_name(struct machine *self, char *bf, size_t size) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 705 | { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 706 | if (machine__is_host(self)) |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 707 | snprintf(bf, size, "[%s]", "kernel.kallsyms"); |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 708 | else if (machine__is_default_guest(self)) |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 709 | snprintf(bf, size, "[%s]", "guest.kernel.kallsyms"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 710 | else |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 711 | snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 712 | |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 713 | return bf; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 714 | } |