]>
Commit | Line | Data |
---|---|---|
66e274f3 | 1 | #include "symbol.h" |
c6e718ff | 2 | #include <errno.h> |
9486aa38 | 3 | #include <inttypes.h> |
4b8cf846 | 4 | #include <limits.h> |
66e274f3 FW |
5 | #include <stdlib.h> |
6 | #include <string.h> | |
7 | #include <stdio.h> | |
a1645ce1 | 8 | #include <unistd.h> |
4b8cf846 | 9 | #include "map.h" |
5cd95c2d | 10 | #include "thread.h" |
c80c3c26 | 11 | #include "strlist.h" |
7dbf4dcf | 12 | #include "vdso.h" |
ebb296c2 | 13 | #include "build-id.h" |
cc8fae1d | 14 | #include "util.h" |
8e16017d | 15 | #include <linux/string.h> |
66e274f3 | 16 | |
3846df2e ACM |
17 | const char *map_type__name[MAP__NR_TYPES] = { |
18 | [MAP__FUNCTION] = "Functions", | |
19 | [MAP__VARIABLE] = "Variables", | |
20 | }; | |
21 | ||
66e274f3 FW |
22 | static inline int is_anon_memory(const char *filename) |
23 | { | |
d0528b5d | 24 | return !strcmp(filename, "//anon") || |
89365e6c | 25 | !strcmp(filename, "/dev/zero (deleted)") || |
d0528b5d | 26 | !strcmp(filename, "/anon_hugepage (deleted)"); |
66e274f3 FW |
27 | } |
28 | ||
87ffef79 JO |
29 | static inline int is_no_dso_memory(const char *filename) |
30 | { | |
1e82574d | 31 | return !strncmp(filename, "[stack", 6) || |
87ffef79 JO |
32 | !strcmp(filename, "[heap]"); |
33 | } | |
34 | ||
eca81836 ML |
35 | static inline int is_android_lib(const char *filename) |
36 | { | |
37 | return !strncmp(filename, "/data/app-lib", 13) || | |
38 | !strncmp(filename, "/system/lib", 11); | |
39 | } | |
40 | ||
41 | static inline bool replace_android_lib(const char *filename, char *newfilename) | |
42 | { | |
43 | const char *libname; | |
44 | char *app_abi; | |
45 | size_t app_abi_length, new_length; | |
46 | size_t lib_length = 0; | |
47 | ||
48 | libname = strrchr(filename, '/'); | |
49 | if (libname) | |
50 | lib_length = strlen(libname); | |
51 | ||
52 | app_abi = getenv("APP_ABI"); | |
53 | if (!app_abi) | |
54 | return false; | |
55 | ||
56 | app_abi_length = strlen(app_abi); | |
57 | ||
58 | if (!strncmp(filename, "/data/app-lib", 13)) { | |
59 | char *apk_path; | |
60 | ||
61 | if (!app_abi_length) | |
62 | return false; | |
63 | ||
64 | new_length = 7 + app_abi_length + lib_length; | |
65 | ||
66 | apk_path = getenv("APK_PATH"); | |
67 | if (apk_path) { | |
68 | new_length += strlen(apk_path) + 1; | |
69 | if (new_length > PATH_MAX) | |
70 | return false; | |
71 | snprintf(newfilename, new_length, | |
72 | "%s/libs/%s/%s", apk_path, app_abi, libname); | |
73 | } else { | |
74 | if (new_length > PATH_MAX) | |
75 | return false; | |
76 | snprintf(newfilename, new_length, | |
77 | "libs/%s/%s", app_abi, libname); | |
78 | } | |
79 | return true; | |
80 | } | |
81 | ||
82 | if (!strncmp(filename, "/system/lib/", 11)) { | |
83 | char *ndk, *app; | |
84 | const char *arch; | |
85 | size_t ndk_length; | |
86 | size_t app_length; | |
87 | ||
88 | ndk = getenv("NDK_ROOT"); | |
89 | app = getenv("APP_PLATFORM"); | |
90 | ||
91 | if (!(ndk && app)) | |
92 | return false; | |
93 | ||
94 | ndk_length = strlen(ndk); | |
95 | app_length = strlen(app); | |
96 | ||
97 | if (!(ndk_length && app_length && app_abi_length)) | |
98 | return false; | |
99 | ||
100 | arch = !strncmp(app_abi, "arm", 3) ? "arm" : | |
101 | !strncmp(app_abi, "mips", 4) ? "mips" : | |
102 | !strncmp(app_abi, "x86", 3) ? "x86" : NULL; | |
103 | ||
104 | if (!arch) | |
105 | return false; | |
106 | ||
107 | new_length = 27 + ndk_length + | |
108 | app_length + lib_length | |
109 | + strlen(arch); | |
110 | ||
111 | if (new_length > PATH_MAX) | |
112 | return false; | |
113 | snprintf(newfilename, new_length, | |
114 | "%s/platforms/%s/arch-%s/usr/lib/%s", | |
115 | ndk, app, arch, libname); | |
116 | ||
117 | return true; | |
118 | } | |
119 | return false; | |
120 | } | |
121 | ||
237a7e04 | 122 | void map__init(struct map *map, enum map_type type, |
3610583c | 123 | u64 start, u64 end, u64 pgoff, struct dso *dso) |
afb7b4f0 | 124 | { |
237a7e04 ACM |
125 | map->type = type; |
126 | map->start = start; | |
127 | map->end = end; | |
128 | map->pgoff = pgoff; | |
9176753d | 129 | map->reloc = 0; |
237a7e04 ACM |
130 | map->dso = dso; |
131 | map->map_ip = map__map_ip; | |
132 | map->unmap_ip = map__unmap_ip; | |
133 | RB_CLEAR_NODE(&map->rb_node); | |
134 | map->groups = NULL; | |
135 | map->referenced = false; | |
136 | map->erange_warned = false; | |
afb7b4f0 ACM |
137 | } |
138 | ||
a1645ce1 | 139 | struct map *map__new(struct list_head *dsos__list, u64 start, u64 len, |
5c5e854b | 140 | u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino, |
7ef80703 | 141 | u64 ino_gen, u32 prot, u32 flags, char *filename, |
361d1346 | 142 | enum map_type type) |
66e274f3 | 143 | { |
237a7e04 | 144 | struct map *map = malloc(sizeof(*map)); |
66e274f3 | 145 | |
237a7e04 | 146 | if (map != NULL) { |
66e274f3 | 147 | char newfilename[PATH_MAX]; |
afb7b4f0 | 148 | struct dso *dso; |
eca81836 | 149 | int anon, no_dso, vdso, android; |
66e274f3 | 150 | |
eca81836 | 151 | android = is_android_lib(filename); |
66e274f3 | 152 | anon = is_anon_memory(filename); |
7dbf4dcf | 153 | vdso = is_vdso_map(filename); |
87ffef79 | 154 | no_dso = is_no_dso_memory(filename); |
66e274f3 | 155 | |
5c5e854b SE |
156 | map->maj = d_maj; |
157 | map->min = d_min; | |
158 | map->ino = ino; | |
159 | map->ino_generation = ino_gen; | |
7ef80703 DZ |
160 | map->prot = prot; |
161 | map->flags = flags; | |
5c5e854b | 162 | |
578c03c8 | 163 | if ((anon || no_dso) && type == MAP__FUNCTION) { |
b177f63f | 164 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid); |
66e274f3 FW |
165 | filename = newfilename; |
166 | } | |
167 | ||
eca81836 ML |
168 | if (android) { |
169 | if (replace_android_lib(filename, newfilename)) | |
170 | filename = newfilename; | |
171 | } | |
172 | ||
7dbf4dcf JO |
173 | if (vdso) { |
174 | pgoff = 0; | |
175 | dso = vdso__dso_findnew(dsos__list); | |
176 | } else | |
177 | dso = __dsos__findnew(dsos__list, filename); | |
178 | ||
afb7b4f0 | 179 | if (dso == NULL) |
66e274f3 FW |
180 | goto out_delete; |
181 | ||
237a7e04 | 182 | map__init(map, type, start, start + len, pgoff, dso); |
afb7b4f0 | 183 | |
87ffef79 | 184 | if (anon || no_dso) { |
237a7e04 | 185 | map->map_ip = map->unmap_ip = identity__map_ip; |
87ffef79 JO |
186 | |
187 | /* | |
188 | * Set memory without DSO as loaded. All map__find_* | |
189 | * functions still return NULL, and we avoid the | |
190 | * unnecessary map__load warning. | |
191 | */ | |
578c03c8 | 192 | if (type != MAP__FUNCTION) |
237a7e04 | 193 | dso__set_loaded(dso, map->type); |
8d92c02a | 194 | } |
66e274f3 | 195 | } |
237a7e04 | 196 | return map; |
66e274f3 | 197 | out_delete: |
237a7e04 | 198 | free(map); |
66e274f3 FW |
199 | return NULL; |
200 | } | |
201 | ||
e5a1845f NK |
202 | /* |
203 | * Constructor variant for modules (where we know from /proc/modules where | |
204 | * they are loaded) and for vmlinux, where only after we load all the | |
205 | * symbols we'll know where it starts and ends. | |
206 | */ | |
207 | struct map *map__new2(u64 start, struct dso *dso, enum map_type type) | |
208 | { | |
209 | struct map *map = calloc(1, (sizeof(*map) + | |
210 | (dso->kernel ? sizeof(struct kmap) : 0))); | |
211 | if (map != NULL) { | |
212 | /* | |
213 | * ->end will be filled after we load all the symbols | |
214 | */ | |
215 | map__init(map, type, start, 0, 0, dso); | |
216 | } | |
217 | ||
218 | return map; | |
219 | } | |
220 | ||
237a7e04 | 221 | void map__delete(struct map *map) |
c338aee8 | 222 | { |
237a7e04 | 223 | free(map); |
c338aee8 ACM |
224 | } |
225 | ||
237a7e04 | 226 | void map__fixup_start(struct map *map) |
c338aee8 | 227 | { |
237a7e04 | 228 | struct rb_root *symbols = &map->dso->symbols[map->type]; |
fcf1203a | 229 | struct rb_node *nd = rb_first(symbols); |
c338aee8 ACM |
230 | if (nd != NULL) { |
231 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
237a7e04 | 232 | map->start = sym->start; |
c338aee8 ACM |
233 | } |
234 | } | |
235 | ||
237a7e04 | 236 | void map__fixup_end(struct map *map) |
c338aee8 | 237 | { |
237a7e04 | 238 | struct rb_root *symbols = &map->dso->symbols[map->type]; |
fcf1203a | 239 | struct rb_node *nd = rb_last(symbols); |
c338aee8 ACM |
240 | if (nd != NULL) { |
241 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
237a7e04 | 242 | map->end = sym->end; |
c338aee8 ACM |
243 | } |
244 | } | |
245 | ||
d70a5402 ACM |
246 | #define DSO__DELETED "(deleted)" |
247 | ||
237a7e04 | 248 | int map__load(struct map *map, symbol_filter_t filter) |
66bd8424 | 249 | { |
237a7e04 | 250 | const char *name = map->dso->long_name; |
a128168d | 251 | int nr; |
79406cd7 | 252 | |
237a7e04 | 253 | if (dso__loaded(map->dso, map->type)) |
a128168d MH |
254 | return 0; |
255 | ||
237a7e04 | 256 | nr = dso__load(map->dso, map, filter); |
79406cd7 | 257 | if (nr < 0) { |
237a7e04 | 258 | if (map->dso->has_build_id) { |
79406cd7 ACM |
259 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
260 | ||
237a7e04 ACM |
261 | build_id__sprintf(map->dso->build_id, |
262 | sizeof(map->dso->build_id), | |
79406cd7 ACM |
263 | sbuild_id); |
264 | pr_warning("%s with build id %s not found", | |
265 | name, sbuild_id); | |
266 | } else | |
267 | pr_warning("Failed to open %s", name); | |
268 | ||
269 | pr_warning(", continuing without symbols\n"); | |
270 | return -1; | |
271 | } else if (nr == 0) { | |
89fe808a | 272 | #ifdef HAVE_LIBELF_SUPPORT |
79406cd7 ACM |
273 | const size_t len = strlen(name); |
274 | const size_t real_len = len - sizeof(DSO__DELETED); | |
275 | ||
276 | if (len > sizeof(DSO__DELETED) && | |
277 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { | |
e77b15bd DA |
278 | pr_warning("%.*s was updated (is prelink enabled?). " |
279 | "Restart the long running apps that use it!\n", | |
79406cd7 ACM |
280 | (int)real_len, name); |
281 | } else { | |
282 | pr_warning("no symbols found in %s, maybe install " | |
283 | "a debug package?\n", name); | |
66bd8424 | 284 | } |
393be2e3 | 285 | #endif |
79406cd7 | 286 | return -1; |
66bd8424 ACM |
287 | } |
288 | ||
79406cd7 ACM |
289 | return 0; |
290 | } | |
291 | ||
237a7e04 | 292 | struct symbol *map__find_symbol(struct map *map, u64 addr, |
9de89fe7 | 293 | symbol_filter_t filter) |
79406cd7 | 294 | { |
237a7e04 | 295 | if (map__load(map, filter) < 0) |
79406cd7 ACM |
296 | return NULL; |
297 | ||
237a7e04 | 298 | return dso__find_symbol(map->dso, map->type, addr); |
66bd8424 ACM |
299 | } |
300 | ||
237a7e04 | 301 | struct symbol *map__find_symbol_by_name(struct map *map, const char *name, |
79406cd7 ACM |
302 | symbol_filter_t filter) |
303 | { | |
237a7e04 | 304 | if (map__load(map, filter) < 0) |
79406cd7 ACM |
305 | return NULL; |
306 | ||
237a7e04 ACM |
307 | if (!dso__sorted_by_name(map->dso, map->type)) |
308 | dso__sort_by_name(map->dso, map->type); | |
79406cd7 | 309 | |
237a7e04 | 310 | return dso__find_symbol_by_name(map->dso, map->type, name); |
79406cd7 ACM |
311 | } |
312 | ||
237a7e04 | 313 | struct map *map__clone(struct map *map) |
66e274f3 | 314 | { |
8e16017d | 315 | return memdup(map, sizeof(*map)); |
66e274f3 FW |
316 | } |
317 | ||
318 | int map__overlap(struct map *l, struct map *r) | |
319 | { | |
320 | if (l->start > r->start) { | |
321 | struct map *t = l; | |
322 | l = r; | |
323 | r = t; | |
324 | } | |
325 | ||
326 | if (l->end > r->start) | |
327 | return 1; | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
237a7e04 | 332 | size_t map__fprintf(struct map *map, FILE *fp) |
66e274f3 | 333 | { |
9486aa38 | 334 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
237a7e04 | 335 | map->start, map->end, map->pgoff, map->dso->name); |
66e274f3 | 336 | } |
7a2b6209 | 337 | |
547a92e0 AN |
338 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
339 | { | |
8f28f19a | 340 | const char *dsoname = "[unknown]"; |
547a92e0 | 341 | |
0bc8d205 AN |
342 | if (map && map->dso && (map->dso->name || map->dso->long_name)) { |
343 | if (symbol_conf.show_kernel_path && map->dso->long_name) | |
344 | dsoname = map->dso->long_name; | |
345 | else if (map->dso->name) | |
346 | dsoname = map->dso->name; | |
8f28f19a | 347 | } |
547a92e0 AN |
348 | |
349 | return fprintf(fp, "%s", dsoname); | |
350 | } | |
351 | ||
cc8fae1d AH |
352 | int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, |
353 | FILE *fp) | |
354 | { | |
355 | char *srcline; | |
356 | int ret = 0; | |
357 | ||
358 | if (map && map->dso) { | |
359 | srcline = get_srcline(map->dso, | |
360 | map__rip_2objdump(map, addr)); | |
361 | if (srcline != SRCLINE_UNKNOWN) | |
362 | ret = fprintf(fp, "%s%s", prefix, srcline); | |
363 | free_srcline(srcline); | |
364 | } | |
365 | return ret; | |
366 | } | |
367 | ||
1d5077bd AH |
368 | /** |
369 | * map__rip_2objdump - convert symbol start address to objdump address. | |
370 | * @map: memory map | |
371 | * @rip: symbol start address | |
372 | * | |
7a2b6209 | 373 | * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN. |
0131c4ec AH |
374 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is |
375 | * relative to section start. | |
1d5077bd AH |
376 | * |
377 | * Return: Address suitable for passing to "objdump --start-address=" | |
7a2b6209 KS |
378 | */ |
379 | u64 map__rip_2objdump(struct map *map, u64 rip) | |
380 | { | |
0131c4ec AH |
381 | if (!map->dso->adjust_symbols) |
382 | return rip; | |
383 | ||
384 | if (map->dso->rel) | |
385 | return rip - map->pgoff; | |
386 | ||
9176753d | 387 | return map->unmap_ip(map, rip) - map->reloc; |
7a2b6209 | 388 | } |
ee11b90b | 389 | |
1d5077bd AH |
390 | /** |
391 | * map__objdump_2mem - convert objdump address to a memory address. | |
392 | * @map: memory map | |
393 | * @ip: objdump address | |
394 | * | |
395 | * Closely related to map__rip_2objdump(), this function takes an address from | |
396 | * objdump and converts it to a memory address. Note this assumes that @map | |
397 | * contains the address. To be sure the result is valid, check it forwards | |
398 | * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip | |
399 | * | |
400 | * Return: Memory address. | |
401 | */ | |
402 | u64 map__objdump_2mem(struct map *map, u64 ip) | |
403 | { | |
404 | if (!map->dso->adjust_symbols) | |
405 | return map->unmap_ip(map, ip); | |
406 | ||
407 | if (map->dso->rel) | |
408 | return map->unmap_ip(map, ip + map->pgoff); | |
409 | ||
9176753d | 410 | return ip + map->reloc; |
1d5077bd AH |
411 | } |
412 | ||
98dfd55d | 413 | void map_groups__init(struct map_groups *mg) |
c6e718ff ACM |
414 | { |
415 | int i; | |
416 | for (i = 0; i < MAP__NR_TYPES; ++i) { | |
98dfd55d ACM |
417 | mg->maps[i] = RB_ROOT; |
418 | INIT_LIST_HEAD(&mg->removed_maps[i]); | |
c6e718ff | 419 | } |
98dfd55d | 420 | mg->machine = NULL; |
a26ca671 | 421 | mg->refcnt = 1; |
c6e718ff ACM |
422 | } |
423 | ||
98dfd55d | 424 | static void maps__delete(struct rb_root *maps) |
591765fd | 425 | { |
98dfd55d | 426 | struct rb_node *next = rb_first(maps); |
591765fd ACM |
427 | |
428 | while (next) { | |
429 | struct map *pos = rb_entry(next, struct map, rb_node); | |
430 | ||
431 | next = rb_next(&pos->rb_node); | |
98dfd55d | 432 | rb_erase(&pos->rb_node, maps); |
591765fd ACM |
433 | map__delete(pos); |
434 | } | |
435 | } | |
436 | ||
98dfd55d | 437 | static void maps__delete_removed(struct list_head *maps) |
591765fd ACM |
438 | { |
439 | struct map *pos, *n; | |
440 | ||
98dfd55d | 441 | list_for_each_entry_safe(pos, n, maps, node) { |
591765fd ACM |
442 | list_del(&pos->node); |
443 | map__delete(pos); | |
444 | } | |
445 | } | |
446 | ||
98dfd55d | 447 | void map_groups__exit(struct map_groups *mg) |
591765fd ACM |
448 | { |
449 | int i; | |
450 | ||
451 | for (i = 0; i < MAP__NR_TYPES; ++i) { | |
98dfd55d ACM |
452 | maps__delete(&mg->maps[i]); |
453 | maps__delete_removed(&mg->removed_maps[i]); | |
591765fd ACM |
454 | } |
455 | } | |
456 | ||
93d5731d ACM |
457 | struct map_groups *map_groups__new(void) |
458 | { | |
459 | struct map_groups *mg = malloc(sizeof(*mg)); | |
460 | ||
461 | if (mg != NULL) | |
462 | map_groups__init(mg); | |
463 | ||
464 | return mg; | |
465 | } | |
466 | ||
467 | void map_groups__delete(struct map_groups *mg) | |
468 | { | |
469 | map_groups__exit(mg); | |
470 | free(mg); | |
471 | } | |
472 | ||
a26ca671 ACM |
473 | void map_groups__put(struct map_groups *mg) |
474 | { | |
475 | if (--mg->refcnt == 0) | |
476 | map_groups__delete(mg); | |
477 | } | |
478 | ||
98dfd55d | 479 | void map_groups__flush(struct map_groups *mg) |
c6e718ff ACM |
480 | { |
481 | int type; | |
482 | ||
483 | for (type = 0; type < MAP__NR_TYPES; type++) { | |
98dfd55d | 484 | struct rb_root *root = &mg->maps[type]; |
c6e718ff ACM |
485 | struct rb_node *next = rb_first(root); |
486 | ||
487 | while (next) { | |
488 | struct map *pos = rb_entry(next, struct map, rb_node); | |
489 | next = rb_next(&pos->rb_node); | |
490 | rb_erase(&pos->rb_node, root); | |
491 | /* | |
492 | * We may have references to this map, for | |
493 | * instance in some hist_entry instances, so | |
494 | * just move them to a separate list. | |
495 | */ | |
98dfd55d | 496 | list_add_tail(&pos->node, &mg->removed_maps[pos->type]); |
c6e718ff ACM |
497 | } |
498 | } | |
499 | } | |
500 | ||
98dfd55d | 501 | struct symbol *map_groups__find_symbol(struct map_groups *mg, |
4b8cf846 | 502 | enum map_type type, u64 addr, |
7e5e1b14 | 503 | struct map **mapp, |
4b8cf846 ACM |
504 | symbol_filter_t filter) |
505 | { | |
98dfd55d | 506 | struct map *map = map_groups__find(mg, type, addr); |
4b8cf846 | 507 | |
4afc81cd MH |
508 | /* Ensure map is loaded before using map->map_ip */ |
509 | if (map != NULL && map__load(map, filter) >= 0) { | |
7e5e1b14 ACM |
510 | if (mapp != NULL) |
511 | *mapp = map; | |
4b8cf846 | 512 | return map__find_symbol(map, map->map_ip(map, addr), filter); |
7e5e1b14 ACM |
513 | } |
514 | ||
515 | return NULL; | |
516 | } | |
517 | ||
98dfd55d | 518 | struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg, |
7e5e1b14 ACM |
519 | enum map_type type, |
520 | const char *name, | |
521 | struct map **mapp, | |
522 | symbol_filter_t filter) | |
523 | { | |
524 | struct rb_node *nd; | |
525 | ||
98dfd55d | 526 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
7e5e1b14 ACM |
527 | struct map *pos = rb_entry(nd, struct map, rb_node); |
528 | struct symbol *sym = map__find_symbol_by_name(pos, name, filter); | |
529 | ||
530 | if (sym == NULL) | |
531 | continue; | |
532 | if (mapp != NULL) | |
533 | *mapp = pos; | |
534 | return sym; | |
535 | } | |
4b8cf846 ACM |
536 | |
537 | return NULL; | |
538 | } | |
539 | ||
4e987712 ACM |
540 | int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter) |
541 | { | |
542 | if (ams->addr < ams->map->start || ams->addr > ams->map->end) { | |
543 | if (ams->map->groups == NULL) | |
544 | return -1; | |
545 | ams->map = map_groups__find(ams->map->groups, ams->map->type, | |
546 | ams->addr); | |
547 | if (ams->map == NULL) | |
548 | return -1; | |
549 | } | |
550 | ||
551 | ams->al_addr = ams->map->map_ip(ams->map, ams->addr); | |
552 | ams->sym = map__find_symbol(ams->map, ams->al_addr, filter); | |
553 | ||
554 | return ams->sym ? 0 : -1; | |
555 | } | |
556 | ||
98dfd55d | 557 | size_t __map_groups__fprintf_maps(struct map_groups *mg, |
c6e718ff ACM |
558 | enum map_type type, int verbose, FILE *fp) |
559 | { | |
560 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); | |
561 | struct rb_node *nd; | |
562 | ||
98dfd55d | 563 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
c6e718ff ACM |
564 | struct map *pos = rb_entry(nd, struct map, rb_node); |
565 | printed += fprintf(fp, "Map:"); | |
566 | printed += map__fprintf(pos, fp); | |
567 | if (verbose > 2) { | |
568 | printed += dso__fprintf(pos->dso, type, fp); | |
569 | printed += fprintf(fp, "--\n"); | |
570 | } | |
571 | } | |
572 | ||
573 | return printed; | |
574 | } | |
575 | ||
98dfd55d | 576 | size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp) |
c6e718ff ACM |
577 | { |
578 | size_t printed = 0, i; | |
579 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
98dfd55d | 580 | printed += __map_groups__fprintf_maps(mg, i, verbose, fp); |
c6e718ff ACM |
581 | return printed; |
582 | } | |
583 | ||
98dfd55d | 584 | static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg, |
c6e718ff ACM |
585 | enum map_type type, |
586 | int verbose, FILE *fp) | |
587 | { | |
588 | struct map *pos; | |
589 | size_t printed = 0; | |
590 | ||
98dfd55d | 591 | list_for_each_entry(pos, &mg->removed_maps[type], node) { |
c6e718ff ACM |
592 | printed += fprintf(fp, "Map:"); |
593 | printed += map__fprintf(pos, fp); | |
594 | if (verbose > 1) { | |
595 | printed += dso__fprintf(pos->dso, type, fp); | |
596 | printed += fprintf(fp, "--\n"); | |
597 | } | |
598 | } | |
599 | return printed; | |
600 | } | |
601 | ||
98dfd55d | 602 | static size_t map_groups__fprintf_removed_maps(struct map_groups *mg, |
c6e718ff ACM |
603 | int verbose, FILE *fp) |
604 | { | |
605 | size_t printed = 0, i; | |
606 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
98dfd55d | 607 | printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp); |
c6e718ff ACM |
608 | return printed; |
609 | } | |
610 | ||
98dfd55d | 611 | size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp) |
c6e718ff | 612 | { |
98dfd55d | 613 | size_t printed = map_groups__fprintf_maps(mg, verbose, fp); |
c6e718ff | 614 | printed += fprintf(fp, "Removed maps:\n"); |
98dfd55d | 615 | return printed + map_groups__fprintf_removed_maps(mg, verbose, fp); |
c6e718ff ACM |
616 | } |
617 | ||
98dfd55d | 618 | int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, |
c6e718ff ACM |
619 | int verbose, FILE *fp) |
620 | { | |
98dfd55d | 621 | struct rb_root *root = &mg->maps[map->type]; |
c6e718ff | 622 | struct rb_node *next = rb_first(root); |
0a1eae39 | 623 | int err = 0; |
c6e718ff ACM |
624 | |
625 | while (next) { | |
626 | struct map *pos = rb_entry(next, struct map, rb_node); | |
627 | next = rb_next(&pos->rb_node); | |
628 | ||
629 | if (!map__overlap(pos, map)) | |
630 | continue; | |
631 | ||
632 | if (verbose >= 2) { | |
633 | fputs("overlapping maps:\n", fp); | |
634 | map__fprintf(map, fp); | |
635 | map__fprintf(pos, fp); | |
636 | } | |
637 | ||
638 | rb_erase(&pos->rb_node, root); | |
c6e718ff ACM |
639 | /* |
640 | * Now check if we need to create new maps for areas not | |
641 | * overlapped by the new map: | |
642 | */ | |
643 | if (map->start > pos->start) { | |
644 | struct map *before = map__clone(pos); | |
645 | ||
0a1eae39 ACM |
646 | if (before == NULL) { |
647 | err = -ENOMEM; | |
648 | goto move_map; | |
649 | } | |
c6e718ff ACM |
650 | |
651 | before->end = map->start - 1; | |
98dfd55d | 652 | map_groups__insert(mg, before); |
c6e718ff ACM |
653 | if (verbose >= 2) |
654 | map__fprintf(before, fp); | |
655 | } | |
656 | ||
657 | if (map->end < pos->end) { | |
658 | struct map *after = map__clone(pos); | |
659 | ||
0a1eae39 ACM |
660 | if (after == NULL) { |
661 | err = -ENOMEM; | |
662 | goto move_map; | |
663 | } | |
c6e718ff ACM |
664 | |
665 | after->start = map->end + 1; | |
98dfd55d | 666 | map_groups__insert(mg, after); |
c6e718ff ACM |
667 | if (verbose >= 2) |
668 | map__fprintf(after, fp); | |
669 | } | |
0a1eae39 ACM |
670 | move_map: |
671 | /* | |
672 | * If we have references, just move them to a separate list. | |
673 | */ | |
674 | if (pos->referenced) | |
98dfd55d | 675 | list_add_tail(&pos->node, &mg->removed_maps[map->type]); |
0a1eae39 ACM |
676 | else |
677 | map__delete(pos); | |
678 | ||
679 | if (err) | |
680 | return err; | |
c6e718ff ACM |
681 | } |
682 | ||
683 | return 0; | |
684 | } | |
685 | ||
686 | /* | |
687 | * XXX This should not really _copy_ te maps, but refcount them. | |
688 | */ | |
98dfd55d | 689 | int map_groups__clone(struct map_groups *mg, |
c6e718ff ACM |
690 | struct map_groups *parent, enum map_type type) |
691 | { | |
692 | struct rb_node *nd; | |
693 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { | |
694 | struct map *map = rb_entry(nd, struct map, rb_node); | |
695 | struct map *new = map__clone(map); | |
696 | if (new == NULL) | |
697 | return -ENOMEM; | |
98dfd55d | 698 | map_groups__insert(mg, new); |
c6e718ff ACM |
699 | } |
700 | return 0; | |
701 | } | |
702 | ||
4b8cf846 ACM |
703 | void maps__insert(struct rb_root *maps, struct map *map) |
704 | { | |
705 | struct rb_node **p = &maps->rb_node; | |
706 | struct rb_node *parent = NULL; | |
707 | const u64 ip = map->start; | |
708 | struct map *m; | |
709 | ||
710 | while (*p != NULL) { | |
711 | parent = *p; | |
712 | m = rb_entry(parent, struct map, rb_node); | |
713 | if (ip < m->start) | |
714 | p = &(*p)->rb_left; | |
715 | else | |
716 | p = &(*p)->rb_right; | |
717 | } | |
718 | ||
719 | rb_link_node(&map->rb_node, parent, p); | |
720 | rb_insert_color(&map->rb_node, maps); | |
721 | } | |
722 | ||
237a7e04 | 723 | void maps__remove(struct rb_root *maps, struct map *map) |
076c6e45 | 724 | { |
237a7e04 | 725 | rb_erase(&map->rb_node, maps); |
076c6e45 ACM |
726 | } |
727 | ||
4b8cf846 ACM |
728 | struct map *maps__find(struct rb_root *maps, u64 ip) |
729 | { | |
730 | struct rb_node **p = &maps->rb_node; | |
731 | struct rb_node *parent = NULL; | |
732 | struct map *m; | |
733 | ||
734 | while (*p != NULL) { | |
735 | parent = *p; | |
736 | m = rb_entry(parent, struct map, rb_node); | |
737 | if (ip < m->start) | |
738 | p = &(*p)->rb_left; | |
739 | else if (ip > m->end) | |
740 | p = &(*p)->rb_right; | |
741 | else | |
742 | return m; | |
743 | } | |
744 | ||
745 | return NULL; | |
746 | } | |
8e0cf965 AH |
747 | |
748 | struct map *maps__first(struct rb_root *maps) | |
749 | { | |
750 | struct rb_node *first = rb_first(maps); | |
751 | ||
752 | if (first) | |
753 | return rb_entry(first, struct map, rb_node); | |
754 | return NULL; | |
755 | } | |
756 | ||
757 | struct map *maps__next(struct map *map) | |
758 | { | |
759 | struct rb_node *next = rb_next(&map->rb_node); | |
760 | ||
761 | if (next) | |
762 | return rb_entry(next, struct map, rb_node); | |
763 | return NULL; | |
764 | } |