7 #include <linux/list.h>
8 #include <linux/kernel.h>
9 #include <linux/bitops.h>
10 #include <sys/utsname.h>
16 #include "trace-event.h"
27 static bool no_buildid_cache = false;
29 static u32 header_argc;
30 static const char **header_argv;
34 * must be a numerical value to let the endianness
35 * determine the memory layout. That way we are able
36 * to detect endianness when reading the perf.data file
39 * we check for legacy (PERFFILE) format.
41 static const char *__perf_magic1 = "PERFFILE";
42 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
43 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
45 #define PERF_MAGIC __perf_magic2
47 struct perf_file_attr {
48 struct perf_event_attr attr;
49 struct perf_file_section ids;
52 void perf_header__set_feat(struct perf_header *header, int feat)
54 set_bit(feat, header->adds_features);
57 void perf_header__clear_feat(struct perf_header *header, int feat)
59 clear_bit(feat, header->adds_features);
62 bool perf_header__has_feat(const struct perf_header *header, int feat)
64 return test_bit(feat, header->adds_features);
67 static int do_write(int fd, const void *buf, size_t size)
70 int ret = write(fd, buf, size);
84 static int write_padded(int fd, const void *bf, size_t count,
87 static const char zero_buf[NAME_ALIGN];
88 int err = do_write(fd, bf, count);
91 err = do_write(fd, zero_buf, count_aligned - count);
96 static int do_write_string(int fd, const char *str)
101 olen = strlen(str) + 1;
102 len = PERF_ALIGN(olen, NAME_ALIGN);
104 /* write len, incl. \0 */
105 ret = do_write(fd, &len, sizeof(len));
109 return write_padded(fd, str, olen, len);
112 static char *do_read_string(int fd, struct perf_header *ph)
118 sz = readn(fd, &len, sizeof(len));
119 if (sz < (ssize_t)sizeof(len))
129 ret = readn(fd, buf, len);
130 if (ret == (ssize_t)len) {
132 * strings are padded by zeroes
133 * thus the actual strlen of buf
134 * may be less than len
144 perf_header__set_cmdline(int argc, const char **argv)
149 * If header_argv has already been set, do not override it.
150 * This allows a command to set the cmdline, parse args and
151 * then call another builtin function that implements a
152 * command -- e.g, cmd_kvm calling cmd_record.
157 header_argc = (u32)argc;
159 /* do not include NULL termination */
160 header_argv = calloc(argc, sizeof(char *));
165 * must copy argv contents because it gets moved
166 * around during option parsing
168 for (i = 0; i < argc ; i++)
169 header_argv[i] = argv[i];
174 #define dsos__for_each_with_build_id(pos, head) \
175 list_for_each_entry(pos, head, node) \
176 if (!pos->has_build_id) \
180 static int write_buildid(const char *name, size_t name_len, u8 *build_id,
181 pid_t pid, u16 misc, int fd)
184 struct build_id_event b;
188 len = PERF_ALIGN(len, NAME_ALIGN);
190 memset(&b, 0, sizeof(b));
191 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
193 b.header.misc = misc;
194 b.header.size = sizeof(b) + len;
196 err = do_write(fd, &b, sizeof(b));
200 return write_padded(fd, name, name_len + 1, len);
203 static int __dsos__hit_all(struct list_head *head)
207 list_for_each_entry(pos, head, node)
213 static int machine__hit_all_dsos(struct machine *machine)
217 err = __dsos__hit_all(&machine->kernel_dsos.head);
221 return __dsos__hit_all(&machine->user_dsos.head);
224 int dsos__hit_all(struct perf_session *session)
229 err = machine__hit_all_dsos(&session->machines.host);
233 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
234 struct machine *pos = rb_entry(nd, struct machine, rb_node);
236 err = machine__hit_all_dsos(pos);
244 static int __dsos__write_buildid_table(struct list_head *head,
245 struct machine *machine,
246 pid_t pid, u16 misc, int fd)
251 dsos__for_each_with_build_id(pos, head) {
259 if (dso__is_vdso(pos)) {
260 name = pos->short_name;
261 name_len = pos->short_name_len + 1;
262 } else if (dso__is_kcore(pos)) {
263 machine__mmap_name(machine, nm, sizeof(nm));
265 name_len = strlen(nm) + 1;
267 name = pos->long_name;
268 name_len = pos->long_name_len + 1;
271 err = write_buildid(name, name_len, pos->build_id,
280 static int machine__write_buildid_table(struct machine *machine, int fd)
283 u16 kmisc = PERF_RECORD_MISC_KERNEL,
284 umisc = PERF_RECORD_MISC_USER;
286 if (!machine__is_host(machine)) {
287 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
288 umisc = PERF_RECORD_MISC_GUEST_USER;
291 err = __dsos__write_buildid_table(&machine->kernel_dsos.head, machine,
292 machine->pid, kmisc, fd);
294 err = __dsos__write_buildid_table(&machine->user_dsos.head,
295 machine, machine->pid, umisc,
300 static int dsos__write_buildid_table(struct perf_header *header, int fd)
302 struct perf_session *session = container_of(header,
303 struct perf_session, header);
305 int err = machine__write_buildid_table(&session->machines.host, fd);
310 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
311 struct machine *pos = rb_entry(nd, struct machine, rb_node);
312 err = machine__write_buildid_table(pos, fd);
319 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
320 const char *name, bool is_kallsyms, bool is_vdso)
322 const size_t size = PATH_MAX;
323 char *realname, *filename = zalloc(size),
324 *linkname = zalloc(size), *targetname;
326 bool slash = is_kallsyms || is_vdso;
329 if (symbol_conf.kptr_restrict) {
330 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
334 realname = (char *) name;
336 realname = realpath(name, NULL);
338 if (realname == NULL || filename == NULL || linkname == NULL)
341 len = scnprintf(filename, size, "%s%s%s",
342 debugdir, slash ? "/" : "",
343 is_vdso ? DSO__NAME_VDSO : realname);
344 if (mkdir_p(filename, 0755))
347 snprintf(filename + len, size - len, "/%s", sbuild_id);
349 if (access(filename, F_OK)) {
351 if (copyfile("/proc/kallsyms", filename))
353 } else if (link(realname, filename) && copyfile(name, filename))
357 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
358 debugdir, sbuild_id);
360 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
363 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
364 targetname = filename + strlen(debugdir) - 5;
365 memcpy(targetname, "../..", 5);
367 if (symlink(targetname, linkname) == 0)
377 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
378 const char *name, const char *debugdir,
379 bool is_kallsyms, bool is_vdso)
381 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
383 build_id__sprintf(build_id, build_id_size, sbuild_id);
385 return build_id_cache__add_s(sbuild_id, debugdir, name,
386 is_kallsyms, is_vdso);
389 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
391 const size_t size = PATH_MAX;
392 char *filename = zalloc(size),
393 *linkname = zalloc(size);
396 if (filename == NULL || linkname == NULL)
399 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
400 debugdir, sbuild_id, sbuild_id + 2);
402 if (access(linkname, F_OK))
405 if (readlink(linkname, filename, size - 1) < 0)
408 if (unlink(linkname))
412 * Since the link is relative, we must make it absolute:
414 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
415 debugdir, sbuild_id, filename);
417 if (unlink(linkname))
427 static int dso__cache_build_id(struct dso *dso, struct machine *machine,
428 const char *debugdir)
430 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
431 bool is_vdso = dso__is_vdso(dso);
432 const char *name = dso->long_name;
435 if (dso__is_kcore(dso)) {
437 machine__mmap_name(machine, nm, sizeof(nm));
440 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
441 debugdir, is_kallsyms, is_vdso);
444 static int __dsos__cache_build_ids(struct list_head *head,
445 struct machine *machine, const char *debugdir)
450 dsos__for_each_with_build_id(pos, head)
451 if (dso__cache_build_id(pos, machine, debugdir))
457 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
459 int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine,
461 ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine,
466 static int perf_session__cache_build_ids(struct perf_session *session)
470 char debugdir[PATH_MAX];
472 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
474 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
477 ret = machine__cache_build_ids(&session->machines.host, debugdir);
479 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
480 struct machine *pos = rb_entry(nd, struct machine, rb_node);
481 ret |= machine__cache_build_ids(pos, debugdir);
486 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
490 ret = __dsos__read_build_ids(&machine->kernel_dsos.head, with_hits);
491 ret |= __dsos__read_build_ids(&machine->user_dsos.head, with_hits);
495 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
498 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
500 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
501 struct machine *pos = rb_entry(nd, struct machine, rb_node);
502 ret |= machine__read_build_ids(pos, with_hits);
508 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
509 struct perf_evlist *evlist)
511 return read_tracing_data(fd, &evlist->entries);
515 static int write_build_id(int fd, struct perf_header *h,
516 struct perf_evlist *evlist __maybe_unused)
518 struct perf_session *session;
521 session = container_of(h, struct perf_session, header);
523 if (!perf_session__read_build_ids(session, true))
526 err = dsos__write_buildid_table(h, fd);
528 pr_debug("failed to write buildid table\n");
531 if (!no_buildid_cache)
532 perf_session__cache_build_ids(session);
537 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
538 struct perf_evlist *evlist __maybe_unused)
547 return do_write_string(fd, uts.nodename);
550 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
551 struct perf_evlist *evlist __maybe_unused)
560 return do_write_string(fd, uts.release);
563 static int write_arch(int fd, struct perf_header *h __maybe_unused,
564 struct perf_evlist *evlist __maybe_unused)
573 return do_write_string(fd, uts.machine);
576 static int write_version(int fd, struct perf_header *h __maybe_unused,
577 struct perf_evlist *evlist __maybe_unused)
579 return do_write_string(fd, perf_version_string);
582 static int __write_cpudesc(int fd, const char *cpuinfo_proc)
587 const char *search = cpuinfo_proc;
594 file = fopen("/proc/cpuinfo", "r");
598 while (getline(&buf, &len, file) > 0) {
599 ret = strncmp(buf, search, strlen(search));
611 p = strchr(buf, ':');
612 if (p && *(p+1) == ' ' && *(p+2))
618 /* squash extra space characters (branding string) */
625 while (*q && isspace(*q))
628 while ((*r++ = *q++));
632 ret = do_write_string(fd, s);
639 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
640 struct perf_evlist *evlist __maybe_unused)
643 #define CPUINFO_PROC {"model name", }
645 const char *cpuinfo_procs[] = CPUINFO_PROC;
648 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
650 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
658 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
659 struct perf_evlist *evlist __maybe_unused)
665 nr = sysconf(_SC_NPROCESSORS_CONF);
669 nrc = (u32)(nr & UINT_MAX);
671 nr = sysconf(_SC_NPROCESSORS_ONLN);
675 nra = (u32)(nr & UINT_MAX);
677 ret = do_write(fd, &nrc, sizeof(nrc));
681 return do_write(fd, &nra, sizeof(nra));
684 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
685 struct perf_evlist *evlist)
687 struct perf_evsel *evsel;
691 nre = evlist->nr_entries;
694 * write number of events
696 ret = do_write(fd, &nre, sizeof(nre));
701 * size of perf_event_attr struct
703 sz = (u32)sizeof(evsel->attr);
704 ret = do_write(fd, &sz, sizeof(sz));
708 evlist__for_each(evlist, evsel) {
709 ret = do_write(fd, &evsel->attr, sz);
713 * write number of unique id per event
714 * there is one id per instance of an event
716 * copy into an nri to be independent of the
720 ret = do_write(fd, &nri, sizeof(nri));
725 * write event string as passed on cmdline
727 ret = do_write_string(fd, perf_evsel__name(evsel));
731 * write unique ids for this event
733 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
740 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
741 struct perf_evlist *evlist __maybe_unused)
743 char buf[MAXPATHLEN];
749 * actual atual path to perf binary
751 sprintf(proc, "/proc/%d/exe", getpid());
752 ret = readlink(proc, buf, sizeof(buf));
756 /* readlink() does not add null termination */
759 /* account for binary path */
762 ret = do_write(fd, &n, sizeof(n));
766 ret = do_write_string(fd, buf);
770 for (i = 0 ; i < header_argc; i++) {
771 ret = do_write_string(fd, header_argv[i]);
778 #define CORE_SIB_FMT \
779 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
780 #define THRD_SIB_FMT \
781 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
786 char **core_siblings;
787 char **thread_siblings;
790 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
793 char filename[MAXPATHLEN];
794 char *buf = NULL, *p;
800 sprintf(filename, CORE_SIB_FMT, cpu);
801 fp = fopen(filename, "r");
805 sret = getline(&buf, &len, fp);
810 p = strchr(buf, '\n');
814 for (i = 0; i < tp->core_sib; i++) {
815 if (!strcmp(buf, tp->core_siblings[i]))
818 if (i == tp->core_sib) {
819 tp->core_siblings[i] = buf;
827 sprintf(filename, THRD_SIB_FMT, cpu);
828 fp = fopen(filename, "r");
832 if (getline(&buf, &len, fp) <= 0)
835 p = strchr(buf, '\n');
839 for (i = 0; i < tp->thread_sib; i++) {
840 if (!strcmp(buf, tp->thread_siblings[i]))
843 if (i == tp->thread_sib) {
844 tp->thread_siblings[i] = buf;
856 static void free_cpu_topo(struct cpu_topo *tp)
863 for (i = 0 ; i < tp->core_sib; i++)
864 zfree(&tp->core_siblings[i]);
866 for (i = 0 ; i < tp->thread_sib; i++)
867 zfree(&tp->thread_siblings[i]);
872 static struct cpu_topo *build_cpu_topology(void)
881 ncpus = sysconf(_SC_NPROCESSORS_CONF);
885 nr = (u32)(ncpus & UINT_MAX);
887 sz = nr * sizeof(char *);
889 addr = calloc(1, sizeof(*tp) + 2 * sz);
896 tp->core_siblings = addr;
898 tp->thread_siblings = addr;
900 for (i = 0; i < nr; i++) {
901 ret = build_cpu_topo(tp, i);
912 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
913 struct perf_evlist *evlist __maybe_unused)
919 tp = build_cpu_topology();
923 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
927 for (i = 0; i < tp->core_sib; i++) {
928 ret = do_write_string(fd, tp->core_siblings[i]);
932 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
936 for (i = 0; i < tp->thread_sib; i++) {
937 ret = do_write_string(fd, tp->thread_siblings[i]);
948 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
949 struct perf_evlist *evlist __maybe_unused)
957 fp = fopen("/proc/meminfo", "r");
961 while (getline(&buf, &len, fp) > 0) {
962 ret = strncmp(buf, "MemTotal:", 9);
967 n = sscanf(buf, "%*s %"PRIu64, &mem);
969 ret = do_write(fd, &mem, sizeof(mem));
977 static int write_topo_node(int fd, int node)
979 char str[MAXPATHLEN];
981 char *buf = NULL, *p;
984 u64 mem_total, mem_free, mem;
987 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
988 fp = fopen(str, "r");
992 while (getline(&buf, &len, fp) > 0) {
993 /* skip over invalid lines */
994 if (!strchr(buf, ':'))
996 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
998 if (!strcmp(field, "MemTotal:"))
1000 if (!strcmp(field, "MemFree:"))
1007 ret = do_write(fd, &mem_total, sizeof(u64));
1011 ret = do_write(fd, &mem_free, sizeof(u64));
1016 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
1018 fp = fopen(str, "r");
1022 if (getline(&buf, &len, fp) <= 0)
1025 p = strchr(buf, '\n');
1029 ret = do_write_string(fd, buf);
1037 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
1038 struct perf_evlist *evlist __maybe_unused)
1043 struct cpu_map *node_map = NULL;
1048 fp = fopen("/sys/devices/system/node/online", "r");
1052 if (getline(&buf, &len, fp) <= 0)
1055 c = strchr(buf, '\n');
1059 node_map = cpu_map__new(buf);
1063 nr = (u32)node_map->nr;
1065 ret = do_write(fd, &nr, sizeof(nr));
1069 for (i = 0; i < nr; i++) {
1070 j = (u32)node_map->map[i];
1071 ret = do_write(fd, &j, sizeof(j));
1075 ret = write_topo_node(fd, i);
1089 * struct pmu_mappings {
1098 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1099 struct perf_evlist *evlist __maybe_unused)
1101 struct perf_pmu *pmu = NULL;
1102 off_t offset = lseek(fd, 0, SEEK_CUR);
1106 /* write real pmu_num later */
1107 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1111 while ((pmu = perf_pmu__scan(pmu))) {
1116 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1120 ret = do_write_string(fd, pmu->name);
1125 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1127 lseek(fd, offset, SEEK_SET);
1137 * struct group_descs {
1139 * struct group_desc {
1146 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1147 struct perf_evlist *evlist)
1149 u32 nr_groups = evlist->nr_groups;
1150 struct perf_evsel *evsel;
1153 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1157 evlist__for_each(evlist, evsel) {
1158 if (perf_evsel__is_group_leader(evsel) &&
1159 evsel->nr_members > 1) {
1160 const char *name = evsel->group_name ?: "{anon_group}";
1161 u32 leader_idx = evsel->idx;
1162 u32 nr_members = evsel->nr_members;
1164 ret = do_write_string(fd, name);
1168 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1172 ret = do_write(fd, &nr_members, sizeof(nr_members));
1181 * default get_cpuid(): nothing gets recorded
1182 * actual implementation must be in arch/$(ARCH)/util/header.c
1184 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1185 size_t sz __maybe_unused)
1190 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1191 struct perf_evlist *evlist __maybe_unused)
1196 ret = get_cpuid(buffer, sizeof(buffer));
1202 return do_write_string(fd, buffer);
1205 static int write_branch_stack(int fd __maybe_unused,
1206 struct perf_header *h __maybe_unused,
1207 struct perf_evlist *evlist __maybe_unused)
1212 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1215 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1218 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1221 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1224 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1226 fprintf(fp, "# arch : %s\n", ph->env.arch);
1229 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1232 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1235 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1238 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1239 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1242 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1245 fprintf(fp, "# perf version : %s\n", ph->env.version);
1248 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1254 nr = ph->env.nr_cmdline;
1255 str = ph->env.cmdline;
1257 fprintf(fp, "# cmdline : ");
1259 for (i = 0; i < nr; i++) {
1260 fprintf(fp, "%s ", str);
1261 str += strlen(str) + 1;
1266 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1272 nr = ph->env.nr_sibling_cores;
1273 str = ph->env.sibling_cores;
1275 for (i = 0; i < nr; i++) {
1276 fprintf(fp, "# sibling cores : %s\n", str);
1277 str += strlen(str) + 1;
1280 nr = ph->env.nr_sibling_threads;
1281 str = ph->env.sibling_threads;
1283 for (i = 0; i < nr; i++) {
1284 fprintf(fp, "# sibling threads : %s\n", str);
1285 str += strlen(str) + 1;
1289 static void free_event_desc(struct perf_evsel *events)
1291 struct perf_evsel *evsel;
1296 for (evsel = events; evsel->attr.size; evsel++) {
1297 zfree(&evsel->name);
1304 static struct perf_evsel *
1305 read_event_desc(struct perf_header *ph, int fd)
1307 struct perf_evsel *evsel, *events = NULL;
1310 u32 nre, sz, nr, i, j;
1314 /* number of events */
1315 ret = readn(fd, &nre, sizeof(nre));
1316 if (ret != (ssize_t)sizeof(nre))
1320 nre = bswap_32(nre);
1322 ret = readn(fd, &sz, sizeof(sz));
1323 if (ret != (ssize_t)sizeof(sz))
1329 /* buffer to hold on file attr struct */
1334 /* the last event terminates with evsel->attr.size == 0: */
1335 events = calloc(nre + 1, sizeof(*events));
1339 msz = sizeof(evsel->attr);
1343 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1347 * must read entire on-file attr struct to
1348 * sync up with layout.
1350 ret = readn(fd, buf, sz);
1351 if (ret != (ssize_t)sz)
1355 perf_event__attr_swap(buf);
1357 memcpy(&evsel->attr, buf, msz);
1359 ret = readn(fd, &nr, sizeof(nr));
1360 if (ret != (ssize_t)sizeof(nr))
1363 if (ph->needs_swap) {
1365 evsel->needs_swap = true;
1368 evsel->name = do_read_string(fd, ph);
1373 id = calloc(nr, sizeof(*id));
1379 for (j = 0 ; j < nr; j++) {
1380 ret = readn(fd, id, sizeof(*id));
1381 if (ret != (ssize_t)sizeof(*id))
1384 *id = bswap_64(*id);
1393 free_event_desc(events);
1398 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1400 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1405 fprintf(fp, "# event desc: not available or unable to read\n");
1409 for (evsel = events; evsel->attr.size; evsel++) {
1410 fprintf(fp, "# event : name = %s, ", evsel->name);
1412 fprintf(fp, "type = %d, config = 0x%"PRIx64
1413 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1415 (u64)evsel->attr.config,
1416 (u64)evsel->attr.config1,
1417 (u64)evsel->attr.config2);
1419 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1420 evsel->attr.exclude_user,
1421 evsel->attr.exclude_kernel);
1423 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1424 evsel->attr.exclude_host,
1425 evsel->attr.exclude_guest);
1427 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1429 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1430 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1431 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
1433 fprintf(fp, ", id = {");
1434 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1437 fprintf(fp, " %"PRIu64, *id);
1445 free_event_desc(events);
1448 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1451 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1454 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1459 uint64_t mem_total, mem_free;
1462 nr = ph->env.nr_numa_nodes;
1463 str = ph->env.numa_nodes;
1465 for (i = 0; i < nr; i++) {
1467 c = strtoul(str, &tmp, 0);
1472 mem_total = strtoull(str, &tmp, 0);
1477 mem_free = strtoull(str, &tmp, 0);
1481 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1482 " free = %"PRIu64" kB\n",
1483 c, mem_total, mem_free);
1486 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1488 str += strlen(str) + 1;
1492 fprintf(fp, "# numa topology : not available\n");
1495 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1497 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1500 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1501 int fd __maybe_unused, FILE *fp)
1503 fprintf(fp, "# contains samples with branch stack\n");
1506 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1509 const char *delimiter = "# pmu mappings: ";
1514 pmu_num = ph->env.nr_pmu_mappings;
1516 fprintf(fp, "# pmu mappings: not available\n");
1520 str = ph->env.pmu_mappings;
1523 type = strtoul(str, &tmp, 0);
1528 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1531 str += strlen(str) + 1;
1540 fprintf(fp, "# pmu mappings: unable to read\n");
1543 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1546 struct perf_session *session;
1547 struct perf_evsel *evsel;
1550 session = container_of(ph, struct perf_session, header);
1552 evlist__for_each(session->evlist, evsel) {
1553 if (perf_evsel__is_group_leader(evsel) &&
1554 evsel->nr_members > 1) {
1555 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1556 perf_evsel__name(evsel));
1558 nr = evsel->nr_members - 1;
1560 fprintf(fp, ",%s", perf_evsel__name(evsel));
1568 static int __event_process_build_id(struct build_id_event *bev,
1570 struct perf_session *session)
1574 struct machine *machine;
1577 enum dso_kernel_type dso_type;
1579 machine = perf_session__findnew_machine(session, bev->pid);
1583 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1586 case PERF_RECORD_MISC_KERNEL:
1587 dso_type = DSO_TYPE_KERNEL;
1588 dsos = &machine->kernel_dsos;
1590 case PERF_RECORD_MISC_GUEST_KERNEL:
1591 dso_type = DSO_TYPE_GUEST_KERNEL;
1592 dsos = &machine->kernel_dsos;
1594 case PERF_RECORD_MISC_USER:
1595 case PERF_RECORD_MISC_GUEST_USER:
1596 dso_type = DSO_TYPE_USER;
1597 dsos = &machine->user_dsos;
1603 dso = __dsos__findnew(dsos, filename);
1605 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1607 dso__set_build_id(dso, &bev->build_id);
1609 if (filename[0] == '[')
1610 dso->kernel = dso_type;
1612 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1614 pr_debug("build id event received for %s: %s\n",
1615 dso->long_name, sbuild_id);
1623 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1624 int input, u64 offset, u64 size)
1626 struct perf_session *session = container_of(header, struct perf_session, header);
1628 struct perf_event_header header;
1629 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1632 struct build_id_event bev;
1633 char filename[PATH_MAX];
1634 u64 limit = offset + size;
1636 while (offset < limit) {
1639 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1642 if (header->needs_swap)
1643 perf_event_header__bswap(&old_bev.header);
1645 len = old_bev.header.size - sizeof(old_bev);
1646 if (readn(input, filename, len) != len)
1649 bev.header = old_bev.header;
1652 * As the pid is the missing value, we need to fill
1653 * it properly. The header.misc value give us nice hint.
1655 bev.pid = HOST_KERNEL_ID;
1656 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1657 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1658 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1660 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1661 __event_process_build_id(&bev, filename, session);
1663 offset += bev.header.size;
1669 static int perf_header__read_build_ids(struct perf_header *header,
1670 int input, u64 offset, u64 size)
1672 struct perf_session *session = container_of(header, struct perf_session, header);
1673 struct build_id_event bev;
1674 char filename[PATH_MAX];
1675 u64 limit = offset + size, orig_offset = offset;
1678 while (offset < limit) {
1681 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1684 if (header->needs_swap)
1685 perf_event_header__bswap(&bev.header);
1687 len = bev.header.size - sizeof(bev);
1688 if (readn(input, filename, len) != len)
1691 * The a1645ce1 changeset:
1693 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1695 * Added a field to struct build_id_event that broke the file
1698 * Since the kernel build-id is the first entry, process the
1699 * table using the old format if the well known
1700 * '[kernel.kallsyms]' string for the kernel build-id has the
1701 * first 4 characters chopped off (where the pid_t sits).
1703 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1704 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1706 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1709 __event_process_build_id(&bev, filename, session);
1711 offset += bev.header.size;
1718 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1719 struct perf_header *ph __maybe_unused,
1722 ssize_t ret = trace_report(fd, data, false);
1723 return ret < 0 ? -1 : 0;
1726 static int process_build_id(struct perf_file_section *section,
1727 struct perf_header *ph, int fd,
1728 void *data __maybe_unused)
1730 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1731 pr_debug("Failed to read buildids, continuing...\n");
1735 static int process_hostname(struct perf_file_section *section __maybe_unused,
1736 struct perf_header *ph, int fd,
1737 void *data __maybe_unused)
1739 ph->env.hostname = do_read_string(fd, ph);
1740 return ph->env.hostname ? 0 : -ENOMEM;
1743 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1744 struct perf_header *ph, int fd,
1745 void *data __maybe_unused)
1747 ph->env.os_release = do_read_string(fd, ph);
1748 return ph->env.os_release ? 0 : -ENOMEM;
1751 static int process_version(struct perf_file_section *section __maybe_unused,
1752 struct perf_header *ph, int fd,
1753 void *data __maybe_unused)
1755 ph->env.version = do_read_string(fd, ph);
1756 return ph->env.version ? 0 : -ENOMEM;
1759 static int process_arch(struct perf_file_section *section __maybe_unused,
1760 struct perf_header *ph, int fd,
1761 void *data __maybe_unused)
1763 ph->env.arch = do_read_string(fd, ph);
1764 return ph->env.arch ? 0 : -ENOMEM;
1767 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1768 struct perf_header *ph, int fd,
1769 void *data __maybe_unused)
1774 ret = readn(fd, &nr, sizeof(nr));
1775 if (ret != sizeof(nr))
1781 ph->env.nr_cpus_online = nr;
1783 ret = readn(fd, &nr, sizeof(nr));
1784 if (ret != sizeof(nr))
1790 ph->env.nr_cpus_avail = nr;
1794 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1795 struct perf_header *ph, int fd,
1796 void *data __maybe_unused)
1798 ph->env.cpu_desc = do_read_string(fd, ph);
1799 return ph->env.cpu_desc ? 0 : -ENOMEM;
1802 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1803 struct perf_header *ph, int fd,
1804 void *data __maybe_unused)
1806 ph->env.cpuid = do_read_string(fd, ph);
1807 return ph->env.cpuid ? 0 : -ENOMEM;
1810 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1811 struct perf_header *ph, int fd,
1812 void *data __maybe_unused)
1817 ret = readn(fd, &mem, sizeof(mem));
1818 if (ret != sizeof(mem))
1822 mem = bswap_64(mem);
1824 ph->env.total_mem = mem;
1828 static struct perf_evsel *
1829 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1831 struct perf_evsel *evsel;
1833 evlist__for_each(evlist, evsel) {
1834 if (evsel->idx == idx)
1842 perf_evlist__set_event_name(struct perf_evlist *evlist,
1843 struct perf_evsel *event)
1845 struct perf_evsel *evsel;
1850 evsel = perf_evlist__find_by_index(evlist, event->idx);
1857 evsel->name = strdup(event->name);
1861 process_event_desc(struct perf_file_section *section __maybe_unused,
1862 struct perf_header *header, int fd,
1863 void *data __maybe_unused)
1865 struct perf_session *session;
1866 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1871 session = container_of(header, struct perf_session, header);
1872 for (evsel = events; evsel->attr.size; evsel++)
1873 perf_evlist__set_event_name(session->evlist, evsel);
1875 free_event_desc(events);
1880 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1881 struct perf_header *ph, int fd,
1882 void *data __maybe_unused)
1889 ret = readn(fd, &nr, sizeof(nr));
1890 if (ret != sizeof(nr))
1896 ph->env.nr_cmdline = nr;
1897 strbuf_init(&sb, 128);
1899 for (i = 0; i < nr; i++) {
1900 str = do_read_string(fd, ph);
1904 /* include a NULL character at the end */
1905 strbuf_add(&sb, str, strlen(str) + 1);
1908 ph->env.cmdline = strbuf_detach(&sb, NULL);
1912 strbuf_release(&sb);
1916 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1917 struct perf_header *ph, int fd,
1918 void *data __maybe_unused)
1925 ret = readn(fd, &nr, sizeof(nr));
1926 if (ret != sizeof(nr))
1932 ph->env.nr_sibling_cores = nr;
1933 strbuf_init(&sb, 128);
1935 for (i = 0; i < nr; i++) {
1936 str = do_read_string(fd, ph);
1940 /* include a NULL character at the end */
1941 strbuf_add(&sb, str, strlen(str) + 1);
1944 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1946 ret = readn(fd, &nr, sizeof(nr));
1947 if (ret != sizeof(nr))
1953 ph->env.nr_sibling_threads = nr;
1955 for (i = 0; i < nr; i++) {
1956 str = do_read_string(fd, ph);
1960 /* include a NULL character at the end */
1961 strbuf_add(&sb, str, strlen(str) + 1);
1964 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1968 strbuf_release(&sb);
1972 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1973 struct perf_header *ph, int fd,
1974 void *data __maybe_unused)
1979 uint64_t mem_total, mem_free;
1983 ret = readn(fd, &nr, sizeof(nr));
1984 if (ret != sizeof(nr))
1990 ph->env.nr_numa_nodes = nr;
1991 strbuf_init(&sb, 256);
1993 for (i = 0; i < nr; i++) {
1995 ret = readn(fd, &node, sizeof(node));
1996 if (ret != sizeof(node))
1999 ret = readn(fd, &mem_total, sizeof(u64));
2000 if (ret != sizeof(u64))
2003 ret = readn(fd, &mem_free, sizeof(u64));
2004 if (ret != sizeof(u64))
2007 if (ph->needs_swap) {
2008 node = bswap_32(node);
2009 mem_total = bswap_64(mem_total);
2010 mem_free = bswap_64(mem_free);
2013 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
2014 node, mem_total, mem_free);
2016 str = do_read_string(fd, ph);
2020 /* include a NULL character at the end */
2021 strbuf_add(&sb, str, strlen(str) + 1);
2024 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
2028 strbuf_release(&sb);
2032 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
2033 struct perf_header *ph, int fd,
2034 void *data __maybe_unused)
2042 ret = readn(fd, &pmu_num, sizeof(pmu_num));
2043 if (ret != sizeof(pmu_num))
2047 pmu_num = bswap_32(pmu_num);
2050 pr_debug("pmu mappings not available\n");
2054 ph->env.nr_pmu_mappings = pmu_num;
2055 strbuf_init(&sb, 128);
2058 if (readn(fd, &type, sizeof(type)) != sizeof(type))
2061 type = bswap_32(type);
2063 name = do_read_string(fd, ph);
2067 strbuf_addf(&sb, "%u:%s", type, name);
2068 /* include a NULL character at the end */
2069 strbuf_add(&sb, "", 1);
2074 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2078 strbuf_release(&sb);
2082 static int process_group_desc(struct perf_file_section *section __maybe_unused,
2083 struct perf_header *ph, int fd,
2084 void *data __maybe_unused)
2087 u32 i, nr, nr_groups;
2088 struct perf_session *session;
2089 struct perf_evsel *evsel, *leader = NULL;
2096 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2100 nr_groups = bswap_32(nr_groups);
2102 ph->env.nr_groups = nr_groups;
2104 pr_debug("group desc not available\n");
2108 desc = calloc(nr_groups, sizeof(*desc));
2112 for (i = 0; i < nr_groups; i++) {
2113 desc[i].name = do_read_string(fd, ph);
2117 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2120 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2123 if (ph->needs_swap) {
2124 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2125 desc[i].nr_members = bswap_32(desc[i].nr_members);
2130 * Rebuild group relationship based on the group_desc
2132 session = container_of(ph, struct perf_session, header);
2133 session->evlist->nr_groups = nr_groups;
2136 evlist__for_each(session->evlist, evsel) {
2137 if (evsel->idx == (int) desc[i].leader_idx) {
2138 evsel->leader = evsel;
2139 /* {anon_group} is a dummy name */
2140 if (strcmp(desc[i].name, "{anon_group}")) {
2141 evsel->group_name = desc[i].name;
2142 desc[i].name = NULL;
2144 evsel->nr_members = desc[i].nr_members;
2146 if (i >= nr_groups || nr > 0) {
2147 pr_debug("invalid group desc\n");
2152 nr = evsel->nr_members - 1;
2155 /* This is a group member */
2156 evsel->leader = leader;
2162 if (i != nr_groups || nr != 0) {
2163 pr_debug("invalid group desc\n");
2169 for (i = 0; i < nr_groups; i++)
2170 zfree(&desc[i].name);
2176 struct feature_ops {
2177 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2178 void (*print)(struct perf_header *h, int fd, FILE *fp);
2179 int (*process)(struct perf_file_section *section,
2180 struct perf_header *h, int fd, void *data);
2185 #define FEAT_OPA(n, func) \
2186 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2187 #define FEAT_OPP(n, func) \
2188 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2189 .process = process_##func }
2190 #define FEAT_OPF(n, func) \
2191 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2192 .process = process_##func, .full_only = true }
2194 /* feature_ops not implemented: */
2195 #define print_tracing_data NULL
2196 #define print_build_id NULL
2198 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2199 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
2200 FEAT_OPP(HEADER_BUILD_ID, build_id),
2201 FEAT_OPP(HEADER_HOSTNAME, hostname),
2202 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2203 FEAT_OPP(HEADER_VERSION, version),
2204 FEAT_OPP(HEADER_ARCH, arch),
2205 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2206 FEAT_OPP(HEADER_CPUDESC, cpudesc),
2207 FEAT_OPP(HEADER_CPUID, cpuid),
2208 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
2209 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
2210 FEAT_OPP(HEADER_CMDLINE, cmdline),
2211 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2212 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
2213 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
2214 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2215 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
2218 struct header_print_data {
2220 bool full; /* extended list of headers */
2223 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2224 struct perf_header *ph,
2225 int feat, int fd, void *data)
2227 struct header_print_data *hd = data;
2229 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2230 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2231 "%d, continuing...\n", section->offset, feat);
2234 if (feat >= HEADER_LAST_FEATURE) {
2235 pr_warning("unknown feature %d\n", feat);
2238 if (!feat_ops[feat].print)
2241 if (!feat_ops[feat].full_only || hd->full)
2242 feat_ops[feat].print(ph, fd, hd->fp);
2244 fprintf(hd->fp, "# %s info available, use -I to display\n",
2245 feat_ops[feat].name);
2250 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2252 struct header_print_data hd;
2253 struct perf_header *header = &session->header;
2254 int fd = perf_data_file__fd(session->file);
2258 perf_header__process_sections(header, fd, &hd,
2259 perf_file_section__fprintf_info);
2263 static int do_write_feat(int fd, struct perf_header *h, int type,
2264 struct perf_file_section **p,
2265 struct perf_evlist *evlist)
2270 if (perf_header__has_feat(h, type)) {
2271 if (!feat_ops[type].write)
2274 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2276 err = feat_ops[type].write(fd, h, evlist);
2278 pr_debug("failed to write feature %d\n", type);
2280 /* undo anything written */
2281 lseek(fd, (*p)->offset, SEEK_SET);
2285 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2291 static int perf_header__adds_write(struct perf_header *header,
2292 struct perf_evlist *evlist, int fd)
2295 struct perf_file_section *feat_sec, *p;
2301 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2305 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2306 if (feat_sec == NULL)
2309 sec_size = sizeof(*feat_sec) * nr_sections;
2311 sec_start = header->feat_offset;
2312 lseek(fd, sec_start + sec_size, SEEK_SET);
2314 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2315 if (do_write_feat(fd, header, feat, &p, evlist))
2316 perf_header__clear_feat(header, feat);
2319 lseek(fd, sec_start, SEEK_SET);
2321 * may write more than needed due to dropped feature, but
2322 * this is okay, reader will skip the mising entries
2324 err = do_write(fd, feat_sec, sec_size);
2326 pr_debug("failed to write feature section\n");
2331 int perf_header__write_pipe(int fd)
2333 struct perf_pipe_file_header f_header;
2336 f_header = (struct perf_pipe_file_header){
2337 .magic = PERF_MAGIC,
2338 .size = sizeof(f_header),
2341 err = do_write(fd, &f_header, sizeof(f_header));
2343 pr_debug("failed to write perf pipe header\n");
2350 int perf_session__write_header(struct perf_session *session,
2351 struct perf_evlist *evlist,
2352 int fd, bool at_exit)
2354 struct perf_file_header f_header;
2355 struct perf_file_attr f_attr;
2356 struct perf_header *header = &session->header;
2357 struct perf_evsel *evsel;
2361 lseek(fd, sizeof(f_header), SEEK_SET);
2363 evlist__for_each(session->evlist, evsel) {
2364 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2365 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2367 pr_debug("failed to write perf header\n");
2372 attr_offset = lseek(fd, 0, SEEK_CUR);
2374 evlist__for_each(evlist, evsel) {
2375 f_attr = (struct perf_file_attr){
2376 .attr = evsel->attr,
2378 .offset = evsel->id_offset,
2379 .size = evsel->ids * sizeof(u64),
2382 err = do_write(fd, &f_attr, sizeof(f_attr));
2384 pr_debug("failed to write perf header attribute\n");
2389 if (!header->data_offset)
2390 header->data_offset = lseek(fd, 0, SEEK_CUR);
2391 header->feat_offset = header->data_offset + header->data_size;
2394 err = perf_header__adds_write(header, evlist, fd);
2399 f_header = (struct perf_file_header){
2400 .magic = PERF_MAGIC,
2401 .size = sizeof(f_header),
2402 .attr_size = sizeof(f_attr),
2404 .offset = attr_offset,
2405 .size = evlist->nr_entries * sizeof(f_attr),
2408 .offset = header->data_offset,
2409 .size = header->data_size,
2411 /* event_types is ignored, store zeros */
2414 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2416 lseek(fd, 0, SEEK_SET);
2417 err = do_write(fd, &f_header, sizeof(f_header));
2419 pr_debug("failed to write perf header\n");
2422 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2427 static int perf_header__getbuffer64(struct perf_header *header,
2428 int fd, void *buf, size_t size)
2430 if (readn(fd, buf, size) <= 0)
2433 if (header->needs_swap)
2434 mem_bswap_64(buf, size);
2439 int perf_header__process_sections(struct perf_header *header, int fd,
2441 int (*process)(struct perf_file_section *section,
2442 struct perf_header *ph,
2443 int feat, int fd, void *data))
2445 struct perf_file_section *feat_sec, *sec;
2451 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2455 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2459 sec_size = sizeof(*feat_sec) * nr_sections;
2461 lseek(fd, header->feat_offset, SEEK_SET);
2463 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2467 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2468 err = process(sec++, header, feat, fd, data);
2478 static const int attr_file_abi_sizes[] = {
2479 [0] = PERF_ATTR_SIZE_VER0,
2480 [1] = PERF_ATTR_SIZE_VER1,
2481 [2] = PERF_ATTR_SIZE_VER2,
2482 [3] = PERF_ATTR_SIZE_VER3,
2487 * In the legacy file format, the magic number is not used to encode endianness.
2488 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2489 * on ABI revisions, we need to try all combinations for all endianness to
2490 * detect the endianness.
2492 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2494 uint64_t ref_size, attr_size;
2497 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2498 ref_size = attr_file_abi_sizes[i]
2499 + sizeof(struct perf_file_section);
2500 if (hdr_sz != ref_size) {
2501 attr_size = bswap_64(hdr_sz);
2502 if (attr_size != ref_size)
2505 ph->needs_swap = true;
2507 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2512 /* could not determine endianness */
2516 #define PERF_PIPE_HDR_VER0 16
2518 static const size_t attr_pipe_abi_sizes[] = {
2519 [0] = PERF_PIPE_HDR_VER0,
2524 * In the legacy pipe format, there is an implicit assumption that endiannesss
2525 * between host recording the samples, and host parsing the samples is the
2526 * same. This is not always the case given that the pipe output may always be
2527 * redirected into a file and analyzed on a different machine with possibly a
2528 * different endianness and perf_event ABI revsions in the perf tool itself.
2530 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2535 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2536 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2537 attr_size = bswap_64(hdr_sz);
2538 if (attr_size != hdr_sz)
2541 ph->needs_swap = true;
2543 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2549 bool is_perf_magic(u64 magic)
2551 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2552 || magic == __perf_magic2
2553 || magic == __perf_magic2_sw)
2559 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2560 bool is_pipe, struct perf_header *ph)
2564 /* check for legacy format */
2565 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2567 ph->version = PERF_HEADER_VERSION_1;
2568 pr_debug("legacy perf.data format\n");
2570 return try_all_pipe_abis(hdr_sz, ph);
2572 return try_all_file_abis(hdr_sz, ph);
2575 * the new magic number serves two purposes:
2576 * - unique number to identify actual perf.data files
2577 * - encode endianness of file
2580 /* check magic number with one endianness */
2581 if (magic == __perf_magic2)
2584 /* check magic number with opposite endianness */
2585 if (magic != __perf_magic2_sw)
2588 ph->needs_swap = true;
2589 ph->version = PERF_HEADER_VERSION_2;
2594 int perf_file_header__read(struct perf_file_header *header,
2595 struct perf_header *ph, int fd)
2599 lseek(fd, 0, SEEK_SET);
2601 ret = readn(fd, header, sizeof(*header));
2605 if (check_magic_endian(header->magic,
2606 header->attr_size, false, ph) < 0) {
2607 pr_debug("magic/endian check failed\n");
2611 if (ph->needs_swap) {
2612 mem_bswap_64(header, offsetof(struct perf_file_header,
2616 if (header->size != sizeof(*header)) {
2617 /* Support the previous format */
2618 if (header->size == offsetof(typeof(*header), adds_features))
2619 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2622 } else if (ph->needs_swap) {
2624 * feature bitmap is declared as an array of unsigned longs --
2625 * not good since its size can differ between the host that
2626 * generated the data file and the host analyzing the file.
2628 * We need to handle endianness, but we don't know the size of
2629 * the unsigned long where the file was generated. Take a best
2630 * guess at determining it: try 64-bit swap first (ie., file
2631 * created on a 64-bit host), and check if the hostname feature
2632 * bit is set (this feature bit is forced on as of fbe96f2).
2633 * If the bit is not, undo the 64-bit swap and try a 32-bit
2634 * swap. If the hostname bit is still not set (e.g., older data
2635 * file), punt and fallback to the original behavior --
2636 * clearing all feature bits and setting buildid.
2638 mem_bswap_64(&header->adds_features,
2639 BITS_TO_U64(HEADER_FEAT_BITS));
2641 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2643 mem_bswap_64(&header->adds_features,
2644 BITS_TO_U64(HEADER_FEAT_BITS));
2647 mem_bswap_32(&header->adds_features,
2648 BITS_TO_U32(HEADER_FEAT_BITS));
2651 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2652 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2653 set_bit(HEADER_BUILD_ID, header->adds_features);
2657 memcpy(&ph->adds_features, &header->adds_features,
2658 sizeof(ph->adds_features));
2660 ph->data_offset = header->data.offset;
2661 ph->data_size = header->data.size;
2662 ph->feat_offset = header->data.offset + header->data.size;
2666 static int perf_file_section__process(struct perf_file_section *section,
2667 struct perf_header *ph,
2668 int feat, int fd, void *data)
2670 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2671 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2672 "%d, continuing...\n", section->offset, feat);
2676 if (feat >= HEADER_LAST_FEATURE) {
2677 pr_debug("unknown feature %d, continuing...\n", feat);
2681 if (!feat_ops[feat].process)
2684 return feat_ops[feat].process(section, ph, fd, data);
2687 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2688 struct perf_header *ph, int fd,
2693 ret = readn(fd, header, sizeof(*header));
2697 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2698 pr_debug("endian/magic failed\n");
2703 header->size = bswap_64(header->size);
2705 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2711 static int perf_header__read_pipe(struct perf_session *session)
2713 struct perf_header *header = &session->header;
2714 struct perf_pipe_file_header f_header;
2716 if (perf_file_header__read_pipe(&f_header, header,
2717 perf_data_file__fd(session->file),
2718 session->repipe) < 0) {
2719 pr_debug("incompatible file format\n");
2726 static int read_attr(int fd, struct perf_header *ph,
2727 struct perf_file_attr *f_attr)
2729 struct perf_event_attr *attr = &f_attr->attr;
2731 size_t our_sz = sizeof(f_attr->attr);
2734 memset(f_attr, 0, sizeof(*f_attr));
2736 /* read minimal guaranteed structure */
2737 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2739 pr_debug("cannot read %d bytes of header attr\n",
2740 PERF_ATTR_SIZE_VER0);
2744 /* on file perf_event_attr size */
2752 sz = PERF_ATTR_SIZE_VER0;
2753 } else if (sz > our_sz) {
2754 pr_debug("file uses a more recent and unsupported ABI"
2755 " (%zu bytes extra)\n", sz - our_sz);
2758 /* what we have not yet read and that we know about */
2759 left = sz - PERF_ATTR_SIZE_VER0;
2762 ptr += PERF_ATTR_SIZE_VER0;
2764 ret = readn(fd, ptr, left);
2766 /* read perf_file_section, ids are read in caller */
2767 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2769 return ret <= 0 ? -1 : 0;
2772 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2773 struct pevent *pevent)
2775 struct event_format *event;
2778 /* already prepared */
2779 if (evsel->tp_format)
2782 if (pevent == NULL) {
2783 pr_debug("broken or missing trace data\n");
2787 event = pevent_find_event(pevent, evsel->attr.config);
2792 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2793 evsel->name = strdup(bf);
2794 if (evsel->name == NULL)
2798 evsel->tp_format = event;
2802 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2803 struct pevent *pevent)
2805 struct perf_evsel *pos;
2807 evlist__for_each(evlist, pos) {
2808 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2809 perf_evsel__prepare_tracepoint_event(pos, pevent))
2816 int perf_session__read_header(struct perf_session *session)
2818 struct perf_data_file *file = session->file;
2819 struct perf_header *header = &session->header;
2820 struct perf_file_header f_header;
2821 struct perf_file_attr f_attr;
2823 int nr_attrs, nr_ids, i, j;
2824 int fd = perf_data_file__fd(file);
2826 session->evlist = perf_evlist__new();
2827 if (session->evlist == NULL)
2830 if (perf_data_file__is_pipe(file))
2831 return perf_header__read_pipe(session);
2833 if (perf_file_header__read(&f_header, header, fd) < 0)
2837 * Sanity check that perf.data was written cleanly; data size is
2838 * initialized to 0 and updated only if the on_exit function is run.
2839 * If data size is still 0 then the file contains only partial
2840 * information. Just warn user and process it as much as it can.
2842 if (f_header.data.size == 0) {
2843 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2844 "Was the 'perf record' command properly terminated?\n",
2848 nr_attrs = f_header.attrs.size / f_header.attr_size;
2849 lseek(fd, f_header.attrs.offset, SEEK_SET);
2851 for (i = 0; i < nr_attrs; i++) {
2852 struct perf_evsel *evsel;
2855 if (read_attr(fd, header, &f_attr) < 0)
2858 if (header->needs_swap)
2859 perf_event__attr_swap(&f_attr.attr);
2861 tmp = lseek(fd, 0, SEEK_CUR);
2862 evsel = perf_evsel__new(&f_attr.attr);
2865 goto out_delete_evlist;
2867 evsel->needs_swap = header->needs_swap;
2869 * Do it before so that if perf_evsel__alloc_id fails, this
2870 * entry gets purged too at perf_evlist__delete().
2872 perf_evlist__add(session->evlist, evsel);
2874 nr_ids = f_attr.ids.size / sizeof(u64);
2876 * We don't have the cpu and thread maps on the header, so
2877 * for allocating the perf_sample_id table we fake 1 cpu and
2878 * hattr->ids threads.
2880 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2881 goto out_delete_evlist;
2883 lseek(fd, f_attr.ids.offset, SEEK_SET);
2885 for (j = 0; j < nr_ids; j++) {
2886 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2889 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2892 lseek(fd, tmp, SEEK_SET);
2895 symbol_conf.nr_events = nr_attrs;
2897 perf_header__process_sections(header, fd, &session->tevent,
2898 perf_file_section__process);
2900 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2901 session->tevent.pevent))
2902 goto out_delete_evlist;
2909 perf_evlist__delete(session->evlist);
2910 session->evlist = NULL;
2914 int perf_event__synthesize_attr(struct perf_tool *tool,
2915 struct perf_event_attr *attr, u32 ids, u64 *id,
2916 perf_event__handler_t process)
2918 union perf_event *ev;
2922 size = sizeof(struct perf_event_attr);
2923 size = PERF_ALIGN(size, sizeof(u64));
2924 size += sizeof(struct perf_event_header);
2925 size += ids * sizeof(u64);
2932 ev->attr.attr = *attr;
2933 memcpy(ev->attr.id, id, ids * sizeof(u64));
2935 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2936 ev->attr.header.size = (u16)size;
2938 if (ev->attr.header.size == size)
2939 err = process(tool, ev, NULL, NULL);
2948 int perf_event__synthesize_attrs(struct perf_tool *tool,
2949 struct perf_session *session,
2950 perf_event__handler_t process)
2952 struct perf_evsel *evsel;
2955 evlist__for_each(session->evlist, evsel) {
2956 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2957 evsel->id, process);
2959 pr_debug("failed to create perf header attribute\n");
2967 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2968 union perf_event *event,
2969 struct perf_evlist **pevlist)
2972 struct perf_evsel *evsel;
2973 struct perf_evlist *evlist = *pevlist;
2975 if (evlist == NULL) {
2976 *pevlist = evlist = perf_evlist__new();
2981 evsel = perf_evsel__new(&event->attr.attr);
2985 perf_evlist__add(evlist, evsel);
2987 ids = event->header.size;
2988 ids -= (void *)&event->attr.id - (void *)event;
2989 n_ids = ids / sizeof(u64);
2991 * We don't have the cpu and thread maps on the header, so
2992 * for allocating the perf_sample_id table we fake 1 cpu and
2993 * hattr->ids threads.
2995 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2998 for (i = 0; i < n_ids; i++) {
2999 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3002 symbol_conf.nr_events = evlist->nr_entries;
3007 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3008 struct perf_evlist *evlist,
3009 perf_event__handler_t process)
3011 union perf_event ev;
3012 struct tracing_data *tdata;
3013 ssize_t size = 0, aligned_size = 0, padding;
3014 int err __maybe_unused = 0;
3017 * We are going to store the size of the data followed
3018 * by the data contents. Since the fd descriptor is a pipe,
3019 * we cannot seek back to store the size of the data once
3020 * we know it. Instead we:
3022 * - write the tracing data to the temp file
3023 * - get/write the data size to pipe
3024 * - write the tracing data from the temp file
3027 tdata = tracing_data_get(&evlist->entries, fd, true);
3031 memset(&ev, 0, sizeof(ev));
3033 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3035 aligned_size = PERF_ALIGN(size, sizeof(u64));
3036 padding = aligned_size - size;
3037 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3038 ev.tracing_data.size = aligned_size;
3040 process(tool, &ev, NULL, NULL);
3043 * The put function will copy all the tracing data
3044 * stored in temp file to the pipe.
3046 tracing_data_put(tdata);
3048 write_padded(fd, NULL, 0, padding);
3050 return aligned_size;
3053 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3054 union perf_event *event,
3055 struct perf_session *session)
3057 ssize_t size_read, padding, size = event->tracing_data.size;
3058 int fd = perf_data_file__fd(session->file);
3059 off_t offset = lseek(fd, 0, SEEK_CUR);
3062 /* setup for reading amidst mmap */
3063 lseek(fd, offset + sizeof(struct tracing_data_event),
3066 size_read = trace_report(fd, &session->tevent,
3068 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3070 if (readn(fd, buf, padding) < 0) {
3071 pr_err("%s: reading input file", __func__);
3074 if (session->repipe) {
3075 int retw = write(STDOUT_FILENO, buf, padding);
3076 if (retw <= 0 || retw != padding) {
3077 pr_err("%s: repiping tracing data padding", __func__);
3082 if (size_read + padding != size) {
3083 pr_err("%s: tracing data size mismatch", __func__);
3087 perf_evlist__prepare_tracepoint_events(session->evlist,
3088 session->tevent.pevent);
3090 return size_read + padding;
3093 int perf_event__synthesize_build_id(struct perf_tool *tool,
3094 struct dso *pos, u16 misc,
3095 perf_event__handler_t process,
3096 struct machine *machine)
3098 union perf_event ev;
3105 memset(&ev, 0, sizeof(ev));
3107 len = pos->long_name_len + 1;
3108 len = PERF_ALIGN(len, NAME_ALIGN);
3109 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3110 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3111 ev.build_id.header.misc = misc;
3112 ev.build_id.pid = machine->pid;
3113 ev.build_id.header.size = sizeof(ev.build_id) + len;
3114 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3116 err = process(tool, &ev, NULL, machine);
3121 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3122 union perf_event *event,
3123 struct perf_session *session)
3125 __event_process_build_id(&event->build_id,
3126 event->build_id.filename,
3131 void disable_buildid_cache(void)
3133 no_buildid_cache = true;