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 LIST_HEAD(ftrace_events
);
24 int trace_define_field(struct ftrace_event_call
*call
, char *type
,
25 char *name
, int offset
, int size
)
27 struct ftrace_event_field
*field
;
29 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
33 field
->name
= kstrdup(name
, GFP_KERNEL
);
37 field
->type
= kstrdup(type
, GFP_KERNEL
);
41 field
->offset
= offset
;
43 list_add(&field
->link
, &call
->fields
);
56 EXPORT_SYMBOL_GPL(trace_define_field
);
58 static void ftrace_clear_events(void)
60 struct ftrace_event_call
*call
;
62 list_for_each_entry(call
, &ftrace_events
, list
) {
71 static void ftrace_event_enable_disable(struct ftrace_event_call
*call
,
91 static int ftrace_set_clr_event(char *buf
, int set
)
93 struct ftrace_event_call
*call
;
94 char *event
= NULL
, *sub
= NULL
, *match
;
98 * The buf format can be <subsystem>:<event-name>
99 * *:<event-name> means any event by that name.
100 * :<event-name> is the same.
102 * <subsystem>:* means all events in that subsystem
103 * <subsystem>: means the same.
105 * <name> (no ':') means all events in a subsystem with
106 * the name <name> or any event that matches <name>
109 match
= strsep(&buf
, ":");
115 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
117 if (!strlen(event
) || strcmp(event
, "*") == 0)
121 mutex_lock(&event_mutex
);
122 list_for_each_entry(call
, &ftrace_events
, list
) {
124 if (!call
->name
|| !call
->regfunc
)
128 strcmp(match
, call
->name
) != 0 &&
129 strcmp(match
, call
->system
) != 0)
132 if (sub
&& strcmp(sub
, call
->system
) != 0)
135 if (event
&& strcmp(event
, call
->name
) != 0)
138 ftrace_event_enable_disable(call
, set
);
142 mutex_unlock(&event_mutex
);
147 /* 128 should be much more than enough */
148 #define EVENT_BUF_SIZE 127
151 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
152 size_t cnt
, loff_t
*ppos
)
163 ret
= tracing_update_buffers();
167 ret
= get_user(ch
, ubuf
++);
173 /* skip white space */
174 while (cnt
&& isspace(ch
)) {
175 ret
= get_user(ch
, ubuf
++);
182 /* Only white space found? */
189 buf
= kmalloc(EVENT_BUF_SIZE
+1, GFP_KERNEL
);
193 if (cnt
> EVENT_BUF_SIZE
)
194 cnt
= EVENT_BUF_SIZE
;
197 while (cnt
&& !isspace(ch
)) {
203 ret
= get_user(ch
, ubuf
++);
213 ret
= ftrace_set_clr_event(buf
, set
);
226 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
228 struct list_head
*list
= m
->private;
229 struct ftrace_event_call
*call
;
234 if (list
== &ftrace_events
)
237 call
= list_entry(list
, struct ftrace_event_call
, list
);
240 * The ftrace subsystem is for showing formats only.
241 * They can not be enabled or disabled via the event files.
249 m
->private = list
->next
;
254 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
256 return t_next(m
, NULL
, pos
);
260 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
262 struct list_head
*list
= m
->private;
263 struct ftrace_event_call
*call
;
268 if (list
== &ftrace_events
)
271 call
= list_entry(list
, struct ftrace_event_call
, list
);
273 if (!call
->enabled
) {
278 m
->private = list
->next
;
283 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
285 return s_next(m
, NULL
, pos
);
288 static int t_show(struct seq_file
*m
, void *v
)
290 struct ftrace_event_call
*call
= v
;
292 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
293 seq_printf(m
, "%s:", call
->system
);
294 seq_printf(m
, "%s\n", call
->name
);
299 static void t_stop(struct seq_file
*m
, void *p
)
304 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
307 const struct seq_operations
*seq_ops
;
309 if ((file
->f_mode
& FMODE_WRITE
) &&
310 !(file
->f_flags
& O_APPEND
))
311 ftrace_clear_events();
313 seq_ops
= inode
->i_private
;
314 ret
= seq_open(file
, seq_ops
);
316 struct seq_file
*m
= file
->private_data
;
318 m
->private = ftrace_events
.next
;
324 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
327 struct ftrace_event_call
*call
= filp
->private_data
;
335 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
339 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
342 struct ftrace_event_call
*call
= filp
->private_data
;
347 if (cnt
>= sizeof(buf
))
350 if (copy_from_user(&buf
, ubuf
, cnt
))
355 ret
= strict_strtoul(buf
, 10, &val
);
359 ret
= tracing_update_buffers();
366 mutex_lock(&event_mutex
);
367 ftrace_event_enable_disable(call
, val
);
368 mutex_unlock(&event_mutex
);
381 #define FIELD(type, name) \
382 #type, "common_" #name, offsetof(typeof(field), name), \
385 static int trace_write_header(struct trace_seq
*s
)
387 struct trace_entry field
;
389 /* struct trace_entry */
390 return trace_seq_printf(s
,
391 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
392 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
393 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
394 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
395 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
397 FIELD(unsigned char, type
),
398 FIELD(unsigned char, flags
),
399 FIELD(unsigned char, preempt_count
),
405 event_format_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
408 struct ftrace_event_call
*call
= filp
->private_data
;
416 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
422 /* If any of the first writes fail, so will the show_format. */
424 trace_seq_printf(s
, "name: %s\n", call
->name
);
425 trace_seq_printf(s
, "ID: %d\n", call
->id
);
426 trace_seq_printf(s
, "format:\n");
427 trace_write_header(s
);
429 r
= call
->show_format(s
);
432 * ug! The format output is bigger than a PAGE!!
434 buf
= "FORMAT TOO BIG\n";
435 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
440 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
448 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
450 struct ftrace_event_call
*call
= filp
->private_data
;
457 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
462 trace_seq_printf(s
, "%d\n", call
->id
);
464 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
471 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
474 struct ftrace_event_call
*call
= filp
->private_data
;
481 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
487 filter_print_preds(call
->preds
, call
->n_preds
, s
);
488 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
496 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
499 struct ftrace_event_call
*call
= filp
->private_data
;
500 char buf
[64], *pbuf
= buf
;
501 struct filter_pred
*pred
;
504 if (cnt
>= sizeof(buf
))
507 if (copy_from_user(&buf
, ubuf
, cnt
))
511 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
515 err
= filter_parse(&pbuf
, pred
);
517 filter_free_pred(pred
);
522 filter_disable_preds(call
);
523 filter_free_pred(pred
);
527 err
= filter_add_pred(call
, pred
);
529 filter_free_pred(pred
);
533 filter_free_pred(pred
);
541 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
544 struct event_subsystem
*system
= filp
->private_data
;
551 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
557 filter_print_preds(system
->preds
, system
->n_preds
, s
);
558 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
566 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
569 struct event_subsystem
*system
= filp
->private_data
;
570 char buf
[64], *pbuf
= buf
;
571 struct filter_pred
*pred
;
574 if (cnt
>= sizeof(buf
))
577 if (copy_from_user(&buf
, ubuf
, cnt
))
581 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
585 err
= filter_parse(&pbuf
, pred
);
587 filter_free_pred(pred
);
592 filter_free_subsystem_preds(system
);
593 filter_free_pred(pred
);
597 err
= filter_add_subsystem_pred(system
, pred
);
599 filter_free_subsystem_preds(system
);
600 filter_free_pred(pred
);
609 static const struct seq_operations show_event_seq_ops
= {
616 static const struct seq_operations show_set_event_seq_ops
= {
623 static const struct file_operations ftrace_avail_fops
= {
624 .open
= ftrace_event_seq_open
,
627 .release
= seq_release
,
630 static const struct file_operations ftrace_set_event_fops
= {
631 .open
= ftrace_event_seq_open
,
633 .write
= ftrace_event_write
,
635 .release
= seq_release
,
638 static const struct file_operations ftrace_enable_fops
= {
639 .open
= tracing_open_generic
,
640 .read
= event_enable_read
,
641 .write
= event_enable_write
,
644 static const struct file_operations ftrace_event_format_fops
= {
645 .open
= tracing_open_generic
,
646 .read
= event_format_read
,
649 static const struct file_operations ftrace_event_id_fops
= {
650 .open
= tracing_open_generic
,
651 .read
= event_id_read
,
654 static const struct file_operations ftrace_event_filter_fops
= {
655 .open
= tracing_open_generic
,
656 .read
= event_filter_read
,
657 .write
= event_filter_write
,
660 static const struct file_operations ftrace_subsystem_filter_fops
= {
661 .open
= tracing_open_generic
,
662 .read
= subsystem_filter_read
,
663 .write
= subsystem_filter_write
,
666 static struct dentry
*event_trace_events_dir(void)
668 static struct dentry
*d_tracer
;
669 static struct dentry
*d_events
;
674 d_tracer
= tracing_init_dentry();
678 d_events
= debugfs_create_dir("events", d_tracer
);
680 pr_warning("Could not create debugfs "
681 "'events' directory\n");
686 static LIST_HEAD(event_subsystems
);
688 static struct dentry
*
689 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
691 struct event_subsystem
*system
;
692 struct dentry
*entry
;
694 /* First see if we did not already create this dir */
695 list_for_each_entry(system
, &event_subsystems
, list
) {
696 if (strcmp(system
->name
, name
) == 0)
697 return system
->entry
;
700 /* need to create new entry */
701 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
703 pr_warning("No memory to create event subsystem %s\n",
708 system
->entry
= debugfs_create_dir(name
, d_events
);
709 if (!system
->entry
) {
710 pr_warning("Could not create event subsystem %s\n",
716 system
->name
= kstrdup(name
, GFP_KERNEL
);
718 debugfs_remove(system
->entry
);
723 list_add(&system
->list
, &event_subsystems
);
725 system
->preds
= NULL
;
728 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
729 &ftrace_subsystem_filter_fops
);
731 pr_warning("Could not create debugfs "
732 "'%s/filter' entry\n", name
);
734 return system
->entry
;
738 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
)
740 struct dentry
*entry
;
744 * If the trace point header did not define TRACE_SYSTEM
745 * then the system would be called "TRACE_SYSTEM".
747 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
748 d_events
= event_subsystem_dir(call
->system
, d_events
);
750 if (call
->raw_init
) {
751 ret
= call
->raw_init();
753 pr_warning("Could not initialize trace point"
754 " events/%s\n", call
->name
);
759 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
761 pr_warning("Could not create debugfs "
762 "'%s' directory\n", call
->name
);
767 entry
= trace_create_file("enable", 0644, call
->dir
, call
,
768 &ftrace_enable_fops
);
771 entry
= trace_create_file("id", 0444, call
->dir
, call
,
772 &ftrace_event_id_fops
);
774 if (call
->define_fields
) {
775 ret
= call
->define_fields();
777 pr_warning("Could not initialize trace point"
778 " events/%s\n", call
->name
);
781 entry
= trace_create_file("filter", 0644, call
->dir
, call
,
782 &ftrace_event_filter_fops
);
785 /* A trace may not want to export its format */
786 if (!call
->show_format
)
789 entry
= trace_create_file("format", 0444, call
->dir
, call
,
790 &ftrace_event_format_fops
);
795 #define for_each_event(event, start, end) \
796 for (event = start; \
797 (unsigned long)event < (unsigned long)end; \
800 #ifdef CONFIG_MODULES
801 static void trace_module_add_events(struct module
*mod
)
803 struct ftrace_event_call
*call
, *start
, *end
;
804 struct dentry
*d_events
;
806 start
= mod
->trace_events
;
807 end
= mod
->trace_events
+ mod
->num_trace_events
;
812 d_events
= event_trace_events_dir();
816 for_each_event(call
, start
, end
) {
817 /* The linker may leave blanks */
821 list_add(&call
->list
, &ftrace_events
);
822 event_create_dir(call
, d_events
);
826 static void trace_module_remove_events(struct module
*mod
)
828 struct ftrace_event_call
*call
, *p
;
830 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
831 if (call
->mod
== mod
) {
837 unregister_ftrace_event(call
->event
);
838 debugfs_remove_recursive(call
->dir
);
839 list_del(&call
->list
);
844 static int trace_module_notify(struct notifier_block
*self
,
845 unsigned long val
, void *data
)
847 struct module
*mod
= data
;
849 mutex_lock(&event_mutex
);
851 case MODULE_STATE_COMING
:
852 trace_module_add_events(mod
);
854 case MODULE_STATE_GOING
:
855 trace_module_remove_events(mod
);
858 mutex_unlock(&event_mutex
);
863 static int trace_module_notify(struct notifier_block
*self
,
864 unsigned long val
, void *data
)
868 #endif /* CONFIG_MODULES */
870 struct notifier_block trace_module_nb
= {
871 .notifier_call
= trace_module_notify
,
875 extern struct ftrace_event_call __start_ftrace_events
[];
876 extern struct ftrace_event_call __stop_ftrace_events
[];
878 static __init
int event_trace_init(void)
880 struct ftrace_event_call
*call
;
881 struct dentry
*d_tracer
;
882 struct dentry
*entry
;
883 struct dentry
*d_events
;
886 d_tracer
= tracing_init_dentry();
890 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
891 (void *)&show_event_seq_ops
,
894 pr_warning("Could not create debugfs "
895 "'available_events' entry\n");
897 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
898 (void *)&show_set_event_seq_ops
,
899 &ftrace_set_event_fops
);
901 pr_warning("Could not create debugfs "
902 "'set_event' entry\n");
904 d_events
= event_trace_events_dir();
908 for_each_event(call
, __start_ftrace_events
, __stop_ftrace_events
) {
909 /* The linker may leave blanks */
912 list_add(&call
->list
, &ftrace_events
);
913 event_create_dir(call
, d_events
);
916 ret
= register_module_notifier(&trace_module_nb
);
918 pr_warning("Failed to register trace events module notifier\n");
922 fs_initcall(event_trace_init
);