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"
24 static bool no_buildid_cache = false;
26 static int event_count;
27 static struct perf_trace_event_type *events;
29 static u32 header_argc;
30 static const char **header_argv;
32 int perf_header__push_event(u64 id, const char *name)
34 struct perf_trace_event_type *nevents;
36 if (strlen(name) > MAX_EVENT_NAME)
37 pr_warning("Event %s will be truncated\n", name);
39 nevents = realloc(events, (event_count + 1) * sizeof(*events));
44 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
45 events[event_count].event_id = id;
46 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
51 char *perf_header__find_event(u64 id)
54 for (i = 0 ; i < event_count; i++) {
55 if (events[i].event_id == id)
56 return events[i].name;
63 * must be a numerical value to let the endianness
64 * determine the memory layout. That way we are able
65 * to detect endianness when reading the perf.data file
68 * we check for legacy (PERFFILE) format.
70 static const char *__perf_magic1 = "PERFFILE";
71 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
72 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
74 #define PERF_MAGIC __perf_magic2
76 struct perf_file_attr {
77 struct perf_event_attr attr;
78 struct perf_file_section ids;
81 void perf_header__set_feat(struct perf_header *header, int feat)
83 set_bit(feat, header->adds_features);
86 void perf_header__clear_feat(struct perf_header *header, int feat)
88 clear_bit(feat, header->adds_features);
91 bool perf_header__has_feat(const struct perf_header *header, int feat)
93 return test_bit(feat, header->adds_features);
96 static int do_write(int fd, const void *buf, size_t size)
99 int ret = write(fd, buf, size);
111 #define NAME_ALIGN 64
113 static int write_padded(int fd, const void *bf, size_t count,
114 size_t count_aligned)
116 static const char zero_buf[NAME_ALIGN];
117 int err = do_write(fd, bf, count);
120 err = do_write(fd, zero_buf, count_aligned - count);
125 static int do_write_string(int fd, const char *str)
130 olen = strlen(str) + 1;
131 len = ALIGN(olen, NAME_ALIGN);
133 /* write len, incl. \0 */
134 ret = do_write(fd, &len, sizeof(len));
138 return write_padded(fd, str, olen, len);
141 static char *do_read_string(int fd, struct perf_header *ph)
147 sz = read(fd, &len, sizeof(len));
148 if (sz < (ssize_t)sizeof(len))
158 ret = read(fd, buf, len);
159 if (ret == (ssize_t)len) {
161 * strings are padded by zeroes
162 * thus the actual strlen of buf
163 * may be less than len
173 perf_header__set_cmdline(int argc, const char **argv)
177 header_argc = (u32)argc;
179 /* do not include NULL termination */
180 header_argv = calloc(argc, sizeof(char *));
185 * must copy argv contents because it gets moved
186 * around during option parsing
188 for (i = 0; i < argc ; i++)
189 header_argv[i] = argv[i];
194 #define dsos__for_each_with_build_id(pos, head) \
195 list_for_each_entry(pos, head, node) \
196 if (!pos->has_build_id) \
200 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
205 dsos__for_each_with_build_id(pos, head) {
207 struct build_id_event b;
212 len = pos->long_name_len + 1;
213 len = ALIGN(len, NAME_ALIGN);
214 memset(&b, 0, sizeof(b));
215 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
217 b.header.misc = misc;
218 b.header.size = sizeof(b) + len;
219 err = do_write(fd, &b, sizeof(b));
222 err = write_padded(fd, pos->long_name,
223 pos->long_name_len + 1, len);
231 static int machine__write_buildid_table(struct machine *machine, int fd)
234 u16 kmisc = PERF_RECORD_MISC_KERNEL,
235 umisc = PERF_RECORD_MISC_USER;
237 if (!machine__is_host(machine)) {
238 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
239 umisc = PERF_RECORD_MISC_GUEST_USER;
242 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
245 err = __dsos__write_buildid_table(&machine->user_dsos,
246 machine->pid, umisc, fd);
250 static int dsos__write_buildid_table(struct perf_header *header, int fd)
252 struct perf_session *session = container_of(header,
253 struct perf_session, header);
255 int err = machine__write_buildid_table(&session->host_machine, fd);
260 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
261 struct machine *pos = rb_entry(nd, struct machine, rb_node);
262 err = machine__write_buildid_table(pos, fd);
269 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
270 const char *name, bool is_kallsyms)
272 const size_t size = PATH_MAX;
273 char *realname, *filename = zalloc(size),
274 *linkname = zalloc(size), *targetname;
278 if (symbol_conf.kptr_restrict) {
279 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
282 realname = (char *)name;
284 realname = realpath(name, NULL);
286 if (realname == NULL || filename == NULL || linkname == NULL)
289 len = scnprintf(filename, size, "%s%s%s",
290 debugdir, is_kallsyms ? "/" : "", realname);
291 if (mkdir_p(filename, 0755))
294 snprintf(filename + len, size - len, "/%s", sbuild_id);
296 if (access(filename, F_OK)) {
298 if (copyfile("/proc/kallsyms", filename))
300 } else if (link(realname, filename) && copyfile(name, filename))
304 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
305 debugdir, sbuild_id);
307 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
310 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
311 targetname = filename + strlen(debugdir) - 5;
312 memcpy(targetname, "../..", 5);
314 if (symlink(targetname, linkname) == 0)
324 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
325 const char *name, const char *debugdir,
328 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
330 build_id__sprintf(build_id, build_id_size, sbuild_id);
332 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
335 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
337 const size_t size = PATH_MAX;
338 char *filename = zalloc(size),
339 *linkname = zalloc(size);
342 if (filename == NULL || linkname == NULL)
345 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
346 debugdir, sbuild_id, sbuild_id + 2);
348 if (access(linkname, F_OK))
351 if (readlink(linkname, filename, size - 1) < 0)
354 if (unlink(linkname))
358 * Since the link is relative, we must make it absolute:
360 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
361 debugdir, sbuild_id, filename);
363 if (unlink(linkname))
373 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
375 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
377 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
378 dso->long_name, debugdir, is_kallsyms);
381 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
386 dsos__for_each_with_build_id(pos, head)
387 if (dso__cache_build_id(pos, debugdir))
393 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
395 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
396 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
400 static int perf_session__cache_build_ids(struct perf_session *session)
404 char debugdir[PATH_MAX];
406 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
408 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
411 ret = machine__cache_build_ids(&session->host_machine, debugdir);
413 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
414 struct machine *pos = rb_entry(nd, struct machine, rb_node);
415 ret |= machine__cache_build_ids(pos, debugdir);
420 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
422 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
423 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
427 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
430 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
432 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
433 struct machine *pos = rb_entry(nd, struct machine, rb_node);
434 ret |= machine__read_build_ids(pos, with_hits);
440 static int write_tracing_data(int fd, struct perf_header *h __used,
441 struct perf_evlist *evlist)
443 return read_tracing_data(fd, &evlist->entries);
447 static int write_build_id(int fd, struct perf_header *h,
448 struct perf_evlist *evlist __used)
450 struct perf_session *session;
453 session = container_of(h, struct perf_session, header);
455 if (!perf_session__read_build_ids(session, true))
458 err = dsos__write_buildid_table(h, fd);
460 pr_debug("failed to write buildid table\n");
463 if (!no_buildid_cache)
464 perf_session__cache_build_ids(session);
469 static int write_hostname(int fd, struct perf_header *h __used,
470 struct perf_evlist *evlist __used)
479 return do_write_string(fd, uts.nodename);
482 static int write_osrelease(int fd, struct perf_header *h __used,
483 struct perf_evlist *evlist __used)
492 return do_write_string(fd, uts.release);
495 static int write_arch(int fd, struct perf_header *h __used,
496 struct perf_evlist *evlist __used)
505 return do_write_string(fd, uts.machine);
508 static int write_version(int fd, struct perf_header *h __used,
509 struct perf_evlist *evlist __used)
511 return do_write_string(fd, perf_version_string);
514 static int write_cpudesc(int fd, struct perf_header *h __used,
515 struct perf_evlist *evlist __used)
518 #define CPUINFO_PROC NULL
523 const char *search = CPUINFO_PROC;
530 file = fopen("/proc/cpuinfo", "r");
534 while (getline(&buf, &len, file) > 0) {
535 ret = strncmp(buf, search, strlen(search));
545 p = strchr(buf, ':');
546 if (p && *(p+1) == ' ' && *(p+2))
552 /* squash extra space characters (branding string) */
559 while (*q && isspace(*q))
562 while ((*r++ = *q++));
566 ret = do_write_string(fd, s);
573 static int write_nrcpus(int fd, struct perf_header *h __used,
574 struct perf_evlist *evlist __used)
580 nr = sysconf(_SC_NPROCESSORS_CONF);
584 nrc = (u32)(nr & UINT_MAX);
586 nr = sysconf(_SC_NPROCESSORS_ONLN);
590 nra = (u32)(nr & UINT_MAX);
592 ret = do_write(fd, &nrc, sizeof(nrc));
596 return do_write(fd, &nra, sizeof(nra));
599 static int write_event_desc(int fd, struct perf_header *h __used,
600 struct perf_evlist *evlist)
602 struct perf_evsel *attr;
603 u32 nre = 0, nri, sz;
606 list_for_each_entry(attr, &evlist->entries, node)
610 * write number of events
612 ret = do_write(fd, &nre, sizeof(nre));
617 * size of perf_event_attr struct
619 sz = (u32)sizeof(attr->attr);
620 ret = do_write(fd, &sz, sizeof(sz));
624 list_for_each_entry(attr, &evlist->entries, node) {
626 ret = do_write(fd, &attr->attr, sz);
630 * write number of unique id per event
631 * there is one id per instance of an event
633 * copy into an nri to be independent of the
637 ret = do_write(fd, &nri, sizeof(nri));
642 * write event string as passed on cmdline
644 ret = do_write_string(fd, perf_evsel__name(attr));
648 * write unique ids for this event
650 ret = do_write(fd, attr->id, attr->ids * sizeof(u64));
657 static int write_cmdline(int fd, struct perf_header *h __used,
658 struct perf_evlist *evlist __used)
660 char buf[MAXPATHLEN];
666 * actual atual path to perf binary
668 sprintf(proc, "/proc/%d/exe", getpid());
669 ret = readlink(proc, buf, sizeof(buf));
673 /* readlink() does not add null termination */
676 /* account for binary path */
679 ret = do_write(fd, &n, sizeof(n));
683 ret = do_write_string(fd, buf);
687 for (i = 0 ; i < header_argc; i++) {
688 ret = do_write_string(fd, header_argv[i]);
695 #define CORE_SIB_FMT \
696 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
697 #define THRD_SIB_FMT \
698 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
703 char **core_siblings;
704 char **thread_siblings;
707 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
710 char filename[MAXPATHLEN];
711 char *buf = NULL, *p;
716 sprintf(filename, CORE_SIB_FMT, cpu);
717 fp = fopen(filename, "r");
721 if (getline(&buf, &len, fp) <= 0)
726 p = strchr(buf, '\n');
730 for (i = 0; i < tp->core_sib; i++) {
731 if (!strcmp(buf, tp->core_siblings[i]))
734 if (i == tp->core_sib) {
735 tp->core_siblings[i] = buf;
741 sprintf(filename, THRD_SIB_FMT, cpu);
742 fp = fopen(filename, "r");
746 if (getline(&buf, &len, fp) <= 0)
749 p = strchr(buf, '\n');
753 for (i = 0; i < tp->thread_sib; i++) {
754 if (!strcmp(buf, tp->thread_siblings[i]))
757 if (i == tp->thread_sib) {
758 tp->thread_siblings[i] = buf;
770 static void free_cpu_topo(struct cpu_topo *tp)
777 for (i = 0 ; i < tp->core_sib; i++)
778 free(tp->core_siblings[i]);
780 for (i = 0 ; i < tp->thread_sib; i++)
781 free(tp->thread_siblings[i]);
786 static struct cpu_topo *build_cpu_topology(void)
795 ncpus = sysconf(_SC_NPROCESSORS_CONF);
799 nr = (u32)(ncpus & UINT_MAX);
801 sz = nr * sizeof(char *);
803 addr = calloc(1, sizeof(*tp) + 2 * sz);
810 tp->core_siblings = addr;
812 tp->thread_siblings = addr;
814 for (i = 0; i < nr; i++) {
815 ret = build_cpu_topo(tp, i);
826 static int write_cpu_topology(int fd, struct perf_header *h __used,
827 struct perf_evlist *evlist __used)
833 tp = build_cpu_topology();
837 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
841 for (i = 0; i < tp->core_sib; i++) {
842 ret = do_write_string(fd, tp->core_siblings[i]);
846 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
850 for (i = 0; i < tp->thread_sib; i++) {
851 ret = do_write_string(fd, tp->thread_siblings[i]);
862 static int write_total_mem(int fd, struct perf_header *h __used,
863 struct perf_evlist *evlist __used)
871 fp = fopen("/proc/meminfo", "r");
875 while (getline(&buf, &len, fp) > 0) {
876 ret = strncmp(buf, "MemTotal:", 9);
881 n = sscanf(buf, "%*s %"PRIu64, &mem);
883 ret = do_write(fd, &mem, sizeof(mem));
890 static int write_topo_node(int fd, int node)
892 char str[MAXPATHLEN];
894 char *buf = NULL, *p;
897 u64 mem_total, mem_free, mem;
900 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
901 fp = fopen(str, "r");
905 while (getline(&buf, &len, fp) > 0) {
906 /* skip over invalid lines */
907 if (!strchr(buf, ':'))
909 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
911 if (!strcmp(field, "MemTotal:"))
913 if (!strcmp(field, "MemFree:"))
919 ret = do_write(fd, &mem_total, sizeof(u64));
923 ret = do_write(fd, &mem_free, sizeof(u64));
928 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
930 fp = fopen(str, "r");
934 if (getline(&buf, &len, fp) <= 0)
937 p = strchr(buf, '\n');
941 ret = do_write_string(fd, buf);
948 static int write_numa_topology(int fd, struct perf_header *h __used,
949 struct perf_evlist *evlist __used)
954 struct cpu_map *node_map = NULL;
959 fp = fopen("/sys/devices/system/node/online", "r");
963 if (getline(&buf, &len, fp) <= 0)
966 c = strchr(buf, '\n');
970 node_map = cpu_map__new(buf);
974 nr = (u32)node_map->nr;
976 ret = do_write(fd, &nr, sizeof(nr));
980 for (i = 0; i < nr; i++) {
981 j = (u32)node_map->map[i];
982 ret = do_write(fd, &j, sizeof(j));
986 ret = write_topo_node(fd, i);
998 * default get_cpuid(): nothing gets recorded
999 * actual implementation must be in arch/$(ARCH)/util/header.c
1001 int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
1006 static int write_cpuid(int fd, struct perf_header *h __used,
1007 struct perf_evlist *evlist __used)
1012 ret = get_cpuid(buffer, sizeof(buffer));
1018 return do_write_string(fd, buffer);
1021 static int write_branch_stack(int fd __used, struct perf_header *h __used,
1022 struct perf_evlist *evlist __used)
1027 static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1029 char *str = do_read_string(fd, ph);
1030 fprintf(fp, "# hostname : %s\n", str);
1034 static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1036 char *str = do_read_string(fd, ph);
1037 fprintf(fp, "# os release : %s\n", str);
1041 static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1043 char *str = do_read_string(fd, ph);
1044 fprintf(fp, "# arch : %s\n", str);
1048 static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1050 char *str = do_read_string(fd, ph);
1051 fprintf(fp, "# cpudesc : %s\n", str);
1055 static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1060 ret = read(fd, &nr, sizeof(nr));
1061 if (ret != (ssize_t)sizeof(nr))
1062 nr = -1; /* interpreted as error */
1067 fprintf(fp, "# nrcpus online : %u\n", nr);
1069 ret = read(fd, &nr, sizeof(nr));
1070 if (ret != (ssize_t)sizeof(nr))
1071 nr = -1; /* interpreted as error */
1076 fprintf(fp, "# nrcpus avail : %u\n", nr);
1079 static void print_version(struct perf_header *ph, int fd, FILE *fp)
1081 char *str = do_read_string(fd, ph);
1082 fprintf(fp, "# perf version : %s\n", str);
1086 static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1092 ret = read(fd, &nr, sizeof(nr));
1093 if (ret != (ssize_t)sizeof(nr))
1099 fprintf(fp, "# cmdline : ");
1101 for (i = 0; i < nr; i++) {
1102 str = do_read_string(fd, ph);
1103 fprintf(fp, "%s ", str);
1109 static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1115 ret = read(fd, &nr, sizeof(nr));
1116 if (ret != (ssize_t)sizeof(nr))
1122 for (i = 0; i < nr; i++) {
1123 str = do_read_string(fd, ph);
1124 fprintf(fp, "# sibling cores : %s\n", str);
1128 ret = read(fd, &nr, sizeof(nr));
1129 if (ret != (ssize_t)sizeof(nr))
1135 for (i = 0; i < nr; i++) {
1136 str = do_read_string(fd, ph);
1137 fprintf(fp, "# sibling threads : %s\n", str);
1142 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1144 struct perf_event_attr attr;
1148 u32 nre, sz, nr, i, j;
1152 /* number of events */
1153 ret = read(fd, &nre, sizeof(nre));
1154 if (ret != (ssize_t)sizeof(nre))
1158 nre = bswap_32(nre);
1160 ret = read(fd, &sz, sizeof(sz));
1161 if (ret != (ssize_t)sizeof(sz))
1167 memset(&attr, 0, sizeof(attr));
1169 /* buffer to hold on file attr struct */
1178 for (i = 0 ; i < nre; i++) {
1181 * must read entire on-file attr struct to
1182 * sync up with layout.
1184 ret = read(fd, buf, sz);
1185 if (ret != (ssize_t)sz)
1189 perf_event__attr_swap(buf);
1191 memcpy(&attr, buf, msz);
1193 ret = read(fd, &nr, sizeof(nr));
1194 if (ret != (ssize_t)sizeof(nr))
1200 str = do_read_string(fd, ph);
1201 fprintf(fp, "# event : name = %s, ", str);
1204 fprintf(fp, "type = %d, config = 0x%"PRIx64
1205 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1211 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1213 attr.exclude_kernel);
1215 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1217 attr.exclude_guest);
1219 fprintf(fp, ", precise_ip = %d", attr.precise_ip);
1222 fprintf(fp, ", id = {");
1224 for (j = 0 ; j < nr; j++) {
1225 ret = read(fd, &id, sizeof(id));
1226 if (ret != (ssize_t)sizeof(id))
1235 fprintf(fp, " %"PRIu64, id);
1244 fprintf(fp, "# event desc: not available or unable to read\n");
1247 static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1252 ret = read(fd, &mem, sizeof(mem));
1253 if (ret != sizeof(mem))
1257 mem = bswap_64(mem);
1259 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1262 fprintf(fp, "# total memory : unknown\n");
1265 static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1270 uint64_t mem_total, mem_free;
1273 ret = read(fd, &nr, sizeof(nr));
1274 if (ret != (ssize_t)sizeof(nr))
1280 for (i = 0; i < nr; i++) {
1283 ret = read(fd, &c, sizeof(c));
1284 if (ret != (ssize_t)sizeof(c))
1290 ret = read(fd, &mem_total, sizeof(u64));
1291 if (ret != sizeof(u64))
1294 ret = read(fd, &mem_free, sizeof(u64));
1295 if (ret != sizeof(u64))
1298 if (h->needs_swap) {
1299 mem_total = bswap_64(mem_total);
1300 mem_free = bswap_64(mem_free);
1303 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1304 " free = %"PRIu64" kB\n",
1309 str = do_read_string(fd, h);
1310 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1315 fprintf(fp, "# numa topology : not available\n");
1318 static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1320 char *str = do_read_string(fd, ph);
1321 fprintf(fp, "# cpuid : %s\n", str);
1325 static void print_branch_stack(struct perf_header *ph __used, int fd __used,
1328 fprintf(fp, "# contains samples with branch stack\n");
1331 static int __event_process_build_id(struct build_id_event *bev,
1333 struct perf_session *session)
1336 struct list_head *head;
1337 struct machine *machine;
1340 enum dso_kernel_type dso_type;
1342 machine = perf_session__findnew_machine(session, bev->pid);
1346 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1349 case PERF_RECORD_MISC_KERNEL:
1350 dso_type = DSO_TYPE_KERNEL;
1351 head = &machine->kernel_dsos;
1353 case PERF_RECORD_MISC_GUEST_KERNEL:
1354 dso_type = DSO_TYPE_GUEST_KERNEL;
1355 head = &machine->kernel_dsos;
1357 case PERF_RECORD_MISC_USER:
1358 case PERF_RECORD_MISC_GUEST_USER:
1359 dso_type = DSO_TYPE_USER;
1360 head = &machine->user_dsos;
1366 dso = __dsos__findnew(head, filename);
1368 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1370 dso__set_build_id(dso, &bev->build_id);
1372 if (filename[0] == '[')
1373 dso->kernel = dso_type;
1375 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1377 pr_debug("build id event received for %s: %s\n",
1378 dso->long_name, sbuild_id);
1386 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1387 int input, u64 offset, u64 size)
1389 struct perf_session *session = container_of(header, struct perf_session, header);
1391 struct perf_event_header header;
1392 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1395 struct build_id_event bev;
1396 char filename[PATH_MAX];
1397 u64 limit = offset + size;
1399 while (offset < limit) {
1402 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1405 if (header->needs_swap)
1406 perf_event_header__bswap(&old_bev.header);
1408 len = old_bev.header.size - sizeof(old_bev);
1409 if (read(input, filename, len) != len)
1412 bev.header = old_bev.header;
1415 * As the pid is the missing value, we need to fill
1416 * it properly. The header.misc value give us nice hint.
1418 bev.pid = HOST_KERNEL_ID;
1419 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1420 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1421 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1423 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1424 __event_process_build_id(&bev, filename, session);
1426 offset += bev.header.size;
1432 static int perf_header__read_build_ids(struct perf_header *header,
1433 int input, u64 offset, u64 size)
1435 struct perf_session *session = container_of(header, struct perf_session, header);
1436 struct build_id_event bev;
1437 char filename[PATH_MAX];
1438 u64 limit = offset + size, orig_offset = offset;
1441 while (offset < limit) {
1444 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1447 if (header->needs_swap)
1448 perf_event_header__bswap(&bev.header);
1450 len = bev.header.size - sizeof(bev);
1451 if (read(input, filename, len) != len)
1454 * The a1645ce1 changeset:
1456 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1458 * Added a field to struct build_id_event that broke the file
1461 * Since the kernel build-id is the first entry, process the
1462 * table using the old format if the well known
1463 * '[kernel.kallsyms]' string for the kernel build-id has the
1464 * first 4 characters chopped off (where the pid_t sits).
1466 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1467 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1469 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1472 __event_process_build_id(&bev, filename, session);
1474 offset += bev.header.size;
1481 static int process_tracing_data(struct perf_file_section *section __unused,
1482 struct perf_header *ph __unused,
1483 int feat __unused, int fd, void *data)
1485 trace_report(fd, data, false);
1489 static int process_build_id(struct perf_file_section *section,
1490 struct perf_header *ph,
1491 int feat __unused, int fd, void *data __used)
1493 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1494 pr_debug("Failed to read buildids, continuing...\n");
1498 struct feature_ops {
1499 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1500 void (*print)(struct perf_header *h, int fd, FILE *fp);
1501 int (*process)(struct perf_file_section *section,
1502 struct perf_header *h, int feat, int fd, void *data);
1507 #define FEAT_OPA(n, func) \
1508 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1509 #define FEAT_OPP(n, func) \
1510 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1511 .process = process_##func }
1512 #define FEAT_OPF(n, func) \
1513 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1516 /* feature_ops not implemented: */
1517 #define print_tracing_data NULL
1518 #define print_build_id NULL
1520 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1521 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
1522 FEAT_OPP(HEADER_BUILD_ID, build_id),
1523 FEAT_OPA(HEADER_HOSTNAME, hostname),
1524 FEAT_OPA(HEADER_OSRELEASE, osrelease),
1525 FEAT_OPA(HEADER_VERSION, version),
1526 FEAT_OPA(HEADER_ARCH, arch),
1527 FEAT_OPA(HEADER_NRCPUS, nrcpus),
1528 FEAT_OPA(HEADER_CPUDESC, cpudesc),
1529 FEAT_OPA(HEADER_CPUID, cpuid),
1530 FEAT_OPA(HEADER_TOTAL_MEM, total_mem),
1531 FEAT_OPA(HEADER_EVENT_DESC, event_desc),
1532 FEAT_OPA(HEADER_CMDLINE, cmdline),
1533 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1534 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1535 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
1538 struct header_print_data {
1540 bool full; /* extended list of headers */
1543 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1544 struct perf_header *ph,
1545 int feat, int fd, void *data)
1547 struct header_print_data *hd = data;
1549 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1550 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1551 "%d, continuing...\n", section->offset, feat);
1554 if (feat >= HEADER_LAST_FEATURE) {
1555 pr_warning("unknown feature %d\n", feat);
1558 if (!feat_ops[feat].print)
1561 if (!feat_ops[feat].full_only || hd->full)
1562 feat_ops[feat].print(ph, fd, hd->fp);
1564 fprintf(hd->fp, "# %s info available, use -I to display\n",
1565 feat_ops[feat].name);
1570 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1572 struct header_print_data hd;
1573 struct perf_header *header = &session->header;
1574 int fd = session->fd;
1578 perf_header__process_sections(header, fd, &hd,
1579 perf_file_section__fprintf_info);
1583 static int do_write_feat(int fd, struct perf_header *h, int type,
1584 struct perf_file_section **p,
1585 struct perf_evlist *evlist)
1590 if (perf_header__has_feat(h, type)) {
1591 if (!feat_ops[type].write)
1594 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1596 err = feat_ops[type].write(fd, h, evlist);
1598 pr_debug("failed to write feature %d\n", type);
1600 /* undo anything written */
1601 lseek(fd, (*p)->offset, SEEK_SET);
1605 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1611 static int perf_header__adds_write(struct perf_header *header,
1612 struct perf_evlist *evlist, int fd)
1615 struct perf_file_section *feat_sec, *p;
1621 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1625 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
1626 if (feat_sec == NULL)
1629 sec_size = sizeof(*feat_sec) * nr_sections;
1631 sec_start = header->data_offset + header->data_size;
1632 lseek(fd, sec_start + sec_size, SEEK_SET);
1634 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1635 if (do_write_feat(fd, header, feat, &p, evlist))
1636 perf_header__clear_feat(header, feat);
1639 lseek(fd, sec_start, SEEK_SET);
1641 * may write more than needed due to dropped feature, but
1642 * this is okay, reader will skip the mising entries
1644 err = do_write(fd, feat_sec, sec_size);
1646 pr_debug("failed to write feature section\n");
1651 int perf_header__write_pipe(int fd)
1653 struct perf_pipe_file_header f_header;
1656 f_header = (struct perf_pipe_file_header){
1657 .magic = PERF_MAGIC,
1658 .size = sizeof(f_header),
1661 err = do_write(fd, &f_header, sizeof(f_header));
1663 pr_debug("failed to write perf pipe header\n");
1670 int perf_session__write_header(struct perf_session *session,
1671 struct perf_evlist *evlist,
1672 int fd, bool at_exit)
1674 struct perf_file_header f_header;
1675 struct perf_file_attr f_attr;
1676 struct perf_header *header = &session->header;
1677 struct perf_evsel *attr, *pair = NULL;
1680 lseek(fd, sizeof(f_header), SEEK_SET);
1682 if (session->evlist != evlist)
1683 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node);
1685 list_for_each_entry(attr, &evlist->entries, node) {
1686 attr->id_offset = lseek(fd, 0, SEEK_CUR);
1687 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
1690 pr_debug("failed to write perf header\n");
1693 if (session->evlist != evlist) {
1694 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1697 attr->ids += pair->ids;
1698 pair = list_entry(pair->node.next, struct perf_evsel, node);
1702 header->attr_offset = lseek(fd, 0, SEEK_CUR);
1704 list_for_each_entry(attr, &evlist->entries, node) {
1705 f_attr = (struct perf_file_attr){
1708 .offset = attr->id_offset,
1709 .size = attr->ids * sizeof(u64),
1712 err = do_write(fd, &f_attr, sizeof(f_attr));
1714 pr_debug("failed to write perf header attribute\n");
1719 header->event_offset = lseek(fd, 0, SEEK_CUR);
1720 header->event_size = event_count * sizeof(struct perf_trace_event_type);
1722 err = do_write(fd, events, header->event_size);
1724 pr_debug("failed to write perf header events\n");
1729 header->data_offset = lseek(fd, 0, SEEK_CUR);
1732 err = perf_header__adds_write(header, evlist, fd);
1737 f_header = (struct perf_file_header){
1738 .magic = PERF_MAGIC,
1739 .size = sizeof(f_header),
1740 .attr_size = sizeof(f_attr),
1742 .offset = header->attr_offset,
1743 .size = evlist->nr_entries * sizeof(f_attr),
1746 .offset = header->data_offset,
1747 .size = header->data_size,
1750 .offset = header->event_offset,
1751 .size = header->event_size,
1755 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
1757 lseek(fd, 0, SEEK_SET);
1758 err = do_write(fd, &f_header, sizeof(f_header));
1760 pr_debug("failed to write perf header\n");
1763 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1769 static int perf_header__getbuffer64(struct perf_header *header,
1770 int fd, void *buf, size_t size)
1772 if (readn(fd, buf, size) <= 0)
1775 if (header->needs_swap)
1776 mem_bswap_64(buf, size);
1781 int perf_header__process_sections(struct perf_header *header, int fd,
1783 int (*process)(struct perf_file_section *section,
1784 struct perf_header *ph,
1785 int feat, int fd, void *data))
1787 struct perf_file_section *feat_sec, *sec;
1793 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1797 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
1801 sec_size = sizeof(*feat_sec) * nr_sections;
1803 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1805 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
1809 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
1810 err = process(sec++, header, feat, fd, data);
1820 static const int attr_file_abi_sizes[] = {
1821 [0] = PERF_ATTR_SIZE_VER0,
1822 [1] = PERF_ATTR_SIZE_VER1,
1827 * In the legacy file format, the magic number is not used to encode endianness.
1828 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
1829 * on ABI revisions, we need to try all combinations for all endianness to
1830 * detect the endianness.
1832 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
1834 uint64_t ref_size, attr_size;
1837 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
1838 ref_size = attr_file_abi_sizes[i]
1839 + sizeof(struct perf_file_section);
1840 if (hdr_sz != ref_size) {
1841 attr_size = bswap_64(hdr_sz);
1842 if (attr_size != ref_size)
1845 ph->needs_swap = true;
1847 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
1852 /* could not determine endianness */
1856 #define PERF_PIPE_HDR_VER0 16
1858 static const size_t attr_pipe_abi_sizes[] = {
1859 [0] = PERF_PIPE_HDR_VER0,
1864 * In the legacy pipe format, there is an implicit assumption that endiannesss
1865 * between host recording the samples, and host parsing the samples is the
1866 * same. This is not always the case given that the pipe output may always be
1867 * redirected into a file and analyzed on a different machine with possibly a
1868 * different endianness and perf_event ABI revsions in the perf tool itself.
1870 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
1875 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
1876 if (hdr_sz != attr_pipe_abi_sizes[i]) {
1877 attr_size = bswap_64(hdr_sz);
1878 if (attr_size != hdr_sz)
1881 ph->needs_swap = true;
1883 pr_debug("Pipe ABI%d perf.data file detected\n", i);
1889 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
1890 bool is_pipe, struct perf_header *ph)
1894 /* check for legacy format */
1895 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
1897 pr_debug("legacy perf.data format\n");
1899 return try_all_pipe_abis(hdr_sz, ph);
1901 return try_all_file_abis(hdr_sz, ph);
1904 * the new magic number serves two purposes:
1905 * - unique number to identify actual perf.data files
1906 * - encode endianness of file
1909 /* check magic number with one endianness */
1910 if (magic == __perf_magic2)
1913 /* check magic number with opposite endianness */
1914 if (magic != __perf_magic2_sw)
1917 ph->needs_swap = true;
1922 int perf_file_header__read(struct perf_file_header *header,
1923 struct perf_header *ph, int fd)
1927 lseek(fd, 0, SEEK_SET);
1929 ret = readn(fd, header, sizeof(*header));
1933 if (check_magic_endian(header->magic,
1934 header->attr_size, false, ph) < 0) {
1935 pr_debug("magic/endian check failed\n");
1939 if (ph->needs_swap) {
1940 mem_bswap_64(header, offsetof(struct perf_file_header,
1944 if (header->size != sizeof(*header)) {
1945 /* Support the previous format */
1946 if (header->size == offsetof(typeof(*header), adds_features))
1947 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1950 } else if (ph->needs_swap) {
1952 * feature bitmap is declared as an array of unsigned longs --
1953 * not good since its size can differ between the host that
1954 * generated the data file and the host analyzing the file.
1956 * We need to handle endianness, but we don't know the size of
1957 * the unsigned long where the file was generated. Take a best
1958 * guess at determining it: try 64-bit swap first (ie., file
1959 * created on a 64-bit host), and check if the hostname feature
1960 * bit is set (this feature bit is forced on as of fbe96f2).
1961 * If the bit is not, undo the 64-bit swap and try a 32-bit
1962 * swap. If the hostname bit is still not set (e.g., older data
1963 * file), punt and fallback to the original behavior --
1964 * clearing all feature bits and setting buildid.
1966 mem_bswap_64(&header->adds_features,
1967 BITS_TO_U64(HEADER_FEAT_BITS));
1969 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1971 mem_bswap_64(&header->adds_features,
1972 BITS_TO_U64(HEADER_FEAT_BITS));
1975 mem_bswap_32(&header->adds_features,
1976 BITS_TO_U32(HEADER_FEAT_BITS));
1979 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1980 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1981 set_bit(HEADER_BUILD_ID, header->adds_features);
1985 memcpy(&ph->adds_features, &header->adds_features,
1986 sizeof(ph->adds_features));
1988 ph->event_offset = header->event_types.offset;
1989 ph->event_size = header->event_types.size;
1990 ph->data_offset = header->data.offset;
1991 ph->data_size = header->data.size;
1995 static int perf_file_section__process(struct perf_file_section *section,
1996 struct perf_header *ph,
1997 int feat, int fd, void *data)
1999 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2000 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2001 "%d, continuing...\n", section->offset, feat);
2005 if (feat >= HEADER_LAST_FEATURE) {
2006 pr_debug("unknown feature %d, continuing...\n", feat);
2010 if (!feat_ops[feat].process)
2013 return feat_ops[feat].process(section, ph, feat, fd, data);
2016 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2017 struct perf_header *ph, int fd,
2022 ret = readn(fd, header, sizeof(*header));
2026 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2027 pr_debug("endian/magic failed\n");
2032 header->size = bswap_64(header->size);
2034 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2040 static int perf_header__read_pipe(struct perf_session *session, int fd)
2042 struct perf_header *header = &session->header;
2043 struct perf_pipe_file_header f_header;
2045 if (perf_file_header__read_pipe(&f_header, header, fd,
2046 session->repipe) < 0) {
2047 pr_debug("incompatible file format\n");
2056 static int read_attr(int fd, struct perf_header *ph,
2057 struct perf_file_attr *f_attr)
2059 struct perf_event_attr *attr = &f_attr->attr;
2061 size_t our_sz = sizeof(f_attr->attr);
2064 memset(f_attr, 0, sizeof(*f_attr));
2066 /* read minimal guaranteed structure */
2067 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2069 pr_debug("cannot read %d bytes of header attr\n",
2070 PERF_ATTR_SIZE_VER0);
2074 /* on file perf_event_attr size */
2082 sz = PERF_ATTR_SIZE_VER0;
2083 } else if (sz > our_sz) {
2084 pr_debug("file uses a more recent and unsupported ABI"
2085 " (%zu bytes extra)\n", sz - our_sz);
2088 /* what we have not yet read and that we know about */
2089 left = sz - PERF_ATTR_SIZE_VER0;
2092 ptr += PERF_ATTR_SIZE_VER0;
2094 ret = readn(fd, ptr, left);
2096 /* read perf_file_section, ids are read in caller */
2097 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2099 return ret <= 0 ? -1 : 0;
2102 static int perf_evsel__set_tracepoint_name(struct perf_evsel *evsel,
2103 struct pevent *pevent)
2105 struct event_format *event = pevent_find_event(pevent,
2106 evsel->attr.config);
2112 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2113 evsel->name = strdup(bf);
2114 if (event->name == NULL)
2120 static int perf_evlist__set_tracepoint_names(struct perf_evlist *evlist,
2121 struct pevent *pevent)
2123 struct perf_evsel *pos;
2125 list_for_each_entry(pos, &evlist->entries, node) {
2126 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2127 perf_evsel__set_tracepoint_name(pos, pevent))
2134 int perf_session__read_header(struct perf_session *session, int fd)
2136 struct perf_header *header = &session->header;
2137 struct perf_file_header f_header;
2138 struct perf_file_attr f_attr;
2140 int nr_attrs, nr_ids, i, j;
2142 session->evlist = perf_evlist__new(NULL, NULL);
2143 if (session->evlist == NULL)
2146 if (session->fd_pipe)
2147 return perf_header__read_pipe(session, fd);
2149 if (perf_file_header__read(&f_header, header, fd) < 0)
2152 nr_attrs = f_header.attrs.size / f_header.attr_size;
2153 lseek(fd, f_header.attrs.offset, SEEK_SET);
2155 for (i = 0; i < nr_attrs; i++) {
2156 struct perf_evsel *evsel;
2159 if (read_attr(fd, header, &f_attr) < 0)
2162 if (header->needs_swap)
2163 perf_event__attr_swap(&f_attr.attr);
2165 tmp = lseek(fd, 0, SEEK_CUR);
2166 evsel = perf_evsel__new(&f_attr.attr, i);
2169 goto out_delete_evlist;
2171 * Do it before so that if perf_evsel__alloc_id fails, this
2172 * entry gets purged too at perf_evlist__delete().
2174 perf_evlist__add(session->evlist, evsel);
2176 nr_ids = f_attr.ids.size / sizeof(u64);
2178 * We don't have the cpu and thread maps on the header, so
2179 * for allocating the perf_sample_id table we fake 1 cpu and
2180 * hattr->ids threads.
2182 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2183 goto out_delete_evlist;
2185 lseek(fd, f_attr.ids.offset, SEEK_SET);
2187 for (j = 0; j < nr_ids; j++) {
2188 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2191 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2194 lseek(fd, tmp, SEEK_SET);
2197 symbol_conf.nr_events = nr_attrs;
2199 if (f_header.event_types.size) {
2200 lseek(fd, f_header.event_types.offset, SEEK_SET);
2201 events = malloc(f_header.event_types.size);
2204 if (perf_header__getbuffer64(header, fd, events,
2205 f_header.event_types.size))
2207 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2210 perf_header__process_sections(header, fd, &session->pevent,
2211 perf_file_section__process);
2213 lseek(fd, header->data_offset, SEEK_SET);
2215 if (perf_evlist__set_tracepoint_names(session->evlist, session->pevent))
2216 goto out_delete_evlist;
2224 perf_evlist__delete(session->evlist);
2225 session->evlist = NULL;
2229 int perf_event__synthesize_attr(struct perf_tool *tool,
2230 struct perf_event_attr *attr, u16 ids, u64 *id,
2231 perf_event__handler_t process)
2233 union perf_event *ev;
2237 size = sizeof(struct perf_event_attr);
2238 size = ALIGN(size, sizeof(u64));
2239 size += sizeof(struct perf_event_header);
2240 size += ids * sizeof(u64);
2247 ev->attr.attr = *attr;
2248 memcpy(ev->attr.id, id, ids * sizeof(u64));
2250 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2251 ev->attr.header.size = size;
2253 err = process(tool, ev, NULL, NULL);
2260 int perf_event__synthesize_attrs(struct perf_tool *tool,
2261 struct perf_session *session,
2262 perf_event__handler_t process)
2264 struct perf_evsel *attr;
2267 list_for_each_entry(attr, &session->evlist->entries, node) {
2268 err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids,
2271 pr_debug("failed to create perf header attribute\n");
2279 int perf_event__process_attr(union perf_event *event,
2280 struct perf_evlist **pevlist)
2282 unsigned int i, ids, n_ids;
2283 struct perf_evsel *evsel;
2284 struct perf_evlist *evlist = *pevlist;
2286 if (evlist == NULL) {
2287 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2292 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2296 perf_evlist__add(evlist, evsel);
2298 ids = event->header.size;
2299 ids -= (void *)&event->attr.id - (void *)event;
2300 n_ids = ids / sizeof(u64);
2302 * We don't have the cpu and thread maps on the header, so
2303 * for allocating the perf_sample_id table we fake 1 cpu and
2304 * hattr->ids threads.
2306 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2309 for (i = 0; i < n_ids; i++) {
2310 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2316 int perf_event__synthesize_event_type(struct perf_tool *tool,
2317 u64 event_id, char *name,
2318 perf_event__handler_t process,
2319 struct machine *machine)
2321 union perf_event ev;
2325 memset(&ev, 0, sizeof(ev));
2327 ev.event_type.event_type.event_id = event_id;
2328 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2329 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2331 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2332 size = strlen(ev.event_type.event_type.name);
2333 size = ALIGN(size, sizeof(u64));
2334 ev.event_type.header.size = sizeof(ev.event_type) -
2335 (sizeof(ev.event_type.event_type.name) - size);
2337 err = process(tool, &ev, NULL, machine);
2342 int perf_event__synthesize_event_types(struct perf_tool *tool,
2343 perf_event__handler_t process,
2344 struct machine *machine)
2346 struct perf_trace_event_type *type;
2349 for (i = 0; i < event_count; i++) {
2352 err = perf_event__synthesize_event_type(tool, type->event_id,
2353 type->name, process,
2356 pr_debug("failed to create perf header event type\n");
2364 int perf_event__process_event_type(struct perf_tool *tool __unused,
2365 union perf_event *event)
2367 if (perf_header__push_event(event->event_type.event_type.event_id,
2368 event->event_type.event_type.name) < 0)
2374 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2375 struct perf_evlist *evlist,
2376 perf_event__handler_t process)
2378 union perf_event ev;
2379 struct tracing_data *tdata;
2380 ssize_t size = 0, aligned_size = 0, padding;
2384 * We are going to store the size of the data followed
2385 * by the data contents. Since the fd descriptor is a pipe,
2386 * we cannot seek back to store the size of the data once
2387 * we know it. Instead we:
2389 * - write the tracing data to the temp file
2390 * - get/write the data size to pipe
2391 * - write the tracing data from the temp file
2394 tdata = tracing_data_get(&evlist->entries, fd, true);
2398 memset(&ev, 0, sizeof(ev));
2400 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2402 aligned_size = ALIGN(size, sizeof(u64));
2403 padding = aligned_size - size;
2404 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2405 ev.tracing_data.size = aligned_size;
2407 process(tool, &ev, NULL, NULL);
2410 * The put function will copy all the tracing data
2411 * stored in temp file to the pipe.
2413 tracing_data_put(tdata);
2415 write_padded(fd, NULL, 0, padding);
2417 return aligned_size;
2420 int perf_event__process_tracing_data(union perf_event *event,
2421 struct perf_session *session)
2423 ssize_t size_read, padding, size = event->tracing_data.size;
2424 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2427 /* setup for reading amidst mmap */
2428 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2431 size_read = trace_report(session->fd, &session->pevent,
2433 padding = ALIGN(size_read, sizeof(u64)) - size_read;
2435 if (read(session->fd, buf, padding) < 0)
2436 die("reading input file");
2437 if (session->repipe) {
2438 int retw = write(STDOUT_FILENO, buf, padding);
2439 if (retw <= 0 || retw != padding)
2440 die("repiping tracing data padding");
2443 if (size_read + padding != size)
2444 die("tracing data size mismatch");
2446 return size_read + padding;
2449 int perf_event__synthesize_build_id(struct perf_tool *tool,
2450 struct dso *pos, u16 misc,
2451 perf_event__handler_t process,
2452 struct machine *machine)
2454 union perf_event ev;
2461 memset(&ev, 0, sizeof(ev));
2463 len = pos->long_name_len + 1;
2464 len = ALIGN(len, NAME_ALIGN);
2465 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2466 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2467 ev.build_id.header.misc = misc;
2468 ev.build_id.pid = machine->pid;
2469 ev.build_id.header.size = sizeof(ev.build_id) + len;
2470 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2472 err = process(tool, &ev, NULL, machine);
2477 int perf_event__process_build_id(struct perf_tool *tool __used,
2478 union perf_event *event,
2479 struct perf_session *session)
2481 __event_process_build_id(&event->build_id,
2482 event->build_id.filename,
2487 void disable_buildid_cache(void)
2489 no_buildid_cache = true;