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