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/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex);
30 LIST_HEAD(ftrace_events);
33 trace_get_fields(struct ftrace_event_call *event_call)
35 if (!event_call->class->get_fields)
36 return &event_call->class->fields;
37 return event_call->class->get_fields(event_call);
40 int trace_define_field(struct ftrace_event_call *call, const char *type,
41 const char *name, int offset, int size, int is_signed,
44 struct ftrace_event_field *field;
45 struct list_head *head;
47 if (WARN_ON(!call->class))
50 field = kzalloc(sizeof(*field), GFP_KERNEL);
54 field->name = kstrdup(name, GFP_KERNEL);
58 field->type = kstrdup(type, GFP_KERNEL);
62 if (filter_type == FILTER_OTHER)
63 field->filter_type = filter_assign_type(type);
65 field->filter_type = filter_type;
67 field->offset = offset;
69 field->is_signed = is_signed;
71 head = trace_get_fields(call);
72 list_add(&field->link, head);
83 EXPORT_SYMBOL_GPL(trace_define_field);
85 #define __common_field(type, item) \
86 ret = trace_define_field(call, #type, "common_" #item, \
87 offsetof(typeof(ent), item), \
89 is_signed_type(type), FILTER_OTHER); \
93 static int trace_define_common_fields(struct ftrace_event_call *call)
96 struct trace_entry ent;
98 __common_field(unsigned short, type);
99 __common_field(unsigned char, flags);
100 __common_field(unsigned char, preempt_count);
101 __common_field(int, pid);
102 __common_field(int, lock_depth);
107 void trace_destroy_fields(struct ftrace_event_call *call)
109 struct ftrace_event_field *field, *next;
110 struct list_head *head;
112 head = trace_get_fields(call);
113 list_for_each_entry_safe(field, next, head, link) {
114 list_del(&field->link);
121 int trace_event_raw_init(struct ftrace_event_call *call)
125 id = register_ftrace_event(call->event);
132 EXPORT_SYMBOL_GPL(trace_event_raw_init);
134 static int ftrace_event_enable_disable(struct ftrace_event_call *call,
143 tracing_stop_cmdline_record();
144 if (call->class->reg)
145 call->class->reg(call, TRACE_REG_UNREGISTER);
147 tracepoint_probe_unregister(call->name,
153 if (!call->enabled) {
154 tracing_start_cmdline_record();
155 if (call->class->reg)
156 ret = call->class->reg(call, TRACE_REG_REGISTER);
158 ret = tracepoint_probe_register(call->name,
162 tracing_stop_cmdline_record();
163 pr_info("event trace: Could not enable event "
175 static void ftrace_clear_events(void)
177 struct ftrace_event_call *call;
179 mutex_lock(&event_mutex);
180 list_for_each_entry(call, &ftrace_events, list) {
181 ftrace_event_enable_disable(call, 0);
183 mutex_unlock(&event_mutex);
187 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
189 static int __ftrace_set_clr_event(const char *match, const char *sub,
190 const char *event, int set)
192 struct ftrace_event_call *call;
195 mutex_lock(&event_mutex);
196 list_for_each_entry(call, &ftrace_events, list) {
198 if (!call->name || !call->class ||
199 (!call->class->probe && !call->class->reg))
203 strcmp(match, call->name) != 0 &&
204 strcmp(match, call->class->system) != 0)
207 if (sub && strcmp(sub, call->class->system) != 0)
210 if (event && strcmp(event, call->name) != 0)
213 ftrace_event_enable_disable(call, set);
217 mutex_unlock(&event_mutex);
222 static int ftrace_set_clr_event(char *buf, int set)
224 char *event = NULL, *sub = NULL, *match;
227 * The buf format can be <subsystem>:<event-name>
228 * *:<event-name> means any event by that name.
229 * :<event-name> is the same.
231 * <subsystem>:* means all events in that subsystem
232 * <subsystem>: means the same.
234 * <name> (no ':') means all events in a subsystem with
235 * the name <name> or any event that matches <name>
238 match = strsep(&buf, ":");
244 if (!strlen(sub) || strcmp(sub, "*") == 0)
246 if (!strlen(event) || strcmp(event, "*") == 0)
250 return __ftrace_set_clr_event(match, sub, event, set);
254 * trace_set_clr_event - enable or disable an event
255 * @system: system name to match (NULL for any system)
256 * @event: event name to match (NULL for all events, within system)
257 * @set: 1 to enable, 0 to disable
259 * This is a way for other parts of the kernel to enable or disable
262 * Returns 0 on success, -EINVAL if the parameters do not match any
265 int trace_set_clr_event(const char *system, const char *event, int set)
267 return __ftrace_set_clr_event(NULL, system, event, set);
270 /* 128 should be much more than enough */
271 #define EVENT_BUF_SIZE 127
274 ftrace_event_write(struct file *file, const char __user *ubuf,
275 size_t cnt, loff_t *ppos)
277 struct trace_parser parser;
283 ret = tracing_update_buffers();
287 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
290 read = trace_get_user(&parser, ubuf, cnt, ppos);
292 if (read >= 0 && trace_parser_loaded((&parser))) {
295 if (*parser.buffer == '!')
298 parser.buffer[parser.idx] = 0;
300 ret = ftrace_set_clr_event(parser.buffer + !set, set);
308 trace_parser_put(&parser);
314 t_next(struct seq_file *m, void *v, loff_t *pos)
316 struct ftrace_event_call *call = v;
320 list_for_each_entry_continue(call, &ftrace_events, list) {
322 * The ftrace subsystem is for showing formats only.
323 * They can not be enabled or disabled via the event files.
325 if (call->class && (call->class->probe || call->class->reg))
332 static void *t_start(struct seq_file *m, loff_t *pos)
334 struct ftrace_event_call *call;
337 mutex_lock(&event_mutex);
339 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
340 for (l = 0; l <= *pos; ) {
341 call = t_next(m, call, &l);
349 s_next(struct seq_file *m, void *v, loff_t *pos)
351 struct ftrace_event_call *call = v;
355 list_for_each_entry_continue(call, &ftrace_events, list) {
363 static void *s_start(struct seq_file *m, loff_t *pos)
365 struct ftrace_event_call *call;
368 mutex_lock(&event_mutex);
370 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
371 for (l = 0; l <= *pos; ) {
372 call = s_next(m, call, &l);
379 static int t_show(struct seq_file *m, void *v)
381 struct ftrace_event_call *call = v;
383 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
384 seq_printf(m, "%s:", call->class->system);
385 seq_printf(m, "%s\n", call->name);
390 static void t_stop(struct seq_file *m, void *p)
392 mutex_unlock(&event_mutex);
396 ftrace_event_seq_open(struct inode *inode, struct file *file)
398 const struct seq_operations *seq_ops;
400 if ((file->f_mode & FMODE_WRITE) &&
401 (file->f_flags & O_TRUNC))
402 ftrace_clear_events();
404 seq_ops = inode->i_private;
405 return seq_open(file, seq_ops);
409 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
412 struct ftrace_event_call *call = filp->private_data;
420 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
424 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
427 struct ftrace_event_call *call = filp->private_data;
432 if (cnt >= sizeof(buf))
435 if (copy_from_user(&buf, ubuf, cnt))
440 ret = strict_strtoul(buf, 10, &val);
444 ret = tracing_update_buffers();
451 mutex_lock(&event_mutex);
452 ret = ftrace_event_enable_disable(call, val);
453 mutex_unlock(&event_mutex);
462 return ret ? ret : cnt;
466 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
469 const char set_to_char[4] = { '?', '0', '1', 'X' };
470 const char *system = filp->private_data;
471 struct ftrace_event_call *call;
476 mutex_lock(&event_mutex);
477 list_for_each_entry(call, &ftrace_events, list) {
478 if (!call->name || !call->class ||
479 (!call->class->probe && !call->class->reg))
482 if (system && strcmp(call->class->system, system) != 0)
486 * We need to find out if all the events are set
487 * or if all events or cleared, or if we have
490 set |= (1 << !!call->enabled);
493 * If we have a mixture, no need to look further.
498 mutex_unlock(&event_mutex);
500 buf[0] = set_to_char[set];
503 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
509 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
512 const char *system = filp->private_data;
517 if (cnt >= sizeof(buf))
520 if (copy_from_user(&buf, ubuf, cnt))
525 ret = strict_strtoul(buf, 10, &val);
529 ret = tracing_update_buffers();
533 if (val != 0 && val != 1)
536 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
549 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
552 struct ftrace_event_call *call = filp->private_data;
553 struct ftrace_event_field *field;
554 struct list_head *head;
556 int common_field_count = 5;
563 s = kmalloc(sizeof(*s), GFP_KERNEL);
569 trace_seq_printf(s, "name: %s\n", call->name);
570 trace_seq_printf(s, "ID: %d\n", call->id);
571 trace_seq_printf(s, "format:\n");
573 head = trace_get_fields(call);
574 list_for_each_entry_reverse(field, head, link) {
576 * Smartly shows the array type(except dynamic array).
579 * If TYPE := TYPE[LEN], it is shown:
580 * field:TYPE VAR[LEN]
582 const char *array_descriptor = strchr(field->type, '[');
584 if (!strncmp(field->type, "__data_loc", 10))
585 array_descriptor = NULL;
587 if (!array_descriptor) {
588 r = trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
589 "\tsize:%u;\tsigned:%d;\n",
590 field->type, field->name, field->offset,
591 field->size, !!field->is_signed);
593 r = trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
594 "\tsize:%u;\tsigned:%d;\n",
595 (int)(array_descriptor - field->type),
596 field->type, field->name,
597 array_descriptor, field->offset,
598 field->size, !!field->is_signed);
601 if (--common_field_count == 0)
602 r = trace_seq_printf(s, "\n");
609 r = trace_seq_printf(s, "\nprint fmt: %s\n",
614 * ug! The format output is bigger than a PAGE!!
616 buf = "FORMAT TOO BIG\n";
617 r = simple_read_from_buffer(ubuf, cnt, ppos,
622 r = simple_read_from_buffer(ubuf, cnt, ppos,
630 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
632 struct ftrace_event_call *call = filp->private_data;
639 s = kmalloc(sizeof(*s), GFP_KERNEL);
644 trace_seq_printf(s, "%d\n", call->id);
646 r = simple_read_from_buffer(ubuf, cnt, ppos,
653 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
656 struct ftrace_event_call *call = filp->private_data;
663 s = kmalloc(sizeof(*s), GFP_KERNEL);
669 print_event_filter(call, s);
670 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
678 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
681 struct ftrace_event_call *call = filp->private_data;
685 if (cnt >= PAGE_SIZE)
688 buf = (char *)__get_free_page(GFP_TEMPORARY);
692 if (copy_from_user(buf, ubuf, cnt)) {
693 free_page((unsigned long) buf);
698 err = apply_event_filter(call, buf);
699 free_page((unsigned long) buf);
709 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
712 struct event_subsystem *system = filp->private_data;
719 s = kmalloc(sizeof(*s), GFP_KERNEL);
725 print_subsystem_event_filter(system, s);
726 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
734 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
737 struct event_subsystem *system = filp->private_data;
741 if (cnt >= PAGE_SIZE)
744 buf = (char *)__get_free_page(GFP_TEMPORARY);
748 if (copy_from_user(buf, ubuf, cnt)) {
749 free_page((unsigned long) buf);
754 err = apply_subsystem_event_filter(system, buf);
755 free_page((unsigned long) buf);
765 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
767 int (*func)(struct trace_seq *s) = filp->private_data;
774 s = kmalloc(sizeof(*s), GFP_KERNEL);
781 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
788 static const struct seq_operations show_event_seq_ops = {
795 static const struct seq_operations show_set_event_seq_ops = {
802 static const struct file_operations ftrace_avail_fops = {
803 .open = ftrace_event_seq_open,
806 .release = seq_release,
809 static const struct file_operations ftrace_set_event_fops = {
810 .open = ftrace_event_seq_open,
812 .write = ftrace_event_write,
814 .release = seq_release,
817 static const struct file_operations ftrace_enable_fops = {
818 .open = tracing_open_generic,
819 .read = event_enable_read,
820 .write = event_enable_write,
823 static const struct file_operations ftrace_event_format_fops = {
824 .open = tracing_open_generic,
825 .read = event_format_read,
828 static const struct file_operations ftrace_event_id_fops = {
829 .open = tracing_open_generic,
830 .read = event_id_read,
833 static const struct file_operations ftrace_event_filter_fops = {
834 .open = tracing_open_generic,
835 .read = event_filter_read,
836 .write = event_filter_write,
839 static const struct file_operations ftrace_subsystem_filter_fops = {
840 .open = tracing_open_generic,
841 .read = subsystem_filter_read,
842 .write = subsystem_filter_write,
845 static const struct file_operations ftrace_system_enable_fops = {
846 .open = tracing_open_generic,
847 .read = system_enable_read,
848 .write = system_enable_write,
851 static const struct file_operations ftrace_show_header_fops = {
852 .open = tracing_open_generic,
856 static struct dentry *event_trace_events_dir(void)
858 static struct dentry *d_tracer;
859 static struct dentry *d_events;
864 d_tracer = tracing_init_dentry();
868 d_events = debugfs_create_dir("events", d_tracer);
870 pr_warning("Could not create debugfs "
871 "'events' directory\n");
876 static LIST_HEAD(event_subsystems);
878 static struct dentry *
879 event_subsystem_dir(const char *name, struct dentry *d_events)
881 struct event_subsystem *system;
882 struct dentry *entry;
884 /* First see if we did not already create this dir */
885 list_for_each_entry(system, &event_subsystems, list) {
886 if (strcmp(system->name, name) == 0) {
888 return system->entry;
892 /* need to create new entry */
893 system = kmalloc(sizeof(*system), GFP_KERNEL);
895 pr_warning("No memory to create event subsystem %s\n",
900 system->entry = debugfs_create_dir(name, d_events);
901 if (!system->entry) {
902 pr_warning("Could not create event subsystem %s\n",
908 system->nr_events = 1;
909 system->name = kstrdup(name, GFP_KERNEL);
911 debugfs_remove(system->entry);
916 list_add(&system->list, &event_subsystems);
918 system->filter = NULL;
920 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
921 if (!system->filter) {
922 pr_warning("Could not allocate filter for subsystem "
924 return system->entry;
927 entry = debugfs_create_file("filter", 0644, system->entry, system,
928 &ftrace_subsystem_filter_fops);
930 kfree(system->filter);
931 system->filter = NULL;
932 pr_warning("Could not create debugfs "
933 "'%s/filter' entry\n", name);
936 trace_create_file("enable", 0644, system->entry,
937 (void *)system->name,
938 &ftrace_system_enable_fops);
940 return system->entry;
944 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
945 const struct file_operations *id,
946 const struct file_operations *enable,
947 const struct file_operations *filter,
948 const struct file_operations *format)
950 struct list_head *head;
954 * If the trace point header did not define TRACE_SYSTEM
955 * then the system would be called "TRACE_SYSTEM".
957 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
958 d_events = event_subsystem_dir(call->class->system, d_events);
960 call->dir = debugfs_create_dir(call->name, d_events);
962 pr_warning("Could not create debugfs "
963 "'%s' directory\n", call->name);
967 if (call->class->probe || call->class->reg)
968 trace_create_file("enable", 0644, call->dir, call,
971 #ifdef CONFIG_PERF_EVENTS
972 if (call->id && (call->class->perf_probe || call->class->reg))
973 trace_create_file("id", 0444, call->dir, call,
977 if (call->class->define_fields) {
979 * Other events may have the same class. Only update
980 * the fields if they are not already defined.
982 head = trace_get_fields(call);
983 if (list_empty(head)) {
984 ret = trace_define_common_fields(call);
986 ret = call->class->define_fields(call);
988 pr_warning("Could not initialize trace point"
989 " events/%s\n", call->name);
993 trace_create_file("filter", 0644, call->dir, call,
997 trace_create_file("format", 0444, call->dir, call,
1003 static int __trace_add_event_call(struct ftrace_event_call *call)
1005 struct dentry *d_events;
1011 if (call->raw_init) {
1012 ret = call->raw_init(call);
1015 pr_warning("Could not initialize trace "
1016 "events/%s\n", call->name);
1021 d_events = event_trace_events_dir();
1025 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1026 &ftrace_enable_fops, &ftrace_event_filter_fops,
1027 &ftrace_event_format_fops);
1029 list_add(&call->list, &ftrace_events);
1034 /* Add an additional event_call dynamically */
1035 int trace_add_event_call(struct ftrace_event_call *call)
1038 mutex_lock(&event_mutex);
1039 ret = __trace_add_event_call(call);
1040 mutex_unlock(&event_mutex);
1044 static void remove_subsystem_dir(const char *name)
1046 struct event_subsystem *system;
1048 if (strcmp(name, TRACE_SYSTEM) == 0)
1051 list_for_each_entry(system, &event_subsystems, list) {
1052 if (strcmp(system->name, name) == 0) {
1053 if (!--system->nr_events) {
1054 struct event_filter *filter = system->filter;
1056 debugfs_remove_recursive(system->entry);
1057 list_del(&system->list);
1059 kfree(filter->filter_string);
1062 kfree(system->name);
1071 * Must be called under locking both of event_mutex and trace_event_mutex.
1073 static void __trace_remove_event_call(struct ftrace_event_call *call)
1075 ftrace_event_enable_disable(call, 0);
1077 __unregister_ftrace_event(call->event);
1078 debugfs_remove_recursive(call->dir);
1079 list_del(&call->list);
1080 trace_destroy_fields(call);
1081 destroy_preds(call);
1082 remove_subsystem_dir(call->class->system);
1085 /* Remove an event_call */
1086 void trace_remove_event_call(struct ftrace_event_call *call)
1088 mutex_lock(&event_mutex);
1089 down_write(&trace_event_mutex);
1090 __trace_remove_event_call(call);
1091 up_write(&trace_event_mutex);
1092 mutex_unlock(&event_mutex);
1095 #define for_each_event(event, start, end) \
1096 for (event = start; \
1097 (unsigned long)event < (unsigned long)end; \
1100 #ifdef CONFIG_MODULES
1102 static LIST_HEAD(ftrace_module_file_list);
1105 * Modules must own their file_operations to keep up with
1106 * reference counting.
1108 struct ftrace_module_file_ops {
1109 struct list_head list;
1111 struct file_operations id;
1112 struct file_operations enable;
1113 struct file_operations format;
1114 struct file_operations filter;
1117 static struct ftrace_module_file_ops *
1118 trace_create_file_ops(struct module *mod)
1120 struct ftrace_module_file_ops *file_ops;
1123 * This is a bit of a PITA. To allow for correct reference
1124 * counting, modules must "own" their file_operations.
1125 * To do this, we allocate the file operations that will be
1126 * used in the event directory.
1129 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1133 file_ops->mod = mod;
1135 file_ops->id = ftrace_event_id_fops;
1136 file_ops->id.owner = mod;
1138 file_ops->enable = ftrace_enable_fops;
1139 file_ops->enable.owner = mod;
1141 file_ops->filter = ftrace_event_filter_fops;
1142 file_ops->filter.owner = mod;
1144 file_ops->format = ftrace_event_format_fops;
1145 file_ops->format.owner = mod;
1147 list_add(&file_ops->list, &ftrace_module_file_list);
1152 static void trace_module_add_events(struct module *mod)
1154 struct ftrace_module_file_ops *file_ops = NULL;
1155 struct ftrace_event_call *call, *start, *end;
1156 struct dentry *d_events;
1159 start = mod->trace_events;
1160 end = mod->trace_events + mod->num_trace_events;
1165 d_events = event_trace_events_dir();
1169 for_each_event(call, start, end) {
1170 /* The linker may leave blanks */
1173 if (call->raw_init) {
1174 ret = call->raw_init(call);
1177 pr_warning("Could not initialize trace "
1178 "point events/%s\n", call->name);
1183 * This module has events, create file ops for this module
1184 * if not already done.
1187 file_ops = trace_create_file_ops(mod);
1192 ret = event_create_dir(call, d_events,
1193 &file_ops->id, &file_ops->enable,
1194 &file_ops->filter, &file_ops->format);
1196 list_add(&call->list, &ftrace_events);
1200 static void trace_module_remove_events(struct module *mod)
1202 struct ftrace_module_file_ops *file_ops;
1203 struct ftrace_event_call *call, *p;
1206 down_write(&trace_event_mutex);
1207 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1208 if (call->mod == mod) {
1210 __trace_remove_event_call(call);
1214 /* Now free the file_operations */
1215 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1216 if (file_ops->mod == mod)
1219 if (&file_ops->list != &ftrace_module_file_list) {
1220 list_del(&file_ops->list);
1225 * It is safest to reset the ring buffer if the module being unloaded
1226 * registered any events.
1229 tracing_reset_current_online_cpus();
1230 up_write(&trace_event_mutex);
1233 static int trace_module_notify(struct notifier_block *self,
1234 unsigned long val, void *data)
1236 struct module *mod = data;
1238 mutex_lock(&event_mutex);
1240 case MODULE_STATE_COMING:
1241 trace_module_add_events(mod);
1243 case MODULE_STATE_GOING:
1244 trace_module_remove_events(mod);
1247 mutex_unlock(&event_mutex);
1252 static int trace_module_notify(struct notifier_block *self,
1253 unsigned long val, void *data)
1257 #endif /* CONFIG_MODULES */
1259 static struct notifier_block trace_module_nb = {
1260 .notifier_call = trace_module_notify,
1264 extern struct ftrace_event_call __start_ftrace_events[];
1265 extern struct ftrace_event_call __stop_ftrace_events[];
1267 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1269 static __init int setup_trace_event(char *str)
1271 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1272 ring_buffer_expanded = 1;
1273 tracing_selftest_disabled = 1;
1277 __setup("trace_event=", setup_trace_event);
1279 static __init int event_trace_init(void)
1281 struct ftrace_event_call *call;
1282 struct dentry *d_tracer;
1283 struct dentry *entry;
1284 struct dentry *d_events;
1286 char *buf = bootup_event_buf;
1289 d_tracer = tracing_init_dentry();
1293 entry = debugfs_create_file("available_events", 0444, d_tracer,
1294 (void *)&show_event_seq_ops,
1295 &ftrace_avail_fops);
1297 pr_warning("Could not create debugfs "
1298 "'available_events' entry\n");
1300 entry = debugfs_create_file("set_event", 0644, d_tracer,
1301 (void *)&show_set_event_seq_ops,
1302 &ftrace_set_event_fops);
1304 pr_warning("Could not create debugfs "
1305 "'set_event' entry\n");
1307 d_events = event_trace_events_dir();
1311 /* ring buffer internal formats */
1312 trace_create_file("header_page", 0444, d_events,
1313 ring_buffer_print_page_header,
1314 &ftrace_show_header_fops);
1316 trace_create_file("header_event", 0444, d_events,
1317 ring_buffer_print_entry_header,
1318 &ftrace_show_header_fops);
1320 trace_create_file("enable", 0644, d_events,
1321 NULL, &ftrace_system_enable_fops);
1323 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1324 /* The linker may leave blanks */
1327 if (call->raw_init) {
1328 ret = call->raw_init(call);
1331 pr_warning("Could not initialize trace "
1332 "point events/%s\n", call->name);
1336 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1337 &ftrace_enable_fops,
1338 &ftrace_event_filter_fops,
1339 &ftrace_event_format_fops);
1341 list_add(&call->list, &ftrace_events);
1345 token = strsep(&buf, ",");
1352 ret = ftrace_set_clr_event(token, 1);
1354 pr_warning("Failed to enable trace event: %s\n", token);
1357 ret = register_module_notifier(&trace_module_nb);
1359 pr_warning("Failed to register trace events module notifier\n");
1363 fs_initcall(event_trace_init);
1365 #ifdef CONFIG_FTRACE_STARTUP_TEST
1367 static DEFINE_SPINLOCK(test_spinlock);
1368 static DEFINE_SPINLOCK(test_spinlock_irq);
1369 static DEFINE_MUTEX(test_mutex);
1371 static __init void test_work(struct work_struct *dummy)
1373 spin_lock(&test_spinlock);
1374 spin_lock_irq(&test_spinlock_irq);
1376 spin_unlock_irq(&test_spinlock_irq);
1377 spin_unlock(&test_spinlock);
1379 mutex_lock(&test_mutex);
1381 mutex_unlock(&test_mutex);
1384 static __init int event_test_thread(void *unused)
1388 test_malloc = kmalloc(1234, GFP_KERNEL);
1390 pr_info("failed to kmalloc\n");
1392 schedule_on_each_cpu(test_work);
1396 set_current_state(TASK_INTERRUPTIBLE);
1397 while (!kthread_should_stop())
1404 * Do various things that may trigger events.
1406 static __init void event_test_stuff(void)
1408 struct task_struct *test_thread;
1410 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1412 kthread_stop(test_thread);
1416 * For every trace event defined, we will test each trace point separately,
1417 * and then by groups, and finally all trace points.
1419 static __init void event_trace_self_tests(void)
1421 struct ftrace_event_call *call;
1422 struct event_subsystem *system;
1425 pr_info("Running tests on trace events:\n");
1427 list_for_each_entry(call, &ftrace_events, list) {
1429 /* Only test those that have a probe */
1430 if (!call->class || !call->class->probe)
1434 * Testing syscall events here is pretty useless, but
1435 * we still do it if configured. But this is time consuming.
1436 * What we really need is a user thread to perform the
1437 * syscalls as we test.
1439 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1440 if (call->class->system &&
1441 strcmp(call->class->system, "syscalls") == 0)
1445 pr_info("Testing event %s: ", call->name);
1448 * If an event is already enabled, someone is using
1449 * it and the self test should not be on.
1451 if (call->enabled) {
1452 pr_warning("Enabled event during self test!\n");
1457 ftrace_event_enable_disable(call, 1);
1459 ftrace_event_enable_disable(call, 0);
1464 /* Now test at the sub system level */
1466 pr_info("Running tests on trace event systems:\n");
1468 list_for_each_entry(system, &event_subsystems, list) {
1470 /* the ftrace system is special, skip it */
1471 if (strcmp(system->name, "ftrace") == 0)
1474 pr_info("Testing event system %s: ", system->name);
1476 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1477 if (WARN_ON_ONCE(ret)) {
1478 pr_warning("error enabling system %s\n",
1485 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1486 if (WARN_ON_ONCE(ret))
1487 pr_warning("error disabling system %s\n",
1493 /* Test with all events enabled */
1495 pr_info("Running tests on all trace events:\n");
1496 pr_info("Testing all events: ");
1498 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1499 if (WARN_ON_ONCE(ret)) {
1500 pr_warning("error enabling all events\n");
1507 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1508 if (WARN_ON_ONCE(ret)) {
1509 pr_warning("error disabling all events\n");
1516 #ifdef CONFIG_FUNCTION_TRACER
1518 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1521 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1523 struct ring_buffer_event *event;
1524 struct ring_buffer *buffer;
1525 struct ftrace_entry *entry;
1526 unsigned long flags;
1532 pc = preempt_count();
1533 resched = ftrace_preempt_disable();
1534 cpu = raw_smp_processor_id();
1535 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1540 local_save_flags(flags);
1542 event = trace_current_buffer_lock_reserve(&buffer,
1543 TRACE_FN, sizeof(*entry),
1547 entry = ring_buffer_event_data(event);
1549 entry->parent_ip = parent_ip;
1551 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1554 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1555 ftrace_preempt_enable(resched);
1558 static struct ftrace_ops trace_ops __initdata =
1560 .func = function_test_events_call,
1563 static __init void event_trace_self_test_with_function(void)
1565 register_ftrace_function(&trace_ops);
1566 pr_info("Running tests again, along with the function tracer\n");
1567 event_trace_self_tests();
1568 unregister_ftrace_function(&trace_ops);
1571 static __init void event_trace_self_test_with_function(void)
1576 static __init int event_trace_self_tests_init(void)
1578 if (!tracing_selftest_disabled) {
1579 event_trace_self_tests();
1580 event_trace_self_test_with_function();
1586 late_initcall(event_trace_self_tests_init);