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);
96 void trace_destroy_fields(struct ftrace_event_call *call)
98 struct ftrace_event_field *field, *next;
100 list_for_each_entry_safe(field, next, &call->fields, link) {
101 list_del(&field->link);
108 int trace_event_raw_init(struct ftrace_event_call *call)
112 id = register_ftrace_event(call->event);
116 INIT_LIST_HEAD(&call->fields);
120 EXPORT_SYMBOL_GPL(trace_event_raw_init);
122 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
129 tracing_stop_cmdline_record();
130 call->unregfunc(call);
134 if (!call->enabled) {
136 tracing_start_cmdline_record();
143 static void ftrace_clear_events(void)
145 struct ftrace_event_call *call;
147 mutex_lock(&event_mutex);
148 list_for_each_entry(call, &ftrace_events, list) {
149 ftrace_event_enable_disable(call, 0);
151 mutex_unlock(&event_mutex);
155 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
157 static int __ftrace_set_clr_event(const char *match, const char *sub,
158 const char *event, int set)
160 struct ftrace_event_call *call;
163 mutex_lock(&event_mutex);
164 list_for_each_entry(call, &ftrace_events, list) {
166 if (!call->name || !call->regfunc)
170 strcmp(match, call->name) != 0 &&
171 strcmp(match, call->system) != 0)
174 if (sub && strcmp(sub, call->system) != 0)
177 if (event && strcmp(event, call->name) != 0)
180 ftrace_event_enable_disable(call, set);
184 mutex_unlock(&event_mutex);
189 static int ftrace_set_clr_event(char *buf, int set)
191 char *event = NULL, *sub = NULL, *match;
194 * The buf format can be <subsystem>:<event-name>
195 * *:<event-name> means any event by that name.
196 * :<event-name> is the same.
198 * <subsystem>:* means all events in that subsystem
199 * <subsystem>: means the same.
201 * <name> (no ':') means all events in a subsystem with
202 * the name <name> or any event that matches <name>
205 match = strsep(&buf, ":");
211 if (!strlen(sub) || strcmp(sub, "*") == 0)
213 if (!strlen(event) || strcmp(event, "*") == 0)
217 return __ftrace_set_clr_event(match, sub, event, set);
221 * trace_set_clr_event - enable or disable an event
222 * @system: system name to match (NULL for any system)
223 * @event: event name to match (NULL for all events, within system)
224 * @set: 1 to enable, 0 to disable
226 * This is a way for other parts of the kernel to enable or disable
229 * Returns 0 on success, -EINVAL if the parameters do not match any
232 int trace_set_clr_event(const char *system, const char *event, int set)
234 return __ftrace_set_clr_event(NULL, system, event, set);
237 /* 128 should be much more than enough */
238 #define EVENT_BUF_SIZE 127
241 ftrace_event_write(struct file *file, const char __user *ubuf,
242 size_t cnt, loff_t *ppos)
244 struct trace_parser parser;
250 ret = tracing_update_buffers();
254 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
257 read = trace_get_user(&parser, ubuf, cnt, ppos);
259 if (read >= 0 && trace_parser_loaded((&parser))) {
262 if (*parser.buffer == '!')
265 parser.buffer[parser.idx] = 0;
267 ret = ftrace_set_clr_event(parser.buffer + !set, set);
275 trace_parser_put(&parser);
281 t_next(struct seq_file *m, void *v, loff_t *pos)
283 struct ftrace_event_call *call = v;
287 list_for_each_entry_continue(call, &ftrace_events, list) {
289 * The ftrace subsystem is for showing formats only.
290 * They can not be enabled or disabled via the event files.
299 static void *t_start(struct seq_file *m, loff_t *pos)
301 struct ftrace_event_call *call;
304 mutex_lock(&event_mutex);
306 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
307 for (l = 0; l <= *pos; ) {
308 call = t_next(m, call, &l);
316 s_next(struct seq_file *m, void *v, loff_t *pos)
318 struct ftrace_event_call *call = v;
322 list_for_each_entry_continue(call, &ftrace_events, list) {
330 static void *s_start(struct seq_file *m, loff_t *pos)
332 struct ftrace_event_call *call;
335 mutex_lock(&event_mutex);
337 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
338 for (l = 0; l <= *pos; ) {
339 call = s_next(m, call, &l);
346 static int t_show(struct seq_file *m, void *v)
348 struct ftrace_event_call *call = v;
350 if (strcmp(call->system, TRACE_SYSTEM) != 0)
351 seq_printf(m, "%s:", call->system);
352 seq_printf(m, "%s\n", call->name);
357 static void t_stop(struct seq_file *m, void *p)
359 mutex_unlock(&event_mutex);
363 ftrace_event_seq_open(struct inode *inode, struct file *file)
365 const struct seq_operations *seq_ops;
367 if ((file->f_mode & FMODE_WRITE) &&
368 (file->f_flags & O_TRUNC))
369 ftrace_clear_events();
371 seq_ops = inode->i_private;
372 return seq_open(file, seq_ops);
376 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
379 struct ftrace_event_call *call = filp->private_data;
387 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
391 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
394 struct ftrace_event_call *call = filp->private_data;
399 if (cnt >= sizeof(buf))
402 if (copy_from_user(&buf, ubuf, cnt))
407 ret = strict_strtoul(buf, 10, &val);
411 ret = tracing_update_buffers();
418 mutex_lock(&event_mutex);
419 ftrace_event_enable_disable(call, val);
420 mutex_unlock(&event_mutex);
433 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
436 const char set_to_char[4] = { '?', '0', '1', 'X' };
437 const char *system = filp->private_data;
438 struct ftrace_event_call *call;
443 mutex_lock(&event_mutex);
444 list_for_each_entry(call, &ftrace_events, list) {
445 if (!call->name || !call->regfunc)
448 if (system && strcmp(call->system, system) != 0)
452 * We need to find out if all the events are set
453 * or if all events or cleared, or if we have
456 set |= (1 << !!call->enabled);
459 * If we have a mixture, no need to look further.
464 mutex_unlock(&event_mutex);
466 buf[0] = set_to_char[set];
469 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
475 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
478 const char *system = filp->private_data;
483 if (cnt >= sizeof(buf))
486 if (copy_from_user(&buf, ubuf, cnt))
491 ret = strict_strtoul(buf, 10, &val);
495 ret = tracing_update_buffers();
499 if (val != 0 && val != 1)
502 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
514 extern char *__bad_type_size(void);
517 #define FIELD(type, name) \
518 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
519 #type, "common_" #name, offsetof(typeof(field), name), \
520 sizeof(field.name), is_signed_type(type)
522 static int trace_write_header(struct trace_seq *s)
524 struct trace_entry field;
526 /* struct trace_entry */
527 return trace_seq_printf(s,
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"
532 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\tsigned:%u;\n"
534 FIELD(unsigned short, type),
535 FIELD(unsigned char, flags),
536 FIELD(unsigned char, preempt_count),
538 FIELD(int, lock_depth));
542 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
545 struct ftrace_event_call *call = filp->private_data;
553 s = kmalloc(sizeof(*s), GFP_KERNEL);
559 /* If any of the first writes fail, so will the show_format. */
561 trace_seq_printf(s, "name: %s\n", call->name);
562 trace_seq_printf(s, "ID: %d\n", call->id);
563 trace_seq_printf(s, "format:\n");
564 trace_write_header(s);
566 r = call->show_format(call, s);
569 * ug! The format output is bigger than a PAGE!!
571 buf = "FORMAT TOO BIG\n";
572 r = simple_read_from_buffer(ubuf, cnt, ppos,
577 r = simple_read_from_buffer(ubuf, cnt, ppos,
585 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
587 struct ftrace_event_call *call = filp->private_data;
594 s = kmalloc(sizeof(*s), GFP_KERNEL);
599 trace_seq_printf(s, "%d\n", call->id);
601 r = simple_read_from_buffer(ubuf, cnt, ppos,
608 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
611 struct ftrace_event_call *call = filp->private_data;
618 s = kmalloc(sizeof(*s), GFP_KERNEL);
624 print_event_filter(call, s);
625 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
633 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
636 struct ftrace_event_call *call = filp->private_data;
640 if (cnt >= PAGE_SIZE)
643 buf = (char *)__get_free_page(GFP_TEMPORARY);
647 if (copy_from_user(buf, ubuf, cnt)) {
648 free_page((unsigned long) buf);
653 err = apply_event_filter(call, buf);
654 free_page((unsigned long) buf);
664 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
667 struct event_subsystem *system = filp->private_data;
674 s = kmalloc(sizeof(*s), GFP_KERNEL);
680 print_subsystem_event_filter(system, s);
681 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
689 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
692 struct event_subsystem *system = filp->private_data;
696 if (cnt >= PAGE_SIZE)
699 buf = (char *)__get_free_page(GFP_TEMPORARY);
703 if (copy_from_user(buf, ubuf, cnt)) {
704 free_page((unsigned long) buf);
709 err = apply_subsystem_event_filter(system, buf);
710 free_page((unsigned long) buf);
720 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
722 int (*func)(struct trace_seq *s) = filp->private_data;
729 s = kmalloc(sizeof(*s), GFP_KERNEL);
736 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
743 static const struct seq_operations show_event_seq_ops = {
750 static const struct seq_operations show_set_event_seq_ops = {
757 static const struct file_operations ftrace_avail_fops = {
758 .open = ftrace_event_seq_open,
761 .release = seq_release,
764 static const struct file_operations ftrace_set_event_fops = {
765 .open = ftrace_event_seq_open,
767 .write = ftrace_event_write,
769 .release = seq_release,
772 static const struct file_operations ftrace_enable_fops = {
773 .open = tracing_open_generic,
774 .read = event_enable_read,
775 .write = event_enable_write,
778 static const struct file_operations ftrace_event_format_fops = {
779 .open = tracing_open_generic,
780 .read = event_format_read,
783 static const struct file_operations ftrace_event_id_fops = {
784 .open = tracing_open_generic,
785 .read = event_id_read,
788 static const struct file_operations ftrace_event_filter_fops = {
789 .open = tracing_open_generic,
790 .read = event_filter_read,
791 .write = event_filter_write,
794 static const struct file_operations ftrace_subsystem_filter_fops = {
795 .open = tracing_open_generic,
796 .read = subsystem_filter_read,
797 .write = subsystem_filter_write,
800 static const struct file_operations ftrace_system_enable_fops = {
801 .open = tracing_open_generic,
802 .read = system_enable_read,
803 .write = system_enable_write,
806 static const struct file_operations ftrace_show_header_fops = {
807 .open = tracing_open_generic,
811 static struct dentry *event_trace_events_dir(void)
813 static struct dentry *d_tracer;
814 static struct dentry *d_events;
819 d_tracer = tracing_init_dentry();
823 d_events = debugfs_create_dir("events", d_tracer);
825 pr_warning("Could not create debugfs "
826 "'events' directory\n");
831 static LIST_HEAD(event_subsystems);
833 static struct dentry *
834 event_subsystem_dir(const char *name, struct dentry *d_events)
836 struct event_subsystem *system;
837 struct dentry *entry;
839 /* First see if we did not already create this dir */
840 list_for_each_entry(system, &event_subsystems, list) {
841 if (strcmp(system->name, name) == 0) {
843 return system->entry;
847 /* need to create new entry */
848 system = kmalloc(sizeof(*system), GFP_KERNEL);
850 pr_warning("No memory to create event subsystem %s\n",
855 system->entry = debugfs_create_dir(name, d_events);
856 if (!system->entry) {
857 pr_warning("Could not create event subsystem %s\n",
863 system->nr_events = 1;
864 system->name = kstrdup(name, GFP_KERNEL);
866 debugfs_remove(system->entry);
871 list_add(&system->list, &event_subsystems);
873 system->filter = NULL;
875 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
876 if (!system->filter) {
877 pr_warning("Could not allocate filter for subsystem "
879 return system->entry;
882 entry = debugfs_create_file("filter", 0644, system->entry, system,
883 &ftrace_subsystem_filter_fops);
885 kfree(system->filter);
886 system->filter = NULL;
887 pr_warning("Could not create debugfs "
888 "'%s/filter' entry\n", name);
891 trace_create_file("enable", 0644, system->entry,
892 (void *)system->name,
893 &ftrace_system_enable_fops);
895 return system->entry;
899 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
900 const struct file_operations *id,
901 const struct file_operations *enable,
902 const struct file_operations *filter,
903 const struct file_operations *format)
908 * If the trace point header did not define TRACE_SYSTEM
909 * then the system would be called "TRACE_SYSTEM".
911 if (strcmp(call->system, TRACE_SYSTEM) != 0)
912 d_events = event_subsystem_dir(call->system, d_events);
914 call->dir = debugfs_create_dir(call->name, d_events);
916 pr_warning("Could not create debugfs "
917 "'%s' directory\n", call->name);
922 trace_create_file("enable", 0644, call->dir, call,
925 if (call->id && call->profile_enable)
926 trace_create_file("id", 0444, call->dir, call,
929 if (call->define_fields) {
930 ret = call->define_fields(call);
932 pr_warning("Could not initialize trace point"
933 " events/%s\n", call->name);
936 trace_create_file("filter", 0644, call->dir, call,
940 /* A trace may not want to export its format */
941 if (!call->show_format)
944 trace_create_file("format", 0444, call->dir, call,
950 static int __trace_add_event_call(struct ftrace_event_call *call)
952 struct dentry *d_events;
958 if (call->raw_init) {
959 ret = call->raw_init(call);
962 pr_warning("Could not initialize trace "
963 "events/%s\n", call->name);
968 d_events = event_trace_events_dir();
972 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
973 &ftrace_enable_fops, &ftrace_event_filter_fops,
974 &ftrace_event_format_fops);
976 list_add(&call->list, &ftrace_events);
981 /* Add an additional event_call dynamically */
982 int trace_add_event_call(struct ftrace_event_call *call)
985 mutex_lock(&event_mutex);
986 ret = __trace_add_event_call(call);
987 mutex_unlock(&event_mutex);
991 static void remove_subsystem_dir(const char *name)
993 struct event_subsystem *system;
995 if (strcmp(name, TRACE_SYSTEM) == 0)
998 list_for_each_entry(system, &event_subsystems, list) {
999 if (strcmp(system->name, name) == 0) {
1000 if (!--system->nr_events) {
1001 struct event_filter *filter = system->filter;
1003 debugfs_remove_recursive(system->entry);
1004 list_del(&system->list);
1006 kfree(filter->filter_string);
1009 kfree(system->name);
1018 * Must be called under locking both of event_mutex and trace_event_mutex.
1020 static void __trace_remove_event_call(struct ftrace_event_call *call)
1022 ftrace_event_enable_disable(call, 0);
1024 __unregister_ftrace_event(call->event);
1025 debugfs_remove_recursive(call->dir);
1026 list_del(&call->list);
1027 trace_destroy_fields(call);
1028 destroy_preds(call);
1029 remove_subsystem_dir(call->system);
1032 /* Remove an event_call */
1033 void trace_remove_event_call(struct ftrace_event_call *call)
1035 mutex_lock(&event_mutex);
1036 down_write(&trace_event_mutex);
1037 __trace_remove_event_call(call);
1038 up_write(&trace_event_mutex);
1039 mutex_unlock(&event_mutex);
1042 #define for_each_event(event, start, end) \
1043 for (event = start; \
1044 (unsigned long)event < (unsigned long)end; \
1047 #ifdef CONFIG_MODULES
1049 static LIST_HEAD(ftrace_module_file_list);
1052 * Modules must own their file_operations to keep up with
1053 * reference counting.
1055 struct ftrace_module_file_ops {
1056 struct list_head list;
1058 struct file_operations id;
1059 struct file_operations enable;
1060 struct file_operations format;
1061 struct file_operations filter;
1064 static struct ftrace_module_file_ops *
1065 trace_create_file_ops(struct module *mod)
1067 struct ftrace_module_file_ops *file_ops;
1070 * This is a bit of a PITA. To allow for correct reference
1071 * counting, modules must "own" their file_operations.
1072 * To do this, we allocate the file operations that will be
1073 * used in the event directory.
1076 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1080 file_ops->mod = mod;
1082 file_ops->id = ftrace_event_id_fops;
1083 file_ops->id.owner = mod;
1085 file_ops->enable = ftrace_enable_fops;
1086 file_ops->enable.owner = mod;
1088 file_ops->filter = ftrace_event_filter_fops;
1089 file_ops->filter.owner = mod;
1091 file_ops->format = ftrace_event_format_fops;
1092 file_ops->format.owner = mod;
1094 list_add(&file_ops->list, &ftrace_module_file_list);
1099 static void trace_module_add_events(struct module *mod)
1101 struct ftrace_module_file_ops *file_ops = NULL;
1102 struct ftrace_event_call *call, *start, *end;
1103 struct dentry *d_events;
1106 start = mod->trace_events;
1107 end = mod->trace_events + mod->num_trace_events;
1112 d_events = event_trace_events_dir();
1116 for_each_event(call, start, end) {
1117 /* The linker may leave blanks */
1120 if (call->raw_init) {
1121 ret = call->raw_init(call);
1124 pr_warning("Could not initialize trace "
1125 "point events/%s\n", call->name);
1130 * This module has events, create file ops for this module
1131 * if not already done.
1134 file_ops = trace_create_file_ops(mod);
1139 ret = event_create_dir(call, d_events,
1140 &file_ops->id, &file_ops->enable,
1141 &file_ops->filter, &file_ops->format);
1143 list_add(&call->list, &ftrace_events);
1147 static void trace_module_remove_events(struct module *mod)
1149 struct ftrace_module_file_ops *file_ops;
1150 struct ftrace_event_call *call, *p;
1153 down_write(&trace_event_mutex);
1154 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1155 if (call->mod == mod) {
1157 __trace_remove_event_call(call);
1161 /* Now free the file_operations */
1162 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1163 if (file_ops->mod == mod)
1166 if (&file_ops->list != &ftrace_module_file_list) {
1167 list_del(&file_ops->list);
1172 * It is safest to reset the ring buffer if the module being unloaded
1173 * registered any events.
1176 tracing_reset_current_online_cpus();
1177 up_write(&trace_event_mutex);
1180 static int trace_module_notify(struct notifier_block *self,
1181 unsigned long val, void *data)
1183 struct module *mod = data;
1185 mutex_lock(&event_mutex);
1187 case MODULE_STATE_COMING:
1188 trace_module_add_events(mod);
1190 case MODULE_STATE_GOING:
1191 trace_module_remove_events(mod);
1194 mutex_unlock(&event_mutex);
1199 static int trace_module_notify(struct notifier_block *self,
1200 unsigned long val, void *data)
1204 #endif /* CONFIG_MODULES */
1206 static struct notifier_block trace_module_nb = {
1207 .notifier_call = trace_module_notify,
1211 extern struct ftrace_event_call __start_ftrace_events[];
1212 extern struct ftrace_event_call __stop_ftrace_events[];
1214 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1216 static __init int setup_trace_event(char *str)
1218 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1219 ring_buffer_expanded = 1;
1220 tracing_selftest_disabled = 1;
1224 __setup("trace_event=", setup_trace_event);
1226 static __init int event_trace_init(void)
1228 struct ftrace_event_call *call;
1229 struct dentry *d_tracer;
1230 struct dentry *entry;
1231 struct dentry *d_events;
1233 char *buf = bootup_event_buf;
1236 d_tracer = tracing_init_dentry();
1240 entry = debugfs_create_file("available_events", 0444, d_tracer,
1241 (void *)&show_event_seq_ops,
1242 &ftrace_avail_fops);
1244 pr_warning("Could not create debugfs "
1245 "'available_events' entry\n");
1247 entry = debugfs_create_file("set_event", 0644, d_tracer,
1248 (void *)&show_set_event_seq_ops,
1249 &ftrace_set_event_fops);
1251 pr_warning("Could not create debugfs "
1252 "'set_event' entry\n");
1254 d_events = event_trace_events_dir();
1258 /* ring buffer internal formats */
1259 trace_create_file("header_page", 0444, d_events,
1260 ring_buffer_print_page_header,
1261 &ftrace_show_header_fops);
1263 trace_create_file("header_event", 0444, d_events,
1264 ring_buffer_print_entry_header,
1265 &ftrace_show_header_fops);
1267 trace_create_file("enable", 0644, d_events,
1268 NULL, &ftrace_system_enable_fops);
1270 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1271 /* The linker may leave blanks */
1274 if (call->raw_init) {
1275 ret = call->raw_init(call);
1278 pr_warning("Could not initialize trace "
1279 "point events/%s\n", call->name);
1283 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1284 &ftrace_enable_fops,
1285 &ftrace_event_filter_fops,
1286 &ftrace_event_format_fops);
1288 list_add(&call->list, &ftrace_events);
1292 token = strsep(&buf, ",");
1299 ret = ftrace_set_clr_event(token, 1);
1301 pr_warning("Failed to enable trace event: %s\n", token);
1304 ret = register_module_notifier(&trace_module_nb);
1306 pr_warning("Failed to register trace events module notifier\n");
1310 fs_initcall(event_trace_init);
1312 #ifdef CONFIG_FTRACE_STARTUP_TEST
1314 static DEFINE_SPINLOCK(test_spinlock);
1315 static DEFINE_SPINLOCK(test_spinlock_irq);
1316 static DEFINE_MUTEX(test_mutex);
1318 static __init void test_work(struct work_struct *dummy)
1320 spin_lock(&test_spinlock);
1321 spin_lock_irq(&test_spinlock_irq);
1323 spin_unlock_irq(&test_spinlock_irq);
1324 spin_unlock(&test_spinlock);
1326 mutex_lock(&test_mutex);
1328 mutex_unlock(&test_mutex);
1331 static __init int event_test_thread(void *unused)
1335 test_malloc = kmalloc(1234, GFP_KERNEL);
1337 pr_info("failed to kmalloc\n");
1339 schedule_on_each_cpu(test_work);
1343 set_current_state(TASK_INTERRUPTIBLE);
1344 while (!kthread_should_stop())
1351 * Do various things that may trigger events.
1353 static __init void event_test_stuff(void)
1355 struct task_struct *test_thread;
1357 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1359 kthread_stop(test_thread);
1363 * For every trace event defined, we will test each trace point separately,
1364 * and then by groups, and finally all trace points.
1366 static __init void event_trace_self_tests(void)
1368 struct ftrace_event_call *call;
1369 struct event_subsystem *system;
1372 pr_info("Running tests on trace events:\n");
1374 list_for_each_entry(call, &ftrace_events, list) {
1376 /* Only test those that have a regfunc */
1381 * Testing syscall events here is pretty useless, but
1382 * we still do it if configured. But this is time consuming.
1383 * What we really need is a user thread to perform the
1384 * syscalls as we test.
1386 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1388 strcmp(call->system, "syscalls") == 0)
1392 pr_info("Testing event %s: ", call->name);
1395 * If an event is already enabled, someone is using
1396 * it and the self test should not be on.
1398 if (call->enabled) {
1399 pr_warning("Enabled event during self test!\n");
1404 ftrace_event_enable_disable(call, 1);
1406 ftrace_event_enable_disable(call, 0);
1411 /* Now test at the sub system level */
1413 pr_info("Running tests on trace event systems:\n");
1415 list_for_each_entry(system, &event_subsystems, list) {
1417 /* the ftrace system is special, skip it */
1418 if (strcmp(system->name, "ftrace") == 0)
1421 pr_info("Testing event system %s: ", system->name);
1423 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1424 if (WARN_ON_ONCE(ret)) {
1425 pr_warning("error enabling system %s\n",
1432 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1433 if (WARN_ON_ONCE(ret))
1434 pr_warning("error disabling system %s\n",
1440 /* Test with all events enabled */
1442 pr_info("Running tests on all trace events:\n");
1443 pr_info("Testing all events: ");
1445 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1446 if (WARN_ON_ONCE(ret)) {
1447 pr_warning("error enabling all events\n");
1454 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1455 if (WARN_ON_ONCE(ret)) {
1456 pr_warning("error disabling all events\n");
1463 #ifdef CONFIG_FUNCTION_TRACER
1465 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1468 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1470 struct ring_buffer_event *event;
1471 struct ring_buffer *buffer;
1472 struct ftrace_entry *entry;
1473 unsigned long flags;
1479 pc = preempt_count();
1480 resched = ftrace_preempt_disable();
1481 cpu = raw_smp_processor_id();
1482 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1487 local_save_flags(flags);
1489 event = trace_current_buffer_lock_reserve(&buffer,
1490 TRACE_FN, sizeof(*entry),
1494 entry = ring_buffer_event_data(event);
1496 entry->parent_ip = parent_ip;
1498 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1501 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1502 ftrace_preempt_enable(resched);
1505 static struct ftrace_ops trace_ops __initdata =
1507 .func = function_test_events_call,
1510 static __init void event_trace_self_test_with_function(void)
1512 register_ftrace_function(&trace_ops);
1513 pr_info("Running tests again, along with the function tracer\n");
1514 event_trace_self_tests();
1515 unregister_ftrace_function(&trace_ops);
1518 static __init void event_trace_self_test_with_function(void)
1523 static __init int event_trace_self_tests_init(void)
1525 if (!tracing_selftest_disabled) {
1526 event_trace_self_tests();
1527 event_trace_self_test_with_function();
1533 late_initcall(event_trace_self_tests_init);