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