1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <stdlib.h> 4 #include <linux/zalloc.h> 5 #include "debug.h" 6 #include "dso.h" 7 #include "map.h" 8 #include "maps.h" 9 #include "rwsem.h" 10 #include "thread.h" 11 #include "ui/ui.h" 12 #include "unwind.h" 13 #include "unwind-libdw.h" 14 #include <internal/rc_check.h> 15 16 /* 17 * Locking/sorting note: 18 * 19 * Sorting is done with the write lock, iteration and binary searching happens 20 * under the read lock requiring being sorted. There is a race between sorting 21 * releasing the write lock and acquiring the read lock for iteration/searching 22 * where another thread could insert and break the sorting of the maps. In 23 * practice inserting maps should be rare meaning that the race shouldn't lead 24 * to live lock. Removal of maps doesn't break being sorted. 25 */ 26 27 DECLARE_RC_STRUCT(maps) { 28 struct rw_semaphore lock; 29 /** 30 * @maps_by_address: array of maps sorted by their starting address if 31 * maps_by_address_sorted is true. 32 */ 33 struct map **maps_by_address; 34 /** 35 * @maps_by_name: optional array of maps sorted by their dso name if 36 * maps_by_name_sorted is true. 37 */ 38 struct map **maps_by_name; 39 struct machine *machine; 40 #ifdef HAVE_LIBUNWIND_SUPPORT 41 void *addr_space; 42 const struct unwind_libunwind_ops *unwind_libunwind_ops; 43 uint16_t e_machine; 44 #endif 45 #ifdef HAVE_LIBDW_SUPPORT 46 void *libdw_addr_space_dwfl; 47 #endif 48 refcount_t refcnt; 49 /** 50 * @nr_maps: number of maps_by_address, and possibly maps_by_name, 51 * entries that contain maps. 52 */ 53 unsigned int nr_maps; 54 /** 55 * @nr_maps_allocated: number of entries in maps_by_address and possibly 56 * maps_by_name. 57 */ 58 unsigned int nr_maps_allocated; 59 /** 60 * @last_search_by_name_idx: cache of last found by name entry's index 61 * as frequent searches for the same dso name are common. 62 */ 63 unsigned int last_search_by_name_idx; 64 /** @maps_by_address_sorted: is maps_by_address sorted. */ 65 bool maps_by_address_sorted; 66 /** @maps_by_name_sorted: is maps_by_name sorted. */ 67 bool maps_by_name_sorted; 68 /** @ends_broken: does the map contain a map where end values are unset/unsorted? */ 69 bool ends_broken; 70 }; 71 72 static void check_invariants(const struct maps *maps __maybe_unused) 73 { 74 #ifndef NDEBUG 75 assert(RC_CHK_ACCESS(maps)->nr_maps <= RC_CHK_ACCESS(maps)->nr_maps_allocated); 76 for (unsigned int i = 0; i < RC_CHK_ACCESS(maps)->nr_maps; i++) { 77 struct map *map = RC_CHK_ACCESS(maps)->maps_by_address[i]; 78 79 /* Check map is well-formed. */ 80 assert(map__end(map) == 0 || map__start(map) <= map__end(map)); 81 /* Expect at least 1 reference count. */ 82 assert(refcount_read(map__refcnt(map)) > 0); 83 84 if (map__dso(map) && dso__kernel(map__dso(map))) 85 assert(RC_CHK_EQUAL(map__kmap(map)->kmaps, maps)); 86 87 if (i > 0) { 88 struct map *prev = RC_CHK_ACCESS(maps)->maps_by_address[i - 1]; 89 90 /* If addresses are sorted... */ 91 if (RC_CHK_ACCESS(maps)->maps_by_address_sorted) { 92 /* Maps should be in start address order. */ 93 assert(map__start(prev) <= map__start(map)); 94 /* 95 * If the ends of maps aren't broken (during 96 * construction) then they should be ordered 97 * too. 98 */ 99 if (!RC_CHK_ACCESS(maps)->ends_broken) { 100 assert(map__end(prev) <= map__end(map)); 101 assert(map__end(prev) <= map__start(map) || 102 map__start(prev) == map__start(map)); 103 } 104 } 105 } 106 } 107 if (RC_CHK_ACCESS(maps)->maps_by_name) { 108 for (unsigned int i = 0; i < RC_CHK_ACCESS(maps)->nr_maps; i++) { 109 struct map *map = RC_CHK_ACCESS(maps)->maps_by_name[i]; 110 111 /* 112 * Maps by name maps should be in maps_by_address, so 113 * the reference count should be higher. 114 */ 115 assert(refcount_read(map__refcnt(map)) > 1); 116 } 117 } 118 #endif 119 } 120 121 static struct map **maps__maps_by_address(const struct maps *maps) 122 { 123 return RC_CHK_ACCESS(maps)->maps_by_address; 124 } 125 126 static void maps__set_maps_by_address(struct maps *maps, struct map **new) 127 { 128 RC_CHK_ACCESS(maps)->maps_by_address = new; 129 130 } 131 132 static void maps__set_nr_maps_allocated(struct maps *maps, unsigned int nr_maps_allocated) 133 { 134 RC_CHK_ACCESS(maps)->nr_maps_allocated = nr_maps_allocated; 135 } 136 137 static void maps__set_nr_maps(struct maps *maps, unsigned int nr_maps) 138 { 139 RC_CHK_ACCESS(maps)->nr_maps = nr_maps; 140 } 141 142 /* Not in the header, to aid reference counting. */ 143 static struct map **maps__maps_by_name(const struct maps *maps) 144 { 145 return RC_CHK_ACCESS(maps)->maps_by_name; 146 147 } 148 149 static void maps__set_maps_by_name(struct maps *maps, struct map **new) 150 { 151 RC_CHK_ACCESS(maps)->maps_by_name = new; 152 153 } 154 155 static bool maps__maps_by_address_sorted(const struct maps *maps) 156 { 157 return RC_CHK_ACCESS(maps)->maps_by_address_sorted; 158 } 159 160 static void maps__set_maps_by_address_sorted(struct maps *maps, bool value) 161 { 162 RC_CHK_ACCESS(maps)->maps_by_address_sorted = value; 163 } 164 165 static bool maps__maps_by_name_sorted(const struct maps *maps) 166 { 167 return RC_CHK_ACCESS(maps)->maps_by_name_sorted; 168 } 169 170 static void maps__set_maps_by_name_sorted(struct maps *maps, bool value) 171 { 172 RC_CHK_ACCESS(maps)->maps_by_name_sorted = value; 173 } 174 175 struct machine *maps__machine(const struct maps *maps) 176 { 177 return RC_CHK_ACCESS(maps)->machine; 178 } 179 180 unsigned int maps__nr_maps(const struct maps *maps) 181 { 182 return RC_CHK_ACCESS(maps)->nr_maps; 183 } 184 185 refcount_t *maps__refcnt(struct maps *maps) 186 { 187 return &RC_CHK_ACCESS(maps)->refcnt; 188 } 189 190 #ifdef HAVE_LIBUNWIND_SUPPORT 191 void *maps__addr_space(const struct maps *maps) 192 { 193 return RC_CHK_ACCESS(maps)->addr_space; 194 } 195 196 void maps__set_addr_space(struct maps *maps, void *addr_space) 197 { 198 RC_CHK_ACCESS(maps)->addr_space = addr_space; 199 } 200 201 uint16_t maps__e_machine(const struct maps *maps) 202 { 203 return RC_CHK_ACCESS(maps)->e_machine; 204 } 205 206 void maps__set_e_machine(struct maps *maps, uint16_t e_machine) 207 { 208 RC_CHK_ACCESS(maps)->e_machine = e_machine; 209 } 210 #endif 211 #ifdef HAVE_LIBDW_SUPPORT 212 void *maps__libdw_addr_space_dwfl(const struct maps *maps) 213 { 214 return RC_CHK_ACCESS(maps)->libdw_addr_space_dwfl; 215 } 216 217 void maps__set_libdw_addr_space_dwfl(struct maps *maps, void *dwfl) 218 { 219 RC_CHK_ACCESS(maps)->libdw_addr_space_dwfl = dwfl; 220 } 221 #endif 222 223 static struct rw_semaphore *maps__lock(struct maps *maps) 224 { 225 return &RC_CHK_ACCESS(maps)->lock; 226 } 227 228 static void maps__init(struct maps *maps, struct machine *machine) 229 { 230 init_rwsem(maps__lock(maps)); 231 RC_CHK_ACCESS(maps)->maps_by_address = NULL; 232 RC_CHK_ACCESS(maps)->maps_by_name = NULL; 233 RC_CHK_ACCESS(maps)->machine = machine; 234 #ifdef HAVE_LIBUNWIND_SUPPORT 235 RC_CHK_ACCESS(maps)->addr_space = NULL; 236 RC_CHK_ACCESS(maps)->unwind_libunwind_ops = NULL; 237 #endif 238 #ifdef HAVE_LIBDW_SUPPORT 239 RC_CHK_ACCESS(maps)->libdw_addr_space_dwfl = NULL; 240 #endif 241 refcount_set(maps__refcnt(maps), 1); 242 RC_CHK_ACCESS(maps)->nr_maps = 0; 243 RC_CHK_ACCESS(maps)->nr_maps_allocated = 0; 244 RC_CHK_ACCESS(maps)->last_search_by_name_idx = 0; 245 RC_CHK_ACCESS(maps)->maps_by_address_sorted = true; 246 RC_CHK_ACCESS(maps)->maps_by_name_sorted = false; 247 } 248 249 static void maps__exit(struct maps *maps) 250 { 251 struct map **maps_by_address = maps__maps_by_address(maps); 252 struct map **maps_by_name = maps__maps_by_name(maps); 253 254 for (unsigned int i = 0; i < maps__nr_maps(maps); i++) { 255 map__zput(maps_by_address[i]); 256 if (maps_by_name) 257 map__zput(maps_by_name[i]); 258 } 259 zfree(&maps_by_address); 260 zfree(&maps_by_name); 261 unwind__finish_access(maps); 262 #ifdef HAVE_LIBDW_SUPPORT 263 libdw__invalidate_dwfl(maps, maps__libdw_addr_space_dwfl(maps)); 264 #endif 265 } 266 267 struct maps *maps__new(struct machine *machine) 268 { 269 struct maps *result; 270 RC_STRUCT(maps) *maps = zalloc(sizeof(*maps)); 271 272 if (ADD_RC_CHK(result, maps)) 273 maps__init(result, machine); 274 275 return result; 276 } 277 278 static void maps__delete(struct maps *maps) 279 { 280 maps__exit(maps); 281 RC_CHK_FREE(maps); 282 } 283 284 struct maps *maps__get(struct maps *maps) 285 { 286 struct maps *result; 287 288 if (RC_CHK_GET(result, maps)) 289 refcount_inc(maps__refcnt(maps)); 290 291 return result; 292 } 293 294 void maps__put(struct maps *maps) 295 { 296 if (maps && refcount_dec_and_test(maps__refcnt(maps))) 297 maps__delete(maps); 298 else 299 RC_CHK_PUT(maps); 300 } 301 302 static void __maps__free_maps_by_name(struct maps *maps) 303 { 304 if (!maps__maps_by_name(maps)) 305 return; 306 307 /* 308 * Free everything to try to do it from the rbtree in the next search 309 */ 310 for (unsigned int i = 0; i < maps__nr_maps(maps); i++) 311 map__put(maps__maps_by_name(maps)[i]); 312 313 zfree(&RC_CHK_ACCESS(maps)->maps_by_name); 314 315 /* Consistent with maps__init(). When maps_by_name == NULL, maps_by_name_sorted == false */ 316 maps__set_maps_by_name_sorted(maps, false); 317 } 318 319 static int map__start_cmp(const void *a, const void *b) 320 { 321 const struct map *map_a = *(const struct map * const *)a; 322 const struct map *map_b = *(const struct map * const *)b; 323 u64 map_a_start = map__start(map_a); 324 u64 map_b_start = map__start(map_b); 325 326 if (map_a_start == map_b_start) { 327 u64 map_a_end = map__end(map_a); 328 u64 map_b_end = map__end(map_b); 329 330 if (map_a_end == map_b_end) { 331 /* Ensure maps with the same addresses have a fixed order. */ 332 if (RC_CHK_ACCESS(map_a) == RC_CHK_ACCESS(map_b)) 333 return 0; 334 return (intptr_t)RC_CHK_ACCESS(map_a) > (intptr_t)RC_CHK_ACCESS(map_b) 335 ? 1 : -1; 336 } 337 return map_a_end > map_b_end ? 1 : -1; 338 } 339 return map_a_start > map_b_start ? 1 : -1; 340 } 341 342 static void __maps__sort_by_address(struct maps *maps) 343 { 344 if (maps__maps_by_address_sorted(maps)) 345 return; 346 347 qsort(maps__maps_by_address(maps), 348 maps__nr_maps(maps), 349 sizeof(struct map *), 350 map__start_cmp); 351 maps__set_maps_by_address_sorted(maps, true); 352 } 353 354 static void maps__sort_by_address(struct maps *maps) 355 { 356 down_write(maps__lock(maps)); 357 __maps__sort_by_address(maps); 358 up_write(maps__lock(maps)); 359 } 360 361 static int map__strcmp(const void *a, const void *b) 362 { 363 const struct map *map_a = *(const struct map * const *)a; 364 const struct map *map_b = *(const struct map * const *)b; 365 const struct dso *dso_a = map__dso(map_a); 366 const struct dso *dso_b = map__dso(map_b); 367 int ret = strcmp(dso__short_name(dso_a), dso__short_name(dso_b)); 368 369 if (ret == 0 && RC_CHK_ACCESS(map_a) != RC_CHK_ACCESS(map_b)) { 370 /* Ensure distinct but name equal maps have an order. */ 371 return map__start_cmp(a, b); 372 } 373 return ret; 374 } 375 376 static int maps__sort_by_name(struct maps *maps) 377 { 378 int err = 0; 379 380 down_write(maps__lock(maps)); 381 if (!maps__maps_by_name_sorted(maps)) { 382 struct map **maps_by_name = maps__maps_by_name(maps); 383 384 if (!maps_by_name) { 385 maps_by_name = malloc(RC_CHK_ACCESS(maps)->nr_maps_allocated * 386 sizeof(*maps_by_name)); 387 if (!maps_by_name) 388 err = -ENOMEM; 389 else { 390 struct map **maps_by_address = maps__maps_by_address(maps); 391 unsigned int n = maps__nr_maps(maps); 392 393 maps__set_maps_by_name(maps, maps_by_name); 394 for (unsigned int i = 0; i < n; i++) 395 maps_by_name[i] = map__get(maps_by_address[i]); 396 } 397 } 398 if (!err) { 399 qsort(maps_by_name, 400 maps__nr_maps(maps), 401 sizeof(struct map *), 402 map__strcmp); 403 maps__set_maps_by_name_sorted(maps, true); 404 } 405 } 406 check_invariants(maps); 407 up_write(maps__lock(maps)); 408 return err; 409 } 410 411 static unsigned int maps__by_address_index(const struct maps *maps, const struct map *map) 412 { 413 struct map **maps_by_address = maps__maps_by_address(maps); 414 415 if (maps__maps_by_address_sorted(maps)) { 416 struct map **mapp = 417 bsearch(&map, maps__maps_by_address(maps), maps__nr_maps(maps), 418 sizeof(*mapp), map__start_cmp); 419 420 if (mapp) 421 return mapp - maps_by_address; 422 } else { 423 for (unsigned int i = 0; i < maps__nr_maps(maps); i++) { 424 if (RC_CHK_ACCESS(maps_by_address[i]) == RC_CHK_ACCESS(map)) 425 return i; 426 } 427 } 428 pr_err("Map missing from maps"); 429 return -1; 430 } 431 432 static unsigned int maps__by_name_index(const struct maps *maps, const struct map *map) 433 { 434 struct map **maps_by_name = maps__maps_by_name(maps); 435 436 if (maps__maps_by_name_sorted(maps)) { 437 struct map **mapp = 438 bsearch(&map, maps_by_name, maps__nr_maps(maps), 439 sizeof(*mapp), map__strcmp); 440 441 if (mapp) 442 return mapp - maps_by_name; 443 } else { 444 for (unsigned int i = 0; i < maps__nr_maps(maps); i++) { 445 if (RC_CHK_ACCESS(maps_by_name[i]) == RC_CHK_ACCESS(map)) 446 return i; 447 } 448 } 449 pr_err("Map missing from maps"); 450 return -1; 451 } 452 453 static void map__set_kmap_maps(struct map *map, struct maps *maps) 454 { 455 struct dso *dso; 456 457 if (map == NULL) 458 return; 459 460 dso = map__dso(map); 461 462 if (dso && dso__kernel(dso)) { 463 struct kmap *kmap = map__kmap(map); 464 465 if (kmap) 466 kmap->kmaps = maps; 467 else 468 pr_err("Internal error: kernel dso with non kernel map\n"); 469 } 470 } 471 472 static int __maps__insert(struct maps *maps, struct map *new) 473 { 474 struct map **maps_by_address = maps__maps_by_address(maps); 475 struct map **maps_by_name = maps__maps_by_name(maps); 476 unsigned int nr_maps = maps__nr_maps(maps); 477 unsigned int nr_allocate = RC_CHK_ACCESS(maps)->nr_maps_allocated; 478 479 if (nr_maps + 1 > nr_allocate) { 480 nr_allocate = !nr_allocate ? 32 : nr_allocate * 2; 481 482 maps_by_address = realloc(maps_by_address, nr_allocate * sizeof(new)); 483 if (!maps_by_address) 484 return -ENOMEM; 485 486 maps__set_maps_by_address(maps, maps_by_address); 487 if (maps_by_name) { 488 maps_by_name = realloc(maps_by_name, nr_allocate * sizeof(new)); 489 if (!maps_by_name) { 490 /* 491 * If by name fails, just disable by name and it will 492 * recompute next time it is required. 493 */ 494 __maps__free_maps_by_name(maps); 495 } 496 maps__set_maps_by_name(maps, maps_by_name); 497 } 498 RC_CHK_ACCESS(maps)->nr_maps_allocated = nr_allocate; 499 } 500 /* Insert the value at the end. */ 501 maps_by_address[nr_maps] = map__get(new); 502 map__set_kmap_maps(new, maps); 503 if (maps_by_name) 504 maps_by_name[nr_maps] = map__get(new); 505 506 nr_maps++; 507 RC_CHK_ACCESS(maps)->nr_maps = nr_maps; 508 509 /* 510 * Recompute if things are sorted. If things are inserted in a sorted 511 * manner, for example by processing /proc/pid/maps, then no 512 * sorting/resorting will be necessary. 513 */ 514 if (nr_maps == 1) { 515 /* If there's just 1 entry then maps are sorted. */ 516 maps__set_maps_by_address_sorted(maps, true); 517 maps__set_maps_by_name_sorted(maps, maps_by_name != NULL); 518 } else { 519 /* Sorted if maps were already sorted and this map starts after the last one. */ 520 maps__set_maps_by_address_sorted(maps, 521 maps__maps_by_address_sorted(maps) && 522 map__end(maps_by_address[nr_maps - 2]) <= map__start(new)); 523 maps__set_maps_by_name_sorted(maps, false); 524 } 525 if (map__end(new) < map__start(new)) 526 RC_CHK_ACCESS(maps)->ends_broken = true; 527 528 return 0; 529 } 530 531 int maps__insert(struct maps *maps, struct map *map) 532 { 533 int ret; 534 535 down_write(maps__lock(maps)); 536 ret = __maps__insert(maps, map); 537 check_invariants(maps); 538 up_write(maps__lock(maps)); 539 return ret; 540 } 541 542 static void __maps__remove(struct maps *maps, struct map *map) 543 { 544 struct map **maps_by_address = maps__maps_by_address(maps); 545 struct map **maps_by_name = maps__maps_by_name(maps); 546 unsigned int nr_maps = maps__nr_maps(maps); 547 unsigned int address_idx; 548 549 /* Slide later mappings over the one to remove */ 550 address_idx = maps__by_address_index(maps, map); 551 map__put(maps_by_address[address_idx]); 552 memmove(&maps_by_address[address_idx], 553 &maps_by_address[address_idx + 1], 554 (nr_maps - address_idx - 1) * sizeof(*maps_by_address)); 555 556 if (maps_by_name) { 557 unsigned int name_idx = maps__by_name_index(maps, map); 558 559 map__put(maps_by_name[name_idx]); 560 memmove(&maps_by_name[name_idx], 561 &maps_by_name[name_idx + 1], 562 (nr_maps - name_idx - 1) * sizeof(*maps_by_name)); 563 } 564 565 --RC_CHK_ACCESS(maps)->nr_maps; 566 } 567 568 void maps__remove(struct maps *maps, struct map *map) 569 { 570 down_write(maps__lock(maps)); 571 __maps__remove(maps, map); 572 check_invariants(maps); 573 up_write(maps__lock(maps)); 574 #ifdef HAVE_LIBDW_SUPPORT 575 libdw__invalidate_dwfl(maps, maps__libdw_addr_space_dwfl(maps)); 576 #endif 577 } 578 579 bool maps__empty(struct maps *maps) 580 { 581 bool res; 582 583 down_read(maps__lock(maps)); 584 res = maps__nr_maps(maps) == 0; 585 up_read(maps__lock(maps)); 586 587 return res; 588 } 589 590 bool maps__equal(struct maps *a, struct maps *b) 591 { 592 return RC_CHK_EQUAL(a, b); 593 } 594 595 int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data) 596 { 597 bool done = false; 598 int ret = 0; 599 600 /* See locking/sorting note. */ 601 while (!done) { 602 down_read(maps__lock(maps)); 603 if (maps__maps_by_address_sorted(maps)) { 604 /* 605 * maps__for_each_map callbacks may buggily/unsafely 606 * insert into maps_by_address. Deliberately reload 607 * maps__nr_maps and maps_by_address on each iteration 608 * to avoid using memory freed by maps__insert growing 609 * the array - this may cause maps to be skipped or 610 * repeated. 611 */ 612 for (unsigned int i = 0; i < maps__nr_maps(maps); i++) { 613 struct map **maps_by_address = maps__maps_by_address(maps); 614 struct map *map = maps_by_address[i]; 615 616 ret = cb(map, data); 617 if (ret) 618 break; 619 } 620 done = true; 621 } 622 up_read(maps__lock(maps)); 623 if (!done) 624 maps__sort_by_address(maps); 625 } 626 return ret; 627 } 628 629 void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data) 630 { 631 struct map **maps_by_address; 632 bool removed = false; 633 634 down_write(maps__lock(maps)); 635 636 maps_by_address = maps__maps_by_address(maps); 637 for (unsigned int i = 0; i < maps__nr_maps(maps);) { 638 if (cb(maps_by_address[i], data)) { 639 __maps__remove(maps, maps_by_address[i]); 640 removed = true; 641 } else { 642 i++; 643 } 644 } 645 check_invariants(maps); 646 up_write(maps__lock(maps)); 647 if (removed) { 648 #ifdef HAVE_LIBDW_SUPPORT 649 libdw__invalidate_dwfl(maps, maps__libdw_addr_space_dwfl(maps)); 650 #endif 651 } 652 } 653 654 struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) 655 { 656 struct map *map = maps__find(maps, addr); 657 struct symbol *result = NULL; 658 659 /* Ensure map is loaded before using map->map_ip */ 660 if (map != NULL && map__load(map) >= 0) 661 result = map__find_symbol(map, map__map_ip(map, addr)); 662 663 if (mapp) 664 *mapp = map; 665 else 666 map__put(map); 667 668 return result; 669 } 670 671 struct maps__find_symbol_by_name_args { 672 struct map **mapp; 673 const char *name; 674 struct symbol *sym; 675 }; 676 677 static int maps__find_symbol_by_name_cb(struct map *map, void *data) 678 { 679 struct maps__find_symbol_by_name_args *args = data; 680 681 args->sym = map__find_symbol_by_name(map, args->name); 682 if (!args->sym) 683 return 0; 684 685 if (!map__contains_symbol(map, args->sym)) { 686 args->sym = NULL; 687 return 0; 688 } 689 690 if (args->mapp != NULL) 691 *args->mapp = map__get(map); 692 return 1; 693 } 694 695 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp) 696 { 697 struct maps__find_symbol_by_name_args args = { 698 .mapp = mapp, 699 .name = name, 700 .sym = NULL, 701 }; 702 703 maps__for_each_map(maps, maps__find_symbol_by_name_cb, &args); 704 return args.sym; 705 } 706 707 int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) 708 { 709 if (ams->addr < map__start(ams->ms.map) || ams->addr >= map__end(ams->ms.map)) { 710 if (maps == NULL) 711 return -1; 712 map__put(ams->ms.map); 713 ams->ms.map = maps__find(maps, ams->addr); 714 if (ams->ms.map == NULL) 715 return -1; 716 } 717 718 ams->al_addr = map__map_ip(ams->ms.map, ams->addr); 719 ams->ms.sym = map__find_symbol(ams->ms.map, ams->al_addr); 720 721 return ams->ms.sym ? 0 : -1; 722 } 723 724 struct maps__fprintf_args { 725 FILE *fp; 726 size_t printed; 727 }; 728 729 static int maps__fprintf_cb(struct map *map, void *data) 730 { 731 struct maps__fprintf_args *args = data; 732 733 args->printed += fprintf(args->fp, "Map:"); 734 args->printed += map__fprintf(map, args->fp); 735 if (verbose > 2) { 736 args->printed += dso__fprintf(map__dso(map), args->fp); 737 args->printed += fprintf(args->fp, "--\n"); 738 } 739 return 0; 740 } 741 742 size_t maps__fprintf(struct maps *maps, FILE *fp) 743 { 744 struct maps__fprintf_args args = { 745 .fp = fp, 746 .printed = 0, 747 }; 748 749 maps__for_each_map(maps, maps__fprintf_cb, &args); 750 751 return args.printed; 752 } 753 754 /* 755 * Find first map where end > map->start. 756 * Same as find_vma() in kernel. 757 */ 758 static unsigned int first_ending_after(struct maps *maps, const struct map *map) 759 { 760 struct map **maps_by_address = maps__maps_by_address(maps); 761 int low = 0, high = (int)maps__nr_maps(maps) - 1, first = high + 1; 762 763 assert(maps__maps_by_address_sorted(maps)); 764 if (low <= high && map__end(maps_by_address[0]) > map__start(map)) 765 return 0; 766 767 while (low <= high) { 768 int mid = (low + high) / 2; 769 struct map *pos = maps_by_address[mid]; 770 771 if (map__end(pos) > map__start(map)) { 772 first = mid; 773 if (map__start(pos) <= map__start(map)) { 774 /* Entry overlaps map. */ 775 break; 776 } 777 high = mid - 1; 778 } else 779 low = mid + 1; 780 } 781 return first; 782 } 783 784 static int __maps__insert_sorted(struct maps *maps, unsigned int first_after_index, 785 struct map *new1, struct map *new2) 786 { 787 struct map **maps_by_address = maps__maps_by_address(maps); 788 struct map **maps_by_name = maps__maps_by_name(maps); 789 unsigned int nr_maps = maps__nr_maps(maps); 790 unsigned int nr_allocate = RC_CHK_ACCESS(maps)->nr_maps_allocated; 791 unsigned int to_add = new2 ? 2 : 1; 792 793 assert(maps__maps_by_address_sorted(maps)); 794 assert(first_after_index == nr_maps || 795 map__end(new1) <= map__start(maps_by_address[first_after_index])); 796 assert(!new2 || map__end(new1) <= map__start(new2)); 797 assert(first_after_index == nr_maps || !new2 || 798 map__end(new2) <= map__start(maps_by_address[first_after_index])); 799 800 if (nr_maps + to_add > nr_allocate) { 801 nr_allocate = !nr_allocate ? 32 : nr_allocate * 2; 802 803 maps_by_address = realloc(maps_by_address, nr_allocate * sizeof(new1)); 804 if (!maps_by_address) 805 return -ENOMEM; 806 807 maps__set_maps_by_address(maps, maps_by_address); 808 if (maps_by_name) { 809 maps_by_name = realloc(maps_by_name, nr_allocate * sizeof(new1)); 810 if (!maps_by_name) { 811 /* 812 * If by name fails, just disable by name and it will 813 * recompute next time it is required. 814 */ 815 __maps__free_maps_by_name(maps); 816 } 817 maps__set_maps_by_name(maps, maps_by_name); 818 } 819 RC_CHK_ACCESS(maps)->nr_maps_allocated = nr_allocate; 820 } 821 memmove(&maps_by_address[first_after_index+to_add], 822 &maps_by_address[first_after_index], 823 (nr_maps - first_after_index) * sizeof(new1)); 824 maps_by_address[first_after_index] = map__get(new1); 825 if (maps_by_name) 826 maps_by_name[nr_maps] = map__get(new1); 827 if (new2) { 828 maps_by_address[first_after_index + 1] = map__get(new2); 829 if (maps_by_name) 830 maps_by_name[nr_maps + 1] = map__get(new2); 831 } 832 RC_CHK_ACCESS(maps)->nr_maps = nr_maps + to_add; 833 maps__set_maps_by_name_sorted(maps, false); 834 map__set_kmap_maps(new1, maps); 835 map__set_kmap_maps(new2, maps); 836 837 check_invariants(maps); 838 return 0; 839 } 840 841 /* 842 * Adds new to maps, if new overlaps existing entries then the existing maps are 843 * adjusted or removed so that new fits without overlapping any entries. 844 */ 845 static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) 846 { 847 int err = 0; 848 unsigned int i, ni = INT_MAX; // Some gcc complain, but depends on maps_by_name... 849 850 if (!maps__maps_by_address_sorted(maps)) 851 __maps__sort_by_address(maps); 852 853 /* 854 * Iterate through entries where the end of the existing entry is 855 * greater-than the new map's start. 856 */ 857 for (i = first_ending_after(maps, new); i < maps__nr_maps(maps); ) { 858 struct map **maps_by_address = maps__maps_by_address(maps); 859 struct map **maps_by_name = maps__maps_by_name(maps); 860 struct map *pos = maps_by_address[i]; 861 struct map *before = NULL, *after = NULL; 862 863 /* 864 * Stop if current map starts after map->end. 865 * Maps are ordered by start: next will not overlap for sure. 866 */ 867 if (map__start(pos) >= map__end(new)) 868 break; 869 870 if (use_browser) { 871 pr_debug("overlapping maps in %s (disable tui for more info)\n", 872 dso__name(map__dso(new))); 873 } else if (verbose >= 2) { 874 pr_debug("overlapping maps:\n"); 875 map__fprintf(new, debug_file()); 876 map__fprintf(pos, debug_file()); 877 } 878 879 if (maps_by_name) 880 ni = maps__by_name_index(maps, pos); 881 882 /* 883 * Now check if we need to create new maps for areas not 884 * overlapped by the new map: 885 */ 886 if (map__start(new) > map__start(pos)) { 887 /* Map starts within existing map. Need to shorten the existing map. */ 888 before = map__clone(pos); 889 890 if (before == NULL) { 891 err = -ENOMEM; 892 goto out_err; 893 } 894 map__set_end(before, map__start(new)); 895 896 if (verbose >= 2 && !use_browser) 897 map__fprintf(before, debug_file()); 898 } 899 if (map__end(new) < map__end(pos)) { 900 /* The new map isn't as long as the existing map. */ 901 after = map__clone(pos); 902 903 if (after == NULL) { 904 map__zput(before); 905 err = -ENOMEM; 906 goto out_err; 907 } 908 909 map__set_start(after, map__end(new)); 910 map__add_pgoff(after, map__end(new) - map__start(pos)); 911 assert(map__map_ip(pos, map__end(new)) == 912 map__map_ip(after, map__end(new))); 913 914 if (verbose >= 2 && !use_browser) 915 map__fprintf(after, debug_file()); 916 } 917 /* 918 * If adding one entry, for `before` or `after`, we can replace 919 * the existing entry. If both `before` and `after` are 920 * necessary than an insert is needed. If the existing entry 921 * entirely overlaps the existing entry it can just be removed. 922 */ 923 if (before) { 924 map__put(maps_by_address[i]); 925 maps_by_address[i] = before; 926 map__set_kmap_maps(before, maps); 927 928 if (maps_by_name) { 929 map__put(maps_by_name[ni]); 930 maps_by_name[ni] = map__get(before); 931 } 932 933 /* Maps are still ordered, go to next one. */ 934 i++; 935 if (after) { 936 /* 937 * 'before' and 'after' mean 'new' split the 938 * 'pos' mapping and therefore there are no 939 * later mappings. 940 */ 941 err = __maps__insert_sorted(maps, i, new, after); 942 map__put(after); 943 check_invariants(maps); 944 return err; 945 } 946 check_invariants(maps); 947 } else if (after) { 948 /* 949 * 'after' means 'new' split 'pos' and there are no 950 * later mappings. 951 */ 952 map__put(maps_by_address[i]); 953 maps_by_address[i] = map__get(new); 954 map__set_kmap_maps(new, maps); 955 956 if (maps_by_name) { 957 map__put(maps_by_name[ni]); 958 maps_by_name[ni] = map__get(new); 959 maps__set_maps_by_name_sorted(maps, false); 960 } 961 962 err = __maps__insert_sorted(maps, i + 1, after, NULL); 963 map__put(after); 964 check_invariants(maps); 965 return err; 966 } else { 967 struct map *next = NULL; 968 unsigned int nr_maps = maps__nr_maps(maps); 969 970 if (i + 1 < nr_maps) 971 next = maps_by_address[i + 1]; 972 973 if (!next || map__start(next) >= map__end(new)) { 974 /* 975 * Replace existing mapping and end knowing 976 * there aren't later overlapping or any 977 * mappings. 978 */ 979 map__put(maps_by_address[i]); 980 maps_by_address[i] = map__get(new); 981 map__set_kmap_maps(new, maps); 982 983 if (maps_by_name) { 984 map__put(maps_by_name[ni]); 985 maps_by_name[ni] = map__get(new); 986 maps__set_maps_by_name_sorted(maps, false); 987 } 988 989 check_invariants(maps); 990 return err; 991 } 992 /* 993 * pos fully covers the previous mapping so remove 994 * it. The following is an inlined version of 995 * maps__remove that reuses the already computed 996 * indices. 997 */ 998 map__put(maps_by_address[i]); 999 memmove(&maps_by_address[i], 1000 &maps_by_address[i + 1], 1001 (nr_maps - i - 1) * sizeof(*maps_by_address)); 1002 1003 if (maps_by_name) { 1004 map__put(maps_by_name[ni]); 1005 memmove(&maps_by_name[ni], 1006 &maps_by_name[ni + 1], 1007 (nr_maps - ni - 1) * sizeof(*maps_by_name)); 1008 } 1009 --RC_CHK_ACCESS(maps)->nr_maps; 1010 check_invariants(maps); 1011 /* 1012 * Maps are ordered but no need to increase `i` as the 1013 * later maps were moved down. 1014 */ 1015 } 1016 } 1017 /* Add the map. */ 1018 err = __maps__insert_sorted(maps, i, new, NULL); 1019 out_err: 1020 return err; 1021 } 1022 1023 int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) 1024 { 1025 int err; 1026 1027 down_write(maps__lock(maps)); 1028 err = __maps__fixup_overlap_and_insert(maps, new); 1029 up_write(maps__lock(maps)); 1030 return err; 1031 } 1032 1033 int maps__copy_from(struct maps *dest, struct maps *parent) 1034 { 1035 /* Note, if struct map were immutable then cloning could use ref counts. */ 1036 struct map **parent_maps_by_address; 1037 int err = 0; 1038 unsigned int n; 1039 1040 down_write(maps__lock(dest)); 1041 down_read(maps__lock(parent)); 1042 1043 #ifdef HAVE_LIBUNWIND_SUPPORT 1044 err = unwind__prepare_access(dest, maps__e_machine(parent)); 1045 if (err) { 1046 up_read(maps__lock(parent)); 1047 up_write(maps__lock(dest)); 1048 return err; 1049 } 1050 #endif 1051 parent_maps_by_address = maps__maps_by_address(parent); 1052 n = maps__nr_maps(parent); 1053 if (maps__nr_maps(dest) == 0) { 1054 /* No existing mappings so just copy from parent to avoid reallocs in insert. */ 1055 unsigned int nr_maps_allocated = RC_CHK_ACCESS(parent)->nr_maps_allocated; 1056 struct map **dest_maps_by_address = 1057 malloc(nr_maps_allocated * sizeof(struct map *)); 1058 struct map **dest_maps_by_name = NULL; 1059 1060 if (!dest_maps_by_address) 1061 err = -ENOMEM; 1062 else { 1063 if (maps__maps_by_name(parent)) { 1064 dest_maps_by_name = 1065 malloc(nr_maps_allocated * sizeof(struct map *)); 1066 } 1067 1068 RC_CHK_ACCESS(dest)->maps_by_address = dest_maps_by_address; 1069 RC_CHK_ACCESS(dest)->maps_by_name = dest_maps_by_name; 1070 RC_CHK_ACCESS(dest)->nr_maps_allocated = nr_maps_allocated; 1071 } 1072 1073 for (unsigned int i = 0; !err && i < n; i++) { 1074 struct map *pos = parent_maps_by_address[i]; 1075 struct map *new = map__clone(pos); 1076 1077 if (!new) 1078 err = -ENOMEM; 1079 else { 1080 dest_maps_by_address[i] = new; 1081 map__set_kmap_maps(new, dest); 1082 if (dest_maps_by_name) 1083 dest_maps_by_name[i] = map__get(new); 1084 RC_CHK_ACCESS(dest)->nr_maps = i + 1; 1085 } 1086 if (err) 1087 map__put(new); 1088 } 1089 maps__set_maps_by_address_sorted(dest, maps__maps_by_address_sorted(parent)); 1090 RC_CHK_ACCESS(dest)->last_search_by_name_idx = 0; 1091 /* Values were copied into the name array in address order. */ 1092 maps__set_maps_by_name_sorted(dest, false); 1093 } else { 1094 /* Unexpected copying to a maps containing entries. */ 1095 for (unsigned int i = 0; !err && i < n; i++) { 1096 struct map *pos = parent_maps_by_address[i]; 1097 struct map *new = map__clone(pos); 1098 1099 if (!new) 1100 err = -ENOMEM; 1101 else { 1102 err = __maps__insert(dest, new); 1103 } 1104 map__put(new); 1105 } 1106 } 1107 check_invariants(dest); 1108 1109 up_read(maps__lock(parent)); 1110 up_write(maps__lock(dest)); 1111 return err; 1112 } 1113 1114 static int map__addr_cmp(const void *key, const void *entry) 1115 { 1116 const u64 ip = *(const u64 *)key; 1117 const struct map *map = *(const struct map * const *)entry; 1118 1119 if (ip < map__start(map)) 1120 return -1; 1121 if (ip >= map__end(map)) 1122 return 1; 1123 return 0; 1124 } 1125 1126 struct map *maps__find(struct maps *maps, u64 ip) 1127 { 1128 struct map *result = NULL; 1129 bool done = false; 1130 1131 /* See locking/sorting note. */ 1132 while (!done) { 1133 down_read(maps__lock(maps)); 1134 if (maps__maps_by_address_sorted(maps)) { 1135 struct map **mapp = NULL; 1136 struct map **maps_by_address = maps__maps_by_address(maps); 1137 unsigned int nr_maps = maps__nr_maps(maps); 1138 1139 if (maps_by_address && nr_maps) 1140 mapp = bsearch(&ip, maps_by_address, nr_maps, sizeof(*mapp), 1141 map__addr_cmp); 1142 if (mapp) 1143 result = map__get(*mapp); 1144 done = true; 1145 } 1146 up_read(maps__lock(maps)); 1147 if (!done) 1148 maps__sort_by_address(maps); 1149 } 1150 return result; 1151 } 1152 1153 static int map__strcmp_name(const void *name, const void *b) 1154 { 1155 const struct dso *dso = map__dso(*(const struct map **)b); 1156 1157 return strcmp(name, dso__short_name(dso)); 1158 } 1159 1160 struct map *maps__find_by_name(struct maps *maps, const char *name) 1161 { 1162 struct map *result = NULL; 1163 bool done = false; 1164 1165 /* See locking/sorting note. */ 1166 while (!done) { 1167 unsigned int i; 1168 1169 down_read(maps__lock(maps)); 1170 1171 /* First check last found entry. */ 1172 i = RC_CHK_ACCESS(maps)->last_search_by_name_idx; 1173 if (i < maps__nr_maps(maps) && maps__maps_by_name(maps)) { 1174 struct dso *dso = map__dso(maps__maps_by_name(maps)[i]); 1175 1176 if (dso && strcmp(dso__short_name(dso), name) == 0) { 1177 result = map__get(maps__maps_by_name(maps)[i]); 1178 done = true; 1179 } 1180 } 1181 1182 /* Second search sorted array. */ 1183 if (!done && maps__maps_by_name_sorted(maps)) { 1184 struct map **mapp = 1185 bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps), 1186 sizeof(*mapp), map__strcmp_name); 1187 1188 if (mapp) { 1189 result = map__get(*mapp); 1190 i = mapp - maps__maps_by_name(maps); 1191 RC_CHK_ACCESS(maps)->last_search_by_name_idx = i; 1192 } 1193 done = true; 1194 } 1195 up_read(maps__lock(maps)); 1196 if (!done) { 1197 /* Sort and retry binary search. */ 1198 if (maps__sort_by_name(maps)) { 1199 /* 1200 * Memory allocation failed do linear search 1201 * through address sorted maps. 1202 */ 1203 struct map **maps_by_address; 1204 unsigned int n; 1205 1206 down_read(maps__lock(maps)); 1207 maps_by_address = maps__maps_by_address(maps); 1208 n = maps__nr_maps(maps); 1209 for (i = 0; i < n; i++) { 1210 struct map *pos = maps_by_address[i]; 1211 struct dso *dso = map__dso(pos); 1212 1213 if (dso && strcmp(dso__short_name(dso), name) == 0) { 1214 result = map__get(pos); 1215 break; 1216 } 1217 } 1218 up_read(maps__lock(maps)); 1219 done = true; 1220 } 1221 } 1222 } 1223 return result; 1224 } 1225 1226 struct map *maps__find_next_entry(struct maps *maps, struct map *map) 1227 { 1228 unsigned int i; 1229 struct map *result = NULL; 1230 1231 down_read(maps__lock(maps)); 1232 while (!maps__maps_by_address_sorted(maps)) { 1233 up_read(maps__lock(maps)); 1234 maps__sort_by_address(maps); 1235 down_read(maps__lock(maps)); 1236 } 1237 i = maps__by_address_index(maps, map); 1238 if (++i < maps__nr_maps(maps)) 1239 result = map__get(maps__maps_by_address(maps)[i]); 1240 1241 up_read(maps__lock(maps)); 1242 return result; 1243 } 1244 1245 void maps__fixup_end(struct maps *maps) 1246 { 1247 struct map **maps_by_address; 1248 unsigned int n; 1249 1250 down_write(maps__lock(maps)); 1251 if (!maps__maps_by_address_sorted(maps)) 1252 __maps__sort_by_address(maps); 1253 1254 maps_by_address = maps__maps_by_address(maps); 1255 n = maps__nr_maps(maps); 1256 for (unsigned int i = 1; i < n; i++) { 1257 struct map *prev = maps_by_address[i - 1]; 1258 struct map *curr = maps_by_address[i]; 1259 1260 if (!map__end(prev) || map__end(prev) > map__start(curr)) 1261 map__set_end(prev, map__start(curr)); 1262 } 1263 1264 /* 1265 * We still haven't the actual symbols, so guess the 1266 * last map final address. 1267 */ 1268 if (n > 0 && !map__end(maps_by_address[n - 1])) 1269 map__set_end(maps_by_address[n - 1], ~0ULL); 1270 1271 RC_CHK_ACCESS(maps)->ends_broken = false; 1272 check_invariants(maps); 1273 1274 up_write(maps__lock(maps)); 1275 } 1276 1277 /* 1278 * Merges map into maps by splitting the new map within the existing map 1279 * regions. 1280 */ 1281 int maps__merge_in(struct maps *kmaps, struct map *new_map) 1282 { 1283 unsigned int first_after_, kmaps__nr_maps; 1284 struct map **kmaps_maps_by_address; 1285 struct map **merged_maps_by_address; 1286 unsigned int merged_nr_maps_allocated; 1287 1288 /* First try under a read lock. */ 1289 while (true) { 1290 down_read(maps__lock(kmaps)); 1291 if (maps__maps_by_address_sorted(kmaps)) 1292 break; 1293 1294 up_read(maps__lock(kmaps)); 1295 1296 /* First after binary search requires sorted maps. Sort and try again. */ 1297 maps__sort_by_address(kmaps); 1298 } 1299 first_after_ = first_ending_after(kmaps, new_map); 1300 kmaps_maps_by_address = maps__maps_by_address(kmaps); 1301 1302 if (first_after_ >= maps__nr_maps(kmaps) || 1303 map__start(kmaps_maps_by_address[first_after_]) >= map__end(new_map)) { 1304 /* No overlap so regular insert suffices. */ 1305 up_read(maps__lock(kmaps)); 1306 return maps__insert(kmaps, new_map); 1307 } 1308 up_read(maps__lock(kmaps)); 1309 1310 /* Plain insert with a read-lock failed, try again now with the write lock. */ 1311 down_write(maps__lock(kmaps)); 1312 if (!maps__maps_by_address_sorted(kmaps)) 1313 __maps__sort_by_address(kmaps); 1314 1315 first_after_ = first_ending_after(kmaps, new_map); 1316 kmaps_maps_by_address = maps__maps_by_address(kmaps); 1317 kmaps__nr_maps = maps__nr_maps(kmaps); 1318 1319 if (first_after_ >= kmaps__nr_maps || 1320 map__start(kmaps_maps_by_address[first_after_]) >= map__end(new_map)) { 1321 /* No overlap so regular insert suffices. */ 1322 int ret = __maps__insert(kmaps, new_map); 1323 1324 check_invariants(kmaps); 1325 up_write(maps__lock(kmaps)); 1326 return ret; 1327 } 1328 /* Array to merge into, possibly 1 more for the sake of new_map. */ 1329 merged_nr_maps_allocated = RC_CHK_ACCESS(kmaps)->nr_maps_allocated; 1330 if (kmaps__nr_maps + 1 == merged_nr_maps_allocated) 1331 merged_nr_maps_allocated++; 1332 1333 merged_maps_by_address = malloc(merged_nr_maps_allocated * sizeof(*merged_maps_by_address)); 1334 if (!merged_maps_by_address) { 1335 up_write(maps__lock(kmaps)); 1336 return -ENOMEM; 1337 } 1338 maps__set_maps_by_address(kmaps, merged_maps_by_address); 1339 maps__set_maps_by_address_sorted(kmaps, true); 1340 __maps__free_maps_by_name(kmaps); 1341 maps__set_nr_maps_allocated(kmaps, merged_nr_maps_allocated); 1342 1343 /* Copy entries before the new_map that can't overlap. */ 1344 for (unsigned int i = 0; i < first_after_; i++) 1345 merged_maps_by_address[i] = map__get(kmaps_maps_by_address[i]); 1346 1347 maps__set_nr_maps(kmaps, first_after_); 1348 1349 /* Add the new map, it will be split when the later overlapping mappings are added. */ 1350 __maps__insert(kmaps, new_map); 1351 1352 /* Insert mappings after new_map, splitting new_map in the process. */ 1353 for (unsigned int i = first_after_; i < kmaps__nr_maps; i++) 1354 __maps__fixup_overlap_and_insert(kmaps, kmaps_maps_by_address[i]); 1355 1356 /* Copy the maps from merged into kmaps. */ 1357 for (unsigned int i = 0; i < kmaps__nr_maps; i++) 1358 map__zput(kmaps_maps_by_address[i]); 1359 1360 free(kmaps_maps_by_address); 1361 check_invariants(kmaps); 1362 up_write(maps__lock(kmaps)); 1363 return 0; 1364 } 1365 1366 void maps__load_first(struct maps *maps) 1367 { 1368 down_read(maps__lock(maps)); 1369 1370 if (maps__nr_maps(maps) > 0) 1371 map__load(maps__maps_by_address(maps)[0]); 1372 1373 up_read(maps__lock(maps)); 1374 } 1375