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>
40 #include "trace-event.h"
50 static unsigned long page_size;
52 static ssize_t calc_data_size;
55 static void *malloc_or_die(int size)
65 static int do_read(int fd, void *buf, int size)
70 int ret = read(fd, buf, size);
76 int retw = write(STDOUT_FILENO, buf, ret);
78 if (retw <= 0 || retw != ret)
79 die("repiping input file");
89 static int read_or_die(void *data, int size)
93 r = do_read(input_fd, data, size);
95 die("reading input file (size expected=%d received=%d)",
104 /* If it fails, the next read will report it */
105 static void skip(int size)
111 r = size > BUFSIZ ? BUFSIZ : size;
117 static unsigned int read4(struct pevent *pevent)
121 read_or_die(&data, 4);
122 return __data2host4(pevent, data);
125 static unsigned long long read8(struct pevent *pevent)
127 unsigned long long data;
129 read_or_die(&data, 8);
130 return __data2host8(pevent, data);
133 static char *read_string(void)
142 r = read(input_fd, &c, 1);
144 die("reading input file");
150 int retw = write(STDOUT_FILENO, &c, 1);
152 if (retw <= 0 || retw != r)
153 die("repiping input file string");
163 calc_data_size += size;
165 str = malloc_or_die(size);
166 memcpy(str, buf, size);
171 static void read_proc_kallsyms(struct pevent *pevent)
176 size = read4(pevent);
180 buf = malloc_or_die(size + 1);
181 read_or_die(buf, size);
184 parse_proc_kallsyms(pevent, buf, size);
189 static void read_ftrace_printk(struct pevent *pevent)
194 size = read4(pevent);
198 buf = malloc_or_die(size);
199 read_or_die(buf, size);
201 parse_ftrace_printk(pevent, buf, size);
206 static void read_header_files(struct pevent *pevent)
208 unsigned long long size;
212 read_or_die(buf, 12);
214 if (memcmp(buf, "header_page", 12) != 0)
215 die("did not read header page");
217 size = read8(pevent);
221 * The size field in the page is of type long,
222 * use that instead, since it represents the kernel.
224 long_size = header_page_size_size;
226 read_or_die(buf, 13);
227 if (memcmp(buf, "header_event", 13) != 0)
228 die("did not read header event");
230 size = read8(pevent);
231 header_event = malloc_or_die(size);
232 read_or_die(header_event, size);
236 static void read_ftrace_file(struct pevent *pevent, unsigned long long size)
240 buf = malloc_or_die(size);
241 read_or_die(buf, size);
242 parse_ftrace_file(pevent, buf, size);
246 static void read_event_file(struct pevent *pevent, char *sys,
247 unsigned long long size)
251 buf = malloc_or_die(size);
252 read_or_die(buf, size);
253 parse_event_file(pevent, buf, size, sys);
257 static void read_ftrace_files(struct pevent *pevent)
259 unsigned long long size;
263 count = read4(pevent);
265 for (i = 0; i < count; i++) {
266 size = read8(pevent);
267 read_ftrace_file(pevent, size);
271 static void read_event_files(struct pevent *pevent)
273 unsigned long long size;
279 systems = read4(pevent);
281 for (i = 0; i < systems; i++) {
284 count = read4(pevent);
285 for (x=0; x < count; x++) {
286 size = read8(pevent);
287 read_event_file(pevent, sys, size);
293 unsigned long long offset;
294 unsigned long long size;
295 unsigned long long timestamp;
296 struct pevent_record *next;
303 static struct cpu_data *cpu_data;
305 static void update_cpu_data_index(int cpu)
307 cpu_data[cpu].offset += page_size;
308 cpu_data[cpu].size -= page_size;
309 cpu_data[cpu].index = 0;
312 static void get_next_page(int cpu)
317 if (!cpu_data[cpu].page)
321 if (cpu_data[cpu].size <= page_size) {
322 free(cpu_data[cpu].page);
323 cpu_data[cpu].page = NULL;
327 update_cpu_data_index(cpu);
329 /* other parts of the code may expect the pointer to not move */
330 save_seek = lseek(input_fd, 0, SEEK_CUR);
332 ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
333 if (ret == (off_t)-1)
334 die("failed to lseek");
335 ret = read(input_fd, cpu_data[cpu].page, page_size);
337 die("failed to read page");
339 /* reset the file pointer back */
340 lseek(input_fd, save_seek, SEEK_SET);
345 munmap(cpu_data[cpu].page, page_size);
346 cpu_data[cpu].page = NULL;
348 if (cpu_data[cpu].size <= page_size)
351 update_cpu_data_index(cpu);
353 cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
354 input_fd, cpu_data[cpu].offset);
355 if (cpu_data[cpu].page == MAP_FAILED)
356 die("failed to mmap cpu %d at offset 0x%llx",
357 cpu, cpu_data[cpu].offset);
360 static unsigned int type_len4host(unsigned int type_len_ts)
363 return (type_len_ts >> 27) & ((1 << 5) - 1);
365 return type_len_ts & ((1 << 5) - 1);
368 static unsigned int ts4host(unsigned int type_len_ts)
371 return type_len_ts & ((1 << 27) - 1);
373 return type_len_ts >> 5;
376 static int calc_index(void *ptr, int cpu)
378 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
381 struct pevent_record *trace_peek_data(struct pevent *pevent, int cpu)
383 struct pevent_record *data;
384 void *page = cpu_data[cpu].page;
385 int idx = cpu_data[cpu].index;
386 void *ptr = page + idx;
387 unsigned long long extend;
388 unsigned int type_len_ts;
389 unsigned int type_len;
391 unsigned int length = 0;
393 if (cpu_data[cpu].next)
394 return cpu_data[cpu].next;
400 /* FIXME: handle header page */
401 if (header_page_ts_size != 8)
402 die("expected a long long type for timestamp");
403 cpu_data[cpu].timestamp = data2host8(pevent, ptr);
405 switch (header_page_size_size) {
407 cpu_data[cpu].page_size = data2host4(pevent, ptr);
411 cpu_data[cpu].page_size = data2host8(pevent, ptr);
415 die("bad long size");
417 ptr = cpu_data[cpu].page + header_page_data_offset;
421 idx = calc_index(ptr, cpu);
423 if (idx >= cpu_data[cpu].page_size) {
425 return trace_peek_data(pevent, cpu);
428 type_len_ts = data2host4(pevent, ptr);
431 type_len = type_len4host(type_len_ts);
432 delta = ts4host(type_len_ts);
435 case RINGBUF_TYPE_PADDING:
437 die("error, hit unexpected end of page");
438 length = data2host4(pevent, ptr);
444 case RINGBUF_TYPE_TIME_EXTEND:
445 extend = data2host4(pevent, ptr);
449 cpu_data[cpu].timestamp += extend;
452 case RINGBUF_TYPE_TIME_STAMP:
456 length = data2host4(pevent, ptr);
458 die("here! length=%d", length);
461 length = type_len * 4;
465 cpu_data[cpu].timestamp += delta;
467 data = malloc_or_die(sizeof(*data));
468 memset(data, 0, sizeof(*data));
470 data->ts = cpu_data[cpu].timestamp;
475 cpu_data[cpu].index = calc_index(ptr, cpu);
476 cpu_data[cpu].next = data;
481 struct pevent_record *trace_read_data(struct pevent *pevent, int cpu)
483 struct pevent_record *data;
485 data = trace_peek_data(pevent, cpu);
486 cpu_data[cpu].next = NULL;
491 ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
494 char test[] = { 23, 8, 68 };
496 int show_version = 0;
507 if (memcmp(buf, test, 3) != 0)
508 die("no trace data in the file");
511 if (memcmp(buf, "tracing", 7) != 0)
512 die("not a trace file (missing 'tracing' tag)");
514 version = read_string();
516 printf("version = %s\n", version);
520 file_bigendian = buf[0];
521 host_bigendian = bigendian();
523 *ppevent = read_trace_init(file_bigendian, host_bigendian);
524 if (*ppevent == NULL)
525 die("read_trace_init failed");
530 page_size = read4(*ppevent);
532 read_header_files(*ppevent);
534 read_ftrace_files(*ppevent);
535 read_event_files(*ppevent);
536 read_proc_kallsyms(*ppevent);
537 read_ftrace_printk(*ppevent);
539 size = calc_data_size - 1;
544 pevent_print_funcs(*ppevent);
548 pevent_print_printk(*ppevent);