1 #define _FILE_OFFSET_BITS 64
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/bitops.h>
12 #include <sys/utsname.h>
18 #include "trace-event.h"
25 static bool no_buildid_cache = false;
27 static int trace_event_count;
28 static struct perf_trace_event_type *trace_events;
30 static u32 header_argc;
31 static const char **header_argv;
33 int perf_header__push_event(u64 id, const char *name)
35 struct perf_trace_event_type *nevents;
37 if (strlen(name) > MAX_EVENT_NAME)
38 pr_warning("Event %s will be truncated\n", name);
40 nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events));
43 trace_events = nevents;
45 memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type));
46 trace_events[trace_event_count].event_id = id;
47 strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1);
52 char *perf_header__find_event(u64 id)
55 for (i = 0 ; i < trace_event_count; i++) {
56 if (trace_events[i].event_id == id)
57 return trace_events[i].name;
64 * must be a numerical value to let the endianness
65 * determine the memory layout. That way we are able
66 * to detect endianness when reading the perf.data file
69 * we check for legacy (PERFFILE) format.
71 static const char *__perf_magic1 = "PERFFILE";
72 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
73 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
75 #define PERF_MAGIC __perf_magic2
77 struct perf_file_attr {
78 struct perf_event_attr attr;
79 struct perf_file_section ids;
82 void perf_header__set_feat(struct perf_header *header, int feat)
84 set_bit(feat, header->adds_features);
87 void perf_header__clear_feat(struct perf_header *header, int feat)
89 clear_bit(feat, header->adds_features);
92 bool perf_header__has_feat(const struct perf_header *header, int feat)
94 return test_bit(feat, header->adds_features);
97 static int do_write(int fd, const void *buf, size_t size)
100 int ret = write(fd, buf, size);
112 #define NAME_ALIGN 64
114 static int write_padded(int fd, const void *bf, size_t count,
115 size_t count_aligned)
117 static const char zero_buf[NAME_ALIGN];
118 int err = do_write(fd, bf, count);
121 err = do_write(fd, zero_buf, count_aligned - count);
126 static int do_write_string(int fd, const char *str)
131 olen = strlen(str) + 1;
132 len = ALIGN(olen, NAME_ALIGN);
134 /* write len, incl. \0 */
135 ret = do_write(fd, &len, sizeof(len));
139 return write_padded(fd, str, olen, len);
142 static char *do_read_string(int fd, struct perf_header *ph)
148 sz = read(fd, &len, sizeof(len));
149 if (sz < (ssize_t)sizeof(len))
159 ret = read(fd, buf, len);
160 if (ret == (ssize_t)len) {
162 * strings are padded by zeroes
163 * thus the actual strlen of buf
164 * may be less than len
174 perf_header__set_cmdline(int argc, const char **argv)
179 * If header_argv has already been set, do not override it.
180 * This allows a command to set the cmdline, parse args and
181 * then call another builtin function that implements a
182 * command -- e.g, cmd_kvm calling cmd_record.
187 header_argc = (u32)argc;
189 /* do not include NULL termination */
190 header_argv = calloc(argc, sizeof(char *));
195 * must copy argv contents because it gets moved
196 * around during option parsing
198 for (i = 0; i < argc ; i++)
199 header_argv[i] = argv[i];
204 #define dsos__for_each_with_build_id(pos, head) \
205 list_for_each_entry(pos, head, node) \
206 if (!pos->has_build_id) \
210 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
215 dsos__for_each_with_build_id(pos, head) {
217 struct build_id_event b;
222 len = pos->long_name_len + 1;
223 len = ALIGN(len, NAME_ALIGN);
224 memset(&b, 0, sizeof(b));
225 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
227 b.header.misc = misc;
228 b.header.size = sizeof(b) + len;
229 err = do_write(fd, &b, sizeof(b));
232 err = write_padded(fd, pos->long_name,
233 pos->long_name_len + 1, len);
241 static int machine__write_buildid_table(struct machine *machine, int fd)
244 u16 kmisc = PERF_RECORD_MISC_KERNEL,
245 umisc = PERF_RECORD_MISC_USER;
247 if (!machine__is_host(machine)) {
248 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
249 umisc = PERF_RECORD_MISC_GUEST_USER;
252 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
255 err = __dsos__write_buildid_table(&machine->user_dsos,
256 machine->pid, umisc, fd);
260 static int dsos__write_buildid_table(struct perf_header *header, int fd)
262 struct perf_session *session = container_of(header,
263 struct perf_session, header);
265 int err = machine__write_buildid_table(&session->host_machine, fd);
270 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
271 struct machine *pos = rb_entry(nd, struct machine, rb_node);
272 err = machine__write_buildid_table(pos, fd);
279 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
280 const char *name, bool is_kallsyms)
282 const size_t size = PATH_MAX;
283 char *realname, *filename = zalloc(size),
284 *linkname = zalloc(size), *targetname;
288 if (symbol_conf.kptr_restrict) {
289 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
292 realname = (char *)name;
294 realname = realpath(name, NULL);
296 if (realname == NULL || filename == NULL || linkname == NULL)
299 len = scnprintf(filename, size, "%s%s%s",
300 debugdir, is_kallsyms ? "/" : "", 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,
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, is_kallsyms);
345 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
347 const size_t size = PATH_MAX;
348 char *filename = zalloc(size),
349 *linkname = zalloc(size);
352 if (filename == NULL || linkname == NULL)
355 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
356 debugdir, sbuild_id, sbuild_id + 2);
358 if (access(linkname, F_OK))
361 if (readlink(linkname, filename, size - 1) < 0)
364 if (unlink(linkname))
368 * Since the link is relative, we must make it absolute:
370 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
371 debugdir, sbuild_id, filename);
373 if (unlink(linkname))
383 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
385 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
387 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
388 dso->long_name, debugdir, is_kallsyms);
391 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
396 dsos__for_each_with_build_id(pos, head)
397 if (dso__cache_build_id(pos, debugdir))
403 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
405 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
406 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
410 static int perf_session__cache_build_ids(struct perf_session *session)
414 char debugdir[PATH_MAX];
416 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
418 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
421 ret = machine__cache_build_ids(&session->host_machine, debugdir);
423 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
424 struct machine *pos = rb_entry(nd, struct machine, rb_node);
425 ret |= machine__cache_build_ids(pos, debugdir);
430 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
432 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
433 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
437 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
440 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
442 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
443 struct machine *pos = rb_entry(nd, struct machine, rb_node);
444 ret |= machine__read_build_ids(pos, with_hits);
450 static int write_tracing_data(int fd, struct perf_header *h __used,
451 struct perf_evlist *evlist)
453 return read_tracing_data(fd, &evlist->entries);
457 static int write_build_id(int fd, struct perf_header *h,
458 struct perf_evlist *evlist __used)
460 struct perf_session *session;
463 session = container_of(h, struct perf_session, header);
465 if (!perf_session__read_build_ids(session, true))
468 err = dsos__write_buildid_table(h, fd);
470 pr_debug("failed to write buildid table\n");
473 if (!no_buildid_cache)
474 perf_session__cache_build_ids(session);
479 static int write_hostname(int fd, struct perf_header *h __used,
480 struct perf_evlist *evlist __used)
489 return do_write_string(fd, uts.nodename);
492 static int write_osrelease(int fd, struct perf_header *h __used,
493 struct perf_evlist *evlist __used)
502 return do_write_string(fd, uts.release);
505 static int write_arch(int fd, struct perf_header *h __used,
506 struct perf_evlist *evlist __used)
515 return do_write_string(fd, uts.machine);
518 static int write_version(int fd, struct perf_header *h __used,
519 struct perf_evlist *evlist __used)
521 return do_write_string(fd, perf_version_string);
524 static int write_cpudesc(int fd, struct perf_header *h __used,
525 struct perf_evlist *evlist __used)
528 #define CPUINFO_PROC NULL
533 const char *search = CPUINFO_PROC;
540 file = fopen("/proc/cpuinfo", "r");
544 while (getline(&buf, &len, file) > 0) {
545 ret = strncmp(buf, search, strlen(search));
555 p = strchr(buf, ':');
556 if (p && *(p+1) == ' ' && *(p+2))
562 /* squash extra space characters (branding string) */
569 while (*q && isspace(*q))
572 while ((*r++ = *q++));
576 ret = do_write_string(fd, s);
583 static int write_nrcpus(int fd, struct perf_header *h __used,
584 struct perf_evlist *evlist __used)
590 nr = sysconf(_SC_NPROCESSORS_CONF);
594 nrc = (u32)(nr & UINT_MAX);
596 nr = sysconf(_SC_NPROCESSORS_ONLN);
600 nra = (u32)(nr & UINT_MAX);
602 ret = do_write(fd, &nrc, sizeof(nrc));
606 return do_write(fd, &nra, sizeof(nra));
609 static int write_event_desc(int fd, struct perf_header *h __used,
610 struct perf_evlist *evlist)
612 struct perf_evsel *evsel;
616 nre = evlist->nr_entries;
619 * write number of events
621 ret = do_write(fd, &nre, sizeof(nre));
626 * size of perf_event_attr struct
628 sz = (u32)sizeof(evsel->attr);
629 ret = do_write(fd, &sz, sizeof(sz));
633 list_for_each_entry(evsel, &evlist->entries, node) {
635 ret = do_write(fd, &evsel->attr, sz);
639 * write number of unique id per event
640 * there is one id per instance of an event
642 * copy into an nri to be independent of the
646 ret = do_write(fd, &nri, sizeof(nri));
651 * write event string as passed on cmdline
653 ret = do_write_string(fd, perf_evsel__name(evsel));
657 * write unique ids for this event
659 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
666 static int write_cmdline(int fd, struct perf_header *h __used,
667 struct perf_evlist *evlist __used)
669 char buf[MAXPATHLEN];
675 * actual atual path to perf binary
677 sprintf(proc, "/proc/%d/exe", getpid());
678 ret = readlink(proc, buf, sizeof(buf));
682 /* readlink() does not add null termination */
685 /* account for binary path */
688 ret = do_write(fd, &n, sizeof(n));
692 ret = do_write_string(fd, buf);
696 for (i = 0 ; i < header_argc; i++) {
697 ret = do_write_string(fd, header_argv[i]);
704 #define CORE_SIB_FMT \
705 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
706 #define THRD_SIB_FMT \
707 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
712 char **core_siblings;
713 char **thread_siblings;
716 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
719 char filename[MAXPATHLEN];
720 char *buf = NULL, *p;
725 sprintf(filename, CORE_SIB_FMT, cpu);
726 fp = fopen(filename, "r");
730 if (getline(&buf, &len, fp) <= 0)
735 p = strchr(buf, '\n');
739 for (i = 0; i < tp->core_sib; i++) {
740 if (!strcmp(buf, tp->core_siblings[i]))
743 if (i == tp->core_sib) {
744 tp->core_siblings[i] = buf;
750 sprintf(filename, THRD_SIB_FMT, cpu);
751 fp = fopen(filename, "r");
755 if (getline(&buf, &len, fp) <= 0)
758 p = strchr(buf, '\n');
762 for (i = 0; i < tp->thread_sib; i++) {
763 if (!strcmp(buf, tp->thread_siblings[i]))
766 if (i == tp->thread_sib) {
767 tp->thread_siblings[i] = buf;
779 static void free_cpu_topo(struct cpu_topo *tp)
786 for (i = 0 ; i < tp->core_sib; i++)
787 free(tp->core_siblings[i]);
789 for (i = 0 ; i < tp->thread_sib; i++)
790 free(tp->thread_siblings[i]);
795 static struct cpu_topo *build_cpu_topology(void)
804 ncpus = sysconf(_SC_NPROCESSORS_CONF);
808 nr = (u32)(ncpus & UINT_MAX);
810 sz = nr * sizeof(char *);
812 addr = calloc(1, sizeof(*tp) + 2 * sz);
819 tp->core_siblings = addr;
821 tp->thread_siblings = addr;
823 for (i = 0; i < nr; i++) {
824 ret = build_cpu_topo(tp, i);
835 static int write_cpu_topology(int fd, struct perf_header *h __used,
836 struct perf_evlist *evlist __used)
842 tp = build_cpu_topology();
846 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
850 for (i = 0; i < tp->core_sib; i++) {
851 ret = do_write_string(fd, tp->core_siblings[i]);
855 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
859 for (i = 0; i < tp->thread_sib; i++) {
860 ret = do_write_string(fd, tp->thread_siblings[i]);
871 static int write_total_mem(int fd, struct perf_header *h __used,
872 struct perf_evlist *evlist __used)
880 fp = fopen("/proc/meminfo", "r");
884 while (getline(&buf, &len, fp) > 0) {
885 ret = strncmp(buf, "MemTotal:", 9);
890 n = sscanf(buf, "%*s %"PRIu64, &mem);
892 ret = do_write(fd, &mem, sizeof(mem));
899 static int write_topo_node(int fd, int node)
901 char str[MAXPATHLEN];
903 char *buf = NULL, *p;
906 u64 mem_total, mem_free, mem;
909 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
910 fp = fopen(str, "r");
914 while (getline(&buf, &len, fp) > 0) {
915 /* skip over invalid lines */
916 if (!strchr(buf, ':'))
918 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
920 if (!strcmp(field, "MemTotal:"))
922 if (!strcmp(field, "MemFree:"))
928 ret = do_write(fd, &mem_total, sizeof(u64));
932 ret = do_write(fd, &mem_free, sizeof(u64));
937 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
939 fp = fopen(str, "r");
943 if (getline(&buf, &len, fp) <= 0)
946 p = strchr(buf, '\n');
950 ret = do_write_string(fd, buf);
957 static int write_numa_topology(int fd, struct perf_header *h __used,
958 struct perf_evlist *evlist __used)
963 struct cpu_map *node_map = NULL;
968 fp = fopen("/sys/devices/system/node/online", "r");
972 if (getline(&buf, &len, fp) <= 0)
975 c = strchr(buf, '\n');
979 node_map = cpu_map__new(buf);
983 nr = (u32)node_map->nr;
985 ret = do_write(fd, &nr, sizeof(nr));
989 for (i = 0; i < nr; i++) {
990 j = (u32)node_map->map[i];
991 ret = do_write(fd, &j, sizeof(j));
995 ret = write_topo_node(fd, i);
1009 * struct pmu_mappings {
1018 static int write_pmu_mappings(int fd, struct perf_header *h __used,
1019 struct perf_evlist *evlist __used)
1021 struct perf_pmu *pmu = NULL;
1022 off_t offset = lseek(fd, 0, SEEK_CUR);
1025 /* write real pmu_num later */
1026 do_write(fd, &pmu_num, sizeof(pmu_num));
1028 while ((pmu = perf_pmu__scan(pmu))) {
1032 do_write(fd, &pmu->type, sizeof(pmu->type));
1033 do_write_string(fd, pmu->name);
1036 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1038 lseek(fd, offset, SEEK_SET);
1046 * default get_cpuid(): nothing gets recorded
1047 * actual implementation must be in arch/$(ARCH)/util/header.c
1049 int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
1054 static int write_cpuid(int fd, struct perf_header *h __used,
1055 struct perf_evlist *evlist __used)
1060 ret = get_cpuid(buffer, sizeof(buffer));
1066 return do_write_string(fd, buffer);
1069 static int write_branch_stack(int fd __used, struct perf_header *h __used,
1070 struct perf_evlist *evlist __used)
1075 static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1077 char *str = do_read_string(fd, ph);
1078 fprintf(fp, "# hostname : %s\n", str);
1082 static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1084 char *str = do_read_string(fd, ph);
1085 fprintf(fp, "# os release : %s\n", str);
1089 static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1091 char *str = do_read_string(fd, ph);
1092 fprintf(fp, "# arch : %s\n", str);
1096 static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1098 char *str = do_read_string(fd, ph);
1099 fprintf(fp, "# cpudesc : %s\n", str);
1103 static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1108 ret = read(fd, &nr, sizeof(nr));
1109 if (ret != (ssize_t)sizeof(nr))
1110 nr = -1; /* interpreted as error */
1115 fprintf(fp, "# nrcpus online : %u\n", nr);
1117 ret = read(fd, &nr, sizeof(nr));
1118 if (ret != (ssize_t)sizeof(nr))
1119 nr = -1; /* interpreted as error */
1124 fprintf(fp, "# nrcpus avail : %u\n", nr);
1127 static void print_version(struct perf_header *ph, int fd, FILE *fp)
1129 char *str = do_read_string(fd, ph);
1130 fprintf(fp, "# perf version : %s\n", str);
1134 static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1140 ret = read(fd, &nr, sizeof(nr));
1141 if (ret != (ssize_t)sizeof(nr))
1147 fprintf(fp, "# cmdline : ");
1149 for (i = 0; i < nr; i++) {
1150 str = do_read_string(fd, ph);
1151 fprintf(fp, "%s ", str);
1157 static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1163 ret = read(fd, &nr, sizeof(nr));
1164 if (ret != (ssize_t)sizeof(nr))
1170 for (i = 0; i < nr; i++) {
1171 str = do_read_string(fd, ph);
1172 fprintf(fp, "# sibling cores : %s\n", str);
1176 ret = read(fd, &nr, sizeof(nr));
1177 if (ret != (ssize_t)sizeof(nr))
1183 for (i = 0; i < nr; i++) {
1184 str = do_read_string(fd, ph);
1185 fprintf(fp, "# sibling threads : %s\n", str);
1190 static void free_event_desc(struct perf_evsel *events)
1192 struct perf_evsel *evsel;
1197 for (evsel = events; evsel->attr.size; evsel++) {
1207 static struct perf_evsel *
1208 read_event_desc(struct perf_header *ph, int fd)
1210 struct perf_evsel *evsel, *events = NULL;
1213 u32 nre, sz, nr, i, j;
1217 /* number of events */
1218 ret = read(fd, &nre, sizeof(nre));
1219 if (ret != (ssize_t)sizeof(nre))
1223 nre = bswap_32(nre);
1225 ret = read(fd, &sz, sizeof(sz));
1226 if (ret != (ssize_t)sizeof(sz))
1232 /* buffer to hold on file attr struct */
1237 /* the last event terminates with evsel->attr.size == 0: */
1238 events = calloc(nre + 1, sizeof(*events));
1242 msz = sizeof(evsel->attr);
1246 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1250 * must read entire on-file attr struct to
1251 * sync up with layout.
1253 ret = read(fd, buf, sz);
1254 if (ret != (ssize_t)sz)
1258 perf_event__attr_swap(buf);
1260 memcpy(&evsel->attr, buf, msz);
1262 ret = read(fd, &nr, sizeof(nr));
1263 if (ret != (ssize_t)sizeof(nr))
1269 evsel->name = do_read_string(fd, ph);
1274 id = calloc(nr, sizeof(*id));
1280 for (j = 0 ; j < nr; j++) {
1281 ret = read(fd, id, sizeof(*id));
1282 if (ret != (ssize_t)sizeof(*id))
1285 *id = bswap_64(*id);
1295 free_event_desc(events);
1300 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1302 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1307 fprintf(fp, "# event desc: not available or unable to read\n");
1311 for (evsel = events; evsel->attr.size; evsel++) {
1312 fprintf(fp, "# event : name = %s, ", evsel->name);
1314 fprintf(fp, "type = %d, config = 0x%"PRIx64
1315 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1317 (u64)evsel->attr.config,
1318 (u64)evsel->attr.config1,
1319 (u64)evsel->attr.config2);
1321 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1322 evsel->attr.exclude_user,
1323 evsel->attr.exclude_kernel);
1325 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1326 evsel->attr.exclude_host,
1327 evsel->attr.exclude_guest);
1329 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1332 fprintf(fp, ", id = {");
1333 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1336 fprintf(fp, " %"PRIu64, *id);
1344 free_event_desc(events);
1347 static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1352 ret = read(fd, &mem, sizeof(mem));
1353 if (ret != sizeof(mem))
1357 mem = bswap_64(mem);
1359 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1362 fprintf(fp, "# total memory : unknown\n");
1365 static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1370 uint64_t mem_total, mem_free;
1373 ret = read(fd, &nr, sizeof(nr));
1374 if (ret != (ssize_t)sizeof(nr))
1380 for (i = 0; i < nr; i++) {
1383 ret = read(fd, &c, sizeof(c));
1384 if (ret != (ssize_t)sizeof(c))
1390 ret = read(fd, &mem_total, sizeof(u64));
1391 if (ret != sizeof(u64))
1394 ret = read(fd, &mem_free, sizeof(u64));
1395 if (ret != sizeof(u64))
1398 if (h->needs_swap) {
1399 mem_total = bswap_64(mem_total);
1400 mem_free = bswap_64(mem_free);
1403 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1404 " free = %"PRIu64" kB\n",
1409 str = do_read_string(fd, h);
1410 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1415 fprintf(fp, "# numa topology : not available\n");
1418 static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1420 char *str = do_read_string(fd, ph);
1421 fprintf(fp, "# cpuid : %s\n", str);
1425 static void print_branch_stack(struct perf_header *ph __used, int fd __used,
1428 fprintf(fp, "# contains samples with branch stack\n");
1431 static void print_pmu_mappings(struct perf_header *ph, int fd, FILE *fp)
1433 const char *delimiter = "# pmu mappings: ";
1439 ret = read(fd, &pmu_num, sizeof(pmu_num));
1440 if (ret != sizeof(pmu_num))
1444 fprintf(fp, "# pmu mappings: not available\n");
1449 if (read(fd, &type, sizeof(type)) != sizeof(type))
1451 name = do_read_string(fd, ph);
1455 fprintf(fp, "%s%s = %" PRIu32, delimiter, name, type);
1465 fprintf(fp, "# pmu mappings: unable to read\n");
1468 static int __event_process_build_id(struct build_id_event *bev,
1470 struct perf_session *session)
1473 struct list_head *head;
1474 struct machine *machine;
1477 enum dso_kernel_type dso_type;
1479 machine = perf_session__findnew_machine(session, bev->pid);
1483 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1486 case PERF_RECORD_MISC_KERNEL:
1487 dso_type = DSO_TYPE_KERNEL;
1488 head = &machine->kernel_dsos;
1490 case PERF_RECORD_MISC_GUEST_KERNEL:
1491 dso_type = DSO_TYPE_GUEST_KERNEL;
1492 head = &machine->kernel_dsos;
1494 case PERF_RECORD_MISC_USER:
1495 case PERF_RECORD_MISC_GUEST_USER:
1496 dso_type = DSO_TYPE_USER;
1497 head = &machine->user_dsos;
1503 dso = __dsos__findnew(head, filename);
1505 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1507 dso__set_build_id(dso, &bev->build_id);
1509 if (filename[0] == '[')
1510 dso->kernel = dso_type;
1512 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1514 pr_debug("build id event received for %s: %s\n",
1515 dso->long_name, sbuild_id);
1523 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1524 int input, u64 offset, u64 size)
1526 struct perf_session *session = container_of(header, struct perf_session, header);
1528 struct perf_event_header header;
1529 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1532 struct build_id_event bev;
1533 char filename[PATH_MAX];
1534 u64 limit = offset + size;
1536 while (offset < limit) {
1539 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1542 if (header->needs_swap)
1543 perf_event_header__bswap(&old_bev.header);
1545 len = old_bev.header.size - sizeof(old_bev);
1546 if (read(input, filename, len) != len)
1549 bev.header = old_bev.header;
1552 * As the pid is the missing value, we need to fill
1553 * it properly. The header.misc value give us nice hint.
1555 bev.pid = HOST_KERNEL_ID;
1556 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1557 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1558 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1560 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1561 __event_process_build_id(&bev, filename, session);
1563 offset += bev.header.size;
1569 static int perf_header__read_build_ids(struct perf_header *header,
1570 int input, u64 offset, u64 size)
1572 struct perf_session *session = container_of(header, struct perf_session, header);
1573 struct build_id_event bev;
1574 char filename[PATH_MAX];
1575 u64 limit = offset + size, orig_offset = offset;
1578 while (offset < limit) {
1581 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1584 if (header->needs_swap)
1585 perf_event_header__bswap(&bev.header);
1587 len = bev.header.size - sizeof(bev);
1588 if (read(input, filename, len) != len)
1591 * The a1645ce1 changeset:
1593 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1595 * Added a field to struct build_id_event that broke the file
1598 * Since the kernel build-id is the first entry, process the
1599 * table using the old format if the well known
1600 * '[kernel.kallsyms]' string for the kernel build-id has the
1601 * first 4 characters chopped off (where the pid_t sits).
1603 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1604 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1606 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1609 __event_process_build_id(&bev, filename, session);
1611 offset += bev.header.size;
1618 static int process_tracing_data(struct perf_file_section *section __unused,
1619 struct perf_header *ph __unused,
1620 int feat __unused, int fd, void *data)
1622 trace_report(fd, data, false);
1626 static int process_build_id(struct perf_file_section *section,
1627 struct perf_header *ph,
1628 int feat __unused, int fd, void *data __used)
1630 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1631 pr_debug("Failed to read buildids, continuing...\n");
1635 static struct perf_evsel *
1636 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1638 struct perf_evsel *evsel;
1640 list_for_each_entry(evsel, &evlist->entries, node) {
1641 if (evsel->idx == idx)
1649 perf_evlist__set_event_name(struct perf_evlist *evlist, struct perf_evsel *event)
1651 struct perf_evsel *evsel;
1656 evsel = perf_evlist__find_by_index(evlist, event->idx);
1663 evsel->name = strdup(event->name);
1667 process_event_desc(struct perf_file_section *section __unused,
1668 struct perf_header *header, int feat __unused, int fd,
1671 struct perf_session *session = container_of(header, struct perf_session, header);
1672 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1677 for (evsel = events; evsel->attr.size; evsel++)
1678 perf_evlist__set_event_name(session->evlist, evsel);
1680 free_event_desc(events);
1685 struct feature_ops {
1686 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1687 void (*print)(struct perf_header *h, int fd, FILE *fp);
1688 int (*process)(struct perf_file_section *section,
1689 struct perf_header *h, int feat, int fd, void *data);
1694 #define FEAT_OPA(n, func) \
1695 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1696 #define FEAT_OPP(n, func) \
1697 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1698 .process = process_##func }
1699 #define FEAT_OPF(n, func) \
1700 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1703 /* feature_ops not implemented: */
1704 #define print_tracing_data NULL
1705 #define print_build_id NULL
1707 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1708 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1709 FEAT_OPP(HEADER_BUILD_ID, build_id),
1710 FEAT_OPA(HEADER_HOSTNAME, hostname),
1711 FEAT_OPA(HEADER_OSRELEASE, osrelease),
1712 FEAT_OPA(HEADER_VERSION, version),
1713 FEAT_OPA(HEADER_ARCH, arch),
1714 FEAT_OPA(HEADER_NRCPUS, nrcpus),
1715 FEAT_OPA(HEADER_CPUDESC, cpudesc),
1716 FEAT_OPA(HEADER_CPUID, cpuid),
1717 FEAT_OPA(HEADER_TOTAL_MEM, total_mem),
1718 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
1719 FEAT_OPA(HEADER_CMDLINE, cmdline),
1720 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1721 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1722 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
1723 FEAT_OPA(HEADER_PMU_MAPPINGS, pmu_mappings),
1726 struct header_print_data {
1728 bool full; /* extended list of headers */
1731 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1732 struct perf_header *ph,
1733 int feat, int fd, void *data)
1735 struct header_print_data *hd = data;
1737 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1738 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1739 "%d, continuing...\n", section->offset, feat);
1742 if (feat >= HEADER_LAST_FEATURE) {
1743 pr_warning("unknown feature %d\n", feat);
1746 if (!feat_ops[feat].print)
1749 if (!feat_ops[feat].full_only || hd->full)
1750 feat_ops[feat].print(ph, fd, hd->fp);
1752 fprintf(hd->fp, "# %s info available, use -I to display\n",
1753 feat_ops[feat].name);
1758 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1760 struct header_print_data hd;
1761 struct perf_header *header = &session->header;
1762 int fd = session->fd;
1766 perf_header__process_sections(header, fd, &hd,
1767 perf_file_section__fprintf_info);
1771 static int do_write_feat(int fd, struct perf_header *h, int type,
1772 struct perf_file_section **p,
1773 struct perf_evlist *evlist)
1778 if (perf_header__has_feat(h, type)) {
1779 if (!feat_ops[type].write)
1782 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1784 err = feat_ops[type].write(fd, h, evlist);
1786 pr_debug("failed to write feature %d\n", type);
1788 /* undo anything written */
1789 lseek(fd, (*p)->offset, SEEK_SET);
1793 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1799 static int perf_header__adds_write(struct perf_header *header,
1800 struct perf_evlist *evlist, int fd)
1803 struct perf_file_section *feat_sec, *p;
1809 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1813 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
1814 if (feat_sec == NULL)
1817 sec_size = sizeof(*feat_sec) * nr_sections;
1819 sec_start = header->data_offset + header->data_size;
1820 lseek(fd, sec_start + sec_size, SEEK_SET);
1822 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1823 if (do_write_feat(fd, header, feat, &p, evlist))
1824 perf_header__clear_feat(header, feat);
1827 lseek(fd, sec_start, SEEK_SET);
1829 * may write more than needed due to dropped feature, but
1830 * this is okay, reader will skip the mising entries
1832 err = do_write(fd, feat_sec, sec_size);
1834 pr_debug("failed to write feature section\n");
1839 int perf_header__write_pipe(int fd)
1841 struct perf_pipe_file_header f_header;
1844 f_header = (struct perf_pipe_file_header){
1845 .magic = PERF_MAGIC,
1846 .size = sizeof(f_header),
1849 err = do_write(fd, &f_header, sizeof(f_header));
1851 pr_debug("failed to write perf pipe header\n");
1858 int perf_session__write_header(struct perf_session *session,
1859 struct perf_evlist *evlist,
1860 int fd, bool at_exit)
1862 struct perf_file_header f_header;
1863 struct perf_file_attr f_attr;
1864 struct perf_header *header = &session->header;
1865 struct perf_evsel *evsel, *pair = NULL;
1868 lseek(fd, sizeof(f_header), SEEK_SET);
1870 if (session->evlist != evlist)
1871 pair = perf_evlist__first(session->evlist);
1873 list_for_each_entry(evsel, &evlist->entries, node) {
1874 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
1875 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
1878 pr_debug("failed to write perf header\n");
1881 if (session->evlist != evlist) {
1882 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1885 evsel->ids += pair->ids;
1886 pair = perf_evsel__next(pair);
1890 header->attr_offset = lseek(fd, 0, SEEK_CUR);
1892 list_for_each_entry(evsel, &evlist->entries, node) {
1893 f_attr = (struct perf_file_attr){
1894 .attr = evsel->attr,
1896 .offset = evsel->id_offset,
1897 .size = evsel->ids * sizeof(u64),
1900 err = do_write(fd, &f_attr, sizeof(f_attr));
1902 pr_debug("failed to write perf header attribute\n");
1907 header->event_offset = lseek(fd, 0, SEEK_CUR);
1908 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
1910 err = do_write(fd, trace_events, header->event_size);
1912 pr_debug("failed to write perf header events\n");
1917 header->data_offset = lseek(fd, 0, SEEK_CUR);
1920 err = perf_header__adds_write(header, evlist, fd);
1925 f_header = (struct perf_file_header){
1926 .magic = PERF_MAGIC,
1927 .size = sizeof(f_header),
1928 .attr_size = sizeof(f_attr),
1930 .offset = header->attr_offset,
1931 .size = evlist->nr_entries * sizeof(f_attr),
1934 .offset = header->data_offset,
1935 .size = header->data_size,
1938 .offset = header->event_offset,
1939 .size = header->event_size,
1943 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
1945 lseek(fd, 0, SEEK_SET);
1946 err = do_write(fd, &f_header, sizeof(f_header));
1948 pr_debug("failed to write perf header\n");
1951 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1957 static int perf_header__getbuffer64(struct perf_header *header,
1958 int fd, void *buf, size_t size)
1960 if (readn(fd, buf, size) <= 0)
1963 if (header->needs_swap)
1964 mem_bswap_64(buf, size);
1969 int perf_header__process_sections(struct perf_header *header, int fd,
1971 int (*process)(struct perf_file_section *section,
1972 struct perf_header *ph,
1973 int feat, int fd, void *data))
1975 struct perf_file_section *feat_sec, *sec;
1981 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1985 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
1989 sec_size = sizeof(*feat_sec) * nr_sections;
1991 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1993 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
1997 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
1998 err = process(sec++, header, feat, fd, data);
2008 static const int attr_file_abi_sizes[] = {
2009 [0] = PERF_ATTR_SIZE_VER0,
2010 [1] = PERF_ATTR_SIZE_VER1,
2011 [2] = PERF_ATTR_SIZE_VER2,
2012 [3] = PERF_ATTR_SIZE_VER3,
2017 * In the legacy file format, the magic number is not used to encode endianness.
2018 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2019 * on ABI revisions, we need to try all combinations for all endianness to
2020 * detect the endianness.
2022 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2024 uint64_t ref_size, attr_size;
2027 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2028 ref_size = attr_file_abi_sizes[i]
2029 + sizeof(struct perf_file_section);
2030 if (hdr_sz != ref_size) {
2031 attr_size = bswap_64(hdr_sz);
2032 if (attr_size != ref_size)
2035 ph->needs_swap = true;
2037 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2042 /* could not determine endianness */
2046 #define PERF_PIPE_HDR_VER0 16
2048 static const size_t attr_pipe_abi_sizes[] = {
2049 [0] = PERF_PIPE_HDR_VER0,
2054 * In the legacy pipe format, there is an implicit assumption that endiannesss
2055 * between host recording the samples, and host parsing the samples is the
2056 * same. This is not always the case given that the pipe output may always be
2057 * redirected into a file and analyzed on a different machine with possibly a
2058 * different endianness and perf_event ABI revsions in the perf tool itself.
2060 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2065 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2066 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2067 attr_size = bswap_64(hdr_sz);
2068 if (attr_size != hdr_sz)
2071 ph->needs_swap = true;
2073 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2079 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2080 bool is_pipe, struct perf_header *ph)
2084 /* check for legacy format */
2085 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2087 pr_debug("legacy perf.data format\n");
2089 return try_all_pipe_abis(hdr_sz, ph);
2091 return try_all_file_abis(hdr_sz, ph);
2094 * the new magic number serves two purposes:
2095 * - unique number to identify actual perf.data files
2096 * - encode endianness of file
2099 /* check magic number with one endianness */
2100 if (magic == __perf_magic2)
2103 /* check magic number with opposite endianness */
2104 if (magic != __perf_magic2_sw)
2107 ph->needs_swap = true;
2112 int perf_file_header__read(struct perf_file_header *header,
2113 struct perf_header *ph, int fd)
2117 lseek(fd, 0, SEEK_SET);
2119 ret = readn(fd, header, sizeof(*header));
2123 if (check_magic_endian(header->magic,
2124 header->attr_size, false, ph) < 0) {
2125 pr_debug("magic/endian check failed\n");
2129 if (ph->needs_swap) {
2130 mem_bswap_64(header, offsetof(struct perf_file_header,
2134 if (header->size != sizeof(*header)) {
2135 /* Support the previous format */
2136 if (header->size == offsetof(typeof(*header), adds_features))
2137 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2140 } else if (ph->needs_swap) {
2142 * feature bitmap is declared as an array of unsigned longs --
2143 * not good since its size can differ between the host that
2144 * generated the data file and the host analyzing the file.
2146 * We need to handle endianness, but we don't know the size of
2147 * the unsigned long where the file was generated. Take a best
2148 * guess at determining it: try 64-bit swap first (ie., file
2149 * created on a 64-bit host), and check if the hostname feature
2150 * bit is set (this feature bit is forced on as of fbe96f2).
2151 * If the bit is not, undo the 64-bit swap and try a 32-bit
2152 * swap. If the hostname bit is still not set (e.g., older data
2153 * file), punt and fallback to the original behavior --
2154 * clearing all feature bits and setting buildid.
2156 mem_bswap_64(&header->adds_features,
2157 BITS_TO_U64(HEADER_FEAT_BITS));
2159 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2161 mem_bswap_64(&header->adds_features,
2162 BITS_TO_U64(HEADER_FEAT_BITS));
2165 mem_bswap_32(&header->adds_features,
2166 BITS_TO_U32(HEADER_FEAT_BITS));
2169 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2170 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2171 set_bit(HEADER_BUILD_ID, header->adds_features);
2175 memcpy(&ph->adds_features, &header->adds_features,
2176 sizeof(ph->adds_features));
2178 ph->event_offset = header->event_types.offset;
2179 ph->event_size = header->event_types.size;
2180 ph->data_offset = header->data.offset;
2181 ph->data_size = header->data.size;
2185 static int perf_file_section__process(struct perf_file_section *section,
2186 struct perf_header *ph,
2187 int feat, int fd, void *data)
2189 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2190 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2191 "%d, continuing...\n", section->offset, feat);
2195 if (feat >= HEADER_LAST_FEATURE) {
2196 pr_debug("unknown feature %d, continuing...\n", feat);
2200 if (!feat_ops[feat].process)
2203 return feat_ops[feat].process(section, ph, feat, fd, data);
2206 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2207 struct perf_header *ph, int fd,
2212 ret = readn(fd, header, sizeof(*header));
2216 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2217 pr_debug("endian/magic failed\n");
2222 header->size = bswap_64(header->size);
2224 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2230 static int perf_header__read_pipe(struct perf_session *session, int fd)
2232 struct perf_header *header = &session->header;
2233 struct perf_pipe_file_header f_header;
2235 if (perf_file_header__read_pipe(&f_header, header, fd,
2236 session->repipe) < 0) {
2237 pr_debug("incompatible file format\n");
2246 static int read_attr(int fd, struct perf_header *ph,
2247 struct perf_file_attr *f_attr)
2249 struct perf_event_attr *attr = &f_attr->attr;
2251 size_t our_sz = sizeof(f_attr->attr);
2254 memset(f_attr, 0, sizeof(*f_attr));
2256 /* read minimal guaranteed structure */
2257 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2259 pr_debug("cannot read %d bytes of header attr\n",
2260 PERF_ATTR_SIZE_VER0);
2264 /* on file perf_event_attr size */
2272 sz = PERF_ATTR_SIZE_VER0;
2273 } else if (sz > our_sz) {
2274 pr_debug("file uses a more recent and unsupported ABI"
2275 " (%zu bytes extra)\n", sz - our_sz);
2278 /* what we have not yet read and that we know about */
2279 left = sz - PERF_ATTR_SIZE_VER0;
2282 ptr += PERF_ATTR_SIZE_VER0;
2284 ret = readn(fd, ptr, left);
2286 /* read perf_file_section, ids are read in caller */
2287 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2289 return ret <= 0 ? -1 : 0;
2292 static int perf_evsel__set_tracepoint_name(struct perf_evsel *evsel,
2293 struct pevent *pevent)
2295 struct event_format *event = pevent_find_event(pevent,
2296 evsel->attr.config);
2302 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2303 evsel->name = strdup(bf);
2304 if (event->name == NULL)
2307 evsel->tp_format = event;
2311 static int perf_evlist__set_tracepoint_names(struct perf_evlist *evlist,
2312 struct pevent *pevent)
2314 struct perf_evsel *pos;
2316 list_for_each_entry(pos, &evlist->entries, node) {
2317 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2318 perf_evsel__set_tracepoint_name(pos, pevent))
2325 int perf_session__read_header(struct perf_session *session, int fd)
2327 struct perf_header *header = &session->header;
2328 struct perf_file_header f_header;
2329 struct perf_file_attr f_attr;
2331 int nr_attrs, nr_ids, i, j;
2333 session->evlist = perf_evlist__new(NULL, NULL);
2334 if (session->evlist == NULL)
2337 if (session->fd_pipe)
2338 return perf_header__read_pipe(session, fd);
2340 if (perf_file_header__read(&f_header, header, fd) < 0)
2343 nr_attrs = f_header.attrs.size / f_header.attr_size;
2344 lseek(fd, f_header.attrs.offset, SEEK_SET);
2346 for (i = 0; i < nr_attrs; i++) {
2347 struct perf_evsel *evsel;
2350 if (read_attr(fd, header, &f_attr) < 0)
2353 if (header->needs_swap)
2354 perf_event__attr_swap(&f_attr.attr);
2356 tmp = lseek(fd, 0, SEEK_CUR);
2357 evsel = perf_evsel__new(&f_attr.attr, i);
2360 goto out_delete_evlist;
2362 * Do it before so that if perf_evsel__alloc_id fails, this
2363 * entry gets purged too at perf_evlist__delete().
2365 perf_evlist__add(session->evlist, evsel);
2367 nr_ids = f_attr.ids.size / sizeof(u64);
2369 * We don't have the cpu and thread maps on the header, so
2370 * for allocating the perf_sample_id table we fake 1 cpu and
2371 * hattr->ids threads.
2373 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2374 goto out_delete_evlist;
2376 lseek(fd, f_attr.ids.offset, SEEK_SET);
2378 for (j = 0; j < nr_ids; j++) {
2379 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2382 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2385 lseek(fd, tmp, SEEK_SET);
2388 symbol_conf.nr_events = nr_attrs;
2390 if (f_header.event_types.size) {
2391 lseek(fd, f_header.event_types.offset, SEEK_SET);
2392 trace_events = malloc(f_header.event_types.size);
2393 if (trace_events == NULL)
2395 if (perf_header__getbuffer64(header, fd, trace_events,
2396 f_header.event_types.size))
2398 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2401 perf_header__process_sections(header, fd, &session->pevent,
2402 perf_file_section__process);
2404 lseek(fd, header->data_offset, SEEK_SET);
2406 if (perf_evlist__set_tracepoint_names(session->evlist, session->pevent))
2407 goto out_delete_evlist;
2415 perf_evlist__delete(session->evlist);
2416 session->evlist = NULL;
2420 int perf_event__synthesize_attr(struct perf_tool *tool,
2421 struct perf_event_attr *attr, u32 ids, u64 *id,
2422 perf_event__handler_t process)
2424 union perf_event *ev;
2428 size = sizeof(struct perf_event_attr);
2429 size = ALIGN(size, sizeof(u64));
2430 size += sizeof(struct perf_event_header);
2431 size += ids * sizeof(u64);
2438 ev->attr.attr = *attr;
2439 memcpy(ev->attr.id, id, ids * sizeof(u64));
2441 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2442 ev->attr.header.size = (u16)size;
2444 if (ev->attr.header.size == size)
2445 err = process(tool, ev, NULL, NULL);
2454 int perf_event__synthesize_attrs(struct perf_tool *tool,
2455 struct perf_session *session,
2456 perf_event__handler_t process)
2458 struct perf_evsel *evsel;
2461 list_for_each_entry(evsel, &session->evlist->entries, node) {
2462 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2463 evsel->id, process);
2465 pr_debug("failed to create perf header attribute\n");
2473 int perf_event__process_attr(union perf_event *event,
2474 struct perf_evlist **pevlist)
2477 struct perf_evsel *evsel;
2478 struct perf_evlist *evlist = *pevlist;
2480 if (evlist == NULL) {
2481 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2486 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2490 perf_evlist__add(evlist, evsel);
2492 ids = event->header.size;
2493 ids -= (void *)&event->attr.id - (void *)event;
2494 n_ids = ids / sizeof(u64);
2496 * We don't have the cpu and thread maps on the header, so
2497 * for allocating the perf_sample_id table we fake 1 cpu and
2498 * hattr->ids threads.
2500 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2503 for (i = 0; i < n_ids; i++) {
2504 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2510 int perf_event__synthesize_event_type(struct perf_tool *tool,
2511 u64 event_id, char *name,
2512 perf_event__handler_t process,
2513 struct machine *machine)
2515 union perf_event ev;
2519 memset(&ev, 0, sizeof(ev));
2521 ev.event_type.event_type.event_id = event_id;
2522 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2523 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2525 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2526 size = strlen(ev.event_type.event_type.name);
2527 size = ALIGN(size, sizeof(u64));
2528 ev.event_type.header.size = sizeof(ev.event_type) -
2529 (sizeof(ev.event_type.event_type.name) - size);
2531 err = process(tool, &ev, NULL, machine);
2536 int perf_event__synthesize_event_types(struct perf_tool *tool,
2537 perf_event__handler_t process,
2538 struct machine *machine)
2540 struct perf_trace_event_type *type;
2543 for (i = 0; i < trace_event_count; i++) {
2544 type = &trace_events[i];
2546 err = perf_event__synthesize_event_type(tool, type->event_id,
2547 type->name, process,
2550 pr_debug("failed to create perf header event type\n");
2558 int perf_event__process_event_type(struct perf_tool *tool __unused,
2559 union perf_event *event)
2561 if (perf_header__push_event(event->event_type.event_type.event_id,
2562 event->event_type.event_type.name) < 0)
2568 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2569 struct perf_evlist *evlist,
2570 perf_event__handler_t process)
2572 union perf_event ev;
2573 struct tracing_data *tdata;
2574 ssize_t size = 0, aligned_size = 0, padding;
2578 * We are going to store the size of the data followed
2579 * by the data contents. Since the fd descriptor is a pipe,
2580 * we cannot seek back to store the size of the data once
2581 * we know it. Instead we:
2583 * - write the tracing data to the temp file
2584 * - get/write the data size to pipe
2585 * - write the tracing data from the temp file
2588 tdata = tracing_data_get(&evlist->entries, fd, true);
2592 memset(&ev, 0, sizeof(ev));
2594 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2596 aligned_size = ALIGN(size, sizeof(u64));
2597 padding = aligned_size - size;
2598 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2599 ev.tracing_data.size = aligned_size;
2601 process(tool, &ev, NULL, NULL);
2604 * The put function will copy all the tracing data
2605 * stored in temp file to the pipe.
2607 tracing_data_put(tdata);
2609 write_padded(fd, NULL, 0, padding);
2611 return aligned_size;
2614 int perf_event__process_tracing_data(union perf_event *event,
2615 struct perf_session *session)
2617 ssize_t size_read, padding, size = event->tracing_data.size;
2618 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2621 /* setup for reading amidst mmap */
2622 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2625 size_read = trace_report(session->fd, &session->pevent,
2627 padding = ALIGN(size_read, sizeof(u64)) - size_read;
2629 if (read(session->fd, buf, padding) < 0)
2630 die("reading input file");
2631 if (session->repipe) {
2632 int retw = write(STDOUT_FILENO, buf, padding);
2633 if (retw <= 0 || retw != padding)
2634 die("repiping tracing data padding");
2637 if (size_read + padding != size)
2638 die("tracing data size mismatch");
2640 perf_evlist__set_tracepoint_names(session->evlist, session->pevent);
2642 return size_read + padding;
2645 int perf_event__synthesize_build_id(struct perf_tool *tool,
2646 struct dso *pos, u16 misc,
2647 perf_event__handler_t process,
2648 struct machine *machine)
2650 union perf_event ev;
2657 memset(&ev, 0, sizeof(ev));
2659 len = pos->long_name_len + 1;
2660 len = ALIGN(len, NAME_ALIGN);
2661 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2662 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2663 ev.build_id.header.misc = misc;
2664 ev.build_id.pid = machine->pid;
2665 ev.build_id.header.size = sizeof(ev.build_id) + len;
2666 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2668 err = process(tool, &ev, NULL, machine);
2673 int perf_event__process_build_id(struct perf_tool *tool __used,
2674 union perf_event *event,
2675 struct perf_session *session)
2677 __event_process_build_id(&event->build_id,
2678 event->build_id.filename,
2683 void disable_buildid_cache(void)
2685 no_buildid_cache = true;