1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 #include "string2.h" 5 #include <sys/param.h> 6 #include <sys/types.h> 7 #include <byteswap.h> 8 #include <unistd.h> 9 #include <regex.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <linux/compiler.h> 13 #include <linux/list.h> 14 #include <linux/kernel.h> 15 #include <linux/bitops.h> 16 #include <linux/string.h> 17 #include <linux/stringify.h> 18 #include <linux/zalloc.h> 19 #include <sys/stat.h> 20 #include <sys/utsname.h> 21 #include <linux/time64.h> 22 #include <dirent.h> 23 #ifdef HAVE_LIBBPF_SUPPORT 24 #include <bpf/libbpf.h> 25 #endif 26 #include <perf/cpumap.h> 27 #include <tools/libc_compat.h> // reallocarray 28 29 #include "dso.h" 30 #include "evlist.h" 31 #include "evsel.h" 32 #include "util/evsel_fprintf.h" 33 #include "header.h" 34 #include "memswap.h" 35 #include "trace-event.h" 36 #include "session.h" 37 #include "symbol.h" 38 #include "debug.h" 39 #include "cpumap.h" 40 #include "pmu.h" 41 #include "pmus.h" 42 #include "vdso.h" 43 #include "strbuf.h" 44 #include "build-id.h" 45 #include "data.h" 46 #include <api/fs/fs.h> 47 #include "asm/bug.h" 48 #include "tool.h" 49 #include "time-utils.h" 50 #include "units.h" 51 #include "util/util.h" // perf_exe() 52 #include "cputopo.h" 53 #include "bpf-event.h" 54 #include "bpf-utils.h" 55 #include "clockid.h" 56 57 #include <linux/ctype.h> 58 #include <internal/lib.h> 59 60 #ifdef HAVE_LIBTRACEEVENT 61 #include <traceevent/event-parse.h> 62 #endif 63 64 /* 65 * magic2 = "PERFILE2" 66 * must be a numerical value to let the endianness 67 * determine the memory layout. That way we are able 68 * to detect endianness when reading the perf.data file 69 * back. 70 * 71 * we check for legacy (PERFFILE) format. 72 */ 73 static const char *__perf_magic1 = "PERFFILE"; 74 static const u64 __perf_magic2 = 0x32454c4946524550ULL; 75 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; 76 77 #define PERF_MAGIC __perf_magic2 78 79 const char perf_version_string[] = PERF_VERSION; 80 81 struct perf_file_attr { 82 struct perf_event_attr attr; 83 struct perf_file_section ids; 84 }; 85 86 void perf_header__set_feat(struct perf_header *header, int feat) 87 { 88 __set_bit(feat, header->adds_features); 89 } 90 91 void perf_header__clear_feat(struct perf_header *header, int feat) 92 { 93 __clear_bit(feat, header->adds_features); 94 } 95 96 bool perf_header__has_feat(const struct perf_header *header, int feat) 97 { 98 return test_bit(feat, header->adds_features); 99 } 100 101 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size) 102 { 103 ssize_t ret = writen(ff->fd, buf, size); 104 105 if (ret != (ssize_t)size) 106 return ret < 0 ? (int)ret : -1; 107 return 0; 108 } 109 110 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size) 111 { 112 /* struct perf_event_header::size is u16 */ 113 const size_t max_size = 0xffff - sizeof(struct perf_event_header); 114 size_t new_size = ff->size; 115 void *addr; 116 117 if (size + ff->offset > max_size) 118 return -E2BIG; 119 120 while (size > (new_size - ff->offset)) 121 new_size <<= 1; 122 new_size = min(max_size, new_size); 123 124 if (ff->size < new_size) { 125 addr = realloc(ff->buf, new_size); 126 if (!addr) 127 return -ENOMEM; 128 ff->buf = addr; 129 ff->size = new_size; 130 } 131 132 memcpy(ff->buf + ff->offset, buf, size); 133 ff->offset += size; 134 135 return 0; 136 } 137 138 /* Return: 0 if succeeded, -ERR if failed. */ 139 int do_write(struct feat_fd *ff, const void *buf, size_t size) 140 { 141 if (!ff->buf) 142 return __do_write_fd(ff, buf, size); 143 return __do_write_buf(ff, buf, size); 144 } 145 146 /* Return: 0 if succeeded, -ERR if failed. */ 147 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size) 148 { 149 u64 *p = (u64 *) set; 150 int i, ret; 151 152 ret = do_write(ff, &size, sizeof(size)); 153 if (ret < 0) 154 return ret; 155 156 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 157 ret = do_write(ff, p + i, sizeof(*p)); 158 if (ret < 0) 159 return ret; 160 } 161 162 return 0; 163 } 164 165 /* Return: 0 if succeeded, -ERR if failed. */ 166 int write_padded(struct feat_fd *ff, const void *bf, 167 size_t count, size_t count_aligned) 168 { 169 static const char zero_buf[NAME_ALIGN]; 170 int err = do_write(ff, bf, count); 171 172 if (!err) 173 err = do_write(ff, zero_buf, count_aligned - count); 174 175 return err; 176 } 177 178 #define string_size(str) \ 179 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32)) 180 181 /* Return: 0 if succeeded, -ERR if failed. */ 182 static int do_write_string(struct feat_fd *ff, const char *str) 183 { 184 u32 len, olen; 185 int ret; 186 187 olen = strlen(str) + 1; 188 len = PERF_ALIGN(olen, NAME_ALIGN); 189 190 /* write len, incl. \0 */ 191 ret = do_write(ff, &len, sizeof(len)); 192 if (ret < 0) 193 return ret; 194 195 return write_padded(ff, str, olen, len); 196 } 197 198 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size) 199 { 200 ssize_t ret = readn(ff->fd, addr, size); 201 202 if (ret != size) 203 return ret < 0 ? (int)ret : -1; 204 return 0; 205 } 206 207 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size) 208 { 209 if (size > (ssize_t)ff->size - ff->offset) 210 return -1; 211 212 memcpy(addr, ff->buf + ff->offset, size); 213 ff->offset += size; 214 215 return 0; 216 217 } 218 219 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size) 220 { 221 if (!ff->buf) 222 return __do_read_fd(ff, addr, size); 223 return __do_read_buf(ff, addr, size); 224 } 225 226 static int do_read_u32(struct feat_fd *ff, u32 *addr) 227 { 228 int ret; 229 230 ret = __do_read(ff, addr, sizeof(*addr)); 231 if (ret) 232 return ret; 233 234 if (ff->ph->needs_swap) 235 *addr = bswap_32(*addr); 236 return 0; 237 } 238 239 static int do_read_u64(struct feat_fd *ff, u64 *addr) 240 { 241 int ret; 242 243 ret = __do_read(ff, addr, sizeof(*addr)); 244 if (ret) 245 return ret; 246 247 if (ff->ph->needs_swap) 248 *addr = bswap_64(*addr); 249 return 0; 250 } 251 252 static char *do_read_string(struct feat_fd *ff) 253 { 254 u32 len; 255 char *buf; 256 257 if (do_read_u32(ff, &len)) 258 return NULL; 259 260 buf = malloc(len); 261 if (!buf) 262 return NULL; 263 264 if (!__do_read(ff, buf, len)) { 265 /* 266 * strings are padded by zeroes 267 * thus the actual strlen of buf 268 * may be less than len 269 */ 270 return buf; 271 } 272 273 free(buf); 274 return NULL; 275 } 276 277 /* Return: 0 if succeeded, -ERR if failed. */ 278 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize) 279 { 280 unsigned long *set; 281 u64 size, *p; 282 int i, ret; 283 284 ret = do_read_u64(ff, &size); 285 if (ret) 286 return ret; 287 288 set = bitmap_zalloc(size); 289 if (!set) 290 return -ENOMEM; 291 292 p = (u64 *) set; 293 294 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 295 ret = do_read_u64(ff, p + i); 296 if (ret < 0) { 297 free(set); 298 return ret; 299 } 300 } 301 302 *pset = set; 303 *psize = size; 304 return 0; 305 } 306 307 #ifdef HAVE_LIBTRACEEVENT 308 static int write_tracing_data(struct feat_fd *ff, 309 struct evlist *evlist) 310 { 311 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 312 return -1; 313 314 return read_tracing_data(ff->fd, &evlist->core.entries); 315 } 316 #endif 317 318 static int write_build_id(struct feat_fd *ff, 319 struct evlist *evlist __maybe_unused) 320 { 321 struct perf_session *session; 322 int err; 323 324 session = container_of(ff->ph, struct perf_session, header); 325 326 if (!perf_session__read_build_ids(session, true)) 327 return -1; 328 329 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 330 return -1; 331 332 err = perf_session__write_buildid_table(session, ff); 333 if (err < 0) { 334 pr_debug("failed to write buildid table\n"); 335 return err; 336 } 337 perf_session__cache_build_ids(session); 338 339 return 0; 340 } 341 342 static int write_hostname(struct feat_fd *ff, 343 struct evlist *evlist __maybe_unused) 344 { 345 struct utsname uts; 346 int ret; 347 348 ret = uname(&uts); 349 if (ret < 0) 350 return -1; 351 352 return do_write_string(ff, uts.nodename); 353 } 354 355 static int write_osrelease(struct feat_fd *ff, 356 struct evlist *evlist __maybe_unused) 357 { 358 struct utsname uts; 359 int ret; 360 361 ret = uname(&uts); 362 if (ret < 0) 363 return -1; 364 365 return do_write_string(ff, uts.release); 366 } 367 368 static int write_arch(struct feat_fd *ff, 369 struct evlist *evlist __maybe_unused) 370 { 371 struct utsname uts; 372 int ret; 373 374 ret = uname(&uts); 375 if (ret < 0) 376 return -1; 377 378 return do_write_string(ff, uts.machine); 379 } 380 381 static int write_version(struct feat_fd *ff, 382 struct evlist *evlist __maybe_unused) 383 { 384 return do_write_string(ff, perf_version_string); 385 } 386 387 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc) 388 { 389 FILE *file; 390 char *buf = NULL; 391 char *s, *p; 392 const char *search = cpuinfo_proc; 393 size_t len = 0; 394 int ret = -1; 395 396 if (!search) 397 return -1; 398 399 file = fopen("/proc/cpuinfo", "r"); 400 if (!file) 401 return -1; 402 403 while (getline(&buf, &len, file) > 0) { 404 ret = strncmp(buf, search, strlen(search)); 405 if (!ret) 406 break; 407 } 408 409 if (ret) { 410 ret = -1; 411 goto done; 412 } 413 414 s = buf; 415 416 p = strchr(buf, ':'); 417 if (p && *(p+1) == ' ' && *(p+2)) 418 s = p + 2; 419 p = strchr(s, '\n'); 420 if (p) 421 *p = '\0'; 422 423 /* squash extra space characters (branding string) */ 424 p = s; 425 while (*p) { 426 if (isspace(*p)) { 427 char *r = p + 1; 428 char *q = skip_spaces(r); 429 *p = ' '; 430 if (q != (p+1)) 431 while ((*r++ = *q++)); 432 } 433 p++; 434 } 435 ret = do_write_string(ff, s); 436 done: 437 free(buf); 438 fclose(file); 439 return ret; 440 } 441 442 static int write_cpudesc(struct feat_fd *ff, 443 struct evlist *evlist __maybe_unused) 444 { 445 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__) 446 #define CPUINFO_PROC { "cpu", } 447 #elif defined(__s390__) 448 #define CPUINFO_PROC { "vendor_id", } 449 #elif defined(__sh__) 450 #define CPUINFO_PROC { "cpu type", } 451 #elif defined(__alpha__) || defined(__mips__) 452 #define CPUINFO_PROC { "cpu model", } 453 #elif defined(__arm__) 454 #define CPUINFO_PROC { "model name", "Processor", } 455 #elif defined(__arc__) 456 #define CPUINFO_PROC { "Processor", } 457 #elif defined(__xtensa__) 458 #define CPUINFO_PROC { "core ID", } 459 #else 460 #define CPUINFO_PROC { "model name", } 461 #endif 462 const char *cpuinfo_procs[] = CPUINFO_PROC; 463 #undef CPUINFO_PROC 464 unsigned int i; 465 466 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) { 467 int ret; 468 ret = __write_cpudesc(ff, cpuinfo_procs[i]); 469 if (ret >= 0) 470 return ret; 471 } 472 return -1; 473 } 474 475 476 static int write_nrcpus(struct feat_fd *ff, 477 struct evlist *evlist __maybe_unused) 478 { 479 long nr; 480 u32 nrc, nra; 481 int ret; 482 483 nrc = cpu__max_present_cpu().cpu; 484 485 nr = sysconf(_SC_NPROCESSORS_ONLN); 486 if (nr < 0) 487 return -1; 488 489 nra = (u32)(nr & UINT_MAX); 490 491 ret = do_write(ff, &nrc, sizeof(nrc)); 492 if (ret < 0) 493 return ret; 494 495 return do_write(ff, &nra, sizeof(nra)); 496 } 497 498 static int write_event_desc(struct feat_fd *ff, 499 struct evlist *evlist) 500 { 501 struct evsel *evsel; 502 u32 nre, nri, sz; 503 int ret; 504 505 nre = evlist->core.nr_entries; 506 507 /* 508 * write number of events 509 */ 510 ret = do_write(ff, &nre, sizeof(nre)); 511 if (ret < 0) 512 return ret; 513 514 /* 515 * size of perf_event_attr struct 516 */ 517 sz = (u32)sizeof(evsel->core.attr); 518 ret = do_write(ff, &sz, sizeof(sz)); 519 if (ret < 0) 520 return ret; 521 522 evlist__for_each_entry(evlist, evsel) { 523 ret = do_write(ff, &evsel->core.attr, sz); 524 if (ret < 0) 525 return ret; 526 /* 527 * write number of unique id per event 528 * there is one id per instance of an event 529 * 530 * copy into an nri to be independent of the 531 * type of ids, 532 */ 533 nri = evsel->core.ids; 534 ret = do_write(ff, &nri, sizeof(nri)); 535 if (ret < 0) 536 return ret; 537 538 /* 539 * write event string as passed on cmdline 540 */ 541 ret = do_write_string(ff, evsel__name(evsel)); 542 if (ret < 0) 543 return ret; 544 /* 545 * write unique ids for this event 546 */ 547 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64)); 548 if (ret < 0) 549 return ret; 550 } 551 return 0; 552 } 553 554 static int write_cmdline(struct feat_fd *ff, 555 struct evlist *evlist __maybe_unused) 556 { 557 char pbuf[MAXPATHLEN], *buf; 558 int i, ret, n; 559 560 /* actual path to perf binary */ 561 buf = perf_exe(pbuf, MAXPATHLEN); 562 563 /* account for binary path */ 564 n = perf_env.nr_cmdline + 1; 565 566 ret = do_write(ff, &n, sizeof(n)); 567 if (ret < 0) 568 return ret; 569 570 ret = do_write_string(ff, buf); 571 if (ret < 0) 572 return ret; 573 574 for (i = 0 ; i < perf_env.nr_cmdline; i++) { 575 ret = do_write_string(ff, perf_env.cmdline_argv[i]); 576 if (ret < 0) 577 return ret; 578 } 579 return 0; 580 } 581 582 583 static int write_cpu_topology(struct feat_fd *ff, 584 struct evlist *evlist __maybe_unused) 585 { 586 struct cpu_topology *tp; 587 u32 i; 588 int ret, j; 589 590 tp = cpu_topology__new(); 591 if (!tp) 592 return -1; 593 594 ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists)); 595 if (ret < 0) 596 goto done; 597 598 for (i = 0; i < tp->package_cpus_lists; i++) { 599 ret = do_write_string(ff, tp->package_cpus_list[i]); 600 if (ret < 0) 601 goto done; 602 } 603 ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists)); 604 if (ret < 0) 605 goto done; 606 607 for (i = 0; i < tp->core_cpus_lists; i++) { 608 ret = do_write_string(ff, tp->core_cpus_list[i]); 609 if (ret < 0) 610 break; 611 } 612 613 ret = perf_env__read_cpu_topology_map(&perf_env); 614 if (ret < 0) 615 goto done; 616 617 for (j = 0; j < perf_env.nr_cpus_avail; j++) { 618 ret = do_write(ff, &perf_env.cpu[j].core_id, 619 sizeof(perf_env.cpu[j].core_id)); 620 if (ret < 0) 621 return ret; 622 ret = do_write(ff, &perf_env.cpu[j].socket_id, 623 sizeof(perf_env.cpu[j].socket_id)); 624 if (ret < 0) 625 return ret; 626 } 627 628 if (!tp->die_cpus_lists) 629 goto done; 630 631 ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists)); 632 if (ret < 0) 633 goto done; 634 635 for (i = 0; i < tp->die_cpus_lists; i++) { 636 ret = do_write_string(ff, tp->die_cpus_list[i]); 637 if (ret < 0) 638 goto done; 639 } 640 641 for (j = 0; j < perf_env.nr_cpus_avail; j++) { 642 ret = do_write(ff, &perf_env.cpu[j].die_id, 643 sizeof(perf_env.cpu[j].die_id)); 644 if (ret < 0) 645 return ret; 646 } 647 648 done: 649 cpu_topology__delete(tp); 650 return ret; 651 } 652 653 654 655 static int write_total_mem(struct feat_fd *ff, 656 struct evlist *evlist __maybe_unused) 657 { 658 char *buf = NULL; 659 FILE *fp; 660 size_t len = 0; 661 int ret = -1, n; 662 uint64_t mem; 663 664 fp = fopen("/proc/meminfo", "r"); 665 if (!fp) 666 return -1; 667 668 while (getline(&buf, &len, fp) > 0) { 669 ret = strncmp(buf, "MemTotal:", 9); 670 if (!ret) 671 break; 672 } 673 if (!ret) { 674 n = sscanf(buf, "%*s %"PRIu64, &mem); 675 if (n == 1) 676 ret = do_write(ff, &mem, sizeof(mem)); 677 } else 678 ret = -1; 679 free(buf); 680 fclose(fp); 681 return ret; 682 } 683 684 static int write_numa_topology(struct feat_fd *ff, 685 struct evlist *evlist __maybe_unused) 686 { 687 struct numa_topology *tp; 688 int ret = -1; 689 u32 i; 690 691 tp = numa_topology__new(); 692 if (!tp) 693 return -ENOMEM; 694 695 ret = do_write(ff, &tp->nr, sizeof(u32)); 696 if (ret < 0) 697 goto err; 698 699 for (i = 0; i < tp->nr; i++) { 700 struct numa_topology_node *n = &tp->nodes[i]; 701 702 ret = do_write(ff, &n->node, sizeof(u32)); 703 if (ret < 0) 704 goto err; 705 706 ret = do_write(ff, &n->mem_total, sizeof(u64)); 707 if (ret) 708 goto err; 709 710 ret = do_write(ff, &n->mem_free, sizeof(u64)); 711 if (ret) 712 goto err; 713 714 ret = do_write_string(ff, n->cpus); 715 if (ret < 0) 716 goto err; 717 } 718 719 ret = 0; 720 721 err: 722 numa_topology__delete(tp); 723 return ret; 724 } 725 726 /* 727 * File format: 728 * 729 * struct pmu_mappings { 730 * u32 pmu_num; 731 * struct pmu_map { 732 * u32 type; 733 * char name[]; 734 * }[pmu_num]; 735 * }; 736 */ 737 738 static int write_pmu_mappings(struct feat_fd *ff, 739 struct evlist *evlist __maybe_unused) 740 { 741 struct perf_pmu *pmu = NULL; 742 u32 pmu_num = 0; 743 int ret; 744 745 /* 746 * Do a first pass to count number of pmu to avoid lseek so this 747 * works in pipe mode as well. 748 */ 749 while ((pmu = perf_pmus__scan(pmu))) { 750 if (!pmu->name) 751 continue; 752 pmu_num++; 753 } 754 755 ret = do_write(ff, &pmu_num, sizeof(pmu_num)); 756 if (ret < 0) 757 return ret; 758 759 while ((pmu = perf_pmus__scan(pmu))) { 760 if (!pmu->name) 761 continue; 762 763 ret = do_write(ff, &pmu->type, sizeof(pmu->type)); 764 if (ret < 0) 765 return ret; 766 767 ret = do_write_string(ff, pmu->name); 768 if (ret < 0) 769 return ret; 770 } 771 772 return 0; 773 } 774 775 /* 776 * File format: 777 * 778 * struct group_descs { 779 * u32 nr_groups; 780 * struct group_desc { 781 * char name[]; 782 * u32 leader_idx; 783 * u32 nr_members; 784 * }[nr_groups]; 785 * }; 786 */ 787 static int write_group_desc(struct feat_fd *ff, 788 struct evlist *evlist) 789 { 790 u32 nr_groups = evlist__nr_groups(evlist); 791 struct evsel *evsel; 792 int ret; 793 794 ret = do_write(ff, &nr_groups, sizeof(nr_groups)); 795 if (ret < 0) 796 return ret; 797 798 evlist__for_each_entry(evlist, evsel) { 799 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { 800 const char *name = evsel->group_name ?: "{anon_group}"; 801 u32 leader_idx = evsel->core.idx; 802 u32 nr_members = evsel->core.nr_members; 803 804 ret = do_write_string(ff, name); 805 if (ret < 0) 806 return ret; 807 808 ret = do_write(ff, &leader_idx, sizeof(leader_idx)); 809 if (ret < 0) 810 return ret; 811 812 ret = do_write(ff, &nr_members, sizeof(nr_members)); 813 if (ret < 0) 814 return ret; 815 } 816 } 817 return 0; 818 } 819 820 /* 821 * Return the CPU id as a raw string. 822 * 823 * Each architecture should provide a more precise id string that 824 * can be use to match the architecture's "mapfile". 825 */ 826 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 827 { 828 return NULL; 829 } 830 831 /* Return zero when the cpuid from the mapfile.csv matches the 832 * cpuid string generated on this platform. 833 * Otherwise return non-zero. 834 */ 835 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) 836 { 837 regex_t re; 838 regmatch_t pmatch[1]; 839 int match; 840 841 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) { 842 /* Warn unable to generate match particular string. */ 843 pr_info("Invalid regular expression %s\n", mapcpuid); 844 return 1; 845 } 846 847 match = !regexec(&re, cpuid, 1, pmatch, 0); 848 regfree(&re); 849 if (match) { 850 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); 851 852 /* Verify the entire string matched. */ 853 if (match_len == strlen(cpuid)) 854 return 0; 855 } 856 return 1; 857 } 858 859 /* 860 * default get_cpuid(): nothing gets recorded 861 * actual implementation must be in arch/$(SRCARCH)/util/header.c 862 */ 863 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused) 864 { 865 return ENOSYS; /* Not implemented */ 866 } 867 868 static int write_cpuid(struct feat_fd *ff, 869 struct evlist *evlist __maybe_unused) 870 { 871 char buffer[64]; 872 int ret; 873 874 ret = get_cpuid(buffer, sizeof(buffer)); 875 if (ret) 876 return -1; 877 878 return do_write_string(ff, buffer); 879 } 880 881 static int write_branch_stack(struct feat_fd *ff __maybe_unused, 882 struct evlist *evlist __maybe_unused) 883 { 884 return 0; 885 } 886 887 static int write_auxtrace(struct feat_fd *ff, 888 struct evlist *evlist __maybe_unused) 889 { 890 struct perf_session *session; 891 int err; 892 893 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 894 return -1; 895 896 session = container_of(ff->ph, struct perf_session, header); 897 898 err = auxtrace_index__write(ff->fd, &session->auxtrace_index); 899 if (err < 0) 900 pr_err("Failed to write auxtrace index\n"); 901 return err; 902 } 903 904 static int write_clockid(struct feat_fd *ff, 905 struct evlist *evlist __maybe_unused) 906 { 907 return do_write(ff, &ff->ph->env.clock.clockid_res_ns, 908 sizeof(ff->ph->env.clock.clockid_res_ns)); 909 } 910 911 static int write_clock_data(struct feat_fd *ff, 912 struct evlist *evlist __maybe_unused) 913 { 914 u64 *data64; 915 u32 data32; 916 int ret; 917 918 /* version */ 919 data32 = 1; 920 921 ret = do_write(ff, &data32, sizeof(data32)); 922 if (ret < 0) 923 return ret; 924 925 /* clockid */ 926 data32 = ff->ph->env.clock.clockid; 927 928 ret = do_write(ff, &data32, sizeof(data32)); 929 if (ret < 0) 930 return ret; 931 932 /* TOD ref time */ 933 data64 = &ff->ph->env.clock.tod_ns; 934 935 ret = do_write(ff, data64, sizeof(*data64)); 936 if (ret < 0) 937 return ret; 938 939 /* clockid ref time */ 940 data64 = &ff->ph->env.clock.clockid_ns; 941 942 return do_write(ff, data64, sizeof(*data64)); 943 } 944 945 static int write_hybrid_topology(struct feat_fd *ff, 946 struct evlist *evlist __maybe_unused) 947 { 948 struct hybrid_topology *tp; 949 int ret; 950 u32 i; 951 952 tp = hybrid_topology__new(); 953 if (!tp) 954 return -ENOENT; 955 956 ret = do_write(ff, &tp->nr, sizeof(u32)); 957 if (ret < 0) 958 goto err; 959 960 for (i = 0; i < tp->nr; i++) { 961 struct hybrid_topology_node *n = &tp->nodes[i]; 962 963 ret = do_write_string(ff, n->pmu_name); 964 if (ret < 0) 965 goto err; 966 967 ret = do_write_string(ff, n->cpus); 968 if (ret < 0) 969 goto err; 970 } 971 972 ret = 0; 973 974 err: 975 hybrid_topology__delete(tp); 976 return ret; 977 } 978 979 static int write_dir_format(struct feat_fd *ff, 980 struct evlist *evlist __maybe_unused) 981 { 982 struct perf_session *session; 983 struct perf_data *data; 984 985 session = container_of(ff->ph, struct perf_session, header); 986 data = session->data; 987 988 if (WARN_ON(!perf_data__is_dir(data))) 989 return -1; 990 991 return do_write(ff, &data->dir.version, sizeof(data->dir.version)); 992 } 993 994 /* 995 * Check whether a CPU is online 996 * 997 * Returns: 998 * 1 -> if CPU is online 999 * 0 -> if CPU is offline 1000 * -1 -> error case 1001 */ 1002 int is_cpu_online(unsigned int cpu) 1003 { 1004 char *str; 1005 size_t strlen; 1006 char buf[256]; 1007 int status = -1; 1008 struct stat statbuf; 1009 1010 snprintf(buf, sizeof(buf), 1011 "/sys/devices/system/cpu/cpu%d", cpu); 1012 if (stat(buf, &statbuf) != 0) 1013 return 0; 1014 1015 /* 1016 * Check if /sys/devices/system/cpu/cpux/online file 1017 * exists. Some cases cpu0 won't have online file since 1018 * it is not expected to be turned off generally. 1019 * In kernels without CONFIG_HOTPLUG_CPU, this 1020 * file won't exist 1021 */ 1022 snprintf(buf, sizeof(buf), 1023 "/sys/devices/system/cpu/cpu%d/online", cpu); 1024 if (stat(buf, &statbuf) != 0) 1025 return 1; 1026 1027 /* 1028 * Read online file using sysfs__read_str. 1029 * If read or open fails, return -1. 1030 * If read succeeds, return value from file 1031 * which gets stored in "str" 1032 */ 1033 snprintf(buf, sizeof(buf), 1034 "devices/system/cpu/cpu%d/online", cpu); 1035 1036 if (sysfs__read_str(buf, &str, &strlen) < 0) 1037 return status; 1038 1039 status = atoi(str); 1040 1041 free(str); 1042 return status; 1043 } 1044 1045 #ifdef HAVE_LIBBPF_SUPPORT 1046 static int write_bpf_prog_info(struct feat_fd *ff, 1047 struct evlist *evlist __maybe_unused) 1048 { 1049 struct perf_env *env = &ff->ph->env; 1050 struct rb_root *root; 1051 struct rb_node *next; 1052 int ret; 1053 1054 down_read(&env->bpf_progs.lock); 1055 1056 ret = do_write(ff, &env->bpf_progs.infos_cnt, 1057 sizeof(env->bpf_progs.infos_cnt)); 1058 if (ret < 0) 1059 goto out; 1060 1061 root = &env->bpf_progs.infos; 1062 next = rb_first(root); 1063 while (next) { 1064 struct bpf_prog_info_node *node; 1065 size_t len; 1066 1067 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 1068 next = rb_next(&node->rb_node); 1069 len = sizeof(struct perf_bpil) + 1070 node->info_linear->data_len; 1071 1072 /* before writing to file, translate address to offset */ 1073 bpil_addr_to_offs(node->info_linear); 1074 ret = do_write(ff, node->info_linear, len); 1075 /* 1076 * translate back to address even when do_write() fails, 1077 * so that this function never changes the data. 1078 */ 1079 bpil_offs_to_addr(node->info_linear); 1080 if (ret < 0) 1081 goto out; 1082 } 1083 out: 1084 up_read(&env->bpf_progs.lock); 1085 return ret; 1086 } 1087 1088 static int write_bpf_btf(struct feat_fd *ff, 1089 struct evlist *evlist __maybe_unused) 1090 { 1091 struct perf_env *env = &ff->ph->env; 1092 struct rb_root *root; 1093 struct rb_node *next; 1094 int ret; 1095 1096 down_read(&env->bpf_progs.lock); 1097 1098 ret = do_write(ff, &env->bpf_progs.btfs_cnt, 1099 sizeof(env->bpf_progs.btfs_cnt)); 1100 1101 if (ret < 0) 1102 goto out; 1103 1104 root = &env->bpf_progs.btfs; 1105 next = rb_first(root); 1106 while (next) { 1107 struct btf_node *node; 1108 1109 node = rb_entry(next, struct btf_node, rb_node); 1110 next = rb_next(&node->rb_node); 1111 ret = do_write(ff, &node->id, 1112 sizeof(u32) * 2 + node->data_size); 1113 if (ret < 0) 1114 goto out; 1115 } 1116 out: 1117 up_read(&env->bpf_progs.lock); 1118 return ret; 1119 } 1120 #endif // HAVE_LIBBPF_SUPPORT 1121 1122 static int cpu_cache_level__sort(const void *a, const void *b) 1123 { 1124 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a; 1125 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b; 1126 1127 return cache_a->level - cache_b->level; 1128 } 1129 1130 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b) 1131 { 1132 if (a->level != b->level) 1133 return false; 1134 1135 if (a->line_size != b->line_size) 1136 return false; 1137 1138 if (a->sets != b->sets) 1139 return false; 1140 1141 if (a->ways != b->ways) 1142 return false; 1143 1144 if (strcmp(a->type, b->type)) 1145 return false; 1146 1147 if (strcmp(a->size, b->size)) 1148 return false; 1149 1150 if (strcmp(a->map, b->map)) 1151 return false; 1152 1153 return true; 1154 } 1155 1156 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level) 1157 { 1158 char path[PATH_MAX], file[PATH_MAX]; 1159 struct stat st; 1160 size_t len; 1161 1162 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level); 1163 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path); 1164 1165 if (stat(file, &st)) 1166 return 1; 1167 1168 scnprintf(file, PATH_MAX, "%s/level", path); 1169 if (sysfs__read_int(file, (int *) &cache->level)) 1170 return -1; 1171 1172 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path); 1173 if (sysfs__read_int(file, (int *) &cache->line_size)) 1174 return -1; 1175 1176 scnprintf(file, PATH_MAX, "%s/number_of_sets", path); 1177 if (sysfs__read_int(file, (int *) &cache->sets)) 1178 return -1; 1179 1180 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path); 1181 if (sysfs__read_int(file, (int *) &cache->ways)) 1182 return -1; 1183 1184 scnprintf(file, PATH_MAX, "%s/type", path); 1185 if (sysfs__read_str(file, &cache->type, &len)) 1186 return -1; 1187 1188 cache->type[len] = 0; 1189 cache->type = strim(cache->type); 1190 1191 scnprintf(file, PATH_MAX, "%s/size", path); 1192 if (sysfs__read_str(file, &cache->size, &len)) { 1193 zfree(&cache->type); 1194 return -1; 1195 } 1196 1197 cache->size[len] = 0; 1198 cache->size = strim(cache->size); 1199 1200 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path); 1201 if (sysfs__read_str(file, &cache->map, &len)) { 1202 zfree(&cache->size); 1203 zfree(&cache->type); 1204 return -1; 1205 } 1206 1207 cache->map[len] = 0; 1208 cache->map = strim(cache->map); 1209 return 0; 1210 } 1211 1212 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c) 1213 { 1214 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map); 1215 } 1216 1217 /* 1218 * Build caches levels for a particular CPU from the data in 1219 * /sys/devices/system/cpu/cpu<cpu>/cache/ 1220 * The cache level data is stored in caches[] from index at 1221 * *cntp. 1222 */ 1223 int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp) 1224 { 1225 u16 level; 1226 1227 for (level = 0; level < MAX_CACHE_LVL; level++) { 1228 struct cpu_cache_level c; 1229 int err; 1230 u32 i; 1231 1232 err = cpu_cache_level__read(&c, cpu, level); 1233 if (err < 0) 1234 return err; 1235 1236 if (err == 1) 1237 break; 1238 1239 for (i = 0; i < *cntp; i++) { 1240 if (cpu_cache_level__cmp(&c, &caches[i])) 1241 break; 1242 } 1243 1244 if (i == *cntp) { 1245 caches[*cntp] = c; 1246 *cntp = *cntp + 1; 1247 } else 1248 cpu_cache_level__free(&c); 1249 } 1250 1251 return 0; 1252 } 1253 1254 static int build_caches(struct cpu_cache_level caches[], u32 *cntp) 1255 { 1256 u32 nr, cpu, cnt = 0; 1257 1258 nr = cpu__max_cpu().cpu; 1259 1260 for (cpu = 0; cpu < nr; cpu++) { 1261 int ret = build_caches_for_cpu(cpu, caches, &cnt); 1262 1263 if (ret) 1264 return ret; 1265 } 1266 *cntp = cnt; 1267 return 0; 1268 } 1269 1270 static int write_cache(struct feat_fd *ff, 1271 struct evlist *evlist __maybe_unused) 1272 { 1273 u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL; 1274 struct cpu_cache_level caches[max_caches]; 1275 u32 cnt = 0, i, version = 1; 1276 int ret; 1277 1278 ret = build_caches(caches, &cnt); 1279 if (ret) 1280 goto out; 1281 1282 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort); 1283 1284 ret = do_write(ff, &version, sizeof(u32)); 1285 if (ret < 0) 1286 goto out; 1287 1288 ret = do_write(ff, &cnt, sizeof(u32)); 1289 if (ret < 0) 1290 goto out; 1291 1292 for (i = 0; i < cnt; i++) { 1293 struct cpu_cache_level *c = &caches[i]; 1294 1295 #define _W(v) \ 1296 ret = do_write(ff, &c->v, sizeof(u32)); \ 1297 if (ret < 0) \ 1298 goto out; 1299 1300 _W(level) 1301 _W(line_size) 1302 _W(sets) 1303 _W(ways) 1304 #undef _W 1305 1306 #define _W(v) \ 1307 ret = do_write_string(ff, (const char *) c->v); \ 1308 if (ret < 0) \ 1309 goto out; 1310 1311 _W(type) 1312 _W(size) 1313 _W(map) 1314 #undef _W 1315 } 1316 1317 out: 1318 for (i = 0; i < cnt; i++) 1319 cpu_cache_level__free(&caches[i]); 1320 return ret; 1321 } 1322 1323 static int write_stat(struct feat_fd *ff __maybe_unused, 1324 struct evlist *evlist __maybe_unused) 1325 { 1326 return 0; 1327 } 1328 1329 static int write_sample_time(struct feat_fd *ff, 1330 struct evlist *evlist) 1331 { 1332 int ret; 1333 1334 ret = do_write(ff, &evlist->first_sample_time, 1335 sizeof(evlist->first_sample_time)); 1336 if (ret < 0) 1337 return ret; 1338 1339 return do_write(ff, &evlist->last_sample_time, 1340 sizeof(evlist->last_sample_time)); 1341 } 1342 1343 1344 static int memory_node__read(struct memory_node *n, unsigned long idx) 1345 { 1346 unsigned int phys, size = 0; 1347 char path[PATH_MAX]; 1348 struct dirent *ent; 1349 DIR *dir; 1350 1351 #define for_each_memory(mem, dir) \ 1352 while ((ent = readdir(dir))) \ 1353 if (strcmp(ent->d_name, ".") && \ 1354 strcmp(ent->d_name, "..") && \ 1355 sscanf(ent->d_name, "memory%u", &mem) == 1) 1356 1357 scnprintf(path, PATH_MAX, 1358 "%s/devices/system/node/node%lu", 1359 sysfs__mountpoint(), idx); 1360 1361 dir = opendir(path); 1362 if (!dir) { 1363 pr_warning("failed: can't open memory sysfs data\n"); 1364 return -1; 1365 } 1366 1367 for_each_memory(phys, dir) { 1368 size = max(phys, size); 1369 } 1370 1371 size++; 1372 1373 n->set = bitmap_zalloc(size); 1374 if (!n->set) { 1375 closedir(dir); 1376 return -ENOMEM; 1377 } 1378 1379 n->node = idx; 1380 n->size = size; 1381 1382 rewinddir(dir); 1383 1384 for_each_memory(phys, dir) { 1385 __set_bit(phys, n->set); 1386 } 1387 1388 closedir(dir); 1389 return 0; 1390 } 1391 1392 static int memory_node__sort(const void *a, const void *b) 1393 { 1394 const struct memory_node *na = a; 1395 const struct memory_node *nb = b; 1396 1397 return na->node - nb->node; 1398 } 1399 1400 static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) 1401 { 1402 char path[PATH_MAX]; 1403 struct dirent *ent; 1404 DIR *dir; 1405 int ret = 0; 1406 size_t cnt = 0, size = 0; 1407 struct memory_node *nodes = NULL; 1408 1409 scnprintf(path, PATH_MAX, "%s/devices/system/node/", 1410 sysfs__mountpoint()); 1411 1412 dir = opendir(path); 1413 if (!dir) { 1414 pr_debug2("%s: couldn't read %s, does this arch have topology information?\n", 1415 __func__, path); 1416 return -1; 1417 } 1418 1419 while (!ret && (ent = readdir(dir))) { 1420 unsigned int idx; 1421 int r; 1422 1423 if (!strcmp(ent->d_name, ".") || 1424 !strcmp(ent->d_name, "..")) 1425 continue; 1426 1427 r = sscanf(ent->d_name, "node%u", &idx); 1428 if (r != 1) 1429 continue; 1430 1431 if (cnt >= size) { 1432 struct memory_node *new_nodes = 1433 reallocarray(nodes, cnt + 4, sizeof(*nodes)); 1434 1435 if (!new_nodes) { 1436 pr_err("Failed to write MEM_TOPOLOGY, size %zd nodes\n", size); 1437 ret = -ENOMEM; 1438 goto out; 1439 } 1440 nodes = new_nodes; 1441 size += 4; 1442 } 1443 ret = memory_node__read(&nodes[cnt++], idx); 1444 } 1445 out: 1446 closedir(dir); 1447 if (!ret) { 1448 *cntp = cnt; 1449 *nodesp = nodes; 1450 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort); 1451 } else 1452 free(nodes); 1453 1454 return ret; 1455 } 1456 1457 /* 1458 * The MEM_TOPOLOGY holds physical memory map for every 1459 * node in system. The format of data is as follows: 1460 * 1461 * 0 - version | for future changes 1462 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes 1463 * 16 - count | number of nodes 1464 * 1465 * For each node we store map of physical indexes for 1466 * each node: 1467 * 1468 * 32 - node id | node index 1469 * 40 - size | size of bitmap 1470 * 48 - bitmap | bitmap of memory indexes that belongs to node 1471 */ 1472 static int write_mem_topology(struct feat_fd *ff __maybe_unused, 1473 struct evlist *evlist __maybe_unused) 1474 { 1475 struct memory_node *nodes = NULL; 1476 u64 bsize, version = 1, i, nr = 0; 1477 int ret; 1478 1479 ret = sysfs__read_xll("devices/system/memory/block_size_bytes", 1480 (unsigned long long *) &bsize); 1481 if (ret) 1482 return ret; 1483 1484 ret = build_mem_topology(&nodes, &nr); 1485 if (ret) 1486 return ret; 1487 1488 ret = do_write(ff, &version, sizeof(version)); 1489 if (ret < 0) 1490 goto out; 1491 1492 ret = do_write(ff, &bsize, sizeof(bsize)); 1493 if (ret < 0) 1494 goto out; 1495 1496 ret = do_write(ff, &nr, sizeof(nr)); 1497 if (ret < 0) 1498 goto out; 1499 1500 for (i = 0; i < nr; i++) { 1501 struct memory_node *n = &nodes[i]; 1502 1503 #define _W(v) \ 1504 ret = do_write(ff, &n->v, sizeof(n->v)); \ 1505 if (ret < 0) \ 1506 goto out; 1507 1508 _W(node) 1509 _W(size) 1510 1511 #undef _W 1512 1513 ret = do_write_bitmap(ff, n->set, n->size); 1514 if (ret < 0) 1515 goto out; 1516 } 1517 1518 out: 1519 free(nodes); 1520 return ret; 1521 } 1522 1523 static int write_compressed(struct feat_fd *ff __maybe_unused, 1524 struct evlist *evlist __maybe_unused) 1525 { 1526 int ret; 1527 1528 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver)); 1529 if (ret) 1530 return ret; 1531 1532 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type)); 1533 if (ret) 1534 return ret; 1535 1536 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level)); 1537 if (ret) 1538 return ret; 1539 1540 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio)); 1541 if (ret) 1542 return ret; 1543 1544 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len)); 1545 } 1546 1547 static int __write_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu, 1548 bool write_pmu) 1549 { 1550 struct perf_pmu_caps *caps = NULL; 1551 int ret; 1552 1553 ret = do_write(ff, &pmu->nr_caps, sizeof(pmu->nr_caps)); 1554 if (ret < 0) 1555 return ret; 1556 1557 list_for_each_entry(caps, &pmu->caps, list) { 1558 ret = do_write_string(ff, caps->name); 1559 if (ret < 0) 1560 return ret; 1561 1562 ret = do_write_string(ff, caps->value); 1563 if (ret < 0) 1564 return ret; 1565 } 1566 1567 if (write_pmu) { 1568 ret = do_write_string(ff, pmu->name); 1569 if (ret < 0) 1570 return ret; 1571 } 1572 1573 return ret; 1574 } 1575 1576 static int write_cpu_pmu_caps(struct feat_fd *ff, 1577 struct evlist *evlist __maybe_unused) 1578 { 1579 struct perf_pmu *cpu_pmu = perf_pmus__find("cpu"); 1580 int ret; 1581 1582 if (!cpu_pmu) 1583 return -ENOENT; 1584 1585 ret = perf_pmu__caps_parse(cpu_pmu); 1586 if (ret < 0) 1587 return ret; 1588 1589 return __write_pmu_caps(ff, cpu_pmu, false); 1590 } 1591 1592 static int write_pmu_caps(struct feat_fd *ff, 1593 struct evlist *evlist __maybe_unused) 1594 { 1595 struct perf_pmu *pmu = NULL; 1596 int nr_pmu = 0; 1597 int ret; 1598 1599 while ((pmu = perf_pmus__scan(pmu))) { 1600 if (!pmu->name || !strcmp(pmu->name, "cpu") || 1601 perf_pmu__caps_parse(pmu) <= 0) 1602 continue; 1603 nr_pmu++; 1604 } 1605 1606 ret = do_write(ff, &nr_pmu, sizeof(nr_pmu)); 1607 if (ret < 0) 1608 return ret; 1609 1610 if (!nr_pmu) 1611 return 0; 1612 1613 /* 1614 * Write hybrid pmu caps first to maintain compatibility with 1615 * older perf tool. 1616 */ 1617 if (perf_pmus__num_core_pmus() > 1) { 1618 pmu = NULL; 1619 while ((pmu = perf_pmus__scan_core(pmu))) { 1620 ret = __write_pmu_caps(ff, pmu, true); 1621 if (ret < 0) 1622 return ret; 1623 } 1624 } 1625 1626 pmu = NULL; 1627 while ((pmu = perf_pmus__scan(pmu))) { 1628 if (pmu->is_core || !pmu->nr_caps) 1629 continue; 1630 1631 ret = __write_pmu_caps(ff, pmu, true); 1632 if (ret < 0) 1633 return ret; 1634 } 1635 return 0; 1636 } 1637 1638 static void print_hostname(struct feat_fd *ff, FILE *fp) 1639 { 1640 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname); 1641 } 1642 1643 static void print_osrelease(struct feat_fd *ff, FILE *fp) 1644 { 1645 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release); 1646 } 1647 1648 static void print_arch(struct feat_fd *ff, FILE *fp) 1649 { 1650 fprintf(fp, "# arch : %s\n", ff->ph->env.arch); 1651 } 1652 1653 static void print_cpudesc(struct feat_fd *ff, FILE *fp) 1654 { 1655 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc); 1656 } 1657 1658 static void print_nrcpus(struct feat_fd *ff, FILE *fp) 1659 { 1660 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online); 1661 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail); 1662 } 1663 1664 static void print_version(struct feat_fd *ff, FILE *fp) 1665 { 1666 fprintf(fp, "# perf version : %s\n", ff->ph->env.version); 1667 } 1668 1669 static void print_cmdline(struct feat_fd *ff, FILE *fp) 1670 { 1671 int nr, i; 1672 1673 nr = ff->ph->env.nr_cmdline; 1674 1675 fprintf(fp, "# cmdline : "); 1676 1677 for (i = 0; i < nr; i++) { 1678 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]); 1679 if (!argv_i) { 1680 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]); 1681 } else { 1682 char *mem = argv_i; 1683 do { 1684 char *quote = strchr(argv_i, '\''); 1685 if (!quote) 1686 break; 1687 *quote++ = '\0'; 1688 fprintf(fp, "%s\\\'", argv_i); 1689 argv_i = quote; 1690 } while (1); 1691 fprintf(fp, "%s ", argv_i); 1692 free(mem); 1693 } 1694 } 1695 fputc('\n', fp); 1696 } 1697 1698 static void print_cpu_topology(struct feat_fd *ff, FILE *fp) 1699 { 1700 struct perf_header *ph = ff->ph; 1701 int cpu_nr = ph->env.nr_cpus_avail; 1702 int nr, i; 1703 char *str; 1704 1705 nr = ph->env.nr_sibling_cores; 1706 str = ph->env.sibling_cores; 1707 1708 for (i = 0; i < nr; i++) { 1709 fprintf(fp, "# sibling sockets : %s\n", str); 1710 str += strlen(str) + 1; 1711 } 1712 1713 if (ph->env.nr_sibling_dies) { 1714 nr = ph->env.nr_sibling_dies; 1715 str = ph->env.sibling_dies; 1716 1717 for (i = 0; i < nr; i++) { 1718 fprintf(fp, "# sibling dies : %s\n", str); 1719 str += strlen(str) + 1; 1720 } 1721 } 1722 1723 nr = ph->env.nr_sibling_threads; 1724 str = ph->env.sibling_threads; 1725 1726 for (i = 0; i < nr; i++) { 1727 fprintf(fp, "# sibling threads : %s\n", str); 1728 str += strlen(str) + 1; 1729 } 1730 1731 if (ph->env.nr_sibling_dies) { 1732 if (ph->env.cpu != NULL) { 1733 for (i = 0; i < cpu_nr; i++) 1734 fprintf(fp, "# CPU %d: Core ID %d, " 1735 "Die ID %d, Socket ID %d\n", 1736 i, ph->env.cpu[i].core_id, 1737 ph->env.cpu[i].die_id, 1738 ph->env.cpu[i].socket_id); 1739 } else 1740 fprintf(fp, "# Core ID, Die ID and Socket ID " 1741 "information is not available\n"); 1742 } else { 1743 if (ph->env.cpu != NULL) { 1744 for (i = 0; i < cpu_nr; i++) 1745 fprintf(fp, "# CPU %d: Core ID %d, " 1746 "Socket ID %d\n", 1747 i, ph->env.cpu[i].core_id, 1748 ph->env.cpu[i].socket_id); 1749 } else 1750 fprintf(fp, "# Core ID and Socket ID " 1751 "information is not available\n"); 1752 } 1753 } 1754 1755 static void print_clockid(struct feat_fd *ff, FILE *fp) 1756 { 1757 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n", 1758 ff->ph->env.clock.clockid_res_ns * 1000); 1759 } 1760 1761 static void print_clock_data(struct feat_fd *ff, FILE *fp) 1762 { 1763 struct timespec clockid_ns; 1764 char tstr[64], date[64]; 1765 struct timeval tod_ns; 1766 clockid_t clockid; 1767 struct tm ltime; 1768 u64 ref; 1769 1770 if (!ff->ph->env.clock.enabled) { 1771 fprintf(fp, "# reference time disabled\n"); 1772 return; 1773 } 1774 1775 /* Compute TOD time. */ 1776 ref = ff->ph->env.clock.tod_ns; 1777 tod_ns.tv_sec = ref / NSEC_PER_SEC; 1778 ref -= tod_ns.tv_sec * NSEC_PER_SEC; 1779 tod_ns.tv_usec = ref / NSEC_PER_USEC; 1780 1781 /* Compute clockid time. */ 1782 ref = ff->ph->env.clock.clockid_ns; 1783 clockid_ns.tv_sec = ref / NSEC_PER_SEC; 1784 ref -= clockid_ns.tv_sec * NSEC_PER_SEC; 1785 clockid_ns.tv_nsec = ref; 1786 1787 clockid = ff->ph->env.clock.clockid; 1788 1789 if (localtime_r(&tod_ns.tv_sec, <ime) == NULL) 1790 snprintf(tstr, sizeof(tstr), "<error>"); 1791 else { 1792 strftime(date, sizeof(date), "%F %T", <ime); 1793 scnprintf(tstr, sizeof(tstr), "%s.%06d", 1794 date, (int) tod_ns.tv_usec); 1795 } 1796 1797 fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid); 1798 fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n", 1799 tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec, 1800 (long) clockid_ns.tv_sec, clockid_ns.tv_nsec, 1801 clockid_name(clockid)); 1802 } 1803 1804 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp) 1805 { 1806 int i; 1807 struct hybrid_node *n; 1808 1809 fprintf(fp, "# hybrid cpu system:\n"); 1810 for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) { 1811 n = &ff->ph->env.hybrid_nodes[i]; 1812 fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus); 1813 } 1814 } 1815 1816 static void print_dir_format(struct feat_fd *ff, FILE *fp) 1817 { 1818 struct perf_session *session; 1819 struct perf_data *data; 1820 1821 session = container_of(ff->ph, struct perf_session, header); 1822 data = session->data; 1823 1824 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version); 1825 } 1826 1827 #ifdef HAVE_LIBBPF_SUPPORT 1828 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp) 1829 { 1830 struct perf_env *env = &ff->ph->env; 1831 struct rb_root *root; 1832 struct rb_node *next; 1833 1834 down_read(&env->bpf_progs.lock); 1835 1836 root = &env->bpf_progs.infos; 1837 next = rb_first(root); 1838 1839 while (next) { 1840 struct bpf_prog_info_node *node; 1841 1842 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 1843 next = rb_next(&node->rb_node); 1844 1845 bpf_event__print_bpf_prog_info(&node->info_linear->info, 1846 env, fp); 1847 } 1848 1849 up_read(&env->bpf_progs.lock); 1850 } 1851 1852 static void print_bpf_btf(struct feat_fd *ff, FILE *fp) 1853 { 1854 struct perf_env *env = &ff->ph->env; 1855 struct rb_root *root; 1856 struct rb_node *next; 1857 1858 down_read(&env->bpf_progs.lock); 1859 1860 root = &env->bpf_progs.btfs; 1861 next = rb_first(root); 1862 1863 while (next) { 1864 struct btf_node *node; 1865 1866 node = rb_entry(next, struct btf_node, rb_node); 1867 next = rb_next(&node->rb_node); 1868 fprintf(fp, "# btf info of id %u\n", node->id); 1869 } 1870 1871 up_read(&env->bpf_progs.lock); 1872 } 1873 #endif // HAVE_LIBBPF_SUPPORT 1874 1875 static void free_event_desc(struct evsel *events) 1876 { 1877 struct evsel *evsel; 1878 1879 if (!events) 1880 return; 1881 1882 for (evsel = events; evsel->core.attr.size; evsel++) { 1883 zfree(&evsel->name); 1884 zfree(&evsel->core.id); 1885 } 1886 1887 free(events); 1888 } 1889 1890 static bool perf_attr_check(struct perf_event_attr *attr) 1891 { 1892 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) { 1893 pr_warning("Reserved bits are set unexpectedly. " 1894 "Please update perf tool.\n"); 1895 return false; 1896 } 1897 1898 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) { 1899 pr_warning("Unknown sample type (0x%llx) is detected. " 1900 "Please update perf tool.\n", 1901 attr->sample_type); 1902 return false; 1903 } 1904 1905 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) { 1906 pr_warning("Unknown read format (0x%llx) is detected. " 1907 "Please update perf tool.\n", 1908 attr->read_format); 1909 return false; 1910 } 1911 1912 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) && 1913 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) { 1914 pr_warning("Unknown branch sample type (0x%llx) is detected. " 1915 "Please update perf tool.\n", 1916 attr->branch_sample_type); 1917 1918 return false; 1919 } 1920 1921 return true; 1922 } 1923 1924 static struct evsel *read_event_desc(struct feat_fd *ff) 1925 { 1926 struct evsel *evsel, *events = NULL; 1927 u64 *id; 1928 void *buf = NULL; 1929 u32 nre, sz, nr, i, j; 1930 size_t msz; 1931 1932 /* number of events */ 1933 if (do_read_u32(ff, &nre)) 1934 goto error; 1935 1936 if (do_read_u32(ff, &sz)) 1937 goto error; 1938 1939 /* buffer to hold on file attr struct */ 1940 buf = malloc(sz); 1941 if (!buf) 1942 goto error; 1943 1944 /* the last event terminates with evsel->core.attr.size == 0: */ 1945 events = calloc(nre + 1, sizeof(*events)); 1946 if (!events) 1947 goto error; 1948 1949 msz = sizeof(evsel->core.attr); 1950 if (sz < msz) 1951 msz = sz; 1952 1953 for (i = 0, evsel = events; i < nre; evsel++, i++) { 1954 evsel->core.idx = i; 1955 1956 /* 1957 * must read entire on-file attr struct to 1958 * sync up with layout. 1959 */ 1960 if (__do_read(ff, buf, sz)) 1961 goto error; 1962 1963 if (ff->ph->needs_swap) 1964 perf_event__attr_swap(buf); 1965 1966 memcpy(&evsel->core.attr, buf, msz); 1967 1968 if (!perf_attr_check(&evsel->core.attr)) 1969 goto error; 1970 1971 if (do_read_u32(ff, &nr)) 1972 goto error; 1973 1974 if (ff->ph->needs_swap) 1975 evsel->needs_swap = true; 1976 1977 evsel->name = do_read_string(ff); 1978 if (!evsel->name) 1979 goto error; 1980 1981 if (!nr) 1982 continue; 1983 1984 id = calloc(nr, sizeof(*id)); 1985 if (!id) 1986 goto error; 1987 evsel->core.ids = nr; 1988 evsel->core.id = id; 1989 1990 for (j = 0 ; j < nr; j++) { 1991 if (do_read_u64(ff, id)) 1992 goto error; 1993 id++; 1994 } 1995 } 1996 out: 1997 free(buf); 1998 return events; 1999 error: 2000 free_event_desc(events); 2001 events = NULL; 2002 goto out; 2003 } 2004 2005 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val, 2006 void *priv __maybe_unused) 2007 { 2008 return fprintf(fp, ", %s = %s", name, val); 2009 } 2010 2011 static void print_event_desc(struct feat_fd *ff, FILE *fp) 2012 { 2013 struct evsel *evsel, *events; 2014 u32 j; 2015 u64 *id; 2016 2017 if (ff->events) 2018 events = ff->events; 2019 else 2020 events = read_event_desc(ff); 2021 2022 if (!events) { 2023 fprintf(fp, "# event desc: not available or unable to read\n"); 2024 return; 2025 } 2026 2027 for (evsel = events; evsel->core.attr.size; evsel++) { 2028 fprintf(fp, "# event : name = %s, ", evsel->name); 2029 2030 if (evsel->core.ids) { 2031 fprintf(fp, ", id = {"); 2032 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) { 2033 if (j) 2034 fputc(',', fp); 2035 fprintf(fp, " %"PRIu64, *id); 2036 } 2037 fprintf(fp, " }"); 2038 } 2039 2040 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL); 2041 2042 fputc('\n', fp); 2043 } 2044 2045 free_event_desc(events); 2046 ff->events = NULL; 2047 } 2048 2049 static void print_total_mem(struct feat_fd *ff, FILE *fp) 2050 { 2051 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem); 2052 } 2053 2054 static void print_numa_topology(struct feat_fd *ff, FILE *fp) 2055 { 2056 int i; 2057 struct numa_node *n; 2058 2059 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) { 2060 n = &ff->ph->env.numa_nodes[i]; 2061 2062 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," 2063 " free = %"PRIu64" kB\n", 2064 n->node, n->mem_total, n->mem_free); 2065 2066 fprintf(fp, "# node%u cpu list : ", n->node); 2067 cpu_map__fprintf(n->map, fp); 2068 } 2069 } 2070 2071 static void print_cpuid(struct feat_fd *ff, FILE *fp) 2072 { 2073 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid); 2074 } 2075 2076 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp) 2077 { 2078 fprintf(fp, "# contains samples with branch stack\n"); 2079 } 2080 2081 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp) 2082 { 2083 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n"); 2084 } 2085 2086 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp) 2087 { 2088 fprintf(fp, "# contains stat data\n"); 2089 } 2090 2091 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused) 2092 { 2093 int i; 2094 2095 fprintf(fp, "# CPU cache info:\n"); 2096 for (i = 0; i < ff->ph->env.caches_cnt; i++) { 2097 fprintf(fp, "# "); 2098 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]); 2099 } 2100 } 2101 2102 static void print_compressed(struct feat_fd *ff, FILE *fp) 2103 { 2104 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n", 2105 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown", 2106 ff->ph->env.comp_level, ff->ph->env.comp_ratio); 2107 } 2108 2109 static void __print_pmu_caps(FILE *fp, int nr_caps, char **caps, char *pmu_name) 2110 { 2111 const char *delimiter = ""; 2112 int i; 2113 2114 if (!nr_caps) { 2115 fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name); 2116 return; 2117 } 2118 2119 fprintf(fp, "# %s pmu capabilities: ", pmu_name); 2120 for (i = 0; i < nr_caps; i++) { 2121 fprintf(fp, "%s%s", delimiter, caps[i]); 2122 delimiter = ", "; 2123 } 2124 2125 fprintf(fp, "\n"); 2126 } 2127 2128 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp) 2129 { 2130 __print_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps, 2131 ff->ph->env.cpu_pmu_caps, (char *)"cpu"); 2132 } 2133 2134 static void print_pmu_caps(struct feat_fd *ff, FILE *fp) 2135 { 2136 struct pmu_caps *pmu_caps; 2137 2138 for (int i = 0; i < ff->ph->env.nr_pmus_with_caps; i++) { 2139 pmu_caps = &ff->ph->env.pmu_caps[i]; 2140 __print_pmu_caps(fp, pmu_caps->nr_caps, pmu_caps->caps, 2141 pmu_caps->pmu_name); 2142 } 2143 } 2144 2145 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp) 2146 { 2147 const char *delimiter = "# pmu mappings: "; 2148 char *str, *tmp; 2149 u32 pmu_num; 2150 u32 type; 2151 2152 pmu_num = ff->ph->env.nr_pmu_mappings; 2153 if (!pmu_num) { 2154 fprintf(fp, "# pmu mappings: not available\n"); 2155 return; 2156 } 2157 2158 str = ff->ph->env.pmu_mappings; 2159 2160 while (pmu_num) { 2161 type = strtoul(str, &tmp, 0); 2162 if (*tmp != ':') 2163 goto error; 2164 2165 str = tmp + 1; 2166 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); 2167 2168 delimiter = ", "; 2169 str += strlen(str) + 1; 2170 pmu_num--; 2171 } 2172 2173 fprintf(fp, "\n"); 2174 2175 if (!pmu_num) 2176 return; 2177 error: 2178 fprintf(fp, "# pmu mappings: unable to read\n"); 2179 } 2180 2181 static void print_group_desc(struct feat_fd *ff, FILE *fp) 2182 { 2183 struct perf_session *session; 2184 struct evsel *evsel; 2185 u32 nr = 0; 2186 2187 session = container_of(ff->ph, struct perf_session, header); 2188 2189 evlist__for_each_entry(session->evlist, evsel) { 2190 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { 2191 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel)); 2192 2193 nr = evsel->core.nr_members - 1; 2194 } else if (nr) { 2195 fprintf(fp, ",%s", evsel__name(evsel)); 2196 2197 if (--nr == 0) 2198 fprintf(fp, "}\n"); 2199 } 2200 } 2201 } 2202 2203 static void print_sample_time(struct feat_fd *ff, FILE *fp) 2204 { 2205 struct perf_session *session; 2206 char time_buf[32]; 2207 double d; 2208 2209 session = container_of(ff->ph, struct perf_session, header); 2210 2211 timestamp__scnprintf_usec(session->evlist->first_sample_time, 2212 time_buf, sizeof(time_buf)); 2213 fprintf(fp, "# time of first sample : %s\n", time_buf); 2214 2215 timestamp__scnprintf_usec(session->evlist->last_sample_time, 2216 time_buf, sizeof(time_buf)); 2217 fprintf(fp, "# time of last sample : %s\n", time_buf); 2218 2219 d = (double)(session->evlist->last_sample_time - 2220 session->evlist->first_sample_time) / NSEC_PER_MSEC; 2221 2222 fprintf(fp, "# sample duration : %10.3f ms\n", d); 2223 } 2224 2225 static void memory_node__fprintf(struct memory_node *n, 2226 unsigned long long bsize, FILE *fp) 2227 { 2228 char buf_map[100], buf_size[50]; 2229 unsigned long long size; 2230 2231 size = bsize * bitmap_weight(n->set, n->size); 2232 unit_number__scnprintf(buf_size, 50, size); 2233 2234 bitmap_scnprintf(n->set, n->size, buf_map, 100); 2235 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map); 2236 } 2237 2238 static void print_mem_topology(struct feat_fd *ff, FILE *fp) 2239 { 2240 struct memory_node *nodes; 2241 int i, nr; 2242 2243 nodes = ff->ph->env.memory_nodes; 2244 nr = ff->ph->env.nr_memory_nodes; 2245 2246 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n", 2247 nr, ff->ph->env.memory_bsize); 2248 2249 for (i = 0; i < nr; i++) { 2250 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp); 2251 } 2252 } 2253 2254 static int __event_process_build_id(struct perf_record_header_build_id *bev, 2255 char *filename, 2256 struct perf_session *session) 2257 { 2258 int err = -1; 2259 struct machine *machine; 2260 u16 cpumode; 2261 struct dso *dso; 2262 enum dso_space_type dso_space; 2263 2264 machine = perf_session__findnew_machine(session, bev->pid); 2265 if (!machine) 2266 goto out; 2267 2268 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 2269 2270 switch (cpumode) { 2271 case PERF_RECORD_MISC_KERNEL: 2272 dso_space = DSO_SPACE__KERNEL; 2273 break; 2274 case PERF_RECORD_MISC_GUEST_KERNEL: 2275 dso_space = DSO_SPACE__KERNEL_GUEST; 2276 break; 2277 case PERF_RECORD_MISC_USER: 2278 case PERF_RECORD_MISC_GUEST_USER: 2279 dso_space = DSO_SPACE__USER; 2280 break; 2281 default: 2282 goto out; 2283 } 2284 2285 dso = machine__findnew_dso(machine, filename); 2286 if (dso != NULL) { 2287 char sbuild_id[SBUILD_ID_SIZE]; 2288 struct build_id bid; 2289 size_t size = BUILD_ID_SIZE; 2290 2291 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE) 2292 size = bev->size; 2293 2294 build_id__init(&bid, bev->data, size); 2295 dso__set_build_id(dso, &bid); 2296 dso->header_build_id = 1; 2297 2298 if (dso_space != DSO_SPACE__USER) { 2299 struct kmod_path m = { .name = NULL, }; 2300 2301 if (!kmod_path__parse_name(&m, filename) && m.kmod) 2302 dso__set_module_info(dso, &m, machine); 2303 2304 dso->kernel = dso_space; 2305 free(m.name); 2306 } 2307 2308 build_id__sprintf(&dso->bid, sbuild_id); 2309 pr_debug("build id event received for %s: %s [%zu]\n", 2310 dso->long_name, sbuild_id, size); 2311 dso__put(dso); 2312 } 2313 2314 err = 0; 2315 out: 2316 return err; 2317 } 2318 2319 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, 2320 int input, u64 offset, u64 size) 2321 { 2322 struct perf_session *session = container_of(header, struct perf_session, header); 2323 struct { 2324 struct perf_event_header header; 2325 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; 2326 char filename[0]; 2327 } old_bev; 2328 struct perf_record_header_build_id bev; 2329 char filename[PATH_MAX]; 2330 u64 limit = offset + size; 2331 2332 while (offset < limit) { 2333 ssize_t len; 2334 2335 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) 2336 return -1; 2337 2338 if (header->needs_swap) 2339 perf_event_header__bswap(&old_bev.header); 2340 2341 len = old_bev.header.size - sizeof(old_bev); 2342 if (readn(input, filename, len) != len) 2343 return -1; 2344 2345 bev.header = old_bev.header; 2346 2347 /* 2348 * As the pid is the missing value, we need to fill 2349 * it properly. The header.misc value give us nice hint. 2350 */ 2351 bev.pid = HOST_KERNEL_ID; 2352 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || 2353 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) 2354 bev.pid = DEFAULT_GUEST_KERNEL_ID; 2355 2356 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); 2357 __event_process_build_id(&bev, filename, session); 2358 2359 offset += bev.header.size; 2360 } 2361 2362 return 0; 2363 } 2364 2365 static int perf_header__read_build_ids(struct perf_header *header, 2366 int input, u64 offset, u64 size) 2367 { 2368 struct perf_session *session = container_of(header, struct perf_session, header); 2369 struct perf_record_header_build_id bev; 2370 char filename[PATH_MAX]; 2371 u64 limit = offset + size, orig_offset = offset; 2372 int err = -1; 2373 2374 while (offset < limit) { 2375 ssize_t len; 2376 2377 if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) 2378 goto out; 2379 2380 if (header->needs_swap) 2381 perf_event_header__bswap(&bev.header); 2382 2383 len = bev.header.size - sizeof(bev); 2384 if (readn(input, filename, len) != len) 2385 goto out; 2386 /* 2387 * The a1645ce1 changeset: 2388 * 2389 * "perf: 'perf kvm' tool for monitoring guest performance from host" 2390 * 2391 * Added a field to struct perf_record_header_build_id that broke the file 2392 * format. 2393 * 2394 * Since the kernel build-id is the first entry, process the 2395 * table using the old format if the well known 2396 * '[kernel.kallsyms]' string for the kernel build-id has the 2397 * first 4 characters chopped off (where the pid_t sits). 2398 */ 2399 if (memcmp(filename, "nel.kallsyms]", 13) == 0) { 2400 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) 2401 return -1; 2402 return perf_header__read_build_ids_abi_quirk(header, input, offset, size); 2403 } 2404 2405 __event_process_build_id(&bev, filename, session); 2406 2407 offset += bev.header.size; 2408 } 2409 err = 0; 2410 out: 2411 return err; 2412 } 2413 2414 /* Macro for features that simply need to read and store a string. */ 2415 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \ 2416 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ 2417 {\ 2418 free(ff->ph->env.__feat_env); \ 2419 ff->ph->env.__feat_env = do_read_string(ff); \ 2420 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \ 2421 } 2422 2423 FEAT_PROCESS_STR_FUN(hostname, hostname); 2424 FEAT_PROCESS_STR_FUN(osrelease, os_release); 2425 FEAT_PROCESS_STR_FUN(version, version); 2426 FEAT_PROCESS_STR_FUN(arch, arch); 2427 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc); 2428 FEAT_PROCESS_STR_FUN(cpuid, cpuid); 2429 2430 #ifdef HAVE_LIBTRACEEVENT 2431 static int process_tracing_data(struct feat_fd *ff, void *data) 2432 { 2433 ssize_t ret = trace_report(ff->fd, data, false); 2434 2435 return ret < 0 ? -1 : 0; 2436 } 2437 #endif 2438 2439 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused) 2440 { 2441 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size)) 2442 pr_debug("Failed to read buildids, continuing...\n"); 2443 return 0; 2444 } 2445 2446 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused) 2447 { 2448 int ret; 2449 u32 nr_cpus_avail, nr_cpus_online; 2450 2451 ret = do_read_u32(ff, &nr_cpus_avail); 2452 if (ret) 2453 return ret; 2454 2455 ret = do_read_u32(ff, &nr_cpus_online); 2456 if (ret) 2457 return ret; 2458 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail; 2459 ff->ph->env.nr_cpus_online = (int)nr_cpus_online; 2460 return 0; 2461 } 2462 2463 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused) 2464 { 2465 u64 total_mem; 2466 int ret; 2467 2468 ret = do_read_u64(ff, &total_mem); 2469 if (ret) 2470 return -1; 2471 ff->ph->env.total_mem = (unsigned long long)total_mem; 2472 return 0; 2473 } 2474 2475 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx) 2476 { 2477 struct evsel *evsel; 2478 2479 evlist__for_each_entry(evlist, evsel) { 2480 if (evsel->core.idx == idx) 2481 return evsel; 2482 } 2483 2484 return NULL; 2485 } 2486 2487 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event) 2488 { 2489 struct evsel *evsel; 2490 2491 if (!event->name) 2492 return; 2493 2494 evsel = evlist__find_by_index(evlist, event->core.idx); 2495 if (!evsel) 2496 return; 2497 2498 if (evsel->name) 2499 return; 2500 2501 evsel->name = strdup(event->name); 2502 } 2503 2504 static int 2505 process_event_desc(struct feat_fd *ff, void *data __maybe_unused) 2506 { 2507 struct perf_session *session; 2508 struct evsel *evsel, *events = read_event_desc(ff); 2509 2510 if (!events) 2511 return 0; 2512 2513 session = container_of(ff->ph, struct perf_session, header); 2514 2515 if (session->data->is_pipe) { 2516 /* Save events for reading later by print_event_desc, 2517 * since they can't be read again in pipe mode. */ 2518 ff->events = events; 2519 } 2520 2521 for (evsel = events; evsel->core.attr.size; evsel++) 2522 evlist__set_event_name(session->evlist, evsel); 2523 2524 if (!session->data->is_pipe) 2525 free_event_desc(events); 2526 2527 return 0; 2528 } 2529 2530 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused) 2531 { 2532 char *str, *cmdline = NULL, **argv = NULL; 2533 u32 nr, i, len = 0; 2534 2535 if (do_read_u32(ff, &nr)) 2536 return -1; 2537 2538 ff->ph->env.nr_cmdline = nr; 2539 2540 cmdline = zalloc(ff->size + nr + 1); 2541 if (!cmdline) 2542 return -1; 2543 2544 argv = zalloc(sizeof(char *) * (nr + 1)); 2545 if (!argv) 2546 goto error; 2547 2548 for (i = 0; i < nr; i++) { 2549 str = do_read_string(ff); 2550 if (!str) 2551 goto error; 2552 2553 argv[i] = cmdline + len; 2554 memcpy(argv[i], str, strlen(str) + 1); 2555 len += strlen(str) + 1; 2556 free(str); 2557 } 2558 ff->ph->env.cmdline = cmdline; 2559 ff->ph->env.cmdline_argv = (const char **) argv; 2560 return 0; 2561 2562 error: 2563 free(argv); 2564 free(cmdline); 2565 return -1; 2566 } 2567 2568 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused) 2569 { 2570 u32 nr, i; 2571 char *str; 2572 struct strbuf sb; 2573 int cpu_nr = ff->ph->env.nr_cpus_avail; 2574 u64 size = 0; 2575 struct perf_header *ph = ff->ph; 2576 bool do_core_id_test = true; 2577 2578 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu)); 2579 if (!ph->env.cpu) 2580 return -1; 2581 2582 if (do_read_u32(ff, &nr)) 2583 goto free_cpu; 2584 2585 ph->env.nr_sibling_cores = nr; 2586 size += sizeof(u32); 2587 if (strbuf_init(&sb, 128) < 0) 2588 goto free_cpu; 2589 2590 for (i = 0; i < nr; i++) { 2591 str = do_read_string(ff); 2592 if (!str) 2593 goto error; 2594 2595 /* include a NULL character at the end */ 2596 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2597 goto error; 2598 size += string_size(str); 2599 free(str); 2600 } 2601 ph->env.sibling_cores = strbuf_detach(&sb, NULL); 2602 2603 if (do_read_u32(ff, &nr)) 2604 return -1; 2605 2606 ph->env.nr_sibling_threads = nr; 2607 size += sizeof(u32); 2608 2609 for (i = 0; i < nr; i++) { 2610 str = do_read_string(ff); 2611 if (!str) 2612 goto error; 2613 2614 /* include a NULL character at the end */ 2615 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2616 goto error; 2617 size += string_size(str); 2618 free(str); 2619 } 2620 ph->env.sibling_threads = strbuf_detach(&sb, NULL); 2621 2622 /* 2623 * The header may be from old perf, 2624 * which doesn't include core id and socket id information. 2625 */ 2626 if (ff->size <= size) { 2627 zfree(&ph->env.cpu); 2628 return 0; 2629 } 2630 2631 /* On s390 the socket_id number is not related to the numbers of cpus. 2632 * The socket_id number might be higher than the numbers of cpus. 2633 * This depends on the configuration. 2634 * AArch64 is the same. 2635 */ 2636 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4) 2637 || !strncmp(ph->env.arch, "aarch64", 7))) 2638 do_core_id_test = false; 2639 2640 for (i = 0; i < (u32)cpu_nr; i++) { 2641 if (do_read_u32(ff, &nr)) 2642 goto free_cpu; 2643 2644 ph->env.cpu[i].core_id = nr; 2645 size += sizeof(u32); 2646 2647 if (do_read_u32(ff, &nr)) 2648 goto free_cpu; 2649 2650 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) { 2651 pr_debug("socket_id number is too big." 2652 "You may need to upgrade the perf tool.\n"); 2653 goto free_cpu; 2654 } 2655 2656 ph->env.cpu[i].socket_id = nr; 2657 size += sizeof(u32); 2658 } 2659 2660 /* 2661 * The header may be from old perf, 2662 * which doesn't include die information. 2663 */ 2664 if (ff->size <= size) 2665 return 0; 2666 2667 if (do_read_u32(ff, &nr)) 2668 return -1; 2669 2670 ph->env.nr_sibling_dies = nr; 2671 size += sizeof(u32); 2672 2673 for (i = 0; i < nr; i++) { 2674 str = do_read_string(ff); 2675 if (!str) 2676 goto error; 2677 2678 /* include a NULL character at the end */ 2679 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2680 goto error; 2681 size += string_size(str); 2682 free(str); 2683 } 2684 ph->env.sibling_dies = strbuf_detach(&sb, NULL); 2685 2686 for (i = 0; i < (u32)cpu_nr; i++) { 2687 if (do_read_u32(ff, &nr)) 2688 goto free_cpu; 2689 2690 ph->env.cpu[i].die_id = nr; 2691 } 2692 2693 return 0; 2694 2695 error: 2696 strbuf_release(&sb); 2697 free_cpu: 2698 zfree(&ph->env.cpu); 2699 return -1; 2700 } 2701 2702 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused) 2703 { 2704 struct numa_node *nodes, *n; 2705 u32 nr, i; 2706 char *str; 2707 2708 /* nr nodes */ 2709 if (do_read_u32(ff, &nr)) 2710 return -1; 2711 2712 nodes = zalloc(sizeof(*nodes) * nr); 2713 if (!nodes) 2714 return -ENOMEM; 2715 2716 for (i = 0; i < nr; i++) { 2717 n = &nodes[i]; 2718 2719 /* node number */ 2720 if (do_read_u32(ff, &n->node)) 2721 goto error; 2722 2723 if (do_read_u64(ff, &n->mem_total)) 2724 goto error; 2725 2726 if (do_read_u64(ff, &n->mem_free)) 2727 goto error; 2728 2729 str = do_read_string(ff); 2730 if (!str) 2731 goto error; 2732 2733 n->map = perf_cpu_map__new(str); 2734 if (!n->map) 2735 goto error; 2736 2737 free(str); 2738 } 2739 ff->ph->env.nr_numa_nodes = nr; 2740 ff->ph->env.numa_nodes = nodes; 2741 return 0; 2742 2743 error: 2744 free(nodes); 2745 return -1; 2746 } 2747 2748 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused) 2749 { 2750 char *name; 2751 u32 pmu_num; 2752 u32 type; 2753 struct strbuf sb; 2754 2755 if (do_read_u32(ff, &pmu_num)) 2756 return -1; 2757 2758 if (!pmu_num) { 2759 pr_debug("pmu mappings not available\n"); 2760 return 0; 2761 } 2762 2763 ff->ph->env.nr_pmu_mappings = pmu_num; 2764 if (strbuf_init(&sb, 128) < 0) 2765 return -1; 2766 2767 while (pmu_num) { 2768 if (do_read_u32(ff, &type)) 2769 goto error; 2770 2771 name = do_read_string(ff); 2772 if (!name) 2773 goto error; 2774 2775 if (strbuf_addf(&sb, "%u:%s", type, name) < 0) 2776 goto error; 2777 /* include a NULL character at the end */ 2778 if (strbuf_add(&sb, "", 1) < 0) 2779 goto error; 2780 2781 if (!strcmp(name, "msr")) 2782 ff->ph->env.msr_pmu_type = type; 2783 2784 free(name); 2785 pmu_num--; 2786 } 2787 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL); 2788 return 0; 2789 2790 error: 2791 strbuf_release(&sb); 2792 return -1; 2793 } 2794 2795 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused) 2796 { 2797 size_t ret = -1; 2798 u32 i, nr, nr_groups; 2799 struct perf_session *session; 2800 struct evsel *evsel, *leader = NULL; 2801 struct group_desc { 2802 char *name; 2803 u32 leader_idx; 2804 u32 nr_members; 2805 } *desc; 2806 2807 if (do_read_u32(ff, &nr_groups)) 2808 return -1; 2809 2810 ff->ph->env.nr_groups = nr_groups; 2811 if (!nr_groups) { 2812 pr_debug("group desc not available\n"); 2813 return 0; 2814 } 2815 2816 desc = calloc(nr_groups, sizeof(*desc)); 2817 if (!desc) 2818 return -1; 2819 2820 for (i = 0; i < nr_groups; i++) { 2821 desc[i].name = do_read_string(ff); 2822 if (!desc[i].name) 2823 goto out_free; 2824 2825 if (do_read_u32(ff, &desc[i].leader_idx)) 2826 goto out_free; 2827 2828 if (do_read_u32(ff, &desc[i].nr_members)) 2829 goto out_free; 2830 } 2831 2832 /* 2833 * Rebuild group relationship based on the group_desc 2834 */ 2835 session = container_of(ff->ph, struct perf_session, header); 2836 2837 i = nr = 0; 2838 evlist__for_each_entry(session->evlist, evsel) { 2839 if (evsel->core.idx == (int) desc[i].leader_idx) { 2840 evsel__set_leader(evsel, evsel); 2841 /* {anon_group} is a dummy name */ 2842 if (strcmp(desc[i].name, "{anon_group}")) { 2843 evsel->group_name = desc[i].name; 2844 desc[i].name = NULL; 2845 } 2846 evsel->core.nr_members = desc[i].nr_members; 2847 2848 if (i >= nr_groups || nr > 0) { 2849 pr_debug("invalid group desc\n"); 2850 goto out_free; 2851 } 2852 2853 leader = evsel; 2854 nr = evsel->core.nr_members - 1; 2855 i++; 2856 } else if (nr) { 2857 /* This is a group member */ 2858 evsel__set_leader(evsel, leader); 2859 2860 nr--; 2861 } 2862 } 2863 2864 if (i != nr_groups || nr != 0) { 2865 pr_debug("invalid group desc\n"); 2866 goto out_free; 2867 } 2868 2869 ret = 0; 2870 out_free: 2871 for (i = 0; i < nr_groups; i++) 2872 zfree(&desc[i].name); 2873 free(desc); 2874 2875 return ret; 2876 } 2877 2878 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused) 2879 { 2880 struct perf_session *session; 2881 int err; 2882 2883 session = container_of(ff->ph, struct perf_session, header); 2884 2885 err = auxtrace_index__process(ff->fd, ff->size, session, 2886 ff->ph->needs_swap); 2887 if (err < 0) 2888 pr_err("Failed to process auxtrace index\n"); 2889 return err; 2890 } 2891 2892 static int process_cache(struct feat_fd *ff, void *data __maybe_unused) 2893 { 2894 struct cpu_cache_level *caches; 2895 u32 cnt, i, version; 2896 2897 if (do_read_u32(ff, &version)) 2898 return -1; 2899 2900 if (version != 1) 2901 return -1; 2902 2903 if (do_read_u32(ff, &cnt)) 2904 return -1; 2905 2906 caches = zalloc(sizeof(*caches) * cnt); 2907 if (!caches) 2908 return -1; 2909 2910 for (i = 0; i < cnt; i++) { 2911 struct cpu_cache_level c; 2912 2913 #define _R(v) \ 2914 if (do_read_u32(ff, &c.v))\ 2915 goto out_free_caches; \ 2916 2917 _R(level) 2918 _R(line_size) 2919 _R(sets) 2920 _R(ways) 2921 #undef _R 2922 2923 #define _R(v) \ 2924 c.v = do_read_string(ff); \ 2925 if (!c.v) \ 2926 goto out_free_caches; 2927 2928 _R(type) 2929 _R(size) 2930 _R(map) 2931 #undef _R 2932 2933 caches[i] = c; 2934 } 2935 2936 ff->ph->env.caches = caches; 2937 ff->ph->env.caches_cnt = cnt; 2938 return 0; 2939 out_free_caches: 2940 free(caches); 2941 return -1; 2942 } 2943 2944 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused) 2945 { 2946 struct perf_session *session; 2947 u64 first_sample_time, last_sample_time; 2948 int ret; 2949 2950 session = container_of(ff->ph, struct perf_session, header); 2951 2952 ret = do_read_u64(ff, &first_sample_time); 2953 if (ret) 2954 return -1; 2955 2956 ret = do_read_u64(ff, &last_sample_time); 2957 if (ret) 2958 return -1; 2959 2960 session->evlist->first_sample_time = first_sample_time; 2961 session->evlist->last_sample_time = last_sample_time; 2962 return 0; 2963 } 2964 2965 static int process_mem_topology(struct feat_fd *ff, 2966 void *data __maybe_unused) 2967 { 2968 struct memory_node *nodes; 2969 u64 version, i, nr, bsize; 2970 int ret = -1; 2971 2972 if (do_read_u64(ff, &version)) 2973 return -1; 2974 2975 if (version != 1) 2976 return -1; 2977 2978 if (do_read_u64(ff, &bsize)) 2979 return -1; 2980 2981 if (do_read_u64(ff, &nr)) 2982 return -1; 2983 2984 nodes = zalloc(sizeof(*nodes) * nr); 2985 if (!nodes) 2986 return -1; 2987 2988 for (i = 0; i < nr; i++) { 2989 struct memory_node n; 2990 2991 #define _R(v) \ 2992 if (do_read_u64(ff, &n.v)) \ 2993 goto out; \ 2994 2995 _R(node) 2996 _R(size) 2997 2998 #undef _R 2999 3000 if (do_read_bitmap(ff, &n.set, &n.size)) 3001 goto out; 3002 3003 nodes[i] = n; 3004 } 3005 3006 ff->ph->env.memory_bsize = bsize; 3007 ff->ph->env.memory_nodes = nodes; 3008 ff->ph->env.nr_memory_nodes = nr; 3009 ret = 0; 3010 3011 out: 3012 if (ret) 3013 free(nodes); 3014 return ret; 3015 } 3016 3017 static int process_clockid(struct feat_fd *ff, 3018 void *data __maybe_unused) 3019 { 3020 if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns)) 3021 return -1; 3022 3023 return 0; 3024 } 3025 3026 static int process_clock_data(struct feat_fd *ff, 3027 void *_data __maybe_unused) 3028 { 3029 u32 data32; 3030 u64 data64; 3031 3032 /* version */ 3033 if (do_read_u32(ff, &data32)) 3034 return -1; 3035 3036 if (data32 != 1) 3037 return -1; 3038 3039 /* clockid */ 3040 if (do_read_u32(ff, &data32)) 3041 return -1; 3042 3043 ff->ph->env.clock.clockid = data32; 3044 3045 /* TOD ref time */ 3046 if (do_read_u64(ff, &data64)) 3047 return -1; 3048 3049 ff->ph->env.clock.tod_ns = data64; 3050 3051 /* clockid ref time */ 3052 if (do_read_u64(ff, &data64)) 3053 return -1; 3054 3055 ff->ph->env.clock.clockid_ns = data64; 3056 ff->ph->env.clock.enabled = true; 3057 return 0; 3058 } 3059 3060 static int process_hybrid_topology(struct feat_fd *ff, 3061 void *data __maybe_unused) 3062 { 3063 struct hybrid_node *nodes, *n; 3064 u32 nr, i; 3065 3066 /* nr nodes */ 3067 if (do_read_u32(ff, &nr)) 3068 return -1; 3069 3070 nodes = zalloc(sizeof(*nodes) * nr); 3071 if (!nodes) 3072 return -ENOMEM; 3073 3074 for (i = 0; i < nr; i++) { 3075 n = &nodes[i]; 3076 3077 n->pmu_name = do_read_string(ff); 3078 if (!n->pmu_name) 3079 goto error; 3080 3081 n->cpus = do_read_string(ff); 3082 if (!n->cpus) 3083 goto error; 3084 } 3085 3086 ff->ph->env.nr_hybrid_nodes = nr; 3087 ff->ph->env.hybrid_nodes = nodes; 3088 return 0; 3089 3090 error: 3091 for (i = 0; i < nr; i++) { 3092 free(nodes[i].pmu_name); 3093 free(nodes[i].cpus); 3094 } 3095 3096 free(nodes); 3097 return -1; 3098 } 3099 3100 static int process_dir_format(struct feat_fd *ff, 3101 void *_data __maybe_unused) 3102 { 3103 struct perf_session *session; 3104 struct perf_data *data; 3105 3106 session = container_of(ff->ph, struct perf_session, header); 3107 data = session->data; 3108 3109 if (WARN_ON(!perf_data__is_dir(data))) 3110 return -1; 3111 3112 return do_read_u64(ff, &data->dir.version); 3113 } 3114 3115 #ifdef HAVE_LIBBPF_SUPPORT 3116 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) 3117 { 3118 struct bpf_prog_info_node *info_node; 3119 struct perf_env *env = &ff->ph->env; 3120 struct perf_bpil *info_linear; 3121 u32 count, i; 3122 int err = -1; 3123 3124 if (ff->ph->needs_swap) { 3125 pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n"); 3126 return 0; 3127 } 3128 3129 if (do_read_u32(ff, &count)) 3130 return -1; 3131 3132 down_write(&env->bpf_progs.lock); 3133 3134 for (i = 0; i < count; ++i) { 3135 u32 info_len, data_len; 3136 3137 info_linear = NULL; 3138 info_node = NULL; 3139 if (do_read_u32(ff, &info_len)) 3140 goto out; 3141 if (do_read_u32(ff, &data_len)) 3142 goto out; 3143 3144 if (info_len > sizeof(struct bpf_prog_info)) { 3145 pr_warning("detected invalid bpf_prog_info\n"); 3146 goto out; 3147 } 3148 3149 info_linear = malloc(sizeof(struct perf_bpil) + 3150 data_len); 3151 if (!info_linear) 3152 goto out; 3153 info_linear->info_len = sizeof(struct bpf_prog_info); 3154 info_linear->data_len = data_len; 3155 if (do_read_u64(ff, (u64 *)(&info_linear->arrays))) 3156 goto out; 3157 if (__do_read(ff, &info_linear->info, info_len)) 3158 goto out; 3159 if (info_len < sizeof(struct bpf_prog_info)) 3160 memset(((void *)(&info_linear->info)) + info_len, 0, 3161 sizeof(struct bpf_prog_info) - info_len); 3162 3163 if (__do_read(ff, info_linear->data, data_len)) 3164 goto out; 3165 3166 info_node = malloc(sizeof(struct bpf_prog_info_node)); 3167 if (!info_node) 3168 goto out; 3169 3170 /* after reading from file, translate offset to address */ 3171 bpil_offs_to_addr(info_linear); 3172 info_node->info_linear = info_linear; 3173 perf_env__insert_bpf_prog_info(env, info_node); 3174 } 3175 3176 up_write(&env->bpf_progs.lock); 3177 return 0; 3178 out: 3179 free(info_linear); 3180 free(info_node); 3181 up_write(&env->bpf_progs.lock); 3182 return err; 3183 } 3184 3185 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) 3186 { 3187 struct perf_env *env = &ff->ph->env; 3188 struct btf_node *node = NULL; 3189 u32 count, i; 3190 int err = -1; 3191 3192 if (ff->ph->needs_swap) { 3193 pr_warning("interpreting btf from systems with endianness is not yet supported\n"); 3194 return 0; 3195 } 3196 3197 if (do_read_u32(ff, &count)) 3198 return -1; 3199 3200 down_write(&env->bpf_progs.lock); 3201 3202 for (i = 0; i < count; ++i) { 3203 u32 id, data_size; 3204 3205 if (do_read_u32(ff, &id)) 3206 goto out; 3207 if (do_read_u32(ff, &data_size)) 3208 goto out; 3209 3210 node = malloc(sizeof(struct btf_node) + data_size); 3211 if (!node) 3212 goto out; 3213 3214 node->id = id; 3215 node->data_size = data_size; 3216 3217 if (__do_read(ff, node->data, data_size)) 3218 goto out; 3219 3220 perf_env__insert_btf(env, node); 3221 node = NULL; 3222 } 3223 3224 err = 0; 3225 out: 3226 up_write(&env->bpf_progs.lock); 3227 free(node); 3228 return err; 3229 } 3230 #endif // HAVE_LIBBPF_SUPPORT 3231 3232 static int process_compressed(struct feat_fd *ff, 3233 void *data __maybe_unused) 3234 { 3235 if (do_read_u32(ff, &(ff->ph->env.comp_ver))) 3236 return -1; 3237 3238 if (do_read_u32(ff, &(ff->ph->env.comp_type))) 3239 return -1; 3240 3241 if (do_read_u32(ff, &(ff->ph->env.comp_level))) 3242 return -1; 3243 3244 if (do_read_u32(ff, &(ff->ph->env.comp_ratio))) 3245 return -1; 3246 3247 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len))) 3248 return -1; 3249 3250 return 0; 3251 } 3252 3253 static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps, 3254 char ***caps, unsigned int *max_branches) 3255 { 3256 char *name, *value, *ptr; 3257 u32 nr_pmu_caps, i; 3258 3259 *nr_caps = 0; 3260 *caps = NULL; 3261 3262 if (do_read_u32(ff, &nr_pmu_caps)) 3263 return -1; 3264 3265 if (!nr_pmu_caps) 3266 return 0; 3267 3268 *caps = zalloc(sizeof(char *) * nr_pmu_caps); 3269 if (!*caps) 3270 return -1; 3271 3272 for (i = 0; i < nr_pmu_caps; i++) { 3273 name = do_read_string(ff); 3274 if (!name) 3275 goto error; 3276 3277 value = do_read_string(ff); 3278 if (!value) 3279 goto free_name; 3280 3281 if (asprintf(&ptr, "%s=%s", name, value) < 0) 3282 goto free_value; 3283 3284 (*caps)[i] = ptr; 3285 3286 if (!strcmp(name, "branches")) 3287 *max_branches = atoi(value); 3288 3289 free(value); 3290 free(name); 3291 } 3292 *nr_caps = nr_pmu_caps; 3293 return 0; 3294 3295 free_value: 3296 free(value); 3297 free_name: 3298 free(name); 3299 error: 3300 for (; i > 0; i--) 3301 free((*caps)[i - 1]); 3302 free(*caps); 3303 *caps = NULL; 3304 *nr_caps = 0; 3305 return -1; 3306 } 3307 3308 static int process_cpu_pmu_caps(struct feat_fd *ff, 3309 void *data __maybe_unused) 3310 { 3311 int ret = __process_pmu_caps(ff, &ff->ph->env.nr_cpu_pmu_caps, 3312 &ff->ph->env.cpu_pmu_caps, 3313 &ff->ph->env.max_branches); 3314 3315 if (!ret && !ff->ph->env.cpu_pmu_caps) 3316 pr_debug("cpu pmu capabilities not available\n"); 3317 return ret; 3318 } 3319 3320 static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused) 3321 { 3322 struct pmu_caps *pmu_caps; 3323 u32 nr_pmu, i; 3324 int ret; 3325 int j; 3326 3327 if (do_read_u32(ff, &nr_pmu)) 3328 return -1; 3329 3330 if (!nr_pmu) { 3331 pr_debug("pmu capabilities not available\n"); 3332 return 0; 3333 } 3334 3335 pmu_caps = zalloc(sizeof(*pmu_caps) * nr_pmu); 3336 if (!pmu_caps) 3337 return -ENOMEM; 3338 3339 for (i = 0; i < nr_pmu; i++) { 3340 ret = __process_pmu_caps(ff, &pmu_caps[i].nr_caps, 3341 &pmu_caps[i].caps, 3342 &pmu_caps[i].max_branches); 3343 if (ret) 3344 goto err; 3345 3346 pmu_caps[i].pmu_name = do_read_string(ff); 3347 if (!pmu_caps[i].pmu_name) { 3348 ret = -1; 3349 goto err; 3350 } 3351 if (!pmu_caps[i].nr_caps) { 3352 pr_debug("%s pmu capabilities not available\n", 3353 pmu_caps[i].pmu_name); 3354 } 3355 } 3356 3357 ff->ph->env.nr_pmus_with_caps = nr_pmu; 3358 ff->ph->env.pmu_caps = pmu_caps; 3359 return 0; 3360 3361 err: 3362 for (i = 0; i < nr_pmu; i++) { 3363 for (j = 0; j < pmu_caps[i].nr_caps; j++) 3364 free(pmu_caps[i].caps[j]); 3365 free(pmu_caps[i].caps); 3366 free(pmu_caps[i].pmu_name); 3367 } 3368 3369 free(pmu_caps); 3370 return ret; 3371 } 3372 3373 #define FEAT_OPR(n, func, __full_only) \ 3374 [HEADER_##n] = { \ 3375 .name = __stringify(n), \ 3376 .write = write_##func, \ 3377 .print = print_##func, \ 3378 .full_only = __full_only, \ 3379 .process = process_##func, \ 3380 .synthesize = true \ 3381 } 3382 3383 #define FEAT_OPN(n, func, __full_only) \ 3384 [HEADER_##n] = { \ 3385 .name = __stringify(n), \ 3386 .write = write_##func, \ 3387 .print = print_##func, \ 3388 .full_only = __full_only, \ 3389 .process = process_##func \ 3390 } 3391 3392 /* feature_ops not implemented: */ 3393 #define print_tracing_data NULL 3394 #define print_build_id NULL 3395 3396 #define process_branch_stack NULL 3397 #define process_stat NULL 3398 3399 // Only used in util/synthetic-events.c 3400 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE]; 3401 3402 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = { 3403 #ifdef HAVE_LIBTRACEEVENT 3404 FEAT_OPN(TRACING_DATA, tracing_data, false), 3405 #endif 3406 FEAT_OPN(BUILD_ID, build_id, false), 3407 FEAT_OPR(HOSTNAME, hostname, false), 3408 FEAT_OPR(OSRELEASE, osrelease, false), 3409 FEAT_OPR(VERSION, version, false), 3410 FEAT_OPR(ARCH, arch, false), 3411 FEAT_OPR(NRCPUS, nrcpus, false), 3412 FEAT_OPR(CPUDESC, cpudesc, false), 3413 FEAT_OPR(CPUID, cpuid, false), 3414 FEAT_OPR(TOTAL_MEM, total_mem, false), 3415 FEAT_OPR(EVENT_DESC, event_desc, false), 3416 FEAT_OPR(CMDLINE, cmdline, false), 3417 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true), 3418 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true), 3419 FEAT_OPN(BRANCH_STACK, branch_stack, false), 3420 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false), 3421 FEAT_OPR(GROUP_DESC, group_desc, false), 3422 FEAT_OPN(AUXTRACE, auxtrace, false), 3423 FEAT_OPN(STAT, stat, false), 3424 FEAT_OPN(CACHE, cache, true), 3425 FEAT_OPR(SAMPLE_TIME, sample_time, false), 3426 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true), 3427 FEAT_OPR(CLOCKID, clockid, false), 3428 FEAT_OPN(DIR_FORMAT, dir_format, false), 3429 #ifdef HAVE_LIBBPF_SUPPORT 3430 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false), 3431 FEAT_OPR(BPF_BTF, bpf_btf, false), 3432 #endif 3433 FEAT_OPR(COMPRESSED, compressed, false), 3434 FEAT_OPR(CPU_PMU_CAPS, cpu_pmu_caps, false), 3435 FEAT_OPR(CLOCK_DATA, clock_data, false), 3436 FEAT_OPN(HYBRID_TOPOLOGY, hybrid_topology, true), 3437 FEAT_OPR(PMU_CAPS, pmu_caps, false), 3438 }; 3439 3440 struct header_print_data { 3441 FILE *fp; 3442 bool full; /* extended list of headers */ 3443 }; 3444 3445 static int perf_file_section__fprintf_info(struct perf_file_section *section, 3446 struct perf_header *ph, 3447 int feat, int fd, void *data) 3448 { 3449 struct header_print_data *hd = data; 3450 struct feat_fd ff; 3451 3452 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 3453 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 3454 "%d, continuing...\n", section->offset, feat); 3455 return 0; 3456 } 3457 if (feat >= HEADER_LAST_FEATURE) { 3458 pr_warning("unknown feature %d\n", feat); 3459 return 0; 3460 } 3461 if (!feat_ops[feat].print) 3462 return 0; 3463 3464 ff = (struct feat_fd) { 3465 .fd = fd, 3466 .ph = ph, 3467 }; 3468 3469 if (!feat_ops[feat].full_only || hd->full) 3470 feat_ops[feat].print(&ff, hd->fp); 3471 else 3472 fprintf(hd->fp, "# %s info available, use -I to display\n", 3473 feat_ops[feat].name); 3474 3475 return 0; 3476 } 3477 3478 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) 3479 { 3480 struct header_print_data hd; 3481 struct perf_header *header = &session->header; 3482 int fd = perf_data__fd(session->data); 3483 struct stat st; 3484 time_t stctime; 3485 int ret, bit; 3486 3487 hd.fp = fp; 3488 hd.full = full; 3489 3490 ret = fstat(fd, &st); 3491 if (ret == -1) 3492 return -1; 3493 3494 stctime = st.st_mtime; 3495 fprintf(fp, "# captured on : %s", ctime(&stctime)); 3496 3497 fprintf(fp, "# header version : %u\n", header->version); 3498 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset); 3499 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size); 3500 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset); 3501 3502 perf_header__process_sections(header, fd, &hd, 3503 perf_file_section__fprintf_info); 3504 3505 if (session->data->is_pipe) 3506 return 0; 3507 3508 fprintf(fp, "# missing features: "); 3509 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) { 3510 if (bit) 3511 fprintf(fp, "%s ", feat_ops[bit].name); 3512 } 3513 3514 fprintf(fp, "\n"); 3515 return 0; 3516 } 3517 3518 struct header_fw { 3519 struct feat_writer fw; 3520 struct feat_fd *ff; 3521 }; 3522 3523 static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz) 3524 { 3525 struct header_fw *h = container_of(fw, struct header_fw, fw); 3526 3527 return do_write(h->ff, buf, sz); 3528 } 3529 3530 static int do_write_feat(struct feat_fd *ff, int type, 3531 struct perf_file_section **p, 3532 struct evlist *evlist, 3533 struct feat_copier *fc) 3534 { 3535 int err; 3536 int ret = 0; 3537 3538 if (perf_header__has_feat(ff->ph, type)) { 3539 if (!feat_ops[type].write) 3540 return -1; 3541 3542 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 3543 return -1; 3544 3545 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR); 3546 3547 /* 3548 * Hook to let perf inject copy features sections from the input 3549 * file. 3550 */ 3551 if (fc && fc->copy) { 3552 struct header_fw h = { 3553 .fw.write = feat_writer_cb, 3554 .ff = ff, 3555 }; 3556 3557 /* ->copy() returns 0 if the feature was not copied */ 3558 err = fc->copy(fc, type, &h.fw); 3559 } else { 3560 err = 0; 3561 } 3562 if (!err) 3563 err = feat_ops[type].write(ff, evlist); 3564 if (err < 0) { 3565 pr_debug("failed to write feature %s\n", feat_ops[type].name); 3566 3567 /* undo anything written */ 3568 lseek(ff->fd, (*p)->offset, SEEK_SET); 3569 3570 return -1; 3571 } 3572 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset; 3573 (*p)++; 3574 } 3575 return ret; 3576 } 3577 3578 static int perf_header__adds_write(struct perf_header *header, 3579 struct evlist *evlist, int fd, 3580 struct feat_copier *fc) 3581 { 3582 int nr_sections; 3583 struct feat_fd ff; 3584 struct perf_file_section *feat_sec, *p; 3585 int sec_size; 3586 u64 sec_start; 3587 int feat; 3588 int err; 3589 3590 ff = (struct feat_fd){ 3591 .fd = fd, 3592 .ph = header, 3593 }; 3594 3595 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 3596 if (!nr_sections) 3597 return 0; 3598 3599 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec)); 3600 if (feat_sec == NULL) 3601 return -ENOMEM; 3602 3603 sec_size = sizeof(*feat_sec) * nr_sections; 3604 3605 sec_start = header->feat_offset; 3606 lseek(fd, sec_start + sec_size, SEEK_SET); 3607 3608 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 3609 if (do_write_feat(&ff, feat, &p, evlist, fc)) 3610 perf_header__clear_feat(header, feat); 3611 } 3612 3613 lseek(fd, sec_start, SEEK_SET); 3614 /* 3615 * may write more than needed due to dropped feature, but 3616 * this is okay, reader will skip the missing entries 3617 */ 3618 err = do_write(&ff, feat_sec, sec_size); 3619 if (err < 0) 3620 pr_debug("failed to write feature section\n"); 3621 free(feat_sec); 3622 return err; 3623 } 3624 3625 int perf_header__write_pipe(int fd) 3626 { 3627 struct perf_pipe_file_header f_header; 3628 struct feat_fd ff; 3629 int err; 3630 3631 ff = (struct feat_fd){ .fd = fd }; 3632 3633 f_header = (struct perf_pipe_file_header){ 3634 .magic = PERF_MAGIC, 3635 .size = sizeof(f_header), 3636 }; 3637 3638 err = do_write(&ff, &f_header, sizeof(f_header)); 3639 if (err < 0) { 3640 pr_debug("failed to write perf pipe header\n"); 3641 return err; 3642 } 3643 3644 return 0; 3645 } 3646 3647 static int perf_session__do_write_header(struct perf_session *session, 3648 struct evlist *evlist, 3649 int fd, bool at_exit, 3650 struct feat_copier *fc) 3651 { 3652 struct perf_file_header f_header; 3653 struct perf_file_attr f_attr; 3654 struct perf_header *header = &session->header; 3655 struct evsel *evsel; 3656 struct feat_fd ff; 3657 u64 attr_offset; 3658 int err; 3659 3660 ff = (struct feat_fd){ .fd = fd}; 3661 lseek(fd, sizeof(f_header), SEEK_SET); 3662 3663 evlist__for_each_entry(session->evlist, evsel) { 3664 evsel->id_offset = lseek(fd, 0, SEEK_CUR); 3665 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64)); 3666 if (err < 0) { 3667 pr_debug("failed to write perf header\n"); 3668 return err; 3669 } 3670 } 3671 3672 attr_offset = lseek(ff.fd, 0, SEEK_CUR); 3673 3674 evlist__for_each_entry(evlist, evsel) { 3675 if (evsel->core.attr.size < sizeof(evsel->core.attr)) { 3676 /* 3677 * We are likely in "perf inject" and have read 3678 * from an older file. Update attr size so that 3679 * reader gets the right offset to the ids. 3680 */ 3681 evsel->core.attr.size = sizeof(evsel->core.attr); 3682 } 3683 f_attr = (struct perf_file_attr){ 3684 .attr = evsel->core.attr, 3685 .ids = { 3686 .offset = evsel->id_offset, 3687 .size = evsel->core.ids * sizeof(u64), 3688 } 3689 }; 3690 err = do_write(&ff, &f_attr, sizeof(f_attr)); 3691 if (err < 0) { 3692 pr_debug("failed to write perf header attribute\n"); 3693 return err; 3694 } 3695 } 3696 3697 if (!header->data_offset) 3698 header->data_offset = lseek(fd, 0, SEEK_CUR); 3699 header->feat_offset = header->data_offset + header->data_size; 3700 3701 if (at_exit) { 3702 err = perf_header__adds_write(header, evlist, fd, fc); 3703 if (err < 0) 3704 return err; 3705 } 3706 3707 f_header = (struct perf_file_header){ 3708 .magic = PERF_MAGIC, 3709 .size = sizeof(f_header), 3710 .attr_size = sizeof(f_attr), 3711 .attrs = { 3712 .offset = attr_offset, 3713 .size = evlist->core.nr_entries * sizeof(f_attr), 3714 }, 3715 .data = { 3716 .offset = header->data_offset, 3717 .size = header->data_size, 3718 }, 3719 /* event_types is ignored, store zeros */ 3720 }; 3721 3722 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); 3723 3724 lseek(fd, 0, SEEK_SET); 3725 err = do_write(&ff, &f_header, sizeof(f_header)); 3726 if (err < 0) { 3727 pr_debug("failed to write perf header\n"); 3728 return err; 3729 } 3730 lseek(fd, header->data_offset + header->data_size, SEEK_SET); 3731 3732 return 0; 3733 } 3734 3735 int perf_session__write_header(struct perf_session *session, 3736 struct evlist *evlist, 3737 int fd, bool at_exit) 3738 { 3739 return perf_session__do_write_header(session, evlist, fd, at_exit, NULL); 3740 } 3741 3742 size_t perf_session__data_offset(const struct evlist *evlist) 3743 { 3744 struct evsel *evsel; 3745 size_t data_offset; 3746 3747 data_offset = sizeof(struct perf_file_header); 3748 evlist__for_each_entry(evlist, evsel) { 3749 data_offset += evsel->core.ids * sizeof(u64); 3750 } 3751 data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr); 3752 3753 return data_offset; 3754 } 3755 3756 int perf_session__inject_header(struct perf_session *session, 3757 struct evlist *evlist, 3758 int fd, 3759 struct feat_copier *fc) 3760 { 3761 return perf_session__do_write_header(session, evlist, fd, true, fc); 3762 } 3763 3764 static int perf_header__getbuffer64(struct perf_header *header, 3765 int fd, void *buf, size_t size) 3766 { 3767 if (readn(fd, buf, size) <= 0) 3768 return -1; 3769 3770 if (header->needs_swap) 3771 mem_bswap_64(buf, size); 3772 3773 return 0; 3774 } 3775 3776 int perf_header__process_sections(struct perf_header *header, int fd, 3777 void *data, 3778 int (*process)(struct perf_file_section *section, 3779 struct perf_header *ph, 3780 int feat, int fd, void *data)) 3781 { 3782 struct perf_file_section *feat_sec, *sec; 3783 int nr_sections; 3784 int sec_size; 3785 int feat; 3786 int err; 3787 3788 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 3789 if (!nr_sections) 3790 return 0; 3791 3792 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec)); 3793 if (!feat_sec) 3794 return -1; 3795 3796 sec_size = sizeof(*feat_sec) * nr_sections; 3797 3798 lseek(fd, header->feat_offset, SEEK_SET); 3799 3800 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); 3801 if (err < 0) 3802 goto out_free; 3803 3804 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { 3805 err = process(sec++, header, feat, fd, data); 3806 if (err < 0) 3807 goto out_free; 3808 } 3809 err = 0; 3810 out_free: 3811 free(feat_sec); 3812 return err; 3813 } 3814 3815 static const int attr_file_abi_sizes[] = { 3816 [0] = PERF_ATTR_SIZE_VER0, 3817 [1] = PERF_ATTR_SIZE_VER1, 3818 [2] = PERF_ATTR_SIZE_VER2, 3819 [3] = PERF_ATTR_SIZE_VER3, 3820 [4] = PERF_ATTR_SIZE_VER4, 3821 0, 3822 }; 3823 3824 /* 3825 * In the legacy file format, the magic number is not used to encode endianness. 3826 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based 3827 * on ABI revisions, we need to try all combinations for all endianness to 3828 * detect the endianness. 3829 */ 3830 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) 3831 { 3832 uint64_t ref_size, attr_size; 3833 int i; 3834 3835 for (i = 0 ; attr_file_abi_sizes[i]; i++) { 3836 ref_size = attr_file_abi_sizes[i] 3837 + sizeof(struct perf_file_section); 3838 if (hdr_sz != ref_size) { 3839 attr_size = bswap_64(hdr_sz); 3840 if (attr_size != ref_size) 3841 continue; 3842 3843 ph->needs_swap = true; 3844 } 3845 pr_debug("ABI%d perf.data file detected, need_swap=%d\n", 3846 i, 3847 ph->needs_swap); 3848 return 0; 3849 } 3850 /* could not determine endianness */ 3851 return -1; 3852 } 3853 3854 #define PERF_PIPE_HDR_VER0 16 3855 3856 static const size_t attr_pipe_abi_sizes[] = { 3857 [0] = PERF_PIPE_HDR_VER0, 3858 0, 3859 }; 3860 3861 /* 3862 * In the legacy pipe format, there is an implicit assumption that endianness 3863 * between host recording the samples, and host parsing the samples is the 3864 * same. This is not always the case given that the pipe output may always be 3865 * redirected into a file and analyzed on a different machine with possibly a 3866 * different endianness and perf_event ABI revisions in the perf tool itself. 3867 */ 3868 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) 3869 { 3870 u64 attr_size; 3871 int i; 3872 3873 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { 3874 if (hdr_sz != attr_pipe_abi_sizes[i]) { 3875 attr_size = bswap_64(hdr_sz); 3876 if (attr_size != hdr_sz) 3877 continue; 3878 3879 ph->needs_swap = true; 3880 } 3881 pr_debug("Pipe ABI%d perf.data file detected\n", i); 3882 return 0; 3883 } 3884 return -1; 3885 } 3886 3887 bool is_perf_magic(u64 magic) 3888 { 3889 if (!memcmp(&magic, __perf_magic1, sizeof(magic)) 3890 || magic == __perf_magic2 3891 || magic == __perf_magic2_sw) 3892 return true; 3893 3894 return false; 3895 } 3896 3897 static int check_magic_endian(u64 magic, uint64_t hdr_sz, 3898 bool is_pipe, struct perf_header *ph) 3899 { 3900 int ret; 3901 3902 /* check for legacy format */ 3903 ret = memcmp(&magic, __perf_magic1, sizeof(magic)); 3904 if (ret == 0) { 3905 ph->version = PERF_HEADER_VERSION_1; 3906 pr_debug("legacy perf.data format\n"); 3907 if (is_pipe) 3908 return try_all_pipe_abis(hdr_sz, ph); 3909 3910 return try_all_file_abis(hdr_sz, ph); 3911 } 3912 /* 3913 * the new magic number serves two purposes: 3914 * - unique number to identify actual perf.data files 3915 * - encode endianness of file 3916 */ 3917 ph->version = PERF_HEADER_VERSION_2; 3918 3919 /* check magic number with one endianness */ 3920 if (magic == __perf_magic2) 3921 return 0; 3922 3923 /* check magic number with opposite endianness */ 3924 if (magic != __perf_magic2_sw) 3925 return -1; 3926 3927 ph->needs_swap = true; 3928 3929 return 0; 3930 } 3931 3932 int perf_file_header__read(struct perf_file_header *header, 3933 struct perf_header *ph, int fd) 3934 { 3935 ssize_t ret; 3936 3937 lseek(fd, 0, SEEK_SET); 3938 3939 ret = readn(fd, header, sizeof(*header)); 3940 if (ret <= 0) 3941 return -1; 3942 3943 if (check_magic_endian(header->magic, 3944 header->attr_size, false, ph) < 0) { 3945 pr_debug("magic/endian check failed\n"); 3946 return -1; 3947 } 3948 3949 if (ph->needs_swap) { 3950 mem_bswap_64(header, offsetof(struct perf_file_header, 3951 adds_features)); 3952 } 3953 3954 if (header->size != sizeof(*header)) { 3955 /* Support the previous format */ 3956 if (header->size == offsetof(typeof(*header), adds_features)) 3957 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 3958 else 3959 return -1; 3960 } else if (ph->needs_swap) { 3961 /* 3962 * feature bitmap is declared as an array of unsigned longs -- 3963 * not good since its size can differ between the host that 3964 * generated the data file and the host analyzing the file. 3965 * 3966 * We need to handle endianness, but we don't know the size of 3967 * the unsigned long where the file was generated. Take a best 3968 * guess at determining it: try 64-bit swap first (ie., file 3969 * created on a 64-bit host), and check if the hostname feature 3970 * bit is set (this feature bit is forced on as of fbe96f2). 3971 * If the bit is not, undo the 64-bit swap and try a 32-bit 3972 * swap. If the hostname bit is still not set (e.g., older data 3973 * file), punt and fallback to the original behavior -- 3974 * clearing all feature bits and setting buildid. 3975 */ 3976 mem_bswap_64(&header->adds_features, 3977 BITS_TO_U64(HEADER_FEAT_BITS)); 3978 3979 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 3980 /* unswap as u64 */ 3981 mem_bswap_64(&header->adds_features, 3982 BITS_TO_U64(HEADER_FEAT_BITS)); 3983 3984 /* unswap as u32 */ 3985 mem_bswap_32(&header->adds_features, 3986 BITS_TO_U32(HEADER_FEAT_BITS)); 3987 } 3988 3989 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 3990 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 3991 __set_bit(HEADER_BUILD_ID, header->adds_features); 3992 } 3993 } 3994 3995 memcpy(&ph->adds_features, &header->adds_features, 3996 sizeof(ph->adds_features)); 3997 3998 ph->data_offset = header->data.offset; 3999 ph->data_size = header->data.size; 4000 ph->feat_offset = header->data.offset + header->data.size; 4001 return 0; 4002 } 4003 4004 static int perf_file_section__process(struct perf_file_section *section, 4005 struct perf_header *ph, 4006 int feat, int fd, void *data) 4007 { 4008 struct feat_fd fdd = { 4009 .fd = fd, 4010 .ph = ph, 4011 .size = section->size, 4012 .offset = section->offset, 4013 }; 4014 4015 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 4016 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 4017 "%d, continuing...\n", section->offset, feat); 4018 return 0; 4019 } 4020 4021 if (feat >= HEADER_LAST_FEATURE) { 4022 pr_debug("unknown feature %d, continuing...\n", feat); 4023 return 0; 4024 } 4025 4026 if (!feat_ops[feat].process) 4027 return 0; 4028 4029 return feat_ops[feat].process(&fdd, data); 4030 } 4031 4032 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, 4033 struct perf_header *ph, 4034 struct perf_data* data, 4035 bool repipe, int repipe_fd) 4036 { 4037 struct feat_fd ff = { 4038 .fd = repipe_fd, 4039 .ph = ph, 4040 }; 4041 ssize_t ret; 4042 4043 ret = perf_data__read(data, header, sizeof(*header)); 4044 if (ret <= 0) 4045 return -1; 4046 4047 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { 4048 pr_debug("endian/magic failed\n"); 4049 return -1; 4050 } 4051 4052 if (ph->needs_swap) 4053 header->size = bswap_64(header->size); 4054 4055 if (repipe && do_write(&ff, header, sizeof(*header)) < 0) 4056 return -1; 4057 4058 return 0; 4059 } 4060 4061 static int perf_header__read_pipe(struct perf_session *session, int repipe_fd) 4062 { 4063 struct perf_header *header = &session->header; 4064 struct perf_pipe_file_header f_header; 4065 4066 if (perf_file_header__read_pipe(&f_header, header, session->data, 4067 session->repipe, repipe_fd) < 0) { 4068 pr_debug("incompatible file format\n"); 4069 return -EINVAL; 4070 } 4071 4072 return f_header.size == sizeof(f_header) ? 0 : -1; 4073 } 4074 4075 static int read_attr(int fd, struct perf_header *ph, 4076 struct perf_file_attr *f_attr) 4077 { 4078 struct perf_event_attr *attr = &f_attr->attr; 4079 size_t sz, left; 4080 size_t our_sz = sizeof(f_attr->attr); 4081 ssize_t ret; 4082 4083 memset(f_attr, 0, sizeof(*f_attr)); 4084 4085 /* read minimal guaranteed structure */ 4086 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); 4087 if (ret <= 0) { 4088 pr_debug("cannot read %d bytes of header attr\n", 4089 PERF_ATTR_SIZE_VER0); 4090 return -1; 4091 } 4092 4093 /* on file perf_event_attr size */ 4094 sz = attr->size; 4095 4096 if (ph->needs_swap) 4097 sz = bswap_32(sz); 4098 4099 if (sz == 0) { 4100 /* assume ABI0 */ 4101 sz = PERF_ATTR_SIZE_VER0; 4102 } else if (sz > our_sz) { 4103 pr_debug("file uses a more recent and unsupported ABI" 4104 " (%zu bytes extra)\n", sz - our_sz); 4105 return -1; 4106 } 4107 /* what we have not yet read and that we know about */ 4108 left = sz - PERF_ATTR_SIZE_VER0; 4109 if (left) { 4110 void *ptr = attr; 4111 ptr += PERF_ATTR_SIZE_VER0; 4112 4113 ret = readn(fd, ptr, left); 4114 } 4115 /* read perf_file_section, ids are read in caller */ 4116 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); 4117 4118 return ret <= 0 ? -1 : 0; 4119 } 4120 4121 #ifdef HAVE_LIBTRACEEVENT 4122 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent) 4123 { 4124 struct tep_event *event; 4125 char bf[128]; 4126 4127 /* already prepared */ 4128 if (evsel->tp_format) 4129 return 0; 4130 4131 if (pevent == NULL) { 4132 pr_debug("broken or missing trace data\n"); 4133 return -1; 4134 } 4135 4136 event = tep_find_event(pevent, evsel->core.attr.config); 4137 if (event == NULL) { 4138 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config); 4139 return -1; 4140 } 4141 4142 if (!evsel->name) { 4143 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); 4144 evsel->name = strdup(bf); 4145 if (evsel->name == NULL) 4146 return -1; 4147 } 4148 4149 evsel->tp_format = event; 4150 return 0; 4151 } 4152 4153 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent) 4154 { 4155 struct evsel *pos; 4156 4157 evlist__for_each_entry(evlist, pos) { 4158 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT && 4159 evsel__prepare_tracepoint_event(pos, pevent)) 4160 return -1; 4161 } 4162 4163 return 0; 4164 } 4165 #endif 4166 4167 int perf_session__read_header(struct perf_session *session, int repipe_fd) 4168 { 4169 struct perf_data *data = session->data; 4170 struct perf_header *header = &session->header; 4171 struct perf_file_header f_header; 4172 struct perf_file_attr f_attr; 4173 u64 f_id; 4174 int nr_attrs, nr_ids, i, j, err; 4175 int fd = perf_data__fd(data); 4176 4177 session->evlist = evlist__new(); 4178 if (session->evlist == NULL) 4179 return -ENOMEM; 4180 4181 session->evlist->env = &header->env; 4182 session->machines.host.env = &header->env; 4183 4184 /* 4185 * We can read 'pipe' data event from regular file, 4186 * check for the pipe header regardless of source. 4187 */ 4188 err = perf_header__read_pipe(session, repipe_fd); 4189 if (!err || perf_data__is_pipe(data)) { 4190 data->is_pipe = true; 4191 return err; 4192 } 4193 4194 if (perf_file_header__read(&f_header, header, fd) < 0) 4195 return -EINVAL; 4196 4197 if (header->needs_swap && data->in_place_update) { 4198 pr_err("In-place update not supported when byte-swapping is required\n"); 4199 return -EINVAL; 4200 } 4201 4202 /* 4203 * Sanity check that perf.data was written cleanly; data size is 4204 * initialized to 0 and updated only if the on_exit function is run. 4205 * If data size is still 0 then the file contains only partial 4206 * information. Just warn user and process it as much as it can. 4207 */ 4208 if (f_header.data.size == 0) { 4209 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n" 4210 "Was the 'perf record' command properly terminated?\n", 4211 data->file.path); 4212 } 4213 4214 if (f_header.attr_size == 0) { 4215 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n" 4216 "Was the 'perf record' command properly terminated?\n", 4217 data->file.path); 4218 return -EINVAL; 4219 } 4220 4221 nr_attrs = f_header.attrs.size / f_header.attr_size; 4222 lseek(fd, f_header.attrs.offset, SEEK_SET); 4223 4224 for (i = 0; i < nr_attrs; i++) { 4225 struct evsel *evsel; 4226 off_t tmp; 4227 4228 if (read_attr(fd, header, &f_attr) < 0) 4229 goto out_errno; 4230 4231 if (header->needs_swap) { 4232 f_attr.ids.size = bswap_64(f_attr.ids.size); 4233 f_attr.ids.offset = bswap_64(f_attr.ids.offset); 4234 perf_event__attr_swap(&f_attr.attr); 4235 } 4236 4237 tmp = lseek(fd, 0, SEEK_CUR); 4238 evsel = evsel__new(&f_attr.attr); 4239 4240 if (evsel == NULL) 4241 goto out_delete_evlist; 4242 4243 evsel->needs_swap = header->needs_swap; 4244 /* 4245 * Do it before so that if perf_evsel__alloc_id fails, this 4246 * entry gets purged too at evlist__delete(). 4247 */ 4248 evlist__add(session->evlist, evsel); 4249 4250 nr_ids = f_attr.ids.size / sizeof(u64); 4251 /* 4252 * We don't have the cpu and thread maps on the header, so 4253 * for allocating the perf_sample_id table we fake 1 cpu and 4254 * hattr->ids threads. 4255 */ 4256 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids)) 4257 goto out_delete_evlist; 4258 4259 lseek(fd, f_attr.ids.offset, SEEK_SET); 4260 4261 for (j = 0; j < nr_ids; j++) { 4262 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) 4263 goto out_errno; 4264 4265 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id); 4266 } 4267 4268 lseek(fd, tmp, SEEK_SET); 4269 } 4270 4271 #ifdef HAVE_LIBTRACEEVENT 4272 perf_header__process_sections(header, fd, &session->tevent, 4273 perf_file_section__process); 4274 4275 if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent)) 4276 goto out_delete_evlist; 4277 #else 4278 perf_header__process_sections(header, fd, NULL, perf_file_section__process); 4279 #endif 4280 4281 return 0; 4282 out_errno: 4283 return -errno; 4284 4285 out_delete_evlist: 4286 evlist__delete(session->evlist); 4287 session->evlist = NULL; 4288 return -ENOMEM; 4289 } 4290 4291 int perf_event__process_feature(struct perf_session *session, 4292 union perf_event *event) 4293 { 4294 struct perf_tool *tool = session->tool; 4295 struct feat_fd ff = { .fd = 0 }; 4296 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event; 4297 int type = fe->header.type; 4298 u64 feat = fe->feat_id; 4299 int ret = 0; 4300 4301 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) { 4302 pr_warning("invalid record type %d in pipe-mode\n", type); 4303 return 0; 4304 } 4305 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) { 4306 pr_warning("invalid record type %d in pipe-mode\n", type); 4307 return -1; 4308 } 4309 4310 if (!feat_ops[feat].process) 4311 return 0; 4312 4313 ff.buf = (void *)fe->data; 4314 ff.size = event->header.size - sizeof(*fe); 4315 ff.ph = &session->header; 4316 4317 if (feat_ops[feat].process(&ff, NULL)) { 4318 ret = -1; 4319 goto out; 4320 } 4321 4322 if (!feat_ops[feat].print || !tool->show_feat_hdr) 4323 goto out; 4324 4325 if (!feat_ops[feat].full_only || 4326 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) { 4327 feat_ops[feat].print(&ff, stdout); 4328 } else { 4329 fprintf(stdout, "# %s info available, use -I to display\n", 4330 feat_ops[feat].name); 4331 } 4332 out: 4333 free_event_desc(ff.events); 4334 return ret; 4335 } 4336 4337 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp) 4338 { 4339 struct perf_record_event_update *ev = &event->event_update; 4340 struct perf_cpu_map *map; 4341 size_t ret; 4342 4343 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id); 4344 4345 switch (ev->type) { 4346 case PERF_EVENT_UPDATE__SCALE: 4347 ret += fprintf(fp, "... scale: %f\n", ev->scale.scale); 4348 break; 4349 case PERF_EVENT_UPDATE__UNIT: 4350 ret += fprintf(fp, "... unit: %s\n", ev->unit); 4351 break; 4352 case PERF_EVENT_UPDATE__NAME: 4353 ret += fprintf(fp, "... name: %s\n", ev->name); 4354 break; 4355 case PERF_EVENT_UPDATE__CPUS: 4356 ret += fprintf(fp, "... "); 4357 4358 map = cpu_map__new_data(&ev->cpus.cpus); 4359 if (map) 4360 ret += cpu_map__fprintf(map, fp); 4361 else 4362 ret += fprintf(fp, "failed to get cpus\n"); 4363 break; 4364 default: 4365 ret += fprintf(fp, "... unknown type\n"); 4366 break; 4367 } 4368 4369 return ret; 4370 } 4371 4372 int perf_event__process_attr(struct perf_tool *tool __maybe_unused, 4373 union perf_event *event, 4374 struct evlist **pevlist) 4375 { 4376 u32 i, ids, n_ids; 4377 struct evsel *evsel; 4378 struct evlist *evlist = *pevlist; 4379 4380 if (evlist == NULL) { 4381 *pevlist = evlist = evlist__new(); 4382 if (evlist == NULL) 4383 return -ENOMEM; 4384 } 4385 4386 evsel = evsel__new(&event->attr.attr); 4387 if (evsel == NULL) 4388 return -ENOMEM; 4389 4390 evlist__add(evlist, evsel); 4391 4392 ids = event->header.size; 4393 ids -= (void *)&event->attr.id - (void *)event; 4394 n_ids = ids / sizeof(u64); 4395 /* 4396 * We don't have the cpu and thread maps on the header, so 4397 * for allocating the perf_sample_id table we fake 1 cpu and 4398 * hattr->ids threads. 4399 */ 4400 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids)) 4401 return -ENOMEM; 4402 4403 for (i = 0; i < n_ids; i++) { 4404 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]); 4405 } 4406 4407 return 0; 4408 } 4409 4410 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused, 4411 union perf_event *event, 4412 struct evlist **pevlist) 4413 { 4414 struct perf_record_event_update *ev = &event->event_update; 4415 struct evlist *evlist; 4416 struct evsel *evsel; 4417 struct perf_cpu_map *map; 4418 4419 if (dump_trace) 4420 perf_event__fprintf_event_update(event, stdout); 4421 4422 if (!pevlist || *pevlist == NULL) 4423 return -EINVAL; 4424 4425 evlist = *pevlist; 4426 4427 evsel = evlist__id2evsel(evlist, ev->id); 4428 if (evsel == NULL) 4429 return -EINVAL; 4430 4431 switch (ev->type) { 4432 case PERF_EVENT_UPDATE__UNIT: 4433 free((char *)evsel->unit); 4434 evsel->unit = strdup(ev->unit); 4435 break; 4436 case PERF_EVENT_UPDATE__NAME: 4437 free(evsel->name); 4438 evsel->name = strdup(ev->name); 4439 break; 4440 case PERF_EVENT_UPDATE__SCALE: 4441 evsel->scale = ev->scale.scale; 4442 break; 4443 case PERF_EVENT_UPDATE__CPUS: 4444 map = cpu_map__new_data(&ev->cpus.cpus); 4445 if (map) { 4446 perf_cpu_map__put(evsel->core.own_cpus); 4447 evsel->core.own_cpus = map; 4448 } else 4449 pr_err("failed to get event_update cpus\n"); 4450 default: 4451 break; 4452 } 4453 4454 return 0; 4455 } 4456 4457 #ifdef HAVE_LIBTRACEEVENT 4458 int perf_event__process_tracing_data(struct perf_session *session, 4459 union perf_event *event) 4460 { 4461 ssize_t size_read, padding, size = event->tracing_data.size; 4462 int fd = perf_data__fd(session->data); 4463 char buf[BUFSIZ]; 4464 4465 /* 4466 * The pipe fd is already in proper place and in any case 4467 * we can't move it, and we'd screw the case where we read 4468 * 'pipe' data from regular file. The trace_report reads 4469 * data from 'fd' so we need to set it directly behind the 4470 * event, where the tracing data starts. 4471 */ 4472 if (!perf_data__is_pipe(session->data)) { 4473 off_t offset = lseek(fd, 0, SEEK_CUR); 4474 4475 /* setup for reading amidst mmap */ 4476 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data), 4477 SEEK_SET); 4478 } 4479 4480 size_read = trace_report(fd, &session->tevent, 4481 session->repipe); 4482 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; 4483 4484 if (readn(fd, buf, padding) < 0) { 4485 pr_err("%s: reading input file", __func__); 4486 return -1; 4487 } 4488 if (session->repipe) { 4489 int retw = write(STDOUT_FILENO, buf, padding); 4490 if (retw <= 0 || retw != padding) { 4491 pr_err("%s: repiping tracing data padding", __func__); 4492 return -1; 4493 } 4494 } 4495 4496 if (size_read + padding != size) { 4497 pr_err("%s: tracing data size mismatch", __func__); 4498 return -1; 4499 } 4500 4501 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent); 4502 4503 return size_read + padding; 4504 } 4505 #endif 4506 4507 int perf_event__process_build_id(struct perf_session *session, 4508 union perf_event *event) 4509 { 4510 __event_process_build_id(&event->build_id, 4511 event->build_id.filename, 4512 session); 4513 return 0; 4514 } 4515