1 #include "util.h" 2 #include <sys/types.h> 3 #include <byteswap.h> 4 #include <unistd.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <linux/list.h> 8 #include <linux/kernel.h> 9 #include <linux/bitops.h> 10 #include <sys/utsname.h> 11 12 #include "evlist.h" 13 #include "evsel.h" 14 #include "header.h" 15 #include "../perf.h" 16 #include "trace-event.h" 17 #include "session.h" 18 #include "symbol.h" 19 #include "debug.h" 20 #include "cpumap.h" 21 #include "pmu.h" 22 #include "vdso.h" 23 #include "strbuf.h" 24 #include "build-id.h" 25 #include "data.h" 26 27 static bool no_buildid_cache = false; 28 29 static u32 header_argc; 30 static const char **header_argv; 31 32 /* 33 * magic2 = "PERFILE2" 34 * must be a numerical value to let the endianness 35 * determine the memory layout. That way we are able 36 * to detect endianness when reading the perf.data file 37 * back. 38 * 39 * we check for legacy (PERFFILE) format. 40 */ 41 static const char *__perf_magic1 = "PERFFILE"; 42 static const u64 __perf_magic2 = 0x32454c4946524550ULL; 43 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; 44 45 #define PERF_MAGIC __perf_magic2 46 47 struct perf_file_attr { 48 struct perf_event_attr attr; 49 struct perf_file_section ids; 50 }; 51 52 void perf_header__set_feat(struct perf_header *header, int feat) 53 { 54 set_bit(feat, header->adds_features); 55 } 56 57 void perf_header__clear_feat(struct perf_header *header, int feat) 58 { 59 clear_bit(feat, header->adds_features); 60 } 61 62 bool perf_header__has_feat(const struct perf_header *header, int feat) 63 { 64 return test_bit(feat, header->adds_features); 65 } 66 67 static int do_write(int fd, const void *buf, size_t size) 68 { 69 while (size) { 70 int ret = write(fd, buf, size); 71 72 if (ret < 0) 73 return -errno; 74 75 size -= ret; 76 buf += ret; 77 } 78 79 return 0; 80 } 81 82 #define NAME_ALIGN 64 83 84 static int write_padded(int fd, const void *bf, size_t count, 85 size_t count_aligned) 86 { 87 static const char zero_buf[NAME_ALIGN]; 88 int err = do_write(fd, bf, count); 89 90 if (!err) 91 err = do_write(fd, zero_buf, count_aligned - count); 92 93 return err; 94 } 95 96 static int do_write_string(int fd, const char *str) 97 { 98 u32 len, olen; 99 int ret; 100 101 olen = strlen(str) + 1; 102 len = PERF_ALIGN(olen, NAME_ALIGN); 103 104 /* write len, incl. \0 */ 105 ret = do_write(fd, &len, sizeof(len)); 106 if (ret < 0) 107 return ret; 108 109 return write_padded(fd, str, olen, len); 110 } 111 112 static char *do_read_string(int fd, struct perf_header *ph) 113 { 114 ssize_t sz, ret; 115 u32 len; 116 char *buf; 117 118 sz = readn(fd, &len, sizeof(len)); 119 if (sz < (ssize_t)sizeof(len)) 120 return NULL; 121 122 if (ph->needs_swap) 123 len = bswap_32(len); 124 125 buf = malloc(len); 126 if (!buf) 127 return NULL; 128 129 ret = readn(fd, buf, len); 130 if (ret == (ssize_t)len) { 131 /* 132 * strings are padded by zeroes 133 * thus the actual strlen of buf 134 * may be less than len 135 */ 136 return buf; 137 } 138 139 free(buf); 140 return NULL; 141 } 142 143 int 144 perf_header__set_cmdline(int argc, const char **argv) 145 { 146 int i; 147 148 /* 149 * If header_argv has already been set, do not override it. 150 * This allows a command to set the cmdline, parse args and 151 * then call another builtin function that implements a 152 * command -- e.g, cmd_kvm calling cmd_record. 153 */ 154 if (header_argv) 155 return 0; 156 157 header_argc = (u32)argc; 158 159 /* do not include NULL termination */ 160 header_argv = calloc(argc, sizeof(char *)); 161 if (!header_argv) 162 return -ENOMEM; 163 164 /* 165 * must copy argv contents because it gets moved 166 * around during option parsing 167 */ 168 for (i = 0; i < argc ; i++) 169 header_argv[i] = argv[i]; 170 171 return 0; 172 } 173 174 #define dsos__for_each_with_build_id(pos, head) \ 175 list_for_each_entry(pos, head, node) \ 176 if (!pos->has_build_id) \ 177 continue; \ 178 else 179 180 static int write_buildid(const char *name, size_t name_len, u8 *build_id, 181 pid_t pid, u16 misc, int fd) 182 { 183 int err; 184 struct build_id_event b; 185 size_t len; 186 187 len = name_len + 1; 188 len = PERF_ALIGN(len, NAME_ALIGN); 189 190 memset(&b, 0, sizeof(b)); 191 memcpy(&b.build_id, build_id, BUILD_ID_SIZE); 192 b.pid = pid; 193 b.header.misc = misc; 194 b.header.size = sizeof(b) + len; 195 196 err = do_write(fd, &b, sizeof(b)); 197 if (err < 0) 198 return err; 199 200 return write_padded(fd, name, name_len + 1, len); 201 } 202 203 static int __dsos__hit_all(struct list_head *head) 204 { 205 struct dso *pos; 206 207 list_for_each_entry(pos, head, node) 208 pos->hit = true; 209 210 return 0; 211 } 212 213 static int machine__hit_all_dsos(struct machine *machine) 214 { 215 int err; 216 217 err = __dsos__hit_all(&machine->kernel_dsos); 218 if (err) 219 return err; 220 221 return __dsos__hit_all(&machine->user_dsos); 222 } 223 224 int dsos__hit_all(struct perf_session *session) 225 { 226 struct rb_node *nd; 227 int err; 228 229 err = machine__hit_all_dsos(&session->machines.host); 230 if (err) 231 return err; 232 233 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 234 struct machine *pos = rb_entry(nd, struct machine, rb_node); 235 236 err = machine__hit_all_dsos(pos); 237 if (err) 238 return err; 239 } 240 241 return 0; 242 } 243 244 static int __dsos__write_buildid_table(struct list_head *head, 245 struct machine *machine, 246 pid_t pid, u16 misc, int fd) 247 { 248 char nm[PATH_MAX]; 249 struct dso *pos; 250 251 dsos__for_each_with_build_id(pos, head) { 252 int err; 253 const char *name; 254 size_t name_len; 255 256 if (!pos->hit) 257 continue; 258 259 if (dso__is_vdso(pos)) { 260 name = pos->short_name; 261 name_len = pos->short_name_len + 1; 262 } else if (dso__is_kcore(pos)) { 263 machine__mmap_name(machine, nm, sizeof(nm)); 264 name = nm; 265 name_len = strlen(nm) + 1; 266 } else { 267 name = pos->long_name; 268 name_len = pos->long_name_len + 1; 269 } 270 271 err = write_buildid(name, name_len, pos->build_id, 272 pid, misc, fd); 273 if (err) 274 return err; 275 } 276 277 return 0; 278 } 279 280 static int machine__write_buildid_table(struct machine *machine, int fd) 281 { 282 int err; 283 u16 kmisc = PERF_RECORD_MISC_KERNEL, 284 umisc = PERF_RECORD_MISC_USER; 285 286 if (!machine__is_host(machine)) { 287 kmisc = PERF_RECORD_MISC_GUEST_KERNEL; 288 umisc = PERF_RECORD_MISC_GUEST_USER; 289 } 290 291 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine, 292 machine->pid, kmisc, fd); 293 if (err == 0) 294 err = __dsos__write_buildid_table(&machine->user_dsos, machine, 295 machine->pid, umisc, fd); 296 return err; 297 } 298 299 static int dsos__write_buildid_table(struct perf_header *header, int fd) 300 { 301 struct perf_session *session = container_of(header, 302 struct perf_session, header); 303 struct rb_node *nd; 304 int err = machine__write_buildid_table(&session->machines.host, fd); 305 306 if (err) 307 return err; 308 309 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 310 struct machine *pos = rb_entry(nd, struct machine, rb_node); 311 err = machine__write_buildid_table(pos, fd); 312 if (err) 313 break; 314 } 315 return err; 316 } 317 318 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, 319 const char *name, bool is_kallsyms, bool is_vdso) 320 { 321 const size_t size = PATH_MAX; 322 char *realname, *filename = zalloc(size), 323 *linkname = zalloc(size), *targetname; 324 int len, err = -1; 325 bool slash = is_kallsyms || is_vdso; 326 327 if (is_kallsyms) { 328 if (symbol_conf.kptr_restrict) { 329 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); 330 err = 0; 331 goto out_free; 332 } 333 realname = (char *) name; 334 } else 335 realname = realpath(name, NULL); 336 337 if (realname == NULL || filename == NULL || linkname == NULL) 338 goto out_free; 339 340 len = scnprintf(filename, size, "%s%s%s", 341 debugdir, slash ? "/" : "", 342 is_vdso ? DSO__NAME_VDSO : realname); 343 if (mkdir_p(filename, 0755)) 344 goto out_free; 345 346 snprintf(filename + len, size - len, "/%s", sbuild_id); 347 348 if (access(filename, F_OK)) { 349 if (is_kallsyms) { 350 if (copyfile("/proc/kallsyms", filename)) 351 goto out_free; 352 } else if (link(realname, filename) && copyfile(name, filename)) 353 goto out_free; 354 } 355 356 len = scnprintf(linkname, size, "%s/.build-id/%.2s", 357 debugdir, sbuild_id); 358 359 if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 360 goto out_free; 361 362 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); 363 targetname = filename + strlen(debugdir) - 5; 364 memcpy(targetname, "../..", 5); 365 366 if (symlink(targetname, linkname) == 0) 367 err = 0; 368 out_free: 369 if (!is_kallsyms) 370 free(realname); 371 free(filename); 372 free(linkname); 373 return err; 374 } 375 376 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, 377 const char *name, const char *debugdir, 378 bool is_kallsyms, bool is_vdso) 379 { 380 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 381 382 build_id__sprintf(build_id, build_id_size, sbuild_id); 383 384 return build_id_cache__add_s(sbuild_id, debugdir, name, 385 is_kallsyms, is_vdso); 386 } 387 388 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) 389 { 390 const size_t size = PATH_MAX; 391 char *filename = zalloc(size), 392 *linkname = zalloc(size); 393 int err = -1; 394 395 if (filename == NULL || linkname == NULL) 396 goto out_free; 397 398 snprintf(linkname, size, "%s/.build-id/%.2s/%s", 399 debugdir, sbuild_id, sbuild_id + 2); 400 401 if (access(linkname, F_OK)) 402 goto out_free; 403 404 if (readlink(linkname, filename, size - 1) < 0) 405 goto out_free; 406 407 if (unlink(linkname)) 408 goto out_free; 409 410 /* 411 * Since the link is relative, we must make it absolute: 412 */ 413 snprintf(linkname, size, "%s/.build-id/%.2s/%s", 414 debugdir, sbuild_id, filename); 415 416 if (unlink(linkname)) 417 goto out_free; 418 419 err = 0; 420 out_free: 421 free(filename); 422 free(linkname); 423 return err; 424 } 425 426 static int dso__cache_build_id(struct dso *dso, struct machine *machine, 427 const char *debugdir) 428 { 429 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; 430 bool is_vdso = dso__is_vdso(dso); 431 const char *name = dso->long_name; 432 char nm[PATH_MAX]; 433 434 if (dso__is_kcore(dso)) { 435 is_kallsyms = true; 436 machine__mmap_name(machine, nm, sizeof(nm)); 437 name = nm; 438 } 439 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name, 440 debugdir, is_kallsyms, is_vdso); 441 } 442 443 static int __dsos__cache_build_ids(struct list_head *head, 444 struct machine *machine, const char *debugdir) 445 { 446 struct dso *pos; 447 int err = 0; 448 449 dsos__for_each_with_build_id(pos, head) 450 if (dso__cache_build_id(pos, machine, debugdir)) 451 err = -1; 452 453 return err; 454 } 455 456 static int machine__cache_build_ids(struct machine *machine, const char *debugdir) 457 { 458 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, machine, 459 debugdir); 460 ret |= __dsos__cache_build_ids(&machine->user_dsos, machine, debugdir); 461 return ret; 462 } 463 464 static int perf_session__cache_build_ids(struct perf_session *session) 465 { 466 struct rb_node *nd; 467 int ret; 468 char debugdir[PATH_MAX]; 469 470 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); 471 472 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) 473 return -1; 474 475 ret = machine__cache_build_ids(&session->machines.host, debugdir); 476 477 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 478 struct machine *pos = rb_entry(nd, struct machine, rb_node); 479 ret |= machine__cache_build_ids(pos, debugdir); 480 } 481 return ret ? -1 : 0; 482 } 483 484 static bool machine__read_build_ids(struct machine *machine, bool with_hits) 485 { 486 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); 487 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); 488 return ret; 489 } 490 491 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) 492 { 493 struct rb_node *nd; 494 bool ret = machine__read_build_ids(&session->machines.host, with_hits); 495 496 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 497 struct machine *pos = rb_entry(nd, struct machine, rb_node); 498 ret |= machine__read_build_ids(pos, with_hits); 499 } 500 501 return ret; 502 } 503 504 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused, 505 struct perf_evlist *evlist) 506 { 507 return read_tracing_data(fd, &evlist->entries); 508 } 509 510 511 static int write_build_id(int fd, struct perf_header *h, 512 struct perf_evlist *evlist __maybe_unused) 513 { 514 struct perf_session *session; 515 int err; 516 517 session = container_of(h, struct perf_session, header); 518 519 if (!perf_session__read_build_ids(session, true)) 520 return -1; 521 522 err = dsos__write_buildid_table(h, fd); 523 if (err < 0) { 524 pr_debug("failed to write buildid table\n"); 525 return err; 526 } 527 if (!no_buildid_cache) 528 perf_session__cache_build_ids(session); 529 530 return 0; 531 } 532 533 static int write_hostname(int fd, struct perf_header *h __maybe_unused, 534 struct perf_evlist *evlist __maybe_unused) 535 { 536 struct utsname uts; 537 int ret; 538 539 ret = uname(&uts); 540 if (ret < 0) 541 return -1; 542 543 return do_write_string(fd, uts.nodename); 544 } 545 546 static int write_osrelease(int fd, struct perf_header *h __maybe_unused, 547 struct perf_evlist *evlist __maybe_unused) 548 { 549 struct utsname uts; 550 int ret; 551 552 ret = uname(&uts); 553 if (ret < 0) 554 return -1; 555 556 return do_write_string(fd, uts.release); 557 } 558 559 static int write_arch(int fd, struct perf_header *h __maybe_unused, 560 struct perf_evlist *evlist __maybe_unused) 561 { 562 struct utsname uts; 563 int ret; 564 565 ret = uname(&uts); 566 if (ret < 0) 567 return -1; 568 569 return do_write_string(fd, uts.machine); 570 } 571 572 static int write_version(int fd, struct perf_header *h __maybe_unused, 573 struct perf_evlist *evlist __maybe_unused) 574 { 575 return do_write_string(fd, perf_version_string); 576 } 577 578 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused, 579 struct perf_evlist *evlist __maybe_unused) 580 { 581 #ifndef CPUINFO_PROC 582 #define CPUINFO_PROC NULL 583 #endif 584 FILE *file; 585 char *buf = NULL; 586 char *s, *p; 587 const char *search = CPUINFO_PROC; 588 size_t len = 0; 589 int ret = -1; 590 591 if (!search) 592 return -1; 593 594 file = fopen("/proc/cpuinfo", "r"); 595 if (!file) 596 return -1; 597 598 while (getline(&buf, &len, file) > 0) { 599 ret = strncmp(buf, search, strlen(search)); 600 if (!ret) 601 break; 602 } 603 604 if (ret) 605 goto done; 606 607 s = buf; 608 609 p = strchr(buf, ':'); 610 if (p && *(p+1) == ' ' && *(p+2)) 611 s = p + 2; 612 p = strchr(s, '\n'); 613 if (p) 614 *p = '\0'; 615 616 /* squash extra space characters (branding string) */ 617 p = s; 618 while (*p) { 619 if (isspace(*p)) { 620 char *r = p + 1; 621 char *q = r; 622 *p = ' '; 623 while (*q && isspace(*q)) 624 q++; 625 if (q != (p+1)) 626 while ((*r++ = *q++)); 627 } 628 p++; 629 } 630 ret = do_write_string(fd, s); 631 done: 632 free(buf); 633 fclose(file); 634 return ret; 635 } 636 637 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused, 638 struct perf_evlist *evlist __maybe_unused) 639 { 640 long nr; 641 u32 nrc, nra; 642 int ret; 643 644 nr = sysconf(_SC_NPROCESSORS_CONF); 645 if (nr < 0) 646 return -1; 647 648 nrc = (u32)(nr & UINT_MAX); 649 650 nr = sysconf(_SC_NPROCESSORS_ONLN); 651 if (nr < 0) 652 return -1; 653 654 nra = (u32)(nr & UINT_MAX); 655 656 ret = do_write(fd, &nrc, sizeof(nrc)); 657 if (ret < 0) 658 return ret; 659 660 return do_write(fd, &nra, sizeof(nra)); 661 } 662 663 static int write_event_desc(int fd, struct perf_header *h __maybe_unused, 664 struct perf_evlist *evlist) 665 { 666 struct perf_evsel *evsel; 667 u32 nre, nri, sz; 668 int ret; 669 670 nre = evlist->nr_entries; 671 672 /* 673 * write number of events 674 */ 675 ret = do_write(fd, &nre, sizeof(nre)); 676 if (ret < 0) 677 return ret; 678 679 /* 680 * size of perf_event_attr struct 681 */ 682 sz = (u32)sizeof(evsel->attr); 683 ret = do_write(fd, &sz, sizeof(sz)); 684 if (ret < 0) 685 return ret; 686 687 evlist__for_each(evlist, evsel) { 688 ret = do_write(fd, &evsel->attr, sz); 689 if (ret < 0) 690 return ret; 691 /* 692 * write number of unique id per event 693 * there is one id per instance of an event 694 * 695 * copy into an nri to be independent of the 696 * type of ids, 697 */ 698 nri = evsel->ids; 699 ret = do_write(fd, &nri, sizeof(nri)); 700 if (ret < 0) 701 return ret; 702 703 /* 704 * write event string as passed on cmdline 705 */ 706 ret = do_write_string(fd, perf_evsel__name(evsel)); 707 if (ret < 0) 708 return ret; 709 /* 710 * write unique ids for this event 711 */ 712 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); 713 if (ret < 0) 714 return ret; 715 } 716 return 0; 717 } 718 719 static int write_cmdline(int fd, struct perf_header *h __maybe_unused, 720 struct perf_evlist *evlist __maybe_unused) 721 { 722 char buf[MAXPATHLEN]; 723 char proc[32]; 724 u32 i, n; 725 int ret; 726 727 /* 728 * actual atual path to perf binary 729 */ 730 sprintf(proc, "/proc/%d/exe", getpid()); 731 ret = readlink(proc, buf, sizeof(buf)); 732 if (ret <= 0) 733 return -1; 734 735 /* readlink() does not add null termination */ 736 buf[ret] = '\0'; 737 738 /* account for binary path */ 739 n = header_argc + 1; 740 741 ret = do_write(fd, &n, sizeof(n)); 742 if (ret < 0) 743 return ret; 744 745 ret = do_write_string(fd, buf); 746 if (ret < 0) 747 return ret; 748 749 for (i = 0 ; i < header_argc; i++) { 750 ret = do_write_string(fd, header_argv[i]); 751 if (ret < 0) 752 return ret; 753 } 754 return 0; 755 } 756 757 #define CORE_SIB_FMT \ 758 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" 759 #define THRD_SIB_FMT \ 760 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" 761 762 struct cpu_topo { 763 u32 core_sib; 764 u32 thread_sib; 765 char **core_siblings; 766 char **thread_siblings; 767 }; 768 769 static int build_cpu_topo(struct cpu_topo *tp, int cpu) 770 { 771 FILE *fp; 772 char filename[MAXPATHLEN]; 773 char *buf = NULL, *p; 774 size_t len = 0; 775 ssize_t sret; 776 u32 i = 0; 777 int ret = -1; 778 779 sprintf(filename, CORE_SIB_FMT, cpu); 780 fp = fopen(filename, "r"); 781 if (!fp) 782 goto try_threads; 783 784 sret = getline(&buf, &len, fp); 785 fclose(fp); 786 if (sret <= 0) 787 goto try_threads; 788 789 p = strchr(buf, '\n'); 790 if (p) 791 *p = '\0'; 792 793 for (i = 0; i < tp->core_sib; i++) { 794 if (!strcmp(buf, tp->core_siblings[i])) 795 break; 796 } 797 if (i == tp->core_sib) { 798 tp->core_siblings[i] = buf; 799 tp->core_sib++; 800 buf = NULL; 801 len = 0; 802 } 803 ret = 0; 804 805 try_threads: 806 sprintf(filename, THRD_SIB_FMT, cpu); 807 fp = fopen(filename, "r"); 808 if (!fp) 809 goto done; 810 811 if (getline(&buf, &len, fp) <= 0) 812 goto done; 813 814 p = strchr(buf, '\n'); 815 if (p) 816 *p = '\0'; 817 818 for (i = 0; i < tp->thread_sib; i++) { 819 if (!strcmp(buf, tp->thread_siblings[i])) 820 break; 821 } 822 if (i == tp->thread_sib) { 823 tp->thread_siblings[i] = buf; 824 tp->thread_sib++; 825 buf = NULL; 826 } 827 ret = 0; 828 done: 829 if(fp) 830 fclose(fp); 831 free(buf); 832 return ret; 833 } 834 835 static void free_cpu_topo(struct cpu_topo *tp) 836 { 837 u32 i; 838 839 if (!tp) 840 return; 841 842 for (i = 0 ; i < tp->core_sib; i++) 843 zfree(&tp->core_siblings[i]); 844 845 for (i = 0 ; i < tp->thread_sib; i++) 846 zfree(&tp->thread_siblings[i]); 847 848 free(tp); 849 } 850 851 static struct cpu_topo *build_cpu_topology(void) 852 { 853 struct cpu_topo *tp; 854 void *addr; 855 u32 nr, i; 856 size_t sz; 857 long ncpus; 858 int ret = -1; 859 860 ncpus = sysconf(_SC_NPROCESSORS_CONF); 861 if (ncpus < 0) 862 return NULL; 863 864 nr = (u32)(ncpus & UINT_MAX); 865 866 sz = nr * sizeof(char *); 867 868 addr = calloc(1, sizeof(*tp) + 2 * sz); 869 if (!addr) 870 return NULL; 871 872 tp = addr; 873 874 addr += sizeof(*tp); 875 tp->core_siblings = addr; 876 addr += sz; 877 tp->thread_siblings = addr; 878 879 for (i = 0; i < nr; i++) { 880 ret = build_cpu_topo(tp, i); 881 if (ret < 0) 882 break; 883 } 884 if (ret) { 885 free_cpu_topo(tp); 886 tp = NULL; 887 } 888 return tp; 889 } 890 891 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused, 892 struct perf_evlist *evlist __maybe_unused) 893 { 894 struct cpu_topo *tp; 895 u32 i; 896 int ret; 897 898 tp = build_cpu_topology(); 899 if (!tp) 900 return -1; 901 902 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib)); 903 if (ret < 0) 904 goto done; 905 906 for (i = 0; i < tp->core_sib; i++) { 907 ret = do_write_string(fd, tp->core_siblings[i]); 908 if (ret < 0) 909 goto done; 910 } 911 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib)); 912 if (ret < 0) 913 goto done; 914 915 for (i = 0; i < tp->thread_sib; i++) { 916 ret = do_write_string(fd, tp->thread_siblings[i]); 917 if (ret < 0) 918 break; 919 } 920 done: 921 free_cpu_topo(tp); 922 return ret; 923 } 924 925 926 927 static int write_total_mem(int fd, struct perf_header *h __maybe_unused, 928 struct perf_evlist *evlist __maybe_unused) 929 { 930 char *buf = NULL; 931 FILE *fp; 932 size_t len = 0; 933 int ret = -1, n; 934 uint64_t mem; 935 936 fp = fopen("/proc/meminfo", "r"); 937 if (!fp) 938 return -1; 939 940 while (getline(&buf, &len, fp) > 0) { 941 ret = strncmp(buf, "MemTotal:", 9); 942 if (!ret) 943 break; 944 } 945 if (!ret) { 946 n = sscanf(buf, "%*s %"PRIu64, &mem); 947 if (n == 1) 948 ret = do_write(fd, &mem, sizeof(mem)); 949 } 950 free(buf); 951 fclose(fp); 952 return ret; 953 } 954 955 static int write_topo_node(int fd, int node) 956 { 957 char str[MAXPATHLEN]; 958 char field[32]; 959 char *buf = NULL, *p; 960 size_t len = 0; 961 FILE *fp; 962 u64 mem_total, mem_free, mem; 963 int ret = -1; 964 965 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node); 966 fp = fopen(str, "r"); 967 if (!fp) 968 return -1; 969 970 while (getline(&buf, &len, fp) > 0) { 971 /* skip over invalid lines */ 972 if (!strchr(buf, ':')) 973 continue; 974 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2) 975 goto done; 976 if (!strcmp(field, "MemTotal:")) 977 mem_total = mem; 978 if (!strcmp(field, "MemFree:")) 979 mem_free = mem; 980 } 981 982 fclose(fp); 983 fp = NULL; 984 985 ret = do_write(fd, &mem_total, sizeof(u64)); 986 if (ret) 987 goto done; 988 989 ret = do_write(fd, &mem_free, sizeof(u64)); 990 if (ret) 991 goto done; 992 993 ret = -1; 994 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node); 995 996 fp = fopen(str, "r"); 997 if (!fp) 998 goto done; 999 1000 if (getline(&buf, &len, fp) <= 0) 1001 goto done; 1002 1003 p = strchr(buf, '\n'); 1004 if (p) 1005 *p = '\0'; 1006 1007 ret = do_write_string(fd, buf); 1008 done: 1009 free(buf); 1010 if (fp) 1011 fclose(fp); 1012 return ret; 1013 } 1014 1015 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused, 1016 struct perf_evlist *evlist __maybe_unused) 1017 { 1018 char *buf = NULL; 1019 size_t len = 0; 1020 FILE *fp; 1021 struct cpu_map *node_map = NULL; 1022 char *c; 1023 u32 nr, i, j; 1024 int ret = -1; 1025 1026 fp = fopen("/sys/devices/system/node/online", "r"); 1027 if (!fp) 1028 return -1; 1029 1030 if (getline(&buf, &len, fp) <= 0) 1031 goto done; 1032 1033 c = strchr(buf, '\n'); 1034 if (c) 1035 *c = '\0'; 1036 1037 node_map = cpu_map__new(buf); 1038 if (!node_map) 1039 goto done; 1040 1041 nr = (u32)node_map->nr; 1042 1043 ret = do_write(fd, &nr, sizeof(nr)); 1044 if (ret < 0) 1045 goto done; 1046 1047 for (i = 0; i < nr; i++) { 1048 j = (u32)node_map->map[i]; 1049 ret = do_write(fd, &j, sizeof(j)); 1050 if (ret < 0) 1051 break; 1052 1053 ret = write_topo_node(fd, i); 1054 if (ret < 0) 1055 break; 1056 } 1057 done: 1058 free(buf); 1059 fclose(fp); 1060 free(node_map); 1061 return ret; 1062 } 1063 1064 /* 1065 * File format: 1066 * 1067 * struct pmu_mappings { 1068 * u32 pmu_num; 1069 * struct pmu_map { 1070 * u32 type; 1071 * char name[]; 1072 * }[pmu_num]; 1073 * }; 1074 */ 1075 1076 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused, 1077 struct perf_evlist *evlist __maybe_unused) 1078 { 1079 struct perf_pmu *pmu = NULL; 1080 off_t offset = lseek(fd, 0, SEEK_CUR); 1081 __u32 pmu_num = 0; 1082 int ret; 1083 1084 /* write real pmu_num later */ 1085 ret = do_write(fd, &pmu_num, sizeof(pmu_num)); 1086 if (ret < 0) 1087 return ret; 1088 1089 while ((pmu = perf_pmu__scan(pmu))) { 1090 if (!pmu->name) 1091 continue; 1092 pmu_num++; 1093 1094 ret = do_write(fd, &pmu->type, sizeof(pmu->type)); 1095 if (ret < 0) 1096 return ret; 1097 1098 ret = do_write_string(fd, pmu->name); 1099 if (ret < 0) 1100 return ret; 1101 } 1102 1103 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) { 1104 /* discard all */ 1105 lseek(fd, offset, SEEK_SET); 1106 return -1; 1107 } 1108 1109 return 0; 1110 } 1111 1112 /* 1113 * File format: 1114 * 1115 * struct group_descs { 1116 * u32 nr_groups; 1117 * struct group_desc { 1118 * char name[]; 1119 * u32 leader_idx; 1120 * u32 nr_members; 1121 * }[nr_groups]; 1122 * }; 1123 */ 1124 static int write_group_desc(int fd, struct perf_header *h __maybe_unused, 1125 struct perf_evlist *evlist) 1126 { 1127 u32 nr_groups = evlist->nr_groups; 1128 struct perf_evsel *evsel; 1129 int ret; 1130 1131 ret = do_write(fd, &nr_groups, sizeof(nr_groups)); 1132 if (ret < 0) 1133 return ret; 1134 1135 evlist__for_each(evlist, evsel) { 1136 if (perf_evsel__is_group_leader(evsel) && 1137 evsel->nr_members > 1) { 1138 const char *name = evsel->group_name ?: "{anon_group}"; 1139 u32 leader_idx = evsel->idx; 1140 u32 nr_members = evsel->nr_members; 1141 1142 ret = do_write_string(fd, name); 1143 if (ret < 0) 1144 return ret; 1145 1146 ret = do_write(fd, &leader_idx, sizeof(leader_idx)); 1147 if (ret < 0) 1148 return ret; 1149 1150 ret = do_write(fd, &nr_members, sizeof(nr_members)); 1151 if (ret < 0) 1152 return ret; 1153 } 1154 } 1155 return 0; 1156 } 1157 1158 /* 1159 * default get_cpuid(): nothing gets recorded 1160 * actual implementation must be in arch/$(ARCH)/util/header.c 1161 */ 1162 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused, 1163 size_t sz __maybe_unused) 1164 { 1165 return -1; 1166 } 1167 1168 static int write_cpuid(int fd, struct perf_header *h __maybe_unused, 1169 struct perf_evlist *evlist __maybe_unused) 1170 { 1171 char buffer[64]; 1172 int ret; 1173 1174 ret = get_cpuid(buffer, sizeof(buffer)); 1175 if (!ret) 1176 goto write_it; 1177 1178 return -1; 1179 write_it: 1180 return do_write_string(fd, buffer); 1181 } 1182 1183 static int write_branch_stack(int fd __maybe_unused, 1184 struct perf_header *h __maybe_unused, 1185 struct perf_evlist *evlist __maybe_unused) 1186 { 1187 return 0; 1188 } 1189 1190 static void print_hostname(struct perf_header *ph, int fd __maybe_unused, 1191 FILE *fp) 1192 { 1193 fprintf(fp, "# hostname : %s\n", ph->env.hostname); 1194 } 1195 1196 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused, 1197 FILE *fp) 1198 { 1199 fprintf(fp, "# os release : %s\n", ph->env.os_release); 1200 } 1201 1202 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp) 1203 { 1204 fprintf(fp, "# arch : %s\n", ph->env.arch); 1205 } 1206 1207 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused, 1208 FILE *fp) 1209 { 1210 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc); 1211 } 1212 1213 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused, 1214 FILE *fp) 1215 { 1216 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online); 1217 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail); 1218 } 1219 1220 static void print_version(struct perf_header *ph, int fd __maybe_unused, 1221 FILE *fp) 1222 { 1223 fprintf(fp, "# perf version : %s\n", ph->env.version); 1224 } 1225 1226 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused, 1227 FILE *fp) 1228 { 1229 int nr, i; 1230 char *str; 1231 1232 nr = ph->env.nr_cmdline; 1233 str = ph->env.cmdline; 1234 1235 fprintf(fp, "# cmdline : "); 1236 1237 for (i = 0; i < nr; i++) { 1238 fprintf(fp, "%s ", str); 1239 str += strlen(str) + 1; 1240 } 1241 fputc('\n', fp); 1242 } 1243 1244 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused, 1245 FILE *fp) 1246 { 1247 int nr, i; 1248 char *str; 1249 1250 nr = ph->env.nr_sibling_cores; 1251 str = ph->env.sibling_cores; 1252 1253 for (i = 0; i < nr; i++) { 1254 fprintf(fp, "# sibling cores : %s\n", str); 1255 str += strlen(str) + 1; 1256 } 1257 1258 nr = ph->env.nr_sibling_threads; 1259 str = ph->env.sibling_threads; 1260 1261 for (i = 0; i < nr; i++) { 1262 fprintf(fp, "# sibling threads : %s\n", str); 1263 str += strlen(str) + 1; 1264 } 1265 } 1266 1267 static void free_event_desc(struct perf_evsel *events) 1268 { 1269 struct perf_evsel *evsel; 1270 1271 if (!events) 1272 return; 1273 1274 for (evsel = events; evsel->attr.size; evsel++) { 1275 zfree(&evsel->name); 1276 zfree(&evsel->id); 1277 } 1278 1279 free(events); 1280 } 1281 1282 static struct perf_evsel * 1283 read_event_desc(struct perf_header *ph, int fd) 1284 { 1285 struct perf_evsel *evsel, *events = NULL; 1286 u64 *id; 1287 void *buf = NULL; 1288 u32 nre, sz, nr, i, j; 1289 ssize_t ret; 1290 size_t msz; 1291 1292 /* number of events */ 1293 ret = readn(fd, &nre, sizeof(nre)); 1294 if (ret != (ssize_t)sizeof(nre)) 1295 goto error; 1296 1297 if (ph->needs_swap) 1298 nre = bswap_32(nre); 1299 1300 ret = readn(fd, &sz, sizeof(sz)); 1301 if (ret != (ssize_t)sizeof(sz)) 1302 goto error; 1303 1304 if (ph->needs_swap) 1305 sz = bswap_32(sz); 1306 1307 /* buffer to hold on file attr struct */ 1308 buf = malloc(sz); 1309 if (!buf) 1310 goto error; 1311 1312 /* the last event terminates with evsel->attr.size == 0: */ 1313 events = calloc(nre + 1, sizeof(*events)); 1314 if (!events) 1315 goto error; 1316 1317 msz = sizeof(evsel->attr); 1318 if (sz < msz) 1319 msz = sz; 1320 1321 for (i = 0, evsel = events; i < nre; evsel++, i++) { 1322 evsel->idx = i; 1323 1324 /* 1325 * must read entire on-file attr struct to 1326 * sync up with layout. 1327 */ 1328 ret = readn(fd, buf, sz); 1329 if (ret != (ssize_t)sz) 1330 goto error; 1331 1332 if (ph->needs_swap) 1333 perf_event__attr_swap(buf); 1334 1335 memcpy(&evsel->attr, buf, msz); 1336 1337 ret = readn(fd, &nr, sizeof(nr)); 1338 if (ret != (ssize_t)sizeof(nr)) 1339 goto error; 1340 1341 if (ph->needs_swap) { 1342 nr = bswap_32(nr); 1343 evsel->needs_swap = true; 1344 } 1345 1346 evsel->name = do_read_string(fd, ph); 1347 1348 if (!nr) 1349 continue; 1350 1351 id = calloc(nr, sizeof(*id)); 1352 if (!id) 1353 goto error; 1354 evsel->ids = nr; 1355 evsel->id = id; 1356 1357 for (j = 0 ; j < nr; j++) { 1358 ret = readn(fd, id, sizeof(*id)); 1359 if (ret != (ssize_t)sizeof(*id)) 1360 goto error; 1361 if (ph->needs_swap) 1362 *id = bswap_64(*id); 1363 id++; 1364 } 1365 } 1366 out: 1367 free(buf); 1368 return events; 1369 error: 1370 if (events) 1371 free_event_desc(events); 1372 events = NULL; 1373 goto out; 1374 } 1375 1376 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) 1377 { 1378 struct perf_evsel *evsel, *events = read_event_desc(ph, fd); 1379 u32 j; 1380 u64 *id; 1381 1382 if (!events) { 1383 fprintf(fp, "# event desc: not available or unable to read\n"); 1384 return; 1385 } 1386 1387 for (evsel = events; evsel->attr.size; evsel++) { 1388 fprintf(fp, "# event : name = %s, ", evsel->name); 1389 1390 fprintf(fp, "type = %d, config = 0x%"PRIx64 1391 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64, 1392 evsel->attr.type, 1393 (u64)evsel->attr.config, 1394 (u64)evsel->attr.config1, 1395 (u64)evsel->attr.config2); 1396 1397 fprintf(fp, ", excl_usr = %d, excl_kern = %d", 1398 evsel->attr.exclude_user, 1399 evsel->attr.exclude_kernel); 1400 1401 fprintf(fp, ", excl_host = %d, excl_guest = %d", 1402 evsel->attr.exclude_host, 1403 evsel->attr.exclude_guest); 1404 1405 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip); 1406 1407 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2); 1408 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap); 1409 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data); 1410 if (evsel->ids) { 1411 fprintf(fp, ", id = {"); 1412 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) { 1413 if (j) 1414 fputc(',', fp); 1415 fprintf(fp, " %"PRIu64, *id); 1416 } 1417 fprintf(fp, " }"); 1418 } 1419 1420 fputc('\n', fp); 1421 } 1422 1423 free_event_desc(events); 1424 } 1425 1426 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused, 1427 FILE *fp) 1428 { 1429 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem); 1430 } 1431 1432 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused, 1433 FILE *fp) 1434 { 1435 u32 nr, c, i; 1436 char *str, *tmp; 1437 uint64_t mem_total, mem_free; 1438 1439 /* nr nodes */ 1440 nr = ph->env.nr_numa_nodes; 1441 str = ph->env.numa_nodes; 1442 1443 for (i = 0; i < nr; i++) { 1444 /* node number */ 1445 c = strtoul(str, &tmp, 0); 1446 if (*tmp != ':') 1447 goto error; 1448 1449 str = tmp + 1; 1450 mem_total = strtoull(str, &tmp, 0); 1451 if (*tmp != ':') 1452 goto error; 1453 1454 str = tmp + 1; 1455 mem_free = strtoull(str, &tmp, 0); 1456 if (*tmp != ':') 1457 goto error; 1458 1459 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," 1460 " free = %"PRIu64" kB\n", 1461 c, mem_total, mem_free); 1462 1463 str = tmp + 1; 1464 fprintf(fp, "# node%u cpu list : %s\n", c, str); 1465 1466 str += strlen(str) + 1; 1467 } 1468 return; 1469 error: 1470 fprintf(fp, "# numa topology : not available\n"); 1471 } 1472 1473 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp) 1474 { 1475 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid); 1476 } 1477 1478 static void print_branch_stack(struct perf_header *ph __maybe_unused, 1479 int fd __maybe_unused, FILE *fp) 1480 { 1481 fprintf(fp, "# contains samples with branch stack\n"); 1482 } 1483 1484 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused, 1485 FILE *fp) 1486 { 1487 const char *delimiter = "# pmu mappings: "; 1488 char *str, *tmp; 1489 u32 pmu_num; 1490 u32 type; 1491 1492 pmu_num = ph->env.nr_pmu_mappings; 1493 if (!pmu_num) { 1494 fprintf(fp, "# pmu mappings: not available\n"); 1495 return; 1496 } 1497 1498 str = ph->env.pmu_mappings; 1499 1500 while (pmu_num) { 1501 type = strtoul(str, &tmp, 0); 1502 if (*tmp != ':') 1503 goto error; 1504 1505 str = tmp + 1; 1506 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); 1507 1508 delimiter = ", "; 1509 str += strlen(str) + 1; 1510 pmu_num--; 1511 } 1512 1513 fprintf(fp, "\n"); 1514 1515 if (!pmu_num) 1516 return; 1517 error: 1518 fprintf(fp, "# pmu mappings: unable to read\n"); 1519 } 1520 1521 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused, 1522 FILE *fp) 1523 { 1524 struct perf_session *session; 1525 struct perf_evsel *evsel; 1526 u32 nr = 0; 1527 1528 session = container_of(ph, struct perf_session, header); 1529 1530 evlist__for_each(session->evlist, evsel) { 1531 if (perf_evsel__is_group_leader(evsel) && 1532 evsel->nr_members > 1) { 1533 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", 1534 perf_evsel__name(evsel)); 1535 1536 nr = evsel->nr_members - 1; 1537 } else if (nr) { 1538 fprintf(fp, ",%s", perf_evsel__name(evsel)); 1539 1540 if (--nr == 0) 1541 fprintf(fp, "}\n"); 1542 } 1543 } 1544 } 1545 1546 static int __event_process_build_id(struct build_id_event *bev, 1547 char *filename, 1548 struct perf_session *session) 1549 { 1550 int err = -1; 1551 struct list_head *head; 1552 struct machine *machine; 1553 u16 misc; 1554 struct dso *dso; 1555 enum dso_kernel_type dso_type; 1556 1557 machine = perf_session__findnew_machine(session, bev->pid); 1558 if (!machine) 1559 goto out; 1560 1561 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1562 1563 switch (misc) { 1564 case PERF_RECORD_MISC_KERNEL: 1565 dso_type = DSO_TYPE_KERNEL; 1566 head = &machine->kernel_dsos; 1567 break; 1568 case PERF_RECORD_MISC_GUEST_KERNEL: 1569 dso_type = DSO_TYPE_GUEST_KERNEL; 1570 head = &machine->kernel_dsos; 1571 break; 1572 case PERF_RECORD_MISC_USER: 1573 case PERF_RECORD_MISC_GUEST_USER: 1574 dso_type = DSO_TYPE_USER; 1575 head = &machine->user_dsos; 1576 break; 1577 default: 1578 goto out; 1579 } 1580 1581 dso = __dsos__findnew(head, filename); 1582 if (dso != NULL) { 1583 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 1584 1585 dso__set_build_id(dso, &bev->build_id); 1586 1587 if (filename[0] == '[') 1588 dso->kernel = dso_type; 1589 1590 build_id__sprintf(dso->build_id, sizeof(dso->build_id), 1591 sbuild_id); 1592 pr_debug("build id event received for %s: %s\n", 1593 dso->long_name, sbuild_id); 1594 } 1595 1596 err = 0; 1597 out: 1598 return err; 1599 } 1600 1601 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, 1602 int input, u64 offset, u64 size) 1603 { 1604 struct perf_session *session = container_of(header, struct perf_session, header); 1605 struct { 1606 struct perf_event_header header; 1607 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; 1608 char filename[0]; 1609 } old_bev; 1610 struct build_id_event bev; 1611 char filename[PATH_MAX]; 1612 u64 limit = offset + size; 1613 1614 while (offset < limit) { 1615 ssize_t len; 1616 1617 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) 1618 return -1; 1619 1620 if (header->needs_swap) 1621 perf_event_header__bswap(&old_bev.header); 1622 1623 len = old_bev.header.size - sizeof(old_bev); 1624 if (readn(input, filename, len) != len) 1625 return -1; 1626 1627 bev.header = old_bev.header; 1628 1629 /* 1630 * As the pid is the missing value, we need to fill 1631 * it properly. The header.misc value give us nice hint. 1632 */ 1633 bev.pid = HOST_KERNEL_ID; 1634 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || 1635 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) 1636 bev.pid = DEFAULT_GUEST_KERNEL_ID; 1637 1638 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); 1639 __event_process_build_id(&bev, filename, session); 1640 1641 offset += bev.header.size; 1642 } 1643 1644 return 0; 1645 } 1646 1647 static int perf_header__read_build_ids(struct perf_header *header, 1648 int input, u64 offset, u64 size) 1649 { 1650 struct perf_session *session = container_of(header, struct perf_session, header); 1651 struct build_id_event bev; 1652 char filename[PATH_MAX]; 1653 u64 limit = offset + size, orig_offset = offset; 1654 int err = -1; 1655 1656 while (offset < limit) { 1657 ssize_t len; 1658 1659 if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) 1660 goto out; 1661 1662 if (header->needs_swap) 1663 perf_event_header__bswap(&bev.header); 1664 1665 len = bev.header.size - sizeof(bev); 1666 if (readn(input, filename, len) != len) 1667 goto out; 1668 /* 1669 * The a1645ce1 changeset: 1670 * 1671 * "perf: 'perf kvm' tool for monitoring guest performance from host" 1672 * 1673 * Added a field to struct build_id_event that broke the file 1674 * format. 1675 * 1676 * Since the kernel build-id is the first entry, process the 1677 * table using the old format if the well known 1678 * '[kernel.kallsyms]' string for the kernel build-id has the 1679 * first 4 characters chopped off (where the pid_t sits). 1680 */ 1681 if (memcmp(filename, "nel.kallsyms]", 13) == 0) { 1682 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) 1683 return -1; 1684 return perf_header__read_build_ids_abi_quirk(header, input, offset, size); 1685 } 1686 1687 __event_process_build_id(&bev, filename, session); 1688 1689 offset += bev.header.size; 1690 } 1691 err = 0; 1692 out: 1693 return err; 1694 } 1695 1696 static int process_tracing_data(struct perf_file_section *section __maybe_unused, 1697 struct perf_header *ph __maybe_unused, 1698 int fd, void *data) 1699 { 1700 ssize_t ret = trace_report(fd, data, false); 1701 return ret < 0 ? -1 : 0; 1702 } 1703 1704 static int process_build_id(struct perf_file_section *section, 1705 struct perf_header *ph, int fd, 1706 void *data __maybe_unused) 1707 { 1708 if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) 1709 pr_debug("Failed to read buildids, continuing...\n"); 1710 return 0; 1711 } 1712 1713 static int process_hostname(struct perf_file_section *section __maybe_unused, 1714 struct perf_header *ph, int fd, 1715 void *data __maybe_unused) 1716 { 1717 ph->env.hostname = do_read_string(fd, ph); 1718 return ph->env.hostname ? 0 : -ENOMEM; 1719 } 1720 1721 static int process_osrelease(struct perf_file_section *section __maybe_unused, 1722 struct perf_header *ph, int fd, 1723 void *data __maybe_unused) 1724 { 1725 ph->env.os_release = do_read_string(fd, ph); 1726 return ph->env.os_release ? 0 : -ENOMEM; 1727 } 1728 1729 static int process_version(struct perf_file_section *section __maybe_unused, 1730 struct perf_header *ph, int fd, 1731 void *data __maybe_unused) 1732 { 1733 ph->env.version = do_read_string(fd, ph); 1734 return ph->env.version ? 0 : -ENOMEM; 1735 } 1736 1737 static int process_arch(struct perf_file_section *section __maybe_unused, 1738 struct perf_header *ph, int fd, 1739 void *data __maybe_unused) 1740 { 1741 ph->env.arch = do_read_string(fd, ph); 1742 return ph->env.arch ? 0 : -ENOMEM; 1743 } 1744 1745 static int process_nrcpus(struct perf_file_section *section __maybe_unused, 1746 struct perf_header *ph, int fd, 1747 void *data __maybe_unused) 1748 { 1749 ssize_t ret; 1750 u32 nr; 1751 1752 ret = readn(fd, &nr, sizeof(nr)); 1753 if (ret != sizeof(nr)) 1754 return -1; 1755 1756 if (ph->needs_swap) 1757 nr = bswap_32(nr); 1758 1759 ph->env.nr_cpus_online = nr; 1760 1761 ret = readn(fd, &nr, sizeof(nr)); 1762 if (ret != sizeof(nr)) 1763 return -1; 1764 1765 if (ph->needs_swap) 1766 nr = bswap_32(nr); 1767 1768 ph->env.nr_cpus_avail = nr; 1769 return 0; 1770 } 1771 1772 static int process_cpudesc(struct perf_file_section *section __maybe_unused, 1773 struct perf_header *ph, int fd, 1774 void *data __maybe_unused) 1775 { 1776 ph->env.cpu_desc = do_read_string(fd, ph); 1777 return ph->env.cpu_desc ? 0 : -ENOMEM; 1778 } 1779 1780 static int process_cpuid(struct perf_file_section *section __maybe_unused, 1781 struct perf_header *ph, int fd, 1782 void *data __maybe_unused) 1783 { 1784 ph->env.cpuid = do_read_string(fd, ph); 1785 return ph->env.cpuid ? 0 : -ENOMEM; 1786 } 1787 1788 static int process_total_mem(struct perf_file_section *section __maybe_unused, 1789 struct perf_header *ph, int fd, 1790 void *data __maybe_unused) 1791 { 1792 uint64_t mem; 1793 ssize_t ret; 1794 1795 ret = readn(fd, &mem, sizeof(mem)); 1796 if (ret != sizeof(mem)) 1797 return -1; 1798 1799 if (ph->needs_swap) 1800 mem = bswap_64(mem); 1801 1802 ph->env.total_mem = mem; 1803 return 0; 1804 } 1805 1806 static struct perf_evsel * 1807 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx) 1808 { 1809 struct perf_evsel *evsel; 1810 1811 evlist__for_each(evlist, evsel) { 1812 if (evsel->idx == idx) 1813 return evsel; 1814 } 1815 1816 return NULL; 1817 } 1818 1819 static void 1820 perf_evlist__set_event_name(struct perf_evlist *evlist, 1821 struct perf_evsel *event) 1822 { 1823 struct perf_evsel *evsel; 1824 1825 if (!event->name) 1826 return; 1827 1828 evsel = perf_evlist__find_by_index(evlist, event->idx); 1829 if (!evsel) 1830 return; 1831 1832 if (evsel->name) 1833 return; 1834 1835 evsel->name = strdup(event->name); 1836 } 1837 1838 static int 1839 process_event_desc(struct perf_file_section *section __maybe_unused, 1840 struct perf_header *header, int fd, 1841 void *data __maybe_unused) 1842 { 1843 struct perf_session *session; 1844 struct perf_evsel *evsel, *events = read_event_desc(header, fd); 1845 1846 if (!events) 1847 return 0; 1848 1849 session = container_of(header, struct perf_session, header); 1850 for (evsel = events; evsel->attr.size; evsel++) 1851 perf_evlist__set_event_name(session->evlist, evsel); 1852 1853 free_event_desc(events); 1854 1855 return 0; 1856 } 1857 1858 static int process_cmdline(struct perf_file_section *section __maybe_unused, 1859 struct perf_header *ph, int fd, 1860 void *data __maybe_unused) 1861 { 1862 ssize_t ret; 1863 char *str; 1864 u32 nr, i; 1865 struct strbuf sb; 1866 1867 ret = readn(fd, &nr, sizeof(nr)); 1868 if (ret != sizeof(nr)) 1869 return -1; 1870 1871 if (ph->needs_swap) 1872 nr = bswap_32(nr); 1873 1874 ph->env.nr_cmdline = nr; 1875 strbuf_init(&sb, 128); 1876 1877 for (i = 0; i < nr; i++) { 1878 str = do_read_string(fd, ph); 1879 if (!str) 1880 goto error; 1881 1882 /* include a NULL character at the end */ 1883 strbuf_add(&sb, str, strlen(str) + 1); 1884 free(str); 1885 } 1886 ph->env.cmdline = strbuf_detach(&sb, NULL); 1887 return 0; 1888 1889 error: 1890 strbuf_release(&sb); 1891 return -1; 1892 } 1893 1894 static int process_cpu_topology(struct perf_file_section *section __maybe_unused, 1895 struct perf_header *ph, int fd, 1896 void *data __maybe_unused) 1897 { 1898 ssize_t ret; 1899 u32 nr, i; 1900 char *str; 1901 struct strbuf sb; 1902 1903 ret = readn(fd, &nr, sizeof(nr)); 1904 if (ret != sizeof(nr)) 1905 return -1; 1906 1907 if (ph->needs_swap) 1908 nr = bswap_32(nr); 1909 1910 ph->env.nr_sibling_cores = nr; 1911 strbuf_init(&sb, 128); 1912 1913 for (i = 0; i < nr; i++) { 1914 str = do_read_string(fd, ph); 1915 if (!str) 1916 goto error; 1917 1918 /* include a NULL character at the end */ 1919 strbuf_add(&sb, str, strlen(str) + 1); 1920 free(str); 1921 } 1922 ph->env.sibling_cores = strbuf_detach(&sb, NULL); 1923 1924 ret = readn(fd, &nr, sizeof(nr)); 1925 if (ret != sizeof(nr)) 1926 return -1; 1927 1928 if (ph->needs_swap) 1929 nr = bswap_32(nr); 1930 1931 ph->env.nr_sibling_threads = nr; 1932 1933 for (i = 0; i < nr; i++) { 1934 str = do_read_string(fd, ph); 1935 if (!str) 1936 goto error; 1937 1938 /* include a NULL character at the end */ 1939 strbuf_add(&sb, str, strlen(str) + 1); 1940 free(str); 1941 } 1942 ph->env.sibling_threads = strbuf_detach(&sb, NULL); 1943 return 0; 1944 1945 error: 1946 strbuf_release(&sb); 1947 return -1; 1948 } 1949 1950 static int process_numa_topology(struct perf_file_section *section __maybe_unused, 1951 struct perf_header *ph, int fd, 1952 void *data __maybe_unused) 1953 { 1954 ssize_t ret; 1955 u32 nr, node, i; 1956 char *str; 1957 uint64_t mem_total, mem_free; 1958 struct strbuf sb; 1959 1960 /* nr nodes */ 1961 ret = readn(fd, &nr, sizeof(nr)); 1962 if (ret != sizeof(nr)) 1963 goto error; 1964 1965 if (ph->needs_swap) 1966 nr = bswap_32(nr); 1967 1968 ph->env.nr_numa_nodes = nr; 1969 strbuf_init(&sb, 256); 1970 1971 for (i = 0; i < nr; i++) { 1972 /* node number */ 1973 ret = readn(fd, &node, sizeof(node)); 1974 if (ret != sizeof(node)) 1975 goto error; 1976 1977 ret = readn(fd, &mem_total, sizeof(u64)); 1978 if (ret != sizeof(u64)) 1979 goto error; 1980 1981 ret = readn(fd, &mem_free, sizeof(u64)); 1982 if (ret != sizeof(u64)) 1983 goto error; 1984 1985 if (ph->needs_swap) { 1986 node = bswap_32(node); 1987 mem_total = bswap_64(mem_total); 1988 mem_free = bswap_64(mem_free); 1989 } 1990 1991 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":", 1992 node, mem_total, mem_free); 1993 1994 str = do_read_string(fd, ph); 1995 if (!str) 1996 goto error; 1997 1998 /* include a NULL character at the end */ 1999 strbuf_add(&sb, str, strlen(str) + 1); 2000 free(str); 2001 } 2002 ph->env.numa_nodes = strbuf_detach(&sb, NULL); 2003 return 0; 2004 2005 error: 2006 strbuf_release(&sb); 2007 return -1; 2008 } 2009 2010 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused, 2011 struct perf_header *ph, int fd, 2012 void *data __maybe_unused) 2013 { 2014 ssize_t ret; 2015 char *name; 2016 u32 pmu_num; 2017 u32 type; 2018 struct strbuf sb; 2019 2020 ret = readn(fd, &pmu_num, sizeof(pmu_num)); 2021 if (ret != sizeof(pmu_num)) 2022 return -1; 2023 2024 if (ph->needs_swap) 2025 pmu_num = bswap_32(pmu_num); 2026 2027 if (!pmu_num) { 2028 pr_debug("pmu mappings not available\n"); 2029 return 0; 2030 } 2031 2032 ph->env.nr_pmu_mappings = pmu_num; 2033 strbuf_init(&sb, 128); 2034 2035 while (pmu_num) { 2036 if (readn(fd, &type, sizeof(type)) != sizeof(type)) 2037 goto error; 2038 if (ph->needs_swap) 2039 type = bswap_32(type); 2040 2041 name = do_read_string(fd, ph); 2042 if (!name) 2043 goto error; 2044 2045 strbuf_addf(&sb, "%u:%s", type, name); 2046 /* include a NULL character at the end */ 2047 strbuf_add(&sb, "", 1); 2048 2049 free(name); 2050 pmu_num--; 2051 } 2052 ph->env.pmu_mappings = strbuf_detach(&sb, NULL); 2053 return 0; 2054 2055 error: 2056 strbuf_release(&sb); 2057 return -1; 2058 } 2059 2060 static int process_group_desc(struct perf_file_section *section __maybe_unused, 2061 struct perf_header *ph, int fd, 2062 void *data __maybe_unused) 2063 { 2064 size_t ret = -1; 2065 u32 i, nr, nr_groups; 2066 struct perf_session *session; 2067 struct perf_evsel *evsel, *leader = NULL; 2068 struct group_desc { 2069 char *name; 2070 u32 leader_idx; 2071 u32 nr_members; 2072 } *desc; 2073 2074 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups)) 2075 return -1; 2076 2077 if (ph->needs_swap) 2078 nr_groups = bswap_32(nr_groups); 2079 2080 ph->env.nr_groups = nr_groups; 2081 if (!nr_groups) { 2082 pr_debug("group desc not available\n"); 2083 return 0; 2084 } 2085 2086 desc = calloc(nr_groups, sizeof(*desc)); 2087 if (!desc) 2088 return -1; 2089 2090 for (i = 0; i < nr_groups; i++) { 2091 desc[i].name = do_read_string(fd, ph); 2092 if (!desc[i].name) 2093 goto out_free; 2094 2095 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32)) 2096 goto out_free; 2097 2098 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32)) 2099 goto out_free; 2100 2101 if (ph->needs_swap) { 2102 desc[i].leader_idx = bswap_32(desc[i].leader_idx); 2103 desc[i].nr_members = bswap_32(desc[i].nr_members); 2104 } 2105 } 2106 2107 /* 2108 * Rebuild group relationship based on the group_desc 2109 */ 2110 session = container_of(ph, struct perf_session, header); 2111 session->evlist->nr_groups = nr_groups; 2112 2113 i = nr = 0; 2114 evlist__for_each(session->evlist, evsel) { 2115 if (evsel->idx == (int) desc[i].leader_idx) { 2116 evsel->leader = evsel; 2117 /* {anon_group} is a dummy name */ 2118 if (strcmp(desc[i].name, "{anon_group}")) { 2119 evsel->group_name = desc[i].name; 2120 desc[i].name = NULL; 2121 } 2122 evsel->nr_members = desc[i].nr_members; 2123 2124 if (i >= nr_groups || nr > 0) { 2125 pr_debug("invalid group desc\n"); 2126 goto out_free; 2127 } 2128 2129 leader = evsel; 2130 nr = evsel->nr_members - 1; 2131 i++; 2132 } else if (nr) { 2133 /* This is a group member */ 2134 evsel->leader = leader; 2135 2136 nr--; 2137 } 2138 } 2139 2140 if (i != nr_groups || nr != 0) { 2141 pr_debug("invalid group desc\n"); 2142 goto out_free; 2143 } 2144 2145 ret = 0; 2146 out_free: 2147 for (i = 0; i < nr_groups; i++) 2148 zfree(&desc[i].name); 2149 free(desc); 2150 2151 return ret; 2152 } 2153 2154 struct feature_ops { 2155 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); 2156 void (*print)(struct perf_header *h, int fd, FILE *fp); 2157 int (*process)(struct perf_file_section *section, 2158 struct perf_header *h, int fd, void *data); 2159 const char *name; 2160 bool full_only; 2161 }; 2162 2163 #define FEAT_OPA(n, func) \ 2164 [n] = { .name = #n, .write = write_##func, .print = print_##func } 2165 #define FEAT_OPP(n, func) \ 2166 [n] = { .name = #n, .write = write_##func, .print = print_##func, \ 2167 .process = process_##func } 2168 #define FEAT_OPF(n, func) \ 2169 [n] = { .name = #n, .write = write_##func, .print = print_##func, \ 2170 .process = process_##func, .full_only = true } 2171 2172 /* feature_ops not implemented: */ 2173 #define print_tracing_data NULL 2174 #define print_build_id NULL 2175 2176 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { 2177 FEAT_OPP(HEADER_TRACING_DATA, tracing_data), 2178 FEAT_OPP(HEADER_BUILD_ID, build_id), 2179 FEAT_OPP(HEADER_HOSTNAME, hostname), 2180 FEAT_OPP(HEADER_OSRELEASE, osrelease), 2181 FEAT_OPP(HEADER_VERSION, version), 2182 FEAT_OPP(HEADER_ARCH, arch), 2183 FEAT_OPP(HEADER_NRCPUS, nrcpus), 2184 FEAT_OPP(HEADER_CPUDESC, cpudesc), 2185 FEAT_OPP(HEADER_CPUID, cpuid), 2186 FEAT_OPP(HEADER_TOTAL_MEM, total_mem), 2187 FEAT_OPP(HEADER_EVENT_DESC, event_desc), 2188 FEAT_OPP(HEADER_CMDLINE, cmdline), 2189 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), 2190 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), 2191 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack), 2192 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings), 2193 FEAT_OPP(HEADER_GROUP_DESC, group_desc), 2194 }; 2195 2196 struct header_print_data { 2197 FILE *fp; 2198 bool full; /* extended list of headers */ 2199 }; 2200 2201 static int perf_file_section__fprintf_info(struct perf_file_section *section, 2202 struct perf_header *ph, 2203 int feat, int fd, void *data) 2204 { 2205 struct header_print_data *hd = data; 2206 2207 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 2208 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 2209 "%d, continuing...\n", section->offset, feat); 2210 return 0; 2211 } 2212 if (feat >= HEADER_LAST_FEATURE) { 2213 pr_warning("unknown feature %d\n", feat); 2214 return 0; 2215 } 2216 if (!feat_ops[feat].print) 2217 return 0; 2218 2219 if (!feat_ops[feat].full_only || hd->full) 2220 feat_ops[feat].print(ph, fd, hd->fp); 2221 else 2222 fprintf(hd->fp, "# %s info available, use -I to display\n", 2223 feat_ops[feat].name); 2224 2225 return 0; 2226 } 2227 2228 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) 2229 { 2230 struct header_print_data hd; 2231 struct perf_header *header = &session->header; 2232 int fd = perf_data_file__fd(session->file); 2233 hd.fp = fp; 2234 hd.full = full; 2235 2236 perf_header__process_sections(header, fd, &hd, 2237 perf_file_section__fprintf_info); 2238 return 0; 2239 } 2240 2241 static int do_write_feat(int fd, struct perf_header *h, int type, 2242 struct perf_file_section **p, 2243 struct perf_evlist *evlist) 2244 { 2245 int err; 2246 int ret = 0; 2247 2248 if (perf_header__has_feat(h, type)) { 2249 if (!feat_ops[type].write) 2250 return -1; 2251 2252 (*p)->offset = lseek(fd, 0, SEEK_CUR); 2253 2254 err = feat_ops[type].write(fd, h, evlist); 2255 if (err < 0) { 2256 pr_debug("failed to write feature %d\n", type); 2257 2258 /* undo anything written */ 2259 lseek(fd, (*p)->offset, SEEK_SET); 2260 2261 return -1; 2262 } 2263 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; 2264 (*p)++; 2265 } 2266 return ret; 2267 } 2268 2269 static int perf_header__adds_write(struct perf_header *header, 2270 struct perf_evlist *evlist, int fd) 2271 { 2272 int nr_sections; 2273 struct perf_file_section *feat_sec, *p; 2274 int sec_size; 2275 u64 sec_start; 2276 int feat; 2277 int err; 2278 2279 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 2280 if (!nr_sections) 2281 return 0; 2282 2283 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec)); 2284 if (feat_sec == NULL) 2285 return -ENOMEM; 2286 2287 sec_size = sizeof(*feat_sec) * nr_sections; 2288 2289 sec_start = header->feat_offset; 2290 lseek(fd, sec_start + sec_size, SEEK_SET); 2291 2292 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 2293 if (do_write_feat(fd, header, feat, &p, evlist)) 2294 perf_header__clear_feat(header, feat); 2295 } 2296 2297 lseek(fd, sec_start, SEEK_SET); 2298 /* 2299 * may write more than needed due to dropped feature, but 2300 * this is okay, reader will skip the mising entries 2301 */ 2302 err = do_write(fd, feat_sec, sec_size); 2303 if (err < 0) 2304 pr_debug("failed to write feature section\n"); 2305 free(feat_sec); 2306 return err; 2307 } 2308 2309 int perf_header__write_pipe(int fd) 2310 { 2311 struct perf_pipe_file_header f_header; 2312 int err; 2313 2314 f_header = (struct perf_pipe_file_header){ 2315 .magic = PERF_MAGIC, 2316 .size = sizeof(f_header), 2317 }; 2318 2319 err = do_write(fd, &f_header, sizeof(f_header)); 2320 if (err < 0) { 2321 pr_debug("failed to write perf pipe header\n"); 2322 return err; 2323 } 2324 2325 return 0; 2326 } 2327 2328 int perf_session__write_header(struct perf_session *session, 2329 struct perf_evlist *evlist, 2330 int fd, bool at_exit) 2331 { 2332 struct perf_file_header f_header; 2333 struct perf_file_attr f_attr; 2334 struct perf_header *header = &session->header; 2335 struct perf_evsel *evsel; 2336 u64 attr_offset; 2337 int err; 2338 2339 lseek(fd, sizeof(f_header), SEEK_SET); 2340 2341 evlist__for_each(session->evlist, evsel) { 2342 evsel->id_offset = lseek(fd, 0, SEEK_CUR); 2343 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); 2344 if (err < 0) { 2345 pr_debug("failed to write perf header\n"); 2346 return err; 2347 } 2348 } 2349 2350 attr_offset = lseek(fd, 0, SEEK_CUR); 2351 2352 evlist__for_each(evlist, evsel) { 2353 f_attr = (struct perf_file_attr){ 2354 .attr = evsel->attr, 2355 .ids = { 2356 .offset = evsel->id_offset, 2357 .size = evsel->ids * sizeof(u64), 2358 } 2359 }; 2360 err = do_write(fd, &f_attr, sizeof(f_attr)); 2361 if (err < 0) { 2362 pr_debug("failed to write perf header attribute\n"); 2363 return err; 2364 } 2365 } 2366 2367 if (!header->data_offset) 2368 header->data_offset = lseek(fd, 0, SEEK_CUR); 2369 header->feat_offset = header->data_offset + header->data_size; 2370 2371 if (at_exit) { 2372 err = perf_header__adds_write(header, evlist, fd); 2373 if (err < 0) 2374 return err; 2375 } 2376 2377 f_header = (struct perf_file_header){ 2378 .magic = PERF_MAGIC, 2379 .size = sizeof(f_header), 2380 .attr_size = sizeof(f_attr), 2381 .attrs = { 2382 .offset = attr_offset, 2383 .size = evlist->nr_entries * sizeof(f_attr), 2384 }, 2385 .data = { 2386 .offset = header->data_offset, 2387 .size = header->data_size, 2388 }, 2389 /* event_types is ignored, store zeros */ 2390 }; 2391 2392 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); 2393 2394 lseek(fd, 0, SEEK_SET); 2395 err = do_write(fd, &f_header, sizeof(f_header)); 2396 if (err < 0) { 2397 pr_debug("failed to write perf header\n"); 2398 return err; 2399 } 2400 lseek(fd, header->data_offset + header->data_size, SEEK_SET); 2401 2402 return 0; 2403 } 2404 2405 static int perf_header__getbuffer64(struct perf_header *header, 2406 int fd, void *buf, size_t size) 2407 { 2408 if (readn(fd, buf, size) <= 0) 2409 return -1; 2410 2411 if (header->needs_swap) 2412 mem_bswap_64(buf, size); 2413 2414 return 0; 2415 } 2416 2417 int perf_header__process_sections(struct perf_header *header, int fd, 2418 void *data, 2419 int (*process)(struct perf_file_section *section, 2420 struct perf_header *ph, 2421 int feat, int fd, void *data)) 2422 { 2423 struct perf_file_section *feat_sec, *sec; 2424 int nr_sections; 2425 int sec_size; 2426 int feat; 2427 int err; 2428 2429 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 2430 if (!nr_sections) 2431 return 0; 2432 2433 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec)); 2434 if (!feat_sec) 2435 return -1; 2436 2437 sec_size = sizeof(*feat_sec) * nr_sections; 2438 2439 lseek(fd, header->feat_offset, SEEK_SET); 2440 2441 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); 2442 if (err < 0) 2443 goto out_free; 2444 2445 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { 2446 err = process(sec++, header, feat, fd, data); 2447 if (err < 0) 2448 goto out_free; 2449 } 2450 err = 0; 2451 out_free: 2452 free(feat_sec); 2453 return err; 2454 } 2455 2456 static const int attr_file_abi_sizes[] = { 2457 [0] = PERF_ATTR_SIZE_VER0, 2458 [1] = PERF_ATTR_SIZE_VER1, 2459 [2] = PERF_ATTR_SIZE_VER2, 2460 [3] = PERF_ATTR_SIZE_VER3, 2461 0, 2462 }; 2463 2464 /* 2465 * In the legacy file format, the magic number is not used to encode endianness. 2466 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based 2467 * on ABI revisions, we need to try all combinations for all endianness to 2468 * detect the endianness. 2469 */ 2470 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) 2471 { 2472 uint64_t ref_size, attr_size; 2473 int i; 2474 2475 for (i = 0 ; attr_file_abi_sizes[i]; i++) { 2476 ref_size = attr_file_abi_sizes[i] 2477 + sizeof(struct perf_file_section); 2478 if (hdr_sz != ref_size) { 2479 attr_size = bswap_64(hdr_sz); 2480 if (attr_size != ref_size) 2481 continue; 2482 2483 ph->needs_swap = true; 2484 } 2485 pr_debug("ABI%d perf.data file detected, need_swap=%d\n", 2486 i, 2487 ph->needs_swap); 2488 return 0; 2489 } 2490 /* could not determine endianness */ 2491 return -1; 2492 } 2493 2494 #define PERF_PIPE_HDR_VER0 16 2495 2496 static const size_t attr_pipe_abi_sizes[] = { 2497 [0] = PERF_PIPE_HDR_VER0, 2498 0, 2499 }; 2500 2501 /* 2502 * In the legacy pipe format, there is an implicit assumption that endiannesss 2503 * between host recording the samples, and host parsing the samples is the 2504 * same. This is not always the case given that the pipe output may always be 2505 * redirected into a file and analyzed on a different machine with possibly a 2506 * different endianness and perf_event ABI revsions in the perf tool itself. 2507 */ 2508 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) 2509 { 2510 u64 attr_size; 2511 int i; 2512 2513 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { 2514 if (hdr_sz != attr_pipe_abi_sizes[i]) { 2515 attr_size = bswap_64(hdr_sz); 2516 if (attr_size != hdr_sz) 2517 continue; 2518 2519 ph->needs_swap = true; 2520 } 2521 pr_debug("Pipe ABI%d perf.data file detected\n", i); 2522 return 0; 2523 } 2524 return -1; 2525 } 2526 2527 bool is_perf_magic(u64 magic) 2528 { 2529 if (!memcmp(&magic, __perf_magic1, sizeof(magic)) 2530 || magic == __perf_magic2 2531 || magic == __perf_magic2_sw) 2532 return true; 2533 2534 return false; 2535 } 2536 2537 static int check_magic_endian(u64 magic, uint64_t hdr_sz, 2538 bool is_pipe, struct perf_header *ph) 2539 { 2540 int ret; 2541 2542 /* check for legacy format */ 2543 ret = memcmp(&magic, __perf_magic1, sizeof(magic)); 2544 if (ret == 0) { 2545 ph->version = PERF_HEADER_VERSION_1; 2546 pr_debug("legacy perf.data format\n"); 2547 if (is_pipe) 2548 return try_all_pipe_abis(hdr_sz, ph); 2549 2550 return try_all_file_abis(hdr_sz, ph); 2551 } 2552 /* 2553 * the new magic number serves two purposes: 2554 * - unique number to identify actual perf.data files 2555 * - encode endianness of file 2556 */ 2557 2558 /* check magic number with one endianness */ 2559 if (magic == __perf_magic2) 2560 return 0; 2561 2562 /* check magic number with opposite endianness */ 2563 if (magic != __perf_magic2_sw) 2564 return -1; 2565 2566 ph->needs_swap = true; 2567 ph->version = PERF_HEADER_VERSION_2; 2568 2569 return 0; 2570 } 2571 2572 int perf_file_header__read(struct perf_file_header *header, 2573 struct perf_header *ph, int fd) 2574 { 2575 ssize_t ret; 2576 2577 lseek(fd, 0, SEEK_SET); 2578 2579 ret = readn(fd, header, sizeof(*header)); 2580 if (ret <= 0) 2581 return -1; 2582 2583 if (check_magic_endian(header->magic, 2584 header->attr_size, false, ph) < 0) { 2585 pr_debug("magic/endian check failed\n"); 2586 return -1; 2587 } 2588 2589 if (ph->needs_swap) { 2590 mem_bswap_64(header, offsetof(struct perf_file_header, 2591 adds_features)); 2592 } 2593 2594 if (header->size != sizeof(*header)) { 2595 /* Support the previous format */ 2596 if (header->size == offsetof(typeof(*header), adds_features)) 2597 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 2598 else 2599 return -1; 2600 } else if (ph->needs_swap) { 2601 /* 2602 * feature bitmap is declared as an array of unsigned longs -- 2603 * not good since its size can differ between the host that 2604 * generated the data file and the host analyzing the file. 2605 * 2606 * We need to handle endianness, but we don't know the size of 2607 * the unsigned long where the file was generated. Take a best 2608 * guess at determining it: try 64-bit swap first (ie., file 2609 * created on a 64-bit host), and check if the hostname feature 2610 * bit is set (this feature bit is forced on as of fbe96f2). 2611 * If the bit is not, undo the 64-bit swap and try a 32-bit 2612 * swap. If the hostname bit is still not set (e.g., older data 2613 * file), punt and fallback to the original behavior -- 2614 * clearing all feature bits and setting buildid. 2615 */ 2616 mem_bswap_64(&header->adds_features, 2617 BITS_TO_U64(HEADER_FEAT_BITS)); 2618 2619 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 2620 /* unswap as u64 */ 2621 mem_bswap_64(&header->adds_features, 2622 BITS_TO_U64(HEADER_FEAT_BITS)); 2623 2624 /* unswap as u32 */ 2625 mem_bswap_32(&header->adds_features, 2626 BITS_TO_U32(HEADER_FEAT_BITS)); 2627 } 2628 2629 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 2630 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 2631 set_bit(HEADER_BUILD_ID, header->adds_features); 2632 } 2633 } 2634 2635 memcpy(&ph->adds_features, &header->adds_features, 2636 sizeof(ph->adds_features)); 2637 2638 ph->data_offset = header->data.offset; 2639 ph->data_size = header->data.size; 2640 ph->feat_offset = header->data.offset + header->data.size; 2641 return 0; 2642 } 2643 2644 static int perf_file_section__process(struct perf_file_section *section, 2645 struct perf_header *ph, 2646 int feat, int fd, void *data) 2647 { 2648 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 2649 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 2650 "%d, continuing...\n", section->offset, feat); 2651 return 0; 2652 } 2653 2654 if (feat >= HEADER_LAST_FEATURE) { 2655 pr_debug("unknown feature %d, continuing...\n", feat); 2656 return 0; 2657 } 2658 2659 if (!feat_ops[feat].process) 2660 return 0; 2661 2662 return feat_ops[feat].process(section, ph, fd, data); 2663 } 2664 2665 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, 2666 struct perf_header *ph, int fd, 2667 bool repipe) 2668 { 2669 ssize_t ret; 2670 2671 ret = readn(fd, header, sizeof(*header)); 2672 if (ret <= 0) 2673 return -1; 2674 2675 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { 2676 pr_debug("endian/magic failed\n"); 2677 return -1; 2678 } 2679 2680 if (ph->needs_swap) 2681 header->size = bswap_64(header->size); 2682 2683 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) 2684 return -1; 2685 2686 return 0; 2687 } 2688 2689 static int perf_header__read_pipe(struct perf_session *session) 2690 { 2691 struct perf_header *header = &session->header; 2692 struct perf_pipe_file_header f_header; 2693 2694 if (perf_file_header__read_pipe(&f_header, header, 2695 perf_data_file__fd(session->file), 2696 session->repipe) < 0) { 2697 pr_debug("incompatible file format\n"); 2698 return -EINVAL; 2699 } 2700 2701 return 0; 2702 } 2703 2704 static int read_attr(int fd, struct perf_header *ph, 2705 struct perf_file_attr *f_attr) 2706 { 2707 struct perf_event_attr *attr = &f_attr->attr; 2708 size_t sz, left; 2709 size_t our_sz = sizeof(f_attr->attr); 2710 ssize_t ret; 2711 2712 memset(f_attr, 0, sizeof(*f_attr)); 2713 2714 /* read minimal guaranteed structure */ 2715 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); 2716 if (ret <= 0) { 2717 pr_debug("cannot read %d bytes of header attr\n", 2718 PERF_ATTR_SIZE_VER0); 2719 return -1; 2720 } 2721 2722 /* on file perf_event_attr size */ 2723 sz = attr->size; 2724 2725 if (ph->needs_swap) 2726 sz = bswap_32(sz); 2727 2728 if (sz == 0) { 2729 /* assume ABI0 */ 2730 sz = PERF_ATTR_SIZE_VER0; 2731 } else if (sz > our_sz) { 2732 pr_debug("file uses a more recent and unsupported ABI" 2733 " (%zu bytes extra)\n", sz - our_sz); 2734 return -1; 2735 } 2736 /* what we have not yet read and that we know about */ 2737 left = sz - PERF_ATTR_SIZE_VER0; 2738 if (left) { 2739 void *ptr = attr; 2740 ptr += PERF_ATTR_SIZE_VER0; 2741 2742 ret = readn(fd, ptr, left); 2743 } 2744 /* read perf_file_section, ids are read in caller */ 2745 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); 2746 2747 return ret <= 0 ? -1 : 0; 2748 } 2749 2750 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel, 2751 struct pevent *pevent) 2752 { 2753 struct event_format *event; 2754 char bf[128]; 2755 2756 /* already prepared */ 2757 if (evsel->tp_format) 2758 return 0; 2759 2760 if (pevent == NULL) { 2761 pr_debug("broken or missing trace data\n"); 2762 return -1; 2763 } 2764 2765 event = pevent_find_event(pevent, evsel->attr.config); 2766 if (event == NULL) 2767 return -1; 2768 2769 if (!evsel->name) { 2770 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); 2771 evsel->name = strdup(bf); 2772 if (evsel->name == NULL) 2773 return -1; 2774 } 2775 2776 evsel->tp_format = event; 2777 return 0; 2778 } 2779 2780 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist, 2781 struct pevent *pevent) 2782 { 2783 struct perf_evsel *pos; 2784 2785 evlist__for_each(evlist, pos) { 2786 if (pos->attr.type == PERF_TYPE_TRACEPOINT && 2787 perf_evsel__prepare_tracepoint_event(pos, pevent)) 2788 return -1; 2789 } 2790 2791 return 0; 2792 } 2793 2794 int perf_session__read_header(struct perf_session *session) 2795 { 2796 struct perf_data_file *file = session->file; 2797 struct perf_header *header = &session->header; 2798 struct perf_file_header f_header; 2799 struct perf_file_attr f_attr; 2800 u64 f_id; 2801 int nr_attrs, nr_ids, i, j; 2802 int fd = perf_data_file__fd(file); 2803 2804 session->evlist = perf_evlist__new(); 2805 if (session->evlist == NULL) 2806 return -ENOMEM; 2807 2808 if (perf_data_file__is_pipe(file)) 2809 return perf_header__read_pipe(session); 2810 2811 if (perf_file_header__read(&f_header, header, fd) < 0) 2812 return -EINVAL; 2813 2814 /* 2815 * Sanity check that perf.data was written cleanly; data size is 2816 * initialized to 0 and updated only if the on_exit function is run. 2817 * If data size is still 0 then the file contains only partial 2818 * information. Just warn user and process it as much as it can. 2819 */ 2820 if (f_header.data.size == 0) { 2821 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n" 2822 "Was the 'perf record' command properly terminated?\n", 2823 file->path); 2824 } 2825 2826 nr_attrs = f_header.attrs.size / f_header.attr_size; 2827 lseek(fd, f_header.attrs.offset, SEEK_SET); 2828 2829 for (i = 0; i < nr_attrs; i++) { 2830 struct perf_evsel *evsel; 2831 off_t tmp; 2832 2833 if (read_attr(fd, header, &f_attr) < 0) 2834 goto out_errno; 2835 2836 if (header->needs_swap) 2837 perf_event__attr_swap(&f_attr.attr); 2838 2839 tmp = lseek(fd, 0, SEEK_CUR); 2840 evsel = perf_evsel__new(&f_attr.attr); 2841 2842 if (evsel == NULL) 2843 goto out_delete_evlist; 2844 2845 evsel->needs_swap = header->needs_swap; 2846 /* 2847 * Do it before so that if perf_evsel__alloc_id fails, this 2848 * entry gets purged too at perf_evlist__delete(). 2849 */ 2850 perf_evlist__add(session->evlist, evsel); 2851 2852 nr_ids = f_attr.ids.size / sizeof(u64); 2853 /* 2854 * We don't have the cpu and thread maps on the header, so 2855 * for allocating the perf_sample_id table we fake 1 cpu and 2856 * hattr->ids threads. 2857 */ 2858 if (perf_evsel__alloc_id(evsel, 1, nr_ids)) 2859 goto out_delete_evlist; 2860 2861 lseek(fd, f_attr.ids.offset, SEEK_SET); 2862 2863 for (j = 0; j < nr_ids; j++) { 2864 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) 2865 goto out_errno; 2866 2867 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); 2868 } 2869 2870 lseek(fd, tmp, SEEK_SET); 2871 } 2872 2873 symbol_conf.nr_events = nr_attrs; 2874 2875 perf_header__process_sections(header, fd, &session->tevent, 2876 perf_file_section__process); 2877 2878 if (perf_evlist__prepare_tracepoint_events(session->evlist, 2879 session->tevent.pevent)) 2880 goto out_delete_evlist; 2881 2882 return 0; 2883 out_errno: 2884 return -errno; 2885 2886 out_delete_evlist: 2887 perf_evlist__delete(session->evlist); 2888 session->evlist = NULL; 2889 return -ENOMEM; 2890 } 2891 2892 int perf_event__synthesize_attr(struct perf_tool *tool, 2893 struct perf_event_attr *attr, u32 ids, u64 *id, 2894 perf_event__handler_t process) 2895 { 2896 union perf_event *ev; 2897 size_t size; 2898 int err; 2899 2900 size = sizeof(struct perf_event_attr); 2901 size = PERF_ALIGN(size, sizeof(u64)); 2902 size += sizeof(struct perf_event_header); 2903 size += ids * sizeof(u64); 2904 2905 ev = malloc(size); 2906 2907 if (ev == NULL) 2908 return -ENOMEM; 2909 2910 ev->attr.attr = *attr; 2911 memcpy(ev->attr.id, id, ids * sizeof(u64)); 2912 2913 ev->attr.header.type = PERF_RECORD_HEADER_ATTR; 2914 ev->attr.header.size = (u16)size; 2915 2916 if (ev->attr.header.size == size) 2917 err = process(tool, ev, NULL, NULL); 2918 else 2919 err = -E2BIG; 2920 2921 free(ev); 2922 2923 return err; 2924 } 2925 2926 int perf_event__synthesize_attrs(struct perf_tool *tool, 2927 struct perf_session *session, 2928 perf_event__handler_t process) 2929 { 2930 struct perf_evsel *evsel; 2931 int err = 0; 2932 2933 evlist__for_each(session->evlist, evsel) { 2934 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids, 2935 evsel->id, process); 2936 if (err) { 2937 pr_debug("failed to create perf header attribute\n"); 2938 return err; 2939 } 2940 } 2941 2942 return err; 2943 } 2944 2945 int perf_event__process_attr(struct perf_tool *tool __maybe_unused, 2946 union perf_event *event, 2947 struct perf_evlist **pevlist) 2948 { 2949 u32 i, ids, n_ids; 2950 struct perf_evsel *evsel; 2951 struct perf_evlist *evlist = *pevlist; 2952 2953 if (evlist == NULL) { 2954 *pevlist = evlist = perf_evlist__new(); 2955 if (evlist == NULL) 2956 return -ENOMEM; 2957 } 2958 2959 evsel = perf_evsel__new(&event->attr.attr); 2960 if (evsel == NULL) 2961 return -ENOMEM; 2962 2963 perf_evlist__add(evlist, evsel); 2964 2965 ids = event->header.size; 2966 ids -= (void *)&event->attr.id - (void *)event; 2967 n_ids = ids / sizeof(u64); 2968 /* 2969 * We don't have the cpu and thread maps on the header, so 2970 * for allocating the perf_sample_id table we fake 1 cpu and 2971 * hattr->ids threads. 2972 */ 2973 if (perf_evsel__alloc_id(evsel, 1, n_ids)) 2974 return -ENOMEM; 2975 2976 for (i = 0; i < n_ids; i++) { 2977 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); 2978 } 2979 2980 symbol_conf.nr_events = evlist->nr_entries; 2981 2982 return 0; 2983 } 2984 2985 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, 2986 struct perf_evlist *evlist, 2987 perf_event__handler_t process) 2988 { 2989 union perf_event ev; 2990 struct tracing_data *tdata; 2991 ssize_t size = 0, aligned_size = 0, padding; 2992 int err __maybe_unused = 0; 2993 2994 /* 2995 * We are going to store the size of the data followed 2996 * by the data contents. Since the fd descriptor is a pipe, 2997 * we cannot seek back to store the size of the data once 2998 * we know it. Instead we: 2999 * 3000 * - write the tracing data to the temp file 3001 * - get/write the data size to pipe 3002 * - write the tracing data from the temp file 3003 * to the pipe 3004 */ 3005 tdata = tracing_data_get(&evlist->entries, fd, true); 3006 if (!tdata) 3007 return -1; 3008 3009 memset(&ev, 0, sizeof(ev)); 3010 3011 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; 3012 size = tdata->size; 3013 aligned_size = PERF_ALIGN(size, sizeof(u64)); 3014 padding = aligned_size - size; 3015 ev.tracing_data.header.size = sizeof(ev.tracing_data); 3016 ev.tracing_data.size = aligned_size; 3017 3018 process(tool, &ev, NULL, NULL); 3019 3020 /* 3021 * The put function will copy all the tracing data 3022 * stored in temp file to the pipe. 3023 */ 3024 tracing_data_put(tdata); 3025 3026 write_padded(fd, NULL, 0, padding); 3027 3028 return aligned_size; 3029 } 3030 3031 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused, 3032 union perf_event *event, 3033 struct perf_session *session) 3034 { 3035 ssize_t size_read, padding, size = event->tracing_data.size; 3036 int fd = perf_data_file__fd(session->file); 3037 off_t offset = lseek(fd, 0, SEEK_CUR); 3038 char buf[BUFSIZ]; 3039 3040 /* setup for reading amidst mmap */ 3041 lseek(fd, offset + sizeof(struct tracing_data_event), 3042 SEEK_SET); 3043 3044 size_read = trace_report(fd, &session->tevent, 3045 session->repipe); 3046 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; 3047 3048 if (readn(fd, buf, padding) < 0) { 3049 pr_err("%s: reading input file", __func__); 3050 return -1; 3051 } 3052 if (session->repipe) { 3053 int retw = write(STDOUT_FILENO, buf, padding); 3054 if (retw <= 0 || retw != padding) { 3055 pr_err("%s: repiping tracing data padding", __func__); 3056 return -1; 3057 } 3058 } 3059 3060 if (size_read + padding != size) { 3061 pr_err("%s: tracing data size mismatch", __func__); 3062 return -1; 3063 } 3064 3065 perf_evlist__prepare_tracepoint_events(session->evlist, 3066 session->tevent.pevent); 3067 3068 return size_read + padding; 3069 } 3070 3071 int perf_event__synthesize_build_id(struct perf_tool *tool, 3072 struct dso *pos, u16 misc, 3073 perf_event__handler_t process, 3074 struct machine *machine) 3075 { 3076 union perf_event ev; 3077 size_t len; 3078 int err = 0; 3079 3080 if (!pos->hit) 3081 return err; 3082 3083 memset(&ev, 0, sizeof(ev)); 3084 3085 len = pos->long_name_len + 1; 3086 len = PERF_ALIGN(len, NAME_ALIGN); 3087 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); 3088 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; 3089 ev.build_id.header.misc = misc; 3090 ev.build_id.pid = machine->pid; 3091 ev.build_id.header.size = sizeof(ev.build_id) + len; 3092 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); 3093 3094 err = process(tool, &ev, NULL, machine); 3095 3096 return err; 3097 } 3098 3099 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused, 3100 union perf_event *event, 3101 struct perf_session *session) 3102 { 3103 __event_process_build_id(&event->build_id, 3104 event->build_id.filename, 3105 session); 3106 return 0; 3107 } 3108 3109 void disable_buildid_cache(void) 3110 { 3111 no_buildid_cache = true; 3112 } 3113