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);
32 #define COMMON_FIELD_COUNT 5
35 trace_get_fields(struct ftrace_event_call *event_call)
37 if (!event_call->class->get_fields)
38 return &event_call->class->fields;
39 return event_call->class->get_fields(event_call);
42 int trace_define_field(struct ftrace_event_call *call, const char *type,
43 const char *name, int offset, int size, int is_signed,
46 struct ftrace_event_field *field;
47 struct list_head *head;
49 if (WARN_ON(!call->class))
52 field = kzalloc(sizeof(*field), GFP_KERNEL);
56 field->name = kstrdup(name, GFP_KERNEL);
60 field->type = kstrdup(type, GFP_KERNEL);
64 if (filter_type == FILTER_OTHER)
65 field->filter_type = filter_assign_type(type);
67 field->filter_type = filter_type;
69 field->offset = offset;
71 field->is_signed = is_signed;
73 head = trace_get_fields(call);
74 list_add(&field->link, head);
85 EXPORT_SYMBOL_GPL(trace_define_field);
87 #define __common_field(type, item) \
88 ret = trace_define_field(call, #type, "common_" #item, \
89 offsetof(typeof(ent), item), \
91 is_signed_type(type), FILTER_OTHER); \
95 static int trace_define_common_fields(struct ftrace_event_call *call)
98 struct trace_entry ent;
100 __common_field(unsigned short, type);
101 __common_field(unsigned char, flags);
102 __common_field(unsigned char, preempt_count);
103 __common_field(int, pid);
104 __common_field(int, lock_depth);
109 void trace_destroy_fields(struct ftrace_event_call *call)
111 struct ftrace_event_field *field, *next;
112 struct list_head *head;
114 head = trace_get_fields(call);
115 list_for_each_entry_safe(field, next, head, link) {
116 list_del(&field->link);
123 int trace_event_raw_init(struct ftrace_event_call *call)
127 id = register_ftrace_event(&call->event);
133 EXPORT_SYMBOL_GPL(trace_event_raw_init);
135 static int ftrace_event_enable_disable(struct ftrace_event_call *call,
142 if (call->flags & TRACE_EVENT_FL_ENABLED) {
143 call->flags &= ~TRACE_EVENT_FL_ENABLED;
144 tracing_stop_cmdline_record();
145 if (call->class->reg)
146 call->class->reg(call, TRACE_REG_UNREGISTER);
148 tracepoint_probe_unregister(call->name,
154 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
155 tracing_start_cmdline_record();
156 if (call->class->reg)
157 ret = call->class->reg(call, TRACE_REG_REGISTER);
159 ret = tracepoint_probe_register(call->name,
163 tracing_stop_cmdline_record();
164 pr_info("event trace: Could not enable event "
168 call->flags |= TRACE_EVENT_FL_ENABLED;
176 static void ftrace_clear_events(void)
178 struct ftrace_event_call *call;
180 mutex_lock(&event_mutex);
181 list_for_each_entry(call, &ftrace_events, list) {
182 ftrace_event_enable_disable(call, 0);
184 mutex_unlock(&event_mutex);
188 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
190 static int __ftrace_set_clr_event(const char *match, const char *sub,
191 const char *event, int set)
193 struct ftrace_event_call *call;
196 mutex_lock(&event_mutex);
197 list_for_each_entry(call, &ftrace_events, list) {
199 if (!call->name || !call->class ||
200 (!call->class->probe && !call->class->reg))
204 strcmp(match, call->name) != 0 &&
205 strcmp(match, call->class->system) != 0)
208 if (sub && strcmp(sub, call->class->system) != 0)
211 if (event && strcmp(event, call->name) != 0)
214 ftrace_event_enable_disable(call, set);
218 mutex_unlock(&event_mutex);
223 static int ftrace_set_clr_event(char *buf, int set)
225 char *event = NULL, *sub = NULL, *match;
228 * The buf format can be <subsystem>:<event-name>
229 * *:<event-name> means any event by that name.
230 * :<event-name> is the same.
232 * <subsystem>:* means all events in that subsystem
233 * <subsystem>: means the same.
235 * <name> (no ':') means all events in a subsystem with
236 * the name <name> or any event that matches <name>
239 match = strsep(&buf, ":");
245 if (!strlen(sub) || strcmp(sub, "*") == 0)
247 if (!strlen(event) || strcmp(event, "*") == 0)
251 return __ftrace_set_clr_event(match, sub, event, set);
255 * trace_set_clr_event - enable or disable an event
256 * @system: system name to match (NULL for any system)
257 * @event: event name to match (NULL for all events, within system)
258 * @set: 1 to enable, 0 to disable
260 * This is a way for other parts of the kernel to enable or disable
263 * Returns 0 on success, -EINVAL if the parameters do not match any
266 int trace_set_clr_event(const char *system, const char *event, int set)
268 return __ftrace_set_clr_event(NULL, system, event, set);
271 /* 128 should be much more than enough */
272 #define EVENT_BUF_SIZE 127
275 ftrace_event_write(struct file *file, const char __user *ubuf,
276 size_t cnt, loff_t *ppos)
278 struct trace_parser parser;
284 ret = tracing_update_buffers();
288 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
291 read = trace_get_user(&parser, ubuf, cnt, ppos);
293 if (read >= 0 && trace_parser_loaded((&parser))) {
296 if (*parser.buffer == '!')
299 parser.buffer[parser.idx] = 0;
301 ret = ftrace_set_clr_event(parser.buffer + !set, set);
309 trace_parser_put(&parser);
315 t_next(struct seq_file *m, void *v, loff_t *pos)
317 struct ftrace_event_call *call = v;
321 list_for_each_entry_continue(call, &ftrace_events, list) {
323 * The ftrace subsystem is for showing formats only.
324 * They can not be enabled or disabled via the event files.
326 if (call->class && (call->class->probe || call->class->reg))
333 static void *t_start(struct seq_file *m, loff_t *pos)
335 struct ftrace_event_call *call;
338 mutex_lock(&event_mutex);
340 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
341 for (l = 0; l <= *pos; ) {
342 call = t_next(m, call, &l);
350 s_next(struct seq_file *m, void *v, loff_t *pos)
352 struct ftrace_event_call *call = v;
356 list_for_each_entry_continue(call, &ftrace_events, list) {
357 if (call->flags & TRACE_EVENT_FL_ENABLED)
364 static void *s_start(struct seq_file *m, loff_t *pos)
366 struct ftrace_event_call *call;
369 mutex_lock(&event_mutex);
371 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
372 for (l = 0; l <= *pos; ) {
373 call = s_next(m, call, &l);
380 static int t_show(struct seq_file *m, void *v)
382 struct ftrace_event_call *call = v;
384 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
385 seq_printf(m, "%s:", call->class->system);
386 seq_printf(m, "%s\n", call->name);
391 static void t_stop(struct seq_file *m, void *p)
393 mutex_unlock(&event_mutex);
397 ftrace_event_seq_open(struct inode *inode, struct file *file)
399 const struct seq_operations *seq_ops;
401 if ((file->f_mode & FMODE_WRITE) &&
402 (file->f_flags & O_TRUNC))
403 ftrace_clear_events();
405 seq_ops = inode->i_private;
406 return seq_open(file, seq_ops);
410 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
413 struct ftrace_event_call *call = filp->private_data;
416 if (call->flags & TRACE_EVENT_FL_ENABLED)
421 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
425 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
428 struct ftrace_event_call *call = filp->private_data;
433 if (cnt >= sizeof(buf))
436 if (copy_from_user(&buf, ubuf, cnt))
441 ret = strict_strtoul(buf, 10, &val);
445 ret = tracing_update_buffers();
452 mutex_lock(&event_mutex);
453 ret = ftrace_event_enable_disable(call, val);
454 mutex_unlock(&event_mutex);
463 return ret ? ret : cnt;
467 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
470 const char set_to_char[4] = { '?', '0', '1', 'X' };
471 const char *system = filp->private_data;
472 struct ftrace_event_call *call;
477 mutex_lock(&event_mutex);
478 list_for_each_entry(call, &ftrace_events, list) {
479 if (!call->name || !call->class ||
480 (!call->class->probe && !call->class->reg))
483 if (system && strcmp(call->class->system, system) != 0)
487 * We need to find out if all the events are set
488 * or if all events or cleared, or if we have
491 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
494 * If we have a mixture, no need to look further.
499 mutex_unlock(&event_mutex);
501 buf[0] = set_to_char[set];
504 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
510 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
513 const char *system = filp->private_data;
518 if (cnt >= sizeof(buf))
521 if (copy_from_user(&buf, ubuf, cnt))
526 ret = strict_strtoul(buf, 10, &val);
530 ret = tracing_update_buffers();
534 if (val != 0 && val != 1)
537 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
554 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
556 struct ftrace_event_call *call = m->private;
557 struct ftrace_event_field *field;
558 struct list_head *head;
563 head = trace_get_fields(call);
565 switch ((unsigned long)v) {
568 if (unlikely(list_empty(head)))
571 field = list_entry(head->prev, struct ftrace_event_field, link);
574 case FORMAT_PRINTFMT:
580 * To separate common fields from event fields, the
581 * LSB is set on the first event field. Clear it in case.
583 v = (void *)((unsigned long)v & ~1L);
586 if (field->link.prev == head)
587 return (void *)FORMAT_PRINTFMT;
589 field = list_entry(field->link.prev, struct ftrace_event_field, link);
591 /* Set the LSB to notify f_show to print an extra newline */
592 if (index == COMMON_FIELD_COUNT)
593 field = (struct ftrace_event_field *)
594 ((unsigned long)field | 1);
599 static void *f_start(struct seq_file *m, loff_t *pos)
604 /* Start by showing the header */
606 return (void *)FORMAT_HEADER;
608 p = (void *)FORMAT_HEADER;
610 p = f_next(m, p, &l);
611 } while (p && l < *pos);
616 static int f_show(struct seq_file *m, void *v)
618 struct ftrace_event_call *call = m->private;
619 struct ftrace_event_field *field;
620 const char *array_descriptor;
622 switch ((unsigned long)v) {
624 seq_printf(m, "name: %s\n", call->name);
625 seq_printf(m, "ID: %d\n", call->event.type);
626 seq_printf(m, "format:\n");
629 case FORMAT_PRINTFMT:
630 seq_printf(m, "\nprint fmt: %s\n",
636 * To separate common fields from event fields, the
637 * LSB is set on the first event field. Clear it and
638 * print a newline if it is set.
640 if ((unsigned long)v & 1) {
642 v = (void *)((unsigned long)v & ~1L);
648 * Smartly shows the array type(except dynamic array).
651 * If TYPE := TYPE[LEN], it is shown:
652 * field:TYPE VAR[LEN]
654 array_descriptor = strchr(field->type, '[');
656 if (!strncmp(field->type, "__data_loc", 10))
657 array_descriptor = NULL;
659 if (!array_descriptor)
660 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
661 field->type, field->name, field->offset,
662 field->size, !!field->is_signed);
664 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
665 (int)(array_descriptor - field->type),
666 field->type, field->name,
667 array_descriptor, field->offset,
668 field->size, !!field->is_signed);
673 static void f_stop(struct seq_file *m, void *p)
677 static const struct seq_operations trace_format_seq_ops = {
684 static int trace_format_open(struct inode *inode, struct file *file)
686 struct ftrace_event_call *call = inode->i_private;
690 ret = seq_open(file, &trace_format_seq_ops);
694 m = file->private_data;
701 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
703 struct ftrace_event_call *call = filp->private_data;
710 s = kmalloc(sizeof(*s), GFP_KERNEL);
715 trace_seq_printf(s, "%d\n", call->event.type);
717 r = simple_read_from_buffer(ubuf, cnt, ppos,
724 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
727 struct ftrace_event_call *call = filp->private_data;
734 s = kmalloc(sizeof(*s), GFP_KERNEL);
740 print_event_filter(call, s);
741 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
749 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
752 struct ftrace_event_call *call = filp->private_data;
756 if (cnt >= PAGE_SIZE)
759 buf = (char *)__get_free_page(GFP_TEMPORARY);
763 if (copy_from_user(buf, ubuf, cnt)) {
764 free_page((unsigned long) buf);
769 err = apply_event_filter(call, buf);
770 free_page((unsigned long) buf);
780 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
783 struct event_subsystem *system = filp->private_data;
790 s = kmalloc(sizeof(*s), GFP_KERNEL);
796 print_subsystem_event_filter(system, s);
797 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
805 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
808 struct event_subsystem *system = filp->private_data;
812 if (cnt >= PAGE_SIZE)
815 buf = (char *)__get_free_page(GFP_TEMPORARY);
819 if (copy_from_user(buf, ubuf, cnt)) {
820 free_page((unsigned long) buf);
825 err = apply_subsystem_event_filter(system, buf);
826 free_page((unsigned long) buf);
836 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
838 int (*func)(struct trace_seq *s) = filp->private_data;
845 s = kmalloc(sizeof(*s), GFP_KERNEL);
852 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
859 static const struct seq_operations show_event_seq_ops = {
866 static const struct seq_operations show_set_event_seq_ops = {
873 static const struct file_operations ftrace_avail_fops = {
874 .open = ftrace_event_seq_open,
877 .release = seq_release,
880 static const struct file_operations ftrace_set_event_fops = {
881 .open = ftrace_event_seq_open,
883 .write = ftrace_event_write,
885 .release = seq_release,
888 static const struct file_operations ftrace_enable_fops = {
889 .open = tracing_open_generic,
890 .read = event_enable_read,
891 .write = event_enable_write,
894 static const struct file_operations ftrace_event_format_fops = {
895 .open = trace_format_open,
898 .release = seq_release,
901 static const struct file_operations ftrace_event_id_fops = {
902 .open = tracing_open_generic,
903 .read = event_id_read,
906 static const struct file_operations ftrace_event_filter_fops = {
907 .open = tracing_open_generic,
908 .read = event_filter_read,
909 .write = event_filter_write,
912 static const struct file_operations ftrace_subsystem_filter_fops = {
913 .open = tracing_open_generic,
914 .read = subsystem_filter_read,
915 .write = subsystem_filter_write,
918 static const struct file_operations ftrace_system_enable_fops = {
919 .open = tracing_open_generic,
920 .read = system_enable_read,
921 .write = system_enable_write,
924 static const struct file_operations ftrace_show_header_fops = {
925 .open = tracing_open_generic,
929 static struct dentry *event_trace_events_dir(void)
931 static struct dentry *d_tracer;
932 static struct dentry *d_events;
937 d_tracer = tracing_init_dentry();
941 d_events = debugfs_create_dir("events", d_tracer);
943 pr_warning("Could not create debugfs "
944 "'events' directory\n");
949 static LIST_HEAD(event_subsystems);
951 static struct dentry *
952 event_subsystem_dir(const char *name, struct dentry *d_events)
954 struct event_subsystem *system;
955 struct dentry *entry;
957 /* First see if we did not already create this dir */
958 list_for_each_entry(system, &event_subsystems, list) {
959 if (strcmp(system->name, name) == 0) {
961 return system->entry;
965 /* need to create new entry */
966 system = kmalloc(sizeof(*system), GFP_KERNEL);
968 pr_warning("No memory to create event subsystem %s\n",
973 system->entry = debugfs_create_dir(name, d_events);
974 if (!system->entry) {
975 pr_warning("Could not create event subsystem %s\n",
981 system->nr_events = 1;
982 system->name = kstrdup(name, GFP_KERNEL);
984 debugfs_remove(system->entry);
989 list_add(&system->list, &event_subsystems);
991 system->filter = NULL;
993 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
994 if (!system->filter) {
995 pr_warning("Could not allocate filter for subsystem "
997 return system->entry;
1000 entry = debugfs_create_file("filter", 0644, system->entry, system,
1001 &ftrace_subsystem_filter_fops);
1003 kfree(system->filter);
1004 system->filter = NULL;
1005 pr_warning("Could not create debugfs "
1006 "'%s/filter' entry\n", name);
1009 trace_create_file("enable", 0644, system->entry,
1010 (void *)system->name,
1011 &ftrace_system_enable_fops);
1013 return system->entry;
1017 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
1018 const struct file_operations *id,
1019 const struct file_operations *enable,
1020 const struct file_operations *filter,
1021 const struct file_operations *format)
1023 struct list_head *head;
1027 * If the trace point header did not define TRACE_SYSTEM
1028 * then the system would be called "TRACE_SYSTEM".
1030 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1031 d_events = event_subsystem_dir(call->class->system, d_events);
1033 call->dir = debugfs_create_dir(call->name, d_events);
1035 pr_warning("Could not create debugfs "
1036 "'%s' directory\n", call->name);
1040 if (call->class->probe || call->class->reg)
1041 trace_create_file("enable", 0644, call->dir, call,
1044 #ifdef CONFIG_PERF_EVENTS
1045 if (call->event.type && (call->class->perf_probe || call->class->reg))
1046 trace_create_file("id", 0444, call->dir, call,
1050 if (call->class->define_fields) {
1052 * Other events may have the same class. Only update
1053 * the fields if they are not already defined.
1055 head = trace_get_fields(call);
1056 if (list_empty(head)) {
1057 ret = trace_define_common_fields(call);
1059 ret = call->class->define_fields(call);
1061 pr_warning("Could not initialize trace point"
1062 " events/%s\n", call->name);
1066 trace_create_file("filter", 0644, call->dir, call,
1070 trace_create_file("format", 0444, call->dir, call,
1076 static int __trace_add_event_call(struct ftrace_event_call *call)
1078 struct dentry *d_events;
1084 if (call->class->raw_init) {
1085 ret = call->class->raw_init(call);
1088 pr_warning("Could not initialize trace "
1089 "events/%s\n", call->name);
1094 d_events = event_trace_events_dir();
1098 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1099 &ftrace_enable_fops, &ftrace_event_filter_fops,
1100 &ftrace_event_format_fops);
1102 list_add(&call->list, &ftrace_events);
1107 /* Add an additional event_call dynamically */
1108 int trace_add_event_call(struct ftrace_event_call *call)
1111 mutex_lock(&event_mutex);
1112 ret = __trace_add_event_call(call);
1113 mutex_unlock(&event_mutex);
1117 static void remove_subsystem_dir(const char *name)
1119 struct event_subsystem *system;
1121 if (strcmp(name, TRACE_SYSTEM) == 0)
1124 list_for_each_entry(system, &event_subsystems, list) {
1125 if (strcmp(system->name, name) == 0) {
1126 if (!--system->nr_events) {
1127 struct event_filter *filter = system->filter;
1129 debugfs_remove_recursive(system->entry);
1130 list_del(&system->list);
1132 kfree(filter->filter_string);
1135 kfree(system->name);
1144 * Must be called under locking both of event_mutex and trace_event_mutex.
1146 static void __trace_remove_event_call(struct ftrace_event_call *call)
1148 ftrace_event_enable_disable(call, 0);
1149 if (call->event.funcs)
1150 __unregister_ftrace_event(&call->event);
1151 debugfs_remove_recursive(call->dir);
1152 list_del(&call->list);
1153 trace_destroy_fields(call);
1154 destroy_preds(call);
1155 remove_subsystem_dir(call->class->system);
1158 /* Remove an event_call */
1159 void trace_remove_event_call(struct ftrace_event_call *call)
1161 mutex_lock(&event_mutex);
1162 down_write(&trace_event_mutex);
1163 __trace_remove_event_call(call);
1164 up_write(&trace_event_mutex);
1165 mutex_unlock(&event_mutex);
1168 #define for_each_event(event, start, end) \
1169 for (event = start; \
1170 (unsigned long)event < (unsigned long)end; \
1173 #ifdef CONFIG_MODULES
1175 static LIST_HEAD(ftrace_module_file_list);
1178 * Modules must own their file_operations to keep up with
1179 * reference counting.
1181 struct ftrace_module_file_ops {
1182 struct list_head list;
1184 struct file_operations id;
1185 struct file_operations enable;
1186 struct file_operations format;
1187 struct file_operations filter;
1190 static struct ftrace_module_file_ops *
1191 trace_create_file_ops(struct module *mod)
1193 struct ftrace_module_file_ops *file_ops;
1196 * This is a bit of a PITA. To allow for correct reference
1197 * counting, modules must "own" their file_operations.
1198 * To do this, we allocate the file operations that will be
1199 * used in the event directory.
1202 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1206 file_ops->mod = mod;
1208 file_ops->id = ftrace_event_id_fops;
1209 file_ops->id.owner = mod;
1211 file_ops->enable = ftrace_enable_fops;
1212 file_ops->enable.owner = mod;
1214 file_ops->filter = ftrace_event_filter_fops;
1215 file_ops->filter.owner = mod;
1217 file_ops->format = ftrace_event_format_fops;
1218 file_ops->format.owner = mod;
1220 list_add(&file_ops->list, &ftrace_module_file_list);
1225 static void trace_module_add_events(struct module *mod)
1227 struct ftrace_module_file_ops *file_ops = NULL;
1228 struct ftrace_event_call *call, *start, *end;
1229 struct dentry *d_events;
1232 start = mod->trace_events;
1233 end = mod->trace_events + mod->num_trace_events;
1238 d_events = event_trace_events_dir();
1242 for_each_event(call, start, end) {
1243 /* The linker may leave blanks */
1246 if (call->class->raw_init) {
1247 ret = call->class->raw_init(call);
1250 pr_warning("Could not initialize trace "
1251 "point events/%s\n", call->name);
1256 * This module has events, create file ops for this module
1257 * if not already done.
1260 file_ops = trace_create_file_ops(mod);
1265 ret = event_create_dir(call, d_events,
1266 &file_ops->id, &file_ops->enable,
1267 &file_ops->filter, &file_ops->format);
1269 list_add(&call->list, &ftrace_events);
1273 static void trace_module_remove_events(struct module *mod)
1275 struct ftrace_module_file_ops *file_ops;
1276 struct ftrace_event_call *call, *p;
1279 down_write(&trace_event_mutex);
1280 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1281 if (call->mod == mod) {
1283 __trace_remove_event_call(call);
1287 /* Now free the file_operations */
1288 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1289 if (file_ops->mod == mod)
1292 if (&file_ops->list != &ftrace_module_file_list) {
1293 list_del(&file_ops->list);
1298 * It is safest to reset the ring buffer if the module being unloaded
1299 * registered any events.
1302 tracing_reset_current_online_cpus();
1303 up_write(&trace_event_mutex);
1306 static int trace_module_notify(struct notifier_block *self,
1307 unsigned long val, void *data)
1309 struct module *mod = data;
1311 mutex_lock(&event_mutex);
1313 case MODULE_STATE_COMING:
1314 trace_module_add_events(mod);
1316 case MODULE_STATE_GOING:
1317 trace_module_remove_events(mod);
1320 mutex_unlock(&event_mutex);
1325 static int trace_module_notify(struct notifier_block *self,
1326 unsigned long val, void *data)
1330 #endif /* CONFIG_MODULES */
1332 static struct notifier_block trace_module_nb = {
1333 .notifier_call = trace_module_notify,
1337 extern struct ftrace_event_call __start_ftrace_events[];
1338 extern struct ftrace_event_call __stop_ftrace_events[];
1340 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1342 static __init int setup_trace_event(char *str)
1344 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1345 ring_buffer_expanded = 1;
1346 tracing_selftest_disabled = 1;
1350 __setup("trace_event=", setup_trace_event);
1352 static __init int event_trace_init(void)
1354 struct ftrace_event_call *call;
1355 struct dentry *d_tracer;
1356 struct dentry *entry;
1357 struct dentry *d_events;
1359 char *buf = bootup_event_buf;
1362 d_tracer = tracing_init_dentry();
1366 entry = debugfs_create_file("available_events", 0444, d_tracer,
1367 (void *)&show_event_seq_ops,
1368 &ftrace_avail_fops);
1370 pr_warning("Could not create debugfs "
1371 "'available_events' entry\n");
1373 entry = debugfs_create_file("set_event", 0644, d_tracer,
1374 (void *)&show_set_event_seq_ops,
1375 &ftrace_set_event_fops);
1377 pr_warning("Could not create debugfs "
1378 "'set_event' entry\n");
1380 d_events = event_trace_events_dir();
1384 /* ring buffer internal formats */
1385 trace_create_file("header_page", 0444, d_events,
1386 ring_buffer_print_page_header,
1387 &ftrace_show_header_fops);
1389 trace_create_file("header_event", 0444, d_events,
1390 ring_buffer_print_entry_header,
1391 &ftrace_show_header_fops);
1393 trace_create_file("enable", 0644, d_events,
1394 NULL, &ftrace_system_enable_fops);
1396 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1397 /* The linker may leave blanks */
1400 if (call->class->raw_init) {
1401 ret = call->class->raw_init(call);
1404 pr_warning("Could not initialize trace "
1405 "point events/%s\n", call->name);
1409 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1410 &ftrace_enable_fops,
1411 &ftrace_event_filter_fops,
1412 &ftrace_event_format_fops);
1414 list_add(&call->list, &ftrace_events);
1418 token = strsep(&buf, ",");
1425 ret = ftrace_set_clr_event(token, 1);
1427 pr_warning("Failed to enable trace event: %s\n", token);
1430 ret = register_module_notifier(&trace_module_nb);
1432 pr_warning("Failed to register trace events module notifier\n");
1436 fs_initcall(event_trace_init);
1438 #ifdef CONFIG_FTRACE_STARTUP_TEST
1440 static DEFINE_SPINLOCK(test_spinlock);
1441 static DEFINE_SPINLOCK(test_spinlock_irq);
1442 static DEFINE_MUTEX(test_mutex);
1444 static __init void test_work(struct work_struct *dummy)
1446 spin_lock(&test_spinlock);
1447 spin_lock_irq(&test_spinlock_irq);
1449 spin_unlock_irq(&test_spinlock_irq);
1450 spin_unlock(&test_spinlock);
1452 mutex_lock(&test_mutex);
1454 mutex_unlock(&test_mutex);
1457 static __init int event_test_thread(void *unused)
1461 test_malloc = kmalloc(1234, GFP_KERNEL);
1463 pr_info("failed to kmalloc\n");
1465 schedule_on_each_cpu(test_work);
1469 set_current_state(TASK_INTERRUPTIBLE);
1470 while (!kthread_should_stop())
1477 * Do various things that may trigger events.
1479 static __init void event_test_stuff(void)
1481 struct task_struct *test_thread;
1483 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1485 kthread_stop(test_thread);
1489 * For every trace event defined, we will test each trace point separately,
1490 * and then by groups, and finally all trace points.
1492 static __init void event_trace_self_tests(void)
1494 struct ftrace_event_call *call;
1495 struct event_subsystem *system;
1498 pr_info("Running tests on trace events:\n");
1500 list_for_each_entry(call, &ftrace_events, list) {
1502 /* Only test those that have a probe */
1503 if (!call->class || !call->class->probe)
1507 * Testing syscall events here is pretty useless, but
1508 * we still do it if configured. But this is time consuming.
1509 * What we really need is a user thread to perform the
1510 * syscalls as we test.
1512 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1513 if (call->class->system &&
1514 strcmp(call->class->system, "syscalls") == 0)
1518 pr_info("Testing event %s: ", call->name);
1521 * If an event is already enabled, someone is using
1522 * it and the self test should not be on.
1524 if (call->flags & TRACE_EVENT_FL_ENABLED) {
1525 pr_warning("Enabled event during self test!\n");
1530 ftrace_event_enable_disable(call, 1);
1532 ftrace_event_enable_disable(call, 0);
1537 /* Now test at the sub system level */
1539 pr_info("Running tests on trace event systems:\n");
1541 list_for_each_entry(system, &event_subsystems, list) {
1543 /* the ftrace system is special, skip it */
1544 if (strcmp(system->name, "ftrace") == 0)
1547 pr_info("Testing event system %s: ", system->name);
1549 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1550 if (WARN_ON_ONCE(ret)) {
1551 pr_warning("error enabling system %s\n",
1558 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1559 if (WARN_ON_ONCE(ret))
1560 pr_warning("error disabling system %s\n",
1566 /* Test with all events enabled */
1568 pr_info("Running tests on all trace events:\n");
1569 pr_info("Testing all events: ");
1571 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1572 if (WARN_ON_ONCE(ret)) {
1573 pr_warning("error enabling all events\n");
1580 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1581 if (WARN_ON_ONCE(ret)) {
1582 pr_warning("error disabling all events\n");
1589 #ifdef CONFIG_FUNCTION_TRACER
1591 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1594 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1596 struct ring_buffer_event *event;
1597 struct ring_buffer *buffer;
1598 struct ftrace_entry *entry;
1599 unsigned long flags;
1605 pc = preempt_count();
1606 resched = ftrace_preempt_disable();
1607 cpu = raw_smp_processor_id();
1608 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1613 local_save_flags(flags);
1615 event = trace_current_buffer_lock_reserve(&buffer,
1616 TRACE_FN, sizeof(*entry),
1620 entry = ring_buffer_event_data(event);
1622 entry->parent_ip = parent_ip;
1624 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1627 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1628 ftrace_preempt_enable(resched);
1631 static struct ftrace_ops trace_ops __initdata =
1633 .func = function_test_events_call,
1636 static __init void event_trace_self_test_with_function(void)
1638 register_ftrace_function(&trace_ops);
1639 pr_info("Running tests again, along with the function tracer\n");
1640 event_trace_self_tests();
1641 unregister_ftrace_function(&trace_ops);
1644 static __init void event_trace_self_test_with_function(void)
1649 static __init int event_trace_self_tests_init(void)
1651 if (!tracing_selftest_disabled) {
1652 event_trace_self_tests();
1653 event_trace_self_test_with_function();
1659 late_initcall(event_trace_self_tests_init);