blob: 644ad3b4edec0e65155e2ab40012301ff3b1eb39 [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 */
Steven Rostedt538bafb2009-08-17 16:18:06 +020021#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>
Steven Rostedt538bafb2009-08-17 16:18:06 +020034#include <errno.h>
35
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020036#include "../perf.h"
Steven Rostedt538bafb2009-08-17 16:18:06 +020037#include "util.h"
38#include "trace-event.h"
39
40static int input_fd;
41
Steven Rostedt538bafb2009-08-17 16:18:06 +020042int file_bigendian;
43int host_bigendian;
44static int long_size;
45
Tom Zanussi92155452010-04-01 23:59:21 -050046static ssize_t calc_data_size;
Tom Zanussi454c4072010-05-01 01:41:20 -050047static bool repipe;
Tom Zanussi92155452010-04-01 23:59:21 -050048
Namhyung Kim4a31e562013-03-21 16:18:50 +090049static int __do_read(int fd, void *buf, int size)
Tom Zanussi92155452010-04-01 23:59:21 -050050{
51 int rsize = size;
52
53 while (size) {
54 int ret = read(fd, buf, size);
55
56 if (ret <= 0)
57 return -1;
58
Tom Zanussi454c4072010-05-01 01:41:20 -050059 if (repipe) {
60 int retw = write(STDOUT_FILENO, buf, ret);
61
Namhyung Kim4a31e562013-03-21 16:18:50 +090062 if (retw <= 0 || retw != ret) {
63 pr_debug("repiping input file");
64 return -1;
65 }
Tom Zanussi454c4072010-05-01 01:41:20 -050066 }
67
Tom Zanussi92155452010-04-01 23:59:21 -050068 size -= ret;
69 buf += ret;
70 }
71
72 return rsize;
73}
74
Namhyung Kim4a31e562013-03-21 16:18:50 +090075static int do_read(void *data, int size)
Steven Rostedt538bafb2009-08-17 16:18:06 +020076{
77 int r;
78
Namhyung Kim4a31e562013-03-21 16:18:50 +090079 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 }
Tom Zanussi92155452010-04-01 23:59:21 -050085
86 if (calc_data_size)
87 calc_data_size += r;
88
Steven Rostedt538bafb2009-08-17 16:18:06 +020089 return r;
90}
91
Tom Zanussicbb5cf72010-05-04 23:02:10 -050092/* 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;
Namhyung Kim4a31e562013-03-21 16:18:50 +0900100 do_read(buf, r);
Tom Zanussicbb5cf72010-05-04 23:02:10 -0500101 size -= r;
102 };
103}
104
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300105static unsigned int read4(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200106{
107 unsigned int data;
108
Namhyung Kim4a31e562013-03-21 16:18:50 +0900109 if (do_read(&data, 4) < 0)
110 return 0;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300111 return __data2host4(pevent, data);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200112}
113
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300114static unsigned long long read8(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200115{
116 unsigned long long data;
117
Namhyung Kim4a31e562013-03-21 16:18:50 +0900118 if (do_read(&data, 8) < 0)
119 return 0;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300120 return __data2host8(pevent, data);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200121}
122
123static char *read_string(void)
124{
125 char buf[BUFSIZ];
126 char *str = NULL;
127 int size = 0;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800128 off_t r;
Tom Zanussi92155452010-04-01 23:59:21 -0500129 char c;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200130
131 for (;;) {
Tom Zanussi92155452010-04-01 23:59:21 -0500132 r = read(input_fd, &c, 1);
Namhyung Kim452958f2013-03-21 16:18:51 +0900133 if (r < 0) {
134 pr_debug("reading input file");
135 goto out;
136 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200137
Namhyung Kim452958f2013-03-21 16:18:51 +0900138 if (!r) {
139 pr_debug("no data");
140 goto out;
141 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200142
Tom Zanussi454c4072010-05-01 01:41:20 -0500143 if (repipe) {
144 int retw = write(STDOUT_FILENO, &c, 1);
145
Namhyung Kim452958f2013-03-21 16:18:51 +0900146 if (retw <= 0 || retw != r) {
147 pr_debug("repiping input file string");
148 goto out;
149 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500150 }
151
Tom Zanussi92155452010-04-01 23:59:21 -0500152 buf[size++] = c;
153
154 if (!c)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200155 break;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200156 }
157
Tom Zanussi92155452010-04-01 23:59:21 -0500158 if (calc_data_size)
159 calc_data_size += size;
Ingo Molnar6f4596d2009-09-03 16:22:45 +0200160
Namhyung Kima4c98362013-03-21 16:18:49 +0900161 str = malloc(size);
162 if (str)
163 memcpy(str, buf, size);
Namhyung Kim452958f2013-03-21 16:18:51 +0900164out:
Steven Rostedt538bafb2009-08-17 16:18:06 +0200165 return str;
166}
167
Namhyung Kima4c98362013-03-21 16:18:49 +0900168static int read_proc_kallsyms(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200169{
170 unsigned int size;
171 char *buf;
172
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300173 size = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200174 if (!size)
Namhyung Kima4c98362013-03-21 16:18:49 +0900175 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200176
Namhyung Kima4c98362013-03-21 16:18:49 +0900177 buf = malloc(size + 1);
178 if (buf == NULL)
179 return -1;
180
Namhyung Kim4a31e562013-03-21 16:18:50 +0900181 if (do_read(buf, size) < 0) {
182 free(buf);
183 return -1;
184 }
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900185 buf[size] = '\0';
Steven Rostedt538bafb2009-08-17 16:18:06 +0200186
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300187 parse_proc_kallsyms(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200188
189 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900190 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200191}
192
Namhyung Kima4c98362013-03-21 16:18:49 +0900193static int read_ftrace_printk(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200194{
195 unsigned int size;
196 char *buf;
197
Namhyung Kim4a31e562013-03-21 16:18:50 +0900198 /* it can have 0 size */
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300199 size = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200200 if (!size)
Namhyung Kima4c98362013-03-21 16:18:49 +0900201 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200202
Namhyung Kima4c98362013-03-21 16:18:49 +0900203 buf = malloc(size);
204 if (buf == NULL)
205 return -1;
206
Namhyung Kim4a31e562013-03-21 16:18:50 +0900207 if (do_read(buf, size) < 0) {
208 free(buf);
209 return -1;
210 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200211
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300212 parse_ftrace_printk(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200213
214 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900215 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200216}
217
Namhyung Kima4c98362013-03-21 16:18:49 +0900218static int read_header_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200219{
220 unsigned long long size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200221 char *header_event;
222 char buf[BUFSIZ];
Namhyung Kim4a31e562013-03-21 16:18:50 +0900223 int ret = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200224
Namhyung Kim4a31e562013-03-21 16:18:50 +0900225 if (do_read(buf, 12) < 0)
226 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200227
Namhyung Kim452958f2013-03-21 16:18:51 +0900228 if (memcmp(buf, "header_page", 12) != 0) {
229 pr_debug("did not read header page");
230 return -1;
231 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200232
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300233 size = read8(pevent);
Frederic Weisbeckerd00a47c2010-05-01 03:08:46 +0200234 skip(size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200235
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
Namhyung Kim4a31e562013-03-21 16:18:50 +0900242 if (do_read(buf, 13) < 0)
243 return -1;
244
Namhyung Kim452958f2013-03-21 16:18:51 +0900245 if (memcmp(buf, "header_event", 13) != 0) {
246 pr_debug("did not read header event");
247 return -1;
248 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200249
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300250 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900251 header_event = malloc(size);
252 if (header_event == NULL)
253 return -1;
254
Namhyung Kim4a31e562013-03-21 16:18:50 +0900255 if (do_read(header_event, size) < 0)
256 ret = -1;
257
Steven Rostedt538bafb2009-08-17 16:18:06 +0200258 free(header_event);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900259 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200260}
261
Namhyung Kima4c98362013-03-21 16:18:49 +0900262static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200263{
264 char *buf;
265
Namhyung Kima4c98362013-03-21 16:18:49 +0900266 buf = malloc(size);
267 if (buf == NULL)
268 return -1;
269
Namhyung Kim4a31e562013-03-21 16:18:50 +0900270 if (do_read(buf, size) < 0) {
271 free(buf);
272 return -1;
273 }
274
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300275 parse_ftrace_file(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200276 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900277 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200278}
279
Namhyung Kima4c98362013-03-21 16:18:49 +0900280static int read_event_file(struct pevent *pevent, char *sys,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300281 unsigned long long size)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200282{
283 char *buf;
284
Namhyung Kima4c98362013-03-21 16:18:49 +0900285 buf = malloc(size);
286 if (buf == NULL)
287 return -1;
288
Namhyung Kim4a31e562013-03-21 16:18:50 +0900289 if (do_read(buf, size) < 0) {
290 free(buf);
291 return -1;
292 }
293
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300294 parse_event_file(pevent, buf, size, sys);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200295 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900296 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200297}
298
Namhyung Kima4c98362013-03-21 16:18:49 +0900299static int read_ftrace_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200300{
301 unsigned long long size;
302 int count;
303 int i;
Namhyung Kima4c98362013-03-21 16:18:49 +0900304 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200305
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300306 count = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200307
308 for (i = 0; i < count; i++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300309 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900310 ret = read_ftrace_file(pevent, size);
311 if (ret)
312 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200313 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900314 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200315}
316
Namhyung Kima4c98362013-03-21 16:18:49 +0900317static int read_event_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200318{
319 unsigned long long size;
320 char *sys;
321 int systems;
322 int count;
323 int i,x;
Namhyung Kima4c98362013-03-21 16:18:49 +0900324 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200325
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300326 systems = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200327
328 for (i = 0; i < systems; i++) {
329 sys = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900330 if (sys == NULL)
331 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200332
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300333 count = read4(pevent);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900334
Steven Rostedt538bafb2009-08-17 16:18:06 +0200335 for (x=0; x < count; x++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300336 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900337 ret = read_event_file(pevent, sys, size);
338 if (ret)
339 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200340 }
341 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900342 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200343}
344
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300345ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200346{
Steven Rostedt538bafb2009-08-17 16:18:06 +0200347 char buf[BUFSIZ];
348 char test[] = { 23, 8, 68 };
349 char *version;
Ingo Molnard9340c12009-09-12 10:08:34 +0200350 int show_version = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200351 int show_funcs = 0;
352 int show_printk = 0;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900353 ssize_t size = -1;
354 struct pevent *pevent;
Namhyung Kima4c98362013-03-21 16:18:49 +0900355 int err;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900356
357 *ppevent = NULL;
Tom Zanussi92155452010-04-01 23:59:21 -0500358
359 calc_data_size = 1;
Tom Zanussi454c4072010-05-01 01:41:20 -0500360 repipe = __repipe;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200361
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200362 input_fd = fd;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200363
Namhyung Kim4a31e562013-03-21 16:18:50 +0900364 if (do_read(buf, 3) < 0)
365 return -1;
Namhyung Kim452958f2013-03-21 16:18:51 +0900366 if (memcmp(buf, test, 3) != 0) {
367 pr_debug("no trace data in the file");
368 return -1;
369 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200370
Namhyung Kim4a31e562013-03-21 16:18:50 +0900371 if (do_read(buf, 7) < 0)
372 return -1;
Namhyung Kim452958f2013-03-21 16:18:51 +0900373 if (memcmp(buf, "tracing", 7) != 0) {
374 pr_debug("not a trace file (missing 'tracing' tag)");
375 return -1;
376 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200377
378 version = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900379 if (version == NULL)
380 return -1;
Ingo Molnard9340c12009-09-12 10:08:34 +0200381 if (show_version)
382 printf("version = %s\n", version);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200383 free(version);
384
Namhyung Kim4a31e562013-03-21 16:18:50 +0900385 if (do_read(buf, 1) < 0)
386 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200387 file_bigendian = buf[0];
388 host_bigendian = bigendian();
389
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900390 pevent = read_trace_init(file_bigendian, host_bigendian);
391 if (pevent == NULL) {
392 pr_debug("read_trace_init failed");
393 goto out;
394 }
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200395
Namhyung Kim4a31e562013-03-21 16:18:50 +0900396 if (do_read(buf, 1) < 0)
397 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200398 long_size = buf[0];
399
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900400 page_size = read4(pevent);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900401 if (!page_size)
402 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200403
Namhyung Kima4c98362013-03-21 16:18:49 +0900404 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;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200419
Tom Zanussi92155452010-04-01 23:59:21 -0500420 size = calc_data_size - 1;
421 calc_data_size = 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500422 repipe = false;
Tom Zanussi92155452010-04-01 23:59:21 -0500423
Steven Rostedt538bafb2009-08-17 16:18:06 +0200424 if (show_funcs) {
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900425 pevent_print_funcs(pevent);
426 } else if (show_printk) {
427 pevent_print_printk(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200428 }
429
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900430 *ppevent = pevent;
431 pevent = NULL;
432
433out:
434 if (pevent)
435 pevent_free(pevent);
Tom Zanussi92155452010-04-01 23:59:21 -0500436 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200437}