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