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 int trace_define_field(struct ftrace_event_call *call, char *type,
23 char *name, int offset, int size)
25 struct ftrace_event_field *field;
27 field = kzalloc(sizeof(*field), GFP_KERNEL);
31 field->name = kstrdup(name, GFP_KERNEL);
35 field->type = kstrdup(type, GFP_KERNEL);
39 field->offset = offset;
41 list_add(&field->link, &call->fields);
55 static void ftrace_clear_events(void)
57 struct ftrace_event_call *call = (void *)__start_ftrace_events;
60 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
70 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
90 static int ftrace_set_clr_event(char *buf, int set)
92 struct ftrace_event_call *call = __start_ftrace_events;
93 char *event = NULL, *sub = NULL, *match;
97 * The buf format can be <subsystem>:<event-name>
98 * *:<event-name> means any event by that name.
99 * :<event-name> is the same.
101 * <subsystem>:* means all events in that subsystem
102 * <subsystem>: means the same.
104 * <name> (no ':') means all events in a subsystem with
105 * the name <name> or any event that matches <name>
108 match = strsep(&buf, ":");
114 if (!strlen(sub) || strcmp(sub, "*") == 0)
116 if (!strlen(event) || strcmp(event, "*") == 0)
120 mutex_lock(&event_mutex);
121 for_each_event(call) {
123 if (!call->name || !call->regfunc)
127 strcmp(match, call->name) != 0 &&
128 strcmp(match, call->system) != 0)
131 if (sub && strcmp(sub, call->system) != 0)
134 if (event && strcmp(event, call->name) != 0)
137 ftrace_event_enable_disable(call, set);
141 mutex_unlock(&event_mutex);
146 /* 128 should be much more than enough */
147 #define EVENT_BUF_SIZE 127
150 ftrace_event_write(struct file *file, const char __user *ubuf,
151 size_t cnt, loff_t *ppos)
162 ret = tracing_update_buffers();
166 ret = get_user(ch, ubuf++);
172 /* skip white space */
173 while (cnt && isspace(ch)) {
174 ret = get_user(ch, ubuf++);
181 /* Only white space found? */
188 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
192 if (cnt > EVENT_BUF_SIZE)
193 cnt = EVENT_BUF_SIZE;
196 while (cnt && !isspace(ch)) {
202 ret = get_user(ch, ubuf++);
212 ret = ftrace_set_clr_event(buf, set);
225 t_next(struct seq_file *m, void *v, loff_t *pos)
227 struct ftrace_event_call *call = m->private;
228 struct ftrace_event_call *next = call;
233 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
237 * The ftrace subsystem is for showing formats only.
238 * They can not be enabled or disabled via the event files.
252 static void *t_start(struct seq_file *m, loff_t *pos)
254 return t_next(m, NULL, pos);
258 s_next(struct seq_file *m, void *v, loff_t *pos)
260 struct ftrace_event_call *call = m->private;
261 struct ftrace_event_call *next;
266 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
269 if (!call->enabled) {
280 static void *s_start(struct seq_file *m, loff_t *pos)
282 return s_next(m, NULL, pos);
285 static int t_show(struct seq_file *m, void *v)
287 struct ftrace_event_call *call = v;
289 if (strcmp(call->system, TRACE_SYSTEM) != 0)
290 seq_printf(m, "%s:", call->system);
291 seq_printf(m, "%s\n", call->name);
296 static void t_stop(struct seq_file *m, void *p)
301 ftrace_event_seq_open(struct inode *inode, struct file *file)
304 const struct seq_operations *seq_ops;
306 if ((file->f_mode & FMODE_WRITE) &&
307 !(file->f_flags & O_APPEND))
308 ftrace_clear_events();
310 seq_ops = inode->i_private;
311 ret = seq_open(file, seq_ops);
313 struct seq_file *m = file->private_data;
315 m->private = __start_ftrace_events;
321 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
324 struct ftrace_event_call *call = filp->private_data;
332 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
336 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
339 struct ftrace_event_call *call = filp->private_data;
344 if (cnt >= sizeof(buf))
347 if (copy_from_user(&buf, ubuf, cnt))
352 ret = strict_strtoul(buf, 10, &val);
356 ret = tracing_update_buffers();
363 mutex_lock(&event_mutex);
364 ftrace_event_enable_disable(call, val);
365 mutex_unlock(&event_mutex);
378 #define FIELD(type, name) \
379 #type, "common_" #name, offsetof(typeof(field), name), \
382 static int trace_write_header(struct trace_seq *s)
384 struct trace_entry field;
386 /* struct trace_entry */
387 return trace_seq_printf(s,
388 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
389 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
390 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
391 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
392 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
394 FIELD(unsigned char, type),
395 FIELD(unsigned char, flags),
396 FIELD(unsigned char, preempt_count),
402 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
405 struct ftrace_event_call *call = filp->private_data;
413 s = kmalloc(sizeof(*s), GFP_KERNEL);
419 /* If any of the first writes fail, so will the show_format. */
421 trace_seq_printf(s, "name: %s\n", call->name);
422 trace_seq_printf(s, "ID: %d\n", call->id);
423 trace_seq_printf(s, "format:\n");
424 trace_write_header(s);
426 r = call->show_format(s);
429 * ug! The format output is bigger than a PAGE!!
431 buf = "FORMAT TOO BIG\n";
432 r = simple_read_from_buffer(ubuf, cnt, ppos,
437 r = simple_read_from_buffer(ubuf, cnt, ppos,
445 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
447 struct ftrace_event_call *call = filp->private_data;
454 s = kmalloc(sizeof(*s), GFP_KERNEL);
459 trace_seq_printf(s, "%d\n", call->id);
461 r = simple_read_from_buffer(ubuf, cnt, ppos,
468 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
471 struct ftrace_event_call *call = filp->private_data;
478 s = kmalloc(sizeof(*s), GFP_KERNEL);
484 r = filter_print_preds(call->preds, s->buffer);
485 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, r);
493 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
496 struct ftrace_event_call *call = filp->private_data;
497 char buf[64], *pbuf = buf;
498 struct filter_pred *pred;
501 if (cnt >= sizeof(buf))
504 if (copy_from_user(&buf, ubuf, cnt))
507 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
511 err = filter_parse(&pbuf, pred);
513 filter_free_pred(pred);
518 filter_free_preds(call);
522 if (filter_add_pred(call, pred)) {
523 filter_free_pred(pred);
533 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
536 struct event_subsystem *system = filp->private_data;
543 s = kmalloc(sizeof(*s), GFP_KERNEL);
549 r = filter_print_preds(system->preds, s->buffer);
550 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, r);
558 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
561 struct event_subsystem *system = filp->private_data;
562 char buf[64], *pbuf = buf;
563 struct filter_pred *pred;
566 if (cnt >= sizeof(buf))
569 if (copy_from_user(&buf, ubuf, cnt))
572 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
576 err = filter_parse(&pbuf, pred);
578 filter_free_pred(pred);
583 filter_free_subsystem_preds(system);
587 if (filter_add_subsystem_pred(system, pred)) {
588 filter_free_pred(pred);
597 static const struct seq_operations show_event_seq_ops = {
604 static const struct seq_operations show_set_event_seq_ops = {
611 static const struct file_operations ftrace_avail_fops = {
612 .open = ftrace_event_seq_open,
615 .release = seq_release,
618 static const struct file_operations ftrace_set_event_fops = {
619 .open = ftrace_event_seq_open,
621 .write = ftrace_event_write,
623 .release = seq_release,
626 static const struct file_operations ftrace_enable_fops = {
627 .open = tracing_open_generic,
628 .read = event_enable_read,
629 .write = event_enable_write,
632 static const struct file_operations ftrace_event_format_fops = {
633 .open = tracing_open_generic,
634 .read = event_format_read,
637 static const struct file_operations ftrace_event_id_fops = {
638 .open = tracing_open_generic,
639 .read = event_id_read,
642 static const struct file_operations ftrace_event_filter_fops = {
643 .open = tracing_open_generic,
644 .read = event_filter_read,
645 .write = event_filter_write,
648 static const struct file_operations ftrace_subsystem_filter_fops = {
649 .open = tracing_open_generic,
650 .read = subsystem_filter_read,
651 .write = subsystem_filter_write,
654 static struct dentry *event_trace_events_dir(void)
656 static struct dentry *d_tracer;
657 static struct dentry *d_events;
662 d_tracer = tracing_init_dentry();
666 d_events = debugfs_create_dir("events", d_tracer);
668 pr_warning("Could not create debugfs "
669 "'events' directory\n");
674 static LIST_HEAD(event_subsystems);
676 static struct dentry *
677 event_subsystem_dir(const char *name, struct dentry *d_events)
679 struct event_subsystem *system;
680 struct dentry *entry;
682 /* First see if we did not already create this dir */
683 list_for_each_entry(system, &event_subsystems, list) {
684 if (strcmp(system->name, name) == 0)
685 return system->entry;
688 /* need to create new entry */
689 system = kmalloc(sizeof(*system), GFP_KERNEL);
691 pr_warning("No memory to create event subsystem %s\n",
696 system->entry = debugfs_create_dir(name, d_events);
697 if (!system->entry) {
698 pr_warning("Could not create event subsystem %s\n",
705 list_add(&system->list, &event_subsystems);
707 system->preds = NULL;
709 entry = debugfs_create_file("filter", 0444, system->entry, system,
710 &ftrace_subsystem_filter_fops);
712 pr_warning("Could not create debugfs "
713 "'%s/filter' entry\n", name);
715 return system->entry;
719 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
721 struct dentry *entry;
725 * If the trace point header did not define TRACE_SYSTEM
726 * then the system would be called "TRACE_SYSTEM".
728 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
729 d_events = event_subsystem_dir(call->system, d_events);
731 if (call->raw_init) {
732 ret = call->raw_init();
734 pr_warning("Could not initialize trace point"
735 " events/%s\n", call->name);
740 call->dir = debugfs_create_dir(call->name, d_events);
742 pr_warning("Could not create debugfs "
743 "'%s' directory\n", call->name);
748 entry = debugfs_create_file("enable", 0644, call->dir, call,
749 &ftrace_enable_fops);
751 pr_warning("Could not create debugfs "
752 "'%s/enable' entry\n", call->name);
756 entry = debugfs_create_file("id", 0444, call->dir, call,
757 &ftrace_event_id_fops);
759 pr_warning("Could not create debugfs '%s/id' entry\n",
763 if (call->define_fields) {
764 ret = call->define_fields();
766 pr_warning("Could not initialize trace point"
767 " events/%s\n", call->name);
772 entry = debugfs_create_file("filter", 0444, call->dir, call,
773 &ftrace_event_filter_fops);
775 pr_warning("Could not create debugfs "
776 "'%s/filter' entry\n", call->name);
778 /* A trace may not want to export its format */
779 if (!call->show_format)
782 entry = debugfs_create_file("format", 0444, call->dir, call,
783 &ftrace_event_format_fops);
785 pr_warning("Could not create debugfs "
786 "'%s/format' entry\n", call->name);
791 static __init int event_trace_init(void)
793 struct ftrace_event_call *call = __start_ftrace_events;
794 struct dentry *d_tracer;
795 struct dentry *entry;
796 struct dentry *d_events;
798 d_tracer = tracing_init_dentry();
802 entry = debugfs_create_file("available_events", 0444, d_tracer,
803 (void *)&show_event_seq_ops,
806 pr_warning("Could not create debugfs "
807 "'available_events' entry\n");
809 entry = debugfs_create_file("set_event", 0644, d_tracer,
810 (void *)&show_set_event_seq_ops,
811 &ftrace_set_event_fops);
813 pr_warning("Could not create debugfs "
814 "'set_event' entry\n");
816 d_events = event_trace_events_dir();
820 for_each_event(call) {
821 /* The linker may leave blanks */
824 event_create_dir(call, d_events);
829 fs_initcall(event_trace_init);