4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
11 #include <linux/debugfs.h>
12 #include <linux/uaccess.h>
13 #include <linux/module.h>
14 #include <linux/ctype.h>
16 #include "trace_output.h"
18 #define TRACE_SYSTEM "TRACE_SYSTEM"
20 static DEFINE_MUTEX(event_mutex);
22 #define events_for_each(event) \
23 for (event = __start_ftrace_events; \
24 (unsigned long)event < (unsigned long)__stop_ftrace_events; \
27 void event_trace_printk(unsigned long ip, const char *fmt, ...)
32 tracing_record_cmdline(current);
33 trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
37 static void ftrace_clear_events(void)
39 struct ftrace_event_call *call = (void *)__start_ftrace_events;
42 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
52 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
72 static int ftrace_set_clr_event(char *buf, int set)
74 struct ftrace_event_call *call = __start_ftrace_events;
75 char *event = NULL, *sub = NULL, *match;
79 * The buf format can be <subsystem>:<event-name>
80 * *:<event-name> means any event by that name.
81 * :<event-name> is the same.
83 * <subsystem>:* means all events in that subsystem
84 * <subsystem>: means the same.
86 * <name> (no ':') means all events in a subsystem with
87 * the name <name> or any event that matches <name>
90 match = strsep(&buf, ":");
96 if (!strlen(sub) || strcmp(sub, "*") == 0)
98 if (!strlen(event) || strcmp(event, "*") == 0)
102 mutex_lock(&event_mutex);
103 events_for_each(call) {
109 strcmp(match, call->name) != 0 &&
110 strcmp(match, call->system) != 0)
113 if (sub && strcmp(sub, call->system) != 0)
116 if (event && strcmp(event, call->name) != 0)
119 ftrace_event_enable_disable(call, set);
123 mutex_unlock(&event_mutex);
128 /* 128 should be much more than enough */
129 #define EVENT_BUF_SIZE 127
132 ftrace_event_write(struct file *file, const char __user *ubuf,
133 size_t cnt, loff_t *ppos)
144 ret = get_user(ch, ubuf++);
150 /* skip white space */
151 while (cnt && isspace(ch)) {
152 ret = get_user(ch, ubuf++);
159 /* Only white space found? */
166 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
170 if (cnt > EVENT_BUF_SIZE)
171 cnt = EVENT_BUF_SIZE;
174 while (cnt && !isspace(ch)) {
180 ret = get_user(ch, ubuf++);
190 ret = ftrace_set_clr_event(buf, set);
203 t_next(struct seq_file *m, void *v, loff_t *pos)
205 struct ftrace_event_call *call = m->private;
206 struct ftrace_event_call *next = call;
210 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
218 static void *t_start(struct seq_file *m, loff_t *pos)
220 return t_next(m, NULL, pos);
224 s_next(struct seq_file *m, void *v, loff_t *pos)
226 struct ftrace_event_call *call = m->private;
227 struct ftrace_event_call *next;
232 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
235 if (!call->enabled) {
246 static void *s_start(struct seq_file *m, loff_t *pos)
248 return s_next(m, NULL, pos);
251 static int t_show(struct seq_file *m, void *v)
253 struct ftrace_event_call *call = v;
255 if (strcmp(call->system, TRACE_SYSTEM) != 0)
256 seq_printf(m, "%s:", call->system);
257 seq_printf(m, "%s\n", call->name);
262 static void t_stop(struct seq_file *m, void *p)
267 ftrace_event_seq_open(struct inode *inode, struct file *file)
270 const struct seq_operations *seq_ops;
272 if ((file->f_mode & FMODE_WRITE) &&
273 !(file->f_flags & O_APPEND))
274 ftrace_clear_events();
276 seq_ops = inode->i_private;
277 ret = seq_open(file, seq_ops);
279 struct seq_file *m = file->private_data;
281 m->private = __start_ftrace_events;
287 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
290 struct ftrace_event_call *call = filp->private_data;
298 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
302 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
305 struct ftrace_event_call *call = filp->private_data;
310 if (cnt >= sizeof(buf))
313 if (copy_from_user(&buf, ubuf, cnt))
318 ret = strict_strtoul(buf, 10, &val);
325 mutex_lock(&event_mutex);
326 ftrace_event_enable_disable(call, val);
327 mutex_unlock(&event_mutex);
340 #define FIELD(type, name) \
341 #type, #name, (unsigned int)offsetof(typeof(field), name), \
342 (unsigned int)sizeof(field.name)
344 static int trace_write_header(struct trace_seq *s)
346 struct trace_entry field;
348 /* struct trace_entry */
349 return trace_seq_printf(s,
350 "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
351 "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
352 "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
353 "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
354 "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
356 FIELD(unsigned char, type),
357 FIELD(unsigned char, flags),
358 FIELD(unsigned char, preempt_count),
364 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
367 struct ftrace_event_call *call = filp->private_data;
372 s = kmalloc(sizeof(*s), GFP_KERNEL);
381 /* If any of the first writes fail, so will the show_format. */
383 trace_seq_printf(s, "name: %s\n", call->name);
384 trace_seq_printf(s, "ID: %d\n", call->id);
385 trace_seq_printf(s, "format:\n");
386 trace_write_header(s);
388 r = call->show_format(s);
391 * ug! The format output is bigger than a PAGE!!
393 buf = "FORMAT TOO BIG\n";
394 r = simple_read_from_buffer(ubuf, cnt, ppos,
399 r = simple_read_from_buffer(ubuf, cnt, ppos,
406 static const struct seq_operations show_event_seq_ops = {
413 static const struct seq_operations show_set_event_seq_ops = {
420 static const struct file_operations ftrace_set_event_fops = {
421 .open = ftrace_event_seq_open,
423 .write = ftrace_event_write,
425 .release = seq_release,
428 static const struct file_operations ftrace_enable_fops = {
429 .open = tracing_open_generic,
430 .read = event_enable_read,
431 .write = event_enable_write,
434 static const struct file_operations ftrace_event_format_fops = {
435 .open = tracing_open_generic,
436 .read = event_format_read,
439 static struct dentry *event_trace_events_dir(void)
441 static struct dentry *d_tracer;
442 static struct dentry *d_events;
447 d_tracer = tracing_init_dentry();
451 d_events = debugfs_create_dir("events", d_tracer);
453 pr_warning("Could not create debugfs "
454 "'events' directory\n");
459 struct event_subsystem {
460 struct list_head list;
462 struct dentry *entry;
465 static LIST_HEAD(event_subsystems);
467 static struct dentry *
468 event_subsystem_dir(const char *name, struct dentry *d_events)
470 struct event_subsystem *system;
472 /* First see if we did not already create this dir */
473 list_for_each_entry(system, &event_subsystems, list) {
474 if (strcmp(system->name, name) == 0)
475 return system->entry;
478 /* need to create new entry */
479 system = kmalloc(sizeof(*system), GFP_KERNEL);
481 pr_warning("No memory to create event subsystem %s\n",
486 system->entry = debugfs_create_dir(name, d_events);
487 if (!system->entry) {
488 pr_warning("Could not create event subsystem %s\n",
495 list_add(&system->list, &event_subsystems);
497 return system->entry;
501 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
503 struct dentry *entry;
507 * If the trace point header did not define TRACE_SYSTEM
508 * then the system would be called "TRACE_SYSTEM".
510 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
511 d_events = event_subsystem_dir(call->system, d_events);
513 if (call->raw_init) {
514 ret = call->raw_init();
516 pr_warning("Could not initialize trace point"
517 " events/%s\n", call->name);
522 call->dir = debugfs_create_dir(call->name, d_events);
524 pr_warning("Could not create debugfs "
525 "'%s' directory\n", call->name);
530 entry = debugfs_create_file("enable", 0644, call->dir, call,
531 &ftrace_enable_fops);
533 pr_warning("Could not create debugfs "
534 "'%s/enable' entry\n", call->name);
537 /* A trace may not want to export its format */
538 if (!call->show_format)
541 entry = debugfs_create_file("format", 0444, call->dir, call,
542 &ftrace_event_format_fops);
544 pr_warning("Could not create debugfs "
545 "'%s/format' entry\n", call->name);
550 static __init int event_trace_init(void)
552 struct ftrace_event_call *call = __start_ftrace_events;
553 struct dentry *d_tracer;
554 struct dentry *entry;
555 struct dentry *d_events;
557 d_tracer = tracing_init_dentry();
561 entry = debugfs_create_file("set_event", 0644, d_tracer,
562 (void *)&show_set_event_seq_ops,
563 &ftrace_set_event_fops);
565 pr_warning("Could not create debugfs "
566 "'set_event' entry\n");
568 d_events = event_trace_events_dir();
572 events_for_each(call) {
573 /* The linker may leave blanks */
576 event_create_dir(call, d_events);
581 fs_initcall(event_trace_init);