blob: af215c0d2379bb1440e9bd43fd19ca09f0e6d50b [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
Namhyung Kimebf3c672013-03-21 16:18:52 +090046static ssize_t trace_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
Namhyung Kimebf3c672013-03-21 16:18:52 +090086 trace_data_size += r;
Tom Zanussi92155452010-04-01 23:59:21 -050087
Steven Rostedt538bafb2009-08-17 16:18:06 +020088 return r;
89}
90
Tom Zanussicbb5cf72010-05-04 23:02:10 -050091/* If it fails, the next read will report it */
92static void skip(int size)
93{
94 char buf[BUFSIZ];
95 int r;
96
97 while (size) {
98 r = size > BUFSIZ ? BUFSIZ : size;
Namhyung Kim4a31e562013-03-21 16:18:50 +090099 do_read(buf, r);
Tom Zanussicbb5cf72010-05-04 23:02:10 -0500100 size -= r;
101 };
102}
103
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300104static unsigned int read4(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200105{
106 unsigned int data;
107
Namhyung Kim4a31e562013-03-21 16:18:50 +0900108 if (do_read(&data, 4) < 0)
109 return 0;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300110 return __data2host4(pevent, data);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200111}
112
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300113static unsigned long long read8(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200114{
115 unsigned long long data;
116
Namhyung Kim4a31e562013-03-21 16:18:50 +0900117 if (do_read(&data, 8) < 0)
118 return 0;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300119 return __data2host8(pevent, data);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200120}
121
122static char *read_string(void)
123{
124 char buf[BUFSIZ];
125 char *str = NULL;
126 int size = 0;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800127 off_t r;
Tom Zanussi92155452010-04-01 23:59:21 -0500128 char c;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200129
130 for (;;) {
Tom Zanussi92155452010-04-01 23:59:21 -0500131 r = read(input_fd, &c, 1);
Namhyung Kim452958f2013-03-21 16:18:51 +0900132 if (r < 0) {
133 pr_debug("reading input file");
134 goto out;
135 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200136
Namhyung Kim452958f2013-03-21 16:18:51 +0900137 if (!r) {
138 pr_debug("no data");
139 goto out;
140 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200141
Tom Zanussi454c4072010-05-01 01:41:20 -0500142 if (repipe) {
143 int retw = write(STDOUT_FILENO, &c, 1);
144
Namhyung Kim452958f2013-03-21 16:18:51 +0900145 if (retw <= 0 || retw != r) {
146 pr_debug("repiping input file string");
147 goto out;
148 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500149 }
150
Tom Zanussi92155452010-04-01 23:59:21 -0500151 buf[size++] = c;
152
153 if (!c)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200154 break;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200155 }
156
Namhyung Kimebf3c672013-03-21 16:18:52 +0900157 trace_data_size += size;
Ingo Molnar6f4596d2009-09-03 16:22:45 +0200158
Namhyung Kima4c98362013-03-21 16:18:49 +0900159 str = malloc(size);
160 if (str)
161 memcpy(str, buf, size);
Namhyung Kim452958f2013-03-21 16:18:51 +0900162out:
Steven Rostedt538bafb2009-08-17 16:18:06 +0200163 return str;
164}
165
Namhyung Kima4c98362013-03-21 16:18:49 +0900166static int read_proc_kallsyms(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200167{
168 unsigned int size;
169 char *buf;
170
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300171 size = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200172 if (!size)
Namhyung Kima4c98362013-03-21 16:18:49 +0900173 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200174
Namhyung Kima4c98362013-03-21 16:18:49 +0900175 buf = malloc(size + 1);
176 if (buf == NULL)
177 return -1;
178
Namhyung Kim4a31e562013-03-21 16:18:50 +0900179 if (do_read(buf, size) < 0) {
180 free(buf);
181 return -1;
182 }
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900183 buf[size] = '\0';
Steven Rostedt538bafb2009-08-17 16:18:06 +0200184
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300185 parse_proc_kallsyms(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200186
187 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900188 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200189}
190
Namhyung Kima4c98362013-03-21 16:18:49 +0900191static int read_ftrace_printk(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200192{
193 unsigned int size;
194 char *buf;
195
Namhyung Kim4a31e562013-03-21 16:18:50 +0900196 /* it can have 0 size */
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300197 size = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200198 if (!size)
Namhyung Kima4c98362013-03-21 16:18:49 +0900199 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200200
Namhyung Kima4c98362013-03-21 16:18:49 +0900201 buf = malloc(size);
202 if (buf == NULL)
203 return -1;
204
Namhyung Kim4a31e562013-03-21 16:18:50 +0900205 if (do_read(buf, size) < 0) {
206 free(buf);
207 return -1;
208 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200209
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300210 parse_ftrace_printk(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200211
212 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900213 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200214}
215
Namhyung Kima4c98362013-03-21 16:18:49 +0900216static int read_header_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200217{
218 unsigned long long size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200219 char *header_event;
220 char buf[BUFSIZ];
Namhyung Kim4a31e562013-03-21 16:18:50 +0900221 int ret = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200222
Namhyung Kim4a31e562013-03-21 16:18:50 +0900223 if (do_read(buf, 12) < 0)
224 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200225
Namhyung Kim452958f2013-03-21 16:18:51 +0900226 if (memcmp(buf, "header_page", 12) != 0) {
227 pr_debug("did not read header page");
228 return -1;
229 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200230
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300231 size = read8(pevent);
Frederic Weisbeckerd00a47c2010-05-01 03:08:46 +0200232 skip(size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200233
234 /*
235 * The size field in the page is of type long,
236 * use that instead, since it represents the kernel.
237 */
238 long_size = header_page_size_size;
239
Namhyung Kim4a31e562013-03-21 16:18:50 +0900240 if (do_read(buf, 13) < 0)
241 return -1;
242
Namhyung Kim452958f2013-03-21 16:18:51 +0900243 if (memcmp(buf, "header_event", 13) != 0) {
244 pr_debug("did not read header event");
245 return -1;
246 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200247
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300248 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900249 header_event = malloc(size);
250 if (header_event == NULL)
251 return -1;
252
Namhyung Kim4a31e562013-03-21 16:18:50 +0900253 if (do_read(header_event, size) < 0)
254 ret = -1;
255
Steven Rostedt538bafb2009-08-17 16:18:06 +0200256 free(header_event);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900257 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200258}
259
Namhyung Kima4c98362013-03-21 16:18:49 +0900260static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200261{
262 char *buf;
263
Namhyung Kima4c98362013-03-21 16:18:49 +0900264 buf = malloc(size);
265 if (buf == NULL)
266 return -1;
267
Namhyung Kim4a31e562013-03-21 16:18:50 +0900268 if (do_read(buf, size) < 0) {
269 free(buf);
270 return -1;
271 }
272
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300273 parse_ftrace_file(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200274 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900275 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200276}
277
Namhyung Kima4c98362013-03-21 16:18:49 +0900278static int read_event_file(struct pevent *pevent, char *sys,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300279 unsigned long long size)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200280{
281 char *buf;
282
Namhyung Kima4c98362013-03-21 16:18:49 +0900283 buf = malloc(size);
284 if (buf == NULL)
285 return -1;
286
Namhyung Kim4a31e562013-03-21 16:18:50 +0900287 if (do_read(buf, size) < 0) {
288 free(buf);
289 return -1;
290 }
291
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300292 parse_event_file(pevent, buf, size, sys);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200293 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900294 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200295}
296
Namhyung Kima4c98362013-03-21 16:18:49 +0900297static int read_ftrace_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200298{
299 unsigned long long size;
300 int count;
301 int i;
Namhyung Kima4c98362013-03-21 16:18:49 +0900302 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200303
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300304 count = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200305
306 for (i = 0; i < count; i++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300307 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900308 ret = read_ftrace_file(pevent, size);
309 if (ret)
310 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200311 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900312 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200313}
314
Namhyung Kima4c98362013-03-21 16:18:49 +0900315static int read_event_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200316{
317 unsigned long long size;
318 char *sys;
319 int systems;
320 int count;
321 int i,x;
Namhyung Kima4c98362013-03-21 16:18:49 +0900322 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200323
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300324 systems = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200325
326 for (i = 0; i < systems; i++) {
327 sys = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900328 if (sys == NULL)
329 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200330
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300331 count = read4(pevent);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900332
Steven Rostedt538bafb2009-08-17 16:18:06 +0200333 for (x=0; x < count; x++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300334 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900335 ret = read_event_file(pevent, sys, size);
336 if (ret)
337 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200338 }
339 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900340 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200341}
342
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300343ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200344{
Steven Rostedt538bafb2009-08-17 16:18:06 +0200345 char buf[BUFSIZ];
346 char test[] = { 23, 8, 68 };
347 char *version;
Ingo Molnard9340c12009-09-12 10:08:34 +0200348 int show_version = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200349 int show_funcs = 0;
350 int show_printk = 0;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900351 ssize_t size = -1;
352 struct pevent *pevent;
Namhyung Kima4c98362013-03-21 16:18:49 +0900353 int err;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900354
355 *ppevent = NULL;
Tom Zanussi92155452010-04-01 23:59:21 -0500356
Tom Zanussi454c4072010-05-01 01:41:20 -0500357 repipe = __repipe;
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200358 input_fd = fd;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200359
Namhyung Kim4a31e562013-03-21 16:18:50 +0900360 if (do_read(buf, 3) < 0)
361 return -1;
Namhyung Kim452958f2013-03-21 16:18:51 +0900362 if (memcmp(buf, test, 3) != 0) {
363 pr_debug("no trace data in the file");
364 return -1;
365 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200366
Namhyung Kim4a31e562013-03-21 16:18:50 +0900367 if (do_read(buf, 7) < 0)
368 return -1;
Namhyung Kim452958f2013-03-21 16:18:51 +0900369 if (memcmp(buf, "tracing", 7) != 0) {
370 pr_debug("not a trace file (missing 'tracing' tag)");
371 return -1;
372 }
Steven Rostedt538bafb2009-08-17 16:18:06 +0200373
374 version = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900375 if (version == NULL)
376 return -1;
Ingo Molnard9340c12009-09-12 10:08:34 +0200377 if (show_version)
378 printf("version = %s\n", version);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200379 free(version);
380
Namhyung Kim4a31e562013-03-21 16:18:50 +0900381 if (do_read(buf, 1) < 0)
382 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200383 file_bigendian = buf[0];
384 host_bigendian = bigendian();
385
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900386 pevent = read_trace_init(file_bigendian, host_bigendian);
387 if (pevent == NULL) {
388 pr_debug("read_trace_init failed");
389 goto out;
390 }
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200391
Namhyung Kim4a31e562013-03-21 16:18:50 +0900392 if (do_read(buf, 1) < 0)
393 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200394 long_size = buf[0];
395
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900396 page_size = read4(pevent);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900397 if (!page_size)
398 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200399
Namhyung Kima4c98362013-03-21 16:18:49 +0900400 err = read_header_files(pevent);
401 if (err)
402 goto out;
403 err = read_ftrace_files(pevent);
404 if (err)
405 goto out;
406 err = read_event_files(pevent);
407 if (err)
408 goto out;
409 err = read_proc_kallsyms(pevent);
410 if (err)
411 goto out;
412 err = read_ftrace_printk(pevent);
413 if (err)
414 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200415
Namhyung Kimebf3c672013-03-21 16:18:52 +0900416 size = trace_data_size;
Tom Zanussi454c4072010-05-01 01:41:20 -0500417 repipe = false;
Tom Zanussi92155452010-04-01 23:59:21 -0500418
Steven Rostedt538bafb2009-08-17 16:18:06 +0200419 if (show_funcs) {
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900420 pevent_print_funcs(pevent);
421 } else if (show_printk) {
422 pevent_print_printk(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200423 }
424
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900425 *ppevent = pevent;
426 pevent = NULL;
427
428out:
429 if (pevent)
430 pevent_free(pevent);
Tom Zanussi92155452010-04-01 23:59:21 -0500431 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200432}