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