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