Merge branch 'linus' into core/softlockup
[linux-2.6] / kernel / trace / trace_events.c
1 /*
2  * event tracer
3  *
4  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5  *
6  *  - Added format output of fields of the trace point.
7  *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8  *
9  */
10
11 #include <linux/debugfs.h>
12 #include <linux/uaccess.h>
13 #include <linux/module.h>
14 #include <linux/ctype.h>
15
16 #include "trace_output.h"
17
18 #define TRACE_SYSTEM "TRACE_SYSTEM"
19
20 static DEFINE_MUTEX(event_mutex);
21
22 int trace_define_field(struct ftrace_event_call *call, char *type,
23                        char *name, int offset, int size)
24 {
25         struct ftrace_event_field *field;
26
27         field = kzalloc(sizeof(*field), GFP_KERNEL);
28         if (!field)
29                 goto err;
30
31         field->name = kstrdup(name, GFP_KERNEL);
32         if (!field->name)
33                 goto err;
34
35         field->type = kstrdup(type, GFP_KERNEL);
36         if (!field->type)
37                 goto err;
38
39         field->offset = offset;
40         field->size = size;
41         list_add(&field->link, &call->fields);
42
43         return 0;
44
45 err:
46         if (field) {
47                 kfree(field->name);
48                 kfree(field->type);
49         }
50         kfree(field);
51
52         return -ENOMEM;
53 }
54
55 static void ftrace_clear_events(void)
56 {
57         struct ftrace_event_call *call = (void *)__start_ftrace_events;
58
59
60         while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
61
62                 if (call->enabled) {
63                         call->enabled = 0;
64                         call->unregfunc();
65                 }
66                 call++;
67         }
68 }
69
70 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
71                                         int enable)
72 {
73
74         switch (enable) {
75         case 0:
76                 if (call->enabled) {
77                         call->enabled = 0;
78                         call->unregfunc();
79                 }
80                 break;
81         case 1:
82                 if (!call->enabled) {
83                         call->enabled = 1;
84                         call->regfunc();
85                 }
86                 break;
87         }
88 }
89
90 static int ftrace_set_clr_event(char *buf, int set)
91 {
92         struct ftrace_event_call *call = __start_ftrace_events;
93         char *event = NULL, *sub = NULL, *match;
94         int ret = -EINVAL;
95
96         /*
97          * The buf format can be <subsystem>:<event-name>
98          *  *:<event-name> means any event by that name.
99          *  :<event-name> is the same.
100          *
101          *  <subsystem>:* means all events in that subsystem
102          *  <subsystem>: means the same.
103          *
104          *  <name> (no ':') means all events in a subsystem with
105          *  the name <name> or any event that matches <name>
106          */
107
108         match = strsep(&buf, ":");
109         if (buf) {
110                 sub = match;
111                 event = buf;
112                 match = NULL;
113
114                 if (!strlen(sub) || strcmp(sub, "*") == 0)
115                         sub = NULL;
116                 if (!strlen(event) || strcmp(event, "*") == 0)
117                         event = NULL;
118         }
119
120         mutex_lock(&event_mutex);
121         for_each_event(call) {
122
123                 if (!call->name || !call->regfunc)
124                         continue;
125
126                 if (match &&
127                     strcmp(match, call->name) != 0 &&
128                     strcmp(match, call->system) != 0)
129                         continue;
130
131                 if (sub && strcmp(sub, call->system) != 0)
132                         continue;
133
134                 if (event && strcmp(event, call->name) != 0)
135                         continue;
136
137                 ftrace_event_enable_disable(call, set);
138
139                 ret = 0;
140         }
141         mutex_unlock(&event_mutex);
142
143         return ret;
144 }
145
146 /* 128 should be much more than enough */
147 #define EVENT_BUF_SIZE          127
148
149 static ssize_t
150 ftrace_event_write(struct file *file, const char __user *ubuf,
151                    size_t cnt, loff_t *ppos)
152 {
153         size_t read = 0;
154         int i, set = 1;
155         ssize_t ret;
156         char *buf;
157         char ch;
158
159         if (!cnt || cnt < 0)
160                 return 0;
161
162         ret = tracing_update_buffers();
163         if (ret < 0)
164                 return ret;
165
166         ret = get_user(ch, ubuf++);
167         if (ret)
168                 return ret;
169         read++;
170         cnt--;
171
172         /* skip white space */
173         while (cnt && isspace(ch)) {
174                 ret = get_user(ch, ubuf++);
175                 if (ret)
176                         return ret;
177                 read++;
178                 cnt--;
179         }
180
181         /* Only white space found? */
182         if (isspace(ch)) {
183                 file->f_pos += read;
184                 ret = read;
185                 return ret;
186         }
187
188         buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
189         if (!buf)
190                 return -ENOMEM;
191
192         if (cnt > EVENT_BUF_SIZE)
193                 cnt = EVENT_BUF_SIZE;
194
195         i = 0;
196         while (cnt && !isspace(ch)) {
197                 if (!i && ch == '!')
198                         set = 0;
199                 else
200                         buf[i++] = ch;
201
202                 ret = get_user(ch, ubuf++);
203                 if (ret)
204                         goto out_free;
205                 read++;
206                 cnt--;
207         }
208         buf[i] = 0;
209
210         file->f_pos += read;
211
212         ret = ftrace_set_clr_event(buf, set);
213         if (ret)
214                 goto out_free;
215
216         ret = read;
217
218  out_free:
219         kfree(buf);
220
221         return ret;
222 }
223
224 static void *
225 t_next(struct seq_file *m, void *v, loff_t *pos)
226 {
227         struct ftrace_event_call *call = m->private;
228         struct ftrace_event_call *next = call;
229
230         (*pos)++;
231
232         for (;;) {
233                 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
234                         return NULL;
235
236                 /*
237                  * The ftrace subsystem is for showing formats only.
238                  * They can not be enabled or disabled via the event files.
239                  */
240                 if (call->regfunc)
241                         break;
242
243                 call++;
244                 next = call;
245         }
246
247         m->private = ++next;
248
249         return call;
250 }
251
252 static void *t_start(struct seq_file *m, loff_t *pos)
253 {
254         return t_next(m, NULL, pos);
255 }
256
257 static void *
258 s_next(struct seq_file *m, void *v, loff_t *pos)
259 {
260         struct ftrace_event_call *call = m->private;
261         struct ftrace_event_call *next;
262
263         (*pos)++;
264
265  retry:
266         if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
267                 return NULL;
268
269         if (!call->enabled) {
270                 call++;
271                 goto retry;
272         }
273
274         next = call;
275         m->private = ++next;
276
277         return call;
278 }
279
280 static void *s_start(struct seq_file *m, loff_t *pos)
281 {
282         return s_next(m, NULL, pos);
283 }
284
285 static int t_show(struct seq_file *m, void *v)
286 {
287         struct ftrace_event_call *call = v;
288
289         if (strcmp(call->system, TRACE_SYSTEM) != 0)
290                 seq_printf(m, "%s:", call->system);
291         seq_printf(m, "%s\n", call->name);
292
293         return 0;
294 }
295
296 static void t_stop(struct seq_file *m, void *p)
297 {
298 }
299
300 static int
301 ftrace_event_seq_open(struct inode *inode, struct file *file)
302 {
303         int ret;
304         const struct seq_operations *seq_ops;
305
306         if ((file->f_mode & FMODE_WRITE) &&
307             !(file->f_flags & O_APPEND))
308                 ftrace_clear_events();
309
310         seq_ops = inode->i_private;
311         ret = seq_open(file, seq_ops);
312         if (!ret) {
313                 struct seq_file *m = file->private_data;
314
315                 m->private = __start_ftrace_events;
316         }
317         return ret;
318 }
319
320 static ssize_t
321 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
322                   loff_t *ppos)
323 {
324         struct ftrace_event_call *call = filp->private_data;
325         char *buf;
326
327         if (call->enabled)
328                 buf = "1\n";
329         else
330                 buf = "0\n";
331
332         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
333 }
334
335 static ssize_t
336 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
337                    loff_t *ppos)
338 {
339         struct ftrace_event_call *call = filp->private_data;
340         char buf[64];
341         unsigned long val;
342         int ret;
343
344         if (cnt >= sizeof(buf))
345                 return -EINVAL;
346
347         if (copy_from_user(&buf, ubuf, cnt))
348                 return -EFAULT;
349
350         buf[cnt] = 0;
351
352         ret = strict_strtoul(buf, 10, &val);
353         if (ret < 0)
354                 return ret;
355
356         ret = tracing_update_buffers();
357         if (ret < 0)
358                 return ret;
359
360         switch (val) {
361         case 0:
362         case 1:
363                 mutex_lock(&event_mutex);
364                 ftrace_event_enable_disable(call, val);
365                 mutex_unlock(&event_mutex);
366                 break;
367
368         default:
369                 return -EINVAL;
370         }
371
372         *ppos += cnt;
373
374         return cnt;
375 }
376
377 #undef FIELD
378 #define FIELD(type, name)                                               \
379         #type, "common_" #name, offsetof(typeof(field), name),          \
380                 sizeof(field.name)
381
382 static int trace_write_header(struct trace_seq *s)
383 {
384         struct trace_entry field;
385
386         /* struct trace_entry */
387         return trace_seq_printf(s,
388                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
389                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
390                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
391                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
392                                 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
393                                 "\n",
394                                 FIELD(unsigned char, type),
395                                 FIELD(unsigned char, flags),
396                                 FIELD(unsigned char, preempt_count),
397                                 FIELD(int, pid),
398                                 FIELD(int, tgid));
399 }
400
401 static ssize_t
402 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
403                   loff_t *ppos)
404 {
405         struct ftrace_event_call *call = filp->private_data;
406         struct trace_seq *s;
407         char *buf;
408         int r;
409
410         if (*ppos)
411                 return 0;
412
413         s = kmalloc(sizeof(*s), GFP_KERNEL);
414         if (!s)
415                 return -ENOMEM;
416
417         trace_seq_init(s);
418
419         /* If any of the first writes fail, so will the show_format. */
420
421         trace_seq_printf(s, "name: %s\n", call->name);
422         trace_seq_printf(s, "ID: %d\n", call->id);
423         trace_seq_printf(s, "format:\n");
424         trace_write_header(s);
425
426         r = call->show_format(s);
427         if (!r) {
428                 /*
429                  * ug!  The format output is bigger than a PAGE!!
430                  */
431                 buf = "FORMAT TOO BIG\n";
432                 r = simple_read_from_buffer(ubuf, cnt, ppos,
433                                               buf, strlen(buf));
434                 goto out;
435         }
436
437         r = simple_read_from_buffer(ubuf, cnt, ppos,
438                                     s->buffer, s->len);
439  out:
440         kfree(s);
441         return r;
442 }
443
444 static ssize_t
445 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
446 {
447         struct ftrace_event_call *call = filp->private_data;
448         struct trace_seq *s;
449         int r;
450
451         if (*ppos)
452                 return 0;
453
454         s = kmalloc(sizeof(*s), GFP_KERNEL);
455         if (!s)
456                 return -ENOMEM;
457
458         trace_seq_init(s);
459         trace_seq_printf(s, "%d\n", call->id);
460
461         r = simple_read_from_buffer(ubuf, cnt, ppos,
462                                     s->buffer, s->len);
463         kfree(s);
464         return r;
465 }
466
467 static ssize_t
468 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
469                   loff_t *ppos)
470 {
471         struct ftrace_event_call *call = filp->private_data;
472         struct trace_seq *s;
473         int r;
474
475         if (*ppos)
476                 return 0;
477
478         s = kmalloc(sizeof(*s), GFP_KERNEL);
479         if (!s)
480                 return -ENOMEM;
481
482         trace_seq_init(s);
483
484         filter_print_preds(call->preds, s);
485         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
486
487         kfree(s);
488
489         return r;
490 }
491
492 static ssize_t
493 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
494                    loff_t *ppos)
495 {
496         struct ftrace_event_call *call = filp->private_data;
497         char buf[64], *pbuf = buf;
498         struct filter_pred *pred;
499         int err;
500
501         if (cnt >= sizeof(buf))
502                 return -EINVAL;
503
504         if (copy_from_user(&buf, ubuf, cnt))
505                 return -EFAULT;
506
507         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
508         if (!pred)
509                 return -ENOMEM;
510
511         err = filter_parse(&pbuf, pred);
512         if (err < 0) {
513                 filter_free_pred(pred);
514                 return err;
515         }
516
517         if (pred->clear) {
518                 filter_free_preds(call);
519                 filter_free_pred(pred);
520                 return cnt;
521         }
522
523         if (filter_add_pred(call, pred)) {
524                 filter_free_pred(pred);
525                 return -EINVAL;
526         }
527
528         *ppos += cnt;
529
530         return cnt;
531 }
532
533 static ssize_t
534 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
535                       loff_t *ppos)
536 {
537         struct event_subsystem *system = filp->private_data;
538         struct trace_seq *s;
539         int r;
540
541         if (*ppos)
542                 return 0;
543
544         s = kmalloc(sizeof(*s), GFP_KERNEL);
545         if (!s)
546                 return -ENOMEM;
547
548         trace_seq_init(s);
549
550         filter_print_preds(system->preds, s);
551         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
552
553         kfree(s);
554
555         return r;
556 }
557
558 static ssize_t
559 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
560                        loff_t *ppos)
561 {
562         struct event_subsystem *system = filp->private_data;
563         char buf[64], *pbuf = buf;
564         struct filter_pred *pred;
565         int err;
566
567         if (cnt >= sizeof(buf))
568                 return -EINVAL;
569
570         if (copy_from_user(&buf, ubuf, cnt))
571                 return -EFAULT;
572
573         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
574         if (!pred)
575                 return -ENOMEM;
576
577         err = filter_parse(&pbuf, pred);
578         if (err < 0) {
579                 filter_free_pred(pred);
580                 return err;
581         }
582
583         if (pred->clear) {
584                 filter_free_subsystem_preds(system);
585                 filter_free_pred(pred);
586                 return cnt;
587         }
588
589         if (filter_add_subsystem_pred(system, pred)) {
590                 filter_free_subsystem_preds(system);
591                 filter_free_pred(pred);
592                 return -EINVAL;
593         }
594
595         *ppos += cnt;
596
597         return cnt;
598 }
599
600 static const struct seq_operations show_event_seq_ops = {
601         .start = t_start,
602         .next = t_next,
603         .show = t_show,
604         .stop = t_stop,
605 };
606
607 static const struct seq_operations show_set_event_seq_ops = {
608         .start = s_start,
609         .next = s_next,
610         .show = t_show,
611         .stop = t_stop,
612 };
613
614 static const struct file_operations ftrace_avail_fops = {
615         .open = ftrace_event_seq_open,
616         .read = seq_read,
617         .llseek = seq_lseek,
618         .release = seq_release,
619 };
620
621 static const struct file_operations ftrace_set_event_fops = {
622         .open = ftrace_event_seq_open,
623         .read = seq_read,
624         .write = ftrace_event_write,
625         .llseek = seq_lseek,
626         .release = seq_release,
627 };
628
629 static const struct file_operations ftrace_enable_fops = {
630         .open = tracing_open_generic,
631         .read = event_enable_read,
632         .write = event_enable_write,
633 };
634
635 static const struct file_operations ftrace_event_format_fops = {
636         .open = tracing_open_generic,
637         .read = event_format_read,
638 };
639
640 static const struct file_operations ftrace_event_id_fops = {
641         .open = tracing_open_generic,
642         .read = event_id_read,
643 };
644
645 static const struct file_operations ftrace_event_filter_fops = {
646         .open = tracing_open_generic,
647         .read = event_filter_read,
648         .write = event_filter_write,
649 };
650
651 static const struct file_operations ftrace_subsystem_filter_fops = {
652         .open = tracing_open_generic,
653         .read = subsystem_filter_read,
654         .write = subsystem_filter_write,
655 };
656
657 static struct dentry *event_trace_events_dir(void)
658 {
659         static struct dentry *d_tracer;
660         static struct dentry *d_events;
661
662         if (d_events)
663                 return d_events;
664
665         d_tracer = tracing_init_dentry();
666         if (!d_tracer)
667                 return NULL;
668
669         d_events = debugfs_create_dir("events", d_tracer);
670         if (!d_events)
671                 pr_warning("Could not create debugfs "
672                            "'events' directory\n");
673
674         return d_events;
675 }
676
677 static LIST_HEAD(event_subsystems);
678
679 static struct dentry *
680 event_subsystem_dir(const char *name, struct dentry *d_events)
681 {
682         struct event_subsystem *system;
683
684         /* First see if we did not already create this dir */
685         list_for_each_entry(system, &event_subsystems, list) {
686                 if (strcmp(system->name, name) == 0)
687                         return system->entry;
688         }
689
690         /* need to create new entry */
691         system = kmalloc(sizeof(*system), GFP_KERNEL);
692         if (!system) {
693                 pr_warning("No memory to create event subsystem %s\n",
694                            name);
695                 return d_events;
696         }
697
698         system->entry = debugfs_create_dir(name, d_events);
699         if (!system->entry) {
700                 pr_warning("Could not create event subsystem %s\n",
701                            name);
702                 kfree(system);
703                 return d_events;
704         }
705
706         system->name = name;
707         list_add(&system->list, &event_subsystems);
708
709         system->preds = NULL;
710
711         return system->entry;
712 }
713
714 static int
715 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
716 {
717         struct dentry *entry;
718         int ret;
719
720         /*
721          * If the trace point header did not define TRACE_SYSTEM
722          * then the system would be called "TRACE_SYSTEM".
723          */
724         if (strcmp(call->system, "TRACE_SYSTEM") != 0)
725                 d_events = event_subsystem_dir(call->system, d_events);
726
727         if (call->raw_init) {
728                 ret = call->raw_init();
729                 if (ret < 0) {
730                         pr_warning("Could not initialize trace point"
731                                    " events/%s\n", call->name);
732                         return ret;
733                 }
734         }
735
736         call->dir = debugfs_create_dir(call->name, d_events);
737         if (!call->dir) {
738                 pr_warning("Could not create debugfs "
739                            "'%s' directory\n", call->name);
740                 return -1;
741         }
742
743         if (call->regfunc) {
744                 entry = debugfs_create_file("enable", 0644, call->dir, call,
745                                             &ftrace_enable_fops);
746                 if (!entry)
747                         pr_warning("Could not create debugfs "
748                                    "'%s/enable' entry\n", call->name);
749         }
750
751         if (call->id) {
752                 entry = debugfs_create_file("id", 0444, call->dir, call,
753                                 &ftrace_event_id_fops);
754                 if (!entry)
755                         pr_warning("Could not create debugfs '%s/id' entry\n",
756                                         call->name);
757         }
758
759         if (call->define_fields) {
760                 ret = call->define_fields();
761                 if (ret < 0) {
762                         pr_warning("Could not initialize trace point"
763                                    " events/%s\n", call->name);
764                         return ret;
765                 }
766                 entry = debugfs_create_file("filter", 0644, call->dir, call,
767                                             &ftrace_event_filter_fops);
768                 if (!entry)
769                         pr_warning("Could not create debugfs "
770                                    "'%s/filter' entry\n", call->name);
771         }
772
773         /* A trace may not want to export its format */
774         if (!call->show_format)
775                 return 0;
776
777         entry = debugfs_create_file("format", 0444, call->dir, call,
778                                     &ftrace_event_format_fops);
779         if (!entry)
780                 pr_warning("Could not create debugfs "
781                            "'%s/format' entry\n", call->name);
782
783         return 0;
784 }
785
786 static __init int event_trace_init(void)
787 {
788         struct ftrace_event_call *call = __start_ftrace_events;
789         struct dentry *d_tracer;
790         struct dentry *entry;
791         struct dentry *d_events;
792
793         d_tracer = tracing_init_dentry();
794         if (!d_tracer)
795                 return 0;
796
797         entry = debugfs_create_file("available_events", 0444, d_tracer,
798                                     (void *)&show_event_seq_ops,
799                                     &ftrace_avail_fops);
800         if (!entry)
801                 pr_warning("Could not create debugfs "
802                            "'available_events' entry\n");
803
804         entry = debugfs_create_file("set_event", 0644, d_tracer,
805                                     (void *)&show_set_event_seq_ops,
806                                     &ftrace_set_event_fops);
807         if (!entry)
808                 pr_warning("Could not create debugfs "
809                            "'set_event' entry\n");
810
811         d_events = event_trace_events_dir();
812         if (!d_events)
813                 return 0;
814
815         for_each_event(call) {
816                 /* The linker may leave blanks */
817                 if (!call->name)
818                         continue;
819                 event_create_dir(call, d_events);
820         }
821
822         return 0;
823 }
824 fs_initcall(event_trace_init);