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);
31 LIST_HEAD(ftrace_common_fields);
34 trace_get_fields(struct ftrace_event_call *event_call)
36 if (!event_call->class->get_fields)
37 return &event_call->class->fields;
38 return event_call->class->get_fields(event_call);
41 static int __trace_define_field(struct list_head *head, const char *type,
42 const char *name, int offset, int size,
43 int is_signed, int filter_type)
45 struct ftrace_event_field *field;
47 field = kzalloc(sizeof(*field), GFP_KERNEL);
51 field->name = kstrdup(name, GFP_KERNEL);
55 field->type = kstrdup(type, GFP_KERNEL);
59 if (filter_type == FILTER_OTHER)
60 field->filter_type = filter_assign_type(type);
62 field->filter_type = filter_type;
64 field->offset = offset;
66 field->is_signed = is_signed;
68 list_add(&field->link, head);
80 int trace_define_field(struct ftrace_event_call *call, const char *type,
81 const char *name, int offset, int size, int is_signed,
84 struct list_head *head;
86 if (WARN_ON(!call->class))
89 head = trace_get_fields(call);
90 return __trace_define_field(head, type, name, offset, size,
91 is_signed, filter_type);
93 EXPORT_SYMBOL_GPL(trace_define_field);
95 #define __common_field(type, item) \
96 ret = __trace_define_field(&ftrace_common_fields, #type, \
98 offsetof(typeof(ent), item), \
100 is_signed_type(type), FILTER_OTHER); \
104 static int trace_define_common_fields(void)
107 struct trace_entry ent;
109 __common_field(unsigned short, type);
110 __common_field(unsigned char, flags);
111 __common_field(unsigned char, preempt_count);
112 __common_field(int, pid);
113 __common_field(int, lock_depth);
118 void trace_destroy_fields(struct ftrace_event_call *call)
120 struct ftrace_event_field *field, *next;
121 struct list_head *head;
123 head = trace_get_fields(call);
124 list_for_each_entry_safe(field, next, head, link) {
125 list_del(&field->link);
132 int trace_event_raw_init(struct ftrace_event_call *call)
136 id = register_ftrace_event(&call->event);
142 EXPORT_SYMBOL_GPL(trace_event_raw_init);
144 int ftrace_event_reg(struct ftrace_event_call *call, enum trace_reg type)
147 case TRACE_REG_REGISTER:
148 return tracepoint_probe_register(call->name,
151 case TRACE_REG_UNREGISTER:
152 tracepoint_probe_unregister(call->name,
157 #ifdef CONFIG_PERF_EVENTS
158 case TRACE_REG_PERF_REGISTER:
159 return tracepoint_probe_register(call->name,
160 call->class->perf_probe,
162 case TRACE_REG_PERF_UNREGISTER:
163 tracepoint_probe_unregister(call->name,
164 call->class->perf_probe,
171 EXPORT_SYMBOL_GPL(ftrace_event_reg);
173 void trace_event_enable_cmd_record(bool enable)
175 struct ftrace_event_call *call;
177 mutex_lock(&event_mutex);
178 list_for_each_entry(call, &ftrace_events, list) {
179 if (!(call->flags & TRACE_EVENT_FL_ENABLED))
183 tracing_start_cmdline_record();
184 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
186 tracing_stop_cmdline_record();
187 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
190 mutex_unlock(&event_mutex);
193 static int ftrace_event_enable_disable(struct ftrace_event_call *call,
200 if (call->flags & TRACE_EVENT_FL_ENABLED) {
201 call->flags &= ~TRACE_EVENT_FL_ENABLED;
202 if (call->flags & TRACE_EVENT_FL_RECORDED_CMD) {
203 tracing_stop_cmdline_record();
204 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
206 call->class->reg(call, TRACE_REG_UNREGISTER);
210 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
211 if (trace_flags & TRACE_ITER_RECORD_CMD) {
212 tracing_start_cmdline_record();
213 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
215 ret = call->class->reg(call, TRACE_REG_REGISTER);
217 tracing_stop_cmdline_record();
218 pr_info("event trace: Could not enable event "
222 call->flags |= TRACE_EVENT_FL_ENABLED;
230 static void ftrace_clear_events(void)
232 struct ftrace_event_call *call;
234 mutex_lock(&event_mutex);
235 list_for_each_entry(call, &ftrace_events, list) {
236 ftrace_event_enable_disable(call, 0);
238 mutex_unlock(&event_mutex);
242 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
244 static int __ftrace_set_clr_event(const char *match, const char *sub,
245 const char *event, int set)
247 struct ftrace_event_call *call;
250 mutex_lock(&event_mutex);
251 list_for_each_entry(call, &ftrace_events, list) {
253 if (!call->name || !call->class || !call->class->reg)
257 strcmp(match, call->name) != 0 &&
258 strcmp(match, call->class->system) != 0)
261 if (sub && strcmp(sub, call->class->system) != 0)
264 if (event && strcmp(event, call->name) != 0)
267 ftrace_event_enable_disable(call, set);
271 mutex_unlock(&event_mutex);
276 static int ftrace_set_clr_event(char *buf, int set)
278 char *event = NULL, *sub = NULL, *match;
281 * The buf format can be <subsystem>:<event-name>
282 * *:<event-name> means any event by that name.
283 * :<event-name> is the same.
285 * <subsystem>:* means all events in that subsystem
286 * <subsystem>: means the same.
288 * <name> (no ':') means all events in a subsystem with
289 * the name <name> or any event that matches <name>
292 match = strsep(&buf, ":");
298 if (!strlen(sub) || strcmp(sub, "*") == 0)
300 if (!strlen(event) || strcmp(event, "*") == 0)
304 return __ftrace_set_clr_event(match, sub, event, set);
308 * trace_set_clr_event - enable or disable an event
309 * @system: system name to match (NULL for any system)
310 * @event: event name to match (NULL for all events, within system)
311 * @set: 1 to enable, 0 to disable
313 * This is a way for other parts of the kernel to enable or disable
316 * Returns 0 on success, -EINVAL if the parameters do not match any
319 int trace_set_clr_event(const char *system, const char *event, int set)
321 return __ftrace_set_clr_event(NULL, system, event, set);
324 /* 128 should be much more than enough */
325 #define EVENT_BUF_SIZE 127
328 ftrace_event_write(struct file *file, const char __user *ubuf,
329 size_t cnt, loff_t *ppos)
331 struct trace_parser parser;
337 ret = tracing_update_buffers();
341 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
344 read = trace_get_user(&parser, ubuf, cnt, ppos);
346 if (read >= 0 && trace_parser_loaded((&parser))) {
349 if (*parser.buffer == '!')
352 parser.buffer[parser.idx] = 0;
354 ret = ftrace_set_clr_event(parser.buffer + !set, set);
362 trace_parser_put(&parser);
368 t_next(struct seq_file *m, void *v, loff_t *pos)
370 struct ftrace_event_call *call = v;
374 list_for_each_entry_continue(call, &ftrace_events, list) {
376 * The ftrace subsystem is for showing formats only.
377 * They can not be enabled or disabled via the event files.
379 if (call->class && call->class->reg)
386 static void *t_start(struct seq_file *m, loff_t *pos)
388 struct ftrace_event_call *call;
391 mutex_lock(&event_mutex);
393 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
394 for (l = 0; l <= *pos; ) {
395 call = t_next(m, call, &l);
403 s_next(struct seq_file *m, void *v, loff_t *pos)
405 struct ftrace_event_call *call = v;
409 list_for_each_entry_continue(call, &ftrace_events, list) {
410 if (call->flags & TRACE_EVENT_FL_ENABLED)
417 static void *s_start(struct seq_file *m, loff_t *pos)
419 struct ftrace_event_call *call;
422 mutex_lock(&event_mutex);
424 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
425 for (l = 0; l <= *pos; ) {
426 call = s_next(m, call, &l);
433 static int t_show(struct seq_file *m, void *v)
435 struct ftrace_event_call *call = v;
437 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
438 seq_printf(m, "%s:", call->class->system);
439 seq_printf(m, "%s\n", call->name);
444 static void t_stop(struct seq_file *m, void *p)
446 mutex_unlock(&event_mutex);
450 ftrace_event_seq_open(struct inode *inode, struct file *file)
452 const struct seq_operations *seq_ops;
454 if ((file->f_mode & FMODE_WRITE) &&
455 (file->f_flags & O_TRUNC))
456 ftrace_clear_events();
458 seq_ops = inode->i_private;
459 return seq_open(file, seq_ops);
463 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
466 struct ftrace_event_call *call = filp->private_data;
469 if (call->flags & TRACE_EVENT_FL_ENABLED)
474 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
478 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
481 struct ftrace_event_call *call = filp->private_data;
486 if (cnt >= sizeof(buf))
489 if (copy_from_user(&buf, ubuf, cnt))
494 ret = strict_strtoul(buf, 10, &val);
498 ret = tracing_update_buffers();
505 mutex_lock(&event_mutex);
506 ret = ftrace_event_enable_disable(call, val);
507 mutex_unlock(&event_mutex);
516 return ret ? ret : cnt;
520 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
523 const char set_to_char[4] = { '?', '0', '1', 'X' };
524 const char *system = filp->private_data;
525 struct ftrace_event_call *call;
530 mutex_lock(&event_mutex);
531 list_for_each_entry(call, &ftrace_events, list) {
532 if (!call->name || !call->class || !call->class->reg)
535 if (system && strcmp(call->class->system, system) != 0)
539 * We need to find out if all the events are set
540 * or if all events or cleared, or if we have
543 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
546 * If we have a mixture, no need to look further.
551 mutex_unlock(&event_mutex);
553 buf[0] = set_to_char[set];
556 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
562 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
565 const char *system = filp->private_data;
570 if (cnt >= sizeof(buf))
573 if (copy_from_user(&buf, ubuf, cnt))
578 ret = strict_strtoul(buf, 10, &val);
582 ret = tracing_update_buffers();
586 if (val != 0 && val != 1)
589 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
601 static void print_event_fields(struct trace_seq *s, struct list_head *head)
603 struct ftrace_event_field *field;
605 list_for_each_entry_reverse(field, head, link) {
607 * Smartly shows the array type(except dynamic array).
610 * If TYPE := TYPE[LEN], it is shown:
611 * field:TYPE VAR[LEN]
613 const char *array_descriptor = strchr(field->type, '[');
615 if (!strncmp(field->type, "__data_loc", 10))
616 array_descriptor = NULL;
618 if (!array_descriptor) {
619 trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
620 "\tsize:%u;\tsigned:%d;\n",
621 field->type, field->name, field->offset,
622 field->size, !!field->is_signed);
624 trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
625 "\tsize:%u;\tsigned:%d;\n",
626 (int)(array_descriptor - field->type),
627 field->type, field->name,
628 array_descriptor, field->offset,
629 field->size, !!field->is_signed);
635 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
638 struct ftrace_event_call *call = filp->private_data;
639 struct list_head *head;
647 s = kmalloc(sizeof(*s), GFP_KERNEL);
653 trace_seq_printf(s, "name: %s\n", call->name);
654 trace_seq_printf(s, "ID: %d\n", call->event.type);
655 trace_seq_printf(s, "format:\n");
657 /* print common fields */
658 print_event_fields(s, &ftrace_common_fields);
660 trace_seq_putc(s, '\n');
662 /* print event specific fields */
663 head = trace_get_fields(call);
664 print_event_fields(s, head);
666 r = trace_seq_printf(s, "\nprint fmt: %s\n", call->print_fmt);
670 * ug! The format output is bigger than a PAGE!!
672 buf = "FORMAT TOO BIG\n";
673 r = simple_read_from_buffer(ubuf, cnt, ppos,
678 r = simple_read_from_buffer(ubuf, cnt, ppos,
686 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
688 struct ftrace_event_call *call = filp->private_data;
695 s = kmalloc(sizeof(*s), GFP_KERNEL);
700 trace_seq_printf(s, "%d\n", call->event.type);
702 r = simple_read_from_buffer(ubuf, cnt, ppos,
709 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
712 struct ftrace_event_call *call = filp->private_data;
719 s = kmalloc(sizeof(*s), GFP_KERNEL);
725 print_event_filter(call, s);
726 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
734 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
737 struct ftrace_event_call *call = 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_event_filter(call, buf);
755 free_page((unsigned long) buf);
765 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
768 struct event_subsystem *system = filp->private_data;
775 s = kmalloc(sizeof(*s), GFP_KERNEL);
781 print_subsystem_event_filter(system, s);
782 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
790 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
793 struct event_subsystem *system = filp->private_data;
797 if (cnt >= PAGE_SIZE)
800 buf = (char *)__get_free_page(GFP_TEMPORARY);
804 if (copy_from_user(buf, ubuf, cnt)) {
805 free_page((unsigned long) buf);
810 err = apply_subsystem_event_filter(system, buf);
811 free_page((unsigned long) buf);
821 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
823 int (*func)(struct trace_seq *s) = filp->private_data;
830 s = kmalloc(sizeof(*s), GFP_KERNEL);
837 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
844 static const struct seq_operations show_event_seq_ops = {
851 static const struct seq_operations show_set_event_seq_ops = {
858 static const struct file_operations ftrace_avail_fops = {
859 .open = ftrace_event_seq_open,
862 .release = seq_release,
865 static const struct file_operations ftrace_set_event_fops = {
866 .open = ftrace_event_seq_open,
868 .write = ftrace_event_write,
870 .release = seq_release,
873 static const struct file_operations ftrace_enable_fops = {
874 .open = tracing_open_generic,
875 .read = event_enable_read,
876 .write = event_enable_write,
879 static const struct file_operations ftrace_event_format_fops = {
880 .open = tracing_open_generic,
881 .read = event_format_read,
884 static const struct file_operations ftrace_event_id_fops = {
885 .open = tracing_open_generic,
886 .read = event_id_read,
889 static const struct file_operations ftrace_event_filter_fops = {
890 .open = tracing_open_generic,
891 .read = event_filter_read,
892 .write = event_filter_write,
895 static const struct file_operations ftrace_subsystem_filter_fops = {
896 .open = tracing_open_generic,
897 .read = subsystem_filter_read,
898 .write = subsystem_filter_write,
901 static const struct file_operations ftrace_system_enable_fops = {
902 .open = tracing_open_generic,
903 .read = system_enable_read,
904 .write = system_enable_write,
907 static const struct file_operations ftrace_show_header_fops = {
908 .open = tracing_open_generic,
912 static struct dentry *event_trace_events_dir(void)
914 static struct dentry *d_tracer;
915 static struct dentry *d_events;
920 d_tracer = tracing_init_dentry();
924 d_events = debugfs_create_dir("events", d_tracer);
926 pr_warning("Could not create debugfs "
927 "'events' directory\n");
932 static LIST_HEAD(event_subsystems);
934 static struct dentry *
935 event_subsystem_dir(const char *name, struct dentry *d_events)
937 struct event_subsystem *system;
938 struct dentry *entry;
940 /* First see if we did not already create this dir */
941 list_for_each_entry(system, &event_subsystems, list) {
942 if (strcmp(system->name, name) == 0) {
944 return system->entry;
948 /* need to create new entry */
949 system = kmalloc(sizeof(*system), GFP_KERNEL);
951 pr_warning("No memory to create event subsystem %s\n",
956 system->entry = debugfs_create_dir(name, d_events);
957 if (!system->entry) {
958 pr_warning("Could not create event subsystem %s\n",
964 system->nr_events = 1;
965 system->name = kstrdup(name, GFP_KERNEL);
967 debugfs_remove(system->entry);
972 list_add(&system->list, &event_subsystems);
974 system->filter = NULL;
976 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
977 if (!system->filter) {
978 pr_warning("Could not allocate filter for subsystem "
980 return system->entry;
983 entry = debugfs_create_file("filter", 0644, system->entry, system,
984 &ftrace_subsystem_filter_fops);
986 kfree(system->filter);
987 system->filter = NULL;
988 pr_warning("Could not create debugfs "
989 "'%s/filter' entry\n", name);
992 trace_create_file("enable", 0644, system->entry,
993 (void *)system->name,
994 &ftrace_system_enable_fops);
996 return system->entry;
1000 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
1001 const struct file_operations *id,
1002 const struct file_operations *enable,
1003 const struct file_operations *filter,
1004 const struct file_operations *format)
1006 struct list_head *head;
1010 * If the trace point header did not define TRACE_SYSTEM
1011 * then the system would be called "TRACE_SYSTEM".
1013 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1014 d_events = event_subsystem_dir(call->class->system, d_events);
1016 call->dir = debugfs_create_dir(call->name, d_events);
1018 pr_warning("Could not create debugfs "
1019 "'%s' directory\n", call->name);
1023 if (call->class->reg)
1024 trace_create_file("enable", 0644, call->dir, call,
1027 #ifdef CONFIG_PERF_EVENTS
1028 if (call->event.type && call->class->reg)
1029 trace_create_file("id", 0444, call->dir, call,
1034 * Other events may have the same class. Only update
1035 * the fields if they are not already defined.
1037 head = trace_get_fields(call);
1038 if (list_empty(head)) {
1039 ret = call->class->define_fields(call);
1041 pr_warning("Could not initialize trace point"
1042 " events/%s\n", call->name);
1046 trace_create_file("filter", 0644, call->dir, call,
1049 trace_create_file("format", 0444, call->dir, call,
1056 __trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
1057 const struct file_operations *id,
1058 const struct file_operations *enable,
1059 const struct file_operations *filter,
1060 const struct file_operations *format)
1062 struct dentry *d_events;
1065 /* The linker may leave blanks */
1069 if (call->class->raw_init) {
1070 ret = call->class->raw_init(call);
1073 pr_warning("Could not initialize trace events/%s\n",
1079 d_events = event_trace_events_dir();
1083 ret = event_create_dir(call, d_events, id, enable, filter, format);
1085 list_add(&call->list, &ftrace_events);
1091 /* Add an additional event_call dynamically */
1092 int trace_add_event_call(struct ftrace_event_call *call)
1095 mutex_lock(&event_mutex);
1096 ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1097 &ftrace_enable_fops,
1098 &ftrace_event_filter_fops,
1099 &ftrace_event_format_fops);
1100 mutex_unlock(&event_mutex);
1104 static void remove_subsystem_dir(const char *name)
1106 struct event_subsystem *system;
1108 if (strcmp(name, TRACE_SYSTEM) == 0)
1111 list_for_each_entry(system, &event_subsystems, list) {
1112 if (strcmp(system->name, name) == 0) {
1113 if (!--system->nr_events) {
1114 struct event_filter *filter = system->filter;
1116 debugfs_remove_recursive(system->entry);
1117 list_del(&system->list);
1119 kfree(filter->filter_string);
1122 kfree(system->name);
1131 * Must be called under locking both of event_mutex and trace_event_mutex.
1133 static void __trace_remove_event_call(struct ftrace_event_call *call)
1135 ftrace_event_enable_disable(call, 0);
1136 if (call->event.funcs)
1137 __unregister_ftrace_event(&call->event);
1138 debugfs_remove_recursive(call->dir);
1139 list_del(&call->list);
1140 trace_destroy_fields(call);
1141 destroy_preds(call);
1142 remove_subsystem_dir(call->class->system);
1145 /* Remove an event_call */
1146 void trace_remove_event_call(struct ftrace_event_call *call)
1148 mutex_lock(&event_mutex);
1149 down_write(&trace_event_mutex);
1150 __trace_remove_event_call(call);
1151 up_write(&trace_event_mutex);
1152 mutex_unlock(&event_mutex);
1155 #define for_each_event(event, start, end) \
1156 for (event = start; \
1157 (unsigned long)event < (unsigned long)end; \
1160 #ifdef CONFIG_MODULES
1162 static LIST_HEAD(ftrace_module_file_list);
1165 * Modules must own their file_operations to keep up with
1166 * reference counting.
1168 struct ftrace_module_file_ops {
1169 struct list_head list;
1171 struct file_operations id;
1172 struct file_operations enable;
1173 struct file_operations format;
1174 struct file_operations filter;
1177 static struct ftrace_module_file_ops *
1178 trace_create_file_ops(struct module *mod)
1180 struct ftrace_module_file_ops *file_ops;
1183 * This is a bit of a PITA. To allow for correct reference
1184 * counting, modules must "own" their file_operations.
1185 * To do this, we allocate the file operations that will be
1186 * used in the event directory.
1189 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1193 file_ops->mod = mod;
1195 file_ops->id = ftrace_event_id_fops;
1196 file_ops->id.owner = mod;
1198 file_ops->enable = ftrace_enable_fops;
1199 file_ops->enable.owner = mod;
1201 file_ops->filter = ftrace_event_filter_fops;
1202 file_ops->filter.owner = mod;
1204 file_ops->format = ftrace_event_format_fops;
1205 file_ops->format.owner = mod;
1207 list_add(&file_ops->list, &ftrace_module_file_list);
1212 static void trace_module_add_events(struct module *mod)
1214 struct ftrace_module_file_ops *file_ops = NULL;
1215 struct ftrace_event_call *call, *start, *end;
1217 start = mod->trace_events;
1218 end = mod->trace_events + mod->num_trace_events;
1223 file_ops = trace_create_file_ops(mod);
1227 for_each_event(call, start, end) {
1228 __trace_add_event_call(call, mod,
1229 &file_ops->id, &file_ops->enable,
1230 &file_ops->filter, &file_ops->format);
1234 static void trace_module_remove_events(struct module *mod)
1236 struct ftrace_module_file_ops *file_ops;
1237 struct ftrace_event_call *call, *p;
1240 down_write(&trace_event_mutex);
1241 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1242 if (call->mod == mod) {
1244 __trace_remove_event_call(call);
1248 /* Now free the file_operations */
1249 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1250 if (file_ops->mod == mod)
1253 if (&file_ops->list != &ftrace_module_file_list) {
1254 list_del(&file_ops->list);
1259 * It is safest to reset the ring buffer if the module being unloaded
1260 * registered any events.
1263 tracing_reset_current_online_cpus();
1264 up_write(&trace_event_mutex);
1267 static int trace_module_notify(struct notifier_block *self,
1268 unsigned long val, void *data)
1270 struct module *mod = data;
1272 mutex_lock(&event_mutex);
1274 case MODULE_STATE_COMING:
1275 trace_module_add_events(mod);
1277 case MODULE_STATE_GOING:
1278 trace_module_remove_events(mod);
1281 mutex_unlock(&event_mutex);
1286 static int trace_module_notify(struct notifier_block *self,
1287 unsigned long val, void *data)
1291 #endif /* CONFIG_MODULES */
1293 static struct notifier_block trace_module_nb = {
1294 .notifier_call = trace_module_notify,
1298 extern struct ftrace_event_call __start_ftrace_events[];
1299 extern struct ftrace_event_call __stop_ftrace_events[];
1301 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1303 static __init int setup_trace_event(char *str)
1305 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1306 ring_buffer_expanded = 1;
1307 tracing_selftest_disabled = 1;
1311 __setup("trace_event=", setup_trace_event);
1313 static __init int event_trace_init(void)
1315 struct ftrace_event_call *call;
1316 struct dentry *d_tracer;
1317 struct dentry *entry;
1318 struct dentry *d_events;
1320 char *buf = bootup_event_buf;
1323 d_tracer = tracing_init_dentry();
1327 entry = debugfs_create_file("available_events", 0444, d_tracer,
1328 (void *)&show_event_seq_ops,
1329 &ftrace_avail_fops);
1331 pr_warning("Could not create debugfs "
1332 "'available_events' entry\n");
1334 entry = debugfs_create_file("set_event", 0644, d_tracer,
1335 (void *)&show_set_event_seq_ops,
1336 &ftrace_set_event_fops);
1338 pr_warning("Could not create debugfs "
1339 "'set_event' entry\n");
1341 d_events = event_trace_events_dir();
1345 /* ring buffer internal formats */
1346 trace_create_file("header_page", 0444, d_events,
1347 ring_buffer_print_page_header,
1348 &ftrace_show_header_fops);
1350 trace_create_file("header_event", 0444, d_events,
1351 ring_buffer_print_entry_header,
1352 &ftrace_show_header_fops);
1354 trace_create_file("enable", 0644, d_events,
1355 NULL, &ftrace_system_enable_fops);
1357 if (trace_define_common_fields())
1358 pr_warning("tracing: Failed to allocate common fields");
1360 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1361 __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1362 &ftrace_enable_fops,
1363 &ftrace_event_filter_fops,
1364 &ftrace_event_format_fops);
1368 token = strsep(&buf, ",");
1375 ret = ftrace_set_clr_event(token, 1);
1377 pr_warning("Failed to enable trace event: %s\n", token);
1380 ret = register_module_notifier(&trace_module_nb);
1382 pr_warning("Failed to register trace events module notifier\n");
1386 fs_initcall(event_trace_init);
1388 #ifdef CONFIG_FTRACE_STARTUP_TEST
1390 static DEFINE_SPINLOCK(test_spinlock);
1391 static DEFINE_SPINLOCK(test_spinlock_irq);
1392 static DEFINE_MUTEX(test_mutex);
1394 static __init void test_work(struct work_struct *dummy)
1396 spin_lock(&test_spinlock);
1397 spin_lock_irq(&test_spinlock_irq);
1399 spin_unlock_irq(&test_spinlock_irq);
1400 spin_unlock(&test_spinlock);
1402 mutex_lock(&test_mutex);
1404 mutex_unlock(&test_mutex);
1407 static __init int event_test_thread(void *unused)
1411 test_malloc = kmalloc(1234, GFP_KERNEL);
1413 pr_info("failed to kmalloc\n");
1415 schedule_on_each_cpu(test_work);
1419 set_current_state(TASK_INTERRUPTIBLE);
1420 while (!kthread_should_stop())
1427 * Do various things that may trigger events.
1429 static __init void event_test_stuff(void)
1431 struct task_struct *test_thread;
1433 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1435 kthread_stop(test_thread);
1439 * For every trace event defined, we will test each trace point separately,
1440 * and then by groups, and finally all trace points.
1442 static __init void event_trace_self_tests(void)
1444 struct ftrace_event_call *call;
1445 struct event_subsystem *system;
1448 pr_info("Running tests on trace events:\n");
1450 list_for_each_entry(call, &ftrace_events, list) {
1452 /* Only test those that have a probe */
1453 if (!call->class || !call->class->probe)
1457 * Testing syscall events here is pretty useless, but
1458 * we still do it if configured. But this is time consuming.
1459 * What we really need is a user thread to perform the
1460 * syscalls as we test.
1462 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1463 if (call->class->system &&
1464 strcmp(call->class->system, "syscalls") == 0)
1468 pr_info("Testing event %s: ", call->name);
1471 * If an event is already enabled, someone is using
1472 * it and the self test should not be on.
1474 if (call->flags & TRACE_EVENT_FL_ENABLED) {
1475 pr_warning("Enabled event during self test!\n");
1480 ftrace_event_enable_disable(call, 1);
1482 ftrace_event_enable_disable(call, 0);
1487 /* Now test at the sub system level */
1489 pr_info("Running tests on trace event systems:\n");
1491 list_for_each_entry(system, &event_subsystems, list) {
1493 /* the ftrace system is special, skip it */
1494 if (strcmp(system->name, "ftrace") == 0)
1497 pr_info("Testing event system %s: ", system->name);
1499 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1500 if (WARN_ON_ONCE(ret)) {
1501 pr_warning("error enabling system %s\n",
1508 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1509 if (WARN_ON_ONCE(ret))
1510 pr_warning("error disabling system %s\n",
1516 /* Test with all events enabled */
1518 pr_info("Running tests on all trace events:\n");
1519 pr_info("Testing all events: ");
1521 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1522 if (WARN_ON_ONCE(ret)) {
1523 pr_warning("error enabling all events\n");
1530 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1531 if (WARN_ON_ONCE(ret)) {
1532 pr_warning("error disabling all events\n");
1539 #ifdef CONFIG_FUNCTION_TRACER
1541 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1544 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1546 struct ring_buffer_event *event;
1547 struct ring_buffer *buffer;
1548 struct ftrace_entry *entry;
1549 unsigned long flags;
1554 pc = preempt_count();
1555 preempt_disable_notrace();
1556 cpu = raw_smp_processor_id();
1557 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1562 local_save_flags(flags);
1564 event = trace_current_buffer_lock_reserve(&buffer,
1565 TRACE_FN, sizeof(*entry),
1569 entry = ring_buffer_event_data(event);
1571 entry->parent_ip = parent_ip;
1573 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1576 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1577 preempt_enable_notrace();
1580 static struct ftrace_ops trace_ops __initdata =
1582 .func = function_test_events_call,
1585 static __init void event_trace_self_test_with_function(void)
1587 register_ftrace_function(&trace_ops);
1588 pr_info("Running tests again, along with the function tracer\n");
1589 event_trace_self_tests();
1590 unregister_ftrace_function(&trace_ops);
1593 static __init void event_trace_self_test_with_function(void)
1598 static __init int event_trace_self_tests_init(void)
1600 if (!tracing_selftest_disabled) {
1601 event_trace_self_tests();
1602 event_trace_self_test_with_function();
1608 late_initcall(event_trace_self_tests_init);