2 * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <sys/types.h>
36 #include <linux/list.h>
37 #include <linux/kernel.h>
40 #include "trace-event.h"
41 #include <lk/debugfs.h>
49 static const char *find_debugfs(void)
51 const char *path
= perf_debugfs_mount(NULL
);
54 pr_debug("Your kernel does not support the debugfs filesystem");
60 * Finds the path to the debugfs/tracing
61 * Allocates the string and stores it.
63 static const char *find_tracing_dir(void)
66 static int tracing_found
;
72 debugfs
= find_debugfs();
76 tracing
= malloc(strlen(debugfs
) + 9);
80 sprintf(tracing
, "%s/tracing", debugfs
);
86 static char *get_tracing_file(const char *name
)
91 tracing
= find_tracing_dir();
95 file
= malloc(strlen(tracing
) + strlen(name
) + 2);
99 sprintf(file
, "%s/%s", tracing
, name
);
103 static void put_tracing_file(char *file
)
110 unsigned char str
[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
113 ptr
= (unsigned int *)(void *)str
;
114 return *ptr
== 0x01020304;
117 /* unfortunately, you can not stat debugfs or proc files for size */
118 static int record_file(const char *file
, ssize_t hdr_sz
)
120 unsigned long long size
= 0;
121 char buf
[BUFSIZ
], *sizep
;
122 off_t hdr_pos
= lseek(output_fd
, 0, SEEK_CUR
);
126 fd
= open(file
, O_RDONLY
);
128 pr_debug("Can't read '%s'", file
);
132 /* put in zeros for file size, then fill true size later */
134 if (write(output_fd
, &size
, hdr_sz
) != hdr_sz
)
139 r
= read(fd
, buf
, BUFSIZ
);
142 if (write(output_fd
, buf
, r
) != r
)
147 /* ugh, handle big-endian hdr_size == 4 */
148 sizep
= (char*)&size
;
150 sizep
+= sizeof(u64
) - hdr_sz
;
152 if (hdr_sz
&& pwrite(output_fd
, sizep
, hdr_sz
, hdr_pos
) < 0) {
153 pr_debug("writing file size failed\n");
163 static int read_header_files(void)
169 path
= get_tracing_file("events/header_page");
171 pr_debug("can't get tracing/events/header_page");
175 if (stat(path
, &st
) < 0) {
176 pr_debug("can't read '%s'", path
);
180 if (write(output_fd
, "header_page", 12) != 12) {
181 pr_debug("can't write header_page\n");
185 if (record_file(path
, 8) < 0) {
186 pr_debug("can't record header_page file\n");
190 put_tracing_file(path
);
192 path
= get_tracing_file("events/header_event");
194 pr_debug("can't get tracing/events/header_event");
199 if (stat(path
, &st
) < 0) {
200 pr_debug("can't read '%s'", path
);
204 if (write(output_fd
, "header_event", 13) != 13) {
205 pr_debug("can't write header_event\n");
209 if (record_file(path
, 8) < 0) {
210 pr_debug("can't record header_event file\n");
216 put_tracing_file(path
);
220 static bool name_in_tp_list(char *sys
, struct tracepoint_path
*tps
)
223 if (!strcmp(sys
, tps
->name
))
231 static int copy_event_system(const char *sys
, struct tracepoint_path
*tps
)
243 pr_debug("can't read directory '%s'", sys
);
247 while ((dent
= readdir(dir
))) {
248 if (dent
->d_type
!= DT_DIR
||
249 strcmp(dent
->d_name
, ".") == 0 ||
250 strcmp(dent
->d_name
, "..") == 0 ||
251 !name_in_tp_list(dent
->d_name
, tps
))
253 format
= malloc(strlen(sys
) + strlen(dent
->d_name
) + 10);
258 sprintf(format
, "%s/%s/format", sys
, dent
->d_name
);
259 ret
= stat(format
, &st
);
266 if (write(output_fd
, &count
, 4) != 4) {
268 pr_debug("can't write count\n");
273 while ((dent
= readdir(dir
))) {
274 if (dent
->d_type
!= DT_DIR
||
275 strcmp(dent
->d_name
, ".") == 0 ||
276 strcmp(dent
->d_name
, "..") == 0 ||
277 !name_in_tp_list(dent
->d_name
, tps
))
279 format
= malloc(strlen(sys
) + strlen(dent
->d_name
) + 10);
284 sprintf(format
, "%s/%s/format", sys
, dent
->d_name
);
285 ret
= stat(format
, &st
);
288 err
= record_file(format
, 8);
302 static int read_ftrace_files(struct tracepoint_path
*tps
)
307 path
= get_tracing_file("events/ftrace");
309 pr_debug("can't get tracing/events/ftrace");
313 ret
= copy_event_system(path
, tps
);
315 put_tracing_file(path
);
320 static bool system_in_tp_list(char *sys
, struct tracepoint_path
*tps
)
323 if (!strcmp(sys
, tps
->system
))
331 static int read_event_files(struct tracepoint_path
*tps
)
342 path
= get_tracing_file("events");
344 pr_debug("can't get tracing/events");
351 pr_debug("can't read directory '%s'", path
);
355 while ((dent
= readdir(dir
))) {
356 if (dent
->d_type
!= DT_DIR
||
357 strcmp(dent
->d_name
, ".") == 0 ||
358 strcmp(dent
->d_name
, "..") == 0 ||
359 strcmp(dent
->d_name
, "ftrace") == 0 ||
360 !system_in_tp_list(dent
->d_name
, tps
))
365 if (write(output_fd
, &count
, 4) != 4) {
367 pr_debug("can't write count\n");
372 while ((dent
= readdir(dir
))) {
373 if (dent
->d_type
!= DT_DIR
||
374 strcmp(dent
->d_name
, ".") == 0 ||
375 strcmp(dent
->d_name
, "..") == 0 ||
376 strcmp(dent
->d_name
, "ftrace") == 0 ||
377 !system_in_tp_list(dent
->d_name
, tps
))
379 sys
= malloc(strlen(path
) + strlen(dent
->d_name
) + 2);
384 sprintf(sys
, "%s/%s", path
, dent
->d_name
);
385 ret
= stat(sys
, &st
);
387 ssize_t size
= strlen(dent
->d_name
) + 1;
389 if (write(output_fd
, dent
->d_name
, size
) != size
||
390 copy_event_system(sys
, tps
) < 0) {
401 put_tracing_file(path
);
406 static int read_proc_kallsyms(void)
409 const char *path
= "/proc/kallsyms";
413 ret
= stat(path
, &st
);
417 if (write(output_fd
, &size
, 4) != 4)
421 return record_file(path
, 4);
424 static int read_ftrace_printk(void)
431 path
= get_tracing_file("printk_formats");
433 pr_debug("can't get tracing/printk_formats");
437 ret
= stat(path
, &st
);
441 if (write(output_fd
, &size
, 4) != 4)
445 err
= record_file(path
, 4);
448 put_tracing_file(path
);
453 put_tracepoints_path(struct tracepoint_path
*tps
)
456 struct tracepoint_path
*t
= tps
;
465 static struct tracepoint_path
*
466 get_tracepoints_path(struct list_head
*pattrs
)
468 struct tracepoint_path path
, *ppath
= &path
;
469 struct perf_evsel
*pos
;
470 int nr_tracepoints
= 0;
472 list_for_each_entry(pos
, pattrs
, node
) {
473 if (pos
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
476 ppath
->next
= tracepoint_id_to_path(pos
->attr
.config
);
478 pr_debug("No memory to alloc tracepoints list\n");
479 put_tracepoints_path(&path
);
485 return nr_tracepoints
> 0 ? path
.next
: NULL
;
488 bool have_tracepoints(struct list_head
*pattrs
)
490 struct perf_evsel
*pos
;
492 list_for_each_entry(pos
, pattrs
, node
)
493 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
)
499 static int tracing_data_header(void)
504 /* just guessing this is someone's birthday.. ;) */
508 memcpy(buf
+ 3, "tracing", 7);
510 if (write(output_fd
, buf
, 10) != 10)
513 size
= strlen(VERSION
) + 1;
514 if (write(output_fd
, VERSION
, size
) != size
)
523 read_trace_init(buf
[0], buf
[0]);
525 if (write(output_fd
, buf
, 1) != 1)
528 /* save size of long */
529 buf
[0] = sizeof(long);
530 if (write(output_fd
, buf
, 1) != 1)
534 if (write(output_fd
, &page_size
, 4) != 4)
540 struct tracing_data
*tracing_data_get(struct list_head
*pattrs
,
543 struct tracepoint_path
*tps
;
544 struct tracing_data
*tdata
;
549 tps
= get_tracepoints_path(pattrs
);
553 tdata
= malloc(sizeof(*tdata
));
563 snprintf(tdata
->temp_file
, sizeof(tdata
->temp_file
),
565 if (!mkstemp(tdata
->temp_file
)) {
566 pr_debug("Can't make temp file");
570 temp_fd
= open(tdata
->temp_file
, O_RDWR
);
572 pr_debug("Can't read '%s'", tdata
->temp_file
);
577 * Set the temp file the default output, so all the
578 * tracing data are stored into it.
583 err
= tracing_data_header();
586 err
= read_header_files();
589 err
= read_ftrace_files(tps
);
592 err
= read_event_files(tps
);
595 err
= read_proc_kallsyms();
598 err
= read_ftrace_printk();
602 * All tracing data are stored by now, we can restore
603 * the default output file in case we used temp file.
606 tdata
->size
= lseek(output_fd
, 0, SEEK_CUR
);
616 put_tracepoints_path(tps
);
620 int tracing_data_put(struct tracing_data
*tdata
)
625 err
= record_file(tdata
->temp_file
, 0);
626 unlink(tdata
->temp_file
);
633 int read_tracing_data(int fd
, struct list_head
*pattrs
)
636 struct tracing_data
*tdata
;
639 * We work over the real file, so we can write data
640 * directly, no temp file is needed.
642 tdata
= tracing_data_get(pattrs
, fd
, false);
646 err
= tracing_data_put(tdata
);