blob: 43f19c1fed3a0cea6e93853eaf9d8b500ddb3127 [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;
Tom Zanussi454c4072010-05-01 01:41:20 -050054static bool repipe;
Tom Zanussi92155452010-04-01 23:59:21 -050055
56static int do_read(int fd, void *buf, int size)
57{
58 int rsize = size;
59
60 while (size) {
61 int ret = read(fd, buf, size);
62
63 if (ret <= 0)
64 return -1;
65
Tom Zanussi454c4072010-05-01 01:41:20 -050066 if (repipe) {
67 int retw = write(STDOUT_FILENO, buf, ret);
68
69 if (retw <= 0 || retw != ret)
70 die("repiping input file");
71 }
72
Tom Zanussi92155452010-04-01 23:59:21 -050073 size -= ret;
74 buf += ret;
75 }
76
77 return rsize;
78}
79
Steven Rostedt538bafb2009-08-17 16:18:06 +020080static int read_or_die(void *data, int size)
81{
82 int r;
83
Tom Zanussi92155452010-04-01 23:59:21 -050084 r = do_read(input_fd, data, size);
85 if (r <= 0)
Steven Rostedt538bafb2009-08-17 16:18:06 +020086 die("reading input file (size expected=%d received=%d)",
87 size, r);
Tom Zanussi92155452010-04-01 23:59:21 -050088
89 if (calc_data_size)
90 calc_data_size += r;
91
Steven Rostedt538bafb2009-08-17 16:18:06 +020092 return r;
93}
94
95static unsigned int read4(void)
96{
97 unsigned int data;
98
99 read_or_die(&data, 4);
100 return __data2host4(data);
101}
102
103static unsigned long long read8(void)
104{
105 unsigned long long data;
106
107 read_or_die(&data, 8);
108 return __data2host8(data);
109}
110
111static char *read_string(void)
112{
113 char buf[BUFSIZ];
114 char *str = NULL;
115 int size = 0;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800116 off_t r;
Tom Zanussi92155452010-04-01 23:59:21 -0500117 char c;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200118
119 for (;;) {
Tom Zanussi92155452010-04-01 23:59:21 -0500120 r = read(input_fd, &c, 1);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200121 if (r < 0)
122 die("reading input file");
123
124 if (!r)
125 die("no data");
126
Tom Zanussi454c4072010-05-01 01:41:20 -0500127 if (repipe) {
128 int retw = write(STDOUT_FILENO, &c, 1);
129
130 if (retw <= 0 || retw != r)
131 die("repiping input file string");
132 }
133
Tom Zanussi92155452010-04-01 23:59:21 -0500134 buf[size++] = c;
135
136 if (!c)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200137 break;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200138 }
139
Tom Zanussi92155452010-04-01 23:59:21 -0500140 if (calc_data_size)
141 calc_data_size += size;
Ingo Molnar6f4596d2009-09-03 16:22:45 +0200142
Tom Zanussi92155452010-04-01 23:59:21 -0500143 str = malloc_or_die(size);
144 memcpy(str, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200145
146 return str;
147}
148
149static void read_proc_kallsyms(void)
150{
151 unsigned int size;
152 char *buf;
153
154 size = read4();
155 if (!size)
156 return;
157
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900158 buf = malloc_or_die(size + 1);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200159 read_or_die(buf, size);
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900160 buf[size] = '\0';
Steven Rostedt538bafb2009-08-17 16:18:06 +0200161
162 parse_proc_kallsyms(buf, size);
163
164 free(buf);
165}
166
167static void read_ftrace_printk(void)
168{
169 unsigned int size;
170 char *buf;
171
172 size = read4();
173 if (!size)
174 return;
175
176 buf = malloc_or_die(size);
177 read_or_die(buf, size);
178
179 parse_ftrace_printk(buf, size);
180
181 free(buf);
182}
183
184static void read_header_files(void)
185{
186 unsigned long long size;
187 char *header_page;
188 char *header_event;
189 char buf[BUFSIZ];
190
191 read_or_die(buf, 12);
192
193 if (memcmp(buf, "header_page", 12) != 0)
194 die("did not read header page");
195
196 size = read8();
197 header_page = malloc_or_die(size);
198 read_or_die(header_page, size);
199 parse_header_page(header_page, size);
200 free(header_page);
201
202 /*
203 * The size field in the page is of type long,
204 * use that instead, since it represents the kernel.
205 */
206 long_size = header_page_size_size;
207
208 read_or_die(buf, 13);
209 if (memcmp(buf, "header_event", 13) != 0)
210 die("did not read header event");
211
212 size = read8();
213 header_event = malloc_or_die(size);
214 read_or_die(header_event, size);
215 free(header_event);
216}
217
218static void read_ftrace_file(unsigned long long size)
219{
220 char *buf;
221
222 buf = malloc_or_die(size);
223 read_or_die(buf, size);
224 parse_ftrace_file(buf, size);
225 free(buf);
226}
227
228static void read_event_file(char *sys, unsigned long long size)
229{
230 char *buf;
231
232 buf = malloc_or_die(size);
233 read_or_die(buf, size);
234 parse_event_file(buf, size, sys);
235 free(buf);
236}
237
238static void read_ftrace_files(void)
239{
240 unsigned long long size;
241 int count;
242 int i;
243
244 count = read4();
245
246 for (i = 0; i < count; i++) {
247 size = read8();
248 read_ftrace_file(size);
249 }
250}
251
252static void read_event_files(void)
253{
254 unsigned long long size;
255 char *sys;
256 int systems;
257 int count;
258 int i,x;
259
260 systems = read4();
261
262 for (i = 0; i < systems; i++) {
263 sys = read_string();
264
265 count = read4();
266 for (x=0; x < count; x++) {
267 size = read8();
268 read_event_file(sys, size);
269 }
270 }
271}
272
273struct cpu_data {
274 unsigned long long offset;
275 unsigned long long size;
276 unsigned long long timestamp;
277 struct record *next;
278 char *page;
279 int cpu;
280 int index;
281 int page_size;
282};
283
284static struct cpu_data *cpu_data;
285
286static void update_cpu_data_index(int cpu)
287{
288 cpu_data[cpu].offset += page_size;
289 cpu_data[cpu].size -= page_size;
290 cpu_data[cpu].index = 0;
291}
292
293static void get_next_page(int cpu)
294{
Xiao Guangrongf887f302010-02-04 16:46:42 +0800295 off_t save_seek;
296 off_t ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200297
298 if (!cpu_data[cpu].page)
299 return;
300
301 if (read_page) {
302 if (cpu_data[cpu].size <= page_size) {
303 free(cpu_data[cpu].page);
304 cpu_data[cpu].page = NULL;
305 return;
306 }
307
308 update_cpu_data_index(cpu);
309
310 /* other parts of the code may expect the pointer to not move */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800311 save_seek = lseek(input_fd, 0, SEEK_CUR);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200312
Xiao Guangrongf887f302010-02-04 16:46:42 +0800313 ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
314 if (ret == (off_t)-1)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200315 die("failed to lseek");
316 ret = read(input_fd, cpu_data[cpu].page, page_size);
317 if (ret < 0)
318 die("failed to read page");
319
320 /* reset the file pointer back */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800321 lseek(input_fd, save_seek, SEEK_SET);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200322
323 return;
324 }
325
326 munmap(cpu_data[cpu].page, page_size);
327 cpu_data[cpu].page = NULL;
328
329 if (cpu_data[cpu].size <= page_size)
330 return;
331
332 update_cpu_data_index(cpu);
333
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);
339}
340
341static unsigned int type_len4host(unsigned int type_len_ts)
342{
343 if (file_bigendian)
344 return (type_len_ts >> 27) & ((1 << 5) - 1);
345 else
346 return type_len_ts & ((1 << 5) - 1);
347}
348
349static unsigned int ts4host(unsigned int type_len_ts)
350{
351 if (file_bigendian)
352 return type_len_ts & ((1 << 27) - 1);
353 else
354 return type_len_ts >> 5;
355}
356
357static int calc_index(void *ptr, int cpu)
358{
359 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
360}
361
362struct record *trace_peek_data(int cpu)
363{
364 struct record *data;
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;
371 unsigned int delta;
372 unsigned int length = 0;
373
374 if (cpu_data[cpu].next)
375 return cpu_data[cpu].next;
376
377 if (!page)
378 return NULL;
379
380 if (!idx) {
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);
385 ptr += 8;
386 switch (header_page_size_size) {
387 case 4:
388 cpu_data[cpu].page_size = data2host4(ptr);
389 ptr += 4;
390 break;
391 case 8:
392 cpu_data[cpu].page_size = data2host8(ptr);
393 ptr += 8;
394 break;
395 default:
396 die("bad long size");
397 }
398 ptr = cpu_data[cpu].page + header_page_data_offset;
399 }
400
401read_again:
402 idx = calc_index(ptr, cpu);
403
404 if (idx >= cpu_data[cpu].page_size) {
405 get_next_page(cpu);
406 return trace_peek_data(cpu);
407 }
408
409 type_len_ts = data2host4(ptr);
410 ptr += 4;
411
412 type_len = type_len4host(type_len_ts);
413 delta = ts4host(type_len_ts);
414
415 switch (type_len) {
416 case RINGBUF_TYPE_PADDING:
417 if (!delta)
418 die("error, hit unexpected end of page");
419 length = data2host4(ptr);
420 ptr += 4;
421 length *= 4;
422 ptr += length;
423 goto read_again;
424
425 case RINGBUF_TYPE_TIME_EXTEND:
426 extend = data2host4(ptr);
427 ptr += 4;
428 extend <<= TS_SHIFT;
429 extend += delta;
430 cpu_data[cpu].timestamp += extend;
431 goto read_again;
432
433 case RINGBUF_TYPE_TIME_STAMP:
434 ptr += 12;
435 break;
436 case 0:
437 length = data2host4(ptr);
438 ptr += 4;
439 die("here! length=%d", length);
440 break;
441 default:
442 length = type_len * 4;
443 break;
444 }
445
446 cpu_data[cpu].timestamp += delta;
447
448 data = malloc_or_die(sizeof(*data));
449 memset(data, 0, sizeof(*data));
450
451 data->ts = cpu_data[cpu].timestamp;
452 data->size = length;
453 data->data = ptr;
454 ptr += length;
455
456 cpu_data[cpu].index = calc_index(ptr, cpu);
457 cpu_data[cpu].next = data;
458
459 return data;
460}
461
462struct record *trace_read_data(int cpu)
463{
464 struct record *data;
465
466 data = trace_peek_data(cpu);
467 cpu_data[cpu].next = NULL;
468
469 return data;
470}
471
Tom Zanussi454c4072010-05-01 01:41:20 -0500472ssize_t trace_report(int fd, bool __repipe)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200473{
Steven Rostedt538bafb2009-08-17 16:18:06 +0200474 char buf[BUFSIZ];
475 char test[] = { 23, 8, 68 };
476 char *version;
Ingo Molnard9340c12009-09-12 10:08:34 +0200477 int show_version = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200478 int show_funcs = 0;
479 int show_printk = 0;
Tom Zanussi92155452010-04-01 23:59:21 -0500480 ssize_t size;
481
482 calc_data_size = 1;
Tom Zanussi454c4072010-05-01 01:41:20 -0500483 repipe = __repipe;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200484
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200485 input_fd = fd;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200486
487 read_or_die(buf, 3);
488 if (memcmp(buf, test, 3) != 0)
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200489 die("no trace data in the file");
Steven Rostedt538bafb2009-08-17 16:18:06 +0200490
491 read_or_die(buf, 7);
492 if (memcmp(buf, "tracing", 7) != 0)
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200493 die("not a trace file (missing 'tracing' tag)");
Steven Rostedt538bafb2009-08-17 16:18:06 +0200494
495 version = read_string();
Ingo Molnard9340c12009-09-12 10:08:34 +0200496 if (show_version)
497 printf("version = %s\n", version);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200498 free(version);
499
500 read_or_die(buf, 1);
501 file_bigendian = buf[0];
502 host_bigendian = bigendian();
503
504 read_or_die(buf, 1);
505 long_size = buf[0];
506
507 page_size = read4();
508
509 read_header_files();
510
511 read_ftrace_files();
512 read_event_files();
513 read_proc_kallsyms();
514 read_ftrace_printk();
515
Tom Zanussi92155452010-04-01 23:59:21 -0500516 size = calc_data_size - 1;
517 calc_data_size = 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500518 repipe = false;
Tom Zanussi92155452010-04-01 23:59:21 -0500519
Steven Rostedt538bafb2009-08-17 16:18:06 +0200520 if (show_funcs) {
521 print_funcs();
Tom Zanussi92155452010-04-01 23:59:21 -0500522 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200523 }
524 if (show_printk) {
525 print_printk();
Tom Zanussi92155452010-04-01 23:59:21 -0500526 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200527 }
528
Tom Zanussi92155452010-04-01 23:59:21 -0500529 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200530}