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