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