]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - tools/perf/util/trace-event-info.c
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
12 #include <sys/types.h>
19 #include <linux/list.h>
20 #include <linux/kernel.h>
21 #include <linux/zalloc.h>
24 #include "trace-event.h"
25 #include <api/fs/tracing_path.h>
36 unsigned char str
[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
39 ptr
= (unsigned int *)(void *)str
;
40 return *ptr
== 0x01020304;
43 /* unfortunately, you can not stat debugfs or proc files for size */
44 static int record_file(const char *file
, ssize_t hdr_sz
)
46 unsigned long long size
= 0;
47 char buf
[BUFSIZ
], *sizep
;
48 off_t hdr_pos
= lseek(output_fd
, 0, SEEK_CUR
);
52 fd
= open(file
, O_RDONLY
);
54 pr_debug("Can't read '%s'", file
);
58 /* put in zeros for file size, then fill true size later */
60 if (write(output_fd
, &size
, hdr_sz
) != hdr_sz
)
65 r
= read(fd
, buf
, BUFSIZ
);
68 if (write(output_fd
, buf
, r
) != r
)
73 /* ugh, handle big-endian hdr_size == 4 */
76 sizep
+= sizeof(u64
) - hdr_sz
;
78 if (hdr_sz
&& pwrite(output_fd
, sizep
, hdr_sz
, hdr_pos
) < 0) {
79 pr_debug("writing file size failed\n");
89 static int record_header_files(void)
91 char *path
= get_events_file("header_page");
96 pr_debug("can't get tracing/events/header_page");
100 if (stat(path
, &st
) < 0) {
101 pr_debug("can't read '%s'", path
);
105 if (write(output_fd
, "header_page", 12) != 12) {
106 pr_debug("can't write header_page\n");
110 if (record_file(path
, 8) < 0) {
111 pr_debug("can't record header_page file\n");
115 put_events_file(path
);
117 path
= get_events_file("header_event");
119 pr_debug("can't get tracing/events/header_event");
124 if (stat(path
, &st
) < 0) {
125 pr_debug("can't read '%s'", path
);
129 if (write(output_fd
, "header_event", 13) != 13) {
130 pr_debug("can't write header_event\n");
134 if (record_file(path
, 8) < 0) {
135 pr_debug("can't record header_event file\n");
141 put_events_file(path
);
145 static bool name_in_tp_list(char *sys
, struct tracepoint_path
*tps
)
148 if (!strcmp(sys
, tps
->name
))
156 #define for_each_event(dir, dent, tps) \
157 while ((dent = readdir(dir))) \
158 if (dent->d_type == DT_DIR && \
159 (strcmp(dent->d_name, ".")) && \
160 (strcmp(dent->d_name, ".."))) \
162 static int copy_event_system(const char *sys, struct tracepoint_path *tps)
174 pr_debug("can't read directory '%s'", sys
);
178 for_each_event(dir
, dent
, tps
) {
179 if (!name_in_tp_list(dent
->d_name
, tps
))
182 if (asprintf(&format
, "%s/%s/format", sys
, dent
->d_name
) < 0) {
186 ret
= stat(format
, &st
);
193 if (write(output_fd
, &count
, 4) != 4) {
195 pr_debug("can't write count\n");
200 for_each_event(dir
, dent
, tps
) {
201 if (!name_in_tp_list(dent
->d_name
, tps
))
204 if (asprintf(&format
, "%s/%s/format", sys
, dent
->d_name
) < 0) {
208 ret
= stat(format
, &st
);
211 err
= record_file(format
, 8);
225 static int record_ftrace_files(struct tracepoint_path
*tps
)
230 path
= get_events_file("ftrace");
232 pr_debug("can't get tracing/events/ftrace");
236 ret
= copy_event_system(path
, tps
);
238 put_tracing_file(path
);
243 static bool system_in_tp_list(char *sys
, struct tracepoint_path
*tps
)
246 if (!strcmp(sys
, tps
->system
))
254 static int record_event_files(struct tracepoint_path
*tps
)
265 path
= get_tracing_file("events");
267 pr_debug("can't get tracing/events");
274 pr_debug("can't read directory '%s'", path
);
278 for_each_event(dir
, dent
, tps
) {
279 if (strcmp(dent
->d_name
, "ftrace") == 0 ||
280 !system_in_tp_list(dent
->d_name
, tps
))
286 if (write(output_fd
, &count
, 4) != 4) {
288 pr_debug("can't write count\n");
293 for_each_event(dir
, dent
, tps
) {
294 if (strcmp(dent
->d_name
, "ftrace") == 0 ||
295 !system_in_tp_list(dent
->d_name
, tps
))
298 if (asprintf(&sys
, "%s/%s", path
, dent
->d_name
) < 0) {
302 ret
= stat(sys
, &st
);
304 ssize_t size
= strlen(dent
->d_name
) + 1;
306 if (write(output_fd
, dent
->d_name
, size
) != size
||
307 copy_event_system(sys
, tps
) < 0) {
318 put_tracing_file(path
);
323 static int record_proc_kallsyms(void)
325 unsigned long long size
= 0;
327 * Just to keep older perf.data file parsers happy, record a zero
328 * sized kallsyms file, i.e. do the same thing that was done when
329 * /proc/kallsyms (or something specified via --kallsyms, in a
330 * different path) couldn't be read.
332 return write(output_fd
, &size
, 4) != 4 ? -EIO
: 0;
335 static int record_ftrace_printk(void)
342 path
= get_tracing_file("printk_formats");
344 pr_debug("can't get tracing/printk_formats");
348 ret
= stat(path
, &st
);
352 if (write(output_fd
, &size
, 4) != 4)
356 err
= record_file(path
, 4);
359 put_tracing_file(path
);
363 static int record_saved_cmdline(void)
365 unsigned long long size
;
370 path
= get_tracing_file("saved_cmdlines");
372 pr_debug("can't get tracing/saved_cmdline");
376 ret
= stat(path
, &st
);
380 if (write(output_fd
, &size
, 8) != 8)
384 err
= record_file(path
, 8);
387 put_tracing_file(path
);
392 put_tracepoints_path(struct tracepoint_path
*tps
)
395 struct tracepoint_path
*t
= tps
;
404 static struct tracepoint_path
*
405 get_tracepoints_path(struct list_head
*pattrs
)
407 struct tracepoint_path path
, *ppath
= &path
;
408 struct perf_evsel
*pos
;
409 int nr_tracepoints
= 0;
411 list_for_each_entry(pos
, pattrs
, node
) {
412 if (pos
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
417 ppath
->next
= tracepoint_name_to_path(pos
->name
);
421 if (strchr(pos
->name
, ':') == NULL
)
428 ppath
->next
= tracepoint_id_to_path(pos
->attr
.config
);
431 pr_debug("No memory to alloc tracepoints list\n");
432 put_tracepoints_path(&path
);
439 return nr_tracepoints
> 0 ? path
.next
: NULL
;
442 bool have_tracepoints(struct list_head
*pattrs
)
444 struct perf_evsel
*pos
;
446 list_for_each_entry(pos
, pattrs
, node
)
447 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
)
453 static int tracing_data_header(void)
458 /* just guessing this is someone's birthday.. ;) */
462 memcpy(buf
+ 3, "tracing", 7);
464 if (write(output_fd
, buf
, 10) != 10)
467 size
= strlen(VERSION
) + 1;
468 if (write(output_fd
, VERSION
, size
) != size
)
477 if (write(output_fd
, buf
, 1) != 1)
480 /* save size of long */
481 buf
[0] = sizeof(long);
482 if (write(output_fd
, buf
, 1) != 1)
486 if (write(output_fd
, &page_size
, 4) != 4)
492 struct tracing_data
*tracing_data_get(struct list_head
*pattrs
,
495 struct tracepoint_path
*tps
;
496 struct tracing_data
*tdata
;
501 tps
= get_tracepoints_path(pattrs
);
505 tdata
= malloc(sizeof(*tdata
));
515 snprintf(tdata
->temp_file
, sizeof(tdata
->temp_file
),
517 if (!mkstemp(tdata
->temp_file
)) {
518 pr_debug("Can't make temp file");
523 temp_fd
= open(tdata
->temp_file
, O_RDWR
);
525 pr_debug("Can't read '%s'", tdata
->temp_file
);
531 * Set the temp file the default output, so all the
532 * tracing data are stored into it.
537 err
= tracing_data_header();
540 err
= record_header_files();
543 err
= record_ftrace_files(tps
);
546 err
= record_event_files(tps
);
549 err
= record_proc_kallsyms();
552 err
= record_ftrace_printk();
555 err
= record_saved_cmdline();
559 * All tracing data are stored by now, we can restore
560 * the default output file in case we used temp file.
563 tdata
->size
= lseek(output_fd
, 0, SEEK_CUR
);
571 put_tracepoints_path(tps
);
575 int tracing_data_put(struct tracing_data
*tdata
)
580 err
= record_file(tdata
->temp_file
, 0);
581 unlink(tdata
->temp_file
);
588 int read_tracing_data(int fd
, struct list_head
*pattrs
)
591 struct tracing_data
*tdata
;
594 * We work over the real file, so we can write data
595 * directly, no temp file is needed.
597 tdata
= tracing_data_get(pattrs
, fd
, false);
601 err
= tracing_data_put(tdata
);