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