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>
46 unsigned int page_size;
48 static const char *output_file = "trace.info";
52 static void *malloc_or_die(unsigned int size)
62 static const char *find_debugfs(void)
64 const char *path = perf_debugfs_mount(NULL);
67 die("Your kernel not support debugfs filesystem");
73 * Finds the path to the debugfs/tracing
74 * Allocates the string and stores it.
76 static const char *find_tracing_dir(void)
79 static int tracing_found;
85 debugfs = find_debugfs();
87 tracing = malloc_or_die(strlen(debugfs) + 9);
89 sprintf(tracing, "%s/tracing", debugfs);
95 static char *get_tracing_file(const char *name)
100 tracing = find_tracing_dir();
104 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
106 sprintf(file, "%s/%s", tracing, name);
110 static void put_tracing_file(char *file)
115 static ssize_t write_or_die(const void *buf, size_t len)
119 ret = write(output_fd, buf, len);
121 die("writing to '%s'", output_file);
128 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
131 ptr = (unsigned int *)(void *)str;
132 return *ptr == 0x01020304;
135 /* unfortunately, you can not stat debugfs or proc files for size */
136 static void record_file(const char *file, size_t hdr_sz)
138 unsigned long long size = 0;
139 char buf[BUFSIZ], *sizep;
140 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
143 fd = open(file, O_RDONLY);
145 die("Can't read '%s'", file);
147 /* put in zeros for file size, then fill true size later */
149 write_or_die(&size, hdr_sz);
152 r = read(fd, buf, BUFSIZ);
155 write_or_die(buf, r);
160 /* ugh, handle big-endian hdr_size == 4 */
161 sizep = (char*)&size;
163 sizep += sizeof(u64) - hdr_sz;
165 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
166 die("writing to %s", output_file);
169 static void read_header_files(void)
174 path = get_tracing_file("events/header_page");
175 if (stat(path, &st) < 0)
176 die("can't read '%s'", path);
178 write_or_die("header_page", 12);
179 record_file(path, 8);
180 put_tracing_file(path);
182 path = get_tracing_file("events/header_event");
183 if (stat(path, &st) < 0)
184 die("can't read '%s'", path);
186 write_or_die("header_event", 13);
187 record_file(path, 8);
188 put_tracing_file(path);
191 static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
194 if (!strcmp(sys, tps->name))
202 static void copy_event_system(const char *sys, struct tracepoint_path *tps)
213 die("can't read directory '%s'", sys);
215 while ((dent = readdir(dir))) {
216 if (dent->d_type != DT_DIR ||
217 strcmp(dent->d_name, ".") == 0 ||
218 strcmp(dent->d_name, "..") == 0 ||
219 !name_in_tp_list(dent->d_name, tps))
221 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
222 sprintf(format, "%s/%s/format", sys, dent->d_name);
223 ret = stat(format, &st);
230 write_or_die(&count, 4);
233 while ((dent = readdir(dir))) {
234 if (dent->d_type != DT_DIR ||
235 strcmp(dent->d_name, ".") == 0 ||
236 strcmp(dent->d_name, "..") == 0 ||
237 !name_in_tp_list(dent->d_name, tps))
239 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
240 sprintf(format, "%s/%s/format", sys, dent->d_name);
241 ret = stat(format, &st);
244 record_file(format, 8);
251 static void read_ftrace_files(struct tracepoint_path *tps)
255 path = get_tracing_file("events/ftrace");
257 copy_event_system(path, tps);
259 put_tracing_file(path);
262 static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
265 if (!strcmp(sys, tps->system))
273 static void read_event_files(struct tracepoint_path *tps)
283 path = get_tracing_file("events");
287 die("can't read directory '%s'", path);
289 while ((dent = readdir(dir))) {
290 if (dent->d_type != DT_DIR ||
291 strcmp(dent->d_name, ".") == 0 ||
292 strcmp(dent->d_name, "..") == 0 ||
293 strcmp(dent->d_name, "ftrace") == 0 ||
294 !system_in_tp_list(dent->d_name, tps))
299 write_or_die(&count, 4);
302 while ((dent = readdir(dir))) {
303 if (dent->d_type != DT_DIR ||
304 strcmp(dent->d_name, ".") == 0 ||
305 strcmp(dent->d_name, "..") == 0 ||
306 strcmp(dent->d_name, "ftrace") == 0 ||
307 !system_in_tp_list(dent->d_name, tps))
309 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
310 sprintf(sys, "%s/%s", path, dent->d_name);
311 ret = stat(sys, &st);
313 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
314 copy_event_system(sys, tps);
320 put_tracing_file(path);
323 static void read_proc_kallsyms(void)
326 const char *path = "/proc/kallsyms";
330 ret = stat(path, &st);
334 write_or_die(&size, 4);
337 record_file(path, 4);
340 static void read_ftrace_printk(void)
347 path = get_tracing_file("printk_formats");
348 ret = stat(path, &st);
352 write_or_die(&size, 4);
355 record_file(path, 4);
358 put_tracing_file(path);
361 static struct tracepoint_path *
362 get_tracepoints_path(struct list_head *pattrs)
364 struct tracepoint_path path, *ppath = &path;
365 struct perf_evsel *pos;
366 int nr_tracepoints = 0;
368 list_for_each_entry(pos, pattrs, node) {
369 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
372 ppath->next = tracepoint_id_to_path(pos->attr.config);
374 die("%s\n", "No memory to alloc tracepoints list");
378 return nr_tracepoints > 0 ? path.next : NULL;
382 put_tracepoints_path(struct tracepoint_path *tps)
385 struct tracepoint_path *t = tps;
394 bool have_tracepoints(struct list_head *pattrs)
396 struct perf_evsel *pos;
398 list_for_each_entry(pos, pattrs, node)
399 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
405 static void tracing_data_header(void)
409 /* just guessing this is someone's birthday.. ;) */
413 memcpy(buf + 3, "tracing", 7);
415 write_or_die(buf, 10);
417 write_or_die(VERSION, strlen(VERSION) + 1);
425 read_trace_init(buf[0], buf[0]);
427 write_or_die(buf, 1);
429 /* save size of long */
430 buf[0] = sizeof(long);
431 write_or_die(buf, 1);
434 page_size = sysconf(_SC_PAGESIZE);
435 write_or_die(&page_size, 4);
438 struct tracing_data *tracing_data_get(struct list_head *pattrs,
441 struct tracepoint_path *tps;
442 struct tracing_data *tdata;
446 tps = get_tracepoints_path(pattrs);
450 tdata = malloc_or_die(sizeof(*tdata));
457 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
459 if (!mkstemp(tdata->temp_file))
460 die("Can't make temp file");
462 temp_fd = open(tdata->temp_file, O_RDWR);
464 die("Can't read '%s'", tdata->temp_file);
467 * Set the temp file the default output, so all the
468 * tracing data are stored into it.
473 tracing_data_header();
475 read_ftrace_files(tps);
476 read_event_files(tps);
477 read_proc_kallsyms();
478 read_ftrace_printk();
481 * All tracing data are stored by now, we can restore
482 * the default output file in case we used temp file.
485 tdata->size = lseek(output_fd, 0, SEEK_CUR);
490 put_tracepoints_path(tps);
494 void tracing_data_put(struct tracing_data *tdata)
497 record_file(tdata->temp_file, 0);
498 unlink(tdata->temp_file);
504 int read_tracing_data(int fd, struct list_head *pattrs)
506 struct tracing_data *tdata;
509 * We work over the real file, so we can write data
510 * directly, no temp file is needed.
512 tdata = tracing_data_get(pattrs, fd, false);
516 tracing_data_put(tdata);