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>
31 #define MAX_EVENT_LENGTH 512
35 struct tracepoint_path {
38 struct tracepoint_path *next;
43 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
46 ptr = (unsigned int *)(void *)str;
47 return *ptr == 0x01020304;
50 /* unfortunately, you can not stat debugfs or proc files for size */
51 static int record_file(const char *file, ssize_t hdr_sz)
53 unsigned long long size = 0;
54 char buf[BUFSIZ], *sizep;
55 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
59 fd = open(file, O_RDONLY);
61 pr_debug("Can't read '%s'", file);
65 /* put in zeros for file size, then fill true size later */
67 if (write(output_fd, &size, hdr_sz) != hdr_sz)
72 r = read(fd, buf, BUFSIZ);
75 if (write(output_fd, buf, r) != r)
80 /* ugh, handle big-endian hdr_size == 4 */
83 sizep += sizeof(u64) - hdr_sz;
85 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) {
86 pr_debug("writing file size failed\n");
96 static int record_header_files(void)
98 char *path = get_events_file("header_page");
103 pr_debug("can't get tracing/events/header_page");
107 if (stat(path, &st) < 0) {
108 pr_debug("can't read '%s'", path);
112 if (write(output_fd, "header_page", 12) != 12) {
113 pr_debug("can't write header_page\n");
117 if (record_file(path, 8) < 0) {
118 pr_debug("can't record header_page file\n");
122 put_events_file(path);
124 path = get_events_file("header_event");
126 pr_debug("can't get tracing/events/header_event");
131 if (stat(path, &st) < 0) {
132 pr_debug("can't read '%s'", path);
136 if (write(output_fd, "header_event", 13) != 13) {
137 pr_debug("can't write header_event\n");
141 if (record_file(path, 8) < 0) {
142 pr_debug("can't record header_event file\n");
148 put_events_file(path);
152 static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
155 if (!strcmp(sys, tps->name))
163 #define for_each_event_tps(dir, dent, tps) \
164 while ((dent = readdir(dir))) \
165 if (dent->d_type == DT_DIR && \
166 (strcmp(dent->d_name, ".")) && \
167 (strcmp(dent->d_name, ".."))) \
169 static int copy_event_system(const char *sys, struct tracepoint_path *tps)
181 pr_debug("can't read directory '%s'", sys);
185 for_each_event_tps(dir, dent, tps) {
186 if (!name_in_tp_list(dent->d_name, tps))
189 if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
193 ret = stat(format, &st);
200 if (write(output_fd, &count, 4) != 4) {
202 pr_debug("can't write count\n");
207 for_each_event_tps(dir, dent, tps) {
208 if (!name_in_tp_list(dent->d_name, tps))
211 if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
215 ret = stat(format, &st);
218 err = record_file(format, 8);
232 static int record_ftrace_files(struct tracepoint_path *tps)
237 path = get_events_file("ftrace");
239 pr_debug("can't get tracing/events/ftrace");
243 ret = copy_event_system(path, tps);
245 put_tracing_file(path);
250 static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
253 if (!strcmp(sys, tps->system))
261 static int record_event_files(struct tracepoint_path *tps)
272 path = get_tracing_file("events");
274 pr_debug("can't get tracing/events");
281 pr_debug("can't read directory '%s'", path);
285 for_each_event_tps(dir, dent, tps) {
286 if (strcmp(dent->d_name, "ftrace") == 0 ||
287 !system_in_tp_list(dent->d_name, tps))
293 if (write(output_fd, &count, 4) != 4) {
295 pr_debug("can't write count\n");
300 for_each_event_tps(dir, dent, tps) {
301 if (strcmp(dent->d_name, "ftrace") == 0 ||
302 !system_in_tp_list(dent->d_name, tps))
305 if (asprintf(&sys, "%s/%s", path, dent->d_name) < 0) {
309 ret = stat(sys, &st);
311 ssize_t size = strlen(dent->d_name) + 1;
313 if (write(output_fd, dent->d_name, size) != size ||
314 copy_event_system(sys, tps) < 0) {
325 put_tracing_file(path);
330 static int record_proc_kallsyms(void)
332 unsigned long long size = 0;
334 * Just to keep older perf.data file parsers happy, record a zero
335 * sized kallsyms file, i.e. do the same thing that was done when
336 * /proc/kallsyms (or something specified via --kallsyms, in a
337 * different path) couldn't be read.
339 return write(output_fd, &size, 4) != 4 ? -EIO : 0;
342 static int record_ftrace_printk(void)
349 path = get_tracing_file("printk_formats");
351 pr_debug("can't get tracing/printk_formats");
355 ret = stat(path, &st);
359 if (write(output_fd, &size, 4) != 4)
363 err = record_file(path, 4);
366 put_tracing_file(path);
370 static int record_saved_cmdline(void)
372 unsigned long long size;
377 path = get_tracing_file("saved_cmdlines");
379 pr_debug("can't get tracing/saved_cmdline");
383 ret = stat(path, &st);
387 if (write(output_fd, &size, 8) != 8)
391 err = record_file(path, 8);
394 put_tracing_file(path);
399 put_tracepoints_path(struct tracepoint_path *tps)
402 struct tracepoint_path *t = tps;
411 static struct tracepoint_path *tracepoint_id_to_path(u64 config)
413 struct tracepoint_path *path = NULL;
414 DIR *sys_dir, *evt_dir;
415 struct dirent *sys_dirent, *evt_dirent;
419 char evt_path[MAXPATHLEN];
422 sys_dir = tracing_events__opendir();
426 for_each_subsystem(sys_dir, sys_dirent) {
427 dir_path = get_events_file(sys_dirent->d_name);
430 evt_dir = opendir(dir_path);
434 for_each_event(dir_path, evt_dir, evt_dirent) {
436 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
438 fd = open(evt_path, O_RDONLY);
441 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
448 put_events_file(dir_path);
451 path = zalloc(sizeof(*path));
454 if (asprintf(&path->system, "%.*s",
455 MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
459 if (asprintf(&path->name, "%.*s",
460 MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
461 zfree(&path->system);
470 put_events_file(dir_path);
477 static struct tracepoint_path *tracepoint_name_to_path(const char *name)
479 struct tracepoint_path *path = zalloc(sizeof(*path));
480 char *str = strchr(name, ':');
482 if (path == NULL || str == NULL) {
487 path->system = strndup(name, str - name);
488 path->name = strdup(str+1);
490 if (path->system == NULL || path->name == NULL) {
491 zfree(&path->system);
499 static struct tracepoint_path *
500 get_tracepoints_path(struct list_head *pattrs)
502 struct tracepoint_path path, *ppath = &path;
504 int nr_tracepoints = 0;
506 list_for_each_entry(pos, pattrs, core.node) {
507 if (pos->core.attr.type != PERF_TYPE_TRACEPOINT)
512 ppath->next = tracepoint_name_to_path(pos->name);
516 if (strchr(pos->name, ':') == NULL)
523 ppath->next = tracepoint_id_to_path(pos->core.attr.config);
526 pr_debug("No memory to alloc tracepoints list\n");
527 put_tracepoints_path(path.next);
534 return nr_tracepoints > 0 ? path.next : NULL;
537 bool have_tracepoints(struct list_head *pattrs)
541 list_for_each_entry(pos, pattrs, core.node)
542 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT)
548 static int tracing_data_header(void)
553 /* just guessing this is someone's birthday.. ;) */
557 memcpy(buf + 3, "tracing", 7);
559 if (write(output_fd, buf, 10) != 10)
562 size = strlen(VERSION) + 1;
563 if (write(output_fd, VERSION, size) != size)
572 if (write(output_fd, buf, 1) != 1)
575 /* save size of long */
576 buf[0] = sizeof(long);
577 if (write(output_fd, buf, 1) != 1)
581 if (write(output_fd, &page_size, 4) != 4)
587 struct tracing_data *tracing_data_get(struct list_head *pattrs,
590 struct tracepoint_path *tps;
591 struct tracing_data *tdata;
596 tps = get_tracepoints_path(pattrs);
600 tdata = malloc(sizeof(*tdata));
610 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
612 if (!mkstemp(tdata->temp_file)) {
613 pr_debug("Can't make temp file");
618 temp_fd = open(tdata->temp_file, O_RDWR);
620 pr_debug("Can't read '%s'", tdata->temp_file);
626 * Set the temp file the default output, so all the
627 * tracing data are stored into it.
632 err = tracing_data_header();
635 err = record_header_files();
638 err = record_ftrace_files(tps);
641 err = record_event_files(tps);
644 err = record_proc_kallsyms();
647 err = record_ftrace_printk();
650 err = record_saved_cmdline();
654 * All tracing data are stored by now, we can restore
655 * the default output file in case we used temp file.
658 tdata->size = lseek(output_fd, 0, SEEK_CUR);
666 put_tracepoints_path(tps);
670 int tracing_data_put(struct tracing_data *tdata)
675 err = record_file(tdata->temp_file, 0);
676 unlink(tdata->temp_file);
683 int read_tracing_data(int fd, struct list_head *pattrs)
686 struct tracing_data *tdata;
689 * We work over the real file, so we can write data
690 * directly, no temp file is needed.
692 tdata = tracing_data_get(pattrs, fd, false);
696 err = tracing_data_put(tdata);