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 static 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);
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 int trace_event_raw_init(struct ftrace_event_call *call)
111 id = register_ftrace_event(call->event);
115 INIT_LIST_HEAD(&call->fields);
119 EXPORT_SYMBOL_GPL(trace_event_raw_init);
121 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
128 tracing_stop_cmdline_record();
129 call->unregfunc(call);
133 if (!call->enabled) {
135 tracing_start_cmdline_record();
142 static void ftrace_clear_events(void)
144 struct ftrace_event_call *call;
146 mutex_lock(&event_mutex);
147 list_for_each_entry(call, &ftrace_events, list) {
148 ftrace_event_enable_disable(call, 0);
150 mutex_unlock(&event_mutex);
154 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
156 static int __ftrace_set_clr_event(const char *match, const char *sub,
157 const char *event, int set)
159 struct ftrace_event_call *call;
162 mutex_lock(&event_mutex);
163 list_for_each_entry(call, &ftrace_events, list) {
165 if (!call->name || !call->regfunc)
169 strcmp(match, call->name) != 0 &&
170 strcmp(match, call->system) != 0)
173 if (sub && strcmp(sub, call->system) != 0)
176 if (event && strcmp(event, call->name) != 0)
179 ftrace_event_enable_disable(call, set);
183 mutex_unlock(&event_mutex);
188 static int ftrace_set_clr_event(char *buf, int set)
190 char *event = NULL, *sub = NULL, *match;
193 * The buf format can be <subsystem>:<event-name>
194 * *:<event-name> means any event by that name.
195 * :<event-name> is the same.
197 * <subsystem>:* means all events in that subsystem
198 * <subsystem>: means the same.
200 * <name> (no ':') means all events in a subsystem with
201 * the name <name> or any event that matches <name>
204 match = strsep(&buf, ":");
210 if (!strlen(sub) || strcmp(sub, "*") == 0)
212 if (!strlen(event) || strcmp(event, "*") == 0)
216 return __ftrace_set_clr_event(match, sub, event, set);
220 * trace_set_clr_event - enable or disable an event
221 * @system: system name to match (NULL for any system)
222 * @event: event name to match (NULL for all events, within system)
223 * @set: 1 to enable, 0 to disable
225 * This is a way for other parts of the kernel to enable or disable
228 * Returns 0 on success, -EINVAL if the parameters do not match any
231 int trace_set_clr_event(const char *system, const char *event, int set)
233 return __ftrace_set_clr_event(NULL, system, event, set);
236 /* 128 should be much more than enough */
237 #define EVENT_BUF_SIZE 127
240 ftrace_event_write(struct file *file, const char __user *ubuf,
241 size_t cnt, loff_t *ppos)
243 struct trace_parser parser;
249 ret = tracing_update_buffers();
253 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
256 read = trace_get_user(&parser, ubuf, cnt, ppos);
258 if (read >= 0 && trace_parser_loaded((&parser))) {
261 if (*parser.buffer == '!')
264 parser.buffer[parser.idx] = 0;
266 ret = ftrace_set_clr_event(parser.buffer + !set, set);
274 trace_parser_put(&parser);
280 t_next(struct seq_file *m, void *v, loff_t *pos)
282 struct ftrace_event_call *call = v;
286 list_for_each_entry_continue(call, &ftrace_events, list) {
288 * The ftrace subsystem is for showing formats only.
289 * They can not be enabled or disabled via the event files.
298 static void *t_start(struct seq_file *m, loff_t *pos)
300 struct ftrace_event_call *call;
303 mutex_lock(&event_mutex);
305 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
306 for (l = 0; l <= *pos; ) {
307 call = t_next(m, call, &l);
315 s_next(struct seq_file *m, void *v, loff_t *pos)
317 struct ftrace_event_call *call = v;
321 list_for_each_entry_continue(call, &ftrace_events, list) {
329 static void *s_start(struct seq_file *m, loff_t *pos)
331 struct ftrace_event_call *call;
334 mutex_lock(&event_mutex);
336 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
337 for (l = 0; l <= *pos; ) {
338 call = s_next(m, call, &l);
345 static int t_show(struct seq_file *m, void *v)
347 struct ftrace_event_call *call = v;
349 if (strcmp(call->system, TRACE_SYSTEM) != 0)
350 seq_printf(m, "%s:", call->system);
351 seq_printf(m, "%s\n", call->name);
356 static void t_stop(struct seq_file *m, void *p)
358 mutex_unlock(&event_mutex);
362 ftrace_event_seq_open(struct inode *inode, struct file *file)
364 const struct seq_operations *seq_ops;
366 if ((file->f_mode & FMODE_WRITE) &&
367 (file->f_flags & O_TRUNC))
368 ftrace_clear_events();
370 seq_ops = inode->i_private;
371 return seq_open(file, seq_ops);
375 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
378 struct ftrace_event_call *call = filp->private_data;
386 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
390 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
393 struct ftrace_event_call *call = filp->private_data;
398 if (cnt >= sizeof(buf))
401 if (copy_from_user(&buf, ubuf, cnt))
406 ret = strict_strtoul(buf, 10, &val);
410 ret = tracing_update_buffers();
417 mutex_lock(&event_mutex);
418 ftrace_event_enable_disable(call, val);
419 mutex_unlock(&event_mutex);
432 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
435 const char set_to_char[4] = { '?', '0', '1', 'X' };
436 const char *system = filp->private_data;
437 struct ftrace_event_call *call;
442 mutex_lock(&event_mutex);
443 list_for_each_entry(call, &ftrace_events, list) {
444 if (!call->name || !call->regfunc)
447 if (system && strcmp(call->system, system) != 0)
451 * We need to find out if all the events are set
452 * or if all events or cleared, or if we have
455 set |= (1 << !!call->enabled);
458 * If we have a mixture, no need to look further.
463 mutex_unlock(&event_mutex);
465 buf[0] = set_to_char[set];
468 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
474 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
477 const char *system = filp->private_data;
482 if (cnt >= sizeof(buf))
485 if (copy_from_user(&buf, ubuf, cnt))
490 ret = strict_strtoul(buf, 10, &val);
494 ret = tracing_update_buffers();
498 if (val != 0 && val != 1)
501 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
513 extern char *__bad_type_size(void);
516 #define FIELD(type, name) \
517 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
518 #type, "common_" #name, offsetof(typeof(field), name), \
519 sizeof(field.name), is_signed_type(type)
521 static int trace_write_header(struct trace_seq *s)
523 struct trace_entry field;
525 /* struct trace_entry */
526 return trace_seq_printf(s,
527 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\tsigned:%u;\n"
528 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\tsigned:%u;\n"
529 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\tsigned:%u;\n"
530 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\tsigned:%u;\n"
531 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\tsigned:%u;\n"
533 FIELD(unsigned short, type),
534 FIELD(unsigned char, flags),
535 FIELD(unsigned char, preempt_count),
537 FIELD(int, lock_depth));
541 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
544 struct ftrace_event_call *call = filp->private_data;
552 s = kmalloc(sizeof(*s), GFP_KERNEL);
558 /* If any of the first writes fail, so will the show_format. */
560 trace_seq_printf(s, "name: %s\n", call->name);
561 trace_seq_printf(s, "ID: %d\n", call->id);
562 trace_seq_printf(s, "format:\n");
563 trace_write_header(s);
565 r = call->show_format(call, s);
568 * ug! The format output is bigger than a PAGE!!
570 buf = "FORMAT TOO BIG\n";
571 r = simple_read_from_buffer(ubuf, cnt, ppos,
576 r = simple_read_from_buffer(ubuf, cnt, ppos,
584 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
586 struct ftrace_event_call *call = filp->private_data;
593 s = kmalloc(sizeof(*s), GFP_KERNEL);
598 trace_seq_printf(s, "%d\n", call->id);
600 r = simple_read_from_buffer(ubuf, cnt, ppos,
607 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
610 struct ftrace_event_call *call = filp->private_data;
617 s = kmalloc(sizeof(*s), GFP_KERNEL);
623 print_event_filter(call, s);
624 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
632 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
635 struct ftrace_event_call *call = filp->private_data;
639 if (cnt >= PAGE_SIZE)
642 buf = (char *)__get_free_page(GFP_TEMPORARY);
646 if (copy_from_user(buf, ubuf, cnt)) {
647 free_page((unsigned long) buf);
652 err = apply_event_filter(call, buf);
653 free_page((unsigned long) buf);
663 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
666 struct event_subsystem *system = filp->private_data;
673 s = kmalloc(sizeof(*s), GFP_KERNEL);
679 print_subsystem_event_filter(system, s);
680 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
688 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
691 struct event_subsystem *system = filp->private_data;
695 if (cnt >= PAGE_SIZE)
698 buf = (char *)__get_free_page(GFP_TEMPORARY);
702 if (copy_from_user(buf, ubuf, cnt)) {
703 free_page((unsigned long) buf);
708 err = apply_subsystem_event_filter(system, buf);
709 free_page((unsigned long) buf);
719 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
721 int (*func)(struct trace_seq *s) = filp->private_data;
728 s = kmalloc(sizeof(*s), GFP_KERNEL);
735 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
742 static const struct seq_operations show_event_seq_ops = {
749 static const struct seq_operations show_set_event_seq_ops = {
756 static const struct file_operations ftrace_avail_fops = {
757 .open = ftrace_event_seq_open,
760 .release = seq_release,
763 static const struct file_operations ftrace_set_event_fops = {
764 .open = ftrace_event_seq_open,
766 .write = ftrace_event_write,
768 .release = seq_release,
771 static const struct file_operations ftrace_enable_fops = {
772 .open = tracing_open_generic,
773 .read = event_enable_read,
774 .write = event_enable_write,
777 static const struct file_operations ftrace_event_format_fops = {
778 .open = tracing_open_generic,
779 .read = event_format_read,
782 static const struct file_operations ftrace_event_id_fops = {
783 .open = tracing_open_generic,
784 .read = event_id_read,
787 static const struct file_operations ftrace_event_filter_fops = {
788 .open = tracing_open_generic,
789 .read = event_filter_read,
790 .write = event_filter_write,
793 static const struct file_operations ftrace_subsystem_filter_fops = {
794 .open = tracing_open_generic,
795 .read = subsystem_filter_read,
796 .write = subsystem_filter_write,
799 static const struct file_operations ftrace_system_enable_fops = {
800 .open = tracing_open_generic,
801 .read = system_enable_read,
802 .write = system_enable_write,
805 static const struct file_operations ftrace_show_header_fops = {
806 .open = tracing_open_generic,
810 static struct dentry *event_trace_events_dir(void)
812 static struct dentry *d_tracer;
813 static struct dentry *d_events;
818 d_tracer = tracing_init_dentry();
822 d_events = debugfs_create_dir("events", d_tracer);
824 pr_warning("Could not create debugfs "
825 "'events' directory\n");
830 static LIST_HEAD(event_subsystems);
832 static struct dentry *
833 event_subsystem_dir(const char *name, struct dentry *d_events)
835 struct event_subsystem *system;
836 struct dentry *entry;
838 /* First see if we did not already create this dir */
839 list_for_each_entry(system, &event_subsystems, list) {
840 if (strcmp(system->name, name) == 0) {
842 return system->entry;
846 /* need to create new entry */
847 system = kmalloc(sizeof(*system), GFP_KERNEL);
849 pr_warning("No memory to create event subsystem %s\n",
854 system->entry = debugfs_create_dir(name, d_events);
855 if (!system->entry) {
856 pr_warning("Could not create event subsystem %s\n",
862 system->nr_events = 1;
863 system->name = kstrdup(name, GFP_KERNEL);
865 debugfs_remove(system->entry);
870 list_add(&system->list, &event_subsystems);
872 system->filter = NULL;
874 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
875 if (!system->filter) {
876 pr_warning("Could not allocate filter for subsystem "
878 return system->entry;
881 entry = debugfs_create_file("filter", 0644, system->entry, system,
882 &ftrace_subsystem_filter_fops);
884 kfree(system->filter);
885 system->filter = NULL;
886 pr_warning("Could not create debugfs "
887 "'%s/filter' entry\n", name);
890 trace_create_file("enable", 0644, system->entry,
891 (void *)system->name,
892 &ftrace_system_enable_fops);
894 return system->entry;
898 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
899 const struct file_operations *id,
900 const struct file_operations *enable,
901 const struct file_operations *filter,
902 const struct file_operations *format)
907 * If the trace point header did not define TRACE_SYSTEM
908 * then the system would be called "TRACE_SYSTEM".
910 if (strcmp(call->system, TRACE_SYSTEM) != 0)
911 d_events = event_subsystem_dir(call->system, d_events);
913 call->dir = debugfs_create_dir(call->name, d_events);
915 pr_warning("Could not create debugfs "
916 "'%s' directory\n", call->name);
921 trace_create_file("enable", 0644, call->dir, call,
924 if (call->id && call->profile_enable)
925 trace_create_file("id", 0444, call->dir, call,
928 if (call->define_fields) {
929 ret = trace_define_common_fields(call);
931 ret = call->define_fields(call);
933 pr_warning("Could not initialize trace point"
934 " events/%s\n", call->name);
937 trace_create_file("filter", 0644, call->dir, call,
941 /* A trace may not want to export its format */
942 if (!call->show_format)
945 trace_create_file("format", 0444, call->dir, call,
951 static int __trace_add_event_call(struct ftrace_event_call *call)
953 struct dentry *d_events;
959 if (call->raw_init) {
960 ret = call->raw_init(call);
963 pr_warning("Could not initialize trace "
964 "events/%s\n", call->name);
969 d_events = event_trace_events_dir();
973 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
974 &ftrace_enable_fops, &ftrace_event_filter_fops,
975 &ftrace_event_format_fops);
977 list_add(&call->list, &ftrace_events);
982 /* Add an additional event_call dynamically */
983 int trace_add_event_call(struct ftrace_event_call *call)
986 mutex_lock(&event_mutex);
987 ret = __trace_add_event_call(call);
988 mutex_unlock(&event_mutex);
992 static void remove_subsystem_dir(const char *name)
994 struct event_subsystem *system;
996 if (strcmp(name, TRACE_SYSTEM) == 0)
999 list_for_each_entry(system, &event_subsystems, list) {
1000 if (strcmp(system->name, name) == 0) {
1001 if (!--system->nr_events) {
1002 struct event_filter *filter = system->filter;
1004 debugfs_remove_recursive(system->entry);
1005 list_del(&system->list);
1007 kfree(filter->filter_string);
1010 kfree(system->name);
1019 * Must be called under locking both of event_mutex and trace_event_mutex.
1021 static void __trace_remove_event_call(struct ftrace_event_call *call)
1023 ftrace_event_enable_disable(call, 0);
1025 __unregister_ftrace_event(call->event);
1026 debugfs_remove_recursive(call->dir);
1027 list_del(&call->list);
1028 trace_destroy_fields(call);
1029 destroy_preds(call);
1030 remove_subsystem_dir(call->system);
1033 /* Remove an event_call */
1034 void trace_remove_event_call(struct ftrace_event_call *call)
1036 mutex_lock(&event_mutex);
1037 down_write(&trace_event_mutex);
1038 __trace_remove_event_call(call);
1039 up_write(&trace_event_mutex);
1040 mutex_unlock(&event_mutex);
1043 #define for_each_event(event, start, end) \
1044 for (event = start; \
1045 (unsigned long)event < (unsigned long)end; \
1048 #ifdef CONFIG_MODULES
1050 static LIST_HEAD(ftrace_module_file_list);
1053 * Modules must own their file_operations to keep up with
1054 * reference counting.
1056 struct ftrace_module_file_ops {
1057 struct list_head list;
1059 struct file_operations id;
1060 struct file_operations enable;
1061 struct file_operations format;
1062 struct file_operations filter;
1065 static struct ftrace_module_file_ops *
1066 trace_create_file_ops(struct module *mod)
1068 struct ftrace_module_file_ops *file_ops;
1071 * This is a bit of a PITA. To allow for correct reference
1072 * counting, modules must "own" their file_operations.
1073 * To do this, we allocate the file operations that will be
1074 * used in the event directory.
1077 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1081 file_ops->mod = mod;
1083 file_ops->id = ftrace_event_id_fops;
1084 file_ops->id.owner = mod;
1086 file_ops->enable = ftrace_enable_fops;
1087 file_ops->enable.owner = mod;
1089 file_ops->filter = ftrace_event_filter_fops;
1090 file_ops->filter.owner = mod;
1092 file_ops->format = ftrace_event_format_fops;
1093 file_ops->format.owner = mod;
1095 list_add(&file_ops->list, &ftrace_module_file_list);
1100 static void trace_module_add_events(struct module *mod)
1102 struct ftrace_module_file_ops *file_ops = NULL;
1103 struct ftrace_event_call *call, *start, *end;
1104 struct dentry *d_events;
1107 start = mod->trace_events;
1108 end = mod->trace_events + mod->num_trace_events;
1113 d_events = event_trace_events_dir();
1117 for_each_event(call, start, end) {
1118 /* The linker may leave blanks */
1121 if (call->raw_init) {
1122 ret = call->raw_init(call);
1125 pr_warning("Could not initialize trace "
1126 "point events/%s\n", call->name);
1131 * This module has events, create file ops for this module
1132 * if not already done.
1135 file_ops = trace_create_file_ops(mod);
1140 ret = event_create_dir(call, d_events,
1141 &file_ops->id, &file_ops->enable,
1142 &file_ops->filter, &file_ops->format);
1144 list_add(&call->list, &ftrace_events);
1148 static void trace_module_remove_events(struct module *mod)
1150 struct ftrace_module_file_ops *file_ops;
1151 struct ftrace_event_call *call, *p;
1154 down_write(&trace_event_mutex);
1155 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1156 if (call->mod == mod) {
1158 __trace_remove_event_call(call);
1162 /* Now free the file_operations */
1163 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1164 if (file_ops->mod == mod)
1167 if (&file_ops->list != &ftrace_module_file_list) {
1168 list_del(&file_ops->list);
1173 * It is safest to reset the ring buffer if the module being unloaded
1174 * registered any events.
1177 tracing_reset_current_online_cpus();
1178 up_write(&trace_event_mutex);
1181 static int trace_module_notify(struct notifier_block *self,
1182 unsigned long val, void *data)
1184 struct module *mod = data;
1186 mutex_lock(&event_mutex);
1188 case MODULE_STATE_COMING:
1189 trace_module_add_events(mod);
1191 case MODULE_STATE_GOING:
1192 trace_module_remove_events(mod);
1195 mutex_unlock(&event_mutex);
1200 static int trace_module_notify(struct notifier_block *self,
1201 unsigned long val, void *data)
1205 #endif /* CONFIG_MODULES */
1207 static struct notifier_block trace_module_nb = {
1208 .notifier_call = trace_module_notify,
1212 extern struct ftrace_event_call __start_ftrace_events[];
1213 extern struct ftrace_event_call __stop_ftrace_events[];
1215 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1217 static __init int setup_trace_event(char *str)
1219 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1220 ring_buffer_expanded = 1;
1221 tracing_selftest_disabled = 1;
1225 __setup("trace_event=", setup_trace_event);
1227 static __init int event_trace_init(void)
1229 struct ftrace_event_call *call;
1230 struct dentry *d_tracer;
1231 struct dentry *entry;
1232 struct dentry *d_events;
1234 char *buf = bootup_event_buf;
1237 d_tracer = tracing_init_dentry();
1241 entry = debugfs_create_file("available_events", 0444, d_tracer,
1242 (void *)&show_event_seq_ops,
1243 &ftrace_avail_fops);
1245 pr_warning("Could not create debugfs "
1246 "'available_events' entry\n");
1248 entry = debugfs_create_file("set_event", 0644, d_tracer,
1249 (void *)&show_set_event_seq_ops,
1250 &ftrace_set_event_fops);
1252 pr_warning("Could not create debugfs "
1253 "'set_event' entry\n");
1255 d_events = event_trace_events_dir();
1259 /* ring buffer internal formats */
1260 trace_create_file("header_page", 0444, d_events,
1261 ring_buffer_print_page_header,
1262 &ftrace_show_header_fops);
1264 trace_create_file("header_event", 0444, d_events,
1265 ring_buffer_print_entry_header,
1266 &ftrace_show_header_fops);
1268 trace_create_file("enable", 0644, d_events,
1269 NULL, &ftrace_system_enable_fops);
1271 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1272 /* The linker may leave blanks */
1275 if (call->raw_init) {
1276 ret = call->raw_init(call);
1279 pr_warning("Could not initialize trace "
1280 "point events/%s\n", call->name);
1284 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1285 &ftrace_enable_fops,
1286 &ftrace_event_filter_fops,
1287 &ftrace_event_format_fops);
1289 list_add(&call->list, &ftrace_events);
1293 token = strsep(&buf, ",");
1300 ret = ftrace_set_clr_event(token, 1);
1302 pr_warning("Failed to enable trace event: %s\n", token);
1305 ret = register_module_notifier(&trace_module_nb);
1307 pr_warning("Failed to register trace events module notifier\n");
1311 fs_initcall(event_trace_init);
1313 #ifdef CONFIG_FTRACE_STARTUP_TEST
1315 static DEFINE_SPINLOCK(test_spinlock);
1316 static DEFINE_SPINLOCK(test_spinlock_irq);
1317 static DEFINE_MUTEX(test_mutex);
1319 static __init void test_work(struct work_struct *dummy)
1321 spin_lock(&test_spinlock);
1322 spin_lock_irq(&test_spinlock_irq);
1324 spin_unlock_irq(&test_spinlock_irq);
1325 spin_unlock(&test_spinlock);
1327 mutex_lock(&test_mutex);
1329 mutex_unlock(&test_mutex);
1332 static __init int event_test_thread(void *unused)
1336 test_malloc = kmalloc(1234, GFP_KERNEL);
1338 pr_info("failed to kmalloc\n");
1340 schedule_on_each_cpu(test_work);
1344 set_current_state(TASK_INTERRUPTIBLE);
1345 while (!kthread_should_stop())
1352 * Do various things that may trigger events.
1354 static __init void event_test_stuff(void)
1356 struct task_struct *test_thread;
1358 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1360 kthread_stop(test_thread);
1364 * For every trace event defined, we will test each trace point separately,
1365 * and then by groups, and finally all trace points.
1367 static __init void event_trace_self_tests(void)
1369 struct ftrace_event_call *call;
1370 struct event_subsystem *system;
1373 pr_info("Running tests on trace events:\n");
1375 list_for_each_entry(call, &ftrace_events, list) {
1377 /* Only test those that have a regfunc */
1382 * Testing syscall events here is pretty useless, but
1383 * we still do it if configured. But this is time consuming.
1384 * What we really need is a user thread to perform the
1385 * syscalls as we test.
1387 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1389 strcmp(call->system, "syscalls") == 0)
1393 pr_info("Testing event %s: ", call->name);
1396 * If an event is already enabled, someone is using
1397 * it and the self test should not be on.
1399 if (call->enabled) {
1400 pr_warning("Enabled event during self test!\n");
1405 ftrace_event_enable_disable(call, 1);
1407 ftrace_event_enable_disable(call, 0);
1412 /* Now test at the sub system level */
1414 pr_info("Running tests on trace event systems:\n");
1416 list_for_each_entry(system, &event_subsystems, list) {
1418 /* the ftrace system is special, skip it */
1419 if (strcmp(system->name, "ftrace") == 0)
1422 pr_info("Testing event system %s: ", system->name);
1424 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1425 if (WARN_ON_ONCE(ret)) {
1426 pr_warning("error enabling system %s\n",
1433 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1434 if (WARN_ON_ONCE(ret))
1435 pr_warning("error disabling system %s\n",
1441 /* Test with all events enabled */
1443 pr_info("Running tests on all trace events:\n");
1444 pr_info("Testing all events: ");
1446 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1447 if (WARN_ON_ONCE(ret)) {
1448 pr_warning("error enabling all events\n");
1455 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1456 if (WARN_ON_ONCE(ret)) {
1457 pr_warning("error disabling all events\n");
1464 #ifdef CONFIG_FUNCTION_TRACER
1466 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1469 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1471 struct ring_buffer_event *event;
1472 struct ring_buffer *buffer;
1473 struct ftrace_entry *entry;
1474 unsigned long flags;
1480 pc = preempt_count();
1481 resched = ftrace_preempt_disable();
1482 cpu = raw_smp_processor_id();
1483 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1488 local_save_flags(flags);
1490 event = trace_current_buffer_lock_reserve(&buffer,
1491 TRACE_FN, sizeof(*entry),
1495 entry = ring_buffer_event_data(event);
1497 entry->parent_ip = parent_ip;
1499 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1502 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1503 ftrace_preempt_enable(resched);
1506 static struct ftrace_ops trace_ops __initdata =
1508 .func = function_test_events_call,
1511 static __init void event_trace_self_test_with_function(void)
1513 register_ftrace_function(&trace_ops);
1514 pr_info("Running tests again, along with the function tracer\n");
1515 event_trace_self_tests();
1516 unregister_ftrace_function(&trace_ops);
1519 static __init void event_trace_self_test_with_function(void)
1524 static __init int event_trace_self_tests_init(void)
1526 if (!tracing_selftest_disabled) {
1527 event_trace_self_tests();
1528 event_trace_self_test_with_function();
1534 late_initcall(event_trace_self_tests_init);