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);
385 #define FIELD(type, name) \
386 #type, "common_" #name, offsetof(typeof(field), name), \
389 static int trace_write_header(struct trace_seq *s)
391 struct trace_entry field;
393 /* struct trace_entry */
394 return trace_seq_printf(s,
395 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
396 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
397 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
398 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
399 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
401 FIELD(unsigned char, type),
402 FIELD(unsigned char, flags),
403 FIELD(unsigned char, preempt_count),
409 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
412 struct ftrace_event_call *call = filp->private_data;
420 s = kmalloc(sizeof(*s), GFP_KERNEL);
426 /* If any of the first writes fail, so will the show_format. */
428 trace_seq_printf(s, "name: %s\n", call->name);
429 trace_seq_printf(s, "ID: %d\n", call->id);
430 trace_seq_printf(s, "format:\n");
431 trace_write_header(s);
433 r = call->show_format(s);
436 * ug! The format output is bigger than a PAGE!!
438 buf = "FORMAT TOO BIG\n";
439 r = simple_read_from_buffer(ubuf, cnt, ppos,
444 r = simple_read_from_buffer(ubuf, cnt, ppos,
452 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
454 struct ftrace_event_call *call = filp->private_data;
461 s = kmalloc(sizeof(*s), GFP_KERNEL);
466 trace_seq_printf(s, "%d\n", call->id);
468 r = simple_read_from_buffer(ubuf, cnt, ppos,
475 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
478 struct ftrace_event_call *call = filp->private_data;
485 s = kmalloc(sizeof(*s), GFP_KERNEL);
491 filter_print_preds(call->preds, call->n_preds, s);
492 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
500 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
503 struct ftrace_event_call *call = filp->private_data;
504 char buf[64], *pbuf = buf;
505 struct filter_pred *pred;
508 if (cnt >= sizeof(buf))
511 if (copy_from_user(&buf, ubuf, cnt))
515 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
519 err = filter_parse(&pbuf, pred);
521 filter_free_pred(pred);
526 filter_disable_preds(call);
527 filter_free_pred(pred);
531 err = filter_add_pred(call, pred);
533 filter_free_pred(pred);
537 filter_free_pred(pred);
545 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
548 struct event_subsystem *system = filp->private_data;
555 s = kmalloc(sizeof(*s), GFP_KERNEL);
561 filter_print_preds(system->preds, system->n_preds, s);
562 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
570 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
573 struct event_subsystem *system = filp->private_data;
574 char buf[64], *pbuf = buf;
575 struct filter_pred *pred;
578 if (cnt >= sizeof(buf))
581 if (copy_from_user(&buf, ubuf, cnt))
585 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
589 err = filter_parse(&pbuf, pred);
591 filter_free_pred(pred);
596 filter_free_subsystem_preds(system);
597 filter_free_pred(pred);
601 err = filter_add_subsystem_pred(system, pred);
603 filter_free_subsystem_preds(system);
604 filter_free_pred(pred);
614 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
616 int (*func)(struct trace_seq *s) = filp->private_data;
623 s = kmalloc(sizeof(*s), GFP_KERNEL);
630 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
637 static const struct seq_operations show_event_seq_ops = {
644 static const struct seq_operations show_set_event_seq_ops = {
651 static const struct file_operations ftrace_avail_fops = {
652 .open = ftrace_event_seq_open,
655 .release = seq_release,
658 static const struct file_operations ftrace_set_event_fops = {
659 .open = ftrace_event_seq_open,
661 .write = ftrace_event_write,
663 .release = seq_release,
666 static const struct file_operations ftrace_enable_fops = {
667 .open = tracing_open_generic,
668 .read = event_enable_read,
669 .write = event_enable_write,
672 static const struct file_operations ftrace_event_format_fops = {
673 .open = tracing_open_generic,
674 .read = event_format_read,
677 static const struct file_operations ftrace_event_id_fops = {
678 .open = tracing_open_generic,
679 .read = event_id_read,
682 static const struct file_operations ftrace_event_filter_fops = {
683 .open = tracing_open_generic,
684 .read = event_filter_read,
685 .write = event_filter_write,
688 static const struct file_operations ftrace_subsystem_filter_fops = {
689 .open = tracing_open_generic,
690 .read = subsystem_filter_read,
691 .write = subsystem_filter_write,
694 static const struct file_operations ftrace_show_header_fops = {
695 .open = tracing_open_generic,
699 static struct dentry *event_trace_events_dir(void)
701 static struct dentry *d_tracer;
702 static struct dentry *d_events;
707 d_tracer = tracing_init_dentry();
711 d_events = debugfs_create_dir("events", d_tracer);
713 pr_warning("Could not create debugfs "
714 "'events' directory\n");
719 static LIST_HEAD(event_subsystems);
721 static struct dentry *
722 event_subsystem_dir(const char *name, struct dentry *d_events)
724 struct event_subsystem *system;
725 struct dentry *entry;
727 /* First see if we did not already create this dir */
728 list_for_each_entry(system, &event_subsystems, list) {
729 if (strcmp(system->name, name) == 0)
730 return system->entry;
733 /* need to create new entry */
734 system = kmalloc(sizeof(*system), GFP_KERNEL);
736 pr_warning("No memory to create event subsystem %s\n",
741 system->entry = debugfs_create_dir(name, d_events);
742 if (!system->entry) {
743 pr_warning("Could not create event subsystem %s\n",
749 system->name = kstrdup(name, GFP_KERNEL);
751 debugfs_remove(system->entry);
756 list_add(&system->list, &event_subsystems);
758 system->preds = NULL;
761 entry = debugfs_create_file("filter", 0644, system->entry, system,
762 &ftrace_subsystem_filter_fops);
764 pr_warning("Could not create debugfs "
765 "'%s/filter' entry\n", name);
767 return system->entry;
771 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
773 struct dentry *entry;
777 * If the trace point header did not define TRACE_SYSTEM
778 * then the system would be called "TRACE_SYSTEM".
780 if (strcmp(call->system, TRACE_SYSTEM) != 0)
781 d_events = event_subsystem_dir(call->system, d_events);
783 if (call->raw_init) {
784 ret = call->raw_init();
786 pr_warning("Could not initialize trace point"
787 " events/%s\n", call->name);
792 call->dir = debugfs_create_dir(call->name, d_events);
794 pr_warning("Could not create debugfs "
795 "'%s' directory\n", call->name);
800 entry = trace_create_file("enable", 0644, call->dir, call,
801 &ftrace_enable_fops);
804 entry = trace_create_file("id", 0444, call->dir, call,
805 &ftrace_event_id_fops);
807 if (call->define_fields) {
808 ret = call->define_fields();
810 pr_warning("Could not initialize trace point"
811 " events/%s\n", call->name);
814 entry = trace_create_file("filter", 0644, call->dir, call,
815 &ftrace_event_filter_fops);
818 /* A trace may not want to export its format */
819 if (!call->show_format)
822 entry = trace_create_file("format", 0444, call->dir, call,
823 &ftrace_event_format_fops);
828 #define for_each_event(event, start, end) \
829 for (event = start; \
830 (unsigned long)event < (unsigned long)end; \
833 #ifdef CONFIG_MODULES
834 static void trace_module_add_events(struct module *mod)
836 struct ftrace_event_call *call, *start, *end;
837 struct dentry *d_events;
839 start = mod->trace_events;
840 end = mod->trace_events + mod->num_trace_events;
845 d_events = event_trace_events_dir();
849 for_each_event(call, start, end) {
850 /* The linker may leave blanks */
854 list_add(&call->list, &ftrace_events);
855 event_create_dir(call, d_events);
859 static void trace_module_remove_events(struct module *mod)
861 struct ftrace_event_call *call, *p;
863 list_for_each_entry_safe(call, p, &ftrace_events, list) {
864 if (call->mod == mod) {
870 unregister_ftrace_event(call->event);
871 debugfs_remove_recursive(call->dir);
872 list_del(&call->list);
877 static int trace_module_notify(struct notifier_block *self,
878 unsigned long val, void *data)
880 struct module *mod = data;
882 mutex_lock(&event_mutex);
884 case MODULE_STATE_COMING:
885 trace_module_add_events(mod);
887 case MODULE_STATE_GOING:
888 trace_module_remove_events(mod);
891 mutex_unlock(&event_mutex);
896 static int trace_module_notify(struct notifier_block *self,
897 unsigned long val, void *data)
901 #endif /* CONFIG_MODULES */
903 struct notifier_block trace_module_nb = {
904 .notifier_call = trace_module_notify,
908 extern struct ftrace_event_call __start_ftrace_events[];
909 extern struct ftrace_event_call __stop_ftrace_events[];
911 static __init int event_trace_init(void)
913 struct ftrace_event_call *call;
914 struct dentry *d_tracer;
915 struct dentry *entry;
916 struct dentry *d_events;
919 d_tracer = tracing_init_dentry();
923 entry = debugfs_create_file("available_events", 0444, d_tracer,
924 (void *)&show_event_seq_ops,
927 pr_warning("Could not create debugfs "
928 "'available_events' entry\n");
930 entry = debugfs_create_file("set_event", 0644, d_tracer,
931 (void *)&show_set_event_seq_ops,
932 &ftrace_set_event_fops);
934 pr_warning("Could not create debugfs "
935 "'set_event' entry\n");
937 d_events = event_trace_events_dir();
941 /* ring buffer internal formats */
942 trace_create_file("header_page", 0444, d_events,
943 ring_buffer_print_page_header,
944 &ftrace_show_header_fops);
946 trace_create_file("header_event", 0444, d_events,
947 ring_buffer_print_entry_header,
948 &ftrace_show_header_fops);
950 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
951 /* The linker may leave blanks */
954 list_add(&call->list, &ftrace_events);
955 event_create_dir(call, d_events);
958 ret = register_module_notifier(&trace_module_nb);
960 pr_warning("Failed to register trace events module notifier\n");
964 fs_initcall(event_trace_init);
966 #ifdef CONFIG_FTRACE_STARTUP_TEST
968 static DEFINE_SPINLOCK(test_spinlock);
969 static DEFINE_SPINLOCK(test_spinlock_irq);
970 static DEFINE_MUTEX(test_mutex);
972 static __init void test_work(struct work_struct *dummy)
974 spin_lock(&test_spinlock);
975 spin_lock_irq(&test_spinlock_irq);
977 spin_unlock_irq(&test_spinlock_irq);
978 spin_unlock(&test_spinlock);
980 mutex_lock(&test_mutex);
982 mutex_unlock(&test_mutex);
985 static __init int event_test_thread(void *unused)
989 test_malloc = kmalloc(1234, GFP_KERNEL);
991 pr_info("failed to kmalloc\n");
993 schedule_on_each_cpu(test_work);
997 set_current_state(TASK_INTERRUPTIBLE);
998 while (!kthread_should_stop())
1005 * Do various things that may trigger events.
1007 static __init void event_test_stuff(void)
1009 struct task_struct *test_thread;
1011 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1013 kthread_stop(test_thread);
1017 * For every trace event defined, we will test each trace point separately,
1018 * and then by groups, and finally all trace points.
1020 static __init void event_trace_self_tests(void)
1022 struct ftrace_event_call *call;
1023 struct event_subsystem *system;
1027 pr_info("Running tests on trace events:\n");
1029 list_for_each_entry(call, &ftrace_events, list) {
1031 /* Only test those that have a regfunc */
1035 pr_info("Testing event %s: ", call->name);
1038 * If an event is already enabled, someone is using
1039 * it and the self test should not be on.
1041 if (call->enabled) {
1042 pr_warning("Enabled event during self test!\n");
1058 /* Now test at the sub system level */
1060 pr_info("Running tests on trace event systems:\n");
1062 list_for_each_entry(system, &event_subsystems, list) {
1064 /* the ftrace system is special, skip it */
1065 if (strcmp(system->name, "ftrace") == 0)
1068 pr_info("Testing event system %s: ", system->name);
1070 /* ftrace_set_clr_event can modify the name passed in. */
1071 sysname = kstrdup(system->name, GFP_KERNEL);
1072 if (WARN_ON(!sysname)) {
1073 pr_warning("Can't allocate memory, giving up!\n");
1076 ret = ftrace_set_clr_event(sysname, 1);
1078 if (WARN_ON_ONCE(ret)) {
1079 pr_warning("error enabling system %s\n",
1086 sysname = kstrdup(system->name, GFP_KERNEL);
1087 if (WARN_ON(!sysname)) {
1088 pr_warning("Can't allocate memory, giving up!\n");
1091 ret = ftrace_set_clr_event(sysname, 0);
1094 if (WARN_ON_ONCE(ret))
1095 pr_warning("error disabling system %s\n",
1101 /* Test with all events enabled */
1103 pr_info("Running tests on all trace events:\n");
1104 pr_info("Testing all events: ");
1106 sysname = kmalloc(4, GFP_KERNEL);
1107 if (WARN_ON(!sysname)) {
1108 pr_warning("Can't allocate memory, giving up!\n");
1111 memcpy(sysname, "*:*", 4);
1112 ret = ftrace_set_clr_event(sysname, 1);
1113 if (WARN_ON_ONCE(ret)) {
1115 pr_warning("error enabling all events\n");
1122 memcpy(sysname, "*:*", 4);
1123 ret = ftrace_set_clr_event(sysname, 0);
1126 if (WARN_ON_ONCE(ret)) {
1127 pr_warning("error disabling all events\n");
1134 #ifdef CONFIG_FUNCTION_TRACER
1136 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1139 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1141 struct ring_buffer_event *event;
1142 struct ftrace_entry *entry;
1143 unsigned long flags;
1149 pc = preempt_count();
1150 resched = ftrace_preempt_disable();
1151 cpu = raw_smp_processor_id();
1152 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1157 local_save_flags(flags);
1159 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1163 entry = ring_buffer_event_data(event);
1165 entry->parent_ip = parent_ip;
1167 trace_current_buffer_unlock_commit(event, flags, pc);
1170 atomic_dec(&per_cpu(test_event_disable, cpu));
1171 ftrace_preempt_enable(resched);
1174 static struct ftrace_ops trace_ops __initdata =
1176 .func = function_test_events_call,
1179 static __init void event_trace_self_test_with_function(void)
1181 register_ftrace_function(&trace_ops);
1182 pr_info("Running tests again, along with the function tracer\n");
1183 event_trace_self_tests();
1184 unregister_ftrace_function(&trace_ops);
1187 static __init void event_trace_self_test_with_function(void)
1192 static __init int event_trace_self_tests_init(void)
1195 event_trace_self_tests();
1197 event_trace_self_test_with_function();
1202 late_initcall(event_trace_self_tests);