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