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