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 fprintf(fp, ", excl_host = %d, excl_guest = %d", 1216 attr.exclude_host, 1217 attr.exclude_guest); 1218 1219 fprintf(fp, ", precise_ip = %d", attr.precise_ip); 1220 1221 if (nr) 1222 fprintf(fp, ", id = {"); 1223 1224 for (j = 0 ; j < nr; j++) { 1225 ret = read(fd, &id, sizeof(id)); 1226 if (ret != (ssize_t)sizeof(id)) 1227 goto error; 1228 1229 if (ph->needs_swap) 1230 id = bswap_64(id); 1231 1232 if (j) 1233 fputc(',', fp); 1234 1235 fprintf(fp, " %"PRIu64, id); 1236 } 1237 if (nr && j == nr) 1238 fprintf(fp, " }"); 1239 fputc('\n', fp); 1240 } 1241 free(buf); 1242 return; 1243 error: 1244 fprintf(fp, "# event desc: not available or unable to read\n"); 1245 } 1246 1247 static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp) 1248 { 1249 uint64_t mem; 1250 ssize_t ret; 1251 1252 ret = read(fd, &mem, sizeof(mem)); 1253 if (ret != sizeof(mem)) 1254 goto error; 1255 1256 if (h->needs_swap) 1257 mem = bswap_64(mem); 1258 1259 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem); 1260 return; 1261 error: 1262 fprintf(fp, "# total memory : unknown\n"); 1263 } 1264 1265 static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp) 1266 { 1267 ssize_t ret; 1268 u32 nr, c, i; 1269 char *str; 1270 uint64_t mem_total, mem_free; 1271 1272 /* nr nodes */ 1273 ret = read(fd, &nr, sizeof(nr)); 1274 if (ret != (ssize_t)sizeof(nr)) 1275 goto error; 1276 1277 if (h->needs_swap) 1278 nr = bswap_32(nr); 1279 1280 for (i = 0; i < nr; i++) { 1281 1282 /* node number */ 1283 ret = read(fd, &c, sizeof(c)); 1284 if (ret != (ssize_t)sizeof(c)) 1285 goto error; 1286 1287 if (h->needs_swap) 1288 c = bswap_32(c); 1289 1290 ret = read(fd, &mem_total, sizeof(u64)); 1291 if (ret != sizeof(u64)) 1292 goto error; 1293 1294 ret = read(fd, &mem_free, sizeof(u64)); 1295 if (ret != sizeof(u64)) 1296 goto error; 1297 1298 if (h->needs_swap) { 1299 mem_total = bswap_64(mem_total); 1300 mem_free = bswap_64(mem_free); 1301 } 1302 1303 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," 1304 " free = %"PRIu64" kB\n", 1305 c, 1306 mem_total, 1307 mem_free); 1308 1309 str = do_read_string(fd, h); 1310 fprintf(fp, "# node%u cpu list : %s\n", c, str); 1311 free(str); 1312 } 1313 return; 1314 error: 1315 fprintf(fp, "# numa topology : not available\n"); 1316 } 1317 1318 static void print_cpuid(struct perf_header *ph, int fd, FILE *fp) 1319 { 1320 char *str = do_read_string(fd, ph); 1321 fprintf(fp, "# cpuid : %s\n", str); 1322 free(str); 1323 } 1324 1325 static void print_branch_stack(struct perf_header *ph __used, int fd __used, 1326 FILE *fp) 1327 { 1328 fprintf(fp, "# contains samples with branch stack\n"); 1329 } 1330 1331 static int __event_process_build_id(struct build_id_event *bev, 1332 char *filename, 1333 struct perf_session *session) 1334 { 1335 int err = -1; 1336 struct list_head *head; 1337 struct machine *machine; 1338 u16 misc; 1339 struct dso *dso; 1340 enum dso_kernel_type dso_type; 1341 1342 machine = perf_session__findnew_machine(session, bev->pid); 1343 if (!machine) 1344 goto out; 1345 1346 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1347 1348 switch (misc) { 1349 case PERF_RECORD_MISC_KERNEL: 1350 dso_type = DSO_TYPE_KERNEL; 1351 head = &machine->kernel_dsos; 1352 break; 1353 case PERF_RECORD_MISC_GUEST_KERNEL: 1354 dso_type = DSO_TYPE_GUEST_KERNEL; 1355 head = &machine->kernel_dsos; 1356 break; 1357 case PERF_RECORD_MISC_USER: 1358 case PERF_RECORD_MISC_GUEST_USER: 1359 dso_type = DSO_TYPE_USER; 1360 head = &machine->user_dsos; 1361 break; 1362 default: 1363 goto out; 1364 } 1365 1366 dso = __dsos__findnew(head, filename); 1367 if (dso != NULL) { 1368 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 1369 1370 dso__set_build_id(dso, &bev->build_id); 1371 1372 if (filename[0] == '[') 1373 dso->kernel = dso_type; 1374 1375 build_id__sprintf(dso->build_id, sizeof(dso->build_id), 1376 sbuild_id); 1377 pr_debug("build id event received for %s: %s\n", 1378 dso->long_name, sbuild_id); 1379 } 1380 1381 err = 0; 1382 out: 1383 return err; 1384 } 1385 1386 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, 1387 int input, u64 offset, u64 size) 1388 { 1389 struct perf_session *session = container_of(header, struct perf_session, header); 1390 struct { 1391 struct perf_event_header header; 1392 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))]; 1393 char filename[0]; 1394 } old_bev; 1395 struct build_id_event bev; 1396 char filename[PATH_MAX]; 1397 u64 limit = offset + size; 1398 1399 while (offset < limit) { 1400 ssize_t len; 1401 1402 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) 1403 return -1; 1404 1405 if (header->needs_swap) 1406 perf_event_header__bswap(&old_bev.header); 1407 1408 len = old_bev.header.size - sizeof(old_bev); 1409 if (read(input, filename, len) != len) 1410 return -1; 1411 1412 bev.header = old_bev.header; 1413 1414 /* 1415 * As the pid is the missing value, we need to fill 1416 * it properly. The header.misc value give us nice hint. 1417 */ 1418 bev.pid = HOST_KERNEL_ID; 1419 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || 1420 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) 1421 bev.pid = DEFAULT_GUEST_KERNEL_ID; 1422 1423 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); 1424 __event_process_build_id(&bev, filename, session); 1425 1426 offset += bev.header.size; 1427 } 1428 1429 return 0; 1430 } 1431 1432 static int perf_header__read_build_ids(struct perf_header *header, 1433 int input, u64 offset, u64 size) 1434 { 1435 struct perf_session *session = container_of(header, struct perf_session, header); 1436 struct build_id_event bev; 1437 char filename[PATH_MAX]; 1438 u64 limit = offset + size, orig_offset = offset; 1439 int err = -1; 1440 1441 while (offset < limit) { 1442 ssize_t len; 1443 1444 if (read(input, &bev, sizeof(bev)) != sizeof(bev)) 1445 goto out; 1446 1447 if (header->needs_swap) 1448 perf_event_header__bswap(&bev.header); 1449 1450 len = bev.header.size - sizeof(bev); 1451 if (read(input, filename, len) != len) 1452 goto out; 1453 /* 1454 * The a1645ce1 changeset: 1455 * 1456 * "perf: 'perf kvm' tool for monitoring guest performance from host" 1457 * 1458 * Added a field to struct build_id_event that broke the file 1459 * format. 1460 * 1461 * Since the kernel build-id is the first entry, process the 1462 * table using the old format if the well known 1463 * '[kernel.kallsyms]' string for the kernel build-id has the 1464 * first 4 characters chopped off (where the pid_t sits). 1465 */ 1466 if (memcmp(filename, "nel.kallsyms]", 13) == 0) { 1467 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) 1468 return -1; 1469 return perf_header__read_build_ids_abi_quirk(header, input, offset, size); 1470 } 1471 1472 __event_process_build_id(&bev, filename, session); 1473 1474 offset += bev.header.size; 1475 } 1476 err = 0; 1477 out: 1478 return err; 1479 } 1480 1481 static int process_tracing_data(struct perf_file_section *section __unused, 1482 struct perf_header *ph __unused, 1483 int feat __unused, int fd, void *data) 1484 { 1485 trace_report(fd, data, false); 1486 return 0; 1487 } 1488 1489 static int process_build_id(struct perf_file_section *section, 1490 struct perf_header *ph, 1491 int feat __unused, int fd, void *data __used) 1492 { 1493 if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) 1494 pr_debug("Failed to read buildids, continuing...\n"); 1495 return 0; 1496 } 1497 1498 struct feature_ops { 1499 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); 1500 void (*print)(struct perf_header *h, int fd, FILE *fp); 1501 int (*process)(struct perf_file_section *section, 1502 struct perf_header *h, int feat, int fd, void *data); 1503 const char *name; 1504 bool full_only; 1505 }; 1506 1507 #define FEAT_OPA(n, func) \ 1508 [n] = { .name = #n, .write = write_##func, .print = print_##func } 1509 #define FEAT_OPP(n, func) \ 1510 [n] = { .name = #n, .write = write_##func, .print = print_##func, \ 1511 .process = process_##func } 1512 #define FEAT_OPF(n, func) \ 1513 [n] = { .name = #n, .write = write_##func, .print = print_##func, \ 1514 .full_only = true } 1515 1516 /* feature_ops not implemented: */ 1517 #define print_tracing_data NULL 1518 #define print_build_id NULL 1519 1520 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { 1521 FEAT_OPP(HEADER_TRACING_DATA, tracing_data), 1522 FEAT_OPP(HEADER_BUILD_ID, build_id), 1523 FEAT_OPA(HEADER_HOSTNAME, hostname), 1524 FEAT_OPA(HEADER_OSRELEASE, osrelease), 1525 FEAT_OPA(HEADER_VERSION, version), 1526 FEAT_OPA(HEADER_ARCH, arch), 1527 FEAT_OPA(HEADER_NRCPUS, nrcpus), 1528 FEAT_OPA(HEADER_CPUDESC, cpudesc), 1529 FEAT_OPA(HEADER_CPUID, cpuid), 1530 FEAT_OPA(HEADER_TOTAL_MEM, total_mem), 1531 FEAT_OPA(HEADER_EVENT_DESC, event_desc), 1532 FEAT_OPA(HEADER_CMDLINE, cmdline), 1533 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), 1534 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), 1535 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack), 1536 }; 1537 1538 struct header_print_data { 1539 FILE *fp; 1540 bool full; /* extended list of headers */ 1541 }; 1542 1543 static int perf_file_section__fprintf_info(struct perf_file_section *section, 1544 struct perf_header *ph, 1545 int feat, int fd, void *data) 1546 { 1547 struct header_print_data *hd = data; 1548 1549 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 1550 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 1551 "%d, continuing...\n", section->offset, feat); 1552 return 0; 1553 } 1554 if (feat >= HEADER_LAST_FEATURE) { 1555 pr_warning("unknown feature %d\n", feat); 1556 return 0; 1557 } 1558 if (!feat_ops[feat].print) 1559 return 0; 1560 1561 if (!feat_ops[feat].full_only || hd->full) 1562 feat_ops[feat].print(ph, fd, hd->fp); 1563 else 1564 fprintf(hd->fp, "# %s info available, use -I to display\n", 1565 feat_ops[feat].name); 1566 1567 return 0; 1568 } 1569 1570 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) 1571 { 1572 struct header_print_data hd; 1573 struct perf_header *header = &session->header; 1574 int fd = session->fd; 1575 hd.fp = fp; 1576 hd.full = full; 1577 1578 perf_header__process_sections(header, fd, &hd, 1579 perf_file_section__fprintf_info); 1580 return 0; 1581 } 1582 1583 static int do_write_feat(int fd, struct perf_header *h, int type, 1584 struct perf_file_section **p, 1585 struct perf_evlist *evlist) 1586 { 1587 int err; 1588 int ret = 0; 1589 1590 if (perf_header__has_feat(h, type)) { 1591 if (!feat_ops[type].write) 1592 return -1; 1593 1594 (*p)->offset = lseek(fd, 0, SEEK_CUR); 1595 1596 err = feat_ops[type].write(fd, h, evlist); 1597 if (err < 0) { 1598 pr_debug("failed to write feature %d\n", type); 1599 1600 /* undo anything written */ 1601 lseek(fd, (*p)->offset, SEEK_SET); 1602 1603 return -1; 1604 } 1605 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; 1606 (*p)++; 1607 } 1608 return ret; 1609 } 1610 1611 static int perf_header__adds_write(struct perf_header *header, 1612 struct perf_evlist *evlist, int fd) 1613 { 1614 int nr_sections; 1615 struct perf_file_section *feat_sec, *p; 1616 int sec_size; 1617 u64 sec_start; 1618 int feat; 1619 int err; 1620 1621 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 1622 if (!nr_sections) 1623 return 0; 1624 1625 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections); 1626 if (feat_sec == NULL) 1627 return -ENOMEM; 1628 1629 sec_size = sizeof(*feat_sec) * nr_sections; 1630 1631 sec_start = header->data_offset + header->data_size; 1632 lseek(fd, sec_start + sec_size, SEEK_SET); 1633 1634 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 1635 if (do_write_feat(fd, header, feat, &p, evlist)) 1636 perf_header__clear_feat(header, feat); 1637 } 1638 1639 lseek(fd, sec_start, SEEK_SET); 1640 /* 1641 * may write more than needed due to dropped feature, but 1642 * this is okay, reader will skip the mising entries 1643 */ 1644 err = do_write(fd, feat_sec, sec_size); 1645 if (err < 0) 1646 pr_debug("failed to write feature section\n"); 1647 free(feat_sec); 1648 return err; 1649 } 1650 1651 int perf_header__write_pipe(int fd) 1652 { 1653 struct perf_pipe_file_header f_header; 1654 int err; 1655 1656 f_header = (struct perf_pipe_file_header){ 1657 .magic = PERF_MAGIC, 1658 .size = sizeof(f_header), 1659 }; 1660 1661 err = do_write(fd, &f_header, sizeof(f_header)); 1662 if (err < 0) { 1663 pr_debug("failed to write perf pipe header\n"); 1664 return err; 1665 } 1666 1667 return 0; 1668 } 1669 1670 int perf_session__write_header(struct perf_session *session, 1671 struct perf_evlist *evlist, 1672 int fd, bool at_exit) 1673 { 1674 struct perf_file_header f_header; 1675 struct perf_file_attr f_attr; 1676 struct perf_header *header = &session->header; 1677 struct perf_evsel *attr, *pair = NULL; 1678 int err; 1679 1680 lseek(fd, sizeof(f_header), SEEK_SET); 1681 1682 if (session->evlist != evlist) 1683 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node); 1684 1685 list_for_each_entry(attr, &evlist->entries, node) { 1686 attr->id_offset = lseek(fd, 0, SEEK_CUR); 1687 err = do_write(fd, attr->id, attr->ids * sizeof(u64)); 1688 if (err < 0) { 1689 out_err_write: 1690 pr_debug("failed to write perf header\n"); 1691 return err; 1692 } 1693 if (session->evlist != evlist) { 1694 err = do_write(fd, pair->id, pair->ids * sizeof(u64)); 1695 if (err < 0) 1696 goto out_err_write; 1697 attr->ids += pair->ids; 1698 pair = list_entry(pair->node.next, struct perf_evsel, node); 1699 } 1700 } 1701 1702 header->attr_offset = lseek(fd, 0, SEEK_CUR); 1703 1704 list_for_each_entry(attr, &evlist->entries, node) { 1705 f_attr = (struct perf_file_attr){ 1706 .attr = attr->attr, 1707 .ids = { 1708 .offset = attr->id_offset, 1709 .size = attr->ids * sizeof(u64), 1710 } 1711 }; 1712 err = do_write(fd, &f_attr, sizeof(f_attr)); 1713 if (err < 0) { 1714 pr_debug("failed to write perf header attribute\n"); 1715 return err; 1716 } 1717 } 1718 1719 header->event_offset = lseek(fd, 0, SEEK_CUR); 1720 header->event_size = event_count * sizeof(struct perf_trace_event_type); 1721 if (events) { 1722 err = do_write(fd, events, header->event_size); 1723 if (err < 0) { 1724 pr_debug("failed to write perf header events\n"); 1725 return err; 1726 } 1727 } 1728 1729 header->data_offset = lseek(fd, 0, SEEK_CUR); 1730 1731 if (at_exit) { 1732 err = perf_header__adds_write(header, evlist, fd); 1733 if (err < 0) 1734 return err; 1735 } 1736 1737 f_header = (struct perf_file_header){ 1738 .magic = PERF_MAGIC, 1739 .size = sizeof(f_header), 1740 .attr_size = sizeof(f_attr), 1741 .attrs = { 1742 .offset = header->attr_offset, 1743 .size = evlist->nr_entries * sizeof(f_attr), 1744 }, 1745 .data = { 1746 .offset = header->data_offset, 1747 .size = header->data_size, 1748 }, 1749 .event_types = { 1750 .offset = header->event_offset, 1751 .size = header->event_size, 1752 }, 1753 }; 1754 1755 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); 1756 1757 lseek(fd, 0, SEEK_SET); 1758 err = do_write(fd, &f_header, sizeof(f_header)); 1759 if (err < 0) { 1760 pr_debug("failed to write perf header\n"); 1761 return err; 1762 } 1763 lseek(fd, header->data_offset + header->data_size, SEEK_SET); 1764 1765 header->frozen = 1; 1766 return 0; 1767 } 1768 1769 static int perf_header__getbuffer64(struct perf_header *header, 1770 int fd, void *buf, size_t size) 1771 { 1772 if (readn(fd, buf, size) <= 0) 1773 return -1; 1774 1775 if (header->needs_swap) 1776 mem_bswap_64(buf, size); 1777 1778 return 0; 1779 } 1780 1781 int perf_header__process_sections(struct perf_header *header, int fd, 1782 void *data, 1783 int (*process)(struct perf_file_section *section, 1784 struct perf_header *ph, 1785 int feat, int fd, void *data)) 1786 { 1787 struct perf_file_section *feat_sec, *sec; 1788 int nr_sections; 1789 int sec_size; 1790 int feat; 1791 int err; 1792 1793 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 1794 if (!nr_sections) 1795 return 0; 1796 1797 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections); 1798 if (!feat_sec) 1799 return -1; 1800 1801 sec_size = sizeof(*feat_sec) * nr_sections; 1802 1803 lseek(fd, header->data_offset + header->data_size, SEEK_SET); 1804 1805 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); 1806 if (err < 0) 1807 goto out_free; 1808 1809 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { 1810 err = process(sec++, header, feat, fd, data); 1811 if (err < 0) 1812 goto out_free; 1813 } 1814 err = 0; 1815 out_free: 1816 free(feat_sec); 1817 return err; 1818 } 1819 1820 static const int attr_file_abi_sizes[] = { 1821 [0] = PERF_ATTR_SIZE_VER0, 1822 [1] = PERF_ATTR_SIZE_VER1, 1823 0, 1824 }; 1825 1826 /* 1827 * In the legacy file format, the magic number is not used to encode endianness. 1828 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based 1829 * on ABI revisions, we need to try all combinations for all endianness to 1830 * detect the endianness. 1831 */ 1832 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) 1833 { 1834 uint64_t ref_size, attr_size; 1835 int i; 1836 1837 for (i = 0 ; attr_file_abi_sizes[i]; i++) { 1838 ref_size = attr_file_abi_sizes[i] 1839 + sizeof(struct perf_file_section); 1840 if (hdr_sz != ref_size) { 1841 attr_size = bswap_64(hdr_sz); 1842 if (attr_size != ref_size) 1843 continue; 1844 1845 ph->needs_swap = true; 1846 } 1847 pr_debug("ABI%d perf.data file detected, need_swap=%d\n", 1848 i, 1849 ph->needs_swap); 1850 return 0; 1851 } 1852 /* could not determine endianness */ 1853 return -1; 1854 } 1855 1856 #define PERF_PIPE_HDR_VER0 16 1857 1858 static const size_t attr_pipe_abi_sizes[] = { 1859 [0] = PERF_PIPE_HDR_VER0, 1860 0, 1861 }; 1862 1863 /* 1864 * In the legacy pipe format, there is an implicit assumption that endiannesss 1865 * between host recording the samples, and host parsing the samples is the 1866 * same. This is not always the case given that the pipe output may always be 1867 * redirected into a file and analyzed on a different machine with possibly a 1868 * different endianness and perf_event ABI revsions in the perf tool itself. 1869 */ 1870 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) 1871 { 1872 u64 attr_size; 1873 int i; 1874 1875 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { 1876 if (hdr_sz != attr_pipe_abi_sizes[i]) { 1877 attr_size = bswap_64(hdr_sz); 1878 if (attr_size != hdr_sz) 1879 continue; 1880 1881 ph->needs_swap = true; 1882 } 1883 pr_debug("Pipe ABI%d perf.data file detected\n", i); 1884 return 0; 1885 } 1886 return -1; 1887 } 1888 1889 static int check_magic_endian(u64 magic, uint64_t hdr_sz, 1890 bool is_pipe, struct perf_header *ph) 1891 { 1892 int ret; 1893 1894 /* check for legacy format */ 1895 ret = memcmp(&magic, __perf_magic1, sizeof(magic)); 1896 if (ret == 0) { 1897 pr_debug("legacy perf.data format\n"); 1898 if (is_pipe) 1899 return try_all_pipe_abis(hdr_sz, ph); 1900 1901 return try_all_file_abis(hdr_sz, ph); 1902 } 1903 /* 1904 * the new magic number serves two purposes: 1905 * - unique number to identify actual perf.data files 1906 * - encode endianness of file 1907 */ 1908 1909 /* check magic number with one endianness */ 1910 if (magic == __perf_magic2) 1911 return 0; 1912 1913 /* check magic number with opposite endianness */ 1914 if (magic != __perf_magic2_sw) 1915 return -1; 1916 1917 ph->needs_swap = true; 1918 1919 return 0; 1920 } 1921 1922 int perf_file_header__read(struct perf_file_header *header, 1923 struct perf_header *ph, int fd) 1924 { 1925 int ret; 1926 1927 lseek(fd, 0, SEEK_SET); 1928 1929 ret = readn(fd, header, sizeof(*header)); 1930 if (ret <= 0) 1931 return -1; 1932 1933 if (check_magic_endian(header->magic, 1934 header->attr_size, false, ph) < 0) { 1935 pr_debug("magic/endian check failed\n"); 1936 return -1; 1937 } 1938 1939 if (ph->needs_swap) { 1940 mem_bswap_64(header, offsetof(struct perf_file_header, 1941 adds_features)); 1942 } 1943 1944 if (header->size != sizeof(*header)) { 1945 /* Support the previous format */ 1946 if (header->size == offsetof(typeof(*header), adds_features)) 1947 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 1948 else 1949 return -1; 1950 } else if (ph->needs_swap) { 1951 /* 1952 * feature bitmap is declared as an array of unsigned longs -- 1953 * not good since its size can differ between the host that 1954 * generated the data file and the host analyzing the file. 1955 * 1956 * We need to handle endianness, but we don't know the size of 1957 * the unsigned long where the file was generated. Take a best 1958 * guess at determining it: try 64-bit swap first (ie., file 1959 * created on a 64-bit host), and check if the hostname feature 1960 * bit is set (this feature bit is forced on as of fbe96f2). 1961 * If the bit is not, undo the 64-bit swap and try a 32-bit 1962 * swap. If the hostname bit is still not set (e.g., older data 1963 * file), punt and fallback to the original behavior -- 1964 * clearing all feature bits and setting buildid. 1965 */ 1966 mem_bswap_64(&header->adds_features, 1967 BITS_TO_U64(HEADER_FEAT_BITS)); 1968 1969 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 1970 /* unswap as u64 */ 1971 mem_bswap_64(&header->adds_features, 1972 BITS_TO_U64(HEADER_FEAT_BITS)); 1973 1974 /* unswap as u32 */ 1975 mem_bswap_32(&header->adds_features, 1976 BITS_TO_U32(HEADER_FEAT_BITS)); 1977 } 1978 1979 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 1980 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 1981 set_bit(HEADER_BUILD_ID, header->adds_features); 1982 } 1983 } 1984 1985 memcpy(&ph->adds_features, &header->adds_features, 1986 sizeof(ph->adds_features)); 1987 1988 ph->event_offset = header->event_types.offset; 1989 ph->event_size = header->event_types.size; 1990 ph->data_offset = header->data.offset; 1991 ph->data_size = header->data.size; 1992 return 0; 1993 } 1994 1995 static int perf_file_section__process(struct perf_file_section *section, 1996 struct perf_header *ph, 1997 int feat, int fd, void *data) 1998 { 1999 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 2000 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 2001 "%d, continuing...\n", section->offset, feat); 2002 return 0; 2003 } 2004 2005 if (feat >= HEADER_LAST_FEATURE) { 2006 pr_debug("unknown feature %d, continuing...\n", feat); 2007 return 0; 2008 } 2009 2010 if (!feat_ops[feat].process) 2011 return 0; 2012 2013 return feat_ops[feat].process(section, ph, feat, fd, data); 2014 } 2015 2016 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, 2017 struct perf_header *ph, int fd, 2018 bool repipe) 2019 { 2020 int ret; 2021 2022 ret = readn(fd, header, sizeof(*header)); 2023 if (ret <= 0) 2024 return -1; 2025 2026 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { 2027 pr_debug("endian/magic failed\n"); 2028 return -1; 2029 } 2030 2031 if (ph->needs_swap) 2032 header->size = bswap_64(header->size); 2033 2034 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) 2035 return -1; 2036 2037 return 0; 2038 } 2039 2040 static int perf_header__read_pipe(struct perf_session *session, int fd) 2041 { 2042 struct perf_header *header = &session->header; 2043 struct perf_pipe_file_header f_header; 2044 2045 if (perf_file_header__read_pipe(&f_header, header, fd, 2046 session->repipe) < 0) { 2047 pr_debug("incompatible file format\n"); 2048 return -EINVAL; 2049 } 2050 2051 session->fd = fd; 2052 2053 return 0; 2054 } 2055 2056 static int read_attr(int fd, struct perf_header *ph, 2057 struct perf_file_attr *f_attr) 2058 { 2059 struct perf_event_attr *attr = &f_attr->attr; 2060 size_t sz, left; 2061 size_t our_sz = sizeof(f_attr->attr); 2062 int ret; 2063 2064 memset(f_attr, 0, sizeof(*f_attr)); 2065 2066 /* read minimal guaranteed structure */ 2067 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); 2068 if (ret <= 0) { 2069 pr_debug("cannot read %d bytes of header attr\n", 2070 PERF_ATTR_SIZE_VER0); 2071 return -1; 2072 } 2073 2074 /* on file perf_event_attr size */ 2075 sz = attr->size; 2076 2077 if (ph->needs_swap) 2078 sz = bswap_32(sz); 2079 2080 if (sz == 0) { 2081 /* assume ABI0 */ 2082 sz = PERF_ATTR_SIZE_VER0; 2083 } else if (sz > our_sz) { 2084 pr_debug("file uses a more recent and unsupported ABI" 2085 " (%zu bytes extra)\n", sz - our_sz); 2086 return -1; 2087 } 2088 /* what we have not yet read and that we know about */ 2089 left = sz - PERF_ATTR_SIZE_VER0; 2090 if (left) { 2091 void *ptr = attr; 2092 ptr += PERF_ATTR_SIZE_VER0; 2093 2094 ret = readn(fd, ptr, left); 2095 } 2096 /* read perf_file_section, ids are read in caller */ 2097 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); 2098 2099 return ret <= 0 ? -1 : 0; 2100 } 2101 2102 static int perf_evsel__set_tracepoint_name(struct perf_evsel *evsel, 2103 struct pevent *pevent) 2104 { 2105 struct event_format *event = pevent_find_event(pevent, 2106 evsel->attr.config); 2107 char bf[128]; 2108 2109 if (event == NULL) 2110 return -1; 2111 2112 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); 2113 evsel->name = strdup(bf); 2114 if (event->name == NULL) 2115 return -1; 2116 2117 return 0; 2118 } 2119 2120 static int perf_evlist__set_tracepoint_names(struct perf_evlist *evlist, 2121 struct pevent *pevent) 2122 { 2123 struct perf_evsel *pos; 2124 2125 list_for_each_entry(pos, &evlist->entries, node) { 2126 if (pos->attr.type == PERF_TYPE_TRACEPOINT && 2127 perf_evsel__set_tracepoint_name(pos, pevent)) 2128 return -1; 2129 } 2130 2131 return 0; 2132 } 2133 2134 int perf_session__read_header(struct perf_session *session, int fd) 2135 { 2136 struct perf_header *header = &session->header; 2137 struct perf_file_header f_header; 2138 struct perf_file_attr f_attr; 2139 u64 f_id; 2140 int nr_attrs, nr_ids, i, j; 2141 2142 session->evlist = perf_evlist__new(NULL, NULL); 2143 if (session->evlist == NULL) 2144 return -ENOMEM; 2145 2146 if (session->fd_pipe) 2147 return perf_header__read_pipe(session, fd); 2148 2149 if (perf_file_header__read(&f_header, header, fd) < 0) 2150 return -EINVAL; 2151 2152 nr_attrs = f_header.attrs.size / f_header.attr_size; 2153 lseek(fd, f_header.attrs.offset, SEEK_SET); 2154 2155 for (i = 0; i < nr_attrs; i++) { 2156 struct perf_evsel *evsel; 2157 off_t tmp; 2158 2159 if (read_attr(fd, header, &f_attr) < 0) 2160 goto out_errno; 2161 2162 if (header->needs_swap) 2163 perf_event__attr_swap(&f_attr.attr); 2164 2165 tmp = lseek(fd, 0, SEEK_CUR); 2166 evsel = perf_evsel__new(&f_attr.attr, i); 2167 2168 if (evsel == NULL) 2169 goto out_delete_evlist; 2170 /* 2171 * Do it before so that if perf_evsel__alloc_id fails, this 2172 * entry gets purged too at perf_evlist__delete(). 2173 */ 2174 perf_evlist__add(session->evlist, evsel); 2175 2176 nr_ids = f_attr.ids.size / sizeof(u64); 2177 /* 2178 * We don't have the cpu and thread maps on the header, so 2179 * for allocating the perf_sample_id table we fake 1 cpu and 2180 * hattr->ids threads. 2181 */ 2182 if (perf_evsel__alloc_id(evsel, 1, nr_ids)) 2183 goto out_delete_evlist; 2184 2185 lseek(fd, f_attr.ids.offset, SEEK_SET); 2186 2187 for (j = 0; j < nr_ids; j++) { 2188 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) 2189 goto out_errno; 2190 2191 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); 2192 } 2193 2194 lseek(fd, tmp, SEEK_SET); 2195 } 2196 2197 symbol_conf.nr_events = nr_attrs; 2198 2199 if (f_header.event_types.size) { 2200 lseek(fd, f_header.event_types.offset, SEEK_SET); 2201 events = malloc(f_header.event_types.size); 2202 if (events == NULL) 2203 return -ENOMEM; 2204 if (perf_header__getbuffer64(header, fd, events, 2205 f_header.event_types.size)) 2206 goto out_errno; 2207 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); 2208 } 2209 2210 perf_header__process_sections(header, fd, &session->pevent, 2211 perf_file_section__process); 2212 2213 lseek(fd, header->data_offset, SEEK_SET); 2214 2215 if (perf_evlist__set_tracepoint_names(session->evlist, session->pevent)) 2216 goto out_delete_evlist; 2217 2218 header->frozen = 1; 2219 return 0; 2220 out_errno: 2221 return -errno; 2222 2223 out_delete_evlist: 2224 perf_evlist__delete(session->evlist); 2225 session->evlist = NULL; 2226 return -ENOMEM; 2227 } 2228 2229 int perf_event__synthesize_attr(struct perf_tool *tool, 2230 struct perf_event_attr *attr, u16 ids, u64 *id, 2231 perf_event__handler_t process) 2232 { 2233 union perf_event *ev; 2234 size_t size; 2235 int err; 2236 2237 size = sizeof(struct perf_event_attr); 2238 size = ALIGN(size, sizeof(u64)); 2239 size += sizeof(struct perf_event_header); 2240 size += ids * sizeof(u64); 2241 2242 ev = malloc(size); 2243 2244 if (ev == NULL) 2245 return -ENOMEM; 2246 2247 ev->attr.attr = *attr; 2248 memcpy(ev->attr.id, id, ids * sizeof(u64)); 2249 2250 ev->attr.header.type = PERF_RECORD_HEADER_ATTR; 2251 ev->attr.header.size = size; 2252 2253 err = process(tool, ev, NULL, NULL); 2254 2255 free(ev); 2256 2257 return err; 2258 } 2259 2260 int perf_event__synthesize_attrs(struct perf_tool *tool, 2261 struct perf_session *session, 2262 perf_event__handler_t process) 2263 { 2264 struct perf_evsel *attr; 2265 int err = 0; 2266 2267 list_for_each_entry(attr, &session->evlist->entries, node) { 2268 err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids, 2269 attr->id, process); 2270 if (err) { 2271 pr_debug("failed to create perf header attribute\n"); 2272 return err; 2273 } 2274 } 2275 2276 return err; 2277 } 2278 2279 int perf_event__process_attr(union perf_event *event, 2280 struct perf_evlist **pevlist) 2281 { 2282 unsigned int i, ids, n_ids; 2283 struct perf_evsel *evsel; 2284 struct perf_evlist *evlist = *pevlist; 2285 2286 if (evlist == NULL) { 2287 *pevlist = evlist = perf_evlist__new(NULL, NULL); 2288 if (evlist == NULL) 2289 return -ENOMEM; 2290 } 2291 2292 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries); 2293 if (evsel == NULL) 2294 return -ENOMEM; 2295 2296 perf_evlist__add(evlist, evsel); 2297 2298 ids = event->header.size; 2299 ids -= (void *)&event->attr.id - (void *)event; 2300 n_ids = ids / sizeof(u64); 2301 /* 2302 * We don't have the cpu and thread maps on the header, so 2303 * for allocating the perf_sample_id table we fake 1 cpu and 2304 * hattr->ids threads. 2305 */ 2306 if (perf_evsel__alloc_id(evsel, 1, n_ids)) 2307 return -ENOMEM; 2308 2309 for (i = 0; i < n_ids; i++) { 2310 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); 2311 } 2312 2313 return 0; 2314 } 2315 2316 int perf_event__synthesize_event_type(struct perf_tool *tool, 2317 u64 event_id, char *name, 2318 perf_event__handler_t process, 2319 struct machine *machine) 2320 { 2321 union perf_event ev; 2322 size_t size = 0; 2323 int err = 0; 2324 2325 memset(&ev, 0, sizeof(ev)); 2326 2327 ev.event_type.event_type.event_id = event_id; 2328 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME); 2329 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1); 2330 2331 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE; 2332 size = strlen(ev.event_type.event_type.name); 2333 size = ALIGN(size, sizeof(u64)); 2334 ev.event_type.header.size = sizeof(ev.event_type) - 2335 (sizeof(ev.event_type.event_type.name) - size); 2336 2337 err = process(tool, &ev, NULL, machine); 2338 2339 return err; 2340 } 2341 2342 int perf_event__synthesize_event_types(struct perf_tool *tool, 2343 perf_event__handler_t process, 2344 struct machine *machine) 2345 { 2346 struct perf_trace_event_type *type; 2347 int i, err = 0; 2348 2349 for (i = 0; i < event_count; i++) { 2350 type = &events[i]; 2351 2352 err = perf_event__synthesize_event_type(tool, type->event_id, 2353 type->name, process, 2354 machine); 2355 if (err) { 2356 pr_debug("failed to create perf header event type\n"); 2357 return err; 2358 } 2359 } 2360 2361 return err; 2362 } 2363 2364 int perf_event__process_event_type(struct perf_tool *tool __unused, 2365 union perf_event *event) 2366 { 2367 if (perf_header__push_event(event->event_type.event_type.event_id, 2368 event->event_type.event_type.name) < 0) 2369 return -ENOMEM; 2370 2371 return 0; 2372 } 2373 2374 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, 2375 struct perf_evlist *evlist, 2376 perf_event__handler_t process) 2377 { 2378 union perf_event ev; 2379 struct tracing_data *tdata; 2380 ssize_t size = 0, aligned_size = 0, padding; 2381 int err __used = 0; 2382 2383 /* 2384 * We are going to store the size of the data followed 2385 * by the data contents. Since the fd descriptor is a pipe, 2386 * we cannot seek back to store the size of the data once 2387 * we know it. Instead we: 2388 * 2389 * - write the tracing data to the temp file 2390 * - get/write the data size to pipe 2391 * - write the tracing data from the temp file 2392 * to the pipe 2393 */ 2394 tdata = tracing_data_get(&evlist->entries, fd, true); 2395 if (!tdata) 2396 return -1; 2397 2398 memset(&ev, 0, sizeof(ev)); 2399 2400 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; 2401 size = tdata->size; 2402 aligned_size = ALIGN(size, sizeof(u64)); 2403 padding = aligned_size - size; 2404 ev.tracing_data.header.size = sizeof(ev.tracing_data); 2405 ev.tracing_data.size = aligned_size; 2406 2407 process(tool, &ev, NULL, NULL); 2408 2409 /* 2410 * The put function will copy all the tracing data 2411 * stored in temp file to the pipe. 2412 */ 2413 tracing_data_put(tdata); 2414 2415 write_padded(fd, NULL, 0, padding); 2416 2417 return aligned_size; 2418 } 2419 2420 int perf_event__process_tracing_data(union perf_event *event, 2421 struct perf_session *session) 2422 { 2423 ssize_t size_read, padding, size = event->tracing_data.size; 2424 off_t offset = lseek(session->fd, 0, SEEK_CUR); 2425 char buf[BUFSIZ]; 2426 2427 /* setup for reading amidst mmap */ 2428 lseek(session->fd, offset + sizeof(struct tracing_data_event), 2429 SEEK_SET); 2430 2431 size_read = trace_report(session->fd, &session->pevent, 2432 session->repipe); 2433 padding = ALIGN(size_read, sizeof(u64)) - size_read; 2434 2435 if (read(session->fd, buf, padding) < 0) 2436 die("reading input file"); 2437 if (session->repipe) { 2438 int retw = write(STDOUT_FILENO, buf, padding); 2439 if (retw <= 0 || retw != padding) 2440 die("repiping tracing data padding"); 2441 } 2442 2443 if (size_read + padding != size) 2444 die("tracing data size mismatch"); 2445 2446 return size_read + padding; 2447 } 2448 2449 int perf_event__synthesize_build_id(struct perf_tool *tool, 2450 struct dso *pos, u16 misc, 2451 perf_event__handler_t process, 2452 struct machine *machine) 2453 { 2454 union perf_event ev; 2455 size_t len; 2456 int err = 0; 2457 2458 if (!pos->hit) 2459 return err; 2460 2461 memset(&ev, 0, sizeof(ev)); 2462 2463 len = pos->long_name_len + 1; 2464 len = ALIGN(len, NAME_ALIGN); 2465 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); 2466 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; 2467 ev.build_id.header.misc = misc; 2468 ev.build_id.pid = machine->pid; 2469 ev.build_id.header.size = sizeof(ev.build_id) + len; 2470 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); 2471 2472 err = process(tool, &ev, NULL, machine); 2473 2474 return err; 2475 } 2476 2477 int perf_event__process_build_id(struct perf_tool *tool __used, 2478 union perf_event *event, 2479 struct perf_session *session) 2480 { 2481 __event_process_build_id(&event->build_id, 2482 event->build_id.filename, 2483 session); 2484 return 0; 2485 } 2486 2487 void disable_buildid_cache(void) 2488 { 2489 no_buildid_cache = true; 2490 } 2491