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_subsystem_preds(system);
589 filter_free_pred(pred);
598 static const struct seq_operations show_event_seq_ops = {
605 static const struct seq_operations show_set_event_seq_ops = {
612 static const struct file_operations ftrace_avail_fops = {
613 .open = ftrace_event_seq_open,
616 .release = seq_release,
619 static const struct file_operations ftrace_set_event_fops = {
620 .open = ftrace_event_seq_open,
622 .write = ftrace_event_write,
624 .release = seq_release,
627 static const struct file_operations ftrace_enable_fops = {
628 .open = tracing_open_generic,
629 .read = event_enable_read,
630 .write = event_enable_write,
633 static const struct file_operations ftrace_event_format_fops = {
634 .open = tracing_open_generic,
635 .read = event_format_read,
638 static const struct file_operations ftrace_event_id_fops = {
639 .open = tracing_open_generic,
640 .read = event_id_read,
643 static const struct file_operations ftrace_event_filter_fops = {
644 .open = tracing_open_generic,
645 .read = event_filter_read,
646 .write = event_filter_write,
649 static const struct file_operations ftrace_subsystem_filter_fops = {
650 .open = tracing_open_generic,
651 .read = subsystem_filter_read,
652 .write = subsystem_filter_write,
655 static struct dentry *event_trace_events_dir(void)
657 static struct dentry *d_tracer;
658 static struct dentry *d_events;
663 d_tracer = tracing_init_dentry();
667 d_events = debugfs_create_dir("events", d_tracer);
669 pr_warning("Could not create debugfs "
670 "'events' directory\n");
675 static LIST_HEAD(event_subsystems);
677 static struct dentry *
678 event_subsystem_dir(const char *name, struct dentry *d_events)
680 struct event_subsystem *system;
681 struct dentry *entry;
683 /* First see if we did not already create this dir */
684 list_for_each_entry(system, &event_subsystems, list) {
685 if (strcmp(system->name, name) == 0)
686 return system->entry;
689 /* need to create new entry */
690 system = kmalloc(sizeof(*system), GFP_KERNEL);
692 pr_warning("No memory to create event subsystem %s\n",
697 system->entry = debugfs_create_dir(name, d_events);
698 if (!system->entry) {
699 pr_warning("Could not create event subsystem %s\n",
706 list_add(&system->list, &event_subsystems);
708 system->preds = NULL;
710 entry = debugfs_create_file("filter", 0644, system->entry, system,
711 &ftrace_subsystem_filter_fops);
713 pr_warning("Could not create debugfs "
714 "'%s/filter' entry\n", name);
716 return system->entry;
720 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
722 struct dentry *entry;
726 * If the trace point header did not define TRACE_SYSTEM
727 * then the system would be called "TRACE_SYSTEM".
729 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
730 d_events = event_subsystem_dir(call->system, d_events);
732 if (call->raw_init) {
733 ret = call->raw_init();
735 pr_warning("Could not initialize trace point"
736 " events/%s\n", call->name);
741 call->dir = debugfs_create_dir(call->name, d_events);
743 pr_warning("Could not create debugfs "
744 "'%s' directory\n", call->name);
749 entry = debugfs_create_file("enable", 0644, call->dir, call,
750 &ftrace_enable_fops);
752 pr_warning("Could not create debugfs "
753 "'%s/enable' entry\n", call->name);
757 entry = debugfs_create_file("id", 0444, call->dir, call,
758 &ftrace_event_id_fops);
760 pr_warning("Could not create debugfs '%s/id' entry\n",
764 if (call->define_fields) {
765 ret = call->define_fields();
767 pr_warning("Could not initialize trace point"
768 " events/%s\n", call->name);
773 entry = debugfs_create_file("filter", 0644, call->dir, call,
774 &ftrace_event_filter_fops);
776 pr_warning("Could not create debugfs "
777 "'%s/filter' entry\n", call->name);
779 /* A trace may not want to export its format */
780 if (!call->show_format)
783 entry = debugfs_create_file("format", 0444, call->dir, call,
784 &ftrace_event_format_fops);
786 pr_warning("Could not create debugfs "
787 "'%s/format' entry\n", call->name);
792 static __init int event_trace_init(void)
794 struct ftrace_event_call *call = __start_ftrace_events;
795 struct dentry *d_tracer;
796 struct dentry *entry;
797 struct dentry *d_events;
799 d_tracer = tracing_init_dentry();
803 entry = debugfs_create_file("available_events", 0444, d_tracer,
804 (void *)&show_event_seq_ops,
807 pr_warning("Could not create debugfs "
808 "'available_events' entry\n");
810 entry = debugfs_create_file("set_event", 0644, d_tracer,
811 (void *)&show_set_event_seq_ops,
812 &ftrace_set_event_fops);
814 pr_warning("Could not create debugfs "
815 "'set_event' entry\n");
817 d_events = event_trace_events_dir();
821 for_each_event(call) {
822 /* The linker may leave blanks */
825 event_create_dir(call, d_events);
830 fs_initcall(event_trace_init);