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"
29 * must be a numerical value to let the endianness
30 * determine the memory layout. That way we are able
31 * to detect endianness when reading the perf.data file
34 * we check for legacy (PERFFILE) format.
36 static const char *__perf_magic1 = "PERFFILE";
37 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
38 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
40 #define PERF_MAGIC __perf_magic2
42 struct perf_file_attr {
43 struct perf_event_attr attr;
44 struct perf_file_section ids;
47 void perf_header__set_feat(struct perf_header *header, int feat)
49 set_bit(feat, header->adds_features);
52 void perf_header__clear_feat(struct perf_header *header, int feat)
54 clear_bit(feat, header->adds_features);
57 bool perf_header__has_feat(const struct perf_header *header, int feat)
59 return test_bit(feat, header->adds_features);
62 static int do_write(int fd, const void *buf, size_t size)
65 int ret = write(fd, buf, size);
77 int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
79 static const char zero_buf[NAME_ALIGN];
80 int err = do_write(fd, bf, count);
83 err = do_write(fd, zero_buf, count_aligned - count);
88 #define string_size(str) \
89 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
91 static int do_write_string(int fd, const char *str)
96 olen = strlen(str) + 1;
97 len = PERF_ALIGN(olen, NAME_ALIGN);
99 /* write len, incl. \0 */
100 ret = do_write(fd, &len, sizeof(len));
104 return write_padded(fd, str, olen, len);
107 static char *do_read_string(int fd, struct perf_header *ph)
113 sz = readn(fd, &len, sizeof(len));
114 if (sz < (ssize_t)sizeof(len))
124 ret = readn(fd, buf, len);
125 if (ret == (ssize_t)len) {
127 * strings are padded by zeroes
128 * thus the actual strlen of buf
129 * may be less than len
138 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
139 struct perf_evlist *evlist)
141 return read_tracing_data(fd, &evlist->entries);
145 static int write_build_id(int fd, struct perf_header *h,
146 struct perf_evlist *evlist __maybe_unused)
148 struct perf_session *session;
151 session = container_of(h, struct perf_session, header);
153 if (!perf_session__read_build_ids(session, true))
156 err = perf_session__write_buildid_table(session, fd);
158 pr_debug("failed to write buildid table\n");
161 perf_session__cache_build_ids(session);
166 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
167 struct perf_evlist *evlist __maybe_unused)
176 return do_write_string(fd, uts.nodename);
179 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
180 struct perf_evlist *evlist __maybe_unused)
189 return do_write_string(fd, uts.release);
192 static int write_arch(int fd, struct perf_header *h __maybe_unused,
193 struct perf_evlist *evlist __maybe_unused)
202 return do_write_string(fd, uts.machine);
205 static int write_version(int fd, struct perf_header *h __maybe_unused,
206 struct perf_evlist *evlist __maybe_unused)
208 return do_write_string(fd, perf_version_string);
211 static int __write_cpudesc(int fd, const char *cpuinfo_proc)
216 const char *search = cpuinfo_proc;
223 file = fopen("/proc/cpuinfo", "r");
227 while (getline(&buf, &len, file) > 0) {
228 ret = strncmp(buf, search, strlen(search));
240 p = strchr(buf, ':');
241 if (p && *(p+1) == ' ' && *(p+2))
247 /* squash extra space characters (branding string) */
254 while (*q && isspace(*q))
257 while ((*r++ = *q++));
261 ret = do_write_string(fd, s);
268 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
269 struct perf_evlist *evlist __maybe_unused)
272 #define CPUINFO_PROC {"model name", }
274 const char *cpuinfo_procs[] = CPUINFO_PROC;
277 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
279 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
287 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
288 struct perf_evlist *evlist __maybe_unused)
294 nr = sysconf(_SC_NPROCESSORS_CONF);
298 nrc = (u32)(nr & UINT_MAX);
300 nr = sysconf(_SC_NPROCESSORS_ONLN);
304 nra = (u32)(nr & UINT_MAX);
306 ret = do_write(fd, &nrc, sizeof(nrc));
310 return do_write(fd, &nra, sizeof(nra));
313 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
314 struct perf_evlist *evlist)
316 struct perf_evsel *evsel;
320 nre = evlist->nr_entries;
323 * write number of events
325 ret = do_write(fd, &nre, sizeof(nre));
330 * size of perf_event_attr struct
332 sz = (u32)sizeof(evsel->attr);
333 ret = do_write(fd, &sz, sizeof(sz));
337 evlist__for_each(evlist, evsel) {
338 ret = do_write(fd, &evsel->attr, sz);
342 * write number of unique id per event
343 * there is one id per instance of an event
345 * copy into an nri to be independent of the
349 ret = do_write(fd, &nri, sizeof(nri));
354 * write event string as passed on cmdline
356 ret = do_write_string(fd, perf_evsel__name(evsel));
360 * write unique ids for this event
362 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
369 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
370 struct perf_evlist *evlist __maybe_unused)
372 char buf[MAXPATHLEN];
378 * actual atual path to perf binary
380 sprintf(proc, "/proc/%d/exe", getpid());
381 ret = readlink(proc, buf, sizeof(buf));
385 /* readlink() does not add null termination */
388 /* account for binary path */
389 n = perf_env.nr_cmdline + 1;
391 ret = do_write(fd, &n, sizeof(n));
395 ret = do_write_string(fd, buf);
399 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
400 ret = do_write_string(fd, perf_env.cmdline_argv[i]);
407 #define CORE_SIB_FMT \
408 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
409 #define THRD_SIB_FMT \
410 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
416 char **core_siblings;
417 char **thread_siblings;
420 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
423 char filename[MAXPATHLEN];
424 char *buf = NULL, *p;
430 sprintf(filename, CORE_SIB_FMT, cpu);
431 fp = fopen(filename, "r");
435 sret = getline(&buf, &len, fp);
440 p = strchr(buf, '\n');
444 for (i = 0; i < tp->core_sib; i++) {
445 if (!strcmp(buf, tp->core_siblings[i]))
448 if (i == tp->core_sib) {
449 tp->core_siblings[i] = buf;
457 sprintf(filename, THRD_SIB_FMT, cpu);
458 fp = fopen(filename, "r");
462 if (getline(&buf, &len, fp) <= 0)
465 p = strchr(buf, '\n');
469 for (i = 0; i < tp->thread_sib; i++) {
470 if (!strcmp(buf, tp->thread_siblings[i]))
473 if (i == tp->thread_sib) {
474 tp->thread_siblings[i] = buf;
486 static void free_cpu_topo(struct cpu_topo *tp)
493 for (i = 0 ; i < tp->core_sib; i++)
494 zfree(&tp->core_siblings[i]);
496 for (i = 0 ; i < tp->thread_sib; i++)
497 zfree(&tp->thread_siblings[i]);
502 static struct cpu_topo *build_cpu_topology(void)
511 ncpus = sysconf(_SC_NPROCESSORS_CONF);
515 nr = (u32)(ncpus & UINT_MAX);
517 sz = nr * sizeof(char *);
519 addr = calloc(1, sizeof(*tp) + 2 * sz);
526 tp->core_siblings = addr;
528 tp->thread_siblings = addr;
530 for (i = 0; i < nr; i++) {
531 ret = build_cpu_topo(tp, i);
542 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
543 struct perf_evlist *evlist __maybe_unused)
549 tp = build_cpu_topology();
553 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
557 for (i = 0; i < tp->core_sib; i++) {
558 ret = do_write_string(fd, tp->core_siblings[i]);
562 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
566 for (i = 0; i < tp->thread_sib; i++) {
567 ret = do_write_string(fd, tp->thread_siblings[i]);
572 ret = perf_env__read_cpu_topology_map(&perf_env);
576 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
577 ret = do_write(fd, &perf_env.cpu[j].core_id,
578 sizeof(perf_env.cpu[j].core_id));
581 ret = do_write(fd, &perf_env.cpu[j].socket_id,
582 sizeof(perf_env.cpu[j].socket_id));
593 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
594 struct perf_evlist *evlist __maybe_unused)
602 fp = fopen("/proc/meminfo", "r");
606 while (getline(&buf, &len, fp) > 0) {
607 ret = strncmp(buf, "MemTotal:", 9);
612 n = sscanf(buf, "%*s %"PRIu64, &mem);
614 ret = do_write(fd, &mem, sizeof(mem));
622 static int write_topo_node(int fd, int node)
624 char str[MAXPATHLEN];
626 char *buf = NULL, *p;
629 u64 mem_total, mem_free, mem;
632 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
633 fp = fopen(str, "r");
637 while (getline(&buf, &len, fp) > 0) {
638 /* skip over invalid lines */
639 if (!strchr(buf, ':'))
641 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
643 if (!strcmp(field, "MemTotal:"))
645 if (!strcmp(field, "MemFree:"))
652 ret = do_write(fd, &mem_total, sizeof(u64));
656 ret = do_write(fd, &mem_free, sizeof(u64));
661 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
663 fp = fopen(str, "r");
667 if (getline(&buf, &len, fp) <= 0)
670 p = strchr(buf, '\n');
674 ret = do_write_string(fd, buf);
682 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
683 struct perf_evlist *evlist __maybe_unused)
688 struct cpu_map *node_map = NULL;
693 fp = fopen("/sys/devices/system/node/online", "r");
697 if (getline(&buf, &len, fp) <= 0)
700 c = strchr(buf, '\n');
704 node_map = cpu_map__new(buf);
708 nr = (u32)node_map->nr;
710 ret = do_write(fd, &nr, sizeof(nr));
714 for (i = 0; i < nr; i++) {
715 j = (u32)node_map->map[i];
716 ret = do_write(fd, &j, sizeof(j));
720 ret = write_topo_node(fd, i);
734 * struct pmu_mappings {
743 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
744 struct perf_evlist *evlist __maybe_unused)
746 struct perf_pmu *pmu = NULL;
747 off_t offset = lseek(fd, 0, SEEK_CUR);
751 /* write real pmu_num later */
752 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
756 while ((pmu = perf_pmu__scan(pmu))) {
761 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
765 ret = do_write_string(fd, pmu->name);
770 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
772 lseek(fd, offset, SEEK_SET);
782 * struct group_descs {
784 * struct group_desc {
791 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
792 struct perf_evlist *evlist)
794 u32 nr_groups = evlist->nr_groups;
795 struct perf_evsel *evsel;
798 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
802 evlist__for_each(evlist, evsel) {
803 if (perf_evsel__is_group_leader(evsel) &&
804 evsel->nr_members > 1) {
805 const char *name = evsel->group_name ?: "{anon_group}";
806 u32 leader_idx = evsel->idx;
807 u32 nr_members = evsel->nr_members;
809 ret = do_write_string(fd, name);
813 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
817 ret = do_write(fd, &nr_members, sizeof(nr_members));
826 * default get_cpuid(): nothing gets recorded
827 * actual implementation must be in arch/$(ARCH)/util/header.c
829 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
830 size_t sz __maybe_unused)
835 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
836 struct perf_evlist *evlist __maybe_unused)
841 ret = get_cpuid(buffer, sizeof(buffer));
847 return do_write_string(fd, buffer);
850 static int write_branch_stack(int fd __maybe_unused,
851 struct perf_header *h __maybe_unused,
852 struct perf_evlist *evlist __maybe_unused)
857 static int write_auxtrace(int fd, struct perf_header *h,
858 struct perf_evlist *evlist __maybe_unused)
860 struct perf_session *session;
863 session = container_of(h, struct perf_session, header);
865 err = auxtrace_index__write(fd, &session->auxtrace_index);
867 pr_err("Failed to write auxtrace index\n");
871 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
874 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
877 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
880 fprintf(fp, "# os release : %s\n", ph->env.os_release);
883 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
885 fprintf(fp, "# arch : %s\n", ph->env.arch);
888 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
891 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
894 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
897 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
898 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
901 static void print_version(struct perf_header *ph, int fd __maybe_unused,
904 fprintf(fp, "# perf version : %s\n", ph->env.version);
907 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
912 nr = ph->env.nr_cmdline;
914 fprintf(fp, "# cmdline : ");
916 for (i = 0; i < nr; i++)
917 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
921 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
926 int cpu_nr = ph->env.nr_cpus_online;
928 nr = ph->env.nr_sibling_cores;
929 str = ph->env.sibling_cores;
931 for (i = 0; i < nr; i++) {
932 fprintf(fp, "# sibling cores : %s\n", str);
933 str += strlen(str) + 1;
936 nr = ph->env.nr_sibling_threads;
937 str = ph->env.sibling_threads;
939 for (i = 0; i < nr; i++) {
940 fprintf(fp, "# sibling threads : %s\n", str);
941 str += strlen(str) + 1;
944 if (ph->env.cpu != NULL) {
945 for (i = 0; i < cpu_nr; i++)
946 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
947 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
949 fprintf(fp, "# Core ID and Socket ID information is not available\n");
952 static void free_event_desc(struct perf_evsel *events)
954 struct perf_evsel *evsel;
959 for (evsel = events; evsel->attr.size; evsel++) {
967 static struct perf_evsel *
968 read_event_desc(struct perf_header *ph, int fd)
970 struct perf_evsel *evsel, *events = NULL;
973 u32 nre, sz, nr, i, j;
977 /* number of events */
978 ret = readn(fd, &nre, sizeof(nre));
979 if (ret != (ssize_t)sizeof(nre))
985 ret = readn(fd, &sz, sizeof(sz));
986 if (ret != (ssize_t)sizeof(sz))
992 /* buffer to hold on file attr struct */
997 /* the last event terminates with evsel->attr.size == 0: */
998 events = calloc(nre + 1, sizeof(*events));
1002 msz = sizeof(evsel->attr);
1006 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1010 * must read entire on-file attr struct to
1011 * sync up with layout.
1013 ret = readn(fd, buf, sz);
1014 if (ret != (ssize_t)sz)
1018 perf_event__attr_swap(buf);
1020 memcpy(&evsel->attr, buf, msz);
1022 ret = readn(fd, &nr, sizeof(nr));
1023 if (ret != (ssize_t)sizeof(nr))
1026 if (ph->needs_swap) {
1028 evsel->needs_swap = true;
1031 evsel->name = do_read_string(fd, ph);
1036 id = calloc(nr, sizeof(*id));
1042 for (j = 0 ; j < nr; j++) {
1043 ret = readn(fd, id, sizeof(*id));
1044 if (ret != (ssize_t)sizeof(*id))
1047 *id = bswap_64(*id);
1055 free_event_desc(events);
1060 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1061 void *priv __attribute__((unused)))
1063 return fprintf(fp, ", %s = %s", name, val);
1066 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1068 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1073 fprintf(fp, "# event desc: not available or unable to read\n");
1077 for (evsel = events; evsel->attr.size; evsel++) {
1078 fprintf(fp, "# event : name = %s, ", evsel->name);
1081 fprintf(fp, ", id = {");
1082 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1085 fprintf(fp, " %"PRIu64, *id);
1090 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1095 free_event_desc(events);
1098 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1101 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1104 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1109 uint64_t mem_total, mem_free;
1112 nr = ph->env.nr_numa_nodes;
1113 str = ph->env.numa_nodes;
1115 for (i = 0; i < nr; i++) {
1117 c = strtoul(str, &tmp, 0);
1122 mem_total = strtoull(str, &tmp, 0);
1127 mem_free = strtoull(str, &tmp, 0);
1131 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1132 " free = %"PRIu64" kB\n",
1133 c, mem_total, mem_free);
1136 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1138 str += strlen(str) + 1;
1142 fprintf(fp, "# numa topology : not available\n");
1145 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1147 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1150 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1151 int fd __maybe_unused, FILE *fp)
1153 fprintf(fp, "# contains samples with branch stack\n");
1156 static void print_auxtrace(struct perf_header *ph __maybe_unused,
1157 int fd __maybe_unused, FILE *fp)
1159 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1162 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1165 const char *delimiter = "# pmu mappings: ";
1170 pmu_num = ph->env.nr_pmu_mappings;
1172 fprintf(fp, "# pmu mappings: not available\n");
1176 str = ph->env.pmu_mappings;
1179 type = strtoul(str, &tmp, 0);
1184 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1187 str += strlen(str) + 1;
1196 fprintf(fp, "# pmu mappings: unable to read\n");
1199 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1202 struct perf_session *session;
1203 struct perf_evsel *evsel;
1206 session = container_of(ph, struct perf_session, header);
1208 evlist__for_each(session->evlist, evsel) {
1209 if (perf_evsel__is_group_leader(evsel) &&
1210 evsel->nr_members > 1) {
1211 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1212 perf_evsel__name(evsel));
1214 nr = evsel->nr_members - 1;
1216 fprintf(fp, ",%s", perf_evsel__name(evsel));
1224 static int __event_process_build_id(struct build_id_event *bev,
1226 struct perf_session *session)
1229 struct machine *machine;
1232 enum dso_kernel_type dso_type;
1234 machine = perf_session__findnew_machine(session, bev->pid);
1238 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1241 case PERF_RECORD_MISC_KERNEL:
1242 dso_type = DSO_TYPE_KERNEL;
1244 case PERF_RECORD_MISC_GUEST_KERNEL:
1245 dso_type = DSO_TYPE_GUEST_KERNEL;
1247 case PERF_RECORD_MISC_USER:
1248 case PERF_RECORD_MISC_GUEST_USER:
1249 dso_type = DSO_TYPE_USER;
1255 dso = machine__findnew_dso(machine, filename);
1257 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1259 dso__set_build_id(dso, &bev->build_id);
1261 if (dso_type != DSO_TYPE_USER) {
1262 struct kmod_path m = { .name = NULL, };
1264 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1265 dso__set_short_name(dso, strdup(m.name), true);
1267 dso->kernel = dso_type;
1272 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1274 pr_debug("build id event received for %s: %s\n",
1275 dso->long_name, sbuild_id);
1284 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1285 int input, u64 offset, u64 size)
1287 struct perf_session *session = container_of(header, struct perf_session, header);
1289 struct perf_event_header header;
1290 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1293 struct build_id_event bev;
1294 char filename[PATH_MAX];
1295 u64 limit = offset + size;
1297 while (offset < limit) {
1300 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1303 if (header->needs_swap)
1304 perf_event_header__bswap(&old_bev.header);
1306 len = old_bev.header.size - sizeof(old_bev);
1307 if (readn(input, filename, len) != len)
1310 bev.header = old_bev.header;
1313 * As the pid is the missing value, we need to fill
1314 * it properly. The header.misc value give us nice hint.
1316 bev.pid = HOST_KERNEL_ID;
1317 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1318 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1319 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1321 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1322 __event_process_build_id(&bev, filename, session);
1324 offset += bev.header.size;
1330 static int perf_header__read_build_ids(struct perf_header *header,
1331 int input, u64 offset, u64 size)
1333 struct perf_session *session = container_of(header, struct perf_session, header);
1334 struct build_id_event bev;
1335 char filename[PATH_MAX];
1336 u64 limit = offset + size, orig_offset = offset;
1339 while (offset < limit) {
1342 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1345 if (header->needs_swap)
1346 perf_event_header__bswap(&bev.header);
1348 len = bev.header.size - sizeof(bev);
1349 if (readn(input, filename, len) != len)
1352 * The a1645ce1 changeset:
1354 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1356 * Added a field to struct build_id_event that broke the file
1359 * Since the kernel build-id is the first entry, process the
1360 * table using the old format if the well known
1361 * '[kernel.kallsyms]' string for the kernel build-id has the
1362 * first 4 characters chopped off (where the pid_t sits).
1364 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1365 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1367 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1370 __event_process_build_id(&bev, filename, session);
1372 offset += bev.header.size;
1379 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1380 struct perf_header *ph __maybe_unused,
1383 ssize_t ret = trace_report(fd, data, false);
1384 return ret < 0 ? -1 : 0;
1387 static int process_build_id(struct perf_file_section *section,
1388 struct perf_header *ph, int fd,
1389 void *data __maybe_unused)
1391 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1392 pr_debug("Failed to read buildids, continuing...\n");
1396 static int process_hostname(struct perf_file_section *section __maybe_unused,
1397 struct perf_header *ph, int fd,
1398 void *data __maybe_unused)
1400 ph->env.hostname = do_read_string(fd, ph);
1401 return ph->env.hostname ? 0 : -ENOMEM;
1404 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1405 struct perf_header *ph, int fd,
1406 void *data __maybe_unused)
1408 ph->env.os_release = do_read_string(fd, ph);
1409 return ph->env.os_release ? 0 : -ENOMEM;
1412 static int process_version(struct perf_file_section *section __maybe_unused,
1413 struct perf_header *ph, int fd,
1414 void *data __maybe_unused)
1416 ph->env.version = do_read_string(fd, ph);
1417 return ph->env.version ? 0 : -ENOMEM;
1420 static int process_arch(struct perf_file_section *section __maybe_unused,
1421 struct perf_header *ph, int fd,
1422 void *data __maybe_unused)
1424 ph->env.arch = do_read_string(fd, ph);
1425 return ph->env.arch ? 0 : -ENOMEM;
1428 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1429 struct perf_header *ph, int fd,
1430 void *data __maybe_unused)
1435 ret = readn(fd, &nr, sizeof(nr));
1436 if (ret != sizeof(nr))
1442 ph->env.nr_cpus_avail = nr;
1444 ret = readn(fd, &nr, sizeof(nr));
1445 if (ret != sizeof(nr))
1451 ph->env.nr_cpus_online = nr;
1455 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1456 struct perf_header *ph, int fd,
1457 void *data __maybe_unused)
1459 ph->env.cpu_desc = do_read_string(fd, ph);
1460 return ph->env.cpu_desc ? 0 : -ENOMEM;
1463 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1464 struct perf_header *ph, int fd,
1465 void *data __maybe_unused)
1467 ph->env.cpuid = do_read_string(fd, ph);
1468 return ph->env.cpuid ? 0 : -ENOMEM;
1471 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1472 struct perf_header *ph, int fd,
1473 void *data __maybe_unused)
1478 ret = readn(fd, &mem, sizeof(mem));
1479 if (ret != sizeof(mem))
1483 mem = bswap_64(mem);
1485 ph->env.total_mem = mem;
1489 static struct perf_evsel *
1490 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1492 struct perf_evsel *evsel;
1494 evlist__for_each(evlist, evsel) {
1495 if (evsel->idx == idx)
1503 perf_evlist__set_event_name(struct perf_evlist *evlist,
1504 struct perf_evsel *event)
1506 struct perf_evsel *evsel;
1511 evsel = perf_evlist__find_by_index(evlist, event->idx);
1518 evsel->name = strdup(event->name);
1522 process_event_desc(struct perf_file_section *section __maybe_unused,
1523 struct perf_header *header, int fd,
1524 void *data __maybe_unused)
1526 struct perf_session *session;
1527 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1532 session = container_of(header, struct perf_session, header);
1533 for (evsel = events; evsel->attr.size; evsel++)
1534 perf_evlist__set_event_name(session->evlist, evsel);
1536 free_event_desc(events);
1541 static int process_cmdline(struct perf_file_section *section,
1542 struct perf_header *ph, int fd,
1543 void *data __maybe_unused)
1546 char *str, *cmdline = NULL, **argv = NULL;
1549 ret = readn(fd, &nr, sizeof(nr));
1550 if (ret != sizeof(nr))
1556 ph->env.nr_cmdline = nr;
1558 cmdline = zalloc(section->size + nr + 1);
1562 argv = zalloc(sizeof(char *) * (nr + 1));
1566 for (i = 0; i < nr; i++) {
1567 str = do_read_string(fd, ph);
1571 argv[i] = cmdline + len;
1572 memcpy(argv[i], str, strlen(str) + 1);
1573 len += strlen(str) + 1;
1576 ph->env.cmdline = cmdline;
1577 ph->env.cmdline_argv = (const char **) argv;
1586 static int process_cpu_topology(struct perf_file_section *section,
1587 struct perf_header *ph, int fd,
1588 void *data __maybe_unused)
1594 int cpu_nr = ph->env.nr_cpus_online;
1597 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1601 ret = readn(fd, &nr, sizeof(nr));
1602 if (ret != sizeof(nr))
1608 ph->env.nr_sibling_cores = nr;
1609 size += sizeof(u32);
1610 strbuf_init(&sb, 128);
1612 for (i = 0; i < nr; i++) {
1613 str = do_read_string(fd, ph);
1617 /* include a NULL character at the end */
1618 strbuf_add(&sb, str, strlen(str) + 1);
1619 size += string_size(str);
1622 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1624 ret = readn(fd, &nr, sizeof(nr));
1625 if (ret != sizeof(nr))
1631 ph->env.nr_sibling_threads = nr;
1632 size += sizeof(u32);
1634 for (i = 0; i < nr; i++) {
1635 str = do_read_string(fd, ph);
1639 /* include a NULL character at the end */
1640 strbuf_add(&sb, str, strlen(str) + 1);
1641 size += string_size(str);
1644 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1647 * The header may be from old perf,
1648 * which doesn't include core id and socket id information.
1650 if (section->size <= size) {
1651 zfree(&ph->env.cpu);
1655 for (i = 0; i < (u32)cpu_nr; i++) {
1656 ret = readn(fd, &nr, sizeof(nr));
1657 if (ret != sizeof(nr))
1663 if (nr > (u32)cpu_nr) {
1664 pr_debug("core_id number is too big."
1665 "You may need to upgrade the perf tool.\n");
1668 ph->env.cpu[i].core_id = nr;
1670 ret = readn(fd, &nr, sizeof(nr));
1671 if (ret != sizeof(nr))
1677 if (nr > (u32)cpu_nr) {
1678 pr_debug("socket_id number is too big."
1679 "You may need to upgrade the perf tool.\n");
1683 ph->env.cpu[i].socket_id = nr;
1689 strbuf_release(&sb);
1691 zfree(&ph->env.cpu);
1695 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1696 struct perf_header *ph, int fd,
1697 void *data __maybe_unused)
1702 uint64_t mem_total, mem_free;
1706 ret = readn(fd, &nr, sizeof(nr));
1707 if (ret != sizeof(nr))
1713 ph->env.nr_numa_nodes = nr;
1714 strbuf_init(&sb, 256);
1716 for (i = 0; i < nr; i++) {
1718 ret = readn(fd, &node, sizeof(node));
1719 if (ret != sizeof(node))
1722 ret = readn(fd, &mem_total, sizeof(u64));
1723 if (ret != sizeof(u64))
1726 ret = readn(fd, &mem_free, sizeof(u64));
1727 if (ret != sizeof(u64))
1730 if (ph->needs_swap) {
1731 node = bswap_32(node);
1732 mem_total = bswap_64(mem_total);
1733 mem_free = bswap_64(mem_free);
1736 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1737 node, mem_total, mem_free);
1739 str = do_read_string(fd, ph);
1743 /* include a NULL character at the end */
1744 strbuf_add(&sb, str, strlen(str) + 1);
1747 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1751 strbuf_release(&sb);
1755 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1756 struct perf_header *ph, int fd,
1757 void *data __maybe_unused)
1765 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1766 if (ret != sizeof(pmu_num))
1770 pmu_num = bswap_32(pmu_num);
1773 pr_debug("pmu mappings not available\n");
1777 ph->env.nr_pmu_mappings = pmu_num;
1778 strbuf_init(&sb, 128);
1781 if (readn(fd, &type, sizeof(type)) != sizeof(type))
1784 type = bswap_32(type);
1786 name = do_read_string(fd, ph);
1790 strbuf_addf(&sb, "%u:%s", type, name);
1791 /* include a NULL character at the end */
1792 strbuf_add(&sb, "", 1);
1794 if (!strcmp(name, "msr"))
1795 ph->env.msr_pmu_type = type;
1800 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1804 strbuf_release(&sb);
1808 static int process_group_desc(struct perf_file_section *section __maybe_unused,
1809 struct perf_header *ph, int fd,
1810 void *data __maybe_unused)
1813 u32 i, nr, nr_groups;
1814 struct perf_session *session;
1815 struct perf_evsel *evsel, *leader = NULL;
1822 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1826 nr_groups = bswap_32(nr_groups);
1828 ph->env.nr_groups = nr_groups;
1830 pr_debug("group desc not available\n");
1834 desc = calloc(nr_groups, sizeof(*desc));
1838 for (i = 0; i < nr_groups; i++) {
1839 desc[i].name = do_read_string(fd, ph);
1843 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1846 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1849 if (ph->needs_swap) {
1850 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1851 desc[i].nr_members = bswap_32(desc[i].nr_members);
1856 * Rebuild group relationship based on the group_desc
1858 session = container_of(ph, struct perf_session, header);
1859 session->evlist->nr_groups = nr_groups;
1862 evlist__for_each(session->evlist, evsel) {
1863 if (evsel->idx == (int) desc[i].leader_idx) {
1864 evsel->leader = evsel;
1865 /* {anon_group} is a dummy name */
1866 if (strcmp(desc[i].name, "{anon_group}")) {
1867 evsel->group_name = desc[i].name;
1868 desc[i].name = NULL;
1870 evsel->nr_members = desc[i].nr_members;
1872 if (i >= nr_groups || nr > 0) {
1873 pr_debug("invalid group desc\n");
1878 nr = evsel->nr_members - 1;
1881 /* This is a group member */
1882 evsel->leader = leader;
1888 if (i != nr_groups || nr != 0) {
1889 pr_debug("invalid group desc\n");
1895 for (i = 0; i < nr_groups; i++)
1896 zfree(&desc[i].name);
1902 static int process_auxtrace(struct perf_file_section *section,
1903 struct perf_header *ph, int fd,
1904 void *data __maybe_unused)
1906 struct perf_session *session;
1909 session = container_of(ph, struct perf_session, header);
1911 err = auxtrace_index__process(fd, section->size, session,
1914 pr_err("Failed to process auxtrace index\n");
1918 struct feature_ops {
1919 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1920 void (*print)(struct perf_header *h, int fd, FILE *fp);
1921 int (*process)(struct perf_file_section *section,
1922 struct perf_header *h, int fd, void *data);
1927 #define FEAT_OPA(n, func) \
1928 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1929 #define FEAT_OPP(n, func) \
1930 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1931 .process = process_##func }
1932 #define FEAT_OPF(n, func) \
1933 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1934 .process = process_##func, .full_only = true }
1936 /* feature_ops not implemented: */
1937 #define print_tracing_data NULL
1938 #define print_build_id NULL
1940 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1941 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1942 FEAT_OPP(HEADER_BUILD_ID, build_id),
1943 FEAT_OPP(HEADER_HOSTNAME, hostname),
1944 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1945 FEAT_OPP(HEADER_VERSION, version),
1946 FEAT_OPP(HEADER_ARCH, arch),
1947 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1948 FEAT_OPP(HEADER_CPUDESC, cpudesc),
1949 FEAT_OPP(HEADER_CPUID, cpuid),
1950 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
1951 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
1952 FEAT_OPP(HEADER_CMDLINE, cmdline),
1953 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1954 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1955 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
1956 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
1957 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
1958 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
1961 struct header_print_data {
1963 bool full; /* extended list of headers */
1966 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1967 struct perf_header *ph,
1968 int feat, int fd, void *data)
1970 struct header_print_data *hd = data;
1972 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1973 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1974 "%d, continuing...\n", section->offset, feat);
1977 if (feat >= HEADER_LAST_FEATURE) {
1978 pr_warning("unknown feature %d\n", feat);
1981 if (!feat_ops[feat].print)
1984 if (!feat_ops[feat].full_only || hd->full)
1985 feat_ops[feat].print(ph, fd, hd->fp);
1987 fprintf(hd->fp, "# %s info available, use -I to display\n",
1988 feat_ops[feat].name);
1993 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1995 struct header_print_data hd;
1996 struct perf_header *header = &session->header;
1997 int fd = perf_data_file__fd(session->file);
2001 perf_header__process_sections(header, fd, &hd,
2002 perf_file_section__fprintf_info);
2006 static int do_write_feat(int fd, struct perf_header *h, int type,
2007 struct perf_file_section **p,
2008 struct perf_evlist *evlist)
2013 if (perf_header__has_feat(h, type)) {
2014 if (!feat_ops[type].write)
2017 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2019 err = feat_ops[type].write(fd, h, evlist);
2021 pr_debug("failed to write feature %d\n", type);
2023 /* undo anything written */
2024 lseek(fd, (*p)->offset, SEEK_SET);
2028 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2034 static int perf_header__adds_write(struct perf_header *header,
2035 struct perf_evlist *evlist, int fd)
2038 struct perf_file_section *feat_sec, *p;
2044 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2048 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2049 if (feat_sec == NULL)
2052 sec_size = sizeof(*feat_sec) * nr_sections;
2054 sec_start = header->feat_offset;
2055 lseek(fd, sec_start + sec_size, SEEK_SET);
2057 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2058 if (do_write_feat(fd, header, feat, &p, evlist))
2059 perf_header__clear_feat(header, feat);
2062 lseek(fd, sec_start, SEEK_SET);
2064 * may write more than needed due to dropped feature, but
2065 * this is okay, reader will skip the mising entries
2067 err = do_write(fd, feat_sec, sec_size);
2069 pr_debug("failed to write feature section\n");
2074 int perf_header__write_pipe(int fd)
2076 struct perf_pipe_file_header f_header;
2079 f_header = (struct perf_pipe_file_header){
2080 .magic = PERF_MAGIC,
2081 .size = sizeof(f_header),
2084 err = do_write(fd, &f_header, sizeof(f_header));
2086 pr_debug("failed to write perf pipe header\n");
2093 int perf_session__write_header(struct perf_session *session,
2094 struct perf_evlist *evlist,
2095 int fd, bool at_exit)
2097 struct perf_file_header f_header;
2098 struct perf_file_attr f_attr;
2099 struct perf_header *header = &session->header;
2100 struct perf_evsel *evsel;
2104 lseek(fd, sizeof(f_header), SEEK_SET);
2106 evlist__for_each(session->evlist, evsel) {
2107 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2108 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2110 pr_debug("failed to write perf header\n");
2115 attr_offset = lseek(fd, 0, SEEK_CUR);
2117 evlist__for_each(evlist, evsel) {
2118 f_attr = (struct perf_file_attr){
2119 .attr = evsel->attr,
2121 .offset = evsel->id_offset,
2122 .size = evsel->ids * sizeof(u64),
2125 err = do_write(fd, &f_attr, sizeof(f_attr));
2127 pr_debug("failed to write perf header attribute\n");
2132 if (!header->data_offset)
2133 header->data_offset = lseek(fd, 0, SEEK_CUR);
2134 header->feat_offset = header->data_offset + header->data_size;
2137 err = perf_header__adds_write(header, evlist, fd);
2142 f_header = (struct perf_file_header){
2143 .magic = PERF_MAGIC,
2144 .size = sizeof(f_header),
2145 .attr_size = sizeof(f_attr),
2147 .offset = attr_offset,
2148 .size = evlist->nr_entries * sizeof(f_attr),
2151 .offset = header->data_offset,
2152 .size = header->data_size,
2154 /* event_types is ignored, store zeros */
2157 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2159 lseek(fd, 0, SEEK_SET);
2160 err = do_write(fd, &f_header, sizeof(f_header));
2162 pr_debug("failed to write perf header\n");
2165 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2170 static int perf_header__getbuffer64(struct perf_header *header,
2171 int fd, void *buf, size_t size)
2173 if (readn(fd, buf, size) <= 0)
2176 if (header->needs_swap)
2177 mem_bswap_64(buf, size);
2182 int perf_header__process_sections(struct perf_header *header, int fd,
2184 int (*process)(struct perf_file_section *section,
2185 struct perf_header *ph,
2186 int feat, int fd, void *data))
2188 struct perf_file_section *feat_sec, *sec;
2194 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2198 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2202 sec_size = sizeof(*feat_sec) * nr_sections;
2204 lseek(fd, header->feat_offset, SEEK_SET);
2206 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2210 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2211 err = process(sec++, header, feat, fd, data);
2221 static const int attr_file_abi_sizes[] = {
2222 [0] = PERF_ATTR_SIZE_VER0,
2223 [1] = PERF_ATTR_SIZE_VER1,
2224 [2] = PERF_ATTR_SIZE_VER2,
2225 [3] = PERF_ATTR_SIZE_VER3,
2226 [4] = PERF_ATTR_SIZE_VER4,
2231 * In the legacy file format, the magic number is not used to encode endianness.
2232 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2233 * on ABI revisions, we need to try all combinations for all endianness to
2234 * detect the endianness.
2236 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2238 uint64_t ref_size, attr_size;
2241 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2242 ref_size = attr_file_abi_sizes[i]
2243 + sizeof(struct perf_file_section);
2244 if (hdr_sz != ref_size) {
2245 attr_size = bswap_64(hdr_sz);
2246 if (attr_size != ref_size)
2249 ph->needs_swap = true;
2251 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2256 /* could not determine endianness */
2260 #define PERF_PIPE_HDR_VER0 16
2262 static const size_t attr_pipe_abi_sizes[] = {
2263 [0] = PERF_PIPE_HDR_VER0,
2268 * In the legacy pipe format, there is an implicit assumption that endiannesss
2269 * between host recording the samples, and host parsing the samples is the
2270 * same. This is not always the case given that the pipe output may always be
2271 * redirected into a file and analyzed on a different machine with possibly a
2272 * different endianness and perf_event ABI revsions in the perf tool itself.
2274 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2279 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2280 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2281 attr_size = bswap_64(hdr_sz);
2282 if (attr_size != hdr_sz)
2285 ph->needs_swap = true;
2287 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2293 bool is_perf_magic(u64 magic)
2295 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2296 || magic == __perf_magic2
2297 || magic == __perf_magic2_sw)
2303 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2304 bool is_pipe, struct perf_header *ph)
2308 /* check for legacy format */
2309 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2311 ph->version = PERF_HEADER_VERSION_1;
2312 pr_debug("legacy perf.data format\n");
2314 return try_all_pipe_abis(hdr_sz, ph);
2316 return try_all_file_abis(hdr_sz, ph);
2319 * the new magic number serves two purposes:
2320 * - unique number to identify actual perf.data files
2321 * - encode endianness of file
2323 ph->version = PERF_HEADER_VERSION_2;
2325 /* check magic number with one endianness */
2326 if (magic == __perf_magic2)
2329 /* check magic number with opposite endianness */
2330 if (magic != __perf_magic2_sw)
2333 ph->needs_swap = true;
2338 int perf_file_header__read(struct perf_file_header *header,
2339 struct perf_header *ph, int fd)
2343 lseek(fd, 0, SEEK_SET);
2345 ret = readn(fd, header, sizeof(*header));
2349 if (check_magic_endian(header->magic,
2350 header->attr_size, false, ph) < 0) {
2351 pr_debug("magic/endian check failed\n");
2355 if (ph->needs_swap) {
2356 mem_bswap_64(header, offsetof(struct perf_file_header,
2360 if (header->size != sizeof(*header)) {
2361 /* Support the previous format */
2362 if (header->size == offsetof(typeof(*header), adds_features))
2363 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2366 } else if (ph->needs_swap) {
2368 * feature bitmap is declared as an array of unsigned longs --
2369 * not good since its size can differ between the host that
2370 * generated the data file and the host analyzing the file.
2372 * We need to handle endianness, but we don't know the size of
2373 * the unsigned long where the file was generated. Take a best
2374 * guess at determining it: try 64-bit swap first (ie., file
2375 * created on a 64-bit host), and check if the hostname feature
2376 * bit is set (this feature bit is forced on as of fbe96f2).
2377 * If the bit is not, undo the 64-bit swap and try a 32-bit
2378 * swap. If the hostname bit is still not set (e.g., older data
2379 * file), punt and fallback to the original behavior --
2380 * clearing all feature bits and setting buildid.
2382 mem_bswap_64(&header->adds_features,
2383 BITS_TO_U64(HEADER_FEAT_BITS));
2385 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2387 mem_bswap_64(&header->adds_features,
2388 BITS_TO_U64(HEADER_FEAT_BITS));
2391 mem_bswap_32(&header->adds_features,
2392 BITS_TO_U32(HEADER_FEAT_BITS));
2395 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2396 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2397 set_bit(HEADER_BUILD_ID, header->adds_features);
2401 memcpy(&ph->adds_features, &header->adds_features,
2402 sizeof(ph->adds_features));
2404 ph->data_offset = header->data.offset;
2405 ph->data_size = header->data.size;
2406 ph->feat_offset = header->data.offset + header->data.size;
2410 static int perf_file_section__process(struct perf_file_section *section,
2411 struct perf_header *ph,
2412 int feat, int fd, void *data)
2414 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2415 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2416 "%d, continuing...\n", section->offset, feat);
2420 if (feat >= HEADER_LAST_FEATURE) {
2421 pr_debug("unknown feature %d, continuing...\n", feat);
2425 if (!feat_ops[feat].process)
2428 return feat_ops[feat].process(section, ph, fd, data);
2431 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2432 struct perf_header *ph, int fd,
2437 ret = readn(fd, header, sizeof(*header));
2441 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2442 pr_debug("endian/magic failed\n");
2447 header->size = bswap_64(header->size);
2449 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2455 static int perf_header__read_pipe(struct perf_session *session)
2457 struct perf_header *header = &session->header;
2458 struct perf_pipe_file_header f_header;
2460 if (perf_file_header__read_pipe(&f_header, header,
2461 perf_data_file__fd(session->file),
2462 session->repipe) < 0) {
2463 pr_debug("incompatible file format\n");
2470 static int read_attr(int fd, struct perf_header *ph,
2471 struct perf_file_attr *f_attr)
2473 struct perf_event_attr *attr = &f_attr->attr;
2475 size_t our_sz = sizeof(f_attr->attr);
2478 memset(f_attr, 0, sizeof(*f_attr));
2480 /* read minimal guaranteed structure */
2481 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2483 pr_debug("cannot read %d bytes of header attr\n",
2484 PERF_ATTR_SIZE_VER0);
2488 /* on file perf_event_attr size */
2496 sz = PERF_ATTR_SIZE_VER0;
2497 } else if (sz > our_sz) {
2498 pr_debug("file uses a more recent and unsupported ABI"
2499 " (%zu bytes extra)\n", sz - our_sz);
2502 /* what we have not yet read and that we know about */
2503 left = sz - PERF_ATTR_SIZE_VER0;
2506 ptr += PERF_ATTR_SIZE_VER0;
2508 ret = readn(fd, ptr, left);
2510 /* read perf_file_section, ids are read in caller */
2511 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2513 return ret <= 0 ? -1 : 0;
2516 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2517 struct pevent *pevent)
2519 struct event_format *event;
2522 /* already prepared */
2523 if (evsel->tp_format)
2526 if (pevent == NULL) {
2527 pr_debug("broken or missing trace data\n");
2531 event = pevent_find_event(pevent, evsel->attr.config);
2536 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2537 evsel->name = strdup(bf);
2538 if (evsel->name == NULL)
2542 evsel->tp_format = event;
2546 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2547 struct pevent *pevent)
2549 struct perf_evsel *pos;
2551 evlist__for_each(evlist, pos) {
2552 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2553 perf_evsel__prepare_tracepoint_event(pos, pevent))
2560 int perf_session__read_header(struct perf_session *session)
2562 struct perf_data_file *file = session->file;
2563 struct perf_header *header = &session->header;
2564 struct perf_file_header f_header;
2565 struct perf_file_attr f_attr;
2567 int nr_attrs, nr_ids, i, j;
2568 int fd = perf_data_file__fd(file);
2570 session->evlist = perf_evlist__new();
2571 if (session->evlist == NULL)
2574 session->evlist->env = &header->env;
2575 session->machines.host.env = &header->env;
2576 if (perf_data_file__is_pipe(file))
2577 return perf_header__read_pipe(session);
2579 if (perf_file_header__read(&f_header, header, fd) < 0)
2583 * Sanity check that perf.data was written cleanly; data size is
2584 * initialized to 0 and updated only if the on_exit function is run.
2585 * If data size is still 0 then the file contains only partial
2586 * information. Just warn user and process it as much as it can.
2588 if (f_header.data.size == 0) {
2589 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2590 "Was the 'perf record' command properly terminated?\n",
2594 nr_attrs = f_header.attrs.size / f_header.attr_size;
2595 lseek(fd, f_header.attrs.offset, SEEK_SET);
2597 for (i = 0; i < nr_attrs; i++) {
2598 struct perf_evsel *evsel;
2601 if (read_attr(fd, header, &f_attr) < 0)
2604 if (header->needs_swap) {
2605 f_attr.ids.size = bswap_64(f_attr.ids.size);
2606 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2607 perf_event__attr_swap(&f_attr.attr);
2610 tmp = lseek(fd, 0, SEEK_CUR);
2611 evsel = perf_evsel__new(&f_attr.attr);
2614 goto out_delete_evlist;
2616 evsel->needs_swap = header->needs_swap;
2618 * Do it before so that if perf_evsel__alloc_id fails, this
2619 * entry gets purged too at perf_evlist__delete().
2621 perf_evlist__add(session->evlist, evsel);
2623 nr_ids = f_attr.ids.size / sizeof(u64);
2625 * We don't have the cpu and thread maps on the header, so
2626 * for allocating the perf_sample_id table we fake 1 cpu and
2627 * hattr->ids threads.
2629 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2630 goto out_delete_evlist;
2632 lseek(fd, f_attr.ids.offset, SEEK_SET);
2634 for (j = 0; j < nr_ids; j++) {
2635 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2638 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2641 lseek(fd, tmp, SEEK_SET);
2644 symbol_conf.nr_events = nr_attrs;
2646 perf_header__process_sections(header, fd, &session->tevent,
2647 perf_file_section__process);
2649 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2650 session->tevent.pevent))
2651 goto out_delete_evlist;
2658 perf_evlist__delete(session->evlist);
2659 session->evlist = NULL;
2663 int perf_event__synthesize_attr(struct perf_tool *tool,
2664 struct perf_event_attr *attr, u32 ids, u64 *id,
2665 perf_event__handler_t process)
2667 union perf_event *ev;
2671 size = sizeof(struct perf_event_attr);
2672 size = PERF_ALIGN(size, sizeof(u64));
2673 size += sizeof(struct perf_event_header);
2674 size += ids * sizeof(u64);
2681 ev->attr.attr = *attr;
2682 memcpy(ev->attr.id, id, ids * sizeof(u64));
2684 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2685 ev->attr.header.size = (u16)size;
2687 if (ev->attr.header.size == size)
2688 err = process(tool, ev, NULL, NULL);
2697 int perf_event__synthesize_attrs(struct perf_tool *tool,
2698 struct perf_session *session,
2699 perf_event__handler_t process)
2701 struct perf_evsel *evsel;
2704 evlist__for_each(session->evlist, evsel) {
2705 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2706 evsel->id, process);
2708 pr_debug("failed to create perf header attribute\n");
2716 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2717 union perf_event *event,
2718 struct perf_evlist **pevlist)
2721 struct perf_evsel *evsel;
2722 struct perf_evlist *evlist = *pevlist;
2724 if (evlist == NULL) {
2725 *pevlist = evlist = perf_evlist__new();
2730 evsel = perf_evsel__new(&event->attr.attr);
2734 perf_evlist__add(evlist, evsel);
2736 ids = event->header.size;
2737 ids -= (void *)&event->attr.id - (void *)event;
2738 n_ids = ids / sizeof(u64);
2740 * We don't have the cpu and thread maps on the header, so
2741 * for allocating the perf_sample_id table we fake 1 cpu and
2742 * hattr->ids threads.
2744 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2747 for (i = 0; i < n_ids; i++) {
2748 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2751 symbol_conf.nr_events = evlist->nr_entries;
2756 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2757 struct perf_evlist *evlist,
2758 perf_event__handler_t process)
2760 union perf_event ev;
2761 struct tracing_data *tdata;
2762 ssize_t size = 0, aligned_size = 0, padding;
2763 int err __maybe_unused = 0;
2766 * We are going to store the size of the data followed
2767 * by the data contents. Since the fd descriptor is a pipe,
2768 * we cannot seek back to store the size of the data once
2769 * we know it. Instead we:
2771 * - write the tracing data to the temp file
2772 * - get/write the data size to pipe
2773 * - write the tracing data from the temp file
2776 tdata = tracing_data_get(&evlist->entries, fd, true);
2780 memset(&ev, 0, sizeof(ev));
2782 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2784 aligned_size = PERF_ALIGN(size, sizeof(u64));
2785 padding = aligned_size - size;
2786 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2787 ev.tracing_data.size = aligned_size;
2789 process(tool, &ev, NULL, NULL);
2792 * The put function will copy all the tracing data
2793 * stored in temp file to the pipe.
2795 tracing_data_put(tdata);
2797 write_padded(fd, NULL, 0, padding);
2799 return aligned_size;
2802 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2803 union perf_event *event,
2804 struct perf_session *session)
2806 ssize_t size_read, padding, size = event->tracing_data.size;
2807 int fd = perf_data_file__fd(session->file);
2808 off_t offset = lseek(fd, 0, SEEK_CUR);
2811 /* setup for reading amidst mmap */
2812 lseek(fd, offset + sizeof(struct tracing_data_event),
2815 size_read = trace_report(fd, &session->tevent,
2817 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2819 if (readn(fd, buf, padding) < 0) {
2820 pr_err("%s: reading input file", __func__);
2823 if (session->repipe) {
2824 int retw = write(STDOUT_FILENO, buf, padding);
2825 if (retw <= 0 || retw != padding) {
2826 pr_err("%s: repiping tracing data padding", __func__);
2831 if (size_read + padding != size) {
2832 pr_err("%s: tracing data size mismatch", __func__);
2836 perf_evlist__prepare_tracepoint_events(session->evlist,
2837 session->tevent.pevent);
2839 return size_read + padding;
2842 int perf_event__synthesize_build_id(struct perf_tool *tool,
2843 struct dso *pos, u16 misc,
2844 perf_event__handler_t process,
2845 struct machine *machine)
2847 union perf_event ev;
2854 memset(&ev, 0, sizeof(ev));
2856 len = pos->long_name_len + 1;
2857 len = PERF_ALIGN(len, NAME_ALIGN);
2858 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2859 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2860 ev.build_id.header.misc = misc;
2861 ev.build_id.pid = machine->pid;
2862 ev.build_id.header.size = sizeof(ev.build_id) + len;
2863 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2865 err = process(tool, &ev, NULL, machine);
2870 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2871 union perf_event *event,
2872 struct perf_session *session)
2874 __event_process_build_id(&event->build_id,
2875 event->build_id.filename,