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 int ftrace_event_enable_disable(struct ftrace_event_call *call,
130 tracing_stop_cmdline_record();
131 call->unregfunc(call);
135 if (!call->enabled) {
136 tracing_start_cmdline_record();
137 ret = call->regfunc(call);
139 tracing_stop_cmdline_record();
140 pr_info("event trace: Could not enable event "
152 static void ftrace_clear_events(void)
154 struct ftrace_event_call *call;
156 mutex_lock(&event_mutex);
157 list_for_each_entry(call, &ftrace_events, list) {
158 ftrace_event_enable_disable(call, 0);
160 mutex_unlock(&event_mutex);
164 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
166 static int __ftrace_set_clr_event(const char *match, const char *sub,
167 const char *event, int set)
169 struct ftrace_event_call *call;
172 mutex_lock(&event_mutex);
173 list_for_each_entry(call, &ftrace_events, list) {
175 if (!call->name || !call->regfunc)
179 strcmp(match, call->name) != 0 &&
180 strcmp(match, call->system) != 0)
183 if (sub && strcmp(sub, call->system) != 0)
186 if (event && strcmp(event, call->name) != 0)
189 ftrace_event_enable_disable(call, set);
193 mutex_unlock(&event_mutex);
198 static int ftrace_set_clr_event(char *buf, int set)
200 char *event = NULL, *sub = NULL, *match;
203 * The buf format can be <subsystem>:<event-name>
204 * *:<event-name> means any event by that name.
205 * :<event-name> is the same.
207 * <subsystem>:* means all events in that subsystem
208 * <subsystem>: means the same.
210 * <name> (no ':') means all events in a subsystem with
211 * the name <name> or any event that matches <name>
214 match = strsep(&buf, ":");
220 if (!strlen(sub) || strcmp(sub, "*") == 0)
222 if (!strlen(event) || strcmp(event, "*") == 0)
226 return __ftrace_set_clr_event(match, sub, event, set);
230 * trace_set_clr_event - enable or disable an event
231 * @system: system name to match (NULL for any system)
232 * @event: event name to match (NULL for all events, within system)
233 * @set: 1 to enable, 0 to disable
235 * This is a way for other parts of the kernel to enable or disable
238 * Returns 0 on success, -EINVAL if the parameters do not match any
241 int trace_set_clr_event(const char *system, const char *event, int set)
243 return __ftrace_set_clr_event(NULL, system, event, set);
246 /* 128 should be much more than enough */
247 #define EVENT_BUF_SIZE 127
250 ftrace_event_write(struct file *file, const char __user *ubuf,
251 size_t cnt, loff_t *ppos)
253 struct trace_parser parser;
259 ret = tracing_update_buffers();
263 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
266 read = trace_get_user(&parser, ubuf, cnt, ppos);
268 if (read >= 0 && trace_parser_loaded((&parser))) {
271 if (*parser.buffer == '!')
274 parser.buffer[parser.idx] = 0;
276 ret = ftrace_set_clr_event(parser.buffer + !set, set);
284 trace_parser_put(&parser);
290 t_next(struct seq_file *m, void *v, loff_t *pos)
292 struct ftrace_event_call *call = v;
296 list_for_each_entry_continue(call, &ftrace_events, list) {
298 * The ftrace subsystem is for showing formats only.
299 * They can not be enabled or disabled via the event files.
308 static void *t_start(struct seq_file *m, loff_t *pos)
310 struct ftrace_event_call *call;
313 mutex_lock(&event_mutex);
315 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
316 for (l = 0; l <= *pos; ) {
317 call = t_next(m, call, &l);
325 s_next(struct seq_file *m, void *v, loff_t *pos)
327 struct ftrace_event_call *call = v;
331 list_for_each_entry_continue(call, &ftrace_events, list) {
339 static void *s_start(struct seq_file *m, loff_t *pos)
341 struct ftrace_event_call *call;
344 mutex_lock(&event_mutex);
346 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
347 for (l = 0; l <= *pos; ) {
348 call = s_next(m, call, &l);
355 static int t_show(struct seq_file *m, void *v)
357 struct ftrace_event_call *call = v;
359 if (strcmp(call->system, TRACE_SYSTEM) != 0)
360 seq_printf(m, "%s:", call->system);
361 seq_printf(m, "%s\n", call->name);
366 static void t_stop(struct seq_file *m, void *p)
368 mutex_unlock(&event_mutex);
372 ftrace_event_seq_open(struct inode *inode, struct file *file)
374 const struct seq_operations *seq_ops;
376 if ((file->f_mode & FMODE_WRITE) &&
377 (file->f_flags & O_TRUNC))
378 ftrace_clear_events();
380 seq_ops = inode->i_private;
381 return seq_open(file, seq_ops);
385 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
388 struct ftrace_event_call *call = filp->private_data;
396 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
400 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
403 struct ftrace_event_call *call = filp->private_data;
408 if (cnt >= sizeof(buf))
411 if (copy_from_user(&buf, ubuf, cnt))
416 ret = strict_strtoul(buf, 10, &val);
420 ret = tracing_update_buffers();
427 mutex_lock(&event_mutex);
428 ret = ftrace_event_enable_disable(call, val);
429 mutex_unlock(&event_mutex);
438 return ret ? ret : cnt;
442 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
445 const char set_to_char[4] = { '?', '0', '1', 'X' };
446 const char *system = filp->private_data;
447 struct ftrace_event_call *call;
452 mutex_lock(&event_mutex);
453 list_for_each_entry(call, &ftrace_events, list) {
454 if (!call->name || !call->regfunc)
457 if (system && strcmp(call->system, system) != 0)
461 * We need to find out if all the events are set
462 * or if all events or cleared, or if we have
465 set |= (1 << !!call->enabled);
468 * If we have a mixture, no need to look further.
473 mutex_unlock(&event_mutex);
475 buf[0] = set_to_char[set];
478 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
484 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
487 const char *system = filp->private_data;
492 if (cnt >= sizeof(buf))
495 if (copy_from_user(&buf, ubuf, cnt))
500 ret = strict_strtoul(buf, 10, &val);
504 ret = tracing_update_buffers();
508 if (val != 0 && val != 1)
511 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
524 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
527 struct ftrace_event_call *call = filp->private_data;
528 struct ftrace_event_field *field;
530 int common_field_count = 5;
537 s = kmalloc(sizeof(*s), GFP_KERNEL);
543 trace_seq_printf(s, "name: %s\n", call->name);
544 trace_seq_printf(s, "ID: %d\n", call->id);
545 trace_seq_printf(s, "format:\n");
547 list_for_each_entry_reverse(field, &call->fields, link) {
549 * Smartly shows the array type(except dynamic array).
552 * If TYPE := TYPE[LEN], it is shown:
553 * field:TYPE VAR[LEN]
555 const char *array_descriptor = strchr(field->type, '[');
557 if (!strncmp(field->type, "__data_loc", 10))
558 array_descriptor = NULL;
560 if (!array_descriptor) {
561 r = trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
562 "\tsize:%u;\tsigned:%d;\n",
563 field->type, field->name, field->offset,
564 field->size, !!field->is_signed);
566 r = trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
567 "\tsize:%u;\tsigned:%d;\n",
568 (int)(array_descriptor - field->type),
569 field->type, field->name,
570 array_descriptor, field->offset,
571 field->size, !!field->is_signed);
574 if (--common_field_count == 0)
575 r = trace_seq_printf(s, "\n");
582 r = trace_seq_printf(s, "\nprint fmt: %s\n",
587 * ug! The format output is bigger than a PAGE!!
589 buf = "FORMAT TOO BIG\n";
590 r = simple_read_from_buffer(ubuf, cnt, ppos,
595 r = simple_read_from_buffer(ubuf, cnt, ppos,
603 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
605 struct ftrace_event_call *call = filp->private_data;
612 s = kmalloc(sizeof(*s), GFP_KERNEL);
617 trace_seq_printf(s, "%d\n", call->id);
619 r = simple_read_from_buffer(ubuf, cnt, ppos,
626 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
629 struct ftrace_event_call *call = filp->private_data;
636 s = kmalloc(sizeof(*s), GFP_KERNEL);
642 print_event_filter(call, s);
643 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
651 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
654 struct ftrace_event_call *call = filp->private_data;
658 if (cnt >= PAGE_SIZE)
661 buf = (char *)__get_free_page(GFP_TEMPORARY);
665 if (copy_from_user(buf, ubuf, cnt)) {
666 free_page((unsigned long) buf);
671 err = apply_event_filter(call, buf);
672 free_page((unsigned long) buf);
682 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
685 struct event_subsystem *system = filp->private_data;
692 s = kmalloc(sizeof(*s), GFP_KERNEL);
698 print_subsystem_event_filter(system, s);
699 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
707 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
710 struct event_subsystem *system = filp->private_data;
714 if (cnt >= PAGE_SIZE)
717 buf = (char *)__get_free_page(GFP_TEMPORARY);
721 if (copy_from_user(buf, ubuf, cnt)) {
722 free_page((unsigned long) buf);
727 err = apply_subsystem_event_filter(system, buf);
728 free_page((unsigned long) buf);
738 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
740 int (*func)(struct trace_seq *s) = filp->private_data;
747 s = kmalloc(sizeof(*s), GFP_KERNEL);
754 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
761 static const struct seq_operations show_event_seq_ops = {
768 static const struct seq_operations show_set_event_seq_ops = {
775 static const struct file_operations ftrace_avail_fops = {
776 .open = ftrace_event_seq_open,
779 .release = seq_release,
782 static const struct file_operations ftrace_set_event_fops = {
783 .open = ftrace_event_seq_open,
785 .write = ftrace_event_write,
787 .release = seq_release,
790 static const struct file_operations ftrace_enable_fops = {
791 .open = tracing_open_generic,
792 .read = event_enable_read,
793 .write = event_enable_write,
796 static const struct file_operations ftrace_event_format_fops = {
797 .open = tracing_open_generic,
798 .read = event_format_read,
801 static const struct file_operations ftrace_event_id_fops = {
802 .open = tracing_open_generic,
803 .read = event_id_read,
806 static const struct file_operations ftrace_event_filter_fops = {
807 .open = tracing_open_generic,
808 .read = event_filter_read,
809 .write = event_filter_write,
812 static const struct file_operations ftrace_subsystem_filter_fops = {
813 .open = tracing_open_generic,
814 .read = subsystem_filter_read,
815 .write = subsystem_filter_write,
818 static const struct file_operations ftrace_system_enable_fops = {
819 .open = tracing_open_generic,
820 .read = system_enable_read,
821 .write = system_enable_write,
824 static const struct file_operations ftrace_show_header_fops = {
825 .open = tracing_open_generic,
829 static struct dentry *event_trace_events_dir(void)
831 static struct dentry *d_tracer;
832 static struct dentry *d_events;
837 d_tracer = tracing_init_dentry();
841 d_events = debugfs_create_dir("events", d_tracer);
843 pr_warning("Could not create debugfs "
844 "'events' directory\n");
849 static LIST_HEAD(event_subsystems);
851 static struct dentry *
852 event_subsystem_dir(const char *name, struct dentry *d_events)
854 struct event_subsystem *system;
855 struct dentry *entry;
857 /* First see if we did not already create this dir */
858 list_for_each_entry(system, &event_subsystems, list) {
859 if (strcmp(system->name, name) == 0) {
861 return system->entry;
865 /* need to create new entry */
866 system = kmalloc(sizeof(*system), GFP_KERNEL);
868 pr_warning("No memory to create event subsystem %s\n",
873 system->entry = debugfs_create_dir(name, d_events);
874 if (!system->entry) {
875 pr_warning("Could not create event subsystem %s\n",
881 system->nr_events = 1;
882 system->name = kstrdup(name, GFP_KERNEL);
884 debugfs_remove(system->entry);
889 list_add(&system->list, &event_subsystems);
891 system->filter = NULL;
893 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
894 if (!system->filter) {
895 pr_warning("Could not allocate filter for subsystem "
897 return system->entry;
900 entry = debugfs_create_file("filter", 0644, system->entry, system,
901 &ftrace_subsystem_filter_fops);
903 kfree(system->filter);
904 system->filter = NULL;
905 pr_warning("Could not create debugfs "
906 "'%s/filter' entry\n", name);
909 trace_create_file("enable", 0644, system->entry,
910 (void *)system->name,
911 &ftrace_system_enable_fops);
913 return system->entry;
917 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
918 const struct file_operations *id,
919 const struct file_operations *enable,
920 const struct file_operations *filter,
921 const struct file_operations *format)
926 * If the trace point header did not define TRACE_SYSTEM
927 * then the system would be called "TRACE_SYSTEM".
929 if (strcmp(call->system, TRACE_SYSTEM) != 0)
930 d_events = event_subsystem_dir(call->system, d_events);
932 call->dir = debugfs_create_dir(call->name, d_events);
934 pr_warning("Could not create debugfs "
935 "'%s' directory\n", call->name);
940 trace_create_file("enable", 0644, call->dir, call,
943 if (call->id && call->profile_enable)
944 trace_create_file("id", 0444, call->dir, call,
947 if (call->define_fields) {
948 ret = trace_define_common_fields(call);
950 ret = call->define_fields(call);
952 pr_warning("Could not initialize trace point"
953 " events/%s\n", call->name);
956 trace_create_file("filter", 0644, call->dir, call,
960 trace_create_file("format", 0444, call->dir, call,
966 static int __trace_add_event_call(struct ftrace_event_call *call)
968 struct dentry *d_events;
974 if (call->raw_init) {
975 ret = call->raw_init(call);
978 pr_warning("Could not initialize trace "
979 "events/%s\n", call->name);
984 d_events = event_trace_events_dir();
988 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
989 &ftrace_enable_fops, &ftrace_event_filter_fops,
990 &ftrace_event_format_fops);
992 list_add(&call->list, &ftrace_events);
997 /* Add an additional event_call dynamically */
998 int trace_add_event_call(struct ftrace_event_call *call)
1001 mutex_lock(&event_mutex);
1002 ret = __trace_add_event_call(call);
1003 mutex_unlock(&event_mutex);
1007 static void remove_subsystem_dir(const char *name)
1009 struct event_subsystem *system;
1011 if (strcmp(name, TRACE_SYSTEM) == 0)
1014 list_for_each_entry(system, &event_subsystems, list) {
1015 if (strcmp(system->name, name) == 0) {
1016 if (!--system->nr_events) {
1017 struct event_filter *filter = system->filter;
1019 debugfs_remove_recursive(system->entry);
1020 list_del(&system->list);
1022 kfree(filter->filter_string);
1025 kfree(system->name);
1034 * Must be called under locking both of event_mutex and trace_event_mutex.
1036 static void __trace_remove_event_call(struct ftrace_event_call *call)
1038 ftrace_event_enable_disable(call, 0);
1040 __unregister_ftrace_event(call->event);
1041 debugfs_remove_recursive(call->dir);
1042 list_del(&call->list);
1043 trace_destroy_fields(call);
1044 destroy_preds(call);
1045 remove_subsystem_dir(call->system);
1048 /* Remove an event_call */
1049 void trace_remove_event_call(struct ftrace_event_call *call)
1051 mutex_lock(&event_mutex);
1052 down_write(&trace_event_mutex);
1053 __trace_remove_event_call(call);
1054 up_write(&trace_event_mutex);
1055 mutex_unlock(&event_mutex);
1058 #define for_each_event(event, start, end) \
1059 for (event = start; \
1060 (unsigned long)event < (unsigned long)end; \
1063 #ifdef CONFIG_MODULES
1065 static LIST_HEAD(ftrace_module_file_list);
1068 * Modules must own their file_operations to keep up with
1069 * reference counting.
1071 struct ftrace_module_file_ops {
1072 struct list_head list;
1074 struct file_operations id;
1075 struct file_operations enable;
1076 struct file_operations format;
1077 struct file_operations filter;
1080 static struct ftrace_module_file_ops *
1081 trace_create_file_ops(struct module *mod)
1083 struct ftrace_module_file_ops *file_ops;
1086 * This is a bit of a PITA. To allow for correct reference
1087 * counting, modules must "own" their file_operations.
1088 * To do this, we allocate the file operations that will be
1089 * used in the event directory.
1092 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1096 file_ops->mod = mod;
1098 file_ops->id = ftrace_event_id_fops;
1099 file_ops->id.owner = mod;
1101 file_ops->enable = ftrace_enable_fops;
1102 file_ops->enable.owner = mod;
1104 file_ops->filter = ftrace_event_filter_fops;
1105 file_ops->filter.owner = mod;
1107 file_ops->format = ftrace_event_format_fops;
1108 file_ops->format.owner = mod;
1110 list_add(&file_ops->list, &ftrace_module_file_list);
1115 static void trace_module_add_events(struct module *mod)
1117 struct ftrace_module_file_ops *file_ops = NULL;
1118 struct ftrace_event_call *call, *start, *end;
1119 struct dentry *d_events;
1122 start = mod->trace_events;
1123 end = mod->trace_events + mod->num_trace_events;
1128 d_events = event_trace_events_dir();
1132 for_each_event(call, start, end) {
1133 /* The linker may leave blanks */
1136 if (call->raw_init) {
1137 ret = call->raw_init(call);
1140 pr_warning("Could not initialize trace "
1141 "point events/%s\n", call->name);
1146 * This module has events, create file ops for this module
1147 * if not already done.
1150 file_ops = trace_create_file_ops(mod);
1155 ret = event_create_dir(call, d_events,
1156 &file_ops->id, &file_ops->enable,
1157 &file_ops->filter, &file_ops->format);
1159 list_add(&call->list, &ftrace_events);
1163 static void trace_module_remove_events(struct module *mod)
1165 struct ftrace_module_file_ops *file_ops;
1166 struct ftrace_event_call *call, *p;
1169 down_write(&trace_event_mutex);
1170 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1171 if (call->mod == mod) {
1173 __trace_remove_event_call(call);
1177 /* Now free the file_operations */
1178 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1179 if (file_ops->mod == mod)
1182 if (&file_ops->list != &ftrace_module_file_list) {
1183 list_del(&file_ops->list);
1188 * It is safest to reset the ring buffer if the module being unloaded
1189 * registered any events.
1192 tracing_reset_current_online_cpus();
1193 up_write(&trace_event_mutex);
1196 static int trace_module_notify(struct notifier_block *self,
1197 unsigned long val, void *data)
1199 struct module *mod = data;
1201 mutex_lock(&event_mutex);
1203 case MODULE_STATE_COMING:
1204 trace_module_add_events(mod);
1206 case MODULE_STATE_GOING:
1207 trace_module_remove_events(mod);
1210 mutex_unlock(&event_mutex);
1215 static int trace_module_notify(struct notifier_block *self,
1216 unsigned long val, void *data)
1220 #endif /* CONFIG_MODULES */
1222 static struct notifier_block trace_module_nb = {
1223 .notifier_call = trace_module_notify,
1227 extern struct ftrace_event_call __start_ftrace_events[];
1228 extern struct ftrace_event_call __stop_ftrace_events[];
1230 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1232 static __init int setup_trace_event(char *str)
1234 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1235 ring_buffer_expanded = 1;
1236 tracing_selftest_disabled = 1;
1240 __setup("trace_event=", setup_trace_event);
1242 static __init int event_trace_init(void)
1244 struct ftrace_event_call *call;
1245 struct dentry *d_tracer;
1246 struct dentry *entry;
1247 struct dentry *d_events;
1249 char *buf = bootup_event_buf;
1252 d_tracer = tracing_init_dentry();
1256 entry = debugfs_create_file("available_events", 0444, d_tracer,
1257 (void *)&show_event_seq_ops,
1258 &ftrace_avail_fops);
1260 pr_warning("Could not create debugfs "
1261 "'available_events' entry\n");
1263 entry = debugfs_create_file("set_event", 0644, d_tracer,
1264 (void *)&show_set_event_seq_ops,
1265 &ftrace_set_event_fops);
1267 pr_warning("Could not create debugfs "
1268 "'set_event' entry\n");
1270 d_events = event_trace_events_dir();
1274 /* ring buffer internal formats */
1275 trace_create_file("header_page", 0444, d_events,
1276 ring_buffer_print_page_header,
1277 &ftrace_show_header_fops);
1279 trace_create_file("header_event", 0444, d_events,
1280 ring_buffer_print_entry_header,
1281 &ftrace_show_header_fops);
1283 trace_create_file("enable", 0644, d_events,
1284 NULL, &ftrace_system_enable_fops);
1286 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1287 /* The linker may leave blanks */
1290 if (call->raw_init) {
1291 ret = call->raw_init(call);
1294 pr_warning("Could not initialize trace "
1295 "point events/%s\n", call->name);
1299 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1300 &ftrace_enable_fops,
1301 &ftrace_event_filter_fops,
1302 &ftrace_event_format_fops);
1304 list_add(&call->list, &ftrace_events);
1308 token = strsep(&buf, ",");
1315 ret = ftrace_set_clr_event(token, 1);
1317 pr_warning("Failed to enable trace event: %s\n", token);
1320 ret = register_module_notifier(&trace_module_nb);
1322 pr_warning("Failed to register trace events module notifier\n");
1326 fs_initcall(event_trace_init);
1328 #ifdef CONFIG_FTRACE_STARTUP_TEST
1330 static DEFINE_SPINLOCK(test_spinlock);
1331 static DEFINE_SPINLOCK(test_spinlock_irq);
1332 static DEFINE_MUTEX(test_mutex);
1334 static __init void test_work(struct work_struct *dummy)
1336 spin_lock(&test_spinlock);
1337 spin_lock_irq(&test_spinlock_irq);
1339 spin_unlock_irq(&test_spinlock_irq);
1340 spin_unlock(&test_spinlock);
1342 mutex_lock(&test_mutex);
1344 mutex_unlock(&test_mutex);
1347 static __init int event_test_thread(void *unused)
1351 test_malloc = kmalloc(1234, GFP_KERNEL);
1353 pr_info("failed to kmalloc\n");
1355 schedule_on_each_cpu(test_work);
1359 set_current_state(TASK_INTERRUPTIBLE);
1360 while (!kthread_should_stop())
1367 * Do various things that may trigger events.
1369 static __init void event_test_stuff(void)
1371 struct task_struct *test_thread;
1373 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1375 kthread_stop(test_thread);
1379 * For every trace event defined, we will test each trace point separately,
1380 * and then by groups, and finally all trace points.
1382 static __init void event_trace_self_tests(void)
1384 struct ftrace_event_call *call;
1385 struct event_subsystem *system;
1388 pr_info("Running tests on trace events:\n");
1390 list_for_each_entry(call, &ftrace_events, list) {
1392 /* Only test those that have a regfunc */
1397 * Testing syscall events here is pretty useless, but
1398 * we still do it if configured. But this is time consuming.
1399 * What we really need is a user thread to perform the
1400 * syscalls as we test.
1402 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1404 strcmp(call->system, "syscalls") == 0)
1408 pr_info("Testing event %s: ", call->name);
1411 * If an event is already enabled, someone is using
1412 * it and the self test should not be on.
1414 if (call->enabled) {
1415 pr_warning("Enabled event during self test!\n");
1420 ftrace_event_enable_disable(call, 1);
1422 ftrace_event_enable_disable(call, 0);
1427 /* Now test at the sub system level */
1429 pr_info("Running tests on trace event systems:\n");
1431 list_for_each_entry(system, &event_subsystems, list) {
1433 /* the ftrace system is special, skip it */
1434 if (strcmp(system->name, "ftrace") == 0)
1437 pr_info("Testing event system %s: ", system->name);
1439 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1440 if (WARN_ON_ONCE(ret)) {
1441 pr_warning("error enabling system %s\n",
1448 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1449 if (WARN_ON_ONCE(ret))
1450 pr_warning("error disabling system %s\n",
1456 /* Test with all events enabled */
1458 pr_info("Running tests on all trace events:\n");
1459 pr_info("Testing all events: ");
1461 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1462 if (WARN_ON_ONCE(ret)) {
1463 pr_warning("error enabling all events\n");
1470 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1471 if (WARN_ON_ONCE(ret)) {
1472 pr_warning("error disabling all events\n");
1479 #ifdef CONFIG_FUNCTION_TRACER
1481 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1484 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1486 struct ring_buffer_event *event;
1487 struct ring_buffer *buffer;
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(ftrace_test_event_disable, cpu));
1503 local_save_flags(flags);
1505 event = trace_current_buffer_lock_reserve(&buffer,
1506 TRACE_FN, sizeof(*entry),
1510 entry = ring_buffer_event_data(event);
1512 entry->parent_ip = parent_ip;
1514 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1517 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1518 ftrace_preempt_enable(resched);
1521 static struct ftrace_ops trace_ops __initdata =
1523 .func = function_test_events_call,
1526 static __init void event_trace_self_test_with_function(void)
1528 register_ftrace_function(&trace_ops);
1529 pr_info("Running tests again, along with the function tracer\n");
1530 event_trace_self_tests();
1531 unregister_ftrace_function(&trace_ops);
1534 static __init void event_trace_self_test_with_function(void)
1539 static __init int event_trace_self_tests_init(void)
1541 if (!tracing_selftest_disabled) {
1542 event_trace_self_tests();
1543 event_trace_self_test_with_function();
1549 late_initcall(event_trace_self_tests_init);