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