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 die("Your kernel not support 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();
85 tracing = malloc_or_die(strlen(debugfs) + 9);
87 sprintf(tracing, "%s/tracing", debugfs);
93 static char *get_tracing_file(const char *name)
98 tracing = find_tracing_dir();
102 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
104 sprintf(file, "%s/%s", tracing, name);
108 static void put_tracing_file(char *file)
113 static ssize_t write_or_die(const void *buf, size_t len)
117 ret = write(output_fd, buf, len);
119 die("writing to '%s'", output_file);
126 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
129 ptr = (unsigned int *)(void *)str;
130 return *ptr == 0x01020304;
133 /* unfortunately, you can not stat debugfs or proc files for size */
134 static void record_file(const char *file, size_t hdr_sz)
136 unsigned long long size = 0;
137 char buf[BUFSIZ], *sizep;
138 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
141 fd = open(file, O_RDONLY);
143 die("Can't read '%s'", file);
145 /* put in zeros for file size, then fill true size later */
147 write_or_die(&size, hdr_sz);
150 r = read(fd, buf, BUFSIZ);
153 write_or_die(buf, r);
158 /* ugh, handle big-endian hdr_size == 4 */
159 sizep = (char*)&size;
161 sizep += sizeof(u64) - hdr_sz;
163 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
164 die("writing to %s", output_file);
167 static void read_header_files(void)
172 path = get_tracing_file("events/header_page");
173 if (stat(path, &st) < 0)
174 die("can't read '%s'", path);
176 write_or_die("header_page", 12);
177 record_file(path, 8);
178 put_tracing_file(path);
180 path = get_tracing_file("events/header_event");
181 if (stat(path, &st) < 0)
182 die("can't read '%s'", path);
184 write_or_die("header_event", 13);
185 record_file(path, 8);
186 put_tracing_file(path);
189 static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
192 if (!strcmp(sys, tps->name))
200 static void copy_event_system(const char *sys, struct tracepoint_path *tps)
211 die("can't read directory '%s'", sys);
213 while ((dent = readdir(dir))) {
214 if (dent->d_type != DT_DIR ||
215 strcmp(dent->d_name, ".") == 0 ||
216 strcmp(dent->d_name, "..") == 0 ||
217 !name_in_tp_list(dent->d_name, tps))
219 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
220 sprintf(format, "%s/%s/format", sys, dent->d_name);
221 ret = stat(format, &st);
228 write_or_die(&count, 4);
231 while ((dent = readdir(dir))) {
232 if (dent->d_type != DT_DIR ||
233 strcmp(dent->d_name, ".") == 0 ||
234 strcmp(dent->d_name, "..") == 0 ||
235 !name_in_tp_list(dent->d_name, tps))
237 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
238 sprintf(format, "%s/%s/format", sys, dent->d_name);
239 ret = stat(format, &st);
242 record_file(format, 8);
249 static void read_ftrace_files(struct tracepoint_path *tps)
253 path = get_tracing_file("events/ftrace");
255 copy_event_system(path, tps);
257 put_tracing_file(path);
260 static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
263 if (!strcmp(sys, tps->system))
271 static void read_event_files(struct tracepoint_path *tps)
281 path = get_tracing_file("events");
285 die("can't read directory '%s'", path);
287 while ((dent = readdir(dir))) {
288 if (dent->d_type != DT_DIR ||
289 strcmp(dent->d_name, ".") == 0 ||
290 strcmp(dent->d_name, "..") == 0 ||
291 strcmp(dent->d_name, "ftrace") == 0 ||
292 !system_in_tp_list(dent->d_name, tps))
297 write_or_die(&count, 4);
300 while ((dent = readdir(dir))) {
301 if (dent->d_type != DT_DIR ||
302 strcmp(dent->d_name, ".") == 0 ||
303 strcmp(dent->d_name, "..") == 0 ||
304 strcmp(dent->d_name, "ftrace") == 0 ||
305 !system_in_tp_list(dent->d_name, tps))
307 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
308 sprintf(sys, "%s/%s", path, dent->d_name);
309 ret = stat(sys, &st);
311 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
312 copy_event_system(sys, tps);
318 put_tracing_file(path);
321 static void read_proc_kallsyms(void)
324 const char *path = "/proc/kallsyms";
328 ret = stat(path, &st);
332 write_or_die(&size, 4);
335 record_file(path, 4);
338 static void read_ftrace_printk(void)
345 path = get_tracing_file("printk_formats");
346 ret = stat(path, &st);
350 write_or_die(&size, 4);
353 record_file(path, 4);
356 put_tracing_file(path);
359 static struct tracepoint_path *
360 get_tracepoints_path(struct list_head *pattrs)
362 struct tracepoint_path path, *ppath = &path;
363 struct perf_evsel *pos;
364 int nr_tracepoints = 0;
366 list_for_each_entry(pos, pattrs, node) {
367 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
370 ppath->next = tracepoint_id_to_path(pos->attr.config);
372 die("%s\n", "No memory to alloc tracepoints list");
376 return nr_tracepoints > 0 ? path.next : NULL;
380 put_tracepoints_path(struct tracepoint_path *tps)
383 struct tracepoint_path *t = tps;
392 bool have_tracepoints(struct list_head *pattrs)
394 struct perf_evsel *pos;
396 list_for_each_entry(pos, pattrs, node)
397 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
403 static void tracing_data_header(void)
407 /* just guessing this is someone's birthday.. ;) */
411 memcpy(buf + 3, "tracing", 7);
413 write_or_die(buf, 10);
415 write_or_die(VERSION, strlen(VERSION) + 1);
423 read_trace_init(buf[0], buf[0]);
425 write_or_die(buf, 1);
427 /* save size of long */
428 buf[0] = sizeof(long);
429 write_or_die(buf, 1);
432 write_or_die(&page_size, 4);
435 struct tracing_data *tracing_data_get(struct list_head *pattrs,
438 struct tracepoint_path *tps;
439 struct tracing_data *tdata;
443 tps = get_tracepoints_path(pattrs);
447 tdata = malloc_or_die(sizeof(*tdata));
454 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
456 if (!mkstemp(tdata->temp_file))
457 die("Can't make temp file");
459 temp_fd = open(tdata->temp_file, O_RDWR);
461 die("Can't read '%s'", tdata->temp_file);
464 * Set the temp file the default output, so all the
465 * tracing data are stored into it.
470 tracing_data_header();
472 read_ftrace_files(tps);
473 read_event_files(tps);
474 read_proc_kallsyms();
475 read_ftrace_printk();
478 * All tracing data are stored by now, we can restore
479 * the default output file in case we used temp file.
482 tdata->size = lseek(output_fd, 0, SEEK_CUR);
487 put_tracepoints_path(tps);
491 void tracing_data_put(struct tracing_data *tdata)
494 record_file(tdata->temp_file, 0);
495 unlink(tdata->temp_file);
501 int read_tracing_data(int fd, struct list_head *pattrs)
503 struct tracing_data *tdata;
506 * We work over the real file, so we can write data
507 * directly, no temp file is needed.
509 tdata = tracing_data_get(pattrs, fd, false);
513 tracing_data_put(tdata);