1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
11 #include <sys/types.h>
18 #include <linux/list.h>
19 #include <linux/kernel.h>
20 #include <linux/zalloc.h>
21 #include <internal/lib.h> // page_size
22 #include <sys/param.h>
24 #include "trace-event.h"
25 #include "tracepoint.h"
26 #include <api/fs/tracing_path.h>
32 #define MAX_EVENT_LENGTH 512
36 struct tracepoint_path
{
39 struct tracepoint_path
*next
;
42 /* unfortunately, you can not stat debugfs or proc files for size */
43 static int record_file(const char *file
, ssize_t hdr_sz
)
45 unsigned long long size
= 0;
46 char buf
[BUFSIZ
], *sizep
;
47 off_t hdr_pos
= lseek(output_fd
, 0, SEEK_CUR
);
51 fd
= open(file
, O_RDONLY
);
53 pr_debug("Can't read '%s'", file
);
57 /* put in zeros for file size, then fill true size later */
59 if (write(output_fd
, &size
, hdr_sz
) != hdr_sz
)
64 r
= read(fd
, buf
, BUFSIZ
);
67 if (write(output_fd
, buf
, r
) != r
)
72 /* ugh, handle big-endian hdr_size == 4 */
74 if (host_is_bigendian())
75 sizep
+= sizeof(u64
) - hdr_sz
;
77 if (hdr_sz
&& pwrite(output_fd
, sizep
, hdr_sz
, hdr_pos
) < 0) {
78 pr_debug("writing file size failed\n");
88 static int record_header_files(void)
90 char *path
= get_events_file("header_page");
95 pr_debug("can't get tracing/events/header_page");
99 if (stat(path
, &st
) < 0) {
100 pr_debug("can't read '%s'", path
);
104 if (write(output_fd
, "header_page", 12) != 12) {
105 pr_debug("can't write header_page\n");
109 if (record_file(path
, 8) < 0) {
110 pr_debug("can't record header_page file\n");
114 put_events_file(path
);
116 path
= get_events_file("header_event");
118 pr_debug("can't get tracing/events/header_event");
123 if (stat(path
, &st
) < 0) {
124 pr_debug("can't read '%s'", path
);
128 if (write(output_fd
, "header_event", 13) != 13) {
129 pr_debug("can't write header_event\n");
133 if (record_file(path
, 8) < 0) {
134 pr_debug("can't record header_event file\n");
140 put_events_file(path
);
144 static bool name_in_tp_list(char *sys
, struct tracepoint_path
*tps
)
147 if (!strcmp(sys
, tps
->name
))
155 #define for_each_event_tps(dir, dent, tps) \
156 while ((dent = readdir(dir))) \
157 if (dent->d_type == DT_DIR && \
158 (strcmp(dent->d_name, ".")) && \
159 (strcmp(dent->d_name, ".."))) \
161 static int copy_event_system(const char *sys, struct tracepoint_path *tps)
173 pr_debug("can't read directory '%s'", sys
);
177 for_each_event_tps(dir
, dent
, tps
) {
178 if (!name_in_tp_list(dent
->d_name
, tps
))
181 if (asprintf(&format
, "%s/%s/format", sys
, dent
->d_name
) < 0) {
185 ret
= stat(format
, &st
);
192 if (write(output_fd
, &count
, 4) != 4) {
194 pr_debug("can't write count\n");
199 for_each_event_tps(dir
, dent
, tps
) {
200 if (!name_in_tp_list(dent
->d_name
, tps
))
203 if (asprintf(&format
, "%s/%s/format", sys
, dent
->d_name
) < 0) {
207 ret
= stat(format
, &st
);
210 err
= record_file(format
, 8);
224 static int record_ftrace_files(struct tracepoint_path
*tps
)
229 path
= get_events_file("ftrace");
231 pr_debug("can't get tracing/events/ftrace");
235 ret
= copy_event_system(path
, tps
);
237 put_tracing_file(path
);
242 static bool system_in_tp_list(char *sys
, struct tracepoint_path
*tps
)
245 if (!strcmp(sys
, tps
->system
))
253 static int record_event_files(struct tracepoint_path
*tps
)
264 path
= get_tracing_file("events");
266 pr_debug("can't get tracing/events");
273 pr_debug("can't read directory '%s'", path
);
277 for_each_event_tps(dir
, dent
, tps
) {
278 if (strcmp(dent
->d_name
, "ftrace") == 0 ||
279 !system_in_tp_list(dent
->d_name
, tps
))
285 if (write(output_fd
, &count
, 4) != 4) {
287 pr_debug("can't write count\n");
292 for_each_event_tps(dir
, dent
, tps
) {
293 if (strcmp(dent
->d_name
, "ftrace") == 0 ||
294 !system_in_tp_list(dent
->d_name
, tps
))
297 if (asprintf(&sys
, "%s/%s", path
, dent
->d_name
) < 0) {
301 ret
= stat(sys
, &st
);
303 ssize_t size
= strlen(dent
->d_name
) + 1;
305 if (write(output_fd
, dent
->d_name
, size
) != size
||
306 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
*tracepoint_id_to_path(u64 config
)
406 struct tracepoint_path
*path
= NULL
;
407 DIR *sys_dir
, *evt_dir
;
408 struct dirent
*sys_dirent
, *evt_dirent
;
412 char evt_path
[MAXPATHLEN
];
415 sys_dir
= tracing_events__opendir();
419 for_each_subsystem(sys_dir
, sys_dirent
) {
420 dir_path
= get_events_file(sys_dirent
->d_name
);
423 evt_dir
= opendir(dir_path
);
427 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
429 scnprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
,
431 fd
= open(evt_path
, O_RDONLY
);
434 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
441 put_events_file(dir_path
);
444 path
= zalloc(sizeof(*path
));
447 if (asprintf(&path
->system
, "%.*s",
448 MAX_EVENT_LENGTH
, sys_dirent
->d_name
) < 0) {
452 if (asprintf(&path
->name
, "%.*s",
453 MAX_EVENT_LENGTH
, evt_dirent
->d_name
) < 0) {
454 zfree(&path
->system
);
463 put_events_file(dir_path
);
470 char *tracepoint_id_to_name(u64 config
)
472 struct tracepoint_path
*path
= tracepoint_id_to_path(config
);
475 if (path
&& asprintf(&buf
, "%s:%s", path
->system
, path
->name
) < 0)
478 put_tracepoints_path(path
);
482 static struct tracepoint_path
*tracepoint_name_to_path(const char *name
)
484 struct tracepoint_path
*path
= zalloc(sizeof(*path
));
485 char *str
= strchr(name
, ':');
487 if (path
== NULL
|| str
== NULL
) {
492 path
->system
= strndup(name
, str
- name
);
493 path
->name
= strdup(str
+1);
495 if (path
->system
== NULL
|| path
->name
== NULL
) {
496 zfree(&path
->system
);
504 static struct tracepoint_path
*
505 get_tracepoints_path(struct list_head
*pattrs
)
507 struct tracepoint_path path
, *ppath
= &path
;
509 int nr_tracepoints
= 0;
511 list_for_each_entry(pos
, pattrs
, core
.node
) {
512 if (pos
->core
.attr
.type
!= PERF_TYPE_TRACEPOINT
)
517 ppath
->next
= tracepoint_name_to_path(pos
->name
);
521 if (strchr(pos
->name
, ':') == NULL
)
528 ppath
->next
= tracepoint_id_to_path(pos
->core
.attr
.config
);
531 pr_debug("No memory to alloc tracepoints list\n");
532 put_tracepoints_path(path
.next
);
539 return nr_tracepoints
> 0 ? path
.next
: NULL
;
542 bool have_tracepoints(struct list_head
*pattrs
)
546 list_for_each_entry(pos
, pattrs
, core
.node
)
547 if (pos
->core
.attr
.type
== PERF_TYPE_TRACEPOINT
)
553 static int tracing_data_header(void)
558 /* just guessing this is someone's birthday.. ;) */
562 memcpy(buf
+ 3, "tracing", 7);
564 if (write(output_fd
, buf
, 10) != 10)
567 size
= strlen(VERSION
) + 1;
568 if (write(output_fd
, VERSION
, size
) != size
)
572 if (host_is_bigendian())
577 if (write(output_fd
, buf
, 1) != 1)
580 /* save size of long */
581 buf
[0] = sizeof(long);
582 if (write(output_fd
, buf
, 1) != 1)
586 if (write(output_fd
, &page_size
, 4) != 4)
592 struct tracing_data
*tracing_data_get(struct list_head
*pattrs
,
595 struct tracepoint_path
*tps
;
596 struct tracing_data
*tdata
;
601 tps
= get_tracepoints_path(pattrs
);
605 tdata
= malloc(sizeof(*tdata
));
615 snprintf(tdata
->temp_file
, sizeof(tdata
->temp_file
),
617 if (!mkstemp(tdata
->temp_file
)) {
618 pr_debug("Can't make temp file");
623 temp_fd
= open(tdata
->temp_file
, O_RDWR
);
625 pr_debug("Can't read '%s'", tdata
->temp_file
);
631 * Set the temp file the default output, so all the
632 * tracing data are stored into it.
637 err
= tracing_data_header();
640 err
= record_header_files();
643 err
= record_ftrace_files(tps
);
646 err
= record_event_files(tps
);
649 err
= record_proc_kallsyms();
652 err
= record_ftrace_printk();
655 err
= record_saved_cmdline();
659 * All tracing data are stored by now, we can restore
660 * the default output file in case we used temp file.
663 tdata
->size
= lseek(output_fd
, 0, SEEK_CUR
);
671 put_tracepoints_path(tps
);
675 int tracing_data_put(struct tracing_data
*tdata
)
680 err
= record_file(tdata
->temp_file
, 0);
681 unlink(tdata
->temp_file
);
688 int read_tracing_data(int fd
, struct list_head
*pattrs
)
691 struct tracing_data
*tdata
;
694 * We work over the real file, so we can write data
695 * directly, no temp file is needed.
697 tdata
= tracing_data_get(pattrs
, fd
, false);
701 err
= tracing_data_put(tdata
);