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