2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <sys/types.h>
38 #include "trace-event.h"
46 static ssize_t calc_data_size;
49 static int do_read(int fd, void *buf, int size)
54 int ret = read(fd, buf, size);
60 int retw = write(STDOUT_FILENO, buf, ret);
62 if (retw <= 0 || retw != ret)
63 die("repiping input file");
73 static int read_or_die(void *data, int size)
77 r = do_read(input_fd, data, size);
79 die("reading input file (size expected=%d received=%d)",
88 /* If it fails, the next read will report it */
89 static void skip(int size)
95 r = size > BUFSIZ ? BUFSIZ : size;
101 static unsigned int read4(struct pevent *pevent)
105 read_or_die(&data, 4);
106 return __data2host4(pevent, data);
109 static unsigned long long read8(struct pevent *pevent)
111 unsigned long long data;
113 read_or_die(&data, 8);
114 return __data2host8(pevent, data);
117 static char *read_string(void)
126 r = read(input_fd, &c, 1);
128 die("reading input file");
134 int retw = write(STDOUT_FILENO, &c, 1);
136 if (retw <= 0 || retw != r)
137 die("repiping input file string");
147 calc_data_size += size;
151 memcpy(str, buf, size);
156 static int read_proc_kallsyms(struct pevent *pevent)
161 size = read4(pevent);
165 buf = malloc(size + 1);
169 read_or_die(buf, size);
172 parse_proc_kallsyms(pevent, buf, size);
178 static int read_ftrace_printk(struct pevent *pevent)
183 size = read4(pevent);
191 read_or_die(buf, size);
193 parse_ftrace_printk(pevent, buf, size);
199 static int read_header_files(struct pevent *pevent)
201 unsigned long long size;
205 read_or_die(buf, 12);
207 if (memcmp(buf, "header_page", 12) != 0)
208 die("did not read header page");
210 size = read8(pevent);
214 * The size field in the page is of type long,
215 * use that instead, since it represents the kernel.
217 long_size = header_page_size_size;
219 read_or_die(buf, 13);
220 if (memcmp(buf, "header_event", 13) != 0)
221 die("did not read header event");
223 size = read8(pevent);
224 header_event = malloc(size);
225 if (header_event == NULL)
228 read_or_die(header_event, size);
233 static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
241 read_or_die(buf, size);
242 parse_ftrace_file(pevent, buf, size);
247 static int read_event_file(struct pevent *pevent, char *sys,
248 unsigned long long size)
256 read_or_die(buf, size);
257 parse_event_file(pevent, buf, size, sys);
262 static int read_ftrace_files(struct pevent *pevent)
264 unsigned long long size;
269 count = read4(pevent);
271 for (i = 0; i < count; i++) {
272 size = read8(pevent);
273 ret = read_ftrace_file(pevent, size);
280 static int read_event_files(struct pevent *pevent)
282 unsigned long long size;
289 systems = read4(pevent);
291 for (i = 0; i < systems; i++) {
296 count = read4(pevent);
297 for (x=0; x < count; x++) {
298 size = read8(pevent);
299 ret = read_event_file(pevent, sys, size);
307 ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
310 char test[] = { 23, 8, 68 };
312 int show_version = 0;
316 struct pevent *pevent;
327 if (memcmp(buf, test, 3) != 0)
328 die("no trace data in the file");
331 if (memcmp(buf, "tracing", 7) != 0)
332 die("not a trace file (missing 'tracing' tag)");
334 version = read_string();
338 printf("version = %s\n", version);
342 file_bigendian = buf[0];
343 host_bigendian = bigendian();
345 pevent = read_trace_init(file_bigendian, host_bigendian);
346 if (pevent == NULL) {
347 pr_debug("read_trace_init failed");
354 page_size = read4(pevent);
356 err = read_header_files(pevent);
359 err = read_ftrace_files(pevent);
362 err = read_event_files(pevent);
365 err = read_proc_kallsyms(pevent);
368 err = read_ftrace_printk(pevent);
372 size = calc_data_size - 1;
377 pevent_print_funcs(pevent);
378 } else if (show_printk) {
379 pevent_print_printk(pevent);