7 #include <linux/list.h>
8 #include <linux/kernel.h>
9 #include <linux/bitops.h>
10 #include <sys/utsname.h>
16 #include "trace-event.h"
26 static bool no_buildid_cache = false;
28 static u32 header_argc;
29 static const char **header_argv;
33 * must be a numerical value to let the endianness
34 * determine the memory layout. That way we are able
35 * to detect endianness when reading the perf.data file
38 * we check for legacy (PERFFILE) format.
40 static const char *__perf_magic1 = "PERFFILE";
41 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
42 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
44 #define PERF_MAGIC __perf_magic2
46 struct perf_file_attr {
47 struct perf_event_attr attr;
48 struct perf_file_section ids;
51 void perf_header__set_feat(struct perf_header *header, int feat)
53 set_bit(feat, header->adds_features);
56 void perf_header__clear_feat(struct perf_header *header, int feat)
58 clear_bit(feat, header->adds_features);
61 bool perf_header__has_feat(const struct perf_header *header, int feat)
63 return test_bit(feat, header->adds_features);
66 static int do_write(int fd, const void *buf, size_t size)
69 int ret = write(fd, buf, size);
83 static int write_padded(int fd, const void *bf, size_t count,
86 static const char zero_buf[NAME_ALIGN];
87 int err = do_write(fd, bf, count);
90 err = do_write(fd, zero_buf, count_aligned - count);
95 static int do_write_string(int fd, const char *str)
100 olen = strlen(str) + 1;
101 len = PERF_ALIGN(olen, NAME_ALIGN);
103 /* write len, incl. \0 */
104 ret = do_write(fd, &len, sizeof(len));
108 return write_padded(fd, str, olen, len);
111 static char *do_read_string(int fd, struct perf_header *ph)
117 sz = readn(fd, &len, sizeof(len));
118 if (sz < (ssize_t)sizeof(len))
128 ret = readn(fd, buf, len);
129 if (ret == (ssize_t)len) {
131 * strings are padded by zeroes
132 * thus the actual strlen of buf
133 * may be less than len
143 perf_header__set_cmdline(int argc, const char **argv)
148 * If header_argv has already been set, do not override it.
149 * This allows a command to set the cmdline, parse args and
150 * then call another builtin function that implements a
151 * command -- e.g, cmd_kvm calling cmd_record.
156 header_argc = (u32)argc;
158 /* do not include NULL termination */
159 header_argv = calloc(argc, sizeof(char *));
164 * must copy argv contents because it gets moved
165 * around during option parsing
167 for (i = 0; i < argc ; i++)
168 header_argv[i] = argv[i];
173 #define dsos__for_each_with_build_id(pos, head) \
174 list_for_each_entry(pos, head, node) \
175 if (!pos->has_build_id) \
179 static int write_buildid(char *name, size_t name_len, u8 *build_id,
180 pid_t pid, u16 misc, int fd)
183 struct build_id_event b;
187 len = PERF_ALIGN(len, NAME_ALIGN);
189 memset(&b, 0, sizeof(b));
190 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
192 b.header.misc = misc;
193 b.header.size = sizeof(b) + len;
195 err = do_write(fd, &b, sizeof(b));
199 return write_padded(fd, name, name_len + 1, len);
202 static int __dsos__write_buildid_table(struct list_head *head,
203 struct machine *machine,
204 pid_t pid, u16 misc, int fd)
209 dsos__for_each_with_build_id(pos, head) {
217 if (is_vdso_map(pos->short_name)) {
218 name = (char *) VDSO__MAP_NAME;
219 name_len = sizeof(VDSO__MAP_NAME) + 1;
220 } else if (dso__is_kcore(pos)) {
221 machine__mmap_name(machine, nm, sizeof(nm));
223 name_len = strlen(nm) + 1;
225 name = pos->long_name;
226 name_len = pos->long_name_len + 1;
229 err = write_buildid(name, name_len, pos->build_id,
238 static int machine__write_buildid_table(struct machine *machine, int fd)
241 u16 kmisc = PERF_RECORD_MISC_KERNEL,
242 umisc = PERF_RECORD_MISC_USER;
244 if (!machine__is_host(machine)) {
245 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
246 umisc = PERF_RECORD_MISC_GUEST_USER;
249 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine,
250 machine->pid, kmisc, fd);
252 err = __dsos__write_buildid_table(&machine->user_dsos, machine,
253 machine->pid, umisc, fd);
257 static int dsos__write_buildid_table(struct perf_header *header, int fd)
259 struct perf_session *session = container_of(header,
260 struct perf_session, header);
262 int err = machine__write_buildid_table(&session->machines.host, fd);
267 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
268 struct machine *pos = rb_entry(nd, struct machine, rb_node);
269 err = machine__write_buildid_table(pos, fd);
276 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
277 const char *name, bool is_kallsyms, bool is_vdso)
279 const size_t size = PATH_MAX;
280 char *realname, *filename = zalloc(size),
281 *linkname = zalloc(size), *targetname;
283 bool slash = is_kallsyms || is_vdso;
286 if (symbol_conf.kptr_restrict) {
287 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
291 realname = (char *) name;
293 realname = realpath(name, NULL);
295 if (realname == NULL || filename == NULL || linkname == NULL)
298 len = scnprintf(filename, size, "%s%s%s",
299 debugdir, slash ? "/" : "",
300 is_vdso ? VDSO__MAP_NAME : realname);
301 if (mkdir_p(filename, 0755))
304 snprintf(filename + len, size - len, "/%s", sbuild_id);
306 if (access(filename, F_OK)) {
308 if (copyfile("/proc/kallsyms", filename))
310 } else if (link(realname, filename) && copyfile(name, filename))
314 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
315 debugdir, sbuild_id);
317 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
320 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
321 targetname = filename + strlen(debugdir) - 5;
322 memcpy(targetname, "../..", 5);
324 if (symlink(targetname, linkname) == 0)
334 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
335 const char *name, const char *debugdir,
336 bool is_kallsyms, bool is_vdso)
338 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
340 build_id__sprintf(build_id, build_id_size, sbuild_id);
342 return build_id_cache__add_s(sbuild_id, debugdir, name,
343 is_kallsyms, is_vdso);
346 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
348 const size_t size = PATH_MAX;
349 char *filename = zalloc(size),
350 *linkname = zalloc(size);
353 if (filename == NULL || linkname == NULL)
356 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
357 debugdir, sbuild_id, sbuild_id + 2);
359 if (access(linkname, F_OK))
362 if (readlink(linkname, filename, size - 1) < 0)
365 if (unlink(linkname))
369 * Since the link is relative, we must make it absolute:
371 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
372 debugdir, sbuild_id, filename);
374 if (unlink(linkname))
384 static int dso__cache_build_id(struct dso *dso, struct machine *machine,
385 const char *debugdir)
387 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
388 bool is_vdso = is_vdso_map(dso->short_name);
389 char *name = dso->long_name;
392 if (dso__is_kcore(dso)) {
394 machine__mmap_name(machine, nm, sizeof(nm));
397 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
398 debugdir, is_kallsyms, is_vdso);
401 static int __dsos__cache_build_ids(struct list_head *head,
402 struct machine *machine, const char *debugdir)
407 dsos__for_each_with_build_id(pos, head)
408 if (dso__cache_build_id(pos, machine, debugdir))
414 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
416 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, machine,
418 ret |= __dsos__cache_build_ids(&machine->user_dsos, machine, debugdir);
422 static int perf_session__cache_build_ids(struct perf_session *session)
426 char debugdir[PATH_MAX];
428 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
430 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
433 ret = machine__cache_build_ids(&session->machines.host, debugdir);
435 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
436 struct machine *pos = rb_entry(nd, struct machine, rb_node);
437 ret |= machine__cache_build_ids(pos, debugdir);
442 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
444 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
445 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
449 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
452 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
454 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
455 struct machine *pos = rb_entry(nd, struct machine, rb_node);
456 ret |= machine__read_build_ids(pos, with_hits);
462 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
463 struct perf_evlist *evlist)
465 return read_tracing_data(fd, &evlist->entries);
469 static int write_build_id(int fd, struct perf_header *h,
470 struct perf_evlist *evlist __maybe_unused)
472 struct perf_session *session;
475 session = container_of(h, struct perf_session, header);
477 if (!perf_session__read_build_ids(session, true))
480 err = dsos__write_buildid_table(h, fd);
482 pr_debug("failed to write buildid table\n");
485 if (!no_buildid_cache)
486 perf_session__cache_build_ids(session);
491 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
492 struct perf_evlist *evlist __maybe_unused)
501 return do_write_string(fd, uts.nodename);
504 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
505 struct perf_evlist *evlist __maybe_unused)
514 return do_write_string(fd, uts.release);
517 static int write_arch(int fd, struct perf_header *h __maybe_unused,
518 struct perf_evlist *evlist __maybe_unused)
527 return do_write_string(fd, uts.machine);
530 static int write_version(int fd, struct perf_header *h __maybe_unused,
531 struct perf_evlist *evlist __maybe_unused)
533 return do_write_string(fd, perf_version_string);
536 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
537 struct perf_evlist *evlist __maybe_unused)
540 #define CPUINFO_PROC NULL
545 const char *search = CPUINFO_PROC;
552 file = fopen("/proc/cpuinfo", "r");
556 while (getline(&buf, &len, file) > 0) {
557 ret = strncmp(buf, search, strlen(search));
567 p = strchr(buf, ':');
568 if (p && *(p+1) == ' ' && *(p+2))
574 /* squash extra space characters (branding string) */
581 while (*q && isspace(*q))
584 while ((*r++ = *q++));
588 ret = do_write_string(fd, s);
595 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
596 struct perf_evlist *evlist __maybe_unused)
602 nr = sysconf(_SC_NPROCESSORS_CONF);
606 nrc = (u32)(nr & UINT_MAX);
608 nr = sysconf(_SC_NPROCESSORS_ONLN);
612 nra = (u32)(nr & UINT_MAX);
614 ret = do_write(fd, &nrc, sizeof(nrc));
618 return do_write(fd, &nra, sizeof(nra));
621 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
622 struct perf_evlist *evlist)
624 struct perf_evsel *evsel;
628 nre = evlist->nr_entries;
631 * write number of events
633 ret = do_write(fd, &nre, sizeof(nre));
638 * size of perf_event_attr struct
640 sz = (u32)sizeof(evsel->attr);
641 ret = do_write(fd, &sz, sizeof(sz));
645 list_for_each_entry(evsel, &evlist->entries, node) {
647 ret = do_write(fd, &evsel->attr, sz);
651 * write number of unique id per event
652 * there is one id per instance of an event
654 * copy into an nri to be independent of the
658 ret = do_write(fd, &nri, sizeof(nri));
663 * write event string as passed on cmdline
665 ret = do_write_string(fd, perf_evsel__name(evsel));
669 * write unique ids for this event
671 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
678 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
679 struct perf_evlist *evlist __maybe_unused)
681 char buf[MAXPATHLEN];
687 * actual atual path to perf binary
689 sprintf(proc, "/proc/%d/exe", getpid());
690 ret = readlink(proc, buf, sizeof(buf));
694 /* readlink() does not add null termination */
697 /* account for binary path */
700 ret = do_write(fd, &n, sizeof(n));
704 ret = do_write_string(fd, buf);
708 for (i = 0 ; i < header_argc; i++) {
709 ret = do_write_string(fd, header_argv[i]);
716 #define CORE_SIB_FMT \
717 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
718 #define THRD_SIB_FMT \
719 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
724 char **core_siblings;
725 char **thread_siblings;
728 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
731 char filename[MAXPATHLEN];
732 char *buf = NULL, *p;
738 sprintf(filename, CORE_SIB_FMT, cpu);
739 fp = fopen(filename, "r");
743 sret = getline(&buf, &len, fp);
748 p = strchr(buf, '\n');
752 for (i = 0; i < tp->core_sib; i++) {
753 if (!strcmp(buf, tp->core_siblings[i]))
756 if (i == tp->core_sib) {
757 tp->core_siblings[i] = buf;
765 sprintf(filename, THRD_SIB_FMT, cpu);
766 fp = fopen(filename, "r");
770 if (getline(&buf, &len, fp) <= 0)
773 p = strchr(buf, '\n');
777 for (i = 0; i < tp->thread_sib; i++) {
778 if (!strcmp(buf, tp->thread_siblings[i]))
781 if (i == tp->thread_sib) {
782 tp->thread_siblings[i] = buf;
794 static void free_cpu_topo(struct cpu_topo *tp)
801 for (i = 0 ; i < tp->core_sib; i++)
802 free(tp->core_siblings[i]);
804 for (i = 0 ; i < tp->thread_sib; i++)
805 free(tp->thread_siblings[i]);
810 static struct cpu_topo *build_cpu_topology(void)
819 ncpus = sysconf(_SC_NPROCESSORS_CONF);
823 nr = (u32)(ncpus & UINT_MAX);
825 sz = nr * sizeof(char *);
827 addr = calloc(1, sizeof(*tp) + 2 * sz);
834 tp->core_siblings = addr;
836 tp->thread_siblings = addr;
838 for (i = 0; i < nr; i++) {
839 ret = build_cpu_topo(tp, i);
850 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
851 struct perf_evlist *evlist __maybe_unused)
857 tp = build_cpu_topology();
861 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
865 for (i = 0; i < tp->core_sib; i++) {
866 ret = do_write_string(fd, tp->core_siblings[i]);
870 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
874 for (i = 0; i < tp->thread_sib; i++) {
875 ret = do_write_string(fd, tp->thread_siblings[i]);
886 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
887 struct perf_evlist *evlist __maybe_unused)
895 fp = fopen("/proc/meminfo", "r");
899 while (getline(&buf, &len, fp) > 0) {
900 ret = strncmp(buf, "MemTotal:", 9);
905 n = sscanf(buf, "%*s %"PRIu64, &mem);
907 ret = do_write(fd, &mem, sizeof(mem));
914 static int write_topo_node(int fd, int node)
916 char str[MAXPATHLEN];
918 char *buf = NULL, *p;
921 u64 mem_total, mem_free, mem;
924 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
925 fp = fopen(str, "r");
929 while (getline(&buf, &len, fp) > 0) {
930 /* skip over invalid lines */
931 if (!strchr(buf, ':'))
933 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
935 if (!strcmp(field, "MemTotal:"))
937 if (!strcmp(field, "MemFree:"))
944 ret = do_write(fd, &mem_total, sizeof(u64));
948 ret = do_write(fd, &mem_free, sizeof(u64));
953 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
955 fp = fopen(str, "r");
959 if (getline(&buf, &len, fp) <= 0)
962 p = strchr(buf, '\n');
966 ret = do_write_string(fd, buf);
974 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
975 struct perf_evlist *evlist __maybe_unused)
980 struct cpu_map *node_map = NULL;
985 fp = fopen("/sys/devices/system/node/online", "r");
989 if (getline(&buf, &len, fp) <= 0)
992 c = strchr(buf, '\n');
996 node_map = cpu_map__new(buf);
1000 nr = (u32)node_map->nr;
1002 ret = do_write(fd, &nr, sizeof(nr));
1006 for (i = 0; i < nr; i++) {
1007 j = (u32)node_map->map[i];
1008 ret = do_write(fd, &j, sizeof(j));
1012 ret = write_topo_node(fd, i);
1026 * struct pmu_mappings {
1035 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1036 struct perf_evlist *evlist __maybe_unused)
1038 struct perf_pmu *pmu = NULL;
1039 off_t offset = lseek(fd, 0, SEEK_CUR);
1043 /* write real pmu_num later */
1044 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1048 while ((pmu = perf_pmu__scan(pmu))) {
1053 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1057 ret = do_write_string(fd, pmu->name);
1062 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1064 lseek(fd, offset, SEEK_SET);
1074 * struct group_descs {
1076 * struct group_desc {
1083 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1084 struct perf_evlist *evlist)
1086 u32 nr_groups = evlist->nr_groups;
1087 struct perf_evsel *evsel;
1090 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1094 list_for_each_entry(evsel, &evlist->entries, node) {
1095 if (perf_evsel__is_group_leader(evsel) &&
1096 evsel->nr_members > 1) {
1097 const char *name = evsel->group_name ?: "{anon_group}";
1098 u32 leader_idx = evsel->idx;
1099 u32 nr_members = evsel->nr_members;
1101 ret = do_write_string(fd, name);
1105 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1109 ret = do_write(fd, &nr_members, sizeof(nr_members));
1118 * default get_cpuid(): nothing gets recorded
1119 * actual implementation must be in arch/$(ARCH)/util/header.c
1121 int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1122 size_t sz __maybe_unused)
1127 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1128 struct perf_evlist *evlist __maybe_unused)
1133 ret = get_cpuid(buffer, sizeof(buffer));
1139 return do_write_string(fd, buffer);
1142 static int write_branch_stack(int fd __maybe_unused,
1143 struct perf_header *h __maybe_unused,
1144 struct perf_evlist *evlist __maybe_unused)
1149 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1152 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1155 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1158 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1161 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1163 fprintf(fp, "# arch : %s\n", ph->env.arch);
1166 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1169 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1172 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1175 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1176 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1179 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1182 fprintf(fp, "# perf version : %s\n", ph->env.version);
1185 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1191 nr = ph->env.nr_cmdline;
1192 str = ph->env.cmdline;
1194 fprintf(fp, "# cmdline : ");
1196 for (i = 0; i < nr; i++) {
1197 fprintf(fp, "%s ", str);
1198 str += strlen(str) + 1;
1203 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1209 nr = ph->env.nr_sibling_cores;
1210 str = ph->env.sibling_cores;
1212 for (i = 0; i < nr; i++) {
1213 fprintf(fp, "# sibling cores : %s\n", str);
1214 str += strlen(str) + 1;
1217 nr = ph->env.nr_sibling_threads;
1218 str = ph->env.sibling_threads;
1220 for (i = 0; i < nr; i++) {
1221 fprintf(fp, "# sibling threads : %s\n", str);
1222 str += strlen(str) + 1;
1226 static void free_event_desc(struct perf_evsel *events)
1228 struct perf_evsel *evsel;
1233 for (evsel = events; evsel->attr.size; evsel++) {
1243 static struct perf_evsel *
1244 read_event_desc(struct perf_header *ph, int fd)
1246 struct perf_evsel *evsel, *events = NULL;
1249 u32 nre, sz, nr, i, j;
1253 /* number of events */
1254 ret = readn(fd, &nre, sizeof(nre));
1255 if (ret != (ssize_t)sizeof(nre))
1259 nre = bswap_32(nre);
1261 ret = readn(fd, &sz, sizeof(sz));
1262 if (ret != (ssize_t)sizeof(sz))
1268 /* buffer to hold on file attr struct */
1273 /* the last event terminates with evsel->attr.size == 0: */
1274 events = calloc(nre + 1, sizeof(*events));
1278 msz = sizeof(evsel->attr);
1282 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1286 * must read entire on-file attr struct to
1287 * sync up with layout.
1289 ret = readn(fd, buf, sz);
1290 if (ret != (ssize_t)sz)
1294 perf_event__attr_swap(buf);
1296 memcpy(&evsel->attr, buf, msz);
1298 ret = readn(fd, &nr, sizeof(nr));
1299 if (ret != (ssize_t)sizeof(nr))
1302 if (ph->needs_swap) {
1304 evsel->needs_swap = true;
1307 evsel->name = do_read_string(fd, ph);
1312 id = calloc(nr, sizeof(*id));
1318 for (j = 0 ; j < nr; j++) {
1319 ret = readn(fd, id, sizeof(*id));
1320 if (ret != (ssize_t)sizeof(*id))
1323 *id = bswap_64(*id);
1333 free_event_desc(events);
1338 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1340 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1345 fprintf(fp, "# event desc: not available or unable to read\n");
1349 for (evsel = events; evsel->attr.size; evsel++) {
1350 fprintf(fp, "# event : name = %s, ", evsel->name);
1352 fprintf(fp, "type = %d, config = 0x%"PRIx64
1353 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1355 (u64)evsel->attr.config,
1356 (u64)evsel->attr.config1,
1357 (u64)evsel->attr.config2);
1359 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1360 evsel->attr.exclude_user,
1361 evsel->attr.exclude_kernel);
1363 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1364 evsel->attr.exclude_host,
1365 evsel->attr.exclude_guest);
1367 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1369 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1370 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1371 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
1373 fprintf(fp, ", id = {");
1374 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1377 fprintf(fp, " %"PRIu64, *id);
1385 free_event_desc(events);
1388 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1391 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1394 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1399 uint64_t mem_total, mem_free;
1402 nr = ph->env.nr_numa_nodes;
1403 str = ph->env.numa_nodes;
1405 for (i = 0; i < nr; i++) {
1407 c = strtoul(str, &tmp, 0);
1412 mem_total = strtoull(str, &tmp, 0);
1417 mem_free = strtoull(str, &tmp, 0);
1421 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1422 " free = %"PRIu64" kB\n",
1423 c, mem_total, mem_free);
1426 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1428 str += strlen(str) + 1;
1432 fprintf(fp, "# numa topology : not available\n");
1435 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1437 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1440 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1441 int fd __maybe_unused, FILE *fp)
1443 fprintf(fp, "# contains samples with branch stack\n");
1446 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1449 const char *delimiter = "# pmu mappings: ";
1454 pmu_num = ph->env.nr_pmu_mappings;
1456 fprintf(fp, "# pmu mappings: not available\n");
1460 str = ph->env.pmu_mappings;
1463 type = strtoul(str, &tmp, 0);
1468 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1471 str += strlen(str) + 1;
1480 fprintf(fp, "# pmu mappings: unable to read\n");
1483 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1486 struct perf_session *session;
1487 struct perf_evsel *evsel;
1490 session = container_of(ph, struct perf_session, header);
1492 list_for_each_entry(evsel, &session->evlist->entries, node) {
1493 if (perf_evsel__is_group_leader(evsel) &&
1494 evsel->nr_members > 1) {
1495 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1496 perf_evsel__name(evsel));
1498 nr = evsel->nr_members - 1;
1500 fprintf(fp, ",%s", perf_evsel__name(evsel));
1508 static int __event_process_build_id(struct build_id_event *bev,
1510 struct perf_session *session)
1513 struct list_head *head;
1514 struct machine *machine;
1517 enum dso_kernel_type dso_type;
1519 machine = perf_session__findnew_machine(session, bev->pid);
1523 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1526 case PERF_RECORD_MISC_KERNEL:
1527 dso_type = DSO_TYPE_KERNEL;
1528 head = &machine->kernel_dsos;
1530 case PERF_RECORD_MISC_GUEST_KERNEL:
1531 dso_type = DSO_TYPE_GUEST_KERNEL;
1532 head = &machine->kernel_dsos;
1534 case PERF_RECORD_MISC_USER:
1535 case PERF_RECORD_MISC_GUEST_USER:
1536 dso_type = DSO_TYPE_USER;
1537 head = &machine->user_dsos;
1543 dso = __dsos__findnew(head, filename);
1545 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1547 dso__set_build_id(dso, &bev->build_id);
1549 if (filename[0] == '[')
1550 dso->kernel = dso_type;
1552 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1554 pr_debug("build id event received for %s: %s\n",
1555 dso->long_name, sbuild_id);
1563 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1564 int input, u64 offset, u64 size)
1566 struct perf_session *session = container_of(header, struct perf_session, header);
1568 struct perf_event_header header;
1569 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1572 struct build_id_event bev;
1573 char filename[PATH_MAX];
1574 u64 limit = offset + size;
1576 while (offset < limit) {
1579 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1582 if (header->needs_swap)
1583 perf_event_header__bswap(&old_bev.header);
1585 len = old_bev.header.size - sizeof(old_bev);
1586 if (readn(input, filename, len) != len)
1589 bev.header = old_bev.header;
1592 * As the pid is the missing value, we need to fill
1593 * it properly. The header.misc value give us nice hint.
1595 bev.pid = HOST_KERNEL_ID;
1596 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1597 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1598 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1600 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1601 __event_process_build_id(&bev, filename, session);
1603 offset += bev.header.size;
1609 static int perf_header__read_build_ids(struct perf_header *header,
1610 int input, u64 offset, u64 size)
1612 struct perf_session *session = container_of(header, struct perf_session, header);
1613 struct build_id_event bev;
1614 char filename[PATH_MAX];
1615 u64 limit = offset + size, orig_offset = offset;
1618 while (offset < limit) {
1621 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1624 if (header->needs_swap)
1625 perf_event_header__bswap(&bev.header);
1627 len = bev.header.size - sizeof(bev);
1628 if (readn(input, filename, len) != len)
1631 * The a1645ce1 changeset:
1633 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1635 * Added a field to struct build_id_event that broke the file
1638 * Since the kernel build-id is the first entry, process the
1639 * table using the old format if the well known
1640 * '[kernel.kallsyms]' string for the kernel build-id has the
1641 * first 4 characters chopped off (where the pid_t sits).
1643 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1644 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1646 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1649 __event_process_build_id(&bev, filename, session);
1651 offset += bev.header.size;
1658 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1659 struct perf_header *ph __maybe_unused,
1662 ssize_t ret = trace_report(fd, data, false);
1663 return ret < 0 ? -1 : 0;
1666 static int process_build_id(struct perf_file_section *section,
1667 struct perf_header *ph, int fd,
1668 void *data __maybe_unused)
1670 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1671 pr_debug("Failed to read buildids, continuing...\n");
1675 static int process_hostname(struct perf_file_section *section __maybe_unused,
1676 struct perf_header *ph, int fd,
1677 void *data __maybe_unused)
1679 ph->env.hostname = do_read_string(fd, ph);
1680 return ph->env.hostname ? 0 : -ENOMEM;
1683 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1684 struct perf_header *ph, int fd,
1685 void *data __maybe_unused)
1687 ph->env.os_release = do_read_string(fd, ph);
1688 return ph->env.os_release ? 0 : -ENOMEM;
1691 static int process_version(struct perf_file_section *section __maybe_unused,
1692 struct perf_header *ph, int fd,
1693 void *data __maybe_unused)
1695 ph->env.version = do_read_string(fd, ph);
1696 return ph->env.version ? 0 : -ENOMEM;
1699 static int process_arch(struct perf_file_section *section __maybe_unused,
1700 struct perf_header *ph, int fd,
1701 void *data __maybe_unused)
1703 ph->env.arch = do_read_string(fd, ph);
1704 return ph->env.arch ? 0 : -ENOMEM;
1707 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1708 struct perf_header *ph, int fd,
1709 void *data __maybe_unused)
1714 ret = readn(fd, &nr, sizeof(nr));
1715 if (ret != sizeof(nr))
1721 ph->env.nr_cpus_online = nr;
1723 ret = readn(fd, &nr, sizeof(nr));
1724 if (ret != sizeof(nr))
1730 ph->env.nr_cpus_avail = nr;
1734 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1735 struct perf_header *ph, int fd,
1736 void *data __maybe_unused)
1738 ph->env.cpu_desc = do_read_string(fd, ph);
1739 return ph->env.cpu_desc ? 0 : -ENOMEM;
1742 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1743 struct perf_header *ph, int fd,
1744 void *data __maybe_unused)
1746 ph->env.cpuid = do_read_string(fd, ph);
1747 return ph->env.cpuid ? 0 : -ENOMEM;
1750 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1751 struct perf_header *ph, int fd,
1752 void *data __maybe_unused)
1757 ret = readn(fd, &mem, sizeof(mem));
1758 if (ret != sizeof(mem))
1762 mem = bswap_64(mem);
1764 ph->env.total_mem = mem;
1768 static struct perf_evsel *
1769 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1771 struct perf_evsel *evsel;
1773 list_for_each_entry(evsel, &evlist->entries, node) {
1774 if (evsel->idx == idx)
1782 perf_evlist__set_event_name(struct perf_evlist *evlist,
1783 struct perf_evsel *event)
1785 struct perf_evsel *evsel;
1790 evsel = perf_evlist__find_by_index(evlist, event->idx);
1797 evsel->name = strdup(event->name);
1801 process_event_desc(struct perf_file_section *section __maybe_unused,
1802 struct perf_header *header, int fd,
1803 void *data __maybe_unused)
1805 struct perf_session *session;
1806 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1811 session = container_of(header, struct perf_session, header);
1812 for (evsel = events; evsel->attr.size; evsel++)
1813 perf_evlist__set_event_name(session->evlist, evsel);
1815 free_event_desc(events);
1820 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1821 struct perf_header *ph, int fd,
1822 void *data __maybe_unused)
1829 ret = readn(fd, &nr, sizeof(nr));
1830 if (ret != sizeof(nr))
1836 ph->env.nr_cmdline = nr;
1837 strbuf_init(&sb, 128);
1839 for (i = 0; i < nr; i++) {
1840 str = do_read_string(fd, ph);
1844 /* include a NULL character at the end */
1845 strbuf_add(&sb, str, strlen(str) + 1);
1848 ph->env.cmdline = strbuf_detach(&sb, NULL);
1852 strbuf_release(&sb);
1856 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1857 struct perf_header *ph, int fd,
1858 void *data __maybe_unused)
1865 ret = readn(fd, &nr, sizeof(nr));
1866 if (ret != sizeof(nr))
1872 ph->env.nr_sibling_cores = nr;
1873 strbuf_init(&sb, 128);
1875 for (i = 0; i < nr; i++) {
1876 str = do_read_string(fd, ph);
1880 /* include a NULL character at the end */
1881 strbuf_add(&sb, str, strlen(str) + 1);
1884 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1886 ret = readn(fd, &nr, sizeof(nr));
1887 if (ret != sizeof(nr))
1893 ph->env.nr_sibling_threads = nr;
1895 for (i = 0; i < nr; i++) {
1896 str = do_read_string(fd, ph);
1900 /* include a NULL character at the end */
1901 strbuf_add(&sb, str, strlen(str) + 1);
1904 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1908 strbuf_release(&sb);
1912 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1913 struct perf_header *ph, int fd,
1914 void *data __maybe_unused)
1919 uint64_t mem_total, mem_free;
1923 ret = readn(fd, &nr, sizeof(nr));
1924 if (ret != sizeof(nr))
1930 ph->env.nr_numa_nodes = nr;
1931 strbuf_init(&sb, 256);
1933 for (i = 0; i < nr; i++) {
1935 ret = readn(fd, &node, sizeof(node));
1936 if (ret != sizeof(node))
1939 ret = readn(fd, &mem_total, sizeof(u64));
1940 if (ret != sizeof(u64))
1943 ret = readn(fd, &mem_free, sizeof(u64));
1944 if (ret != sizeof(u64))
1947 if (ph->needs_swap) {
1948 node = bswap_32(node);
1949 mem_total = bswap_64(mem_total);
1950 mem_free = bswap_64(mem_free);
1953 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1954 node, mem_total, mem_free);
1956 str = do_read_string(fd, ph);
1960 /* include a NULL character at the end */
1961 strbuf_add(&sb, str, strlen(str) + 1);
1964 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1968 strbuf_release(&sb);
1972 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1973 struct perf_header *ph, int fd,
1974 void *data __maybe_unused)
1982 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1983 if (ret != sizeof(pmu_num))
1987 pmu_num = bswap_32(pmu_num);
1990 pr_debug("pmu mappings not available\n");
1994 ph->env.nr_pmu_mappings = pmu_num;
1995 strbuf_init(&sb, 128);
1998 if (readn(fd, &type, sizeof(type)) != sizeof(type))
2001 type = bswap_32(type);
2003 name = do_read_string(fd, ph);
2007 strbuf_addf(&sb, "%u:%s", type, name);
2008 /* include a NULL character at the end */
2009 strbuf_add(&sb, "", 1);
2014 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2018 strbuf_release(&sb);
2022 static int process_group_desc(struct perf_file_section *section __maybe_unused,
2023 struct perf_header *ph, int fd,
2024 void *data __maybe_unused)
2027 u32 i, nr, nr_groups;
2028 struct perf_session *session;
2029 struct perf_evsel *evsel, *leader = NULL;
2036 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2040 nr_groups = bswap_32(nr_groups);
2042 ph->env.nr_groups = nr_groups;
2044 pr_debug("group desc not available\n");
2048 desc = calloc(nr_groups, sizeof(*desc));
2052 for (i = 0; i < nr_groups; i++) {
2053 desc[i].name = do_read_string(fd, ph);
2057 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2060 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2063 if (ph->needs_swap) {
2064 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2065 desc[i].nr_members = bswap_32(desc[i].nr_members);
2070 * Rebuild group relationship based on the group_desc
2072 session = container_of(ph, struct perf_session, header);
2073 session->evlist->nr_groups = nr_groups;
2076 list_for_each_entry(evsel, &session->evlist->entries, node) {
2077 if (evsel->idx == (int) desc[i].leader_idx) {
2078 evsel->leader = evsel;
2079 /* {anon_group} is a dummy name */
2080 if (strcmp(desc[i].name, "{anon_group}"))
2081 evsel->group_name = desc[i].name;
2082 evsel->nr_members = desc[i].nr_members;
2084 if (i >= nr_groups || nr > 0) {
2085 pr_debug("invalid group desc\n");
2090 nr = evsel->nr_members - 1;
2093 /* This is a group member */
2094 evsel->leader = leader;
2100 if (i != nr_groups || nr != 0) {
2101 pr_debug("invalid group desc\n");
2107 while ((int) --i >= 0)
2114 struct feature_ops {
2115 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2116 void (*print)(struct perf_header *h, int fd, FILE *fp);
2117 int (*process)(struct perf_file_section *section,
2118 struct perf_header *h, int fd, void *data);
2123 #define FEAT_OPA(n, func) \
2124 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2125 #define FEAT_OPP(n, func) \
2126 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2127 .process = process_##func }
2128 #define FEAT_OPF(n, func) \
2129 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2130 .process = process_##func, .full_only = true }
2132 /* feature_ops not implemented: */
2133 #define print_tracing_data NULL
2134 #define print_build_id NULL
2136 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2137 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
2138 FEAT_OPP(HEADER_BUILD_ID, build_id),
2139 FEAT_OPP(HEADER_HOSTNAME, hostname),
2140 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2141 FEAT_OPP(HEADER_VERSION, version),
2142 FEAT_OPP(HEADER_ARCH, arch),
2143 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2144 FEAT_OPP(HEADER_CPUDESC, cpudesc),
2145 FEAT_OPP(HEADER_CPUID, cpuid),
2146 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
2147 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
2148 FEAT_OPP(HEADER_CMDLINE, cmdline),
2149 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2150 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
2151 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
2152 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2153 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
2156 struct header_print_data {
2158 bool full; /* extended list of headers */
2161 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2162 struct perf_header *ph,
2163 int feat, int fd, void *data)
2165 struct header_print_data *hd = data;
2167 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2168 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2169 "%d, continuing...\n", section->offset, feat);
2172 if (feat >= HEADER_LAST_FEATURE) {
2173 pr_warning("unknown feature %d\n", feat);
2176 if (!feat_ops[feat].print)
2179 if (!feat_ops[feat].full_only || hd->full)
2180 feat_ops[feat].print(ph, fd, hd->fp);
2182 fprintf(hd->fp, "# %s info available, use -I to display\n",
2183 feat_ops[feat].name);
2188 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2190 struct header_print_data hd;
2191 struct perf_header *header = &session->header;
2192 int fd = session->fd;
2196 perf_header__process_sections(header, fd, &hd,
2197 perf_file_section__fprintf_info);
2201 static int do_write_feat(int fd, struct perf_header *h, int type,
2202 struct perf_file_section **p,
2203 struct perf_evlist *evlist)
2208 if (perf_header__has_feat(h, type)) {
2209 if (!feat_ops[type].write)
2212 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2214 err = feat_ops[type].write(fd, h, evlist);
2216 pr_debug("failed to write feature %d\n", type);
2218 /* undo anything written */
2219 lseek(fd, (*p)->offset, SEEK_SET);
2223 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2229 static int perf_header__adds_write(struct perf_header *header,
2230 struct perf_evlist *evlist, int fd)
2233 struct perf_file_section *feat_sec, *p;
2239 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2243 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2244 if (feat_sec == NULL)
2247 sec_size = sizeof(*feat_sec) * nr_sections;
2249 sec_start = header->feat_offset;
2250 lseek(fd, sec_start + sec_size, SEEK_SET);
2252 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2253 if (do_write_feat(fd, header, feat, &p, evlist))
2254 perf_header__clear_feat(header, feat);
2257 lseek(fd, sec_start, SEEK_SET);
2259 * may write more than needed due to dropped feature, but
2260 * this is okay, reader will skip the mising entries
2262 err = do_write(fd, feat_sec, sec_size);
2264 pr_debug("failed to write feature section\n");
2269 int perf_header__write_pipe(int fd)
2271 struct perf_pipe_file_header f_header;
2274 f_header = (struct perf_pipe_file_header){
2275 .magic = PERF_MAGIC,
2276 .size = sizeof(f_header),
2279 err = do_write(fd, &f_header, sizeof(f_header));
2281 pr_debug("failed to write perf pipe header\n");
2288 int perf_session__write_header(struct perf_session *session,
2289 struct perf_evlist *evlist,
2290 int fd, bool at_exit)
2292 struct perf_file_header f_header;
2293 struct perf_file_attr f_attr;
2294 struct perf_header *header = &session->header;
2295 struct perf_evsel *evsel;
2299 lseek(fd, sizeof(f_header), SEEK_SET);
2301 list_for_each_entry(evsel, &evlist->entries, node) {
2302 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2303 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2305 pr_debug("failed to write perf header\n");
2310 attr_offset = lseek(fd, 0, SEEK_CUR);
2312 list_for_each_entry(evsel, &evlist->entries, node) {
2313 f_attr = (struct perf_file_attr){
2314 .attr = evsel->attr,
2316 .offset = evsel->id_offset,
2317 .size = evsel->ids * sizeof(u64),
2320 err = do_write(fd, &f_attr, sizeof(f_attr));
2322 pr_debug("failed to write perf header attribute\n");
2327 header->data_offset = lseek(fd, 0, SEEK_CUR);
2328 header->feat_offset = header->data_offset + header->data_size;
2331 err = perf_header__adds_write(header, evlist, fd);
2336 f_header = (struct perf_file_header){
2337 .magic = PERF_MAGIC,
2338 .size = sizeof(f_header),
2339 .attr_size = sizeof(f_attr),
2341 .offset = attr_offset,
2342 .size = evlist->nr_entries * sizeof(f_attr),
2345 .offset = header->data_offset,
2346 .size = header->data_size,
2348 /* event_types is ignored, store zeros */
2351 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2353 lseek(fd, 0, SEEK_SET);
2354 err = do_write(fd, &f_header, sizeof(f_header));
2356 pr_debug("failed to write perf header\n");
2359 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2364 static int perf_header__getbuffer64(struct perf_header *header,
2365 int fd, void *buf, size_t size)
2367 if (readn(fd, buf, size) <= 0)
2370 if (header->needs_swap)
2371 mem_bswap_64(buf, size);
2376 int perf_header__process_sections(struct perf_header *header, int fd,
2378 int (*process)(struct perf_file_section *section,
2379 struct perf_header *ph,
2380 int feat, int fd, void *data))
2382 struct perf_file_section *feat_sec, *sec;
2388 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2392 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2396 sec_size = sizeof(*feat_sec) * nr_sections;
2398 lseek(fd, header->feat_offset, SEEK_SET);
2400 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2404 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2405 err = process(sec++, header, feat, fd, data);
2415 static const int attr_file_abi_sizes[] = {
2416 [0] = PERF_ATTR_SIZE_VER0,
2417 [1] = PERF_ATTR_SIZE_VER1,
2418 [2] = PERF_ATTR_SIZE_VER2,
2419 [3] = PERF_ATTR_SIZE_VER3,
2424 * In the legacy file format, the magic number is not used to encode endianness.
2425 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2426 * on ABI revisions, we need to try all combinations for all endianness to
2427 * detect the endianness.
2429 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2431 uint64_t ref_size, attr_size;
2434 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2435 ref_size = attr_file_abi_sizes[i]
2436 + sizeof(struct perf_file_section);
2437 if (hdr_sz != ref_size) {
2438 attr_size = bswap_64(hdr_sz);
2439 if (attr_size != ref_size)
2442 ph->needs_swap = true;
2444 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2449 /* could not determine endianness */
2453 #define PERF_PIPE_HDR_VER0 16
2455 static const size_t attr_pipe_abi_sizes[] = {
2456 [0] = PERF_PIPE_HDR_VER0,
2461 * In the legacy pipe format, there is an implicit assumption that endiannesss
2462 * between host recording the samples, and host parsing the samples is the
2463 * same. This is not always the case given that the pipe output may always be
2464 * redirected into a file and analyzed on a different machine with possibly a
2465 * different endianness and perf_event ABI revsions in the perf tool itself.
2467 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2472 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2473 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2474 attr_size = bswap_64(hdr_sz);
2475 if (attr_size != hdr_sz)
2478 ph->needs_swap = true;
2480 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2486 bool is_perf_magic(u64 magic)
2488 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2489 || magic == __perf_magic2
2490 || magic == __perf_magic2_sw)
2496 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2497 bool is_pipe, struct perf_header *ph)
2501 /* check for legacy format */
2502 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2504 ph->version = PERF_HEADER_VERSION_1;
2505 pr_debug("legacy perf.data format\n");
2507 return try_all_pipe_abis(hdr_sz, ph);
2509 return try_all_file_abis(hdr_sz, ph);
2512 * the new magic number serves two purposes:
2513 * - unique number to identify actual perf.data files
2514 * - encode endianness of file
2517 /* check magic number with one endianness */
2518 if (magic == __perf_magic2)
2521 /* check magic number with opposite endianness */
2522 if (magic != __perf_magic2_sw)
2525 ph->needs_swap = true;
2526 ph->version = PERF_HEADER_VERSION_2;
2531 int perf_file_header__read(struct perf_file_header *header,
2532 struct perf_header *ph, int fd)
2536 lseek(fd, 0, SEEK_SET);
2538 ret = readn(fd, header, sizeof(*header));
2542 if (check_magic_endian(header->magic,
2543 header->attr_size, false, ph) < 0) {
2544 pr_debug("magic/endian check failed\n");
2548 if (ph->needs_swap) {
2549 mem_bswap_64(header, offsetof(struct perf_file_header,
2553 if (header->size != sizeof(*header)) {
2554 /* Support the previous format */
2555 if (header->size == offsetof(typeof(*header), adds_features))
2556 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2559 } else if (ph->needs_swap) {
2561 * feature bitmap is declared as an array of unsigned longs --
2562 * not good since its size can differ between the host that
2563 * generated the data file and the host analyzing the file.
2565 * We need to handle endianness, but we don't know the size of
2566 * the unsigned long where the file was generated. Take a best
2567 * guess at determining it: try 64-bit swap first (ie., file
2568 * created on a 64-bit host), and check if the hostname feature
2569 * bit is set (this feature bit is forced on as of fbe96f2).
2570 * If the bit is not, undo the 64-bit swap and try a 32-bit
2571 * swap. If the hostname bit is still not set (e.g., older data
2572 * file), punt and fallback to the original behavior --
2573 * clearing all feature bits and setting buildid.
2575 mem_bswap_64(&header->adds_features,
2576 BITS_TO_U64(HEADER_FEAT_BITS));
2578 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2580 mem_bswap_64(&header->adds_features,
2581 BITS_TO_U64(HEADER_FEAT_BITS));
2584 mem_bswap_32(&header->adds_features,
2585 BITS_TO_U32(HEADER_FEAT_BITS));
2588 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2589 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2590 set_bit(HEADER_BUILD_ID, header->adds_features);
2594 memcpy(&ph->adds_features, &header->adds_features,
2595 sizeof(ph->adds_features));
2597 ph->data_offset = header->data.offset;
2598 ph->data_size = header->data.size;
2599 ph->feat_offset = header->data.offset + header->data.size;
2603 static int perf_file_section__process(struct perf_file_section *section,
2604 struct perf_header *ph,
2605 int feat, int fd, void *data)
2607 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2608 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2609 "%d, continuing...\n", section->offset, feat);
2613 if (feat >= HEADER_LAST_FEATURE) {
2614 pr_debug("unknown feature %d, continuing...\n", feat);
2618 if (!feat_ops[feat].process)
2621 return feat_ops[feat].process(section, ph, fd, data);
2624 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2625 struct perf_header *ph, int fd,
2630 ret = readn(fd, header, sizeof(*header));
2634 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2635 pr_debug("endian/magic failed\n");
2640 header->size = bswap_64(header->size);
2642 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2648 static int perf_header__read_pipe(struct perf_session *session)
2650 struct perf_header *header = &session->header;
2651 struct perf_pipe_file_header f_header;
2653 if (perf_file_header__read_pipe(&f_header, header, session->fd,
2654 session->repipe) < 0) {
2655 pr_debug("incompatible file format\n");
2662 static int read_attr(int fd, struct perf_header *ph,
2663 struct perf_file_attr *f_attr)
2665 struct perf_event_attr *attr = &f_attr->attr;
2667 size_t our_sz = sizeof(f_attr->attr);
2670 memset(f_attr, 0, sizeof(*f_attr));
2672 /* read minimal guaranteed structure */
2673 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2675 pr_debug("cannot read %d bytes of header attr\n",
2676 PERF_ATTR_SIZE_VER0);
2680 /* on file perf_event_attr size */
2688 sz = PERF_ATTR_SIZE_VER0;
2689 } else if (sz > our_sz) {
2690 pr_debug("file uses a more recent and unsupported ABI"
2691 " (%zu bytes extra)\n", sz - our_sz);
2694 /* what we have not yet read and that we know about */
2695 left = sz - PERF_ATTR_SIZE_VER0;
2698 ptr += PERF_ATTR_SIZE_VER0;
2700 ret = readn(fd, ptr, left);
2702 /* read perf_file_section, ids are read in caller */
2703 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2705 return ret <= 0 ? -1 : 0;
2708 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2709 struct pevent *pevent)
2711 struct event_format *event;
2714 /* already prepared */
2715 if (evsel->tp_format)
2718 if (pevent == NULL) {
2719 pr_debug("broken or missing trace data\n");
2723 event = pevent_find_event(pevent, evsel->attr.config);
2728 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2729 evsel->name = strdup(bf);
2730 if (evsel->name == NULL)
2734 evsel->tp_format = event;
2738 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2739 struct pevent *pevent)
2741 struct perf_evsel *pos;
2743 list_for_each_entry(pos, &evlist->entries, node) {
2744 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2745 perf_evsel__prepare_tracepoint_event(pos, pevent))
2752 int perf_session__read_header(struct perf_session *session)
2754 struct perf_header *header = &session->header;
2755 struct perf_file_header f_header;
2756 struct perf_file_attr f_attr;
2758 int nr_attrs, nr_ids, i, j;
2759 int fd = session->fd;
2761 session->evlist = perf_evlist__new();
2762 if (session->evlist == NULL)
2765 if (session->fd_pipe)
2766 return perf_header__read_pipe(session);
2768 if (perf_file_header__read(&f_header, header, fd) < 0)
2771 nr_attrs = f_header.attrs.size / f_header.attr_size;
2772 lseek(fd, f_header.attrs.offset, SEEK_SET);
2774 for (i = 0; i < nr_attrs; i++) {
2775 struct perf_evsel *evsel;
2778 if (read_attr(fd, header, &f_attr) < 0)
2781 if (header->needs_swap)
2782 perf_event__attr_swap(&f_attr.attr);
2784 tmp = lseek(fd, 0, SEEK_CUR);
2785 evsel = perf_evsel__new(&f_attr.attr, i);
2788 goto out_delete_evlist;
2790 evsel->needs_swap = header->needs_swap;
2792 * Do it before so that if perf_evsel__alloc_id fails, this
2793 * entry gets purged too at perf_evlist__delete().
2795 perf_evlist__add(session->evlist, evsel);
2797 nr_ids = f_attr.ids.size / sizeof(u64);
2799 * We don't have the cpu and thread maps on the header, so
2800 * for allocating the perf_sample_id table we fake 1 cpu and
2801 * hattr->ids threads.
2803 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2804 goto out_delete_evlist;
2806 lseek(fd, f_attr.ids.offset, SEEK_SET);
2808 for (j = 0; j < nr_ids; j++) {
2809 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2812 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2815 lseek(fd, tmp, SEEK_SET);
2818 symbol_conf.nr_events = nr_attrs;
2820 perf_header__process_sections(header, fd, &session->pevent,
2821 perf_file_section__process);
2823 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2825 goto out_delete_evlist;
2832 perf_evlist__delete(session->evlist);
2833 session->evlist = NULL;
2837 int perf_event__synthesize_attr(struct perf_tool *tool,
2838 struct perf_event_attr *attr, u32 ids, u64 *id,
2839 perf_event__handler_t process)
2841 union perf_event *ev;
2845 size = sizeof(struct perf_event_attr);
2846 size = PERF_ALIGN(size, sizeof(u64));
2847 size += sizeof(struct perf_event_header);
2848 size += ids * sizeof(u64);
2855 ev->attr.attr = *attr;
2856 memcpy(ev->attr.id, id, ids * sizeof(u64));
2858 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2859 ev->attr.header.size = (u16)size;
2861 if (ev->attr.header.size == size)
2862 err = process(tool, ev, NULL, NULL);
2871 int perf_event__synthesize_attrs(struct perf_tool *tool,
2872 struct perf_session *session,
2873 perf_event__handler_t process)
2875 struct perf_evsel *evsel;
2878 list_for_each_entry(evsel, &session->evlist->entries, node) {
2879 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2880 evsel->id, process);
2882 pr_debug("failed to create perf header attribute\n");
2890 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2891 union perf_event *event,
2892 struct perf_evlist **pevlist)
2895 struct perf_evsel *evsel;
2896 struct perf_evlist *evlist = *pevlist;
2898 if (evlist == NULL) {
2899 *pevlist = evlist = perf_evlist__new();
2904 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2908 perf_evlist__add(evlist, evsel);
2910 ids = event->header.size;
2911 ids -= (void *)&event->attr.id - (void *)event;
2912 n_ids = ids / sizeof(u64);
2914 * We don't have the cpu and thread maps on the header, so
2915 * for allocating the perf_sample_id table we fake 1 cpu and
2916 * hattr->ids threads.
2918 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2921 for (i = 0; i < n_ids; i++) {
2922 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2925 symbol_conf.nr_events = evlist->nr_entries;
2930 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2931 struct perf_evlist *evlist,
2932 perf_event__handler_t process)
2934 union perf_event ev;
2935 struct tracing_data *tdata;
2936 ssize_t size = 0, aligned_size = 0, padding;
2937 int err __maybe_unused = 0;
2940 * We are going to store the size of the data followed
2941 * by the data contents. Since the fd descriptor is a pipe,
2942 * we cannot seek back to store the size of the data once
2943 * we know it. Instead we:
2945 * - write the tracing data to the temp file
2946 * - get/write the data size to pipe
2947 * - write the tracing data from the temp file
2950 tdata = tracing_data_get(&evlist->entries, fd, true);
2954 memset(&ev, 0, sizeof(ev));
2956 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2958 aligned_size = PERF_ALIGN(size, sizeof(u64));
2959 padding = aligned_size - size;
2960 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2961 ev.tracing_data.size = aligned_size;
2963 process(tool, &ev, NULL, NULL);
2966 * The put function will copy all the tracing data
2967 * stored in temp file to the pipe.
2969 tracing_data_put(tdata);
2971 write_padded(fd, NULL, 0, padding);
2973 return aligned_size;
2976 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2977 union perf_event *event,
2978 struct perf_session *session)
2980 ssize_t size_read, padding, size = event->tracing_data.size;
2981 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2984 /* setup for reading amidst mmap */
2985 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2988 size_read = trace_report(session->fd, &session->pevent,
2990 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2992 if (readn(session->fd, buf, padding) < 0) {
2993 pr_err("%s: reading input file", __func__);
2996 if (session->repipe) {
2997 int retw = write(STDOUT_FILENO, buf, padding);
2998 if (retw <= 0 || retw != padding) {
2999 pr_err("%s: repiping tracing data padding", __func__);
3004 if (size_read + padding != size) {
3005 pr_err("%s: tracing data size mismatch", __func__);
3009 perf_evlist__prepare_tracepoint_events(session->evlist,
3012 return size_read + padding;
3015 int perf_event__synthesize_build_id(struct perf_tool *tool,
3016 struct dso *pos, u16 misc,
3017 perf_event__handler_t process,
3018 struct machine *machine)
3020 union perf_event ev;
3027 memset(&ev, 0, sizeof(ev));
3029 len = pos->long_name_len + 1;
3030 len = PERF_ALIGN(len, NAME_ALIGN);
3031 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3032 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3033 ev.build_id.header.misc = misc;
3034 ev.build_id.pid = machine->pid;
3035 ev.build_id.header.size = sizeof(ev.build_id) + len;
3036 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3038 err = process(tool, &ev, NULL, machine);
3043 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3044 union perf_event *event,
3045 struct perf_session *session)
3047 __event_process_build_id(&event->build_id,
3048 event->build_id.filename,
3053 void disable_buildid_cache(void)
3055 no_buildid_cache = true;