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