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