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