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