1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * build-id.c 4 * 5 * build-id support 6 * 7 * Copyright (C) 2009, 2010 Red Hat Inc. 8 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com> 9 */ 10 #include "util.h" // lsdir(), mkdir_p(), rm_rf() 11 #include <dirent.h> 12 #include <errno.h> 13 #include <stdio.h> 14 #include <sys/stat.h> 15 #include <sys/types.h> 16 #include "util/copyfile.h" 17 #include "dso.h" 18 #include "build-id.h" 19 #include "event.h" 20 #include "namespaces.h" 21 #include "map.h" 22 #include "symbol.h" 23 #include "thread.h" 24 #include <linux/kernel.h> 25 #include "debug.h" 26 #include "session.h" 27 #include "tool.h" 28 #include "header.h" 29 #include "vdso.h" 30 #include "path.h" 31 #include "probe-file.h" 32 #include "strlist.h" 33 34 #ifdef HAVE_DEBUGINFOD_SUPPORT 35 #include <elfutils/debuginfod.h> 36 #endif 37 38 #include <linux/ctype.h> 39 #include <linux/zalloc.h> 40 #include <linux/string.h> 41 #include <asm/bug.h> 42 43 static bool no_buildid_cache; 44 45 static int mark_dso_hit_callback(struct callchain_cursor_node *node, void *data __maybe_unused) 46 { 47 struct map *map = node->ms.map; 48 49 if (map) 50 dso__set_hit(map__dso(map)); 51 52 return 0; 53 } 54 55 int build_id__mark_dso_hit(const struct perf_tool *tool __maybe_unused, 56 union perf_event *event, 57 struct perf_sample *sample, 58 struct evsel *evsel, 59 struct machine *machine) 60 { 61 struct addr_location al; 62 struct thread *thread = machine__findnew_thread(machine, sample->pid, 63 sample->tid); 64 65 if (thread == NULL) { 66 pr_err("problem processing %d event, skipping it.\n", 67 event->header.type); 68 return -1; 69 } 70 71 addr_location__init(&al); 72 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) 73 dso__set_hit(map__dso(al.map)); 74 75 addr_location__exit(&al); 76 77 sample__for_each_callchain_node(thread, evsel, sample, PERF_MAX_STACK_DEPTH, 78 /*symbols=*/false, mark_dso_hit_callback, /*data=*/NULL); 79 80 81 thread__put(thread); 82 return 0; 83 } 84 85 int build_id__snprintf(const struct build_id *build_id, char *bf, size_t bf_size) 86 { 87 size_t offs = 0; 88 89 for (size_t i = 0; i < build_id->size && offs < bf_size; ++i) 90 offs += snprintf(bf + offs, bf_size - offs, "%02x", build_id->data[i]); 91 92 return offs; 93 } 94 95 int sysfs__snprintf_build_id(const char *root_dir, char *sbuild_id, size_t sbuild_id_size) 96 { 97 char notes[PATH_MAX]; 98 struct build_id bid = { .size = 0, }; 99 int ret; 100 101 if (!root_dir) 102 root_dir = ""; 103 104 scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir); 105 106 ret = sysfs__read_build_id(notes, &bid); 107 if (ret < 0) 108 return ret; 109 110 return build_id__snprintf(&bid, sbuild_id, sbuild_id_size); 111 } 112 113 int filename__snprintf_build_id(const char *pathname, char *sbuild_id, size_t sbuild_id_size) 114 { 115 struct build_id bid = { .size = 0, }; 116 int ret; 117 118 ret = filename__read_build_id(pathname, &bid, /*block=*/true); 119 if (ret < 0) 120 return ret; 121 122 return build_id__snprintf(&bid, sbuild_id, sbuild_id_size); 123 } 124 125 /* asnprintf consolidates asprintf and snprintf */ 126 static int asnprintf(char **strp, size_t size, const char *fmt, ...) 127 { 128 va_list ap; 129 int ret; 130 131 if (!strp) 132 return -EINVAL; 133 134 va_start(ap, fmt); 135 if (*strp) 136 ret = vsnprintf(*strp, size, fmt, ap); 137 else 138 ret = vasprintf(strp, fmt, ap); 139 va_end(ap); 140 141 return ret; 142 } 143 144 char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf, 145 size_t size) 146 { 147 bool retry_old = true; 148 149 snprintf(bf, size, "%s/%s/%s/kallsyms", 150 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); 151 retry: 152 if (!access(bf, F_OK)) 153 return bf; 154 if (retry_old) { 155 /* Try old style kallsyms cache */ 156 snprintf(bf, size, "%s/%s/%s", 157 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); 158 retry_old = false; 159 goto retry; 160 } 161 162 return NULL; 163 } 164 165 char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size) 166 { 167 char *tmp = bf; 168 int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir, 169 sbuild_id, sbuild_id + 2); 170 if (ret < 0 || (tmp && size < (unsigned int)ret)) 171 return NULL; 172 return bf; 173 } 174 175 /* The caller is responsible to free the returned buffer. */ 176 char *build_id_cache__origname(const char *sbuild_id) 177 { 178 char *linkname; 179 char buf[PATH_MAX]; 180 char *ret = NULL, *p; 181 size_t offs = 5; /* == strlen("../..") */ 182 ssize_t len; 183 184 linkname = build_id_cache__linkname(sbuild_id, NULL, 0); 185 if (!linkname) 186 return NULL; 187 188 len = readlink(linkname, buf, sizeof(buf) - 1); 189 if (len <= 0) 190 goto out; 191 buf[len] = '\0'; 192 193 /* The link should be "../..<origpath>/<sbuild_id>" */ 194 p = strrchr(buf, '/'); /* Cut off the "/<sbuild_id>" */ 195 if (p && (p > buf + offs)) { 196 *p = '\0'; 197 if (buf[offs + 1] == '[') 198 offs++; /* 199 * This is a DSO name, like [kernel.kallsyms]. 200 * Skip the first '/', since this is not the 201 * cache of a regular file. 202 */ 203 ret = strdup(buf + offs); /* Skip "../..[/]" */ 204 } 205 out: 206 free(linkname); 207 return ret; 208 } 209 210 /* Check if the given build_id cache is valid on current running system */ 211 static bool build_id_cache__valid_id(char *sbuild_id) 212 { 213 char real_sbuild_id[SBUILD_ID_SIZE] = ""; 214 char *pathname; 215 int ret = 0; 216 bool result = false; 217 218 pathname = build_id_cache__origname(sbuild_id); 219 if (!pathname) 220 return false; 221 222 if (!strcmp(pathname, DSO__NAME_KALLSYMS)) 223 ret = sysfs__snprintf_build_id("/", real_sbuild_id, sizeof(real_sbuild_id)); 224 else if (pathname[0] == '/') 225 ret = filename__snprintf_build_id(pathname, real_sbuild_id, sizeof(real_sbuild_id)); 226 else 227 ret = -EINVAL; /* Should we support other special DSO cache? */ 228 if (ret >= 0) 229 result = (strcmp(sbuild_id, real_sbuild_id) == 0); 230 free(pathname); 231 232 return result; 233 } 234 235 static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso, 236 bool is_debug) 237 { 238 return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : (is_debug ? 239 "debug" : "elf")); 240 } 241 242 char *__dso__build_id_filename(const struct dso *dso, char *bf, size_t size, 243 bool is_debug, bool is_kallsyms) 244 { 245 bool is_vdso = dso__is_vdso((struct dso *)dso); 246 char sbuild_id[SBUILD_ID_SIZE]; 247 char *linkname; 248 bool alloc = (bf == NULL); 249 int ret; 250 251 if (!dso__has_build_id(dso)) 252 return NULL; 253 254 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); 255 linkname = build_id_cache__linkname(sbuild_id, NULL, 0); 256 if (!linkname) 257 return NULL; 258 259 /* Check if old style build_id cache */ 260 if (is_regular_file(linkname)) 261 ret = asnprintf(&bf, size, "%s", linkname); 262 else 263 ret = asnprintf(&bf, size, "%s/%s", linkname, 264 build_id_cache__basename(is_kallsyms, is_vdso, 265 is_debug)); 266 if (ret < 0 || (!alloc && size < (unsigned int)ret)) 267 bf = NULL; 268 free(linkname); 269 270 return bf; 271 } 272 273 char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size, 274 bool is_debug) 275 { 276 bool is_kallsyms = dso__is_kallsyms((struct dso *)dso); 277 278 return __dso__build_id_filename(dso, bf, size, is_debug, is_kallsyms); 279 } 280 281 static int write_buildid(const char *name, size_t name_len, struct build_id *bid, 282 pid_t pid, u16 misc, struct feat_fd *fd) 283 { 284 int err; 285 struct perf_record_header_build_id b; 286 size_t len; 287 288 len = name_len + 1; 289 len = PERF_ALIGN(len, sizeof(u64)); 290 291 memset(&b, 0, sizeof(b)); 292 memcpy(&b.data, bid->data, bid->size); 293 b.size = (u8) bid->size; 294 misc |= PERF_RECORD_MISC_BUILD_ID_SIZE; 295 b.pid = pid; 296 b.header.misc = misc; 297 b.header.size = sizeof(b) + len; 298 299 err = do_write(fd, &b, sizeof(b)); 300 if (err < 0) 301 return err; 302 303 return write_padded(fd, name, name_len + 1, len); 304 } 305 306 struct machine__write_buildid_table_cb_args { 307 struct machine *machine; 308 struct feat_fd *fd; 309 u16 kmisc, umisc; 310 }; 311 312 static int machine__write_buildid_table_cb(struct dso *dso, void *data) 313 { 314 struct machine__write_buildid_table_cb_args *args = data; 315 const char *name; 316 size_t name_len; 317 bool in_kernel = false; 318 319 if (!dso__has_build_id(dso)) 320 return 0; 321 322 if (!dso__hit(dso) && !dso__is_vdso(dso)) 323 return 0; 324 325 if (dso__is_vdso(dso)) { 326 name = dso__short_name(dso); 327 name_len = dso__short_name_len(dso); 328 } else if (dso__is_kcore(dso)) { 329 name = args->machine->mmap_name; 330 name_len = strlen(name); 331 } else { 332 name = dso__long_name(dso); 333 name_len = dso__long_name_len(dso); 334 } 335 336 in_kernel = dso__kernel(dso) || is_kernel_module(name, PERF_RECORD_MISC_CPUMODE_UNKNOWN); 337 return write_buildid(name, name_len, &dso__id(dso)->build_id, args->machine->pid, 338 in_kernel ? args->kmisc : args->umisc, args->fd); 339 } 340 341 static int machine__write_buildid_table(struct machine *machine, struct feat_fd *fd) 342 { 343 struct machine__write_buildid_table_cb_args args = { 344 .machine = machine, 345 .fd = fd, 346 .kmisc = PERF_RECORD_MISC_KERNEL, 347 .umisc = PERF_RECORD_MISC_USER, 348 }; 349 350 if (!machine__is_host(machine)) { 351 args.kmisc = PERF_RECORD_MISC_GUEST_KERNEL; 352 args.umisc = PERF_RECORD_MISC_GUEST_USER; 353 } 354 355 return dsos__for_each_dso(&machine->dsos, machine__write_buildid_table_cb, &args); 356 } 357 358 int perf_session__write_buildid_table(struct perf_session *session, 359 struct feat_fd *fd) 360 { 361 struct rb_node *nd; 362 int err = machine__write_buildid_table(&session->machines.host, fd); 363 364 if (err) 365 return err; 366 367 for (nd = rb_first_cached(&session->machines.guests); nd; 368 nd = rb_next(nd)) { 369 struct machine *pos = rb_entry(nd, struct machine, rb_node); 370 err = machine__write_buildid_table(pos, fd); 371 if (err) 372 break; 373 } 374 return err; 375 } 376 377 void disable_buildid_cache(void) 378 { 379 no_buildid_cache = true; 380 } 381 382 static bool lsdir_bid_head_filter(const char *name __maybe_unused, 383 struct dirent *d) 384 { 385 return (strlen(d->d_name) == 2) && 386 isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]); 387 } 388 389 static bool lsdir_bid_tail_filter(const char *name __maybe_unused, 390 struct dirent *d) 391 { 392 int i = 0; 393 while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3) 394 i++; 395 return (i >= SBUILD_ID_MIN_SIZE - 3) && (i <= SBUILD_ID_SIZE - 3) && 396 (d->d_name[i] == '\0'); 397 } 398 399 struct strlist *build_id_cache__list_all(bool validonly) 400 { 401 struct strlist *toplist, *linklist = NULL, *bidlist; 402 struct str_node *nd, *nd2; 403 char *topdir, *linkdir = NULL; 404 char sbuild_id[SBUILD_ID_SIZE]; 405 406 /* for filename__ functions */ 407 if (validonly) 408 symbol__init(NULL); 409 410 /* Open the top-level directory */ 411 if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0) 412 return NULL; 413 414 bidlist = strlist__new(NULL, NULL); 415 if (!bidlist) 416 goto out; 417 418 toplist = lsdir(topdir, lsdir_bid_head_filter); 419 if (!toplist) { 420 pr_debug("Error in lsdir(%s): %d\n", topdir, errno); 421 /* If there is no buildid cache, return an empty list */ 422 if (errno == ENOENT) 423 goto out; 424 goto err_out; 425 } 426 427 strlist__for_each_entry(nd, toplist) { 428 if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0) 429 goto err_out; 430 /* Open the lower-level directory */ 431 linklist = lsdir(linkdir, lsdir_bid_tail_filter); 432 if (!linklist) { 433 pr_debug("Error in lsdir(%s): %d\n", linkdir, errno); 434 goto err_out; 435 } 436 strlist__for_each_entry(nd2, linklist) { 437 if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s", 438 nd->s, nd2->s) > SBUILD_ID_SIZE - 1) 439 goto err_out; 440 if (validonly && !build_id_cache__valid_id(sbuild_id)) 441 continue; 442 if (strlist__add(bidlist, sbuild_id) < 0) 443 goto err_out; 444 } 445 strlist__delete(linklist); 446 zfree(&linkdir); 447 } 448 449 out_free: 450 strlist__delete(toplist); 451 out: 452 free(topdir); 453 454 return bidlist; 455 456 err_out: 457 strlist__delete(linklist); 458 zfree(&linkdir); 459 strlist__delete(bidlist); 460 bidlist = NULL; 461 goto out_free; 462 } 463 464 static bool str_is_build_id(const char *maybe_sbuild_id, size_t len) 465 { 466 size_t i; 467 468 for (i = 0; i < len; i++) { 469 if (!isxdigit(maybe_sbuild_id[i])) 470 return false; 471 } 472 return true; 473 } 474 475 /* Return the valid complete build-id */ 476 char *build_id_cache__complement(const char *incomplete_sbuild_id) 477 { 478 struct strlist *bidlist; 479 struct str_node *nd, *cand = NULL; 480 char *sbuild_id = NULL; 481 size_t len = strlen(incomplete_sbuild_id); 482 483 if (len >= SBUILD_ID_SIZE || 484 !str_is_build_id(incomplete_sbuild_id, len)) 485 return NULL; 486 487 bidlist = build_id_cache__list_all(true); 488 if (!bidlist) 489 return NULL; 490 491 strlist__for_each_entry(nd, bidlist) { 492 if (strncmp(nd->s, incomplete_sbuild_id, len) != 0) 493 continue; 494 if (cand) { /* Error: There are more than 2 candidates. */ 495 cand = NULL; 496 break; 497 } 498 cand = nd; 499 } 500 if (cand) 501 sbuild_id = strdup(cand->s); 502 strlist__delete(bidlist); 503 504 return sbuild_id; 505 } 506 507 char *build_id_cache__cachedir(const char *sbuild_id, const char *name, 508 struct nsinfo *nsi, bool is_kallsyms, 509 bool is_vdso) 510 { 511 char *realname = NULL, *filename; 512 bool slash = is_kallsyms || is_vdso; 513 514 if (!slash) 515 realname = nsinfo__realpath(name, nsi); 516 517 if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "", 518 is_vdso ? DSO__NAME_VDSO : (realname ? realname : name), 519 sbuild_id ? "/" : "", sbuild_id ?: "") < 0) 520 filename = NULL; 521 522 free(realname); 523 return filename; 524 } 525 526 int build_id_cache__list_build_ids(const char *pathname, struct nsinfo *nsi, 527 struct strlist **result) 528 { 529 char *dir_name; 530 int ret = 0; 531 532 dir_name = build_id_cache__cachedir(NULL, pathname, nsi, false, false); 533 if (!dir_name) 534 return -ENOMEM; 535 536 *result = lsdir(dir_name, lsdir_no_dot_filter); 537 if (!*result) 538 ret = -errno; 539 free(dir_name); 540 541 return ret; 542 } 543 544 #if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT) 545 static int build_id_cache__add_sdt_cache(const char *sbuild_id, 546 const char *realname, 547 struct nsinfo *nsi) 548 { 549 struct probe_cache *cache; 550 int ret; 551 struct nscookie nsc; 552 553 cache = probe_cache__new(sbuild_id, nsi); 554 if (!cache) 555 return -1; 556 557 nsinfo__mountns_enter(nsi, &nsc); 558 ret = probe_cache__scan_sdt(cache, realname); 559 nsinfo__mountns_exit(&nsc); 560 if (ret >= 0) { 561 pr_debug4("Found %d SDTs in %s\n", ret, realname); 562 if (probe_cache__commit(cache) < 0) 563 ret = -1; 564 } 565 probe_cache__delete(cache); 566 return ret; 567 } 568 #else 569 #define build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) (0) 570 #endif 571 572 static char *build_id_cache__find_debug(const char *sbuild_id, 573 struct nsinfo *nsi, 574 const char *root_dir) 575 { 576 const char *dirname = "/usr/lib/debug/.build-id/"; 577 char *realname = NULL; 578 char dirbuf[PATH_MAX]; 579 char *debugfile; 580 struct nscookie nsc; 581 size_t len = 0; 582 583 debugfile = calloc(1, PATH_MAX); 584 if (!debugfile) 585 goto out; 586 587 if (root_dir) { 588 path__join(dirbuf, PATH_MAX, root_dir, dirname); 589 dirname = dirbuf; 590 } 591 592 len = __symbol__join_symfs(debugfile, PATH_MAX, dirname); 593 snprintf(debugfile + len, PATH_MAX - len, "%.2s/%s.debug", sbuild_id, 594 sbuild_id + 2); 595 596 nsinfo__mountns_enter(nsi, &nsc); 597 realname = realpath(debugfile, NULL); 598 if (realname && access(realname, R_OK)) 599 zfree(&realname); 600 nsinfo__mountns_exit(&nsc); 601 602 #ifdef HAVE_DEBUGINFOD_SUPPORT 603 if (realname == NULL) { 604 debuginfod_client* c; 605 606 pr_debug("Downloading debug info with build id %s\n", sbuild_id); 607 608 c = debuginfod_begin(); 609 if (c != NULL) { 610 int fd = debuginfod_find_debuginfo(c, 611 (const unsigned char*)sbuild_id, 0, 612 &realname); 613 if (fd >= 0) 614 close(fd); /* retaining reference by realname */ 615 debuginfod_end(c); 616 } 617 } 618 #endif 619 620 out: 621 free(debugfile); 622 return realname; 623 } 624 625 int 626 build_id_cache__add(const char *sbuild_id, const char *name, const char *realname, 627 struct nsinfo *nsi, bool is_kallsyms, bool is_vdso, 628 const char *proper_name, const char *root_dir) 629 { 630 const size_t size = PATH_MAX; 631 char *filename = NULL, *dir_name = NULL, *linkname = zalloc(size), *tmp; 632 char *debugfile = NULL; 633 int err = -1; 634 635 if (!proper_name) 636 proper_name = name; 637 638 dir_name = build_id_cache__cachedir(sbuild_id, proper_name, nsi, is_kallsyms, 639 is_vdso); 640 if (!dir_name) 641 goto out_free; 642 643 /* Remove old style build-id cache */ 644 if (is_regular_file(dir_name)) 645 if (unlink(dir_name)) 646 goto out_free; 647 648 if (mkdir_p(dir_name, 0755)) 649 goto out_free; 650 651 /* Save the allocated buildid dirname */ 652 if (asprintf(&filename, "%s/%s", dir_name, 653 build_id_cache__basename(is_kallsyms, is_vdso, 654 false)) < 0) { 655 filename = NULL; 656 goto out_free; 657 } 658 659 if (access(filename, F_OK)) { 660 if (is_kallsyms) { 661 if (copyfile("/proc/kallsyms", filename)) 662 goto out_free; 663 } else if (nsi && nsinfo__need_setns(nsi)) { 664 if (copyfile_ns(name, filename, nsi)) 665 goto out_free; 666 } else if (link(realname, filename) && errno != EEXIST) { 667 struct stat f_stat; 668 669 if (!(stat(name, &f_stat) < 0) && 670 copyfile_mode(name, filename, f_stat.st_mode)) 671 goto out_free; 672 } 673 } 674 675 /* Some binaries are stripped, but have .debug files with their symbol 676 * table. Check to see if we can locate one of those, since the elf 677 * file itself may not be very useful to users of our tools without a 678 * symtab. 679 */ 680 if (!is_kallsyms && !is_vdso && 681 strncmp(".ko", name + strlen(name) - 3, 3)) { 682 debugfile = build_id_cache__find_debug(sbuild_id, nsi, root_dir); 683 if (debugfile) { 684 zfree(&filename); 685 if (asprintf(&filename, "%s/%s", dir_name, 686 build_id_cache__basename(false, false, true)) < 0) { 687 filename = NULL; 688 goto out_free; 689 } 690 if (access(filename, F_OK)) { 691 if (nsi && nsinfo__need_setns(nsi)) { 692 if (copyfile_ns(debugfile, filename, 693 nsi)) 694 goto out_free; 695 } else if (link(debugfile, filename) && 696 errno != EEXIST && 697 copyfile(debugfile, filename)) 698 goto out_free; 699 } 700 } 701 } 702 703 if (!build_id_cache__linkname(sbuild_id, linkname, size)) 704 goto out_free; 705 tmp = strrchr(linkname, '/'); 706 *tmp = '\0'; 707 708 if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 709 goto out_free; 710 711 *tmp = '/'; 712 tmp = dir_name + strlen(buildid_dir) - 5; 713 memcpy(tmp, "../..", 5); 714 715 if (symlink(tmp, linkname) == 0) { 716 err = 0; 717 } else if (errno == EEXIST) { 718 char path[PATH_MAX]; 719 ssize_t len; 720 721 len = readlink(linkname, path, sizeof(path) - 1); 722 if (len <= 0) { 723 pr_err("Can't read link: %s\n", linkname); 724 goto out_free; 725 } 726 path[len] = '\0'; 727 728 if (strcmp(tmp, path)) { 729 pr_debug("build <%s> already linked to %s\n", 730 sbuild_id, linkname); 731 } 732 err = 0; 733 } 734 735 /* Update SDT cache : error is just warned */ 736 if (realname && 737 build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) < 0) 738 pr_debug4("Failed to update/scan SDT cache for %s\n", realname); 739 740 out_free: 741 free(filename); 742 free(debugfile); 743 free(dir_name); 744 free(linkname); 745 return err; 746 } 747 748 int __build_id_cache__add_s(const char *sbuild_id, const char *name, 749 struct nsinfo *nsi, bool is_kallsyms, bool is_vdso, 750 const char *proper_name, const char *root_dir) 751 { 752 char *realname = NULL; 753 int err = -1; 754 755 if (!is_kallsyms) { 756 if (!is_vdso) 757 realname = nsinfo__realpath(name, nsi); 758 else 759 realname = realpath(name, NULL); 760 if (!realname) 761 goto out_free; 762 } 763 764 err = build_id_cache__add(sbuild_id, name, realname, nsi, 765 is_kallsyms, is_vdso, proper_name, root_dir); 766 out_free: 767 if (!is_kallsyms) 768 free(realname); 769 return err; 770 } 771 772 static int build_id_cache__add_b(const struct build_id *bid, 773 const char *name, struct nsinfo *nsi, 774 bool is_kallsyms, bool is_vdso, 775 const char *proper_name, 776 const char *root_dir) 777 { 778 char sbuild_id[SBUILD_ID_SIZE]; 779 780 build_id__snprintf(bid, sbuild_id, sizeof(sbuild_id)); 781 782 return __build_id_cache__add_s(sbuild_id, name, nsi, is_kallsyms, 783 is_vdso, proper_name, root_dir); 784 } 785 786 bool build_id_cache__cached(const char *sbuild_id) 787 { 788 bool ret = false; 789 char *filename = build_id_cache__linkname(sbuild_id, NULL, 0); 790 791 if (filename && !access(filename, F_OK)) 792 ret = true; 793 free(filename); 794 795 return ret; 796 } 797 798 int build_id_cache__remove_s(const char *sbuild_id) 799 { 800 const size_t size = PATH_MAX; 801 char *filename = zalloc(size), 802 *linkname = zalloc(size), *tmp; 803 int err = -1; 804 805 if (filename == NULL || linkname == NULL) 806 goto out_free; 807 808 if (!build_id_cache__linkname(sbuild_id, linkname, size)) 809 goto out_free; 810 811 if (access(linkname, F_OK)) 812 goto out_free; 813 814 if (readlink(linkname, filename, size - 1) < 0) 815 goto out_free; 816 817 if (unlink(linkname)) 818 goto out_free; 819 820 /* 821 * Since the link is relative, we must make it absolute: 822 */ 823 tmp = strrchr(linkname, '/') + 1; 824 snprintf(tmp, size - (tmp - linkname), "%s", filename); 825 826 if (rm_rf(linkname)) 827 goto out_free; 828 829 err = 0; 830 out_free: 831 free(filename); 832 free(linkname); 833 return err; 834 } 835 836 static int filename__read_build_id_ns(const char *filename, 837 struct build_id *bid, 838 struct nsinfo *nsi) 839 { 840 struct nscookie nsc; 841 int ret; 842 843 nsinfo__mountns_enter(nsi, &nsc); 844 ret = filename__read_build_id(filename, bid, /*block=*/true); 845 nsinfo__mountns_exit(&nsc); 846 847 return ret; 848 } 849 850 static bool dso__build_id_mismatch(struct dso *dso, const char *name) 851 { 852 struct build_id bid = { .size = 0, }; 853 bool ret = false; 854 855 mutex_lock(dso__lock(dso)); 856 if (filename__read_build_id_ns(name, &bid, dso__nsinfo(dso)) >= 0) 857 ret = !dso__build_id_equal(dso, &bid); 858 859 mutex_unlock(dso__lock(dso)); 860 861 return ret; 862 } 863 864 static int dso__cache_build_id(struct dso *dso, struct machine *machine, 865 void *priv __maybe_unused) 866 { 867 bool is_kallsyms = dso__is_kallsyms(dso); 868 bool is_vdso = dso__is_vdso(dso); 869 const char *name = dso__long_name(dso); 870 const char *proper_name = NULL; 871 const char *root_dir = NULL; 872 char *allocated_name = NULL; 873 int ret = 0; 874 875 if (!dso__has_build_id(dso) || !dso__hit(dso)) 876 return 0; 877 878 if (dso__is_kcore(dso)) { 879 is_kallsyms = true; 880 name = machine->mmap_name; 881 } 882 883 if (!machine__is_host(machine)) { 884 if (*machine->root_dir) { 885 root_dir = machine->root_dir; 886 ret = asprintf(&allocated_name, "%s/%s", root_dir, name); 887 if (ret < 0) 888 return ret; 889 proper_name = name; 890 name = allocated_name; 891 } else if (is_kallsyms) { 892 /* Cannot get guest kallsyms */ 893 return 0; 894 } 895 } 896 897 if (!is_kallsyms && dso__build_id_mismatch(dso, name)) 898 goto out_free; 899 900 mutex_lock(dso__lock(dso)); 901 ret = build_id_cache__add_b(dso__bid(dso), name, dso__nsinfo(dso), 902 is_kallsyms, is_vdso, proper_name, root_dir); 903 mutex_unlock(dso__lock(dso)); 904 out_free: 905 free(allocated_name); 906 return ret; 907 } 908 909 static int 910 machines__for_each_dso(struct machines *machines, machine__dso_t fn, void *priv) 911 { 912 int ret = machine__for_each_dso(&machines->host, fn, priv); 913 struct rb_node *nd; 914 915 for (nd = rb_first_cached(&machines->guests); nd; 916 nd = rb_next(nd)) { 917 struct machine *pos = rb_entry(nd, struct machine, rb_node); 918 919 ret |= machine__for_each_dso(pos, fn, priv); 920 } 921 return ret ? -1 : 0; 922 } 923 924 int __perf_session__cache_build_ids(struct perf_session *session, 925 machine__dso_t fn, void *priv) 926 { 927 if (no_buildid_cache) 928 return 0; 929 930 if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST) 931 return -1; 932 933 return machines__for_each_dso(&session->machines, fn, priv) ? -1 : 0; 934 } 935 936 int perf_session__cache_build_ids(struct perf_session *session) 937 { 938 return __perf_session__cache_build_ids(session, dso__cache_build_id, NULL); 939 } 940 941 static bool machine__read_build_ids(struct machine *machine, bool with_hits) 942 { 943 return dsos__read_build_ids(&machine->dsos, with_hits); 944 } 945 946 bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) 947 { 948 struct rb_node *nd; 949 bool ret = machine__read_build_ids(&session->machines.host, with_hits); 950 951 for (nd = rb_first_cached(&session->machines.guests); nd; 952 nd = rb_next(nd)) { 953 struct machine *pos = rb_entry(nd, struct machine, rb_node); 954 ret |= machine__read_build_ids(pos, with_hits); 955 } 956 957 return ret; 958 } 959 960 void build_id__init(struct build_id *bid, const u8 *data, size_t size) 961 { 962 if (size > BUILD_ID_SIZE) { 963 pr_debug("Truncating build_id size from %zd\n", size); 964 size = BUILD_ID_SIZE; 965 } 966 memcpy(bid->data, data, size); 967 bid->size = size; 968 } 969 970 bool build_id__is_defined(const struct build_id *bid) 971 { 972 return bid && bid->size ? !!memchr_inv(bid->data, 0, bid->size) : false; 973 } 974