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