1 // SPDX-License-Identifier: GPL-2.0 2 #include "dso.h" 3 4 #include <errno.h> 5 #include <stdlib.h> 6 7 #include <asm/bug.h> 8 #include <fcntl.h> 9 #include <linux/kernel.h> 10 #include <linux/string.h> 11 #include <linux/zalloc.h> 12 #include <sys/resource.h> 13 #include <sys/stat.h> 14 #include <sys/time.h> 15 #include <sys/types.h> 16 #include <unistd.h> 17 18 #include "annotate-data.h" 19 #include "auxtrace.h" 20 #include "compress.h" 21 #include "debug.h" 22 #include "dsos.h" 23 #include "env.h" 24 #include "libbfd.h" 25 #include "libdw.h" 26 #include "machine.h" 27 #include "map.h" 28 #include "namespaces.h" 29 #include "path.h" 30 #include "srcline.h" 31 #include "string2.h" 32 #include "symbol.h" 33 #include "util.h" /* O_CLOEXEC for older systems */ 34 #include "vdso.h" 35 36 #ifdef HAVE_LIBBPF_SUPPORT 37 #include <bpf/libbpf.h> 38 39 #include "bpf-event.h" 40 #include "bpf-utils.h" 41 #endif 42 43 static const char * const debuglink_paths[] = { 44 "%.0s%s", 45 "%s/%s", 46 "%s/.debug/%s", 47 "/usr/lib/debug%s/%s" 48 }; 49 50 void dso__set_nsinfo(struct dso *dso, struct nsinfo *nsi) 51 { 52 nsinfo__put(RC_CHK_ACCESS(dso)->nsinfo); 53 RC_CHK_ACCESS(dso)->nsinfo = nsi; 54 } 55 56 char dso__symtab_origin(const struct dso *dso) 57 { 58 static const char origin[] = { 59 [DSO_BINARY_TYPE__KALLSYMS] = 'k', 60 [DSO_BINARY_TYPE__VMLINUX] = 'v', 61 [DSO_BINARY_TYPE__JAVA_JIT] = 'j', 62 [DSO_BINARY_TYPE__DEBUGLINK] = 'l', 63 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B', 64 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D', 65 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f', 66 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u', 67 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x', 68 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o', 69 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b', 70 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd', 71 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K', 72 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm', 73 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g', 74 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G', 75 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M', 76 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V', 77 [DSO_BINARY_TYPE__GNU_DEBUGDATA] = 'n', 78 }; 79 80 if (dso == NULL || dso__symtab_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) 81 return '!'; 82 return origin[dso__symtab_type(dso)]; 83 } 84 85 bool dso__is_object_file(const struct dso *dso) 86 { 87 switch (dso__binary_type(dso)) { 88 case DSO_BINARY_TYPE__KALLSYMS: 89 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 90 case DSO_BINARY_TYPE__JAVA_JIT: 91 case DSO_BINARY_TYPE__BPF_PROG_INFO: 92 case DSO_BINARY_TYPE__BPF_IMAGE: 93 case DSO_BINARY_TYPE__OOL: 94 return false; 95 case DSO_BINARY_TYPE__VMLINUX: 96 case DSO_BINARY_TYPE__GUEST_VMLINUX: 97 case DSO_BINARY_TYPE__DEBUGLINK: 98 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 99 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 100 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 101 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 102 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 103 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 104 case DSO_BINARY_TYPE__GNU_DEBUGDATA: 105 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 106 case DSO_BINARY_TYPE__GUEST_KMODULE: 107 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 108 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 109 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 110 case DSO_BINARY_TYPE__KCORE: 111 case DSO_BINARY_TYPE__GUEST_KCORE: 112 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 113 case DSO_BINARY_TYPE__NOT_FOUND: 114 default: 115 return true; 116 } 117 } 118 119 int dso__read_binary_type_filename(const struct dso *dso, 120 enum dso_binary_type type, 121 const char *root_dir, char *filename, size_t size) 122 { 123 char build_id_hex[SBUILD_ID_SIZE]; 124 char relative[PATH_MAX]; 125 int ret = 0; 126 size_t len; 127 128 switch (type) { 129 case DSO_BINARY_TYPE__DEBUGLINK: 130 { 131 const char *last_slash; 132 char dso_dir[PATH_MAX]; 133 char symfile[PATH_MAX]; 134 unsigned int i; 135 136 len = __symbol__join_symfs(filename, size, dso__long_name(dso)); 137 last_slash = filename + len; 138 while (last_slash != filename && *last_slash != '/') 139 last_slash--; 140 141 strncpy(dso_dir, filename, last_slash - filename); 142 dso_dir[last_slash-filename] = '\0'; 143 144 if (!is_regular_file(filename)) { 145 ret = -1; 146 break; 147 } 148 149 ret = filename__read_debuglink(filename, symfile, PATH_MAX); 150 if (ret) 151 break; 152 153 /* Check predefined locations where debug file might reside */ 154 ret = -1; 155 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) { 156 snprintf(filename, size, 157 debuglink_paths[i], dso_dir, symfile); 158 if (is_regular_file(filename)) { 159 ret = 0; 160 break; 161 } 162 } 163 164 break; 165 } 166 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 167 if (dso__build_id_filename(dso, filename, size, false) == NULL) 168 ret = -1; 169 break; 170 171 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 172 if (dso__build_id_filename(dso, filename, size, true) == NULL) 173 ret = -1; 174 break; 175 176 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 177 snprintf(relative, sizeof(relative), "/usr/lib/debug%s.debug", 178 dso__long_name(dso)); 179 __symbol__join_symfs(filename, size, relative); 180 break; 181 182 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 183 snprintf(relative, sizeof(relative), "/usr/lib/debug%s", 184 dso__long_name(dso)); 185 __symbol__join_symfs(filename, size, relative); 186 break; 187 188 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 189 /* 190 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in 191 * /usr/lib/debug/lib when it is expected to be in 192 * /usr/lib/debug/usr/lib 193 */ 194 if (strlen(dso__long_name(dso)) < 9 || 195 strncmp(dso__long_name(dso), "/usr/lib/", 9)) { 196 ret = -1; 197 break; 198 } 199 snprintf(relative, sizeof(relative), "/usr/lib/debug%s", 200 dso__long_name(dso) + 4); 201 __symbol__join_symfs(filename, size, relative); 202 break; 203 204 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 205 { 206 const char *last_slash; 207 size_t dir_size; 208 209 last_slash = dso__long_name(dso) + dso__long_name_len(dso); 210 while (last_slash != dso__long_name(dso) && *last_slash != '/') 211 last_slash--; 212 213 dir_size = last_slash - dso__long_name(dso) + 2; 214 if (dir_size > sizeof(relative)) { 215 ret = -1; 216 break; 217 } 218 len = scnprintf(relative, dir_size, "%s", dso__long_name(dso)); 219 scnprintf(relative + len, sizeof(relative) - len, ".debug%s", 220 last_slash); 221 __symbol__join_symfs(filename, size, relative); 222 break; 223 } 224 225 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 226 if (!dso__has_build_id(dso)) { 227 ret = -1; 228 break; 229 } 230 231 build_id__snprintf(dso__bid(dso), build_id_hex, sizeof(build_id_hex)); 232 /* 233 * The build id cache layout splits the first two characters off 234 * into a directory name, so the basename of the hierarchy path 235 * is only part of the build id. Name the flat file after the 236 * whole build id instead. 237 */ 238 if (symbol_conf.symfs_layout_flat) 239 snprintf(relative, sizeof(relative), "/%s.debug", 240 build_id_hex); 241 else 242 snprintf(relative, sizeof(relative), 243 "/usr/lib/debug/.build-id/%.2s/%s.debug", 244 build_id_hex, build_id_hex + 2); 245 __symbol__join_symfs(filename, size, relative); 246 break; 247 248 case DSO_BINARY_TYPE__VMLINUX: 249 case DSO_BINARY_TYPE__GUEST_VMLINUX: 250 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 251 case DSO_BINARY_TYPE__GNU_DEBUGDATA: 252 __symbol__join_symfs(filename, size, dso__long_name(dso)); 253 break; 254 255 case DSO_BINARY_TYPE__GUEST_KMODULE: 256 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 257 path__join3(filename, size, symbol_conf.symfs, 258 root_dir, dso__long_name(dso)); 259 break; 260 261 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 262 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 263 __symbol__join_symfs(filename, size, dso__long_name(dso)); 264 break; 265 266 case DSO_BINARY_TYPE__KCORE: 267 case DSO_BINARY_TYPE__GUEST_KCORE: 268 snprintf(filename, size, "%s", dso__long_name(dso)); 269 break; 270 271 default: 272 case DSO_BINARY_TYPE__KALLSYMS: 273 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 274 case DSO_BINARY_TYPE__JAVA_JIT: 275 case DSO_BINARY_TYPE__BPF_PROG_INFO: 276 case DSO_BINARY_TYPE__BPF_IMAGE: 277 case DSO_BINARY_TYPE__OOL: 278 case DSO_BINARY_TYPE__NOT_FOUND: 279 ret = -1; 280 break; 281 } 282 283 return ret; 284 } 285 286 enum { 287 COMP_ID__NONE = 0, 288 }; 289 290 static const struct { 291 const char *fmt; 292 int (*decompress)(const char *input, int output); 293 bool (*is_compressed)(const char *input); 294 } compressions[] = { 295 [COMP_ID__NONE] = { .fmt = NULL, }, 296 #ifdef HAVE_ZLIB_SUPPORT 297 { "gz", gzip_decompress_to_file, gzip_is_compressed }, 298 #endif 299 #ifdef HAVE_LZMA_SUPPORT 300 { "xz", lzma_decompress_to_file, lzma_is_compressed }, 301 #endif 302 { NULL, NULL, NULL }, 303 }; 304 305 static int is_supported_compression(const char *ext) 306 { 307 unsigned i; 308 309 for (i = 1; compressions[i].fmt; i++) { 310 if (!strcmp(ext, compressions[i].fmt)) 311 return i; 312 } 313 return COMP_ID__NONE; 314 } 315 316 bool is_kernel_module(const char *pathname, int cpumode) 317 { 318 struct kmod_path m; 319 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK; 320 321 WARN_ONCE(mode != cpumode, 322 "Internal error: passing unmasked cpumode (%x) to is_kernel_module", 323 cpumode); 324 325 switch (mode) { 326 case PERF_RECORD_MISC_USER: 327 case PERF_RECORD_MISC_HYPERVISOR: 328 case PERF_RECORD_MISC_GUEST_USER: 329 return false; 330 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */ 331 default: 332 if (kmod_path__parse(&m, pathname)) { 333 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.", 334 pathname); 335 return true; 336 } 337 } 338 339 return m.kmod; 340 } 341 342 bool dso__needs_decompress(struct dso *dso) 343 { 344 return dso__symtab_type(dso) == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || 345 dso__symtab_type(dso) == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; 346 } 347 348 int filename__decompress(const char *name, char *pathname, 349 size_t len, int comp, int *err) 350 { 351 char tmpbuf[] = KMOD_DECOMP_NAME; 352 int fd = -1; 353 354 /* 355 * We have proper compression id for DSO and yet the file 356 * behind the 'name' can still be plain uncompressed object. 357 * 358 * The reason is behind the logic we open the DSO object files, 359 * when we try all possible 'debug' objects until we find the 360 * data. So even if the DSO is represented by 'krava.xz' module, 361 * we can end up here opening ~/.debug/....23432432/debug' file 362 * which is not compressed. 363 * 364 * To keep this transparent, we detect this and return the file 365 * descriptor to the uncompressed file. 366 */ 367 if (!compressions[comp].is_compressed(name)) { 368 fd = open(name, O_RDONLY | O_CLOEXEC); 369 if (fd < 0) 370 *err = errno; 371 if (pathname && len > 0) 372 pathname[0] = '\0'; 373 return fd; 374 } 375 376 fd = mkostemp(tmpbuf, O_CLOEXEC); 377 if (fd < 0) { 378 *err = errno; 379 return -1; 380 } 381 382 if (compressions[comp].decompress(name, fd)) { 383 *err = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE; 384 close(fd); 385 fd = -1; 386 } 387 388 if (!pathname || (fd < 0)) 389 unlink(tmpbuf); 390 391 if (pathname && (fd >= 0)) 392 strlcpy(pathname, tmpbuf, len); 393 394 return fd; 395 } 396 397 static int decompress_kmodule(struct dso *dso, const char *name, 398 char *pathname, size_t len) 399 { 400 if (!dso__needs_decompress(dso)) 401 return -1; 402 403 if (dso__comp(dso) == COMP_ID__NONE) 404 return -1; 405 406 return filename__decompress(name, pathname, len, dso__comp(dso), dso__load_errno(dso)); 407 } 408 409 int dso__decompress_kmodule_fd(struct dso *dso, const char *name) 410 { 411 return decompress_kmodule(dso, name, NULL, 0); 412 } 413 414 int dso__decompress_kmodule_path(struct dso *dso, const char *name, 415 char *pathname, size_t len) 416 { 417 int fd = decompress_kmodule(dso, name, pathname, len); 418 419 /* decompress_kmodule() returns -1 on failure, don't close(-1) */ 420 if (fd >= 0) 421 close(fd); 422 return fd >= 0 ? 0 : -1; 423 } 424 425 /* 426 * Parses kernel module specified in @path and updates 427 * @m argument like: 428 * 429 * @comp - true if @path contains supported compression suffix, 430 * false otherwise 431 * @kmod - true if @path contains '.ko' suffix in right position, 432 * false otherwise 433 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name 434 * of the kernel module without suffixes, otherwise strudup-ed 435 * base name of @path 436 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string 437 * the compression suffix 438 * 439 * Returns 0 if there's no strdup error, -ENOMEM otherwise. 440 */ 441 int __kmod_path__parse(struct kmod_path *m, const char *path, 442 bool alloc_name) 443 { 444 const char *name = strrchr(path, '/'); 445 const char *ext = strrchr(path, '.'); 446 bool is_simple_name = false; 447 448 memset(m, 0x0, sizeof(*m)); 449 name = name ? name + 1 : path; 450 451 /* 452 * '.' is also a valid character for module name. For example: 453 * [aaa.bbb] is a valid module name. '[' should have higher 454 * priority than '.ko' suffix. 455 * 456 * The kernel names are from machine__mmap_name. Such 457 * name should belong to kernel itself, not kernel module. 458 */ 459 if (name[0] == '[') { 460 is_simple_name = true; 461 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) || 462 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) || 463 (strncmp(name, "[vdso]", 6) == 0) || 464 (strncmp(name, "[vdso32]", 8) == 0) || 465 (strncmp(name, "[vdsox32]", 9) == 0) || 466 (strncmp(name, "[vsyscall]", 10) == 0)) { 467 m->kmod = false; 468 469 } else 470 m->kmod = true; 471 } 472 473 /* No extension, just return name. */ 474 if ((ext == NULL) || is_simple_name) { 475 if (alloc_name) { 476 m->name = strdup(name); 477 return m->name ? 0 : -ENOMEM; 478 } 479 return 0; 480 } 481 482 m->comp = is_supported_compression(ext + 1); 483 if (m->comp > COMP_ID__NONE) 484 ext -= 3; 485 486 /* Check .ko extension only if there's enough name left. */ 487 if (ext > name) 488 m->kmod = !strncmp(ext, ".ko", 3); 489 490 if (alloc_name) { 491 if (m->kmod) { 492 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1) 493 return -ENOMEM; 494 } else { 495 if (asprintf(&m->name, "%s", name) == -1) 496 return -ENOMEM; 497 } 498 499 strreplace(m->name, '-', '_'); 500 } 501 502 return 0; 503 } 504 505 void dso__set_module_info(struct dso *dso, struct kmod_path *m, 506 struct machine *machine) 507 { 508 if (machine__is_host(machine)) 509 dso__set_symtab_type(dso, DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE); 510 else 511 dso__set_symtab_type(dso, DSO_BINARY_TYPE__GUEST_KMODULE); 512 513 /* _KMODULE_COMP should be next to _KMODULE */ 514 if (m->kmod && m->comp) { 515 dso__set_symtab_type(dso, dso__symtab_type(dso) + 1); 516 dso__set_comp(dso, m->comp); 517 } 518 519 dso__set_is_kmod(dso); 520 dso__set_short_name(dso, strdup(m->name), true); 521 } 522 523 /* 524 * Global list of open DSOs and the counter. 525 */ 526 struct mutex _dso__data_open_lock; 527 static LIST_HEAD(dso__data_open); 528 static long dso__data_open_cnt GUARDED_BY(_dso__data_open_lock); 529 530 static void dso__data_open_lock_init(void) 531 { 532 mutex_init(&_dso__data_open_lock); 533 } 534 535 static struct mutex *dso__data_open_lock(void) LOCK_RETURNED(_dso__data_open_lock) 536 { 537 static pthread_once_t data_open_lock_once = PTHREAD_ONCE_INIT; 538 539 pthread_once(&data_open_lock_once, dso__data_open_lock_init); 540 541 return &_dso__data_open_lock; 542 } 543 544 static void dso__list_add(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 545 { 546 list_add_tail(&dso__data(dso)->open_entry, &dso__data_open); 547 #ifdef REFCNT_CHECKING 548 dso__data(dso)->dso = dso__get(dso); 549 #endif 550 /* Assume the dso is part of dsos, hence the optional reference count above. */ 551 assert(dso__dsos(dso)); 552 dso__data_open_cnt++; 553 } 554 555 static void dso__list_del(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 556 { 557 list_del_init(&dso__data(dso)->open_entry); 558 #ifdef REFCNT_CHECKING 559 mutex_unlock(dso__data_open_lock()); 560 dso__put(dso__data(dso)->dso); 561 mutex_lock(dso__data_open_lock()); 562 #endif 563 WARN_ONCE(dso__data_open_cnt <= 0, 564 "DSO data fd counter out of bounds."); 565 dso__data_open_cnt--; 566 } 567 568 static void close_first_dso(void); 569 570 static int do_open(char *name) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 571 { 572 do { 573 int fd = open(name, O_RDONLY|O_CLOEXEC); 574 575 if (fd >= 0) 576 return fd; 577 578 pr_debug("dso open failed: %m\n"); 579 if (!dso__data_open_cnt || errno != EMFILE) 580 break; 581 582 close_first_dso(); 583 } while (1); 584 585 return -1; 586 } 587 588 char *dso__filename_with_chroot(const struct dso *dso, const char *filename) 589 { 590 return filename_with_chroot(nsinfo__pid(dso__nsinfo_const(dso)), filename); 591 } 592 593 char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp, 594 enum dso_binary_type type) 595 { 596 char *name = malloc(PATH_MAX); 597 598 *decomp = false; 599 600 if (name == NULL) 601 return NULL; 602 603 if (dso__read_binary_type_filename(dso, type, root_dir, name, PATH_MAX)) 604 goto out; 605 606 if (!is_regular_file(name)) { 607 struct stat st; 608 char *new_name; 609 610 /* 611 * errno only reflects the failure reason when stat() itself 612 * failed: a successful stat() on a non-regular file (e.g. a 613 * directory) leaves a stale errno, which a previous failed 614 * iteration of the try_to_open_dso() fallback loop may have 615 * set to ENOENT. 616 */ 617 if (stat(name, &st) == 0 || errno != ENOENT || 618 dso__nsinfo(dso) == NULL) 619 goto out; 620 621 new_name = dso__filename_with_chroot(dso, name); 622 if (!new_name) 623 goto out; 624 625 free(name); 626 name = new_name; 627 } 628 629 if (dso__needs_decompress(dso)) { 630 char newpath[KMOD_DECOMP_LEN]; 631 size_t len = sizeof(newpath); 632 633 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) { 634 /* 635 * Use a standard errno value, not the negative custom 636 * DSO_LOAD_ERRNO stored in dso__load_errno(dso): 637 * __open_dso() computes fd = -errno, so a negative 638 * errno produces a positive fd that looks valid. 639 */ 640 errno = EIO; 641 goto out; 642 } 643 644 /* empty pathname means file wasn't actually compressed */ 645 if (newpath[0] != '\0') { 646 char *tmp = strdup(newpath); 647 648 if (!tmp) { 649 unlink(newpath); 650 goto out; 651 } 652 free(name); 653 name = tmp; 654 *decomp = true; 655 } 656 } 657 return name; 658 659 out: 660 free(name); 661 return NULL; 662 } 663 664 void dso__put_filename(struct dso *dso __maybe_unused, char *filename, bool decomp) 665 { 666 if (decomp) 667 unlink(filename); 668 669 free(filename); 670 } 671 672 static int __open_dso(struct dso *dso, struct machine *machine) 673 EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 674 { 675 int fd = -EINVAL; 676 char *name; 677 bool decomp = false; 678 679 mutex_lock(dso__lock(dso)); 680 681 name = dso__get_filename(dso, machine ? machine->root_dir : "", &decomp, 682 dso__binary_type(dso)); 683 if (name) { 684 fd = do_open(name); 685 } else { 686 if (errno == 0) 687 errno = ENOENT; 688 fd = -errno; 689 } 690 691 dso__put_filename(dso, name, decomp); 692 mutex_unlock(dso__lock(dso)); 693 return fd; 694 } 695 696 static void check_data_close(void); 697 698 /** 699 * dso_close - Open DSO data file 700 * @dso: dso object 701 * 702 * Open @dso's data file descriptor and updates 703 * list/count of open DSO objects. 704 */ 705 static int open_dso(struct dso *dso, struct machine *machine) 706 EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 707 { 708 int fd; 709 struct nscookie nsc; 710 711 if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE) { 712 mutex_lock(dso__lock(dso)); 713 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 714 mutex_unlock(dso__lock(dso)); 715 } 716 fd = __open_dso(dso, machine); 717 if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE) 718 nsinfo__mountns_exit(&nsc); 719 720 if (fd >= 0) { 721 dso__list_add(dso); 722 /* 723 * Check if we crossed the allowed number 724 * of opened DSOs and close one if needed. 725 */ 726 check_data_close(); 727 } 728 729 return fd; 730 } 731 732 static void close_data_fd(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 733 { 734 if (dso__data(dso)->fd >= 0) { 735 close(dso__data(dso)->fd); 736 dso__data(dso)->fd = -1; 737 dso__data(dso)->file_size = 0; 738 dso__list_del(dso); 739 } 740 } 741 742 /** 743 * dso_close - Close DSO data file 744 * @dso: dso object 745 * 746 * Close @dso's data file descriptor and updates 747 * list/count of open DSO objects. 748 */ 749 static void close_dso(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 750 { 751 close_data_fd(dso); 752 } 753 754 static void close_first_dso(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 755 { 756 struct dso_data *dso_data; 757 struct dso *dso; 758 759 dso_data = list_first_entry(&dso__data_open, struct dso_data, open_entry); 760 #ifdef REFCNT_CHECKING 761 dso = dso_data->dso; 762 #else 763 dso = container_of(dso_data, struct dso, data); 764 #endif 765 close_dso(dso); 766 } 767 768 static rlim_t get_fd_limit(void) 769 { 770 struct rlimit l; 771 rlim_t limit = 0; 772 773 /* Allow half of the current open fd limit. */ 774 if (getrlimit(RLIMIT_NOFILE, &l) == 0) { 775 if (l.rlim_cur == RLIM_INFINITY) 776 limit = l.rlim_cur; 777 else 778 limit = l.rlim_cur / 2; 779 } else { 780 pr_err("failed to get fd limit\n"); 781 limit = 1; 782 } 783 784 return limit; 785 } 786 787 static rlim_t fd_limit; 788 789 /* 790 * Used only by tests/dso-data.c to reset the environment 791 * for tests. I dont expect we should change this during 792 * standard runtime. 793 */ 794 void reset_fd_limit(void) 795 { 796 fd_limit = 0; 797 } 798 799 static bool may_cache_fd(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 800 { 801 if (!fd_limit) 802 fd_limit = get_fd_limit(); 803 804 if (fd_limit == RLIM_INFINITY) 805 return true; 806 807 return fd_limit > (rlim_t) dso__data_open_cnt; 808 } 809 810 /* 811 * Check and close LRU dso if we crossed allowed limit 812 * for opened dso file descriptors. The limit is half 813 * of the RLIMIT_NOFILE files opened. 814 */ 815 static void check_data_close(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 816 { 817 bool cache_fd = may_cache_fd(); 818 819 if (!cache_fd) 820 close_first_dso(); 821 } 822 823 /** 824 * dso__data_close - Close DSO data file 825 * @dso: dso object 826 * 827 * External interface to close @dso's data file descriptor. 828 */ 829 void dso__data_close(struct dso *dso) 830 { 831 mutex_lock(dso__data_open_lock()); 832 close_dso(dso); 833 mutex_unlock(dso__data_open_lock()); 834 } 835 836 static int dso__find_binary_type_locked(struct dso *dso) 837 EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)) 838 { 839 enum dso_binary_type binary_type_data[] = { 840 DSO_BINARY_TYPE__BUILD_ID_CACHE, 841 DSO_BINARY_TYPE__SYSTEM_PATH_DSO, 842 dso__symtab_type(dso), 843 DSO_BINARY_TYPE__NOT_FOUND, 844 }; 845 int i = 0; 846 char *path; 847 bool found = false; 848 bool decomp = false; 849 int saved_errno; 850 851 if (dso__bintype_searched(dso)) 852 return dso__binary_type(dso); 853 dso__set_bintype_searched(dso); 854 855 switch (dso__binary_type(dso)) { 856 case DSO_BINARY_TYPE__KALLSYMS: 857 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 858 case DSO_BINARY_TYPE__VMLINUX: 859 case DSO_BINARY_TYPE__GUEST_VMLINUX: 860 case DSO_BINARY_TYPE__KCORE: 861 case DSO_BINARY_TYPE__GUEST_KCORE: 862 /* Nothing to do with kernel images */ 863 WARN_ON(!dso__kernel(dso)); 864 return dso__binary_type(dso); 865 case DSO_BINARY_TYPE__BPF_PROG_INFO: 866 case DSO_BINARY_TYPE__BPF_IMAGE: 867 case DSO_BINARY_TYPE__OOL: 868 case DSO_BINARY_TYPE__JAVA_JIT: 869 /* Same for the special DSOs */ 870 return dso__binary_type(dso); 871 case DSO_BINARY_TYPE__GUEST_KMODULE: 872 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 873 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 874 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 875 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 876 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 877 /* We don't expect these are set; fall through */ 878 case DSO_BINARY_TYPE__NOT_FOUND: 879 /* Let's find it out (for user DSOs or kernel modules) */ 880 break; 881 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 882 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 883 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 884 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 885 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 886 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 887 case DSO_BINARY_TYPE__GNU_DEBUGDATA: 888 case DSO_BINARY_TYPE__DEBUGLINK: 889 default: 890 /* Unexpected binary types (usually debug only) */ 891 break; 892 } 893 894 do { 895 dso__set_binary_type(dso, binary_type_data[i++]); 896 897 errno = 0; 898 path = dso__get_filename(dso, "", &decomp, dso__binary_type(dso)); 899 if (path == NULL) { 900 if (errno == EMFILE) 901 goto open_err; 902 continue; 903 } 904 905 errno = 0; 906 found = filename__has_section(path, ".text"); 907 if (!found) 908 saved_errno = errno; 909 dso__put_filename(dso, path, decomp); 910 911 if (found) 912 return dso__binary_type(dso); 913 else if (saved_errno == EMFILE) 914 goto open_err; 915 916 } while (dso__binary_type(dso) != DSO_BINARY_TYPE__NOT_FOUND); 917 918 return dso__binary_type(dso); 919 920 open_err: 921 /* Too many open files. Allow retry after closing one. */ 922 RC_CHK_ACCESS(dso)->bintype_searched = 0; 923 dso__set_binary_type(dso, DSO_BINARY_TYPE__NOT_FOUND); 924 return -1; 925 } 926 927 #define FIND_MAX_RETRY 3 928 static int __dso__find_binary_type(struct dso *dso) 929 EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 930 { 931 int ret; 932 int retry = 0; 933 934 again: 935 mutex_lock(dso__lock(dso)); 936 ret = dso__find_binary_type_locked(dso); 937 mutex_unlock(dso__lock(dso)); 938 939 if (ret == -1 && retry++ < FIND_MAX_RETRY) { 940 if (dso__data_open_cnt) { 941 close_first_dso(); 942 goto again; 943 } 944 } 945 946 if (ret == -1) { 947 mutex_lock(dso__lock(dso)); 948 ret = dso__binary_type(dso); 949 dso__set_bintype_searched(dso); 950 mutex_unlock(dso__lock(dso)); 951 } 952 953 return ret; 954 } 955 #undef FIND_MAX_RETRY 956 957 enum dso_binary_type dso__find_binary_type(struct dso *dso) 958 { 959 int ret; 960 961 mutex_lock(dso__data_open_lock()); 962 ret = __dso__find_binary_type(dso); 963 mutex_unlock(dso__data_open_lock()); 964 965 return ret; 966 } 967 968 static void try_to_open_dso(struct dso *dso, struct machine *machine) 969 EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) 970 { 971 struct dso_data *dso_data = dso__data(dso); 972 973 if (dso_data->fd >= 0) 974 return; 975 976 __dso__find_binary_type(dso); 977 dso_data->fd = open_dso(dso, machine); 978 979 if (dso_data->fd >= 0) 980 dso_data->status = DSO_DATA_STATUS_OK; 981 else 982 dso_data->status = DSO_DATA_STATUS_ERROR; 983 } 984 985 /** 986 * dso__data_get_fd - Get dso's data file descriptor 987 * @dso: dso object 988 * @machine: machine object 989 * 990 * External interface to find dso's file, open it and 991 * returns file descriptor. It should be paired with 992 * dso__data_put_fd() if it returns non-negative value. 993 */ 994 bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd) 995 { 996 *fd = -1; 997 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR) 998 return false; 999 1000 mutex_lock(dso__data_open_lock()); 1001 1002 try_to_open_dso(dso, machine); 1003 1004 *fd = dso__data(dso)->fd; 1005 if (*fd >= 0) 1006 return true; 1007 1008 mutex_unlock(dso__data_open_lock()); 1009 return false; 1010 } 1011 1012 void dso__data_put_fd(struct dso *dso __maybe_unused) 1013 { 1014 mutex_unlock(dso__data_open_lock()); 1015 } 1016 1017 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) 1018 { 1019 u32 flag = 1 << by; 1020 1021 if (dso__data(dso)->status_seen & flag) 1022 return true; 1023 1024 dso__data(dso)->status_seen |= flag; 1025 1026 return false; 1027 } 1028 1029 #ifdef HAVE_LIBBPF_SUPPORT 1030 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data) 1031 { 1032 struct bpf_prog_info_node *node; 1033 ssize_t size = DSO__DATA_CACHE_SIZE; 1034 struct dso_bpf_prog *dso_bpf_prog = dso__bpf_prog(dso); 1035 u64 len; 1036 u8 *buf; 1037 1038 node = perf_env__find_bpf_prog_info(dso_bpf_prog->env, dso_bpf_prog->id); 1039 if (!node || !node->info_linear) { 1040 dso__data(dso)->status = DSO_DATA_STATUS_ERROR; 1041 return -1; 1042 } 1043 1044 /* jited_prog_insns is only valid if bpil_offs_to_addr() converted it */ 1045 if (!(node->info_linear->arrays & (1UL << PERF_BPIL_JITED_INSNS))) { 1046 dso__data(dso)->status = DSO_DATA_STATUS_ERROR; 1047 return -1; 1048 } 1049 1050 len = node->info_linear->info.jited_prog_len; 1051 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns; 1052 1053 if (offset >= len) 1054 return -1; 1055 1056 size = (ssize_t)min(len - offset, (u64)size); 1057 memcpy(data, buf + offset, size); 1058 return size; 1059 } 1060 1061 static int bpf_size(struct dso *dso) 1062 { 1063 struct bpf_prog_info_node *node; 1064 struct dso_bpf_prog *dso_bpf_prog = dso__bpf_prog(dso); 1065 1066 node = perf_env__find_bpf_prog_info(dso_bpf_prog->env, dso_bpf_prog->id); 1067 if (!node || !node->info_linear) { 1068 dso__data(dso)->status = DSO_DATA_STATUS_ERROR; 1069 return -1; 1070 } 1071 1072 dso__data(dso)->file_size = node->info_linear->info.jited_prog_len; 1073 return 0; 1074 } 1075 #endif // HAVE_LIBBPF_SUPPORT 1076 1077 static void 1078 dso_cache__free(struct dso *dso) 1079 { 1080 struct rb_root *root = &dso__data(dso)->cache; 1081 struct rb_node *next = rb_first(root); 1082 1083 mutex_lock(dso__lock(dso)); 1084 while (next) { 1085 struct dso_cache *cache; 1086 1087 cache = rb_entry(next, struct dso_cache, rb_node); 1088 next = rb_next(&cache->rb_node); 1089 rb_erase(&cache->rb_node, root); 1090 free(cache); 1091 } 1092 mutex_unlock(dso__lock(dso)); 1093 } 1094 1095 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset) 1096 { 1097 const struct rb_root *root = &dso__data(dso)->cache; 1098 struct rb_node * const *p = &root->rb_node; 1099 const struct rb_node *parent = NULL; 1100 struct dso_cache *cache; 1101 1102 while (*p != NULL) { 1103 u64 end; 1104 1105 parent = *p; 1106 cache = rb_entry(parent, struct dso_cache, rb_node); 1107 end = cache->offset + DSO__DATA_CACHE_SIZE; 1108 1109 if (offset < cache->offset) 1110 p = &(*p)->rb_left; 1111 else if (offset >= end) 1112 p = &(*p)->rb_right; 1113 else 1114 return cache; 1115 } 1116 1117 return NULL; 1118 } 1119 1120 static struct dso_cache * 1121 dso_cache__insert(struct dso *dso, struct dso_cache *new) 1122 { 1123 struct rb_root *root = &dso__data(dso)->cache; 1124 struct rb_node **p = &root->rb_node; 1125 struct rb_node *parent = NULL; 1126 struct dso_cache *cache; 1127 u64 offset = new->offset; 1128 1129 mutex_lock(dso__lock(dso)); 1130 while (*p != NULL) { 1131 u64 end; 1132 1133 parent = *p; 1134 cache = rb_entry(parent, struct dso_cache, rb_node); 1135 end = cache->offset + DSO__DATA_CACHE_SIZE; 1136 1137 if (offset < cache->offset) 1138 p = &(*p)->rb_left; 1139 else if (offset >= end) 1140 p = &(*p)->rb_right; 1141 else 1142 goto out; 1143 } 1144 1145 rb_link_node(&new->rb_node, parent, p); 1146 rb_insert_color(&new->rb_node, root); 1147 1148 cache = NULL; 1149 out: 1150 mutex_unlock(dso__lock(dso)); 1151 return cache; 1152 } 1153 1154 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data, 1155 u64 size, bool out) 1156 { 1157 u64 cache_offset = offset - cache->offset; 1158 u64 cache_size; 1159 1160 /* 1161 * The RB tree matches using DSO__DATA_CACHE_SIZE, but a short 1162 * pread may leave cache->size smaller. For a regular file a 1163 * short pread only happens at end-of-file, so an offset past 1164 * the valid data is EOF: return 0, matching what a direct 1165 * pread() at that offset would return, and cached_io() then 1166 * stops its read loop. 1167 */ 1168 if (cache_offset >= cache->size) 1169 return 0; 1170 1171 cache_size = min(cache->size - cache_offset, size); 1172 1173 if (out) 1174 memcpy(data, cache->data + cache_offset, cache_size); 1175 else 1176 memcpy(cache->data + cache_offset, data, cache_size); 1177 return cache_size; 1178 } 1179 1180 static ssize_t file_read(struct dso *dso, struct machine *machine, 1181 u64 offset, char *data) 1182 { 1183 ssize_t ret; 1184 1185 mutex_lock(dso__data_open_lock()); 1186 1187 /* 1188 * dso__data(dso)->fd might be closed if other thread opened another 1189 * file (dso) due to open file limit (RLIMIT_NOFILE). 1190 */ 1191 try_to_open_dso(dso, machine); 1192 1193 if (dso__data(dso)->fd < 0) { 1194 dso__data(dso)->status = DSO_DATA_STATUS_ERROR; 1195 ret = dso__data(dso)->fd; 1196 goto out; 1197 } 1198 1199 ret = pread(dso__data(dso)->fd, data, DSO__DATA_CACHE_SIZE, offset); 1200 out: 1201 mutex_unlock(dso__data_open_lock()); 1202 return ret; 1203 } 1204 1205 static struct dso_cache *dso_cache__populate(struct dso *dso, 1206 struct machine *machine, 1207 u64 offset, ssize_t *ret) 1208 { 1209 u64 cache_offset = offset & DSO__DATA_CACHE_MASK; 1210 struct dso_cache *cache; 1211 struct dso_cache *old; 1212 1213 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); 1214 if (!cache) { 1215 *ret = -ENOMEM; 1216 return NULL; 1217 } 1218 #ifdef HAVE_LIBBPF_SUPPORT 1219 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) 1220 *ret = bpf_read(dso, cache_offset, cache->data); 1221 else 1222 #endif 1223 if (dso__binary_type(dso) == DSO_BINARY_TYPE__OOL) 1224 *ret = DSO__DATA_CACHE_SIZE; 1225 else 1226 *ret = file_read(dso, machine, cache_offset, cache->data); 1227 1228 if (*ret <= 0) { 1229 free(cache); 1230 return NULL; 1231 } 1232 1233 cache->offset = cache_offset; 1234 cache->size = *ret; 1235 1236 old = dso_cache__insert(dso, cache); 1237 if (old) { 1238 /* we lose the race */ 1239 free(cache); 1240 cache = old; 1241 } 1242 1243 return cache; 1244 } 1245 1246 static struct dso_cache *dso_cache__find(struct dso *dso, 1247 struct machine *machine, 1248 u64 offset, 1249 ssize_t *ret) 1250 { 1251 struct dso_cache *cache = __dso_cache__find(dso, offset); 1252 1253 return cache ? cache : dso_cache__populate(dso, machine, offset, ret); 1254 } 1255 1256 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine, 1257 u64 offset, u8 *data, ssize_t size, bool out) 1258 { 1259 struct dso_cache *cache; 1260 ssize_t ret = 0; 1261 1262 cache = dso_cache__find(dso, machine, offset, &ret); 1263 if (!cache) 1264 return ret; 1265 1266 return dso_cache__memcpy(cache, offset, data, size, out); 1267 } 1268 1269 /* 1270 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks 1271 * in the rb_tree. Any read to already cached data is served 1272 * by cached data. Writes update the cache only, not the backing file. 1273 */ 1274 static ssize_t cached_io(struct dso *dso, struct machine *machine, 1275 u64 offset, u8 *data, ssize_t size, bool out) 1276 { 1277 ssize_t r = 0; 1278 u8 *p = data; 1279 1280 do { 1281 ssize_t ret; 1282 1283 ret = dso_cache_io(dso, machine, offset, p, size, out); 1284 if (ret < 0) 1285 return ret; 1286 1287 /* Reached EOF, return what we have. */ 1288 if (!ret) 1289 break; 1290 1291 BUG_ON(ret > size); 1292 1293 r += ret; 1294 p += ret; 1295 offset += ret; 1296 size -= ret; 1297 1298 } while (size); 1299 1300 return r; 1301 } 1302 1303 static int file_size(struct dso *dso, struct machine *machine) 1304 { 1305 int ret = 0; 1306 struct stat st; 1307 1308 mutex_lock(dso__data_open_lock()); 1309 1310 /* 1311 * dso__data(dso)->fd might be closed if other thread opened another 1312 * file (dso) due to open file limit (RLIMIT_NOFILE). 1313 */ 1314 try_to_open_dso(dso, machine); 1315 1316 if (dso__data(dso)->fd < 0) { 1317 dso__data(dso)->status = DSO_DATA_STATUS_ERROR; 1318 ret = dso__data(dso)->fd; 1319 goto out; 1320 } 1321 1322 if (fstat(dso__data(dso)->fd, &st) < 0) { 1323 ret = -errno; 1324 pr_err("dso cache fstat failed: %m\n"); 1325 dso__data(dso)->status = DSO_DATA_STATUS_ERROR; 1326 goto out; 1327 } 1328 dso__data(dso)->file_size = st.st_size; 1329 1330 out: 1331 mutex_unlock(dso__data_open_lock()); 1332 return ret; 1333 } 1334 1335 int dso__data_file_size(struct dso *dso, struct machine *machine) 1336 { 1337 if (dso__data(dso)->file_size) 1338 return 0; 1339 1340 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR) 1341 return -1; 1342 #ifdef HAVE_LIBBPF_SUPPORT 1343 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) 1344 return bpf_size(dso); 1345 #endif 1346 return file_size(dso, machine); 1347 } 1348 1349 /** 1350 * dso__data_size - Return dso data size 1351 * @dso: dso object 1352 * @machine: machine object 1353 * 1354 * Return: dso data size 1355 */ 1356 off_t dso__data_size(struct dso *dso, struct machine *machine) 1357 { 1358 if (dso__data_file_size(dso, machine)) 1359 return -1; 1360 1361 /* For now just estimate dso data size is close to file size */ 1362 return dso__data(dso)->file_size; 1363 } 1364 1365 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine, 1366 u64 offset, u8 *data, ssize_t size, 1367 bool out) 1368 { 1369 if (dso__data_file_size(dso, machine)) 1370 return -1; 1371 1372 /* Check the offset sanity. */ 1373 if (offset > dso__data(dso)->file_size) 1374 return -1; 1375 1376 if (offset + size < offset) 1377 return -1; 1378 1379 return cached_io(dso, machine, offset, data, size, out); 1380 } 1381 1382 /** 1383 * dso__data_read_offset - Read data from dso file offset 1384 * @dso: dso object 1385 * @machine: machine object 1386 * @offset: file offset 1387 * @data: buffer to store data 1388 * @size: size of the @data buffer 1389 * 1390 * External interface to read data from dso file offset. Open 1391 * dso data file and use cached_read to get the data. 1392 */ 1393 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, 1394 u64 offset, u8 *data, ssize_t size) 1395 { 1396 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR) 1397 return -1; 1398 1399 return data_read_write_offset(dso, machine, offset, data, size, true); 1400 } 1401 1402 static enum dso_swap_type dso_swap_type__from_elf_data(unsigned char eidata) 1403 { 1404 static const unsigned int endian = 1; 1405 1406 switch (eidata) { 1407 case ELFDATA2LSB: 1408 /* We are big endian, DSO is little endian. */ 1409 return (*(unsigned char const *)&endian != 1) ? DSO_SWAP__YES : DSO_SWAP__NO; 1410 case ELFDATA2MSB: 1411 /* We are little endian, DSO is big endian. */ 1412 return (*(unsigned char const *)&endian != 0) ? DSO_SWAP__YES : DSO_SWAP__NO; 1413 default: 1414 return DSO_SWAP__UNSET; 1415 } 1416 } 1417 1418 /* Reads e_machine from fd, optionally caching data in dso. */ 1419 uint16_t dso__read_e_machine_endian(struct dso *optional_dso, int fd, uint32_t *e_flags, 1420 bool *is_big_endian) 1421 { 1422 uint16_t e_machine = EM_NONE; 1423 unsigned char e_ident[EI_NIDENT]; 1424 enum dso_swap_type swap_type; 1425 bool need_e_flags; 1426 1427 if (e_flags) 1428 *e_flags = 0; 1429 1430 { 1431 _Static_assert(offsetof(Elf32_Ehdr, e_ident) == 0, "Unexpected offset"); 1432 _Static_assert(offsetof(Elf64_Ehdr, e_ident) == 0, "Unexpected offset"); 1433 } 1434 if (pread(fd, &e_ident, sizeof(e_ident), 0) != sizeof(e_ident)) 1435 return EM_NONE; // Read failed. 1436 1437 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) 1438 return EM_NONE; // Not an ELF file. 1439 1440 if (e_ident[EI_CLASS] == ELFCLASSNONE || e_ident[EI_CLASS] >= ELFCLASSNUM) 1441 return EM_NONE; // Bad ELF class (32 or 64-bit objects). 1442 1443 if (e_ident[EI_VERSION] != EV_CURRENT) 1444 return EM_NONE; // Bad ELF version. 1445 1446 swap_type = dso_swap_type__from_elf_data(e_ident[EI_DATA]); 1447 if (swap_type == DSO_SWAP__UNSET) 1448 return EM_NONE; // Bad ELF data encoding. 1449 1450 if (is_big_endian) 1451 *is_big_endian = (e_ident[EI_DATA] == ELFDATA2MSB); 1452 1453 /* Cache the need for swapping. */ 1454 if (optional_dso) { 1455 assert(dso__needs_swap(optional_dso) == DSO_SWAP__UNSET || 1456 dso__needs_swap(optional_dso) == swap_type); 1457 dso__set_needs_swap(optional_dso, swap_type); 1458 } 1459 1460 { 1461 _Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset"); 1462 _Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset"); 1463 } 1464 if (pread(fd, &e_machine, sizeof(e_machine), 18) != sizeof(e_machine)) 1465 return EM_NONE; // e_machine read failed. 1466 1467 e_machine = DSO_SWAP_TYPE__SWAP(swap_type, uint16_t, e_machine); 1468 if (e_machine >= EM_NUM) 1469 return EM_NONE; // Bad ELF machine number. 1470 1471 #ifdef NDEBUG 1472 /* In production code the e_flags are only needed on CSKY. */ 1473 need_e_flags = e_flags && e_machine == EM_CSKY; 1474 #else 1475 /* Debug code will always read the e_flags. */ 1476 need_e_flags = e_flags != NULL; 1477 #endif 1478 if (need_e_flags) { 1479 off_t offset = e_ident[EI_CLASS] == ELFCLASS32 1480 ? offsetof(Elf32_Ehdr, e_flags) 1481 : offsetof(Elf64_Ehdr, e_flags); 1482 1483 if (pread(fd, e_flags, sizeof(*e_flags), offset) != sizeof(*e_flags)) { 1484 *e_flags = 0; 1485 return EM_NONE; // e_flags read failed. 1486 } 1487 } 1488 return e_machine; 1489 } 1490 1491 uint16_t dso__e_machine_endian(struct dso *dso, struct machine *machine, uint32_t *e_flags, 1492 bool *is_big_endian) 1493 { 1494 uint16_t e_machine = EM_NONE; 1495 enum dso_binary_type dbt = dso__find_binary_type(dso); 1496 int fd; 1497 1498 switch (dbt) { 1499 case DSO_BINARY_TYPE__KALLSYMS: 1500 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 1501 case DSO_BINARY_TYPE__VMLINUX: 1502 case DSO_BINARY_TYPE__GUEST_VMLINUX: 1503 case DSO_BINARY_TYPE__GUEST_KMODULE: 1504 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 1505 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 1506 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 1507 case DSO_BINARY_TYPE__KCORE: 1508 case DSO_BINARY_TYPE__GUEST_KCORE: 1509 case DSO_BINARY_TYPE__BPF_PROG_INFO: 1510 case DSO_BINARY_TYPE__BPF_IMAGE: 1511 case DSO_BINARY_TYPE__OOL: 1512 case DSO_BINARY_TYPE__JAVA_JIT: 1513 if (is_big_endian) { 1514 *is_big_endian = perf_arch_is_big_endian( 1515 machine && machine->env ? perf_env__arch(machine->env) : NULL); 1516 } 1517 return perf_env__e_machine(machine ? machine->env : NULL, e_flags); 1518 case DSO_BINARY_TYPE__DEBUGLINK: 1519 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 1520 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 1521 case DSO_BINARY_TYPE__GNU_DEBUGDATA: 1522 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 1523 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 1524 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 1525 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 1526 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 1527 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 1528 break; 1529 case DSO_BINARY_TYPE__NOT_FOUND: 1530 default: 1531 if (e_flags) 1532 *e_flags = 0; 1533 return EM_NONE; 1534 } 1535 1536 mutex_lock(dso__data_open_lock()); 1537 1538 /* 1539 * dso__data(dso)->fd might be closed if other thread opened another 1540 * file (dso) due to open file limit (RLIMIT_NOFILE). 1541 */ 1542 try_to_open_dso(dso, machine); 1543 fd = dso__data(dso)->fd; 1544 if (fd >= 0) 1545 e_machine = dso__read_e_machine_endian(dso, fd, e_flags, is_big_endian); 1546 else if (e_flags) 1547 *e_flags = 0; 1548 1549 mutex_unlock(dso__data_open_lock()); 1550 return e_machine; 1551 } 1552 1553 /** 1554 * dso__data_read_addr - Read data from dso address 1555 * @dso: dso object 1556 * @machine: machine object 1557 * @add: virtual memory address 1558 * @data: buffer to store data 1559 * @size: size of the @data buffer 1560 * 1561 * External interface to read data from dso address. 1562 */ 1563 ssize_t dso__data_read_addr(struct dso *dso, struct map *map, 1564 struct machine *machine, u64 addr, 1565 u8 *data, ssize_t size) 1566 { 1567 u64 offset = map__map_ip(map, addr); 1568 1569 return dso__data_read_offset(dso, machine, offset, data, size); 1570 } 1571 1572 /** 1573 * dso__data_write_cache_offs - Write data to dso data cache at file offset 1574 * @dso: dso object 1575 * @machine: machine object 1576 * @offset: file offset 1577 * @data: buffer to write 1578 * @size: size of the @data buffer 1579 * 1580 * Write into the dso file data cache, but do not change the file itself. 1581 */ 1582 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine, 1583 u64 offset, const u8 *data_in, ssize_t size) 1584 { 1585 u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */ 1586 1587 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR) 1588 return -1; 1589 1590 return data_read_write_offset(dso, machine, offset, data, size, false); 1591 } 1592 1593 /** 1594 * dso__data_write_cache_addr - Write data to dso data cache at dso address 1595 * @dso: dso object 1596 * @machine: machine object 1597 * @add: virtual memory address 1598 * @data: buffer to write 1599 * @size: size of the @data buffer 1600 * 1601 * External interface to write into the dso file data cache, but do not change 1602 * the file itself. 1603 */ 1604 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map, 1605 struct machine *machine, u64 addr, 1606 const u8 *data, ssize_t size) 1607 { 1608 u64 offset = map__map_ip(map, addr); 1609 1610 return dso__data_write_cache_offs(dso, machine, offset, data, size); 1611 } 1612 1613 struct map *dso__new_map(const char *name) 1614 { 1615 struct map *map = NULL; 1616 struct dso *dso = dso__new(name); 1617 1618 if (dso) { 1619 map = map__new2(0, dso); 1620 dso__put(dso); 1621 } 1622 1623 return map; 1624 } 1625 1626 struct dso *machine__findnew_kernel(struct machine *machine, const char *name, 1627 const char *short_name, int dso_type) 1628 { 1629 /* 1630 * The kernel dso could be created by build_id processing. 1631 */ 1632 struct dso *dso = machine__findnew_dso(machine, name); 1633 1634 /* 1635 * We need to run this in all cases, since during the build_id 1636 * processing we had no idea this was the kernel dso. 1637 */ 1638 if (dso != NULL) { 1639 dso__set_short_name(dso, short_name, false); 1640 dso__set_kernel(dso, dso_type); 1641 } 1642 1643 return dso; 1644 } 1645 1646 static void __dso__set_long_name_id(struct dso *dso, const char *name, bool name_allocated) 1647 { 1648 if (dso__long_name_allocated(dso)) 1649 free((char *)dso__long_name(dso)); 1650 1651 RC_CHK_ACCESS(dso)->long_name = name; 1652 RC_CHK_ACCESS(dso)->long_name_len = strlen(name); 1653 dso__set_long_name_allocated(dso, name_allocated); 1654 } 1655 1656 static void dso__set_long_name_id(struct dso *dso, const char *name, bool name_allocated) 1657 { 1658 struct dsos *dsos = dso__dsos(dso); 1659 1660 if (name == NULL) 1661 return; 1662 1663 if (dsos) { 1664 /* 1665 * Need to avoid re-sorting the dsos breaking by non-atomically 1666 * renaming the dso. 1667 */ 1668 down_write(&dsos->lock); 1669 __dso__set_long_name_id(dso, name, name_allocated); 1670 dsos->sorted = false; 1671 up_write(&dsos->lock); 1672 } else { 1673 __dso__set_long_name_id(dso, name, name_allocated); 1674 } 1675 } 1676 1677 static int __dso_id__cmp(const struct dso_id *a, const struct dso_id *b) 1678 { 1679 if (a->mmap2_valid && b->mmap2_valid) { 1680 if (a->maj > b->maj) return -1; 1681 if (a->maj < b->maj) return 1; 1682 1683 if (a->min > b->min) return -1; 1684 if (a->min < b->min) return 1; 1685 1686 if (a->ino > b->ino) return -1; 1687 if (a->ino < b->ino) return 1; 1688 } 1689 if (a->mmap2_ino_generation_valid && b->mmap2_ino_generation_valid) { 1690 if (a->ino_generation > b->ino_generation) return -1; 1691 if (a->ino_generation < b->ino_generation) return 1; 1692 } 1693 if (build_id__is_defined(&a->build_id) && build_id__is_defined(&b->build_id)) { 1694 if (a->build_id.size != b->build_id.size) 1695 return a->build_id.size < b->build_id.size ? -1 : 1; 1696 return memcmp(a->build_id.data, b->build_id.data, a->build_id.size); 1697 } 1698 return 0; 1699 } 1700 1701 const struct dso_id dso_id_empty = { 1702 { 1703 .maj = 0, 1704 .min = 0, 1705 .ino = 0, 1706 .ino_generation = 0, 1707 }, 1708 .mmap2_valid = false, 1709 .mmap2_ino_generation_valid = false, 1710 { 1711 .size = 0, 1712 } 1713 }; 1714 1715 void __dso__improve_id(struct dso *dso, const struct dso_id *id) 1716 { 1717 struct dsos *dsos = dso__dsos(dso); 1718 struct dso_id *dso_id = dso__id(dso); 1719 bool changed = false; 1720 1721 /* dsos write lock held by caller. */ 1722 1723 if (id->mmap2_valid && !dso_id->mmap2_valid) { 1724 dso_id->maj = id->maj; 1725 dso_id->min = id->min; 1726 dso_id->ino = id->ino; 1727 dso_id->mmap2_valid = true; 1728 changed = true; 1729 } 1730 if (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid) { 1731 dso_id->ino_generation = id->ino_generation; 1732 dso_id->mmap2_ino_generation_valid = true; 1733 changed = true; 1734 } 1735 if (build_id__is_defined(&id->build_id) && !build_id__is_defined(&dso_id->build_id)) { 1736 dso_id->build_id = id->build_id; 1737 changed = true; 1738 } 1739 if (changed && dsos) 1740 dsos->sorted = false; 1741 } 1742 1743 int dso_id__cmp(const struct dso_id *a, const struct dso_id *b) 1744 { 1745 if (a == &dso_id_empty || b == &dso_id_empty) { 1746 /* There is no valid data to compare so the comparison always returns identical. */ 1747 return 0; 1748 } 1749 1750 return __dso_id__cmp(a, b); 1751 } 1752 1753 int dso__cmp_id(struct dso *a, struct dso *b) 1754 { 1755 return __dso_id__cmp(dso__id(a), dso__id(b)); 1756 } 1757 1758 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated) 1759 { 1760 dso__set_long_name_id(dso, name, name_allocated); 1761 } 1762 1763 static void __dso__set_short_name(struct dso *dso, const char *name, bool name_allocated) 1764 { 1765 if (dso__short_name_allocated(dso)) 1766 free((char *)dso__short_name(dso)); 1767 1768 RC_CHK_ACCESS(dso)->short_name = name; 1769 RC_CHK_ACCESS(dso)->short_name_len = strlen(name); 1770 dso__set_short_name_allocated(dso, name_allocated); 1771 } 1772 1773 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated) 1774 { 1775 struct dsos *dsos = dso__dsos(dso); 1776 1777 if (name == NULL) 1778 return; 1779 1780 if (dsos) { 1781 /* 1782 * Need to avoid re-sorting the dsos breaking by non-atomically 1783 * renaming the dso. 1784 */ 1785 down_write(&dsos->lock); 1786 __dso__set_short_name(dso, name, name_allocated); 1787 dsos->sorted = false; 1788 up_write(&dsos->lock); 1789 } else { 1790 __dso__set_short_name(dso, name, name_allocated); 1791 } 1792 } 1793 1794 int dso__name_len(const struct dso *dso) 1795 { 1796 if (!dso) 1797 return strlen("[unknown]"); 1798 if (verbose > 0) 1799 return dso__long_name_len(dso); 1800 1801 return dso__short_name_len(dso); 1802 } 1803 1804 bool dso__loaded(const struct dso *dso) 1805 { 1806 return RC_CHK_ACCESS(dso)->loaded; 1807 } 1808 1809 bool dso__sorted_by_name(const struct dso *dso) 1810 { 1811 return RC_CHK_ACCESS(dso)->sorted_by_name; 1812 } 1813 1814 void dso__set_sorted_by_name(struct dso *dso) 1815 { 1816 RC_CHK_ACCESS(dso)->sorted_by_name = true; 1817 } 1818 1819 struct dso *dso__new_id(const char *name, const struct dso_id *id) 1820 { 1821 RC_STRUCT(dso) *dso = zalloc(sizeof(*dso) + strlen(name) + 1); 1822 struct dso *res; 1823 struct dso_data *data; 1824 1825 if (!dso) 1826 return NULL; 1827 1828 if (ADD_RC_CHK(res, dso)) { 1829 strcpy(dso->name, name); 1830 if (id) 1831 dso->id = *id; 1832 dso__set_long_name_id(res, dso->name, false); 1833 dso__set_short_name(res, dso->name, false); 1834 dso->symbols = RB_ROOT_CACHED; 1835 dso->symbol_names = NULL; 1836 dso->symbol_names_len = 0; 1837 dso->inlined_nodes = RB_ROOT_CACHED; 1838 dso->srclines = RB_ROOT_CACHED; 1839 dso->data_types = RB_ROOT; 1840 dso->global_vars = RB_ROOT; 1841 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; 1842 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; 1843 dso->dbginfo_type = DSO_BINARY_TYPE__NOT_FOUND; 1844 dso->is_64_bit = (sizeof(void *) == 8); 1845 dso->loaded = 0; 1846 dso->rel = 0; 1847 dso->sorted_by_name = 0; 1848 dso->has_srcline = 1; 1849 dso->a2l_fails = 1; 1850 dso->kernel = DSO_SPACE__USER; 1851 dso->is_kmod = 0; 1852 dso->needs_swap = DSO_SWAP__UNSET; 1853 dso->comp = COMP_ID__NONE; 1854 mutex_init(&dso->lock); 1855 refcount_set(&dso->refcnt, 1); 1856 data = &dso->data; 1857 data->cache = RB_ROOT; 1858 data->fd = -1; 1859 data->status = DSO_DATA_STATUS_UNKNOWN; 1860 INIT_LIST_HEAD(&data->open_entry); 1861 #ifdef REFCNT_CHECKING 1862 data->dso = NULL; /* Set when on the open_entry list. */ 1863 #endif 1864 } 1865 return res; 1866 } 1867 1868 struct dso *dso__new(const char *name) 1869 { 1870 return dso__new_id(name, NULL); 1871 } 1872 1873 void dso__delete(struct dso *dso) 1874 { 1875 if (dso__dsos(dso)) 1876 pr_err("DSO %s is still in rbtree when being deleted!\n", dso__long_name(dso)); 1877 1878 /* free inlines first, as they reference symbols */ 1879 inlines__tree_delete(&RC_CHK_ACCESS(dso)->inlined_nodes); 1880 srcline__tree_delete(&RC_CHK_ACCESS(dso)->srclines); 1881 symbols__delete(&RC_CHK_ACCESS(dso)->symbols); 1882 RC_CHK_ACCESS(dso)->symbol_names_len = 0; 1883 zfree(&RC_CHK_ACCESS(dso)->symbol_names); 1884 annotated_data_type__tree_delete(dso__data_types(dso)); 1885 global_var_type__tree_delete(dso__global_vars(dso)); 1886 1887 if (RC_CHK_ACCESS(dso)->short_name_allocated) { 1888 zfree((char **)&RC_CHK_ACCESS(dso)->short_name); 1889 RC_CHK_ACCESS(dso)->short_name_allocated = false; 1890 } 1891 1892 if (RC_CHK_ACCESS(dso)->long_name_allocated) { 1893 zfree((char **)&RC_CHK_ACCESS(dso)->long_name); 1894 RC_CHK_ACCESS(dso)->long_name_allocated = false; 1895 } 1896 1897 dso__data_close(dso); 1898 auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache); 1899 dso_cache__free(dso); 1900 dso__free_a2l(dso); 1901 dso__free_a2l_libbfd(dso); 1902 dso__free_libdw(dso); 1903 dso__free_symsrc_filename(dso); 1904 nsinfo__zput(RC_CHK_ACCESS(dso)->nsinfo); 1905 mutex_destroy(dso__lock(dso)); 1906 RC_CHK_FREE(dso); 1907 } 1908 1909 struct dso *dso__get(struct dso *dso) 1910 { 1911 struct dso *result; 1912 1913 if (RC_CHK_GET(result, dso)) 1914 refcount_inc(&RC_CHK_ACCESS(dso)->refcnt); 1915 1916 return result; 1917 } 1918 1919 void dso__put(struct dso *dso) 1920 { 1921 #ifdef REFCNT_CHECKING 1922 if (dso && dso__data(dso) && refcount_read(&RC_CHK_ACCESS(dso)->refcnt) == 2) 1923 dso__data_close(dso); 1924 #endif 1925 if (dso && refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt)) 1926 dso__delete(dso); 1927 else 1928 RC_CHK_PUT(dso); 1929 } 1930 1931 int dso__swap_init(struct dso *dso, unsigned char eidata) 1932 { 1933 enum dso_swap_type type = dso_swap_type__from_elf_data(eidata); 1934 1935 dso__set_needs_swap(dso, type); 1936 if (type == DSO_SWAP__UNSET) { 1937 pr_err("unrecognized DSO data encoding %d\n", eidata); 1938 return -EINVAL; 1939 } 1940 return 0; 1941 } 1942 1943 void dso__set_build_id(struct dso *dso, const struct build_id *bid) 1944 { 1945 dso__id(dso)->build_id = *bid; 1946 } 1947 1948 bool dso__build_id_equal(const struct dso *dso, const struct build_id *bid) 1949 { 1950 const struct build_id *dso_bid = dso__bid(dso); 1951 1952 if (dso_bid->size > bid->size && dso_bid->size == BUILD_ID_SIZE) { 1953 /* 1954 * For the backward compatibility, it allows a build-id has 1955 * trailing zeros. 1956 */ 1957 return !memcmp(dso_bid->data, bid->data, bid->size) && 1958 !memchr_inv(&dso_bid->data[bid->size], 0, 1959 dso_bid->size - bid->size); 1960 } 1961 1962 return dso_bid->size == bid->size && 1963 memcmp(dso_bid->data, bid->data, dso_bid->size) == 0; 1964 } 1965 1966 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) 1967 { 1968 char path[PATH_MAX]; 1969 struct build_id bid = { .size = 0, }; 1970 1971 if (machine__is_default_guest(machine)) 1972 return; 1973 snprintf(path, sizeof(path), "%s/sys/kernel/notes", machine->root_dir); 1974 sysfs__read_build_id(path, &bid); 1975 dso__set_build_id(dso, &bid); 1976 } 1977 1978 int dso__kernel_module_get_build_id(struct dso *dso, 1979 const char *root_dir) 1980 { 1981 char filename[PATH_MAX]; 1982 struct build_id bid = { .size = 0, }; 1983 /* 1984 * kernel module short names are of the form "[module]" and 1985 * we need just "module" here. 1986 */ 1987 const char *name = dso__short_name(dso) + 1; 1988 1989 snprintf(filename, sizeof(filename), 1990 "%s/sys/module/%.*s/notes/.note.gnu.build-id", 1991 root_dir, (int)strlen(name) - 1, name); 1992 1993 sysfs__read_build_id(filename, &bid); 1994 dso__set_build_id(dso, &bid); 1995 return 0; 1996 } 1997 1998 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) 1999 { 2000 char sbuild_id[SBUILD_ID_SIZE]; 2001 2002 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); 2003 return fprintf(fp, "%s", sbuild_id); 2004 } 2005 2006 size_t dso__fprintf(struct dso *dso, FILE *fp) 2007 { 2008 struct rb_node *nd; 2009 size_t ret = fprintf(fp, "dso: %s (", dso__short_name(dso)); 2010 2011 if (dso__short_name(dso) != dso__long_name(dso)) 2012 ret += fprintf(fp, "%s, ", dso__long_name(dso)); 2013 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT "); 2014 ret += dso__fprintf_buildid(dso, fp); 2015 ret += fprintf(fp, ")\n"); 2016 for (nd = rb_first_cached(dso__symbols(dso)); nd; nd = rb_next(nd)) { 2017 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 2018 ret += symbol__fprintf(pos, fp); 2019 } 2020 2021 return ret; 2022 } 2023 2024 enum dso_type dso__type(struct dso *dso, struct machine *machine) 2025 { 2026 int fd = -1; 2027 enum dso_type type = DSO__TYPE_UNKNOWN; 2028 2029 if (dso__data_get_fd(dso, machine, &fd)) { 2030 type = dso__type_fd(fd); 2031 dso__data_put_fd(dso); 2032 } 2033 2034 return type; 2035 } 2036 2037 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen) 2038 { 2039 int idx, errnum = *dso__load_errno(dso); 2040 /* 2041 * This must have a same ordering as the enum dso_load_errno. 2042 */ 2043 static const char *dso_load__error_str[] = { 2044 "Internal tools/perf/ library error", 2045 "Invalid ELF file", 2046 "Can not read build id", 2047 "Mismatching build id", 2048 "Decompression failure", 2049 }; 2050 2051 BUG_ON(buflen == 0); 2052 2053 if (errnum >= 0) { 2054 errno = errnum; 2055 scnprintf(buf, buflen, "%m"); 2056 2057 return 0; 2058 } 2059 2060 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END) 2061 return -1; 2062 2063 idx = errnum - __DSO_LOAD_ERRNO__START; 2064 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]); 2065 return 0; 2066 } 2067 2068 bool perf_pid_map_tid(const char *dso_name, int *tid) 2069 { 2070 return sscanf(dso_name, "/tmp/perf-%d.map", tid) == 1; 2071 } 2072 2073 bool is_perf_pid_map_name(const char *dso_name) 2074 { 2075 int tid; 2076 2077 return perf_pid_map_tid(dso_name, &tid); 2078 } 2079 2080 struct find_file_offset_data { 2081 u64 ip; 2082 u64 offset; 2083 }; 2084 2085 /* This will be called for each PHDR in an ELF binary */ 2086 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg) 2087 { 2088 struct find_file_offset_data *data = arg; 2089 2090 if (start <= data->ip && data->ip < start + len) { 2091 data->offset = pgoff + data->ip - start; 2092 return 1; 2093 } 2094 return 0; 2095 } 2096 2097 static const u8 *__dso__read_symbol(struct dso *dso, const char *symfs_filename, 2098 u64 start, size_t len, 2099 u8 **out_buf, u64 *out_buf_len, bool *is_64bit) 2100 { 2101 struct nscookie nsc; 2102 int fd; 2103 ssize_t count; 2104 struct find_file_offset_data data = { 2105 .ip = start, 2106 }; 2107 u8 *code_buf = NULL; 2108 int saved_errno; 2109 2110 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 2111 fd = open(symfs_filename, O_RDONLY | O_CLOEXEC); 2112 saved_errno = errno; 2113 nsinfo__mountns_exit(&nsc); 2114 if (fd < 0) { 2115 errno = saved_errno; 2116 return NULL; 2117 } 2118 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) <= 0) { 2119 close(fd); 2120 errno = ENOENT; 2121 return NULL; 2122 } 2123 code_buf = malloc(len); 2124 if (code_buf == NULL) { 2125 close(fd); 2126 errno = ENOMEM; 2127 return NULL; 2128 } 2129 count = pread(fd, code_buf, len, data.offset); 2130 saved_errno = errno; 2131 close(fd); 2132 if ((u64)count != len) { 2133 free(code_buf); 2134 errno = saved_errno; 2135 return NULL; 2136 } 2137 *out_buf = code_buf; 2138 *out_buf_len = len; 2139 return code_buf; 2140 } 2141 2142 /* 2143 * Read a symbol into memory for disassembly by a library like capstone of 2144 * libLLVM. If memory is allocated out_buf holds it. 2145 */ 2146 const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, 2147 const struct map *map, const struct symbol *sym, 2148 u8 **out_buf, u64 *out_buf_len, bool *is_64bit) 2149 { 2150 u64 start = map__rip_2objdump(map, sym->start); 2151 u64 end = map__rip_2objdump(map, sym->end); 2152 size_t len = end - start; 2153 2154 *out_buf = NULL; 2155 *out_buf_len = 0; 2156 *is_64bit = false; 2157 2158 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) { 2159 /* 2160 * Note, there is fallback BPF image disassembly in the objdump 2161 * version but it currently does nothing. 2162 */ 2163 errno = EOPNOTSUPP; 2164 return NULL; 2165 } 2166 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) { 2167 #ifdef HAVE_LIBBPF_SUPPORT 2168 struct bpf_prog_info_node *info_node; 2169 struct perf_bpil *info_linear; 2170 2171 *is_64bit = sizeof(void *) == sizeof(u64); 2172 info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env, 2173 dso__bpf_prog(dso)->id); 2174 if (!info_node) { 2175 errno = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; 2176 return NULL; 2177 } 2178 info_linear = info_node->info_linear; 2179 if (!(info_linear->arrays & (1UL << PERF_BPIL_JITED_INSNS))) { 2180 errno = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; 2181 return NULL; 2182 } 2183 if (len > info_linear->info.jited_prog_len) { 2184 pr_debug("BPF symbol length %zu exceeds jited_prog_len %u\n", 2185 len, info_linear->info.jited_prog_len); 2186 errno = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; 2187 return NULL; 2188 } 2189 *out_buf_len = len; 2190 return (const u8 *)(uintptr_t)(info_linear->info.jited_prog_insns); 2191 #else 2192 pr_debug("No BPF program disassembly support\n"); 2193 errno = EOPNOTSUPP; 2194 return NULL; 2195 #endif 2196 } 2197 return __dso__read_symbol(dso, symfs_filename, start, len, 2198 out_buf, out_buf_len, is_64bit); 2199 } 2200 2201 struct debuginfo *dso__debuginfo(struct dso *dso) 2202 { 2203 struct debuginfo *dinfo; 2204 2205 mutex_lock(dso__lock(dso)); 2206 dinfo = debuginfo__new(dso); 2207 mutex_unlock(dso__lock(dso)); 2208 return dinfo; 2209 } 2210 2211 void dso__set_symsrc_filename(struct dso *dso, char *val) 2212 { 2213 RC_CHK_ACCESS(dso)->symsrc_filename = val; 2214 dso__free_libdw(dso); 2215 dso__free_a2l(dso); 2216 dso__free_a2l_libbfd(dso); 2217 dso__set_has_srcline(dso, true); 2218 dso__set_a2l_fails(dso, 0); 2219 } 2220 2221 void dso__find_dbginfo_type(struct dso *dso) 2222 { 2223 static const enum dso_binary_type dbginfo_types[] = { 2224 DSO_BINARY_TYPE__FEDORA_DEBUGINFO, 2225 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, 2226 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO, 2227 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, 2228 DSO_BINARY_TYPE__BUILDID_DEBUGINFO, 2229 DSO_BINARY_TYPE__SYSTEM_PATH_DSO, 2230 DSO_BINARY_TYPE__DEBUGLINK, 2231 DSO_BINARY_TYPE__NOT_FOUND, 2232 }; 2233 const enum dso_binary_type *type; 2234 char *path; 2235 bool found, decomp = false; 2236 2237 if (dso__dbginfo_type(dso) != DSO_BINARY_TYPE__NOT_FOUND) 2238 return; 2239 2240 if (dso__debuginfo_searched(dso)) 2241 return; 2242 dso__set_debuginfo_searched(dso); 2243 2244 /* Maybe debug info is in the same file with the symbol table */ 2245 path = dso__get_filename(dso, "", &decomp, dso__symtab_type(dso)); 2246 if (path) { 2247 found = filename__has_section(path, ".debug_info") || 2248 filename__has_section(path, ".zdebug_info"); 2249 2250 dso__put_filename(dso, path, decomp); 2251 if (found) { 2252 dso__set_dbginfo_type(dso, dso__symtab_type(dso)); 2253 return; 2254 } 2255 } 2256 2257 /* Otherwise check distro debug locations */ 2258 for (type = dbginfo_types; *type != DSO_BINARY_TYPE__NOT_FOUND; type++) { 2259 path = dso__get_filename(dso, "", &decomp, *type); 2260 if (path == NULL) 2261 continue; 2262 2263 found = filename__has_section(path, ".debug_info") || 2264 filename__has_section(path, ".zdebug_info"); 2265 2266 dso__put_filename(dso, path, decomp); 2267 if (found) 2268 break; 2269 } 2270 2271 dso__set_dbginfo_type(dso, *type); 2272 } 2273