tracing: Use defined fields and print_fmt to print formats
[safe/jmp/linux-2.6] / kernel / trace / trace_events.c
1 /*
2  * event tracer
3  *
4  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5  *
6  *  - Added format output of fields of the trace point.
7  *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8  *
9  */
10
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/delay.h>
19
20 #include <asm/setup.h>
21
22 #include "trace_output.h"
23
24 #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 extern char *__bad_type_size(void);
524
525 #undef FIELD
526 #define FIELD(type, name)                                               \
527         sizeof(type) != sizeof(field.name) ? __bad_type_size() :        \
528         #type, "common_" #name, offsetof(typeof(field), name),          \
529                 sizeof(field.name), is_signed_type(type)
530
531 static ssize_t
532 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
533                   loff_t *ppos)
534 {
535         struct ftrace_event_call *call = filp->private_data;
536         struct ftrace_event_field *field;
537         struct trace_seq *s;
538         int common_field_count = 5;
539         char *buf;
540         int r = 0;
541
542         if (*ppos)
543                 return 0;
544
545         s = kmalloc(sizeof(*s), GFP_KERNEL);
546         if (!s)
547                 return -ENOMEM;
548
549         trace_seq_init(s);
550
551         trace_seq_printf(s, "name: %s\n", call->name);
552         trace_seq_printf(s, "ID: %d\n", call->id);
553         trace_seq_printf(s, "format:\n");
554
555         list_for_each_entry_reverse(field, &call->fields, link) {
556                 /*
557                  * Smartly shows the array type(except dynamic array).
558                  * Normal:
559                  *      field:TYPE VAR
560                  * If TYPE := TYPE[LEN], it is shown:
561                  *      field:TYPE VAR[LEN]
562                  */
563                 const char *array_descriptor = strchr(field->type, '[');
564
565                 if (!strncmp(field->type, "__data_loc", 10))
566                         array_descriptor = NULL;
567
568                 if (!array_descriptor) {
569                         r = trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
570                                         "\tsize:%u;\tsigned:%d;\n",
571                                         field->type, field->name, field->offset,
572                                         field->size, !!field->is_signed);
573                 } else {
574                         r = trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
575                                         "\tsize:%u;\tsigned:%d;\n",
576                                         (int)(array_descriptor - field->type),
577                                         field->type, field->name,
578                                         array_descriptor, field->offset,
579                                         field->size, !!field->is_signed);
580                 }
581
582                 if (--common_field_count == 0)
583                         r = trace_seq_printf(s, "\n");
584
585                 if (!r)
586                         break;
587         }
588
589         if (r)
590                 r = trace_seq_printf(s, "\nprint fmt: %s\n",
591                                 call->print_fmt);
592
593         if (!r) {
594                 /*
595                  * ug!  The format output is bigger than a PAGE!!
596                  */
597                 buf = "FORMAT TOO BIG\n";
598                 r = simple_read_from_buffer(ubuf, cnt, ppos,
599                                               buf, strlen(buf));
600                 goto out;
601         }
602
603         r = simple_read_from_buffer(ubuf, cnt, ppos,
604                                     s->buffer, s->len);
605  out:
606         kfree(s);
607         return r;
608 }
609
610 static ssize_t
611 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
612 {
613         struct ftrace_event_call *call = filp->private_data;
614         struct trace_seq *s;
615         int r;
616
617         if (*ppos)
618                 return 0;
619
620         s = kmalloc(sizeof(*s), GFP_KERNEL);
621         if (!s)
622                 return -ENOMEM;
623
624         trace_seq_init(s);
625         trace_seq_printf(s, "%d\n", call->id);
626
627         r = simple_read_from_buffer(ubuf, cnt, ppos,
628                                     s->buffer, s->len);
629         kfree(s);
630         return r;
631 }
632
633 static ssize_t
634 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
635                   loff_t *ppos)
636 {
637         struct ftrace_event_call *call = filp->private_data;
638         struct trace_seq *s;
639         int r;
640
641         if (*ppos)
642                 return 0;
643
644         s = kmalloc(sizeof(*s), GFP_KERNEL);
645         if (!s)
646                 return -ENOMEM;
647
648         trace_seq_init(s);
649
650         print_event_filter(call, s);
651         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
652
653         kfree(s);
654
655         return r;
656 }
657
658 static ssize_t
659 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
660                    loff_t *ppos)
661 {
662         struct ftrace_event_call *call = filp->private_data;
663         char *buf;
664         int err;
665
666         if (cnt >= PAGE_SIZE)
667                 return -EINVAL;
668
669         buf = (char *)__get_free_page(GFP_TEMPORARY);
670         if (!buf)
671                 return -ENOMEM;
672
673         if (copy_from_user(buf, ubuf, cnt)) {
674                 free_page((unsigned long) buf);
675                 return -EFAULT;
676         }
677         buf[cnt] = '\0';
678
679         err = apply_event_filter(call, buf);
680         free_page((unsigned long) buf);
681         if (err < 0)
682                 return err;
683
684         *ppos += cnt;
685
686         return cnt;
687 }
688
689 static ssize_t
690 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
691                       loff_t *ppos)
692 {
693         struct event_subsystem *system = filp->private_data;
694         struct trace_seq *s;
695         int r;
696
697         if (*ppos)
698                 return 0;
699
700         s = kmalloc(sizeof(*s), GFP_KERNEL);
701         if (!s)
702                 return -ENOMEM;
703
704         trace_seq_init(s);
705
706         print_subsystem_event_filter(system, s);
707         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
708
709         kfree(s);
710
711         return r;
712 }
713
714 static ssize_t
715 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
716                        loff_t *ppos)
717 {
718         struct event_subsystem *system = filp->private_data;
719         char *buf;
720         int err;
721
722         if (cnt >= PAGE_SIZE)
723                 return -EINVAL;
724
725         buf = (char *)__get_free_page(GFP_TEMPORARY);
726         if (!buf)
727                 return -ENOMEM;
728
729         if (copy_from_user(buf, ubuf, cnt)) {
730                 free_page((unsigned long) buf);
731                 return -EFAULT;
732         }
733         buf[cnt] = '\0';
734
735         err = apply_subsystem_event_filter(system, buf);
736         free_page((unsigned long) buf);
737         if (err < 0)
738                 return err;
739
740         *ppos += cnt;
741
742         return cnt;
743 }
744
745 static ssize_t
746 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
747 {
748         int (*func)(struct trace_seq *s) = filp->private_data;
749         struct trace_seq *s;
750         int r;
751
752         if (*ppos)
753                 return 0;
754
755         s = kmalloc(sizeof(*s), GFP_KERNEL);
756         if (!s)
757                 return -ENOMEM;
758
759         trace_seq_init(s);
760
761         func(s);
762         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
763
764         kfree(s);
765
766         return r;
767 }
768
769 static const struct seq_operations show_event_seq_ops = {
770         .start = t_start,
771         .next = t_next,
772         .show = t_show,
773         .stop = t_stop,
774 };
775
776 static const struct seq_operations show_set_event_seq_ops = {
777         .start = s_start,
778         .next = s_next,
779         .show = t_show,
780         .stop = t_stop,
781 };
782
783 static const struct file_operations ftrace_avail_fops = {
784         .open = ftrace_event_seq_open,
785         .read = seq_read,
786         .llseek = seq_lseek,
787         .release = seq_release,
788 };
789
790 static const struct file_operations ftrace_set_event_fops = {
791         .open = ftrace_event_seq_open,
792         .read = seq_read,
793         .write = ftrace_event_write,
794         .llseek = seq_lseek,
795         .release = seq_release,
796 };
797
798 static const struct file_operations ftrace_enable_fops = {
799         .open = tracing_open_generic,
800         .read = event_enable_read,
801         .write = event_enable_write,
802 };
803
804 static const struct file_operations ftrace_event_format_fops = {
805         .open = tracing_open_generic,
806         .read = event_format_read,
807 };
808
809 static const struct file_operations ftrace_event_id_fops = {
810         .open = tracing_open_generic,
811         .read = event_id_read,
812 };
813
814 static const struct file_operations ftrace_event_filter_fops = {
815         .open = tracing_open_generic,
816         .read = event_filter_read,
817         .write = event_filter_write,
818 };
819
820 static const struct file_operations ftrace_subsystem_filter_fops = {
821         .open = tracing_open_generic,
822         .read = subsystem_filter_read,
823         .write = subsystem_filter_write,
824 };
825
826 static const struct file_operations ftrace_system_enable_fops = {
827         .open = tracing_open_generic,
828         .read = system_enable_read,
829         .write = system_enable_write,
830 };
831
832 static const struct file_operations ftrace_show_header_fops = {
833         .open = tracing_open_generic,
834         .read = show_header,
835 };
836
837 static struct dentry *event_trace_events_dir(void)
838 {
839         static struct dentry *d_tracer;
840         static struct dentry *d_events;
841
842         if (d_events)
843                 return d_events;
844
845         d_tracer = tracing_init_dentry();
846         if (!d_tracer)
847                 return NULL;
848
849         d_events = debugfs_create_dir("events", d_tracer);
850         if (!d_events)
851                 pr_warning("Could not create debugfs "
852                            "'events' directory\n");
853
854         return d_events;
855 }
856
857 static LIST_HEAD(event_subsystems);
858
859 static struct dentry *
860 event_subsystem_dir(const char *name, struct dentry *d_events)
861 {
862         struct event_subsystem *system;
863         struct dentry *entry;
864
865         /* First see if we did not already create this dir */
866         list_for_each_entry(system, &event_subsystems, list) {
867                 if (strcmp(system->name, name) == 0) {
868                         system->nr_events++;
869                         return system->entry;
870                 }
871         }
872
873         /* need to create new entry */
874         system = kmalloc(sizeof(*system), GFP_KERNEL);
875         if (!system) {
876                 pr_warning("No memory to create event subsystem %s\n",
877                            name);
878                 return d_events;
879         }
880
881         system->entry = debugfs_create_dir(name, d_events);
882         if (!system->entry) {
883                 pr_warning("Could not create event subsystem %s\n",
884                            name);
885                 kfree(system);
886                 return d_events;
887         }
888
889         system->nr_events = 1;
890         system->name = kstrdup(name, GFP_KERNEL);
891         if (!system->name) {
892                 debugfs_remove(system->entry);
893                 kfree(system);
894                 return d_events;
895         }
896
897         list_add(&system->list, &event_subsystems);
898
899         system->filter = NULL;
900
901         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
902         if (!system->filter) {
903                 pr_warning("Could not allocate filter for subsystem "
904                            "'%s'\n", name);
905                 return system->entry;
906         }
907
908         entry = debugfs_create_file("filter", 0644, system->entry, system,
909                                     &ftrace_subsystem_filter_fops);
910         if (!entry) {
911                 kfree(system->filter);
912                 system->filter = NULL;
913                 pr_warning("Could not create debugfs "
914                            "'%s/filter' entry\n", name);
915         }
916
917         trace_create_file("enable", 0644, system->entry,
918                           (void *)system->name,
919                           &ftrace_system_enable_fops);
920
921         return system->entry;
922 }
923
924 static int
925 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
926                  const struct file_operations *id,
927                  const struct file_operations *enable,
928                  const struct file_operations *filter,
929                  const struct file_operations *format)
930 {
931         int ret;
932
933         /*
934          * If the trace point header did not define TRACE_SYSTEM
935          * then the system would be called "TRACE_SYSTEM".
936          */
937         if (strcmp(call->system, TRACE_SYSTEM) != 0)
938                 d_events = event_subsystem_dir(call->system, d_events);
939
940         call->dir = debugfs_create_dir(call->name, d_events);
941         if (!call->dir) {
942                 pr_warning("Could not create debugfs "
943                            "'%s' directory\n", call->name);
944                 return -1;
945         }
946
947         if (call->regfunc)
948                 trace_create_file("enable", 0644, call->dir, call,
949                                   enable);
950
951         if (call->id && call->profile_enable)
952                 trace_create_file("id", 0444, call->dir, call,
953                                   id);
954
955         if (call->define_fields) {
956                 ret = trace_define_common_fields(call);
957                 if (!ret)
958                         ret = call->define_fields(call);
959                 if (ret < 0) {
960                         pr_warning("Could not initialize trace point"
961                                    " events/%s\n", call->name);
962                         return ret;
963                 }
964                 trace_create_file("filter", 0644, call->dir, call,
965                                   filter);
966         }
967
968         /* A trace may not want to export its format */
969         if (!call->show_format)
970                 return 0;
971
972         trace_create_file("format", 0444, call->dir, call,
973                           format);
974
975         return 0;
976 }
977
978 static int __trace_add_event_call(struct ftrace_event_call *call)
979 {
980         struct dentry *d_events;
981         int ret;
982
983         if (!call->name)
984                 return -EINVAL;
985
986         if (call->raw_init) {
987                 ret = call->raw_init(call);
988                 if (ret < 0) {
989                         if (ret != -ENOSYS)
990                                 pr_warning("Could not initialize trace "
991                                 "events/%s\n", call->name);
992                         return ret;
993                 }
994         }
995
996         d_events = event_trace_events_dir();
997         if (!d_events)
998                 return -ENOENT;
999
1000         ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1001                                 &ftrace_enable_fops, &ftrace_event_filter_fops,
1002                                 &ftrace_event_format_fops);
1003         if (!ret)
1004                 list_add(&call->list, &ftrace_events);
1005
1006         return ret;
1007 }
1008
1009 /* Add an additional event_call dynamically */
1010 int trace_add_event_call(struct ftrace_event_call *call)
1011 {
1012         int ret;
1013         mutex_lock(&event_mutex);
1014         ret = __trace_add_event_call(call);
1015         mutex_unlock(&event_mutex);
1016         return ret;
1017 }
1018
1019 static void remove_subsystem_dir(const char *name)
1020 {
1021         struct event_subsystem *system;
1022
1023         if (strcmp(name, TRACE_SYSTEM) == 0)
1024                 return;
1025
1026         list_for_each_entry(system, &event_subsystems, list) {
1027                 if (strcmp(system->name, name) == 0) {
1028                         if (!--system->nr_events) {
1029                                 struct event_filter *filter = system->filter;
1030
1031                                 debugfs_remove_recursive(system->entry);
1032                                 list_del(&system->list);
1033                                 if (filter) {
1034                                         kfree(filter->filter_string);
1035                                         kfree(filter);
1036                                 }
1037                                 kfree(system->name);
1038                                 kfree(system);
1039                         }
1040                         break;
1041                 }
1042         }
1043 }
1044
1045 /*
1046  * Must be called under locking both of event_mutex and trace_event_mutex.
1047  */
1048 static void __trace_remove_event_call(struct ftrace_event_call *call)
1049 {
1050         ftrace_event_enable_disable(call, 0);
1051         if (call->event)
1052                 __unregister_ftrace_event(call->event);
1053         debugfs_remove_recursive(call->dir);
1054         list_del(&call->list);
1055         trace_destroy_fields(call);
1056         destroy_preds(call);
1057         remove_subsystem_dir(call->system);
1058 }
1059
1060 /* Remove an event_call */
1061 void trace_remove_event_call(struct ftrace_event_call *call)
1062 {
1063         mutex_lock(&event_mutex);
1064         down_write(&trace_event_mutex);
1065         __trace_remove_event_call(call);
1066         up_write(&trace_event_mutex);
1067         mutex_unlock(&event_mutex);
1068 }
1069
1070 #define for_each_event(event, start, end)                       \
1071         for (event = start;                                     \
1072              (unsigned long)event < (unsigned long)end;         \
1073              event++)
1074
1075 #ifdef CONFIG_MODULES
1076
1077 static LIST_HEAD(ftrace_module_file_list);
1078
1079 /*
1080  * Modules must own their file_operations to keep up with
1081  * reference counting.
1082  */
1083 struct ftrace_module_file_ops {
1084         struct list_head                list;
1085         struct module                   *mod;
1086         struct file_operations          id;
1087         struct file_operations          enable;
1088         struct file_operations          format;
1089         struct file_operations          filter;
1090 };
1091
1092 static struct ftrace_module_file_ops *
1093 trace_create_file_ops(struct module *mod)
1094 {
1095         struct ftrace_module_file_ops *file_ops;
1096
1097         /*
1098          * This is a bit of a PITA. To allow for correct reference
1099          * counting, modules must "own" their file_operations.
1100          * To do this, we allocate the file operations that will be
1101          * used in the event directory.
1102          */
1103
1104         file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1105         if (!file_ops)
1106                 return NULL;
1107
1108         file_ops->mod = mod;
1109
1110         file_ops->id = ftrace_event_id_fops;
1111         file_ops->id.owner = mod;
1112
1113         file_ops->enable = ftrace_enable_fops;
1114         file_ops->enable.owner = mod;
1115
1116         file_ops->filter = ftrace_event_filter_fops;
1117         file_ops->filter.owner = mod;
1118
1119         file_ops->format = ftrace_event_format_fops;
1120         file_ops->format.owner = mod;
1121
1122         list_add(&file_ops->list, &ftrace_module_file_list);
1123
1124         return file_ops;
1125 }
1126
1127 static void trace_module_add_events(struct module *mod)
1128 {
1129         struct ftrace_module_file_ops *file_ops = NULL;
1130         struct ftrace_event_call *call, *start, *end;
1131         struct dentry *d_events;
1132         int ret;
1133
1134         start = mod->trace_events;
1135         end = mod->trace_events + mod->num_trace_events;
1136
1137         if (start == end)
1138                 return;
1139
1140         d_events = event_trace_events_dir();
1141         if (!d_events)
1142                 return;
1143
1144         for_each_event(call, start, end) {
1145                 /* The linker may leave blanks */
1146                 if (!call->name)
1147                         continue;
1148                 if (call->raw_init) {
1149                         ret = call->raw_init(call);
1150                         if (ret < 0) {
1151                                 if (ret != -ENOSYS)
1152                                         pr_warning("Could not initialize trace "
1153                                         "point events/%s\n", call->name);
1154                                 continue;
1155                         }
1156                 }
1157                 /*
1158                  * This module has events, create file ops for this module
1159                  * if not already done.
1160                  */
1161                 if (!file_ops) {
1162                         file_ops = trace_create_file_ops(mod);
1163                         if (!file_ops)
1164                                 return;
1165                 }
1166                 call->mod = mod;
1167                 ret = event_create_dir(call, d_events,
1168                                        &file_ops->id, &file_ops->enable,
1169                                        &file_ops->filter, &file_ops->format);
1170                 if (!ret)
1171                         list_add(&call->list, &ftrace_events);
1172         }
1173 }
1174
1175 static void trace_module_remove_events(struct module *mod)
1176 {
1177         struct ftrace_module_file_ops *file_ops;
1178         struct ftrace_event_call *call, *p;
1179         bool found = false;
1180
1181         down_write(&trace_event_mutex);
1182         list_for_each_entry_safe(call, p, &ftrace_events, list) {
1183                 if (call->mod == mod) {
1184                         found = true;
1185                         __trace_remove_event_call(call);
1186                 }
1187         }
1188
1189         /* Now free the file_operations */
1190         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1191                 if (file_ops->mod == mod)
1192                         break;
1193         }
1194         if (&file_ops->list != &ftrace_module_file_list) {
1195                 list_del(&file_ops->list);
1196                 kfree(file_ops);
1197         }
1198
1199         /*
1200          * It is safest to reset the ring buffer if the module being unloaded
1201          * registered any events.
1202          */
1203         if (found)
1204                 tracing_reset_current_online_cpus();
1205         up_write(&trace_event_mutex);
1206 }
1207
1208 static int trace_module_notify(struct notifier_block *self,
1209                                unsigned long val, void *data)
1210 {
1211         struct module *mod = data;
1212
1213         mutex_lock(&event_mutex);
1214         switch (val) {
1215         case MODULE_STATE_COMING:
1216                 trace_module_add_events(mod);
1217                 break;
1218         case MODULE_STATE_GOING:
1219                 trace_module_remove_events(mod);
1220                 break;
1221         }
1222         mutex_unlock(&event_mutex);
1223
1224         return 0;
1225 }
1226 #else
1227 static int trace_module_notify(struct notifier_block *self,
1228                                unsigned long val, void *data)
1229 {
1230         return 0;
1231 }
1232 #endif /* CONFIG_MODULES */
1233
1234 static struct notifier_block trace_module_nb = {
1235         .notifier_call = trace_module_notify,
1236         .priority = 0,
1237 };
1238
1239 extern struct ftrace_event_call __start_ftrace_events[];
1240 extern struct ftrace_event_call __stop_ftrace_events[];
1241
1242 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1243
1244 static __init int setup_trace_event(char *str)
1245 {
1246         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1247         ring_buffer_expanded = 1;
1248         tracing_selftest_disabled = 1;
1249
1250         return 1;
1251 }
1252 __setup("trace_event=", setup_trace_event);
1253
1254 static __init int event_trace_init(void)
1255 {
1256         struct ftrace_event_call *call;
1257         struct dentry *d_tracer;
1258         struct dentry *entry;
1259         struct dentry *d_events;
1260         int ret;
1261         char *buf = bootup_event_buf;
1262         char *token;
1263
1264         d_tracer = tracing_init_dentry();
1265         if (!d_tracer)
1266                 return 0;
1267
1268         entry = debugfs_create_file("available_events", 0444, d_tracer,
1269                                     (void *)&show_event_seq_ops,
1270                                     &ftrace_avail_fops);
1271         if (!entry)
1272                 pr_warning("Could not create debugfs "
1273                            "'available_events' entry\n");
1274
1275         entry = debugfs_create_file("set_event", 0644, d_tracer,
1276                                     (void *)&show_set_event_seq_ops,
1277                                     &ftrace_set_event_fops);
1278         if (!entry)
1279                 pr_warning("Could not create debugfs "
1280                            "'set_event' entry\n");
1281
1282         d_events = event_trace_events_dir();
1283         if (!d_events)
1284                 return 0;
1285
1286         /* ring buffer internal formats */
1287         trace_create_file("header_page", 0444, d_events,
1288                           ring_buffer_print_page_header,
1289                           &ftrace_show_header_fops);
1290
1291         trace_create_file("header_event", 0444, d_events,
1292                           ring_buffer_print_entry_header,
1293                           &ftrace_show_header_fops);
1294
1295         trace_create_file("enable", 0644, d_events,
1296                           NULL, &ftrace_system_enable_fops);
1297
1298         for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1299                 /* The linker may leave blanks */
1300                 if (!call->name)
1301                         continue;
1302                 if (call->raw_init) {
1303                         ret = call->raw_init(call);
1304                         if (ret < 0) {
1305                                 if (ret != -ENOSYS)
1306                                         pr_warning("Could not initialize trace "
1307                                         "point events/%s\n", call->name);
1308                                 continue;
1309                         }
1310                 }
1311                 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1312                                        &ftrace_enable_fops,
1313                                        &ftrace_event_filter_fops,
1314                                        &ftrace_event_format_fops);
1315                 if (!ret)
1316                         list_add(&call->list, &ftrace_events);
1317         }
1318
1319         while (true) {
1320                 token = strsep(&buf, ",");
1321
1322                 if (!token)
1323                         break;
1324                 if (!*token)
1325                         continue;
1326
1327                 ret = ftrace_set_clr_event(token, 1);
1328                 if (ret)
1329                         pr_warning("Failed to enable trace event: %s\n", token);
1330         }
1331
1332         ret = register_module_notifier(&trace_module_nb);
1333         if (ret)
1334                 pr_warning("Failed to register trace events module notifier\n");
1335
1336         return 0;
1337 }
1338 fs_initcall(event_trace_init);
1339
1340 #ifdef CONFIG_FTRACE_STARTUP_TEST
1341
1342 static DEFINE_SPINLOCK(test_spinlock);
1343 static DEFINE_SPINLOCK(test_spinlock_irq);
1344 static DEFINE_MUTEX(test_mutex);
1345
1346 static __init void test_work(struct work_struct *dummy)
1347 {
1348         spin_lock(&test_spinlock);
1349         spin_lock_irq(&test_spinlock_irq);
1350         udelay(1);
1351         spin_unlock_irq(&test_spinlock_irq);
1352         spin_unlock(&test_spinlock);
1353
1354         mutex_lock(&test_mutex);
1355         msleep(1);
1356         mutex_unlock(&test_mutex);
1357 }
1358
1359 static __init int event_test_thread(void *unused)
1360 {
1361         void *test_malloc;
1362
1363         test_malloc = kmalloc(1234, GFP_KERNEL);
1364         if (!test_malloc)
1365                 pr_info("failed to kmalloc\n");
1366
1367         schedule_on_each_cpu(test_work);
1368
1369         kfree(test_malloc);
1370
1371         set_current_state(TASK_INTERRUPTIBLE);
1372         while (!kthread_should_stop())
1373                 schedule();
1374
1375         return 0;
1376 }
1377
1378 /*
1379  * Do various things that may trigger events.
1380  */
1381 static __init void event_test_stuff(void)
1382 {
1383         struct task_struct *test_thread;
1384
1385         test_thread = kthread_run(event_test_thread, NULL, "test-events");
1386         msleep(1);
1387         kthread_stop(test_thread);
1388 }
1389
1390 /*
1391  * For every trace event defined, we will test each trace point separately,
1392  * and then by groups, and finally all trace points.
1393  */
1394 static __init void event_trace_self_tests(void)
1395 {
1396         struct ftrace_event_call *call;
1397         struct event_subsystem *system;
1398         int ret;
1399
1400         pr_info("Running tests on trace events:\n");
1401
1402         list_for_each_entry(call, &ftrace_events, list) {
1403
1404                 /* Only test those that have a regfunc */
1405                 if (!call->regfunc)
1406                         continue;
1407
1408 /*
1409  * Testing syscall events here is pretty useless, but
1410  * we still do it if configured. But this is time consuming.
1411  * What we really need is a user thread to perform the
1412  * syscalls as we test.
1413  */
1414 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1415                 if (call->system &&
1416                     strcmp(call->system, "syscalls") == 0)
1417                         continue;
1418 #endif
1419
1420                 pr_info("Testing event %s: ", call->name);
1421
1422                 /*
1423                  * If an event is already enabled, someone is using
1424                  * it and the self test should not be on.
1425                  */
1426                 if (call->enabled) {
1427                         pr_warning("Enabled event during self test!\n");
1428                         WARN_ON_ONCE(1);
1429                         continue;
1430                 }
1431
1432                 ftrace_event_enable_disable(call, 1);
1433                 event_test_stuff();
1434                 ftrace_event_enable_disable(call, 0);
1435
1436                 pr_cont("OK\n");
1437         }
1438
1439         /* Now test at the sub system level */
1440
1441         pr_info("Running tests on trace event systems:\n");
1442
1443         list_for_each_entry(system, &event_subsystems, list) {
1444
1445                 /* the ftrace system is special, skip it */
1446                 if (strcmp(system->name, "ftrace") == 0)
1447                         continue;
1448
1449                 pr_info("Testing event system %s: ", system->name);
1450
1451                 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1452                 if (WARN_ON_ONCE(ret)) {
1453                         pr_warning("error enabling system %s\n",
1454                                    system->name);
1455                         continue;
1456                 }
1457
1458                 event_test_stuff();
1459
1460                 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1461                 if (WARN_ON_ONCE(ret))
1462                         pr_warning("error disabling system %s\n",
1463                                    system->name);
1464
1465                 pr_cont("OK\n");
1466         }
1467
1468         /* Test with all events enabled */
1469
1470         pr_info("Running tests on all trace events:\n");
1471         pr_info("Testing all events: ");
1472
1473         ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1474         if (WARN_ON_ONCE(ret)) {
1475                 pr_warning("error enabling all events\n");
1476                 return;
1477         }
1478
1479         event_test_stuff();
1480
1481         /* reset sysname */
1482         ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1483         if (WARN_ON_ONCE(ret)) {
1484                 pr_warning("error disabling all events\n");
1485                 return;
1486         }
1487
1488         pr_cont("OK\n");
1489 }
1490
1491 #ifdef CONFIG_FUNCTION_TRACER
1492
1493 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1494
1495 static void
1496 function_test_events_call(unsigned long ip, unsigned long parent_ip)
1497 {
1498         struct ring_buffer_event *event;
1499         struct ring_buffer *buffer;
1500         struct ftrace_entry *entry;
1501         unsigned long flags;
1502         long disabled;
1503         int resched;
1504         int cpu;
1505         int pc;
1506
1507         pc = preempt_count();
1508         resched = ftrace_preempt_disable();
1509         cpu = raw_smp_processor_id();
1510         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1511
1512         if (disabled != 1)
1513                 goto out;
1514
1515         local_save_flags(flags);
1516
1517         event = trace_current_buffer_lock_reserve(&buffer,
1518                                                   TRACE_FN, sizeof(*entry),
1519                                                   flags, pc);
1520         if (!event)
1521                 goto out;
1522         entry   = ring_buffer_event_data(event);
1523         entry->ip                       = ip;
1524         entry->parent_ip                = parent_ip;
1525
1526         trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1527
1528  out:
1529         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1530         ftrace_preempt_enable(resched);
1531 }
1532
1533 static struct ftrace_ops trace_ops __initdata  =
1534 {
1535         .func = function_test_events_call,
1536 };
1537
1538 static __init void event_trace_self_test_with_function(void)
1539 {
1540         register_ftrace_function(&trace_ops);
1541         pr_info("Running tests again, along with the function tracer\n");
1542         event_trace_self_tests();
1543         unregister_ftrace_function(&trace_ops);
1544 }
1545 #else
1546 static __init void event_trace_self_test_with_function(void)
1547 {
1548 }
1549 #endif
1550
1551 static __init int event_trace_self_tests_init(void)
1552 {
1553         if (!tracing_selftest_disabled) {
1554                 event_trace_self_tests();
1555                 event_trace_self_test_with_function();
1556         }
1557
1558         return 0;
1559 }
1560
1561 late_initcall(event_trace_self_tests_init);
1562
1563 #endif