]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/trace/trace_events.c
tracing: x86, mmiotrace: only register for die notifier when tracer active
[mirror_ubuntu-zesty-kernel.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
e6187007
SR
11#include <linux/workqueue.h>
12#include <linux/spinlock.h>
13#include <linux/kthread.h>
b77e38aa
SR
14#include <linux/debugfs.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/ctype.h>
e6187007 18#include <linux/delay.h>
b77e38aa 19
91729ef9 20#include "trace_output.h"
b77e38aa 21
b628b3e6
SR
22#define TRACE_SYSTEM "TRACE_SYSTEM"
23
11a241a3
SR
24static DEFINE_MUTEX(event_mutex);
25
a59fd602
SR
26LIST_HEAD(ftrace_events);
27
cf027f64
TZ
28int trace_define_field(struct ftrace_event_call *call, char *type,
29 char *name, int offset, int size)
30{
31 struct ftrace_event_field *field;
32
fe9f57f2 33 field = kzalloc(sizeof(*field), GFP_KERNEL);
cf027f64
TZ
34 if (!field)
35 goto err;
fe9f57f2 36
cf027f64
TZ
37 field->name = kstrdup(name, GFP_KERNEL);
38 if (!field->name)
39 goto err;
fe9f57f2 40
cf027f64
TZ
41 field->type = kstrdup(type, GFP_KERNEL);
42 if (!field->type)
43 goto err;
fe9f57f2 44
cf027f64
TZ
45 field->offset = offset;
46 field->size = size;
47 list_add(&field->link, &call->fields);
48
49 return 0;
fe9f57f2 50
cf027f64
TZ
51err:
52 if (field) {
53 kfree(field->name);
54 kfree(field->type);
55 }
56 kfree(field);
fe9f57f2 57
cf027f64
TZ
58 return -ENOMEM;
59}
17c873ec 60EXPORT_SYMBOL_GPL(trace_define_field);
cf027f64 61
b77e38aa
SR
62static void ftrace_clear_events(void)
63{
a59fd602 64 struct ftrace_event_call *call;
b77e38aa 65
a59fd602 66 list_for_each_entry(call, &ftrace_events, list) {
b77e38aa
SR
67
68 if (call->enabled) {
69 call->enabled = 0;
70 call->unregfunc();
71 }
b77e38aa
SR
72 }
73}
74
fd994989
SR
75static void ftrace_event_enable_disable(struct ftrace_event_call *call,
76 int enable)
77{
78
79 switch (enable) {
80 case 0:
81 if (call->enabled) {
82 call->enabled = 0;
83 call->unregfunc();
84 }
fd994989
SR
85 break;
86 case 1:
da4d0302 87 if (!call->enabled) {
fd994989
SR
88 call->enabled = 1;
89 call->regfunc();
90 }
fd994989
SR
91 break;
92 }
93}
94
b77e38aa
SR
95static int ftrace_set_clr_event(char *buf, int set)
96{
a59fd602 97 struct ftrace_event_call *call;
b628b3e6
SR
98 char *event = NULL, *sub = NULL, *match;
99 int ret = -EINVAL;
100
101 /*
102 * The buf format can be <subsystem>:<event-name>
103 * *:<event-name> means any event by that name.
104 * :<event-name> is the same.
105 *
106 * <subsystem>:* means all events in that subsystem
107 * <subsystem>: means the same.
108 *
109 * <name> (no ':') means all events in a subsystem with
110 * the name <name> or any event that matches <name>
111 */
112
113 match = strsep(&buf, ":");
114 if (buf) {
115 sub = match;
116 event = buf;
117 match = NULL;
118
119 if (!strlen(sub) || strcmp(sub, "*") == 0)
120 sub = NULL;
121 if (!strlen(event) || strcmp(event, "*") == 0)
122 event = NULL;
123 }
b77e38aa 124
11a241a3 125 mutex_lock(&event_mutex);
a59fd602 126 list_for_each_entry(call, &ftrace_events, list) {
b77e38aa 127
40e26815 128 if (!call->name || !call->regfunc)
1473e441
SR
129 continue;
130
b628b3e6
SR
131 if (match &&
132 strcmp(match, call->name) != 0 &&
133 strcmp(match, call->system) != 0)
134 continue;
135
136 if (sub && strcmp(sub, call->system) != 0)
137 continue;
138
139 if (event && strcmp(event, call->name) != 0)
b77e38aa 140 continue;
b77e38aa 141
fd994989
SR
142 ftrace_event_enable_disable(call, set);
143
b628b3e6 144 ret = 0;
b77e38aa 145 }
11a241a3
SR
146 mutex_unlock(&event_mutex);
147
b628b3e6 148 return ret;
b77e38aa
SR
149}
150
151/* 128 should be much more than enough */
152#define EVENT_BUF_SIZE 127
153
154static ssize_t
155ftrace_event_write(struct file *file, const char __user *ubuf,
156 size_t cnt, loff_t *ppos)
157{
158 size_t read = 0;
159 int i, set = 1;
160 ssize_t ret;
161 char *buf;
162 char ch;
163
164 if (!cnt || cnt < 0)
165 return 0;
166
1852fcce
SR
167 ret = tracing_update_buffers();
168 if (ret < 0)
169 return ret;
170
b77e38aa
SR
171 ret = get_user(ch, ubuf++);
172 if (ret)
173 return ret;
174 read++;
175 cnt--;
176
177 /* skip white space */
178 while (cnt && isspace(ch)) {
179 ret = get_user(ch, ubuf++);
180 if (ret)
181 return ret;
182 read++;
183 cnt--;
184 }
185
186 /* Only white space found? */
187 if (isspace(ch)) {
188 file->f_pos += read;
189 ret = read;
190 return ret;
191 }
192
193 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
194 if (!buf)
195 return -ENOMEM;
196
197 if (cnt > EVENT_BUF_SIZE)
198 cnt = EVENT_BUF_SIZE;
199
200 i = 0;
201 while (cnt && !isspace(ch)) {
202 if (!i && ch == '!')
203 set = 0;
204 else
205 buf[i++] = ch;
206
207 ret = get_user(ch, ubuf++);
208 if (ret)
209 goto out_free;
210 read++;
211 cnt--;
212 }
213 buf[i] = 0;
214
215 file->f_pos += read;
216
217 ret = ftrace_set_clr_event(buf, set);
218 if (ret)
219 goto out_free;
220
221 ret = read;
222
223 out_free:
224 kfree(buf);
225
226 return ret;
227}
228
229static void *
230t_next(struct seq_file *m, void *v, loff_t *pos)
231{
a59fd602
SR
232 struct list_head *list = m->private;
233 struct ftrace_event_call *call;
b77e38aa
SR
234
235 (*pos)++;
236
40e26815 237 for (;;) {
a59fd602 238 if (list == &ftrace_events)
40e26815
SR
239 return NULL;
240
a59fd602
SR
241 call = list_entry(list, struct ftrace_event_call, list);
242
40e26815
SR
243 /*
244 * The ftrace subsystem is for showing formats only.
245 * They can not be enabled or disabled via the event files.
246 */
247 if (call->regfunc)
248 break;
249
a59fd602 250 list = list->next;
40e26815 251 }
b77e38aa 252
a59fd602 253 m->private = list->next;
b77e38aa
SR
254
255 return call;
256}
257
258static void *t_start(struct seq_file *m, loff_t *pos)
259{
260 return t_next(m, NULL, pos);
261}
262
263static void *
264s_next(struct seq_file *m, void *v, loff_t *pos)
265{
a59fd602
SR
266 struct list_head *list = m->private;
267 struct ftrace_event_call *call;
b77e38aa
SR
268
269 (*pos)++;
270
271 retry:
a59fd602 272 if (list == &ftrace_events)
b77e38aa
SR
273 return NULL;
274
a59fd602
SR
275 call = list_entry(list, struct ftrace_event_call, list);
276
b77e38aa 277 if (!call->enabled) {
a59fd602 278 list = list->next;
b77e38aa
SR
279 goto retry;
280 }
281
a59fd602 282 m->private = list->next;
b77e38aa
SR
283
284 return call;
285}
286
287static void *s_start(struct seq_file *m, loff_t *pos)
288{
289 return s_next(m, NULL, pos);
290}
291
292static int t_show(struct seq_file *m, void *v)
293{
294 struct ftrace_event_call *call = v;
295
b628b3e6
SR
296 if (strcmp(call->system, TRACE_SYSTEM) != 0)
297 seq_printf(m, "%s:", call->system);
b77e38aa
SR
298 seq_printf(m, "%s\n", call->name);
299
300 return 0;
301}
302
303static void t_stop(struct seq_file *m, void *p)
304{
305}
306
307static int
308ftrace_event_seq_open(struct inode *inode, struct file *file)
309{
310 int ret;
311 const struct seq_operations *seq_ops;
312
313 if ((file->f_mode & FMODE_WRITE) &&
314 !(file->f_flags & O_APPEND))
315 ftrace_clear_events();
316
317 seq_ops = inode->i_private;
318 ret = seq_open(file, seq_ops);
319 if (!ret) {
320 struct seq_file *m = file->private_data;
321
a59fd602 322 m->private = ftrace_events.next;
b77e38aa
SR
323 }
324 return ret;
325}
326
1473e441
SR
327static ssize_t
328event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
329 loff_t *ppos)
330{
331 struct ftrace_event_call *call = filp->private_data;
332 char *buf;
333
da4d0302 334 if (call->enabled)
1473e441
SR
335 buf = "1\n";
336 else
337 buf = "0\n";
338
339 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
340}
341
342static ssize_t
343event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
344 loff_t *ppos)
345{
346 struct ftrace_event_call *call = filp->private_data;
347 char buf[64];
348 unsigned long val;
349 int ret;
350
351 if (cnt >= sizeof(buf))
352 return -EINVAL;
353
354 if (copy_from_user(&buf, ubuf, cnt))
355 return -EFAULT;
356
357 buf[cnt] = 0;
358
359 ret = strict_strtoul(buf, 10, &val);
360 if (ret < 0)
361 return ret;
362
1852fcce
SR
363 ret = tracing_update_buffers();
364 if (ret < 0)
365 return ret;
366
1473e441
SR
367 switch (val) {
368 case 0:
1473e441 369 case 1:
11a241a3 370 mutex_lock(&event_mutex);
fd994989 371 ftrace_event_enable_disable(call, val);
11a241a3 372 mutex_unlock(&event_mutex);
1473e441
SR
373 break;
374
375 default:
376 return -EINVAL;
377 }
378
379 *ppos += cnt;
380
381 return cnt;
382}
383
75db37d2
SR
384extern char *__bad_type_size(void);
385
91729ef9 386#undef FIELD
156b5f17 387#define FIELD(type, name) \
75db37d2 388 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
cf027f64
TZ
389 #type, "common_" #name, offsetof(typeof(field), name), \
390 sizeof(field.name)
91729ef9
SR
391
392static int trace_write_header(struct trace_seq *s)
393{
394 struct trace_entry field;
395
396 /* struct trace_entry */
397 return trace_seq_printf(s,
ce8eb2bf
SR
398 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
399 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
400 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
401 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
402 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
91729ef9 403 "\n",
89ec0dee 404 FIELD(unsigned short, type),
91729ef9
SR
405 FIELD(unsigned char, flags),
406 FIELD(unsigned char, preempt_count),
407 FIELD(int, pid),
408 FIELD(int, tgid));
409}
da4d0302 410
981d081e
SR
411static ssize_t
412event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
413 loff_t *ppos)
414{
415 struct ftrace_event_call *call = filp->private_data;
416 struct trace_seq *s;
417 char *buf;
418 int r;
419
c269fc8c
TZ
420 if (*ppos)
421 return 0;
422
981d081e
SR
423 s = kmalloc(sizeof(*s), GFP_KERNEL);
424 if (!s)
425 return -ENOMEM;
426
427 trace_seq_init(s);
428
c5e4e192
SR
429 /* If any of the first writes fail, so will the show_format. */
430
431 trace_seq_printf(s, "name: %s\n", call->name);
432 trace_seq_printf(s, "ID: %d\n", call->id);
433 trace_seq_printf(s, "format:\n");
91729ef9
SR
434 trace_write_header(s);
435
981d081e
SR
436 r = call->show_format(s);
437 if (!r) {
438 /*
439 * ug! The format output is bigger than a PAGE!!
440 */
441 buf = "FORMAT TOO BIG\n";
442 r = simple_read_from_buffer(ubuf, cnt, ppos,
443 buf, strlen(buf));
444 goto out;
445 }
446
447 r = simple_read_from_buffer(ubuf, cnt, ppos,
448 s->buffer, s->len);
449 out:
450 kfree(s);
451 return r;
452}
453
23725aee
PZ
454static ssize_t
455event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
456{
457 struct ftrace_event_call *call = filp->private_data;
458 struct trace_seq *s;
459 int r;
460
461 if (*ppos)
462 return 0;
463
464 s = kmalloc(sizeof(*s), GFP_KERNEL);
465 if (!s)
466 return -ENOMEM;
467
468 trace_seq_init(s);
469 trace_seq_printf(s, "%d\n", call->id);
470
471 r = simple_read_from_buffer(ubuf, cnt, ppos,
472 s->buffer, s->len);
473 kfree(s);
474 return r;
475}
476
7ce7e424
TZ
477static ssize_t
478event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
479 loff_t *ppos)
480{
481 struct ftrace_event_call *call = filp->private_data;
482 struct trace_seq *s;
483 int r;
484
485 if (*ppos)
486 return 0;
487
488 s = kmalloc(sizeof(*s), GFP_KERNEL);
489 if (!s)
490 return -ENOMEM;
491
492 trace_seq_init(s);
493
ac1adc55 494 filter_print_preds(call, s);
4bda2d51 495 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
7ce7e424
TZ
496
497 kfree(s);
498
499 return r;
500}
501
502static ssize_t
503event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
504 loff_t *ppos)
505{
506 struct ftrace_event_call *call = filp->private_data;
507 char buf[64], *pbuf = buf;
508 struct filter_pred *pred;
509 int err;
510
511 if (cnt >= sizeof(buf))
512 return -EINVAL;
513
514 if (copy_from_user(&buf, ubuf, cnt))
515 return -EFAULT;
8433a40e 516 buf[cnt] = '\0';
7ce7e424
TZ
517
518 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
519 if (!pred)
520 return -ENOMEM;
521
522 err = filter_parse(&pbuf, pred);
523 if (err < 0) {
524 filter_free_pred(pred);
525 return err;
526 }
527
528 if (pred->clear) {
0a19e53c 529 filter_disable_preds(call);
09f1f245 530 filter_free_pred(pred);
7ce7e424
TZ
531 return cnt;
532 }
533
44e9c8b7
LZ
534 err = filter_add_pred(call, pred);
535 if (err < 0) {
7ce7e424 536 filter_free_pred(pred);
44e9c8b7 537 return err;
7ce7e424
TZ
538 }
539
0a19e53c
TZ
540 filter_free_pred(pred);
541
7ce7e424
TZ
542 *ppos += cnt;
543
544 return cnt;
545}
546
cfb180f3
TZ
547static ssize_t
548subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
549 loff_t *ppos)
550{
551 struct event_subsystem *system = filp->private_data;
552 struct trace_seq *s;
553 int r;
554
555 if (*ppos)
556 return 0;
557
558 s = kmalloc(sizeof(*s), GFP_KERNEL);
559 if (!s)
560 return -ENOMEM;
561
562 trace_seq_init(s);
563
ac1adc55 564 filter_print_subsystem_preds(system, s);
4bda2d51 565 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
cfb180f3
TZ
566
567 kfree(s);
568
569 return r;
570}
571
572static ssize_t
573subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
574 loff_t *ppos)
575{
576 struct event_subsystem *system = filp->private_data;
577 char buf[64], *pbuf = buf;
578 struct filter_pred *pred;
579 int err;
580
581 if (cnt >= sizeof(buf))
582 return -EINVAL;
583
584 if (copy_from_user(&buf, ubuf, cnt))
585 return -EFAULT;
8433a40e 586 buf[cnt] = '\0';
cfb180f3
TZ
587
588 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
589 if (!pred)
590 return -ENOMEM;
591
592 err = filter_parse(&pbuf, pred);
593 if (err < 0) {
594 filter_free_pred(pred);
595 return err;
596 }
597
598 if (pred->clear) {
599 filter_free_subsystem_preds(system);
09f1f245 600 filter_free_pred(pred);
cfb180f3
TZ
601 return cnt;
602 }
603
44e9c8b7
LZ
604 err = filter_add_subsystem_pred(system, pred);
605 if (err < 0) {
cfb180f3 606 filter_free_pred(pred);
44e9c8b7 607 return err;
cfb180f3
TZ
608 }
609
610 *ppos += cnt;
611
612 return cnt;
613}
614
d1b182a8
SR
615static ssize_t
616show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
617{
618 int (*func)(struct trace_seq *s) = filp->private_data;
619 struct trace_seq *s;
620 int r;
621
622 if (*ppos)
623 return 0;
624
625 s = kmalloc(sizeof(*s), GFP_KERNEL);
626 if (!s)
627 return -ENOMEM;
628
629 trace_seq_init(s);
630
631 func(s);
632 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
633
634 kfree(s);
635
636 return r;
637}
638
b77e38aa
SR
639static const struct seq_operations show_event_seq_ops = {
640 .start = t_start,
641 .next = t_next,
642 .show = t_show,
643 .stop = t_stop,
644};
645
646static const struct seq_operations show_set_event_seq_ops = {
647 .start = s_start,
648 .next = s_next,
649 .show = t_show,
650 .stop = t_stop,
651};
652
2314c4ae
SR
653static const struct file_operations ftrace_avail_fops = {
654 .open = ftrace_event_seq_open,
655 .read = seq_read,
656 .llseek = seq_lseek,
657 .release = seq_release,
658};
659
b77e38aa
SR
660static const struct file_operations ftrace_set_event_fops = {
661 .open = ftrace_event_seq_open,
662 .read = seq_read,
663 .write = ftrace_event_write,
664 .llseek = seq_lseek,
665 .release = seq_release,
666};
667
1473e441
SR
668static const struct file_operations ftrace_enable_fops = {
669 .open = tracing_open_generic,
670 .read = event_enable_read,
671 .write = event_enable_write,
672};
673
981d081e
SR
674static const struct file_operations ftrace_event_format_fops = {
675 .open = tracing_open_generic,
676 .read = event_format_read,
677};
678
23725aee
PZ
679static const struct file_operations ftrace_event_id_fops = {
680 .open = tracing_open_generic,
681 .read = event_id_read,
682};
683
7ce7e424
TZ
684static const struct file_operations ftrace_event_filter_fops = {
685 .open = tracing_open_generic,
686 .read = event_filter_read,
687 .write = event_filter_write,
688};
689
cfb180f3
TZ
690static const struct file_operations ftrace_subsystem_filter_fops = {
691 .open = tracing_open_generic,
692 .read = subsystem_filter_read,
693 .write = subsystem_filter_write,
694};
695
d1b182a8
SR
696static const struct file_operations ftrace_show_header_fops = {
697 .open = tracing_open_generic,
698 .read = show_header,
699};
700
1473e441
SR
701static struct dentry *event_trace_events_dir(void)
702{
703 static struct dentry *d_tracer;
704 static struct dentry *d_events;
705
706 if (d_events)
707 return d_events;
708
709 d_tracer = tracing_init_dentry();
710 if (!d_tracer)
711 return NULL;
712
713 d_events = debugfs_create_dir("events", d_tracer);
714 if (!d_events)
715 pr_warning("Could not create debugfs "
716 "'events' directory\n");
717
718 return d_events;
719}
720
6ecc2d1c
SR
721static LIST_HEAD(event_subsystems);
722
723static struct dentry *
724event_subsystem_dir(const char *name, struct dentry *d_events)
725{
726 struct event_subsystem *system;
e1112b4d 727 struct dentry *entry;
6ecc2d1c
SR
728
729 /* First see if we did not already create this dir */
730 list_for_each_entry(system, &event_subsystems, list) {
731 if (strcmp(system->name, name) == 0)
732 return system->entry;
733 }
734
735 /* need to create new entry */
736 system = kmalloc(sizeof(*system), GFP_KERNEL);
737 if (!system) {
738 pr_warning("No memory to create event subsystem %s\n",
739 name);
740 return d_events;
741 }
742
743 system->entry = debugfs_create_dir(name, d_events);
744 if (!system->entry) {
745 pr_warning("Could not create event subsystem %s\n",
746 name);
747 kfree(system);
748 return d_events;
749 }
750
6d723736
SR
751 system->name = kstrdup(name, GFP_KERNEL);
752 if (!system->name) {
753 debugfs_remove(system->entry);
754 kfree(system);
755 return d_events;
756 }
757
6ecc2d1c
SR
758 list_add(&system->list, &event_subsystems);
759
cfb180f3 760 system->preds = NULL;
0a19e53c 761 system->n_preds = 0;
cfb180f3 762
e1112b4d
TZ
763 entry = debugfs_create_file("filter", 0644, system->entry, system,
764 &ftrace_subsystem_filter_fops);
765 if (!entry)
766 pr_warning("Could not create debugfs "
767 "'%s/filter' entry\n", name);
768
6ecc2d1c
SR
769 return system->entry;
770}
771
1473e441 772static int
701970b3
SR
773event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
774 const struct file_operations *id,
775 const struct file_operations *enable,
776 const struct file_operations *filter,
777 const struct file_operations *format)
1473e441
SR
778{
779 struct dentry *entry;
fd994989 780 int ret;
1473e441 781
6ecc2d1c
SR
782 /*
783 * If the trace point header did not define TRACE_SYSTEM
784 * then the system would be called "TRACE_SYSTEM".
785 */
6d723736 786 if (strcmp(call->system, TRACE_SYSTEM) != 0)
6ecc2d1c
SR
787 d_events = event_subsystem_dir(call->system, d_events);
788
fd994989
SR
789 if (call->raw_init) {
790 ret = call->raw_init();
791 if (ret < 0) {
792 pr_warning("Could not initialize trace point"
793 " events/%s\n", call->name);
794 return ret;
795 }
796 }
797
1473e441
SR
798 call->dir = debugfs_create_dir(call->name, d_events);
799 if (!call->dir) {
800 pr_warning("Could not create debugfs "
801 "'%s' directory\n", call->name);
802 return -1;
803 }
804
6d723736
SR
805 if (call->regfunc)
806 entry = trace_create_file("enable", 0644, call->dir, call,
701970b3 807 enable);
1473e441 808
6d723736
SR
809 if (call->id)
810 entry = trace_create_file("id", 0444, call->dir, call,
701970b3 811 id);
23725aee 812
cf027f64
TZ
813 if (call->define_fields) {
814 ret = call->define_fields();
815 if (ret < 0) {
816 pr_warning("Could not initialize trace point"
817 " events/%s\n", call->name);
818 return ret;
819 }
6d723736 820 entry = trace_create_file("filter", 0644, call->dir, call,
701970b3 821 filter);
cf027f64
TZ
822 }
823
981d081e
SR
824 /* A trace may not want to export its format */
825 if (!call->show_format)
826 return 0;
827
6d723736 828 entry = trace_create_file("format", 0444, call->dir, call,
701970b3 829 format);
6d723736
SR
830
831 return 0;
832}
833
834#define for_each_event(event, start, end) \
835 for (event = start; \
836 (unsigned long)event < (unsigned long)end; \
837 event++)
838
61f919a1 839#ifdef CONFIG_MODULES
701970b3
SR
840
841static LIST_HEAD(ftrace_module_file_list);
842
843/*
844 * Modules must own their file_operations to keep up with
845 * reference counting.
846 */
847struct ftrace_module_file_ops {
848 struct list_head list;
849 struct module *mod;
850 struct file_operations id;
851 struct file_operations enable;
852 struct file_operations format;
853 struct file_operations filter;
854};
855
856static struct ftrace_module_file_ops *
857trace_create_file_ops(struct module *mod)
858{
859 struct ftrace_module_file_ops *file_ops;
860
861 /*
862 * This is a bit of a PITA. To allow for correct reference
863 * counting, modules must "own" their file_operations.
864 * To do this, we allocate the file operations that will be
865 * used in the event directory.
866 */
867
868 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
869 if (!file_ops)
870 return NULL;
871
872 file_ops->mod = mod;
873
874 file_ops->id = ftrace_event_id_fops;
875 file_ops->id.owner = mod;
876
877 file_ops->enable = ftrace_enable_fops;
878 file_ops->enable.owner = mod;
879
880 file_ops->filter = ftrace_event_filter_fops;
881 file_ops->filter.owner = mod;
882
883 file_ops->format = ftrace_event_format_fops;
884 file_ops->format.owner = mod;
885
886 list_add(&file_ops->list, &ftrace_module_file_list);
887
888 return file_ops;
889}
890
6d723736
SR
891static void trace_module_add_events(struct module *mod)
892{
701970b3 893 struct ftrace_module_file_ops *file_ops = NULL;
6d723736
SR
894 struct ftrace_event_call *call, *start, *end;
895 struct dentry *d_events;
896
897 start = mod->trace_events;
898 end = mod->trace_events + mod->num_trace_events;
899
900 if (start == end)
901 return;
902
903 d_events = event_trace_events_dir();
904 if (!d_events)
905 return;
906
907 for_each_event(call, start, end) {
908 /* The linker may leave blanks */
909 if (!call->name)
910 continue;
701970b3
SR
911
912 /*
913 * This module has events, create file ops for this module
914 * if not already done.
915 */
916 if (!file_ops) {
917 file_ops = trace_create_file_ops(mod);
918 if (!file_ops)
919 return;
920 }
6d723736
SR
921 call->mod = mod;
922 list_add(&call->list, &ftrace_events);
701970b3
SR
923 event_create_dir(call, d_events,
924 &file_ops->id, &file_ops->enable,
925 &file_ops->filter, &file_ops->format);
6d723736
SR
926 }
927}
928
929static void trace_module_remove_events(struct module *mod)
930{
701970b3 931 struct ftrace_module_file_ops *file_ops;
6d723736
SR
932 struct ftrace_event_call *call, *p;
933
934 list_for_each_entry_safe(call, p, &ftrace_events, list) {
935 if (call->mod == mod) {
936 if (call->enabled) {
937 call->enabled = 0;
938 call->unregfunc();
939 }
940 if (call->event)
941 unregister_ftrace_event(call->event);
942 debugfs_remove_recursive(call->dir);
943 list_del(&call->list);
944 }
945 }
701970b3
SR
946
947 /* Now free the file_operations */
948 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
949 if (file_ops->mod == mod)
950 break;
951 }
952 if (&file_ops->list != &ftrace_module_file_list) {
953 list_del(&file_ops->list);
954 kfree(file_ops);
955 }
6d723736
SR
956}
957
61f919a1
SR
958static int trace_module_notify(struct notifier_block *self,
959 unsigned long val, void *data)
6d723736
SR
960{
961 struct module *mod = data;
962
963 mutex_lock(&event_mutex);
964 switch (val) {
965 case MODULE_STATE_COMING:
966 trace_module_add_events(mod);
967 break;
968 case MODULE_STATE_GOING:
969 trace_module_remove_events(mod);
970 break;
971 }
972 mutex_unlock(&event_mutex);
fd994989 973
1473e441
SR
974 return 0;
975}
61f919a1
SR
976#else
977static int trace_module_notify(struct notifier_block *self,
978 unsigned long val, void *data)
979{
980 return 0;
981}
982#endif /* CONFIG_MODULES */
1473e441 983
6d723736
SR
984struct notifier_block trace_module_nb = {
985 .notifier_call = trace_module_notify,
986 .priority = 0,
987};
988
a59fd602
SR
989extern struct ftrace_event_call __start_ftrace_events[];
990extern struct ftrace_event_call __stop_ftrace_events[];
991
b77e38aa
SR
992static __init int event_trace_init(void)
993{
a59fd602 994 struct ftrace_event_call *call;
b77e38aa
SR
995 struct dentry *d_tracer;
996 struct dentry *entry;
1473e441 997 struct dentry *d_events;
6d723736 998 int ret;
b77e38aa
SR
999
1000 d_tracer = tracing_init_dentry();
1001 if (!d_tracer)
1002 return 0;
1003
2314c4ae
SR
1004 entry = debugfs_create_file("available_events", 0444, d_tracer,
1005 (void *)&show_event_seq_ops,
1006 &ftrace_avail_fops);
1007 if (!entry)
1008 pr_warning("Could not create debugfs "
1009 "'available_events' entry\n");
1010
b77e38aa
SR
1011 entry = debugfs_create_file("set_event", 0644, d_tracer,
1012 (void *)&show_set_event_seq_ops,
1013 &ftrace_set_event_fops);
1014 if (!entry)
1015 pr_warning("Could not create debugfs "
1016 "'set_event' entry\n");
1017
1473e441
SR
1018 d_events = event_trace_events_dir();
1019 if (!d_events)
1020 return 0;
1021
d1b182a8
SR
1022 /* ring buffer internal formats */
1023 trace_create_file("header_page", 0444, d_events,
1024 ring_buffer_print_page_header,
1025 &ftrace_show_header_fops);
1026
1027 trace_create_file("header_event", 0444, d_events,
1028 ring_buffer_print_entry_header,
1029 &ftrace_show_header_fops);
1030
6d723736 1031 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1473e441
SR
1032 /* The linker may leave blanks */
1033 if (!call->name)
1034 continue;
a59fd602 1035 list_add(&call->list, &ftrace_events);
701970b3
SR
1036 event_create_dir(call, d_events, &ftrace_event_id_fops,
1037 &ftrace_enable_fops, &ftrace_event_filter_fops,
1038 &ftrace_event_format_fops);
1473e441
SR
1039 }
1040
6d723736
SR
1041 ret = register_module_notifier(&trace_module_nb);
1042 if (!ret)
1043 pr_warning("Failed to register trace events module notifier\n");
1044
b77e38aa
SR
1045 return 0;
1046}
1047fs_initcall(event_trace_init);
e6187007
SR
1048
1049#ifdef CONFIG_FTRACE_STARTUP_TEST
1050
1051static DEFINE_SPINLOCK(test_spinlock);
1052static DEFINE_SPINLOCK(test_spinlock_irq);
1053static DEFINE_MUTEX(test_mutex);
1054
1055static __init void test_work(struct work_struct *dummy)
1056{
1057 spin_lock(&test_spinlock);
1058 spin_lock_irq(&test_spinlock_irq);
1059 udelay(1);
1060 spin_unlock_irq(&test_spinlock_irq);
1061 spin_unlock(&test_spinlock);
1062
1063 mutex_lock(&test_mutex);
1064 msleep(1);
1065 mutex_unlock(&test_mutex);
1066}
1067
1068static __init int event_test_thread(void *unused)
1069{
1070 void *test_malloc;
1071
1072 test_malloc = kmalloc(1234, GFP_KERNEL);
1073 if (!test_malloc)
1074 pr_info("failed to kmalloc\n");
1075
1076 schedule_on_each_cpu(test_work);
1077
1078 kfree(test_malloc);
1079
1080 set_current_state(TASK_INTERRUPTIBLE);
1081 while (!kthread_should_stop())
1082 schedule();
1083
1084 return 0;
1085}
1086
1087/*
1088 * Do various things that may trigger events.
1089 */
1090static __init void event_test_stuff(void)
1091{
1092 struct task_struct *test_thread;
1093
1094 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1095 msleep(1);
1096 kthread_stop(test_thread);
1097}
1098
1099/*
1100 * For every trace event defined, we will test each trace point separately,
1101 * and then by groups, and finally all trace points.
1102 */
9ea21c1e 1103static __init void event_trace_self_tests(void)
e6187007
SR
1104{
1105 struct ftrace_event_call *call;
1106 struct event_subsystem *system;
1107 char *sysname;
1108 int ret;
1109
1110 pr_info("Running tests on trace events:\n");
1111
1112 list_for_each_entry(call, &ftrace_events, list) {
1113
1114 /* Only test those that have a regfunc */
1115 if (!call->regfunc)
1116 continue;
1117
1118 pr_info("Testing event %s: ", call->name);
1119
1120 /*
1121 * If an event is already enabled, someone is using
1122 * it and the self test should not be on.
1123 */
1124 if (call->enabled) {
1125 pr_warning("Enabled event during self test!\n");
1126 WARN_ON_ONCE(1);
1127 continue;
1128 }
1129
1130 call->enabled = 1;
1131 call->regfunc();
1132
1133 event_test_stuff();
1134
1135 call->unregfunc();
1136 call->enabled = 0;
1137
1138 pr_cont("OK\n");
1139 }
1140
1141 /* Now test at the sub system level */
1142
1143 pr_info("Running tests on trace event systems:\n");
1144
1145 list_for_each_entry(system, &event_subsystems, list) {
1146
1147 /* the ftrace system is special, skip it */
1148 if (strcmp(system->name, "ftrace") == 0)
1149 continue;
1150
1151 pr_info("Testing event system %s: ", system->name);
1152
1153 /* ftrace_set_clr_event can modify the name passed in. */
1154 sysname = kstrdup(system->name, GFP_KERNEL);
1155 if (WARN_ON(!sysname)) {
1156 pr_warning("Can't allocate memory, giving up!\n");
9ea21c1e 1157 return;
e6187007
SR
1158 }
1159 ret = ftrace_set_clr_event(sysname, 1);
1160 kfree(sysname);
1161 if (WARN_ON_ONCE(ret)) {
1162 pr_warning("error enabling system %s\n",
1163 system->name);
1164 continue;
1165 }
1166
1167 event_test_stuff();
1168
1169 sysname = kstrdup(system->name, GFP_KERNEL);
1170 if (WARN_ON(!sysname)) {
1171 pr_warning("Can't allocate memory, giving up!\n");
9ea21c1e 1172 return;
e6187007
SR
1173 }
1174 ret = ftrace_set_clr_event(sysname, 0);
1175 kfree(sysname);
1176
1177 if (WARN_ON_ONCE(ret))
1178 pr_warning("error disabling system %s\n",
1179 system->name);
1180
1181 pr_cont("OK\n");
1182 }
1183
1184 /* Test with all events enabled */
1185
1186 pr_info("Running tests on all trace events:\n");
1187 pr_info("Testing all events: ");
1188
1189 sysname = kmalloc(4, GFP_KERNEL);
1190 if (WARN_ON(!sysname)) {
1191 pr_warning("Can't allocate memory, giving up!\n");
9ea21c1e 1192 return;
e6187007
SR
1193 }
1194 memcpy(sysname, "*:*", 4);
1195 ret = ftrace_set_clr_event(sysname, 1);
1196 if (WARN_ON_ONCE(ret)) {
1197 kfree(sysname);
1198 pr_warning("error enabling all events\n");
9ea21c1e 1199 return;
e6187007
SR
1200 }
1201
1202 event_test_stuff();
1203
1204 /* reset sysname */
1205 memcpy(sysname, "*:*", 4);
1206 ret = ftrace_set_clr_event(sysname, 0);
1207 kfree(sysname);
1208
1209 if (WARN_ON_ONCE(ret)) {
1210 pr_warning("error disabling all events\n");
9ea21c1e 1211 return;
e6187007
SR
1212 }
1213
1214 pr_cont("OK\n");
9ea21c1e
SR
1215}
1216
1217#ifdef CONFIG_FUNCTION_TRACER
1218
1219static DEFINE_PER_CPU(atomic_t, test_event_disable);
1220
1221static void
1222function_test_events_call(unsigned long ip, unsigned long parent_ip)
1223{
1224 struct ring_buffer_event *event;
1225 struct ftrace_entry *entry;
1226 unsigned long flags;
1227 long disabled;
1228 int resched;
1229 int cpu;
1230 int pc;
1231
1232 pc = preempt_count();
1233 resched = ftrace_preempt_disable();
1234 cpu = raw_smp_processor_id();
1235 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1236
1237 if (disabled != 1)
1238 goto out;
1239
1240 local_save_flags(flags);
1241
1242 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1243 flags, pc);
1244 if (!event)
1245 goto out;
1246 entry = ring_buffer_event_data(event);
1247 entry->ip = ip;
1248 entry->parent_ip = parent_ip;
1249
cb4764a6 1250 trace_nowake_buffer_unlock_commit(event, flags, pc);
9ea21c1e
SR
1251
1252 out:
1253 atomic_dec(&per_cpu(test_event_disable, cpu));
1254 ftrace_preempt_enable(resched);
1255}
1256
1257static struct ftrace_ops trace_ops __initdata =
1258{
1259 .func = function_test_events_call,
1260};
1261
1262static __init void event_trace_self_test_with_function(void)
1263{
1264 register_ftrace_function(&trace_ops);
1265 pr_info("Running tests again, along with the function tracer\n");
1266 event_trace_self_tests();
1267 unregister_ftrace_function(&trace_ops);
1268}
1269#else
1270static __init void event_trace_self_test_with_function(void)
1271{
1272}
1273#endif
1274
1275static __init int event_trace_self_tests_init(void)
1276{
1277
1278 event_trace_self_tests();
1279
1280 event_trace_self_test_with_function();
e6187007
SR
1281
1282 return 0;
1283}
1284
28d20e2d 1285late_initcall(event_trace_self_tests_init);
e6187007
SR
1286
1287#endif