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 trace_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)",
91 /* If it fails, the next read will report it */
92 static void skip(int size)
98 r = size > BUFSIZ ? BUFSIZ : size;
104 static unsigned int read4(struct pevent *pevent)
108 if (do_read(&data, 4) < 0)
110 return __data2host4(pevent, data);
113 static unsigned long long read8(struct pevent *pevent)
115 unsigned long long data;
117 if (do_read(&data, 8) < 0)
119 return __data2host8(pevent, data);
122 static char *read_string(void)
131 r = read(input_fd, &c, 1);
133 pr_debug("reading input file");
143 int retw = write(STDOUT_FILENO, &c, 1);
145 if (retw <= 0 || retw != r) {
146 pr_debug("repiping input file string");
157 trace_data_size += size;
161 memcpy(str, buf, size);
166 static int read_proc_kallsyms(struct pevent *pevent)
171 size = read4(pevent);
175 buf = malloc(size + 1);
179 if (do_read(buf, size) < 0) {
185 parse_proc_kallsyms(pevent, buf, size);
191 static int read_ftrace_printk(struct pevent *pevent)
196 /* it can have 0 size */
197 size = read4(pevent);
205 if (do_read(buf, size) < 0) {
210 parse_ftrace_printk(pevent, buf, size);
216 static int read_header_files(struct pevent *pevent)
218 unsigned long long size;
223 if (do_read(buf, 12) < 0)
226 if (memcmp(buf, "header_page", 12) != 0) {
227 pr_debug("did not read header page");
231 size = read8(pevent);
235 * The size field in the page is of type long,
236 * use that instead, since it represents the kernel.
238 long_size = header_page_size_size;
240 if (do_read(buf, 13) < 0)
243 if (memcmp(buf, "header_event", 13) != 0) {
244 pr_debug("did not read header event");
248 size = read8(pevent);
249 header_event = malloc(size);
250 if (header_event == NULL)
253 if (do_read(header_event, size) < 0)
260 static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
268 if (do_read(buf, size) < 0) {
273 parse_ftrace_file(pevent, buf, size);
278 static int read_event_file(struct pevent *pevent, char *sys,
279 unsigned long long size)
287 if (do_read(buf, size) < 0) {
292 parse_event_file(pevent, buf, size, sys);
297 static int read_ftrace_files(struct pevent *pevent)
299 unsigned long long size;
304 count = read4(pevent);
306 for (i = 0; i < count; i++) {
307 size = read8(pevent);
308 ret = read_ftrace_file(pevent, size);
315 static int read_event_files(struct pevent *pevent)
317 unsigned long long size;
324 systems = read4(pevent);
326 for (i = 0; i < systems; i++) {
331 count = read4(pevent);
333 for (x=0; x < count; x++) {
334 size = read8(pevent);
335 ret = read_event_file(pevent, sys, size);
343 ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
346 char test[] = { 23, 8, 68 };
348 int show_version = 0;
352 struct pevent *pevent;
360 if (do_read(buf, 3) < 0)
362 if (memcmp(buf, test, 3) != 0) {
363 pr_debug("no trace data in the file");
367 if (do_read(buf, 7) < 0)
369 if (memcmp(buf, "tracing", 7) != 0) {
370 pr_debug("not a trace file (missing 'tracing' tag)");
374 version = read_string();
378 printf("version = %s\n", version);
381 if (do_read(buf, 1) < 0)
383 file_bigendian = buf[0];
384 host_bigendian = bigendian();
386 pevent = read_trace_init(file_bigendian, host_bigendian);
387 if (pevent == NULL) {
388 pr_debug("read_trace_init failed");
392 if (do_read(buf, 1) < 0)
396 page_size = read4(pevent);
400 err = read_header_files(pevent);
403 err = read_ftrace_files(pevent);
406 err = read_event_files(pevent);
409 err = read_proc_kallsyms(pevent);
412 err = read_ftrace_printk(pevent);
416 size = trace_data_size;
420 pevent_print_funcs(pevent);
421 } else if (show_printk) {
422 pevent_print_printk(pevent);