blob: fa45fca2a2d34f4401c68ceb60ad33208c407a91 [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
Namhyung Kimebf3c672013-03-21 16:18:52 +090042static ssize_t trace_data_size;
Tom Zanussi454c4072010-05-01 01:41:20 -050043static bool repipe;
Tom Zanussi92155452010-04-01 23:59:21 -050044
Namhyung Kim4a31e562013-03-21 16:18:50 +090045static int __do_read(int fd, void *buf, int size)
Tom Zanussi92155452010-04-01 23:59:21 -050046{
47 int rsize = size;
48
49 while (size) {
50 int ret = read(fd, buf, size);
51
52 if (ret <= 0)
53 return -1;
54
Tom Zanussi454c4072010-05-01 01:41:20 -050055 if (repipe) {
56 int retw = write(STDOUT_FILENO, buf, ret);
57
Namhyung Kim4a31e562013-03-21 16:18:50 +090058 if (retw <= 0 || retw != ret) {
59 pr_debug("repiping input file");
60 return -1;
61 }
Tom Zanussi454c4072010-05-01 01:41:20 -050062 }
63
Tom Zanussi92155452010-04-01 23:59:21 -050064 size -= ret;
65 buf += ret;
66 }
67
68 return rsize;
69}
70
Namhyung Kim4a31e562013-03-21 16:18:50 +090071static int do_read(void *data, int size)
Steven Rostedt538bafb2009-08-17 16:18:06 +020072{
73 int r;
74
Namhyung Kim4a31e562013-03-21 16:18:50 +090075 r = __do_read(input_fd, data, size);
76 if (r <= 0) {
77 pr_debug("reading input file (size expected=%d received=%d)",
78 size, r);
79 return -1;
80 }
Tom Zanussi92155452010-04-01 23:59:21 -050081
Namhyung Kimebf3c672013-03-21 16:18:52 +090082 trace_data_size += r;
Tom Zanussi92155452010-04-01 23:59:21 -050083
Steven Rostedt538bafb2009-08-17 16:18:06 +020084 return r;
85}
86
Tom Zanussicbb5cf72010-05-04 23:02:10 -050087/* If it fails, the next read will report it */
88static void skip(int size)
89{
90 char buf[BUFSIZ];
91 int r;
92
93 while (size) {
94 r = size > BUFSIZ ? BUFSIZ : size;
Namhyung Kim4a31e562013-03-21 16:18:50 +090095 do_read(buf, r);
Tom Zanussicbb5cf72010-05-04 23:02:10 -050096 size -= r;
97 };
98}
99
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300100static unsigned int read4(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200101{
102 unsigned int data;
103
Namhyung Kim4a31e562013-03-21 16:18:50 +0900104 if (do_read(&data, 4) < 0)
105 return 0;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300106 return __data2host4(pevent, data);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200107}
108
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300109static unsigned long long read8(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200110{
111 unsigned long long data;
112
Namhyung Kim4a31e562013-03-21 16:18:50 +0900113 if (do_read(&data, 8) < 0)
114 return 0;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300115 return __data2host8(pevent, data);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200116}
117
118static char *read_string(void)
119{
120 char buf[BUFSIZ];
121 char *str = NULL;
122 int size = 0;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800123 off_t r;
Tom Zanussi92155452010-04-01 23:59:21 -0500124 char c;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200125
126 for (;;) {
Tom Zanussi92155452010-04-01 23:59:21 -0500127 r = read(input_fd, &c, 1);
Namhyung Kim452958f2013-03-21 16:18:51 +0900128 if (r < 0) {
129 pr_debug("reading input file");
130 goto out;
131 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200132
Namhyung Kim452958f2013-03-21 16:18:51 +0900133 if (!r) {
134 pr_debug("no data");
135 goto out;
136 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200137
Tom Zanussi454c4072010-05-01 01:41:20 -0500138 if (repipe) {
139 int retw = write(STDOUT_FILENO, &c, 1);
140
Namhyung Kim452958f2013-03-21 16:18:51 +0900141 if (retw <= 0 || retw != r) {
142 pr_debug("repiping input file string");
143 goto out;
144 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500145 }
146
Tom Zanussi92155452010-04-01 23:59:21 -0500147 buf[size++] = c;
148
149 if (!c)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200150 break;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200151 }
152
Namhyung Kimebf3c672013-03-21 16:18:52 +0900153 trace_data_size += size;
Ingo Molnar6f4596d2009-09-03 16:22:45 +0200154
Namhyung Kima4c98362013-03-21 16:18:49 +0900155 str = malloc(size);
156 if (str)
157 memcpy(str, buf, size);
Namhyung Kim452958f2013-03-21 16:18:51 +0900158out:
Steven Rostedt538bafb2009-08-17 16:18:06 +0200159 return str;
160}
161
Namhyung Kima4c98362013-03-21 16:18:49 +0900162static int read_proc_kallsyms(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200163{
164 unsigned int size;
165 char *buf;
166
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300167 size = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200168 if (!size)
Namhyung Kima4c98362013-03-21 16:18:49 +0900169 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200170
Namhyung Kima4c98362013-03-21 16:18:49 +0900171 buf = malloc(size + 1);
172 if (buf == NULL)
173 return -1;
174
Namhyung Kim4a31e562013-03-21 16:18:50 +0900175 if (do_read(buf, size) < 0) {
176 free(buf);
177 return -1;
178 }
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900179 buf[size] = '\0';
Steven Rostedt538bafb2009-08-17 16:18:06 +0200180
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300181 parse_proc_kallsyms(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200182
183 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900184 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200185}
186
Namhyung Kima4c98362013-03-21 16:18:49 +0900187static int read_ftrace_printk(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200188{
189 unsigned int size;
190 char *buf;
191
Namhyung Kim4a31e562013-03-21 16:18:50 +0900192 /* it can have 0 size */
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300193 size = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200194 if (!size)
Namhyung Kima4c98362013-03-21 16:18:49 +0900195 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200196
Namhyung Kima4c98362013-03-21 16:18:49 +0900197 buf = malloc(size);
198 if (buf == NULL)
199 return -1;
200
Namhyung Kim4a31e562013-03-21 16:18:50 +0900201 if (do_read(buf, size) < 0) {
202 free(buf);
203 return -1;
204 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200205
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300206 parse_ftrace_printk(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200207
208 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900209 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200210}
211
Namhyung Kima4c98362013-03-21 16:18:49 +0900212static int read_header_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200213{
214 unsigned long long size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200215 char buf[BUFSIZ];
Namhyung Kim4a31e562013-03-21 16:18:50 +0900216 int ret = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200217
Namhyung Kim4a31e562013-03-21 16:18:50 +0900218 if (do_read(buf, 12) < 0)
219 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200220
Namhyung Kim452958f2013-03-21 16:18:51 +0900221 if (memcmp(buf, "header_page", 12) != 0) {
222 pr_debug("did not read header page");
223 return -1;
224 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200225
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300226 size = read8(pevent);
Frederic Weisbeckerd00a47c2010-05-01 03:08:46 +0200227 skip(size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200228
Namhyung Kim4a31e562013-03-21 16:18:50 +0900229 if (do_read(buf, 13) < 0)
230 return -1;
231
Namhyung Kim452958f2013-03-21 16:18:51 +0900232 if (memcmp(buf, "header_event", 13) != 0) {
233 pr_debug("did not read header event");
234 return -1;
235 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200236
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300237 size = read8(pevent);
Namhyung Kim2b2efc72013-06-04 14:20:25 +0900238 skip(size);
Namhyung Kima4c98362013-03-21 16:18:49 +0900239
Namhyung Kim4a31e562013-03-21 16:18:50 +0900240 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200241}
242
Namhyung Kima4c98362013-03-21 16:18:49 +0900243static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200244{
245 char *buf;
246
Namhyung Kima4c98362013-03-21 16:18:49 +0900247 buf = malloc(size);
248 if (buf == NULL)
249 return -1;
250
Namhyung Kim4a31e562013-03-21 16:18:50 +0900251 if (do_read(buf, size) < 0) {
252 free(buf);
253 return -1;
254 }
255
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300256 parse_ftrace_file(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200257 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900258 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200259}
260
Namhyung Kima4c98362013-03-21 16:18:49 +0900261static int read_event_file(struct pevent *pevent, char *sys,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300262 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_event_file(pevent, buf, size, sys);
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_ftrace_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200281{
282 unsigned long long size;
283 int count;
284 int i;
Namhyung Kima4c98362013-03-21 16:18:49 +0900285 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200286
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300287 count = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200288
289 for (i = 0; i < count; i++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300290 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900291 ret = read_ftrace_file(pevent, size);
292 if (ret)
293 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200294 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900295 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200296}
297
Namhyung Kima4c98362013-03-21 16:18:49 +0900298static int read_event_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200299{
300 unsigned long long size;
301 char *sys;
302 int systems;
303 int count;
304 int i,x;
Namhyung Kima4c98362013-03-21 16:18:49 +0900305 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200306
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300307 systems = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200308
309 for (i = 0; i < systems; i++) {
310 sys = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900311 if (sys == NULL)
312 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200313
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300314 count = read4(pevent);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900315
Steven Rostedt538bafb2009-08-17 16:18:06 +0200316 for (x=0; x < count; x++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300317 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900318 ret = read_event_file(pevent, sys, size);
319 if (ret)
320 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200321 }
322 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900323 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200324}
325
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300326ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200327{
Steven Rostedt538bafb2009-08-17 16:18:06 +0200328 char buf[BUFSIZ];
329 char test[] = { 23, 8, 68 };
330 char *version;
Ingo Molnard9340c12009-09-12 10:08:34 +0200331 int show_version = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200332 int show_funcs = 0;
333 int show_printk = 0;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900334 ssize_t size = -1;
Namhyung Kim63af28f2013-06-04 14:20:24 +0900335 int file_bigendian;
336 int host_bigendian;
Namhyung Kim59657f92013-06-04 14:20:23 +0900337 int file_long_size;
Namhyung Kim30f36762013-06-04 14:20:22 +0900338 int file_page_size;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900339 struct pevent *pevent;
Namhyung Kima4c98362013-03-21 16:18:49 +0900340 int err;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900341
342 *ppevent = NULL;
Tom Zanussi92155452010-04-01 23:59:21 -0500343
Tom Zanussi454c4072010-05-01 01:41:20 -0500344 repipe = __repipe;
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200345 input_fd = fd;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200346
Namhyung Kim4a31e562013-03-21 16:18:50 +0900347 if (do_read(buf, 3) < 0)
348 return -1;
Namhyung Kim452958f2013-03-21 16:18:51 +0900349 if (memcmp(buf, test, 3) != 0) {
350 pr_debug("no trace data in the file");
351 return -1;
352 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200353
Namhyung Kim4a31e562013-03-21 16:18:50 +0900354 if (do_read(buf, 7) < 0)
355 return -1;
Namhyung Kim452958f2013-03-21 16:18:51 +0900356 if (memcmp(buf, "tracing", 7) != 0) {
357 pr_debug("not a trace file (missing 'tracing' tag)");
358 return -1;
359 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200360
361 version = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900362 if (version == NULL)
363 return -1;
Ingo Molnard9340c12009-09-12 10:08:34 +0200364 if (show_version)
365 printf("version = %s\n", version);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200366 free(version);
367
Namhyung Kim4a31e562013-03-21 16:18:50 +0900368 if (do_read(buf, 1) < 0)
369 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200370 file_bigendian = buf[0];
371 host_bigendian = bigendian();
372
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900373 pevent = read_trace_init(file_bigendian, host_bigendian);
374 if (pevent == NULL) {
375 pr_debug("read_trace_init failed");
376 goto out;
377 }
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200378
Namhyung Kim4a31e562013-03-21 16:18:50 +0900379 if (do_read(buf, 1) < 0)
380 goto out;
Namhyung Kim59657f92013-06-04 14:20:23 +0900381 file_long_size = buf[0];
Steven Rostedt538bafb2009-08-17 16:18:06 +0200382
Namhyung Kim30f36762013-06-04 14:20:22 +0900383 file_page_size = read4(pevent);
384 if (!file_page_size)
Namhyung Kim4a31e562013-03-21 16:18:50 +0900385 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200386
Namhyung Kim59657f92013-06-04 14:20:23 +0900387 pevent_set_long_size(pevent, file_long_size);
Namhyung Kim30f36762013-06-04 14:20:22 +0900388 pevent_set_page_size(pevent, file_page_size);
389
Namhyung Kima4c98362013-03-21 16:18:49 +0900390 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;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200405
Namhyung Kimebf3c672013-03-21 16:18:52 +0900406 size = trace_data_size;
Tom Zanussi454c4072010-05-01 01:41:20 -0500407 repipe = false;
Tom Zanussi92155452010-04-01 23:59:21 -0500408
Steven Rostedt538bafb2009-08-17 16:18:06 +0200409 if (show_funcs) {
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900410 pevent_print_funcs(pevent);
411 } else if (show_printk) {
412 pevent_print_printk(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200413 }
414
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900415 *ppevent = pevent;
416 pevent = NULL;
417
418out:
419 if (pevent)
420 pevent_free(pevent);
Tom Zanussi92155452010-04-01 23:59:21 -0500421 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200422}