blob: d120cfe3cca781a7214e79eaf4f54170e685db3f [file] [log] [blame]
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001/*
2 * event tracer
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
Steven Rostedt981d0812009-03-02 13:53:59 -05006 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 *
Steven Rostedtb77e38a2009-02-24 10:21:36 -05009 */
10
Fabian Frederick3448bac2014-06-07 13:43:08 +020011#define pr_fmt(fmt) fmt
12
Steven Rostedte6187002009-04-15 13:36:40 -040013#include <linux/workqueue.h>
14#include <linux/spinlock.h>
15#include <linux/kthread.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050016#include <linux/tracefs.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050017#include <linux/uaccess.h>
18#include <linux/module.h>
19#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Steven Rostedte6187002009-04-15 13:36:40 -040021#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050022
Li Zefan020e5f82009-07-01 10:47:05 +080023#include <asm/setup.h>
24
Steven Rostedt91729ef92009-03-02 15:03:01 -050025#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050026
Steven Rostedt4e5292e2009-09-12 19:26:21 -040027#undef TRACE_SYSTEM
Steven Rostedtb628b3e2009-02-27 23:32:58 -050028#define TRACE_SYSTEM "TRACE_SYSTEM"
29
Li Zefan20c89282009-05-06 10:33:45 +080030DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050031
Steven Rostedta59fd602009-04-10 13:52:20 -040032LIST_HEAD(ftrace_events);
Daniel Wagner9f616682015-08-10 14:35:46 +020033static LIST_HEAD(ftrace_generic_fields);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080034static LIST_HEAD(ftrace_common_fields);
Steven Rostedta59fd602009-04-10 13:52:20 -040035
Steven Rostedtd1a29142013-02-27 20:23:57 -050036#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
37
38static struct kmem_cache *field_cachep;
39static struct kmem_cache *file_cachep;
40
Steven Rostedt6e94a782013-06-27 10:58:31 -040041static inline int system_refcount(struct event_subsystem *system)
42{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020043 return system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040044}
45
46static int system_refcount_inc(struct event_subsystem *system)
47{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020048 return system->ref_count++;
Steven Rostedt6e94a782013-06-27 10:58:31 -040049}
50
51static int system_refcount_dec(struct event_subsystem *system)
52{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020053 return --system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040054}
55
Steven Rostedtae63b31e2012-05-03 23:09:03 -040056/* Double loops, do not use break, only goto's work */
57#define do_for_each_event_file(tr, file) \
58 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
59 list_for_each_entry(file, &tr->events, list)
60
61#define do_for_each_event_file_safe(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040063 struct trace_event_file *___n; \
Steven Rostedtae63b31e2012-05-03 23:09:03 -040064 list_for_each_entry_safe(file, ___n, &tr->events, list)
65
66#define while_for_each_event_file() \
67 }
68
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080069static struct list_head *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040070trace_get_fields(struct trace_event_call *event_call)
Steven Rostedt2e33af02010-04-22 10:35:55 -040071{
72 if (!event_call->class->get_fields)
73 return &event_call->class->fields;
74 return event_call->class->get_fields(event_call);
75}
76
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080077static struct ftrace_event_field *
78__find_event_field(struct list_head *head, char *name)
79{
80 struct ftrace_event_field *field;
81
82 list_for_each_entry(field, head, link) {
83 if (!strcmp(field->name, name))
84 return field;
85 }
86
87 return NULL;
88}
89
90struct ftrace_event_field *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040091trace_find_event_field(struct trace_event_call *call, char *name)
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080092{
93 struct ftrace_event_field *field;
94 struct list_head *head;
95
Daniel Wagner9f616682015-08-10 14:35:46 +020096 field = __find_event_field(&ftrace_generic_fields, name);
97 if (field)
98 return field;
99
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +0800100 field = __find_event_field(&ftrace_common_fields, name);
101 if (field)
102 return field;
103
104 head = trace_get_fields(call);
105 return __find_event_field(head, name);
106}
107
Li Zefan8728fe52010-05-24 16:22:49 +0800108static int __trace_define_field(struct list_head *head, const char *type,
109 const char *name, int offset, int size,
110 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -0500111{
112 struct ftrace_event_field *field;
113
Steven Rostedtd1a29142013-02-27 20:23:57 -0500114 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -0500115 if (!field)
Namhyung Kimaaf6ac02013-06-07 15:07:48 +0900116 return -ENOMEM;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100117
Steven Rostedt92edca02013-02-27 20:41:37 -0500118 field->name = name;
119 field->type = type;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100120
Li Zefan43b51ea2009-08-07 10:33:22 +0800121 if (filter_type == FILTER_OTHER)
122 field->filter_type = filter_assign_type(type);
123 else
124 field->filter_type = filter_type;
125
Tom Zanussicf027f62009-03-22 03:30:39 -0500126 field->offset = offset;
127 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -0500128 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +0800129
Steven Rostedt2e33af02010-04-22 10:35:55 -0400130 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -0500131
132 return 0;
Tom Zanussicf027f62009-03-22 03:30:39 -0500133}
Li Zefan8728fe52010-05-24 16:22:49 +0800134
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400135int trace_define_field(struct trace_event_call *call, const char *type,
Li Zefan8728fe52010-05-24 16:22:49 +0800136 const char *name, int offset, int size, int is_signed,
137 int filter_type)
138{
139 struct list_head *head;
140
141 if (WARN_ON(!call->class))
142 return 0;
143
144 head = trace_get_fields(call);
145 return __trace_define_field(head, type, name, offset, size,
146 is_signed, filter_type);
147}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400148EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500149
Daniel Wagner9f616682015-08-10 14:35:46 +0200150#define __generic_field(type, item, filter_type) \
151 ret = __trace_define_field(&ftrace_generic_fields, #type, \
152 #item, 0, 0, is_signed_type(type), \
153 filter_type); \
154 if (ret) \
155 return ret;
156
Li Zefane647d6b2009-08-19 15:54:32 +0800157#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800158 ret = __trace_define_field(&ftrace_common_fields, #type, \
159 "common_" #item, \
160 offsetof(typeof(ent), item), \
161 sizeof(ent.item), \
162 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800163 if (ret) \
164 return ret;
165
Daniel Wagner9f616682015-08-10 14:35:46 +0200166static int trace_define_generic_fields(void)
167{
168 int ret;
169
170 __generic_field(int, cpu, FILTER_OTHER);
171 __generic_field(char *, comm, FILTER_PTR_STRING);
172
173 return ret;
174}
175
Li Zefan8728fe52010-05-24 16:22:49 +0800176static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800177{
178 int ret;
179 struct trace_entry ent;
180
181 __common_field(unsigned short, type);
182 __common_field(unsigned char, flags);
183 __common_field(unsigned char, preempt_count);
184 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800185
186 return ret;
187}
188
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400189static void trace_destroy_fields(struct trace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800190{
191 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400192 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800193
Steven Rostedt2e33af02010-04-22 10:35:55 -0400194 head = trace_get_fields(call);
195 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800196 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500197 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800198 }
199}
200
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400201int trace_event_raw_init(struct trace_event_call *call)
Li Zefan87d9b4e2009-12-08 11:14:20 +0800202{
203 int id;
204
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400205 id = register_trace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800206 if (!id)
207 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800208
209 return 0;
210}
211EXPORT_SYMBOL_GPL(trace_event_raw_init);
212
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400213void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
214 struct trace_event_file *trace_file,
215 unsigned long len)
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400216{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400217 struct trace_event_call *event_call = trace_file->event_call;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400218
219 local_save_flags(fbuffer->flags);
220 fbuffer->pc = preempt_count();
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400221 fbuffer->trace_file = trace_file;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400222
223 fbuffer->event =
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400224 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400225 event_call->event.type, len,
226 fbuffer->flags, fbuffer->pc);
227 if (!fbuffer->event)
228 return NULL;
229
230 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
231 return fbuffer->entry;
232}
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400233EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400234
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500235static DEFINE_SPINLOCK(tracepoint_iter_lock);
236
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400237static void output_printk(struct trace_event_buffer *fbuffer)
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500238{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400239 struct trace_event_call *event_call;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500240 struct trace_event *event;
241 unsigned long flags;
242 struct trace_iterator *iter = tracepoint_print_iter;
243
244 if (!iter)
245 return;
246
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400247 event_call = fbuffer->trace_file->event_call;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500248 if (!event_call || !event_call->event.funcs ||
249 !event_call->event.funcs->trace)
250 return;
251
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400252 event = &fbuffer->trace_file->event_call->event;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500253
254 spin_lock_irqsave(&tracepoint_iter_lock, flags);
255 trace_seq_init(&iter->seq);
256 iter->ent = fbuffer->entry;
257 event_call->event.funcs->trace(iter, 0, event);
258 trace_seq_putc(&iter->seq, 0);
259 printk("%s", iter->seq.buffer);
260
261 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
262}
263
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400264void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400265{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500266 if (tracepoint_printk)
267 output_printk(fbuffer);
268
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400269 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400270 fbuffer->event, fbuffer->entry,
271 fbuffer->flags, fbuffer->pc);
272}
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400273EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400274
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400275int trace_event_reg(struct trace_event_call *call,
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400276 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400277{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400278 struct trace_event_file *file = data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400279
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400280 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400281 switch (type) {
282 case TRACE_REG_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400283 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400284 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400285 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400286 case TRACE_REG_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400287 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400288 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400289 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400290 return 0;
291
292#ifdef CONFIG_PERF_EVENTS
293 case TRACE_REG_PERF_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400294 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400295 call->class->perf_probe,
296 call);
297 case TRACE_REG_PERF_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400298 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400299 call->class->perf_probe,
300 call);
301 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100302 case TRACE_REG_PERF_OPEN:
303 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100304 case TRACE_REG_PERF_ADD:
305 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100306 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400307#endif
308 }
309 return 0;
310}
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400311EXPORT_SYMBOL_GPL(trace_event_reg);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400312
Li Zefane870e9a2010-07-02 11:07:32 +0800313void trace_event_enable_cmd_record(bool enable)
314{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400315 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400316 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800317
318 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400319 do_for_each_event_file(tr, file) {
320
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400321 if (!(file->flags & EVENT_FILE_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800322 continue;
323
324 if (enable) {
325 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400326 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800327 } else {
328 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400329 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800330 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400331 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800332 mutex_unlock(&event_mutex);
333}
334
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400335static int __ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400336 int enable, int soft_disable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500337{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400338 struct trace_event_call *call = file->event_call;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400339 struct trace_array *tr = file->tr;
Li Zefan3b8e4272009-12-08 11:14:52 +0800340 int ret = 0;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400341 int disable;
Li Zefan3b8e4272009-12-08 11:14:52 +0800342
Steven Rostedtfd994982009-02-28 02:41:25 -0500343 switch (enable) {
344 case 0:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400345 /*
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900346 * When soft_disable is set and enable is cleared, the sm_ref
347 * reference counter is decremented. If it reaches 0, we want
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400348 * to clear the SOFT_DISABLED flag but leave the event in the
349 * state that it was. That is, if the event was enabled and
350 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
351 * is set we do not want the event to be enabled before we
352 * clear the bit.
353 *
354 * When soft_disable is not set but the SOFT_MODE flag is,
355 * we do nothing. Do not disable the tracepoint, otherwise
356 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
357 */
358 if (soft_disable) {
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900359 if (atomic_dec_return(&file->sm_ref) > 0)
360 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400361 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
362 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400363 } else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400364 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400365
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400366 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
367 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
368 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800369 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400370 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800371 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400372 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500373 }
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500374 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400375 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
376 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500377 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400378 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500379 break;
380 case 1:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400381 /*
382 * When soft_disable is set and enable is set, we want to
383 * register the tracepoint for the event, but leave the event
384 * as is. That means, if the event was already enabled, we do
385 * nothing (but set SOFT_MODE). If the event is disabled, we
386 * set SOFT_DISABLED before enabling the event tracepoint, so
387 * it still seems to be disabled.
388 */
389 if (!soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400390 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900391 else {
392 if (atomic_inc_return(&file->sm_ref) > 1)
393 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400394 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900395 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400396
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400397 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400398
399 /* Keep the event disabled, when going to SOFT_MODE. */
400 if (soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400401 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400402
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400403 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800404 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400405 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800406 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400407 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800408 if (ret) {
409 tracing_stop_cmdline_record();
410 pr_info("event trace: Could not enable event "
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400411 "%s\n", trace_event_name(call));
Li Zefan3b8e4272009-12-08 11:14:52 +0800412 break;
413 }
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400414 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -0500415
416 /* WAS_ENABLED gets set but never cleared. */
417 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
Steven Rostedtfd994982009-02-28 02:41:25 -0500418 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500419 break;
420 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800421
422 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500423}
424
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400425int trace_event_enable_disable(struct trace_event_file *file,
Tom Zanussi85f2b082013-10-24 08:59:24 -0500426 int enable, int soft_disable)
427{
428 return __ftrace_event_enable_disable(file, enable, soft_disable);
429}
430
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400431static int ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400432 int enable)
433{
434 return __ftrace_event_enable_disable(file, enable, 0);
435}
436
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400437static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800438{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400439 struct trace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800440
441 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400442 list_for_each_entry(file, &tr->events, list) {
443 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800444 }
445 mutex_unlock(&event_mutex);
446}
447
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400448static void __put_system(struct event_subsystem *system)
449{
450 struct event_filter *filter = system->filter;
451
Steven Rostedt6e94a782013-06-27 10:58:31 -0400452 WARN_ON_ONCE(system_refcount(system) == 0);
453 if (system_refcount_dec(system))
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400454 return;
455
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400456 list_del(&system->list);
457
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400458 if (filter) {
459 kfree(filter->filter_string);
460 kfree(filter);
461 }
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +0200462 kfree_const(system->name);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400463 kfree(system);
464}
465
466static void __get_system(struct event_subsystem *system)
467{
Steven Rostedt6e94a782013-06-27 10:58:31 -0400468 WARN_ON_ONCE(system_refcount(system) == 0);
469 system_refcount_inc(system);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400470}
471
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400472static void __get_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400473{
474 WARN_ON_ONCE(dir->ref_count == 0);
475 dir->ref_count++;
476 __get_system(dir->subsystem);
477}
478
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400479static void __put_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400480{
481 WARN_ON_ONCE(dir->ref_count == 0);
482 /* If the subsystem is about to be freed, the dir must be too */
Steven Rostedt6e94a782013-06-27 10:58:31 -0400483 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400484
485 __put_system(dir->subsystem);
486 if (!--dir->ref_count)
487 kfree(dir);
488}
489
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400490static void put_system(struct trace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400491{
492 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400493 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400494 mutex_unlock(&event_mutex);
495}
496
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400497static void remove_subsystem(struct trace_subsystem_dir *dir)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200498{
499 if (!dir)
500 return;
501
502 if (!--dir->nr_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500503 tracefs_remove_recursive(dir->entry);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200504 list_del(&dir->list);
505 __put_system_dir(dir);
506 }
507}
508
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400509static void remove_event_file_dir(struct trace_event_file *file)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200510{
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200511 struct dentry *dir = file->dir;
512 struct dentry *child;
513
514 if (dir) {
515 spin_lock(&dir->d_lock); /* probably unneeded */
Al Viro946e51f2014-10-26 19:19:16 -0400516 list_for_each_entry(child, &dir->d_subdirs, d_child) {
David Howells7682c912015-03-17 22:26:16 +0000517 if (d_really_is_positive(child)) /* probably unneeded */
518 d_inode(child)->i_private = NULL;
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200519 }
520 spin_unlock(&dir->d_lock);
521
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500522 tracefs_remove_recursive(dir);
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200523 }
524
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200525 list_del(&file->list);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200526 remove_subsystem(file->system);
Oleg Nesterov2448e342014-07-11 21:06:38 +0200527 free_event_filter(file->filter);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200528 kmem_cache_free(file_cachep, file);
529}
530
Li Zefan8f31bfe2009-05-08 10:31:42 +0800531/*
532 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
533 */
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400534static int
535__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
536 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500537{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400538 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400539 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400540 const char *name;
Steven Rostedt29f93942009-05-08 16:06:47 -0400541 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500542
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400543 list_for_each_entry(file, &tr->events, list) {
544
545 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400546 name = trace_event_name(call);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500547
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400548 if (!name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500549 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500550
Steven Rostedt9b637762012-05-10 15:55:43 -0400551 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
552 continue;
553
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500554 if (match &&
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400555 strcmp(match, name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400556 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500557 continue;
558
Steven Rostedt8f082012010-04-20 10:47:33 -0400559 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500560 continue;
561
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400562 if (event && strcmp(event, name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500563 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500564
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400565 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500566
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500567 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500568 }
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400569
570 return ret;
571}
572
573static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
574 const char *sub, const char *event, int set)
575{
576 int ret;
577
578 mutex_lock(&event_mutex);
579 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500580 mutex_unlock(&event_mutex);
581
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500582 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500583}
584
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400585static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800586{
587 char *event = NULL, *sub = NULL, *match;
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900588 int ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800589
590 /*
591 * The buf format can be <subsystem>:<event-name>
592 * *:<event-name> means any event by that name.
593 * :<event-name> is the same.
594 *
595 * <subsystem>:* means all events in that subsystem
596 * <subsystem>: means the same.
597 *
598 * <name> (no ':') means all events in a subsystem with
599 * the name <name> or any event that matches <name>
600 */
601
602 match = strsep(&buf, ":");
603 if (buf) {
604 sub = match;
605 event = buf;
606 match = NULL;
607
608 if (!strlen(sub) || strcmp(sub, "*") == 0)
609 sub = NULL;
610 if (!strlen(event) || strcmp(event, "*") == 0)
611 event = NULL;
612 }
613
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900614 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
615
616 /* Put back the colon to allow this to be called again */
617 if (buf)
618 *(buf - 1) = ':';
619
620 return ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800621}
622
Steven Rostedt4671c792009-05-08 16:27:41 -0400623/**
624 * trace_set_clr_event - enable or disable an event
625 * @system: system name to match (NULL for any system)
626 * @event: event name to match (NULL for all events, within system)
627 * @set: 1 to enable, 0 to disable
628 *
629 * This is a way for other parts of the kernel to enable or disable
630 * event recording.
631 *
632 * Returns 0 on success, -EINVAL if the parameters do not match any
633 * registered events.
634 */
635int trace_set_clr_event(const char *system, const char *event, int set)
636{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400637 struct trace_array *tr = top_trace_array();
638
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +0900639 if (!tr)
640 return -ENODEV;
641
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400642 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400643}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800644EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400645
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500646/* 128 should be much more than enough */
647#define EVENT_BUF_SIZE 127
648
649static ssize_t
650ftrace_event_write(struct file *file, const char __user *ubuf,
651 size_t cnt, loff_t *ppos)
652{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200653 struct trace_parser parser;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400654 struct seq_file *m = file->private_data;
655 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800656 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500657
Li Zefan4ba79782009-09-22 13:52:20 +0800658 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500659 return 0;
660
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400661 ret = tracing_update_buffers();
662 if (ret < 0)
663 return ret;
664
jolsa@redhat.com48966362009-09-11 17:29:28 +0200665 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500666 return -ENOMEM;
667
jolsa@redhat.com48966362009-09-11 17:29:28 +0200668 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500669
Li Zefan4ba79782009-09-22 13:52:20 +0800670 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200671 int set = 1;
672
673 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500674 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500675
jolsa@redhat.com48966362009-09-11 17:29:28 +0200676 parser.buffer[parser.idx] = 0;
677
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400678 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500679 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200680 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500681 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500682
683 ret = read;
684
jolsa@redhat.com48966362009-09-11 17:29:28 +0200685 out_put:
686 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500687
688 return ret;
689}
690
691static void *
692t_next(struct seq_file *m, void *v, loff_t *pos)
693{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400694 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400695 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400696 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500697
698 (*pos)++;
699
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400700 list_for_each_entry_continue(file, &tr->events, list) {
701 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400702 /*
703 * The ftrace subsystem is for showing formats only.
704 * They can not be enabled or disabled via the event files.
705 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400706 if (call->class && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400707 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400708 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500709
Li Zefan30bd39c2009-09-18 14:07:05 +0800710 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500711}
712
713static void *t_start(struct seq_file *m, loff_t *pos)
714{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400715 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400716 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800717 loff_t l;
718
Li Zefan20c89282009-05-06 10:33:45 +0800719 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800720
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400721 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800722 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400723 file = t_next(m, file, &l);
724 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800725 break;
726 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400727 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500728}
729
730static void *
731s_next(struct seq_file *m, void *v, loff_t *pos)
732{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400733 struct trace_event_file *file = v;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400734 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500735
736 (*pos)++;
737
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400738 list_for_each_entry_continue(file, &tr->events, list) {
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400739 if (file->flags & EVENT_FILE_FL_ENABLED)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400740 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500741 }
742
Li Zefan30bd39c2009-09-18 14:07:05 +0800743 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500744}
745
746static void *s_start(struct seq_file *m, loff_t *pos)
747{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400748 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400749 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800750 loff_t l;
751
Li Zefan20c89282009-05-06 10:33:45 +0800752 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800753
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400754 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800755 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400756 file = s_next(m, file, &l);
757 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800758 break;
759 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400760 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500761}
762
763static int t_show(struct seq_file *m, void *v)
764{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400765 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400766 struct trace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500767
Steven Rostedt8f082012010-04-20 10:47:33 -0400768 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
769 seq_printf(m, "%s:", call->class->system);
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400770 seq_printf(m, "%s\n", trace_event_name(call));
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500771
772 return 0;
773}
774
775static void t_stop(struct seq_file *m, void *p)
776{
Li Zefan20c89282009-05-06 10:33:45 +0800777 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500778}
779
Steven Rostedt1473e442009-02-24 14:15:08 -0500780static ssize_t
781event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
782 loff_t *ppos)
783{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400784 struct trace_event_file *file;
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200785 unsigned long flags;
Tom Zanussia4390592013-06-29 00:08:04 -0500786 char buf[4] = "0";
Steven Rostedt1473e442009-02-24 14:15:08 -0500787
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200788 mutex_lock(&event_mutex);
789 file = event_file_data(filp);
790 if (likely(file))
791 flags = file->flags;
792 mutex_unlock(&event_mutex);
793
794 if (!file)
795 return -ENODEV;
796
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400797 if (flags & EVENT_FILE_FL_ENABLED &&
798 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussia4390592013-06-29 00:08:04 -0500799 strcpy(buf, "1");
800
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400801 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
802 flags & EVENT_FILE_FL_SOFT_MODE)
Tom Zanussia4390592013-06-29 00:08:04 -0500803 strcat(buf, "*");
804
805 strcat(buf, "\n");
Steven Rostedt1473e442009-02-24 14:15:08 -0500806
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400807 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -0500808}
809
810static ssize_t
811event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
812 loff_t *ppos)
813{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400814 struct trace_event_file *file;
Steven Rostedt1473e442009-02-24 14:15:08 -0500815 unsigned long val;
816 int ret;
817
Peter Huewe22fe9b52011-06-07 21:58:27 +0200818 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
819 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -0500820 return ret;
821
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400822 ret = tracing_update_buffers();
823 if (ret < 0)
824 return ret;
825
Steven Rostedt1473e442009-02-24 14:15:08 -0500826 switch (val) {
827 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500828 case 1:
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200829 ret = -ENODEV;
Steven Rostedt11a241a2009-03-02 11:49:04 -0500830 mutex_lock(&event_mutex);
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200831 file = event_file_data(filp);
832 if (likely(file))
833 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500834 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500835 break;
836
837 default:
838 return -EINVAL;
839 }
840
841 *ppos += cnt;
842
Li Zefan3b8e4272009-12-08 11:14:52 +0800843 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500844}
845
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400846static ssize_t
847system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
848 loff_t *ppos)
849{
Li Zefanc142b152009-05-08 10:32:05 +0800850 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400851 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400852 struct event_subsystem *system = dir->subsystem;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400853 struct trace_event_call *call;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400854 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400855 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400856 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800857 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400858 int ret;
859
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400860 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400861 list_for_each_entry(file, &tr->events, list) {
862 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400863 if (!trace_event_name(call) || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400864 continue;
865
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400866 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400867 continue;
868
869 /*
870 * We need to find out if all the events are set
871 * or if all events or cleared, or if we have
872 * a mixture.
873 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400874 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +0800875
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400876 /*
877 * If we have a mixture, no need to look further.
878 */
Li Zefanc142b152009-05-08 10:32:05 +0800879 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400880 break;
881 }
882 mutex_unlock(&event_mutex);
883
Li Zefanc142b152009-05-08 10:32:05 +0800884 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400885 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400886
887 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
888
889 return ret;
890}
891
892static ssize_t
893system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
894 loff_t *ppos)
895{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400896 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400897 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400898 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400899 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400900 ssize_t ret;
901
Peter Huewe22fe9b52011-06-07 21:58:27 +0200902 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
903 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400904 return ret;
905
906 ret = tracing_update_buffers();
907 if (ret < 0)
908 return ret;
909
Li Zefan8f31bfe2009-05-08 10:31:42 +0800910 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400911 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400912
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400913 /*
914 * Opening of "enable" adds a ref count to system,
915 * so the name is safe to use.
916 */
917 if (system)
918 name = system->name;
919
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400920 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400921 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800922 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400923
924 ret = cnt;
925
Li Zefan8f31bfe2009-05-08 10:31:42 +0800926out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400927 *ppos += cnt;
928
929 return ret;
930}
931
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400932enum {
933 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +0800934 FORMAT_FIELD_SEPERATOR = 2,
935 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400936};
937
938static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -0500939{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400940 struct trace_event_call *call = event_file_data(m->private);
Li Zefan86397dc2010-08-17 13:53:06 +0800941 struct list_head *common_head = &ftrace_common_fields;
942 struct list_head *head = trace_get_fields(call);
Oleg Nesterov7710b632013-07-18 20:47:10 +0200943 struct list_head *node = v;
Steven Rostedt981d0812009-03-02 13:53:59 -0500944
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400945 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800946
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400947 switch ((unsigned long)v) {
948 case FORMAT_HEADER:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200949 node = common_head;
950 break;
Li Zefan86397dc2010-08-17 13:53:06 +0800951
952 case FORMAT_FIELD_SEPERATOR:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200953 node = head;
954 break;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800955
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400956 case FORMAT_PRINTFMT:
957 /* all done */
958 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800959 }
960
Oleg Nesterov7710b632013-07-18 20:47:10 +0200961 node = node->prev;
962 if (node == common_head)
Li Zefan86397dc2010-08-17 13:53:06 +0800963 return (void *)FORMAT_FIELD_SEPERATOR;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200964 else if (node == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400965 return (void *)FORMAT_PRINTFMT;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200966 else
967 return node;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400968}
969
970static int f_show(struct seq_file *m, void *v)
971{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400972 struct trace_event_call *call = event_file_data(m->private);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400973 struct ftrace_event_field *field;
974 const char *array_descriptor;
975
976 switch ((unsigned long)v) {
977 case FORMAT_HEADER:
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400978 seq_printf(m, "name: %s\n", trace_event_name(call));
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400979 seq_printf(m, "ID: %d\n", call->event.type);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100980 seq_puts(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +0800981 return 0;
982
Li Zefan86397dc2010-08-17 13:53:06 +0800983 case FORMAT_FIELD_SEPERATOR:
984 seq_putc(m, '\n');
985 return 0;
986
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400987 case FORMAT_PRINTFMT:
988 seq_printf(m, "\nprint fmt: %s\n",
989 call->print_fmt);
990 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500991 }
992
Oleg Nesterov7710b632013-07-18 20:47:10 +0200993 field = list_entry(v, struct ftrace_event_field, link);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400994 /*
995 * Smartly shows the array type(except dynamic array).
996 * Normal:
997 * field:TYPE VAR
998 * If TYPE := TYPE[LEN], it is shown:
999 * field:TYPE VAR[LEN]
1000 */
1001 array_descriptor = strchr(field->type, '[');
1002
1003 if (!strncmp(field->type, "__data_loc", 10))
1004 array_descriptor = NULL;
1005
1006 if (!array_descriptor)
1007 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1008 field->type, field->name, field->offset,
1009 field->size, !!field->is_signed);
1010 else
1011 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1012 (int)(array_descriptor - field->type),
1013 field->type, field->name,
1014 array_descriptor, field->offset,
1015 field->size, !!field->is_signed);
1016
1017 return 0;
1018}
1019
Oleg Nesterov7710b632013-07-18 20:47:10 +02001020static void *f_start(struct seq_file *m, loff_t *pos)
1021{
1022 void *p = (void *)FORMAT_HEADER;
1023 loff_t l = 0;
1024
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001025 /* ->stop() is called even if ->start() fails */
1026 mutex_lock(&event_mutex);
1027 if (!event_file_data(m->private))
1028 return ERR_PTR(-ENODEV);
1029
Oleg Nesterov7710b632013-07-18 20:47:10 +02001030 while (l < *pos && p)
1031 p = f_next(m, p, &l);
1032
1033 return p;
1034}
1035
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001036static void f_stop(struct seq_file *m, void *p)
1037{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001038 mutex_unlock(&event_mutex);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001039}
1040
1041static const struct seq_operations trace_format_seq_ops = {
1042 .start = f_start,
1043 .next = f_next,
1044 .stop = f_stop,
1045 .show = f_show,
1046};
1047
1048static int trace_format_open(struct inode *inode, struct file *file)
1049{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001050 struct seq_file *m;
1051 int ret;
1052
1053 ret = seq_open(file, &trace_format_seq_ops);
1054 if (ret < 0)
1055 return ret;
1056
1057 m = file->private_data;
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001058 m->private = file;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001059
1060 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001061}
1062
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001063static ssize_t
1064event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1065{
Oleg Nesterov1a111262013-07-26 19:25:32 +02001066 int id = (long)event_file_data(filp);
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001067 char buf[32];
1068 int len;
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001069
1070 if (*ppos)
1071 return 0;
1072
Oleg Nesterov1a111262013-07-26 19:25:32 +02001073 if (unlikely(!id))
1074 return -ENODEV;
1075
1076 len = sprintf(buf, "%d\n", id);
1077
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001078 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001079}
1080
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001081static ssize_t
1082event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1083 loff_t *ppos)
1084{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001085 struct trace_event_file *file;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001086 struct trace_seq *s;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001087 int r = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001088
1089 if (*ppos)
1090 return 0;
1091
1092 s = kmalloc(sizeof(*s), GFP_KERNEL);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001093
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001094 if (!s)
1095 return -ENOMEM;
1096
1097 trace_seq_init(s);
1098
Oleg Nesterove2912b02013-07-26 19:25:40 +02001099 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001100 file = event_file_data(filp);
1101 if (file)
1102 print_event_filter(file, s);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001103 mutex_unlock(&event_mutex);
1104
Tom Zanussif306cc82013-10-24 08:34:17 -05001105 if (file)
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001106 r = simple_read_from_buffer(ubuf, cnt, ppos,
1107 s->buffer, trace_seq_used(s));
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001108
1109 kfree(s);
1110
1111 return r;
1112}
1113
1114static ssize_t
1115event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1116 loff_t *ppos)
1117{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001118 struct trace_event_file *file;
Tom Zanussi8b372562009-04-28 03:04:59 -05001119 char *buf;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001120 int err = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001121
Tom Zanussi8b372562009-04-28 03:04:59 -05001122 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001123 return -EINVAL;
1124
Tom Zanussi8b372562009-04-28 03:04:59 -05001125 buf = (char *)__get_free_page(GFP_TEMPORARY);
1126 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001127 return -ENOMEM;
1128
Tom Zanussi8b372562009-04-28 03:04:59 -05001129 if (copy_from_user(buf, ubuf, cnt)) {
1130 free_page((unsigned long) buf);
1131 return -EFAULT;
1132 }
1133 buf[cnt] = '\0';
1134
Oleg Nesterove2912b02013-07-26 19:25:40 +02001135 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001136 file = event_file_data(filp);
1137 if (file)
1138 err = apply_event_filter(file, buf);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001139 mutex_unlock(&event_mutex);
1140
Tom Zanussi8b372562009-04-28 03:04:59 -05001141 free_page((unsigned long) buf);
1142 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001143 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -05001144
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001145 *ppos += cnt;
1146
1147 return cnt;
1148}
1149
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001150static LIST_HEAD(event_subsystems);
1151
1152static int subsystem_open(struct inode *inode, struct file *filp)
1153{
1154 struct event_subsystem *system = NULL;
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001155 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001156 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001157 int ret;
1158
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001159 if (tracing_is_disabled())
1160 return -ENODEV;
1161
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001162 /* Make sure the system still exists */
Alexander Z Lama8227412013-07-01 19:37:54 -07001163 mutex_lock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001164 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001165 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1166 list_for_each_entry(dir, &tr->systems, list) {
1167 if (dir == inode->i_private) {
1168 /* Don't open systems with no events */
1169 if (dir->nr_events) {
1170 __get_system_dir(dir);
1171 system = dir->subsystem;
1172 }
1173 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001174 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001175 }
1176 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001177 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001178 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001179 mutex_unlock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001180
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001181 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001182 return -ENODEV;
1183
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001184 /* Some versions of gcc think dir can be uninitialized here */
1185 WARN_ON(!dir);
1186
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001187 /* Still need to increment the ref count of the system */
1188 if (trace_array_get(tr) < 0) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001189 put_system(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001190 return -ENODEV;
1191 }
1192
1193 ret = tracing_open_generic(inode, filp);
1194 if (ret < 0) {
1195 trace_array_put(tr);
1196 put_system(dir);
1197 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001198
1199 return ret;
1200}
1201
1202static int system_tr_open(struct inode *inode, struct file *filp)
1203{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001204 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001205 struct trace_array *tr = inode->i_private;
1206 int ret;
1207
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001208 if (tracing_is_disabled())
1209 return -ENODEV;
1210
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001211 if (trace_array_get(tr) < 0)
1212 return -ENODEV;
1213
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001214 /* Make a temporary dir that has no system but points to tr */
1215 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001216 if (!dir) {
1217 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001218 return -ENOMEM;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001219 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001220
1221 dir->tr = tr;
1222
1223 ret = tracing_open_generic(inode, filp);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001224 if (ret < 0) {
1225 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001226 kfree(dir);
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001227 return ret;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001228 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001229
1230 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001231
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001232 return 0;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001233}
1234
1235static int subsystem_release(struct inode *inode, struct file *file)
1236{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001237 struct trace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001238
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001239 trace_array_put(dir->tr);
1240
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001241 /*
1242 * If dir->subsystem is NULL, then this is a temporary
1243 * descriptor that was made for a trace_array to enable
1244 * all subsystems.
1245 */
1246 if (dir->subsystem)
1247 put_system(dir);
1248 else
1249 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001250
1251 return 0;
1252}
1253
Tom Zanussicfb180f2009-03-22 03:31:17 -05001254static ssize_t
1255subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1256 loff_t *ppos)
1257{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001258 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001259 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001260 struct trace_seq *s;
1261 int r;
1262
1263 if (*ppos)
1264 return 0;
1265
1266 s = kmalloc(sizeof(*s), GFP_KERNEL);
1267 if (!s)
1268 return -ENOMEM;
1269
1270 trace_seq_init(s);
1271
Tom Zanussi8b372562009-04-28 03:04:59 -05001272 print_subsystem_event_filter(system, s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001273 r = simple_read_from_buffer(ubuf, cnt, ppos,
1274 s->buffer, trace_seq_used(s));
Tom Zanussicfb180f2009-03-22 03:31:17 -05001275
1276 kfree(s);
1277
1278 return r;
1279}
1280
1281static ssize_t
1282subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1283 loff_t *ppos)
1284{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001285 struct trace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001286 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001287 int err;
1288
Tom Zanussi8b372562009-04-28 03:04:59 -05001289 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001290 return -EINVAL;
1291
Tom Zanussi8b372562009-04-28 03:04:59 -05001292 buf = (char *)__get_free_page(GFP_TEMPORARY);
1293 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001294 return -ENOMEM;
1295
Tom Zanussi8b372562009-04-28 03:04:59 -05001296 if (copy_from_user(buf, ubuf, cnt)) {
1297 free_page((unsigned long) buf);
1298 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001299 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001300 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001301
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001302 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001303 free_page((unsigned long) buf);
1304 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001305 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001306
1307 *ppos += cnt;
1308
1309 return cnt;
1310}
1311
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001312static ssize_t
1313show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1314{
1315 int (*func)(struct trace_seq *s) = filp->private_data;
1316 struct trace_seq *s;
1317 int r;
1318
1319 if (*ppos)
1320 return 0;
1321
1322 s = kmalloc(sizeof(*s), GFP_KERNEL);
1323 if (!s)
1324 return -ENOMEM;
1325
1326 trace_seq_init(s);
1327
1328 func(s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001329 r = simple_read_from_buffer(ubuf, cnt, ppos,
1330 s->buffer, trace_seq_used(s));
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001331
1332 kfree(s);
1333
1334 return r;
1335}
1336
Steven Rostedt15075ca2012-05-03 14:57:28 -04001337static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1338static int ftrace_event_set_open(struct inode *inode, struct file *file);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001339static int ftrace_event_release(struct inode *inode, struct file *file);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001340
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001341static const struct seq_operations show_event_seq_ops = {
1342 .start = t_start,
1343 .next = t_next,
1344 .show = t_show,
1345 .stop = t_stop,
1346};
1347
1348static const struct seq_operations show_set_event_seq_ops = {
1349 .start = s_start,
1350 .next = s_next,
1351 .show = t_show,
1352 .stop = t_stop,
1353};
1354
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001355static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001356 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001357 .read = seq_read,
1358 .llseek = seq_lseek,
1359 .release = seq_release,
1360};
1361
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001362static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001363 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001364 .read = seq_read,
1365 .write = ftrace_event_write,
1366 .llseek = seq_lseek,
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001367 .release = ftrace_event_release,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001368};
1369
Steven Rostedt1473e442009-02-24 14:15:08 -05001370static const struct file_operations ftrace_enable_fops = {
Oleg Nesterovbf682c32013-07-28 20:35:27 +02001371 .open = tracing_open_generic,
Steven Rostedt1473e442009-02-24 14:15:08 -05001372 .read = event_enable_read,
1373 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001374 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001375};
1376
Steven Rostedt981d0812009-03-02 13:53:59 -05001377static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001378 .open = trace_format_open,
1379 .read = seq_read,
1380 .llseek = seq_lseek,
1381 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001382};
1383
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001384static const struct file_operations ftrace_event_id_fops = {
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001385 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001386 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001387};
1388
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001389static const struct file_operations ftrace_event_filter_fops = {
1390 .open = tracing_open_generic,
1391 .read = event_filter_read,
1392 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001393 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001394};
1395
Tom Zanussicfb180f2009-03-22 03:31:17 -05001396static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001397 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001398 .read = subsystem_filter_read,
1399 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001400 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001401 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001402};
1403
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001404static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001405 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001406 .read = system_enable_read,
1407 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001408 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001409 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001410};
1411
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001412static const struct file_operations ftrace_tr_enable_fops = {
1413 .open = system_tr_open,
1414 .read = system_enable_read,
1415 .write = system_enable_write,
1416 .llseek = default_llseek,
1417 .release = subsystem_release,
1418};
1419
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001420static const struct file_operations ftrace_show_header_fops = {
1421 .open = tracing_open_generic,
1422 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001423 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001424};
1425
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001426static int
1427ftrace_event_open(struct inode *inode, struct file *file,
1428 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001429{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001430 struct seq_file *m;
1431 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001432
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001433 ret = seq_open(file, seq_ops);
1434 if (ret < 0)
1435 return ret;
1436 m = file->private_data;
1437 /* copy tr over to seq ops */
1438 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001439
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001440 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001441}
1442
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001443static int ftrace_event_release(struct inode *inode, struct file *file)
1444{
1445 struct trace_array *tr = inode->i_private;
1446
1447 trace_array_put(tr);
1448
1449 return seq_release(inode, file);
1450}
1451
Steven Rostedt15075ca2012-05-03 14:57:28 -04001452static int
1453ftrace_event_avail_open(struct inode *inode, struct file *file)
1454{
1455 const struct seq_operations *seq_ops = &show_event_seq_ops;
1456
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001457 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001458}
1459
1460static int
1461ftrace_event_set_open(struct inode *inode, struct file *file)
1462{
1463 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001464 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001465 int ret;
1466
1467 if (trace_array_get(tr) < 0)
1468 return -ENODEV;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001469
1470 if ((file->f_mode & FMODE_WRITE) &&
1471 (file->f_flags & O_TRUNC))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001472 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001473
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001474 ret = ftrace_event_open(inode, file, seq_ops);
1475 if (ret < 0)
1476 trace_array_put(tr);
1477 return ret;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001478}
1479
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001480static struct event_subsystem *
1481create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001482{
1483 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001484
1485 /* need to create new entry */
1486 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001487 if (!system)
1488 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001489
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001490 system->ref_count = 1;
Steven Rostedt6e94a782013-06-27 10:58:31 -04001491
1492 /* Only allocate if dynamic (kprobes and modules) */
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001493 system->name = kstrdup_const(name, GFP_KERNEL);
1494 if (!system->name)
1495 goto out_free;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001496
Tom Zanussi30e673b2009-04-28 03:04:47 -05001497 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001498
Tom Zanussi8b372562009-04-28 03:04:59 -05001499 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001500 if (!system->filter)
1501 goto out_free;
1502
1503 list_add(&system->list, &event_subsystems);
1504
1505 return system;
1506
1507 out_free:
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001508 kfree_const(system->name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001509 kfree(system);
1510 return NULL;
1511}
1512
1513static struct dentry *
1514event_subsystem_dir(struct trace_array *tr, const char *name,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001515 struct trace_event_file *file, struct dentry *parent)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001516{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001517 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001518 struct event_subsystem *system;
1519 struct dentry *entry;
1520
1521 /* First see if we did not already create this dir */
1522 list_for_each_entry(dir, &tr->systems, list) {
1523 system = dir->subsystem;
1524 if (strcmp(system->name, name) == 0) {
1525 dir->nr_events++;
1526 file->system = dir;
1527 return dir->entry;
1528 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001529 }
1530
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001531 /* Now see if the system itself exists. */
1532 list_for_each_entry(system, &event_subsystems, list) {
1533 if (strcmp(system->name, name) == 0)
1534 break;
1535 }
1536 /* Reset system variable when not found */
1537 if (&system->list == &event_subsystems)
1538 system = NULL;
1539
1540 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1541 if (!dir)
1542 goto out_fail;
1543
1544 if (!system) {
1545 system = create_new_subsystem(name);
1546 if (!system)
1547 goto out_free;
1548 } else
1549 __get_system(system);
1550
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001551 dir->entry = tracefs_create_dir(name, parent);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001552 if (!dir->entry) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001553 pr_warn("Failed to create system directory %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001554 __put_system(system);
1555 goto out_free;
1556 }
1557
1558 dir->tr = tr;
1559 dir->ref_count = 1;
1560 dir->nr_events = 1;
1561 dir->subsystem = system;
1562 file->system = dir;
1563
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001564 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001565 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001566 if (!entry) {
1567 kfree(system->filter);
1568 system->filter = NULL;
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001569 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001570 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001571
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001572 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001573 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001574
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001575 list_add(&dir->list, &tr->systems);
1576
1577 return dir->entry;
1578
1579 out_free:
1580 kfree(dir);
1581 out_fail:
1582 /* Only print this message if failed on memory allocation */
1583 if (!dir || !system)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001584 pr_warn("No memory to create event subsystem %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001585 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001586}
1587
Steven Rostedt1473e442009-02-24 14:15:08 -05001588static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001589event_create_dir(struct dentry *parent, struct trace_event_file *file)
Steven Rostedt1473e442009-02-24 14:15:08 -05001590{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001591 struct trace_event_call *call = file->event_call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001592 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001593 struct list_head *head;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001594 struct dentry *d_events;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001595 const char *name;
Steven Rostedtfd994982009-02-28 02:41:25 -05001596 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001597
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001598 /*
1599 * If the trace point header did not define TRACE_SYSTEM
1600 * then the system would be called "TRACE_SYSTEM".
1601 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001602 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1603 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1604 if (!d_events)
1605 return -ENOMEM;
1606 } else
1607 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001608
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001609 name = trace_event_name(call);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001610 file->dir = tracefs_create_dir(name, d_events);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001611 if (!file->dir) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001612 pr_warn("Could not create tracefs '%s' directory\n", name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001613 return -1;
1614 }
1615
Steven Rostedt9b637762012-05-10 15:55:43 -04001616 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001617 trace_create_file("enable", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001618 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001619
Steven Rostedt22392912010-04-21 12:27:06 -04001620#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001621 if (call->event.type && call->class->reg)
Oleg Nesterov1a111262013-07-26 19:25:32 +02001622 trace_create_file("id", 0444, file->dir,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001623 (void *)(long)call->event.type,
1624 &ftrace_event_id_fops);
Steven Rostedt22392912010-04-21 12:27:06 -04001625#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001626
Li Zefanc9d932c2010-05-24 16:24:28 +08001627 /*
1628 * Other events may have the same class. Only update
1629 * the fields if they are not already defined.
1630 */
1631 head = trace_get_fields(call);
1632 if (list_empty(head)) {
1633 ret = call->class->define_fields(call);
1634 if (ret < 0) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001635 pr_warn("Could not initialize trace point events/%s\n",
1636 name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001637 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001638 }
1639 }
Tom Zanussif306cc82013-10-24 08:34:17 -05001640 trace_create_file("filter", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001641 &ftrace_event_filter_fops);
Tom Zanussicf027f62009-03-22 03:30:39 -05001642
Tom Zanussi85f2b082013-10-24 08:59:24 -05001643 trace_create_file("trigger", 0644, file->dir, file,
1644 &event_trigger_fops);
1645
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001646 trace_create_file("format", 0444, file->dir, call,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001647 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -05001648
Steven Rostedt1473e442009-02-24 14:15:08 -05001649 return 0;
1650}
1651
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001652static void remove_event_from_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001653{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001654 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001655 struct trace_array *tr;
1656
1657 do_for_each_event_file_safe(tr, file) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001658 if (file->event_call != call)
1659 continue;
1660
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02001661 remove_event_file_dir(file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001662 /*
1663 * The do_for_each_event_file_safe() is
1664 * a double loop. After finding the call for this
1665 * trace_array, we use break to jump to the next
1666 * trace_array.
1667 */
1668 break;
1669 } while_for_each_event_file();
1670}
1671
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001672static void event_remove(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03001673{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001674 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001675 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001676
1677 do_for_each_event_file(tr, file) {
1678 if (file->event_call != call)
1679 continue;
1680 ftrace_event_enable_disable(file, 0);
1681 /*
1682 * The do_for_each_event_file() is
1683 * a double loop. After finding the call for this
1684 * trace_array, we use break to jump to the next
1685 * trace_array.
1686 */
1687 break;
1688 } while_for_each_event_file();
1689
Ezequiel Garcia87819152012-09-12 11:47:57 -03001690 if (call->event.funcs)
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001691 __unregister_trace_event(&call->event);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001692 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001693 list_del(&call->list);
1694}
1695
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001696static int event_init(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03001697{
1698 int ret = 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001699 const char *name;
Ezequiel Garcia87819152012-09-12 11:47:57 -03001700
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001701 name = trace_event_name(call);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001702 if (WARN_ON(!name))
Ezequiel Garcia87819152012-09-12 11:47:57 -03001703 return -EINVAL;
1704
1705 if (call->class->raw_init) {
1706 ret = call->class->raw_init(call);
1707 if (ret < 0 && ret != -ENOSYS)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001708 pr_warn("Could not initialize trace events/%s\n", name);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001709 }
1710
1711 return ret;
1712}
1713
Li Zefan67ead0a2010-05-24 16:25:13 +08001714static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001715__register_event(struct trace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001716{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001717 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001718
Ezequiel Garcia87819152012-09-12 11:47:57 -03001719 ret = event_init(call);
1720 if (ret < 0)
1721 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04001722
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001723 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08001724 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001725
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001726 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001727}
1728
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001729static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
1730{
1731 int rlen;
1732 int elen;
1733
1734 /* Find the length of the enum value as a string */
1735 elen = snprintf(ptr, 0, "%ld", map->enum_value);
1736 /* Make sure there's enough room to replace the string with the value */
1737 if (len < elen)
1738 return NULL;
1739
1740 snprintf(ptr, elen + 1, "%ld", map->enum_value);
1741
1742 /* Get the rest of the string of ptr */
1743 rlen = strlen(ptr + len);
1744 memmove(ptr + elen, ptr + len, rlen);
1745 /* Make sure we end the new string */
1746 ptr[elen + rlen] = 0;
1747
1748 return ptr + elen;
1749}
1750
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001751static void update_event_printk(struct trace_event_call *call,
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001752 struct trace_enum_map *map)
1753{
1754 char *ptr;
1755 int quote = 0;
1756 int len = strlen(map->enum_string);
1757
1758 for (ptr = call->print_fmt; *ptr; ptr++) {
1759 if (*ptr == '\\') {
1760 ptr++;
1761 /* paranoid */
1762 if (!*ptr)
1763 break;
1764 continue;
1765 }
1766 if (*ptr == '"') {
1767 quote ^= 1;
1768 continue;
1769 }
1770 if (quote)
1771 continue;
1772 if (isdigit(*ptr)) {
1773 /* skip numbers */
1774 do {
1775 ptr++;
1776 /* Check for alpha chars like ULL */
1777 } while (isalnum(*ptr));
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04001778 if (!*ptr)
1779 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001780 /*
1781 * A number must have some kind of delimiter after
1782 * it, and we can ignore that too.
1783 */
1784 continue;
1785 }
1786 if (isalpha(*ptr) || *ptr == '_') {
1787 if (strncmp(map->enum_string, ptr, len) == 0 &&
1788 !isalnum(ptr[len]) && ptr[len] != '_') {
1789 ptr = enum_replace(ptr, map, len);
1790 /* Hmm, enum string smaller than value */
1791 if (WARN_ON_ONCE(!ptr))
1792 return;
1793 /*
1794 * No need to decrement here, as enum_replace()
1795 * returns the pointer to the character passed
1796 * the enum, and two enums can not be placed
1797 * back to back without something in between.
1798 * We can skip that something in between.
1799 */
1800 continue;
1801 }
1802 skip_more:
1803 do {
1804 ptr++;
1805 } while (isalnum(*ptr) || *ptr == '_');
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04001806 if (!*ptr)
1807 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001808 /*
1809 * If what comes after this variable is a '.' or
1810 * '->' then we can continue to ignore that string.
1811 */
1812 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
1813 ptr += *ptr == '.' ? 1 : 2;
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04001814 if (!*ptr)
1815 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001816 goto skip_more;
1817 }
1818 /*
1819 * Once again, we can skip the delimiter that came
1820 * after the string.
1821 */
1822 continue;
1823 }
1824 }
1825}
1826
1827void trace_event_enum_update(struct trace_enum_map **map, int len)
1828{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001829 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001830 const char *last_system = NULL;
1831 int last_i;
1832 int i;
1833
1834 down_write(&trace_event_sem);
1835 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1836 /* events are usually grouped together with systems */
1837 if (!last_system || call->class->system != last_system) {
1838 last_i = 0;
1839 last_system = call->class->system;
1840 }
1841
1842 for (i = last_i; i < len; i++) {
1843 if (call->class->system == map[i]->system) {
1844 /* Save the first system if need be */
1845 if (!last_i)
1846 last_i = i;
1847 update_event_printk(call, map[i]);
1848 }
1849 }
1850 }
1851 up_write(&trace_event_sem);
1852}
1853
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001854static struct trace_event_file *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001855trace_create_new_event(struct trace_event_call *call,
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001856 struct trace_array *tr)
1857{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001858 struct trace_event_file *file;
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001859
1860 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1861 if (!file)
1862 return NULL;
1863
1864 file->event_call = call;
1865 file->tr = tr;
1866 atomic_set(&file->sm_ref, 0);
Tom Zanussi85f2b082013-10-24 08:59:24 -05001867 atomic_set(&file->tm_ref, 0);
1868 INIT_LIST_HEAD(&file->triggers);
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001869 list_add(&file->list, &tr->events);
1870
1871 return file;
1872}
1873
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001874/* Add an event to a trace directory */
1875static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001876__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001877{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001878 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001879
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001880 file = trace_create_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001881 if (!file)
1882 return -ENOMEM;
1883
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001884 return event_create_dir(tr->event_dir, file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001885}
1886
Steven Rostedt77248222013-02-27 16:28:06 -05001887/*
1888 * Just create a decriptor for early init. A descriptor is required
1889 * for enabling events at boot. We want to enable events before
1890 * the filesystem is initialized.
1891 */
1892static __init int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001893__trace_early_add_new_event(struct trace_event_call *call,
Steven Rostedt77248222013-02-27 16:28:06 -05001894 struct trace_array *tr)
1895{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001896 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05001897
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001898 file = trace_create_new_event(call, tr);
Steven Rostedt77248222013-02-27 16:28:06 -05001899 if (!file)
1900 return -ENOMEM;
1901
Steven Rostedt77248222013-02-27 16:28:06 -05001902 return 0;
1903}
1904
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001905struct ftrace_module_file_ops;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001906static void __add_event_to_tracers(struct trace_event_call *call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001907
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001908/* Add an additional event_call dynamically */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001909int trace_add_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001910{
1911 int ret;
Alexander Z Lama8227412013-07-01 19:37:54 -07001912 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001913 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001914
1915 ret = __register_event(call, NULL);
1916 if (ret >= 0)
Oleg Nesterov779c5e32013-07-31 19:31:32 +02001917 __add_event_to_tracers(call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001918
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001919 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001920 mutex_unlock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001921 return ret;
1922}
Steven Rostedt701970b2009-04-24 23:11:22 -04001923
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001924/*
Alexander Z Lama8227412013-07-01 19:37:54 -07001925 * Must be called under locking of trace_types_lock, event_mutex and
1926 * trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001927 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001928static void __trace_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001929{
Ezequiel Garcia87819152012-09-12 11:47:57 -03001930 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001931 trace_destroy_fields(call);
Oleg Nesterov57375742014-07-15 20:48:16 +02001932 free_event_filter(call->filter);
1933 call->filter = NULL;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001934}
1935
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001936static int probe_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001937{
Oleg Nesterov2816c552013-07-29 19:50:33 +02001938 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001939 struct trace_event_file *file;
Oleg Nesterov2816c552013-07-29 19:50:33 +02001940
1941#ifdef CONFIG_PERF_EVENTS
1942 if (call->perf_refcount)
1943 return -EBUSY;
1944#endif
1945 do_for_each_event_file(tr, file) {
1946 if (file->event_call != call)
1947 continue;
1948 /*
1949 * We can't rely on ftrace_event_enable_disable(enable => 0)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001950 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
Oleg Nesterov2816c552013-07-29 19:50:33 +02001951 * TRACE_REG_UNREGISTER.
1952 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001953 if (file->flags & EVENT_FILE_FL_ENABLED)
Oleg Nesterov2816c552013-07-29 19:50:33 +02001954 return -EBUSY;
Steven Rostedt (Red Hat)2ba64032013-07-31 13:16:22 -04001955 /*
1956 * The do_for_each_event_file_safe() is
1957 * a double loop. After finding the call for this
1958 * trace_array, we use break to jump to the next
1959 * trace_array.
1960 */
Oleg Nesterov2816c552013-07-29 19:50:33 +02001961 break;
1962 } while_for_each_event_file();
1963
1964 __trace_remove_event_call(call);
1965
1966 return 0;
1967}
1968
1969/* Remove an event_call */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001970int trace_remove_event_call(struct trace_event_call *call)
Oleg Nesterov2816c552013-07-29 19:50:33 +02001971{
1972 int ret;
1973
Alexander Z Lama8227412013-07-01 19:37:54 -07001974 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001975 mutex_lock(&event_mutex);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001976 down_write(&trace_event_sem);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001977 ret = probe_remove_event_call(call);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001978 up_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001979 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001980 mutex_unlock(&trace_types_lock);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001981
1982 return ret;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001983}
1984
1985#define for_each_event(event, start, end) \
1986 for (event = start; \
1987 (unsigned long)event < (unsigned long)end; \
1988 event++)
1989
1990#ifdef CONFIG_MODULES
1991
Steven Rostedt6d723732009-04-10 14:53:50 -04001992static void trace_module_add_events(struct module *mod)
1993{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001994 struct trace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04001995
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -05001996 if (!mod->num_trace_events)
1997 return;
1998
1999 /* Don't add infrastructure for mods without tracepoints */
2000 if (trace_module_has_bad_taint(mod)) {
2001 pr_err("%s: module has bad taint, not creating trace events\n",
2002 mod->name);
2003 return;
2004 }
2005
Steven Rostedt6d723732009-04-10 14:53:50 -04002006 start = mod->trace_events;
2007 end = mod->trace_events + mod->num_trace_events;
2008
Steven Rostedt6d723732009-04-10 14:53:50 -04002009 for_each_event(call, start, end) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002010 __register_event(*call, mod);
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002011 __add_event_to_tracers(*call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002012 }
2013}
2014
2015static void trace_module_remove_events(struct module *mod)
2016{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002017 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002018 bool clear_trace = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04002019
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002020 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04002021 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2022 if (call->mod == mod) {
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002023 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2024 clear_trace = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002025 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002026 }
2027 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002028 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002029
2030 /*
2031 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002032 * registered any events that were used. The only worry is if
2033 * a new module gets loaded, and takes on the same id as the events
2034 * of this module. When printing out the buffer, traced events left
2035 * over from this module may be passed to the new module events and
2036 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002037 */
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002038 if (clear_trace)
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002039 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04002040}
2041
Steven Rostedt61f919a2009-04-14 18:22:32 -04002042static int trace_module_notify(struct notifier_block *self,
2043 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04002044{
2045 struct module *mod = data;
2046
Alexander Z Lama8227412013-07-01 19:37:54 -07002047 mutex_lock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002048 mutex_lock(&event_mutex);
2049 switch (val) {
2050 case MODULE_STATE_COMING:
2051 trace_module_add_events(mod);
2052 break;
2053 case MODULE_STATE_GOING:
2054 trace_module_remove_events(mod);
2055 break;
2056 }
2057 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002058 mutex_unlock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002059
2060 return 0;
2061}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05002062
Oleg Nesterov836d4812013-07-31 19:31:37 +02002063static struct notifier_block trace_module_nb = {
2064 .notifier_call = trace_module_notify,
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002065 .priority = 1, /* higher than trace.c module notify */
Oleg Nesterov836d4812013-07-31 19:31:37 +02002066};
Steven Rostedt61f919a2009-04-14 18:22:32 -04002067#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04002068
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002069/* Create a new event directory structure for a trace directory. */
2070static void
2071__trace_add_event_dirs(struct trace_array *tr)
2072{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002073 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002074 int ret;
2075
2076 list_for_each_entry(call, &ftrace_events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002077 ret = __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002078 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002079 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002080 trace_event_name(call));
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002081 }
2082}
2083
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002084struct trace_event_file *
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002085find_event_file(struct trace_array *tr, const char *system, const char *event)
2086{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002087 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002088 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002089 const char *name;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002090
2091 list_for_each_entry(file, &tr->events, list) {
2092
2093 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002094 name = trace_event_name(call);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002095
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002096 if (!name || !call->class || !call->class->reg)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002097 continue;
2098
2099 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2100 continue;
2101
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002102 if (strcmp(event, name) == 0 &&
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002103 strcmp(system, call->class->system) == 0)
2104 return file;
2105 }
2106 return NULL;
2107}
2108
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002109#ifdef CONFIG_DYNAMIC_FTRACE
2110
2111/* Avoid typos */
2112#define ENABLE_EVENT_STR "enable_event"
2113#define DISABLE_EVENT_STR "disable_event"
2114
2115struct event_probe_data {
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002116 struct trace_event_file *file;
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002117 unsigned long count;
2118 int ref;
2119 bool enable;
2120};
2121
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002122static void
2123event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2124{
2125 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2126 struct event_probe_data *data = *pdata;
2127
2128 if (!data)
2129 return;
2130
2131 if (data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002132 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002133 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002134 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002135}
2136
2137static void
2138event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2139{
2140 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2141 struct event_probe_data *data = *pdata;
2142
2143 if (!data)
2144 return;
2145
2146 if (!data->count)
2147 return;
2148
2149 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002150 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002151 return;
2152
2153 if (data->count != -1)
2154 (data->count)--;
2155
2156 event_enable_probe(ip, parent_ip, _data);
2157}
2158
2159static int
2160event_enable_print(struct seq_file *m, unsigned long ip,
2161 struct ftrace_probe_ops *ops, void *_data)
2162{
2163 struct event_probe_data *data = _data;
2164
2165 seq_printf(m, "%ps:", (void *)ip);
2166
2167 seq_printf(m, "%s:%s:%s",
2168 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2169 data->file->event_call->class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002170 trace_event_name(data->file->event_call));
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002171
2172 if (data->count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002173 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002174 else
2175 seq_printf(m, ":count=%ld\n", data->count);
2176
2177 return 0;
2178}
2179
2180static int
2181event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2182 void **_data)
2183{
2184 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2185 struct event_probe_data *data = *pdata;
2186
2187 data->ref++;
2188 return 0;
2189}
2190
2191static void
2192event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2193 void **_data)
2194{
2195 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2196 struct event_probe_data *data = *pdata;
2197
2198 if (WARN_ON_ONCE(data->ref <= 0))
2199 return;
2200
2201 data->ref--;
2202 if (!data->ref) {
2203 /* Remove the SOFT_MODE flag */
2204 __ftrace_event_enable_disable(data->file, 0, 1);
2205 module_put(data->file->event_call->mod);
2206 kfree(data);
2207 }
2208 *pdata = NULL;
2209}
2210
2211static struct ftrace_probe_ops event_enable_probe_ops = {
2212 .func = event_enable_probe,
2213 .print = event_enable_print,
2214 .init = event_enable_init,
2215 .free = event_enable_free,
2216};
2217
2218static struct ftrace_probe_ops event_enable_count_probe_ops = {
2219 .func = event_enable_count_probe,
2220 .print = event_enable_print,
2221 .init = event_enable_init,
2222 .free = event_enable_free,
2223};
2224
2225static struct ftrace_probe_ops event_disable_probe_ops = {
2226 .func = event_enable_probe,
2227 .print = event_enable_print,
2228 .init = event_enable_init,
2229 .free = event_enable_free,
2230};
2231
2232static struct ftrace_probe_ops event_disable_count_probe_ops = {
2233 .func = event_enable_count_probe,
2234 .print = event_enable_print,
2235 .init = event_enable_init,
2236 .free = event_enable_free,
2237};
2238
2239static int
2240event_enable_func(struct ftrace_hash *hash,
2241 char *glob, char *cmd, char *param, int enabled)
2242{
2243 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002244 struct trace_event_file *file;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002245 struct ftrace_probe_ops *ops;
2246 struct event_probe_data *data;
2247 const char *system;
2248 const char *event;
2249 char *number;
2250 bool enable;
2251 int ret;
2252
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002253 if (!tr)
2254 return -ENODEV;
2255
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002256 /* hash funcs only work with set_ftrace_filter */
Harsh Prateek Bora8092e802013-05-24 12:52:17 +05302257 if (!enabled || !param)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002258 return -EINVAL;
2259
2260 system = strsep(&param, ":");
2261 if (!param)
2262 return -EINVAL;
2263
2264 event = strsep(&param, ":");
2265
2266 mutex_lock(&event_mutex);
2267
2268 ret = -EINVAL;
2269 file = find_event_file(tr, system, event);
2270 if (!file)
2271 goto out;
2272
2273 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2274
2275 if (enable)
2276 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2277 else
2278 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2279
2280 if (glob[0] == '!') {
2281 unregister_ftrace_function_probe_func(glob+1, ops);
2282 ret = 0;
2283 goto out;
2284 }
2285
2286 ret = -ENOMEM;
2287 data = kzalloc(sizeof(*data), GFP_KERNEL);
2288 if (!data)
2289 goto out;
2290
2291 data->enable = enable;
2292 data->count = -1;
2293 data->file = file;
2294
2295 if (!param)
2296 goto out_reg;
2297
2298 number = strsep(&param, ":");
2299
2300 ret = -EINVAL;
2301 if (!strlen(number))
2302 goto out_free;
2303
2304 /*
2305 * We use the callback data field (which is a pointer)
2306 * as our counter.
2307 */
2308 ret = kstrtoul(number, 0, &data->count);
2309 if (ret)
2310 goto out_free;
2311
2312 out_reg:
2313 /* Don't let event modules unload while probe registered */
2314 ret = try_module_get(file->event_call->mod);
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002315 if (!ret) {
2316 ret = -EBUSY;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002317 goto out_free;
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002318 }
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002319
2320 ret = __ftrace_event_enable_disable(file, 1, 1);
2321 if (ret < 0)
2322 goto out_put;
2323 ret = register_ftrace_function_probe(glob, ops, data);
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002324 /*
2325 * The above returns on success the # of functions enabled,
2326 * but if it didn't find any functions it returns zero.
2327 * Consider no functions a failure too.
2328 */
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002329 if (!ret) {
2330 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002331 goto out_disable;
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002332 } else if (ret < 0)
2333 goto out_disable;
2334 /* Just return zero, not the number of enabled functions */
2335 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002336 out:
2337 mutex_unlock(&event_mutex);
2338 return ret;
2339
2340 out_disable:
2341 __ftrace_event_enable_disable(file, 0, 1);
2342 out_put:
2343 module_put(file->event_call->mod);
2344 out_free:
2345 kfree(data);
2346 goto out;
2347}
2348
2349static struct ftrace_func_command event_enable_cmd = {
2350 .name = ENABLE_EVENT_STR,
2351 .func = event_enable_func,
2352};
2353
2354static struct ftrace_func_command event_disable_cmd = {
2355 .name = DISABLE_EVENT_STR,
2356 .func = event_enable_func,
2357};
2358
2359static __init int register_event_cmds(void)
2360{
2361 int ret;
2362
2363 ret = register_ftrace_command(&event_enable_cmd);
2364 if (WARN_ON(ret < 0))
2365 return ret;
2366 ret = register_ftrace_command(&event_disable_cmd);
2367 if (WARN_ON(ret < 0))
2368 unregister_ftrace_command(&event_enable_cmd);
2369 return ret;
2370}
2371#else
2372static inline int register_event_cmds(void) { return 0; }
2373#endif /* CONFIG_DYNAMIC_FTRACE */
2374
Steven Rostedt77248222013-02-27 16:28:06 -05002375/*
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002376 * The top level array has already had its trace_event_file
Steven Rostedt77248222013-02-27 16:28:06 -05002377 * descriptors created in order to allow for early events to
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002378 * be recorded. This function is called after the tracefs has been
Steven Rostedt77248222013-02-27 16:28:06 -05002379 * initialized, and we now have to create the files associated
2380 * to the events.
2381 */
2382static __init void
2383__trace_early_add_event_dirs(struct trace_array *tr)
2384{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002385 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002386 int ret;
2387
2388
2389 list_for_each_entry(file, &tr->events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002390 ret = event_create_dir(tr->event_dir, file);
Steven Rostedt77248222013-02-27 16:28:06 -05002391 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002392 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002393 trace_event_name(file->event_call));
Steven Rostedt77248222013-02-27 16:28:06 -05002394 }
2395}
2396
2397/*
2398 * For early boot up, the top trace array requires to have
2399 * a list of events that can be enabled. This must be done before
2400 * the filesystem is set up in order to allow events to be traced
2401 * early.
2402 */
2403static __init void
2404__trace_early_add_events(struct trace_array *tr)
2405{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002406 struct trace_event_call *call;
Steven Rostedt77248222013-02-27 16:28:06 -05002407 int ret;
2408
2409 list_for_each_entry(call, &ftrace_events, list) {
2410 /* Early boot up should not have any modules loaded */
2411 if (WARN_ON_ONCE(call->mod))
2412 continue;
2413
2414 ret = __trace_early_add_new_event(call, tr);
2415 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002416 pr_warn("Could not create early event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002417 trace_event_name(call));
Steven Rostedt77248222013-02-27 16:28:06 -05002418 }
2419}
2420
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002421/* Remove the event directory structure for a trace directory. */
2422static void
2423__trace_remove_event_dirs(struct trace_array *tr)
2424{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002425 struct trace_event_file *file, *next;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002426
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002427 list_for_each_entry_safe(file, next, &tr->events, list)
2428 remove_event_file_dir(file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002429}
2430
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002431static void __add_event_to_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002432{
2433 struct trace_array *tr;
2434
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002435 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2436 __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002437}
2438
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002439extern struct trace_event_call *__start_ftrace_events[];
2440extern struct trace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002441
Li Zefan020e5f82009-07-01 10:47:05 +08002442static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2443
2444static __init int setup_trace_event(char *str)
2445{
2446 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002447 ring_buffer_expanded = true;
2448 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002449
2450 return 1;
2451}
2452__setup("trace_event=", setup_trace_event);
2453
Steven Rostedt77248222013-02-27 16:28:06 -05002454/* Expects to have event_mutex held when called */
2455static int
2456create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002457{
2458 struct dentry *d_events;
2459 struct dentry *entry;
2460
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002461 entry = tracefs_create_file("set_event", 0644, parent,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002462 tr, &ftrace_set_event_fops);
2463 if (!entry) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002464 pr_warn("Could not create tracefs 'set_event' entry\n");
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002465 return -ENOMEM;
2466 }
2467
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002468 d_events = tracefs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002469 if (!d_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002470 pr_warn("Could not create tracefs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002471 return -ENOMEM;
2472 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002473
2474 /* ring buffer internal formats */
2475 trace_create_file("header_page", 0444, d_events,
2476 ring_buffer_print_page_header,
2477 &ftrace_show_header_fops);
2478
2479 trace_create_file("header_event", 0444, d_events,
2480 ring_buffer_print_entry_header,
2481 &ftrace_show_header_fops);
2482
2483 trace_create_file("enable", 0644, d_events,
2484 tr, &ftrace_tr_enable_fops);
2485
2486 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002487
2488 return 0;
2489}
2490
2491/**
2492 * event_trace_add_tracer - add a instance of a trace_array to events
2493 * @parent: The parent dentry to place the files/directories for events in
2494 * @tr: The trace array associated with these events
2495 *
2496 * When a new instance is created, it needs to set up its events
2497 * directory, as well as other files associated with events. It also
2498 * creates the event hierachry in the @parent/events directory.
2499 *
2500 * Returns 0 on success.
2501 */
2502int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2503{
2504 int ret;
2505
2506 mutex_lock(&event_mutex);
2507
2508 ret = create_event_toplevel_files(parent, tr);
2509 if (ret)
2510 goto out_unlock;
2511
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002512 down_write(&trace_event_sem);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002513 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002514 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002515
Steven Rostedt77248222013-02-27 16:28:06 -05002516 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04002517 mutex_unlock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002518
Steven Rostedt77248222013-02-27 16:28:06 -05002519 return ret;
2520}
2521
2522/*
2523 * The top trace array already had its file descriptors created.
2524 * Now the files themselves need to be created.
2525 */
2526static __init int
2527early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2528{
2529 int ret;
2530
2531 mutex_lock(&event_mutex);
2532
2533 ret = create_event_toplevel_files(parent, tr);
2534 if (ret)
2535 goto out_unlock;
2536
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002537 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002538 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002539 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002540
2541 out_unlock:
2542 mutex_unlock(&event_mutex);
2543
2544 return ret;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002545}
2546
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002547int event_trace_del_tracer(struct trace_array *tr)
2548{
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002549 mutex_lock(&event_mutex);
2550
Tom Zanussi85f2b082013-10-24 08:59:24 -05002551 /* Disable any event triggers and associated soft-disabled events */
2552 clear_event_triggers(tr);
2553
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -04002554 /* Disable any running events */
2555 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2556
Steven Rostedt3ccb0122013-12-03 12:41:20 -05002557 /* Access to events are within rcu_read_lock_sched() */
2558 synchronize_sched();
2559
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002560 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002561 __trace_remove_event_dirs(tr);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002562 tracefs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002563 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002564
2565 tr->event_dir = NULL;
2566
2567 mutex_unlock(&event_mutex);
2568
2569 return 0;
2570}
2571
Steven Rostedtd1a29142013-02-27 20:23:57 -05002572static __init int event_trace_memsetup(void)
2573{
2574 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002575 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
Steven Rostedtd1a29142013-02-27 20:23:57 -05002576 return 0;
2577}
2578
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002579static __init void
2580early_enable_events(struct trace_array *tr, bool disable_first)
2581{
2582 char *buf = bootup_event_buf;
2583 char *token;
2584 int ret;
2585
2586 while (true) {
2587 token = strsep(&buf, ",");
2588
2589 if (!token)
2590 break;
2591 if (!*token)
2592 continue;
2593
2594 /* Restarting syscalls requires that we stop them first */
2595 if (disable_first)
2596 ftrace_set_clr_event(tr, token, 0);
2597
2598 ret = ftrace_set_clr_event(tr, token, 1);
2599 if (ret)
2600 pr_warn("Failed to enable trace event: %s\n", token);
2601
2602 /* Put back the comma to allow this to be called again */
2603 if (buf)
2604 *(buf - 1) = ',';
2605 }
2606}
2607
Ezequiel Garcia87819152012-09-12 11:47:57 -03002608static __init int event_trace_enable(void)
2609{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002610 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002611 struct trace_event_call **iter, *call;
Ezequiel Garcia87819152012-09-12 11:47:57 -03002612 int ret;
2613
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002614 if (!tr)
2615 return -ENODEV;
2616
Ezequiel Garcia87819152012-09-12 11:47:57 -03002617 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2618
2619 call = *iter;
2620 ret = event_init(call);
2621 if (!ret)
2622 list_add(&call->list, &ftrace_events);
2623 }
2624
Steven Rostedt77248222013-02-27 16:28:06 -05002625 /*
2626 * We need the top trace array to have a working set of trace
2627 * points at early init, before the debug files and directories
2628 * are created. Create the file entries now, and attach them
2629 * to the actual file dentries later.
2630 */
2631 __trace_early_add_events(tr);
2632
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002633 early_enable_events(tr, false);
Steven Rostedt81698832012-10-11 10:15:05 -04002634
2635 trace_printk_start_comm();
2636
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002637 register_event_cmds();
2638
Tom Zanussi85f2b082013-10-24 08:59:24 -05002639 register_trigger_cmds();
2640
Ezequiel Garcia87819152012-09-12 11:47:57 -03002641 return 0;
2642}
2643
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002644/*
2645 * event_trace_enable() is called from trace_event_init() first to
2646 * initialize events and perhaps start any events that are on the
2647 * command line. Unfortunately, there are some events that will not
2648 * start this early, like the system call tracepoints that need
2649 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2650 * is called before pid 1 starts, and this flag is never set, making
2651 * the syscall tracepoint never get reached, but the event is enabled
2652 * regardless (and not doing anything).
2653 */
2654static __init int event_trace_enable_again(void)
2655{
2656 struct trace_array *tr;
2657
2658 tr = top_trace_array();
2659 if (!tr)
2660 return -ENODEV;
2661
2662 early_enable_events(tr, true);
2663
2664 return 0;
2665}
2666
2667early_initcall(event_trace_enable_again);
2668
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002669static __init int event_trace_init(void)
2670{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002671 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002672 struct dentry *d_tracer;
2673 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04002674 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002675
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002676 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002677 if (!tr)
2678 return -ENODEV;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002679
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002680 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05002681 if (IS_ERR(d_tracer))
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002682 return 0;
2683
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002684 entry = tracefs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002685 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002686 if (!entry)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002687 pr_warn("Could not create tracefs 'available_events' entry\n");
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002688
Daniel Wagner9f616682015-08-10 14:35:46 +02002689 if (trace_define_generic_fields())
2690 pr_warn("tracing: Failed to allocated generic fields");
2691
Li Zefan8728fe52010-05-24 16:22:49 +08002692 if (trace_define_common_fields())
Fabian Frederick3448bac2014-06-07 13:43:08 +02002693 pr_warn("tracing: Failed to allocate common fields");
Li Zefan8728fe52010-05-24 16:22:49 +08002694
Steven Rostedt77248222013-02-27 16:28:06 -05002695 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002696 if (ret)
2697 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08002698
Oleg Nesterov836d4812013-07-31 19:31:37 +02002699#ifdef CONFIG_MODULES
Steven Rostedt6d723732009-04-10 14:53:50 -04002700 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08002701 if (ret)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002702 pr_warn("Failed to register trace events module notifier\n");
Oleg Nesterov836d4812013-07-31 19:31:37 +02002703#endif
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002704 return 0;
2705}
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05002706
2707void __init trace_event_init(void)
2708{
2709 event_trace_memsetup();
2710 init_ftrace_syscalls();
2711 event_trace_enable();
2712}
2713
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002714fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002715
2716#ifdef CONFIG_FTRACE_STARTUP_TEST
2717
2718static DEFINE_SPINLOCK(test_spinlock);
2719static DEFINE_SPINLOCK(test_spinlock_irq);
2720static DEFINE_MUTEX(test_mutex);
2721
2722static __init void test_work(struct work_struct *dummy)
2723{
2724 spin_lock(&test_spinlock);
2725 spin_lock_irq(&test_spinlock_irq);
2726 udelay(1);
2727 spin_unlock_irq(&test_spinlock_irq);
2728 spin_unlock(&test_spinlock);
2729
2730 mutex_lock(&test_mutex);
2731 msleep(1);
2732 mutex_unlock(&test_mutex);
2733}
2734
2735static __init int event_test_thread(void *unused)
2736{
2737 void *test_malloc;
2738
2739 test_malloc = kmalloc(1234, GFP_KERNEL);
2740 if (!test_malloc)
2741 pr_info("failed to kmalloc\n");
2742
2743 schedule_on_each_cpu(test_work);
2744
2745 kfree(test_malloc);
2746
2747 set_current_state(TASK_INTERRUPTIBLE);
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02002748 while (!kthread_should_stop()) {
Steven Rostedte6187002009-04-15 13:36:40 -04002749 schedule();
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02002750 set_current_state(TASK_INTERRUPTIBLE);
2751 }
2752 __set_current_state(TASK_RUNNING);
Steven Rostedte6187002009-04-15 13:36:40 -04002753
2754 return 0;
2755}
2756
2757/*
2758 * Do various things that may trigger events.
2759 */
2760static __init void event_test_stuff(void)
2761{
2762 struct task_struct *test_thread;
2763
2764 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2765 msleep(1);
2766 kthread_stop(test_thread);
2767}
2768
2769/*
2770 * For every trace event defined, we will test each trace point separately,
2771 * and then by groups, and finally all trace points.
2772 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002773static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04002774{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04002775 struct trace_subsystem_dir *dir;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002776 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002777 struct trace_event_call *call;
Steven Rostedte6187002009-04-15 13:36:40 -04002778 struct event_subsystem *system;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002779 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04002780 int ret;
2781
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002782 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002783 if (!tr)
2784 return;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002785
Steven Rostedte6187002009-04-15 13:36:40 -04002786 pr_info("Running tests on trace events:\n");
2787
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002788 list_for_each_entry(file, &tr->events, list) {
2789
2790 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04002791
Steven Rostedt22392912010-04-21 12:27:06 -04002792 /* Only test those that have a probe */
2793 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04002794 continue;
2795
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002796/*
2797 * Testing syscall events here is pretty useless, but
2798 * we still do it if configured. But this is time consuming.
2799 * What we really need is a user thread to perform the
2800 * syscalls as we test.
2801 */
2802#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04002803 if (call->class->system &&
2804 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002805 continue;
2806#endif
2807
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002808 pr_info("Testing event %s: ", trace_event_name(call));
Steven Rostedte6187002009-04-15 13:36:40 -04002809
2810 /*
2811 * If an event is already enabled, someone is using
2812 * it and the self test should not be on.
2813 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002814 if (file->flags & EVENT_FILE_FL_ENABLED) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002815 pr_warn("Enabled event during self test!\n");
Steven Rostedte6187002009-04-15 13:36:40 -04002816 WARN_ON_ONCE(1);
2817 continue;
2818 }
2819
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002820 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002821 event_test_stuff();
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002822 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002823
2824 pr_cont("OK\n");
2825 }
2826
2827 /* Now test at the sub system level */
2828
2829 pr_info("Running tests on trace event systems:\n");
2830
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002831 list_for_each_entry(dir, &tr->systems, list) {
2832
2833 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04002834
2835 /* the ftrace system is special, skip it */
2836 if (strcmp(system->name, "ftrace") == 0)
2837 continue;
2838
2839 pr_info("Testing event system %s: ", system->name);
2840
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002841 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002842 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002843 pr_warn("error enabling system %s\n",
2844 system->name);
Steven Rostedte6187002009-04-15 13:36:40 -04002845 continue;
2846 }
2847
2848 event_test_stuff();
2849
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002850 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002851 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002852 pr_warn("error disabling system %s\n",
2853 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002854 continue;
2855 }
Steven Rostedte6187002009-04-15 13:36:40 -04002856
2857 pr_cont("OK\n");
2858 }
2859
2860 /* Test with all events enabled */
2861
2862 pr_info("Running tests on all trace events:\n");
2863 pr_info("Testing all events: ");
2864
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002865 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002866 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002867 pr_warn("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002868 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002869 }
2870
2871 event_test_stuff();
2872
2873 /* reset sysname */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002874 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002875 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002876 pr_warn("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002877 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002878 }
2879
2880 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002881}
2882
2883#ifdef CONFIG_FUNCTION_TRACER
2884
Tejun Heo245b2e72009-06-24 15:13:48 +09002885static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002886
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002887static struct trace_array *event_tr;
2888
2889static void __init
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002890function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002891 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002892{
2893 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002894 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002895 struct ftrace_entry *entry;
2896 unsigned long flags;
2897 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002898 int cpu;
2899 int pc;
2900
2901 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002902 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002903 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09002904 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002905
2906 if (disabled != 1)
2907 goto out;
2908
2909 local_save_flags(flags);
2910
Steven Rostedte77405a2009-09-02 14:17:06 -04002911 event = trace_current_buffer_lock_reserve(&buffer,
2912 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002913 flags, pc);
2914 if (!event)
2915 goto out;
2916 entry = ring_buffer_event_data(event);
2917 entry->ip = ip;
2918 entry->parent_ip = parent_ip;
2919
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002920 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002921
2922 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09002923 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04002924 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002925}
2926
2927static struct ftrace_ops trace_ops __initdata =
2928{
2929 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04002930 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002931};
2932
2933static __init void event_trace_self_test_with_function(void)
2934{
Steven Rostedt17bb6152011-05-23 15:27:46 -04002935 int ret;
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002936 event_tr = top_trace_array();
2937 if (WARN_ON(!event_tr))
2938 return;
Steven Rostedt17bb6152011-05-23 15:27:46 -04002939 ret = register_ftrace_function(&trace_ops);
2940 if (WARN_ON(ret < 0)) {
2941 pr_info("Failed to enable function tracer for event tests\n");
2942 return;
2943 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002944 pr_info("Running tests again, along with the function tracer\n");
2945 event_trace_self_tests();
2946 unregister_ftrace_function(&trace_ops);
2947}
2948#else
2949static __init void event_trace_self_test_with_function(void)
2950{
2951}
2952#endif
2953
2954static __init int event_trace_self_tests_init(void)
2955{
Li Zefan020e5f82009-07-01 10:47:05 +08002956 if (!tracing_selftest_disabled) {
2957 event_trace_self_tests();
2958 event_trace_self_test_with_function();
2959 }
Steven Rostedte6187002009-04-15 13:36:40 -04002960
2961 return 0;
2962}
2963
Steven Rostedt28d20e22009-04-20 12:12:44 -04002964late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002965
2966#endif