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 static int do_read(int fd, void *buf, int size)
61 int ret = read(fd, buf, size);
67 int retw = write(STDOUT_FILENO, buf, ret);
69 if (retw <= 0 || retw != ret)
70 die("repiping input file");
80 static int read_or_die(void *data, int size)
84 r = do_read(input_fd, data, size);
86 die("reading input file (size expected=%d received=%d)",
95 static unsigned int read4(void)
99 read_or_die(&data, 4);
100 return __data2host4(data);
103 static unsigned long long read8(void)
105 unsigned long long data;
107 read_or_die(&data, 8);
108 return __data2host8(data);
111 static char *read_string(void)
120 r = read(input_fd, &c, 1);
122 die("reading input file");
128 int retw = write(STDOUT_FILENO, &c, 1);
130 if (retw <= 0 || retw != r)
131 die("repiping input file string");
141 calc_data_size += size;
143 str = malloc_or_die(size);
144 memcpy(str, buf, size);
149 static void read_proc_kallsyms(void)
158 buf = malloc_or_die(size + 1);
159 read_or_die(buf, size);
162 parse_proc_kallsyms(buf, size);
167 static void read_ftrace_printk(void)
176 buf = malloc_or_die(size);
177 read_or_die(buf, size);
179 parse_ftrace_printk(buf, size);
184 static void read_header_files(void)
186 unsigned long long size;
191 read_or_die(buf, 12);
193 if (memcmp(buf, "header_page", 12) != 0)
194 die("did not read header page");
197 header_page = malloc_or_die(size);
198 read_or_die(header_page, size);
199 parse_header_page(header_page, size);
203 * The size field in the page is of type long,
204 * use that instead, since it represents the kernel.
206 long_size = header_page_size_size;
208 read_or_die(buf, 13);
209 if (memcmp(buf, "header_event", 13) != 0)
210 die("did not read header event");
213 header_event = malloc_or_die(size);
214 read_or_die(header_event, size);
218 static void read_ftrace_file(unsigned long long size)
222 buf = malloc_or_die(size);
223 read_or_die(buf, size);
224 parse_ftrace_file(buf, size);
228 static void read_event_file(char *sys, unsigned long long size)
232 buf = malloc_or_die(size);
233 read_or_die(buf, size);
234 parse_event_file(buf, size, sys);
238 static void read_ftrace_files(void)
240 unsigned long long size;
246 for (i = 0; i < count; i++) {
248 read_ftrace_file(size);
252 static void read_event_files(void)
254 unsigned long long size;
262 for (i = 0; i < systems; i++) {
266 for (x=0; x < count; x++) {
268 read_event_file(sys, size);
274 unsigned long long offset;
275 unsigned long long size;
276 unsigned long long timestamp;
284 static struct cpu_data *cpu_data;
286 static void update_cpu_data_index(int cpu)
288 cpu_data[cpu].offset += page_size;
289 cpu_data[cpu].size -= page_size;
290 cpu_data[cpu].index = 0;
293 static void get_next_page(int cpu)
298 if (!cpu_data[cpu].page)
302 if (cpu_data[cpu].size <= page_size) {
303 free(cpu_data[cpu].page);
304 cpu_data[cpu].page = NULL;
308 update_cpu_data_index(cpu);
310 /* other parts of the code may expect the pointer to not move */
311 save_seek = lseek(input_fd, 0, SEEK_CUR);
313 ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
314 if (ret == (off_t)-1)
315 die("failed to lseek");
316 ret = read(input_fd, cpu_data[cpu].page, page_size);
318 die("failed to read page");
320 /* reset the file pointer back */
321 lseek(input_fd, save_seek, SEEK_SET);
326 munmap(cpu_data[cpu].page, page_size);
327 cpu_data[cpu].page = NULL;
329 if (cpu_data[cpu].size <= page_size)
332 update_cpu_data_index(cpu);
334 cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
335 input_fd, cpu_data[cpu].offset);
336 if (cpu_data[cpu].page == MAP_FAILED)
337 die("failed to mmap cpu %d at offset 0x%llx",
338 cpu, cpu_data[cpu].offset);
341 static unsigned int type_len4host(unsigned int type_len_ts)
344 return (type_len_ts >> 27) & ((1 << 5) - 1);
346 return type_len_ts & ((1 << 5) - 1);
349 static unsigned int ts4host(unsigned int type_len_ts)
352 return type_len_ts & ((1 << 27) - 1);
354 return type_len_ts >> 5;
357 static int calc_index(void *ptr, int cpu)
359 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
362 struct record *trace_peek_data(int cpu)
365 void *page = cpu_data[cpu].page;
366 int idx = cpu_data[cpu].index;
367 void *ptr = page + idx;
368 unsigned long long extend;
369 unsigned int type_len_ts;
370 unsigned int type_len;
372 unsigned int length = 0;
374 if (cpu_data[cpu].next)
375 return cpu_data[cpu].next;
381 /* FIXME: handle header page */
382 if (header_page_ts_size != 8)
383 die("expected a long long type for timestamp");
384 cpu_data[cpu].timestamp = data2host8(ptr);
386 switch (header_page_size_size) {
388 cpu_data[cpu].page_size = data2host4(ptr);
392 cpu_data[cpu].page_size = data2host8(ptr);
396 die("bad long size");
398 ptr = cpu_data[cpu].page + header_page_data_offset;
402 idx = calc_index(ptr, cpu);
404 if (idx >= cpu_data[cpu].page_size) {
406 return trace_peek_data(cpu);
409 type_len_ts = data2host4(ptr);
412 type_len = type_len4host(type_len_ts);
413 delta = ts4host(type_len_ts);
416 case RINGBUF_TYPE_PADDING:
418 die("error, hit unexpected end of page");
419 length = data2host4(ptr);
425 case RINGBUF_TYPE_TIME_EXTEND:
426 extend = data2host4(ptr);
430 cpu_data[cpu].timestamp += extend;
433 case RINGBUF_TYPE_TIME_STAMP:
437 length = data2host4(ptr);
439 die("here! length=%d", length);
442 length = type_len * 4;
446 cpu_data[cpu].timestamp += delta;
448 data = malloc_or_die(sizeof(*data));
449 memset(data, 0, sizeof(*data));
451 data->ts = cpu_data[cpu].timestamp;
456 cpu_data[cpu].index = calc_index(ptr, cpu);
457 cpu_data[cpu].next = data;
462 struct record *trace_read_data(int cpu)
466 data = trace_peek_data(cpu);
467 cpu_data[cpu].next = NULL;
472 ssize_t trace_report(int fd, bool __repipe)
475 char test[] = { 23, 8, 68 };
477 int show_version = 0;
488 if (memcmp(buf, test, 3) != 0)
489 die("no trace data in the file");
492 if (memcmp(buf, "tracing", 7) != 0)
493 die("not a trace file (missing 'tracing' tag)");
495 version = read_string();
497 printf("version = %s\n", version);
501 file_bigendian = buf[0];
502 host_bigendian = bigendian();
513 read_proc_kallsyms();
514 read_ftrace_printk();
516 size = calc_data_size - 1;