1 #define _FILE_OFFSET_BITS 64
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/bitops.h>
12 #include <sys/utsname.h>
18 #include "trace-event.h"
27 static bool no_buildid_cache = false;
29 static int trace_event_count;
30 static struct perf_trace_event_type *trace_events;
32 static u32 header_argc;
33 static const char **header_argv;
35 int perf_header__push_event(u64 id, const char *name)
37 struct perf_trace_event_type *nevents;
39 if (strlen(name) > MAX_EVENT_NAME)
40 pr_warning("Event %s will be truncated\n", name);
42 nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events));
45 trace_events = nevents;
47 memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type));
48 trace_events[trace_event_count].event_id = id;
49 strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1);
54 char *perf_header__find_event(u64 id)
57 for (i = 0 ; i < trace_event_count; i++) {
58 if (trace_events[i].event_id == id)
59 return trace_events[i].name;
66 * must be a numerical value to let the endianness
67 * determine the memory layout. That way we are able
68 * to detect endianness when reading the perf.data file
71 * we check for legacy (PERFFILE) format.
73 static const char *__perf_magic1 = "PERFFILE";
74 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
75 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
77 #define PERF_MAGIC __perf_magic2
79 struct perf_file_attr {
80 struct perf_event_attr attr;
81 struct perf_file_section ids;
84 void perf_header__set_feat(struct perf_header *header, int feat)
86 set_bit(feat, header->adds_features);
89 void perf_header__clear_feat(struct perf_header *header, int feat)
91 clear_bit(feat, header->adds_features);
94 bool perf_header__has_feat(const struct perf_header *header, int feat)
96 return test_bit(feat, header->adds_features);
99 static int do_write(int fd, const void *buf, size_t size)
102 int ret = write(fd, buf, size);
114 #define NAME_ALIGN 64
116 static int write_padded(int fd, const void *bf, size_t count,
117 size_t count_aligned)
119 static const char zero_buf[NAME_ALIGN];
120 int err = do_write(fd, bf, count);
123 err = do_write(fd, zero_buf, count_aligned - count);
128 static int do_write_string(int fd, const char *str)
133 olen = strlen(str) + 1;
134 len = PERF_ALIGN(olen, NAME_ALIGN);
136 /* write len, incl. \0 */
137 ret = do_write(fd, &len, sizeof(len));
141 return write_padded(fd, str, olen, len);
144 static char *do_read_string(int fd, struct perf_header *ph)
150 sz = read(fd, &len, sizeof(len));
151 if (sz < (ssize_t)sizeof(len))
161 ret = read(fd, buf, len);
162 if (ret == (ssize_t)len) {
164 * strings are padded by zeroes
165 * thus the actual strlen of buf
166 * may be less than len
176 perf_header__set_cmdline(int argc, const char **argv)
181 * If header_argv has already been set, do not override it.
182 * This allows a command to set the cmdline, parse args and
183 * then call another builtin function that implements a
184 * command -- e.g, cmd_kvm calling cmd_record.
189 header_argc = (u32)argc;
191 /* do not include NULL termination */
192 header_argv = calloc(argc, sizeof(char *));
197 * must copy argv contents because it gets moved
198 * around during option parsing
200 for (i = 0; i < argc ; i++)
201 header_argv[i] = argv[i];
206 #define dsos__for_each_with_build_id(pos, head) \
207 list_for_each_entry(pos, head, node) \
208 if (!pos->has_build_id) \
212 static int write_buildid(char *name, size_t name_len, u8 *build_id,
213 pid_t pid, u16 misc, int fd)
216 struct build_id_event b;
220 len = PERF_ALIGN(len, NAME_ALIGN);
222 memset(&b, 0, sizeof(b));
223 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
225 b.header.misc = misc;
226 b.header.size = sizeof(b) + len;
228 err = do_write(fd, &b, sizeof(b));
232 return write_padded(fd, name, name_len + 1, len);
235 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
240 dsos__for_each_with_build_id(pos, head) {
248 if (is_vdso_map(pos->short_name)) {
249 name = (char *) VDSO__MAP_NAME;
250 name_len = sizeof(VDSO__MAP_NAME) + 1;
252 name = pos->long_name;
253 name_len = pos->long_name_len + 1;
256 err = write_buildid(name, name_len, pos->build_id,
265 static int machine__write_buildid_table(struct machine *machine, int fd)
268 u16 kmisc = PERF_RECORD_MISC_KERNEL,
269 umisc = PERF_RECORD_MISC_USER;
271 if (!machine__is_host(machine)) {
272 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
273 umisc = PERF_RECORD_MISC_GUEST_USER;
276 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
279 err = __dsos__write_buildid_table(&machine->user_dsos,
280 machine->pid, umisc, fd);
284 static int dsos__write_buildid_table(struct perf_header *header, int fd)
286 struct perf_session *session = container_of(header,
287 struct perf_session, header);
289 int err = machine__write_buildid_table(&session->host_machine, fd);
294 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
295 struct machine *pos = rb_entry(nd, struct machine, rb_node);
296 err = machine__write_buildid_table(pos, fd);
303 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
304 const char *name, bool is_kallsyms, bool is_vdso)
306 const size_t size = PATH_MAX;
307 char *realname, *filename = zalloc(size),
308 *linkname = zalloc(size), *targetname;
310 bool slash = is_kallsyms || is_vdso;
313 if (symbol_conf.kptr_restrict) {
314 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
317 realname = (char *) name;
319 realname = realpath(name, NULL);
321 if (realname == NULL || filename == NULL || linkname == NULL)
324 len = scnprintf(filename, size, "%s%s%s",
325 debugdir, slash ? "/" : "",
326 is_vdso ? VDSO__MAP_NAME : realname);
327 if (mkdir_p(filename, 0755))
330 snprintf(filename + len, size - len, "/%s", sbuild_id);
332 if (access(filename, F_OK)) {
334 if (copyfile("/proc/kallsyms", filename))
336 } else if (link(realname, filename) && copyfile(name, filename))
340 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
341 debugdir, sbuild_id);
343 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
346 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
347 targetname = filename + strlen(debugdir) - 5;
348 memcpy(targetname, "../..", 5);
350 if (symlink(targetname, linkname) == 0)
360 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
361 const char *name, const char *debugdir,
362 bool is_kallsyms, bool is_vdso)
364 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
366 build_id__sprintf(build_id, build_id_size, sbuild_id);
368 return build_id_cache__add_s(sbuild_id, debugdir, name,
369 is_kallsyms, is_vdso);
372 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
374 const size_t size = PATH_MAX;
375 char *filename = zalloc(size),
376 *linkname = zalloc(size);
379 if (filename == NULL || linkname == NULL)
382 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
383 debugdir, sbuild_id, sbuild_id + 2);
385 if (access(linkname, F_OK))
388 if (readlink(linkname, filename, size - 1) < 0)
391 if (unlink(linkname))
395 * Since the link is relative, we must make it absolute:
397 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
398 debugdir, sbuild_id, filename);
400 if (unlink(linkname))
410 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
412 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
413 bool is_vdso = is_vdso_map(dso->short_name);
415 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
416 dso->long_name, debugdir,
417 is_kallsyms, is_vdso);
420 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
425 dsos__for_each_with_build_id(pos, head)
426 if (dso__cache_build_id(pos, debugdir))
432 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
434 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
435 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
439 static int perf_session__cache_build_ids(struct perf_session *session)
443 char debugdir[PATH_MAX];
445 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
447 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
450 ret = machine__cache_build_ids(&session->host_machine, debugdir);
452 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
453 struct machine *pos = rb_entry(nd, struct machine, rb_node);
454 ret |= machine__cache_build_ids(pos, debugdir);
459 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
461 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
462 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
466 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
469 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
471 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
472 struct machine *pos = rb_entry(nd, struct machine, rb_node);
473 ret |= machine__read_build_ids(pos, with_hits);
479 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
480 struct perf_evlist *evlist)
482 return read_tracing_data(fd, &evlist->entries);
486 static int write_build_id(int fd, struct perf_header *h,
487 struct perf_evlist *evlist __maybe_unused)
489 struct perf_session *session;
492 session = container_of(h, struct perf_session, header);
494 if (!perf_session__read_build_ids(session, true))
497 err = dsos__write_buildid_table(h, fd);
499 pr_debug("failed to write buildid table\n");
502 if (!no_buildid_cache)
503 perf_session__cache_build_ids(session);
508 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
509 struct perf_evlist *evlist __maybe_unused)
518 return do_write_string(fd, uts.nodename);
521 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
522 struct perf_evlist *evlist __maybe_unused)
531 return do_write_string(fd, uts.release);
534 static int write_arch(int fd, struct perf_header *h __maybe_unused,
535 struct perf_evlist *evlist __maybe_unused)
544 return do_write_string(fd, uts.machine);
547 static int write_version(int fd, struct perf_header *h __maybe_unused,
548 struct perf_evlist *evlist __maybe_unused)
550 return do_write_string(fd, perf_version_string);
553 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
554 struct perf_evlist *evlist __maybe_unused)
557 #define CPUINFO_PROC NULL
562 const char *search = CPUINFO_PROC;
569 file = fopen("/proc/cpuinfo", "r");
573 while (getline(&buf, &len, file) > 0) {
574 ret = strncmp(buf, search, strlen(search));
584 p = strchr(buf, ':');
585 if (p && *(p+1) == ' ' && *(p+2))
591 /* squash extra space characters (branding string) */
598 while (*q && isspace(*q))
601 while ((*r++ = *q++));
605 ret = do_write_string(fd, s);
612 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
613 struct perf_evlist *evlist __maybe_unused)
619 nr = sysconf(_SC_NPROCESSORS_CONF);
623 nrc = (u32)(nr & UINT_MAX);
625 nr = sysconf(_SC_NPROCESSORS_ONLN);
629 nra = (u32)(nr & UINT_MAX);
631 ret = do_write(fd, &nrc, sizeof(nrc));
635 return do_write(fd, &nra, sizeof(nra));
638 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
639 struct perf_evlist *evlist)
641 struct perf_evsel *evsel;
645 nre = evlist->nr_entries;
648 * write number of events
650 ret = do_write(fd, &nre, sizeof(nre));
655 * size of perf_event_attr struct
657 sz = (u32)sizeof(evsel->attr);
658 ret = do_write(fd, &sz, sizeof(sz));
662 list_for_each_entry(evsel, &evlist->entries, node) {
664 ret = do_write(fd, &evsel->attr, sz);
668 * write number of unique id per event
669 * there is one id per instance of an event
671 * copy into an nri to be independent of the
675 ret = do_write(fd, &nri, sizeof(nri));
680 * write event string as passed on cmdline
682 ret = do_write_string(fd, perf_evsel__name(evsel));
686 * write unique ids for this event
688 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
695 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
696 struct perf_evlist *evlist __maybe_unused)
698 char buf[MAXPATHLEN];
704 * actual atual path to perf binary
706 sprintf(proc, "/proc/%d/exe", getpid());
707 ret = readlink(proc, buf, sizeof(buf));
711 /* readlink() does not add null termination */
714 /* account for binary path */
717 ret = do_write(fd, &n, sizeof(n));
721 ret = do_write_string(fd, buf);
725 for (i = 0 ; i < header_argc; i++) {
726 ret = do_write_string(fd, header_argv[i]);
733 #define CORE_SIB_FMT \
734 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
735 #define THRD_SIB_FMT \
736 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
741 char **core_siblings;
742 char **thread_siblings;
745 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
748 char filename[MAXPATHLEN];
749 char *buf = NULL, *p;
754 sprintf(filename, CORE_SIB_FMT, cpu);
755 fp = fopen(filename, "r");
759 if (getline(&buf, &len, fp) <= 0)
764 p = strchr(buf, '\n');
768 for (i = 0; i < tp->core_sib; i++) {
769 if (!strcmp(buf, tp->core_siblings[i]))
772 if (i == tp->core_sib) {
773 tp->core_siblings[i] = buf;
779 sprintf(filename, THRD_SIB_FMT, cpu);
780 fp = fopen(filename, "r");
784 if (getline(&buf, &len, fp) <= 0)
787 p = strchr(buf, '\n');
791 for (i = 0; i < tp->thread_sib; i++) {
792 if (!strcmp(buf, tp->thread_siblings[i]))
795 if (i == tp->thread_sib) {
796 tp->thread_siblings[i] = buf;
808 static void free_cpu_topo(struct cpu_topo *tp)
815 for (i = 0 ; i < tp->core_sib; i++)
816 free(tp->core_siblings[i]);
818 for (i = 0 ; i < tp->thread_sib; i++)
819 free(tp->thread_siblings[i]);
824 static struct cpu_topo *build_cpu_topology(void)
833 ncpus = sysconf(_SC_NPROCESSORS_CONF);
837 nr = (u32)(ncpus & UINT_MAX);
839 sz = nr * sizeof(char *);
841 addr = calloc(1, sizeof(*tp) + 2 * sz);
848 tp->core_siblings = addr;
850 tp->thread_siblings = addr;
852 for (i = 0; i < nr; i++) {
853 ret = build_cpu_topo(tp, i);
864 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
865 struct perf_evlist *evlist __maybe_unused)
871 tp = build_cpu_topology();
875 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
879 for (i = 0; i < tp->core_sib; i++) {
880 ret = do_write_string(fd, tp->core_siblings[i]);
884 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
888 for (i = 0; i < tp->thread_sib; i++) {
889 ret = do_write_string(fd, tp->thread_siblings[i]);
900 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
901 struct perf_evlist *evlist __maybe_unused)
909 fp = fopen("/proc/meminfo", "r");
913 while (getline(&buf, &len, fp) > 0) {
914 ret = strncmp(buf, "MemTotal:", 9);
919 n = sscanf(buf, "%*s %"PRIu64, &mem);
921 ret = do_write(fd, &mem, sizeof(mem));
928 static int write_topo_node(int fd, int node)
930 char str[MAXPATHLEN];
932 char *buf = NULL, *p;
935 u64 mem_total, mem_free, mem;
938 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
939 fp = fopen(str, "r");
943 while (getline(&buf, &len, fp) > 0) {
944 /* skip over invalid lines */
945 if (!strchr(buf, ':'))
947 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
949 if (!strcmp(field, "MemTotal:"))
951 if (!strcmp(field, "MemFree:"))
957 ret = do_write(fd, &mem_total, sizeof(u64));
961 ret = do_write(fd, &mem_free, sizeof(u64));
966 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
968 fp = fopen(str, "r");
972 if (getline(&buf, &len, fp) <= 0)
975 p = strchr(buf, '\n');
979 ret = do_write_string(fd, buf);
986 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
987 struct perf_evlist *evlist __maybe_unused)
992 struct cpu_map *node_map = NULL;
997 fp = fopen("/sys/devices/system/node/online", "r");
1001 if (getline(&buf, &len, fp) <= 0)
1004 c = strchr(buf, '\n');
1008 node_map = cpu_map__new(buf);
1012 nr = (u32)node_map->nr;
1014 ret = do_write(fd, &nr, sizeof(nr));
1018 for (i = 0; i < nr; i++) {
1019 j = (u32)node_map->map[i];
1020 ret = do_write(fd, &j, sizeof(j));
1024 ret = write_topo_node(fd, i);
1038 * struct pmu_mappings {
1047 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1048 struct perf_evlist *evlist __maybe_unused)
1050 struct perf_pmu *pmu = NULL;
1051 off_t offset = lseek(fd, 0, SEEK_CUR);
1054 /* write real pmu_num later */
1055 do_write(fd, &pmu_num, sizeof(pmu_num));
1057 while ((pmu = perf_pmu__scan(pmu))) {
1061 do_write(fd, &pmu->type, sizeof(pmu->type));
1062 do_write_string(fd, pmu->name);
1065 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1067 lseek(fd, offset, SEEK_SET);
1075 * default get_cpuid(): nothing gets recorded
1076 * actual implementation must be in arch/$(ARCH)/util/header.c
1078 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1079 size_t sz __maybe_unused)
1084 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1085 struct perf_evlist *evlist __maybe_unused)
1090 ret = get_cpuid(buffer, sizeof(buffer));
1096 return do_write_string(fd, buffer);
1099 static int write_branch_stack(int fd __maybe_unused,
1100 struct perf_header *h __maybe_unused,
1101 struct perf_evlist *evlist __maybe_unused)
1106 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1109 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1112 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1115 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1118 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1120 fprintf(fp, "# arch : %s\n", ph->env.arch);
1123 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1126 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1129 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1132 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1133 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1136 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1139 fprintf(fp, "# perf version : %s\n", ph->env.version);
1142 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1148 nr = ph->env.nr_cmdline;
1149 str = ph->env.cmdline;
1151 fprintf(fp, "# cmdline : ");
1153 for (i = 0; i < nr; i++) {
1154 fprintf(fp, "%s ", str);
1155 str += strlen(str) + 1;
1160 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1166 nr = ph->env.nr_sibling_cores;
1167 str = ph->env.sibling_cores;
1169 for (i = 0; i < nr; i++) {
1170 fprintf(fp, "# sibling cores : %s\n", str);
1171 str += strlen(str) + 1;
1174 nr = ph->env.nr_sibling_threads;
1175 str = ph->env.sibling_threads;
1177 for (i = 0; i < nr; i++) {
1178 fprintf(fp, "# sibling threads : %s\n", str);
1179 str += strlen(str) + 1;
1183 static void free_event_desc(struct perf_evsel *events)
1185 struct perf_evsel *evsel;
1190 for (evsel = events; evsel->attr.size; evsel++) {
1200 static struct perf_evsel *
1201 read_event_desc(struct perf_header *ph, int fd)
1203 struct perf_evsel *evsel, *events = NULL;
1206 u32 nre, sz, nr, i, j;
1210 /* number of events */
1211 ret = read(fd, &nre, sizeof(nre));
1212 if (ret != (ssize_t)sizeof(nre))
1216 nre = bswap_32(nre);
1218 ret = read(fd, &sz, sizeof(sz));
1219 if (ret != (ssize_t)sizeof(sz))
1225 /* buffer to hold on file attr struct */
1230 /* the last event terminates with evsel->attr.size == 0: */
1231 events = calloc(nre + 1, sizeof(*events));
1235 msz = sizeof(evsel->attr);
1239 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1243 * must read entire on-file attr struct to
1244 * sync up with layout.
1246 ret = read(fd, buf, sz);
1247 if (ret != (ssize_t)sz)
1251 perf_event__attr_swap(buf);
1253 memcpy(&evsel->attr, buf, msz);
1255 ret = read(fd, &nr, sizeof(nr));
1256 if (ret != (ssize_t)sizeof(nr))
1259 if (ph->needs_swap) {
1261 evsel->needs_swap = true;
1264 evsel->name = do_read_string(fd, ph);
1269 id = calloc(nr, sizeof(*id));
1275 for (j = 0 ; j < nr; j++) {
1276 ret = read(fd, id, sizeof(*id));
1277 if (ret != (ssize_t)sizeof(*id))
1280 *id = bswap_64(*id);
1290 free_event_desc(events);
1295 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1297 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1302 fprintf(fp, "# event desc: not available or unable to read\n");
1306 for (evsel = events; evsel->attr.size; evsel++) {
1307 fprintf(fp, "# event : name = %s, ", evsel->name);
1309 fprintf(fp, "type = %d, config = 0x%"PRIx64
1310 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1312 (u64)evsel->attr.config,
1313 (u64)evsel->attr.config1,
1314 (u64)evsel->attr.config2);
1316 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1317 evsel->attr.exclude_user,
1318 evsel->attr.exclude_kernel);
1320 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1321 evsel->attr.exclude_host,
1322 evsel->attr.exclude_guest);
1324 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1327 fprintf(fp, ", id = {");
1328 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1331 fprintf(fp, " %"PRIu64, *id);
1339 free_event_desc(events);
1342 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1345 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1348 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1353 uint64_t mem_total, mem_free;
1356 nr = ph->env.nr_numa_nodes;
1357 str = ph->env.numa_nodes;
1359 for (i = 0; i < nr; i++) {
1361 c = strtoul(str, &tmp, 0);
1366 mem_total = strtoull(str, &tmp, 0);
1371 mem_free = strtoull(str, &tmp, 0);
1375 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1376 " free = %"PRIu64" kB\n",
1377 c, mem_total, mem_free);
1380 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1384 fprintf(fp, "# numa topology : not available\n");
1387 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1389 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1392 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1393 int fd __maybe_unused, FILE *fp)
1395 fprintf(fp, "# contains samples with branch stack\n");
1398 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1401 const char *delimiter = "# pmu mappings: ";
1406 pmu_num = ph->env.nr_pmu_mappings;
1408 fprintf(fp, "# pmu mappings: not available\n");
1412 str = ph->env.pmu_mappings;
1415 type = strtoul(str, &tmp, 0);
1420 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1423 str += strlen(str) + 1;
1432 fprintf(fp, "# pmu mappings: unable to read\n");
1435 static int __event_process_build_id(struct build_id_event *bev,
1437 struct perf_session *session)
1440 struct list_head *head;
1441 struct machine *machine;
1444 enum dso_kernel_type dso_type;
1446 machine = perf_session__findnew_machine(session, bev->pid);
1450 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1453 case PERF_RECORD_MISC_KERNEL:
1454 dso_type = DSO_TYPE_KERNEL;
1455 head = &machine->kernel_dsos;
1457 case PERF_RECORD_MISC_GUEST_KERNEL:
1458 dso_type = DSO_TYPE_GUEST_KERNEL;
1459 head = &machine->kernel_dsos;
1461 case PERF_RECORD_MISC_USER:
1462 case PERF_RECORD_MISC_GUEST_USER:
1463 dso_type = DSO_TYPE_USER;
1464 head = &machine->user_dsos;
1470 dso = __dsos__findnew(head, filename);
1472 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1474 dso__set_build_id(dso, &bev->build_id);
1476 if (filename[0] == '[')
1477 dso->kernel = dso_type;
1479 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1481 pr_debug("build id event received for %s: %s\n",
1482 dso->long_name, sbuild_id);
1490 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1491 int input, u64 offset, u64 size)
1493 struct perf_session *session = container_of(header, struct perf_session, header);
1495 struct perf_event_header header;
1496 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1499 struct build_id_event bev;
1500 char filename[PATH_MAX];
1501 u64 limit = offset + size;
1503 while (offset < limit) {
1506 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1509 if (header->needs_swap)
1510 perf_event_header__bswap(&old_bev.header);
1512 len = old_bev.header.size - sizeof(old_bev);
1513 if (read(input, filename, len) != len)
1516 bev.header = old_bev.header;
1519 * As the pid is the missing value, we need to fill
1520 * it properly. The header.misc value give us nice hint.
1522 bev.pid = HOST_KERNEL_ID;
1523 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1524 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1525 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1527 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1528 __event_process_build_id(&bev, filename, session);
1530 offset += bev.header.size;
1536 static int perf_header__read_build_ids(struct perf_header *header,
1537 int input, u64 offset, u64 size)
1539 struct perf_session *session = container_of(header, struct perf_session, header);
1540 struct build_id_event bev;
1541 char filename[PATH_MAX];
1542 u64 limit = offset + size, orig_offset = offset;
1545 while (offset < limit) {
1548 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1551 if (header->needs_swap)
1552 perf_event_header__bswap(&bev.header);
1554 len = bev.header.size - sizeof(bev);
1555 if (read(input, filename, len) != len)
1558 * The a1645ce1 changeset:
1560 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1562 * Added a field to struct build_id_event that broke the file
1565 * Since the kernel build-id is the first entry, process the
1566 * table using the old format if the well known
1567 * '[kernel.kallsyms]' string for the kernel build-id has the
1568 * first 4 characters chopped off (where the pid_t sits).
1570 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1571 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1573 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1576 __event_process_build_id(&bev, filename, session);
1578 offset += bev.header.size;
1585 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1586 struct perf_header *ph __maybe_unused,
1589 trace_report(fd, data, false);
1593 static int process_build_id(struct perf_file_section *section,
1594 struct perf_header *ph, int fd,
1595 void *data __maybe_unused)
1597 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1598 pr_debug("Failed to read buildids, continuing...\n");
1602 static int process_hostname(struct perf_file_section *section __maybe_unused,
1603 struct perf_header *ph, int fd,
1604 void *data __maybe_unused)
1606 ph->env.hostname = do_read_string(fd, ph);
1607 return ph->env.hostname ? 0 : -ENOMEM;
1610 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1611 struct perf_header *ph, int fd,
1612 void *data __maybe_unused)
1614 ph->env.os_release = do_read_string(fd, ph);
1615 return ph->env.os_release ? 0 : -ENOMEM;
1618 static int process_version(struct perf_file_section *section __maybe_unused,
1619 struct perf_header *ph, int fd,
1620 void *data __maybe_unused)
1622 ph->env.version = do_read_string(fd, ph);
1623 return ph->env.version ? 0 : -ENOMEM;
1626 static int process_arch(struct perf_file_section *section __maybe_unused,
1627 struct perf_header *ph, int fd,
1628 void *data __maybe_unused)
1630 ph->env.arch = do_read_string(fd, ph);
1631 return ph->env.arch ? 0 : -ENOMEM;
1634 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1635 struct perf_header *ph, int fd,
1636 void *data __maybe_unused)
1641 ret = read(fd, &nr, sizeof(nr));
1642 if (ret != sizeof(nr))
1648 ph->env.nr_cpus_online = nr;
1650 ret = read(fd, &nr, sizeof(nr));
1651 if (ret != sizeof(nr))
1657 ph->env.nr_cpus_avail = nr;
1661 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1662 struct perf_header *ph, int fd,
1663 void *data __maybe_unused)
1665 ph->env.cpu_desc = do_read_string(fd, ph);
1666 return ph->env.cpu_desc ? 0 : -ENOMEM;
1669 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1670 struct perf_header *ph, int fd,
1671 void *data __maybe_unused)
1673 ph->env.cpuid = do_read_string(fd, ph);
1674 return ph->env.cpuid ? 0 : -ENOMEM;
1677 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1678 struct perf_header *ph, int fd,
1679 void *data __maybe_unused)
1684 ret = read(fd, &mem, sizeof(mem));
1685 if (ret != sizeof(mem))
1689 mem = bswap_64(mem);
1691 ph->env.total_mem = mem;
1695 static struct perf_evsel *
1696 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1698 struct perf_evsel *evsel;
1700 list_for_each_entry(evsel, &evlist->entries, node) {
1701 if (evsel->idx == idx)
1709 perf_evlist__set_event_name(struct perf_evlist *evlist,
1710 struct perf_evsel *event)
1712 struct perf_evsel *evsel;
1717 evsel = perf_evlist__find_by_index(evlist, event->idx);
1724 evsel->name = strdup(event->name);
1728 process_event_desc(struct perf_file_section *section __maybe_unused,
1729 struct perf_header *header, int fd,
1730 void *data __maybe_unused)
1732 struct perf_session *session;
1733 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1738 session = container_of(header, struct perf_session, header);
1739 for (evsel = events; evsel->attr.size; evsel++)
1740 perf_evlist__set_event_name(session->evlist, evsel);
1742 free_event_desc(events);
1747 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1748 struct perf_header *ph, int fd,
1749 void *data __maybe_unused)
1756 ret = read(fd, &nr, sizeof(nr));
1757 if (ret != sizeof(nr))
1763 ph->env.nr_cmdline = nr;
1764 strbuf_init(&sb, 128);
1766 for (i = 0; i < nr; i++) {
1767 str = do_read_string(fd, ph);
1771 /* include a NULL character at the end */
1772 strbuf_add(&sb, str, strlen(str) + 1);
1775 ph->env.cmdline = strbuf_detach(&sb, NULL);
1779 strbuf_release(&sb);
1783 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1784 struct perf_header *ph, int fd,
1785 void *data __maybe_unused)
1792 ret = read(fd, &nr, sizeof(nr));
1793 if (ret != sizeof(nr))
1799 ph->env.nr_sibling_cores = nr;
1800 strbuf_init(&sb, 128);
1802 for (i = 0; i < nr; i++) {
1803 str = do_read_string(fd, ph);
1807 /* include a NULL character at the end */
1808 strbuf_add(&sb, str, strlen(str) + 1);
1811 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1813 ret = read(fd, &nr, sizeof(nr));
1814 if (ret != sizeof(nr))
1820 ph->env.nr_sibling_threads = nr;
1822 for (i = 0; i < nr; i++) {
1823 str = do_read_string(fd, ph);
1827 /* include a NULL character at the end */
1828 strbuf_add(&sb, str, strlen(str) + 1);
1831 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1835 strbuf_release(&sb);
1839 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1840 struct perf_header *ph, int fd,
1841 void *data __maybe_unused)
1846 uint64_t mem_total, mem_free;
1850 ret = read(fd, &nr, sizeof(nr));
1851 if (ret != sizeof(nr))
1857 ph->env.nr_numa_nodes = nr;
1858 strbuf_init(&sb, 256);
1860 for (i = 0; i < nr; i++) {
1862 ret = read(fd, &node, sizeof(node));
1863 if (ret != sizeof(node))
1866 ret = read(fd, &mem_total, sizeof(u64));
1867 if (ret != sizeof(u64))
1870 ret = read(fd, &mem_free, sizeof(u64));
1871 if (ret != sizeof(u64))
1874 if (ph->needs_swap) {
1875 node = bswap_32(node);
1876 mem_total = bswap_64(mem_total);
1877 mem_free = bswap_64(mem_free);
1880 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1881 node, mem_total, mem_free);
1883 str = do_read_string(fd, ph);
1887 /* include a NULL character at the end */
1888 strbuf_add(&sb, str, strlen(str) + 1);
1891 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1895 strbuf_release(&sb);
1899 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1900 struct perf_header *ph, int fd,
1901 void *data __maybe_unused)
1909 ret = read(fd, &pmu_num, sizeof(pmu_num));
1910 if (ret != sizeof(pmu_num))
1914 pmu_num = bswap_32(pmu_num);
1917 pr_debug("pmu mappings not available\n");
1921 ph->env.nr_pmu_mappings = pmu_num;
1922 strbuf_init(&sb, 128);
1925 if (read(fd, &type, sizeof(type)) != sizeof(type))
1928 type = bswap_32(type);
1930 name = do_read_string(fd, ph);
1934 strbuf_addf(&sb, "%u:%s", type, name);
1935 /* include a NULL character at the end */
1936 strbuf_add(&sb, "", 1);
1941 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1945 strbuf_release(&sb);
1949 struct feature_ops {
1950 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1951 void (*print)(struct perf_header *h, int fd, FILE *fp);
1952 int (*process)(struct perf_file_section *section,
1953 struct perf_header *h, int fd, void *data);
1958 #define FEAT_OPA(n, func) \
1959 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1960 #define FEAT_OPP(n, func) \
1961 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1962 .process = process_##func }
1963 #define FEAT_OPF(n, func) \
1964 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1965 .process = process_##func, .full_only = true }
1967 /* feature_ops not implemented: */
1968 #define print_tracing_data NULL
1969 #define print_build_id NULL
1971 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1972 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1973 FEAT_OPP(HEADER_BUILD_ID, build_id),
1974 FEAT_OPP(HEADER_HOSTNAME, hostname),
1975 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1976 FEAT_OPP(HEADER_VERSION, version),
1977 FEAT_OPP(HEADER_ARCH, arch),
1978 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1979 FEAT_OPP(HEADER_CPUDESC, cpudesc),
1980 FEAT_OPP(HEADER_CPUID, cpuid),
1981 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
1982 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
1983 FEAT_OPP(HEADER_CMDLINE, cmdline),
1984 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1985 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1986 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
1987 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
1990 struct header_print_data {
1992 bool full; /* extended list of headers */
1995 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1996 struct perf_header *ph,
1997 int feat, int fd, void *data)
1999 struct header_print_data *hd = data;
2001 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2002 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2003 "%d, continuing...\n", section->offset, feat);
2006 if (feat >= HEADER_LAST_FEATURE) {
2007 pr_warning("unknown feature %d\n", feat);
2010 if (!feat_ops[feat].print)
2013 if (!feat_ops[feat].full_only || hd->full)
2014 feat_ops[feat].print(ph, fd, hd->fp);
2016 fprintf(hd->fp, "# %s info available, use -I to display\n",
2017 feat_ops[feat].name);
2022 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2024 struct header_print_data hd;
2025 struct perf_header *header = &session->header;
2026 int fd = session->fd;
2030 perf_header__process_sections(header, fd, &hd,
2031 perf_file_section__fprintf_info);
2035 static int do_write_feat(int fd, struct perf_header *h, int type,
2036 struct perf_file_section **p,
2037 struct perf_evlist *evlist)
2042 if (perf_header__has_feat(h, type)) {
2043 if (!feat_ops[type].write)
2046 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2048 err = feat_ops[type].write(fd, h, evlist);
2050 pr_debug("failed to write feature %d\n", type);
2052 /* undo anything written */
2053 lseek(fd, (*p)->offset, SEEK_SET);
2057 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2063 static int perf_header__adds_write(struct perf_header *header,
2064 struct perf_evlist *evlist, int fd)
2067 struct perf_file_section *feat_sec, *p;
2073 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2077 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
2078 if (feat_sec == NULL)
2081 sec_size = sizeof(*feat_sec) * nr_sections;
2083 sec_start = header->data_offset + header->data_size;
2084 lseek(fd, sec_start + sec_size, SEEK_SET);
2086 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2087 if (do_write_feat(fd, header, feat, &p, evlist))
2088 perf_header__clear_feat(header, feat);
2091 lseek(fd, sec_start, SEEK_SET);
2093 * may write more than needed due to dropped feature, but
2094 * this is okay, reader will skip the mising entries
2096 err = do_write(fd, feat_sec, sec_size);
2098 pr_debug("failed to write feature section\n");
2103 int perf_header__write_pipe(int fd)
2105 struct perf_pipe_file_header f_header;
2108 f_header = (struct perf_pipe_file_header){
2109 .magic = PERF_MAGIC,
2110 .size = sizeof(f_header),
2113 err = do_write(fd, &f_header, sizeof(f_header));
2115 pr_debug("failed to write perf pipe header\n");
2122 int perf_session__write_header(struct perf_session *session,
2123 struct perf_evlist *evlist,
2124 int fd, bool at_exit)
2126 struct perf_file_header f_header;
2127 struct perf_file_attr f_attr;
2128 struct perf_header *header = &session->header;
2129 struct perf_evsel *evsel, *pair = NULL;
2132 lseek(fd, sizeof(f_header), SEEK_SET);
2134 if (session->evlist != evlist)
2135 pair = perf_evlist__first(session->evlist);
2137 list_for_each_entry(evsel, &evlist->entries, node) {
2138 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2139 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2142 pr_debug("failed to write perf header\n");
2145 if (session->evlist != evlist) {
2146 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
2149 evsel->ids += pair->ids;
2150 pair = perf_evsel__next(pair);
2154 header->attr_offset = lseek(fd, 0, SEEK_CUR);
2156 list_for_each_entry(evsel, &evlist->entries, node) {
2157 f_attr = (struct perf_file_attr){
2158 .attr = evsel->attr,
2160 .offset = evsel->id_offset,
2161 .size = evsel->ids * sizeof(u64),
2164 err = do_write(fd, &f_attr, sizeof(f_attr));
2166 pr_debug("failed to write perf header attribute\n");
2171 header->event_offset = lseek(fd, 0, SEEK_CUR);
2172 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
2174 err = do_write(fd, trace_events, header->event_size);
2176 pr_debug("failed to write perf header events\n");
2181 header->data_offset = lseek(fd, 0, SEEK_CUR);
2184 err = perf_header__adds_write(header, evlist, fd);
2189 f_header = (struct perf_file_header){
2190 .magic = PERF_MAGIC,
2191 .size = sizeof(f_header),
2192 .attr_size = sizeof(f_attr),
2194 .offset = header->attr_offset,
2195 .size = evlist->nr_entries * sizeof(f_attr),
2198 .offset = header->data_offset,
2199 .size = header->data_size,
2202 .offset = header->event_offset,
2203 .size = header->event_size,
2207 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2209 lseek(fd, 0, SEEK_SET);
2210 err = do_write(fd, &f_header, sizeof(f_header));
2212 pr_debug("failed to write perf header\n");
2215 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2221 static int perf_header__getbuffer64(struct perf_header *header,
2222 int fd, void *buf, size_t size)
2224 if (readn(fd, buf, size) <= 0)
2227 if (header->needs_swap)
2228 mem_bswap_64(buf, size);
2233 int perf_header__process_sections(struct perf_header *header, int fd,
2235 int (*process)(struct perf_file_section *section,
2236 struct perf_header *ph,
2237 int feat, int fd, void *data))
2239 struct perf_file_section *feat_sec, *sec;
2245 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2249 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
2253 sec_size = sizeof(*feat_sec) * nr_sections;
2255 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2257 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2261 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2262 err = process(sec++, header, feat, fd, data);
2272 static const int attr_file_abi_sizes[] = {
2273 [0] = PERF_ATTR_SIZE_VER0,
2274 [1] = PERF_ATTR_SIZE_VER1,
2275 [2] = PERF_ATTR_SIZE_VER2,
2276 [3] = PERF_ATTR_SIZE_VER3,
2281 * In the legacy file format, the magic number is not used to encode endianness.
2282 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2283 * on ABI revisions, we need to try all combinations for all endianness to
2284 * detect the endianness.
2286 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2288 uint64_t ref_size, attr_size;
2291 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2292 ref_size = attr_file_abi_sizes[i]
2293 + sizeof(struct perf_file_section);
2294 if (hdr_sz != ref_size) {
2295 attr_size = bswap_64(hdr_sz);
2296 if (attr_size != ref_size)
2299 ph->needs_swap = true;
2301 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2306 /* could not determine endianness */
2310 #define PERF_PIPE_HDR_VER0 16
2312 static const size_t attr_pipe_abi_sizes[] = {
2313 [0] = PERF_PIPE_HDR_VER0,
2318 * In the legacy pipe format, there is an implicit assumption that endiannesss
2319 * between host recording the samples, and host parsing the samples is the
2320 * same. This is not always the case given that the pipe output may always be
2321 * redirected into a file and analyzed on a different machine with possibly a
2322 * different endianness and perf_event ABI revsions in the perf tool itself.
2324 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2329 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2330 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2331 attr_size = bswap_64(hdr_sz);
2332 if (attr_size != hdr_sz)
2335 ph->needs_swap = true;
2337 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2343 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2344 bool is_pipe, struct perf_header *ph)
2348 /* check for legacy format */
2349 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2351 pr_debug("legacy perf.data format\n");
2353 return try_all_pipe_abis(hdr_sz, ph);
2355 return try_all_file_abis(hdr_sz, ph);
2358 * the new magic number serves two purposes:
2359 * - unique number to identify actual perf.data files
2360 * - encode endianness of file
2363 /* check magic number with one endianness */
2364 if (magic == __perf_magic2)
2367 /* check magic number with opposite endianness */
2368 if (magic != __perf_magic2_sw)
2371 ph->needs_swap = true;
2376 int perf_file_header__read(struct perf_file_header *header,
2377 struct perf_header *ph, int fd)
2381 lseek(fd, 0, SEEK_SET);
2383 ret = readn(fd, header, sizeof(*header));
2387 if (check_magic_endian(header->magic,
2388 header->attr_size, false, ph) < 0) {
2389 pr_debug("magic/endian check failed\n");
2393 if (ph->needs_swap) {
2394 mem_bswap_64(header, offsetof(struct perf_file_header,
2398 if (header->size != sizeof(*header)) {
2399 /* Support the previous format */
2400 if (header->size == offsetof(typeof(*header), adds_features))
2401 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2404 } else if (ph->needs_swap) {
2406 * feature bitmap is declared as an array of unsigned longs --
2407 * not good since its size can differ between the host that
2408 * generated the data file and the host analyzing the file.
2410 * We need to handle endianness, but we don't know the size of
2411 * the unsigned long where the file was generated. Take a best
2412 * guess at determining it: try 64-bit swap first (ie., file
2413 * created on a 64-bit host), and check if the hostname feature
2414 * bit is set (this feature bit is forced on as of fbe96f2).
2415 * If the bit is not, undo the 64-bit swap and try a 32-bit
2416 * swap. If the hostname bit is still not set (e.g., older data
2417 * file), punt and fallback to the original behavior --
2418 * clearing all feature bits and setting buildid.
2420 mem_bswap_64(&header->adds_features,
2421 BITS_TO_U64(HEADER_FEAT_BITS));
2423 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2425 mem_bswap_64(&header->adds_features,
2426 BITS_TO_U64(HEADER_FEAT_BITS));
2429 mem_bswap_32(&header->adds_features,
2430 BITS_TO_U32(HEADER_FEAT_BITS));
2433 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2434 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2435 set_bit(HEADER_BUILD_ID, header->adds_features);
2439 memcpy(&ph->adds_features, &header->adds_features,
2440 sizeof(ph->adds_features));
2442 ph->event_offset = header->event_types.offset;
2443 ph->event_size = header->event_types.size;
2444 ph->data_offset = header->data.offset;
2445 ph->data_size = header->data.size;
2449 static int perf_file_section__process(struct perf_file_section *section,
2450 struct perf_header *ph,
2451 int feat, int fd, void *data)
2453 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2454 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2455 "%d, continuing...\n", section->offset, feat);
2459 if (feat >= HEADER_LAST_FEATURE) {
2460 pr_debug("unknown feature %d, continuing...\n", feat);
2464 if (!feat_ops[feat].process)
2467 return feat_ops[feat].process(section, ph, fd, data);
2470 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2471 struct perf_header *ph, int fd,
2476 ret = readn(fd, header, sizeof(*header));
2480 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2481 pr_debug("endian/magic failed\n");
2486 header->size = bswap_64(header->size);
2488 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2494 static int perf_header__read_pipe(struct perf_session *session, int fd)
2496 struct perf_header *header = &session->header;
2497 struct perf_pipe_file_header f_header;
2499 if (perf_file_header__read_pipe(&f_header, header, fd,
2500 session->repipe) < 0) {
2501 pr_debug("incompatible file format\n");
2510 static int read_attr(int fd, struct perf_header *ph,
2511 struct perf_file_attr *f_attr)
2513 struct perf_event_attr *attr = &f_attr->attr;
2515 size_t our_sz = sizeof(f_attr->attr);
2518 memset(f_attr, 0, sizeof(*f_attr));
2520 /* read minimal guaranteed structure */
2521 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2523 pr_debug("cannot read %d bytes of header attr\n",
2524 PERF_ATTR_SIZE_VER0);
2528 /* on file perf_event_attr size */
2536 sz = PERF_ATTR_SIZE_VER0;
2537 } else if (sz > our_sz) {
2538 pr_debug("file uses a more recent and unsupported ABI"
2539 " (%zu bytes extra)\n", sz - our_sz);
2542 /* what we have not yet read and that we know about */
2543 left = sz - PERF_ATTR_SIZE_VER0;
2546 ptr += PERF_ATTR_SIZE_VER0;
2548 ret = readn(fd, ptr, left);
2550 /* read perf_file_section, ids are read in caller */
2551 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2553 return ret <= 0 ? -1 : 0;
2556 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2557 struct pevent *pevent)
2559 struct event_format *event;
2562 /* already prepared */
2563 if (evsel->tp_format)
2566 event = pevent_find_event(pevent, evsel->attr.config);
2571 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2572 evsel->name = strdup(bf);
2573 if (evsel->name == NULL)
2577 evsel->tp_format = event;
2581 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2582 struct pevent *pevent)
2584 struct perf_evsel *pos;
2586 list_for_each_entry(pos, &evlist->entries, node) {
2587 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2588 perf_evsel__prepare_tracepoint_event(pos, pevent))
2595 int perf_session__read_header(struct perf_session *session, int fd)
2597 struct perf_header *header = &session->header;
2598 struct perf_file_header f_header;
2599 struct perf_file_attr f_attr;
2601 int nr_attrs, nr_ids, i, j;
2603 session->evlist = perf_evlist__new(NULL, NULL);
2604 if (session->evlist == NULL)
2607 if (session->fd_pipe)
2608 return perf_header__read_pipe(session, fd);
2610 if (perf_file_header__read(&f_header, header, fd) < 0)
2613 nr_attrs = f_header.attrs.size / f_header.attr_size;
2614 lseek(fd, f_header.attrs.offset, SEEK_SET);
2616 for (i = 0; i < nr_attrs; i++) {
2617 struct perf_evsel *evsel;
2620 if (read_attr(fd, header, &f_attr) < 0)
2623 if (header->needs_swap)
2624 perf_event__attr_swap(&f_attr.attr);
2626 tmp = lseek(fd, 0, SEEK_CUR);
2627 evsel = perf_evsel__new(&f_attr.attr, i);
2630 goto out_delete_evlist;
2632 evsel->needs_swap = header->needs_swap;
2634 * Do it before so that if perf_evsel__alloc_id fails, this
2635 * entry gets purged too at perf_evlist__delete().
2637 perf_evlist__add(session->evlist, evsel);
2639 nr_ids = f_attr.ids.size / sizeof(u64);
2641 * We don't have the cpu and thread maps on the header, so
2642 * for allocating the perf_sample_id table we fake 1 cpu and
2643 * hattr->ids threads.
2645 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2646 goto out_delete_evlist;
2648 lseek(fd, f_attr.ids.offset, SEEK_SET);
2650 for (j = 0; j < nr_ids; j++) {
2651 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2654 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2657 lseek(fd, tmp, SEEK_SET);
2660 symbol_conf.nr_events = nr_attrs;
2662 if (f_header.event_types.size) {
2663 lseek(fd, f_header.event_types.offset, SEEK_SET);
2664 trace_events = malloc(f_header.event_types.size);
2665 if (trace_events == NULL)
2667 if (perf_header__getbuffer64(header, fd, trace_events,
2668 f_header.event_types.size))
2670 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2673 perf_header__process_sections(header, fd, &session->pevent,
2674 perf_file_section__process);
2676 lseek(fd, header->data_offset, SEEK_SET);
2678 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2680 goto out_delete_evlist;
2688 perf_evlist__delete(session->evlist);
2689 session->evlist = NULL;
2693 int perf_event__synthesize_attr(struct perf_tool *tool,
2694 struct perf_event_attr *attr, u32 ids, u64 *id,
2695 perf_event__handler_t process)
2697 union perf_event *ev;
2701 size = sizeof(struct perf_event_attr);
2702 size = PERF_ALIGN(size, sizeof(u64));
2703 size += sizeof(struct perf_event_header);
2704 size += ids * sizeof(u64);
2711 ev->attr.attr = *attr;
2712 memcpy(ev->attr.id, id, ids * sizeof(u64));
2714 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2715 ev->attr.header.size = (u16)size;
2717 if (ev->attr.header.size == size)
2718 err = process(tool, ev, NULL, NULL);
2727 int perf_event__synthesize_attrs(struct perf_tool *tool,
2728 struct perf_session *session,
2729 perf_event__handler_t process)
2731 struct perf_evsel *evsel;
2734 list_for_each_entry(evsel, &session->evlist->entries, node) {
2735 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2736 evsel->id, process);
2738 pr_debug("failed to create perf header attribute\n");
2746 int perf_event__process_attr(union perf_event *event,
2747 struct perf_evlist **pevlist)
2750 struct perf_evsel *evsel;
2751 struct perf_evlist *evlist = *pevlist;
2753 if (evlist == NULL) {
2754 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2759 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2763 perf_evlist__add(evlist, evsel);
2765 ids = event->header.size;
2766 ids -= (void *)&event->attr.id - (void *)event;
2767 n_ids = ids / sizeof(u64);
2769 * We don't have the cpu and thread maps on the header, so
2770 * for allocating the perf_sample_id table we fake 1 cpu and
2771 * hattr->ids threads.
2773 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2776 for (i = 0; i < n_ids; i++) {
2777 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2783 int perf_event__synthesize_event_type(struct perf_tool *tool,
2784 u64 event_id, char *name,
2785 perf_event__handler_t process,
2786 struct machine *machine)
2788 union perf_event ev;
2792 memset(&ev, 0, sizeof(ev));
2794 ev.event_type.event_type.event_id = event_id;
2795 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2796 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2798 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2799 size = strlen(ev.event_type.event_type.name);
2800 size = PERF_ALIGN(size, sizeof(u64));
2801 ev.event_type.header.size = sizeof(ev.event_type) -
2802 (sizeof(ev.event_type.event_type.name) - size);
2804 err = process(tool, &ev, NULL, machine);
2809 int perf_event__synthesize_event_types(struct perf_tool *tool,
2810 perf_event__handler_t process,
2811 struct machine *machine)
2813 struct perf_trace_event_type *type;
2816 for (i = 0; i < trace_event_count; i++) {
2817 type = &trace_events[i];
2819 err = perf_event__synthesize_event_type(tool, type->event_id,
2820 type->name, process,
2823 pr_debug("failed to create perf header event type\n");
2831 int perf_event__process_event_type(struct perf_tool *tool __maybe_unused,
2832 union perf_event *event)
2834 if (perf_header__push_event(event->event_type.event_type.event_id,
2835 event->event_type.event_type.name) < 0)
2841 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2842 struct perf_evlist *evlist,
2843 perf_event__handler_t process)
2845 union perf_event ev;
2846 struct tracing_data *tdata;
2847 ssize_t size = 0, aligned_size = 0, padding;
2848 int err __maybe_unused = 0;
2851 * We are going to store the size of the data followed
2852 * by the data contents. Since the fd descriptor is a pipe,
2853 * we cannot seek back to store the size of the data once
2854 * we know it. Instead we:
2856 * - write the tracing data to the temp file
2857 * - get/write the data size to pipe
2858 * - write the tracing data from the temp file
2861 tdata = tracing_data_get(&evlist->entries, fd, true);
2865 memset(&ev, 0, sizeof(ev));
2867 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2869 aligned_size = PERF_ALIGN(size, sizeof(u64));
2870 padding = aligned_size - size;
2871 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2872 ev.tracing_data.size = aligned_size;
2874 process(tool, &ev, NULL, NULL);
2877 * The put function will copy all the tracing data
2878 * stored in temp file to the pipe.
2880 tracing_data_put(tdata);
2882 write_padded(fd, NULL, 0, padding);
2884 return aligned_size;
2887 int perf_event__process_tracing_data(union perf_event *event,
2888 struct perf_session *session)
2890 ssize_t size_read, padding, size = event->tracing_data.size;
2891 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2894 /* setup for reading amidst mmap */
2895 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2898 size_read = trace_report(session->fd, &session->pevent,
2900 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2902 if (read(session->fd, buf, padding) < 0)
2903 die("reading input file");
2904 if (session->repipe) {
2905 int retw = write(STDOUT_FILENO, buf, padding);
2906 if (retw <= 0 || retw != padding)
2907 die("repiping tracing data padding");
2910 if (size_read + padding != size)
2911 die("tracing data size mismatch");
2913 perf_evlist__prepare_tracepoint_events(session->evlist,
2916 return size_read + padding;
2919 int perf_event__synthesize_build_id(struct perf_tool *tool,
2920 struct dso *pos, u16 misc,
2921 perf_event__handler_t process,
2922 struct machine *machine)
2924 union perf_event ev;
2931 memset(&ev, 0, sizeof(ev));
2933 len = pos->long_name_len + 1;
2934 len = PERF_ALIGN(len, NAME_ALIGN);
2935 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2936 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2937 ev.build_id.header.misc = misc;
2938 ev.build_id.pid = machine->pid;
2939 ev.build_id.header.size = sizeof(ev.build_id) + len;
2940 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2942 err = process(tool, &ev, NULL, machine);
2947 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2948 union perf_event *event,
2949 struct perf_session *session)
2951 __event_process_build_id(&event->build_id,
2952 event->build_id.filename,
2957 void disable_buildid_cache(void)
2959 no_buildid_cache = true;