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 <asm/setup.h>
22 #include "trace_output.h"
25 #define TRACE_SYSTEM "TRACE_SYSTEM"
27 DEFINE_MUTEX(event_mutex);
29 LIST_HEAD(ftrace_events);
31 int trace_define_field(struct ftrace_event_call *call, const char *type,
32 const char *name, int offset, int size, int is_signed,
35 struct ftrace_event_field *field;
37 field = kzalloc(sizeof(*field), GFP_KERNEL);
41 field->name = kstrdup(name, GFP_KERNEL);
45 field->type = kstrdup(type, GFP_KERNEL);
49 if (filter_type == FILTER_OTHER)
50 field->filter_type = filter_assign_type(type);
52 field->filter_type = filter_type;
54 field->offset = offset;
56 field->is_signed = is_signed;
58 list_add(&field->link, &call->fields);
71 EXPORT_SYMBOL_GPL(trace_define_field);
73 #define __common_field(type, item) \
74 ret = trace_define_field(call, #type, "common_" #item, \
75 offsetof(typeof(ent), item), \
77 is_signed_type(type), FILTER_OTHER); \
81 int trace_define_common_fields(struct ftrace_event_call *call)
84 struct trace_entry ent;
86 __common_field(unsigned short, type);
87 __common_field(unsigned char, flags);
88 __common_field(unsigned char, preempt_count);
89 __common_field(int, pid);
90 __common_field(int, lock_depth);
94 EXPORT_SYMBOL_GPL(trace_define_common_fields);
98 static void trace_destroy_fields(struct ftrace_event_call *call)
100 struct ftrace_event_field *field, *next;
102 list_for_each_entry_safe(field, next, &call->fields, link) {
103 list_del(&field->link);
110 #endif /* CONFIG_MODULES */
112 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
119 tracing_stop_cmdline_record();
120 call->unregfunc(call->data);
124 if (!call->enabled) {
126 tracing_start_cmdline_record();
127 call->regfunc(call->data);
133 static void ftrace_clear_events(void)
135 struct ftrace_event_call *call;
137 mutex_lock(&event_mutex);
138 list_for_each_entry(call, &ftrace_events, list) {
139 ftrace_event_enable_disable(call, 0);
141 mutex_unlock(&event_mutex);
145 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
147 static int __ftrace_set_clr_event(const char *match, const char *sub,
148 const char *event, int set)
150 struct ftrace_event_call *call;
153 mutex_lock(&event_mutex);
154 list_for_each_entry(call, &ftrace_events, list) {
156 if (!call->name || !call->regfunc)
160 strcmp(match, call->name) != 0 &&
161 strcmp(match, call->system) != 0)
164 if (sub && strcmp(sub, call->system) != 0)
167 if (event && strcmp(event, call->name) != 0)
170 ftrace_event_enable_disable(call, set);
174 mutex_unlock(&event_mutex);
179 static int ftrace_set_clr_event(char *buf, int set)
181 char *event = NULL, *sub = NULL, *match;
184 * The buf format can be <subsystem>:<event-name>
185 * *:<event-name> means any event by that name.
186 * :<event-name> is the same.
188 * <subsystem>:* means all events in that subsystem
189 * <subsystem>: means the same.
191 * <name> (no ':') means all events in a subsystem with
192 * the name <name> or any event that matches <name>
195 match = strsep(&buf, ":");
201 if (!strlen(sub) || strcmp(sub, "*") == 0)
203 if (!strlen(event) || strcmp(event, "*") == 0)
207 return __ftrace_set_clr_event(match, sub, event, set);
211 * trace_set_clr_event - enable or disable an event
212 * @system: system name to match (NULL for any system)
213 * @event: event name to match (NULL for all events, within system)
214 * @set: 1 to enable, 0 to disable
216 * This is a way for other parts of the kernel to enable or disable
219 * Returns 0 on success, -EINVAL if the parameters do not match any
222 int trace_set_clr_event(const char *system, const char *event, int set)
224 return __ftrace_set_clr_event(NULL, system, event, set);
227 /* 128 should be much more than enough */
228 #define EVENT_BUF_SIZE 127
231 ftrace_event_write(struct file *file, const char __user *ubuf,
232 size_t cnt, loff_t *ppos)
234 struct trace_parser parser;
241 ret = tracing_update_buffers();
245 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
248 read = trace_get_user(&parser, ubuf, cnt, ppos);
250 if (trace_parser_loaded((&parser))) {
253 if (*parser.buffer == '!')
256 parser.buffer[parser.idx] = 0;
258 ret = ftrace_set_clr_event(parser.buffer + !set, set);
266 trace_parser_put(&parser);
272 t_next(struct seq_file *m, void *v, loff_t *pos)
274 struct ftrace_event_call *call = v;
278 list_for_each_entry_continue(call, &ftrace_events, list) {
280 * The ftrace subsystem is for showing formats only.
281 * They can not be enabled or disabled via the event files.
290 static void *t_start(struct seq_file *m, loff_t *pos)
292 struct ftrace_event_call *call;
295 mutex_lock(&event_mutex);
297 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
298 for (l = 0; l <= *pos; ) {
299 call = t_next(m, call, &l);
307 s_next(struct seq_file *m, void *v, loff_t *pos)
309 struct ftrace_event_call *call = v;
313 list_for_each_entry_continue(call, &ftrace_events, list) {
321 static void *s_start(struct seq_file *m, loff_t *pos)
323 struct ftrace_event_call *call;
326 mutex_lock(&event_mutex);
328 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
329 for (l = 0; l <= *pos; ) {
330 call = s_next(m, call, &l);
337 static int t_show(struct seq_file *m, void *v)
339 struct ftrace_event_call *call = v;
341 if (strcmp(call->system, TRACE_SYSTEM) != 0)
342 seq_printf(m, "%s:", call->system);
343 seq_printf(m, "%s\n", call->name);
348 static void t_stop(struct seq_file *m, void *p)
350 mutex_unlock(&event_mutex);
354 ftrace_event_seq_open(struct inode *inode, struct file *file)
356 const struct seq_operations *seq_ops;
358 if ((file->f_mode & FMODE_WRITE) &&
359 (file->f_flags & O_TRUNC))
360 ftrace_clear_events();
362 seq_ops = inode->i_private;
363 return seq_open(file, seq_ops);
367 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
370 struct ftrace_event_call *call = filp->private_data;
378 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
382 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
385 struct ftrace_event_call *call = filp->private_data;
390 if (cnt >= sizeof(buf))
393 if (copy_from_user(&buf, ubuf, cnt))
398 ret = strict_strtoul(buf, 10, &val);
402 ret = tracing_update_buffers();
409 mutex_lock(&event_mutex);
410 ftrace_event_enable_disable(call, val);
411 mutex_unlock(&event_mutex);
424 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
427 const char set_to_char[4] = { '?', '0', '1', 'X' };
428 const char *system = filp->private_data;
429 struct ftrace_event_call *call;
434 mutex_lock(&event_mutex);
435 list_for_each_entry(call, &ftrace_events, list) {
436 if (!call->name || !call->regfunc)
439 if (system && strcmp(call->system, system) != 0)
443 * We need to find out if all the events are set
444 * or if all events or cleared, or if we have
447 set |= (1 << !!call->enabled);
450 * If we have a mixture, no need to look further.
455 mutex_unlock(&event_mutex);
457 buf[0] = set_to_char[set];
460 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
466 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
469 const char *system = filp->private_data;
474 if (cnt >= sizeof(buf))
477 if (copy_from_user(&buf, ubuf, cnt))
482 ret = strict_strtoul(buf, 10, &val);
486 ret = tracing_update_buffers();
490 if (val != 0 && val != 1)
493 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
505 extern char *__bad_type_size(void);
508 #define FIELD(type, name) \
509 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
510 #type, "common_" #name, offsetof(typeof(field), name), \
513 static int trace_write_header(struct trace_seq *s)
515 struct trace_entry field;
517 /* struct trace_entry */
518 return trace_seq_printf(s,
519 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
520 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
521 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
522 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
523 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
525 FIELD(unsigned short, type),
526 FIELD(unsigned char, flags),
527 FIELD(unsigned char, preempt_count),
529 FIELD(int, lock_depth));
533 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
536 struct ftrace_event_call *call = filp->private_data;
544 s = kmalloc(sizeof(*s), GFP_KERNEL);
550 /* If any of the first writes fail, so will the show_format. */
552 trace_seq_printf(s, "name: %s\n", call->name);
553 trace_seq_printf(s, "ID: %d\n", call->id);
554 trace_seq_printf(s, "format:\n");
555 trace_write_header(s);
557 r = call->show_format(call, s);
560 * ug! The format output is bigger than a PAGE!!
562 buf = "FORMAT TOO BIG\n";
563 r = simple_read_from_buffer(ubuf, cnt, ppos,
568 r = simple_read_from_buffer(ubuf, cnt, ppos,
576 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
578 struct ftrace_event_call *call = filp->private_data;
585 s = kmalloc(sizeof(*s), GFP_KERNEL);
590 trace_seq_printf(s, "%d\n", call->id);
592 r = simple_read_from_buffer(ubuf, cnt, ppos,
599 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
602 struct ftrace_event_call *call = filp->private_data;
609 s = kmalloc(sizeof(*s), GFP_KERNEL);
615 print_event_filter(call, s);
616 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
624 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
627 struct ftrace_event_call *call = filp->private_data;
631 if (cnt >= PAGE_SIZE)
634 buf = (char *)__get_free_page(GFP_TEMPORARY);
638 if (copy_from_user(buf, ubuf, cnt)) {
639 free_page((unsigned long) buf);
644 err = apply_event_filter(call, buf);
645 free_page((unsigned long) buf);
655 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
658 struct event_subsystem *system = filp->private_data;
665 s = kmalloc(sizeof(*s), GFP_KERNEL);
671 print_subsystem_event_filter(system, s);
672 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
680 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
683 struct event_subsystem *system = filp->private_data;
687 if (cnt >= PAGE_SIZE)
690 buf = (char *)__get_free_page(GFP_TEMPORARY);
694 if (copy_from_user(buf, ubuf, cnt)) {
695 free_page((unsigned long) buf);
700 err = apply_subsystem_event_filter(system, buf);
701 free_page((unsigned long) buf);
711 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
713 int (*func)(struct trace_seq *s) = filp->private_data;
720 s = kmalloc(sizeof(*s), GFP_KERNEL);
727 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
734 static const struct seq_operations show_event_seq_ops = {
741 static const struct seq_operations show_set_event_seq_ops = {
748 static const struct file_operations ftrace_avail_fops = {
749 .open = ftrace_event_seq_open,
752 .release = seq_release,
755 static const struct file_operations ftrace_set_event_fops = {
756 .open = ftrace_event_seq_open,
758 .write = ftrace_event_write,
760 .release = seq_release,
763 static const struct file_operations ftrace_enable_fops = {
764 .open = tracing_open_generic,
765 .read = event_enable_read,
766 .write = event_enable_write,
769 static const struct file_operations ftrace_event_format_fops = {
770 .open = tracing_open_generic,
771 .read = event_format_read,
774 static const struct file_operations ftrace_event_id_fops = {
775 .open = tracing_open_generic,
776 .read = event_id_read,
779 static const struct file_operations ftrace_event_filter_fops = {
780 .open = tracing_open_generic,
781 .read = event_filter_read,
782 .write = event_filter_write,
785 static const struct file_operations ftrace_subsystem_filter_fops = {
786 .open = tracing_open_generic,
787 .read = subsystem_filter_read,
788 .write = subsystem_filter_write,
791 static const struct file_operations ftrace_system_enable_fops = {
792 .open = tracing_open_generic,
793 .read = system_enable_read,
794 .write = system_enable_write,
797 static const struct file_operations ftrace_show_header_fops = {
798 .open = tracing_open_generic,
802 static struct dentry *event_trace_events_dir(void)
804 static struct dentry *d_tracer;
805 static struct dentry *d_events;
810 d_tracer = tracing_init_dentry();
814 d_events = debugfs_create_dir("events", d_tracer);
816 pr_warning("Could not create debugfs "
817 "'events' directory\n");
822 static LIST_HEAD(event_subsystems);
824 static struct dentry *
825 event_subsystem_dir(const char *name, struct dentry *d_events)
827 struct event_subsystem *system;
828 struct dentry *entry;
830 /* First see if we did not already create this dir */
831 list_for_each_entry(system, &event_subsystems, list) {
832 if (strcmp(system->name, name) == 0) {
834 return system->entry;
838 /* need to create new entry */
839 system = kmalloc(sizeof(*system), GFP_KERNEL);
841 pr_warning("No memory to create event subsystem %s\n",
846 system->entry = debugfs_create_dir(name, d_events);
847 if (!system->entry) {
848 pr_warning("Could not create event subsystem %s\n",
854 system->nr_events = 1;
855 system->name = kstrdup(name, GFP_KERNEL);
857 debugfs_remove(system->entry);
862 list_add(&system->list, &event_subsystems);
864 system->filter = NULL;
866 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
867 if (!system->filter) {
868 pr_warning("Could not allocate filter for subsystem "
870 return system->entry;
873 entry = debugfs_create_file("filter", 0644, system->entry, system,
874 &ftrace_subsystem_filter_fops);
876 kfree(system->filter);
877 system->filter = NULL;
878 pr_warning("Could not create debugfs "
879 "'%s/filter' entry\n", name);
882 entry = trace_create_file("enable", 0644, system->entry,
883 (void *)system->name,
884 &ftrace_system_enable_fops);
886 return system->entry;
890 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
891 const struct file_operations *id,
892 const struct file_operations *enable,
893 const struct file_operations *filter,
894 const struct file_operations *format)
896 struct dentry *entry;
900 * If the trace point header did not define TRACE_SYSTEM
901 * then the system would be called "TRACE_SYSTEM".
903 if (strcmp(call->system, TRACE_SYSTEM) != 0)
904 d_events = event_subsystem_dir(call->system, d_events);
906 call->dir = debugfs_create_dir(call->name, d_events);
908 pr_warning("Could not create debugfs "
909 "'%s' directory\n", call->name);
914 entry = trace_create_file("enable", 0644, call->dir, call,
917 if (call->id && call->profile_enable)
918 entry = trace_create_file("id", 0444, call->dir, call,
921 if (call->define_fields) {
922 ret = call->define_fields(call);
924 pr_warning("Could not initialize trace point"
925 " events/%s\n", call->name);
928 entry = trace_create_file("filter", 0644, call->dir, call,
932 /* A trace may not want to export its format */
933 if (!call->show_format)
936 entry = trace_create_file("format", 0444, call->dir, call,
942 #define for_each_event(event, start, end) \
943 for (event = start; \
944 (unsigned long)event < (unsigned long)end; \
947 #ifdef CONFIG_MODULES
949 static LIST_HEAD(ftrace_module_file_list);
952 * Modules must own their file_operations to keep up with
953 * reference counting.
955 struct ftrace_module_file_ops {
956 struct list_head list;
958 struct file_operations id;
959 struct file_operations enable;
960 struct file_operations format;
961 struct file_operations filter;
964 static void remove_subsystem_dir(const char *name)
966 struct event_subsystem *system;
968 if (strcmp(name, TRACE_SYSTEM) == 0)
971 list_for_each_entry(system, &event_subsystems, list) {
972 if (strcmp(system->name, name) == 0) {
973 if (!--system->nr_events) {
974 struct event_filter *filter = system->filter;
976 debugfs_remove_recursive(system->entry);
977 list_del(&system->list);
979 kfree(filter->filter_string);
990 static struct ftrace_module_file_ops *
991 trace_create_file_ops(struct module *mod)
993 struct ftrace_module_file_ops *file_ops;
996 * This is a bit of a PITA. To allow for correct reference
997 * counting, modules must "own" their file_operations.
998 * To do this, we allocate the file operations that will be
999 * used in the event directory.
1002 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1006 file_ops->mod = mod;
1008 file_ops->id = ftrace_event_id_fops;
1009 file_ops->id.owner = mod;
1011 file_ops->enable = ftrace_enable_fops;
1012 file_ops->enable.owner = mod;
1014 file_ops->filter = ftrace_event_filter_fops;
1015 file_ops->filter.owner = mod;
1017 file_ops->format = ftrace_event_format_fops;
1018 file_ops->format.owner = mod;
1020 list_add(&file_ops->list, &ftrace_module_file_list);
1025 static void trace_module_add_events(struct module *mod)
1027 struct ftrace_module_file_ops *file_ops = NULL;
1028 struct ftrace_event_call *call, *start, *end;
1029 struct dentry *d_events;
1032 start = mod->trace_events;
1033 end = mod->trace_events + mod->num_trace_events;
1038 d_events = event_trace_events_dir();
1042 for_each_event(call, start, end) {
1043 /* The linker may leave blanks */
1046 if (call->raw_init) {
1047 ret = call->raw_init();
1050 pr_warning("Could not initialize trace "
1051 "point events/%s\n", call->name);
1056 * This module has events, create file ops for this module
1057 * if not already done.
1060 file_ops = trace_create_file_ops(mod);
1065 list_add(&call->list, &ftrace_events);
1066 event_create_dir(call, d_events,
1067 &file_ops->id, &file_ops->enable,
1068 &file_ops->filter, &file_ops->format);
1072 static void trace_module_remove_events(struct module *mod)
1074 struct ftrace_module_file_ops *file_ops;
1075 struct ftrace_event_call *call, *p;
1078 down_write(&trace_event_mutex);
1079 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1080 if (call->mod == mod) {
1082 ftrace_event_enable_disable(call, 0);
1084 __unregister_ftrace_event(call->event);
1085 debugfs_remove_recursive(call->dir);
1086 list_del(&call->list);
1087 trace_destroy_fields(call);
1088 destroy_preds(call);
1089 remove_subsystem_dir(call->system);
1093 /* Now free the file_operations */
1094 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1095 if (file_ops->mod == mod)
1098 if (&file_ops->list != &ftrace_module_file_list) {
1099 list_del(&file_ops->list);
1104 * It is safest to reset the ring buffer if the module being unloaded
1105 * registered any events.
1108 tracing_reset_current_online_cpus();
1109 up_write(&trace_event_mutex);
1112 static int trace_module_notify(struct notifier_block *self,
1113 unsigned long val, void *data)
1115 struct module *mod = data;
1117 mutex_lock(&event_mutex);
1119 case MODULE_STATE_COMING:
1120 trace_module_add_events(mod);
1122 case MODULE_STATE_GOING:
1123 trace_module_remove_events(mod);
1126 mutex_unlock(&event_mutex);
1131 static int trace_module_notify(struct notifier_block *self,
1132 unsigned long val, void *data)
1136 #endif /* CONFIG_MODULES */
1138 static struct notifier_block trace_module_nb = {
1139 .notifier_call = trace_module_notify,
1143 extern struct ftrace_event_call __start_ftrace_events[];
1144 extern struct ftrace_event_call __stop_ftrace_events[];
1146 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1148 static __init int setup_trace_event(char *str)
1150 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1151 ring_buffer_expanded = 1;
1152 tracing_selftest_disabled = 1;
1156 __setup("trace_event=", setup_trace_event);
1158 static __init int event_trace_init(void)
1160 struct ftrace_event_call *call;
1161 struct dentry *d_tracer;
1162 struct dentry *entry;
1163 struct dentry *d_events;
1165 char *buf = bootup_event_buf;
1168 d_tracer = tracing_init_dentry();
1172 entry = debugfs_create_file("available_events", 0444, d_tracer,
1173 (void *)&show_event_seq_ops,
1174 &ftrace_avail_fops);
1176 pr_warning("Could not create debugfs "
1177 "'available_events' entry\n");
1179 entry = debugfs_create_file("set_event", 0644, d_tracer,
1180 (void *)&show_set_event_seq_ops,
1181 &ftrace_set_event_fops);
1183 pr_warning("Could not create debugfs "
1184 "'set_event' entry\n");
1186 d_events = event_trace_events_dir();
1190 /* ring buffer internal formats */
1191 trace_create_file("header_page", 0444, d_events,
1192 ring_buffer_print_page_header,
1193 &ftrace_show_header_fops);
1195 trace_create_file("header_event", 0444, d_events,
1196 ring_buffer_print_entry_header,
1197 &ftrace_show_header_fops);
1199 trace_create_file("enable", 0644, d_events,
1200 NULL, &ftrace_system_enable_fops);
1202 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1203 /* The linker may leave blanks */
1206 if (call->raw_init) {
1207 ret = call->raw_init();
1210 pr_warning("Could not initialize trace "
1211 "point events/%s\n", call->name);
1215 list_add(&call->list, &ftrace_events);
1216 event_create_dir(call, d_events, &ftrace_event_id_fops,
1217 &ftrace_enable_fops, &ftrace_event_filter_fops,
1218 &ftrace_event_format_fops);
1222 token = strsep(&buf, ",");
1229 ret = ftrace_set_clr_event(token, 1);
1231 pr_warning("Failed to enable trace event: %s\n", token);
1234 ret = register_module_notifier(&trace_module_nb);
1236 pr_warning("Failed to register trace events module notifier\n");
1240 fs_initcall(event_trace_init);
1242 #ifdef CONFIG_FTRACE_STARTUP_TEST
1244 static DEFINE_SPINLOCK(test_spinlock);
1245 static DEFINE_SPINLOCK(test_spinlock_irq);
1246 static DEFINE_MUTEX(test_mutex);
1248 static __init void test_work(struct work_struct *dummy)
1250 spin_lock(&test_spinlock);
1251 spin_lock_irq(&test_spinlock_irq);
1253 spin_unlock_irq(&test_spinlock_irq);
1254 spin_unlock(&test_spinlock);
1256 mutex_lock(&test_mutex);
1258 mutex_unlock(&test_mutex);
1261 static __init int event_test_thread(void *unused)
1265 test_malloc = kmalloc(1234, GFP_KERNEL);
1267 pr_info("failed to kmalloc\n");
1269 schedule_on_each_cpu(test_work);
1273 set_current_state(TASK_INTERRUPTIBLE);
1274 while (!kthread_should_stop())
1281 * Do various things that may trigger events.
1283 static __init void event_test_stuff(void)
1285 struct task_struct *test_thread;
1287 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1289 kthread_stop(test_thread);
1293 * For every trace event defined, we will test each trace point separately,
1294 * and then by groups, and finally all trace points.
1296 static __init void event_trace_self_tests(void)
1298 struct ftrace_event_call *call;
1299 struct event_subsystem *system;
1302 pr_info("Running tests on trace events:\n");
1304 list_for_each_entry(call, &ftrace_events, list) {
1306 /* Only test those that have a regfunc */
1311 * Testing syscall events here is pretty useless, but
1312 * we still do it if configured. But this is time consuming.
1313 * What we really need is a user thread to perform the
1314 * syscalls as we test.
1316 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1318 strcmp(call->system, "syscalls") == 0)
1322 pr_info("Testing event %s: ", call->name);
1325 * If an event is already enabled, someone is using
1326 * it and the self test should not be on.
1328 if (call->enabled) {
1329 pr_warning("Enabled event during self test!\n");
1334 ftrace_event_enable_disable(call, 1);
1336 ftrace_event_enable_disable(call, 0);
1341 /* Now test at the sub system level */
1343 pr_info("Running tests on trace event systems:\n");
1345 list_for_each_entry(system, &event_subsystems, list) {
1347 /* the ftrace system is special, skip it */
1348 if (strcmp(system->name, "ftrace") == 0)
1351 pr_info("Testing event system %s: ", system->name);
1353 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1354 if (WARN_ON_ONCE(ret)) {
1355 pr_warning("error enabling system %s\n",
1362 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1363 if (WARN_ON_ONCE(ret))
1364 pr_warning("error disabling system %s\n",
1370 /* Test with all events enabled */
1372 pr_info("Running tests on all trace events:\n");
1373 pr_info("Testing all events: ");
1375 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1376 if (WARN_ON_ONCE(ret)) {
1377 pr_warning("error enabling all events\n");
1384 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1385 if (WARN_ON_ONCE(ret)) {
1386 pr_warning("error disabling all events\n");
1393 #ifdef CONFIG_FUNCTION_TRACER
1395 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1398 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1400 struct ring_buffer_event *event;
1401 struct ring_buffer *buffer;
1402 struct ftrace_entry *entry;
1403 unsigned long flags;
1409 pc = preempt_count();
1410 resched = ftrace_preempt_disable();
1411 cpu = raw_smp_processor_id();
1412 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1417 local_save_flags(flags);
1419 event = trace_current_buffer_lock_reserve(&buffer,
1420 TRACE_FN, sizeof(*entry),
1424 entry = ring_buffer_event_data(event);
1426 entry->parent_ip = parent_ip;
1428 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1431 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1432 ftrace_preempt_enable(resched);
1435 static struct ftrace_ops trace_ops __initdata =
1437 .func = function_test_events_call,
1440 static __init void event_trace_self_test_with_function(void)
1442 register_ftrace_function(&trace_ops);
1443 pr_info("Running tests again, along with the function tracer\n");
1444 event_trace_self_tests();
1445 unregister_ftrace_function(&trace_ops);
1448 static __init void event_trace_self_test_with_function(void)
1453 static __init int event_trace_self_tests_init(void)
1455 if (!tracing_selftest_disabled) {
1456 event_trace_self_tests();
1457 event_trace_self_test_with_function();
1463 late_initcall(event_trace_self_tests_init);