1 /* 2 * build-id.c 3 * 4 * build-id support 5 * 6 * Copyright (C) 2009, 2010 Red Hat Inc. 7 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com> 8 */ 9 #include "util.h" 10 #include <stdio.h> 11 #include "build-id.h" 12 #include "event.h" 13 #include "symbol.h" 14 #include <linux/kernel.h> 15 #include "debug.h" 16 #include "session.h" 17 #include "tool.h" 18 #include "header.h" 19 #include "vdso.h" 20 #include "probe-file.h" 21 22 23 static bool no_buildid_cache; 24 25 int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused, 26 union perf_event *event, 27 struct perf_sample *sample, 28 struct perf_evsel *evsel __maybe_unused, 29 struct machine *machine) 30 { 31 struct addr_location al; 32 struct thread *thread = machine__findnew_thread(machine, sample->pid, 33 sample->tid); 34 35 if (thread == NULL) { 36 pr_err("problem processing %d event, skipping it.\n", 37 event->header.type); 38 return -1; 39 } 40 41 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al); 42 43 if (al.map != NULL) 44 al.map->dso->hit = 1; 45 46 thread__put(thread); 47 return 0; 48 } 49 50 static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused, 51 union perf_event *event, 52 struct perf_sample *sample 53 __maybe_unused, 54 struct machine *machine) 55 { 56 struct thread *thread = machine__findnew_thread(machine, 57 event->fork.pid, 58 event->fork.tid); 59 60 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid, 61 event->fork.ppid, event->fork.ptid); 62 63 if (thread) { 64 machine__remove_thread(machine, thread); 65 thread__put(thread); 66 } 67 68 return 0; 69 } 70 71 struct perf_tool build_id__mark_dso_hit_ops = { 72 .sample = build_id__mark_dso_hit, 73 .mmap = perf_event__process_mmap, 74 .mmap2 = perf_event__process_mmap2, 75 .fork = perf_event__process_fork, 76 .exit = perf_event__exit_del_thread, 77 .attr = perf_event__process_attr, 78 .build_id = perf_event__process_build_id, 79 .ordered_events = true, 80 }; 81 82 int build_id__sprintf(const u8 *build_id, int len, char *bf) 83 { 84 char *bid = bf; 85 const u8 *raw = build_id; 86 int i; 87 88 for (i = 0; i < len; ++i) { 89 sprintf(bid, "%02x", *raw); 90 ++raw; 91 bid += 2; 92 } 93 94 return (bid - bf) + 1; 95 } 96 97 int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id) 98 { 99 char notes[PATH_MAX]; 100 u8 build_id[BUILD_ID_SIZE]; 101 int ret; 102 103 if (!root_dir) 104 root_dir = ""; 105 106 scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir); 107 108 ret = sysfs__read_build_id(notes, build_id, sizeof(build_id)); 109 if (ret < 0) 110 return ret; 111 112 return build_id__sprintf(build_id, sizeof(build_id), sbuild_id); 113 } 114 115 int filename__sprintf_build_id(const char *pathname, char *sbuild_id) 116 { 117 u8 build_id[BUILD_ID_SIZE]; 118 int ret; 119 120 ret = filename__read_build_id(pathname, build_id, sizeof(build_id)); 121 if (ret < 0) 122 return ret; 123 else if (ret != sizeof(build_id)) 124 return -EINVAL; 125 126 return build_id__sprintf(build_id, sizeof(build_id), sbuild_id); 127 } 128 129 /* asnprintf consolidates asprintf and snprintf */ 130 static int asnprintf(char **strp, size_t size, const char *fmt, ...) 131 { 132 va_list ap; 133 int ret; 134 135 if (!strp) 136 return -EINVAL; 137 138 va_start(ap, fmt); 139 if (*strp) 140 ret = vsnprintf(*strp, size, fmt, ap); 141 else 142 ret = vasprintf(strp, fmt, ap); 143 va_end(ap); 144 145 return ret; 146 } 147 148 char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf, 149 size_t size) 150 { 151 bool retry_old = true; 152 153 snprintf(bf, size, "%s/%s/%s/kallsyms", 154 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); 155 retry: 156 if (!access(bf, F_OK)) 157 return bf; 158 if (retry_old) { 159 /* Try old style kallsyms cache */ 160 snprintf(bf, size, "%s/%s/%s", 161 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); 162 retry_old = false; 163 goto retry; 164 } 165 166 return NULL; 167 } 168 169 char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size) 170 { 171 char *tmp = bf; 172 int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir, 173 sbuild_id, sbuild_id + 2); 174 if (ret < 0 || (tmp && size < (unsigned int)ret)) 175 return NULL; 176 return bf; 177 } 178 179 char *build_id_cache__origname(const char *sbuild_id) 180 { 181 char *linkname; 182 char buf[PATH_MAX]; 183 char *ret = NULL, *p; 184 size_t offs = 5; /* == strlen("../..") */ 185 186 linkname = build_id_cache__linkname(sbuild_id, NULL, 0); 187 if (!linkname) 188 return NULL; 189 190 if (readlink(linkname, buf, PATH_MAX) < 0) 191 goto out; 192 /* The link should be "../..<origpath>/<sbuild_id>" */ 193 p = strrchr(buf, '/'); /* Cut off the "/<sbuild_id>" */ 194 if (p && (p > buf + offs)) { 195 *p = '\0'; 196 if (buf[offs + 1] == '[') 197 offs++; /* 198 * This is a DSO name, like [kernel.kallsyms]. 199 * Skip the first '/', since this is not the 200 * cache of a regular file. 201 */ 202 ret = strdup(buf + offs); /* Skip "../..[/]" */ 203 } 204 out: 205 free(linkname); 206 return ret; 207 } 208 209 static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso) 210 { 211 return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : "elf"); 212 } 213 214 char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size) 215 { 216 bool is_kallsyms = dso__is_kallsyms((struct dso *)dso); 217 bool is_vdso = dso__is_vdso((struct dso *)dso); 218 char sbuild_id[SBUILD_ID_SIZE]; 219 char *linkname; 220 bool alloc = (bf == NULL); 221 int ret; 222 223 if (!dso->has_build_id) 224 return NULL; 225 226 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); 227 linkname = build_id_cache__linkname(sbuild_id, NULL, 0); 228 if (!linkname) 229 return NULL; 230 231 /* Check if old style build_id cache */ 232 if (is_regular_file(linkname)) 233 ret = asnprintf(&bf, size, "%s", linkname); 234 else 235 ret = asnprintf(&bf, size, "%s/%s", linkname, 236 build_id_cache__basename(is_kallsyms, is_vdso)); 237 if (ret < 0 || (!alloc && size < (unsigned int)ret)) 238 bf = NULL; 239 free(linkname); 240 241 return bf; 242 } 243 244 bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size) 245 { 246 char *id_name = NULL, *ch; 247 struct stat sb; 248 char sbuild_id[SBUILD_ID_SIZE]; 249 250 if (!dso->has_build_id) 251 goto err; 252 253 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); 254 id_name = build_id_cache__linkname(sbuild_id, NULL, 0); 255 if (!id_name) 256 goto err; 257 if (access(id_name, F_OK)) 258 goto err; 259 if (lstat(id_name, &sb) == -1) 260 goto err; 261 if ((size_t)sb.st_size > size - 1) 262 goto err; 263 if (readlink(id_name, bf, size - 1) < 0) 264 goto err; 265 266 bf[sb.st_size] = '\0'; 267 268 /* 269 * link should be: 270 * ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92 271 */ 272 ch = strrchr(bf, '/'); 273 if (!ch) 274 goto err; 275 if (ch - 3 < bf) 276 goto err; 277 278 free(id_name); 279 return strncmp(".ko", ch - 3, 3) == 0; 280 err: 281 pr_err("Invalid build id: %s\n", id_name ? : 282 dso->long_name ? : 283 dso->short_name ? : 284 "[unknown]"); 285 free(id_name); 286 return false; 287 } 288 289 #define dsos__for_each_with_build_id(pos, head) \ 290 list_for_each_entry(pos, head, node) \ 291 if (!pos->has_build_id) \ 292 continue; \ 293 else 294 295 static int write_buildid(const char *name, size_t name_len, u8 *build_id, 296 pid_t pid, u16 misc, int fd) 297 { 298 int err; 299 struct build_id_event b; 300 size_t len; 301 302 len = name_len + 1; 303 len = PERF_ALIGN(len, NAME_ALIGN); 304 305 memset(&b, 0, sizeof(b)); 306 memcpy(&b.build_id, build_id, BUILD_ID_SIZE); 307 b.pid = pid; 308 b.header.misc = misc; 309 b.header.size = sizeof(b) + len; 310 311 err = writen(fd, &b, sizeof(b)); 312 if (err < 0) 313 return err; 314 315 return write_padded(fd, name, name_len + 1, len); 316 } 317 318 static int machine__write_buildid_table(struct machine *machine, int fd) 319 { 320 int err = 0; 321 char nm[PATH_MAX]; 322 struct dso *pos; 323 u16 kmisc = PERF_RECORD_MISC_KERNEL, 324 umisc = PERF_RECORD_MISC_USER; 325 326 if (!machine__is_host(machine)) { 327 kmisc = PERF_RECORD_MISC_GUEST_KERNEL; 328 umisc = PERF_RECORD_MISC_GUEST_USER; 329 } 330 331 dsos__for_each_with_build_id(pos, &machine->dsos.head) { 332 const char *name; 333 size_t name_len; 334 bool in_kernel = false; 335 336 if (!pos->hit && !dso__is_vdso(pos)) 337 continue; 338 339 if (dso__is_vdso(pos)) { 340 name = pos->short_name; 341 name_len = pos->short_name_len; 342 } else if (dso__is_kcore(pos)) { 343 machine__mmap_name(machine, nm, sizeof(nm)); 344 name = nm; 345 name_len = strlen(nm); 346 } else { 347 name = pos->long_name; 348 name_len = pos->long_name_len; 349 } 350 351 in_kernel = pos->kernel || 352 is_kernel_module(name, 353 PERF_RECORD_MISC_CPUMODE_UNKNOWN); 354 err = write_buildid(name, name_len, pos->build_id, machine->pid, 355 in_kernel ? kmisc : umisc, fd); 356 if (err) 357 break; 358 } 359 360 return err; 361 } 362 363 int perf_session__write_buildid_table(struct perf_session *session, int fd) 364 { 365 struct rb_node *nd; 366 int err = machine__write_buildid_table(&session->machines.host, fd); 367 368 if (err) 369 return err; 370 371 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 372 struct machine *pos = rb_entry(nd, struct machine, rb_node); 373 err = machine__write_buildid_table(pos, fd); 374 if (err) 375 break; 376 } 377 return err; 378 } 379 380 static int __dsos__hit_all(struct list_head *head) 381 { 382 struct dso *pos; 383 384 list_for_each_entry(pos, head, node) 385 pos->hit = true; 386 387 return 0; 388 } 389 390 static int machine__hit_all_dsos(struct machine *machine) 391 { 392 return __dsos__hit_all(&machine->dsos.head); 393 } 394 395 int dsos__hit_all(struct perf_session *session) 396 { 397 struct rb_node *nd; 398 int err; 399 400 err = machine__hit_all_dsos(&session->machines.host); 401 if (err) 402 return err; 403 404 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 405 struct machine *pos = rb_entry(nd, struct machine, rb_node); 406 407 err = machine__hit_all_dsos(pos); 408 if (err) 409 return err; 410 } 411 412 return 0; 413 } 414 415 void disable_buildid_cache(void) 416 { 417 no_buildid_cache = true; 418 } 419 420 static bool lsdir_bid_head_filter(const char *name __maybe_unused, 421 struct dirent *d __maybe_unused) 422 { 423 return (strlen(d->d_name) == 2) && 424 isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]); 425 } 426 427 static bool lsdir_bid_tail_filter(const char *name __maybe_unused, 428 struct dirent *d __maybe_unused) 429 { 430 int i = 0; 431 while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3) 432 i++; 433 return (i == SBUILD_ID_SIZE - 3) && (d->d_name[i] == '\0'); 434 } 435 436 struct strlist *build_id_cache__list_all(void) 437 { 438 struct strlist *toplist, *linklist = NULL, *bidlist; 439 struct str_node *nd, *nd2; 440 char *topdir, *linkdir = NULL; 441 char sbuild_id[SBUILD_ID_SIZE]; 442 443 /* Open the top-level directory */ 444 if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0) 445 return NULL; 446 447 bidlist = strlist__new(NULL, NULL); 448 if (!bidlist) 449 goto out; 450 451 toplist = lsdir(topdir, lsdir_bid_head_filter); 452 if (!toplist) { 453 pr_debug("Error in lsdir(%s): %d\n", topdir, errno); 454 /* If there is no buildid cache, return an empty list */ 455 if (errno == ENOENT) 456 goto out; 457 goto err_out; 458 } 459 460 strlist__for_each_entry(nd, toplist) { 461 if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0) 462 goto err_out; 463 /* Open the lower-level directory */ 464 linklist = lsdir(linkdir, lsdir_bid_tail_filter); 465 if (!linklist) { 466 pr_debug("Error in lsdir(%s): %d\n", linkdir, errno); 467 goto err_out; 468 } 469 strlist__for_each_entry(nd2, linklist) { 470 if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s", 471 nd->s, nd2->s) != SBUILD_ID_SIZE - 1) 472 goto err_out; 473 if (strlist__add(bidlist, sbuild_id) < 0) 474 goto err_out; 475 } 476 strlist__delete(linklist); 477 zfree(&linkdir); 478 } 479 480 out_free: 481 strlist__delete(toplist); 482 out: 483 free(topdir); 484 485 return bidlist; 486 487 err_out: 488 strlist__delete(linklist); 489 zfree(&linkdir); 490 strlist__delete(bidlist); 491 bidlist = NULL; 492 goto out_free; 493 } 494 495 char *build_id_cache__cachedir(const char *sbuild_id, const char *name, 496 bool is_kallsyms, bool is_vdso) 497 { 498 char *realname = (char *)name, *filename; 499 bool slash = is_kallsyms || is_vdso; 500 501 if (!slash) { 502 realname = realpath(name, NULL); 503 if (!realname) 504 return NULL; 505 } 506 507 if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "", 508 is_vdso ? DSO__NAME_VDSO : realname, 509 sbuild_id ? "/" : "", sbuild_id ?: "") < 0) 510 filename = NULL; 511 512 if (!slash) 513 free(realname); 514 515 return filename; 516 } 517 518 int build_id_cache__list_build_ids(const char *pathname, 519 struct strlist **result) 520 { 521 char *dir_name; 522 int ret = 0; 523 524 dir_name = build_id_cache__cachedir(NULL, pathname, false, false); 525 if (!dir_name) 526 return -ENOMEM; 527 528 *result = lsdir(dir_name, lsdir_no_dot_filter); 529 if (!*result) 530 ret = -errno; 531 free(dir_name); 532 533 return ret; 534 } 535 536 #ifdef HAVE_LIBELF_SUPPORT 537 static int build_id_cache__add_sdt_cache(const char *sbuild_id, 538 const char *realname) 539 { 540 struct probe_cache *cache; 541 int ret; 542 543 cache = probe_cache__new(sbuild_id); 544 if (!cache) 545 return -1; 546 547 ret = probe_cache__scan_sdt(cache, realname); 548 if (ret >= 0) { 549 pr_debug("Found %d SDTs in %s\n", ret, realname); 550 if (probe_cache__commit(cache) < 0) 551 ret = -1; 552 } 553 probe_cache__delete(cache); 554 return ret; 555 } 556 #else 557 #define build_id_cache__add_sdt_cache(sbuild_id, realname) (0) 558 #endif 559 560 int build_id_cache__add_s(const char *sbuild_id, const char *name, 561 bool is_kallsyms, bool is_vdso) 562 { 563 const size_t size = PATH_MAX; 564 char *realname = NULL, *filename = NULL, *dir_name = NULL, 565 *linkname = zalloc(size), *tmp; 566 int err = -1; 567 568 if (!is_kallsyms) { 569 realname = realpath(name, NULL); 570 if (!realname) 571 goto out_free; 572 } 573 574 dir_name = build_id_cache__cachedir(sbuild_id, name, 575 is_kallsyms, is_vdso); 576 if (!dir_name) 577 goto out_free; 578 579 /* Remove old style build-id cache */ 580 if (is_regular_file(dir_name)) 581 if (unlink(dir_name)) 582 goto out_free; 583 584 if (mkdir_p(dir_name, 0755)) 585 goto out_free; 586 587 /* Save the allocated buildid dirname */ 588 if (asprintf(&filename, "%s/%s", dir_name, 589 build_id_cache__basename(is_kallsyms, is_vdso)) < 0) { 590 filename = NULL; 591 goto out_free; 592 } 593 594 if (access(filename, F_OK)) { 595 if (is_kallsyms) { 596 if (copyfile("/proc/kallsyms", filename)) 597 goto out_free; 598 } else if (link(realname, filename) && errno != EEXIST && 599 copyfile(name, filename)) 600 goto out_free; 601 } 602 603 if (!build_id_cache__linkname(sbuild_id, linkname, size)) 604 goto out_free; 605 tmp = strrchr(linkname, '/'); 606 *tmp = '\0'; 607 608 if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 609 goto out_free; 610 611 *tmp = '/'; 612 tmp = dir_name + strlen(buildid_dir) - 5; 613 memcpy(tmp, "../..", 5); 614 615 if (symlink(tmp, linkname) == 0) 616 err = 0; 617 618 /* Update SDT cache : error is just warned */ 619 if (build_id_cache__add_sdt_cache(sbuild_id, realname) < 0) 620 pr_debug("Failed to update/scan SDT cache for %s\n", realname); 621 622 out_free: 623 if (!is_kallsyms) 624 free(realname); 625 free(filename); 626 free(dir_name); 627 free(linkname); 628 return err; 629 } 630 631 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, 632 const char *name, bool is_kallsyms, 633 bool is_vdso) 634 { 635 char sbuild_id[SBUILD_ID_SIZE]; 636 637 build_id__sprintf(build_id, build_id_size, sbuild_id); 638 639 return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso); 640 } 641 642 bool build_id_cache__cached(const char *sbuild_id) 643 { 644 bool ret = false; 645 char *filename = build_id_cache__linkname(sbuild_id, NULL, 0); 646 647 if (filename && !access(filename, F_OK)) 648 ret = true; 649 free(filename); 650 651 return ret; 652 } 653 654 int build_id_cache__remove_s(const char *sbuild_id) 655 { 656 const size_t size = PATH_MAX; 657 char *filename = zalloc(size), 658 *linkname = zalloc(size), *tmp; 659 int err = -1; 660 661 if (filename == NULL || linkname == NULL) 662 goto out_free; 663 664 if (!build_id_cache__linkname(sbuild_id, linkname, size)) 665 goto out_free; 666 667 if (access(linkname, F_OK)) 668 goto out_free; 669 670 if (readlink(linkname, filename, size - 1) < 0) 671 goto out_free; 672 673 if (unlink(linkname)) 674 goto out_free; 675 676 /* 677 * Since the link is relative, we must make it absolute: 678 */ 679 tmp = strrchr(linkname, '/') + 1; 680 snprintf(tmp, size - (tmp - linkname), "%s", filename); 681 682 if (rm_rf(linkname)) 683 goto out_free; 684 685 err = 0; 686 out_free: 687 free(filename); 688 free(linkname); 689 return err; 690 } 691 692 static int dso__cache_build_id(struct dso *dso, struct machine *machine) 693 { 694 bool is_kallsyms = dso__is_kallsyms(dso); 695 bool is_vdso = dso__is_vdso(dso); 696 const char *name = dso->long_name; 697 char nm[PATH_MAX]; 698 699 if (dso__is_kcore(dso)) { 700 is_kallsyms = true; 701 machine__mmap_name(machine, nm, sizeof(nm)); 702 name = nm; 703 } 704 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name, 705 is_kallsyms, is_vdso); 706 } 707 708 static int __dsos__cache_build_ids(struct list_head *head, 709 struct machine *machine) 710 { 711 struct dso *pos; 712 int err = 0; 713 714 dsos__for_each_with_build_id(pos, head) 715 if (dso__cache_build_id(pos, machine)) 716 err = -1; 717 718 return err; 719 } 720 721 static int machine__cache_build_ids(struct machine *machine) 722 { 723 return __dsos__cache_build_ids(&machine->dsos.head, machine); 724 } 725 726 int perf_session__cache_build_ids(struct perf_session *session) 727 { 728 struct rb_node *nd; 729 int ret; 730 731 if (no_buildid_cache) 732 return 0; 733 734 if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST) 735 return -1; 736 737 ret = machine__cache_build_ids(&session->machines.host); 738 739 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 740 struct machine *pos = rb_entry(nd, struct machine, rb_node); 741 ret |= machine__cache_build_ids(pos); 742 } 743 return ret ? -1 : 0; 744 } 745 746 static bool machine__read_build_ids(struct machine *machine, bool with_hits) 747 { 748 return __dsos__read_build_ids(&machine->dsos.head, with_hits); 749 } 750 751 bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) 752 { 753 struct rb_node *nd; 754 bool ret = machine__read_build_ids(&session->machines.host, with_hits); 755 756 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 757 struct machine *pos = rb_entry(nd, struct machine, rb_node); 758 ret |= machine__read_build_ids(pos, with_hits); 759 } 760 761 return ret; 762 } 763