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