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