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