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 _LARGEFILE64_SOURCE
29 #include <sys/types.h>
41 #include "trace-event.h"
51 static unsigned long page_size;
53 static int read_or_die(void *data, int size)
57 r = read(input_fd, data, size);
59 die("reading input file (size expected=%d received=%d)",
64 static unsigned int read4(void)
68 read_or_die(&data, 4);
69 return __data2host4(data);
72 static unsigned long long read8(void)
74 unsigned long long data;
76 read_or_die(&data, 8);
77 return __data2host8(data);
80 static char *read_string(void)
89 r = read(input_fd, buf, BUFSIZ);
91 die("reading input file");
96 for (i = 0; i < r; i++) {
105 str = realloc(str, size);
107 die("malloc of size %d", size);
108 memcpy(str + (size - BUFSIZ), buf, BUFSIZ);
111 str = malloc_or_die(size);
112 memcpy(str, buf, size);
119 /* move the file descriptor to the end of the string */
120 r = lseek(input_fd, -(r - i), SEEK_CUR);
126 str = realloc(str, size);
128 die("malloc of size %d", size);
129 memcpy(str + (size - i), buf, i);
132 str = malloc_or_die(i);
139 static void read_proc_kallsyms(void)
148 buf = malloc_or_die(size + 1);
149 read_or_die(buf, size);
152 parse_proc_kallsyms(buf, size);
157 static void read_ftrace_printk(void)
166 buf = malloc_or_die(size);
167 read_or_die(buf, size);
169 parse_ftrace_printk(buf, size);
174 static void read_header_files(void)
176 unsigned long long size;
181 read_or_die(buf, 12);
183 if (memcmp(buf, "header_page", 12) != 0)
184 die("did not read header page");
187 header_page = malloc_or_die(size);
188 read_or_die(header_page, size);
189 parse_header_page(header_page, size);
193 * The size field in the page is of type long,
194 * use that instead, since it represents the kernel.
196 long_size = header_page_size_size;
198 read_or_die(buf, 13);
199 if (memcmp(buf, "header_event", 13) != 0)
200 die("did not read header event");
203 header_event = malloc_or_die(size);
204 read_or_die(header_event, size);
208 static void read_ftrace_file(unsigned long long size)
212 buf = malloc_or_die(size);
213 read_or_die(buf, size);
214 parse_ftrace_file(buf, size);
218 static void read_event_file(char *sys, unsigned long long size)
222 buf = malloc_or_die(size);
223 read_or_die(buf, size);
224 parse_event_file(buf, size, sys);
228 static void read_ftrace_files(void)
230 unsigned long long size;
236 for (i = 0; i < count; i++) {
238 read_ftrace_file(size);
242 static void read_event_files(void)
244 unsigned long long size;
252 for (i = 0; i < systems; i++) {
256 for (x=0; x < count; x++) {
258 read_event_file(sys, size);
264 unsigned long long offset;
265 unsigned long long size;
266 unsigned long long timestamp;
274 static struct cpu_data *cpu_data;
276 static void update_cpu_data_index(int cpu)
278 cpu_data[cpu].offset += page_size;
279 cpu_data[cpu].size -= page_size;
280 cpu_data[cpu].index = 0;
283 static void get_next_page(int cpu)
288 if (!cpu_data[cpu].page)
292 if (cpu_data[cpu].size <= page_size) {
293 free(cpu_data[cpu].page);
294 cpu_data[cpu].page = NULL;
298 update_cpu_data_index(cpu);
300 /* other parts of the code may expect the pointer to not move */
301 save_seek = lseek64(input_fd, 0, SEEK_CUR);
303 ret = lseek64(input_fd, cpu_data[cpu].offset, SEEK_SET);
305 die("failed to lseek");
306 ret = read(input_fd, cpu_data[cpu].page, page_size);
308 die("failed to read page");
310 /* reset the file pointer back */
311 lseek64(input_fd, save_seek, SEEK_SET);
316 munmap(cpu_data[cpu].page, page_size);
317 cpu_data[cpu].page = NULL;
319 if (cpu_data[cpu].size <= page_size)
322 update_cpu_data_index(cpu);
324 cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
325 input_fd, cpu_data[cpu].offset);
326 if (cpu_data[cpu].page == MAP_FAILED)
327 die("failed to mmap cpu %d at offset 0x%llx",
328 cpu, cpu_data[cpu].offset);
331 static unsigned int type_len4host(unsigned int type_len_ts)
334 return (type_len_ts >> 27) & ((1 << 5) - 1);
336 return type_len_ts & ((1 << 5) - 1);
339 static unsigned int ts4host(unsigned int type_len_ts)
342 return type_len_ts & ((1 << 27) - 1);
344 return type_len_ts >> 5;
347 static int calc_index(void *ptr, int cpu)
349 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
352 struct record *trace_peek_data(int cpu)
355 void *page = cpu_data[cpu].page;
356 int idx = cpu_data[cpu].index;
357 void *ptr = page + idx;
358 unsigned long long extend;
359 unsigned int type_len_ts;
360 unsigned int type_len;
362 unsigned int length = 0;
364 if (cpu_data[cpu].next)
365 return cpu_data[cpu].next;
371 /* FIXME: handle header page */
372 if (header_page_ts_size != 8)
373 die("expected a long long type for timestamp");
374 cpu_data[cpu].timestamp = data2host8(ptr);
376 switch (header_page_size_size) {
378 cpu_data[cpu].page_size = data2host4(ptr);
382 cpu_data[cpu].page_size = data2host8(ptr);
386 die("bad long size");
388 ptr = cpu_data[cpu].page + header_page_data_offset;
392 idx = calc_index(ptr, cpu);
394 if (idx >= cpu_data[cpu].page_size) {
396 return trace_peek_data(cpu);
399 type_len_ts = data2host4(ptr);
402 type_len = type_len4host(type_len_ts);
403 delta = ts4host(type_len_ts);
406 case RINGBUF_TYPE_PADDING:
408 die("error, hit unexpected end of page");
409 length = data2host4(ptr);
415 case RINGBUF_TYPE_TIME_EXTEND:
416 extend = data2host4(ptr);
420 cpu_data[cpu].timestamp += extend;
423 case RINGBUF_TYPE_TIME_STAMP:
427 length = data2host4(ptr);
429 die("here! length=%d", length);
432 length = type_len * 4;
436 cpu_data[cpu].timestamp += delta;
438 data = malloc_or_die(sizeof(*data));
439 memset(data, 0, sizeof(*data));
441 data->ts = cpu_data[cpu].timestamp;
446 cpu_data[cpu].index = calc_index(ptr, cpu);
447 cpu_data[cpu].next = data;
452 struct record *trace_read_data(int cpu)
456 data = trace_peek_data(cpu);
457 cpu_data[cpu].next = NULL;
462 void trace_report(int fd)
465 char test[] = { 23, 8, 68 };
467 int show_version = 0;
474 if (memcmp(buf, test, 3) != 0)
475 die("no trace data in the file");
478 if (memcmp(buf, "tracing", 7) != 0)
479 die("not a trace file (missing 'tracing' tag)");
481 version = read_string();
483 printf("version = %s\n", version);
487 file_bigendian = buf[0];
488 host_bigendian = bigendian();
499 read_proc_kallsyms();
500 read_ftrace_printk();