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