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;
55 static int do_read(int fd, void *buf, int size)
60 int ret = read(fd, buf, size);
72 static int read_or_die(void *data, int size)
76 r = do_read(input_fd, data, size);
78 die("reading input file (size expected=%d received=%d)",
87 static unsigned int read4(void)
91 read_or_die(&data, 4);
92 return __data2host4(data);
95 static unsigned long long read8(void)
97 unsigned long long data;
99 read_or_die(&data, 8);
100 return __data2host8(data);
103 static char *read_string(void)
112 r = read(input_fd, &c, 1);
114 die("reading input file");
126 calc_data_size += size;
128 str = malloc_or_die(size);
129 memcpy(str, buf, size);
134 static void read_proc_kallsyms(void)
143 buf = malloc_or_die(size + 1);
144 read_or_die(buf, size);
147 parse_proc_kallsyms(buf, size);
152 static void read_ftrace_printk(void)
161 buf = malloc_or_die(size);
162 read_or_die(buf, size);
164 parse_ftrace_printk(buf, size);
169 static void read_header_files(void)
171 unsigned long long size;
176 read_or_die(buf, 12);
178 if (memcmp(buf, "header_page", 12) != 0)
179 die("did not read header page");
182 header_page = malloc_or_die(size);
183 read_or_die(header_page, size);
184 parse_header_page(header_page, size);
188 * The size field in the page is of type long,
189 * use that instead, since it represents the kernel.
191 long_size = header_page_size_size;
193 read_or_die(buf, 13);
194 if (memcmp(buf, "header_event", 13) != 0)
195 die("did not read header event");
198 header_event = malloc_or_die(size);
199 read_or_die(header_event, size);
203 static void read_ftrace_file(unsigned long long size)
207 buf = malloc_or_die(size);
208 read_or_die(buf, size);
209 parse_ftrace_file(buf, size);
213 static void read_event_file(char *sys, unsigned long long size)
217 buf = malloc_or_die(size);
218 read_or_die(buf, size);
219 parse_event_file(buf, size, sys);
223 static void read_ftrace_files(void)
225 unsigned long long size;
231 for (i = 0; i < count; i++) {
233 read_ftrace_file(size);
237 static void read_event_files(void)
239 unsigned long long size;
247 for (i = 0; i < systems; i++) {
251 for (x=0; x < count; x++) {
253 read_event_file(sys, size);
259 unsigned long long offset;
260 unsigned long long size;
261 unsigned long long timestamp;
269 static struct cpu_data *cpu_data;
271 static void update_cpu_data_index(int cpu)
273 cpu_data[cpu].offset += page_size;
274 cpu_data[cpu].size -= page_size;
275 cpu_data[cpu].index = 0;
278 static void get_next_page(int cpu)
283 if (!cpu_data[cpu].page)
287 if (cpu_data[cpu].size <= page_size) {
288 free(cpu_data[cpu].page);
289 cpu_data[cpu].page = NULL;
293 update_cpu_data_index(cpu);
295 /* other parts of the code may expect the pointer to not move */
296 save_seek = lseek(input_fd, 0, SEEK_CUR);
298 ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
299 if (ret == (off_t)-1)
300 die("failed to lseek");
301 ret = read(input_fd, cpu_data[cpu].page, page_size);
303 die("failed to read page");
305 /* reset the file pointer back */
306 lseek(input_fd, save_seek, SEEK_SET);
311 munmap(cpu_data[cpu].page, page_size);
312 cpu_data[cpu].page = NULL;
314 if (cpu_data[cpu].size <= page_size)
317 update_cpu_data_index(cpu);
319 cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
320 input_fd, cpu_data[cpu].offset);
321 if (cpu_data[cpu].page == MAP_FAILED)
322 die("failed to mmap cpu %d at offset 0x%llx",
323 cpu, cpu_data[cpu].offset);
326 static unsigned int type_len4host(unsigned int type_len_ts)
329 return (type_len_ts >> 27) & ((1 << 5) - 1);
331 return type_len_ts & ((1 << 5) - 1);
334 static unsigned int ts4host(unsigned int type_len_ts)
337 return type_len_ts & ((1 << 27) - 1);
339 return type_len_ts >> 5;
342 static int calc_index(void *ptr, int cpu)
344 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
347 struct record *trace_peek_data(int cpu)
350 void *page = cpu_data[cpu].page;
351 int idx = cpu_data[cpu].index;
352 void *ptr = page + idx;
353 unsigned long long extend;
354 unsigned int type_len_ts;
355 unsigned int type_len;
357 unsigned int length = 0;
359 if (cpu_data[cpu].next)
360 return cpu_data[cpu].next;
366 /* FIXME: handle header page */
367 if (header_page_ts_size != 8)
368 die("expected a long long type for timestamp");
369 cpu_data[cpu].timestamp = data2host8(ptr);
371 switch (header_page_size_size) {
373 cpu_data[cpu].page_size = data2host4(ptr);
377 cpu_data[cpu].page_size = data2host8(ptr);
381 die("bad long size");
383 ptr = cpu_data[cpu].page + header_page_data_offset;
387 idx = calc_index(ptr, cpu);
389 if (idx >= cpu_data[cpu].page_size) {
391 return trace_peek_data(cpu);
394 type_len_ts = data2host4(ptr);
397 type_len = type_len4host(type_len_ts);
398 delta = ts4host(type_len_ts);
401 case RINGBUF_TYPE_PADDING:
403 die("error, hit unexpected end of page");
404 length = data2host4(ptr);
410 case RINGBUF_TYPE_TIME_EXTEND:
411 extend = data2host4(ptr);
415 cpu_data[cpu].timestamp += extend;
418 case RINGBUF_TYPE_TIME_STAMP:
422 length = data2host4(ptr);
424 die("here! length=%d", length);
427 length = type_len * 4;
431 cpu_data[cpu].timestamp += delta;
433 data = malloc_or_die(sizeof(*data));
434 memset(data, 0, sizeof(*data));
436 data->ts = cpu_data[cpu].timestamp;
441 cpu_data[cpu].index = calc_index(ptr, cpu);
442 cpu_data[cpu].next = data;
447 struct record *trace_read_data(int cpu)
451 data = trace_peek_data(cpu);
452 cpu_data[cpu].next = NULL;
457 ssize_t trace_report(int fd)
460 char test[] = { 23, 8, 68 };
462 int show_version = 0;
472 if (memcmp(buf, test, 3) != 0)
473 die("no trace data in the file");
476 if (memcmp(buf, "tracing", 7) != 0)
477 die("not a trace file (missing 'tracing' tag)");
479 version = read_string();
481 printf("version = %s\n", version);
485 file_bigendian = buf[0];
486 host_bigendian = bigendian();
497 read_proc_kallsyms();
498 read_ftrace_printk();
500 size = calc_data_size - 1;