perf inject: Refactor read_buildid function
[safe/jmp/linux-2.6] / tools / perf / util / trace-event-read.c
1 /*
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  */
21 #define _FILE_OFFSET_BITS 64
22
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
39 #include "../perf.h"
40 #include "util.h"
41 #include "trace-event.h"
42
43 static int input_fd;
44
45 static int read_page;
46
47 int file_bigendian;
48 int host_bigendian;
49 static int long_size;
50
51 static unsigned long    page_size;
52
53 static ssize_t calc_data_size;
54 static bool repipe;
55
56 static 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
66                 if (repipe) {
67                         int retw = write(STDOUT_FILENO, buf, ret);
68
69                         if (retw <= 0 || retw != ret)
70                                 die("repiping input file");
71                 }
72
73                 size -= ret;
74                 buf += ret;
75         }
76
77         return rsize;
78 }
79
80 static int read_or_die(void *data, int size)
81 {
82         int r;
83
84         r = do_read(input_fd, data, size);
85         if (r <= 0)
86                 die("reading input file (size expected=%d received=%d)",
87                     size, r);
88
89         if (calc_data_size)
90                 calc_data_size += r;
91
92         return r;
93 }
94
95 static unsigned int read4(void)
96 {
97         unsigned int data;
98
99         read_or_die(&data, 4);
100         return __data2host4(data);
101 }
102
103 static unsigned long long read8(void)
104 {
105         unsigned long long data;
106
107         read_or_die(&data, 8);
108         return __data2host8(data);
109 }
110
111 static char *read_string(void)
112 {
113         char buf[BUFSIZ];
114         char *str = NULL;
115         int size = 0;
116         off_t r;
117         char c;
118
119         for (;;) {
120                 r = read(input_fd, &c, 1);
121                 if (r < 0)
122                         die("reading input file");
123
124                 if (!r)
125                         die("no data");
126
127                 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
134                 buf[size++] = c;
135
136                 if (!c)
137                         break;
138         }
139
140         if (calc_data_size)
141                 calc_data_size += size;
142
143         str = malloc_or_die(size);
144         memcpy(str, buf, size);
145
146         return str;
147 }
148
149 static void read_proc_kallsyms(void)
150 {
151         unsigned int size;
152         char *buf;
153
154         size = read4();
155         if (!size)
156                 return;
157
158         buf = malloc_or_die(size + 1);
159         read_or_die(buf, size);
160         buf[size] = '\0';
161
162         parse_proc_kallsyms(buf, size);
163
164         free(buf);
165 }
166
167 static 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
184 static 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
218 static 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
228 static 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
238 static 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
252 static 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
273 struct 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
284 static struct cpu_data *cpu_data;
285
286 static 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
293 static void get_next_page(int cpu)
294 {
295         off_t save_seek;
296         off_t ret;
297
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 */
311                 save_seek = lseek(input_fd, 0, SEEK_CUR);
312
313                 ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
314                 if (ret == (off_t)-1)
315                         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 */
321                 lseek(input_fd, save_seek, SEEK_SET);
322
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
341 static 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
349 static 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
357 static int calc_index(void *ptr, int cpu)
358 {
359         return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
360 }
361
362 struct 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
401 read_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
462 struct 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
472 ssize_t trace_report(int fd, bool __repipe)
473 {
474         char buf[BUFSIZ];
475         char test[] = { 23, 8, 68 };
476         char *version;
477         int show_version = 0;
478         int show_funcs = 0;
479         int show_printk = 0;
480         ssize_t size;
481
482         calc_data_size = 1;
483         repipe = __repipe;
484
485         input_fd = fd;
486
487         read_or_die(buf, 3);
488         if (memcmp(buf, test, 3) != 0)
489                 die("no trace data in the file");
490
491         read_or_die(buf, 7);
492         if (memcmp(buf, "tracing", 7) != 0)
493                 die("not a trace file (missing 'tracing' tag)");
494
495         version = read_string();
496         if (show_version)
497                 printf("version = %s\n", version);
498         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
516         size = calc_data_size - 1;
517         calc_data_size = 0;
518         repipe = false;
519
520         if (show_funcs) {
521                 print_funcs();
522                 return size;
523         }
524         if (show_printk) {
525                 print_printk();
526                 return size;
527         }
528
529         return size;
530 }