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,
34 struct ftrace_event_field *field;
36 field = kzalloc(sizeof(*field), GFP_KERNEL);
40 field->name = kstrdup(name, GFP_KERNEL);
44 field->type = kstrdup(type, GFP_KERNEL);
48 if (filter_type == FILTER_OTHER)
49 field->filter_type = filter_assign_type(type);
51 field->filter_type = filter_type;
53 field->offset = offset;
55 field->is_signed = is_signed;
57 list_add(&field->link, &call->fields);
70 EXPORT_SYMBOL_GPL(trace_define_field);
72 #define __common_field(type, item) \
73 ret = trace_define_field(call, #type, "common_" #item, \
74 offsetof(typeof(ent), item), \
76 is_signed_type(type), FILTER_OTHER); \
80 int trace_define_common_fields(struct ftrace_event_call *call)
83 struct trace_entry ent;
85 __common_field(unsigned short, type);
86 __common_field(unsigned char, flags);
87 __common_field(unsigned char, preempt_count);
88 __common_field(int, pid);
89 __common_field(int, tgid);
93 EXPORT_SYMBOL_GPL(trace_define_common_fields);
95 void trace_destroy_fields(struct ftrace_event_call *call)
97 struct ftrace_event_field *field, *next;
99 list_for_each_entry_safe(field, next, &call->fields, link) {
100 list_del(&field->link);
107 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
114 tracing_stop_cmdline_record();
115 call->unregfunc(call);
119 if (!call->enabled) {
121 tracing_start_cmdline_record();
128 static void ftrace_clear_events(void)
130 struct ftrace_event_call *call;
132 mutex_lock(&event_mutex);
133 list_for_each_entry(call, &ftrace_events, list) {
134 ftrace_event_enable_disable(call, 0);
136 mutex_unlock(&event_mutex);
140 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
142 static int __ftrace_set_clr_event(const char *match, const char *sub,
143 const char *event, int set)
145 struct ftrace_event_call *call;
148 mutex_lock(&event_mutex);
149 list_for_each_entry(call, &ftrace_events, list) {
151 if (!call->name || !call->regfunc)
155 strcmp(match, call->name) != 0 &&
156 strcmp(match, call->system) != 0)
159 if (sub && strcmp(sub, call->system) != 0)
162 if (event && strcmp(event, call->name) != 0)
165 ftrace_event_enable_disable(call, set);
169 mutex_unlock(&event_mutex);
174 static int ftrace_set_clr_event(char *buf, int set)
176 char *event = NULL, *sub = NULL, *match;
179 * The buf format can be <subsystem>:<event-name>
180 * *:<event-name> means any event by that name.
181 * :<event-name> is the same.
183 * <subsystem>:* means all events in that subsystem
184 * <subsystem>: means the same.
186 * <name> (no ':') means all events in a subsystem with
187 * the name <name> or any event that matches <name>
190 match = strsep(&buf, ":");
196 if (!strlen(sub) || strcmp(sub, "*") == 0)
198 if (!strlen(event) || strcmp(event, "*") == 0)
202 return __ftrace_set_clr_event(match, sub, event, set);
206 * trace_set_clr_event - enable or disable an event
207 * @system: system name to match (NULL for any system)
208 * @event: event name to match (NULL for all events, within system)
209 * @set: 1 to enable, 0 to disable
211 * This is a way for other parts of the kernel to enable or disable
214 * Returns 0 on success, -EINVAL if the parameters do not match any
217 int trace_set_clr_event(const char *system, const char *event, int set)
219 return __ftrace_set_clr_event(NULL, system, event, set);
222 /* 128 should be much more than enough */
223 #define EVENT_BUF_SIZE 127
226 ftrace_event_write(struct file *file, const char __user *ubuf,
227 size_t cnt, loff_t *ppos)
238 ret = tracing_update_buffers();
242 ret = get_user(ch, ubuf++);
248 /* skip white space */
249 while (cnt && isspace(ch)) {
250 ret = get_user(ch, ubuf++);
257 /* Only white space found? */
264 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
268 if (cnt > EVENT_BUF_SIZE)
269 cnt = EVENT_BUF_SIZE;
272 while (cnt && !isspace(ch)) {
278 ret = get_user(ch, ubuf++);
288 ret = ftrace_set_clr_event(buf, set);
301 t_next(struct seq_file *m, void *v, loff_t *pos)
303 struct list_head *list = m->private;
304 struct ftrace_event_call *call;
309 if (list == &ftrace_events)
312 call = list_entry(list, struct ftrace_event_call, list);
315 * The ftrace subsystem is for showing formats only.
316 * They can not be enabled or disabled via the event files.
324 m->private = list->next;
329 static void *t_start(struct seq_file *m, loff_t *pos)
331 struct ftrace_event_call *call = NULL;
334 mutex_lock(&event_mutex);
336 m->private = ftrace_events.next;
337 for (l = 0; l <= *pos; ) {
338 call = t_next(m, NULL, &l);
346 s_next(struct seq_file *m, void *v, loff_t *pos)
348 struct list_head *list = m->private;
349 struct ftrace_event_call *call;
354 if (list == &ftrace_events)
357 call = list_entry(list, struct ftrace_event_call, list);
359 if (!call->enabled) {
364 m->private = list->next;
369 static void *s_start(struct seq_file *m, loff_t *pos)
371 struct ftrace_event_call *call = NULL;
374 mutex_lock(&event_mutex);
376 m->private = ftrace_events.next;
377 for (l = 0; l <= *pos; ) {
378 call = s_next(m, NULL, &l);
385 static int t_show(struct seq_file *m, void *v)
387 struct ftrace_event_call *call = v;
389 if (strcmp(call->system, TRACE_SYSTEM) != 0)
390 seq_printf(m, "%s:", call->system);
391 seq_printf(m, "%s\n", call->name);
396 static void t_stop(struct seq_file *m, void *p)
398 mutex_unlock(&event_mutex);
402 ftrace_event_seq_open(struct inode *inode, struct file *file)
404 const struct seq_operations *seq_ops;
406 if ((file->f_mode & FMODE_WRITE) &&
407 (file->f_flags & O_TRUNC))
408 ftrace_clear_events();
410 seq_ops = inode->i_private;
411 return seq_open(file, seq_ops);
415 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
418 struct ftrace_event_call *call = filp->private_data;
426 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
430 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
433 struct ftrace_event_call *call = filp->private_data;
438 if (cnt >= sizeof(buf))
441 if (copy_from_user(&buf, ubuf, cnt))
446 ret = strict_strtoul(buf, 10, &val);
450 ret = tracing_update_buffers();
457 mutex_lock(&event_mutex);
458 ftrace_event_enable_disable(call, val);
459 mutex_unlock(&event_mutex);
472 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
475 const char set_to_char[4] = { '?', '0', '1', 'X' };
476 const char *system = filp->private_data;
477 struct ftrace_event_call *call;
482 mutex_lock(&event_mutex);
483 list_for_each_entry(call, &ftrace_events, list) {
484 if (!call->name || !call->regfunc)
487 if (system && strcmp(call->system, system) != 0)
491 * We need to find out if all the events are set
492 * or if all events or cleared, or if we have
495 set |= (1 << !!call->enabled);
498 * If we have a mixture, no need to look further.
503 mutex_unlock(&event_mutex);
505 buf[0] = set_to_char[set];
508 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
514 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
517 const char *system = filp->private_data;
522 if (cnt >= sizeof(buf))
525 if (copy_from_user(&buf, ubuf, cnt))
530 ret = strict_strtoul(buf, 10, &val);
534 ret = tracing_update_buffers();
538 if (val != 0 && val != 1)
541 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
553 extern char *__bad_type_size(void);
556 #define FIELD(type, name) \
557 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
558 #type, "common_" #name, offsetof(typeof(field), name), \
561 static int trace_write_header(struct trace_seq *s)
563 struct trace_entry field;
565 /* struct trace_entry */
566 return trace_seq_printf(s,
567 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
568 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
569 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
570 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
571 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
573 FIELD(unsigned short, type),
574 FIELD(unsigned char, flags),
575 FIELD(unsigned char, preempt_count),
581 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
584 struct ftrace_event_call *call = filp->private_data;
592 s = kmalloc(sizeof(*s), GFP_KERNEL);
598 /* If any of the first writes fail, so will the show_format. */
600 trace_seq_printf(s, "name: %s\n", call->name);
601 trace_seq_printf(s, "ID: %d\n", call->id);
602 trace_seq_printf(s, "format:\n");
603 trace_write_header(s);
605 r = call->show_format(call, s);
608 * ug! The format output is bigger than a PAGE!!
610 buf = "FORMAT TOO BIG\n";
611 r = simple_read_from_buffer(ubuf, cnt, ppos,
616 r = simple_read_from_buffer(ubuf, cnt, ppos,
624 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
626 struct ftrace_event_call *call = filp->private_data;
633 s = kmalloc(sizeof(*s), GFP_KERNEL);
638 trace_seq_printf(s, "%d\n", call->id);
640 r = simple_read_from_buffer(ubuf, cnt, ppos,
647 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
650 struct ftrace_event_call *call = filp->private_data;
657 s = kmalloc(sizeof(*s), GFP_KERNEL);
663 print_event_filter(call, s);
664 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
672 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
675 struct ftrace_event_call *call = filp->private_data;
679 if (cnt >= PAGE_SIZE)
682 buf = (char *)__get_free_page(GFP_TEMPORARY);
686 if (copy_from_user(buf, ubuf, cnt)) {
687 free_page((unsigned long) buf);
692 err = apply_event_filter(call, buf);
693 free_page((unsigned long) buf);
703 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
706 struct event_subsystem *system = filp->private_data;
713 s = kmalloc(sizeof(*s), GFP_KERNEL);
719 print_subsystem_event_filter(system, s);
720 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
728 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
731 struct event_subsystem *system = filp->private_data;
735 if (cnt >= PAGE_SIZE)
738 buf = (char *)__get_free_page(GFP_TEMPORARY);
742 if (copy_from_user(buf, ubuf, cnt)) {
743 free_page((unsigned long) buf);
748 err = apply_subsystem_event_filter(system, buf);
749 free_page((unsigned long) buf);
759 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
761 int (*func)(struct trace_seq *s) = filp->private_data;
768 s = kmalloc(sizeof(*s), GFP_KERNEL);
775 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
782 static const struct seq_operations show_event_seq_ops = {
789 static const struct seq_operations show_set_event_seq_ops = {
796 static const struct file_operations ftrace_avail_fops = {
797 .open = ftrace_event_seq_open,
800 .release = seq_release,
803 static const struct file_operations ftrace_set_event_fops = {
804 .open = ftrace_event_seq_open,
806 .write = ftrace_event_write,
808 .release = seq_release,
811 static const struct file_operations ftrace_enable_fops = {
812 .open = tracing_open_generic,
813 .read = event_enable_read,
814 .write = event_enable_write,
817 static const struct file_operations ftrace_event_format_fops = {
818 .open = tracing_open_generic,
819 .read = event_format_read,
822 static const struct file_operations ftrace_event_id_fops = {
823 .open = tracing_open_generic,
824 .read = event_id_read,
827 static const struct file_operations ftrace_event_filter_fops = {
828 .open = tracing_open_generic,
829 .read = event_filter_read,
830 .write = event_filter_write,
833 static const struct file_operations ftrace_subsystem_filter_fops = {
834 .open = tracing_open_generic,
835 .read = subsystem_filter_read,
836 .write = subsystem_filter_write,
839 static const struct file_operations ftrace_system_enable_fops = {
840 .open = tracing_open_generic,
841 .read = system_enable_read,
842 .write = system_enable_write,
845 static const struct file_operations ftrace_show_header_fops = {
846 .open = tracing_open_generic,
850 static struct dentry *event_trace_events_dir(void)
852 static struct dentry *d_tracer;
853 static struct dentry *d_events;
858 d_tracer = tracing_init_dentry();
862 d_events = debugfs_create_dir("events", d_tracer);
864 pr_warning("Could not create debugfs "
865 "'events' directory\n");
870 static LIST_HEAD(event_subsystems);
872 static struct dentry *
873 event_subsystem_dir(const char *name, struct dentry *d_events)
875 struct event_subsystem *system;
876 struct dentry *entry;
878 /* First see if we did not already create this dir */
879 list_for_each_entry(system, &event_subsystems, list) {
880 if (strcmp(system->name, name) == 0) {
882 return system->entry;
886 /* need to create new entry */
887 system = kmalloc(sizeof(*system), GFP_KERNEL);
889 pr_warning("No memory to create event subsystem %s\n",
894 system->entry = debugfs_create_dir(name, d_events);
895 if (!system->entry) {
896 pr_warning("Could not create event subsystem %s\n",
902 system->nr_events = 1;
903 system->name = kstrdup(name, GFP_KERNEL);
905 debugfs_remove(system->entry);
910 list_add(&system->list, &event_subsystems);
912 system->filter = NULL;
914 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
915 if (!system->filter) {
916 pr_warning("Could not allocate filter for subsystem "
918 return system->entry;
921 entry = debugfs_create_file("filter", 0644, system->entry, system,
922 &ftrace_subsystem_filter_fops);
924 kfree(system->filter);
925 system->filter = NULL;
926 pr_warning("Could not create debugfs "
927 "'%s/filter' entry\n", name);
930 entry = trace_create_file("enable", 0644, system->entry,
931 (void *)system->name,
932 &ftrace_system_enable_fops);
934 return system->entry;
938 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
939 const struct file_operations *id,
940 const struct file_operations *enable,
941 const struct file_operations *filter,
942 const struct file_operations *format)
944 struct dentry *entry;
948 * If the trace point header did not define TRACE_SYSTEM
949 * then the system would be called "TRACE_SYSTEM".
951 if (strcmp(call->system, TRACE_SYSTEM) != 0)
952 d_events = event_subsystem_dir(call->system, d_events);
954 call->dir = debugfs_create_dir(call->name, d_events);
956 pr_warning("Could not create debugfs "
957 "'%s' directory\n", call->name);
962 entry = trace_create_file("enable", 0644, call->dir, call,
965 if (call->id && call->profile_enable)
966 entry = trace_create_file("id", 0444, call->dir, call,
969 if (call->define_fields) {
970 ret = call->define_fields(call);
972 pr_warning("Could not initialize trace point"
973 " events/%s\n", call->name);
976 entry = trace_create_file("filter", 0644, call->dir, call,
980 /* A trace may not want to export its format */
981 if (!call->show_format)
984 entry = trace_create_file("format", 0444, call->dir, call,
990 static int __trace_add_event_call(struct ftrace_event_call *call)
992 struct dentry *d_events;
998 if (call->raw_init) {
999 ret = call->raw_init(call);
1002 pr_warning("Could not initialize trace "
1003 "events/%s\n", call->name);
1008 d_events = event_trace_events_dir();
1012 list_add(&call->list, &ftrace_events);
1013 return event_create_dir(call, d_events, &ftrace_event_id_fops,
1014 &ftrace_enable_fops, &ftrace_event_filter_fops,
1015 &ftrace_event_format_fops);
1018 /* Add an additional event_call dynamically */
1019 int trace_add_event_call(struct ftrace_event_call *call)
1022 mutex_lock(&event_mutex);
1023 ret = __trace_add_event_call(call);
1024 mutex_unlock(&event_mutex);
1028 static void remove_subsystem_dir(const char *name)
1030 struct event_subsystem *system;
1032 if (strcmp(name, TRACE_SYSTEM) == 0)
1035 list_for_each_entry(system, &event_subsystems, list) {
1036 if (strcmp(system->name, name) == 0) {
1037 if (!--system->nr_events) {
1038 struct event_filter *filter = system->filter;
1040 debugfs_remove_recursive(system->entry);
1041 list_del(&system->list);
1043 kfree(filter->filter_string);
1046 kfree(system->name);
1054 static void __trace_remove_event_call(struct ftrace_event_call *call)
1056 ftrace_event_enable_disable(call, 0);
1058 __unregister_ftrace_event(call->event);
1059 debugfs_remove_recursive(call->dir);
1060 list_del(&call->list);
1061 trace_destroy_fields(call);
1062 destroy_preds(call);
1063 remove_subsystem_dir(call->system);
1066 /* Remove an event_call */
1067 void trace_remove_event_call(struct ftrace_event_call *call)
1069 mutex_lock(&event_mutex);
1070 __trace_remove_event_call(call);
1071 mutex_unlock(&event_mutex);
1074 #define for_each_event(event, start, end) \
1075 for (event = start; \
1076 (unsigned long)event < (unsigned long)end; \
1079 #ifdef CONFIG_MODULES
1081 static LIST_HEAD(ftrace_module_file_list);
1084 * Modules must own their file_operations to keep up with
1085 * reference counting.
1087 struct ftrace_module_file_ops {
1088 struct list_head list;
1090 struct file_operations id;
1091 struct file_operations enable;
1092 struct file_operations format;
1093 struct file_operations filter;
1096 static struct ftrace_module_file_ops *
1097 trace_create_file_ops(struct module *mod)
1099 struct ftrace_module_file_ops *file_ops;
1102 * This is a bit of a PITA. To allow for correct reference
1103 * counting, modules must "own" their file_operations.
1104 * To do this, we allocate the file operations that will be
1105 * used in the event directory.
1108 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1112 file_ops->mod = mod;
1114 file_ops->id = ftrace_event_id_fops;
1115 file_ops->id.owner = mod;
1117 file_ops->enable = ftrace_enable_fops;
1118 file_ops->enable.owner = mod;
1120 file_ops->filter = ftrace_event_filter_fops;
1121 file_ops->filter.owner = mod;
1123 file_ops->format = ftrace_event_format_fops;
1124 file_ops->format.owner = mod;
1126 list_add(&file_ops->list, &ftrace_module_file_list);
1131 static void trace_module_add_events(struct module *mod)
1133 struct ftrace_module_file_ops *file_ops = NULL;
1134 struct ftrace_event_call *call, *start, *end;
1135 struct dentry *d_events;
1138 start = mod->trace_events;
1139 end = mod->trace_events + mod->num_trace_events;
1144 d_events = event_trace_events_dir();
1148 for_each_event(call, start, end) {
1149 /* The linker may leave blanks */
1152 if (call->raw_init) {
1153 ret = call->raw_init(call);
1156 pr_warning("Could not initialize trace "
1157 "point events/%s\n", call->name);
1162 * This module has events, create file ops for this module
1163 * if not already done.
1166 file_ops = trace_create_file_ops(mod);
1171 list_add(&call->list, &ftrace_events);
1172 event_create_dir(call, d_events,
1173 &file_ops->id, &file_ops->enable,
1174 &file_ops->filter, &file_ops->format);
1178 static void trace_module_remove_events(struct module *mod)
1180 struct ftrace_module_file_ops *file_ops;
1181 struct ftrace_event_call *call, *p;
1184 down_write(&trace_event_mutex);
1185 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1186 if (call->mod == mod) {
1188 __trace_remove_event_call(call);
1192 /* Now free the file_operations */
1193 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1194 if (file_ops->mod == mod)
1197 if (&file_ops->list != &ftrace_module_file_list) {
1198 list_del(&file_ops->list);
1203 * It is safest to reset the ring buffer if the module being unloaded
1204 * registered any events.
1207 tracing_reset_current_online_cpus();
1208 up_write(&trace_event_mutex);
1211 static int trace_module_notify(struct notifier_block *self,
1212 unsigned long val, void *data)
1214 struct module *mod = data;
1216 mutex_lock(&event_mutex);
1218 case MODULE_STATE_COMING:
1219 trace_module_add_events(mod);
1221 case MODULE_STATE_GOING:
1222 trace_module_remove_events(mod);
1225 mutex_unlock(&event_mutex);
1230 static int trace_module_notify(struct notifier_block *self,
1231 unsigned long val, void *data)
1235 #endif /* CONFIG_MODULES */
1237 struct notifier_block trace_module_nb = {
1238 .notifier_call = trace_module_notify,
1242 extern struct ftrace_event_call __start_ftrace_events[];
1243 extern struct ftrace_event_call __stop_ftrace_events[];
1245 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1247 static __init int setup_trace_event(char *str)
1249 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1250 ring_buffer_expanded = 1;
1251 tracing_selftest_disabled = 1;
1255 __setup("trace_event=", setup_trace_event);
1257 static __init int event_trace_init(void)
1259 struct ftrace_event_call *call;
1260 struct dentry *d_tracer;
1261 struct dentry *entry;
1262 struct dentry *d_events;
1264 char *buf = bootup_event_buf;
1267 d_tracer = tracing_init_dentry();
1271 entry = debugfs_create_file("available_events", 0444, d_tracer,
1272 (void *)&show_event_seq_ops,
1273 &ftrace_avail_fops);
1275 pr_warning("Could not create debugfs "
1276 "'available_events' entry\n");
1278 entry = debugfs_create_file("set_event", 0644, d_tracer,
1279 (void *)&show_set_event_seq_ops,
1280 &ftrace_set_event_fops);
1282 pr_warning("Could not create debugfs "
1283 "'set_event' entry\n");
1285 d_events = event_trace_events_dir();
1289 /* ring buffer internal formats */
1290 trace_create_file("header_page", 0444, d_events,
1291 ring_buffer_print_page_header,
1292 &ftrace_show_header_fops);
1294 trace_create_file("header_event", 0444, d_events,
1295 ring_buffer_print_entry_header,
1296 &ftrace_show_header_fops);
1298 trace_create_file("enable", 0644, d_events,
1299 NULL, &ftrace_system_enable_fops);
1301 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1302 /* The linker may leave blanks */
1305 if (call->raw_init) {
1306 ret = call->raw_init(call);
1309 pr_warning("Could not initialize trace "
1310 "point events/%s\n", call->name);
1314 list_add(&call->list, &ftrace_events);
1315 event_create_dir(call, d_events, &ftrace_event_id_fops,
1316 &ftrace_enable_fops, &ftrace_event_filter_fops,
1317 &ftrace_event_format_fops);
1321 token = strsep(&buf, ",");
1328 ret = ftrace_set_clr_event(token, 1);
1330 pr_warning("Failed to enable trace event: %s\n", token);
1333 ret = register_module_notifier(&trace_module_nb);
1335 pr_warning("Failed to register trace events module notifier\n");
1339 fs_initcall(event_trace_init);
1341 #ifdef CONFIG_FTRACE_STARTUP_TEST
1343 static DEFINE_SPINLOCK(test_spinlock);
1344 static DEFINE_SPINLOCK(test_spinlock_irq);
1345 static DEFINE_MUTEX(test_mutex);
1347 static __init void test_work(struct work_struct *dummy)
1349 spin_lock(&test_spinlock);
1350 spin_lock_irq(&test_spinlock_irq);
1352 spin_unlock_irq(&test_spinlock_irq);
1353 spin_unlock(&test_spinlock);
1355 mutex_lock(&test_mutex);
1357 mutex_unlock(&test_mutex);
1360 static __init int event_test_thread(void *unused)
1364 test_malloc = kmalloc(1234, GFP_KERNEL);
1366 pr_info("failed to kmalloc\n");
1368 schedule_on_each_cpu(test_work);
1372 set_current_state(TASK_INTERRUPTIBLE);
1373 while (!kthread_should_stop())
1380 * Do various things that may trigger events.
1382 static __init void event_test_stuff(void)
1384 struct task_struct *test_thread;
1386 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1388 kthread_stop(test_thread);
1392 * For every trace event defined, we will test each trace point separately,
1393 * and then by groups, and finally all trace points.
1395 static __init void event_trace_self_tests(void)
1397 struct ftrace_event_call *call;
1398 struct event_subsystem *system;
1401 pr_info("Running tests on trace events:\n");
1403 list_for_each_entry(call, &ftrace_events, list) {
1405 /* Only test those that have a regfunc */
1409 pr_info("Testing event %s: ", call->name);
1412 * If an event is already enabled, someone is using
1413 * it and the self test should not be on.
1415 if (call->enabled) {
1416 pr_warning("Enabled event during self test!\n");
1421 ftrace_event_enable_disable(call, 1);
1423 ftrace_event_enable_disable(call, 0);
1428 /* Now test at the sub system level */
1430 pr_info("Running tests on trace event systems:\n");
1432 list_for_each_entry(system, &event_subsystems, list) {
1434 /* the ftrace system is special, skip it */
1435 if (strcmp(system->name, "ftrace") == 0)
1438 pr_info("Testing event system %s: ", system->name);
1440 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1441 if (WARN_ON_ONCE(ret)) {
1442 pr_warning("error enabling system %s\n",
1449 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1450 if (WARN_ON_ONCE(ret))
1451 pr_warning("error disabling system %s\n",
1457 /* Test with all events enabled */
1459 pr_info("Running tests on all trace events:\n");
1460 pr_info("Testing all events: ");
1462 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1463 if (WARN_ON_ONCE(ret)) {
1464 pr_warning("error enabling all events\n");
1471 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1472 if (WARN_ON_ONCE(ret)) {
1473 pr_warning("error disabling all events\n");
1480 #ifdef CONFIG_FUNCTION_TRACER
1482 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1485 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1487 struct ring_buffer_event *event;
1488 struct ftrace_entry *entry;
1489 unsigned long flags;
1495 pc = preempt_count();
1496 resched = ftrace_preempt_disable();
1497 cpu = raw_smp_processor_id();
1498 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1503 local_save_flags(flags);
1505 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1509 entry = ring_buffer_event_data(event);
1511 entry->parent_ip = parent_ip;
1513 trace_nowake_buffer_unlock_commit(event, flags, pc);
1516 atomic_dec(&per_cpu(test_event_disable, cpu));
1517 ftrace_preempt_enable(resched);
1520 static struct ftrace_ops trace_ops __initdata =
1522 .func = function_test_events_call,
1525 static __init void event_trace_self_test_with_function(void)
1527 register_ftrace_function(&trace_ops);
1528 pr_info("Running tests again, along with the function tracer\n");
1529 event_trace_self_tests();
1530 unregister_ftrace_function(&trace_ops);
1533 static __init void event_trace_self_test_with_function(void)
1538 static __init int event_trace_self_tests_init(void)
1540 if (!tracing_selftest_disabled) {
1541 event_trace_self_tests();
1542 event_trace_self_test_with_function();
1548 late_initcall(event_trace_self_tests_init);