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/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/delay.h>
20 #include "trace_output.h"
22 #define TRACE_SYSTEM "TRACE_SYSTEM"
24 static DEFINE_MUTEX(event_mutex
);
26 LIST_HEAD(ftrace_events
);
28 int trace_define_field(struct ftrace_event_call
*call
, char *type
,
29 char *name
, int offset
, int size
)
31 struct ftrace_event_field
*field
;
33 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
37 field
->name
= kstrdup(name
, GFP_KERNEL
);
41 field
->type
= kstrdup(type
, GFP_KERNEL
);
45 field
->offset
= offset
;
47 list_add(&field
->link
, &call
->fields
);
60 EXPORT_SYMBOL_GPL(trace_define_field
);
62 static void ftrace_clear_events(void)
64 struct ftrace_event_call
*call
;
66 list_for_each_entry(call
, &ftrace_events
, list
) {
75 static void ftrace_event_enable_disable(struct ftrace_event_call
*call
,
95 static int ftrace_set_clr_event(char *buf
, int set
)
97 struct ftrace_event_call
*call
;
98 char *event
= NULL
, *sub
= NULL
, *match
;
102 * The buf format can be <subsystem>:<event-name>
103 * *:<event-name> means any event by that name.
104 * :<event-name> is the same.
106 * <subsystem>:* means all events in that subsystem
107 * <subsystem>: means the same.
109 * <name> (no ':') means all events in a subsystem with
110 * the name <name> or any event that matches <name>
113 match
= strsep(&buf
, ":");
119 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
121 if (!strlen(event
) || strcmp(event
, "*") == 0)
125 mutex_lock(&event_mutex
);
126 list_for_each_entry(call
, &ftrace_events
, list
) {
128 if (!call
->name
|| !call
->regfunc
)
132 strcmp(match
, call
->name
) != 0 &&
133 strcmp(match
, call
->system
) != 0)
136 if (sub
&& strcmp(sub
, call
->system
) != 0)
139 if (event
&& strcmp(event
, call
->name
) != 0)
142 ftrace_event_enable_disable(call
, set
);
146 mutex_unlock(&event_mutex
);
151 /* 128 should be much more than enough */
152 #define EVENT_BUF_SIZE 127
155 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
156 size_t cnt
, loff_t
*ppos
)
167 ret
= tracing_update_buffers();
171 ret
= get_user(ch
, ubuf
++);
177 /* skip white space */
178 while (cnt
&& isspace(ch
)) {
179 ret
= get_user(ch
, ubuf
++);
186 /* Only white space found? */
193 buf
= kmalloc(EVENT_BUF_SIZE
+1, GFP_KERNEL
);
197 if (cnt
> EVENT_BUF_SIZE
)
198 cnt
= EVENT_BUF_SIZE
;
201 while (cnt
&& !isspace(ch
)) {
207 ret
= get_user(ch
, ubuf
++);
217 ret
= ftrace_set_clr_event(buf
, set
);
230 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
232 struct list_head
*list
= m
->private;
233 struct ftrace_event_call
*call
;
238 if (list
== &ftrace_events
)
241 call
= list_entry(list
, struct ftrace_event_call
, list
);
244 * The ftrace subsystem is for showing formats only.
245 * They can not be enabled or disabled via the event files.
253 m
->private = list
->next
;
258 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
260 return t_next(m
, NULL
, pos
);
264 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
266 struct list_head
*list
= m
->private;
267 struct ftrace_event_call
*call
;
272 if (list
== &ftrace_events
)
275 call
= list_entry(list
, struct ftrace_event_call
, list
);
277 if (!call
->enabled
) {
282 m
->private = list
->next
;
287 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
289 return s_next(m
, NULL
, pos
);
292 static int t_show(struct seq_file
*m
, void *v
)
294 struct ftrace_event_call
*call
= v
;
296 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
297 seq_printf(m
, "%s:", call
->system
);
298 seq_printf(m
, "%s\n", call
->name
);
303 static void t_stop(struct seq_file
*m
, void *p
)
308 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
311 const struct seq_operations
*seq_ops
;
313 if ((file
->f_mode
& FMODE_WRITE
) &&
314 !(file
->f_flags
& O_APPEND
))
315 ftrace_clear_events();
317 seq_ops
= inode
->i_private
;
318 ret
= seq_open(file
, seq_ops
);
320 struct seq_file
*m
= file
->private_data
;
322 m
->private = ftrace_events
.next
;
328 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
331 struct ftrace_event_call
*call
= filp
->private_data
;
339 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
343 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
346 struct ftrace_event_call
*call
= filp
->private_data
;
351 if (cnt
>= sizeof(buf
))
354 if (copy_from_user(&buf
, ubuf
, cnt
))
359 ret
= strict_strtoul(buf
, 10, &val
);
363 ret
= tracing_update_buffers();
370 mutex_lock(&event_mutex
);
371 ftrace_event_enable_disable(call
, val
);
372 mutex_unlock(&event_mutex
);
385 #define FIELD(type, name) \
386 #type, "common_" #name, offsetof(typeof(field), name), \
389 static int trace_write_header(struct trace_seq
*s
)
391 struct trace_entry field
;
393 /* struct trace_entry */
394 return trace_seq_printf(s
,
395 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
396 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
397 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
398 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
399 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
401 FIELD(unsigned char, type
),
402 FIELD(unsigned char, flags
),
403 FIELD(unsigned char, preempt_count
),
409 event_format_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
412 struct ftrace_event_call
*call
= filp
->private_data
;
420 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
426 /* If any of the first writes fail, so will the show_format. */
428 trace_seq_printf(s
, "name: %s\n", call
->name
);
429 trace_seq_printf(s
, "ID: %d\n", call
->id
);
430 trace_seq_printf(s
, "format:\n");
431 trace_write_header(s
);
433 r
= call
->show_format(s
);
436 * ug! The format output is bigger than a PAGE!!
438 buf
= "FORMAT TOO BIG\n";
439 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
444 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
452 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
454 struct ftrace_event_call
*call
= filp
->private_data
;
461 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
466 trace_seq_printf(s
, "%d\n", call
->id
);
468 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
475 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
478 struct ftrace_event_call
*call
= filp
->private_data
;
485 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
491 filter_print_preds(call
->preds
, call
->n_preds
, s
);
492 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
500 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
503 struct ftrace_event_call
*call
= filp
->private_data
;
504 char buf
[64], *pbuf
= buf
;
505 struct filter_pred
*pred
;
508 if (cnt
>= sizeof(buf
))
511 if (copy_from_user(&buf
, ubuf
, cnt
))
515 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
519 err
= filter_parse(&pbuf
, pred
);
521 filter_free_pred(pred
);
526 filter_disable_preds(call
);
527 filter_free_pred(pred
);
531 err
= filter_add_pred(call
, pred
);
533 filter_free_pred(pred
);
537 filter_free_pred(pred
);
545 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
548 struct event_subsystem
*system
= filp
->private_data
;
555 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
561 filter_print_preds(system
->preds
, system
->n_preds
, s
);
562 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
570 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
573 struct event_subsystem
*system
= filp
->private_data
;
574 char buf
[64], *pbuf
= buf
;
575 struct filter_pred
*pred
;
578 if (cnt
>= sizeof(buf
))
581 if (copy_from_user(&buf
, ubuf
, cnt
))
585 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
589 err
= filter_parse(&pbuf
, pred
);
591 filter_free_pred(pred
);
596 filter_free_subsystem_preds(system
);
597 filter_free_pred(pred
);
601 err
= filter_add_subsystem_pred(system
, pred
);
603 filter_free_subsystem_preds(system
);
604 filter_free_pred(pred
);
613 static const struct seq_operations show_event_seq_ops
= {
620 static const struct seq_operations show_set_event_seq_ops
= {
627 static const struct file_operations ftrace_avail_fops
= {
628 .open
= ftrace_event_seq_open
,
631 .release
= seq_release
,
634 static const struct file_operations ftrace_set_event_fops
= {
635 .open
= ftrace_event_seq_open
,
637 .write
= ftrace_event_write
,
639 .release
= seq_release
,
642 static const struct file_operations ftrace_enable_fops
= {
643 .open
= tracing_open_generic
,
644 .read
= event_enable_read
,
645 .write
= event_enable_write
,
648 static const struct file_operations ftrace_event_format_fops
= {
649 .open
= tracing_open_generic
,
650 .read
= event_format_read
,
653 static const struct file_operations ftrace_event_id_fops
= {
654 .open
= tracing_open_generic
,
655 .read
= event_id_read
,
658 static const struct file_operations ftrace_event_filter_fops
= {
659 .open
= tracing_open_generic
,
660 .read
= event_filter_read
,
661 .write
= event_filter_write
,
664 static const struct file_operations ftrace_subsystem_filter_fops
= {
665 .open
= tracing_open_generic
,
666 .read
= subsystem_filter_read
,
667 .write
= subsystem_filter_write
,
670 static struct dentry
*event_trace_events_dir(void)
672 static struct dentry
*d_tracer
;
673 static struct dentry
*d_events
;
678 d_tracer
= tracing_init_dentry();
682 d_events
= debugfs_create_dir("events", d_tracer
);
684 pr_warning("Could not create debugfs "
685 "'events' directory\n");
690 static LIST_HEAD(event_subsystems
);
692 static struct dentry
*
693 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
695 struct event_subsystem
*system
;
696 struct dentry
*entry
;
698 /* First see if we did not already create this dir */
699 list_for_each_entry(system
, &event_subsystems
, list
) {
700 if (strcmp(system
->name
, name
) == 0)
701 return system
->entry
;
704 /* need to create new entry */
705 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
707 pr_warning("No memory to create event subsystem %s\n",
712 system
->entry
= debugfs_create_dir(name
, d_events
);
713 if (!system
->entry
) {
714 pr_warning("Could not create event subsystem %s\n",
720 system
->name
= kstrdup(name
, GFP_KERNEL
);
722 debugfs_remove(system
->entry
);
727 list_add(&system
->list
, &event_subsystems
);
729 system
->preds
= NULL
;
732 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
733 &ftrace_subsystem_filter_fops
);
735 pr_warning("Could not create debugfs "
736 "'%s/filter' entry\n", name
);
738 return system
->entry
;
742 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
)
744 struct dentry
*entry
;
748 * If the trace point header did not define TRACE_SYSTEM
749 * then the system would be called "TRACE_SYSTEM".
751 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
752 d_events
= event_subsystem_dir(call
->system
, d_events
);
754 if (call
->raw_init
) {
755 ret
= call
->raw_init();
757 pr_warning("Could not initialize trace point"
758 " events/%s\n", call
->name
);
763 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
765 pr_warning("Could not create debugfs "
766 "'%s' directory\n", call
->name
);
771 entry
= trace_create_file("enable", 0644, call
->dir
, call
,
772 &ftrace_enable_fops
);
775 entry
= trace_create_file("id", 0444, call
->dir
, call
,
776 &ftrace_event_id_fops
);
778 if (call
->define_fields
) {
779 ret
= call
->define_fields();
781 pr_warning("Could not initialize trace point"
782 " events/%s\n", call
->name
);
785 entry
= trace_create_file("filter", 0644, call
->dir
, call
,
786 &ftrace_event_filter_fops
);
789 /* A trace may not want to export its format */
790 if (!call
->show_format
)
793 entry
= trace_create_file("format", 0444, call
->dir
, call
,
794 &ftrace_event_format_fops
);
799 #define for_each_event(event, start, end) \
800 for (event = start; \
801 (unsigned long)event < (unsigned long)end; \
804 #ifdef CONFIG_MODULES
805 static void trace_module_add_events(struct module
*mod
)
807 struct ftrace_event_call
*call
, *start
, *end
;
808 struct dentry
*d_events
;
810 start
= mod
->trace_events
;
811 end
= mod
->trace_events
+ mod
->num_trace_events
;
816 d_events
= event_trace_events_dir();
820 for_each_event(call
, start
, end
) {
821 /* The linker may leave blanks */
825 list_add(&call
->list
, &ftrace_events
);
826 event_create_dir(call
, d_events
);
830 static void trace_module_remove_events(struct module
*mod
)
832 struct ftrace_event_call
*call
, *p
;
834 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
835 if (call
->mod
== mod
) {
841 unregister_ftrace_event(call
->event
);
842 debugfs_remove_recursive(call
->dir
);
843 list_del(&call
->list
);
848 static int trace_module_notify(struct notifier_block
*self
,
849 unsigned long val
, void *data
)
851 struct module
*mod
= data
;
853 mutex_lock(&event_mutex
);
855 case MODULE_STATE_COMING
:
856 trace_module_add_events(mod
);
858 case MODULE_STATE_GOING
:
859 trace_module_remove_events(mod
);
862 mutex_unlock(&event_mutex
);
867 static int trace_module_notify(struct notifier_block
*self
,
868 unsigned long val
, void *data
)
872 #endif /* CONFIG_MODULES */
874 struct notifier_block trace_module_nb
= {
875 .notifier_call
= trace_module_notify
,
879 extern struct ftrace_event_call __start_ftrace_events
[];
880 extern struct ftrace_event_call __stop_ftrace_events
[];
882 static __init
int event_trace_init(void)
884 struct ftrace_event_call
*call
;
885 struct dentry
*d_tracer
;
886 struct dentry
*entry
;
887 struct dentry
*d_events
;
890 d_tracer
= tracing_init_dentry();
894 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
895 (void *)&show_event_seq_ops
,
898 pr_warning("Could not create debugfs "
899 "'available_events' entry\n");
901 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
902 (void *)&show_set_event_seq_ops
,
903 &ftrace_set_event_fops
);
905 pr_warning("Could not create debugfs "
906 "'set_event' entry\n");
908 d_events
= event_trace_events_dir();
912 for_each_event(call
, __start_ftrace_events
, __stop_ftrace_events
) {
913 /* The linker may leave blanks */
916 list_add(&call
->list
, &ftrace_events
);
917 event_create_dir(call
, d_events
);
920 ret
= register_module_notifier(&trace_module_nb
);
922 pr_warning("Failed to register trace events module notifier\n");
926 fs_initcall(event_trace_init
);
928 #ifdef CONFIG_FTRACE_STARTUP_TEST
930 static DEFINE_SPINLOCK(test_spinlock
);
931 static DEFINE_SPINLOCK(test_spinlock_irq
);
932 static DEFINE_MUTEX(test_mutex
);
934 static __init
void test_work(struct work_struct
*dummy
)
936 spin_lock(&test_spinlock
);
937 spin_lock_irq(&test_spinlock_irq
);
939 spin_unlock_irq(&test_spinlock_irq
);
940 spin_unlock(&test_spinlock
);
942 mutex_lock(&test_mutex
);
944 mutex_unlock(&test_mutex
);
947 static __init
int event_test_thread(void *unused
)
951 test_malloc
= kmalloc(1234, GFP_KERNEL
);
953 pr_info("failed to kmalloc\n");
955 schedule_on_each_cpu(test_work
);
959 set_current_state(TASK_INTERRUPTIBLE
);
960 while (!kthread_should_stop())
967 * Do various things that may trigger events.
969 static __init
void event_test_stuff(void)
971 struct task_struct
*test_thread
;
973 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
975 kthread_stop(test_thread
);
979 * For every trace event defined, we will test each trace point separately,
980 * and then by groups, and finally all trace points.
982 static __init
int event_trace_self_tests(void)
984 struct ftrace_event_call
*call
;
985 struct event_subsystem
*system
;
989 pr_info("Running tests on trace events:\n");
991 list_for_each_entry(call
, &ftrace_events
, list
) {
993 /* Only test those that have a regfunc */
997 pr_info("Testing event %s: ", call
->name
);
1000 * If an event is already enabled, someone is using
1001 * it and the self test should not be on.
1003 if (call
->enabled
) {
1004 pr_warning("Enabled event during self test!\n");
1020 /* Now test at the sub system level */
1022 pr_info("Running tests on trace event systems:\n");
1024 list_for_each_entry(system
, &event_subsystems
, list
) {
1026 /* the ftrace system is special, skip it */
1027 if (strcmp(system
->name
, "ftrace") == 0)
1030 pr_info("Testing event system %s: ", system
->name
);
1032 /* ftrace_set_clr_event can modify the name passed in. */
1033 sysname
= kstrdup(system
->name
, GFP_KERNEL
);
1034 if (WARN_ON(!sysname
)) {
1035 pr_warning("Can't allocate memory, giving up!\n");
1038 ret
= ftrace_set_clr_event(sysname
, 1);
1040 if (WARN_ON_ONCE(ret
)) {
1041 pr_warning("error enabling system %s\n",
1048 sysname
= kstrdup(system
->name
, GFP_KERNEL
);
1049 if (WARN_ON(!sysname
)) {
1050 pr_warning("Can't allocate memory, giving up!\n");
1053 ret
= ftrace_set_clr_event(sysname
, 0);
1056 if (WARN_ON_ONCE(ret
))
1057 pr_warning("error disabling system %s\n",
1063 /* Test with all events enabled */
1065 pr_info("Running tests on all trace events:\n");
1066 pr_info("Testing all events: ");
1068 sysname
= kmalloc(4, GFP_KERNEL
);
1069 if (WARN_ON(!sysname
)) {
1070 pr_warning("Can't allocate memory, giving up!\n");
1073 memcpy(sysname
, "*:*", 4);
1074 ret
= ftrace_set_clr_event(sysname
, 1);
1075 if (WARN_ON_ONCE(ret
)) {
1077 pr_warning("error enabling all events\n");
1084 memcpy(sysname
, "*:*", 4);
1085 ret
= ftrace_set_clr_event(sysname
, 0);
1088 if (WARN_ON_ONCE(ret
)) {
1089 pr_warning("error disabling all events\n");
1098 late_initcall(event_trace_self_tests
);