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"
24 #define TRACE_SYSTEM "TRACE_SYSTEM"
26 DEFINE_MUTEX(event_mutex);
28 LIST_HEAD(ftrace_events);
30 int trace_define_field(struct ftrace_event_call *call, char *type,
31 char *name, int offset, int size, int is_signed)
33 struct ftrace_event_field *field;
35 field = kzalloc(sizeof(*field), GFP_KERNEL);
39 field->name = kstrdup(name, GFP_KERNEL);
43 field->type = kstrdup(type, GFP_KERNEL);
47 field->offset = offset;
49 field->is_signed = is_signed;
50 list_add(&field->link, &call->fields);
63 EXPORT_SYMBOL_GPL(trace_define_field);
65 #define __common_field(type, item) \
66 ret = trace_define_field(call, #type, "common_" #item, \
67 offsetof(typeof(ent), item), \
69 is_signed_type(type)); \
73 int trace_define_common_fields(struct ftrace_event_call *call)
76 struct trace_entry ent;
78 __common_field(unsigned short, type);
79 __common_field(unsigned char, flags);
80 __common_field(unsigned char, preempt_count);
81 __common_field(int, pid);
82 __common_field(int, tgid);
89 static void trace_destroy_fields(struct ftrace_event_call *call)
91 struct ftrace_event_field *field, *next;
93 list_for_each_entry_safe(field, next, &call->fields, link) {
94 list_del(&field->link);
101 #endif /* CONFIG_MODULES */
103 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
110 tracing_stop_cmdline_record();
111 call->unregfunc(call->data);
115 if (!call->enabled) {
117 tracing_start_cmdline_record();
118 call->regfunc(call->data);
124 static void ftrace_clear_events(void)
126 struct ftrace_event_call *call;
128 mutex_lock(&event_mutex);
129 list_for_each_entry(call, &ftrace_events, list) {
130 ftrace_event_enable_disable(call, 0);
132 mutex_unlock(&event_mutex);
136 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
138 static int __ftrace_set_clr_event(const char *match, const char *sub,
139 const char *event, int set)
141 struct ftrace_event_call *call;
144 mutex_lock(&event_mutex);
145 list_for_each_entry(call, &ftrace_events, list) {
147 if (!call->name || !call->regfunc)
151 strcmp(match, call->name) != 0 &&
152 strcmp(match, call->system) != 0)
155 if (sub && strcmp(sub, call->system) != 0)
158 if (event && strcmp(event, call->name) != 0)
161 ftrace_event_enable_disable(call, set);
165 mutex_unlock(&event_mutex);
170 static int ftrace_set_clr_event(char *buf, int set)
172 char *event = NULL, *sub = NULL, *match;
175 * The buf format can be <subsystem>:<event-name>
176 * *:<event-name> means any event by that name.
177 * :<event-name> is the same.
179 * <subsystem>:* means all events in that subsystem
180 * <subsystem>: means the same.
182 * <name> (no ':') means all events in a subsystem with
183 * the name <name> or any event that matches <name>
186 match = strsep(&buf, ":");
192 if (!strlen(sub) || strcmp(sub, "*") == 0)
194 if (!strlen(event) || strcmp(event, "*") == 0)
198 return __ftrace_set_clr_event(match, sub, event, set);
202 * trace_set_clr_event - enable or disable an event
203 * @system: system name to match (NULL for any system)
204 * @event: event name to match (NULL for all events, within system)
205 * @set: 1 to enable, 0 to disable
207 * This is a way for other parts of the kernel to enable or disable
210 * Returns 0 on success, -EINVAL if the parameters do not match any
213 int trace_set_clr_event(const char *system, const char *event, int set)
215 return __ftrace_set_clr_event(NULL, system, event, set);
218 /* 128 should be much more than enough */
219 #define EVENT_BUF_SIZE 127
222 ftrace_event_write(struct file *file, const char __user *ubuf,
223 size_t cnt, loff_t *ppos)
234 ret = tracing_update_buffers();
238 ret = get_user(ch, ubuf++);
244 /* skip white space */
245 while (cnt && isspace(ch)) {
246 ret = get_user(ch, ubuf++);
253 /* Only white space found? */
260 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
264 if (cnt > EVENT_BUF_SIZE)
265 cnt = EVENT_BUF_SIZE;
268 while (cnt && !isspace(ch)) {
274 ret = get_user(ch, ubuf++);
284 ret = ftrace_set_clr_event(buf, set);
297 t_next(struct seq_file *m, void *v, loff_t *pos)
299 struct list_head *list = m->private;
300 struct ftrace_event_call *call;
305 if (list == &ftrace_events)
308 call = list_entry(list, struct ftrace_event_call, list);
311 * The ftrace subsystem is for showing formats only.
312 * They can not be enabled or disabled via the event files.
320 m->private = list->next;
325 static void *t_start(struct seq_file *m, loff_t *pos)
327 struct ftrace_event_call *call = NULL;
330 mutex_lock(&event_mutex);
332 m->private = ftrace_events.next;
333 for (l = 0; l <= *pos; ) {
334 call = t_next(m, NULL, &l);
342 s_next(struct seq_file *m, void *v, loff_t *pos)
344 struct list_head *list = m->private;
345 struct ftrace_event_call *call;
350 if (list == &ftrace_events)
353 call = list_entry(list, struct ftrace_event_call, list);
355 if (!call->enabled) {
360 m->private = list->next;
365 static void *s_start(struct seq_file *m, loff_t *pos)
367 struct ftrace_event_call *call = NULL;
370 mutex_lock(&event_mutex);
372 m->private = ftrace_events.next;
373 for (l = 0; l <= *pos; ) {
374 call = s_next(m, NULL, &l);
381 static int t_show(struct seq_file *m, void *v)
383 struct ftrace_event_call *call = v;
385 if (strcmp(call->system, TRACE_SYSTEM) != 0)
386 seq_printf(m, "%s:", call->system);
387 seq_printf(m, "%s\n", call->name);
392 static void t_stop(struct seq_file *m, void *p)
394 mutex_unlock(&event_mutex);
398 ftrace_event_seq_open(struct inode *inode, struct file *file)
400 const struct seq_operations *seq_ops;
402 if ((file->f_mode & FMODE_WRITE) &&
403 (file->f_flags & O_TRUNC))
404 ftrace_clear_events();
406 seq_ops = inode->i_private;
407 return seq_open(file, seq_ops);
411 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
414 struct ftrace_event_call *call = filp->private_data;
422 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
426 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
429 struct ftrace_event_call *call = filp->private_data;
434 if (cnt >= sizeof(buf))
437 if (copy_from_user(&buf, ubuf, cnt))
442 ret = strict_strtoul(buf, 10, &val);
446 ret = tracing_update_buffers();
453 mutex_lock(&event_mutex);
454 ftrace_event_enable_disable(call, val);
455 mutex_unlock(&event_mutex);
468 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
471 const char set_to_char[4] = { '?', '0', '1', 'X' };
472 const char *system = filp->private_data;
473 struct ftrace_event_call *call;
478 mutex_lock(&event_mutex);
479 list_for_each_entry(call, &ftrace_events, list) {
480 if (!call->name || !call->regfunc)
483 if (system && strcmp(call->system, system) != 0)
487 * We need to find out if all the events are set
488 * or if all events or cleared, or if we have
491 set |= (1 << !!call->enabled);
494 * If we have a mixture, no need to look further.
499 mutex_unlock(&event_mutex);
501 buf[0] = set_to_char[set];
504 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
510 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
513 const char *system = filp->private_data;
518 if (cnt >= sizeof(buf))
521 if (copy_from_user(&buf, ubuf, cnt))
526 ret = strict_strtoul(buf, 10, &val);
530 ret = tracing_update_buffers();
534 if (val != 0 && val != 1)
537 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
549 extern char *__bad_type_size(void);
552 #define FIELD(type, name) \
553 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
554 #type, "common_" #name, offsetof(typeof(field), name), \
557 static int trace_write_header(struct trace_seq *s)
559 struct trace_entry field;
561 /* struct trace_entry */
562 return trace_seq_printf(s,
563 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
564 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
565 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
566 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
567 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
569 FIELD(unsigned short, type),
570 FIELD(unsigned char, flags),
571 FIELD(unsigned char, preempt_count),
577 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
580 struct ftrace_event_call *call = filp->private_data;
588 s = kmalloc(sizeof(*s), GFP_KERNEL);
594 /* If any of the first writes fail, so will the show_format. */
596 trace_seq_printf(s, "name: %s\n", call->name);
597 trace_seq_printf(s, "ID: %d\n", call->id);
598 trace_seq_printf(s, "format:\n");
599 trace_write_header(s);
601 r = call->show_format(call, s);
604 * ug! The format output is bigger than a PAGE!!
606 buf = "FORMAT TOO BIG\n";
607 r = simple_read_from_buffer(ubuf, cnt, ppos,
612 r = simple_read_from_buffer(ubuf, cnt, ppos,
620 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
622 struct ftrace_event_call *call = filp->private_data;
629 s = kmalloc(sizeof(*s), GFP_KERNEL);
634 trace_seq_printf(s, "%d\n", call->id);
636 r = simple_read_from_buffer(ubuf, cnt, ppos,
643 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
646 struct ftrace_event_call *call = filp->private_data;
653 s = kmalloc(sizeof(*s), GFP_KERNEL);
659 print_event_filter(call, s);
660 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
668 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
671 struct ftrace_event_call *call = filp->private_data;
675 if (cnt >= PAGE_SIZE)
678 buf = (char *)__get_free_page(GFP_TEMPORARY);
682 if (copy_from_user(buf, ubuf, cnt)) {
683 free_page((unsigned long) buf);
688 err = apply_event_filter(call, buf);
689 free_page((unsigned long) buf);
699 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
702 struct event_subsystem *system = filp->private_data;
709 s = kmalloc(sizeof(*s), GFP_KERNEL);
715 print_subsystem_event_filter(system, s);
716 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
724 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
727 struct event_subsystem *system = filp->private_data;
731 if (cnt >= PAGE_SIZE)
734 buf = (char *)__get_free_page(GFP_TEMPORARY);
738 if (copy_from_user(buf, ubuf, cnt)) {
739 free_page((unsigned long) buf);
744 err = apply_subsystem_event_filter(system, buf);
745 free_page((unsigned long) buf);
755 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
757 int (*func)(struct trace_seq *s) = filp->private_data;
764 s = kmalloc(sizeof(*s), GFP_KERNEL);
771 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
778 static const struct seq_operations show_event_seq_ops = {
785 static const struct seq_operations show_set_event_seq_ops = {
792 static const struct file_operations ftrace_avail_fops = {
793 .open = ftrace_event_seq_open,
796 .release = seq_release,
799 static const struct file_operations ftrace_set_event_fops = {
800 .open = ftrace_event_seq_open,
802 .write = ftrace_event_write,
804 .release = seq_release,
807 static const struct file_operations ftrace_enable_fops = {
808 .open = tracing_open_generic,
809 .read = event_enable_read,
810 .write = event_enable_write,
813 static const struct file_operations ftrace_event_format_fops = {
814 .open = tracing_open_generic,
815 .read = event_format_read,
818 static const struct file_operations ftrace_event_id_fops = {
819 .open = tracing_open_generic,
820 .read = event_id_read,
823 static const struct file_operations ftrace_event_filter_fops = {
824 .open = tracing_open_generic,
825 .read = event_filter_read,
826 .write = event_filter_write,
829 static const struct file_operations ftrace_subsystem_filter_fops = {
830 .open = tracing_open_generic,
831 .read = subsystem_filter_read,
832 .write = subsystem_filter_write,
835 static const struct file_operations ftrace_system_enable_fops = {
836 .open = tracing_open_generic,
837 .read = system_enable_read,
838 .write = system_enable_write,
841 static const struct file_operations ftrace_show_header_fops = {
842 .open = tracing_open_generic,
846 static struct dentry *event_trace_events_dir(void)
848 static struct dentry *d_tracer;
849 static struct dentry *d_events;
854 d_tracer = tracing_init_dentry();
858 d_events = debugfs_create_dir("events", d_tracer);
860 pr_warning("Could not create debugfs "
861 "'events' directory\n");
866 static LIST_HEAD(event_subsystems);
868 static struct dentry *
869 event_subsystem_dir(const char *name, struct dentry *d_events)
871 struct event_subsystem *system;
872 struct dentry *entry;
874 /* First see if we did not already create this dir */
875 list_for_each_entry(system, &event_subsystems, list) {
876 if (strcmp(system->name, name) == 0) {
878 return system->entry;
882 /* need to create new entry */
883 system = kmalloc(sizeof(*system), GFP_KERNEL);
885 pr_warning("No memory to create event subsystem %s\n",
890 system->entry = debugfs_create_dir(name, d_events);
891 if (!system->entry) {
892 pr_warning("Could not create event subsystem %s\n",
898 system->nr_events = 1;
899 system->name = kstrdup(name, GFP_KERNEL);
901 debugfs_remove(system->entry);
906 list_add(&system->list, &event_subsystems);
908 system->filter = NULL;
910 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
911 if (!system->filter) {
912 pr_warning("Could not allocate filter for subsystem "
914 return system->entry;
917 entry = debugfs_create_file("filter", 0644, system->entry, system,
918 &ftrace_subsystem_filter_fops);
920 kfree(system->filter);
921 system->filter = NULL;
922 pr_warning("Could not create debugfs "
923 "'%s/filter' entry\n", name);
926 entry = trace_create_file("enable", 0644, system->entry,
927 (void *)system->name,
928 &ftrace_system_enable_fops);
930 return system->entry;
934 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
935 const struct file_operations *id,
936 const struct file_operations *enable,
937 const struct file_operations *filter,
938 const struct file_operations *format)
940 struct dentry *entry;
944 * If the trace point header did not define TRACE_SYSTEM
945 * then the system would be called "TRACE_SYSTEM".
947 if (strcmp(call->system, TRACE_SYSTEM) != 0)
948 d_events = event_subsystem_dir(call->system, d_events);
950 call->dir = debugfs_create_dir(call->name, d_events);
952 pr_warning("Could not create debugfs "
953 "'%s' directory\n", call->name);
958 entry = trace_create_file("enable", 0644, call->dir, call,
961 if (call->id && call->profile_enable)
962 entry = trace_create_file("id", 0444, call->dir, call,
965 if (call->define_fields) {
966 ret = call->define_fields(call);
968 pr_warning("Could not initialize trace point"
969 " events/%s\n", call->name);
972 entry = trace_create_file("filter", 0644, call->dir, call,
976 /* A trace may not want to export its format */
977 if (!call->show_format)
980 entry = trace_create_file("format", 0444, call->dir, call,
986 #define for_each_event(event, start, end) \
987 for (event = start; \
988 (unsigned long)event < (unsigned long)end; \
991 #ifdef CONFIG_MODULES
993 static LIST_HEAD(ftrace_module_file_list);
996 * Modules must own their file_operations to keep up with
997 * reference counting.
999 struct ftrace_module_file_ops {
1000 struct list_head list;
1002 struct file_operations id;
1003 struct file_operations enable;
1004 struct file_operations format;
1005 struct file_operations filter;
1008 static void remove_subsystem_dir(const char *name)
1010 struct event_subsystem *system;
1012 if (strcmp(name, TRACE_SYSTEM) == 0)
1015 list_for_each_entry(system, &event_subsystems, list) {
1016 if (strcmp(system->name, name) == 0) {
1017 if (!--system->nr_events) {
1018 struct event_filter *filter = system->filter;
1020 debugfs_remove_recursive(system->entry);
1021 list_del(&system->list);
1023 kfree(filter->filter_string);
1026 kfree(system->name);
1034 static struct ftrace_module_file_ops *
1035 trace_create_file_ops(struct module *mod)
1037 struct ftrace_module_file_ops *file_ops;
1040 * This is a bit of a PITA. To allow for correct reference
1041 * counting, modules must "own" their file_operations.
1042 * To do this, we allocate the file operations that will be
1043 * used in the event directory.
1046 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1050 file_ops->mod = mod;
1052 file_ops->id = ftrace_event_id_fops;
1053 file_ops->id.owner = mod;
1055 file_ops->enable = ftrace_enable_fops;
1056 file_ops->enable.owner = mod;
1058 file_ops->filter = ftrace_event_filter_fops;
1059 file_ops->filter.owner = mod;
1061 file_ops->format = ftrace_event_format_fops;
1062 file_ops->format.owner = mod;
1064 list_add(&file_ops->list, &ftrace_module_file_list);
1069 static void trace_module_add_events(struct module *mod)
1071 struct ftrace_module_file_ops *file_ops = NULL;
1072 struct ftrace_event_call *call, *start, *end;
1073 struct dentry *d_events;
1076 start = mod->trace_events;
1077 end = mod->trace_events + mod->num_trace_events;
1082 d_events = event_trace_events_dir();
1086 for_each_event(call, start, end) {
1087 /* The linker may leave blanks */
1090 if (call->raw_init) {
1091 ret = call->raw_init();
1094 pr_warning("Could not initialize trace "
1095 "point events/%s\n", call->name);
1100 * This module has events, create file ops for this module
1101 * if not already done.
1104 file_ops = trace_create_file_ops(mod);
1109 list_add(&call->list, &ftrace_events);
1110 event_create_dir(call, d_events,
1111 &file_ops->id, &file_ops->enable,
1112 &file_ops->filter, &file_ops->format);
1116 static void trace_module_remove_events(struct module *mod)
1118 struct ftrace_module_file_ops *file_ops;
1119 struct ftrace_event_call *call, *p;
1122 down_write(&trace_event_mutex);
1123 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1124 if (call->mod == mod) {
1126 ftrace_event_enable_disable(call, 0);
1128 __unregister_ftrace_event(call->event);
1129 debugfs_remove_recursive(call->dir);
1130 list_del(&call->list);
1131 trace_destroy_fields(call);
1132 destroy_preds(call);
1133 remove_subsystem_dir(call->system);
1137 /* Now free the file_operations */
1138 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1139 if (file_ops->mod == mod)
1142 if (&file_ops->list != &ftrace_module_file_list) {
1143 list_del(&file_ops->list);
1148 * It is safest to reset the ring buffer if the module being unloaded
1149 * registered any events.
1152 tracing_reset_current_online_cpus();
1153 up_write(&trace_event_mutex);
1156 static int trace_module_notify(struct notifier_block *self,
1157 unsigned long val, void *data)
1159 struct module *mod = data;
1161 mutex_lock(&event_mutex);
1163 case MODULE_STATE_COMING:
1164 trace_module_add_events(mod);
1166 case MODULE_STATE_GOING:
1167 trace_module_remove_events(mod);
1170 mutex_unlock(&event_mutex);
1175 static int trace_module_notify(struct notifier_block *self,
1176 unsigned long val, void *data)
1180 #endif /* CONFIG_MODULES */
1182 struct notifier_block trace_module_nb = {
1183 .notifier_call = trace_module_notify,
1187 extern struct ftrace_event_call __start_ftrace_events[];
1188 extern struct ftrace_event_call __stop_ftrace_events[];
1190 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1192 static __init int setup_trace_event(char *str)
1194 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1195 ring_buffer_expanded = 1;
1196 tracing_selftest_disabled = 1;
1200 __setup("trace_event=", setup_trace_event);
1202 static __init int event_trace_init(void)
1204 struct ftrace_event_call *call;
1205 struct dentry *d_tracer;
1206 struct dentry *entry;
1207 struct dentry *d_events;
1209 char *buf = bootup_event_buf;
1212 d_tracer = tracing_init_dentry();
1216 entry = debugfs_create_file("available_events", 0444, d_tracer,
1217 (void *)&show_event_seq_ops,
1218 &ftrace_avail_fops);
1220 pr_warning("Could not create debugfs "
1221 "'available_events' entry\n");
1223 entry = debugfs_create_file("set_event", 0644, d_tracer,
1224 (void *)&show_set_event_seq_ops,
1225 &ftrace_set_event_fops);
1227 pr_warning("Could not create debugfs "
1228 "'set_event' entry\n");
1230 d_events = event_trace_events_dir();
1234 /* ring buffer internal formats */
1235 trace_create_file("header_page", 0444, d_events,
1236 ring_buffer_print_page_header,
1237 &ftrace_show_header_fops);
1239 trace_create_file("header_event", 0444, d_events,
1240 ring_buffer_print_entry_header,
1241 &ftrace_show_header_fops);
1243 trace_create_file("enable", 0644, d_events,
1244 NULL, &ftrace_system_enable_fops);
1246 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1247 /* The linker may leave blanks */
1250 if (call->raw_init) {
1251 ret = call->raw_init();
1254 pr_warning("Could not initialize trace "
1255 "point events/%s\n", call->name);
1259 list_add(&call->list, &ftrace_events);
1260 event_create_dir(call, d_events, &ftrace_event_id_fops,
1261 &ftrace_enable_fops, &ftrace_event_filter_fops,
1262 &ftrace_event_format_fops);
1266 token = strsep(&buf, ",");
1273 ret = ftrace_set_clr_event(token, 1);
1275 pr_warning("Failed to enable trace event: %s\n", token);
1278 ret = register_module_notifier(&trace_module_nb);
1280 pr_warning("Failed to register trace events module notifier\n");
1284 fs_initcall(event_trace_init);
1286 #ifdef CONFIG_FTRACE_STARTUP_TEST
1288 static DEFINE_SPINLOCK(test_spinlock);
1289 static DEFINE_SPINLOCK(test_spinlock_irq);
1290 static DEFINE_MUTEX(test_mutex);
1292 static __init void test_work(struct work_struct *dummy)
1294 spin_lock(&test_spinlock);
1295 spin_lock_irq(&test_spinlock_irq);
1297 spin_unlock_irq(&test_spinlock_irq);
1298 spin_unlock(&test_spinlock);
1300 mutex_lock(&test_mutex);
1302 mutex_unlock(&test_mutex);
1305 static __init int event_test_thread(void *unused)
1309 test_malloc = kmalloc(1234, GFP_KERNEL);
1311 pr_info("failed to kmalloc\n");
1313 schedule_on_each_cpu(test_work);
1317 set_current_state(TASK_INTERRUPTIBLE);
1318 while (!kthread_should_stop())
1325 * Do various things that may trigger events.
1327 static __init void event_test_stuff(void)
1329 struct task_struct *test_thread;
1331 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1333 kthread_stop(test_thread);
1337 * For every trace event defined, we will test each trace point separately,
1338 * and then by groups, and finally all trace points.
1340 static __init void event_trace_self_tests(void)
1342 struct ftrace_event_call *call;
1343 struct event_subsystem *system;
1346 pr_info("Running tests on trace events:\n");
1348 list_for_each_entry(call, &ftrace_events, list) {
1350 /* Only test those that have a regfunc */
1354 pr_info("Testing event %s: ", call->name);
1357 * If an event is already enabled, someone is using
1358 * it and the self test should not be on.
1360 if (call->enabled) {
1361 pr_warning("Enabled event during self test!\n");
1366 ftrace_event_enable_disable(call, 1);
1368 ftrace_event_enable_disable(call, 0);
1373 /* Now test at the sub system level */
1375 pr_info("Running tests on trace event systems:\n");
1377 list_for_each_entry(system, &event_subsystems, list) {
1379 /* the ftrace system is special, skip it */
1380 if (strcmp(system->name, "ftrace") == 0)
1383 pr_info("Testing event system %s: ", system->name);
1385 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1386 if (WARN_ON_ONCE(ret)) {
1387 pr_warning("error enabling system %s\n",
1394 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1395 if (WARN_ON_ONCE(ret))
1396 pr_warning("error disabling system %s\n",
1402 /* Test with all events enabled */
1404 pr_info("Running tests on all trace events:\n");
1405 pr_info("Testing all events: ");
1407 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1408 if (WARN_ON_ONCE(ret)) {
1409 pr_warning("error enabling all events\n");
1416 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1417 if (WARN_ON_ONCE(ret)) {
1418 pr_warning("error disabling all events\n");
1425 #ifdef CONFIG_FUNCTION_TRACER
1427 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1430 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1432 struct ring_buffer_event *event;
1433 struct ftrace_entry *entry;
1434 unsigned long flags;
1440 pc = preempt_count();
1441 resched = ftrace_preempt_disable();
1442 cpu = raw_smp_processor_id();
1443 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1448 local_save_flags(flags);
1450 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1454 entry = ring_buffer_event_data(event);
1456 entry->parent_ip = parent_ip;
1458 trace_nowake_buffer_unlock_commit(event, flags, pc);
1461 atomic_dec(&per_cpu(test_event_disable, cpu));
1462 ftrace_preempt_enable(resched);
1465 static struct ftrace_ops trace_ops __initdata =
1467 .func = function_test_events_call,
1470 static __init void event_trace_self_test_with_function(void)
1472 register_ftrace_function(&trace_ops);
1473 pr_info("Running tests again, along with the function tracer\n");
1474 event_trace_self_tests();
1475 unregister_ftrace_function(&trace_ops);
1478 static __init void event_trace_self_test_with_function(void)
1483 static __init int event_trace_self_tests_init(void)
1485 if (!tracing_selftest_disabled) {
1486 event_trace_self_tests();
1487 event_trace_self_test_with_function();
1493 late_initcall(event_trace_self_tests_init);