1 #define _FILE_OFFSET_BITS 64 2 3 #include "util.h" 4 #include <sys/types.h> 5 #include <byteswap.h> 6 #include <unistd.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <linux/list.h> 10 #include <linux/kernel.h> 11 #include <linux/bitops.h> 12 #include <sys/utsname.h> 13 14 #include "evlist.h" 15 #include "evsel.h" 16 #include "header.h" 17 #include "../perf.h" 18 #include "trace-event.h" 19 #include "session.h" 20 #include "symbol.h" 21 #include "debug.h" 22 #include "cpumap.h" 23 24 static bool no_buildid_cache = false; 25 26 static int event_count; 27 static struct perf_trace_event_type *events; 28 29 static u32 header_argc; 30 static const char **header_argv; 31 32 int perf_header__push_event(u64 id, const char *name) 33 { 34 struct perf_trace_event_type *nevents; 35 36 if (strlen(name) > MAX_EVENT_NAME) 37 pr_warning("Event %s will be truncated\n", name); 38 39 nevents = realloc(events, (event_count + 1) * sizeof(*events)); 40 if (nevents == NULL) 41 return -ENOMEM; 42 events = nevents; 43 44 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); 45 events[event_count].event_id = id; 46 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); 47 event_count++; 48 return 0; 49 } 50 51 char *perf_header__find_event(u64 id) 52 { 53 int i; 54 for (i = 0 ; i < event_count; i++) { 55 if (events[i].event_id == id) 56 return events[i].name; 57 } 58 return NULL; 59 } 60 61 /* 62 * magic2 = "PERFILE2" 63 * must be a numerical value to let the endianness 64 * determine the memory layout. That way we are able 65 * to detect endianness when reading the perf.data file 66 * back. 67 * 68 * we check for legacy (PERFFILE) format. 69 */ 70 static const char *__perf_magic1 = "PERFFILE"; 71 static const u64 __perf_magic2 = 0x32454c4946524550ULL; 72 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; 73 74 #define PERF_MAGIC __perf_magic2 75 76 struct perf_file_attr { 77 struct perf_event_attr attr; 78 struct perf_file_section ids; 79 }; 80 81 void perf_header__set_feat(struct perf_header *header, int feat) 82 { 83 set_bit(feat, header->adds_features); 84 } 85 86 void perf_header__clear_feat(struct perf_header *header, int feat) 87 { 88 clear_bit(feat, header->adds_features); 89 } 90 91 bool perf_header__has_feat(const struct perf_header *header, int feat) 92 { 93 return test_bit(feat, header->adds_features); 94 } 95 96 static int do_write(int fd, const void *buf, size_t size) 97 { 98 while (size) { 99 int ret = write(fd, buf, size); 100 101 if (ret < 0) 102 return -errno; 103 104 size -= ret; 105 buf += ret; 106 } 107 108 return 0; 109 } 110 111 #define NAME_ALIGN 64 112 113 static int write_padded(int fd, const void *bf, size_t count, 114 size_t count_aligned) 115 { 116 static const char zero_buf[NAME_ALIGN]; 117 int err = do_write(fd, bf, count); 118 119 if (!err) 120 err = do_write(fd, zero_buf, count_aligned - count); 121 122 return err; 123 } 124 125 static int do_write_string(int fd, const char *str) 126 { 127 u32 len, olen; 128 int ret; 129 130 olen = strlen(str) + 1; 131 len = ALIGN(olen, NAME_ALIGN); 132 133 /* write len, incl. \0 */ 134 ret = do_write(fd, &len, sizeof(len)); 135 if (ret < 0) 136 return ret; 137 138 return write_padded(fd, str, olen, len); 139 } 140 141 static char *do_read_string(int fd, struct perf_header *ph) 142 { 143 ssize_t sz, ret; 144 u32 len; 145 char *buf; 146 147 sz = read(fd, &len, sizeof(len)); 148 if (sz < (ssize_t)sizeof(len)) 149 return NULL; 150 151 if (ph->needs_swap) 152 len = bswap_32(len); 153 154 buf = malloc(len); 155 if (!buf) 156 return NULL; 157 158 ret = read(fd, buf, len); 159 if (ret == (ssize_t)len) { 160 /* 161 * strings are padded by zeroes 162 * thus the actual strlen of buf 163 * may be less than len 164 */ 165 return buf; 166 } 167 168 free(buf); 169 return NULL; 170 } 171 172 int 173 perf_header__set_cmdline(int argc, const char **argv) 174 { 175 int i; 176 177 header_argc = (u32)argc; 178 179 /* do not include NULL termination */ 180 header_argv = calloc(argc, sizeof(char *)); 181 if (!header_argv) 182 return -ENOMEM; 183 184 /* 185 * must copy argv contents because it gets moved 186 * around during option parsing 187 */ 188 for (i = 0; i < argc ; i++) 189 header_argv[i] = argv[i]; 190 191 return 0; 192 } 193 194 #define dsos__for_each_with_build_id(pos, head) \ 195 list_for_each_entry(pos, head, node) \ 196 if (!pos->has_build_id) \ 197 continue; \ 198 else 199 200 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, 201 u16 misc, int fd) 202 { 203 struct dso *pos; 204 205 dsos__for_each_with_build_id(pos, head) { 206 int err; 207 struct build_id_event b; 208 size_t len; 209 210 if (!pos->hit) 211 continue; 212 len = pos->long_name_len + 1; 213 len = ALIGN(len, NAME_ALIGN); 214 memset(&b, 0, sizeof(b)); 215 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); 216 b.pid = pid; 217 b.header.misc = misc; 218 b.header.size = sizeof(b) + len; 219 err = do_write(fd, &b, sizeof(b)); 220 if (err < 0) 221 return err; 222 err = write_padded(fd, pos->long_name, 223 pos->long_name_len + 1, len); 224 if (err < 0) 225 return err; 226 } 227 228 return 0; 229 } 230 231 static int machine__write_buildid_table(struct machine *machine, int fd) 232 { 233 int err; 234 u16 kmisc = PERF_RECORD_MISC_KERNEL, 235 umisc = PERF_RECORD_MISC_USER; 236 237 if (!machine__is_host(machine)) { 238 kmisc = PERF_RECORD_MISC_GUEST_KERNEL; 239 umisc = PERF_RECORD_MISC_GUEST_USER; 240 } 241 242 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, 243 kmisc, fd); 244 if (err == 0) 245 err = __dsos__write_buildid_table(&machine->user_dsos, 246 machine->pid, umisc, fd); 247 return err; 248 } 249 250 static int dsos__write_buildid_table(struct perf_header *header, int fd) 251 { 252 struct perf_session *session = container_of(header, 253 struct perf_session, header); 254 struct rb_node *nd; 255 int err = machine__write_buildid_table(&session->host_machine, fd); 256 257 if (err) 258 return err; 259 260 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 261 struct machine *pos = rb_entry(nd, struct machine, rb_node); 262 err = machine__write_buildid_table(pos, fd); 263 if (err) 264 break; 265 } 266 return err; 267 } 268 269 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, 270 const char *name, bool is_kallsyms) 271 { 272 const size_t size = PATH_MAX; 273 char *realname, *filename = zalloc(size), 274 *linkname = zalloc(size), *targetname; 275 int len, err = -1; 276 277 if (is_kallsyms) { 278 if (symbol_conf.kptr_restrict) { 279 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); 280 return 0; 281 } 282 realname = (char *)name; 283 } else 284 realname = realpath(name, NULL); 285 286 if (realname == NULL || filename == NULL || linkname == NULL) 287 goto out_free; 288 289 len = scnprintf(filename, size, "%s%s%s", 290 debugdir, is_kallsyms ? "/" : "", realname); 291 if (mkdir_p(filename, 0755)) 292 goto out_free; 293 294 snprintf(filename + len, size - len, "/%s", sbuild_id); 295 296 if (access(filename, F_OK)) { 297 if (is_kallsyms) { 298 if (copyfile("/proc/kallsyms", filename)) 299 goto out_free; 300 } else if (link(realname, filename) && copyfile(name, filename)) 301 goto out_free; 302 } 303 304 len = scnprintf(linkname, size, "%s/.build-id/%.2s", 305 debugdir, sbuild_id); 306 307 if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 308 goto out_free; 309 310 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); 311 targetname = filename + strlen(debugdir) - 5; 312 memcpy(targetname, "../..", 5); 313 314 if (symlink(targetname, linkname) == 0) 315 err = 0; 316 out_free: 317 if (!is_kallsyms) 318 free(realname); 319 free(filename); 320 free(linkname); 321 return err; 322 } 323 324 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, 325 const char *name, const char *debugdir, 326 bool is_kallsyms) 327 { 328 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 329 330 build_id__sprintf(build_id, build_id_size, sbuild_id); 331 332 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms); 333 } 334 335 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) 336 { 337 const size_t size = PATH_MAX; 338 char *filename = zalloc(size), 339 *linkname = zalloc(size); 340 int err = -1; 341 342 if (filename == NULL || linkname == NULL) 343 goto out_free; 344 345 snprintf(linkname, size, "%s/.build-id/%.2s/%s", 346 debugdir, sbuild_id, sbuild_id + 2); 347 348 if (access(linkname, F_OK)) 349 goto out_free; 350 351 if (readlink(linkname, filename, size - 1) < 0) 352 goto out_free; 353 354 if (unlink(linkname)) 355 goto out_free; 356 357 /* 358 * Since the link is relative, we must make it absolute: 359 */ 360 snprintf(linkname, size, "%s/.build-id/%.2s/%s", 361 debugdir, sbuild_id, filename); 362 363 if (unlink(linkname)) 364 goto out_free; 365 366 err = 0; 367 out_free: 368 free(filename); 369 free(linkname); 370 return err; 371 } 372 373 static int dso__cache_build_id(struct dso *dso, const char *debugdir) 374 { 375 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; 376 377 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), 378 dso->long_name, debugdir, is_kallsyms); 379 } 380 381 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) 382 { 383 struct dso *pos; 384 int err = 0; 385 386 dsos__for_each_with_build_id(pos, head) 387 if (dso__cache_build_id(pos, debugdir)) 388 err = -1; 389 390 return err; 391 } 392 393 static int machine__cache_build_ids(struct machine *machine, const char *debugdir) 394 { 395 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); 396 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); 397 return ret; 398 } 399 400 static int perf_session__cache_build_ids(struct perf_session *session) 401 { 402 struct rb_node *nd; 403 int ret; 404 char debugdir[PATH_MAX]; 405 406 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); 407 408 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) 409 return -1; 410 411 ret = machine__cache_build_ids(&session->host_machine, debugdir); 412 413 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 414 struct machine *pos = rb_entry(nd, struct machine, rb_node); 415 ret |= machine__cache_build_ids(pos, debugdir); 416 } 417 return ret ? -1 : 0; 418 } 419 420 static bool machine__read_build_ids(struct machine *machine, bool with_hits) 421 { 422 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); 423 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); 424 return ret; 425 } 426 427 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) 428 { 429 struct rb_node *nd; 430 bool ret = machine__read_build_ids(&session->host_machine, with_hits); 431 432 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 433 struct machine *pos = rb_entry(nd, struct machine, rb_node); 434 ret |= machine__read_build_ids(pos, with_hits); 435 } 436 437 return ret; 438 } 439 440 static int write_tracing_data(int fd, struct perf_header *h __used, 441 struct perf_evlist *evlist) 442 { 443 return read_tracing_data(fd, &evlist->entries); 444 } 445 446 447 static int write_build_id(int fd, struct perf_header *h, 448 struct perf_evlist *evlist __used) 449 { 450 struct perf_session *session; 451 int err; 452 453 session = container_of(h, struct perf_session, header); 454 455 if (!perf_session__read_build_ids(session, true)) 456 return -1; 457 458 err = dsos__write_buildid_table(h, fd); 459 if (err < 0) { 460 pr_debug("failed to write buildid table\n"); 461 return err; 462 } 463 if (!no_buildid_cache) 464 perf_session__cache_build_ids(session); 465 466 return 0; 467 } 468 469 static int write_hostname(int fd, struct perf_header *h __used, 470 struct perf_evlist *evlist __used) 471 { 472 struct utsname uts; 473 int ret; 474 475 ret = uname(&uts); 476 if (ret < 0) 477 return -1; 478 479 return do_write_string(fd, uts.nodename); 480 } 481 482 static int write_osrelease(int fd, struct perf_header *h __used, 483 struct perf_evlist *evlist __used) 484 { 485 struct utsname uts; 486 int ret; 487 488 ret = uname(&uts); 489 if (ret < 0) 490 return -1; 491 492 return do_write_string(fd, uts.release); 493 } 494 495 static int write_arch(int fd, struct perf_header *h __used, 496 struct perf_evlist *evlist __used) 497 { 498 struct utsname uts; 499 int ret; 500 501 ret = uname(&uts); 502 if (ret < 0) 503 return -1; 504 505 return do_write_string(fd, uts.machine); 506 } 507 508 static int write_version(int fd, struct perf_header *h __used, 509 struct perf_evlist *evlist __used) 510 { 511 return do_write_string(fd, perf_version_string); 512 } 513 514 static int write_cpudesc(int fd, struct perf_header *h __used, 515 struct perf_evlist *evlist __used) 516 { 517 #ifndef CPUINFO_PROC 518 #define CPUINFO_PROC NULL 519 #endif 520 FILE *file; 521 char *buf = NULL; 522 char *s, *p; 523 const char *search = CPUINFO_PROC; 524 size_t len = 0; 525 int ret = -1; 526 527 if (!search) 528 return -1; 529 530 file = fopen("/proc/cpuinfo", "r"); 531 if (!file) 532 return -1; 533 534 while (getline(&buf, &len, file) > 0) { 535 ret = strncmp(buf, search, strlen(search)); 536 if (!ret) 537 break; 538 } 539 540 if (ret) 541 goto done; 542 543 s = buf; 544 545 p = strchr(buf, ':'); 546 if (p && *(p+1) == ' ' && *(p+2)) 547 s = p + 2; 548 p = strchr(s, '\n'); 549 if (p) 550 *p = '\0'; 551 552 /* squash extra space characters (branding string) */ 553 p = s; 554 while (*p) { 555 if (isspace(*p)) { 556 char *r = p + 1; 557 char *q = r; 558 *p = ' '; 559 while (*q && isspace(*q)) 560 q++; 561 if (q != (p+1)) 562 while ((*r++ = *q++)); 563 } 564 p++; 565 } 566 ret = do_write_string(fd, s); 567 done: 568 free(buf); 569 fclose(file); 570 return ret; 571 } 572 573 static int write_nrcpus(int fd, struct perf_header *h __used, 574 struct perf_evlist *evlist __used) 575 { 576 long nr; 577 u32 nrc, nra; 578 int ret; 579 580 nr = sysconf(_SC_NPROCESSORS_CONF); 581 if (nr < 0) 582 return -1; 583 584 nrc = (u32)(nr & UINT_MAX); 585 586 nr = sysconf(_SC_NPROCESSORS_ONLN); 587 if (nr < 0) 588 return -1; 589 590 nra = (u32)(nr & UINT_MAX); 591 592 ret = do_write(fd, &nrc, sizeof(nrc)); 593 if (ret < 0) 594 return ret; 595 596 return do_write(fd, &nra, sizeof(nra)); 597 } 598 599 static int write_event_desc(int fd, struct perf_header *h __used, 600 struct perf_evlist *evlist) 601 { 602 struct perf_evsel *attr; 603 u32 nre = 0, nri, sz; 604 int ret; 605 606 list_for_each_entry(attr, &evlist->entries, node) 607 nre++; 608 609 /* 610 * write number of events 611 */ 612 ret = do_write(fd, &nre, sizeof(nre)); 613 if (ret < 0) 614 return ret; 615 616 /* 617 * size of perf_event_attr struct 618 */ 619 sz = (u32)sizeof(attr->attr); 620 ret = do_write(fd, &sz, sizeof(sz)); 621 if (ret < 0) 622 return ret; 623 624 list_for_each_entry(attr, &evlist->entries, node) { 625 626 ret = do_write(fd, &attr->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 = attr->ids; 637 ret = do_write(fd, &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(fd, perf_evsel__name(attr)); 645 if (ret < 0) 646 return ret; 647 /* 648 * write unique ids for this event 649 */ 650 ret = do_write(fd, attr->id, attr->ids * sizeof(u64)); 651 if (ret < 0) 652 return ret; 653 } 654 return 0; 655 } 656 657 static int write_cmdline(int fd, struct perf_header *h __used, 658 struct perf_evlist *evlist __used) 659 { 660 char buf[MAXPATHLEN]; 661 char proc[32]; 662 u32 i, n; 663 int ret; 664 665 /* 666 * actual atual path to perf binary 667 */ 668 sprintf(proc, "/proc/%d/exe", getpid()); 669 ret = readlink(proc, buf, sizeof(buf)); 670 if (ret <= 0) 671 return -1; 672 673 /* readlink() does not add null termination */ 674 buf[ret] = '\0'; 675 676 /* account for binary path */ 677 n = header_argc + 1; 678 679 ret = do_write(fd, &n, sizeof(n)); 680 if (ret < 0) 681 return ret; 682 683 ret = do_write_string(fd, buf); 684 if (ret < 0) 685 return ret; 686 687 for (i = 0 ; i < header_argc; i++) { 688 ret = do_write_string(fd, header_argv[i]); 689 if (ret < 0) 690 return ret; 691 } 692 return 0; 693 } 694 695 #define CORE_SIB_FMT \ 696 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" 697 #define THRD_SIB_FMT \ 698 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" 699 700 struct cpu_topo { 701 u32 core_sib; 702 u32 thread_sib; 703 char **core_siblings; 704 char **thread_siblings; 705 }; 706 707 static int build_cpu_topo(struct cpu_topo *tp, int cpu) 708 { 709 FILE *fp; 710 char filename[MAXPATHLEN]; 711 char *buf = NULL, *p; 712 size_t len = 0; 713 u32 i = 0; 714 int ret = -1; 715 716 sprintf(filename, CORE_SIB_FMT, cpu); 717 fp = fopen(filename, "r"); 718 if (!fp) 719 return -1; 720 721 if (getline(&buf, &len, fp) <= 0) 722 goto done; 723 724 fclose(fp); 725 726 p = strchr(buf, '\n'); 727 if (p) 728 *p = '\0'; 729 730 for (i = 0; i < tp->core_sib; i++) { 731 if (!strcmp(buf, tp->core_siblings[i])) 732 break; 733 } 734 if (i == tp->core_sib) { 735 tp->core_siblings[i] = buf; 736 tp->core_sib++; 737 buf = NULL; 738 len = 0; 739 } 740 741 sprintf(filename, THRD_SIB_FMT, cpu); 742 fp = fopen(filename, "r"); 743 if (!fp) 744 goto done; 745 746 if (getline(&buf, &len, fp) <= 0) 747 goto done; 748 749 p = strchr(buf, '\n'); 750 if (p) 751 *p = '\0'; 752 753 for (i = 0; i < tp->thread_sib; i++) { 754 if (!strcmp(buf, tp->thread_siblings[i])) 755 break; 756 } 757 if (i == tp->thread_sib) { 758 tp->thread_siblings[i] = buf; 759 tp->thread_sib++; 760 buf = NULL; 761 } 762 ret = 0; 763 done: 764 if(fp) 765 fclose(fp); 766 free(buf); 767 return ret; 768 } 769 770 static void free_cpu_topo(struct cpu_topo *tp) 771 { 772 u32 i; 773 774 if (!tp) 775 return; 776 777 for (i = 0 ; i < tp->core_sib; i++) 778 free(tp->core_siblings[i]); 779 780 for (i = 0 ; i < tp->thread_sib; i++) 781 free(tp->thread_siblings[i]); 782 783 free(tp); 784 } 785 786 static struct cpu_topo *build_cpu_topology(void) 787 { 788 struct cpu_topo *tp; 789 void *addr; 790 u32 nr, i; 791 size_t sz; 792 long ncpus; 793 int ret = -1; 794 795 ncpus = sysconf(_SC_NPROCESSORS_CONF); 796 if (ncpus < 0) 797 return NULL; 798 799 nr = (u32)(ncpus & UINT_MAX); 800 801 sz = nr * sizeof(char *); 802 803 addr = calloc(1, sizeof(*tp) + 2 * sz); 804 if (!addr) 805 return NULL; 806 807 tp = addr; 808 809 addr += sizeof(*tp); 810 tp->core_siblings = addr; 811 addr += sz; 812 tp->thread_siblings = addr; 813 814 for (i = 0; i < nr; i++) { 815 ret = build_cpu_topo(tp, i); 816 if (ret < 0) 817 break; 818 } 819 if (ret) { 820 free_cpu_topo(tp); 821 tp = NULL; 822 } 823 return tp; 824 } 825 826 static int write_cpu_topology(int fd, struct perf_header *h __used, 827 struct perf_evlist *evlist __used) 828 { 829 struct cpu_topo *tp; 830 u32 i; 831 int ret; 832 833 tp = build_cpu_topology(); 834 if (!tp) 835 return -1; 836 837 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib)); 838 if (ret < 0) 839 goto done; 840 841 for (i = 0; i < tp->core_sib; i++) { 842 ret = do_write_string(fd, tp->core_siblings[i]); 843 if (ret < 0) 844 goto done; 845 } 846 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib)); 847 if (ret < 0) 848 goto done; 849 850 for (i = 0; i < tp->thread_sib; i++) { 851 ret = do_write_string(fd, tp->thread_siblings[i]); 852 if (ret < 0) 853 break; 854 } 855 done: 856 free_cpu_topo(tp); 857 return ret; 858 } 859 860 861 862 static int write_total_mem(int fd, struct perf_header *h __used, 863 struct perf_evlist *evlist __used) 864 { 865 char *buf = NULL; 866 FILE *fp; 867 size_t len = 0; 868 int ret = -1, n; 869 uint64_t mem; 870 871 fp = fopen("/proc/meminfo", "r"); 872 if (!fp) 873 return -1; 874 875 while (getline(&buf, &len, fp) > 0) { 876 ret = strncmp(buf, "MemTotal:", 9); 877 if (!ret) 878 break; 879 } 880 if (!ret) { 881 n = sscanf(buf, "%*s %"PRIu64, &mem); 882 if (n == 1) 883 ret = do_write(fd, &mem, sizeof(mem)); 884 } 885 free(buf); 886 fclose(fp); 887 return ret; 888 } 889 890 static int write_topo_node(int fd, int node) 891 { 892 char str[MAXPATHLEN]; 893 char field[32]; 894 char *buf = NULL, *p; 895 size_t len = 0; 896 FILE *fp; 897 u64 mem_total, mem_free, mem; 898 int ret = -1; 899 900 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node); 901 fp = fopen(str, "r"); 902 if (!fp) 903 return -1; 904 905 while (getline(&buf, &len, fp) > 0) { 906 /* skip over invalid lines */ 907 if (!strchr(buf, ':')) 908 continue; 909 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2) 910 goto done; 911 if (!strcmp(field, "MemTotal:")) 912 mem_total = mem; 913 if (!strcmp(field, "MemFree:")) 914 mem_free = mem; 915 } 916 917 fclose(fp); 918 919 ret = do_write(fd, &mem_total, sizeof(u64)); 920 if (ret) 921 goto done; 922 923 ret = do_write(fd, &mem_free, sizeof(u64)); 924 if (ret) 925 goto done; 926 927 ret = -1; 928 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node); 929 930 fp = fopen(str, "r"); 931 if (!fp) 932 goto done; 933 934 if (getline(&buf, &len, fp) <= 0) 935 goto done; 936 937 p = strchr(buf, '\n'); 938 if (p) 939 *p = '\0'; 940 941 ret = do_write_string(fd, buf); 942 done: 943 free(buf); 944 fclose(fp); 945 return ret; 946 } 947 948 static int write_numa_topology(int fd, struct perf_header *h __used, 949 struct perf_evlist *evlist __used) 950 { 951 char *buf = NULL; 952 size_t len = 0; 953 FILE *fp; 954 struct cpu_map *node_map = NULL; 955 char *c; 956 u32 nr, i, j; 957 int ret = -1; 958 959 fp = fopen("/sys/devices/system/node/online", "r"); 960 if (!fp) 961 return -1; 962 963 if (getline(&buf, &len, fp) <= 0) 964 goto done; 965 966 c = strchr(buf, '\n'); 967 if (c) 968 *c = '\0'; 969 970 node_map = cpu_map__new(buf); 971 if (!node_map) 972 goto done; 973 974 nr = (u32)node_map->nr; 975 976 ret = do_write(fd, &nr, sizeof(nr)); 977 if (ret < 0) 978 goto done; 979 980 for (i = 0; i < nr; i++) { 981 j = (u32)node_map->map[i]; 982 ret = do_write(fd, &j, sizeof(j)); 983 if (ret < 0) 984 break; 985 986 ret = write_topo_node(fd, i); 987 if (ret < 0) 988 break; 989 } 990 done: 991 free(buf); 992 fclose(fp); 993 free(node_map); 994 return ret; 995 } 996 997 /* 998 * default get_cpuid(): nothing gets recorded 999 * actual implementation must be in arch/$(ARCH)/util/header.c 1000 */ 1001 int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used) 1002 { 1003 return -1; 1004 } 1005 1006 static int write_cpuid(int fd, struct perf_header *h __used, 1007 struct perf_evlist *evlist __used) 1008 { 1009 char buffer[64]; 1010 int ret; 1011 1012 ret = get_cpuid(buffer, sizeof(buffer)); 1013 if (!ret) 1014 goto write_it; 1015 1016 return -1; 1017 write_it: 1018 return do_write_string(fd, buffer); 1019 } 1020 1021 static int write_branch_stack(int fd __used, struct perf_header *h __used, 1022 struct perf_evlist *evlist __used) 1023 { 1024 return 0; 1025 } 1026 1027 static void print_hostname(struct perf_header *ph, int fd, FILE *fp) 1028 { 1029 char *str = do_read_string(fd, ph); 1030 fprintf(fp, "# hostname : %s\n", str); 1031 free(str); 1032 } 1033 1034 static void print_osrelease(struct perf_header *ph, int fd, FILE *fp) 1035 { 1036 char *str = do_read_string(fd, ph); 1037 fprintf(fp, "# os release : %s\n", str); 1038 free(str); 1039 } 1040 1041 static void print_arch(struct perf_header *ph, int fd, FILE *fp) 1042 { 1043 char *str = do_read_string(fd, ph); 1044 fprintf(fp, "# arch : %s\n", str); 1045 free(str); 1046 } 1047 1048 static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp) 1049 { 1050 char *str = do_read_string(fd, ph); 1051 fprintf(fp, "# cpudesc : %s\n", str); 1052 free(str); 1053 } 1054 1055 static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp) 1056 { 1057 ssize_t ret; 1058 u32 nr; 1059 1060 ret = read(fd, &nr, sizeof(nr)); 1061 if (ret != (ssize_t)sizeof(nr)) 1062 nr = -1; /* interpreted as error */ 1063 1064 if (ph->needs_swap) 1065 nr = bswap_32(nr); 1066 1067 fprintf(fp, "# nrcpus online : %u\n", nr); 1068 1069 ret = read(fd, &nr, sizeof(nr)); 1070 if (ret != (ssize_t)sizeof(nr)) 1071 nr = -1; /* interpreted as error */ 1072 1073 if (ph->needs_swap) 1074 nr = bswap_32(nr); 1075 1076 fprintf(fp, "# nrcpus avail : %u\n", nr); 1077 } 1078 1079 static void print_version(struct perf_header *ph, int fd, FILE *fp) 1080 { 1081 char *str = do_read_string(fd, ph); 1082 fprintf(fp, "# perf version : %s\n", str); 1083 free(str); 1084 } 1085 1086 static void print_cmdline(struct perf_header *ph, int fd, FILE *fp) 1087 { 1088 ssize_t ret; 1089 char *str; 1090 u32 nr, i; 1091 1092 ret = read(fd, &nr, sizeof(nr)); 1093 if (ret != (ssize_t)sizeof(nr)) 1094 return; 1095 1096 if (ph->needs_swap) 1097 nr = bswap_32(nr); 1098 1099 fprintf(fp, "# cmdline : "); 1100 1101 for (i = 0; i < nr; i++) { 1102 str = do_read_string(fd, ph); 1103 fprintf(fp, "%s ", str); 1104 free(str); 1105 } 1106 fputc('\n', fp); 1107 } 1108 1109 static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp) 1110 { 1111 ssize_t ret; 1112 u32 nr, i; 1113 char *str; 1114 1115 ret = read(fd, &nr, sizeof(nr)); 1116 if (ret != (ssize_t)sizeof(nr)) 1117 return; 1118 1119 if (ph->needs_swap) 1120 nr = bswap_32(nr); 1121 1122 for (i = 0; i < nr; i++) { 1123 str = do_read_string(fd, ph); 1124 fprintf(fp, "# sibling cores : %s\n", str); 1125 free(str); 1126 } 1127 1128 ret = read(fd, &nr, sizeof(nr)); 1129 if (ret != (ssize_t)sizeof(nr)) 1130 return; 1131 1132 if (ph->needs_swap) 1133 nr = bswap_32(nr); 1134 1135 for (i = 0; i < nr; i++) { 1136 str = do_read_string(fd, ph); 1137 fprintf(fp, "# sibling threads : %s\n", str); 1138 free(str); 1139 } 1140 } 1141 1142 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) 1143 { 1144 struct perf_event_attr attr; 1145 uint64_t id; 1146 void *buf = NULL; 1147 char *str; 1148 u32 nre, sz, nr, i, j; 1149 ssize_t ret; 1150 size_t msz; 1151 1152 /* number of events */ 1153 ret = read(fd, &nre, sizeof(nre)); 1154 if (ret != (ssize_t)sizeof(nre)) 1155 goto error; 1156 1157 if (ph->needs_swap) 1158 nre = bswap_32(nre); 1159 1160 ret = read(fd, &sz, sizeof(sz)); 1161 if (ret != (ssize_t)sizeof(sz)) 1162 goto error; 1163 1164 if (ph->needs_swap) 1165 sz = bswap_32(sz); 1166 1167 memset(&attr, 0, sizeof(attr)); 1168 1169 /* buffer to hold on file attr struct */ 1170 buf = malloc(sz); 1171 if (!buf) 1172 goto error; 1173 1174 msz = sizeof(attr); 1175 if (sz < msz) 1176 msz = sz; 1177 1178 for (i = 0 ; i < nre; i++) { 1179 1180 /* 1181 * must read entire on-file attr struct to 1182 * sync up with layout. 1183 */ 1184 ret = read(fd, buf, sz); 1185 if (ret != (ssize_t)sz) 1186 goto error; 1187 1188 if (ph->needs_swap) 1189 perf_event__attr_swap(buf); 1190 1191 memcpy(&attr, buf, msz); 1192 1193 ret = read(fd, &nr, sizeof(nr)); 1194 if (ret != (ssize_t)sizeof(nr)) 1195 goto error; 1196 1197 if (ph->needs_swap) 1198 nr = bswap_32(nr); 1199 1200 str = do_read_string(fd, ph); 1201 fprintf(fp, "# event : name = %s, ", str); 1202 free(str); 1203 1204 fprintf(fp, "type = %d, config = 0x%"PRIx64 1205 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64, 1206 attr.type, 1207 (u64)attr.config, 1208 (u64)attr.config1, 1209 (u64)attr.config2); 1210 1211 fprintf(fp, ", excl_usr = %d, excl_kern = %d", 1212 attr.exclude_user, 1213 attr.exclude_kernel); 1214 1215 if (nr) 1216 fprintf(fp, ", id = {"); 1217 1218 for (j = 0 ; j < nr; j++) { 1219 ret = read(fd, &id, sizeof(id)); 1220 if (ret != (ssize_t)sizeof(id)) 1221 goto error; 1222 1223 if (ph->needs_swap) 1224 id = bswap_64(id); 1225 1226 if (j) 1227 fputc(',', fp); 1228 1229 fprintf(fp, " %"PRIu64, id); 1230 } 1231 if (nr && j == nr) 1232 fprintf(fp, " }"); 1233 fputc('\n', fp); 1234 } 1235 free(buf); 1236 return; 1237 error: 1238 fprintf(fp, "# event desc: not available or unable to read\n"); 1239 } 1240 1241 static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp) 1242 { 1243 uint64_t mem; 1244 ssize_t ret; 1245 1246 ret = read(fd, &mem, sizeof(mem)); 1247 if (ret != sizeof(mem)) 1248 goto error; 1249 1250 if (h->needs_swap) 1251 mem = bswap_64(mem); 1252 1253 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem); 1254 return; 1255 error: 1256 fprintf(fp, "# total memory : unknown\n"); 1257 } 1258 1259 static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp) 1260 { 1261 ssize_t ret; 1262 u32 nr, c, i; 1263 char *str; 1264 uint64_t mem_total, mem_free; 1265 1266 /* nr nodes */ 1267 ret = read(fd, &nr, sizeof(nr)); 1268 if (ret != (ssize_t)sizeof(nr)) 1269 goto error; 1270 1271 if (h->needs_swap) 1272 nr = bswap_32(nr); 1273 1274 for (i = 0; i < nr; i++) { 1275 1276 /* node number */ 1277 ret = read(fd, &c, sizeof(c)); 1278 if (ret != (ssize_t)sizeof(c)) 1279 goto error; 1280 1281 if (h->needs_swap) 1282 c = bswap_32(c); 1283 1284 ret = read(fd, &mem_total, sizeof(u64)); 1285 if (ret != sizeof(u64)) 1286 goto error; 1287 1288 ret = read(fd, &mem_free, sizeof(u64)); 1289 if (ret != sizeof(u64)) 1290 goto error; 1291 1292 if (h->needs_swap) { 1293 mem_total = bswap_64(mem_total); 1294 mem_free = bswap_64(mem_free); 1295 } 1296 1297 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," 1298 " free = %"PRIu64" kB\n", 1299 c, 1300 mem_total, 1301 mem_free); 1302 1303 str = do_read_string(fd, h); 1304 fprintf(fp, "# node%u cpu list : %s\n", c, str); 1305 free(str); 1306 } 1307 return; 1308 error: 1309 fprintf(fp, "# numa topology : not available\n"); 1310 } 1311 1312 static void print_cpuid(struct perf_header *ph, int fd, FILE *fp) 1313 { 1314 char *str = do_read_string(fd, ph); 1315 fprintf(fp, "# cpuid : %s\n", str); 1316 free(str); 1317 } 1318 1319 static void print_branch_stack(struct perf_header *ph __used, int fd __used, 1320 FILE *fp) 1321 { 1322 fprintf(fp, "# contains samples with branch stack\n"); 1323 } 1324 1325 static int __event_process_build_id(struct build_id_event *bev, 1326 char *filename, 1327 struct perf_session *session) 1328 { 1329 int err = -1; 1330 struct list_head *head; 1331 struct machine *machine; 1332 u16 misc; 1333 struct dso *dso; 1334 enum dso_kernel_type dso_type; 1335 1336 machine = perf_session__findnew_machine(session, bev->pid); 1337 if (!machine) 1338 goto out; 1339 1340 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1341 1342 switch (misc) { 1343 case PERF_RECORD_MISC_KERNEL: 1344 dso_type = DSO_TYPE_KERNEL; 1345 head = &machine->kernel_dsos; 1346 break; 1347 case PERF_RECORD_MISC_GUEST_KERNEL: 1348 dso_type = DSO_TYPE_GUEST_KERNEL; 1349 head = &machine->kernel_dsos; 1350 break; 1351 case PERF_RECORD_MISC_USER: 1352 case PERF_RECORD_MISC_GUEST_USER: 1353 dso_type = DSO_TYPE_USER; 1354 head = &machine->user_dsos; 1355 break; 1356 default: 1357 goto out; 1358 } 1359 1360 dso = __dsos__findnew(head, filename); 1361 if (dso != NULL) { 1362 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 1363 1364 dso__set_build_id(dso, &bev->build_id); 1365 1366 if (filename[0] == '[') 1367 dso->kernel = dso_type; 1368 1369 build_id__sprintf(dso->build_id, sizeof(dso->build_id), 1370 sbuild_id); 1371 pr_debug("build id event received for %s: %s\n", 1372 dso->long_name, sbuild_id); 1373 } 1374 1375 err = 0; 1376 out: 1377 return err; 1378 } 1379 1380 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, 1381 int input, u64 offset, u64 size) 1382 { 1383 struct perf_session *session = container_of(header, struct perf_session, header); 1384 struct { 1385 struct perf_event_header header; 1386 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))]; 1387 char filename[0]; 1388 } old_bev; 1389 struct build_id_event bev; 1390 char filename[PATH_MAX]; 1391 u64 limit = offset + size; 1392 1393 while (offset < limit) { 1394 ssize_t len; 1395 1396 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) 1397 return -1; 1398 1399 if (header->needs_swap) 1400 perf_event_header__bswap(&old_bev.header); 1401 1402 len = old_bev.header.size - sizeof(old_bev); 1403 if (read(input, filename, len) != len) 1404 return -1; 1405 1406 bev.header = old_bev.header; 1407 1408 /* 1409 * As the pid is the missing value, we need to fill 1410 * it properly. The header.misc value give us nice hint. 1411 */ 1412 bev.pid = HOST_KERNEL_ID; 1413 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || 1414 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) 1415 bev.pid = DEFAULT_GUEST_KERNEL_ID; 1416 1417 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); 1418 __event_process_build_id(&bev, filename, session); 1419 1420 offset += bev.header.size; 1421 } 1422 1423 return 0; 1424 } 1425 1426 static int perf_header__read_build_ids(struct perf_header *header, 1427 int input, u64 offset, u64 size) 1428 { 1429 struct perf_session *session = container_of(header, struct perf_session, header); 1430 struct build_id_event bev; 1431 char filename[PATH_MAX]; 1432 u64 limit = offset + size, orig_offset = offset; 1433 int err = -1; 1434 1435 while (offset < limit) { 1436 ssize_t len; 1437 1438 if (read(input, &bev, sizeof(bev)) != sizeof(bev)) 1439 goto out; 1440 1441 if (header->needs_swap) 1442 perf_event_header__bswap(&bev.header); 1443 1444 len = bev.header.size - sizeof(bev); 1445 if (read(input, filename, len) != len) 1446 goto out; 1447 /* 1448 * The a1645ce1 changeset: 1449 * 1450 * "perf: 'perf kvm' tool for monitoring guest performance from host" 1451 * 1452 * Added a field to struct build_id_event that broke the file 1453 * format. 1454 * 1455 * Since the kernel build-id is the first entry, process the 1456 * table using the old format if the well known 1457 * '[kernel.kallsyms]' string for the kernel build-id has the 1458 * first 4 characters chopped off (where the pid_t sits). 1459 */ 1460 if (memcmp(filename, "nel.kallsyms]", 13) == 0) { 1461 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) 1462 return -1; 1463 return perf_header__read_build_ids_abi_quirk(header, input, offset, size); 1464 } 1465 1466 __event_process_build_id(&bev, filename, session); 1467 1468 offset += bev.header.size; 1469 } 1470 err = 0; 1471 out: 1472 return err; 1473 } 1474 1475 static int process_tracing_data(struct perf_file_section *section __unused, 1476 struct perf_header *ph __unused, 1477 int feat __unused, int fd, void *data) 1478 { 1479 trace_report(fd, data, false); 1480 return 0; 1481 } 1482 1483 static int process_build_id(struct perf_file_section *section, 1484 struct perf_header *ph, 1485 int feat __unused, int fd, void *data __used) 1486 { 1487 if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) 1488 pr_debug("Failed to read buildids, continuing...\n"); 1489 return 0; 1490 } 1491 1492 struct feature_ops { 1493 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); 1494 void (*print)(struct perf_header *h, int fd, FILE *fp); 1495 int (*process)(struct perf_file_section *section, 1496 struct perf_header *h, int feat, int fd, void *data); 1497 const char *name; 1498 bool full_only; 1499 }; 1500 1501 #define FEAT_OPA(n, func) \ 1502 [n] = { .name = #n, .write = write_##func, .print = print_##func } 1503 #define FEAT_OPP(n, func) \ 1504 [n] = { .name = #n, .write = write_##func, .print = print_##func, \ 1505 .process = process_##func } 1506 #define FEAT_OPF(n, func) \ 1507 [n] = { .name = #n, .write = write_##func, .print = print_##func, \ 1508 .full_only = true } 1509 1510 /* feature_ops not implemented: */ 1511 #define print_tracing_data NULL 1512 #define print_build_id NULL 1513 1514 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { 1515 FEAT_OPP(HEADER_TRACING_DATA, tracing_data), 1516 FEAT_OPP(HEADER_BUILD_ID, build_id), 1517 FEAT_OPA(HEADER_HOSTNAME, hostname), 1518 FEAT_OPA(HEADER_OSRELEASE, osrelease), 1519 FEAT_OPA(HEADER_VERSION, version), 1520 FEAT_OPA(HEADER_ARCH, arch), 1521 FEAT_OPA(HEADER_NRCPUS, nrcpus), 1522 FEAT_OPA(HEADER_CPUDESC, cpudesc), 1523 FEAT_OPA(HEADER_CPUID, cpuid), 1524 FEAT_OPA(HEADER_TOTAL_MEM, total_mem), 1525 FEAT_OPA(HEADER_EVENT_DESC, event_desc), 1526 FEAT_OPA(HEADER_CMDLINE, cmdline), 1527 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), 1528 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), 1529 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack), 1530 }; 1531 1532 struct header_print_data { 1533 FILE *fp; 1534 bool full; /* extended list of headers */ 1535 }; 1536 1537 static int perf_file_section__fprintf_info(struct perf_file_section *section, 1538 struct perf_header *ph, 1539 int feat, int fd, void *data) 1540 { 1541 struct header_print_data *hd = data; 1542 1543 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 1544 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 1545 "%d, continuing...\n", section->offset, feat); 1546 return 0; 1547 } 1548 if (feat >= HEADER_LAST_FEATURE) { 1549 pr_warning("unknown feature %d\n", feat); 1550 return 0; 1551 } 1552 if (!feat_ops[feat].print) 1553 return 0; 1554 1555 if (!feat_ops[feat].full_only || hd->full) 1556 feat_ops[feat].print(ph, fd, hd->fp); 1557 else 1558 fprintf(hd->fp, "# %s info available, use -I to display\n", 1559 feat_ops[feat].name); 1560 1561 return 0; 1562 } 1563 1564 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) 1565 { 1566 struct header_print_data hd; 1567 struct perf_header *header = &session->header; 1568 int fd = session->fd; 1569 hd.fp = fp; 1570 hd.full = full; 1571 1572 perf_header__process_sections(header, fd, &hd, 1573 perf_file_section__fprintf_info); 1574 return 0; 1575 } 1576 1577 static int do_write_feat(int fd, struct perf_header *h, int type, 1578 struct perf_file_section **p, 1579 struct perf_evlist *evlist) 1580 { 1581 int err; 1582 int ret = 0; 1583 1584 if (perf_header__has_feat(h, type)) { 1585 if (!feat_ops[type].write) 1586 return -1; 1587 1588 (*p)->offset = lseek(fd, 0, SEEK_CUR); 1589 1590 err = feat_ops[type].write(fd, h, evlist); 1591 if (err < 0) { 1592 pr_debug("failed to write feature %d\n", type); 1593 1594 /* undo anything written */ 1595 lseek(fd, (*p)->offset, SEEK_SET); 1596 1597 return -1; 1598 } 1599 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; 1600 (*p)++; 1601 } 1602 return ret; 1603 } 1604 1605 static int perf_header__adds_write(struct perf_header *header, 1606 struct perf_evlist *evlist, int fd) 1607 { 1608 int nr_sections; 1609 struct perf_file_section *feat_sec, *p; 1610 int sec_size; 1611 u64 sec_start; 1612 int feat; 1613 int err; 1614 1615 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 1616 if (!nr_sections) 1617 return 0; 1618 1619 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections); 1620 if (feat_sec == NULL) 1621 return -ENOMEM; 1622 1623 sec_size = sizeof(*feat_sec) * nr_sections; 1624 1625 sec_start = header->data_offset + header->data_size; 1626 lseek(fd, sec_start + sec_size, SEEK_SET); 1627 1628 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 1629 if (do_write_feat(fd, header, feat, &p, evlist)) 1630 perf_header__clear_feat(header, feat); 1631 } 1632 1633 lseek(fd, sec_start, SEEK_SET); 1634 /* 1635 * may write more than needed due to dropped feature, but 1636 * this is okay, reader will skip the mising entries 1637 */ 1638 err = do_write(fd, feat_sec, sec_size); 1639 if (err < 0) 1640 pr_debug("failed to write feature section\n"); 1641 free(feat_sec); 1642 return err; 1643 } 1644 1645 int perf_header__write_pipe(int fd) 1646 { 1647 struct perf_pipe_file_header f_header; 1648 int err; 1649 1650 f_header = (struct perf_pipe_file_header){ 1651 .magic = PERF_MAGIC, 1652 .size = sizeof(f_header), 1653 }; 1654 1655 err = do_write(fd, &f_header, sizeof(f_header)); 1656 if (err < 0) { 1657 pr_debug("failed to write perf pipe header\n"); 1658 return err; 1659 } 1660 1661 return 0; 1662 } 1663 1664 int perf_session__write_header(struct perf_session *session, 1665 struct perf_evlist *evlist, 1666 int fd, bool at_exit) 1667 { 1668 struct perf_file_header f_header; 1669 struct perf_file_attr f_attr; 1670 struct perf_header *header = &session->header; 1671 struct perf_evsel *attr, *pair = NULL; 1672 int err; 1673 1674 lseek(fd, sizeof(f_header), SEEK_SET); 1675 1676 if (session->evlist != evlist) 1677 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node); 1678 1679 list_for_each_entry(attr, &evlist->entries, node) { 1680 attr->id_offset = lseek(fd, 0, SEEK_CUR); 1681 err = do_write(fd, attr->id, attr->ids * sizeof(u64)); 1682 if (err < 0) { 1683 out_err_write: 1684 pr_debug("failed to write perf header\n"); 1685 return err; 1686 } 1687 if (session->evlist != evlist) { 1688 err = do_write(fd, pair->id, pair->ids * sizeof(u64)); 1689 if (err < 0) 1690 goto out_err_write; 1691 attr->ids += pair->ids; 1692 pair = list_entry(pair->node.next, struct perf_evsel, node); 1693 } 1694 } 1695 1696 header->attr_offset = lseek(fd, 0, SEEK_CUR); 1697 1698 list_for_each_entry(attr, &evlist->entries, node) { 1699 f_attr = (struct perf_file_attr){ 1700 .attr = attr->attr, 1701 .ids = { 1702 .offset = attr->id_offset, 1703 .size = attr->ids * sizeof(u64), 1704 } 1705 }; 1706 err = do_write(fd, &f_attr, sizeof(f_attr)); 1707 if (err < 0) { 1708 pr_debug("failed to write perf header attribute\n"); 1709 return err; 1710 } 1711 } 1712 1713 header->event_offset = lseek(fd, 0, SEEK_CUR); 1714 header->event_size = event_count * sizeof(struct perf_trace_event_type); 1715 if (events) { 1716 err = do_write(fd, events, header->event_size); 1717 if (err < 0) { 1718 pr_debug("failed to write perf header events\n"); 1719 return err; 1720 } 1721 } 1722 1723 header->data_offset = lseek(fd, 0, SEEK_CUR); 1724 1725 if (at_exit) { 1726 err = perf_header__adds_write(header, evlist, fd); 1727 if (err < 0) 1728 return err; 1729 } 1730 1731 f_header = (struct perf_file_header){ 1732 .magic = PERF_MAGIC, 1733 .size = sizeof(f_header), 1734 .attr_size = sizeof(f_attr), 1735 .attrs = { 1736 .offset = header->attr_offset, 1737 .size = evlist->nr_entries * sizeof(f_attr), 1738 }, 1739 .data = { 1740 .offset = header->data_offset, 1741 .size = header->data_size, 1742 }, 1743 .event_types = { 1744 .offset = header->event_offset, 1745 .size = header->event_size, 1746 }, 1747 }; 1748 1749 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); 1750 1751 lseek(fd, 0, SEEK_SET); 1752 err = do_write(fd, &f_header, sizeof(f_header)); 1753 if (err < 0) { 1754 pr_debug("failed to write perf header\n"); 1755 return err; 1756 } 1757 lseek(fd, header->data_offset + header->data_size, SEEK_SET); 1758 1759 header->frozen = 1; 1760 return 0; 1761 } 1762 1763 static int perf_header__getbuffer64(struct perf_header *header, 1764 int fd, void *buf, size_t size) 1765 { 1766 if (readn(fd, buf, size) <= 0) 1767 return -1; 1768 1769 if (header->needs_swap) 1770 mem_bswap_64(buf, size); 1771 1772 return 0; 1773 } 1774 1775 int perf_header__process_sections(struct perf_header *header, int fd, 1776 void *data, 1777 int (*process)(struct perf_file_section *section, 1778 struct perf_header *ph, 1779 int feat, int fd, void *data)) 1780 { 1781 struct perf_file_section *feat_sec, *sec; 1782 int nr_sections; 1783 int sec_size; 1784 int feat; 1785 int err; 1786 1787 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 1788 if (!nr_sections) 1789 return 0; 1790 1791 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections); 1792 if (!feat_sec) 1793 return -1; 1794 1795 sec_size = sizeof(*feat_sec) * nr_sections; 1796 1797 lseek(fd, header->data_offset + header->data_size, SEEK_SET); 1798 1799 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); 1800 if (err < 0) 1801 goto out_free; 1802 1803 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { 1804 err = process(sec++, header, feat, fd, data); 1805 if (err < 0) 1806 goto out_free; 1807 } 1808 err = 0; 1809 out_free: 1810 free(feat_sec); 1811 return err; 1812 } 1813 1814 static const int attr_file_abi_sizes[] = { 1815 [0] = PERF_ATTR_SIZE_VER0, 1816 [1] = PERF_ATTR_SIZE_VER1, 1817 0, 1818 }; 1819 1820 /* 1821 * In the legacy file format, the magic number is not used to encode endianness. 1822 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based 1823 * on ABI revisions, we need to try all combinations for all endianness to 1824 * detect the endianness. 1825 */ 1826 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) 1827 { 1828 uint64_t ref_size, attr_size; 1829 int i; 1830 1831 for (i = 0 ; attr_file_abi_sizes[i]; i++) { 1832 ref_size = attr_file_abi_sizes[i] 1833 + sizeof(struct perf_file_section); 1834 if (hdr_sz != ref_size) { 1835 attr_size = bswap_64(hdr_sz); 1836 if (attr_size != ref_size) 1837 continue; 1838 1839 ph->needs_swap = true; 1840 } 1841 pr_debug("ABI%d perf.data file detected, need_swap=%d\n", 1842 i, 1843 ph->needs_swap); 1844 return 0; 1845 } 1846 /* could not determine endianness */ 1847 return -1; 1848 } 1849 1850 #define PERF_PIPE_HDR_VER0 16 1851 1852 static const size_t attr_pipe_abi_sizes[] = { 1853 [0] = PERF_PIPE_HDR_VER0, 1854 0, 1855 }; 1856 1857 /* 1858 * In the legacy pipe format, there is an implicit assumption that endiannesss 1859 * between host recording the samples, and host parsing the samples is the 1860 * same. This is not always the case given that the pipe output may always be 1861 * redirected into a file and analyzed on a different machine with possibly a 1862 * different endianness and perf_event ABI revsions in the perf tool itself. 1863 */ 1864 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) 1865 { 1866 u64 attr_size; 1867 int i; 1868 1869 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { 1870 if (hdr_sz != attr_pipe_abi_sizes[i]) { 1871 attr_size = bswap_64(hdr_sz); 1872 if (attr_size != hdr_sz) 1873 continue; 1874 1875 ph->needs_swap = true; 1876 } 1877 pr_debug("Pipe ABI%d perf.data file detected\n", i); 1878 return 0; 1879 } 1880 return -1; 1881 } 1882 1883 static int check_magic_endian(u64 magic, uint64_t hdr_sz, 1884 bool is_pipe, struct perf_header *ph) 1885 { 1886 int ret; 1887 1888 /* check for legacy format */ 1889 ret = memcmp(&magic, __perf_magic1, sizeof(magic)); 1890 if (ret == 0) { 1891 pr_debug("legacy perf.data format\n"); 1892 if (is_pipe) 1893 return try_all_pipe_abis(hdr_sz, ph); 1894 1895 return try_all_file_abis(hdr_sz, ph); 1896 } 1897 /* 1898 * the new magic number serves two purposes: 1899 * - unique number to identify actual perf.data files 1900 * - encode endianness of file 1901 */ 1902 1903 /* check magic number with one endianness */ 1904 if (magic == __perf_magic2) 1905 return 0; 1906 1907 /* check magic number with opposite endianness */ 1908 if (magic != __perf_magic2_sw) 1909 return -1; 1910 1911 ph->needs_swap = true; 1912 1913 return 0; 1914 } 1915 1916 int perf_file_header__read(struct perf_file_header *header, 1917 struct perf_header *ph, int fd) 1918 { 1919 int ret; 1920 1921 lseek(fd, 0, SEEK_SET); 1922 1923 ret = readn(fd, header, sizeof(*header)); 1924 if (ret <= 0) 1925 return -1; 1926 1927 if (check_magic_endian(header->magic, 1928 header->attr_size, false, ph) < 0) { 1929 pr_debug("magic/endian check failed\n"); 1930 return -1; 1931 } 1932 1933 if (ph->needs_swap) { 1934 mem_bswap_64(header, offsetof(struct perf_file_header, 1935 adds_features)); 1936 } 1937 1938 if (header->size != sizeof(*header)) { 1939 /* Support the previous format */ 1940 if (header->size == offsetof(typeof(*header), adds_features)) 1941 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 1942 else 1943 return -1; 1944 } else if (ph->needs_swap) { 1945 /* 1946 * feature bitmap is declared as an array of unsigned longs -- 1947 * not good since its size can differ between the host that 1948 * generated the data file and the host analyzing the file. 1949 * 1950 * We need to handle endianness, but we don't know the size of 1951 * the unsigned long where the file was generated. Take a best 1952 * guess at determining it: try 64-bit swap first (ie., file 1953 * created on a 64-bit host), and check if the hostname feature 1954 * bit is set (this feature bit is forced on as of fbe96f2). 1955 * If the bit is not, undo the 64-bit swap and try a 32-bit 1956 * swap. If the hostname bit is still not set (e.g., older data 1957 * file), punt and fallback to the original behavior -- 1958 * clearing all feature bits and setting buildid. 1959 */ 1960 mem_bswap_64(&header->adds_features, 1961 BITS_TO_U64(HEADER_FEAT_BITS)); 1962 1963 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 1964 /* unswap as u64 */ 1965 mem_bswap_64(&header->adds_features, 1966 BITS_TO_U64(HEADER_FEAT_BITS)); 1967 1968 /* unswap as u32 */ 1969 mem_bswap_32(&header->adds_features, 1970 BITS_TO_U32(HEADER_FEAT_BITS)); 1971 } 1972 1973 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 1974 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 1975 set_bit(HEADER_BUILD_ID, header->adds_features); 1976 } 1977 } 1978 1979 memcpy(&ph->adds_features, &header->adds_features, 1980 sizeof(ph->adds_features)); 1981 1982 ph->event_offset = header->event_types.offset; 1983 ph->event_size = header->event_types.size; 1984 ph->data_offset = header->data.offset; 1985 ph->data_size = header->data.size; 1986 return 0; 1987 } 1988 1989 static int perf_file_section__process(struct perf_file_section *section, 1990 struct perf_header *ph, 1991 int feat, int fd, void *data) 1992 { 1993 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 1994 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 1995 "%d, continuing...\n", section->offset, feat); 1996 return 0; 1997 } 1998 1999 if (feat >= HEADER_LAST_FEATURE) { 2000 pr_debug("unknown feature %d, continuing...\n", feat); 2001 return 0; 2002 } 2003 2004 if (!feat_ops[feat].process) 2005 return 0; 2006 2007 return feat_ops[feat].process(section, ph, feat, fd, data); 2008 } 2009 2010 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, 2011 struct perf_header *ph, int fd, 2012 bool repipe) 2013 { 2014 int ret; 2015 2016 ret = readn(fd, header, sizeof(*header)); 2017 if (ret <= 0) 2018 return -1; 2019 2020 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { 2021 pr_debug("endian/magic failed\n"); 2022 return -1; 2023 } 2024 2025 if (ph->needs_swap) 2026 header->size = bswap_64(header->size); 2027 2028 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) 2029 return -1; 2030 2031 return 0; 2032 } 2033 2034 static int perf_header__read_pipe(struct perf_session *session, int fd) 2035 { 2036 struct perf_header *header = &session->header; 2037 struct perf_pipe_file_header f_header; 2038 2039 if (perf_file_header__read_pipe(&f_header, header, fd, 2040 session->repipe) < 0) { 2041 pr_debug("incompatible file format\n"); 2042 return -EINVAL; 2043 } 2044 2045 session->fd = fd; 2046 2047 return 0; 2048 } 2049 2050 static int read_attr(int fd, struct perf_header *ph, 2051 struct perf_file_attr *f_attr) 2052 { 2053 struct perf_event_attr *attr = &f_attr->attr; 2054 size_t sz, left; 2055 size_t our_sz = sizeof(f_attr->attr); 2056 int ret; 2057 2058 memset(f_attr, 0, sizeof(*f_attr)); 2059 2060 /* read minimal guaranteed structure */ 2061 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); 2062 if (ret <= 0) { 2063 pr_debug("cannot read %d bytes of header attr\n", 2064 PERF_ATTR_SIZE_VER0); 2065 return -1; 2066 } 2067 2068 /* on file perf_event_attr size */ 2069 sz = attr->size; 2070 2071 if (ph->needs_swap) 2072 sz = bswap_32(sz); 2073 2074 if (sz == 0) { 2075 /* assume ABI0 */ 2076 sz = PERF_ATTR_SIZE_VER0; 2077 } else if (sz > our_sz) { 2078 pr_debug("file uses a more recent and unsupported ABI" 2079 " (%zu bytes extra)\n", sz - our_sz); 2080 return -1; 2081 } 2082 /* what we have not yet read and that we know about */ 2083 left = sz - PERF_ATTR_SIZE_VER0; 2084 if (left) { 2085 void *ptr = attr; 2086 ptr += PERF_ATTR_SIZE_VER0; 2087 2088 ret = readn(fd, ptr, left); 2089 } 2090 /* read perf_file_section, ids are read in caller */ 2091 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); 2092 2093 return ret <= 0 ? -1 : 0; 2094 } 2095 2096 static int perf_evsel__set_tracepoint_name(struct perf_evsel *evsel, 2097 struct pevent *pevent) 2098 { 2099 struct event_format *event = pevent_find_event(pevent, 2100 evsel->attr.config); 2101 char bf[128]; 2102 2103 if (event == NULL) 2104 return -1; 2105 2106 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); 2107 evsel->name = strdup(bf); 2108 if (event->name == NULL) 2109 return -1; 2110 2111 return 0; 2112 } 2113 2114 static int perf_evlist__set_tracepoint_names(struct perf_evlist *evlist, 2115 struct pevent *pevent) 2116 { 2117 struct perf_evsel *pos; 2118 2119 list_for_each_entry(pos, &evlist->entries, node) { 2120 if (pos->attr.type == PERF_TYPE_TRACEPOINT && 2121 perf_evsel__set_tracepoint_name(pos, pevent)) 2122 return -1; 2123 } 2124 2125 return 0; 2126 } 2127 2128 int perf_session__read_header(struct perf_session *session, int fd) 2129 { 2130 struct perf_header *header = &session->header; 2131 struct perf_file_header f_header; 2132 struct perf_file_attr f_attr; 2133 u64 f_id; 2134 int nr_attrs, nr_ids, i, j; 2135 2136 session->evlist = perf_evlist__new(NULL, NULL); 2137 if (session->evlist == NULL) 2138 return -ENOMEM; 2139 2140 if (session->fd_pipe) 2141 return perf_header__read_pipe(session, fd); 2142 2143 if (perf_file_header__read(&f_header, header, fd) < 0) 2144 return -EINVAL; 2145 2146 nr_attrs = f_header.attrs.size / f_header.attr_size; 2147 lseek(fd, f_header.attrs.offset, SEEK_SET); 2148 2149 for (i = 0; i < nr_attrs; i++) { 2150 struct perf_evsel *evsel; 2151 off_t tmp; 2152 2153 if (read_attr(fd, header, &f_attr) < 0) 2154 goto out_errno; 2155 2156 if (header->needs_swap) 2157 perf_event__attr_swap(&f_attr.attr); 2158 2159 tmp = lseek(fd, 0, SEEK_CUR); 2160 evsel = perf_evsel__new(&f_attr.attr, i); 2161 2162 if (evsel == NULL) 2163 goto out_delete_evlist; 2164 /* 2165 * Do it before so that if perf_evsel__alloc_id fails, this 2166 * entry gets purged too at perf_evlist__delete(). 2167 */ 2168 perf_evlist__add(session->evlist, evsel); 2169 2170 nr_ids = f_attr.ids.size / sizeof(u64); 2171 /* 2172 * We don't have the cpu and thread maps on the header, so 2173 * for allocating the perf_sample_id table we fake 1 cpu and 2174 * hattr->ids threads. 2175 */ 2176 if (perf_evsel__alloc_id(evsel, 1, nr_ids)) 2177 goto out_delete_evlist; 2178 2179 lseek(fd, f_attr.ids.offset, SEEK_SET); 2180 2181 for (j = 0; j < nr_ids; j++) { 2182 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) 2183 goto out_errno; 2184 2185 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); 2186 } 2187 2188 lseek(fd, tmp, SEEK_SET); 2189 } 2190 2191 symbol_conf.nr_events = nr_attrs; 2192 2193 if (f_header.event_types.size) { 2194 lseek(fd, f_header.event_types.offset, SEEK_SET); 2195 events = malloc(f_header.event_types.size); 2196 if (events == NULL) 2197 return -ENOMEM; 2198 if (perf_header__getbuffer64(header, fd, events, 2199 f_header.event_types.size)) 2200 goto out_errno; 2201 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); 2202 } 2203 2204 perf_header__process_sections(header, fd, &session->pevent, 2205 perf_file_section__process); 2206 2207 lseek(fd, header->data_offset, SEEK_SET); 2208 2209 if (perf_evlist__set_tracepoint_names(session->evlist, session->pevent)) 2210 goto out_delete_evlist; 2211 2212 header->frozen = 1; 2213 return 0; 2214 out_errno: 2215 return -errno; 2216 2217 out_delete_evlist: 2218 perf_evlist__delete(session->evlist); 2219 session->evlist = NULL; 2220 return -ENOMEM; 2221 } 2222 2223 int perf_event__synthesize_attr(struct perf_tool *tool, 2224 struct perf_event_attr *attr, u16 ids, u64 *id, 2225 perf_event__handler_t process) 2226 { 2227 union perf_event *ev; 2228 size_t size; 2229 int err; 2230 2231 size = sizeof(struct perf_event_attr); 2232 size = ALIGN(size, sizeof(u64)); 2233 size += sizeof(struct perf_event_header); 2234 size += ids * sizeof(u64); 2235 2236 ev = malloc(size); 2237 2238 if (ev == NULL) 2239 return -ENOMEM; 2240 2241 ev->attr.attr = *attr; 2242 memcpy(ev->attr.id, id, ids * sizeof(u64)); 2243 2244 ev->attr.header.type = PERF_RECORD_HEADER_ATTR; 2245 ev->attr.header.size = size; 2246 2247 err = process(tool, ev, NULL, NULL); 2248 2249 free(ev); 2250 2251 return err; 2252 } 2253 2254 int perf_event__synthesize_attrs(struct perf_tool *tool, 2255 struct perf_session *session, 2256 perf_event__handler_t process) 2257 { 2258 struct perf_evsel *attr; 2259 int err = 0; 2260 2261 list_for_each_entry(attr, &session->evlist->entries, node) { 2262 err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids, 2263 attr->id, process); 2264 if (err) { 2265 pr_debug("failed to create perf header attribute\n"); 2266 return err; 2267 } 2268 } 2269 2270 return err; 2271 } 2272 2273 int perf_event__process_attr(union perf_event *event, 2274 struct perf_evlist **pevlist) 2275 { 2276 unsigned int i, ids, n_ids; 2277 struct perf_evsel *evsel; 2278 struct perf_evlist *evlist = *pevlist; 2279 2280 if (evlist == NULL) { 2281 *pevlist = evlist = perf_evlist__new(NULL, NULL); 2282 if (evlist == NULL) 2283 return -ENOMEM; 2284 } 2285 2286 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries); 2287 if (evsel == NULL) 2288 return -ENOMEM; 2289 2290 perf_evlist__add(evlist, evsel); 2291 2292 ids = event->header.size; 2293 ids -= (void *)&event->attr.id - (void *)event; 2294 n_ids = ids / sizeof(u64); 2295 /* 2296 * We don't have the cpu and thread maps on the header, so 2297 * for allocating the perf_sample_id table we fake 1 cpu and 2298 * hattr->ids threads. 2299 */ 2300 if (perf_evsel__alloc_id(evsel, 1, n_ids)) 2301 return -ENOMEM; 2302 2303 for (i = 0; i < n_ids; i++) { 2304 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); 2305 } 2306 2307 return 0; 2308 } 2309 2310 int perf_event__synthesize_event_type(struct perf_tool *tool, 2311 u64 event_id, char *name, 2312 perf_event__handler_t process, 2313 struct machine *machine) 2314 { 2315 union perf_event ev; 2316 size_t size = 0; 2317 int err = 0; 2318 2319 memset(&ev, 0, sizeof(ev)); 2320 2321 ev.event_type.event_type.event_id = event_id; 2322 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME); 2323 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1); 2324 2325 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE; 2326 size = strlen(ev.event_type.event_type.name); 2327 size = ALIGN(size, sizeof(u64)); 2328 ev.event_type.header.size = sizeof(ev.event_type) - 2329 (sizeof(ev.event_type.event_type.name) - size); 2330 2331 err = process(tool, &ev, NULL, machine); 2332 2333 return err; 2334 } 2335 2336 int perf_event__synthesize_event_types(struct perf_tool *tool, 2337 perf_event__handler_t process, 2338 struct machine *machine) 2339 { 2340 struct perf_trace_event_type *type; 2341 int i, err = 0; 2342 2343 for (i = 0; i < event_count; i++) { 2344 type = &events[i]; 2345 2346 err = perf_event__synthesize_event_type(tool, type->event_id, 2347 type->name, process, 2348 machine); 2349 if (err) { 2350 pr_debug("failed to create perf header event type\n"); 2351 return err; 2352 } 2353 } 2354 2355 return err; 2356 } 2357 2358 int perf_event__process_event_type(struct perf_tool *tool __unused, 2359 union perf_event *event) 2360 { 2361 if (perf_header__push_event(event->event_type.event_type.event_id, 2362 event->event_type.event_type.name) < 0) 2363 return -ENOMEM; 2364 2365 return 0; 2366 } 2367 2368 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, 2369 struct perf_evlist *evlist, 2370 perf_event__handler_t process) 2371 { 2372 union perf_event ev; 2373 struct tracing_data *tdata; 2374 ssize_t size = 0, aligned_size = 0, padding; 2375 int err __used = 0; 2376 2377 /* 2378 * We are going to store the size of the data followed 2379 * by the data contents. Since the fd descriptor is a pipe, 2380 * we cannot seek back to store the size of the data once 2381 * we know it. Instead we: 2382 * 2383 * - write the tracing data to the temp file 2384 * - get/write the data size to pipe 2385 * - write the tracing data from the temp file 2386 * to the pipe 2387 */ 2388 tdata = tracing_data_get(&evlist->entries, fd, true); 2389 if (!tdata) 2390 return -1; 2391 2392 memset(&ev, 0, sizeof(ev)); 2393 2394 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; 2395 size = tdata->size; 2396 aligned_size = ALIGN(size, sizeof(u64)); 2397 padding = aligned_size - size; 2398 ev.tracing_data.header.size = sizeof(ev.tracing_data); 2399 ev.tracing_data.size = aligned_size; 2400 2401 process(tool, &ev, NULL, NULL); 2402 2403 /* 2404 * The put function will copy all the tracing data 2405 * stored in temp file to the pipe. 2406 */ 2407 tracing_data_put(tdata); 2408 2409 write_padded(fd, NULL, 0, padding); 2410 2411 return aligned_size; 2412 } 2413 2414 int perf_event__process_tracing_data(union perf_event *event, 2415 struct perf_session *session) 2416 { 2417 ssize_t size_read, padding, size = event->tracing_data.size; 2418 off_t offset = lseek(session->fd, 0, SEEK_CUR); 2419 char buf[BUFSIZ]; 2420 2421 /* setup for reading amidst mmap */ 2422 lseek(session->fd, offset + sizeof(struct tracing_data_event), 2423 SEEK_SET); 2424 2425 size_read = trace_report(session->fd, &session->pevent, 2426 session->repipe); 2427 padding = ALIGN(size_read, sizeof(u64)) - size_read; 2428 2429 if (read(session->fd, buf, padding) < 0) 2430 die("reading input file"); 2431 if (session->repipe) { 2432 int retw = write(STDOUT_FILENO, buf, padding); 2433 if (retw <= 0 || retw != padding) 2434 die("repiping tracing data padding"); 2435 } 2436 2437 if (size_read + padding != size) 2438 die("tracing data size mismatch"); 2439 2440 return size_read + padding; 2441 } 2442 2443 int perf_event__synthesize_build_id(struct perf_tool *tool, 2444 struct dso *pos, u16 misc, 2445 perf_event__handler_t process, 2446 struct machine *machine) 2447 { 2448 union perf_event ev; 2449 size_t len; 2450 int err = 0; 2451 2452 if (!pos->hit) 2453 return err; 2454 2455 memset(&ev, 0, sizeof(ev)); 2456 2457 len = pos->long_name_len + 1; 2458 len = ALIGN(len, NAME_ALIGN); 2459 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); 2460 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; 2461 ev.build_id.header.misc = misc; 2462 ev.build_id.pid = machine->pid; 2463 ev.build_id.header.size = sizeof(ev.build_id) + len; 2464 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); 2465 2466 err = process(tool, &ev, NULL, machine); 2467 2468 return err; 2469 } 2470 2471 int perf_event__process_build_id(struct perf_tool *tool __used, 2472 union perf_event *event, 2473 struct perf_session *session) 2474 { 2475 __event_process_build_id(&event->build_id, 2476 event->build_id.filename, 2477 session); 2478 return 0; 2479 } 2480 2481 void disable_buildid_cache(void) 2482 { 2483 no_buildid_cache = true; 2484 } 2485