blob: 877706bd454f5256c2c2074e27f9ac31483f34a9 [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);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200133 if (r < 0)
134 die("reading input file");
135
136 if (!r)
137 die("no data");
138
Tom Zanussi454c4072010-05-01 01:41:20 -0500139 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
Tom Zanussi92155452010-04-01 23:59:21 -0500146 buf[size++] = c;
147
148 if (!c)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200149 break;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200150 }
151
Tom Zanussi92155452010-04-01 23:59:21 -0500152 if (calc_data_size)
153 calc_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);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200158
159 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 *header_event;
216 char buf[BUFSIZ];
Namhyung Kim4a31e562013-03-21 16:18:50 +0900217 int ret = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200218
Namhyung Kim4a31e562013-03-21 16:18:50 +0900219 if (do_read(buf, 12) < 0)
220 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200221
222 if (memcmp(buf, "header_page", 12) != 0)
223 die("did not read header page");
224
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300225 size = read8(pevent);
Frederic Weisbeckerd00a47c2010-05-01 03:08:46 +0200226 skip(size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200227
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
Namhyung Kim4a31e562013-03-21 16:18:50 +0900234 if (do_read(buf, 13) < 0)
235 return -1;
236
Steven Rostedt538bafb2009-08-17 16:18:06 +0200237 if (memcmp(buf, "header_event", 13) != 0)
238 die("did not read header event");
239
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300240 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900241 header_event = malloc(size);
242 if (header_event == NULL)
243 return -1;
244
Namhyung Kim4a31e562013-03-21 16:18:50 +0900245 if (do_read(header_event, size) < 0)
246 ret = -1;
247
Steven Rostedt538bafb2009-08-17 16:18:06 +0200248 free(header_event);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900249 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200250}
251
Namhyung Kima4c98362013-03-21 16:18:49 +0900252static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200253{
254 char *buf;
255
Namhyung Kima4c98362013-03-21 16:18:49 +0900256 buf = malloc(size);
257 if (buf == NULL)
258 return -1;
259
Namhyung Kim4a31e562013-03-21 16:18:50 +0900260 if (do_read(buf, size) < 0) {
261 free(buf);
262 return -1;
263 }
264
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300265 parse_ftrace_file(pevent, buf, size);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200266 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900267 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200268}
269
Namhyung Kima4c98362013-03-21 16:18:49 +0900270static int read_event_file(struct pevent *pevent, char *sys,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300271 unsigned long long size)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200272{
273 char *buf;
274
Namhyung Kima4c98362013-03-21 16:18:49 +0900275 buf = malloc(size);
276 if (buf == NULL)
277 return -1;
278
Namhyung Kim4a31e562013-03-21 16:18:50 +0900279 if (do_read(buf, size) < 0) {
280 free(buf);
281 return -1;
282 }
283
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300284 parse_event_file(pevent, buf, size, sys);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200285 free(buf);
Namhyung Kima4c98362013-03-21 16:18:49 +0900286 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200287}
288
Namhyung Kima4c98362013-03-21 16:18:49 +0900289static int read_ftrace_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200290{
291 unsigned long long size;
292 int count;
293 int i;
Namhyung Kima4c98362013-03-21 16:18:49 +0900294 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200295
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300296 count = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200297
298 for (i = 0; i < count; i++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300299 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900300 ret = read_ftrace_file(pevent, size);
301 if (ret)
302 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200303 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900304 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200305}
306
Namhyung Kima4c98362013-03-21 16:18:49 +0900307static int read_event_files(struct pevent *pevent)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200308{
309 unsigned long long size;
310 char *sys;
311 int systems;
312 int count;
313 int i,x;
Namhyung Kima4c98362013-03-21 16:18:49 +0900314 int ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200315
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300316 systems = read4(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200317
318 for (i = 0; i < systems; i++) {
319 sys = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900320 if (sys == NULL)
321 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200322
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300323 count = read4(pevent);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900324
Steven Rostedt538bafb2009-08-17 16:18:06 +0200325 for (x=0; x < count; x++) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300326 size = read8(pevent);
Namhyung Kima4c98362013-03-21 16:18:49 +0900327 ret = read_event_file(pevent, sys, size);
328 if (ret)
329 return ret;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200330 }
331 }
Namhyung Kima4c98362013-03-21 16:18:49 +0900332 return 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200333}
334
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300335ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
Steven Rostedt538bafb2009-08-17 16:18:06 +0200336{
Steven Rostedt538bafb2009-08-17 16:18:06 +0200337 char buf[BUFSIZ];
338 char test[] = { 23, 8, 68 };
339 char *version;
Ingo Molnard9340c12009-09-12 10:08:34 +0200340 int show_version = 0;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200341 int show_funcs = 0;
342 int show_printk = 0;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900343 ssize_t size = -1;
344 struct pevent *pevent;
Namhyung Kima4c98362013-03-21 16:18:49 +0900345 int err;
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900346
347 *ppevent = NULL;
Tom Zanussi92155452010-04-01 23:59:21 -0500348
349 calc_data_size = 1;
Tom Zanussi454c4072010-05-01 01:41:20 -0500350 repipe = __repipe;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200351
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200352 input_fd = fd;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200353
Namhyung Kim4a31e562013-03-21 16:18:50 +0900354 if (do_read(buf, 3) < 0)
355 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200356 if (memcmp(buf, test, 3) != 0)
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200357 die("no trace data in the file");
Steven Rostedt538bafb2009-08-17 16:18:06 +0200358
Namhyung Kim4a31e562013-03-21 16:18:50 +0900359 if (do_read(buf, 7) < 0)
360 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200361 if (memcmp(buf, "tracing", 7) != 0)
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200362 die("not a trace file (missing 'tracing' tag)");
Steven Rostedt538bafb2009-08-17 16:18:06 +0200363
364 version = read_string();
Namhyung Kima4c98362013-03-21 16:18:49 +0900365 if (version == NULL)
366 return -1;
Ingo Molnard9340c12009-09-12 10:08:34 +0200367 if (show_version)
368 printf("version = %s\n", version);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200369 free(version);
370
Namhyung Kim4a31e562013-03-21 16:18:50 +0900371 if (do_read(buf, 1) < 0)
372 return -1;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200373 file_bigendian = buf[0];
374 host_bigendian = bigendian();
375
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900376 pevent = read_trace_init(file_bigendian, host_bigendian);
377 if (pevent == NULL) {
378 pr_debug("read_trace_init failed");
379 goto out;
380 }
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200381
Namhyung Kim4a31e562013-03-21 16:18:50 +0900382 if (do_read(buf, 1) < 0)
383 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200384 long_size = buf[0];
385
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900386 page_size = read4(pevent);
Namhyung Kim4a31e562013-03-21 16:18:50 +0900387 if (!page_size)
388 goto out;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200389
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
Tom Zanussi92155452010-04-01 23:59:21 -0500406 size = calc_data_size - 1;
407 calc_data_size = 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500408 repipe = false;
Tom Zanussi92155452010-04-01 23:59:21 -0500409
Steven Rostedt538bafb2009-08-17 16:18:06 +0200410 if (show_funcs) {
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900411 pevent_print_funcs(pevent);
412 } else if (show_printk) {
413 pevent_print_printk(pevent);
Steven Rostedt538bafb2009-08-17 16:18:06 +0200414 }
415
Namhyung Kim3dce2ce2013-03-21 16:18:48 +0900416 *ppevent = pevent;
417 pevent = NULL;
418
419out:
420 if (pevent)
421 pevent_free(pevent);
Tom Zanussi92155452010-04-01 23:59:21 -0500422 return size;
Steven Rostedt538bafb2009-08-17 16:18:06 +0200423}