limit of out of order packets and receive workqueue size
[cor_2_6_31.git] / kernel / trace / trace_events.c
blob53c8fd376a88a18ac2c77f5073f76194a6e26c1a
1 /*
2 * event tracer
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>.
9 */
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);
34 if (!field)
35 goto err;
37 field->name = kstrdup(name, GFP_KERNEL);
38 if (!field->name)
39 goto err;
41 field->type = kstrdup(type, GFP_KERNEL);
42 if (!field->type)
43 goto err;
45 field->offset = offset;
46 field->size = size;
47 field->is_signed = is_signed;
48 list_add(&field->link, &call->fields);
50 return 0;
52 err:
53 if (field) {
54 kfree(field->name);
55 kfree(field->type);
57 kfree(field);
59 return -ENOMEM;
61 EXPORT_SYMBOL_GPL(trace_define_field);
63 #ifdef CONFIG_MODULES
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);
71 kfree(field->type);
72 kfree(field->name);
73 kfree(field);
77 #endif /* CONFIG_MODULES */
79 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
80 int enable)
82 switch (enable) {
83 case 0:
84 if (call->enabled) {
85 call->enabled = 0;
86 tracing_stop_cmdline_record();
87 call->unregfunc();
89 break;
90 case 1:
91 if (!call->enabled) {
92 call->enabled = 1;
93 tracing_start_cmdline_record();
94 call->regfunc();
96 break;
100 static void ftrace_clear_events(void)
102 struct ftrace_event_call *call;
104 mutex_lock(&event_mutex);
105 list_for_each_entry(call, &ftrace_events, list) {
106 ftrace_event_enable_disable(call, 0);
108 mutex_unlock(&event_mutex);
112 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
114 static int __ftrace_set_clr_event(const char *match, const char *sub,
115 const char *event, int set)
117 struct ftrace_event_call *call;
118 int ret = -EINVAL;
120 mutex_lock(&event_mutex);
121 list_for_each_entry(call, &ftrace_events, list) {
123 if (!call->name || !call->regfunc)
124 continue;
126 if (match &&
127 strcmp(match, call->name) != 0 &&
128 strcmp(match, call->system) != 0)
129 continue;
131 if (sub && strcmp(sub, call->system) != 0)
132 continue;
134 if (event && strcmp(event, call->name) != 0)
135 continue;
137 ftrace_event_enable_disable(call, set);
139 ret = 0;
141 mutex_unlock(&event_mutex);
143 return ret;
146 static int ftrace_set_clr_event(char *buf, int set)
148 char *event = NULL, *sub = NULL, *match;
151 * The buf format can be <subsystem>:<event-name>
152 * *:<event-name> means any event by that name.
153 * :<event-name> is the same.
155 * <subsystem>:* means all events in that subsystem
156 * <subsystem>: means the same.
158 * <name> (no ':') means all events in a subsystem with
159 * the name <name> or any event that matches <name>
162 match = strsep(&buf, ":");
163 if (buf) {
164 sub = match;
165 event = buf;
166 match = NULL;
168 if (!strlen(sub) || strcmp(sub, "*") == 0)
169 sub = NULL;
170 if (!strlen(event) || strcmp(event, "*") == 0)
171 event = NULL;
174 return __ftrace_set_clr_event(match, sub, event, set);
178 * trace_set_clr_event - enable or disable an event
179 * @system: system name to match (NULL for any system)
180 * @event: event name to match (NULL for all events, within system)
181 * @set: 1 to enable, 0 to disable
183 * This is a way for other parts of the kernel to enable or disable
184 * event recording.
186 * Returns 0 on success, -EINVAL if the parameters do not match any
187 * registered events.
189 int trace_set_clr_event(const char *system, const char *event, int set)
191 return __ftrace_set_clr_event(NULL, system, event, set);
194 /* 128 should be much more than enough */
195 #define EVENT_BUF_SIZE 127
197 static ssize_t
198 ftrace_event_write(struct file *file, const char __user *ubuf,
199 size_t cnt, loff_t *ppos)
201 size_t read = 0;
202 int i, set = 1;
203 ssize_t ret;
204 char *buf;
205 char ch;
207 if (!cnt || cnt < 0)
208 return 0;
210 ret = tracing_update_buffers();
211 if (ret < 0)
212 return ret;
214 ret = get_user(ch, ubuf++);
215 if (ret)
216 return ret;
217 read++;
218 cnt--;
220 /* skip white space */
221 while (cnt && isspace(ch)) {
222 ret = get_user(ch, ubuf++);
223 if (ret)
224 return ret;
225 read++;
226 cnt--;
229 /* Only white space found? */
230 if (isspace(ch)) {
231 file->f_pos += read;
232 ret = read;
233 return ret;
236 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
237 if (!buf)
238 return -ENOMEM;
240 if (cnt > EVENT_BUF_SIZE)
241 cnt = EVENT_BUF_SIZE;
243 i = 0;
244 while (cnt && !isspace(ch)) {
245 if (!i && ch == '!')
246 set = 0;
247 else
248 buf[i++] = ch;
250 ret = get_user(ch, ubuf++);
251 if (ret)
252 goto out_free;
253 read++;
254 cnt--;
256 buf[i] = 0;
258 file->f_pos += read;
260 ret = ftrace_set_clr_event(buf, set);
261 if (ret)
262 goto out_free;
264 ret = read;
266 out_free:
267 kfree(buf);
269 return ret;
272 static void *
273 t_next(struct seq_file *m, void *v, loff_t *pos)
275 struct list_head *list = m->private;
276 struct ftrace_event_call *call;
278 (*pos)++;
280 for (;;) {
281 if (list == &ftrace_events)
282 return NULL;
284 call = list_entry(list, struct ftrace_event_call, list);
287 * The ftrace subsystem is for showing formats only.
288 * They can not be enabled or disabled via the event files.
290 if (call->regfunc)
291 break;
293 list = list->next;
296 m->private = list->next;
298 return call;
301 static void *t_start(struct seq_file *m, loff_t *pos)
303 struct ftrace_event_call *call = NULL;
304 loff_t l;
306 mutex_lock(&event_mutex);
308 m->private = ftrace_events.next;
309 for (l = 0; l <= *pos; ) {
310 call = t_next(m, NULL, &l);
311 if (!call)
312 break;
314 return call;
317 static void *
318 s_next(struct seq_file *m, void *v, loff_t *pos)
320 struct list_head *list = m->private;
321 struct ftrace_event_call *call;
323 (*pos)++;
325 retry:
326 if (list == &ftrace_events)
327 return NULL;
329 call = list_entry(list, struct ftrace_event_call, list);
331 if (!call->enabled) {
332 list = list->next;
333 goto retry;
336 m->private = list->next;
338 return call;
341 static void *s_start(struct seq_file *m, loff_t *pos)
343 struct ftrace_event_call *call = NULL;
344 loff_t l;
346 mutex_lock(&event_mutex);
348 m->private = ftrace_events.next;
349 for (l = 0; l <= *pos; ) {
350 call = s_next(m, NULL, &l);
351 if (!call)
352 break;
354 return call;
357 static int t_show(struct seq_file *m, void *v)
359 struct ftrace_event_call *call = v;
361 if (strcmp(call->system, TRACE_SYSTEM) != 0)
362 seq_printf(m, "%s:", call->system);
363 seq_printf(m, "%s\n", call->name);
365 return 0;
368 static void t_stop(struct seq_file *m, void *p)
370 mutex_unlock(&event_mutex);
373 static int
374 ftrace_event_seq_open(struct inode *inode, struct file *file)
376 const struct seq_operations *seq_ops;
378 if ((file->f_mode & FMODE_WRITE) &&
379 !(file->f_flags & O_APPEND))
380 ftrace_clear_events();
382 seq_ops = inode->i_private;
383 return seq_open(file, seq_ops);
386 static ssize_t
387 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
388 loff_t *ppos)
390 struct ftrace_event_call *call = filp->private_data;
391 char *buf;
393 if (call->enabled)
394 buf = "1\n";
395 else
396 buf = "0\n";
398 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
401 static ssize_t
402 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
403 loff_t *ppos)
405 struct ftrace_event_call *call = filp->private_data;
406 char buf[64];
407 unsigned long val;
408 int ret;
410 if (cnt >= sizeof(buf))
411 return -EINVAL;
413 if (copy_from_user(&buf, ubuf, cnt))
414 return -EFAULT;
416 buf[cnt] = 0;
418 ret = strict_strtoul(buf, 10, &val);
419 if (ret < 0)
420 return ret;
422 ret = tracing_update_buffers();
423 if (ret < 0)
424 return ret;
426 switch (val) {
427 case 0:
428 case 1:
429 mutex_lock(&event_mutex);
430 ftrace_event_enable_disable(call, val);
431 mutex_unlock(&event_mutex);
432 break;
434 default:
435 return -EINVAL;
438 *ppos += cnt;
440 return cnt;
443 static ssize_t
444 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
445 loff_t *ppos)
447 const char set_to_char[4] = { '?', '0', '1', 'X' };
448 const char *system = filp->private_data;
449 struct ftrace_event_call *call;
450 char buf[2];
451 int set = 0;
452 int ret;
454 mutex_lock(&event_mutex);
455 list_for_each_entry(call, &ftrace_events, list) {
456 if (!call->name || !call->regfunc)
457 continue;
459 if (system && strcmp(call->system, system) != 0)
460 continue;
463 * We need to find out if all the events are set
464 * or if all events or cleared, or if we have
465 * a mixture.
467 set |= (1 << !!call->enabled);
470 * If we have a mixture, no need to look further.
472 if (set == 3)
473 break;
475 mutex_unlock(&event_mutex);
477 buf[0] = set_to_char[set];
478 buf[1] = '\n';
480 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
482 return ret;
485 static ssize_t
486 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
487 loff_t *ppos)
489 const char *system = filp->private_data;
490 unsigned long val;
491 char buf[64];
492 ssize_t ret;
494 if (cnt >= sizeof(buf))
495 return -EINVAL;
497 if (copy_from_user(&buf, ubuf, cnt))
498 return -EFAULT;
500 buf[cnt] = 0;
502 ret = strict_strtoul(buf, 10, &val);
503 if (ret < 0)
504 return ret;
506 ret = tracing_update_buffers();
507 if (ret < 0)
508 return ret;
510 if (val != 0 && val != 1)
511 return -EINVAL;
513 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
514 if (ret)
515 goto out;
517 ret = cnt;
519 out:
520 *ppos += cnt;
522 return ret;
525 extern char *__bad_type_size(void);
527 #undef FIELD
528 #define FIELD(type, name) \
529 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
530 #type, "common_" #name, offsetof(typeof(field), name), \
531 sizeof(field.name)
533 static int trace_write_header(struct trace_seq *s)
535 struct trace_entry field;
537 /* struct trace_entry */
538 return trace_seq_printf(s,
539 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
540 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
541 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
542 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
543 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
544 "\n",
545 FIELD(unsigned short, type),
546 FIELD(unsigned char, flags),
547 FIELD(unsigned char, preempt_count),
548 FIELD(int, pid),
549 FIELD(int, tgid));
552 static ssize_t
553 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
554 loff_t *ppos)
556 struct ftrace_event_call *call = filp->private_data;
557 struct trace_seq *s;
558 char *buf;
559 int r;
561 if (*ppos)
562 return 0;
564 s = kmalloc(sizeof(*s), GFP_KERNEL);
565 if (!s)
566 return -ENOMEM;
568 trace_seq_init(s);
570 /* If any of the first writes fail, so will the show_format. */
572 trace_seq_printf(s, "name: %s\n", call->name);
573 trace_seq_printf(s, "ID: %d\n", call->id);
574 trace_seq_printf(s, "format:\n");
575 trace_write_header(s);
577 r = call->show_format(s);
578 if (!r) {
580 * ug! The format output is bigger than a PAGE!!
582 buf = "FORMAT TOO BIG\n";
583 r = simple_read_from_buffer(ubuf, cnt, ppos,
584 buf, strlen(buf));
585 goto out;
588 r = simple_read_from_buffer(ubuf, cnt, ppos,
589 s->buffer, s->len);
590 out:
591 kfree(s);
592 return r;
595 static ssize_t
596 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
598 struct ftrace_event_call *call = filp->private_data;
599 struct trace_seq *s;
600 int r;
602 if (*ppos)
603 return 0;
605 s = kmalloc(sizeof(*s), GFP_KERNEL);
606 if (!s)
607 return -ENOMEM;
609 trace_seq_init(s);
610 trace_seq_printf(s, "%d\n", call->id);
612 r = simple_read_from_buffer(ubuf, cnt, ppos,
613 s->buffer, s->len);
614 kfree(s);
615 return r;
618 static ssize_t
619 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
620 loff_t *ppos)
622 struct ftrace_event_call *call = filp->private_data;
623 struct trace_seq *s;
624 int r;
626 if (*ppos)
627 return 0;
629 s = kmalloc(sizeof(*s), GFP_KERNEL);
630 if (!s)
631 return -ENOMEM;
633 trace_seq_init(s);
635 print_event_filter(call, s);
636 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
638 kfree(s);
640 return r;
643 static ssize_t
644 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
645 loff_t *ppos)
647 struct ftrace_event_call *call = filp->private_data;
648 char *buf;
649 int err;
651 if (cnt >= PAGE_SIZE)
652 return -EINVAL;
654 buf = (char *)__get_free_page(GFP_TEMPORARY);
655 if (!buf)
656 return -ENOMEM;
658 if (copy_from_user(buf, ubuf, cnt)) {
659 free_page((unsigned long) buf);
660 return -EFAULT;
662 buf[cnt] = '\0';
664 err = apply_event_filter(call, buf);
665 free_page((unsigned long) buf);
666 if (err < 0)
667 return err;
669 *ppos += cnt;
671 return cnt;
674 static ssize_t
675 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
676 loff_t *ppos)
678 struct event_subsystem *system = filp->private_data;
679 struct trace_seq *s;
680 int r;
682 if (*ppos)
683 return 0;
685 s = kmalloc(sizeof(*s), GFP_KERNEL);
686 if (!s)
687 return -ENOMEM;
689 trace_seq_init(s);
691 print_subsystem_event_filter(system, s);
692 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
694 kfree(s);
696 return r;
699 static ssize_t
700 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
701 loff_t *ppos)
703 struct event_subsystem *system = filp->private_data;
704 char *buf;
705 int err;
707 if (cnt >= PAGE_SIZE)
708 return -EINVAL;
710 buf = (char *)__get_free_page(GFP_TEMPORARY);
711 if (!buf)
712 return -ENOMEM;
714 if (copy_from_user(buf, ubuf, cnt)) {
715 free_page((unsigned long) buf);
716 return -EFAULT;
718 buf[cnt] = '\0';
720 err = apply_subsystem_event_filter(system, buf);
721 free_page((unsigned long) buf);
722 if (err < 0)
723 return err;
725 *ppos += cnt;
727 return cnt;
730 static ssize_t
731 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
733 int (*func)(struct trace_seq *s) = filp->private_data;
734 struct trace_seq *s;
735 int r;
737 if (*ppos)
738 return 0;
740 s = kmalloc(sizeof(*s), GFP_KERNEL);
741 if (!s)
742 return -ENOMEM;
744 trace_seq_init(s);
746 func(s);
747 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
749 kfree(s);
751 return r;
754 static const struct seq_operations show_event_seq_ops = {
755 .start = t_start,
756 .next = t_next,
757 .show = t_show,
758 .stop = t_stop,
761 static const struct seq_operations show_set_event_seq_ops = {
762 .start = s_start,
763 .next = s_next,
764 .show = t_show,
765 .stop = t_stop,
768 static const struct file_operations ftrace_avail_fops = {
769 .open = ftrace_event_seq_open,
770 .read = seq_read,
771 .llseek = seq_lseek,
772 .release = seq_release,
775 static const struct file_operations ftrace_set_event_fops = {
776 .open = ftrace_event_seq_open,
777 .read = seq_read,
778 .write = ftrace_event_write,
779 .llseek = seq_lseek,
780 .release = seq_release,
783 static const struct file_operations ftrace_enable_fops = {
784 .open = tracing_open_generic,
785 .read = event_enable_read,
786 .write = event_enable_write,
789 static const struct file_operations ftrace_event_format_fops = {
790 .open = tracing_open_generic,
791 .read = event_format_read,
794 static const struct file_operations ftrace_event_id_fops = {
795 .open = tracing_open_generic,
796 .read = event_id_read,
799 static const struct file_operations ftrace_event_filter_fops = {
800 .open = tracing_open_generic,
801 .read = event_filter_read,
802 .write = event_filter_write,
805 static const struct file_operations ftrace_subsystem_filter_fops = {
806 .open = tracing_open_generic,
807 .read = subsystem_filter_read,
808 .write = subsystem_filter_write,
811 static const struct file_operations ftrace_system_enable_fops = {
812 .open = tracing_open_generic,
813 .read = system_enable_read,
814 .write = system_enable_write,
817 static const struct file_operations ftrace_show_header_fops = {
818 .open = tracing_open_generic,
819 .read = show_header,
822 static struct dentry *event_trace_events_dir(void)
824 static struct dentry *d_tracer;
825 static struct dentry *d_events;
827 if (d_events)
828 return d_events;
830 d_tracer = tracing_init_dentry();
831 if (!d_tracer)
832 return NULL;
834 d_events = debugfs_create_dir("events", d_tracer);
835 if (!d_events)
836 pr_warning("Could not create debugfs "
837 "'events' directory\n");
839 return d_events;
842 static LIST_HEAD(event_subsystems);
844 static struct dentry *
845 event_subsystem_dir(const char *name, struct dentry *d_events)
847 struct event_subsystem *system;
848 struct dentry *entry;
850 /* First see if we did not already create this dir */
851 list_for_each_entry(system, &event_subsystems, list) {
852 if (strcmp(system->name, name) == 0)
853 return system->entry;
856 /* need to create new entry */
857 system = kmalloc(sizeof(*system), GFP_KERNEL);
858 if (!system) {
859 pr_warning("No memory to create event subsystem %s\n",
860 name);
861 return d_events;
864 system->entry = debugfs_create_dir(name, d_events);
865 if (!system->entry) {
866 pr_warning("Could not create event subsystem %s\n",
867 name);
868 kfree(system);
869 return d_events;
872 system->name = kstrdup(name, GFP_KERNEL);
873 if (!system->name) {
874 debugfs_remove(system->entry);
875 kfree(system);
876 return d_events;
879 list_add(&system->list, &event_subsystems);
881 system->filter = NULL;
883 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
884 if (!system->filter) {
885 pr_warning("Could not allocate filter for subsystem "
886 "'%s'\n", name);
887 return system->entry;
890 entry = debugfs_create_file("filter", 0644, system->entry, system,
891 &ftrace_subsystem_filter_fops);
892 if (!entry) {
893 kfree(system->filter);
894 system->filter = NULL;
895 pr_warning("Could not create debugfs "
896 "'%s/filter' entry\n", name);
899 entry = trace_create_file("enable", 0644, system->entry,
900 (void *)system->name,
901 &ftrace_system_enable_fops);
903 return system->entry;
906 static int
907 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
908 const struct file_operations *id,
909 const struct file_operations *enable,
910 const struct file_operations *filter,
911 const struct file_operations *format)
913 struct dentry *entry;
914 int ret;
917 * If the trace point header did not define TRACE_SYSTEM
918 * then the system would be called "TRACE_SYSTEM".
920 if (strcmp(call->system, TRACE_SYSTEM) != 0)
921 d_events = event_subsystem_dir(call->system, d_events);
923 if (call->raw_init) {
924 ret = call->raw_init();
925 if (ret < 0) {
926 pr_warning("Could not initialize trace point"
927 " events/%s\n", call->name);
928 return ret;
932 call->dir = debugfs_create_dir(call->name, d_events);
933 if (!call->dir) {
934 pr_warning("Could not create debugfs "
935 "'%s' directory\n", call->name);
936 return -1;
939 if (call->regfunc)
940 entry = trace_create_file("enable", 0644, call->dir, call,
941 enable);
943 if (call->id)
944 entry = trace_create_file("id", 0444, call->dir, call,
945 id);
947 if (call->define_fields) {
948 ret = call->define_fields();
949 if (ret < 0) {
950 pr_warning("Could not initialize trace point"
951 " events/%s\n", call->name);
952 return ret;
954 entry = trace_create_file("filter", 0644, call->dir, call,
955 filter);
958 /* A trace may not want to export its format */
959 if (!call->show_format)
960 return 0;
962 entry = trace_create_file("format", 0444, call->dir, call,
963 format);
965 return 0;
968 #define for_each_event(event, start, end) \
969 for (event = start; \
970 (unsigned long)event < (unsigned long)end; \
971 event++)
973 #ifdef CONFIG_MODULES
975 static LIST_HEAD(ftrace_module_file_list);
978 * Modules must own their file_operations to keep up with
979 * reference counting.
981 struct ftrace_module_file_ops {
982 struct list_head list;
983 struct module *mod;
984 struct file_operations id;
985 struct file_operations enable;
986 struct file_operations format;
987 struct file_operations filter;
990 static struct ftrace_module_file_ops *
991 trace_create_file_ops(struct module *mod)
993 struct ftrace_module_file_ops *file_ops;
996 * This is a bit of a PITA. To allow for correct reference
997 * counting, modules must "own" their file_operations.
998 * To do this, we allocate the file operations that will be
999 * used in the event directory.
1002 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1003 if (!file_ops)
1004 return NULL;
1006 file_ops->mod = mod;
1008 file_ops->id = ftrace_event_id_fops;
1009 file_ops->id.owner = mod;
1011 file_ops->enable = ftrace_enable_fops;
1012 file_ops->enable.owner = mod;
1014 file_ops->filter = ftrace_event_filter_fops;
1015 file_ops->filter.owner = mod;
1017 file_ops->format = ftrace_event_format_fops;
1018 file_ops->format.owner = mod;
1020 list_add(&file_ops->list, &ftrace_module_file_list);
1022 return file_ops;
1025 static void trace_module_add_events(struct module *mod)
1027 struct ftrace_module_file_ops *file_ops = NULL;
1028 struct ftrace_event_call *call, *start, *end;
1029 struct dentry *d_events;
1031 start = mod->trace_events;
1032 end = mod->trace_events + mod->num_trace_events;
1034 if (start == end)
1035 return;
1037 d_events = event_trace_events_dir();
1038 if (!d_events)
1039 return;
1041 for_each_event(call, start, end) {
1042 /* The linker may leave blanks */
1043 if (!call->name)
1044 continue;
1047 * This module has events, create file ops for this module
1048 * if not already done.
1050 if (!file_ops) {
1051 file_ops = trace_create_file_ops(mod);
1052 if (!file_ops)
1053 return;
1055 call->mod = mod;
1056 list_add(&call->list, &ftrace_events);
1057 event_create_dir(call, d_events,
1058 &file_ops->id, &file_ops->enable,
1059 &file_ops->filter, &file_ops->format);
1063 static void trace_module_remove_events(struct module *mod)
1065 struct ftrace_module_file_ops *file_ops;
1066 struct ftrace_event_call *call, *p;
1067 bool found = false;
1069 down_write(&trace_event_mutex);
1070 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1071 if (call->mod == mod) {
1072 found = true;
1073 ftrace_event_enable_disable(call, 0);
1074 if (call->event)
1075 __unregister_ftrace_event(call->event);
1076 debugfs_remove_recursive(call->dir);
1077 list_del(&call->list);
1078 trace_destroy_fields(call);
1079 destroy_preds(call);
1083 /* Now free the file_operations */
1084 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1085 if (file_ops->mod == mod)
1086 break;
1088 if (&file_ops->list != &ftrace_module_file_list) {
1089 list_del(&file_ops->list);
1090 kfree(file_ops);
1094 * It is safest to reset the ring buffer if the module being unloaded
1095 * registered any events.
1097 if (found)
1098 tracing_reset_current_online_cpus();
1099 up_write(&trace_event_mutex);
1102 static int trace_module_notify(struct notifier_block *self,
1103 unsigned long val, void *data)
1105 struct module *mod = data;
1107 mutex_lock(&event_mutex);
1108 switch (val) {
1109 case MODULE_STATE_COMING:
1110 trace_module_add_events(mod);
1111 break;
1112 case MODULE_STATE_GOING:
1113 trace_module_remove_events(mod);
1114 break;
1116 mutex_unlock(&event_mutex);
1118 return 0;
1120 #else
1121 static int trace_module_notify(struct notifier_block *self,
1122 unsigned long val, void *data)
1124 return 0;
1126 #endif /* CONFIG_MODULES */
1128 struct notifier_block trace_module_nb = {
1129 .notifier_call = trace_module_notify,
1130 .priority = 0,
1133 extern struct ftrace_event_call __start_ftrace_events[];
1134 extern struct ftrace_event_call __stop_ftrace_events[];
1136 static __init int event_trace_init(void)
1138 struct ftrace_event_call *call;
1139 struct dentry *d_tracer;
1140 struct dentry *entry;
1141 struct dentry *d_events;
1142 int ret;
1144 d_tracer = tracing_init_dentry();
1145 if (!d_tracer)
1146 return 0;
1148 entry = debugfs_create_file("available_events", 0444, d_tracer,
1149 (void *)&show_event_seq_ops,
1150 &ftrace_avail_fops);
1151 if (!entry)
1152 pr_warning("Could not create debugfs "
1153 "'available_events' entry\n");
1155 entry = debugfs_create_file("set_event", 0644, d_tracer,
1156 (void *)&show_set_event_seq_ops,
1157 &ftrace_set_event_fops);
1158 if (!entry)
1159 pr_warning("Could not create debugfs "
1160 "'set_event' entry\n");
1162 d_events = event_trace_events_dir();
1163 if (!d_events)
1164 return 0;
1166 /* ring buffer internal formats */
1167 trace_create_file("header_page", 0444, d_events,
1168 ring_buffer_print_page_header,
1169 &ftrace_show_header_fops);
1171 trace_create_file("header_event", 0444, d_events,
1172 ring_buffer_print_entry_header,
1173 &ftrace_show_header_fops);
1175 trace_create_file("enable", 0644, d_events,
1176 NULL, &ftrace_system_enable_fops);
1178 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1179 /* The linker may leave blanks */
1180 if (!call->name)
1181 continue;
1182 list_add(&call->list, &ftrace_events);
1183 event_create_dir(call, d_events, &ftrace_event_id_fops,
1184 &ftrace_enable_fops, &ftrace_event_filter_fops,
1185 &ftrace_event_format_fops);
1188 ret = register_module_notifier(&trace_module_nb);
1189 if (ret)
1190 pr_warning("Failed to register trace events module notifier\n");
1192 return 0;
1194 fs_initcall(event_trace_init);
1196 #ifdef CONFIG_FTRACE_STARTUP_TEST
1198 static DEFINE_SPINLOCK(test_spinlock);
1199 static DEFINE_SPINLOCK(test_spinlock_irq);
1200 static DEFINE_MUTEX(test_mutex);
1202 static __init void test_work(struct work_struct *dummy)
1204 spin_lock(&test_spinlock);
1205 spin_lock_irq(&test_spinlock_irq);
1206 udelay(1);
1207 spin_unlock_irq(&test_spinlock_irq);
1208 spin_unlock(&test_spinlock);
1210 mutex_lock(&test_mutex);
1211 msleep(1);
1212 mutex_unlock(&test_mutex);
1215 static __init int event_test_thread(void *unused)
1217 void *test_malloc;
1219 test_malloc = kmalloc(1234, GFP_KERNEL);
1220 if (!test_malloc)
1221 pr_info("failed to kmalloc\n");
1223 schedule_on_each_cpu(test_work);
1225 kfree(test_malloc);
1227 set_current_state(TASK_INTERRUPTIBLE);
1228 while (!kthread_should_stop())
1229 schedule();
1231 return 0;
1235 * Do various things that may trigger events.
1237 static __init void event_test_stuff(void)
1239 struct task_struct *test_thread;
1241 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1242 msleep(1);
1243 kthread_stop(test_thread);
1247 * For every trace event defined, we will test each trace point separately,
1248 * and then by groups, and finally all trace points.
1250 static __init void event_trace_self_tests(void)
1252 struct ftrace_event_call *call;
1253 struct event_subsystem *system;
1254 int ret;
1256 pr_info("Running tests on trace events:\n");
1258 list_for_each_entry(call, &ftrace_events, list) {
1260 /* Only test those that have a regfunc */
1261 if (!call->regfunc)
1262 continue;
1264 pr_info("Testing event %s: ", call->name);
1267 * If an event is already enabled, someone is using
1268 * it and the self test should not be on.
1270 if (call->enabled) {
1271 pr_warning("Enabled event during self test!\n");
1272 WARN_ON_ONCE(1);
1273 continue;
1276 ftrace_event_enable_disable(call, 1);
1277 event_test_stuff();
1278 ftrace_event_enable_disable(call, 0);
1280 pr_cont("OK\n");
1283 /* Now test at the sub system level */
1285 pr_info("Running tests on trace event systems:\n");
1287 list_for_each_entry(system, &event_subsystems, list) {
1289 /* the ftrace system is special, skip it */
1290 if (strcmp(system->name, "ftrace") == 0)
1291 continue;
1293 pr_info("Testing event system %s: ", system->name);
1295 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1296 if (WARN_ON_ONCE(ret)) {
1297 pr_warning("error enabling system %s\n",
1298 system->name);
1299 continue;
1302 event_test_stuff();
1304 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1305 if (WARN_ON_ONCE(ret))
1306 pr_warning("error disabling system %s\n",
1307 system->name);
1309 pr_cont("OK\n");
1312 /* Test with all events enabled */
1314 pr_info("Running tests on all trace events:\n");
1315 pr_info("Testing all events: ");
1317 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1318 if (WARN_ON_ONCE(ret)) {
1319 pr_warning("error enabling all events\n");
1320 return;
1323 event_test_stuff();
1325 /* reset sysname */
1326 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1327 if (WARN_ON_ONCE(ret)) {
1328 pr_warning("error disabling all events\n");
1329 return;
1332 pr_cont("OK\n");
1335 #ifdef CONFIG_FUNCTION_TRACER
1337 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1339 static void
1340 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1342 struct ring_buffer_event *event;
1343 struct ftrace_entry *entry;
1344 unsigned long flags;
1345 long disabled;
1346 int resched;
1347 int cpu;
1348 int pc;
1350 pc = preempt_count();
1351 resched = ftrace_preempt_disable();
1352 cpu = raw_smp_processor_id();
1353 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1355 if (disabled != 1)
1356 goto out;
1358 local_save_flags(flags);
1360 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1361 flags, pc);
1362 if (!event)
1363 goto out;
1364 entry = ring_buffer_event_data(event);
1365 entry->ip = ip;
1366 entry->parent_ip = parent_ip;
1368 trace_nowake_buffer_unlock_commit(event, flags, pc);
1370 out:
1371 atomic_dec(&per_cpu(test_event_disable, cpu));
1372 ftrace_preempt_enable(resched);
1375 static struct ftrace_ops trace_ops __initdata =
1377 .func = function_test_events_call,
1380 static __init void event_trace_self_test_with_function(void)
1382 register_ftrace_function(&trace_ops);
1383 pr_info("Running tests again, along with the function tracer\n");
1384 event_trace_self_tests();
1385 unregister_ftrace_function(&trace_ops);
1387 #else
1388 static __init void event_trace_self_test_with_function(void)
1391 #endif
1393 static __init int event_trace_self_tests_init(void)
1396 event_trace_self_tests();
1398 event_trace_self_test_with_function();
1400 return 0;
1403 late_initcall(event_trace_self_tests_init);
1405 #endif