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