blob: 46066391288acd37159e7a6a3e72b0f516439dc1 [file] [log] [blame]
Steven Rostedt538bafb2009-08-17 16:18:06 +02001/*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
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!)
9 *
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.
14 *
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
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
Xiao Guangrongf887f302010-02-04 16:46:42 +080021#define _FILE_OFFSET_BITS 64
Steven Rostedt538bafb2009-08-17 16:18:06 +020022
23#include <dirent.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <stdarg.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/wait.h>
32#include <sys/mman.h>
33#include <pthread.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <ctype.h>
37#include <errno.h>
38
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020039#include "../perf.h"
Steven Rostedt538bafb2009-08-17 16:18:06 +020040#include "util.h"
41#include "trace-event.h"
42
43static int input_fd;
44
45static int read_page;
46
47int file_bigendian;
48int host_bigendian;
49static int long_size;
50
51static unsigned long page_size;
52
Tom Zanussi92155452010-04-01 23:59:21 -050053static ssize_t calc_data_size;
54
Frederic Weisbeckerd00a47c2010-05-01 03:08:46 +020055/* If it fails, the next read will report it */
56static void skip(int size)
57{
58 lseek(input_fd, size, SEEK_CUR);
59}
60
Tom Zanussi92155452010-04-01 23:59:21 -050061static int do_read(int fd, void *buf, int size)
62{
63 int rsize = size;
64
65 while (size) {
66 int ret = read(fd, buf, size);
67
68 if (ret <= 0)
69 return -1;
70
71 size -= ret;
72 buf += ret;
73 }
74
75 return rsize;
76}
77
Steven Rostedt538bafb2009-08-17 16:18:06 +020078static int read_or_die(void *data, int size)
79{
80 int r;
81
Tom Zanussi92155452010-04-01 23:59:21 -050082 r = do_read(input_fd, data, size);
83 if (r <= 0)
Steven Rostedt538bafb2009-08-17 16:18:06 +020084 die("reading input file (size expected=%d received=%d)",
85 size, r);
Tom Zanussi92155452010-04-01 23:59:21 -050086
87 if (calc_data_size)
88 calc_data_size += r;
89
Steven Rostedt538bafb2009-08-17 16:18:06 +020090 return r;
91}
92
93static unsigned int read4(void)
94{
95 unsigned int data;
96
97 read_or_die(&data, 4);
98 return __data2host4(data);
99}
100
101static unsigned long long read8(void)
102{
103 unsigned long long data;
104
105 read_or_die(&data, 8);
106 return __data2host8(data);
107}
108
109static char *read_string(void)
110{
111 char buf[BUFSIZ];
112 char *str = NULL;
113 int size = 0;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800114 off_t r;
Tom Zanussi92155452010-04-01 23:59:21 -0500115 char c;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200116
117 for (;;) {
Tom Zanussi92155452010-04-01 23:59:21 -0500118 r = read(input_fd, &c, 1);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200119 if (r < 0)
120 die("reading input file");
121
122 if (!r)
123 die("no data");
124
Tom Zanussi92155452010-04-01 23:59:21 -0500125 buf[size++] = c;
126
127 if (!c)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200128 break;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200129 }
130
Tom Zanussi92155452010-04-01 23:59:21 -0500131 if (calc_data_size)
132 calc_data_size += size;
Ingo Molnar6f4596d2009-09-03 16:22:45 +0200133
Tom Zanussi92155452010-04-01 23:59:21 -0500134 str = malloc_or_die(size);
135 memcpy(str, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200136
137 return str;
138}
139
140static void read_proc_kallsyms(void)
141{
142 unsigned int size;
143 char *buf;
144
145 size = read4();
146 if (!size)
147 return;
148
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900149 buf = malloc_or_die(size + 1);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200150 read_or_die(buf, size);
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900151 buf[size] = '\0';
Steven Rostedt538bafb2009-08-17 16:18:06 +0200152
153 parse_proc_kallsyms(buf, size);
154
155 free(buf);
156}
157
158static void read_ftrace_printk(void)
159{
160 unsigned int size;
161 char *buf;
162
163 size = read4();
164 if (!size)
165 return;
166
167 buf = malloc_or_die(size);
168 read_or_die(buf, size);
169
170 parse_ftrace_printk(buf, size);
171
172 free(buf);
173}
174
175static void read_header_files(void)
176{
177 unsigned long long size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200178 char *header_event;
179 char buf[BUFSIZ];
180
181 read_or_die(buf, 12);
182
183 if (memcmp(buf, "header_page", 12) != 0)
184 die("did not read header page");
185
186 size = read8();
Frederic Weisbeckerd00a47c2010-05-01 03:08:46 +0200187 skip(size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200188
189 /*
190 * The size field in the page is of type long,
191 * use that instead, since it represents the kernel.
192 */
193 long_size = header_page_size_size;
194
195 read_or_die(buf, 13);
196 if (memcmp(buf, "header_event", 13) != 0)
197 die("did not read header event");
198
199 size = read8();
200 header_event = malloc_or_die(size);
201 read_or_die(header_event, size);
202 free(header_event);
203}
204
205static void read_ftrace_file(unsigned long long size)
206{
207 char *buf;
208
209 buf = malloc_or_die(size);
210 read_or_die(buf, size);
211 parse_ftrace_file(buf, size);
212 free(buf);
213}
214
215static void read_event_file(char *sys, unsigned long long size)
216{
217 char *buf;
218
219 buf = malloc_or_die(size);
220 read_or_die(buf, size);
221 parse_event_file(buf, size, sys);
222 free(buf);
223}
224
225static void read_ftrace_files(void)
226{
227 unsigned long long size;
228 int count;
229 int i;
230
231 count = read4();
232
233 for (i = 0; i < count; i++) {
234 size = read8();
235 read_ftrace_file(size);
236 }
237}
238
239static void read_event_files(void)
240{
241 unsigned long long size;
242 char *sys;
243 int systems;
244 int count;
245 int i,x;
246
247 systems = read4();
248
249 for (i = 0; i < systems; i++) {
250 sys = read_string();
251
252 count = read4();
253 for (x=0; x < count; x++) {
254 size = read8();
255 read_event_file(sys, size);
256 }
257 }
258}
259
260struct cpu_data {
261 unsigned long long offset;
262 unsigned long long size;
263 unsigned long long timestamp;
264 struct record *next;
265 char *page;
266 int cpu;
267 int index;
268 int page_size;
269};
270
271static struct cpu_data *cpu_data;
272
273static void update_cpu_data_index(int cpu)
274{
275 cpu_data[cpu].offset += page_size;
276 cpu_data[cpu].size -= page_size;
277 cpu_data[cpu].index = 0;
278}
279
280static void get_next_page(int cpu)
281{
Xiao Guangrongf887f302010-02-04 16:46:42 +0800282 off_t save_seek;
283 off_t ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200284
285 if (!cpu_data[cpu].page)
286 return;
287
288 if (read_page) {
289 if (cpu_data[cpu].size <= page_size) {
290 free(cpu_data[cpu].page);
291 cpu_data[cpu].page = NULL;
292 return;
293 }
294
295 update_cpu_data_index(cpu);
296
297 /* other parts of the code may expect the pointer to not move */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800298 save_seek = lseek(input_fd, 0, SEEK_CUR);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200299
Xiao Guangrongf887f302010-02-04 16:46:42 +0800300 ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
301 if (ret == (off_t)-1)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200302 die("failed to lseek");
303 ret = read(input_fd, cpu_data[cpu].page, page_size);
304 if (ret < 0)
305 die("failed to read page");
306
307 /* reset the file pointer back */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800308 lseek(input_fd, save_seek, SEEK_SET);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200309
310 return;
311 }
312
313 munmap(cpu_data[cpu].page, page_size);
314 cpu_data[cpu].page = NULL;
315
316 if (cpu_data[cpu].size <= page_size)
317 return;
318
319 update_cpu_data_index(cpu);
320
321 cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
322 input_fd, cpu_data[cpu].offset);
323 if (cpu_data[cpu].page == MAP_FAILED)
324 die("failed to mmap cpu %d at offset 0x%llx",
325 cpu, cpu_data[cpu].offset);
326}
327
328static unsigned int type_len4host(unsigned int type_len_ts)
329{
330 if (file_bigendian)
331 return (type_len_ts >> 27) & ((1 << 5) - 1);
332 else
333 return type_len_ts & ((1 << 5) - 1);
334}
335
336static unsigned int ts4host(unsigned int type_len_ts)
337{
338 if (file_bigendian)
339 return type_len_ts & ((1 << 27) - 1);
340 else
341 return type_len_ts >> 5;
342}
343
344static int calc_index(void *ptr, int cpu)
345{
346 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
347}
348
349struct record *trace_peek_data(int cpu)
350{
351 struct record *data;
352 void *page = cpu_data[cpu].page;
353 int idx = cpu_data[cpu].index;
354 void *ptr = page + idx;
355 unsigned long long extend;
356 unsigned int type_len_ts;
357 unsigned int type_len;
358 unsigned int delta;
359 unsigned int length = 0;
360
361 if (cpu_data[cpu].next)
362 return cpu_data[cpu].next;
363
364 if (!page)
365 return NULL;
366
367 if (!idx) {
368 /* FIXME: handle header page */
369 if (header_page_ts_size != 8)
370 die("expected a long long type for timestamp");
371 cpu_data[cpu].timestamp = data2host8(ptr);
372 ptr += 8;
373 switch (header_page_size_size) {
374 case 4:
375 cpu_data[cpu].page_size = data2host4(ptr);
376 ptr += 4;
377 break;
378 case 8:
379 cpu_data[cpu].page_size = data2host8(ptr);
380 ptr += 8;
381 break;
382 default:
383 die("bad long size");
384 }
385 ptr = cpu_data[cpu].page + header_page_data_offset;
386 }
387
388read_again:
389 idx = calc_index(ptr, cpu);
390
391 if (idx >= cpu_data[cpu].page_size) {
392 get_next_page(cpu);
393 return trace_peek_data(cpu);
394 }
395
396 type_len_ts = data2host4(ptr);
397 ptr += 4;
398
399 type_len = type_len4host(type_len_ts);
400 delta = ts4host(type_len_ts);
401
402 switch (type_len) {
403 case RINGBUF_TYPE_PADDING:
404 if (!delta)
405 die("error, hit unexpected end of page");
406 length = data2host4(ptr);
407 ptr += 4;
408 length *= 4;
409 ptr += length;
410 goto read_again;
411
412 case RINGBUF_TYPE_TIME_EXTEND:
413 extend = data2host4(ptr);
414 ptr += 4;
415 extend <<= TS_SHIFT;
416 extend += delta;
417 cpu_data[cpu].timestamp += extend;
418 goto read_again;
419
420 case RINGBUF_TYPE_TIME_STAMP:
421 ptr += 12;
422 break;
423 case 0:
424 length = data2host4(ptr);
425 ptr += 4;
426 die("here! length=%d", length);
427 break;
428 default:
429 length = type_len * 4;
430 break;
431 }
432
433 cpu_data[cpu].timestamp += delta;
434
435 data = malloc_or_die(sizeof(*data));
436 memset(data, 0, sizeof(*data));
437
438 data->ts = cpu_data[cpu].timestamp;
439 data->size = length;
440 data->data = ptr;
441 ptr += length;
442
443 cpu_data[cpu].index = calc_index(ptr, cpu);
444 cpu_data[cpu].next = data;
445
446 return data;
447}
448
449struct record *trace_read_data(int cpu)
450{
451 struct record *data;
452
453 data = trace_peek_data(cpu);
454 cpu_data[cpu].next = NULL;
455
456 return data;
457}
458
Tom Zanussi92155452010-04-01 23:59:21 -0500459ssize_t trace_report(int fd)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200460{
Steven Rostedt538bafb2009-08-17 16:18:06 +0200461 char buf[BUFSIZ];
462 char test[] = { 23, 8, 68 };
463 char *version;
Ingo Molnard9340c12009-09-12 10:08:34 +0200464 int show_version = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200465 int show_funcs = 0;
466 int show_printk = 0;
Tom Zanussi92155452010-04-01 23:59:21 -0500467 ssize_t size;
468
469 calc_data_size = 1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200470
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200471 input_fd = fd;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200472
473 read_or_die(buf, 3);
474 if (memcmp(buf, test, 3) != 0)
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200475 die("no trace data in the file");
Steven Rostedt538bafb2009-08-17 16:18:06 +0200476
477 read_or_die(buf, 7);
478 if (memcmp(buf, "tracing", 7) != 0)
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200479 die("not a trace file (missing 'tracing' tag)");
Steven Rostedt538bafb2009-08-17 16:18:06 +0200480
481 version = read_string();
Ingo Molnard9340c12009-09-12 10:08:34 +0200482 if (show_version)
483 printf("version = %s\n", version);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200484 free(version);
485
486 read_or_die(buf, 1);
487 file_bigendian = buf[0];
488 host_bigendian = bigendian();
489
490 read_or_die(buf, 1);
491 long_size = buf[0];
492
493 page_size = read4();
494
495 read_header_files();
496
497 read_ftrace_files();
498 read_event_files();
499 read_proc_kallsyms();
500 read_ftrace_printk();
501
Tom Zanussi92155452010-04-01 23:59:21 -0500502 size = calc_data_size - 1;
503 calc_data_size = 0;
504
Steven Rostedt538bafb2009-08-17 16:18:06 +0200505 if (show_funcs) {
506 print_funcs();
Tom Zanussi92155452010-04-01 23:59:21 -0500507 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200508 }
509 if (show_printk) {
510 print_printk();
Tom Zanussi92155452010-04-01 23:59:21 -0500511 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200512 }
513
Tom Zanussi92155452010-04-01 23:59:21 -0500514 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200515}