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