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
23 #include "trace-event.h"
24 #include <api/fs/tracing_path.h>
35 unsigned char str
[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
38 ptr
= (unsigned int *)(void *)str
;
39 return *ptr
== 0x01020304;
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 */
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(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(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(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(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(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
*
404 get_tracepoints_path(struct list_head
*pattrs
)
406 struct tracepoint_path path
, *ppath
= &path
;
408 int nr_tracepoints
= 0;
410 list_for_each_entry(pos
, pattrs
, core
.node
) {
411 if (pos
->core
.attr
.type
!= PERF_TYPE_TRACEPOINT
)
416 ppath
->next
= tracepoint_name_to_path(pos
->name
);
420 if (strchr(pos
->name
, ':') == NULL
)
427 ppath
->next
= tracepoint_id_to_path(pos
->core
.attr
.config
);
430 pr_debug("No memory to alloc tracepoints list\n");
431 put_tracepoints_path(path
.next
);
438 return nr_tracepoints
> 0 ? path
.next
: NULL
;
441 bool have_tracepoints(struct list_head
*pattrs
)
445 list_for_each_entry(pos
, pattrs
, core
.node
)
446 if (pos
->core
.attr
.type
== PERF_TYPE_TRACEPOINT
)
452 static int tracing_data_header(void)
457 /* just guessing this is someone's birthday.. ;) */
461 memcpy(buf
+ 3, "tracing", 7);
463 if (write(output_fd
, buf
, 10) != 10)
466 size
= strlen(VERSION
) + 1;
467 if (write(output_fd
, VERSION
, size
) != size
)
476 if (write(output_fd
, buf
, 1) != 1)
479 /* save size of long */
480 buf
[0] = sizeof(long);
481 if (write(output_fd
, buf
, 1) != 1)
485 if (write(output_fd
, &page_size
, 4) != 4)
491 struct tracing_data
*tracing_data_get(struct list_head
*pattrs
,
494 struct tracepoint_path
*tps
;
495 struct tracing_data
*tdata
;
500 tps
= get_tracepoints_path(pattrs
);
504 tdata
= malloc(sizeof(*tdata
));
514 snprintf(tdata
->temp_file
, sizeof(tdata
->temp_file
),
516 if (!mkstemp(tdata
->temp_file
)) {
517 pr_debug("Can't make temp file");
522 temp_fd
= open(tdata
->temp_file
, O_RDWR
);
524 pr_debug("Can't read '%s'", tdata
->temp_file
);
530 * Set the temp file the default output, so all the
531 * tracing data are stored into it.
536 err
= tracing_data_header();
539 err
= record_header_files();
542 err
= record_ftrace_files(tps
);
545 err
= record_event_files(tps
);
548 err
= record_proc_kallsyms();
551 err
= record_ftrace_printk();
554 err
= record_saved_cmdline();
558 * All tracing data are stored by now, we can restore
559 * the default output file in case we used temp file.
562 tdata
->size
= lseek(output_fd
, 0, SEEK_CUR
);
570 put_tracepoints_path(tps
);
574 int tracing_data_put(struct tracing_data
*tdata
)
579 err
= record_file(tdata
->temp_file
, 0);
580 unlink(tdata
->temp_file
);
587 int read_tracing_data(int fd
, struct list_head
*pattrs
)
590 struct tracing_data
*tdata
;
593 * We work over the real file, so we can write data
594 * directly, no temp file is needed.
596 tdata
= tracing_data_get(pattrs
, fd
, false);
600 err
= tracing_data_put(tdata
);