28 #include <sys/types.h>
36 #include <linux/list.h>
37 #include <linux/kernel.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"
55 static const char *output_file =
"trace.info";
81 static const char *find_debugfs(
void)
86 die(
"Your kernel not support debugfs filesystem");
95 static const char *find_tracing_dir(
void)
98 static int tracing_found;
104 debugfs = find_debugfs();
108 sprintf(tracing,
"%s/tracing", debugfs);
114 static char *get_tracing_file(
const char *
name)
119 tracing = find_tracing_dir();
125 sprintf(file,
"%s/%s", tracing, name);
129 static void put_tracing_file(
char *file)
136 static ssize_t write_or_die(
const void *
buf,
size_t len)
140 if (calc_data_size) {
141 calc_data_size += len;
145 ret =
write(output_fd, buf, len);
147 die(
"writing to '%s'", output_file);
154 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
157 ptr = (
unsigned int *)(
void *)
str;
158 return *ptr == 0x01020304;
162 static void record_file(
const char *file,
size_t hdr_sz)
164 unsigned long long size = 0;
165 char buf[BUFSIZ], *sizep;
171 die(
"Can't read '%s'", file);
175 write_or_die(&size, hdr_sz);
178 r =
read(fd, buf, BUFSIZ);
181 write_or_die(buf, r);
187 sizep = (
char*)&size;
189 sizep +=
sizeof(
u64) - hdr_sz;
191 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
192 die(
"writing to %s", output_file);
195 static void read_header_files(
void)
200 path = get_tracing_file(
"events/header_page");
202 die(
"can't read '%s'", path);
204 write_or_die(
"header_page", 12);
205 record_file(path, 8);
206 put_tracing_file(path);
208 path = get_tracing_file(
"events/header_event");
210 die(
"can't read '%s'", path);
212 write_or_die(
"header_event", 13);
213 record_file(path, 8);
214 put_tracing_file(path);
228 static void copy_event_system(
const char *sys,
struct tracepoint_path *tps)
239 die(
"can't read directory '%s'", sys);
241 while ((dent = readdir(dir))) {
242 if (dent->d_type !=
DT_DIR ||
243 strcmp(dent->d_name,
".") == 0 ||
244 strcmp(dent->d_name,
"..") == 0 ||
245 !name_in_tp_list(dent->d_name, tps))
248 sprintf(format,
"%s/%s/format", sys, dent->d_name);
256 write_or_die(&count, 4);
259 while ((dent = readdir(dir))) {
260 if (dent->d_type !=
DT_DIR ||
261 strcmp(dent->d_name,
".") == 0 ||
262 strcmp(dent->d_name,
"..") == 0 ||
263 !name_in_tp_list(dent->d_name, tps))
266 sprintf(format,
"%s/%s/format", sys, dent->d_name);
270 record_file(format, 8);
281 path = get_tracing_file(
"events/ftrace");
283 copy_event_system(path, tps);
285 put_tracing_file(path);
309 path = get_tracing_file(
"events");
313 die(
"can't read directory '%s'", path);
315 while ((dent = readdir(dir))) {
316 if (dent->d_type !=
DT_DIR ||
317 strcmp(dent->d_name,
".") == 0 ||
318 strcmp(dent->d_name,
"..") == 0 ||
319 strcmp(dent->d_name,
"ftrace") == 0 ||
320 !system_in_tp_list(dent->d_name, tps))
325 write_or_die(&count, 4);
328 while ((dent = readdir(dir))) {
329 if (dent->d_type !=
DT_DIR ||
330 strcmp(dent->d_name,
".") == 0 ||
331 strcmp(dent->d_name,
"..") == 0 ||
332 strcmp(dent->d_name,
"ftrace") == 0 ||
333 !system_in_tp_list(dent->d_name, tps))
336 sprintf(sys,
"%s/%s", path, dent->d_name);
339 write_or_die(dent->d_name,
strlen(dent->d_name) + 1);
340 copy_event_system(sys, tps);
346 put_tracing_file(path);
349 static void read_proc_kallsyms(
void)
352 const char *path =
"/proc/kallsyms";
360 write_or_die(&size, 4);
363 record_file(path, 4);
366 static void read_ftrace_printk(
void)
373 path = get_tracing_file(
"printk_formats");
378 write_or_die(&size, 4);
381 record_file(path, 4);
384 put_tracing_file(path);
388 get_tracepoints_path(
struct list_head *pattrs)
392 int nr_tracepoints = 0;
400 die(
"%s\n",
"No memory to alloc tracepoints list");
404 return nr_tracepoints > 0 ? path.next :
NULL;
431 static void tracing_data_header(
void)
439 memcpy(buf + 3,
"tracing", 7);
441 write_or_die(buf, 10);
453 write_or_die(buf, 1);
456 buf[0] =
sizeof(
long);
457 write_or_die(buf, 1);
472 tps = get_tracepoints_path(pattrs);
486 die(
"Can't make temp file");
499 tracing_data_header();
501 read_ftrace_files(tps);
502 read_event_files(tps);
503 read_proc_kallsyms();
504 read_ftrace_printk();
516 put_tracepoints_path(tps);