rt2x00: do not drop usb dev reference counter on suspend
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_events.c
blob3e2a7c91c548d2d1a16b0c89f4e62d686fb245b1
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 DEFINE_MUTEX(event_storage_mutex);
31 EXPORT_SYMBOL_GPL(event_storage_mutex);
33 char event_storage[EVENT_STORAGE_SIZE];
34 EXPORT_SYMBOL_GPL(event_storage);
36 LIST_HEAD(ftrace_events);
37 LIST_HEAD(ftrace_common_fields);
39 struct list_head *
40 trace_get_fields(struct ftrace_event_call *event_call)
42 if (!event_call->class->get_fields)
43 return &event_call->class->fields;
44 return event_call->class->get_fields(event_call);
47 static int __trace_define_field(struct list_head *head, const char *type,
48 const char *name, int offset, int size,
49 int is_signed, int filter_type)
51 struct ftrace_event_field *field;
53 field = kzalloc(sizeof(*field), GFP_KERNEL);
54 if (!field)
55 goto err;
57 field->name = kstrdup(name, GFP_KERNEL);
58 if (!field->name)
59 goto err;
61 field->type = kstrdup(type, GFP_KERNEL);
62 if (!field->type)
63 goto err;
65 if (filter_type == FILTER_OTHER)
66 field->filter_type = filter_assign_type(type);
67 else
68 field->filter_type = filter_type;
70 field->offset = offset;
71 field->size = size;
72 field->is_signed = is_signed;
74 list_add(&field->link, head);
76 return 0;
78 err:
79 if (field)
80 kfree(field->name);
81 kfree(field);
83 return -ENOMEM;
86 int trace_define_field(struct ftrace_event_call *call, const char *type,
87 const char *name, int offset, int size, int is_signed,
88 int filter_type)
90 struct list_head *head;
92 if (WARN_ON(!call->class))
93 return 0;
95 head = trace_get_fields(call);
96 return __trace_define_field(head, type, name, offset, size,
97 is_signed, filter_type);
99 EXPORT_SYMBOL_GPL(trace_define_field);
101 #define __common_field(type, item) \
102 ret = __trace_define_field(&ftrace_common_fields, #type, \
103 "common_" #item, \
104 offsetof(typeof(ent), item), \
105 sizeof(ent.item), \
106 is_signed_type(type), FILTER_OTHER); \
107 if (ret) \
108 return ret;
110 static int trace_define_common_fields(void)
112 int ret;
113 struct trace_entry ent;
115 __common_field(unsigned short, type);
116 __common_field(unsigned char, flags);
117 __common_field(unsigned char, preempt_count);
118 __common_field(int, pid);
119 __common_field(int, padding);
121 return ret;
124 void trace_destroy_fields(struct ftrace_event_call *call)
126 struct ftrace_event_field *field, *next;
127 struct list_head *head;
129 head = trace_get_fields(call);
130 list_for_each_entry_safe(field, next, head, link) {
131 list_del(&field->link);
132 kfree(field->type);
133 kfree(field->name);
134 kfree(field);
138 int trace_event_raw_init(struct ftrace_event_call *call)
140 int id;
142 id = register_ftrace_event(&call->event);
143 if (!id)
144 return -ENODEV;
146 return 0;
148 EXPORT_SYMBOL_GPL(trace_event_raw_init);
150 int ftrace_event_reg(struct ftrace_event_call *call, enum trace_reg type)
152 switch (type) {
153 case TRACE_REG_REGISTER:
154 return tracepoint_probe_register(call->name,
155 call->class->probe,
156 call);
157 case TRACE_REG_UNREGISTER:
158 tracepoint_probe_unregister(call->name,
159 call->class->probe,
160 call);
161 return 0;
163 #ifdef CONFIG_PERF_EVENTS
164 case TRACE_REG_PERF_REGISTER:
165 return tracepoint_probe_register(call->name,
166 call->class->perf_probe,
167 call);
168 case TRACE_REG_PERF_UNREGISTER:
169 tracepoint_probe_unregister(call->name,
170 call->class->perf_probe,
171 call);
172 return 0;
173 #endif
175 return 0;
177 EXPORT_SYMBOL_GPL(ftrace_event_reg);
179 void trace_event_enable_cmd_record(bool enable)
181 struct ftrace_event_call *call;
183 mutex_lock(&event_mutex);
184 list_for_each_entry(call, &ftrace_events, list) {
185 if (!(call->flags & TRACE_EVENT_FL_ENABLED))
186 continue;
188 if (enable) {
189 tracing_start_cmdline_record();
190 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
191 } else {
192 tracing_stop_cmdline_record();
193 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
196 mutex_unlock(&event_mutex);
199 static int ftrace_event_enable_disable(struct ftrace_event_call *call,
200 int enable)
202 int ret = 0;
204 switch (enable) {
205 case 0:
206 if (call->flags & TRACE_EVENT_FL_ENABLED) {
207 call->flags &= ~TRACE_EVENT_FL_ENABLED;
208 if (call->flags & TRACE_EVENT_FL_RECORDED_CMD) {
209 tracing_stop_cmdline_record();
210 call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
212 call->class->reg(call, TRACE_REG_UNREGISTER);
214 break;
215 case 1:
216 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
217 if (trace_flags & TRACE_ITER_RECORD_CMD) {
218 tracing_start_cmdline_record();
219 call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
221 ret = call->class->reg(call, TRACE_REG_REGISTER);
222 if (ret) {
223 tracing_stop_cmdline_record();
224 pr_info("event trace: Could not enable event "
225 "%s\n", call->name);
226 break;
228 call->flags |= TRACE_EVENT_FL_ENABLED;
230 break;
233 return ret;
236 static void ftrace_clear_events(void)
238 struct ftrace_event_call *call;
240 mutex_lock(&event_mutex);
241 list_for_each_entry(call, &ftrace_events, list) {
242 ftrace_event_enable_disable(call, 0);
244 mutex_unlock(&event_mutex);
247 static void __put_system(struct event_subsystem *system)
249 struct event_filter *filter = system->filter;
251 WARN_ON_ONCE(system->ref_count == 0);
252 if (--system->ref_count)
253 return;
255 if (filter) {
256 kfree(filter->filter_string);
257 kfree(filter);
259 kfree(system->name);
260 kfree(system);
263 static void __get_system(struct event_subsystem *system)
265 WARN_ON_ONCE(system->ref_count == 0);
266 system->ref_count++;
269 static void put_system(struct event_subsystem *system)
271 mutex_lock(&event_mutex);
272 __put_system(system);
273 mutex_unlock(&event_mutex);
277 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
279 static int __ftrace_set_clr_event(const char *match, const char *sub,
280 const char *event, int set)
282 struct ftrace_event_call *call;
283 int ret = -EINVAL;
285 mutex_lock(&event_mutex);
286 list_for_each_entry(call, &ftrace_events, list) {
288 if (!call->name || !call->class || !call->class->reg)
289 continue;
291 if (match &&
292 strcmp(match, call->name) != 0 &&
293 strcmp(match, call->class->system) != 0)
294 continue;
296 if (sub && strcmp(sub, call->class->system) != 0)
297 continue;
299 if (event && strcmp(event, call->name) != 0)
300 continue;
302 ftrace_event_enable_disable(call, set);
304 ret = 0;
306 mutex_unlock(&event_mutex);
308 return ret;
311 static int ftrace_set_clr_event(char *buf, int set)
313 char *event = NULL, *sub = NULL, *match;
316 * The buf format can be <subsystem>:<event-name>
317 * *:<event-name> means any event by that name.
318 * :<event-name> is the same.
320 * <subsystem>:* means all events in that subsystem
321 * <subsystem>: means the same.
323 * <name> (no ':') means all events in a subsystem with
324 * the name <name> or any event that matches <name>
327 match = strsep(&buf, ":");
328 if (buf) {
329 sub = match;
330 event = buf;
331 match = NULL;
333 if (!strlen(sub) || strcmp(sub, "*") == 0)
334 sub = NULL;
335 if (!strlen(event) || strcmp(event, "*") == 0)
336 event = NULL;
339 return __ftrace_set_clr_event(match, sub, event, set);
343 * trace_set_clr_event - enable or disable an event
344 * @system: system name to match (NULL for any system)
345 * @event: event name to match (NULL for all events, within system)
346 * @set: 1 to enable, 0 to disable
348 * This is a way for other parts of the kernel to enable or disable
349 * event recording.
351 * Returns 0 on success, -EINVAL if the parameters do not match any
352 * registered events.
354 int trace_set_clr_event(const char *system, const char *event, int set)
356 return __ftrace_set_clr_event(NULL, system, event, set);
358 EXPORT_SYMBOL_GPL(trace_set_clr_event);
360 /* 128 should be much more than enough */
361 #define EVENT_BUF_SIZE 127
363 static ssize_t
364 ftrace_event_write(struct file *file, const char __user *ubuf,
365 size_t cnt, loff_t *ppos)
367 struct trace_parser parser;
368 ssize_t read, ret;
370 if (!cnt)
371 return 0;
373 ret = tracing_update_buffers();
374 if (ret < 0)
375 return ret;
377 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
378 return -ENOMEM;
380 read = trace_get_user(&parser, ubuf, cnt, ppos);
382 if (read >= 0 && trace_parser_loaded((&parser))) {
383 int set = 1;
385 if (*parser.buffer == '!')
386 set = 0;
388 parser.buffer[parser.idx] = 0;
390 ret = ftrace_set_clr_event(parser.buffer + !set, set);
391 if (ret)
392 goto out_put;
395 ret = read;
397 out_put:
398 trace_parser_put(&parser);
400 return ret;
403 static void *
404 t_next(struct seq_file *m, void *v, loff_t *pos)
406 struct ftrace_event_call *call = v;
408 (*pos)++;
410 list_for_each_entry_continue(call, &ftrace_events, list) {
412 * The ftrace subsystem is for showing formats only.
413 * They can not be enabled or disabled via the event files.
415 if (call->class && call->class->reg)
416 return call;
419 return NULL;
422 static void *t_start(struct seq_file *m, loff_t *pos)
424 struct ftrace_event_call *call;
425 loff_t l;
427 mutex_lock(&event_mutex);
429 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
430 for (l = 0; l <= *pos; ) {
431 call = t_next(m, call, &l);
432 if (!call)
433 break;
435 return call;
438 static void *
439 s_next(struct seq_file *m, void *v, loff_t *pos)
441 struct ftrace_event_call *call = v;
443 (*pos)++;
445 list_for_each_entry_continue(call, &ftrace_events, list) {
446 if (call->flags & TRACE_EVENT_FL_ENABLED)
447 return call;
450 return NULL;
453 static void *s_start(struct seq_file *m, loff_t *pos)
455 struct ftrace_event_call *call;
456 loff_t l;
458 mutex_lock(&event_mutex);
460 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
461 for (l = 0; l <= *pos; ) {
462 call = s_next(m, call, &l);
463 if (!call)
464 break;
466 return call;
469 static int t_show(struct seq_file *m, void *v)
471 struct ftrace_event_call *call = v;
473 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
474 seq_printf(m, "%s:", call->class->system);
475 seq_printf(m, "%s\n", call->name);
477 return 0;
480 static void t_stop(struct seq_file *m, void *p)
482 mutex_unlock(&event_mutex);
485 static int
486 ftrace_event_seq_open(struct inode *inode, struct file *file)
488 const struct seq_operations *seq_ops;
490 if ((file->f_mode & FMODE_WRITE) &&
491 (file->f_flags & O_TRUNC))
492 ftrace_clear_events();
494 seq_ops = inode->i_private;
495 return seq_open(file, seq_ops);
498 static ssize_t
499 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
500 loff_t *ppos)
502 struct ftrace_event_call *call = filp->private_data;
503 char *buf;
505 if (call->flags & TRACE_EVENT_FL_ENABLED)
506 buf = "1\n";
507 else
508 buf = "0\n";
510 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
513 static ssize_t
514 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
515 loff_t *ppos)
517 struct ftrace_event_call *call = filp->private_data;
518 char buf[64];
519 unsigned long val;
520 int ret;
522 if (cnt >= sizeof(buf))
523 return -EINVAL;
525 if (copy_from_user(&buf, ubuf, cnt))
526 return -EFAULT;
528 buf[cnt] = 0;
530 ret = strict_strtoul(buf, 10, &val);
531 if (ret < 0)
532 return ret;
534 ret = tracing_update_buffers();
535 if (ret < 0)
536 return ret;
538 switch (val) {
539 case 0:
540 case 1:
541 mutex_lock(&event_mutex);
542 ret = ftrace_event_enable_disable(call, val);
543 mutex_unlock(&event_mutex);
544 break;
546 default:
547 return -EINVAL;
550 *ppos += cnt;
552 return ret ? ret : cnt;
555 static ssize_t
556 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
557 loff_t *ppos)
559 const char set_to_char[4] = { '?', '0', '1', 'X' };
560 struct event_subsystem *system = filp->private_data;
561 struct ftrace_event_call *call;
562 char buf[2];
563 int set = 0;
564 int ret;
566 mutex_lock(&event_mutex);
567 list_for_each_entry(call, &ftrace_events, list) {
568 if (!call->name || !call->class || !call->class->reg)
569 continue;
571 if (system && strcmp(call->class->system, system->name) != 0)
572 continue;
575 * We need to find out if all the events are set
576 * or if all events or cleared, or if we have
577 * a mixture.
579 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
582 * If we have a mixture, no need to look further.
584 if (set == 3)
585 break;
587 mutex_unlock(&event_mutex);
589 buf[0] = set_to_char[set];
590 buf[1] = '\n';
592 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
594 return ret;
597 static ssize_t
598 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
599 loff_t *ppos)
601 struct event_subsystem *system = filp->private_data;
602 const char *name = NULL;
603 unsigned long val;
604 char buf[64];
605 ssize_t ret;
607 if (cnt >= sizeof(buf))
608 return -EINVAL;
610 if (copy_from_user(&buf, ubuf, cnt))
611 return -EFAULT;
613 buf[cnt] = 0;
615 ret = strict_strtoul(buf, 10, &val);
616 if (ret < 0)
617 return ret;
619 ret = tracing_update_buffers();
620 if (ret < 0)
621 return ret;
623 if (val != 0 && val != 1)
624 return -EINVAL;
627 * Opening of "enable" adds a ref count to system,
628 * so the name is safe to use.
630 if (system)
631 name = system->name;
633 ret = __ftrace_set_clr_event(NULL, name, NULL, val);
634 if (ret)
635 goto out;
637 ret = cnt;
639 out:
640 *ppos += cnt;
642 return ret;
645 enum {
646 FORMAT_HEADER = 1,
647 FORMAT_FIELD_SEPERATOR = 2,
648 FORMAT_PRINTFMT = 3,
651 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
653 struct ftrace_event_call *call = m->private;
654 struct ftrace_event_field *field;
655 struct list_head *common_head = &ftrace_common_fields;
656 struct list_head *head = trace_get_fields(call);
658 (*pos)++;
660 switch ((unsigned long)v) {
661 case FORMAT_HEADER:
662 if (unlikely(list_empty(common_head)))
663 return NULL;
665 field = list_entry(common_head->prev,
666 struct ftrace_event_field, link);
667 return field;
669 case FORMAT_FIELD_SEPERATOR:
670 if (unlikely(list_empty(head)))
671 return NULL;
673 field = list_entry(head->prev, struct ftrace_event_field, link);
674 return field;
676 case FORMAT_PRINTFMT:
677 /* all done */
678 return NULL;
681 field = v;
682 if (field->link.prev == common_head)
683 return (void *)FORMAT_FIELD_SEPERATOR;
684 else if (field->link.prev == head)
685 return (void *)FORMAT_PRINTFMT;
687 field = list_entry(field->link.prev, struct ftrace_event_field, link);
689 return field;
692 static void *f_start(struct seq_file *m, loff_t *pos)
694 loff_t l = 0;
695 void *p;
697 /* Start by showing the header */
698 if (!*pos)
699 return (void *)FORMAT_HEADER;
701 p = (void *)FORMAT_HEADER;
702 do {
703 p = f_next(m, p, &l);
704 } while (p && l < *pos);
706 return p;
709 static int f_show(struct seq_file *m, void *v)
711 struct ftrace_event_call *call = m->private;
712 struct ftrace_event_field *field;
713 const char *array_descriptor;
715 switch ((unsigned long)v) {
716 case FORMAT_HEADER:
717 seq_printf(m, "name: %s\n", call->name);
718 seq_printf(m, "ID: %d\n", call->event.type);
719 seq_printf(m, "format:\n");
720 return 0;
722 case FORMAT_FIELD_SEPERATOR:
723 seq_putc(m, '\n');
724 return 0;
726 case FORMAT_PRINTFMT:
727 seq_printf(m, "\nprint fmt: %s\n",
728 call->print_fmt);
729 return 0;
732 field = v;
735 * Smartly shows the array type(except dynamic array).
736 * Normal:
737 * field:TYPE VAR
738 * If TYPE := TYPE[LEN], it is shown:
739 * field:TYPE VAR[LEN]
741 array_descriptor = strchr(field->type, '[');
743 if (!strncmp(field->type, "__data_loc", 10))
744 array_descriptor = NULL;
746 if (!array_descriptor)
747 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
748 field->type, field->name, field->offset,
749 field->size, !!field->is_signed);
750 else
751 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
752 (int)(array_descriptor - field->type),
753 field->type, field->name,
754 array_descriptor, field->offset,
755 field->size, !!field->is_signed);
757 return 0;
760 static void f_stop(struct seq_file *m, void *p)
764 static const struct seq_operations trace_format_seq_ops = {
765 .start = f_start,
766 .next = f_next,
767 .stop = f_stop,
768 .show = f_show,
771 static int trace_format_open(struct inode *inode, struct file *file)
773 struct ftrace_event_call *call = inode->i_private;
774 struct seq_file *m;
775 int ret;
777 ret = seq_open(file, &trace_format_seq_ops);
778 if (ret < 0)
779 return ret;
781 m = file->private_data;
782 m->private = call;
784 return 0;
787 static ssize_t
788 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
790 struct ftrace_event_call *call = filp->private_data;
791 struct trace_seq *s;
792 int r;
794 if (*ppos)
795 return 0;
797 s = kmalloc(sizeof(*s), GFP_KERNEL);
798 if (!s)
799 return -ENOMEM;
801 trace_seq_init(s);
802 trace_seq_printf(s, "%d\n", call->event.type);
804 r = simple_read_from_buffer(ubuf, cnt, ppos,
805 s->buffer, s->len);
806 kfree(s);
807 return r;
810 static ssize_t
811 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
812 loff_t *ppos)
814 struct ftrace_event_call *call = filp->private_data;
815 struct trace_seq *s;
816 int r;
818 if (*ppos)
819 return 0;
821 s = kmalloc(sizeof(*s), GFP_KERNEL);
822 if (!s)
823 return -ENOMEM;
825 trace_seq_init(s);
827 print_event_filter(call, s);
828 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
830 kfree(s);
832 return r;
835 static ssize_t
836 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
837 loff_t *ppos)
839 struct ftrace_event_call *call = filp->private_data;
840 char *buf;
841 int err;
843 if (cnt >= PAGE_SIZE)
844 return -EINVAL;
846 buf = (char *)__get_free_page(GFP_TEMPORARY);
847 if (!buf)
848 return -ENOMEM;
850 if (copy_from_user(buf, ubuf, cnt)) {
851 free_page((unsigned long) buf);
852 return -EFAULT;
854 buf[cnt] = '\0';
856 err = apply_event_filter(call, buf);
857 free_page((unsigned long) buf);
858 if (err < 0)
859 return err;
861 *ppos += cnt;
863 return cnt;
866 static LIST_HEAD(event_subsystems);
868 static int subsystem_open(struct inode *inode, struct file *filp)
870 struct event_subsystem *system = NULL;
871 int ret;
873 if (!inode->i_private)
874 goto skip_search;
876 /* Make sure the system still exists */
877 mutex_lock(&event_mutex);
878 list_for_each_entry(system, &event_subsystems, list) {
879 if (system == inode->i_private) {
880 /* Don't open systems with no events */
881 if (!system->nr_events) {
882 system = NULL;
883 break;
885 __get_system(system);
886 break;
889 mutex_unlock(&event_mutex);
891 if (system != inode->i_private)
892 return -ENODEV;
894 skip_search:
895 ret = tracing_open_generic(inode, filp);
896 if (ret < 0 && system)
897 put_system(system);
899 return ret;
902 static int subsystem_release(struct inode *inode, struct file *file)
904 struct event_subsystem *system = inode->i_private;
906 if (system)
907 put_system(system);
909 return 0;
912 static ssize_t
913 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
914 loff_t *ppos)
916 struct event_subsystem *system = filp->private_data;
917 struct trace_seq *s;
918 int r;
920 if (*ppos)
921 return 0;
923 s = kmalloc(sizeof(*s), GFP_KERNEL);
924 if (!s)
925 return -ENOMEM;
927 trace_seq_init(s);
929 print_subsystem_event_filter(system, s);
930 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
932 kfree(s);
934 return r;
937 static ssize_t
938 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
939 loff_t *ppos)
941 struct event_subsystem *system = filp->private_data;
942 char *buf;
943 int err;
945 if (cnt >= PAGE_SIZE)
946 return -EINVAL;
948 buf = (char *)__get_free_page(GFP_TEMPORARY);
949 if (!buf)
950 return -ENOMEM;
952 if (copy_from_user(buf, ubuf, cnt)) {
953 free_page((unsigned long) buf);
954 return -EFAULT;
956 buf[cnt] = '\0';
958 err = apply_subsystem_event_filter(system, buf);
959 free_page((unsigned long) buf);
960 if (err < 0)
961 return err;
963 *ppos += cnt;
965 return cnt;
968 static ssize_t
969 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
971 int (*func)(struct trace_seq *s) = filp->private_data;
972 struct trace_seq *s;
973 int r;
975 if (*ppos)
976 return 0;
978 s = kmalloc(sizeof(*s), GFP_KERNEL);
979 if (!s)
980 return -ENOMEM;
982 trace_seq_init(s);
984 func(s);
985 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
987 kfree(s);
989 return r;
992 static const struct seq_operations show_event_seq_ops = {
993 .start = t_start,
994 .next = t_next,
995 .show = t_show,
996 .stop = t_stop,
999 static const struct seq_operations show_set_event_seq_ops = {
1000 .start = s_start,
1001 .next = s_next,
1002 .show = t_show,
1003 .stop = t_stop,
1006 static const struct file_operations ftrace_avail_fops = {
1007 .open = ftrace_event_seq_open,
1008 .read = seq_read,
1009 .llseek = seq_lseek,
1010 .release = seq_release,
1013 static const struct file_operations ftrace_set_event_fops = {
1014 .open = ftrace_event_seq_open,
1015 .read = seq_read,
1016 .write = ftrace_event_write,
1017 .llseek = seq_lseek,
1018 .release = seq_release,
1021 static const struct file_operations ftrace_enable_fops = {
1022 .open = tracing_open_generic,
1023 .read = event_enable_read,
1024 .write = event_enable_write,
1025 .llseek = default_llseek,
1028 static const struct file_operations ftrace_event_format_fops = {
1029 .open = trace_format_open,
1030 .read = seq_read,
1031 .llseek = seq_lseek,
1032 .release = seq_release,
1035 static const struct file_operations ftrace_event_id_fops = {
1036 .open = tracing_open_generic,
1037 .read = event_id_read,
1038 .llseek = default_llseek,
1041 static const struct file_operations ftrace_event_filter_fops = {
1042 .open = tracing_open_generic,
1043 .read = event_filter_read,
1044 .write = event_filter_write,
1045 .llseek = default_llseek,
1048 static const struct file_operations ftrace_subsystem_filter_fops = {
1049 .open = subsystem_open,
1050 .read = subsystem_filter_read,
1051 .write = subsystem_filter_write,
1052 .llseek = default_llseek,
1053 .release = subsystem_release,
1056 static const struct file_operations ftrace_system_enable_fops = {
1057 .open = subsystem_open,
1058 .read = system_enable_read,
1059 .write = system_enable_write,
1060 .llseek = default_llseek,
1061 .release = subsystem_release,
1064 static const struct file_operations ftrace_show_header_fops = {
1065 .open = tracing_open_generic,
1066 .read = show_header,
1067 .llseek = default_llseek,
1070 static struct dentry *event_trace_events_dir(void)
1072 static struct dentry *d_tracer;
1073 static struct dentry *d_events;
1075 if (d_events)
1076 return d_events;
1078 d_tracer = tracing_init_dentry();
1079 if (!d_tracer)
1080 return NULL;
1082 d_events = debugfs_create_dir("events", d_tracer);
1083 if (!d_events)
1084 pr_warning("Could not create debugfs "
1085 "'events' directory\n");
1087 return d_events;
1090 static struct dentry *
1091 event_subsystem_dir(const char *name, struct dentry *d_events)
1093 struct event_subsystem *system;
1094 struct dentry *entry;
1096 /* First see if we did not already create this dir */
1097 list_for_each_entry(system, &event_subsystems, list) {
1098 if (strcmp(system->name, name) == 0) {
1099 __get_system(system);
1100 system->nr_events++;
1101 return system->entry;
1105 /* need to create new entry */
1106 system = kmalloc(sizeof(*system), GFP_KERNEL);
1107 if (!system) {
1108 pr_warning("No memory to create event subsystem %s\n",
1109 name);
1110 return d_events;
1113 system->entry = debugfs_create_dir(name, d_events);
1114 if (!system->entry) {
1115 pr_warning("Could not create event subsystem %s\n",
1116 name);
1117 kfree(system);
1118 return d_events;
1121 system->nr_events = 1;
1122 system->ref_count = 1;
1123 system->name = kstrdup(name, GFP_KERNEL);
1124 if (!system->name) {
1125 debugfs_remove(system->entry);
1126 kfree(system);
1127 return d_events;
1130 list_add(&system->list, &event_subsystems);
1132 system->filter = NULL;
1134 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1135 if (!system->filter) {
1136 pr_warning("Could not allocate filter for subsystem "
1137 "'%s'\n", name);
1138 return system->entry;
1141 entry = debugfs_create_file("filter", 0644, system->entry, system,
1142 &ftrace_subsystem_filter_fops);
1143 if (!entry) {
1144 kfree(system->filter);
1145 system->filter = NULL;
1146 pr_warning("Could not create debugfs "
1147 "'%s/filter' entry\n", name);
1150 trace_create_file("enable", 0644, system->entry, system,
1151 &ftrace_system_enable_fops);
1153 return system->entry;
1156 static int
1157 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
1158 const struct file_operations *id,
1159 const struct file_operations *enable,
1160 const struct file_operations *filter,
1161 const struct file_operations *format)
1163 struct list_head *head;
1164 int ret;
1167 * If the trace point header did not define TRACE_SYSTEM
1168 * then the system would be called "TRACE_SYSTEM".
1170 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1171 d_events = event_subsystem_dir(call->class->system, d_events);
1173 call->dir = debugfs_create_dir(call->name, d_events);
1174 if (!call->dir) {
1175 pr_warning("Could not create debugfs "
1176 "'%s' directory\n", call->name);
1177 return -1;
1180 if (call->class->reg)
1181 trace_create_file("enable", 0644, call->dir, call,
1182 enable);
1184 #ifdef CONFIG_PERF_EVENTS
1185 if (call->event.type && call->class->reg)
1186 trace_create_file("id", 0444, call->dir, call,
1187 id);
1188 #endif
1191 * Other events may have the same class. Only update
1192 * the fields if they are not already defined.
1194 head = trace_get_fields(call);
1195 if (list_empty(head)) {
1196 ret = call->class->define_fields(call);
1197 if (ret < 0) {
1198 pr_warning("Could not initialize trace point"
1199 " events/%s\n", call->name);
1200 return ret;
1203 trace_create_file("filter", 0644, call->dir, call,
1204 filter);
1206 trace_create_file("format", 0444, call->dir, call,
1207 format);
1209 return 0;
1212 static int
1213 __trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
1214 const struct file_operations *id,
1215 const struct file_operations *enable,
1216 const struct file_operations *filter,
1217 const struct file_operations *format)
1219 struct dentry *d_events;
1220 int ret;
1222 /* The linker may leave blanks */
1223 if (!call->name)
1224 return -EINVAL;
1226 if (call->class->raw_init) {
1227 ret = call->class->raw_init(call);
1228 if (ret < 0) {
1229 if (ret != -ENOSYS)
1230 pr_warning("Could not initialize trace events/%s\n",
1231 call->name);
1232 return ret;
1236 d_events = event_trace_events_dir();
1237 if (!d_events)
1238 return -ENOENT;
1240 ret = event_create_dir(call, d_events, id, enable, filter, format);
1241 if (!ret)
1242 list_add(&call->list, &ftrace_events);
1243 call->mod = mod;
1245 return ret;
1248 /* Add an additional event_call dynamically */
1249 int trace_add_event_call(struct ftrace_event_call *call)
1251 int ret;
1252 mutex_lock(&event_mutex);
1253 ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1254 &ftrace_enable_fops,
1255 &ftrace_event_filter_fops,
1256 &ftrace_event_format_fops);
1257 mutex_unlock(&event_mutex);
1258 return ret;
1261 static void remove_subsystem_dir(const char *name)
1263 struct event_subsystem *system;
1265 if (strcmp(name, TRACE_SYSTEM) == 0)
1266 return;
1268 list_for_each_entry(system, &event_subsystems, list) {
1269 if (strcmp(system->name, name) == 0) {
1270 if (!--system->nr_events) {
1271 debugfs_remove_recursive(system->entry);
1272 list_del(&system->list);
1273 __put_system(system);
1275 break;
1281 * Must be called under locking both of event_mutex and trace_event_mutex.
1283 static void __trace_remove_event_call(struct ftrace_event_call *call)
1285 ftrace_event_enable_disable(call, 0);
1286 if (call->event.funcs)
1287 __unregister_ftrace_event(&call->event);
1288 debugfs_remove_recursive(call->dir);
1289 list_del(&call->list);
1290 trace_destroy_fields(call);
1291 destroy_preds(call);
1292 remove_subsystem_dir(call->class->system);
1295 /* Remove an event_call */
1296 void trace_remove_event_call(struct ftrace_event_call *call)
1298 mutex_lock(&event_mutex);
1299 down_write(&trace_event_mutex);
1300 __trace_remove_event_call(call);
1301 up_write(&trace_event_mutex);
1302 mutex_unlock(&event_mutex);
1305 #define for_each_event(event, start, end) \
1306 for (event = start; \
1307 (unsigned long)event < (unsigned long)end; \
1308 event++)
1310 #ifdef CONFIG_MODULES
1312 static LIST_HEAD(ftrace_module_file_list);
1315 * Modules must own their file_operations to keep up with
1316 * reference counting.
1318 struct ftrace_module_file_ops {
1319 struct list_head list;
1320 struct module *mod;
1321 struct file_operations id;
1322 struct file_operations enable;
1323 struct file_operations format;
1324 struct file_operations filter;
1327 static struct ftrace_module_file_ops *
1328 trace_create_file_ops(struct module *mod)
1330 struct ftrace_module_file_ops *file_ops;
1333 * This is a bit of a PITA. To allow for correct reference
1334 * counting, modules must "own" their file_operations.
1335 * To do this, we allocate the file operations that will be
1336 * used in the event directory.
1339 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1340 if (!file_ops)
1341 return NULL;
1343 file_ops->mod = mod;
1345 file_ops->id = ftrace_event_id_fops;
1346 file_ops->id.owner = mod;
1348 file_ops->enable = ftrace_enable_fops;
1349 file_ops->enable.owner = mod;
1351 file_ops->filter = ftrace_event_filter_fops;
1352 file_ops->filter.owner = mod;
1354 file_ops->format = ftrace_event_format_fops;
1355 file_ops->format.owner = mod;
1357 list_add(&file_ops->list, &ftrace_module_file_list);
1359 return file_ops;
1362 static void trace_module_add_events(struct module *mod)
1364 struct ftrace_module_file_ops *file_ops = NULL;
1365 struct ftrace_event_call **call, **start, **end;
1367 start = mod->trace_events;
1368 end = mod->trace_events + mod->num_trace_events;
1370 if (start == end)
1371 return;
1373 file_ops = trace_create_file_ops(mod);
1374 if (!file_ops)
1375 return;
1377 for_each_event(call, start, end) {
1378 __trace_add_event_call(*call, mod,
1379 &file_ops->id, &file_ops->enable,
1380 &file_ops->filter, &file_ops->format);
1384 static void trace_module_remove_events(struct module *mod)
1386 struct ftrace_module_file_ops *file_ops;
1387 struct ftrace_event_call *call, *p;
1388 bool found = false;
1390 down_write(&trace_event_mutex);
1391 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1392 if (call->mod == mod) {
1393 found = true;
1394 __trace_remove_event_call(call);
1398 /* Now free the file_operations */
1399 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1400 if (file_ops->mod == mod)
1401 break;
1403 if (&file_ops->list != &ftrace_module_file_list) {
1404 list_del(&file_ops->list);
1405 kfree(file_ops);
1409 * It is safest to reset the ring buffer if the module being unloaded
1410 * registered any events.
1412 if (found)
1413 tracing_reset_current_online_cpus();
1414 up_write(&trace_event_mutex);
1417 static int trace_module_notify(struct notifier_block *self,
1418 unsigned long val, void *data)
1420 struct module *mod = data;
1422 mutex_lock(&event_mutex);
1423 switch (val) {
1424 case MODULE_STATE_COMING:
1425 trace_module_add_events(mod);
1426 break;
1427 case MODULE_STATE_GOING:
1428 trace_module_remove_events(mod);
1429 break;
1431 mutex_unlock(&event_mutex);
1433 return 0;
1435 #else
1436 static int trace_module_notify(struct notifier_block *self,
1437 unsigned long val, void *data)
1439 return 0;
1441 #endif /* CONFIG_MODULES */
1443 static struct notifier_block trace_module_nb = {
1444 .notifier_call = trace_module_notify,
1445 .priority = 0,
1448 extern struct ftrace_event_call *__start_ftrace_events[];
1449 extern struct ftrace_event_call *__stop_ftrace_events[];
1451 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1453 static __init int setup_trace_event(char *str)
1455 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1456 ring_buffer_expanded = 1;
1457 tracing_selftest_disabled = 1;
1459 return 1;
1461 __setup("trace_event=", setup_trace_event);
1463 static __init int event_trace_init(void)
1465 struct ftrace_event_call **call;
1466 struct dentry *d_tracer;
1467 struct dentry *entry;
1468 struct dentry *d_events;
1469 int ret;
1470 char *buf = bootup_event_buf;
1471 char *token;
1473 d_tracer = tracing_init_dentry();
1474 if (!d_tracer)
1475 return 0;
1477 entry = debugfs_create_file("available_events", 0444, d_tracer,
1478 (void *)&show_event_seq_ops,
1479 &ftrace_avail_fops);
1480 if (!entry)
1481 pr_warning("Could not create debugfs "
1482 "'available_events' entry\n");
1484 entry = debugfs_create_file("set_event", 0644, d_tracer,
1485 (void *)&show_set_event_seq_ops,
1486 &ftrace_set_event_fops);
1487 if (!entry)
1488 pr_warning("Could not create debugfs "
1489 "'set_event' entry\n");
1491 d_events = event_trace_events_dir();
1492 if (!d_events)
1493 return 0;
1495 /* ring buffer internal formats */
1496 trace_create_file("header_page", 0444, d_events,
1497 ring_buffer_print_page_header,
1498 &ftrace_show_header_fops);
1500 trace_create_file("header_event", 0444, d_events,
1501 ring_buffer_print_entry_header,
1502 &ftrace_show_header_fops);
1504 trace_create_file("enable", 0644, d_events,
1505 NULL, &ftrace_system_enable_fops);
1507 if (trace_define_common_fields())
1508 pr_warning("tracing: Failed to allocate common fields");
1510 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1511 __trace_add_event_call(*call, NULL, &ftrace_event_id_fops,
1512 &ftrace_enable_fops,
1513 &ftrace_event_filter_fops,
1514 &ftrace_event_format_fops);
1517 while (true) {
1518 token = strsep(&buf, ",");
1520 if (!token)
1521 break;
1522 if (!*token)
1523 continue;
1525 ret = ftrace_set_clr_event(token, 1);
1526 if (ret)
1527 pr_warning("Failed to enable trace event: %s\n", token);
1530 ret = register_module_notifier(&trace_module_nb);
1531 if (ret)
1532 pr_warning("Failed to register trace events module notifier\n");
1534 return 0;
1536 fs_initcall(event_trace_init);
1538 #ifdef CONFIG_FTRACE_STARTUP_TEST
1540 static DEFINE_SPINLOCK(test_spinlock);
1541 static DEFINE_SPINLOCK(test_spinlock_irq);
1542 static DEFINE_MUTEX(test_mutex);
1544 static __init void test_work(struct work_struct *dummy)
1546 spin_lock(&test_spinlock);
1547 spin_lock_irq(&test_spinlock_irq);
1548 udelay(1);
1549 spin_unlock_irq(&test_spinlock_irq);
1550 spin_unlock(&test_spinlock);
1552 mutex_lock(&test_mutex);
1553 msleep(1);
1554 mutex_unlock(&test_mutex);
1557 static __init int event_test_thread(void *unused)
1559 void *test_malloc;
1561 test_malloc = kmalloc(1234, GFP_KERNEL);
1562 if (!test_malloc)
1563 pr_info("failed to kmalloc\n");
1565 schedule_on_each_cpu(test_work);
1567 kfree(test_malloc);
1569 set_current_state(TASK_INTERRUPTIBLE);
1570 while (!kthread_should_stop())
1571 schedule();
1573 return 0;
1577 * Do various things that may trigger events.
1579 static __init void event_test_stuff(void)
1581 struct task_struct *test_thread;
1583 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1584 msleep(1);
1585 kthread_stop(test_thread);
1589 * For every trace event defined, we will test each trace point separately,
1590 * and then by groups, and finally all trace points.
1592 static __init void event_trace_self_tests(void)
1594 struct ftrace_event_call *call;
1595 struct event_subsystem *system;
1596 int ret;
1598 pr_info("Running tests on trace events:\n");
1600 list_for_each_entry(call, &ftrace_events, list) {
1602 /* Only test those that have a probe */
1603 if (!call->class || !call->class->probe)
1604 continue;
1607 * Testing syscall events here is pretty useless, but
1608 * we still do it if configured. But this is time consuming.
1609 * What we really need is a user thread to perform the
1610 * syscalls as we test.
1612 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1613 if (call->class->system &&
1614 strcmp(call->class->system, "syscalls") == 0)
1615 continue;
1616 #endif
1618 pr_info("Testing event %s: ", call->name);
1621 * If an event is already enabled, someone is using
1622 * it and the self test should not be on.
1624 if (call->flags & TRACE_EVENT_FL_ENABLED) {
1625 pr_warning("Enabled event during self test!\n");
1626 WARN_ON_ONCE(1);
1627 continue;
1630 ftrace_event_enable_disable(call, 1);
1631 event_test_stuff();
1632 ftrace_event_enable_disable(call, 0);
1634 pr_cont("OK\n");
1637 /* Now test at the sub system level */
1639 pr_info("Running tests on trace event systems:\n");
1641 list_for_each_entry(system, &event_subsystems, list) {
1643 /* the ftrace system is special, skip it */
1644 if (strcmp(system->name, "ftrace") == 0)
1645 continue;
1647 pr_info("Testing event system %s: ", system->name);
1649 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1650 if (WARN_ON_ONCE(ret)) {
1651 pr_warning("error enabling system %s\n",
1652 system->name);
1653 continue;
1656 event_test_stuff();
1658 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1659 if (WARN_ON_ONCE(ret))
1660 pr_warning("error disabling system %s\n",
1661 system->name);
1663 pr_cont("OK\n");
1666 /* Test with all events enabled */
1668 pr_info("Running tests on all trace events:\n");
1669 pr_info("Testing all events: ");
1671 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1672 if (WARN_ON_ONCE(ret)) {
1673 pr_warning("error enabling all events\n");
1674 return;
1677 event_test_stuff();
1679 /* reset sysname */
1680 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1681 if (WARN_ON_ONCE(ret)) {
1682 pr_warning("error disabling all events\n");
1683 return;
1686 pr_cont("OK\n");
1689 #ifdef CONFIG_FUNCTION_TRACER
1691 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1693 static void
1694 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1696 struct ring_buffer_event *event;
1697 struct ring_buffer *buffer;
1698 struct ftrace_entry *entry;
1699 unsigned long flags;
1700 long disabled;
1701 int cpu;
1702 int pc;
1704 pc = preempt_count();
1705 preempt_disable_notrace();
1706 cpu = raw_smp_processor_id();
1707 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1709 if (disabled != 1)
1710 goto out;
1712 local_save_flags(flags);
1714 event = trace_current_buffer_lock_reserve(&buffer,
1715 TRACE_FN, sizeof(*entry),
1716 flags, pc);
1717 if (!event)
1718 goto out;
1719 entry = ring_buffer_event_data(event);
1720 entry->ip = ip;
1721 entry->parent_ip = parent_ip;
1723 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1725 out:
1726 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1727 preempt_enable_notrace();
1730 static struct ftrace_ops trace_ops __initdata =
1732 .func = function_test_events_call,
1735 static __init void event_trace_self_test_with_function(void)
1737 int ret;
1738 ret = register_ftrace_function(&trace_ops);
1739 if (WARN_ON(ret < 0)) {
1740 pr_info("Failed to enable function tracer for event tests\n");
1741 return;
1743 pr_info("Running tests again, along with the function tracer\n");
1744 event_trace_self_tests();
1745 unregister_ftrace_function(&trace_ops);
1747 #else
1748 static __init void event_trace_self_test_with_function(void)
1751 #endif
1753 static __init int event_trace_self_tests_init(void)
1755 if (!tracing_selftest_disabled) {
1756 event_trace_self_tests();
1757 event_trace_self_test_with_function();
1760 return 0;
1763 late_initcall(event_trace_self_tests_init);
1765 #endif