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 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
115 tracing_stop_cmdline_record();
116 call->unregfunc(call);
120 if (!call->enabled) {
122 tracing_start_cmdline_record();
129 static void ftrace_clear_events(void)
131 struct ftrace_event_call *call;
133 mutex_lock(&event_mutex);
134 list_for_each_entry(call, &ftrace_events, list) {
135 ftrace_event_enable_disable(call, 0);
137 mutex_unlock(&event_mutex);
141 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
143 static int __ftrace_set_clr_event(const char *match, const char *sub,
144 const char *event, int set)
146 struct ftrace_event_call *call;
149 mutex_lock(&event_mutex);
150 list_for_each_entry(call, &ftrace_events, list) {
152 if (!call->name || !call->regfunc)
156 strcmp(match, call->name) != 0 &&
157 strcmp(match, call->system) != 0)
160 if (sub && strcmp(sub, call->system) != 0)
163 if (event && strcmp(event, call->name) != 0)
166 ftrace_event_enable_disable(call, set);
170 mutex_unlock(&event_mutex);
175 static int ftrace_set_clr_event(char *buf, int set)
177 char *event = NULL, *sub = NULL, *match;
180 * The buf format can be <subsystem>:<event-name>
181 * *:<event-name> means any event by that name.
182 * :<event-name> is the same.
184 * <subsystem>:* means all events in that subsystem
185 * <subsystem>: means the same.
187 * <name> (no ':') means all events in a subsystem with
188 * the name <name> or any event that matches <name>
191 match = strsep(&buf, ":");
197 if (!strlen(sub) || strcmp(sub, "*") == 0)
199 if (!strlen(event) || strcmp(event, "*") == 0)
203 return __ftrace_set_clr_event(match, sub, event, set);
207 * trace_set_clr_event - enable or disable an event
208 * @system: system name to match (NULL for any system)
209 * @event: event name to match (NULL for all events, within system)
210 * @set: 1 to enable, 0 to disable
212 * This is a way for other parts of the kernel to enable or disable
215 * Returns 0 on success, -EINVAL if the parameters do not match any
218 int trace_set_clr_event(const char *system, const char *event, int set)
220 return __ftrace_set_clr_event(NULL, system, event, set);
223 /* 128 should be much more than enough */
224 #define EVENT_BUF_SIZE 127
227 ftrace_event_write(struct file *file, const char __user *ubuf,
228 size_t cnt, loff_t *ppos)
230 struct trace_parser parser;
237 ret = tracing_update_buffers();
241 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
244 read = trace_get_user(&parser, ubuf, cnt, ppos);
246 if (trace_parser_loaded((&parser))) {
249 if (*parser.buffer == '!')
252 parser.buffer[parser.idx] = 0;
254 ret = ftrace_set_clr_event(parser.buffer + !set, set);
262 trace_parser_put(&parser);
268 t_next(struct seq_file *m, void *v, loff_t *pos)
270 struct ftrace_event_call *call = v;
274 list_for_each_entry_continue(call, &ftrace_events, list) {
276 * The ftrace subsystem is for showing formats only.
277 * They can not be enabled or disabled via the event files.
286 static void *t_start(struct seq_file *m, loff_t *pos)
288 struct ftrace_event_call *call;
291 mutex_lock(&event_mutex);
293 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
294 for (l = 0; l <= *pos; ) {
295 call = t_next(m, call, &l);
303 s_next(struct seq_file *m, void *v, loff_t *pos)
305 struct ftrace_event_call *call = v;
309 list_for_each_entry_continue(call, &ftrace_events, list) {
317 static void *s_start(struct seq_file *m, loff_t *pos)
319 struct ftrace_event_call *call;
322 mutex_lock(&event_mutex);
324 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
325 for (l = 0; l <= *pos; ) {
326 call = s_next(m, call, &l);
333 static int t_show(struct seq_file *m, void *v)
335 struct ftrace_event_call *call = v;
337 if (strcmp(call->system, TRACE_SYSTEM) != 0)
338 seq_printf(m, "%s:", call->system);
339 seq_printf(m, "%s\n", call->name);
344 static void t_stop(struct seq_file *m, void *p)
346 mutex_unlock(&event_mutex);
350 ftrace_event_seq_open(struct inode *inode, struct file *file)
352 const struct seq_operations *seq_ops;
354 if ((file->f_mode & FMODE_WRITE) &&
355 (file->f_flags & O_TRUNC))
356 ftrace_clear_events();
358 seq_ops = inode->i_private;
359 return seq_open(file, seq_ops);
363 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
366 struct ftrace_event_call *call = filp->private_data;
374 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
378 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
381 struct ftrace_event_call *call = filp->private_data;
386 if (cnt >= sizeof(buf))
389 if (copy_from_user(&buf, ubuf, cnt))
394 ret = strict_strtoul(buf, 10, &val);
398 ret = tracing_update_buffers();
405 mutex_lock(&event_mutex);
406 ftrace_event_enable_disable(call, val);
407 mutex_unlock(&event_mutex);
420 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
423 const char set_to_char[4] = { '?', '0', '1', 'X' };
424 const char *system = filp->private_data;
425 struct ftrace_event_call *call;
430 mutex_lock(&event_mutex);
431 list_for_each_entry(call, &ftrace_events, list) {
432 if (!call->name || !call->regfunc)
435 if (system && strcmp(call->system, system) != 0)
439 * We need to find out if all the events are set
440 * or if all events or cleared, or if we have
443 set |= (1 << !!call->enabled);
446 * If we have a mixture, no need to look further.
451 mutex_unlock(&event_mutex);
453 buf[0] = set_to_char[set];
456 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
462 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
465 const char *system = filp->private_data;
470 if (cnt >= sizeof(buf))
473 if (copy_from_user(&buf, ubuf, cnt))
478 ret = strict_strtoul(buf, 10, &val);
482 ret = tracing_update_buffers();
486 if (val != 0 && val != 1)
489 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
501 extern char *__bad_type_size(void);
504 #define FIELD(type, name) \
505 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
506 #type, "common_" #name, offsetof(typeof(field), name), \
509 static int trace_write_header(struct trace_seq *s)
511 struct trace_entry field;
513 /* struct trace_entry */
514 return trace_seq_printf(s,
515 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
516 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
517 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
518 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
519 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
521 FIELD(unsigned short, type),
522 FIELD(unsigned char, flags),
523 FIELD(unsigned char, preempt_count),
525 FIELD(int, lock_depth));
529 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
532 struct ftrace_event_call *call = filp->private_data;
540 s = kmalloc(sizeof(*s), GFP_KERNEL);
546 /* If any of the first writes fail, so will the show_format. */
548 trace_seq_printf(s, "name: %s\n", call->name);
549 trace_seq_printf(s, "ID: %d\n", call->id);
550 trace_seq_printf(s, "format:\n");
551 trace_write_header(s);
553 r = call->show_format(call, s);
556 * ug! The format output is bigger than a PAGE!!
558 buf = "FORMAT TOO BIG\n";
559 r = simple_read_from_buffer(ubuf, cnt, ppos,
564 r = simple_read_from_buffer(ubuf, cnt, ppos,
572 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
574 struct ftrace_event_call *call = filp->private_data;
581 s = kmalloc(sizeof(*s), GFP_KERNEL);
586 trace_seq_printf(s, "%d\n", call->id);
588 r = simple_read_from_buffer(ubuf, cnt, ppos,
595 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
598 struct ftrace_event_call *call = filp->private_data;
605 s = kmalloc(sizeof(*s), GFP_KERNEL);
611 print_event_filter(call, s);
612 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
620 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
623 struct ftrace_event_call *call = filp->private_data;
627 if (cnt >= PAGE_SIZE)
630 buf = (char *)__get_free_page(GFP_TEMPORARY);
634 if (copy_from_user(buf, ubuf, cnt)) {
635 free_page((unsigned long) buf);
640 err = apply_event_filter(call, buf);
641 free_page((unsigned long) buf);
651 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
654 struct event_subsystem *system = filp->private_data;
661 s = kmalloc(sizeof(*s), GFP_KERNEL);
667 print_subsystem_event_filter(system, s);
668 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
676 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
679 struct event_subsystem *system = filp->private_data;
683 if (cnt >= PAGE_SIZE)
686 buf = (char *)__get_free_page(GFP_TEMPORARY);
690 if (copy_from_user(buf, ubuf, cnt)) {
691 free_page((unsigned long) buf);
696 err = apply_subsystem_event_filter(system, buf);
697 free_page((unsigned long) buf);
707 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
709 int (*func)(struct trace_seq *s) = filp->private_data;
716 s = kmalloc(sizeof(*s), GFP_KERNEL);
723 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
730 static const struct seq_operations show_event_seq_ops = {
737 static const struct seq_operations show_set_event_seq_ops = {
744 static const struct file_operations ftrace_avail_fops = {
745 .open = ftrace_event_seq_open,
748 .release = seq_release,
751 static const struct file_operations ftrace_set_event_fops = {
752 .open = ftrace_event_seq_open,
754 .write = ftrace_event_write,
756 .release = seq_release,
759 static const struct file_operations ftrace_enable_fops = {
760 .open = tracing_open_generic,
761 .read = event_enable_read,
762 .write = event_enable_write,
765 static const struct file_operations ftrace_event_format_fops = {
766 .open = tracing_open_generic,
767 .read = event_format_read,
770 static const struct file_operations ftrace_event_id_fops = {
771 .open = tracing_open_generic,
772 .read = event_id_read,
775 static const struct file_operations ftrace_event_filter_fops = {
776 .open = tracing_open_generic,
777 .read = event_filter_read,
778 .write = event_filter_write,
781 static const struct file_operations ftrace_subsystem_filter_fops = {
782 .open = tracing_open_generic,
783 .read = subsystem_filter_read,
784 .write = subsystem_filter_write,
787 static const struct file_operations ftrace_system_enable_fops = {
788 .open = tracing_open_generic,
789 .read = system_enable_read,
790 .write = system_enable_write,
793 static const struct file_operations ftrace_show_header_fops = {
794 .open = tracing_open_generic,
798 static struct dentry *event_trace_events_dir(void)
800 static struct dentry *d_tracer;
801 static struct dentry *d_events;
806 d_tracer = tracing_init_dentry();
810 d_events = debugfs_create_dir("events", d_tracer);
812 pr_warning("Could not create debugfs "
813 "'events' directory\n");
818 static LIST_HEAD(event_subsystems);
820 static struct dentry *
821 event_subsystem_dir(const char *name, struct dentry *d_events)
823 struct event_subsystem *system;
824 struct dentry *entry;
826 /* First see if we did not already create this dir */
827 list_for_each_entry(system, &event_subsystems, list) {
828 if (strcmp(system->name, name) == 0) {
830 return system->entry;
834 /* need to create new entry */
835 system = kmalloc(sizeof(*system), GFP_KERNEL);
837 pr_warning("No memory to create event subsystem %s\n",
842 system->entry = debugfs_create_dir(name, d_events);
843 if (!system->entry) {
844 pr_warning("Could not create event subsystem %s\n",
850 system->nr_events = 1;
851 system->name = kstrdup(name, GFP_KERNEL);
853 debugfs_remove(system->entry);
858 list_add(&system->list, &event_subsystems);
860 system->filter = NULL;
862 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
863 if (!system->filter) {
864 pr_warning("Could not allocate filter for subsystem "
866 return system->entry;
869 entry = debugfs_create_file("filter", 0644, system->entry, system,
870 &ftrace_subsystem_filter_fops);
872 kfree(system->filter);
873 system->filter = NULL;
874 pr_warning("Could not create debugfs "
875 "'%s/filter' entry\n", name);
878 entry = trace_create_file("enable", 0644, system->entry,
879 (void *)system->name,
880 &ftrace_system_enable_fops);
882 return system->entry;
886 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
887 const struct file_operations *id,
888 const struct file_operations *enable,
889 const struct file_operations *filter,
890 const struct file_operations *format)
892 struct dentry *entry;
896 * If the trace point header did not define TRACE_SYSTEM
897 * then the system would be called "TRACE_SYSTEM".
899 if (strcmp(call->system, TRACE_SYSTEM) != 0)
900 d_events = event_subsystem_dir(call->system, d_events);
902 call->dir = debugfs_create_dir(call->name, d_events);
904 pr_warning("Could not create debugfs "
905 "'%s' directory\n", call->name);
910 entry = trace_create_file("enable", 0644, call->dir, call,
913 if (call->id && call->profile_enable)
914 entry = trace_create_file("id", 0444, call->dir, call,
917 if (call->define_fields) {
918 ret = call->define_fields(call);
920 pr_warning("Could not initialize trace point"
921 " events/%s\n", call->name);
924 entry = trace_create_file("filter", 0644, call->dir, call,
928 /* A trace may not want to export its format */
929 if (!call->show_format)
932 entry = trace_create_file("format", 0444, call->dir, call,
938 static int __trace_add_event_call(struct ftrace_event_call *call)
940 struct dentry *d_events;
946 if (call->raw_init) {
947 ret = call->raw_init(call);
950 pr_warning("Could not initialize trace "
951 "events/%s\n", call->name);
956 d_events = event_trace_events_dir();
960 list_add(&call->list, &ftrace_events);
961 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
962 &ftrace_enable_fops, &ftrace_event_filter_fops,
963 &ftrace_event_format_fops);
965 list_del(&call->list);
969 /* Add an additional event_call dynamically */
970 int trace_add_event_call(struct ftrace_event_call *call)
973 mutex_lock(&event_mutex);
974 ret = __trace_add_event_call(call);
975 mutex_unlock(&event_mutex);
979 static void remove_subsystem_dir(const char *name)
981 struct event_subsystem *system;
983 if (strcmp(name, TRACE_SYSTEM) == 0)
986 list_for_each_entry(system, &event_subsystems, list) {
987 if (strcmp(system->name, name) == 0) {
988 if (!--system->nr_events) {
989 struct event_filter *filter = system->filter;
991 debugfs_remove_recursive(system->entry);
992 list_del(&system->list);
994 kfree(filter->filter_string);
1006 * Must be called under locking both of event_mutex and trace_event_mutex.
1008 static void __trace_remove_event_call(struct ftrace_event_call *call)
1010 ftrace_event_enable_disable(call, 0);
1012 __unregister_ftrace_event(call->event);
1013 debugfs_remove_recursive(call->dir);
1014 list_del(&call->list);
1015 trace_destroy_fields(call);
1016 destroy_preds(call);
1017 remove_subsystem_dir(call->system);
1020 /* Remove an event_call */
1021 void trace_remove_event_call(struct ftrace_event_call *call)
1023 mutex_lock(&event_mutex);
1024 down_write(&trace_event_mutex);
1025 __trace_remove_event_call(call);
1026 up_write(&trace_event_mutex);
1027 mutex_unlock(&event_mutex);
1030 #define for_each_event(event, start, end) \
1031 for (event = start; \
1032 (unsigned long)event < (unsigned long)end; \
1035 #ifdef CONFIG_MODULES
1037 static LIST_HEAD(ftrace_module_file_list);
1040 * Modules must own their file_operations to keep up with
1041 * reference counting.
1043 struct ftrace_module_file_ops {
1044 struct list_head list;
1046 struct file_operations id;
1047 struct file_operations enable;
1048 struct file_operations format;
1049 struct file_operations filter;
1052 static struct ftrace_module_file_ops *
1053 trace_create_file_ops(struct module *mod)
1055 struct ftrace_module_file_ops *file_ops;
1058 * This is a bit of a PITA. To allow for correct reference
1059 * counting, modules must "own" their file_operations.
1060 * To do this, we allocate the file operations that will be
1061 * used in the event directory.
1064 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1068 file_ops->mod = mod;
1070 file_ops->id = ftrace_event_id_fops;
1071 file_ops->id.owner = mod;
1073 file_ops->enable = ftrace_enable_fops;
1074 file_ops->enable.owner = mod;
1076 file_ops->filter = ftrace_event_filter_fops;
1077 file_ops->filter.owner = mod;
1079 file_ops->format = ftrace_event_format_fops;
1080 file_ops->format.owner = mod;
1082 list_add(&file_ops->list, &ftrace_module_file_list);
1087 static void trace_module_add_events(struct module *mod)
1089 struct ftrace_module_file_ops *file_ops = NULL;
1090 struct ftrace_event_call *call, *start, *end;
1091 struct dentry *d_events;
1094 start = mod->trace_events;
1095 end = mod->trace_events + mod->num_trace_events;
1100 d_events = event_trace_events_dir();
1104 for_each_event(call, start, end) {
1105 /* The linker may leave blanks */
1108 if (call->raw_init) {
1109 ret = call->raw_init(call);
1112 pr_warning("Could not initialize trace "
1113 "point events/%s\n", call->name);
1118 * This module has events, create file ops for this module
1119 * if not already done.
1122 file_ops = trace_create_file_ops(mod);
1127 list_add(&call->list, &ftrace_events);
1128 event_create_dir(call, d_events,
1129 &file_ops->id, &file_ops->enable,
1130 &file_ops->filter, &file_ops->format);
1134 static void trace_module_remove_events(struct module *mod)
1136 struct ftrace_module_file_ops *file_ops;
1137 struct ftrace_event_call *call, *p;
1140 down_write(&trace_event_mutex);
1141 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1142 if (call->mod == mod) {
1144 __trace_remove_event_call(call);
1148 /* Now free the file_operations */
1149 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1150 if (file_ops->mod == mod)
1153 if (&file_ops->list != &ftrace_module_file_list) {
1154 list_del(&file_ops->list);
1159 * It is safest to reset the ring buffer if the module being unloaded
1160 * registered any events.
1163 tracing_reset_current_online_cpus();
1164 up_write(&trace_event_mutex);
1167 static int trace_module_notify(struct notifier_block *self,
1168 unsigned long val, void *data)
1170 struct module *mod = data;
1172 mutex_lock(&event_mutex);
1174 case MODULE_STATE_COMING:
1175 trace_module_add_events(mod);
1177 case MODULE_STATE_GOING:
1178 trace_module_remove_events(mod);
1181 mutex_unlock(&event_mutex);
1186 static int trace_module_notify(struct notifier_block *self,
1187 unsigned long val, void *data)
1191 #endif /* CONFIG_MODULES */
1193 static struct notifier_block trace_module_nb = {
1194 .notifier_call = trace_module_notify,
1198 extern struct ftrace_event_call __start_ftrace_events[];
1199 extern struct ftrace_event_call __stop_ftrace_events[];
1201 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1203 static __init int setup_trace_event(char *str)
1205 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1206 ring_buffer_expanded = 1;
1207 tracing_selftest_disabled = 1;
1211 __setup("trace_event=", setup_trace_event);
1213 static __init int event_trace_init(void)
1215 struct ftrace_event_call *call;
1216 struct dentry *d_tracer;
1217 struct dentry *entry;
1218 struct dentry *d_events;
1220 char *buf = bootup_event_buf;
1223 d_tracer = tracing_init_dentry();
1227 entry = debugfs_create_file("available_events", 0444, d_tracer,
1228 (void *)&show_event_seq_ops,
1229 &ftrace_avail_fops);
1231 pr_warning("Could not create debugfs "
1232 "'available_events' entry\n");
1234 entry = debugfs_create_file("set_event", 0644, d_tracer,
1235 (void *)&show_set_event_seq_ops,
1236 &ftrace_set_event_fops);
1238 pr_warning("Could not create debugfs "
1239 "'set_event' entry\n");
1241 d_events = event_trace_events_dir();
1245 /* ring buffer internal formats */
1246 trace_create_file("header_page", 0444, d_events,
1247 ring_buffer_print_page_header,
1248 &ftrace_show_header_fops);
1250 trace_create_file("header_event", 0444, d_events,
1251 ring_buffer_print_entry_header,
1252 &ftrace_show_header_fops);
1254 trace_create_file("enable", 0644, d_events,
1255 NULL, &ftrace_system_enable_fops);
1257 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1258 /* The linker may leave blanks */
1261 if (call->raw_init) {
1262 ret = call->raw_init(call);
1265 pr_warning("Could not initialize trace "
1266 "point events/%s\n", call->name);
1270 list_add(&call->list, &ftrace_events);
1271 event_create_dir(call, d_events, &ftrace_event_id_fops,
1272 &ftrace_enable_fops, &ftrace_event_filter_fops,
1273 &ftrace_event_format_fops);
1277 token = strsep(&buf, ",");
1284 ret = ftrace_set_clr_event(token, 1);
1286 pr_warning("Failed to enable trace event: %s\n", token);
1289 ret = register_module_notifier(&trace_module_nb);
1291 pr_warning("Failed to register trace events module notifier\n");
1295 fs_initcall(event_trace_init);
1297 #ifdef CONFIG_FTRACE_STARTUP_TEST
1299 static DEFINE_SPINLOCK(test_spinlock);
1300 static DEFINE_SPINLOCK(test_spinlock_irq);
1301 static DEFINE_MUTEX(test_mutex);
1303 static __init void test_work(struct work_struct *dummy)
1305 spin_lock(&test_spinlock);
1306 spin_lock_irq(&test_spinlock_irq);
1308 spin_unlock_irq(&test_spinlock_irq);
1309 spin_unlock(&test_spinlock);
1311 mutex_lock(&test_mutex);
1313 mutex_unlock(&test_mutex);
1316 static __init int event_test_thread(void *unused)
1320 test_malloc = kmalloc(1234, GFP_KERNEL);
1322 pr_info("failed to kmalloc\n");
1324 schedule_on_each_cpu(test_work);
1328 set_current_state(TASK_INTERRUPTIBLE);
1329 while (!kthread_should_stop())
1336 * Do various things that may trigger events.
1338 static __init void event_test_stuff(void)
1340 struct task_struct *test_thread;
1342 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1344 kthread_stop(test_thread);
1348 * For every trace event defined, we will test each trace point separately,
1349 * and then by groups, and finally all trace points.
1351 static __init void event_trace_self_tests(void)
1353 struct ftrace_event_call *call;
1354 struct event_subsystem *system;
1357 pr_info("Running tests on trace events:\n");
1359 list_for_each_entry(call, &ftrace_events, list) {
1361 /* Only test those that have a regfunc */
1366 * Testing syscall events here is pretty useless, but
1367 * we still do it if configured. But this is time consuming.
1368 * What we really need is a user thread to perform the
1369 * syscalls as we test.
1371 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1373 strcmp(call->system, "syscalls") == 0)
1377 pr_info("Testing event %s: ", call->name);
1380 * If an event is already enabled, someone is using
1381 * it and the self test should not be on.
1383 if (call->enabled) {
1384 pr_warning("Enabled event during self test!\n");
1389 ftrace_event_enable_disable(call, 1);
1391 ftrace_event_enable_disable(call, 0);
1396 /* Now test at the sub system level */
1398 pr_info("Running tests on trace event systems:\n");
1400 list_for_each_entry(system, &event_subsystems, list) {
1402 /* the ftrace system is special, skip it */
1403 if (strcmp(system->name, "ftrace") == 0)
1406 pr_info("Testing event system %s: ", system->name);
1408 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1409 if (WARN_ON_ONCE(ret)) {
1410 pr_warning("error enabling system %s\n",
1417 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1418 if (WARN_ON_ONCE(ret))
1419 pr_warning("error disabling system %s\n",
1425 /* Test with all events enabled */
1427 pr_info("Running tests on all trace events:\n");
1428 pr_info("Testing all events: ");
1430 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1431 if (WARN_ON_ONCE(ret)) {
1432 pr_warning("error enabling all events\n");
1439 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1440 if (WARN_ON_ONCE(ret)) {
1441 pr_warning("error disabling all events\n");
1448 #ifdef CONFIG_FUNCTION_TRACER
1450 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1453 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1455 struct ring_buffer_event *event;
1456 struct ring_buffer *buffer;
1457 struct ftrace_entry *entry;
1458 unsigned long flags;
1464 pc = preempt_count();
1465 resched = ftrace_preempt_disable();
1466 cpu = raw_smp_processor_id();
1467 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1472 local_save_flags(flags);
1474 event = trace_current_buffer_lock_reserve(&buffer,
1475 TRACE_FN, sizeof(*entry),
1479 entry = ring_buffer_event_data(event);
1481 entry->parent_ip = parent_ip;
1483 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1486 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1487 ftrace_preempt_enable(resched);
1490 static struct ftrace_ops trace_ops __initdata =
1492 .func = function_test_events_call,
1495 static __init void event_trace_self_test_with_function(void)
1497 register_ftrace_function(&trace_ops);
1498 pr_info("Running tests again, along with the function tracer\n");
1499 event_trace_self_tests();
1500 unregister_ftrace_function(&trace_ops);
1503 static __init void event_trace_self_test_with_function(void)
1508 static __init int event_trace_self_tests_init(void)
1510 if (!tracing_selftest_disabled) {
1511 event_trace_self_tests();
1512 event_trace_self_test_with_function();
1518 late_initcall(event_trace_self_tests_init);