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