1
2
3
4
5
6
7
8
9
10
11
12#define pr_fmt(fmt) fmt
13
14#include <linux/workqueue.h>
15#include <linux/spinlock.h>
16#include <linux/kthread.h>
17#include <linux/tracefs.h>
18#include <linux/uaccess.h>
19#include <linux/module.h>
20#include <linux/ctype.h>
21#include <linux/sort.h>
22#include <linux/slab.h>
23#include <linux/delay.h>
24
25#include <trace/events/sched.h>
26
27#include <asm/setup.h>
28
29#include "trace_output.h"
30
31#undef TRACE_SYSTEM
32#define TRACE_SYSTEM "TRACE_SYSTEM"
33
34DEFINE_MUTEX(event_mutex);
35
36LIST_HEAD(ftrace_events);
37static LIST_HEAD(ftrace_generic_fields);
38static LIST_HEAD(ftrace_common_fields);
39
40#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41
42static struct kmem_cache *field_cachep;
43static struct kmem_cache *file_cachep;
44
45static inline int system_refcount(struct event_subsystem *system)
46{
47 return system->ref_count;
48}
49
50static int system_refcount_inc(struct event_subsystem *system)
51{
52 return system->ref_count++;
53}
54
55static int system_refcount_dec(struct event_subsystem *system)
56{
57 return --system->ref_count;
58}
59
60
61#define do_for_each_event_file(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
63 list_for_each_entry(file, &tr->events, list)
64
65#define do_for_each_event_file_safe(tr, file) \
66 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
67 struct trace_event_file *___n; \
68 list_for_each_entry_safe(file, ___n, &tr->events, list)
69
70#define while_for_each_event_file() \
71 }
72
73static struct ftrace_event_field *
74__find_event_field(struct list_head *head, char *name)
75{
76 struct ftrace_event_field *field;
77
78 list_for_each_entry(field, head, link) {
79 if (!strcmp(field->name, name))
80 return field;
81 }
82
83 return NULL;
84}
85
86struct ftrace_event_field *
87trace_find_event_field(struct trace_event_call *call, char *name)
88{
89 struct ftrace_event_field *field;
90 struct list_head *head;
91
92 head = trace_get_fields(call);
93 field = __find_event_field(head, name);
94 if (field)
95 return field;
96
97 field = __find_event_field(&ftrace_generic_fields, name);
98 if (field)
99 return field;
100
101 return __find_event_field(&ftrace_common_fields, name);
102}
103
104static int __trace_define_field(struct list_head *head, const char *type,
105 const char *name, int offset, int size,
106 int is_signed, int filter_type)
107{
108 struct ftrace_event_field *field;
109
110 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
111 if (!field)
112 return -ENOMEM;
113
114 field->name = name;
115 field->type = type;
116
117 if (filter_type == FILTER_OTHER)
118 field->filter_type = filter_assign_type(type);
119 else
120 field->filter_type = filter_type;
121
122 field->offset = offset;
123 field->size = size;
124 field->is_signed = is_signed;
125
126 list_add(&field->link, head);
127
128 return 0;
129}
130
131int trace_define_field(struct trace_event_call *call, const char *type,
132 const char *name, int offset, int size, int is_signed,
133 int filter_type)
134{
135 struct list_head *head;
136
137 if (WARN_ON(!call->class))
138 return 0;
139
140 head = trace_get_fields(call);
141 return __trace_define_field(head, type, name, offset, size,
142 is_signed, filter_type);
143}
144EXPORT_SYMBOL_GPL(trace_define_field);
145
146#define __generic_field(type, item, filter_type) \
147 ret = __trace_define_field(&ftrace_generic_fields, #type, \
148 #item, 0, 0, is_signed_type(type), \
149 filter_type); \
150 if (ret) \
151 return ret;
152
153#define __common_field(type, item) \
154 ret = __trace_define_field(&ftrace_common_fields, #type, \
155 "common_" #item, \
156 offsetof(typeof(ent), item), \
157 sizeof(ent.item), \
158 is_signed_type(type), FILTER_OTHER); \
159 if (ret) \
160 return ret;
161
162static int trace_define_generic_fields(void)
163{
164 int ret;
165
166 __generic_field(int, CPU, FILTER_CPU);
167 __generic_field(int, cpu, FILTER_CPU);
168 __generic_field(char *, COMM, FILTER_COMM);
169 __generic_field(char *, comm, FILTER_COMM);
170
171 return ret;
172}
173
174static int trace_define_common_fields(void)
175{
176 int ret;
177 struct trace_entry ent;
178
179 __common_field(unsigned short, type);
180 __common_field(unsigned char, flags);
181 __common_field(unsigned char, preempt_count);
182 __common_field(int, pid);
183
184 return ret;
185}
186
187static void trace_destroy_fields(struct trace_event_call *call)
188{
189 struct ftrace_event_field *field, *next;
190 struct list_head *head;
191
192 head = trace_get_fields(call);
193 list_for_each_entry_safe(field, next, head, link) {
194 list_del(&field->link);
195 kmem_cache_free(field_cachep, field);
196 }
197}
198
199
200
201
202
203int trace_event_get_offsets(struct trace_event_call *call)
204{
205 struct ftrace_event_field *tail;
206 struct list_head *head;
207
208 head = trace_get_fields(call);
209
210
211
212
213 tail = list_first_entry(head, struct ftrace_event_field, link);
214 return tail->offset + tail->size;
215}
216
217int trace_event_raw_init(struct trace_event_call *call)
218{
219 int id;
220
221 id = register_trace_event(&call->event);
222 if (!id)
223 return -ENODEV;
224
225 return 0;
226}
227EXPORT_SYMBOL_GPL(trace_event_raw_init);
228
229bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
230{
231 struct trace_array *tr = trace_file->tr;
232 struct trace_array_cpu *data;
233 struct trace_pid_list *pid_list;
234
235 pid_list = rcu_dereference_raw(tr->filtered_pids);
236 if (!pid_list)
237 return false;
238
239 data = this_cpu_ptr(tr->trace_buffer.data);
240
241 return data->ignore_pid;
242}
243EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
244
245void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
246 struct trace_event_file *trace_file,
247 unsigned long len)
248{
249 struct trace_event_call *event_call = trace_file->event_call;
250
251 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
252 trace_event_ignore_this_pid(trace_file))
253 return NULL;
254
255 local_save_flags(fbuffer->flags);
256 fbuffer->pc = preempt_count();
257
258
259
260
261
262
263 if (IS_ENABLED(CONFIG_PREEMPT))
264 fbuffer->pc--;
265 fbuffer->trace_file = trace_file;
266
267 fbuffer->event =
268 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
269 event_call->event.type, len,
270 fbuffer->flags, fbuffer->pc);
271 if (!fbuffer->event)
272 return NULL;
273
274 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
275 return fbuffer->entry;
276}
277EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
278
279int trace_event_reg(struct trace_event_call *call,
280 enum trace_reg type, void *data)
281{
282 struct trace_event_file *file = data;
283
284 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
285 switch (type) {
286 case TRACE_REG_REGISTER:
287 return tracepoint_probe_register(call->tp,
288 call->class->probe,
289 file);
290 case TRACE_REG_UNREGISTER:
291 tracepoint_probe_unregister(call->tp,
292 call->class->probe,
293 file);
294 return 0;
295
296#ifdef CONFIG_PERF_EVENTS
297 case TRACE_REG_PERF_REGISTER:
298 return tracepoint_probe_register(call->tp,
299 call->class->perf_probe,
300 call);
301 case TRACE_REG_PERF_UNREGISTER:
302 tracepoint_probe_unregister(call->tp,
303 call->class->perf_probe,
304 call);
305 return 0;
306 case TRACE_REG_PERF_OPEN:
307 case TRACE_REG_PERF_CLOSE:
308 case TRACE_REG_PERF_ADD:
309 case TRACE_REG_PERF_DEL:
310 return 0;
311#endif
312 }
313 return 0;
314}
315EXPORT_SYMBOL_GPL(trace_event_reg);
316
317void trace_event_enable_cmd_record(bool enable)
318{
319 struct trace_event_file *file;
320 struct trace_array *tr;
321
322 mutex_lock(&event_mutex);
323 do_for_each_event_file(tr, file) {
324
325 if (!(file->flags & EVENT_FILE_FL_ENABLED))
326 continue;
327
328 if (enable) {
329 tracing_start_cmdline_record();
330 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
331 } else {
332 tracing_stop_cmdline_record();
333 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
334 }
335 } while_for_each_event_file();
336 mutex_unlock(&event_mutex);
337}
338
339void trace_event_enable_tgid_record(bool enable)
340{
341 struct trace_event_file *file;
342 struct trace_array *tr;
343
344 mutex_lock(&event_mutex);
345 do_for_each_event_file(tr, file) {
346 if (!(file->flags & EVENT_FILE_FL_ENABLED))
347 continue;
348
349 if (enable) {
350 tracing_start_tgid_record();
351 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
352 } else {
353 tracing_stop_tgid_record();
354 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
355 &file->flags);
356 }
357 } while_for_each_event_file();
358 mutex_unlock(&event_mutex);
359}
360
361static int __ftrace_event_enable_disable(struct trace_event_file *file,
362 int enable, int soft_disable)
363{
364 struct trace_event_call *call = file->event_call;
365 struct trace_array *tr = file->tr;
366 unsigned long file_flags = file->flags;
367 int ret = 0;
368 int disable;
369
370 switch (enable) {
371 case 0:
372
373
374
375
376
377
378
379
380
381
382
383
384
385 if (soft_disable) {
386 if (atomic_dec_return(&file->sm_ref) > 0)
387 break;
388 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
389 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
390 } else
391 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
392
393 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
394 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
395 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
396 tracing_stop_cmdline_record();
397 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
398 }
399
400 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
401 tracing_stop_tgid_record();
402 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
403 }
404
405 call->class->reg(call, TRACE_REG_UNREGISTER, file);
406 }
407
408 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
409 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
410 else
411 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
412 break;
413 case 1:
414
415
416
417
418
419
420
421
422 if (!soft_disable)
423 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
424 else {
425 if (atomic_inc_return(&file->sm_ref) > 1)
426 break;
427 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
428 }
429
430 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
431 bool cmd = false, tgid = false;
432
433
434 if (soft_disable)
435 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
436
437 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
438 cmd = true;
439 tracing_start_cmdline_record();
440 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
441 }
442
443 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
444 tgid = true;
445 tracing_start_tgid_record();
446 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
447 }
448
449 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
450 if (ret) {
451 if (cmd)
452 tracing_stop_cmdline_record();
453 if (tgid)
454 tracing_stop_tgid_record();
455 pr_info("event trace: Could not enable event "
456 "%s\n", trace_event_name(call));
457 break;
458 }
459 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
460
461
462 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
463 }
464 break;
465 }
466
467
468 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
469 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
470 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
471 trace_buffered_event_enable();
472 else
473 trace_buffered_event_disable();
474 }
475
476 return ret;
477}
478
479int trace_event_enable_disable(struct trace_event_file *file,
480 int enable, int soft_disable)
481{
482 return __ftrace_event_enable_disable(file, enable, soft_disable);
483}
484
485static int ftrace_event_enable_disable(struct trace_event_file *file,
486 int enable)
487{
488 return __ftrace_event_enable_disable(file, enable, 0);
489}
490
491static void ftrace_clear_events(struct trace_array *tr)
492{
493 struct trace_event_file *file;
494
495 mutex_lock(&event_mutex);
496 list_for_each_entry(file, &tr->events, list) {
497 ftrace_event_enable_disable(file, 0);
498 }
499 mutex_unlock(&event_mutex);
500}
501
502static void
503event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
504{
505 struct trace_pid_list *pid_list;
506 struct trace_array *tr = data;
507
508 pid_list = rcu_dereference_raw(tr->filtered_pids);
509 trace_filter_add_remove_task(pid_list, NULL, task);
510}
511
512static void
513event_filter_pid_sched_process_fork(void *data,
514 struct task_struct *self,
515 struct task_struct *task)
516{
517 struct trace_pid_list *pid_list;
518 struct trace_array *tr = data;
519
520 pid_list = rcu_dereference_sched(tr->filtered_pids);
521 trace_filter_add_remove_task(pid_list, self, task);
522}
523
524void trace_event_follow_fork(struct trace_array *tr, bool enable)
525{
526 if (enable) {
527 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
528 tr, INT_MIN);
529 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit,
530 tr, INT_MAX);
531 } else {
532 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
533 tr);
534 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit,
535 tr);
536 }
537}
538
539static void
540event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
541 struct task_struct *prev, struct task_struct *next)
542{
543 struct trace_array *tr = data;
544 struct trace_pid_list *pid_list;
545
546 pid_list = rcu_dereference_sched(tr->filtered_pids);
547
548 this_cpu_write(tr->trace_buffer.data->ignore_pid,
549 trace_ignore_this_task(pid_list, prev) &&
550 trace_ignore_this_task(pid_list, next));
551}
552
553static void
554event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
555 struct task_struct *prev, struct task_struct *next)
556{
557 struct trace_array *tr = data;
558 struct trace_pid_list *pid_list;
559
560 pid_list = rcu_dereference_sched(tr->filtered_pids);
561
562 this_cpu_write(tr->trace_buffer.data->ignore_pid,
563 trace_ignore_this_task(pid_list, next));
564}
565
566static void
567event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
568{
569 struct trace_array *tr = data;
570 struct trace_pid_list *pid_list;
571
572
573 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
574 return;
575
576 pid_list = rcu_dereference_sched(tr->filtered_pids);
577
578 this_cpu_write(tr->trace_buffer.data->ignore_pid,
579 trace_ignore_this_task(pid_list, task));
580}
581
582static void
583event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
584{
585 struct trace_array *tr = data;
586 struct trace_pid_list *pid_list;
587
588
589 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
590 return;
591
592 pid_list = rcu_dereference_sched(tr->filtered_pids);
593
594
595 this_cpu_write(tr->trace_buffer.data->ignore_pid,
596 trace_ignore_this_task(pid_list, current));
597}
598
599static void __ftrace_clear_event_pids(struct trace_array *tr)
600{
601 struct trace_pid_list *pid_list;
602 struct trace_event_file *file;
603 int cpu;
604
605 pid_list = rcu_dereference_protected(tr->filtered_pids,
606 lockdep_is_held(&event_mutex));
607 if (!pid_list)
608 return;
609
610 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
611 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
612
613 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
614 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
615
616 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
617 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
618
619 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
620 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
621
622 list_for_each_entry(file, &tr->events, list) {
623 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
624 }
625
626 for_each_possible_cpu(cpu)
627 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
628
629 rcu_assign_pointer(tr->filtered_pids, NULL);
630
631
632 tracepoint_synchronize_unregister();
633
634 trace_free_pid_list(pid_list);
635}
636
637static void ftrace_clear_event_pids(struct trace_array *tr)
638{
639 mutex_lock(&event_mutex);
640 __ftrace_clear_event_pids(tr);
641 mutex_unlock(&event_mutex);
642}
643
644static void __put_system(struct event_subsystem *system)
645{
646 struct event_filter *filter = system->filter;
647
648 WARN_ON_ONCE(system_refcount(system) == 0);
649 if (system_refcount_dec(system))
650 return;
651
652 list_del(&system->list);
653
654 if (filter) {
655 kfree(filter->filter_string);
656 kfree(filter);
657 }
658 kfree_const(system->name);
659 kfree(system);
660}
661
662static void __get_system(struct event_subsystem *system)
663{
664 WARN_ON_ONCE(system_refcount(system) == 0);
665 system_refcount_inc(system);
666}
667
668static void __get_system_dir(struct trace_subsystem_dir *dir)
669{
670 WARN_ON_ONCE(dir->ref_count == 0);
671 dir->ref_count++;
672 __get_system(dir->subsystem);
673}
674
675static void __put_system_dir(struct trace_subsystem_dir *dir)
676{
677 WARN_ON_ONCE(dir->ref_count == 0);
678
679 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
680
681 __put_system(dir->subsystem);
682 if (!--dir->ref_count)
683 kfree(dir);
684}
685
686static void put_system(struct trace_subsystem_dir *dir)
687{
688 mutex_lock(&event_mutex);
689 __put_system_dir(dir);
690 mutex_unlock(&event_mutex);
691}
692
693static void remove_subsystem(struct trace_subsystem_dir *dir)
694{
695 if (!dir)
696 return;
697
698 if (!--dir->nr_events) {
699 tracefs_remove_recursive(dir->entry);
700 list_del(&dir->list);
701 __put_system_dir(dir);
702 }
703}
704
705static void remove_event_file_dir(struct trace_event_file *file)
706{
707 struct dentry *dir = file->dir;
708 struct dentry *child;
709
710 if (dir) {
711 spin_lock(&dir->d_lock);
712 list_for_each_entry(child, &dir->d_subdirs, d_child) {
713 if (d_really_is_positive(child))
714 d_inode(child)->i_private = NULL;
715 }
716 spin_unlock(&dir->d_lock);
717
718 tracefs_remove_recursive(dir);
719 }
720
721 list_del(&file->list);
722 remove_subsystem(file->system);
723 free_event_filter(file->filter);
724 kmem_cache_free(file_cachep, file);
725}
726
727
728
729
730static int
731__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
732 const char *sub, const char *event, int set)
733{
734 struct trace_event_file *file;
735 struct trace_event_call *call;
736 const char *name;
737 int ret = -EINVAL;
738 int eret = 0;
739
740 list_for_each_entry(file, &tr->events, list) {
741
742 call = file->event_call;
743 name = trace_event_name(call);
744
745 if (!name || !call->class || !call->class->reg)
746 continue;
747
748 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
749 continue;
750
751 if (match &&
752 strcmp(match, name) != 0 &&
753 strcmp(match, call->class->system) != 0)
754 continue;
755
756 if (sub && strcmp(sub, call->class->system) != 0)
757 continue;
758
759 if (event && strcmp(event, name) != 0)
760 continue;
761
762 ret = ftrace_event_enable_disable(file, set);
763
764
765
766
767
768
769 if (ret && !eret)
770 eret = ret;
771
772 ret = eret;
773 }
774
775 return ret;
776}
777
778static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
779 const char *sub, const char *event, int set)
780{
781 int ret;
782
783 mutex_lock(&event_mutex);
784 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
785 mutex_unlock(&event_mutex);
786
787 return ret;
788}
789
790int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
791{
792 char *event = NULL, *sub = NULL, *match;
793 int ret;
794
795
796
797
798
799
800
801
802
803
804
805
806
807 match = strsep(&buf, ":");
808 if (buf) {
809 sub = match;
810 event = buf;
811 match = NULL;
812
813 if (!strlen(sub) || strcmp(sub, "*") == 0)
814 sub = NULL;
815 if (!strlen(event) || strcmp(event, "*") == 0)
816 event = NULL;
817 }
818
819 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
820
821
822 if (buf)
823 *(buf - 1) = ':';
824
825 return ret;
826}
827EXPORT_SYMBOL_GPL(ftrace_set_clr_event);
828
829
830
831
832
833
834
835
836
837
838
839
840
841int trace_set_clr_event(const char *system, const char *event, int set)
842{
843 struct trace_array *tr = top_trace_array();
844
845 if (!tr)
846 return -ENODEV;
847
848 return __ftrace_set_clr_event(tr, NULL, system, event, set);
849}
850EXPORT_SYMBOL_GPL(trace_set_clr_event);
851
852
853#define EVENT_BUF_SIZE 127
854
855static ssize_t
856ftrace_event_write(struct file *file, const char __user *ubuf,
857 size_t cnt, loff_t *ppos)
858{
859 struct trace_parser parser;
860 struct seq_file *m = file->private_data;
861 struct trace_array *tr = m->private;
862 ssize_t read, ret;
863
864 if (!cnt)
865 return 0;
866
867 ret = tracing_update_buffers();
868 if (ret < 0)
869 return ret;
870
871 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
872 return -ENOMEM;
873
874 read = trace_get_user(&parser, ubuf, cnt, ppos);
875
876 if (read >= 0 && trace_parser_loaded((&parser))) {
877 int set = 1;
878
879 if (*parser.buffer == '!')
880 set = 0;
881
882 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
883 if (ret)
884 goto out_put;
885 }
886
887 ret = read;
888
889 out_put:
890 trace_parser_put(&parser);
891
892 return ret;
893}
894
895static void *
896t_next(struct seq_file *m, void *v, loff_t *pos)
897{
898 struct trace_event_file *file = v;
899 struct trace_event_call *call;
900 struct trace_array *tr = m->private;
901
902 (*pos)++;
903
904 list_for_each_entry_continue(file, &tr->events, list) {
905 call = file->event_call;
906
907
908
909
910 if (call->class && call->class->reg &&
911 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
912 return file;
913 }
914
915 return NULL;
916}
917
918static void *t_start(struct seq_file *m, loff_t *pos)
919{
920 struct trace_event_file *file;
921 struct trace_array *tr = m->private;
922 loff_t l;
923
924 mutex_lock(&event_mutex);
925
926 file = list_entry(&tr->events, struct trace_event_file, list);
927 for (l = 0; l <= *pos; ) {
928 file = t_next(m, file, &l);
929 if (!file)
930 break;
931 }
932 return file;
933}
934
935static void *
936s_next(struct seq_file *m, void *v, loff_t *pos)
937{
938 struct trace_event_file *file = v;
939 struct trace_array *tr = m->private;
940
941 (*pos)++;
942
943 list_for_each_entry_continue(file, &tr->events, list) {
944 if (file->flags & EVENT_FILE_FL_ENABLED)
945 return file;
946 }
947
948 return NULL;
949}
950
951static void *s_start(struct seq_file *m, loff_t *pos)
952{
953 struct trace_event_file *file;
954 struct trace_array *tr = m->private;
955 loff_t l;
956
957 mutex_lock(&event_mutex);
958
959 file = list_entry(&tr->events, struct trace_event_file, list);
960 for (l = 0; l <= *pos; ) {
961 file = s_next(m, file, &l);
962 if (!file)
963 break;
964 }
965 return file;
966}
967
968static int t_show(struct seq_file *m, void *v)
969{
970 struct trace_event_file *file = v;
971 struct trace_event_call *call = file->event_call;
972
973 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
974 seq_printf(m, "%s:", call->class->system);
975 seq_printf(m, "%s\n", trace_event_name(call));
976
977 return 0;
978}
979
980static void t_stop(struct seq_file *m, void *p)
981{
982 mutex_unlock(&event_mutex);
983}
984
985static void *
986p_next(struct seq_file *m, void *v, loff_t *pos)
987{
988 struct trace_array *tr = m->private;
989 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
990
991 return trace_pid_next(pid_list, v, pos);
992}
993
994static void *p_start(struct seq_file *m, loff_t *pos)
995 __acquires(RCU)
996{
997 struct trace_pid_list *pid_list;
998 struct trace_array *tr = m->private;
999
1000
1001
1002
1003
1004
1005
1006 mutex_lock(&event_mutex);
1007 rcu_read_lock_sched();
1008
1009 pid_list = rcu_dereference_sched(tr->filtered_pids);
1010
1011 if (!pid_list)
1012 return NULL;
1013
1014 return trace_pid_start(pid_list, pos);
1015}
1016
1017static void p_stop(struct seq_file *m, void *p)
1018 __releases(RCU)
1019{
1020 rcu_read_unlock_sched();
1021 mutex_unlock(&event_mutex);
1022}
1023
1024static ssize_t
1025event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1026 loff_t *ppos)
1027{
1028 struct trace_event_file *file;
1029 unsigned long flags;
1030 char buf[4] = "0";
1031
1032 mutex_lock(&event_mutex);
1033 file = event_file_data(filp);
1034 if (likely(file))
1035 flags = file->flags;
1036 mutex_unlock(&event_mutex);
1037
1038 if (!file)
1039 return -ENODEV;
1040
1041 if (flags & EVENT_FILE_FL_ENABLED &&
1042 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1043 strcpy(buf, "1");
1044
1045 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1046 flags & EVENT_FILE_FL_SOFT_MODE)
1047 strcat(buf, "*");
1048
1049 strcat(buf, "\n");
1050
1051 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1052}
1053
1054static ssize_t
1055event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1056 loff_t *ppos)
1057{
1058 struct trace_event_file *file;
1059 unsigned long val;
1060 int ret;
1061
1062 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1063 if (ret)
1064 return ret;
1065
1066 ret = tracing_update_buffers();
1067 if (ret < 0)
1068 return ret;
1069
1070 switch (val) {
1071 case 0:
1072 case 1:
1073 ret = -ENODEV;
1074 mutex_lock(&event_mutex);
1075 file = event_file_data(filp);
1076 if (likely(file))
1077 ret = ftrace_event_enable_disable(file, val);
1078 mutex_unlock(&event_mutex);
1079 break;
1080
1081 default:
1082 return -EINVAL;
1083 }
1084
1085 *ppos += cnt;
1086
1087 return ret ? ret : cnt;
1088}
1089
1090static ssize_t
1091system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1092 loff_t *ppos)
1093{
1094 const char set_to_char[4] = { '?', '0', '1', 'X' };
1095 struct trace_subsystem_dir *dir = filp->private_data;
1096 struct event_subsystem *system = dir->subsystem;
1097 struct trace_event_call *call;
1098 struct trace_event_file *file;
1099 struct trace_array *tr = dir->tr;
1100 char buf[2];
1101 int set = 0;
1102 int ret;
1103
1104 mutex_lock(&event_mutex);
1105 list_for_each_entry(file, &tr->events, list) {
1106 call = file->event_call;
1107 if (!trace_event_name(call) || !call->class || !call->class->reg)
1108 continue;
1109
1110 if (system && strcmp(call->class->system, system->name) != 0)
1111 continue;
1112
1113
1114
1115
1116
1117
1118 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1119
1120
1121
1122
1123 if (set == 3)
1124 break;
1125 }
1126 mutex_unlock(&event_mutex);
1127
1128 buf[0] = set_to_char[set];
1129 buf[1] = '\n';
1130
1131 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1132
1133 return ret;
1134}
1135
1136static ssize_t
1137system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1138 loff_t *ppos)
1139{
1140 struct trace_subsystem_dir *dir = filp->private_data;
1141 struct event_subsystem *system = dir->subsystem;
1142 const char *name = NULL;
1143 unsigned long val;
1144 ssize_t ret;
1145
1146 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1147 if (ret)
1148 return ret;
1149
1150 ret = tracing_update_buffers();
1151 if (ret < 0)
1152 return ret;
1153
1154 if (val != 0 && val != 1)
1155 return -EINVAL;
1156
1157
1158
1159
1160
1161 if (system)
1162 name = system->name;
1163
1164 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1165 if (ret)
1166 goto out;
1167
1168 ret = cnt;
1169
1170out:
1171 *ppos += cnt;
1172
1173 return ret;
1174}
1175
1176enum {
1177 FORMAT_HEADER = 1,
1178 FORMAT_FIELD_SEPERATOR = 2,
1179 FORMAT_PRINTFMT = 3,
1180};
1181
1182static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1183{
1184 struct trace_event_call *call = event_file_data(m->private);
1185 struct list_head *common_head = &ftrace_common_fields;
1186 struct list_head *head = trace_get_fields(call);
1187 struct list_head *node = v;
1188
1189 (*pos)++;
1190
1191 switch ((unsigned long)v) {
1192 case FORMAT_HEADER:
1193 node = common_head;
1194 break;
1195
1196 case FORMAT_FIELD_SEPERATOR:
1197 node = head;
1198 break;
1199
1200 case FORMAT_PRINTFMT:
1201
1202 return NULL;
1203 }
1204
1205 node = node->prev;
1206 if (node == common_head)
1207 return (void *)FORMAT_FIELD_SEPERATOR;
1208 else if (node == head)
1209 return (void *)FORMAT_PRINTFMT;
1210 else
1211 return node;
1212}
1213
1214static int f_show(struct seq_file *m, void *v)
1215{
1216 struct trace_event_call *call = event_file_data(m->private);
1217 struct ftrace_event_field *field;
1218 const char *array_descriptor;
1219
1220 switch ((unsigned long)v) {
1221 case FORMAT_HEADER:
1222 seq_printf(m, "name: %s\n", trace_event_name(call));
1223 seq_printf(m, "ID: %d\n", call->event.type);
1224 seq_puts(m, "format:\n");
1225 return 0;
1226
1227 case FORMAT_FIELD_SEPERATOR:
1228 seq_putc(m, '\n');
1229 return 0;
1230
1231 case FORMAT_PRINTFMT:
1232 seq_printf(m, "\nprint fmt: %s\n",
1233 call->print_fmt);
1234 return 0;
1235 }
1236
1237 field = list_entry(v, struct ftrace_event_field, link);
1238
1239
1240
1241
1242
1243
1244
1245 array_descriptor = strchr(field->type, '[');
1246
1247 if (str_has_prefix(field->type, "__data_loc"))
1248 array_descriptor = NULL;
1249
1250 if (!array_descriptor)
1251 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1252 field->type, field->name, field->offset,
1253 field->size, !!field->is_signed);
1254 else
1255 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1256 (int)(array_descriptor - field->type),
1257 field->type, field->name,
1258 array_descriptor, field->offset,
1259 field->size, !!field->is_signed);
1260
1261 return 0;
1262}
1263
1264static void *f_start(struct seq_file *m, loff_t *pos)
1265{
1266 void *p = (void *)FORMAT_HEADER;
1267 loff_t l = 0;
1268
1269
1270 mutex_lock(&event_mutex);
1271 if (!event_file_data(m->private))
1272 return ERR_PTR(-ENODEV);
1273
1274 while (l < *pos && p)
1275 p = f_next(m, p, &l);
1276
1277 return p;
1278}
1279
1280static void f_stop(struct seq_file *m, void *p)
1281{
1282 mutex_unlock(&event_mutex);
1283}
1284
1285static const struct seq_operations trace_format_seq_ops = {
1286 .start = f_start,
1287 .next = f_next,
1288 .stop = f_stop,
1289 .show = f_show,
1290};
1291
1292static int trace_format_open(struct inode *inode, struct file *file)
1293{
1294 struct seq_file *m;
1295 int ret;
1296
1297 ret = seq_open(file, &trace_format_seq_ops);
1298 if (ret < 0)
1299 return ret;
1300
1301 m = file->private_data;
1302 m->private = file;
1303
1304 return 0;
1305}
1306
1307static ssize_t
1308event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1309{
1310 int id = (long)event_file_data(filp);
1311 char buf[32];
1312 int len;
1313
1314 if (unlikely(!id))
1315 return -ENODEV;
1316
1317 len = sprintf(buf, "%d\n", id);
1318
1319 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1320}
1321
1322static ssize_t
1323event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1324 loff_t *ppos)
1325{
1326 struct trace_event_file *file;
1327 struct trace_seq *s;
1328 int r = -ENODEV;
1329
1330 if (*ppos)
1331 return 0;
1332
1333 s = kmalloc(sizeof(*s), GFP_KERNEL);
1334
1335 if (!s)
1336 return -ENOMEM;
1337
1338 trace_seq_init(s);
1339
1340 mutex_lock(&event_mutex);
1341 file = event_file_data(filp);
1342 if (file)
1343 print_event_filter(file, s);
1344 mutex_unlock(&event_mutex);
1345
1346 if (file)
1347 r = simple_read_from_buffer(ubuf, cnt, ppos,
1348 s->buffer, trace_seq_used(s));
1349
1350 kfree(s);
1351
1352 return r;
1353}
1354
1355static ssize_t
1356event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1357 loff_t *ppos)
1358{
1359 struct trace_event_file *file;
1360 char *buf;
1361 int err = -ENODEV;
1362
1363 if (cnt >= PAGE_SIZE)
1364 return -EINVAL;
1365
1366 buf = memdup_user_nul(ubuf, cnt);
1367 if (IS_ERR(buf))
1368 return PTR_ERR(buf);
1369
1370 mutex_lock(&event_mutex);
1371 file = event_file_data(filp);
1372 if (file)
1373 err = apply_event_filter(file, buf);
1374 mutex_unlock(&event_mutex);
1375
1376 kfree(buf);
1377 if (err < 0)
1378 return err;
1379
1380 *ppos += cnt;
1381
1382 return cnt;
1383}
1384
1385static LIST_HEAD(event_subsystems);
1386
1387static int subsystem_open(struct inode *inode, struct file *filp)
1388{
1389 struct event_subsystem *system = NULL;
1390 struct trace_subsystem_dir *dir = NULL;
1391 struct trace_array *tr;
1392 int ret;
1393
1394 if (tracing_is_disabled())
1395 return -ENODEV;
1396
1397
1398 mutex_lock(&event_mutex);
1399 mutex_lock(&trace_types_lock);
1400 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1401 list_for_each_entry(dir, &tr->systems, list) {
1402 if (dir == inode->i_private) {
1403
1404 if (dir->nr_events) {
1405 __get_system_dir(dir);
1406 system = dir->subsystem;
1407 }
1408 goto exit_loop;
1409 }
1410 }
1411 }
1412 exit_loop:
1413 mutex_unlock(&trace_types_lock);
1414 mutex_unlock(&event_mutex);
1415
1416 if (!system)
1417 return -ENODEV;
1418
1419
1420 WARN_ON(!dir);
1421
1422
1423 if (trace_array_get(tr) < 0) {
1424 put_system(dir);
1425 return -ENODEV;
1426 }
1427
1428 ret = tracing_open_generic(inode, filp);
1429 if (ret < 0) {
1430 trace_array_put(tr);
1431 put_system(dir);
1432 }
1433
1434 return ret;
1435}
1436
1437static int system_tr_open(struct inode *inode, struct file *filp)
1438{
1439 struct trace_subsystem_dir *dir;
1440 struct trace_array *tr = inode->i_private;
1441 int ret;
1442
1443 if (tracing_is_disabled())
1444 return -ENODEV;
1445
1446 if (trace_array_get(tr) < 0)
1447 return -ENODEV;
1448
1449
1450 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1451 if (!dir) {
1452 trace_array_put(tr);
1453 return -ENOMEM;
1454 }
1455
1456 dir->tr = tr;
1457
1458 ret = tracing_open_generic(inode, filp);
1459 if (ret < 0) {
1460 trace_array_put(tr);
1461 kfree(dir);
1462 return ret;
1463 }
1464
1465 filp->private_data = dir;
1466
1467 return 0;
1468}
1469
1470static int subsystem_release(struct inode *inode, struct file *file)
1471{
1472 struct trace_subsystem_dir *dir = file->private_data;
1473
1474 trace_array_put(dir->tr);
1475
1476
1477
1478
1479
1480
1481 if (dir->subsystem)
1482 put_system(dir);
1483 else
1484 kfree(dir);
1485
1486 return 0;
1487}
1488
1489static ssize_t
1490subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1491 loff_t *ppos)
1492{
1493 struct trace_subsystem_dir *dir = filp->private_data;
1494 struct event_subsystem *system = dir->subsystem;
1495 struct trace_seq *s;
1496 int r;
1497
1498 if (*ppos)
1499 return 0;
1500
1501 s = kmalloc(sizeof(*s), GFP_KERNEL);
1502 if (!s)
1503 return -ENOMEM;
1504
1505 trace_seq_init(s);
1506
1507 print_subsystem_event_filter(system, s);
1508 r = simple_read_from_buffer(ubuf, cnt, ppos,
1509 s->buffer, trace_seq_used(s));
1510
1511 kfree(s);
1512
1513 return r;
1514}
1515
1516static ssize_t
1517subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1518 loff_t *ppos)
1519{
1520 struct trace_subsystem_dir *dir = filp->private_data;
1521 char *buf;
1522 int err;
1523
1524 if (cnt >= PAGE_SIZE)
1525 return -EINVAL;
1526
1527 buf = memdup_user_nul(ubuf, cnt);
1528 if (IS_ERR(buf))
1529 return PTR_ERR(buf);
1530
1531 err = apply_subsystem_event_filter(dir, buf);
1532 kfree(buf);
1533 if (err < 0)
1534 return err;
1535
1536 *ppos += cnt;
1537
1538 return cnt;
1539}
1540
1541static ssize_t
1542show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1543{
1544 int (*func)(struct trace_seq *s) = filp->private_data;
1545 struct trace_seq *s;
1546 int r;
1547
1548 if (*ppos)
1549 return 0;
1550
1551 s = kmalloc(sizeof(*s), GFP_KERNEL);
1552 if (!s)
1553 return -ENOMEM;
1554
1555 trace_seq_init(s);
1556
1557 func(s);
1558 r = simple_read_from_buffer(ubuf, cnt, ppos,
1559 s->buffer, trace_seq_used(s));
1560
1561 kfree(s);
1562
1563 return r;
1564}
1565
1566static void ignore_task_cpu(void *data)
1567{
1568 struct trace_array *tr = data;
1569 struct trace_pid_list *pid_list;
1570
1571
1572
1573
1574
1575 pid_list = rcu_dereference_protected(tr->filtered_pids,
1576 mutex_is_locked(&event_mutex));
1577
1578 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1579 trace_ignore_this_task(pid_list, current));
1580}
1581
1582static ssize_t
1583ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1584 size_t cnt, loff_t *ppos)
1585{
1586 struct seq_file *m = filp->private_data;
1587 struct trace_array *tr = m->private;
1588 struct trace_pid_list *filtered_pids = NULL;
1589 struct trace_pid_list *pid_list;
1590 struct trace_event_file *file;
1591 ssize_t ret;
1592
1593 if (!cnt)
1594 return 0;
1595
1596 ret = tracing_update_buffers();
1597 if (ret < 0)
1598 return ret;
1599
1600 mutex_lock(&event_mutex);
1601
1602 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1603 lockdep_is_held(&event_mutex));
1604
1605 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1606 if (ret < 0)
1607 goto out;
1608
1609 rcu_assign_pointer(tr->filtered_pids, pid_list);
1610
1611 list_for_each_entry(file, &tr->events, list) {
1612 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1613 }
1614
1615 if (filtered_pids) {
1616 tracepoint_synchronize_unregister();
1617 trace_free_pid_list(filtered_pids);
1618 } else if (pid_list) {
1619
1620
1621
1622
1623
1624
1625 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1626 tr, INT_MAX);
1627 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1628 tr, 0);
1629
1630 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1631 tr, INT_MAX);
1632 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1633 tr, 0);
1634
1635 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1636 tr, INT_MAX);
1637 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1638 tr, 0);
1639
1640 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1641 tr, INT_MAX);
1642 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1643 tr, 0);
1644 }
1645
1646
1647
1648
1649
1650
1651 on_each_cpu(ignore_task_cpu, tr, 1);
1652
1653 out:
1654 mutex_unlock(&event_mutex);
1655
1656 if (ret > 0)
1657 *ppos += ret;
1658
1659 return ret;
1660}
1661
1662static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1663static int ftrace_event_set_open(struct inode *inode, struct file *file);
1664static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1665static int ftrace_event_release(struct inode *inode, struct file *file);
1666
1667static const struct seq_operations show_event_seq_ops = {
1668 .start = t_start,
1669 .next = t_next,
1670 .show = t_show,
1671 .stop = t_stop,
1672};
1673
1674static const struct seq_operations show_set_event_seq_ops = {
1675 .start = s_start,
1676 .next = s_next,
1677 .show = t_show,
1678 .stop = t_stop,
1679};
1680
1681static const struct seq_operations show_set_pid_seq_ops = {
1682 .start = p_start,
1683 .next = p_next,
1684 .show = trace_pid_show,
1685 .stop = p_stop,
1686};
1687
1688static const struct file_operations ftrace_avail_fops = {
1689 .open = ftrace_event_avail_open,
1690 .read = seq_read,
1691 .llseek = seq_lseek,
1692 .release = seq_release,
1693};
1694
1695static const struct file_operations ftrace_set_event_fops = {
1696 .open = ftrace_event_set_open,
1697 .read = seq_read,
1698 .write = ftrace_event_write,
1699 .llseek = seq_lseek,
1700 .release = ftrace_event_release,
1701};
1702
1703static const struct file_operations ftrace_set_event_pid_fops = {
1704 .open = ftrace_event_set_pid_open,
1705 .read = seq_read,
1706 .write = ftrace_event_pid_write,
1707 .llseek = seq_lseek,
1708 .release = ftrace_event_release,
1709};
1710
1711static const struct file_operations ftrace_enable_fops = {
1712 .open = tracing_open_generic,
1713 .read = event_enable_read,
1714 .write = event_enable_write,
1715 .llseek = default_llseek,
1716};
1717
1718static const struct file_operations ftrace_event_format_fops = {
1719 .open = trace_format_open,
1720 .read = seq_read,
1721 .llseek = seq_lseek,
1722 .release = seq_release,
1723};
1724
1725static const struct file_operations ftrace_event_id_fops = {
1726 .read = event_id_read,
1727 .llseek = default_llseek,
1728};
1729
1730static const struct file_operations ftrace_event_filter_fops = {
1731 .open = tracing_open_generic,
1732 .read = event_filter_read,
1733 .write = event_filter_write,
1734 .llseek = default_llseek,
1735};
1736
1737static const struct file_operations ftrace_subsystem_filter_fops = {
1738 .open = subsystem_open,
1739 .read = subsystem_filter_read,
1740 .write = subsystem_filter_write,
1741 .llseek = default_llseek,
1742 .release = subsystem_release,
1743};
1744
1745static const struct file_operations ftrace_system_enable_fops = {
1746 .open = subsystem_open,
1747 .read = system_enable_read,
1748 .write = system_enable_write,
1749 .llseek = default_llseek,
1750 .release = subsystem_release,
1751};
1752
1753static const struct file_operations ftrace_tr_enable_fops = {
1754 .open = system_tr_open,
1755 .read = system_enable_read,
1756 .write = system_enable_write,
1757 .llseek = default_llseek,
1758 .release = subsystem_release,
1759};
1760
1761static const struct file_operations ftrace_show_header_fops = {
1762 .open = tracing_open_generic,
1763 .read = show_header,
1764 .llseek = default_llseek,
1765};
1766
1767static int
1768ftrace_event_open(struct inode *inode, struct file *file,
1769 const struct seq_operations *seq_ops)
1770{
1771 struct seq_file *m;
1772 int ret;
1773
1774 ret = seq_open(file, seq_ops);
1775 if (ret < 0)
1776 return ret;
1777 m = file->private_data;
1778
1779 m->private = inode->i_private;
1780
1781 return ret;
1782}
1783
1784static int ftrace_event_release(struct inode *inode, struct file *file)
1785{
1786 struct trace_array *tr = inode->i_private;
1787
1788 trace_array_put(tr);
1789
1790 return seq_release(inode, file);
1791}
1792
1793static int
1794ftrace_event_avail_open(struct inode *inode, struct file *file)
1795{
1796 const struct seq_operations *seq_ops = &show_event_seq_ops;
1797
1798 return ftrace_event_open(inode, file, seq_ops);
1799}
1800
1801static int
1802ftrace_event_set_open(struct inode *inode, struct file *file)
1803{
1804 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1805 struct trace_array *tr = inode->i_private;
1806 int ret;
1807
1808 if (trace_array_get(tr) < 0)
1809 return -ENODEV;
1810
1811 if ((file->f_mode & FMODE_WRITE) &&
1812 (file->f_flags & O_TRUNC))
1813 ftrace_clear_events(tr);
1814
1815 ret = ftrace_event_open(inode, file, seq_ops);
1816 if (ret < 0)
1817 trace_array_put(tr);
1818 return ret;
1819}
1820
1821static int
1822ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1823{
1824 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1825 struct trace_array *tr = inode->i_private;
1826 int ret;
1827
1828 if (trace_array_get(tr) < 0)
1829 return -ENODEV;
1830
1831 if ((file->f_mode & FMODE_WRITE) &&
1832 (file->f_flags & O_TRUNC))
1833 ftrace_clear_event_pids(tr);
1834
1835 ret = ftrace_event_open(inode, file, seq_ops);
1836 if (ret < 0)
1837 trace_array_put(tr);
1838 return ret;
1839}
1840
1841static struct event_subsystem *
1842create_new_subsystem(const char *name)
1843{
1844 struct event_subsystem *system;
1845
1846
1847 system = kmalloc(sizeof(*system), GFP_KERNEL);
1848 if (!system)
1849 return NULL;
1850
1851 system->ref_count = 1;
1852
1853
1854 system->name = kstrdup_const(name, GFP_KERNEL);
1855 if (!system->name)
1856 goto out_free;
1857
1858 system->filter = NULL;
1859
1860 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1861 if (!system->filter)
1862 goto out_free;
1863
1864 list_add(&system->list, &event_subsystems);
1865
1866 return system;
1867
1868 out_free:
1869 kfree_const(system->name);
1870 kfree(system);
1871 return NULL;
1872}
1873
1874static struct dentry *
1875event_subsystem_dir(struct trace_array *tr, const char *name,
1876 struct trace_event_file *file, struct dentry *parent)
1877{
1878 struct trace_subsystem_dir *dir;
1879 struct event_subsystem *system;
1880 struct dentry *entry;
1881
1882
1883 list_for_each_entry(dir, &tr->systems, list) {
1884 system = dir->subsystem;
1885 if (strcmp(system->name, name) == 0) {
1886 dir->nr_events++;
1887 file->system = dir;
1888 return dir->entry;
1889 }
1890 }
1891
1892
1893 list_for_each_entry(system, &event_subsystems, list) {
1894 if (strcmp(system->name, name) == 0)
1895 break;
1896 }
1897
1898 if (&system->list == &event_subsystems)
1899 system = NULL;
1900
1901 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1902 if (!dir)
1903 goto out_fail;
1904
1905 if (!system) {
1906 system = create_new_subsystem(name);
1907 if (!system)
1908 goto out_free;
1909 } else
1910 __get_system(system);
1911
1912 dir->entry = tracefs_create_dir(name, parent);
1913 if (!dir->entry) {
1914 pr_warn("Failed to create system directory %s\n", name);
1915 __put_system(system);
1916 goto out_free;
1917 }
1918
1919 dir->tr = tr;
1920 dir->ref_count = 1;
1921 dir->nr_events = 1;
1922 dir->subsystem = system;
1923 file->system = dir;
1924
1925 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1926 &ftrace_subsystem_filter_fops);
1927 if (!entry) {
1928 kfree(system->filter);
1929 system->filter = NULL;
1930 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1931 }
1932
1933 trace_create_file("enable", 0644, dir->entry, dir,
1934 &ftrace_system_enable_fops);
1935
1936 list_add(&dir->list, &tr->systems);
1937
1938 return dir->entry;
1939
1940 out_free:
1941 kfree(dir);
1942 out_fail:
1943
1944 if (!dir || !system)
1945 pr_warn("No memory to create event subsystem %s\n", name);
1946 return NULL;
1947}
1948
1949static int
1950event_create_dir(struct dentry *parent, struct trace_event_file *file)
1951{
1952 struct trace_event_call *call = file->event_call;
1953 struct trace_array *tr = file->tr;
1954 struct list_head *head;
1955 struct dentry *d_events;
1956 const char *name;
1957 int ret;
1958
1959
1960
1961
1962
1963 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1964 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1965 if (!d_events)
1966 return -ENOMEM;
1967 } else
1968 d_events = parent;
1969
1970 name = trace_event_name(call);
1971 file->dir = tracefs_create_dir(name, d_events);
1972 if (!file->dir) {
1973 pr_warn("Could not create tracefs '%s' directory\n", name);
1974 return -1;
1975 }
1976
1977 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1978 trace_create_file("enable", 0644, file->dir, file,
1979 &ftrace_enable_fops);
1980
1981#ifdef CONFIG_PERF_EVENTS
1982 if (call->event.type && call->class->reg)
1983 trace_create_file("id", 0444, file->dir,
1984 (void *)(long)call->event.type,
1985 &ftrace_event_id_fops);
1986#endif
1987
1988
1989
1990
1991
1992 head = trace_get_fields(call);
1993 if (list_empty(head)) {
1994 ret = call->class->define_fields(call);
1995 if (ret < 0) {
1996 pr_warn("Could not initialize trace point events/%s\n",
1997 name);
1998 return -1;
1999 }
2000 }
2001
2002
2003
2004
2005
2006 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2007 trace_create_file("filter", 0644, file->dir, file,
2008 &ftrace_event_filter_fops);
2009
2010 trace_create_file("trigger", 0644, file->dir, file,
2011 &event_trigger_fops);
2012 }
2013
2014#ifdef CONFIG_HIST_TRIGGERS
2015 trace_create_file("hist", 0444, file->dir, file,
2016 &event_hist_fops);
2017#endif
2018 trace_create_file("format", 0444, file->dir, call,
2019 &ftrace_event_format_fops);
2020
2021 return 0;
2022}
2023
2024static void remove_event_from_tracers(struct trace_event_call *call)
2025{
2026 struct trace_event_file *file;
2027 struct trace_array *tr;
2028
2029 do_for_each_event_file_safe(tr, file) {
2030 if (file->event_call != call)
2031 continue;
2032
2033 remove_event_file_dir(file);
2034
2035
2036
2037
2038
2039
2040 break;
2041 } while_for_each_event_file();
2042}
2043
2044static void event_remove(struct trace_event_call *call)
2045{
2046 struct trace_array *tr;
2047 struct trace_event_file *file;
2048
2049 do_for_each_event_file(tr, file) {
2050 if (file->event_call != call)
2051 continue;
2052
2053 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2054 tr->clear_trace = true;
2055
2056 ftrace_event_enable_disable(file, 0);
2057
2058
2059
2060
2061
2062
2063 break;
2064 } while_for_each_event_file();
2065
2066 if (call->event.funcs)
2067 __unregister_trace_event(&call->event);
2068 remove_event_from_tracers(call);
2069 list_del(&call->list);
2070}
2071
2072static int event_init(struct trace_event_call *call)
2073{
2074 int ret = 0;
2075 const char *name;
2076
2077 name = trace_event_name(call);
2078 if (WARN_ON(!name))
2079 return -EINVAL;
2080
2081 if (call->class->raw_init) {
2082 ret = call->class->raw_init(call);
2083 if (ret < 0 && ret != -ENOSYS)
2084 pr_warn("Could not initialize trace events/%s\n", name);
2085 }
2086
2087 return ret;
2088}
2089
2090static int
2091__register_event(struct trace_event_call *call, struct module *mod)
2092{
2093 int ret;
2094
2095 ret = event_init(call);
2096 if (ret < 0)
2097 return ret;
2098
2099 list_add(&call->list, &ftrace_events);
2100 call->mod = mod;
2101
2102 return 0;
2103}
2104
2105static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2106{
2107 int rlen;
2108 int elen;
2109
2110
2111 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2112
2113 if (len < elen)
2114 return NULL;
2115
2116 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2117
2118
2119 rlen = strlen(ptr + len);
2120 memmove(ptr + elen, ptr + len, rlen);
2121
2122 ptr[elen + rlen] = 0;
2123
2124 return ptr + elen;
2125}
2126
2127static void update_event_printk(struct trace_event_call *call,
2128 struct trace_eval_map *map)
2129{
2130 char *ptr;
2131 int quote = 0;
2132 int len = strlen(map->eval_string);
2133
2134 for (ptr = call->print_fmt; *ptr; ptr++) {
2135 if (*ptr == '\\') {
2136 ptr++;
2137
2138 if (!*ptr)
2139 break;
2140 continue;
2141 }
2142 if (*ptr == '"') {
2143 quote ^= 1;
2144 continue;
2145 }
2146 if (quote)
2147 continue;
2148 if (isdigit(*ptr)) {
2149
2150 do {
2151 ptr++;
2152
2153 } while (isalnum(*ptr));
2154 if (!*ptr)
2155 break;
2156
2157
2158
2159
2160 continue;
2161 }
2162 if (isalpha(*ptr) || *ptr == '_') {
2163 if (strncmp(map->eval_string, ptr, len) == 0 &&
2164 !isalnum(ptr[len]) && ptr[len] != '_') {
2165 ptr = eval_replace(ptr, map, len);
2166
2167 if (WARN_ON_ONCE(!ptr))
2168 return;
2169
2170
2171
2172
2173
2174
2175
2176 continue;
2177 }
2178 skip_more:
2179 do {
2180 ptr++;
2181 } while (isalnum(*ptr) || *ptr == '_');
2182 if (!*ptr)
2183 break;
2184
2185
2186
2187
2188 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2189 ptr += *ptr == '.' ? 1 : 2;
2190 if (!*ptr)
2191 break;
2192 goto skip_more;
2193 }
2194
2195
2196
2197
2198 continue;
2199 }
2200 }
2201}
2202
2203void trace_event_eval_update(struct trace_eval_map **map, int len)
2204{
2205 struct trace_event_call *call, *p;
2206 const char *last_system = NULL;
2207 bool first = false;
2208 int last_i;
2209 int i;
2210
2211 down_write(&trace_event_sem);
2212 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2213
2214 if (!last_system || call->class->system != last_system) {
2215 first = true;
2216 last_i = 0;
2217 last_system = call->class->system;
2218 }
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230 for (i = last_i; i < len; i++) {
2231 if (call->class->system == map[i]->system) {
2232
2233 if (first) {
2234 last_i = i;
2235 first = false;
2236 }
2237 update_event_printk(call, map[i]);
2238 }
2239 }
2240 }
2241 up_write(&trace_event_sem);
2242}
2243
2244static struct trace_event_file *
2245trace_create_new_event(struct trace_event_call *call,
2246 struct trace_array *tr)
2247{
2248 struct trace_event_file *file;
2249
2250 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2251 if (!file)
2252 return NULL;
2253
2254 file->event_call = call;
2255 file->tr = tr;
2256 atomic_set(&file->sm_ref, 0);
2257 atomic_set(&file->tm_ref, 0);
2258 INIT_LIST_HEAD(&file->triggers);
2259 list_add(&file->list, &tr->events);
2260
2261 return file;
2262}
2263
2264
2265static int
2266__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2267{
2268 struct trace_event_file *file;
2269
2270 file = trace_create_new_event(call, tr);
2271 if (!file)
2272 return -ENOMEM;
2273
2274 return event_create_dir(tr->event_dir, file);
2275}
2276
2277
2278
2279
2280
2281
2282static __init int
2283__trace_early_add_new_event(struct trace_event_call *call,
2284 struct trace_array *tr)
2285{
2286 struct trace_event_file *file;
2287
2288 file = trace_create_new_event(call, tr);
2289 if (!file)
2290 return -ENOMEM;
2291
2292 return 0;
2293}
2294
2295struct ftrace_module_file_ops;
2296static void __add_event_to_tracers(struct trace_event_call *call);
2297
2298
2299int trace_add_event_call(struct trace_event_call *call)
2300{
2301 int ret;
2302 lockdep_assert_held(&event_mutex);
2303
2304 mutex_lock(&trace_types_lock);
2305
2306 ret = __register_event(call, NULL);
2307 if (ret >= 0)
2308 __add_event_to_tracers(call);
2309
2310 mutex_unlock(&trace_types_lock);
2311 return ret;
2312}
2313
2314
2315
2316
2317
2318static void __trace_remove_event_call(struct trace_event_call *call)
2319{
2320 event_remove(call);
2321 trace_destroy_fields(call);
2322 free_event_filter(call->filter);
2323 call->filter = NULL;
2324}
2325
2326static int probe_remove_event_call(struct trace_event_call *call)
2327{
2328 struct trace_array *tr;
2329 struct trace_event_file *file;
2330
2331#ifdef CONFIG_PERF_EVENTS
2332 if (call->perf_refcount)
2333 return -EBUSY;
2334#endif
2335 do_for_each_event_file(tr, file) {
2336 if (file->event_call != call)
2337 continue;
2338
2339
2340
2341
2342
2343 if (file->flags & EVENT_FILE_FL_ENABLED)
2344 return -EBUSY;
2345
2346
2347
2348
2349
2350
2351 break;
2352 } while_for_each_event_file();
2353
2354 __trace_remove_event_call(call);
2355
2356 return 0;
2357}
2358
2359
2360int trace_remove_event_call(struct trace_event_call *call)
2361{
2362 int ret;
2363
2364 lockdep_assert_held(&event_mutex);
2365
2366 mutex_lock(&trace_types_lock);
2367 down_write(&trace_event_sem);
2368 ret = probe_remove_event_call(call);
2369 up_write(&trace_event_sem);
2370 mutex_unlock(&trace_types_lock);
2371
2372 return ret;
2373}
2374
2375#define for_each_event(event, start, end) \
2376 for (event = start; \
2377 (unsigned long)event < (unsigned long)end; \
2378 event++)
2379
2380#ifdef CONFIG_MODULES
2381
2382static void trace_module_add_events(struct module *mod)
2383{
2384 struct trace_event_call **call, **start, **end;
2385
2386 if (!mod->num_trace_events)
2387 return;
2388
2389
2390 if (trace_module_has_bad_taint(mod)) {
2391 pr_err("%s: module has bad taint, not creating trace events\n",
2392 mod->name);
2393 return;
2394 }
2395
2396 start = mod->trace_events;
2397 end = mod->trace_events + mod->num_trace_events;
2398
2399 for_each_event(call, start, end) {
2400 __register_event(*call, mod);
2401 __add_event_to_tracers(*call);
2402 }
2403}
2404
2405static void trace_module_remove_events(struct module *mod)
2406{
2407 struct trace_event_call *call, *p;
2408
2409 down_write(&trace_event_sem);
2410 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2411 if (call->mod == mod)
2412 __trace_remove_event_call(call);
2413 }
2414 up_write(&trace_event_sem);
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424 tracing_reset_all_online_cpus();
2425}
2426
2427static int trace_module_notify(struct notifier_block *self,
2428 unsigned long val, void *data)
2429{
2430 struct module *mod = data;
2431
2432 mutex_lock(&event_mutex);
2433 mutex_lock(&trace_types_lock);
2434 switch (val) {
2435 case MODULE_STATE_COMING:
2436 trace_module_add_events(mod);
2437 break;
2438 case MODULE_STATE_GOING:
2439 trace_module_remove_events(mod);
2440 break;
2441 }
2442 mutex_unlock(&trace_types_lock);
2443 mutex_unlock(&event_mutex);
2444
2445 return 0;
2446}
2447
2448static struct notifier_block trace_module_nb = {
2449 .notifier_call = trace_module_notify,
2450 .priority = 1,
2451};
2452#endif
2453
2454
2455static void
2456__trace_add_event_dirs(struct trace_array *tr)
2457{
2458 struct trace_event_call *call;
2459 int ret;
2460
2461 list_for_each_entry(call, &ftrace_events, list) {
2462 ret = __trace_add_new_event(call, tr);
2463 if (ret < 0)
2464 pr_warn("Could not create directory for event %s\n",
2465 trace_event_name(call));
2466 }
2467}
2468
2469
2470struct trace_event_file *
2471__find_event_file(struct trace_array *tr, const char *system, const char *event)
2472{
2473 struct trace_event_file *file;
2474 struct trace_event_call *call;
2475 const char *name;
2476
2477 list_for_each_entry(file, &tr->events, list) {
2478
2479 call = file->event_call;
2480 name = trace_event_name(call);
2481
2482 if (!name || !call->class)
2483 continue;
2484
2485 if (strcmp(event, name) == 0 &&
2486 strcmp(system, call->class->system) == 0)
2487 return file;
2488 }
2489 return NULL;
2490}
2491
2492
2493struct trace_event_file *
2494find_event_file(struct trace_array *tr, const char *system, const char *event)
2495{
2496 struct trace_event_file *file;
2497
2498 file = __find_event_file(tr, system, event);
2499 if (!file || !file->event_call->class->reg ||
2500 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2501 return NULL;
2502
2503 return file;
2504}
2505
2506#ifdef CONFIG_DYNAMIC_FTRACE
2507
2508
2509#define ENABLE_EVENT_STR "enable_event"
2510#define DISABLE_EVENT_STR "disable_event"
2511
2512struct event_probe_data {
2513 struct trace_event_file *file;
2514 unsigned long count;
2515 int ref;
2516 bool enable;
2517};
2518
2519static void update_event_probe(struct event_probe_data *data)
2520{
2521 if (data->enable)
2522 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2523 else
2524 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2525}
2526
2527static void
2528event_enable_probe(unsigned long ip, unsigned long parent_ip,
2529 struct trace_array *tr, struct ftrace_probe_ops *ops,
2530 void *data)
2531{
2532 struct ftrace_func_mapper *mapper = data;
2533 struct event_probe_data *edata;
2534 void **pdata;
2535
2536 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2537 if (!pdata || !*pdata)
2538 return;
2539
2540 edata = *pdata;
2541 update_event_probe(edata);
2542}
2543
2544static void
2545event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2546 struct trace_array *tr, struct ftrace_probe_ops *ops,
2547 void *data)
2548{
2549 struct ftrace_func_mapper *mapper = data;
2550 struct event_probe_data *edata;
2551 void **pdata;
2552
2553 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2554 if (!pdata || !*pdata)
2555 return;
2556
2557 edata = *pdata;
2558
2559 if (!edata->count)
2560 return;
2561
2562
2563 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2564 return;
2565
2566 if (edata->count != -1)
2567 (edata->count)--;
2568
2569 update_event_probe(edata);
2570}
2571
2572static int
2573event_enable_print(struct seq_file *m, unsigned long ip,
2574 struct ftrace_probe_ops *ops, void *data)
2575{
2576 struct ftrace_func_mapper *mapper = data;
2577 struct event_probe_data *edata;
2578 void **pdata;
2579
2580 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2581
2582 if (WARN_ON_ONCE(!pdata || !*pdata))
2583 return 0;
2584
2585 edata = *pdata;
2586
2587 seq_printf(m, "%ps:", (void *)ip);
2588
2589 seq_printf(m, "%s:%s:%s",
2590 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2591 edata->file->event_call->class->system,
2592 trace_event_name(edata->file->event_call));
2593
2594 if (edata->count == -1)
2595 seq_puts(m, ":unlimited\n");
2596 else
2597 seq_printf(m, ":count=%ld\n", edata->count);
2598
2599 return 0;
2600}
2601
2602static int
2603event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2604 unsigned long ip, void *init_data, void **data)
2605{
2606 struct ftrace_func_mapper *mapper = *data;
2607 struct event_probe_data *edata = init_data;
2608 int ret;
2609
2610 if (!mapper) {
2611 mapper = allocate_ftrace_func_mapper();
2612 if (!mapper)
2613 return -ENODEV;
2614 *data = mapper;
2615 }
2616
2617 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2618 if (ret < 0)
2619 return ret;
2620
2621 edata->ref++;
2622
2623 return 0;
2624}
2625
2626static int free_probe_data(void *data)
2627{
2628 struct event_probe_data *edata = data;
2629
2630 edata->ref--;
2631 if (!edata->ref) {
2632
2633 __ftrace_event_enable_disable(edata->file, 0, 1);
2634 module_put(edata->file->event_call->mod);
2635 kfree(edata);
2636 }
2637 return 0;
2638}
2639
2640static void
2641event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2642 unsigned long ip, void *data)
2643{
2644 struct ftrace_func_mapper *mapper = data;
2645 struct event_probe_data *edata;
2646
2647 if (!ip) {
2648 if (!mapper)
2649 return;
2650 free_ftrace_func_mapper(mapper, free_probe_data);
2651 return;
2652 }
2653
2654 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2655
2656 if (WARN_ON_ONCE(!edata))
2657 return;
2658
2659 if (WARN_ON_ONCE(edata->ref <= 0))
2660 return;
2661
2662 free_probe_data(edata);
2663}
2664
2665static struct ftrace_probe_ops event_enable_probe_ops = {
2666 .func = event_enable_probe,
2667 .print = event_enable_print,
2668 .init = event_enable_init,
2669 .free = event_enable_free,
2670};
2671
2672static struct ftrace_probe_ops event_enable_count_probe_ops = {
2673 .func = event_enable_count_probe,
2674 .print = event_enable_print,
2675 .init = event_enable_init,
2676 .free = event_enable_free,
2677};
2678
2679static struct ftrace_probe_ops event_disable_probe_ops = {
2680 .func = event_enable_probe,
2681 .print = event_enable_print,
2682 .init = event_enable_init,
2683 .free = event_enable_free,
2684};
2685
2686static struct ftrace_probe_ops event_disable_count_probe_ops = {
2687 .func = event_enable_count_probe,
2688 .print = event_enable_print,
2689 .init = event_enable_init,
2690 .free = event_enable_free,
2691};
2692
2693static int
2694event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
2695 char *glob, char *cmd, char *param, int enabled)
2696{
2697 struct trace_event_file *file;
2698 struct ftrace_probe_ops *ops;
2699 struct event_probe_data *data;
2700 const char *system;
2701 const char *event;
2702 char *number;
2703 bool enable;
2704 int ret;
2705
2706 if (!tr)
2707 return -ENODEV;
2708
2709
2710 if (!enabled || !param)
2711 return -EINVAL;
2712
2713 system = strsep(¶m, ":");
2714 if (!param)
2715 return -EINVAL;
2716
2717 event = strsep(¶m, ":");
2718
2719 mutex_lock(&event_mutex);
2720
2721 ret = -EINVAL;
2722 file = find_event_file(tr, system, event);
2723 if (!file)
2724 goto out;
2725
2726 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2727
2728 if (enable)
2729 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2730 else
2731 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2732
2733 if (glob[0] == '!') {
2734 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
2735 goto out;
2736 }
2737
2738 ret = -ENOMEM;
2739
2740 data = kzalloc(sizeof(*data), GFP_KERNEL);
2741 if (!data)
2742 goto out;
2743
2744 data->enable = enable;
2745 data->count = -1;
2746 data->file = file;
2747
2748 if (!param)
2749 goto out_reg;
2750
2751 number = strsep(¶m, ":");
2752
2753 ret = -EINVAL;
2754 if (!strlen(number))
2755 goto out_free;
2756
2757
2758
2759
2760
2761 ret = kstrtoul(number, 0, &data->count);
2762 if (ret)
2763 goto out_free;
2764
2765 out_reg:
2766
2767 ret = try_module_get(file->event_call->mod);
2768 if (!ret) {
2769 ret = -EBUSY;
2770 goto out_free;
2771 }
2772
2773 ret = __ftrace_event_enable_disable(file, 1, 1);
2774 if (ret < 0)
2775 goto out_put;
2776
2777 ret = register_ftrace_function_probe(glob, tr, ops, data);
2778
2779
2780
2781
2782
2783 if (!ret) {
2784 ret = -ENOENT;
2785 goto out_disable;
2786 } else if (ret < 0)
2787 goto out_disable;
2788
2789 ret = 0;
2790 out:
2791 mutex_unlock(&event_mutex);
2792 return ret;
2793
2794 out_disable:
2795 __ftrace_event_enable_disable(file, 0, 1);
2796 out_put:
2797 module_put(file->event_call->mod);
2798 out_free:
2799 kfree(data);
2800 goto out;
2801}
2802
2803static struct ftrace_func_command event_enable_cmd = {
2804 .name = ENABLE_EVENT_STR,
2805 .func = event_enable_func,
2806};
2807
2808static struct ftrace_func_command event_disable_cmd = {
2809 .name = DISABLE_EVENT_STR,
2810 .func = event_enable_func,
2811};
2812
2813static __init int register_event_cmds(void)
2814{
2815 int ret;
2816
2817 ret = register_ftrace_command(&event_enable_cmd);
2818 if (WARN_ON(ret < 0))
2819 return ret;
2820 ret = register_ftrace_command(&event_disable_cmd);
2821 if (WARN_ON(ret < 0))
2822 unregister_ftrace_command(&event_enable_cmd);
2823 return ret;
2824}
2825#else
2826static inline int register_event_cmds(void) { return 0; }
2827#endif
2828
2829
2830
2831
2832
2833
2834
2835
2836static __init void
2837__trace_early_add_event_dirs(struct trace_array *tr)
2838{
2839 struct trace_event_file *file;
2840 int ret;
2841
2842
2843 list_for_each_entry(file, &tr->events, list) {
2844 ret = event_create_dir(tr->event_dir, file);
2845 if (ret < 0)
2846 pr_warn("Could not create directory for event %s\n",
2847 trace_event_name(file->event_call));
2848 }
2849}
2850
2851
2852
2853
2854
2855
2856
2857static __init void
2858__trace_early_add_events(struct trace_array *tr)
2859{
2860 struct trace_event_call *call;
2861 int ret;
2862
2863 list_for_each_entry(call, &ftrace_events, list) {
2864
2865 if (WARN_ON_ONCE(call->mod))
2866 continue;
2867
2868 ret = __trace_early_add_new_event(call, tr);
2869 if (ret < 0)
2870 pr_warn("Could not create early event %s\n",
2871 trace_event_name(call));
2872 }
2873}
2874
2875
2876static void
2877__trace_remove_event_dirs(struct trace_array *tr)
2878{
2879 struct trace_event_file *file, *next;
2880
2881 list_for_each_entry_safe(file, next, &tr->events, list)
2882 remove_event_file_dir(file);
2883}
2884
2885static void __add_event_to_tracers(struct trace_event_call *call)
2886{
2887 struct trace_array *tr;
2888
2889 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2890 __trace_add_new_event(call, tr);
2891}
2892
2893extern struct trace_event_call *__start_ftrace_events[];
2894extern struct trace_event_call *__stop_ftrace_events[];
2895
2896static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2897
2898static __init int setup_trace_event(char *str)
2899{
2900 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2901 ring_buffer_expanded = true;
2902 tracing_selftest_disabled = true;
2903
2904 return 1;
2905}
2906__setup("trace_event=", setup_trace_event);
2907
2908
2909static int
2910create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2911{
2912 struct dentry *d_events;
2913 struct dentry *entry;
2914
2915 entry = tracefs_create_file("set_event", 0644, parent,
2916 tr, &ftrace_set_event_fops);
2917 if (!entry) {
2918 pr_warn("Could not create tracefs 'set_event' entry\n");
2919 return -ENOMEM;
2920 }
2921
2922 d_events = tracefs_create_dir("events", parent);
2923 if (!d_events) {
2924 pr_warn("Could not create tracefs 'events' directory\n");
2925 return -ENOMEM;
2926 }
2927
2928 entry = trace_create_file("enable", 0644, d_events,
2929 tr, &ftrace_tr_enable_fops);
2930 if (!entry) {
2931 pr_warn("Could not create tracefs 'enable' entry\n");
2932 return -ENOMEM;
2933 }
2934
2935
2936
2937 entry = tracefs_create_file("set_event_pid", 0644, parent,
2938 tr, &ftrace_set_event_pid_fops);
2939 if (!entry)
2940 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
2941
2942
2943 entry = trace_create_file("header_page", 0444, d_events,
2944 ring_buffer_print_page_header,
2945 &ftrace_show_header_fops);
2946 if (!entry)
2947 pr_warn("Could not create tracefs 'header_page' entry\n");
2948
2949 entry = trace_create_file("header_event", 0444, d_events,
2950 ring_buffer_print_entry_header,
2951 &ftrace_show_header_fops);
2952 if (!entry)
2953 pr_warn("Could not create tracefs 'header_event' entry\n");
2954
2955 tr->event_dir = d_events;
2956
2957 return 0;
2958}
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2974{
2975 int ret;
2976
2977 lockdep_assert_held(&event_mutex);
2978
2979 ret = create_event_toplevel_files(parent, tr);
2980 if (ret)
2981 goto out;
2982
2983 down_write(&trace_event_sem);
2984 __trace_add_event_dirs(tr);
2985 up_write(&trace_event_sem);
2986
2987 out:
2988 return ret;
2989}
2990
2991
2992
2993
2994
2995static __init int
2996early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2997{
2998 int ret;
2999
3000 mutex_lock(&event_mutex);
3001
3002 ret = create_event_toplevel_files(parent, tr);
3003 if (ret)
3004 goto out_unlock;
3005
3006 down_write(&trace_event_sem);
3007 __trace_early_add_event_dirs(tr);
3008 up_write(&trace_event_sem);
3009
3010 out_unlock:
3011 mutex_unlock(&event_mutex);
3012
3013 return ret;
3014}
3015
3016
3017int event_trace_del_tracer(struct trace_array *tr)
3018{
3019 lockdep_assert_held(&event_mutex);
3020
3021
3022 clear_event_triggers(tr);
3023
3024
3025 __ftrace_clear_event_pids(tr);
3026
3027
3028 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3029
3030
3031 tracepoint_synchronize_unregister();
3032
3033 down_write(&trace_event_sem);
3034 __trace_remove_event_dirs(tr);
3035 tracefs_remove_recursive(tr->event_dir);
3036 up_write(&trace_event_sem);
3037
3038 tr->event_dir = NULL;
3039
3040 return 0;
3041}
3042
3043static __init int event_trace_memsetup(void)
3044{
3045 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3046 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3047 return 0;
3048}
3049
3050static __init void
3051early_enable_events(struct trace_array *tr, bool disable_first)
3052{
3053 char *buf = bootup_event_buf;
3054 char *token;
3055 int ret;
3056
3057 while (true) {
3058 token = strsep(&buf, ",");
3059
3060 if (!token)
3061 break;
3062
3063 if (*token) {
3064
3065 if (disable_first)
3066 ftrace_set_clr_event(tr, token, 0);
3067
3068 ret = ftrace_set_clr_event(tr, token, 1);
3069 if (ret)
3070 pr_warn("Failed to enable trace event: %s\n", token);
3071 }
3072
3073
3074 if (buf)
3075 *(buf - 1) = ',';
3076 }
3077}
3078
3079static __init int event_trace_enable(void)
3080{
3081 struct trace_array *tr = top_trace_array();
3082 struct trace_event_call **iter, *call;
3083 int ret;
3084
3085 if (!tr)
3086 return -ENODEV;
3087
3088 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3089
3090 call = *iter;
3091 ret = event_init(call);
3092 if (!ret)
3093 list_add(&call->list, &ftrace_events);
3094 }
3095
3096
3097
3098
3099
3100
3101
3102 __trace_early_add_events(tr);
3103
3104 early_enable_events(tr, false);
3105
3106 trace_printk_start_comm();
3107
3108 register_event_cmds();
3109
3110 register_trigger_cmds();
3111
3112 return 0;
3113}
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125static __init int event_trace_enable_again(void)
3126{
3127 struct trace_array *tr;
3128
3129 tr = top_trace_array();
3130 if (!tr)
3131 return -ENODEV;
3132
3133 early_enable_events(tr, true);
3134
3135 return 0;
3136}
3137
3138early_initcall(event_trace_enable_again);
3139
3140__init int event_trace_init(void)
3141{
3142 struct trace_array *tr;
3143 struct dentry *d_tracer;
3144 struct dentry *entry;
3145 int ret;
3146
3147 tr = top_trace_array();
3148 if (!tr)
3149 return -ENODEV;
3150
3151 d_tracer = tracing_init_dentry();
3152 if (IS_ERR(d_tracer))
3153 return 0;
3154
3155 entry = tracefs_create_file("available_events", 0444, d_tracer,
3156 tr, &ftrace_avail_fops);
3157 if (!entry)
3158 pr_warn("Could not create tracefs 'available_events' entry\n");
3159
3160 if (trace_define_generic_fields())
3161 pr_warn("tracing: Failed to allocated generic fields");
3162
3163 if (trace_define_common_fields())
3164 pr_warn("tracing: Failed to allocate common fields");
3165
3166 ret = early_event_add_tracer(d_tracer, tr);
3167 if (ret)
3168 return ret;
3169
3170#ifdef CONFIG_MODULES
3171 ret = register_module_notifier(&trace_module_nb);
3172 if (ret)
3173 pr_warn("Failed to register trace events module notifier\n");
3174#endif
3175 return 0;
3176}
3177
3178void __init trace_event_init(void)
3179{
3180 event_trace_memsetup();
3181 init_ftrace_syscalls();
3182 event_trace_enable();
3183}
3184
3185#ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3186
3187static DEFINE_SPINLOCK(test_spinlock);
3188static DEFINE_SPINLOCK(test_spinlock_irq);
3189static DEFINE_MUTEX(test_mutex);
3190
3191static __init void test_work(struct work_struct *dummy)
3192{
3193 spin_lock(&test_spinlock);
3194 spin_lock_irq(&test_spinlock_irq);
3195 udelay(1);
3196 spin_unlock_irq(&test_spinlock_irq);
3197 spin_unlock(&test_spinlock);
3198
3199 mutex_lock(&test_mutex);
3200 msleep(1);
3201 mutex_unlock(&test_mutex);
3202}
3203
3204static __init int event_test_thread(void *unused)
3205{
3206 void *test_malloc;
3207
3208 test_malloc = kmalloc(1234, GFP_KERNEL);
3209 if (!test_malloc)
3210 pr_info("failed to kmalloc\n");
3211
3212 schedule_on_each_cpu(test_work);
3213
3214 kfree(test_malloc);
3215
3216 set_current_state(TASK_INTERRUPTIBLE);
3217 while (!kthread_should_stop()) {
3218 schedule();
3219 set_current_state(TASK_INTERRUPTIBLE);
3220 }
3221 __set_current_state(TASK_RUNNING);
3222
3223 return 0;
3224}
3225
3226
3227
3228
3229static __init void event_test_stuff(void)
3230{
3231 struct task_struct *test_thread;
3232
3233 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3234 msleep(1);
3235 kthread_stop(test_thread);
3236}
3237
3238
3239
3240
3241
3242static __init void event_trace_self_tests(void)
3243{
3244 struct trace_subsystem_dir *dir;
3245 struct trace_event_file *file;
3246 struct trace_event_call *call;
3247 struct event_subsystem *system;
3248 struct trace_array *tr;
3249 int ret;
3250
3251 tr = top_trace_array();
3252 if (!tr)
3253 return;
3254
3255 pr_info("Running tests on trace events:\n");
3256
3257 list_for_each_entry(file, &tr->events, list) {
3258
3259 call = file->event_call;
3260
3261
3262 if (!call->class || !call->class->probe)
3263 continue;
3264
3265
3266
3267
3268
3269
3270
3271#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3272 if (call->class->system &&
3273 strcmp(call->class->system, "syscalls") == 0)
3274 continue;
3275#endif
3276
3277 pr_info("Testing event %s: ", trace_event_name(call));
3278
3279
3280
3281
3282
3283 if (file->flags & EVENT_FILE_FL_ENABLED) {
3284 pr_warn("Enabled event during self test!\n");
3285 WARN_ON_ONCE(1);
3286 continue;
3287 }
3288
3289 ftrace_event_enable_disable(file, 1);
3290 event_test_stuff();
3291 ftrace_event_enable_disable(file, 0);
3292
3293 pr_cont("OK\n");
3294 }
3295
3296
3297
3298 pr_info("Running tests on trace event systems:\n");
3299
3300 list_for_each_entry(dir, &tr->systems, list) {
3301
3302 system = dir->subsystem;
3303
3304
3305 if (strcmp(system->name, "ftrace") == 0)
3306 continue;
3307
3308 pr_info("Testing event system %s: ", system->name);
3309
3310 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3311 if (WARN_ON_ONCE(ret)) {
3312 pr_warn("error enabling system %s\n",
3313 system->name);
3314 continue;
3315 }
3316
3317 event_test_stuff();
3318
3319 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3320 if (WARN_ON_ONCE(ret)) {
3321 pr_warn("error disabling system %s\n",
3322 system->name);
3323 continue;
3324 }
3325
3326 pr_cont("OK\n");
3327 }
3328
3329
3330
3331 pr_info("Running tests on all trace events:\n");
3332 pr_info("Testing all events: ");
3333
3334 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3335 if (WARN_ON_ONCE(ret)) {
3336 pr_warn("error enabling all events\n");
3337 return;
3338 }
3339
3340 event_test_stuff();
3341
3342
3343 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3344 if (WARN_ON_ONCE(ret)) {
3345 pr_warn("error disabling all events\n");
3346 return;
3347 }
3348
3349 pr_cont("OK\n");
3350}
3351
3352#ifdef CONFIG_FUNCTION_TRACER
3353
3354static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3355
3356static struct trace_event_file event_trace_file __initdata;
3357
3358static void __init
3359function_test_events_call(unsigned long ip, unsigned long parent_ip,
3360 struct ftrace_ops *op, struct pt_regs *pt_regs)
3361{
3362 struct ring_buffer_event *event;
3363 struct ring_buffer *buffer;
3364 struct ftrace_entry *entry;
3365 unsigned long flags;
3366 long disabled;
3367 int cpu;
3368 int pc;
3369
3370 pc = preempt_count();
3371 preempt_disable_notrace();
3372 cpu = raw_smp_processor_id();
3373 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3374
3375 if (disabled != 1)
3376 goto out;
3377
3378 local_save_flags(flags);
3379
3380 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3381 TRACE_FN, sizeof(*entry),
3382 flags, pc);
3383 if (!event)
3384 goto out;
3385 entry = ring_buffer_event_data(event);
3386 entry->ip = ip;
3387 entry->parent_ip = parent_ip;
3388
3389 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3390 entry, flags, pc);
3391 out:
3392 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3393 preempt_enable_notrace();
3394}
3395
3396static struct ftrace_ops trace_ops __initdata =
3397{
3398 .func = function_test_events_call,
3399 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3400};
3401
3402static __init void event_trace_self_test_with_function(void)
3403{
3404 int ret;
3405
3406 event_trace_file.tr = top_trace_array();
3407 if (WARN_ON(!event_trace_file.tr))
3408 return;
3409
3410 ret = register_ftrace_function(&trace_ops);
3411 if (WARN_ON(ret < 0)) {
3412 pr_info("Failed to enable function tracer for event tests\n");
3413 return;
3414 }
3415 pr_info("Running tests again, along with the function tracer\n");
3416 event_trace_self_tests();
3417 unregister_ftrace_function(&trace_ops);
3418}
3419#else
3420static __init void event_trace_self_test_with_function(void)
3421{
3422}
3423#endif
3424
3425static __init int event_trace_self_tests_init(void)
3426{
3427 if (!tracing_selftest_disabled) {
3428 event_trace_self_tests();
3429 event_trace_self_test_with_function();
3430 }
3431
3432 return 0;
3433}
3434
3435late_initcall(event_trace_self_tests_init);
3436
3437#endif
3438