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>
37 #include <linux/list.h>
38 #include <linux/kernel.h>
41 #include "trace-event.h"
48 #define STR(x) _STR(x)
51 #define TRACE_CTRL "tracing_on"
53 #define AVAILABLE "available_tracers"
54 #define CURRENT "current_tracer"
55 #define ITER_CTRL "trace_options"
56 #define MAX_LATENCY "tracing_max_latency"
58 unsigned int page_size;
60 static const char *output_file = "trace.info";
64 struct event_list *next;
69 struct events *sibling;
70 struct events *children;
77 static void die(const char *fmt, ...)
89 vfprintf(stderr, fmt, ap);
92 fprintf(stderr, "\n");
96 void *malloc_or_die(unsigned int size)
106 static const char *find_debugfs(void)
108 const char *path = debugfs_mount(NULL);
111 die("Your kernel not support debugfs filesystem");
117 * Finds the path to the debugfs/tracing
118 * Allocates the string and stores it.
120 static const char *find_tracing_dir(void)
122 static char *tracing;
123 static int tracing_found;
129 debugfs = find_debugfs();
131 tracing = malloc_or_die(strlen(debugfs) + 9);
133 sprintf(tracing, "%s/tracing", debugfs);
139 static char *get_tracing_file(const char *name)
144 tracing = find_tracing_dir();
148 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
150 sprintf(file, "%s/%s", tracing, name);
154 static void put_tracing_file(char *file)
159 static ssize_t calc_data_size;
161 static ssize_t write_or_die(const void *buf, size_t len)
165 if (calc_data_size) {
166 calc_data_size += len;
170 ret = write(output_fd, buf, len);
172 die("writing to '%s'", output_file);
179 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
182 ptr = (unsigned int *)(void *)str;
183 return *ptr == 0x01020304;
186 /* unfortunately, you can not stat debugfs or proc files for size */
187 static void record_file(const char *file, size_t hdr_sz)
189 unsigned long long size = 0;
190 char buf[BUFSIZ], *sizep;
191 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
194 fd = open(file, O_RDONLY);
196 die("Can't read '%s'", file);
198 /* put in zeros for file size, then fill true size later */
200 write_or_die(&size, hdr_sz);
203 r = read(fd, buf, BUFSIZ);
206 write_or_die(buf, r);
211 /* ugh, handle big-endian hdr_size == 4 */
212 sizep = (char*)&size;
214 sizep += sizeof(u64) - hdr_sz;
216 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
217 die("writing to %s", output_file);
220 static void read_header_files(void)
225 path = get_tracing_file("events/header_page");
226 if (stat(path, &st) < 0)
227 die("can't read '%s'", path);
229 write_or_die("header_page", 12);
230 record_file(path, 8);
231 put_tracing_file(path);
233 path = get_tracing_file("events/header_event");
234 if (stat(path, &st) < 0)
235 die("can't read '%s'", path);
237 write_or_die("header_event", 13);
238 record_file(path, 8);
239 put_tracing_file(path);
242 static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
245 if (!strcmp(sys, tps->name))
253 static void copy_event_system(const char *sys, struct tracepoint_path *tps)
264 die("can't read directory '%s'", sys);
266 while ((dent = readdir(dir))) {
267 if (dent->d_type != DT_DIR ||
268 strcmp(dent->d_name, ".") == 0 ||
269 strcmp(dent->d_name, "..") == 0 ||
270 !name_in_tp_list(dent->d_name, tps))
272 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
273 sprintf(format, "%s/%s/format", sys, dent->d_name);
274 ret = stat(format, &st);
281 write_or_die(&count, 4);
284 while ((dent = readdir(dir))) {
285 if (dent->d_type != DT_DIR ||
286 strcmp(dent->d_name, ".") == 0 ||
287 strcmp(dent->d_name, "..") == 0 ||
288 !name_in_tp_list(dent->d_name, tps))
290 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
291 sprintf(format, "%s/%s/format", sys, dent->d_name);
292 ret = stat(format, &st);
295 record_file(format, 8);
302 static void read_ftrace_files(struct tracepoint_path *tps)
306 path = get_tracing_file("events/ftrace");
308 copy_event_system(path, tps);
310 put_tracing_file(path);
313 static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
316 if (!strcmp(sys, tps->system))
324 static void read_event_files(struct tracepoint_path *tps)
334 path = get_tracing_file("events");
338 die("can't read directory '%s'", path);
340 while ((dent = readdir(dir))) {
341 if (dent->d_type != DT_DIR ||
342 strcmp(dent->d_name, ".") == 0 ||
343 strcmp(dent->d_name, "..") == 0 ||
344 strcmp(dent->d_name, "ftrace") == 0 ||
345 !system_in_tp_list(dent->d_name, tps))
350 write_or_die(&count, 4);
353 while ((dent = readdir(dir))) {
354 if (dent->d_type != DT_DIR ||
355 strcmp(dent->d_name, ".") == 0 ||
356 strcmp(dent->d_name, "..") == 0 ||
357 strcmp(dent->d_name, "ftrace") == 0 ||
358 !system_in_tp_list(dent->d_name, tps))
360 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
361 sprintf(sys, "%s/%s", path, dent->d_name);
362 ret = stat(sys, &st);
364 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
365 copy_event_system(sys, tps);
371 put_tracing_file(path);
374 static void read_proc_kallsyms(void)
377 const char *path = "/proc/kallsyms";
381 ret = stat(path, &st);
385 write_or_die(&size, 4);
388 record_file(path, 4);
391 static void read_ftrace_printk(void)
398 path = get_tracing_file("printk_formats");
399 ret = stat(path, &st);
403 write_or_die(&size, 4);
406 record_file(path, 4);
409 put_tracing_file(path);
412 static struct tracepoint_path *
413 get_tracepoints_path(struct list_head *pattrs)
415 struct tracepoint_path path, *ppath = &path;
416 struct perf_evsel *pos;
417 int nr_tracepoints = 0;
419 list_for_each_entry(pos, pattrs, node) {
420 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
423 ppath->next = tracepoint_id_to_path(pos->attr.config);
425 die("%s\n", "No memory to alloc tracepoints list");
429 return nr_tracepoints > 0 ? path.next : NULL;
433 put_tracepoints_path(struct tracepoint_path *tps)
436 struct tracepoint_path *t = tps;
445 bool have_tracepoints(struct list_head *pattrs)
447 struct perf_evsel *pos;
449 list_for_each_entry(pos, pattrs, node)
450 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
456 static void tracing_data_header(void)
460 /* just guessing this is someone's birthday.. ;) */
464 memcpy(buf + 3, "tracing", 7);
466 write_or_die(buf, 10);
468 write_or_die(VERSION, strlen(VERSION) + 1);
476 write_or_die(buf, 1);
478 /* save size of long */
479 buf[0] = sizeof(long);
480 write_or_die(buf, 1);
483 page_size = sysconf(_SC_PAGESIZE);
484 write_or_die(&page_size, 4);
487 struct tracing_data *tracing_data_get(struct list_head *pattrs,
490 struct tracepoint_path *tps;
491 struct tracing_data *tdata;
495 tps = get_tracepoints_path(pattrs);
499 tdata = malloc_or_die(sizeof(*tdata));
506 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
508 if (!mkstemp(tdata->temp_file))
509 die("Can't make temp file");
511 temp_fd = open(tdata->temp_file, O_RDWR);
513 die("Can't read '%s'", tdata->temp_file);
516 * Set the temp file the default output, so all the
517 * tracing data are stored into it.
522 tracing_data_header();
524 read_ftrace_files(tps);
525 read_event_files(tps);
526 read_proc_kallsyms();
527 read_ftrace_printk();
530 * All tracing data are stored by now, we can restore
531 * the default output file in case we used temp file.
534 tdata->size = lseek(output_fd, 0, SEEK_CUR);
539 put_tracepoints_path(tps);
543 void tracing_data_put(struct tracing_data *tdata)
546 record_file(tdata->temp_file, 0);
547 unlink(tdata->temp_file);
553 int read_tracing_data(int fd, struct list_head *pattrs)
555 struct tracing_data *tdata;
558 * We work over the real file, so we can write data
559 * directly, no temp file is needed.
561 tdata = tracing_data_get(pattrs, fd, false);
565 tracing_data_put(tdata);