]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
66e274f3 | 2 | #include "symbol.h" |
c6e718ff | 3 | #include <errno.h> |
9486aa38 | 4 | #include <inttypes.h> |
4b8cf846 | 5 | #include <limits.h> |
66e274f3 FW |
6 | #include <stdlib.h> |
7 | #include <string.h> | |
8 | #include <stdio.h> | |
a1645ce1 | 9 | #include <unistd.h> |
fbef103f | 10 | #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ |
4b8cf846 | 11 | #include "map.h" |
5cd95c2d | 12 | #include "thread.h" |
7dbf4dcf | 13 | #include "vdso.h" |
ebb296c2 | 14 | #include "build-id.h" |
cc8fae1d | 15 | #include "util.h" |
acebd408 | 16 | #include "debug.h" |
2a03068c | 17 | #include "machine.h" |
8e16017d | 18 | #include <linux/string.h> |
632a5cab | 19 | #include "srcline.h" |
843ff37b | 20 | #include "namespaces.h" |
6c502584 | 21 | #include "unwind.h" |
dd2e18e9 | 22 | #include "srccode.h" |
66e274f3 | 23 | |
6a2ffcdd | 24 | static void __maps__insert(struct maps *maps, struct map *map); |
1e628569 | 25 | static void __maps__insert_name(struct maps *maps, struct map *map); |
6a2ffcdd | 26 | |
0ac3348e | 27 | static inline int is_anon_memory(const char *filename, u32 flags) |
66e274f3 | 28 | { |
fbef103f | 29 | return flags & MAP_HUGETLB || |
0ac3348e | 30 | !strcmp(filename, "//anon") || |
b2be5451 YB |
31 | !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) || |
32 | !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1); | |
66e274f3 FW |
33 | } |
34 | ||
87ffef79 JO |
35 | static inline int is_no_dso_memory(const char *filename) |
36 | { | |
1e82574d | 37 | return !strncmp(filename, "[stack", 6) || |
700be564 | 38 | !strncmp(filename, "/SYSV",5) || |
87ffef79 JO |
39 | !strcmp(filename, "[heap]"); |
40 | } | |
41 | ||
eca81836 ML |
42 | static inline int is_android_lib(const char *filename) |
43 | { | |
44 | return !strncmp(filename, "/data/app-lib", 13) || | |
45 | !strncmp(filename, "/system/lib", 11); | |
46 | } | |
47 | ||
48 | static inline bool replace_android_lib(const char *filename, char *newfilename) | |
49 | { | |
50 | const char *libname; | |
51 | char *app_abi; | |
52 | size_t app_abi_length, new_length; | |
53 | size_t lib_length = 0; | |
54 | ||
55 | libname = strrchr(filename, '/'); | |
56 | if (libname) | |
57 | lib_length = strlen(libname); | |
58 | ||
59 | app_abi = getenv("APP_ABI"); | |
60 | if (!app_abi) | |
61 | return false; | |
62 | ||
63 | app_abi_length = strlen(app_abi); | |
64 | ||
65 | if (!strncmp(filename, "/data/app-lib", 13)) { | |
66 | char *apk_path; | |
67 | ||
68 | if (!app_abi_length) | |
69 | return false; | |
70 | ||
71 | new_length = 7 + app_abi_length + lib_length; | |
72 | ||
73 | apk_path = getenv("APK_PATH"); | |
74 | if (apk_path) { | |
75 | new_length += strlen(apk_path) + 1; | |
76 | if (new_length > PATH_MAX) | |
77 | return false; | |
78 | snprintf(newfilename, new_length, | |
79 | "%s/libs/%s/%s", apk_path, app_abi, libname); | |
80 | } else { | |
81 | if (new_length > PATH_MAX) | |
82 | return false; | |
83 | snprintf(newfilename, new_length, | |
84 | "libs/%s/%s", app_abi, libname); | |
85 | } | |
86 | return true; | |
87 | } | |
88 | ||
89 | if (!strncmp(filename, "/system/lib/", 11)) { | |
90 | char *ndk, *app; | |
91 | const char *arch; | |
92 | size_t ndk_length; | |
93 | size_t app_length; | |
94 | ||
95 | ndk = getenv("NDK_ROOT"); | |
96 | app = getenv("APP_PLATFORM"); | |
97 | ||
98 | if (!(ndk && app)) | |
99 | return false; | |
100 | ||
101 | ndk_length = strlen(ndk); | |
102 | app_length = strlen(app); | |
103 | ||
104 | if (!(ndk_length && app_length && app_abi_length)) | |
105 | return false; | |
106 | ||
107 | arch = !strncmp(app_abi, "arm", 3) ? "arm" : | |
108 | !strncmp(app_abi, "mips", 4) ? "mips" : | |
109 | !strncmp(app_abi, "x86", 3) ? "x86" : NULL; | |
110 | ||
111 | if (!arch) | |
112 | return false; | |
113 | ||
114 | new_length = 27 + ndk_length + | |
115 | app_length + lib_length | |
116 | + strlen(arch); | |
117 | ||
118 | if (new_length > PATH_MAX) | |
119 | return false; | |
120 | snprintf(newfilename, new_length, | |
121 | "%s/platforms/%s/arch-%s/usr/lib/%s", | |
122 | ndk, app, arch, libname); | |
123 | ||
124 | return true; | |
125 | } | |
126 | return false; | |
127 | } | |
128 | ||
3183f8ca | 129 | void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) |
afb7b4f0 | 130 | { |
237a7e04 ACM |
131 | map->start = start; |
132 | map->end = end; | |
133 | map->pgoff = pgoff; | |
9176753d | 134 | map->reloc = 0; |
d3a7c489 | 135 | map->dso = dso__get(dso); |
237a7e04 ACM |
136 | map->map_ip = map__map_ip; |
137 | map->unmap_ip = map__unmap_ip; | |
138 | RB_CLEAR_NODE(&map->rb_node); | |
139 | map->groups = NULL; | |
237a7e04 | 140 | map->erange_warned = false; |
e3a42cdd | 141 | refcount_set(&map->refcnt, 1); |
afb7b4f0 ACM |
142 | } |
143 | ||
2a03068c | 144 | struct map *map__new(struct machine *machine, u64 start, u64 len, |
bf2e710b | 145 | u64 pgoff, u32 d_maj, u32 d_min, u64 ino, |
7ef80703 | 146 | u64 ino_gen, u32 prot, u32 flags, char *filename, |
3183f8ca | 147 | struct thread *thread) |
66e274f3 | 148 | { |
237a7e04 | 149 | struct map *map = malloc(sizeof(*map)); |
bf2e710b KJ |
150 | struct nsinfo *nsi = NULL; |
151 | struct nsinfo *nnsi; | |
66e274f3 | 152 | |
237a7e04 | 153 | if (map != NULL) { |
66e274f3 | 154 | char newfilename[PATH_MAX]; |
afb7b4f0 | 155 | struct dso *dso; |
eca81836 | 156 | int anon, no_dso, vdso, android; |
66e274f3 | 157 | |
eca81836 | 158 | android = is_android_lib(filename); |
0ac3348e | 159 | anon = is_anon_memory(filename, flags); |
7dbf4dcf | 160 | vdso = is_vdso_map(filename); |
87ffef79 | 161 | no_dso = is_no_dso_memory(filename); |
66e274f3 | 162 | |
5c5e854b SE |
163 | map->maj = d_maj; |
164 | map->min = d_min; | |
165 | map->ino = ino; | |
166 | map->ino_generation = ino_gen; | |
7ef80703 DZ |
167 | map->prot = prot; |
168 | map->flags = flags; | |
bf2e710b | 169 | nsi = nsinfo__get(thread->nsinfo); |
5c5e854b | 170 | |
d183b261 | 171 | if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) { |
bf2e710b KJ |
172 | snprintf(newfilename, sizeof(newfilename), |
173 | "/tmp/perf-%d.map", nsi->pid); | |
66e274f3 FW |
174 | filename = newfilename; |
175 | } | |
176 | ||
eca81836 ML |
177 | if (android) { |
178 | if (replace_android_lib(filename, newfilename)) | |
179 | filename = newfilename; | |
180 | } | |
181 | ||
7dbf4dcf | 182 | if (vdso) { |
bf2e710b KJ |
183 | /* The vdso maps are always on the host and not the |
184 | * container. Ensure that we don't use setns to look | |
185 | * them up. | |
186 | */ | |
187 | nnsi = nsinfo__copy(nsi); | |
188 | if (nnsi) { | |
189 | nsinfo__put(nsi); | |
190 | nnsi->need_setns = false; | |
191 | nsi = nnsi; | |
192 | } | |
7dbf4dcf | 193 | pgoff = 0; |
9a4388c7 | 194 | dso = machine__findnew_vdso(machine, thread); |
7dbf4dcf | 195 | } else |
aa7cc2ae | 196 | dso = machine__findnew_dso(machine, filename); |
7dbf4dcf | 197 | |
afb7b4f0 | 198 | if (dso == NULL) |
66e274f3 FW |
199 | goto out_delete; |
200 | ||
3183f8ca | 201 | map__init(map, start, start + len, pgoff, dso); |
afb7b4f0 | 202 | |
87ffef79 | 203 | if (anon || no_dso) { |
237a7e04 | 204 | map->map_ip = map->unmap_ip = identity__map_ip; |
87ffef79 JO |
205 | |
206 | /* | |
207 | * Set memory without DSO as loaded. All map__find_* | |
208 | * functions still return NULL, and we avoid the | |
209 | * unnecessary map__load warning. | |
210 | */ | |
d183b261 | 211 | if (!(prot & PROT_EXEC)) |
3183f8ca | 212 | dso__set_loaded(dso); |
8d92c02a | 213 | } |
bf2e710b | 214 | dso->nsinfo = nsi; |
d3a7c489 | 215 | dso__put(dso); |
66e274f3 | 216 | } |
237a7e04 | 217 | return map; |
66e274f3 | 218 | out_delete: |
bf2e710b | 219 | nsinfo__put(nsi); |
237a7e04 | 220 | free(map); |
66e274f3 FW |
221 | return NULL; |
222 | } | |
223 | ||
e5a1845f NK |
224 | /* |
225 | * Constructor variant for modules (where we know from /proc/modules where | |
226 | * they are loaded) and for vmlinux, where only after we load all the | |
227 | * symbols we'll know where it starts and ends. | |
228 | */ | |
3183f8ca | 229 | struct map *map__new2(u64 start, struct dso *dso) |
e5a1845f NK |
230 | { |
231 | struct map *map = calloc(1, (sizeof(*map) + | |
232 | (dso->kernel ? sizeof(struct kmap) : 0))); | |
233 | if (map != NULL) { | |
234 | /* | |
235 | * ->end will be filled after we load all the symbols | |
236 | */ | |
3183f8ca | 237 | map__init(map, start, 0, 0, dso); |
e5a1845f NK |
238 | } |
239 | ||
240 | return map; | |
241 | } | |
242 | ||
e6ce7126 ACM |
243 | /* |
244 | * Use this and __map__is_kmodule() for map instances that are in | |
245 | * machine->kmaps, and thus have map->groups->machine all properly set, to | |
246 | * disambiguate between the kernel and modules. | |
247 | * | |
248 | * When the need arises, introduce map__is_{kernel,kmodule)() that | |
249 | * checks (map->groups != NULL && map->groups->machine != NULL && | |
250 | * map->dso->kernel) before calling __map__is_{kernel,kmodule}()) | |
251 | */ | |
252 | bool __map__is_kernel(const struct map *map) | |
253 | { | |
3183f8ca | 254 | return machine__kernel_map(map->groups->machine) == map; |
e6ce7126 ACM |
255 | } |
256 | ||
5759a682 AH |
257 | bool __map__is_extra_kernel_map(const struct map *map) |
258 | { | |
259 | struct kmap *kmap = __map__kmap((struct map *)map); | |
260 | ||
261 | return kmap && kmap->name[0]; | |
262 | } | |
263 | ||
e94b861a ACM |
264 | bool map__has_symbols(const struct map *map) |
265 | { | |
3183f8ca | 266 | return dso__has_symbols(map->dso); |
e94b861a ACM |
267 | } |
268 | ||
d3a7c489 | 269 | static void map__exit(struct map *map) |
c338aee8 | 270 | { |
facf3f06 | 271 | BUG_ON(!RB_EMPTY_NODE(&map->rb_node)); |
d3a7c489 ACM |
272 | dso__zput(map->dso); |
273 | } | |
274 | ||
275 | void map__delete(struct map *map) | |
276 | { | |
277 | map__exit(map); | |
237a7e04 | 278 | free(map); |
c338aee8 ACM |
279 | } |
280 | ||
84c2cafa ACM |
281 | void map__put(struct map *map) |
282 | { | |
e3a42cdd | 283 | if (map && refcount_dec_and_test(&map->refcnt)) |
84c2cafa ACM |
284 | map__delete(map); |
285 | } | |
286 | ||
237a7e04 | 287 | void map__fixup_start(struct map *map) |
c338aee8 | 288 | { |
3183f8ca | 289 | struct rb_root *symbols = &map->dso->symbols; |
fcf1203a | 290 | struct rb_node *nd = rb_first(symbols); |
c338aee8 ACM |
291 | if (nd != NULL) { |
292 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
237a7e04 | 293 | map->start = sym->start; |
c338aee8 ACM |
294 | } |
295 | } | |
296 | ||
237a7e04 | 297 | void map__fixup_end(struct map *map) |
c338aee8 | 298 | { |
3183f8ca | 299 | struct rb_root *symbols = &map->dso->symbols; |
fcf1203a | 300 | struct rb_node *nd = rb_last(symbols); |
c338aee8 ACM |
301 | if (nd != NULL) { |
302 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
237a7e04 | 303 | map->end = sym->end; |
c338aee8 ACM |
304 | } |
305 | } | |
306 | ||
d70a5402 ACM |
307 | #define DSO__DELETED "(deleted)" |
308 | ||
be39db9f | 309 | int map__load(struct map *map) |
66bd8424 | 310 | { |
237a7e04 | 311 | const char *name = map->dso->long_name; |
a128168d | 312 | int nr; |
79406cd7 | 313 | |
3183f8ca | 314 | if (dso__loaded(map->dso)) |
a128168d MH |
315 | return 0; |
316 | ||
be39db9f | 317 | nr = dso__load(map->dso, map); |
79406cd7 | 318 | if (nr < 0) { |
237a7e04 | 319 | if (map->dso->has_build_id) { |
b5d8bbe8 | 320 | char sbuild_id[SBUILD_ID_SIZE]; |
79406cd7 | 321 | |
237a7e04 ACM |
322 | build_id__sprintf(map->dso->build_id, |
323 | sizeof(map->dso->build_id), | |
79406cd7 | 324 | sbuild_id); |
d8e75a11 | 325 | pr_debug("%s with build id %s not found", name, sbuild_id); |
79406cd7 | 326 | } else |
d8e75a11 | 327 | pr_debug("Failed to open %s", name); |
79406cd7 | 328 | |
d8e75a11 | 329 | pr_debug(", continuing without symbols\n"); |
79406cd7 ACM |
330 | return -1; |
331 | } else if (nr == 0) { | |
89fe808a | 332 | #ifdef HAVE_LIBELF_SUPPORT |
79406cd7 ACM |
333 | const size_t len = strlen(name); |
334 | const size_t real_len = len - sizeof(DSO__DELETED); | |
335 | ||
336 | if (len > sizeof(DSO__DELETED) && | |
337 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { | |
d8e75a11 | 338 | pr_debug("%.*s was updated (is prelink enabled?). " |
e77b15bd | 339 | "Restart the long running apps that use it!\n", |
79406cd7 ACM |
340 | (int)real_len, name); |
341 | } else { | |
d8e75a11 | 342 | pr_debug("no symbols found in %s, maybe install a debug package?\n", name); |
66bd8424 | 343 | } |
393be2e3 | 344 | #endif |
79406cd7 | 345 | return -1; |
66bd8424 ACM |
346 | } |
347 | ||
79406cd7 ACM |
348 | return 0; |
349 | } | |
350 | ||
be39db9f | 351 | struct symbol *map__find_symbol(struct map *map, u64 addr) |
79406cd7 | 352 | { |
be39db9f | 353 | if (map__load(map) < 0) |
79406cd7 ACM |
354 | return NULL; |
355 | ||
3183f8ca | 356 | return dso__find_symbol(map->dso, addr); |
66bd8424 ACM |
357 | } |
358 | ||
be39db9f | 359 | struct symbol *map__find_symbol_by_name(struct map *map, const char *name) |
79406cd7 | 360 | { |
be39db9f | 361 | if (map__load(map) < 0) |
79406cd7 ACM |
362 | return NULL; |
363 | ||
3183f8ca ACM |
364 | if (!dso__sorted_by_name(map->dso)) |
365 | dso__sort_by_name(map->dso); | |
79406cd7 | 366 | |
3183f8ca | 367 | return dso__find_symbol_by_name(map->dso, name); |
79406cd7 ACM |
368 | } |
369 | ||
66671d00 | 370 | struct map *map__clone(struct map *from) |
66e274f3 | 371 | { |
66671d00 ACM |
372 | struct map *map = memdup(from, sizeof(*map)); |
373 | ||
374 | if (map != NULL) { | |
e3a42cdd | 375 | refcount_set(&map->refcnt, 1); |
66671d00 ACM |
376 | RB_CLEAR_NODE(&map->rb_node); |
377 | dso__get(map->dso); | |
378 | map->groups = NULL; | |
379 | } | |
380 | ||
381 | return map; | |
66e274f3 FW |
382 | } |
383 | ||
237a7e04 | 384 | size_t map__fprintf(struct map *map, FILE *fp) |
66e274f3 | 385 | { |
9486aa38 | 386 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
237a7e04 | 387 | map->start, map->end, map->pgoff, map->dso->name); |
66e274f3 | 388 | } |
7a2b6209 | 389 | |
547a92e0 AN |
390 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
391 | { | |
8f28f19a | 392 | const char *dsoname = "[unknown]"; |
547a92e0 | 393 | |
5eae7d84 | 394 | if (map && map->dso) { |
0bc8d205 AN |
395 | if (symbol_conf.show_kernel_path && map->dso->long_name) |
396 | dsoname = map->dso->long_name; | |
5eae7d84 | 397 | else |
0bc8d205 | 398 | dsoname = map->dso->name; |
8f28f19a | 399 | } |
547a92e0 AN |
400 | |
401 | return fprintf(fp, "%s", dsoname); | |
402 | } | |
403 | ||
e2d88aaa ACM |
404 | char *map__srcline(struct map *map, u64 addr, struct symbol *sym) |
405 | { | |
406 | if (map == NULL) | |
407 | return SRCLINE_UNKNOWN; | |
408 | return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr); | |
409 | } | |
410 | ||
cc8fae1d AH |
411 | int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, |
412 | FILE *fp) | |
413 | { | |
cc8fae1d AH |
414 | int ret = 0; |
415 | ||
416 | if (map && map->dso) { | |
e2d88aaa | 417 | char *srcline = map__srcline(map, addr, NULL); |
cc8fae1d AH |
418 | if (srcline != SRCLINE_UNKNOWN) |
419 | ret = fprintf(fp, "%s%s", prefix, srcline); | |
420 | free_srcline(srcline); | |
421 | } | |
422 | return ret; | |
423 | } | |
424 | ||
dd2e18e9 AK |
425 | int map__fprintf_srccode(struct map *map, u64 addr, |
426 | FILE *fp, | |
427 | struct srccode_state *state) | |
428 | { | |
429 | char *srcfile; | |
430 | int ret = 0; | |
431 | unsigned line; | |
432 | int len; | |
433 | char *srccode; | |
434 | ||
435 | if (!map || !map->dso) | |
436 | return 0; | |
437 | srcfile = get_srcline_split(map->dso, | |
438 | map__rip_2objdump(map, addr), | |
439 | &line); | |
440 | if (!srcfile) | |
441 | return 0; | |
442 | ||
443 | /* Avoid redundant printing */ | |
444 | if (state && | |
445 | state->srcfile && | |
446 | !strcmp(state->srcfile, srcfile) && | |
447 | state->line == line) { | |
448 | free(srcfile); | |
449 | return 0; | |
450 | } | |
451 | ||
452 | srccode = find_sourceline(srcfile, line, &len); | |
453 | if (!srccode) | |
454 | goto out_free_line; | |
455 | ||
456 | ret = fprintf(fp, "|%-8d %.*s", line, len, srccode); | |
457 | state->srcfile = srcfile; | |
458 | state->line = line; | |
459 | return ret; | |
460 | ||
461 | out_free_line: | |
462 | free(srcfile); | |
463 | return ret; | |
464 | } | |
465 | ||
466 | ||
467 | void srccode_state_free(struct srccode_state *state) | |
468 | { | |
469 | zfree(&state->srcfile); | |
470 | state->line = 0; | |
471 | } | |
472 | ||
1d5077bd AH |
473 | /** |
474 | * map__rip_2objdump - convert symbol start address to objdump address. | |
475 | * @map: memory map | |
476 | * @rip: symbol start address | |
477 | * | |
7a2b6209 | 478 | * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN. |
0131c4ec AH |
479 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is |
480 | * relative to section start. | |
1d5077bd AH |
481 | * |
482 | * Return: Address suitable for passing to "objdump --start-address=" | |
7a2b6209 KS |
483 | */ |
484 | u64 map__rip_2objdump(struct map *map, u64 rip) | |
485 | { | |
97802f3b AH |
486 | struct kmap *kmap = __map__kmap(map); |
487 | ||
488 | /* | |
489 | * vmlinux does not have program headers for PTI entry trampolines and | |
490 | * kcore may not either. However the trampoline object code is on the | |
491 | * main kernel map, so just use that instead. | |
492 | */ | |
493 | if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) { | |
494 | struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine); | |
495 | ||
496 | if (kernel_map) | |
497 | map = kernel_map; | |
498 | } | |
499 | ||
0131c4ec AH |
500 | if (!map->dso->adjust_symbols) |
501 | return rip; | |
502 | ||
503 | if (map->dso->rel) | |
504 | return rip - map->pgoff; | |
505 | ||
a58f7033 WN |
506 | /* |
507 | * kernel modules also have DSO_TYPE_USER in dso->kernel, | |
508 | * but all kernel modules are ET_REL, so won't get here. | |
509 | */ | |
510 | if (map->dso->kernel == DSO_TYPE_USER) | |
511 | return rip + map->dso->text_offset; | |
512 | ||
9176753d | 513 | return map->unmap_ip(map, rip) - map->reloc; |
7a2b6209 | 514 | } |
ee11b90b | 515 | |
1d5077bd AH |
516 | /** |
517 | * map__objdump_2mem - convert objdump address to a memory address. | |
518 | * @map: memory map | |
519 | * @ip: objdump address | |
520 | * | |
521 | * Closely related to map__rip_2objdump(), this function takes an address from | |
522 | * objdump and converts it to a memory address. Note this assumes that @map | |
523 | * contains the address. To be sure the result is valid, check it forwards | |
524 | * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip | |
525 | * | |
526 | * Return: Memory address. | |
527 | */ | |
528 | u64 map__objdump_2mem(struct map *map, u64 ip) | |
529 | { | |
530 | if (!map->dso->adjust_symbols) | |
531 | return map->unmap_ip(map, ip); | |
532 | ||
533 | if (map->dso->rel) | |
534 | return map->unmap_ip(map, ip + map->pgoff); | |
535 | ||
a58f7033 WN |
536 | /* |
537 | * kernel modules also have DSO_TYPE_USER in dso->kernel, | |
538 | * but all kernel modules are ET_REL, so won't get here. | |
539 | */ | |
540 | if (map->dso->kernel == DSO_TYPE_USER) | |
541 | return map->unmap_ip(map, ip - map->dso->text_offset); | |
542 | ||
9176753d | 543 | return ip + map->reloc; |
1d5077bd AH |
544 | } |
545 | ||
1eee78ae ACM |
546 | static void maps__init(struct maps *maps) |
547 | { | |
548 | maps->entries = RB_ROOT; | |
1e628569 | 549 | maps->names = RB_ROOT; |
0a7c74ea | 550 | init_rwsem(&maps->lock); |
1eee78ae ACM |
551 | } |
552 | ||
11246c70 | 553 | void map_groups__init(struct map_groups *mg, struct machine *machine) |
c6e718ff | 554 | { |
3183f8ca | 555 | maps__init(&mg->maps); |
11246c70 | 556 | mg->machine = machine; |
ead05e8f | 557 | refcount_set(&mg->refcnt, 1); |
c6e718ff ACM |
558 | } |
559 | ||
6a2ffcdd | 560 | static void __maps__purge(struct maps *maps) |
591765fd | 561 | { |
1eee78ae ACM |
562 | struct rb_root *root = &maps->entries; |
563 | struct rb_node *next = rb_first(root); | |
591765fd ACM |
564 | |
565 | while (next) { | |
566 | struct map *pos = rb_entry(next, struct map, rb_node); | |
567 | ||
568 | next = rb_next(&pos->rb_node); | |
facf3f06 | 569 | rb_erase_init(&pos->rb_node, root); |
84c2cafa | 570 | map__put(pos); |
591765fd ACM |
571 | } |
572 | } | |
573 | ||
ea050a9b CD |
574 | static void __maps__purge_names(struct maps *maps) |
575 | { | |
576 | struct rb_root *root = &maps->names; | |
577 | struct rb_node *next = rb_first(root); | |
578 | ||
579 | while (next) { | |
580 | struct map *pos = rb_entry(next, struct map, rb_node_name); | |
581 | ||
582 | next = rb_next(&pos->rb_node_name); | |
583 | rb_erase_init(&pos->rb_node_name, root); | |
584 | map__put(pos); | |
585 | } | |
586 | } | |
587 | ||
1eee78ae ACM |
588 | static void maps__exit(struct maps *maps) |
589 | { | |
0a7c74ea | 590 | down_write(&maps->lock); |
6a2ffcdd | 591 | __maps__purge(maps); |
ea050a9b | 592 | __maps__purge_names(maps); |
0a7c74ea | 593 | up_write(&maps->lock); |
1eee78ae ACM |
594 | } |
595 | ||
98dfd55d | 596 | void map_groups__exit(struct map_groups *mg) |
591765fd | 597 | { |
3183f8ca | 598 | maps__exit(&mg->maps); |
591765fd ACM |
599 | } |
600 | ||
29ce3612 AH |
601 | bool map_groups__empty(struct map_groups *mg) |
602 | { | |
3183f8ca | 603 | return !maps__first(&mg->maps); |
29ce3612 AH |
604 | } |
605 | ||
11246c70 | 606 | struct map_groups *map_groups__new(struct machine *machine) |
93d5731d ACM |
607 | { |
608 | struct map_groups *mg = malloc(sizeof(*mg)); | |
609 | ||
610 | if (mg != NULL) | |
11246c70 | 611 | map_groups__init(mg, machine); |
93d5731d ACM |
612 | |
613 | return mg; | |
614 | } | |
615 | ||
616 | void map_groups__delete(struct map_groups *mg) | |
617 | { | |
618 | map_groups__exit(mg); | |
619 | free(mg); | |
620 | } | |
621 | ||
a26ca671 ACM |
622 | void map_groups__put(struct map_groups *mg) |
623 | { | |
ead05e8f | 624 | if (mg && refcount_dec_and_test(&mg->refcnt)) |
a26ca671 ACM |
625 | map_groups__delete(mg); |
626 | } | |
627 | ||
98dfd55d | 628 | struct symbol *map_groups__find_symbol(struct map_groups *mg, |
3183f8ca | 629 | u64 addr, struct map **mapp) |
4b8cf846 | 630 | { |
3183f8ca | 631 | struct map *map = map_groups__find(mg, addr); |
4b8cf846 | 632 | |
4afc81cd | 633 | /* Ensure map is loaded before using map->map_ip */ |
be39db9f | 634 | if (map != NULL && map__load(map) >= 0) { |
7e5e1b14 ACM |
635 | if (mapp != NULL) |
636 | *mapp = map; | |
be39db9f | 637 | return map__find_symbol(map, map->map_ip(map, addr)); |
7e5e1b14 ACM |
638 | } |
639 | ||
640 | return NULL; | |
641 | } | |
642 | ||
03db8b58 AH |
643 | static bool map__contains_symbol(struct map *map, struct symbol *sym) |
644 | { | |
645 | u64 ip = map->unmap_ip(map, sym->start); | |
646 | ||
647 | return ip >= map->start && ip < map->end; | |
648 | } | |
649 | ||
b7f9ff56 | 650 | struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, |
be39db9f | 651 | struct map **mapp) |
7e5e1b14 | 652 | { |
6a2ffcdd | 653 | struct symbol *sym; |
7e5e1b14 ACM |
654 | struct rb_node *nd; |
655 | ||
0a7c74ea | 656 | down_read(&maps->lock); |
6a2ffcdd ACM |
657 | |
658 | for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) { | |
7e5e1b14 | 659 | struct map *pos = rb_entry(nd, struct map, rb_node); |
6a2ffcdd | 660 | |
be39db9f | 661 | sym = map__find_symbol_by_name(pos, name); |
7e5e1b14 ACM |
662 | |
663 | if (sym == NULL) | |
664 | continue; | |
03db8b58 AH |
665 | if (!map__contains_symbol(pos, sym)) { |
666 | sym = NULL; | |
667 | continue; | |
668 | } | |
7e5e1b14 ACM |
669 | if (mapp != NULL) |
670 | *mapp = pos; | |
6a2ffcdd | 671 | goto out; |
7e5e1b14 | 672 | } |
4b8cf846 | 673 | |
6a2ffcdd ACM |
674 | sym = NULL; |
675 | out: | |
0a7c74ea | 676 | up_read(&maps->lock); |
6a2ffcdd | 677 | return sym; |
4b8cf846 ACM |
678 | } |
679 | ||
b7f9ff56 | 680 | struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg, |
b7f9ff56 | 681 | const char *name, |
be39db9f | 682 | struct map **mapp) |
b7f9ff56 | 683 | { |
3183f8ca | 684 | return maps__find_symbol_by_name(&mg->maps, name, mapp); |
b7f9ff56 ACM |
685 | } |
686 | ||
be39db9f | 687 | int map_groups__find_ams(struct addr_map_symbol *ams) |
4e987712 | 688 | { |
77faf4d0 | 689 | if (ams->addr < ams->map->start || ams->addr >= ams->map->end) { |
4e987712 ACM |
690 | if (ams->map->groups == NULL) |
691 | return -1; | |
3183f8ca | 692 | ams->map = map_groups__find(ams->map->groups, ams->addr); |
4e987712 ACM |
693 | if (ams->map == NULL) |
694 | return -1; | |
695 | } | |
696 | ||
697 | ams->al_addr = ams->map->map_ip(ams->map, ams->addr); | |
be39db9f | 698 | ams->sym = map__find_symbol(ams->map, ams->al_addr); |
4e987712 ACM |
699 | |
700 | return ams->sym ? 0 : -1; | |
701 | } | |
702 | ||
6a2ffcdd | 703 | static size_t maps__fprintf(struct maps *maps, FILE *fp) |
c6e718ff | 704 | { |
6a2ffcdd | 705 | size_t printed = 0; |
c6e718ff ACM |
706 | struct rb_node *nd; |
707 | ||
0a7c74ea | 708 | down_read(&maps->lock); |
6a2ffcdd ACM |
709 | |
710 | for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) { | |
c6e718ff ACM |
711 | struct map *pos = rb_entry(nd, struct map, rb_node); |
712 | printed += fprintf(fp, "Map:"); | |
713 | printed += map__fprintf(pos, fp); | |
714 | if (verbose > 2) { | |
3183f8ca | 715 | printed += dso__fprintf(pos->dso, fp); |
c6e718ff ACM |
716 | printed += fprintf(fp, "--\n"); |
717 | } | |
718 | } | |
719 | ||
0a7c74ea | 720 | up_read(&maps->lock); |
6a2ffcdd | 721 | |
c6e718ff ACM |
722 | return printed; |
723 | } | |
724 | ||
5c24b67a | 725 | size_t map_groups__fprintf(struct map_groups *mg, FILE *fp) |
c6e718ff | 726 | { |
3183f8ca | 727 | return maps__fprintf(&mg->maps, fp); |
c6e718ff ACM |
728 | } |
729 | ||
cb8382e0 JO |
730 | static void __map_groups__insert(struct map_groups *mg, struct map *map) |
731 | { | |
3183f8ca | 732 | __maps__insert(&mg->maps, map); |
1e628569 | 733 | __maps__insert_name(&mg->maps, map); |
cb8382e0 JO |
734 | map->groups = mg; |
735 | } | |
736 | ||
6a2ffcdd | 737 | static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) |
c6e718ff | 738 | { |
6a2ffcdd | 739 | struct rb_root *root; |
6a9405b5 | 740 | struct rb_node *next, *first; |
0a1eae39 | 741 | int err = 0; |
c6e718ff | 742 | |
0a7c74ea | 743 | down_write(&maps->lock); |
6a2ffcdd ACM |
744 | |
745 | root = &maps->entries; | |
6a2ffcdd | 746 | |
6a9405b5 KK |
747 | /* |
748 | * Find first map where end > map->start. | |
749 | * Same as find_vma() in kernel. | |
750 | */ | |
751 | next = root->rb_node; | |
752 | first = NULL; | |
753 | while (next) { | |
754 | struct map *pos = rb_entry(next, struct map, rb_node); | |
755 | ||
756 | if (pos->end > map->start) { | |
757 | first = next; | |
758 | if (pos->start <= map->start) | |
759 | break; | |
760 | next = next->rb_left; | |
761 | } else | |
762 | next = next->rb_right; | |
763 | } | |
764 | ||
765 | next = first; | |
c6e718ff ACM |
766 | while (next) { |
767 | struct map *pos = rb_entry(next, struct map, rb_node); | |
768 | next = rb_next(&pos->rb_node); | |
769 | ||
6a9405b5 KK |
770 | /* |
771 | * Stop if current map starts after map->end. | |
772 | * Maps are ordered by start: next will not overlap for sure. | |
773 | */ | |
774 | if (pos->start >= map->end) | |
775 | break; | |
c6e718ff ACM |
776 | |
777 | if (verbose >= 2) { | |
21e8c810 AB |
778 | |
779 | if (use_browser) { | |
d8e75a11 | 780 | pr_debug("overlapping maps in %s (disable tui for more info)\n", |
21e8c810 AB |
781 | map->dso->name); |
782 | } else { | |
783 | fputs("overlapping maps:\n", fp); | |
784 | map__fprintf(map, fp); | |
785 | map__fprintf(pos, fp); | |
786 | } | |
c6e718ff ACM |
787 | } |
788 | ||
facf3f06 | 789 | rb_erase_init(&pos->rb_node, root); |
c6e718ff ACM |
790 | /* |
791 | * Now check if we need to create new maps for areas not | |
792 | * overlapped by the new map: | |
793 | */ | |
794 | if (map->start > pos->start) { | |
795 | struct map *before = map__clone(pos); | |
796 | ||
0a1eae39 ACM |
797 | if (before == NULL) { |
798 | err = -ENOMEM; | |
84c2cafa | 799 | goto put_map; |
0a1eae39 | 800 | } |
c6e718ff | 801 | |
77faf4d0 | 802 | before->end = map->start; |
cb8382e0 | 803 | __map_groups__insert(pos->groups, before); |
21e8c810 | 804 | if (verbose >= 2 && !use_browser) |
c6e718ff | 805 | map__fprintf(before, fp); |
d91130e9 | 806 | map__put(before); |
c6e718ff ACM |
807 | } |
808 | ||
809 | if (map->end < pos->end) { | |
810 | struct map *after = map__clone(pos); | |
811 | ||
0a1eae39 ACM |
812 | if (after == NULL) { |
813 | err = -ENOMEM; | |
84c2cafa | 814 | goto put_map; |
0a1eae39 | 815 | } |
c6e718ff | 816 | |
77faf4d0 | 817 | after->start = map->end; |
cb8382e0 | 818 | __map_groups__insert(pos->groups, after); |
21e8c810 | 819 | if (verbose >= 2 && !use_browser) |
c6e718ff | 820 | map__fprintf(after, fp); |
d91130e9 | 821 | map__put(after); |
c6e718ff | 822 | } |
84c2cafa | 823 | put_map: |
5c24b67a | 824 | map__put(pos); |
0a1eae39 ACM |
825 | |
826 | if (err) | |
6a2ffcdd | 827 | goto out; |
c6e718ff ACM |
828 | } |
829 | ||
6a2ffcdd ACM |
830 | err = 0; |
831 | out: | |
0a7c74ea | 832 | up_write(&maps->lock); |
6a2ffcdd ACM |
833 | return err; |
834 | } | |
835 | ||
836 | int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, | |
837 | FILE *fp) | |
838 | { | |
3183f8ca | 839 | return maps__fixup_overlappings(&mg->maps, map, fp); |
c6e718ff ACM |
840 | } |
841 | ||
842 | /* | |
843 | * XXX This should not really _copy_ te maps, but refcount them. | |
844 | */ | |
3183f8ca | 845 | int map_groups__clone(struct thread *thread, struct map_groups *parent) |
c6e718ff | 846 | { |
6c502584 | 847 | struct map_groups *mg = thread->mg; |
6a2ffcdd | 848 | int err = -ENOMEM; |
4bb7123d | 849 | struct map *map; |
3183f8ca | 850 | struct maps *maps = &parent->maps; |
4bb7123d | 851 | |
0a7c74ea | 852 | down_read(&maps->lock); |
6a2ffcdd | 853 | |
4bb7123d | 854 | for (map = maps__first(maps); map; map = map__next(map)) { |
c6e718ff ACM |
855 | struct map *new = map__clone(map); |
856 | if (new == NULL) | |
6a2ffcdd | 857 | goto out_unlock; |
6c502584 JO |
858 | |
859 | err = unwind__prepare_access(thread, new, NULL); | |
860 | if (err) | |
861 | goto out_unlock; | |
862 | ||
98dfd55d | 863 | map_groups__insert(mg, new); |
bae32b50 | 864 | map__put(new); |
c6e718ff | 865 | } |
6a2ffcdd ACM |
866 | |
867 | err = 0; | |
868 | out_unlock: | |
0a7c74ea | 869 | up_read(&maps->lock); |
6a2ffcdd | 870 | return err; |
c6e718ff ACM |
871 | } |
872 | ||
6a2ffcdd | 873 | static void __maps__insert(struct maps *maps, struct map *map) |
4b8cf846 | 874 | { |
1eee78ae | 875 | struct rb_node **p = &maps->entries.rb_node; |
4b8cf846 ACM |
876 | struct rb_node *parent = NULL; |
877 | const u64 ip = map->start; | |
878 | struct map *m; | |
879 | ||
880 | while (*p != NULL) { | |
881 | parent = *p; | |
882 | m = rb_entry(parent, struct map, rb_node); | |
883 | if (ip < m->start) | |
884 | p = &(*p)->rb_left; | |
885 | else | |
886 | p = &(*p)->rb_right; | |
887 | } | |
888 | ||
889 | rb_link_node(&map->rb_node, parent, p); | |
1eee78ae | 890 | rb_insert_color(&map->rb_node, &maps->entries); |
84c2cafa | 891 | map__get(map); |
4b8cf846 ACM |
892 | } |
893 | ||
1e628569 ESE |
894 | static void __maps__insert_name(struct maps *maps, struct map *map) |
895 | { | |
896 | struct rb_node **p = &maps->names.rb_node; | |
897 | struct rb_node *parent = NULL; | |
898 | struct map *m; | |
899 | int rc; | |
900 | ||
901 | while (*p != NULL) { | |
902 | parent = *p; | |
903 | m = rb_entry(parent, struct map, rb_node_name); | |
904 | rc = strcmp(m->dso->short_name, map->dso->short_name); | |
905 | if (rc < 0) | |
906 | p = &(*p)->rb_left; | |
907 | else if (rc > 0) | |
908 | p = &(*p)->rb_right; | |
909 | else | |
910 | return; | |
911 | } | |
912 | rb_link_node(&map->rb_node_name, parent, p); | |
913 | rb_insert_color(&map->rb_node_name, &maps->names); | |
914 | map__get(map); | |
915 | } | |
916 | ||
6a2ffcdd ACM |
917 | void maps__insert(struct maps *maps, struct map *map) |
918 | { | |
0a7c74ea | 919 | down_write(&maps->lock); |
6a2ffcdd | 920 | __maps__insert(maps, map); |
1e628569 | 921 | __maps__insert_name(maps, map); |
0a7c74ea | 922 | up_write(&maps->lock); |
6a2ffcdd ACM |
923 | } |
924 | ||
925 | static void __maps__remove(struct maps *maps, struct map *map) | |
076c6e45 | 926 | { |
facf3f06 | 927 | rb_erase_init(&map->rb_node, &maps->entries); |
84c2cafa | 928 | map__put(map); |
87cf9e23 CD |
929 | |
930 | rb_erase_init(&map->rb_node_name, &maps->names); | |
931 | map__put(map); | |
076c6e45 ACM |
932 | } |
933 | ||
6a2ffcdd ACM |
934 | void maps__remove(struct maps *maps, struct map *map) |
935 | { | |
0a7c74ea | 936 | down_write(&maps->lock); |
6a2ffcdd | 937 | __maps__remove(maps, map); |
0a7c74ea | 938 | up_write(&maps->lock); |
6a2ffcdd ACM |
939 | } |
940 | ||
1eee78ae | 941 | struct map *maps__find(struct maps *maps, u64 ip) |
4b8cf846 | 942 | { |
b18e0888 | 943 | struct rb_node *p; |
4b8cf846 ACM |
944 | struct map *m; |
945 | ||
0a7c74ea | 946 | down_read(&maps->lock); |
6a2ffcdd | 947 | |
b18e0888 ESE |
948 | p = maps->entries.rb_node; |
949 | while (p != NULL) { | |
950 | m = rb_entry(p, struct map, rb_node); | |
4b8cf846 | 951 | if (ip < m->start) |
b18e0888 | 952 | p = p->rb_left; |
4955ea22 | 953 | else if (ip >= m->end) |
b18e0888 | 954 | p = p->rb_right; |
4b8cf846 | 955 | else |
6a2ffcdd | 956 | goto out; |
4b8cf846 ACM |
957 | } |
958 | ||
6a2ffcdd ACM |
959 | m = NULL; |
960 | out: | |
0a7c74ea | 961 | up_read(&maps->lock); |
6a2ffcdd | 962 | return m; |
4b8cf846 | 963 | } |
8e0cf965 | 964 | |
1eee78ae | 965 | struct map *maps__first(struct maps *maps) |
8e0cf965 | 966 | { |
1eee78ae | 967 | struct rb_node *first = rb_first(&maps->entries); |
8e0cf965 AH |
968 | |
969 | if (first) | |
970 | return rb_entry(first, struct map, rb_node); | |
971 | return NULL; | |
972 | } | |
973 | ||
4d4dee9a | 974 | struct map *map__next(struct map *map) |
8e0cf965 AH |
975 | { |
976 | struct rb_node *next = rb_next(&map->rb_node); | |
977 | ||
978 | if (next) | |
979 | return rb_entry(next, struct map, rb_node); | |
980 | return NULL; | |
981 | } | |
ba92732e | 982 | |
5759a682 | 983 | struct kmap *__map__kmap(struct map *map) |
ba92732e | 984 | { |
5759a682 | 985 | if (!map->dso || !map->dso->kernel) |
ba92732e | 986 | return NULL; |
ba92732e WN |
987 | return (struct kmap *)(map + 1); |
988 | } | |
989 | ||
5759a682 AH |
990 | struct kmap *map__kmap(struct map *map) |
991 | { | |
992 | struct kmap *kmap = __map__kmap(map); | |
993 | ||
994 | if (!kmap) | |
995 | pr_err("Internal error: map__kmap with a non-kernel map\n"); | |
996 | return kmap; | |
997 | } | |
998 | ||
ba92732e WN |
999 | struct map_groups *map__kmaps(struct map *map) |
1000 | { | |
1001 | struct kmap *kmap = map__kmap(map); | |
1002 | ||
1003 | if (!kmap || !kmap->kmaps) { | |
1004 | pr_err("Internal error: map__kmaps with a non-kernel map\n"); | |
1005 | return NULL; | |
1006 | } | |
1007 | return kmap->kmaps; | |
1008 | } |