]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/trace/trace_events.c
tracing: Move the pid_list seq_file functions to be global
[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>
8434dc93 16#include <linux/tracefs.h>
b77e38aa 17#include <linux/uaccess.h>
f4d34a87 18#include <linux/vmalloc.h>
b77e38aa
SR
19#include <linux/module.h>
20#include <linux/ctype.h>
49090107 21#include <linux/sort.h>
5a0e3ad6 22#include <linux/slab.h>
e6187007 23#include <linux/delay.h>
b77e38aa 24
3fdaf80f
SRRH
25#include <trace/events/sched.h>
26
020e5f85
LZ
27#include <asm/setup.h>
28
91729ef9 29#include "trace_output.h"
b77e38aa 30
4e5292ea 31#undef TRACE_SYSTEM
b628b3e6
SR
32#define TRACE_SYSTEM "TRACE_SYSTEM"
33
20c8928a 34DEFINE_MUTEX(event_mutex);
11a241a3 35
a59fd602 36LIST_HEAD(ftrace_events);
9f616680 37static LIST_HEAD(ftrace_generic_fields);
b3a8c6fd 38static LIST_HEAD(ftrace_common_fields);
a59fd602 39
d1a29143
SR
40#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41
42static struct kmem_cache *field_cachep;
43static struct kmem_cache *file_cachep;
44
6e94a780
SR
45static inline int system_refcount(struct event_subsystem *system)
46{
79ac6ef5 47 return system->ref_count;
6e94a780
SR
48}
49
50static int system_refcount_inc(struct event_subsystem *system)
51{
79ac6ef5 52 return system->ref_count++;
6e94a780
SR
53}
54
55static int system_refcount_dec(struct event_subsystem *system)
56{
79ac6ef5 57 return --system->ref_count;
6e94a780
SR
58}
59
ae63b31e
SR
60/* Double loops, do not use break, only goto's work */
61#define do_for_each_event_file(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
63 list_for_each_entry(file, &tr->events, list)
64
65#define do_for_each_event_file_safe(tr, file) \
66 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
7f1d2f82 67 struct trace_event_file *___n; \
ae63b31e
SR
68 list_for_each_entry_safe(file, ___n, &tr->events, list)
69
70#define while_for_each_event_file() \
71 }
72
b3a8c6fd 73static struct list_head *
2425bcb9 74trace_get_fields(struct trace_event_call *event_call)
2e33af02
SR
75{
76 if (!event_call->class->get_fields)
77 return &event_call->class->fields;
78 return event_call->class->get_fields(event_call);
79}
80
b3a8c6fd
J
81static struct ftrace_event_field *
82__find_event_field(struct list_head *head, char *name)
83{
84 struct ftrace_event_field *field;
85
86 list_for_each_entry(field, head, link) {
87 if (!strcmp(field->name, name))
88 return field;
89 }
90
91 return NULL;
92}
93
94struct ftrace_event_field *
2425bcb9 95trace_find_event_field(struct trace_event_call *call, char *name)
b3a8c6fd
J
96{
97 struct ftrace_event_field *field;
98 struct list_head *head;
99
e57cbaf0
SRRH
100 head = trace_get_fields(call);
101 field = __find_event_field(head, name);
9f616680
DW
102 if (field)
103 return field;
104
e57cbaf0 105 field = __find_event_field(&ftrace_generic_fields, name);
b3a8c6fd
J
106 if (field)
107 return field;
108
e57cbaf0 109 return __find_event_field(&ftrace_common_fields, name);
b3a8c6fd
J
110}
111
8728fe50
LZ
112static int __trace_define_field(struct list_head *head, const char *type,
113 const char *name, int offset, int size,
114 int is_signed, int filter_type)
cf027f64
TZ
115{
116 struct ftrace_event_field *field;
117
d1a29143 118 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
cf027f64 119 if (!field)
aaf6ac0f 120 return -ENOMEM;
fe9f57f2 121
92edca07
SR
122 field->name = name;
123 field->type = type;
fe9f57f2 124
43b51ead
LZ
125 if (filter_type == FILTER_OTHER)
126 field->filter_type = filter_assign_type(type);
127 else
128 field->filter_type = filter_type;
129
cf027f64
TZ
130 field->offset = offset;
131 field->size = size;
a118e4d1 132 field->is_signed = is_signed;
aa38e9fc 133
2e33af02 134 list_add(&field->link, head);
cf027f64
TZ
135
136 return 0;
cf027f64 137}
8728fe50 138
2425bcb9 139int trace_define_field(struct trace_event_call *call, const char *type,
8728fe50
LZ
140 const char *name, int offset, int size, int is_signed,
141 int filter_type)
142{
143 struct list_head *head;
144
145 if (WARN_ON(!call->class))
146 return 0;
147
148 head = trace_get_fields(call);
149 return __trace_define_field(head, type, name, offset, size,
150 is_signed, filter_type);
151}
17c873ec 152EXPORT_SYMBOL_GPL(trace_define_field);
cf027f64 153
9f616680
DW
154#define __generic_field(type, item, filter_type) \
155 ret = __trace_define_field(&ftrace_generic_fields, #type, \
156 #item, 0, 0, is_signed_type(type), \
157 filter_type); \
158 if (ret) \
159 return ret;
160
e647d6b3 161#define __common_field(type, item) \
8728fe50
LZ
162 ret = __trace_define_field(&ftrace_common_fields, #type, \
163 "common_" #item, \
164 offsetof(typeof(ent), item), \
165 sizeof(ent.item), \
166 is_signed_type(type), FILTER_OTHER); \
e647d6b3
LZ
167 if (ret) \
168 return ret;
169
9f616680
DW
170static int trace_define_generic_fields(void)
171{
172 int ret;
173
e57cbaf0
SRRH
174 __generic_field(int, CPU, FILTER_CPU);
175 __generic_field(int, cpu, FILTER_CPU);
176 __generic_field(char *, COMM, FILTER_COMM);
177 __generic_field(char *, comm, FILTER_COMM);
9f616680
DW
178
179 return ret;
180}
181
8728fe50 182static int trace_define_common_fields(void)
e647d6b3
LZ
183{
184 int ret;
185 struct trace_entry ent;
186
187 __common_field(unsigned short, type);
188 __common_field(unsigned char, flags);
189 __common_field(unsigned char, preempt_count);
190 __common_field(int, pid);
e647d6b3
LZ
191
192 return ret;
193}
194
2425bcb9 195static void trace_destroy_fields(struct trace_event_call *call)
2df75e41
LZ
196{
197 struct ftrace_event_field *field, *next;
2e33af02 198 struct list_head *head;
2df75e41 199
2e33af02
SR
200 head = trace_get_fields(call);
201 list_for_each_entry_safe(field, next, head, link) {
2df75e41 202 list_del(&field->link);
d1a29143 203 kmem_cache_free(field_cachep, field);
2df75e41
LZ
204 }
205}
206
32bbe007
AS
207/*
208 * run-time version of trace_event_get_offsets_<call>() that returns the last
209 * accessible offset of trace fields excluding __dynamic_array bytes
210 */
211int trace_event_get_offsets(struct trace_event_call *call)
212{
213 struct ftrace_event_field *tail;
214 struct list_head *head;
215
216 head = trace_get_fields(call);
217 /*
218 * head->next points to the last field with the largest offset,
219 * since it was added last by trace_define_field()
220 */
221 tail = list_first_entry(head, struct ftrace_event_field, link);
222 return tail->offset + tail->size;
223}
224
2425bcb9 225int trace_event_raw_init(struct trace_event_call *call)
87d9b4e1
LZ
226{
227 int id;
228
9023c930 229 id = register_trace_event(&call->event);
87d9b4e1
LZ
230 if (!id)
231 return -ENODEV;
87d9b4e1
LZ
232
233 return 0;
234}
235EXPORT_SYMBOL_GPL(trace_event_raw_init);
236
3fdaf80f
SRRH
237bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
238{
239 struct trace_array *tr = trace_file->tr;
240 struct trace_array_cpu *data;
241 struct trace_pid_list *pid_list;
242
243 pid_list = rcu_dereference_sched(tr->filtered_pids);
244 if (!pid_list)
245 return false;
246
247 data = this_cpu_ptr(tr->trace_buffer.data);
248
249 return data->ignore_pid;
250}
251EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
252
3f795dcf
SRRH
253void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
254 struct trace_event_file *trace_file,
255 unsigned long len)
3fd40d1e 256{
2425bcb9 257 struct trace_event_call *event_call = trace_file->event_call;
3fd40d1e 258
3fdaf80f
SRRH
259 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
260 trace_event_ignore_this_pid(trace_file))
261 return NULL;
262
3fd40d1e
SR
263 local_save_flags(fbuffer->flags);
264 fbuffer->pc = preempt_count();
7f1d2f82 265 fbuffer->trace_file = trace_file;
3fd40d1e
SR
266
267 fbuffer->event =
7f1d2f82 268 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
3fd40d1e
SR
269 event_call->event.type, len,
270 fbuffer->flags, fbuffer->pc);
271 if (!fbuffer->event)
272 return NULL;
273
274 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
275 return fbuffer->entry;
276}
3f795dcf 277EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
3fd40d1e 278
0daa2302
SRRH
279static DEFINE_SPINLOCK(tracepoint_iter_lock);
280
3f795dcf 281static void output_printk(struct trace_event_buffer *fbuffer)
0daa2302 282{
2425bcb9 283 struct trace_event_call *event_call;
0daa2302
SRRH
284 struct trace_event *event;
285 unsigned long flags;
286 struct trace_iterator *iter = tracepoint_print_iter;
287
288 if (!iter)
289 return;
290
7f1d2f82 291 event_call = fbuffer->trace_file->event_call;
0daa2302
SRRH
292 if (!event_call || !event_call->event.funcs ||
293 !event_call->event.funcs->trace)
294 return;
295
7f1d2f82 296 event = &fbuffer->trace_file->event_call->event;
0daa2302
SRRH
297
298 spin_lock_irqsave(&tracepoint_iter_lock, flags);
299 trace_seq_init(&iter->seq);
300 iter->ent = fbuffer->entry;
301 event_call->event.funcs->trace(iter, 0, event);
302 trace_seq_putc(&iter->seq, 0);
303 printk("%s", iter->seq.buffer);
304
305 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
306}
307
3f795dcf 308void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
3fd40d1e 309{
0daa2302
SRRH
310 if (tracepoint_printk)
311 output_printk(fbuffer);
312
7f1d2f82 313 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
3fd40d1e
SR
314 fbuffer->event, fbuffer->entry,
315 fbuffer->flags, fbuffer->pc);
316}
3f795dcf 317EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
3fd40d1e 318
2425bcb9 319int trace_event_reg(struct trace_event_call *call,
9023c930 320 enum trace_reg type, void *data)
a1d0ce82 321{
7f1d2f82 322 struct trace_event_file *file = data;
ae63b31e 323
de7b2973 324 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
a1d0ce82
SR
325 switch (type) {
326 case TRACE_REG_REGISTER:
de7b2973 327 return tracepoint_probe_register(call->tp,
a1d0ce82 328 call->class->probe,
ae63b31e 329 file);
a1d0ce82 330 case TRACE_REG_UNREGISTER:
de7b2973 331 tracepoint_probe_unregister(call->tp,
a1d0ce82 332 call->class->probe,
ae63b31e 333 file);
a1d0ce82
SR
334 return 0;
335
336#ifdef CONFIG_PERF_EVENTS
337 case TRACE_REG_PERF_REGISTER:
de7b2973 338 return tracepoint_probe_register(call->tp,
a1d0ce82
SR
339 call->class->perf_probe,
340 call);
341 case TRACE_REG_PERF_UNREGISTER:
de7b2973 342 tracepoint_probe_unregister(call->tp,
a1d0ce82
SR
343 call->class->perf_probe,
344 call);
345 return 0;
ceec0b6f
JO
346 case TRACE_REG_PERF_OPEN:
347 case TRACE_REG_PERF_CLOSE:
489c75c3
JO
348 case TRACE_REG_PERF_ADD:
349 case TRACE_REG_PERF_DEL:
ceec0b6f 350 return 0;
a1d0ce82
SR
351#endif
352 }
353 return 0;
354}
9023c930 355EXPORT_SYMBOL_GPL(trace_event_reg);
a1d0ce82 356
e870e9a1
LZ
357void trace_event_enable_cmd_record(bool enable)
358{
7f1d2f82 359 struct trace_event_file *file;
ae63b31e 360 struct trace_array *tr;
e870e9a1
LZ
361
362 mutex_lock(&event_mutex);
ae63b31e
SR
363 do_for_each_event_file(tr, file) {
364
5d6ad960 365 if (!(file->flags & EVENT_FILE_FL_ENABLED))
e870e9a1
LZ
366 continue;
367
368 if (enable) {
369 tracing_start_cmdline_record();
5d6ad960 370 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1
LZ
371 } else {
372 tracing_stop_cmdline_record();
5d6ad960 373 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 374 }
ae63b31e 375 } while_for_each_event_file();
e870e9a1
LZ
376 mutex_unlock(&event_mutex);
377}
378
7f1d2f82 379static int __ftrace_event_enable_disable(struct trace_event_file *file,
417944c4 380 int enable, int soft_disable)
fd994989 381{
2425bcb9 382 struct trace_event_call *call = file->event_call;
983f938a 383 struct trace_array *tr = file->tr;
0fc1b09f 384 unsigned long file_flags = file->flags;
3b8e4273 385 int ret = 0;
417944c4 386 int disable;
3b8e4273 387
fd994989
SR
388 switch (enable) {
389 case 0:
417944c4 390 /*
1cf4c073
MH
391 * When soft_disable is set and enable is cleared, the sm_ref
392 * reference counter is decremented. If it reaches 0, we want
417944c4
SRRH
393 * to clear the SOFT_DISABLED flag but leave the event in the
394 * state that it was. That is, if the event was enabled and
395 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
396 * is set we do not want the event to be enabled before we
397 * clear the bit.
398 *
399 * When soft_disable is not set but the SOFT_MODE flag is,
400 * we do nothing. Do not disable the tracepoint, otherwise
401 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
402 */
403 if (soft_disable) {
1cf4c073
MH
404 if (atomic_dec_return(&file->sm_ref) > 0)
405 break;
5d6ad960
SRRH
406 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
407 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
417944c4 408 } else
5d6ad960 409 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
417944c4 410
5d6ad960
SRRH
411 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
412 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
413 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
e870e9a1 414 tracing_stop_cmdline_record();
5d6ad960 415 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 416 }
ae63b31e 417 call->class->reg(call, TRACE_REG_UNREGISTER, file);
fd994989 418 }
3baa5e4c 419 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
5d6ad960
SRRH
420 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
421 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
3baa5e4c 422 else
5d6ad960 423 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
fd994989
SR
424 break;
425 case 1:
417944c4
SRRH
426 /*
427 * When soft_disable is set and enable is set, we want to
428 * register the tracepoint for the event, but leave the event
429 * as is. That means, if the event was already enabled, we do
430 * nothing (but set SOFT_MODE). If the event is disabled, we
431 * set SOFT_DISABLED before enabling the event tracepoint, so
432 * it still seems to be disabled.
433 */
434 if (!soft_disable)
5d6ad960 435 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
1cf4c073
MH
436 else {
437 if (atomic_inc_return(&file->sm_ref) > 1)
438 break;
5d6ad960 439 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
1cf4c073 440 }
417944c4 441
5d6ad960 442 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
417944c4
SRRH
443
444 /* Keep the event disabled, when going to SOFT_MODE. */
445 if (soft_disable)
5d6ad960 446 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
417944c4 447
983f938a 448 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
e870e9a1 449 tracing_start_cmdline_record();
5d6ad960 450 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 451 }
ae63b31e 452 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
3b8e4273
LZ
453 if (ret) {
454 tracing_stop_cmdline_record();
455 pr_info("event trace: Could not enable event "
687fcc4a 456 "%s\n", trace_event_name(call));
3b8e4273
LZ
457 break;
458 }
5d6ad960 459 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
575380da
SRRH
460
461 /* WAS_ENABLED gets set but never cleared. */
462 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
fd994989 463 }
fd994989
SR
464 break;
465 }
3b8e4273 466
0fc1b09f
SRRH
467 /* Enable or disable use of trace_buffered_event */
468 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
469 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
470 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
471 trace_buffered_event_enable();
472 else
473 trace_buffered_event_disable();
474 }
475
3b8e4273 476 return ret;
fd994989
SR
477}
478
7f1d2f82 479int trace_event_enable_disable(struct trace_event_file *file,
85f2b082
TZ
480 int enable, int soft_disable)
481{
482 return __ftrace_event_enable_disable(file, enable, soft_disable);
483}
484
7f1d2f82 485static int ftrace_event_enable_disable(struct trace_event_file *file,
417944c4
SRRH
486 int enable)
487{
488 return __ftrace_event_enable_disable(file, enable, 0);
489}
490
ae63b31e 491static void ftrace_clear_events(struct trace_array *tr)
0e907c99 492{
7f1d2f82 493 struct trace_event_file *file;
0e907c99
Z
494
495 mutex_lock(&event_mutex);
ae63b31e
SR
496 list_for_each_entry(file, &tr->events, list) {
497 ftrace_event_enable_disable(file, 0);
0e907c99
Z
498 }
499 mutex_unlock(&event_mutex);
500}
501
f4d34a87
SR
502/* Shouldn't this be in a header? */
503extern int pid_max;
49090107 504
c37775d5
SR
505static void
506event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
507{
508 struct trace_pid_list *pid_list;
509 struct trace_array *tr = data;
510
511 pid_list = rcu_dereference_sched(tr->filtered_pids);
4e267db1 512 trace_filter_add_remove_task(pid_list, NULL, task);
c37775d5
SR
513}
514
515static void
516event_filter_pid_sched_process_fork(void *data,
517 struct task_struct *self,
518 struct task_struct *task)
519{
520 struct trace_pid_list *pid_list;
521 struct trace_array *tr = data;
522
523 pid_list = rcu_dereference_sched(tr->filtered_pids);
4e267db1 524 trace_filter_add_remove_task(pid_list, self, task);
c37775d5
SR
525}
526
527void trace_event_follow_fork(struct trace_array *tr, bool enable)
528{
529 if (enable) {
530 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
531 tr, INT_MIN);
532 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit,
533 tr, INT_MAX);
534 } else {
535 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
536 tr);
537 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit,
538 tr);
539 }
3fdaf80f
SRRH
540}
541
542static void
22402cd0 543event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
3fdaf80f
SRRH
544 struct task_struct *prev, struct task_struct *next)
545{
546 struct trace_array *tr = data;
547 struct trace_pid_list *pid_list;
548
549 pid_list = rcu_dereference_sched(tr->filtered_pids);
550
551 this_cpu_write(tr->trace_buffer.data->ignore_pid,
4e267db1
SR
552 trace_ignore_this_task(pid_list, prev) &&
553 trace_ignore_this_task(pid_list, next));
3fdaf80f
SRRH
554}
555
556static void
22402cd0 557event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
3fdaf80f
SRRH
558 struct task_struct *prev, struct task_struct *next)
559{
560 struct trace_array *tr = data;
561 struct trace_pid_list *pid_list;
562
563 pid_list = rcu_dereference_sched(tr->filtered_pids);
564
565 this_cpu_write(tr->trace_buffer.data->ignore_pid,
4e267db1 566 trace_ignore_this_task(pid_list, next));
3fdaf80f
SRRH
567}
568
569static void
570event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
571{
572 struct trace_array *tr = data;
573 struct trace_pid_list *pid_list;
574
575 /* Nothing to do if we are already tracing */
576 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
577 return;
578
579 pid_list = rcu_dereference_sched(tr->filtered_pids);
580
581 this_cpu_write(tr->trace_buffer.data->ignore_pid,
4e267db1 582 trace_ignore_this_task(pid_list, task));
3fdaf80f
SRRH
583}
584
585static void
586event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
587{
588 struct trace_array *tr = data;
589 struct trace_pid_list *pid_list;
590
591 /* Nothing to do if we are not tracing */
592 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
593 return;
594
595 pid_list = rcu_dereference_sched(tr->filtered_pids);
596
597 /* Set tracing if current is enabled */
598 this_cpu_write(tr->trace_buffer.data->ignore_pid,
4e267db1 599 trace_ignore_this_task(pid_list, current));
3fdaf80f
SRRH
600}
601
49090107
SRRH
602static void __ftrace_clear_event_pids(struct trace_array *tr)
603{
604 struct trace_pid_list *pid_list;
3fdaf80f
SRRH
605 struct trace_event_file *file;
606 int cpu;
49090107
SRRH
607
608 pid_list = rcu_dereference_protected(tr->filtered_pids,
609 lockdep_is_held(&event_mutex));
610 if (!pid_list)
611 return;
612
3fdaf80f
SRRH
613 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
614 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
615
616 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
617 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
618
0f72e37e
SRRH
619 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
620 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
621
622 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
623 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
624
3fdaf80f
SRRH
625 list_for_each_entry(file, &tr->events, list) {
626 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
627 }
628
629 for_each_possible_cpu(cpu)
630 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
631
49090107
SRRH
632 rcu_assign_pointer(tr->filtered_pids, NULL);
633
634 /* Wait till all users are no longer using pid filtering */
635 synchronize_sched();
636
f4d34a87 637 vfree(pid_list->pids);
49090107
SRRH
638 kfree(pid_list);
639}
640
641static void ftrace_clear_event_pids(struct trace_array *tr)
642{
643 mutex_lock(&event_mutex);
644 __ftrace_clear_event_pids(tr);
645 mutex_unlock(&event_mutex);
646}
647
e9dbfae5
SR
648static void __put_system(struct event_subsystem *system)
649{
650 struct event_filter *filter = system->filter;
651
6e94a780
SR
652 WARN_ON_ONCE(system_refcount(system) == 0);
653 if (system_refcount_dec(system))
e9dbfae5
SR
654 return;
655
ae63b31e
SR
656 list_del(&system->list);
657
e9dbfae5
SR
658 if (filter) {
659 kfree(filter->filter_string);
660 kfree(filter);
661 }
79ac6ef5 662 kfree_const(system->name);
e9dbfae5
SR
663 kfree(system);
664}
665
666static void __get_system(struct event_subsystem *system)
667{
6e94a780
SR
668 WARN_ON_ONCE(system_refcount(system) == 0);
669 system_refcount_inc(system);
e9dbfae5
SR
670}
671
7967b3e0 672static void __get_system_dir(struct trace_subsystem_dir *dir)
ae63b31e
SR
673{
674 WARN_ON_ONCE(dir->ref_count == 0);
675 dir->ref_count++;
676 __get_system(dir->subsystem);
677}
678
7967b3e0 679static void __put_system_dir(struct trace_subsystem_dir *dir)
ae63b31e
SR
680{
681 WARN_ON_ONCE(dir->ref_count == 0);
682 /* If the subsystem is about to be freed, the dir must be too */
6e94a780 683 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
ae63b31e
SR
684
685 __put_system(dir->subsystem);
686 if (!--dir->ref_count)
687 kfree(dir);
688}
689
7967b3e0 690static void put_system(struct trace_subsystem_dir *dir)
e9dbfae5
SR
691{
692 mutex_lock(&event_mutex);
ae63b31e 693 __put_system_dir(dir);
e9dbfae5
SR
694 mutex_unlock(&event_mutex);
695}
696
7967b3e0 697static void remove_subsystem(struct trace_subsystem_dir *dir)
f6a84bdc
ON
698{
699 if (!dir)
700 return;
701
702 if (!--dir->nr_events) {
8434dc93 703 tracefs_remove_recursive(dir->entry);
f6a84bdc
ON
704 list_del(&dir->list);
705 __put_system_dir(dir);
706 }
707}
708
7f1d2f82 709static void remove_event_file_dir(struct trace_event_file *file)
f6a84bdc 710{
bf682c31
ON
711 struct dentry *dir = file->dir;
712 struct dentry *child;
713
714 if (dir) {
715 spin_lock(&dir->d_lock); /* probably unneeded */
946e51f2 716 list_for_each_entry(child, &dir->d_subdirs, d_child) {
7682c918
DH
717 if (d_really_is_positive(child)) /* probably unneeded */
718 d_inode(child)->i_private = NULL;
bf682c31
ON
719 }
720 spin_unlock(&dir->d_lock);
721
8434dc93 722 tracefs_remove_recursive(dir);
bf682c31
ON
723 }
724
f6a84bdc 725 list_del(&file->list);
f6a84bdc 726 remove_subsystem(file->system);
2448e349 727 free_event_filter(file->filter);
f6a84bdc
ON
728 kmem_cache_free(file_cachep, file);
729}
730
8f31bfe5
LZ
731/*
732 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
733 */
2a6c24af
SRRH
734static int
735__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
736 const char *sub, const char *event, int set)
b77e38aa 737{
7f1d2f82 738 struct trace_event_file *file;
2425bcb9 739 struct trace_event_call *call;
de7b2973 740 const char *name;
29f93943 741 int ret = -EINVAL;
8f31bfe5 742
ae63b31e
SR
743 list_for_each_entry(file, &tr->events, list) {
744
745 call = file->event_call;
687fcc4a 746 name = trace_event_name(call);
8f31bfe5 747
de7b2973 748 if (!name || !call->class || !call->class->reg)
8f31bfe5
LZ
749 continue;
750
9b63776f
SR
751 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
752 continue;
753
8f31bfe5 754 if (match &&
de7b2973 755 strcmp(match, name) != 0 &&
8f082018 756 strcmp(match, call->class->system) != 0)
8f31bfe5
LZ
757 continue;
758
8f082018 759 if (sub && strcmp(sub, call->class->system) != 0)
8f31bfe5
LZ
760 continue;
761
de7b2973 762 if (event && strcmp(event, name) != 0)
8f31bfe5
LZ
763 continue;
764
ae63b31e 765 ftrace_event_enable_disable(file, set);
8f31bfe5
LZ
766
767 ret = 0;
768 }
2a6c24af
SRRH
769
770 return ret;
771}
772
773static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
774 const char *sub, const char *event, int set)
775{
776 int ret;
777
778 mutex_lock(&event_mutex);
779 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
8f31bfe5
LZ
780 mutex_unlock(&event_mutex);
781
782 return ret;
783}
784
ae63b31e 785static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
8f31bfe5 786{
b628b3e6 787 char *event = NULL, *sub = NULL, *match;
84fce9db 788 int ret;
b628b3e6
SR
789
790 /*
791 * The buf format can be <subsystem>:<event-name>
792 * *:<event-name> means any event by that name.
793 * :<event-name> is the same.
794 *
795 * <subsystem>:* means all events in that subsystem
796 * <subsystem>: means the same.
797 *
798 * <name> (no ':') means all events in a subsystem with
799 * the name <name> or any event that matches <name>
800 */
801
802 match = strsep(&buf, ":");
803 if (buf) {
804 sub = match;
805 event = buf;
806 match = NULL;
807
808 if (!strlen(sub) || strcmp(sub, "*") == 0)
809 sub = NULL;
810 if (!strlen(event) || strcmp(event, "*") == 0)
811 event = NULL;
812 }
b77e38aa 813
84fce9db
JK
814 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
815
816 /* Put back the colon to allow this to be called again */
817 if (buf)
818 *(buf - 1) = ':';
819
820 return ret;
b77e38aa
SR
821}
822
4671c794
SR
823/**
824 * trace_set_clr_event - enable or disable an event
825 * @system: system name to match (NULL for any system)
826 * @event: event name to match (NULL for all events, within system)
827 * @set: 1 to enable, 0 to disable
828 *
829 * This is a way for other parts of the kernel to enable or disable
830 * event recording.
831 *
832 * Returns 0 on success, -EINVAL if the parameters do not match any
833 * registered events.
834 */
835int trace_set_clr_event(const char *system, const char *event, int set)
836{
ae63b31e
SR
837 struct trace_array *tr = top_trace_array();
838
dc81e5e3
YY
839 if (!tr)
840 return -ENODEV;
841
ae63b31e 842 return __ftrace_set_clr_event(tr, NULL, system, event, set);
4671c794 843}
56355b83 844EXPORT_SYMBOL_GPL(trace_set_clr_event);
4671c794 845
b77e38aa
SR
846/* 128 should be much more than enough */
847#define EVENT_BUF_SIZE 127
848
849static ssize_t
850ftrace_event_write(struct file *file, const char __user *ubuf,
851 size_t cnt, loff_t *ppos)
852{
48966364 853 struct trace_parser parser;
ae63b31e
SR
854 struct seq_file *m = file->private_data;
855 struct trace_array *tr = m->private;
4ba7978e 856 ssize_t read, ret;
b77e38aa 857
4ba7978e 858 if (!cnt)
b77e38aa
SR
859 return 0;
860
1852fcce
SR
861 ret = tracing_update_buffers();
862 if (ret < 0)
863 return ret;
864
48966364 865 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
b77e38aa
SR
866 return -ENOMEM;
867
48966364 868 read = trace_get_user(&parser, ubuf, cnt, ppos);
869
4ba7978e 870 if (read >= 0 && trace_parser_loaded((&parser))) {
48966364 871 int set = 1;
b77e38aa 872
48966364 873 if (*parser.buffer == '!')
b77e38aa 874 set = 0;
b77e38aa 875
48966364 876 parser.buffer[parser.idx] = 0;
877
ae63b31e 878 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
b77e38aa 879 if (ret)
48966364 880 goto out_put;
b77e38aa 881 }
b77e38aa
SR
882
883 ret = read;
884
48966364 885 out_put:
886 trace_parser_put(&parser);
b77e38aa
SR
887
888 return ret;
889}
890
891static void *
892t_next(struct seq_file *m, void *v, loff_t *pos)
893{
7f1d2f82 894 struct trace_event_file *file = v;
2425bcb9 895 struct trace_event_call *call;
ae63b31e 896 struct trace_array *tr = m->private;
b77e38aa
SR
897
898 (*pos)++;
899
ae63b31e
SR
900 list_for_each_entry_continue(file, &tr->events, list) {
901 call = file->event_call;
40e26815
SR
902 /*
903 * The ftrace subsystem is for showing formats only.
904 * They can not be enabled or disabled via the event files.
905 */
d045437a
SRRH
906 if (call->class && call->class->reg &&
907 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
ae63b31e 908 return file;
40e26815 909 }
b77e38aa 910
30bd39cd 911 return NULL;
b77e38aa
SR
912}
913
914static void *t_start(struct seq_file *m, loff_t *pos)
915{
7f1d2f82 916 struct trace_event_file *file;
ae63b31e 917 struct trace_array *tr = m->private;
e1c7e2a6
LZ
918 loff_t l;
919
20c8928a 920 mutex_lock(&event_mutex);
e1c7e2a6 921
7f1d2f82 922 file = list_entry(&tr->events, struct trace_event_file, list);
e1c7e2a6 923 for (l = 0; l <= *pos; ) {
ae63b31e
SR
924 file = t_next(m, file, &l);
925 if (!file)
e1c7e2a6
LZ
926 break;
927 }
ae63b31e 928 return file;
b77e38aa
SR
929}
930
931static void *
932s_next(struct seq_file *m, void *v, loff_t *pos)
933{
7f1d2f82 934 struct trace_event_file *file = v;
ae63b31e 935 struct trace_array *tr = m->private;
b77e38aa
SR
936
937 (*pos)++;
938
ae63b31e 939 list_for_each_entry_continue(file, &tr->events, list) {
5d6ad960 940 if (file->flags & EVENT_FILE_FL_ENABLED)
ae63b31e 941 return file;
b77e38aa
SR
942 }
943
30bd39cd 944 return NULL;
b77e38aa
SR
945}
946
947static void *s_start(struct seq_file *m, loff_t *pos)
948{
7f1d2f82 949 struct trace_event_file *file;
ae63b31e 950 struct trace_array *tr = m->private;
e1c7e2a6
LZ
951 loff_t l;
952
20c8928a 953 mutex_lock(&event_mutex);
e1c7e2a6 954
7f1d2f82 955 file = list_entry(&tr->events, struct trace_event_file, list);
e1c7e2a6 956 for (l = 0; l <= *pos; ) {
ae63b31e
SR
957 file = s_next(m, file, &l);
958 if (!file)
e1c7e2a6
LZ
959 break;
960 }
ae63b31e 961 return file;
b77e38aa
SR
962}
963
964static int t_show(struct seq_file *m, void *v)
965{
7f1d2f82 966 struct trace_event_file *file = v;
2425bcb9 967 struct trace_event_call *call = file->event_call;
b77e38aa 968
8f082018
SR
969 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
970 seq_printf(m, "%s:", call->class->system);
687fcc4a 971 seq_printf(m, "%s\n", trace_event_name(call));
b77e38aa
SR
972
973 return 0;
974}
975
976static void t_stop(struct seq_file *m, void *p)
977{
20c8928a 978 mutex_unlock(&event_mutex);
b77e38aa
SR
979}
980
f4d34a87
SR
981static void *
982p_next(struct seq_file *m, void *v, loff_t *pos)
983{
984 struct trace_array *tr = m->private;
985 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
f4d34a87 986
5cc8976b 987 return trace_pid_next(pid_list, v, pos);
f4d34a87
SR
988}
989
49090107 990static void *p_start(struct seq_file *m, loff_t *pos)
fb662288 991 __acquires(RCU)
49090107
SRRH
992{
993 struct trace_pid_list *pid_list;
994 struct trace_array *tr = m->private;
995
996 /*
997 * Grab the mutex, to keep calls to p_next() having the same
998 * tr->filtered_pids as p_start() has.
999 * If we just passed the tr->filtered_pids around, then RCU would
1000 * have been enough, but doing that makes things more complex.
1001 */
1002 mutex_lock(&event_mutex);
1003 rcu_read_lock_sched();
1004
1005 pid_list = rcu_dereference_sched(tr->filtered_pids);
1006
f4d34a87 1007 if (!pid_list)
49090107
SRRH
1008 return NULL;
1009
5cc8976b 1010 return trace_pid_start(pid_list, pos);
49090107
SRRH
1011}
1012
1013static void p_stop(struct seq_file *m, void *p)
fb662288 1014 __releases(RCU)
49090107
SRRH
1015{
1016 rcu_read_unlock_sched();
1017 mutex_unlock(&event_mutex);
1018}
1019
1473e441
SR
1020static ssize_t
1021event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1022 loff_t *ppos)
1023{
7f1d2f82 1024 struct trace_event_file *file;
bc6f6b08 1025 unsigned long flags;
a4390596
TZ
1026 char buf[4] = "0";
1027
bc6f6b08
ON
1028 mutex_lock(&event_mutex);
1029 file = event_file_data(filp);
1030 if (likely(file))
1031 flags = file->flags;
1032 mutex_unlock(&event_mutex);
1033
1034 if (!file)
1035 return -ENODEV;
1036
5d6ad960
SRRH
1037 if (flags & EVENT_FILE_FL_ENABLED &&
1038 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
a4390596
TZ
1039 strcpy(buf, "1");
1040
5d6ad960
SRRH
1041 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1042 flags & EVENT_FILE_FL_SOFT_MODE)
a4390596
TZ
1043 strcat(buf, "*");
1044
1045 strcat(buf, "\n");
1473e441 1046
417944c4 1047 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1473e441
SR
1048}
1049
1050static ssize_t
1051event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1052 loff_t *ppos)
1053{
7f1d2f82 1054 struct trace_event_file *file;
1473e441
SR
1055 unsigned long val;
1056 int ret;
1057
22fe9b54
PH
1058 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1059 if (ret)
1473e441
SR
1060 return ret;
1061
1852fcce
SR
1062 ret = tracing_update_buffers();
1063 if (ret < 0)
1064 return ret;
1065
1473e441
SR
1066 switch (val) {
1067 case 0:
1473e441 1068 case 1:
bc6f6b08 1069 ret = -ENODEV;
11a241a3 1070 mutex_lock(&event_mutex);
bc6f6b08
ON
1071 file = event_file_data(filp);
1072 if (likely(file))
1073 ret = ftrace_event_enable_disable(file, val);
11a241a3 1074 mutex_unlock(&event_mutex);
1473e441
SR
1075 break;
1076
1077 default:
1078 return -EINVAL;
1079 }
1080
1081 *ppos += cnt;
1082
3b8e4273 1083 return ret ? ret : cnt;
1473e441
SR
1084}
1085
8ae79a13
SR
1086static ssize_t
1087system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1088 loff_t *ppos)
1089{
c142b15d 1090 const char set_to_char[4] = { '?', '0', '1', 'X' };
7967b3e0 1091 struct trace_subsystem_dir *dir = filp->private_data;
ae63b31e 1092 struct event_subsystem *system = dir->subsystem;
2425bcb9 1093 struct trace_event_call *call;
7f1d2f82 1094 struct trace_event_file *file;
ae63b31e 1095 struct trace_array *tr = dir->tr;
8ae79a13 1096 char buf[2];
c142b15d 1097 int set = 0;
8ae79a13
SR
1098 int ret;
1099
8ae79a13 1100 mutex_lock(&event_mutex);
ae63b31e
SR
1101 list_for_each_entry(file, &tr->events, list) {
1102 call = file->event_call;
687fcc4a 1103 if (!trace_event_name(call) || !call->class || !call->class->reg)
8ae79a13
SR
1104 continue;
1105
40ee4dff 1106 if (system && strcmp(call->class->system, system->name) != 0)
8ae79a13
SR
1107 continue;
1108
1109 /*
1110 * We need to find out if all the events are set
1111 * or if all events or cleared, or if we have
1112 * a mixture.
1113 */
5d6ad960 1114 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
c142b15d 1115
8ae79a13
SR
1116 /*
1117 * If we have a mixture, no need to look further.
1118 */
c142b15d 1119 if (set == 3)
8ae79a13
SR
1120 break;
1121 }
1122 mutex_unlock(&event_mutex);
1123
c142b15d 1124 buf[0] = set_to_char[set];
8ae79a13 1125 buf[1] = '\n';
8ae79a13
SR
1126
1127 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1128
1129 return ret;
1130}
1131
1132static ssize_t
1133system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1134 loff_t *ppos)
1135{
7967b3e0 1136 struct trace_subsystem_dir *dir = filp->private_data;
ae63b31e 1137 struct event_subsystem *system = dir->subsystem;
40ee4dff 1138 const char *name = NULL;
8ae79a13 1139 unsigned long val;
8ae79a13
SR
1140 ssize_t ret;
1141
22fe9b54
PH
1142 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1143 if (ret)
8ae79a13
SR
1144 return ret;
1145
1146 ret = tracing_update_buffers();
1147 if (ret < 0)
1148 return ret;
1149
8f31bfe5 1150 if (val != 0 && val != 1)
8ae79a13 1151 return -EINVAL;
8ae79a13 1152
40ee4dff
SR
1153 /*
1154 * Opening of "enable" adds a ref count to system,
1155 * so the name is safe to use.
1156 */
1157 if (system)
1158 name = system->name;
1159
ae63b31e 1160 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
8ae79a13 1161 if (ret)
8f31bfe5 1162 goto out;
8ae79a13
SR
1163
1164 ret = cnt;
1165
8f31bfe5 1166out:
8ae79a13
SR
1167 *ppos += cnt;
1168
1169 return ret;
1170}
1171
2a37a3df
SR
1172enum {
1173 FORMAT_HEADER = 1,
86397dc3
LZ
1174 FORMAT_FIELD_SEPERATOR = 2,
1175 FORMAT_PRINTFMT = 3,
2a37a3df
SR
1176};
1177
1178static void *f_next(struct seq_file *m, void *v, loff_t *pos)
981d081e 1179{
2425bcb9 1180 struct trace_event_call *call = event_file_data(m->private);
86397dc3
LZ
1181 struct list_head *common_head = &ftrace_common_fields;
1182 struct list_head *head = trace_get_fields(call);
7710b639 1183 struct list_head *node = v;
981d081e 1184
2a37a3df 1185 (*pos)++;
5a65e956 1186
2a37a3df
SR
1187 switch ((unsigned long)v) {
1188 case FORMAT_HEADER:
7710b639
ON
1189 node = common_head;
1190 break;
5a65e956 1191
86397dc3 1192 case FORMAT_FIELD_SEPERATOR:
7710b639
ON
1193 node = head;
1194 break;
5a65e956 1195
2a37a3df
SR
1196 case FORMAT_PRINTFMT:
1197 /* all done */
1198 return NULL;
5a65e956
LJ
1199 }
1200
7710b639
ON
1201 node = node->prev;
1202 if (node == common_head)
86397dc3 1203 return (void *)FORMAT_FIELD_SEPERATOR;
7710b639 1204 else if (node == head)
2a37a3df 1205 return (void *)FORMAT_PRINTFMT;
7710b639
ON
1206 else
1207 return node;
2a37a3df
SR
1208}
1209
1210static int f_show(struct seq_file *m, void *v)
1211{
2425bcb9 1212 struct trace_event_call *call = event_file_data(m->private);
2a37a3df
SR
1213 struct ftrace_event_field *field;
1214 const char *array_descriptor;
1215
1216 switch ((unsigned long)v) {
1217 case FORMAT_HEADER:
687fcc4a 1218 seq_printf(m, "name: %s\n", trace_event_name(call));
2a37a3df 1219 seq_printf(m, "ID: %d\n", call->event.type);
fa6f0cc7 1220 seq_puts(m, "format:\n");
8728fe50 1221 return 0;
5a65e956 1222
86397dc3
LZ
1223 case FORMAT_FIELD_SEPERATOR:
1224 seq_putc(m, '\n');
1225 return 0;
1226
2a37a3df
SR
1227 case FORMAT_PRINTFMT:
1228 seq_printf(m, "\nprint fmt: %s\n",
1229 call->print_fmt);
1230 return 0;
981d081e 1231 }
8728fe50 1232
7710b639 1233 field = list_entry(v, struct ftrace_event_field, link);
2a37a3df
SR
1234 /*
1235 * Smartly shows the array type(except dynamic array).
1236 * Normal:
1237 * field:TYPE VAR
1238 * If TYPE := TYPE[LEN], it is shown:
1239 * field:TYPE VAR[LEN]
1240 */
1241 array_descriptor = strchr(field->type, '[');
8728fe50 1242
2a37a3df
SR
1243 if (!strncmp(field->type, "__data_loc", 10))
1244 array_descriptor = NULL;
8728fe50 1245
2a37a3df
SR
1246 if (!array_descriptor)
1247 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1248 field->type, field->name, field->offset,
1249 field->size, !!field->is_signed);
1250 else
1251 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1252 (int)(array_descriptor - field->type),
1253 field->type, field->name,
1254 array_descriptor, field->offset,
1255 field->size, !!field->is_signed);
8728fe50 1256
2a37a3df
SR
1257 return 0;
1258}
5a65e956 1259
7710b639
ON
1260static void *f_start(struct seq_file *m, loff_t *pos)
1261{
1262 void *p = (void *)FORMAT_HEADER;
1263 loff_t l = 0;
1264
c5a44a12
ON
1265 /* ->stop() is called even if ->start() fails */
1266 mutex_lock(&event_mutex);
1267 if (!event_file_data(m->private))
1268 return ERR_PTR(-ENODEV);
1269
7710b639
ON
1270 while (l < *pos && p)
1271 p = f_next(m, p, &l);
1272
1273 return p;
1274}
1275
2a37a3df
SR
1276static void f_stop(struct seq_file *m, void *p)
1277{
c5a44a12 1278 mutex_unlock(&event_mutex);
2a37a3df 1279}
981d081e 1280
2a37a3df
SR
1281static const struct seq_operations trace_format_seq_ops = {
1282 .start = f_start,
1283 .next = f_next,
1284 .stop = f_stop,
1285 .show = f_show,
1286};
1287
1288static int trace_format_open(struct inode *inode, struct file *file)
1289{
2a37a3df
SR
1290 struct seq_file *m;
1291 int ret;
1292
1293 ret = seq_open(file, &trace_format_seq_ops);
1294 if (ret < 0)
1295 return ret;
1296
1297 m = file->private_data;
c5a44a12 1298 m->private = file;
2a37a3df
SR
1299
1300 return 0;
981d081e
SR
1301}
1302
23725aee
PZ
1303static ssize_t
1304event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1305{
1a11126b 1306 int id = (long)event_file_data(filp);
cd458ba9
ON
1307 char buf[32];
1308 int len;
23725aee
PZ
1309
1310 if (*ppos)
1311 return 0;
1312
1a11126b
ON
1313 if (unlikely(!id))
1314 return -ENODEV;
1315
1316 len = sprintf(buf, "%d\n", id);
1317
cd458ba9 1318 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
23725aee
PZ
1319}
1320
7ce7e424
TZ
1321static ssize_t
1322event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1323 loff_t *ppos)
1324{
7f1d2f82 1325 struct trace_event_file *file;
7ce7e424 1326 struct trace_seq *s;
e2912b09 1327 int r = -ENODEV;
7ce7e424
TZ
1328
1329 if (*ppos)
1330 return 0;
1331
1332 s = kmalloc(sizeof(*s), GFP_KERNEL);
e2912b09 1333
7ce7e424
TZ
1334 if (!s)
1335 return -ENOMEM;
1336
1337 trace_seq_init(s);
1338
e2912b09 1339 mutex_lock(&event_mutex);
f306cc82
TZ
1340 file = event_file_data(filp);
1341 if (file)
1342 print_event_filter(file, s);
e2912b09
ON
1343 mutex_unlock(&event_mutex);
1344
f306cc82 1345 if (file)
5ac48378
SRRH
1346 r = simple_read_from_buffer(ubuf, cnt, ppos,
1347 s->buffer, trace_seq_used(s));
7ce7e424
TZ
1348
1349 kfree(s);
1350
1351 return r;
1352}
1353
1354static ssize_t
1355event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1356 loff_t *ppos)
1357{
7f1d2f82 1358 struct trace_event_file *file;
8b372562 1359 char *buf;
e2912b09 1360 int err = -ENODEV;
7ce7e424 1361
8b372562 1362 if (cnt >= PAGE_SIZE)
7ce7e424
TZ
1363 return -EINVAL;
1364
70f6cbb6
AV
1365 buf = memdup_user_nul(ubuf, cnt);
1366 if (IS_ERR(buf))
1367 return PTR_ERR(buf);
7ce7e424 1368
e2912b09 1369 mutex_lock(&event_mutex);
f306cc82
TZ
1370 file = event_file_data(filp);
1371 if (file)
1372 err = apply_event_filter(file, buf);
e2912b09
ON
1373 mutex_unlock(&event_mutex);
1374
70f6cbb6 1375 kfree(buf);
8b372562 1376 if (err < 0)
44e9c8b7 1377 return err;
0a19e53c 1378
7ce7e424
TZ
1379 *ppos += cnt;
1380
1381 return cnt;
1382}
1383
e9dbfae5
SR
1384static LIST_HEAD(event_subsystems);
1385
1386static int subsystem_open(struct inode *inode, struct file *filp)
1387{
1388 struct event_subsystem *system = NULL;
7967b3e0 1389 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
ae63b31e 1390 struct trace_array *tr;
e9dbfae5
SR
1391 int ret;
1392
d6d3523c
GB
1393 if (tracing_is_disabled())
1394 return -ENODEV;
1395
e9dbfae5 1396 /* Make sure the system still exists */
a8227415 1397 mutex_lock(&trace_types_lock);
e9dbfae5 1398 mutex_lock(&event_mutex);
ae63b31e
SR
1399 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1400 list_for_each_entry(dir, &tr->systems, list) {
1401 if (dir == inode->i_private) {
1402 /* Don't open systems with no events */
1403 if (dir->nr_events) {
1404 __get_system_dir(dir);
1405 system = dir->subsystem;
1406 }
1407 goto exit_loop;
e9dbfae5 1408 }
e9dbfae5
SR
1409 }
1410 }
ae63b31e 1411 exit_loop:
e9dbfae5 1412 mutex_unlock(&event_mutex);
a8227415 1413 mutex_unlock(&trace_types_lock);
e9dbfae5 1414
ae63b31e 1415 if (!system)
e9dbfae5
SR
1416 return -ENODEV;
1417
ae63b31e
SR
1418 /* Some versions of gcc think dir can be uninitialized here */
1419 WARN_ON(!dir);
1420
8e2e2fa4
SRRH
1421 /* Still need to increment the ref count of the system */
1422 if (trace_array_get(tr) < 0) {
1423 put_system(dir);
1424 return -ENODEV;
1425 }
1426
e9dbfae5 1427 ret = tracing_open_generic(inode, filp);
8e2e2fa4
SRRH
1428 if (ret < 0) {
1429 trace_array_put(tr);
ae63b31e 1430 put_system(dir);
8e2e2fa4 1431 }
ae63b31e
SR
1432
1433 return ret;
1434}
1435
1436static int system_tr_open(struct inode *inode, struct file *filp)
1437{
7967b3e0 1438 struct trace_subsystem_dir *dir;
ae63b31e
SR
1439 struct trace_array *tr = inode->i_private;
1440 int ret;
1441
d6d3523c
GB
1442 if (tracing_is_disabled())
1443 return -ENODEV;
1444
8e2e2fa4
SRRH
1445 if (trace_array_get(tr) < 0)
1446 return -ENODEV;
1447
ae63b31e
SR
1448 /* Make a temporary dir that has no system but points to tr */
1449 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
8e2e2fa4
SRRH
1450 if (!dir) {
1451 trace_array_put(tr);
ae63b31e 1452 return -ENOMEM;
8e2e2fa4 1453 }
ae63b31e
SR
1454
1455 dir->tr = tr;
1456
1457 ret = tracing_open_generic(inode, filp);
8e2e2fa4
SRRH
1458 if (ret < 0) {
1459 trace_array_put(tr);
ae63b31e 1460 kfree(dir);
d6d3523c 1461 return ret;
8e2e2fa4 1462 }
ae63b31e
SR
1463
1464 filp->private_data = dir;
e9dbfae5 1465
d6d3523c 1466 return 0;
e9dbfae5
SR
1467}
1468
1469static int subsystem_release(struct inode *inode, struct file *file)
1470{
7967b3e0 1471 struct trace_subsystem_dir *dir = file->private_data;
e9dbfae5 1472
8e2e2fa4
SRRH
1473 trace_array_put(dir->tr);
1474
ae63b31e
SR
1475 /*
1476 * If dir->subsystem is NULL, then this is a temporary
1477 * descriptor that was made for a trace_array to enable
1478 * all subsystems.
1479 */
1480 if (dir->subsystem)
1481 put_system(dir);
1482 else
1483 kfree(dir);
e9dbfae5
SR
1484
1485 return 0;
1486}
1487
cfb180f3
TZ
1488static ssize_t
1489subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1490 loff_t *ppos)
1491{
7967b3e0 1492 struct trace_subsystem_dir *dir = filp->private_data;
ae63b31e 1493 struct event_subsystem *system = dir->subsystem;
cfb180f3
TZ
1494 struct trace_seq *s;
1495 int r;
1496
1497 if (*ppos)
1498 return 0;
1499
1500 s = kmalloc(sizeof(*s), GFP_KERNEL);
1501 if (!s)
1502 return -ENOMEM;
1503
1504 trace_seq_init(s);
1505
8b372562 1506 print_subsystem_event_filter(system, s);
5ac48378
SRRH
1507 r = simple_read_from_buffer(ubuf, cnt, ppos,
1508 s->buffer, trace_seq_used(s));
cfb180f3
TZ
1509
1510 kfree(s);
1511
1512 return r;
1513}
1514
1515static ssize_t
1516subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1517 loff_t *ppos)
1518{
7967b3e0 1519 struct trace_subsystem_dir *dir = filp->private_data;
8b372562 1520 char *buf;
cfb180f3
TZ
1521 int err;
1522
8b372562 1523 if (cnt >= PAGE_SIZE)
cfb180f3
TZ
1524 return -EINVAL;
1525
70f6cbb6
AV
1526 buf = memdup_user_nul(ubuf, cnt);
1527 if (IS_ERR(buf))
1528 return PTR_ERR(buf);
cfb180f3 1529
ae63b31e 1530 err = apply_subsystem_event_filter(dir, buf);
70f6cbb6 1531 kfree(buf);
8b372562 1532 if (err < 0)
44e9c8b7 1533 return err;
cfb180f3
TZ
1534
1535 *ppos += cnt;
1536
1537 return cnt;
1538}
1539
d1b182a8
SR
1540static ssize_t
1541show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1542{
1543 int (*func)(struct trace_seq *s) = filp->private_data;
1544 struct trace_seq *s;
1545 int r;
1546
1547 if (*ppos)
1548 return 0;
1549
1550 s = kmalloc(sizeof(*s), GFP_KERNEL);
1551 if (!s)
1552 return -ENOMEM;
1553
1554 trace_seq_init(s);
1555
1556 func(s);
5ac48378
SRRH
1557 r = simple_read_from_buffer(ubuf, cnt, ppos,
1558 s->buffer, trace_seq_used(s));
d1b182a8
SR
1559
1560 kfree(s);
1561
1562 return r;
1563}
1564
8ca532ad
SRRH
1565static void ignore_task_cpu(void *data)
1566{
1567 struct trace_array *tr = data;
1568 struct trace_pid_list *pid_list;
1569
1570 /*
1571 * This function is called by on_each_cpu() while the
1572 * event_mutex is held.
1573 */
1574 pid_list = rcu_dereference_protected(tr->filtered_pids,
1575 mutex_is_locked(&event_mutex));
1576
1577 this_cpu_write(tr->trace_buffer.data->ignore_pid,
4e267db1 1578 trace_ignore_this_task(pid_list, current));
8ca532ad
SRRH
1579}
1580
49090107 1581static ssize_t
3fdaf80f 1582ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
49090107
SRRH
1583 size_t cnt, loff_t *ppos)
1584{
3fdaf80f 1585 struct seq_file *m = filp->private_data;
49090107
SRRH
1586 struct trace_array *tr = m->private;
1587 struct trace_pid_list *filtered_pids = NULL;
f4d34a87 1588 struct trace_pid_list *pid_list;
3fdaf80f 1589 struct trace_event_file *file;
49090107
SRRH
1590 struct trace_parser parser;
1591 unsigned long val;
1592 loff_t this_pos;
1593 ssize_t read = 0;
1594 ssize_t ret = 0;
1595 pid_t pid;
f4d34a87 1596 int nr_pids = 0;
49090107
SRRH
1597
1598 if (!cnt)
1599 return 0;
1600
1601 ret = tracing_update_buffers();
1602 if (ret < 0)
1603 return ret;
1604
1605 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1606 return -ENOMEM;
1607
1608 mutex_lock(&event_mutex);
f4d34a87
SR
1609 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1610 lockdep_is_held(&event_mutex));
1611
49090107 1612 /*
f4d34a87
SR
1613 * Always recreate a new array. The write is an all or nothing
1614 * operation. Always create a new array when adding new pids by
1615 * the user. If the operation fails, then the current list is
1616 * not modified.
49090107 1617 */
f4d34a87
SR
1618 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1619 if (!pid_list) {
1620 read = -ENOMEM;
1621 goto out;
1622 }
1623 pid_list->pid_max = READ_ONCE(pid_max);
1624 /* Only truncating will shrink pid_max */
1625 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
1626 pid_list->pid_max = filtered_pids->pid_max;
1627 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
1628 if (!pid_list->pids) {
1629 kfree(pid_list);
1630 read = -ENOMEM;
1631 goto out;
1632 }
1633 if (filtered_pids) {
1634 /* copy the current bits to the new max */
1635 pid = find_first_bit(filtered_pids->pids,
1636 filtered_pids->pid_max);
1637 while (pid < filtered_pids->pid_max) {
1638 set_bit(pid, pid_list->pids);
1639 pid = find_next_bit(filtered_pids->pids,
1640 filtered_pids->pid_max,
1641 pid + 1);
1642 nr_pids++;
1643 }
1644 }
1645
49090107
SRRH
1646 while (cnt > 0) {
1647
1648 this_pos = 0;
1649
1650 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1651 if (ret < 0 || !trace_parser_loaded(&parser))
1652 break;
1653
1654 read += ret;
1655 ubuf += ret;
1656 cnt -= ret;
1657
1658 parser.buffer[parser.idx] = 0;
1659
1660 ret = -EINVAL;
1661 if (kstrtoul(parser.buffer, 0, &val))
1662 break;
f4d34a87 1663 if (val >= pid_list->pid_max)
49090107
SRRH
1664 break;
1665
1666 pid = (pid_t)val;
1667
f4d34a87
SR
1668 set_bit(pid, pid_list->pids);
1669 nr_pids++;
49090107 1670
49090107
SRRH
1671 trace_parser_clear(&parser);
1672 ret = 0;
1673 }
1674 trace_parser_put(&parser);
1675
1676 if (ret < 0) {
f4d34a87 1677 vfree(pid_list->pids);
49090107 1678 kfree(pid_list);
f4d34a87
SR
1679 read = ret;
1680 goto out;
49090107
SRRH
1681 }
1682
f4d34a87
SR
1683 if (!nr_pids) {
1684 /* Cleared the list of pids */
1685 vfree(pid_list->pids);
1686 kfree(pid_list);
1687 read = ret;
1688 if (!filtered_pids)
1689 goto out;
1690 pid_list = NULL;
49090107 1691 }
49090107
SRRH
1692 rcu_assign_pointer(tr->filtered_pids, pid_list);
1693
3fdaf80f
SRRH
1694 list_for_each_entry(file, &tr->events, list) {
1695 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1696 }
49090107
SRRH
1697
1698 if (filtered_pids) {
1699 synchronize_sched();
1700
f4d34a87 1701 vfree(filtered_pids->pids);
49090107 1702 kfree(filtered_pids);
3fdaf80f
SRRH
1703 } else {
1704 /*
1705 * Register a probe that is called before all other probes
1706 * to set ignore_pid if next or prev do not match.
1707 * Register a probe this is called after all other probes
1708 * to only keep ignore_pid set if next pid matches.
1709 */
1710 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1711 tr, INT_MAX);
1712 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1713 tr, 0);
1714
1715 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1716 tr, INT_MAX);
1717 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1718 tr, 0);
0f72e37e
SRRH
1719
1720 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1721 tr, INT_MAX);
1722 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1723 tr, 0);
1724
1725 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1726 tr, INT_MAX);
1727 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1728 tr, 0);
49090107
SRRH
1729 }
1730
799fd44c
SRRH
1731 /*
1732 * Ignoring of pids is done at task switch. But we have to
1733 * check for those tasks that are currently running.
1734 * Always do this in case a pid was appended or removed.
1735 */
1736 on_each_cpu(ignore_task_cpu, tr, 1);
1737
f4d34a87 1738 out:
3fdaf80f
SRRH
1739 mutex_unlock(&event_mutex);
1740
49090107 1741 ret = read;
f4d34a87
SR
1742 if (read > 0)
1743 *ppos += read;
49090107
SRRH
1744
1745 return ret;
1746}
1747
15075cac
SR
1748static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1749static int ftrace_event_set_open(struct inode *inode, struct file *file);
49090107 1750static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
f77d09a3 1751static int ftrace_event_release(struct inode *inode, struct file *file);
15075cac 1752
b77e38aa
SR
1753static const struct seq_operations show_event_seq_ops = {
1754 .start = t_start,
1755 .next = t_next,
1756 .show = t_show,
1757 .stop = t_stop,
1758};
1759
1760static const struct seq_operations show_set_event_seq_ops = {
1761 .start = s_start,
1762 .next = s_next,
1763 .show = t_show,
1764 .stop = t_stop,
1765};
1766
49090107
SRRH
1767static const struct seq_operations show_set_pid_seq_ops = {
1768 .start = p_start,
1769 .next = p_next,
5cc8976b 1770 .show = trace_pid_show,
49090107
SRRH
1771 .stop = p_stop,
1772};
1773
2314c4ae 1774static const struct file_operations ftrace_avail_fops = {
15075cac 1775 .open = ftrace_event_avail_open,
2314c4ae
SR
1776 .read = seq_read,
1777 .llseek = seq_lseek,
1778 .release = seq_release,
1779};
1780
b77e38aa 1781static const struct file_operations ftrace_set_event_fops = {
15075cac 1782 .open = ftrace_event_set_open,
b77e38aa
SR
1783 .read = seq_read,
1784 .write = ftrace_event_write,
1785 .llseek = seq_lseek,
f77d09a3 1786 .release = ftrace_event_release,
b77e38aa
SR
1787};
1788
49090107
SRRH
1789static const struct file_operations ftrace_set_event_pid_fops = {
1790 .open = ftrace_event_set_pid_open,
1791 .read = seq_read,
1792 .write = ftrace_event_pid_write,
1793 .llseek = seq_lseek,
1794 .release = ftrace_event_release,
1795};
1796
1473e441 1797static const struct file_operations ftrace_enable_fops = {
bf682c31 1798 .open = tracing_open_generic,
1473e441
SR
1799 .read = event_enable_read,
1800 .write = event_enable_write,
6038f373 1801 .llseek = default_llseek,
1473e441
SR
1802};
1803
981d081e 1804static const struct file_operations ftrace_event_format_fops = {
2a37a3df
SR
1805 .open = trace_format_open,
1806 .read = seq_read,
1807 .llseek = seq_lseek,
1808 .release = seq_release,
981d081e
SR
1809};
1810
23725aee 1811static const struct file_operations ftrace_event_id_fops = {
23725aee 1812 .read = event_id_read,
6038f373 1813 .llseek = default_llseek,
23725aee
PZ
1814};
1815
7ce7e424
TZ
1816static const struct file_operations ftrace_event_filter_fops = {
1817 .open = tracing_open_generic,
1818 .read = event_filter_read,
1819 .write = event_filter_write,
6038f373 1820 .llseek = default_llseek,
7ce7e424
TZ
1821};
1822
cfb180f3 1823static const struct file_operations ftrace_subsystem_filter_fops = {
e9dbfae5 1824 .open = subsystem_open,
cfb180f3
TZ
1825 .read = subsystem_filter_read,
1826 .write = subsystem_filter_write,
6038f373 1827 .llseek = default_llseek,
e9dbfae5 1828 .release = subsystem_release,
cfb180f3
TZ
1829};
1830
8ae79a13 1831static const struct file_operations ftrace_system_enable_fops = {
40ee4dff 1832 .open = subsystem_open,
8ae79a13
SR
1833 .read = system_enable_read,
1834 .write = system_enable_write,
6038f373 1835 .llseek = default_llseek,
40ee4dff 1836 .release = subsystem_release,
8ae79a13
SR
1837};
1838
ae63b31e
SR
1839static const struct file_operations ftrace_tr_enable_fops = {
1840 .open = system_tr_open,
1841 .read = system_enable_read,
1842 .write = system_enable_write,
1843 .llseek = default_llseek,
1844 .release = subsystem_release,
1845};
1846
d1b182a8
SR
1847static const struct file_operations ftrace_show_header_fops = {
1848 .open = tracing_open_generic,
1849 .read = show_header,
6038f373 1850 .llseek = default_llseek,
d1b182a8
SR
1851};
1852
ae63b31e
SR
1853static int
1854ftrace_event_open(struct inode *inode, struct file *file,
1855 const struct seq_operations *seq_ops)
1473e441 1856{
ae63b31e
SR
1857 struct seq_file *m;
1858 int ret;
1473e441 1859
ae63b31e
SR
1860 ret = seq_open(file, seq_ops);
1861 if (ret < 0)
1862 return ret;
1863 m = file->private_data;
1864 /* copy tr over to seq ops */
1865 m->private = inode->i_private;
1473e441 1866
ae63b31e 1867 return ret;
1473e441
SR
1868}
1869
f77d09a3
AL
1870static int ftrace_event_release(struct inode *inode, struct file *file)
1871{
1872 struct trace_array *tr = inode->i_private;
1873
1874 trace_array_put(tr);
1875
1876 return seq_release(inode, file);
1877}
1878
15075cac
SR
1879static int
1880ftrace_event_avail_open(struct inode *inode, struct file *file)
1881{
1882 const struct seq_operations *seq_ops = &show_event_seq_ops;
1883
ae63b31e 1884 return ftrace_event_open(inode, file, seq_ops);
15075cac
SR
1885}
1886
1887static int
1888ftrace_event_set_open(struct inode *inode, struct file *file)
1889{
1890 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
ae63b31e 1891 struct trace_array *tr = inode->i_private;
f77d09a3
AL
1892 int ret;
1893
1894 if (trace_array_get(tr) < 0)
1895 return -ENODEV;
15075cac
SR
1896
1897 if ((file->f_mode & FMODE_WRITE) &&
1898 (file->f_flags & O_TRUNC))
ae63b31e 1899 ftrace_clear_events(tr);
15075cac 1900
f77d09a3
AL
1901 ret = ftrace_event_open(inode, file, seq_ops);
1902 if (ret < 0)
1903 trace_array_put(tr);
1904 return ret;
ae63b31e
SR
1905}
1906
49090107
SRRH
1907static int
1908ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1909{
1910 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1911 struct trace_array *tr = inode->i_private;
1912 int ret;
1913
1914 if (trace_array_get(tr) < 0)
1915 return -ENODEV;
1916
1917 if ((file->f_mode & FMODE_WRITE) &&
1918 (file->f_flags & O_TRUNC))
1919 ftrace_clear_event_pids(tr);
1920
1921 ret = ftrace_event_open(inode, file, seq_ops);
1922 if (ret < 0)
1923 trace_array_put(tr);
1924 return ret;
1925}
1926
ae63b31e
SR
1927static struct event_subsystem *
1928create_new_subsystem(const char *name)
1929{
1930 struct event_subsystem *system;
1931
1932 /* need to create new entry */
1933 system = kmalloc(sizeof(*system), GFP_KERNEL);
1934 if (!system)
1935 return NULL;
1936
1937 system->ref_count = 1;
6e94a780
SR
1938
1939 /* Only allocate if dynamic (kprobes and modules) */
79ac6ef5
RV
1940 system->name = kstrdup_const(name, GFP_KERNEL);
1941 if (!system->name)
1942 goto out_free;
ae63b31e
SR
1943
1944 system->filter = NULL;
1945
1946 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1947 if (!system->filter)
1948 goto out_free;
1949
1950 list_add(&system->list, &event_subsystems);
1951
1952 return system;
1953
1954 out_free:
79ac6ef5 1955 kfree_const(system->name);
ae63b31e
SR
1956 kfree(system);
1957 return NULL;
15075cac
SR
1958}
1959
6ecc2d1c 1960static struct dentry *
ae63b31e 1961event_subsystem_dir(struct trace_array *tr, const char *name,
7f1d2f82 1962 struct trace_event_file *file, struct dentry *parent)
6ecc2d1c 1963{
7967b3e0 1964 struct trace_subsystem_dir *dir;
6ecc2d1c 1965 struct event_subsystem *system;
e1112b4d 1966 struct dentry *entry;
6ecc2d1c
SR
1967
1968 /* First see if we did not already create this dir */
ae63b31e
SR
1969 list_for_each_entry(dir, &tr->systems, list) {
1970 system = dir->subsystem;
dc82ec98 1971 if (strcmp(system->name, name) == 0) {
ae63b31e
SR
1972 dir->nr_events++;
1973 file->system = dir;
1974 return dir->entry;
dc82ec98 1975 }
6ecc2d1c
SR
1976 }
1977
ae63b31e
SR
1978 /* Now see if the system itself exists. */
1979 list_for_each_entry(system, &event_subsystems, list) {
1980 if (strcmp(system->name, name) == 0)
1981 break;
6ecc2d1c 1982 }
ae63b31e
SR
1983 /* Reset system variable when not found */
1984 if (&system->list == &event_subsystems)
1985 system = NULL;
6ecc2d1c 1986
ae63b31e
SR
1987 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1988 if (!dir)
1989 goto out_fail;
6ecc2d1c 1990
ae63b31e
SR
1991 if (!system) {
1992 system = create_new_subsystem(name);
1993 if (!system)
1994 goto out_free;
1995 } else
1996 __get_system(system);
1997
8434dc93 1998 dir->entry = tracefs_create_dir(name, parent);
ae63b31e 1999 if (!dir->entry) {
3448bac3 2000 pr_warn("Failed to create system directory %s\n", name);
ae63b31e
SR
2001 __put_system(system);
2002 goto out_free;
6d723736
SR
2003 }
2004
ae63b31e
SR
2005 dir->tr = tr;
2006 dir->ref_count = 1;
2007 dir->nr_events = 1;
2008 dir->subsystem = system;
2009 file->system = dir;
8b372562 2010
8434dc93 2011 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
e1112b4d 2012 &ftrace_subsystem_filter_fops);
8b372562
TZ
2013 if (!entry) {
2014 kfree(system->filter);
2015 system->filter = NULL;
8434dc93 2016 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
8b372562 2017 }
e1112b4d 2018
ae63b31e 2019 trace_create_file("enable", 0644, dir->entry, dir,
f3f3f009 2020 &ftrace_system_enable_fops);
8ae79a13 2021
ae63b31e
SR
2022 list_add(&dir->list, &tr->systems);
2023
2024 return dir->entry;
2025
2026 out_free:
2027 kfree(dir);
2028 out_fail:
2029 /* Only print this message if failed on memory allocation */
2030 if (!dir || !system)
3448bac3 2031 pr_warn("No memory to create event subsystem %s\n", name);
ae63b31e 2032 return NULL;
6ecc2d1c
SR
2033}
2034
1473e441 2035static int
7f1d2f82 2036event_create_dir(struct dentry *parent, struct trace_event_file *file)
1473e441 2037{
2425bcb9 2038 struct trace_event_call *call = file->event_call;
ae63b31e 2039 struct trace_array *tr = file->tr;
2e33af02 2040 struct list_head *head;
ae63b31e 2041 struct dentry *d_events;
de7b2973 2042 const char *name;
fd994989 2043 int ret;
1473e441 2044
6ecc2d1c
SR
2045 /*
2046 * If the trace point header did not define TRACE_SYSTEM
2047 * then the system would be called "TRACE_SYSTEM".
2048 */
ae63b31e
SR
2049 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2050 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2051 if (!d_events)
2052 return -ENOMEM;
2053 } else
2054 d_events = parent;
2055
687fcc4a 2056 name = trace_event_name(call);
8434dc93 2057 file->dir = tracefs_create_dir(name, d_events);
ae63b31e 2058 if (!file->dir) {
8434dc93 2059 pr_warn("Could not create tracefs '%s' directory\n", name);
1473e441
SR
2060 return -1;
2061 }
2062
9b63776f 2063 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
ae63b31e 2064 trace_create_file("enable", 0644, file->dir, file,
620a30e9 2065 &ftrace_enable_fops);
1473e441 2066
2239291a 2067#ifdef CONFIG_PERF_EVENTS
a1d0ce82 2068 if (call->event.type && call->class->reg)
1a11126b 2069 trace_create_file("id", 0444, file->dir,
620a30e9
ON
2070 (void *)(long)call->event.type,
2071 &ftrace_event_id_fops);
2239291a 2072#endif
23725aee 2073
c9d932cf
LZ
2074 /*
2075 * Other events may have the same class. Only update
2076 * the fields if they are not already defined.
2077 */
2078 head = trace_get_fields(call);
2079 if (list_empty(head)) {
2080 ret = call->class->define_fields(call);
2081 if (ret < 0) {
3448bac3
FF
2082 pr_warn("Could not initialize trace point events/%s\n",
2083 name);
ae63b31e 2084 return -1;
cf027f64
TZ
2085 }
2086 }
f306cc82 2087 trace_create_file("filter", 0644, file->dir, file,
620a30e9 2088 &ftrace_event_filter_fops);
cf027f64 2089
854145e0
CH
2090 /*
2091 * Only event directories that can be enabled should have
2092 * triggers.
2093 */
2094 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2095 trace_create_file("trigger", 0644, file->dir, file,
2096 &event_trigger_fops);
85f2b082 2097
7ef224d1
TZ
2098#ifdef CONFIG_HIST_TRIGGERS
2099 trace_create_file("hist", 0444, file->dir, file,
2100 &event_hist_fops);
2101#endif
ae63b31e 2102 trace_create_file("format", 0444, file->dir, call,
620a30e9 2103 &ftrace_event_format_fops);
6d723736
SR
2104
2105 return 0;
2106}
2107
2425bcb9 2108static void remove_event_from_tracers(struct trace_event_call *call)
ae63b31e 2109{
7f1d2f82 2110 struct trace_event_file *file;
ae63b31e
SR
2111 struct trace_array *tr;
2112
2113 do_for_each_event_file_safe(tr, file) {
ae63b31e
SR
2114 if (file->event_call != call)
2115 continue;
2116
f6a84bdc 2117 remove_event_file_dir(file);
ae63b31e
SR
2118 /*
2119 * The do_for_each_event_file_safe() is
2120 * a double loop. After finding the call for this
2121 * trace_array, we use break to jump to the next
2122 * trace_array.
2123 */
2124 break;
2125 } while_for_each_event_file();
2126}
2127
2425bcb9 2128static void event_remove(struct trace_event_call *call)
8781915a 2129{
ae63b31e 2130 struct trace_array *tr;
7f1d2f82 2131 struct trace_event_file *file;
ae63b31e
SR
2132
2133 do_for_each_event_file(tr, file) {
2134 if (file->event_call != call)
2135 continue;
2136 ftrace_event_enable_disable(file, 0);
2137 /*
2138 * The do_for_each_event_file() is
2139 * a double loop. After finding the call for this
2140 * trace_array, we use break to jump to the next
2141 * trace_array.
2142 */
2143 break;
2144 } while_for_each_event_file();
2145
8781915a 2146 if (call->event.funcs)
9023c930 2147 __unregister_trace_event(&call->event);
ae63b31e 2148 remove_event_from_tracers(call);
8781915a
EG
2149 list_del(&call->list);
2150}
2151
2425bcb9 2152static int event_init(struct trace_event_call *call)
8781915a
EG
2153{
2154 int ret = 0;
de7b2973 2155 const char *name;
8781915a 2156
687fcc4a 2157 name = trace_event_name(call);
de7b2973 2158 if (WARN_ON(!name))
8781915a
EG
2159 return -EINVAL;
2160
2161 if (call->class->raw_init) {
2162 ret = call->class->raw_init(call);
2163 if (ret < 0 && ret != -ENOSYS)
3448bac3 2164 pr_warn("Could not initialize trace events/%s\n", name);
8781915a
EG
2165 }
2166
2167 return ret;
2168}
2169
67ead0a6 2170static int
2425bcb9 2171__register_event(struct trace_event_call *call, struct module *mod)
bd1a5c84 2172{
bd1a5c84 2173 int ret;
6d723736 2174
8781915a
EG
2175 ret = event_init(call);
2176 if (ret < 0)
2177 return ret;
701970b3 2178
ae63b31e 2179 list_add(&call->list, &ftrace_events);
67ead0a6 2180 call->mod = mod;
88f70d75 2181
ae63b31e 2182 return 0;
bd1a5c84
MH
2183}
2184
0c564a53
SRRH
2185static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2186{
2187 int rlen;
2188 int elen;
2189
2190 /* Find the length of the enum value as a string */
2191 elen = snprintf(ptr, 0, "%ld", map->enum_value);
2192 /* Make sure there's enough room to replace the string with the value */
2193 if (len < elen)
2194 return NULL;
2195
2196 snprintf(ptr, elen + 1, "%ld", map->enum_value);
2197
2198 /* Get the rest of the string of ptr */
2199 rlen = strlen(ptr + len);
2200 memmove(ptr + elen, ptr + len, rlen);
2201 /* Make sure we end the new string */
2202 ptr[elen + rlen] = 0;
2203
2204 return ptr + elen;
2205}
2206
2425bcb9 2207static void update_event_printk(struct trace_event_call *call,
0c564a53
SRRH
2208 struct trace_enum_map *map)
2209{
2210 char *ptr;
2211 int quote = 0;
2212 int len = strlen(map->enum_string);
2213
2214 for (ptr = call->print_fmt; *ptr; ptr++) {
2215 if (*ptr == '\\') {
2216 ptr++;
2217 /* paranoid */
2218 if (!*ptr)
2219 break;
2220 continue;
2221 }
2222 if (*ptr == '"') {
2223 quote ^= 1;
2224 continue;
2225 }
2226 if (quote)
2227 continue;
2228 if (isdigit(*ptr)) {
2229 /* skip numbers */
2230 do {
2231 ptr++;
2232 /* Check for alpha chars like ULL */
2233 } while (isalnum(*ptr));
3193899d
SRRH
2234 if (!*ptr)
2235 break;
0c564a53
SRRH
2236 /*
2237 * A number must have some kind of delimiter after
2238 * it, and we can ignore that too.
2239 */
2240 continue;
2241 }
2242 if (isalpha(*ptr) || *ptr == '_') {
2243 if (strncmp(map->enum_string, ptr, len) == 0 &&
2244 !isalnum(ptr[len]) && ptr[len] != '_') {
2245 ptr = enum_replace(ptr, map, len);
2246 /* Hmm, enum string smaller than value */
2247 if (WARN_ON_ONCE(!ptr))
2248 return;
2249 /*
2250 * No need to decrement here, as enum_replace()
2251 * returns the pointer to the character passed
2252 * the enum, and two enums can not be placed
2253 * back to back without something in between.
2254 * We can skip that something in between.
2255 */
2256 continue;
2257 }
2258 skip_more:
2259 do {
2260 ptr++;
2261 } while (isalnum(*ptr) || *ptr == '_');
3193899d
SRRH
2262 if (!*ptr)
2263 break;
0c564a53
SRRH
2264 /*
2265 * If what comes after this variable is a '.' or
2266 * '->' then we can continue to ignore that string.
2267 */
2268 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2269 ptr += *ptr == '.' ? 1 : 2;
3193899d
SRRH
2270 if (!*ptr)
2271 break;
0c564a53
SRRH
2272 goto skip_more;
2273 }
2274 /*
2275 * Once again, we can skip the delimiter that came
2276 * after the string.
2277 */
2278 continue;
2279 }
2280 }
2281}
2282
2283void trace_event_enum_update(struct trace_enum_map **map, int len)
2284{
2425bcb9 2285 struct trace_event_call *call, *p;
0c564a53
SRRH
2286 const char *last_system = NULL;
2287 int last_i;
2288 int i;
2289
2290 down_write(&trace_event_sem);
2291 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2292 /* events are usually grouped together with systems */
2293 if (!last_system || call->class->system != last_system) {
2294 last_i = 0;
2295 last_system = call->class->system;
2296 }
2297
2298 for (i = last_i; i < len; i++) {
2299 if (call->class->system == map[i]->system) {
2300 /* Save the first system if need be */
2301 if (!last_i)
2302 last_i = i;
2303 update_event_printk(call, map[i]);
2304 }
2305 }
2306 }
2307 up_write(&trace_event_sem);
2308}
2309
7f1d2f82 2310static struct trace_event_file *
2425bcb9 2311trace_create_new_event(struct trace_event_call *call,
da511bf3
SRRH
2312 struct trace_array *tr)
2313{
7f1d2f82 2314 struct trace_event_file *file;
da511bf3
SRRH
2315
2316 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2317 if (!file)
2318 return NULL;
2319
2320 file->event_call = call;
2321 file->tr = tr;
2322 atomic_set(&file->sm_ref, 0);
85f2b082
TZ
2323 atomic_set(&file->tm_ref, 0);
2324 INIT_LIST_HEAD(&file->triggers);
da511bf3
SRRH
2325 list_add(&file->list, &tr->events);
2326
2327 return file;
2328}
2329
ae63b31e
SR
2330/* Add an event to a trace directory */
2331static int
2425bcb9 2332__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
ae63b31e 2333{
7f1d2f82 2334 struct trace_event_file *file;
ae63b31e 2335
da511bf3 2336 file = trace_create_new_event(call, tr);
ae63b31e
SR
2337 if (!file)
2338 return -ENOMEM;
2339
620a30e9 2340 return event_create_dir(tr->event_dir, file);
ae63b31e
SR
2341}
2342
77248221
SR
2343/*
2344 * Just create a decriptor for early init. A descriptor is required
2345 * for enabling events at boot. We want to enable events before
2346 * the filesystem is initialized.
2347 */
2348static __init int
2425bcb9 2349__trace_early_add_new_event(struct trace_event_call *call,
77248221
SR
2350 struct trace_array *tr)
2351{
7f1d2f82 2352 struct trace_event_file *file;
77248221 2353
da511bf3 2354 file = trace_create_new_event(call, tr);
77248221
SR
2355 if (!file)
2356 return -ENOMEM;
2357
77248221
SR
2358 return 0;
2359}
2360
ae63b31e 2361struct ftrace_module_file_ops;
2425bcb9 2362static void __add_event_to_tracers(struct trace_event_call *call);
ae63b31e 2363
bd1a5c84 2364/* Add an additional event_call dynamically */
2425bcb9 2365int trace_add_event_call(struct trace_event_call *call)
bd1a5c84
MH
2366{
2367 int ret;
a8227415 2368 mutex_lock(&trace_types_lock);
bd1a5c84 2369 mutex_lock(&event_mutex);
701970b3 2370
ae63b31e
SR
2371 ret = __register_event(call, NULL);
2372 if (ret >= 0)
779c5e37 2373 __add_event_to_tracers(call);
a2ca5e03 2374
ae63b31e 2375 mutex_unlock(&event_mutex);
a8227415 2376 mutex_unlock(&trace_types_lock);
ae63b31e 2377 return ret;
a2ca5e03
FW
2378}
2379
4fead8e4 2380/*
a8227415
AL
2381 * Must be called under locking of trace_types_lock, event_mutex and
2382 * trace_event_sem.
4fead8e4 2383 */
2425bcb9 2384static void __trace_remove_event_call(struct trace_event_call *call)
bd1a5c84 2385{
8781915a 2386 event_remove(call);
bd1a5c84 2387 trace_destroy_fields(call);
57375747
ON
2388 free_event_filter(call->filter);
2389 call->filter = NULL;
bd1a5c84
MH
2390}
2391
2425bcb9 2392static int probe_remove_event_call(struct trace_event_call *call)
2816c551
ON
2393{
2394 struct trace_array *tr;
7f1d2f82 2395 struct trace_event_file *file;
2816c551
ON
2396
2397#ifdef CONFIG_PERF_EVENTS
2398 if (call->perf_refcount)
2399 return -EBUSY;
2400#endif
2401 do_for_each_event_file(tr, file) {
2402 if (file->event_call != call)
2403 continue;
2404 /*
2405 * We can't rely on ftrace_event_enable_disable(enable => 0)
5d6ad960 2406 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2816c551
ON
2407 * TRACE_REG_UNREGISTER.
2408 */
5d6ad960 2409 if (file->flags & EVENT_FILE_FL_ENABLED)
2816c551 2410 return -EBUSY;
2ba64035
SRRH
2411 /*
2412 * The do_for_each_event_file_safe() is
2413 * a double loop. After finding the call for this
2414 * trace_array, we use break to jump to the next
2415 * trace_array.
2416 */
2816c551
ON
2417 break;
2418 } while_for_each_event_file();
2419
2420 __trace_remove_event_call(call);
2421
2422 return 0;
2423}
2424
bd1a5c84 2425/* Remove an event_call */
2425bcb9 2426int trace_remove_event_call(struct trace_event_call *call)
bd1a5c84 2427{
2816c551
ON
2428 int ret;
2429
a8227415 2430 mutex_lock(&trace_types_lock);
bd1a5c84 2431 mutex_lock(&event_mutex);
52f6ad6d 2432 down_write(&trace_event_sem);
2816c551 2433 ret = probe_remove_event_call(call);
52f6ad6d 2434 up_write(&trace_event_sem);
bd1a5c84 2435 mutex_unlock(&event_mutex);
a8227415 2436 mutex_unlock(&trace_types_lock);
2816c551
ON
2437
2438 return ret;
bd1a5c84
MH
2439}
2440
2441#define for_each_event(event, start, end) \
2442 for (event = start; \
2443 (unsigned long)event < (unsigned long)end; \
2444 event++)
2445
2446#ifdef CONFIG_MODULES
2447
6d723736
SR
2448static void trace_module_add_events(struct module *mod)
2449{
2425bcb9 2450 struct trace_event_call **call, **start, **end;
6d723736 2451
45ab2813
SRRH
2452 if (!mod->num_trace_events)
2453 return;
2454
2455 /* Don't add infrastructure for mods without tracepoints */
2456 if (trace_module_has_bad_taint(mod)) {
2457 pr_err("%s: module has bad taint, not creating trace events\n",
2458 mod->name);
2459 return;
2460 }
2461
6d723736
SR
2462 start = mod->trace_events;
2463 end = mod->trace_events + mod->num_trace_events;
2464
6d723736 2465 for_each_event(call, start, end) {
ae63b31e 2466 __register_event(*call, mod);
779c5e37 2467 __add_event_to_tracers(*call);
6d723736
SR
2468 }
2469}
2470
2471static void trace_module_remove_events(struct module *mod)
2472{
2425bcb9 2473 struct trace_event_call *call, *p;
575380da 2474 bool clear_trace = false;
6d723736 2475
52f6ad6d 2476 down_write(&trace_event_sem);
6d723736
SR
2477 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2478 if (call->mod == mod) {
575380da
SRRH
2479 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2480 clear_trace = true;
bd1a5c84 2481 __trace_remove_event_call(call);
6d723736
SR
2482 }
2483 }
52f6ad6d 2484 up_write(&trace_event_sem);
9456f0fa
SR
2485
2486 /*
2487 * It is safest to reset the ring buffer if the module being unloaded
873c642f
SRRH
2488 * registered any events that were used. The only worry is if
2489 * a new module gets loaded, and takes on the same id as the events
2490 * of this module. When printing out the buffer, traced events left
2491 * over from this module may be passed to the new module events and
2492 * unexpected results may occur.
9456f0fa 2493 */
575380da 2494 if (clear_trace)
873c642f 2495 tracing_reset_all_online_cpus();
6d723736
SR
2496}
2497
61f919a1
SR
2498static int trace_module_notify(struct notifier_block *self,
2499 unsigned long val, void *data)
6d723736
SR
2500{
2501 struct module *mod = data;
2502
a8227415 2503 mutex_lock(&trace_types_lock);
6d723736
SR
2504 mutex_lock(&event_mutex);
2505 switch (val) {
2506 case MODULE_STATE_COMING:
2507 trace_module_add_events(mod);
2508 break;
2509 case MODULE_STATE_GOING:
2510 trace_module_remove_events(mod);
2511 break;
2512 }
2513 mutex_unlock(&event_mutex);
a8227415 2514 mutex_unlock(&trace_types_lock);
fd994989 2515
1473e441
SR
2516 return 0;
2517}
315326c1 2518
836d481e
ON
2519static struct notifier_block trace_module_nb = {
2520 .notifier_call = trace_module_notify,
3673b8e4 2521 .priority = 1, /* higher than trace.c module notify */
836d481e 2522};
61f919a1 2523#endif /* CONFIG_MODULES */
1473e441 2524
ae63b31e
SR
2525/* Create a new event directory structure for a trace directory. */
2526static void
2527__trace_add_event_dirs(struct trace_array *tr)
2528{
2425bcb9 2529 struct trace_event_call *call;
ae63b31e
SR
2530 int ret;
2531
2532 list_for_each_entry(call, &ftrace_events, list) {
620a30e9 2533 ret = __trace_add_new_event(call, tr);
ae63b31e 2534 if (ret < 0)
3448bac3 2535 pr_warn("Could not create directory for event %s\n",
687fcc4a 2536 trace_event_name(call));
ae63b31e
SR
2537 }
2538}
2539
7f1d2f82 2540struct trace_event_file *
3cd715de
SRRH
2541find_event_file(struct trace_array *tr, const char *system, const char *event)
2542{
7f1d2f82 2543 struct trace_event_file *file;
2425bcb9 2544 struct trace_event_call *call;
de7b2973 2545 const char *name;
3cd715de
SRRH
2546
2547 list_for_each_entry(file, &tr->events, list) {
2548
2549 call = file->event_call;
687fcc4a 2550 name = trace_event_name(call);
3cd715de 2551
de7b2973 2552 if (!name || !call->class || !call->class->reg)
3cd715de
SRRH
2553 continue;
2554
2555 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2556 continue;
2557
de7b2973 2558 if (strcmp(event, name) == 0 &&
3cd715de
SRRH
2559 strcmp(system, call->class->system) == 0)
2560 return file;
2561 }
2562 return NULL;
2563}
2564
2875a08b
SRRH
2565#ifdef CONFIG_DYNAMIC_FTRACE
2566
2567/* Avoid typos */
2568#define ENABLE_EVENT_STR "enable_event"
2569#define DISABLE_EVENT_STR "disable_event"
2570
2571struct event_probe_data {
7f1d2f82 2572 struct trace_event_file *file;
2875a08b
SRRH
2573 unsigned long count;
2574 int ref;
2575 bool enable;
2576};
2577
3cd715de
SRRH
2578static void
2579event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2580{
2581 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2582 struct event_probe_data *data = *pdata;
2583
2584 if (!data)
2585 return;
2586
2587 if (data->enable)
5d6ad960 2588 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3cd715de 2589 else
5d6ad960 2590 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3cd715de
SRRH
2591}
2592
2593static void
2594event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2595{
2596 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2597 struct event_probe_data *data = *pdata;
2598
2599 if (!data)
2600 return;
2601
2602 if (!data->count)
2603 return;
2604
2605 /* Skip if the event is in a state we want to switch to */
5d6ad960 2606 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
3cd715de
SRRH
2607 return;
2608
2609 if (data->count != -1)
2610 (data->count)--;
2611
2612 event_enable_probe(ip, parent_ip, _data);
2613}
2614
2615static int
2616event_enable_print(struct seq_file *m, unsigned long ip,
2617 struct ftrace_probe_ops *ops, void *_data)
2618{
2619 struct event_probe_data *data = _data;
2620
2621 seq_printf(m, "%ps:", (void *)ip);
2622
2623 seq_printf(m, "%s:%s:%s",
2624 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2625 data->file->event_call->class->system,
687fcc4a 2626 trace_event_name(data->file->event_call));
3cd715de
SRRH
2627
2628 if (data->count == -1)
fa6f0cc7 2629 seq_puts(m, ":unlimited\n");
3cd715de
SRRH
2630 else
2631 seq_printf(m, ":count=%ld\n", data->count);
2632
2633 return 0;
2634}
2635
2636static int
2637event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2638 void **_data)
2639{
2640 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2641 struct event_probe_data *data = *pdata;
2642
2643 data->ref++;
2644 return 0;
2645}
2646
2647static void
2648event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2649 void **_data)
2650{
2651 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2652 struct event_probe_data *data = *pdata;
2653
2654 if (WARN_ON_ONCE(data->ref <= 0))
2655 return;
2656
2657 data->ref--;
2658 if (!data->ref) {
2659 /* Remove the SOFT_MODE flag */
2660 __ftrace_event_enable_disable(data->file, 0, 1);
2661 module_put(data->file->event_call->mod);
2662 kfree(data);
2663 }
2664 *pdata = NULL;
2665}
2666
2667static struct ftrace_probe_ops event_enable_probe_ops = {
2668 .func = event_enable_probe,
2669 .print = event_enable_print,
2670 .init = event_enable_init,
2671 .free = event_enable_free,
2672};
2673
2674static struct ftrace_probe_ops event_enable_count_probe_ops = {
2675 .func = event_enable_count_probe,
2676 .print = event_enable_print,
2677 .init = event_enable_init,
2678 .free = event_enable_free,
2679};
2680
2681static struct ftrace_probe_ops event_disable_probe_ops = {
2682 .func = event_enable_probe,
2683 .print = event_enable_print,
2684 .init = event_enable_init,
2685 .free = event_enable_free,
2686};
2687
2688static struct ftrace_probe_ops event_disable_count_probe_ops = {
2689 .func = event_enable_count_probe,
2690 .print = event_enable_print,
2691 .init = event_enable_init,
2692 .free = event_enable_free,
2693};
2694
2695static int
2696event_enable_func(struct ftrace_hash *hash,
2697 char *glob, char *cmd, char *param, int enabled)
2698{
2699 struct trace_array *tr = top_trace_array();
7f1d2f82 2700 struct trace_event_file *file;
3cd715de
SRRH
2701 struct ftrace_probe_ops *ops;
2702 struct event_probe_data *data;
2703 const char *system;
2704 const char *event;
2705 char *number;
2706 bool enable;
2707 int ret;
2708
dc81e5e3
YY
2709 if (!tr)
2710 return -ENODEV;
2711
3cd715de 2712 /* hash funcs only work with set_ftrace_filter */
8092e808 2713 if (!enabled || !param)
3cd715de
SRRH
2714 return -EINVAL;
2715
2716 system = strsep(&param, ":");
2717 if (!param)
2718 return -EINVAL;
2719
2720 event = strsep(&param, ":");
2721
2722 mutex_lock(&event_mutex);
2723
2724 ret = -EINVAL;
2725 file = find_event_file(tr, system, event);
2726 if (!file)
2727 goto out;
2728
2729 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2730
2731 if (enable)
2732 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2733 else
2734 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2735
2736 if (glob[0] == '!') {
2737 unregister_ftrace_function_probe_func(glob+1, ops);
2738 ret = 0;
2739 goto out;
2740 }
2741
2742 ret = -ENOMEM;
2743 data = kzalloc(sizeof(*data), GFP_KERNEL);
2744 if (!data)
2745 goto out;
2746
2747 data->enable = enable;
2748 data->count = -1;
2749 data->file = file;
2750
2751 if (!param)
2752 goto out_reg;
2753
2754 number = strsep(&param, ":");
2755
2756 ret = -EINVAL;
2757 if (!strlen(number))
2758 goto out_free;
2759
2760 /*
2761 * We use the callback data field (which is a pointer)
2762 * as our counter.
2763 */
2764 ret = kstrtoul(number, 0, &data->count);
2765 if (ret)
2766 goto out_free;
2767
2768 out_reg:
2769 /* Don't let event modules unload while probe registered */
2770 ret = try_module_get(file->event_call->mod);
6ed01066
MH
2771 if (!ret) {
2772 ret = -EBUSY;
3cd715de 2773 goto out_free;
6ed01066 2774 }
3cd715de
SRRH
2775
2776 ret = __ftrace_event_enable_disable(file, 1, 1);
2777 if (ret < 0)
2778 goto out_put;
2779 ret = register_ftrace_function_probe(glob, ops, data);
ff305ded
SRRH
2780 /*
2781 * The above returns on success the # of functions enabled,
2782 * but if it didn't find any functions it returns zero.
2783 * Consider no functions a failure too.
2784 */
a5b85bd1
MH
2785 if (!ret) {
2786 ret = -ENOENT;
3cd715de 2787 goto out_disable;
ff305ded
SRRH
2788 } else if (ret < 0)
2789 goto out_disable;
2790 /* Just return zero, not the number of enabled functions */
2791 ret = 0;
3cd715de
SRRH
2792 out:
2793 mutex_unlock(&event_mutex);
2794 return ret;
2795
2796 out_disable:
2797 __ftrace_event_enable_disable(file, 0, 1);
2798 out_put:
2799 module_put(file->event_call->mod);
2800 out_free:
2801 kfree(data);
2802 goto out;
2803}
2804
2805static struct ftrace_func_command event_enable_cmd = {
2806 .name = ENABLE_EVENT_STR,
2807 .func = event_enable_func,
2808};
2809
2810static struct ftrace_func_command event_disable_cmd = {
2811 .name = DISABLE_EVENT_STR,
2812 .func = event_enable_func,
2813};
2814
2815static __init int register_event_cmds(void)
2816{
2817 int ret;
2818
2819 ret = register_ftrace_command(&event_enable_cmd);
2820 if (WARN_ON(ret < 0))
2821 return ret;
2822 ret = register_ftrace_command(&event_disable_cmd);
2823 if (WARN_ON(ret < 0))
2824 unregister_ftrace_command(&event_enable_cmd);
2825 return ret;
2826}
2827#else
2828static inline int register_event_cmds(void) { return 0; }
2829#endif /* CONFIG_DYNAMIC_FTRACE */
2830
77248221 2831/*
7f1d2f82 2832 * The top level array has already had its trace_event_file
77248221 2833 * descriptors created in order to allow for early events to
8434dc93 2834 * be recorded. This function is called after the tracefs has been
77248221
SR
2835 * initialized, and we now have to create the files associated
2836 * to the events.
2837 */
2838static __init void
2839__trace_early_add_event_dirs(struct trace_array *tr)
2840{
7f1d2f82 2841 struct trace_event_file *file;
77248221
SR
2842 int ret;
2843
2844
2845 list_for_each_entry(file, &tr->events, list) {
620a30e9 2846 ret = event_create_dir(tr->event_dir, file);
77248221 2847 if (ret < 0)
3448bac3 2848 pr_warn("Could not create directory for event %s\n",
687fcc4a 2849 trace_event_name(file->event_call));
77248221
SR
2850 }
2851}
2852
2853/*
2854 * For early boot up, the top trace array requires to have
2855 * a list of events that can be enabled. This must be done before
2856 * the filesystem is set up in order to allow events to be traced
2857 * early.
2858 */
2859static __init void
2860__trace_early_add_events(struct trace_array *tr)
2861{
2425bcb9 2862 struct trace_event_call *call;
77248221
SR
2863 int ret;
2864
2865 list_for_each_entry(call, &ftrace_events, list) {
2866 /* Early boot up should not have any modules loaded */
2867 if (WARN_ON_ONCE(call->mod))
2868 continue;
2869
2870 ret = __trace_early_add_new_event(call, tr);
2871 if (ret < 0)
3448bac3 2872 pr_warn("Could not create early event %s\n",
687fcc4a 2873 trace_event_name(call));
77248221
SR
2874 }
2875}
2876
0c8916c3
SR
2877/* Remove the event directory structure for a trace directory. */
2878static void
2879__trace_remove_event_dirs(struct trace_array *tr)
2880{
7f1d2f82 2881 struct trace_event_file *file, *next;
0c8916c3 2882
f6a84bdc
ON
2883 list_for_each_entry_safe(file, next, &tr->events, list)
2884 remove_event_file_dir(file);
0c8916c3
SR
2885}
2886
2425bcb9 2887static void __add_event_to_tracers(struct trace_event_call *call)
ae63b31e
SR
2888{
2889 struct trace_array *tr;
2890
620a30e9
ON
2891 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2892 __trace_add_new_event(call, tr);
ae63b31e
SR
2893}
2894
2425bcb9
SRRH
2895extern struct trace_event_call *__start_ftrace_events[];
2896extern struct trace_event_call *__stop_ftrace_events[];
a59fd602 2897
020e5f85
LZ
2898static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2899
2900static __init int setup_trace_event(char *str)
2901{
2902 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
55034cd6
SRRH
2903 ring_buffer_expanded = true;
2904 tracing_selftest_disabled = true;
020e5f85
LZ
2905
2906 return 1;
2907}
2908__setup("trace_event=", setup_trace_event);
2909
77248221
SR
2910/* Expects to have event_mutex held when called */
2911static int
2912create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
ae63b31e
SR
2913{
2914 struct dentry *d_events;
2915 struct dentry *entry;
2916
8434dc93 2917 entry = tracefs_create_file("set_event", 0644, parent,
ae63b31e
SR
2918 tr, &ftrace_set_event_fops);
2919 if (!entry) {
8434dc93 2920 pr_warn("Could not create tracefs 'set_event' entry\n");
ae63b31e
SR
2921 return -ENOMEM;
2922 }
2923
8434dc93 2924 d_events = tracefs_create_dir("events", parent);
277ba044 2925 if (!d_events) {
8434dc93 2926 pr_warn("Could not create tracefs 'events' directory\n");
277ba044
SR
2927 return -ENOMEM;
2928 }
ae63b31e 2929
49090107
SRRH
2930 entry = tracefs_create_file("set_event_pid", 0644, parent,
2931 tr, &ftrace_set_event_pid_fops);
2932
ae63b31e
SR
2933 /* ring buffer internal formats */
2934 trace_create_file("header_page", 0444, d_events,
2935 ring_buffer_print_page_header,
2936 &ftrace_show_header_fops);
2937
2938 trace_create_file("header_event", 0444, d_events,
2939 ring_buffer_print_entry_header,
2940 &ftrace_show_header_fops);
2941
2942 trace_create_file("enable", 0644, d_events,
2943 tr, &ftrace_tr_enable_fops);
2944
2945 tr->event_dir = d_events;
77248221
SR
2946
2947 return 0;
2948}
2949
2950/**
2951 * event_trace_add_tracer - add a instance of a trace_array to events
2952 * @parent: The parent dentry to place the files/directories for events in
2953 * @tr: The trace array associated with these events
2954 *
2955 * When a new instance is created, it needs to set up its events
2956 * directory, as well as other files associated with events. It also
2957 * creates the event hierachry in the @parent/events directory.
2958 *
2959 * Returns 0 on success.
2960 */
2961int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2962{
2963 int ret;
2964
2965 mutex_lock(&event_mutex);
2966
2967 ret = create_event_toplevel_files(parent, tr);
2968 if (ret)
2969 goto out_unlock;
2970
52f6ad6d 2971 down_write(&trace_event_sem);
ae63b31e 2972 __trace_add_event_dirs(tr);
52f6ad6d 2973 up_write(&trace_event_sem);
277ba044 2974
77248221 2975 out_unlock:
277ba044 2976 mutex_unlock(&event_mutex);
ae63b31e 2977
77248221
SR
2978 return ret;
2979}
2980
2981/*
2982 * The top trace array already had its file descriptors created.
2983 * Now the files themselves need to be created.
2984 */
2985static __init int
2986early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2987{
2988 int ret;
2989
2990 mutex_lock(&event_mutex);
2991
2992 ret = create_event_toplevel_files(parent, tr);
2993 if (ret)
2994 goto out_unlock;
2995
52f6ad6d 2996 down_write(&trace_event_sem);
77248221 2997 __trace_early_add_event_dirs(tr);
52f6ad6d 2998 up_write(&trace_event_sem);
77248221
SR
2999
3000 out_unlock:
3001 mutex_unlock(&event_mutex);
3002
3003 return ret;
ae63b31e
SR
3004}
3005
0c8916c3
SR
3006int event_trace_del_tracer(struct trace_array *tr)
3007{
0c8916c3
SR
3008 mutex_lock(&event_mutex);
3009
85f2b082
TZ
3010 /* Disable any event triggers and associated soft-disabled events */
3011 clear_event_triggers(tr);
3012
49090107
SRRH
3013 /* Clear the pid list */
3014 __ftrace_clear_event_pids(tr);
3015
2a6c24af
SRRH
3016 /* Disable any running events */
3017 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3018
3ccb0123
SR
3019 /* Access to events are within rcu_read_lock_sched() */
3020 synchronize_sched();
3021
52f6ad6d 3022 down_write(&trace_event_sem);
0c8916c3 3023 __trace_remove_event_dirs(tr);
8434dc93 3024 tracefs_remove_recursive(tr->event_dir);
52f6ad6d 3025 up_write(&trace_event_sem);
0c8916c3
SR
3026
3027 tr->event_dir = NULL;
3028
3029 mutex_unlock(&event_mutex);
3030
3031 return 0;
3032}
3033
d1a29143
SR
3034static __init int event_trace_memsetup(void)
3035{
3036 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
7f1d2f82 3037 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
d1a29143
SR
3038 return 0;
3039}
3040
ce1039bd
SRRH
3041static __init void
3042early_enable_events(struct trace_array *tr, bool disable_first)
3043{
3044 char *buf = bootup_event_buf;
3045 char *token;
3046 int ret;
3047
3048 while (true) {
3049 token = strsep(&buf, ",");
3050
3051 if (!token)
3052 break;
ce1039bd 3053
43ed3843
SRRH
3054 if (*token) {
3055 /* Restarting syscalls requires that we stop them first */
3056 if (disable_first)
3057 ftrace_set_clr_event(tr, token, 0);
ce1039bd 3058
43ed3843
SRRH
3059 ret = ftrace_set_clr_event(tr, token, 1);
3060 if (ret)
3061 pr_warn("Failed to enable trace event: %s\n", token);
3062 }
ce1039bd
SRRH
3063
3064 /* Put back the comma to allow this to be called again */
3065 if (buf)
3066 *(buf - 1) = ',';
3067 }
3068}
3069
8781915a
EG
3070static __init int event_trace_enable(void)
3071{
ae63b31e 3072 struct trace_array *tr = top_trace_array();
2425bcb9 3073 struct trace_event_call **iter, *call;
8781915a
EG
3074 int ret;
3075
dc81e5e3
YY
3076 if (!tr)
3077 return -ENODEV;
3078
8781915a
EG
3079 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3080
3081 call = *iter;
3082 ret = event_init(call);
3083 if (!ret)
3084 list_add(&call->list, &ftrace_events);
3085 }
3086
77248221
SR
3087 /*
3088 * We need the top trace array to have a working set of trace
3089 * points at early init, before the debug files and directories
3090 * are created. Create the file entries now, and attach them
3091 * to the actual file dentries later.
3092 */
3093 __trace_early_add_events(tr);
3094
ce1039bd 3095 early_enable_events(tr, false);
81698831
SR
3096
3097 trace_printk_start_comm();
3098
3cd715de
SRRH
3099 register_event_cmds();
3100
85f2b082
TZ
3101 register_trigger_cmds();
3102
8781915a
EG
3103 return 0;
3104}
3105
ce1039bd
SRRH
3106/*
3107 * event_trace_enable() is called from trace_event_init() first to
3108 * initialize events and perhaps start any events that are on the
3109 * command line. Unfortunately, there are some events that will not
3110 * start this early, like the system call tracepoints that need
3111 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3112 * is called before pid 1 starts, and this flag is never set, making
3113 * the syscall tracepoint never get reached, but the event is enabled
3114 * regardless (and not doing anything).
3115 */
3116static __init int event_trace_enable_again(void)
3117{
3118 struct trace_array *tr;
3119
3120 tr = top_trace_array();
3121 if (!tr)
3122 return -ENODEV;
3123
3124 early_enable_events(tr, true);
3125
3126 return 0;
3127}
3128
3129early_initcall(event_trace_enable_again);
3130
b77e38aa
SR
3131static __init int event_trace_init(void)
3132{
ae63b31e 3133 struct trace_array *tr;
b77e38aa
SR
3134 struct dentry *d_tracer;
3135 struct dentry *entry;
6d723736 3136 int ret;
b77e38aa 3137
ae63b31e 3138 tr = top_trace_array();
dc81e5e3
YY
3139 if (!tr)
3140 return -ENODEV;
ae63b31e 3141
b77e38aa 3142 d_tracer = tracing_init_dentry();
14a5ae40 3143 if (IS_ERR(d_tracer))
b77e38aa
SR
3144 return 0;
3145
8434dc93 3146 entry = tracefs_create_file("available_events", 0444, d_tracer,
ae63b31e 3147 tr, &ftrace_avail_fops);
2314c4ae 3148 if (!entry)
8434dc93 3149 pr_warn("Could not create tracefs 'available_events' entry\n");
2314c4ae 3150
9f616680
DW
3151 if (trace_define_generic_fields())
3152 pr_warn("tracing: Failed to allocated generic fields");
3153
8728fe50 3154 if (trace_define_common_fields())
3448bac3 3155 pr_warn("tracing: Failed to allocate common fields");
8728fe50 3156
77248221 3157 ret = early_event_add_tracer(d_tracer, tr);
ae63b31e
SR
3158 if (ret)
3159 return ret;
020e5f85 3160
836d481e 3161#ifdef CONFIG_MODULES
6d723736 3162 ret = register_module_notifier(&trace_module_nb);
55379376 3163 if (ret)
3448bac3 3164 pr_warn("Failed to register trace events module notifier\n");
836d481e 3165#endif
b77e38aa
SR
3166 return 0;
3167}
5f893b26
SRRH
3168
3169void __init trace_event_init(void)
3170{
3171 event_trace_memsetup();
3172 init_ftrace_syscalls();
3173 event_trace_enable();
3174}
3175
b77e38aa 3176fs_initcall(event_trace_init);
e6187007
SR
3177
3178#ifdef CONFIG_FTRACE_STARTUP_TEST
3179
3180static DEFINE_SPINLOCK(test_spinlock);
3181static DEFINE_SPINLOCK(test_spinlock_irq);
3182static DEFINE_MUTEX(test_mutex);
3183
3184static __init void test_work(struct work_struct *dummy)
3185{
3186 spin_lock(&test_spinlock);
3187 spin_lock_irq(&test_spinlock_irq);
3188 udelay(1);
3189 spin_unlock_irq(&test_spinlock_irq);
3190 spin_unlock(&test_spinlock);
3191
3192 mutex_lock(&test_mutex);
3193 msleep(1);
3194 mutex_unlock(&test_mutex);
3195}
3196
3197static __init int event_test_thread(void *unused)
3198{
3199 void *test_malloc;
3200
3201 test_malloc = kmalloc(1234, GFP_KERNEL);
3202 if (!test_malloc)
3203 pr_info("failed to kmalloc\n");
3204
3205 schedule_on_each_cpu(test_work);
3206
3207 kfree(test_malloc);
3208
3209 set_current_state(TASK_INTERRUPTIBLE);
fe0e01c7 3210 while (!kthread_should_stop()) {
e6187007 3211 schedule();
fe0e01c7
PZ
3212 set_current_state(TASK_INTERRUPTIBLE);
3213 }
3214 __set_current_state(TASK_RUNNING);
e6187007
SR
3215
3216 return 0;
3217}
3218
3219/*
3220 * Do various things that may trigger events.
3221 */
3222static __init void event_test_stuff(void)
3223{
3224 struct task_struct *test_thread;
3225
3226 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3227 msleep(1);
3228 kthread_stop(test_thread);
3229}
3230
3231/*
3232 * For every trace event defined, we will test each trace point separately,
3233 * and then by groups, and finally all trace points.
3234 */
9ea21c1e 3235static __init void event_trace_self_tests(void)
e6187007 3236{
7967b3e0 3237 struct trace_subsystem_dir *dir;
7f1d2f82 3238 struct trace_event_file *file;
2425bcb9 3239 struct trace_event_call *call;
e6187007 3240 struct event_subsystem *system;
ae63b31e 3241 struct trace_array *tr;
e6187007
SR
3242 int ret;
3243
ae63b31e 3244 tr = top_trace_array();
dc81e5e3
YY
3245 if (!tr)
3246 return;
ae63b31e 3247
e6187007
SR
3248 pr_info("Running tests on trace events:\n");
3249
ae63b31e
SR
3250 list_for_each_entry(file, &tr->events, list) {
3251
3252 call = file->event_call;
e6187007 3253
2239291a
SR
3254 /* Only test those that have a probe */
3255 if (!call->class || !call->class->probe)
e6187007
SR
3256 continue;
3257
1f5a6b45
SR
3258/*
3259 * Testing syscall events here is pretty useless, but
3260 * we still do it if configured. But this is time consuming.
3261 * What we really need is a user thread to perform the
3262 * syscalls as we test.
3263 */
3264#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
8f082018
SR
3265 if (call->class->system &&
3266 strcmp(call->class->system, "syscalls") == 0)
1f5a6b45
SR
3267 continue;
3268#endif
3269
687fcc4a 3270 pr_info("Testing event %s: ", trace_event_name(call));
e6187007
SR
3271
3272 /*
3273 * If an event is already enabled, someone is using
3274 * it and the self test should not be on.
3275 */
5d6ad960 3276 if (file->flags & EVENT_FILE_FL_ENABLED) {
3448bac3 3277 pr_warn("Enabled event during self test!\n");
e6187007
SR
3278 WARN_ON_ONCE(1);
3279 continue;
3280 }
3281
ae63b31e 3282 ftrace_event_enable_disable(file, 1);
e6187007 3283 event_test_stuff();
ae63b31e 3284 ftrace_event_enable_disable(file, 0);
e6187007
SR
3285
3286 pr_cont("OK\n");
3287 }
3288
3289 /* Now test at the sub system level */
3290
3291 pr_info("Running tests on trace event systems:\n");
3292
ae63b31e
SR
3293 list_for_each_entry(dir, &tr->systems, list) {
3294
3295 system = dir->subsystem;
e6187007
SR
3296
3297 /* the ftrace system is special, skip it */
3298 if (strcmp(system->name, "ftrace") == 0)
3299 continue;
3300
3301 pr_info("Testing event system %s: ", system->name);
3302
ae63b31e 3303 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
e6187007 3304 if (WARN_ON_ONCE(ret)) {
3448bac3
FF
3305 pr_warn("error enabling system %s\n",
3306 system->name);
e6187007
SR
3307 continue;
3308 }
3309
3310 event_test_stuff();
3311
ae63b31e 3312 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
76bab1b7 3313 if (WARN_ON_ONCE(ret)) {
3448bac3
FF
3314 pr_warn("error disabling system %s\n",
3315 system->name);
76bab1b7
YL
3316 continue;
3317 }
e6187007
SR
3318
3319 pr_cont("OK\n");
3320 }
3321
3322 /* Test with all events enabled */
3323
3324 pr_info("Running tests on all trace events:\n");
3325 pr_info("Testing all events: ");
3326
ae63b31e 3327 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
e6187007 3328 if (WARN_ON_ONCE(ret)) {
3448bac3 3329 pr_warn("error enabling all events\n");
9ea21c1e 3330 return;
e6187007
SR
3331 }
3332
3333 event_test_stuff();
3334
3335 /* reset sysname */
ae63b31e 3336 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
e6187007 3337 if (WARN_ON_ONCE(ret)) {
3448bac3 3338 pr_warn("error disabling all events\n");
9ea21c1e 3339 return;
e6187007
SR
3340 }
3341
3342 pr_cont("OK\n");
9ea21c1e
SR
3343}
3344
3345#ifdef CONFIG_FUNCTION_TRACER
3346
245b2e70 3347static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
9ea21c1e 3348
9b9db275 3349static struct trace_event_file event_trace_file __initdata;
b7f0c959
SRRH
3350
3351static void __init
2f5f6ad9 3352function_test_events_call(unsigned long ip, unsigned long parent_ip,
a1e2e31d 3353 struct ftrace_ops *op, struct pt_regs *pt_regs)
9ea21c1e
SR
3354{
3355 struct ring_buffer_event *event;
e77405ad 3356 struct ring_buffer *buffer;
9ea21c1e
SR
3357 struct ftrace_entry *entry;
3358 unsigned long flags;
3359 long disabled;
9ea21c1e
SR
3360 int cpu;
3361 int pc;
3362
3363 pc = preempt_count();
5168ae50 3364 preempt_disable_notrace();
9ea21c1e 3365 cpu = raw_smp_processor_id();
245b2e70 3366 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
9ea21c1e
SR
3367
3368 if (disabled != 1)
3369 goto out;
3370
3371 local_save_flags(flags);
3372
9b9db275
SRRH
3373 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3374 TRACE_FN, sizeof(*entry),
3375 flags, pc);
9ea21c1e
SR
3376 if (!event)
3377 goto out;
3378 entry = ring_buffer_event_data(event);
3379 entry->ip = ip;
3380 entry->parent_ip = parent_ip;
3381
9b9db275
SRRH
3382 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3383 entry, flags, pc);
9ea21c1e 3384 out:
245b2e70 3385 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
5168ae50 3386 preempt_enable_notrace();
9ea21c1e
SR
3387}
3388
3389static struct ftrace_ops trace_ops __initdata =
3390{
3391 .func = function_test_events_call,
4740974a 3392 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
9ea21c1e
SR
3393};
3394
3395static __init void event_trace_self_test_with_function(void)
3396{
17bb615a 3397 int ret;
9b9db275
SRRH
3398
3399 event_trace_file.tr = top_trace_array();
3400 if (WARN_ON(!event_trace_file.tr))
2d34f489 3401 return;
9b9db275 3402
17bb615a
SR
3403 ret = register_ftrace_function(&trace_ops);
3404 if (WARN_ON(ret < 0)) {
3405 pr_info("Failed to enable function tracer for event tests\n");
3406 return;
3407 }
9ea21c1e
SR
3408 pr_info("Running tests again, along with the function tracer\n");
3409 event_trace_self_tests();
3410 unregister_ftrace_function(&trace_ops);
3411}
3412#else
3413static __init void event_trace_self_test_with_function(void)
3414{
3415}
3416#endif
3417
3418static __init int event_trace_self_tests_init(void)
3419{
020e5f85
LZ
3420 if (!tracing_selftest_disabled) {
3421 event_trace_self_tests();
3422 event_trace_self_test_with_function();
3423 }
e6187007
SR
3424
3425 return 0;
3426}
3427
28d20e2d 3428late_initcall(event_trace_self_tests_init);
e6187007
SR
3429
3430#endif