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