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