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 if (strlen(name) > MAX_EVENT_NAME)
35 pr_warning("Event %s will be truncated\n", name);
38 events = malloc(sizeof(struct perf_trace_event_type));
42 struct perf_trace_event_type *nevents;
44 nevents = realloc(events, (event_count + 1) * sizeof(*events));
49 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
50 events[event_count].event_id = id;
51 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
56 char *perf_header__find_event(u64 id)
59 for (i = 0 ; i < event_count; i++) {
60 if (events[i].event_id == id)
61 return events[i].name;
68 * must be a numerical value to let the endianness
69 * determine the memory layout. That way we are able
70 * to detect endianness when reading the perf.data file
73 * we check for legacy (PERFFILE) format.
75 static const char *__perf_magic1 = "PERFFILE";
76 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
77 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
79 #define PERF_MAGIC __perf_magic2
81 struct perf_file_attr {
82 struct perf_event_attr attr;
83 struct perf_file_section ids;
86 void perf_header__set_feat(struct perf_header *header, int feat)
88 set_bit(feat, header->adds_features);
91 void perf_header__clear_feat(struct perf_header *header, int feat)
93 clear_bit(feat, header->adds_features);
96 bool perf_header__has_feat(const struct perf_header *header, int feat)
98 return test_bit(feat, header->adds_features);
101 static int do_write(int fd, const void *buf, size_t size)
104 int ret = write(fd, buf, size);
116 #define NAME_ALIGN 64
118 static int write_padded(int fd, const void *bf, size_t count,
119 size_t count_aligned)
121 static const char zero_buf[NAME_ALIGN];
122 int err = do_write(fd, bf, count);
125 err = do_write(fd, zero_buf, count_aligned - count);
130 static int do_write_string(int fd, const char *str)
135 olen = strlen(str) + 1;
136 len = ALIGN(olen, NAME_ALIGN);
138 /* write len, incl. \0 */
139 ret = do_write(fd, &len, sizeof(len));
143 return write_padded(fd, str, olen, len);
146 static char *do_read_string(int fd, struct perf_header *ph)
152 sz = read(fd, &len, sizeof(len));
153 if (sz < (ssize_t)sizeof(len))
163 ret = read(fd, buf, len);
164 if (ret == (ssize_t)len) {
166 * strings are padded by zeroes
167 * thus the actual strlen of buf
168 * may be less than len
178 perf_header__set_cmdline(int argc, const char **argv)
182 header_argc = (u32)argc;
184 /* do not include NULL termination */
185 header_argv = calloc(argc, sizeof(char *));
190 * must copy argv contents because it gets moved
191 * around during option parsing
193 for (i = 0; i < argc ; i++)
194 header_argv[i] = argv[i];
199 #define dsos__for_each_with_build_id(pos, head) \
200 list_for_each_entry(pos, head, node) \
201 if (!pos->has_build_id) \
205 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
210 dsos__for_each_with_build_id(pos, head) {
212 struct build_id_event b;
217 len = pos->long_name_len + 1;
218 len = ALIGN(len, NAME_ALIGN);
219 memset(&b, 0, sizeof(b));
220 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
222 b.header.misc = misc;
223 b.header.size = sizeof(b) + len;
224 err = do_write(fd, &b, sizeof(b));
227 err = write_padded(fd, pos->long_name,
228 pos->long_name_len + 1, len);
236 static int machine__write_buildid_table(struct machine *machine, int fd)
239 u16 kmisc = PERF_RECORD_MISC_KERNEL,
240 umisc = PERF_RECORD_MISC_USER;
242 if (!machine__is_host(machine)) {
243 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
244 umisc = PERF_RECORD_MISC_GUEST_USER;
247 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
250 err = __dsos__write_buildid_table(&machine->user_dsos,
251 machine->pid, umisc, fd);
255 static int dsos__write_buildid_table(struct perf_header *header, int fd)
257 struct perf_session *session = container_of(header,
258 struct perf_session, header);
260 int err = machine__write_buildid_table(&session->host_machine, fd);
265 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
266 struct machine *pos = rb_entry(nd, struct machine, rb_node);
267 err = machine__write_buildid_table(pos, fd);
274 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
275 const char *name, bool is_kallsyms)
277 const size_t size = PATH_MAX;
278 char *realname, *filename = zalloc(size),
279 *linkname = zalloc(size), *targetname;
283 if (symbol_conf.kptr_restrict) {
284 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
287 realname = (char *)name;
289 realname = realpath(name, NULL);
291 if (realname == NULL || filename == NULL || linkname == NULL)
294 len = snprintf(filename, size, "%s%s%s",
295 debugdir, is_kallsyms ? "/" : "", realname);
296 if (mkdir_p(filename, 0755))
299 snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
301 if (access(filename, F_OK)) {
303 if (copyfile("/proc/kallsyms", filename))
305 } else if (link(realname, filename) && copyfile(name, filename))
309 len = snprintf(linkname, size, "%s/.build-id/%.2s",
310 debugdir, sbuild_id);
312 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
315 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
316 targetname = filename + strlen(debugdir) - 5;
317 memcpy(targetname, "../..", 5);
319 if (symlink(targetname, linkname) == 0)
329 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
330 const char *name, const char *debugdir,
333 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
335 build_id__sprintf(build_id, build_id_size, sbuild_id);
337 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
340 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
342 const size_t size = PATH_MAX;
343 char *filename = zalloc(size),
344 *linkname = zalloc(size);
347 if (filename == NULL || linkname == NULL)
350 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
351 debugdir, sbuild_id, sbuild_id + 2);
353 if (access(linkname, F_OK))
356 if (readlink(linkname, filename, size - 1) < 0)
359 if (unlink(linkname))
363 * Since the link is relative, we must make it absolute:
365 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
366 debugdir, sbuild_id, filename);
368 if (unlink(linkname))
378 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
380 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
382 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
383 dso->long_name, debugdir, is_kallsyms);
386 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
391 dsos__for_each_with_build_id(pos, head)
392 if (dso__cache_build_id(pos, debugdir))
398 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
400 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
401 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
405 static int perf_session__cache_build_ids(struct perf_session *session)
409 char debugdir[PATH_MAX];
411 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
413 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
416 ret = machine__cache_build_ids(&session->host_machine, debugdir);
418 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
419 struct machine *pos = rb_entry(nd, struct machine, rb_node);
420 ret |= machine__cache_build_ids(pos, debugdir);
425 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
427 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
428 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
432 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
435 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
437 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
438 struct machine *pos = rb_entry(nd, struct machine, rb_node);
439 ret |= machine__read_build_ids(pos, with_hits);
445 static int write_trace_info(int fd, struct perf_header *h __used,
446 struct perf_evlist *evlist)
448 return read_tracing_data(fd, &evlist->entries);
452 static int write_build_id(int fd, struct perf_header *h,
453 struct perf_evlist *evlist __used)
455 struct perf_session *session;
458 session = container_of(h, struct perf_session, header);
460 if (!perf_session__read_build_ids(session, true))
463 err = dsos__write_buildid_table(h, fd);
465 pr_debug("failed to write buildid table\n");
468 if (!no_buildid_cache)
469 perf_session__cache_build_ids(session);
474 static int write_hostname(int fd, struct perf_header *h __used,
475 struct perf_evlist *evlist __used)
484 return do_write_string(fd, uts.nodename);
487 static int write_osrelease(int fd, struct perf_header *h __used,
488 struct perf_evlist *evlist __used)
497 return do_write_string(fd, uts.release);
500 static int write_arch(int fd, struct perf_header *h __used,
501 struct perf_evlist *evlist __used)
510 return do_write_string(fd, uts.machine);
513 static int write_version(int fd, struct perf_header *h __used,
514 struct perf_evlist *evlist __used)
516 return do_write_string(fd, perf_version_string);
519 static int write_cpudesc(int fd, struct perf_header *h __used,
520 struct perf_evlist *evlist __used)
523 #define CPUINFO_PROC NULL
528 const char *search = CPUINFO_PROC;
535 file = fopen("/proc/cpuinfo", "r");
539 while (getline(&buf, &len, file) > 0) {
540 ret = strncmp(buf, search, strlen(search));
550 p = strchr(buf, ':');
551 if (p && *(p+1) == ' ' && *(p+2))
557 /* squash extra space characters (branding string) */
564 while (*q && isspace(*q))
567 while ((*r++ = *q++));
571 ret = do_write_string(fd, s);
578 static int write_nrcpus(int fd, struct perf_header *h __used,
579 struct perf_evlist *evlist __used)
585 nr = sysconf(_SC_NPROCESSORS_CONF);
589 nrc = (u32)(nr & UINT_MAX);
591 nr = sysconf(_SC_NPROCESSORS_ONLN);
595 nra = (u32)(nr & UINT_MAX);
597 ret = do_write(fd, &nrc, sizeof(nrc));
601 return do_write(fd, &nra, sizeof(nra));
604 static int write_event_desc(int fd, struct perf_header *h __used,
605 struct perf_evlist *evlist)
607 struct perf_evsel *attr;
608 u32 nre = 0, nri, sz;
611 list_for_each_entry(attr, &evlist->entries, node)
615 * write number of events
617 ret = do_write(fd, &nre, sizeof(nre));
622 * size of perf_event_attr struct
624 sz = (u32)sizeof(attr->attr);
625 ret = do_write(fd, &sz, sizeof(sz));
629 list_for_each_entry(attr, &evlist->entries, node) {
631 ret = do_write(fd, &attr->attr, sz);
635 * write number of unique id per event
636 * there is one id per instance of an event
638 * copy into an nri to be independent of the
642 ret = do_write(fd, &nri, sizeof(nri));
647 * write event string as passed on cmdline
649 ret = do_write_string(fd, event_name(attr));
653 * write unique ids for this event
655 ret = do_write(fd, attr->id, attr->ids * sizeof(u64));
662 static int write_cmdline(int fd, struct perf_header *h __used,
663 struct perf_evlist *evlist __used)
665 char buf[MAXPATHLEN];
671 * actual atual path to perf binary
673 sprintf(proc, "/proc/%d/exe", getpid());
674 ret = readlink(proc, buf, sizeof(buf));
678 /* readlink() does not add null termination */
681 /* account for binary path */
684 ret = do_write(fd, &n, sizeof(n));
688 ret = do_write_string(fd, buf);
692 for (i = 0 ; i < header_argc; i++) {
693 ret = do_write_string(fd, header_argv[i]);
700 #define CORE_SIB_FMT \
701 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
702 #define THRD_SIB_FMT \
703 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
708 char **core_siblings;
709 char **thread_siblings;
712 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
715 char filename[MAXPATHLEN];
716 char *buf = NULL, *p;
721 sprintf(filename, CORE_SIB_FMT, cpu);
722 fp = fopen(filename, "r");
726 if (getline(&buf, &len, fp) <= 0)
731 p = strchr(buf, '\n');
735 for (i = 0; i < tp->core_sib; i++) {
736 if (!strcmp(buf, tp->core_siblings[i]))
739 if (i == tp->core_sib) {
740 tp->core_siblings[i] = buf;
746 sprintf(filename, THRD_SIB_FMT, cpu);
747 fp = fopen(filename, "r");
751 if (getline(&buf, &len, fp) <= 0)
754 p = strchr(buf, '\n');
758 for (i = 0; i < tp->thread_sib; i++) {
759 if (!strcmp(buf, tp->thread_siblings[i]))
762 if (i == tp->thread_sib) {
763 tp->thread_siblings[i] = buf;
775 static void free_cpu_topo(struct cpu_topo *tp)
782 for (i = 0 ; i < tp->core_sib; i++)
783 free(tp->core_siblings[i]);
785 for (i = 0 ; i < tp->thread_sib; i++)
786 free(tp->thread_siblings[i]);
791 static struct cpu_topo *build_cpu_topology(void)
800 ncpus = sysconf(_SC_NPROCESSORS_CONF);
804 nr = (u32)(ncpus & UINT_MAX);
806 sz = nr * sizeof(char *);
808 addr = calloc(1, sizeof(*tp) + 2 * sz);
815 tp->core_siblings = addr;
817 tp->thread_siblings = addr;
819 for (i = 0; i < nr; i++) {
820 ret = build_cpu_topo(tp, i);
831 static int write_cpu_topology(int fd, struct perf_header *h __used,
832 struct perf_evlist *evlist __used)
838 tp = build_cpu_topology();
842 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
846 for (i = 0; i < tp->core_sib; i++) {
847 ret = do_write_string(fd, tp->core_siblings[i]);
851 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
855 for (i = 0; i < tp->thread_sib; i++) {
856 ret = do_write_string(fd, tp->thread_siblings[i]);
867 static int write_total_mem(int fd, struct perf_header *h __used,
868 struct perf_evlist *evlist __used)
876 fp = fopen("/proc/meminfo", "r");
880 while (getline(&buf, &len, fp) > 0) {
881 ret = strncmp(buf, "MemTotal:", 9);
886 n = sscanf(buf, "%*s %"PRIu64, &mem);
888 ret = do_write(fd, &mem, sizeof(mem));
895 static int write_topo_node(int fd, int node)
897 char str[MAXPATHLEN];
899 char *buf = NULL, *p;
902 u64 mem_total, mem_free, mem;
905 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
906 fp = fopen(str, "r");
910 while (getline(&buf, &len, fp) > 0) {
911 /* skip over invalid lines */
912 if (!strchr(buf, ':'))
914 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
916 if (!strcmp(field, "MemTotal:"))
918 if (!strcmp(field, "MemFree:"))
924 ret = do_write(fd, &mem_total, sizeof(u64));
928 ret = do_write(fd, &mem_free, sizeof(u64));
933 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
935 fp = fopen(str, "r");
939 if (getline(&buf, &len, fp) <= 0)
942 p = strchr(buf, '\n');
946 ret = do_write_string(fd, buf);
953 static int write_numa_topology(int fd, struct perf_header *h __used,
954 struct perf_evlist *evlist __used)
959 struct cpu_map *node_map = NULL;
964 fp = fopen("/sys/devices/system/node/online", "r");
968 if (getline(&buf, &len, fp) <= 0)
971 c = strchr(buf, '\n');
975 node_map = cpu_map__new(buf);
979 nr = (u32)node_map->nr;
981 ret = do_write(fd, &nr, sizeof(nr));
985 for (i = 0; i < nr; i++) {
986 j = (u32)node_map->map[i];
987 ret = do_write(fd, &j, sizeof(j));
991 ret = write_topo_node(fd, i);
1003 * default get_cpuid(): nothing gets recorded
1004 * actual implementation must be in arch/$(ARCH)/util/header.c
1006 int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
1011 static int write_cpuid(int fd, struct perf_header *h __used,
1012 struct perf_evlist *evlist __used)
1017 ret = get_cpuid(buffer, sizeof(buffer));
1023 return do_write_string(fd, buffer);
1026 static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1028 char *str = do_read_string(fd, ph);
1029 fprintf(fp, "# hostname : %s\n", str);
1033 static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1035 char *str = do_read_string(fd, ph);
1036 fprintf(fp, "# os release : %s\n", str);
1040 static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1042 char *str = do_read_string(fd, ph);
1043 fprintf(fp, "# arch : %s\n", str);
1047 static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1049 char *str = do_read_string(fd, ph);
1050 fprintf(fp, "# cpudesc : %s\n", str);
1054 static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1059 ret = read(fd, &nr, sizeof(nr));
1060 if (ret != (ssize_t)sizeof(nr))
1061 nr = -1; /* interpreted as error */
1066 fprintf(fp, "# nrcpus online : %u\n", nr);
1068 ret = read(fd, &nr, sizeof(nr));
1069 if (ret != (ssize_t)sizeof(nr))
1070 nr = -1; /* interpreted as error */
1075 fprintf(fp, "# nrcpus avail : %u\n", nr);
1078 static void print_version(struct perf_header *ph, int fd, FILE *fp)
1080 char *str = do_read_string(fd, ph);
1081 fprintf(fp, "# perf version : %s\n", str);
1085 static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1091 ret = read(fd, &nr, sizeof(nr));
1092 if (ret != (ssize_t)sizeof(nr))
1098 fprintf(fp, "# cmdline : ");
1100 for (i = 0; i < nr; i++) {
1101 str = do_read_string(fd, ph);
1102 fprintf(fp, "%s ", str);
1108 static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1114 ret = read(fd, &nr, sizeof(nr));
1115 if (ret != (ssize_t)sizeof(nr))
1121 for (i = 0; i < nr; i++) {
1122 str = do_read_string(fd, ph);
1123 fprintf(fp, "# sibling cores : %s\n", str);
1127 ret = read(fd, &nr, sizeof(nr));
1128 if (ret != (ssize_t)sizeof(nr))
1134 for (i = 0; i < nr; i++) {
1135 str = do_read_string(fd, ph);
1136 fprintf(fp, "# sibling threads : %s\n", str);
1141 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1143 struct perf_event_attr attr;
1147 u32 nre, sz, nr, i, j;
1151 /* number of events */
1152 ret = read(fd, &nre, sizeof(nre));
1153 if (ret != (ssize_t)sizeof(nre))
1157 nre = bswap_32(nre);
1159 ret = read(fd, &sz, sizeof(sz));
1160 if (ret != (ssize_t)sizeof(sz))
1166 memset(&attr, 0, sizeof(attr));
1168 /* buffer to hold on file attr struct */
1174 if (sz < (ssize_t)msz)
1177 for (i = 0 ; i < nre; i++) {
1180 * must read entire on-file attr struct to
1181 * sync up with layout.
1183 ret = read(fd, buf, sz);
1184 if (ret != (ssize_t)sz)
1188 perf_event__attr_swap(buf);
1190 memcpy(&attr, buf, msz);
1192 ret = read(fd, &nr, sizeof(nr));
1193 if (ret != (ssize_t)sizeof(nr))
1199 str = do_read_string(fd, ph);
1200 fprintf(fp, "# event : name = %s, ", str);
1203 fprintf(fp, "type = %d, config = 0x%"PRIx64
1204 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1210 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1212 attr.exclude_kernel);
1215 fprintf(fp, ", id = {");
1217 for (j = 0 ; j < nr; j++) {
1218 ret = read(fd, &id, sizeof(id));
1219 if (ret != (ssize_t)sizeof(id))
1228 fprintf(fp, " %"PRIu64, id);
1237 fprintf(fp, "# event desc: not available or unable to read\n");
1240 static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1245 ret = read(fd, &mem, sizeof(mem));
1246 if (ret != sizeof(mem))
1250 mem = bswap_64(mem);
1252 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1255 fprintf(fp, "# total memory : unknown\n");
1258 static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1263 uint64_t mem_total, mem_free;
1266 ret = read(fd, &nr, sizeof(nr));
1267 if (ret != (ssize_t)sizeof(nr))
1273 for (i = 0; i < nr; i++) {
1276 ret = read(fd, &c, sizeof(c));
1277 if (ret != (ssize_t)sizeof(c))
1283 ret = read(fd, &mem_total, sizeof(u64));
1284 if (ret != sizeof(u64))
1287 ret = read(fd, &mem_free, sizeof(u64));
1288 if (ret != sizeof(u64))
1291 if (h->needs_swap) {
1292 mem_total = bswap_64(mem_total);
1293 mem_free = bswap_64(mem_free);
1296 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1297 " free = %"PRIu64" kB\n",
1302 str = do_read_string(fd, h);
1303 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1308 fprintf(fp, "# numa topology : not available\n");
1311 static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1313 char *str = do_read_string(fd, ph);
1314 fprintf(fp, "# cpuid : %s\n", str);
1318 static int __event_process_build_id(struct build_id_event *bev,
1320 struct perf_session *session)
1323 struct list_head *head;
1324 struct machine *machine;
1327 enum dso_kernel_type dso_type;
1329 machine = perf_session__findnew_machine(session, bev->pid);
1333 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1336 case PERF_RECORD_MISC_KERNEL:
1337 dso_type = DSO_TYPE_KERNEL;
1338 head = &machine->kernel_dsos;
1340 case PERF_RECORD_MISC_GUEST_KERNEL:
1341 dso_type = DSO_TYPE_GUEST_KERNEL;
1342 head = &machine->kernel_dsos;
1344 case PERF_RECORD_MISC_USER:
1345 case PERF_RECORD_MISC_GUEST_USER:
1346 dso_type = DSO_TYPE_USER;
1347 head = &machine->user_dsos;
1353 dso = __dsos__findnew(head, filename);
1355 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1357 dso__set_build_id(dso, &bev->build_id);
1359 if (filename[0] == '[')
1360 dso->kernel = dso_type;
1362 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1364 pr_debug("build id event received for %s: %s\n",
1365 dso->long_name, sbuild_id);
1373 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1374 int input, u64 offset, u64 size)
1376 struct perf_session *session = container_of(header, struct perf_session, header);
1378 struct perf_event_header header;
1379 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1382 struct build_id_event bev;
1383 char filename[PATH_MAX];
1384 u64 limit = offset + size;
1386 while (offset < limit) {
1389 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1392 if (header->needs_swap)
1393 perf_event_header__bswap(&old_bev.header);
1395 len = old_bev.header.size - sizeof(old_bev);
1396 if (read(input, filename, len) != len)
1399 bev.header = old_bev.header;
1402 * As the pid is the missing value, we need to fill
1403 * it properly. The header.misc value give us nice hint.
1405 bev.pid = HOST_KERNEL_ID;
1406 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1407 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1408 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1410 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1411 __event_process_build_id(&bev, filename, session);
1413 offset += bev.header.size;
1419 static int perf_header__read_build_ids(struct perf_header *header,
1420 int input, u64 offset, u64 size)
1422 struct perf_session *session = container_of(header, struct perf_session, header);
1423 struct build_id_event bev;
1424 char filename[PATH_MAX];
1425 u64 limit = offset + size, orig_offset = offset;
1428 while (offset < limit) {
1431 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1434 if (header->needs_swap)
1435 perf_event_header__bswap(&bev.header);
1437 len = bev.header.size - sizeof(bev);
1438 if (read(input, filename, len) != len)
1441 * The a1645ce1 changeset:
1443 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1445 * Added a field to struct build_id_event that broke the file
1448 * Since the kernel build-id is the first entry, process the
1449 * table using the old format if the well known
1450 * '[kernel.kallsyms]' string for the kernel build-id has the
1451 * first 4 characters chopped off (where the pid_t sits).
1453 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1454 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1456 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1459 __event_process_build_id(&bev, filename, session);
1461 offset += bev.header.size;
1468 static int process_trace_info(struct perf_file_section *section __unused,
1469 struct perf_header *ph __unused,
1470 int feat __unused, int fd)
1472 trace_report(fd, false);
1476 static int process_build_id(struct perf_file_section *section,
1477 struct perf_header *ph,
1478 int feat __unused, int fd)
1480 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1481 pr_debug("Failed to read buildids, continuing...\n");
1485 struct feature_ops {
1486 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1487 void (*print)(struct perf_header *h, int fd, FILE *fp);
1488 int (*process)(struct perf_file_section *section,
1489 struct perf_header *h, int feat, int fd);
1494 #define FEAT_OPA(n, func) \
1495 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1496 #define FEAT_OPP(n, func) \
1497 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1498 .process = process_##func }
1499 #define FEAT_OPF(n, func) \
1500 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1503 /* feature_ops not implemented: */
1504 #define print_trace_info NULL
1505 #define print_build_id NULL
1507 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1508 FEAT_OPP(HEADER_TRACE_INFO, trace_info),
1509 FEAT_OPP(HEADER_BUILD_ID, build_id),
1510 FEAT_OPA(HEADER_HOSTNAME, hostname),
1511 FEAT_OPA(HEADER_OSRELEASE, osrelease),
1512 FEAT_OPA(HEADER_VERSION, version),
1513 FEAT_OPA(HEADER_ARCH, arch),
1514 FEAT_OPA(HEADER_NRCPUS, nrcpus),
1515 FEAT_OPA(HEADER_CPUDESC, cpudesc),
1516 FEAT_OPA(HEADER_CPUID, cpuid),
1517 FEAT_OPA(HEADER_TOTAL_MEM, total_mem),
1518 FEAT_OPA(HEADER_EVENT_DESC, event_desc),
1519 FEAT_OPA(HEADER_CMDLINE, cmdline),
1520 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1521 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
1524 struct header_print_data {
1526 bool full; /* extended list of headers */
1529 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1530 struct perf_header *ph,
1531 int feat, int fd, void *data)
1533 struct header_print_data *hd = data;
1535 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1536 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1537 "%d, continuing...\n", section->offset, feat);
1540 if (feat >= HEADER_LAST_FEATURE) {
1541 pr_warning("unknown feature %d\n", feat);
1544 if (!feat_ops[feat].print)
1547 if (!feat_ops[feat].full_only || hd->full)
1548 feat_ops[feat].print(ph, fd, hd->fp);
1550 fprintf(hd->fp, "# %s info available, use -I to display\n",
1551 feat_ops[feat].name);
1556 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1558 struct header_print_data hd;
1559 struct perf_header *header = &session->header;
1560 int fd = session->fd;
1564 perf_header__process_sections(header, fd, &hd,
1565 perf_file_section__fprintf_info);
1569 static int do_write_feat(int fd, struct perf_header *h, int type,
1570 struct perf_file_section **p,
1571 struct perf_evlist *evlist)
1576 if (perf_header__has_feat(h, type)) {
1577 if (!feat_ops[type].write)
1580 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1582 err = feat_ops[type].write(fd, h, evlist);
1584 pr_debug("failed to write feature %d\n", type);
1586 /* undo anything written */
1587 lseek(fd, (*p)->offset, SEEK_SET);
1591 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1597 static int perf_header__adds_write(struct perf_header *header,
1598 struct perf_evlist *evlist, int fd)
1601 struct perf_file_section *feat_sec, *p;
1607 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1611 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
1612 if (feat_sec == NULL)
1615 sec_size = sizeof(*feat_sec) * nr_sections;
1617 sec_start = header->data_offset + header->data_size;
1618 lseek(fd, sec_start + sec_size, SEEK_SET);
1620 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1621 if (do_write_feat(fd, header, feat, &p, evlist))
1622 perf_header__clear_feat(header, feat);
1625 lseek(fd, sec_start, SEEK_SET);
1627 * may write more than needed due to dropped feature, but
1628 * this is okay, reader will skip the mising entries
1630 err = do_write(fd, feat_sec, sec_size);
1632 pr_debug("failed to write feature section\n");
1637 int perf_header__write_pipe(int fd)
1639 struct perf_pipe_file_header f_header;
1642 f_header = (struct perf_pipe_file_header){
1643 .magic = PERF_MAGIC,
1644 .size = sizeof(f_header),
1647 err = do_write(fd, &f_header, sizeof(f_header));
1649 pr_debug("failed to write perf pipe header\n");
1656 int perf_session__write_header(struct perf_session *session,
1657 struct perf_evlist *evlist,
1658 int fd, bool at_exit)
1660 struct perf_file_header f_header;
1661 struct perf_file_attr f_attr;
1662 struct perf_header *header = &session->header;
1663 struct perf_evsel *attr, *pair = NULL;
1666 lseek(fd, sizeof(f_header), SEEK_SET);
1668 if (session->evlist != evlist)
1669 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node);
1671 list_for_each_entry(attr, &evlist->entries, node) {
1672 attr->id_offset = lseek(fd, 0, SEEK_CUR);
1673 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
1676 pr_debug("failed to write perf header\n");
1679 if (session->evlist != evlist) {
1680 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1683 attr->ids += pair->ids;
1684 pair = list_entry(pair->node.next, struct perf_evsel, node);
1688 header->attr_offset = lseek(fd, 0, SEEK_CUR);
1690 list_for_each_entry(attr, &evlist->entries, node) {
1691 f_attr = (struct perf_file_attr){
1694 .offset = attr->id_offset,
1695 .size = attr->ids * sizeof(u64),
1698 err = do_write(fd, &f_attr, sizeof(f_attr));
1700 pr_debug("failed to write perf header attribute\n");
1705 header->event_offset = lseek(fd, 0, SEEK_CUR);
1706 header->event_size = event_count * sizeof(struct perf_trace_event_type);
1708 err = do_write(fd, events, header->event_size);
1710 pr_debug("failed to write perf header events\n");
1715 header->data_offset = lseek(fd, 0, SEEK_CUR);
1718 err = perf_header__adds_write(header, evlist, fd);
1723 f_header = (struct perf_file_header){
1724 .magic = PERF_MAGIC,
1725 .size = sizeof(f_header),
1726 .attr_size = sizeof(f_attr),
1728 .offset = header->attr_offset,
1729 .size = evlist->nr_entries * sizeof(f_attr),
1732 .offset = header->data_offset,
1733 .size = header->data_size,
1736 .offset = header->event_offset,
1737 .size = header->event_size,
1741 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
1743 lseek(fd, 0, SEEK_SET);
1744 err = do_write(fd, &f_header, sizeof(f_header));
1746 pr_debug("failed to write perf header\n");
1749 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1755 static int perf_header__getbuffer64(struct perf_header *header,
1756 int fd, void *buf, size_t size)
1758 if (readn(fd, buf, size) <= 0)
1761 if (header->needs_swap)
1762 mem_bswap_64(buf, size);
1767 int perf_header__process_sections(struct perf_header *header, int fd,
1769 int (*process)(struct perf_file_section *section,
1770 struct perf_header *ph,
1771 int feat, int fd, void *data))
1773 struct perf_file_section *feat_sec, *sec;
1779 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1783 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
1787 sec_size = sizeof(*feat_sec) * nr_sections;
1789 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1791 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
1795 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
1796 err = process(sec++, header, feat, fd, data);
1806 static const int attr_file_abi_sizes[] = {
1807 [0] = PERF_ATTR_SIZE_VER0,
1808 [1] = PERF_ATTR_SIZE_VER1,
1813 * In the legacy file format, the magic number is not used to encode endianness.
1814 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
1815 * on ABI revisions, we need to try all combinations for all endianness to
1816 * detect the endianness.
1818 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
1820 uint64_t ref_size, attr_size;
1823 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
1824 ref_size = attr_file_abi_sizes[i]
1825 + sizeof(struct perf_file_section);
1826 if (hdr_sz != ref_size) {
1827 attr_size = bswap_64(hdr_sz);
1828 if (attr_size != ref_size)
1831 ph->needs_swap = true;
1833 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
1838 /* could not determine endianness */
1842 #define PERF_PIPE_HDR_VER0 16
1844 static const size_t attr_pipe_abi_sizes[] = {
1845 [0] = PERF_PIPE_HDR_VER0,
1850 * In the legacy pipe format, there is an implicit assumption that endiannesss
1851 * between host recording the samples, and host parsing the samples is the
1852 * same. This is not always the case given that the pipe output may always be
1853 * redirected into a file and analyzed on a different machine with possibly a
1854 * different endianness and perf_event ABI revsions in the perf tool itself.
1856 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
1861 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
1862 if (hdr_sz != attr_pipe_abi_sizes[i]) {
1863 attr_size = bswap_64(hdr_sz);
1864 if (attr_size != hdr_sz)
1867 ph->needs_swap = true;
1869 pr_debug("Pipe ABI%d perf.data file detected\n", i);
1875 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
1876 bool is_pipe, struct perf_header *ph)
1880 /* check for legacy format */
1881 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
1883 pr_debug("legacy perf.data format\n");
1885 return try_all_pipe_abis(hdr_sz, ph);
1887 return try_all_file_abis(hdr_sz, ph);
1890 * the new magic number serves two purposes:
1891 * - unique number to identify actual perf.data files
1892 * - encode endianness of file
1895 /* check magic number with one endianness */
1896 if (magic == __perf_magic2)
1899 /* check magic number with opposite endianness */
1900 if (magic != __perf_magic2_sw)
1903 ph->needs_swap = true;
1908 int perf_file_header__read(struct perf_file_header *header,
1909 struct perf_header *ph, int fd)
1913 lseek(fd, 0, SEEK_SET);
1915 ret = readn(fd, header, sizeof(*header));
1919 if (check_magic_endian(header->magic,
1920 header->attr_size, false, ph) < 0) {
1921 pr_debug("magic/endian check failed\n");
1925 if (ph->needs_swap) {
1926 mem_bswap_64(header, offsetof(struct perf_file_header,
1930 if (header->size != sizeof(*header)) {
1931 /* Support the previous format */
1932 if (header->size == offsetof(typeof(*header), adds_features))
1933 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1936 } else if (ph->needs_swap) {
1939 * feature bitmap is declared as an array of unsigned longs --
1940 * not good since its size can differ between the host that
1941 * generated the data file and the host analyzing the file.
1943 * We need to handle endianness, but we don't know the size of
1944 * the unsigned long where the file was generated. Take a best
1945 * guess at determining it: try 64-bit swap first (ie., file
1946 * created on a 64-bit host), and check if the hostname feature
1947 * bit is set (this feature bit is forced on as of fbe96f2).
1948 * If the bit is not, undo the 64-bit swap and try a 32-bit
1949 * swap. If the hostname bit is still not set (e.g., older data
1950 * file), punt and fallback to the original behavior --
1951 * clearing all feature bits and setting buildid.
1953 for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i)
1954 header->adds_features[i] = bswap_64(header->adds_features[i]);
1956 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1957 for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) {
1958 header->adds_features[i] = bswap_64(header->adds_features[i]);
1959 header->adds_features[i] = bswap_32(header->adds_features[i]);
1963 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1964 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1965 set_bit(HEADER_BUILD_ID, header->adds_features);
1969 memcpy(&ph->adds_features, &header->adds_features,
1970 sizeof(ph->adds_features));
1972 ph->event_offset = header->event_types.offset;
1973 ph->event_size = header->event_types.size;
1974 ph->data_offset = header->data.offset;
1975 ph->data_size = header->data.size;
1979 static int perf_file_section__process(struct perf_file_section *section,
1980 struct perf_header *ph,
1981 int feat, int fd, void *data __used)
1983 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1984 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1985 "%d, continuing...\n", section->offset, feat);
1989 if (feat >= HEADER_LAST_FEATURE) {
1990 pr_debug("unknown feature %d, continuing...\n", feat);
1994 if (!feat_ops[feat].process)
1997 return feat_ops[feat].process(section, ph, feat, fd);
2000 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2001 struct perf_header *ph, int fd,
2006 ret = readn(fd, header, sizeof(*header));
2010 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2011 pr_debug("endian/magic failed\n");
2016 header->size = bswap_64(header->size);
2018 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2024 static int perf_header__read_pipe(struct perf_session *session, int fd)
2026 struct perf_header *header = &session->header;
2027 struct perf_pipe_file_header f_header;
2029 if (perf_file_header__read_pipe(&f_header, header, fd,
2030 session->repipe) < 0) {
2031 pr_debug("incompatible file format\n");
2040 static int read_attr(int fd, struct perf_header *ph,
2041 struct perf_file_attr *f_attr)
2043 struct perf_event_attr *attr = &f_attr->attr;
2045 size_t our_sz = sizeof(f_attr->attr);
2048 memset(f_attr, 0, sizeof(*f_attr));
2050 /* read minimal guaranteed structure */
2051 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2053 pr_debug("cannot read %d bytes of header attr\n",
2054 PERF_ATTR_SIZE_VER0);
2058 /* on file perf_event_attr size */
2066 sz = PERF_ATTR_SIZE_VER0;
2067 } else if (sz > our_sz) {
2068 pr_debug("file uses a more recent and unsupported ABI"
2069 " (%zu bytes extra)\n", sz - our_sz);
2072 /* what we have not yet read and that we know about */
2073 left = sz - PERF_ATTR_SIZE_VER0;
2076 ptr += PERF_ATTR_SIZE_VER0;
2078 ret = readn(fd, ptr, left);
2080 /* read perf_file_section, ids are read in caller */
2081 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2083 return ret <= 0 ? -1 : 0;
2086 int perf_session__read_header(struct perf_session *session, int fd)
2088 struct perf_header *header = &session->header;
2089 struct perf_file_header f_header;
2090 struct perf_file_attr f_attr;
2092 int nr_attrs, nr_ids, i, j;
2094 session->evlist = perf_evlist__new(NULL, NULL);
2095 if (session->evlist == NULL)
2098 if (session->fd_pipe)
2099 return perf_header__read_pipe(session, fd);
2101 if (perf_file_header__read(&f_header, header, fd) < 0)
2104 nr_attrs = f_header.attrs.size / f_header.attr_size;
2105 lseek(fd, f_header.attrs.offset, SEEK_SET);
2107 for (i = 0; i < nr_attrs; i++) {
2108 struct perf_evsel *evsel;
2111 if (read_attr(fd, header, &f_attr) < 0)
2114 if (header->needs_swap)
2115 perf_event__attr_swap(&f_attr.attr);
2117 tmp = lseek(fd, 0, SEEK_CUR);
2118 evsel = perf_evsel__new(&f_attr.attr, i);
2121 goto out_delete_evlist;
2123 * Do it before so that if perf_evsel__alloc_id fails, this
2124 * entry gets purged too at perf_evlist__delete().
2126 perf_evlist__add(session->evlist, evsel);
2128 nr_ids = f_attr.ids.size / sizeof(u64);
2130 * We don't have the cpu and thread maps on the header, so
2131 * for allocating the perf_sample_id table we fake 1 cpu and
2132 * hattr->ids threads.
2134 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2135 goto out_delete_evlist;
2137 lseek(fd, f_attr.ids.offset, SEEK_SET);
2139 for (j = 0; j < nr_ids; j++) {
2140 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2143 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2146 lseek(fd, tmp, SEEK_SET);
2149 symbol_conf.nr_events = nr_attrs;
2151 if (f_header.event_types.size) {
2152 lseek(fd, f_header.event_types.offset, SEEK_SET);
2153 events = malloc(f_header.event_types.size);
2156 if (perf_header__getbuffer64(header, fd, events,
2157 f_header.event_types.size))
2159 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2162 perf_header__process_sections(header, fd, NULL,
2163 perf_file_section__process);
2165 lseek(fd, header->data_offset, SEEK_SET);
2173 perf_evlist__delete(session->evlist);
2174 session->evlist = NULL;
2178 int perf_event__synthesize_attr(struct perf_tool *tool,
2179 struct perf_event_attr *attr, u16 ids, u64 *id,
2180 perf_event__handler_t process)
2182 union perf_event *ev;
2186 size = sizeof(struct perf_event_attr);
2187 size = ALIGN(size, sizeof(u64));
2188 size += sizeof(struct perf_event_header);
2189 size += ids * sizeof(u64);
2196 ev->attr.attr = *attr;
2197 memcpy(ev->attr.id, id, ids * sizeof(u64));
2199 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2200 ev->attr.header.size = size;
2202 err = process(tool, ev, NULL, NULL);
2209 int perf_event__synthesize_attrs(struct perf_tool *tool,
2210 struct perf_session *session,
2211 perf_event__handler_t process)
2213 struct perf_evsel *attr;
2216 list_for_each_entry(attr, &session->evlist->entries, node) {
2217 err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids,
2220 pr_debug("failed to create perf header attribute\n");
2228 int perf_event__process_attr(union perf_event *event,
2229 struct perf_evlist **pevlist)
2231 unsigned int i, ids, n_ids;
2232 struct perf_evsel *evsel;
2233 struct perf_evlist *evlist = *pevlist;
2235 if (evlist == NULL) {
2236 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2241 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2245 perf_evlist__add(evlist, evsel);
2247 ids = event->header.size;
2248 ids -= (void *)&event->attr.id - (void *)event;
2249 n_ids = ids / sizeof(u64);
2251 * We don't have the cpu and thread maps on the header, so
2252 * for allocating the perf_sample_id table we fake 1 cpu and
2253 * hattr->ids threads.
2255 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2258 for (i = 0; i < n_ids; i++) {
2259 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2265 int perf_event__synthesize_event_type(struct perf_tool *tool,
2266 u64 event_id, char *name,
2267 perf_event__handler_t process,
2268 struct machine *machine)
2270 union perf_event ev;
2274 memset(&ev, 0, sizeof(ev));
2276 ev.event_type.event_type.event_id = event_id;
2277 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2278 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2280 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2281 size = strlen(ev.event_type.event_type.name);
2282 size = ALIGN(size, sizeof(u64));
2283 ev.event_type.header.size = sizeof(ev.event_type) -
2284 (sizeof(ev.event_type.event_type.name) - size);
2286 err = process(tool, &ev, NULL, machine);
2291 int perf_event__synthesize_event_types(struct perf_tool *tool,
2292 perf_event__handler_t process,
2293 struct machine *machine)
2295 struct perf_trace_event_type *type;
2298 for (i = 0; i < event_count; i++) {
2301 err = perf_event__synthesize_event_type(tool, type->event_id,
2302 type->name, process,
2305 pr_debug("failed to create perf header event type\n");
2313 int perf_event__process_event_type(struct perf_tool *tool __unused,
2314 union perf_event *event)
2316 if (perf_header__push_event(event->event_type.event_type.event_id,
2317 event->event_type.event_type.name) < 0)
2323 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2324 struct perf_evlist *evlist,
2325 perf_event__handler_t process)
2327 union perf_event ev;
2328 struct tracing_data *tdata;
2329 ssize_t size = 0, aligned_size = 0, padding;
2333 * We are going to store the size of the data followed
2334 * by the data contents. Since the fd descriptor is a pipe,
2335 * we cannot seek back to store the size of the data once
2336 * we know it. Instead we:
2338 * - write the tracing data to the temp file
2339 * - get/write the data size to pipe
2340 * - write the tracing data from the temp file
2343 tdata = tracing_data_get(&evlist->entries, fd, true);
2347 memset(&ev, 0, sizeof(ev));
2349 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2351 aligned_size = ALIGN(size, sizeof(u64));
2352 padding = aligned_size - size;
2353 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2354 ev.tracing_data.size = aligned_size;
2356 process(tool, &ev, NULL, NULL);
2359 * The put function will copy all the tracing data
2360 * stored in temp file to the pipe.
2362 tracing_data_put(tdata);
2364 write_padded(fd, NULL, 0, padding);
2366 return aligned_size;
2369 int perf_event__process_tracing_data(union perf_event *event,
2370 struct perf_session *session)
2372 ssize_t size_read, padding, size = event->tracing_data.size;
2373 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2376 /* setup for reading amidst mmap */
2377 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2380 size_read = trace_report(session->fd, session->repipe);
2382 padding = ALIGN(size_read, sizeof(u64)) - size_read;
2384 if (read(session->fd, buf, padding) < 0)
2385 die("reading input file");
2386 if (session->repipe) {
2387 int retw = write(STDOUT_FILENO, buf, padding);
2388 if (retw <= 0 || retw != padding)
2389 die("repiping tracing data padding");
2392 if (size_read + padding != size)
2393 die("tracing data size mismatch");
2395 return size_read + padding;
2398 int perf_event__synthesize_build_id(struct perf_tool *tool,
2399 struct dso *pos, u16 misc,
2400 perf_event__handler_t process,
2401 struct machine *machine)
2403 union perf_event ev;
2410 memset(&ev, 0, sizeof(ev));
2412 len = pos->long_name_len + 1;
2413 len = ALIGN(len, NAME_ALIGN);
2414 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2415 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2416 ev.build_id.header.misc = misc;
2417 ev.build_id.pid = machine->pid;
2418 ev.build_id.header.size = sizeof(ev.build_id) + len;
2419 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2421 err = process(tool, &ev, NULL, machine);
2426 int perf_event__process_build_id(struct perf_tool *tool __used,
2427 union perf_event *event,
2428 struct perf_session *session)
2430 __event_process_build_id(&event->build_id,
2431 event->build_id.filename,
2436 void disable_buildid_cache(void)
2438 no_buildid_cache = true;