Merge commit 'tracing/core' into tracing/kprobes
[pandora-kernel.git] / kernel / trace / trace_events.c
1 /*
2  * event tracer
3  *
4  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5  *
6  *  - Added format output of fields of the trace point.
7  *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8  *
9  */
10
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/delay.h>
19
20 #include <asm/setup.h>
21
22 #include "trace_output.h"
23
24 #define TRACE_SYSTEM "TRACE_SYSTEM"
25
26 DEFINE_MUTEX(event_mutex);
27
28 LIST_HEAD(ftrace_events);
29
30 int trace_define_field(struct ftrace_event_call *call, const char *type,
31                        const char *name, int offset, int size, int is_signed,
32                        int filter_type)
33 {
34         struct ftrace_event_field *field;
35
36         field = kzalloc(sizeof(*field), GFP_KERNEL);
37         if (!field)
38                 goto err;
39
40         field->name = kstrdup(name, GFP_KERNEL);
41         if (!field->name)
42                 goto err;
43
44         field->type = kstrdup(type, GFP_KERNEL);
45         if (!field->type)
46                 goto err;
47
48         if (filter_type == FILTER_OTHER)
49                 field->filter_type = filter_assign_type(type);
50         else
51                 field->filter_type = filter_type;
52
53         field->offset = offset;
54         field->size = size;
55         field->is_signed = is_signed;
56
57         list_add(&field->link, &call->fields);
58
59         return 0;
60
61 err:
62         if (field) {
63                 kfree(field->name);
64                 kfree(field->type);
65         }
66         kfree(field);
67
68         return -ENOMEM;
69 }
70 EXPORT_SYMBOL_GPL(trace_define_field);
71
72 #define __common_field(type, item)                                      \
73         ret = trace_define_field(call, #type, "common_" #item,          \
74                                  offsetof(typeof(ent), item),           \
75                                  sizeof(ent.item),                      \
76                                  is_signed_type(type), FILTER_OTHER);   \
77         if (ret)                                                        \
78                 return ret;
79
80 int trace_define_common_fields(struct ftrace_event_call *call)
81 {
82         int ret;
83         struct trace_entry ent;
84
85         __common_field(unsigned short, type);
86         __common_field(unsigned char, flags);
87         __common_field(unsigned char, preempt_count);
88         __common_field(int, pid);
89         __common_field(int, tgid);
90
91         return ret;
92 }
93 EXPORT_SYMBOL_GPL(trace_define_common_fields);
94
95 void trace_destroy_fields(struct ftrace_event_call *call)
96 {
97         struct ftrace_event_field *field, *next;
98
99         list_for_each_entry_safe(field, next, &call->fields, link) {
100                 list_del(&field->link);
101                 kfree(field->type);
102                 kfree(field->name);
103                 kfree(field);
104         }
105 }
106
107 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
108                                         int enable)
109 {
110         switch (enable) {
111         case 0:
112                 if (call->enabled) {
113                         call->enabled = 0;
114                         tracing_stop_cmdline_record();
115                         call->unregfunc(call);
116                 }
117                 break;
118         case 1:
119                 if (!call->enabled) {
120                         call->enabled = 1;
121                         tracing_start_cmdline_record();
122                         call->regfunc(call);
123                 }
124                 break;
125         }
126 }
127
128 static void ftrace_clear_events(void)
129 {
130         struct ftrace_event_call *call;
131
132         mutex_lock(&event_mutex);
133         list_for_each_entry(call, &ftrace_events, list) {
134                 ftrace_event_enable_disable(call, 0);
135         }
136         mutex_unlock(&event_mutex);
137 }
138
139 /*
140  * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
141  */
142 static int __ftrace_set_clr_event(const char *match, const char *sub,
143                                   const char *event, int set)
144 {
145         struct ftrace_event_call *call;
146         int ret = -EINVAL;
147
148         mutex_lock(&event_mutex);
149         list_for_each_entry(call, &ftrace_events, list) {
150
151                 if (!call->name || !call->regfunc)
152                         continue;
153
154                 if (match &&
155                     strcmp(match, call->name) != 0 &&
156                     strcmp(match, call->system) != 0)
157                         continue;
158
159                 if (sub && strcmp(sub, call->system) != 0)
160                         continue;
161
162                 if (event && strcmp(event, call->name) != 0)
163                         continue;
164
165                 ftrace_event_enable_disable(call, set);
166
167                 ret = 0;
168         }
169         mutex_unlock(&event_mutex);
170
171         return ret;
172 }
173
174 static int ftrace_set_clr_event(char *buf, int set)
175 {
176         char *event = NULL, *sub = NULL, *match;
177
178         /*
179          * The buf format can be <subsystem>:<event-name>
180          *  *:<event-name> means any event by that name.
181          *  :<event-name> is the same.
182          *
183          *  <subsystem>:* means all events in that subsystem
184          *  <subsystem>: means the same.
185          *
186          *  <name> (no ':') means all events in a subsystem with
187          *  the name <name> or any event that matches <name>
188          */
189
190         match = strsep(&buf, ":");
191         if (buf) {
192                 sub = match;
193                 event = buf;
194                 match = NULL;
195
196                 if (!strlen(sub) || strcmp(sub, "*") == 0)
197                         sub = NULL;
198                 if (!strlen(event) || strcmp(event, "*") == 0)
199                         event = NULL;
200         }
201
202         return __ftrace_set_clr_event(match, sub, event, set);
203 }
204
205 /**
206  * trace_set_clr_event - enable or disable an event
207  * @system: system name to match (NULL for any system)
208  * @event: event name to match (NULL for all events, within system)
209  * @set: 1 to enable, 0 to disable
210  *
211  * This is a way for other parts of the kernel to enable or disable
212  * event recording.
213  *
214  * Returns 0 on success, -EINVAL if the parameters do not match any
215  * registered events.
216  */
217 int trace_set_clr_event(const char *system, const char *event, int set)
218 {
219         return __ftrace_set_clr_event(NULL, system, event, set);
220 }
221
222 /* 128 should be much more than enough */
223 #define EVENT_BUF_SIZE          127
224
225 static ssize_t
226 ftrace_event_write(struct file *file, const char __user *ubuf,
227                    size_t cnt, loff_t *ppos)
228 {
229         size_t read = 0;
230         int i, set = 1;
231         ssize_t ret;
232         char *buf;
233         char ch;
234
235         if (!cnt || cnt < 0)
236                 return 0;
237
238         ret = tracing_update_buffers();
239         if (ret < 0)
240                 return ret;
241
242         ret = get_user(ch, ubuf++);
243         if (ret)
244                 return ret;
245         read++;
246         cnt--;
247
248         /* skip white space */
249         while (cnt && isspace(ch)) {
250                 ret = get_user(ch, ubuf++);
251                 if (ret)
252                         return ret;
253                 read++;
254                 cnt--;
255         }
256
257         /* Only white space found? */
258         if (isspace(ch)) {
259                 file->f_pos += read;
260                 ret = read;
261                 return ret;
262         }
263
264         buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
265         if (!buf)
266                 return -ENOMEM;
267
268         if (cnt > EVENT_BUF_SIZE)
269                 cnt = EVENT_BUF_SIZE;
270
271         i = 0;
272         while (cnt && !isspace(ch)) {
273                 if (!i && ch == '!')
274                         set = 0;
275                 else
276                         buf[i++] = ch;
277
278                 ret = get_user(ch, ubuf++);
279                 if (ret)
280                         goto out_free;
281                 read++;
282                 cnt--;
283         }
284         buf[i] = 0;
285
286         file->f_pos += read;
287
288         ret = ftrace_set_clr_event(buf, set);
289         if (ret)
290                 goto out_free;
291
292         ret = read;
293
294  out_free:
295         kfree(buf);
296
297         return ret;
298 }
299
300 static void *
301 t_next(struct seq_file *m, void *v, loff_t *pos)
302 {
303         struct list_head *list = m->private;
304         struct ftrace_event_call *call;
305
306         (*pos)++;
307
308         for (;;) {
309                 if (list == &ftrace_events)
310                         return NULL;
311
312                 call = list_entry(list, struct ftrace_event_call, list);
313
314                 /*
315                  * The ftrace subsystem is for showing formats only.
316                  * They can not be enabled or disabled via the event files.
317                  */
318                 if (call->regfunc)
319                         break;
320
321                 list = list->next;
322         }
323
324         m->private = list->next;
325
326         return call;
327 }
328
329 static void *t_start(struct seq_file *m, loff_t *pos)
330 {
331         struct ftrace_event_call *call = NULL;
332         loff_t l;
333
334         mutex_lock(&event_mutex);
335
336         m->private = ftrace_events.next;
337         for (l = 0; l <= *pos; ) {
338                 call = t_next(m, NULL, &l);
339                 if (!call)
340                         break;
341         }
342         return call;
343 }
344
345 static void *
346 s_next(struct seq_file *m, void *v, loff_t *pos)
347 {
348         struct list_head *list = m->private;
349         struct ftrace_event_call *call;
350
351         (*pos)++;
352
353  retry:
354         if (list == &ftrace_events)
355                 return NULL;
356
357         call = list_entry(list, struct ftrace_event_call, list);
358
359         if (!call->enabled) {
360                 list = list->next;
361                 goto retry;
362         }
363
364         m->private = list->next;
365
366         return call;
367 }
368
369 static void *s_start(struct seq_file *m, loff_t *pos)
370 {
371         struct ftrace_event_call *call = NULL;
372         loff_t l;
373
374         mutex_lock(&event_mutex);
375
376         m->private = ftrace_events.next;
377         for (l = 0; l <= *pos; ) {
378                 call = s_next(m, NULL, &l);
379                 if (!call)
380                         break;
381         }
382         return call;
383 }
384
385 static int t_show(struct seq_file *m, void *v)
386 {
387         struct ftrace_event_call *call = v;
388
389         if (strcmp(call->system, TRACE_SYSTEM) != 0)
390                 seq_printf(m, "%s:", call->system);
391         seq_printf(m, "%s\n", call->name);
392
393         return 0;
394 }
395
396 static void t_stop(struct seq_file *m, void *p)
397 {
398         mutex_unlock(&event_mutex);
399 }
400
401 static int
402 ftrace_event_seq_open(struct inode *inode, struct file *file)
403 {
404         const struct seq_operations *seq_ops;
405
406         if ((file->f_mode & FMODE_WRITE) &&
407             (file->f_flags & O_TRUNC))
408                 ftrace_clear_events();
409
410         seq_ops = inode->i_private;
411         return seq_open(file, seq_ops);
412 }
413
414 static ssize_t
415 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
416                   loff_t *ppos)
417 {
418         struct ftrace_event_call *call = filp->private_data;
419         char *buf;
420
421         if (call->enabled)
422                 buf = "1\n";
423         else
424                 buf = "0\n";
425
426         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
427 }
428
429 static ssize_t
430 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
431                    loff_t *ppos)
432 {
433         struct ftrace_event_call *call = filp->private_data;
434         char buf[64];
435         unsigned long val;
436         int ret;
437
438         if (cnt >= sizeof(buf))
439                 return -EINVAL;
440
441         if (copy_from_user(&buf, ubuf, cnt))
442                 return -EFAULT;
443
444         buf[cnt] = 0;
445
446         ret = strict_strtoul(buf, 10, &val);
447         if (ret < 0)
448                 return ret;
449
450         ret = tracing_update_buffers();
451         if (ret < 0)
452                 return ret;
453
454         switch (val) {
455         case 0:
456         case 1:
457                 mutex_lock(&event_mutex);
458                 ftrace_event_enable_disable(call, val);
459                 mutex_unlock(&event_mutex);
460                 break;
461
462         default:
463                 return -EINVAL;
464         }
465
466         *ppos += cnt;
467
468         return cnt;
469 }
470
471 static ssize_t
472 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
473                    loff_t *ppos)
474 {
475         const char set_to_char[4] = { '?', '0', '1', 'X' };
476         const char *system = filp->private_data;
477         struct ftrace_event_call *call;
478         char buf[2];
479         int set = 0;
480         int ret;
481
482         mutex_lock(&event_mutex);
483         list_for_each_entry(call, &ftrace_events, list) {
484                 if (!call->name || !call->regfunc)
485                         continue;
486
487                 if (system && strcmp(call->system, system) != 0)
488                         continue;
489
490                 /*
491                  * We need to find out if all the events are set
492                  * or if all events or cleared, or if we have
493                  * a mixture.
494                  */
495                 set |= (1 << !!call->enabled);
496
497                 /*
498                  * If we have a mixture, no need to look further.
499                  */
500                 if (set == 3)
501                         break;
502         }
503         mutex_unlock(&event_mutex);
504
505         buf[0] = set_to_char[set];
506         buf[1] = '\n';
507
508         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
509
510         return ret;
511 }
512
513 static ssize_t
514 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
515                     loff_t *ppos)
516 {
517         const char *system = filp->private_data;
518         unsigned long val;
519         char buf[64];
520         ssize_t ret;
521
522         if (cnt >= sizeof(buf))
523                 return -EINVAL;
524
525         if (copy_from_user(&buf, ubuf, cnt))
526                 return -EFAULT;
527
528         buf[cnt] = 0;
529
530         ret = strict_strtoul(buf, 10, &val);
531         if (ret < 0)
532                 return ret;
533
534         ret = tracing_update_buffers();
535         if (ret < 0)
536                 return ret;
537
538         if (val != 0 && val != 1)
539                 return -EINVAL;
540
541         ret = __ftrace_set_clr_event(NULL, system, NULL, val);
542         if (ret)
543                 goto out;
544
545         ret = cnt;
546
547 out:
548         *ppos += cnt;
549
550         return ret;
551 }
552
553 extern char *__bad_type_size(void);
554
555 #undef FIELD
556 #define FIELD(type, name)                                               \
557         sizeof(type) != sizeof(field.name) ? __bad_type_size() :        \
558         #type, "common_" #name, offsetof(typeof(field), name),          \
559                 sizeof(field.name)
560
561 static int trace_write_header(struct trace_seq *s)
562 {
563         struct trace_entry field;
564
565         /* struct trace_entry */
566         return trace_seq_printf(s,
567                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
568                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
569                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
570                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
571                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
572                                 "\n",
573                                 FIELD(unsigned short, type),
574                                 FIELD(unsigned char, flags),
575                                 FIELD(unsigned char, preempt_count),
576                                 FIELD(int, pid),
577                                 FIELD(int, tgid));
578 }
579
580 static ssize_t
581 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
582                   loff_t *ppos)
583 {
584         struct ftrace_event_call *call = filp->private_data;
585         struct trace_seq *s;
586         char *buf;
587         int r;
588
589         if (*ppos)
590                 return 0;
591
592         s = kmalloc(sizeof(*s), GFP_KERNEL);
593         if (!s)
594                 return -ENOMEM;
595
596         trace_seq_init(s);
597
598         /* If any of the first writes fail, so will the show_format. */
599
600         trace_seq_printf(s, "name: %s\n", call->name);
601         trace_seq_printf(s, "ID: %d\n", call->id);
602         trace_seq_printf(s, "format:\n");
603         trace_write_header(s);
604
605         r = call->show_format(call, s);
606         if (!r) {
607                 /*
608                  * ug!  The format output is bigger than a PAGE!!
609                  */
610                 buf = "FORMAT TOO BIG\n";
611                 r = simple_read_from_buffer(ubuf, cnt, ppos,
612                                               buf, strlen(buf));
613                 goto out;
614         }
615
616         r = simple_read_from_buffer(ubuf, cnt, ppos,
617                                     s->buffer, s->len);
618  out:
619         kfree(s);
620         return r;
621 }
622
623 static ssize_t
624 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
625 {
626         struct ftrace_event_call *call = filp->private_data;
627         struct trace_seq *s;
628         int r;
629
630         if (*ppos)
631                 return 0;
632
633         s = kmalloc(sizeof(*s), GFP_KERNEL);
634         if (!s)
635                 return -ENOMEM;
636
637         trace_seq_init(s);
638         trace_seq_printf(s, "%d\n", call->id);
639
640         r = simple_read_from_buffer(ubuf, cnt, ppos,
641                                     s->buffer, s->len);
642         kfree(s);
643         return r;
644 }
645
646 static ssize_t
647 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
648                   loff_t *ppos)
649 {
650         struct ftrace_event_call *call = filp->private_data;
651         struct trace_seq *s;
652         int r;
653
654         if (*ppos)
655                 return 0;
656
657         s = kmalloc(sizeof(*s), GFP_KERNEL);
658         if (!s)
659                 return -ENOMEM;
660
661         trace_seq_init(s);
662
663         print_event_filter(call, s);
664         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
665
666         kfree(s);
667
668         return r;
669 }
670
671 static ssize_t
672 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
673                    loff_t *ppos)
674 {
675         struct ftrace_event_call *call = filp->private_data;
676         char *buf;
677         int err;
678
679         if (cnt >= PAGE_SIZE)
680                 return -EINVAL;
681
682         buf = (char *)__get_free_page(GFP_TEMPORARY);
683         if (!buf)
684                 return -ENOMEM;
685
686         if (copy_from_user(buf, ubuf, cnt)) {
687                 free_page((unsigned long) buf);
688                 return -EFAULT;
689         }
690         buf[cnt] = '\0';
691
692         err = apply_event_filter(call, buf);
693         free_page((unsigned long) buf);
694         if (err < 0)
695                 return err;
696
697         *ppos += cnt;
698
699         return cnt;
700 }
701
702 static ssize_t
703 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
704                       loff_t *ppos)
705 {
706         struct event_subsystem *system = filp->private_data;
707         struct trace_seq *s;
708         int r;
709
710         if (*ppos)
711                 return 0;
712
713         s = kmalloc(sizeof(*s), GFP_KERNEL);
714         if (!s)
715                 return -ENOMEM;
716
717         trace_seq_init(s);
718
719         print_subsystem_event_filter(system, s);
720         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
721
722         kfree(s);
723
724         return r;
725 }
726
727 static ssize_t
728 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
729                        loff_t *ppos)
730 {
731         struct event_subsystem *system = filp->private_data;
732         char *buf;
733         int err;
734
735         if (cnt >= PAGE_SIZE)
736                 return -EINVAL;
737
738         buf = (char *)__get_free_page(GFP_TEMPORARY);
739         if (!buf)
740                 return -ENOMEM;
741
742         if (copy_from_user(buf, ubuf, cnt)) {
743                 free_page((unsigned long) buf);
744                 return -EFAULT;
745         }
746         buf[cnt] = '\0';
747
748         err = apply_subsystem_event_filter(system, buf);
749         free_page((unsigned long) buf);
750         if (err < 0)
751                 return err;
752
753         *ppos += cnt;
754
755         return cnt;
756 }
757
758 static ssize_t
759 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
760 {
761         int (*func)(struct trace_seq *s) = filp->private_data;
762         struct trace_seq *s;
763         int r;
764
765         if (*ppos)
766                 return 0;
767
768         s = kmalloc(sizeof(*s), GFP_KERNEL);
769         if (!s)
770                 return -ENOMEM;
771
772         trace_seq_init(s);
773
774         func(s);
775         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
776
777         kfree(s);
778
779         return r;
780 }
781
782 static const struct seq_operations show_event_seq_ops = {
783         .start = t_start,
784         .next = t_next,
785         .show = t_show,
786         .stop = t_stop,
787 };
788
789 static const struct seq_operations show_set_event_seq_ops = {
790         .start = s_start,
791         .next = s_next,
792         .show = t_show,
793         .stop = t_stop,
794 };
795
796 static const struct file_operations ftrace_avail_fops = {
797         .open = ftrace_event_seq_open,
798         .read = seq_read,
799         .llseek = seq_lseek,
800         .release = seq_release,
801 };
802
803 static const struct file_operations ftrace_set_event_fops = {
804         .open = ftrace_event_seq_open,
805         .read = seq_read,
806         .write = ftrace_event_write,
807         .llseek = seq_lseek,
808         .release = seq_release,
809 };
810
811 static const struct file_operations ftrace_enable_fops = {
812         .open = tracing_open_generic,
813         .read = event_enable_read,
814         .write = event_enable_write,
815 };
816
817 static const struct file_operations ftrace_event_format_fops = {
818         .open = tracing_open_generic,
819         .read = event_format_read,
820 };
821
822 static const struct file_operations ftrace_event_id_fops = {
823         .open = tracing_open_generic,
824         .read = event_id_read,
825 };
826
827 static const struct file_operations ftrace_event_filter_fops = {
828         .open = tracing_open_generic,
829         .read = event_filter_read,
830         .write = event_filter_write,
831 };
832
833 static const struct file_operations ftrace_subsystem_filter_fops = {
834         .open = tracing_open_generic,
835         .read = subsystem_filter_read,
836         .write = subsystem_filter_write,
837 };
838
839 static const struct file_operations ftrace_system_enable_fops = {
840         .open = tracing_open_generic,
841         .read = system_enable_read,
842         .write = system_enable_write,
843 };
844
845 static const struct file_operations ftrace_show_header_fops = {
846         .open = tracing_open_generic,
847         .read = show_header,
848 };
849
850 static struct dentry *event_trace_events_dir(void)
851 {
852         static struct dentry *d_tracer;
853         static struct dentry *d_events;
854
855         if (d_events)
856                 return d_events;
857
858         d_tracer = tracing_init_dentry();
859         if (!d_tracer)
860                 return NULL;
861
862         d_events = debugfs_create_dir("events", d_tracer);
863         if (!d_events)
864                 pr_warning("Could not create debugfs "
865                            "'events' directory\n");
866
867         return d_events;
868 }
869
870 static LIST_HEAD(event_subsystems);
871
872 static struct dentry *
873 event_subsystem_dir(const char *name, struct dentry *d_events)
874 {
875         struct event_subsystem *system;
876         struct dentry *entry;
877
878         /* First see if we did not already create this dir */
879         list_for_each_entry(system, &event_subsystems, list) {
880                 if (strcmp(system->name, name) == 0) {
881                         system->nr_events++;
882                         return system->entry;
883                 }
884         }
885
886         /* need to create new entry */
887         system = kmalloc(sizeof(*system), GFP_KERNEL);
888         if (!system) {
889                 pr_warning("No memory to create event subsystem %s\n",
890                            name);
891                 return d_events;
892         }
893
894         system->entry = debugfs_create_dir(name, d_events);
895         if (!system->entry) {
896                 pr_warning("Could not create event subsystem %s\n",
897                            name);
898                 kfree(system);
899                 return d_events;
900         }
901
902         system->nr_events = 1;
903         system->name = kstrdup(name, GFP_KERNEL);
904         if (!system->name) {
905                 debugfs_remove(system->entry);
906                 kfree(system);
907                 return d_events;
908         }
909
910         list_add(&system->list, &event_subsystems);
911
912         system->filter = NULL;
913
914         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
915         if (!system->filter) {
916                 pr_warning("Could not allocate filter for subsystem "
917                            "'%s'\n", name);
918                 return system->entry;
919         }
920
921         entry = debugfs_create_file("filter", 0644, system->entry, system,
922                                     &ftrace_subsystem_filter_fops);
923         if (!entry) {
924                 kfree(system->filter);
925                 system->filter = NULL;
926                 pr_warning("Could not create debugfs "
927                            "'%s/filter' entry\n", name);
928         }
929
930         entry = trace_create_file("enable", 0644, system->entry,
931                                   (void *)system->name,
932                                   &ftrace_system_enable_fops);
933
934         return system->entry;
935 }
936
937 static int
938 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
939                  const struct file_operations *id,
940                  const struct file_operations *enable,
941                  const struct file_operations *filter,
942                  const struct file_operations *format)
943 {
944         struct dentry *entry;
945         int ret;
946
947         /*
948          * If the trace point header did not define TRACE_SYSTEM
949          * then the system would be called "TRACE_SYSTEM".
950          */
951         if (strcmp(call->system, TRACE_SYSTEM) != 0)
952                 d_events = event_subsystem_dir(call->system, d_events);
953
954         call->dir = debugfs_create_dir(call->name, d_events);
955         if (!call->dir) {
956                 pr_warning("Could not create debugfs "
957                            "'%s' directory\n", call->name);
958                 return -1;
959         }
960
961         if (call->regfunc)
962                 entry = trace_create_file("enable", 0644, call->dir, call,
963                                           enable);
964
965         if (call->id && call->profile_enable)
966                 entry = trace_create_file("id", 0444, call->dir, call,
967                                           id);
968
969         if (call->define_fields) {
970                 ret = call->define_fields(call);
971                 if (ret < 0) {
972                         pr_warning("Could not initialize trace point"
973                                    " events/%s\n", call->name);
974                         return ret;
975                 }
976                 entry = trace_create_file("filter", 0644, call->dir, call,
977                                           filter);
978         }
979
980         /* A trace may not want to export its format */
981         if (!call->show_format)
982                 return 0;
983
984         entry = trace_create_file("format", 0444, call->dir, call,
985                                   format);
986
987         return 0;
988 }
989
990 static int __trace_add_event_call(struct ftrace_event_call *call)
991 {
992         struct dentry *d_events;
993         int ret;
994
995         if (!call->name)
996                 return -EINVAL;
997
998         if (call->raw_init) {
999                 ret = call->raw_init(call);
1000                 if (ret < 0) {
1001                         if (ret != -ENOSYS)
1002                                 pr_warning("Could not initialize trace "
1003                                 "events/%s\n", call->name);
1004                         return ret;
1005                 }
1006         }
1007
1008         d_events = event_trace_events_dir();
1009         if (!d_events)
1010                 return -ENOENT;
1011
1012         list_add(&call->list, &ftrace_events);
1013         return event_create_dir(call, d_events, &ftrace_event_id_fops,
1014                                 &ftrace_enable_fops, &ftrace_event_filter_fops,
1015                                 &ftrace_event_format_fops);
1016 }
1017
1018 /* Add an additional event_call dynamically */
1019 int trace_add_event_call(struct ftrace_event_call *call)
1020 {
1021         int ret;
1022         mutex_lock(&event_mutex);
1023         ret = __trace_add_event_call(call);
1024         mutex_unlock(&event_mutex);
1025         return ret;
1026 }
1027
1028 static void remove_subsystem_dir(const char *name)
1029 {
1030         struct event_subsystem *system;
1031
1032         if (strcmp(name, TRACE_SYSTEM) == 0)
1033                 return;
1034
1035         list_for_each_entry(system, &event_subsystems, list) {
1036                 if (strcmp(system->name, name) == 0) {
1037                         if (!--system->nr_events) {
1038                                 struct event_filter *filter = system->filter;
1039
1040                                 debugfs_remove_recursive(system->entry);
1041                                 list_del(&system->list);
1042                                 if (filter) {
1043                                         kfree(filter->filter_string);
1044                                         kfree(filter);
1045                                 }
1046                                 kfree(system->name);
1047                                 kfree(system);
1048                         }
1049                         break;
1050                 }
1051         }
1052 }
1053
1054 static void __trace_remove_event_call(struct ftrace_event_call *call)
1055 {
1056         ftrace_event_enable_disable(call, 0);
1057         if (call->event)
1058                 __unregister_ftrace_event(call->event);
1059         debugfs_remove_recursive(call->dir);
1060         list_del(&call->list);
1061         trace_destroy_fields(call);
1062         destroy_preds(call);
1063         remove_subsystem_dir(call->system);
1064 }
1065
1066 /* Remove an event_call */
1067 void trace_remove_event_call(struct ftrace_event_call *call)
1068 {
1069         mutex_lock(&event_mutex);
1070         __trace_remove_event_call(call);
1071         mutex_unlock(&event_mutex);
1072 }
1073
1074 #define for_each_event(event, start, end)                       \
1075         for (event = start;                                     \
1076              (unsigned long)event < (unsigned long)end;         \
1077              event++)
1078
1079 #ifdef CONFIG_MODULES
1080
1081 static LIST_HEAD(ftrace_module_file_list);
1082
1083 /*
1084  * Modules must own their file_operations to keep up with
1085  * reference counting.
1086  */
1087 struct ftrace_module_file_ops {
1088         struct list_head                list;
1089         struct module                   *mod;
1090         struct file_operations          id;
1091         struct file_operations          enable;
1092         struct file_operations          format;
1093         struct file_operations          filter;
1094 };
1095
1096 static struct ftrace_module_file_ops *
1097 trace_create_file_ops(struct module *mod)
1098 {
1099         struct ftrace_module_file_ops *file_ops;
1100
1101         /*
1102          * This is a bit of a PITA. To allow for correct reference
1103          * counting, modules must "own" their file_operations.
1104          * To do this, we allocate the file operations that will be
1105          * used in the event directory.
1106          */
1107
1108         file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1109         if (!file_ops)
1110                 return NULL;
1111
1112         file_ops->mod = mod;
1113
1114         file_ops->id = ftrace_event_id_fops;
1115         file_ops->id.owner = mod;
1116
1117         file_ops->enable = ftrace_enable_fops;
1118         file_ops->enable.owner = mod;
1119
1120         file_ops->filter = ftrace_event_filter_fops;
1121         file_ops->filter.owner = mod;
1122
1123         file_ops->format = ftrace_event_format_fops;
1124         file_ops->format.owner = mod;
1125
1126         list_add(&file_ops->list, &ftrace_module_file_list);
1127
1128         return file_ops;
1129 }
1130
1131 static void trace_module_add_events(struct module *mod)
1132 {
1133         struct ftrace_module_file_ops *file_ops = NULL;
1134         struct ftrace_event_call *call, *start, *end;
1135         struct dentry *d_events;
1136         int ret;
1137
1138         start = mod->trace_events;
1139         end = mod->trace_events + mod->num_trace_events;
1140
1141         if (start == end)
1142                 return;
1143
1144         d_events = event_trace_events_dir();
1145         if (!d_events)
1146                 return;
1147
1148         for_each_event(call, start, end) {
1149                 /* The linker may leave blanks */
1150                 if (!call->name)
1151                         continue;
1152                 if (call->raw_init) {
1153                         ret = call->raw_init(call);
1154                         if (ret < 0) {
1155                                 if (ret != -ENOSYS)
1156                                         pr_warning("Could not initialize trace "
1157                                         "point events/%s\n", call->name);
1158                                 continue;
1159                         }
1160                 }
1161                 /*
1162                  * This module has events, create file ops for this module
1163                  * if not already done.
1164                  */
1165                 if (!file_ops) {
1166                         file_ops = trace_create_file_ops(mod);
1167                         if (!file_ops)
1168                                 return;
1169                 }
1170                 call->mod = mod;
1171                 list_add(&call->list, &ftrace_events);
1172                 event_create_dir(call, d_events,
1173                                  &file_ops->id, &file_ops->enable,
1174                                  &file_ops->filter, &file_ops->format);
1175         }
1176 }
1177
1178 static void trace_module_remove_events(struct module *mod)
1179 {
1180         struct ftrace_module_file_ops *file_ops;
1181         struct ftrace_event_call *call, *p;
1182         bool found = false;
1183
1184         down_write(&trace_event_mutex);
1185         list_for_each_entry_safe(call, p, &ftrace_events, list) {
1186                 if (call->mod == mod) {
1187                         found = true;
1188                         __trace_remove_event_call(call);
1189                 }
1190         }
1191
1192         /* Now free the file_operations */
1193         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1194                 if (file_ops->mod == mod)
1195                         break;
1196         }
1197         if (&file_ops->list != &ftrace_module_file_list) {
1198                 list_del(&file_ops->list);
1199                 kfree(file_ops);
1200         }
1201
1202         /*
1203          * It is safest to reset the ring buffer if the module being unloaded
1204          * registered any events.
1205          */
1206         if (found)
1207                 tracing_reset_current_online_cpus();
1208         up_write(&trace_event_mutex);
1209 }
1210
1211 static int trace_module_notify(struct notifier_block *self,
1212                                unsigned long val, void *data)
1213 {
1214         struct module *mod = data;
1215
1216         mutex_lock(&event_mutex);
1217         switch (val) {
1218         case MODULE_STATE_COMING:
1219                 trace_module_add_events(mod);
1220                 break;
1221         case MODULE_STATE_GOING:
1222                 trace_module_remove_events(mod);
1223                 break;
1224         }
1225         mutex_unlock(&event_mutex);
1226
1227         return 0;
1228 }
1229 #else
1230 static int trace_module_notify(struct notifier_block *self,
1231                                unsigned long val, void *data)
1232 {
1233         return 0;
1234 }
1235 #endif /* CONFIG_MODULES */
1236
1237 struct notifier_block trace_module_nb = {
1238         .notifier_call = trace_module_notify,
1239         .priority = 0,
1240 };
1241
1242 extern struct ftrace_event_call __start_ftrace_events[];
1243 extern struct ftrace_event_call __stop_ftrace_events[];
1244
1245 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1246
1247 static __init int setup_trace_event(char *str)
1248 {
1249         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1250         ring_buffer_expanded = 1;
1251         tracing_selftest_disabled = 1;
1252
1253         return 1;
1254 }
1255 __setup("trace_event=", setup_trace_event);
1256
1257 static __init int event_trace_init(void)
1258 {
1259         struct ftrace_event_call *call;
1260         struct dentry *d_tracer;
1261         struct dentry *entry;
1262         struct dentry *d_events;
1263         int ret;
1264         char *buf = bootup_event_buf;
1265         char *token;
1266
1267         d_tracer = tracing_init_dentry();
1268         if (!d_tracer)
1269                 return 0;
1270
1271         entry = debugfs_create_file("available_events", 0444, d_tracer,
1272                                     (void *)&show_event_seq_ops,
1273                                     &ftrace_avail_fops);
1274         if (!entry)
1275                 pr_warning("Could not create debugfs "
1276                            "'available_events' entry\n");
1277
1278         entry = debugfs_create_file("set_event", 0644, d_tracer,
1279                                     (void *)&show_set_event_seq_ops,
1280                                     &ftrace_set_event_fops);
1281         if (!entry)
1282                 pr_warning("Could not create debugfs "
1283                            "'set_event' entry\n");
1284
1285         d_events = event_trace_events_dir();
1286         if (!d_events)
1287                 return 0;
1288
1289         /* ring buffer internal formats */
1290         trace_create_file("header_page", 0444, d_events,
1291                           ring_buffer_print_page_header,
1292                           &ftrace_show_header_fops);
1293
1294         trace_create_file("header_event", 0444, d_events,
1295                           ring_buffer_print_entry_header,
1296                           &ftrace_show_header_fops);
1297
1298         trace_create_file("enable", 0644, d_events,
1299                           NULL, &ftrace_system_enable_fops);
1300
1301         for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1302                 /* The linker may leave blanks */
1303                 if (!call->name)
1304                         continue;
1305                 if (call->raw_init) {
1306                         ret = call->raw_init(call);
1307                         if (ret < 0) {
1308                                 if (ret != -ENOSYS)
1309                                         pr_warning("Could not initialize trace "
1310                                         "point events/%s\n", call->name);
1311                                 continue;
1312                         }
1313                 }
1314                 list_add(&call->list, &ftrace_events);
1315                 event_create_dir(call, d_events, &ftrace_event_id_fops,
1316                                  &ftrace_enable_fops, &ftrace_event_filter_fops,
1317                                  &ftrace_event_format_fops);
1318         }
1319
1320         while (true) {
1321                 token = strsep(&buf, ",");
1322
1323                 if (!token)
1324                         break;
1325                 if (!*token)
1326                         continue;
1327
1328                 ret = ftrace_set_clr_event(token, 1);
1329                 if (ret)
1330                         pr_warning("Failed to enable trace event: %s\n", token);
1331         }
1332
1333         ret = register_module_notifier(&trace_module_nb);
1334         if (ret)
1335                 pr_warning("Failed to register trace events module notifier\n");
1336
1337         return 0;
1338 }
1339 fs_initcall(event_trace_init);
1340
1341 #ifdef CONFIG_FTRACE_STARTUP_TEST
1342
1343 static DEFINE_SPINLOCK(test_spinlock);
1344 static DEFINE_SPINLOCK(test_spinlock_irq);
1345 static DEFINE_MUTEX(test_mutex);
1346
1347 static __init void test_work(struct work_struct *dummy)
1348 {
1349         spin_lock(&test_spinlock);
1350         spin_lock_irq(&test_spinlock_irq);
1351         udelay(1);
1352         spin_unlock_irq(&test_spinlock_irq);
1353         spin_unlock(&test_spinlock);
1354
1355         mutex_lock(&test_mutex);
1356         msleep(1);
1357         mutex_unlock(&test_mutex);
1358 }
1359
1360 static __init int event_test_thread(void *unused)
1361 {
1362         void *test_malloc;
1363
1364         test_malloc = kmalloc(1234, GFP_KERNEL);
1365         if (!test_malloc)
1366                 pr_info("failed to kmalloc\n");
1367
1368         schedule_on_each_cpu(test_work);
1369
1370         kfree(test_malloc);
1371
1372         set_current_state(TASK_INTERRUPTIBLE);
1373         while (!kthread_should_stop())
1374                 schedule();
1375
1376         return 0;
1377 }
1378
1379 /*
1380  * Do various things that may trigger events.
1381  */
1382 static __init void event_test_stuff(void)
1383 {
1384         struct task_struct *test_thread;
1385
1386         test_thread = kthread_run(event_test_thread, NULL, "test-events");
1387         msleep(1);
1388         kthread_stop(test_thread);
1389 }
1390
1391 /*
1392  * For every trace event defined, we will test each trace point separately,
1393  * and then by groups, and finally all trace points.
1394  */
1395 static __init void event_trace_self_tests(void)
1396 {
1397         struct ftrace_event_call *call;
1398         struct event_subsystem *system;
1399         int ret;
1400
1401         pr_info("Running tests on trace events:\n");
1402
1403         list_for_each_entry(call, &ftrace_events, list) {
1404
1405                 /* Only test those that have a regfunc */
1406                 if (!call->regfunc)
1407                         continue;
1408
1409                 pr_info("Testing event %s: ", call->name);
1410
1411                 /*
1412                  * If an event is already enabled, someone is using
1413                  * it and the self test should not be on.
1414                  */
1415                 if (call->enabled) {
1416                         pr_warning("Enabled event during self test!\n");
1417                         WARN_ON_ONCE(1);
1418                         continue;
1419                 }
1420
1421                 ftrace_event_enable_disable(call, 1);
1422                 event_test_stuff();
1423                 ftrace_event_enable_disable(call, 0);
1424
1425                 pr_cont("OK\n");
1426         }
1427
1428         /* Now test at the sub system level */
1429
1430         pr_info("Running tests on trace event systems:\n");
1431
1432         list_for_each_entry(system, &event_subsystems, list) {
1433
1434                 /* the ftrace system is special, skip it */
1435                 if (strcmp(system->name, "ftrace") == 0)
1436                         continue;
1437
1438                 pr_info("Testing event system %s: ", system->name);
1439
1440                 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1441                 if (WARN_ON_ONCE(ret)) {
1442                         pr_warning("error enabling system %s\n",
1443                                    system->name);
1444                         continue;
1445                 }
1446
1447                 event_test_stuff();
1448
1449                 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1450                 if (WARN_ON_ONCE(ret))
1451                         pr_warning("error disabling system %s\n",
1452                                    system->name);
1453
1454                 pr_cont("OK\n");
1455         }
1456
1457         /* Test with all events enabled */
1458
1459         pr_info("Running tests on all trace events:\n");
1460         pr_info("Testing all events: ");
1461
1462         ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1463         if (WARN_ON_ONCE(ret)) {
1464                 pr_warning("error enabling all events\n");
1465                 return;
1466         }
1467
1468         event_test_stuff();
1469
1470         /* reset sysname */
1471         ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1472         if (WARN_ON_ONCE(ret)) {
1473                 pr_warning("error disabling all events\n");
1474                 return;
1475         }
1476
1477         pr_cont("OK\n");
1478 }
1479
1480 #ifdef CONFIG_FUNCTION_TRACER
1481
1482 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1483
1484 static void
1485 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1486 {
1487         struct ring_buffer_event *event;
1488         struct ring_buffer *buffer;
1489         struct ftrace_entry *entry;
1490         unsigned long flags;
1491         long disabled;
1492         int resched;
1493         int cpu;
1494         int pc;
1495
1496         pc = preempt_count();
1497         resched = ftrace_preempt_disable();
1498         cpu = raw_smp_processor_id();
1499         disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1500
1501         if (disabled != 1)
1502                 goto out;
1503
1504         local_save_flags(flags);
1505
1506         event = trace_current_buffer_lock_reserve(&buffer,
1507                                                   TRACE_FN, sizeof(*entry),
1508                                                   flags, pc);
1509         if (!event)
1510                 goto out;
1511         entry   = ring_buffer_event_data(event);
1512         entry->ip                       = ip;
1513         entry->parent_ip                = parent_ip;
1514
1515         trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1516
1517  out:
1518         atomic_dec(&per_cpu(test_event_disable, cpu));
1519         ftrace_preempt_enable(resched);
1520 }
1521
1522 static struct ftrace_ops trace_ops __initdata  =
1523 {
1524         .func = function_test_events_call,
1525 };
1526
1527 static __init void event_trace_self_test_with_function(void)
1528 {
1529         register_ftrace_function(&trace_ops);
1530         pr_info("Running tests again, along with the function tracer\n");
1531         event_trace_self_tests();
1532         unregister_ftrace_function(&trace_ops);
1533 }
1534 #else
1535 static __init void event_trace_self_test_with_function(void)
1536 {
1537 }
1538 #endif
1539
1540 static __init int event_trace_self_tests_init(void)
1541 {
1542         if (!tracing_selftest_disabled) {
1543                 event_trace_self_tests();
1544                 event_trace_self_test_with_function();
1545         }
1546
1547         return 0;
1548 }
1549
1550 late_initcall(event_trace_self_tests_init);
1551
1552 #endif