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