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