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