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/delay.h>
20 #include "trace_output.h"
22 #define TRACE_SYSTEM "TRACE_SYSTEM"
24 DEFINE_MUTEX(event_mutex);
26 LIST_HEAD(ftrace_events);
28 int trace_define_field(struct ftrace_event_call *call, char *type,
29 char *name, int offset, int size, int is_signed)
31 struct ftrace_event_field *field;
33 field = kzalloc(sizeof(*field), GFP_KERNEL);
37 field->name = kstrdup(name, GFP_KERNEL);
41 field->type = kstrdup(type, GFP_KERNEL);
45 field->offset = offset;
47 field->is_signed = is_signed;
48 list_add(&field->link, &call->fields);
61 EXPORT_SYMBOL_GPL(trace_define_field);
65 static void trace_destroy_fields(struct ftrace_event_call *call)
67 struct ftrace_event_field *field, *next;
69 list_for_each_entry_safe(field, next, &call->fields, link) {
70 list_del(&field->link);
77 #endif /* CONFIG_MODULES */
79 static void ftrace_clear_events(void)
81 struct ftrace_event_call *call;
83 mutex_lock(&event_mutex);
84 list_for_each_entry(call, &ftrace_events, list) {
91 mutex_unlock(&event_mutex);
94 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
106 if (!call->enabled) {
114 static int ftrace_set_clr_event(char *buf, int set)
116 struct ftrace_event_call *call;
117 char *event = NULL, *sub = NULL, *match;
121 * The buf format can be <subsystem>:<event-name>
122 * *:<event-name> means any event by that name.
123 * :<event-name> is the same.
125 * <subsystem>:* means all events in that subsystem
126 * <subsystem>: means the same.
128 * <name> (no ':') means all events in a subsystem with
129 * the name <name> or any event that matches <name>
132 match = strsep(&buf, ":");
138 if (!strlen(sub) || strcmp(sub, "*") == 0)
140 if (!strlen(event) || strcmp(event, "*") == 0)
144 mutex_lock(&event_mutex);
145 list_for_each_entry(call, &ftrace_events, list) {
147 if (!call->name || !call->regfunc)
151 strcmp(match, call->name) != 0 &&
152 strcmp(match, call->system) != 0)
155 if (sub && strcmp(sub, call->system) != 0)
158 if (event && strcmp(event, call->name) != 0)
161 ftrace_event_enable_disable(call, set);
165 mutex_unlock(&event_mutex);
170 /* 128 should be much more than enough */
171 #define EVENT_BUF_SIZE 127
174 ftrace_event_write(struct file *file, const char __user *ubuf,
175 size_t cnt, loff_t *ppos)
186 ret = tracing_update_buffers();
190 ret = get_user(ch, ubuf++);
196 /* skip white space */
197 while (cnt && isspace(ch)) {
198 ret = get_user(ch, ubuf++);
205 /* Only white space found? */
212 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
216 if (cnt > EVENT_BUF_SIZE)
217 cnt = EVENT_BUF_SIZE;
220 while (cnt && !isspace(ch)) {
226 ret = get_user(ch, ubuf++);
236 ret = ftrace_set_clr_event(buf, set);
249 t_next(struct seq_file *m, void *v, loff_t *pos)
251 struct list_head *list = m->private;
252 struct ftrace_event_call *call;
257 if (list == &ftrace_events)
260 call = list_entry(list, struct ftrace_event_call, list);
263 * The ftrace subsystem is for showing formats only.
264 * They can not be enabled or disabled via the event files.
272 m->private = list->next;
277 static void *t_start(struct seq_file *m, loff_t *pos)
279 mutex_lock(&event_mutex);
281 m->private = ftrace_events.next;
282 return t_next(m, NULL, pos);
286 s_next(struct seq_file *m, void *v, loff_t *pos)
288 struct list_head *list = m->private;
289 struct ftrace_event_call *call;
294 if (list == &ftrace_events)
297 call = list_entry(list, struct ftrace_event_call, list);
299 if (!call->enabled) {
304 m->private = list->next;
309 static void *s_start(struct seq_file *m, loff_t *pos)
311 mutex_lock(&event_mutex);
313 m->private = ftrace_events.next;
314 return s_next(m, NULL, pos);
317 static int t_show(struct seq_file *m, void *v)
319 struct ftrace_event_call *call = v;
321 if (strcmp(call->system, TRACE_SYSTEM) != 0)
322 seq_printf(m, "%s:", call->system);
323 seq_printf(m, "%s\n", call->name);
328 static void t_stop(struct seq_file *m, void *p)
330 mutex_unlock(&event_mutex);
334 ftrace_event_seq_open(struct inode *inode, struct file *file)
336 const struct seq_operations *seq_ops;
338 if ((file->f_mode & FMODE_WRITE) &&
339 !(file->f_flags & O_APPEND))
340 ftrace_clear_events();
342 seq_ops = inode->i_private;
343 return seq_open(file, seq_ops);
347 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
350 struct ftrace_event_call *call = filp->private_data;
358 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
362 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
365 struct ftrace_event_call *call = filp->private_data;
370 if (cnt >= sizeof(buf))
373 if (copy_from_user(&buf, ubuf, cnt))
378 ret = strict_strtoul(buf, 10, &val);
382 ret = tracing_update_buffers();
389 mutex_lock(&event_mutex);
390 ftrace_event_enable_disable(call, val);
391 mutex_unlock(&event_mutex);
403 extern char *__bad_type_size(void);
406 #define FIELD(type, name) \
407 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
408 #type, "common_" #name, offsetof(typeof(field), name), \
411 static int trace_write_header(struct trace_seq *s)
413 struct trace_entry field;
415 /* struct trace_entry */
416 return trace_seq_printf(s,
417 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
418 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
419 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
420 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
421 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
423 FIELD(unsigned short, type),
424 FIELD(unsigned char, flags),
425 FIELD(unsigned char, preempt_count),
431 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
434 struct ftrace_event_call *call = filp->private_data;
442 s = kmalloc(sizeof(*s), GFP_KERNEL);
448 /* If any of the first writes fail, so will the show_format. */
450 trace_seq_printf(s, "name: %s\n", call->name);
451 trace_seq_printf(s, "ID: %d\n", call->id);
452 trace_seq_printf(s, "format:\n");
453 trace_write_header(s);
455 r = call->show_format(s);
458 * ug! The format output is bigger than a PAGE!!
460 buf = "FORMAT TOO BIG\n";
461 r = simple_read_from_buffer(ubuf, cnt, ppos,
466 r = simple_read_from_buffer(ubuf, cnt, ppos,
474 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
476 struct ftrace_event_call *call = filp->private_data;
483 s = kmalloc(sizeof(*s), GFP_KERNEL);
488 trace_seq_printf(s, "%d\n", call->id);
490 r = simple_read_from_buffer(ubuf, cnt, ppos,
497 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
500 struct ftrace_event_call *call = filp->private_data;
507 s = kmalloc(sizeof(*s), GFP_KERNEL);
513 print_event_filter(call, s);
514 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
522 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
525 struct ftrace_event_call *call = filp->private_data;
529 if (cnt >= PAGE_SIZE)
532 buf = (char *)__get_free_page(GFP_TEMPORARY);
536 if (copy_from_user(buf, ubuf, cnt)) {
537 free_page((unsigned long) buf);
542 err = apply_event_filter(call, buf);
543 free_page((unsigned long) buf);
553 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
556 struct event_subsystem *system = filp->private_data;
563 s = kmalloc(sizeof(*s), GFP_KERNEL);
569 print_subsystem_event_filter(system, s);
570 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
578 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
581 struct event_subsystem *system = filp->private_data;
585 if (cnt >= PAGE_SIZE)
588 buf = (char *)__get_free_page(GFP_TEMPORARY);
592 if (copy_from_user(buf, ubuf, cnt)) {
593 free_page((unsigned long) buf);
598 err = apply_subsystem_event_filter(system, buf);
599 free_page((unsigned long) buf);
609 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
611 int (*func)(struct trace_seq *s) = filp->private_data;
618 s = kmalloc(sizeof(*s), GFP_KERNEL);
625 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
632 static const struct seq_operations show_event_seq_ops = {
639 static const struct seq_operations show_set_event_seq_ops = {
646 static const struct file_operations ftrace_avail_fops = {
647 .open = ftrace_event_seq_open,
650 .release = seq_release,
653 static const struct file_operations ftrace_set_event_fops = {
654 .open = ftrace_event_seq_open,
656 .write = ftrace_event_write,
658 .release = seq_release,
661 static const struct file_operations ftrace_enable_fops = {
662 .open = tracing_open_generic,
663 .read = event_enable_read,
664 .write = event_enable_write,
667 static const struct file_operations ftrace_event_format_fops = {
668 .open = tracing_open_generic,
669 .read = event_format_read,
672 static const struct file_operations ftrace_event_id_fops = {
673 .open = tracing_open_generic,
674 .read = event_id_read,
677 static const struct file_operations ftrace_event_filter_fops = {
678 .open = tracing_open_generic,
679 .read = event_filter_read,
680 .write = event_filter_write,
683 static const struct file_operations ftrace_subsystem_filter_fops = {
684 .open = tracing_open_generic,
685 .read = subsystem_filter_read,
686 .write = subsystem_filter_write,
689 static const struct file_operations ftrace_show_header_fops = {
690 .open = tracing_open_generic,
694 static struct dentry *event_trace_events_dir(void)
696 static struct dentry *d_tracer;
697 static struct dentry *d_events;
702 d_tracer = tracing_init_dentry();
706 d_events = debugfs_create_dir("events", d_tracer);
708 pr_warning("Could not create debugfs "
709 "'events' directory\n");
714 static LIST_HEAD(event_subsystems);
716 static struct dentry *
717 event_subsystem_dir(const char *name, struct dentry *d_events)
719 struct event_subsystem *system;
720 struct dentry *entry;
722 /* First see if we did not already create this dir */
723 list_for_each_entry(system, &event_subsystems, list) {
724 if (strcmp(system->name, name) == 0)
725 return system->entry;
728 /* need to create new entry */
729 system = kmalloc(sizeof(*system), GFP_KERNEL);
731 pr_warning("No memory to create event subsystem %s\n",
736 system->entry = debugfs_create_dir(name, d_events);
737 if (!system->entry) {
738 pr_warning("Could not create event subsystem %s\n",
744 system->name = kstrdup(name, GFP_KERNEL);
746 debugfs_remove(system->entry);
751 list_add(&system->list, &event_subsystems);
753 system->filter = NULL;
755 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
756 if (!system->filter) {
757 pr_warning("Could not allocate filter for subsystem "
759 return system->entry;
762 entry = debugfs_create_file("filter", 0644, system->entry, system,
763 &ftrace_subsystem_filter_fops);
765 kfree(system->filter);
766 system->filter = NULL;
767 pr_warning("Could not create debugfs "
768 "'%s/filter' entry\n", name);
771 return system->entry;
775 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
776 const struct file_operations *id,
777 const struct file_operations *enable,
778 const struct file_operations *filter,
779 const struct file_operations *format)
781 struct dentry *entry;
785 * If the trace point header did not define TRACE_SYSTEM
786 * then the system would be called "TRACE_SYSTEM".
788 if (strcmp(call->system, TRACE_SYSTEM) != 0)
789 d_events = event_subsystem_dir(call->system, d_events);
791 if (call->raw_init) {
792 ret = call->raw_init();
794 pr_warning("Could not initialize trace point"
795 " events/%s\n", call->name);
800 call->dir = debugfs_create_dir(call->name, d_events);
802 pr_warning("Could not create debugfs "
803 "'%s' directory\n", call->name);
808 entry = trace_create_file("enable", 0644, call->dir, call,
812 entry = trace_create_file("id", 0444, call->dir, call,
815 if (call->define_fields) {
816 ret = call->define_fields();
818 pr_warning("Could not initialize trace point"
819 " events/%s\n", call->name);
822 entry = trace_create_file("filter", 0644, call->dir, call,
826 /* A trace may not want to export its format */
827 if (!call->show_format)
830 entry = trace_create_file("format", 0444, call->dir, call,
836 #define for_each_event(event, start, end) \
837 for (event = start; \
838 (unsigned long)event < (unsigned long)end; \
841 #ifdef CONFIG_MODULES
843 static LIST_HEAD(ftrace_module_file_list);
846 * Modules must own their file_operations to keep up with
847 * reference counting.
849 struct ftrace_module_file_ops {
850 struct list_head list;
852 struct file_operations id;
853 struct file_operations enable;
854 struct file_operations format;
855 struct file_operations filter;
858 static struct ftrace_module_file_ops *
859 trace_create_file_ops(struct module *mod)
861 struct ftrace_module_file_ops *file_ops;
864 * This is a bit of a PITA. To allow for correct reference
865 * counting, modules must "own" their file_operations.
866 * To do this, we allocate the file operations that will be
867 * used in the event directory.
870 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
876 file_ops->id = ftrace_event_id_fops;
877 file_ops->id.owner = mod;
879 file_ops->enable = ftrace_enable_fops;
880 file_ops->enable.owner = mod;
882 file_ops->filter = ftrace_event_filter_fops;
883 file_ops->filter.owner = mod;
885 file_ops->format = ftrace_event_format_fops;
886 file_ops->format.owner = mod;
888 list_add(&file_ops->list, &ftrace_module_file_list);
893 static void trace_module_add_events(struct module *mod)
895 struct ftrace_module_file_ops *file_ops = NULL;
896 struct ftrace_event_call *call, *start, *end;
897 struct dentry *d_events;
899 start = mod->trace_events;
900 end = mod->trace_events + mod->num_trace_events;
905 d_events = event_trace_events_dir();
909 for_each_event(call, start, end) {
910 /* The linker may leave blanks */
915 * This module has events, create file ops for this module
916 * if not already done.
919 file_ops = trace_create_file_ops(mod);
924 list_add(&call->list, &ftrace_events);
925 event_create_dir(call, d_events,
926 &file_ops->id, &file_ops->enable,
927 &file_ops->filter, &file_ops->format);
931 static void trace_module_remove_events(struct module *mod)
933 struct ftrace_module_file_ops *file_ops;
934 struct ftrace_event_call *call, *p;
936 list_for_each_entry_safe(call, p, &ftrace_events, list) {
937 if (call->mod == mod) {
943 unregister_ftrace_event(call->event);
944 debugfs_remove_recursive(call->dir);
945 list_del(&call->list);
946 trace_destroy_fields(call);
951 /* Now free the file_operations */
952 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
953 if (file_ops->mod == mod)
956 if (&file_ops->list != &ftrace_module_file_list) {
957 list_del(&file_ops->list);
962 static int trace_module_notify(struct notifier_block *self,
963 unsigned long val, void *data)
965 struct module *mod = data;
967 mutex_lock(&event_mutex);
969 case MODULE_STATE_COMING:
970 trace_module_add_events(mod);
972 case MODULE_STATE_GOING:
973 trace_module_remove_events(mod);
976 mutex_unlock(&event_mutex);
981 static int trace_module_notify(struct notifier_block *self,
982 unsigned long val, void *data)
986 #endif /* CONFIG_MODULES */
988 struct notifier_block trace_module_nb = {
989 .notifier_call = trace_module_notify,
993 extern struct ftrace_event_call __start_ftrace_events[];
994 extern struct ftrace_event_call __stop_ftrace_events[];
996 static __init int event_trace_init(void)
998 struct ftrace_event_call *call;
999 struct dentry *d_tracer;
1000 struct dentry *entry;
1001 struct dentry *d_events;
1004 d_tracer = tracing_init_dentry();
1008 entry = debugfs_create_file("available_events", 0444, d_tracer,
1009 (void *)&show_event_seq_ops,
1010 &ftrace_avail_fops);
1012 pr_warning("Could not create debugfs "
1013 "'available_events' entry\n");
1015 entry = debugfs_create_file("set_event", 0644, d_tracer,
1016 (void *)&show_set_event_seq_ops,
1017 &ftrace_set_event_fops);
1019 pr_warning("Could not create debugfs "
1020 "'set_event' entry\n");
1022 d_events = event_trace_events_dir();
1026 /* ring buffer internal formats */
1027 trace_create_file("header_page", 0444, d_events,
1028 ring_buffer_print_page_header,
1029 &ftrace_show_header_fops);
1031 trace_create_file("header_event", 0444, d_events,
1032 ring_buffer_print_entry_header,
1033 &ftrace_show_header_fops);
1035 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1036 /* The linker may leave blanks */
1039 list_add(&call->list, &ftrace_events);
1040 event_create_dir(call, d_events, &ftrace_event_id_fops,
1041 &ftrace_enable_fops, &ftrace_event_filter_fops,
1042 &ftrace_event_format_fops);
1045 ret = register_module_notifier(&trace_module_nb);
1047 pr_warning("Failed to register trace events module notifier\n");
1051 fs_initcall(event_trace_init);
1053 #ifdef CONFIG_FTRACE_STARTUP_TEST
1055 static DEFINE_SPINLOCK(test_spinlock);
1056 static DEFINE_SPINLOCK(test_spinlock_irq);
1057 static DEFINE_MUTEX(test_mutex);
1059 static __init void test_work(struct work_struct *dummy)
1061 spin_lock(&test_spinlock);
1062 spin_lock_irq(&test_spinlock_irq);
1064 spin_unlock_irq(&test_spinlock_irq);
1065 spin_unlock(&test_spinlock);
1067 mutex_lock(&test_mutex);
1069 mutex_unlock(&test_mutex);
1072 static __init int event_test_thread(void *unused)
1076 test_malloc = kmalloc(1234, GFP_KERNEL);
1078 pr_info("failed to kmalloc\n");
1080 schedule_on_each_cpu(test_work);
1084 set_current_state(TASK_INTERRUPTIBLE);
1085 while (!kthread_should_stop())
1092 * Do various things that may trigger events.
1094 static __init void event_test_stuff(void)
1096 struct task_struct *test_thread;
1098 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1100 kthread_stop(test_thread);
1104 * For every trace event defined, we will test each trace point separately,
1105 * and then by groups, and finally all trace points.
1107 static __init void event_trace_self_tests(void)
1109 struct ftrace_event_call *call;
1110 struct event_subsystem *system;
1114 pr_info("Running tests on trace events:\n");
1116 list_for_each_entry(call, &ftrace_events, list) {
1118 /* Only test those that have a regfunc */
1122 pr_info("Testing event %s: ", call->name);
1125 * If an event is already enabled, someone is using
1126 * it and the self test should not be on.
1128 if (call->enabled) {
1129 pr_warning("Enabled event during self test!\n");
1145 /* Now test at the sub system level */
1147 pr_info("Running tests on trace event systems:\n");
1149 list_for_each_entry(system, &event_subsystems, list) {
1151 /* the ftrace system is special, skip it */
1152 if (strcmp(system->name, "ftrace") == 0)
1155 pr_info("Testing event system %s: ", system->name);
1157 /* ftrace_set_clr_event can modify the name passed in. */
1158 sysname = kstrdup(system->name, GFP_KERNEL);
1159 if (WARN_ON(!sysname)) {
1160 pr_warning("Can't allocate memory, giving up!\n");
1163 ret = ftrace_set_clr_event(sysname, 1);
1165 if (WARN_ON_ONCE(ret)) {
1166 pr_warning("error enabling system %s\n",
1173 sysname = kstrdup(system->name, GFP_KERNEL);
1174 if (WARN_ON(!sysname)) {
1175 pr_warning("Can't allocate memory, giving up!\n");
1178 ret = ftrace_set_clr_event(sysname, 0);
1181 if (WARN_ON_ONCE(ret))
1182 pr_warning("error disabling system %s\n",
1188 /* Test with all events enabled */
1190 pr_info("Running tests on all trace events:\n");
1191 pr_info("Testing all events: ");
1193 sysname = kmalloc(4, GFP_KERNEL);
1194 if (WARN_ON(!sysname)) {
1195 pr_warning("Can't allocate memory, giving up!\n");
1198 memcpy(sysname, "*:*", 4);
1199 ret = ftrace_set_clr_event(sysname, 1);
1200 if (WARN_ON_ONCE(ret)) {
1202 pr_warning("error enabling all events\n");
1209 memcpy(sysname, "*:*", 4);
1210 ret = ftrace_set_clr_event(sysname, 0);
1213 if (WARN_ON_ONCE(ret)) {
1214 pr_warning("error disabling all events\n");
1221 #ifdef CONFIG_FUNCTION_TRACER
1223 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1226 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1228 struct ring_buffer_event *event;
1229 struct ftrace_entry *entry;
1230 unsigned long flags;
1236 pc = preempt_count();
1237 resched = ftrace_preempt_disable();
1238 cpu = raw_smp_processor_id();
1239 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1244 local_save_flags(flags);
1246 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1250 entry = ring_buffer_event_data(event);
1252 entry->parent_ip = parent_ip;
1254 trace_nowake_buffer_unlock_commit(event, flags, pc);
1257 atomic_dec(&per_cpu(test_event_disable, cpu));
1258 ftrace_preempt_enable(resched);
1261 static struct ftrace_ops trace_ops __initdata =
1263 .func = function_test_events_call,
1266 static __init void event_trace_self_test_with_function(void)
1268 register_ftrace_function(&trace_ops);
1269 pr_info("Running tests again, along with the function tracer\n");
1270 event_trace_self_tests();
1271 unregister_ftrace_function(&trace_ops);
1274 static __init void event_trace_self_test_with_function(void)
1279 static __init int event_trace_self_tests_init(void)
1282 event_trace_self_tests();
1284 event_trace_self_test_with_function();
1289 late_initcall(event_trace_self_tests_init);