tracing/filters: add filter_mutex to protect filter predicates
[linux-2.6.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 "trace_output.h"
21
22 #define TRACE_SYSTEM "TRACE_SYSTEM"
23
24 static DEFINE_MUTEX(event_mutex);
25
26 LIST_HEAD(ftrace_events);
27
28 int trace_define_field(struct ftrace_event_call *call, char *type,
29                        char *name, int offset, int size)
30 {
31         struct ftrace_event_field *field;
32
33         field = kzalloc(sizeof(*field), GFP_KERNEL);
34         if (!field)
35                 goto err;
36
37         field->name = kstrdup(name, GFP_KERNEL);
38         if (!field->name)
39                 goto err;
40
41         field->type = kstrdup(type, GFP_KERNEL);
42         if (!field->type)
43                 goto err;
44
45         field->offset = offset;
46         field->size = size;
47         list_add(&field->link, &call->fields);
48
49         return 0;
50
51 err:
52         if (field) {
53                 kfree(field->name);
54                 kfree(field->type);
55         }
56         kfree(field);
57
58         return -ENOMEM;
59 }
60 EXPORT_SYMBOL_GPL(trace_define_field);
61
62 static void ftrace_clear_events(void)
63 {
64         struct ftrace_event_call *call;
65
66         list_for_each_entry(call, &ftrace_events, list) {
67
68                 if (call->enabled) {
69                         call->enabled = 0;
70                         call->unregfunc();
71                 }
72         }
73 }
74
75 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
76                                         int enable)
77 {
78
79         switch (enable) {
80         case 0:
81                 if (call->enabled) {
82                         call->enabled = 0;
83                         call->unregfunc();
84                 }
85                 break;
86         case 1:
87                 if (!call->enabled) {
88                         call->enabled = 1;
89                         call->regfunc();
90                 }
91                 break;
92         }
93 }
94
95 static int ftrace_set_clr_event(char *buf, int set)
96 {
97         struct ftrace_event_call *call;
98         char *event = NULL, *sub = NULL, *match;
99         int ret = -EINVAL;
100
101         /*
102          * The buf format can be <subsystem>:<event-name>
103          *  *:<event-name> means any event by that name.
104          *  :<event-name> is the same.
105          *
106          *  <subsystem>:* means all events in that subsystem
107          *  <subsystem>: means the same.
108          *
109          *  <name> (no ':') means all events in a subsystem with
110          *  the name <name> or any event that matches <name>
111          */
112
113         match = strsep(&buf, ":");
114         if (buf) {
115                 sub = match;
116                 event = buf;
117                 match = NULL;
118
119                 if (!strlen(sub) || strcmp(sub, "*") == 0)
120                         sub = NULL;
121                 if (!strlen(event) || strcmp(event, "*") == 0)
122                         event = NULL;
123         }
124
125         mutex_lock(&event_mutex);
126         list_for_each_entry(call, &ftrace_events, list) {
127
128                 if (!call->name || !call->regfunc)
129                         continue;
130
131                 if (match &&
132                     strcmp(match, call->name) != 0 &&
133                     strcmp(match, call->system) != 0)
134                         continue;
135
136                 if (sub && strcmp(sub, call->system) != 0)
137                         continue;
138
139                 if (event && strcmp(event, call->name) != 0)
140                         continue;
141
142                 ftrace_event_enable_disable(call, set);
143
144                 ret = 0;
145         }
146         mutex_unlock(&event_mutex);
147
148         return ret;
149 }
150
151 /* 128 should be much more than enough */
152 #define EVENT_BUF_SIZE          127
153
154 static ssize_t
155 ftrace_event_write(struct file *file, const char __user *ubuf,
156                    size_t cnt, loff_t *ppos)
157 {
158         size_t read = 0;
159         int i, set = 1;
160         ssize_t ret;
161         char *buf;
162         char ch;
163
164         if (!cnt || cnt < 0)
165                 return 0;
166
167         ret = tracing_update_buffers();
168         if (ret < 0)
169                 return ret;
170
171         ret = get_user(ch, ubuf++);
172         if (ret)
173                 return ret;
174         read++;
175         cnt--;
176
177         /* skip white space */
178         while (cnt && isspace(ch)) {
179                 ret = get_user(ch, ubuf++);
180                 if (ret)
181                         return ret;
182                 read++;
183                 cnt--;
184         }
185
186         /* Only white space found? */
187         if (isspace(ch)) {
188                 file->f_pos += read;
189                 ret = read;
190                 return ret;
191         }
192
193         buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
194         if (!buf)
195                 return -ENOMEM;
196
197         if (cnt > EVENT_BUF_SIZE)
198                 cnt = EVENT_BUF_SIZE;
199
200         i = 0;
201         while (cnt && !isspace(ch)) {
202                 if (!i && ch == '!')
203                         set = 0;
204                 else
205                         buf[i++] = ch;
206
207                 ret = get_user(ch, ubuf++);
208                 if (ret)
209                         goto out_free;
210                 read++;
211                 cnt--;
212         }
213         buf[i] = 0;
214
215         file->f_pos += read;
216
217         ret = ftrace_set_clr_event(buf, set);
218         if (ret)
219                 goto out_free;
220
221         ret = read;
222
223  out_free:
224         kfree(buf);
225
226         return ret;
227 }
228
229 static void *
230 t_next(struct seq_file *m, void *v, loff_t *pos)
231 {
232         struct list_head *list = m->private;
233         struct ftrace_event_call *call;
234
235         (*pos)++;
236
237         for (;;) {
238                 if (list == &ftrace_events)
239                         return NULL;
240
241                 call = list_entry(list, struct ftrace_event_call, list);
242
243                 /*
244                  * The ftrace subsystem is for showing formats only.
245                  * They can not be enabled or disabled via the event files.
246                  */
247                 if (call->regfunc)
248                         break;
249
250                 list = list->next;
251         }
252
253         m->private = list->next;
254
255         return call;
256 }
257
258 static void *t_start(struct seq_file *m, loff_t *pos)
259 {
260         return t_next(m, NULL, pos);
261 }
262
263 static void *
264 s_next(struct seq_file *m, void *v, loff_t *pos)
265 {
266         struct list_head *list = m->private;
267         struct ftrace_event_call *call;
268
269         (*pos)++;
270
271  retry:
272         if (list == &ftrace_events)
273                 return NULL;
274
275         call = list_entry(list, struct ftrace_event_call, list);
276
277         if (!call->enabled) {
278                 list = list->next;
279                 goto retry;
280         }
281
282         m->private = list->next;
283
284         return call;
285 }
286
287 static void *s_start(struct seq_file *m, loff_t *pos)
288 {
289         return s_next(m, NULL, pos);
290 }
291
292 static int t_show(struct seq_file *m, void *v)
293 {
294         struct ftrace_event_call *call = v;
295
296         if (strcmp(call->system, TRACE_SYSTEM) != 0)
297                 seq_printf(m, "%s:", call->system);
298         seq_printf(m, "%s\n", call->name);
299
300         return 0;
301 }
302
303 static void t_stop(struct seq_file *m, void *p)
304 {
305 }
306
307 static int
308 ftrace_event_seq_open(struct inode *inode, struct file *file)
309 {
310         int ret;
311         const struct seq_operations *seq_ops;
312
313         if ((file->f_mode & FMODE_WRITE) &&
314             !(file->f_flags & O_APPEND))
315                 ftrace_clear_events();
316
317         seq_ops = inode->i_private;
318         ret = seq_open(file, seq_ops);
319         if (!ret) {
320                 struct seq_file *m = file->private_data;
321
322                 m->private = ftrace_events.next;
323         }
324         return ret;
325 }
326
327 static ssize_t
328 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
329                   loff_t *ppos)
330 {
331         struct ftrace_event_call *call = filp->private_data;
332         char *buf;
333
334         if (call->enabled)
335                 buf = "1\n";
336         else
337                 buf = "0\n";
338
339         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
340 }
341
342 static ssize_t
343 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
344                    loff_t *ppos)
345 {
346         struct ftrace_event_call *call = filp->private_data;
347         char buf[64];
348         unsigned long val;
349         int ret;
350
351         if (cnt >= sizeof(buf))
352                 return -EINVAL;
353
354         if (copy_from_user(&buf, ubuf, cnt))
355                 return -EFAULT;
356
357         buf[cnt] = 0;
358
359         ret = strict_strtoul(buf, 10, &val);
360         if (ret < 0)
361                 return ret;
362
363         ret = tracing_update_buffers();
364         if (ret < 0)
365                 return ret;
366
367         switch (val) {
368         case 0:
369         case 1:
370                 mutex_lock(&event_mutex);
371                 ftrace_event_enable_disable(call, val);
372                 mutex_unlock(&event_mutex);
373                 break;
374
375         default:
376                 return -EINVAL;
377         }
378
379         *ppos += cnt;
380
381         return cnt;
382 }
383
384 #undef FIELD
385 #define FIELD(type, name)                                               \
386         #type, "common_" #name, offsetof(typeof(field), name),          \
387                 sizeof(field.name)
388
389 static int trace_write_header(struct trace_seq *s)
390 {
391         struct trace_entry field;
392
393         /* struct trace_entry */
394         return trace_seq_printf(s,
395                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
396                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
397                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
398                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
399                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
400                                 "\n",
401                                 FIELD(unsigned char, type),
402                                 FIELD(unsigned char, flags),
403                                 FIELD(unsigned char, preempt_count),
404                                 FIELD(int, pid),
405                                 FIELD(int, tgid));
406 }
407
408 static ssize_t
409 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
410                   loff_t *ppos)
411 {
412         struct ftrace_event_call *call = filp->private_data;
413         struct trace_seq *s;
414         char *buf;
415         int r;
416
417         if (*ppos)
418                 return 0;
419
420         s = kmalloc(sizeof(*s), GFP_KERNEL);
421         if (!s)
422                 return -ENOMEM;
423
424         trace_seq_init(s);
425
426         /* If any of the first writes fail, so will the show_format. */
427
428         trace_seq_printf(s, "name: %s\n", call->name);
429         trace_seq_printf(s, "ID: %d\n", call->id);
430         trace_seq_printf(s, "format:\n");
431         trace_write_header(s);
432
433         r = call->show_format(s);
434         if (!r) {
435                 /*
436                  * ug!  The format output is bigger than a PAGE!!
437                  */
438                 buf = "FORMAT TOO BIG\n";
439                 r = simple_read_from_buffer(ubuf, cnt, ppos,
440                                               buf, strlen(buf));
441                 goto out;
442         }
443
444         r = simple_read_from_buffer(ubuf, cnt, ppos,
445                                     s->buffer, s->len);
446  out:
447         kfree(s);
448         return r;
449 }
450
451 static ssize_t
452 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
453 {
454         struct ftrace_event_call *call = filp->private_data;
455         struct trace_seq *s;
456         int r;
457
458         if (*ppos)
459                 return 0;
460
461         s = kmalloc(sizeof(*s), GFP_KERNEL);
462         if (!s)
463                 return -ENOMEM;
464
465         trace_seq_init(s);
466         trace_seq_printf(s, "%d\n", call->id);
467
468         r = simple_read_from_buffer(ubuf, cnt, ppos,
469                                     s->buffer, s->len);
470         kfree(s);
471         return r;
472 }
473
474 static ssize_t
475 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
476                   loff_t *ppos)
477 {
478         struct ftrace_event_call *call = filp->private_data;
479         struct trace_seq *s;
480         int r;
481
482         if (*ppos)
483                 return 0;
484
485         s = kmalloc(sizeof(*s), GFP_KERNEL);
486         if (!s)
487                 return -ENOMEM;
488
489         trace_seq_init(s);
490
491         filter_print_preds(call, s);
492         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
493
494         kfree(s);
495
496         return r;
497 }
498
499 static ssize_t
500 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
501                    loff_t *ppos)
502 {
503         struct ftrace_event_call *call = filp->private_data;
504         char buf[64], *pbuf = buf;
505         struct filter_pred *pred;
506         int err;
507
508         if (cnt >= sizeof(buf))
509                 return -EINVAL;
510
511         if (copy_from_user(&buf, ubuf, cnt))
512                 return -EFAULT;
513         buf[cnt] = '\0';
514
515         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
516         if (!pred)
517                 return -ENOMEM;
518
519         err = filter_parse(&pbuf, pred);
520         if (err < 0) {
521                 filter_free_pred(pred);
522                 return err;
523         }
524
525         if (pred->clear) {
526                 filter_disable_preds(call);
527                 filter_free_pred(pred);
528                 return cnt;
529         }
530
531         err = filter_add_pred(call, pred);
532         if (err < 0) {
533                 filter_free_pred(pred);
534                 return err;
535         }
536
537         filter_free_pred(pred);
538
539         *ppos += cnt;
540
541         return cnt;
542 }
543
544 static ssize_t
545 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
546                       loff_t *ppos)
547 {
548         struct event_subsystem *system = filp->private_data;
549         struct trace_seq *s;
550         int r;
551
552         if (*ppos)
553                 return 0;
554
555         s = kmalloc(sizeof(*s), GFP_KERNEL);
556         if (!s)
557                 return -ENOMEM;
558
559         trace_seq_init(s);
560
561         filter_print_subsystem_preds(system, s);
562         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
563
564         kfree(s);
565
566         return r;
567 }
568
569 static ssize_t
570 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
571                        loff_t *ppos)
572 {
573         struct event_subsystem *system = filp->private_data;
574         char buf[64], *pbuf = buf;
575         struct filter_pred *pred;
576         int err;
577
578         if (cnt >= sizeof(buf))
579                 return -EINVAL;
580
581         if (copy_from_user(&buf, ubuf, cnt))
582                 return -EFAULT;
583         buf[cnt] = '\0';
584
585         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
586         if (!pred)
587                 return -ENOMEM;
588
589         err = filter_parse(&pbuf, pred);
590         if (err < 0) {
591                 filter_free_pred(pred);
592                 return err;
593         }
594
595         if (pred->clear) {
596                 filter_free_subsystem_preds(system);
597                 filter_free_pred(pred);
598                 return cnt;
599         }
600
601         err = filter_add_subsystem_pred(system, pred);
602         if (err < 0) {
603                 filter_free_subsystem_preds(system);
604                 filter_free_pred(pred);
605                 return err;
606         }
607
608         *ppos += cnt;
609
610         return cnt;
611 }
612
613 static ssize_t
614 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
615 {
616         int (*func)(struct trace_seq *s) = filp->private_data;
617         struct trace_seq *s;
618         int r;
619
620         if (*ppos)
621                 return 0;
622
623         s = kmalloc(sizeof(*s), GFP_KERNEL);
624         if (!s)
625                 return -ENOMEM;
626
627         trace_seq_init(s);
628
629         func(s);
630         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
631
632         kfree(s);
633
634         return r;
635 }
636
637 static const struct seq_operations show_event_seq_ops = {
638         .start = t_start,
639         .next = t_next,
640         .show = t_show,
641         .stop = t_stop,
642 };
643
644 static const struct seq_operations show_set_event_seq_ops = {
645         .start = s_start,
646         .next = s_next,
647         .show = t_show,
648         .stop = t_stop,
649 };
650
651 static const struct file_operations ftrace_avail_fops = {
652         .open = ftrace_event_seq_open,
653         .read = seq_read,
654         .llseek = seq_lseek,
655         .release = seq_release,
656 };
657
658 static const struct file_operations ftrace_set_event_fops = {
659         .open = ftrace_event_seq_open,
660         .read = seq_read,
661         .write = ftrace_event_write,
662         .llseek = seq_lseek,
663         .release = seq_release,
664 };
665
666 static const struct file_operations ftrace_enable_fops = {
667         .open = tracing_open_generic,
668         .read = event_enable_read,
669         .write = event_enable_write,
670 };
671
672 static const struct file_operations ftrace_event_format_fops = {
673         .open = tracing_open_generic,
674         .read = event_format_read,
675 };
676
677 static const struct file_operations ftrace_event_id_fops = {
678         .open = tracing_open_generic,
679         .read = event_id_read,
680 };
681
682 static const struct file_operations ftrace_event_filter_fops = {
683         .open = tracing_open_generic,
684         .read = event_filter_read,
685         .write = event_filter_write,
686 };
687
688 static const struct file_operations ftrace_subsystem_filter_fops = {
689         .open = tracing_open_generic,
690         .read = subsystem_filter_read,
691         .write = subsystem_filter_write,
692 };
693
694 static const struct file_operations ftrace_show_header_fops = {
695         .open = tracing_open_generic,
696         .read = show_header,
697 };
698
699 static struct dentry *event_trace_events_dir(void)
700 {
701         static struct dentry *d_tracer;
702         static struct dentry *d_events;
703
704         if (d_events)
705                 return d_events;
706
707         d_tracer = tracing_init_dentry();
708         if (!d_tracer)
709                 return NULL;
710
711         d_events = debugfs_create_dir("events", d_tracer);
712         if (!d_events)
713                 pr_warning("Could not create debugfs "
714                            "'events' directory\n");
715
716         return d_events;
717 }
718
719 static LIST_HEAD(event_subsystems);
720
721 static struct dentry *
722 event_subsystem_dir(const char *name, struct dentry *d_events)
723 {
724         struct event_subsystem *system;
725         struct dentry *entry;
726
727         /* First see if we did not already create this dir */
728         list_for_each_entry(system, &event_subsystems, list) {
729                 if (strcmp(system->name, name) == 0)
730                         return system->entry;
731         }
732
733         /* need to create new entry */
734         system = kmalloc(sizeof(*system), GFP_KERNEL);
735         if (!system) {
736                 pr_warning("No memory to create event subsystem %s\n",
737                            name);
738                 return d_events;
739         }
740
741         system->entry = debugfs_create_dir(name, d_events);
742         if (!system->entry) {
743                 pr_warning("Could not create event subsystem %s\n",
744                            name);
745                 kfree(system);
746                 return d_events;
747         }
748
749         system->name = kstrdup(name, GFP_KERNEL);
750         if (!system->name) {
751                 debugfs_remove(system->entry);
752                 kfree(system);
753                 return d_events;
754         }
755
756         list_add(&system->list, &event_subsystems);
757
758         system->preds = NULL;
759         system->n_preds = 0;
760
761         entry = debugfs_create_file("filter", 0644, system->entry, system,
762                                     &ftrace_subsystem_filter_fops);
763         if (!entry)
764                 pr_warning("Could not create debugfs "
765                            "'%s/filter' entry\n", name);
766
767         return system->entry;
768 }
769
770 static int
771 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
772 {
773         struct dentry *entry;
774         int ret;
775
776         /*
777          * If the trace point header did not define TRACE_SYSTEM
778          * then the system would be called "TRACE_SYSTEM".
779          */
780         if (strcmp(call->system, TRACE_SYSTEM) != 0)
781                 d_events = event_subsystem_dir(call->system, d_events);
782
783         if (call->raw_init) {
784                 ret = call->raw_init();
785                 if (ret < 0) {
786                         pr_warning("Could not initialize trace point"
787                                    " events/%s\n", call->name);
788                         return ret;
789                 }
790         }
791
792         call->dir = debugfs_create_dir(call->name, d_events);
793         if (!call->dir) {
794                 pr_warning("Could not create debugfs "
795                            "'%s' directory\n", call->name);
796                 return -1;
797         }
798
799         if (call->regfunc)
800                 entry = trace_create_file("enable", 0644, call->dir, call,
801                                           &ftrace_enable_fops);
802
803         if (call->id)
804                 entry = trace_create_file("id", 0444, call->dir, call,
805                                           &ftrace_event_id_fops);
806
807         if (call->define_fields) {
808                 ret = call->define_fields();
809                 if (ret < 0) {
810                         pr_warning("Could not initialize trace point"
811                                    " events/%s\n", call->name);
812                         return ret;
813                 }
814                 entry = trace_create_file("filter", 0644, call->dir, call,
815                                           &ftrace_event_filter_fops);
816         }
817
818         /* A trace may not want to export its format */
819         if (!call->show_format)
820                 return 0;
821
822         entry = trace_create_file("format", 0444, call->dir, call,
823                                   &ftrace_event_format_fops);
824
825         return 0;
826 }
827
828 #define for_each_event(event, start, end)                       \
829         for (event = start;                                     \
830              (unsigned long)event < (unsigned long)end;         \
831              event++)
832
833 #ifdef CONFIG_MODULES
834 static void trace_module_add_events(struct module *mod)
835 {
836         struct ftrace_event_call *call, *start, *end;
837         struct dentry *d_events;
838
839         start = mod->trace_events;
840         end = mod->trace_events + mod->num_trace_events;
841
842         if (start == end)
843                 return;
844
845         d_events = event_trace_events_dir();
846         if (!d_events)
847                 return;
848
849         for_each_event(call, start, end) {
850                 /* The linker may leave blanks */
851                 if (!call->name)
852                         continue;
853                 call->mod = mod;
854                 list_add(&call->list, &ftrace_events);
855                 event_create_dir(call, d_events);
856         }
857 }
858
859 static void trace_module_remove_events(struct module *mod)
860 {
861         struct ftrace_event_call *call, *p;
862
863         list_for_each_entry_safe(call, p, &ftrace_events, list) {
864                 if (call->mod == mod) {
865                         if (call->enabled) {
866                                 call->enabled = 0;
867                                 call->unregfunc();
868                         }
869                         if (call->event)
870                                 unregister_ftrace_event(call->event);
871                         debugfs_remove_recursive(call->dir);
872                         list_del(&call->list);
873                 }
874         }
875 }
876
877 static int trace_module_notify(struct notifier_block *self,
878                                unsigned long val, void *data)
879 {
880         struct module *mod = data;
881
882         mutex_lock(&event_mutex);
883         switch (val) {
884         case MODULE_STATE_COMING:
885                 trace_module_add_events(mod);
886                 break;
887         case MODULE_STATE_GOING:
888                 trace_module_remove_events(mod);
889                 break;
890         }
891         mutex_unlock(&event_mutex);
892
893         return 0;
894 }
895 #else
896 static int trace_module_notify(struct notifier_block *self,
897                                unsigned long val, void *data)
898 {
899         return 0;
900 }
901 #endif /* CONFIG_MODULES */
902
903 struct notifier_block trace_module_nb = {
904         .notifier_call = trace_module_notify,
905         .priority = 0,
906 };
907
908 extern struct ftrace_event_call __start_ftrace_events[];
909 extern struct ftrace_event_call __stop_ftrace_events[];
910
911 static __init int event_trace_init(void)
912 {
913         struct ftrace_event_call *call;
914         struct dentry *d_tracer;
915         struct dentry *entry;
916         struct dentry *d_events;
917         int ret;
918
919         d_tracer = tracing_init_dentry();
920         if (!d_tracer)
921                 return 0;
922
923         entry = debugfs_create_file("available_events", 0444, d_tracer,
924                                     (void *)&show_event_seq_ops,
925                                     &ftrace_avail_fops);
926         if (!entry)
927                 pr_warning("Could not create debugfs "
928                            "'available_events' entry\n");
929
930         entry = debugfs_create_file("set_event", 0644, d_tracer,
931                                     (void *)&show_set_event_seq_ops,
932                                     &ftrace_set_event_fops);
933         if (!entry)
934                 pr_warning("Could not create debugfs "
935                            "'set_event' entry\n");
936
937         d_events = event_trace_events_dir();
938         if (!d_events)
939                 return 0;
940
941         /* ring buffer internal formats */
942         trace_create_file("header_page", 0444, d_events,
943                           ring_buffer_print_page_header,
944                           &ftrace_show_header_fops);
945
946         trace_create_file("header_event", 0444, d_events,
947                           ring_buffer_print_entry_header,
948                           &ftrace_show_header_fops);
949
950         for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
951                 /* The linker may leave blanks */
952                 if (!call->name)
953                         continue;
954                 list_add(&call->list, &ftrace_events);
955                 event_create_dir(call, d_events);
956         }
957
958         ret = register_module_notifier(&trace_module_nb);
959         if (!ret)
960                 pr_warning("Failed to register trace events module notifier\n");
961
962         return 0;
963 }
964 fs_initcall(event_trace_init);
965
966 #ifdef CONFIG_FTRACE_STARTUP_TEST
967
968 static DEFINE_SPINLOCK(test_spinlock);
969 static DEFINE_SPINLOCK(test_spinlock_irq);
970 static DEFINE_MUTEX(test_mutex);
971
972 static __init void test_work(struct work_struct *dummy)
973 {
974         spin_lock(&test_spinlock);
975         spin_lock_irq(&test_spinlock_irq);
976         udelay(1);
977         spin_unlock_irq(&test_spinlock_irq);
978         spin_unlock(&test_spinlock);
979
980         mutex_lock(&test_mutex);
981         msleep(1);
982         mutex_unlock(&test_mutex);
983 }
984
985 static __init int event_test_thread(void *unused)
986 {
987         void *test_malloc;
988
989         test_malloc = kmalloc(1234, GFP_KERNEL);
990         if (!test_malloc)
991                 pr_info("failed to kmalloc\n");
992
993         schedule_on_each_cpu(test_work);
994
995         kfree(test_malloc);
996
997         set_current_state(TASK_INTERRUPTIBLE);
998         while (!kthread_should_stop())
999                 schedule();
1000
1001         return 0;
1002 }
1003
1004 /*
1005  * Do various things that may trigger events.
1006  */
1007 static __init void event_test_stuff(void)
1008 {
1009         struct task_struct *test_thread;
1010
1011         test_thread = kthread_run(event_test_thread, NULL, "test-events");
1012         msleep(1);
1013         kthread_stop(test_thread);
1014 }
1015
1016 /*
1017  * For every trace event defined, we will test each trace point separately,
1018  * and then by groups, and finally all trace points.
1019  */
1020 static __init void event_trace_self_tests(void)
1021 {
1022         struct ftrace_event_call *call;
1023         struct event_subsystem *system;
1024         char *sysname;
1025         int ret;
1026
1027         pr_info("Running tests on trace events:\n");
1028
1029         list_for_each_entry(call, &ftrace_events, list) {
1030
1031                 /* Only test those that have a regfunc */
1032                 if (!call->regfunc)
1033                         continue;
1034
1035                 pr_info("Testing event %s: ", call->name);
1036
1037                 /*
1038                  * If an event is already enabled, someone is using
1039                  * it and the self test should not be on.
1040                  */
1041                 if (call->enabled) {
1042                         pr_warning("Enabled event during self test!\n");
1043                         WARN_ON_ONCE(1);
1044                         continue;
1045                 }
1046
1047                 call->enabled = 1;
1048                 call->regfunc();
1049
1050                 event_test_stuff();
1051
1052                 call->unregfunc();
1053                 call->enabled = 0;
1054
1055                 pr_cont("OK\n");
1056         }
1057
1058         /* Now test at the sub system level */
1059
1060         pr_info("Running tests on trace event systems:\n");
1061
1062         list_for_each_entry(system, &event_subsystems, list) {
1063
1064                 /* the ftrace system is special, skip it */
1065                 if (strcmp(system->name, "ftrace") == 0)
1066                         continue;
1067
1068                 pr_info("Testing event system %s: ", system->name);
1069
1070                 /* ftrace_set_clr_event can modify the name passed in. */
1071                 sysname = kstrdup(system->name, GFP_KERNEL);
1072                 if (WARN_ON(!sysname)) {
1073                         pr_warning("Can't allocate memory, giving up!\n");
1074                         return;
1075                 }
1076                 ret = ftrace_set_clr_event(sysname, 1);
1077                 kfree(sysname);
1078                 if (WARN_ON_ONCE(ret)) {
1079                         pr_warning("error enabling system %s\n",
1080                                    system->name);
1081                         continue;
1082                 }
1083
1084                 event_test_stuff();
1085
1086                 sysname = kstrdup(system->name, GFP_KERNEL);
1087                 if (WARN_ON(!sysname)) {
1088                         pr_warning("Can't allocate memory, giving up!\n");
1089                         return;
1090                 }
1091                 ret = ftrace_set_clr_event(sysname, 0);
1092                 kfree(sysname);
1093
1094                 if (WARN_ON_ONCE(ret))
1095                         pr_warning("error disabling system %s\n",
1096                                    system->name);
1097
1098                 pr_cont("OK\n");
1099         }
1100
1101         /* Test with all events enabled */
1102
1103         pr_info("Running tests on all trace events:\n");
1104         pr_info("Testing all events: ");
1105
1106         sysname = kmalloc(4, GFP_KERNEL);
1107         if (WARN_ON(!sysname)) {
1108                 pr_warning("Can't allocate memory, giving up!\n");
1109                 return;
1110         }
1111         memcpy(sysname, "*:*", 4);
1112         ret = ftrace_set_clr_event(sysname, 1);
1113         if (WARN_ON_ONCE(ret)) {
1114                 kfree(sysname);
1115                 pr_warning("error enabling all events\n");
1116                 return;
1117         }
1118
1119         event_test_stuff();
1120
1121         /* reset sysname */
1122         memcpy(sysname, "*:*", 4);
1123         ret = ftrace_set_clr_event(sysname, 0);
1124         kfree(sysname);
1125
1126         if (WARN_ON_ONCE(ret)) {
1127                 pr_warning("error disabling all events\n");
1128                 return;
1129         }
1130
1131         pr_cont("OK\n");
1132 }
1133
1134 #ifdef CONFIG_FUNCTION_TRACER
1135
1136 static DEFINE_PER_CPU(atomic_t, test_event_disable);
1137
1138 static void
1139 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1140 {
1141         struct ring_buffer_event *event;
1142         struct ftrace_entry *entry;
1143         unsigned long flags;
1144         long disabled;
1145         int resched;
1146         int cpu;
1147         int pc;
1148
1149         pc = preempt_count();
1150         resched = ftrace_preempt_disable();
1151         cpu = raw_smp_processor_id();
1152         disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1153
1154         if (disabled != 1)
1155                 goto out;
1156
1157         local_save_flags(flags);
1158
1159         event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1160                                                   flags, pc);
1161         if (!event)
1162                 goto out;
1163         entry   = ring_buffer_event_data(event);
1164         entry->ip                       = ip;
1165         entry->parent_ip                = parent_ip;
1166
1167         trace_current_buffer_unlock_commit(event, flags, pc);
1168
1169  out:
1170         atomic_dec(&per_cpu(test_event_disable, cpu));
1171         ftrace_preempt_enable(resched);
1172 }
1173
1174 static struct ftrace_ops trace_ops __initdata  =
1175 {
1176         .func = function_test_events_call,
1177 };
1178
1179 static __init void event_trace_self_test_with_function(void)
1180 {
1181         register_ftrace_function(&trace_ops);
1182         pr_info("Running tests again, along with the function tracer\n");
1183         event_trace_self_tests();
1184         unregister_ftrace_function(&trace_ops);
1185 }
1186 #else
1187 static __init void event_trace_self_test_with_function(void)
1188 {
1189 }
1190 #endif
1191
1192 static __init int event_trace_self_tests_init(void)
1193 {
1194
1195         event_trace_self_tests();
1196
1197         event_trace_self_test_with_function();
1198
1199         return 0;
1200 }
1201
1202 late_initcall(event_trace_self_tests);
1203
1204 #endif