tracing: Add alloc_snapshot kernel command line parameter
[linux-2.6.git] / kernel / trace / trace_events.c
blob38b54c5edeb9c0d2643af73432c1cff3c4e92797
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 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41 static struct kmem_cache *field_cachep;
42 static struct kmem_cache *file_cachep;
44 /* Double loops, do not use break, only goto's work */
45 #define do_for_each_event_file(tr, file) \
46 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
47 list_for_each_entry(file, &tr->events, list)
49 #define do_for_each_event_file_safe(tr, file) \
50 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
51 struct ftrace_event_file *___n; \
52 list_for_each_entry_safe(file, ___n, &tr->events, list)
54 #define while_for_each_event_file() \
57 struct list_head *
58 trace_get_fields(struct ftrace_event_call *event_call)
60 if (!event_call->class->get_fields)
61 return &event_call->class->fields;
62 return event_call->class->get_fields(event_call);
65 static int __trace_define_field(struct list_head *head, const char *type,
66 const char *name, int offset, int size,
67 int is_signed, int filter_type)
69 struct ftrace_event_field *field;
71 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
72 if (!field)
73 goto err;
75 field->name = name;
76 field->type = type;
78 if (filter_type == FILTER_OTHER)
79 field->filter_type = filter_assign_type(type);
80 else
81 field->filter_type = filter_type;
83 field->offset = offset;
84 field->size = size;
85 field->is_signed = is_signed;
87 list_add(&field->link, head);
89 return 0;
91 err:
92 kmem_cache_free(field_cachep, field);
94 return -ENOMEM;
97 int trace_define_field(struct ftrace_event_call *call, const char *type,
98 const char *name, int offset, int size, int is_signed,
99 int filter_type)
101 struct list_head *head;
103 if (WARN_ON(!call->class))
104 return 0;
106 head = trace_get_fields(call);
107 return __trace_define_field(head, type, name, offset, size,
108 is_signed, filter_type);
110 EXPORT_SYMBOL_GPL(trace_define_field);
112 #define __common_field(type, item) \
113 ret = __trace_define_field(&ftrace_common_fields, #type, \
114 "common_" #item, \
115 offsetof(typeof(ent), item), \
116 sizeof(ent.item), \
117 is_signed_type(type), FILTER_OTHER); \
118 if (ret) \
119 return ret;
121 static int trace_define_common_fields(void)
123 int ret;
124 struct trace_entry ent;
126 __common_field(unsigned short, type);
127 __common_field(unsigned char, flags);
128 __common_field(unsigned char, preempt_count);
129 __common_field(int, pid);
131 return ret;
134 void trace_destroy_fields(struct ftrace_event_call *call)
136 struct ftrace_event_field *field, *next;
137 struct list_head *head;
139 head = trace_get_fields(call);
140 list_for_each_entry_safe(field, next, head, link) {
141 list_del(&field->link);
142 kmem_cache_free(field_cachep, field);
146 int trace_event_raw_init(struct ftrace_event_call *call)
148 int id;
150 id = register_ftrace_event(&call->event);
151 if (!id)
152 return -ENODEV;
154 return 0;
156 EXPORT_SYMBOL_GPL(trace_event_raw_init);
158 int ftrace_event_reg(struct ftrace_event_call *call,
159 enum trace_reg type, void *data)
161 struct ftrace_event_file *file = data;
163 switch (type) {
164 case TRACE_REG_REGISTER:
165 return tracepoint_probe_register(call->name,
166 call->class->probe,
167 file);
168 case TRACE_REG_UNREGISTER:
169 tracepoint_probe_unregister(call->name,
170 call->class->probe,
171 file);
172 return 0;
174 #ifdef CONFIG_PERF_EVENTS
175 case TRACE_REG_PERF_REGISTER:
176 return tracepoint_probe_register(call->name,
177 call->class->perf_probe,
178 call);
179 case TRACE_REG_PERF_UNREGISTER:
180 tracepoint_probe_unregister(call->name,
181 call->class->perf_probe,
182 call);
183 return 0;
184 case TRACE_REG_PERF_OPEN:
185 case TRACE_REG_PERF_CLOSE:
186 case TRACE_REG_PERF_ADD:
187 case TRACE_REG_PERF_DEL:
188 return 0;
189 #endif
191 return 0;
193 EXPORT_SYMBOL_GPL(ftrace_event_reg);
195 void trace_event_enable_cmd_record(bool enable)
197 struct ftrace_event_file *file;
198 struct trace_array *tr;
200 mutex_lock(&event_mutex);
201 do_for_each_event_file(tr, file) {
203 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
204 continue;
206 if (enable) {
207 tracing_start_cmdline_record();
208 file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
209 } else {
210 tracing_stop_cmdline_record();
211 file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
213 } while_for_each_event_file();
214 mutex_unlock(&event_mutex);
217 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
218 int enable)
220 struct ftrace_event_call *call = file->event_call;
221 int ret = 0;
223 switch (enable) {
224 case 0:
225 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
226 file->flags &= ~FTRACE_EVENT_FL_ENABLED;
227 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
228 tracing_stop_cmdline_record();
229 file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
231 call->class->reg(call, TRACE_REG_UNREGISTER, file);
233 break;
234 case 1:
235 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
236 if (trace_flags & TRACE_ITER_RECORD_CMD) {
237 tracing_start_cmdline_record();
238 file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
240 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
241 if (ret) {
242 tracing_stop_cmdline_record();
243 pr_info("event trace: Could not enable event "
244 "%s\n", call->name);
245 break;
247 file->flags |= FTRACE_EVENT_FL_ENABLED;
249 /* WAS_ENABLED gets set but never cleared. */
250 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
252 break;
255 return ret;
258 static void ftrace_clear_events(struct trace_array *tr)
260 struct ftrace_event_file *file;
262 mutex_lock(&event_mutex);
263 list_for_each_entry(file, &tr->events, list) {
264 ftrace_event_enable_disable(file, 0);
266 mutex_unlock(&event_mutex);
269 static void __put_system(struct event_subsystem *system)
271 struct event_filter *filter = system->filter;
273 WARN_ON_ONCE(system->ref_count == 0);
274 if (--system->ref_count)
275 return;
277 list_del(&system->list);
279 if (filter) {
280 kfree(filter->filter_string);
281 kfree(filter);
283 kfree(system);
286 static void __get_system(struct event_subsystem *system)
288 WARN_ON_ONCE(system->ref_count == 0);
289 system->ref_count++;
292 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
294 WARN_ON_ONCE(dir->ref_count == 0);
295 dir->ref_count++;
296 __get_system(dir->subsystem);
299 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
301 WARN_ON_ONCE(dir->ref_count == 0);
302 /* If the subsystem is about to be freed, the dir must be too */
303 WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
305 __put_system(dir->subsystem);
306 if (!--dir->ref_count)
307 kfree(dir);
310 static void put_system(struct ftrace_subsystem_dir *dir)
312 mutex_lock(&event_mutex);
313 __put_system_dir(dir);
314 mutex_unlock(&event_mutex);
318 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
320 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
321 const char *sub, const char *event, int set)
323 struct ftrace_event_file *file;
324 struct ftrace_event_call *call;
325 int ret = -EINVAL;
327 mutex_lock(&event_mutex);
328 list_for_each_entry(file, &tr->events, list) {
330 call = file->event_call;
332 if (!call->name || !call->class || !call->class->reg)
333 continue;
335 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
336 continue;
338 if (match &&
339 strcmp(match, call->name) != 0 &&
340 strcmp(match, call->class->system) != 0)
341 continue;
343 if (sub && strcmp(sub, call->class->system) != 0)
344 continue;
346 if (event && strcmp(event, call->name) != 0)
347 continue;
349 ftrace_event_enable_disable(file, set);
351 ret = 0;
353 mutex_unlock(&event_mutex);
355 return ret;
358 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
360 char *event = NULL, *sub = NULL, *match;
363 * The buf format can be <subsystem>:<event-name>
364 * *:<event-name> means any event by that name.
365 * :<event-name> is the same.
367 * <subsystem>:* means all events in that subsystem
368 * <subsystem>: means the same.
370 * <name> (no ':') means all events in a subsystem with
371 * the name <name> or any event that matches <name>
374 match = strsep(&buf, ":");
375 if (buf) {
376 sub = match;
377 event = buf;
378 match = NULL;
380 if (!strlen(sub) || strcmp(sub, "*") == 0)
381 sub = NULL;
382 if (!strlen(event) || strcmp(event, "*") == 0)
383 event = NULL;
386 return __ftrace_set_clr_event(tr, match, sub, event, set);
390 * trace_set_clr_event - enable or disable an event
391 * @system: system name to match (NULL for any system)
392 * @event: event name to match (NULL for all events, within system)
393 * @set: 1 to enable, 0 to disable
395 * This is a way for other parts of the kernel to enable or disable
396 * event recording.
398 * Returns 0 on success, -EINVAL if the parameters do not match any
399 * registered events.
401 int trace_set_clr_event(const char *system, const char *event, int set)
403 struct trace_array *tr = top_trace_array();
405 return __ftrace_set_clr_event(tr, NULL, system, event, set);
407 EXPORT_SYMBOL_GPL(trace_set_clr_event);
409 /* 128 should be much more than enough */
410 #define EVENT_BUF_SIZE 127
412 static ssize_t
413 ftrace_event_write(struct file *file, const char __user *ubuf,
414 size_t cnt, loff_t *ppos)
416 struct trace_parser parser;
417 struct seq_file *m = file->private_data;
418 struct trace_array *tr = m->private;
419 ssize_t read, ret;
421 if (!cnt)
422 return 0;
424 ret = tracing_update_buffers();
425 if (ret < 0)
426 return ret;
428 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
429 return -ENOMEM;
431 read = trace_get_user(&parser, ubuf, cnt, ppos);
433 if (read >= 0 && trace_parser_loaded((&parser))) {
434 int set = 1;
436 if (*parser.buffer == '!')
437 set = 0;
439 parser.buffer[parser.idx] = 0;
441 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
442 if (ret)
443 goto out_put;
446 ret = read;
448 out_put:
449 trace_parser_put(&parser);
451 return ret;
454 static void *
455 t_next(struct seq_file *m, void *v, loff_t *pos)
457 struct ftrace_event_file *file = v;
458 struct ftrace_event_call *call;
459 struct trace_array *tr = m->private;
461 (*pos)++;
463 list_for_each_entry_continue(file, &tr->events, list) {
464 call = file->event_call;
466 * The ftrace subsystem is for showing formats only.
467 * They can not be enabled or disabled via the event files.
469 if (call->class && call->class->reg)
470 return file;
473 return NULL;
476 static void *t_start(struct seq_file *m, loff_t *pos)
478 struct ftrace_event_file *file;
479 struct trace_array *tr = m->private;
480 loff_t l;
482 mutex_lock(&event_mutex);
484 file = list_entry(&tr->events, struct ftrace_event_file, list);
485 for (l = 0; l <= *pos; ) {
486 file = t_next(m, file, &l);
487 if (!file)
488 break;
490 return file;
493 static void *
494 s_next(struct seq_file *m, void *v, loff_t *pos)
496 struct ftrace_event_file *file = v;
497 struct trace_array *tr = m->private;
499 (*pos)++;
501 list_for_each_entry_continue(file, &tr->events, list) {
502 if (file->flags & FTRACE_EVENT_FL_ENABLED)
503 return file;
506 return NULL;
509 static void *s_start(struct seq_file *m, loff_t *pos)
511 struct ftrace_event_file *file;
512 struct trace_array *tr = m->private;
513 loff_t l;
515 mutex_lock(&event_mutex);
517 file = list_entry(&tr->events, struct ftrace_event_file, list);
518 for (l = 0; l <= *pos; ) {
519 file = s_next(m, file, &l);
520 if (!file)
521 break;
523 return file;
526 static int t_show(struct seq_file *m, void *v)
528 struct ftrace_event_file *file = v;
529 struct ftrace_event_call *call = file->event_call;
531 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
532 seq_printf(m, "%s:", call->class->system);
533 seq_printf(m, "%s\n", call->name);
535 return 0;
538 static void t_stop(struct seq_file *m, void *p)
540 mutex_unlock(&event_mutex);
543 static ssize_t
544 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
545 loff_t *ppos)
547 struct ftrace_event_file *file = filp->private_data;
548 char *buf;
550 if (file->flags & FTRACE_EVENT_FL_ENABLED)
551 buf = "1\n";
552 else
553 buf = "0\n";
555 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
558 static ssize_t
559 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
560 loff_t *ppos)
562 struct ftrace_event_file *file = filp->private_data;
563 unsigned long val;
564 int ret;
566 if (!file)
567 return -EINVAL;
569 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
570 if (ret)
571 return ret;
573 ret = tracing_update_buffers();
574 if (ret < 0)
575 return ret;
577 switch (val) {
578 case 0:
579 case 1:
580 mutex_lock(&event_mutex);
581 ret = ftrace_event_enable_disable(file, val);
582 mutex_unlock(&event_mutex);
583 break;
585 default:
586 return -EINVAL;
589 *ppos += cnt;
591 return ret ? ret : cnt;
594 static ssize_t
595 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
596 loff_t *ppos)
598 const char set_to_char[4] = { '?', '0', '1', 'X' };
599 struct ftrace_subsystem_dir *dir = filp->private_data;
600 struct event_subsystem *system = dir->subsystem;
601 struct ftrace_event_call *call;
602 struct ftrace_event_file *file;
603 struct trace_array *tr = dir->tr;
604 char buf[2];
605 int set = 0;
606 int ret;
608 mutex_lock(&event_mutex);
609 list_for_each_entry(file, &tr->events, list) {
610 call = file->event_call;
611 if (!call->name || !call->class || !call->class->reg)
612 continue;
614 if (system && strcmp(call->class->system, system->name) != 0)
615 continue;
618 * We need to find out if all the events are set
619 * or if all events or cleared, or if we have
620 * a mixture.
622 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
625 * If we have a mixture, no need to look further.
627 if (set == 3)
628 break;
630 mutex_unlock(&event_mutex);
632 buf[0] = set_to_char[set];
633 buf[1] = '\n';
635 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
637 return ret;
640 static ssize_t
641 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
642 loff_t *ppos)
644 struct ftrace_subsystem_dir *dir = filp->private_data;
645 struct event_subsystem *system = dir->subsystem;
646 const char *name = NULL;
647 unsigned long val;
648 ssize_t ret;
650 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
651 if (ret)
652 return ret;
654 ret = tracing_update_buffers();
655 if (ret < 0)
656 return ret;
658 if (val != 0 && val != 1)
659 return -EINVAL;
662 * Opening of "enable" adds a ref count to system,
663 * so the name is safe to use.
665 if (system)
666 name = system->name;
668 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
669 if (ret)
670 goto out;
672 ret = cnt;
674 out:
675 *ppos += cnt;
677 return ret;
680 enum {
681 FORMAT_HEADER = 1,
682 FORMAT_FIELD_SEPERATOR = 2,
683 FORMAT_PRINTFMT = 3,
686 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
688 struct ftrace_event_call *call = m->private;
689 struct ftrace_event_field *field;
690 struct list_head *common_head = &ftrace_common_fields;
691 struct list_head *head = trace_get_fields(call);
693 (*pos)++;
695 switch ((unsigned long)v) {
696 case FORMAT_HEADER:
697 if (unlikely(list_empty(common_head)))
698 return NULL;
700 field = list_entry(common_head->prev,
701 struct ftrace_event_field, link);
702 return field;
704 case FORMAT_FIELD_SEPERATOR:
705 if (unlikely(list_empty(head)))
706 return NULL;
708 field = list_entry(head->prev, struct ftrace_event_field, link);
709 return field;
711 case FORMAT_PRINTFMT:
712 /* all done */
713 return NULL;
716 field = v;
717 if (field->link.prev == common_head)
718 return (void *)FORMAT_FIELD_SEPERATOR;
719 else if (field->link.prev == head)
720 return (void *)FORMAT_PRINTFMT;
722 field = list_entry(field->link.prev, struct ftrace_event_field, link);
724 return field;
727 static void *f_start(struct seq_file *m, loff_t *pos)
729 loff_t l = 0;
730 void *p;
732 /* Start by showing the header */
733 if (!*pos)
734 return (void *)FORMAT_HEADER;
736 p = (void *)FORMAT_HEADER;
737 do {
738 p = f_next(m, p, &l);
739 } while (p && l < *pos);
741 return p;
744 static int f_show(struct seq_file *m, void *v)
746 struct ftrace_event_call *call = m->private;
747 struct ftrace_event_field *field;
748 const char *array_descriptor;
750 switch ((unsigned long)v) {
751 case FORMAT_HEADER:
752 seq_printf(m, "name: %s\n", call->name);
753 seq_printf(m, "ID: %d\n", call->event.type);
754 seq_printf(m, "format:\n");
755 return 0;
757 case FORMAT_FIELD_SEPERATOR:
758 seq_putc(m, '\n');
759 return 0;
761 case FORMAT_PRINTFMT:
762 seq_printf(m, "\nprint fmt: %s\n",
763 call->print_fmt);
764 return 0;
767 field = v;
770 * Smartly shows the array type(except dynamic array).
771 * Normal:
772 * field:TYPE VAR
773 * If TYPE := TYPE[LEN], it is shown:
774 * field:TYPE VAR[LEN]
776 array_descriptor = strchr(field->type, '[');
778 if (!strncmp(field->type, "__data_loc", 10))
779 array_descriptor = NULL;
781 if (!array_descriptor)
782 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
783 field->type, field->name, field->offset,
784 field->size, !!field->is_signed);
785 else
786 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
787 (int)(array_descriptor - field->type),
788 field->type, field->name,
789 array_descriptor, field->offset,
790 field->size, !!field->is_signed);
792 return 0;
795 static void f_stop(struct seq_file *m, void *p)
799 static const struct seq_operations trace_format_seq_ops = {
800 .start = f_start,
801 .next = f_next,
802 .stop = f_stop,
803 .show = f_show,
806 static int trace_format_open(struct inode *inode, struct file *file)
808 struct ftrace_event_call *call = inode->i_private;
809 struct seq_file *m;
810 int ret;
812 ret = seq_open(file, &trace_format_seq_ops);
813 if (ret < 0)
814 return ret;
816 m = file->private_data;
817 m->private = call;
819 return 0;
822 static ssize_t
823 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
825 struct ftrace_event_call *call = filp->private_data;
826 struct trace_seq *s;
827 int r;
829 if (*ppos)
830 return 0;
832 s = kmalloc(sizeof(*s), GFP_KERNEL);
833 if (!s)
834 return -ENOMEM;
836 trace_seq_init(s);
837 trace_seq_printf(s, "%d\n", call->event.type);
839 r = simple_read_from_buffer(ubuf, cnt, ppos,
840 s->buffer, s->len);
841 kfree(s);
842 return r;
845 static ssize_t
846 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
847 loff_t *ppos)
849 struct ftrace_event_call *call = 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 print_event_filter(call, s);
863 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
865 kfree(s);
867 return r;
870 static ssize_t
871 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
872 loff_t *ppos)
874 struct ftrace_event_call *call = filp->private_data;
875 char *buf;
876 int err;
878 if (cnt >= PAGE_SIZE)
879 return -EINVAL;
881 buf = (char *)__get_free_page(GFP_TEMPORARY);
882 if (!buf)
883 return -ENOMEM;
885 if (copy_from_user(buf, ubuf, cnt)) {
886 free_page((unsigned long) buf);
887 return -EFAULT;
889 buf[cnt] = '\0';
891 err = apply_event_filter(call, buf);
892 free_page((unsigned long) buf);
893 if (err < 0)
894 return err;
896 *ppos += cnt;
898 return cnt;
901 static LIST_HEAD(event_subsystems);
903 static int subsystem_open(struct inode *inode, struct file *filp)
905 struct event_subsystem *system = NULL;
906 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
907 struct trace_array *tr;
908 int ret;
910 /* Make sure the system still exists */
911 mutex_lock(&event_mutex);
912 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
913 list_for_each_entry(dir, &tr->systems, list) {
914 if (dir == inode->i_private) {
915 /* Don't open systems with no events */
916 if (dir->nr_events) {
917 __get_system_dir(dir);
918 system = dir->subsystem;
920 goto exit_loop;
924 exit_loop:
925 mutex_unlock(&event_mutex);
927 if (!system)
928 return -ENODEV;
930 /* Some versions of gcc think dir can be uninitialized here */
931 WARN_ON(!dir);
933 ret = tracing_open_generic(inode, filp);
934 if (ret < 0)
935 put_system(dir);
937 return ret;
940 static int system_tr_open(struct inode *inode, struct file *filp)
942 struct ftrace_subsystem_dir *dir;
943 struct trace_array *tr = inode->i_private;
944 int ret;
946 /* Make a temporary dir that has no system but points to tr */
947 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
948 if (!dir)
949 return -ENOMEM;
951 dir->tr = tr;
953 ret = tracing_open_generic(inode, filp);
954 if (ret < 0)
955 kfree(dir);
957 filp->private_data = dir;
959 return ret;
962 static int subsystem_release(struct inode *inode, struct file *file)
964 struct ftrace_subsystem_dir *dir = file->private_data;
967 * If dir->subsystem is NULL, then this is a temporary
968 * descriptor that was made for a trace_array to enable
969 * all subsystems.
971 if (dir->subsystem)
972 put_system(dir);
973 else
974 kfree(dir);
976 return 0;
979 static ssize_t
980 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
981 loff_t *ppos)
983 struct ftrace_subsystem_dir *dir = filp->private_data;
984 struct event_subsystem *system = dir->subsystem;
985 struct trace_seq *s;
986 int r;
988 if (*ppos)
989 return 0;
991 s = kmalloc(sizeof(*s), GFP_KERNEL);
992 if (!s)
993 return -ENOMEM;
995 trace_seq_init(s);
997 print_subsystem_event_filter(system, s);
998 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1000 kfree(s);
1002 return r;
1005 static ssize_t
1006 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1007 loff_t *ppos)
1009 struct ftrace_subsystem_dir *dir = filp->private_data;
1010 char *buf;
1011 int err;
1013 if (cnt >= PAGE_SIZE)
1014 return -EINVAL;
1016 buf = (char *)__get_free_page(GFP_TEMPORARY);
1017 if (!buf)
1018 return -ENOMEM;
1020 if (copy_from_user(buf, ubuf, cnt)) {
1021 free_page((unsigned long) buf);
1022 return -EFAULT;
1024 buf[cnt] = '\0';
1026 err = apply_subsystem_event_filter(dir, buf);
1027 free_page((unsigned long) buf);
1028 if (err < 0)
1029 return err;
1031 *ppos += cnt;
1033 return cnt;
1036 static ssize_t
1037 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1039 int (*func)(struct trace_seq *s) = filp->private_data;
1040 struct trace_seq *s;
1041 int r;
1043 if (*ppos)
1044 return 0;
1046 s = kmalloc(sizeof(*s), GFP_KERNEL);
1047 if (!s)
1048 return -ENOMEM;
1050 trace_seq_init(s);
1052 func(s);
1053 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1055 kfree(s);
1057 return r;
1060 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1061 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1063 static const struct seq_operations show_event_seq_ops = {
1064 .start = t_start,
1065 .next = t_next,
1066 .show = t_show,
1067 .stop = t_stop,
1070 static const struct seq_operations show_set_event_seq_ops = {
1071 .start = s_start,
1072 .next = s_next,
1073 .show = t_show,
1074 .stop = t_stop,
1077 static const struct file_operations ftrace_avail_fops = {
1078 .open = ftrace_event_avail_open,
1079 .read = seq_read,
1080 .llseek = seq_lseek,
1081 .release = seq_release,
1084 static const struct file_operations ftrace_set_event_fops = {
1085 .open = ftrace_event_set_open,
1086 .read = seq_read,
1087 .write = ftrace_event_write,
1088 .llseek = seq_lseek,
1089 .release = seq_release,
1092 static const struct file_operations ftrace_enable_fops = {
1093 .open = tracing_open_generic,
1094 .read = event_enable_read,
1095 .write = event_enable_write,
1096 .llseek = default_llseek,
1099 static const struct file_operations ftrace_event_format_fops = {
1100 .open = trace_format_open,
1101 .read = seq_read,
1102 .llseek = seq_lseek,
1103 .release = seq_release,
1106 static const struct file_operations ftrace_event_id_fops = {
1107 .open = tracing_open_generic,
1108 .read = event_id_read,
1109 .llseek = default_llseek,
1112 static const struct file_operations ftrace_event_filter_fops = {
1113 .open = tracing_open_generic,
1114 .read = event_filter_read,
1115 .write = event_filter_write,
1116 .llseek = default_llseek,
1119 static const struct file_operations ftrace_subsystem_filter_fops = {
1120 .open = subsystem_open,
1121 .read = subsystem_filter_read,
1122 .write = subsystem_filter_write,
1123 .llseek = default_llseek,
1124 .release = subsystem_release,
1127 static const struct file_operations ftrace_system_enable_fops = {
1128 .open = subsystem_open,
1129 .read = system_enable_read,
1130 .write = system_enable_write,
1131 .llseek = default_llseek,
1132 .release = subsystem_release,
1135 static const struct file_operations ftrace_tr_enable_fops = {
1136 .open = system_tr_open,
1137 .read = system_enable_read,
1138 .write = system_enable_write,
1139 .llseek = default_llseek,
1140 .release = subsystem_release,
1143 static const struct file_operations ftrace_show_header_fops = {
1144 .open = tracing_open_generic,
1145 .read = show_header,
1146 .llseek = default_llseek,
1149 static int
1150 ftrace_event_open(struct inode *inode, struct file *file,
1151 const struct seq_operations *seq_ops)
1153 struct seq_file *m;
1154 int ret;
1156 ret = seq_open(file, seq_ops);
1157 if (ret < 0)
1158 return ret;
1159 m = file->private_data;
1160 /* copy tr over to seq ops */
1161 m->private = inode->i_private;
1163 return ret;
1166 static int
1167 ftrace_event_avail_open(struct inode *inode, struct file *file)
1169 const struct seq_operations *seq_ops = &show_event_seq_ops;
1171 return ftrace_event_open(inode, file, seq_ops);
1174 static int
1175 ftrace_event_set_open(struct inode *inode, struct file *file)
1177 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1178 struct trace_array *tr = inode->i_private;
1180 if ((file->f_mode & FMODE_WRITE) &&
1181 (file->f_flags & O_TRUNC))
1182 ftrace_clear_events(tr);
1184 return ftrace_event_open(inode, file, seq_ops);
1187 static struct event_subsystem *
1188 create_new_subsystem(const char *name)
1190 struct event_subsystem *system;
1192 /* need to create new entry */
1193 system = kmalloc(sizeof(*system), GFP_KERNEL);
1194 if (!system)
1195 return NULL;
1197 system->ref_count = 1;
1198 system->name = name;
1200 system->filter = NULL;
1202 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1203 if (!system->filter)
1204 goto out_free;
1206 list_add(&system->list, &event_subsystems);
1208 return system;
1210 out_free:
1211 kfree(system);
1212 return NULL;
1215 static struct dentry *
1216 event_subsystem_dir(struct trace_array *tr, const char *name,
1217 struct ftrace_event_file *file, struct dentry *parent)
1219 struct ftrace_subsystem_dir *dir;
1220 struct event_subsystem *system;
1221 struct dentry *entry;
1223 /* First see if we did not already create this dir */
1224 list_for_each_entry(dir, &tr->systems, list) {
1225 system = dir->subsystem;
1226 if (strcmp(system->name, name) == 0) {
1227 dir->nr_events++;
1228 file->system = dir;
1229 return dir->entry;
1233 /* Now see if the system itself exists. */
1234 list_for_each_entry(system, &event_subsystems, list) {
1235 if (strcmp(system->name, name) == 0)
1236 break;
1238 /* Reset system variable when not found */
1239 if (&system->list == &event_subsystems)
1240 system = NULL;
1242 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1243 if (!dir)
1244 goto out_fail;
1246 if (!system) {
1247 system = create_new_subsystem(name);
1248 if (!system)
1249 goto out_free;
1250 } else
1251 __get_system(system);
1253 dir->entry = debugfs_create_dir(name, parent);
1254 if (!dir->entry) {
1255 pr_warning("Failed to create system directory %s\n", name);
1256 __put_system(system);
1257 goto out_free;
1260 dir->tr = tr;
1261 dir->ref_count = 1;
1262 dir->nr_events = 1;
1263 dir->subsystem = system;
1264 file->system = dir;
1266 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1267 &ftrace_subsystem_filter_fops);
1268 if (!entry) {
1269 kfree(system->filter);
1270 system->filter = NULL;
1271 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1274 trace_create_file("enable", 0644, dir->entry, dir,
1275 &ftrace_system_enable_fops);
1277 list_add(&dir->list, &tr->systems);
1279 return dir->entry;
1281 out_free:
1282 kfree(dir);
1283 out_fail:
1284 /* Only print this message if failed on memory allocation */
1285 if (!dir || !system)
1286 pr_warning("No memory to create event subsystem %s\n",
1287 name);
1288 return NULL;
1291 static int
1292 event_create_dir(struct dentry *parent,
1293 struct ftrace_event_file *file,
1294 const struct file_operations *id,
1295 const struct file_operations *enable,
1296 const struct file_operations *filter,
1297 const struct file_operations *format)
1299 struct ftrace_event_call *call = file->event_call;
1300 struct trace_array *tr = file->tr;
1301 struct list_head *head;
1302 struct dentry *d_events;
1303 int ret;
1306 * If the trace point header did not define TRACE_SYSTEM
1307 * then the system would be called "TRACE_SYSTEM".
1309 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1310 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1311 if (!d_events)
1312 return -ENOMEM;
1313 } else
1314 d_events = parent;
1316 file->dir = debugfs_create_dir(call->name, d_events);
1317 if (!file->dir) {
1318 pr_warning("Could not create debugfs '%s' directory\n",
1319 call->name);
1320 return -1;
1323 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1324 trace_create_file("enable", 0644, file->dir, file,
1325 enable);
1327 #ifdef CONFIG_PERF_EVENTS
1328 if (call->event.type && call->class->reg)
1329 trace_create_file("id", 0444, file->dir, call,
1330 id);
1331 #endif
1334 * Other events may have the same class. Only update
1335 * the fields if they are not already defined.
1337 head = trace_get_fields(call);
1338 if (list_empty(head)) {
1339 ret = call->class->define_fields(call);
1340 if (ret < 0) {
1341 pr_warning("Could not initialize trace point"
1342 " events/%s\n", call->name);
1343 return -1;
1346 trace_create_file("filter", 0644, file->dir, call,
1347 filter);
1349 trace_create_file("format", 0444, file->dir, call,
1350 format);
1352 return 0;
1355 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1357 if (!dir)
1358 return;
1360 if (!--dir->nr_events) {
1361 debugfs_remove_recursive(dir->entry);
1362 list_del(&dir->list);
1363 __put_system_dir(dir);
1367 static void remove_event_from_tracers(struct ftrace_event_call *call)
1369 struct ftrace_event_file *file;
1370 struct trace_array *tr;
1372 do_for_each_event_file_safe(tr, file) {
1374 if (file->event_call != call)
1375 continue;
1377 list_del(&file->list);
1378 debugfs_remove_recursive(file->dir);
1379 remove_subsystem(file->system);
1380 kmem_cache_free(file_cachep, file);
1383 * The do_for_each_event_file_safe() is
1384 * a double loop. After finding the call for this
1385 * trace_array, we use break to jump to the next
1386 * trace_array.
1388 break;
1389 } while_for_each_event_file();
1392 static void event_remove(struct ftrace_event_call *call)
1394 struct trace_array *tr;
1395 struct ftrace_event_file *file;
1397 do_for_each_event_file(tr, file) {
1398 if (file->event_call != call)
1399 continue;
1400 ftrace_event_enable_disable(file, 0);
1402 * The do_for_each_event_file() is
1403 * a double loop. After finding the call for this
1404 * trace_array, we use break to jump to the next
1405 * trace_array.
1407 break;
1408 } while_for_each_event_file();
1410 if (call->event.funcs)
1411 __unregister_ftrace_event(&call->event);
1412 remove_event_from_tracers(call);
1413 list_del(&call->list);
1416 static int event_init(struct ftrace_event_call *call)
1418 int ret = 0;
1420 if (WARN_ON(!call->name))
1421 return -EINVAL;
1423 if (call->class->raw_init) {
1424 ret = call->class->raw_init(call);
1425 if (ret < 0 && ret != -ENOSYS)
1426 pr_warn("Could not initialize trace events/%s\n",
1427 call->name);
1430 return ret;
1433 static int
1434 __register_event(struct ftrace_event_call *call, struct module *mod)
1436 int ret;
1438 ret = event_init(call);
1439 if (ret < 0)
1440 return ret;
1442 list_add(&call->list, &ftrace_events);
1443 call->mod = mod;
1445 return 0;
1448 /* Add an event to a trace directory */
1449 static int
1450 __trace_add_new_event(struct ftrace_event_call *call,
1451 struct trace_array *tr,
1452 const struct file_operations *id,
1453 const struct file_operations *enable,
1454 const struct file_operations *filter,
1455 const struct file_operations *format)
1457 struct ftrace_event_file *file;
1459 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1460 if (!file)
1461 return -ENOMEM;
1463 file->event_call = call;
1464 file->tr = tr;
1465 list_add(&file->list, &tr->events);
1467 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1471 * Just create a decriptor for early init. A descriptor is required
1472 * for enabling events at boot. We want to enable events before
1473 * the filesystem is initialized.
1475 static __init int
1476 __trace_early_add_new_event(struct ftrace_event_call *call,
1477 struct trace_array *tr)
1479 struct ftrace_event_file *file;
1481 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1482 if (!file)
1483 return -ENOMEM;
1485 file->event_call = call;
1486 file->tr = tr;
1487 list_add(&file->list, &tr->events);
1489 return 0;
1492 struct ftrace_module_file_ops;
1493 static void __add_event_to_tracers(struct ftrace_event_call *call,
1494 struct ftrace_module_file_ops *file_ops);
1496 /* Add an additional event_call dynamically */
1497 int trace_add_event_call(struct ftrace_event_call *call)
1499 int ret;
1500 mutex_lock(&event_mutex);
1502 ret = __register_event(call, NULL);
1503 if (ret >= 0)
1504 __add_event_to_tracers(call, NULL);
1506 mutex_unlock(&event_mutex);
1507 return ret;
1511 * Must be called under locking both of event_mutex and trace_event_mutex.
1513 static void __trace_remove_event_call(struct ftrace_event_call *call)
1515 event_remove(call);
1516 trace_destroy_fields(call);
1517 destroy_preds(call);
1520 /* Remove an event_call */
1521 void trace_remove_event_call(struct ftrace_event_call *call)
1523 mutex_lock(&event_mutex);
1524 down_write(&trace_event_mutex);
1525 __trace_remove_event_call(call);
1526 up_write(&trace_event_mutex);
1527 mutex_unlock(&event_mutex);
1530 #define for_each_event(event, start, end) \
1531 for (event = start; \
1532 (unsigned long)event < (unsigned long)end; \
1533 event++)
1535 #ifdef CONFIG_MODULES
1537 static LIST_HEAD(ftrace_module_file_list);
1540 * Modules must own their file_operations to keep up with
1541 * reference counting.
1543 struct ftrace_module_file_ops {
1544 struct list_head list;
1545 struct module *mod;
1546 struct file_operations id;
1547 struct file_operations enable;
1548 struct file_operations format;
1549 struct file_operations filter;
1552 static struct ftrace_module_file_ops *
1553 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1556 * As event_calls are added in groups by module,
1557 * when we find one file_ops, we don't need to search for
1558 * each call in that module, as the rest should be the
1559 * same. Only search for a new one if the last one did
1560 * not match.
1562 if (file_ops && mod == file_ops->mod)
1563 return file_ops;
1565 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1566 if (file_ops->mod == mod)
1567 return file_ops;
1569 return NULL;
1572 static struct ftrace_module_file_ops *
1573 trace_create_file_ops(struct module *mod)
1575 struct ftrace_module_file_ops *file_ops;
1578 * This is a bit of a PITA. To allow for correct reference
1579 * counting, modules must "own" their file_operations.
1580 * To do this, we allocate the file operations that will be
1581 * used in the event directory.
1584 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1585 if (!file_ops)
1586 return NULL;
1588 file_ops->mod = mod;
1590 file_ops->id = ftrace_event_id_fops;
1591 file_ops->id.owner = mod;
1593 file_ops->enable = ftrace_enable_fops;
1594 file_ops->enable.owner = mod;
1596 file_ops->filter = ftrace_event_filter_fops;
1597 file_ops->filter.owner = mod;
1599 file_ops->format = ftrace_event_format_fops;
1600 file_ops->format.owner = mod;
1602 list_add(&file_ops->list, &ftrace_module_file_list);
1604 return file_ops;
1607 static void trace_module_add_events(struct module *mod)
1609 struct ftrace_module_file_ops *file_ops = NULL;
1610 struct ftrace_event_call **call, **start, **end;
1612 start = mod->trace_events;
1613 end = mod->trace_events + mod->num_trace_events;
1615 if (start == end)
1616 return;
1618 file_ops = trace_create_file_ops(mod);
1619 if (!file_ops)
1620 return;
1622 for_each_event(call, start, end) {
1623 __register_event(*call, mod);
1624 __add_event_to_tracers(*call, file_ops);
1628 static void trace_module_remove_events(struct module *mod)
1630 struct ftrace_module_file_ops *file_ops;
1631 struct ftrace_event_call *call, *p;
1632 bool clear_trace = false;
1634 down_write(&trace_event_mutex);
1635 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1636 if (call->mod == mod) {
1637 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1638 clear_trace = true;
1639 __trace_remove_event_call(call);
1643 /* Now free the file_operations */
1644 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1645 if (file_ops->mod == mod)
1646 break;
1648 if (&file_ops->list != &ftrace_module_file_list) {
1649 list_del(&file_ops->list);
1650 kfree(file_ops);
1652 up_write(&trace_event_mutex);
1655 * It is safest to reset the ring buffer if the module being unloaded
1656 * registered any events that were used. The only worry is if
1657 * a new module gets loaded, and takes on the same id as the events
1658 * of this module. When printing out the buffer, traced events left
1659 * over from this module may be passed to the new module events and
1660 * unexpected results may occur.
1662 if (clear_trace)
1663 tracing_reset_all_online_cpus();
1666 static int trace_module_notify(struct notifier_block *self,
1667 unsigned long val, void *data)
1669 struct module *mod = data;
1671 mutex_lock(&event_mutex);
1672 switch (val) {
1673 case MODULE_STATE_COMING:
1674 trace_module_add_events(mod);
1675 break;
1676 case MODULE_STATE_GOING:
1677 trace_module_remove_events(mod);
1678 break;
1680 mutex_unlock(&event_mutex);
1682 return 0;
1685 static int
1686 __trace_add_new_mod_event(struct ftrace_event_call *call,
1687 struct trace_array *tr,
1688 struct ftrace_module_file_ops *file_ops)
1690 return __trace_add_new_event(call, tr,
1691 &file_ops->id, &file_ops->enable,
1692 &file_ops->filter, &file_ops->format);
1695 #else
1696 static inline struct ftrace_module_file_ops *
1697 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1699 return NULL;
1701 static inline int trace_module_notify(struct notifier_block *self,
1702 unsigned long val, void *data)
1704 return 0;
1706 static inline int
1707 __trace_add_new_mod_event(struct ftrace_event_call *call,
1708 struct trace_array *tr,
1709 struct ftrace_module_file_ops *file_ops)
1711 return -ENODEV;
1713 #endif /* CONFIG_MODULES */
1715 /* Create a new event directory structure for a trace directory. */
1716 static void
1717 __trace_add_event_dirs(struct trace_array *tr)
1719 struct ftrace_module_file_ops *file_ops = NULL;
1720 struct ftrace_event_call *call;
1721 int ret;
1723 list_for_each_entry(call, &ftrace_events, list) {
1724 if (call->mod) {
1726 * Directories for events by modules need to
1727 * keep module ref counts when opened (as we don't
1728 * want the module to disappear when reading one
1729 * of these files). The file_ops keep account of
1730 * the module ref count.
1732 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1733 if (!file_ops)
1734 continue; /* Warn? */
1735 ret = __trace_add_new_mod_event(call, tr, file_ops);
1736 if (ret < 0)
1737 pr_warning("Could not create directory for event %s\n",
1738 call->name);
1739 continue;
1741 ret = __trace_add_new_event(call, tr,
1742 &ftrace_event_id_fops,
1743 &ftrace_enable_fops,
1744 &ftrace_event_filter_fops,
1745 &ftrace_event_format_fops);
1746 if (ret < 0)
1747 pr_warning("Could not create directory for event %s\n",
1748 call->name);
1753 * The top level array has already had its ftrace_event_file
1754 * descriptors created in order to allow for early events to
1755 * be recorded. This function is called after the debugfs has been
1756 * initialized, and we now have to create the files associated
1757 * to the events.
1759 static __init void
1760 __trace_early_add_event_dirs(struct trace_array *tr)
1762 struct ftrace_event_file *file;
1763 int ret;
1766 list_for_each_entry(file, &tr->events, list) {
1767 ret = event_create_dir(tr->event_dir, file,
1768 &ftrace_event_id_fops,
1769 &ftrace_enable_fops,
1770 &ftrace_event_filter_fops,
1771 &ftrace_event_format_fops);
1772 if (ret < 0)
1773 pr_warning("Could not create directory for event %s\n",
1774 file->event_call->name);
1779 * For early boot up, the top trace array requires to have
1780 * a list of events that can be enabled. This must be done before
1781 * the filesystem is set up in order to allow events to be traced
1782 * early.
1784 static __init void
1785 __trace_early_add_events(struct trace_array *tr)
1787 struct ftrace_event_call *call;
1788 int ret;
1790 list_for_each_entry(call, &ftrace_events, list) {
1791 /* Early boot up should not have any modules loaded */
1792 if (WARN_ON_ONCE(call->mod))
1793 continue;
1795 ret = __trace_early_add_new_event(call, tr);
1796 if (ret < 0)
1797 pr_warning("Could not create early event %s\n",
1798 call->name);
1802 /* Remove the event directory structure for a trace directory. */
1803 static void
1804 __trace_remove_event_dirs(struct trace_array *tr)
1806 struct ftrace_event_file *file, *next;
1808 list_for_each_entry_safe(file, next, &tr->events, list) {
1809 list_del(&file->list);
1810 debugfs_remove_recursive(file->dir);
1811 remove_subsystem(file->system);
1812 kmem_cache_free(file_cachep, file);
1816 static void
1817 __add_event_to_tracers(struct ftrace_event_call *call,
1818 struct ftrace_module_file_ops *file_ops)
1820 struct trace_array *tr;
1822 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1823 if (file_ops)
1824 __trace_add_new_mod_event(call, tr, file_ops);
1825 else
1826 __trace_add_new_event(call, tr,
1827 &ftrace_event_id_fops,
1828 &ftrace_enable_fops,
1829 &ftrace_event_filter_fops,
1830 &ftrace_event_format_fops);
1834 static struct notifier_block trace_module_nb = {
1835 .notifier_call = trace_module_notify,
1836 .priority = 0,
1839 extern struct ftrace_event_call *__start_ftrace_events[];
1840 extern struct ftrace_event_call *__stop_ftrace_events[];
1842 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1844 static __init int setup_trace_event(char *str)
1846 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1847 ring_buffer_expanded = true;
1848 tracing_selftest_disabled = true;
1850 return 1;
1852 __setup("trace_event=", setup_trace_event);
1854 /* Expects to have event_mutex held when called */
1855 static int
1856 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
1858 struct dentry *d_events;
1859 struct dentry *entry;
1861 entry = debugfs_create_file("set_event", 0644, parent,
1862 tr, &ftrace_set_event_fops);
1863 if (!entry) {
1864 pr_warning("Could not create debugfs 'set_event' entry\n");
1865 return -ENOMEM;
1868 d_events = debugfs_create_dir("events", parent);
1869 if (!d_events) {
1870 pr_warning("Could not create debugfs 'events' directory\n");
1871 return -ENOMEM;
1874 /* ring buffer internal formats */
1875 trace_create_file("header_page", 0444, d_events,
1876 ring_buffer_print_page_header,
1877 &ftrace_show_header_fops);
1879 trace_create_file("header_event", 0444, d_events,
1880 ring_buffer_print_entry_header,
1881 &ftrace_show_header_fops);
1883 trace_create_file("enable", 0644, d_events,
1884 tr, &ftrace_tr_enable_fops);
1886 tr->event_dir = d_events;
1888 return 0;
1892 * event_trace_add_tracer - add a instance of a trace_array to events
1893 * @parent: The parent dentry to place the files/directories for events in
1894 * @tr: The trace array associated with these events
1896 * When a new instance is created, it needs to set up its events
1897 * directory, as well as other files associated with events. It also
1898 * creates the event hierachry in the @parent/events directory.
1900 * Returns 0 on success.
1902 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
1904 int ret;
1906 mutex_lock(&event_mutex);
1908 ret = create_event_toplevel_files(parent, tr);
1909 if (ret)
1910 goto out_unlock;
1912 down_write(&trace_event_mutex);
1913 __trace_add_event_dirs(tr);
1914 up_write(&trace_event_mutex);
1916 out_unlock:
1917 mutex_unlock(&event_mutex);
1919 return ret;
1923 * The top trace array already had its file descriptors created.
1924 * Now the files themselves need to be created.
1926 static __init int
1927 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
1929 int ret;
1931 mutex_lock(&event_mutex);
1933 ret = create_event_toplevel_files(parent, tr);
1934 if (ret)
1935 goto out_unlock;
1937 down_write(&trace_event_mutex);
1938 __trace_early_add_event_dirs(tr);
1939 up_write(&trace_event_mutex);
1941 out_unlock:
1942 mutex_unlock(&event_mutex);
1944 return ret;
1947 int event_trace_del_tracer(struct trace_array *tr)
1949 /* Disable any running events */
1950 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
1952 mutex_lock(&event_mutex);
1954 down_write(&trace_event_mutex);
1955 __trace_remove_event_dirs(tr);
1956 debugfs_remove_recursive(tr->event_dir);
1957 up_write(&trace_event_mutex);
1959 tr->event_dir = NULL;
1961 mutex_unlock(&event_mutex);
1963 return 0;
1966 static __init int event_trace_memsetup(void)
1968 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
1969 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
1970 return 0;
1973 static __init int event_trace_enable(void)
1975 struct trace_array *tr = top_trace_array();
1976 struct ftrace_event_call **iter, *call;
1977 char *buf = bootup_event_buf;
1978 char *token;
1979 int ret;
1981 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
1983 call = *iter;
1984 ret = event_init(call);
1985 if (!ret)
1986 list_add(&call->list, &ftrace_events);
1990 * We need the top trace array to have a working set of trace
1991 * points at early init, before the debug files and directories
1992 * are created. Create the file entries now, and attach them
1993 * to the actual file dentries later.
1995 __trace_early_add_events(tr);
1997 while (true) {
1998 token = strsep(&buf, ",");
2000 if (!token)
2001 break;
2002 if (!*token)
2003 continue;
2005 ret = ftrace_set_clr_event(tr, token, 1);
2006 if (ret)
2007 pr_warn("Failed to enable trace event: %s\n", token);
2010 trace_printk_start_comm();
2012 return 0;
2015 static __init int event_trace_init(void)
2017 struct trace_array *tr;
2018 struct dentry *d_tracer;
2019 struct dentry *entry;
2020 int ret;
2022 tr = top_trace_array();
2024 d_tracer = tracing_init_dentry();
2025 if (!d_tracer)
2026 return 0;
2028 entry = debugfs_create_file("available_events", 0444, d_tracer,
2029 tr, &ftrace_avail_fops);
2030 if (!entry)
2031 pr_warning("Could not create debugfs "
2032 "'available_events' entry\n");
2034 if (trace_define_common_fields())
2035 pr_warning("tracing: Failed to allocate common fields");
2037 ret = early_event_add_tracer(d_tracer, tr);
2038 if (ret)
2039 return ret;
2041 ret = register_module_notifier(&trace_module_nb);
2042 if (ret)
2043 pr_warning("Failed to register trace events module notifier\n");
2045 return 0;
2047 early_initcall(event_trace_memsetup);
2048 core_initcall(event_trace_enable);
2049 fs_initcall(event_trace_init);
2051 #ifdef CONFIG_FTRACE_STARTUP_TEST
2053 static DEFINE_SPINLOCK(test_spinlock);
2054 static DEFINE_SPINLOCK(test_spinlock_irq);
2055 static DEFINE_MUTEX(test_mutex);
2057 static __init void test_work(struct work_struct *dummy)
2059 spin_lock(&test_spinlock);
2060 spin_lock_irq(&test_spinlock_irq);
2061 udelay(1);
2062 spin_unlock_irq(&test_spinlock_irq);
2063 spin_unlock(&test_spinlock);
2065 mutex_lock(&test_mutex);
2066 msleep(1);
2067 mutex_unlock(&test_mutex);
2070 static __init int event_test_thread(void *unused)
2072 void *test_malloc;
2074 test_malloc = kmalloc(1234, GFP_KERNEL);
2075 if (!test_malloc)
2076 pr_info("failed to kmalloc\n");
2078 schedule_on_each_cpu(test_work);
2080 kfree(test_malloc);
2082 set_current_state(TASK_INTERRUPTIBLE);
2083 while (!kthread_should_stop())
2084 schedule();
2086 return 0;
2090 * Do various things that may trigger events.
2092 static __init void event_test_stuff(void)
2094 struct task_struct *test_thread;
2096 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2097 msleep(1);
2098 kthread_stop(test_thread);
2102 * For every trace event defined, we will test each trace point separately,
2103 * and then by groups, and finally all trace points.
2105 static __init void event_trace_self_tests(void)
2107 struct ftrace_subsystem_dir *dir;
2108 struct ftrace_event_file *file;
2109 struct ftrace_event_call *call;
2110 struct event_subsystem *system;
2111 struct trace_array *tr;
2112 int ret;
2114 tr = top_trace_array();
2116 pr_info("Running tests on trace events:\n");
2118 list_for_each_entry(file, &tr->events, list) {
2120 call = file->event_call;
2122 /* Only test those that have a probe */
2123 if (!call->class || !call->class->probe)
2124 continue;
2127 * Testing syscall events here is pretty useless, but
2128 * we still do it if configured. But this is time consuming.
2129 * What we really need is a user thread to perform the
2130 * syscalls as we test.
2132 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2133 if (call->class->system &&
2134 strcmp(call->class->system, "syscalls") == 0)
2135 continue;
2136 #endif
2138 pr_info("Testing event %s: ", call->name);
2141 * If an event is already enabled, someone is using
2142 * it and the self test should not be on.
2144 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2145 pr_warning("Enabled event during self test!\n");
2146 WARN_ON_ONCE(1);
2147 continue;
2150 ftrace_event_enable_disable(file, 1);
2151 event_test_stuff();
2152 ftrace_event_enable_disable(file, 0);
2154 pr_cont("OK\n");
2157 /* Now test at the sub system level */
2159 pr_info("Running tests on trace event systems:\n");
2161 list_for_each_entry(dir, &tr->systems, list) {
2163 system = dir->subsystem;
2165 /* the ftrace system is special, skip it */
2166 if (strcmp(system->name, "ftrace") == 0)
2167 continue;
2169 pr_info("Testing event system %s: ", system->name);
2171 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2172 if (WARN_ON_ONCE(ret)) {
2173 pr_warning("error enabling system %s\n",
2174 system->name);
2175 continue;
2178 event_test_stuff();
2180 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2181 if (WARN_ON_ONCE(ret)) {
2182 pr_warning("error disabling system %s\n",
2183 system->name);
2184 continue;
2187 pr_cont("OK\n");
2190 /* Test with all events enabled */
2192 pr_info("Running tests on all trace events:\n");
2193 pr_info("Testing all events: ");
2195 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2196 if (WARN_ON_ONCE(ret)) {
2197 pr_warning("error enabling all events\n");
2198 return;
2201 event_test_stuff();
2203 /* reset sysname */
2204 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2205 if (WARN_ON_ONCE(ret)) {
2206 pr_warning("error disabling all events\n");
2207 return;
2210 pr_cont("OK\n");
2213 #ifdef CONFIG_FUNCTION_TRACER
2215 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2217 static void
2218 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2219 struct ftrace_ops *op, struct pt_regs *pt_regs)
2221 struct ring_buffer_event *event;
2222 struct ring_buffer *buffer;
2223 struct ftrace_entry *entry;
2224 unsigned long flags;
2225 long disabled;
2226 int cpu;
2227 int pc;
2229 pc = preempt_count();
2230 preempt_disable_notrace();
2231 cpu = raw_smp_processor_id();
2232 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2234 if (disabled != 1)
2235 goto out;
2237 local_save_flags(flags);
2239 event = trace_current_buffer_lock_reserve(&buffer,
2240 TRACE_FN, sizeof(*entry),
2241 flags, pc);
2242 if (!event)
2243 goto out;
2244 entry = ring_buffer_event_data(event);
2245 entry->ip = ip;
2246 entry->parent_ip = parent_ip;
2248 trace_buffer_unlock_commit(buffer, event, flags, pc);
2250 out:
2251 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2252 preempt_enable_notrace();
2255 static struct ftrace_ops trace_ops __initdata =
2257 .func = function_test_events_call,
2258 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2261 static __init void event_trace_self_test_with_function(void)
2263 int ret;
2264 ret = register_ftrace_function(&trace_ops);
2265 if (WARN_ON(ret < 0)) {
2266 pr_info("Failed to enable function tracer for event tests\n");
2267 return;
2269 pr_info("Running tests again, along with the function tracer\n");
2270 event_trace_self_tests();
2271 unregister_ftrace_function(&trace_ops);
2273 #else
2274 static __init void event_trace_self_test_with_function(void)
2277 #endif
2279 static __init int event_trace_self_tests_init(void)
2281 if (!tracing_selftest_disabled) {
2282 event_trace_self_tests();
2283 event_trace_self_test_with_function();
2286 return 0;
2289 late_initcall(event_trace_self_tests_init);
2291 #endif