Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / trace-event-info.c
blob8ad8e755127bd704e991254ee735e1f0d6ab9ed3
1 /*
2 * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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!)
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.
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
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #include "util.h"
22 #include <dirent.h>
23 #include <mntent.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <stdbool.h>
35 #include <linux/list.h>
36 #include <linux/kernel.h>
38 #include "../perf.h"
39 #include "trace-event.h"
40 #include <api/fs/tracing_path.h>
41 #include "evsel.h"
42 #include "debug.h"
44 #define VERSION "0.6"
46 static int output_fd;
49 int bigendian(void)
51 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
52 unsigned int *ptr;
54 ptr = (unsigned int *)(void *)str;
55 return *ptr == 0x01020304;
58 /* unfortunately, you can not stat debugfs or proc files for size */
59 static int record_file(const char *file, ssize_t hdr_sz)
61 unsigned long long size = 0;
62 char buf[BUFSIZ], *sizep;
63 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
64 int r, fd;
65 int err = -EIO;
67 fd = open(file, O_RDONLY);
68 if (fd < 0) {
69 pr_debug("Can't read '%s'", file);
70 return -errno;
73 /* put in zeros for file size, then fill true size later */
74 if (hdr_sz) {
75 if (write(output_fd, &size, hdr_sz) != hdr_sz)
76 goto out;
79 do {
80 r = read(fd, buf, BUFSIZ);
81 if (r > 0) {
82 size += r;
83 if (write(output_fd, buf, r) != r)
84 goto out;
86 } while (r > 0);
88 /* ugh, handle big-endian hdr_size == 4 */
89 sizep = (char*)&size;
90 if (bigendian())
91 sizep += sizeof(u64) - hdr_sz;
93 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) {
94 pr_debug("writing file size failed\n");
95 goto out;
98 err = 0;
99 out:
100 close(fd);
101 return err;
104 static int record_header_files(void)
106 char *path = get_events_file("header_page");
107 struct stat st;
108 int err = -EIO;
110 if (!path) {
111 pr_debug("can't get tracing/events/header_page");
112 return -ENOMEM;
115 if (stat(path, &st) < 0) {
116 pr_debug("can't read '%s'", path);
117 goto out;
120 if (write(output_fd, "header_page", 12) != 12) {
121 pr_debug("can't write header_page\n");
122 goto out;
125 if (record_file(path, 8) < 0) {
126 pr_debug("can't record header_page file\n");
127 goto out;
130 put_events_file(path);
132 path = get_events_file("header_event");
133 if (!path) {
134 pr_debug("can't get tracing/events/header_event");
135 err = -ENOMEM;
136 goto out;
139 if (stat(path, &st) < 0) {
140 pr_debug("can't read '%s'", path);
141 goto out;
144 if (write(output_fd, "header_event", 13) != 13) {
145 pr_debug("can't write header_event\n");
146 goto out;
149 if (record_file(path, 8) < 0) {
150 pr_debug("can't record header_event file\n");
151 goto out;
154 err = 0;
155 out:
156 put_events_file(path);
157 return err;
160 static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
162 while (tps) {
163 if (!strcmp(sys, tps->name))
164 return true;
165 tps = tps->next;
168 return false;
171 #define for_each_event(dir, dent, tps) \
172 while ((dent = readdir(dir))) \
173 if (dent->d_type == DT_DIR && \
174 (strcmp(dent->d_name, ".")) && \
175 (strcmp(dent->d_name, ".."))) \
177 static int copy_event_system(const char *sys, struct tracepoint_path *tps)
179 struct dirent *dent;
180 struct stat st;
181 char *format;
182 DIR *dir;
183 int count = 0;
184 int ret;
185 int err;
187 dir = opendir(sys);
188 if (!dir) {
189 pr_debug("can't read directory '%s'", sys);
190 return -errno;
193 for_each_event(dir, dent, tps) {
194 if (!name_in_tp_list(dent->d_name, tps))
195 continue;
197 if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
198 err = -ENOMEM;
199 goto out;
201 ret = stat(format, &st);
202 free(format);
203 if (ret < 0)
204 continue;
205 count++;
208 if (write(output_fd, &count, 4) != 4) {
209 err = -EIO;
210 pr_debug("can't write count\n");
211 goto out;
214 rewinddir(dir);
215 for_each_event(dir, dent, tps) {
216 if (!name_in_tp_list(dent->d_name, tps))
217 continue;
219 if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
220 err = -ENOMEM;
221 goto out;
223 ret = stat(format, &st);
225 if (ret >= 0) {
226 err = record_file(format, 8);
227 if (err) {
228 free(format);
229 goto out;
232 free(format);
234 err = 0;
235 out:
236 closedir(dir);
237 return err;
240 static int record_ftrace_files(struct tracepoint_path *tps)
242 char *path;
243 int ret;
245 path = get_events_file("ftrace");
246 if (!path) {
247 pr_debug("can't get tracing/events/ftrace");
248 return -ENOMEM;
251 ret = copy_event_system(path, tps);
253 put_tracing_file(path);
255 return ret;
258 static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
260 while (tps) {
261 if (!strcmp(sys, tps->system))
262 return true;
263 tps = tps->next;
266 return false;
269 static int record_event_files(struct tracepoint_path *tps)
271 struct dirent *dent;
272 struct stat st;
273 char *path;
274 char *sys;
275 DIR *dir;
276 int count = 0;
277 int ret;
278 int err;
280 path = get_tracing_file("events");
281 if (!path) {
282 pr_debug("can't get tracing/events");
283 return -ENOMEM;
286 dir = opendir(path);
287 if (!dir) {
288 err = -errno;
289 pr_debug("can't read directory '%s'", path);
290 goto out;
293 for_each_event(dir, dent, tps) {
294 if (strcmp(dent->d_name, "ftrace") == 0 ||
295 !system_in_tp_list(dent->d_name, tps))
296 continue;
298 count++;
301 if (write(output_fd, &count, 4) != 4) {
302 err = -EIO;
303 pr_debug("can't write count\n");
304 goto out;
307 rewinddir(dir);
308 for_each_event(dir, dent, tps) {
309 if (strcmp(dent->d_name, "ftrace") == 0 ||
310 !system_in_tp_list(dent->d_name, tps))
311 continue;
313 if (asprintf(&sys, "%s/%s", path, dent->d_name) < 0) {
314 err = -ENOMEM;
315 goto out;
317 ret = stat(sys, &st);
318 if (ret >= 0) {
319 ssize_t size = strlen(dent->d_name) + 1;
321 if (write(output_fd, dent->d_name, size) != size ||
322 copy_event_system(sys, tps) < 0) {
323 err = -EIO;
324 free(sys);
325 goto out;
328 free(sys);
330 err = 0;
331 out:
332 closedir(dir);
333 put_tracing_file(path);
335 return err;
338 static int record_proc_kallsyms(void)
340 unsigned long long size = 0;
342 * Just to keep older perf.data file parsers happy, record a zero
343 * sized kallsyms file, i.e. do the same thing that was done when
344 * /proc/kallsyms (or something specified via --kallsyms, in a
345 * different path) couldn't be read.
347 return write(output_fd, &size, 4) != 4 ? -EIO : 0;
350 static int record_ftrace_printk(void)
352 unsigned int size;
353 char *path;
354 struct stat st;
355 int ret, err = 0;
357 path = get_tracing_file("printk_formats");
358 if (!path) {
359 pr_debug("can't get tracing/printk_formats");
360 return -ENOMEM;
363 ret = stat(path, &st);
364 if (ret < 0) {
365 /* not found */
366 size = 0;
367 if (write(output_fd, &size, 4) != 4)
368 err = -EIO;
369 goto out;
371 err = record_file(path, 4);
373 out:
374 put_tracing_file(path);
375 return err;
378 static int record_saved_cmdline(void)
380 unsigned long long size;
381 char *path;
382 struct stat st;
383 int ret, err = 0;
385 path = get_tracing_file("saved_cmdlines");
386 if (!path) {
387 pr_debug("can't get tracing/saved_cmdline");
388 return -ENOMEM;
391 ret = stat(path, &st);
392 if (ret < 0) {
393 /* not found */
394 size = 0;
395 if (write(output_fd, &size, 8) != 8)
396 err = -EIO;
397 goto out;
399 err = record_file(path, 8);
401 out:
402 put_tracing_file(path);
403 return err;
406 static void
407 put_tracepoints_path(struct tracepoint_path *tps)
409 while (tps) {
410 struct tracepoint_path *t = tps;
412 tps = tps->next;
413 zfree(&t->name);
414 zfree(&t->system);
415 free(t);
419 static struct tracepoint_path *
420 get_tracepoints_path(struct list_head *pattrs)
422 struct tracepoint_path path, *ppath = &path;
423 struct perf_evsel *pos;
424 int nr_tracepoints = 0;
426 list_for_each_entry(pos, pattrs, node) {
427 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
428 continue;
429 ++nr_tracepoints;
431 if (pos->name) {
432 ppath->next = tracepoint_name_to_path(pos->name);
433 if (ppath->next)
434 goto next;
436 if (strchr(pos->name, ':') == NULL)
437 goto try_id;
439 goto error;
442 try_id:
443 ppath->next = tracepoint_id_to_path(pos->attr.config);
444 if (!ppath->next) {
445 error:
446 pr_debug("No memory to alloc tracepoints list\n");
447 put_tracepoints_path(&path);
448 return NULL;
450 next:
451 ppath = ppath->next;
454 return nr_tracepoints > 0 ? path.next : NULL;
457 bool have_tracepoints(struct list_head *pattrs)
459 struct perf_evsel *pos;
461 list_for_each_entry(pos, pattrs, node)
462 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
463 return true;
465 return false;
468 static int tracing_data_header(void)
470 char buf[20];
471 ssize_t size;
473 /* just guessing this is someone's birthday.. ;) */
474 buf[0] = 23;
475 buf[1] = 8;
476 buf[2] = 68;
477 memcpy(buf + 3, "tracing", 7);
479 if (write(output_fd, buf, 10) != 10)
480 return -1;
482 size = strlen(VERSION) + 1;
483 if (write(output_fd, VERSION, size) != size)
484 return -1;
486 /* save endian */
487 if (bigendian())
488 buf[0] = 1;
489 else
490 buf[0] = 0;
492 if (write(output_fd, buf, 1) != 1)
493 return -1;
495 /* save size of long */
496 buf[0] = sizeof(long);
497 if (write(output_fd, buf, 1) != 1)
498 return -1;
500 /* save page_size */
501 if (write(output_fd, &page_size, 4) != 4)
502 return -1;
504 return 0;
507 struct tracing_data *tracing_data_get(struct list_head *pattrs,
508 int fd, bool temp)
510 struct tracepoint_path *tps;
511 struct tracing_data *tdata;
512 int err;
514 output_fd = fd;
516 tps = get_tracepoints_path(pattrs);
517 if (!tps)
518 return NULL;
520 tdata = malloc(sizeof(*tdata));
521 if (!tdata)
522 return NULL;
524 tdata->temp = temp;
525 tdata->size = 0;
527 if (temp) {
528 int temp_fd;
530 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
531 "/tmp/perf-XXXXXX");
532 if (!mkstemp(tdata->temp_file)) {
533 pr_debug("Can't make temp file");
534 free(tdata);
535 return NULL;
538 temp_fd = open(tdata->temp_file, O_RDWR);
539 if (temp_fd < 0) {
540 pr_debug("Can't read '%s'", tdata->temp_file);
541 free(tdata);
542 return NULL;
546 * Set the temp file the default output, so all the
547 * tracing data are stored into it.
549 output_fd = temp_fd;
552 err = tracing_data_header();
553 if (err)
554 goto out;
555 err = record_header_files();
556 if (err)
557 goto out;
558 err = record_ftrace_files(tps);
559 if (err)
560 goto out;
561 err = record_event_files(tps);
562 if (err)
563 goto out;
564 err = record_proc_kallsyms();
565 if (err)
566 goto out;
567 err = record_ftrace_printk();
568 if (err)
569 goto out;
570 err = record_saved_cmdline();
572 out:
574 * All tracing data are stored by now, we can restore
575 * the default output file in case we used temp file.
577 if (temp) {
578 tdata->size = lseek(output_fd, 0, SEEK_CUR);
579 close(output_fd);
580 output_fd = fd;
583 if (err)
584 zfree(&tdata);
586 put_tracepoints_path(tps);
587 return tdata;
590 int tracing_data_put(struct tracing_data *tdata)
592 int err = 0;
594 if (tdata->temp) {
595 err = record_file(tdata->temp_file, 0);
596 unlink(tdata->temp_file);
599 free(tdata);
600 return err;
603 int read_tracing_data(int fd, struct list_head *pattrs)
605 int err;
606 struct tracing_data *tdata;
609 * We work over the real file, so we can write data
610 * directly, no temp file is needed.
612 tdata = tracing_data_get(pattrs, fd, false);
613 if (!tdata)
614 return -ENOMEM;
616 err = tracing_data_put(tdata);
617 return err;