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__write_buildid_table(struct list_head *head,
204 struct machine *machine,
205 pid_t pid, u16 misc, int fd)
210 dsos__for_each_with_build_id(pos, head) {
218 if (is_vdso_map(pos->short_name)) {
219 name = (char *) VDSO__MAP_NAME;
220 name_len = sizeof(VDSO__MAP_NAME) + 1;
221 } else if (dso__is_kcore(pos)) {
222 machine__mmap_name(machine, nm, sizeof(nm));
224 name_len = strlen(nm) + 1;
226 name = pos->long_name;
227 name_len = pos->long_name_len + 1;
230 err = write_buildid(name, name_len, pos->build_id,
239 static int machine__write_buildid_table(struct machine *machine, int fd)
242 u16 kmisc = PERF_RECORD_MISC_KERNEL,
243 umisc = PERF_RECORD_MISC_USER;
245 if (!machine__is_host(machine)) {
246 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
247 umisc = PERF_RECORD_MISC_GUEST_USER;
250 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine,
251 machine->pid, kmisc, fd);
253 err = __dsos__write_buildid_table(&machine->user_dsos, machine,
254 machine->pid, umisc, fd);
258 static int dsos__write_buildid_table(struct perf_header *header, int fd)
260 struct perf_session *session = container_of(header,
261 struct perf_session, header);
263 int err = machine__write_buildid_table(&session->machines.host, fd);
268 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
269 struct machine *pos = rb_entry(nd, struct machine, rb_node);
270 err = machine__write_buildid_table(pos, fd);
277 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
278 const char *name, bool is_kallsyms, bool is_vdso)
280 const size_t size = PATH_MAX;
281 char *realname, *filename = zalloc(size),
282 *linkname = zalloc(size), *targetname;
284 bool slash = is_kallsyms || is_vdso;
287 if (symbol_conf.kptr_restrict) {
288 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
292 realname = (char *) name;
294 realname = realpath(name, NULL);
296 if (realname == NULL || filename == NULL || linkname == NULL)
299 len = scnprintf(filename, size, "%s%s%s",
300 debugdir, slash ? "/" : "",
301 is_vdso ? VDSO__MAP_NAME : realname);
302 if (mkdir_p(filename, 0755))
305 snprintf(filename + len, size - len, "/%s", sbuild_id);
307 if (access(filename, F_OK)) {
309 if (copyfile("/proc/kallsyms", filename))
311 } else if (link(realname, filename) && copyfile(name, filename))
315 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
316 debugdir, sbuild_id);
318 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
321 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
322 targetname = filename + strlen(debugdir) - 5;
323 memcpy(targetname, "../..", 5);
325 if (symlink(targetname, linkname) == 0)
335 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
336 const char *name, const char *debugdir,
337 bool is_kallsyms, bool is_vdso)
339 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
341 build_id__sprintf(build_id, build_id_size, sbuild_id);
343 return build_id_cache__add_s(sbuild_id, debugdir, name,
344 is_kallsyms, is_vdso);
347 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
349 const size_t size = PATH_MAX;
350 char *filename = zalloc(size),
351 *linkname = zalloc(size);
354 if (filename == NULL || linkname == NULL)
357 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
358 debugdir, sbuild_id, sbuild_id + 2);
360 if (access(linkname, F_OK))
363 if (readlink(linkname, filename, size - 1) < 0)
366 if (unlink(linkname))
370 * Since the link is relative, we must make it absolute:
372 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
373 debugdir, sbuild_id, filename);
375 if (unlink(linkname))
385 static int dso__cache_build_id(struct dso *dso, struct machine *machine,
386 const char *debugdir)
388 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
389 bool is_vdso = is_vdso_map(dso->short_name);
390 const char *name = dso->long_name;
393 if (dso__is_kcore(dso)) {
395 machine__mmap_name(machine, nm, sizeof(nm));
398 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
399 debugdir, is_kallsyms, is_vdso);
402 static int __dsos__cache_build_ids(struct list_head *head,
403 struct machine *machine, const char *debugdir)
408 dsos__for_each_with_build_id(pos, head)
409 if (dso__cache_build_id(pos, machine, debugdir))
415 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
417 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, machine,
419 ret |= __dsos__cache_build_ids(&machine->user_dsos, machine, debugdir);
423 static int perf_session__cache_build_ids(struct perf_session *session)
427 char debugdir[PATH_MAX];
429 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
431 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
434 ret = machine__cache_build_ids(&session->machines.host, debugdir);
436 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
437 struct machine *pos = rb_entry(nd, struct machine, rb_node);
438 ret |= machine__cache_build_ids(pos, debugdir);
443 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
445 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
446 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
450 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
453 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
455 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
456 struct machine *pos = rb_entry(nd, struct machine, rb_node);
457 ret |= machine__read_build_ids(pos, with_hits);
463 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
464 struct perf_evlist *evlist)
466 return read_tracing_data(fd, &evlist->entries);
470 static int write_build_id(int fd, struct perf_header *h,
471 struct perf_evlist *evlist __maybe_unused)
473 struct perf_session *session;
476 session = container_of(h, struct perf_session, header);
478 if (!perf_session__read_build_ids(session, true))
481 err = dsos__write_buildid_table(h, fd);
483 pr_debug("failed to write buildid table\n");
486 if (!no_buildid_cache)
487 perf_session__cache_build_ids(session);
492 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
493 struct perf_evlist *evlist __maybe_unused)
502 return do_write_string(fd, uts.nodename);
505 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
506 struct perf_evlist *evlist __maybe_unused)
515 return do_write_string(fd, uts.release);
518 static int write_arch(int fd, struct perf_header *h __maybe_unused,
519 struct perf_evlist *evlist __maybe_unused)
528 return do_write_string(fd, uts.machine);
531 static int write_version(int fd, struct perf_header *h __maybe_unused,
532 struct perf_evlist *evlist __maybe_unused)
534 return do_write_string(fd, perf_version_string);
537 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
538 struct perf_evlist *evlist __maybe_unused)
541 #define CPUINFO_PROC NULL
546 const char *search = CPUINFO_PROC;
553 file = fopen("/proc/cpuinfo", "r");
557 while (getline(&buf, &len, file) > 0) {
558 ret = strncmp(buf, search, strlen(search));
568 p = strchr(buf, ':');
569 if (p && *(p+1) == ' ' && *(p+2))
575 /* squash extra space characters (branding string) */
582 while (*q && isspace(*q))
585 while ((*r++ = *q++));
589 ret = do_write_string(fd, s);
596 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
597 struct perf_evlist *evlist __maybe_unused)
603 nr = sysconf(_SC_NPROCESSORS_CONF);
607 nrc = (u32)(nr & UINT_MAX);
609 nr = sysconf(_SC_NPROCESSORS_ONLN);
613 nra = (u32)(nr & UINT_MAX);
615 ret = do_write(fd, &nrc, sizeof(nrc));
619 return do_write(fd, &nra, sizeof(nra));
622 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
623 struct perf_evlist *evlist)
625 struct perf_evsel *evsel;
629 nre = evlist->nr_entries;
632 * write number of events
634 ret = do_write(fd, &nre, sizeof(nre));
639 * size of perf_event_attr struct
641 sz = (u32)sizeof(evsel->attr);
642 ret = do_write(fd, &sz, sizeof(sz));
646 evlist__for_each(evlist, evsel) {
647 ret = do_write(fd, &evsel->attr, sz);
651 * write number of unique id per event
652 * there is one id per instance of an event
654 * copy into an nri to be independent of the
658 ret = do_write(fd, &nri, sizeof(nri));
663 * write event string as passed on cmdline
665 ret = do_write_string(fd, perf_evsel__name(evsel));
669 * write unique ids for this event
671 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
678 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
679 struct perf_evlist *evlist __maybe_unused)
681 char buf[MAXPATHLEN];
687 * actual atual path to perf binary
689 sprintf(proc, "/proc/%d/exe", getpid());
690 ret = readlink(proc, buf, sizeof(buf));
694 /* readlink() does not add null termination */
697 /* account for binary path */
700 ret = do_write(fd, &n, sizeof(n));
704 ret = do_write_string(fd, buf);
708 for (i = 0 ; i < header_argc; i++) {
709 ret = do_write_string(fd, header_argv[i]);
716 #define CORE_SIB_FMT \
717 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
718 #define THRD_SIB_FMT \
719 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
724 char **core_siblings;
725 char **thread_siblings;
728 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
731 char filename[MAXPATHLEN];
732 char *buf = NULL, *p;
738 sprintf(filename, CORE_SIB_FMT, cpu);
739 fp = fopen(filename, "r");
743 sret = getline(&buf, &len, fp);
748 p = strchr(buf, '\n');
752 for (i = 0; i < tp->core_sib; i++) {
753 if (!strcmp(buf, tp->core_siblings[i]))
756 if (i == tp->core_sib) {
757 tp->core_siblings[i] = buf;
765 sprintf(filename, THRD_SIB_FMT, cpu);
766 fp = fopen(filename, "r");
770 if (getline(&buf, &len, fp) <= 0)
773 p = strchr(buf, '\n');
777 for (i = 0; i < tp->thread_sib; i++) {
778 if (!strcmp(buf, tp->thread_siblings[i]))
781 if (i == tp->thread_sib) {
782 tp->thread_siblings[i] = buf;
794 static void free_cpu_topo(struct cpu_topo *tp)
801 for (i = 0 ; i < tp->core_sib; i++)
802 zfree(&tp->core_siblings[i]);
804 for (i = 0 ; i < tp->thread_sib; i++)
805 zfree(&tp->thread_siblings[i]);
810 static struct cpu_topo *build_cpu_topology(void)
819 ncpus = sysconf(_SC_NPROCESSORS_CONF);
823 nr = (u32)(ncpus & UINT_MAX);
825 sz = nr * sizeof(char *);
827 addr = calloc(1, sizeof(*tp) + 2 * sz);
834 tp->core_siblings = addr;
836 tp->thread_siblings = addr;
838 for (i = 0; i < nr; i++) {
839 ret = build_cpu_topo(tp, i);
850 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
851 struct perf_evlist *evlist __maybe_unused)
857 tp = build_cpu_topology();
861 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
865 for (i = 0; i < tp->core_sib; i++) {
866 ret = do_write_string(fd, tp->core_siblings[i]);
870 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
874 for (i = 0; i < tp->thread_sib; i++) {
875 ret = do_write_string(fd, tp->thread_siblings[i]);
886 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
887 struct perf_evlist *evlist __maybe_unused)
895 fp = fopen("/proc/meminfo", "r");
899 while (getline(&buf, &len, fp) > 0) {
900 ret = strncmp(buf, "MemTotal:", 9);
905 n = sscanf(buf, "%*s %"PRIu64, &mem);
907 ret = do_write(fd, &mem, sizeof(mem));
914 static int write_topo_node(int fd, int node)
916 char str[MAXPATHLEN];
918 char *buf = NULL, *p;
921 u64 mem_total, mem_free, mem;
924 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
925 fp = fopen(str, "r");
929 while (getline(&buf, &len, fp) > 0) {
930 /* skip over invalid lines */
931 if (!strchr(buf, ':'))
933 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
935 if (!strcmp(field, "MemTotal:"))
937 if (!strcmp(field, "MemFree:"))
944 ret = do_write(fd, &mem_total, sizeof(u64));
948 ret = do_write(fd, &mem_free, sizeof(u64));
953 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
955 fp = fopen(str, "r");
959 if (getline(&buf, &len, fp) <= 0)
962 p = strchr(buf, '\n');
966 ret = do_write_string(fd, buf);
974 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
975 struct perf_evlist *evlist __maybe_unused)
980 struct cpu_map *node_map = NULL;
985 fp = fopen("/sys/devices/system/node/online", "r");
989 if (getline(&buf, &len, fp) <= 0)
992 c = strchr(buf, '\n');
996 node_map = cpu_map__new(buf);
1000 nr = (u32)node_map->nr;
1002 ret = do_write(fd, &nr, sizeof(nr));
1006 for (i = 0; i < nr; i++) {
1007 j = (u32)node_map->map[i];
1008 ret = do_write(fd, &j, sizeof(j));
1012 ret = write_topo_node(fd, i);
1026 * struct pmu_mappings {
1035 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1036 struct perf_evlist *evlist __maybe_unused)
1038 struct perf_pmu *pmu = NULL;
1039 off_t offset = lseek(fd, 0, SEEK_CUR);
1043 /* write real pmu_num later */
1044 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1048 while ((pmu = perf_pmu__scan(pmu))) {
1053 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1057 ret = do_write_string(fd, pmu->name);
1062 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1064 lseek(fd, offset, SEEK_SET);
1074 * struct group_descs {
1076 * struct group_desc {
1083 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1084 struct perf_evlist *evlist)
1086 u32 nr_groups = evlist->nr_groups;
1087 struct perf_evsel *evsel;
1090 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1094 evlist__for_each(evlist, evsel) {
1095 if (perf_evsel__is_group_leader(evsel) &&
1096 evsel->nr_members > 1) {
1097 const char *name = evsel->group_name ?: "{anon_group}";
1098 u32 leader_idx = evsel->idx;
1099 u32 nr_members = evsel->nr_members;
1101 ret = do_write_string(fd, name);
1105 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1109 ret = do_write(fd, &nr_members, sizeof(nr_members));
1118 * default get_cpuid(): nothing gets recorded
1119 * actual implementation must be in arch/$(ARCH)/util/header.c
1121 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1122 size_t sz __maybe_unused)
1127 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1128 struct perf_evlist *evlist __maybe_unused)
1133 ret = get_cpuid(buffer, sizeof(buffer));
1139 return do_write_string(fd, buffer);
1142 static int write_branch_stack(int fd __maybe_unused,
1143 struct perf_header *h __maybe_unused,
1144 struct perf_evlist *evlist __maybe_unused)
1149 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1152 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1155 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1158 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1161 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1163 fprintf(fp, "# arch : %s\n", ph->env.arch);
1166 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1169 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1172 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1175 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1176 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1179 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1182 fprintf(fp, "# perf version : %s\n", ph->env.version);
1185 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1191 nr = ph->env.nr_cmdline;
1192 str = ph->env.cmdline;
1194 fprintf(fp, "# cmdline : ");
1196 for (i = 0; i < nr; i++) {
1197 fprintf(fp, "%s ", str);
1198 str += strlen(str) + 1;
1203 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1209 nr = ph->env.nr_sibling_cores;
1210 str = ph->env.sibling_cores;
1212 for (i = 0; i < nr; i++) {
1213 fprintf(fp, "# sibling cores : %s\n", str);
1214 str += strlen(str) + 1;
1217 nr = ph->env.nr_sibling_threads;
1218 str = ph->env.sibling_threads;
1220 for (i = 0; i < nr; i++) {
1221 fprintf(fp, "# sibling threads : %s\n", str);
1222 str += strlen(str) + 1;
1226 static void free_event_desc(struct perf_evsel *events)
1228 struct perf_evsel *evsel;
1233 for (evsel = events; evsel->attr.size; evsel++) {
1234 zfree(&evsel->name);
1241 static struct perf_evsel *
1242 read_event_desc(struct perf_header *ph, int fd)
1244 struct perf_evsel *evsel, *events = NULL;
1247 u32 nre, sz, nr, i, j;
1251 /* number of events */
1252 ret = readn(fd, &nre, sizeof(nre));
1253 if (ret != (ssize_t)sizeof(nre))
1257 nre = bswap_32(nre);
1259 ret = readn(fd, &sz, sizeof(sz));
1260 if (ret != (ssize_t)sizeof(sz))
1266 /* buffer to hold on file attr struct */
1271 /* the last event terminates with evsel->attr.size == 0: */
1272 events = calloc(nre + 1, sizeof(*events));
1276 msz = sizeof(evsel->attr);
1280 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1284 * must read entire on-file attr struct to
1285 * sync up with layout.
1287 ret = readn(fd, buf, sz);
1288 if (ret != (ssize_t)sz)
1292 perf_event__attr_swap(buf);
1294 memcpy(&evsel->attr, buf, msz);
1296 ret = readn(fd, &nr, sizeof(nr));
1297 if (ret != (ssize_t)sizeof(nr))
1300 if (ph->needs_swap) {
1302 evsel->needs_swap = true;
1305 evsel->name = do_read_string(fd, ph);
1310 id = calloc(nr, sizeof(*id));
1316 for (j = 0 ; j < nr; j++) {
1317 ret = readn(fd, id, sizeof(*id));
1318 if (ret != (ssize_t)sizeof(*id))
1321 *id = bswap_64(*id);
1330 free_event_desc(events);
1335 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1337 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1342 fprintf(fp, "# event desc: not available or unable to read\n");
1346 for (evsel = events; evsel->attr.size; evsel++) {
1347 fprintf(fp, "# event : name = %s, ", evsel->name);
1349 fprintf(fp, "type = %d, config = 0x%"PRIx64
1350 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1352 (u64)evsel->attr.config,
1353 (u64)evsel->attr.config1,
1354 (u64)evsel->attr.config2);
1356 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1357 evsel->attr.exclude_user,
1358 evsel->attr.exclude_kernel);
1360 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1361 evsel->attr.exclude_host,
1362 evsel->attr.exclude_guest);
1364 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1366 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1367 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1368 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
1370 fprintf(fp, ", id = {");
1371 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1374 fprintf(fp, " %"PRIu64, *id);
1382 free_event_desc(events);
1385 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1388 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1391 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1396 uint64_t mem_total, mem_free;
1399 nr = ph->env.nr_numa_nodes;
1400 str = ph->env.numa_nodes;
1402 for (i = 0; i < nr; i++) {
1404 c = strtoul(str, &tmp, 0);
1409 mem_total = strtoull(str, &tmp, 0);
1414 mem_free = strtoull(str, &tmp, 0);
1418 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1419 " free = %"PRIu64" kB\n",
1420 c, mem_total, mem_free);
1423 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1425 str += strlen(str) + 1;
1429 fprintf(fp, "# numa topology : not available\n");
1432 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1434 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1437 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1438 int fd __maybe_unused, FILE *fp)
1440 fprintf(fp, "# contains samples with branch stack\n");
1443 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1446 const char *delimiter = "# pmu mappings: ";
1451 pmu_num = ph->env.nr_pmu_mappings;
1453 fprintf(fp, "# pmu mappings: not available\n");
1457 str = ph->env.pmu_mappings;
1460 type = strtoul(str, &tmp, 0);
1465 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1468 str += strlen(str) + 1;
1477 fprintf(fp, "# pmu mappings: unable to read\n");
1480 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1483 struct perf_session *session;
1484 struct perf_evsel *evsel;
1487 session = container_of(ph, struct perf_session, header);
1489 evlist__for_each(session->evlist, evsel) {
1490 if (perf_evsel__is_group_leader(evsel) &&
1491 evsel->nr_members > 1) {
1492 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1493 perf_evsel__name(evsel));
1495 nr = evsel->nr_members - 1;
1497 fprintf(fp, ",%s", perf_evsel__name(evsel));
1505 static int __event_process_build_id(struct build_id_event *bev,
1507 struct perf_session *session)
1510 struct list_head *head;
1511 struct machine *machine;
1514 enum dso_kernel_type dso_type;
1516 machine = perf_session__findnew_machine(session, bev->pid);
1520 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1523 case PERF_RECORD_MISC_KERNEL:
1524 dso_type = DSO_TYPE_KERNEL;
1525 head = &machine->kernel_dsos;
1527 case PERF_RECORD_MISC_GUEST_KERNEL:
1528 dso_type = DSO_TYPE_GUEST_KERNEL;
1529 head = &machine->kernel_dsos;
1531 case PERF_RECORD_MISC_USER:
1532 case PERF_RECORD_MISC_GUEST_USER:
1533 dso_type = DSO_TYPE_USER;
1534 head = &machine->user_dsos;
1540 dso = __dsos__findnew(head, filename);
1542 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1544 dso__set_build_id(dso, &bev->build_id);
1546 if (filename[0] == '[')
1547 dso->kernel = dso_type;
1549 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1551 pr_debug("build id event received for %s: %s\n",
1552 dso->long_name, sbuild_id);
1560 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1561 int input, u64 offset, u64 size)
1563 struct perf_session *session = container_of(header, struct perf_session, header);
1565 struct perf_event_header header;
1566 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1569 struct build_id_event bev;
1570 char filename[PATH_MAX];
1571 u64 limit = offset + size;
1573 while (offset < limit) {
1576 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1579 if (header->needs_swap)
1580 perf_event_header__bswap(&old_bev.header);
1582 len = old_bev.header.size - sizeof(old_bev);
1583 if (readn(input, filename, len) != len)
1586 bev.header = old_bev.header;
1589 * As the pid is the missing value, we need to fill
1590 * it properly. The header.misc value give us nice hint.
1592 bev.pid = HOST_KERNEL_ID;
1593 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1594 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1595 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1597 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1598 __event_process_build_id(&bev, filename, session);
1600 offset += bev.header.size;
1606 static int perf_header__read_build_ids(struct perf_header *header,
1607 int input, u64 offset, u64 size)
1609 struct perf_session *session = container_of(header, struct perf_session, header);
1610 struct build_id_event bev;
1611 char filename[PATH_MAX];
1612 u64 limit = offset + size, orig_offset = offset;
1615 while (offset < limit) {
1618 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1621 if (header->needs_swap)
1622 perf_event_header__bswap(&bev.header);
1624 len = bev.header.size - sizeof(bev);
1625 if (readn(input, filename, len) != len)
1628 * The a1645ce1 changeset:
1630 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1632 * Added a field to struct build_id_event that broke the file
1635 * Since the kernel build-id is the first entry, process the
1636 * table using the old format if the well known
1637 * '[kernel.kallsyms]' string for the kernel build-id has the
1638 * first 4 characters chopped off (where the pid_t sits).
1640 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1641 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1643 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1646 __event_process_build_id(&bev, filename, session);
1648 offset += bev.header.size;
1655 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1656 struct perf_header *ph __maybe_unused,
1659 ssize_t ret = trace_report(fd, data, false);
1660 return ret < 0 ? -1 : 0;
1663 static int process_build_id(struct perf_file_section *section,
1664 struct perf_header *ph, int fd,
1665 void *data __maybe_unused)
1667 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1668 pr_debug("Failed to read buildids, continuing...\n");
1672 static int process_hostname(struct perf_file_section *section __maybe_unused,
1673 struct perf_header *ph, int fd,
1674 void *data __maybe_unused)
1676 ph->env.hostname = do_read_string(fd, ph);
1677 return ph->env.hostname ? 0 : -ENOMEM;
1680 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1681 struct perf_header *ph, int fd,
1682 void *data __maybe_unused)
1684 ph->env.os_release = do_read_string(fd, ph);
1685 return ph->env.os_release ? 0 : -ENOMEM;
1688 static int process_version(struct perf_file_section *section __maybe_unused,
1689 struct perf_header *ph, int fd,
1690 void *data __maybe_unused)
1692 ph->env.version = do_read_string(fd, ph);
1693 return ph->env.version ? 0 : -ENOMEM;
1696 static int process_arch(struct perf_file_section *section __maybe_unused,
1697 struct perf_header *ph, int fd,
1698 void *data __maybe_unused)
1700 ph->env.arch = do_read_string(fd, ph);
1701 return ph->env.arch ? 0 : -ENOMEM;
1704 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1705 struct perf_header *ph, int fd,
1706 void *data __maybe_unused)
1711 ret = readn(fd, &nr, sizeof(nr));
1712 if (ret != sizeof(nr))
1718 ph->env.nr_cpus_online = nr;
1720 ret = readn(fd, &nr, sizeof(nr));
1721 if (ret != sizeof(nr))
1727 ph->env.nr_cpus_avail = nr;
1731 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1732 struct perf_header *ph, int fd,
1733 void *data __maybe_unused)
1735 ph->env.cpu_desc = do_read_string(fd, ph);
1736 return ph->env.cpu_desc ? 0 : -ENOMEM;
1739 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1740 struct perf_header *ph, int fd,
1741 void *data __maybe_unused)
1743 ph->env.cpuid = do_read_string(fd, ph);
1744 return ph->env.cpuid ? 0 : -ENOMEM;
1747 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1748 struct perf_header *ph, int fd,
1749 void *data __maybe_unused)
1754 ret = readn(fd, &mem, sizeof(mem));
1755 if (ret != sizeof(mem))
1759 mem = bswap_64(mem);
1761 ph->env.total_mem = mem;
1765 static struct perf_evsel *
1766 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1768 struct perf_evsel *evsel;
1770 evlist__for_each(evlist, evsel) {
1771 if (evsel->idx == idx)
1779 perf_evlist__set_event_name(struct perf_evlist *evlist,
1780 struct perf_evsel *event)
1782 struct perf_evsel *evsel;
1787 evsel = perf_evlist__find_by_index(evlist, event->idx);
1794 evsel->name = strdup(event->name);
1798 process_event_desc(struct perf_file_section *section __maybe_unused,
1799 struct perf_header *header, int fd,
1800 void *data __maybe_unused)
1802 struct perf_session *session;
1803 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1808 session = container_of(header, struct perf_session, header);
1809 for (evsel = events; evsel->attr.size; evsel++)
1810 perf_evlist__set_event_name(session->evlist, evsel);
1812 free_event_desc(events);
1817 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1818 struct perf_header *ph, int fd,
1819 void *data __maybe_unused)
1826 ret = readn(fd, &nr, sizeof(nr));
1827 if (ret != sizeof(nr))
1833 ph->env.nr_cmdline = nr;
1834 strbuf_init(&sb, 128);
1836 for (i = 0; i < nr; i++) {
1837 str = do_read_string(fd, ph);
1841 /* include a NULL character at the end */
1842 strbuf_add(&sb, str, strlen(str) + 1);
1845 ph->env.cmdline = strbuf_detach(&sb, NULL);
1849 strbuf_release(&sb);
1853 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1854 struct perf_header *ph, int fd,
1855 void *data __maybe_unused)
1862 ret = readn(fd, &nr, sizeof(nr));
1863 if (ret != sizeof(nr))
1869 ph->env.nr_sibling_cores = nr;
1870 strbuf_init(&sb, 128);
1872 for (i = 0; i < nr; i++) {
1873 str = do_read_string(fd, ph);
1877 /* include a NULL character at the end */
1878 strbuf_add(&sb, str, strlen(str) + 1);
1881 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1883 ret = readn(fd, &nr, sizeof(nr));
1884 if (ret != sizeof(nr))
1890 ph->env.nr_sibling_threads = nr;
1892 for (i = 0; i < nr; i++) {
1893 str = do_read_string(fd, ph);
1897 /* include a NULL character at the end */
1898 strbuf_add(&sb, str, strlen(str) + 1);
1901 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1905 strbuf_release(&sb);
1909 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1910 struct perf_header *ph, int fd,
1911 void *data __maybe_unused)
1916 uint64_t mem_total, mem_free;
1920 ret = readn(fd, &nr, sizeof(nr));
1921 if (ret != sizeof(nr))
1927 ph->env.nr_numa_nodes = nr;
1928 strbuf_init(&sb, 256);
1930 for (i = 0; i < nr; i++) {
1932 ret = readn(fd, &node, sizeof(node));
1933 if (ret != sizeof(node))
1936 ret = readn(fd, &mem_total, sizeof(u64));
1937 if (ret != sizeof(u64))
1940 ret = readn(fd, &mem_free, sizeof(u64));
1941 if (ret != sizeof(u64))
1944 if (ph->needs_swap) {
1945 node = bswap_32(node);
1946 mem_total = bswap_64(mem_total);
1947 mem_free = bswap_64(mem_free);
1950 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1951 node, mem_total, mem_free);
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.numa_nodes = strbuf_detach(&sb, NULL);
1965 strbuf_release(&sb);
1969 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1970 struct perf_header *ph, int fd,
1971 void *data __maybe_unused)
1979 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1980 if (ret != sizeof(pmu_num))
1984 pmu_num = bswap_32(pmu_num);
1987 pr_debug("pmu mappings not available\n");
1991 ph->env.nr_pmu_mappings = pmu_num;
1992 strbuf_init(&sb, 128);
1995 if (readn(fd, &type, sizeof(type)) != sizeof(type))
1998 type = bswap_32(type);
2000 name = do_read_string(fd, ph);
2004 strbuf_addf(&sb, "%u:%s", type, name);
2005 /* include a NULL character at the end */
2006 strbuf_add(&sb, "", 1);
2011 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2015 strbuf_release(&sb);
2019 static int process_group_desc(struct perf_file_section *section __maybe_unused,
2020 struct perf_header *ph, int fd,
2021 void *data __maybe_unused)
2024 u32 i, nr, nr_groups;
2025 struct perf_session *session;
2026 struct perf_evsel *evsel, *leader = NULL;
2033 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2037 nr_groups = bswap_32(nr_groups);
2039 ph->env.nr_groups = nr_groups;
2041 pr_debug("group desc not available\n");
2045 desc = calloc(nr_groups, sizeof(*desc));
2049 for (i = 0; i < nr_groups; i++) {
2050 desc[i].name = do_read_string(fd, ph);
2054 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2057 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2060 if (ph->needs_swap) {
2061 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2062 desc[i].nr_members = bswap_32(desc[i].nr_members);
2067 * Rebuild group relationship based on the group_desc
2069 session = container_of(ph, struct perf_session, header);
2070 session->evlist->nr_groups = nr_groups;
2073 evlist__for_each(session->evlist, evsel) {
2074 if (evsel->idx == (int) desc[i].leader_idx) {
2075 evsel->leader = evsel;
2076 /* {anon_group} is a dummy name */
2077 if (strcmp(desc[i].name, "{anon_group}")) {
2078 evsel->group_name = desc[i].name;
2079 desc[i].name = NULL;
2081 evsel->nr_members = desc[i].nr_members;
2083 if (i >= nr_groups || nr > 0) {
2084 pr_debug("invalid group desc\n");
2089 nr = evsel->nr_members - 1;
2092 /* This is a group member */
2093 evsel->leader = leader;
2099 if (i != nr_groups || nr != 0) {
2100 pr_debug("invalid group desc\n");
2106 for (i = 0; i < nr_groups; i++)
2107 zfree(&desc[i].name);
2113 struct feature_ops {
2114 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2115 void (*print)(struct perf_header *h, int fd, FILE *fp);
2116 int (*process)(struct perf_file_section *section,
2117 struct perf_header *h, int fd, void *data);
2122 #define FEAT_OPA(n, func) \
2123 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2124 #define FEAT_OPP(n, func) \
2125 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2126 .process = process_##func }
2127 #define FEAT_OPF(n, func) \
2128 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2129 .process = process_##func, .full_only = true }
2131 /* feature_ops not implemented: */
2132 #define print_tracing_data NULL
2133 #define print_build_id NULL
2135 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2136 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
2137 FEAT_OPP(HEADER_BUILD_ID, build_id),
2138 FEAT_OPP(HEADER_HOSTNAME, hostname),
2139 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2140 FEAT_OPP(HEADER_VERSION, version),
2141 FEAT_OPP(HEADER_ARCH, arch),
2142 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2143 FEAT_OPP(HEADER_CPUDESC, cpudesc),
2144 FEAT_OPP(HEADER_CPUID, cpuid),
2145 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
2146 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
2147 FEAT_OPP(HEADER_CMDLINE, cmdline),
2148 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2149 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
2150 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
2151 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2152 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
2155 struct header_print_data {
2157 bool full; /* extended list of headers */
2160 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2161 struct perf_header *ph,
2162 int feat, int fd, void *data)
2164 struct header_print_data *hd = data;
2166 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2167 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2168 "%d, continuing...\n", section->offset, feat);
2171 if (feat >= HEADER_LAST_FEATURE) {
2172 pr_warning("unknown feature %d\n", feat);
2175 if (!feat_ops[feat].print)
2178 if (!feat_ops[feat].full_only || hd->full)
2179 feat_ops[feat].print(ph, fd, hd->fp);
2181 fprintf(hd->fp, "# %s info available, use -I to display\n",
2182 feat_ops[feat].name);
2187 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2189 struct header_print_data hd;
2190 struct perf_header *header = &session->header;
2191 int fd = perf_data_file__fd(session->file);
2195 perf_header__process_sections(header, fd, &hd,
2196 perf_file_section__fprintf_info);
2200 static int do_write_feat(int fd, struct perf_header *h, int type,
2201 struct perf_file_section **p,
2202 struct perf_evlist *evlist)
2207 if (perf_header__has_feat(h, type)) {
2208 if (!feat_ops[type].write)
2211 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2213 err = feat_ops[type].write(fd, h, evlist);
2215 pr_debug("failed to write feature %d\n", type);
2217 /* undo anything written */
2218 lseek(fd, (*p)->offset, SEEK_SET);
2222 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2228 static int perf_header__adds_write(struct perf_header *header,
2229 struct perf_evlist *evlist, int fd)
2232 struct perf_file_section *feat_sec, *p;
2238 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2242 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2243 if (feat_sec == NULL)
2246 sec_size = sizeof(*feat_sec) * nr_sections;
2248 sec_start = header->feat_offset;
2249 lseek(fd, sec_start + sec_size, SEEK_SET);
2251 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2252 if (do_write_feat(fd, header, feat, &p, evlist))
2253 perf_header__clear_feat(header, feat);
2256 lseek(fd, sec_start, SEEK_SET);
2258 * may write more than needed due to dropped feature, but
2259 * this is okay, reader will skip the mising entries
2261 err = do_write(fd, feat_sec, sec_size);
2263 pr_debug("failed to write feature section\n");
2268 int perf_header__write_pipe(int fd)
2270 struct perf_pipe_file_header f_header;
2273 f_header = (struct perf_pipe_file_header){
2274 .magic = PERF_MAGIC,
2275 .size = sizeof(f_header),
2278 err = do_write(fd, &f_header, sizeof(f_header));
2280 pr_debug("failed to write perf pipe header\n");
2287 int perf_session__write_header(struct perf_session *session,
2288 struct perf_evlist *evlist,
2289 int fd, bool at_exit)
2291 struct perf_file_header f_header;
2292 struct perf_file_attr f_attr;
2293 struct perf_header *header = &session->header;
2294 struct perf_evsel *evsel;
2298 lseek(fd, sizeof(f_header), SEEK_SET);
2300 evlist__for_each(session->evlist, evsel) {
2301 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2302 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2304 pr_debug("failed to write perf header\n");
2309 attr_offset = lseek(fd, 0, SEEK_CUR);
2311 evlist__for_each(evlist, evsel) {
2312 f_attr = (struct perf_file_attr){
2313 .attr = evsel->attr,
2315 .offset = evsel->id_offset,
2316 .size = evsel->ids * sizeof(u64),
2319 err = do_write(fd, &f_attr, sizeof(f_attr));
2321 pr_debug("failed to write perf header attribute\n");
2326 if (!header->data_offset)
2327 header->data_offset = lseek(fd, 0, SEEK_CUR);
2328 header->feat_offset = header->data_offset + header->data_size;
2331 err = perf_header__adds_write(header, evlist, fd);
2336 f_header = (struct perf_file_header){
2337 .magic = PERF_MAGIC,
2338 .size = sizeof(f_header),
2339 .attr_size = sizeof(f_attr),
2341 .offset = attr_offset,
2342 .size = evlist->nr_entries * sizeof(f_attr),
2345 .offset = header->data_offset,
2346 .size = header->data_size,
2348 /* event_types is ignored, store zeros */
2351 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2353 lseek(fd, 0, SEEK_SET);
2354 err = do_write(fd, &f_header, sizeof(f_header));
2356 pr_debug("failed to write perf header\n");
2359 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2364 static int perf_header__getbuffer64(struct perf_header *header,
2365 int fd, void *buf, size_t size)
2367 if (readn(fd, buf, size) <= 0)
2370 if (header->needs_swap)
2371 mem_bswap_64(buf, size);
2376 int perf_header__process_sections(struct perf_header *header, int fd,
2378 int (*process)(struct perf_file_section *section,
2379 struct perf_header *ph,
2380 int feat, int fd, void *data))
2382 struct perf_file_section *feat_sec, *sec;
2388 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2392 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2396 sec_size = sizeof(*feat_sec) * nr_sections;
2398 lseek(fd, header->feat_offset, SEEK_SET);
2400 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2404 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2405 err = process(sec++, header, feat, fd, data);
2415 static const int attr_file_abi_sizes[] = {
2416 [0] = PERF_ATTR_SIZE_VER0,
2417 [1] = PERF_ATTR_SIZE_VER1,
2418 [2] = PERF_ATTR_SIZE_VER2,
2419 [3] = PERF_ATTR_SIZE_VER3,
2424 * In the legacy file format, the magic number is not used to encode endianness.
2425 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2426 * on ABI revisions, we need to try all combinations for all endianness to
2427 * detect the endianness.
2429 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2431 uint64_t ref_size, attr_size;
2434 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2435 ref_size = attr_file_abi_sizes[i]
2436 + sizeof(struct perf_file_section);
2437 if (hdr_sz != ref_size) {
2438 attr_size = bswap_64(hdr_sz);
2439 if (attr_size != ref_size)
2442 ph->needs_swap = true;
2444 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2449 /* could not determine endianness */
2453 #define PERF_PIPE_HDR_VER0 16
2455 static const size_t attr_pipe_abi_sizes[] = {
2456 [0] = PERF_PIPE_HDR_VER0,
2461 * In the legacy pipe format, there is an implicit assumption that endiannesss
2462 * between host recording the samples, and host parsing the samples is the
2463 * same. This is not always the case given that the pipe output may always be
2464 * redirected into a file and analyzed on a different machine with possibly a
2465 * different endianness and perf_event ABI revsions in the perf tool itself.
2467 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2472 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2473 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2474 attr_size = bswap_64(hdr_sz);
2475 if (attr_size != hdr_sz)
2478 ph->needs_swap = true;
2480 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2486 bool is_perf_magic(u64 magic)
2488 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2489 || magic == __perf_magic2
2490 || magic == __perf_magic2_sw)
2496 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2497 bool is_pipe, struct perf_header *ph)
2501 /* check for legacy format */
2502 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2504 ph->version = PERF_HEADER_VERSION_1;
2505 pr_debug("legacy perf.data format\n");
2507 return try_all_pipe_abis(hdr_sz, ph);
2509 return try_all_file_abis(hdr_sz, ph);
2512 * the new magic number serves two purposes:
2513 * - unique number to identify actual perf.data files
2514 * - encode endianness of file
2517 /* check magic number with one endianness */
2518 if (magic == __perf_magic2)
2521 /* check magic number with opposite endianness */
2522 if (magic != __perf_magic2_sw)
2525 ph->needs_swap = true;
2526 ph->version = PERF_HEADER_VERSION_2;
2531 int perf_file_header__read(struct perf_file_header *header,
2532 struct perf_header *ph, int fd)
2536 lseek(fd, 0, SEEK_SET);
2538 ret = readn(fd, header, sizeof(*header));
2542 if (check_magic_endian(header->magic,
2543 header->attr_size, false, ph) < 0) {
2544 pr_debug("magic/endian check failed\n");
2548 if (ph->needs_swap) {
2549 mem_bswap_64(header, offsetof(struct perf_file_header,
2553 if (header->size != sizeof(*header)) {
2554 /* Support the previous format */
2555 if (header->size == offsetof(typeof(*header), adds_features))
2556 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2559 } else if (ph->needs_swap) {
2561 * feature bitmap is declared as an array of unsigned longs --
2562 * not good since its size can differ between the host that
2563 * generated the data file and the host analyzing the file.
2565 * We need to handle endianness, but we don't know the size of
2566 * the unsigned long where the file was generated. Take a best
2567 * guess at determining it: try 64-bit swap first (ie., file
2568 * created on a 64-bit host), and check if the hostname feature
2569 * bit is set (this feature bit is forced on as of fbe96f2).
2570 * If the bit is not, undo the 64-bit swap and try a 32-bit
2571 * swap. If the hostname bit is still not set (e.g., older data
2572 * file), punt and fallback to the original behavior --
2573 * clearing all feature bits and setting buildid.
2575 mem_bswap_64(&header->adds_features,
2576 BITS_TO_U64(HEADER_FEAT_BITS));
2578 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2580 mem_bswap_64(&header->adds_features,
2581 BITS_TO_U64(HEADER_FEAT_BITS));
2584 mem_bswap_32(&header->adds_features,
2585 BITS_TO_U32(HEADER_FEAT_BITS));
2588 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2589 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2590 set_bit(HEADER_BUILD_ID, header->adds_features);
2594 memcpy(&ph->adds_features, &header->adds_features,
2595 sizeof(ph->adds_features));
2597 ph->data_offset = header->data.offset;
2598 ph->data_size = header->data.size;
2599 ph->feat_offset = header->data.offset + header->data.size;
2603 static int perf_file_section__process(struct perf_file_section *section,
2604 struct perf_header *ph,
2605 int feat, int fd, void *data)
2607 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2608 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2609 "%d, continuing...\n", section->offset, feat);
2613 if (feat >= HEADER_LAST_FEATURE) {
2614 pr_debug("unknown feature %d, continuing...\n", feat);
2618 if (!feat_ops[feat].process)
2621 return feat_ops[feat].process(section, ph, fd, data);
2624 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2625 struct perf_header *ph, int fd,
2630 ret = readn(fd, header, sizeof(*header));
2634 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2635 pr_debug("endian/magic failed\n");
2640 header->size = bswap_64(header->size);
2642 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2648 static int perf_header__read_pipe(struct perf_session *session)
2650 struct perf_header *header = &session->header;
2651 struct perf_pipe_file_header f_header;
2653 if (perf_file_header__read_pipe(&f_header, header,
2654 perf_data_file__fd(session->file),
2655 session->repipe) < 0) {
2656 pr_debug("incompatible file format\n");
2663 static int read_attr(int fd, struct perf_header *ph,
2664 struct perf_file_attr *f_attr)
2666 struct perf_event_attr *attr = &f_attr->attr;
2668 size_t our_sz = sizeof(f_attr->attr);
2671 memset(f_attr, 0, sizeof(*f_attr));
2673 /* read minimal guaranteed structure */
2674 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2676 pr_debug("cannot read %d bytes of header attr\n",
2677 PERF_ATTR_SIZE_VER0);
2681 /* on file perf_event_attr size */
2689 sz = PERF_ATTR_SIZE_VER0;
2690 } else if (sz > our_sz) {
2691 pr_debug("file uses a more recent and unsupported ABI"
2692 " (%zu bytes extra)\n", sz - our_sz);
2695 /* what we have not yet read and that we know about */
2696 left = sz - PERF_ATTR_SIZE_VER0;
2699 ptr += PERF_ATTR_SIZE_VER0;
2701 ret = readn(fd, ptr, left);
2703 /* read perf_file_section, ids are read in caller */
2704 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2706 return ret <= 0 ? -1 : 0;
2709 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2710 struct pevent *pevent)
2712 struct event_format *event;
2715 /* already prepared */
2716 if (evsel->tp_format)
2719 if (pevent == NULL) {
2720 pr_debug("broken or missing trace data\n");
2724 event = pevent_find_event(pevent, evsel->attr.config);
2729 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2730 evsel->name = strdup(bf);
2731 if (evsel->name == NULL)
2735 evsel->tp_format = event;
2739 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2740 struct pevent *pevent)
2742 struct perf_evsel *pos;
2744 evlist__for_each(evlist, pos) {
2745 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2746 perf_evsel__prepare_tracepoint_event(pos, pevent))
2753 int perf_session__read_header(struct perf_session *session)
2755 struct perf_data_file *file = session->file;
2756 struct perf_header *header = &session->header;
2757 struct perf_file_header f_header;
2758 struct perf_file_attr f_attr;
2760 int nr_attrs, nr_ids, i, j;
2761 int fd = perf_data_file__fd(file);
2763 session->evlist = perf_evlist__new();
2764 if (session->evlist == NULL)
2767 if (perf_data_file__is_pipe(file))
2768 return perf_header__read_pipe(session);
2770 if (perf_file_header__read(&f_header, header, fd) < 0)
2774 * Sanity check that perf.data was written cleanly; data size is
2775 * initialized to 0 and updated only if the on_exit function is run.
2776 * If data size is still 0 then the file contains only partial
2777 * information. Just warn user and process it as much as it can.
2779 if (f_header.data.size == 0) {
2780 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2781 "Was the 'perf record' command properly terminated?\n",
2785 nr_attrs = f_header.attrs.size / f_header.attr_size;
2786 lseek(fd, f_header.attrs.offset, SEEK_SET);
2788 for (i = 0; i < nr_attrs; i++) {
2789 struct perf_evsel *evsel;
2792 if (read_attr(fd, header, &f_attr) < 0)
2795 if (header->needs_swap)
2796 perf_event__attr_swap(&f_attr.attr);
2798 tmp = lseek(fd, 0, SEEK_CUR);
2799 evsel = perf_evsel__new(&f_attr.attr);
2802 goto out_delete_evlist;
2804 evsel->needs_swap = header->needs_swap;
2806 * Do it before so that if perf_evsel__alloc_id fails, this
2807 * entry gets purged too at perf_evlist__delete().
2809 perf_evlist__add(session->evlist, evsel);
2811 nr_ids = f_attr.ids.size / sizeof(u64);
2813 * We don't have the cpu and thread maps on the header, so
2814 * for allocating the perf_sample_id table we fake 1 cpu and
2815 * hattr->ids threads.
2817 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2818 goto out_delete_evlist;
2820 lseek(fd, f_attr.ids.offset, SEEK_SET);
2822 for (j = 0; j < nr_ids; j++) {
2823 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2826 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2829 lseek(fd, tmp, SEEK_SET);
2832 symbol_conf.nr_events = nr_attrs;
2834 perf_header__process_sections(header, fd, &session->tevent,
2835 perf_file_section__process);
2837 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2838 session->tevent.pevent))
2839 goto out_delete_evlist;
2846 perf_evlist__delete(session->evlist);
2847 session->evlist = NULL;
2851 int perf_event__synthesize_attr(struct perf_tool *tool,
2852 struct perf_event_attr *attr, u32 ids, u64 *id,
2853 perf_event__handler_t process)
2855 union perf_event *ev;
2859 size = sizeof(struct perf_event_attr);
2860 size = PERF_ALIGN(size, sizeof(u64));
2861 size += sizeof(struct perf_event_header);
2862 size += ids * sizeof(u64);
2869 ev->attr.attr = *attr;
2870 memcpy(ev->attr.id, id, ids * sizeof(u64));
2872 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2873 ev->attr.header.size = (u16)size;
2875 if (ev->attr.header.size == size)
2876 err = process(tool, ev, NULL, NULL);
2885 int perf_event__synthesize_attrs(struct perf_tool *tool,
2886 struct perf_session *session,
2887 perf_event__handler_t process)
2889 struct perf_evsel *evsel;
2892 evlist__for_each(session->evlist, evsel) {
2893 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2894 evsel->id, process);
2896 pr_debug("failed to create perf header attribute\n");
2904 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2905 union perf_event *event,
2906 struct perf_evlist **pevlist)
2909 struct perf_evsel *evsel;
2910 struct perf_evlist *evlist = *pevlist;
2912 if (evlist == NULL) {
2913 *pevlist = evlist = perf_evlist__new();
2918 evsel = perf_evsel__new(&event->attr.attr);
2922 perf_evlist__add(evlist, evsel);
2924 ids = event->header.size;
2925 ids -= (void *)&event->attr.id - (void *)event;
2926 n_ids = ids / sizeof(u64);
2928 * We don't have the cpu and thread maps on the header, so
2929 * for allocating the perf_sample_id table we fake 1 cpu and
2930 * hattr->ids threads.
2932 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2935 for (i = 0; i < n_ids; i++) {
2936 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2939 symbol_conf.nr_events = evlist->nr_entries;
2944 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2945 struct perf_evlist *evlist,
2946 perf_event__handler_t process)
2948 union perf_event ev;
2949 struct tracing_data *tdata;
2950 ssize_t size = 0, aligned_size = 0, padding;
2951 int err __maybe_unused = 0;
2954 * We are going to store the size of the data followed
2955 * by the data contents. Since the fd descriptor is a pipe,
2956 * we cannot seek back to store the size of the data once
2957 * we know it. Instead we:
2959 * - write the tracing data to the temp file
2960 * - get/write the data size to pipe
2961 * - write the tracing data from the temp file
2964 tdata = tracing_data_get(&evlist->entries, fd, true);
2968 memset(&ev, 0, sizeof(ev));
2970 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2972 aligned_size = PERF_ALIGN(size, sizeof(u64));
2973 padding = aligned_size - size;
2974 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2975 ev.tracing_data.size = aligned_size;
2977 process(tool, &ev, NULL, NULL);
2980 * The put function will copy all the tracing data
2981 * stored in temp file to the pipe.
2983 tracing_data_put(tdata);
2985 write_padded(fd, NULL, 0, padding);
2987 return aligned_size;
2990 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2991 union perf_event *event,
2992 struct perf_session *session)
2994 ssize_t size_read, padding, size = event->tracing_data.size;
2995 int fd = perf_data_file__fd(session->file);
2996 off_t offset = lseek(fd, 0, SEEK_CUR);
2999 /* setup for reading amidst mmap */
3000 lseek(fd, offset + sizeof(struct tracing_data_event),
3003 size_read = trace_report(fd, &session->tevent,
3005 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3007 if (readn(fd, buf, padding) < 0) {
3008 pr_err("%s: reading input file", __func__);
3011 if (session->repipe) {
3012 int retw = write(STDOUT_FILENO, buf, padding);
3013 if (retw <= 0 || retw != padding) {
3014 pr_err("%s: repiping tracing data padding", __func__);
3019 if (size_read + padding != size) {
3020 pr_err("%s: tracing data size mismatch", __func__);
3024 perf_evlist__prepare_tracepoint_events(session->evlist,
3025 session->tevent.pevent);
3027 return size_read + padding;
3030 int perf_event__synthesize_build_id(struct perf_tool *tool,
3031 struct dso *pos, u16 misc,
3032 perf_event__handler_t process,
3033 struct machine *machine)
3035 union perf_event ev;
3042 memset(&ev, 0, sizeof(ev));
3044 len = pos->long_name_len + 1;
3045 len = PERF_ALIGN(len, NAME_ALIGN);
3046 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3047 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3048 ev.build_id.header.misc = misc;
3049 ev.build_id.pid = machine->pid;
3050 ev.build_id.header.size = sizeof(ev.build_id) + len;
3051 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3053 err = process(tool, &ev, NULL, machine);
3058 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3059 union perf_event *event,
3060 struct perf_session *session)
3062 __event_process_build_id(&event->build_id,
3063 event->build_id.filename,
3068 void disable_buildid_cache(void)
3070 no_buildid_cache = true;