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 DEFINE_MUTEX(event_storage_mutex);
31 EXPORT_SYMBOL_GPL(event_storage_mutex);
33 char event_storage[EVENT_STORAGE_SIZE];
34 EXPORT_SYMBOL_GPL(event_storage);
36 LIST_HEAD(ftrace_events);
37 LIST_HEAD(ftrace_common_fields);
40 trace_get_fields(struct ftrace_event_call *event_call)
42 if (!event_call->class->get_fields)
43 return &event_call->class->fields;
44 return event_call->class->get_fields(event_call);
47 static int __trace_define_field(struct list_head *head, const char *type,
48 const char *name, int offset, int size,
49 int is_signed, int filter_type)
51 struct ftrace_event_field *field;
53 field = kzalloc(sizeof(*field), GFP_KERNEL);
57 field->name = kstrdup(name, GFP_KERNEL);
61 field->type = kstrdup(type, GFP_KERNEL);
65 if (filter_type == FILTER_OTHER)
66 field->filter_type = filter_assign_type(type);
68 field->filter_type = filter_type;
70 field->offset = offset;
72 field->is_signed = is_signed;
74 list_add(&field->link, head);
86 int trace_define_field(struct ftrace_event_call *call, const char *type,
87 const char *name, int offset, int size, int is_signed,
90 struct list_head *head;
92 if (WARN_ON(!call->class))
95 head = trace_get_fields(call);
96 return __trace_define_field(head, type, name, offset, size,
97 is_signed, filter_type);
99 EXPORT_SYMBOL_GPL(trace_define_field);
101 #define __common_field(type, item) \
102 ret = __trace_define_field(&ftrace_common_fields, #type, \
104 offsetof(typeof(ent), item), \
106 is_signed_type(type), FILTER_OTHER); \
110 static int trace_define_common_fields(void)
113 struct trace_entry ent;
115 __common_field(unsigned short, type);
116 __common_field(unsigned char, flags);
117 __common_field(unsigned char, preempt_count);
118 __common_field(int, pid);
123 void trace_destroy_fields(struct ftrace_event_call *call)
125 struct ftrace_event_field *field, *next;
126 struct list_head *head;
128 head = trace_get_fields(call);
129 list_for_each_entry_safe(field, next, head, link) {
130 list_del(&field->link);
137 int trace_event_raw_init(struct ftrace_event_call *call)
141 id = register_ftrace_event(&call->event);
147 EXPORT_SYMBOL_GPL(trace_event_raw_init);
149 int ftrace_event_reg(struct ftrace_event_call *call,
150 enum trace_reg type, void *data)
153 case TRACE_REG_REGISTER:
154 return tracepoint_probe_register(call->name,
157 case TRACE_REG_UNREGISTER:
158 tracepoint_probe_unregister(call->name,
163 #ifdef CONFIG_PERF_EVENTS
164 case TRACE_REG_PERF_REGISTER:
165 return tracepoint_probe_register(call->name,
166 call->class->perf_probe,
168 case TRACE_REG_PERF_UNREGISTER:
169 tracepoint_probe_unregister(call->name,
170 call->class->perf_probe,
173 case TRACE_REG_PERF_OPEN:
174 case TRACE_REG_PERF_CLOSE:
175 case TRACE_REG_PERF_ADD:
176 case TRACE_REG_PERF_DEL:
182 EXPORT_SYMBOL_GPL(ftrace_event_reg);
184 void trace_event_enable_cmd_record(bool enable)
186 struct ftrace_event_call *call;
188 mutex_lock(&event_mutex);
189 list_for_each_entry(call, &ftrace_events, list) {
190 if (!(call->flags & TRACE_EVENT_FL_ENABLED))
194 tracing_start_cmdline_record();
195 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
197 tracing_stop_cmdline_record();
198 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
201 mutex_unlock(&event_mutex);
204 static int ftrace_event_enable_disable(struct ftrace_event_call *call,
211 if (call->flags & TRACE_EVENT_FL_ENABLED) {
212 call->flags &= ~TRACE_EVENT_FL_ENABLED;
213 if (call->flags & TRACE_EVENT_FL_RECORDED_CMD) {
214 tracing_stop_cmdline_record();
215 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
217 call->class->reg(call, TRACE_REG_UNREGISTER, NULL);
221 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
222 if (trace_flags & TRACE_ITER_RECORD_CMD) {
223 tracing_start_cmdline_record();
224 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
226 ret = call->class->reg(call, TRACE_REG_REGISTER, NULL);
228 tracing_stop_cmdline_record();
229 pr_info("event trace: Could not enable event "
233 call->flags |= TRACE_EVENT_FL_ENABLED;
241 static void ftrace_clear_events(void)
243 struct ftrace_event_call *call;
245 mutex_lock(&event_mutex);
246 list_for_each_entry(call, &ftrace_events, list) {
247 ftrace_event_enable_disable(call, 0);
249 mutex_unlock(&event_mutex);
252 static void __put_system(struct event_subsystem *system)
254 struct event_filter *filter = system->filter;
256 WARN_ON_ONCE(system->ref_count == 0);
257 if (--system->ref_count)
261 kfree(filter->filter_string);
268 static void __get_system(struct event_subsystem *system)
270 WARN_ON_ONCE(system->ref_count == 0);
274 static void put_system(struct event_subsystem *system)
276 mutex_lock(&event_mutex);
277 __put_system(system);
278 mutex_unlock(&event_mutex);
282 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
284 static int __ftrace_set_clr_event(const char *match, const char *sub,
285 const char *event, int set)
287 struct ftrace_event_call *call;
290 mutex_lock(&event_mutex);
291 list_for_each_entry(call, &ftrace_events, list) {
293 if (!call->name || !call->class || !call->class->reg)
296 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
300 strcmp(match, call->name) != 0 &&
301 strcmp(match, call->class->system) != 0)
304 if (sub && strcmp(sub, call->class->system) != 0)
307 if (event && strcmp(event, call->name) != 0)
310 ftrace_event_enable_disable(call, set);
314 mutex_unlock(&event_mutex);
319 static int ftrace_set_clr_event(char *buf, int set)
321 char *event = NULL, *sub = NULL, *match;
324 * The buf format can be <subsystem>:<event-name>
325 * *:<event-name> means any event by that name.
326 * :<event-name> is the same.
328 * <subsystem>:* means all events in that subsystem
329 * <subsystem>: means the same.
331 * <name> (no ':') means all events in a subsystem with
332 * the name <name> or any event that matches <name>
335 match = strsep(&buf, ":");
341 if (!strlen(sub) || strcmp(sub, "*") == 0)
343 if (!strlen(event) || strcmp(event, "*") == 0)
347 return __ftrace_set_clr_event(match, sub, event, set);
351 * trace_set_clr_event - enable or disable an event
352 * @system: system name to match (NULL for any system)
353 * @event: event name to match (NULL for all events, within system)
354 * @set: 1 to enable, 0 to disable
356 * This is a way for other parts of the kernel to enable or disable
359 * Returns 0 on success, -EINVAL if the parameters do not match any
362 int trace_set_clr_event(const char *system, const char *event, int set)
364 return __ftrace_set_clr_event(NULL, system, event, set);
366 EXPORT_SYMBOL_GPL(trace_set_clr_event);
368 /* 128 should be much more than enough */
369 #define EVENT_BUF_SIZE 127
372 ftrace_event_write(struct file *file, const char __user *ubuf,
373 size_t cnt, loff_t *ppos)
375 struct trace_parser parser;
381 ret = tracing_update_buffers();
385 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
388 read = trace_get_user(&parser, ubuf, cnt, ppos);
390 if (read >= 0 && trace_parser_loaded((&parser))) {
393 if (*parser.buffer == '!')
396 parser.buffer[parser.idx] = 0;
398 ret = ftrace_set_clr_event(parser.buffer + !set, set);
406 trace_parser_put(&parser);
412 t_next(struct seq_file *m, void *v, loff_t *pos)
414 struct ftrace_event_call *call = v;
418 list_for_each_entry_continue(call, &ftrace_events, list) {
420 * The ftrace subsystem is for showing formats only.
421 * They can not be enabled or disabled via the event files.
423 if (call->class && call->class->reg)
430 static void *t_start(struct seq_file *m, loff_t *pos)
432 struct ftrace_event_call *call;
435 mutex_lock(&event_mutex);
437 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
438 for (l = 0; l <= *pos; ) {
439 call = t_next(m, call, &l);
447 s_next(struct seq_file *m, void *v, loff_t *pos)
449 struct ftrace_event_call *call = v;
453 list_for_each_entry_continue(call, &ftrace_events, list) {
454 if (call->flags & TRACE_EVENT_FL_ENABLED)
461 static void *s_start(struct seq_file *m, loff_t *pos)
463 struct ftrace_event_call *call;
466 mutex_lock(&event_mutex);
468 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
469 for (l = 0; l <= *pos; ) {
470 call = s_next(m, call, &l);
477 static int t_show(struct seq_file *m, void *v)
479 struct ftrace_event_call *call = v;
481 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
482 seq_printf(m, "%s:", call->class->system);
483 seq_printf(m, "%s\n", call->name);
488 static void t_stop(struct seq_file *m, void *p)
490 mutex_unlock(&event_mutex);
494 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
497 struct ftrace_event_call *call = filp->private_data;
500 if (call->flags & TRACE_EVENT_FL_ENABLED)
505 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
509 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
512 struct ftrace_event_call *call = filp->private_data;
516 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
520 ret = tracing_update_buffers();
527 mutex_lock(&event_mutex);
528 ret = ftrace_event_enable_disable(call, val);
529 mutex_unlock(&event_mutex);
538 return ret ? ret : cnt;
542 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
545 const char set_to_char[4] = { '?', '0', '1', 'X' };
546 struct event_subsystem *system = filp->private_data;
547 struct ftrace_event_call *call;
552 mutex_lock(&event_mutex);
553 list_for_each_entry(call, &ftrace_events, list) {
554 if (!call->name || !call->class || !call->class->reg)
557 if (system && strcmp(call->class->system, system->name) != 0)
561 * We need to find out if all the events are set
562 * or if all events or cleared, or if we have
565 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
568 * If we have a mixture, no need to look further.
573 mutex_unlock(&event_mutex);
575 buf[0] = set_to_char[set];
578 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
584 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
587 struct event_subsystem *system = filp->private_data;
588 const char *name = NULL;
592 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
596 ret = tracing_update_buffers();
600 if (val != 0 && val != 1)
604 * Opening of "enable" adds a ref count to system,
605 * so the name is safe to use.
610 ret = __ftrace_set_clr_event(NULL, name, NULL, val);
624 FORMAT_FIELD_SEPERATOR = 2,
628 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
630 struct ftrace_event_call *call = m->private;
631 struct ftrace_event_field *field;
632 struct list_head *common_head = &ftrace_common_fields;
633 struct list_head *head = trace_get_fields(call);
637 switch ((unsigned long)v) {
639 if (unlikely(list_empty(common_head)))
642 field = list_entry(common_head->prev,
643 struct ftrace_event_field, link);
646 case FORMAT_FIELD_SEPERATOR:
647 if (unlikely(list_empty(head)))
650 field = list_entry(head->prev, struct ftrace_event_field, link);
653 case FORMAT_PRINTFMT:
659 if (field->link.prev == common_head)
660 return (void *)FORMAT_FIELD_SEPERATOR;
661 else if (field->link.prev == head)
662 return (void *)FORMAT_PRINTFMT;
664 field = list_entry(field->link.prev, struct ftrace_event_field, link);
669 static void *f_start(struct seq_file *m, loff_t *pos)
674 /* Start by showing the header */
676 return (void *)FORMAT_HEADER;
678 p = (void *)FORMAT_HEADER;
680 p = f_next(m, p, &l);
681 } while (p && l < *pos);
686 static int f_show(struct seq_file *m, void *v)
688 struct ftrace_event_call *call = m->private;
689 struct ftrace_event_field *field;
690 const char *array_descriptor;
692 switch ((unsigned long)v) {
694 seq_printf(m, "name: %s\n", call->name);
695 seq_printf(m, "ID: %d\n", call->event.type);
696 seq_printf(m, "format:\n");
699 case FORMAT_FIELD_SEPERATOR:
703 case FORMAT_PRINTFMT:
704 seq_printf(m, "\nprint fmt: %s\n",
712 * Smartly shows the array type(except dynamic array).
715 * If TYPE := TYPE[LEN], it is shown:
716 * field:TYPE VAR[LEN]
718 array_descriptor = strchr(field->type, '[');
720 if (!strncmp(field->type, "__data_loc", 10))
721 array_descriptor = NULL;
723 if (!array_descriptor)
724 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
725 field->type, field->name, field->offset,
726 field->size, !!field->is_signed);
728 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
729 (int)(array_descriptor - field->type),
730 field->type, field->name,
731 array_descriptor, field->offset,
732 field->size, !!field->is_signed);
737 static void f_stop(struct seq_file *m, void *p)
741 static const struct seq_operations trace_format_seq_ops = {
748 static int trace_format_open(struct inode *inode, struct file *file)
750 struct ftrace_event_call *call = inode->i_private;
754 ret = seq_open(file, &trace_format_seq_ops);
758 m = file->private_data;
765 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
767 struct ftrace_event_call *call = filp->private_data;
774 s = kmalloc(sizeof(*s), GFP_KERNEL);
779 trace_seq_printf(s, "%d\n", call->event.type);
781 r = simple_read_from_buffer(ubuf, cnt, ppos,
788 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
791 struct ftrace_event_call *call = filp->private_data;
798 s = kmalloc(sizeof(*s), GFP_KERNEL);
804 print_event_filter(call, s);
805 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
813 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
816 struct ftrace_event_call *call = filp->private_data;
820 if (cnt >= PAGE_SIZE)
823 buf = (char *)__get_free_page(GFP_TEMPORARY);
827 if (copy_from_user(buf, ubuf, cnt)) {
828 free_page((unsigned long) buf);
833 err = apply_event_filter(call, buf);
834 free_page((unsigned long) buf);
843 static LIST_HEAD(event_subsystems);
845 static int subsystem_open(struct inode *inode, struct file *filp)
847 struct event_subsystem *system = NULL;
850 if (!inode->i_private)
853 /* Make sure the system still exists */
854 mutex_lock(&event_mutex);
855 list_for_each_entry(system, &event_subsystems, list) {
856 if (system == inode->i_private) {
857 /* Don't open systems with no events */
858 if (!system->nr_events) {
862 __get_system(system);
866 mutex_unlock(&event_mutex);
868 if (system != inode->i_private)
872 ret = tracing_open_generic(inode, filp);
873 if (ret < 0 && system)
879 static int subsystem_release(struct inode *inode, struct file *file)
881 struct event_subsystem *system = inode->i_private;
890 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
893 struct event_subsystem *system = filp->private_data;
900 s = kmalloc(sizeof(*s), GFP_KERNEL);
906 print_subsystem_event_filter(system, s);
907 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
915 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
918 struct event_subsystem *system = filp->private_data;
922 if (cnt >= PAGE_SIZE)
925 buf = (char *)__get_free_page(GFP_TEMPORARY);
929 if (copy_from_user(buf, ubuf, cnt)) {
930 free_page((unsigned long) buf);
935 err = apply_subsystem_event_filter(system, buf);
936 free_page((unsigned long) buf);
946 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
948 int (*func)(struct trace_seq *s) = filp->private_data;
955 s = kmalloc(sizeof(*s), GFP_KERNEL);
962 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
969 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
970 static int ftrace_event_set_open(struct inode *inode, struct file *file);
972 static const struct seq_operations show_event_seq_ops = {
979 static const struct seq_operations show_set_event_seq_ops = {
986 static const struct file_operations ftrace_avail_fops = {
987 .open = ftrace_event_avail_open,
990 .release = seq_release,
993 static const struct file_operations ftrace_set_event_fops = {
994 .open = ftrace_event_set_open,
996 .write = ftrace_event_write,
998 .release = seq_release,
1001 static const struct file_operations ftrace_enable_fops = {
1002 .open = tracing_open_generic,
1003 .read = event_enable_read,
1004 .write = event_enable_write,
1005 .llseek = default_llseek,
1008 static const struct file_operations ftrace_event_format_fops = {
1009 .open = trace_format_open,
1011 .llseek = seq_lseek,
1012 .release = seq_release,
1015 static const struct file_operations ftrace_event_id_fops = {
1016 .open = tracing_open_generic,
1017 .read = event_id_read,
1018 .llseek = default_llseek,
1021 static const struct file_operations ftrace_event_filter_fops = {
1022 .open = tracing_open_generic,
1023 .read = event_filter_read,
1024 .write = event_filter_write,
1025 .llseek = default_llseek,
1028 static const struct file_operations ftrace_subsystem_filter_fops = {
1029 .open = subsystem_open,
1030 .read = subsystem_filter_read,
1031 .write = subsystem_filter_write,
1032 .llseek = default_llseek,
1033 .release = subsystem_release,
1036 static const struct file_operations ftrace_system_enable_fops = {
1037 .open = subsystem_open,
1038 .read = system_enable_read,
1039 .write = system_enable_write,
1040 .llseek = default_llseek,
1041 .release = subsystem_release,
1044 static const struct file_operations ftrace_show_header_fops = {
1045 .open = tracing_open_generic,
1046 .read = show_header,
1047 .llseek = default_llseek,
1050 static struct dentry *event_trace_events_dir(void)
1052 static struct dentry *d_tracer;
1053 static struct dentry *d_events;
1058 d_tracer = tracing_init_dentry();
1062 d_events = debugfs_create_dir("events", d_tracer);
1064 pr_warning("Could not create debugfs "
1065 "'events' directory\n");
1071 ftrace_event_avail_open(struct inode *inode, struct file *file)
1073 const struct seq_operations *seq_ops = &show_event_seq_ops;
1075 return seq_open(file, seq_ops);
1079 ftrace_event_set_open(struct inode *inode, struct file *file)
1081 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1083 if ((file->f_mode & FMODE_WRITE) &&
1084 (file->f_flags & O_TRUNC))
1085 ftrace_clear_events();
1087 return seq_open(file, seq_ops);
1090 static struct dentry *
1091 event_subsystem_dir(const char *name, struct dentry *d_events)
1093 struct event_subsystem *system;
1094 struct dentry *entry;
1096 /* First see if we did not already create this dir */
1097 list_for_each_entry(system, &event_subsystems, list) {
1098 if (strcmp(system->name, name) == 0) {
1099 system->nr_events++;
1100 return system->entry;
1104 /* need to create new entry */
1105 system = kmalloc(sizeof(*system), GFP_KERNEL);
1107 pr_warning("No memory to create event subsystem %s\n",
1112 system->entry = debugfs_create_dir(name, d_events);
1113 if (!system->entry) {
1114 pr_warning("Could not create event subsystem %s\n",
1120 system->nr_events = 1;
1121 system->ref_count = 1;
1122 system->name = kstrdup(name, GFP_KERNEL);
1123 if (!system->name) {
1124 debugfs_remove(system->entry);
1129 list_add(&system->list, &event_subsystems);
1131 system->filter = NULL;
1133 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1134 if (!system->filter) {
1135 pr_warning("Could not allocate filter for subsystem "
1137 return system->entry;
1140 entry = debugfs_create_file("filter", 0644, system->entry, system,
1141 &ftrace_subsystem_filter_fops);
1143 kfree(system->filter);
1144 system->filter = NULL;
1145 pr_warning("Could not create debugfs "
1146 "'%s/filter' entry\n", name);
1149 trace_create_file("enable", 0644, system->entry, system,
1150 &ftrace_system_enable_fops);
1152 return system->entry;
1156 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
1157 const struct file_operations *id,
1158 const struct file_operations *enable,
1159 const struct file_operations *filter,
1160 const struct file_operations *format)
1162 struct list_head *head;
1166 * If the trace point header did not define TRACE_SYSTEM
1167 * then the system would be called "TRACE_SYSTEM".
1169 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1170 d_events = event_subsystem_dir(call->class->system, d_events);
1172 call->dir = debugfs_create_dir(call->name, d_events);
1174 pr_warning("Could not create debugfs "
1175 "'%s' directory\n", call->name);
1179 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1180 trace_create_file("enable", 0644, call->dir, call,
1183 #ifdef CONFIG_PERF_EVENTS
1184 if (call->event.type && call->class->reg)
1185 trace_create_file("id", 0444, call->dir, call,
1190 * Other events may have the same class. Only update
1191 * the fields if they are not already defined.
1193 head = trace_get_fields(call);
1194 if (list_empty(head)) {
1195 ret = call->class->define_fields(call);
1197 pr_warning("Could not initialize trace point"
1198 " events/%s\n", call->name);
1202 trace_create_file("filter", 0644, call->dir, call,
1205 trace_create_file("format", 0444, call->dir, call,
1211 static void event_remove(struct ftrace_event_call *call)
1213 ftrace_event_enable_disable(call, 0);
1214 if (call->event.funcs)
1215 __unregister_ftrace_event(&call->event);
1216 list_del(&call->list);
1219 static int event_init(struct ftrace_event_call *call)
1223 if (WARN_ON(!call->name))
1226 if (call->class->raw_init) {
1227 ret = call->class->raw_init(call);
1228 if (ret < 0 && ret != -ENOSYS)
1229 pr_warn("Could not initialize trace events/%s\n",
1237 __trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
1238 const struct file_operations *id,
1239 const struct file_operations *enable,
1240 const struct file_operations *filter,
1241 const struct file_operations *format)
1243 struct dentry *d_events;
1246 ret = event_init(call);
1250 d_events = event_trace_events_dir();
1254 ret = event_create_dir(call, d_events, id, enable, filter, format);
1256 list_add(&call->list, &ftrace_events);
1262 /* Add an additional event_call dynamically */
1263 int trace_add_event_call(struct ftrace_event_call *call)
1266 mutex_lock(&event_mutex);
1267 ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1268 &ftrace_enable_fops,
1269 &ftrace_event_filter_fops,
1270 &ftrace_event_format_fops);
1271 mutex_unlock(&event_mutex);
1275 static void remove_subsystem_dir(const char *name)
1277 struct event_subsystem *system;
1279 if (strcmp(name, TRACE_SYSTEM) == 0)
1282 list_for_each_entry(system, &event_subsystems, list) {
1283 if (strcmp(system->name, name) == 0) {
1284 if (!--system->nr_events) {
1285 debugfs_remove_recursive(system->entry);
1286 list_del(&system->list);
1287 __put_system(system);
1295 * Must be called under locking both of event_mutex and trace_event_mutex.
1297 static void __trace_remove_event_call(struct ftrace_event_call *call)
1300 trace_destroy_fields(call);
1301 destroy_preds(call);
1302 debugfs_remove_recursive(call->dir);
1303 remove_subsystem_dir(call->class->system);
1306 /* Remove an event_call */
1307 void trace_remove_event_call(struct ftrace_event_call *call)
1309 mutex_lock(&event_mutex);
1310 down_write(&trace_event_mutex);
1311 __trace_remove_event_call(call);
1312 up_write(&trace_event_mutex);
1313 mutex_unlock(&event_mutex);
1316 #define for_each_event(event, start, end) \
1317 for (event = start; \
1318 (unsigned long)event < (unsigned long)end; \
1321 #ifdef CONFIG_MODULES
1323 static LIST_HEAD(ftrace_module_file_list);
1326 * Modules must own their file_operations to keep up with
1327 * reference counting.
1329 struct ftrace_module_file_ops {
1330 struct list_head list;
1332 struct file_operations id;
1333 struct file_operations enable;
1334 struct file_operations format;
1335 struct file_operations filter;
1338 static struct ftrace_module_file_ops *
1339 trace_create_file_ops(struct module *mod)
1341 struct ftrace_module_file_ops *file_ops;
1344 * This is a bit of a PITA. To allow for correct reference
1345 * counting, modules must "own" their file_operations.
1346 * To do this, we allocate the file operations that will be
1347 * used in the event directory.
1350 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1354 file_ops->mod = mod;
1356 file_ops->id = ftrace_event_id_fops;
1357 file_ops->id.owner = mod;
1359 file_ops->enable = ftrace_enable_fops;
1360 file_ops->enable.owner = mod;
1362 file_ops->filter = ftrace_event_filter_fops;
1363 file_ops->filter.owner = mod;
1365 file_ops->format = ftrace_event_format_fops;
1366 file_ops->format.owner = mod;
1368 list_add(&file_ops->list, &ftrace_module_file_list);
1373 static void trace_module_add_events(struct module *mod)
1375 struct ftrace_module_file_ops *file_ops = NULL;
1376 struct ftrace_event_call **call, **start, **end;
1378 start = mod->trace_events;
1379 end = mod->trace_events + mod->num_trace_events;
1384 file_ops = trace_create_file_ops(mod);
1388 for_each_event(call, start, end) {
1389 __trace_add_event_call(*call, mod,
1390 &file_ops->id, &file_ops->enable,
1391 &file_ops->filter, &file_ops->format);
1395 static void trace_module_remove_events(struct module *mod)
1397 struct ftrace_module_file_ops *file_ops;
1398 struct ftrace_event_call *call, *p;
1401 down_write(&trace_event_mutex);
1402 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1403 if (call->mod == mod) {
1405 __trace_remove_event_call(call);
1409 /* Now free the file_operations */
1410 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1411 if (file_ops->mod == mod)
1414 if (&file_ops->list != &ftrace_module_file_list) {
1415 list_del(&file_ops->list);
1420 * It is safest to reset the ring buffer if the module being unloaded
1421 * registered any events.
1424 tracing_reset_current_online_cpus();
1425 up_write(&trace_event_mutex);
1428 static int trace_module_notify(struct notifier_block *self,
1429 unsigned long val, void *data)
1431 struct module *mod = data;
1433 mutex_lock(&event_mutex);
1435 case MODULE_STATE_COMING:
1436 trace_module_add_events(mod);
1438 case MODULE_STATE_GOING:
1439 trace_module_remove_events(mod);
1442 mutex_unlock(&event_mutex);
1447 static int trace_module_notify(struct notifier_block *self,
1448 unsigned long val, void *data)
1452 #endif /* CONFIG_MODULES */
1454 static struct notifier_block trace_module_nb = {
1455 .notifier_call = trace_module_notify,
1459 extern struct ftrace_event_call *__start_ftrace_events[];
1460 extern struct ftrace_event_call *__stop_ftrace_events[];
1462 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1464 static __init int setup_trace_event(char *str)
1466 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1467 ring_buffer_expanded = 1;
1468 tracing_selftest_disabled = 1;
1472 __setup("trace_event=", setup_trace_event);
1474 static __init int event_trace_enable(void)
1476 struct ftrace_event_call **iter, *call;
1477 char *buf = bootup_event_buf;
1481 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
1484 ret = event_init(call);
1486 list_add(&call->list, &ftrace_events);
1490 token = strsep(&buf, ",");
1497 ret = ftrace_set_clr_event(token, 1);
1499 pr_warn("Failed to enable trace event: %s\n", token);
1502 trace_printk_start_comm();
1507 static __init int event_trace_init(void)
1509 struct ftrace_event_call *call;
1510 struct dentry *d_tracer;
1511 struct dentry *entry;
1512 struct dentry *d_events;
1515 d_tracer = tracing_init_dentry();
1519 entry = debugfs_create_file("available_events", 0444, d_tracer,
1520 NULL, &ftrace_avail_fops);
1522 pr_warning("Could not create debugfs "
1523 "'available_events' entry\n");
1525 entry = debugfs_create_file("set_event", 0644, d_tracer,
1526 NULL, &ftrace_set_event_fops);
1528 pr_warning("Could not create debugfs "
1529 "'set_event' entry\n");
1531 d_events = event_trace_events_dir();
1535 /* ring buffer internal formats */
1536 trace_create_file("header_page", 0444, d_events,
1537 ring_buffer_print_page_header,
1538 &ftrace_show_header_fops);
1540 trace_create_file("header_event", 0444, d_events,
1541 ring_buffer_print_entry_header,
1542 &ftrace_show_header_fops);
1544 trace_create_file("enable", 0644, d_events,
1545 NULL, &ftrace_system_enable_fops);
1547 if (trace_define_common_fields())
1548 pr_warning("tracing: Failed to allocate common fields");
1551 * Early initialization already enabled ftrace event.
1552 * Now it's only necessary to create the event directory.
1554 list_for_each_entry(call, &ftrace_events, list) {
1556 ret = event_create_dir(call, d_events,
1557 &ftrace_event_id_fops,
1558 &ftrace_enable_fops,
1559 &ftrace_event_filter_fops,
1560 &ftrace_event_format_fops);
1565 ret = register_module_notifier(&trace_module_nb);
1567 pr_warning("Failed to register trace events module notifier\n");
1571 core_initcall(event_trace_enable);
1572 fs_initcall(event_trace_init);
1574 #ifdef CONFIG_FTRACE_STARTUP_TEST
1576 static DEFINE_SPINLOCK(test_spinlock);
1577 static DEFINE_SPINLOCK(test_spinlock_irq);
1578 static DEFINE_MUTEX(test_mutex);
1580 static __init void test_work(struct work_struct *dummy)
1582 spin_lock(&test_spinlock);
1583 spin_lock_irq(&test_spinlock_irq);
1585 spin_unlock_irq(&test_spinlock_irq);
1586 spin_unlock(&test_spinlock);
1588 mutex_lock(&test_mutex);
1590 mutex_unlock(&test_mutex);
1593 static __init int event_test_thread(void *unused)
1597 test_malloc = kmalloc(1234, GFP_KERNEL);
1599 pr_info("failed to kmalloc\n");
1601 schedule_on_each_cpu(test_work);
1605 set_current_state(TASK_INTERRUPTIBLE);
1606 while (!kthread_should_stop())
1613 * Do various things that may trigger events.
1615 static __init void event_test_stuff(void)
1617 struct task_struct *test_thread;
1619 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1621 kthread_stop(test_thread);
1625 * For every trace event defined, we will test each trace point separately,
1626 * and then by groups, and finally all trace points.
1628 static __init void event_trace_self_tests(void)
1630 struct ftrace_event_call *call;
1631 struct event_subsystem *system;
1634 pr_info("Running tests on trace events:\n");
1636 list_for_each_entry(call, &ftrace_events, list) {
1638 /* Only test those that have a probe */
1639 if (!call->class || !call->class->probe)
1643 * Testing syscall events here is pretty useless, but
1644 * we still do it if configured. But this is time consuming.
1645 * What we really need is a user thread to perform the
1646 * syscalls as we test.
1648 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1649 if (call->class->system &&
1650 strcmp(call->class->system, "syscalls") == 0)
1654 pr_info("Testing event %s: ", call->name);
1657 * If an event is already enabled, someone is using
1658 * it and the self test should not be on.
1660 if (call->flags & TRACE_EVENT_FL_ENABLED) {
1661 pr_warning("Enabled event during self test!\n");
1666 ftrace_event_enable_disable(call, 1);
1668 ftrace_event_enable_disable(call, 0);
1673 /* Now test at the sub system level */
1675 pr_info("Running tests on trace event systems:\n");
1677 list_for_each_entry(system, &event_subsystems, list) {
1679 /* the ftrace system is special, skip it */
1680 if (strcmp(system->name, "ftrace") == 0)
1683 pr_info("Testing event system %s: ", system->name);
1685 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1686 if (WARN_ON_ONCE(ret)) {
1687 pr_warning("error enabling system %s\n",
1694 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1695 if (WARN_ON_ONCE(ret)) {
1696 pr_warning("error disabling system %s\n",
1704 /* Test with all events enabled */
1706 pr_info("Running tests on all trace events:\n");
1707 pr_info("Testing all events: ");
1709 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1710 if (WARN_ON_ONCE(ret)) {
1711 pr_warning("error enabling all events\n");
1718 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1719 if (WARN_ON_ONCE(ret)) {
1720 pr_warning("error disabling all events\n");
1727 #ifdef CONFIG_FUNCTION_TRACER
1729 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1732 function_test_events_call(unsigned long ip, unsigned long parent_ip,
1733 struct ftrace_ops *op, struct pt_regs *pt_regs)
1735 struct ring_buffer_event *event;
1736 struct ring_buffer *buffer;
1737 struct ftrace_entry *entry;
1738 unsigned long flags;
1743 pc = preempt_count();
1744 preempt_disable_notrace();
1745 cpu = raw_smp_processor_id();
1746 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1751 local_save_flags(flags);
1753 event = trace_current_buffer_lock_reserve(&buffer,
1754 TRACE_FN, sizeof(*entry),
1758 entry = ring_buffer_event_data(event);
1760 entry->parent_ip = parent_ip;
1762 trace_buffer_unlock_commit(buffer, event, flags, pc);
1765 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1766 preempt_enable_notrace();
1769 static struct ftrace_ops trace_ops __initdata =
1771 .func = function_test_events_call,
1772 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1775 static __init void event_trace_self_test_with_function(void)
1778 ret = register_ftrace_function(&trace_ops);
1779 if (WARN_ON(ret < 0)) {
1780 pr_info("Failed to enable function tracer for event tests\n");
1783 pr_info("Running tests again, along with the function tracer\n");
1784 event_trace_self_tests();
1785 unregister_ftrace_function(&trace_ops);
1788 static __init void event_trace_self_test_with_function(void)
1793 static __init int event_trace_self_tests_init(void)
1795 if (!tracing_selftest_disabled) {
1796 event_trace_self_tests();
1797 event_trace_self_test_with_function();
1803 late_initcall(event_trace_self_tests_init);