tracing/function-graph: Use correct string size for snprintf
[linux-2.6-block.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>
5a0e3ad6 18#include <linux/slab.h>
e6187007 19#include <linux/delay.h>
b77e38aa 20
020e5f85
LZ
21#include <asm/setup.h>
22
91729ef9 23#include "trace_output.h"
b77e38aa 24
4e5292ea 25#undef TRACE_SYSTEM
b628b3e6
SR
26#define TRACE_SYSTEM "TRACE_SYSTEM"
27
20c8928a 28DEFINE_MUTEX(event_mutex);
11a241a3 29
a59fd602 30LIST_HEAD(ftrace_events);
8728fe50 31LIST_HEAD(ftrace_common_fields);
a59fd602 32
2e33af02
SR
33struct list_head *
34trace_get_fields(struct ftrace_event_call *event_call)
35{
36 if (!event_call->class->get_fields)
37 return &event_call->class->fields;
38 return event_call->class->get_fields(event_call);
39}
40
8728fe50
LZ
41static int __trace_define_field(struct list_head *head, const char *type,
42 const char *name, int offset, int size,
43 int is_signed, int filter_type)
cf027f64
TZ
44{
45 struct ftrace_event_field *field;
46
fe9f57f2 47 field = kzalloc(sizeof(*field), GFP_KERNEL);
cf027f64
TZ
48 if (!field)
49 goto err;
fe9f57f2 50
cf027f64
TZ
51 field->name = kstrdup(name, GFP_KERNEL);
52 if (!field->name)
53 goto err;
fe9f57f2 54
cf027f64
TZ
55 field->type = kstrdup(type, GFP_KERNEL);
56 if (!field->type)
57 goto err;
fe9f57f2 58
43b51ead
LZ
59 if (filter_type == FILTER_OTHER)
60 field->filter_type = filter_assign_type(type);
61 else
62 field->filter_type = filter_type;
63
cf027f64
TZ
64 field->offset = offset;
65 field->size = size;
a118e4d1 66 field->is_signed = is_signed;
aa38e9fc 67
2e33af02 68 list_add(&field->link, head);
cf027f64
TZ
69
70 return 0;
fe9f57f2 71
cf027f64 72err:
7b60997f 73 if (field)
cf027f64 74 kfree(field->name);
cf027f64 75 kfree(field);
fe9f57f2 76
cf027f64
TZ
77 return -ENOMEM;
78}
8728fe50
LZ
79
80int trace_define_field(struct ftrace_event_call *call, const char *type,
81 const char *name, int offset, int size, int is_signed,
82 int filter_type)
83{
84 struct list_head *head;
85
86 if (WARN_ON(!call->class))
87 return 0;
88
89 head = trace_get_fields(call);
90 return __trace_define_field(head, type, name, offset, size,
91 is_signed, filter_type);
92}
17c873ec 93EXPORT_SYMBOL_GPL(trace_define_field);
cf027f64 94
e647d6b3 95#define __common_field(type, item) \
8728fe50
LZ
96 ret = __trace_define_field(&ftrace_common_fields, #type, \
97 "common_" #item, \
98 offsetof(typeof(ent), item), \
99 sizeof(ent.item), \
100 is_signed_type(type), FILTER_OTHER); \
e647d6b3
LZ
101 if (ret) \
102 return ret;
103
8728fe50 104static int trace_define_common_fields(void)
e647d6b3
LZ
105{
106 int ret;
107 struct trace_entry ent;
108
109 __common_field(unsigned short, type);
110 __common_field(unsigned char, flags);
111 __common_field(unsigned char, preempt_count);
112 __common_field(int, pid);
637e7e86 113 __common_field(int, lock_depth);
e647d6b3
LZ
114
115 return ret;
116}
117
bd1a5c84 118void trace_destroy_fields(struct ftrace_event_call *call)
2df75e41
LZ
119{
120 struct ftrace_event_field *field, *next;
2e33af02 121 struct list_head *head;
2df75e41 122
2e33af02
SR
123 head = trace_get_fields(call);
124 list_for_each_entry_safe(field, next, head, link) {
2df75e41
LZ
125 list_del(&field->link);
126 kfree(field->type);
127 kfree(field->name);
128 kfree(field);
129 }
130}
131
87d9b4e1
LZ
132int trace_event_raw_init(struct ftrace_event_call *call)
133{
134 int id;
135
80decc70 136 id = register_ftrace_event(&call->event);
87d9b4e1
LZ
137 if (!id)
138 return -ENODEV;
87d9b4e1
LZ
139
140 return 0;
141}
142EXPORT_SYMBOL_GPL(trace_event_raw_init);
143
3b8e4273 144static int ftrace_event_enable_disable(struct ftrace_event_call *call,
fd994989
SR
145 int enable)
146{
3b8e4273
LZ
147 int ret = 0;
148
fd994989
SR
149 switch (enable) {
150 case 0:
553552ce
SR
151 if (call->flags & TRACE_EVENT_FL_ENABLED) {
152 call->flags &= ~TRACE_EVENT_FL_ENABLED;
b11c53e1 153 tracing_stop_cmdline_record();
2239291a
SR
154 if (call->class->reg)
155 call->class->reg(call, TRACE_REG_UNREGISTER);
156 else
157 tracepoint_probe_unregister(call->name,
158 call->class->probe,
159 call);
fd994989 160 }
fd994989
SR
161 break;
162 case 1:
553552ce 163 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
b11c53e1 164 tracing_start_cmdline_record();
2239291a
SR
165 if (call->class->reg)
166 ret = call->class->reg(call, TRACE_REG_REGISTER);
167 else
168 ret = tracepoint_probe_register(call->name,
169 call->class->probe,
170 call);
3b8e4273
LZ
171 if (ret) {
172 tracing_stop_cmdline_record();
173 pr_info("event trace: Could not enable event "
174 "%s\n", call->name);
175 break;
176 }
553552ce 177 call->flags |= TRACE_EVENT_FL_ENABLED;
fd994989 178 }
fd994989
SR
179 break;
180 }
3b8e4273
LZ
181
182 return ret;
fd994989
SR
183}
184
0e907c99
Z
185static void ftrace_clear_events(void)
186{
187 struct ftrace_event_call *call;
188
189 mutex_lock(&event_mutex);
190 list_for_each_entry(call, &ftrace_events, list) {
191 ftrace_event_enable_disable(call, 0);
192 }
193 mutex_unlock(&event_mutex);
194}
195
8f31bfe5
LZ
196/*
197 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
198 */
199static int __ftrace_set_clr_event(const char *match, const char *sub,
200 const char *event, int set)
b77e38aa 201{
a59fd602 202 struct ftrace_event_call *call;
29f93943 203 int ret = -EINVAL;
8f31bfe5
LZ
204
205 mutex_lock(&event_mutex);
206 list_for_each_entry(call, &ftrace_events, list) {
207
2239291a
SR
208 if (!call->name || !call->class ||
209 (!call->class->probe && !call->class->reg))
8f31bfe5
LZ
210 continue;
211
212 if (match &&
213 strcmp(match, call->name) != 0 &&
8f082018 214 strcmp(match, call->class->system) != 0)
8f31bfe5
LZ
215 continue;
216
8f082018 217 if (sub && strcmp(sub, call->class->system) != 0)
8f31bfe5
LZ
218 continue;
219
220 if (event && strcmp(event, call->name) != 0)
221 continue;
222
223 ftrace_event_enable_disable(call, set);
224
225 ret = 0;
226 }
227 mutex_unlock(&event_mutex);
228
229 return ret;
230}
231
232static int ftrace_set_clr_event(char *buf, int set)
233{
b628b3e6 234 char *event = NULL, *sub = NULL, *match;
b628b3e6
SR
235
236 /*
237 * The buf format can be <subsystem>:<event-name>
238 * *:<event-name> means any event by that name.
239 * :<event-name> is the same.
240 *
241 * <subsystem>:* means all events in that subsystem
242 * <subsystem>: means the same.
243 *
244 * <name> (no ':') means all events in a subsystem with
245 * the name <name> or any event that matches <name>
246 */
247
248 match = strsep(&buf, ":");
249 if (buf) {
250 sub = match;
251 event = buf;
252 match = NULL;
253
254 if (!strlen(sub) || strcmp(sub, "*") == 0)
255 sub = NULL;
256 if (!strlen(event) || strcmp(event, "*") == 0)
257 event = NULL;
258 }
b77e38aa 259
8f31bfe5 260 return __ftrace_set_clr_event(match, sub, event, set);
b77e38aa
SR
261}
262
4671c794
SR
263/**
264 * trace_set_clr_event - enable or disable an event
265 * @system: system name to match (NULL for any system)
266 * @event: event name to match (NULL for all events, within system)
267 * @set: 1 to enable, 0 to disable
268 *
269 * This is a way for other parts of the kernel to enable or disable
270 * event recording.
271 *
272 * Returns 0 on success, -EINVAL if the parameters do not match any
273 * registered events.
274 */
275int trace_set_clr_event(const char *system, const char *event, int set)
276{
277 return __ftrace_set_clr_event(NULL, system, event, set);
278}
279
b77e38aa
SR
280/* 128 should be much more than enough */
281#define EVENT_BUF_SIZE 127
282
283static ssize_t
284ftrace_event_write(struct file *file, const char __user *ubuf,
285 size_t cnt, loff_t *ppos)
286{
48966364 287 struct trace_parser parser;
4ba7978e 288 ssize_t read, ret;
b77e38aa 289
4ba7978e 290 if (!cnt)
b77e38aa
SR
291 return 0;
292
1852fcce
SR
293 ret = tracing_update_buffers();
294 if (ret < 0)
295 return ret;
296
48966364 297 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
b77e38aa
SR
298 return -ENOMEM;
299
48966364 300 read = trace_get_user(&parser, ubuf, cnt, ppos);
301
4ba7978e 302 if (read >= 0 && trace_parser_loaded((&parser))) {
48966364 303 int set = 1;
b77e38aa 304
48966364 305 if (*parser.buffer == '!')
b77e38aa 306 set = 0;
b77e38aa 307
48966364 308 parser.buffer[parser.idx] = 0;
309
310 ret = ftrace_set_clr_event(parser.buffer + !set, set);
b77e38aa 311 if (ret)
48966364 312 goto out_put;
b77e38aa 313 }
b77e38aa
SR
314
315 ret = read;
316
48966364 317 out_put:
318 trace_parser_put(&parser);
b77e38aa
SR
319
320 return ret;
321}
322
323static void *
324t_next(struct seq_file *m, void *v, loff_t *pos)
325{
30bd39cd 326 struct ftrace_event_call *call = v;
b77e38aa
SR
327
328 (*pos)++;
329
30bd39cd 330 list_for_each_entry_continue(call, &ftrace_events, list) {
40e26815
SR
331 /*
332 * The ftrace subsystem is for showing formats only.
333 * They can not be enabled or disabled via the event files.
334 */
2239291a 335 if (call->class && (call->class->probe || call->class->reg))
30bd39cd 336 return call;
40e26815 337 }
b77e38aa 338
30bd39cd 339 return NULL;
b77e38aa
SR
340}
341
342static void *t_start(struct seq_file *m, loff_t *pos)
343{
30bd39cd 344 struct ftrace_event_call *call;
e1c7e2a6
LZ
345 loff_t l;
346
20c8928a 347 mutex_lock(&event_mutex);
e1c7e2a6 348
30bd39cd 349 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
e1c7e2a6 350 for (l = 0; l <= *pos; ) {
30bd39cd 351 call = t_next(m, call, &l);
e1c7e2a6
LZ
352 if (!call)
353 break;
354 }
355 return call;
b77e38aa
SR
356}
357
358static void *
359s_next(struct seq_file *m, void *v, loff_t *pos)
360{
30bd39cd 361 struct ftrace_event_call *call = v;
b77e38aa
SR
362
363 (*pos)++;
364
30bd39cd 365 list_for_each_entry_continue(call, &ftrace_events, list) {
553552ce 366 if (call->flags & TRACE_EVENT_FL_ENABLED)
30bd39cd 367 return call;
b77e38aa
SR
368 }
369
30bd39cd 370 return NULL;
b77e38aa
SR
371}
372
373static void *s_start(struct seq_file *m, loff_t *pos)
374{
30bd39cd 375 struct ftrace_event_call *call;
e1c7e2a6
LZ
376 loff_t l;
377
20c8928a 378 mutex_lock(&event_mutex);
e1c7e2a6 379
30bd39cd 380 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
e1c7e2a6 381 for (l = 0; l <= *pos; ) {
30bd39cd 382 call = s_next(m, call, &l);
e1c7e2a6
LZ
383 if (!call)
384 break;
385 }
386 return call;
b77e38aa
SR
387}
388
389static int t_show(struct seq_file *m, void *v)
390{
391 struct ftrace_event_call *call = v;
392
8f082018
SR
393 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
394 seq_printf(m, "%s:", call->class->system);
b77e38aa
SR
395 seq_printf(m, "%s\n", call->name);
396
397 return 0;
398}
399
400static void t_stop(struct seq_file *m, void *p)
401{
20c8928a 402 mutex_unlock(&event_mutex);
b77e38aa
SR
403}
404
405static int
406ftrace_event_seq_open(struct inode *inode, struct file *file)
407{
b77e38aa
SR
408 const struct seq_operations *seq_ops;
409
410 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 411 (file->f_flags & O_TRUNC))
b77e38aa
SR
412 ftrace_clear_events();
413
414 seq_ops = inode->i_private;
20c8928a 415 return seq_open(file, seq_ops);
b77e38aa
SR
416}
417
1473e441
SR
418static ssize_t
419event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
420 loff_t *ppos)
421{
422 struct ftrace_event_call *call = filp->private_data;
423 char *buf;
424
553552ce 425 if (call->flags & TRACE_EVENT_FL_ENABLED)
1473e441
SR
426 buf = "1\n";
427 else
428 buf = "0\n";
429
430 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
431}
432
433static ssize_t
434event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
435 loff_t *ppos)
436{
437 struct ftrace_event_call *call = filp->private_data;
438 char buf[64];
439 unsigned long val;
440 int ret;
441
442 if (cnt >= sizeof(buf))
443 return -EINVAL;
444
445 if (copy_from_user(&buf, ubuf, cnt))
446 return -EFAULT;
447
448 buf[cnt] = 0;
449
450 ret = strict_strtoul(buf, 10, &val);
451 if (ret < 0)
452 return ret;
453
1852fcce
SR
454 ret = tracing_update_buffers();
455 if (ret < 0)
456 return ret;
457
1473e441
SR
458 switch (val) {
459 case 0:
1473e441 460 case 1:
11a241a3 461 mutex_lock(&event_mutex);
3b8e4273 462 ret = ftrace_event_enable_disable(call, val);
11a241a3 463 mutex_unlock(&event_mutex);
1473e441
SR
464 break;
465
466 default:
467 return -EINVAL;
468 }
469
470 *ppos += cnt;
471
3b8e4273 472 return ret ? ret : cnt;
1473e441
SR
473}
474
8ae79a13
SR
475static ssize_t
476system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
477 loff_t *ppos)
478{
c142b15d 479 const char set_to_char[4] = { '?', '0', '1', 'X' };
8ae79a13
SR
480 const char *system = filp->private_data;
481 struct ftrace_event_call *call;
482 char buf[2];
c142b15d 483 int set = 0;
8ae79a13
SR
484 int ret;
485
8ae79a13
SR
486 mutex_lock(&event_mutex);
487 list_for_each_entry(call, &ftrace_events, list) {
2239291a
SR
488 if (!call->name || !call->class ||
489 (!call->class->probe && !call->class->reg))
8ae79a13
SR
490 continue;
491
8f082018 492 if (system && strcmp(call->class->system, system) != 0)
8ae79a13
SR
493 continue;
494
495 /*
496 * We need to find out if all the events are set
497 * or if all events or cleared, or if we have
498 * a mixture.
499 */
553552ce 500 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
c142b15d 501
8ae79a13
SR
502 /*
503 * If we have a mixture, no need to look further.
504 */
c142b15d 505 if (set == 3)
8ae79a13
SR
506 break;
507 }
508 mutex_unlock(&event_mutex);
509
c142b15d 510 buf[0] = set_to_char[set];
8ae79a13 511 buf[1] = '\n';
8ae79a13
SR
512
513 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
514
515 return ret;
516}
517
518static ssize_t
519system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
520 loff_t *ppos)
521{
522 const char *system = filp->private_data;
523 unsigned long val;
8ae79a13
SR
524 char buf[64];
525 ssize_t ret;
526
527 if (cnt >= sizeof(buf))
528 return -EINVAL;
529
530 if (copy_from_user(&buf, ubuf, cnt))
531 return -EFAULT;
532
533 buf[cnt] = 0;
534
535 ret = strict_strtoul(buf, 10, &val);
536 if (ret < 0)
537 return ret;
538
539 ret = tracing_update_buffers();
540 if (ret < 0)
541 return ret;
542
8f31bfe5 543 if (val != 0 && val != 1)
8ae79a13 544 return -EINVAL;
8ae79a13 545
8f31bfe5 546 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
8ae79a13 547 if (ret)
8f31bfe5 548 goto out;
8ae79a13
SR
549
550 ret = cnt;
551
8f31bfe5 552out:
8ae79a13
SR
553 *ppos += cnt;
554
555 return ret;
556}
557
8728fe50 558static void print_event_fields(struct trace_seq *s, struct list_head *head)
981d081e 559{
5a65e956 560 struct ftrace_event_field *field;
981d081e 561
2e33af02 562 list_for_each_entry_reverse(field, head, link) {
5a65e956
LJ
563 /*
564 * Smartly shows the array type(except dynamic array).
565 * Normal:
566 * field:TYPE VAR
567 * If TYPE := TYPE[LEN], it is shown:
568 * field:TYPE VAR[LEN]
569 */
570 const char *array_descriptor = strchr(field->type, '[');
571
572 if (!strncmp(field->type, "__data_loc", 10))
573 array_descriptor = NULL;
574
575 if (!array_descriptor) {
8728fe50 576 trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
5a65e956
LJ
577 "\tsize:%u;\tsigned:%d;\n",
578 field->type, field->name, field->offset,
579 field->size, !!field->is_signed);
580 } else {
8728fe50 581 trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
5a65e956
LJ
582 "\tsize:%u;\tsigned:%d;\n",
583 (int)(array_descriptor - field->type),
584 field->type, field->name,
585 array_descriptor, field->offset,
586 field->size, !!field->is_signed);
587 }
8728fe50
LZ
588 }
589}
5a65e956 590
8728fe50
LZ
591static ssize_t
592event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
593 loff_t *ppos)
594{
595 struct ftrace_event_call *call = filp->private_data;
596 struct list_head *head;
597 struct trace_seq *s;
598 char *buf;
599 int r;
5a65e956 600
8728fe50
LZ
601 if (*ppos)
602 return 0;
5a65e956 603
8728fe50
LZ
604 s = kmalloc(sizeof(*s), GFP_KERNEL);
605 if (!s)
606 return -ENOMEM;
607
608 trace_seq_init(s);
609
610 trace_seq_printf(s, "name: %s\n", call->name);
611 trace_seq_printf(s, "ID: %d\n", call->event.type);
612 trace_seq_printf(s, "format:\n");
613
614 /* print common fields */
615 print_event_fields(s, &ftrace_common_fields);
616
617 trace_seq_putc(s, '\n');
618
619 /* print event specific fields */
620 head = trace_get_fields(call);
621 print_event_fields(s, head);
622
623 r = trace_seq_printf(s, "\nprint fmt: %s\n", call->print_fmt);
5a65e956 624
981d081e
SR
625 if (!r) {
626 /*
627 * ug! The format output is bigger than a PAGE!!
628 */
629 buf = "FORMAT TOO BIG\n";
630 r = simple_read_from_buffer(ubuf, cnt, ppos,
631 buf, strlen(buf));
632 goto out;
633 }
634
635 r = simple_read_from_buffer(ubuf, cnt, ppos,
636 s->buffer, s->len);
637 out:
638 kfree(s);
639 return r;
640}
641
23725aee
PZ
642static ssize_t
643event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
644{
645 struct ftrace_event_call *call = filp->private_data;
646 struct trace_seq *s;
647 int r;
648
649 if (*ppos)
650 return 0;
651
652 s = kmalloc(sizeof(*s), GFP_KERNEL);
653 if (!s)
654 return -ENOMEM;
655
656 trace_seq_init(s);
32c0edae 657 trace_seq_printf(s, "%d\n", call->event.type);
23725aee
PZ
658
659 r = simple_read_from_buffer(ubuf, cnt, ppos,
660 s->buffer, s->len);
661 kfree(s);
662 return r;
663}
664
7ce7e424
TZ
665static ssize_t
666event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
667 loff_t *ppos)
668{
669 struct ftrace_event_call *call = filp->private_data;
670 struct trace_seq *s;
671 int r;
672
673 if (*ppos)
674 return 0;
675
676 s = kmalloc(sizeof(*s), GFP_KERNEL);
677 if (!s)
678 return -ENOMEM;
679
680 trace_seq_init(s);
681
8b372562 682 print_event_filter(call, s);
4bda2d51 683 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
7ce7e424
TZ
684
685 kfree(s);
686
687 return r;
688}
689
690static ssize_t
691event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
692 loff_t *ppos)
693{
694 struct ftrace_event_call *call = filp->private_data;
8b372562 695 char *buf;
7ce7e424
TZ
696 int err;
697
8b372562 698 if (cnt >= PAGE_SIZE)
7ce7e424
TZ
699 return -EINVAL;
700
8b372562
TZ
701 buf = (char *)__get_free_page(GFP_TEMPORARY);
702 if (!buf)
7ce7e424
TZ
703 return -ENOMEM;
704
8b372562
TZ
705 if (copy_from_user(buf, ubuf, cnt)) {
706 free_page((unsigned long) buf);
707 return -EFAULT;
7ce7e424 708 }
8b372562 709 buf[cnt] = '\0';
7ce7e424 710
8b372562
TZ
711 err = apply_event_filter(call, buf);
712 free_page((unsigned long) buf);
713 if (err < 0)
44e9c8b7 714 return err;
0a19e53c 715
7ce7e424
TZ
716 *ppos += cnt;
717
718 return cnt;
719}
720
cfb180f3
TZ
721static ssize_t
722subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
723 loff_t *ppos)
724{
725 struct event_subsystem *system = filp->private_data;
726 struct trace_seq *s;
727 int r;
728
729 if (*ppos)
730 return 0;
731
732 s = kmalloc(sizeof(*s), GFP_KERNEL);
733 if (!s)
734 return -ENOMEM;
735
736 trace_seq_init(s);
737
8b372562 738 print_subsystem_event_filter(system, s);
4bda2d51 739 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
cfb180f3
TZ
740
741 kfree(s);
742
743 return r;
744}
745
746static ssize_t
747subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
748 loff_t *ppos)
749{
750 struct event_subsystem *system = filp->private_data;
8b372562 751 char *buf;
cfb180f3
TZ
752 int err;
753
8b372562 754 if (cnt >= PAGE_SIZE)
cfb180f3
TZ
755 return -EINVAL;
756
8b372562
TZ
757 buf = (char *)__get_free_page(GFP_TEMPORARY);
758 if (!buf)
cfb180f3
TZ
759 return -ENOMEM;
760
8b372562
TZ
761 if (copy_from_user(buf, ubuf, cnt)) {
762 free_page((unsigned long) buf);
763 return -EFAULT;
cfb180f3 764 }
8b372562 765 buf[cnt] = '\0';
cfb180f3 766
8b372562
TZ
767 err = apply_subsystem_event_filter(system, buf);
768 free_page((unsigned long) buf);
769 if (err < 0)
44e9c8b7 770 return err;
cfb180f3
TZ
771
772 *ppos += cnt;
773
774 return cnt;
775}
776
d1b182a8
SR
777static ssize_t
778show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
779{
780 int (*func)(struct trace_seq *s) = filp->private_data;
781 struct trace_seq *s;
782 int r;
783
784 if (*ppos)
785 return 0;
786
787 s = kmalloc(sizeof(*s), GFP_KERNEL);
788 if (!s)
789 return -ENOMEM;
790
791 trace_seq_init(s);
792
793 func(s);
794 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
795
796 kfree(s);
797
798 return r;
799}
800
b77e38aa
SR
801static const struct seq_operations show_event_seq_ops = {
802 .start = t_start,
803 .next = t_next,
804 .show = t_show,
805 .stop = t_stop,
806};
807
808static const struct seq_operations show_set_event_seq_ops = {
809 .start = s_start,
810 .next = s_next,
811 .show = t_show,
812 .stop = t_stop,
813};
814
2314c4ae
SR
815static const struct file_operations ftrace_avail_fops = {
816 .open = ftrace_event_seq_open,
817 .read = seq_read,
818 .llseek = seq_lseek,
819 .release = seq_release,
820};
821
b77e38aa
SR
822static const struct file_operations ftrace_set_event_fops = {
823 .open = ftrace_event_seq_open,
824 .read = seq_read,
825 .write = ftrace_event_write,
826 .llseek = seq_lseek,
827 .release = seq_release,
828};
829
1473e441
SR
830static const struct file_operations ftrace_enable_fops = {
831 .open = tracing_open_generic,
832 .read = event_enable_read,
833 .write = event_enable_write,
834};
835
981d081e
SR
836static const struct file_operations ftrace_event_format_fops = {
837 .open = tracing_open_generic,
838 .read = event_format_read,
839};
840
23725aee
PZ
841static const struct file_operations ftrace_event_id_fops = {
842 .open = tracing_open_generic,
843 .read = event_id_read,
844};
845
7ce7e424
TZ
846static const struct file_operations ftrace_event_filter_fops = {
847 .open = tracing_open_generic,
848 .read = event_filter_read,
849 .write = event_filter_write,
850};
851
cfb180f3
TZ
852static const struct file_operations ftrace_subsystem_filter_fops = {
853 .open = tracing_open_generic,
854 .read = subsystem_filter_read,
855 .write = subsystem_filter_write,
856};
857
8ae79a13
SR
858static const struct file_operations ftrace_system_enable_fops = {
859 .open = tracing_open_generic,
860 .read = system_enable_read,
861 .write = system_enable_write,
862};
863
d1b182a8
SR
864static const struct file_operations ftrace_show_header_fops = {
865 .open = tracing_open_generic,
866 .read = show_header,
867};
868
1473e441
SR
869static struct dentry *event_trace_events_dir(void)
870{
871 static struct dentry *d_tracer;
872 static struct dentry *d_events;
873
874 if (d_events)
875 return d_events;
876
877 d_tracer = tracing_init_dentry();
878 if (!d_tracer)
879 return NULL;
880
881 d_events = debugfs_create_dir("events", d_tracer);
882 if (!d_events)
883 pr_warning("Could not create debugfs "
884 "'events' directory\n");
885
886 return d_events;
887}
888
6ecc2d1c
SR
889static LIST_HEAD(event_subsystems);
890
891static struct dentry *
892event_subsystem_dir(const char *name, struct dentry *d_events)
893{
894 struct event_subsystem *system;
e1112b4d 895 struct dentry *entry;
6ecc2d1c
SR
896
897 /* First see if we did not already create this dir */
898 list_for_each_entry(system, &event_subsystems, list) {
dc82ec98
XG
899 if (strcmp(system->name, name) == 0) {
900 system->nr_events++;
6ecc2d1c 901 return system->entry;
dc82ec98 902 }
6ecc2d1c
SR
903 }
904
905 /* need to create new entry */
906 system = kmalloc(sizeof(*system), GFP_KERNEL);
907 if (!system) {
908 pr_warning("No memory to create event subsystem %s\n",
909 name);
910 return d_events;
911 }
912
913 system->entry = debugfs_create_dir(name, d_events);
914 if (!system->entry) {
915 pr_warning("Could not create event subsystem %s\n",
916 name);
917 kfree(system);
918 return d_events;
919 }
920
dc82ec98 921 system->nr_events = 1;
6d723736
SR
922 system->name = kstrdup(name, GFP_KERNEL);
923 if (!system->name) {
924 debugfs_remove(system->entry);
925 kfree(system);
926 return d_events;
927 }
928
6ecc2d1c
SR
929 list_add(&system->list, &event_subsystems);
930
30e673b2 931 system->filter = NULL;
cfb180f3 932
8b372562
TZ
933 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
934 if (!system->filter) {
935 pr_warning("Could not allocate filter for subsystem "
936 "'%s'\n", name);
937 return system->entry;
938 }
939
e1112b4d
TZ
940 entry = debugfs_create_file("filter", 0644, system->entry, system,
941 &ftrace_subsystem_filter_fops);
8b372562
TZ
942 if (!entry) {
943 kfree(system->filter);
944 system->filter = NULL;
e1112b4d
TZ
945 pr_warning("Could not create debugfs "
946 "'%s/filter' entry\n", name);
8b372562 947 }
e1112b4d 948
f3f3f009
FW
949 trace_create_file("enable", 0644, system->entry,
950 (void *)system->name,
951 &ftrace_system_enable_fops);
8ae79a13 952
6ecc2d1c
SR
953 return system->entry;
954}
955
1473e441 956static int
701970b3
SR
957event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
958 const struct file_operations *id,
959 const struct file_operations *enable,
960 const struct file_operations *filter,
961 const struct file_operations *format)
1473e441 962{
2e33af02 963 struct list_head *head;
fd994989 964 int ret;
1473e441 965
6ecc2d1c
SR
966 /*
967 * If the trace point header did not define TRACE_SYSTEM
968 * then the system would be called "TRACE_SYSTEM".
969 */
8f082018
SR
970 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
971 d_events = event_subsystem_dir(call->class->system, d_events);
6ecc2d1c 972
1473e441
SR
973 call->dir = debugfs_create_dir(call->name, d_events);
974 if (!call->dir) {
975 pr_warning("Could not create debugfs "
976 "'%s' directory\n", call->name);
977 return -1;
978 }
979
2239291a 980 if (call->class->probe || call->class->reg)
f3f3f009
FW
981 trace_create_file("enable", 0644, call->dir, call,
982 enable);
1473e441 983
2239291a 984#ifdef CONFIG_PERF_EVENTS
32c0edae 985 if (call->event.type && (call->class->perf_probe || call->class->reg))
f3f3f009
FW
986 trace_create_file("id", 0444, call->dir, call,
987 id);
2239291a 988#endif
23725aee 989
c9d932cf
LZ
990 /*
991 * Other events may have the same class. Only update
992 * the fields if they are not already defined.
993 */
994 head = trace_get_fields(call);
995 if (list_empty(head)) {
996 ret = call->class->define_fields(call);
997 if (ret < 0) {
998 pr_warning("Could not initialize trace point"
999 " events/%s\n", call->name);
1000 return ret;
cf027f64
TZ
1001 }
1002 }
c9d932cf
LZ
1003 trace_create_file("filter", 0644, call->dir, call,
1004 filter);
cf027f64 1005
f3f3f009
FW
1006 trace_create_file("format", 0444, call->dir, call,
1007 format);
6d723736
SR
1008
1009 return 0;
1010}
1011
67ead0a6
LZ
1012static int
1013__trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
1014 const struct file_operations *id,
1015 const struct file_operations *enable,
1016 const struct file_operations *filter,
1017 const struct file_operations *format)
bd1a5c84
MH
1018{
1019 struct dentry *d_events;
1020 int ret;
6d723736 1021
67ead0a6 1022 /* The linker may leave blanks */
bd1a5c84
MH
1023 if (!call->name)
1024 return -EINVAL;
701970b3 1025
0405ab80
SR
1026 if (call->class->raw_init) {
1027 ret = call->class->raw_init(call);
bd1a5c84
MH
1028 if (ret < 0) {
1029 if (ret != -ENOSYS)
67ead0a6
LZ
1030 pr_warning("Could not initialize trace events/%s\n",
1031 call->name);
bd1a5c84
MH
1032 return ret;
1033 }
1034 }
701970b3 1035
bd1a5c84
MH
1036 d_events = event_trace_events_dir();
1037 if (!d_events)
1038 return -ENOENT;
1039
67ead0a6 1040 ret = event_create_dir(call, d_events, id, enable, filter, format);
88f70d75
MH
1041 if (!ret)
1042 list_add(&call->list, &ftrace_events);
67ead0a6 1043 call->mod = mod;
88f70d75 1044
588bebb7 1045 return ret;
bd1a5c84
MH
1046}
1047
1048/* Add an additional event_call dynamically */
1049int trace_add_event_call(struct ftrace_event_call *call)
1050{
1051 int ret;
1052 mutex_lock(&event_mutex);
67ead0a6
LZ
1053 ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1054 &ftrace_enable_fops,
1055 &ftrace_event_filter_fops,
1056 &ftrace_event_format_fops);
bd1a5c84
MH
1057 mutex_unlock(&event_mutex);
1058 return ret;
1059}
701970b3 1060
a2ca5e03
FW
1061static void remove_subsystem_dir(const char *name)
1062{
1063 struct event_subsystem *system;
1064
1065 if (strcmp(name, TRACE_SYSTEM) == 0)
1066 return;
1067
1068 list_for_each_entry(system, &event_subsystems, list) {
1069 if (strcmp(system->name, name) == 0) {
1070 if (!--system->nr_events) {
1071 struct event_filter *filter = system->filter;
1072
1073 debugfs_remove_recursive(system->entry);
1074 list_del(&system->list);
1075 if (filter) {
1076 kfree(filter->filter_string);
1077 kfree(filter);
1078 }
1079 kfree(system->name);
1080 kfree(system);
1081 }
1082 break;
1083 }
1084 }
1085}
1086
4fead8e4
MH
1087/*
1088 * Must be called under locking both of event_mutex and trace_event_mutex.
1089 */
bd1a5c84
MH
1090static void __trace_remove_event_call(struct ftrace_event_call *call)
1091{
1092 ftrace_event_enable_disable(call, 0);
80decc70
SR
1093 if (call->event.funcs)
1094 __unregister_ftrace_event(&call->event);
bd1a5c84
MH
1095 debugfs_remove_recursive(call->dir);
1096 list_del(&call->list);
1097 trace_destroy_fields(call);
1098 destroy_preds(call);
8f082018 1099 remove_subsystem_dir(call->class->system);
bd1a5c84
MH
1100}
1101
1102/* Remove an event_call */
1103void trace_remove_event_call(struct ftrace_event_call *call)
1104{
1105 mutex_lock(&event_mutex);
4fead8e4 1106 down_write(&trace_event_mutex);
bd1a5c84 1107 __trace_remove_event_call(call);
4fead8e4 1108 up_write(&trace_event_mutex);
bd1a5c84
MH
1109 mutex_unlock(&event_mutex);
1110}
1111
1112#define for_each_event(event, start, end) \
1113 for (event = start; \
1114 (unsigned long)event < (unsigned long)end; \
1115 event++)
1116
1117#ifdef CONFIG_MODULES
1118
1119static LIST_HEAD(ftrace_module_file_list);
1120
1121/*
1122 * Modules must own their file_operations to keep up with
1123 * reference counting.
1124 */
1125struct ftrace_module_file_ops {
1126 struct list_head list;
1127 struct module *mod;
1128 struct file_operations id;
1129 struct file_operations enable;
1130 struct file_operations format;
1131 struct file_operations filter;
1132};
1133
701970b3
SR
1134static struct ftrace_module_file_ops *
1135trace_create_file_ops(struct module *mod)
1136{
1137 struct ftrace_module_file_ops *file_ops;
1138
1139 /*
1140 * This is a bit of a PITA. To allow for correct reference
1141 * counting, modules must "own" their file_operations.
1142 * To do this, we allocate the file operations that will be
1143 * used in the event directory.
1144 */
1145
1146 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1147 if (!file_ops)
1148 return NULL;
1149
1150 file_ops->mod = mod;
1151
1152 file_ops->id = ftrace_event_id_fops;
1153 file_ops->id.owner = mod;
1154
1155 file_ops->enable = ftrace_enable_fops;
1156 file_ops->enable.owner = mod;
1157
1158 file_ops->filter = ftrace_event_filter_fops;
1159 file_ops->filter.owner = mod;
1160
1161 file_ops->format = ftrace_event_format_fops;
1162 file_ops->format.owner = mod;
1163
1164 list_add(&file_ops->list, &ftrace_module_file_list);
1165
1166 return file_ops;
1167}
1168
6d723736
SR
1169static void trace_module_add_events(struct module *mod)
1170{
701970b3 1171 struct ftrace_module_file_ops *file_ops = NULL;
6d723736 1172 struct ftrace_event_call *call, *start, *end;
6d723736
SR
1173
1174 start = mod->trace_events;
1175 end = mod->trace_events + mod->num_trace_events;
1176
1177 if (start == end)
1178 return;
1179
67ead0a6
LZ
1180 file_ops = trace_create_file_ops(mod);
1181 if (!file_ops)
6d723736
SR
1182 return;
1183
1184 for_each_event(call, start, end) {
67ead0a6 1185 __trace_add_event_call(call, mod,
88f70d75
MH
1186 &file_ops->id, &file_ops->enable,
1187 &file_ops->filter, &file_ops->format);
6d723736
SR
1188 }
1189}
1190
1191static void trace_module_remove_events(struct module *mod)
1192{
701970b3 1193 struct ftrace_module_file_ops *file_ops;
6d723736 1194 struct ftrace_event_call *call, *p;
9456f0fa 1195 bool found = false;
6d723736 1196
110bf2b7 1197 down_write(&trace_event_mutex);
6d723736
SR
1198 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1199 if (call->mod == mod) {
9456f0fa 1200 found = true;
bd1a5c84 1201 __trace_remove_event_call(call);
6d723736
SR
1202 }
1203 }
701970b3
SR
1204
1205 /* Now free the file_operations */
1206 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1207 if (file_ops->mod == mod)
1208 break;
1209 }
1210 if (&file_ops->list != &ftrace_module_file_list) {
1211 list_del(&file_ops->list);
1212 kfree(file_ops);
1213 }
9456f0fa
SR
1214
1215 /*
1216 * It is safest to reset the ring buffer if the module being unloaded
1217 * registered any events.
1218 */
1219 if (found)
1220 tracing_reset_current_online_cpus();
110bf2b7 1221 up_write(&trace_event_mutex);
6d723736
SR
1222}
1223
61f919a1
SR
1224static int trace_module_notify(struct notifier_block *self,
1225 unsigned long val, void *data)
6d723736
SR
1226{
1227 struct module *mod = data;
1228
1229 mutex_lock(&event_mutex);
1230 switch (val) {
1231 case MODULE_STATE_COMING:
1232 trace_module_add_events(mod);
1233 break;
1234 case MODULE_STATE_GOING:
1235 trace_module_remove_events(mod);
1236 break;
1237 }
1238 mutex_unlock(&event_mutex);
fd994989 1239
1473e441
SR
1240 return 0;
1241}
61f919a1
SR
1242#else
1243static int trace_module_notify(struct notifier_block *self,
1244 unsigned long val, void *data)
1245{
1246 return 0;
1247}
1248#endif /* CONFIG_MODULES */
1473e441 1249
ec827c7e 1250static struct notifier_block trace_module_nb = {
6d723736
SR
1251 .notifier_call = trace_module_notify,
1252 .priority = 0,
1253};
1254
a59fd602
SR
1255extern struct ftrace_event_call __start_ftrace_events[];
1256extern struct ftrace_event_call __stop_ftrace_events[];
1257
020e5f85
LZ
1258static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1259
1260static __init int setup_trace_event(char *str)
1261{
1262 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1263 ring_buffer_expanded = 1;
1264 tracing_selftest_disabled = 1;
1265
1266 return 1;
1267}
1268__setup("trace_event=", setup_trace_event);
1269
b77e38aa
SR
1270static __init int event_trace_init(void)
1271{
a59fd602 1272 struct ftrace_event_call *call;
b77e38aa
SR
1273 struct dentry *d_tracer;
1274 struct dentry *entry;
1473e441 1275 struct dentry *d_events;
6d723736 1276 int ret;
020e5f85
LZ
1277 char *buf = bootup_event_buf;
1278 char *token;
b77e38aa
SR
1279
1280 d_tracer = tracing_init_dentry();
1281 if (!d_tracer)
1282 return 0;
1283
2314c4ae
SR
1284 entry = debugfs_create_file("available_events", 0444, d_tracer,
1285 (void *)&show_event_seq_ops,
1286 &ftrace_avail_fops);
1287 if (!entry)
1288 pr_warning("Could not create debugfs "
1289 "'available_events' entry\n");
1290
b77e38aa
SR
1291 entry = debugfs_create_file("set_event", 0644, d_tracer,
1292 (void *)&show_set_event_seq_ops,
1293 &ftrace_set_event_fops);
1294 if (!entry)
1295 pr_warning("Could not create debugfs "
1296 "'set_event' entry\n");
1297
1473e441
SR
1298 d_events = event_trace_events_dir();
1299 if (!d_events)
1300 return 0;
1301
d1b182a8
SR
1302 /* ring buffer internal formats */
1303 trace_create_file("header_page", 0444, d_events,
1304 ring_buffer_print_page_header,
1305 &ftrace_show_header_fops);
1306
1307 trace_create_file("header_event", 0444, d_events,
1308 ring_buffer_print_entry_header,
1309 &ftrace_show_header_fops);
1310
8ae79a13 1311 trace_create_file("enable", 0644, d_events,
8f31bfe5 1312 NULL, &ftrace_system_enable_fops);
8ae79a13 1313
8728fe50
LZ
1314 if (trace_define_common_fields())
1315 pr_warning("tracing: Failed to allocate common fields");
1316
6d723736 1317 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
67ead0a6 1318 __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
88f70d75
MH
1319 &ftrace_enable_fops,
1320 &ftrace_event_filter_fops,
1321 &ftrace_event_format_fops);
1473e441
SR
1322 }
1323
020e5f85
LZ
1324 while (true) {
1325 token = strsep(&buf, ",");
1326
1327 if (!token)
1328 break;
1329 if (!*token)
1330 continue;
1331
1332 ret = ftrace_set_clr_event(token, 1);
1333 if (ret)
1334 pr_warning("Failed to enable trace event: %s\n", token);
1335 }
1336
6d723736 1337 ret = register_module_notifier(&trace_module_nb);
55379376 1338 if (ret)
6d723736
SR
1339 pr_warning("Failed to register trace events module notifier\n");
1340
b77e38aa
SR
1341 return 0;
1342}
1343fs_initcall(event_trace_init);
e6187007
SR
1344
1345#ifdef CONFIG_FTRACE_STARTUP_TEST
1346
1347static DEFINE_SPINLOCK(test_spinlock);
1348static DEFINE_SPINLOCK(test_spinlock_irq);
1349static DEFINE_MUTEX(test_mutex);
1350
1351static __init void test_work(struct work_struct *dummy)
1352{
1353 spin_lock(&test_spinlock);
1354 spin_lock_irq(&test_spinlock_irq);
1355 udelay(1);
1356 spin_unlock_irq(&test_spinlock_irq);
1357 spin_unlock(&test_spinlock);
1358
1359 mutex_lock(&test_mutex);
1360 msleep(1);
1361 mutex_unlock(&test_mutex);
1362}
1363
1364static __init int event_test_thread(void *unused)
1365{
1366 void *test_malloc;
1367
1368 test_malloc = kmalloc(1234, GFP_KERNEL);
1369 if (!test_malloc)
1370 pr_info("failed to kmalloc\n");
1371
1372 schedule_on_each_cpu(test_work);
1373
1374 kfree(test_malloc);
1375
1376 set_current_state(TASK_INTERRUPTIBLE);
1377 while (!kthread_should_stop())
1378 schedule();
1379
1380 return 0;
1381}
1382
1383/*
1384 * Do various things that may trigger events.
1385 */
1386static __init void event_test_stuff(void)
1387{
1388 struct task_struct *test_thread;
1389
1390 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1391 msleep(1);
1392 kthread_stop(test_thread);
1393}
1394
1395/*
1396 * For every trace event defined, we will test each trace point separately,
1397 * and then by groups, and finally all trace points.
1398 */
9ea21c1e 1399static __init void event_trace_self_tests(void)
e6187007
SR
1400{
1401 struct ftrace_event_call *call;
1402 struct event_subsystem *system;
e6187007
SR
1403 int ret;
1404
1405 pr_info("Running tests on trace events:\n");
1406
1407 list_for_each_entry(call, &ftrace_events, list) {
1408
2239291a
SR
1409 /* Only test those that have a probe */
1410 if (!call->class || !call->class->probe)
e6187007
SR
1411 continue;
1412
1f5a6b45
SR
1413/*
1414 * Testing syscall events here is pretty useless, but
1415 * we still do it if configured. But this is time consuming.
1416 * What we really need is a user thread to perform the
1417 * syscalls as we test.
1418 */
1419#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
8f082018
SR
1420 if (call->class->system &&
1421 strcmp(call->class->system, "syscalls") == 0)
1f5a6b45
SR
1422 continue;
1423#endif
1424
e6187007
SR
1425 pr_info("Testing event %s: ", call->name);
1426
1427 /*
1428 * If an event is already enabled, someone is using
1429 * it and the self test should not be on.
1430 */
553552ce 1431 if (call->flags & TRACE_EVENT_FL_ENABLED) {
e6187007
SR
1432 pr_warning("Enabled event during self test!\n");
1433 WARN_ON_ONCE(1);
1434 continue;
1435 }
1436
0e907c99 1437 ftrace_event_enable_disable(call, 1);
e6187007 1438 event_test_stuff();
0e907c99 1439 ftrace_event_enable_disable(call, 0);
e6187007
SR
1440
1441 pr_cont("OK\n");
1442 }
1443
1444 /* Now test at the sub system level */
1445
1446 pr_info("Running tests on trace event systems:\n");
1447
1448 list_for_each_entry(system, &event_subsystems, list) {
1449
1450 /* the ftrace system is special, skip it */
1451 if (strcmp(system->name, "ftrace") == 0)
1452 continue;
1453
1454 pr_info("Testing event system %s: ", system->name);
1455
8f31bfe5 1456 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
e6187007
SR
1457 if (WARN_ON_ONCE(ret)) {
1458 pr_warning("error enabling system %s\n",
1459 system->name);
1460 continue;
1461 }
1462
1463 event_test_stuff();
1464
8f31bfe5 1465 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
e6187007
SR
1466 if (WARN_ON_ONCE(ret))
1467 pr_warning("error disabling system %s\n",
1468 system->name);
1469
1470 pr_cont("OK\n");
1471 }
1472
1473 /* Test with all events enabled */
1474
1475 pr_info("Running tests on all trace events:\n");
1476 pr_info("Testing all events: ");
1477
8f31bfe5 1478 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
e6187007 1479 if (WARN_ON_ONCE(ret)) {
e6187007 1480 pr_warning("error enabling all events\n");
9ea21c1e 1481 return;
e6187007
SR
1482 }
1483
1484 event_test_stuff();
1485
1486 /* reset sysname */
8f31bfe5 1487 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
e6187007
SR
1488 if (WARN_ON_ONCE(ret)) {
1489 pr_warning("error disabling all events\n");
9ea21c1e 1490 return;
e6187007
SR
1491 }
1492
1493 pr_cont("OK\n");
9ea21c1e
SR
1494}
1495
1496#ifdef CONFIG_FUNCTION_TRACER
1497
245b2e70 1498static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
9ea21c1e
SR
1499
1500static void
1501function_test_events_call(unsigned long ip, unsigned long parent_ip)
1502{
1503 struct ring_buffer_event *event;
e77405ad 1504 struct ring_buffer *buffer;
9ea21c1e
SR
1505 struct ftrace_entry *entry;
1506 unsigned long flags;
1507 long disabled;
9ea21c1e
SR
1508 int cpu;
1509 int pc;
1510
1511 pc = preempt_count();
5168ae50 1512 preempt_disable_notrace();
9ea21c1e 1513 cpu = raw_smp_processor_id();
245b2e70 1514 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
9ea21c1e
SR
1515
1516 if (disabled != 1)
1517 goto out;
1518
1519 local_save_flags(flags);
1520
e77405ad
SR
1521 event = trace_current_buffer_lock_reserve(&buffer,
1522 TRACE_FN, sizeof(*entry),
9ea21c1e
SR
1523 flags, pc);
1524 if (!event)
1525 goto out;
1526 entry = ring_buffer_event_data(event);
1527 entry->ip = ip;
1528 entry->parent_ip = parent_ip;
1529
e77405ad 1530 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
9ea21c1e
SR
1531
1532 out:
245b2e70 1533 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
5168ae50 1534 preempt_enable_notrace();
9ea21c1e
SR
1535}
1536
1537static struct ftrace_ops trace_ops __initdata =
1538{
1539 .func = function_test_events_call,
1540};
1541
1542static __init void event_trace_self_test_with_function(void)
1543{
1544 register_ftrace_function(&trace_ops);
1545 pr_info("Running tests again, along with the function tracer\n");
1546 event_trace_self_tests();
1547 unregister_ftrace_function(&trace_ops);
1548}
1549#else
1550static __init void event_trace_self_test_with_function(void)
1551{
1552}
1553#endif
1554
1555static __init int event_trace_self_tests_init(void)
1556{
020e5f85
LZ
1557 if (!tracing_selftest_disabled) {
1558 event_trace_self_tests();
1559 event_trace_self_test_with_function();
1560 }
e6187007
SR
1561
1562 return 0;
1563}
1564
28d20e2d 1565late_initcall(event_trace_self_tests_init);
e6187007
SR
1566
1567#endif