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