1 #define _FILE_OFFSET_BITS 64
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/bitops.h>
12 #include <sys/utsname.h>
18 #include "trace-event.h"
28 static bool no_buildid_cache = false;
30 static int trace_event_count;
31 static struct perf_trace_event_type *trace_events;
33 static u32 header_argc;
34 static const char **header_argv;
36 int perf_header__push_event(u64 id, const char *name)
38 struct perf_trace_event_type *nevents;
40 if (strlen(name) > MAX_EVENT_NAME)
41 pr_warning("Event %s will be truncated\n", name);
43 nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events));
46 trace_events = nevents;
48 memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type));
49 trace_events[trace_event_count].event_id = id;
50 strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1);
55 char *perf_header__find_event(u64 id)
58 for (i = 0 ; i < trace_event_count; i++) {
59 if (trace_events[i].event_id == id)
60 return trace_events[i].name;
67 * must be a numerical value to let the endianness
68 * determine the memory layout. That way we are able
69 * to detect endianness when reading the perf.data file
72 * we check for legacy (PERFFILE) format.
74 static const char *__perf_magic1 = "PERFFILE";
75 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
76 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
78 #define PERF_MAGIC __perf_magic2
80 struct perf_file_attr {
81 struct perf_event_attr attr;
82 struct perf_file_section ids;
85 void perf_header__set_feat(struct perf_header *header, int feat)
87 set_bit(feat, header->adds_features);
90 void perf_header__clear_feat(struct perf_header *header, int feat)
92 clear_bit(feat, header->adds_features);
95 bool perf_header__has_feat(const struct perf_header *header, int feat)
97 return test_bit(feat, header->adds_features);
100 static int do_write(int fd, const void *buf, size_t size)
103 int ret = write(fd, buf, size);
115 #define NAME_ALIGN 64
117 static int write_padded(int fd, const void *bf, size_t count,
118 size_t count_aligned)
120 static const char zero_buf[NAME_ALIGN];
121 int err = do_write(fd, bf, count);
124 err = do_write(fd, zero_buf, count_aligned - count);
129 static int do_write_string(int fd, const char *str)
134 olen = strlen(str) + 1;
135 len = PERF_ALIGN(olen, NAME_ALIGN);
137 /* write len, incl. \0 */
138 ret = do_write(fd, &len, sizeof(len));
142 return write_padded(fd, str, olen, len);
145 static char *do_read_string(int fd, struct perf_header *ph)
151 sz = readn(fd, &len, sizeof(len));
152 if (sz < (ssize_t)sizeof(len))
162 ret = readn(fd, buf, len);
163 if (ret == (ssize_t)len) {
165 * strings are padded by zeroes
166 * thus the actual strlen of buf
167 * may be less than len
177 perf_header__set_cmdline(int argc, const char **argv)
182 * If header_argv has already been set, do not override it.
183 * This allows a command to set the cmdline, parse args and
184 * then call another builtin function that implements a
185 * command -- e.g, cmd_kvm calling cmd_record.
190 header_argc = (u32)argc;
192 /* do not include NULL termination */
193 header_argv = calloc(argc, sizeof(char *));
198 * must copy argv contents because it gets moved
199 * around during option parsing
201 for (i = 0; i < argc ; i++)
202 header_argv[i] = argv[i];
207 #define dsos__for_each_with_build_id(pos, head) \
208 list_for_each_entry(pos, head, node) \
209 if (!pos->has_build_id) \
213 static int write_buildid(char *name, size_t name_len, u8 *build_id,
214 pid_t pid, u16 misc, int fd)
217 struct build_id_event b;
221 len = PERF_ALIGN(len, NAME_ALIGN);
223 memset(&b, 0, sizeof(b));
224 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
226 b.header.misc = misc;
227 b.header.size = sizeof(b) + len;
229 err = do_write(fd, &b, sizeof(b));
233 return write_padded(fd, name, name_len + 1, len);
236 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
241 dsos__for_each_with_build_id(pos, head) {
249 if (is_vdso_map(pos->short_name)) {
250 name = (char *) VDSO__MAP_NAME;
251 name_len = sizeof(VDSO__MAP_NAME) + 1;
253 name = pos->long_name;
254 name_len = pos->long_name_len + 1;
257 err = write_buildid(name, name_len, pos->build_id,
266 static int machine__write_buildid_table(struct machine *machine, int fd)
269 u16 kmisc = PERF_RECORD_MISC_KERNEL,
270 umisc = PERF_RECORD_MISC_USER;
272 if (!machine__is_host(machine)) {
273 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
274 umisc = PERF_RECORD_MISC_GUEST_USER;
277 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
280 err = __dsos__write_buildid_table(&machine->user_dsos,
281 machine->pid, umisc, fd);
285 static int dsos__write_buildid_table(struct perf_header *header, int fd)
287 struct perf_session *session = container_of(header,
288 struct perf_session, header);
290 int err = machine__write_buildid_table(&session->machines.host, fd);
295 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
296 struct machine *pos = rb_entry(nd, struct machine, rb_node);
297 err = machine__write_buildid_table(pos, fd);
304 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
305 const char *name, bool is_kallsyms, bool is_vdso)
307 const size_t size = PATH_MAX;
308 char *realname, *filename = zalloc(size),
309 *linkname = zalloc(size), *targetname;
311 bool slash = is_kallsyms || is_vdso;
314 if (symbol_conf.kptr_restrict) {
315 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
319 realname = (char *) name;
321 realname = realpath(name, NULL);
323 if (realname == NULL || filename == NULL || linkname == NULL)
326 len = scnprintf(filename, size, "%s%s%s",
327 debugdir, slash ? "/" : "",
328 is_vdso ? VDSO__MAP_NAME : realname);
329 if (mkdir_p(filename, 0755))
332 snprintf(filename + len, size - len, "/%s", sbuild_id);
334 if (access(filename, F_OK)) {
336 if (copyfile("/proc/kallsyms", filename))
338 } else if (link(realname, filename) && copyfile(name, filename))
342 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
343 debugdir, sbuild_id);
345 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
348 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
349 targetname = filename + strlen(debugdir) - 5;
350 memcpy(targetname, "../..", 5);
352 if (symlink(targetname, linkname) == 0)
362 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
363 const char *name, const char *debugdir,
364 bool is_kallsyms, bool is_vdso)
366 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
368 build_id__sprintf(build_id, build_id_size, sbuild_id);
370 return build_id_cache__add_s(sbuild_id, debugdir, name,
371 is_kallsyms, is_vdso);
374 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
376 const size_t size = PATH_MAX;
377 char *filename = zalloc(size),
378 *linkname = zalloc(size);
381 if (filename == NULL || linkname == NULL)
384 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
385 debugdir, sbuild_id, sbuild_id + 2);
387 if (access(linkname, F_OK))
390 if (readlink(linkname, filename, size - 1) < 0)
393 if (unlink(linkname))
397 * Since the link is relative, we must make it absolute:
399 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
400 debugdir, sbuild_id, filename);
402 if (unlink(linkname))
412 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
414 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
415 bool is_vdso = is_vdso_map(dso->short_name);
417 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
418 dso->long_name, debugdir,
419 is_kallsyms, is_vdso);
422 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
427 dsos__for_each_with_build_id(pos, head)
428 if (dso__cache_build_id(pos, debugdir))
434 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
436 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
437 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
441 static int perf_session__cache_build_ids(struct perf_session *session)
445 char debugdir[PATH_MAX];
447 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
449 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
452 ret = machine__cache_build_ids(&session->machines.host, debugdir);
454 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
455 struct machine *pos = rb_entry(nd, struct machine, rb_node);
456 ret |= machine__cache_build_ids(pos, debugdir);
461 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
463 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
464 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
468 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
471 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
473 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
474 struct machine *pos = rb_entry(nd, struct machine, rb_node);
475 ret |= machine__read_build_ids(pos, with_hits);
481 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
482 struct perf_evlist *evlist)
484 return read_tracing_data(fd, &evlist->entries);
488 static int write_build_id(int fd, struct perf_header *h,
489 struct perf_evlist *evlist __maybe_unused)
491 struct perf_session *session;
494 session = container_of(h, struct perf_session, header);
496 if (!perf_session__read_build_ids(session, true))
499 err = dsos__write_buildid_table(h, fd);
501 pr_debug("failed to write buildid table\n");
504 if (!no_buildid_cache)
505 perf_session__cache_build_ids(session);
510 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
511 struct perf_evlist *evlist __maybe_unused)
520 return do_write_string(fd, uts.nodename);
523 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
524 struct perf_evlist *evlist __maybe_unused)
533 return do_write_string(fd, uts.release);
536 static int write_arch(int fd, struct perf_header *h __maybe_unused,
537 struct perf_evlist *evlist __maybe_unused)
546 return do_write_string(fd, uts.machine);
549 static int write_version(int fd, struct perf_header *h __maybe_unused,
550 struct perf_evlist *evlist __maybe_unused)
552 return do_write_string(fd, perf_version_string);
555 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
556 struct perf_evlist *evlist __maybe_unused)
559 #define CPUINFO_PROC NULL
564 const char *search = CPUINFO_PROC;
571 file = fopen("/proc/cpuinfo", "r");
575 while (getline(&buf, &len, file) > 0) {
576 ret = strncmp(buf, search, strlen(search));
586 p = strchr(buf, ':');
587 if (p && *(p+1) == ' ' && *(p+2))
593 /* squash extra space characters (branding string) */
600 while (*q && isspace(*q))
603 while ((*r++ = *q++));
607 ret = do_write_string(fd, s);
614 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
615 struct perf_evlist *evlist __maybe_unused)
621 nr = sysconf(_SC_NPROCESSORS_CONF);
625 nrc = (u32)(nr & UINT_MAX);
627 nr = sysconf(_SC_NPROCESSORS_ONLN);
631 nra = (u32)(nr & UINT_MAX);
633 ret = do_write(fd, &nrc, sizeof(nrc));
637 return do_write(fd, &nra, sizeof(nra));
640 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
641 struct perf_evlist *evlist)
643 struct perf_evsel *evsel;
647 nre = evlist->nr_entries;
650 * write number of events
652 ret = do_write(fd, &nre, sizeof(nre));
657 * size of perf_event_attr struct
659 sz = (u32)sizeof(evsel->attr);
660 ret = do_write(fd, &sz, sizeof(sz));
664 list_for_each_entry(evsel, &evlist->entries, node) {
666 ret = do_write(fd, &evsel->attr, sz);
670 * write number of unique id per event
671 * there is one id per instance of an event
673 * copy into an nri to be independent of the
677 ret = do_write(fd, &nri, sizeof(nri));
682 * write event string as passed on cmdline
684 ret = do_write_string(fd, perf_evsel__name(evsel));
688 * write unique ids for this event
690 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
697 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
698 struct perf_evlist *evlist __maybe_unused)
700 char buf[MAXPATHLEN];
706 * actual atual path to perf binary
708 sprintf(proc, "/proc/%d/exe", getpid());
709 ret = readlink(proc, buf, sizeof(buf));
713 /* readlink() does not add null termination */
716 /* account for binary path */
719 ret = do_write(fd, &n, sizeof(n));
723 ret = do_write_string(fd, buf);
727 for (i = 0 ; i < header_argc; i++) {
728 ret = do_write_string(fd, header_argv[i]);
735 #define CORE_SIB_FMT \
736 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
737 #define THRD_SIB_FMT \
738 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
743 char **core_siblings;
744 char **thread_siblings;
747 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
750 char filename[MAXPATHLEN];
751 char *buf = NULL, *p;
756 sprintf(filename, CORE_SIB_FMT, cpu);
757 fp = fopen(filename, "r");
761 if (getline(&buf, &len, fp) <= 0)
766 p = strchr(buf, '\n');
770 for (i = 0; i < tp->core_sib; i++) {
771 if (!strcmp(buf, tp->core_siblings[i]))
774 if (i == tp->core_sib) {
775 tp->core_siblings[i] = buf;
781 sprintf(filename, THRD_SIB_FMT, cpu);
782 fp = fopen(filename, "r");
786 if (getline(&buf, &len, fp) <= 0)
789 p = strchr(buf, '\n');
793 for (i = 0; i < tp->thread_sib; i++) {
794 if (!strcmp(buf, tp->thread_siblings[i]))
797 if (i == tp->thread_sib) {
798 tp->thread_siblings[i] = buf;
810 static void free_cpu_topo(struct cpu_topo *tp)
817 for (i = 0 ; i < tp->core_sib; i++)
818 free(tp->core_siblings[i]);
820 for (i = 0 ; i < tp->thread_sib; i++)
821 free(tp->thread_siblings[i]);
826 static struct cpu_topo *build_cpu_topology(void)
835 ncpus = sysconf(_SC_NPROCESSORS_CONF);
839 nr = (u32)(ncpus & UINT_MAX);
841 sz = nr * sizeof(char *);
843 addr = calloc(1, sizeof(*tp) + 2 * sz);
850 tp->core_siblings = addr;
852 tp->thread_siblings = addr;
854 for (i = 0; i < nr; i++) {
855 ret = build_cpu_topo(tp, i);
866 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
867 struct perf_evlist *evlist __maybe_unused)
873 tp = build_cpu_topology();
877 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
881 for (i = 0; i < tp->core_sib; i++) {
882 ret = do_write_string(fd, tp->core_siblings[i]);
886 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
890 for (i = 0; i < tp->thread_sib; i++) {
891 ret = do_write_string(fd, tp->thread_siblings[i]);
902 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
903 struct perf_evlist *evlist __maybe_unused)
911 fp = fopen("/proc/meminfo", "r");
915 while (getline(&buf, &len, fp) > 0) {
916 ret = strncmp(buf, "MemTotal:", 9);
921 n = sscanf(buf, "%*s %"PRIu64, &mem);
923 ret = do_write(fd, &mem, sizeof(mem));
930 static int write_topo_node(int fd, int node)
932 char str[MAXPATHLEN];
934 char *buf = NULL, *p;
937 u64 mem_total, mem_free, mem;
940 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
941 fp = fopen(str, "r");
945 while (getline(&buf, &len, fp) > 0) {
946 /* skip over invalid lines */
947 if (!strchr(buf, ':'))
949 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
951 if (!strcmp(field, "MemTotal:"))
953 if (!strcmp(field, "MemFree:"))
959 ret = do_write(fd, &mem_total, sizeof(u64));
963 ret = do_write(fd, &mem_free, sizeof(u64));
968 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
970 fp = fopen(str, "r");
974 if (getline(&buf, &len, fp) <= 0)
977 p = strchr(buf, '\n');
981 ret = do_write_string(fd, buf);
988 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
989 struct perf_evlist *evlist __maybe_unused)
994 struct cpu_map *node_map = NULL;
999 fp = fopen("/sys/devices/system/node/online", "r");
1003 if (getline(&buf, &len, fp) <= 0)
1006 c = strchr(buf, '\n');
1010 node_map = cpu_map__new(buf);
1014 nr = (u32)node_map->nr;
1016 ret = do_write(fd, &nr, sizeof(nr));
1020 for (i = 0; i < nr; i++) {
1021 j = (u32)node_map->map[i];
1022 ret = do_write(fd, &j, sizeof(j));
1026 ret = write_topo_node(fd, i);
1040 * struct pmu_mappings {
1049 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1050 struct perf_evlist *evlist __maybe_unused)
1052 struct perf_pmu *pmu = NULL;
1053 off_t offset = lseek(fd, 0, SEEK_CUR);
1057 /* write real pmu_num later */
1058 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1062 while ((pmu = perf_pmu__scan(pmu))) {
1067 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1071 ret = do_write_string(fd, pmu->name);
1076 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1078 lseek(fd, offset, SEEK_SET);
1086 * default get_cpuid(): nothing gets recorded
1087 * actual implementation must be in arch/$(ARCH)/util/header.c
1089 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1090 size_t sz __maybe_unused)
1095 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1096 struct perf_evlist *evlist __maybe_unused)
1101 ret = get_cpuid(buffer, sizeof(buffer));
1107 return do_write_string(fd, buffer);
1110 static int write_branch_stack(int fd __maybe_unused,
1111 struct perf_header *h __maybe_unused,
1112 struct perf_evlist *evlist __maybe_unused)
1117 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1120 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1123 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1126 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1129 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1131 fprintf(fp, "# arch : %s\n", ph->env.arch);
1134 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1137 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1140 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1143 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1144 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1147 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1150 fprintf(fp, "# perf version : %s\n", ph->env.version);
1153 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1159 nr = ph->env.nr_cmdline;
1160 str = ph->env.cmdline;
1162 fprintf(fp, "# cmdline : ");
1164 for (i = 0; i < nr; i++) {
1165 fprintf(fp, "%s ", str);
1166 str += strlen(str) + 1;
1171 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1177 nr = ph->env.nr_sibling_cores;
1178 str = ph->env.sibling_cores;
1180 for (i = 0; i < nr; i++) {
1181 fprintf(fp, "# sibling cores : %s\n", str);
1182 str += strlen(str) + 1;
1185 nr = ph->env.nr_sibling_threads;
1186 str = ph->env.sibling_threads;
1188 for (i = 0; i < nr; i++) {
1189 fprintf(fp, "# sibling threads : %s\n", str);
1190 str += strlen(str) + 1;
1194 static void free_event_desc(struct perf_evsel *events)
1196 struct perf_evsel *evsel;
1201 for (evsel = events; evsel->attr.size; evsel++) {
1211 static struct perf_evsel *
1212 read_event_desc(struct perf_header *ph, int fd)
1214 struct perf_evsel *evsel, *events = NULL;
1217 u32 nre, sz, nr, i, j;
1221 /* number of events */
1222 ret = readn(fd, &nre, sizeof(nre));
1223 if (ret != (ssize_t)sizeof(nre))
1227 nre = bswap_32(nre);
1229 ret = readn(fd, &sz, sizeof(sz));
1230 if (ret != (ssize_t)sizeof(sz))
1236 /* buffer to hold on file attr struct */
1241 /* the last event terminates with evsel->attr.size == 0: */
1242 events = calloc(nre + 1, sizeof(*events));
1246 msz = sizeof(evsel->attr);
1250 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1254 * must read entire on-file attr struct to
1255 * sync up with layout.
1257 ret = readn(fd, buf, sz);
1258 if (ret != (ssize_t)sz)
1262 perf_event__attr_swap(buf);
1264 memcpy(&evsel->attr, buf, msz);
1266 ret = readn(fd, &nr, sizeof(nr));
1267 if (ret != (ssize_t)sizeof(nr))
1270 if (ph->needs_swap) {
1272 evsel->needs_swap = true;
1275 evsel->name = do_read_string(fd, ph);
1280 id = calloc(nr, sizeof(*id));
1286 for (j = 0 ; j < nr; j++) {
1287 ret = readn(fd, id, sizeof(*id));
1288 if (ret != (ssize_t)sizeof(*id))
1291 *id = bswap_64(*id);
1301 free_event_desc(events);
1306 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1308 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1313 fprintf(fp, "# event desc: not available or unable to read\n");
1317 for (evsel = events; evsel->attr.size; evsel++) {
1318 fprintf(fp, "# event : name = %s, ", evsel->name);
1320 fprintf(fp, "type = %d, config = 0x%"PRIx64
1321 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1323 (u64)evsel->attr.config,
1324 (u64)evsel->attr.config1,
1325 (u64)evsel->attr.config2);
1327 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1328 evsel->attr.exclude_user,
1329 evsel->attr.exclude_kernel);
1331 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1332 evsel->attr.exclude_host,
1333 evsel->attr.exclude_guest);
1335 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1338 fprintf(fp, ", id = {");
1339 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1342 fprintf(fp, " %"PRIu64, *id);
1350 free_event_desc(events);
1353 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1356 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1359 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1364 uint64_t mem_total, mem_free;
1367 nr = ph->env.nr_numa_nodes;
1368 str = ph->env.numa_nodes;
1370 for (i = 0; i < nr; i++) {
1372 c = strtoul(str, &tmp, 0);
1377 mem_total = strtoull(str, &tmp, 0);
1382 mem_free = strtoull(str, &tmp, 0);
1386 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1387 " free = %"PRIu64" kB\n",
1388 c, mem_total, mem_free);
1391 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1393 str += strlen(str) + 1;
1397 fprintf(fp, "# numa topology : not available\n");
1400 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1402 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1405 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1406 int fd __maybe_unused, FILE *fp)
1408 fprintf(fp, "# contains samples with branch stack\n");
1411 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1414 const char *delimiter = "# pmu mappings: ";
1419 pmu_num = ph->env.nr_pmu_mappings;
1421 fprintf(fp, "# pmu mappings: not available\n");
1425 str = ph->env.pmu_mappings;
1428 type = strtoul(str, &tmp, 0);
1433 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1436 str += strlen(str) + 1;
1445 fprintf(fp, "# pmu mappings: unable to read\n");
1448 static int __event_process_build_id(struct build_id_event *bev,
1450 struct perf_session *session)
1453 struct list_head *head;
1454 struct machine *machine;
1457 enum dso_kernel_type dso_type;
1459 machine = perf_session__findnew_machine(session, bev->pid);
1463 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1466 case PERF_RECORD_MISC_KERNEL:
1467 dso_type = DSO_TYPE_KERNEL;
1468 head = &machine->kernel_dsos;
1470 case PERF_RECORD_MISC_GUEST_KERNEL:
1471 dso_type = DSO_TYPE_GUEST_KERNEL;
1472 head = &machine->kernel_dsos;
1474 case PERF_RECORD_MISC_USER:
1475 case PERF_RECORD_MISC_GUEST_USER:
1476 dso_type = DSO_TYPE_USER;
1477 head = &machine->user_dsos;
1483 dso = __dsos__findnew(head, filename);
1485 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1487 dso__set_build_id(dso, &bev->build_id);
1489 if (filename[0] == '[')
1490 dso->kernel = dso_type;
1492 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1494 pr_debug("build id event received for %s: %s\n",
1495 dso->long_name, sbuild_id);
1503 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1504 int input, u64 offset, u64 size)
1506 struct perf_session *session = container_of(header, struct perf_session, header);
1508 struct perf_event_header header;
1509 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1512 struct build_id_event bev;
1513 char filename[PATH_MAX];
1514 u64 limit = offset + size;
1516 while (offset < limit) {
1519 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1522 if (header->needs_swap)
1523 perf_event_header__bswap(&old_bev.header);
1525 len = old_bev.header.size - sizeof(old_bev);
1526 if (readn(input, filename, len) != len)
1529 bev.header = old_bev.header;
1532 * As the pid is the missing value, we need to fill
1533 * it properly. The header.misc value give us nice hint.
1535 bev.pid = HOST_KERNEL_ID;
1536 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1537 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1538 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1540 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1541 __event_process_build_id(&bev, filename, session);
1543 offset += bev.header.size;
1549 static int perf_header__read_build_ids(struct perf_header *header,
1550 int input, u64 offset, u64 size)
1552 struct perf_session *session = container_of(header, struct perf_session, header);
1553 struct build_id_event bev;
1554 char filename[PATH_MAX];
1555 u64 limit = offset + size, orig_offset = offset;
1558 while (offset < limit) {
1561 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1564 if (header->needs_swap)
1565 perf_event_header__bswap(&bev.header);
1567 len = bev.header.size - sizeof(bev);
1568 if (readn(input, filename, len) != len)
1571 * The a1645ce1 changeset:
1573 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1575 * Added a field to struct build_id_event that broke the file
1578 * Since the kernel build-id is the first entry, process the
1579 * table using the old format if the well known
1580 * '[kernel.kallsyms]' string for the kernel build-id has the
1581 * first 4 characters chopped off (where the pid_t sits).
1583 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1584 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1586 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1589 __event_process_build_id(&bev, filename, session);
1591 offset += bev.header.size;
1598 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1599 struct perf_header *ph __maybe_unused,
1602 trace_report(fd, data, false);
1606 static int process_build_id(struct perf_file_section *section,
1607 struct perf_header *ph, int fd,
1608 void *data __maybe_unused)
1610 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1611 pr_debug("Failed to read buildids, continuing...\n");
1615 static int process_hostname(struct perf_file_section *section __maybe_unused,
1616 struct perf_header *ph, int fd,
1617 void *data __maybe_unused)
1619 ph->env.hostname = do_read_string(fd, ph);
1620 return ph->env.hostname ? 0 : -ENOMEM;
1623 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1624 struct perf_header *ph, int fd,
1625 void *data __maybe_unused)
1627 ph->env.os_release = do_read_string(fd, ph);
1628 return ph->env.os_release ? 0 : -ENOMEM;
1631 static int process_version(struct perf_file_section *section __maybe_unused,
1632 struct perf_header *ph, int fd,
1633 void *data __maybe_unused)
1635 ph->env.version = do_read_string(fd, ph);
1636 return ph->env.version ? 0 : -ENOMEM;
1639 static int process_arch(struct perf_file_section *section __maybe_unused,
1640 struct perf_header *ph, int fd,
1641 void *data __maybe_unused)
1643 ph->env.arch = do_read_string(fd, ph);
1644 return ph->env.arch ? 0 : -ENOMEM;
1647 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1648 struct perf_header *ph, int fd,
1649 void *data __maybe_unused)
1654 ret = readn(fd, &nr, sizeof(nr));
1655 if (ret != sizeof(nr))
1661 ph->env.nr_cpus_online = nr;
1663 ret = readn(fd, &nr, sizeof(nr));
1664 if (ret != sizeof(nr))
1670 ph->env.nr_cpus_avail = nr;
1674 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1675 struct perf_header *ph, int fd,
1676 void *data __maybe_unused)
1678 ph->env.cpu_desc = do_read_string(fd, ph);
1679 return ph->env.cpu_desc ? 0 : -ENOMEM;
1682 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1683 struct perf_header *ph, int fd,
1684 void *data __maybe_unused)
1686 ph->env.cpuid = do_read_string(fd, ph);
1687 return ph->env.cpuid ? 0 : -ENOMEM;
1690 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1691 struct perf_header *ph, int fd,
1692 void *data __maybe_unused)
1697 ret = readn(fd, &mem, sizeof(mem));
1698 if (ret != sizeof(mem))
1702 mem = bswap_64(mem);
1704 ph->env.total_mem = mem;
1708 static struct perf_evsel *
1709 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1711 struct perf_evsel *evsel;
1713 list_for_each_entry(evsel, &evlist->entries, node) {
1714 if (evsel->idx == idx)
1722 perf_evlist__set_event_name(struct perf_evlist *evlist,
1723 struct perf_evsel *event)
1725 struct perf_evsel *evsel;
1730 evsel = perf_evlist__find_by_index(evlist, event->idx);
1737 evsel->name = strdup(event->name);
1741 process_event_desc(struct perf_file_section *section __maybe_unused,
1742 struct perf_header *header, int fd,
1743 void *data __maybe_unused)
1745 struct perf_session *session;
1746 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1751 session = container_of(header, struct perf_session, header);
1752 for (evsel = events; evsel->attr.size; evsel++)
1753 perf_evlist__set_event_name(session->evlist, evsel);
1755 free_event_desc(events);
1760 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1761 struct perf_header *ph, int fd,
1762 void *data __maybe_unused)
1769 ret = readn(fd, &nr, sizeof(nr));
1770 if (ret != sizeof(nr))
1776 ph->env.nr_cmdline = nr;
1777 strbuf_init(&sb, 128);
1779 for (i = 0; i < nr; i++) {
1780 str = do_read_string(fd, ph);
1784 /* include a NULL character at the end */
1785 strbuf_add(&sb, str, strlen(str) + 1);
1788 ph->env.cmdline = strbuf_detach(&sb, NULL);
1792 strbuf_release(&sb);
1796 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1797 struct perf_header *ph, int fd,
1798 void *data __maybe_unused)
1805 ret = readn(fd, &nr, sizeof(nr));
1806 if (ret != sizeof(nr))
1812 ph->env.nr_sibling_cores = nr;
1813 strbuf_init(&sb, 128);
1815 for (i = 0; i < nr; i++) {
1816 str = do_read_string(fd, ph);
1820 /* include a NULL character at the end */
1821 strbuf_add(&sb, str, strlen(str) + 1);
1824 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1826 ret = readn(fd, &nr, sizeof(nr));
1827 if (ret != sizeof(nr))
1833 ph->env.nr_sibling_threads = nr;
1835 for (i = 0; i < nr; i++) {
1836 str = do_read_string(fd, ph);
1840 /* include a NULL character at the end */
1841 strbuf_add(&sb, str, strlen(str) + 1);
1844 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1848 strbuf_release(&sb);
1852 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1853 struct perf_header *ph, int fd,
1854 void *data __maybe_unused)
1859 uint64_t mem_total, mem_free;
1863 ret = readn(fd, &nr, sizeof(nr));
1864 if (ret != sizeof(nr))
1870 ph->env.nr_numa_nodes = nr;
1871 strbuf_init(&sb, 256);
1873 for (i = 0; i < nr; i++) {
1875 ret = readn(fd, &node, sizeof(node));
1876 if (ret != sizeof(node))
1879 ret = readn(fd, &mem_total, sizeof(u64));
1880 if (ret != sizeof(u64))
1883 ret = readn(fd, &mem_free, sizeof(u64));
1884 if (ret != sizeof(u64))
1887 if (ph->needs_swap) {
1888 node = bswap_32(node);
1889 mem_total = bswap_64(mem_total);
1890 mem_free = bswap_64(mem_free);
1893 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1894 node, mem_total, mem_free);
1896 str = do_read_string(fd, ph);
1900 /* include a NULL character at the end */
1901 strbuf_add(&sb, str, strlen(str) + 1);
1904 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1908 strbuf_release(&sb);
1912 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1913 struct perf_header *ph, int fd,
1914 void *data __maybe_unused)
1922 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1923 if (ret != sizeof(pmu_num))
1927 pmu_num = bswap_32(pmu_num);
1930 pr_debug("pmu mappings not available\n");
1934 ph->env.nr_pmu_mappings = pmu_num;
1935 strbuf_init(&sb, 128);
1938 if (readn(fd, &type, sizeof(type)) != sizeof(type))
1941 type = bswap_32(type);
1943 name = do_read_string(fd, ph);
1947 strbuf_addf(&sb, "%u:%s", type, name);
1948 /* include a NULL character at the end */
1949 strbuf_add(&sb, "", 1);
1954 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1958 strbuf_release(&sb);
1962 struct feature_ops {
1963 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1964 void (*print)(struct perf_header *h, int fd, FILE *fp);
1965 int (*process)(struct perf_file_section *section,
1966 struct perf_header *h, int fd, void *data);
1971 #define FEAT_OPA(n, func) \
1972 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1973 #define FEAT_OPP(n, func) \
1974 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1975 .process = process_##func }
1976 #define FEAT_OPF(n, func) \
1977 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1978 .process = process_##func, .full_only = true }
1980 /* feature_ops not implemented: */
1981 #define print_tracing_data NULL
1982 #define print_build_id NULL
1984 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1985 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1986 FEAT_OPP(HEADER_BUILD_ID, build_id),
1987 FEAT_OPP(HEADER_HOSTNAME, hostname),
1988 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1989 FEAT_OPP(HEADER_VERSION, version),
1990 FEAT_OPP(HEADER_ARCH, arch),
1991 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1992 FEAT_OPP(HEADER_CPUDESC, cpudesc),
1993 FEAT_OPP(HEADER_CPUID, cpuid),
1994 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
1995 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
1996 FEAT_OPP(HEADER_CMDLINE, cmdline),
1997 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1998 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1999 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
2000 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2003 struct header_print_data {
2005 bool full; /* extended list of headers */
2008 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2009 struct perf_header *ph,
2010 int feat, int fd, void *data)
2012 struct header_print_data *hd = data;
2014 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2015 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2016 "%d, continuing...\n", section->offset, feat);
2019 if (feat >= HEADER_LAST_FEATURE) {
2020 pr_warning("unknown feature %d\n", feat);
2023 if (!feat_ops[feat].print)
2026 if (!feat_ops[feat].full_only || hd->full)
2027 feat_ops[feat].print(ph, fd, hd->fp);
2029 fprintf(hd->fp, "# %s info available, use -I to display\n",
2030 feat_ops[feat].name);
2035 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2037 struct header_print_data hd;
2038 struct perf_header *header = &session->header;
2039 int fd = session->fd;
2043 perf_header__process_sections(header, fd, &hd,
2044 perf_file_section__fprintf_info);
2048 static int do_write_feat(int fd, struct perf_header *h, int type,
2049 struct perf_file_section **p,
2050 struct perf_evlist *evlist)
2055 if (perf_header__has_feat(h, type)) {
2056 if (!feat_ops[type].write)
2059 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2061 err = feat_ops[type].write(fd, h, evlist);
2063 pr_debug("failed to write feature %d\n", type);
2065 /* undo anything written */
2066 lseek(fd, (*p)->offset, SEEK_SET);
2070 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2076 static int perf_header__adds_write(struct perf_header *header,
2077 struct perf_evlist *evlist, int fd)
2080 struct perf_file_section *feat_sec, *p;
2086 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2090 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
2091 if (feat_sec == NULL)
2094 sec_size = sizeof(*feat_sec) * nr_sections;
2096 sec_start = header->data_offset + header->data_size;
2097 lseek(fd, sec_start + sec_size, SEEK_SET);
2099 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2100 if (do_write_feat(fd, header, feat, &p, evlist))
2101 perf_header__clear_feat(header, feat);
2104 lseek(fd, sec_start, SEEK_SET);
2106 * may write more than needed due to dropped feature, but
2107 * this is okay, reader will skip the mising entries
2109 err = do_write(fd, feat_sec, sec_size);
2111 pr_debug("failed to write feature section\n");
2116 int perf_header__write_pipe(int fd)
2118 struct perf_pipe_file_header f_header;
2121 f_header = (struct perf_pipe_file_header){
2122 .magic = PERF_MAGIC,
2123 .size = sizeof(f_header),
2126 err = do_write(fd, &f_header, sizeof(f_header));
2128 pr_debug("failed to write perf pipe header\n");
2135 int perf_session__write_header(struct perf_session *session,
2136 struct perf_evlist *evlist,
2137 int fd, bool at_exit)
2139 struct perf_file_header f_header;
2140 struct perf_file_attr f_attr;
2141 struct perf_header *header = &session->header;
2142 struct perf_evsel *evsel, *pair = NULL;
2145 lseek(fd, sizeof(f_header), SEEK_SET);
2147 if (session->evlist != evlist)
2148 pair = perf_evlist__first(session->evlist);
2150 list_for_each_entry(evsel, &evlist->entries, node) {
2151 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2152 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2155 pr_debug("failed to write perf header\n");
2158 if (session->evlist != evlist) {
2159 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
2162 evsel->ids += pair->ids;
2163 pair = perf_evsel__next(pair);
2167 header->attr_offset = lseek(fd, 0, SEEK_CUR);
2169 list_for_each_entry(evsel, &evlist->entries, node) {
2170 f_attr = (struct perf_file_attr){
2171 .attr = evsel->attr,
2173 .offset = evsel->id_offset,
2174 .size = evsel->ids * sizeof(u64),
2177 err = do_write(fd, &f_attr, sizeof(f_attr));
2179 pr_debug("failed to write perf header attribute\n");
2184 header->event_offset = lseek(fd, 0, SEEK_CUR);
2185 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
2187 err = do_write(fd, trace_events, header->event_size);
2189 pr_debug("failed to write perf header events\n");
2194 header->data_offset = lseek(fd, 0, SEEK_CUR);
2197 err = perf_header__adds_write(header, evlist, fd);
2202 f_header = (struct perf_file_header){
2203 .magic = PERF_MAGIC,
2204 .size = sizeof(f_header),
2205 .attr_size = sizeof(f_attr),
2207 .offset = header->attr_offset,
2208 .size = evlist->nr_entries * sizeof(f_attr),
2211 .offset = header->data_offset,
2212 .size = header->data_size,
2215 .offset = header->event_offset,
2216 .size = header->event_size,
2220 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2222 lseek(fd, 0, SEEK_SET);
2223 err = do_write(fd, &f_header, sizeof(f_header));
2225 pr_debug("failed to write perf header\n");
2228 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2234 static int perf_header__getbuffer64(struct perf_header *header,
2235 int fd, void *buf, size_t size)
2237 if (readn(fd, buf, size) <= 0)
2240 if (header->needs_swap)
2241 mem_bswap_64(buf, size);
2246 int perf_header__process_sections(struct perf_header *header, int fd,
2248 int (*process)(struct perf_file_section *section,
2249 struct perf_header *ph,
2250 int feat, int fd, void *data))
2252 struct perf_file_section *feat_sec, *sec;
2258 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2262 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
2266 sec_size = sizeof(*feat_sec) * nr_sections;
2268 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2270 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2274 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2275 err = process(sec++, header, feat, fd, data);
2285 static const int attr_file_abi_sizes[] = {
2286 [0] = PERF_ATTR_SIZE_VER0,
2287 [1] = PERF_ATTR_SIZE_VER1,
2288 [2] = PERF_ATTR_SIZE_VER2,
2289 [3] = PERF_ATTR_SIZE_VER3,
2294 * In the legacy file format, the magic number is not used to encode endianness.
2295 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2296 * on ABI revisions, we need to try all combinations for all endianness to
2297 * detect the endianness.
2299 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2301 uint64_t ref_size, attr_size;
2304 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2305 ref_size = attr_file_abi_sizes[i]
2306 + sizeof(struct perf_file_section);
2307 if (hdr_sz != ref_size) {
2308 attr_size = bswap_64(hdr_sz);
2309 if (attr_size != ref_size)
2312 ph->needs_swap = true;
2314 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2319 /* could not determine endianness */
2323 #define PERF_PIPE_HDR_VER0 16
2325 static const size_t attr_pipe_abi_sizes[] = {
2326 [0] = PERF_PIPE_HDR_VER0,
2331 * In the legacy pipe format, there is an implicit assumption that endiannesss
2332 * between host recording the samples, and host parsing the samples is the
2333 * same. This is not always the case given that the pipe output may always be
2334 * redirected into a file and analyzed on a different machine with possibly a
2335 * different endianness and perf_event ABI revsions in the perf tool itself.
2337 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2342 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2343 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2344 attr_size = bswap_64(hdr_sz);
2345 if (attr_size != hdr_sz)
2348 ph->needs_swap = true;
2350 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2356 bool is_perf_magic(u64 magic)
2358 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2359 || magic == __perf_magic2
2360 || magic == __perf_magic2_sw)
2366 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2367 bool is_pipe, struct perf_header *ph)
2371 /* check for legacy format */
2372 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2374 pr_debug("legacy perf.data format\n");
2376 return try_all_pipe_abis(hdr_sz, ph);
2378 return try_all_file_abis(hdr_sz, ph);
2381 * the new magic number serves two purposes:
2382 * - unique number to identify actual perf.data files
2383 * - encode endianness of file
2386 /* check magic number with one endianness */
2387 if (magic == __perf_magic2)
2390 /* check magic number with opposite endianness */
2391 if (magic != __perf_magic2_sw)
2394 ph->needs_swap = true;
2399 int perf_file_header__read(struct perf_file_header *header,
2400 struct perf_header *ph, int fd)
2404 lseek(fd, 0, SEEK_SET);
2406 ret = readn(fd, header, sizeof(*header));
2410 if (check_magic_endian(header->magic,
2411 header->attr_size, false, ph) < 0) {
2412 pr_debug("magic/endian check failed\n");
2416 if (ph->needs_swap) {
2417 mem_bswap_64(header, offsetof(struct perf_file_header,
2421 if (header->size != sizeof(*header)) {
2422 /* Support the previous format */
2423 if (header->size == offsetof(typeof(*header), adds_features))
2424 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2427 } else if (ph->needs_swap) {
2429 * feature bitmap is declared as an array of unsigned longs --
2430 * not good since its size can differ between the host that
2431 * generated the data file and the host analyzing the file.
2433 * We need to handle endianness, but we don't know the size of
2434 * the unsigned long where the file was generated. Take a best
2435 * guess at determining it: try 64-bit swap first (ie., file
2436 * created on a 64-bit host), and check if the hostname feature
2437 * bit is set (this feature bit is forced on as of fbe96f2).
2438 * If the bit is not, undo the 64-bit swap and try a 32-bit
2439 * swap. If the hostname bit is still not set (e.g., older data
2440 * file), punt and fallback to the original behavior --
2441 * clearing all feature bits and setting buildid.
2443 mem_bswap_64(&header->adds_features,
2444 BITS_TO_U64(HEADER_FEAT_BITS));
2446 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2448 mem_bswap_64(&header->adds_features,
2449 BITS_TO_U64(HEADER_FEAT_BITS));
2452 mem_bswap_32(&header->adds_features,
2453 BITS_TO_U32(HEADER_FEAT_BITS));
2456 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2457 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2458 set_bit(HEADER_BUILD_ID, header->adds_features);
2462 memcpy(&ph->adds_features, &header->adds_features,
2463 sizeof(ph->adds_features));
2465 ph->event_offset = header->event_types.offset;
2466 ph->event_size = header->event_types.size;
2467 ph->data_offset = header->data.offset;
2468 ph->data_size = header->data.size;
2472 static int perf_file_section__process(struct perf_file_section *section,
2473 struct perf_header *ph,
2474 int feat, int fd, void *data)
2476 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2477 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2478 "%d, continuing...\n", section->offset, feat);
2482 if (feat >= HEADER_LAST_FEATURE) {
2483 pr_debug("unknown feature %d, continuing...\n", feat);
2487 if (!feat_ops[feat].process)
2490 return feat_ops[feat].process(section, ph, fd, data);
2493 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2494 struct perf_header *ph, int fd,
2499 ret = readn(fd, header, sizeof(*header));
2503 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2504 pr_debug("endian/magic failed\n");
2509 header->size = bswap_64(header->size);
2511 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2517 static int perf_header__read_pipe(struct perf_session *session, int fd)
2519 struct perf_header *header = &session->header;
2520 struct perf_pipe_file_header f_header;
2522 if (perf_file_header__read_pipe(&f_header, header, fd,
2523 session->repipe) < 0) {
2524 pr_debug("incompatible file format\n");
2533 static int read_attr(int fd, struct perf_header *ph,
2534 struct perf_file_attr *f_attr)
2536 struct perf_event_attr *attr = &f_attr->attr;
2538 size_t our_sz = sizeof(f_attr->attr);
2541 memset(f_attr, 0, sizeof(*f_attr));
2543 /* read minimal guaranteed structure */
2544 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2546 pr_debug("cannot read %d bytes of header attr\n",
2547 PERF_ATTR_SIZE_VER0);
2551 /* on file perf_event_attr size */
2559 sz = PERF_ATTR_SIZE_VER0;
2560 } else if (sz > our_sz) {
2561 pr_debug("file uses a more recent and unsupported ABI"
2562 " (%zu bytes extra)\n", sz - our_sz);
2565 /* what we have not yet read and that we know about */
2566 left = sz - PERF_ATTR_SIZE_VER0;
2569 ptr += PERF_ATTR_SIZE_VER0;
2571 ret = readn(fd, ptr, left);
2573 /* read perf_file_section, ids are read in caller */
2574 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2576 return ret <= 0 ? -1 : 0;
2579 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2580 struct pevent *pevent)
2582 struct event_format *event;
2585 /* already prepared */
2586 if (evsel->tp_format)
2589 event = pevent_find_event(pevent, evsel->attr.config);
2594 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2595 evsel->name = strdup(bf);
2596 if (evsel->name == NULL)
2600 evsel->tp_format = event;
2604 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2605 struct pevent *pevent)
2607 struct perf_evsel *pos;
2609 list_for_each_entry(pos, &evlist->entries, node) {
2610 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2611 perf_evsel__prepare_tracepoint_event(pos, pevent))
2618 int perf_session__read_header(struct perf_session *session, int fd)
2620 struct perf_header *header = &session->header;
2621 struct perf_file_header f_header;
2622 struct perf_file_attr f_attr;
2624 int nr_attrs, nr_ids, i, j;
2626 session->evlist = perf_evlist__new(NULL, NULL);
2627 if (session->evlist == NULL)
2630 if (session->fd_pipe)
2631 return perf_header__read_pipe(session, fd);
2633 if (perf_file_header__read(&f_header, header, fd) < 0)
2636 nr_attrs = f_header.attrs.size / f_header.attr_size;
2637 lseek(fd, f_header.attrs.offset, SEEK_SET);
2639 for (i = 0; i < nr_attrs; i++) {
2640 struct perf_evsel *evsel;
2643 if (read_attr(fd, header, &f_attr) < 0)
2646 if (header->needs_swap)
2647 perf_event__attr_swap(&f_attr.attr);
2649 tmp = lseek(fd, 0, SEEK_CUR);
2650 evsel = perf_evsel__new(&f_attr.attr, i);
2653 goto out_delete_evlist;
2655 evsel->needs_swap = header->needs_swap;
2657 * Do it before so that if perf_evsel__alloc_id fails, this
2658 * entry gets purged too at perf_evlist__delete().
2660 perf_evlist__add(session->evlist, evsel);
2662 nr_ids = f_attr.ids.size / sizeof(u64);
2664 * We don't have the cpu and thread maps on the header, so
2665 * for allocating the perf_sample_id table we fake 1 cpu and
2666 * hattr->ids threads.
2668 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2669 goto out_delete_evlist;
2671 lseek(fd, f_attr.ids.offset, SEEK_SET);
2673 for (j = 0; j < nr_ids; j++) {
2674 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2677 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2680 lseek(fd, tmp, SEEK_SET);
2683 symbol_conf.nr_events = nr_attrs;
2685 if (f_header.event_types.size) {
2686 lseek(fd, f_header.event_types.offset, SEEK_SET);
2687 trace_events = malloc(f_header.event_types.size);
2688 if (trace_events == NULL)
2690 if (perf_header__getbuffer64(header, fd, trace_events,
2691 f_header.event_types.size))
2693 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2696 perf_header__process_sections(header, fd, &session->pevent,
2697 perf_file_section__process);
2699 lseek(fd, header->data_offset, SEEK_SET);
2701 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2703 goto out_delete_evlist;
2711 perf_evlist__delete(session->evlist);
2712 session->evlist = NULL;
2716 int perf_event__synthesize_attr(struct perf_tool *tool,
2717 struct perf_event_attr *attr, u32 ids, u64 *id,
2718 perf_event__handler_t process)
2720 union perf_event *ev;
2724 size = sizeof(struct perf_event_attr);
2725 size = PERF_ALIGN(size, sizeof(u64));
2726 size += sizeof(struct perf_event_header);
2727 size += ids * sizeof(u64);
2734 ev->attr.attr = *attr;
2735 memcpy(ev->attr.id, id, ids * sizeof(u64));
2737 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2738 ev->attr.header.size = (u16)size;
2740 if (ev->attr.header.size == size)
2741 err = process(tool, ev, NULL, NULL);
2750 int perf_event__synthesize_attrs(struct perf_tool *tool,
2751 struct perf_session *session,
2752 perf_event__handler_t process)
2754 struct perf_evsel *evsel;
2757 list_for_each_entry(evsel, &session->evlist->entries, node) {
2758 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2759 evsel->id, process);
2761 pr_debug("failed to create perf header attribute\n");
2769 int perf_event__process_attr(union perf_event *event,
2770 struct perf_evlist **pevlist)
2773 struct perf_evsel *evsel;
2774 struct perf_evlist *evlist = *pevlist;
2776 if (evlist == NULL) {
2777 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2782 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2786 perf_evlist__add(evlist, evsel);
2788 ids = event->header.size;
2789 ids -= (void *)&event->attr.id - (void *)event;
2790 n_ids = ids / sizeof(u64);
2792 * We don't have the cpu and thread maps on the header, so
2793 * for allocating the perf_sample_id table we fake 1 cpu and
2794 * hattr->ids threads.
2796 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2799 for (i = 0; i < n_ids; i++) {
2800 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2806 int perf_event__synthesize_event_type(struct perf_tool *tool,
2807 u64 event_id, char *name,
2808 perf_event__handler_t process,
2809 struct machine *machine)
2811 union perf_event ev;
2815 memset(&ev, 0, sizeof(ev));
2817 ev.event_type.event_type.event_id = event_id;
2818 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2819 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2821 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2822 size = strlen(ev.event_type.event_type.name);
2823 size = PERF_ALIGN(size, sizeof(u64));
2824 ev.event_type.header.size = sizeof(ev.event_type) -
2825 (sizeof(ev.event_type.event_type.name) - size);
2827 err = process(tool, &ev, NULL, machine);
2832 int perf_event__synthesize_event_types(struct perf_tool *tool,
2833 perf_event__handler_t process,
2834 struct machine *machine)
2836 struct perf_trace_event_type *type;
2839 for (i = 0; i < trace_event_count; i++) {
2840 type = &trace_events[i];
2842 err = perf_event__synthesize_event_type(tool, type->event_id,
2843 type->name, process,
2846 pr_debug("failed to create perf header event type\n");
2854 int perf_event__process_event_type(struct perf_tool *tool __maybe_unused,
2855 union perf_event *event)
2857 if (perf_header__push_event(event->event_type.event_type.event_id,
2858 event->event_type.event_type.name) < 0)
2864 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2865 struct perf_evlist *evlist,
2866 perf_event__handler_t process)
2868 union perf_event ev;
2869 struct tracing_data *tdata;
2870 ssize_t size = 0, aligned_size = 0, padding;
2871 int err __maybe_unused = 0;
2874 * We are going to store the size of the data followed
2875 * by the data contents. Since the fd descriptor is a pipe,
2876 * we cannot seek back to store the size of the data once
2877 * we know it. Instead we:
2879 * - write the tracing data to the temp file
2880 * - get/write the data size to pipe
2881 * - write the tracing data from the temp file
2884 tdata = tracing_data_get(&evlist->entries, fd, true);
2888 memset(&ev, 0, sizeof(ev));
2890 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2892 aligned_size = PERF_ALIGN(size, sizeof(u64));
2893 padding = aligned_size - size;
2894 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2895 ev.tracing_data.size = aligned_size;
2897 process(tool, &ev, NULL, NULL);
2900 * The put function will copy all the tracing data
2901 * stored in temp file to the pipe.
2903 tracing_data_put(tdata);
2905 write_padded(fd, NULL, 0, padding);
2907 return aligned_size;
2910 int perf_event__process_tracing_data(union perf_event *event,
2911 struct perf_session *session)
2913 ssize_t size_read, padding, size = event->tracing_data.size;
2914 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2917 /* setup for reading amidst mmap */
2918 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2921 size_read = trace_report(session->fd, &session->pevent,
2923 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2925 if (readn(session->fd, buf, padding) < 0) {
2926 pr_err("%s: reading input file", __func__);
2929 if (session->repipe) {
2930 int retw = write(STDOUT_FILENO, buf, padding);
2931 if (retw <= 0 || retw != padding) {
2932 pr_err("%s: repiping tracing data padding", __func__);
2937 if (size_read + padding != size) {
2938 pr_err("%s: tracing data size mismatch", __func__);
2942 perf_evlist__prepare_tracepoint_events(session->evlist,
2945 return size_read + padding;
2948 int perf_event__synthesize_build_id(struct perf_tool *tool,
2949 struct dso *pos, u16 misc,
2950 perf_event__handler_t process,
2951 struct machine *machine)
2953 union perf_event ev;
2960 memset(&ev, 0, sizeof(ev));
2962 len = pos->long_name_len + 1;
2963 len = PERF_ALIGN(len, NAME_ALIGN);
2964 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2965 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2966 ev.build_id.header.misc = misc;
2967 ev.build_id.pid = machine->pid;
2968 ev.build_id.header.size = sizeof(ev.build_id) + len;
2969 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2971 err = process(tool, &ev, NULL, machine);
2976 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2977 union perf_event *event,
2978 struct perf_session *session)
2980 __event_process_build_id(&event->build_id,
2981 event->build_id.filename,
2986 void disable_buildid_cache(void)
2988 no_buildid_cache = true;