tracing: Have "enable" file use refcounts like the "filter"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_events.c
blob83ff94a475baed844a69056bd014cf3db5d0d33d
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/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
25 #undef TRACE_SYSTEM
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex);
30 LIST_HEAD(ftrace_events);
32 struct list_head *
33 trace_get_fields(struct ftrace_event_call *event_call)
35 if (!event_call->class->get_fields)
36 return &event_call->class->fields;
37 return event_call->class->get_fields(event_call);
40 int trace_define_field(struct ftrace_event_call *call, const char *type,
41 const char *name, int offset, int size, int is_signed,
42 int filter_type)
44 struct ftrace_event_field *field;
45 struct list_head *head;
47 if (WARN_ON(!call->class))
48 return 0;
50 field = kzalloc(sizeof(*field), GFP_KERNEL);
51 if (!field)
52 goto err;
54 field->name = kstrdup(name, GFP_KERNEL);
55 if (!field->name)
56 goto err;
58 field->type = kstrdup(type, GFP_KERNEL);
59 if (!field->type)
60 goto err;
62 if (filter_type == FILTER_OTHER)
63 field->filter_type = filter_assign_type(type);
64 else
65 field->filter_type = filter_type;
67 field->offset = offset;
68 field->size = size;
69 field->is_signed = is_signed;
71 head = trace_get_fields(call);
72 list_add(&field->link, head);
74 return 0;
76 err:
77 if (field)
78 kfree(field->name);
79 kfree(field);
81 return -ENOMEM;
83 EXPORT_SYMBOL_GPL(trace_define_field);
85 #define __common_field(type, item) \
86 ret = trace_define_field(call, #type, "common_" #item, \
87 offsetof(typeof(ent), item), \
88 sizeof(ent.item), \
89 is_signed_type(type), FILTER_OTHER); \
90 if (ret) \
91 return ret;
93 static int trace_define_common_fields(struct ftrace_event_call *call)
95 int ret;
96 struct trace_entry ent;
98 __common_field(unsigned short, type);
99 __common_field(unsigned char, flags);
100 __common_field(unsigned char, preempt_count);
101 __common_field(int, pid);
102 __common_field(int, lock_depth);
104 return ret;
107 void trace_destroy_fields(struct ftrace_event_call *call)
109 struct ftrace_event_field *field, *next;
110 struct list_head *head;
112 head = trace_get_fields(call);
113 list_for_each_entry_safe(field, next, head, link) {
114 list_del(&field->link);
115 kfree(field->type);
116 kfree(field->name);
117 kfree(field);
121 int trace_event_raw_init(struct ftrace_event_call *call)
123 int id;
125 id = register_ftrace_event(&call->event);
126 if (!id)
127 return -ENODEV;
129 return 0;
131 EXPORT_SYMBOL_GPL(trace_event_raw_init);
133 static int ftrace_event_enable_disable(struct ftrace_event_call *call,
134 int enable)
136 int ret = 0;
138 switch (enable) {
139 case 0:
140 if (call->flags & TRACE_EVENT_FL_ENABLED) {
141 call->flags &= ~TRACE_EVENT_FL_ENABLED;
142 tracing_stop_cmdline_record();
143 if (call->class->reg)
144 call->class->reg(call, TRACE_REG_UNREGISTER);
145 else
146 tracepoint_probe_unregister(call->name,
147 call->class->probe,
148 call);
150 break;
151 case 1:
152 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
153 tracing_start_cmdline_record();
154 if (call->class->reg)
155 ret = call->class->reg(call, TRACE_REG_REGISTER);
156 else
157 ret = tracepoint_probe_register(call->name,
158 call->class->probe,
159 call);
160 if (ret) {
161 tracing_stop_cmdline_record();
162 pr_info("event trace: Could not enable event "
163 "%s\n", call->name);
164 break;
166 call->flags |= TRACE_EVENT_FL_ENABLED;
168 break;
171 return ret;
174 static void ftrace_clear_events(void)
176 struct ftrace_event_call *call;
178 mutex_lock(&event_mutex);
179 list_for_each_entry(call, &ftrace_events, list) {
180 ftrace_event_enable_disable(call, 0);
182 mutex_unlock(&event_mutex);
185 static void __put_system(struct event_subsystem *system)
187 struct event_filter *filter = system->filter;
189 WARN_ON_ONCE(system->ref_count == 0);
190 if (--system->ref_count)
191 return;
193 if (filter) {
194 kfree(filter->filter_string);
195 kfree(filter);
197 kfree(system->name);
198 kfree(system);
201 static void __get_system(struct event_subsystem *system)
203 WARN_ON_ONCE(system->ref_count == 0);
204 system->ref_count++;
207 static void put_system(struct event_subsystem *system)
209 mutex_lock(&event_mutex);
210 __put_system(system);
211 mutex_unlock(&event_mutex);
215 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
217 static int __ftrace_set_clr_event(const char *match, const char *sub,
218 const char *event, int set)
220 struct ftrace_event_call *call;
221 int ret = -EINVAL;
223 mutex_lock(&event_mutex);
224 list_for_each_entry(call, &ftrace_events, list) {
226 if (!call->name || !call->class ||
227 (!call->class->probe && !call->class->reg))
228 continue;
230 if (match &&
231 strcmp(match, call->name) != 0 &&
232 strcmp(match, call->class->system) != 0)
233 continue;
235 if (sub && strcmp(sub, call->class->system) != 0)
236 continue;
238 if (event && strcmp(event, call->name) != 0)
239 continue;
241 ftrace_event_enable_disable(call, set);
243 ret = 0;
245 mutex_unlock(&event_mutex);
247 return ret;
250 static int ftrace_set_clr_event(char *buf, int set)
252 char *event = NULL, *sub = NULL, *match;
255 * The buf format can be <subsystem>:<event-name>
256 * *:<event-name> means any event by that name.
257 * :<event-name> is the same.
259 * <subsystem>:* means all events in that subsystem
260 * <subsystem>: means the same.
262 * <name> (no ':') means all events in a subsystem with
263 * the name <name> or any event that matches <name>
266 match = strsep(&buf, ":");
267 if (buf) {
268 sub = match;
269 event = buf;
270 match = NULL;
272 if (!strlen(sub) || strcmp(sub, "*") == 0)
273 sub = NULL;
274 if (!strlen(event) || strcmp(event, "*") == 0)
275 event = NULL;
278 return __ftrace_set_clr_event(match, sub, event, set);
282 * trace_set_clr_event - enable or disable an event
283 * @system: system name to match (NULL for any system)
284 * @event: event name to match (NULL for all events, within system)
285 * @set: 1 to enable, 0 to disable
287 * This is a way for other parts of the kernel to enable or disable
288 * event recording.
290 * Returns 0 on success, -EINVAL if the parameters do not match any
291 * registered events.
293 int trace_set_clr_event(const char *system, const char *event, int set)
295 return __ftrace_set_clr_event(NULL, system, event, set);
298 /* 128 should be much more than enough */
299 #define EVENT_BUF_SIZE 127
301 static ssize_t
302 ftrace_event_write(struct file *file, const char __user *ubuf,
303 size_t cnt, loff_t *ppos)
305 struct trace_parser parser;
306 ssize_t read, ret;
308 if (!cnt)
309 return 0;
311 ret = tracing_update_buffers();
312 if (ret < 0)
313 return ret;
315 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
316 return -ENOMEM;
318 read = trace_get_user(&parser, ubuf, cnt, ppos);
320 if (read >= 0 && trace_parser_loaded((&parser))) {
321 int set = 1;
323 if (*parser.buffer == '!')
324 set = 0;
326 parser.buffer[parser.idx] = 0;
328 ret = ftrace_set_clr_event(parser.buffer + !set, set);
329 if (ret)
330 goto out_put;
333 ret = read;
335 out_put:
336 trace_parser_put(&parser);
338 return ret;
341 static void *
342 t_next(struct seq_file *m, void *v, loff_t *pos)
344 struct ftrace_event_call *call = v;
346 (*pos)++;
348 list_for_each_entry_continue(call, &ftrace_events, list) {
350 * The ftrace subsystem is for showing formats only.
351 * They can not be enabled or disabled via the event files.
353 if (call->class && (call->class->probe || call->class->reg))
354 return call;
357 return NULL;
360 static void *t_start(struct seq_file *m, loff_t *pos)
362 struct ftrace_event_call *call;
363 loff_t l;
365 mutex_lock(&event_mutex);
367 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
368 for (l = 0; l <= *pos; ) {
369 call = t_next(m, call, &l);
370 if (!call)
371 break;
373 return call;
376 static void *
377 s_next(struct seq_file *m, void *v, loff_t *pos)
379 struct ftrace_event_call *call = v;
381 (*pos)++;
383 list_for_each_entry_continue(call, &ftrace_events, list) {
384 if (call->flags & TRACE_EVENT_FL_ENABLED)
385 return call;
388 return NULL;
391 static void *s_start(struct seq_file *m, loff_t *pos)
393 struct ftrace_event_call *call;
394 loff_t l;
396 mutex_lock(&event_mutex);
398 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
399 for (l = 0; l <= *pos; ) {
400 call = s_next(m, call, &l);
401 if (!call)
402 break;
404 return call;
407 static int t_show(struct seq_file *m, void *v)
409 struct ftrace_event_call *call = v;
411 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
412 seq_printf(m, "%s:", call->class->system);
413 seq_printf(m, "%s\n", call->name);
415 return 0;
418 static void t_stop(struct seq_file *m, void *p)
420 mutex_unlock(&event_mutex);
423 static int
424 ftrace_event_seq_open(struct inode *inode, struct file *file)
426 const struct seq_operations *seq_ops;
428 if ((file->f_mode & FMODE_WRITE) &&
429 (file->f_flags & O_TRUNC))
430 ftrace_clear_events();
432 seq_ops = inode->i_private;
433 return seq_open(file, seq_ops);
436 static ssize_t
437 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
438 loff_t *ppos)
440 struct ftrace_event_call *call = filp->private_data;
441 char *buf;
443 if (call->flags & TRACE_EVENT_FL_ENABLED)
444 buf = "1\n";
445 else
446 buf = "0\n";
448 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
451 static ssize_t
452 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
453 loff_t *ppos)
455 struct ftrace_event_call *call = filp->private_data;
456 char buf[64];
457 unsigned long val;
458 int ret;
460 if (cnt >= sizeof(buf))
461 return -EINVAL;
463 if (copy_from_user(&buf, ubuf, cnt))
464 return -EFAULT;
466 buf[cnt] = 0;
468 ret = strict_strtoul(buf, 10, &val);
469 if (ret < 0)
470 return ret;
472 ret = tracing_update_buffers();
473 if (ret < 0)
474 return ret;
476 switch (val) {
477 case 0:
478 case 1:
479 mutex_lock(&event_mutex);
480 ret = ftrace_event_enable_disable(call, val);
481 mutex_unlock(&event_mutex);
482 break;
484 default:
485 return -EINVAL;
488 *ppos += cnt;
490 return ret ? ret : cnt;
493 static ssize_t
494 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
495 loff_t *ppos)
497 const char set_to_char[4] = { '?', '0', '1', 'X' };
498 struct event_subsystem *system = filp->private_data;
499 struct ftrace_event_call *call;
500 char buf[2];
501 int set = 0;
502 int ret;
504 mutex_lock(&event_mutex);
505 list_for_each_entry(call, &ftrace_events, list) {
506 if (!call->name || !call->class ||
507 (!call->class->probe && !call->class->reg))
508 continue;
510 if (system && strcmp(call->class->system, system->name) != 0)
511 continue;
514 * We need to find out if all the events are set
515 * or if all events or cleared, or if we have
516 * a mixture.
518 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
521 * If we have a mixture, no need to look further.
523 if (set == 3)
524 break;
526 mutex_unlock(&event_mutex);
528 buf[0] = set_to_char[set];
529 buf[1] = '\n';
531 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
533 return ret;
536 static ssize_t
537 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
538 loff_t *ppos)
540 struct event_subsystem *system = filp->private_data;
541 const char *name = NULL;
542 unsigned long val;
543 char buf[64];
544 ssize_t ret;
546 if (cnt >= sizeof(buf))
547 return -EINVAL;
549 if (copy_from_user(&buf, ubuf, cnt))
550 return -EFAULT;
552 buf[cnt] = 0;
554 ret = strict_strtoul(buf, 10, &val);
555 if (ret < 0)
556 return ret;
558 ret = tracing_update_buffers();
559 if (ret < 0)
560 return ret;
562 if (val != 0 && val != 1)
563 return -EINVAL;
566 * Opening of "enable" adds a ref count to system,
567 * so the name is safe to use.
569 if (system)
570 name = system->name;
572 ret = __ftrace_set_clr_event(NULL, name, NULL, val);
573 if (ret)
574 goto out;
576 ret = cnt;
578 out:
579 *ppos += cnt;
581 return ret;
584 static ssize_t
585 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
586 loff_t *ppos)
588 struct ftrace_event_call *call = filp->private_data;
589 struct ftrace_event_field *field;
590 struct list_head *head;
591 struct trace_seq *s;
592 int common_field_count = 5;
593 char *buf;
594 int r = 0;
596 if (*ppos)
597 return 0;
599 s = kmalloc(sizeof(*s), GFP_KERNEL);
600 if (!s)
601 return -ENOMEM;
603 trace_seq_init(s);
605 trace_seq_printf(s, "name: %s\n", call->name);
606 trace_seq_printf(s, "ID: %d\n", call->event.type);
607 trace_seq_printf(s, "format:\n");
609 head = trace_get_fields(call);
610 list_for_each_entry_reverse(field, head, link) {
612 * Smartly shows the array type(except dynamic array).
613 * Normal:
614 * field:TYPE VAR
615 * If TYPE := TYPE[LEN], it is shown:
616 * field:TYPE VAR[LEN]
618 const char *array_descriptor = strchr(field->type, '[');
620 if (!strncmp(field->type, "__data_loc", 10))
621 array_descriptor = NULL;
623 if (!array_descriptor) {
624 r = trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
625 "\tsize:%u;\tsigned:%d;\n",
626 field->type, field->name, field->offset,
627 field->size, !!field->is_signed);
628 } else {
629 r = trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
630 "\tsize:%u;\tsigned:%d;\n",
631 (int)(array_descriptor - field->type),
632 field->type, field->name,
633 array_descriptor, field->offset,
634 field->size, !!field->is_signed);
637 if (--common_field_count == 0)
638 r = trace_seq_printf(s, "\n");
640 if (!r)
641 break;
644 if (r)
645 r = trace_seq_printf(s, "\nprint fmt: %s\n",
646 call->print_fmt);
648 if (!r) {
650 * ug! The format output is bigger than a PAGE!!
652 buf = "FORMAT TOO BIG\n";
653 r = simple_read_from_buffer(ubuf, cnt, ppos,
654 buf, strlen(buf));
655 goto out;
658 r = simple_read_from_buffer(ubuf, cnt, ppos,
659 s->buffer, s->len);
660 out:
661 kfree(s);
662 return r;
665 static ssize_t
666 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
668 struct ftrace_event_call *call = filp->private_data;
669 struct trace_seq *s;
670 int r;
672 if (*ppos)
673 return 0;
675 s = kmalloc(sizeof(*s), GFP_KERNEL);
676 if (!s)
677 return -ENOMEM;
679 trace_seq_init(s);
680 trace_seq_printf(s, "%d\n", call->event.type);
682 r = simple_read_from_buffer(ubuf, cnt, ppos,
683 s->buffer, s->len);
684 kfree(s);
685 return r;
688 static ssize_t
689 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
690 loff_t *ppos)
692 struct ftrace_event_call *call = filp->private_data;
693 struct trace_seq *s;
694 int r;
696 if (*ppos)
697 return 0;
699 s = kmalloc(sizeof(*s), GFP_KERNEL);
700 if (!s)
701 return -ENOMEM;
703 trace_seq_init(s);
705 print_event_filter(call, s);
706 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
708 kfree(s);
710 return r;
713 static ssize_t
714 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
715 loff_t *ppos)
717 struct ftrace_event_call *call = filp->private_data;
718 char *buf;
719 int err;
721 if (cnt >= PAGE_SIZE)
722 return -EINVAL;
724 buf = (char *)__get_free_page(GFP_TEMPORARY);
725 if (!buf)
726 return -ENOMEM;
728 if (copy_from_user(buf, ubuf, cnt)) {
729 free_page((unsigned long) buf);
730 return -EFAULT;
732 buf[cnt] = '\0';
734 err = apply_event_filter(call, buf);
735 free_page((unsigned long) buf);
736 if (err < 0)
737 return err;
739 *ppos += cnt;
741 return cnt;
744 static LIST_HEAD(event_subsystems);
746 static int subsystem_open(struct inode *inode, struct file *filp)
748 struct event_subsystem *system = NULL;
749 int ret;
751 if (!inode->i_private)
752 goto skip_search;
754 /* Make sure the system still exists */
755 mutex_lock(&event_mutex);
756 list_for_each_entry(system, &event_subsystems, list) {
757 if (system == inode->i_private) {
758 /* Don't open systems with no events */
759 if (!system->nr_events) {
760 system = NULL;
761 break;
763 __get_system(system);
764 break;
767 mutex_unlock(&event_mutex);
769 if (system != inode->i_private)
770 return -ENODEV;
772 skip_search:
773 ret = tracing_open_generic(inode, filp);
774 if (ret < 0 && system)
775 put_system(system);
777 return ret;
780 static int subsystem_release(struct inode *inode, struct file *file)
782 struct event_subsystem *system = inode->i_private;
784 if (system)
785 put_system(system);
787 return 0;
790 static ssize_t
791 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
792 loff_t *ppos)
794 struct event_subsystem *system = filp->private_data;
795 struct trace_seq *s;
796 int r;
798 if (*ppos)
799 return 0;
801 s = kmalloc(sizeof(*s), GFP_KERNEL);
802 if (!s)
803 return -ENOMEM;
805 trace_seq_init(s);
807 print_subsystem_event_filter(system, s);
808 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
810 kfree(s);
812 return r;
815 static ssize_t
816 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
817 loff_t *ppos)
819 struct event_subsystem *system = filp->private_data;
820 char *buf;
821 int err;
823 if (cnt >= PAGE_SIZE)
824 return -EINVAL;
826 buf = (char *)__get_free_page(GFP_TEMPORARY);
827 if (!buf)
828 return -ENOMEM;
830 if (copy_from_user(buf, ubuf, cnt)) {
831 free_page((unsigned long) buf);
832 return -EFAULT;
834 buf[cnt] = '\0';
836 err = apply_subsystem_event_filter(system, buf);
837 free_page((unsigned long) buf);
838 if (err < 0)
839 return err;
841 *ppos += cnt;
843 return cnt;
846 static ssize_t
847 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
849 int (*func)(struct trace_seq *s) = filp->private_data;
850 struct trace_seq *s;
851 int r;
853 if (*ppos)
854 return 0;
856 s = kmalloc(sizeof(*s), GFP_KERNEL);
857 if (!s)
858 return -ENOMEM;
860 trace_seq_init(s);
862 func(s);
863 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
865 kfree(s);
867 return r;
870 static const struct seq_operations show_event_seq_ops = {
871 .start = t_start,
872 .next = t_next,
873 .show = t_show,
874 .stop = t_stop,
877 static const struct seq_operations show_set_event_seq_ops = {
878 .start = s_start,
879 .next = s_next,
880 .show = t_show,
881 .stop = t_stop,
884 static const struct file_operations ftrace_avail_fops = {
885 .open = ftrace_event_seq_open,
886 .read = seq_read,
887 .llseek = seq_lseek,
888 .release = seq_release,
891 static const struct file_operations ftrace_set_event_fops = {
892 .open = ftrace_event_seq_open,
893 .read = seq_read,
894 .write = ftrace_event_write,
895 .llseek = seq_lseek,
896 .release = seq_release,
899 static const struct file_operations ftrace_enable_fops = {
900 .open = tracing_open_generic,
901 .read = event_enable_read,
902 .write = event_enable_write,
905 static const struct file_operations ftrace_event_format_fops = {
906 .open = tracing_open_generic,
907 .read = event_format_read,
910 static const struct file_operations ftrace_event_id_fops = {
911 .open = tracing_open_generic,
912 .read = event_id_read,
915 static const struct file_operations ftrace_event_filter_fops = {
916 .open = tracing_open_generic,
917 .read = event_filter_read,
918 .write = event_filter_write,
921 static const struct file_operations ftrace_subsystem_filter_fops = {
922 .open = subsystem_open,
923 .read = subsystem_filter_read,
924 .write = subsystem_filter_write,
925 .release = subsystem_release,
928 static const struct file_operations ftrace_system_enable_fops = {
929 .open = subsystem_open,
930 .read = system_enable_read,
931 .write = system_enable_write,
932 .release = subsystem_release,
935 static const struct file_operations ftrace_show_header_fops = {
936 .open = tracing_open_generic,
937 .read = show_header,
940 static struct dentry *event_trace_events_dir(void)
942 static struct dentry *d_tracer;
943 static struct dentry *d_events;
945 if (d_events)
946 return d_events;
948 d_tracer = tracing_init_dentry();
949 if (!d_tracer)
950 return NULL;
952 d_events = debugfs_create_dir("events", d_tracer);
953 if (!d_events)
954 pr_warning("Could not create debugfs "
955 "'events' directory\n");
957 return d_events;
960 static struct dentry *
961 event_subsystem_dir(const char *name, struct dentry *d_events)
963 struct event_subsystem *system;
964 struct dentry *entry;
966 /* First see if we did not already create this dir */
967 list_for_each_entry(system, &event_subsystems, list) {
968 if (strcmp(system->name, name) == 0) {
969 __get_system(system);
970 system->nr_events++;
971 return system->entry;
975 /* need to create new entry */
976 system = kmalloc(sizeof(*system), GFP_KERNEL);
977 if (!system) {
978 pr_warning("No memory to create event subsystem %s\n",
979 name);
980 return d_events;
983 system->entry = debugfs_create_dir(name, d_events);
984 if (!system->entry) {
985 pr_warning("Could not create event subsystem %s\n",
986 name);
987 kfree(system);
988 return d_events;
991 system->nr_events = 1;
992 system->ref_count = 1;
993 system->name = kstrdup(name, GFP_KERNEL);
994 if (!system->name) {
995 debugfs_remove(system->entry);
996 kfree(system);
997 return d_events;
1000 list_add(&system->list, &event_subsystems);
1002 system->filter = NULL;
1004 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1005 if (!system->filter) {
1006 pr_warning("Could not allocate filter for subsystem "
1007 "'%s'\n", name);
1008 return system->entry;
1011 entry = debugfs_create_file("filter", 0644, system->entry, system,
1012 &ftrace_subsystem_filter_fops);
1013 if (!entry) {
1014 kfree(system->filter);
1015 system->filter = NULL;
1016 pr_warning("Could not create debugfs "
1017 "'%s/filter' entry\n", name);
1020 trace_create_file("enable", 0644, system->entry, system,
1021 &ftrace_system_enable_fops);
1023 return system->entry;
1026 static int
1027 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
1028 const struct file_operations *id,
1029 const struct file_operations *enable,
1030 const struct file_operations *filter,
1031 const struct file_operations *format)
1033 struct list_head *head;
1034 int ret;
1037 * If the trace point header did not define TRACE_SYSTEM
1038 * then the system would be called "TRACE_SYSTEM".
1040 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1041 d_events = event_subsystem_dir(call->class->system, d_events);
1043 call->dir = debugfs_create_dir(call->name, d_events);
1044 if (!call->dir) {
1045 pr_warning("Could not create debugfs "
1046 "'%s' directory\n", call->name);
1047 return -1;
1050 if (call->class->probe || call->class->reg)
1051 trace_create_file("enable", 0644, call->dir, call,
1052 enable);
1054 #ifdef CONFIG_PERF_EVENTS
1055 if (call->event.type && (call->class->perf_probe || call->class->reg))
1056 trace_create_file("id", 0444, call->dir, call,
1057 id);
1058 #endif
1060 if (call->class->define_fields) {
1062 * Other events may have the same class. Only update
1063 * the fields if they are not already defined.
1065 head = trace_get_fields(call);
1066 if (list_empty(head)) {
1067 ret = trace_define_common_fields(call);
1068 if (!ret)
1069 ret = call->class->define_fields(call);
1070 if (ret < 0) {
1071 pr_warning("Could not initialize trace point"
1072 " events/%s\n", call->name);
1073 return ret;
1076 trace_create_file("filter", 0644, call->dir, call,
1077 filter);
1080 trace_create_file("format", 0444, call->dir, call,
1081 format);
1083 return 0;
1086 static int __trace_add_event_call(struct ftrace_event_call *call)
1088 struct dentry *d_events;
1089 int ret;
1091 if (!call->name)
1092 return -EINVAL;
1094 if (call->class->raw_init) {
1095 ret = call->class->raw_init(call);
1096 if (ret < 0) {
1097 if (ret != -ENOSYS)
1098 pr_warning("Could not initialize trace "
1099 "events/%s\n", call->name);
1100 return ret;
1104 d_events = event_trace_events_dir();
1105 if (!d_events)
1106 return -ENOENT;
1108 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1109 &ftrace_enable_fops, &ftrace_event_filter_fops,
1110 &ftrace_event_format_fops);
1111 if (!ret)
1112 list_add(&call->list, &ftrace_events);
1114 return ret;
1117 /* Add an additional event_call dynamically */
1118 int trace_add_event_call(struct ftrace_event_call *call)
1120 int ret;
1121 mutex_lock(&event_mutex);
1122 ret = __trace_add_event_call(call);
1123 mutex_unlock(&event_mutex);
1124 return ret;
1127 static void remove_subsystem_dir(const char *name)
1129 struct event_subsystem *system;
1131 if (strcmp(name, TRACE_SYSTEM) == 0)
1132 return;
1134 list_for_each_entry(system, &event_subsystems, list) {
1135 if (strcmp(system->name, name) == 0) {
1136 if (!--system->nr_events) {
1137 debugfs_remove_recursive(system->entry);
1138 list_del(&system->list);
1139 __put_system(system);
1141 break;
1147 * Must be called under locking both of event_mutex and trace_event_mutex.
1149 static void __trace_remove_event_call(struct ftrace_event_call *call)
1151 ftrace_event_enable_disable(call, 0);
1152 if (call->event.funcs)
1153 __unregister_ftrace_event(&call->event);
1154 debugfs_remove_recursive(call->dir);
1155 list_del(&call->list);
1156 trace_destroy_fields(call);
1157 destroy_preds(call);
1158 remove_subsystem_dir(call->class->system);
1161 /* Remove an event_call */
1162 void trace_remove_event_call(struct ftrace_event_call *call)
1164 mutex_lock(&event_mutex);
1165 down_write(&trace_event_mutex);
1166 __trace_remove_event_call(call);
1167 up_write(&trace_event_mutex);
1168 mutex_unlock(&event_mutex);
1171 #define for_each_event(event, start, end) \
1172 for (event = start; \
1173 (unsigned long)event < (unsigned long)end; \
1174 event++)
1176 #ifdef CONFIG_MODULES
1178 static LIST_HEAD(ftrace_module_file_list);
1181 * Modules must own their file_operations to keep up with
1182 * reference counting.
1184 struct ftrace_module_file_ops {
1185 struct list_head list;
1186 struct module *mod;
1187 struct file_operations id;
1188 struct file_operations enable;
1189 struct file_operations format;
1190 struct file_operations filter;
1193 static struct ftrace_module_file_ops *
1194 trace_create_file_ops(struct module *mod)
1196 struct ftrace_module_file_ops *file_ops;
1199 * This is a bit of a PITA. To allow for correct reference
1200 * counting, modules must "own" their file_operations.
1201 * To do this, we allocate the file operations that will be
1202 * used in the event directory.
1205 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1206 if (!file_ops)
1207 return NULL;
1209 file_ops->mod = mod;
1211 file_ops->id = ftrace_event_id_fops;
1212 file_ops->id.owner = mod;
1214 file_ops->enable = ftrace_enable_fops;
1215 file_ops->enable.owner = mod;
1217 file_ops->filter = ftrace_event_filter_fops;
1218 file_ops->filter.owner = mod;
1220 file_ops->format = ftrace_event_format_fops;
1221 file_ops->format.owner = mod;
1223 list_add(&file_ops->list, &ftrace_module_file_list);
1225 return file_ops;
1228 static void trace_module_add_events(struct module *mod)
1230 struct ftrace_module_file_ops *file_ops = NULL;
1231 struct ftrace_event_call *call, *start, *end;
1232 struct dentry *d_events;
1233 int ret;
1235 start = mod->trace_events;
1236 end = mod->trace_events + mod->num_trace_events;
1238 if (start == end)
1239 return;
1241 d_events = event_trace_events_dir();
1242 if (!d_events)
1243 return;
1245 for_each_event(call, start, end) {
1246 /* The linker may leave blanks */
1247 if (!call->name)
1248 continue;
1249 if (call->class->raw_init) {
1250 ret = call->class->raw_init(call);
1251 if (ret < 0) {
1252 if (ret != -ENOSYS)
1253 pr_warning("Could not initialize trace "
1254 "point events/%s\n", call->name);
1255 continue;
1259 * This module has events, create file ops for this module
1260 * if not already done.
1262 if (!file_ops) {
1263 file_ops = trace_create_file_ops(mod);
1264 if (!file_ops)
1265 return;
1267 call->mod = mod;
1268 ret = event_create_dir(call, d_events,
1269 &file_ops->id, &file_ops->enable,
1270 &file_ops->filter, &file_ops->format);
1271 if (!ret)
1272 list_add(&call->list, &ftrace_events);
1276 static void trace_module_remove_events(struct module *mod)
1278 struct ftrace_module_file_ops *file_ops;
1279 struct ftrace_event_call *call, *p;
1280 bool found = false;
1282 down_write(&trace_event_mutex);
1283 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1284 if (call->mod == mod) {
1285 found = true;
1286 __trace_remove_event_call(call);
1290 /* Now free the file_operations */
1291 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1292 if (file_ops->mod == mod)
1293 break;
1295 if (&file_ops->list != &ftrace_module_file_list) {
1296 list_del(&file_ops->list);
1297 kfree(file_ops);
1301 * It is safest to reset the ring buffer if the module being unloaded
1302 * registered any events.
1304 if (found)
1305 tracing_reset_current_online_cpus();
1306 up_write(&trace_event_mutex);
1309 static int trace_module_notify(struct notifier_block *self,
1310 unsigned long val, void *data)
1312 struct module *mod = data;
1314 mutex_lock(&event_mutex);
1315 switch (val) {
1316 case MODULE_STATE_COMING:
1317 trace_module_add_events(mod);
1318 break;
1319 case MODULE_STATE_GOING:
1320 trace_module_remove_events(mod);
1321 break;
1323 mutex_unlock(&event_mutex);
1325 return 0;
1327 #else
1328 static int trace_module_notify(struct notifier_block *self,
1329 unsigned long val, void *data)
1331 return 0;
1333 #endif /* CONFIG_MODULES */
1335 static struct notifier_block trace_module_nb = {
1336 .notifier_call = trace_module_notify,
1337 .priority = 0,
1340 extern struct ftrace_event_call __start_ftrace_events[];
1341 extern struct ftrace_event_call __stop_ftrace_events[];
1343 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1345 static __init int setup_trace_event(char *str)
1347 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1348 ring_buffer_expanded = 1;
1349 tracing_selftest_disabled = 1;
1351 return 1;
1353 __setup("trace_event=", setup_trace_event);
1355 static __init int event_trace_init(void)
1357 struct ftrace_event_call *call;
1358 struct dentry *d_tracer;
1359 struct dentry *entry;
1360 struct dentry *d_events;
1361 int ret;
1362 char *buf = bootup_event_buf;
1363 char *token;
1365 d_tracer = tracing_init_dentry();
1366 if (!d_tracer)
1367 return 0;
1369 entry = debugfs_create_file("available_events", 0444, d_tracer,
1370 (void *)&show_event_seq_ops,
1371 &ftrace_avail_fops);
1372 if (!entry)
1373 pr_warning("Could not create debugfs "
1374 "'available_events' entry\n");
1376 entry = debugfs_create_file("set_event", 0644, d_tracer,
1377 (void *)&show_set_event_seq_ops,
1378 &ftrace_set_event_fops);
1379 if (!entry)
1380 pr_warning("Could not create debugfs "
1381 "'set_event' entry\n");
1383 d_events = event_trace_events_dir();
1384 if (!d_events)
1385 return 0;
1387 /* ring buffer internal formats */
1388 trace_create_file("header_page", 0444, d_events,
1389 ring_buffer_print_page_header,
1390 &ftrace_show_header_fops);
1392 trace_create_file("header_event", 0444, d_events,
1393 ring_buffer_print_entry_header,
1394 &ftrace_show_header_fops);
1396 trace_create_file("enable", 0644, d_events,
1397 NULL, &ftrace_system_enable_fops);
1399 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1400 /* The linker may leave blanks */
1401 if (!call->name)
1402 continue;
1403 if (call->class->raw_init) {
1404 ret = call->class->raw_init(call);
1405 if (ret < 0) {
1406 if (ret != -ENOSYS)
1407 pr_warning("Could not initialize trace "
1408 "point events/%s\n", call->name);
1409 continue;
1412 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1413 &ftrace_enable_fops,
1414 &ftrace_event_filter_fops,
1415 &ftrace_event_format_fops);
1416 if (!ret)
1417 list_add(&call->list, &ftrace_events);
1420 while (true) {
1421 token = strsep(&buf, ",");
1423 if (!token)
1424 break;
1425 if (!*token)
1426 continue;
1428 ret = ftrace_set_clr_event(token, 1);
1429 if (ret)
1430 pr_warning("Failed to enable trace event: %s\n", token);
1433 ret = register_module_notifier(&trace_module_nb);
1434 if (ret)
1435 pr_warning("Failed to register trace events module notifier\n");
1437 return 0;
1439 fs_initcall(event_trace_init);
1441 #ifdef CONFIG_FTRACE_STARTUP_TEST
1443 static DEFINE_SPINLOCK(test_spinlock);
1444 static DEFINE_SPINLOCK(test_spinlock_irq);
1445 static DEFINE_MUTEX(test_mutex);
1447 static __init void test_work(struct work_struct *dummy)
1449 spin_lock(&test_spinlock);
1450 spin_lock_irq(&test_spinlock_irq);
1451 udelay(1);
1452 spin_unlock_irq(&test_spinlock_irq);
1453 spin_unlock(&test_spinlock);
1455 mutex_lock(&test_mutex);
1456 msleep(1);
1457 mutex_unlock(&test_mutex);
1460 static __init int event_test_thread(void *unused)
1462 void *test_malloc;
1464 test_malloc = kmalloc(1234, GFP_KERNEL);
1465 if (!test_malloc)
1466 pr_info("failed to kmalloc\n");
1468 schedule_on_each_cpu(test_work);
1470 kfree(test_malloc);
1472 set_current_state(TASK_INTERRUPTIBLE);
1473 while (!kthread_should_stop())
1474 schedule();
1476 return 0;
1480 * Do various things that may trigger events.
1482 static __init void event_test_stuff(void)
1484 struct task_struct *test_thread;
1486 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1487 msleep(1);
1488 kthread_stop(test_thread);
1492 * For every trace event defined, we will test each trace point separately,
1493 * and then by groups, and finally all trace points.
1495 static __init void event_trace_self_tests(void)
1497 struct ftrace_event_call *call;
1498 struct event_subsystem *system;
1499 int ret;
1501 pr_info("Running tests on trace events:\n");
1503 list_for_each_entry(call, &ftrace_events, list) {
1505 /* Only test those that have a probe */
1506 if (!call->class || !call->class->probe)
1507 continue;
1510 * Testing syscall events here is pretty useless, but
1511 * we still do it if configured. But this is time consuming.
1512 * What we really need is a user thread to perform the
1513 * syscalls as we test.
1515 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1516 if (call->class->system &&
1517 strcmp(call->class->system, "syscalls") == 0)
1518 continue;
1519 #endif
1521 pr_info("Testing event %s: ", call->name);
1524 * If an event is already enabled, someone is using
1525 * it and the self test should not be on.
1527 if (call->flags & TRACE_EVENT_FL_ENABLED) {
1528 pr_warning("Enabled event during self test!\n");
1529 WARN_ON_ONCE(1);
1530 continue;
1533 ftrace_event_enable_disable(call, 1);
1534 event_test_stuff();
1535 ftrace_event_enable_disable(call, 0);
1537 pr_cont("OK\n");
1540 /* Now test at the sub system level */
1542 pr_info("Running tests on trace event systems:\n");
1544 list_for_each_entry(system, &event_subsystems, list) {
1546 /* the ftrace system is special, skip it */
1547 if (strcmp(system->name, "ftrace") == 0)
1548 continue;
1550 pr_info("Testing event system %s: ", system->name);
1552 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1553 if (WARN_ON_ONCE(ret)) {
1554 pr_warning("error enabling system %s\n",
1555 system->name);
1556 continue;
1559 event_test_stuff();
1561 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1562 if (WARN_ON_ONCE(ret))
1563 pr_warning("error disabling system %s\n",
1564 system->name);
1566 pr_cont("OK\n");
1569 /* Test with all events enabled */
1571 pr_info("Running tests on all trace events:\n");
1572 pr_info("Testing all events: ");
1574 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1575 if (WARN_ON_ONCE(ret)) {
1576 pr_warning("error enabling all events\n");
1577 return;
1580 event_test_stuff();
1582 /* reset sysname */
1583 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1584 if (WARN_ON_ONCE(ret)) {
1585 pr_warning("error disabling all events\n");
1586 return;
1589 pr_cont("OK\n");
1592 #ifdef CONFIG_FUNCTION_TRACER
1594 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1596 static void
1597 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1599 struct ring_buffer_event *event;
1600 struct ring_buffer *buffer;
1601 struct ftrace_entry *entry;
1602 unsigned long flags;
1603 long disabled;
1604 int resched;
1605 int cpu;
1606 int pc;
1608 pc = preempt_count();
1609 resched = ftrace_preempt_disable();
1610 cpu = raw_smp_processor_id();
1611 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1613 if (disabled != 1)
1614 goto out;
1616 local_save_flags(flags);
1618 event = trace_current_buffer_lock_reserve(&buffer,
1619 TRACE_FN, sizeof(*entry),
1620 flags, pc);
1621 if (!event)
1622 goto out;
1623 entry = ring_buffer_event_data(event);
1624 entry->ip = ip;
1625 entry->parent_ip = parent_ip;
1627 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1629 out:
1630 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1631 ftrace_preempt_enable(resched);
1634 static struct ftrace_ops trace_ops __initdata =
1636 .func = function_test_events_call,
1639 static __init void event_trace_self_test_with_function(void)
1641 register_ftrace_function(&trace_ops);
1642 pr_info("Running tests again, along with the function tracer\n");
1643 event_trace_self_tests();
1644 unregister_ftrace_function(&trace_ops);
1646 #else
1647 static __init void event_trace_self_test_with_function(void)
1650 #endif
1652 static __init int event_trace_self_tests_init(void)
1654 if (!tracing_selftest_disabled) {
1655 event_trace_self_tests();
1656 event_trace_self_test_with_function();
1659 return 0;
1662 late_initcall(event_trace_self_tests_init);
1664 #endif