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