1 // SPDX-License-Identifier: GPL-2.0-only
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) {
317 put_tracing_file(path);
322 static int record_proc_kallsyms(void)
324 unsigned long long size = 0;
326 * Just to keep older perf.data file parsers happy, record a zero
327 * sized kallsyms file, i.e. do the same thing that was done when
328 * /proc/kallsyms (or something specified via --kallsyms, in a
329 * different path) couldn't be read.
331 return write(output_fd, &size, 4) != 4 ? -EIO : 0;
334 static int record_ftrace_printk(void)
341 path = get_tracing_file("printk_formats");
343 pr_debug("can't get tracing/printk_formats");
347 ret = stat(path, &st);
351 if (write(output_fd, &size, 4) != 4)
355 err = record_file(path, 4);
358 put_tracing_file(path);
362 static int record_saved_cmdline(void)
364 unsigned long long size;
369 path = get_tracing_file("saved_cmdlines");
371 pr_debug("can't get tracing/saved_cmdline");
375 ret = stat(path, &st);
379 if (write(output_fd, &size, 8) != 8)
383 err = record_file(path, 8);
386 put_tracing_file(path);
391 put_tracepoints_path(struct tracepoint_path *tps)
394 struct tracepoint_path *t = tps;
403 static struct tracepoint_path *tracepoint_id_to_path(u64 config)
405 struct tracepoint_path *path = NULL;
406 DIR *sys_dir, *evt_dir;
407 struct dirent *sys_dirent, *evt_dirent;
411 char evt_path[MAXPATHLEN];
414 sys_dir = tracing_events__opendir();
418 for_each_subsystem(sys_dir, sys_dirent) {
419 dir_path = get_events_file(sys_dirent->d_name);
422 evt_dir = opendir(dir_path);
426 for_each_event(dir_path, evt_dir, evt_dirent) {
428 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
430 fd = open(evt_path, O_RDONLY);
433 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
440 put_events_file(dir_path);
443 path = zalloc(sizeof(*path));
446 if (asprintf(&path->system, "%.*s",
447 MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
451 if (asprintf(&path->name, "%.*s",
452 MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
453 zfree(&path->system);
462 put_events_file(dir_path);
469 static struct tracepoint_path *tracepoint_name_to_path(const char *name)
471 struct tracepoint_path *path = zalloc(sizeof(*path));
472 char *str = strchr(name, ':');
474 if (path == NULL || str == NULL) {
479 path->system = strndup(name, str - name);
480 path->name = strdup(str+1);
482 if (path->system == NULL || path->name == NULL) {
483 zfree(&path->system);
491 static struct tracepoint_path *
492 get_tracepoints_path(struct list_head *pattrs)
494 struct tracepoint_path path, *ppath = &path;
496 int nr_tracepoints = 0;
498 list_for_each_entry(pos, pattrs, core.node) {
499 if (pos->core.attr.type != PERF_TYPE_TRACEPOINT)
504 ppath->next = tracepoint_name_to_path(pos->name);
508 if (strchr(pos->name, ':') == NULL)
515 ppath->next = tracepoint_id_to_path(pos->core.attr.config);
518 pr_debug("No memory to alloc tracepoints list\n");
519 put_tracepoints_path(path.next);
526 return nr_tracepoints > 0 ? path.next : NULL;
529 bool have_tracepoints(struct list_head *pattrs)
533 list_for_each_entry(pos, pattrs, core.node)
534 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT)
540 static int tracing_data_header(void)
545 /* just guessing this is someone's birthday.. ;) */
549 memcpy(buf + 3, "tracing", 7);
551 if (write(output_fd, buf, 10) != 10)
554 size = strlen(VERSION) + 1;
555 if (write(output_fd, VERSION, size) != size)
559 if (host_is_bigendian())
564 if (write(output_fd, buf, 1) != 1)
567 /* save size of long */
568 buf[0] = sizeof(long);
569 if (write(output_fd, buf, 1) != 1)
573 if (write(output_fd, &page_size, 4) != 4)
579 struct tracing_data *tracing_data_get(struct list_head *pattrs,
582 struct tracepoint_path *tps;
583 struct tracing_data *tdata;
588 tps = get_tracepoints_path(pattrs);
592 tdata = malloc(sizeof(*tdata));
602 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
604 if (!mkstemp(tdata->temp_file)) {
605 pr_debug("Can't make temp file");
610 temp_fd = open(tdata->temp_file, O_RDWR);
612 pr_debug("Can't read '%s'", tdata->temp_file);
618 * Set the temp file the default output, so all the
619 * tracing data are stored into it.
624 err = tracing_data_header();
627 err = record_header_files();
630 err = record_ftrace_files(tps);
633 err = record_event_files(tps);
636 err = record_proc_kallsyms();
639 err = record_ftrace_printk();
642 err = record_saved_cmdline();
646 * All tracing data are stored by now, we can restore
647 * the default output file in case we used temp file.
650 tdata->size = lseek(output_fd, 0, SEEK_CUR);
658 put_tracepoints_path(tps);
662 int tracing_data_put(struct tracing_data *tdata)
667 err = record_file(tdata->temp_file, 0);
668 unlink(tdata->temp_file);
675 int read_tracing_data(int fd, struct list_head *pattrs)
678 struct tracing_data *tdata;
681 * We work over the real file, so we can write data
682 * directly, no temp file is needed.
684 tdata = tracing_data_get(pattrs, fd, false);
688 err = tracing_data_put(tdata);