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 = read(fd, &len, sizeof(len));
152 if (sz < (ssize_t)sizeof(len))
162 ret = read(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->host_machine, fd);
295 for (nd = rb_first(&session->machines); 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");
318 realname = (char *) name;
320 realname = realpath(name, NULL);
322 if (realname == NULL || filename == NULL || linkname == NULL)
325 len = scnprintf(filename, size, "%s%s%s",
326 debugdir, slash ? "/" : "",
327 is_vdso ? VDSO__MAP_NAME : realname);
328 if (mkdir_p(filename, 0755))
331 snprintf(filename + len, size - len, "/%s", sbuild_id);
333 if (access(filename, F_OK)) {
335 if (copyfile("/proc/kallsyms", filename))
337 } else if (link(realname, filename) && copyfile(name, filename))
341 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
342 debugdir, sbuild_id);
344 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
347 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
348 targetname = filename + strlen(debugdir) - 5;
349 memcpy(targetname, "../..", 5);
351 if (symlink(targetname, linkname) == 0)
361 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
362 const char *name, const char *debugdir,
363 bool is_kallsyms, bool is_vdso)
365 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
367 build_id__sprintf(build_id, build_id_size, sbuild_id);
369 return build_id_cache__add_s(sbuild_id, debugdir, name,
370 is_kallsyms, is_vdso);
373 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
375 const size_t size = PATH_MAX;
376 char *filename = zalloc(size),
377 *linkname = zalloc(size);
380 if (filename == NULL || linkname == NULL)
383 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
384 debugdir, sbuild_id, sbuild_id + 2);
386 if (access(linkname, F_OK))
389 if (readlink(linkname, filename, size - 1) < 0)
392 if (unlink(linkname))
396 * Since the link is relative, we must make it absolute:
398 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
399 debugdir, sbuild_id, filename);
401 if (unlink(linkname))
411 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
413 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
414 bool is_vdso = is_vdso_map(dso->short_name);
416 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
417 dso->long_name, debugdir,
418 is_kallsyms, is_vdso);
421 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
426 dsos__for_each_with_build_id(pos, head)
427 if (dso__cache_build_id(pos, debugdir))
433 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
435 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
436 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
440 static int perf_session__cache_build_ids(struct perf_session *session)
444 char debugdir[PATH_MAX];
446 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
448 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
451 ret = machine__cache_build_ids(&session->host_machine, debugdir);
453 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
454 struct machine *pos = rb_entry(nd, struct machine, rb_node);
455 ret |= machine__cache_build_ids(pos, debugdir);
460 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
462 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
463 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
467 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
470 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
472 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
473 struct machine *pos = rb_entry(nd, struct machine, rb_node);
474 ret |= machine__read_build_ids(pos, with_hits);
480 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
481 struct perf_evlist *evlist)
483 return read_tracing_data(fd, &evlist->entries);
487 static int write_build_id(int fd, struct perf_header *h,
488 struct perf_evlist *evlist __maybe_unused)
490 struct perf_session *session;
493 session = container_of(h, struct perf_session, header);
495 if (!perf_session__read_build_ids(session, true))
498 err = dsos__write_buildid_table(h, fd);
500 pr_debug("failed to write buildid table\n");
503 if (!no_buildid_cache)
504 perf_session__cache_build_ids(session);
509 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
510 struct perf_evlist *evlist __maybe_unused)
519 return do_write_string(fd, uts.nodename);
522 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
523 struct perf_evlist *evlist __maybe_unused)
532 return do_write_string(fd, uts.release);
535 static int write_arch(int fd, struct perf_header *h __maybe_unused,
536 struct perf_evlist *evlist __maybe_unused)
545 return do_write_string(fd, uts.machine);
548 static int write_version(int fd, struct perf_header *h __maybe_unused,
549 struct perf_evlist *evlist __maybe_unused)
551 return do_write_string(fd, perf_version_string);
554 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
555 struct perf_evlist *evlist __maybe_unused)
558 #define CPUINFO_PROC NULL
563 const char *search = CPUINFO_PROC;
570 file = fopen("/proc/cpuinfo", "r");
574 while (getline(&buf, &len, file) > 0) {
575 ret = strncmp(buf, search, strlen(search));
585 p = strchr(buf, ':');
586 if (p && *(p+1) == ' ' && *(p+2))
592 /* squash extra space characters (branding string) */
599 while (*q && isspace(*q))
602 while ((*r++ = *q++));
606 ret = do_write_string(fd, s);
613 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
614 struct perf_evlist *evlist __maybe_unused)
620 nr = sysconf(_SC_NPROCESSORS_CONF);
624 nrc = (u32)(nr & UINT_MAX);
626 nr = sysconf(_SC_NPROCESSORS_ONLN);
630 nra = (u32)(nr & UINT_MAX);
632 ret = do_write(fd, &nrc, sizeof(nrc));
636 return do_write(fd, &nra, sizeof(nra));
639 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
640 struct perf_evlist *evlist)
642 struct perf_evsel *evsel;
646 nre = evlist->nr_entries;
649 * write number of events
651 ret = do_write(fd, &nre, sizeof(nre));
656 * size of perf_event_attr struct
658 sz = (u32)sizeof(evsel->attr);
659 ret = do_write(fd, &sz, sizeof(sz));
663 list_for_each_entry(evsel, &evlist->entries, node) {
665 ret = do_write(fd, &evsel->attr, sz);
669 * write number of unique id per event
670 * there is one id per instance of an event
672 * copy into an nri to be independent of the
676 ret = do_write(fd, &nri, sizeof(nri));
681 * write event string as passed on cmdline
683 ret = do_write_string(fd, perf_evsel__name(evsel));
687 * write unique ids for this event
689 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
696 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
697 struct perf_evlist *evlist __maybe_unused)
699 char buf[MAXPATHLEN];
705 * actual atual path to perf binary
707 sprintf(proc, "/proc/%d/exe", getpid());
708 ret = readlink(proc, buf, sizeof(buf));
712 /* readlink() does not add null termination */
715 /* account for binary path */
718 ret = do_write(fd, &n, sizeof(n));
722 ret = do_write_string(fd, buf);
726 for (i = 0 ; i < header_argc; i++) {
727 ret = do_write_string(fd, header_argv[i]);
734 #define CORE_SIB_FMT \
735 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
736 #define THRD_SIB_FMT \
737 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
742 char **core_siblings;
743 char **thread_siblings;
746 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
749 char filename[MAXPATHLEN];
750 char *buf = NULL, *p;
755 sprintf(filename, CORE_SIB_FMT, cpu);
756 fp = fopen(filename, "r");
760 if (getline(&buf, &len, fp) <= 0)
765 p = strchr(buf, '\n');
769 for (i = 0; i < tp->core_sib; i++) {
770 if (!strcmp(buf, tp->core_siblings[i]))
773 if (i == tp->core_sib) {
774 tp->core_siblings[i] = buf;
780 sprintf(filename, THRD_SIB_FMT, cpu);
781 fp = fopen(filename, "r");
785 if (getline(&buf, &len, fp) <= 0)
788 p = strchr(buf, '\n');
792 for (i = 0; i < tp->thread_sib; i++) {
793 if (!strcmp(buf, tp->thread_siblings[i]))
796 if (i == tp->thread_sib) {
797 tp->thread_siblings[i] = buf;
809 static void free_cpu_topo(struct cpu_topo *tp)
816 for (i = 0 ; i < tp->core_sib; i++)
817 free(tp->core_siblings[i]);
819 for (i = 0 ; i < tp->thread_sib; i++)
820 free(tp->thread_siblings[i]);
825 static struct cpu_topo *build_cpu_topology(void)
834 ncpus = sysconf(_SC_NPROCESSORS_CONF);
838 nr = (u32)(ncpus & UINT_MAX);
840 sz = nr * sizeof(char *);
842 addr = calloc(1, sizeof(*tp) + 2 * sz);
849 tp->core_siblings = addr;
851 tp->thread_siblings = addr;
853 for (i = 0; i < nr; i++) {
854 ret = build_cpu_topo(tp, i);
865 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
866 struct perf_evlist *evlist __maybe_unused)
872 tp = build_cpu_topology();
876 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
880 for (i = 0; i < tp->core_sib; i++) {
881 ret = do_write_string(fd, tp->core_siblings[i]);
885 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
889 for (i = 0; i < tp->thread_sib; i++) {
890 ret = do_write_string(fd, tp->thread_siblings[i]);
901 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
902 struct perf_evlist *evlist __maybe_unused)
910 fp = fopen("/proc/meminfo", "r");
914 while (getline(&buf, &len, fp) > 0) {
915 ret = strncmp(buf, "MemTotal:", 9);
920 n = sscanf(buf, "%*s %"PRIu64, &mem);
922 ret = do_write(fd, &mem, sizeof(mem));
929 static int write_topo_node(int fd, int node)
931 char str[MAXPATHLEN];
933 char *buf = NULL, *p;
936 u64 mem_total, mem_free, mem;
939 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
940 fp = fopen(str, "r");
944 while (getline(&buf, &len, fp) > 0) {
945 /* skip over invalid lines */
946 if (!strchr(buf, ':'))
948 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
950 if (!strcmp(field, "MemTotal:"))
952 if (!strcmp(field, "MemFree:"))
958 ret = do_write(fd, &mem_total, sizeof(u64));
962 ret = do_write(fd, &mem_free, sizeof(u64));
967 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
969 fp = fopen(str, "r");
973 if (getline(&buf, &len, fp) <= 0)
976 p = strchr(buf, '\n');
980 ret = do_write_string(fd, buf);
987 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
988 struct perf_evlist *evlist __maybe_unused)
993 struct cpu_map *node_map = NULL;
998 fp = fopen("/sys/devices/system/node/online", "r");
1002 if (getline(&buf, &len, fp) <= 0)
1005 c = strchr(buf, '\n');
1009 node_map = cpu_map__new(buf);
1013 nr = (u32)node_map->nr;
1015 ret = do_write(fd, &nr, sizeof(nr));
1019 for (i = 0; i < nr; i++) {
1020 j = (u32)node_map->map[i];
1021 ret = do_write(fd, &j, sizeof(j));
1025 ret = write_topo_node(fd, i);
1039 * struct pmu_mappings {
1048 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1049 struct perf_evlist *evlist __maybe_unused)
1051 struct perf_pmu *pmu = NULL;
1052 off_t offset = lseek(fd, 0, SEEK_CUR);
1055 /* write real pmu_num later */
1056 do_write(fd, &pmu_num, sizeof(pmu_num));
1058 while ((pmu = perf_pmu__scan(pmu))) {
1062 do_write(fd, &pmu->type, sizeof(pmu->type));
1063 do_write_string(fd, pmu->name);
1066 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1068 lseek(fd, offset, SEEK_SET);
1076 * default get_cpuid(): nothing gets recorded
1077 * actual implementation must be in arch/$(ARCH)/util/header.c
1079 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1080 size_t sz __maybe_unused)
1085 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1086 struct perf_evlist *evlist __maybe_unused)
1091 ret = get_cpuid(buffer, sizeof(buffer));
1097 return do_write_string(fd, buffer);
1100 static int write_branch_stack(int fd __maybe_unused,
1101 struct perf_header *h __maybe_unused,
1102 struct perf_evlist *evlist __maybe_unused)
1107 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1110 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1113 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1116 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1119 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1121 fprintf(fp, "# arch : %s\n", ph->env.arch);
1124 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1127 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1130 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1133 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1134 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1137 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1140 fprintf(fp, "# perf version : %s\n", ph->env.version);
1143 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1149 nr = ph->env.nr_cmdline;
1150 str = ph->env.cmdline;
1152 fprintf(fp, "# cmdline : ");
1154 for (i = 0; i < nr; i++) {
1155 fprintf(fp, "%s ", str);
1156 str += strlen(str) + 1;
1161 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1167 nr = ph->env.nr_sibling_cores;
1168 str = ph->env.sibling_cores;
1170 for (i = 0; i < nr; i++) {
1171 fprintf(fp, "# sibling cores : %s\n", str);
1172 str += strlen(str) + 1;
1175 nr = ph->env.nr_sibling_threads;
1176 str = ph->env.sibling_threads;
1178 for (i = 0; i < nr; i++) {
1179 fprintf(fp, "# sibling threads : %s\n", str);
1180 str += strlen(str) + 1;
1184 static void free_event_desc(struct perf_evsel *events)
1186 struct perf_evsel *evsel;
1191 for (evsel = events; evsel->attr.size; evsel++) {
1201 static struct perf_evsel *
1202 read_event_desc(struct perf_header *ph, int fd)
1204 struct perf_evsel *evsel, *events = NULL;
1207 u32 nre, sz, nr, i, j;
1211 /* number of events */
1212 ret = read(fd, &nre, sizeof(nre));
1213 if (ret != (ssize_t)sizeof(nre))
1217 nre = bswap_32(nre);
1219 ret = read(fd, &sz, sizeof(sz));
1220 if (ret != (ssize_t)sizeof(sz))
1226 /* buffer to hold on file attr struct */
1231 /* the last event terminates with evsel->attr.size == 0: */
1232 events = calloc(nre + 1, sizeof(*events));
1236 msz = sizeof(evsel->attr);
1240 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1244 * must read entire on-file attr struct to
1245 * sync up with layout.
1247 ret = read(fd, buf, sz);
1248 if (ret != (ssize_t)sz)
1252 perf_event__attr_swap(buf);
1254 memcpy(&evsel->attr, buf, msz);
1256 ret = read(fd, &nr, sizeof(nr));
1257 if (ret != (ssize_t)sizeof(nr))
1260 if (ph->needs_swap) {
1262 evsel->needs_swap = true;
1265 evsel->name = do_read_string(fd, ph);
1270 id = calloc(nr, sizeof(*id));
1276 for (j = 0 ; j < nr; j++) {
1277 ret = read(fd, id, sizeof(*id));
1278 if (ret != (ssize_t)sizeof(*id))
1281 *id = bswap_64(*id);
1291 free_event_desc(events);
1296 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1298 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1303 fprintf(fp, "# event desc: not available or unable to read\n");
1307 for (evsel = events; evsel->attr.size; evsel++) {
1308 fprintf(fp, "# event : name = %s, ", evsel->name);
1310 fprintf(fp, "type = %d, config = 0x%"PRIx64
1311 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1313 (u64)evsel->attr.config,
1314 (u64)evsel->attr.config1,
1315 (u64)evsel->attr.config2);
1317 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1318 evsel->attr.exclude_user,
1319 evsel->attr.exclude_kernel);
1321 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1322 evsel->attr.exclude_host,
1323 evsel->attr.exclude_guest);
1325 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1328 fprintf(fp, ", id = {");
1329 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1332 fprintf(fp, " %"PRIu64, *id);
1340 free_event_desc(events);
1343 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1346 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1349 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1354 uint64_t mem_total, mem_free;
1357 nr = ph->env.nr_numa_nodes;
1358 str = ph->env.numa_nodes;
1360 for (i = 0; i < nr; i++) {
1362 c = strtoul(str, &tmp, 0);
1367 mem_total = strtoull(str, &tmp, 0);
1372 mem_free = strtoull(str, &tmp, 0);
1376 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1377 " free = %"PRIu64" kB\n",
1378 c, mem_total, mem_free);
1381 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1383 str += strlen(str) + 1;
1387 fprintf(fp, "# numa topology : not available\n");
1390 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1392 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1395 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1396 int fd __maybe_unused, FILE *fp)
1398 fprintf(fp, "# contains samples with branch stack\n");
1401 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1404 const char *delimiter = "# pmu mappings: ";
1409 pmu_num = ph->env.nr_pmu_mappings;
1411 fprintf(fp, "# pmu mappings: not available\n");
1415 str = ph->env.pmu_mappings;
1418 type = strtoul(str, &tmp, 0);
1423 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1426 str += strlen(str) + 1;
1435 fprintf(fp, "# pmu mappings: unable to read\n");
1438 static int __event_process_build_id(struct build_id_event *bev,
1440 struct perf_session *session)
1443 struct list_head *head;
1444 struct machine *machine;
1447 enum dso_kernel_type dso_type;
1449 machine = perf_session__findnew_machine(session, bev->pid);
1453 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1456 case PERF_RECORD_MISC_KERNEL:
1457 dso_type = DSO_TYPE_KERNEL;
1458 head = &machine->kernel_dsos;
1460 case PERF_RECORD_MISC_GUEST_KERNEL:
1461 dso_type = DSO_TYPE_GUEST_KERNEL;
1462 head = &machine->kernel_dsos;
1464 case PERF_RECORD_MISC_USER:
1465 case PERF_RECORD_MISC_GUEST_USER:
1466 dso_type = DSO_TYPE_USER;
1467 head = &machine->user_dsos;
1473 dso = __dsos__findnew(head, filename);
1475 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1477 dso__set_build_id(dso, &bev->build_id);
1479 if (filename[0] == '[')
1480 dso->kernel = dso_type;
1482 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1484 pr_debug("build id event received for %s: %s\n",
1485 dso->long_name, sbuild_id);
1493 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1494 int input, u64 offset, u64 size)
1496 struct perf_session *session = container_of(header, struct perf_session, header);
1498 struct perf_event_header header;
1499 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1502 struct build_id_event bev;
1503 char filename[PATH_MAX];
1504 u64 limit = offset + size;
1506 while (offset < limit) {
1509 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1512 if (header->needs_swap)
1513 perf_event_header__bswap(&old_bev.header);
1515 len = old_bev.header.size - sizeof(old_bev);
1516 if (read(input, filename, len) != len)
1519 bev.header = old_bev.header;
1522 * As the pid is the missing value, we need to fill
1523 * it properly. The header.misc value give us nice hint.
1525 bev.pid = HOST_KERNEL_ID;
1526 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1527 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1528 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1530 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1531 __event_process_build_id(&bev, filename, session);
1533 offset += bev.header.size;
1539 static int perf_header__read_build_ids(struct perf_header *header,
1540 int input, u64 offset, u64 size)
1542 struct perf_session *session = container_of(header, struct perf_session, header);
1543 struct build_id_event bev;
1544 char filename[PATH_MAX];
1545 u64 limit = offset + size, orig_offset = offset;
1548 while (offset < limit) {
1551 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1554 if (header->needs_swap)
1555 perf_event_header__bswap(&bev.header);
1557 len = bev.header.size - sizeof(bev);
1558 if (read(input, filename, len) != len)
1561 * The a1645ce1 changeset:
1563 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1565 * Added a field to struct build_id_event that broke the file
1568 * Since the kernel build-id is the first entry, process the
1569 * table using the old format if the well known
1570 * '[kernel.kallsyms]' string for the kernel build-id has the
1571 * first 4 characters chopped off (where the pid_t sits).
1573 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1574 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1576 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1579 __event_process_build_id(&bev, filename, session);
1581 offset += bev.header.size;
1588 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1589 struct perf_header *ph __maybe_unused,
1592 trace_report(fd, data, false);
1596 static int process_build_id(struct perf_file_section *section,
1597 struct perf_header *ph, int fd,
1598 void *data __maybe_unused)
1600 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1601 pr_debug("Failed to read buildids, continuing...\n");
1605 static int process_hostname(struct perf_file_section *section __maybe_unused,
1606 struct perf_header *ph, int fd,
1607 void *data __maybe_unused)
1609 ph->env.hostname = do_read_string(fd, ph);
1610 return ph->env.hostname ? 0 : -ENOMEM;
1613 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1614 struct perf_header *ph, int fd,
1615 void *data __maybe_unused)
1617 ph->env.os_release = do_read_string(fd, ph);
1618 return ph->env.os_release ? 0 : -ENOMEM;
1621 static int process_version(struct perf_file_section *section __maybe_unused,
1622 struct perf_header *ph, int fd,
1623 void *data __maybe_unused)
1625 ph->env.version = do_read_string(fd, ph);
1626 return ph->env.version ? 0 : -ENOMEM;
1629 static int process_arch(struct perf_file_section *section __maybe_unused,
1630 struct perf_header *ph, int fd,
1631 void *data __maybe_unused)
1633 ph->env.arch = do_read_string(fd, ph);
1634 return ph->env.arch ? 0 : -ENOMEM;
1637 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1638 struct perf_header *ph, int fd,
1639 void *data __maybe_unused)
1644 ret = read(fd, &nr, sizeof(nr));
1645 if (ret != sizeof(nr))
1651 ph->env.nr_cpus_online = nr;
1653 ret = read(fd, &nr, sizeof(nr));
1654 if (ret != sizeof(nr))
1660 ph->env.nr_cpus_avail = nr;
1664 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1665 struct perf_header *ph, int fd,
1666 void *data __maybe_unused)
1668 ph->env.cpu_desc = do_read_string(fd, ph);
1669 return ph->env.cpu_desc ? 0 : -ENOMEM;
1672 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1673 struct perf_header *ph, int fd,
1674 void *data __maybe_unused)
1676 ph->env.cpuid = do_read_string(fd, ph);
1677 return ph->env.cpuid ? 0 : -ENOMEM;
1680 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1681 struct perf_header *ph, int fd,
1682 void *data __maybe_unused)
1687 ret = read(fd, &mem, sizeof(mem));
1688 if (ret != sizeof(mem))
1692 mem = bswap_64(mem);
1694 ph->env.total_mem = mem;
1698 static struct perf_evsel *
1699 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1701 struct perf_evsel *evsel;
1703 list_for_each_entry(evsel, &evlist->entries, node) {
1704 if (evsel->idx == idx)
1712 perf_evlist__set_event_name(struct perf_evlist *evlist,
1713 struct perf_evsel *event)
1715 struct perf_evsel *evsel;
1720 evsel = perf_evlist__find_by_index(evlist, event->idx);
1727 evsel->name = strdup(event->name);
1731 process_event_desc(struct perf_file_section *section __maybe_unused,
1732 struct perf_header *header, int fd,
1733 void *data __maybe_unused)
1735 struct perf_session *session;
1736 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1741 session = container_of(header, struct perf_session, header);
1742 for (evsel = events; evsel->attr.size; evsel++)
1743 perf_evlist__set_event_name(session->evlist, evsel);
1745 free_event_desc(events);
1750 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1751 struct perf_header *ph, int fd,
1752 void *data __maybe_unused)
1759 ret = read(fd, &nr, sizeof(nr));
1760 if (ret != sizeof(nr))
1766 ph->env.nr_cmdline = nr;
1767 strbuf_init(&sb, 128);
1769 for (i = 0; i < nr; i++) {
1770 str = do_read_string(fd, ph);
1774 /* include a NULL character at the end */
1775 strbuf_add(&sb, str, strlen(str) + 1);
1778 ph->env.cmdline = strbuf_detach(&sb, NULL);
1782 strbuf_release(&sb);
1786 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1787 struct perf_header *ph, int fd,
1788 void *data __maybe_unused)
1795 ret = read(fd, &nr, sizeof(nr));
1796 if (ret != sizeof(nr))
1802 ph->env.nr_sibling_cores = nr;
1803 strbuf_init(&sb, 128);
1805 for (i = 0; i < nr; i++) {
1806 str = do_read_string(fd, ph);
1810 /* include a NULL character at the end */
1811 strbuf_add(&sb, str, strlen(str) + 1);
1814 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1816 ret = read(fd, &nr, sizeof(nr));
1817 if (ret != sizeof(nr))
1823 ph->env.nr_sibling_threads = nr;
1825 for (i = 0; i < nr; i++) {
1826 str = do_read_string(fd, ph);
1830 /* include a NULL character at the end */
1831 strbuf_add(&sb, str, strlen(str) + 1);
1834 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1838 strbuf_release(&sb);
1842 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1843 struct perf_header *ph, int fd,
1844 void *data __maybe_unused)
1849 uint64_t mem_total, mem_free;
1853 ret = read(fd, &nr, sizeof(nr));
1854 if (ret != sizeof(nr))
1860 ph->env.nr_numa_nodes = nr;
1861 strbuf_init(&sb, 256);
1863 for (i = 0; i < nr; i++) {
1865 ret = read(fd, &node, sizeof(node));
1866 if (ret != sizeof(node))
1869 ret = read(fd, &mem_total, sizeof(u64));
1870 if (ret != sizeof(u64))
1873 ret = read(fd, &mem_free, sizeof(u64));
1874 if (ret != sizeof(u64))
1877 if (ph->needs_swap) {
1878 node = bswap_32(node);
1879 mem_total = bswap_64(mem_total);
1880 mem_free = bswap_64(mem_free);
1883 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1884 node, mem_total, mem_free);
1886 str = do_read_string(fd, ph);
1890 /* include a NULL character at the end */
1891 strbuf_add(&sb, str, strlen(str) + 1);
1894 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1898 strbuf_release(&sb);
1902 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1903 struct perf_header *ph, int fd,
1904 void *data __maybe_unused)
1912 ret = read(fd, &pmu_num, sizeof(pmu_num));
1913 if (ret != sizeof(pmu_num))
1917 pmu_num = bswap_32(pmu_num);
1920 pr_debug("pmu mappings not available\n");
1924 ph->env.nr_pmu_mappings = pmu_num;
1925 strbuf_init(&sb, 128);
1928 if (read(fd, &type, sizeof(type)) != sizeof(type))
1931 type = bswap_32(type);
1933 name = do_read_string(fd, ph);
1937 strbuf_addf(&sb, "%u:%s", type, name);
1938 /* include a NULL character at the end */
1939 strbuf_add(&sb, "", 1);
1944 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1948 strbuf_release(&sb);
1952 struct feature_ops {
1953 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1954 void (*print)(struct perf_header *h, int fd, FILE *fp);
1955 int (*process)(struct perf_file_section *section,
1956 struct perf_header *h, int fd, void *data);
1961 #define FEAT_OPA(n, func) \
1962 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1963 #define FEAT_OPP(n, func) \
1964 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1965 .process = process_##func }
1966 #define FEAT_OPF(n, func) \
1967 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1968 .process = process_##func, .full_only = true }
1970 /* feature_ops not implemented: */
1971 #define print_tracing_data NULL
1972 #define print_build_id NULL
1974 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1975 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1976 FEAT_OPP(HEADER_BUILD_ID, build_id),
1977 FEAT_OPP(HEADER_HOSTNAME, hostname),
1978 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1979 FEAT_OPP(HEADER_VERSION, version),
1980 FEAT_OPP(HEADER_ARCH, arch),
1981 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1982 FEAT_OPP(HEADER_CPUDESC, cpudesc),
1983 FEAT_OPP(HEADER_CPUID, cpuid),
1984 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
1985 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
1986 FEAT_OPP(HEADER_CMDLINE, cmdline),
1987 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1988 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1989 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
1990 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
1993 struct header_print_data {
1995 bool full; /* extended list of headers */
1998 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1999 struct perf_header *ph,
2000 int feat, int fd, void *data)
2002 struct header_print_data *hd = data;
2004 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2005 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2006 "%d, continuing...\n", section->offset, feat);
2009 if (feat >= HEADER_LAST_FEATURE) {
2010 pr_warning("unknown feature %d\n", feat);
2013 if (!feat_ops[feat].print)
2016 if (!feat_ops[feat].full_only || hd->full)
2017 feat_ops[feat].print(ph, fd, hd->fp);
2019 fprintf(hd->fp, "# %s info available, use -I to display\n",
2020 feat_ops[feat].name);
2025 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2027 struct header_print_data hd;
2028 struct perf_header *header = &session->header;
2029 int fd = session->fd;
2033 perf_header__process_sections(header, fd, &hd,
2034 perf_file_section__fprintf_info);
2038 static int do_write_feat(int fd, struct perf_header *h, int type,
2039 struct perf_file_section **p,
2040 struct perf_evlist *evlist)
2045 if (perf_header__has_feat(h, type)) {
2046 if (!feat_ops[type].write)
2049 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2051 err = feat_ops[type].write(fd, h, evlist);
2053 pr_debug("failed to write feature %d\n", type);
2055 /* undo anything written */
2056 lseek(fd, (*p)->offset, SEEK_SET);
2060 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2066 static int perf_header__adds_write(struct perf_header *header,
2067 struct perf_evlist *evlist, int fd)
2070 struct perf_file_section *feat_sec, *p;
2076 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2080 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
2081 if (feat_sec == NULL)
2084 sec_size = sizeof(*feat_sec) * nr_sections;
2086 sec_start = header->data_offset + header->data_size;
2087 lseek(fd, sec_start + sec_size, SEEK_SET);
2089 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2090 if (do_write_feat(fd, header, feat, &p, evlist))
2091 perf_header__clear_feat(header, feat);
2094 lseek(fd, sec_start, SEEK_SET);
2096 * may write more than needed due to dropped feature, but
2097 * this is okay, reader will skip the mising entries
2099 err = do_write(fd, feat_sec, sec_size);
2101 pr_debug("failed to write feature section\n");
2106 int perf_header__write_pipe(int fd)
2108 struct perf_pipe_file_header f_header;
2111 f_header = (struct perf_pipe_file_header){
2112 .magic = PERF_MAGIC,
2113 .size = sizeof(f_header),
2116 err = do_write(fd, &f_header, sizeof(f_header));
2118 pr_debug("failed to write perf pipe header\n");
2125 int perf_session__write_header(struct perf_session *session,
2126 struct perf_evlist *evlist,
2127 int fd, bool at_exit)
2129 struct perf_file_header f_header;
2130 struct perf_file_attr f_attr;
2131 struct perf_header *header = &session->header;
2132 struct perf_evsel *evsel, *pair = NULL;
2135 lseek(fd, sizeof(f_header), SEEK_SET);
2137 if (session->evlist != evlist)
2138 pair = perf_evlist__first(session->evlist);
2140 list_for_each_entry(evsel, &evlist->entries, node) {
2141 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2142 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2145 pr_debug("failed to write perf header\n");
2148 if (session->evlist != evlist) {
2149 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
2152 evsel->ids += pair->ids;
2153 pair = perf_evsel__next(pair);
2157 header->attr_offset = lseek(fd, 0, SEEK_CUR);
2159 list_for_each_entry(evsel, &evlist->entries, node) {
2160 f_attr = (struct perf_file_attr){
2161 .attr = evsel->attr,
2163 .offset = evsel->id_offset,
2164 .size = evsel->ids * sizeof(u64),
2167 err = do_write(fd, &f_attr, sizeof(f_attr));
2169 pr_debug("failed to write perf header attribute\n");
2174 header->event_offset = lseek(fd, 0, SEEK_CUR);
2175 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
2177 err = do_write(fd, trace_events, header->event_size);
2179 pr_debug("failed to write perf header events\n");
2184 header->data_offset = lseek(fd, 0, SEEK_CUR);
2187 err = perf_header__adds_write(header, evlist, fd);
2192 f_header = (struct perf_file_header){
2193 .magic = PERF_MAGIC,
2194 .size = sizeof(f_header),
2195 .attr_size = sizeof(f_attr),
2197 .offset = header->attr_offset,
2198 .size = evlist->nr_entries * sizeof(f_attr),
2201 .offset = header->data_offset,
2202 .size = header->data_size,
2205 .offset = header->event_offset,
2206 .size = header->event_size,
2210 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2212 lseek(fd, 0, SEEK_SET);
2213 err = do_write(fd, &f_header, sizeof(f_header));
2215 pr_debug("failed to write perf header\n");
2218 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2224 static int perf_header__getbuffer64(struct perf_header *header,
2225 int fd, void *buf, size_t size)
2227 if (readn(fd, buf, size) <= 0)
2230 if (header->needs_swap)
2231 mem_bswap_64(buf, size);
2236 int perf_header__process_sections(struct perf_header *header, int fd,
2238 int (*process)(struct perf_file_section *section,
2239 struct perf_header *ph,
2240 int feat, int fd, void *data))
2242 struct perf_file_section *feat_sec, *sec;
2248 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2252 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
2256 sec_size = sizeof(*feat_sec) * nr_sections;
2258 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2260 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2264 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2265 err = process(sec++, header, feat, fd, data);
2275 static const int attr_file_abi_sizes[] = {
2276 [0] = PERF_ATTR_SIZE_VER0,
2277 [1] = PERF_ATTR_SIZE_VER1,
2278 [2] = PERF_ATTR_SIZE_VER2,
2279 [3] = PERF_ATTR_SIZE_VER3,
2284 * In the legacy file format, the magic number is not used to encode endianness.
2285 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2286 * on ABI revisions, we need to try all combinations for all endianness to
2287 * detect the endianness.
2289 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2291 uint64_t ref_size, attr_size;
2294 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2295 ref_size = attr_file_abi_sizes[i]
2296 + sizeof(struct perf_file_section);
2297 if (hdr_sz != ref_size) {
2298 attr_size = bswap_64(hdr_sz);
2299 if (attr_size != ref_size)
2302 ph->needs_swap = true;
2304 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2309 /* could not determine endianness */
2313 #define PERF_PIPE_HDR_VER0 16
2315 static const size_t attr_pipe_abi_sizes[] = {
2316 [0] = PERF_PIPE_HDR_VER0,
2321 * In the legacy pipe format, there is an implicit assumption that endiannesss
2322 * between host recording the samples, and host parsing the samples is the
2323 * same. This is not always the case given that the pipe output may always be
2324 * redirected into a file and analyzed on a different machine with possibly a
2325 * different endianness and perf_event ABI revsions in the perf tool itself.
2327 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2332 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2333 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2334 attr_size = bswap_64(hdr_sz);
2335 if (attr_size != hdr_sz)
2338 ph->needs_swap = true;
2340 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2346 bool is_perf_magic(u64 magic)
2348 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2349 || magic == __perf_magic2
2350 || magic == __perf_magic2_sw)
2356 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2357 bool is_pipe, struct perf_header *ph)
2361 /* check for legacy format */
2362 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2364 pr_debug("legacy perf.data format\n");
2366 return try_all_pipe_abis(hdr_sz, ph);
2368 return try_all_file_abis(hdr_sz, ph);
2371 * the new magic number serves two purposes:
2372 * - unique number to identify actual perf.data files
2373 * - encode endianness of file
2376 /* check magic number with one endianness */
2377 if (magic == __perf_magic2)
2380 /* check magic number with opposite endianness */
2381 if (magic != __perf_magic2_sw)
2384 ph->needs_swap = true;
2389 int perf_file_header__read(struct perf_file_header *header,
2390 struct perf_header *ph, int fd)
2394 lseek(fd, 0, SEEK_SET);
2396 ret = readn(fd, header, sizeof(*header));
2400 if (check_magic_endian(header->magic,
2401 header->attr_size, false, ph) < 0) {
2402 pr_debug("magic/endian check failed\n");
2406 if (ph->needs_swap) {
2407 mem_bswap_64(header, offsetof(struct perf_file_header,
2411 if (header->size != sizeof(*header)) {
2412 /* Support the previous format */
2413 if (header->size == offsetof(typeof(*header), adds_features))
2414 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2417 } else if (ph->needs_swap) {
2419 * feature bitmap is declared as an array of unsigned longs --
2420 * not good since its size can differ between the host that
2421 * generated the data file and the host analyzing the file.
2423 * We need to handle endianness, but we don't know the size of
2424 * the unsigned long where the file was generated. Take a best
2425 * guess at determining it: try 64-bit swap first (ie., file
2426 * created on a 64-bit host), and check if the hostname feature
2427 * bit is set (this feature bit is forced on as of fbe96f2).
2428 * If the bit is not, undo the 64-bit swap and try a 32-bit
2429 * swap. If the hostname bit is still not set (e.g., older data
2430 * file), punt and fallback to the original behavior --
2431 * clearing all feature bits and setting buildid.
2433 mem_bswap_64(&header->adds_features,
2434 BITS_TO_U64(HEADER_FEAT_BITS));
2436 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2438 mem_bswap_64(&header->adds_features,
2439 BITS_TO_U64(HEADER_FEAT_BITS));
2442 mem_bswap_32(&header->adds_features,
2443 BITS_TO_U32(HEADER_FEAT_BITS));
2446 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2447 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2448 set_bit(HEADER_BUILD_ID, header->adds_features);
2452 memcpy(&ph->adds_features, &header->adds_features,
2453 sizeof(ph->adds_features));
2455 ph->event_offset = header->event_types.offset;
2456 ph->event_size = header->event_types.size;
2457 ph->data_offset = header->data.offset;
2458 ph->data_size = header->data.size;
2462 static int perf_file_section__process(struct perf_file_section *section,
2463 struct perf_header *ph,
2464 int feat, int fd, void *data)
2466 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2467 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2468 "%d, continuing...\n", section->offset, feat);
2472 if (feat >= HEADER_LAST_FEATURE) {
2473 pr_debug("unknown feature %d, continuing...\n", feat);
2477 if (!feat_ops[feat].process)
2480 return feat_ops[feat].process(section, ph, fd, data);
2483 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2484 struct perf_header *ph, int fd,
2489 ret = readn(fd, header, sizeof(*header));
2493 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2494 pr_debug("endian/magic failed\n");
2499 header->size = bswap_64(header->size);
2501 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2507 static int perf_header__read_pipe(struct perf_session *session, int fd)
2509 struct perf_header *header = &session->header;
2510 struct perf_pipe_file_header f_header;
2512 if (perf_file_header__read_pipe(&f_header, header, fd,
2513 session->repipe) < 0) {
2514 pr_debug("incompatible file format\n");
2523 static int read_attr(int fd, struct perf_header *ph,
2524 struct perf_file_attr *f_attr)
2526 struct perf_event_attr *attr = &f_attr->attr;
2528 size_t our_sz = sizeof(f_attr->attr);
2531 memset(f_attr, 0, sizeof(*f_attr));
2533 /* read minimal guaranteed structure */
2534 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2536 pr_debug("cannot read %d bytes of header attr\n",
2537 PERF_ATTR_SIZE_VER0);
2541 /* on file perf_event_attr size */
2549 sz = PERF_ATTR_SIZE_VER0;
2550 } else if (sz > our_sz) {
2551 pr_debug("file uses a more recent and unsupported ABI"
2552 " (%zu bytes extra)\n", sz - our_sz);
2555 /* what we have not yet read and that we know about */
2556 left = sz - PERF_ATTR_SIZE_VER0;
2559 ptr += PERF_ATTR_SIZE_VER0;
2561 ret = readn(fd, ptr, left);
2563 /* read perf_file_section, ids are read in caller */
2564 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2566 return ret <= 0 ? -1 : 0;
2569 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2570 struct pevent *pevent)
2572 struct event_format *event;
2575 /* already prepared */
2576 if (evsel->tp_format)
2579 event = pevent_find_event(pevent, evsel->attr.config);
2584 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2585 evsel->name = strdup(bf);
2586 if (evsel->name == NULL)
2590 evsel->tp_format = event;
2594 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2595 struct pevent *pevent)
2597 struct perf_evsel *pos;
2599 list_for_each_entry(pos, &evlist->entries, node) {
2600 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2601 perf_evsel__prepare_tracepoint_event(pos, pevent))
2608 int perf_session__read_header(struct perf_session *session, int fd)
2610 struct perf_header *header = &session->header;
2611 struct perf_file_header f_header;
2612 struct perf_file_attr f_attr;
2614 int nr_attrs, nr_ids, i, j;
2616 session->evlist = perf_evlist__new(NULL, NULL);
2617 if (session->evlist == NULL)
2620 if (session->fd_pipe)
2621 return perf_header__read_pipe(session, fd);
2623 if (perf_file_header__read(&f_header, header, fd) < 0)
2626 nr_attrs = f_header.attrs.size / f_header.attr_size;
2627 lseek(fd, f_header.attrs.offset, SEEK_SET);
2629 for (i = 0; i < nr_attrs; i++) {
2630 struct perf_evsel *evsel;
2633 if (read_attr(fd, header, &f_attr) < 0)
2636 if (header->needs_swap)
2637 perf_event__attr_swap(&f_attr.attr);
2639 tmp = lseek(fd, 0, SEEK_CUR);
2640 evsel = perf_evsel__new(&f_attr.attr, i);
2643 goto out_delete_evlist;
2645 evsel->needs_swap = header->needs_swap;
2647 * Do it before so that if perf_evsel__alloc_id fails, this
2648 * entry gets purged too at perf_evlist__delete().
2650 perf_evlist__add(session->evlist, evsel);
2652 nr_ids = f_attr.ids.size / sizeof(u64);
2654 * We don't have the cpu and thread maps on the header, so
2655 * for allocating the perf_sample_id table we fake 1 cpu and
2656 * hattr->ids threads.
2658 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2659 goto out_delete_evlist;
2661 lseek(fd, f_attr.ids.offset, SEEK_SET);
2663 for (j = 0; j < nr_ids; j++) {
2664 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2667 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2670 lseek(fd, tmp, SEEK_SET);
2673 symbol_conf.nr_events = nr_attrs;
2675 if (f_header.event_types.size) {
2676 lseek(fd, f_header.event_types.offset, SEEK_SET);
2677 trace_events = malloc(f_header.event_types.size);
2678 if (trace_events == NULL)
2680 if (perf_header__getbuffer64(header, fd, trace_events,
2681 f_header.event_types.size))
2683 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2686 perf_header__process_sections(header, fd, &session->pevent,
2687 perf_file_section__process);
2689 lseek(fd, header->data_offset, SEEK_SET);
2691 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2693 goto out_delete_evlist;
2701 perf_evlist__delete(session->evlist);
2702 session->evlist = NULL;
2706 int perf_event__synthesize_attr(struct perf_tool *tool,
2707 struct perf_event_attr *attr, u32 ids, u64 *id,
2708 perf_event__handler_t process)
2710 union perf_event *ev;
2714 size = sizeof(struct perf_event_attr);
2715 size = PERF_ALIGN(size, sizeof(u64));
2716 size += sizeof(struct perf_event_header);
2717 size += ids * sizeof(u64);
2724 ev->attr.attr = *attr;
2725 memcpy(ev->attr.id, id, ids * sizeof(u64));
2727 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2728 ev->attr.header.size = (u16)size;
2730 if (ev->attr.header.size == size)
2731 err = process(tool, ev, NULL, NULL);
2740 int perf_event__synthesize_attrs(struct perf_tool *tool,
2741 struct perf_session *session,
2742 perf_event__handler_t process)
2744 struct perf_evsel *evsel;
2747 list_for_each_entry(evsel, &session->evlist->entries, node) {
2748 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2749 evsel->id, process);
2751 pr_debug("failed to create perf header attribute\n");
2759 int perf_event__process_attr(union perf_event *event,
2760 struct perf_evlist **pevlist)
2763 struct perf_evsel *evsel;
2764 struct perf_evlist *evlist = *pevlist;
2766 if (evlist == NULL) {
2767 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2772 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2776 perf_evlist__add(evlist, evsel);
2778 ids = event->header.size;
2779 ids -= (void *)&event->attr.id - (void *)event;
2780 n_ids = ids / sizeof(u64);
2782 * We don't have the cpu and thread maps on the header, so
2783 * for allocating the perf_sample_id table we fake 1 cpu and
2784 * hattr->ids threads.
2786 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2789 for (i = 0; i < n_ids; i++) {
2790 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2796 int perf_event__synthesize_event_type(struct perf_tool *tool,
2797 u64 event_id, char *name,
2798 perf_event__handler_t process,
2799 struct machine *machine)
2801 union perf_event ev;
2805 memset(&ev, 0, sizeof(ev));
2807 ev.event_type.event_type.event_id = event_id;
2808 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2809 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2811 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2812 size = strlen(ev.event_type.event_type.name);
2813 size = PERF_ALIGN(size, sizeof(u64));
2814 ev.event_type.header.size = sizeof(ev.event_type) -
2815 (sizeof(ev.event_type.event_type.name) - size);
2817 err = process(tool, &ev, NULL, machine);
2822 int perf_event__synthesize_event_types(struct perf_tool *tool,
2823 perf_event__handler_t process,
2824 struct machine *machine)
2826 struct perf_trace_event_type *type;
2829 for (i = 0; i < trace_event_count; i++) {
2830 type = &trace_events[i];
2832 err = perf_event__synthesize_event_type(tool, type->event_id,
2833 type->name, process,
2836 pr_debug("failed to create perf header event type\n");
2844 int perf_event__process_event_type(struct perf_tool *tool __maybe_unused,
2845 union perf_event *event)
2847 if (perf_header__push_event(event->event_type.event_type.event_id,
2848 event->event_type.event_type.name) < 0)
2854 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2855 struct perf_evlist *evlist,
2856 perf_event__handler_t process)
2858 union perf_event ev;
2859 struct tracing_data *tdata;
2860 ssize_t size = 0, aligned_size = 0, padding;
2861 int err __maybe_unused = 0;
2864 * We are going to store the size of the data followed
2865 * by the data contents. Since the fd descriptor is a pipe,
2866 * we cannot seek back to store the size of the data once
2867 * we know it. Instead we:
2869 * - write the tracing data to the temp file
2870 * - get/write the data size to pipe
2871 * - write the tracing data from the temp file
2874 tdata = tracing_data_get(&evlist->entries, fd, true);
2878 memset(&ev, 0, sizeof(ev));
2880 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2882 aligned_size = PERF_ALIGN(size, sizeof(u64));
2883 padding = aligned_size - size;
2884 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2885 ev.tracing_data.size = aligned_size;
2887 process(tool, &ev, NULL, NULL);
2890 * The put function will copy all the tracing data
2891 * stored in temp file to the pipe.
2893 tracing_data_put(tdata);
2895 write_padded(fd, NULL, 0, padding);
2897 return aligned_size;
2900 int perf_event__process_tracing_data(union perf_event *event,
2901 struct perf_session *session)
2903 ssize_t size_read, padding, size = event->tracing_data.size;
2904 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2907 /* setup for reading amidst mmap */
2908 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2911 size_read = trace_report(session->fd, &session->pevent,
2913 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2915 if (read(session->fd, buf, padding) < 0)
2916 die("reading input file");
2917 if (session->repipe) {
2918 int retw = write(STDOUT_FILENO, buf, padding);
2919 if (retw <= 0 || retw != padding)
2920 die("repiping tracing data padding");
2923 if (size_read + padding != size)
2924 die("tracing data size mismatch");
2926 perf_evlist__prepare_tracepoint_events(session->evlist,
2929 return size_read + padding;
2932 int perf_event__synthesize_build_id(struct perf_tool *tool,
2933 struct dso *pos, u16 misc,
2934 perf_event__handler_t process,
2935 struct machine *machine)
2937 union perf_event ev;
2944 memset(&ev, 0, sizeof(ev));
2946 len = pos->long_name_len + 1;
2947 len = PERF_ALIGN(len, NAME_ALIGN);
2948 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2949 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2950 ev.build_id.header.misc = misc;
2951 ev.build_id.pid = machine->pid;
2952 ev.build_id.header.size = sizeof(ev.build_id) + len;
2953 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2955 err = process(tool, &ev, NULL, machine);
2960 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2961 union perf_event *event,
2962 struct perf_session *session)
2964 __event_process_build_id(&event->build_id,
2965 event->build_id.filename,
2970 void disable_buildid_cache(void)
2972 no_buildid_cache = true;