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"
26 static bool no_buildid_cache = false;
28 static u32 header_argc;
29 static const char **header_argv;
33 * must be a numerical value to let the endianness
34 * determine the memory layout. That way we are able
35 * to detect endianness when reading the perf.data file
38 * we check for legacy (PERFFILE) format.
40 static const char *__perf_magic1 = "PERFFILE";
41 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
42 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
44 #define PERF_MAGIC __perf_magic2
46 struct perf_file_attr {
47 struct perf_event_attr attr;
48 struct perf_file_section ids;
51 void perf_header__set_feat(struct perf_header *header, int feat)
53 set_bit(feat, header->adds_features);
56 void perf_header__clear_feat(struct perf_header *header, int feat)
58 clear_bit(feat, header->adds_features);
61 bool perf_header__has_feat(const struct perf_header *header, int feat)
63 return test_bit(feat, header->adds_features);
66 static int do_write(int fd, const void *buf, size_t size)
69 int ret = write(fd, buf, size);
83 static int write_padded(int fd, const void *bf, size_t count,
86 static const char zero_buf[NAME_ALIGN];
87 int err = do_write(fd, bf, count);
90 err = do_write(fd, zero_buf, count_aligned - count);
95 static int do_write_string(int fd, const char *str)
100 olen = strlen(str) + 1;
101 len = PERF_ALIGN(olen, NAME_ALIGN);
103 /* write len, incl. \0 */
104 ret = do_write(fd, &len, sizeof(len));
108 return write_padded(fd, str, olen, len);
111 static char *do_read_string(int fd, struct perf_header *ph)
117 sz = readn(fd, &len, sizeof(len));
118 if (sz < (ssize_t)sizeof(len))
128 ret = readn(fd, buf, len);
129 if (ret == (ssize_t)len) {
131 * strings are padded by zeroes
132 * thus the actual strlen of buf
133 * may be less than len
143 perf_header__set_cmdline(int argc, const char **argv)
148 * If header_argv has already been set, do not override it.
149 * This allows a command to set the cmdline, parse args and
150 * then call another builtin function that implements a
151 * command -- e.g, cmd_kvm calling cmd_record.
156 header_argc = (u32)argc;
158 /* do not include NULL termination */
159 header_argv = calloc(argc, sizeof(char *));
164 * must copy argv contents because it gets moved
165 * around during option parsing
167 for (i = 0; i < argc ; i++)
168 header_argv[i] = argv[i];
173 #define dsos__for_each_with_build_id(pos, head) \
174 list_for_each_entry(pos, head, node) \
175 if (!pos->has_build_id) \
179 static int write_buildid(char *name, size_t name_len, u8 *build_id,
180 pid_t pid, u16 misc, int fd)
183 struct build_id_event b;
187 len = PERF_ALIGN(len, NAME_ALIGN);
189 memset(&b, 0, sizeof(b));
190 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
192 b.header.misc = misc;
193 b.header.size = sizeof(b) + len;
195 err = do_write(fd, &b, sizeof(b));
199 return write_padded(fd, name, name_len + 1, len);
202 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
207 dsos__for_each_with_build_id(pos, head) {
215 if (is_vdso_map(pos->short_name)) {
216 name = (char *) VDSO__MAP_NAME;
217 name_len = sizeof(VDSO__MAP_NAME) + 1;
219 name = pos->long_name;
220 name_len = pos->long_name_len + 1;
223 err = write_buildid(name, name_len, pos->build_id,
232 static int machine__write_buildid_table(struct machine *machine, int fd)
235 u16 kmisc = PERF_RECORD_MISC_KERNEL,
236 umisc = PERF_RECORD_MISC_USER;
238 if (!machine__is_host(machine)) {
239 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
240 umisc = PERF_RECORD_MISC_GUEST_USER;
243 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
246 err = __dsos__write_buildid_table(&machine->user_dsos,
247 machine->pid, umisc, fd);
251 static int dsos__write_buildid_table(struct perf_header *header, int fd)
253 struct perf_session *session = container_of(header,
254 struct perf_session, header);
256 int err = machine__write_buildid_table(&session->machines.host, fd);
261 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
262 struct machine *pos = rb_entry(nd, struct machine, rb_node);
263 err = machine__write_buildid_table(pos, fd);
270 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
271 const char *name, bool is_kallsyms, bool is_vdso)
273 const size_t size = PATH_MAX;
274 char *realname, *filename = zalloc(size),
275 *linkname = zalloc(size), *targetname;
277 bool slash = is_kallsyms || is_vdso;
280 if (symbol_conf.kptr_restrict) {
281 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
285 realname = (char *) name;
287 realname = realpath(name, NULL);
289 if (realname == NULL || filename == NULL || linkname == NULL)
292 len = scnprintf(filename, size, "%s%s%s",
293 debugdir, slash ? "/" : "",
294 is_vdso ? VDSO__MAP_NAME : realname);
295 if (mkdir_p(filename, 0755))
298 snprintf(filename + len, size - len, "/%s", sbuild_id);
300 if (access(filename, F_OK)) {
302 if (copyfile("/proc/kallsyms", filename))
304 } else if (link(realname, filename) && copyfile(name, filename))
308 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
309 debugdir, sbuild_id);
311 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
314 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
315 targetname = filename + strlen(debugdir) - 5;
316 memcpy(targetname, "../..", 5);
318 if (symlink(targetname, linkname) == 0)
328 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
329 const char *name, const char *debugdir,
330 bool is_kallsyms, bool is_vdso)
332 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
334 build_id__sprintf(build_id, build_id_size, sbuild_id);
336 return build_id_cache__add_s(sbuild_id, debugdir, name,
337 is_kallsyms, is_vdso);
340 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
342 const size_t size = PATH_MAX;
343 char *filename = zalloc(size),
344 *linkname = zalloc(size);
347 if (filename == NULL || linkname == NULL)
350 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
351 debugdir, sbuild_id, sbuild_id + 2);
353 if (access(linkname, F_OK))
356 if (readlink(linkname, filename, size - 1) < 0)
359 if (unlink(linkname))
363 * Since the link is relative, we must make it absolute:
365 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
366 debugdir, sbuild_id, filename);
368 if (unlink(linkname))
378 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
380 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
381 bool is_vdso = is_vdso_map(dso->short_name);
383 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
384 dso->long_name, debugdir,
385 is_kallsyms, is_vdso);
388 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
393 dsos__for_each_with_build_id(pos, head)
394 if (dso__cache_build_id(pos, debugdir))
400 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
402 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
403 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
407 static int perf_session__cache_build_ids(struct perf_session *session)
411 char debugdir[PATH_MAX];
413 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
415 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
418 ret = machine__cache_build_ids(&session->machines.host, debugdir);
420 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
421 struct machine *pos = rb_entry(nd, struct machine, rb_node);
422 ret |= machine__cache_build_ids(pos, debugdir);
427 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
429 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
430 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
434 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
437 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
439 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
440 struct machine *pos = rb_entry(nd, struct machine, rb_node);
441 ret |= machine__read_build_ids(pos, with_hits);
447 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
448 struct perf_evlist *evlist)
450 return read_tracing_data(fd, &evlist->entries);
454 static int write_build_id(int fd, struct perf_header *h,
455 struct perf_evlist *evlist __maybe_unused)
457 struct perf_session *session;
460 session = container_of(h, struct perf_session, header);
462 if (!perf_session__read_build_ids(session, true))
465 err = dsos__write_buildid_table(h, fd);
467 pr_debug("failed to write buildid table\n");
470 if (!no_buildid_cache)
471 perf_session__cache_build_ids(session);
476 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
477 struct perf_evlist *evlist __maybe_unused)
486 return do_write_string(fd, uts.nodename);
489 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
490 struct perf_evlist *evlist __maybe_unused)
499 return do_write_string(fd, uts.release);
502 static int write_arch(int fd, struct perf_header *h __maybe_unused,
503 struct perf_evlist *evlist __maybe_unused)
512 return do_write_string(fd, uts.machine);
515 static int write_version(int fd, struct perf_header *h __maybe_unused,
516 struct perf_evlist *evlist __maybe_unused)
518 return do_write_string(fd, perf_version_string);
521 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
522 struct perf_evlist *evlist __maybe_unused)
525 #define CPUINFO_PROC NULL
530 const char *search = CPUINFO_PROC;
537 file = fopen("/proc/cpuinfo", "r");
541 while (getline(&buf, &len, file) > 0) {
542 ret = strncmp(buf, search, strlen(search));
552 p = strchr(buf, ':');
553 if (p && *(p+1) == ' ' && *(p+2))
559 /* squash extra space characters (branding string) */
566 while (*q && isspace(*q))
569 while ((*r++ = *q++));
573 ret = do_write_string(fd, s);
580 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
581 struct perf_evlist *evlist __maybe_unused)
587 nr = sysconf(_SC_NPROCESSORS_CONF);
591 nrc = (u32)(nr & UINT_MAX);
593 nr = sysconf(_SC_NPROCESSORS_ONLN);
597 nra = (u32)(nr & UINT_MAX);
599 ret = do_write(fd, &nrc, sizeof(nrc));
603 return do_write(fd, &nra, sizeof(nra));
606 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
607 struct perf_evlist *evlist)
609 struct perf_evsel *evsel;
613 nre = evlist->nr_entries;
616 * write number of events
618 ret = do_write(fd, &nre, sizeof(nre));
623 * size of perf_event_attr struct
625 sz = (u32)sizeof(evsel->attr);
626 ret = do_write(fd, &sz, sizeof(sz));
630 list_for_each_entry(evsel, &evlist->entries, node) {
632 ret = do_write(fd, &evsel->attr, sz);
636 * write number of unique id per event
637 * there is one id per instance of an event
639 * copy into an nri to be independent of the
643 ret = do_write(fd, &nri, sizeof(nri));
648 * write event string as passed on cmdline
650 ret = do_write_string(fd, perf_evsel__name(evsel));
654 * write unique ids for this event
656 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
663 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
664 struct perf_evlist *evlist __maybe_unused)
666 char buf[MAXPATHLEN];
672 * actual atual path to perf binary
674 sprintf(proc, "/proc/%d/exe", getpid());
675 ret = readlink(proc, buf, sizeof(buf));
679 /* readlink() does not add null termination */
682 /* account for binary path */
685 ret = do_write(fd, &n, sizeof(n));
689 ret = do_write_string(fd, buf);
693 for (i = 0 ; i < header_argc; i++) {
694 ret = do_write_string(fd, header_argv[i]);
701 #define CORE_SIB_FMT \
702 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
703 #define THRD_SIB_FMT \
704 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
709 char **core_siblings;
710 char **thread_siblings;
713 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
716 char filename[MAXPATHLEN];
717 char *buf = NULL, *p;
723 sprintf(filename, CORE_SIB_FMT, cpu);
724 fp = fopen(filename, "r");
728 sret = getline(&buf, &len, fp);
733 p = strchr(buf, '\n');
737 for (i = 0; i < tp->core_sib; i++) {
738 if (!strcmp(buf, tp->core_siblings[i]))
741 if (i == tp->core_sib) {
742 tp->core_siblings[i] = buf;
750 sprintf(filename, THRD_SIB_FMT, cpu);
751 fp = fopen(filename, "r");
755 if (getline(&buf, &len, fp) <= 0)
758 p = strchr(buf, '\n');
762 for (i = 0; i < tp->thread_sib; i++) {
763 if (!strcmp(buf, tp->thread_siblings[i]))
766 if (i == tp->thread_sib) {
767 tp->thread_siblings[i] = buf;
779 static void free_cpu_topo(struct cpu_topo *tp)
786 for (i = 0 ; i < tp->core_sib; i++)
787 free(tp->core_siblings[i]);
789 for (i = 0 ; i < tp->thread_sib; i++)
790 free(tp->thread_siblings[i]);
795 static struct cpu_topo *build_cpu_topology(void)
804 ncpus = sysconf(_SC_NPROCESSORS_CONF);
808 nr = (u32)(ncpus & UINT_MAX);
810 sz = nr * sizeof(char *);
812 addr = calloc(1, sizeof(*tp) + 2 * sz);
819 tp->core_siblings = addr;
821 tp->thread_siblings = addr;
823 for (i = 0; i < nr; i++) {
824 ret = build_cpu_topo(tp, i);
835 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
836 struct perf_evlist *evlist __maybe_unused)
842 tp = build_cpu_topology();
846 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
850 for (i = 0; i < tp->core_sib; i++) {
851 ret = do_write_string(fd, tp->core_siblings[i]);
855 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
859 for (i = 0; i < tp->thread_sib; i++) {
860 ret = do_write_string(fd, tp->thread_siblings[i]);
871 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
872 struct perf_evlist *evlist __maybe_unused)
880 fp = fopen("/proc/meminfo", "r");
884 while (getline(&buf, &len, fp) > 0) {
885 ret = strncmp(buf, "MemTotal:", 9);
890 n = sscanf(buf, "%*s %"PRIu64, &mem);
892 ret = do_write(fd, &mem, sizeof(mem));
899 static int write_topo_node(int fd, int node)
901 char str[MAXPATHLEN];
903 char *buf = NULL, *p;
906 u64 mem_total, mem_free, mem;
909 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
910 fp = fopen(str, "r");
914 while (getline(&buf, &len, fp) > 0) {
915 /* skip over invalid lines */
916 if (!strchr(buf, ':'))
918 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
920 if (!strcmp(field, "MemTotal:"))
922 if (!strcmp(field, "MemFree:"))
929 ret = do_write(fd, &mem_total, sizeof(u64));
933 ret = do_write(fd, &mem_free, sizeof(u64));
938 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
940 fp = fopen(str, "r");
944 if (getline(&buf, &len, fp) <= 0)
947 p = strchr(buf, '\n');
951 ret = do_write_string(fd, buf);
959 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
960 struct perf_evlist *evlist __maybe_unused)
965 struct cpu_map *node_map = NULL;
970 fp = fopen("/sys/devices/system/node/online", "r");
974 if (getline(&buf, &len, fp) <= 0)
977 c = strchr(buf, '\n');
981 node_map = cpu_map__new(buf);
985 nr = (u32)node_map->nr;
987 ret = do_write(fd, &nr, sizeof(nr));
991 for (i = 0; i < nr; i++) {
992 j = (u32)node_map->map[i];
993 ret = do_write(fd, &j, sizeof(j));
997 ret = write_topo_node(fd, i);
1011 * struct pmu_mappings {
1020 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1021 struct perf_evlist *evlist __maybe_unused)
1023 struct perf_pmu *pmu = NULL;
1024 off_t offset = lseek(fd, 0, SEEK_CUR);
1028 /* write real pmu_num later */
1029 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1033 while ((pmu = perf_pmu__scan(pmu))) {
1038 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1042 ret = do_write_string(fd, pmu->name);
1047 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1049 lseek(fd, offset, SEEK_SET);
1059 * struct group_descs {
1061 * struct group_desc {
1068 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1069 struct perf_evlist *evlist)
1071 u32 nr_groups = evlist->nr_groups;
1072 struct perf_evsel *evsel;
1075 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1079 list_for_each_entry(evsel, &evlist->entries, node) {
1080 if (perf_evsel__is_group_leader(evsel) &&
1081 evsel->nr_members > 1) {
1082 const char *name = evsel->group_name ?: "{anon_group}";
1083 u32 leader_idx = evsel->idx;
1084 u32 nr_members = evsel->nr_members;
1086 ret = do_write_string(fd, name);
1090 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1094 ret = do_write(fd, &nr_members, sizeof(nr_members));
1103 * default get_cpuid(): nothing gets recorded
1104 * actual implementation must be in arch/$(ARCH)/util/header.c
1106 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1107 size_t sz __maybe_unused)
1112 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1113 struct perf_evlist *evlist __maybe_unused)
1118 ret = get_cpuid(buffer, sizeof(buffer));
1124 return do_write_string(fd, buffer);
1127 static int write_branch_stack(int fd __maybe_unused,
1128 struct perf_header *h __maybe_unused,
1129 struct perf_evlist *evlist __maybe_unused)
1134 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1137 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1140 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1143 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1146 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1148 fprintf(fp, "# arch : %s\n", ph->env.arch);
1151 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1154 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1157 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1160 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1161 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1164 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1167 fprintf(fp, "# perf version : %s\n", ph->env.version);
1170 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1176 nr = ph->env.nr_cmdline;
1177 str = ph->env.cmdline;
1179 fprintf(fp, "# cmdline : ");
1181 for (i = 0; i < nr; i++) {
1182 fprintf(fp, "%s ", str);
1183 str += strlen(str) + 1;
1188 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1194 nr = ph->env.nr_sibling_cores;
1195 str = ph->env.sibling_cores;
1197 for (i = 0; i < nr; i++) {
1198 fprintf(fp, "# sibling cores : %s\n", str);
1199 str += strlen(str) + 1;
1202 nr = ph->env.nr_sibling_threads;
1203 str = ph->env.sibling_threads;
1205 for (i = 0; i < nr; i++) {
1206 fprintf(fp, "# sibling threads : %s\n", str);
1207 str += strlen(str) + 1;
1211 static void free_event_desc(struct perf_evsel *events)
1213 struct perf_evsel *evsel;
1218 for (evsel = events; evsel->attr.size; evsel++) {
1228 static struct perf_evsel *
1229 read_event_desc(struct perf_header *ph, int fd)
1231 struct perf_evsel *evsel, *events = NULL;
1234 u32 nre, sz, nr, i, j;
1238 /* number of events */
1239 ret = readn(fd, &nre, sizeof(nre));
1240 if (ret != (ssize_t)sizeof(nre))
1244 nre = bswap_32(nre);
1246 ret = readn(fd, &sz, sizeof(sz));
1247 if (ret != (ssize_t)sizeof(sz))
1253 /* buffer to hold on file attr struct */
1258 /* the last event terminates with evsel->attr.size == 0: */
1259 events = calloc(nre + 1, sizeof(*events));
1263 msz = sizeof(evsel->attr);
1267 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1271 * must read entire on-file attr struct to
1272 * sync up with layout.
1274 ret = readn(fd, buf, sz);
1275 if (ret != (ssize_t)sz)
1279 perf_event__attr_swap(buf);
1281 memcpy(&evsel->attr, buf, msz);
1283 ret = readn(fd, &nr, sizeof(nr));
1284 if (ret != (ssize_t)sizeof(nr))
1287 if (ph->needs_swap) {
1289 evsel->needs_swap = true;
1292 evsel->name = do_read_string(fd, ph);
1297 id = calloc(nr, sizeof(*id));
1303 for (j = 0 ; j < nr; j++) {
1304 ret = readn(fd, id, sizeof(*id));
1305 if (ret != (ssize_t)sizeof(*id))
1308 *id = bswap_64(*id);
1318 free_event_desc(events);
1323 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1325 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1330 fprintf(fp, "# event desc: not available or unable to read\n");
1334 for (evsel = events; evsel->attr.size; evsel++) {
1335 fprintf(fp, "# event : name = %s, ", evsel->name);
1337 fprintf(fp, "type = %d, config = 0x%"PRIx64
1338 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1340 (u64)evsel->attr.config,
1341 (u64)evsel->attr.config1,
1342 (u64)evsel->attr.config2);
1344 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1345 evsel->attr.exclude_user,
1346 evsel->attr.exclude_kernel);
1348 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1349 evsel->attr.exclude_host,
1350 evsel->attr.exclude_guest);
1352 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1354 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1355 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1356 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
1358 fprintf(fp, ", id = {");
1359 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1362 fprintf(fp, " %"PRIu64, *id);
1370 free_event_desc(events);
1373 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1376 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1379 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1384 uint64_t mem_total, mem_free;
1387 nr = ph->env.nr_numa_nodes;
1388 str = ph->env.numa_nodes;
1390 for (i = 0; i < nr; i++) {
1392 c = strtoul(str, &tmp, 0);
1397 mem_total = strtoull(str, &tmp, 0);
1402 mem_free = strtoull(str, &tmp, 0);
1406 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1407 " free = %"PRIu64" kB\n",
1408 c, mem_total, mem_free);
1411 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1413 str += strlen(str) + 1;
1417 fprintf(fp, "# numa topology : not available\n");
1420 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1422 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1425 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1426 int fd __maybe_unused, FILE *fp)
1428 fprintf(fp, "# contains samples with branch stack\n");
1431 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1434 const char *delimiter = "# pmu mappings: ";
1439 pmu_num = ph->env.nr_pmu_mappings;
1441 fprintf(fp, "# pmu mappings: not available\n");
1445 str = ph->env.pmu_mappings;
1448 type = strtoul(str, &tmp, 0);
1453 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1456 str += strlen(str) + 1;
1465 fprintf(fp, "# pmu mappings: unable to read\n");
1468 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1471 struct perf_session *session;
1472 struct perf_evsel *evsel;
1475 session = container_of(ph, struct perf_session, header);
1477 list_for_each_entry(evsel, &session->evlist->entries, node) {
1478 if (perf_evsel__is_group_leader(evsel) &&
1479 evsel->nr_members > 1) {
1480 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1481 perf_evsel__name(evsel));
1483 nr = evsel->nr_members - 1;
1485 fprintf(fp, ",%s", perf_evsel__name(evsel));
1493 static int __event_process_build_id(struct build_id_event *bev,
1495 struct perf_session *session)
1498 struct list_head *head;
1499 struct machine *machine;
1502 enum dso_kernel_type dso_type;
1504 machine = perf_session__findnew_machine(session, bev->pid);
1508 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1511 case PERF_RECORD_MISC_KERNEL:
1512 dso_type = DSO_TYPE_KERNEL;
1513 head = &machine->kernel_dsos;
1515 case PERF_RECORD_MISC_GUEST_KERNEL:
1516 dso_type = DSO_TYPE_GUEST_KERNEL;
1517 head = &machine->kernel_dsos;
1519 case PERF_RECORD_MISC_USER:
1520 case PERF_RECORD_MISC_GUEST_USER:
1521 dso_type = DSO_TYPE_USER;
1522 head = &machine->user_dsos;
1528 dso = __dsos__findnew(head, filename);
1530 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1532 dso__set_build_id(dso, &bev->build_id);
1534 if (filename[0] == '[')
1535 dso->kernel = dso_type;
1537 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1539 pr_debug("build id event received for %s: %s\n",
1540 dso->long_name, sbuild_id);
1548 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1549 int input, u64 offset, u64 size)
1551 struct perf_session *session = container_of(header, struct perf_session, header);
1553 struct perf_event_header header;
1554 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1557 struct build_id_event bev;
1558 char filename[PATH_MAX];
1559 u64 limit = offset + size;
1561 while (offset < limit) {
1564 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1567 if (header->needs_swap)
1568 perf_event_header__bswap(&old_bev.header);
1570 len = old_bev.header.size - sizeof(old_bev);
1571 if (readn(input, filename, len) != len)
1574 bev.header = old_bev.header;
1577 * As the pid is the missing value, we need to fill
1578 * it properly. The header.misc value give us nice hint.
1580 bev.pid = HOST_KERNEL_ID;
1581 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1582 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1583 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1585 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1586 __event_process_build_id(&bev, filename, session);
1588 offset += bev.header.size;
1594 static int perf_header__read_build_ids(struct perf_header *header,
1595 int input, u64 offset, u64 size)
1597 struct perf_session *session = container_of(header, struct perf_session, header);
1598 struct build_id_event bev;
1599 char filename[PATH_MAX];
1600 u64 limit = offset + size, orig_offset = offset;
1603 while (offset < limit) {
1606 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1609 if (header->needs_swap)
1610 perf_event_header__bswap(&bev.header);
1612 len = bev.header.size - sizeof(bev);
1613 if (readn(input, filename, len) != len)
1616 * The a1645ce1 changeset:
1618 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1620 * Added a field to struct build_id_event that broke the file
1623 * Since the kernel build-id is the first entry, process the
1624 * table using the old format if the well known
1625 * '[kernel.kallsyms]' string for the kernel build-id has the
1626 * first 4 characters chopped off (where the pid_t sits).
1628 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1629 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1631 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1634 __event_process_build_id(&bev, filename, session);
1636 offset += bev.header.size;
1643 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1644 struct perf_header *ph __maybe_unused,
1647 ssize_t ret = trace_report(fd, data, false);
1648 return ret < 0 ? -1 : 0;
1651 static int process_build_id(struct perf_file_section *section,
1652 struct perf_header *ph, int fd,
1653 void *data __maybe_unused)
1655 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1656 pr_debug("Failed to read buildids, continuing...\n");
1660 static int process_hostname(struct perf_file_section *section __maybe_unused,
1661 struct perf_header *ph, int fd,
1662 void *data __maybe_unused)
1664 ph->env.hostname = do_read_string(fd, ph);
1665 return ph->env.hostname ? 0 : -ENOMEM;
1668 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1669 struct perf_header *ph, int fd,
1670 void *data __maybe_unused)
1672 ph->env.os_release = do_read_string(fd, ph);
1673 return ph->env.os_release ? 0 : -ENOMEM;
1676 static int process_version(struct perf_file_section *section __maybe_unused,
1677 struct perf_header *ph, int fd,
1678 void *data __maybe_unused)
1680 ph->env.version = do_read_string(fd, ph);
1681 return ph->env.version ? 0 : -ENOMEM;
1684 static int process_arch(struct perf_file_section *section __maybe_unused,
1685 struct perf_header *ph, int fd,
1686 void *data __maybe_unused)
1688 ph->env.arch = do_read_string(fd, ph);
1689 return ph->env.arch ? 0 : -ENOMEM;
1692 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1693 struct perf_header *ph, int fd,
1694 void *data __maybe_unused)
1699 ret = readn(fd, &nr, sizeof(nr));
1700 if (ret != sizeof(nr))
1706 ph->env.nr_cpus_online = nr;
1708 ret = readn(fd, &nr, sizeof(nr));
1709 if (ret != sizeof(nr))
1715 ph->env.nr_cpus_avail = nr;
1719 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1720 struct perf_header *ph, int fd,
1721 void *data __maybe_unused)
1723 ph->env.cpu_desc = do_read_string(fd, ph);
1724 return ph->env.cpu_desc ? 0 : -ENOMEM;
1727 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1728 struct perf_header *ph, int fd,
1729 void *data __maybe_unused)
1731 ph->env.cpuid = do_read_string(fd, ph);
1732 return ph->env.cpuid ? 0 : -ENOMEM;
1735 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1736 struct perf_header *ph, int fd,
1737 void *data __maybe_unused)
1742 ret = readn(fd, &mem, sizeof(mem));
1743 if (ret != sizeof(mem))
1747 mem = bswap_64(mem);
1749 ph->env.total_mem = mem;
1753 static struct perf_evsel *
1754 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1756 struct perf_evsel *evsel;
1758 list_for_each_entry(evsel, &evlist->entries, node) {
1759 if (evsel->idx == idx)
1767 perf_evlist__set_event_name(struct perf_evlist *evlist,
1768 struct perf_evsel *event)
1770 struct perf_evsel *evsel;
1775 evsel = perf_evlist__find_by_index(evlist, event->idx);
1782 evsel->name = strdup(event->name);
1786 process_event_desc(struct perf_file_section *section __maybe_unused,
1787 struct perf_header *header, int fd,
1788 void *data __maybe_unused)
1790 struct perf_session *session;
1791 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1796 session = container_of(header, struct perf_session, header);
1797 for (evsel = events; evsel->attr.size; evsel++)
1798 perf_evlist__set_event_name(session->evlist, evsel);
1800 free_event_desc(events);
1805 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1806 struct perf_header *ph, int fd,
1807 void *data __maybe_unused)
1814 ret = readn(fd, &nr, sizeof(nr));
1815 if (ret != sizeof(nr))
1821 ph->env.nr_cmdline = nr;
1822 strbuf_init(&sb, 128);
1824 for (i = 0; i < nr; i++) {
1825 str = do_read_string(fd, ph);
1829 /* include a NULL character at the end */
1830 strbuf_add(&sb, str, strlen(str) + 1);
1833 ph->env.cmdline = strbuf_detach(&sb, NULL);
1837 strbuf_release(&sb);
1841 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1842 struct perf_header *ph, int fd,
1843 void *data __maybe_unused)
1850 ret = readn(fd, &nr, sizeof(nr));
1851 if (ret != sizeof(nr))
1857 ph->env.nr_sibling_cores = nr;
1858 strbuf_init(&sb, 128);
1860 for (i = 0; i < nr; i++) {
1861 str = do_read_string(fd, ph);
1865 /* include a NULL character at the end */
1866 strbuf_add(&sb, str, strlen(str) + 1);
1869 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1871 ret = readn(fd, &nr, sizeof(nr));
1872 if (ret != sizeof(nr))
1878 ph->env.nr_sibling_threads = nr;
1880 for (i = 0; i < nr; i++) {
1881 str = do_read_string(fd, ph);
1885 /* include a NULL character at the end */
1886 strbuf_add(&sb, str, strlen(str) + 1);
1889 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1893 strbuf_release(&sb);
1897 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1898 struct perf_header *ph, int fd,
1899 void *data __maybe_unused)
1904 uint64_t mem_total, mem_free;
1908 ret = readn(fd, &nr, sizeof(nr));
1909 if (ret != sizeof(nr))
1915 ph->env.nr_numa_nodes = nr;
1916 strbuf_init(&sb, 256);
1918 for (i = 0; i < nr; i++) {
1920 ret = readn(fd, &node, sizeof(node));
1921 if (ret != sizeof(node))
1924 ret = readn(fd, &mem_total, sizeof(u64));
1925 if (ret != sizeof(u64))
1928 ret = readn(fd, &mem_free, sizeof(u64));
1929 if (ret != sizeof(u64))
1932 if (ph->needs_swap) {
1933 node = bswap_32(node);
1934 mem_total = bswap_64(mem_total);
1935 mem_free = bswap_64(mem_free);
1938 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1939 node, mem_total, mem_free);
1941 str = do_read_string(fd, ph);
1945 /* include a NULL character at the end */
1946 strbuf_add(&sb, str, strlen(str) + 1);
1949 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1953 strbuf_release(&sb);
1957 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1958 struct perf_header *ph, int fd,
1959 void *data __maybe_unused)
1967 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1968 if (ret != sizeof(pmu_num))
1972 pmu_num = bswap_32(pmu_num);
1975 pr_debug("pmu mappings not available\n");
1979 ph->env.nr_pmu_mappings = pmu_num;
1980 strbuf_init(&sb, 128);
1983 if (readn(fd, &type, sizeof(type)) != sizeof(type))
1986 type = bswap_32(type);
1988 name = do_read_string(fd, ph);
1992 strbuf_addf(&sb, "%u:%s", type, name);
1993 /* include a NULL character at the end */
1994 strbuf_add(&sb, "", 1);
1999 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2003 strbuf_release(&sb);
2007 static int process_group_desc(struct perf_file_section *section __maybe_unused,
2008 struct perf_header *ph, int fd,
2009 void *data __maybe_unused)
2012 u32 i, nr, nr_groups;
2013 struct perf_session *session;
2014 struct perf_evsel *evsel, *leader = NULL;
2021 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2025 nr_groups = bswap_32(nr_groups);
2027 ph->env.nr_groups = nr_groups;
2029 pr_debug("group desc not available\n");
2033 desc = calloc(nr_groups, sizeof(*desc));
2037 for (i = 0; i < nr_groups; i++) {
2038 desc[i].name = do_read_string(fd, ph);
2042 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2045 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2048 if (ph->needs_swap) {
2049 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2050 desc[i].nr_members = bswap_32(desc[i].nr_members);
2055 * Rebuild group relationship based on the group_desc
2057 session = container_of(ph, struct perf_session, header);
2058 session->evlist->nr_groups = nr_groups;
2061 list_for_each_entry(evsel, &session->evlist->entries, node) {
2062 if (evsel->idx == (int) desc[i].leader_idx) {
2063 evsel->leader = evsel;
2064 /* {anon_group} is a dummy name */
2065 if (strcmp(desc[i].name, "{anon_group}"))
2066 evsel->group_name = desc[i].name;
2067 evsel->nr_members = desc[i].nr_members;
2069 if (i >= nr_groups || nr > 0) {
2070 pr_debug("invalid group desc\n");
2075 nr = evsel->nr_members - 1;
2078 /* This is a group member */
2079 evsel->leader = leader;
2085 if (i != nr_groups || nr != 0) {
2086 pr_debug("invalid group desc\n");
2092 while ((int) --i >= 0)
2099 struct feature_ops {
2100 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2101 void (*print)(struct perf_header *h, int fd, FILE *fp);
2102 int (*process)(struct perf_file_section *section,
2103 struct perf_header *h, int fd, void *data);
2108 #define FEAT_OPA(n, func) \
2109 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2110 #define FEAT_OPP(n, func) \
2111 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2112 .process = process_##func }
2113 #define FEAT_OPF(n, func) \
2114 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2115 .process = process_##func, .full_only = true }
2117 /* feature_ops not implemented: */
2118 #define print_tracing_data NULL
2119 #define print_build_id NULL
2121 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2122 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
2123 FEAT_OPP(HEADER_BUILD_ID, build_id),
2124 FEAT_OPP(HEADER_HOSTNAME, hostname),
2125 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2126 FEAT_OPP(HEADER_VERSION, version),
2127 FEAT_OPP(HEADER_ARCH, arch),
2128 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2129 FEAT_OPP(HEADER_CPUDESC, cpudesc),
2130 FEAT_OPP(HEADER_CPUID, cpuid),
2131 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
2132 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
2133 FEAT_OPP(HEADER_CMDLINE, cmdline),
2134 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2135 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
2136 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
2137 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2138 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
2141 struct header_print_data {
2143 bool full; /* extended list of headers */
2146 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2147 struct perf_header *ph,
2148 int feat, int fd, void *data)
2150 struct header_print_data *hd = data;
2152 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2153 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2154 "%d, continuing...\n", section->offset, feat);
2157 if (feat >= HEADER_LAST_FEATURE) {
2158 pr_warning("unknown feature %d\n", feat);
2161 if (!feat_ops[feat].print)
2164 if (!feat_ops[feat].full_only || hd->full)
2165 feat_ops[feat].print(ph, fd, hd->fp);
2167 fprintf(hd->fp, "# %s info available, use -I to display\n",
2168 feat_ops[feat].name);
2173 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2175 struct header_print_data hd;
2176 struct perf_header *header = &session->header;
2177 int fd = session->fd;
2181 perf_header__process_sections(header, fd, &hd,
2182 perf_file_section__fprintf_info);
2186 static int do_write_feat(int fd, struct perf_header *h, int type,
2187 struct perf_file_section **p,
2188 struct perf_evlist *evlist)
2193 if (perf_header__has_feat(h, type)) {
2194 if (!feat_ops[type].write)
2197 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2199 err = feat_ops[type].write(fd, h, evlist);
2201 pr_debug("failed to write feature %d\n", type);
2203 /* undo anything written */
2204 lseek(fd, (*p)->offset, SEEK_SET);
2208 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2214 static int perf_header__adds_write(struct perf_header *header,
2215 struct perf_evlist *evlist, int fd)
2218 struct perf_file_section *feat_sec, *p;
2224 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2228 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2229 if (feat_sec == NULL)
2232 sec_size = sizeof(*feat_sec) * nr_sections;
2234 sec_start = header->feat_offset;
2235 lseek(fd, sec_start + sec_size, SEEK_SET);
2237 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2238 if (do_write_feat(fd, header, feat, &p, evlist))
2239 perf_header__clear_feat(header, feat);
2242 lseek(fd, sec_start, SEEK_SET);
2244 * may write more than needed due to dropped feature, but
2245 * this is okay, reader will skip the mising entries
2247 err = do_write(fd, feat_sec, sec_size);
2249 pr_debug("failed to write feature section\n");
2254 int perf_header__write_pipe(int fd)
2256 struct perf_pipe_file_header f_header;
2259 f_header = (struct perf_pipe_file_header){
2260 .magic = PERF_MAGIC,
2261 .size = sizeof(f_header),
2264 err = do_write(fd, &f_header, sizeof(f_header));
2266 pr_debug("failed to write perf pipe header\n");
2273 int perf_session__write_header(struct perf_session *session,
2274 struct perf_evlist *evlist,
2275 int fd, bool at_exit)
2277 struct perf_file_header f_header;
2278 struct perf_file_attr f_attr;
2279 struct perf_header *header = &session->header;
2280 struct perf_evsel *evsel;
2284 lseek(fd, sizeof(f_header), SEEK_SET);
2286 list_for_each_entry(evsel, &evlist->entries, node) {
2287 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2288 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2290 pr_debug("failed to write perf header\n");
2295 attr_offset = lseek(fd, 0, SEEK_CUR);
2297 list_for_each_entry(evsel, &evlist->entries, node) {
2298 f_attr = (struct perf_file_attr){
2299 .attr = evsel->attr,
2301 .offset = evsel->id_offset,
2302 .size = evsel->ids * sizeof(u64),
2305 err = do_write(fd, &f_attr, sizeof(f_attr));
2307 pr_debug("failed to write perf header attribute\n");
2312 header->data_offset = lseek(fd, 0, SEEK_CUR);
2313 header->feat_offset = header->data_offset + header->data_size;
2316 err = perf_header__adds_write(header, evlist, fd);
2321 f_header = (struct perf_file_header){
2322 .magic = PERF_MAGIC,
2323 .size = sizeof(f_header),
2324 .attr_size = sizeof(f_attr),
2326 .offset = attr_offset,
2327 .size = evlist->nr_entries * sizeof(f_attr),
2330 .offset = header->data_offset,
2331 .size = header->data_size,
2333 /* event_types is ignored, store zeros */
2336 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2338 lseek(fd, 0, SEEK_SET);
2339 err = do_write(fd, &f_header, sizeof(f_header));
2341 pr_debug("failed to write perf header\n");
2344 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2349 static int perf_header__getbuffer64(struct perf_header *header,
2350 int fd, void *buf, size_t size)
2352 if (readn(fd, buf, size) <= 0)
2355 if (header->needs_swap)
2356 mem_bswap_64(buf, size);
2361 int perf_header__process_sections(struct perf_header *header, int fd,
2363 int (*process)(struct perf_file_section *section,
2364 struct perf_header *ph,
2365 int feat, int fd, void *data))
2367 struct perf_file_section *feat_sec, *sec;
2373 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2377 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2381 sec_size = sizeof(*feat_sec) * nr_sections;
2383 lseek(fd, header->feat_offset, SEEK_SET);
2385 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2389 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2390 err = process(sec++, header, feat, fd, data);
2400 static const int attr_file_abi_sizes[] = {
2401 [0] = PERF_ATTR_SIZE_VER0,
2402 [1] = PERF_ATTR_SIZE_VER1,
2403 [2] = PERF_ATTR_SIZE_VER2,
2404 [3] = PERF_ATTR_SIZE_VER3,
2409 * In the legacy file format, the magic number is not used to encode endianness.
2410 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2411 * on ABI revisions, we need to try all combinations for all endianness to
2412 * detect the endianness.
2414 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2416 uint64_t ref_size, attr_size;
2419 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2420 ref_size = attr_file_abi_sizes[i]
2421 + sizeof(struct perf_file_section);
2422 if (hdr_sz != ref_size) {
2423 attr_size = bswap_64(hdr_sz);
2424 if (attr_size != ref_size)
2427 ph->needs_swap = true;
2429 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2434 /* could not determine endianness */
2438 #define PERF_PIPE_HDR_VER0 16
2440 static const size_t attr_pipe_abi_sizes[] = {
2441 [0] = PERF_PIPE_HDR_VER0,
2446 * In the legacy pipe format, there is an implicit assumption that endiannesss
2447 * between host recording the samples, and host parsing the samples is the
2448 * same. This is not always the case given that the pipe output may always be
2449 * redirected into a file and analyzed on a different machine with possibly a
2450 * different endianness and perf_event ABI revsions in the perf tool itself.
2452 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2457 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2458 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2459 attr_size = bswap_64(hdr_sz);
2460 if (attr_size != hdr_sz)
2463 ph->needs_swap = true;
2465 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2471 bool is_perf_magic(u64 magic)
2473 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2474 || magic == __perf_magic2
2475 || magic == __perf_magic2_sw)
2481 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2482 bool is_pipe, struct perf_header *ph)
2486 /* check for legacy format */
2487 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2489 ph->version = PERF_HEADER_VERSION_1;
2490 pr_debug("legacy perf.data format\n");
2492 return try_all_pipe_abis(hdr_sz, ph);
2494 return try_all_file_abis(hdr_sz, ph);
2497 * the new magic number serves two purposes:
2498 * - unique number to identify actual perf.data files
2499 * - encode endianness of file
2502 /* check magic number with one endianness */
2503 if (magic == __perf_magic2)
2506 /* check magic number with opposite endianness */
2507 if (magic != __perf_magic2_sw)
2510 ph->needs_swap = true;
2511 ph->version = PERF_HEADER_VERSION_2;
2516 int perf_file_header__read(struct perf_file_header *header,
2517 struct perf_header *ph, int fd)
2521 lseek(fd, 0, SEEK_SET);
2523 ret = readn(fd, header, sizeof(*header));
2527 if (check_magic_endian(header->magic,
2528 header->attr_size, false, ph) < 0) {
2529 pr_debug("magic/endian check failed\n");
2533 if (ph->needs_swap) {
2534 mem_bswap_64(header, offsetof(struct perf_file_header,
2538 if (header->size != sizeof(*header)) {
2539 /* Support the previous format */
2540 if (header->size == offsetof(typeof(*header), adds_features))
2541 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2544 } else if (ph->needs_swap) {
2546 * feature bitmap is declared as an array of unsigned longs --
2547 * not good since its size can differ between the host that
2548 * generated the data file and the host analyzing the file.
2550 * We need to handle endianness, but we don't know the size of
2551 * the unsigned long where the file was generated. Take a best
2552 * guess at determining it: try 64-bit swap first (ie., file
2553 * created on a 64-bit host), and check if the hostname feature
2554 * bit is set (this feature bit is forced on as of fbe96f2).
2555 * If the bit is not, undo the 64-bit swap and try a 32-bit
2556 * swap. If the hostname bit is still not set (e.g., older data
2557 * file), punt and fallback to the original behavior --
2558 * clearing all feature bits and setting buildid.
2560 mem_bswap_64(&header->adds_features,
2561 BITS_TO_U64(HEADER_FEAT_BITS));
2563 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2565 mem_bswap_64(&header->adds_features,
2566 BITS_TO_U64(HEADER_FEAT_BITS));
2569 mem_bswap_32(&header->adds_features,
2570 BITS_TO_U32(HEADER_FEAT_BITS));
2573 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2574 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2575 set_bit(HEADER_BUILD_ID, header->adds_features);
2579 memcpy(&ph->adds_features, &header->adds_features,
2580 sizeof(ph->adds_features));
2582 ph->data_offset = header->data.offset;
2583 ph->data_size = header->data.size;
2584 ph->feat_offset = header->data.offset + header->data.size;
2588 static int perf_file_section__process(struct perf_file_section *section,
2589 struct perf_header *ph,
2590 int feat, int fd, void *data)
2592 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2593 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2594 "%d, continuing...\n", section->offset, feat);
2598 if (feat >= HEADER_LAST_FEATURE) {
2599 pr_debug("unknown feature %d, continuing...\n", feat);
2603 if (!feat_ops[feat].process)
2606 return feat_ops[feat].process(section, ph, fd, data);
2609 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2610 struct perf_header *ph, int fd,
2615 ret = readn(fd, header, sizeof(*header));
2619 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2620 pr_debug("endian/magic failed\n");
2625 header->size = bswap_64(header->size);
2627 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2633 static int perf_header__read_pipe(struct perf_session *session)
2635 struct perf_header *header = &session->header;
2636 struct perf_pipe_file_header f_header;
2638 if (perf_file_header__read_pipe(&f_header, header, session->fd,
2639 session->repipe) < 0) {
2640 pr_debug("incompatible file format\n");
2647 static int read_attr(int fd, struct perf_header *ph,
2648 struct perf_file_attr *f_attr)
2650 struct perf_event_attr *attr = &f_attr->attr;
2652 size_t our_sz = sizeof(f_attr->attr);
2655 memset(f_attr, 0, sizeof(*f_attr));
2657 /* read minimal guaranteed structure */
2658 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2660 pr_debug("cannot read %d bytes of header attr\n",
2661 PERF_ATTR_SIZE_VER0);
2665 /* on file perf_event_attr size */
2673 sz = PERF_ATTR_SIZE_VER0;
2674 } else if (sz > our_sz) {
2675 pr_debug("file uses a more recent and unsupported ABI"
2676 " (%zu bytes extra)\n", sz - our_sz);
2679 /* what we have not yet read and that we know about */
2680 left = sz - PERF_ATTR_SIZE_VER0;
2683 ptr += PERF_ATTR_SIZE_VER0;
2685 ret = readn(fd, ptr, left);
2687 /* read perf_file_section, ids are read in caller */
2688 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2690 return ret <= 0 ? -1 : 0;
2693 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2694 struct pevent *pevent)
2696 struct event_format *event;
2699 /* already prepared */
2700 if (evsel->tp_format)
2703 if (pevent == NULL) {
2704 pr_debug("broken or missing trace data\n");
2708 event = pevent_find_event(pevent, evsel->attr.config);
2713 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2714 evsel->name = strdup(bf);
2715 if (evsel->name == NULL)
2719 evsel->tp_format = event;
2723 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2724 struct pevent *pevent)
2726 struct perf_evsel *pos;
2728 list_for_each_entry(pos, &evlist->entries, node) {
2729 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2730 perf_evsel__prepare_tracepoint_event(pos, pevent))
2737 int perf_session__read_header(struct perf_session *session)
2739 struct perf_header *header = &session->header;
2740 struct perf_file_header f_header;
2741 struct perf_file_attr f_attr;
2743 int nr_attrs, nr_ids, i, j;
2744 int fd = session->fd;
2746 session->evlist = perf_evlist__new();
2747 if (session->evlist == NULL)
2750 if (session->fd_pipe)
2751 return perf_header__read_pipe(session);
2753 if (perf_file_header__read(&f_header, header, fd) < 0)
2756 nr_attrs = f_header.attrs.size / f_header.attr_size;
2757 lseek(fd, f_header.attrs.offset, SEEK_SET);
2759 for (i = 0; i < nr_attrs; i++) {
2760 struct perf_evsel *evsel;
2763 if (read_attr(fd, header, &f_attr) < 0)
2766 if (header->needs_swap)
2767 perf_event__attr_swap(&f_attr.attr);
2769 tmp = lseek(fd, 0, SEEK_CUR);
2770 evsel = perf_evsel__new(&f_attr.attr, i);
2773 goto out_delete_evlist;
2775 evsel->needs_swap = header->needs_swap;
2777 * Do it before so that if perf_evsel__alloc_id fails, this
2778 * entry gets purged too at perf_evlist__delete().
2780 perf_evlist__add(session->evlist, evsel);
2782 nr_ids = f_attr.ids.size / sizeof(u64);
2784 * We don't have the cpu and thread maps on the header, so
2785 * for allocating the perf_sample_id table we fake 1 cpu and
2786 * hattr->ids threads.
2788 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2789 goto out_delete_evlist;
2791 lseek(fd, f_attr.ids.offset, SEEK_SET);
2793 for (j = 0; j < nr_ids; j++) {
2794 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2797 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2800 lseek(fd, tmp, SEEK_SET);
2803 symbol_conf.nr_events = nr_attrs;
2805 perf_header__process_sections(header, fd, &session->pevent,
2806 perf_file_section__process);
2808 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2810 goto out_delete_evlist;
2817 perf_evlist__delete(session->evlist);
2818 session->evlist = NULL;
2822 int perf_event__synthesize_attr(struct perf_tool *tool,
2823 struct perf_event_attr *attr, u32 ids, u64 *id,
2824 perf_event__handler_t process)
2826 union perf_event *ev;
2830 size = sizeof(struct perf_event_attr);
2831 size = PERF_ALIGN(size, sizeof(u64));
2832 size += sizeof(struct perf_event_header);
2833 size += ids * sizeof(u64);
2840 ev->attr.attr = *attr;
2841 memcpy(ev->attr.id, id, ids * sizeof(u64));
2843 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2844 ev->attr.header.size = (u16)size;
2846 if (ev->attr.header.size == size)
2847 err = process(tool, ev, NULL, NULL);
2856 int perf_event__synthesize_attrs(struct perf_tool *tool,
2857 struct perf_session *session,
2858 perf_event__handler_t process)
2860 struct perf_evsel *evsel;
2863 list_for_each_entry(evsel, &session->evlist->entries, node) {
2864 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2865 evsel->id, process);
2867 pr_debug("failed to create perf header attribute\n");
2875 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2876 union perf_event *event,
2877 struct perf_evlist **pevlist)
2880 struct perf_evsel *evsel;
2881 struct perf_evlist *evlist = *pevlist;
2883 if (evlist == NULL) {
2884 *pevlist = evlist = perf_evlist__new();
2889 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2893 perf_evlist__add(evlist, evsel);
2895 ids = event->header.size;
2896 ids -= (void *)&event->attr.id - (void *)event;
2897 n_ids = ids / sizeof(u64);
2899 * We don't have the cpu and thread maps on the header, so
2900 * for allocating the perf_sample_id table we fake 1 cpu and
2901 * hattr->ids threads.
2903 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2906 for (i = 0; i < n_ids; i++) {
2907 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2910 symbol_conf.nr_events = evlist->nr_entries;
2915 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2916 struct perf_evlist *evlist,
2917 perf_event__handler_t process)
2919 union perf_event ev;
2920 struct tracing_data *tdata;
2921 ssize_t size = 0, aligned_size = 0, padding;
2922 int err __maybe_unused = 0;
2925 * We are going to store the size of the data followed
2926 * by the data contents. Since the fd descriptor is a pipe,
2927 * we cannot seek back to store the size of the data once
2928 * we know it. Instead we:
2930 * - write the tracing data to the temp file
2931 * - get/write the data size to pipe
2932 * - write the tracing data from the temp file
2935 tdata = tracing_data_get(&evlist->entries, fd, true);
2939 memset(&ev, 0, sizeof(ev));
2941 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2943 aligned_size = PERF_ALIGN(size, sizeof(u64));
2944 padding = aligned_size - size;
2945 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2946 ev.tracing_data.size = aligned_size;
2948 process(tool, &ev, NULL, NULL);
2951 * The put function will copy all the tracing data
2952 * stored in temp file to the pipe.
2954 tracing_data_put(tdata);
2956 write_padded(fd, NULL, 0, padding);
2958 return aligned_size;
2961 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2962 union perf_event *event,
2963 struct perf_session *session)
2965 ssize_t size_read, padding, size = event->tracing_data.size;
2966 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2969 /* setup for reading amidst mmap */
2970 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2973 size_read = trace_report(session->fd, &session->pevent,
2975 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2977 if (readn(session->fd, buf, padding) < 0) {
2978 pr_err("%s: reading input file", __func__);
2981 if (session->repipe) {
2982 int retw = write(STDOUT_FILENO, buf, padding);
2983 if (retw <= 0 || retw != padding) {
2984 pr_err("%s: repiping tracing data padding", __func__);
2989 if (size_read + padding != size) {
2990 pr_err("%s: tracing data size mismatch", __func__);
2994 perf_evlist__prepare_tracepoint_events(session->evlist,
2997 return size_read + padding;
3000 int perf_event__synthesize_build_id(struct perf_tool *tool,
3001 struct dso *pos, u16 misc,
3002 perf_event__handler_t process,
3003 struct machine *machine)
3005 union perf_event ev;
3012 memset(&ev, 0, sizeof(ev));
3014 len = pos->long_name_len + 1;
3015 len = PERF_ALIGN(len, NAME_ALIGN);
3016 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3017 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3018 ev.build_id.header.misc = misc;
3019 ev.build_id.pid = machine->pid;
3020 ev.build_id.header.size = sizeof(ev.build_id) + len;
3021 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3023 err = process(tool, &ev, NULL, machine);
3028 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3029 union perf_event *event,
3030 struct perf_session *session)
3032 __event_process_build_id(&event->build_id,
3033 event->build_id.filename,
3038 void disable_buildid_cache(void)
3040 no_buildid_cache = true;