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 #define TRACE_CTRL "tracing_on"
48 #define AVAILABLE "available_tracers"
49 #define CURRENT "current_tracer"
50 #define ITER_CTRL "trace_options"
51 #define MAX_LATENCY "tracing_max_latency"
53 unsigned int page_size;
55 static const char *output_file = "trace.info";
59 static void *malloc_or_die(unsigned int size)
69 static const char *find_debugfs(void)
71 const char *path = perf_debugfs_mount(NULL);
74 die("Your kernel not support debugfs filesystem");
80 * Finds the path to the debugfs/tracing
81 * Allocates the string and stores it.
83 static const char *find_tracing_dir(void)
86 static int tracing_found;
92 debugfs = find_debugfs();
94 tracing = malloc_or_die(strlen(debugfs) + 9);
96 sprintf(tracing, "%s/tracing", debugfs);
102 static char *get_tracing_file(const char *name)
107 tracing = find_tracing_dir();
111 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
113 sprintf(file, "%s/%s", tracing, name);
117 static void put_tracing_file(char *file)
122 static ssize_t write_or_die(const void *buf, size_t len)
126 ret = write(output_fd, buf, len);
128 die("writing to '%s'", output_file);
135 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
138 ptr = (unsigned int *)(void *)str;
139 return *ptr == 0x01020304;
142 /* unfortunately, you can not stat debugfs or proc files for size */
143 static void record_file(const char *file, size_t hdr_sz)
145 unsigned long long size = 0;
146 char buf[BUFSIZ], *sizep;
147 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
150 fd = open(file, O_RDONLY);
152 die("Can't read '%s'", file);
154 /* put in zeros for file size, then fill true size later */
156 write_or_die(&size, hdr_sz);
159 r = read(fd, buf, BUFSIZ);
162 write_or_die(buf, r);
167 /* ugh, handle big-endian hdr_size == 4 */
168 sizep = (char*)&size;
170 sizep += sizeof(u64) - hdr_sz;
172 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
173 die("writing to %s", output_file);
176 static void read_header_files(void)
181 path = get_tracing_file("events/header_page");
182 if (stat(path, &st) < 0)
183 die("can't read '%s'", path);
185 write_or_die("header_page", 12);
186 record_file(path, 8);
187 put_tracing_file(path);
189 path = get_tracing_file("events/header_event");
190 if (stat(path, &st) < 0)
191 die("can't read '%s'", path);
193 write_or_die("header_event", 13);
194 record_file(path, 8);
195 put_tracing_file(path);
198 static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
201 if (!strcmp(sys, tps->name))
209 static void copy_event_system(const char *sys, struct tracepoint_path *tps)
220 die("can't read directory '%s'", sys);
222 while ((dent = readdir(dir))) {
223 if (dent->d_type != DT_DIR ||
224 strcmp(dent->d_name, ".") == 0 ||
225 strcmp(dent->d_name, "..") == 0 ||
226 !name_in_tp_list(dent->d_name, tps))
228 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
229 sprintf(format, "%s/%s/format", sys, dent->d_name);
230 ret = stat(format, &st);
237 write_or_die(&count, 4);
240 while ((dent = readdir(dir))) {
241 if (dent->d_type != DT_DIR ||
242 strcmp(dent->d_name, ".") == 0 ||
243 strcmp(dent->d_name, "..") == 0 ||
244 !name_in_tp_list(dent->d_name, tps))
246 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
247 sprintf(format, "%s/%s/format", sys, dent->d_name);
248 ret = stat(format, &st);
251 record_file(format, 8);
258 static void read_ftrace_files(struct tracepoint_path *tps)
262 path = get_tracing_file("events/ftrace");
264 copy_event_system(path, tps);
266 put_tracing_file(path);
269 static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
272 if (!strcmp(sys, tps->system))
280 static void read_event_files(struct tracepoint_path *tps)
290 path = get_tracing_file("events");
294 die("can't read directory '%s'", path);
296 while ((dent = readdir(dir))) {
297 if (dent->d_type != DT_DIR ||
298 strcmp(dent->d_name, ".") == 0 ||
299 strcmp(dent->d_name, "..") == 0 ||
300 strcmp(dent->d_name, "ftrace") == 0 ||
301 !system_in_tp_list(dent->d_name, tps))
306 write_or_die(&count, 4);
309 while ((dent = readdir(dir))) {
310 if (dent->d_type != DT_DIR ||
311 strcmp(dent->d_name, ".") == 0 ||
312 strcmp(dent->d_name, "..") == 0 ||
313 strcmp(dent->d_name, "ftrace") == 0 ||
314 !system_in_tp_list(dent->d_name, tps))
316 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
317 sprintf(sys, "%s/%s", path, dent->d_name);
318 ret = stat(sys, &st);
320 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
321 copy_event_system(sys, tps);
327 put_tracing_file(path);
330 static void read_proc_kallsyms(void)
333 const char *path = "/proc/kallsyms";
337 ret = stat(path, &st);
341 write_or_die(&size, 4);
344 record_file(path, 4);
347 static void read_ftrace_printk(void)
354 path = get_tracing_file("printk_formats");
355 ret = stat(path, &st);
359 write_or_die(&size, 4);
362 record_file(path, 4);
365 put_tracing_file(path);
368 static struct tracepoint_path *
369 get_tracepoints_path(struct list_head *pattrs)
371 struct tracepoint_path path, *ppath = &path;
372 struct perf_evsel *pos;
373 int nr_tracepoints = 0;
375 list_for_each_entry(pos, pattrs, node) {
376 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
379 ppath->next = tracepoint_id_to_path(pos->attr.config);
381 die("%s\n", "No memory to alloc tracepoints list");
385 return nr_tracepoints > 0 ? path.next : NULL;
389 put_tracepoints_path(struct tracepoint_path *tps)
392 struct tracepoint_path *t = tps;
401 bool have_tracepoints(struct list_head *pattrs)
403 struct perf_evsel *pos;
405 list_for_each_entry(pos, pattrs, node)
406 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
412 static void tracing_data_header(void)
416 /* just guessing this is someone's birthday.. ;) */
420 memcpy(buf + 3, "tracing", 7);
422 write_or_die(buf, 10);
424 write_or_die(VERSION, strlen(VERSION) + 1);
432 read_trace_init(buf[0], buf[0]);
434 write_or_die(buf, 1);
436 /* save size of long */
437 buf[0] = sizeof(long);
438 write_or_die(buf, 1);
441 page_size = sysconf(_SC_PAGESIZE);
442 write_or_die(&page_size, 4);
445 struct tracing_data *tracing_data_get(struct list_head *pattrs,
448 struct tracepoint_path *tps;
449 struct tracing_data *tdata;
453 tps = get_tracepoints_path(pattrs);
457 tdata = malloc_or_die(sizeof(*tdata));
464 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
466 if (!mkstemp(tdata->temp_file))
467 die("Can't make temp file");
469 temp_fd = open(tdata->temp_file, O_RDWR);
471 die("Can't read '%s'", tdata->temp_file);
474 * Set the temp file the default output, so all the
475 * tracing data are stored into it.
480 tracing_data_header();
482 read_ftrace_files(tps);
483 read_event_files(tps);
484 read_proc_kallsyms();
485 read_ftrace_printk();
488 * All tracing data are stored by now, we can restore
489 * the default output file in case we used temp file.
492 tdata->size = lseek(output_fd, 0, SEEK_CUR);
497 put_tracepoints_path(tps);
501 void tracing_data_put(struct tracing_data *tdata)
504 record_file(tdata->temp_file, 0);
505 unlink(tdata->temp_file);
511 int read_tracing_data(int fd, struct list_head *pattrs)
513 struct tracing_data *tdata;
516 * We work over the real file, so we can write data
517 * directly, no temp file is needed.
519 tdata = tracing_data_get(pattrs, fd, false);
523 tracing_data_put(tdata);