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
)
178 * If header_argv has already been set, do not override it.
179 * This allows a command to set the cmdline, parse args and
180 * then call another builtin function that implements a
181 * command -- e.g, cmd_kvm calling cmd_record.
186 header_argc
= (u32
)argc
;
188 /* do not include NULL termination */
189 header_argv
= calloc(argc
, sizeof(char *));
194 * must copy argv contents because it gets moved
195 * around during option parsing
197 for (i
= 0; i
< argc
; i
++)
198 header_argv
[i
] = argv
[i
];
203 #define dsos__for_each_with_build_id(pos, head) \
204 list_for_each_entry(pos, head, node) \
205 if (!pos->has_build_id) \
209 static int __dsos__write_buildid_table(struct list_head
*head
, pid_t pid
,
214 dsos__for_each_with_build_id(pos
, head
) {
216 struct build_id_event b
;
221 len
= pos
->long_name_len
+ 1;
222 len
= ALIGN(len
, NAME_ALIGN
);
223 memset(&b
, 0, sizeof(b
));
224 memcpy(&b
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
226 b
.header
.misc
= misc
;
227 b
.header
.size
= sizeof(b
) + len
;
228 err
= do_write(fd
, &b
, sizeof(b
));
231 err
= write_padded(fd
, pos
->long_name
,
232 pos
->long_name_len
+ 1, len
);
240 static int machine__write_buildid_table(struct machine
*machine
, int fd
)
243 u16 kmisc
= PERF_RECORD_MISC_KERNEL
,
244 umisc
= PERF_RECORD_MISC_USER
;
246 if (!machine__is_host(machine
)) {
247 kmisc
= PERF_RECORD_MISC_GUEST_KERNEL
;
248 umisc
= PERF_RECORD_MISC_GUEST_USER
;
251 err
= __dsos__write_buildid_table(&machine
->kernel_dsos
, machine
->pid
,
254 err
= __dsos__write_buildid_table(&machine
->user_dsos
,
255 machine
->pid
, umisc
, fd
);
259 static int dsos__write_buildid_table(struct perf_header
*header
, int fd
)
261 struct perf_session
*session
= container_of(header
,
262 struct perf_session
, header
);
264 int err
= machine__write_buildid_table(&session
->host_machine
, fd
);
269 for (nd
= rb_first(&session
->machines
); nd
; nd
= rb_next(nd
)) {
270 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
271 err
= machine__write_buildid_table(pos
, fd
);
278 int build_id_cache__add_s(const char *sbuild_id
, const char *debugdir
,
279 const char *name
, bool is_kallsyms
)
281 const size_t size
= PATH_MAX
;
282 char *realname
, *filename
= zalloc(size
),
283 *linkname
= zalloc(size
), *targetname
;
287 if (symbol_conf
.kptr_restrict
) {
288 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
291 realname
= (char *)name
;
293 realname
= realpath(name
, NULL
);
295 if (realname
== NULL
|| filename
== NULL
|| linkname
== NULL
)
298 len
= scnprintf(filename
, size
, "%s%s%s",
299 debugdir
, is_kallsyms
? "/" : "", realname
);
300 if (mkdir_p(filename
, 0755))
303 snprintf(filename
+ len
, size
- len
, "/%s", sbuild_id
);
305 if (access(filename
, F_OK
)) {
307 if (copyfile("/proc/kallsyms", filename
))
309 } else if (link(realname
, filename
) && copyfile(name
, filename
))
313 len
= scnprintf(linkname
, size
, "%s/.build-id/%.2s",
314 debugdir
, sbuild_id
);
316 if (access(linkname
, X_OK
) && mkdir_p(linkname
, 0755))
319 snprintf(linkname
+ len
, size
- len
, "/%s", sbuild_id
+ 2);
320 targetname
= filename
+ strlen(debugdir
) - 5;
321 memcpy(targetname
, "../..", 5);
323 if (symlink(targetname
, linkname
) == 0)
333 static int build_id_cache__add_b(const u8
*build_id
, size_t build_id_size
,
334 const char *name
, const char *debugdir
,
337 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
339 build_id__sprintf(build_id
, build_id_size
, sbuild_id
);
341 return build_id_cache__add_s(sbuild_id
, debugdir
, name
, is_kallsyms
);
344 int build_id_cache__remove_s(const char *sbuild_id
, const char *debugdir
)
346 const size_t size
= PATH_MAX
;
347 char *filename
= zalloc(size
),
348 *linkname
= zalloc(size
);
351 if (filename
== NULL
|| linkname
== NULL
)
354 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
355 debugdir
, sbuild_id
, sbuild_id
+ 2);
357 if (access(linkname
, F_OK
))
360 if (readlink(linkname
, filename
, size
- 1) < 0)
363 if (unlink(linkname
))
367 * Since the link is relative, we must make it absolute:
369 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
370 debugdir
, sbuild_id
, filename
);
372 if (unlink(linkname
))
382 static int dso__cache_build_id(struct dso
*dso
, const char *debugdir
)
384 bool is_kallsyms
= dso
->kernel
&& dso
->long_name
[0] != '/';
386 return build_id_cache__add_b(dso
->build_id
, sizeof(dso
->build_id
),
387 dso
->long_name
, debugdir
, is_kallsyms
);
390 static int __dsos__cache_build_ids(struct list_head
*head
, const char *debugdir
)
395 dsos__for_each_with_build_id(pos
, head
)
396 if (dso__cache_build_id(pos
, debugdir
))
402 static int machine__cache_build_ids(struct machine
*machine
, const char *debugdir
)
404 int ret
= __dsos__cache_build_ids(&machine
->kernel_dsos
, debugdir
);
405 ret
|= __dsos__cache_build_ids(&machine
->user_dsos
, debugdir
);
409 static int perf_session__cache_build_ids(struct perf_session
*session
)
413 char debugdir
[PATH_MAX
];
415 snprintf(debugdir
, sizeof(debugdir
), "%s", buildid_dir
);
417 if (mkdir(debugdir
, 0755) != 0 && errno
!= EEXIST
)
420 ret
= machine__cache_build_ids(&session
->host_machine
, debugdir
);
422 for (nd
= rb_first(&session
->machines
); nd
; nd
= rb_next(nd
)) {
423 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
424 ret
|= machine__cache_build_ids(pos
, debugdir
);
429 static bool machine__read_build_ids(struct machine
*machine
, bool with_hits
)
431 bool ret
= __dsos__read_build_ids(&machine
->kernel_dsos
, with_hits
);
432 ret
|= __dsos__read_build_ids(&machine
->user_dsos
, with_hits
);
436 static bool perf_session__read_build_ids(struct perf_session
*session
, bool with_hits
)
439 bool ret
= machine__read_build_ids(&session
->host_machine
, with_hits
);
441 for (nd
= rb_first(&session
->machines
); nd
; nd
= rb_next(nd
)) {
442 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
443 ret
|= machine__read_build_ids(pos
, with_hits
);
449 static int write_tracing_data(int fd
, struct perf_header
*h __used
,
450 struct perf_evlist
*evlist
)
452 return read_tracing_data(fd
, &evlist
->entries
);
456 static int write_build_id(int fd
, struct perf_header
*h
,
457 struct perf_evlist
*evlist __used
)
459 struct perf_session
*session
;
462 session
= container_of(h
, struct perf_session
, header
);
464 if (!perf_session__read_build_ids(session
, true))
467 err
= dsos__write_buildid_table(h
, fd
);
469 pr_debug("failed to write buildid table\n");
472 if (!no_buildid_cache
)
473 perf_session__cache_build_ids(session
);
478 static int write_hostname(int fd
, struct perf_header
*h __used
,
479 struct perf_evlist
*evlist __used
)
488 return do_write_string(fd
, uts
.nodename
);
491 static int write_osrelease(int fd
, struct perf_header
*h __used
,
492 struct perf_evlist
*evlist __used
)
501 return do_write_string(fd
, uts
.release
);
504 static int write_arch(int fd
, struct perf_header
*h __used
,
505 struct perf_evlist
*evlist __used
)
514 return do_write_string(fd
, uts
.machine
);
517 static int write_version(int fd
, struct perf_header
*h __used
,
518 struct perf_evlist
*evlist __used
)
520 return do_write_string(fd
, perf_version_string
);
523 static int write_cpudesc(int fd
, struct perf_header
*h __used
,
524 struct perf_evlist
*evlist __used
)
527 #define CPUINFO_PROC NULL
532 const char *search
= CPUINFO_PROC
;
539 file
= fopen("/proc/cpuinfo", "r");
543 while (getline(&buf
, &len
, file
) > 0) {
544 ret
= strncmp(buf
, search
, strlen(search
));
554 p
= strchr(buf
, ':');
555 if (p
&& *(p
+1) == ' ' && *(p
+2))
561 /* squash extra space characters (branding string) */
568 while (*q
&& isspace(*q
))
571 while ((*r
++ = *q
++));
575 ret
= do_write_string(fd
, s
);
582 static int write_nrcpus(int fd
, struct perf_header
*h __used
,
583 struct perf_evlist
*evlist __used
)
589 nr
= sysconf(_SC_NPROCESSORS_CONF
);
593 nrc
= (u32
)(nr
& UINT_MAX
);
595 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
599 nra
= (u32
)(nr
& UINT_MAX
);
601 ret
= do_write(fd
, &nrc
, sizeof(nrc
));
605 return do_write(fd
, &nra
, sizeof(nra
));
608 static int write_event_desc(int fd
, struct perf_header
*h __used
,
609 struct perf_evlist
*evlist
)
611 struct perf_evsel
*attr
;
612 u32 nre
= 0, nri
, sz
;
615 list_for_each_entry(attr
, &evlist
->entries
, node
)
619 * write number of events
621 ret
= do_write(fd
, &nre
, sizeof(nre
));
626 * size of perf_event_attr struct
628 sz
= (u32
)sizeof(attr
->attr
);
629 ret
= do_write(fd
, &sz
, sizeof(sz
));
633 list_for_each_entry(attr
, &evlist
->entries
, node
) {
635 ret
= do_write(fd
, &attr
->attr
, sz
);
639 * write number of unique id per event
640 * there is one id per instance of an event
642 * copy into an nri to be independent of the
646 ret
= do_write(fd
, &nri
, sizeof(nri
));
651 * write event string as passed on cmdline
653 ret
= do_write_string(fd
, perf_evsel__name(attr
));
657 * write unique ids for this event
659 ret
= do_write(fd
, attr
->id
, attr
->ids
* sizeof(u64
));
666 static int write_cmdline(int fd
, struct perf_header
*h __used
,
667 struct perf_evlist
*evlist __used
)
669 char buf
[MAXPATHLEN
];
675 * actual atual path to perf binary
677 sprintf(proc
, "/proc/%d/exe", getpid());
678 ret
= readlink(proc
, buf
, sizeof(buf
));
682 /* readlink() does not add null termination */
685 /* account for binary path */
688 ret
= do_write(fd
, &n
, sizeof(n
));
692 ret
= do_write_string(fd
, buf
);
696 for (i
= 0 ; i
< header_argc
; i
++) {
697 ret
= do_write_string(fd
, header_argv
[i
]);
704 #define CORE_SIB_FMT \
705 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
706 #define THRD_SIB_FMT \
707 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
712 char **core_siblings
;
713 char **thread_siblings
;
716 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
719 char filename
[MAXPATHLEN
];
720 char *buf
= NULL
, *p
;
725 sprintf(filename
, CORE_SIB_FMT
, cpu
);
726 fp
= fopen(filename
, "r");
730 if (getline(&buf
, &len
, fp
) <= 0)
735 p
= strchr(buf
, '\n');
739 for (i
= 0; i
< tp
->core_sib
; i
++) {
740 if (!strcmp(buf
, tp
->core_siblings
[i
]))
743 if (i
== tp
->core_sib
) {
744 tp
->core_siblings
[i
] = buf
;
750 sprintf(filename
, THRD_SIB_FMT
, cpu
);
751 fp
= fopen(filename
, "r");
755 if (getline(&buf
, &len
, fp
) <= 0)
758 p
= strchr(buf
, '\n');
762 for (i
= 0; i
< tp
->thread_sib
; i
++) {
763 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
766 if (i
== tp
->thread_sib
) {
767 tp
->thread_siblings
[i
] = buf
;
779 static void free_cpu_topo(struct cpu_topo
*tp
)
786 for (i
= 0 ; i
< tp
->core_sib
; i
++)
787 free(tp
->core_siblings
[i
]);
789 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
790 free(tp
->thread_siblings
[i
]);
795 static struct cpu_topo
*build_cpu_topology(void)
804 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
808 nr
= (u32
)(ncpus
& UINT_MAX
);
810 sz
= nr
* sizeof(char *);
812 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
819 tp
->core_siblings
= addr
;
821 tp
->thread_siblings
= addr
;
823 for (i
= 0; i
< nr
; i
++) {
824 ret
= build_cpu_topo(tp
, i
);
835 static int write_cpu_topology(int fd
, struct perf_header
*h __used
,
836 struct perf_evlist
*evlist __used
)
842 tp
= build_cpu_topology();
846 ret
= do_write(fd
, &tp
->core_sib
, sizeof(tp
->core_sib
));
850 for (i
= 0; i
< tp
->core_sib
; i
++) {
851 ret
= do_write_string(fd
, tp
->core_siblings
[i
]);
855 ret
= do_write(fd
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
859 for (i
= 0; i
< tp
->thread_sib
; i
++) {
860 ret
= do_write_string(fd
, tp
->thread_siblings
[i
]);
871 static int write_total_mem(int fd
, struct perf_header
*h __used
,
872 struct perf_evlist
*evlist __used
)
880 fp
= fopen("/proc/meminfo", "r");
884 while (getline(&buf
, &len
, fp
) > 0) {
885 ret
= strncmp(buf
, "MemTotal:", 9);
890 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
892 ret
= do_write(fd
, &mem
, sizeof(mem
));
899 static int write_topo_node(int fd
, int node
)
901 char str
[MAXPATHLEN
];
903 char *buf
= NULL
, *p
;
906 u64 mem_total
, mem_free
, mem
;
909 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
910 fp
= fopen(str
, "r");
914 while (getline(&buf
, &len
, fp
) > 0) {
915 /* skip over invalid lines */
916 if (!strchr(buf
, ':'))
918 if (sscanf(buf
, "%*s %*d %s %"PRIu64
, field
, &mem
) != 2)
920 if (!strcmp(field
, "MemTotal:"))
922 if (!strcmp(field
, "MemFree:"))
928 ret
= do_write(fd
, &mem_total
, sizeof(u64
));
932 ret
= do_write(fd
, &mem_free
, sizeof(u64
));
937 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
939 fp
= fopen(str
, "r");
943 if (getline(&buf
, &len
, fp
) <= 0)
946 p
= strchr(buf
, '\n');
950 ret
= do_write_string(fd
, buf
);
957 static int write_numa_topology(int fd
, struct perf_header
*h __used
,
958 struct perf_evlist
*evlist __used
)
963 struct cpu_map
*node_map
= NULL
;
968 fp
= fopen("/sys/devices/system/node/online", "r");
972 if (getline(&buf
, &len
, fp
) <= 0)
975 c
= strchr(buf
, '\n');
979 node_map
= cpu_map__new(buf
);
983 nr
= (u32
)node_map
->nr
;
985 ret
= do_write(fd
, &nr
, sizeof(nr
));
989 for (i
= 0; i
< nr
; i
++) {
990 j
= (u32
)node_map
->map
[i
];
991 ret
= do_write(fd
, &j
, sizeof(j
));
995 ret
= write_topo_node(fd
, i
);
1007 * default get_cpuid(): nothing gets recorded
1008 * actual implementation must be in arch/$(ARCH)/util/header.c
1010 int __attribute__((weak
)) get_cpuid(char *buffer __used
, size_t sz __used
)
1015 static int write_cpuid(int fd
, struct perf_header
*h __used
,
1016 struct perf_evlist
*evlist __used
)
1021 ret
= get_cpuid(buffer
, sizeof(buffer
));
1027 return do_write_string(fd
, buffer
);
1030 static int write_branch_stack(int fd __used
, struct perf_header
*h __used
,
1031 struct perf_evlist
*evlist __used
)
1036 static void print_hostname(struct perf_header
*ph
, int fd
, FILE *fp
)
1038 char *str
= do_read_string(fd
, ph
);
1039 fprintf(fp
, "# hostname : %s\n", str
);
1043 static void print_osrelease(struct perf_header
*ph
, int fd
, FILE *fp
)
1045 char *str
= do_read_string(fd
, ph
);
1046 fprintf(fp
, "# os release : %s\n", str
);
1050 static void print_arch(struct perf_header
*ph
, int fd
, FILE *fp
)
1052 char *str
= do_read_string(fd
, ph
);
1053 fprintf(fp
, "# arch : %s\n", str
);
1057 static void print_cpudesc(struct perf_header
*ph
, int fd
, FILE *fp
)
1059 char *str
= do_read_string(fd
, ph
);
1060 fprintf(fp
, "# cpudesc : %s\n", str
);
1064 static void print_nrcpus(struct perf_header
*ph
, int fd
, FILE *fp
)
1069 ret
= read(fd
, &nr
, sizeof(nr
));
1070 if (ret
!= (ssize_t
)sizeof(nr
))
1071 nr
= -1; /* interpreted as error */
1076 fprintf(fp
, "# nrcpus online : %u\n", nr
);
1078 ret
= read(fd
, &nr
, sizeof(nr
));
1079 if (ret
!= (ssize_t
)sizeof(nr
))
1080 nr
= -1; /* interpreted as error */
1085 fprintf(fp
, "# nrcpus avail : %u\n", nr
);
1088 static void print_version(struct perf_header
*ph
, int fd
, FILE *fp
)
1090 char *str
= do_read_string(fd
, ph
);
1091 fprintf(fp
, "# perf version : %s\n", str
);
1095 static void print_cmdline(struct perf_header
*ph
, int fd
, FILE *fp
)
1101 ret
= read(fd
, &nr
, sizeof(nr
));
1102 if (ret
!= (ssize_t
)sizeof(nr
))
1108 fprintf(fp
, "# cmdline : ");
1110 for (i
= 0; i
< nr
; i
++) {
1111 str
= do_read_string(fd
, ph
);
1112 fprintf(fp
, "%s ", str
);
1118 static void print_cpu_topology(struct perf_header
*ph
, int fd
, FILE *fp
)
1124 ret
= read(fd
, &nr
, sizeof(nr
));
1125 if (ret
!= (ssize_t
)sizeof(nr
))
1131 for (i
= 0; i
< nr
; i
++) {
1132 str
= do_read_string(fd
, ph
);
1133 fprintf(fp
, "# sibling cores : %s\n", str
);
1137 ret
= read(fd
, &nr
, sizeof(nr
));
1138 if (ret
!= (ssize_t
)sizeof(nr
))
1144 for (i
= 0; i
< nr
; i
++) {
1145 str
= do_read_string(fd
, ph
);
1146 fprintf(fp
, "# sibling threads : %s\n", str
);
1151 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1153 struct perf_event_attr attr
;
1157 u32 nre
, sz
, nr
, i
, j
;
1161 /* number of events */
1162 ret
= read(fd
, &nre
, sizeof(nre
));
1163 if (ret
!= (ssize_t
)sizeof(nre
))
1167 nre
= bswap_32(nre
);
1169 ret
= read(fd
, &sz
, sizeof(sz
));
1170 if (ret
!= (ssize_t
)sizeof(sz
))
1176 memset(&attr
, 0, sizeof(attr
));
1178 /* buffer to hold on file attr struct */
1187 for (i
= 0 ; i
< nre
; i
++) {
1190 * must read entire on-file attr struct to
1191 * sync up with layout.
1193 ret
= read(fd
, buf
, sz
);
1194 if (ret
!= (ssize_t
)sz
)
1198 perf_event__attr_swap(buf
);
1200 memcpy(&attr
, buf
, msz
);
1202 ret
= read(fd
, &nr
, sizeof(nr
));
1203 if (ret
!= (ssize_t
)sizeof(nr
))
1209 str
= do_read_string(fd
, ph
);
1210 fprintf(fp
, "# event : name = %s, ", str
);
1213 fprintf(fp
, "type = %d, config = 0x%"PRIx64
1214 ", config1 = 0x%"PRIx64
", config2 = 0x%"PRIx64
,
1220 fprintf(fp
, ", excl_usr = %d, excl_kern = %d",
1222 attr
.exclude_kernel
);
1224 fprintf(fp
, ", excl_host = %d, excl_guest = %d",
1226 attr
.exclude_guest
);
1228 fprintf(fp
, ", precise_ip = %d", attr
.precise_ip
);
1231 fprintf(fp
, ", id = {");
1233 for (j
= 0 ; j
< nr
; j
++) {
1234 ret
= read(fd
, &id
, sizeof(id
));
1235 if (ret
!= (ssize_t
)sizeof(id
))
1244 fprintf(fp
, " %"PRIu64
, id
);
1253 fprintf(fp
, "# event desc: not available or unable to read\n");
1256 static void print_total_mem(struct perf_header
*h __used
, int fd
, FILE *fp
)
1261 ret
= read(fd
, &mem
, sizeof(mem
));
1262 if (ret
!= sizeof(mem
))
1266 mem
= bswap_64(mem
);
1268 fprintf(fp
, "# total memory : %"PRIu64
" kB\n", mem
);
1271 fprintf(fp
, "# total memory : unknown\n");
1274 static void print_numa_topology(struct perf_header
*h __used
, int fd
, FILE *fp
)
1279 uint64_t mem_total
, mem_free
;
1282 ret
= read(fd
, &nr
, sizeof(nr
));
1283 if (ret
!= (ssize_t
)sizeof(nr
))
1289 for (i
= 0; i
< nr
; i
++) {
1292 ret
= read(fd
, &c
, sizeof(c
));
1293 if (ret
!= (ssize_t
)sizeof(c
))
1299 ret
= read(fd
, &mem_total
, sizeof(u64
));
1300 if (ret
!= sizeof(u64
))
1303 ret
= read(fd
, &mem_free
, sizeof(u64
));
1304 if (ret
!= sizeof(u64
))
1307 if (h
->needs_swap
) {
1308 mem_total
= bswap_64(mem_total
);
1309 mem_free
= bswap_64(mem_free
);
1312 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1313 " free = %"PRIu64
" kB\n",
1318 str
= do_read_string(fd
, h
);
1319 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1324 fprintf(fp
, "# numa topology : not available\n");
1327 static void print_cpuid(struct perf_header
*ph
, int fd
, FILE *fp
)
1329 char *str
= do_read_string(fd
, ph
);
1330 fprintf(fp
, "# cpuid : %s\n", str
);
1334 static void print_branch_stack(struct perf_header
*ph __used
, int fd __used
,
1337 fprintf(fp
, "# contains samples with branch stack\n");
1340 static int __event_process_build_id(struct build_id_event
*bev
,
1342 struct perf_session
*session
)
1345 struct list_head
*head
;
1346 struct machine
*machine
;
1349 enum dso_kernel_type dso_type
;
1351 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1355 misc
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1358 case PERF_RECORD_MISC_KERNEL
:
1359 dso_type
= DSO_TYPE_KERNEL
;
1360 head
= &machine
->kernel_dsos
;
1362 case PERF_RECORD_MISC_GUEST_KERNEL
:
1363 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1364 head
= &machine
->kernel_dsos
;
1366 case PERF_RECORD_MISC_USER
:
1367 case PERF_RECORD_MISC_GUEST_USER
:
1368 dso_type
= DSO_TYPE_USER
;
1369 head
= &machine
->user_dsos
;
1375 dso
= __dsos__findnew(head
, filename
);
1377 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1379 dso__set_build_id(dso
, &bev
->build_id
);
1381 if (filename
[0] == '[')
1382 dso
->kernel
= dso_type
;
1384 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1386 pr_debug("build id event received for %s: %s\n",
1387 dso
->long_name
, sbuild_id
);
1395 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1396 int input
, u64 offset
, u64 size
)
1398 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1400 struct perf_event_header header
;
1401 u8 build_id
[ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1404 struct build_id_event bev
;
1405 char filename
[PATH_MAX
];
1406 u64 limit
= offset
+ size
;
1408 while (offset
< limit
) {
1411 if (read(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1414 if (header
->needs_swap
)
1415 perf_event_header__bswap(&old_bev
.header
);
1417 len
= old_bev
.header
.size
- sizeof(old_bev
);
1418 if (read(input
, filename
, len
) != len
)
1421 bev
.header
= old_bev
.header
;
1424 * As the pid is the missing value, we need to fill
1425 * it properly. The header.misc value give us nice hint.
1427 bev
.pid
= HOST_KERNEL_ID
;
1428 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1429 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1430 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1432 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1433 __event_process_build_id(&bev
, filename
, session
);
1435 offset
+= bev
.header
.size
;
1441 static int perf_header__read_build_ids(struct perf_header
*header
,
1442 int input
, u64 offset
, u64 size
)
1444 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1445 struct build_id_event bev
;
1446 char filename
[PATH_MAX
];
1447 u64 limit
= offset
+ size
, orig_offset
= offset
;
1450 while (offset
< limit
) {
1453 if (read(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1456 if (header
->needs_swap
)
1457 perf_event_header__bswap(&bev
.header
);
1459 len
= bev
.header
.size
- sizeof(bev
);
1460 if (read(input
, filename
, len
) != len
)
1463 * The a1645ce1 changeset:
1465 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1467 * Added a field to struct build_id_event that broke the file
1470 * Since the kernel build-id is the first entry, process the
1471 * table using the old format if the well known
1472 * '[kernel.kallsyms]' string for the kernel build-id has the
1473 * first 4 characters chopped off (where the pid_t sits).
1475 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1476 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1478 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1481 __event_process_build_id(&bev
, filename
, session
);
1483 offset
+= bev
.header
.size
;
1490 static int process_tracing_data(struct perf_file_section
*section __unused
,
1491 struct perf_header
*ph __unused
,
1492 int feat __unused
, int fd
, void *data
)
1494 trace_report(fd
, data
, false);
1498 static int process_build_id(struct perf_file_section
*section
,
1499 struct perf_header
*ph
,
1500 int feat __unused
, int fd
, void *data __used
)
1502 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1503 pr_debug("Failed to read buildids, continuing...\n");
1507 struct feature_ops
{
1508 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
1509 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
1510 int (*process
)(struct perf_file_section
*section
,
1511 struct perf_header
*h
, int feat
, int fd
, void *data
);
1516 #define FEAT_OPA(n, func) \
1517 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1518 #define FEAT_OPP(n, func) \
1519 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1520 .process = process_##func }
1521 #define FEAT_OPF(n, func) \
1522 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1525 /* feature_ops not implemented: */
1526 #define print_tracing_data NULL
1527 #define print_build_id NULL
1529 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
1530 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
1531 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
1532 FEAT_OPA(HEADER_HOSTNAME
, hostname
),
1533 FEAT_OPA(HEADER_OSRELEASE
, osrelease
),
1534 FEAT_OPA(HEADER_VERSION
, version
),
1535 FEAT_OPA(HEADER_ARCH
, arch
),
1536 FEAT_OPA(HEADER_NRCPUS
, nrcpus
),
1537 FEAT_OPA(HEADER_CPUDESC
, cpudesc
),
1538 FEAT_OPA(HEADER_CPUID
, cpuid
),
1539 FEAT_OPA(HEADER_TOTAL_MEM
, total_mem
),
1540 FEAT_OPA(HEADER_EVENT_DESC
, event_desc
),
1541 FEAT_OPA(HEADER_CMDLINE
, cmdline
),
1542 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
1543 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
1544 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
1547 struct header_print_data
{
1549 bool full
; /* extended list of headers */
1552 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
1553 struct perf_header
*ph
,
1554 int feat
, int fd
, void *data
)
1556 struct header_print_data
*hd
= data
;
1558 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
1559 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
1560 "%d, continuing...\n", section
->offset
, feat
);
1563 if (feat
>= HEADER_LAST_FEATURE
) {
1564 pr_warning("unknown feature %d\n", feat
);
1567 if (!feat_ops
[feat
].print
)
1570 if (!feat_ops
[feat
].full_only
|| hd
->full
)
1571 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
1573 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
1574 feat_ops
[feat
].name
);
1579 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
1581 struct header_print_data hd
;
1582 struct perf_header
*header
= &session
->header
;
1583 int fd
= session
->fd
;
1587 perf_header__process_sections(header
, fd
, &hd
,
1588 perf_file_section__fprintf_info
);
1592 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
1593 struct perf_file_section
**p
,
1594 struct perf_evlist
*evlist
)
1599 if (perf_header__has_feat(h
, type
)) {
1600 if (!feat_ops
[type
].write
)
1603 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
1605 err
= feat_ops
[type
].write(fd
, h
, evlist
);
1607 pr_debug("failed to write feature %d\n", type
);
1609 /* undo anything written */
1610 lseek(fd
, (*p
)->offset
, SEEK_SET
);
1614 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
1620 static int perf_header__adds_write(struct perf_header
*header
,
1621 struct perf_evlist
*evlist
, int fd
)
1624 struct perf_file_section
*feat_sec
, *p
;
1630 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
1634 feat_sec
= p
= calloc(sizeof(*feat_sec
), nr_sections
);
1635 if (feat_sec
== NULL
)
1638 sec_size
= sizeof(*feat_sec
) * nr_sections
;
1640 sec_start
= header
->data_offset
+ header
->data_size
;
1641 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
1643 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
1644 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
1645 perf_header__clear_feat(header
, feat
);
1648 lseek(fd
, sec_start
, SEEK_SET
);
1650 * may write more than needed due to dropped feature, but
1651 * this is okay, reader will skip the mising entries
1653 err
= do_write(fd
, feat_sec
, sec_size
);
1655 pr_debug("failed to write feature section\n");
1660 int perf_header__write_pipe(int fd
)
1662 struct perf_pipe_file_header f_header
;
1665 f_header
= (struct perf_pipe_file_header
){
1666 .magic
= PERF_MAGIC
,
1667 .size
= sizeof(f_header
),
1670 err
= do_write(fd
, &f_header
, sizeof(f_header
));
1672 pr_debug("failed to write perf pipe header\n");
1679 int perf_session__write_header(struct perf_session
*session
,
1680 struct perf_evlist
*evlist
,
1681 int fd
, bool at_exit
)
1683 struct perf_file_header f_header
;
1684 struct perf_file_attr f_attr
;
1685 struct perf_header
*header
= &session
->header
;
1686 struct perf_evsel
*attr
, *pair
= NULL
;
1689 lseek(fd
, sizeof(f_header
), SEEK_SET
);
1691 if (session
->evlist
!= evlist
)
1692 pair
= list_entry(session
->evlist
->entries
.next
, struct perf_evsel
, node
);
1694 list_for_each_entry(attr
, &evlist
->entries
, node
) {
1695 attr
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
1696 err
= do_write(fd
, attr
->id
, attr
->ids
* sizeof(u64
));
1699 pr_debug("failed to write perf header\n");
1702 if (session
->evlist
!= evlist
) {
1703 err
= do_write(fd
, pair
->id
, pair
->ids
* sizeof(u64
));
1706 attr
->ids
+= pair
->ids
;
1707 pair
= list_entry(pair
->node
.next
, struct perf_evsel
, node
);
1711 header
->attr_offset
= lseek(fd
, 0, SEEK_CUR
);
1713 list_for_each_entry(attr
, &evlist
->entries
, node
) {
1714 f_attr
= (struct perf_file_attr
){
1717 .offset
= attr
->id_offset
,
1718 .size
= attr
->ids
* sizeof(u64
),
1721 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
1723 pr_debug("failed to write perf header attribute\n");
1728 header
->event_offset
= lseek(fd
, 0, SEEK_CUR
);
1729 header
->event_size
= event_count
* sizeof(struct perf_trace_event_type
);
1731 err
= do_write(fd
, events
, header
->event_size
);
1733 pr_debug("failed to write perf header events\n");
1738 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
1741 err
= perf_header__adds_write(header
, evlist
, fd
);
1746 f_header
= (struct perf_file_header
){
1747 .magic
= PERF_MAGIC
,
1748 .size
= sizeof(f_header
),
1749 .attr_size
= sizeof(f_attr
),
1751 .offset
= header
->attr_offset
,
1752 .size
= evlist
->nr_entries
* sizeof(f_attr
),
1755 .offset
= header
->data_offset
,
1756 .size
= header
->data_size
,
1759 .offset
= header
->event_offset
,
1760 .size
= header
->event_size
,
1764 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
1766 lseek(fd
, 0, SEEK_SET
);
1767 err
= do_write(fd
, &f_header
, sizeof(f_header
));
1769 pr_debug("failed to write perf header\n");
1772 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
1778 static int perf_header__getbuffer64(struct perf_header
*header
,
1779 int fd
, void *buf
, size_t size
)
1781 if (readn(fd
, buf
, size
) <= 0)
1784 if (header
->needs_swap
)
1785 mem_bswap_64(buf
, size
);
1790 int perf_header__process_sections(struct perf_header
*header
, int fd
,
1792 int (*process
)(struct perf_file_section
*section
,
1793 struct perf_header
*ph
,
1794 int feat
, int fd
, void *data
))
1796 struct perf_file_section
*feat_sec
, *sec
;
1802 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
1806 feat_sec
= sec
= calloc(sizeof(*feat_sec
), nr_sections
);
1810 sec_size
= sizeof(*feat_sec
) * nr_sections
;
1812 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
1814 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
1818 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
1819 err
= process(sec
++, header
, feat
, fd
, data
);
1829 static const int attr_file_abi_sizes
[] = {
1830 [0] = PERF_ATTR_SIZE_VER0
,
1831 [1] = PERF_ATTR_SIZE_VER1
,
1836 * In the legacy file format, the magic number is not used to encode endianness.
1837 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
1838 * on ABI revisions, we need to try all combinations for all endianness to
1839 * detect the endianness.
1841 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
1843 uint64_t ref_size
, attr_size
;
1846 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
1847 ref_size
= attr_file_abi_sizes
[i
]
1848 + sizeof(struct perf_file_section
);
1849 if (hdr_sz
!= ref_size
) {
1850 attr_size
= bswap_64(hdr_sz
);
1851 if (attr_size
!= ref_size
)
1854 ph
->needs_swap
= true;
1856 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
1861 /* could not determine endianness */
1865 #define PERF_PIPE_HDR_VER0 16
1867 static const size_t attr_pipe_abi_sizes
[] = {
1868 [0] = PERF_PIPE_HDR_VER0
,
1873 * In the legacy pipe format, there is an implicit assumption that endiannesss
1874 * between host recording the samples, and host parsing the samples is the
1875 * same. This is not always the case given that the pipe output may always be
1876 * redirected into a file and analyzed on a different machine with possibly a
1877 * different endianness and perf_event ABI revsions in the perf tool itself.
1879 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
1884 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
1885 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
1886 attr_size
= bswap_64(hdr_sz
);
1887 if (attr_size
!= hdr_sz
)
1890 ph
->needs_swap
= true;
1892 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
1898 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
1899 bool is_pipe
, struct perf_header
*ph
)
1903 /* check for legacy format */
1904 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
1906 pr_debug("legacy perf.data format\n");
1908 return try_all_pipe_abis(hdr_sz
, ph
);
1910 return try_all_file_abis(hdr_sz
, ph
);
1913 * the new magic number serves two purposes:
1914 * - unique number to identify actual perf.data files
1915 * - encode endianness of file
1918 /* check magic number with one endianness */
1919 if (magic
== __perf_magic2
)
1922 /* check magic number with opposite endianness */
1923 if (magic
!= __perf_magic2_sw
)
1926 ph
->needs_swap
= true;
1931 int perf_file_header__read(struct perf_file_header
*header
,
1932 struct perf_header
*ph
, int fd
)
1936 lseek(fd
, 0, SEEK_SET
);
1938 ret
= readn(fd
, header
, sizeof(*header
));
1942 if (check_magic_endian(header
->magic
,
1943 header
->attr_size
, false, ph
) < 0) {
1944 pr_debug("magic/endian check failed\n");
1948 if (ph
->needs_swap
) {
1949 mem_bswap_64(header
, offsetof(struct perf_file_header
,
1953 if (header
->size
!= sizeof(*header
)) {
1954 /* Support the previous format */
1955 if (header
->size
== offsetof(typeof(*header
), adds_features
))
1956 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
1959 } else if (ph
->needs_swap
) {
1961 * feature bitmap is declared as an array of unsigned longs --
1962 * not good since its size can differ between the host that
1963 * generated the data file and the host analyzing the file.
1965 * We need to handle endianness, but we don't know the size of
1966 * the unsigned long where the file was generated. Take a best
1967 * guess at determining it: try 64-bit swap first (ie., file
1968 * created on a 64-bit host), and check if the hostname feature
1969 * bit is set (this feature bit is forced on as of fbe96f2).
1970 * If the bit is not, undo the 64-bit swap and try a 32-bit
1971 * swap. If the hostname bit is still not set (e.g., older data
1972 * file), punt and fallback to the original behavior --
1973 * clearing all feature bits and setting buildid.
1975 mem_bswap_64(&header
->adds_features
,
1976 BITS_TO_U64(HEADER_FEAT_BITS
));
1978 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
1980 mem_bswap_64(&header
->adds_features
,
1981 BITS_TO_U64(HEADER_FEAT_BITS
));
1984 mem_bswap_32(&header
->adds_features
,
1985 BITS_TO_U32(HEADER_FEAT_BITS
));
1988 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
1989 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
1990 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
1994 memcpy(&ph
->adds_features
, &header
->adds_features
,
1995 sizeof(ph
->adds_features
));
1997 ph
->event_offset
= header
->event_types
.offset
;
1998 ph
->event_size
= header
->event_types
.size
;
1999 ph
->data_offset
= header
->data
.offset
;
2000 ph
->data_size
= header
->data
.size
;
2004 static int perf_file_section__process(struct perf_file_section
*section
,
2005 struct perf_header
*ph
,
2006 int feat
, int fd
, void *data
)
2008 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2009 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2010 "%d, continuing...\n", section
->offset
, feat
);
2014 if (feat
>= HEADER_LAST_FEATURE
) {
2015 pr_debug("unknown feature %d, continuing...\n", feat
);
2019 if (!feat_ops
[feat
].process
)
2022 return feat_ops
[feat
].process(section
, ph
, feat
, fd
, data
);
2025 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2026 struct perf_header
*ph
, int fd
,
2031 ret
= readn(fd
, header
, sizeof(*header
));
2035 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2036 pr_debug("endian/magic failed\n");
2041 header
->size
= bswap_64(header
->size
);
2043 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2049 static int perf_header__read_pipe(struct perf_session
*session
, int fd
)
2051 struct perf_header
*header
= &session
->header
;
2052 struct perf_pipe_file_header f_header
;
2054 if (perf_file_header__read_pipe(&f_header
, header
, fd
,
2055 session
->repipe
) < 0) {
2056 pr_debug("incompatible file format\n");
2065 static int read_attr(int fd
, struct perf_header
*ph
,
2066 struct perf_file_attr
*f_attr
)
2068 struct perf_event_attr
*attr
= &f_attr
->attr
;
2070 size_t our_sz
= sizeof(f_attr
->attr
);
2073 memset(f_attr
, 0, sizeof(*f_attr
));
2075 /* read minimal guaranteed structure */
2076 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2078 pr_debug("cannot read %d bytes of header attr\n",
2079 PERF_ATTR_SIZE_VER0
);
2083 /* on file perf_event_attr size */
2091 sz
= PERF_ATTR_SIZE_VER0
;
2092 } else if (sz
> our_sz
) {
2093 pr_debug("file uses a more recent and unsupported ABI"
2094 " (%zu bytes extra)\n", sz
- our_sz
);
2097 /* what we have not yet read and that we know about */
2098 left
= sz
- PERF_ATTR_SIZE_VER0
;
2101 ptr
+= PERF_ATTR_SIZE_VER0
;
2103 ret
= readn(fd
, ptr
, left
);
2105 /* read perf_file_section, ids are read in caller */
2106 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2108 return ret
<= 0 ? -1 : 0;
2111 static int perf_evsel__set_tracepoint_name(struct perf_evsel
*evsel
,
2112 struct pevent
*pevent
)
2114 struct event_format
*event
= pevent_find_event(pevent
,
2115 evsel
->attr
.config
);
2121 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2122 evsel
->name
= strdup(bf
);
2123 if (event
->name
== NULL
)
2129 static int perf_evlist__set_tracepoint_names(struct perf_evlist
*evlist
,
2130 struct pevent
*pevent
)
2132 struct perf_evsel
*pos
;
2134 list_for_each_entry(pos
, &evlist
->entries
, node
) {
2135 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2136 perf_evsel__set_tracepoint_name(pos
, pevent
))
2143 int perf_session__read_header(struct perf_session
*session
, int fd
)
2145 struct perf_header
*header
= &session
->header
;
2146 struct perf_file_header f_header
;
2147 struct perf_file_attr f_attr
;
2149 int nr_attrs
, nr_ids
, i
, j
;
2151 session
->evlist
= perf_evlist__new(NULL
, NULL
);
2152 if (session
->evlist
== NULL
)
2155 if (session
->fd_pipe
)
2156 return perf_header__read_pipe(session
, fd
);
2158 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2161 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2162 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2164 for (i
= 0; i
< nr_attrs
; i
++) {
2165 struct perf_evsel
*evsel
;
2168 if (read_attr(fd
, header
, &f_attr
) < 0)
2171 if (header
->needs_swap
)
2172 perf_event__attr_swap(&f_attr
.attr
);
2174 tmp
= lseek(fd
, 0, SEEK_CUR
);
2175 evsel
= perf_evsel__new(&f_attr
.attr
, i
);
2178 goto out_delete_evlist
;
2180 * Do it before so that if perf_evsel__alloc_id fails, this
2181 * entry gets purged too at perf_evlist__delete().
2183 perf_evlist__add(session
->evlist
, evsel
);
2185 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2187 * We don't have the cpu and thread maps on the header, so
2188 * for allocating the perf_sample_id table we fake 1 cpu and
2189 * hattr->ids threads.
2191 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2192 goto out_delete_evlist
;
2194 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2196 for (j
= 0; j
< nr_ids
; j
++) {
2197 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2200 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2203 lseek(fd
, tmp
, SEEK_SET
);
2206 symbol_conf
.nr_events
= nr_attrs
;
2208 if (f_header
.event_types
.size
) {
2209 lseek(fd
, f_header
.event_types
.offset
, SEEK_SET
);
2210 events
= malloc(f_header
.event_types
.size
);
2213 if (perf_header__getbuffer64(header
, fd
, events
,
2214 f_header
.event_types
.size
))
2216 event_count
= f_header
.event_types
.size
/ sizeof(struct perf_trace_event_type
);
2219 perf_header__process_sections(header
, fd
, &session
->pevent
,
2220 perf_file_section__process
);
2222 lseek(fd
, header
->data_offset
, SEEK_SET
);
2224 if (perf_evlist__set_tracepoint_names(session
->evlist
, session
->pevent
))
2225 goto out_delete_evlist
;
2233 perf_evlist__delete(session
->evlist
);
2234 session
->evlist
= NULL
;
2238 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2239 struct perf_event_attr
*attr
, u16 ids
, u64
*id
,
2240 perf_event__handler_t process
)
2242 union perf_event
*ev
;
2246 size
= sizeof(struct perf_event_attr
);
2247 size
= ALIGN(size
, sizeof(u64
));
2248 size
+= sizeof(struct perf_event_header
);
2249 size
+= ids
* sizeof(u64
);
2256 ev
->attr
.attr
= *attr
;
2257 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2259 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2260 ev
->attr
.header
.size
= size
;
2262 err
= process(tool
, ev
, NULL
, NULL
);
2269 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2270 struct perf_session
*session
,
2271 perf_event__handler_t process
)
2273 struct perf_evsel
*attr
;
2276 list_for_each_entry(attr
, &session
->evlist
->entries
, node
) {
2277 err
= perf_event__synthesize_attr(tool
, &attr
->attr
, attr
->ids
,
2280 pr_debug("failed to create perf header attribute\n");
2288 int perf_event__process_attr(union perf_event
*event
,
2289 struct perf_evlist
**pevlist
)
2291 unsigned int i
, ids
, n_ids
;
2292 struct perf_evsel
*evsel
;
2293 struct perf_evlist
*evlist
= *pevlist
;
2295 if (evlist
== NULL
) {
2296 *pevlist
= evlist
= perf_evlist__new(NULL
, NULL
);
2301 evsel
= perf_evsel__new(&event
->attr
.attr
, evlist
->nr_entries
);
2305 perf_evlist__add(evlist
, evsel
);
2307 ids
= event
->header
.size
;
2308 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2309 n_ids
= ids
/ sizeof(u64
);
2311 * We don't have the cpu and thread maps on the header, so
2312 * for allocating the perf_sample_id table we fake 1 cpu and
2313 * hattr->ids threads.
2315 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2318 for (i
= 0; i
< n_ids
; i
++) {
2319 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2325 int perf_event__synthesize_event_type(struct perf_tool
*tool
,
2326 u64 event_id
, char *name
,
2327 perf_event__handler_t process
,
2328 struct machine
*machine
)
2330 union perf_event ev
;
2334 memset(&ev
, 0, sizeof(ev
));
2336 ev
.event_type
.event_type
.event_id
= event_id
;
2337 memset(ev
.event_type
.event_type
.name
, 0, MAX_EVENT_NAME
);
2338 strncpy(ev
.event_type
.event_type
.name
, name
, MAX_EVENT_NAME
- 1);
2340 ev
.event_type
.header
.type
= PERF_RECORD_HEADER_EVENT_TYPE
;
2341 size
= strlen(ev
.event_type
.event_type
.name
);
2342 size
= ALIGN(size
, sizeof(u64
));
2343 ev
.event_type
.header
.size
= sizeof(ev
.event_type
) -
2344 (sizeof(ev
.event_type
.event_type
.name
) - size
);
2346 err
= process(tool
, &ev
, NULL
, machine
);
2351 int perf_event__synthesize_event_types(struct perf_tool
*tool
,
2352 perf_event__handler_t process
,
2353 struct machine
*machine
)
2355 struct perf_trace_event_type
*type
;
2358 for (i
= 0; i
< event_count
; i
++) {
2361 err
= perf_event__synthesize_event_type(tool
, type
->event_id
,
2362 type
->name
, process
,
2365 pr_debug("failed to create perf header event type\n");
2373 int perf_event__process_event_type(struct perf_tool
*tool __unused
,
2374 union perf_event
*event
)
2376 if (perf_header__push_event(event
->event_type
.event_type
.event_id
,
2377 event
->event_type
.event_type
.name
) < 0)
2383 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
2384 struct perf_evlist
*evlist
,
2385 perf_event__handler_t process
)
2387 union perf_event ev
;
2388 struct tracing_data
*tdata
;
2389 ssize_t size
= 0, aligned_size
= 0, padding
;
2393 * We are going to store the size of the data followed
2394 * by the data contents. Since the fd descriptor is a pipe,
2395 * we cannot seek back to store the size of the data once
2396 * we know it. Instead we:
2398 * - write the tracing data to the temp file
2399 * - get/write the data size to pipe
2400 * - write the tracing data from the temp file
2403 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
2407 memset(&ev
, 0, sizeof(ev
));
2409 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
2411 aligned_size
= ALIGN(size
, sizeof(u64
));
2412 padding
= aligned_size
- size
;
2413 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
2414 ev
.tracing_data
.size
= aligned_size
;
2416 process(tool
, &ev
, NULL
, NULL
);
2419 * The put function will copy all the tracing data
2420 * stored in temp file to the pipe.
2422 tracing_data_put(tdata
);
2424 write_padded(fd
, NULL
, 0, padding
);
2426 return aligned_size
;
2429 int perf_event__process_tracing_data(union perf_event
*event
,
2430 struct perf_session
*session
)
2432 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
2433 off_t offset
= lseek(session
->fd
, 0, SEEK_CUR
);
2436 /* setup for reading amidst mmap */
2437 lseek(session
->fd
, offset
+ sizeof(struct tracing_data_event
),
2440 size_read
= trace_report(session
->fd
, &session
->pevent
,
2442 padding
= ALIGN(size_read
, sizeof(u64
)) - size_read
;
2444 if (read(session
->fd
, buf
, padding
) < 0)
2445 die("reading input file");
2446 if (session
->repipe
) {
2447 int retw
= write(STDOUT_FILENO
, buf
, padding
);
2448 if (retw
<= 0 || retw
!= padding
)
2449 die("repiping tracing data padding");
2452 if (size_read
+ padding
!= size
)
2453 die("tracing data size mismatch");
2455 return size_read
+ padding
;
2458 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
2459 struct dso
*pos
, u16 misc
,
2460 perf_event__handler_t process
,
2461 struct machine
*machine
)
2463 union perf_event ev
;
2470 memset(&ev
, 0, sizeof(ev
));
2472 len
= pos
->long_name_len
+ 1;
2473 len
= ALIGN(len
, NAME_ALIGN
);
2474 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
2475 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
2476 ev
.build_id
.header
.misc
= misc
;
2477 ev
.build_id
.pid
= machine
->pid
;
2478 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
2479 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
2481 err
= process(tool
, &ev
, NULL
, machine
);
2486 int perf_event__process_build_id(struct perf_tool
*tool __used
,
2487 union perf_event
*event
,
2488 struct perf_session
*session
)
2490 __event_process_build_id(&event
->build_id
,
2491 event
->build_id
.filename
,
2496 void disable_buildid_cache(void)
2498 no_buildid_cache
= true;