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>
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,
62 if (call->raw_enabled) {
63 call->raw_enabled = 0;
69 (call->type & TRACE_EVENT_TYPE_PRINTF)) {
73 if (!call->raw_enabled &&
74 (call->type & TRACE_EVENT_TYPE_RAW)) {
75 call->raw_enabled = 1;
82 static int ftrace_set_clr_event(char *buf, int set)
84 struct ftrace_event_call *call = __start_ftrace_events;
85 char *event = NULL, *sub = NULL, *match;
89 * The buf format can be <subsystem>:<event-name>
90 * *:<event-name> means any event by that name.
91 * :<event-name> is the same.
93 * <subsystem>:* means all events in that subsystem
94 * <subsystem>: means the same.
96 * <name> (no ':') means all events in a subsystem with
97 * the name <name> or any event that matches <name>
100 match = strsep(&buf, ":");
106 if (!strlen(sub) || strcmp(sub, "*") == 0)
108 if (!strlen(event) || strcmp(event, "*") == 0)
112 mutex_lock(&event_mutex);
113 events_for_each(call) {
119 strcmp(match, call->name) != 0 &&
120 strcmp(match, call->system) != 0)
123 if (sub && strcmp(sub, call->system) != 0)
126 if (event && strcmp(event, call->name) != 0)
129 ftrace_event_enable_disable(call, set);
133 mutex_unlock(&event_mutex);
138 /* 128 should be much more than enough */
139 #define EVENT_BUF_SIZE 127
142 ftrace_event_write(struct file *file, const char __user *ubuf,
143 size_t cnt, loff_t *ppos)
154 ret = get_user(ch, ubuf++);
160 /* skip white space */
161 while (cnt && isspace(ch)) {
162 ret = get_user(ch, ubuf++);
169 /* Only white space found? */
176 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
180 if (cnt > EVENT_BUF_SIZE)
181 cnt = EVENT_BUF_SIZE;
184 while (cnt && !isspace(ch)) {
190 ret = get_user(ch, ubuf++);
200 ret = ftrace_set_clr_event(buf, set);
213 t_next(struct seq_file *m, void *v, loff_t *pos)
215 struct ftrace_event_call *call = m->private;
216 struct ftrace_event_call *next = call;
220 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
228 static void *t_start(struct seq_file *m, loff_t *pos)
230 return t_next(m, NULL, pos);
234 s_next(struct seq_file *m, void *v, loff_t *pos)
236 struct ftrace_event_call *call = m->private;
237 struct ftrace_event_call *next;
242 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
245 if (!call->enabled) {
256 static void *s_start(struct seq_file *m, loff_t *pos)
258 return s_next(m, NULL, pos);
261 static int t_show(struct seq_file *m, void *v)
263 struct ftrace_event_call *call = v;
265 if (strcmp(call->system, TRACE_SYSTEM) != 0)
266 seq_printf(m, "%s:", call->system);
267 seq_printf(m, "%s\n", call->name);
272 static void t_stop(struct seq_file *m, void *p)
277 ftrace_event_seq_open(struct inode *inode, struct file *file)
280 const struct seq_operations *seq_ops;
282 if ((file->f_mode & FMODE_WRITE) &&
283 !(file->f_flags & O_APPEND))
284 ftrace_clear_events();
286 seq_ops = inode->i_private;
287 ret = seq_open(file, seq_ops);
289 struct seq_file *m = file->private_data;
291 m->private = __start_ftrace_events;
297 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
300 struct ftrace_event_call *call = filp->private_data;
303 if (call->enabled || call->raw_enabled)
308 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
312 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
315 struct ftrace_event_call *call = filp->private_data;
320 if (cnt >= sizeof(buf))
323 if (copy_from_user(&buf, ubuf, cnt))
328 ret = strict_strtoul(buf, 10, &val);
335 mutex_lock(&event_mutex);
336 ftrace_event_enable_disable(call, val);
337 mutex_unlock(&event_mutex);
350 event_type_read(struct file *filp, char __user *ubuf, size_t cnt,
353 struct ftrace_event_call *call = filp->private_data;
357 if (call->type & TRACE_EVENT_TYPE_PRINTF)
358 r += sprintf(buf, "printf\n");
360 if (call->type & TRACE_EVENT_TYPE_RAW)
361 r += sprintf(buf+r, "raw\n");
363 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
367 event_type_write(struct file *filp, const char __user *ubuf, size_t cnt,
370 struct ftrace_event_call *call = filp->private_data;
374 * If there's only one type, we can't change it.
375 * And currently we always have printf type, and we
376 * may or may not have raw type.
378 * This is a redundant check, the file should be read
379 * only if this is the case anyway.
385 if (cnt >= sizeof(buf))
388 if (copy_from_user(&buf, ubuf, cnt))
393 if (!strncmp(buf, "printf", 6) &&
394 (!buf[6] || isspace(buf[6]))) {
396 call->type = TRACE_EVENT_TYPE_PRINTF;
399 * If raw enabled, the disable it and enable
402 if (call->raw_enabled) {
403 call->raw_enabled = 0;
410 } else if (!strncmp(buf, "raw", 3) &&
411 (!buf[3] || isspace(buf[3]))) {
413 call->type = TRACE_EVENT_TYPE_RAW;
416 * If printf enabled, the disable it and enable
423 call->raw_enabled = 1;
435 event_available_types_read(struct file *filp, char __user *ubuf, size_t cnt,
438 struct ftrace_event_call *call = filp->private_data;
442 r += sprintf(buf, "printf\n");
445 r += sprintf(buf+r, "raw\n");
447 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
451 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
454 struct ftrace_event_call *call = filp->private_data;
459 s = kmalloc(sizeof(*s), GFP_KERNEL);
468 r = call->show_format(s);
471 * ug! The format output is bigger than a PAGE!!
473 buf = "FORMAT TOO BIG\n";
474 r = simple_read_from_buffer(ubuf, cnt, ppos,
479 r = simple_read_from_buffer(ubuf, cnt, ppos,
486 static const struct seq_operations show_event_seq_ops = {
493 static const struct seq_operations show_set_event_seq_ops = {
500 static const struct file_operations ftrace_avail_fops = {
501 .open = ftrace_event_seq_open,
504 .release = seq_release,
507 static const struct file_operations ftrace_set_event_fops = {
508 .open = ftrace_event_seq_open,
510 .write = ftrace_event_write,
512 .release = seq_release,
515 static const struct file_operations ftrace_enable_fops = {
516 .open = tracing_open_generic,
517 .read = event_enable_read,
518 .write = event_enable_write,
521 static const struct file_operations ftrace_type_fops = {
522 .open = tracing_open_generic,
523 .read = event_type_read,
524 .write = event_type_write,
527 static const struct file_operations ftrace_available_types_fops = {
528 .open = tracing_open_generic,
529 .read = event_available_types_read,
532 static const struct file_operations ftrace_event_format_fops = {
533 .open = tracing_open_generic,
534 .read = event_format_read,
537 static struct dentry *event_trace_events_dir(void)
539 static struct dentry *d_tracer;
540 static struct dentry *d_events;
545 d_tracer = tracing_init_dentry();
549 d_events = debugfs_create_dir("events", d_tracer);
551 pr_warning("Could not create debugfs "
552 "'events' directory\n");
557 struct event_subsystem {
558 struct list_head list;
560 struct dentry *entry;
563 static LIST_HEAD(event_subsystems);
565 static struct dentry *
566 event_subsystem_dir(const char *name, struct dentry *d_events)
568 struct event_subsystem *system;
570 /* First see if we did not already create this dir */
571 list_for_each_entry(system, &event_subsystems, list) {
572 if (strcmp(system->name, name) == 0)
573 return system->entry;
576 /* need to create new entry */
577 system = kmalloc(sizeof(*system), GFP_KERNEL);
579 pr_warning("No memory to create event subsystem %s\n",
584 system->entry = debugfs_create_dir(name, d_events);
585 if (!system->entry) {
586 pr_warning("Could not create event subsystem %s\n",
593 list_add(&system->list, &event_subsystems);
595 return system->entry;
599 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
601 struct dentry *entry;
605 * If the trace point header did not define TRACE_SYSTEM
606 * then the system would be called "TRACE_SYSTEM".
608 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
609 d_events = event_subsystem_dir(call->system, d_events);
611 if (call->raw_init) {
612 ret = call->raw_init();
614 pr_warning("Could not initialize trace point"
615 " events/%s\n", call->name);
620 /* default the output to printf */
621 call->type = TRACE_EVENT_TYPE_PRINTF;
623 call->dir = debugfs_create_dir(call->name, d_events);
625 pr_warning("Could not create debugfs "
626 "'%s' directory\n", call->name);
630 entry = debugfs_create_file("enable", 0644, call->dir, call,
631 &ftrace_enable_fops);
633 pr_warning("Could not create debugfs "
634 "'%s/enable' entry\n", call->name);
636 /* Only let type be writable, if we can change it */
637 entry = debugfs_create_file("type",
638 call->raw_init ? 0644 : 0444,
642 pr_warning("Could not create debugfs "
643 "'%s/type' entry\n", call->name);
645 entry = debugfs_create_file("available_types", 0444, call->dir, call,
646 &ftrace_available_types_fops);
648 pr_warning("Could not create debugfs "
649 "'%s/available_types' entry\n", call->name);
651 /* A trace may not want to export its format */
652 if (!call->show_format)
655 entry = debugfs_create_file("format", 0444, call->dir, call,
656 &ftrace_event_format_fops);
658 pr_warning("Could not create debugfs "
659 "'%s/format' entry\n", call->name);
664 static __init int event_trace_init(void)
666 struct ftrace_event_call *call = __start_ftrace_events;
667 struct dentry *d_tracer;
668 struct dentry *entry;
669 struct dentry *d_events;
671 d_tracer = tracing_init_dentry();
675 entry = debugfs_create_file("available_events", 0444, d_tracer,
676 (void *)&show_event_seq_ops,
679 pr_warning("Could not create debugfs "
680 "'available_events' entry\n");
682 entry = debugfs_create_file("set_event", 0644, d_tracer,
683 (void *)&show_set_event_seq_ops,
684 &ftrace_set_event_fops);
686 pr_warning("Could not create debugfs "
687 "'set_event' entry\n");
689 d_events = event_trace_events_dir();
693 events_for_each(call) {
694 /* The linker may leave blanks */
697 event_create_dir(call, d_events);
702 fs_initcall(event_trace_init);