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 static 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)
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 list_add(&field->link, &call->fields);
60 EXPORT_SYMBOL_GPL(trace_define_field);
62 static void ftrace_clear_events(void)
64 struct ftrace_event_call *call;
66 list_for_each_entry(call, &ftrace_events, list) {
75 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
95 static int ftrace_set_clr_event(char *buf, int set)
97 struct ftrace_event_call *call;
98 char *event = NULL, *sub = NULL, *match;
102 * The buf format can be <subsystem>:<event-name>
103 * *:<event-name> means any event by that name.
104 * :<event-name> is the same.
106 * <subsystem>:* means all events in that subsystem
107 * <subsystem>: means the same.
109 * <name> (no ':') means all events in a subsystem with
110 * the name <name> or any event that matches <name>
113 match = strsep(&buf, ":");
119 if (!strlen(sub) || strcmp(sub, "*") == 0)
121 if (!strlen(event) || strcmp(event, "*") == 0)
125 mutex_lock(&event_mutex);
126 list_for_each_entry(call, &ftrace_events, list) {
128 if (!call->name || !call->regfunc)
132 strcmp(match, call->name) != 0 &&
133 strcmp(match, call->system) != 0)
136 if (sub && strcmp(sub, call->system) != 0)
139 if (event && strcmp(event, call->name) != 0)
142 ftrace_event_enable_disable(call, set);
146 mutex_unlock(&event_mutex);
151 /* 128 should be much more than enough */
152 #define EVENT_BUF_SIZE 127
155 ftrace_event_write(struct file *file, const char __user *ubuf,
156 size_t cnt, loff_t *ppos)
167 ret = tracing_update_buffers();
171 ret = get_user(ch, ubuf++);
177 /* skip white space */
178 while (cnt && isspace(ch)) {
179 ret = get_user(ch, ubuf++);
186 /* Only white space found? */
193 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
197 if (cnt > EVENT_BUF_SIZE)
198 cnt = EVENT_BUF_SIZE;
201 while (cnt && !isspace(ch)) {
207 ret = get_user(ch, ubuf++);
217 ret = ftrace_set_clr_event(buf, set);
230 t_next(struct seq_file *m, void *v, loff_t *pos)
232 struct list_head *list = m->private;
233 struct ftrace_event_call *call;
238 if (list == &ftrace_events)
241 call = list_entry(list, struct ftrace_event_call, list);
244 * The ftrace subsystem is for showing formats only.
245 * They can not be enabled or disabled via the event files.
253 m->private = list->next;
258 static void *t_start(struct seq_file *m, loff_t *pos)
260 return t_next(m, NULL, pos);
264 s_next(struct seq_file *m, void *v, loff_t *pos)
266 struct list_head *list = m->private;
267 struct ftrace_event_call *call;
272 if (list == &ftrace_events)
275 call = list_entry(list, struct ftrace_event_call, list);
277 if (!call->enabled) {
282 m->private = list->next;
287 static void *s_start(struct seq_file *m, loff_t *pos)
289 return s_next(m, NULL, pos);
292 static int t_show(struct seq_file *m, void *v)
294 struct ftrace_event_call *call = v;
296 if (strcmp(call->system, TRACE_SYSTEM) != 0)
297 seq_printf(m, "%s:", call->system);
298 seq_printf(m, "%s\n", call->name);
303 static void t_stop(struct seq_file *m, void *p)
308 ftrace_event_seq_open(struct inode *inode, struct file *file)
311 const struct seq_operations *seq_ops;
313 if ((file->f_mode & FMODE_WRITE) &&
314 !(file->f_flags & O_APPEND))
315 ftrace_clear_events();
317 seq_ops = inode->i_private;
318 ret = seq_open(file, seq_ops);
320 struct seq_file *m = file->private_data;
322 m->private = ftrace_events.next;
328 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
331 struct ftrace_event_call *call = filp->private_data;
339 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
343 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
346 struct ftrace_event_call *call = filp->private_data;
351 if (cnt >= sizeof(buf))
354 if (copy_from_user(&buf, ubuf, cnt))
359 ret = strict_strtoul(buf, 10, &val);
363 ret = tracing_update_buffers();
370 mutex_lock(&event_mutex);
371 ftrace_event_enable_disable(call, val);
372 mutex_unlock(&event_mutex);
384 extern char *__bad_type_size(void);
387 #define FIELD(type, name) \
388 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
389 #type, "common_" #name, offsetof(typeof(field), name), \
392 static int trace_write_header(struct trace_seq *s)
394 struct trace_entry field;
396 /* struct trace_entry */
397 return trace_seq_printf(s,
398 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
399 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
400 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
401 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
402 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
404 FIELD(unsigned short, type),
405 FIELD(unsigned char, flags),
406 FIELD(unsigned char, preempt_count),
412 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
415 struct ftrace_event_call *call = filp->private_data;
423 s = kmalloc(sizeof(*s), GFP_KERNEL);
429 /* If any of the first writes fail, so will the show_format. */
431 trace_seq_printf(s, "name: %s\n", call->name);
432 trace_seq_printf(s, "ID: %d\n", call->id);
433 trace_seq_printf(s, "format:\n");
434 trace_write_header(s);
436 r = call->show_format(s);
439 * ug! The format output is bigger than a PAGE!!
441 buf = "FORMAT TOO BIG\n";
442 r = simple_read_from_buffer(ubuf, cnt, ppos,
447 r = simple_read_from_buffer(ubuf, cnt, ppos,
455 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
457 struct ftrace_event_call *call = filp->private_data;
464 s = kmalloc(sizeof(*s), GFP_KERNEL);
469 trace_seq_printf(s, "%d\n", call->id);
471 r = simple_read_from_buffer(ubuf, cnt, ppos,
478 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
481 struct ftrace_event_call *call = filp->private_data;
488 s = kmalloc(sizeof(*s), GFP_KERNEL);
494 filter_print_preds(call, s);
495 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
503 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
506 struct ftrace_event_call *call = filp->private_data;
507 char buf[64], *pbuf = buf;
508 struct filter_pred *pred;
511 if (cnt >= sizeof(buf))
514 if (copy_from_user(&buf, ubuf, cnt))
518 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
522 err = filter_parse(&pbuf, pred);
524 filter_free_pred(pred);
529 filter_disable_preds(call);
530 filter_free_pred(pred);
534 err = filter_add_pred(call, pred);
536 filter_free_pred(pred);
540 filter_free_pred(pred);
548 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
551 struct event_subsystem *system = filp->private_data;
558 s = kmalloc(sizeof(*s), GFP_KERNEL);
564 filter_print_subsystem_preds(system, s);
565 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
573 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
576 struct event_subsystem *system = filp->private_data;
577 char buf[64], *pbuf = buf;
578 struct filter_pred *pred;
581 if (cnt >= sizeof(buf))
584 if (copy_from_user(&buf, ubuf, cnt))
588 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
592 err = filter_parse(&pbuf, pred);
594 filter_free_pred(pred);
599 filter_free_subsystem_preds(system);
600 filter_free_pred(pred);
604 err = filter_add_subsystem_pred(system, pred);
606 filter_free_pred(pred);
616 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
618 int (*func)(struct trace_seq *s) = filp->private_data;
625 s = kmalloc(sizeof(*s), GFP_KERNEL);
632 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
639 static const struct seq_operations show_event_seq_ops = {
646 static const struct seq_operations show_set_event_seq_ops = {
653 static const struct file_operations ftrace_avail_fops = {
654 .open = ftrace_event_seq_open,
657 .release = seq_release,
660 static const struct file_operations ftrace_set_event_fops = {
661 .open = ftrace_event_seq_open,
663 .write = ftrace_event_write,
665 .release = seq_release,
668 static const struct file_operations ftrace_enable_fops = {
669 .open = tracing_open_generic,
670 .read = event_enable_read,
671 .write = event_enable_write,
674 static const struct file_operations ftrace_event_format_fops = {
675 .open = tracing_open_generic,
676 .read = event_format_read,
679 static const struct file_operations ftrace_event_id_fops = {
680 .open = tracing_open_generic,
681 .read = event_id_read,
684 static const struct file_operations ftrace_event_filter_fops = {
685 .open = tracing_open_generic,
686 .read = event_filter_read,
687 .write = event_filter_write,
690 static const struct file_operations ftrace_subsystem_filter_fops = {
691 .open = tracing_open_generic,
692 .read = subsystem_filter_read,
693 .write = subsystem_filter_write,
696 static const struct file_operations ftrace_show_header_fops = {
697 .open = tracing_open_generic,
701 static struct dentry *event_trace_events_dir(void)
703 static struct dentry *d_tracer;
704 static struct dentry *d_events;
709 d_tracer = tracing_init_dentry();
713 d_events = debugfs_create_dir("events", d_tracer);
715 pr_warning("Could not create debugfs "
716 "'events' directory\n");
721 static LIST_HEAD(event_subsystems);
723 static struct dentry *
724 event_subsystem_dir(const char *name, struct dentry *d_events)
726 struct event_subsystem *system;
727 struct dentry *entry;
729 /* First see if we did not already create this dir */
730 list_for_each_entry(system, &event_subsystems, list) {
731 if (strcmp(system->name, name) == 0)
732 return system->entry;
735 /* need to create new entry */
736 system = kmalloc(sizeof(*system), GFP_KERNEL);
738 pr_warning("No memory to create event subsystem %s\n",
743 system->entry = debugfs_create_dir(name, d_events);
744 if (!system->entry) {
745 pr_warning("Could not create event subsystem %s\n",
751 system->name = kstrdup(name, GFP_KERNEL);
753 debugfs_remove(system->entry);
758 list_add(&system->list, &event_subsystems);
760 system->preds = NULL;
763 entry = debugfs_create_file("filter", 0644, system->entry, system,
764 &ftrace_subsystem_filter_fops);
766 pr_warning("Could not create debugfs "
767 "'%s/filter' entry\n", name);
769 return system->entry;
773 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
774 const struct file_operations *id,
775 const struct file_operations *enable,
776 const struct file_operations *filter,
777 const struct file_operations *format)
779 struct dentry *entry;
783 * If the trace point header did not define TRACE_SYSTEM
784 * then the system would be called "TRACE_SYSTEM".
786 if (strcmp(call->system, TRACE_SYSTEM) != 0)
787 d_events = event_subsystem_dir(call->system, d_events);
789 if (call->raw_init) {
790 ret = call->raw_init();
792 pr_warning("Could not initialize trace point"
793 " events/%s\n", call->name);
798 call->dir = debugfs_create_dir(call->name, d_events);
800 pr_warning("Could not create debugfs "
801 "'%s' directory\n", call->name);
806 entry = trace_create_file("enable", 0644, call->dir, call,
810 entry = trace_create_file("id", 0444, call->dir, call,
813 if (call->define_fields) {
814 ret = call->define_fields();
816 pr_warning("Could not initialize trace point"
817 " events/%s\n", call->name);
820 entry = trace_create_file("filter", 0644, call->dir, call,
824 /* A trace may not want to export its format */
825 if (!call->show_format)
828 entry = trace_create_file("format", 0444, call->dir, call,
834 #define for_each_event(event, start, end) \
835 for (event = start; \
836 (unsigned long)event < (unsigned long)end; \
839 #ifdef CONFIG_MODULES
841 static LIST_HEAD(ftrace_module_file_list);
844 * Modules must own their file_operations to keep up with
845 * reference counting.
847 struct ftrace_module_file_ops {
848 struct list_head list;
850 struct file_operations id;
851 struct file_operations enable;
852 struct file_operations format;
853 struct file_operations filter;
856 static struct ftrace_module_file_ops *
857 trace_create_file_ops(struct module *mod)
859 struct ftrace_module_file_ops *file_ops;
862 * This is a bit of a PITA. To allow for correct reference
863 * counting, modules must "own" their file_operations.
864 * To do this, we allocate the file operations that will be
865 * used in the event directory.
868 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
874 file_ops->id = ftrace_event_id_fops;
875 file_ops->id.owner = mod;
877 file_ops->enable = ftrace_enable_fops;
878 file_ops->enable.owner = mod;
880 file_ops->filter = ftrace_event_filter_fops;
881 file_ops->filter.owner = mod;
883 file_ops->format = ftrace_event_format_fops;
884 file_ops->format.owner = mod;
886 list_add(&file_ops->list, &ftrace_module_file_list);
891 static void trace_module_add_events(struct module *mod)
893 struct ftrace_module_file_ops *file_ops = NULL;
894 struct ftrace_event_call *call, *start, *end;
895 struct dentry *d_events;
897 start = mod->trace_events;
898 end = mod->trace_events + mod->num_trace_events;
903 d_events = event_trace_events_dir();
907 for_each_event(call, start, end) {
908 /* The linker may leave blanks */
913 * This module has events, create file ops for this module
914 * if not already done.
917 file_ops = trace_create_file_ops(mod);
922 list_add(&call->list, &ftrace_events);
923 event_create_dir(call, d_events,
924 &file_ops->id, &file_ops->enable,
925 &file_ops->filter, &file_ops->format);
929 static void trace_module_remove_events(struct module *mod)
931 struct ftrace_module_file_ops *file_ops;
932 struct ftrace_event_call *call, *p;
934 list_for_each_entry_safe(call, p, &ftrace_events, list) {
935 if (call->mod == mod) {
941 unregister_ftrace_event(call->event);
942 debugfs_remove_recursive(call->dir);
943 list_del(&call->list);
947 /* Now free the file_operations */
948 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
949 if (file_ops->mod == mod)
952 if (&file_ops->list != &ftrace_module_file_list) {
953 list_del(&file_ops->list);
958 static int trace_module_notify(struct notifier_block *self,
959 unsigned long val, void *data)
961 struct module *mod = data;
963 mutex_lock(&event_mutex);
965 case MODULE_STATE_COMING:
966 trace_module_add_events(mod);
968 case MODULE_STATE_GOING:
969 trace_module_remove_events(mod);
972 mutex_unlock(&event_mutex);
977 static int trace_module_notify(struct notifier_block *self,
978 unsigned long val, void *data)
982 #endif /* CONFIG_MODULES */
984 struct notifier_block trace_module_nb = {
985 .notifier_call = trace_module_notify,
989 extern struct ftrace_event_call __start_ftrace_events[];
990 extern struct ftrace_event_call __stop_ftrace_events[];
992 static __init int event_trace_init(void)
994 struct ftrace_event_call *call;
995 struct dentry *d_tracer;
996 struct dentry *entry;
997 struct dentry *d_events;
1000 d_tracer = tracing_init_dentry();
1004 entry = debugfs_create_file("available_events", 0444, d_tracer,
1005 (void *)&show_event_seq_ops,
1006 &ftrace_avail_fops);
1008 pr_warning("Could not create debugfs "
1009 "'available_events' entry\n");
1011 entry = debugfs_create_file("set_event", 0644, d_tracer,
1012 (void *)&show_set_event_seq_ops,
1013 &ftrace_set_event_fops);
1015 pr_warning("Could not create debugfs "
1016 "'set_event' entry\n");
1018 d_events = event_trace_events_dir();
1022 /* ring buffer internal formats */
1023 trace_create_file("header_page", 0444, d_events,
1024 ring_buffer_print_page_header,
1025 &ftrace_show_header_fops);
1027 trace_create_file("header_event", 0444, d_events,
1028 ring_buffer_print_entry_header,
1029 &ftrace_show_header_fops);
1031 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1032 /* The linker may leave blanks */
1035 list_add(&call->list, &ftrace_events);
1036 event_create_dir(call, d_events, &ftrace_event_id_fops,
1037 &ftrace_enable_fops, &ftrace_event_filter_fops,
1038 &ftrace_event_format_fops);
1041 ret = register_module_notifier(&trace_module_nb);
1043 pr_warning("Failed to register trace events module notifier\n");
1047 fs_initcall(event_trace_init);
1049 #ifdef CONFIG_FTRACE_STARTUP_TEST
1051 static DEFINE_SPINLOCK(test_spinlock);
1052 static DEFINE_SPINLOCK(test_spinlock_irq);
1053 static DEFINE_MUTEX(test_mutex);
1055 static __init void test_work(struct work_struct *dummy)
1057 spin_lock(&test_spinlock);
1058 spin_lock_irq(&test_spinlock_irq);
1060 spin_unlock_irq(&test_spinlock_irq);
1061 spin_unlock(&test_spinlock);
1063 mutex_lock(&test_mutex);
1065 mutex_unlock(&test_mutex);
1068 static __init int event_test_thread(void *unused)
1072 test_malloc = kmalloc(1234, GFP_KERNEL);
1074 pr_info("failed to kmalloc\n");
1076 schedule_on_each_cpu(test_work);
1080 set_current_state(TASK_INTERRUPTIBLE);
1081 while (!kthread_should_stop())
1088 * Do various things that may trigger events.
1090 static __init void event_test_stuff(void)
1092 struct task_struct *test_thread;
1094 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1096 kthread_stop(test_thread);
1100 * For every trace event defined, we will test each trace point separately,
1101 * and then by groups, and finally all trace points.
1103 static __init void event_trace_self_tests(void)
1105 struct ftrace_event_call *call;
1106 struct event_subsystem *system;
1110 pr_info("Running tests on trace events:\n");
1112 list_for_each_entry(call, &ftrace_events, list) {
1114 /* Only test those that have a regfunc */
1118 pr_info("Testing event %s: ", call->name);
1121 * If an event is already enabled, someone is using
1122 * it and the self test should not be on.
1124 if (call->enabled) {
1125 pr_warning("Enabled event during self test!\n");
1141 /* Now test at the sub system level */
1143 pr_info("Running tests on trace event systems:\n");
1145 list_for_each_entry(system, &event_subsystems, list) {
1147 /* the ftrace system is special, skip it */
1148 if (strcmp(system->name, "ftrace") == 0)
1151 pr_info("Testing event system %s: ", system->name);
1153 /* ftrace_set_clr_event can modify the name passed in. */
1154 sysname = kstrdup(system->name, GFP_KERNEL);
1155 if (WARN_ON(!sysname)) {
1156 pr_warning("Can't allocate memory, giving up!\n");
1159 ret = ftrace_set_clr_event(sysname, 1);
1161 if (WARN_ON_ONCE(ret)) {
1162 pr_warning("error enabling system %s\n",
1169 sysname = kstrdup(system->name, GFP_KERNEL);
1170 if (WARN_ON(!sysname)) {
1171 pr_warning("Can't allocate memory, giving up!\n");
1174 ret = ftrace_set_clr_event(sysname, 0);
1177 if (WARN_ON_ONCE(ret))
1178 pr_warning("error disabling system %s\n",
1184 /* Test with all events enabled */
1186 pr_info("Running tests on all trace events:\n");
1187 pr_info("Testing all events: ");
1189 sysname = kmalloc(4, GFP_KERNEL);
1190 if (WARN_ON(!sysname)) {
1191 pr_warning("Can't allocate memory, giving up!\n");
1194 memcpy(sysname, "*:*", 4);
1195 ret = ftrace_set_clr_event(sysname, 1);
1196 if (WARN_ON_ONCE(ret)) {
1198 pr_warning("error enabling all events\n");
1205 memcpy(sysname, "*:*", 4);
1206 ret = ftrace_set_clr_event(sysname, 0);
1209 if (WARN_ON_ONCE(ret)) {
1210 pr_warning("error disabling all events\n");
1217 #ifdef CONFIG_FUNCTION_TRACER
1219 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1222 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1224 struct ring_buffer_event *event;
1225 struct ftrace_entry *entry;
1226 unsigned long flags;
1232 pc = preempt_count();
1233 resched = ftrace_preempt_disable();
1234 cpu = raw_smp_processor_id();
1235 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1240 local_save_flags(flags);
1242 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1246 entry = ring_buffer_event_data(event);
1248 entry->parent_ip = parent_ip;
1250 trace_nowake_buffer_unlock_commit(event, flags, pc);
1253 atomic_dec(&per_cpu(test_event_disable, cpu));
1254 ftrace_preempt_enable(resched);
1257 static struct ftrace_ops trace_ops __initdata =
1259 .func = function_test_events_call,
1262 static __init void event_trace_self_test_with_function(void)
1264 register_ftrace_function(&trace_ops);
1265 pr_info("Running tests again, along with the function tracer\n");
1266 event_trace_self_tests();
1267 unregister_ftrace_function(&trace_ops);
1270 static __init void event_trace_self_test_with_function(void)
1275 static __init int event_trace_self_tests_init(void)
1278 event_trace_self_tests();
1280 event_trace_self_test_with_function();
1285 late_initcall(event_trace_self_tests_init);