tracing: add format file to describe event struct fields
[linux-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/debugfs.h>
12 #include <linux/uaccess.h>
13 #include <linux/module.h>
14 #include <linux/ctype.h>
15
16 #include "trace.h"
17
18 #define TRACE_SYSTEM "TRACE_SYSTEM"
19
20 static DEFINE_MUTEX(event_mutex);
21
22 #define events_for_each(event)                                          \
23         for (event = __start_ftrace_events;                             \
24              (unsigned long)event < (unsigned long)__stop_ftrace_events; \
25              event++)
26
27 void event_trace_printk(unsigned long ip, const char *fmt, ...)
28 {
29         va_list ap;
30
31         va_start(ap, fmt);
32         tracing_record_cmdline(current);
33         trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
34         va_end(ap);
35 }
36
37 static void ftrace_clear_events(void)
38 {
39         struct ftrace_event_call *call = (void *)__start_ftrace_events;
40
41
42         while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
43
44                 if (call->enabled) {
45                         call->enabled = 0;
46                         call->unregfunc();
47                 }
48                 call++;
49         }
50 }
51
52 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
53                                         int enable)
54 {
55
56         switch (enable) {
57         case 0:
58                 if (call->enabled) {
59                         call->enabled = 0;
60                         call->unregfunc();
61                 }
62                 if (call->raw_enabled) {
63                         call->raw_enabled = 0;
64                         call->raw_unreg();
65                 }
66                 break;
67         case 1:
68                 if (!call->enabled &&
69                     (call->type & TRACE_EVENT_TYPE_PRINTF)) {
70                         call->enabled = 1;
71                         call->regfunc();
72                 }
73                 if (!call->raw_enabled &&
74                     (call->type & TRACE_EVENT_TYPE_RAW)) {
75                         call->raw_enabled = 1;
76                         call->raw_reg();
77                 }
78                 break;
79         }
80 }
81
82 static int ftrace_set_clr_event(char *buf, int set)
83 {
84         struct ftrace_event_call *call = __start_ftrace_events;
85         char *event = NULL, *sub = NULL, *match;
86         int ret = -EINVAL;
87
88         /*
89          * The buf format can be <subsystem>:<event-name>
90          *  *:<event-name> means any event by that name.
91          *  :<event-name> is the same.
92          *
93          *  <subsystem>:* means all events in that subsystem
94          *  <subsystem>: means the same.
95          *
96          *  <name> (no ':') means all events in a subsystem with
97          *  the name <name> or any event that matches <name>
98          */
99
100         match = strsep(&buf, ":");
101         if (buf) {
102                 sub = match;
103                 event = buf;
104                 match = NULL;
105
106                 if (!strlen(sub) || strcmp(sub, "*") == 0)
107                         sub = NULL;
108                 if (!strlen(event) || strcmp(event, "*") == 0)
109                         event = NULL;
110         }
111
112         mutex_lock(&event_mutex);
113         events_for_each(call) {
114
115                 if (!call->name)
116                         continue;
117
118                 if (match &&
119                     strcmp(match, call->name) != 0 &&
120                     strcmp(match, call->system) != 0)
121                         continue;
122
123                 if (sub && strcmp(sub, call->system) != 0)
124                         continue;
125
126                 if (event && strcmp(event, call->name) != 0)
127                         continue;
128
129                 ftrace_event_enable_disable(call, set);
130
131                 ret = 0;
132         }
133         mutex_unlock(&event_mutex);
134
135         return ret;
136 }
137
138 /* 128 should be much more than enough */
139 #define EVENT_BUF_SIZE          127
140
141 static ssize_t
142 ftrace_event_write(struct file *file, const char __user *ubuf,
143                    size_t cnt, loff_t *ppos)
144 {
145         size_t read = 0;
146         int i, set = 1;
147         ssize_t ret;
148         char *buf;
149         char ch;
150
151         if (!cnt || cnt < 0)
152                 return 0;
153
154         ret = get_user(ch, ubuf++);
155         if (ret)
156                 return ret;
157         read++;
158         cnt--;
159
160         /* skip white space */
161         while (cnt && isspace(ch)) {
162                 ret = get_user(ch, ubuf++);
163                 if (ret)
164                         return ret;
165                 read++;
166                 cnt--;
167         }
168
169         /* Only white space found? */
170         if (isspace(ch)) {
171                 file->f_pos += read;
172                 ret = read;
173                 return ret;
174         }
175
176         buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
177         if (!buf)
178                 return -ENOMEM;
179
180         if (cnt > EVENT_BUF_SIZE)
181                 cnt = EVENT_BUF_SIZE;
182
183         i = 0;
184         while (cnt && !isspace(ch)) {
185                 if (!i && ch == '!')
186                         set = 0;
187                 else
188                         buf[i++] = ch;
189
190                 ret = get_user(ch, ubuf++);
191                 if (ret)
192                         goto out_free;
193                 read++;
194                 cnt--;
195         }
196         buf[i] = 0;
197
198         file->f_pos += read;
199
200         ret = ftrace_set_clr_event(buf, set);
201         if (ret)
202                 goto out_free;
203
204         ret = read;
205
206  out_free:
207         kfree(buf);
208
209         return ret;
210 }
211
212 static void *
213 t_next(struct seq_file *m, void *v, loff_t *pos)
214 {
215         struct ftrace_event_call *call = m->private;
216         struct ftrace_event_call *next = call;
217
218         (*pos)++;
219
220         if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
221                 return NULL;
222
223         m->private = ++next;
224
225         return call;
226 }
227
228 static void *t_start(struct seq_file *m, loff_t *pos)
229 {
230         return t_next(m, NULL, pos);
231 }
232
233 static void *
234 s_next(struct seq_file *m, void *v, loff_t *pos)
235 {
236         struct ftrace_event_call *call = m->private;
237         struct ftrace_event_call *next;
238
239         (*pos)++;
240
241  retry:
242         if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
243                 return NULL;
244
245         if (!call->enabled) {
246                 call++;
247                 goto retry;
248         }
249
250         next = call;
251         m->private = ++next;
252
253         return call;
254 }
255
256 static void *s_start(struct seq_file *m, loff_t *pos)
257 {
258         return s_next(m, NULL, pos);
259 }
260
261 static int t_show(struct seq_file *m, void *v)
262 {
263         struct ftrace_event_call *call = v;
264
265         if (strcmp(call->system, TRACE_SYSTEM) != 0)
266                 seq_printf(m, "%s:", call->system);
267         seq_printf(m, "%s\n", call->name);
268
269         return 0;
270 }
271
272 static void t_stop(struct seq_file *m, void *p)
273 {
274 }
275
276 static int
277 ftrace_event_seq_open(struct inode *inode, struct file *file)
278 {
279         int ret;
280         const struct seq_operations *seq_ops;
281
282         if ((file->f_mode & FMODE_WRITE) &&
283             !(file->f_flags & O_APPEND))
284                 ftrace_clear_events();
285
286         seq_ops = inode->i_private;
287         ret = seq_open(file, seq_ops);
288         if (!ret) {
289                 struct seq_file *m = file->private_data;
290
291                 m->private = __start_ftrace_events;
292         }
293         return ret;
294 }
295
296 static ssize_t
297 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
298                   loff_t *ppos)
299 {
300         struct ftrace_event_call *call = filp->private_data;
301         char *buf;
302
303         if (call->enabled || call->raw_enabled)
304                 buf = "1\n";
305         else
306                 buf = "0\n";
307
308         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
309 }
310
311 static ssize_t
312 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
313                    loff_t *ppos)
314 {
315         struct ftrace_event_call *call = filp->private_data;
316         char buf[64];
317         unsigned long val;
318         int ret;
319
320         if (cnt >= sizeof(buf))
321                 return -EINVAL;
322
323         if (copy_from_user(&buf, ubuf, cnt))
324                 return -EFAULT;
325
326         buf[cnt] = 0;
327
328         ret = strict_strtoul(buf, 10, &val);
329         if (ret < 0)
330                 return ret;
331
332         switch (val) {
333         case 0:
334         case 1:
335                 mutex_lock(&event_mutex);
336                 ftrace_event_enable_disable(call, val);
337                 mutex_unlock(&event_mutex);
338                 break;
339
340         default:
341                 return -EINVAL;
342         }
343
344         *ppos += cnt;
345
346         return cnt;
347 }
348
349 static ssize_t
350 event_type_read(struct file *filp, char __user *ubuf, size_t cnt,
351                 loff_t *ppos)
352 {
353         struct ftrace_event_call *call = filp->private_data;
354         char buf[16];
355         int r = 0;
356
357         if (call->type & TRACE_EVENT_TYPE_PRINTF)
358                 r += sprintf(buf, "printf\n");
359
360         if (call->type & TRACE_EVENT_TYPE_RAW)
361                 r += sprintf(buf+r, "raw\n");
362
363         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
364 }
365
366 static ssize_t
367 event_type_write(struct file *filp, const char __user *ubuf, size_t cnt,
368                  loff_t *ppos)
369 {
370         struct ftrace_event_call *call = filp->private_data;
371         char buf[64];
372
373         /*
374          * If there's only one type, we can't change it.
375          * And currently we always have printf type, and we
376          * may or may not have raw type.
377          *
378          * This is a redundant check, the file should be read
379          * only if this is the case anyway.
380          */
381
382         if (!call->raw_init)
383                 return -EPERM;
384
385         if (cnt >= sizeof(buf))
386                 return -EINVAL;
387
388         if (copy_from_user(&buf, ubuf, cnt))
389                 return -EFAULT;
390
391         buf[cnt] = 0;
392
393         if (!strncmp(buf, "printf", 6) &&
394             (!buf[6] || isspace(buf[6]))) {
395
396                 call->type = TRACE_EVENT_TYPE_PRINTF;
397
398                 /*
399                  * If raw enabled, the disable it and enable
400                  * printf type.
401                  */
402                 if (call->raw_enabled) {
403                         call->raw_enabled = 0;
404                         call->raw_unreg();
405
406                         call->enabled = 1;
407                         call->regfunc();
408                 }
409
410         } else if (!strncmp(buf, "raw", 3) &&
411             (!buf[3] || isspace(buf[3]))) {
412
413                 call->type = TRACE_EVENT_TYPE_RAW;
414
415                 /*
416                  * If printf enabled, the disable it and enable
417                  * raw type.
418                  */
419                 if (call->enabled) {
420                         call->enabled = 0;
421                         call->unregfunc();
422
423                         call->raw_enabled = 1;
424                         call->raw_reg();
425                 }
426         } else
427                 return -EINVAL;
428
429         *ppos += cnt;
430
431         return cnt;
432 }
433
434 static ssize_t
435 event_available_types_read(struct file *filp, char __user *ubuf, size_t cnt,
436                            loff_t *ppos)
437 {
438         struct ftrace_event_call *call = filp->private_data;
439         char buf[16];
440         int r = 0;
441
442         r += sprintf(buf, "printf\n");
443
444         if (call->raw_init)
445                 r += sprintf(buf+r, "raw\n");
446
447         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
448 }
449
450 static ssize_t
451 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
452                   loff_t *ppos)
453 {
454         struct ftrace_event_call *call = filp->private_data;
455         struct trace_seq *s;
456         char *buf;
457         int r;
458
459         s = kmalloc(sizeof(*s), GFP_KERNEL);
460         if (!s)
461                 return -ENOMEM;
462
463         trace_seq_init(s);
464
465         if (*ppos)
466                 return 0;
467
468         r = call->show_format(s);
469         if (!r) {
470                 /*
471                  * ug!  The format output is bigger than a PAGE!!
472                  */
473                 buf = "FORMAT TOO BIG\n";
474                 r = simple_read_from_buffer(ubuf, cnt, ppos,
475                                               buf, strlen(buf));
476                 goto out;
477         }
478
479         r = simple_read_from_buffer(ubuf, cnt, ppos,
480                                     s->buffer, s->len);
481  out:
482         kfree(s);
483         return r;
484 }
485
486 static const struct seq_operations show_event_seq_ops = {
487         .start = t_start,
488         .next = t_next,
489         .show = t_show,
490         .stop = t_stop,
491 };
492
493 static const struct seq_operations show_set_event_seq_ops = {
494         .start = s_start,
495         .next = s_next,
496         .show = t_show,
497         .stop = t_stop,
498 };
499
500 static const struct file_operations ftrace_avail_fops = {
501         .open = ftrace_event_seq_open,
502         .read = seq_read,
503         .llseek = seq_lseek,
504         .release = seq_release,
505 };
506
507 static const struct file_operations ftrace_set_event_fops = {
508         .open = ftrace_event_seq_open,
509         .read = seq_read,
510         .write = ftrace_event_write,
511         .llseek = seq_lseek,
512         .release = seq_release,
513 };
514
515 static const struct file_operations ftrace_enable_fops = {
516         .open = tracing_open_generic,
517         .read = event_enable_read,
518         .write = event_enable_write,
519 };
520
521 static const struct file_operations ftrace_type_fops = {
522         .open = tracing_open_generic,
523         .read = event_type_read,
524         .write = event_type_write,
525 };
526
527 static const struct file_operations ftrace_available_types_fops = {
528         .open = tracing_open_generic,
529         .read = event_available_types_read,
530 };
531
532 static const struct file_operations ftrace_event_format_fops = {
533         .open = tracing_open_generic,
534         .read = event_format_read,
535 };
536
537 static struct dentry *event_trace_events_dir(void)
538 {
539         static struct dentry *d_tracer;
540         static struct dentry *d_events;
541
542         if (d_events)
543                 return d_events;
544
545         d_tracer = tracing_init_dentry();
546         if (!d_tracer)
547                 return NULL;
548
549         d_events = debugfs_create_dir("events", d_tracer);
550         if (!d_events)
551                 pr_warning("Could not create debugfs "
552                            "'events' directory\n");
553
554         return d_events;
555 }
556
557 struct event_subsystem {
558         struct list_head        list;
559         const char              *name;
560         struct dentry           *entry;
561 };
562
563 static LIST_HEAD(event_subsystems);
564
565 static struct dentry *
566 event_subsystem_dir(const char *name, struct dentry *d_events)
567 {
568         struct event_subsystem *system;
569
570         /* First see if we did not already create this dir */
571         list_for_each_entry(system, &event_subsystems, list) {
572                 if (strcmp(system->name, name) == 0)
573                         return system->entry;
574         }
575
576         /* need to create new entry */
577         system = kmalloc(sizeof(*system), GFP_KERNEL);
578         if (!system) {
579                 pr_warning("No memory to create event subsystem %s\n",
580                            name);
581                 return d_events;
582         }
583
584         system->entry = debugfs_create_dir(name, d_events);
585         if (!system->entry) {
586                 pr_warning("Could not create event subsystem %s\n",
587                            name);
588                 kfree(system);
589                 return d_events;
590         }
591
592         system->name = name;
593         list_add(&system->list, &event_subsystems);
594
595         return system->entry;
596 }
597
598 static int
599 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
600 {
601         struct dentry *entry;
602         int ret;
603
604         /*
605          * If the trace point header did not define TRACE_SYSTEM
606          * then the system would be called "TRACE_SYSTEM".
607          */
608         if (strcmp(call->system, "TRACE_SYSTEM") != 0)
609                 d_events = event_subsystem_dir(call->system, d_events);
610
611         if (call->raw_init) {
612                 ret = call->raw_init();
613                 if (ret < 0) {
614                         pr_warning("Could not initialize trace point"
615                                    " events/%s\n", call->name);
616                         return ret;
617                 }
618         }
619
620         /* default the output to printf */
621         call->type = TRACE_EVENT_TYPE_PRINTF;
622
623         call->dir = debugfs_create_dir(call->name, d_events);
624         if (!call->dir) {
625                 pr_warning("Could not create debugfs "
626                            "'%s' directory\n", call->name);
627                 return -1;
628         }
629
630         entry = debugfs_create_file("enable", 0644, call->dir, call,
631                                     &ftrace_enable_fops);
632         if (!entry)
633                 pr_warning("Could not create debugfs "
634                            "'%s/enable' entry\n", call->name);
635
636         /* Only let type be writable, if we can change it */
637         entry = debugfs_create_file("type",
638                                     call->raw_init ? 0644 : 0444,
639                                     call->dir, call,
640                                     &ftrace_type_fops);
641         if (!entry)
642                 pr_warning("Could not create debugfs "
643                            "'%s/type' entry\n", call->name);
644
645         entry = debugfs_create_file("available_types", 0444, call->dir, call,
646                                     &ftrace_available_types_fops);
647         if (!entry)
648                 pr_warning("Could not create debugfs "
649                            "'%s/available_types' entry\n", call->name);
650
651         /* A trace may not want to export its format */
652         if (!call->show_format)
653                 return 0;
654
655         entry = debugfs_create_file("format", 0444, call->dir, call,
656                                     &ftrace_event_format_fops);
657         if (!entry)
658                 pr_warning("Could not create debugfs "
659                            "'%s/format' entry\n", call->name);
660
661         return 0;
662 }
663
664 static __init int event_trace_init(void)
665 {
666         struct ftrace_event_call *call = __start_ftrace_events;
667         struct dentry *d_tracer;
668         struct dentry *entry;
669         struct dentry *d_events;
670
671         d_tracer = tracing_init_dentry();
672         if (!d_tracer)
673                 return 0;
674
675         entry = debugfs_create_file("available_events", 0444, d_tracer,
676                                     (void *)&show_event_seq_ops,
677                                     &ftrace_avail_fops);
678         if (!entry)
679                 pr_warning("Could not create debugfs "
680                            "'available_events' entry\n");
681
682         entry = debugfs_create_file("set_event", 0644, d_tracer,
683                                     (void *)&show_set_event_seq_ops,
684                                     &ftrace_set_event_fops);
685         if (!entry)
686                 pr_warning("Could not create debugfs "
687                            "'set_event' entry\n");
688
689         d_events = event_trace_events_dir();
690         if (!d_events)
691                 return 0;
692
693         events_for_each(call) {
694                 /* The linker may leave blanks */
695                 if (!call->name)
696                         continue;
697                 event_create_dir(call, d_events);
698         }
699
700         return 0;
701 }
702 fs_initcall(event_trace_init);