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> |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 7 | #include "map.h" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 8 | |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 9 | const char *map_type__name[MAP__NR_TYPES] = { |
| 10 | [MAP__FUNCTION] = "Functions", |
| 11 | [MAP__VARIABLE] = "Variables", |
| 12 | }; |
| 13 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 14 | static inline int is_anon_memory(const char *filename) |
| 15 | { |
| 16 | return strcmp(filename, "//anon") == 0; |
| 17 | } |
| 18 | |
| 19 | static int strcommon(const char *pathname, char *cwd, int cwdlen) |
| 20 | { |
| 21 | int n = 0; |
| 22 | |
| 23 | while (n < cwdlen && pathname[n] == cwd[n]) |
| 24 | ++n; |
| 25 | |
| 26 | return n; |
| 27 | } |
| 28 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 29 | void map__init(struct map *self, enum map_type type, |
| 30 | u64 start, u64 end, u64 pgoff, struct dso *dso) |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 31 | { |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 32 | self->type = type; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 33 | self->start = start; |
| 34 | self->end = end; |
| 35 | self->pgoff = pgoff; |
| 36 | self->dso = dso; |
| 37 | self->map_ip = map__map_ip; |
| 38 | self->unmap_ip = map__unmap_ip; |
| 39 | RB_CLEAR_NODE(&self->rb_node); |
| 40 | } |
| 41 | |
Arnaldo Carvalho de Melo | b177f63 | 2010-03-25 19:58:57 -0300 | [diff] [blame] | 42 | struct map *map__new(u64 start, u64 len, u64 pgoff, u32 pid, char *filename, |
| 43 | enum map_type type, char *cwd, int cwdlen) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 44 | { |
| 45 | struct map *self = malloc(sizeof(*self)); |
| 46 | |
| 47 | if (self != NULL) { |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 48 | char newfilename[PATH_MAX]; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 49 | struct dso *dso; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 50 | int anon; |
| 51 | |
| 52 | if (cwd) { |
| 53 | int n = strcommon(filename, cwd, cwdlen); |
| 54 | |
| 55 | if (n == cwdlen) { |
| 56 | snprintf(newfilename, sizeof(newfilename), |
| 57 | ".%s", filename + n); |
| 58 | filename = newfilename; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | anon = is_anon_memory(filename); |
| 63 | |
| 64 | if (anon) { |
Arnaldo Carvalho de Melo | b177f63 | 2010-03-25 19:58:57 -0300 | [diff] [blame] | 65 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 66 | filename = newfilename; |
| 67 | } |
| 68 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 69 | dso = dsos__findnew(filename); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 70 | if (dso == NULL) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 71 | goto out_delete; |
| 72 | |
Arnaldo Carvalho de Melo | b177f63 | 2010-03-25 19:58:57 -0300 | [diff] [blame] | 73 | map__init(self, type, start, start + len, pgoff, dso); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 74 | |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame] | 75 | if (anon) { |
| 76 | set_identity: |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 77 | self->map_ip = self->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame] | 78 | } else if (strcmp(filename, "[vdso]") == 0) { |
| 79 | dso__set_loaded(dso, self->type); |
| 80 | goto set_identity; |
| 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 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 89 | void map__delete(struct map *self) |
| 90 | { |
| 91 | free(self); |
| 92 | } |
| 93 | |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 94 | void map__fixup_start(struct map *self) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 95 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 96 | struct rb_root *symbols = &self->dso->symbols[self->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 97 | struct rb_node *nd = rb_first(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 98 | if (nd != NULL) { |
| 99 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 100 | self->start = sym->start; |
| 101 | } |
| 102 | } |
| 103 | |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 104 | void map__fixup_end(struct map *self) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 105 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 106 | struct rb_root *symbols = &self->dso->symbols[self->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 107 | struct rb_node *nd = rb_last(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 108 | if (nd != NULL) { |
| 109 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 110 | self->end = sym->end; |
| 111 | } |
| 112 | } |
| 113 | |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 114 | #define DSO__DELETED "(deleted)" |
| 115 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 116 | int map__load(struct map *self, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 117 | { |
| 118 | const char *name = self->dso->long_name; |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 119 | int nr; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 120 | |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 121 | if (dso__loaded(self->dso, self->type)) |
| 122 | return 0; |
| 123 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 124 | nr = dso__load(self->dso, self, filter); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 125 | if (nr < 0) { |
| 126 | if (self->dso->has_build_id) { |
| 127 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 128 | |
| 129 | build_id__sprintf(self->dso->build_id, |
| 130 | sizeof(self->dso->build_id), |
| 131 | sbuild_id); |
| 132 | pr_warning("%s with build id %s not found", |
| 133 | name, sbuild_id); |
| 134 | } else |
| 135 | pr_warning("Failed to open %s", name); |
| 136 | |
| 137 | pr_warning(", continuing without symbols\n"); |
| 138 | return -1; |
| 139 | } else if (nr == 0) { |
| 140 | const size_t len = strlen(name); |
| 141 | const size_t real_len = len - sizeof(DSO__DELETED); |
| 142 | |
| 143 | if (len > sizeof(DSO__DELETED) && |
| 144 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { |
| 145 | pr_warning("%.*s was updated, restart the long " |
| 146 | "running apps that use it!\n", |
| 147 | (int)real_len, name); |
| 148 | } else { |
| 149 | pr_warning("no symbols found in %s, maybe install " |
| 150 | "a debug package?\n", name); |
| 151 | } |
| 152 | |
| 153 | return -1; |
| 154 | } |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 155 | /* |
| 156 | * Only applies to the kernel, as its symtabs aren't relative like the |
| 157 | * module ones. |
| 158 | */ |
| 159 | if (self->dso->kernel) |
| 160 | map__reloc_vmlinux(self); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 165 | struct symbol *map__find_symbol(struct map *self, u64 addr, |
| 166 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 167 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 168 | if (map__load(self, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 169 | return NULL; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 170 | |
Arnaldo Carvalho de Melo | ea08d8c | 2009-12-11 18:56:39 -0200 | [diff] [blame] | 171 | return dso__find_symbol(self->dso, self->type, addr); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 172 | } |
| 173 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 174 | struct symbol *map__find_symbol_by_name(struct map *self, const char *name, |
| 175 | symbol_filter_t filter) |
| 176 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 177 | if (map__load(self, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 178 | return NULL; |
| 179 | |
| 180 | if (!dso__sorted_by_name(self->dso, self->type)) |
| 181 | dso__sort_by_name(self->dso, self->type); |
| 182 | |
| 183 | return dso__find_symbol_by_name(self->dso, self->type, name); |
| 184 | } |
| 185 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 186 | struct map *map__clone(struct map *self) |
| 187 | { |
| 188 | struct map *map = malloc(sizeof(*self)); |
| 189 | |
| 190 | if (!map) |
| 191 | return NULL; |
| 192 | |
| 193 | memcpy(map, self, sizeof(*self)); |
| 194 | |
| 195 | return map; |
| 196 | } |
| 197 | |
| 198 | int map__overlap(struct map *l, struct map *r) |
| 199 | { |
| 200 | if (l->start > r->start) { |
| 201 | struct map *t = l; |
| 202 | l = r; |
| 203 | r = t; |
| 204 | } |
| 205 | |
| 206 | if (l->end > r->start) |
| 207 | return 1; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | size_t map__fprintf(struct map *self, FILE *fp) |
| 213 | { |
| 214 | return fprintf(fp, " %Lx-%Lx %Lx %s\n", |
| 215 | self->start, self->end, self->pgoff, self->dso->name); |
| 216 | } |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN. |
| 220 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases. |
| 221 | */ |
| 222 | u64 map__rip_2objdump(struct map *map, u64 rip) |
| 223 | { |
| 224 | u64 addr = map->dso->adjust_symbols ? |
| 225 | map->unmap_ip(map, rip) : /* RIP -> IP */ |
| 226 | rip; |
| 227 | return addr; |
| 228 | } |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 229 | |
| 230 | u64 map__objdump_2ip(struct map *map, u64 addr) |
| 231 | { |
| 232 | u64 ip = map->dso->adjust_symbols ? |
| 233 | addr : |
| 234 | map->unmap_ip(map, addr); /* RIP -> IP */ |
| 235 | return ip; |
| 236 | } |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 237 | |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame^] | 238 | void map_groups__init(struct map_groups *self) |
| 239 | { |
| 240 | int i; |
| 241 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
| 242 | self->maps[i] = RB_ROOT; |
| 243 | INIT_LIST_HEAD(&self->removed_maps[i]); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void map_groups__flush(struct map_groups *self) |
| 248 | { |
| 249 | int type; |
| 250 | |
| 251 | for (type = 0; type < MAP__NR_TYPES; type++) { |
| 252 | struct rb_root *root = &self->maps[type]; |
| 253 | struct rb_node *next = rb_first(root); |
| 254 | |
| 255 | while (next) { |
| 256 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 257 | next = rb_next(&pos->rb_node); |
| 258 | rb_erase(&pos->rb_node, root); |
| 259 | /* |
| 260 | * We may have references to this map, for |
| 261 | * instance in some hist_entry instances, so |
| 262 | * just move them to a separate list. |
| 263 | */ |
| 264 | list_add_tail(&pos->node, &self->removed_maps[pos->type]); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 269 | struct symbol *map_groups__find_symbol(struct map_groups *self, |
| 270 | enum map_type type, u64 addr, |
| 271 | symbol_filter_t filter) |
| 272 | { |
| 273 | struct map *map = map_groups__find(self, type, addr); |
| 274 | |
| 275 | if (map != NULL) |
| 276 | return map__find_symbol(map, map->map_ip(map, addr), filter); |
| 277 | |
| 278 | return NULL; |
| 279 | } |
| 280 | |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame^] | 281 | size_t __map_groups__fprintf_maps(struct map_groups *self, |
| 282 | enum map_type type, int verbose, FILE *fp) |
| 283 | { |
| 284 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); |
| 285 | struct rb_node *nd; |
| 286 | |
| 287 | for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) { |
| 288 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 289 | printed += fprintf(fp, "Map:"); |
| 290 | printed += map__fprintf(pos, fp); |
| 291 | if (verbose > 2) { |
| 292 | printed += dso__fprintf(pos->dso, type, fp); |
| 293 | printed += fprintf(fp, "--\n"); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return printed; |
| 298 | } |
| 299 | |
| 300 | size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp) |
| 301 | { |
| 302 | size_t printed = 0, i; |
| 303 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 304 | printed += __map_groups__fprintf_maps(self, i, verbose, fp); |
| 305 | return printed; |
| 306 | } |
| 307 | |
| 308 | static size_t __map_groups__fprintf_removed_maps(struct map_groups *self, |
| 309 | enum map_type type, |
| 310 | int verbose, FILE *fp) |
| 311 | { |
| 312 | struct map *pos; |
| 313 | size_t printed = 0; |
| 314 | |
| 315 | list_for_each_entry(pos, &self->removed_maps[type], node) { |
| 316 | printed += fprintf(fp, "Map:"); |
| 317 | printed += map__fprintf(pos, fp); |
| 318 | if (verbose > 1) { |
| 319 | printed += dso__fprintf(pos->dso, type, fp); |
| 320 | printed += fprintf(fp, "--\n"); |
| 321 | } |
| 322 | } |
| 323 | return printed; |
| 324 | } |
| 325 | |
| 326 | static size_t map_groups__fprintf_removed_maps(struct map_groups *self, |
| 327 | int verbose, FILE *fp) |
| 328 | { |
| 329 | size_t printed = 0, i; |
| 330 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 331 | printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp); |
| 332 | return printed; |
| 333 | } |
| 334 | |
| 335 | size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp) |
| 336 | { |
| 337 | size_t printed = map_groups__fprintf_maps(self, verbose, fp); |
| 338 | printed += fprintf(fp, "Removed maps:\n"); |
| 339 | return printed + map_groups__fprintf_removed_maps(self, verbose, fp); |
| 340 | } |
| 341 | |
| 342 | int map_groups__fixup_overlappings(struct map_groups *self, struct map *map, |
| 343 | int verbose, FILE *fp) |
| 344 | { |
| 345 | struct rb_root *root = &self->maps[map->type]; |
| 346 | struct rb_node *next = rb_first(root); |
| 347 | |
| 348 | while (next) { |
| 349 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 350 | next = rb_next(&pos->rb_node); |
| 351 | |
| 352 | if (!map__overlap(pos, map)) |
| 353 | continue; |
| 354 | |
| 355 | if (verbose >= 2) { |
| 356 | fputs("overlapping maps:\n", fp); |
| 357 | map__fprintf(map, fp); |
| 358 | map__fprintf(pos, fp); |
| 359 | } |
| 360 | |
| 361 | rb_erase(&pos->rb_node, root); |
| 362 | /* |
| 363 | * We may have references to this map, for instance in some |
| 364 | * hist_entry instances, so just move them to a separate |
| 365 | * list. |
| 366 | */ |
| 367 | list_add_tail(&pos->node, &self->removed_maps[map->type]); |
| 368 | /* |
| 369 | * Now check if we need to create new maps for areas not |
| 370 | * overlapped by the new map: |
| 371 | */ |
| 372 | if (map->start > pos->start) { |
| 373 | struct map *before = map__clone(pos); |
| 374 | |
| 375 | if (before == NULL) |
| 376 | return -ENOMEM; |
| 377 | |
| 378 | before->end = map->start - 1; |
| 379 | map_groups__insert(self, before); |
| 380 | if (verbose >= 2) |
| 381 | map__fprintf(before, fp); |
| 382 | } |
| 383 | |
| 384 | if (map->end < pos->end) { |
| 385 | struct map *after = map__clone(pos); |
| 386 | |
| 387 | if (after == NULL) |
| 388 | return -ENOMEM; |
| 389 | |
| 390 | after->start = map->end + 1; |
| 391 | map_groups__insert(self, after); |
| 392 | if (verbose >= 2) |
| 393 | map__fprintf(after, fp); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * XXX This should not really _copy_ te maps, but refcount them. |
| 402 | */ |
| 403 | int map_groups__clone(struct map_groups *self, |
| 404 | struct map_groups *parent, enum map_type type) |
| 405 | { |
| 406 | struct rb_node *nd; |
| 407 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { |
| 408 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 409 | struct map *new = map__clone(map); |
| 410 | if (new == NULL) |
| 411 | return -ENOMEM; |
| 412 | map_groups__insert(self, new); |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 417 | static u64 map__reloc_map_ip(struct map *map, u64 ip) |
| 418 | { |
| 419 | return ip + (s64)map->pgoff; |
| 420 | } |
| 421 | |
| 422 | static u64 map__reloc_unmap_ip(struct map *map, u64 ip) |
| 423 | { |
| 424 | return ip - (s64)map->pgoff; |
| 425 | } |
| 426 | |
| 427 | void map__reloc_vmlinux(struct map *self) |
| 428 | { |
| 429 | struct kmap *kmap = map__kmap(self); |
| 430 | s64 reloc; |
| 431 | |
| 432 | if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr) |
| 433 | return; |
| 434 | |
| 435 | reloc = (kmap->ref_reloc_sym->unrelocated_addr - |
| 436 | kmap->ref_reloc_sym->addr); |
| 437 | |
| 438 | if (!reloc) |
| 439 | return; |
| 440 | |
| 441 | self->map_ip = map__reloc_map_ip; |
| 442 | self->unmap_ip = map__reloc_unmap_ip; |
| 443 | self->pgoff = reloc; |
| 444 | } |
| 445 | |
| 446 | void maps__insert(struct rb_root *maps, struct map *map) |
| 447 | { |
| 448 | struct rb_node **p = &maps->rb_node; |
| 449 | struct rb_node *parent = NULL; |
| 450 | const u64 ip = map->start; |
| 451 | struct map *m; |
| 452 | |
| 453 | while (*p != NULL) { |
| 454 | parent = *p; |
| 455 | m = rb_entry(parent, struct map, rb_node); |
| 456 | if (ip < m->start) |
| 457 | p = &(*p)->rb_left; |
| 458 | else |
| 459 | p = &(*p)->rb_right; |
| 460 | } |
| 461 | |
| 462 | rb_link_node(&map->rb_node, parent, p); |
| 463 | rb_insert_color(&map->rb_node, maps); |
| 464 | } |
| 465 | |
| 466 | struct map *maps__find(struct rb_root *maps, u64 ip) |
| 467 | { |
| 468 | struct rb_node **p = &maps->rb_node; |
| 469 | struct rb_node *parent = NULL; |
| 470 | struct map *m; |
| 471 | |
| 472 | while (*p != NULL) { |
| 473 | parent = *p; |
| 474 | m = rb_entry(parent, struct map, rb_node); |
| 475 | if (ip < m->start) |
| 476 | p = &(*p)->rb_left; |
| 477 | else if (ip > m->end) |
| 478 | p = &(*p)->rb_right; |
| 479 | else |
| 480 | return m; |
| 481 | } |
| 482 | |
| 483 | return NULL; |
| 484 | } |