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 static const char *output_file = "trace.info";
50 static void *malloc_or_die(unsigned int size)
60 static const char *find_debugfs(void)
62 const char *path = perf_debugfs_mount(NULL);
65 pr_debug("Your kernel does not support the debugfs filesystem");
71 * Finds the path to the debugfs/tracing
72 * Allocates the string and stores it.
74 static const char *find_tracing_dir(void)
77 static int tracing_found;
83 debugfs = find_debugfs();
87 tracing = malloc(strlen(debugfs) + 9);
91 sprintf(tracing, "%s/tracing", debugfs);
97 static char *get_tracing_file(const char *name)
102 tracing = find_tracing_dir();
106 file = malloc(strlen(tracing) + strlen(name) + 2);
110 sprintf(file, "%s/%s", tracing, name);
114 static void put_tracing_file(char *file)
119 static ssize_t write_or_die(const void *buf, size_t len)
123 ret = write(output_fd, buf, len);
125 die("writing to '%s'", output_file);
132 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
135 ptr = (unsigned int *)(void *)str;
136 return *ptr == 0x01020304;
139 /* unfortunately, you can not stat debugfs or proc files for size */
140 static void record_file(const char *file, size_t hdr_sz)
142 unsigned long long size = 0;
143 char buf[BUFSIZ], *sizep;
144 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
147 fd = open(file, O_RDONLY);
149 die("Can't read '%s'", file);
151 /* put in zeros for file size, then fill true size later */
153 write_or_die(&size, hdr_sz);
156 r = read(fd, buf, BUFSIZ);
159 write_or_die(buf, r);
164 /* ugh, handle big-endian hdr_size == 4 */
165 sizep = (char*)&size;
167 sizep += sizeof(u64) - hdr_sz;
169 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
170 die("writing to %s", output_file);
173 static void read_header_files(void)
178 path = get_tracing_file("events/header_page");
180 die("can't get tracing/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");
191 die("can't get tracing/events/header_event");
193 if (stat(path, &st) < 0)
194 die("can't read '%s'", path);
196 write_or_die("header_event", 13);
197 record_file(path, 8);
198 put_tracing_file(path);
201 static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
204 if (!strcmp(sys, tps->name))
212 static void copy_event_system(const char *sys, struct tracepoint_path *tps)
223 die("can't read directory '%s'", sys);
225 while ((dent = readdir(dir))) {
226 if (dent->d_type != DT_DIR ||
227 strcmp(dent->d_name, ".") == 0 ||
228 strcmp(dent->d_name, "..") == 0 ||
229 !name_in_tp_list(dent->d_name, tps))
231 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
232 sprintf(format, "%s/%s/format", sys, dent->d_name);
233 ret = stat(format, &st);
240 write_or_die(&count, 4);
243 while ((dent = readdir(dir))) {
244 if (dent->d_type != DT_DIR ||
245 strcmp(dent->d_name, ".") == 0 ||
246 strcmp(dent->d_name, "..") == 0 ||
247 !name_in_tp_list(dent->d_name, tps))
249 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
250 sprintf(format, "%s/%s/format", sys, dent->d_name);
251 ret = stat(format, &st);
254 record_file(format, 8);
261 static void read_ftrace_files(struct tracepoint_path *tps)
265 path = get_tracing_file("events/ftrace");
267 die("can't get tracing/events/ftrace");
269 copy_event_system(path, tps);
271 put_tracing_file(path);
274 static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
277 if (!strcmp(sys, tps->system))
285 static void read_event_files(struct tracepoint_path *tps)
295 path = get_tracing_file("events");
297 die("can't get tracing/events");
301 die("can't read directory '%s'", path);
303 while ((dent = readdir(dir))) {
304 if (dent->d_type != DT_DIR ||
305 strcmp(dent->d_name, ".") == 0 ||
306 strcmp(dent->d_name, "..") == 0 ||
307 strcmp(dent->d_name, "ftrace") == 0 ||
308 !system_in_tp_list(dent->d_name, tps))
313 write_or_die(&count, 4);
316 while ((dent = readdir(dir))) {
317 if (dent->d_type != DT_DIR ||
318 strcmp(dent->d_name, ".") == 0 ||
319 strcmp(dent->d_name, "..") == 0 ||
320 strcmp(dent->d_name, "ftrace") == 0 ||
321 !system_in_tp_list(dent->d_name, tps))
323 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
324 sprintf(sys, "%s/%s", path, dent->d_name);
325 ret = stat(sys, &st);
327 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
328 copy_event_system(sys, tps);
334 put_tracing_file(path);
337 static void read_proc_kallsyms(void)
340 const char *path = "/proc/kallsyms";
344 ret = stat(path, &st);
348 write_or_die(&size, 4);
351 record_file(path, 4);
354 static void read_ftrace_printk(void)
361 path = get_tracing_file("printk_formats");
363 die("can't get tracing/printk_formats");
365 ret = stat(path, &st);
369 write_or_die(&size, 4);
372 record_file(path, 4);
375 put_tracing_file(path);
378 static struct tracepoint_path *
379 get_tracepoints_path(struct list_head *pattrs)
381 struct tracepoint_path path, *ppath = &path;
382 struct perf_evsel *pos;
383 int nr_tracepoints = 0;
385 list_for_each_entry(pos, pattrs, node) {
386 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
389 ppath->next = tracepoint_id_to_path(pos->attr.config);
391 die("%s\n", "No memory to alloc tracepoints list");
395 return nr_tracepoints > 0 ? path.next : NULL;
399 put_tracepoints_path(struct tracepoint_path *tps)
402 struct tracepoint_path *t = tps;
411 bool have_tracepoints(struct list_head *pattrs)
413 struct perf_evsel *pos;
415 list_for_each_entry(pos, pattrs, node)
416 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
422 static void tracing_data_header(void)
426 /* just guessing this is someone's birthday.. ;) */
430 memcpy(buf + 3, "tracing", 7);
432 write_or_die(buf, 10);
434 write_or_die(VERSION, strlen(VERSION) + 1);
442 read_trace_init(buf[0], buf[0]);
444 write_or_die(buf, 1);
446 /* save size of long */
447 buf[0] = sizeof(long);
448 write_or_die(buf, 1);
451 write_or_die(&page_size, 4);
454 struct tracing_data *tracing_data_get(struct list_head *pattrs,
457 struct tracepoint_path *tps;
458 struct tracing_data *tdata;
462 tps = get_tracepoints_path(pattrs);
466 tdata = malloc_or_die(sizeof(*tdata));
473 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
475 if (!mkstemp(tdata->temp_file))
476 die("Can't make temp file");
478 temp_fd = open(tdata->temp_file, O_RDWR);
480 die("Can't read '%s'", tdata->temp_file);
483 * Set the temp file the default output, so all the
484 * tracing data are stored into it.
489 tracing_data_header();
491 read_ftrace_files(tps);
492 read_event_files(tps);
493 read_proc_kallsyms();
494 read_ftrace_printk();
497 * All tracing data are stored by now, we can restore
498 * the default output file in case we used temp file.
501 tdata->size = lseek(output_fd, 0, SEEK_CUR);
506 put_tracepoints_path(tps);
510 void tracing_data_put(struct tracing_data *tdata)
513 record_file(tdata->temp_file, 0);
514 unlink(tdata->temp_file);
520 int read_tracing_data(int fd, struct list_head *pattrs)
522 struct tracing_data *tdata;
525 * We work over the real file, so we can write data
526 * directly, no temp file is needed.
528 tdata = tracing_data_get(pattrs, fd, false);
532 tracing_data_put(tdata);