1 // SPDX-License-Identifier: GPL-2.0 2 #include <asm/bug.h> 3 #include <linux/kernel.h> 4 #include <sys/time.h> 5 #include <sys/resource.h> 6 #include <sys/types.h> 7 #include <sys/stat.h> 8 #include <unistd.h> 9 #include <errno.h> 10 #include <fcntl.h> 11 #include <libgen.h> 12 #include "compress.h" 13 #include "namespaces.h" 14 #include "path.h" 15 #include "map.h" 16 #include "symbol.h" 17 #include "srcline.h" 18 #include "dso.h" 19 #include "machine.h" 20 #include "auxtrace.h" 21 #include "util.h" 22 #include "debug.h" 23 #include "string2.h" 24 #include "vdso.h" 25 26 static const char * const debuglink_paths[] = { 27 "%.0s%s", 28 "%s/%s", 29 "%s/.debug/%s", 30 "/usr/lib/debug%s/%s" 31 }; 32 33 char dso__symtab_origin(const struct dso *dso) 34 { 35 static const char origin[] = { 36 [DSO_BINARY_TYPE__KALLSYMS] = 'k', 37 [DSO_BINARY_TYPE__VMLINUX] = 'v', 38 [DSO_BINARY_TYPE__JAVA_JIT] = 'j', 39 [DSO_BINARY_TYPE__DEBUGLINK] = 'l', 40 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B', 41 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D', 42 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f', 43 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u', 44 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o', 45 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b', 46 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd', 47 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K', 48 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm', 49 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g', 50 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G', 51 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M', 52 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V', 53 }; 54 55 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND) 56 return '!'; 57 return origin[dso->symtab_type]; 58 } 59 60 int dso__read_binary_type_filename(const struct dso *dso, 61 enum dso_binary_type type, 62 char *root_dir, char *filename, size_t size) 63 { 64 char build_id_hex[SBUILD_ID_SIZE]; 65 int ret = 0; 66 size_t len; 67 68 switch (type) { 69 case DSO_BINARY_TYPE__DEBUGLINK: 70 { 71 const char *last_slash; 72 char dso_dir[PATH_MAX]; 73 char symfile[PATH_MAX]; 74 unsigned int i; 75 76 len = __symbol__join_symfs(filename, size, dso->long_name); 77 last_slash = filename + len; 78 while (last_slash != filename && *last_slash != '/') 79 last_slash--; 80 81 strncpy(dso_dir, filename, last_slash - filename); 82 dso_dir[last_slash-filename] = '\0'; 83 84 if (!is_regular_file(filename)) { 85 ret = -1; 86 break; 87 } 88 89 ret = filename__read_debuglink(filename, symfile, PATH_MAX); 90 if (ret) 91 break; 92 93 /* Check predefined locations where debug file might reside */ 94 ret = -1; 95 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) { 96 snprintf(filename, size, 97 debuglink_paths[i], dso_dir, symfile); 98 if (is_regular_file(filename)) { 99 ret = 0; 100 break; 101 } 102 } 103 104 break; 105 } 106 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 107 if (dso__build_id_filename(dso, filename, size, false) == NULL) 108 ret = -1; 109 break; 110 111 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 112 if (dso__build_id_filename(dso, filename, size, true) == NULL) 113 ret = -1; 114 break; 115 116 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 117 len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); 118 snprintf(filename + len, size - len, "%s.debug", dso->long_name); 119 break; 120 121 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 122 len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); 123 snprintf(filename + len, size - len, "%s", dso->long_name); 124 break; 125 126 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 127 { 128 const char *last_slash; 129 size_t dir_size; 130 131 last_slash = dso->long_name + dso->long_name_len; 132 while (last_slash != dso->long_name && *last_slash != '/') 133 last_slash--; 134 135 len = __symbol__join_symfs(filename, size, ""); 136 dir_size = last_slash - dso->long_name + 2; 137 if (dir_size > (size - len)) { 138 ret = -1; 139 break; 140 } 141 len += scnprintf(filename + len, dir_size, "%s", dso->long_name); 142 len += scnprintf(filename + len , size - len, ".debug%s", 143 last_slash); 144 break; 145 } 146 147 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 148 if (!dso->has_build_id) { 149 ret = -1; 150 break; 151 } 152 153 build_id__sprintf(dso->build_id, 154 sizeof(dso->build_id), 155 build_id_hex); 156 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/"); 157 snprintf(filename + len, size - len, "%.2s/%s.debug", 158 build_id_hex, build_id_hex + 2); 159 break; 160 161 case DSO_BINARY_TYPE__VMLINUX: 162 case DSO_BINARY_TYPE__GUEST_VMLINUX: 163 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 164 __symbol__join_symfs(filename, size, dso->long_name); 165 break; 166 167 case DSO_BINARY_TYPE__GUEST_KMODULE: 168 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 169 path__join3(filename, size, symbol_conf.symfs, 170 root_dir, dso->long_name); 171 break; 172 173 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 174 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 175 __symbol__join_symfs(filename, size, dso->long_name); 176 break; 177 178 case DSO_BINARY_TYPE__KCORE: 179 case DSO_BINARY_TYPE__GUEST_KCORE: 180 snprintf(filename, size, "%s", dso->long_name); 181 break; 182 183 default: 184 case DSO_BINARY_TYPE__KALLSYMS: 185 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 186 case DSO_BINARY_TYPE__JAVA_JIT: 187 case DSO_BINARY_TYPE__NOT_FOUND: 188 ret = -1; 189 break; 190 } 191 192 return ret; 193 } 194 195 enum { 196 COMP_ID__NONE = 0, 197 }; 198 199 static const struct { 200 const char *fmt; 201 int (*decompress)(const char *input, int output); 202 bool (*is_compressed)(const char *input); 203 } compressions[] = { 204 [COMP_ID__NONE] = { .fmt = NULL, }, 205 #ifdef HAVE_ZLIB_SUPPORT 206 { "gz", gzip_decompress_to_file, gzip_is_compressed }, 207 #endif 208 #ifdef HAVE_LZMA_SUPPORT 209 { "xz", lzma_decompress_to_file, lzma_is_compressed }, 210 #endif 211 { NULL, NULL, NULL }, 212 }; 213 214 static int is_supported_compression(const char *ext) 215 { 216 unsigned i; 217 218 for (i = 1; compressions[i].fmt; i++) { 219 if (!strcmp(ext, compressions[i].fmt)) 220 return i; 221 } 222 return COMP_ID__NONE; 223 } 224 225 bool is_kernel_module(const char *pathname, int cpumode) 226 { 227 struct kmod_path m; 228 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK; 229 230 WARN_ONCE(mode != cpumode, 231 "Internal error: passing unmasked cpumode (%x) to is_kernel_module", 232 cpumode); 233 234 switch (mode) { 235 case PERF_RECORD_MISC_USER: 236 case PERF_RECORD_MISC_HYPERVISOR: 237 case PERF_RECORD_MISC_GUEST_USER: 238 return false; 239 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */ 240 default: 241 if (kmod_path__parse(&m, pathname)) { 242 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.", 243 pathname); 244 return true; 245 } 246 } 247 248 return m.kmod; 249 } 250 251 bool dso__needs_decompress(struct dso *dso) 252 { 253 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || 254 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; 255 } 256 257 static int decompress_kmodule(struct dso *dso, const char *name, 258 char *pathname, size_t len) 259 { 260 char tmpbuf[] = KMOD_DECOMP_NAME; 261 int fd = -1; 262 263 if (!dso__needs_decompress(dso)) 264 return -1; 265 266 if (dso->comp == COMP_ID__NONE) 267 return -1; 268 269 /* 270 * We have proper compression id for DSO and yet the file 271 * behind the 'name' can still be plain uncompressed object. 272 * 273 * The reason is behind the logic we open the DSO object files, 274 * when we try all possible 'debug' objects until we find the 275 * data. So even if the DSO is represented by 'krava.xz' module, 276 * we can end up here opening ~/.debug/....23432432/debug' file 277 * which is not compressed. 278 * 279 * To keep this transparent, we detect this and return the file 280 * descriptor to the uncompressed file. 281 */ 282 if (!compressions[dso->comp].is_compressed(name)) 283 return open(name, O_RDONLY); 284 285 fd = mkstemp(tmpbuf); 286 if (fd < 0) { 287 dso->load_errno = errno; 288 return -1; 289 } 290 291 if (compressions[dso->comp].decompress(name, fd)) { 292 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE; 293 close(fd); 294 fd = -1; 295 } 296 297 if (!pathname || (fd < 0)) 298 unlink(tmpbuf); 299 300 if (pathname && (fd >= 0)) 301 strlcpy(pathname, tmpbuf, len); 302 303 return fd; 304 } 305 306 int dso__decompress_kmodule_fd(struct dso *dso, const char *name) 307 { 308 return decompress_kmodule(dso, name, NULL, 0); 309 } 310 311 int dso__decompress_kmodule_path(struct dso *dso, const char *name, 312 char *pathname, size_t len) 313 { 314 int fd = decompress_kmodule(dso, name, pathname, len); 315 316 close(fd); 317 return fd >= 0 ? 0 : -1; 318 } 319 320 /* 321 * Parses kernel module specified in @path and updates 322 * @m argument like: 323 * 324 * @comp - true if @path contains supported compression suffix, 325 * false otherwise 326 * @kmod - true if @path contains '.ko' suffix in right position, 327 * false otherwise 328 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name 329 * of the kernel module without suffixes, otherwise strudup-ed 330 * base name of @path 331 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string 332 * the compression suffix 333 * 334 * Returns 0 if there's no strdup error, -ENOMEM otherwise. 335 */ 336 int __kmod_path__parse(struct kmod_path *m, const char *path, 337 bool alloc_name) 338 { 339 const char *name = strrchr(path, '/'); 340 const char *ext = strrchr(path, '.'); 341 bool is_simple_name = false; 342 343 memset(m, 0x0, sizeof(*m)); 344 name = name ? name + 1 : path; 345 346 /* 347 * '.' is also a valid character for module name. For example: 348 * [aaa.bbb] is a valid module name. '[' should have higher 349 * priority than '.ko' suffix. 350 * 351 * The kernel names are from machine__mmap_name. Such 352 * name should belong to kernel itself, not kernel module. 353 */ 354 if (name[0] == '[') { 355 is_simple_name = true; 356 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) || 357 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) || 358 (strncmp(name, "[vdso]", 6) == 0) || 359 (strncmp(name, "[vdso32]", 8) == 0) || 360 (strncmp(name, "[vdsox32]", 9) == 0) || 361 (strncmp(name, "[vsyscall]", 10) == 0)) { 362 m->kmod = false; 363 364 } else 365 m->kmod = true; 366 } 367 368 /* No extension, just return name. */ 369 if ((ext == NULL) || is_simple_name) { 370 if (alloc_name) { 371 m->name = strdup(name); 372 return m->name ? 0 : -ENOMEM; 373 } 374 return 0; 375 } 376 377 m->comp = is_supported_compression(ext + 1); 378 if (m->comp > COMP_ID__NONE) 379 ext -= 3; 380 381 /* Check .ko extension only if there's enough name left. */ 382 if (ext > name) 383 m->kmod = !strncmp(ext, ".ko", 3); 384 385 if (alloc_name) { 386 if (m->kmod) { 387 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1) 388 return -ENOMEM; 389 } else { 390 if (asprintf(&m->name, "%s", name) == -1) 391 return -ENOMEM; 392 } 393 394 strxfrchar(m->name, '-', '_'); 395 } 396 397 return 0; 398 } 399 400 void dso__set_module_info(struct dso *dso, struct kmod_path *m, 401 struct machine *machine) 402 { 403 if (machine__is_host(machine)) 404 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE; 405 else 406 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE; 407 408 /* _KMODULE_COMP should be next to _KMODULE */ 409 if (m->kmod && m->comp) { 410 dso->symtab_type++; 411 dso->comp = m->comp; 412 } 413 414 dso__set_short_name(dso, strdup(m->name), true); 415 } 416 417 /* 418 * Global list of open DSOs and the counter. 419 */ 420 static LIST_HEAD(dso__data_open); 421 static long dso__data_open_cnt; 422 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER; 423 424 static void dso__list_add(struct dso *dso) 425 { 426 list_add_tail(&dso->data.open_entry, &dso__data_open); 427 dso__data_open_cnt++; 428 } 429 430 static void dso__list_del(struct dso *dso) 431 { 432 list_del(&dso->data.open_entry); 433 WARN_ONCE(dso__data_open_cnt <= 0, 434 "DSO data fd counter out of bounds."); 435 dso__data_open_cnt--; 436 } 437 438 static void close_first_dso(void); 439 440 static int do_open(char *name) 441 { 442 int fd; 443 char sbuf[STRERR_BUFSIZE]; 444 445 do { 446 fd = open(name, O_RDONLY|O_CLOEXEC); 447 if (fd >= 0) 448 return fd; 449 450 pr_debug("dso open failed: %s\n", 451 str_error_r(errno, sbuf, sizeof(sbuf))); 452 if (!dso__data_open_cnt || errno != EMFILE) 453 break; 454 455 close_first_dso(); 456 } while (1); 457 458 return -1; 459 } 460 461 static int __open_dso(struct dso *dso, struct machine *machine) 462 { 463 int fd = -EINVAL; 464 char *root_dir = (char *)""; 465 char *name = malloc(PATH_MAX); 466 bool decomp = false; 467 468 if (!name) 469 return -ENOMEM; 470 471 if (machine) 472 root_dir = machine->root_dir; 473 474 if (dso__read_binary_type_filename(dso, dso->binary_type, 475 root_dir, name, PATH_MAX)) 476 goto out; 477 478 if (!is_regular_file(name)) 479 goto out; 480 481 if (dso__needs_decompress(dso)) { 482 char newpath[KMOD_DECOMP_LEN]; 483 size_t len = sizeof(newpath); 484 485 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) { 486 fd = -dso->load_errno; 487 goto out; 488 } 489 490 decomp = true; 491 strcpy(name, newpath); 492 } 493 494 fd = do_open(name); 495 496 if (decomp) 497 unlink(name); 498 499 out: 500 free(name); 501 return fd; 502 } 503 504 static void check_data_close(void); 505 506 /** 507 * dso_close - Open DSO data file 508 * @dso: dso object 509 * 510 * Open @dso's data file descriptor and updates 511 * list/count of open DSO objects. 512 */ 513 static int open_dso(struct dso *dso, struct machine *machine) 514 { 515 int fd; 516 struct nscookie nsc; 517 518 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) 519 nsinfo__mountns_enter(dso->nsinfo, &nsc); 520 fd = __open_dso(dso, machine); 521 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) 522 nsinfo__mountns_exit(&nsc); 523 524 if (fd >= 0) { 525 dso__list_add(dso); 526 /* 527 * Check if we crossed the allowed number 528 * of opened DSOs and close one if needed. 529 */ 530 check_data_close(); 531 } 532 533 return fd; 534 } 535 536 static void close_data_fd(struct dso *dso) 537 { 538 if (dso->data.fd >= 0) { 539 close(dso->data.fd); 540 dso->data.fd = -1; 541 dso->data.file_size = 0; 542 dso__list_del(dso); 543 } 544 } 545 546 /** 547 * dso_close - Close DSO data file 548 * @dso: dso object 549 * 550 * Close @dso's data file descriptor and updates 551 * list/count of open DSO objects. 552 */ 553 static void close_dso(struct dso *dso) 554 { 555 close_data_fd(dso); 556 } 557 558 static void close_first_dso(void) 559 { 560 struct dso *dso; 561 562 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry); 563 close_dso(dso); 564 } 565 566 static rlim_t get_fd_limit(void) 567 { 568 struct rlimit l; 569 rlim_t limit = 0; 570 571 /* Allow half of the current open fd limit. */ 572 if (getrlimit(RLIMIT_NOFILE, &l) == 0) { 573 if (l.rlim_cur == RLIM_INFINITY) 574 limit = l.rlim_cur; 575 else 576 limit = l.rlim_cur / 2; 577 } else { 578 pr_err("failed to get fd limit\n"); 579 limit = 1; 580 } 581 582 return limit; 583 } 584 585 static rlim_t fd_limit; 586 587 /* 588 * Used only by tests/dso-data.c to reset the environment 589 * for tests. I dont expect we should change this during 590 * standard runtime. 591 */ 592 void reset_fd_limit(void) 593 { 594 fd_limit = 0; 595 } 596 597 static bool may_cache_fd(void) 598 { 599 if (!fd_limit) 600 fd_limit = get_fd_limit(); 601 602 if (fd_limit == RLIM_INFINITY) 603 return true; 604 605 return fd_limit > (rlim_t) dso__data_open_cnt; 606 } 607 608 /* 609 * Check and close LRU dso if we crossed allowed limit 610 * for opened dso file descriptors. The limit is half 611 * of the RLIMIT_NOFILE files opened. 612 */ 613 static void check_data_close(void) 614 { 615 bool cache_fd = may_cache_fd(); 616 617 if (!cache_fd) 618 close_first_dso(); 619 } 620 621 /** 622 * dso__data_close - Close DSO data file 623 * @dso: dso object 624 * 625 * External interface to close @dso's data file descriptor. 626 */ 627 void dso__data_close(struct dso *dso) 628 { 629 pthread_mutex_lock(&dso__data_open_lock); 630 close_dso(dso); 631 pthread_mutex_unlock(&dso__data_open_lock); 632 } 633 634 static void try_to_open_dso(struct dso *dso, struct machine *machine) 635 { 636 enum dso_binary_type binary_type_data[] = { 637 DSO_BINARY_TYPE__BUILD_ID_CACHE, 638 DSO_BINARY_TYPE__SYSTEM_PATH_DSO, 639 DSO_BINARY_TYPE__NOT_FOUND, 640 }; 641 int i = 0; 642 643 if (dso->data.fd >= 0) 644 return; 645 646 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) { 647 dso->data.fd = open_dso(dso, machine); 648 goto out; 649 } 650 651 do { 652 dso->binary_type = binary_type_data[i++]; 653 654 dso->data.fd = open_dso(dso, machine); 655 if (dso->data.fd >= 0) 656 goto out; 657 658 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND); 659 out: 660 if (dso->data.fd >= 0) 661 dso->data.status = DSO_DATA_STATUS_OK; 662 else 663 dso->data.status = DSO_DATA_STATUS_ERROR; 664 } 665 666 /** 667 * dso__data_get_fd - Get dso's data file descriptor 668 * @dso: dso object 669 * @machine: machine object 670 * 671 * External interface to find dso's file, open it and 672 * returns file descriptor. It should be paired with 673 * dso__data_put_fd() if it returns non-negative value. 674 */ 675 int dso__data_get_fd(struct dso *dso, struct machine *machine) 676 { 677 if (dso->data.status == DSO_DATA_STATUS_ERROR) 678 return -1; 679 680 if (pthread_mutex_lock(&dso__data_open_lock) < 0) 681 return -1; 682 683 try_to_open_dso(dso, machine); 684 685 if (dso->data.fd < 0) 686 pthread_mutex_unlock(&dso__data_open_lock); 687 688 return dso->data.fd; 689 } 690 691 void dso__data_put_fd(struct dso *dso __maybe_unused) 692 { 693 pthread_mutex_unlock(&dso__data_open_lock); 694 } 695 696 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) 697 { 698 u32 flag = 1 << by; 699 700 if (dso->data.status_seen & flag) 701 return true; 702 703 dso->data.status_seen |= flag; 704 705 return false; 706 } 707 708 static void 709 dso_cache__free(struct dso *dso) 710 { 711 struct rb_root *root = &dso->data.cache; 712 struct rb_node *next = rb_first(root); 713 714 pthread_mutex_lock(&dso->lock); 715 while (next) { 716 struct dso_cache *cache; 717 718 cache = rb_entry(next, struct dso_cache, rb_node); 719 next = rb_next(&cache->rb_node); 720 rb_erase(&cache->rb_node, root); 721 free(cache); 722 } 723 pthread_mutex_unlock(&dso->lock); 724 } 725 726 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset) 727 { 728 const struct rb_root *root = &dso->data.cache; 729 struct rb_node * const *p = &root->rb_node; 730 const struct rb_node *parent = NULL; 731 struct dso_cache *cache; 732 733 while (*p != NULL) { 734 u64 end; 735 736 parent = *p; 737 cache = rb_entry(parent, struct dso_cache, rb_node); 738 end = cache->offset + DSO__DATA_CACHE_SIZE; 739 740 if (offset < cache->offset) 741 p = &(*p)->rb_left; 742 else if (offset >= end) 743 p = &(*p)->rb_right; 744 else 745 return cache; 746 } 747 748 return NULL; 749 } 750 751 static struct dso_cache * 752 dso_cache__insert(struct dso *dso, struct dso_cache *new) 753 { 754 struct rb_root *root = &dso->data.cache; 755 struct rb_node **p = &root->rb_node; 756 struct rb_node *parent = NULL; 757 struct dso_cache *cache; 758 u64 offset = new->offset; 759 760 pthread_mutex_lock(&dso->lock); 761 while (*p != NULL) { 762 u64 end; 763 764 parent = *p; 765 cache = rb_entry(parent, struct dso_cache, rb_node); 766 end = cache->offset + DSO__DATA_CACHE_SIZE; 767 768 if (offset < cache->offset) 769 p = &(*p)->rb_left; 770 else if (offset >= end) 771 p = &(*p)->rb_right; 772 else 773 goto out; 774 } 775 776 rb_link_node(&new->rb_node, parent, p); 777 rb_insert_color(&new->rb_node, root); 778 779 cache = NULL; 780 out: 781 pthread_mutex_unlock(&dso->lock); 782 return cache; 783 } 784 785 static ssize_t 786 dso_cache__memcpy(struct dso_cache *cache, u64 offset, 787 u8 *data, u64 size) 788 { 789 u64 cache_offset = offset - cache->offset; 790 u64 cache_size = min(cache->size - cache_offset, size); 791 792 memcpy(data, cache->data + cache_offset, cache_size); 793 return cache_size; 794 } 795 796 static ssize_t 797 dso_cache__read(struct dso *dso, struct machine *machine, 798 u64 offset, u8 *data, ssize_t size) 799 { 800 struct dso_cache *cache; 801 struct dso_cache *old; 802 ssize_t ret; 803 804 do { 805 u64 cache_offset; 806 807 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); 808 if (!cache) 809 return -ENOMEM; 810 811 pthread_mutex_lock(&dso__data_open_lock); 812 813 /* 814 * dso->data.fd might be closed if other thread opened another 815 * file (dso) due to open file limit (RLIMIT_NOFILE). 816 */ 817 try_to_open_dso(dso, machine); 818 819 if (dso->data.fd < 0) { 820 ret = -errno; 821 dso->data.status = DSO_DATA_STATUS_ERROR; 822 break; 823 } 824 825 cache_offset = offset & DSO__DATA_CACHE_MASK; 826 827 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset); 828 if (ret <= 0) 829 break; 830 831 cache->offset = cache_offset; 832 cache->size = ret; 833 } while (0); 834 835 pthread_mutex_unlock(&dso__data_open_lock); 836 837 if (ret > 0) { 838 old = dso_cache__insert(dso, cache); 839 if (old) { 840 /* we lose the race */ 841 free(cache); 842 cache = old; 843 } 844 845 ret = dso_cache__memcpy(cache, offset, data, size); 846 } 847 848 if (ret <= 0) 849 free(cache); 850 851 return ret; 852 } 853 854 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine, 855 u64 offset, u8 *data, ssize_t size) 856 { 857 struct dso_cache *cache; 858 859 cache = dso_cache__find(dso, offset); 860 if (cache) 861 return dso_cache__memcpy(cache, offset, data, size); 862 else 863 return dso_cache__read(dso, machine, offset, data, size); 864 } 865 866 /* 867 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks 868 * in the rb_tree. Any read to already cached data is served 869 * by cached data. 870 */ 871 static ssize_t cached_read(struct dso *dso, struct machine *machine, 872 u64 offset, u8 *data, ssize_t size) 873 { 874 ssize_t r = 0; 875 u8 *p = data; 876 877 do { 878 ssize_t ret; 879 880 ret = dso_cache_read(dso, machine, offset, p, size); 881 if (ret < 0) 882 return ret; 883 884 /* Reached EOF, return what we have. */ 885 if (!ret) 886 break; 887 888 BUG_ON(ret > size); 889 890 r += ret; 891 p += ret; 892 offset += ret; 893 size -= ret; 894 895 } while (size); 896 897 return r; 898 } 899 900 int dso__data_file_size(struct dso *dso, struct machine *machine) 901 { 902 int ret = 0; 903 struct stat st; 904 char sbuf[STRERR_BUFSIZE]; 905 906 if (dso->data.file_size) 907 return 0; 908 909 if (dso->data.status == DSO_DATA_STATUS_ERROR) 910 return -1; 911 912 pthread_mutex_lock(&dso__data_open_lock); 913 914 /* 915 * dso->data.fd might be closed if other thread opened another 916 * file (dso) due to open file limit (RLIMIT_NOFILE). 917 */ 918 try_to_open_dso(dso, machine); 919 920 if (dso->data.fd < 0) { 921 ret = -errno; 922 dso->data.status = DSO_DATA_STATUS_ERROR; 923 goto out; 924 } 925 926 if (fstat(dso->data.fd, &st) < 0) { 927 ret = -errno; 928 pr_err("dso cache fstat failed: %s\n", 929 str_error_r(errno, sbuf, sizeof(sbuf))); 930 dso->data.status = DSO_DATA_STATUS_ERROR; 931 goto out; 932 } 933 dso->data.file_size = st.st_size; 934 935 out: 936 pthread_mutex_unlock(&dso__data_open_lock); 937 return ret; 938 } 939 940 /** 941 * dso__data_size - Return dso data size 942 * @dso: dso object 943 * @machine: machine object 944 * 945 * Return: dso data size 946 */ 947 off_t dso__data_size(struct dso *dso, struct machine *machine) 948 { 949 if (dso__data_file_size(dso, machine)) 950 return -1; 951 952 /* For now just estimate dso data size is close to file size */ 953 return dso->data.file_size; 954 } 955 956 static ssize_t data_read_offset(struct dso *dso, struct machine *machine, 957 u64 offset, u8 *data, ssize_t size) 958 { 959 if (dso__data_file_size(dso, machine)) 960 return -1; 961 962 /* Check the offset sanity. */ 963 if (offset > dso->data.file_size) 964 return -1; 965 966 if (offset + size < offset) 967 return -1; 968 969 return cached_read(dso, machine, offset, data, size); 970 } 971 972 /** 973 * dso__data_read_offset - Read data from dso file offset 974 * @dso: dso object 975 * @machine: machine object 976 * @offset: file offset 977 * @data: buffer to store data 978 * @size: size of the @data buffer 979 * 980 * External interface to read data from dso file offset. Open 981 * dso data file and use cached_read to get the data. 982 */ 983 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, 984 u64 offset, u8 *data, ssize_t size) 985 { 986 if (dso->data.status == DSO_DATA_STATUS_ERROR) 987 return -1; 988 989 return data_read_offset(dso, machine, offset, data, size); 990 } 991 992 /** 993 * dso__data_read_addr - Read data from dso address 994 * @dso: dso object 995 * @machine: machine object 996 * @add: virtual memory address 997 * @data: buffer to store data 998 * @size: size of the @data buffer 999 * 1000 * External interface to read data from dso address. 1001 */ 1002 ssize_t dso__data_read_addr(struct dso *dso, struct map *map, 1003 struct machine *machine, u64 addr, 1004 u8 *data, ssize_t size) 1005 { 1006 u64 offset = map->map_ip(map, addr); 1007 return dso__data_read_offset(dso, machine, offset, data, size); 1008 } 1009 1010 struct map *dso__new_map(const char *name) 1011 { 1012 struct map *map = NULL; 1013 struct dso *dso = dso__new(name); 1014 1015 if (dso) 1016 map = map__new2(0, dso); 1017 1018 return map; 1019 } 1020 1021 struct dso *machine__findnew_kernel(struct machine *machine, const char *name, 1022 const char *short_name, int dso_type) 1023 { 1024 /* 1025 * The kernel dso could be created by build_id processing. 1026 */ 1027 struct dso *dso = machine__findnew_dso(machine, name); 1028 1029 /* 1030 * We need to run this in all cases, since during the build_id 1031 * processing we had no idea this was the kernel dso. 1032 */ 1033 if (dso != NULL) { 1034 dso__set_short_name(dso, short_name, false); 1035 dso->kernel = dso_type; 1036 } 1037 1038 return dso; 1039 } 1040 1041 /* 1042 * Find a matching entry and/or link current entry to RB tree. 1043 * Either one of the dso or name parameter must be non-NULL or the 1044 * function will not work. 1045 */ 1046 static struct dso *__dso__findlink_by_longname(struct rb_root *root, 1047 struct dso *dso, const char *name) 1048 { 1049 struct rb_node **p = &root->rb_node; 1050 struct rb_node *parent = NULL; 1051 1052 if (!name) 1053 name = dso->long_name; 1054 /* 1055 * Find node with the matching name 1056 */ 1057 while (*p) { 1058 struct dso *this = rb_entry(*p, struct dso, rb_node); 1059 int rc = strcmp(name, this->long_name); 1060 1061 parent = *p; 1062 if (rc == 0) { 1063 /* 1064 * In case the new DSO is a duplicate of an existing 1065 * one, print a one-time warning & put the new entry 1066 * at the end of the list of duplicates. 1067 */ 1068 if (!dso || (dso == this)) 1069 return this; /* Find matching dso */ 1070 /* 1071 * The core kernel DSOs may have duplicated long name. 1072 * In this case, the short name should be different. 1073 * Comparing the short names to differentiate the DSOs. 1074 */ 1075 rc = strcmp(dso->short_name, this->short_name); 1076 if (rc == 0) { 1077 pr_err("Duplicated dso name: %s\n", name); 1078 return NULL; 1079 } 1080 } 1081 if (rc < 0) 1082 p = &parent->rb_left; 1083 else 1084 p = &parent->rb_right; 1085 } 1086 if (dso) { 1087 /* Add new node and rebalance tree */ 1088 rb_link_node(&dso->rb_node, parent, p); 1089 rb_insert_color(&dso->rb_node, root); 1090 dso->root = root; 1091 } 1092 return NULL; 1093 } 1094 1095 static inline struct dso *__dso__find_by_longname(struct rb_root *root, 1096 const char *name) 1097 { 1098 return __dso__findlink_by_longname(root, NULL, name); 1099 } 1100 1101 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated) 1102 { 1103 struct rb_root *root = dso->root; 1104 1105 if (name == NULL) 1106 return; 1107 1108 if (dso->long_name_allocated) 1109 free((char *)dso->long_name); 1110 1111 if (root) { 1112 rb_erase(&dso->rb_node, root); 1113 /* 1114 * __dso__findlink_by_longname() isn't guaranteed to add it 1115 * back, so a clean removal is required here. 1116 */ 1117 RB_CLEAR_NODE(&dso->rb_node); 1118 dso->root = NULL; 1119 } 1120 1121 dso->long_name = name; 1122 dso->long_name_len = strlen(name); 1123 dso->long_name_allocated = name_allocated; 1124 1125 if (root) 1126 __dso__findlink_by_longname(root, dso, NULL); 1127 } 1128 1129 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated) 1130 { 1131 if (name == NULL) 1132 return; 1133 1134 if (dso->short_name_allocated) 1135 free((char *)dso->short_name); 1136 1137 dso->short_name = name; 1138 dso->short_name_len = strlen(name); 1139 dso->short_name_allocated = name_allocated; 1140 } 1141 1142 static void dso__set_basename(struct dso *dso) 1143 { 1144 /* 1145 * basename() may modify path buffer, so we must pass 1146 * a copy. 1147 */ 1148 char *base, *lname = strdup(dso->long_name); 1149 1150 if (!lname) 1151 return; 1152 1153 /* 1154 * basename() may return a pointer to internal 1155 * storage which is reused in subsequent calls 1156 * so copy the result. 1157 */ 1158 base = strdup(basename(lname)); 1159 1160 free(lname); 1161 1162 if (!base) 1163 return; 1164 1165 dso__set_short_name(dso, base, true); 1166 } 1167 1168 int dso__name_len(const struct dso *dso) 1169 { 1170 if (!dso) 1171 return strlen("[unknown]"); 1172 if (verbose > 0) 1173 return dso->long_name_len; 1174 1175 return dso->short_name_len; 1176 } 1177 1178 bool dso__loaded(const struct dso *dso) 1179 { 1180 return dso->loaded; 1181 } 1182 1183 bool dso__sorted_by_name(const struct dso *dso) 1184 { 1185 return dso->sorted_by_name; 1186 } 1187 1188 void dso__set_sorted_by_name(struct dso *dso) 1189 { 1190 dso->sorted_by_name = true; 1191 } 1192 1193 struct dso *dso__new(const char *name) 1194 { 1195 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1); 1196 1197 if (dso != NULL) { 1198 strcpy(dso->name, name); 1199 dso__set_long_name(dso, dso->name, false); 1200 dso__set_short_name(dso, dso->name, false); 1201 dso->symbols = dso->symbol_names = RB_ROOT_CACHED; 1202 dso->data.cache = RB_ROOT; 1203 dso->inlined_nodes = RB_ROOT_CACHED; 1204 dso->srclines = RB_ROOT_CACHED; 1205 dso->data.fd = -1; 1206 dso->data.status = DSO_DATA_STATUS_UNKNOWN; 1207 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; 1208 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; 1209 dso->is_64_bit = (sizeof(void *) == 8); 1210 dso->loaded = 0; 1211 dso->rel = 0; 1212 dso->sorted_by_name = 0; 1213 dso->has_build_id = 0; 1214 dso->has_srcline = 1; 1215 dso->a2l_fails = 1; 1216 dso->kernel = DSO_TYPE_USER; 1217 dso->needs_swap = DSO_SWAP__UNSET; 1218 dso->comp = COMP_ID__NONE; 1219 RB_CLEAR_NODE(&dso->rb_node); 1220 dso->root = NULL; 1221 INIT_LIST_HEAD(&dso->node); 1222 INIT_LIST_HEAD(&dso->data.open_entry); 1223 pthread_mutex_init(&dso->lock, NULL); 1224 refcount_set(&dso->refcnt, 1); 1225 } 1226 1227 return dso; 1228 } 1229 1230 void dso__delete(struct dso *dso) 1231 { 1232 if (!RB_EMPTY_NODE(&dso->rb_node)) 1233 pr_err("DSO %s is still in rbtree when being deleted!\n", 1234 dso->long_name); 1235 1236 /* free inlines first, as they reference symbols */ 1237 inlines__tree_delete(&dso->inlined_nodes); 1238 srcline__tree_delete(&dso->srclines); 1239 symbols__delete(&dso->symbols); 1240 1241 if (dso->short_name_allocated) { 1242 zfree((char **)&dso->short_name); 1243 dso->short_name_allocated = false; 1244 } 1245 1246 if (dso->long_name_allocated) { 1247 zfree((char **)&dso->long_name); 1248 dso->long_name_allocated = false; 1249 } 1250 1251 dso__data_close(dso); 1252 auxtrace_cache__free(dso->auxtrace_cache); 1253 dso_cache__free(dso); 1254 dso__free_a2l(dso); 1255 zfree(&dso->symsrc_filename); 1256 nsinfo__zput(dso->nsinfo); 1257 pthread_mutex_destroy(&dso->lock); 1258 free(dso); 1259 } 1260 1261 struct dso *dso__get(struct dso *dso) 1262 { 1263 if (dso) 1264 refcount_inc(&dso->refcnt); 1265 return dso; 1266 } 1267 1268 void dso__put(struct dso *dso) 1269 { 1270 if (dso && refcount_dec_and_test(&dso->refcnt)) 1271 dso__delete(dso); 1272 } 1273 1274 void dso__set_build_id(struct dso *dso, void *build_id) 1275 { 1276 memcpy(dso->build_id, build_id, sizeof(dso->build_id)); 1277 dso->has_build_id = 1; 1278 } 1279 1280 bool dso__build_id_equal(const struct dso *dso, u8 *build_id) 1281 { 1282 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0; 1283 } 1284 1285 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) 1286 { 1287 char path[PATH_MAX]; 1288 1289 if (machine__is_default_guest(machine)) 1290 return; 1291 sprintf(path, "%s/sys/kernel/notes", machine->root_dir); 1292 if (sysfs__read_build_id(path, dso->build_id, 1293 sizeof(dso->build_id)) == 0) 1294 dso->has_build_id = true; 1295 } 1296 1297 int dso__kernel_module_get_build_id(struct dso *dso, 1298 const char *root_dir) 1299 { 1300 char filename[PATH_MAX]; 1301 /* 1302 * kernel module short names are of the form "[module]" and 1303 * we need just "module" here. 1304 */ 1305 const char *name = dso->short_name + 1; 1306 1307 snprintf(filename, sizeof(filename), 1308 "%s/sys/module/%.*s/notes/.note.gnu.build-id", 1309 root_dir, (int)strlen(name) - 1, name); 1310 1311 if (sysfs__read_build_id(filename, dso->build_id, 1312 sizeof(dso->build_id)) == 0) 1313 dso->has_build_id = true; 1314 1315 return 0; 1316 } 1317 1318 bool __dsos__read_build_ids(struct list_head *head, bool with_hits) 1319 { 1320 bool have_build_id = false; 1321 struct dso *pos; 1322 struct nscookie nsc; 1323 1324 list_for_each_entry(pos, head, node) { 1325 if (with_hits && !pos->hit && !dso__is_vdso(pos)) 1326 continue; 1327 if (pos->has_build_id) { 1328 have_build_id = true; 1329 continue; 1330 } 1331 nsinfo__mountns_enter(pos->nsinfo, &nsc); 1332 if (filename__read_build_id(pos->long_name, pos->build_id, 1333 sizeof(pos->build_id)) > 0) { 1334 have_build_id = true; 1335 pos->has_build_id = true; 1336 } 1337 nsinfo__mountns_exit(&nsc); 1338 } 1339 1340 return have_build_id; 1341 } 1342 1343 void __dsos__add(struct dsos *dsos, struct dso *dso) 1344 { 1345 list_add_tail(&dso->node, &dsos->head); 1346 __dso__findlink_by_longname(&dsos->root, dso, NULL); 1347 /* 1348 * It is now in the linked list, grab a reference, then garbage collect 1349 * this when needing memory, by looking at LRU dso instances in the 1350 * list with atomic_read(&dso->refcnt) == 1, i.e. no references 1351 * anywhere besides the one for the list, do, under a lock for the 1352 * list: remove it from the list, then a dso__put(), that probably will 1353 * be the last and will then call dso__delete(), end of life. 1354 * 1355 * That, or at the end of the 'struct machine' lifetime, when all 1356 * 'struct dso' instances will be removed from the list, in 1357 * dsos__exit(), if they have no other reference from some other data 1358 * structure. 1359 * 1360 * E.g.: after processing a 'perf.data' file and storing references 1361 * to objects instantiated while processing events, we will have 1362 * references to the 'thread', 'map', 'dso' structs all from 'struct 1363 * hist_entry' instances, but we may not need anything not referenced, 1364 * so we might as well call machines__exit()/machines__delete() and 1365 * garbage collect it. 1366 */ 1367 dso__get(dso); 1368 } 1369 1370 void dsos__add(struct dsos *dsos, struct dso *dso) 1371 { 1372 down_write(&dsos->lock); 1373 __dsos__add(dsos, dso); 1374 up_write(&dsos->lock); 1375 } 1376 1377 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short) 1378 { 1379 struct dso *pos; 1380 1381 if (cmp_short) { 1382 list_for_each_entry(pos, &dsos->head, node) 1383 if (strcmp(pos->short_name, name) == 0) 1384 return pos; 1385 return NULL; 1386 } 1387 return __dso__find_by_longname(&dsos->root, name); 1388 } 1389 1390 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) 1391 { 1392 struct dso *dso; 1393 down_read(&dsos->lock); 1394 dso = __dsos__find(dsos, name, cmp_short); 1395 up_read(&dsos->lock); 1396 return dso; 1397 } 1398 1399 struct dso *__dsos__addnew(struct dsos *dsos, const char *name) 1400 { 1401 struct dso *dso = dso__new(name); 1402 1403 if (dso != NULL) { 1404 __dsos__add(dsos, dso); 1405 dso__set_basename(dso); 1406 /* Put dso here because __dsos_add already got it */ 1407 dso__put(dso); 1408 } 1409 return dso; 1410 } 1411 1412 struct dso *__dsos__findnew(struct dsos *dsos, const char *name) 1413 { 1414 struct dso *dso = __dsos__find(dsos, name, false); 1415 1416 return dso ? dso : __dsos__addnew(dsos, name); 1417 } 1418 1419 struct dso *dsos__findnew(struct dsos *dsos, const char *name) 1420 { 1421 struct dso *dso; 1422 down_write(&dsos->lock); 1423 dso = dso__get(__dsos__findnew(dsos, name)); 1424 up_write(&dsos->lock); 1425 return dso; 1426 } 1427 1428 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, 1429 bool (skip)(struct dso *dso, int parm), int parm) 1430 { 1431 struct dso *pos; 1432 size_t ret = 0; 1433 1434 list_for_each_entry(pos, head, node) { 1435 if (skip && skip(pos, parm)) 1436 continue; 1437 ret += dso__fprintf_buildid(pos, fp); 1438 ret += fprintf(fp, " %s\n", pos->long_name); 1439 } 1440 return ret; 1441 } 1442 1443 size_t __dsos__fprintf(struct list_head *head, FILE *fp) 1444 { 1445 struct dso *pos; 1446 size_t ret = 0; 1447 1448 list_for_each_entry(pos, head, node) { 1449 ret += dso__fprintf(pos, fp); 1450 } 1451 1452 return ret; 1453 } 1454 1455 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) 1456 { 1457 char sbuild_id[SBUILD_ID_SIZE]; 1458 1459 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); 1460 return fprintf(fp, "%s", sbuild_id); 1461 } 1462 1463 size_t dso__fprintf(struct dso *dso, FILE *fp) 1464 { 1465 struct rb_node *nd; 1466 size_t ret = fprintf(fp, "dso: %s (", dso->short_name); 1467 1468 if (dso->short_name != dso->long_name) 1469 ret += fprintf(fp, "%s, ", dso->long_name); 1470 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT "); 1471 ret += dso__fprintf_buildid(dso, fp); 1472 ret += fprintf(fp, ")\n"); 1473 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) { 1474 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 1475 ret += symbol__fprintf(pos, fp); 1476 } 1477 1478 return ret; 1479 } 1480 1481 enum dso_type dso__type(struct dso *dso, struct machine *machine) 1482 { 1483 int fd; 1484 enum dso_type type = DSO__TYPE_UNKNOWN; 1485 1486 fd = dso__data_get_fd(dso, machine); 1487 if (fd >= 0) { 1488 type = dso__type_fd(fd); 1489 dso__data_put_fd(dso); 1490 } 1491 1492 return type; 1493 } 1494 1495 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen) 1496 { 1497 int idx, errnum = dso->load_errno; 1498 /* 1499 * This must have a same ordering as the enum dso_load_errno. 1500 */ 1501 static const char *dso_load__error_str[] = { 1502 "Internal tools/perf/ library error", 1503 "Invalid ELF file", 1504 "Can not read build id", 1505 "Mismatching build id", 1506 "Decompression failure", 1507 }; 1508 1509 BUG_ON(buflen == 0); 1510 1511 if (errnum >= 0) { 1512 const char *err = str_error_r(errnum, buf, buflen); 1513 1514 if (err != buf) 1515 scnprintf(buf, buflen, "%s", err); 1516 1517 return 0; 1518 } 1519 1520 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END) 1521 return -1; 1522 1523 idx = errnum - __DSO_LOAD_ERRNO__START; 1524 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]); 1525 return 0; 1526 } 1527