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));
609 p = strchr(buf, ':');
610 if (p && *(p+1) == ' ' && *(p+2))
616 /* squash extra space characters (branding string) */
623 while (*q && isspace(*q))
626 while ((*r++ = *q++));
630 ret = do_write_string(fd, s);
637 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
638 struct perf_evlist *evlist __maybe_unused)
641 #define CPUINFO_PROC {"model name", }
643 const char *cpuinfo_procs[] = CPUINFO_PROC;
646 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
648 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
656 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
657 struct perf_evlist *evlist __maybe_unused)
663 nr = sysconf(_SC_NPROCESSORS_CONF);
667 nrc = (u32)(nr & UINT_MAX);
669 nr = sysconf(_SC_NPROCESSORS_ONLN);
673 nra = (u32)(nr & UINT_MAX);
675 ret = do_write(fd, &nrc, sizeof(nrc));
679 return do_write(fd, &nra, sizeof(nra));
682 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
683 struct perf_evlist *evlist)
685 struct perf_evsel *evsel;
689 nre = evlist->nr_entries;
692 * write number of events
694 ret = do_write(fd, &nre, sizeof(nre));
699 * size of perf_event_attr struct
701 sz = (u32)sizeof(evsel->attr);
702 ret = do_write(fd, &sz, sizeof(sz));
706 evlist__for_each(evlist, evsel) {
707 ret = do_write(fd, &evsel->attr, sz);
711 * write number of unique id per event
712 * there is one id per instance of an event
714 * copy into an nri to be independent of the
718 ret = do_write(fd, &nri, sizeof(nri));
723 * write event string as passed on cmdline
725 ret = do_write_string(fd, perf_evsel__name(evsel));
729 * write unique ids for this event
731 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
738 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
739 struct perf_evlist *evlist __maybe_unused)
741 char buf[MAXPATHLEN];
747 * actual atual path to perf binary
749 sprintf(proc, "/proc/%d/exe", getpid());
750 ret = readlink(proc, buf, sizeof(buf));
754 /* readlink() does not add null termination */
757 /* account for binary path */
760 ret = do_write(fd, &n, sizeof(n));
764 ret = do_write_string(fd, buf);
768 for (i = 0 ; i < header_argc; i++) {
769 ret = do_write_string(fd, header_argv[i]);
776 #define CORE_SIB_FMT \
777 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
778 #define THRD_SIB_FMT \
779 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
784 char **core_siblings;
785 char **thread_siblings;
788 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
791 char filename[MAXPATHLEN];
792 char *buf = NULL, *p;
798 sprintf(filename, CORE_SIB_FMT, cpu);
799 fp = fopen(filename, "r");
803 sret = getline(&buf, &len, fp);
808 p = strchr(buf, '\n');
812 for (i = 0; i < tp->core_sib; i++) {
813 if (!strcmp(buf, tp->core_siblings[i]))
816 if (i == tp->core_sib) {
817 tp->core_siblings[i] = buf;
825 sprintf(filename, THRD_SIB_FMT, cpu);
826 fp = fopen(filename, "r");
830 if (getline(&buf, &len, fp) <= 0)
833 p = strchr(buf, '\n');
837 for (i = 0; i < tp->thread_sib; i++) {
838 if (!strcmp(buf, tp->thread_siblings[i]))
841 if (i == tp->thread_sib) {
842 tp->thread_siblings[i] = buf;
854 static void free_cpu_topo(struct cpu_topo *tp)
861 for (i = 0 ; i < tp->core_sib; i++)
862 zfree(&tp->core_siblings[i]);
864 for (i = 0 ; i < tp->thread_sib; i++)
865 zfree(&tp->thread_siblings[i]);
870 static struct cpu_topo *build_cpu_topology(void)
879 ncpus = sysconf(_SC_NPROCESSORS_CONF);
883 nr = (u32)(ncpus & UINT_MAX);
885 sz = nr * sizeof(char *);
887 addr = calloc(1, sizeof(*tp) + 2 * sz);
894 tp->core_siblings = addr;
896 tp->thread_siblings = addr;
898 for (i = 0; i < nr; i++) {
899 ret = build_cpu_topo(tp, i);
910 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
911 struct perf_evlist *evlist __maybe_unused)
917 tp = build_cpu_topology();
921 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
925 for (i = 0; i < tp->core_sib; i++) {
926 ret = do_write_string(fd, tp->core_siblings[i]);
930 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
934 for (i = 0; i < tp->thread_sib; i++) {
935 ret = do_write_string(fd, tp->thread_siblings[i]);
946 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
947 struct perf_evlist *evlist __maybe_unused)
955 fp = fopen("/proc/meminfo", "r");
959 while (getline(&buf, &len, fp) > 0) {
960 ret = strncmp(buf, "MemTotal:", 9);
965 n = sscanf(buf, "%*s %"PRIu64, &mem);
967 ret = do_write(fd, &mem, sizeof(mem));
974 static int write_topo_node(int fd, int node)
976 char str[MAXPATHLEN];
978 char *buf = NULL, *p;
981 u64 mem_total, mem_free, mem;
984 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
985 fp = fopen(str, "r");
989 while (getline(&buf, &len, fp) > 0) {
990 /* skip over invalid lines */
991 if (!strchr(buf, ':'))
993 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
995 if (!strcmp(field, "MemTotal:"))
997 if (!strcmp(field, "MemFree:"))
1004 ret = do_write(fd, &mem_total, sizeof(u64));
1008 ret = do_write(fd, &mem_free, sizeof(u64));
1013 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
1015 fp = fopen(str, "r");
1019 if (getline(&buf, &len, fp) <= 0)
1022 p = strchr(buf, '\n');
1026 ret = do_write_string(fd, buf);
1034 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
1035 struct perf_evlist *evlist __maybe_unused)
1040 struct cpu_map *node_map = NULL;
1045 fp = fopen("/sys/devices/system/node/online", "r");
1049 if (getline(&buf, &len, fp) <= 0)
1052 c = strchr(buf, '\n');
1056 node_map = cpu_map__new(buf);
1060 nr = (u32)node_map->nr;
1062 ret = do_write(fd, &nr, sizeof(nr));
1066 for (i = 0; i < nr; i++) {
1067 j = (u32)node_map->map[i];
1068 ret = do_write(fd, &j, sizeof(j));
1072 ret = write_topo_node(fd, i);
1086 * struct pmu_mappings {
1095 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1096 struct perf_evlist *evlist __maybe_unused)
1098 struct perf_pmu *pmu = NULL;
1099 off_t offset = lseek(fd, 0, SEEK_CUR);
1103 /* write real pmu_num later */
1104 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1108 while ((pmu = perf_pmu__scan(pmu))) {
1113 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1117 ret = do_write_string(fd, pmu->name);
1122 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1124 lseek(fd, offset, SEEK_SET);
1134 * struct group_descs {
1136 * struct group_desc {
1143 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1144 struct perf_evlist *evlist)
1146 u32 nr_groups = evlist->nr_groups;
1147 struct perf_evsel *evsel;
1150 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1154 evlist__for_each(evlist, evsel) {
1155 if (perf_evsel__is_group_leader(evsel) &&
1156 evsel->nr_members > 1) {
1157 const char *name = evsel->group_name ?: "{anon_group}";
1158 u32 leader_idx = evsel->idx;
1159 u32 nr_members = evsel->nr_members;
1161 ret = do_write_string(fd, name);
1165 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1169 ret = do_write(fd, &nr_members, sizeof(nr_members));
1178 * default get_cpuid(): nothing gets recorded
1179 * actual implementation must be in arch/$(ARCH)/util/header.c
1181 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1182 size_t sz __maybe_unused)
1187 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1188 struct perf_evlist *evlist __maybe_unused)
1193 ret = get_cpuid(buffer, sizeof(buffer));
1199 return do_write_string(fd, buffer);
1202 static int write_branch_stack(int fd __maybe_unused,
1203 struct perf_header *h __maybe_unused,
1204 struct perf_evlist *evlist __maybe_unused)
1209 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1212 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1215 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1218 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1221 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1223 fprintf(fp, "# arch : %s\n", ph->env.arch);
1226 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1229 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1232 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1235 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1236 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1239 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1242 fprintf(fp, "# perf version : %s\n", ph->env.version);
1245 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1251 nr = ph->env.nr_cmdline;
1252 str = ph->env.cmdline;
1254 fprintf(fp, "# cmdline : ");
1256 for (i = 0; i < nr; i++) {
1257 fprintf(fp, "%s ", str);
1258 str += strlen(str) + 1;
1263 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1269 nr = ph->env.nr_sibling_cores;
1270 str = ph->env.sibling_cores;
1272 for (i = 0; i < nr; i++) {
1273 fprintf(fp, "# sibling cores : %s\n", str);
1274 str += strlen(str) + 1;
1277 nr = ph->env.nr_sibling_threads;
1278 str = ph->env.sibling_threads;
1280 for (i = 0; i < nr; i++) {
1281 fprintf(fp, "# sibling threads : %s\n", str);
1282 str += strlen(str) + 1;
1286 static void free_event_desc(struct perf_evsel *events)
1288 struct perf_evsel *evsel;
1293 for (evsel = events; evsel->attr.size; evsel++) {
1294 zfree(&evsel->name);
1301 static struct perf_evsel *
1302 read_event_desc(struct perf_header *ph, int fd)
1304 struct perf_evsel *evsel, *events = NULL;
1307 u32 nre, sz, nr, i, j;
1311 /* number of events */
1312 ret = readn(fd, &nre, sizeof(nre));
1313 if (ret != (ssize_t)sizeof(nre))
1317 nre = bswap_32(nre);
1319 ret = readn(fd, &sz, sizeof(sz));
1320 if (ret != (ssize_t)sizeof(sz))
1326 /* buffer to hold on file attr struct */
1331 /* the last event terminates with evsel->attr.size == 0: */
1332 events = calloc(nre + 1, sizeof(*events));
1336 msz = sizeof(evsel->attr);
1340 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1344 * must read entire on-file attr struct to
1345 * sync up with layout.
1347 ret = readn(fd, buf, sz);
1348 if (ret != (ssize_t)sz)
1352 perf_event__attr_swap(buf);
1354 memcpy(&evsel->attr, buf, msz);
1356 ret = readn(fd, &nr, sizeof(nr));
1357 if (ret != (ssize_t)sizeof(nr))
1360 if (ph->needs_swap) {
1362 evsel->needs_swap = true;
1365 evsel->name = do_read_string(fd, ph);
1370 id = calloc(nr, sizeof(*id));
1376 for (j = 0 ; j < nr; j++) {
1377 ret = readn(fd, id, sizeof(*id));
1378 if (ret != (ssize_t)sizeof(*id))
1381 *id = bswap_64(*id);
1390 free_event_desc(events);
1395 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1397 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1402 fprintf(fp, "# event desc: not available or unable to read\n");
1406 for (evsel = events; evsel->attr.size; evsel++) {
1407 fprintf(fp, "# event : name = %s, ", evsel->name);
1409 fprintf(fp, "type = %d, config = 0x%"PRIx64
1410 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1412 (u64)evsel->attr.config,
1413 (u64)evsel->attr.config1,
1414 (u64)evsel->attr.config2);
1416 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1417 evsel->attr.exclude_user,
1418 evsel->attr.exclude_kernel);
1420 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1421 evsel->attr.exclude_host,
1422 evsel->attr.exclude_guest);
1424 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1426 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1427 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1428 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
1430 fprintf(fp, ", id = {");
1431 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1434 fprintf(fp, " %"PRIu64, *id);
1442 free_event_desc(events);
1445 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1448 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1451 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1456 uint64_t mem_total, mem_free;
1459 nr = ph->env.nr_numa_nodes;
1460 str = ph->env.numa_nodes;
1462 for (i = 0; i < nr; i++) {
1464 c = strtoul(str, &tmp, 0);
1469 mem_total = strtoull(str, &tmp, 0);
1474 mem_free = strtoull(str, &tmp, 0);
1478 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1479 " free = %"PRIu64" kB\n",
1480 c, mem_total, mem_free);
1483 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1485 str += strlen(str) + 1;
1489 fprintf(fp, "# numa topology : not available\n");
1492 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1494 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1497 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1498 int fd __maybe_unused, FILE *fp)
1500 fprintf(fp, "# contains samples with branch stack\n");
1503 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1506 const char *delimiter = "# pmu mappings: ";
1511 pmu_num = ph->env.nr_pmu_mappings;
1513 fprintf(fp, "# pmu mappings: not available\n");
1517 str = ph->env.pmu_mappings;
1520 type = strtoul(str, &tmp, 0);
1525 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1528 str += strlen(str) + 1;
1537 fprintf(fp, "# pmu mappings: unable to read\n");
1540 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1543 struct perf_session *session;
1544 struct perf_evsel *evsel;
1547 session = container_of(ph, struct perf_session, header);
1549 evlist__for_each(session->evlist, evsel) {
1550 if (perf_evsel__is_group_leader(evsel) &&
1551 evsel->nr_members > 1) {
1552 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1553 perf_evsel__name(evsel));
1555 nr = evsel->nr_members - 1;
1557 fprintf(fp, ",%s", perf_evsel__name(evsel));
1565 static int __event_process_build_id(struct build_id_event *bev,
1567 struct perf_session *session)
1571 struct machine *machine;
1574 enum dso_kernel_type dso_type;
1576 machine = perf_session__findnew_machine(session, bev->pid);
1580 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1583 case PERF_RECORD_MISC_KERNEL:
1584 dso_type = DSO_TYPE_KERNEL;
1585 dsos = &machine->kernel_dsos;
1587 case PERF_RECORD_MISC_GUEST_KERNEL:
1588 dso_type = DSO_TYPE_GUEST_KERNEL;
1589 dsos = &machine->kernel_dsos;
1591 case PERF_RECORD_MISC_USER:
1592 case PERF_RECORD_MISC_GUEST_USER:
1593 dso_type = DSO_TYPE_USER;
1594 dsos = &machine->user_dsos;
1600 dso = __dsos__findnew(dsos, filename);
1602 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1604 dso__set_build_id(dso, &bev->build_id);
1606 if (filename[0] == '[')
1607 dso->kernel = dso_type;
1609 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1611 pr_debug("build id event received for %s: %s\n",
1612 dso->long_name, sbuild_id);
1620 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1621 int input, u64 offset, u64 size)
1623 struct perf_session *session = container_of(header, struct perf_session, header);
1625 struct perf_event_header header;
1626 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1629 struct build_id_event bev;
1630 char filename[PATH_MAX];
1631 u64 limit = offset + size;
1633 while (offset < limit) {
1636 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1639 if (header->needs_swap)
1640 perf_event_header__bswap(&old_bev.header);
1642 len = old_bev.header.size - sizeof(old_bev);
1643 if (readn(input, filename, len) != len)
1646 bev.header = old_bev.header;
1649 * As the pid is the missing value, we need to fill
1650 * it properly. The header.misc value give us nice hint.
1652 bev.pid = HOST_KERNEL_ID;
1653 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1654 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1655 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1657 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1658 __event_process_build_id(&bev, filename, session);
1660 offset += bev.header.size;
1666 static int perf_header__read_build_ids(struct perf_header *header,
1667 int input, u64 offset, u64 size)
1669 struct perf_session *session = container_of(header, struct perf_session, header);
1670 struct build_id_event bev;
1671 char filename[PATH_MAX];
1672 u64 limit = offset + size, orig_offset = offset;
1675 while (offset < limit) {
1678 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1681 if (header->needs_swap)
1682 perf_event_header__bswap(&bev.header);
1684 len = bev.header.size - sizeof(bev);
1685 if (readn(input, filename, len) != len)
1688 * The a1645ce1 changeset:
1690 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1692 * Added a field to struct build_id_event that broke the file
1695 * Since the kernel build-id is the first entry, process the
1696 * table using the old format if the well known
1697 * '[kernel.kallsyms]' string for the kernel build-id has the
1698 * first 4 characters chopped off (where the pid_t sits).
1700 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1701 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1703 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1706 __event_process_build_id(&bev, filename, session);
1708 offset += bev.header.size;
1715 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1716 struct perf_header *ph __maybe_unused,
1719 ssize_t ret = trace_report(fd, data, false);
1720 return ret < 0 ? -1 : 0;
1723 static int process_build_id(struct perf_file_section *section,
1724 struct perf_header *ph, int fd,
1725 void *data __maybe_unused)
1727 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1728 pr_debug("Failed to read buildids, continuing...\n");
1732 static int process_hostname(struct perf_file_section *section __maybe_unused,
1733 struct perf_header *ph, int fd,
1734 void *data __maybe_unused)
1736 ph->env.hostname = do_read_string(fd, ph);
1737 return ph->env.hostname ? 0 : -ENOMEM;
1740 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1741 struct perf_header *ph, int fd,
1742 void *data __maybe_unused)
1744 ph->env.os_release = do_read_string(fd, ph);
1745 return ph->env.os_release ? 0 : -ENOMEM;
1748 static int process_version(struct perf_file_section *section __maybe_unused,
1749 struct perf_header *ph, int fd,
1750 void *data __maybe_unused)
1752 ph->env.version = do_read_string(fd, ph);
1753 return ph->env.version ? 0 : -ENOMEM;
1756 static int process_arch(struct perf_file_section *section __maybe_unused,
1757 struct perf_header *ph, int fd,
1758 void *data __maybe_unused)
1760 ph->env.arch = do_read_string(fd, ph);
1761 return ph->env.arch ? 0 : -ENOMEM;
1764 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1765 struct perf_header *ph, int fd,
1766 void *data __maybe_unused)
1771 ret = readn(fd, &nr, sizeof(nr));
1772 if (ret != sizeof(nr))
1778 ph->env.nr_cpus_online = nr;
1780 ret = readn(fd, &nr, sizeof(nr));
1781 if (ret != sizeof(nr))
1787 ph->env.nr_cpus_avail = nr;
1791 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1792 struct perf_header *ph, int fd,
1793 void *data __maybe_unused)
1795 ph->env.cpu_desc = do_read_string(fd, ph);
1796 return ph->env.cpu_desc ? 0 : -ENOMEM;
1799 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1800 struct perf_header *ph, int fd,
1801 void *data __maybe_unused)
1803 ph->env.cpuid = do_read_string(fd, ph);
1804 return ph->env.cpuid ? 0 : -ENOMEM;
1807 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1808 struct perf_header *ph, int fd,
1809 void *data __maybe_unused)
1814 ret = readn(fd, &mem, sizeof(mem));
1815 if (ret != sizeof(mem))
1819 mem = bswap_64(mem);
1821 ph->env.total_mem = mem;
1825 static struct perf_evsel *
1826 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1828 struct perf_evsel *evsel;
1830 evlist__for_each(evlist, evsel) {
1831 if (evsel->idx == idx)
1839 perf_evlist__set_event_name(struct perf_evlist *evlist,
1840 struct perf_evsel *event)
1842 struct perf_evsel *evsel;
1847 evsel = perf_evlist__find_by_index(evlist, event->idx);
1854 evsel->name = strdup(event->name);
1858 process_event_desc(struct perf_file_section *section __maybe_unused,
1859 struct perf_header *header, int fd,
1860 void *data __maybe_unused)
1862 struct perf_session *session;
1863 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1868 session = container_of(header, struct perf_session, header);
1869 for (evsel = events; evsel->attr.size; evsel++)
1870 perf_evlist__set_event_name(session->evlist, evsel);
1872 free_event_desc(events);
1877 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1878 struct perf_header *ph, int fd,
1879 void *data __maybe_unused)
1886 ret = readn(fd, &nr, sizeof(nr));
1887 if (ret != sizeof(nr))
1893 ph->env.nr_cmdline = nr;
1894 strbuf_init(&sb, 128);
1896 for (i = 0; i < nr; i++) {
1897 str = do_read_string(fd, ph);
1901 /* include a NULL character at the end */
1902 strbuf_add(&sb, str, strlen(str) + 1);
1905 ph->env.cmdline = strbuf_detach(&sb, NULL);
1909 strbuf_release(&sb);
1913 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1914 struct perf_header *ph, int fd,
1915 void *data __maybe_unused)
1922 ret = readn(fd, &nr, sizeof(nr));
1923 if (ret != sizeof(nr))
1929 ph->env.nr_sibling_cores = nr;
1930 strbuf_init(&sb, 128);
1932 for (i = 0; i < nr; i++) {
1933 str = do_read_string(fd, ph);
1937 /* include a NULL character at the end */
1938 strbuf_add(&sb, str, strlen(str) + 1);
1941 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1943 ret = readn(fd, &nr, sizeof(nr));
1944 if (ret != sizeof(nr))
1950 ph->env.nr_sibling_threads = nr;
1952 for (i = 0; i < nr; i++) {
1953 str = do_read_string(fd, ph);
1957 /* include a NULL character at the end */
1958 strbuf_add(&sb, str, strlen(str) + 1);
1961 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1965 strbuf_release(&sb);
1969 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1970 struct perf_header *ph, int fd,
1971 void *data __maybe_unused)
1976 uint64_t mem_total, mem_free;
1980 ret = readn(fd, &nr, sizeof(nr));
1981 if (ret != sizeof(nr))
1987 ph->env.nr_numa_nodes = nr;
1988 strbuf_init(&sb, 256);
1990 for (i = 0; i < nr; i++) {
1992 ret = readn(fd, &node, sizeof(node));
1993 if (ret != sizeof(node))
1996 ret = readn(fd, &mem_total, sizeof(u64));
1997 if (ret != sizeof(u64))
2000 ret = readn(fd, &mem_free, sizeof(u64));
2001 if (ret != sizeof(u64))
2004 if (ph->needs_swap) {
2005 node = bswap_32(node);
2006 mem_total = bswap_64(mem_total);
2007 mem_free = bswap_64(mem_free);
2010 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
2011 node, mem_total, mem_free);
2013 str = do_read_string(fd, ph);
2017 /* include a NULL character at the end */
2018 strbuf_add(&sb, str, strlen(str) + 1);
2021 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
2025 strbuf_release(&sb);
2029 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
2030 struct perf_header *ph, int fd,
2031 void *data __maybe_unused)
2039 ret = readn(fd, &pmu_num, sizeof(pmu_num));
2040 if (ret != sizeof(pmu_num))
2044 pmu_num = bswap_32(pmu_num);
2047 pr_debug("pmu mappings not available\n");
2051 ph->env.nr_pmu_mappings = pmu_num;
2052 strbuf_init(&sb, 128);
2055 if (readn(fd, &type, sizeof(type)) != sizeof(type))
2058 type = bswap_32(type);
2060 name = do_read_string(fd, ph);
2064 strbuf_addf(&sb, "%u:%s", type, name);
2065 /* include a NULL character at the end */
2066 strbuf_add(&sb, "", 1);
2071 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2075 strbuf_release(&sb);
2079 static int process_group_desc(struct perf_file_section *section __maybe_unused,
2080 struct perf_header *ph, int fd,
2081 void *data __maybe_unused)
2084 u32 i, nr, nr_groups;
2085 struct perf_session *session;
2086 struct perf_evsel *evsel, *leader = NULL;
2093 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2097 nr_groups = bswap_32(nr_groups);
2099 ph->env.nr_groups = nr_groups;
2101 pr_debug("group desc not available\n");
2105 desc = calloc(nr_groups, sizeof(*desc));
2109 for (i = 0; i < nr_groups; i++) {
2110 desc[i].name = do_read_string(fd, ph);
2114 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2117 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2120 if (ph->needs_swap) {
2121 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2122 desc[i].nr_members = bswap_32(desc[i].nr_members);
2127 * Rebuild group relationship based on the group_desc
2129 session = container_of(ph, struct perf_session, header);
2130 session->evlist->nr_groups = nr_groups;
2133 evlist__for_each(session->evlist, evsel) {
2134 if (evsel->idx == (int) desc[i].leader_idx) {
2135 evsel->leader = evsel;
2136 /* {anon_group} is a dummy name */
2137 if (strcmp(desc[i].name, "{anon_group}")) {
2138 evsel->group_name = desc[i].name;
2139 desc[i].name = NULL;
2141 evsel->nr_members = desc[i].nr_members;
2143 if (i >= nr_groups || nr > 0) {
2144 pr_debug("invalid group desc\n");
2149 nr = evsel->nr_members - 1;
2152 /* This is a group member */
2153 evsel->leader = leader;
2159 if (i != nr_groups || nr != 0) {
2160 pr_debug("invalid group desc\n");
2166 for (i = 0; i < nr_groups; i++)
2167 zfree(&desc[i].name);
2173 struct feature_ops {
2174 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2175 void (*print)(struct perf_header *h, int fd, FILE *fp);
2176 int (*process)(struct perf_file_section *section,
2177 struct perf_header *h, int fd, void *data);
2182 #define FEAT_OPA(n, func) \
2183 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2184 #define FEAT_OPP(n, func) \
2185 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2186 .process = process_##func }
2187 #define FEAT_OPF(n, func) \
2188 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2189 .process = process_##func, .full_only = true }
2191 /* feature_ops not implemented: */
2192 #define print_tracing_data NULL
2193 #define print_build_id NULL
2195 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2196 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
2197 FEAT_OPP(HEADER_BUILD_ID, build_id),
2198 FEAT_OPP(HEADER_HOSTNAME, hostname),
2199 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2200 FEAT_OPP(HEADER_VERSION, version),
2201 FEAT_OPP(HEADER_ARCH, arch),
2202 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2203 FEAT_OPP(HEADER_CPUDESC, cpudesc),
2204 FEAT_OPP(HEADER_CPUID, cpuid),
2205 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
2206 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
2207 FEAT_OPP(HEADER_CMDLINE, cmdline),
2208 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2209 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
2210 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
2211 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2212 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
2215 struct header_print_data {
2217 bool full; /* extended list of headers */
2220 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2221 struct perf_header *ph,
2222 int feat, int fd, void *data)
2224 struct header_print_data *hd = data;
2226 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2227 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2228 "%d, continuing...\n", section->offset, feat);
2231 if (feat >= HEADER_LAST_FEATURE) {
2232 pr_warning("unknown feature %d\n", feat);
2235 if (!feat_ops[feat].print)
2238 if (!feat_ops[feat].full_only || hd->full)
2239 feat_ops[feat].print(ph, fd, hd->fp);
2241 fprintf(hd->fp, "# %s info available, use -I to display\n",
2242 feat_ops[feat].name);
2247 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2249 struct header_print_data hd;
2250 struct perf_header *header = &session->header;
2251 int fd = perf_data_file__fd(session->file);
2255 perf_header__process_sections(header, fd, &hd,
2256 perf_file_section__fprintf_info);
2260 static int do_write_feat(int fd, struct perf_header *h, int type,
2261 struct perf_file_section **p,
2262 struct perf_evlist *evlist)
2267 if (perf_header__has_feat(h, type)) {
2268 if (!feat_ops[type].write)
2271 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2273 err = feat_ops[type].write(fd, h, evlist);
2275 pr_debug("failed to write feature %d\n", type);
2277 /* undo anything written */
2278 lseek(fd, (*p)->offset, SEEK_SET);
2282 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2288 static int perf_header__adds_write(struct perf_header *header,
2289 struct perf_evlist *evlist, int fd)
2292 struct perf_file_section *feat_sec, *p;
2298 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2302 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2303 if (feat_sec == NULL)
2306 sec_size = sizeof(*feat_sec) * nr_sections;
2308 sec_start = header->feat_offset;
2309 lseek(fd, sec_start + sec_size, SEEK_SET);
2311 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2312 if (do_write_feat(fd, header, feat, &p, evlist))
2313 perf_header__clear_feat(header, feat);
2316 lseek(fd, sec_start, SEEK_SET);
2318 * may write more than needed due to dropped feature, but
2319 * this is okay, reader will skip the mising entries
2321 err = do_write(fd, feat_sec, sec_size);
2323 pr_debug("failed to write feature section\n");
2328 int perf_header__write_pipe(int fd)
2330 struct perf_pipe_file_header f_header;
2333 f_header = (struct perf_pipe_file_header){
2334 .magic = PERF_MAGIC,
2335 .size = sizeof(f_header),
2338 err = do_write(fd, &f_header, sizeof(f_header));
2340 pr_debug("failed to write perf pipe header\n");
2347 int perf_session__write_header(struct perf_session *session,
2348 struct perf_evlist *evlist,
2349 int fd, bool at_exit)
2351 struct perf_file_header f_header;
2352 struct perf_file_attr f_attr;
2353 struct perf_header *header = &session->header;
2354 struct perf_evsel *evsel;
2358 lseek(fd, sizeof(f_header), SEEK_SET);
2360 evlist__for_each(session->evlist, evsel) {
2361 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2362 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2364 pr_debug("failed to write perf header\n");
2369 attr_offset = lseek(fd, 0, SEEK_CUR);
2371 evlist__for_each(evlist, evsel) {
2372 f_attr = (struct perf_file_attr){
2373 .attr = evsel->attr,
2375 .offset = evsel->id_offset,
2376 .size = evsel->ids * sizeof(u64),
2379 err = do_write(fd, &f_attr, sizeof(f_attr));
2381 pr_debug("failed to write perf header attribute\n");
2386 if (!header->data_offset)
2387 header->data_offset = lseek(fd, 0, SEEK_CUR);
2388 header->feat_offset = header->data_offset + header->data_size;
2391 err = perf_header__adds_write(header, evlist, fd);
2396 f_header = (struct perf_file_header){
2397 .magic = PERF_MAGIC,
2398 .size = sizeof(f_header),
2399 .attr_size = sizeof(f_attr),
2401 .offset = attr_offset,
2402 .size = evlist->nr_entries * sizeof(f_attr),
2405 .offset = header->data_offset,
2406 .size = header->data_size,
2408 /* event_types is ignored, store zeros */
2411 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2413 lseek(fd, 0, SEEK_SET);
2414 err = do_write(fd, &f_header, sizeof(f_header));
2416 pr_debug("failed to write perf header\n");
2419 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2424 static int perf_header__getbuffer64(struct perf_header *header,
2425 int fd, void *buf, size_t size)
2427 if (readn(fd, buf, size) <= 0)
2430 if (header->needs_swap)
2431 mem_bswap_64(buf, size);
2436 int perf_header__process_sections(struct perf_header *header, int fd,
2438 int (*process)(struct perf_file_section *section,
2439 struct perf_header *ph,
2440 int feat, int fd, void *data))
2442 struct perf_file_section *feat_sec, *sec;
2448 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2452 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2456 sec_size = sizeof(*feat_sec) * nr_sections;
2458 lseek(fd, header->feat_offset, SEEK_SET);
2460 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2464 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2465 err = process(sec++, header, feat, fd, data);
2475 static const int attr_file_abi_sizes[] = {
2476 [0] = PERF_ATTR_SIZE_VER0,
2477 [1] = PERF_ATTR_SIZE_VER1,
2478 [2] = PERF_ATTR_SIZE_VER2,
2479 [3] = PERF_ATTR_SIZE_VER3,
2484 * In the legacy file format, the magic number is not used to encode endianness.
2485 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2486 * on ABI revisions, we need to try all combinations for all endianness to
2487 * detect the endianness.
2489 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2491 uint64_t ref_size, attr_size;
2494 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2495 ref_size = attr_file_abi_sizes[i]
2496 + sizeof(struct perf_file_section);
2497 if (hdr_sz != ref_size) {
2498 attr_size = bswap_64(hdr_sz);
2499 if (attr_size != ref_size)
2502 ph->needs_swap = true;
2504 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2509 /* could not determine endianness */
2513 #define PERF_PIPE_HDR_VER0 16
2515 static const size_t attr_pipe_abi_sizes[] = {
2516 [0] = PERF_PIPE_HDR_VER0,
2521 * In the legacy pipe format, there is an implicit assumption that endiannesss
2522 * between host recording the samples, and host parsing the samples is the
2523 * same. This is not always the case given that the pipe output may always be
2524 * redirected into a file and analyzed on a different machine with possibly a
2525 * different endianness and perf_event ABI revsions in the perf tool itself.
2527 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2532 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2533 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2534 attr_size = bswap_64(hdr_sz);
2535 if (attr_size != hdr_sz)
2538 ph->needs_swap = true;
2540 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2546 bool is_perf_magic(u64 magic)
2548 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2549 || magic == __perf_magic2
2550 || magic == __perf_magic2_sw)
2556 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2557 bool is_pipe, struct perf_header *ph)
2561 /* check for legacy format */
2562 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2564 ph->version = PERF_HEADER_VERSION_1;
2565 pr_debug("legacy perf.data format\n");
2567 return try_all_pipe_abis(hdr_sz, ph);
2569 return try_all_file_abis(hdr_sz, ph);
2572 * the new magic number serves two purposes:
2573 * - unique number to identify actual perf.data files
2574 * - encode endianness of file
2577 /* check magic number with one endianness */
2578 if (magic == __perf_magic2)
2581 /* check magic number with opposite endianness */
2582 if (magic != __perf_magic2_sw)
2585 ph->needs_swap = true;
2586 ph->version = PERF_HEADER_VERSION_2;
2591 int perf_file_header__read(struct perf_file_header *header,
2592 struct perf_header *ph, int fd)
2596 lseek(fd, 0, SEEK_SET);
2598 ret = readn(fd, header, sizeof(*header));
2602 if (check_magic_endian(header->magic,
2603 header->attr_size, false, ph) < 0) {
2604 pr_debug("magic/endian check failed\n");
2608 if (ph->needs_swap) {
2609 mem_bswap_64(header, offsetof(struct perf_file_header,
2613 if (header->size != sizeof(*header)) {
2614 /* Support the previous format */
2615 if (header->size == offsetof(typeof(*header), adds_features))
2616 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2619 } else if (ph->needs_swap) {
2621 * feature bitmap is declared as an array of unsigned longs --
2622 * not good since its size can differ between the host that
2623 * generated the data file and the host analyzing the file.
2625 * We need to handle endianness, but we don't know the size of
2626 * the unsigned long where the file was generated. Take a best
2627 * guess at determining it: try 64-bit swap first (ie., file
2628 * created on a 64-bit host), and check if the hostname feature
2629 * bit is set (this feature bit is forced on as of fbe96f2).
2630 * If the bit is not, undo the 64-bit swap and try a 32-bit
2631 * swap. If the hostname bit is still not set (e.g., older data
2632 * file), punt and fallback to the original behavior --
2633 * clearing all feature bits and setting buildid.
2635 mem_bswap_64(&header->adds_features,
2636 BITS_TO_U64(HEADER_FEAT_BITS));
2638 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2640 mem_bswap_64(&header->adds_features,
2641 BITS_TO_U64(HEADER_FEAT_BITS));
2644 mem_bswap_32(&header->adds_features,
2645 BITS_TO_U32(HEADER_FEAT_BITS));
2648 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2649 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2650 set_bit(HEADER_BUILD_ID, header->adds_features);
2654 memcpy(&ph->adds_features, &header->adds_features,
2655 sizeof(ph->adds_features));
2657 ph->data_offset = header->data.offset;
2658 ph->data_size = header->data.size;
2659 ph->feat_offset = header->data.offset + header->data.size;
2663 static int perf_file_section__process(struct perf_file_section *section,
2664 struct perf_header *ph,
2665 int feat, int fd, void *data)
2667 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2668 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2669 "%d, continuing...\n", section->offset, feat);
2673 if (feat >= HEADER_LAST_FEATURE) {
2674 pr_debug("unknown feature %d, continuing...\n", feat);
2678 if (!feat_ops[feat].process)
2681 return feat_ops[feat].process(section, ph, fd, data);
2684 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2685 struct perf_header *ph, int fd,
2690 ret = readn(fd, header, sizeof(*header));
2694 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2695 pr_debug("endian/magic failed\n");
2700 header->size = bswap_64(header->size);
2702 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2708 static int perf_header__read_pipe(struct perf_session *session)
2710 struct perf_header *header = &session->header;
2711 struct perf_pipe_file_header f_header;
2713 if (perf_file_header__read_pipe(&f_header, header,
2714 perf_data_file__fd(session->file),
2715 session->repipe) < 0) {
2716 pr_debug("incompatible file format\n");
2723 static int read_attr(int fd, struct perf_header *ph,
2724 struct perf_file_attr *f_attr)
2726 struct perf_event_attr *attr = &f_attr->attr;
2728 size_t our_sz = sizeof(f_attr->attr);
2731 memset(f_attr, 0, sizeof(*f_attr));
2733 /* read minimal guaranteed structure */
2734 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2736 pr_debug("cannot read %d bytes of header attr\n",
2737 PERF_ATTR_SIZE_VER0);
2741 /* on file perf_event_attr size */
2749 sz = PERF_ATTR_SIZE_VER0;
2750 } else if (sz > our_sz) {
2751 pr_debug("file uses a more recent and unsupported ABI"
2752 " (%zu bytes extra)\n", sz - our_sz);
2755 /* what we have not yet read and that we know about */
2756 left = sz - PERF_ATTR_SIZE_VER0;
2759 ptr += PERF_ATTR_SIZE_VER0;
2761 ret = readn(fd, ptr, left);
2763 /* read perf_file_section, ids are read in caller */
2764 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2766 return ret <= 0 ? -1 : 0;
2769 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2770 struct pevent *pevent)
2772 struct event_format *event;
2775 /* already prepared */
2776 if (evsel->tp_format)
2779 if (pevent == NULL) {
2780 pr_debug("broken or missing trace data\n");
2784 event = pevent_find_event(pevent, evsel->attr.config);
2789 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2790 evsel->name = strdup(bf);
2791 if (evsel->name == NULL)
2795 evsel->tp_format = event;
2799 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2800 struct pevent *pevent)
2802 struct perf_evsel *pos;
2804 evlist__for_each(evlist, pos) {
2805 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2806 perf_evsel__prepare_tracepoint_event(pos, pevent))
2813 int perf_session__read_header(struct perf_session *session)
2815 struct perf_data_file *file = session->file;
2816 struct perf_header *header = &session->header;
2817 struct perf_file_header f_header;
2818 struct perf_file_attr f_attr;
2820 int nr_attrs, nr_ids, i, j;
2821 int fd = perf_data_file__fd(file);
2823 session->evlist = perf_evlist__new();
2824 if (session->evlist == NULL)
2827 if (perf_data_file__is_pipe(file))
2828 return perf_header__read_pipe(session);
2830 if (perf_file_header__read(&f_header, header, fd) < 0)
2834 * Sanity check that perf.data was written cleanly; data size is
2835 * initialized to 0 and updated only if the on_exit function is run.
2836 * If data size is still 0 then the file contains only partial
2837 * information. Just warn user and process it as much as it can.
2839 if (f_header.data.size == 0) {
2840 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2841 "Was the 'perf record' command properly terminated?\n",
2845 nr_attrs = f_header.attrs.size / f_header.attr_size;
2846 lseek(fd, f_header.attrs.offset, SEEK_SET);
2848 for (i = 0; i < nr_attrs; i++) {
2849 struct perf_evsel *evsel;
2852 if (read_attr(fd, header, &f_attr) < 0)
2855 if (header->needs_swap)
2856 perf_event__attr_swap(&f_attr.attr);
2858 tmp = lseek(fd, 0, SEEK_CUR);
2859 evsel = perf_evsel__new(&f_attr.attr);
2862 goto out_delete_evlist;
2864 evsel->needs_swap = header->needs_swap;
2866 * Do it before so that if perf_evsel__alloc_id fails, this
2867 * entry gets purged too at perf_evlist__delete().
2869 perf_evlist__add(session->evlist, evsel);
2871 nr_ids = f_attr.ids.size / sizeof(u64);
2873 * We don't have the cpu and thread maps on the header, so
2874 * for allocating the perf_sample_id table we fake 1 cpu and
2875 * hattr->ids threads.
2877 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2878 goto out_delete_evlist;
2880 lseek(fd, f_attr.ids.offset, SEEK_SET);
2882 for (j = 0; j < nr_ids; j++) {
2883 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2886 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2889 lseek(fd, tmp, SEEK_SET);
2892 symbol_conf.nr_events = nr_attrs;
2894 perf_header__process_sections(header, fd, &session->tevent,
2895 perf_file_section__process);
2897 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2898 session->tevent.pevent))
2899 goto out_delete_evlist;
2906 perf_evlist__delete(session->evlist);
2907 session->evlist = NULL;
2911 int perf_event__synthesize_attr(struct perf_tool *tool,
2912 struct perf_event_attr *attr, u32 ids, u64 *id,
2913 perf_event__handler_t process)
2915 union perf_event *ev;
2919 size = sizeof(struct perf_event_attr);
2920 size = PERF_ALIGN(size, sizeof(u64));
2921 size += sizeof(struct perf_event_header);
2922 size += ids * sizeof(u64);
2929 ev->attr.attr = *attr;
2930 memcpy(ev->attr.id, id, ids * sizeof(u64));
2932 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2933 ev->attr.header.size = (u16)size;
2935 if (ev->attr.header.size == size)
2936 err = process(tool, ev, NULL, NULL);
2945 int perf_event__synthesize_attrs(struct perf_tool *tool,
2946 struct perf_session *session,
2947 perf_event__handler_t process)
2949 struct perf_evsel *evsel;
2952 evlist__for_each(session->evlist, evsel) {
2953 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2954 evsel->id, process);
2956 pr_debug("failed to create perf header attribute\n");
2964 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2965 union perf_event *event,
2966 struct perf_evlist **pevlist)
2969 struct perf_evsel *evsel;
2970 struct perf_evlist *evlist = *pevlist;
2972 if (evlist == NULL) {
2973 *pevlist = evlist = perf_evlist__new();
2978 evsel = perf_evsel__new(&event->attr.attr);
2982 perf_evlist__add(evlist, evsel);
2984 ids = event->header.size;
2985 ids -= (void *)&event->attr.id - (void *)event;
2986 n_ids = ids / sizeof(u64);
2988 * We don't have the cpu and thread maps on the header, so
2989 * for allocating the perf_sample_id table we fake 1 cpu and
2990 * hattr->ids threads.
2992 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2995 for (i = 0; i < n_ids; i++) {
2996 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2999 symbol_conf.nr_events = evlist->nr_entries;
3004 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3005 struct perf_evlist *evlist,
3006 perf_event__handler_t process)
3008 union perf_event ev;
3009 struct tracing_data *tdata;
3010 ssize_t size = 0, aligned_size = 0, padding;
3011 int err __maybe_unused = 0;
3014 * We are going to store the size of the data followed
3015 * by the data contents. Since the fd descriptor is a pipe,
3016 * we cannot seek back to store the size of the data once
3017 * we know it. Instead we:
3019 * - write the tracing data to the temp file
3020 * - get/write the data size to pipe
3021 * - write the tracing data from the temp file
3024 tdata = tracing_data_get(&evlist->entries, fd, true);
3028 memset(&ev, 0, sizeof(ev));
3030 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3032 aligned_size = PERF_ALIGN(size, sizeof(u64));
3033 padding = aligned_size - size;
3034 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3035 ev.tracing_data.size = aligned_size;
3037 process(tool, &ev, NULL, NULL);
3040 * The put function will copy all the tracing data
3041 * stored in temp file to the pipe.
3043 tracing_data_put(tdata);
3045 write_padded(fd, NULL, 0, padding);
3047 return aligned_size;
3050 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3051 union perf_event *event,
3052 struct perf_session *session)
3054 ssize_t size_read, padding, size = event->tracing_data.size;
3055 int fd = perf_data_file__fd(session->file);
3056 off_t offset = lseek(fd, 0, SEEK_CUR);
3059 /* setup for reading amidst mmap */
3060 lseek(fd, offset + sizeof(struct tracing_data_event),
3063 size_read = trace_report(fd, &session->tevent,
3065 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3067 if (readn(fd, buf, padding) < 0) {
3068 pr_err("%s: reading input file", __func__);
3071 if (session->repipe) {
3072 int retw = write(STDOUT_FILENO, buf, padding);
3073 if (retw <= 0 || retw != padding) {
3074 pr_err("%s: repiping tracing data padding", __func__);
3079 if (size_read + padding != size) {
3080 pr_err("%s: tracing data size mismatch", __func__);
3084 perf_evlist__prepare_tracepoint_events(session->evlist,
3085 session->tevent.pevent);
3087 return size_read + padding;
3090 int perf_event__synthesize_build_id(struct perf_tool *tool,
3091 struct dso *pos, u16 misc,
3092 perf_event__handler_t process,
3093 struct machine *machine)
3095 union perf_event ev;
3102 memset(&ev, 0, sizeof(ev));
3104 len = pos->long_name_len + 1;
3105 len = PERF_ALIGN(len, NAME_ALIGN);
3106 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3107 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3108 ev.build_id.header.misc = misc;
3109 ev.build_id.pid = machine->pid;
3110 ev.build_id.header.size = sizeof(ev.build_id) + len;
3111 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3113 err = process(tool, &ev, NULL, machine);
3118 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3119 union perf_event *event,
3120 struct perf_session *session)
3122 __event_process_build_id(&event->build_id,
3123 event->build_id.filename,
3128 void disable_buildid_cache(void)
3130 no_buildid_cache = true;