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 filter_print_preds(call
->preds
, s
);
485 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
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
))
508 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
512 err
= filter_parse(&pbuf
, pred
);
514 filter_free_pred(pred
);
519 filter_free_preds(call
);
520 filter_free_pred(pred
);
524 err
= filter_add_pred(call
, pred
);
526 filter_free_pred(pred
);
536 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
539 struct event_subsystem
*system
= filp
->private_data
;
546 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
552 filter_print_preds(system
->preds
, s
);
553 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
561 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
564 struct event_subsystem
*system
= filp
->private_data
;
565 char buf
[64], *pbuf
= buf
;
566 struct filter_pred
*pred
;
569 if (cnt
>= sizeof(buf
))
572 if (copy_from_user(&buf
, ubuf
, cnt
))
576 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
580 err
= filter_parse(&pbuf
, pred
);
582 filter_free_pred(pred
);
587 filter_free_subsystem_preds(system
);
588 filter_free_pred(pred
);
592 err
= filter_add_subsystem_pred(system
, pred
);
594 filter_free_subsystem_preds(system
);
595 filter_free_pred(pred
);
604 static const struct seq_operations show_event_seq_ops
= {
611 static const struct seq_operations show_set_event_seq_ops
= {
618 static const struct file_operations ftrace_avail_fops
= {
619 .open
= ftrace_event_seq_open
,
622 .release
= seq_release
,
625 static const struct file_operations ftrace_set_event_fops
= {
626 .open
= ftrace_event_seq_open
,
628 .write
= ftrace_event_write
,
630 .release
= seq_release
,
633 static const struct file_operations ftrace_enable_fops
= {
634 .open
= tracing_open_generic
,
635 .read
= event_enable_read
,
636 .write
= event_enable_write
,
639 static const struct file_operations ftrace_event_format_fops
= {
640 .open
= tracing_open_generic
,
641 .read
= event_format_read
,
644 static const struct file_operations ftrace_event_id_fops
= {
645 .open
= tracing_open_generic
,
646 .read
= event_id_read
,
649 static const struct file_operations ftrace_event_filter_fops
= {
650 .open
= tracing_open_generic
,
651 .read
= event_filter_read
,
652 .write
= event_filter_write
,
655 static const struct file_operations ftrace_subsystem_filter_fops
= {
656 .open
= tracing_open_generic
,
657 .read
= subsystem_filter_read
,
658 .write
= subsystem_filter_write
,
661 static struct dentry
*event_trace_events_dir(void)
663 static struct dentry
*d_tracer
;
664 static struct dentry
*d_events
;
669 d_tracer
= tracing_init_dentry();
673 d_events
= debugfs_create_dir("events", d_tracer
);
675 pr_warning("Could not create debugfs "
676 "'events' directory\n");
681 static LIST_HEAD(event_subsystems
);
683 static struct dentry
*
684 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
686 struct event_subsystem
*system
;
688 /* First see if we did not already create this dir */
689 list_for_each_entry(system
, &event_subsystems
, list
) {
690 if (strcmp(system
->name
, name
) == 0)
691 return system
->entry
;
694 /* need to create new entry */
695 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
697 pr_warning("No memory to create event subsystem %s\n",
702 system
->entry
= debugfs_create_dir(name
, d_events
);
703 if (!system
->entry
) {
704 pr_warning("Could not create event subsystem %s\n",
711 list_add(&system
->list
, &event_subsystems
);
713 system
->preds
= NULL
;
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
);
770 entry
= debugfs_create_file("filter", 0644, call
->dir
, call
,
771 &ftrace_event_filter_fops
);
773 pr_warning("Could not create debugfs "
774 "'%s/filter' entry\n", call
->name
);
777 /* A trace may not want to export its format */
778 if (!call
->show_format
)
781 entry
= debugfs_create_file("format", 0444, call
->dir
, call
,
782 &ftrace_event_format_fops
);
784 pr_warning("Could not create debugfs "
785 "'%s/format' entry\n", call
->name
);
790 static __init
int event_trace_init(void)
792 struct ftrace_event_call
*call
= __start_ftrace_events
;
793 struct dentry
*d_tracer
;
794 struct dentry
*entry
;
795 struct dentry
*d_events
;
797 d_tracer
= tracing_init_dentry();
801 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
802 (void *)&show_event_seq_ops
,
805 pr_warning("Could not create debugfs "
806 "'available_events' entry\n");
808 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
809 (void *)&show_set_event_seq_ops
,
810 &ftrace_set_event_fops
);
812 pr_warning("Could not create debugfs "
813 "'set_event' entry\n");
815 d_events
= event_trace_events_dir();
819 for_each_event(call
) {
820 /* The linker may leave blanks */
823 event_create_dir(call
, d_events
);
828 fs_initcall(event_trace_init
);