]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/util/trace-event-read.c
perf tools: Get rid of read_or_die() in trace-event-read.c
[karo-tx-linux.git] / 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 #include <dirent.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <getopt.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <sys/mman.h>
31 #include <pthread.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <errno.h>
35
36 #include "../perf.h"
37 #include "util.h"
38 #include "trace-event.h"
39
40 static int input_fd;
41
42 int file_bigendian;
43 int host_bigendian;
44 static int long_size;
45
46 static ssize_t calc_data_size;
47 static bool repipe;
48
49 static int __do_read(int fd, void *buf, int size)
50 {
51         int rsize = size;
52
53         while (size) {
54                 int ret = read(fd, buf, size);
55
56                 if (ret <= 0)
57                         return -1;
58
59                 if (repipe) {
60                         int retw = write(STDOUT_FILENO, buf, ret);
61
62                         if (retw <= 0 || retw != ret) {
63                                 pr_debug("repiping input file");
64                                 return -1;
65                         }
66                 }
67
68                 size -= ret;
69                 buf += ret;
70         }
71
72         return rsize;
73 }
74
75 static int do_read(void *data, int size)
76 {
77         int r;
78
79         r = __do_read(input_fd, data, size);
80         if (r <= 0) {
81                 pr_debug("reading input file (size expected=%d received=%d)",
82                          size, r);
83                 return -1;
84         }
85
86         if (calc_data_size)
87                 calc_data_size += r;
88
89         return r;
90 }
91
92 /* If it fails, the next read will report it */
93 static void skip(int size)
94 {
95         char buf[BUFSIZ];
96         int r;
97
98         while (size) {
99                 r = size > BUFSIZ ? BUFSIZ : size;
100                 do_read(buf, r);
101                 size -= r;
102         };
103 }
104
105 static unsigned int read4(struct pevent *pevent)
106 {
107         unsigned int data;
108
109         if (do_read(&data, 4) < 0)
110                 return 0;
111         return __data2host4(pevent, data);
112 }
113
114 static unsigned long long read8(struct pevent *pevent)
115 {
116         unsigned long long data;
117
118         if (do_read(&data, 8) < 0)
119                 return 0;
120         return __data2host8(pevent, data);
121 }
122
123 static char *read_string(void)
124 {
125         char buf[BUFSIZ];
126         char *str = NULL;
127         int size = 0;
128         off_t r;
129         char c;
130
131         for (;;) {
132                 r = read(input_fd, &c, 1);
133                 if (r < 0)
134                         die("reading input file");
135
136                 if (!r)
137                         die("no data");
138
139                 if (repipe) {
140                         int retw = write(STDOUT_FILENO, &c, 1);
141
142                         if (retw <= 0 || retw != r)
143                                 die("repiping input file string");
144                 }
145
146                 buf[size++] = c;
147
148                 if (!c)
149                         break;
150         }
151
152         if (calc_data_size)
153                 calc_data_size += size;
154
155         str = malloc(size);
156         if (str)
157                 memcpy(str, buf, size);
158
159         return str;
160 }
161
162 static int read_proc_kallsyms(struct pevent *pevent)
163 {
164         unsigned int size;
165         char *buf;
166
167         size = read4(pevent);
168         if (!size)
169                 return 0;
170
171         buf = malloc(size + 1);
172         if (buf == NULL)
173                 return -1;
174
175         if (do_read(buf, size) < 0) {
176                 free(buf);
177                 return -1;
178         }
179         buf[size] = '\0';
180
181         parse_proc_kallsyms(pevent, buf, size);
182
183         free(buf);
184         return 0;
185 }
186
187 static int read_ftrace_printk(struct pevent *pevent)
188 {
189         unsigned int size;
190         char *buf;
191
192         /* it can have 0 size */
193         size = read4(pevent);
194         if (!size)
195                 return 0;
196
197         buf = malloc(size);
198         if (buf == NULL)
199                 return -1;
200
201         if (do_read(buf, size) < 0) {
202                 free(buf);
203                 return -1;
204         }
205
206         parse_ftrace_printk(pevent, buf, size);
207
208         free(buf);
209         return 0;
210 }
211
212 static int read_header_files(struct pevent *pevent)
213 {
214         unsigned long long size;
215         char *header_event;
216         char buf[BUFSIZ];
217         int ret = 0;
218
219         if (do_read(buf, 12) < 0)
220                 return -1;
221
222         if (memcmp(buf, "header_page", 12) != 0)
223                 die("did not read header page");
224
225         size = read8(pevent);
226         skip(size);
227
228         /*
229          * The size field in the page is of type long,
230          * use that instead, since it represents the kernel.
231          */
232         long_size = header_page_size_size;
233
234         if (do_read(buf, 13) < 0)
235                 return -1;
236
237         if (memcmp(buf, "header_event", 13) != 0)
238                 die("did not read header event");
239
240         size = read8(pevent);
241         header_event = malloc(size);
242         if (header_event == NULL)
243                 return -1;
244
245         if (do_read(header_event, size) < 0)
246                 ret = -1;
247
248         free(header_event);
249         return ret;
250 }
251
252 static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
253 {
254         char *buf;
255
256         buf = malloc(size);
257         if (buf == NULL)
258                 return -1;
259
260         if (do_read(buf, size) < 0) {
261                 free(buf);
262                 return -1;
263         }
264
265         parse_ftrace_file(pevent, buf, size);
266         free(buf);
267         return 0;
268 }
269
270 static int read_event_file(struct pevent *pevent, char *sys,
271                             unsigned long long size)
272 {
273         char *buf;
274
275         buf = malloc(size);
276         if (buf == NULL)
277                 return -1;
278
279         if (do_read(buf, size) < 0) {
280                 free(buf);
281                 return -1;
282         }
283
284         parse_event_file(pevent, buf, size, sys);
285         free(buf);
286         return 0;
287 }
288
289 static int read_ftrace_files(struct pevent *pevent)
290 {
291         unsigned long long size;
292         int count;
293         int i;
294         int ret;
295
296         count = read4(pevent);
297
298         for (i = 0; i < count; i++) {
299                 size = read8(pevent);
300                 ret = read_ftrace_file(pevent, size);
301                 if (ret)
302                         return ret;
303         }
304         return 0;
305 }
306
307 static int read_event_files(struct pevent *pevent)
308 {
309         unsigned long long size;
310         char *sys;
311         int systems;
312         int count;
313         int i,x;
314         int ret;
315
316         systems = read4(pevent);
317
318         for (i = 0; i < systems; i++) {
319                 sys = read_string();
320                 if (sys == NULL)
321                         return -1;
322
323                 count = read4(pevent);
324
325                 for (x=0; x < count; x++) {
326                         size = read8(pevent);
327                         ret = read_event_file(pevent, sys, size);
328                         if (ret)
329                                 return ret;
330                 }
331         }
332         return 0;
333 }
334
335 ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
336 {
337         char buf[BUFSIZ];
338         char test[] = { 23, 8, 68 };
339         char *version;
340         int show_version = 0;
341         int show_funcs = 0;
342         int show_printk = 0;
343         ssize_t size = -1;
344         struct pevent *pevent;
345         int err;
346
347         *ppevent = NULL;
348
349         calc_data_size = 1;
350         repipe = __repipe;
351
352         input_fd = fd;
353
354         if (do_read(buf, 3) < 0)
355                 return -1;
356         if (memcmp(buf, test, 3) != 0)
357                 die("no trace data in the file");
358
359         if (do_read(buf, 7) < 0)
360                 return -1;
361         if (memcmp(buf, "tracing", 7) != 0)
362                 die("not a trace file (missing 'tracing' tag)");
363
364         version = read_string();
365         if (version == NULL)
366                 return -1;
367         if (show_version)
368                 printf("version = %s\n", version);
369         free(version);
370
371         if (do_read(buf, 1) < 0)
372                 return -1;
373         file_bigendian = buf[0];
374         host_bigendian = bigendian();
375
376         pevent = read_trace_init(file_bigendian, host_bigendian);
377         if (pevent == NULL) {
378                 pr_debug("read_trace_init failed");
379                 goto out;
380         }
381
382         if (do_read(buf, 1) < 0)
383                 goto out;
384         long_size = buf[0];
385
386         page_size = read4(pevent);
387         if (!page_size)
388                 goto out;
389
390         err = read_header_files(pevent);
391         if (err)
392                 goto out;
393         err = read_ftrace_files(pevent);
394         if (err)
395                 goto out;
396         err = read_event_files(pevent);
397         if (err)
398                 goto out;
399         err = read_proc_kallsyms(pevent);
400         if (err)
401                 goto out;
402         err = read_ftrace_printk(pevent);
403         if (err)
404                 goto out;
405
406         size = calc_data_size - 1;
407         calc_data_size = 0;
408         repipe = false;
409
410         if (show_funcs) {
411                 pevent_print_funcs(pevent);
412         } else if (show_printk) {
413                 pevent_print_printk(pevent);
414         }
415
416         *ppevent = pevent;
417         pevent = NULL;
418
419 out:
420         if (pevent)
421                 pevent_free(pevent);
422         return size;
423 }