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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #define _FILE_OFFSET_BITS 64
29 #include <sys/types.h>
41 #include "trace-event.h"
51 static unsigned long page_size;
53 static ssize_t calc_data_size;
56 /* If it fails, the next read will report it */
57 static void skip(int size)
59 lseek(input_fd, size, SEEK_CUR);
62 static int do_read(int fd, void *buf, int size)
67 int ret = read(fd, buf, size);
73 int retw = write(STDOUT_FILENO, buf, ret);
75 if (retw <= 0 || retw != ret)
76 die("repiping input file");
86 static int read_or_die(void *data, int size)
90 r = do_read(input_fd, data, size);
92 die("reading input file (size expected=%d received=%d)",
101 static unsigned int read4(void)
105 read_or_die(&data, 4);
106 return __data2host4(data);
109 static unsigned long long read8(void)
111 unsigned long long data;
113 read_or_die(&data, 8);
114 return __data2host8(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;
149 str = malloc_or_die(size);
150 memcpy(str, buf, size);
155 static void read_proc_kallsyms(void)
164 buf = malloc_or_die(size + 1);
165 read_or_die(buf, size);
168 parse_proc_kallsyms(buf, size);
173 static void read_ftrace_printk(void)
182 buf = malloc_or_die(size);
183 read_or_die(buf, size);
185 parse_ftrace_printk(buf, size);
190 static void read_header_files(void)
192 unsigned long long size;
196 read_or_die(buf, 12);
198 if (memcmp(buf, "header_page", 12) != 0)
199 die("did not read header page");
205 * The size field in the page is of type long,
206 * use that instead, since it represents the kernel.
208 long_size = header_page_size_size;
210 read_or_die(buf, 13);
211 if (memcmp(buf, "header_event", 13) != 0)
212 die("did not read header event");
215 header_event = malloc_or_die(size);
216 read_or_die(header_event, size);
220 static void read_ftrace_file(unsigned long long size)
224 buf = malloc_or_die(size);
225 read_or_die(buf, size);
226 parse_ftrace_file(buf, size);
230 static void read_event_file(char *sys, unsigned long long size)
234 buf = malloc_or_die(size);
235 read_or_die(buf, size);
236 parse_event_file(buf, size, sys);
240 static void read_ftrace_files(void)
242 unsigned long long size;
248 for (i = 0; i < count; i++) {
250 read_ftrace_file(size);
254 static void read_event_files(void)
256 unsigned long long size;
264 for (i = 0; i < systems; i++) {
268 for (x=0; x < count; x++) {
270 read_event_file(sys, size);
276 unsigned long long offset;
277 unsigned long long size;
278 unsigned long long timestamp;
286 static struct cpu_data *cpu_data;
288 static void update_cpu_data_index(int cpu)
290 cpu_data[cpu].offset += page_size;
291 cpu_data[cpu].size -= page_size;
292 cpu_data[cpu].index = 0;
295 static void get_next_page(int cpu)
300 if (!cpu_data[cpu].page)
304 if (cpu_data[cpu].size <= page_size) {
305 free(cpu_data[cpu].page);
306 cpu_data[cpu].page = NULL;
310 update_cpu_data_index(cpu);
312 /* other parts of the code may expect the pointer to not move */
313 save_seek = lseek(input_fd, 0, SEEK_CUR);
315 ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
316 if (ret == (off_t)-1)
317 die("failed to lseek");
318 ret = read(input_fd, cpu_data[cpu].page, page_size);
320 die("failed to read page");
322 /* reset the file pointer back */
323 lseek(input_fd, save_seek, SEEK_SET);
328 munmap(cpu_data[cpu].page, page_size);
329 cpu_data[cpu].page = NULL;
331 if (cpu_data[cpu].size <= page_size)
334 update_cpu_data_index(cpu);
336 cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
337 input_fd, cpu_data[cpu].offset);
338 if (cpu_data[cpu].page == MAP_FAILED)
339 die("failed to mmap cpu %d at offset 0x%llx",
340 cpu, cpu_data[cpu].offset);
343 static unsigned int type_len4host(unsigned int type_len_ts)
346 return (type_len_ts >> 27) & ((1 << 5) - 1);
348 return type_len_ts & ((1 << 5) - 1);
351 static unsigned int ts4host(unsigned int type_len_ts)
354 return type_len_ts & ((1 << 27) - 1);
356 return type_len_ts >> 5;
359 static int calc_index(void *ptr, int cpu)
361 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
364 struct record *trace_peek_data(int cpu)
367 void *page = cpu_data[cpu].page;
368 int idx = cpu_data[cpu].index;
369 void *ptr = page + idx;
370 unsigned long long extend;
371 unsigned int type_len_ts;
372 unsigned int type_len;
374 unsigned int length = 0;
376 if (cpu_data[cpu].next)
377 return cpu_data[cpu].next;
383 /* FIXME: handle header page */
384 if (header_page_ts_size != 8)
385 die("expected a long long type for timestamp");
386 cpu_data[cpu].timestamp = data2host8(ptr);
388 switch (header_page_size_size) {
390 cpu_data[cpu].page_size = data2host4(ptr);
394 cpu_data[cpu].page_size = data2host8(ptr);
398 die("bad long size");
400 ptr = cpu_data[cpu].page + header_page_data_offset;
404 idx = calc_index(ptr, cpu);
406 if (idx >= cpu_data[cpu].page_size) {
408 return trace_peek_data(cpu);
411 type_len_ts = data2host4(ptr);
414 type_len = type_len4host(type_len_ts);
415 delta = ts4host(type_len_ts);
418 case RINGBUF_TYPE_PADDING:
420 die("error, hit unexpected end of page");
421 length = data2host4(ptr);
427 case RINGBUF_TYPE_TIME_EXTEND:
428 extend = data2host4(ptr);
432 cpu_data[cpu].timestamp += extend;
435 case RINGBUF_TYPE_TIME_STAMP:
439 length = data2host4(ptr);
441 die("here! length=%d", length);
444 length = type_len * 4;
448 cpu_data[cpu].timestamp += delta;
450 data = malloc_or_die(sizeof(*data));
451 memset(data, 0, sizeof(*data));
453 data->ts = cpu_data[cpu].timestamp;
458 cpu_data[cpu].index = calc_index(ptr, cpu);
459 cpu_data[cpu].next = data;
464 struct record *trace_read_data(int cpu)
468 data = trace_peek_data(cpu);
469 cpu_data[cpu].next = NULL;
474 ssize_t trace_report(int fd, bool __repipe)
477 char test[] = { 23, 8, 68 };
479 int show_version = 0;
490 if (memcmp(buf, test, 3) != 0)
491 die("no trace data in the file");
494 if (memcmp(buf, "tracing", 7) != 0)
495 die("not a trace file (missing 'tracing' tag)");
497 version = read_string();
499 printf("version = %s\n", version);
503 file_bigendian = buf[0];
504 host_bigendian = bigendian();
515 read_proc_kallsyms();
516 read_ftrace_printk();
518 size = calc_data_size - 1;