1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
10 #include <sys/types.h>
14 #include <event-parse.h>
19 #include "trace-event.h"
25 static ssize_t trace_data_size
;
28 static int __do_read(int fd
, void *buf
, int size
)
33 int ret
= read(fd
, buf
, size
);
39 int retw
= write(STDOUT_FILENO
, buf
, ret
);
41 if (retw
<= 0 || retw
!= ret
) {
42 pr_debug("repiping input file");
54 static int do_read(void *data
, int size
)
58 r
= __do_read(input_fd
, data
, size
);
60 pr_debug("reading input file (size expected=%d received=%d)",
70 /* If it fails, the next read will report it */
71 static void skip(int size
)
77 r
= size
> BUFSIZ
? BUFSIZ
: size
;
83 static unsigned int read4(struct tep_handle
*pevent
)
87 if (do_read(&data
, 4) < 0)
89 return tep_read_number(pevent
, &data
, 4);
92 static unsigned long long read8(struct tep_handle
*pevent
)
94 unsigned long long data
;
96 if (do_read(&data
, 8) < 0)
98 return tep_read_number(pevent
, &data
, 8);
101 static char *read_string(void)
110 r
= read(input_fd
, &c
, 1);
112 pr_debug("reading input file");
122 int retw
= write(STDOUT_FILENO
, &c
, 1);
124 if (retw
<= 0 || retw
!= r
) {
125 pr_debug("repiping input file string");
136 trace_data_size
+= size
;
140 memcpy(str
, buf
, size
);
145 static int read_proc_kallsyms(struct tep_handle
*pevent
)
149 size
= read4(pevent
);
153 * Just skip it, now that we configure libtraceevent to use the
154 * tools/perf/ symbol resolver.
156 * We need to skip it so that we can continue parsing old perf.data
157 * files, that contains this /proc/kallsyms payload.
159 * Newer perf.data files will have just the 4-bytes zeros "kallsyms
160 * payload", so that older tools can continue reading it and interpret
161 * it as "no kallsyms payload is present".
163 lseek(input_fd
, size
, SEEK_CUR
);
164 trace_data_size
+= size
;
168 static int read_ftrace_printk(struct tep_handle
*pevent
)
173 /* it can have 0 size */
174 size
= read4(pevent
);
178 buf
= malloc(size
+ 1);
182 if (do_read(buf
, size
) < 0) {
189 parse_ftrace_printk(pevent
, buf
, size
);
195 static int read_header_files(struct tep_handle
*pevent
)
197 unsigned long long size
;
202 if (do_read(buf
, 12) < 0)
205 if (memcmp(buf
, "header_page", 12) != 0) {
206 pr_debug("did not read header page");
210 size
= read8(pevent
);
212 header_page
= malloc(size
);
213 if (header_page
== NULL
)
216 if (do_read(header_page
, size
) < 0) {
217 pr_debug("did not read header page");
222 if (!tep_parse_header_page(pevent
, header_page
, size
,
223 tep_get_long_size(pevent
))) {
225 * The commit field in the page is of type long,
226 * use that instead, since it represents the kernel.
228 tep_set_long_size(pevent
, tep_get_header_page_size(pevent
));
232 if (do_read(buf
, 13) < 0)
235 if (memcmp(buf
, "header_event", 13) != 0) {
236 pr_debug("did not read header event");
240 size
= read8(pevent
);
246 static int read_ftrace_file(struct tep_handle
*pevent
, unsigned long long size
)
253 pr_debug("memory allocation failure\n");
257 ret
= do_read(buf
, size
);
259 pr_debug("error reading ftrace file.\n");
263 ret
= parse_ftrace_file(pevent
, buf
, size
);
265 pr_debug("error parsing ftrace file.\n");
271 static int read_event_file(struct tep_handle
*pevent
, char *sys
,
272 unsigned long long size
)
279 pr_debug("memory allocation failure\n");
283 ret
= do_read(buf
, size
);
287 ret
= parse_event_file(pevent
, buf
, size
, sys
);
289 pr_debug("error parsing event file.\n");
295 static int read_ftrace_files(struct tep_handle
*pevent
)
297 unsigned long long size
;
302 count
= read4(pevent
);
304 for (i
= 0; i
< count
; i
++) {
305 size
= read8(pevent
);
306 ret
= read_ftrace_file(pevent
, size
);
313 static int read_event_files(struct tep_handle
*pevent
)
315 unsigned long long size
;
322 systems
= read4(pevent
);
324 for (i
= 0; i
< systems
; i
++) {
329 count
= read4(pevent
);
331 for (x
=0; x
< count
; x
++) {
332 size
= read8(pevent
);
333 ret
= read_event_file(pevent
, sys
, size
);
344 static int read_saved_cmdline(struct tep_handle
*pevent
)
346 unsigned long long size
;
350 /* it can have 0 size */
351 size
= read8(pevent
);
355 buf
= malloc(size
+ 1);
357 pr_debug("memory allocation failure\n");
361 ret
= do_read(buf
, size
);
363 pr_debug("error reading saved cmdlines\n");
368 parse_saved_cmdline(pevent
, buf
, size
);
375 ssize_t
trace_report(int fd
, struct trace_event
*tevent
, bool __repipe
)
378 char test
[] = { 23, 8, 68 };
380 int show_version
= 0;
388 struct tep_handle
*pevent
= NULL
;
394 if (do_read(buf
, 3) < 0)
396 if (memcmp(buf
, test
, 3) != 0) {
397 pr_debug("no trace data in the file");
401 if (do_read(buf
, 7) < 0)
403 if (memcmp(buf
, "tracing", 7) != 0) {
404 pr_debug("not a trace file (missing 'tracing' tag)");
408 version
= read_string();
412 printf("version = %s\n", version
);
414 if (do_read(buf
, 1) < 0) {
418 file_bigendian
= buf
[0];
419 host_bigendian
= host_is_bigendian() ? 1 : 0;
421 if (trace_event__init(tevent
)) {
422 pr_debug("trace_event__init failed");
426 pevent
= tevent
->pevent
;
428 tep_set_flag(pevent
, TEP_NSEC_OUTPUT
);
429 tep_set_file_bigendian(pevent
, file_bigendian
);
430 tep_set_local_bigendian(pevent
, host_bigendian
);
432 if (do_read(buf
, 1) < 0)
434 file_long_size
= buf
[0];
436 file_page_size
= read4(pevent
);
440 tep_set_long_size(pevent
, file_long_size
);
441 tep_set_page_size(pevent
, file_page_size
);
443 err
= read_header_files(pevent
);
446 err
= read_ftrace_files(pevent
);
449 err
= read_event_files(pevent
);
452 err
= read_proc_kallsyms(pevent
);
455 err
= read_ftrace_printk(pevent
);
458 if (atof(version
) >= 0.6) {
459 err
= read_saved_cmdline(pevent
);
464 size
= trace_data_size
;
468 tep_print_funcs(pevent
);
469 } else if (show_printk
) {
470 tep_print_printk(pevent
);
477 trace_event__cleanup(tevent
);