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 pr_debug("reading input file");
144 int retw = write(STDOUT_FILENO, &c, 1);
146 if (retw <= 0 || retw != r) {
147 pr_debug("repiping input file string");
159 calc_data_size += size;
163 memcpy(str, buf, size);
168 static int read_proc_kallsyms(struct pevent *pevent)
173 size = read4(pevent);
177 buf = malloc(size + 1);
181 if (do_read(buf, size) < 0) {
187 parse_proc_kallsyms(pevent, buf, size);
193 static int read_ftrace_printk(struct pevent *pevent)
198 /* it can have 0 size */
199 size = read4(pevent);
207 if (do_read(buf, size) < 0) {
212 parse_ftrace_printk(pevent, buf, size);
218 static int read_header_files(struct pevent *pevent)
220 unsigned long long size;
225 if (do_read(buf, 12) < 0)
228 if (memcmp(buf, "header_page", 12) != 0) {
229 pr_debug("did not read header page");
233 size = read8(pevent);
237 * The size field in the page is of type long,
238 * use that instead, since it represents the kernel.
240 long_size = header_page_size_size;
242 if (do_read(buf, 13) < 0)
245 if (memcmp(buf, "header_event", 13) != 0) {
246 pr_debug("did not read header event");
250 size = read8(pevent);
251 header_event = malloc(size);
252 if (header_event == NULL)
255 if (do_read(header_event, size) < 0)
262 static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
270 if (do_read(buf, size) < 0) {
275 parse_ftrace_file(pevent, buf, size);
280 static int read_event_file(struct pevent *pevent, char *sys,
281 unsigned long long size)
289 if (do_read(buf, size) < 0) {
294 parse_event_file(pevent, buf, size, sys);
299 static int read_ftrace_files(struct pevent *pevent)
301 unsigned long long size;
306 count = read4(pevent);
308 for (i = 0; i < count; i++) {
309 size = read8(pevent);
310 ret = read_ftrace_file(pevent, size);
317 static int read_event_files(struct pevent *pevent)
319 unsigned long long size;
326 systems = read4(pevent);
328 for (i = 0; i < systems; i++) {
333 count = read4(pevent);
335 for (x=0; x < count; x++) {
336 size = read8(pevent);
337 ret = read_event_file(pevent, sys, size);
345 ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
348 char test[] = { 23, 8, 68 };
350 int show_version = 0;
354 struct pevent *pevent;
364 if (do_read(buf, 3) < 0)
366 if (memcmp(buf, test, 3) != 0) {
367 pr_debug("no trace data in the file");
371 if (do_read(buf, 7) < 0)
373 if (memcmp(buf, "tracing", 7) != 0) {
374 pr_debug("not a trace file (missing 'tracing' tag)");
378 version = read_string();
382 printf("version = %s\n", version);
385 if (do_read(buf, 1) < 0)
387 file_bigendian = buf[0];
388 host_bigendian = bigendian();
390 pevent = read_trace_init(file_bigendian, host_bigendian);
391 if (pevent == NULL) {
392 pr_debug("read_trace_init failed");
396 if (do_read(buf, 1) < 0)
400 page_size = read4(pevent);
404 err = read_header_files(pevent);
407 err = read_ftrace_files(pevent);
410 err = read_event_files(pevent);
413 err = read_proc_kallsyms(pevent);
416 err = read_ftrace_printk(pevent);
420 size = calc_data_size - 1;
425 pevent_print_funcs(pevent);
426 } else if (show_printk) {
427 pevent_print_printk(pevent);