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->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);
1056 /* write real pmu_num later */
1057 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1061 while ((pmu = perf_pmu__scan(pmu))) {
1066 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1070 ret = do_write_string(fd, pmu->name);
1075 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1077 lseek(fd, offset, SEEK_SET);
1085 * default get_cpuid(): nothing gets recorded
1086 * actual implementation must be in arch/$(ARCH)/util/header.c
1088 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1089 size_t sz __maybe_unused)
1094 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1095 struct perf_evlist *evlist __maybe_unused)
1100 ret = get_cpuid(buffer, sizeof(buffer));
1106 return do_write_string(fd, buffer);
1109 static int write_branch_stack(int fd __maybe_unused,
1110 struct perf_header *h __maybe_unused,
1111 struct perf_evlist *evlist __maybe_unused)
1116 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1119 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1122 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1125 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1128 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1130 fprintf(fp, "# arch : %s\n", ph->env.arch);
1133 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1136 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1139 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1142 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1143 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1146 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1149 fprintf(fp, "# perf version : %s\n", ph->env.version);
1152 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1158 nr = ph->env.nr_cmdline;
1159 str = ph->env.cmdline;
1161 fprintf(fp, "# cmdline : ");
1163 for (i = 0; i < nr; i++) {
1164 fprintf(fp, "%s ", str);
1165 str += strlen(str) + 1;
1170 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1176 nr = ph->env.nr_sibling_cores;
1177 str = ph->env.sibling_cores;
1179 for (i = 0; i < nr; i++) {
1180 fprintf(fp, "# sibling cores : %s\n", str);
1181 str += strlen(str) + 1;
1184 nr = ph->env.nr_sibling_threads;
1185 str = ph->env.sibling_threads;
1187 for (i = 0; i < nr; i++) {
1188 fprintf(fp, "# sibling threads : %s\n", str);
1189 str += strlen(str) + 1;
1193 static void free_event_desc(struct perf_evsel *events)
1195 struct perf_evsel *evsel;
1200 for (evsel = events; evsel->attr.size; evsel++) {
1210 static struct perf_evsel *
1211 read_event_desc(struct perf_header *ph, int fd)
1213 struct perf_evsel *evsel, *events = NULL;
1216 u32 nre, sz, nr, i, j;
1220 /* number of events */
1221 ret = readn(fd, &nre, sizeof(nre));
1222 if (ret != (ssize_t)sizeof(nre))
1226 nre = bswap_32(nre);
1228 ret = readn(fd, &sz, sizeof(sz));
1229 if (ret != (ssize_t)sizeof(sz))
1235 /* buffer to hold on file attr struct */
1240 /* the last event terminates with evsel->attr.size == 0: */
1241 events = calloc(nre + 1, sizeof(*events));
1245 msz = sizeof(evsel->attr);
1249 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1253 * must read entire on-file attr struct to
1254 * sync up with layout.
1256 ret = readn(fd, buf, sz);
1257 if (ret != (ssize_t)sz)
1261 perf_event__attr_swap(buf);
1263 memcpy(&evsel->attr, buf, msz);
1265 ret = readn(fd, &nr, sizeof(nr));
1266 if (ret != (ssize_t)sizeof(nr))
1269 if (ph->needs_swap) {
1271 evsel->needs_swap = true;
1274 evsel->name = do_read_string(fd, ph);
1279 id = calloc(nr, sizeof(*id));
1285 for (j = 0 ; j < nr; j++) {
1286 ret = readn(fd, id, sizeof(*id));
1287 if (ret != (ssize_t)sizeof(*id))
1290 *id = bswap_64(*id);
1300 free_event_desc(events);
1305 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1307 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1312 fprintf(fp, "# event desc: not available or unable to read\n");
1316 for (evsel = events; evsel->attr.size; evsel++) {
1317 fprintf(fp, "# event : name = %s, ", evsel->name);
1319 fprintf(fp, "type = %d, config = 0x%"PRIx64
1320 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1322 (u64)evsel->attr.config,
1323 (u64)evsel->attr.config1,
1324 (u64)evsel->attr.config2);
1326 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1327 evsel->attr.exclude_user,
1328 evsel->attr.exclude_kernel);
1330 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1331 evsel->attr.exclude_host,
1332 evsel->attr.exclude_guest);
1334 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1337 fprintf(fp, ", id = {");
1338 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1341 fprintf(fp, " %"PRIu64, *id);
1349 free_event_desc(events);
1352 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1355 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1358 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1363 uint64_t mem_total, mem_free;
1366 nr = ph->env.nr_numa_nodes;
1367 str = ph->env.numa_nodes;
1369 for (i = 0; i < nr; i++) {
1371 c = strtoul(str, &tmp, 0);
1376 mem_total = strtoull(str, &tmp, 0);
1381 mem_free = strtoull(str, &tmp, 0);
1385 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1386 " free = %"PRIu64" kB\n",
1387 c, mem_total, mem_free);
1390 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1392 str += strlen(str) + 1;
1396 fprintf(fp, "# numa topology : not available\n");
1399 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1401 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1404 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1405 int fd __maybe_unused, FILE *fp)
1407 fprintf(fp, "# contains samples with branch stack\n");
1410 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1413 const char *delimiter = "# pmu mappings: ";
1418 pmu_num = ph->env.nr_pmu_mappings;
1420 fprintf(fp, "# pmu mappings: not available\n");
1424 str = ph->env.pmu_mappings;
1427 type = strtoul(str, &tmp, 0);
1432 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1435 str += strlen(str) + 1;
1444 fprintf(fp, "# pmu mappings: unable to read\n");
1447 static int __event_process_build_id(struct build_id_event *bev,
1449 struct perf_session *session)
1452 struct list_head *head;
1453 struct machine *machine;
1456 enum dso_kernel_type dso_type;
1458 machine = perf_session__findnew_machine(session, bev->pid);
1462 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1465 case PERF_RECORD_MISC_KERNEL:
1466 dso_type = DSO_TYPE_KERNEL;
1467 head = &machine->kernel_dsos;
1469 case PERF_RECORD_MISC_GUEST_KERNEL:
1470 dso_type = DSO_TYPE_GUEST_KERNEL;
1471 head = &machine->kernel_dsos;
1473 case PERF_RECORD_MISC_USER:
1474 case PERF_RECORD_MISC_GUEST_USER:
1475 dso_type = DSO_TYPE_USER;
1476 head = &machine->user_dsos;
1482 dso = __dsos__findnew(head, filename);
1484 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1486 dso__set_build_id(dso, &bev->build_id);
1488 if (filename[0] == '[')
1489 dso->kernel = dso_type;
1491 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1493 pr_debug("build id event received for %s: %s\n",
1494 dso->long_name, sbuild_id);
1502 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1503 int input, u64 offset, u64 size)
1505 struct perf_session *session = container_of(header, struct perf_session, header);
1507 struct perf_event_header header;
1508 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1511 struct build_id_event bev;
1512 char filename[PATH_MAX];
1513 u64 limit = offset + size;
1515 while (offset < limit) {
1518 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1521 if (header->needs_swap)
1522 perf_event_header__bswap(&old_bev.header);
1524 len = old_bev.header.size - sizeof(old_bev);
1525 if (readn(input, filename, len) != len)
1528 bev.header = old_bev.header;
1531 * As the pid is the missing value, we need to fill
1532 * it properly. The header.misc value give us nice hint.
1534 bev.pid = HOST_KERNEL_ID;
1535 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1536 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1537 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1539 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1540 __event_process_build_id(&bev, filename, session);
1542 offset += bev.header.size;
1548 static int perf_header__read_build_ids(struct perf_header *header,
1549 int input, u64 offset, u64 size)
1551 struct perf_session *session = container_of(header, struct perf_session, header);
1552 struct build_id_event bev;
1553 char filename[PATH_MAX];
1554 u64 limit = offset + size, orig_offset = offset;
1557 while (offset < limit) {
1560 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1563 if (header->needs_swap)
1564 perf_event_header__bswap(&bev.header);
1566 len = bev.header.size - sizeof(bev);
1567 if (readn(input, filename, len) != len)
1570 * The a1645ce1 changeset:
1572 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1574 * Added a field to struct build_id_event that broke the file
1577 * Since the kernel build-id is the first entry, process the
1578 * table using the old format if the well known
1579 * '[kernel.kallsyms]' string for the kernel build-id has the
1580 * first 4 characters chopped off (where the pid_t sits).
1582 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1583 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1585 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1588 __event_process_build_id(&bev, filename, session);
1590 offset += bev.header.size;
1597 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1598 struct perf_header *ph __maybe_unused,
1601 trace_report(fd, data, false);
1605 static int process_build_id(struct perf_file_section *section,
1606 struct perf_header *ph, int fd,
1607 void *data __maybe_unused)
1609 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1610 pr_debug("Failed to read buildids, continuing...\n");
1614 static int process_hostname(struct perf_file_section *section __maybe_unused,
1615 struct perf_header *ph, int fd,
1616 void *data __maybe_unused)
1618 ph->env.hostname = do_read_string(fd, ph);
1619 return ph->env.hostname ? 0 : -ENOMEM;
1622 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1623 struct perf_header *ph, int fd,
1624 void *data __maybe_unused)
1626 ph->env.os_release = do_read_string(fd, ph);
1627 return ph->env.os_release ? 0 : -ENOMEM;
1630 static int process_version(struct perf_file_section *section __maybe_unused,
1631 struct perf_header *ph, int fd,
1632 void *data __maybe_unused)
1634 ph->env.version = do_read_string(fd, ph);
1635 return ph->env.version ? 0 : -ENOMEM;
1638 static int process_arch(struct perf_file_section *section __maybe_unused,
1639 struct perf_header *ph, int fd,
1640 void *data __maybe_unused)
1642 ph->env.arch = do_read_string(fd, ph);
1643 return ph->env.arch ? 0 : -ENOMEM;
1646 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1647 struct perf_header *ph, int fd,
1648 void *data __maybe_unused)
1653 ret = readn(fd, &nr, sizeof(nr));
1654 if (ret != sizeof(nr))
1660 ph->env.nr_cpus_online = nr;
1662 ret = readn(fd, &nr, sizeof(nr));
1663 if (ret != sizeof(nr))
1669 ph->env.nr_cpus_avail = nr;
1673 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1674 struct perf_header *ph, int fd,
1675 void *data __maybe_unused)
1677 ph->env.cpu_desc = do_read_string(fd, ph);
1678 return ph->env.cpu_desc ? 0 : -ENOMEM;
1681 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1682 struct perf_header *ph, int fd,
1683 void *data __maybe_unused)
1685 ph->env.cpuid = do_read_string(fd, ph);
1686 return ph->env.cpuid ? 0 : -ENOMEM;
1689 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1690 struct perf_header *ph, int fd,
1691 void *data __maybe_unused)
1696 ret = readn(fd, &mem, sizeof(mem));
1697 if (ret != sizeof(mem))
1701 mem = bswap_64(mem);
1703 ph->env.total_mem = mem;
1707 static struct perf_evsel *
1708 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1710 struct perf_evsel *evsel;
1712 list_for_each_entry(evsel, &evlist->entries, node) {
1713 if (evsel->idx == idx)
1721 perf_evlist__set_event_name(struct perf_evlist *evlist,
1722 struct perf_evsel *event)
1724 struct perf_evsel *evsel;
1729 evsel = perf_evlist__find_by_index(evlist, event->idx);
1736 evsel->name = strdup(event->name);
1740 process_event_desc(struct perf_file_section *section __maybe_unused,
1741 struct perf_header *header, int fd,
1742 void *data __maybe_unused)
1744 struct perf_session *session;
1745 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1750 session = container_of(header, struct perf_session, header);
1751 for (evsel = events; evsel->attr.size; evsel++)
1752 perf_evlist__set_event_name(session->evlist, evsel);
1754 free_event_desc(events);
1759 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1760 struct perf_header *ph, int fd,
1761 void *data __maybe_unused)
1768 ret = readn(fd, &nr, sizeof(nr));
1769 if (ret != sizeof(nr))
1775 ph->env.nr_cmdline = nr;
1776 strbuf_init(&sb, 128);
1778 for (i = 0; i < nr; i++) {
1779 str = do_read_string(fd, ph);
1783 /* include a NULL character at the end */
1784 strbuf_add(&sb, str, strlen(str) + 1);
1787 ph->env.cmdline = strbuf_detach(&sb, NULL);
1791 strbuf_release(&sb);
1795 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1796 struct perf_header *ph, int fd,
1797 void *data __maybe_unused)
1804 ret = readn(fd, &nr, sizeof(nr));
1805 if (ret != sizeof(nr))
1811 ph->env.nr_sibling_cores = nr;
1812 strbuf_init(&sb, 128);
1814 for (i = 0; i < nr; i++) {
1815 str = do_read_string(fd, ph);
1819 /* include a NULL character at the end */
1820 strbuf_add(&sb, str, strlen(str) + 1);
1823 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1825 ret = readn(fd, &nr, sizeof(nr));
1826 if (ret != sizeof(nr))
1832 ph->env.nr_sibling_threads = nr;
1834 for (i = 0; i < nr; i++) {
1835 str = do_read_string(fd, ph);
1839 /* include a NULL character at the end */
1840 strbuf_add(&sb, str, strlen(str) + 1);
1843 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1847 strbuf_release(&sb);
1851 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1852 struct perf_header *ph, int fd,
1853 void *data __maybe_unused)
1858 uint64_t mem_total, mem_free;
1862 ret = readn(fd, &nr, sizeof(nr));
1863 if (ret != sizeof(nr))
1869 ph->env.nr_numa_nodes = nr;
1870 strbuf_init(&sb, 256);
1872 for (i = 0; i < nr; i++) {
1874 ret = readn(fd, &node, sizeof(node));
1875 if (ret != sizeof(node))
1878 ret = readn(fd, &mem_total, sizeof(u64));
1879 if (ret != sizeof(u64))
1882 ret = readn(fd, &mem_free, sizeof(u64));
1883 if (ret != sizeof(u64))
1886 if (ph->needs_swap) {
1887 node = bswap_32(node);
1888 mem_total = bswap_64(mem_total);
1889 mem_free = bswap_64(mem_free);
1892 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1893 node, mem_total, mem_free);
1895 str = do_read_string(fd, ph);
1899 /* include a NULL character at the end */
1900 strbuf_add(&sb, str, strlen(str) + 1);
1903 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1907 strbuf_release(&sb);
1911 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1912 struct perf_header *ph, int fd,
1913 void *data __maybe_unused)
1921 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1922 if (ret != sizeof(pmu_num))
1926 pmu_num = bswap_32(pmu_num);
1929 pr_debug("pmu mappings not available\n");
1933 ph->env.nr_pmu_mappings = pmu_num;
1934 strbuf_init(&sb, 128);
1937 if (readn(fd, &type, sizeof(type)) != sizeof(type))
1940 type = bswap_32(type);
1942 name = do_read_string(fd, ph);
1946 strbuf_addf(&sb, "%u:%s", type, name);
1947 /* include a NULL character at the end */
1948 strbuf_add(&sb, "", 1);
1953 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1957 strbuf_release(&sb);
1961 struct feature_ops {
1962 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1963 void (*print)(struct perf_header *h, int fd, FILE *fp);
1964 int (*process)(struct perf_file_section *section,
1965 struct perf_header *h, int fd, void *data);
1970 #define FEAT_OPA(n, func) \
1971 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1972 #define FEAT_OPP(n, func) \
1973 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1974 .process = process_##func }
1975 #define FEAT_OPF(n, func) \
1976 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1977 .process = process_##func, .full_only = true }
1979 /* feature_ops not implemented: */
1980 #define print_tracing_data NULL
1981 #define print_build_id NULL
1983 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1984 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1985 FEAT_OPP(HEADER_BUILD_ID, build_id),
1986 FEAT_OPP(HEADER_HOSTNAME, hostname),
1987 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1988 FEAT_OPP(HEADER_VERSION, version),
1989 FEAT_OPP(HEADER_ARCH, arch),
1990 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1991 FEAT_OPP(HEADER_CPUDESC, cpudesc),
1992 FEAT_OPP(HEADER_CPUID, cpuid),
1993 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
1994 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
1995 FEAT_OPP(HEADER_CMDLINE, cmdline),
1996 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1997 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1998 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
1999 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2002 struct header_print_data {
2004 bool full; /* extended list of headers */
2007 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2008 struct perf_header *ph,
2009 int feat, int fd, void *data)
2011 struct header_print_data *hd = data;
2013 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2014 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2015 "%d, continuing...\n", section->offset, feat);
2018 if (feat >= HEADER_LAST_FEATURE) {
2019 pr_warning("unknown feature %d\n", feat);
2022 if (!feat_ops[feat].print)
2025 if (!feat_ops[feat].full_only || hd->full)
2026 feat_ops[feat].print(ph, fd, hd->fp);
2028 fprintf(hd->fp, "# %s info available, use -I to display\n",
2029 feat_ops[feat].name);
2034 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2036 struct header_print_data hd;
2037 struct perf_header *header = &session->header;
2038 int fd = session->fd;
2042 perf_header__process_sections(header, fd, &hd,
2043 perf_file_section__fprintf_info);
2047 static int do_write_feat(int fd, struct perf_header *h, int type,
2048 struct perf_file_section **p,
2049 struct perf_evlist *evlist)
2054 if (perf_header__has_feat(h, type)) {
2055 if (!feat_ops[type].write)
2058 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2060 err = feat_ops[type].write(fd, h, evlist);
2062 pr_debug("failed to write feature %d\n", type);
2064 /* undo anything written */
2065 lseek(fd, (*p)->offset, SEEK_SET);
2069 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2075 static int perf_header__adds_write(struct perf_header *header,
2076 struct perf_evlist *evlist, int fd)
2079 struct perf_file_section *feat_sec, *p;
2085 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2089 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
2090 if (feat_sec == NULL)
2093 sec_size = sizeof(*feat_sec) * nr_sections;
2095 sec_start = header->data_offset + header->data_size;
2096 lseek(fd, sec_start + sec_size, SEEK_SET);
2098 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2099 if (do_write_feat(fd, header, feat, &p, evlist))
2100 perf_header__clear_feat(header, feat);
2103 lseek(fd, sec_start, SEEK_SET);
2105 * may write more than needed due to dropped feature, but
2106 * this is okay, reader will skip the mising entries
2108 err = do_write(fd, feat_sec, sec_size);
2110 pr_debug("failed to write feature section\n");
2115 int perf_header__write_pipe(int fd)
2117 struct perf_pipe_file_header f_header;
2120 f_header = (struct perf_pipe_file_header){
2121 .magic = PERF_MAGIC,
2122 .size = sizeof(f_header),
2125 err = do_write(fd, &f_header, sizeof(f_header));
2127 pr_debug("failed to write perf pipe header\n");
2134 int perf_session__write_header(struct perf_session *session,
2135 struct perf_evlist *evlist,
2136 int fd, bool at_exit)
2138 struct perf_file_header f_header;
2139 struct perf_file_attr f_attr;
2140 struct perf_header *header = &session->header;
2141 struct perf_evsel *evsel, *pair = NULL;
2144 lseek(fd, sizeof(f_header), SEEK_SET);
2146 if (session->evlist != evlist)
2147 pair = perf_evlist__first(session->evlist);
2149 list_for_each_entry(evsel, &evlist->entries, node) {
2150 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2151 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2154 pr_debug("failed to write perf header\n");
2157 if (session->evlist != evlist) {
2158 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
2161 evsel->ids += pair->ids;
2162 pair = perf_evsel__next(pair);
2166 header->attr_offset = lseek(fd, 0, SEEK_CUR);
2168 list_for_each_entry(evsel, &evlist->entries, node) {
2169 f_attr = (struct perf_file_attr){
2170 .attr = evsel->attr,
2172 .offset = evsel->id_offset,
2173 .size = evsel->ids * sizeof(u64),
2176 err = do_write(fd, &f_attr, sizeof(f_attr));
2178 pr_debug("failed to write perf header attribute\n");
2183 header->event_offset = lseek(fd, 0, SEEK_CUR);
2184 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
2186 err = do_write(fd, trace_events, header->event_size);
2188 pr_debug("failed to write perf header events\n");
2193 header->data_offset = lseek(fd, 0, SEEK_CUR);
2196 err = perf_header__adds_write(header, evlist, fd);
2201 f_header = (struct perf_file_header){
2202 .magic = PERF_MAGIC,
2203 .size = sizeof(f_header),
2204 .attr_size = sizeof(f_attr),
2206 .offset = header->attr_offset,
2207 .size = evlist->nr_entries * sizeof(f_attr),
2210 .offset = header->data_offset,
2211 .size = header->data_size,
2214 .offset = header->event_offset,
2215 .size = header->event_size,
2219 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2221 lseek(fd, 0, SEEK_SET);
2222 err = do_write(fd, &f_header, sizeof(f_header));
2224 pr_debug("failed to write perf header\n");
2227 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2233 static int perf_header__getbuffer64(struct perf_header *header,
2234 int fd, void *buf, size_t size)
2236 if (readn(fd, buf, size) <= 0)
2239 if (header->needs_swap)
2240 mem_bswap_64(buf, size);
2245 int perf_header__process_sections(struct perf_header *header, int fd,
2247 int (*process)(struct perf_file_section *section,
2248 struct perf_header *ph,
2249 int feat, int fd, void *data))
2251 struct perf_file_section *feat_sec, *sec;
2257 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2261 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
2265 sec_size = sizeof(*feat_sec) * nr_sections;
2267 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2269 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2273 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2274 err = process(sec++, header, feat, fd, data);
2284 static const int attr_file_abi_sizes[] = {
2285 [0] = PERF_ATTR_SIZE_VER0,
2286 [1] = PERF_ATTR_SIZE_VER1,
2287 [2] = PERF_ATTR_SIZE_VER2,
2288 [3] = PERF_ATTR_SIZE_VER3,
2293 * In the legacy file format, the magic number is not used to encode endianness.
2294 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2295 * on ABI revisions, we need to try all combinations for all endianness to
2296 * detect the endianness.
2298 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2300 uint64_t ref_size, attr_size;
2303 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2304 ref_size = attr_file_abi_sizes[i]
2305 + sizeof(struct perf_file_section);
2306 if (hdr_sz != ref_size) {
2307 attr_size = bswap_64(hdr_sz);
2308 if (attr_size != ref_size)
2311 ph->needs_swap = true;
2313 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2318 /* could not determine endianness */
2322 #define PERF_PIPE_HDR_VER0 16
2324 static const size_t attr_pipe_abi_sizes[] = {
2325 [0] = PERF_PIPE_HDR_VER0,
2330 * In the legacy pipe format, there is an implicit assumption that endiannesss
2331 * between host recording the samples, and host parsing the samples is the
2332 * same. This is not always the case given that the pipe output may always be
2333 * redirected into a file and analyzed on a different machine with possibly a
2334 * different endianness and perf_event ABI revsions in the perf tool itself.
2336 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2341 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2342 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2343 attr_size = bswap_64(hdr_sz);
2344 if (attr_size != hdr_sz)
2347 ph->needs_swap = true;
2349 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2355 bool is_perf_magic(u64 magic)
2357 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2358 || magic == __perf_magic2
2359 || magic == __perf_magic2_sw)
2365 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2366 bool is_pipe, struct perf_header *ph)
2370 /* check for legacy format */
2371 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2373 pr_debug("legacy perf.data format\n");
2375 return try_all_pipe_abis(hdr_sz, ph);
2377 return try_all_file_abis(hdr_sz, ph);
2380 * the new magic number serves two purposes:
2381 * - unique number to identify actual perf.data files
2382 * - encode endianness of file
2385 /* check magic number with one endianness */
2386 if (magic == __perf_magic2)
2389 /* check magic number with opposite endianness */
2390 if (magic != __perf_magic2_sw)
2393 ph->needs_swap = true;
2398 int perf_file_header__read(struct perf_file_header *header,
2399 struct perf_header *ph, int fd)
2403 lseek(fd, 0, SEEK_SET);
2405 ret = readn(fd, header, sizeof(*header));
2409 if (check_magic_endian(header->magic,
2410 header->attr_size, false, ph) < 0) {
2411 pr_debug("magic/endian check failed\n");
2415 if (ph->needs_swap) {
2416 mem_bswap_64(header, offsetof(struct perf_file_header,
2420 if (header->size != sizeof(*header)) {
2421 /* Support the previous format */
2422 if (header->size == offsetof(typeof(*header), adds_features))
2423 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2426 } else if (ph->needs_swap) {
2428 * feature bitmap is declared as an array of unsigned longs --
2429 * not good since its size can differ between the host that
2430 * generated the data file and the host analyzing the file.
2432 * We need to handle endianness, but we don't know the size of
2433 * the unsigned long where the file was generated. Take a best
2434 * guess at determining it: try 64-bit swap first (ie., file
2435 * created on a 64-bit host), and check if the hostname feature
2436 * bit is set (this feature bit is forced on as of fbe96f2).
2437 * If the bit is not, undo the 64-bit swap and try a 32-bit
2438 * swap. If the hostname bit is still not set (e.g., older data
2439 * file), punt and fallback to the original behavior --
2440 * clearing all feature bits and setting buildid.
2442 mem_bswap_64(&header->adds_features,
2443 BITS_TO_U64(HEADER_FEAT_BITS));
2445 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2447 mem_bswap_64(&header->adds_features,
2448 BITS_TO_U64(HEADER_FEAT_BITS));
2451 mem_bswap_32(&header->adds_features,
2452 BITS_TO_U32(HEADER_FEAT_BITS));
2455 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2456 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2457 set_bit(HEADER_BUILD_ID, header->adds_features);
2461 memcpy(&ph->adds_features, &header->adds_features,
2462 sizeof(ph->adds_features));
2464 ph->event_offset = header->event_types.offset;
2465 ph->event_size = header->event_types.size;
2466 ph->data_offset = header->data.offset;
2467 ph->data_size = header->data.size;
2471 static int perf_file_section__process(struct perf_file_section *section,
2472 struct perf_header *ph,
2473 int feat, int fd, void *data)
2475 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2476 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2477 "%d, continuing...\n", section->offset, feat);
2481 if (feat >= HEADER_LAST_FEATURE) {
2482 pr_debug("unknown feature %d, continuing...\n", feat);
2486 if (!feat_ops[feat].process)
2489 return feat_ops[feat].process(section, ph, fd, data);
2492 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2493 struct perf_header *ph, int fd,
2498 ret = readn(fd, header, sizeof(*header));
2502 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2503 pr_debug("endian/magic failed\n");
2508 header->size = bswap_64(header->size);
2510 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2516 static int perf_header__read_pipe(struct perf_session *session, int fd)
2518 struct perf_header *header = &session->header;
2519 struct perf_pipe_file_header f_header;
2521 if (perf_file_header__read_pipe(&f_header, header, fd,
2522 session->repipe) < 0) {
2523 pr_debug("incompatible file format\n");
2532 static int read_attr(int fd, struct perf_header *ph,
2533 struct perf_file_attr *f_attr)
2535 struct perf_event_attr *attr = &f_attr->attr;
2537 size_t our_sz = sizeof(f_attr->attr);
2540 memset(f_attr, 0, sizeof(*f_attr));
2542 /* read minimal guaranteed structure */
2543 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2545 pr_debug("cannot read %d bytes of header attr\n",
2546 PERF_ATTR_SIZE_VER0);
2550 /* on file perf_event_attr size */
2558 sz = PERF_ATTR_SIZE_VER0;
2559 } else if (sz > our_sz) {
2560 pr_debug("file uses a more recent and unsupported ABI"
2561 " (%zu bytes extra)\n", sz - our_sz);
2564 /* what we have not yet read and that we know about */
2565 left = sz - PERF_ATTR_SIZE_VER0;
2568 ptr += PERF_ATTR_SIZE_VER0;
2570 ret = readn(fd, ptr, left);
2572 /* read perf_file_section, ids are read in caller */
2573 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2575 return ret <= 0 ? -1 : 0;
2578 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2579 struct pevent *pevent)
2581 struct event_format *event;
2584 /* already prepared */
2585 if (evsel->tp_format)
2588 event = pevent_find_event(pevent, evsel->attr.config);
2593 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2594 evsel->name = strdup(bf);
2595 if (evsel->name == NULL)
2599 evsel->tp_format = event;
2603 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2604 struct pevent *pevent)
2606 struct perf_evsel *pos;
2608 list_for_each_entry(pos, &evlist->entries, node) {
2609 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2610 perf_evsel__prepare_tracepoint_event(pos, pevent))
2617 int perf_session__read_header(struct perf_session *session, int fd)
2619 struct perf_header *header = &session->header;
2620 struct perf_file_header f_header;
2621 struct perf_file_attr f_attr;
2623 int nr_attrs, nr_ids, i, j;
2625 session->evlist = perf_evlist__new(NULL, NULL);
2626 if (session->evlist == NULL)
2629 if (session->fd_pipe)
2630 return perf_header__read_pipe(session, fd);
2632 if (perf_file_header__read(&f_header, header, fd) < 0)
2635 nr_attrs = f_header.attrs.size / f_header.attr_size;
2636 lseek(fd, f_header.attrs.offset, SEEK_SET);
2638 for (i = 0; i < nr_attrs; i++) {
2639 struct perf_evsel *evsel;
2642 if (read_attr(fd, header, &f_attr) < 0)
2645 if (header->needs_swap)
2646 perf_event__attr_swap(&f_attr.attr);
2648 tmp = lseek(fd, 0, SEEK_CUR);
2649 evsel = perf_evsel__new(&f_attr.attr, i);
2652 goto out_delete_evlist;
2654 evsel->needs_swap = header->needs_swap;
2656 * Do it before so that if perf_evsel__alloc_id fails, this
2657 * entry gets purged too at perf_evlist__delete().
2659 perf_evlist__add(session->evlist, evsel);
2661 nr_ids = f_attr.ids.size / sizeof(u64);
2663 * We don't have the cpu and thread maps on the header, so
2664 * for allocating the perf_sample_id table we fake 1 cpu and
2665 * hattr->ids threads.
2667 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2668 goto out_delete_evlist;
2670 lseek(fd, f_attr.ids.offset, SEEK_SET);
2672 for (j = 0; j < nr_ids; j++) {
2673 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2676 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2679 lseek(fd, tmp, SEEK_SET);
2682 symbol_conf.nr_events = nr_attrs;
2684 if (f_header.event_types.size) {
2685 lseek(fd, f_header.event_types.offset, SEEK_SET);
2686 trace_events = malloc(f_header.event_types.size);
2687 if (trace_events == NULL)
2689 if (perf_header__getbuffer64(header, fd, trace_events,
2690 f_header.event_types.size))
2692 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2695 perf_header__process_sections(header, fd, &session->pevent,
2696 perf_file_section__process);
2698 lseek(fd, header->data_offset, SEEK_SET);
2700 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2702 goto out_delete_evlist;
2710 perf_evlist__delete(session->evlist);
2711 session->evlist = NULL;
2715 int perf_event__synthesize_attr(struct perf_tool *tool,
2716 struct perf_event_attr *attr, u32 ids, u64 *id,
2717 perf_event__handler_t process)
2719 union perf_event *ev;
2723 size = sizeof(struct perf_event_attr);
2724 size = PERF_ALIGN(size, sizeof(u64));
2725 size += sizeof(struct perf_event_header);
2726 size += ids * sizeof(u64);
2733 ev->attr.attr = *attr;
2734 memcpy(ev->attr.id, id, ids * sizeof(u64));
2736 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2737 ev->attr.header.size = (u16)size;
2739 if (ev->attr.header.size == size)
2740 err = process(tool, ev, NULL, NULL);
2749 int perf_event__synthesize_attrs(struct perf_tool *tool,
2750 struct perf_session *session,
2751 perf_event__handler_t process)
2753 struct perf_evsel *evsel;
2756 list_for_each_entry(evsel, &session->evlist->entries, node) {
2757 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2758 evsel->id, process);
2760 pr_debug("failed to create perf header attribute\n");
2768 int perf_event__process_attr(union perf_event *event,
2769 struct perf_evlist **pevlist)
2772 struct perf_evsel *evsel;
2773 struct perf_evlist *evlist = *pevlist;
2775 if (evlist == NULL) {
2776 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2781 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2785 perf_evlist__add(evlist, evsel);
2787 ids = event->header.size;
2788 ids -= (void *)&event->attr.id - (void *)event;
2789 n_ids = ids / sizeof(u64);
2791 * We don't have the cpu and thread maps on the header, so
2792 * for allocating the perf_sample_id table we fake 1 cpu and
2793 * hattr->ids threads.
2795 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2798 for (i = 0; i < n_ids; i++) {
2799 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2805 int perf_event__synthesize_event_type(struct perf_tool *tool,
2806 u64 event_id, char *name,
2807 perf_event__handler_t process,
2808 struct machine *machine)
2810 union perf_event ev;
2814 memset(&ev, 0, sizeof(ev));
2816 ev.event_type.event_type.event_id = event_id;
2817 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2818 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2820 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2821 size = strlen(ev.event_type.event_type.name);
2822 size = PERF_ALIGN(size, sizeof(u64));
2823 ev.event_type.header.size = sizeof(ev.event_type) -
2824 (sizeof(ev.event_type.event_type.name) - size);
2826 err = process(tool, &ev, NULL, machine);
2831 int perf_event__synthesize_event_types(struct perf_tool *tool,
2832 perf_event__handler_t process,
2833 struct machine *machine)
2835 struct perf_trace_event_type *type;
2838 for (i = 0; i < trace_event_count; i++) {
2839 type = &trace_events[i];
2841 err = perf_event__synthesize_event_type(tool, type->event_id,
2842 type->name, process,
2845 pr_debug("failed to create perf header event type\n");
2853 int perf_event__process_event_type(struct perf_tool *tool __maybe_unused,
2854 union perf_event *event)
2856 if (perf_header__push_event(event->event_type.event_type.event_id,
2857 event->event_type.event_type.name) < 0)
2863 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2864 struct perf_evlist *evlist,
2865 perf_event__handler_t process)
2867 union perf_event ev;
2868 struct tracing_data *tdata;
2869 ssize_t size = 0, aligned_size = 0, padding;
2870 int err __maybe_unused = 0;
2873 * We are going to store the size of the data followed
2874 * by the data contents. Since the fd descriptor is a pipe,
2875 * we cannot seek back to store the size of the data once
2876 * we know it. Instead we:
2878 * - write the tracing data to the temp file
2879 * - get/write the data size to pipe
2880 * - write the tracing data from the temp file
2883 tdata = tracing_data_get(&evlist->entries, fd, true);
2887 memset(&ev, 0, sizeof(ev));
2889 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2891 aligned_size = PERF_ALIGN(size, sizeof(u64));
2892 padding = aligned_size - size;
2893 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2894 ev.tracing_data.size = aligned_size;
2896 process(tool, &ev, NULL, NULL);
2899 * The put function will copy all the tracing data
2900 * stored in temp file to the pipe.
2902 tracing_data_put(tdata);
2904 write_padded(fd, NULL, 0, padding);
2906 return aligned_size;
2909 int perf_event__process_tracing_data(union perf_event *event,
2910 struct perf_session *session)
2912 ssize_t size_read, padding, size = event->tracing_data.size;
2913 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2916 /* setup for reading amidst mmap */
2917 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2920 size_read = trace_report(session->fd, &session->pevent,
2922 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2924 if (readn(session->fd, buf, padding) < 0)
2925 die("reading input file");
2926 if (session->repipe) {
2927 int retw = write(STDOUT_FILENO, buf, padding);
2928 if (retw <= 0 || retw != padding)
2929 die("repiping tracing data padding");
2932 if (size_read + padding != size)
2933 die("tracing data size mismatch");
2935 perf_evlist__prepare_tracepoint_events(session->evlist,
2938 return size_read + padding;
2941 int perf_event__synthesize_build_id(struct perf_tool *tool,
2942 struct dso *pos, u16 misc,
2943 perf_event__handler_t process,
2944 struct machine *machine)
2946 union perf_event ev;
2953 memset(&ev, 0, sizeof(ev));
2955 len = pos->long_name_len + 1;
2956 len = PERF_ALIGN(len, NAME_ALIGN);
2957 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2958 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2959 ev.build_id.header.misc = misc;
2960 ev.build_id.pid = machine->pid;
2961 ev.build_id.header.size = sizeof(ev.build_id) + len;
2962 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2964 err = process(tool, &ev, NULL, machine);
2969 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2970 union perf_event *event,
2971 struct perf_session *session)
2973 __event_process_build_id(&event->build_id,
2974 event->build_id.filename,
2979 void disable_buildid_cache(void)
2981 no_buildid_cache = true;