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);
119 __common_field(int, padding);
124 void trace_destroy_fields(struct ftrace_event_call *call)
126 struct ftrace_event_field *field, *next;
127 struct list_head *head;
129 head = trace_get_fields(call);
130 list_for_each_entry_safe(field, next, head, link) {
131 list_del(&field->link);
138 int trace_event_raw_init(struct ftrace_event_call *call)
142 id = register_ftrace_event(&call->event);
148 EXPORT_SYMBOL_GPL(trace_event_raw_init);
150 int ftrace_event_reg(struct ftrace_event_call *call, enum trace_reg type)
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,
177 EXPORT_SYMBOL_GPL(ftrace_event_reg);
179 void trace_event_enable_cmd_record(bool enable)
181 struct ftrace_event_call *call;
183 mutex_lock(&event_mutex);
184 list_for_each_entry(call, &ftrace_events, list) {
185 if (!(call->flags & TRACE_EVENT_FL_ENABLED))
189 tracing_start_cmdline_record();
190 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
192 tracing_stop_cmdline_record();
193 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
196 mutex_unlock(&event_mutex);
199 static int ftrace_event_enable_disable(struct ftrace_event_call *call,
206 if (call->flags & TRACE_EVENT_FL_ENABLED) {
207 call->flags &= ~TRACE_EVENT_FL_ENABLED;
208 if (call->flags & TRACE_EVENT_FL_RECORDED_CMD) {
209 tracing_stop_cmdline_record();
210 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
212 call->class->reg(call, TRACE_REG_UNREGISTER);
216 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
217 if (trace_flags & TRACE_ITER_RECORD_CMD) {
218 tracing_start_cmdline_record();
219 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
221 ret = call->class->reg(call, TRACE_REG_REGISTER);
223 tracing_stop_cmdline_record();
224 pr_info("event trace: Could not enable event "
228 call->flags |= TRACE_EVENT_FL_ENABLED;
236 static void ftrace_clear_events(void)
238 struct ftrace_event_call *call;
240 mutex_lock(&event_mutex);
241 list_for_each_entry(call, &ftrace_events, list) {
242 ftrace_event_enable_disable(call, 0);
244 mutex_unlock(&event_mutex);
248 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
250 static int __ftrace_set_clr_event(const char *match, const char *sub,
251 const char *event, int set)
253 struct ftrace_event_call *call;
256 mutex_lock(&event_mutex);
257 list_for_each_entry(call, &ftrace_events, list) {
259 if (!call->name || !call->class || !call->class->reg)
263 strcmp(match, call->name) != 0 &&
264 strcmp(match, call->class->system) != 0)
267 if (sub && strcmp(sub, call->class->system) != 0)
270 if (event && strcmp(event, call->name) != 0)
273 ftrace_event_enable_disable(call, set);
277 mutex_unlock(&event_mutex);
282 static int ftrace_set_clr_event(char *buf, int set)
284 char *event = NULL, *sub = NULL, *match;
287 * The buf format can be <subsystem>:<event-name>
288 * *:<event-name> means any event by that name.
289 * :<event-name> is the same.
291 * <subsystem>:* means all events in that subsystem
292 * <subsystem>: means the same.
294 * <name> (no ':') means all events in a subsystem with
295 * the name <name> or any event that matches <name>
298 match = strsep(&buf, ":");
304 if (!strlen(sub) || strcmp(sub, "*") == 0)
306 if (!strlen(event) || strcmp(event, "*") == 0)
310 return __ftrace_set_clr_event(match, sub, event, set);
314 * trace_set_clr_event - enable or disable an event
315 * @system: system name to match (NULL for any system)
316 * @event: event name to match (NULL for all events, within system)
317 * @set: 1 to enable, 0 to disable
319 * This is a way for other parts of the kernel to enable or disable
322 * Returns 0 on success, -EINVAL if the parameters do not match any
325 int trace_set_clr_event(const char *system, const char *event, int set)
327 return __ftrace_set_clr_event(NULL, system, event, set);
329 EXPORT_SYMBOL_GPL(trace_set_clr_event);
331 /* 128 should be much more than enough */
332 #define EVENT_BUF_SIZE 127
335 ftrace_event_write(struct file *file, const char __user *ubuf,
336 size_t cnt, loff_t *ppos)
338 struct trace_parser parser;
344 ret = tracing_update_buffers();
348 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
351 read = trace_get_user(&parser, ubuf, cnt, ppos);
353 if (read >= 0 && trace_parser_loaded((&parser))) {
356 if (*parser.buffer == '!')
359 parser.buffer[parser.idx] = 0;
361 ret = ftrace_set_clr_event(parser.buffer + !set, set);
369 trace_parser_put(&parser);
375 t_next(struct seq_file *m, void *v, loff_t *pos)
377 struct ftrace_event_call *call = v;
381 list_for_each_entry_continue(call, &ftrace_events, list) {
383 * The ftrace subsystem is for showing formats only.
384 * They can not be enabled or disabled via the event files.
386 if (call->class && call->class->reg)
393 static void *t_start(struct seq_file *m, loff_t *pos)
395 struct ftrace_event_call *call;
398 mutex_lock(&event_mutex);
400 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
401 for (l = 0; l <= *pos; ) {
402 call = t_next(m, call, &l);
410 s_next(struct seq_file *m, void *v, loff_t *pos)
412 struct ftrace_event_call *call = v;
416 list_for_each_entry_continue(call, &ftrace_events, list) {
417 if (call->flags & TRACE_EVENT_FL_ENABLED)
424 static void *s_start(struct seq_file *m, loff_t *pos)
426 struct ftrace_event_call *call;
429 mutex_lock(&event_mutex);
431 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
432 for (l = 0; l <= *pos; ) {
433 call = s_next(m, call, &l);
440 static int t_show(struct seq_file *m, void *v)
442 struct ftrace_event_call *call = v;
444 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
445 seq_printf(m, "%s:", call->class->system);
446 seq_printf(m, "%s\n", call->name);
451 static void t_stop(struct seq_file *m, void *p)
453 mutex_unlock(&event_mutex);
457 ftrace_event_seq_open(struct inode *inode, struct file *file)
459 const struct seq_operations *seq_ops;
461 if ((file->f_mode & FMODE_WRITE) &&
462 (file->f_flags & O_TRUNC))
463 ftrace_clear_events();
465 seq_ops = inode->i_private;
466 return seq_open(file, seq_ops);
470 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
473 struct ftrace_event_call *call = filp->private_data;
476 if (call->flags & TRACE_EVENT_FL_ENABLED)
481 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
485 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
488 struct ftrace_event_call *call = filp->private_data;
493 if (cnt >= sizeof(buf))
496 if (copy_from_user(&buf, ubuf, cnt))
501 ret = strict_strtoul(buf, 10, &val);
505 ret = tracing_update_buffers();
512 mutex_lock(&event_mutex);
513 ret = ftrace_event_enable_disable(call, val);
514 mutex_unlock(&event_mutex);
523 return ret ? ret : cnt;
527 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
530 const char set_to_char[4] = { '?', '0', '1', 'X' };
531 const char *system = filp->private_data;
532 struct ftrace_event_call *call;
537 mutex_lock(&event_mutex);
538 list_for_each_entry(call, &ftrace_events, list) {
539 if (!call->name || !call->class || !call->class->reg)
542 if (system && strcmp(call->class->system, system) != 0)
546 * We need to find out if all the events are set
547 * or if all events or cleared, or if we have
550 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
553 * If we have a mixture, no need to look further.
558 mutex_unlock(&event_mutex);
560 buf[0] = set_to_char[set];
563 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
569 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
572 const char *system = filp->private_data;
577 if (cnt >= sizeof(buf))
580 if (copy_from_user(&buf, ubuf, cnt))
585 ret = strict_strtoul(buf, 10, &val);
589 ret = tracing_update_buffers();
593 if (val != 0 && val != 1)
596 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
610 FORMAT_FIELD_SEPERATOR = 2,
614 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
616 struct ftrace_event_call *call = m->private;
617 struct ftrace_event_field *field;
618 struct list_head *common_head = &ftrace_common_fields;
619 struct list_head *head = trace_get_fields(call);
623 switch ((unsigned long)v) {
625 if (unlikely(list_empty(common_head)))
628 field = list_entry(common_head->prev,
629 struct ftrace_event_field, link);
632 case FORMAT_FIELD_SEPERATOR:
633 if (unlikely(list_empty(head)))
636 field = list_entry(head->prev, struct ftrace_event_field, link);
639 case FORMAT_PRINTFMT:
645 if (field->link.prev == common_head)
646 return (void *)FORMAT_FIELD_SEPERATOR;
647 else if (field->link.prev == head)
648 return (void *)FORMAT_PRINTFMT;
650 field = list_entry(field->link.prev, struct ftrace_event_field, link);
655 static void *f_start(struct seq_file *m, loff_t *pos)
660 /* Start by showing the header */
662 return (void *)FORMAT_HEADER;
664 p = (void *)FORMAT_HEADER;
666 p = f_next(m, p, &l);
667 } while (p && l < *pos);
672 static int f_show(struct seq_file *m, void *v)
674 struct ftrace_event_call *call = m->private;
675 struct ftrace_event_field *field;
676 const char *array_descriptor;
678 switch ((unsigned long)v) {
680 seq_printf(m, "name: %s\n", call->name);
681 seq_printf(m, "ID: %d\n", call->event.type);
682 seq_printf(m, "format:\n");
685 case FORMAT_FIELD_SEPERATOR:
689 case FORMAT_PRINTFMT:
690 seq_printf(m, "\nprint fmt: %s\n",
698 * Smartly shows the array type(except dynamic array).
701 * If TYPE := TYPE[LEN], it is shown:
702 * field:TYPE VAR[LEN]
704 array_descriptor = strchr(field->type, '[');
706 if (!strncmp(field->type, "__data_loc", 10))
707 array_descriptor = NULL;
709 if (!array_descriptor)
710 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
711 field->type, field->name, field->offset,
712 field->size, !!field->is_signed);
714 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
715 (int)(array_descriptor - field->type),
716 field->type, field->name,
717 array_descriptor, field->offset,
718 field->size, !!field->is_signed);
723 static void f_stop(struct seq_file *m, void *p)
727 static const struct seq_operations trace_format_seq_ops = {
734 static int trace_format_open(struct inode *inode, struct file *file)
736 struct ftrace_event_call *call = inode->i_private;
740 ret = seq_open(file, &trace_format_seq_ops);
744 m = file->private_data;
751 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
753 struct ftrace_event_call *call = filp->private_data;
760 s = kmalloc(sizeof(*s), GFP_KERNEL);
765 trace_seq_printf(s, "%d\n", call->event.type);
767 r = simple_read_from_buffer(ubuf, cnt, ppos,
774 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
777 struct ftrace_event_call *call = filp->private_data;
784 s = kmalloc(sizeof(*s), GFP_KERNEL);
790 print_event_filter(call, s);
791 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
799 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
802 struct ftrace_event_call *call = filp->private_data;
806 if (cnt >= PAGE_SIZE)
809 buf = (char *)__get_free_page(GFP_TEMPORARY);
813 if (copy_from_user(buf, ubuf, cnt)) {
814 free_page((unsigned long) buf);
819 err = apply_event_filter(call, buf);
820 free_page((unsigned long) buf);
830 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
833 struct event_subsystem *system = filp->private_data;
840 s = kmalloc(sizeof(*s), GFP_KERNEL);
846 print_subsystem_event_filter(system, s);
847 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
855 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
858 struct event_subsystem *system = filp->private_data;
862 if (cnt >= PAGE_SIZE)
865 buf = (char *)__get_free_page(GFP_TEMPORARY);
869 if (copy_from_user(buf, ubuf, cnt)) {
870 free_page((unsigned long) buf);
875 err = apply_subsystem_event_filter(system, buf);
876 free_page((unsigned long) buf);
886 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
888 int (*func)(struct trace_seq *s) = filp->private_data;
895 s = kmalloc(sizeof(*s), GFP_KERNEL);
902 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
909 static const struct seq_operations show_event_seq_ops = {
916 static const struct seq_operations show_set_event_seq_ops = {
923 static const struct file_operations ftrace_avail_fops = {
924 .open = ftrace_event_seq_open,
927 .release = seq_release,
930 static const struct file_operations ftrace_set_event_fops = {
931 .open = ftrace_event_seq_open,
933 .write = ftrace_event_write,
935 .release = seq_release,
938 static const struct file_operations ftrace_enable_fops = {
939 .open = tracing_open_generic,
940 .read = event_enable_read,
941 .write = event_enable_write,
942 .llseek = default_llseek,
945 static const struct file_operations ftrace_event_format_fops = {
946 .open = trace_format_open,
949 .release = seq_release,
952 static const struct file_operations ftrace_event_id_fops = {
953 .open = tracing_open_generic,
954 .read = event_id_read,
955 .llseek = default_llseek,
958 static const struct file_operations ftrace_event_filter_fops = {
959 .open = tracing_open_generic,
960 .read = event_filter_read,
961 .write = event_filter_write,
962 .llseek = default_llseek,
965 static const struct file_operations ftrace_subsystem_filter_fops = {
966 .open = tracing_open_generic,
967 .read = subsystem_filter_read,
968 .write = subsystem_filter_write,
969 .llseek = default_llseek,
972 static const struct file_operations ftrace_system_enable_fops = {
973 .open = tracing_open_generic,
974 .read = system_enable_read,
975 .write = system_enable_write,
976 .llseek = default_llseek,
979 static const struct file_operations ftrace_show_header_fops = {
980 .open = tracing_open_generic,
982 .llseek = default_llseek,
985 static struct dentry *event_trace_events_dir(void)
987 static struct dentry *d_tracer;
988 static struct dentry *d_events;
993 d_tracer = tracing_init_dentry();
997 d_events = debugfs_create_dir("events", d_tracer);
999 pr_warning("Could not create debugfs "
1000 "'events' directory\n");
1005 static LIST_HEAD(event_subsystems);
1007 static struct dentry *
1008 event_subsystem_dir(const char *name, struct dentry *d_events)
1010 struct event_subsystem *system;
1011 struct dentry *entry;
1013 /* First see if we did not already create this dir */
1014 list_for_each_entry(system, &event_subsystems, list) {
1015 if (strcmp(system->name, name) == 0) {
1016 system->nr_events++;
1017 return system->entry;
1021 /* need to create new entry */
1022 system = kmalloc(sizeof(*system), GFP_KERNEL);
1024 pr_warning("No memory to create event subsystem %s\n",
1029 system->entry = debugfs_create_dir(name, d_events);
1030 if (!system->entry) {
1031 pr_warning("Could not create event subsystem %s\n",
1037 system->nr_events = 1;
1038 system->name = kstrdup(name, GFP_KERNEL);
1039 if (!system->name) {
1040 debugfs_remove(system->entry);
1045 list_add(&system->list, &event_subsystems);
1047 system->filter = NULL;
1049 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1050 if (!system->filter) {
1051 pr_warning("Could not allocate filter for subsystem "
1053 return system->entry;
1056 entry = debugfs_create_file("filter", 0644, system->entry, system,
1057 &ftrace_subsystem_filter_fops);
1059 kfree(system->filter);
1060 system->filter = NULL;
1061 pr_warning("Could not create debugfs "
1062 "'%s/filter' entry\n", name);
1065 trace_create_file("enable", 0644, system->entry,
1066 (void *)system->name,
1067 &ftrace_system_enable_fops);
1069 return system->entry;
1073 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
1074 const struct file_operations *id,
1075 const struct file_operations *enable,
1076 const struct file_operations *filter,
1077 const struct file_operations *format)
1079 struct list_head *head;
1083 * If the trace point header did not define TRACE_SYSTEM
1084 * then the system would be called "TRACE_SYSTEM".
1086 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1087 d_events = event_subsystem_dir(call->class->system, d_events);
1089 call->dir = debugfs_create_dir(call->name, d_events);
1091 pr_warning("Could not create debugfs "
1092 "'%s' directory\n", call->name);
1096 if (call->class->reg)
1097 trace_create_file("enable", 0644, call->dir, call,
1100 #ifdef CONFIG_PERF_EVENTS
1101 if (call->event.type && call->class->reg)
1102 trace_create_file("id", 0444, call->dir, call,
1107 * Other events may have the same class. Only update
1108 * the fields if they are not already defined.
1110 head = trace_get_fields(call);
1111 if (list_empty(head)) {
1112 ret = call->class->define_fields(call);
1114 pr_warning("Could not initialize trace point"
1115 " events/%s\n", call->name);
1119 trace_create_file("filter", 0644, call->dir, call,
1122 trace_create_file("format", 0444, call->dir, call,
1129 __trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
1130 const struct file_operations *id,
1131 const struct file_operations *enable,
1132 const struct file_operations *filter,
1133 const struct file_operations *format)
1135 struct dentry *d_events;
1138 /* The linker may leave blanks */
1142 if (call->class->raw_init) {
1143 ret = call->class->raw_init(call);
1146 pr_warning("Could not initialize trace events/%s\n",
1152 d_events = event_trace_events_dir();
1156 ret = event_create_dir(call, d_events, id, enable, filter, format);
1158 list_add(&call->list, &ftrace_events);
1164 /* Add an additional event_call dynamically */
1165 int trace_add_event_call(struct ftrace_event_call *call)
1168 mutex_lock(&event_mutex);
1169 ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1170 &ftrace_enable_fops,
1171 &ftrace_event_filter_fops,
1172 &ftrace_event_format_fops);
1173 mutex_unlock(&event_mutex);
1177 static void remove_subsystem_dir(const char *name)
1179 struct event_subsystem *system;
1181 if (strcmp(name, TRACE_SYSTEM) == 0)
1184 list_for_each_entry(system, &event_subsystems, list) {
1185 if (strcmp(system->name, name) == 0) {
1186 if (!--system->nr_events) {
1187 struct event_filter *filter = system->filter;
1189 debugfs_remove_recursive(system->entry);
1190 list_del(&system->list);
1192 kfree(filter->filter_string);
1195 kfree(system->name);
1204 * Must be called under locking both of event_mutex and trace_event_mutex.
1206 static void __trace_remove_event_call(struct ftrace_event_call *call)
1208 ftrace_event_enable_disable(call, 0);
1209 if (call->event.funcs)
1210 __unregister_ftrace_event(&call->event);
1211 debugfs_remove_recursive(call->dir);
1212 list_del(&call->list);
1213 trace_destroy_fields(call);
1214 destroy_preds(call);
1215 remove_subsystem_dir(call->class->system);
1218 /* Remove an event_call */
1219 void trace_remove_event_call(struct ftrace_event_call *call)
1221 mutex_lock(&event_mutex);
1222 down_write(&trace_event_mutex);
1223 __trace_remove_event_call(call);
1224 up_write(&trace_event_mutex);
1225 mutex_unlock(&event_mutex);
1228 #define for_each_event(event, start, end) \
1229 for (event = start; \
1230 (unsigned long)event < (unsigned long)end; \
1233 #ifdef CONFIG_MODULES
1235 static LIST_HEAD(ftrace_module_file_list);
1238 * Modules must own their file_operations to keep up with
1239 * reference counting.
1241 struct ftrace_module_file_ops {
1242 struct list_head list;
1244 struct file_operations id;
1245 struct file_operations enable;
1246 struct file_operations format;
1247 struct file_operations filter;
1250 static struct ftrace_module_file_ops *
1251 trace_create_file_ops(struct module *mod)
1253 struct ftrace_module_file_ops *file_ops;
1256 * This is a bit of a PITA. To allow for correct reference
1257 * counting, modules must "own" their file_operations.
1258 * To do this, we allocate the file operations that will be
1259 * used in the event directory.
1262 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1266 file_ops->mod = mod;
1268 file_ops->id = ftrace_event_id_fops;
1269 file_ops->id.owner = mod;
1271 file_ops->enable = ftrace_enable_fops;
1272 file_ops->enable.owner = mod;
1274 file_ops->filter = ftrace_event_filter_fops;
1275 file_ops->filter.owner = mod;
1277 file_ops->format = ftrace_event_format_fops;
1278 file_ops->format.owner = mod;
1280 list_add(&file_ops->list, &ftrace_module_file_list);
1285 static void trace_module_add_events(struct module *mod)
1287 struct ftrace_module_file_ops *file_ops = NULL;
1288 struct ftrace_event_call **call, **start, **end;
1290 start = mod->trace_events;
1291 end = mod->trace_events + mod->num_trace_events;
1296 file_ops = trace_create_file_ops(mod);
1300 for_each_event(call, start, end) {
1301 __trace_add_event_call(*call, mod,
1302 &file_ops->id, &file_ops->enable,
1303 &file_ops->filter, &file_ops->format);
1307 static void trace_module_remove_events(struct module *mod)
1309 struct ftrace_module_file_ops *file_ops;
1310 struct ftrace_event_call *call, *p;
1313 down_write(&trace_event_mutex);
1314 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1315 if (call->mod == mod) {
1317 __trace_remove_event_call(call);
1321 /* Now free the file_operations */
1322 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1323 if (file_ops->mod == mod)
1326 if (&file_ops->list != &ftrace_module_file_list) {
1327 list_del(&file_ops->list);
1332 * It is safest to reset the ring buffer if the module being unloaded
1333 * registered any events.
1336 tracing_reset_current_online_cpus();
1337 up_write(&trace_event_mutex);
1340 static int trace_module_notify(struct notifier_block *self,
1341 unsigned long val, void *data)
1343 struct module *mod = data;
1345 mutex_lock(&event_mutex);
1347 case MODULE_STATE_COMING:
1348 trace_module_add_events(mod);
1350 case MODULE_STATE_GOING:
1351 trace_module_remove_events(mod);
1354 mutex_unlock(&event_mutex);
1359 static int trace_module_notify(struct notifier_block *self,
1360 unsigned long val, void *data)
1364 #endif /* CONFIG_MODULES */
1366 static struct notifier_block trace_module_nb = {
1367 .notifier_call = trace_module_notify,
1371 extern struct ftrace_event_call *__start_ftrace_events[];
1372 extern struct ftrace_event_call *__stop_ftrace_events[];
1374 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1376 static __init int setup_trace_event(char *str)
1378 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1379 ring_buffer_expanded = 1;
1380 tracing_selftest_disabled = 1;
1384 __setup("trace_event=", setup_trace_event);
1386 static __init int event_trace_init(void)
1388 struct ftrace_event_call **call;
1389 struct dentry *d_tracer;
1390 struct dentry *entry;
1391 struct dentry *d_events;
1393 char *buf = bootup_event_buf;
1396 d_tracer = tracing_init_dentry();
1400 entry = debugfs_create_file("available_events", 0444, d_tracer,
1401 (void *)&show_event_seq_ops,
1402 &ftrace_avail_fops);
1404 pr_warning("Could not create debugfs "
1405 "'available_events' entry\n");
1407 entry = debugfs_create_file("set_event", 0644, d_tracer,
1408 (void *)&show_set_event_seq_ops,
1409 &ftrace_set_event_fops);
1411 pr_warning("Could not create debugfs "
1412 "'set_event' entry\n");
1414 d_events = event_trace_events_dir();
1418 /* ring buffer internal formats */
1419 trace_create_file("header_page", 0444, d_events,
1420 ring_buffer_print_page_header,
1421 &ftrace_show_header_fops);
1423 trace_create_file("header_event", 0444, d_events,
1424 ring_buffer_print_entry_header,
1425 &ftrace_show_header_fops);
1427 trace_create_file("enable", 0644, d_events,
1428 NULL, &ftrace_system_enable_fops);
1430 if (trace_define_common_fields())
1431 pr_warning("tracing: Failed to allocate common fields");
1433 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1434 __trace_add_event_call(*call, NULL, &ftrace_event_id_fops,
1435 &ftrace_enable_fops,
1436 &ftrace_event_filter_fops,
1437 &ftrace_event_format_fops);
1441 token = strsep(&buf, ",");
1448 ret = ftrace_set_clr_event(token, 1);
1450 pr_warning("Failed to enable trace event: %s\n", token);
1453 ret = register_module_notifier(&trace_module_nb);
1455 pr_warning("Failed to register trace events module notifier\n");
1459 fs_initcall(event_trace_init);
1461 #ifdef CONFIG_FTRACE_STARTUP_TEST
1463 static DEFINE_SPINLOCK(test_spinlock);
1464 static DEFINE_SPINLOCK(test_spinlock_irq);
1465 static DEFINE_MUTEX(test_mutex);
1467 static __init void test_work(struct work_struct *dummy)
1469 spin_lock(&test_spinlock);
1470 spin_lock_irq(&test_spinlock_irq);
1472 spin_unlock_irq(&test_spinlock_irq);
1473 spin_unlock(&test_spinlock);
1475 mutex_lock(&test_mutex);
1477 mutex_unlock(&test_mutex);
1480 static __init int event_test_thread(void *unused)
1484 test_malloc = kmalloc(1234, GFP_KERNEL);
1486 pr_info("failed to kmalloc\n");
1488 schedule_on_each_cpu(test_work);
1492 set_current_state(TASK_INTERRUPTIBLE);
1493 while (!kthread_should_stop())
1500 * Do various things that may trigger events.
1502 static __init void event_test_stuff(void)
1504 struct task_struct *test_thread;
1506 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1508 kthread_stop(test_thread);
1512 * For every trace event defined, we will test each trace point separately,
1513 * and then by groups, and finally all trace points.
1515 static __init void event_trace_self_tests(void)
1517 struct ftrace_event_call *call;
1518 struct event_subsystem *system;
1521 pr_info("Running tests on trace events:\n");
1523 list_for_each_entry(call, &ftrace_events, list) {
1525 /* Only test those that have a probe */
1526 if (!call->class || !call->class->probe)
1530 * Testing syscall events here is pretty useless, but
1531 * we still do it if configured. But this is time consuming.
1532 * What we really need is a user thread to perform the
1533 * syscalls as we test.
1535 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1536 if (call->class->system &&
1537 strcmp(call->class->system, "syscalls") == 0)
1541 pr_info("Testing event %s: ", call->name);
1544 * If an event is already enabled, someone is using
1545 * it and the self test should not be on.
1547 if (call->flags & TRACE_EVENT_FL_ENABLED) {
1548 pr_warning("Enabled event during self test!\n");
1553 ftrace_event_enable_disable(call, 1);
1555 ftrace_event_enable_disable(call, 0);
1560 /* Now test at the sub system level */
1562 pr_info("Running tests on trace event systems:\n");
1564 list_for_each_entry(system, &event_subsystems, list) {
1566 /* the ftrace system is special, skip it */
1567 if (strcmp(system->name, "ftrace") == 0)
1570 pr_info("Testing event system %s: ", system->name);
1572 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1573 if (WARN_ON_ONCE(ret)) {
1574 pr_warning("error enabling system %s\n",
1581 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1582 if (WARN_ON_ONCE(ret))
1583 pr_warning("error disabling system %s\n",
1589 /* Test with all events enabled */
1591 pr_info("Running tests on all trace events:\n");
1592 pr_info("Testing all events: ");
1594 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1595 if (WARN_ON_ONCE(ret)) {
1596 pr_warning("error enabling all events\n");
1603 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1604 if (WARN_ON_ONCE(ret)) {
1605 pr_warning("error disabling all events\n");
1612 #ifdef CONFIG_FUNCTION_TRACER
1614 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1617 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1619 struct ring_buffer_event *event;
1620 struct ring_buffer *buffer;
1621 struct ftrace_entry *entry;
1622 unsigned long flags;
1627 pc = preempt_count();
1628 preempt_disable_notrace();
1629 cpu = raw_smp_processor_id();
1630 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1635 local_save_flags(flags);
1637 event = trace_current_buffer_lock_reserve(&buffer,
1638 TRACE_FN, sizeof(*entry),
1642 entry = ring_buffer_event_data(event);
1644 entry->parent_ip = parent_ip;
1646 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1649 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1650 preempt_enable_notrace();
1653 static struct ftrace_ops trace_ops __initdata =
1655 .func = function_test_events_call,
1658 static __init void event_trace_self_test_with_function(void)
1660 register_ftrace_function(&trace_ops);
1661 pr_info("Running tests again, along with the function tracer\n");
1662 event_trace_self_tests();
1663 unregister_ftrace_function(&trace_ops);
1666 static __init void event_trace_self_test_with_function(void)
1671 static __init int event_trace_self_tests_init(void)
1673 if (!tracing_selftest_disabled) {
1674 event_trace_self_tests();
1675 event_trace_self_test_with_function();
1681 late_initcall(event_trace_self_tests_init);