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 pr_debug("repiping input file");
75 static int do_read(void *data, int size)
79 r = __do_read(input_fd, data, size);
81 pr_debug("reading input file (size expected=%d received=%d)",
92 /* If it fails, the next read will report it */
93 static void skip(int size)
99 r = size > BUFSIZ ? BUFSIZ : size;
105 static unsigned int read4(struct pevent *pevent)
109 if (do_read(&data, 4) < 0)
111 return __data2host4(pevent, data);
114 static unsigned long long read8(struct pevent *pevent)
116 unsigned long long data;
118 if (do_read(&data, 8) < 0)
120 return __data2host8(pevent, data);
123 static char *read_string(void)
132 r = read(input_fd, &c, 1);
134 die("reading input file");
140 int retw = write(STDOUT_FILENO, &c, 1);
142 if (retw <= 0 || retw != r)
143 die("repiping input file string");
153 calc_data_size += size;
157 memcpy(str, buf, size);
162 static int read_proc_kallsyms(struct pevent *pevent)
167 size = read4(pevent);
171 buf = malloc(size + 1);
175 if (do_read(buf, size) < 0) {
181 parse_proc_kallsyms(pevent, buf, size);
187 static int read_ftrace_printk(struct pevent *pevent)
192 /* it can have 0 size */
193 size = read4(pevent);
201 if (do_read(buf, size) < 0) {
206 parse_ftrace_printk(pevent, buf, size);
212 static int read_header_files(struct pevent *pevent)
214 unsigned long long size;
219 if (do_read(buf, 12) < 0)
222 if (memcmp(buf, "header_page", 12) != 0)
223 die("did not read header page");
225 size = read8(pevent);
229 * The size field in the page is of type long,
230 * use that instead, since it represents the kernel.
232 long_size = header_page_size_size;
234 if (do_read(buf, 13) < 0)
237 if (memcmp(buf, "header_event", 13) != 0)
238 die("did not read header event");
240 size = read8(pevent);
241 header_event = malloc(size);
242 if (header_event == NULL)
245 if (do_read(header_event, size) < 0)
252 static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
260 if (do_read(buf, size) < 0) {
265 parse_ftrace_file(pevent, buf, size);
270 static int read_event_file(struct pevent *pevent, char *sys,
271 unsigned long long size)
279 if (do_read(buf, size) < 0) {
284 parse_event_file(pevent, buf, size, sys);
289 static int read_ftrace_files(struct pevent *pevent)
291 unsigned long long size;
296 count = read4(pevent);
298 for (i = 0; i < count; i++) {
299 size = read8(pevent);
300 ret = read_ftrace_file(pevent, size);
307 static int read_event_files(struct pevent *pevent)
309 unsigned long long size;
316 systems = read4(pevent);
318 for (i = 0; i < systems; i++) {
323 count = read4(pevent);
325 for (x=0; x < count; x++) {
326 size = read8(pevent);
327 ret = read_event_file(pevent, sys, size);
335 ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
338 char test[] = { 23, 8, 68 };
340 int show_version = 0;
344 struct pevent *pevent;
354 if (do_read(buf, 3) < 0)
356 if (memcmp(buf, test, 3) != 0)
357 die("no trace data in the file");
359 if (do_read(buf, 7) < 0)
361 if (memcmp(buf, "tracing", 7) != 0)
362 die("not a trace file (missing 'tracing' tag)");
364 version = read_string();
368 printf("version = %s\n", version);
371 if (do_read(buf, 1) < 0)
373 file_bigendian = buf[0];
374 host_bigendian = bigendian();
376 pevent = read_trace_init(file_bigendian, host_bigendian);
377 if (pevent == NULL) {
378 pr_debug("read_trace_init failed");
382 if (do_read(buf, 1) < 0)
386 page_size = read4(pevent);
390 err = read_header_files(pevent);
393 err = read_ftrace_files(pevent);
396 err = read_event_files(pevent);
399 err = read_proc_kallsyms(pevent);
402 err = read_ftrace_printk(pevent);
406 size = calc_data_size - 1;
411 pevent_print_funcs(pevent);
412 } else if (show_printk) {
413 pevent_print_printk(pevent);