]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/trace/trace_events.c
tracing: add saved_cmdlines file to show cached task comms
[mirror_ubuntu-focal-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
91729ef9 384#undef FIELD
156b5f17 385#define FIELD(type, name) \
cf027f64
TZ
386 #type, "common_" #name, offsetof(typeof(field), name), \
387 sizeof(field.name)
91729ef9
SR
388
389static int trace_write_header(struct trace_seq *s)
390{
391 struct trace_entry field;
392
393 /* struct trace_entry */
394 return trace_seq_printf(s,
ce8eb2bf
SR
395 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
396 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
397 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
398 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
399 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
91729ef9
SR
400 "\n",
401 FIELD(unsigned char, type),
402 FIELD(unsigned char, flags),
403 FIELD(unsigned char, preempt_count),
404 FIELD(int, pid),
405 FIELD(int, tgid));
406}
da4d0302 407
981d081e
SR
408static ssize_t
409event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
410 loff_t *ppos)
411{
412 struct ftrace_event_call *call = filp->private_data;
413 struct trace_seq *s;
414 char *buf;
415 int r;
416
c269fc8c
TZ
417 if (*ppos)
418 return 0;
419
981d081e
SR
420 s = kmalloc(sizeof(*s), GFP_KERNEL);
421 if (!s)
422 return -ENOMEM;
423
424 trace_seq_init(s);
425
c5e4e192
SR
426 /* If any of the first writes fail, so will the show_format. */
427
428 trace_seq_printf(s, "name: %s\n", call->name);
429 trace_seq_printf(s, "ID: %d\n", call->id);
430 trace_seq_printf(s, "format:\n");
91729ef9
SR
431 trace_write_header(s);
432
981d081e
SR
433 r = call->show_format(s);
434 if (!r) {
435 /*
436 * ug! The format output is bigger than a PAGE!!
437 */
438 buf = "FORMAT TOO BIG\n";
439 r = simple_read_from_buffer(ubuf, cnt, ppos,
440 buf, strlen(buf));
441 goto out;
442 }
443
444 r = simple_read_from_buffer(ubuf, cnt, ppos,
445 s->buffer, s->len);
446 out:
447 kfree(s);
448 return r;
449}
450
23725aee
PZ
451static ssize_t
452event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
453{
454 struct ftrace_event_call *call = filp->private_data;
455 struct trace_seq *s;
456 int r;
457
458 if (*ppos)
459 return 0;
460
461 s = kmalloc(sizeof(*s), GFP_KERNEL);
462 if (!s)
463 return -ENOMEM;
464
465 trace_seq_init(s);
466 trace_seq_printf(s, "%d\n", call->id);
467
468 r = simple_read_from_buffer(ubuf, cnt, ppos,
469 s->buffer, s->len);
470 kfree(s);
471 return r;
472}
473
7ce7e424
TZ
474static ssize_t
475event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
476 loff_t *ppos)
477{
478 struct ftrace_event_call *call = filp->private_data;
479 struct trace_seq *s;
480 int r;
481
482 if (*ppos)
483 return 0;
484
485 s = kmalloc(sizeof(*s), GFP_KERNEL);
486 if (!s)
487 return -ENOMEM;
488
489 trace_seq_init(s);
490
0a19e53c 491 filter_print_preds(call->preds, call->n_preds, s);
4bda2d51 492 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
7ce7e424
TZ
493
494 kfree(s);
495
496 return r;
497}
498
499static ssize_t
500event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
501 loff_t *ppos)
502{
503 struct ftrace_event_call *call = filp->private_data;
504 char buf[64], *pbuf = buf;
505 struct filter_pred *pred;
506 int err;
507
508 if (cnt >= sizeof(buf))
509 return -EINVAL;
510
511 if (copy_from_user(&buf, ubuf, cnt))
512 return -EFAULT;
8433a40e 513 buf[cnt] = '\0';
7ce7e424
TZ
514
515 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
516 if (!pred)
517 return -ENOMEM;
518
519 err = filter_parse(&pbuf, pred);
520 if (err < 0) {
521 filter_free_pred(pred);
522 return err;
523 }
524
525 if (pred->clear) {
0a19e53c 526 filter_disable_preds(call);
09f1f245 527 filter_free_pred(pred);
7ce7e424
TZ
528 return cnt;
529 }
530
44e9c8b7
LZ
531 err = filter_add_pred(call, pred);
532 if (err < 0) {
7ce7e424 533 filter_free_pred(pred);
44e9c8b7 534 return err;
7ce7e424
TZ
535 }
536
0a19e53c
TZ
537 filter_free_pred(pred);
538
7ce7e424
TZ
539 *ppos += cnt;
540
541 return cnt;
542}
543
cfb180f3
TZ
544static ssize_t
545subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
546 loff_t *ppos)
547{
548 struct event_subsystem *system = filp->private_data;
549 struct trace_seq *s;
550 int r;
551
552 if (*ppos)
553 return 0;
554
555 s = kmalloc(sizeof(*s), GFP_KERNEL);
556 if (!s)
557 return -ENOMEM;
558
559 trace_seq_init(s);
560
0a19e53c 561 filter_print_preds(system->preds, system->n_preds, s);
4bda2d51 562 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
cfb180f3
TZ
563
564 kfree(s);
565
566 return r;
567}
568
569static ssize_t
570subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
571 loff_t *ppos)
572{
573 struct event_subsystem *system = filp->private_data;
574 char buf[64], *pbuf = buf;
575 struct filter_pred *pred;
576 int err;
577
578 if (cnt >= sizeof(buf))
579 return -EINVAL;
580
581 if (copy_from_user(&buf, ubuf, cnt))
582 return -EFAULT;
8433a40e 583 buf[cnt] = '\0';
cfb180f3
TZ
584
585 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
586 if (!pred)
587 return -ENOMEM;
588
589 err = filter_parse(&pbuf, pred);
590 if (err < 0) {
591 filter_free_pred(pred);
592 return err;
593 }
594
595 if (pred->clear) {
596 filter_free_subsystem_preds(system);
09f1f245 597 filter_free_pred(pred);
cfb180f3
TZ
598 return cnt;
599 }
600
44e9c8b7
LZ
601 err = filter_add_subsystem_pred(system, pred);
602 if (err < 0) {
c4cff064 603 filter_free_subsystem_preds(system);
cfb180f3 604 filter_free_pred(pred);
44e9c8b7 605 return err;
cfb180f3
TZ
606 }
607
608 *ppos += cnt;
609
610 return cnt;
611}
612
d1b182a8
SR
613static ssize_t
614show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
615{
616 int (*func)(struct trace_seq *s) = filp->private_data;
617 struct trace_seq *s;
618 int r;
619
620 if (*ppos)
621 return 0;
622
623 s = kmalloc(sizeof(*s), GFP_KERNEL);
624 if (!s)
625 return -ENOMEM;
626
627 trace_seq_init(s);
628
629 func(s);
630 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
631
632 kfree(s);
633
634 return r;
635}
636
b77e38aa
SR
637static const struct seq_operations show_event_seq_ops = {
638 .start = t_start,
639 .next = t_next,
640 .show = t_show,
641 .stop = t_stop,
642};
643
644static const struct seq_operations show_set_event_seq_ops = {
645 .start = s_start,
646 .next = s_next,
647 .show = t_show,
648 .stop = t_stop,
649};
650
2314c4ae
SR
651static const struct file_operations ftrace_avail_fops = {
652 .open = ftrace_event_seq_open,
653 .read = seq_read,
654 .llseek = seq_lseek,
655 .release = seq_release,
656};
657
b77e38aa
SR
658static const struct file_operations ftrace_set_event_fops = {
659 .open = ftrace_event_seq_open,
660 .read = seq_read,
661 .write = ftrace_event_write,
662 .llseek = seq_lseek,
663 .release = seq_release,
664};
665
1473e441
SR
666static const struct file_operations ftrace_enable_fops = {
667 .open = tracing_open_generic,
668 .read = event_enable_read,
669 .write = event_enable_write,
670};
671
981d081e
SR
672static const struct file_operations ftrace_event_format_fops = {
673 .open = tracing_open_generic,
674 .read = event_format_read,
675};
676
23725aee
PZ
677static const struct file_operations ftrace_event_id_fops = {
678 .open = tracing_open_generic,
679 .read = event_id_read,
680};
681
7ce7e424
TZ
682static const struct file_operations ftrace_event_filter_fops = {
683 .open = tracing_open_generic,
684 .read = event_filter_read,
685 .write = event_filter_write,
686};
687
cfb180f3
TZ
688static const struct file_operations ftrace_subsystem_filter_fops = {
689 .open = tracing_open_generic,
690 .read = subsystem_filter_read,
691 .write = subsystem_filter_write,
692};
693
d1b182a8
SR
694static const struct file_operations ftrace_show_header_fops = {
695 .open = tracing_open_generic,
696 .read = show_header,
697};
698
1473e441
SR
699static struct dentry *event_trace_events_dir(void)
700{
701 static struct dentry *d_tracer;
702 static struct dentry *d_events;
703
704 if (d_events)
705 return d_events;
706
707 d_tracer = tracing_init_dentry();
708 if (!d_tracer)
709 return NULL;
710
711 d_events = debugfs_create_dir("events", d_tracer);
712 if (!d_events)
713 pr_warning("Could not create debugfs "
714 "'events' directory\n");
715
716 return d_events;
717}
718
6ecc2d1c
SR
719static LIST_HEAD(event_subsystems);
720
721static struct dentry *
722event_subsystem_dir(const char *name, struct dentry *d_events)
723{
724 struct event_subsystem *system;
e1112b4d 725 struct dentry *entry;
6ecc2d1c
SR
726
727 /* First see if we did not already create this dir */
728 list_for_each_entry(system, &event_subsystems, list) {
729 if (strcmp(system->name, name) == 0)
730 return system->entry;
731 }
732
733 /* need to create new entry */
734 system = kmalloc(sizeof(*system), GFP_KERNEL);
735 if (!system) {
736 pr_warning("No memory to create event subsystem %s\n",
737 name);
738 return d_events;
739 }
740
741 system->entry = debugfs_create_dir(name, d_events);
742 if (!system->entry) {
743 pr_warning("Could not create event subsystem %s\n",
744 name);
745 kfree(system);
746 return d_events;
747 }
748
6d723736
SR
749 system->name = kstrdup(name, GFP_KERNEL);
750 if (!system->name) {
751 debugfs_remove(system->entry);
752 kfree(system);
753 return d_events;
754 }
755
6ecc2d1c
SR
756 list_add(&system->list, &event_subsystems);
757
cfb180f3 758 system->preds = NULL;
0a19e53c 759 system->n_preds = 0;
cfb180f3 760
e1112b4d
TZ
761 entry = debugfs_create_file("filter", 0644, system->entry, system,
762 &ftrace_subsystem_filter_fops);
763 if (!entry)
764 pr_warning("Could not create debugfs "
765 "'%s/filter' entry\n", name);
766
6ecc2d1c
SR
767 return system->entry;
768}
769
1473e441
SR
770static int
771event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
772{
773 struct dentry *entry;
fd994989 774 int ret;
1473e441 775
6ecc2d1c
SR
776 /*
777 * If the trace point header did not define TRACE_SYSTEM
778 * then the system would be called "TRACE_SYSTEM".
779 */
6d723736 780 if (strcmp(call->system, TRACE_SYSTEM) != 0)
6ecc2d1c
SR
781 d_events = event_subsystem_dir(call->system, d_events);
782
fd994989
SR
783 if (call->raw_init) {
784 ret = call->raw_init();
785 if (ret < 0) {
786 pr_warning("Could not initialize trace point"
787 " events/%s\n", call->name);
788 return ret;
789 }
790 }
791
1473e441
SR
792 call->dir = debugfs_create_dir(call->name, d_events);
793 if (!call->dir) {
794 pr_warning("Could not create debugfs "
795 "'%s' directory\n", call->name);
796 return -1;
797 }
798
6d723736
SR
799 if (call->regfunc)
800 entry = trace_create_file("enable", 0644, call->dir, call,
801 &ftrace_enable_fops);
1473e441 802
6d723736
SR
803 if (call->id)
804 entry = trace_create_file("id", 0444, call->dir, call,
805 &ftrace_event_id_fops);
23725aee 806
cf027f64
TZ
807 if (call->define_fields) {
808 ret = call->define_fields();
809 if (ret < 0) {
810 pr_warning("Could not initialize trace point"
811 " events/%s\n", call->name);
812 return ret;
813 }
6d723736
SR
814 entry = trace_create_file("filter", 0644, call->dir, call,
815 &ftrace_event_filter_fops);
cf027f64
TZ
816 }
817
981d081e
SR
818 /* A trace may not want to export its format */
819 if (!call->show_format)
820 return 0;
821
6d723736
SR
822 entry = trace_create_file("format", 0444, call->dir, call,
823 &ftrace_event_format_fops);
824
825 return 0;
826}
827
828#define for_each_event(event, start, end) \
829 for (event = start; \
830 (unsigned long)event < (unsigned long)end; \
831 event++)
832
61f919a1 833#ifdef CONFIG_MODULES
6d723736
SR
834static void trace_module_add_events(struct module *mod)
835{
836 struct ftrace_event_call *call, *start, *end;
837 struct dentry *d_events;
838
839 start = mod->trace_events;
840 end = mod->trace_events + mod->num_trace_events;
841
842 if (start == end)
843 return;
844
845 d_events = event_trace_events_dir();
846 if (!d_events)
847 return;
848
849 for_each_event(call, start, end) {
850 /* The linker may leave blanks */
851 if (!call->name)
852 continue;
853 call->mod = mod;
854 list_add(&call->list, &ftrace_events);
855 event_create_dir(call, d_events);
856 }
857}
858
859static void trace_module_remove_events(struct module *mod)
860{
861 struct ftrace_event_call *call, *p;
862
863 list_for_each_entry_safe(call, p, &ftrace_events, list) {
864 if (call->mod == mod) {
865 if (call->enabled) {
866 call->enabled = 0;
867 call->unregfunc();
868 }
869 if (call->event)
870 unregister_ftrace_event(call->event);
871 debugfs_remove_recursive(call->dir);
872 list_del(&call->list);
873 }
874 }
875}
876
61f919a1
SR
877static int trace_module_notify(struct notifier_block *self,
878 unsigned long val, void *data)
6d723736
SR
879{
880 struct module *mod = data;
881
882 mutex_lock(&event_mutex);
883 switch (val) {
884 case MODULE_STATE_COMING:
885 trace_module_add_events(mod);
886 break;
887 case MODULE_STATE_GOING:
888 trace_module_remove_events(mod);
889 break;
890 }
891 mutex_unlock(&event_mutex);
fd994989 892
1473e441
SR
893 return 0;
894}
61f919a1
SR
895#else
896static int trace_module_notify(struct notifier_block *self,
897 unsigned long val, void *data)
898{
899 return 0;
900}
901#endif /* CONFIG_MODULES */
1473e441 902
6d723736
SR
903struct notifier_block trace_module_nb = {
904 .notifier_call = trace_module_notify,
905 .priority = 0,
906};
907
a59fd602
SR
908extern struct ftrace_event_call __start_ftrace_events[];
909extern struct ftrace_event_call __stop_ftrace_events[];
910
b77e38aa
SR
911static __init int event_trace_init(void)
912{
a59fd602 913 struct ftrace_event_call *call;
b77e38aa
SR
914 struct dentry *d_tracer;
915 struct dentry *entry;
1473e441 916 struct dentry *d_events;
6d723736 917 int ret;
b77e38aa
SR
918
919 d_tracer = tracing_init_dentry();
920 if (!d_tracer)
921 return 0;
922
2314c4ae
SR
923 entry = debugfs_create_file("available_events", 0444, d_tracer,
924 (void *)&show_event_seq_ops,
925 &ftrace_avail_fops);
926 if (!entry)
927 pr_warning("Could not create debugfs "
928 "'available_events' entry\n");
929
b77e38aa
SR
930 entry = debugfs_create_file("set_event", 0644, d_tracer,
931 (void *)&show_set_event_seq_ops,
932 &ftrace_set_event_fops);
933 if (!entry)
934 pr_warning("Could not create debugfs "
935 "'set_event' entry\n");
936
1473e441
SR
937 d_events = event_trace_events_dir();
938 if (!d_events)
939 return 0;
940
d1b182a8
SR
941 /* ring buffer internal formats */
942 trace_create_file("header_page", 0444, d_events,
943 ring_buffer_print_page_header,
944 &ftrace_show_header_fops);
945
946 trace_create_file("header_event", 0444, d_events,
947 ring_buffer_print_entry_header,
948 &ftrace_show_header_fops);
949
6d723736 950 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1473e441
SR
951 /* The linker may leave blanks */
952 if (!call->name)
953 continue;
a59fd602 954 list_add(&call->list, &ftrace_events);
1473e441
SR
955 event_create_dir(call, d_events);
956 }
957
6d723736
SR
958 ret = register_module_notifier(&trace_module_nb);
959 if (!ret)
960 pr_warning("Failed to register trace events module notifier\n");
961
b77e38aa
SR
962 return 0;
963}
964fs_initcall(event_trace_init);
e6187007
SR
965
966#ifdef CONFIG_FTRACE_STARTUP_TEST
967
968static DEFINE_SPINLOCK(test_spinlock);
969static DEFINE_SPINLOCK(test_spinlock_irq);
970static DEFINE_MUTEX(test_mutex);
971
972static __init void test_work(struct work_struct *dummy)
973{
974 spin_lock(&test_spinlock);
975 spin_lock_irq(&test_spinlock_irq);
976 udelay(1);
977 spin_unlock_irq(&test_spinlock_irq);
978 spin_unlock(&test_spinlock);
979
980 mutex_lock(&test_mutex);
981 msleep(1);
982 mutex_unlock(&test_mutex);
983}
984
985static __init int event_test_thread(void *unused)
986{
987 void *test_malloc;
988
989 test_malloc = kmalloc(1234, GFP_KERNEL);
990 if (!test_malloc)
991 pr_info("failed to kmalloc\n");
992
993 schedule_on_each_cpu(test_work);
994
995 kfree(test_malloc);
996
997 set_current_state(TASK_INTERRUPTIBLE);
998 while (!kthread_should_stop())
999 schedule();
1000
1001 return 0;
1002}
1003
1004/*
1005 * Do various things that may trigger events.
1006 */
1007static __init void event_test_stuff(void)
1008{
1009 struct task_struct *test_thread;
1010
1011 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1012 msleep(1);
1013 kthread_stop(test_thread);
1014}
1015
1016/*
1017 * For every trace event defined, we will test each trace point separately,
1018 * and then by groups, and finally all trace points.
1019 */
1020static __init int event_trace_self_tests(void)
1021{
1022 struct ftrace_event_call *call;
1023 struct event_subsystem *system;
1024 char *sysname;
1025 int ret;
1026
1027 pr_info("Running tests on trace events:\n");
1028
1029 list_for_each_entry(call, &ftrace_events, list) {
1030
1031 /* Only test those that have a regfunc */
1032 if (!call->regfunc)
1033 continue;
1034
1035 pr_info("Testing event %s: ", call->name);
1036
1037 /*
1038 * If an event is already enabled, someone is using
1039 * it and the self test should not be on.
1040 */
1041 if (call->enabled) {
1042 pr_warning("Enabled event during self test!\n");
1043 WARN_ON_ONCE(1);
1044 continue;
1045 }
1046
1047 call->enabled = 1;
1048 call->regfunc();
1049
1050 event_test_stuff();
1051
1052 call->unregfunc();
1053 call->enabled = 0;
1054
1055 pr_cont("OK\n");
1056 }
1057
1058 /* Now test at the sub system level */
1059
1060 pr_info("Running tests on trace event systems:\n");
1061
1062 list_for_each_entry(system, &event_subsystems, list) {
1063
1064 /* the ftrace system is special, skip it */
1065 if (strcmp(system->name, "ftrace") == 0)
1066 continue;
1067
1068 pr_info("Testing event system %s: ", system->name);
1069
1070 /* ftrace_set_clr_event can modify the name passed in. */
1071 sysname = kstrdup(system->name, GFP_KERNEL);
1072 if (WARN_ON(!sysname)) {
1073 pr_warning("Can't allocate memory, giving up!\n");
1074 return 0;
1075 }
1076 ret = ftrace_set_clr_event(sysname, 1);
1077 kfree(sysname);
1078 if (WARN_ON_ONCE(ret)) {
1079 pr_warning("error enabling system %s\n",
1080 system->name);
1081 continue;
1082 }
1083
1084 event_test_stuff();
1085
1086 sysname = kstrdup(system->name, GFP_KERNEL);
1087 if (WARN_ON(!sysname)) {
1088 pr_warning("Can't allocate memory, giving up!\n");
1089 return 0;
1090 }
1091 ret = ftrace_set_clr_event(sysname, 0);
1092 kfree(sysname);
1093
1094 if (WARN_ON_ONCE(ret))
1095 pr_warning("error disabling system %s\n",
1096 system->name);
1097
1098 pr_cont("OK\n");
1099 }
1100
1101 /* Test with all events enabled */
1102
1103 pr_info("Running tests on all trace events:\n");
1104 pr_info("Testing all events: ");
1105
1106 sysname = kmalloc(4, GFP_KERNEL);
1107 if (WARN_ON(!sysname)) {
1108 pr_warning("Can't allocate memory, giving up!\n");
1109 return 0;
1110 }
1111 memcpy(sysname, "*:*", 4);
1112 ret = ftrace_set_clr_event(sysname, 1);
1113 if (WARN_ON_ONCE(ret)) {
1114 kfree(sysname);
1115 pr_warning("error enabling all events\n");
1116 return 0;
1117 }
1118
1119 event_test_stuff();
1120
1121 /* reset sysname */
1122 memcpy(sysname, "*:*", 4);
1123 ret = ftrace_set_clr_event(sysname, 0);
1124 kfree(sysname);
1125
1126 if (WARN_ON_ONCE(ret)) {
1127 pr_warning("error disabling all events\n");
1128 return 0;
1129 }
1130
1131 pr_cont("OK\n");
1132
1133 return 0;
1134}
1135
1136late_initcall(event_trace_self_tests);
1137
1138#endif