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 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
, int is_signed
)
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 field
->is_signed
= is_signed
;
48 list_add(&field
->link
, &call
->fields
);
61 EXPORT_SYMBOL_GPL(trace_define_field
);
65 static void trace_destroy_fields(struct ftrace_event_call
*call
)
67 struct ftrace_event_field
*field
, *next
;
69 list_for_each_entry_safe(field
, next
, &call
->fields
, link
) {
70 list_del(&field
->link
);
77 #endif /* CONFIG_MODULES */
79 static void ftrace_clear_events(void)
81 struct ftrace_event_call
*call
;
83 mutex_lock(&event_mutex
);
84 list_for_each_entry(call
, &ftrace_events
, list
) {
91 mutex_unlock(&event_mutex
);
94 static void ftrace_event_enable_disable(struct ftrace_event_call
*call
,
106 if (!call
->enabled
) {
115 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
117 static int __ftrace_set_clr_event(const char *match
, const char *sub
,
118 const char *event
, int set
)
120 struct ftrace_event_call
*call
;
123 mutex_lock(&event_mutex
);
124 list_for_each_entry(call
, &ftrace_events
, list
) {
126 if (!call
->name
|| !call
->regfunc
)
130 strcmp(match
, call
->name
) != 0 &&
131 strcmp(match
, call
->system
) != 0)
134 if (sub
&& strcmp(sub
, call
->system
) != 0)
137 if (event
&& strcmp(event
, call
->name
) != 0)
140 ftrace_event_enable_disable(call
, set
);
144 mutex_unlock(&event_mutex
);
149 static int ftrace_set_clr_event(char *buf
, int set
)
151 char *event
= NULL
, *sub
= NULL
, *match
;
154 * The buf format can be <subsystem>:<event-name>
155 * *:<event-name> means any event by that name.
156 * :<event-name> is the same.
158 * <subsystem>:* means all events in that subsystem
159 * <subsystem>: means the same.
161 * <name> (no ':') means all events in a subsystem with
162 * the name <name> or any event that matches <name>
165 match
= strsep(&buf
, ":");
171 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
173 if (!strlen(event
) || strcmp(event
, "*") == 0)
177 return __ftrace_set_clr_event(match
, sub
, event
, set
);
181 * trace_set_clr_event - enable or disable an event
182 * @system: system name to match (NULL for any system)
183 * @event: event name to match (NULL for all events, within system)
184 * @set: 1 to enable, 0 to disable
186 * This is a way for other parts of the kernel to enable or disable
189 * Returns 0 on success, -EINVAL if the parameters do not match any
192 int trace_set_clr_event(const char *system
, const char *event
, int set
)
194 return __ftrace_set_clr_event(NULL
, system
, event
, set
);
197 /* 128 should be much more than enough */
198 #define EVENT_BUF_SIZE 127
201 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
202 size_t cnt
, loff_t
*ppos
)
213 ret
= tracing_update_buffers();
217 ret
= get_user(ch
, ubuf
++);
223 /* skip white space */
224 while (cnt
&& isspace(ch
)) {
225 ret
= get_user(ch
, ubuf
++);
232 /* Only white space found? */
239 buf
= kmalloc(EVENT_BUF_SIZE
+1, GFP_KERNEL
);
243 if (cnt
> EVENT_BUF_SIZE
)
244 cnt
= EVENT_BUF_SIZE
;
247 while (cnt
&& !isspace(ch
)) {
253 ret
= get_user(ch
, ubuf
++);
263 ret
= ftrace_set_clr_event(buf
, set
);
276 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
278 struct list_head
*list
= m
->private;
279 struct ftrace_event_call
*call
;
284 if (list
== &ftrace_events
)
287 call
= list_entry(list
, struct ftrace_event_call
, list
);
290 * The ftrace subsystem is for showing formats only.
291 * They can not be enabled or disabled via the event files.
299 m
->private = list
->next
;
304 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
306 mutex_lock(&event_mutex
);
308 m
->private = ftrace_events
.next
;
309 return t_next(m
, NULL
, pos
);
313 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
315 struct list_head
*list
= m
->private;
316 struct ftrace_event_call
*call
;
321 if (list
== &ftrace_events
)
324 call
= list_entry(list
, struct ftrace_event_call
, list
);
326 if (!call
->enabled
) {
331 m
->private = list
->next
;
336 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
338 mutex_lock(&event_mutex
);
340 m
->private = ftrace_events
.next
;
341 return s_next(m
, NULL
, pos
);
344 static int t_show(struct seq_file
*m
, void *v
)
346 struct ftrace_event_call
*call
= v
;
348 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
349 seq_printf(m
, "%s:", call
->system
);
350 seq_printf(m
, "%s\n", call
->name
);
355 static void t_stop(struct seq_file
*m
, void *p
)
357 mutex_unlock(&event_mutex
);
361 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
363 const struct seq_operations
*seq_ops
;
365 if ((file
->f_mode
& FMODE_WRITE
) &&
366 !(file
->f_flags
& O_APPEND
))
367 ftrace_clear_events();
369 seq_ops
= inode
->i_private
;
370 return seq_open(file
, seq_ops
);
374 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
377 struct ftrace_event_call
*call
= filp
->private_data
;
385 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
389 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
392 struct ftrace_event_call
*call
= filp
->private_data
;
397 if (cnt
>= sizeof(buf
))
400 if (copy_from_user(&buf
, ubuf
, cnt
))
405 ret
= strict_strtoul(buf
, 10, &val
);
409 ret
= tracing_update_buffers();
416 mutex_lock(&event_mutex
);
417 ftrace_event_enable_disable(call
, val
);
418 mutex_unlock(&event_mutex
);
431 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
434 const char set_to_char
[4] = { '?', '0', '1', 'X' };
435 const char *system
= filp
->private_data
;
436 struct ftrace_event_call
*call
;
441 mutex_lock(&event_mutex
);
442 list_for_each_entry(call
, &ftrace_events
, list
) {
443 if (!call
->name
|| !call
->regfunc
)
446 if (system
&& strcmp(call
->system
, system
) != 0)
450 * We need to find out if all the events are set
451 * or if all events or cleared, or if we have
454 set
|= (1 << !!call
->enabled
);
457 * If we have a mixture, no need to look further.
462 mutex_unlock(&event_mutex
);
464 buf
[0] = set_to_char
[set
];
467 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
473 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
476 const char *system
= filp
->private_data
;
481 if (cnt
>= sizeof(buf
))
484 if (copy_from_user(&buf
, ubuf
, cnt
))
489 ret
= strict_strtoul(buf
, 10, &val
);
493 ret
= tracing_update_buffers();
497 if (val
!= 0 && val
!= 1)
500 ret
= __ftrace_set_clr_event(NULL
, system
, NULL
, val
);
512 extern char *__bad_type_size(void);
515 #define FIELD(type, name) \
516 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
517 #type, "common_" #name, offsetof(typeof(field), name), \
520 static int trace_write_header(struct trace_seq
*s
)
522 struct trace_entry field
;
524 /* struct trace_entry */
525 return trace_seq_printf(s
,
526 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
527 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
528 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
529 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
530 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
532 FIELD(unsigned short, type
),
533 FIELD(unsigned char, flags
),
534 FIELD(unsigned char, preempt_count
),
540 event_format_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
543 struct ftrace_event_call
*call
= filp
->private_data
;
551 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
557 /* If any of the first writes fail, so will the show_format. */
559 trace_seq_printf(s
, "name: %s\n", call
->name
);
560 trace_seq_printf(s
, "ID: %d\n", call
->id
);
561 trace_seq_printf(s
, "format:\n");
562 trace_write_header(s
);
564 r
= call
->show_format(s
);
567 * ug! The format output is bigger than a PAGE!!
569 buf
= "FORMAT TOO BIG\n";
570 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
575 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
583 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
585 struct ftrace_event_call
*call
= filp
->private_data
;
592 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
597 trace_seq_printf(s
, "%d\n", call
->id
);
599 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
606 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
609 struct ftrace_event_call
*call
= filp
->private_data
;
616 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
622 print_event_filter(call
, s
);
623 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
631 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
634 struct ftrace_event_call
*call
= filp
->private_data
;
638 if (cnt
>= PAGE_SIZE
)
641 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
645 if (copy_from_user(buf
, ubuf
, cnt
)) {
646 free_page((unsigned long) buf
);
651 err
= apply_event_filter(call
, buf
);
652 free_page((unsigned long) buf
);
662 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
665 struct event_subsystem
*system
= filp
->private_data
;
672 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
678 print_subsystem_event_filter(system
, s
);
679 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
687 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
690 struct event_subsystem
*system
= filp
->private_data
;
694 if (cnt
>= PAGE_SIZE
)
697 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
701 if (copy_from_user(buf
, ubuf
, cnt
)) {
702 free_page((unsigned long) buf
);
707 err
= apply_subsystem_event_filter(system
, buf
);
708 free_page((unsigned long) buf
);
718 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
720 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
727 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
734 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
741 static const struct seq_operations show_event_seq_ops
= {
748 static const struct seq_operations show_set_event_seq_ops
= {
755 static const struct file_operations ftrace_avail_fops
= {
756 .open
= ftrace_event_seq_open
,
759 .release
= seq_release
,
762 static const struct file_operations ftrace_set_event_fops
= {
763 .open
= ftrace_event_seq_open
,
765 .write
= ftrace_event_write
,
767 .release
= seq_release
,
770 static const struct file_operations ftrace_enable_fops
= {
771 .open
= tracing_open_generic
,
772 .read
= event_enable_read
,
773 .write
= event_enable_write
,
776 static const struct file_operations ftrace_event_format_fops
= {
777 .open
= tracing_open_generic
,
778 .read
= event_format_read
,
781 static const struct file_operations ftrace_event_id_fops
= {
782 .open
= tracing_open_generic
,
783 .read
= event_id_read
,
786 static const struct file_operations ftrace_event_filter_fops
= {
787 .open
= tracing_open_generic
,
788 .read
= event_filter_read
,
789 .write
= event_filter_write
,
792 static const struct file_operations ftrace_subsystem_filter_fops
= {
793 .open
= tracing_open_generic
,
794 .read
= subsystem_filter_read
,
795 .write
= subsystem_filter_write
,
798 static const struct file_operations ftrace_system_enable_fops
= {
799 .open
= tracing_open_generic
,
800 .read
= system_enable_read
,
801 .write
= system_enable_write
,
804 static const struct file_operations ftrace_show_header_fops
= {
805 .open
= tracing_open_generic
,
809 static struct dentry
*event_trace_events_dir(void)
811 static struct dentry
*d_tracer
;
812 static struct dentry
*d_events
;
817 d_tracer
= tracing_init_dentry();
821 d_events
= debugfs_create_dir("events", d_tracer
);
823 pr_warning("Could not create debugfs "
824 "'events' directory\n");
829 static LIST_HEAD(event_subsystems
);
831 static struct dentry
*
832 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
834 struct event_subsystem
*system
;
835 struct dentry
*entry
;
837 /* First see if we did not already create this dir */
838 list_for_each_entry(system
, &event_subsystems
, list
) {
839 if (strcmp(system
->name
, name
) == 0)
840 return system
->entry
;
843 /* need to create new entry */
844 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
846 pr_warning("No memory to create event subsystem %s\n",
851 system
->entry
= debugfs_create_dir(name
, d_events
);
852 if (!system
->entry
) {
853 pr_warning("Could not create event subsystem %s\n",
859 system
->name
= kstrdup(name
, GFP_KERNEL
);
861 debugfs_remove(system
->entry
);
866 list_add(&system
->list
, &event_subsystems
);
868 system
->filter
= NULL
;
870 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
871 if (!system
->filter
) {
872 pr_warning("Could not allocate filter for subsystem "
874 return system
->entry
;
877 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
878 &ftrace_subsystem_filter_fops
);
880 kfree(system
->filter
);
881 system
->filter
= NULL
;
882 pr_warning("Could not create debugfs "
883 "'%s/filter' entry\n", name
);
886 entry
= trace_create_file("enable", 0644, system
->entry
,
887 (void *)system
->name
,
888 &ftrace_system_enable_fops
);
890 return system
->entry
;
894 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
,
895 const struct file_operations
*id
,
896 const struct file_operations
*enable
,
897 const struct file_operations
*filter
,
898 const struct file_operations
*format
)
900 struct dentry
*entry
;
904 * If the trace point header did not define TRACE_SYSTEM
905 * then the system would be called "TRACE_SYSTEM".
907 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
908 d_events
= event_subsystem_dir(call
->system
, d_events
);
910 if (call
->raw_init
) {
911 ret
= call
->raw_init();
913 pr_warning("Could not initialize trace point"
914 " events/%s\n", call
->name
);
919 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
921 pr_warning("Could not create debugfs "
922 "'%s' directory\n", call
->name
);
927 entry
= trace_create_file("enable", 0644, call
->dir
, call
,
931 entry
= trace_create_file("id", 0444, call
->dir
, call
,
934 if (call
->define_fields
) {
935 ret
= call
->define_fields();
937 pr_warning("Could not initialize trace point"
938 " events/%s\n", call
->name
);
941 entry
= trace_create_file("filter", 0644, call
->dir
, call
,
945 /* A trace may not want to export its format */
946 if (!call
->show_format
)
949 entry
= trace_create_file("format", 0444, call
->dir
, call
,
955 #define for_each_event(event, start, end) \
956 for (event = start; \
957 (unsigned long)event < (unsigned long)end; \
960 #ifdef CONFIG_MODULES
962 static LIST_HEAD(ftrace_module_file_list
);
965 * Modules must own their file_operations to keep up with
966 * reference counting.
968 struct ftrace_module_file_ops
{
969 struct list_head list
;
971 struct file_operations id
;
972 struct file_operations enable
;
973 struct file_operations format
;
974 struct file_operations filter
;
977 static struct ftrace_module_file_ops
*
978 trace_create_file_ops(struct module
*mod
)
980 struct ftrace_module_file_ops
*file_ops
;
983 * This is a bit of a PITA. To allow for correct reference
984 * counting, modules must "own" their file_operations.
985 * To do this, we allocate the file operations that will be
986 * used in the event directory.
989 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
995 file_ops
->id
= ftrace_event_id_fops
;
996 file_ops
->id
.owner
= mod
;
998 file_ops
->enable
= ftrace_enable_fops
;
999 file_ops
->enable
.owner
= mod
;
1001 file_ops
->filter
= ftrace_event_filter_fops
;
1002 file_ops
->filter
.owner
= mod
;
1004 file_ops
->format
= ftrace_event_format_fops
;
1005 file_ops
->format
.owner
= mod
;
1007 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1012 static void trace_module_add_events(struct module
*mod
)
1014 struct ftrace_module_file_ops
*file_ops
= NULL
;
1015 struct ftrace_event_call
*call
, *start
, *end
;
1016 struct dentry
*d_events
;
1018 start
= mod
->trace_events
;
1019 end
= mod
->trace_events
+ mod
->num_trace_events
;
1024 d_events
= event_trace_events_dir();
1028 for_each_event(call
, start
, end
) {
1029 /* The linker may leave blanks */
1034 * This module has events, create file ops for this module
1035 * if not already done.
1038 file_ops
= trace_create_file_ops(mod
);
1043 list_add(&call
->list
, &ftrace_events
);
1044 event_create_dir(call
, d_events
,
1045 &file_ops
->id
, &file_ops
->enable
,
1046 &file_ops
->filter
, &file_ops
->format
);
1050 static void trace_module_remove_events(struct module
*mod
)
1052 struct ftrace_module_file_ops
*file_ops
;
1053 struct ftrace_event_call
*call
, *p
;
1056 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1057 if (call
->mod
== mod
) {
1059 if (call
->enabled
) {
1064 unregister_ftrace_event(call
->event
);
1065 debugfs_remove_recursive(call
->dir
);
1066 list_del(&call
->list
);
1067 trace_destroy_fields(call
);
1068 destroy_preds(call
);
1072 /* Now free the file_operations */
1073 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1074 if (file_ops
->mod
== mod
)
1077 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1078 list_del(&file_ops
->list
);
1083 * It is safest to reset the ring buffer if the module being unloaded
1084 * registered any events.
1087 tracing_reset_current_online_cpus();
1090 static int trace_module_notify(struct notifier_block
*self
,
1091 unsigned long val
, void *data
)
1093 struct module
*mod
= data
;
1095 mutex_lock(&event_mutex
);
1097 case MODULE_STATE_COMING
:
1098 trace_module_add_events(mod
);
1100 case MODULE_STATE_GOING
:
1101 trace_module_remove_events(mod
);
1104 mutex_unlock(&event_mutex
);
1109 static int trace_module_notify(struct notifier_block
*self
,
1110 unsigned long val
, void *data
)
1114 #endif /* CONFIG_MODULES */
1116 struct notifier_block trace_module_nb
= {
1117 .notifier_call
= trace_module_notify
,
1121 extern struct ftrace_event_call __start_ftrace_events
[];
1122 extern struct ftrace_event_call __stop_ftrace_events
[];
1124 static __init
int event_trace_init(void)
1126 struct ftrace_event_call
*call
;
1127 struct dentry
*d_tracer
;
1128 struct dentry
*entry
;
1129 struct dentry
*d_events
;
1132 d_tracer
= tracing_init_dentry();
1136 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
1137 (void *)&show_event_seq_ops
,
1138 &ftrace_avail_fops
);
1140 pr_warning("Could not create debugfs "
1141 "'available_events' entry\n");
1143 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
1144 (void *)&show_set_event_seq_ops
,
1145 &ftrace_set_event_fops
);
1147 pr_warning("Could not create debugfs "
1148 "'set_event' entry\n");
1150 d_events
= event_trace_events_dir();
1154 /* ring buffer internal formats */
1155 trace_create_file("header_page", 0444, d_events
,
1156 ring_buffer_print_page_header
,
1157 &ftrace_show_header_fops
);
1159 trace_create_file("header_event", 0444, d_events
,
1160 ring_buffer_print_entry_header
,
1161 &ftrace_show_header_fops
);
1163 trace_create_file("enable", 0644, d_events
,
1164 NULL
, &ftrace_system_enable_fops
);
1166 for_each_event(call
, __start_ftrace_events
, __stop_ftrace_events
) {
1167 /* The linker may leave blanks */
1170 list_add(&call
->list
, &ftrace_events
);
1171 event_create_dir(call
, d_events
, &ftrace_event_id_fops
,
1172 &ftrace_enable_fops
, &ftrace_event_filter_fops
,
1173 &ftrace_event_format_fops
);
1176 ret
= register_module_notifier(&trace_module_nb
);
1178 pr_warning("Failed to register trace events module notifier\n");
1182 fs_initcall(event_trace_init
);
1184 #ifdef CONFIG_FTRACE_STARTUP_TEST
1186 static DEFINE_SPINLOCK(test_spinlock
);
1187 static DEFINE_SPINLOCK(test_spinlock_irq
);
1188 static DEFINE_MUTEX(test_mutex
);
1190 static __init
void test_work(struct work_struct
*dummy
)
1192 spin_lock(&test_spinlock
);
1193 spin_lock_irq(&test_spinlock_irq
);
1195 spin_unlock_irq(&test_spinlock_irq
);
1196 spin_unlock(&test_spinlock
);
1198 mutex_lock(&test_mutex
);
1200 mutex_unlock(&test_mutex
);
1203 static __init
int event_test_thread(void *unused
)
1207 test_malloc
= kmalloc(1234, GFP_KERNEL
);
1209 pr_info("failed to kmalloc\n");
1211 schedule_on_each_cpu(test_work
);
1215 set_current_state(TASK_INTERRUPTIBLE
);
1216 while (!kthread_should_stop())
1223 * Do various things that may trigger events.
1225 static __init
void event_test_stuff(void)
1227 struct task_struct
*test_thread
;
1229 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
1231 kthread_stop(test_thread
);
1235 * For every trace event defined, we will test each trace point separately,
1236 * and then by groups, and finally all trace points.
1238 static __init
void event_trace_self_tests(void)
1240 struct ftrace_event_call
*call
;
1241 struct event_subsystem
*system
;
1244 pr_info("Running tests on trace events:\n");
1246 list_for_each_entry(call
, &ftrace_events
, list
) {
1248 /* Only test those that have a regfunc */
1252 pr_info("Testing event %s: ", call
->name
);
1255 * If an event is already enabled, someone is using
1256 * it and the self test should not be on.
1258 if (call
->enabled
) {
1259 pr_warning("Enabled event during self test!\n");
1275 /* Now test at the sub system level */
1277 pr_info("Running tests on trace event systems:\n");
1279 list_for_each_entry(system
, &event_subsystems
, list
) {
1281 /* the ftrace system is special, skip it */
1282 if (strcmp(system
->name
, "ftrace") == 0)
1285 pr_info("Testing event system %s: ", system
->name
);
1287 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 1);
1288 if (WARN_ON_ONCE(ret
)) {
1289 pr_warning("error enabling system %s\n",
1296 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 0);
1297 if (WARN_ON_ONCE(ret
))
1298 pr_warning("error disabling system %s\n",
1304 /* Test with all events enabled */
1306 pr_info("Running tests on all trace events:\n");
1307 pr_info("Testing all events: ");
1309 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 1);
1310 if (WARN_ON_ONCE(ret
)) {
1311 pr_warning("error enabling all events\n");
1318 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 0);
1319 if (WARN_ON_ONCE(ret
)) {
1320 pr_warning("error disabling all events\n");
1327 #ifdef CONFIG_FUNCTION_TRACER
1329 static DEFINE_PER_CPU(atomic_t
, test_event_disable
);
1332 function_test_events_call(unsigned long ip
, unsigned long parent_ip
)
1334 struct ring_buffer_event
*event
;
1335 struct ftrace_entry
*entry
;
1336 unsigned long flags
;
1342 pc
= preempt_count();
1343 resched
= ftrace_preempt_disable();
1344 cpu
= raw_smp_processor_id();
1345 disabled
= atomic_inc_return(&per_cpu(test_event_disable
, cpu
));
1350 local_save_flags(flags
);
1352 event
= trace_current_buffer_lock_reserve(TRACE_FN
, sizeof(*entry
),
1356 entry
= ring_buffer_event_data(event
);
1358 entry
->parent_ip
= parent_ip
;
1360 trace_nowake_buffer_unlock_commit(event
, flags
, pc
);
1363 atomic_dec(&per_cpu(test_event_disable
, cpu
));
1364 ftrace_preempt_enable(resched
);
1367 static struct ftrace_ops trace_ops __initdata
=
1369 .func
= function_test_events_call
,
1372 static __init
void event_trace_self_test_with_function(void)
1374 register_ftrace_function(&trace_ops
);
1375 pr_info("Running tests again, along with the function tracer\n");
1376 event_trace_self_tests();
1377 unregister_ftrace_function(&trace_ops
);
1380 static __init
void event_trace_self_test_with_function(void)
1385 static __init
int event_trace_self_tests_init(void)
1388 event_trace_self_tests();
1390 event_trace_self_test_with_function();
1395 late_initcall(event_trace_self_tests_init
);