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