1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 #include <limits.h> 5 #include "string2.h" 6 #include <sys/param.h> 7 #include <sys/types.h> 8 #include <byteswap.h> 9 #include <unistd.h> 10 #include <regex.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <linux/compiler.h> 14 #include <linux/list.h> 15 #include <linux/kernel.h> 16 #include <linux/bitops.h> 17 #include <linux/string.h> 18 #include <linux/stringify.h> 19 #include <linux/zalloc.h> 20 #include <sys/stat.h> 21 #include <sys/utsname.h> 22 #include <linux/time64.h> 23 #include <dirent.h> 24 #ifdef HAVE_LIBBPF_SUPPORT 25 #include <bpf/libbpf.h> 26 #endif 27 #include <perf/cpumap.h> 28 #include <tools/libc_compat.h> // reallocarray 29 30 #include "dso.h" 31 #include "evlist.h" 32 #include "evsel.h" 33 #include "util/evsel_fprintf.h" 34 #include "header.h" 35 #include "memswap.h" 36 #include "trace-event.h" 37 #include "session.h" 38 #include "symbol.h" 39 #include "debug.h" 40 #include "cpumap.h" 41 #include "pmu.h" 42 #include "pmus.h" 43 #include "vdso.h" 44 #include "strbuf.h" 45 #include "build-id.h" 46 #include "data.h" 47 #include <api/fs/fs.h> 48 #include <api/io_dir.h> 49 #include "asm/bug.h" 50 #include "tool.h" 51 #include "../perf.h" 52 #include "time-utils.h" 53 #include "units.h" 54 #include "util/util.h" // perf_exe() 55 #include "cputopo.h" 56 #include "bpf-event.h" 57 #include "bpf-utils.h" 58 #include "clockid.h" 59 #include "cacheline.h" 60 61 #include <linux/ctype.h> 62 #include <internal/lib.h> 63 64 #ifdef HAVE_LIBTRACEEVENT 65 #include <event-parse.h> 66 #endif 67 68 /* 69 * nr_ids * sizeof(struct perf_sample_id) must not overflow 70 * size_t on 32-bit; the struct is ~104 bytes (32-bit) or 71 * ~184 bytes (64-bit), so 1<<24 (16M) keeps the product 72 * under 2 GB on 32-bit. 73 * 74 * This is a per-attribute cap only — the total across all 75 * attributes is not capped because legitimate high-core-count 76 * workloads (e.g. 5000 tracepoints × 4096 CPUs) can exceed 77 * a single-attribute limit. 78 */ 79 #define MAX_IDS_PER_ATTR (1 << 24) 80 /* 81 * Cap nr_attrs to prevent resource exhaustion from crafted 82 * files. 65536 is well beyond any real workload (perf stat 83 * typically uses < 100 events) but prevents u64-to-int 84 * truncation on the attr count. 85 */ 86 #define MAX_NR_ATTRS (1 << 16) 87 #define MAX_BPF_DATA_LEN (256 * 1024 * 1024) 88 #define MAX_BPF_PROGS 131072 89 #define MAX_CACHE_ENTRIES 32768 90 #define MAX_GROUP_DESC 32768 91 #define MAX_NUMA_NODES 4096 92 #define MAX_PMU_CAPS 512 93 #define MAX_PMU_MAPPINGS 4096 94 #define MAX_SCHED_DOMAINS 64 95 96 /* 97 * magic2 = "PERFILE2" 98 * must be a numerical value to let the endianness 99 * determine the memory layout. That way we are able 100 * to detect endianness when reading the perf.data file 101 * back. 102 * 103 * we check for legacy (PERFFILE) format. 104 */ 105 static const char *__perf_magic1 = "PERFFILE"; 106 static const u64 __perf_magic2 = 0x32454c4946524550ULL; 107 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; 108 109 #define PERF_MAGIC __perf_magic2 110 #define DNAME_LEN 16 111 112 const char perf_version_string[] = PERF_VERSION; 113 114 struct perf_file_attr { 115 struct perf_event_attr attr; 116 struct perf_file_section ids; 117 }; 118 119 void perf_header__set_feat(struct perf_header *header, int feat) 120 { 121 __set_bit(feat, header->adds_features); 122 } 123 124 void perf_header__clear_feat(struct perf_header *header, int feat) 125 { 126 __clear_bit(feat, header->adds_features); 127 } 128 129 bool perf_header__has_feat(const struct perf_header *header, int feat) 130 { 131 return test_bit(feat, header->adds_features); 132 } 133 134 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size) 135 { 136 ssize_t ret = writen(ff->fd, buf, size); 137 138 if (ret != (ssize_t)size) 139 return ret < 0 ? (int)ret : -1; 140 return 0; 141 } 142 143 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size) 144 { 145 /* struct perf_event_header::size is u16 */ 146 const size_t max_size = 0xffff - sizeof(struct perf_event_header); 147 size_t new_size = ff->size; 148 void *addr; 149 150 if (size + ff->offset > max_size) 151 return -E2BIG; 152 153 while (size > (new_size - ff->offset)) 154 new_size <<= 1; 155 new_size = min(max_size, new_size); 156 157 if (ff->size < new_size) { 158 addr = realloc(ff->buf, new_size); 159 if (!addr) 160 return -ENOMEM; 161 ff->buf = addr; 162 ff->size = new_size; 163 } 164 165 memcpy(ff->buf + ff->offset, buf, size); 166 ff->offset += size; 167 168 return 0; 169 } 170 171 /* Return: 0 if succeeded, -ERR if failed. */ 172 int do_write(struct feat_fd *ff, const void *buf, size_t size) 173 { 174 if (!ff->buf) 175 return __do_write_fd(ff, buf, size); 176 return __do_write_buf(ff, buf, size); 177 } 178 179 /* Return: 0 if succeeded, -ERR if failed. */ 180 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size) 181 { 182 size_t byte_size = BITS_TO_LONGS(size) * sizeof(unsigned long); 183 int i, ret; 184 185 ret = do_write(ff, &size, sizeof(size)); 186 if (ret < 0) 187 return ret; 188 189 /* 190 * The on-disk format uses u64 elements, but the in-memory bitmap 191 * uses unsigned long, which is only 4 bytes on 32-bit architectures. 192 * Copy with bounded size so the last element doesn't read past the 193 * bitmap allocation when BITS_TO_LONGS(size) is odd. 194 */ 195 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 196 u64 val = 0; 197 size_t off = i * sizeof(val); 198 199 memcpy(&val, (char *)set + off, min(sizeof(val), byte_size - off)); 200 ret = do_write(ff, &val, sizeof(val)); 201 if (ret < 0) 202 return ret; 203 } 204 205 return 0; 206 } 207 208 /* Return: 0 if succeeded, -ERR if failed. */ 209 int write_padded(struct feat_fd *ff, const void *bf, 210 size_t count, size_t count_aligned) 211 { 212 static const char zero_buf[NAME_ALIGN]; 213 int err = do_write(ff, bf, count); 214 215 if (!err) 216 err = do_write(ff, zero_buf, count_aligned - count); 217 218 return err; 219 } 220 221 #define string_size(str) \ 222 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32)) 223 224 /* Return: 0 if succeeded, -ERR if failed. */ 225 static int do_write_string(struct feat_fd *ff, const char *str) 226 { 227 u32 len, olen; 228 int ret; 229 230 olen = strlen(str) + 1; 231 len = PERF_ALIGN(olen, NAME_ALIGN); 232 233 /* write len, incl. \0 */ 234 ret = do_write(ff, &len, sizeof(len)); 235 if (ret < 0) 236 return ret; 237 238 return write_padded(ff, str, olen, len); 239 } 240 241 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size) 242 { 243 ssize_t ret = readn(ff->fd, addr, size); 244 245 if (ret != size) 246 return ret < 0 ? (int)ret : -1; 247 ff->offset += size; 248 return 0; 249 } 250 251 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size) 252 { 253 memcpy(addr, ff->buf + ff->offset, size); 254 ff->offset += size; 255 256 return 0; 257 } 258 259 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size) 260 { 261 /* 262 * Reject negative sizes, which on 32-bit can occur when a 263 * u32 >= 0x80000000 is passed as ssize_t. The cast to 264 * ssize_t is safe because perf_header__process_sections() 265 * validates that each section fits within the file size 266 * before any feature callback reaches here, and only 267 * feature sections (metadata like build IDs, topology, etc.) 268 * use this path — these cannot legitimately approach 2GB. 269 */ 270 if (size < 0 || size > (ssize_t)ff->size - ff->offset) 271 return -1; 272 273 if (!ff->buf) 274 return __do_read_fd(ff, addr, size); 275 return __do_read_buf(ff, addr, size); 276 } 277 278 static int do_read_u32(struct feat_fd *ff, u32 *addr) 279 { 280 int ret; 281 282 ret = __do_read(ff, addr, sizeof(*addr)); 283 if (ret) 284 return ret; 285 286 if (ff->ph->needs_swap) 287 *addr = bswap_32(*addr); 288 return 0; 289 } 290 291 static int do_read_u64(struct feat_fd *ff, u64 *addr) 292 { 293 int ret; 294 295 ret = __do_read(ff, addr, sizeof(*addr)); 296 if (ret) 297 return ret; 298 299 if (ff->ph->needs_swap) 300 *addr = bswap_64(*addr); 301 return 0; 302 } 303 304 static char *do_read_string(struct feat_fd *ff) 305 { 306 u32 len; 307 char *buf; 308 309 if (do_read_u32(ff, &len)) 310 return NULL; 311 312 /* At least the null terminator. */ 313 if (len < 1 || len > ff->size - ff->offset) { 314 pr_debug("do_read_string: invalid length %u (remaining %zu)\n", 315 len, (size_t)(ff->size - ff->offset)); 316 return NULL; 317 } 318 319 buf = malloc(len); 320 if (!buf) 321 return NULL; 322 323 if (!__do_read(ff, buf, len)) { 324 /* 325 * do_write_string() writes len including the null 326 * terminator, padded to NAME_ALIGN. Ensure the 327 * string is always null-terminated even if the file 328 * data has been tampered with. 329 */ 330 buf[len - 1] = '\0'; 331 return buf; 332 } 333 334 free(buf); 335 return NULL; 336 } 337 338 /* Return: 0 if succeeded, -ERR if failed. */ 339 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize) 340 { 341 unsigned long *set; 342 u64 size, *p; 343 int i, ret; 344 345 ret = do_read_u64(ff, &size); 346 if (ret) 347 return ret; 348 349 /* Bitmap APIs use int for nbits; reject u64 values that truncate. */ 350 if (size > INT_MAX || 351 BITS_TO_U64(size) > (ff->size - ff->offset) / sizeof(u64)) { 352 pr_debug("do_read_bitmap: size %" PRIu64 " exceeds section bounds\n", size); 353 return -1; 354 } 355 356 /* 357 * bitmap_zalloc() allocates in unsigned long units, which are only 358 * 4 bytes on 32-bit architectures. The read loop below casts the 359 * buffer to u64 * and writes 8-byte elements, so allocate in u64 360 * units to ensure the buffer is large enough. 361 */ 362 set = calloc(BITS_TO_U64(size), sizeof(u64)); 363 if (!set) 364 return -ENOMEM; 365 366 p = (u64 *) set; 367 368 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 369 ret = do_read_u64(ff, p + i); 370 if (ret < 0) { 371 free(set); 372 return ret; 373 } 374 } 375 376 *pset = set; 377 *psize = size; 378 return 0; 379 } 380 381 static int write_tracing_data(struct feat_fd *ff, 382 struct evlist *evlist __maybe_unused) 383 { 384 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 385 return -1; 386 387 #ifdef HAVE_LIBTRACEEVENT 388 return read_tracing_data(ff->fd, &evlist->core.entries); 389 #else 390 pr_err("ERROR: Trying to write tracing data without libtraceevent support.\n"); 391 return -1; 392 #endif 393 } 394 395 static int write_build_id(struct feat_fd *ff, 396 struct evlist *evlist __maybe_unused) 397 { 398 struct perf_session *session; 399 int err; 400 401 session = container_of(ff->ph, struct perf_session, header); 402 403 if (!perf_session__read_build_ids(session, true)) 404 return -1; 405 406 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 407 return -1; 408 409 err = perf_session__write_buildid_table(session, ff); 410 if (err < 0) { 411 pr_debug("failed to write buildid table\n"); 412 return err; 413 } 414 415 return 0; 416 } 417 418 static int write_hostname(struct feat_fd *ff, 419 struct evlist *evlist __maybe_unused) 420 { 421 struct utsname uts; 422 int ret; 423 424 ret = uname(&uts); 425 if (ret < 0) 426 return -1; 427 428 return do_write_string(ff, uts.nodename); 429 } 430 431 static int write_osrelease(struct feat_fd *ff, 432 struct evlist *evlist __maybe_unused) 433 { 434 struct utsname uts; 435 const char *release = NULL; 436 437 if (evlist->session) 438 release = perf_env__os_release(perf_session__env(evlist->session)); 439 440 if (!release) { 441 int ret = uname(&uts); 442 443 if (ret < 0) 444 return -1; 445 release = uts.release; 446 } 447 return do_write_string(ff, release); 448 } 449 450 static int write_arch(struct feat_fd *ff, struct evlist *evlist) 451 { 452 struct utsname uts; 453 const char *arch = NULL; 454 455 if (evlist->session) 456 arch = perf_env__arch(perf_session__env(evlist->session)); 457 458 if (!arch) { 459 int ret = uname(&uts); 460 461 if (ret < 0) 462 return -1; 463 arch = uts.machine; 464 } 465 return do_write_string(ff, arch); 466 } 467 468 static int write_e_machine(struct feat_fd *ff, struct evlist *evlist) 469 { 470 /* e_machine expanded from 16 to 32-bits for alignment. */ 471 uint32_t e_flags; 472 uint32_t e_machine = perf_session__e_machine(evlist->session, &e_flags); 473 int ret; 474 475 ret = do_write(ff, &e_machine, sizeof(e_machine)); 476 if (ret) 477 return ret; 478 479 return do_write(ff, &e_flags, sizeof(e_flags)); 480 } 481 482 static int write_version(struct feat_fd *ff, 483 struct evlist *evlist __maybe_unused) 484 { 485 return do_write_string(ff, perf_version_string); 486 } 487 488 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc) 489 { 490 FILE *file; 491 char *buf = NULL; 492 char *s, *p; 493 const char *search = cpuinfo_proc; 494 size_t len = 0; 495 int ret = -1; 496 497 if (!search) 498 return -1; 499 500 file = fopen("/proc/cpuinfo", "r"); 501 if (!file) 502 return -1; 503 504 while (getline(&buf, &len, file) > 0) { 505 ret = strncmp(buf, search, strlen(search)); 506 if (!ret) 507 break; 508 } 509 510 if (ret) { 511 ret = -1; 512 goto done; 513 } 514 515 s = buf; 516 517 p = strchr(buf, ':'); 518 if (p && *(p+1) == ' ' && *(p+2)) 519 s = p + 2; 520 p = strchr(s, '\n'); 521 if (p) 522 *p = '\0'; 523 524 /* squash extra space characters (branding string) */ 525 p = s; 526 while (*p) { 527 if (isspace(*p)) { 528 char *r = p + 1; 529 char *q = skip_spaces(r); 530 *p = ' '; 531 if (q != (p+1)) 532 while ((*r++ = *q++)); 533 } 534 p++; 535 } 536 ret = do_write_string(ff, s); 537 done: 538 free(buf); 539 fclose(file); 540 return ret; 541 } 542 543 static int write_cpudesc(struct feat_fd *ff, 544 struct evlist *evlist __maybe_unused) 545 { 546 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__) 547 #define CPUINFO_PROC { "cpu", } 548 #elif defined(__s390__) 549 #define CPUINFO_PROC { "vendor_id", } 550 #elif defined(__sh__) 551 #define CPUINFO_PROC { "cpu type", } 552 #elif defined(__alpha__) || defined(__mips__) 553 #define CPUINFO_PROC { "cpu model", } 554 #elif defined(__arm__) 555 #define CPUINFO_PROC { "model name", "Processor", } 556 #elif defined(__arc__) 557 #define CPUINFO_PROC { "Processor", } 558 #elif defined(__xtensa__) 559 #define CPUINFO_PROC { "core ID", } 560 #elif defined(__loongarch__) 561 #define CPUINFO_PROC { "Model Name", } 562 #else 563 #define CPUINFO_PROC { "model name", } 564 #endif 565 const char *cpuinfo_procs[] = CPUINFO_PROC; 566 #undef CPUINFO_PROC 567 unsigned int i; 568 569 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) { 570 int ret; 571 ret = __write_cpudesc(ff, cpuinfo_procs[i]); 572 if (ret >= 0) 573 return ret; 574 } 575 return -1; 576 } 577 578 579 static int write_nrcpus(struct feat_fd *ff, 580 struct evlist *evlist __maybe_unused) 581 { 582 long nr; 583 u32 nrc, nra; 584 int ret; 585 586 nrc = cpu__max_present_cpu().cpu; 587 588 nr = sysconf(_SC_NPROCESSORS_ONLN); 589 if (nr < 0) 590 return -1; 591 592 nra = (u32)(nr & UINT_MAX); 593 594 ret = do_write(ff, &nrc, sizeof(nrc)); 595 if (ret < 0) 596 return ret; 597 598 return do_write(ff, &nra, sizeof(nra)); 599 } 600 601 static int write_event_desc(struct feat_fd *ff, 602 struct evlist *evlist) 603 { 604 struct evsel *evsel; 605 u32 nre, nri, sz; 606 int ret; 607 608 nre = evlist->core.nr_entries; 609 610 /* 611 * write number of events 612 */ 613 ret = do_write(ff, &nre, sizeof(nre)); 614 if (ret < 0) 615 return ret; 616 617 /* 618 * size of perf_event_attr struct 619 */ 620 sz = (u32)sizeof(evsel->core.attr); 621 ret = do_write(ff, &sz, sizeof(sz)); 622 if (ret < 0) 623 return ret; 624 625 evlist__for_each_entry(evlist, evsel) { 626 ret = do_write(ff, &evsel->core.attr, sz); 627 if (ret < 0) 628 return ret; 629 /* 630 * write number of unique id per event 631 * there is one id per instance of an event 632 * 633 * copy into an nri to be independent of the 634 * type of ids, 635 */ 636 nri = evsel->core.ids; 637 ret = do_write(ff, &nri, sizeof(nri)); 638 if (ret < 0) 639 return ret; 640 641 /* 642 * write event string as passed on cmdline 643 */ 644 ret = do_write_string(ff, evsel__name(evsel)); 645 if (ret < 0) 646 return ret; 647 /* 648 * write unique ids for this event 649 */ 650 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64)); 651 if (ret < 0) 652 return ret; 653 } 654 return 0; 655 } 656 657 static int write_cmdline(struct feat_fd *ff, 658 struct evlist *evlist __maybe_unused) 659 { 660 struct perf_env *env = &ff->ph->env; 661 char pbuf[MAXPATHLEN], *buf; 662 int i, ret, n; 663 664 /* actual path to perf binary */ 665 buf = perf_exe(pbuf, MAXPATHLEN); 666 667 /* account for binary path */ 668 n = env->nr_cmdline + 1; 669 670 ret = do_write(ff, &n, sizeof(n)); 671 if (ret < 0) 672 return ret; 673 674 ret = do_write_string(ff, buf); 675 if (ret < 0) 676 return ret; 677 678 for (i = 0 ; i < env->nr_cmdline; i++) { 679 ret = do_write_string(ff, env->cmdline_argv[i]); 680 if (ret < 0) 681 return ret; 682 } 683 return 0; 684 } 685 686 687 static int write_cpu_topology(struct feat_fd *ff, 688 struct evlist *evlist __maybe_unused) 689 { 690 struct perf_env *env = &ff->ph->env; 691 struct cpu_topology *tp; 692 u32 i; 693 int ret, j; 694 695 tp = cpu_topology__new(); 696 if (!tp) 697 return -1; 698 699 ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists)); 700 if (ret < 0) 701 goto done; 702 703 for (i = 0; i < tp->package_cpus_lists; i++) { 704 ret = do_write_string(ff, tp->package_cpus_list[i]); 705 if (ret < 0) 706 goto done; 707 } 708 ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists)); 709 if (ret < 0) 710 goto done; 711 712 for (i = 0; i < tp->core_cpus_lists; i++) { 713 ret = do_write_string(ff, tp->core_cpus_list[i]); 714 if (ret < 0) 715 break; 716 } 717 718 ret = perf_env__read_cpu_topology_map(env); 719 if (ret < 0) 720 goto done; 721 722 for (j = 0; j < env->nr_cpus_avail; j++) { 723 ret = do_write(ff, &env->cpu[j].core_id, 724 sizeof(env->cpu[j].core_id)); 725 if (ret < 0) 726 return ret; 727 ret = do_write(ff, &env->cpu[j].socket_id, 728 sizeof(env->cpu[j].socket_id)); 729 if (ret < 0) 730 return ret; 731 } 732 733 if (!tp->die_cpus_lists) 734 goto done; 735 736 ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists)); 737 if (ret < 0) 738 goto done; 739 740 for (i = 0; i < tp->die_cpus_lists; i++) { 741 ret = do_write_string(ff, tp->die_cpus_list[i]); 742 if (ret < 0) 743 goto done; 744 } 745 746 for (j = 0; j < env->nr_cpus_avail; j++) { 747 ret = do_write(ff, &env->cpu[j].die_id, 748 sizeof(env->cpu[j].die_id)); 749 if (ret < 0) 750 return ret; 751 } 752 753 done: 754 cpu_topology__delete(tp); 755 return ret; 756 } 757 758 759 760 static int write_total_mem(struct feat_fd *ff, 761 struct evlist *evlist __maybe_unused) 762 { 763 char *buf = NULL; 764 FILE *fp; 765 size_t len = 0; 766 int ret = -1, n; 767 uint64_t mem; 768 769 fp = fopen("/proc/meminfo", "r"); 770 if (!fp) 771 return -1; 772 773 while (getline(&buf, &len, fp) > 0) { 774 ret = strncmp(buf, "MemTotal:", 9); 775 if (!ret) 776 break; 777 } 778 if (!ret) { 779 n = sscanf(buf, "%*s %"PRIu64, &mem); 780 if (n == 1) 781 ret = do_write(ff, &mem, sizeof(mem)); 782 } else 783 ret = -1; 784 free(buf); 785 fclose(fp); 786 return ret; 787 } 788 789 static int write_numa_topology(struct feat_fd *ff, 790 struct evlist *evlist __maybe_unused) 791 { 792 struct numa_topology *tp; 793 int ret = -1; 794 u32 i; 795 796 tp = numa_topology__new(); 797 if (!tp) 798 return -ENOMEM; 799 800 ret = do_write(ff, &tp->nr, sizeof(u32)); 801 if (ret < 0) 802 goto err; 803 804 for (i = 0; i < tp->nr; i++) { 805 struct numa_topology_node *n = &tp->nodes[i]; 806 807 ret = do_write(ff, &n->node, sizeof(u32)); 808 if (ret < 0) 809 goto err; 810 811 ret = do_write(ff, &n->mem_total, sizeof(u64)); 812 if (ret) 813 goto err; 814 815 ret = do_write(ff, &n->mem_free, sizeof(u64)); 816 if (ret) 817 goto err; 818 819 ret = do_write_string(ff, n->cpus); 820 if (ret < 0) 821 goto err; 822 } 823 824 ret = 0; 825 826 err: 827 numa_topology__delete(tp); 828 return ret; 829 } 830 831 /* 832 * File format: 833 * 834 * struct pmu_mappings { 835 * u32 pmu_num; 836 * struct pmu_map { 837 * u32 type; 838 * char name[]; 839 * }[pmu_num]; 840 * }; 841 */ 842 843 static int write_pmu_mappings(struct feat_fd *ff, 844 struct evlist *evlist __maybe_unused) 845 { 846 struct perf_pmu *pmu = NULL; 847 u32 pmu_num = 0; 848 int ret; 849 850 /* 851 * Do a first pass to count number of pmu to avoid lseek so this 852 * works in pipe mode as well. 853 */ 854 while ((pmu = perf_pmus__scan(pmu))) 855 pmu_num++; 856 857 ret = do_write(ff, &pmu_num, sizeof(pmu_num)); 858 if (ret < 0) 859 return ret; 860 861 while ((pmu = perf_pmus__scan(pmu))) { 862 ret = do_write(ff, &pmu->type, sizeof(pmu->type)); 863 if (ret < 0) 864 return ret; 865 866 ret = do_write_string(ff, pmu->name); 867 if (ret < 0) 868 return ret; 869 } 870 871 return 0; 872 } 873 874 /* 875 * File format: 876 * 877 * struct group_descs { 878 * u32 nr_groups; 879 * struct group_desc { 880 * char name[]; 881 * u32 leader_idx; 882 * u32 nr_members; 883 * }[nr_groups]; 884 * }; 885 */ 886 static int write_group_desc(struct feat_fd *ff, 887 struct evlist *evlist) 888 { 889 u32 nr_groups = evlist__nr_groups(evlist); 890 struct evsel *evsel; 891 int ret; 892 893 ret = do_write(ff, &nr_groups, sizeof(nr_groups)); 894 if (ret < 0) 895 return ret; 896 897 evlist__for_each_entry(evlist, evsel) { 898 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { 899 const char *name = evsel->group_name ?: "{anon_group}"; 900 u32 leader_idx = evsel->core.idx; 901 u32 nr_members = evsel->core.nr_members; 902 903 ret = do_write_string(ff, name); 904 if (ret < 0) 905 return ret; 906 907 ret = do_write(ff, &leader_idx, sizeof(leader_idx)); 908 if (ret < 0) 909 return ret; 910 911 ret = do_write(ff, &nr_members, sizeof(nr_members)); 912 if (ret < 0) 913 return ret; 914 } 915 } 916 return 0; 917 } 918 919 /* 920 * Return the CPU id as a raw string. 921 * 922 * Each architecture should provide a more precise id string that 923 * can be use to match the architecture's "mapfile". 924 */ 925 char * __weak get_cpuid_str(struct perf_cpu cpu __maybe_unused) 926 { 927 return NULL; 928 } 929 930 char *get_cpuid_allow_env_override(struct perf_cpu cpu) 931 { 932 char *cpuid; 933 static bool printed; 934 935 cpuid = getenv("PERF_CPUID"); 936 if (cpuid) 937 cpuid = strdup(cpuid); 938 if (!cpuid) 939 cpuid = get_cpuid_str(cpu); 940 if (!cpuid) 941 return NULL; 942 943 if (!printed) { 944 pr_debug("Using CPUID %s\n", cpuid); 945 printed = true; 946 } 947 return cpuid; 948 } 949 950 /* Return zero when the cpuid from the mapfile.csv matches the 951 * cpuid string generated on this platform. 952 * Otherwise return non-zero. 953 */ 954 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) 955 { 956 regex_t re; 957 regmatch_t pmatch[1]; 958 int match; 959 960 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) { 961 /* Warn unable to generate match particular string. */ 962 pr_info("Invalid regular expression %s\n", mapcpuid); 963 return 1; 964 } 965 966 match = !regexec(&re, cpuid, 1, pmatch, 0); 967 regfree(&re); 968 if (match) { 969 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); 970 971 /* Verify the entire string matched. */ 972 if (match_len == strlen(cpuid)) 973 return 0; 974 } 975 return 1; 976 } 977 978 /* 979 * default get_cpuid(): nothing gets recorded 980 * actual implementation must be in arch/$(SRCARCH)/util/header.c 981 */ 982 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused, 983 struct perf_cpu cpu __maybe_unused) 984 { 985 return ENOSYS; /* Not implemented */ 986 } 987 988 static int write_cpuid(struct feat_fd *ff, struct evlist *evlist) 989 { 990 struct perf_cpu cpu = perf_cpu_map__min(evlist->core.all_cpus); 991 char buffer[64]; 992 int ret; 993 994 ret = get_cpuid(buffer, sizeof(buffer), cpu); 995 if (ret) 996 return -1; 997 998 return do_write_string(ff, buffer); 999 } 1000 1001 static int write_branch_stack(struct feat_fd *ff __maybe_unused, 1002 struct evlist *evlist __maybe_unused) 1003 { 1004 return 0; 1005 } 1006 1007 static int write_auxtrace(struct feat_fd *ff, 1008 struct evlist *evlist __maybe_unused) 1009 { 1010 struct perf_session *session; 1011 int err; 1012 1013 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 1014 return -1; 1015 1016 session = container_of(ff->ph, struct perf_session, header); 1017 1018 err = auxtrace_index__write(ff->fd, &session->auxtrace_index); 1019 if (err < 0) 1020 pr_err("Failed to write auxtrace index\n"); 1021 return err; 1022 } 1023 1024 static int write_clockid(struct feat_fd *ff, 1025 struct evlist *evlist __maybe_unused) 1026 { 1027 return do_write(ff, &ff->ph->env.clock.clockid_res_ns, 1028 sizeof(ff->ph->env.clock.clockid_res_ns)); 1029 } 1030 1031 static int write_clock_data(struct feat_fd *ff, 1032 struct evlist *evlist __maybe_unused) 1033 { 1034 u64 *data64; 1035 u32 data32; 1036 int ret; 1037 1038 /* version */ 1039 data32 = 1; 1040 1041 ret = do_write(ff, &data32, sizeof(data32)); 1042 if (ret < 0) 1043 return ret; 1044 1045 /* clockid */ 1046 data32 = ff->ph->env.clock.clockid; 1047 1048 ret = do_write(ff, &data32, sizeof(data32)); 1049 if (ret < 0) 1050 return ret; 1051 1052 /* TOD ref time */ 1053 data64 = &ff->ph->env.clock.tod_ns; 1054 1055 ret = do_write(ff, data64, sizeof(*data64)); 1056 if (ret < 0) 1057 return ret; 1058 1059 /* clockid ref time */ 1060 data64 = &ff->ph->env.clock.clockid_ns; 1061 1062 return do_write(ff, data64, sizeof(*data64)); 1063 } 1064 1065 static int write_hybrid_topology(struct feat_fd *ff, 1066 struct evlist *evlist __maybe_unused) 1067 { 1068 struct hybrid_topology *tp; 1069 int ret; 1070 u32 i; 1071 1072 tp = hybrid_topology__new(); 1073 if (!tp) 1074 return -ENOENT; 1075 1076 ret = do_write(ff, &tp->nr, sizeof(u32)); 1077 if (ret < 0) 1078 goto err; 1079 1080 for (i = 0; i < tp->nr; i++) { 1081 struct hybrid_topology_node *n = &tp->nodes[i]; 1082 1083 ret = do_write_string(ff, n->pmu_name); 1084 if (ret < 0) 1085 goto err; 1086 1087 ret = do_write_string(ff, n->cpus); 1088 if (ret < 0) 1089 goto err; 1090 } 1091 1092 ret = 0; 1093 1094 err: 1095 hybrid_topology__delete(tp); 1096 return ret; 1097 } 1098 1099 static int write_dir_format(struct feat_fd *ff, 1100 struct evlist *evlist __maybe_unused) 1101 { 1102 struct perf_session *session; 1103 struct perf_data *data; 1104 1105 session = container_of(ff->ph, struct perf_session, header); 1106 data = session->data; 1107 1108 if (WARN_ON(!perf_data__is_dir(data))) 1109 return -1; 1110 1111 return do_write(ff, &data->dir.version, sizeof(data->dir.version)); 1112 } 1113 1114 static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused, 1115 struct evlist *evlist __maybe_unused) 1116 { 1117 #ifdef HAVE_LIBBPF_SUPPORT 1118 struct perf_env *env = &ff->ph->env; 1119 struct rb_root *root; 1120 struct rb_node *next; 1121 int ret = 0; 1122 1123 down_read(&env->bpf_progs.lock); 1124 1125 ret = do_write(ff, &env->bpf_progs.infos_cnt, 1126 sizeof(env->bpf_progs.infos_cnt)); 1127 if (ret < 0 || env->bpf_progs.infos_cnt == 0) 1128 goto out; 1129 1130 root = &env->bpf_progs.infos; 1131 next = rb_first(root); 1132 while (next) { 1133 struct bpf_prog_info_node *node; 1134 size_t len; 1135 1136 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 1137 next = rb_next(&node->rb_node); 1138 len = sizeof(struct perf_bpil) + 1139 node->info_linear->data_len; 1140 1141 /* before writing to file, translate address to offset */ 1142 bpil_addr_to_offs(node->info_linear); 1143 ret = do_write(ff, node->info_linear, len); 1144 /* 1145 * translate back to address even when do_write() fails, 1146 * so that this function never changes the data. 1147 */ 1148 bpil_offs_to_addr(node->info_linear); 1149 if (ret < 0) 1150 goto out; 1151 } 1152 out: 1153 up_read(&env->bpf_progs.lock); 1154 return ret; 1155 #else 1156 pr_err("ERROR: Trying to write bpf_prog_info without libbpf support.\n"); 1157 return -1; 1158 #endif // HAVE_LIBBPF_SUPPORT 1159 } 1160 1161 static int write_bpf_btf(struct feat_fd *ff __maybe_unused, 1162 struct evlist *evlist __maybe_unused) 1163 { 1164 #ifdef HAVE_LIBBPF_SUPPORT 1165 struct perf_env *env = &ff->ph->env; 1166 struct rb_root *root; 1167 struct rb_node *next; 1168 int ret = 0; 1169 1170 down_read(&env->bpf_progs.lock); 1171 1172 ret = do_write(ff, &env->bpf_progs.btfs_cnt, 1173 sizeof(env->bpf_progs.btfs_cnt)); 1174 1175 if (ret < 0 || env->bpf_progs.btfs_cnt == 0) 1176 goto out; 1177 1178 root = &env->bpf_progs.btfs; 1179 next = rb_first(root); 1180 while (next) { 1181 struct btf_node *node; 1182 1183 node = rb_entry(next, struct btf_node, rb_node); 1184 next = rb_next(&node->rb_node); 1185 ret = do_write(ff, &node->id, 1186 sizeof(u32) * 2 + node->data_size); 1187 if (ret < 0) 1188 goto out; 1189 } 1190 out: 1191 up_read(&env->bpf_progs.lock); 1192 return ret; 1193 #else 1194 pr_err("ERROR: Trying to write btf data without libbpf support.\n"); 1195 return -1; 1196 #endif // HAVE_LIBBPF_SUPPORT 1197 } 1198 1199 static int cpu_cache_level__sort(const void *a, const void *b) 1200 { 1201 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a; 1202 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b; 1203 1204 return cache_a->level - cache_b->level; 1205 } 1206 1207 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b) 1208 { 1209 if (a->level != b->level) 1210 return false; 1211 1212 if (a->line_size != b->line_size) 1213 return false; 1214 1215 if (a->sets != b->sets) 1216 return false; 1217 1218 if (a->ways != b->ways) 1219 return false; 1220 1221 if (strcmp(a->type, b->type)) 1222 return false; 1223 1224 if (strcmp(a->size, b->size)) 1225 return false; 1226 1227 if (strcmp(a->map, b->map)) 1228 return false; 1229 1230 return true; 1231 } 1232 1233 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level) 1234 { 1235 char path[PATH_MAX], file[PATH_MAX]; 1236 struct stat st; 1237 size_t len; 1238 1239 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level); 1240 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path); 1241 1242 if (stat(file, &st)) 1243 return 1; 1244 1245 scnprintf(file, PATH_MAX, "%s/level", path); 1246 if (sysfs__read_int(file, (int *) &cache->level)) 1247 return -1; 1248 1249 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path); 1250 if (sysfs__read_int(file, (int *) &cache->line_size)) 1251 return -1; 1252 1253 scnprintf(file, PATH_MAX, "%s/number_of_sets", path); 1254 if (sysfs__read_int(file, (int *) &cache->sets)) 1255 return -1; 1256 1257 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path); 1258 if (sysfs__read_int(file, (int *) &cache->ways)) 1259 return -1; 1260 1261 scnprintf(file, PATH_MAX, "%s/type", path); 1262 if (sysfs__read_str(file, &cache->type, &len)) 1263 return -1; 1264 1265 cache->type[len] = 0; 1266 cache->type = strim(cache->type); 1267 1268 scnprintf(file, PATH_MAX, "%s/size", path); 1269 if (sysfs__read_str(file, &cache->size, &len)) { 1270 zfree(&cache->type); 1271 return -1; 1272 } 1273 1274 cache->size[len] = 0; 1275 cache->size = strim(cache->size); 1276 1277 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path); 1278 if (sysfs__read_str(file, &cache->map, &len)) { 1279 zfree(&cache->size); 1280 zfree(&cache->type); 1281 return -1; 1282 } 1283 1284 cache->map[len] = 0; 1285 cache->map = strim(cache->map); 1286 return 0; 1287 } 1288 1289 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c) 1290 { 1291 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map); 1292 } 1293 1294 /* 1295 * Build caches levels for a particular CPU from the data in 1296 * /sys/devices/system/cpu/cpu<cpu>/cache/ 1297 * The cache level data is stored in caches[] from index at 1298 * *cntp. 1299 */ 1300 int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp) 1301 { 1302 u16 level; 1303 1304 for (level = 0; level < MAX_CACHE_LVL; level++) { 1305 struct cpu_cache_level c; 1306 int err; 1307 u32 i; 1308 1309 err = cpu_cache_level__read(&c, cpu, level); 1310 if (err < 0) 1311 return err; 1312 1313 if (err == 1) 1314 break; 1315 1316 for (i = 0; i < *cntp; i++) { 1317 if (cpu_cache_level__cmp(&c, &caches[i])) 1318 break; 1319 } 1320 1321 if (i == *cntp) { 1322 caches[*cntp] = c; 1323 *cntp = *cntp + 1; 1324 } else 1325 cpu_cache_level__free(&c); 1326 } 1327 1328 return 0; 1329 } 1330 1331 static int build_caches(struct cpu_cache_level caches[], u32 *cntp) 1332 { 1333 u32 nr, cpu, cnt = 0; 1334 1335 nr = cpu__max_cpu().cpu; 1336 1337 for (cpu = 0; cpu < nr; cpu++) { 1338 int ret = build_caches_for_cpu(cpu, caches, &cnt); 1339 1340 if (ret) 1341 return ret; 1342 } 1343 *cntp = cnt; 1344 return 0; 1345 } 1346 1347 static int write_cache(struct feat_fd *ff, 1348 struct evlist *evlist __maybe_unused) 1349 { 1350 u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL; 1351 struct cpu_cache_level caches[max_caches]; 1352 u32 cnt = 0, i, version = 1; 1353 int ret; 1354 1355 ret = build_caches(caches, &cnt); 1356 if (ret) 1357 goto out; 1358 1359 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort); 1360 1361 ret = do_write(ff, &version, sizeof(u32)); 1362 if (ret < 0) 1363 goto out; 1364 1365 ret = do_write(ff, &cnt, sizeof(u32)); 1366 if (ret < 0) 1367 goto out; 1368 1369 for (i = 0; i < cnt; i++) { 1370 struct cpu_cache_level *c = &caches[i]; 1371 1372 #define _W(v) \ 1373 ret = do_write(ff, &c->v, sizeof(u32)); \ 1374 if (ret < 0) \ 1375 goto out; 1376 1377 _W(level) 1378 _W(line_size) 1379 _W(sets) 1380 _W(ways) 1381 #undef _W 1382 1383 #define _W(v) \ 1384 ret = do_write_string(ff, (const char *) c->v); \ 1385 if (ret < 0) \ 1386 goto out; 1387 1388 _W(type) 1389 _W(size) 1390 _W(map) 1391 #undef _W 1392 } 1393 1394 out: 1395 for (i = 0; i < cnt; i++) 1396 cpu_cache_level__free(&caches[i]); 1397 return ret; 1398 } 1399 1400 static int write_cln_size(struct feat_fd *ff, 1401 struct evlist *evlist __maybe_unused) 1402 { 1403 int cln_size = cacheline_size(); 1404 1405 if (!cln_size) 1406 cln_size = DEFAULT_CACHELINE_SIZE; 1407 1408 ff->ph->env.cln_size = cln_size; 1409 1410 return do_write(ff, &cln_size, sizeof(cln_size)); 1411 } 1412 1413 static int write_stat(struct feat_fd *ff __maybe_unused, 1414 struct evlist *evlist __maybe_unused) 1415 { 1416 return 0; 1417 } 1418 1419 static int write_sample_time(struct feat_fd *ff, 1420 struct evlist *evlist) 1421 { 1422 int ret; 1423 1424 ret = do_write(ff, &evlist->first_sample_time, 1425 sizeof(evlist->first_sample_time)); 1426 if (ret < 0) 1427 return ret; 1428 1429 return do_write(ff, &evlist->last_sample_time, 1430 sizeof(evlist->last_sample_time)); 1431 } 1432 1433 1434 static int memory_node__read(struct memory_node *n, unsigned long idx) 1435 { 1436 unsigned int phys, size = 0; 1437 char path[PATH_MAX]; 1438 struct io_dirent64 *ent; 1439 struct io_dir dir; 1440 1441 #define for_each_memory(mem, dir) \ 1442 while ((ent = io_dir__readdir(&dir)) != NULL) \ 1443 if (strcmp(ent->d_name, ".") && \ 1444 strcmp(ent->d_name, "..") && \ 1445 sscanf(ent->d_name, "memory%u", &mem) == 1) 1446 1447 scnprintf(path, PATH_MAX, 1448 "%s/devices/system/node/node%lu", 1449 sysfs__mountpoint(), idx); 1450 1451 io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); 1452 if (dir.dirfd < 0) { 1453 pr_warning("failed: can't open memory sysfs data '%s'\n", path); 1454 return -1; 1455 } 1456 1457 for_each_memory(phys, dir) { 1458 size = max(phys, size); 1459 } 1460 1461 size++; 1462 1463 n->set = bitmap_zalloc(size); 1464 if (!n->set) { 1465 close(dir.dirfd); 1466 return -ENOMEM; 1467 } 1468 1469 n->node = idx; 1470 n->size = size; 1471 1472 io_dir__rewinddir(&dir); 1473 1474 for_each_memory(phys, dir) { 1475 __set_bit(phys, n->set); 1476 } 1477 1478 close(dir.dirfd); 1479 return 0; 1480 } 1481 1482 static void memory_node__delete_nodes(struct memory_node *nodesp, u64 cnt) 1483 { 1484 for (u64 i = 0; i < cnt; i++) 1485 bitmap_free(nodesp[i].set); 1486 1487 free(nodesp); 1488 } 1489 1490 static int memory_node__sort(const void *a, const void *b) 1491 { 1492 const struct memory_node *na = a; 1493 const struct memory_node *nb = b; 1494 1495 return na->node - nb->node; 1496 } 1497 1498 static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) 1499 { 1500 char path[PATH_MAX]; 1501 struct io_dirent64 *ent; 1502 struct io_dir dir; 1503 int ret = 0; 1504 size_t cnt = 0, size = 0; 1505 struct memory_node *nodes = NULL; 1506 1507 scnprintf(path, PATH_MAX, "%s/devices/system/node/", 1508 sysfs__mountpoint()); 1509 1510 io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); 1511 if (dir.dirfd < 0) { 1512 pr_debug2("%s: couldn't read %s, does this arch have topology information?\n", 1513 __func__, path); 1514 return -1; 1515 } 1516 1517 while (!ret && (ent = io_dir__readdir(&dir))) { 1518 unsigned int idx; 1519 int r; 1520 1521 if (!strcmp(ent->d_name, ".") || 1522 !strcmp(ent->d_name, "..")) 1523 continue; 1524 1525 r = sscanf(ent->d_name, "node%u", &idx); 1526 if (r != 1) 1527 continue; 1528 1529 if (cnt >= size) { 1530 struct memory_node *new_nodes = 1531 reallocarray(nodes, cnt + 4, sizeof(*nodes)); 1532 1533 if (!new_nodes) { 1534 pr_err("Failed to write MEM_TOPOLOGY, size %zd nodes\n", size); 1535 ret = -ENOMEM; 1536 goto out; 1537 } 1538 nodes = new_nodes; 1539 size += 4; 1540 } 1541 ret = memory_node__read(&nodes[cnt], idx); 1542 if (!ret) 1543 cnt += 1; 1544 } 1545 out: 1546 close(dir.dirfd); 1547 if (!ret) { 1548 *cntp = cnt; 1549 *nodesp = nodes; 1550 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort); 1551 } else 1552 memory_node__delete_nodes(nodes, cnt); 1553 1554 return ret; 1555 } 1556 1557 /* 1558 * The MEM_TOPOLOGY holds physical memory map for every 1559 * node in system. The format of data is as follows: 1560 * 1561 * 0 - version | for future changes 1562 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes 1563 * 16 - count | number of nodes 1564 * 1565 * For each node we store map of physical indexes for 1566 * each node: 1567 * 1568 * 32 - node id | node index 1569 * 40 - size | size of bitmap 1570 * 48 - bitmap | bitmap of memory indexes that belongs to node 1571 */ 1572 static int write_mem_topology(struct feat_fd *ff __maybe_unused, 1573 struct evlist *evlist __maybe_unused) 1574 { 1575 struct memory_node *nodes = NULL; 1576 u64 bsize, version = 1, i, nr = 0; 1577 int ret; 1578 1579 ret = sysfs__read_xll("devices/system/memory/block_size_bytes", 1580 (unsigned long long *) &bsize); 1581 if (ret) 1582 return ret; 1583 1584 ret = build_mem_topology(&nodes, &nr); 1585 if (ret) 1586 return ret; 1587 1588 ret = do_write(ff, &version, sizeof(version)); 1589 if (ret < 0) 1590 goto out; 1591 1592 ret = do_write(ff, &bsize, sizeof(bsize)); 1593 if (ret < 0) 1594 goto out; 1595 1596 ret = do_write(ff, &nr, sizeof(nr)); 1597 if (ret < 0) 1598 goto out; 1599 1600 for (i = 0; i < nr; i++) { 1601 struct memory_node *n = &nodes[i]; 1602 1603 #define _W(v) \ 1604 ret = do_write(ff, &n->v, sizeof(n->v)); \ 1605 if (ret < 0) \ 1606 goto out; 1607 1608 _W(node) 1609 _W(size) 1610 1611 #undef _W 1612 1613 ret = do_write_bitmap(ff, n->set, n->size); 1614 if (ret < 0) 1615 goto out; 1616 } 1617 1618 out: 1619 memory_node__delete_nodes(nodes, nr); 1620 return ret; 1621 } 1622 1623 static int write_compressed(struct feat_fd *ff __maybe_unused, 1624 struct evlist *evlist __maybe_unused) 1625 { 1626 int ret; 1627 1628 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver)); 1629 if (ret) 1630 return ret; 1631 1632 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type)); 1633 if (ret) 1634 return ret; 1635 1636 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level)); 1637 if (ret) 1638 return ret; 1639 1640 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio)); 1641 if (ret) 1642 return ret; 1643 1644 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len)); 1645 } 1646 1647 static int __write_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu, 1648 bool write_pmu) 1649 { 1650 struct perf_pmu_caps *caps = NULL; 1651 int ret; 1652 1653 ret = do_write(ff, &pmu->nr_caps, sizeof(pmu->nr_caps)); 1654 if (ret < 0) 1655 return ret; 1656 1657 list_for_each_entry(caps, &pmu->caps, list) { 1658 ret = do_write_string(ff, caps->name); 1659 if (ret < 0) 1660 return ret; 1661 1662 ret = do_write_string(ff, caps->value); 1663 if (ret < 0) 1664 return ret; 1665 } 1666 1667 if (write_pmu) { 1668 ret = do_write_string(ff, pmu->name); 1669 if (ret < 0) 1670 return ret; 1671 } 1672 1673 return ret; 1674 } 1675 1676 static int write_cpu_pmu_caps(struct feat_fd *ff, 1677 struct evlist *evlist __maybe_unused) 1678 { 1679 struct perf_pmu *cpu_pmu = perf_pmus__find_core_pmu(); 1680 int ret; 1681 1682 if (!cpu_pmu) 1683 return -ENOENT; 1684 1685 ret = perf_pmu__caps_parse(cpu_pmu); 1686 if (ret < 0) 1687 return ret; 1688 1689 return __write_pmu_caps(ff, cpu_pmu, false); 1690 } 1691 1692 static int write_pmu_caps(struct feat_fd *ff, 1693 struct evlist *evlist __maybe_unused) 1694 { 1695 struct perf_pmu *pmu = NULL; 1696 int nr_pmu = 0; 1697 int ret; 1698 1699 while ((pmu = perf_pmus__scan(pmu))) { 1700 if (!strcmp(pmu->name, "cpu")) { 1701 /* 1702 * The "cpu" PMU is special and covered by 1703 * HEADER_CPU_PMU_CAPS. Note, core PMUs are 1704 * counted/written here for ARM, s390 and Intel hybrid. 1705 */ 1706 continue; 1707 } 1708 if (perf_pmu__caps_parse(pmu) <= 0) 1709 continue; 1710 nr_pmu++; 1711 } 1712 1713 ret = do_write(ff, &nr_pmu, sizeof(nr_pmu)); 1714 if (ret < 0) 1715 return ret; 1716 1717 if (!nr_pmu) 1718 return 0; 1719 1720 /* 1721 * Note older perf tools assume core PMUs come first, this is a property 1722 * of perf_pmus__scan. 1723 */ 1724 pmu = NULL; 1725 while ((pmu = perf_pmus__scan(pmu))) { 1726 if (!strcmp(pmu->name, "cpu")) { 1727 /* Skip as above. */ 1728 continue; 1729 } 1730 if (perf_pmu__caps_parse(pmu) <= 0) 1731 continue; 1732 ret = __write_pmu_caps(ff, pmu, true); 1733 if (ret < 0) 1734 return ret; 1735 } 1736 return 0; 1737 } 1738 1739 struct cpu_domain_map **build_cpu_domain_map(u32 *schedstat_version, u32 *max_sched_domains, u32 nr) 1740 { 1741 char dname[DNAME_LEN], cpumask[MAX_NR_CPUS]; 1742 struct domain_info *domain_info; 1743 struct cpu_domain_map **cd_map; 1744 char cpulist[MAX_NR_CPUS]; 1745 char *line = NULL; 1746 u32 cpu, domain; 1747 u32 dcount = 0; 1748 size_t len; 1749 FILE *fp; 1750 1751 fp = fopen("/proc/schedstat", "r"); 1752 if (!fp) { 1753 pr_err("Failed to open /proc/schedstat\n"); 1754 return NULL; 1755 } 1756 1757 cd_map = zalloc(sizeof(*cd_map) * nr); 1758 if (!cd_map) 1759 goto out; 1760 1761 while (getline(&line, &len, fp) > 0) { 1762 int retval; 1763 1764 if (strncmp(line, "version", 7) == 0) { 1765 retval = sscanf(line, "version %d\n", schedstat_version); 1766 if (retval != 1) 1767 continue; 1768 1769 } else if (strncmp(line, "cpu", 3) == 0) { 1770 retval = sscanf(line, "cpu%u %*s", &cpu); 1771 if (retval == 1) { 1772 cd_map[cpu] = zalloc(sizeof(*cd_map[cpu])); 1773 if (!cd_map[cpu]) 1774 goto out_free_line; 1775 cd_map[cpu]->cpu = cpu; 1776 } else 1777 continue; 1778 1779 dcount = 0; 1780 } else if (strncmp(line, "domain", 6) == 0) { 1781 struct domain_info **temp_domains; 1782 1783 dcount++; 1784 temp_domains = realloc(cd_map[cpu]->domains, dcount * sizeof(domain_info)); 1785 if (!temp_domains) 1786 goto out_free_line; 1787 else 1788 cd_map[cpu]->domains = temp_domains; 1789 1790 domain_info = zalloc(sizeof(*domain_info)); 1791 if (!domain_info) 1792 goto out_free_line; 1793 1794 cd_map[cpu]->domains[dcount - 1] = domain_info; 1795 1796 if (*schedstat_version >= 17) { 1797 retval = sscanf(line, "domain%u %s %s %*s", &domain, dname, 1798 cpumask); 1799 if (retval != 3) 1800 continue; 1801 1802 domain_info->dname = strdup(dname); 1803 if (!domain_info->dname) 1804 goto out_free_line; 1805 } else { 1806 retval = sscanf(line, "domain%u %s %*s", &domain, cpumask); 1807 if (retval != 2) 1808 continue; 1809 } 1810 1811 domain_info->domain = domain; 1812 if (domain > *max_sched_domains) 1813 *max_sched_domains = domain; 1814 1815 domain_info->cpumask = strdup(cpumask); 1816 if (!domain_info->cpumask) 1817 goto out_free_line; 1818 1819 cpumask_to_cpulist(cpumask, cpulist); 1820 domain_info->cpulist = strdup(cpulist); 1821 if (!domain_info->cpulist) 1822 goto out_free_line; 1823 1824 cd_map[cpu]->nr_domains = dcount; 1825 } 1826 } 1827 1828 out_free_line: 1829 free(line); 1830 out: 1831 fclose(fp); 1832 return cd_map; 1833 } 1834 1835 static int write_cpu_domain_info(struct feat_fd *ff, 1836 struct evlist *evlist __maybe_unused) 1837 { 1838 u32 max_sched_domains = 0, schedstat_version = 0; 1839 struct cpu_domain_map **cd_map; 1840 u32 i, j, nr, ret; 1841 1842 nr = cpu__max_present_cpu().cpu; 1843 1844 cd_map = build_cpu_domain_map(&schedstat_version, &max_sched_domains, nr); 1845 if (!cd_map) 1846 return -1; 1847 1848 ret = do_write(ff, &schedstat_version, sizeof(u32)); 1849 if (ret < 0) 1850 goto out; 1851 1852 max_sched_domains += 1; 1853 ret = do_write(ff, &max_sched_domains, sizeof(u32)); 1854 if (ret < 0) 1855 goto out; 1856 1857 for (i = 0; i < nr; i++) { 1858 if (!cd_map[i]) 1859 continue; 1860 1861 ret = do_write(ff, &cd_map[i]->cpu, sizeof(u32)); 1862 if (ret < 0) 1863 goto out; 1864 1865 ret = do_write(ff, &cd_map[i]->nr_domains, sizeof(u32)); 1866 if (ret < 0) 1867 goto out; 1868 1869 for (j = 0; j < cd_map[i]->nr_domains; j++) { 1870 ret = do_write(ff, &cd_map[i]->domains[j]->domain, sizeof(u32)); 1871 if (ret < 0) 1872 goto out; 1873 if (schedstat_version >= 17) { 1874 ret = do_write_string(ff, cd_map[i]->domains[j]->dname); 1875 if (ret < 0) 1876 goto out; 1877 } 1878 1879 ret = do_write_string(ff, cd_map[i]->domains[j]->cpumask); 1880 if (ret < 0) 1881 goto out; 1882 1883 ret = do_write_string(ff, cd_map[i]->domains[j]->cpulist); 1884 if (ret < 0) 1885 goto out; 1886 } 1887 } 1888 1889 out: 1890 free_cpu_domain_info(cd_map, schedstat_version, nr); 1891 return ret; 1892 } 1893 1894 static void print_hostname(struct feat_fd *ff, FILE *fp) 1895 { 1896 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname); 1897 } 1898 1899 static void print_osrelease(struct feat_fd *ff, FILE *fp) 1900 { 1901 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release); 1902 } 1903 1904 static void print_arch(struct feat_fd *ff, FILE *fp) 1905 { 1906 fprintf(fp, "# arch : %s\n", ff->ph->env.arch); 1907 } 1908 1909 static void print_e_machine(struct feat_fd *ff, FILE *fp) 1910 { 1911 fprintf(fp, "# e_machine : %u\n", ff->ph->env.e_machine); 1912 fprintf(fp, "# e_flags : %u\n", ff->ph->env.e_flags); 1913 } 1914 1915 static void print_cpudesc(struct feat_fd *ff, FILE *fp) 1916 { 1917 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc); 1918 } 1919 1920 static void print_nrcpus(struct feat_fd *ff, FILE *fp) 1921 { 1922 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online); 1923 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail); 1924 } 1925 1926 static void print_version(struct feat_fd *ff, FILE *fp) 1927 { 1928 fprintf(fp, "# perf version : %s\n", ff->ph->env.version); 1929 } 1930 1931 static void print_cmdline(struct feat_fd *ff, FILE *fp) 1932 { 1933 int nr, i; 1934 1935 nr = ff->ph->env.nr_cmdline; 1936 1937 fprintf(fp, "# cmdline : "); 1938 1939 for (i = 0; i < nr; i++) { 1940 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]); 1941 if (!argv_i) { 1942 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]); 1943 } else { 1944 char *mem = argv_i; 1945 do { 1946 char *quote = strchr(argv_i, '\''); 1947 if (!quote) 1948 break; 1949 *quote++ = '\0'; 1950 fprintf(fp, "%s\\\'", argv_i); 1951 argv_i = quote; 1952 } while (1); 1953 fprintf(fp, "%s ", argv_i); 1954 free(mem); 1955 } 1956 } 1957 fputc('\n', fp); 1958 } 1959 1960 static void print_cpu_topology(struct feat_fd *ff, FILE *fp) 1961 { 1962 struct perf_header *ph = ff->ph; 1963 int cpu_nr = ph->env.nr_cpus_avail; 1964 int nr, i; 1965 char *str; 1966 1967 nr = ph->env.nr_sibling_cores; 1968 str = ph->env.sibling_cores; 1969 1970 for (i = 0; i < nr; i++) { 1971 fprintf(fp, "# sibling sockets : %s\n", str); 1972 str += strlen(str) + 1; 1973 } 1974 1975 if (ph->env.nr_sibling_dies) { 1976 nr = ph->env.nr_sibling_dies; 1977 str = ph->env.sibling_dies; 1978 1979 for (i = 0; i < nr; i++) { 1980 fprintf(fp, "# sibling dies : %s\n", str); 1981 str += strlen(str) + 1; 1982 } 1983 } 1984 1985 nr = ph->env.nr_sibling_threads; 1986 str = ph->env.sibling_threads; 1987 1988 for (i = 0; i < nr; i++) { 1989 fprintf(fp, "# sibling threads : %s\n", str); 1990 str += strlen(str) + 1; 1991 } 1992 1993 if (ph->env.nr_sibling_dies) { 1994 if (ph->env.cpu != NULL) { 1995 for (i = 0; i < cpu_nr; i++) 1996 fprintf(fp, "# CPU %d: Core ID %d, " 1997 "Die ID %d, Socket ID %d\n", 1998 i, ph->env.cpu[i].core_id, 1999 ph->env.cpu[i].die_id, 2000 ph->env.cpu[i].socket_id); 2001 } else 2002 fprintf(fp, "# Core ID, Die ID and Socket ID " 2003 "information is not available\n"); 2004 } else { 2005 if (ph->env.cpu != NULL) { 2006 for (i = 0; i < cpu_nr; i++) 2007 fprintf(fp, "# CPU %d: Core ID %d, " 2008 "Socket ID %d\n", 2009 i, ph->env.cpu[i].core_id, 2010 ph->env.cpu[i].socket_id); 2011 } else 2012 fprintf(fp, "# Core ID and Socket ID " 2013 "information is not available\n"); 2014 } 2015 } 2016 2017 static void print_clockid(struct feat_fd *ff, FILE *fp) 2018 { 2019 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n", 2020 ff->ph->env.clock.clockid_res_ns * 1000); 2021 } 2022 2023 static void print_clock_data(struct feat_fd *ff, FILE *fp) 2024 { 2025 struct timespec clockid_ns; 2026 char tstr[64], date[64]; 2027 struct timeval tod_ns; 2028 clockid_t clockid; 2029 struct tm ltime; 2030 u64 ref; 2031 2032 if (!ff->ph->env.clock.enabled) { 2033 fprintf(fp, "# reference time disabled\n"); 2034 return; 2035 } 2036 2037 /* Compute TOD time. */ 2038 ref = ff->ph->env.clock.tod_ns; 2039 tod_ns.tv_sec = ref / NSEC_PER_SEC; 2040 ref -= tod_ns.tv_sec * NSEC_PER_SEC; 2041 tod_ns.tv_usec = ref / NSEC_PER_USEC; 2042 2043 /* Compute clockid time. */ 2044 ref = ff->ph->env.clock.clockid_ns; 2045 clockid_ns.tv_sec = ref / NSEC_PER_SEC; 2046 ref -= clockid_ns.tv_sec * NSEC_PER_SEC; 2047 clockid_ns.tv_nsec = ref; 2048 2049 clockid = ff->ph->env.clock.clockid; 2050 2051 if (localtime_r(&tod_ns.tv_sec, <ime) == NULL) 2052 snprintf(tstr, sizeof(tstr), "<error>"); 2053 else { 2054 strftime(date, sizeof(date), "%F %T", <ime); 2055 scnprintf(tstr, sizeof(tstr), "%s.%06d", 2056 date, (int) tod_ns.tv_usec); 2057 } 2058 2059 fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid); 2060 fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n", 2061 tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec, 2062 (long) clockid_ns.tv_sec, clockid_ns.tv_nsec, 2063 clockid_name(clockid)); 2064 } 2065 2066 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp) 2067 { 2068 int i; 2069 struct hybrid_node *n; 2070 2071 fprintf(fp, "# hybrid cpu system:\n"); 2072 for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) { 2073 n = &ff->ph->env.hybrid_nodes[i]; 2074 fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus); 2075 } 2076 } 2077 2078 static void print_dir_format(struct feat_fd *ff, FILE *fp) 2079 { 2080 struct perf_session *session; 2081 struct perf_data *data; 2082 2083 session = container_of(ff->ph, struct perf_session, header); 2084 data = session->data; 2085 2086 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version); 2087 } 2088 2089 static void print_bpf_prog_info(struct feat_fd *ff __maybe_unused, FILE *fp) 2090 { 2091 #ifdef HAVE_LIBBPF_SUPPORT 2092 struct perf_env *env = &ff->ph->env; 2093 struct rb_root *root; 2094 struct rb_node *next; 2095 2096 down_read(&env->bpf_progs.lock); 2097 2098 root = &env->bpf_progs.infos; 2099 next = rb_first(root); 2100 2101 if (!next) 2102 fprintf(fp, "# bpf_prog_info empty\n"); 2103 2104 while (next) { 2105 struct bpf_prog_info_node *node; 2106 2107 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 2108 next = rb_next(&node->rb_node); 2109 2110 __bpf_event__print_bpf_prog_info(&node->info_linear->info, 2111 env, fp); 2112 } 2113 2114 up_read(&env->bpf_progs.lock); 2115 #else 2116 fprintf(fp, "# bpf_prog_info missing, no libbpf support\n"); 2117 #endif // HAVE_LIBBPF_SUPPORT 2118 } 2119 2120 static void print_bpf_btf(struct feat_fd *ff __maybe_unused, FILE *fp) 2121 { 2122 #ifdef HAVE_LIBBPF_SUPPORT 2123 struct perf_env *env = &ff->ph->env; 2124 struct rb_root *root; 2125 struct rb_node *next; 2126 2127 down_read(&env->bpf_progs.lock); 2128 2129 root = &env->bpf_progs.btfs; 2130 next = rb_first(root); 2131 2132 if (!next) 2133 printf("# btf info empty\n"); 2134 2135 while (next) { 2136 struct btf_node *node; 2137 2138 node = rb_entry(next, struct btf_node, rb_node); 2139 next = rb_next(&node->rb_node); 2140 fprintf(fp, "# btf info of id %u\n", node->id); 2141 } 2142 2143 up_read(&env->bpf_progs.lock); 2144 #else 2145 fprintf(fp, "# bpf btf data missing, no libbpf support\n"); 2146 #endif // HAVE_LIBBPF_SUPPORT 2147 } 2148 2149 static void free_event_desc(struct evsel *events) 2150 { 2151 struct evsel *evsel; 2152 2153 if (!events) 2154 return; 2155 2156 for (evsel = events; evsel->core.attr.size; evsel++) { 2157 zfree(&evsel->name); 2158 zfree(&evsel->core.id); 2159 } 2160 2161 free(events); 2162 } 2163 2164 static bool perf_attr_check(struct perf_event_attr *attr) 2165 { 2166 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) { 2167 pr_warning("Reserved bits are set unexpectedly. " 2168 "Please update perf tool.\n"); 2169 return false; 2170 } 2171 2172 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) { 2173 pr_warning("Unknown sample type (0x%llx) is detected. " 2174 "Please update perf tool.\n", 2175 attr->sample_type); 2176 return false; 2177 } 2178 2179 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) { 2180 pr_warning("Unknown read format (0x%llx) is detected. " 2181 "Please update perf tool.\n", 2182 attr->read_format); 2183 return false; 2184 } 2185 2186 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) && 2187 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) { 2188 pr_warning("Unknown branch sample type (0x%llx) is detected. " 2189 "Please update perf tool.\n", 2190 attr->branch_sample_type); 2191 2192 return false; 2193 } 2194 2195 return true; 2196 } 2197 2198 static struct evsel *read_event_desc(struct feat_fd *ff) 2199 { 2200 struct evsel *evsel, *events = NULL; 2201 u64 *id; 2202 void *buf = NULL; 2203 u32 nre, sz, nr, i, j; 2204 size_t msz; 2205 2206 /* number of events */ 2207 if (do_read_u32(ff, &nre)) 2208 goto error; 2209 2210 /* Size of each of the nre attributes. */ 2211 if (do_read_u32(ff, &sz)) 2212 goto error; 2213 2214 /* 2215 * Require at least one event with an attr no smaller than the 2216 * first published struct, and reject sz values where 2217 * sz + sizeof(u32) would overflow size_t (possible on 32-bit) 2218 * or nre == UINT32_MAX where nre + 1 wraps to 0 in the calloc. 2219 * 2220 * The minimum section footprint per event is sz bytes for the 2221 * attr plus a u32 for the id count, check that nre events fit. 2222 */ 2223 if (!nre || sz < PERF_ATTR_SIZE_VER0 || 2224 sz > ff->size || (size_t)sz > SIZE_MAX - sizeof(u32) || 2225 nre == UINT32_MAX || 2226 nre > (ff->size - ff->offset) / (sz + sizeof(u32))) { 2227 pr_err("Invalid HEADER_EVENT_DESC: nre=%u sz=%u (min %d)\n", 2228 nre, sz, PERF_ATTR_SIZE_VER0); 2229 goto error; 2230 } 2231 2232 /* buffer to hold on file attr struct */ 2233 buf = malloc(sz); 2234 if (!buf) 2235 goto error; 2236 2237 /* the last event terminates with evsel->core.attr.size == 0: */ 2238 events = calloc(nre + 1, sizeof(*events)); 2239 if (!events) 2240 goto error; 2241 2242 msz = sizeof(evsel->core.attr); 2243 if (sz < msz) 2244 msz = sz; 2245 2246 for (i = 0, evsel = events; i < nre; evsel++, i++) { 2247 struct perf_event_attr *attr = buf; 2248 u32 attr_size; 2249 2250 evsel->core.idx = i; 2251 2252 /* 2253 * must read entire on-file attr struct to 2254 * sync up with layout. 2255 */ 2256 if (__do_read(ff, buf, sz)) 2257 goto error; 2258 2259 /* Reject before attr_swap to prevent OOB via bswap_safe() */ 2260 attr_size = ff->ph->needs_swap ? bswap_32(attr->size) : attr->size; 2261 /* ABI0: size == 0 means the producer didn't set it */ 2262 if (!attr_size) { 2263 attr_size = PERF_ATTR_SIZE_VER0; 2264 /* 2265 * Write back so free_event_desc() doesn't 2266 * treat this event as the end-of-array sentinel 2267 * (it iterates while attr.size != 0). 2268 * 2269 * Only for native — the swap path must NOT 2270 * write native-endian VER0 here because 2271 * perf_event__attr_swap() would re-swap it 2272 * to 0x40000000, defeating bswap_safe() bounds. 2273 * perf_event__attr_swap() has its own ABI0 2274 * fallback that sets VER0 after swapping. 2275 */ 2276 if (!ff->ph->needs_swap) 2277 attr->size = attr_size; 2278 } 2279 if (attr_size < PERF_ATTR_SIZE_VER0 || attr_size > sz) { 2280 pr_err("Event %d attr.size (%u) invalid (min: %d, max: %u)\n", 2281 i, attr_size, PERF_ATTR_SIZE_VER0, sz); 2282 goto error; 2283 } 2284 2285 if (ff->ph->needs_swap) 2286 perf_event__attr_swap(buf); 2287 2288 memcpy(&evsel->core.attr, buf, msz); 2289 2290 if (!perf_attr_check(&evsel->core.attr)) 2291 goto error; 2292 2293 if (do_read_u32(ff, &nr)) 2294 goto error; 2295 2296 if (ff->ph->needs_swap) 2297 evsel->needs_swap = true; 2298 2299 evsel->name = do_read_string(ff); 2300 if (!evsel->name) 2301 goto error; 2302 2303 if (!nr) 2304 continue; 2305 2306 /* Prevent oversized allocation from crafted nr */ 2307 if (nr > (ff->size - ff->offset) / sizeof(*id)) { 2308 pr_err("Event %d: id count %u exceeds remaining section\n", i, nr); 2309 goto error; 2310 } 2311 2312 id = calloc(nr, sizeof(*id)); 2313 if (!id) 2314 goto error; 2315 evsel->core.ids = nr; 2316 evsel->core.id = id; 2317 2318 for (j = 0 ; j < nr; j++) { 2319 if (do_read_u64(ff, id)) 2320 goto error; 2321 id++; 2322 } 2323 } 2324 out: 2325 free(buf); 2326 return events; 2327 error: 2328 free_event_desc(events); 2329 events = NULL; 2330 goto out; 2331 } 2332 2333 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val, 2334 void *priv __maybe_unused) 2335 { 2336 return fprintf(fp, ", %s = %s", name, val); 2337 } 2338 2339 static void print_event_desc(struct feat_fd *ff, FILE *fp) 2340 { 2341 struct evsel *evsel, *events; 2342 u32 j; 2343 u64 *id; 2344 2345 if (ff->events) 2346 events = ff->events; 2347 else 2348 events = read_event_desc(ff); 2349 2350 if (!events) { 2351 fprintf(fp, "# event desc: not available or unable to read\n"); 2352 return; 2353 } 2354 2355 for (evsel = events; evsel->core.attr.size; evsel++) { 2356 fprintf(fp, "# event : name = %s, ", evsel->name); 2357 2358 if (evsel->core.ids) { 2359 fprintf(fp, ", id = {"); 2360 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) { 2361 if (j) 2362 fputc(',', fp); 2363 fprintf(fp, " %"PRIu64, *id); 2364 } 2365 fprintf(fp, " }"); 2366 } 2367 2368 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL); 2369 2370 fputc('\n', fp); 2371 } 2372 2373 free_event_desc(events); 2374 ff->events = NULL; 2375 } 2376 2377 static void print_total_mem(struct feat_fd *ff, FILE *fp) 2378 { 2379 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem); 2380 } 2381 2382 static void print_numa_topology(struct feat_fd *ff, FILE *fp) 2383 { 2384 int i; 2385 struct numa_node *n; 2386 2387 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) { 2388 n = &ff->ph->env.numa_nodes[i]; 2389 2390 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," 2391 " free = %"PRIu64" kB\n", 2392 n->node, n->mem_total, n->mem_free); 2393 2394 fprintf(fp, "# node%u cpu list : ", n->node); 2395 cpu_map__fprintf(n->map, fp); 2396 } 2397 } 2398 2399 static void print_cpuid(struct feat_fd *ff, FILE *fp) 2400 { 2401 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid); 2402 } 2403 2404 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp) 2405 { 2406 fprintf(fp, "# contains samples with branch stack\n"); 2407 } 2408 2409 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp) 2410 { 2411 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n"); 2412 } 2413 2414 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp) 2415 { 2416 fprintf(fp, "# contains stat data\n"); 2417 } 2418 2419 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused) 2420 { 2421 int i; 2422 2423 fprintf(fp, "# CPU cache info:\n"); 2424 for (i = 0; i < ff->ph->env.caches_cnt; i++) { 2425 fprintf(fp, "# "); 2426 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]); 2427 } 2428 } 2429 2430 static void print_cln_size(struct feat_fd *ff, FILE *fp) 2431 { 2432 fprintf(fp, "# cacheline size: %u\n", ff->ph->env.cln_size); 2433 } 2434 2435 static void print_compressed(struct feat_fd *ff, FILE *fp) 2436 { 2437 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n", 2438 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown", 2439 ff->ph->env.comp_level, ff->ph->env.comp_ratio); 2440 } 2441 2442 static void __print_pmu_caps(FILE *fp, int nr_caps, char **caps, char *pmu_name) 2443 { 2444 const char *delimiter = ""; 2445 int i; 2446 2447 if (!nr_caps) { 2448 fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name); 2449 return; 2450 } 2451 2452 fprintf(fp, "# %s pmu capabilities: ", pmu_name); 2453 for (i = 0; i < nr_caps; i++) { 2454 fprintf(fp, "%s%s", delimiter, caps[i]); 2455 delimiter = ", "; 2456 } 2457 2458 fprintf(fp, "\n"); 2459 } 2460 2461 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp) 2462 { 2463 __print_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps, 2464 ff->ph->env.cpu_pmu_caps, (char *)"cpu"); 2465 } 2466 2467 static void print_pmu_caps(struct feat_fd *ff, FILE *fp) 2468 { 2469 struct perf_env *env = &ff->ph->env; 2470 uint16_t e_machine = perf_env__e_machine(env, /*e_flags=*/NULL); 2471 2472 for (int i = 0; i < env->nr_pmus_with_caps; i++) { 2473 struct pmu_caps *pmu_caps = &env->pmu_caps[i]; 2474 2475 __print_pmu_caps(fp, pmu_caps->nr_caps, pmu_caps->caps, 2476 pmu_caps->pmu_name); 2477 } 2478 2479 if ((e_machine == EM_X86_64 || e_machine == EM_386) && 2480 perf_env__has_pmu_mapping(env, "ibs_op")) { 2481 char *max_precise = perf_env__find_pmu_cap(env, "cpu", "max_precise"); 2482 2483 if (max_precise != NULL && atoi(max_precise) == 0) 2484 fprintf(fp, "# AMD systems uses ibs_op// PMU for some precise events, e.g.: cycles:p, see the 'perf list' man page for further details.\n"); 2485 } 2486 } 2487 2488 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp) 2489 { 2490 struct perf_env *env = &ff->ph->env; 2491 const char *delimiter = "# pmu mappings: "; 2492 char *str, *tmp; 2493 u32 pmu_num; 2494 u32 type; 2495 2496 pmu_num = env->nr_pmu_mappings; 2497 if (!pmu_num) { 2498 fprintf(fp, "# pmu mappings: not available\n"); 2499 return; 2500 } 2501 2502 str = env->pmu_mappings; 2503 2504 while (pmu_num) { 2505 type = strtoul(str, &tmp, 0); 2506 if (*tmp != ':') 2507 goto error; 2508 2509 str = tmp + 1; 2510 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); 2511 2512 delimiter = ", "; 2513 str += strlen(str) + 1; 2514 pmu_num--; 2515 } 2516 2517 fprintf(fp, "\n"); 2518 2519 if (!pmu_num) 2520 return; 2521 error: 2522 fprintf(fp, "# pmu mappings: unable to read\n"); 2523 } 2524 2525 static void print_group_desc(struct feat_fd *ff, FILE *fp) 2526 { 2527 struct perf_session *session; 2528 struct evsel *evsel; 2529 u32 nr = 0; 2530 2531 session = container_of(ff->ph, struct perf_session, header); 2532 2533 evlist__for_each_entry(session->evlist, evsel) { 2534 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { 2535 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel)); 2536 2537 nr = evsel->core.nr_members - 1; 2538 } else if (nr) { 2539 fprintf(fp, ",%s", evsel__name(evsel)); 2540 2541 if (--nr == 0) 2542 fprintf(fp, "}\n"); 2543 } 2544 } 2545 } 2546 2547 static void print_sample_time(struct feat_fd *ff, FILE *fp) 2548 { 2549 struct perf_session *session; 2550 char time_buf[32]; 2551 double d; 2552 2553 session = container_of(ff->ph, struct perf_session, header); 2554 2555 timestamp__scnprintf_usec(session->evlist->first_sample_time, 2556 time_buf, sizeof(time_buf)); 2557 fprintf(fp, "# time of first sample : %s\n", time_buf); 2558 2559 timestamp__scnprintf_usec(session->evlist->last_sample_time, 2560 time_buf, sizeof(time_buf)); 2561 fprintf(fp, "# time of last sample : %s\n", time_buf); 2562 2563 d = (double)(session->evlist->last_sample_time - 2564 session->evlist->first_sample_time) / NSEC_PER_MSEC; 2565 2566 fprintf(fp, "# sample duration : %10.3f ms\n", d); 2567 } 2568 2569 static void memory_node__fprintf(struct memory_node *n, 2570 unsigned long long bsize, FILE *fp) 2571 { 2572 char buf_map[100], buf_size[50]; 2573 unsigned long long size; 2574 2575 size = bsize * bitmap_weight(n->set, n->size); 2576 unit_number__scnprintf(buf_size, 50, size); 2577 2578 bitmap_scnprintf(n->set, n->size, buf_map, 100); 2579 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map); 2580 } 2581 2582 static void print_mem_topology(struct feat_fd *ff, FILE *fp) 2583 { 2584 struct perf_env *env = &ff->ph->env; 2585 struct memory_node *nodes; 2586 int i, nr; 2587 2588 nodes = env->memory_nodes; 2589 nr = env->nr_memory_nodes; 2590 2591 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n", 2592 nr, env->memory_bsize); 2593 2594 for (i = 0; i < nr; i++) { 2595 memory_node__fprintf(&nodes[i], env->memory_bsize, fp); 2596 } 2597 } 2598 2599 static void print_cpu_domain_info(struct feat_fd *ff, FILE *fp) 2600 { 2601 struct cpu_domain_map **cd_map = ff->ph->env.cpu_domain; 2602 u32 nr = ff->ph->env.nr_cpus_avail; 2603 struct domain_info *d_info; 2604 u32 i, j; 2605 2606 fprintf(fp, "# schedstat version : %u\n", ff->ph->env.schedstat_version); 2607 fprintf(fp, "# Maximum sched domains : %u\n", ff->ph->env.max_sched_domains); 2608 2609 for (i = 0; i < nr; i++) { 2610 if (!cd_map[i]) 2611 continue; 2612 2613 fprintf(fp, "# cpu : %u\n", cd_map[i]->cpu); 2614 fprintf(fp, "# nr_domains : %u\n", cd_map[i]->nr_domains); 2615 2616 for (j = 0; j < cd_map[i]->nr_domains; j++) { 2617 d_info = cd_map[i]->domains[j]; 2618 if (!d_info) 2619 continue; 2620 2621 fprintf(fp, "# Domain : %u\n", d_info->domain); 2622 2623 if (ff->ph->env.schedstat_version >= 17) 2624 fprintf(fp, "# Domain name : %s\n", d_info->dname); 2625 2626 fprintf(fp, "# Domain cpu map : %s\n", d_info->cpumask); 2627 fprintf(fp, "# Domain cpu list : %s\n", d_info->cpulist); 2628 } 2629 } 2630 } 2631 2632 static int __event_process_build_id(struct perf_record_header_build_id *bev, 2633 char *filename, 2634 struct perf_session *session) 2635 { 2636 int err = -1; 2637 struct machine *machine; 2638 u16 cpumode; 2639 struct dso *dso; 2640 enum dso_space_type dso_space; 2641 2642 machine = perf_session__findnew_machine(session, bev->pid); 2643 if (!machine) 2644 goto out; 2645 2646 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 2647 2648 switch (cpumode) { 2649 case PERF_RECORD_MISC_KERNEL: 2650 dso_space = DSO_SPACE__KERNEL; 2651 break; 2652 case PERF_RECORD_MISC_GUEST_KERNEL: 2653 dso_space = DSO_SPACE__KERNEL_GUEST; 2654 break; 2655 case PERF_RECORD_MISC_USER: 2656 case PERF_RECORD_MISC_GUEST_USER: 2657 dso_space = DSO_SPACE__USER; 2658 break; 2659 default: 2660 goto out; 2661 } 2662 2663 dso = machine__findnew_dso(machine, filename); 2664 if (dso != NULL) { 2665 char sbuild_id[SBUILD_ID_SIZE]; 2666 struct build_id bid; 2667 size_t size = BUILD_ID_SIZE; 2668 2669 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE) 2670 size = bev->size; 2671 2672 build_id__init(&bid, bev->data, size); 2673 dso__set_build_id(dso, &bid); 2674 dso__set_header_build_id(dso, true); 2675 2676 if (dso_space != DSO_SPACE__USER) { 2677 struct kmod_path m = { .name = NULL, }; 2678 2679 if (!kmod_path__parse_name(&m, filename) && m.kmod) 2680 dso__set_module_info(dso, &m, machine); 2681 2682 dso__set_kernel(dso, dso_space); 2683 free(m.name); 2684 } 2685 2686 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); 2687 pr_debug("build id event received for %s: %s [%zu]\n", 2688 dso__long_name(dso), sbuild_id, size); 2689 dso__put(dso); 2690 } 2691 2692 err = 0; 2693 out: 2694 return err; 2695 } 2696 2697 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, 2698 int input, u64 offset, u64 size) 2699 { 2700 struct perf_session *session = container_of(header, struct perf_session, header); 2701 struct { 2702 struct perf_event_header header; 2703 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; 2704 char filename[0]; 2705 } old_bev; 2706 struct perf_record_header_build_id bev; 2707 char filename[PATH_MAX]; 2708 u64 limit; 2709 2710 /* Prevent offset + size from wrapping past ULLONG_MAX */ 2711 if (size > ULLONG_MAX - offset) 2712 return -1; 2713 2714 limit = offset + size; 2715 2716 while (offset < limit) { 2717 ssize_t len; 2718 2719 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) 2720 return -1; 2721 2722 if (header->needs_swap) 2723 perf_event_header__bswap(&old_bev.header); 2724 2725 /* size == 0 loops forever; size > remaining reads past section */ 2726 if (old_bev.header.size == 0 || old_bev.header.size > limit - offset) 2727 return -1; 2728 2729 len = old_bev.header.size - sizeof(old_bev); 2730 if (len < 0 || len >= PATH_MAX) { 2731 pr_warning("invalid build_id filename length %zd\n", len); 2732 return -1; 2733 } 2734 2735 if (readn(input, filename, len) != len) 2736 return -1; 2737 /* 2738 * The file data may lack a null terminator, which could 2739 * indicate a corrupt or crafted perf.data file. Ensure 2740 * filename is always a valid C string before passing it 2741 * to functions like machine__findnew_dso(). 2742 */ 2743 filename[len] = '\0'; 2744 2745 bev.header = old_bev.header; 2746 2747 /* 2748 * As the pid is the missing value, we need to fill 2749 * it properly. The header.misc value give us nice hint. 2750 */ 2751 bev.pid = HOST_KERNEL_ID; 2752 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || 2753 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) 2754 bev.pid = DEFAULT_GUEST_KERNEL_ID; 2755 2756 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); 2757 __event_process_build_id(&bev, filename, session); 2758 2759 offset += bev.header.size; 2760 } 2761 2762 return 0; 2763 } 2764 2765 static int perf_header__read_build_ids(struct perf_header *header, 2766 int input, u64 offset, u64 size) 2767 { 2768 struct perf_session *session = container_of(header, struct perf_session, header); 2769 struct perf_record_header_build_id bev; 2770 char filename[PATH_MAX]; 2771 u64 limit, orig_offset = offset; 2772 int err = -1; 2773 2774 /* Prevent offset + size from wrapping past ULLONG_MAX */ 2775 if (size > ULLONG_MAX - offset) 2776 return -1; 2777 2778 limit = offset + size; 2779 2780 while (offset < limit) { 2781 ssize_t len; 2782 2783 if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) 2784 goto out; 2785 2786 if (header->needs_swap) { 2787 perf_event_header__bswap(&bev.header); 2788 bev.pid = bswap_32(bev.pid); 2789 } 2790 2791 /* 2792 * size == 0 would loop forever (offset never advances); 2793 * size > remaining would read past the section boundary. 2794 */ 2795 if (bev.header.size == 0 || bev.header.size > limit - offset) 2796 goto out; 2797 2798 len = bev.header.size - sizeof(bev); 2799 if (len < 0 || len >= PATH_MAX) { 2800 pr_warning("invalid build_id filename length %zd\n", len); 2801 goto out; 2802 } 2803 2804 if (readn(input, filename, len) != len) 2805 goto out; 2806 /* 2807 * The file data may lack a null terminator, which could 2808 * indicate a corrupt or crafted perf.data file. Ensure 2809 * filename is always a valid C string before passing it 2810 * to functions like machine__findnew_dso(). 2811 */ 2812 filename[len] = '\0'; 2813 /* 2814 * The a1645ce1 changeset: 2815 * 2816 * "perf: 'perf kvm' tool for monitoring guest performance from host" 2817 * 2818 * Added a field to struct perf_record_header_build_id that broke the file 2819 * format. 2820 * 2821 * Since the kernel build-id is the first entry, process the 2822 * table using the old format if the well known 2823 * '[kernel.kallsyms]' string for the kernel build-id has the 2824 * first 4 characters chopped off (where the pid_t sits). 2825 */ 2826 /* Guard short filenames against memcmp reading past the buffer */ 2827 if (len >= (ssize_t)sizeof("nel.kallsyms]") - 1 && 2828 memcmp(filename, "nel.kallsyms]", sizeof("nel.kallsyms]") - 1) == 0) { 2829 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) 2830 return -1; 2831 return perf_header__read_build_ids_abi_quirk(header, input, offset, size); 2832 } 2833 2834 __event_process_build_id(&bev, filename, session); 2835 2836 offset += bev.header.size; 2837 } 2838 err = 0; 2839 out: 2840 return err; 2841 } 2842 2843 /* Macro for features that simply need to read and store a string. */ 2844 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \ 2845 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ 2846 {\ 2847 free(ff->ph->env.__feat_env); \ 2848 ff->ph->env.__feat_env = do_read_string(ff); \ 2849 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \ 2850 } 2851 2852 FEAT_PROCESS_STR_FUN(hostname, hostname); 2853 FEAT_PROCESS_STR_FUN(osrelease, os_release); 2854 FEAT_PROCESS_STR_FUN(version, version); 2855 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc); 2856 FEAT_PROCESS_STR_FUN(cpuid, cpuid); 2857 2858 static int process_arch(struct feat_fd *ff, void *data __maybe_unused) 2859 { 2860 free(ff->ph->env.arch); 2861 ff->ph->env.arch = do_read_string(ff); 2862 if (!ff->ph->env.arch) 2863 return -ENOMEM; 2864 return 0; 2865 } 2866 2867 static int process_e_machine(struct feat_fd *ff, void *data __maybe_unused) 2868 { 2869 int ret; 2870 2871 ret = do_read_u32(ff, &ff->ph->env.e_machine); 2872 if (ret) 2873 return ret; 2874 2875 return do_read_u32(ff, &ff->ph->env.e_flags); 2876 } 2877 2878 static int process_tracing_data(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) 2879 { 2880 #ifdef HAVE_LIBTRACEEVENT 2881 ssize_t ret = trace_report(ff->fd, data, false); 2882 2883 return ret < 0 ? -1 : 0; 2884 #else 2885 /* Not an error — the feature is simply unsupported in this build */ 2886 pr_debug("Tracing data present but libtraceevent not available, skipping.\n"); 2887 return 0; 2888 #endif 2889 } 2890 2891 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused) 2892 { 2893 /* lseek fails in pipe mode — fall back to ff->offset */ 2894 off_t offset = lseek(ff->fd, 0, SEEK_CUR); 2895 2896 if (offset == (off_t)-1) 2897 offset = ff->offset; 2898 2899 if (perf_header__read_build_ids(ff->ph, ff->fd, offset, ff->size)) 2900 pr_debug("Failed to read buildids, continuing...\n"); 2901 return 0; 2902 } 2903 2904 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused) 2905 { 2906 struct perf_env *env = &ff->ph->env; 2907 int ret; 2908 u32 nr_cpus_avail, nr_cpus_online; 2909 2910 ret = do_read_u32(ff, &nr_cpus_avail); 2911 if (ret) 2912 return ret; 2913 2914 ret = do_read_u32(ff, &nr_cpus_online); 2915 if (ret) 2916 return ret; 2917 2918 /* 2919 * Cap at 1M CPUs — generous for any real system but prevents 2920 * stack overflow from VLA allocations sized by nr_cpus_avail 2921 * (e.g. DECLARE_BITMAP in builtin-c2c.c node_entry()). 2922 */ 2923 if (nr_cpus_avail > (1U << 20)) { 2924 pr_err("Invalid HEADER_NRCPUS: nr_cpus_avail (%u) exceeds maximum (%u)\n", 2925 nr_cpus_avail, 1U << 20); 2926 return -1; 2927 } 2928 2929 if (nr_cpus_online > nr_cpus_avail) { 2930 pr_err("Invalid HEADER_NRCPUS: nr_cpus_online (%u) > nr_cpus_avail (%u)\n", 2931 nr_cpus_online, nr_cpus_avail); 2932 return -1; 2933 } 2934 2935 env->nr_cpus_avail = (int)nr_cpus_avail; 2936 env->nr_cpus_online = (int)nr_cpus_online; 2937 return 0; 2938 } 2939 2940 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused) 2941 { 2942 struct perf_env *env = &ff->ph->env; 2943 u64 total_mem; 2944 int ret; 2945 2946 ret = do_read_u64(ff, &total_mem); 2947 if (ret) 2948 return -1; 2949 env->total_mem = (unsigned long long)total_mem; 2950 return 0; 2951 } 2952 2953 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx) 2954 { 2955 struct evsel *evsel; 2956 2957 evlist__for_each_entry(evlist, evsel) { 2958 if (evsel->core.idx == idx) 2959 return evsel; 2960 } 2961 2962 return NULL; 2963 } 2964 2965 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event) 2966 { 2967 struct evsel *evsel; 2968 2969 if (!event->name) 2970 return; 2971 2972 evsel = evlist__find_by_index(evlist, event->core.idx); 2973 if (!evsel) 2974 return; 2975 2976 if (evsel->name) 2977 return; 2978 2979 evsel->name = strdup(event->name); 2980 } 2981 2982 static int 2983 process_event_desc(struct feat_fd *ff, void *data __maybe_unused) 2984 { 2985 struct perf_session *session; 2986 struct evsel *evsel, *events = read_event_desc(ff); 2987 2988 if (!events) 2989 return 0; 2990 2991 session = container_of(ff->ph, struct perf_session, header); 2992 2993 if (session->data->is_pipe) { 2994 /* Save events for reading later by print_event_desc, 2995 * since they can't be read again in pipe mode. */ 2996 ff->events = events; 2997 } 2998 2999 for (evsel = events; evsel->core.attr.size; evsel++) 3000 evlist__set_event_name(session->evlist, evsel); 3001 3002 if (!session->data->is_pipe) 3003 free_event_desc(events); 3004 3005 return 0; 3006 } 3007 3008 /* 3009 * Some arbitrary max for the number of command line arguments, 3010 * Wildcards can expand and end up with tons of command line args. 3011 */ 3012 #define MAX_CMDLINE_NR 1048576 3013 3014 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused) 3015 { 3016 struct perf_env *env = &ff->ph->env; 3017 char *str, *cmdline = NULL, **argv = NULL; 3018 u32 nr, i, len = 0; 3019 3020 if (do_read_u32(ff, &nr)) 3021 return -1; 3022 3023 if (nr > MAX_CMDLINE_NR) 3024 return -1; 3025 3026 env->nr_cmdline = nr; 3027 3028 cmdline = zalloc(ff->size + nr + 1); 3029 if (!cmdline) 3030 return -1; 3031 3032 argv = calloc(nr + 1, sizeof(char *)); 3033 if (!argv) 3034 goto error; 3035 3036 for (i = 0; i < nr; i++) { 3037 str = do_read_string(ff); 3038 if (!str) 3039 goto error; 3040 3041 argv[i] = cmdline + len; 3042 memcpy(argv[i], str, strlen(str) + 1); 3043 len += strlen(str) + 1; 3044 free(str); 3045 } 3046 env->cmdline = cmdline; 3047 env->cmdline_argv = (const char **) argv; 3048 return 0; 3049 3050 error: 3051 free(argv); 3052 free(cmdline); 3053 return -1; 3054 } 3055 3056 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused) 3057 { 3058 u32 nr, i; 3059 char *str = NULL; 3060 struct strbuf sb; 3061 struct perf_env *env = &ff->ph->env; 3062 int cpu_nr = env->nr_cpus_avail; 3063 u64 size = 0; 3064 3065 if (cpu_nr == 0) { 3066 pr_err("Invalid HEADER_CPU_TOPOLOGY: missing HEADER_NRCPUS\n"); 3067 return -1; 3068 } 3069 3070 env->cpu = calloc(cpu_nr, sizeof(*env->cpu)); 3071 if (!env->cpu) 3072 return -1; 3073 3074 if (do_read_u32(ff, &nr)) 3075 goto free_cpu; 3076 3077 if (nr > (u32)cpu_nr) { 3078 pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_cores (%u) > nr_cpus_avail (%d)\n", 3079 nr, cpu_nr); 3080 goto free_cpu; 3081 } 3082 3083 env->nr_sibling_cores = nr; 3084 size += sizeof(u32); 3085 if (strbuf_init(&sb, 128) < 0) 3086 goto free_cpu; 3087 3088 for (i = 0; i < nr; i++) { 3089 str = do_read_string(ff); 3090 if (!str) 3091 goto error; 3092 3093 /* include a NULL character at the end */ 3094 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 3095 goto error; 3096 size += string_size(str); 3097 zfree(&str); 3098 } 3099 env->sibling_cores = strbuf_detach(&sb, NULL); 3100 3101 if (do_read_u32(ff, &nr)) 3102 goto free_cpu; 3103 3104 if (nr > (u32)cpu_nr) { 3105 pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_threads (%u) > nr_cpus_avail (%d)\n", 3106 nr, cpu_nr); 3107 goto free_cpu; 3108 } 3109 3110 env->nr_sibling_threads = nr; 3111 size += sizeof(u32); 3112 3113 for (i = 0; i < nr; i++) { 3114 str = do_read_string(ff); 3115 if (!str) 3116 goto error; 3117 3118 /* include a NULL character at the end */ 3119 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 3120 goto error; 3121 size += string_size(str); 3122 zfree(&str); 3123 } 3124 env->sibling_threads = strbuf_detach(&sb, NULL); 3125 3126 /* 3127 * The header may be from old perf, 3128 * which doesn't include core id and socket id information. 3129 */ 3130 if (ff->size <= size) { 3131 zfree(&env->cpu); 3132 return 0; 3133 } 3134 3135 for (i = 0; i < (u32)cpu_nr; i++) { 3136 if (do_read_u32(ff, &nr)) 3137 goto free_cpu; 3138 3139 env->cpu[i].core_id = nr; 3140 size += sizeof(u32); 3141 3142 if (do_read_u32(ff, &nr)) 3143 goto free_cpu; 3144 3145 env->cpu[i].socket_id = nr; 3146 size += sizeof(u32); 3147 } 3148 3149 /* 3150 * The header may be from old perf, 3151 * which doesn't include die information. 3152 */ 3153 if (ff->size <= size) 3154 return 0; 3155 3156 if (do_read_u32(ff, &nr)) 3157 goto free_cpu; 3158 3159 if (nr > (u32)cpu_nr) { 3160 pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_dies (%u) > nr_cpus_avail (%d)\n", 3161 nr, cpu_nr); 3162 goto free_cpu; 3163 } 3164 3165 env->nr_sibling_dies = nr; 3166 size += sizeof(u32); 3167 3168 for (i = 0; i < nr; i++) { 3169 str = do_read_string(ff); 3170 if (!str) 3171 goto error; 3172 3173 /* include a NULL character at the end */ 3174 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 3175 goto error; 3176 size += string_size(str); 3177 zfree(&str); 3178 } 3179 env->sibling_dies = strbuf_detach(&sb, NULL); 3180 3181 for (i = 0; i < (u32)cpu_nr; i++) { 3182 if (do_read_u32(ff, &nr)) 3183 goto free_cpu; 3184 3185 env->cpu[i].die_id = nr; 3186 } 3187 3188 return 0; 3189 3190 error: 3191 strbuf_release(&sb); 3192 zfree(&str); 3193 free_cpu: 3194 zfree(&env->cpu); 3195 return -1; 3196 } 3197 3198 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused) 3199 { 3200 struct perf_env *env = &ff->ph->env; 3201 struct numa_node *nodes, *n; 3202 u32 nr, i; 3203 char *str; 3204 3205 /* nr nodes */ 3206 if (do_read_u32(ff, &nr)) 3207 return -1; 3208 3209 if (nr > MAX_NUMA_NODES) { 3210 pr_err("Invalid HEADER_NUMA_TOPOLOGY: nr_nodes (%u) > %u\n", 3211 nr, MAX_NUMA_NODES); 3212 return -1; 3213 } 3214 3215 if (ff->size < sizeof(u32) + nr * (sizeof(u32) + 2 * sizeof(u64))) { 3216 pr_err("Invalid HEADER_NUMA_TOPOLOGY: section too small (%zu) for %u nodes\n", 3217 ff->size, nr); 3218 return -1; 3219 } 3220 3221 nodes = calloc(nr, sizeof(*nodes)); 3222 if (!nodes) 3223 return -ENOMEM; 3224 3225 for (i = 0; i < nr; i++) { 3226 n = &nodes[i]; 3227 3228 /* node number */ 3229 if (do_read_u32(ff, &n->node)) 3230 goto error; 3231 3232 if (do_read_u64(ff, &n->mem_total)) 3233 goto error; 3234 3235 if (do_read_u64(ff, &n->mem_free)) 3236 goto error; 3237 3238 str = do_read_string(ff); 3239 if (!str) 3240 goto error; 3241 3242 n->map = perf_cpu_map__new(str); 3243 free(str); 3244 if (!n->map) 3245 goto error; 3246 } 3247 env->nr_numa_nodes = nr; 3248 env->numa_nodes = nodes; 3249 return 0; 3250 3251 error: 3252 free(nodes); 3253 return -1; 3254 } 3255 3256 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused) 3257 { 3258 struct perf_env *env = &ff->ph->env; 3259 char *name; 3260 u32 pmu_num; 3261 u32 type; 3262 struct strbuf sb; 3263 3264 if (do_read_u32(ff, &pmu_num)) 3265 return -1; 3266 3267 if (!pmu_num) { 3268 pr_debug("pmu mappings not available\n"); 3269 return 0; 3270 } 3271 3272 if (pmu_num > MAX_PMU_MAPPINGS) { 3273 pr_err("Invalid HEADER_PMU_MAPPINGS: pmu_num (%u) > %u\n", 3274 pmu_num, MAX_PMU_MAPPINGS); 3275 return -1; 3276 } 3277 3278 if (ff->size < sizeof(u32) + pmu_num * 2 * sizeof(u32)) { 3279 pr_err("Invalid HEADER_PMU_MAPPINGS: section too small (%zu) for %u PMUs\n", 3280 ff->size, pmu_num); 3281 return -1; 3282 } 3283 3284 env->nr_pmu_mappings = pmu_num; 3285 if (strbuf_init(&sb, 128) < 0) 3286 return -1; 3287 3288 while (pmu_num) { 3289 if (do_read_u32(ff, &type)) 3290 goto error; 3291 3292 name = do_read_string(ff); 3293 if (!name) 3294 goto error; 3295 3296 if (strbuf_addf(&sb, "%u:%s", type, name) < 0) 3297 goto error; 3298 /* include a NULL character at the end */ 3299 if (strbuf_add(&sb, "", 1) < 0) 3300 goto error; 3301 3302 if (!strcmp(name, "msr")) 3303 env->msr_pmu_type = type; 3304 3305 free(name); 3306 pmu_num--; 3307 } 3308 /* AMD may set it by evlist__has_amd_ibs() from perf_session__new() */ 3309 free(env->pmu_mappings); 3310 env->pmu_mappings = strbuf_detach(&sb, NULL); 3311 return 0; 3312 3313 error: 3314 strbuf_release(&sb); 3315 return -1; 3316 } 3317 3318 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused) 3319 { 3320 struct perf_env *env = &ff->ph->env; 3321 size_t ret = -1; 3322 u32 i, nr, nr_groups; 3323 struct perf_session *session; 3324 struct evsel *evsel, *leader = NULL; 3325 struct group_desc { 3326 char *name; 3327 u32 leader_idx; 3328 u32 nr_members; 3329 } *desc; 3330 3331 if (do_read_u32(ff, &nr_groups)) 3332 return -1; 3333 3334 if (!nr_groups) { 3335 pr_debug("group desc not available\n"); 3336 return 0; 3337 } 3338 3339 if (nr_groups > MAX_GROUP_DESC) { 3340 pr_err("Invalid HEADER_GROUP_DESC: nr_groups (%u) > %u\n", 3341 nr_groups, MAX_GROUP_DESC); 3342 return -1; 3343 } 3344 3345 if (ff->size < sizeof(u32) + nr_groups * 3 * sizeof(u32)) { 3346 pr_err("Invalid HEADER_GROUP_DESC: section too small (%zu) for %u groups\n", 3347 ff->size, nr_groups); 3348 return -1; 3349 } 3350 3351 env->nr_groups = nr_groups; 3352 3353 desc = calloc(nr_groups, sizeof(*desc)); 3354 if (!desc) 3355 return -1; 3356 3357 for (i = 0; i < nr_groups; i++) { 3358 desc[i].name = do_read_string(ff); 3359 if (!desc[i].name) 3360 goto out_free; 3361 3362 if (do_read_u32(ff, &desc[i].leader_idx)) 3363 goto out_free; 3364 3365 if (do_read_u32(ff, &desc[i].nr_members)) 3366 goto out_free; 3367 } 3368 3369 /* 3370 * Rebuild group relationship based on the group_desc 3371 */ 3372 session = container_of(ff->ph, struct perf_session, header); 3373 3374 i = nr = 0; 3375 evlist__for_each_entry(session->evlist, evsel) { 3376 if (i < nr_groups && evsel->core.idx == (int) desc[i].leader_idx) { 3377 evsel__set_leader(evsel, evsel); 3378 /* {anon_group} is a dummy name */ 3379 if (strcmp(desc[i].name, "{anon_group}")) { 3380 evsel->group_name = desc[i].name; 3381 desc[i].name = NULL; 3382 } 3383 evsel->core.nr_members = desc[i].nr_members; 3384 3385 if (i >= nr_groups || nr > 0) { 3386 pr_debug("invalid group desc\n"); 3387 goto out_free; 3388 } 3389 3390 leader = evsel; 3391 nr = evsel->core.nr_members - 1; 3392 i++; 3393 } else if (nr) { 3394 /* This is a group member */ 3395 evsel__set_leader(evsel, leader); 3396 3397 nr--; 3398 } 3399 } 3400 3401 if (i != nr_groups || nr != 0) { 3402 pr_debug("invalid group desc\n"); 3403 goto out_free; 3404 } 3405 3406 ret = 0; 3407 out_free: 3408 for (i = 0; i < nr_groups; i++) 3409 zfree(&desc[i].name); 3410 free(desc); 3411 3412 return ret; 3413 } 3414 3415 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused) 3416 { 3417 struct perf_session *session; 3418 int err; 3419 3420 session = container_of(ff->ph, struct perf_session, header); 3421 3422 err = auxtrace_index__process(ff->fd, ff->size, session, 3423 ff->ph->needs_swap); 3424 if (err < 0) 3425 pr_err("Failed to process auxtrace index\n"); 3426 return err; 3427 } 3428 3429 static int process_cache(struct feat_fd *ff, void *data __maybe_unused) 3430 { 3431 struct perf_env *env = &ff->ph->env; 3432 struct cpu_cache_level *caches; 3433 u32 cnt, i, version; 3434 3435 if (do_read_u32(ff, &version)) 3436 return -1; 3437 3438 if (version != 1) 3439 return -1; 3440 3441 if (do_read_u32(ff, &cnt)) 3442 return -1; 3443 3444 if (cnt > MAX_CACHE_ENTRIES) { 3445 pr_err("Invalid HEADER_CACHE: cnt (%u) > %u\n", 3446 cnt, MAX_CACHE_ENTRIES); 3447 return -1; 3448 } 3449 3450 if (ff->size < 2 * sizeof(u32) + cnt * 7 * sizeof(u32)) { 3451 pr_err("Invalid HEADER_CACHE: section too small (%zu) for %u entries\n", 3452 ff->size, cnt); 3453 return -1; 3454 } 3455 3456 caches = calloc(cnt, sizeof(*caches)); 3457 if (!caches) 3458 return -1; 3459 3460 for (i = 0; i < cnt; i++) { 3461 struct cpu_cache_level *c = &caches[i]; 3462 3463 #define _R(v) \ 3464 if (do_read_u32(ff, &c->v)) \ 3465 goto out_free_caches; \ 3466 3467 _R(level) 3468 _R(line_size) 3469 _R(sets) 3470 _R(ways) 3471 #undef _R 3472 3473 #define _R(v) \ 3474 c->v = do_read_string(ff); \ 3475 if (!c->v) \ 3476 goto out_free_caches; \ 3477 3478 _R(type) 3479 _R(size) 3480 _R(map) 3481 #undef _R 3482 } 3483 3484 env->caches = caches; 3485 env->caches_cnt = cnt; 3486 return 0; 3487 out_free_caches: 3488 for (i = 0; i < cnt; i++) { 3489 free(caches[i].type); 3490 free(caches[i].size); 3491 free(caches[i].map); 3492 } 3493 free(caches); 3494 return -1; 3495 } 3496 3497 static int process_cln_size(struct feat_fd *ff, void *data __maybe_unused) 3498 { 3499 struct perf_env *env = &ff->ph->env; 3500 3501 if (do_read_u32(ff, &env->cln_size)) 3502 return -1; 3503 3504 return 0; 3505 } 3506 3507 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused) 3508 { 3509 struct perf_session *session; 3510 u64 first_sample_time, last_sample_time; 3511 int ret; 3512 3513 session = container_of(ff->ph, struct perf_session, header); 3514 3515 ret = do_read_u64(ff, &first_sample_time); 3516 if (ret) 3517 return -1; 3518 3519 ret = do_read_u64(ff, &last_sample_time); 3520 if (ret) 3521 return -1; 3522 3523 session->evlist->first_sample_time = first_sample_time; 3524 session->evlist->last_sample_time = last_sample_time; 3525 return 0; 3526 } 3527 3528 static int process_mem_topology(struct feat_fd *ff, 3529 void *data __maybe_unused) 3530 { 3531 struct perf_env *env = &ff->ph->env; 3532 struct memory_node *nodes; 3533 u64 version, i, nr, bsize; 3534 int ret = -1; 3535 3536 if (do_read_u64(ff, &version)) 3537 return -1; 3538 3539 if (version != 1) 3540 return -1; 3541 3542 if (do_read_u64(ff, &bsize)) 3543 return -1; 3544 3545 if (do_read_u64(ff, &nr)) 3546 return -1; 3547 3548 if (nr > MAX_NUMA_NODES) { 3549 pr_err("Invalid HEADER_MEM_TOPOLOGY: nr_nodes (%llu) > %u\n", 3550 (unsigned long long)nr, MAX_NUMA_NODES); 3551 return -1; 3552 } 3553 3554 /* Per node: node_id(u64) + mem_size(u64) + bitmap_nr_bits(u64) */ 3555 if (ff->size < 3 * sizeof(u64) + nr * 3 * sizeof(u64)) { 3556 pr_err("Invalid HEADER_MEM_TOPOLOGY: section too small (%zu) for %llu nodes\n", 3557 ff->size, (unsigned long long)nr); 3558 return -1; 3559 } 3560 3561 nodes = calloc(nr, sizeof(*nodes)); 3562 if (!nodes) 3563 return -1; 3564 3565 for (i = 0; i < nr; i++) { 3566 struct memory_node n; 3567 3568 #define _R(v) \ 3569 if (do_read_u64(ff, &n.v)) \ 3570 goto out; \ 3571 3572 _R(node) 3573 _R(size) 3574 3575 #undef _R 3576 3577 if (do_read_bitmap(ff, &n.set, &n.size)) 3578 goto out; 3579 3580 nodes[i] = n; 3581 } 3582 3583 env->memory_bsize = bsize; 3584 env->memory_nodes = nodes; 3585 env->nr_memory_nodes = nr; 3586 ret = 0; 3587 3588 out: 3589 if (ret) 3590 memory_node__delete_nodes(nodes, nr); 3591 return ret; 3592 } 3593 3594 static int process_clockid(struct feat_fd *ff, 3595 void *data __maybe_unused) 3596 { 3597 struct perf_env *env = &ff->ph->env; 3598 3599 if (do_read_u64(ff, &env->clock.clockid_res_ns)) 3600 return -1; 3601 3602 return 0; 3603 } 3604 3605 static int process_clock_data(struct feat_fd *ff, 3606 void *_data __maybe_unused) 3607 { 3608 struct perf_env *env = &ff->ph->env; 3609 u32 data32; 3610 u64 data64; 3611 3612 /* version */ 3613 if (do_read_u32(ff, &data32)) 3614 return -1; 3615 3616 if (data32 != 1) 3617 return -1; 3618 3619 /* clockid */ 3620 if (do_read_u32(ff, &data32)) 3621 return -1; 3622 3623 env->clock.clockid = data32; 3624 3625 /* TOD ref time */ 3626 if (do_read_u64(ff, &data64)) 3627 return -1; 3628 3629 env->clock.tod_ns = data64; 3630 3631 /* clockid ref time */ 3632 if (do_read_u64(ff, &data64)) 3633 return -1; 3634 3635 env->clock.clockid_ns = data64; 3636 env->clock.enabled = true; 3637 return 0; 3638 } 3639 3640 static int process_hybrid_topology(struct feat_fd *ff, 3641 void *data __maybe_unused) 3642 { 3643 struct perf_env *env = &ff->ph->env; 3644 struct hybrid_node *nodes, *n; 3645 u32 nr, i; 3646 3647 /* nr nodes */ 3648 if (do_read_u32(ff, &nr)) 3649 return -1; 3650 3651 if (nr > MAX_PMU_MAPPINGS) { 3652 pr_err("Invalid HEADER_HYBRID_TOPOLOGY: nr_nodes (%u) > %u\n", 3653 nr, MAX_PMU_MAPPINGS); 3654 return -1; 3655 } 3656 3657 if (ff->size < sizeof(u32) + nr * 2 * sizeof(u32)) { 3658 pr_err("Invalid HEADER_HYBRID_TOPOLOGY: section too small (%zu) for %u nodes\n", 3659 ff->size, nr); 3660 return -1; 3661 } 3662 3663 nodes = calloc(nr, sizeof(*nodes)); 3664 if (!nodes) 3665 return -ENOMEM; 3666 3667 for (i = 0; i < nr; i++) { 3668 n = &nodes[i]; 3669 3670 n->pmu_name = do_read_string(ff); 3671 if (!n->pmu_name) 3672 goto error; 3673 3674 n->cpus = do_read_string(ff); 3675 if (!n->cpus) 3676 goto error; 3677 } 3678 3679 env->nr_hybrid_nodes = nr; 3680 env->hybrid_nodes = nodes; 3681 return 0; 3682 3683 error: 3684 for (i = 0; i < nr; i++) { 3685 free(nodes[i].pmu_name); 3686 free(nodes[i].cpus); 3687 } 3688 3689 free(nodes); 3690 return -1; 3691 } 3692 3693 static int process_dir_format(struct feat_fd *ff, 3694 void *_data __maybe_unused) 3695 { 3696 struct perf_session *session; 3697 struct perf_data *data; 3698 3699 session = container_of(ff->ph, struct perf_session, header); 3700 data = session->data; 3701 3702 if (WARN_ON(!perf_data__is_dir(data))) 3703 return -1; 3704 3705 return do_read_u64(ff, &data->dir.version); 3706 } 3707 3708 static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) 3709 { 3710 #ifdef HAVE_LIBBPF_SUPPORT 3711 struct bpf_prog_info_node *info_node; 3712 struct perf_env *env = &ff->ph->env; 3713 struct perf_bpil *info_linear; 3714 u32 count, i; 3715 int err = -1; 3716 3717 if (ff->ph->needs_swap) { 3718 pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n"); 3719 return 0; 3720 } 3721 3722 if (do_read_u32(ff, &count)) 3723 return -1; 3724 3725 if (count > MAX_BPF_PROGS) { 3726 pr_err("Invalid HEADER_BPF_PROG_INFO: count (%u) > %u\n", 3727 count, MAX_BPF_PROGS); 3728 return -1; 3729 } 3730 3731 if (ff->size < sizeof(u32) + count * (2 * sizeof(u32) + sizeof(u64))) { 3732 pr_err("Invalid HEADER_BPF_PROG_INFO: section too small (%zu) for %u entries\n", 3733 ff->size, count); 3734 return -1; 3735 } 3736 3737 down_write(&env->bpf_progs.lock); 3738 3739 for (i = 0; i < count; ++i) { 3740 u32 info_len, data_len; 3741 3742 info_linear = NULL; 3743 info_node = NULL; 3744 if (do_read_u32(ff, &info_len)) 3745 goto out; 3746 if (do_read_u32(ff, &data_len)) 3747 goto out; 3748 3749 if (info_len > sizeof(struct bpf_prog_info)) { 3750 pr_warning("detected invalid bpf_prog_info\n"); 3751 goto out; 3752 } 3753 3754 if (data_len > MAX_BPF_DATA_LEN) { 3755 pr_warning("Invalid HEADER_BPF_PROG_INFO: data_len (%u) too large\n", 3756 data_len); 3757 goto out; 3758 } 3759 3760 info_linear = malloc(sizeof(struct perf_bpil) + 3761 data_len); 3762 if (!info_linear) 3763 goto out; 3764 info_linear->info_len = sizeof(struct bpf_prog_info); 3765 info_linear->data_len = data_len; 3766 if (do_read_u64(ff, (u64 *)(&info_linear->arrays))) 3767 goto out; 3768 if (__do_read(ff, &info_linear->info, info_len)) 3769 goto out; 3770 if (info_len < sizeof(struct bpf_prog_info)) 3771 memset(((void *)(&info_linear->info)) + info_len, 0, 3772 sizeof(struct bpf_prog_info) - info_len); 3773 3774 if (__do_read(ff, info_linear->data, data_len)) 3775 goto out; 3776 3777 info_node = malloc(sizeof(struct bpf_prog_info_node)); 3778 if (!info_node) 3779 goto out; 3780 3781 /* after reading from file, translate offset to address */ 3782 bpil_offs_to_addr(info_linear); 3783 info_node->info_linear = info_linear; 3784 info_node->metadata = NULL; 3785 if (!__perf_env__insert_bpf_prog_info(env, info_node)) { 3786 free(info_linear); 3787 free(info_node); 3788 } 3789 } 3790 3791 up_write(&env->bpf_progs.lock); 3792 return 0; 3793 out: 3794 free(info_linear); 3795 free(info_node); 3796 up_write(&env->bpf_progs.lock); 3797 return err; 3798 #else 3799 /* Not an error — the feature is simply unsupported in this build */ 3800 pr_debug("BPF prog info present but libbpf not available, skipping.\n"); 3801 return 0; 3802 #endif // HAVE_LIBBPF_SUPPORT 3803 } 3804 3805 static int process_bpf_btf(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) 3806 { 3807 #ifdef HAVE_LIBBPF_SUPPORT 3808 struct perf_env *env = &ff->ph->env; 3809 struct btf_node *node = NULL; 3810 u32 count, i; 3811 int err = -1; 3812 3813 if (ff->ph->needs_swap) { 3814 pr_warning("interpreting btf from systems with endianness is not yet supported\n"); 3815 return 0; 3816 } 3817 3818 if (do_read_u32(ff, &count)) 3819 return -1; 3820 3821 if (count > MAX_BPF_PROGS) { 3822 pr_err("bpf btf count %u too large (max %u)\n", count, MAX_BPF_PROGS); 3823 return -1; 3824 } 3825 3826 if (ff->size < sizeof(u32) + count * 2 * sizeof(u32)) { 3827 pr_err("Invalid HEADER_BPF_BTF: section too small (%zu) for %u entries\n", 3828 ff->size, count); 3829 return -1; 3830 } 3831 3832 down_write(&env->bpf_progs.lock); 3833 3834 for (i = 0; i < count; ++i) { 3835 u32 id, data_size; 3836 3837 if (do_read_u32(ff, &id)) 3838 goto out; 3839 if (do_read_u32(ff, &data_size)) 3840 goto out; 3841 3842 if (data_size > MAX_BPF_DATA_LEN) { 3843 pr_err("bpf btf data size %u too large (max %u)\n", 3844 data_size, MAX_BPF_DATA_LEN); 3845 goto out; 3846 } 3847 3848 node = malloc(sizeof(struct btf_node) + data_size); 3849 if (!node) 3850 goto out; 3851 3852 node->id = id; 3853 node->data_size = data_size; 3854 3855 if (__do_read(ff, node->data, data_size)) 3856 goto out; 3857 3858 if (!__perf_env__insert_btf(env, node)) 3859 free(node); 3860 node = NULL; 3861 } 3862 3863 err = 0; 3864 out: 3865 up_write(&env->bpf_progs.lock); 3866 free(node); 3867 return err; 3868 #else 3869 /* Not an error — the feature is simply unsupported in this build */ 3870 pr_debug("BTF data present but libbpf not available, skipping.\n"); 3871 return 0; 3872 #endif // HAVE_LIBBPF_SUPPORT 3873 } 3874 3875 static int process_compressed(struct feat_fd *ff, 3876 void *data __maybe_unused) 3877 { 3878 struct perf_env *env = &ff->ph->env; 3879 3880 if (do_read_u32(ff, &(env->comp_ver))) 3881 return -1; 3882 3883 if (do_read_u32(ff, &(env->comp_type))) 3884 return -1; 3885 3886 if (do_read_u32(ff, &(env->comp_level))) 3887 return -1; 3888 3889 if (do_read_u32(ff, &(env->comp_ratio))) 3890 return -1; 3891 3892 if (do_read_u32(ff, &(env->comp_mmap_len))) 3893 return -1; 3894 3895 /* 3896 * FIXME: perf.data should record the recording system's page 3897 * size — it affects mmap buffer alignment, sample addresses, 3898 * and data_page_size/code_page_size interpretation. Without 3899 * it we assume 4K (the smallest Linux page size) as a safe 3900 * minimum alignment for comp_mmap_len validation. 3901 * 3902 * No upper-bound cap: perf_session__process_compressed_event() 3903 * checks decomp_len + sizeof(struct decomp) against SIZE_MAX 3904 * before allocating, which handles 32-bit safety. 3905 */ 3906 if (env->comp_mmap_len < 4096 || env->comp_mmap_len % 4096) { 3907 pr_err("Invalid HEADER_COMPRESSED: comp_mmap_len (%u) must be a 4K-aligned value >= 4096\n", 3908 env->comp_mmap_len); 3909 return -1; 3910 } 3911 3912 return 0; 3913 } 3914 3915 static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps, 3916 char ***caps, unsigned int *max_branches, 3917 unsigned int *br_cntr_nr, 3918 unsigned int *br_cntr_width) 3919 { 3920 char *name, *value, *ptr; 3921 u32 nr_pmu_caps, i; 3922 3923 *nr_caps = 0; 3924 *caps = NULL; 3925 3926 if (do_read_u32(ff, &nr_pmu_caps)) 3927 return -1; 3928 3929 if (!nr_pmu_caps) 3930 return 0; 3931 3932 if (nr_pmu_caps > MAX_PMU_CAPS) { 3933 pr_err("Invalid pmu caps: nr_pmu_caps (%u) > %u\n", 3934 nr_pmu_caps, MAX_PMU_CAPS); 3935 return -1; 3936 } 3937 3938 *caps = calloc(nr_pmu_caps, sizeof(char *)); 3939 if (!*caps) 3940 return -1; 3941 3942 for (i = 0; i < nr_pmu_caps; i++) { 3943 name = do_read_string(ff); 3944 if (!name) 3945 goto error; 3946 3947 value = do_read_string(ff); 3948 if (!value) 3949 goto free_name; 3950 3951 if (asprintf(&ptr, "%s=%s", name, value) < 0) 3952 goto free_value; 3953 3954 (*caps)[i] = ptr; 3955 3956 if (!strcmp(name, "branches")) 3957 *max_branches = atoi(value); 3958 3959 if (!strcmp(name, "branch_counter_nr")) 3960 *br_cntr_nr = atoi(value); 3961 3962 if (!strcmp(name, "branch_counter_width")) 3963 *br_cntr_width = atoi(value); 3964 3965 free(value); 3966 free(name); 3967 } 3968 *nr_caps = nr_pmu_caps; 3969 return 0; 3970 3971 free_value: 3972 free(value); 3973 free_name: 3974 free(name); 3975 error: 3976 for (; i > 0; i--) 3977 free((*caps)[i - 1]); 3978 free(*caps); 3979 *caps = NULL; 3980 *nr_caps = 0; 3981 return -1; 3982 } 3983 3984 static int process_cpu_pmu_caps(struct feat_fd *ff, 3985 void *data __maybe_unused) 3986 { 3987 struct perf_env *env = &ff->ph->env; 3988 int ret = __process_pmu_caps(ff, &env->nr_cpu_pmu_caps, 3989 &env->cpu_pmu_caps, 3990 &env->max_branches, 3991 &env->br_cntr_nr, 3992 &env->br_cntr_width); 3993 3994 if (!ret && !env->cpu_pmu_caps) 3995 pr_debug("cpu pmu capabilities not available\n"); 3996 return ret; 3997 } 3998 3999 static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused) 4000 { 4001 struct perf_env *env = &ff->ph->env; 4002 struct pmu_caps *pmu_caps; 4003 u32 nr_pmu, i; 4004 int ret; 4005 int j; 4006 4007 if (do_read_u32(ff, &nr_pmu)) 4008 return -1; 4009 4010 if (!nr_pmu) { 4011 pr_debug("pmu capabilities not available\n"); 4012 return 0; 4013 } 4014 4015 if (nr_pmu > MAX_PMU_MAPPINGS) { 4016 pr_err("Invalid HEADER_PMU_CAPS: nr_pmu (%u) > %u\n", 4017 nr_pmu, MAX_PMU_MAPPINGS); 4018 return -1; 4019 } 4020 4021 if (ff->size < sizeof(u32) + nr_pmu * sizeof(u32)) { 4022 pr_err("Invalid HEADER_PMU_CAPS: section too small (%zu) for %u PMUs\n", 4023 ff->size, nr_pmu); 4024 return -1; 4025 } 4026 4027 pmu_caps = calloc(nr_pmu, sizeof(*pmu_caps)); 4028 if (!pmu_caps) 4029 return -ENOMEM; 4030 4031 for (i = 0; i < nr_pmu; i++) { 4032 ret = __process_pmu_caps(ff, &pmu_caps[i].nr_caps, 4033 &pmu_caps[i].caps, 4034 &pmu_caps[i].max_branches, 4035 &pmu_caps[i].br_cntr_nr, 4036 &pmu_caps[i].br_cntr_width); 4037 if (ret) 4038 goto err; 4039 4040 pmu_caps[i].pmu_name = do_read_string(ff); 4041 if (!pmu_caps[i].pmu_name) { 4042 ret = -1; 4043 goto err; 4044 } 4045 if (!pmu_caps[i].nr_caps) { 4046 pr_debug("%s pmu capabilities not available\n", 4047 pmu_caps[i].pmu_name); 4048 } 4049 } 4050 4051 env->nr_pmus_with_caps = nr_pmu; 4052 env->pmu_caps = pmu_caps; 4053 return 0; 4054 4055 err: 4056 for (i = 0; i < nr_pmu; i++) { 4057 for (j = 0; j < pmu_caps[i].nr_caps; j++) 4058 free(pmu_caps[i].caps[j]); 4059 free(pmu_caps[i].caps); 4060 free(pmu_caps[i].pmu_name); 4061 } 4062 4063 free(pmu_caps); 4064 return ret; 4065 } 4066 4067 static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused) 4068 { 4069 u32 schedstat_version, max_sched_domains, cpu, domain, nr_domains; 4070 struct perf_env *env = &ff->ph->env; 4071 char *dname, *cpumask, *cpulist; 4072 struct cpu_domain_map **cd_map; 4073 struct domain_info *d_info; 4074 u32 nra, nr, i, j; 4075 int ret; 4076 4077 nra = env->nr_cpus_avail; 4078 nr = env->nr_cpus_online; 4079 4080 if (nra == 0 || nr == 0) { 4081 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: missing HEADER_NRCPUS\n"); 4082 return -1; 4083 } 4084 4085 if (ff->size < 2 * sizeof(u32) + nr * 2 * sizeof(u32)) { 4086 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: section too small (%zu) for %u CPUs\n", 4087 (size_t)ff->size, nr); 4088 return -1; 4089 } 4090 4091 cd_map = calloc(nra, sizeof(*cd_map)); 4092 if (!cd_map) 4093 return -1; 4094 4095 env->cpu_domain = cd_map; 4096 4097 ret = do_read_u32(ff, &schedstat_version); 4098 if (ret) 4099 return ret; 4100 4101 env->schedstat_version = schedstat_version; 4102 4103 ret = do_read_u32(ff, &max_sched_domains); 4104 if (ret) 4105 return ret; 4106 4107 /* 4108 * Sanity check: real systems have at most ~10 sched domain levels 4109 * (SMT, CLS, MC, PKG + NUMA hops). Reject obviously bogus values 4110 * from malformed perf.data files before they cause excessive 4111 * allocation in the per-CPU loop. 4112 */ 4113 if (max_sched_domains > MAX_SCHED_DOMAINS) { 4114 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: max_sched_domains %u > %u\n", 4115 max_sched_domains, MAX_SCHED_DOMAINS); 4116 return -1; 4117 } 4118 4119 env->max_sched_domains = max_sched_domains; 4120 4121 for (i = 0; i < nr; i++) { 4122 if (do_read_u32(ff, &cpu)) 4123 return -1; 4124 4125 if (cpu >= nra) { 4126 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: cpu %d >= nr_cpus_avail (%d)\n", cpu, nra); 4127 return -1; 4128 } 4129 4130 if (cd_map[cpu]) { 4131 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: duplicate cpu %u\n", cpu); 4132 return -1; 4133 } 4134 4135 cd_map[cpu] = zalloc(sizeof(*cd_map[cpu])); 4136 if (!cd_map[cpu]) 4137 return -1; 4138 4139 cd_map[cpu]->cpu = cpu; 4140 4141 if (do_read_u32(ff, &nr_domains)) 4142 return -1; 4143 4144 if (nr_domains > max_sched_domains) { 4145 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: nr_domains %u > max_sched_domains (%u)\n", 4146 nr_domains, max_sched_domains); 4147 return -1; 4148 } 4149 4150 cd_map[cpu]->nr_domains = nr_domains; 4151 4152 cd_map[cpu]->domains = calloc(max_sched_domains, sizeof(*d_info)); 4153 if (!cd_map[cpu]->domains) 4154 return -1; 4155 4156 for (j = 0; j < nr_domains; j++) { 4157 if (do_read_u32(ff, &domain)) 4158 return -1; 4159 4160 if (domain >= max_sched_domains) { 4161 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: domain %d >= max_sched_domains (%d)\n", 4162 domain, max_sched_domains); 4163 return -1; 4164 } 4165 4166 d_info = zalloc(sizeof(*d_info)); 4167 if (!d_info) 4168 return -1; 4169 4170 if (cd_map[cpu]->domains[domain]) { 4171 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: duplicate domain %u for cpu %u\n", 4172 domain, cpu); 4173 free(d_info); 4174 return -1; 4175 } 4176 4177 cd_map[cpu]->domains[domain] = d_info; 4178 d_info->domain = domain; 4179 4180 if (schedstat_version >= 17) { 4181 dname = do_read_string(ff); 4182 if (!dname) 4183 return -1; 4184 4185 d_info->dname = dname; 4186 } 4187 4188 cpumask = do_read_string(ff); 4189 if (!cpumask) 4190 return -1; 4191 4192 d_info->cpumask = cpumask; 4193 4194 cpulist = do_read_string(ff); 4195 if (!cpulist) 4196 return -1; 4197 4198 d_info->cpulist = cpulist; 4199 } 4200 } 4201 4202 return ret; 4203 } 4204 4205 #define FEAT_OPR(n, func, __full_only) \ 4206 [HEADER_##n] = { \ 4207 .name = __stringify(n), \ 4208 .write = write_##func, \ 4209 .print = print_##func, \ 4210 .full_only = __full_only, \ 4211 .process = process_##func, \ 4212 .synthesize = true \ 4213 } 4214 4215 #define FEAT_OPN(n, func, __full_only) \ 4216 [HEADER_##n] = { \ 4217 .name = __stringify(n), \ 4218 .write = write_##func, \ 4219 .print = print_##func, \ 4220 .full_only = __full_only, \ 4221 .process = process_##func \ 4222 } 4223 4224 /* feature_ops not implemented: */ 4225 #define print_tracing_data NULL 4226 #define print_build_id NULL 4227 4228 #define process_branch_stack NULL 4229 #define process_stat NULL 4230 4231 // Only used in util/synthetic-events.c 4232 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE]; 4233 4234 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = { 4235 FEAT_OPN(TRACING_DATA, tracing_data, false), 4236 FEAT_OPN(BUILD_ID, build_id, false), 4237 FEAT_OPR(HOSTNAME, hostname, false), 4238 FEAT_OPR(OSRELEASE, osrelease, false), 4239 FEAT_OPR(VERSION, version, false), 4240 FEAT_OPR(ARCH, arch, false), 4241 FEAT_OPR(NRCPUS, nrcpus, false), 4242 FEAT_OPR(CPUDESC, cpudesc, false), 4243 FEAT_OPR(CPUID, cpuid, false), 4244 FEAT_OPR(TOTAL_MEM, total_mem, false), 4245 FEAT_OPR(EVENT_DESC, event_desc, false), 4246 FEAT_OPR(CMDLINE, cmdline, false), 4247 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true), 4248 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true), 4249 FEAT_OPN(BRANCH_STACK, branch_stack, false), 4250 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false), 4251 FEAT_OPR(GROUP_DESC, group_desc, false), 4252 FEAT_OPN(AUXTRACE, auxtrace, false), 4253 FEAT_OPN(STAT, stat, false), 4254 FEAT_OPN(CACHE, cache, true), 4255 FEAT_OPR(SAMPLE_TIME, sample_time, false), 4256 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true), 4257 FEAT_OPR(CLOCKID, clockid, false), 4258 FEAT_OPN(DIR_FORMAT, dir_format, false), 4259 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false), 4260 FEAT_OPR(BPF_BTF, bpf_btf, false), 4261 FEAT_OPR(COMPRESSED, compressed, false), 4262 FEAT_OPR(CPU_PMU_CAPS, cpu_pmu_caps, false), 4263 FEAT_OPR(CLOCK_DATA, clock_data, false), 4264 FEAT_OPN(HYBRID_TOPOLOGY, hybrid_topology, true), 4265 FEAT_OPR(PMU_CAPS, pmu_caps, false), 4266 FEAT_OPR(CPU_DOMAIN_INFO, cpu_domain_info, true), 4267 FEAT_OPR(E_MACHINE, e_machine, false), 4268 FEAT_OPR(CLN_SIZE, cln_size, false), 4269 }; 4270 4271 struct header_print_data { 4272 FILE *fp; 4273 bool full; /* extended list of headers */ 4274 }; 4275 4276 const char *header_feat__name(unsigned int id) 4277 { 4278 if (id < HEADER_LAST_FEATURE) 4279 return feat_ops[id].name ?: "INVALID"; 4280 return "INVALID"; 4281 } 4282 4283 static int perf_file_section__fprintf_info(struct perf_file_section *section, 4284 struct perf_header *ph, 4285 int feat, int fd, void *data) 4286 { 4287 struct header_print_data *hd = data; 4288 struct feat_fd ff; 4289 4290 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 4291 pr_debug("Failed to lseek to %" PRIu64 " offset for feature %s (%d), continuing...\n", 4292 section->offset, header_feat__name(feat), feat); 4293 return 0; 4294 } 4295 if (feat >= ph->last_feat) { 4296 pr_warning("unknown feature %d\n", feat); 4297 return 0; 4298 } 4299 if (!feat_ops[feat].print) 4300 return 0; 4301 4302 ff = (struct feat_fd) { 4303 .fd = fd, 4304 .ph = ph, 4305 .size = section->size, 4306 }; 4307 4308 if (!feat_ops[feat].full_only || hd->full) 4309 feat_ops[feat].print(&ff, hd->fp); 4310 else 4311 fprintf(hd->fp, "# %s info available, use -I to display\n", 4312 feat_ops[feat].name); 4313 4314 return 0; 4315 } 4316 4317 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) 4318 { 4319 struct header_print_data hd; 4320 struct perf_header *header = &session->header; 4321 int fd = perf_data__fd(session->data); 4322 struct stat st; 4323 time_t stctime; 4324 int ret, bit; 4325 4326 hd.fp = fp; 4327 hd.full = full; 4328 4329 ret = fstat(fd, &st); 4330 if (ret == -1) 4331 return -1; 4332 4333 stctime = st.st_mtime; 4334 fprintf(fp, "# captured on : %s", ctime(&stctime)); 4335 4336 fprintf(fp, "# header version : %u\n", header->version); 4337 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset); 4338 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size); 4339 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset); 4340 4341 perf_header__process_sections(header, fd, &hd, 4342 perf_file_section__fprintf_info); 4343 4344 if (session->data->is_pipe) 4345 return 0; 4346 4347 fprintf(fp, "# missing features: "); 4348 for_each_clear_bit(bit, header->adds_features, header->last_feat) { 4349 if (bit) 4350 fprintf(fp, "%s ", feat_ops[bit].name); 4351 } 4352 4353 fprintf(fp, "\n"); 4354 return 0; 4355 } 4356 4357 struct header_fw { 4358 struct feat_writer fw; 4359 struct feat_fd *ff; 4360 }; 4361 4362 static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz) 4363 { 4364 struct header_fw *h = container_of(fw, struct header_fw, fw); 4365 4366 return do_write(h->ff, buf, sz); 4367 } 4368 4369 static int do_write_feat(struct feat_fd *ff, int type, 4370 struct perf_file_section **p, 4371 struct evlist *evlist, 4372 struct feat_copier *fc) 4373 { 4374 int err; 4375 int ret = 0; 4376 4377 if (perf_header__has_feat(ff->ph, type)) { 4378 if (!feat_ops[type].write) 4379 return -1; 4380 4381 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 4382 return -1; 4383 4384 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR); 4385 4386 /* 4387 * Hook to let perf inject copy features sections from the input 4388 * file. 4389 */ 4390 if (fc && fc->copy) { 4391 struct header_fw h = { 4392 .fw.write = feat_writer_cb, 4393 .ff = ff, 4394 }; 4395 4396 /* ->copy() returns 0 if the feature was not copied */ 4397 err = fc->copy(fc, type, &h.fw); 4398 } else { 4399 err = 0; 4400 } 4401 if (!err) 4402 err = feat_ops[type].write(ff, evlist); 4403 if (err < 0) { 4404 pr_debug("failed to write feature %s\n", feat_ops[type].name); 4405 4406 /* undo anything written */ 4407 lseek(ff->fd, (*p)->offset, SEEK_SET); 4408 4409 return -1; 4410 } 4411 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset; 4412 (*p)++; 4413 } 4414 return ret; 4415 } 4416 4417 static int perf_header__adds_write(struct perf_header *header, 4418 struct evlist *evlist, int fd, 4419 struct feat_copier *fc) 4420 { 4421 int nr_sections; 4422 struct feat_fd ff = { 4423 .fd = fd, 4424 .ph = header, 4425 }; 4426 struct perf_file_section *feat_sec, *p; 4427 int sec_size; 4428 u64 sec_start; 4429 int feat; 4430 int err; 4431 4432 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 4433 if (!nr_sections) 4434 return 0; 4435 4436 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec)); 4437 if (feat_sec == NULL) 4438 return -ENOMEM; 4439 4440 sec_size = sizeof(*feat_sec) * nr_sections; 4441 4442 sec_start = header->feat_offset; 4443 lseek(fd, sec_start + sec_size, SEEK_SET); 4444 4445 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 4446 if (do_write_feat(&ff, feat, &p, evlist, fc)) 4447 perf_header__clear_feat(header, feat); 4448 } 4449 4450 lseek(fd, sec_start, SEEK_SET); 4451 /* 4452 * may write more than needed due to dropped feature, but 4453 * this is okay, reader will skip the missing entries 4454 */ 4455 err = do_write(&ff, feat_sec, sec_size); 4456 if (err < 0) 4457 pr_debug("failed to write feature section\n"); 4458 free(ff.buf); /* TODO: added to silence clang-tidy. */ 4459 free(feat_sec); 4460 return err; 4461 } 4462 4463 int perf_header__write_pipe(int fd) 4464 { 4465 struct perf_pipe_file_header f_header; 4466 struct feat_fd ff = { 4467 .fd = fd, 4468 }; 4469 int err; 4470 4471 f_header = (struct perf_pipe_file_header){ 4472 .magic = PERF_MAGIC, 4473 .size = sizeof(f_header), 4474 }; 4475 4476 err = do_write(&ff, &f_header, sizeof(f_header)); 4477 if (err < 0) { 4478 pr_debug("failed to write perf pipe header\n"); 4479 return err; 4480 } 4481 free(ff.buf); 4482 return 0; 4483 } 4484 4485 static int perf_session__do_write_header(struct perf_session *session, 4486 struct evlist *evlist, 4487 int fd, bool at_exit, 4488 struct feat_copier *fc, 4489 bool write_attrs_after_data) 4490 { 4491 struct perf_file_header f_header; 4492 struct perf_header *header = &session->header; 4493 struct evsel *evsel; 4494 struct feat_fd ff = { 4495 .ph = header, 4496 .fd = fd, 4497 }; 4498 u64 attr_offset = sizeof(f_header), attr_size = 0; 4499 int err; 4500 4501 if (write_attrs_after_data && at_exit) { 4502 /* 4503 * Write features at the end of the file first so that 4504 * attributes may come after them. 4505 */ 4506 if (!header->data_offset && header->data_size) { 4507 pr_err("File contains data but offset unknown\n"); 4508 err = -1; 4509 goto err_out; 4510 } 4511 header->feat_offset = header->data_offset + header->data_size; 4512 err = perf_header__adds_write(header, evlist, fd, fc); 4513 if (err < 0) 4514 goto err_out; 4515 attr_offset = lseek(fd, 0, SEEK_CUR); 4516 } else { 4517 lseek(fd, attr_offset, SEEK_SET); 4518 } 4519 4520 evlist__for_each_entry(session->evlist, evsel) { 4521 evsel->id_offset = attr_offset; 4522 /* Avoid writing at the end of the file until the session is exiting. */ 4523 if (!write_attrs_after_data || at_exit) { 4524 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64)); 4525 if (err < 0) { 4526 pr_debug("failed to write perf header\n"); 4527 goto err_out; 4528 } 4529 } 4530 attr_offset += evsel->core.ids * sizeof(u64); 4531 } 4532 4533 evlist__for_each_entry(evlist, evsel) { 4534 if (evsel->core.attr.size < sizeof(evsel->core.attr)) { 4535 /* 4536 * We are likely in "perf inject" and have read 4537 * from an older file. Update attr size so that 4538 * reader gets the right offset to the ids. 4539 */ 4540 evsel->core.attr.size = sizeof(evsel->core.attr); 4541 } 4542 /* Avoid writing at the end of the file until the session is exiting. */ 4543 if (!write_attrs_after_data || at_exit) { 4544 struct perf_file_attr f_attr = { 4545 .attr = evsel->core.attr, 4546 .ids = { 4547 .offset = evsel->id_offset, 4548 .size = evsel->core.ids * sizeof(u64), 4549 } 4550 }; 4551 err = do_write(&ff, &f_attr, sizeof(f_attr)); 4552 if (err < 0) { 4553 pr_debug("failed to write perf header attribute\n"); 4554 goto err_out; 4555 } 4556 } 4557 attr_size += sizeof(struct perf_file_attr); 4558 } 4559 4560 if (!header->data_offset) { 4561 if (write_attrs_after_data) 4562 header->data_offset = sizeof(f_header); 4563 else 4564 header->data_offset = attr_offset + attr_size; 4565 } 4566 header->feat_offset = header->data_offset + header->data_size; 4567 4568 if (!write_attrs_after_data && at_exit) { 4569 /* Write features now feat_offset is known. */ 4570 err = perf_header__adds_write(header, evlist, fd, fc); 4571 if (err < 0) 4572 goto err_out; 4573 } 4574 4575 f_header = (struct perf_file_header){ 4576 .magic = PERF_MAGIC, 4577 .size = sizeof(f_header), 4578 .attr_size = sizeof(struct perf_file_attr), 4579 .attrs = { 4580 .offset = attr_offset, 4581 .size = attr_size, 4582 }, 4583 .data = { 4584 .offset = header->data_offset, 4585 .size = header->data_size, 4586 }, 4587 /* event_types is ignored, store zeros */ 4588 }; 4589 4590 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); 4591 4592 lseek(fd, 0, SEEK_SET); 4593 err = do_write(&ff, &f_header, sizeof(f_header)); 4594 if (err < 0) { 4595 pr_debug("failed to write perf header\n"); 4596 goto err_out; 4597 } else { 4598 lseek(fd, 0, SEEK_END); 4599 err = 0; 4600 } 4601 err_out: 4602 free(ff.buf); 4603 return err; 4604 } 4605 4606 int perf_session__write_header(struct perf_session *session, 4607 struct evlist *evlist, 4608 int fd, bool at_exit) 4609 { 4610 return perf_session__do_write_header(session, evlist, fd, at_exit, /*fc=*/NULL, 4611 /*write_attrs_after_data=*/false); 4612 } 4613 4614 size_t perf_session__data_offset(const struct evlist *evlist) 4615 { 4616 struct evsel *evsel; 4617 size_t data_offset; 4618 4619 data_offset = sizeof(struct perf_file_header); 4620 evlist__for_each_entry(evlist, evsel) { 4621 data_offset += evsel->core.ids * sizeof(u64); 4622 } 4623 data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr); 4624 4625 return data_offset; 4626 } 4627 4628 int perf_session__inject_header(struct perf_session *session, 4629 struct evlist *evlist, 4630 int fd, 4631 struct feat_copier *fc, 4632 bool write_attrs_after_data) 4633 { 4634 return perf_session__do_write_header(session, evlist, fd, true, fc, 4635 write_attrs_after_data); 4636 } 4637 4638 static int perf_header__getbuffer64(struct perf_header *header, 4639 int fd, void *buf, size_t size) 4640 { 4641 ssize_t n = readn(fd, buf, size); 4642 4643 if (n <= 0) { 4644 if (n == 0) 4645 errno = EIO; 4646 return -1; 4647 } 4648 4649 if (header->needs_swap) 4650 mem_bswap_64(buf, size); 4651 4652 return 0; 4653 } 4654 4655 int perf_header__process_sections(struct perf_header *header, int fd, 4656 void *data, 4657 int (*process)(struct perf_file_section *section, 4658 struct perf_header *ph, 4659 int feat, int fd, void *data)) 4660 { 4661 struct perf_file_section *feat_sec, *sec; 4662 int nr_sections; 4663 int sec_size; 4664 int feat; 4665 int err; 4666 struct stat st; 4667 4668 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 4669 if (!nr_sections) 4670 return 0; 4671 4672 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec)); 4673 if (!feat_sec) 4674 return -1; 4675 4676 sec_size = sizeof(*feat_sec) * nr_sections; 4677 4678 lseek(fd, header->feat_offset, SEEK_SET); 4679 4680 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); 4681 if (err < 0) 4682 goto out_free; 4683 4684 if (fstat(fd, &st) < 0) { 4685 pr_err("Failed to stat the perf data file\n"); 4686 err = -1; 4687 goto out_free; 4688 } 4689 4690 for_each_set_bit(feat, header->adds_features, header->last_feat) { 4691 /* 4692 * FIXME: block devices have st_size == 0, so we skip 4693 * bounds checking entirely. Historically perf never 4694 * prevented using a block device as input, but it 4695 * probably should — there's no valid use case for it 4696 * and it bypasses all file-size validation. 4697 */ 4698 if (S_ISREG(st.st_mode) && 4699 (sec->offset > (u64)st.st_size || 4700 sec->size > (u64)st.st_size - sec->offset)) { 4701 pr_err("Feature %s (%d) section extends past EOF (offset=%" PRIu64 ", size=%" PRIu64 ", file=%" PRIu64 ")\n", 4702 header_feat__name(feat), feat, 4703 sec->offset, sec->size, (u64)st.st_size); 4704 err = -1; 4705 goto out_free; 4706 } 4707 err = process(sec++, header, feat, fd, data); 4708 if (err < 0) 4709 goto out_free; 4710 } 4711 err = 0; 4712 out_free: 4713 free(feat_sec); 4714 return err; 4715 } 4716 4717 static const int attr_file_abi_sizes[] = { 4718 [0] = PERF_ATTR_SIZE_VER0, 4719 [1] = PERF_ATTR_SIZE_VER1, 4720 [2] = PERF_ATTR_SIZE_VER2, 4721 [3] = PERF_ATTR_SIZE_VER3, 4722 [4] = PERF_ATTR_SIZE_VER4, 4723 0, 4724 }; 4725 4726 /* 4727 * In the legacy file format, the magic number is not used to encode endianness. 4728 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based 4729 * on ABI revisions, we need to try all combinations for all endianness to 4730 * detect the endianness. 4731 */ 4732 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) 4733 { 4734 uint64_t ref_size, attr_size; 4735 int i; 4736 4737 for (i = 0 ; attr_file_abi_sizes[i]; i++) { 4738 ref_size = attr_file_abi_sizes[i] 4739 + sizeof(struct perf_file_section); 4740 if (hdr_sz != ref_size) { 4741 attr_size = bswap_64(hdr_sz); 4742 if (attr_size != ref_size) 4743 continue; 4744 4745 ph->needs_swap = true; 4746 } 4747 pr_debug("ABI%d perf.data file detected, need_swap=%d\n", 4748 i, 4749 ph->needs_swap); 4750 return 0; 4751 } 4752 /* could not determine endianness */ 4753 return -1; 4754 } 4755 4756 #define PERF_PIPE_HDR_VER0 16 4757 4758 static const size_t attr_pipe_abi_sizes[] = { 4759 [0] = PERF_PIPE_HDR_VER0, 4760 0, 4761 }; 4762 4763 /* 4764 * In the legacy pipe format, there is an implicit assumption that endianness 4765 * between host recording the samples, and host parsing the samples is the 4766 * same. This is not always the case given that the pipe output may always be 4767 * redirected into a file and analyzed on a different machine with possibly a 4768 * different endianness and perf_event ABI revisions in the perf tool itself. 4769 */ 4770 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) 4771 { 4772 u64 attr_size; 4773 int i; 4774 4775 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { 4776 if (hdr_sz != attr_pipe_abi_sizes[i]) { 4777 attr_size = bswap_64(hdr_sz); 4778 if (attr_size != hdr_sz) 4779 continue; 4780 4781 ph->needs_swap = true; 4782 } 4783 pr_debug("Pipe ABI%d perf.data file detected\n", i); 4784 return 0; 4785 } 4786 return -1; 4787 } 4788 4789 bool is_perf_magic(u64 magic) 4790 { 4791 if (!memcmp(&magic, __perf_magic1, sizeof(magic)) 4792 || magic == __perf_magic2 4793 || magic == __perf_magic2_sw) 4794 return true; 4795 4796 return false; 4797 } 4798 4799 static int check_magic_endian(u64 magic, uint64_t hdr_sz, 4800 bool is_pipe, struct perf_header *ph) 4801 { 4802 int ret; 4803 4804 /* check for legacy format */ 4805 ret = memcmp(&magic, __perf_magic1, sizeof(magic)); 4806 if (ret == 0) { 4807 ph->version = PERF_HEADER_VERSION_1; 4808 pr_debug("legacy perf.data format\n"); 4809 if (is_pipe) 4810 return try_all_pipe_abis(hdr_sz, ph); 4811 4812 return try_all_file_abis(hdr_sz, ph); 4813 } 4814 /* 4815 * the new magic number serves two purposes: 4816 * - unique number to identify actual perf.data files 4817 * - encode endianness of file 4818 */ 4819 ph->version = PERF_HEADER_VERSION_2; 4820 4821 /* check magic number with one endianness */ 4822 if (magic == __perf_magic2) 4823 return 0; 4824 4825 /* check magic number with opposite endianness */ 4826 if (magic != __perf_magic2_sw) 4827 return -1; 4828 4829 ph->needs_swap = true; 4830 4831 return 0; 4832 } 4833 4834 int perf_file_header__read(struct perf_file_header *header, 4835 struct perf_header *ph, int fd) 4836 { 4837 ssize_t ret; 4838 4839 lseek(fd, 0, SEEK_SET); 4840 4841 ret = readn(fd, header, sizeof(*header)); 4842 if (ret <= 0) 4843 return -1; 4844 4845 if (check_magic_endian(header->magic, 4846 header->attr_size, false, ph) < 0) { 4847 pr_debug("magic/endian check failed\n"); 4848 return -1; 4849 } 4850 4851 if (ph->needs_swap) { 4852 mem_bswap_64(header, offsetof(struct perf_file_header, 4853 adds_features)); 4854 } 4855 4856 if (header->size > header->attrs.offset) { 4857 pr_err("Perf file header corrupt: header overlaps attrs\n"); 4858 return -1; 4859 } 4860 4861 if (header->size > header->data.offset) { 4862 pr_err("Perf file header corrupt: header overlaps data\n"); 4863 return -1; 4864 } 4865 4866 if ((header->attrs.offset <= header->data.offset && 4867 header->attrs.offset + header->attrs.size > header->data.offset) || 4868 (header->attrs.offset > header->data.offset && 4869 header->data.offset + header->data.size > header->attrs.offset)) { 4870 pr_err("Perf file header corrupt: Attributes and data overlap\n"); 4871 return -1; 4872 } 4873 4874 if (header->size != sizeof(*header)) { 4875 /* Support the previous format */ 4876 if (header->size == offsetof(typeof(*header), adds_features)) 4877 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 4878 else 4879 return -1; 4880 } else if (ph->needs_swap) { 4881 /* 4882 * feature bitmap is declared as an array of unsigned longs -- 4883 * not good since its size can differ between the host that 4884 * generated the data file and the host analyzing the file. 4885 * 4886 * We need to handle endianness, but we don't know the size of 4887 * the unsigned long where the file was generated. Take a best 4888 * guess at determining it: try 64-bit swap first (ie., file 4889 * created on a 64-bit host), and check if the hostname feature 4890 * bit is set (this feature bit is forced on as of fbe96f2). 4891 * If the bit is not, undo the 64-bit swap and try a 32-bit 4892 * swap. If the hostname bit is still not set (e.g., older data 4893 * file), punt and fallback to the original behavior -- 4894 * clearing all feature bits and setting buildid. 4895 */ 4896 mem_bswap_64(&header->adds_features, 4897 BITS_TO_U64(HEADER_FEAT_BITS)); 4898 4899 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 4900 /* unswap as u64 */ 4901 mem_bswap_64(&header->adds_features, 4902 BITS_TO_U64(HEADER_FEAT_BITS)); 4903 4904 /* unswap as u32 */ 4905 mem_bswap_32(&header->adds_features, 4906 BITS_TO_U32(HEADER_FEAT_BITS)); 4907 } 4908 4909 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 4910 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 4911 __set_bit(HEADER_BUILD_ID, header->adds_features); 4912 } 4913 } 4914 4915 memcpy(&ph->adds_features, &header->adds_features, 4916 sizeof(ph->adds_features)); 4917 4918 ph->data_offset = header->data.offset; 4919 ph->data_size = header->data.size; 4920 ph->feat_offset = header->data.offset + header->data.size; 4921 ph->last_feat = HEADER_LAST_FEATURE; 4922 return 0; 4923 } 4924 4925 static int perf_file_section__process(struct perf_file_section *section, 4926 struct perf_header *ph, 4927 int feat, int fd, void *data) 4928 { 4929 struct feat_fd fdd = { 4930 .fd = fd, 4931 .ph = ph, 4932 .size = section->size, 4933 .offset = 0, 4934 }; 4935 4936 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 4937 pr_debug("Failed to lseek to %" PRIu64 " offset for feature %s (%d), continuing...\n", 4938 section->offset, header_feat__name(feat), feat); 4939 return 0; 4940 } 4941 4942 if (feat >= HEADER_LAST_FEATURE) { 4943 pr_debug("unknown feature %d, continuing...\n", feat); 4944 return 0; 4945 } 4946 4947 if (!feat_ops[feat].process) 4948 return 0; 4949 4950 return feat_ops[feat].process(&fdd, data); 4951 } 4952 4953 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, 4954 struct perf_header *ph, 4955 struct perf_data *data) 4956 { 4957 ssize_t ret; 4958 4959 ret = perf_data__read(data, header, sizeof(*header)); 4960 if (ret <= 0) 4961 return -1; 4962 4963 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { 4964 pr_debug("endian/magic failed\n"); 4965 return -1; 4966 } 4967 4968 if (ph->needs_swap) 4969 header->size = bswap_64(header->size); 4970 4971 /* The last feature is written out as a 0 sized event and will update this value. */ 4972 ph->last_feat = 0; 4973 return 0; 4974 } 4975 4976 static int perf_header__read_pipe(struct perf_session *session) 4977 { 4978 struct perf_header *header = &session->header; 4979 struct perf_pipe_file_header f_header; 4980 4981 if (perf_file_header__read_pipe(&f_header, header, session->data) < 0) { 4982 pr_debug("incompatible file format\n"); 4983 return -EINVAL; 4984 } 4985 4986 return f_header.size == sizeof(f_header) ? 0 : -1; 4987 } 4988 4989 static int read_attr(int fd, struct perf_header *ph, 4990 struct perf_file_attr *f_attr) 4991 { 4992 struct perf_event_attr *attr = &f_attr->attr; 4993 size_t sz, left; 4994 size_t our_sz = sizeof(f_attr->attr); 4995 ssize_t ret; 4996 4997 memset(f_attr, 0, sizeof(*f_attr)); 4998 4999 /* read minimal guaranteed structure */ 5000 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); 5001 if (ret <= 0) { 5002 pr_debug("cannot read %d bytes of header attr\n", 5003 PERF_ATTR_SIZE_VER0); 5004 if (ret == 0) 5005 errno = EIO; 5006 return -1; 5007 } 5008 5009 /* on file perf_event_attr size */ 5010 sz = attr->size; 5011 5012 if (ph->needs_swap) 5013 sz = bswap_32(sz); 5014 5015 if (sz == 0) { 5016 /* assume ABI0 */ 5017 sz = PERF_ATTR_SIZE_VER0; 5018 } else if (sz < PERF_ATTR_SIZE_VER0) { 5019 pr_debug("bad attr size %zu, expected at least %d\n", 5020 sz, PERF_ATTR_SIZE_VER0); 5021 errno = EINVAL; 5022 return -1; 5023 } else if (sz > our_sz) { 5024 pr_debug("file uses a more recent and unsupported ABI" 5025 " (%zu bytes extra)\n", sz - our_sz); 5026 errno = EINVAL; 5027 return -1; 5028 } 5029 /* what we have not yet read and that we know about */ 5030 left = sz - PERF_ATTR_SIZE_VER0; 5031 if (left) { 5032 void *ptr = attr; 5033 ptr += PERF_ATTR_SIZE_VER0; 5034 5035 ret = readn(fd, ptr, left); 5036 if (ret <= 0) { 5037 if (ret == 0) 5038 errno = EIO; 5039 return -1; 5040 } 5041 } 5042 /* read perf_file_section, ids are read in caller */ 5043 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); 5044 if (ret <= 0) { 5045 if (ret == 0) 5046 errno = EIO; 5047 return -1; 5048 } 5049 5050 return 0; 5051 } 5052 5053 #ifdef HAVE_LIBTRACEEVENT 5054 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent) 5055 { 5056 struct tep_event *event; 5057 char bf[128]; 5058 5059 /* already prepared */ 5060 if (evsel->tp_format) 5061 return 0; 5062 5063 if (pevent == NULL) { 5064 pr_debug("broken or missing trace data\n"); 5065 return -1; 5066 } 5067 5068 event = tep_find_event(pevent, evsel->core.attr.config); 5069 if (event == NULL) { 5070 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config); 5071 return -1; 5072 } 5073 5074 if (!evsel->name) { 5075 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); 5076 evsel->name = strdup(bf); 5077 if (evsel->name == NULL) 5078 return -1; 5079 } 5080 5081 evsel->tp_format = event; 5082 return 0; 5083 } 5084 5085 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent) 5086 { 5087 struct evsel *pos; 5088 5089 evlist__for_each_entry(evlist, pos) { 5090 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT && 5091 evsel__prepare_tracepoint_event(pos, pevent)) 5092 return -1; 5093 } 5094 5095 return 0; 5096 } 5097 #endif 5098 5099 int perf_session__read_header(struct perf_session *session) 5100 { 5101 struct perf_data *data = session->data; 5102 struct perf_header *header = &session->header; 5103 struct perf_file_header f_header; 5104 struct perf_file_attr f_attr; 5105 u64 f_id; 5106 struct stat input_stat; 5107 int nr_attrs, nr_ids, i, j, err = -ENOMEM; 5108 int fd = perf_data__fd(data); 5109 5110 session->evlist = evlist__new(); 5111 if (session->evlist == NULL) 5112 return -ENOMEM; 5113 5114 session->evlist->session = session; 5115 session->machines.host.env = &header->env; 5116 5117 /* 5118 * We can read 'pipe' data event from regular file, 5119 * check for the pipe header regardless of source. 5120 */ 5121 err = perf_header__read_pipe(session); 5122 if (!err || perf_data__is_pipe(data)) { 5123 data->is_pipe = true; 5124 return err; 5125 } 5126 5127 err = -ENOMEM; 5128 if (perf_file_header__read(&f_header, header, fd) < 0) 5129 return -EINVAL; 5130 5131 if (header->needs_swap && data->in_place_update) { 5132 pr_err("In-place update not supported when byte-swapping is required\n"); 5133 return -EINVAL; 5134 } 5135 5136 /* 5137 * Sanity check that perf.data was written cleanly; data size is 5138 * initialized to 0 and updated only if the on_exit function is run. 5139 * If data size is still 0 then the file contains only partial 5140 * information. Just warn user and process it as much as it can. 5141 */ 5142 if (f_header.data.size == 0) { 5143 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n" 5144 "Was the 'perf record' command properly terminated?\n", 5145 data->file.path); 5146 } 5147 5148 if (f_header.attr_size == 0) { 5149 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n" 5150 "Was the 'perf record' command properly terminated?\n", 5151 data->file.path); 5152 return -EINVAL; 5153 } 5154 5155 if (fstat(fd, &input_stat) < 0) 5156 return -errno; 5157 5158 /* Check before assigning to int to avoid u64-to-int truncation */ 5159 if (f_header.attrs.size / f_header.attr_size > MAX_NR_ATTRS) { 5160 pr_err("Too many attributes: %" PRIu64 " (max %d)\n", 5161 f_header.attrs.size / f_header.attr_size, MAX_NR_ATTRS); 5162 return -EINVAL; 5163 } 5164 nr_attrs = f_header.attrs.size / f_header.attr_size; 5165 lseek(fd, f_header.attrs.offset, SEEK_SET); 5166 5167 for (i = 0; i < nr_attrs; i++) { 5168 struct evsel *evsel; 5169 off_t tmp; 5170 5171 if (read_attr(fd, header, &f_attr) < 0) 5172 goto out_errno; 5173 5174 if (header->needs_swap) { 5175 f_attr.ids.size = bswap_64(f_attr.ids.size); 5176 f_attr.ids.offset = bswap_64(f_attr.ids.offset); 5177 perf_event__attr_swap(&f_attr.attr); 5178 } 5179 5180 /* 5181 * Validate ids section: must be aligned to u64, and 5182 * the count must fit in an int to avoid truncation in 5183 * nr_ids and size_t overflow in perf_evsel__alloc_id() 5184 * on 32-bit architectures. 5185 */ 5186 if (f_attr.ids.size % sizeof(u64)) { 5187 pr_err("Invalid ids section size %" PRIu64 " for attr %d, not aligned to u64\n", 5188 f_attr.ids.size, i); 5189 err = -EINVAL; 5190 goto out_delete_evlist; 5191 } 5192 5193 /* 5194 * Cap the ID count to avoid int truncation of nr_ids 5195 * on 64-bit and size_t overflow in the allocation 5196 * paths (nr_ids * sizeof(u64), nr_ids * 5197 * sizeof(struct perf_sample_id)) on 32-bit. 5198 */ 5199 if (f_attr.ids.size / sizeof(u64) > MAX_IDS_PER_ATTR) { 5200 pr_err("Invalid ids section size %" PRIu64 " for attr %d, too many IDs\n", 5201 f_attr.ids.size, i); 5202 err = -EINVAL; 5203 goto out_delete_evlist; 5204 } 5205 5206 /* 5207 * FIXME: see perf_header__process_sections() — block 5208 * devices bypass this check because st_size is 0. 5209 */ 5210 if (S_ISREG(input_stat.st_mode) && 5211 (f_attr.ids.offset > (u64)input_stat.st_size || 5212 f_attr.ids.size > (u64)input_stat.st_size - f_attr.ids.offset)) { 5213 pr_err("Invalid ids section for attr %d: offset=%" PRIu64 " size=%" PRIu64 " exceeds file size %" PRIu64 "\n", 5214 i, f_attr.ids.offset, f_attr.ids.size, (u64)input_stat.st_size); 5215 err = -EINVAL; 5216 goto out_delete_evlist; 5217 } 5218 5219 tmp = lseek(fd, 0, SEEK_CUR); 5220 evsel = evsel__new(&f_attr.attr); 5221 5222 if (evsel == NULL) 5223 goto out_delete_evlist; 5224 5225 evsel->needs_swap = header->needs_swap; 5226 /* 5227 * Do it before so that if perf_evsel__alloc_id fails, this 5228 * entry gets purged too at evlist__delete(). 5229 */ 5230 evlist__add(session->evlist, evsel); 5231 5232 nr_ids = f_attr.ids.size / sizeof(u64); 5233 /* 5234 * We don't have the cpu and thread maps on the header, so 5235 * for allocating the perf_sample_id table we fake 1 cpu and 5236 * hattr->ids threads. 5237 */ 5238 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids)) 5239 goto out_delete_evlist; 5240 5241 lseek(fd, f_attr.ids.offset, SEEK_SET); 5242 5243 for (j = 0; j < nr_ids; j++) { 5244 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) 5245 goto out_errno; 5246 5247 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id); 5248 } 5249 5250 lseek(fd, tmp, SEEK_SET); 5251 } 5252 5253 /* 5254 * Skip feature section processing for truncated files 5255 * (data.size == 0 means recording was interrupted). The 5256 * section table is unreliable in that case, and the event 5257 * data can still be processed without the feature headers. 5258 * Clear the bitmap so has_feat() returns false and tools 5259 * use their "feature not present" fallbacks instead of 5260 * accessing uninitialized env fields. 5261 */ 5262 if (f_header.data.size == 0) { 5263 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 5264 } else { 5265 #ifdef HAVE_LIBTRACEEVENT 5266 err = perf_header__process_sections(header, fd, &session->tevent, 5267 perf_file_section__process); 5268 if (err < 0) 5269 goto out_delete_evlist; 5270 5271 if (evlist__prepare_tracepoint_events(session->evlist, 5272 session->tevent.pevent)) { 5273 err = -ENOMEM; 5274 goto out_delete_evlist; 5275 } 5276 #else 5277 err = perf_header__process_sections(header, fd, NULL, 5278 perf_file_section__process); 5279 if (err < 0) 5280 goto out_delete_evlist; 5281 #endif 5282 } 5283 5284 /* 5285 * Without nr_cpus_avail the sample CPU bounds check in 5286 * perf_session__deliver_event() is bypassed, allowing crafted 5287 * CPU IDs to reach downstream consumers that index fixed-size 5288 * arrays (timechart, kwork, sched — all sized MAX_NR_CPUS). 5289 * 5290 * This can happen with truncated files (interrupted recording 5291 * loses all feature sections), very old files that predate 5292 * HEADER_NRCPUS, or crafted files that omit it. Fall back to 5293 * MAX_NR_CPUS so the bounds check is still effective — any 5294 * CPU ID below that limit is safe for all downstream arrays. 5295 */ 5296 if (header->env.nr_cpus_avail == 0) { 5297 header->env.nr_cpus_avail = MAX_NR_CPUS; 5298 pr_warning("WARNING: perf.data is missing HEADER_NRCPUS, using MAX_NR_CPUS (%d) as CPU bound\n", 5299 MAX_NR_CPUS); 5300 } 5301 5302 return 0; 5303 out_errno: 5304 return -errno; 5305 5306 out_delete_evlist: 5307 evlist__delete(session->evlist); 5308 session->evlist = NULL; 5309 return err; 5310 } 5311 5312 int perf_event__process_feature(const struct perf_tool *tool __maybe_unused, 5313 struct perf_session *session, 5314 union perf_event *event) 5315 { 5316 struct feat_fd ff = { .fd = 0 }; 5317 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event; 5318 struct perf_header *header = &session->header; 5319 int type = fe->header.type; 5320 int feat = (int)fe->feat_id; 5321 int ret = 0; 5322 bool print = dump_trace; 5323 bool last_feature_mark = false; 5324 5325 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) { 5326 pr_warning("invalid record type %d in pipe-mode\n", type); 5327 return 0; 5328 } 5329 if (feat == HEADER_RESERVED) { 5330 pr_warning("invalid reserved record type in pipe-mode\n"); 5331 return -1; 5332 } 5333 if (feat < 0 || feat == INT_MAX) { 5334 pr_warning("invalid value for feature type %x\n", feat); 5335 return -1; 5336 } 5337 if (feat >= header->last_feat) { 5338 if (event->header.size == sizeof(*fe)) { 5339 /* 5340 * Either an unexpected zero size feature or the 5341 * HEADER_LAST_FEATURE mark. 5342 */ 5343 if (feat > header->last_feat) 5344 header->last_feat = min(feat, HEADER_LAST_FEATURE); 5345 last_feature_mark = true; 5346 } else { 5347 /* 5348 * A feature but beyond what is known as in 5349 * bounds. Assume the last feature is 1 beyond this 5350 * feature. 5351 */ 5352 session->header.last_feat = min(feat + 1, HEADER_LAST_FEATURE); 5353 } 5354 } 5355 if (feat >= HEADER_LAST_FEATURE) { 5356 if (!last_feature_mark) { 5357 pr_warning("unknown feature %d for data file version (%s) in this version of perf (%s)\n", 5358 feat, header->env.version, perf_version_string); 5359 } 5360 return 0; 5361 } 5362 if (event->header.size < sizeof(*fe)) { 5363 pr_warning("feature header size too small\n"); 5364 return -1; 5365 } 5366 ff.buf = (void *)fe->data; 5367 ff.size = event->header.size - sizeof(*fe); 5368 ff.ph = header; 5369 5370 if (feat_ops[feat].process && feat_ops[feat].process(&ff, NULL)) { 5371 // Processing failed, ignore when this is the last feature mark. 5372 if (!last_feature_mark) 5373 ret = -1; 5374 goto out; 5375 } 5376 5377 if (session->tool->show_feat_hdr) { 5378 if (!feat_ops[feat].full_only || 5379 session->tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) { 5380 print = true; 5381 } else { 5382 fprintf(stdout, "# %s info available, use -I to display\n", 5383 feat_ops[feat].name); 5384 } 5385 } 5386 5387 if (dump_trace) 5388 printf(", "); 5389 5390 if (print) { 5391 if (feat_ops[feat].print) 5392 feat_ops[feat].print(&ff, stdout); 5393 else 5394 printf("# %s", feat_ops[feat].name); 5395 } 5396 5397 out: 5398 free_event_desc(ff.events); 5399 return ret; 5400 } 5401 5402 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp) 5403 { 5404 struct perf_record_event_update *ev = &event->event_update; 5405 struct perf_cpu_map *map; 5406 size_t ret; 5407 5408 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id); 5409 5410 switch (ev->type) { 5411 case PERF_EVENT_UPDATE__SCALE: 5412 if (event->header.size < offsetof(struct perf_record_event_update, scale) + 5413 sizeof(ev->scale)) { 5414 ret += fprintf(fp, "... scale: (truncated)\n"); 5415 break; 5416 } 5417 ret += fprintf(fp, "... scale: %f\n", ev->scale.scale); 5418 break; 5419 case PERF_EVENT_UPDATE__UNIT: 5420 case PERF_EVENT_UPDATE__NAME: { 5421 size_t str_off = offsetof(struct perf_record_event_update, unit); 5422 size_t max_len = event->header.size > str_off ? 5423 event->header.size - str_off : 0; 5424 5425 if (max_len == 0 || strnlen(ev->unit, max_len) == max_len) { 5426 ret += fprintf(fp, "... %s: (unterminated)\n", 5427 ev->type == PERF_EVENT_UPDATE__UNIT ? "unit" : "name"); 5428 break; 5429 } 5430 ret += fprintf(fp, "... %s: %s\n", 5431 ev->type == PERF_EVENT_UPDATE__UNIT ? "unit" : "name", 5432 ev->unit); 5433 break; 5434 } 5435 case PERF_EVENT_UPDATE__CPUS: { 5436 size_t cpus_off = offsetof(struct perf_record_event_update, cpus); 5437 u32 cpus_payload; 5438 5439 if (event->header.size < cpus_off + sizeof(__u16) + 5440 sizeof(struct perf_record_range_cpu_map)) { 5441 ret += fprintf(fp, "... cpus: (truncated)\n"); 5442 break; 5443 } 5444 5445 /* 5446 * Validate nr against payload — this function may be 5447 * called from the stub handler (dump_trace path) which 5448 * bypasses perf_event__process_event_update() validation. 5449 */ 5450 cpus_payload = event->header.size - cpus_off; 5451 if (ev->cpus.cpus.type == PERF_CPU_MAP__CPUS) { 5452 if (cpus_payload < offsetof(struct perf_record_cpu_map_data, cpus_data.cpu) || 5453 ev->cpus.cpus.cpus_data.nr > 5454 (cpus_payload - offsetof(struct perf_record_cpu_map_data, cpus_data.cpu)) / 5455 sizeof(ev->cpus.cpus.cpus_data.cpu[0])) { 5456 ret += fprintf(fp, "... cpus: nr %u exceeds payload\n", 5457 ev->cpus.cpus.cpus_data.nr); 5458 break; 5459 } 5460 } else if (ev->cpus.cpus.type == PERF_CPU_MAP__MASK) { 5461 if (ev->cpus.cpus.mask32_data.long_size == 4) { 5462 if (cpus_payload < offsetof(struct perf_record_cpu_map_data, mask32_data.mask) || 5463 ev->cpus.cpus.mask32_data.nr > 5464 (cpus_payload - offsetof(struct perf_record_cpu_map_data, mask32_data.mask)) / 5465 sizeof(ev->cpus.cpus.mask32_data.mask[0])) { 5466 ret += fprintf(fp, "... cpus: mask nr %u exceeds payload\n", 5467 ev->cpus.cpus.mask32_data.nr); 5468 break; 5469 } 5470 } else if (ev->cpus.cpus.mask64_data.long_size == 8) { 5471 if (cpus_payload < offsetof(struct perf_record_cpu_map_data, mask64_data.mask) || 5472 ev->cpus.cpus.mask64_data.nr > 5473 (cpus_payload - offsetof(struct perf_record_cpu_map_data, mask64_data.mask)) / 5474 sizeof(ev->cpus.cpus.mask64_data.mask[0])) { 5475 ret += fprintf(fp, "... cpus: mask nr %u exceeds payload\n", 5476 ev->cpus.cpus.mask64_data.nr); 5477 break; 5478 } 5479 } 5480 } 5481 5482 ret += fprintf(fp, "... "); 5483 5484 map = cpu_map__new_data(&ev->cpus.cpus); 5485 if (map) { 5486 ret += cpu_map__fprintf(map, fp); 5487 perf_cpu_map__put(map); 5488 } else 5489 ret += fprintf(fp, "failed to get cpus\n"); 5490 break; 5491 } 5492 default: 5493 ret += fprintf(fp, "... unknown type\n"); 5494 break; 5495 } 5496 5497 return ret; 5498 } 5499 5500 size_t perf_event__fprintf_attr(union perf_event *event, FILE *fp) 5501 { 5502 return perf_event_attr__fprintf(fp, &event->attr.attr, __desc_attr__fprintf, NULL); 5503 } 5504 5505 int perf_event__process_attr(const struct perf_tool *tool __maybe_unused, 5506 union perf_event *event, 5507 struct evlist **pevlist) 5508 { 5509 struct perf_event_attr attr; 5510 u32 i, n_ids, raw_attr_size; 5511 u64 *ids; 5512 size_t attr_size, copy_size; 5513 struct evsel *evsel; 5514 struct evlist *evlist = *pevlist; 5515 5516 /* 5517 * HEADER_ATTR event layout (pipe/inject mode): 5518 * 5519 * [header (8 bytes)] [attr (attr_size bytes)] [id0 id1 ... idN] 5520 * |<------------------ header.size --------------------------->| 5521 * 5522 * attr_size varies across perf versions: VER0 = 64 bytes, 5523 * current sizeof(struct perf_event_attr) = larger. A newer 5524 * producer may emit a larger attr than we understand. 5525 * 5526 * attr.size == 0 (ABI0) means the producer didn't set it 5527 * (e.g., bench/inject-buildid, older perf). Treat as VER0. 5528 * 5529 * Require 8-byte alignment so the u64 ID array is aligned 5530 * and attr.size fits cleanly within the payload. 5531 * 5532 * Read attr.size once — the event may be on a shared mmap 5533 * and re-reading could yield a different value. 5534 */ 5535 raw_attr_size = event->attr.attr.size; 5536 if (event->header.size < sizeof(event->header) + PERF_ATTR_SIZE_VER0 || 5537 (raw_attr_size && (raw_attr_size < PERF_ATTR_SIZE_VER0 || 5538 raw_attr_size % sizeof(u64) || 5539 raw_attr_size > event->header.size - sizeof(event->header)))) { 5540 pr_err("PERF_RECORD_HEADER_ATTR: invalid attr.size %u (event size %u, min %d)\n", 5541 raw_attr_size, event->header.size, PERF_ATTR_SIZE_VER0); 5542 return -EINVAL; 5543 } 5544 5545 if (dump_trace) 5546 perf_event__fprintf_attr(event, stdout); 5547 5548 if (evlist == NULL) { 5549 *pevlist = evlist = evlist__new(); 5550 if (evlist == NULL) 5551 return -ENOMEM; 5552 } 5553 5554 /* 5555 * attr_size = footprint of the attr in the event — determines 5556 * where the ID array starts. For ABI0, assume VER0 (64 bytes). 5557 * 5558 * copy_size = how much we copy into our local struct, capped at 5559 * sizeof(attr) so a newer producer's larger attr doesn't 5560 * overflow. Fields beyond copy_size are zeroed. 5561 * 5562 * Do NOT write attr_size back to the event — native-endian 5563 * files use MAP_SHARED (read-only), writing would SIGSEGV. 5564 * The swap path handles ABI0 in perf_event__attr_swap() 5565 * which writes to the writable MAP_PRIVATE copy instead. 5566 */ 5567 attr_size = raw_attr_size ?: PERF_ATTR_SIZE_VER0; 5568 copy_size = min(attr_size, sizeof(attr)); 5569 memcpy(&attr, &event->attr.attr, copy_size); 5570 if (copy_size < sizeof(attr)) 5571 memset((void *)&attr + copy_size, 0, sizeof(attr) - copy_size); 5572 5573 /* 5574 * Normalize ABI0: the swap path sets attr.size = VER0 on the 5575 * event, but the native path leaves it as 0. Set it on the 5576 * local copy so perf inject re-synthesizes with consistent 5577 * layout regardless of endianness. 5578 */ 5579 attr.size = attr_size; 5580 5581 evsel = evsel__new(&attr); 5582 if (evsel == NULL) 5583 return -ENOMEM; 5584 5585 evlist__add(evlist, evsel); 5586 5587 /* 5588 * IDs occupy the remainder after header + attr. Use attr_size 5589 * (not copy_size) — even if the producer's attr is larger than 5590 * our struct, the IDs start after attr_size bytes in the event. 5591 * Validation above guarantees attr_size <= payload size. 5592 */ 5593 n_ids = event->header.size - sizeof(event->header) - attr_size; 5594 n_ids = n_ids / sizeof(u64); 5595 /* 5596 * We don't have the cpu and thread maps on the header, so 5597 * for allocating the perf_sample_id table we fake 1 cpu and 5598 * hattr->ids threads. 5599 */ 5600 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids)) 5601 return -ENOMEM; 5602 5603 /* 5604 * Locate IDs at attr_size bytes past the attr start in the 5605 * event. Cannot use perf_record_header_attr_id() — that 5606 * macro reads event->attr.attr.size, which is 0 for ABI0 5607 * on the native-endian path (no swap handler to fix it up). 5608 */ 5609 ids = (void *)&event->attr.attr + attr_size; 5610 for (i = 0; i < n_ids; i++) { 5611 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, ids[i]); 5612 } 5613 5614 return 0; 5615 } 5616 5617 int perf_event__process_event_update(const struct perf_tool *tool __maybe_unused, 5618 union perf_event *event, 5619 struct evlist **pevlist) 5620 { 5621 struct perf_record_event_update *ev = &event->event_update; 5622 struct evlist *evlist; 5623 struct evsel *evsel; 5624 struct perf_cpu_map *map; 5625 5626 /* 5627 * Validate payload before dump_trace or processing — both 5628 * paths access variant-specific fields without further checks. 5629 */ 5630 if (ev->type == PERF_EVENT_UPDATE__UNIT || 5631 ev->type == PERF_EVENT_UPDATE__NAME) { 5632 size_t str_off = offsetof(struct perf_record_event_update, unit); 5633 size_t max_len = event->header.size > str_off ? 5634 event->header.size - str_off : 0; 5635 5636 if (max_len == 0 || strnlen(ev->unit, max_len) == max_len) { 5637 pr_warning("WARNING: PERF_RECORD_EVENT_UPDATE: %s not null-terminated, skipping\n", 5638 ev->type == PERF_EVENT_UPDATE__UNIT ? "unit" : "name"); 5639 return 0; 5640 } 5641 } else if (ev->type == PERF_EVENT_UPDATE__SCALE) { 5642 if (event->header.size < offsetof(struct perf_record_event_update, scale) + 5643 sizeof(ev->scale)) { 5644 pr_warning("WARNING: PERF_RECORD_EVENT_UPDATE: SCALE payload too small, skipping\n"); 5645 return 0; 5646 } 5647 } else if (ev->type == PERF_EVENT_UPDATE__CPUS) { 5648 size_t cpus_off = offsetof(struct perf_record_event_update, cpus); 5649 size_t min_cpus = sizeof(__u16) + 5650 sizeof(struct perf_record_range_cpu_map); 5651 u32 cpus_payload; 5652 5653 if (event->header.size < cpus_off + min_cpus) { 5654 pr_warning("WARNING: PERF_RECORD_EVENT_UPDATE: CPUS payload too small, skipping\n"); 5655 return 0; 5656 } 5657 5658 /* 5659 * Validate per-variant nr against the remaining 5660 * payload on the native path — the swap path clamps 5661 * nr in perf_event__event_update_swap(), but native 5662 * events are read-only and cannot be clamped in place. 5663 * cpu_map__new_data() trusts nr for allocation and 5664 * iteration, so unchecked values cause OOB reads. 5665 */ 5666 cpus_payload = event->header.size - cpus_off; 5667 switch (ev->cpus.cpus.type) { 5668 case PERF_CPU_MAP__CPUS: 5669 if (ev->cpus.cpus.cpus_data.nr > 5670 (cpus_payload - offsetof(struct perf_record_cpu_map_data, cpus_data.cpu)) / 5671 sizeof(ev->cpus.cpus.cpus_data.cpu[0])) { 5672 pr_warning("WARNING: EVENT_UPDATE CPUS: nr %u exceeds payload, skipping\n", 5673 ev->cpus.cpus.cpus_data.nr); 5674 return 0; 5675 } 5676 break; 5677 case PERF_CPU_MAP__MASK: 5678 if (ev->cpus.cpus.mask32_data.long_size == 4) { 5679 if (cpus_payload < offsetof(struct perf_record_cpu_map_data, mask32_data.mask) || 5680 ev->cpus.cpus.mask32_data.nr > 5681 (cpus_payload - offsetof(struct perf_record_cpu_map_data, mask32_data.mask)) / 5682 sizeof(ev->cpus.cpus.mask32_data.mask[0])) { 5683 pr_warning("WARNING: EVENT_UPDATE MASK: nr %u exceeds payload, skipping\n", 5684 ev->cpus.cpus.mask32_data.nr); 5685 return 0; 5686 } 5687 } else if (ev->cpus.cpus.mask64_data.long_size == 8) { 5688 if (cpus_payload < offsetof(struct perf_record_cpu_map_data, mask64_data.mask) || 5689 ev->cpus.cpus.mask64_data.nr > 5690 (cpus_payload - offsetof(struct perf_record_cpu_map_data, mask64_data.mask)) / 5691 sizeof(ev->cpus.cpus.mask64_data.mask[0])) { 5692 pr_warning("WARNING: EVENT_UPDATE MASK: nr %u exceeds payload, skipping\n", 5693 ev->cpus.cpus.mask64_data.nr); 5694 return 0; 5695 } 5696 } 5697 break; 5698 default: 5699 break; 5700 } 5701 } 5702 5703 if (dump_trace) 5704 perf_event__fprintf_event_update(event, stdout); 5705 5706 if (!pevlist || *pevlist == NULL) 5707 return -EINVAL; 5708 5709 evlist = *pevlist; 5710 5711 evsel = evlist__id2evsel(evlist, ev->id); 5712 if (evsel == NULL) 5713 return -EINVAL; 5714 5715 switch (ev->type) { 5716 case PERF_EVENT_UPDATE__UNIT: 5717 free((char *)evsel->unit); 5718 evsel->unit = strdup(ev->unit); 5719 break; 5720 case PERF_EVENT_UPDATE__NAME: 5721 free(evsel->name); 5722 evsel->name = strdup(ev->name); 5723 break; 5724 case PERF_EVENT_UPDATE__SCALE: 5725 evsel->scale = ev->scale.scale; 5726 break; 5727 case PERF_EVENT_UPDATE__CPUS: 5728 map = cpu_map__new_data(&ev->cpus.cpus); 5729 if (map) { 5730 perf_cpu_map__put(evsel->core.pmu_cpus); 5731 evsel->core.pmu_cpus = map; 5732 } else 5733 pr_err("failed to get event_update cpus\n"); 5734 break; 5735 default: 5736 break; 5737 } 5738 5739 return 0; 5740 } 5741 5742 #ifdef HAVE_LIBTRACEEVENT 5743 int perf_event__process_tracing_data(const struct perf_tool *tool __maybe_unused, 5744 struct perf_session *session, 5745 union perf_event *event) 5746 { 5747 ssize_t size_read, padding, size = event->tracing_data.size; 5748 int fd = perf_data__fd(session->data); 5749 char buf[BUFSIZ]; 5750 5751 /* 5752 * The pipe fd is already in proper place and in any case 5753 * we can't move it, and we'd screw the case where we read 5754 * 'pipe' data from regular file. The trace_report reads 5755 * data from 'fd' so we need to set it directly behind the 5756 * event, where the tracing data starts. 5757 */ 5758 if (!perf_data__is_pipe(session->data)) { 5759 off_t offset = lseek(fd, 0, SEEK_CUR); 5760 5761 /* setup for reading amidst mmap */ 5762 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data), 5763 SEEK_SET); 5764 } 5765 5766 size_read = trace_report(fd, &session->tevent, session->trace_event_repipe); 5767 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; 5768 5769 if (readn(fd, buf, padding) < 0) { 5770 pr_err("%s: reading input file", __func__); 5771 return -1; 5772 } 5773 if (session->trace_event_repipe) { 5774 int retw = write(STDOUT_FILENO, buf, padding); 5775 if (retw <= 0 || retw != padding) { 5776 pr_err("%s: repiping tracing data padding", __func__); 5777 return -1; 5778 } 5779 } 5780 5781 if (size_read + padding != size) { 5782 pr_err("%s: tracing data size mismatch", __func__); 5783 return -1; 5784 } 5785 5786 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent); 5787 5788 return size_read + padding; 5789 } 5790 #endif 5791 5792 int perf_event__process_build_id(const struct perf_tool *tool __maybe_unused, 5793 struct perf_session *session, 5794 union perf_event *event) 5795 { 5796 __event_process_build_id(&event->build_id, 5797 event->build_id.filename, 5798 session); 5799 return 0; 5800 } 5801