1 #define _FILE_OFFSET_BITS 64
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/bitops.h>
12 #include <sys/utsname.h>
18 #include "trace-event.h"
25 static bool no_buildid_cache
= false;
27 static int trace_event_count
;
28 static struct perf_trace_event_type
*trace_events
;
30 static u32 header_argc
;
31 static const char **header_argv
;
33 int perf_header__push_event(u64 id
, const char *name
)
35 struct perf_trace_event_type
*nevents
;
37 if (strlen(name
) > MAX_EVENT_NAME
)
38 pr_warning("Event %s will be truncated\n", name
);
40 nevents
= realloc(trace_events
, (trace_event_count
+ 1) * sizeof(*trace_events
));
43 trace_events
= nevents
;
45 memset(&trace_events
[trace_event_count
], 0, sizeof(struct perf_trace_event_type
));
46 trace_events
[trace_event_count
].event_id
= id
;
47 strncpy(trace_events
[trace_event_count
].name
, name
, MAX_EVENT_NAME
- 1);
52 char *perf_header__find_event(u64 id
)
55 for (i
= 0 ; i
< trace_event_count
; i
++) {
56 if (trace_events
[i
].event_id
== id
)
57 return trace_events
[i
].name
;
64 * must be a numerical value to let the endianness
65 * determine the memory layout. That way we are able
66 * to detect endianness when reading the perf.data file
69 * we check for legacy (PERFFILE) format.
71 static const char *__perf_magic1
= "PERFFILE";
72 static const u64 __perf_magic2
= 0x32454c4946524550ULL
;
73 static const u64 __perf_magic2_sw
= 0x50455246494c4532ULL
;
75 #define PERF_MAGIC __perf_magic2
77 struct perf_file_attr
{
78 struct perf_event_attr attr
;
79 struct perf_file_section ids
;
82 void perf_header__set_feat(struct perf_header
*header
, int feat
)
84 set_bit(feat
, header
->adds_features
);
87 void perf_header__clear_feat(struct perf_header
*header
, int feat
)
89 clear_bit(feat
, header
->adds_features
);
92 bool perf_header__has_feat(const struct perf_header
*header
, int feat
)
94 return test_bit(feat
, header
->adds_features
);
97 static int do_write(int fd
, const void *buf
, size_t size
)
100 int ret
= write(fd
, buf
, size
);
112 #define NAME_ALIGN 64
114 static int write_padded(int fd
, const void *bf
, size_t count
,
115 size_t count_aligned
)
117 static const char zero_buf
[NAME_ALIGN
];
118 int err
= do_write(fd
, bf
, count
);
121 err
= do_write(fd
, zero_buf
, count_aligned
- count
);
126 static int do_write_string(int fd
, const char *str
)
131 olen
= strlen(str
) + 1;
132 len
= ALIGN(olen
, NAME_ALIGN
);
134 /* write len, incl. \0 */
135 ret
= do_write(fd
, &len
, sizeof(len
));
139 return write_padded(fd
, str
, olen
, len
);
142 static char *do_read_string(int fd
, struct perf_header
*ph
)
148 sz
= read(fd
, &len
, sizeof(len
));
149 if (sz
< (ssize_t
)sizeof(len
))
159 ret
= read(fd
, buf
, len
);
160 if (ret
== (ssize_t
)len
) {
162 * strings are padded by zeroes
163 * thus the actual strlen of buf
164 * may be less than len
174 perf_header__set_cmdline(int argc
, const char **argv
)
179 * If header_argv has already been set, do not override it.
180 * This allows a command to set the cmdline, parse args and
181 * then call another builtin function that implements a
182 * command -- e.g, cmd_kvm calling cmd_record.
187 header_argc
= (u32
)argc
;
189 /* do not include NULL termination */
190 header_argv
= calloc(argc
, sizeof(char *));
195 * must copy argv contents because it gets moved
196 * around during option parsing
198 for (i
= 0; i
< argc
; i
++)
199 header_argv
[i
] = argv
[i
];
204 #define dsos__for_each_with_build_id(pos, head) \
205 list_for_each_entry(pos, head, node) \
206 if (!pos->has_build_id) \
210 static int __dsos__write_buildid_table(struct list_head
*head
, pid_t pid
,
215 dsos__for_each_with_build_id(pos
, head
) {
217 struct build_id_event b
;
222 len
= pos
->long_name_len
+ 1;
223 len
= ALIGN(len
, NAME_ALIGN
);
224 memset(&b
, 0, sizeof(b
));
225 memcpy(&b
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
227 b
.header
.misc
= misc
;
228 b
.header
.size
= sizeof(b
) + len
;
229 err
= do_write(fd
, &b
, sizeof(b
));
232 err
= write_padded(fd
, pos
->long_name
,
233 pos
->long_name_len
+ 1, len
);
241 static int machine__write_buildid_table(struct machine
*machine
, int fd
)
244 u16 kmisc
= PERF_RECORD_MISC_KERNEL
,
245 umisc
= PERF_RECORD_MISC_USER
;
247 if (!machine__is_host(machine
)) {
248 kmisc
= PERF_RECORD_MISC_GUEST_KERNEL
;
249 umisc
= PERF_RECORD_MISC_GUEST_USER
;
252 err
= __dsos__write_buildid_table(&machine
->kernel_dsos
, machine
->pid
,
255 err
= __dsos__write_buildid_table(&machine
->user_dsos
,
256 machine
->pid
, umisc
, fd
);
260 static int dsos__write_buildid_table(struct perf_header
*header
, int fd
)
262 struct perf_session
*session
= container_of(header
,
263 struct perf_session
, header
);
265 int err
= machine__write_buildid_table(&session
->host_machine
, fd
);
270 for (nd
= rb_first(&session
->machines
); nd
; nd
= rb_next(nd
)) {
271 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
272 err
= machine__write_buildid_table(pos
, fd
);
279 int build_id_cache__add_s(const char *sbuild_id
, const char *debugdir
,
280 const char *name
, bool is_kallsyms
)
282 const size_t size
= PATH_MAX
;
283 char *realname
, *filename
= zalloc(size
),
284 *linkname
= zalloc(size
), *targetname
;
288 if (symbol_conf
.kptr_restrict
) {
289 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
292 realname
= (char *)name
;
294 realname
= realpath(name
, NULL
);
296 if (realname
== NULL
|| filename
== NULL
|| linkname
== NULL
)
299 len
= scnprintf(filename
, size
, "%s%s%s",
300 debugdir
, is_kallsyms
? "/" : "", realname
);
301 if (mkdir_p(filename
, 0755))
304 snprintf(filename
+ len
, size
- len
, "/%s", sbuild_id
);
306 if (access(filename
, F_OK
)) {
308 if (copyfile("/proc/kallsyms", filename
))
310 } else if (link(realname
, filename
) && copyfile(name
, filename
))
314 len
= scnprintf(linkname
, size
, "%s/.build-id/%.2s",
315 debugdir
, sbuild_id
);
317 if (access(linkname
, X_OK
) && mkdir_p(linkname
, 0755))
320 snprintf(linkname
+ len
, size
- len
, "/%s", sbuild_id
+ 2);
321 targetname
= filename
+ strlen(debugdir
) - 5;
322 memcpy(targetname
, "../..", 5);
324 if (symlink(targetname
, linkname
) == 0)
334 static int build_id_cache__add_b(const u8
*build_id
, size_t build_id_size
,
335 const char *name
, const char *debugdir
,
338 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
340 build_id__sprintf(build_id
, build_id_size
, sbuild_id
);
342 return build_id_cache__add_s(sbuild_id
, debugdir
, name
, is_kallsyms
);
345 int build_id_cache__remove_s(const char *sbuild_id
, const char *debugdir
)
347 const size_t size
= PATH_MAX
;
348 char *filename
= zalloc(size
),
349 *linkname
= zalloc(size
);
352 if (filename
== NULL
|| linkname
== NULL
)
355 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
356 debugdir
, sbuild_id
, sbuild_id
+ 2);
358 if (access(linkname
, F_OK
))
361 if (readlink(linkname
, filename
, size
- 1) < 0)
364 if (unlink(linkname
))
368 * Since the link is relative, we must make it absolute:
370 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
371 debugdir
, sbuild_id
, filename
);
373 if (unlink(linkname
))
383 static int dso__cache_build_id(struct dso
*dso
, const char *debugdir
)
385 bool is_kallsyms
= dso
->kernel
&& dso
->long_name
[0] != '/';
387 return build_id_cache__add_b(dso
->build_id
, sizeof(dso
->build_id
),
388 dso
->long_name
, debugdir
, is_kallsyms
);
391 static int __dsos__cache_build_ids(struct list_head
*head
, const char *debugdir
)
396 dsos__for_each_with_build_id(pos
, head
)
397 if (dso__cache_build_id(pos
, debugdir
))
403 static int machine__cache_build_ids(struct machine
*machine
, const char *debugdir
)
405 int ret
= __dsos__cache_build_ids(&machine
->kernel_dsos
, debugdir
);
406 ret
|= __dsos__cache_build_ids(&machine
->user_dsos
, debugdir
);
410 static int perf_session__cache_build_ids(struct perf_session
*session
)
414 char debugdir
[PATH_MAX
];
416 snprintf(debugdir
, sizeof(debugdir
), "%s", buildid_dir
);
418 if (mkdir(debugdir
, 0755) != 0 && errno
!= EEXIST
)
421 ret
= machine__cache_build_ids(&session
->host_machine
, debugdir
);
423 for (nd
= rb_first(&session
->machines
); nd
; nd
= rb_next(nd
)) {
424 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
425 ret
|= machine__cache_build_ids(pos
, debugdir
);
430 static bool machine__read_build_ids(struct machine
*machine
, bool with_hits
)
432 bool ret
= __dsos__read_build_ids(&machine
->kernel_dsos
, with_hits
);
433 ret
|= __dsos__read_build_ids(&machine
->user_dsos
, with_hits
);
437 static bool perf_session__read_build_ids(struct perf_session
*session
, bool with_hits
)
440 bool ret
= machine__read_build_ids(&session
->host_machine
, with_hits
);
442 for (nd
= rb_first(&session
->machines
); nd
; nd
= rb_next(nd
)) {
443 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
444 ret
|= machine__read_build_ids(pos
, with_hits
);
450 static int write_tracing_data(int fd
, struct perf_header
*h __used
,
451 struct perf_evlist
*evlist
)
453 return read_tracing_data(fd
, &evlist
->entries
);
457 static int write_build_id(int fd
, struct perf_header
*h
,
458 struct perf_evlist
*evlist __used
)
460 struct perf_session
*session
;
463 session
= container_of(h
, struct perf_session
, header
);
465 if (!perf_session__read_build_ids(session
, true))
468 err
= dsos__write_buildid_table(h
, fd
);
470 pr_debug("failed to write buildid table\n");
473 if (!no_buildid_cache
)
474 perf_session__cache_build_ids(session
);
479 static int write_hostname(int fd
, struct perf_header
*h __used
,
480 struct perf_evlist
*evlist __used
)
489 return do_write_string(fd
, uts
.nodename
);
492 static int write_osrelease(int fd
, struct perf_header
*h __used
,
493 struct perf_evlist
*evlist __used
)
502 return do_write_string(fd
, uts
.release
);
505 static int write_arch(int fd
, struct perf_header
*h __used
,
506 struct perf_evlist
*evlist __used
)
515 return do_write_string(fd
, uts
.machine
);
518 static int write_version(int fd
, struct perf_header
*h __used
,
519 struct perf_evlist
*evlist __used
)
521 return do_write_string(fd
, perf_version_string
);
524 static int write_cpudesc(int fd
, struct perf_header
*h __used
,
525 struct perf_evlist
*evlist __used
)
528 #define CPUINFO_PROC NULL
533 const char *search
= CPUINFO_PROC
;
540 file
= fopen("/proc/cpuinfo", "r");
544 while (getline(&buf
, &len
, file
) > 0) {
545 ret
= strncmp(buf
, search
, strlen(search
));
555 p
= strchr(buf
, ':');
556 if (p
&& *(p
+1) == ' ' && *(p
+2))
562 /* squash extra space characters (branding string) */
569 while (*q
&& isspace(*q
))
572 while ((*r
++ = *q
++));
576 ret
= do_write_string(fd
, s
);
583 static int write_nrcpus(int fd
, struct perf_header
*h __used
,
584 struct perf_evlist
*evlist __used
)
590 nr
= sysconf(_SC_NPROCESSORS_CONF
);
594 nrc
= (u32
)(nr
& UINT_MAX
);
596 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
600 nra
= (u32
)(nr
& UINT_MAX
);
602 ret
= do_write(fd
, &nrc
, sizeof(nrc
));
606 return do_write(fd
, &nra
, sizeof(nra
));
609 static int write_event_desc(int fd
, struct perf_header
*h __used
,
610 struct perf_evlist
*evlist
)
612 struct perf_evsel
*evsel
;
616 nre
= evlist
->nr_entries
;
619 * write number of events
621 ret
= do_write(fd
, &nre
, sizeof(nre
));
626 * size of perf_event_attr struct
628 sz
= (u32
)sizeof(evsel
->attr
);
629 ret
= do_write(fd
, &sz
, sizeof(sz
));
633 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
635 ret
= do_write(fd
, &evsel
->attr
, sz
);
639 * write number of unique id per event
640 * there is one id per instance of an event
642 * copy into an nri to be independent of the
646 ret
= do_write(fd
, &nri
, sizeof(nri
));
651 * write event string as passed on cmdline
653 ret
= do_write_string(fd
, perf_evsel__name(evsel
));
657 * write unique ids for this event
659 ret
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
666 static int write_cmdline(int fd
, struct perf_header
*h __used
,
667 struct perf_evlist
*evlist __used
)
669 char buf
[MAXPATHLEN
];
675 * actual atual path to perf binary
677 sprintf(proc
, "/proc/%d/exe", getpid());
678 ret
= readlink(proc
, buf
, sizeof(buf
));
682 /* readlink() does not add null termination */
685 /* account for binary path */
688 ret
= do_write(fd
, &n
, sizeof(n
));
692 ret
= do_write_string(fd
, buf
);
696 for (i
= 0 ; i
< header_argc
; i
++) {
697 ret
= do_write_string(fd
, header_argv
[i
]);
704 #define CORE_SIB_FMT \
705 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
706 #define THRD_SIB_FMT \
707 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
712 char **core_siblings
;
713 char **thread_siblings
;
716 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
719 char filename
[MAXPATHLEN
];
720 char *buf
= NULL
, *p
;
725 sprintf(filename
, CORE_SIB_FMT
, cpu
);
726 fp
= fopen(filename
, "r");
730 if (getline(&buf
, &len
, fp
) <= 0)
735 p
= strchr(buf
, '\n');
739 for (i
= 0; i
< tp
->core_sib
; i
++) {
740 if (!strcmp(buf
, tp
->core_siblings
[i
]))
743 if (i
== tp
->core_sib
) {
744 tp
->core_siblings
[i
] = buf
;
750 sprintf(filename
, THRD_SIB_FMT
, cpu
);
751 fp
= fopen(filename
, "r");
755 if (getline(&buf
, &len
, fp
) <= 0)
758 p
= strchr(buf
, '\n');
762 for (i
= 0; i
< tp
->thread_sib
; i
++) {
763 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
766 if (i
== tp
->thread_sib
) {
767 tp
->thread_siblings
[i
] = buf
;
779 static void free_cpu_topo(struct cpu_topo
*tp
)
786 for (i
= 0 ; i
< tp
->core_sib
; i
++)
787 free(tp
->core_siblings
[i
]);
789 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
790 free(tp
->thread_siblings
[i
]);
795 static struct cpu_topo
*build_cpu_topology(void)
804 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
808 nr
= (u32
)(ncpus
& UINT_MAX
);
810 sz
= nr
* sizeof(char *);
812 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
819 tp
->core_siblings
= addr
;
821 tp
->thread_siblings
= addr
;
823 for (i
= 0; i
< nr
; i
++) {
824 ret
= build_cpu_topo(tp
, i
);
835 static int write_cpu_topology(int fd
, struct perf_header
*h __used
,
836 struct perf_evlist
*evlist __used
)
842 tp
= build_cpu_topology();
846 ret
= do_write(fd
, &tp
->core_sib
, sizeof(tp
->core_sib
));
850 for (i
= 0; i
< tp
->core_sib
; i
++) {
851 ret
= do_write_string(fd
, tp
->core_siblings
[i
]);
855 ret
= do_write(fd
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
859 for (i
= 0; i
< tp
->thread_sib
; i
++) {
860 ret
= do_write_string(fd
, tp
->thread_siblings
[i
]);
871 static int write_total_mem(int fd
, struct perf_header
*h __used
,
872 struct perf_evlist
*evlist __used
)
880 fp
= fopen("/proc/meminfo", "r");
884 while (getline(&buf
, &len
, fp
) > 0) {
885 ret
= strncmp(buf
, "MemTotal:", 9);
890 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
892 ret
= do_write(fd
, &mem
, sizeof(mem
));
899 static int write_topo_node(int fd
, int node
)
901 char str
[MAXPATHLEN
];
903 char *buf
= NULL
, *p
;
906 u64 mem_total
, mem_free
, mem
;
909 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
910 fp
= fopen(str
, "r");
914 while (getline(&buf
, &len
, fp
) > 0) {
915 /* skip over invalid lines */
916 if (!strchr(buf
, ':'))
918 if (sscanf(buf
, "%*s %*d %s %"PRIu64
, field
, &mem
) != 2)
920 if (!strcmp(field
, "MemTotal:"))
922 if (!strcmp(field
, "MemFree:"))
928 ret
= do_write(fd
, &mem_total
, sizeof(u64
));
932 ret
= do_write(fd
, &mem_free
, sizeof(u64
));
937 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
939 fp
= fopen(str
, "r");
943 if (getline(&buf
, &len
, fp
) <= 0)
946 p
= strchr(buf
, '\n');
950 ret
= do_write_string(fd
, buf
);
957 static int write_numa_topology(int fd
, struct perf_header
*h __used
,
958 struct perf_evlist
*evlist __used
)
963 struct cpu_map
*node_map
= NULL
;
968 fp
= fopen("/sys/devices/system/node/online", "r");
972 if (getline(&buf
, &len
, fp
) <= 0)
975 c
= strchr(buf
, '\n');
979 node_map
= cpu_map__new(buf
);
983 nr
= (u32
)node_map
->nr
;
985 ret
= do_write(fd
, &nr
, sizeof(nr
));
989 for (i
= 0; i
< nr
; i
++) {
990 j
= (u32
)node_map
->map
[i
];
991 ret
= do_write(fd
, &j
, sizeof(j
));
995 ret
= write_topo_node(fd
, i
);
1009 * struct pmu_mappings {
1018 static int write_pmu_mappings(int fd
, struct perf_header
*h __used
,
1019 struct perf_evlist
*evlist __used
)
1021 struct perf_pmu
*pmu
= NULL
;
1022 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
1025 /* write real pmu_num later */
1026 do_write(fd
, &pmu_num
, sizeof(pmu_num
));
1028 while ((pmu
= perf_pmu__scan(pmu
))) {
1032 do_write(fd
, &pmu
->type
, sizeof(pmu
->type
));
1033 do_write_string(fd
, pmu
->name
);
1036 if (pwrite(fd
, &pmu_num
, sizeof(pmu_num
), offset
) != sizeof(pmu_num
)) {
1038 lseek(fd
, offset
, SEEK_SET
);
1046 * default get_cpuid(): nothing gets recorded
1047 * actual implementation must be in arch/$(ARCH)/util/header.c
1049 int __attribute__((weak
)) get_cpuid(char *buffer __used
, size_t sz __used
)
1054 static int write_cpuid(int fd
, struct perf_header
*h __used
,
1055 struct perf_evlist
*evlist __used
)
1060 ret
= get_cpuid(buffer
, sizeof(buffer
));
1066 return do_write_string(fd
, buffer
);
1069 static int write_branch_stack(int fd __used
, struct perf_header
*h __used
,
1070 struct perf_evlist
*evlist __used
)
1075 static void print_hostname(struct perf_header
*ph
, int fd
, FILE *fp
)
1077 char *str
= do_read_string(fd
, ph
);
1078 fprintf(fp
, "# hostname : %s\n", str
);
1082 static void print_osrelease(struct perf_header
*ph
, int fd
, FILE *fp
)
1084 char *str
= do_read_string(fd
, ph
);
1085 fprintf(fp
, "# os release : %s\n", str
);
1089 static void print_arch(struct perf_header
*ph
, int fd
, FILE *fp
)
1091 char *str
= do_read_string(fd
, ph
);
1092 fprintf(fp
, "# arch : %s\n", str
);
1096 static void print_cpudesc(struct perf_header
*ph
, int fd
, FILE *fp
)
1098 char *str
= do_read_string(fd
, ph
);
1099 fprintf(fp
, "# cpudesc : %s\n", str
);
1103 static void print_nrcpus(struct perf_header
*ph
, int fd
, FILE *fp
)
1108 ret
= read(fd
, &nr
, sizeof(nr
));
1109 if (ret
!= (ssize_t
)sizeof(nr
))
1110 nr
= -1; /* interpreted as error */
1115 fprintf(fp
, "# nrcpus online : %u\n", nr
);
1117 ret
= read(fd
, &nr
, sizeof(nr
));
1118 if (ret
!= (ssize_t
)sizeof(nr
))
1119 nr
= -1; /* interpreted as error */
1124 fprintf(fp
, "# nrcpus avail : %u\n", nr
);
1127 static void print_version(struct perf_header
*ph
, int fd
, FILE *fp
)
1129 char *str
= do_read_string(fd
, ph
);
1130 fprintf(fp
, "# perf version : %s\n", str
);
1134 static void print_cmdline(struct perf_header
*ph
, int fd
, FILE *fp
)
1140 ret
= read(fd
, &nr
, sizeof(nr
));
1141 if (ret
!= (ssize_t
)sizeof(nr
))
1147 fprintf(fp
, "# cmdline : ");
1149 for (i
= 0; i
< nr
; i
++) {
1150 str
= do_read_string(fd
, ph
);
1151 fprintf(fp
, "%s ", str
);
1157 static void print_cpu_topology(struct perf_header
*ph
, int fd
, FILE *fp
)
1163 ret
= read(fd
, &nr
, sizeof(nr
));
1164 if (ret
!= (ssize_t
)sizeof(nr
))
1170 for (i
= 0; i
< nr
; i
++) {
1171 str
= do_read_string(fd
, ph
);
1172 fprintf(fp
, "# sibling cores : %s\n", str
);
1176 ret
= read(fd
, &nr
, sizeof(nr
));
1177 if (ret
!= (ssize_t
)sizeof(nr
))
1183 for (i
= 0; i
< nr
; i
++) {
1184 str
= do_read_string(fd
, ph
);
1185 fprintf(fp
, "# sibling threads : %s\n", str
);
1190 static void free_event_desc(struct perf_evsel
*events
)
1192 struct perf_evsel
*evsel
;
1197 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1207 static struct perf_evsel
*
1208 read_event_desc(struct perf_header
*ph
, int fd
)
1210 struct perf_evsel
*evsel
, *events
= NULL
;
1213 u32 nre
, sz
, nr
, i
, j
;
1217 /* number of events */
1218 ret
= read(fd
, &nre
, sizeof(nre
));
1219 if (ret
!= (ssize_t
)sizeof(nre
))
1223 nre
= bswap_32(nre
);
1225 ret
= read(fd
, &sz
, sizeof(sz
));
1226 if (ret
!= (ssize_t
)sizeof(sz
))
1232 /* buffer to hold on file attr struct */
1237 /* the last event terminates with evsel->attr.size == 0: */
1238 events
= calloc(nre
+ 1, sizeof(*events
));
1242 msz
= sizeof(evsel
->attr
);
1246 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1250 * must read entire on-file attr struct to
1251 * sync up with layout.
1253 ret
= read(fd
, buf
, sz
);
1254 if (ret
!= (ssize_t
)sz
)
1258 perf_event__attr_swap(buf
);
1260 memcpy(&evsel
->attr
, buf
, msz
);
1262 ret
= read(fd
, &nr
, sizeof(nr
));
1263 if (ret
!= (ssize_t
)sizeof(nr
))
1269 evsel
->name
= do_read_string(fd
, ph
);
1274 id
= calloc(nr
, sizeof(*id
));
1280 for (j
= 0 ; j
< nr
; j
++) {
1281 ret
= read(fd
, id
, sizeof(*id
));
1282 if (ret
!= (ssize_t
)sizeof(*id
))
1285 *id
= bswap_64(*id
);
1295 free_event_desc(events
);
1300 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1302 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1307 fprintf(fp
, "# event desc: not available or unable to read\n");
1311 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1312 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1314 fprintf(fp
, "type = %d, config = 0x%"PRIx64
1315 ", config1 = 0x%"PRIx64
", config2 = 0x%"PRIx64
,
1317 (u64
)evsel
->attr
.config
,
1318 (u64
)evsel
->attr
.config1
,
1319 (u64
)evsel
->attr
.config2
);
1321 fprintf(fp
, ", excl_usr = %d, excl_kern = %d",
1322 evsel
->attr
.exclude_user
,
1323 evsel
->attr
.exclude_kernel
);
1325 fprintf(fp
, ", excl_host = %d, excl_guest = %d",
1326 evsel
->attr
.exclude_host
,
1327 evsel
->attr
.exclude_guest
);
1329 fprintf(fp
, ", precise_ip = %d", evsel
->attr
.precise_ip
);
1332 fprintf(fp
, ", id = {");
1333 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1336 fprintf(fp
, " %"PRIu64
, *id
);
1344 free_event_desc(events
);
1347 static void print_total_mem(struct perf_header
*h __used
, int fd
, FILE *fp
)
1352 ret
= read(fd
, &mem
, sizeof(mem
));
1353 if (ret
!= sizeof(mem
))
1357 mem
= bswap_64(mem
);
1359 fprintf(fp
, "# total memory : %"PRIu64
" kB\n", mem
);
1362 fprintf(fp
, "# total memory : unknown\n");
1365 static void print_numa_topology(struct perf_header
*h __used
, int fd
, FILE *fp
)
1370 uint64_t mem_total
, mem_free
;
1373 ret
= read(fd
, &nr
, sizeof(nr
));
1374 if (ret
!= (ssize_t
)sizeof(nr
))
1380 for (i
= 0; i
< nr
; i
++) {
1383 ret
= read(fd
, &c
, sizeof(c
));
1384 if (ret
!= (ssize_t
)sizeof(c
))
1390 ret
= read(fd
, &mem_total
, sizeof(u64
));
1391 if (ret
!= sizeof(u64
))
1394 ret
= read(fd
, &mem_free
, sizeof(u64
));
1395 if (ret
!= sizeof(u64
))
1398 if (h
->needs_swap
) {
1399 mem_total
= bswap_64(mem_total
);
1400 mem_free
= bswap_64(mem_free
);
1403 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1404 " free = %"PRIu64
" kB\n",
1409 str
= do_read_string(fd
, h
);
1410 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1415 fprintf(fp
, "# numa topology : not available\n");
1418 static void print_cpuid(struct perf_header
*ph
, int fd
, FILE *fp
)
1420 char *str
= do_read_string(fd
, ph
);
1421 fprintf(fp
, "# cpuid : %s\n", str
);
1425 static void print_branch_stack(struct perf_header
*ph __used
, int fd __used
,
1428 fprintf(fp
, "# contains samples with branch stack\n");
1431 static void print_pmu_mappings(struct perf_header
*ph
, int fd
, FILE *fp
)
1433 const char *delimiter
= "# pmu mappings: ";
1439 ret
= read(fd
, &pmu_num
, sizeof(pmu_num
));
1440 if (ret
!= sizeof(pmu_num
))
1444 pmu_num
= bswap_32(pmu_num
);
1447 fprintf(fp
, "# pmu mappings: not available\n");
1452 if (read(fd
, &type
, sizeof(type
)) != sizeof(type
))
1455 type
= bswap_32(type
);
1457 name
= do_read_string(fd
, ph
);
1461 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, name
, type
);
1471 fprintf(fp
, "# pmu mappings: unable to read\n");
1474 static int __event_process_build_id(struct build_id_event
*bev
,
1476 struct perf_session
*session
)
1479 struct list_head
*head
;
1480 struct machine
*machine
;
1483 enum dso_kernel_type dso_type
;
1485 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1489 misc
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1492 case PERF_RECORD_MISC_KERNEL
:
1493 dso_type
= DSO_TYPE_KERNEL
;
1494 head
= &machine
->kernel_dsos
;
1496 case PERF_RECORD_MISC_GUEST_KERNEL
:
1497 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1498 head
= &machine
->kernel_dsos
;
1500 case PERF_RECORD_MISC_USER
:
1501 case PERF_RECORD_MISC_GUEST_USER
:
1502 dso_type
= DSO_TYPE_USER
;
1503 head
= &machine
->user_dsos
;
1509 dso
= __dsos__findnew(head
, filename
);
1511 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1513 dso__set_build_id(dso
, &bev
->build_id
);
1515 if (filename
[0] == '[')
1516 dso
->kernel
= dso_type
;
1518 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1520 pr_debug("build id event received for %s: %s\n",
1521 dso
->long_name
, sbuild_id
);
1529 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1530 int input
, u64 offset
, u64 size
)
1532 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1534 struct perf_event_header header
;
1535 u8 build_id
[ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1538 struct build_id_event bev
;
1539 char filename
[PATH_MAX
];
1540 u64 limit
= offset
+ size
;
1542 while (offset
< limit
) {
1545 if (read(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1548 if (header
->needs_swap
)
1549 perf_event_header__bswap(&old_bev
.header
);
1551 len
= old_bev
.header
.size
- sizeof(old_bev
);
1552 if (read(input
, filename
, len
) != len
)
1555 bev
.header
= old_bev
.header
;
1558 * As the pid is the missing value, we need to fill
1559 * it properly. The header.misc value give us nice hint.
1561 bev
.pid
= HOST_KERNEL_ID
;
1562 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1563 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1564 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1566 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1567 __event_process_build_id(&bev
, filename
, session
);
1569 offset
+= bev
.header
.size
;
1575 static int perf_header__read_build_ids(struct perf_header
*header
,
1576 int input
, u64 offset
, u64 size
)
1578 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1579 struct build_id_event bev
;
1580 char filename
[PATH_MAX
];
1581 u64 limit
= offset
+ size
, orig_offset
= offset
;
1584 while (offset
< limit
) {
1587 if (read(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1590 if (header
->needs_swap
)
1591 perf_event_header__bswap(&bev
.header
);
1593 len
= bev
.header
.size
- sizeof(bev
);
1594 if (read(input
, filename
, len
) != len
)
1597 * The a1645ce1 changeset:
1599 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1601 * Added a field to struct build_id_event that broke the file
1604 * Since the kernel build-id is the first entry, process the
1605 * table using the old format if the well known
1606 * '[kernel.kallsyms]' string for the kernel build-id has the
1607 * first 4 characters chopped off (where the pid_t sits).
1609 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1610 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1612 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1615 __event_process_build_id(&bev
, filename
, session
);
1617 offset
+= bev
.header
.size
;
1624 static int process_tracing_data(struct perf_file_section
*section __unused
,
1625 struct perf_header
*ph __unused
,
1626 int feat __unused
, int fd
, void *data
)
1628 trace_report(fd
, data
, false);
1632 static int process_build_id(struct perf_file_section
*section
,
1633 struct perf_header
*ph
,
1634 int feat __unused
, int fd
, void *data __used
)
1636 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1637 pr_debug("Failed to read buildids, continuing...\n");
1641 static struct perf_evsel
*
1642 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1644 struct perf_evsel
*evsel
;
1646 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
1647 if (evsel
->idx
== idx
)
1655 perf_evlist__set_event_name(struct perf_evlist
*evlist
, struct perf_evsel
*event
)
1657 struct perf_evsel
*evsel
;
1662 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1669 evsel
->name
= strdup(event
->name
);
1673 process_event_desc(struct perf_file_section
*section __unused
,
1674 struct perf_header
*header
, int feat __unused
, int fd
,
1677 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1678 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1683 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1684 perf_evlist__set_event_name(session
->evlist
, evsel
);
1686 free_event_desc(events
);
1691 struct feature_ops
{
1692 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
1693 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
1694 int (*process
)(struct perf_file_section
*section
,
1695 struct perf_header
*h
, int feat
, int fd
, void *data
);
1700 #define FEAT_OPA(n, func) \
1701 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1702 #define FEAT_OPP(n, func) \
1703 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1704 .process = process_##func }
1705 #define FEAT_OPF(n, func) \
1706 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1709 /* feature_ops not implemented: */
1710 #define print_tracing_data NULL
1711 #define print_build_id NULL
1713 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
1714 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
1715 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
1716 FEAT_OPA(HEADER_HOSTNAME
, hostname
),
1717 FEAT_OPA(HEADER_OSRELEASE
, osrelease
),
1718 FEAT_OPA(HEADER_VERSION
, version
),
1719 FEAT_OPA(HEADER_ARCH
, arch
),
1720 FEAT_OPA(HEADER_NRCPUS
, nrcpus
),
1721 FEAT_OPA(HEADER_CPUDESC
, cpudesc
),
1722 FEAT_OPA(HEADER_CPUID
, cpuid
),
1723 FEAT_OPA(HEADER_TOTAL_MEM
, total_mem
),
1724 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
1725 FEAT_OPA(HEADER_CMDLINE
, cmdline
),
1726 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
1727 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
1728 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
1729 FEAT_OPA(HEADER_PMU_MAPPINGS
, pmu_mappings
),
1732 struct header_print_data
{
1734 bool full
; /* extended list of headers */
1737 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
1738 struct perf_header
*ph
,
1739 int feat
, int fd
, void *data
)
1741 struct header_print_data
*hd
= data
;
1743 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
1744 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
1745 "%d, continuing...\n", section
->offset
, feat
);
1748 if (feat
>= HEADER_LAST_FEATURE
) {
1749 pr_warning("unknown feature %d\n", feat
);
1752 if (!feat_ops
[feat
].print
)
1755 if (!feat_ops
[feat
].full_only
|| hd
->full
)
1756 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
1758 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
1759 feat_ops
[feat
].name
);
1764 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
1766 struct header_print_data hd
;
1767 struct perf_header
*header
= &session
->header
;
1768 int fd
= session
->fd
;
1772 perf_header__process_sections(header
, fd
, &hd
,
1773 perf_file_section__fprintf_info
);
1777 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
1778 struct perf_file_section
**p
,
1779 struct perf_evlist
*evlist
)
1784 if (perf_header__has_feat(h
, type
)) {
1785 if (!feat_ops
[type
].write
)
1788 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
1790 err
= feat_ops
[type
].write(fd
, h
, evlist
);
1792 pr_debug("failed to write feature %d\n", type
);
1794 /* undo anything written */
1795 lseek(fd
, (*p
)->offset
, SEEK_SET
);
1799 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
1805 static int perf_header__adds_write(struct perf_header
*header
,
1806 struct perf_evlist
*evlist
, int fd
)
1809 struct perf_file_section
*feat_sec
, *p
;
1815 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
1819 feat_sec
= p
= calloc(sizeof(*feat_sec
), nr_sections
);
1820 if (feat_sec
== NULL
)
1823 sec_size
= sizeof(*feat_sec
) * nr_sections
;
1825 sec_start
= header
->data_offset
+ header
->data_size
;
1826 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
1828 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
1829 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
1830 perf_header__clear_feat(header
, feat
);
1833 lseek(fd
, sec_start
, SEEK_SET
);
1835 * may write more than needed due to dropped feature, but
1836 * this is okay, reader will skip the mising entries
1838 err
= do_write(fd
, feat_sec
, sec_size
);
1840 pr_debug("failed to write feature section\n");
1845 int perf_header__write_pipe(int fd
)
1847 struct perf_pipe_file_header f_header
;
1850 f_header
= (struct perf_pipe_file_header
){
1851 .magic
= PERF_MAGIC
,
1852 .size
= sizeof(f_header
),
1855 err
= do_write(fd
, &f_header
, sizeof(f_header
));
1857 pr_debug("failed to write perf pipe header\n");
1864 int perf_session__write_header(struct perf_session
*session
,
1865 struct perf_evlist
*evlist
,
1866 int fd
, bool at_exit
)
1868 struct perf_file_header f_header
;
1869 struct perf_file_attr f_attr
;
1870 struct perf_header
*header
= &session
->header
;
1871 struct perf_evsel
*evsel
, *pair
= NULL
;
1874 lseek(fd
, sizeof(f_header
), SEEK_SET
);
1876 if (session
->evlist
!= evlist
)
1877 pair
= perf_evlist__first(session
->evlist
);
1879 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
1880 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
1881 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
1884 pr_debug("failed to write perf header\n");
1887 if (session
->evlist
!= evlist
) {
1888 err
= do_write(fd
, pair
->id
, pair
->ids
* sizeof(u64
));
1891 evsel
->ids
+= pair
->ids
;
1892 pair
= perf_evsel__next(pair
);
1896 header
->attr_offset
= lseek(fd
, 0, SEEK_CUR
);
1898 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
1899 f_attr
= (struct perf_file_attr
){
1900 .attr
= evsel
->attr
,
1902 .offset
= evsel
->id_offset
,
1903 .size
= evsel
->ids
* sizeof(u64
),
1906 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
1908 pr_debug("failed to write perf header attribute\n");
1913 header
->event_offset
= lseek(fd
, 0, SEEK_CUR
);
1914 header
->event_size
= trace_event_count
* sizeof(struct perf_trace_event_type
);
1916 err
= do_write(fd
, trace_events
, header
->event_size
);
1918 pr_debug("failed to write perf header events\n");
1923 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
1926 err
= perf_header__adds_write(header
, evlist
, fd
);
1931 f_header
= (struct perf_file_header
){
1932 .magic
= PERF_MAGIC
,
1933 .size
= sizeof(f_header
),
1934 .attr_size
= sizeof(f_attr
),
1936 .offset
= header
->attr_offset
,
1937 .size
= evlist
->nr_entries
* sizeof(f_attr
),
1940 .offset
= header
->data_offset
,
1941 .size
= header
->data_size
,
1944 .offset
= header
->event_offset
,
1945 .size
= header
->event_size
,
1949 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
1951 lseek(fd
, 0, SEEK_SET
);
1952 err
= do_write(fd
, &f_header
, sizeof(f_header
));
1954 pr_debug("failed to write perf header\n");
1957 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
1963 static int perf_header__getbuffer64(struct perf_header
*header
,
1964 int fd
, void *buf
, size_t size
)
1966 if (readn(fd
, buf
, size
) <= 0)
1969 if (header
->needs_swap
)
1970 mem_bswap_64(buf
, size
);
1975 int perf_header__process_sections(struct perf_header
*header
, int fd
,
1977 int (*process
)(struct perf_file_section
*section
,
1978 struct perf_header
*ph
,
1979 int feat
, int fd
, void *data
))
1981 struct perf_file_section
*feat_sec
, *sec
;
1987 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
1991 feat_sec
= sec
= calloc(sizeof(*feat_sec
), nr_sections
);
1995 sec_size
= sizeof(*feat_sec
) * nr_sections
;
1997 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
1999 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2003 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2004 err
= process(sec
++, header
, feat
, fd
, data
);
2014 static const int attr_file_abi_sizes
[] = {
2015 [0] = PERF_ATTR_SIZE_VER0
,
2016 [1] = PERF_ATTR_SIZE_VER1
,
2017 [2] = PERF_ATTR_SIZE_VER2
,
2018 [3] = PERF_ATTR_SIZE_VER3
,
2023 * In the legacy file format, the magic number is not used to encode endianness.
2024 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2025 * on ABI revisions, we need to try all combinations for all endianness to
2026 * detect the endianness.
2028 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2030 uint64_t ref_size
, attr_size
;
2033 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2034 ref_size
= attr_file_abi_sizes
[i
]
2035 + sizeof(struct perf_file_section
);
2036 if (hdr_sz
!= ref_size
) {
2037 attr_size
= bswap_64(hdr_sz
);
2038 if (attr_size
!= ref_size
)
2041 ph
->needs_swap
= true;
2043 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2048 /* could not determine endianness */
2052 #define PERF_PIPE_HDR_VER0 16
2054 static const size_t attr_pipe_abi_sizes
[] = {
2055 [0] = PERF_PIPE_HDR_VER0
,
2060 * In the legacy pipe format, there is an implicit assumption that endiannesss
2061 * between host recording the samples, and host parsing the samples is the
2062 * same. This is not always the case given that the pipe output may always be
2063 * redirected into a file and analyzed on a different machine with possibly a
2064 * different endianness and perf_event ABI revsions in the perf tool itself.
2066 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2071 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2072 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2073 attr_size
= bswap_64(hdr_sz
);
2074 if (attr_size
!= hdr_sz
)
2077 ph
->needs_swap
= true;
2079 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2085 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2086 bool is_pipe
, struct perf_header
*ph
)
2090 /* check for legacy format */
2091 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2093 pr_debug("legacy perf.data format\n");
2095 return try_all_pipe_abis(hdr_sz
, ph
);
2097 return try_all_file_abis(hdr_sz
, ph
);
2100 * the new magic number serves two purposes:
2101 * - unique number to identify actual perf.data files
2102 * - encode endianness of file
2105 /* check magic number with one endianness */
2106 if (magic
== __perf_magic2
)
2109 /* check magic number with opposite endianness */
2110 if (magic
!= __perf_magic2_sw
)
2113 ph
->needs_swap
= true;
2118 int perf_file_header__read(struct perf_file_header
*header
,
2119 struct perf_header
*ph
, int fd
)
2123 lseek(fd
, 0, SEEK_SET
);
2125 ret
= readn(fd
, header
, sizeof(*header
));
2129 if (check_magic_endian(header
->magic
,
2130 header
->attr_size
, false, ph
) < 0) {
2131 pr_debug("magic/endian check failed\n");
2135 if (ph
->needs_swap
) {
2136 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2140 if (header
->size
!= sizeof(*header
)) {
2141 /* Support the previous format */
2142 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2143 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2146 } else if (ph
->needs_swap
) {
2148 * feature bitmap is declared as an array of unsigned longs --
2149 * not good since its size can differ between the host that
2150 * generated the data file and the host analyzing the file.
2152 * We need to handle endianness, but we don't know the size of
2153 * the unsigned long where the file was generated. Take a best
2154 * guess at determining it: try 64-bit swap first (ie., file
2155 * created on a 64-bit host), and check if the hostname feature
2156 * bit is set (this feature bit is forced on as of fbe96f2).
2157 * If the bit is not, undo the 64-bit swap and try a 32-bit
2158 * swap. If the hostname bit is still not set (e.g., older data
2159 * file), punt and fallback to the original behavior --
2160 * clearing all feature bits and setting buildid.
2162 mem_bswap_64(&header
->adds_features
,
2163 BITS_TO_U64(HEADER_FEAT_BITS
));
2165 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2167 mem_bswap_64(&header
->adds_features
,
2168 BITS_TO_U64(HEADER_FEAT_BITS
));
2171 mem_bswap_32(&header
->adds_features
,
2172 BITS_TO_U32(HEADER_FEAT_BITS
));
2175 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2176 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2177 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2181 memcpy(&ph
->adds_features
, &header
->adds_features
,
2182 sizeof(ph
->adds_features
));
2184 ph
->event_offset
= header
->event_types
.offset
;
2185 ph
->event_size
= header
->event_types
.size
;
2186 ph
->data_offset
= header
->data
.offset
;
2187 ph
->data_size
= header
->data
.size
;
2191 static int perf_file_section__process(struct perf_file_section
*section
,
2192 struct perf_header
*ph
,
2193 int feat
, int fd
, void *data
)
2195 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2196 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2197 "%d, continuing...\n", section
->offset
, feat
);
2201 if (feat
>= HEADER_LAST_FEATURE
) {
2202 pr_debug("unknown feature %d, continuing...\n", feat
);
2206 if (!feat_ops
[feat
].process
)
2209 return feat_ops
[feat
].process(section
, ph
, feat
, fd
, data
);
2212 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2213 struct perf_header
*ph
, int fd
,
2218 ret
= readn(fd
, header
, sizeof(*header
));
2222 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2223 pr_debug("endian/magic failed\n");
2228 header
->size
= bswap_64(header
->size
);
2230 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2236 static int perf_header__read_pipe(struct perf_session
*session
, int fd
)
2238 struct perf_header
*header
= &session
->header
;
2239 struct perf_pipe_file_header f_header
;
2241 if (perf_file_header__read_pipe(&f_header
, header
, fd
,
2242 session
->repipe
) < 0) {
2243 pr_debug("incompatible file format\n");
2252 static int read_attr(int fd
, struct perf_header
*ph
,
2253 struct perf_file_attr
*f_attr
)
2255 struct perf_event_attr
*attr
= &f_attr
->attr
;
2257 size_t our_sz
= sizeof(f_attr
->attr
);
2260 memset(f_attr
, 0, sizeof(*f_attr
));
2262 /* read minimal guaranteed structure */
2263 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2265 pr_debug("cannot read %d bytes of header attr\n",
2266 PERF_ATTR_SIZE_VER0
);
2270 /* on file perf_event_attr size */
2278 sz
= PERF_ATTR_SIZE_VER0
;
2279 } else if (sz
> our_sz
) {
2280 pr_debug("file uses a more recent and unsupported ABI"
2281 " (%zu bytes extra)\n", sz
- our_sz
);
2284 /* what we have not yet read and that we know about */
2285 left
= sz
- PERF_ATTR_SIZE_VER0
;
2288 ptr
+= PERF_ATTR_SIZE_VER0
;
2290 ret
= readn(fd
, ptr
, left
);
2292 /* read perf_file_section, ids are read in caller */
2293 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2295 return ret
<= 0 ? -1 : 0;
2298 static int perf_evsel__set_tracepoint_name(struct perf_evsel
*evsel
,
2299 struct pevent
*pevent
)
2301 struct event_format
*event
= pevent_find_event(pevent
,
2302 evsel
->attr
.config
);
2308 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2309 evsel
->name
= strdup(bf
);
2310 if (evsel
->name
== NULL
)
2313 evsel
->tp_format
= event
;
2317 static int perf_evlist__set_tracepoint_names(struct perf_evlist
*evlist
,
2318 struct pevent
*pevent
)
2320 struct perf_evsel
*pos
;
2322 list_for_each_entry(pos
, &evlist
->entries
, node
) {
2323 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&& !pos
->name
&&
2324 perf_evsel__set_tracepoint_name(pos
, pevent
))
2331 int perf_session__read_header(struct perf_session
*session
, int fd
)
2333 struct perf_header
*header
= &session
->header
;
2334 struct perf_file_header f_header
;
2335 struct perf_file_attr f_attr
;
2337 int nr_attrs
, nr_ids
, i
, j
;
2339 session
->evlist
= perf_evlist__new(NULL
, NULL
);
2340 if (session
->evlist
== NULL
)
2343 if (session
->fd_pipe
)
2344 return perf_header__read_pipe(session
, fd
);
2346 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2349 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2350 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2352 for (i
= 0; i
< nr_attrs
; i
++) {
2353 struct perf_evsel
*evsel
;
2356 if (read_attr(fd
, header
, &f_attr
) < 0)
2359 if (header
->needs_swap
)
2360 perf_event__attr_swap(&f_attr
.attr
);
2362 tmp
= lseek(fd
, 0, SEEK_CUR
);
2363 evsel
= perf_evsel__new(&f_attr
.attr
, i
);
2366 goto out_delete_evlist
;
2368 * Do it before so that if perf_evsel__alloc_id fails, this
2369 * entry gets purged too at perf_evlist__delete().
2371 perf_evlist__add(session
->evlist
, evsel
);
2373 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2375 * We don't have the cpu and thread maps on the header, so
2376 * for allocating the perf_sample_id table we fake 1 cpu and
2377 * hattr->ids threads.
2379 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2380 goto out_delete_evlist
;
2382 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2384 for (j
= 0; j
< nr_ids
; j
++) {
2385 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2388 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2391 lseek(fd
, tmp
, SEEK_SET
);
2394 symbol_conf
.nr_events
= nr_attrs
;
2396 if (f_header
.event_types
.size
) {
2397 lseek(fd
, f_header
.event_types
.offset
, SEEK_SET
);
2398 trace_events
= malloc(f_header
.event_types
.size
);
2399 if (trace_events
== NULL
)
2401 if (perf_header__getbuffer64(header
, fd
, trace_events
,
2402 f_header
.event_types
.size
))
2404 trace_event_count
= f_header
.event_types
.size
/ sizeof(struct perf_trace_event_type
);
2407 perf_header__process_sections(header
, fd
, &session
->pevent
,
2408 perf_file_section__process
);
2410 lseek(fd
, header
->data_offset
, SEEK_SET
);
2412 if (perf_evlist__set_tracepoint_names(session
->evlist
, session
->pevent
))
2413 goto out_delete_evlist
;
2421 perf_evlist__delete(session
->evlist
);
2422 session
->evlist
= NULL
;
2426 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2427 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2428 perf_event__handler_t process
)
2430 union perf_event
*ev
;
2434 size
= sizeof(struct perf_event_attr
);
2435 size
= ALIGN(size
, sizeof(u64
));
2436 size
+= sizeof(struct perf_event_header
);
2437 size
+= ids
* sizeof(u64
);
2444 ev
->attr
.attr
= *attr
;
2445 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2447 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2448 ev
->attr
.header
.size
= (u16
)size
;
2450 if (ev
->attr
.header
.size
== size
)
2451 err
= process(tool
, ev
, NULL
, NULL
);
2460 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2461 struct perf_session
*session
,
2462 perf_event__handler_t process
)
2464 struct perf_evsel
*evsel
;
2467 list_for_each_entry(evsel
, &session
->evlist
->entries
, node
) {
2468 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
2469 evsel
->id
, process
);
2471 pr_debug("failed to create perf header attribute\n");
2479 int perf_event__process_attr(union perf_event
*event
,
2480 struct perf_evlist
**pevlist
)
2483 struct perf_evsel
*evsel
;
2484 struct perf_evlist
*evlist
= *pevlist
;
2486 if (evlist
== NULL
) {
2487 *pevlist
= evlist
= perf_evlist__new(NULL
, NULL
);
2492 evsel
= perf_evsel__new(&event
->attr
.attr
, evlist
->nr_entries
);
2496 perf_evlist__add(evlist
, evsel
);
2498 ids
= event
->header
.size
;
2499 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2500 n_ids
= ids
/ sizeof(u64
);
2502 * We don't have the cpu and thread maps on the header, so
2503 * for allocating the perf_sample_id table we fake 1 cpu and
2504 * hattr->ids threads.
2506 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2509 for (i
= 0; i
< n_ids
; i
++) {
2510 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2516 int perf_event__synthesize_event_type(struct perf_tool
*tool
,
2517 u64 event_id
, char *name
,
2518 perf_event__handler_t process
,
2519 struct machine
*machine
)
2521 union perf_event ev
;
2525 memset(&ev
, 0, sizeof(ev
));
2527 ev
.event_type
.event_type
.event_id
= event_id
;
2528 memset(ev
.event_type
.event_type
.name
, 0, MAX_EVENT_NAME
);
2529 strncpy(ev
.event_type
.event_type
.name
, name
, MAX_EVENT_NAME
- 1);
2531 ev
.event_type
.header
.type
= PERF_RECORD_HEADER_EVENT_TYPE
;
2532 size
= strlen(ev
.event_type
.event_type
.name
);
2533 size
= ALIGN(size
, sizeof(u64
));
2534 ev
.event_type
.header
.size
= sizeof(ev
.event_type
) -
2535 (sizeof(ev
.event_type
.event_type
.name
) - size
);
2537 err
= process(tool
, &ev
, NULL
, machine
);
2542 int perf_event__synthesize_event_types(struct perf_tool
*tool
,
2543 perf_event__handler_t process
,
2544 struct machine
*machine
)
2546 struct perf_trace_event_type
*type
;
2549 for (i
= 0; i
< trace_event_count
; i
++) {
2550 type
= &trace_events
[i
];
2552 err
= perf_event__synthesize_event_type(tool
, type
->event_id
,
2553 type
->name
, process
,
2556 pr_debug("failed to create perf header event type\n");
2564 int perf_event__process_event_type(struct perf_tool
*tool __unused
,
2565 union perf_event
*event
)
2567 if (perf_header__push_event(event
->event_type
.event_type
.event_id
,
2568 event
->event_type
.event_type
.name
) < 0)
2574 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
2575 struct perf_evlist
*evlist
,
2576 perf_event__handler_t process
)
2578 union perf_event ev
;
2579 struct tracing_data
*tdata
;
2580 ssize_t size
= 0, aligned_size
= 0, padding
;
2584 * We are going to store the size of the data followed
2585 * by the data contents. Since the fd descriptor is a pipe,
2586 * we cannot seek back to store the size of the data once
2587 * we know it. Instead we:
2589 * - write the tracing data to the temp file
2590 * - get/write the data size to pipe
2591 * - write the tracing data from the temp file
2594 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
2598 memset(&ev
, 0, sizeof(ev
));
2600 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
2602 aligned_size
= ALIGN(size
, sizeof(u64
));
2603 padding
= aligned_size
- size
;
2604 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
2605 ev
.tracing_data
.size
= aligned_size
;
2607 process(tool
, &ev
, NULL
, NULL
);
2610 * The put function will copy all the tracing data
2611 * stored in temp file to the pipe.
2613 tracing_data_put(tdata
);
2615 write_padded(fd
, NULL
, 0, padding
);
2617 return aligned_size
;
2620 int perf_event__process_tracing_data(union perf_event
*event
,
2621 struct perf_session
*session
)
2623 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
2624 off_t offset
= lseek(session
->fd
, 0, SEEK_CUR
);
2627 /* setup for reading amidst mmap */
2628 lseek(session
->fd
, offset
+ sizeof(struct tracing_data_event
),
2631 size_read
= trace_report(session
->fd
, &session
->pevent
,
2633 padding
= ALIGN(size_read
, sizeof(u64
)) - size_read
;
2635 if (read(session
->fd
, buf
, padding
) < 0)
2636 die("reading input file");
2637 if (session
->repipe
) {
2638 int retw
= write(STDOUT_FILENO
, buf
, padding
);
2639 if (retw
<= 0 || retw
!= padding
)
2640 die("repiping tracing data padding");
2643 if (size_read
+ padding
!= size
)
2644 die("tracing data size mismatch");
2646 perf_evlist__set_tracepoint_names(session
->evlist
, session
->pevent
);
2648 return size_read
+ padding
;
2651 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
2652 struct dso
*pos
, u16 misc
,
2653 perf_event__handler_t process
,
2654 struct machine
*machine
)
2656 union perf_event ev
;
2663 memset(&ev
, 0, sizeof(ev
));
2665 len
= pos
->long_name_len
+ 1;
2666 len
= ALIGN(len
, NAME_ALIGN
);
2667 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
2668 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
2669 ev
.build_id
.header
.misc
= misc
;
2670 ev
.build_id
.pid
= machine
->pid
;
2671 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
2672 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
2674 err
= process(tool
, &ev
, NULL
, machine
);
2679 int perf_event__process_build_id(struct perf_tool
*tool __used
,
2680 union perf_event
*event
,
2681 struct perf_session
*session
)
2683 __event_process_build_id(&event
->build_id
,
2684 event
->build_id
.filename
,
2689 void disable_buildid_cache(void)
2691 no_buildid_cache
= true;