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