tracing: Change f_start() to take event_mutex and verify i_private != NULL
[GitHub/exynos8895/android_kernel_samsung_universal8895.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
04295780
SR
30DEFINE_MUTEX(event_storage_mutex);
31EXPORT_SYMBOL_GPL(event_storage_mutex);
32
33char event_storage[EVENT_STORAGE_SIZE];
34EXPORT_SYMBOL_GPL(event_storage);
35
a59fd602 36LIST_HEAD(ftrace_events);
b3a8c6fd 37static LIST_HEAD(ftrace_common_fields);
a59fd602 38
d1a29143
SR
39#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
40
41static struct kmem_cache *field_cachep;
42static struct kmem_cache *file_cachep;
43
6e94a780
SR
44#define SYSTEM_FL_FREE_NAME (1 << 31)
45
46static inline int system_refcount(struct event_subsystem *system)
47{
48 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
49}
50
51static int system_refcount_inc(struct event_subsystem *system)
52{
53 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
54}
55
56static int system_refcount_dec(struct event_subsystem *system)
57{
58 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
59}
60
ae63b31e
SR
61/* Double loops, do not use break, only goto's work */
62#define do_for_each_event_file(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 list_for_each_entry(file, &tr->events, list)
65
66#define do_for_each_event_file_safe(tr, file) \
67 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
68 struct ftrace_event_file *___n; \
69 list_for_each_entry_safe(file, ___n, &tr->events, list)
70
71#define while_for_each_event_file() \
72 }
73
b3a8c6fd 74static struct list_head *
2e33af02
SR
75trace_get_fields(struct ftrace_event_call *event_call)
76{
77 if (!event_call->class->get_fields)
78 return &event_call->class->fields;
79 return event_call->class->get_fields(event_call);
80}
81
b3a8c6fd
J
82static struct ftrace_event_field *
83__find_event_field(struct list_head *head, char *name)
84{
85 struct ftrace_event_field *field;
86
87 list_for_each_entry(field, head, link) {
88 if (!strcmp(field->name, name))
89 return field;
90 }
91
92 return NULL;
93}
94
95struct ftrace_event_field *
96trace_find_event_field(struct ftrace_event_call *call, char *name)
97{
98 struct ftrace_event_field *field;
99 struct list_head *head;
100
101 field = __find_event_field(&ftrace_common_fields, name);
102 if (field)
103 return field;
104
105 head = trace_get_fields(call);
106 return __find_event_field(head, name);
107}
108
8728fe50
LZ
109static int __trace_define_field(struct list_head *head, const char *type,
110 const char *name, int offset, int size,
111 int is_signed, int filter_type)
cf027f64
TZ
112{
113 struct ftrace_event_field *field;
114
d1a29143 115 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
cf027f64 116 if (!field)
aaf6ac0f 117 return -ENOMEM;
fe9f57f2 118
92edca07
SR
119 field->name = name;
120 field->type = type;
fe9f57f2 121
43b51ead
LZ
122 if (filter_type == FILTER_OTHER)
123 field->filter_type = filter_assign_type(type);
124 else
125 field->filter_type = filter_type;
126
cf027f64
TZ
127 field->offset = offset;
128 field->size = size;
a118e4d1 129 field->is_signed = is_signed;
aa38e9fc 130
2e33af02 131 list_add(&field->link, head);
cf027f64
TZ
132
133 return 0;
cf027f64 134}
8728fe50
LZ
135
136int trace_define_field(struct ftrace_event_call *call, const char *type,
137 const char *name, int offset, int size, int is_signed,
138 int filter_type)
139{
140 struct list_head *head;
141
142 if (WARN_ON(!call->class))
143 return 0;
144
145 head = trace_get_fields(call);
146 return __trace_define_field(head, type, name, offset, size,
147 is_signed, filter_type);
148}
17c873ec 149EXPORT_SYMBOL_GPL(trace_define_field);
cf027f64 150
e647d6b3 151#define __common_field(type, item) \
8728fe50
LZ
152 ret = __trace_define_field(&ftrace_common_fields, #type, \
153 "common_" #item, \
154 offsetof(typeof(ent), item), \
155 sizeof(ent.item), \
156 is_signed_type(type), FILTER_OTHER); \
e647d6b3
LZ
157 if (ret) \
158 return ret;
159
8728fe50 160static int trace_define_common_fields(void)
e647d6b3
LZ
161{
162 int ret;
163 struct trace_entry ent;
164
165 __common_field(unsigned short, type);
166 __common_field(unsigned char, flags);
167 __common_field(unsigned char, preempt_count);
168 __common_field(int, pid);
e647d6b3
LZ
169
170 return ret;
171}
172
ad7067ce 173static void trace_destroy_fields(struct ftrace_event_call *call)
2df75e41
LZ
174{
175 struct ftrace_event_field *field, *next;
2e33af02 176 struct list_head *head;
2df75e41 177
2e33af02
SR
178 head = trace_get_fields(call);
179 list_for_each_entry_safe(field, next, head, link) {
2df75e41 180 list_del(&field->link);
d1a29143 181 kmem_cache_free(field_cachep, field);
2df75e41
LZ
182 }
183}
184
87d9b4e1
LZ
185int trace_event_raw_init(struct ftrace_event_call *call)
186{
187 int id;
188
80decc70 189 id = register_ftrace_event(&call->event);
87d9b4e1
LZ
190 if (!id)
191 return -ENODEV;
87d9b4e1
LZ
192
193 return 0;
194}
195EXPORT_SYMBOL_GPL(trace_event_raw_init);
196
ceec0b6f
JO
197int ftrace_event_reg(struct ftrace_event_call *call,
198 enum trace_reg type, void *data)
a1d0ce82 199{
ae63b31e
SR
200 struct ftrace_event_file *file = data;
201
a1d0ce82
SR
202 switch (type) {
203 case TRACE_REG_REGISTER:
204 return tracepoint_probe_register(call->name,
205 call->class->probe,
ae63b31e 206 file);
a1d0ce82
SR
207 case TRACE_REG_UNREGISTER:
208 tracepoint_probe_unregister(call->name,
209 call->class->probe,
ae63b31e 210 file);
a1d0ce82
SR
211 return 0;
212
213#ifdef CONFIG_PERF_EVENTS
214 case TRACE_REG_PERF_REGISTER:
215 return tracepoint_probe_register(call->name,
216 call->class->perf_probe,
217 call);
218 case TRACE_REG_PERF_UNREGISTER:
219 tracepoint_probe_unregister(call->name,
220 call->class->perf_probe,
221 call);
222 return 0;
ceec0b6f
JO
223 case TRACE_REG_PERF_OPEN:
224 case TRACE_REG_PERF_CLOSE:
489c75c3
JO
225 case TRACE_REG_PERF_ADD:
226 case TRACE_REG_PERF_DEL:
ceec0b6f 227 return 0;
a1d0ce82
SR
228#endif
229 }
230 return 0;
231}
232EXPORT_SYMBOL_GPL(ftrace_event_reg);
233
e870e9a1
LZ
234void trace_event_enable_cmd_record(bool enable)
235{
ae63b31e
SR
236 struct ftrace_event_file *file;
237 struct trace_array *tr;
e870e9a1
LZ
238
239 mutex_lock(&event_mutex);
ae63b31e
SR
240 do_for_each_event_file(tr, file) {
241
242 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
e870e9a1
LZ
243 continue;
244
245 if (enable) {
246 tracing_start_cmdline_record();
417944c4 247 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1
LZ
248 } else {
249 tracing_stop_cmdline_record();
417944c4 250 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 251 }
ae63b31e 252 } while_for_each_event_file();
e870e9a1
LZ
253 mutex_unlock(&event_mutex);
254}
255
417944c4
SRRH
256static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
257 int enable, int soft_disable)
fd994989 258{
ae63b31e 259 struct ftrace_event_call *call = file->event_call;
3b8e4273 260 int ret = 0;
417944c4 261 int disable;
3b8e4273 262
fd994989
SR
263 switch (enable) {
264 case 0:
417944c4 265 /*
1cf4c073
MH
266 * When soft_disable is set and enable is cleared, the sm_ref
267 * reference counter is decremented. If it reaches 0, we want
417944c4
SRRH
268 * to clear the SOFT_DISABLED flag but leave the event in the
269 * state that it was. That is, if the event was enabled and
270 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
271 * is set we do not want the event to be enabled before we
272 * clear the bit.
273 *
274 * When soft_disable is not set but the SOFT_MODE flag is,
275 * we do nothing. Do not disable the tracepoint, otherwise
276 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
277 */
278 if (soft_disable) {
1cf4c073
MH
279 if (atomic_dec_return(&file->sm_ref) > 0)
280 break;
417944c4
SRRH
281 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
282 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
283 } else
284 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
285
286 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
287 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
ae63b31e 288 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
e870e9a1 289 tracing_stop_cmdline_record();
417944c4 290 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 291 }
ae63b31e 292 call->class->reg(call, TRACE_REG_UNREGISTER, file);
fd994989 293 }
3baa5e4c 294 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
417944c4
SRRH
295 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
296 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
3baa5e4c
TZ
297 else
298 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
fd994989
SR
299 break;
300 case 1:
417944c4
SRRH
301 /*
302 * When soft_disable is set and enable is set, we want to
303 * register the tracepoint for the event, but leave the event
304 * as is. That means, if the event was already enabled, we do
305 * nothing (but set SOFT_MODE). If the event is disabled, we
306 * set SOFT_DISABLED before enabling the event tracepoint, so
307 * it still seems to be disabled.
308 */
309 if (!soft_disable)
310 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
1cf4c073
MH
311 else {
312 if (atomic_inc_return(&file->sm_ref) > 1)
313 break;
417944c4 314 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
1cf4c073 315 }
417944c4 316
ae63b31e 317 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
417944c4
SRRH
318
319 /* Keep the event disabled, when going to SOFT_MODE. */
320 if (soft_disable)
321 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
322
e870e9a1
LZ
323 if (trace_flags & TRACE_ITER_RECORD_CMD) {
324 tracing_start_cmdline_record();
417944c4 325 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 326 }
ae63b31e 327 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
3b8e4273
LZ
328 if (ret) {
329 tracing_stop_cmdline_record();
330 pr_info("event trace: Could not enable event "
331 "%s\n", call->name);
332 break;
333 }
417944c4 334 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
575380da
SRRH
335
336 /* WAS_ENABLED gets set but never cleared. */
337 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
fd994989 338 }
fd994989
SR
339 break;
340 }
3b8e4273
LZ
341
342 return ret;
fd994989
SR
343}
344
417944c4
SRRH
345static int ftrace_event_enable_disable(struct ftrace_event_file *file,
346 int enable)
347{
348 return __ftrace_event_enable_disable(file, enable, 0);
349}
350
ae63b31e 351static void ftrace_clear_events(struct trace_array *tr)
0e907c99 352{
ae63b31e 353 struct ftrace_event_file *file;
0e907c99
Z
354
355 mutex_lock(&event_mutex);
ae63b31e
SR
356 list_for_each_entry(file, &tr->events, list) {
357 ftrace_event_enable_disable(file, 0);
0e907c99
Z
358 }
359 mutex_unlock(&event_mutex);
360}
361
e9dbfae5
SR
362static void __put_system(struct event_subsystem *system)
363{
364 struct event_filter *filter = system->filter;
365
6e94a780
SR
366 WARN_ON_ONCE(system_refcount(system) == 0);
367 if (system_refcount_dec(system))
e9dbfae5
SR
368 return;
369
ae63b31e
SR
370 list_del(&system->list);
371
e9dbfae5
SR
372 if (filter) {
373 kfree(filter->filter_string);
374 kfree(filter);
375 }
6e94a780
SR
376 if (system->ref_count & SYSTEM_FL_FREE_NAME)
377 kfree(system->name);
e9dbfae5
SR
378 kfree(system);
379}
380
381static void __get_system(struct event_subsystem *system)
382{
6e94a780
SR
383 WARN_ON_ONCE(system_refcount(system) == 0);
384 system_refcount_inc(system);
e9dbfae5
SR
385}
386
ae63b31e
SR
387static void __get_system_dir(struct ftrace_subsystem_dir *dir)
388{
389 WARN_ON_ONCE(dir->ref_count == 0);
390 dir->ref_count++;
391 __get_system(dir->subsystem);
392}
393
394static void __put_system_dir(struct ftrace_subsystem_dir *dir)
395{
396 WARN_ON_ONCE(dir->ref_count == 0);
397 /* If the subsystem is about to be freed, the dir must be too */
6e94a780 398 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
ae63b31e
SR
399
400 __put_system(dir->subsystem);
401 if (!--dir->ref_count)
402 kfree(dir);
403}
404
405static void put_system(struct ftrace_subsystem_dir *dir)
e9dbfae5
SR
406{
407 mutex_lock(&event_mutex);
ae63b31e 408 __put_system_dir(dir);
e9dbfae5
SR
409 mutex_unlock(&event_mutex);
410}
411
1a11126b
ON
412static void *event_file_data(struct file *filp)
413{
414 return ACCESS_ONCE(file_inode(filp)->i_private);
415}
416
8e2e2fa4
SRRH
417/*
418 * Open and update trace_array ref count.
419 * Must have the current trace_array passed to it.
420 */
421static int tracing_open_generic_file(struct inode *inode, struct file *filp)
422{
423 struct ftrace_event_file *file = inode->i_private;
424 struct trace_array *tr = file->tr;
425 int ret;
426
427 if (trace_array_get(tr) < 0)
428 return -ENODEV;
429
430 ret = tracing_open_generic(inode, filp);
431 if (ret < 0)
432 trace_array_put(tr);
433 return ret;
434}
435
436static int tracing_release_generic_file(struct inode *inode, struct file *filp)
437{
438 struct ftrace_event_file *file = inode->i_private;
439 struct trace_array *tr = file->tr;
440
441 trace_array_put(tr);
442
443 return 0;
444}
445
8f31bfe5
LZ
446/*
447 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
448 */
2a6c24af
SRRH
449static int
450__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
451 const char *sub, const char *event, int set)
b77e38aa 452{
ae63b31e 453 struct ftrace_event_file *file;
a59fd602 454 struct ftrace_event_call *call;
29f93943 455 int ret = -EINVAL;
8f31bfe5 456
ae63b31e
SR
457 list_for_each_entry(file, &tr->events, list) {
458
459 call = file->event_call;
8f31bfe5 460
a1d0ce82 461 if (!call->name || !call->class || !call->class->reg)
8f31bfe5
LZ
462 continue;
463
9b63776f
SR
464 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
465 continue;
466
8f31bfe5
LZ
467 if (match &&
468 strcmp(match, call->name) != 0 &&
8f082018 469 strcmp(match, call->class->system) != 0)
8f31bfe5
LZ
470 continue;
471
8f082018 472 if (sub && strcmp(sub, call->class->system) != 0)
8f31bfe5
LZ
473 continue;
474
475 if (event && strcmp(event, call->name) != 0)
476 continue;
477
ae63b31e 478 ftrace_event_enable_disable(file, set);
8f31bfe5
LZ
479
480 ret = 0;
481 }
2a6c24af
SRRH
482
483 return ret;
484}
485
486static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
487 const char *sub, const char *event, int set)
488{
489 int ret;
490
491 mutex_lock(&event_mutex);
492 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
8f31bfe5
LZ
493 mutex_unlock(&event_mutex);
494
495 return ret;
496}
497
ae63b31e 498static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
8f31bfe5 499{
b628b3e6 500 char *event = NULL, *sub = NULL, *match;
b628b3e6
SR
501
502 /*
503 * The buf format can be <subsystem>:<event-name>
504 * *:<event-name> means any event by that name.
505 * :<event-name> is the same.
506 *
507 * <subsystem>:* means all events in that subsystem
508 * <subsystem>: means the same.
509 *
510 * <name> (no ':') means all events in a subsystem with
511 * the name <name> or any event that matches <name>
512 */
513
514 match = strsep(&buf, ":");
515 if (buf) {
516 sub = match;
517 event = buf;
518 match = NULL;
519
520 if (!strlen(sub) || strcmp(sub, "*") == 0)
521 sub = NULL;
522 if (!strlen(event) || strcmp(event, "*") == 0)
523 event = NULL;
524 }
b77e38aa 525
ae63b31e 526 return __ftrace_set_clr_event(tr, match, sub, event, set);
b77e38aa
SR
527}
528
4671c794
SR
529/**
530 * trace_set_clr_event - enable or disable an event
531 * @system: system name to match (NULL for any system)
532 * @event: event name to match (NULL for all events, within system)
533 * @set: 1 to enable, 0 to disable
534 *
535 * This is a way for other parts of the kernel to enable or disable
536 * event recording.
537 *
538 * Returns 0 on success, -EINVAL if the parameters do not match any
539 * registered events.
540 */
541int trace_set_clr_event(const char *system, const char *event, int set)
542{
ae63b31e
SR
543 struct trace_array *tr = top_trace_array();
544
545 return __ftrace_set_clr_event(tr, NULL, system, event, set);
4671c794 546}
56355b83 547EXPORT_SYMBOL_GPL(trace_set_clr_event);
4671c794 548
b77e38aa
SR
549/* 128 should be much more than enough */
550#define EVENT_BUF_SIZE 127
551
552static ssize_t
553ftrace_event_write(struct file *file, const char __user *ubuf,
554 size_t cnt, loff_t *ppos)
555{
48966364 556 struct trace_parser parser;
ae63b31e
SR
557 struct seq_file *m = file->private_data;
558 struct trace_array *tr = m->private;
4ba7978e 559 ssize_t read, ret;
b77e38aa 560
4ba7978e 561 if (!cnt)
b77e38aa
SR
562 return 0;
563
1852fcce
SR
564 ret = tracing_update_buffers();
565 if (ret < 0)
566 return ret;
567
48966364 568 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
b77e38aa
SR
569 return -ENOMEM;
570
48966364 571 read = trace_get_user(&parser, ubuf, cnt, ppos);
572
4ba7978e 573 if (read >= 0 && trace_parser_loaded((&parser))) {
48966364 574 int set = 1;
b77e38aa 575
48966364 576 if (*parser.buffer == '!')
b77e38aa 577 set = 0;
b77e38aa 578
48966364 579 parser.buffer[parser.idx] = 0;
580
ae63b31e 581 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
b77e38aa 582 if (ret)
48966364 583 goto out_put;
b77e38aa 584 }
b77e38aa
SR
585
586 ret = read;
587
48966364 588 out_put:
589 trace_parser_put(&parser);
b77e38aa
SR
590
591 return ret;
592}
593
594static void *
595t_next(struct seq_file *m, void *v, loff_t *pos)
596{
ae63b31e
SR
597 struct ftrace_event_file *file = v;
598 struct ftrace_event_call *call;
599 struct trace_array *tr = m->private;
b77e38aa
SR
600
601 (*pos)++;
602
ae63b31e
SR
603 list_for_each_entry_continue(file, &tr->events, list) {
604 call = file->event_call;
40e26815
SR
605 /*
606 * The ftrace subsystem is for showing formats only.
607 * They can not be enabled or disabled via the event files.
608 */
a1d0ce82 609 if (call->class && call->class->reg)
ae63b31e 610 return file;
40e26815 611 }
b77e38aa 612
30bd39cd 613 return NULL;
b77e38aa
SR
614}
615
616static void *t_start(struct seq_file *m, loff_t *pos)
617{
ae63b31e
SR
618 struct ftrace_event_file *file;
619 struct trace_array *tr = m->private;
e1c7e2a6
LZ
620 loff_t l;
621
20c8928a 622 mutex_lock(&event_mutex);
e1c7e2a6 623
ae63b31e 624 file = list_entry(&tr->events, struct ftrace_event_file, list);
e1c7e2a6 625 for (l = 0; l <= *pos; ) {
ae63b31e
SR
626 file = t_next(m, file, &l);
627 if (!file)
e1c7e2a6
LZ
628 break;
629 }
ae63b31e 630 return file;
b77e38aa
SR
631}
632
633static void *
634s_next(struct seq_file *m, void *v, loff_t *pos)
635{
ae63b31e
SR
636 struct ftrace_event_file *file = v;
637 struct trace_array *tr = m->private;
b77e38aa
SR
638
639 (*pos)++;
640
ae63b31e
SR
641 list_for_each_entry_continue(file, &tr->events, list) {
642 if (file->flags & FTRACE_EVENT_FL_ENABLED)
643 return file;
b77e38aa
SR
644 }
645
30bd39cd 646 return NULL;
b77e38aa
SR
647}
648
649static void *s_start(struct seq_file *m, loff_t *pos)
650{
ae63b31e
SR
651 struct ftrace_event_file *file;
652 struct trace_array *tr = m->private;
e1c7e2a6
LZ
653 loff_t l;
654
20c8928a 655 mutex_lock(&event_mutex);
e1c7e2a6 656
ae63b31e 657 file = list_entry(&tr->events, struct ftrace_event_file, list);
e1c7e2a6 658 for (l = 0; l <= *pos; ) {
ae63b31e
SR
659 file = s_next(m, file, &l);
660 if (!file)
e1c7e2a6
LZ
661 break;
662 }
ae63b31e 663 return file;
b77e38aa
SR
664}
665
666static int t_show(struct seq_file *m, void *v)
667{
ae63b31e
SR
668 struct ftrace_event_file *file = v;
669 struct ftrace_event_call *call = file->event_call;
b77e38aa 670
8f082018
SR
671 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
672 seq_printf(m, "%s:", call->class->system);
b77e38aa
SR
673 seq_printf(m, "%s\n", call->name);
674
675 return 0;
676}
677
678static void t_stop(struct seq_file *m, void *p)
679{
20c8928a 680 mutex_unlock(&event_mutex);
b77e38aa
SR
681}
682
1473e441
SR
683static ssize_t
684event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
685 loff_t *ppos)
686{
bc6f6b08
ON
687 struct ftrace_event_file *file;
688 unsigned long flags;
a4390596
TZ
689 char buf[4] = "0";
690
bc6f6b08
ON
691 mutex_lock(&event_mutex);
692 file = event_file_data(filp);
693 if (likely(file))
694 flags = file->flags;
695 mutex_unlock(&event_mutex);
696
697 if (!file)
698 return -ENODEV;
699
700 if (flags & FTRACE_EVENT_FL_ENABLED &&
701 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
a4390596
TZ
702 strcpy(buf, "1");
703
bc6f6b08
ON
704 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
705 flags & FTRACE_EVENT_FL_SOFT_MODE)
a4390596
TZ
706 strcat(buf, "*");
707
708 strcat(buf, "\n");
1473e441 709
417944c4 710 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1473e441
SR
711}
712
713static ssize_t
714event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
715 loff_t *ppos)
716{
bc6f6b08 717 struct ftrace_event_file *file;
1473e441
SR
718 unsigned long val;
719 int ret;
720
22fe9b54
PH
721 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
722 if (ret)
1473e441
SR
723 return ret;
724
1852fcce
SR
725 ret = tracing_update_buffers();
726 if (ret < 0)
727 return ret;
728
1473e441
SR
729 switch (val) {
730 case 0:
1473e441 731 case 1:
bc6f6b08 732 ret = -ENODEV;
11a241a3 733 mutex_lock(&event_mutex);
bc6f6b08
ON
734 file = event_file_data(filp);
735 if (likely(file))
736 ret = ftrace_event_enable_disable(file, val);
11a241a3 737 mutex_unlock(&event_mutex);
1473e441
SR
738 break;
739
740 default:
741 return -EINVAL;
742 }
743
744 *ppos += cnt;
745
3b8e4273 746 return ret ? ret : cnt;
1473e441
SR
747}
748
8ae79a13
SR
749static ssize_t
750system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
751 loff_t *ppos)
752{
c142b15d 753 const char set_to_char[4] = { '?', '0', '1', 'X' };
ae63b31e
SR
754 struct ftrace_subsystem_dir *dir = filp->private_data;
755 struct event_subsystem *system = dir->subsystem;
8ae79a13 756 struct ftrace_event_call *call;
ae63b31e
SR
757 struct ftrace_event_file *file;
758 struct trace_array *tr = dir->tr;
8ae79a13 759 char buf[2];
c142b15d 760 int set = 0;
8ae79a13
SR
761 int ret;
762
8ae79a13 763 mutex_lock(&event_mutex);
ae63b31e
SR
764 list_for_each_entry(file, &tr->events, list) {
765 call = file->event_call;
a1d0ce82 766 if (!call->name || !call->class || !call->class->reg)
8ae79a13
SR
767 continue;
768
40ee4dff 769 if (system && strcmp(call->class->system, system->name) != 0)
8ae79a13
SR
770 continue;
771
772 /*
773 * We need to find out if all the events are set
774 * or if all events or cleared, or if we have
775 * a mixture.
776 */
ae63b31e 777 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
c142b15d 778
8ae79a13
SR
779 /*
780 * If we have a mixture, no need to look further.
781 */
c142b15d 782 if (set == 3)
8ae79a13
SR
783 break;
784 }
785 mutex_unlock(&event_mutex);
786
c142b15d 787 buf[0] = set_to_char[set];
8ae79a13 788 buf[1] = '\n';
8ae79a13
SR
789
790 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
791
792 return ret;
793}
794
795static ssize_t
796system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
797 loff_t *ppos)
798{
ae63b31e
SR
799 struct ftrace_subsystem_dir *dir = filp->private_data;
800 struct event_subsystem *system = dir->subsystem;
40ee4dff 801 const char *name = NULL;
8ae79a13 802 unsigned long val;
8ae79a13
SR
803 ssize_t ret;
804
22fe9b54
PH
805 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
806 if (ret)
8ae79a13
SR
807 return ret;
808
809 ret = tracing_update_buffers();
810 if (ret < 0)
811 return ret;
812
8f31bfe5 813 if (val != 0 && val != 1)
8ae79a13 814 return -EINVAL;
8ae79a13 815
40ee4dff
SR
816 /*
817 * Opening of "enable" adds a ref count to system,
818 * so the name is safe to use.
819 */
820 if (system)
821 name = system->name;
822
ae63b31e 823 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
8ae79a13 824 if (ret)
8f31bfe5 825 goto out;
8ae79a13
SR
826
827 ret = cnt;
828
8f31bfe5 829out:
8ae79a13
SR
830 *ppos += cnt;
831
832 return ret;
833}
834
2a37a3df
SR
835enum {
836 FORMAT_HEADER = 1,
86397dc3
LZ
837 FORMAT_FIELD_SEPERATOR = 2,
838 FORMAT_PRINTFMT = 3,
2a37a3df
SR
839};
840
841static void *f_next(struct seq_file *m, void *v, loff_t *pos)
981d081e 842{
c5a44a12 843 struct ftrace_event_call *call = event_file_data(m->private);
86397dc3
LZ
844 struct list_head *common_head = &ftrace_common_fields;
845 struct list_head *head = trace_get_fields(call);
7710b639 846 struct list_head *node = v;
981d081e 847
2a37a3df 848 (*pos)++;
5a65e956 849
2a37a3df
SR
850 switch ((unsigned long)v) {
851 case FORMAT_HEADER:
7710b639
ON
852 node = common_head;
853 break;
5a65e956 854
86397dc3 855 case FORMAT_FIELD_SEPERATOR:
7710b639
ON
856 node = head;
857 break;
5a65e956 858
2a37a3df
SR
859 case FORMAT_PRINTFMT:
860 /* all done */
861 return NULL;
5a65e956
LJ
862 }
863
7710b639
ON
864 node = node->prev;
865 if (node == common_head)
86397dc3 866 return (void *)FORMAT_FIELD_SEPERATOR;
7710b639 867 else if (node == head)
2a37a3df 868 return (void *)FORMAT_PRINTFMT;
7710b639
ON
869 else
870 return node;
2a37a3df
SR
871}
872
873static int f_show(struct seq_file *m, void *v)
874{
c5a44a12 875 struct ftrace_event_call *call = event_file_data(m->private);
2a37a3df
SR
876 struct ftrace_event_field *field;
877 const char *array_descriptor;
878
879 switch ((unsigned long)v) {
880 case FORMAT_HEADER:
881 seq_printf(m, "name: %s\n", call->name);
882 seq_printf(m, "ID: %d\n", call->event.type);
883 seq_printf(m, "format:\n");
8728fe50 884 return 0;
5a65e956 885
86397dc3
LZ
886 case FORMAT_FIELD_SEPERATOR:
887 seq_putc(m, '\n');
888 return 0;
889
2a37a3df
SR
890 case FORMAT_PRINTFMT:
891 seq_printf(m, "\nprint fmt: %s\n",
892 call->print_fmt);
893 return 0;
981d081e 894 }
8728fe50 895
7710b639 896 field = list_entry(v, struct ftrace_event_field, link);
2a37a3df
SR
897 /*
898 * Smartly shows the array type(except dynamic array).
899 * Normal:
900 * field:TYPE VAR
901 * If TYPE := TYPE[LEN], it is shown:
902 * field:TYPE VAR[LEN]
903 */
904 array_descriptor = strchr(field->type, '[');
8728fe50 905
2a37a3df
SR
906 if (!strncmp(field->type, "__data_loc", 10))
907 array_descriptor = NULL;
8728fe50 908
2a37a3df
SR
909 if (!array_descriptor)
910 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
911 field->type, field->name, field->offset,
912 field->size, !!field->is_signed);
913 else
914 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
915 (int)(array_descriptor - field->type),
916 field->type, field->name,
917 array_descriptor, field->offset,
918 field->size, !!field->is_signed);
8728fe50 919
2a37a3df
SR
920 return 0;
921}
5a65e956 922
7710b639
ON
923static void *f_start(struct seq_file *m, loff_t *pos)
924{
925 void *p = (void *)FORMAT_HEADER;
926 loff_t l = 0;
927
c5a44a12
ON
928 /* ->stop() is called even if ->start() fails */
929 mutex_lock(&event_mutex);
930 if (!event_file_data(m->private))
931 return ERR_PTR(-ENODEV);
932
7710b639
ON
933 while (l < *pos && p)
934 p = f_next(m, p, &l);
935
936 return p;
937}
938
2a37a3df
SR
939static void f_stop(struct seq_file *m, void *p)
940{
c5a44a12 941 mutex_unlock(&event_mutex);
2a37a3df 942}
981d081e 943
2a37a3df
SR
944static const struct seq_operations trace_format_seq_ops = {
945 .start = f_start,
946 .next = f_next,
947 .stop = f_stop,
948 .show = f_show,
949};
950
951static int trace_format_open(struct inode *inode, struct file *file)
952{
2a37a3df
SR
953 struct seq_file *m;
954 int ret;
955
956 ret = seq_open(file, &trace_format_seq_ops);
957 if (ret < 0)
958 return ret;
959
960 m = file->private_data;
c5a44a12 961 m->private = file;
2a37a3df
SR
962
963 return 0;
981d081e
SR
964}
965
23725aee
PZ
966static ssize_t
967event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
968{
1a11126b 969 int id = (long)event_file_data(filp);
cd458ba9
ON
970 char buf[32];
971 int len;
23725aee
PZ
972
973 if (*ppos)
974 return 0;
975
1a11126b
ON
976 if (unlikely(!id))
977 return -ENODEV;
978
979 len = sprintf(buf, "%d\n", id);
980
cd458ba9 981 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
23725aee
PZ
982}
983
7ce7e424
TZ
984static ssize_t
985event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
986 loff_t *ppos)
987{
e2912b09 988 struct ftrace_event_call *call;
7ce7e424 989 struct trace_seq *s;
e2912b09 990 int r = -ENODEV;
7ce7e424
TZ
991
992 if (*ppos)
993 return 0;
994
995 s = kmalloc(sizeof(*s), GFP_KERNEL);
e2912b09 996
7ce7e424
TZ
997 if (!s)
998 return -ENOMEM;
999
1000 trace_seq_init(s);
1001
e2912b09
ON
1002 mutex_lock(&event_mutex);
1003 call = event_file_data(filp);
1004 if (call)
1005 print_event_filter(call, s);
1006 mutex_unlock(&event_mutex);
1007
1008 if (call)
1009 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
7ce7e424
TZ
1010
1011 kfree(s);
1012
1013 return r;
1014}
1015
1016static ssize_t
1017event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1018 loff_t *ppos)
1019{
e2912b09 1020 struct ftrace_event_call *call;
8b372562 1021 char *buf;
e2912b09 1022 int err = -ENODEV;
7ce7e424 1023
8b372562 1024 if (cnt >= PAGE_SIZE)
7ce7e424
TZ
1025 return -EINVAL;
1026
8b372562
TZ
1027 buf = (char *)__get_free_page(GFP_TEMPORARY);
1028 if (!buf)
7ce7e424
TZ
1029 return -ENOMEM;
1030
8b372562
TZ
1031 if (copy_from_user(buf, ubuf, cnt)) {
1032 free_page((unsigned long) buf);
1033 return -EFAULT;
7ce7e424 1034 }
8b372562 1035 buf[cnt] = '\0';
7ce7e424 1036
e2912b09
ON
1037 mutex_lock(&event_mutex);
1038 call = event_file_data(filp);
1039 if (call)
1040 err = apply_event_filter(call, buf);
1041 mutex_unlock(&event_mutex);
1042
8b372562
TZ
1043 free_page((unsigned long) buf);
1044 if (err < 0)
44e9c8b7 1045 return err;
0a19e53c 1046
7ce7e424
TZ
1047 *ppos += cnt;
1048
1049 return cnt;
1050}
1051
e9dbfae5
SR
1052static LIST_HEAD(event_subsystems);
1053
1054static int subsystem_open(struct inode *inode, struct file *filp)
1055{
1056 struct event_subsystem *system = NULL;
ae63b31e
SR
1057 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1058 struct trace_array *tr;
e9dbfae5
SR
1059 int ret;
1060
1061 /* Make sure the system still exists */
a8227415 1062 mutex_lock(&trace_types_lock);
e9dbfae5 1063 mutex_lock(&event_mutex);
ae63b31e
SR
1064 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1065 list_for_each_entry(dir, &tr->systems, list) {
1066 if (dir == inode->i_private) {
1067 /* Don't open systems with no events */
1068 if (dir->nr_events) {
1069 __get_system_dir(dir);
1070 system = dir->subsystem;
1071 }
1072 goto exit_loop;
e9dbfae5 1073 }
e9dbfae5
SR
1074 }
1075 }
ae63b31e 1076 exit_loop:
e9dbfae5 1077 mutex_unlock(&event_mutex);
a8227415 1078 mutex_unlock(&trace_types_lock);
e9dbfae5 1079
ae63b31e 1080 if (!system)
e9dbfae5
SR
1081 return -ENODEV;
1082
ae63b31e
SR
1083 /* Some versions of gcc think dir can be uninitialized here */
1084 WARN_ON(!dir);
1085
8e2e2fa4
SRRH
1086 /* Still need to increment the ref count of the system */
1087 if (trace_array_get(tr) < 0) {
1088 put_system(dir);
1089 return -ENODEV;
1090 }
1091
e9dbfae5 1092 ret = tracing_open_generic(inode, filp);
8e2e2fa4
SRRH
1093 if (ret < 0) {
1094 trace_array_put(tr);
ae63b31e 1095 put_system(dir);
8e2e2fa4 1096 }
ae63b31e
SR
1097
1098 return ret;
1099}
1100
1101static int system_tr_open(struct inode *inode, struct file *filp)
1102{
1103 struct ftrace_subsystem_dir *dir;
1104 struct trace_array *tr = inode->i_private;
1105 int ret;
1106
8e2e2fa4
SRRH
1107 if (trace_array_get(tr) < 0)
1108 return -ENODEV;
1109
ae63b31e
SR
1110 /* Make a temporary dir that has no system but points to tr */
1111 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
8e2e2fa4
SRRH
1112 if (!dir) {
1113 trace_array_put(tr);
ae63b31e 1114 return -ENOMEM;
8e2e2fa4 1115 }
ae63b31e
SR
1116
1117 dir->tr = tr;
1118
1119 ret = tracing_open_generic(inode, filp);
8e2e2fa4
SRRH
1120 if (ret < 0) {
1121 trace_array_put(tr);
ae63b31e 1122 kfree(dir);
8e2e2fa4 1123 }
ae63b31e
SR
1124
1125 filp->private_data = dir;
e9dbfae5
SR
1126
1127 return ret;
1128}
1129
1130static int subsystem_release(struct inode *inode, struct file *file)
1131{
ae63b31e 1132 struct ftrace_subsystem_dir *dir = file->private_data;
e9dbfae5 1133
8e2e2fa4
SRRH
1134 trace_array_put(dir->tr);
1135
ae63b31e
SR
1136 /*
1137 * If dir->subsystem is NULL, then this is a temporary
1138 * descriptor that was made for a trace_array to enable
1139 * all subsystems.
1140 */
1141 if (dir->subsystem)
1142 put_system(dir);
1143 else
1144 kfree(dir);
e9dbfae5
SR
1145
1146 return 0;
1147}
1148
cfb180f3
TZ
1149static ssize_t
1150subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1151 loff_t *ppos)
1152{
ae63b31e
SR
1153 struct ftrace_subsystem_dir *dir = filp->private_data;
1154 struct event_subsystem *system = dir->subsystem;
cfb180f3
TZ
1155 struct trace_seq *s;
1156 int r;
1157
1158 if (*ppos)
1159 return 0;
1160
1161 s = kmalloc(sizeof(*s), GFP_KERNEL);
1162 if (!s)
1163 return -ENOMEM;
1164
1165 trace_seq_init(s);
1166
8b372562 1167 print_subsystem_event_filter(system, s);
4bda2d51 1168 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
cfb180f3
TZ
1169
1170 kfree(s);
1171
1172 return r;
1173}
1174
1175static ssize_t
1176subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1177 loff_t *ppos)
1178{
ae63b31e 1179 struct ftrace_subsystem_dir *dir = filp->private_data;
8b372562 1180 char *buf;
cfb180f3
TZ
1181 int err;
1182
8b372562 1183 if (cnt >= PAGE_SIZE)
cfb180f3
TZ
1184 return -EINVAL;
1185
8b372562
TZ
1186 buf = (char *)__get_free_page(GFP_TEMPORARY);
1187 if (!buf)
cfb180f3
TZ
1188 return -ENOMEM;
1189
8b372562
TZ
1190 if (copy_from_user(buf, ubuf, cnt)) {
1191 free_page((unsigned long) buf);
1192 return -EFAULT;
cfb180f3 1193 }
8b372562 1194 buf[cnt] = '\0';
cfb180f3 1195
ae63b31e 1196 err = apply_subsystem_event_filter(dir, buf);
8b372562
TZ
1197 free_page((unsigned long) buf);
1198 if (err < 0)
44e9c8b7 1199 return err;
cfb180f3
TZ
1200
1201 *ppos += cnt;
1202
1203 return cnt;
1204}
1205
d1b182a8
SR
1206static ssize_t
1207show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1208{
1209 int (*func)(struct trace_seq *s) = filp->private_data;
1210 struct trace_seq *s;
1211 int r;
1212
1213 if (*ppos)
1214 return 0;
1215
1216 s = kmalloc(sizeof(*s), GFP_KERNEL);
1217 if (!s)
1218 return -ENOMEM;
1219
1220 trace_seq_init(s);
1221
1222 func(s);
1223 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1224
1225 kfree(s);
1226
1227 return r;
1228}
1229
15075cac
SR
1230static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1231static int ftrace_event_set_open(struct inode *inode, struct file *file);
f77d09a3 1232static int ftrace_event_release(struct inode *inode, struct file *file);
15075cac 1233
b77e38aa
SR
1234static const struct seq_operations show_event_seq_ops = {
1235 .start = t_start,
1236 .next = t_next,
1237 .show = t_show,
1238 .stop = t_stop,
1239};
1240
1241static const struct seq_operations show_set_event_seq_ops = {
1242 .start = s_start,
1243 .next = s_next,
1244 .show = t_show,
1245 .stop = t_stop,
1246};
1247
2314c4ae 1248static const struct file_operations ftrace_avail_fops = {
15075cac 1249 .open = ftrace_event_avail_open,
2314c4ae
SR
1250 .read = seq_read,
1251 .llseek = seq_lseek,
1252 .release = seq_release,
1253};
1254
b77e38aa 1255static const struct file_operations ftrace_set_event_fops = {
15075cac 1256 .open = ftrace_event_set_open,
b77e38aa
SR
1257 .read = seq_read,
1258 .write = ftrace_event_write,
1259 .llseek = seq_lseek,
f77d09a3 1260 .release = ftrace_event_release,
b77e38aa
SR
1261};
1262
1473e441 1263static const struct file_operations ftrace_enable_fops = {
8e2e2fa4 1264 .open = tracing_open_generic_file,
1473e441
SR
1265 .read = event_enable_read,
1266 .write = event_enable_write,
8e2e2fa4 1267 .release = tracing_release_generic_file,
6038f373 1268 .llseek = default_llseek,
1473e441
SR
1269};
1270
981d081e 1271static const struct file_operations ftrace_event_format_fops = {
2a37a3df
SR
1272 .open = trace_format_open,
1273 .read = seq_read,
1274 .llseek = seq_lseek,
1275 .release = seq_release,
981d081e
SR
1276};
1277
23725aee 1278static const struct file_operations ftrace_event_id_fops = {
23725aee 1279 .read = event_id_read,
6038f373 1280 .llseek = default_llseek,
23725aee
PZ
1281};
1282
7ce7e424
TZ
1283static const struct file_operations ftrace_event_filter_fops = {
1284 .open = tracing_open_generic,
1285 .read = event_filter_read,
1286 .write = event_filter_write,
6038f373 1287 .llseek = default_llseek,
7ce7e424
TZ
1288};
1289
cfb180f3 1290static const struct file_operations ftrace_subsystem_filter_fops = {
e9dbfae5 1291 .open = subsystem_open,
cfb180f3
TZ
1292 .read = subsystem_filter_read,
1293 .write = subsystem_filter_write,
6038f373 1294 .llseek = default_llseek,
e9dbfae5 1295 .release = subsystem_release,
cfb180f3
TZ
1296};
1297
8ae79a13 1298static const struct file_operations ftrace_system_enable_fops = {
40ee4dff 1299 .open = subsystem_open,
8ae79a13
SR
1300 .read = system_enable_read,
1301 .write = system_enable_write,
6038f373 1302 .llseek = default_llseek,
40ee4dff 1303 .release = subsystem_release,
8ae79a13
SR
1304};
1305
ae63b31e
SR
1306static const struct file_operations ftrace_tr_enable_fops = {
1307 .open = system_tr_open,
1308 .read = system_enable_read,
1309 .write = system_enable_write,
1310 .llseek = default_llseek,
1311 .release = subsystem_release,
1312};
1313
d1b182a8
SR
1314static const struct file_operations ftrace_show_header_fops = {
1315 .open = tracing_open_generic,
1316 .read = show_header,
6038f373 1317 .llseek = default_llseek,
d1b182a8
SR
1318};
1319
ae63b31e
SR
1320static int
1321ftrace_event_open(struct inode *inode, struct file *file,
1322 const struct seq_operations *seq_ops)
1473e441 1323{
ae63b31e
SR
1324 struct seq_file *m;
1325 int ret;
1473e441 1326
ae63b31e
SR
1327 ret = seq_open(file, seq_ops);
1328 if (ret < 0)
1329 return ret;
1330 m = file->private_data;
1331 /* copy tr over to seq ops */
1332 m->private = inode->i_private;
1473e441 1333
ae63b31e 1334 return ret;
1473e441
SR
1335}
1336
f77d09a3
AL
1337static int ftrace_event_release(struct inode *inode, struct file *file)
1338{
1339 struct trace_array *tr = inode->i_private;
1340
1341 trace_array_put(tr);
1342
1343 return seq_release(inode, file);
1344}
1345
15075cac
SR
1346static int
1347ftrace_event_avail_open(struct inode *inode, struct file *file)
1348{
1349 const struct seq_operations *seq_ops = &show_event_seq_ops;
1350
ae63b31e 1351 return ftrace_event_open(inode, file, seq_ops);
15075cac
SR
1352}
1353
1354static int
1355ftrace_event_set_open(struct inode *inode, struct file *file)
1356{
1357 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
ae63b31e 1358 struct trace_array *tr = inode->i_private;
f77d09a3
AL
1359 int ret;
1360
1361 if (trace_array_get(tr) < 0)
1362 return -ENODEV;
15075cac
SR
1363
1364 if ((file->f_mode & FMODE_WRITE) &&
1365 (file->f_flags & O_TRUNC))
ae63b31e 1366 ftrace_clear_events(tr);
15075cac 1367
f77d09a3
AL
1368 ret = ftrace_event_open(inode, file, seq_ops);
1369 if (ret < 0)
1370 trace_array_put(tr);
1371 return ret;
ae63b31e
SR
1372}
1373
1374static struct event_subsystem *
1375create_new_subsystem(const char *name)
1376{
1377 struct event_subsystem *system;
1378
1379 /* need to create new entry */
1380 system = kmalloc(sizeof(*system), GFP_KERNEL);
1381 if (!system)
1382 return NULL;
1383
1384 system->ref_count = 1;
6e94a780
SR
1385
1386 /* Only allocate if dynamic (kprobes and modules) */
1387 if (!core_kernel_data((unsigned long)name)) {
1388 system->ref_count |= SYSTEM_FL_FREE_NAME;
1389 system->name = kstrdup(name, GFP_KERNEL);
1390 if (!system->name)
1391 goto out_free;
1392 } else
1393 system->name = name;
ae63b31e
SR
1394
1395 system->filter = NULL;
1396
1397 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1398 if (!system->filter)
1399 goto out_free;
1400
1401 list_add(&system->list, &event_subsystems);
1402
1403 return system;
1404
1405 out_free:
6e94a780
SR
1406 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1407 kfree(system->name);
ae63b31e
SR
1408 kfree(system);
1409 return NULL;
15075cac
SR
1410}
1411
6ecc2d1c 1412static struct dentry *
ae63b31e
SR
1413event_subsystem_dir(struct trace_array *tr, const char *name,
1414 struct ftrace_event_file *file, struct dentry *parent)
6ecc2d1c 1415{
ae63b31e 1416 struct ftrace_subsystem_dir *dir;
6ecc2d1c 1417 struct event_subsystem *system;
e1112b4d 1418 struct dentry *entry;
6ecc2d1c
SR
1419
1420 /* First see if we did not already create this dir */
ae63b31e
SR
1421 list_for_each_entry(dir, &tr->systems, list) {
1422 system = dir->subsystem;
dc82ec98 1423 if (strcmp(system->name, name) == 0) {
ae63b31e
SR
1424 dir->nr_events++;
1425 file->system = dir;
1426 return dir->entry;
dc82ec98 1427 }
6ecc2d1c
SR
1428 }
1429
ae63b31e
SR
1430 /* Now see if the system itself exists. */
1431 list_for_each_entry(system, &event_subsystems, list) {
1432 if (strcmp(system->name, name) == 0)
1433 break;
6ecc2d1c 1434 }
ae63b31e
SR
1435 /* Reset system variable when not found */
1436 if (&system->list == &event_subsystems)
1437 system = NULL;
6ecc2d1c 1438
ae63b31e
SR
1439 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1440 if (!dir)
1441 goto out_fail;
6ecc2d1c 1442
ae63b31e
SR
1443 if (!system) {
1444 system = create_new_subsystem(name);
1445 if (!system)
1446 goto out_free;
1447 } else
1448 __get_system(system);
1449
1450 dir->entry = debugfs_create_dir(name, parent);
1451 if (!dir->entry) {
1452 pr_warning("Failed to create system directory %s\n", name);
1453 __put_system(system);
1454 goto out_free;
6d723736
SR
1455 }
1456
ae63b31e
SR
1457 dir->tr = tr;
1458 dir->ref_count = 1;
1459 dir->nr_events = 1;
1460 dir->subsystem = system;
1461 file->system = dir;
8b372562 1462
ae63b31e 1463 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
e1112b4d 1464 &ftrace_subsystem_filter_fops);
8b372562
TZ
1465 if (!entry) {
1466 kfree(system->filter);
1467 system->filter = NULL;
ae63b31e 1468 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
8b372562 1469 }
e1112b4d 1470
ae63b31e 1471 trace_create_file("enable", 0644, dir->entry, dir,
f3f3f009 1472 &ftrace_system_enable_fops);
8ae79a13 1473
ae63b31e
SR
1474 list_add(&dir->list, &tr->systems);
1475
1476 return dir->entry;
1477
1478 out_free:
1479 kfree(dir);
1480 out_fail:
1481 /* Only print this message if failed on memory allocation */
1482 if (!dir || !system)
1483 pr_warning("No memory to create event subsystem %s\n",
1484 name);
1485 return NULL;
6ecc2d1c
SR
1486}
1487
1473e441 1488static int
ae63b31e
SR
1489event_create_dir(struct dentry *parent,
1490 struct ftrace_event_file *file,
701970b3
SR
1491 const struct file_operations *id,
1492 const struct file_operations *enable,
1493 const struct file_operations *filter,
1494 const struct file_operations *format)
1473e441 1495{
ae63b31e
SR
1496 struct ftrace_event_call *call = file->event_call;
1497 struct trace_array *tr = file->tr;
2e33af02 1498 struct list_head *head;
ae63b31e 1499 struct dentry *d_events;
fd994989 1500 int ret;
1473e441 1501
6ecc2d1c
SR
1502 /*
1503 * If the trace point header did not define TRACE_SYSTEM
1504 * then the system would be called "TRACE_SYSTEM".
1505 */
ae63b31e
SR
1506 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1507 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1508 if (!d_events)
1509 return -ENOMEM;
1510 } else
1511 d_events = parent;
1512
1513 file->dir = debugfs_create_dir(call->name, d_events);
1514 if (!file->dir) {
1515 pr_warning("Could not create debugfs '%s' directory\n",
1516 call->name);
1473e441
SR
1517 return -1;
1518 }
1519
9b63776f 1520 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
ae63b31e 1521 trace_create_file("enable", 0644, file->dir, file,
f3f3f009 1522 enable);
1473e441 1523
2239291a 1524#ifdef CONFIG_PERF_EVENTS
a1d0ce82 1525 if (call->event.type && call->class->reg)
1a11126b
ON
1526 trace_create_file("id", 0444, file->dir,
1527 (void *)(long)call->event.type, id);
2239291a 1528#endif
23725aee 1529
c9d932cf
LZ
1530 /*
1531 * Other events may have the same class. Only update
1532 * the fields if they are not already defined.
1533 */
1534 head = trace_get_fields(call);
1535 if (list_empty(head)) {
1536 ret = call->class->define_fields(call);
1537 if (ret < 0) {
1538 pr_warning("Could not initialize trace point"
1539 " events/%s\n", call->name);
ae63b31e 1540 return -1;
cf027f64
TZ
1541 }
1542 }
ae63b31e 1543 trace_create_file("filter", 0644, file->dir, call,
c9d932cf 1544 filter);
cf027f64 1545
ae63b31e 1546 trace_create_file("format", 0444, file->dir, call,
f3f3f009 1547 format);
6d723736
SR
1548
1549 return 0;
1550}
1551
ae63b31e
SR
1552static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1553{
1554 if (!dir)
1555 return;
1556
1557 if (!--dir->nr_events) {
1558 debugfs_remove_recursive(dir->entry);
1559 list_del(&dir->list);
1560 __put_system_dir(dir);
1561 }
1562}
1563
1564static void remove_event_from_tracers(struct ftrace_event_call *call)
1565{
1566 struct ftrace_event_file *file;
1567 struct trace_array *tr;
1568
1569 do_for_each_event_file_safe(tr, file) {
1570
1571 if (file->event_call != call)
1572 continue;
1573
1574 list_del(&file->list);
1575 debugfs_remove_recursive(file->dir);
1576 remove_subsystem(file->system);
d1a29143 1577 kmem_cache_free(file_cachep, file);
ae63b31e
SR
1578
1579 /*
1580 * The do_for_each_event_file_safe() is
1581 * a double loop. After finding the call for this
1582 * trace_array, we use break to jump to the next
1583 * trace_array.
1584 */
1585 break;
1586 } while_for_each_event_file();
1587}
1588
8781915a
EG
1589static void event_remove(struct ftrace_event_call *call)
1590{
ae63b31e
SR
1591 struct trace_array *tr;
1592 struct ftrace_event_file *file;
1593
1594 do_for_each_event_file(tr, file) {
1595 if (file->event_call != call)
1596 continue;
1597 ftrace_event_enable_disable(file, 0);
1598 /*
1599 * The do_for_each_event_file() is
1600 * a double loop. After finding the call for this
1601 * trace_array, we use break to jump to the next
1602 * trace_array.
1603 */
1604 break;
1605 } while_for_each_event_file();
1606
8781915a
EG
1607 if (call->event.funcs)
1608 __unregister_ftrace_event(&call->event);
ae63b31e 1609 remove_event_from_tracers(call);
8781915a
EG
1610 list_del(&call->list);
1611}
1612
1613static int event_init(struct ftrace_event_call *call)
1614{
1615 int ret = 0;
1616
1617 if (WARN_ON(!call->name))
1618 return -EINVAL;
1619
1620 if (call->class->raw_init) {
1621 ret = call->class->raw_init(call);
1622 if (ret < 0 && ret != -ENOSYS)
1623 pr_warn("Could not initialize trace events/%s\n",
1624 call->name);
1625 }
1626
1627 return ret;
1628}
1629
67ead0a6 1630static int
ae63b31e 1631__register_event(struct ftrace_event_call *call, struct module *mod)
bd1a5c84 1632{
bd1a5c84 1633 int ret;
6d723736 1634
8781915a
EG
1635 ret = event_init(call);
1636 if (ret < 0)
1637 return ret;
701970b3 1638
ae63b31e 1639 list_add(&call->list, &ftrace_events);
67ead0a6 1640 call->mod = mod;
88f70d75 1641
ae63b31e 1642 return 0;
bd1a5c84
MH
1643}
1644
da511bf3
SRRH
1645static struct ftrace_event_file *
1646trace_create_new_event(struct ftrace_event_call *call,
1647 struct trace_array *tr)
1648{
1649 struct ftrace_event_file *file;
1650
1651 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1652 if (!file)
1653 return NULL;
1654
1655 file->event_call = call;
1656 file->tr = tr;
1657 atomic_set(&file->sm_ref, 0);
1658 list_add(&file->list, &tr->events);
1659
1660 return file;
1661}
1662
ae63b31e
SR
1663/* Add an event to a trace directory */
1664static int
1665__trace_add_new_event(struct ftrace_event_call *call,
1666 struct trace_array *tr,
1667 const struct file_operations *id,
1668 const struct file_operations *enable,
1669 const struct file_operations *filter,
1670 const struct file_operations *format)
1671{
1672 struct ftrace_event_file *file;
1673
da511bf3 1674 file = trace_create_new_event(call, tr);
ae63b31e
SR
1675 if (!file)
1676 return -ENOMEM;
1677
ae63b31e
SR
1678 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1679}
1680
77248221
SR
1681/*
1682 * Just create a decriptor for early init. A descriptor is required
1683 * for enabling events at boot. We want to enable events before
1684 * the filesystem is initialized.
1685 */
1686static __init int
1687__trace_early_add_new_event(struct ftrace_event_call *call,
1688 struct trace_array *tr)
1689{
1690 struct ftrace_event_file *file;
1691
da511bf3 1692 file = trace_create_new_event(call, tr);
77248221
SR
1693 if (!file)
1694 return -ENOMEM;
1695
77248221
SR
1696 return 0;
1697}
1698
ae63b31e
SR
1699struct ftrace_module_file_ops;
1700static void __add_event_to_tracers(struct ftrace_event_call *call,
1701 struct ftrace_module_file_ops *file_ops);
1702
bd1a5c84
MH
1703/* Add an additional event_call dynamically */
1704int trace_add_event_call(struct ftrace_event_call *call)
1705{
1706 int ret;
a8227415 1707 mutex_lock(&trace_types_lock);
bd1a5c84 1708 mutex_lock(&event_mutex);
701970b3 1709
ae63b31e
SR
1710 ret = __register_event(call, NULL);
1711 if (ret >= 0)
1712 __add_event_to_tracers(call, NULL);
a2ca5e03 1713
ae63b31e 1714 mutex_unlock(&event_mutex);
a8227415 1715 mutex_unlock(&trace_types_lock);
ae63b31e 1716 return ret;
a2ca5e03
FW
1717}
1718
4fead8e4 1719/*
a8227415
AL
1720 * Must be called under locking of trace_types_lock, event_mutex and
1721 * trace_event_sem.
4fead8e4 1722 */
bd1a5c84
MH
1723static void __trace_remove_event_call(struct ftrace_event_call *call)
1724{
8781915a 1725 event_remove(call);
bd1a5c84
MH
1726 trace_destroy_fields(call);
1727 destroy_preds(call);
bd1a5c84
MH
1728}
1729
1730/* Remove an event_call */
1731void trace_remove_event_call(struct ftrace_event_call *call)
1732{
a8227415 1733 mutex_lock(&trace_types_lock);
bd1a5c84 1734 mutex_lock(&event_mutex);
52f6ad6d 1735 down_write(&trace_event_sem);
bd1a5c84 1736 __trace_remove_event_call(call);
52f6ad6d 1737 up_write(&trace_event_sem);
bd1a5c84 1738 mutex_unlock(&event_mutex);
a8227415 1739 mutex_unlock(&trace_types_lock);
bd1a5c84
MH
1740}
1741
1742#define for_each_event(event, start, end) \
1743 for (event = start; \
1744 (unsigned long)event < (unsigned long)end; \
1745 event++)
1746
1747#ifdef CONFIG_MODULES
1748
1749static LIST_HEAD(ftrace_module_file_list);
1750
1751/*
1752 * Modules must own their file_operations to keep up with
1753 * reference counting.
1754 */
1755struct ftrace_module_file_ops {
1756 struct list_head list;
1757 struct module *mod;
1758 struct file_operations id;
1759 struct file_operations enable;
1760 struct file_operations format;
1761 struct file_operations filter;
1762};
1763
315326c1
SRRH
1764static struct ftrace_module_file_ops *
1765find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
ae63b31e 1766{
315326c1
SRRH
1767 /*
1768 * As event_calls are added in groups by module,
1769 * when we find one file_ops, we don't need to search for
1770 * each call in that module, as the rest should be the
1771 * same. Only search for a new one if the last one did
1772 * not match.
1773 */
1774 if (file_ops && mod == file_ops->mod)
1775 return file_ops;
ae63b31e
SR
1776
1777 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1778 if (file_ops->mod == mod)
1779 return file_ops;
1780 }
1781 return NULL;
1782}
1783
701970b3
SR
1784static struct ftrace_module_file_ops *
1785trace_create_file_ops(struct module *mod)
1786{
1787 struct ftrace_module_file_ops *file_ops;
1788
1789 /*
1790 * This is a bit of a PITA. To allow for correct reference
1791 * counting, modules must "own" their file_operations.
1792 * To do this, we allocate the file operations that will be
1793 * used in the event directory.
1794 */
1795
1796 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1797 if (!file_ops)
1798 return NULL;
1799
1800 file_ops->mod = mod;
1801
1802 file_ops->id = ftrace_event_id_fops;
1803 file_ops->id.owner = mod;
1804
1805 file_ops->enable = ftrace_enable_fops;
1806 file_ops->enable.owner = mod;
1807
1808 file_ops->filter = ftrace_event_filter_fops;
1809 file_ops->filter.owner = mod;
1810
1811 file_ops->format = ftrace_event_format_fops;
1812 file_ops->format.owner = mod;
1813
1814 list_add(&file_ops->list, &ftrace_module_file_list);
1815
1816 return file_ops;
1817}
1818
6d723736
SR
1819static void trace_module_add_events(struct module *mod)
1820{
701970b3 1821 struct ftrace_module_file_ops *file_ops = NULL;
e4a9ea5e 1822 struct ftrace_event_call **call, **start, **end;
6d723736
SR
1823
1824 start = mod->trace_events;
1825 end = mod->trace_events + mod->num_trace_events;
1826
1827 if (start == end)
1828 return;
1829
67ead0a6
LZ
1830 file_ops = trace_create_file_ops(mod);
1831 if (!file_ops)
6d723736
SR
1832 return;
1833
1834 for_each_event(call, start, end) {
ae63b31e
SR
1835 __register_event(*call, mod);
1836 __add_event_to_tracers(*call, file_ops);
6d723736
SR
1837 }
1838}
1839
1840static void trace_module_remove_events(struct module *mod)
1841{
701970b3 1842 struct ftrace_module_file_ops *file_ops;
6d723736 1843 struct ftrace_event_call *call, *p;
575380da 1844 bool clear_trace = false;
6d723736 1845
52f6ad6d 1846 down_write(&trace_event_sem);
6d723736
SR
1847 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1848 if (call->mod == mod) {
575380da
SRRH
1849 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1850 clear_trace = true;
bd1a5c84 1851 __trace_remove_event_call(call);
6d723736
SR
1852 }
1853 }
701970b3
SR
1854
1855 /* Now free the file_operations */
1856 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1857 if (file_ops->mod == mod)
1858 break;
1859 }
1860 if (&file_ops->list != &ftrace_module_file_list) {
1861 list_del(&file_ops->list);
1862 kfree(file_ops);
1863 }
52f6ad6d 1864 up_write(&trace_event_sem);
9456f0fa
SR
1865
1866 /*
1867 * It is safest to reset the ring buffer if the module being unloaded
873c642f
SRRH
1868 * registered any events that were used. The only worry is if
1869 * a new module gets loaded, and takes on the same id as the events
1870 * of this module. When printing out the buffer, traced events left
1871 * over from this module may be passed to the new module events and
1872 * unexpected results may occur.
9456f0fa 1873 */
575380da 1874 if (clear_trace)
873c642f 1875 tracing_reset_all_online_cpus();
6d723736
SR
1876}
1877
61f919a1
SR
1878static int trace_module_notify(struct notifier_block *self,
1879 unsigned long val, void *data)
6d723736
SR
1880{
1881 struct module *mod = data;
1882
a8227415 1883 mutex_lock(&trace_types_lock);
6d723736
SR
1884 mutex_lock(&event_mutex);
1885 switch (val) {
1886 case MODULE_STATE_COMING:
1887 trace_module_add_events(mod);
1888 break;
1889 case MODULE_STATE_GOING:
1890 trace_module_remove_events(mod);
1891 break;
1892 }
1893 mutex_unlock(&event_mutex);
a8227415 1894 mutex_unlock(&trace_types_lock);
fd994989 1895
1473e441
SR
1896 return 0;
1897}
315326c1
SRRH
1898
1899static int
1900__trace_add_new_mod_event(struct ftrace_event_call *call,
1901 struct trace_array *tr,
1902 struct ftrace_module_file_ops *file_ops)
1903{
1904 return __trace_add_new_event(call, tr,
1905 &file_ops->id, &file_ops->enable,
1906 &file_ops->filter, &file_ops->format);
1907}
1908
61f919a1 1909#else
315326c1
SRRH
1910static inline struct ftrace_module_file_ops *
1911find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
ae63b31e
SR
1912{
1913 return NULL;
1914}
315326c1
SRRH
1915static inline int trace_module_notify(struct notifier_block *self,
1916 unsigned long val, void *data)
61f919a1
SR
1917{
1918 return 0;
1919}
315326c1
SRRH
1920static inline int
1921__trace_add_new_mod_event(struct ftrace_event_call *call,
1922 struct trace_array *tr,
1923 struct ftrace_module_file_ops *file_ops)
1924{
1925 return -ENODEV;
1926}
61f919a1 1927#endif /* CONFIG_MODULES */
1473e441 1928
ae63b31e
SR
1929/* Create a new event directory structure for a trace directory. */
1930static void
1931__trace_add_event_dirs(struct trace_array *tr)
1932{
1933 struct ftrace_module_file_ops *file_ops = NULL;
1934 struct ftrace_event_call *call;
1935 int ret;
1936
1937 list_for_each_entry(call, &ftrace_events, list) {
1938 if (call->mod) {
1939 /*
1940 * Directories for events by modules need to
1941 * keep module ref counts when opened (as we don't
1942 * want the module to disappear when reading one
1943 * of these files). The file_ops keep account of
1944 * the module ref count.
ae63b31e 1945 */
315326c1 1946 file_ops = find_ftrace_file_ops(file_ops, call->mod);
ae63b31e
SR
1947 if (!file_ops)
1948 continue; /* Warn? */
315326c1 1949 ret = __trace_add_new_mod_event(call, tr, file_ops);
ae63b31e
SR
1950 if (ret < 0)
1951 pr_warning("Could not create directory for event %s\n",
1952 call->name);
1953 continue;
1954 }
1955 ret = __trace_add_new_event(call, tr,
1956 &ftrace_event_id_fops,
1957 &ftrace_enable_fops,
1958 &ftrace_event_filter_fops,
1959 &ftrace_event_format_fops);
1960 if (ret < 0)
1961 pr_warning("Could not create directory for event %s\n",
1962 call->name);
1963 }
1964}
1965
3cd715de
SRRH
1966#ifdef CONFIG_DYNAMIC_FTRACE
1967
1968/* Avoid typos */
1969#define ENABLE_EVENT_STR "enable_event"
1970#define DISABLE_EVENT_STR "disable_event"
1971
1972struct event_probe_data {
1973 struct ftrace_event_file *file;
1974 unsigned long count;
1975 int ref;
1976 bool enable;
1977};
1978
1979static struct ftrace_event_file *
1980find_event_file(struct trace_array *tr, const char *system, const char *event)
1981{
1982 struct ftrace_event_file *file;
1983 struct ftrace_event_call *call;
1984
1985 list_for_each_entry(file, &tr->events, list) {
1986
1987 call = file->event_call;
1988
1989 if (!call->name || !call->class || !call->class->reg)
1990 continue;
1991
1992 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1993 continue;
1994
1995 if (strcmp(event, call->name) == 0 &&
1996 strcmp(system, call->class->system) == 0)
1997 return file;
1998 }
1999 return NULL;
2000}
2001
2002static void
2003event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2004{
2005 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2006 struct event_probe_data *data = *pdata;
2007
2008 if (!data)
2009 return;
2010
2011 if (data->enable)
2012 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2013 else
2014 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2015}
2016
2017static void
2018event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2019{
2020 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2021 struct event_probe_data *data = *pdata;
2022
2023 if (!data)
2024 return;
2025
2026 if (!data->count)
2027 return;
2028
2029 /* Skip if the event is in a state we want to switch to */
2030 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2031 return;
2032
2033 if (data->count != -1)
2034 (data->count)--;
2035
2036 event_enable_probe(ip, parent_ip, _data);
2037}
2038
2039static int
2040event_enable_print(struct seq_file *m, unsigned long ip,
2041 struct ftrace_probe_ops *ops, void *_data)
2042{
2043 struct event_probe_data *data = _data;
2044
2045 seq_printf(m, "%ps:", (void *)ip);
2046
2047 seq_printf(m, "%s:%s:%s",
2048 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2049 data->file->event_call->class->system,
2050 data->file->event_call->name);
2051
2052 if (data->count == -1)
2053 seq_printf(m, ":unlimited\n");
2054 else
2055 seq_printf(m, ":count=%ld\n", data->count);
2056
2057 return 0;
2058}
2059
2060static int
2061event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2062 void **_data)
2063{
2064 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2065 struct event_probe_data *data = *pdata;
2066
2067 data->ref++;
2068 return 0;
2069}
2070
2071static void
2072event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2073 void **_data)
2074{
2075 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2076 struct event_probe_data *data = *pdata;
2077
2078 if (WARN_ON_ONCE(data->ref <= 0))
2079 return;
2080
2081 data->ref--;
2082 if (!data->ref) {
2083 /* Remove the SOFT_MODE flag */
2084 __ftrace_event_enable_disable(data->file, 0, 1);
2085 module_put(data->file->event_call->mod);
2086 kfree(data);
2087 }
2088 *pdata = NULL;
2089}
2090
2091static struct ftrace_probe_ops event_enable_probe_ops = {
2092 .func = event_enable_probe,
2093 .print = event_enable_print,
2094 .init = event_enable_init,
2095 .free = event_enable_free,
2096};
2097
2098static struct ftrace_probe_ops event_enable_count_probe_ops = {
2099 .func = event_enable_count_probe,
2100 .print = event_enable_print,
2101 .init = event_enable_init,
2102 .free = event_enable_free,
2103};
2104
2105static struct ftrace_probe_ops event_disable_probe_ops = {
2106 .func = event_enable_probe,
2107 .print = event_enable_print,
2108 .init = event_enable_init,
2109 .free = event_enable_free,
2110};
2111
2112static struct ftrace_probe_ops event_disable_count_probe_ops = {
2113 .func = event_enable_count_probe,
2114 .print = event_enable_print,
2115 .init = event_enable_init,
2116 .free = event_enable_free,
2117};
2118
2119static int
2120event_enable_func(struct ftrace_hash *hash,
2121 char *glob, char *cmd, char *param, int enabled)
2122{
2123 struct trace_array *tr = top_trace_array();
2124 struct ftrace_event_file *file;
2125 struct ftrace_probe_ops *ops;
2126 struct event_probe_data *data;
2127 const char *system;
2128 const char *event;
2129 char *number;
2130 bool enable;
2131 int ret;
2132
2133 /* hash funcs only work with set_ftrace_filter */
8092e808 2134 if (!enabled || !param)
3cd715de
SRRH
2135 return -EINVAL;
2136
2137 system = strsep(&param, ":");
2138 if (!param)
2139 return -EINVAL;
2140
2141 event = strsep(&param, ":");
2142
2143 mutex_lock(&event_mutex);
2144
2145 ret = -EINVAL;
2146 file = find_event_file(tr, system, event);
2147 if (!file)
2148 goto out;
2149
2150 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2151
2152 if (enable)
2153 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2154 else
2155 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2156
2157 if (glob[0] == '!') {
2158 unregister_ftrace_function_probe_func(glob+1, ops);
2159 ret = 0;
2160 goto out;
2161 }
2162
2163 ret = -ENOMEM;
2164 data = kzalloc(sizeof(*data), GFP_KERNEL);
2165 if (!data)
2166 goto out;
2167
2168 data->enable = enable;
2169 data->count = -1;
2170 data->file = file;
2171
2172 if (!param)
2173 goto out_reg;
2174
2175 number = strsep(&param, ":");
2176
2177 ret = -EINVAL;
2178 if (!strlen(number))
2179 goto out_free;
2180
2181 /*
2182 * We use the callback data field (which is a pointer)
2183 * as our counter.
2184 */
2185 ret = kstrtoul(number, 0, &data->count);
2186 if (ret)
2187 goto out_free;
2188
2189 out_reg:
2190 /* Don't let event modules unload while probe registered */
2191 ret = try_module_get(file->event_call->mod);
6ed01066
MH
2192 if (!ret) {
2193 ret = -EBUSY;
3cd715de 2194 goto out_free;
6ed01066 2195 }
3cd715de
SRRH
2196
2197 ret = __ftrace_event_enable_disable(file, 1, 1);
2198 if (ret < 0)
2199 goto out_put;
2200 ret = register_ftrace_function_probe(glob, ops, data);
ff305ded
SRRH
2201 /*
2202 * The above returns on success the # of functions enabled,
2203 * but if it didn't find any functions it returns zero.
2204 * Consider no functions a failure too.
2205 */
a5b85bd1
MH
2206 if (!ret) {
2207 ret = -ENOENT;
3cd715de 2208 goto out_disable;
ff305ded
SRRH
2209 } else if (ret < 0)
2210 goto out_disable;
2211 /* Just return zero, not the number of enabled functions */
2212 ret = 0;
3cd715de
SRRH
2213 out:
2214 mutex_unlock(&event_mutex);
2215 return ret;
2216
2217 out_disable:
2218 __ftrace_event_enable_disable(file, 0, 1);
2219 out_put:
2220 module_put(file->event_call->mod);
2221 out_free:
2222 kfree(data);
2223 goto out;
2224}
2225
2226static struct ftrace_func_command event_enable_cmd = {
2227 .name = ENABLE_EVENT_STR,
2228 .func = event_enable_func,
2229};
2230
2231static struct ftrace_func_command event_disable_cmd = {
2232 .name = DISABLE_EVENT_STR,
2233 .func = event_enable_func,
2234};
2235
2236static __init int register_event_cmds(void)
2237{
2238 int ret;
2239
2240 ret = register_ftrace_command(&event_enable_cmd);
2241 if (WARN_ON(ret < 0))
2242 return ret;
2243 ret = register_ftrace_command(&event_disable_cmd);
2244 if (WARN_ON(ret < 0))
2245 unregister_ftrace_command(&event_enable_cmd);
2246 return ret;
2247}
2248#else
2249static inline int register_event_cmds(void) { return 0; }
2250#endif /* CONFIG_DYNAMIC_FTRACE */
2251
77248221
SR
2252/*
2253 * The top level array has already had its ftrace_event_file
2254 * descriptors created in order to allow for early events to
2255 * be recorded. This function is called after the debugfs has been
2256 * initialized, and we now have to create the files associated
2257 * to the events.
2258 */
2259static __init void
2260__trace_early_add_event_dirs(struct trace_array *tr)
2261{
2262 struct ftrace_event_file *file;
2263 int ret;
2264
2265
2266 list_for_each_entry(file, &tr->events, list) {
2267 ret = event_create_dir(tr->event_dir, file,
2268 &ftrace_event_id_fops,
2269 &ftrace_enable_fops,
2270 &ftrace_event_filter_fops,
2271 &ftrace_event_format_fops);
2272 if (ret < 0)
2273 pr_warning("Could not create directory for event %s\n",
2274 file->event_call->name);
2275 }
2276}
2277
2278/*
2279 * For early boot up, the top trace array requires to have
2280 * a list of events that can be enabled. This must be done before
2281 * the filesystem is set up in order to allow events to be traced
2282 * early.
2283 */
2284static __init void
2285__trace_early_add_events(struct trace_array *tr)
2286{
2287 struct ftrace_event_call *call;
2288 int ret;
2289
2290 list_for_each_entry(call, &ftrace_events, list) {
2291 /* Early boot up should not have any modules loaded */
2292 if (WARN_ON_ONCE(call->mod))
2293 continue;
2294
2295 ret = __trace_early_add_new_event(call, tr);
2296 if (ret < 0)
2297 pr_warning("Could not create early event %s\n",
2298 call->name);
2299 }
2300}
2301
0c8916c3
SR
2302/* Remove the event directory structure for a trace directory. */
2303static void
2304__trace_remove_event_dirs(struct trace_array *tr)
2305{
2306 struct ftrace_event_file *file, *next;
2307
2308 list_for_each_entry_safe(file, next, &tr->events, list) {
2309 list_del(&file->list);
2310 debugfs_remove_recursive(file->dir);
2311 remove_subsystem(file->system);
d1a29143 2312 kmem_cache_free(file_cachep, file);
0c8916c3
SR
2313 }
2314}
2315
ae63b31e
SR
2316static void
2317__add_event_to_tracers(struct ftrace_event_call *call,
2318 struct ftrace_module_file_ops *file_ops)
2319{
2320 struct trace_array *tr;
2321
2322 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2323 if (file_ops)
315326c1 2324 __trace_add_new_mod_event(call, tr, file_ops);
ae63b31e
SR
2325 else
2326 __trace_add_new_event(call, tr,
2327 &ftrace_event_id_fops,
2328 &ftrace_enable_fops,
2329 &ftrace_event_filter_fops,
2330 &ftrace_event_format_fops);
2331 }
2332}
2333
ec827c7e 2334static struct notifier_block trace_module_nb = {
6d723736
SR
2335 .notifier_call = trace_module_notify,
2336 .priority = 0,
2337};
2338
e4a9ea5e
SR
2339extern struct ftrace_event_call *__start_ftrace_events[];
2340extern struct ftrace_event_call *__stop_ftrace_events[];
a59fd602 2341
020e5f85
LZ
2342static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2343
2344static __init int setup_trace_event(char *str)
2345{
2346 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
55034cd6
SRRH
2347 ring_buffer_expanded = true;
2348 tracing_selftest_disabled = true;
020e5f85
LZ
2349
2350 return 1;
2351}
2352__setup("trace_event=", setup_trace_event);
2353
77248221
SR
2354/* Expects to have event_mutex held when called */
2355static int
2356create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
ae63b31e
SR
2357{
2358 struct dentry *d_events;
2359 struct dentry *entry;
2360
2361 entry = debugfs_create_file("set_event", 0644, parent,
2362 tr, &ftrace_set_event_fops);
2363 if (!entry) {
2364 pr_warning("Could not create debugfs 'set_event' entry\n");
2365 return -ENOMEM;
2366 }
2367
2368 d_events = debugfs_create_dir("events", parent);
277ba044 2369 if (!d_events) {
ae63b31e 2370 pr_warning("Could not create debugfs 'events' directory\n");
277ba044
SR
2371 return -ENOMEM;
2372 }
ae63b31e
SR
2373
2374 /* ring buffer internal formats */
2375 trace_create_file("header_page", 0444, d_events,
2376 ring_buffer_print_page_header,
2377 &ftrace_show_header_fops);
2378
2379 trace_create_file("header_event", 0444, d_events,
2380 ring_buffer_print_entry_header,
2381 &ftrace_show_header_fops);
2382
2383 trace_create_file("enable", 0644, d_events,
2384 tr, &ftrace_tr_enable_fops);
2385
2386 tr->event_dir = d_events;
77248221
SR
2387
2388 return 0;
2389}
2390
2391/**
2392 * event_trace_add_tracer - add a instance of a trace_array to events
2393 * @parent: The parent dentry to place the files/directories for events in
2394 * @tr: The trace array associated with these events
2395 *
2396 * When a new instance is created, it needs to set up its events
2397 * directory, as well as other files associated with events. It also
2398 * creates the event hierachry in the @parent/events directory.
2399 *
2400 * Returns 0 on success.
2401 */
2402int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2403{
2404 int ret;
2405
2406 mutex_lock(&event_mutex);
2407
2408 ret = create_event_toplevel_files(parent, tr);
2409 if (ret)
2410 goto out_unlock;
2411
52f6ad6d 2412 down_write(&trace_event_sem);
ae63b31e 2413 __trace_add_event_dirs(tr);
52f6ad6d 2414 up_write(&trace_event_sem);
277ba044 2415
77248221 2416 out_unlock:
277ba044 2417 mutex_unlock(&event_mutex);
ae63b31e 2418
77248221
SR
2419 return ret;
2420}
2421
2422/*
2423 * The top trace array already had its file descriptors created.
2424 * Now the files themselves need to be created.
2425 */
2426static __init int
2427early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2428{
2429 int ret;
2430
2431 mutex_lock(&event_mutex);
2432
2433 ret = create_event_toplevel_files(parent, tr);
2434 if (ret)
2435 goto out_unlock;
2436
52f6ad6d 2437 down_write(&trace_event_sem);
77248221 2438 __trace_early_add_event_dirs(tr);
52f6ad6d 2439 up_write(&trace_event_sem);
77248221
SR
2440
2441 out_unlock:
2442 mutex_unlock(&event_mutex);
2443
2444 return ret;
ae63b31e
SR
2445}
2446
0c8916c3
SR
2447int event_trace_del_tracer(struct trace_array *tr)
2448{
0c8916c3
SR
2449 mutex_lock(&event_mutex);
2450
2a6c24af
SRRH
2451 /* Disable any running events */
2452 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2453
52f6ad6d 2454 down_write(&trace_event_sem);
0c8916c3
SR
2455 __trace_remove_event_dirs(tr);
2456 debugfs_remove_recursive(tr->event_dir);
52f6ad6d 2457 up_write(&trace_event_sem);
0c8916c3
SR
2458
2459 tr->event_dir = NULL;
2460
2461 mutex_unlock(&event_mutex);
2462
2463 return 0;
2464}
2465
d1a29143
SR
2466static __init int event_trace_memsetup(void)
2467{
2468 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2469 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2470 return 0;
2471}
2472
8781915a
EG
2473static __init int event_trace_enable(void)
2474{
ae63b31e 2475 struct trace_array *tr = top_trace_array();
8781915a
EG
2476 struct ftrace_event_call **iter, *call;
2477 char *buf = bootup_event_buf;
2478 char *token;
2479 int ret;
2480
2481 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2482
2483 call = *iter;
2484 ret = event_init(call);
2485 if (!ret)
2486 list_add(&call->list, &ftrace_events);
2487 }
2488
77248221
SR
2489 /*
2490 * We need the top trace array to have a working set of trace
2491 * points at early init, before the debug files and directories
2492 * are created. Create the file entries now, and attach them
2493 * to the actual file dentries later.
2494 */
2495 __trace_early_add_events(tr);
2496
8781915a
EG
2497 while (true) {
2498 token = strsep(&buf, ",");
2499
2500 if (!token)
2501 break;
2502 if (!*token)
2503 continue;
2504
ae63b31e 2505 ret = ftrace_set_clr_event(tr, token, 1);
8781915a
EG
2506 if (ret)
2507 pr_warn("Failed to enable trace event: %s\n", token);
2508 }
81698831
SR
2509
2510 trace_printk_start_comm();
2511
3cd715de
SRRH
2512 register_event_cmds();
2513
8781915a
EG
2514 return 0;
2515}
2516
b77e38aa
SR
2517static __init int event_trace_init(void)
2518{
ae63b31e 2519 struct trace_array *tr;
b77e38aa
SR
2520 struct dentry *d_tracer;
2521 struct dentry *entry;
6d723736 2522 int ret;
b77e38aa 2523
ae63b31e
SR
2524 tr = top_trace_array();
2525
b77e38aa
SR
2526 d_tracer = tracing_init_dentry();
2527 if (!d_tracer)
2528 return 0;
2529
2314c4ae 2530 entry = debugfs_create_file("available_events", 0444, d_tracer,
ae63b31e 2531 tr, &ftrace_avail_fops);
2314c4ae
SR
2532 if (!entry)
2533 pr_warning("Could not create debugfs "
2534 "'available_events' entry\n");
2535
8728fe50
LZ
2536 if (trace_define_common_fields())
2537 pr_warning("tracing: Failed to allocate common fields");
2538
77248221 2539 ret = early_event_add_tracer(d_tracer, tr);
ae63b31e
SR
2540 if (ret)
2541 return ret;
020e5f85 2542
6d723736 2543 ret = register_module_notifier(&trace_module_nb);
55379376 2544 if (ret)
6d723736
SR
2545 pr_warning("Failed to register trace events module notifier\n");
2546
b77e38aa
SR
2547 return 0;
2548}
d1a29143 2549early_initcall(event_trace_memsetup);
8781915a 2550core_initcall(event_trace_enable);
b77e38aa 2551fs_initcall(event_trace_init);
e6187007
SR
2552
2553#ifdef CONFIG_FTRACE_STARTUP_TEST
2554
2555static DEFINE_SPINLOCK(test_spinlock);
2556static DEFINE_SPINLOCK(test_spinlock_irq);
2557static DEFINE_MUTEX(test_mutex);
2558
2559static __init void test_work(struct work_struct *dummy)
2560{
2561 spin_lock(&test_spinlock);
2562 spin_lock_irq(&test_spinlock_irq);
2563 udelay(1);
2564 spin_unlock_irq(&test_spinlock_irq);
2565 spin_unlock(&test_spinlock);
2566
2567 mutex_lock(&test_mutex);
2568 msleep(1);
2569 mutex_unlock(&test_mutex);
2570}
2571
2572static __init int event_test_thread(void *unused)
2573{
2574 void *test_malloc;
2575
2576 test_malloc = kmalloc(1234, GFP_KERNEL);
2577 if (!test_malloc)
2578 pr_info("failed to kmalloc\n");
2579
2580 schedule_on_each_cpu(test_work);
2581
2582 kfree(test_malloc);
2583
2584 set_current_state(TASK_INTERRUPTIBLE);
2585 while (!kthread_should_stop())
2586 schedule();
2587
2588 return 0;
2589}
2590
2591/*
2592 * Do various things that may trigger events.
2593 */
2594static __init void event_test_stuff(void)
2595{
2596 struct task_struct *test_thread;
2597
2598 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2599 msleep(1);
2600 kthread_stop(test_thread);
2601}
2602
2603/*
2604 * For every trace event defined, we will test each trace point separately,
2605 * and then by groups, and finally all trace points.
2606 */
9ea21c1e 2607static __init void event_trace_self_tests(void)
e6187007 2608{
ae63b31e
SR
2609 struct ftrace_subsystem_dir *dir;
2610 struct ftrace_event_file *file;
e6187007
SR
2611 struct ftrace_event_call *call;
2612 struct event_subsystem *system;
ae63b31e 2613 struct trace_array *tr;
e6187007
SR
2614 int ret;
2615
ae63b31e
SR
2616 tr = top_trace_array();
2617
e6187007
SR
2618 pr_info("Running tests on trace events:\n");
2619
ae63b31e
SR
2620 list_for_each_entry(file, &tr->events, list) {
2621
2622 call = file->event_call;
e6187007 2623
2239291a
SR
2624 /* Only test those that have a probe */
2625 if (!call->class || !call->class->probe)
e6187007
SR
2626 continue;
2627
1f5a6b45
SR
2628/*
2629 * Testing syscall events here is pretty useless, but
2630 * we still do it if configured. But this is time consuming.
2631 * What we really need is a user thread to perform the
2632 * syscalls as we test.
2633 */
2634#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
8f082018
SR
2635 if (call->class->system &&
2636 strcmp(call->class->system, "syscalls") == 0)
1f5a6b45
SR
2637 continue;
2638#endif
2639
e6187007
SR
2640 pr_info("Testing event %s: ", call->name);
2641
2642 /*
2643 * If an event is already enabled, someone is using
2644 * it and the self test should not be on.
2645 */
ae63b31e 2646 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
e6187007
SR
2647 pr_warning("Enabled event during self test!\n");
2648 WARN_ON_ONCE(1);
2649 continue;
2650 }
2651
ae63b31e 2652 ftrace_event_enable_disable(file, 1);
e6187007 2653 event_test_stuff();
ae63b31e 2654 ftrace_event_enable_disable(file, 0);
e6187007
SR
2655
2656 pr_cont("OK\n");
2657 }
2658
2659 /* Now test at the sub system level */
2660
2661 pr_info("Running tests on trace event systems:\n");
2662
ae63b31e
SR
2663 list_for_each_entry(dir, &tr->systems, list) {
2664
2665 system = dir->subsystem;
e6187007
SR
2666
2667 /* the ftrace system is special, skip it */
2668 if (strcmp(system->name, "ftrace") == 0)
2669 continue;
2670
2671 pr_info("Testing event system %s: ", system->name);
2672
ae63b31e 2673 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
e6187007
SR
2674 if (WARN_ON_ONCE(ret)) {
2675 pr_warning("error enabling system %s\n",
2676 system->name);
2677 continue;
2678 }
2679
2680 event_test_stuff();
2681
ae63b31e 2682 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
76bab1b7 2683 if (WARN_ON_ONCE(ret)) {
e6187007
SR
2684 pr_warning("error disabling system %s\n",
2685 system->name);
76bab1b7
YL
2686 continue;
2687 }
e6187007
SR
2688
2689 pr_cont("OK\n");
2690 }
2691
2692 /* Test with all events enabled */
2693
2694 pr_info("Running tests on all trace events:\n");
2695 pr_info("Testing all events: ");
2696
ae63b31e 2697 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
e6187007 2698 if (WARN_ON_ONCE(ret)) {
e6187007 2699 pr_warning("error enabling all events\n");
9ea21c1e 2700 return;
e6187007
SR
2701 }
2702
2703 event_test_stuff();
2704
2705 /* reset sysname */
ae63b31e 2706 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
e6187007
SR
2707 if (WARN_ON_ONCE(ret)) {
2708 pr_warning("error disabling all events\n");
9ea21c1e 2709 return;
e6187007
SR
2710 }
2711
2712 pr_cont("OK\n");
9ea21c1e
SR
2713}
2714
2715#ifdef CONFIG_FUNCTION_TRACER
2716
245b2e70 2717static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
9ea21c1e
SR
2718
2719static void
2f5f6ad9 2720function_test_events_call(unsigned long ip, unsigned long parent_ip,
a1e2e31d 2721 struct ftrace_ops *op, struct pt_regs *pt_regs)
9ea21c1e
SR
2722{
2723 struct ring_buffer_event *event;
e77405ad 2724 struct ring_buffer *buffer;
9ea21c1e
SR
2725 struct ftrace_entry *entry;
2726 unsigned long flags;
2727 long disabled;
9ea21c1e
SR
2728 int cpu;
2729 int pc;
2730
2731 pc = preempt_count();
5168ae50 2732 preempt_disable_notrace();
9ea21c1e 2733 cpu = raw_smp_processor_id();
245b2e70 2734 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
9ea21c1e
SR
2735
2736 if (disabled != 1)
2737 goto out;
2738
2739 local_save_flags(flags);
2740
e77405ad
SR
2741 event = trace_current_buffer_lock_reserve(&buffer,
2742 TRACE_FN, sizeof(*entry),
9ea21c1e
SR
2743 flags, pc);
2744 if (!event)
2745 goto out;
2746 entry = ring_buffer_event_data(event);
2747 entry->ip = ip;
2748 entry->parent_ip = parent_ip;
2749
0d5c6e1c 2750 trace_buffer_unlock_commit(buffer, event, flags, pc);
9ea21c1e
SR
2751
2752 out:
245b2e70 2753 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
5168ae50 2754 preempt_enable_notrace();
9ea21c1e
SR
2755}
2756
2757static struct ftrace_ops trace_ops __initdata =
2758{
2759 .func = function_test_events_call,
4740974a 2760 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
9ea21c1e
SR
2761};
2762
2763static __init void event_trace_self_test_with_function(void)
2764{
17bb615a
SR
2765 int ret;
2766 ret = register_ftrace_function(&trace_ops);
2767 if (WARN_ON(ret < 0)) {
2768 pr_info("Failed to enable function tracer for event tests\n");
2769 return;
2770 }
9ea21c1e
SR
2771 pr_info("Running tests again, along with the function tracer\n");
2772 event_trace_self_tests();
2773 unregister_ftrace_function(&trace_ops);
2774}
2775#else
2776static __init void event_trace_self_test_with_function(void)
2777{
2778}
2779#endif
2780
2781static __init int event_trace_self_tests_init(void)
2782{
020e5f85
LZ
2783 if (!tracing_selftest_disabled) {
2784 event_trace_self_tests();
2785 event_trace_self_test_with_function();
2786 }
e6187007
SR
2787
2788 return 0;
2789}
2790
28d20e2d 2791late_initcall(event_trace_self_tests_init);
e6187007
SR
2792
2793#endif