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 u32 header_argc;
28 static const char **header_argv;
32 * must be a numerical value to let the endianness
33 * determine the memory layout. That way we are able
34 * to detect endianness when reading the perf.data file
37 * we check for legacy (PERFFILE) format.
39 static const char *__perf_magic1 = "PERFFILE";
40 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
41 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
43 #define PERF_MAGIC __perf_magic2
45 struct perf_file_attr {
46 struct perf_event_attr attr;
47 struct perf_file_section ids;
50 void perf_header__set_feat(struct perf_header *header, int feat)
52 set_bit(feat, header->adds_features);
55 void perf_header__clear_feat(struct perf_header *header, int feat)
57 clear_bit(feat, header->adds_features);
60 bool perf_header__has_feat(const struct perf_header *header, int feat)
62 return test_bit(feat, header->adds_features);
65 static int do_write(int fd, const void *buf, size_t size)
68 int ret = write(fd, buf, size);
80 int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
82 static const char zero_buf[NAME_ALIGN];
83 int err = do_write(fd, bf, count);
86 err = do_write(fd, zero_buf, count_aligned - count);
91 #define string_size(str) \
92 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
94 static int do_write_string(int fd, const char *str)
99 olen = strlen(str) + 1;
100 len = PERF_ALIGN(olen, NAME_ALIGN);
102 /* write len, incl. \0 */
103 ret = do_write(fd, &len, sizeof(len));
107 return write_padded(fd, str, olen, len);
110 static char *do_read_string(int fd, struct perf_header *ph)
116 sz = readn(fd, &len, sizeof(len));
117 if (sz < (ssize_t)sizeof(len))
127 ret = readn(fd, buf, len);
128 if (ret == (ssize_t)len) {
130 * strings are padded by zeroes
131 * thus the actual strlen of buf
132 * may be less than len
142 perf_header__set_cmdline(int argc, const char **argv)
147 * If header_argv has already been set, do not override it.
148 * This allows a command to set the cmdline, parse args and
149 * then call another builtin function that implements a
150 * command -- e.g, cmd_kvm calling cmd_record.
155 header_argc = (u32)argc;
157 /* do not include NULL termination */
158 header_argv = calloc(argc, sizeof(char *));
163 * must copy argv contents because it gets moved
164 * around during option parsing
166 for (i = 0; i < argc ; i++)
167 header_argv[i] = argv[i];
172 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
173 struct perf_evlist *evlist)
175 return read_tracing_data(fd, &evlist->entries);
179 static int write_build_id(int fd, struct perf_header *h,
180 struct perf_evlist *evlist __maybe_unused)
182 struct perf_session *session;
185 session = container_of(h, struct perf_session, header);
187 if (!perf_session__read_build_ids(session, true))
190 err = perf_session__write_buildid_table(session, fd);
192 pr_debug("failed to write buildid table\n");
195 perf_session__cache_build_ids(session);
200 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
201 struct perf_evlist *evlist __maybe_unused)
210 return do_write_string(fd, uts.nodename);
213 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
214 struct perf_evlist *evlist __maybe_unused)
223 return do_write_string(fd, uts.release);
226 static int write_arch(int fd, struct perf_header *h __maybe_unused,
227 struct perf_evlist *evlist __maybe_unused)
236 return do_write_string(fd, uts.machine);
239 static int write_version(int fd, struct perf_header *h __maybe_unused,
240 struct perf_evlist *evlist __maybe_unused)
242 return do_write_string(fd, perf_version_string);
245 static int __write_cpudesc(int fd, const char *cpuinfo_proc)
250 const char *search = cpuinfo_proc;
257 file = fopen("/proc/cpuinfo", "r");
261 while (getline(&buf, &len, file) > 0) {
262 ret = strncmp(buf, search, strlen(search));
274 p = strchr(buf, ':');
275 if (p && *(p+1) == ' ' && *(p+2))
281 /* squash extra space characters (branding string) */
288 while (*q && isspace(*q))
291 while ((*r++ = *q++));
295 ret = do_write_string(fd, s);
302 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
303 struct perf_evlist *evlist __maybe_unused)
306 #define CPUINFO_PROC {"model name", }
308 const char *cpuinfo_procs[] = CPUINFO_PROC;
311 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
313 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
321 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
322 struct perf_evlist *evlist __maybe_unused)
328 nr = sysconf(_SC_NPROCESSORS_CONF);
332 nrc = (u32)(nr & UINT_MAX);
334 nr = sysconf(_SC_NPROCESSORS_ONLN);
338 nra = (u32)(nr & UINT_MAX);
340 ret = do_write(fd, &nrc, sizeof(nrc));
344 return do_write(fd, &nra, sizeof(nra));
347 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
348 struct perf_evlist *evlist)
350 struct perf_evsel *evsel;
354 nre = evlist->nr_entries;
357 * write number of events
359 ret = do_write(fd, &nre, sizeof(nre));
364 * size of perf_event_attr struct
366 sz = (u32)sizeof(evsel->attr);
367 ret = do_write(fd, &sz, sizeof(sz));
371 evlist__for_each(evlist, evsel) {
372 ret = do_write(fd, &evsel->attr, sz);
376 * write number of unique id per event
377 * there is one id per instance of an event
379 * copy into an nri to be independent of the
383 ret = do_write(fd, &nri, sizeof(nri));
388 * write event string as passed on cmdline
390 ret = do_write_string(fd, perf_evsel__name(evsel));
394 * write unique ids for this event
396 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
403 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
404 struct perf_evlist *evlist __maybe_unused)
406 char buf[MAXPATHLEN];
412 * actual atual path to perf binary
414 sprintf(proc, "/proc/%d/exe", getpid());
415 ret = readlink(proc, buf, sizeof(buf));
419 /* readlink() does not add null termination */
422 /* account for binary path */
425 ret = do_write(fd, &n, sizeof(n));
429 ret = do_write_string(fd, buf);
433 for (i = 0 ; i < header_argc; i++) {
434 ret = do_write_string(fd, header_argv[i]);
441 #define CORE_SIB_FMT \
442 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
443 #define THRD_SIB_FMT \
444 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
450 char **core_siblings;
451 char **thread_siblings;
456 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
459 char filename[MAXPATHLEN];
460 char *buf = NULL, *p;
466 sprintf(filename, CORE_SIB_FMT, cpu);
467 fp = fopen(filename, "r");
471 sret = getline(&buf, &len, fp);
476 p = strchr(buf, '\n');
480 for (i = 0; i < tp->core_sib; i++) {
481 if (!strcmp(buf, tp->core_siblings[i]))
484 if (i == tp->core_sib) {
485 tp->core_siblings[i] = buf;
493 sprintf(filename, THRD_SIB_FMT, cpu);
494 fp = fopen(filename, "r");
498 if (getline(&buf, &len, fp) <= 0)
501 p = strchr(buf, '\n');
505 for (i = 0; i < tp->thread_sib; i++) {
506 if (!strcmp(buf, tp->thread_siblings[i]))
509 if (i == tp->thread_sib) {
510 tp->thread_siblings[i] = buf;
516 tp->core_id[cpu] = cpu_map__get_core_id(cpu);
517 tp->phy_pkg_id[cpu] = cpu_map__get_socket_id(cpu);
525 static void free_cpu_topo(struct cpu_topo *tp)
532 for (i = 0 ; i < tp->core_sib; i++)
533 zfree(&tp->core_siblings[i]);
535 for (i = 0 ; i < tp->thread_sib; i++)
536 zfree(&tp->thread_siblings[i]);
541 static struct cpu_topo *build_cpu_topology(void)
550 ncpus = sysconf(_SC_NPROCESSORS_CONF);
554 nr = (u32)(ncpus & UINT_MAX);
556 sz = nr * sizeof(char *);
557 sz_id = nr * sizeof(int);
559 addr = calloc(1, sizeof(*tp) + 2 * sz + 2 * sz_id);
566 tp->core_siblings = addr;
568 tp->thread_siblings = addr;
572 tp->phy_pkg_id = addr;
574 for (i = 0; i < nr; i++) {
575 ret = build_cpu_topo(tp, i);
586 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
587 struct perf_evlist *evlist __maybe_unused)
593 tp = build_cpu_topology();
597 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
601 for (i = 0; i < tp->core_sib; i++) {
602 ret = do_write_string(fd, tp->core_siblings[i]);
606 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
610 for (i = 0; i < tp->thread_sib; i++) {
611 ret = do_write_string(fd, tp->thread_siblings[i]);
616 for (i = 0; i < tp->cpu_nr; i++) {
617 ret = do_write(fd, &tp->core_id[i], sizeof(int));
620 ret = do_write(fd, &tp->phy_pkg_id[i], sizeof(int));
631 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
632 struct perf_evlist *evlist __maybe_unused)
640 fp = fopen("/proc/meminfo", "r");
644 while (getline(&buf, &len, fp) > 0) {
645 ret = strncmp(buf, "MemTotal:", 9);
650 n = sscanf(buf, "%*s %"PRIu64, &mem);
652 ret = do_write(fd, &mem, sizeof(mem));
660 static int write_topo_node(int fd, int node)
662 char str[MAXPATHLEN];
664 char *buf = NULL, *p;
667 u64 mem_total, mem_free, mem;
670 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
671 fp = fopen(str, "r");
675 while (getline(&buf, &len, fp) > 0) {
676 /* skip over invalid lines */
677 if (!strchr(buf, ':'))
679 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
681 if (!strcmp(field, "MemTotal:"))
683 if (!strcmp(field, "MemFree:"))
690 ret = do_write(fd, &mem_total, sizeof(u64));
694 ret = do_write(fd, &mem_free, sizeof(u64));
699 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
701 fp = fopen(str, "r");
705 if (getline(&buf, &len, fp) <= 0)
708 p = strchr(buf, '\n');
712 ret = do_write_string(fd, buf);
720 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
721 struct perf_evlist *evlist __maybe_unused)
726 struct cpu_map *node_map = NULL;
731 fp = fopen("/sys/devices/system/node/online", "r");
735 if (getline(&buf, &len, fp) <= 0)
738 c = strchr(buf, '\n');
742 node_map = cpu_map__new(buf);
746 nr = (u32)node_map->nr;
748 ret = do_write(fd, &nr, sizeof(nr));
752 for (i = 0; i < nr; i++) {
753 j = (u32)node_map->map[i];
754 ret = do_write(fd, &j, sizeof(j));
758 ret = write_topo_node(fd, i);
772 * struct pmu_mappings {
781 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
782 struct perf_evlist *evlist __maybe_unused)
784 struct perf_pmu *pmu = NULL;
785 off_t offset = lseek(fd, 0, SEEK_CUR);
789 /* write real pmu_num later */
790 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
794 while ((pmu = perf_pmu__scan(pmu))) {
799 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
803 ret = do_write_string(fd, pmu->name);
808 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
810 lseek(fd, offset, SEEK_SET);
820 * struct group_descs {
822 * struct group_desc {
829 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
830 struct perf_evlist *evlist)
832 u32 nr_groups = evlist->nr_groups;
833 struct perf_evsel *evsel;
836 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
840 evlist__for_each(evlist, evsel) {
841 if (perf_evsel__is_group_leader(evsel) &&
842 evsel->nr_members > 1) {
843 const char *name = evsel->group_name ?: "{anon_group}";
844 u32 leader_idx = evsel->idx;
845 u32 nr_members = evsel->nr_members;
847 ret = do_write_string(fd, name);
851 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
855 ret = do_write(fd, &nr_members, sizeof(nr_members));
864 * default get_cpuid(): nothing gets recorded
865 * actual implementation must be in arch/$(ARCH)/util/header.c
867 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
868 size_t sz __maybe_unused)
873 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
874 struct perf_evlist *evlist __maybe_unused)
879 ret = get_cpuid(buffer, sizeof(buffer));
885 return do_write_string(fd, buffer);
888 static int write_branch_stack(int fd __maybe_unused,
889 struct perf_header *h __maybe_unused,
890 struct perf_evlist *evlist __maybe_unused)
895 static int write_auxtrace(int fd, struct perf_header *h,
896 struct perf_evlist *evlist __maybe_unused)
898 struct perf_session *session;
901 session = container_of(h, struct perf_session, header);
903 err = auxtrace_index__write(fd, &session->auxtrace_index);
905 pr_err("Failed to write auxtrace index\n");
909 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
912 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
915 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
918 fprintf(fp, "# os release : %s\n", ph->env.os_release);
921 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
923 fprintf(fp, "# arch : %s\n", ph->env.arch);
926 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
929 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
932 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
935 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
936 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
939 static void print_version(struct perf_header *ph, int fd __maybe_unused,
942 fprintf(fp, "# perf version : %s\n", ph->env.version);
945 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
950 nr = ph->env.nr_cmdline;
952 fprintf(fp, "# cmdline : ");
954 for (i = 0; i < nr; i++)
955 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
959 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
964 int cpu_nr = ph->env.nr_cpus_online;
966 nr = ph->env.nr_sibling_cores;
967 str = ph->env.sibling_cores;
969 for (i = 0; i < nr; i++) {
970 fprintf(fp, "# sibling cores : %s\n", str);
971 str += strlen(str) + 1;
974 nr = ph->env.nr_sibling_threads;
975 str = ph->env.sibling_threads;
977 for (i = 0; i < nr; i++) {
978 fprintf(fp, "# sibling threads : %s\n", str);
979 str += strlen(str) + 1;
982 if (ph->env.cpu != NULL) {
983 for (i = 0; i < cpu_nr; i++)
984 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
985 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
987 fprintf(fp, "# Core ID and Socket ID information is not available\n");
990 static void free_event_desc(struct perf_evsel *events)
992 struct perf_evsel *evsel;
997 for (evsel = events; evsel->attr.size; evsel++) {
1005 static struct perf_evsel *
1006 read_event_desc(struct perf_header *ph, int fd)
1008 struct perf_evsel *evsel, *events = NULL;
1011 u32 nre, sz, nr, i, j;
1015 /* number of events */
1016 ret = readn(fd, &nre, sizeof(nre));
1017 if (ret != (ssize_t)sizeof(nre))
1021 nre = bswap_32(nre);
1023 ret = readn(fd, &sz, sizeof(sz));
1024 if (ret != (ssize_t)sizeof(sz))
1030 /* buffer to hold on file attr struct */
1035 /* the last event terminates with evsel->attr.size == 0: */
1036 events = calloc(nre + 1, sizeof(*events));
1040 msz = sizeof(evsel->attr);
1044 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1048 * must read entire on-file attr struct to
1049 * sync up with layout.
1051 ret = readn(fd, buf, sz);
1052 if (ret != (ssize_t)sz)
1056 perf_event__attr_swap(buf);
1058 memcpy(&evsel->attr, buf, msz);
1060 ret = readn(fd, &nr, sizeof(nr));
1061 if (ret != (ssize_t)sizeof(nr))
1064 if (ph->needs_swap) {
1066 evsel->needs_swap = true;
1069 evsel->name = do_read_string(fd, ph);
1074 id = calloc(nr, sizeof(*id));
1080 for (j = 0 ; j < nr; j++) {
1081 ret = readn(fd, id, sizeof(*id));
1082 if (ret != (ssize_t)sizeof(*id))
1085 *id = bswap_64(*id);
1093 free_event_desc(events);
1098 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1099 void *priv __attribute__((unused)))
1101 return fprintf(fp, ", %s = %s", name, val);
1104 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1106 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1111 fprintf(fp, "# event desc: not available or unable to read\n");
1115 for (evsel = events; evsel->attr.size; evsel++) {
1116 fprintf(fp, "# event : name = %s, ", evsel->name);
1119 fprintf(fp, ", id = {");
1120 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1123 fprintf(fp, " %"PRIu64, *id);
1128 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1133 free_event_desc(events);
1136 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1139 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1142 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1147 uint64_t mem_total, mem_free;
1150 nr = ph->env.nr_numa_nodes;
1151 str = ph->env.numa_nodes;
1153 for (i = 0; i < nr; i++) {
1155 c = strtoul(str, &tmp, 0);
1160 mem_total = strtoull(str, &tmp, 0);
1165 mem_free = strtoull(str, &tmp, 0);
1169 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1170 " free = %"PRIu64" kB\n",
1171 c, mem_total, mem_free);
1174 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1176 str += strlen(str) + 1;
1180 fprintf(fp, "# numa topology : not available\n");
1183 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1185 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1188 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1189 int fd __maybe_unused, FILE *fp)
1191 fprintf(fp, "# contains samples with branch stack\n");
1194 static void print_auxtrace(struct perf_header *ph __maybe_unused,
1195 int fd __maybe_unused, FILE *fp)
1197 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1200 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1203 const char *delimiter = "# pmu mappings: ";
1208 pmu_num = ph->env.nr_pmu_mappings;
1210 fprintf(fp, "# pmu mappings: not available\n");
1214 str = ph->env.pmu_mappings;
1217 type = strtoul(str, &tmp, 0);
1222 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1225 str += strlen(str) + 1;
1234 fprintf(fp, "# pmu mappings: unable to read\n");
1237 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1240 struct perf_session *session;
1241 struct perf_evsel *evsel;
1244 session = container_of(ph, struct perf_session, header);
1246 evlist__for_each(session->evlist, evsel) {
1247 if (perf_evsel__is_group_leader(evsel) &&
1248 evsel->nr_members > 1) {
1249 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1250 perf_evsel__name(evsel));
1252 nr = evsel->nr_members - 1;
1254 fprintf(fp, ",%s", perf_evsel__name(evsel));
1262 static int __event_process_build_id(struct build_id_event *bev,
1264 struct perf_session *session)
1267 struct machine *machine;
1270 enum dso_kernel_type dso_type;
1272 machine = perf_session__findnew_machine(session, bev->pid);
1276 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1279 case PERF_RECORD_MISC_KERNEL:
1280 dso_type = DSO_TYPE_KERNEL;
1282 case PERF_RECORD_MISC_GUEST_KERNEL:
1283 dso_type = DSO_TYPE_GUEST_KERNEL;
1285 case PERF_RECORD_MISC_USER:
1286 case PERF_RECORD_MISC_GUEST_USER:
1287 dso_type = DSO_TYPE_USER;
1293 dso = machine__findnew_dso(machine, filename);
1295 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1297 dso__set_build_id(dso, &bev->build_id);
1299 if (!is_kernel_module(filename, cpumode))
1300 dso->kernel = dso_type;
1302 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1304 pr_debug("build id event received for %s: %s\n",
1305 dso->long_name, sbuild_id);
1314 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1315 int input, u64 offset, u64 size)
1317 struct perf_session *session = container_of(header, struct perf_session, header);
1319 struct perf_event_header header;
1320 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1323 struct build_id_event bev;
1324 char filename[PATH_MAX];
1325 u64 limit = offset + size;
1327 while (offset < limit) {
1330 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1333 if (header->needs_swap)
1334 perf_event_header__bswap(&old_bev.header);
1336 len = old_bev.header.size - sizeof(old_bev);
1337 if (readn(input, filename, len) != len)
1340 bev.header = old_bev.header;
1343 * As the pid is the missing value, we need to fill
1344 * it properly. The header.misc value give us nice hint.
1346 bev.pid = HOST_KERNEL_ID;
1347 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1348 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1349 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1351 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1352 __event_process_build_id(&bev, filename, session);
1354 offset += bev.header.size;
1360 static int perf_header__read_build_ids(struct perf_header *header,
1361 int input, u64 offset, u64 size)
1363 struct perf_session *session = container_of(header, struct perf_session, header);
1364 struct build_id_event bev;
1365 char filename[PATH_MAX];
1366 u64 limit = offset + size, orig_offset = offset;
1369 while (offset < limit) {
1372 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1375 if (header->needs_swap)
1376 perf_event_header__bswap(&bev.header);
1378 len = bev.header.size - sizeof(bev);
1379 if (readn(input, filename, len) != len)
1382 * The a1645ce1 changeset:
1384 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1386 * Added a field to struct build_id_event that broke the file
1389 * Since the kernel build-id is the first entry, process the
1390 * table using the old format if the well known
1391 * '[kernel.kallsyms]' string for the kernel build-id has the
1392 * first 4 characters chopped off (where the pid_t sits).
1394 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1395 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1397 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1400 __event_process_build_id(&bev, filename, session);
1402 offset += bev.header.size;
1409 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1410 struct perf_header *ph __maybe_unused,
1413 ssize_t ret = trace_report(fd, data, false);
1414 return ret < 0 ? -1 : 0;
1417 static int process_build_id(struct perf_file_section *section,
1418 struct perf_header *ph, int fd,
1419 void *data __maybe_unused)
1421 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1422 pr_debug("Failed to read buildids, continuing...\n");
1426 static int process_hostname(struct perf_file_section *section __maybe_unused,
1427 struct perf_header *ph, int fd,
1428 void *data __maybe_unused)
1430 ph->env.hostname = do_read_string(fd, ph);
1431 return ph->env.hostname ? 0 : -ENOMEM;
1434 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1435 struct perf_header *ph, int fd,
1436 void *data __maybe_unused)
1438 ph->env.os_release = do_read_string(fd, ph);
1439 return ph->env.os_release ? 0 : -ENOMEM;
1442 static int process_version(struct perf_file_section *section __maybe_unused,
1443 struct perf_header *ph, int fd,
1444 void *data __maybe_unused)
1446 ph->env.version = do_read_string(fd, ph);
1447 return ph->env.version ? 0 : -ENOMEM;
1450 static int process_arch(struct perf_file_section *section __maybe_unused,
1451 struct perf_header *ph, int fd,
1452 void *data __maybe_unused)
1454 ph->env.arch = do_read_string(fd, ph);
1455 return ph->env.arch ? 0 : -ENOMEM;
1458 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1459 struct perf_header *ph, int fd,
1460 void *data __maybe_unused)
1465 ret = readn(fd, &nr, sizeof(nr));
1466 if (ret != sizeof(nr))
1472 ph->env.nr_cpus_online = nr;
1474 ret = readn(fd, &nr, sizeof(nr));
1475 if (ret != sizeof(nr))
1481 ph->env.nr_cpus_avail = nr;
1485 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1486 struct perf_header *ph, int fd,
1487 void *data __maybe_unused)
1489 ph->env.cpu_desc = do_read_string(fd, ph);
1490 return ph->env.cpu_desc ? 0 : -ENOMEM;
1493 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1494 struct perf_header *ph, int fd,
1495 void *data __maybe_unused)
1497 ph->env.cpuid = do_read_string(fd, ph);
1498 return ph->env.cpuid ? 0 : -ENOMEM;
1501 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1502 struct perf_header *ph, int fd,
1503 void *data __maybe_unused)
1508 ret = readn(fd, &mem, sizeof(mem));
1509 if (ret != sizeof(mem))
1513 mem = bswap_64(mem);
1515 ph->env.total_mem = mem;
1519 static struct perf_evsel *
1520 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1522 struct perf_evsel *evsel;
1524 evlist__for_each(evlist, evsel) {
1525 if (evsel->idx == idx)
1533 perf_evlist__set_event_name(struct perf_evlist *evlist,
1534 struct perf_evsel *event)
1536 struct perf_evsel *evsel;
1541 evsel = perf_evlist__find_by_index(evlist, event->idx);
1548 evsel->name = strdup(event->name);
1552 process_event_desc(struct perf_file_section *section __maybe_unused,
1553 struct perf_header *header, int fd,
1554 void *data __maybe_unused)
1556 struct perf_session *session;
1557 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1562 session = container_of(header, struct perf_session, header);
1563 for (evsel = events; evsel->attr.size; evsel++)
1564 perf_evlist__set_event_name(session->evlist, evsel);
1566 free_event_desc(events);
1571 static int process_cmdline(struct perf_file_section *section,
1572 struct perf_header *ph, int fd,
1573 void *data __maybe_unused)
1576 char *str, *cmdline = NULL, **argv = NULL;
1579 ret = readn(fd, &nr, sizeof(nr));
1580 if (ret != sizeof(nr))
1586 ph->env.nr_cmdline = nr;
1588 cmdline = zalloc(section->size + nr + 1);
1592 argv = zalloc(sizeof(char *) * (nr + 1));
1596 for (i = 0; i < nr; i++) {
1597 str = do_read_string(fd, ph);
1601 argv[i] = cmdline + len;
1602 memcpy(argv[i], str, strlen(str) + 1);
1603 len += strlen(str) + 1;
1606 ph->env.cmdline = cmdline;
1607 ph->env.cmdline_argv = (const char **) argv;
1616 static int process_cpu_topology(struct perf_file_section *section,
1617 struct perf_header *ph, int fd,
1618 void *data __maybe_unused)
1624 int cpu_nr = ph->env.nr_cpus_online;
1627 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1631 ret = readn(fd, &nr, sizeof(nr));
1632 if (ret != sizeof(nr))
1638 ph->env.nr_sibling_cores = nr;
1639 size += sizeof(u32);
1640 strbuf_init(&sb, 128);
1642 for (i = 0; i < nr; i++) {
1643 str = do_read_string(fd, ph);
1647 /* include a NULL character at the end */
1648 strbuf_add(&sb, str, strlen(str) + 1);
1649 size += string_size(str);
1652 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1654 ret = readn(fd, &nr, sizeof(nr));
1655 if (ret != sizeof(nr))
1661 ph->env.nr_sibling_threads = nr;
1662 size += sizeof(u32);
1664 for (i = 0; i < nr; i++) {
1665 str = do_read_string(fd, ph);
1669 /* include a NULL character at the end */
1670 strbuf_add(&sb, str, strlen(str) + 1);
1671 size += string_size(str);
1674 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1677 * The header may be from old perf,
1678 * which doesn't include core id and socket id information.
1680 if (section->size <= size) {
1681 zfree(&ph->env.cpu);
1685 for (i = 0; i < (u32)cpu_nr; i++) {
1686 ret = readn(fd, &nr, sizeof(nr));
1687 if (ret != sizeof(nr))
1693 if (nr > (u32)cpu_nr) {
1694 pr_debug("core_id number is too big."
1695 "You may need to upgrade the perf tool.\n");
1698 ph->env.cpu[i].core_id = nr;
1700 ret = readn(fd, &nr, sizeof(nr));
1701 if (ret != sizeof(nr))
1707 if (nr > (u32)cpu_nr) {
1708 pr_debug("socket_id number is too big."
1709 "You may need to upgrade the perf tool.\n");
1713 ph->env.cpu[i].socket_id = nr;
1719 strbuf_release(&sb);
1721 zfree(&ph->env.cpu);
1725 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1726 struct perf_header *ph, int fd,
1727 void *data __maybe_unused)
1732 uint64_t mem_total, mem_free;
1736 ret = readn(fd, &nr, sizeof(nr));
1737 if (ret != sizeof(nr))
1743 ph->env.nr_numa_nodes = nr;
1744 strbuf_init(&sb, 256);
1746 for (i = 0; i < nr; i++) {
1748 ret = readn(fd, &node, sizeof(node));
1749 if (ret != sizeof(node))
1752 ret = readn(fd, &mem_total, sizeof(u64));
1753 if (ret != sizeof(u64))
1756 ret = readn(fd, &mem_free, sizeof(u64));
1757 if (ret != sizeof(u64))
1760 if (ph->needs_swap) {
1761 node = bswap_32(node);
1762 mem_total = bswap_64(mem_total);
1763 mem_free = bswap_64(mem_free);
1766 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1767 node, mem_total, mem_free);
1769 str = do_read_string(fd, ph);
1773 /* include a NULL character at the end */
1774 strbuf_add(&sb, str, strlen(str) + 1);
1777 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1781 strbuf_release(&sb);
1785 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1786 struct perf_header *ph, int fd,
1787 void *data __maybe_unused)
1795 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1796 if (ret != sizeof(pmu_num))
1800 pmu_num = bswap_32(pmu_num);
1803 pr_debug("pmu mappings not available\n");
1807 ph->env.nr_pmu_mappings = pmu_num;
1808 strbuf_init(&sb, 128);
1811 if (readn(fd, &type, sizeof(type)) != sizeof(type))
1814 type = bswap_32(type);
1816 name = do_read_string(fd, ph);
1820 strbuf_addf(&sb, "%u:%s", type, name);
1821 /* include a NULL character at the end */
1822 strbuf_add(&sb, "", 1);
1827 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1831 strbuf_release(&sb);
1835 static int process_group_desc(struct perf_file_section *section __maybe_unused,
1836 struct perf_header *ph, int fd,
1837 void *data __maybe_unused)
1840 u32 i, nr, nr_groups;
1841 struct perf_session *session;
1842 struct perf_evsel *evsel, *leader = NULL;
1849 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1853 nr_groups = bswap_32(nr_groups);
1855 ph->env.nr_groups = nr_groups;
1857 pr_debug("group desc not available\n");
1861 desc = calloc(nr_groups, sizeof(*desc));
1865 for (i = 0; i < nr_groups; i++) {
1866 desc[i].name = do_read_string(fd, ph);
1870 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1873 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1876 if (ph->needs_swap) {
1877 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1878 desc[i].nr_members = bswap_32(desc[i].nr_members);
1883 * Rebuild group relationship based on the group_desc
1885 session = container_of(ph, struct perf_session, header);
1886 session->evlist->nr_groups = nr_groups;
1889 evlist__for_each(session->evlist, evsel) {
1890 if (evsel->idx == (int) desc[i].leader_idx) {
1891 evsel->leader = evsel;
1892 /* {anon_group} is a dummy name */
1893 if (strcmp(desc[i].name, "{anon_group}")) {
1894 evsel->group_name = desc[i].name;
1895 desc[i].name = NULL;
1897 evsel->nr_members = desc[i].nr_members;
1899 if (i >= nr_groups || nr > 0) {
1900 pr_debug("invalid group desc\n");
1905 nr = evsel->nr_members - 1;
1908 /* This is a group member */
1909 evsel->leader = leader;
1915 if (i != nr_groups || nr != 0) {
1916 pr_debug("invalid group desc\n");
1922 for (i = 0; i < nr_groups; i++)
1923 zfree(&desc[i].name);
1929 static int process_auxtrace(struct perf_file_section *section,
1930 struct perf_header *ph, int fd,
1931 void *data __maybe_unused)
1933 struct perf_session *session;
1936 session = container_of(ph, struct perf_session, header);
1938 err = auxtrace_index__process(fd, section->size, session,
1941 pr_err("Failed to process auxtrace index\n");
1945 struct feature_ops {
1946 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1947 void (*print)(struct perf_header *h, int fd, FILE *fp);
1948 int (*process)(struct perf_file_section *section,
1949 struct perf_header *h, int fd, void *data);
1954 #define FEAT_OPA(n, func) \
1955 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1956 #define FEAT_OPP(n, func) \
1957 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1958 .process = process_##func }
1959 #define FEAT_OPF(n, func) \
1960 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1961 .process = process_##func, .full_only = true }
1963 /* feature_ops not implemented: */
1964 #define print_tracing_data NULL
1965 #define print_build_id NULL
1967 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1968 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1969 FEAT_OPP(HEADER_BUILD_ID, build_id),
1970 FEAT_OPP(HEADER_HOSTNAME, hostname),
1971 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1972 FEAT_OPP(HEADER_VERSION, version),
1973 FEAT_OPP(HEADER_ARCH, arch),
1974 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1975 FEAT_OPP(HEADER_CPUDESC, cpudesc),
1976 FEAT_OPP(HEADER_CPUID, cpuid),
1977 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
1978 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
1979 FEAT_OPP(HEADER_CMDLINE, cmdline),
1980 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1981 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1982 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
1983 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
1984 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
1985 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
1988 struct header_print_data {
1990 bool full; /* extended list of headers */
1993 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1994 struct perf_header *ph,
1995 int feat, int fd, void *data)
1997 struct header_print_data *hd = data;
1999 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2000 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2001 "%d, continuing...\n", section->offset, feat);
2004 if (feat >= HEADER_LAST_FEATURE) {
2005 pr_warning("unknown feature %d\n", feat);
2008 if (!feat_ops[feat].print)
2011 if (!feat_ops[feat].full_only || hd->full)
2012 feat_ops[feat].print(ph, fd, hd->fp);
2014 fprintf(hd->fp, "# %s info available, use -I to display\n",
2015 feat_ops[feat].name);
2020 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2022 struct header_print_data hd;
2023 struct perf_header *header = &session->header;
2024 int fd = perf_data_file__fd(session->file);
2028 perf_header__process_sections(header, fd, &hd,
2029 perf_file_section__fprintf_info);
2033 static int do_write_feat(int fd, struct perf_header *h, int type,
2034 struct perf_file_section **p,
2035 struct perf_evlist *evlist)
2040 if (perf_header__has_feat(h, type)) {
2041 if (!feat_ops[type].write)
2044 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2046 err = feat_ops[type].write(fd, h, evlist);
2048 pr_debug("failed to write feature %d\n", type);
2050 /* undo anything written */
2051 lseek(fd, (*p)->offset, SEEK_SET);
2055 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2061 static int perf_header__adds_write(struct perf_header *header,
2062 struct perf_evlist *evlist, int fd)
2065 struct perf_file_section *feat_sec, *p;
2071 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2075 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2076 if (feat_sec == NULL)
2079 sec_size = sizeof(*feat_sec) * nr_sections;
2081 sec_start = header->feat_offset;
2082 lseek(fd, sec_start + sec_size, SEEK_SET);
2084 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2085 if (do_write_feat(fd, header, feat, &p, evlist))
2086 perf_header__clear_feat(header, feat);
2089 lseek(fd, sec_start, SEEK_SET);
2091 * may write more than needed due to dropped feature, but
2092 * this is okay, reader will skip the mising entries
2094 err = do_write(fd, feat_sec, sec_size);
2096 pr_debug("failed to write feature section\n");
2101 int perf_header__write_pipe(int fd)
2103 struct perf_pipe_file_header f_header;
2106 f_header = (struct perf_pipe_file_header){
2107 .magic = PERF_MAGIC,
2108 .size = sizeof(f_header),
2111 err = do_write(fd, &f_header, sizeof(f_header));
2113 pr_debug("failed to write perf pipe header\n");
2120 int perf_session__write_header(struct perf_session *session,
2121 struct perf_evlist *evlist,
2122 int fd, bool at_exit)
2124 struct perf_file_header f_header;
2125 struct perf_file_attr f_attr;
2126 struct perf_header *header = &session->header;
2127 struct perf_evsel *evsel;
2131 lseek(fd, sizeof(f_header), SEEK_SET);
2133 evlist__for_each(session->evlist, evsel) {
2134 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2135 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2137 pr_debug("failed to write perf header\n");
2142 attr_offset = lseek(fd, 0, SEEK_CUR);
2144 evlist__for_each(evlist, evsel) {
2145 f_attr = (struct perf_file_attr){
2146 .attr = evsel->attr,
2148 .offset = evsel->id_offset,
2149 .size = evsel->ids * sizeof(u64),
2152 err = do_write(fd, &f_attr, sizeof(f_attr));
2154 pr_debug("failed to write perf header attribute\n");
2159 if (!header->data_offset)
2160 header->data_offset = lseek(fd, 0, SEEK_CUR);
2161 header->feat_offset = header->data_offset + header->data_size;
2164 err = perf_header__adds_write(header, evlist, fd);
2169 f_header = (struct perf_file_header){
2170 .magic = PERF_MAGIC,
2171 .size = sizeof(f_header),
2172 .attr_size = sizeof(f_attr),
2174 .offset = attr_offset,
2175 .size = evlist->nr_entries * sizeof(f_attr),
2178 .offset = header->data_offset,
2179 .size = header->data_size,
2181 /* event_types is ignored, store zeros */
2184 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2186 lseek(fd, 0, SEEK_SET);
2187 err = do_write(fd, &f_header, sizeof(f_header));
2189 pr_debug("failed to write perf header\n");
2192 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2197 static int perf_header__getbuffer64(struct perf_header *header,
2198 int fd, void *buf, size_t size)
2200 if (readn(fd, buf, size) <= 0)
2203 if (header->needs_swap)
2204 mem_bswap_64(buf, size);
2209 int perf_header__process_sections(struct perf_header *header, int fd,
2211 int (*process)(struct perf_file_section *section,
2212 struct perf_header *ph,
2213 int feat, int fd, void *data))
2215 struct perf_file_section *feat_sec, *sec;
2221 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2225 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2229 sec_size = sizeof(*feat_sec) * nr_sections;
2231 lseek(fd, header->feat_offset, SEEK_SET);
2233 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2237 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2238 err = process(sec++, header, feat, fd, data);
2248 static const int attr_file_abi_sizes[] = {
2249 [0] = PERF_ATTR_SIZE_VER0,
2250 [1] = PERF_ATTR_SIZE_VER1,
2251 [2] = PERF_ATTR_SIZE_VER2,
2252 [3] = PERF_ATTR_SIZE_VER3,
2253 [4] = PERF_ATTR_SIZE_VER4,
2258 * In the legacy file format, the magic number is not used to encode endianness.
2259 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2260 * on ABI revisions, we need to try all combinations for all endianness to
2261 * detect the endianness.
2263 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2265 uint64_t ref_size, attr_size;
2268 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2269 ref_size = attr_file_abi_sizes[i]
2270 + sizeof(struct perf_file_section);
2271 if (hdr_sz != ref_size) {
2272 attr_size = bswap_64(hdr_sz);
2273 if (attr_size != ref_size)
2276 ph->needs_swap = true;
2278 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2283 /* could not determine endianness */
2287 #define PERF_PIPE_HDR_VER0 16
2289 static const size_t attr_pipe_abi_sizes[] = {
2290 [0] = PERF_PIPE_HDR_VER0,
2295 * In the legacy pipe format, there is an implicit assumption that endiannesss
2296 * between host recording the samples, and host parsing the samples is the
2297 * same. This is not always the case given that the pipe output may always be
2298 * redirected into a file and analyzed on a different machine with possibly a
2299 * different endianness and perf_event ABI revsions in the perf tool itself.
2301 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2306 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2307 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2308 attr_size = bswap_64(hdr_sz);
2309 if (attr_size != hdr_sz)
2312 ph->needs_swap = true;
2314 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2320 bool is_perf_magic(u64 magic)
2322 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2323 || magic == __perf_magic2
2324 || magic == __perf_magic2_sw)
2330 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2331 bool is_pipe, struct perf_header *ph)
2335 /* check for legacy format */
2336 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2338 ph->version = PERF_HEADER_VERSION_1;
2339 pr_debug("legacy perf.data format\n");
2341 return try_all_pipe_abis(hdr_sz, ph);
2343 return try_all_file_abis(hdr_sz, ph);
2346 * the new magic number serves two purposes:
2347 * - unique number to identify actual perf.data files
2348 * - encode endianness of file
2350 ph->version = PERF_HEADER_VERSION_2;
2352 /* check magic number with one endianness */
2353 if (magic == __perf_magic2)
2356 /* check magic number with opposite endianness */
2357 if (magic != __perf_magic2_sw)
2360 ph->needs_swap = true;
2365 int perf_file_header__read(struct perf_file_header *header,
2366 struct perf_header *ph, int fd)
2370 lseek(fd, 0, SEEK_SET);
2372 ret = readn(fd, header, sizeof(*header));
2376 if (check_magic_endian(header->magic,
2377 header->attr_size, false, ph) < 0) {
2378 pr_debug("magic/endian check failed\n");
2382 if (ph->needs_swap) {
2383 mem_bswap_64(header, offsetof(struct perf_file_header,
2387 if (header->size != sizeof(*header)) {
2388 /* Support the previous format */
2389 if (header->size == offsetof(typeof(*header), adds_features))
2390 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2393 } else if (ph->needs_swap) {
2395 * feature bitmap is declared as an array of unsigned longs --
2396 * not good since its size can differ between the host that
2397 * generated the data file and the host analyzing the file.
2399 * We need to handle endianness, but we don't know the size of
2400 * the unsigned long where the file was generated. Take a best
2401 * guess at determining it: try 64-bit swap first (ie., file
2402 * created on a 64-bit host), and check if the hostname feature
2403 * bit is set (this feature bit is forced on as of fbe96f2).
2404 * If the bit is not, undo the 64-bit swap and try a 32-bit
2405 * swap. If the hostname bit is still not set (e.g., older data
2406 * file), punt and fallback to the original behavior --
2407 * clearing all feature bits and setting buildid.
2409 mem_bswap_64(&header->adds_features,
2410 BITS_TO_U64(HEADER_FEAT_BITS));
2412 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2414 mem_bswap_64(&header->adds_features,
2415 BITS_TO_U64(HEADER_FEAT_BITS));
2418 mem_bswap_32(&header->adds_features,
2419 BITS_TO_U32(HEADER_FEAT_BITS));
2422 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2423 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2424 set_bit(HEADER_BUILD_ID, header->adds_features);
2428 memcpy(&ph->adds_features, &header->adds_features,
2429 sizeof(ph->adds_features));
2431 ph->data_offset = header->data.offset;
2432 ph->data_size = header->data.size;
2433 ph->feat_offset = header->data.offset + header->data.size;
2437 static int perf_file_section__process(struct perf_file_section *section,
2438 struct perf_header *ph,
2439 int feat, int fd, void *data)
2441 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2442 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2443 "%d, continuing...\n", section->offset, feat);
2447 if (feat >= HEADER_LAST_FEATURE) {
2448 pr_debug("unknown feature %d, continuing...\n", feat);
2452 if (!feat_ops[feat].process)
2455 return feat_ops[feat].process(section, ph, fd, data);
2458 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2459 struct perf_header *ph, int fd,
2464 ret = readn(fd, header, sizeof(*header));
2468 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2469 pr_debug("endian/magic failed\n");
2474 header->size = bswap_64(header->size);
2476 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2482 static int perf_header__read_pipe(struct perf_session *session)
2484 struct perf_header *header = &session->header;
2485 struct perf_pipe_file_header f_header;
2487 if (perf_file_header__read_pipe(&f_header, header,
2488 perf_data_file__fd(session->file),
2489 session->repipe) < 0) {
2490 pr_debug("incompatible file format\n");
2497 static int read_attr(int fd, struct perf_header *ph,
2498 struct perf_file_attr *f_attr)
2500 struct perf_event_attr *attr = &f_attr->attr;
2502 size_t our_sz = sizeof(f_attr->attr);
2505 memset(f_attr, 0, sizeof(*f_attr));
2507 /* read minimal guaranteed structure */
2508 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2510 pr_debug("cannot read %d bytes of header attr\n",
2511 PERF_ATTR_SIZE_VER0);
2515 /* on file perf_event_attr size */
2523 sz = PERF_ATTR_SIZE_VER0;
2524 } else if (sz > our_sz) {
2525 pr_debug("file uses a more recent and unsupported ABI"
2526 " (%zu bytes extra)\n", sz - our_sz);
2529 /* what we have not yet read and that we know about */
2530 left = sz - PERF_ATTR_SIZE_VER0;
2533 ptr += PERF_ATTR_SIZE_VER0;
2535 ret = readn(fd, ptr, left);
2537 /* read perf_file_section, ids are read in caller */
2538 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2540 return ret <= 0 ? -1 : 0;
2543 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2544 struct pevent *pevent)
2546 struct event_format *event;
2549 /* already prepared */
2550 if (evsel->tp_format)
2553 if (pevent == NULL) {
2554 pr_debug("broken or missing trace data\n");
2558 event = pevent_find_event(pevent, evsel->attr.config);
2563 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2564 evsel->name = strdup(bf);
2565 if (evsel->name == NULL)
2569 evsel->tp_format = event;
2573 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2574 struct pevent *pevent)
2576 struct perf_evsel *pos;
2578 evlist__for_each(evlist, pos) {
2579 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2580 perf_evsel__prepare_tracepoint_event(pos, pevent))
2587 int perf_session__read_header(struct perf_session *session)
2589 struct perf_data_file *file = session->file;
2590 struct perf_header *header = &session->header;
2591 struct perf_file_header f_header;
2592 struct perf_file_attr f_attr;
2594 int nr_attrs, nr_ids, i, j;
2595 int fd = perf_data_file__fd(file);
2597 session->evlist = perf_evlist__new();
2598 if (session->evlist == NULL)
2601 session->evlist->env = &header->env;
2602 if (perf_data_file__is_pipe(file))
2603 return perf_header__read_pipe(session);
2605 if (perf_file_header__read(&f_header, header, fd) < 0)
2609 * Sanity check that perf.data was written cleanly; data size is
2610 * initialized to 0 and updated only if the on_exit function is run.
2611 * If data size is still 0 then the file contains only partial
2612 * information. Just warn user and process it as much as it can.
2614 if (f_header.data.size == 0) {
2615 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2616 "Was the 'perf record' command properly terminated?\n",
2620 nr_attrs = f_header.attrs.size / f_header.attr_size;
2621 lseek(fd, f_header.attrs.offset, SEEK_SET);
2623 for (i = 0; i < nr_attrs; i++) {
2624 struct perf_evsel *evsel;
2627 if (read_attr(fd, header, &f_attr) < 0)
2630 if (header->needs_swap) {
2631 f_attr.ids.size = bswap_64(f_attr.ids.size);
2632 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2633 perf_event__attr_swap(&f_attr.attr);
2636 tmp = lseek(fd, 0, SEEK_CUR);
2637 evsel = perf_evsel__new(&f_attr.attr);
2640 goto out_delete_evlist;
2642 evsel->needs_swap = header->needs_swap;
2644 * Do it before so that if perf_evsel__alloc_id fails, this
2645 * entry gets purged too at perf_evlist__delete().
2647 perf_evlist__add(session->evlist, evsel);
2649 nr_ids = f_attr.ids.size / sizeof(u64);
2651 * We don't have the cpu and thread maps on the header, so
2652 * for allocating the perf_sample_id table we fake 1 cpu and
2653 * hattr->ids threads.
2655 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2656 goto out_delete_evlist;
2658 lseek(fd, f_attr.ids.offset, SEEK_SET);
2660 for (j = 0; j < nr_ids; j++) {
2661 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2664 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2667 lseek(fd, tmp, SEEK_SET);
2670 symbol_conf.nr_events = nr_attrs;
2672 perf_header__process_sections(header, fd, &session->tevent,
2673 perf_file_section__process);
2675 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2676 session->tevent.pevent))
2677 goto out_delete_evlist;
2684 perf_evlist__delete(session->evlist);
2685 session->evlist = NULL;
2689 int perf_event__synthesize_attr(struct perf_tool *tool,
2690 struct perf_event_attr *attr, u32 ids, u64 *id,
2691 perf_event__handler_t process)
2693 union perf_event *ev;
2697 size = sizeof(struct perf_event_attr);
2698 size = PERF_ALIGN(size, sizeof(u64));
2699 size += sizeof(struct perf_event_header);
2700 size += ids * sizeof(u64);
2707 ev->attr.attr = *attr;
2708 memcpy(ev->attr.id, id, ids * sizeof(u64));
2710 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2711 ev->attr.header.size = (u16)size;
2713 if (ev->attr.header.size == size)
2714 err = process(tool, ev, NULL, NULL);
2723 int perf_event__synthesize_attrs(struct perf_tool *tool,
2724 struct perf_session *session,
2725 perf_event__handler_t process)
2727 struct perf_evsel *evsel;
2730 evlist__for_each(session->evlist, evsel) {
2731 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2732 evsel->id, process);
2734 pr_debug("failed to create perf header attribute\n");
2742 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2743 union perf_event *event,
2744 struct perf_evlist **pevlist)
2747 struct perf_evsel *evsel;
2748 struct perf_evlist *evlist = *pevlist;
2750 if (evlist == NULL) {
2751 *pevlist = evlist = perf_evlist__new();
2756 evsel = perf_evsel__new(&event->attr.attr);
2760 perf_evlist__add(evlist, evsel);
2762 ids = event->header.size;
2763 ids -= (void *)&event->attr.id - (void *)event;
2764 n_ids = ids / sizeof(u64);
2766 * We don't have the cpu and thread maps on the header, so
2767 * for allocating the perf_sample_id table we fake 1 cpu and
2768 * hattr->ids threads.
2770 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2773 for (i = 0; i < n_ids; i++) {
2774 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2777 symbol_conf.nr_events = evlist->nr_entries;
2782 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2783 struct perf_evlist *evlist,
2784 perf_event__handler_t process)
2786 union perf_event ev;
2787 struct tracing_data *tdata;
2788 ssize_t size = 0, aligned_size = 0, padding;
2789 int err __maybe_unused = 0;
2792 * We are going to store the size of the data followed
2793 * by the data contents. Since the fd descriptor is a pipe,
2794 * we cannot seek back to store the size of the data once
2795 * we know it. Instead we:
2797 * - write the tracing data to the temp file
2798 * - get/write the data size to pipe
2799 * - write the tracing data from the temp file
2802 tdata = tracing_data_get(&evlist->entries, fd, true);
2806 memset(&ev, 0, sizeof(ev));
2808 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2810 aligned_size = PERF_ALIGN(size, sizeof(u64));
2811 padding = aligned_size - size;
2812 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2813 ev.tracing_data.size = aligned_size;
2815 process(tool, &ev, NULL, NULL);
2818 * The put function will copy all the tracing data
2819 * stored in temp file to the pipe.
2821 tracing_data_put(tdata);
2823 write_padded(fd, NULL, 0, padding);
2825 return aligned_size;
2828 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2829 union perf_event *event,
2830 struct perf_session *session)
2832 ssize_t size_read, padding, size = event->tracing_data.size;
2833 int fd = perf_data_file__fd(session->file);
2834 off_t offset = lseek(fd, 0, SEEK_CUR);
2837 /* setup for reading amidst mmap */
2838 lseek(fd, offset + sizeof(struct tracing_data_event),
2841 size_read = trace_report(fd, &session->tevent,
2843 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2845 if (readn(fd, buf, padding) < 0) {
2846 pr_err("%s: reading input file", __func__);
2849 if (session->repipe) {
2850 int retw = write(STDOUT_FILENO, buf, padding);
2851 if (retw <= 0 || retw != padding) {
2852 pr_err("%s: repiping tracing data padding", __func__);
2857 if (size_read + padding != size) {
2858 pr_err("%s: tracing data size mismatch", __func__);
2862 perf_evlist__prepare_tracepoint_events(session->evlist,
2863 session->tevent.pevent);
2865 return size_read + padding;
2868 int perf_event__synthesize_build_id(struct perf_tool *tool,
2869 struct dso *pos, u16 misc,
2870 perf_event__handler_t process,
2871 struct machine *machine)
2873 union perf_event ev;
2880 memset(&ev, 0, sizeof(ev));
2882 len = pos->long_name_len + 1;
2883 len = PERF_ALIGN(len, NAME_ALIGN);
2884 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2885 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2886 ev.build_id.header.misc = misc;
2887 ev.build_id.pid = machine->pid;
2888 ev.build_id.header.size = sizeof(ev.build_id) + len;
2889 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2891 err = process(tool, &ev, NULL, machine);
2896 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2897 union perf_event *event,
2898 struct perf_session *session)
2900 __event_process_build_id(&event->build_id,
2901 event->build_id.filename,