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