blob: fb0261e90acc8534fcaf21fd566bbe2f3a447743 [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>
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -040018#include <linux/bsearch.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050019#include <linux/module.h>
20#include <linux/ctype.h>
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -040021#include <linux/sort.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Steven Rostedte6187002009-04-15 13:36:40 -040023#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050024
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -040025#include <trace/events/sched.h>
26
Li Zefan020e5f82009-07-01 10:47:05 +080027#include <asm/setup.h>
28
Steven Rostedt91729ef92009-03-02 15:03:01 -050029#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050030
Steven Rostedt4e5292e2009-09-12 19:26:21 -040031#undef TRACE_SYSTEM
Steven Rostedtb628b3e2009-02-27 23:32:58 -050032#define TRACE_SYSTEM "TRACE_SYSTEM"
33
Li Zefan20c89282009-05-06 10:33:45 +080034DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050035
Steven Rostedta59fd602009-04-10 13:52:20 -040036LIST_HEAD(ftrace_events);
Daniel Wagner9f616682015-08-10 14:35:46 +020037static LIST_HEAD(ftrace_generic_fields);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080038static LIST_HEAD(ftrace_common_fields);
Steven Rostedta59fd602009-04-10 13:52:20 -040039
Steven Rostedtd1a29142013-02-27 20:23:57 -050040#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41
42static struct kmem_cache *field_cachep;
43static struct kmem_cache *file_cachep;
44
Steven Rostedt6e94a782013-06-27 10:58:31 -040045static inline int system_refcount(struct event_subsystem *system)
46{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020047 return system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040048}
49
50static int system_refcount_inc(struct event_subsystem *system)
51{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020052 return system->ref_count++;
Steven Rostedt6e94a782013-06-27 10:58:31 -040053}
54
55static int system_refcount_dec(struct event_subsystem *system)
56{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020057 return --system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040058}
59
Steven Rostedtae63b31e2012-05-03 23:09:03 -040060/* Double loops, do not use break, only goto's work */
61#define do_for_each_event_file(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
63 list_for_each_entry(file, &tr->events, list)
64
65#define do_for_each_event_file_safe(tr, file) \
66 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040067 struct trace_event_file *___n; \
Steven Rostedtae63b31e2012-05-03 23:09:03 -040068 list_for_each_entry_safe(file, ___n, &tr->events, list)
69
70#define while_for_each_event_file() \
71 }
72
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080073static struct list_head *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040074trace_get_fields(struct trace_event_call *event_call)
Steven Rostedt2e33af02010-04-22 10:35:55 -040075{
76 if (!event_call->class->get_fields)
77 return &event_call->class->fields;
78 return event_call->class->get_fields(event_call);
79}
80
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080081static struct ftrace_event_field *
82__find_event_field(struct list_head *head, char *name)
83{
84 struct ftrace_event_field *field;
85
86 list_for_each_entry(field, head, link) {
87 if (!strcmp(field->name, name))
88 return field;
89 }
90
91 return NULL;
92}
93
94struct ftrace_event_field *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040095trace_find_event_field(struct trace_event_call *call, char *name)
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080096{
97 struct ftrace_event_field *field;
98 struct list_head *head;
99
Daniel Wagner9f616682015-08-10 14:35:46 +0200100 field = __find_event_field(&ftrace_generic_fields, name);
101 if (field)
102 return field;
103
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +0800104 field = __find_event_field(&ftrace_common_fields, name);
105 if (field)
106 return field;
107
108 head = trace_get_fields(call);
109 return __find_event_field(head, name);
110}
111
Li Zefan8728fe52010-05-24 16:22:49 +0800112static int __trace_define_field(struct list_head *head, const char *type,
113 const char *name, int offset, int size,
114 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -0500115{
116 struct ftrace_event_field *field;
117
Steven Rostedtd1a29142013-02-27 20:23:57 -0500118 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -0500119 if (!field)
Namhyung Kimaaf6ac02013-06-07 15:07:48 +0900120 return -ENOMEM;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100121
Steven Rostedt92edca02013-02-27 20:41:37 -0500122 field->name = name;
123 field->type = type;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100124
Li Zefan43b51ea2009-08-07 10:33:22 +0800125 if (filter_type == FILTER_OTHER)
126 field->filter_type = filter_assign_type(type);
127 else
128 field->filter_type = filter_type;
129
Tom Zanussicf027f62009-03-22 03:30:39 -0500130 field->offset = offset;
131 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -0500132 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +0800133
Steven Rostedt2e33af02010-04-22 10:35:55 -0400134 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -0500135
136 return 0;
Tom Zanussicf027f62009-03-22 03:30:39 -0500137}
Li Zefan8728fe52010-05-24 16:22:49 +0800138
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400139int trace_define_field(struct trace_event_call *call, const char *type,
Li Zefan8728fe52010-05-24 16:22:49 +0800140 const char *name, int offset, int size, int is_signed,
141 int filter_type)
142{
143 struct list_head *head;
144
145 if (WARN_ON(!call->class))
146 return 0;
147
148 head = trace_get_fields(call);
149 return __trace_define_field(head, type, name, offset, size,
150 is_signed, filter_type);
151}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400152EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500153
Daniel Wagner9f616682015-08-10 14:35:46 +0200154#define __generic_field(type, item, filter_type) \
155 ret = __trace_define_field(&ftrace_generic_fields, #type, \
156 #item, 0, 0, is_signed_type(type), \
157 filter_type); \
158 if (ret) \
159 return ret;
160
Li Zefane647d6b2009-08-19 15:54:32 +0800161#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800162 ret = __trace_define_field(&ftrace_common_fields, #type, \
163 "common_" #item, \
164 offsetof(typeof(ent), item), \
165 sizeof(ent.item), \
166 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800167 if (ret) \
168 return ret;
169
Daniel Wagner9f616682015-08-10 14:35:46 +0200170static int trace_define_generic_fields(void)
171{
172 int ret;
173
174 __generic_field(int, cpu, FILTER_OTHER);
175 __generic_field(char *, comm, FILTER_PTR_STRING);
176
177 return ret;
178}
179
Li Zefan8728fe52010-05-24 16:22:49 +0800180static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800181{
182 int ret;
183 struct trace_entry ent;
184
185 __common_field(unsigned short, type);
186 __common_field(unsigned char, flags);
187 __common_field(unsigned char, preempt_count);
188 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800189
190 return ret;
191}
192
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400193static void trace_destroy_fields(struct trace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800194{
195 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400196 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800197
Steven Rostedt2e33af02010-04-22 10:35:55 -0400198 head = trace_get_fields(call);
199 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800200 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500201 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800202 }
203}
204
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400205int trace_event_raw_init(struct trace_event_call *call)
Li Zefan87d9b4e2009-12-08 11:14:20 +0800206{
207 int id;
208
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400209 id = register_trace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800210 if (!id)
211 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800212
213 return 0;
214}
215EXPORT_SYMBOL_GPL(trace_event_raw_init);
216
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400217bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
218{
219 struct trace_array *tr = trace_file->tr;
220 struct trace_array_cpu *data;
221 struct trace_pid_list *pid_list;
222
223 pid_list = rcu_dereference_sched(tr->filtered_pids);
224 if (!pid_list)
225 return false;
226
227 data = this_cpu_ptr(tr->trace_buffer.data);
228
229 return data->ignore_pid;
230}
231EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
232
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400233void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
234 struct trace_event_file *trace_file,
235 unsigned long len)
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400236{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400237 struct trace_event_call *event_call = trace_file->event_call;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400238
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400239 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
240 trace_event_ignore_this_pid(trace_file))
241 return NULL;
242
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400243 local_save_flags(fbuffer->flags);
244 fbuffer->pc = preempt_count();
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400245 fbuffer->trace_file = trace_file;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400246
247 fbuffer->event =
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400248 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400249 event_call->event.type, len,
250 fbuffer->flags, fbuffer->pc);
251 if (!fbuffer->event)
252 return NULL;
253
254 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
255 return fbuffer->entry;
256}
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400257EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400258
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500259static DEFINE_SPINLOCK(tracepoint_iter_lock);
260
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400261static void output_printk(struct trace_event_buffer *fbuffer)
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500262{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400263 struct trace_event_call *event_call;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500264 struct trace_event *event;
265 unsigned long flags;
266 struct trace_iterator *iter = tracepoint_print_iter;
267
268 if (!iter)
269 return;
270
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400271 event_call = fbuffer->trace_file->event_call;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500272 if (!event_call || !event_call->event.funcs ||
273 !event_call->event.funcs->trace)
274 return;
275
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400276 event = &fbuffer->trace_file->event_call->event;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500277
278 spin_lock_irqsave(&tracepoint_iter_lock, flags);
279 trace_seq_init(&iter->seq);
280 iter->ent = fbuffer->entry;
281 event_call->event.funcs->trace(iter, 0, event);
282 trace_seq_putc(&iter->seq, 0);
283 printk("%s", iter->seq.buffer);
284
285 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
286}
287
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400288void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400289{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500290 if (tracepoint_printk)
291 output_printk(fbuffer);
292
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400293 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400294 fbuffer->event, fbuffer->entry,
295 fbuffer->flags, fbuffer->pc);
296}
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400297EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400298
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400299int trace_event_reg(struct trace_event_call *call,
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400300 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400301{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400302 struct trace_event_file *file = data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400303
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400304 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400305 switch (type) {
306 case TRACE_REG_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400307 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400308 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400309 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400310 case TRACE_REG_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400311 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400312 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400313 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400314 return 0;
315
316#ifdef CONFIG_PERF_EVENTS
317 case TRACE_REG_PERF_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400318 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400319 call->class->perf_probe,
320 call);
321 case TRACE_REG_PERF_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400322 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400323 call->class->perf_probe,
324 call);
325 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100326 case TRACE_REG_PERF_OPEN:
327 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100328 case TRACE_REG_PERF_ADD:
329 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100330 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400331#endif
332 }
333 return 0;
334}
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400335EXPORT_SYMBOL_GPL(trace_event_reg);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400336
Li Zefane870e9a2010-07-02 11:07:32 +0800337void trace_event_enable_cmd_record(bool enable)
338{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400339 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400340 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800341
342 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400343 do_for_each_event_file(tr, file) {
344
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400345 if (!(file->flags & EVENT_FILE_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800346 continue;
347
348 if (enable) {
349 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400350 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800351 } else {
352 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400353 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800354 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400355 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800356 mutex_unlock(&event_mutex);
357}
358
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400359static int __ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400360 int enable, int soft_disable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500361{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400362 struct trace_event_call *call = file->event_call;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400363 struct trace_array *tr = file->tr;
Li Zefan3b8e4272009-12-08 11:14:52 +0800364 int ret = 0;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400365 int disable;
Li Zefan3b8e4272009-12-08 11:14:52 +0800366
Steven Rostedtfd994982009-02-28 02:41:25 -0500367 switch (enable) {
368 case 0:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400369 /*
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900370 * When soft_disable is set and enable is cleared, the sm_ref
371 * reference counter is decremented. If it reaches 0, we want
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400372 * to clear the SOFT_DISABLED flag but leave the event in the
373 * state that it was. That is, if the event was enabled and
374 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
375 * is set we do not want the event to be enabled before we
376 * clear the bit.
377 *
378 * When soft_disable is not set but the SOFT_MODE flag is,
379 * we do nothing. Do not disable the tracepoint, otherwise
380 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
381 */
382 if (soft_disable) {
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900383 if (atomic_dec_return(&file->sm_ref) > 0)
384 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400385 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
386 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400387 } else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400388 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400389
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400390 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
391 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
392 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800393 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400394 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800395 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400396 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500397 }
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500398 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400399 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
400 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500401 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400402 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500403 break;
404 case 1:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400405 /*
406 * When soft_disable is set and enable is set, we want to
407 * register the tracepoint for the event, but leave the event
408 * as is. That means, if the event was already enabled, we do
409 * nothing (but set SOFT_MODE). If the event is disabled, we
410 * set SOFT_DISABLED before enabling the event tracepoint, so
411 * it still seems to be disabled.
412 */
413 if (!soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400414 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900415 else {
416 if (atomic_inc_return(&file->sm_ref) > 1)
417 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400418 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900419 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400420
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400421 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400422
423 /* Keep the event disabled, when going to SOFT_MODE. */
424 if (soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400425 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400426
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400427 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800428 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400429 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800430 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400431 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800432 if (ret) {
433 tracing_stop_cmdline_record();
434 pr_info("event trace: Could not enable event "
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400435 "%s\n", trace_event_name(call));
Li Zefan3b8e4272009-12-08 11:14:52 +0800436 break;
437 }
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400438 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -0500439
440 /* WAS_ENABLED gets set but never cleared. */
441 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
Steven Rostedtfd994982009-02-28 02:41:25 -0500442 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500443 break;
444 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800445
446 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500447}
448
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400449int trace_event_enable_disable(struct trace_event_file *file,
Tom Zanussi85f2b082013-10-24 08:59:24 -0500450 int enable, int soft_disable)
451{
452 return __ftrace_event_enable_disable(file, enable, soft_disable);
453}
454
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400455static int ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400456 int enable)
457{
458 return __ftrace_event_enable_disable(file, enable, 0);
459}
460
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400461static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800462{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400463 struct trace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800464
465 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400466 list_for_each_entry(file, &tr->events, list) {
467 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800468 }
469 mutex_unlock(&event_mutex);
470}
471
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400472static int cmp_pid(const void *key, const void *elt)
473{
474 const pid_t *search_pid = key;
475 const pid_t *pid = elt;
476
477 if (*search_pid == *pid)
478 return 0;
479 if (*search_pid < *pid)
480 return -1;
481 return 1;
482}
483
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400484static bool
485check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task)
486{
487 pid_t search_pid;
488 pid_t *pid;
489
490 /*
491 * Return false, because if filtered_pids does not exist,
492 * all pids are good to trace.
493 */
494 if (!filtered_pids)
495 return false;
496
497 search_pid = task->pid;
498
499 pid = bsearch(&search_pid, filtered_pids->pids,
500 filtered_pids->nr_pids, sizeof(pid_t),
501 cmp_pid);
502 if (!pid)
503 return true;
504
505 return false;
506}
507
508static void
509event_filter_pid_sched_switch_probe_pre(void *data,
510 struct task_struct *prev, struct task_struct *next)
511{
512 struct trace_array *tr = data;
513 struct trace_pid_list *pid_list;
514
515 pid_list = rcu_dereference_sched(tr->filtered_pids);
516
517 this_cpu_write(tr->trace_buffer.data->ignore_pid,
518 check_ignore_pid(pid_list, prev) &&
519 check_ignore_pid(pid_list, next));
520}
521
522static void
523event_filter_pid_sched_switch_probe_post(void *data,
524 struct task_struct *prev, struct task_struct *next)
525{
526 struct trace_array *tr = data;
527 struct trace_pid_list *pid_list;
528
529 pid_list = rcu_dereference_sched(tr->filtered_pids);
530
531 this_cpu_write(tr->trace_buffer.data->ignore_pid,
532 check_ignore_pid(pid_list, next));
533}
534
535static void
536event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
537{
538 struct trace_array *tr = data;
539 struct trace_pid_list *pid_list;
540
541 /* Nothing to do if we are already tracing */
542 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
543 return;
544
545 pid_list = rcu_dereference_sched(tr->filtered_pids);
546
547 this_cpu_write(tr->trace_buffer.data->ignore_pid,
548 check_ignore_pid(pid_list, task));
549}
550
551static void
552event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
553{
554 struct trace_array *tr = data;
555 struct trace_pid_list *pid_list;
556
557 /* Nothing to do if we are not tracing */
558 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
559 return;
560
561 pid_list = rcu_dereference_sched(tr->filtered_pids);
562
563 /* Set tracing if current is enabled */
564 this_cpu_write(tr->trace_buffer.data->ignore_pid,
565 check_ignore_pid(pid_list, current));
566}
567
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400568static void __ftrace_clear_event_pids(struct trace_array *tr)
569{
570 struct trace_pid_list *pid_list;
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400571 struct trace_event_file *file;
572 int cpu;
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400573
574 pid_list = rcu_dereference_protected(tr->filtered_pids,
575 lockdep_is_held(&event_mutex));
576 if (!pid_list)
577 return;
578
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400579 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
580 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
581
582 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
583 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
584
585 list_for_each_entry(file, &tr->events, list) {
586 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
587 }
588
589 for_each_possible_cpu(cpu)
590 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
591
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400592 rcu_assign_pointer(tr->filtered_pids, NULL);
593
594 /* Wait till all users are no longer using pid filtering */
595 synchronize_sched();
596
597 free_pages((unsigned long)pid_list->pids, pid_list->order);
598 kfree(pid_list);
599}
600
601static void ftrace_clear_event_pids(struct trace_array *tr)
602{
603 mutex_lock(&event_mutex);
604 __ftrace_clear_event_pids(tr);
605 mutex_unlock(&event_mutex);
606}
607
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400608static void __put_system(struct event_subsystem *system)
609{
610 struct event_filter *filter = system->filter;
611
Steven Rostedt6e94a782013-06-27 10:58:31 -0400612 WARN_ON_ONCE(system_refcount(system) == 0);
613 if (system_refcount_dec(system))
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400614 return;
615
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400616 list_del(&system->list);
617
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400618 if (filter) {
619 kfree(filter->filter_string);
620 kfree(filter);
621 }
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +0200622 kfree_const(system->name);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400623 kfree(system);
624}
625
626static void __get_system(struct event_subsystem *system)
627{
Steven Rostedt6e94a782013-06-27 10:58:31 -0400628 WARN_ON_ONCE(system_refcount(system) == 0);
629 system_refcount_inc(system);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400630}
631
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400632static void __get_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400633{
634 WARN_ON_ONCE(dir->ref_count == 0);
635 dir->ref_count++;
636 __get_system(dir->subsystem);
637}
638
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400639static void __put_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400640{
641 WARN_ON_ONCE(dir->ref_count == 0);
642 /* If the subsystem is about to be freed, the dir must be too */
Steven Rostedt6e94a782013-06-27 10:58:31 -0400643 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400644
645 __put_system(dir->subsystem);
646 if (!--dir->ref_count)
647 kfree(dir);
648}
649
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400650static void put_system(struct trace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400651{
652 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400653 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400654 mutex_unlock(&event_mutex);
655}
656
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400657static void remove_subsystem(struct trace_subsystem_dir *dir)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200658{
659 if (!dir)
660 return;
661
662 if (!--dir->nr_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500663 tracefs_remove_recursive(dir->entry);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200664 list_del(&dir->list);
665 __put_system_dir(dir);
666 }
667}
668
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400669static void remove_event_file_dir(struct trace_event_file *file)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200670{
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200671 struct dentry *dir = file->dir;
672 struct dentry *child;
673
674 if (dir) {
675 spin_lock(&dir->d_lock); /* probably unneeded */
Al Viro946e51f2014-10-26 19:19:16 -0400676 list_for_each_entry(child, &dir->d_subdirs, d_child) {
David Howells7682c912015-03-17 22:26:16 +0000677 if (d_really_is_positive(child)) /* probably unneeded */
678 d_inode(child)->i_private = NULL;
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200679 }
680 spin_unlock(&dir->d_lock);
681
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500682 tracefs_remove_recursive(dir);
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200683 }
684
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200685 list_del(&file->list);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200686 remove_subsystem(file->system);
Oleg Nesterov2448e342014-07-11 21:06:38 +0200687 free_event_filter(file->filter);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200688 kmem_cache_free(file_cachep, file);
689}
690
Li Zefan8f31bfe2009-05-08 10:31:42 +0800691/*
692 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
693 */
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400694static int
695__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
696 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500697{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400698 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400699 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400700 const char *name;
Steven Rostedt29f93942009-05-08 16:06:47 -0400701 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500702
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400703 list_for_each_entry(file, &tr->events, list) {
704
705 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400706 name = trace_event_name(call);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500707
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400708 if (!name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500709 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500710
Steven Rostedt9b637762012-05-10 15:55:43 -0400711 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
712 continue;
713
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500714 if (match &&
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400715 strcmp(match, name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400716 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500717 continue;
718
Steven Rostedt8f082012010-04-20 10:47:33 -0400719 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500720 continue;
721
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400722 if (event && strcmp(event, name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500723 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500724
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400725 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500726
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500727 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500728 }
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400729
730 return ret;
731}
732
733static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
734 const char *sub, const char *event, int set)
735{
736 int ret;
737
738 mutex_lock(&event_mutex);
739 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500740 mutex_unlock(&event_mutex);
741
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500742 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500743}
744
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400745static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800746{
747 char *event = NULL, *sub = NULL, *match;
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900748 int ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800749
750 /*
751 * The buf format can be <subsystem>:<event-name>
752 * *:<event-name> means any event by that name.
753 * :<event-name> is the same.
754 *
755 * <subsystem>:* means all events in that subsystem
756 * <subsystem>: means the same.
757 *
758 * <name> (no ':') means all events in a subsystem with
759 * the name <name> or any event that matches <name>
760 */
761
762 match = strsep(&buf, ":");
763 if (buf) {
764 sub = match;
765 event = buf;
766 match = NULL;
767
768 if (!strlen(sub) || strcmp(sub, "*") == 0)
769 sub = NULL;
770 if (!strlen(event) || strcmp(event, "*") == 0)
771 event = NULL;
772 }
773
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900774 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
775
776 /* Put back the colon to allow this to be called again */
777 if (buf)
778 *(buf - 1) = ':';
779
780 return ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800781}
782
Steven Rostedt4671c792009-05-08 16:27:41 -0400783/**
784 * trace_set_clr_event - enable or disable an event
785 * @system: system name to match (NULL for any system)
786 * @event: event name to match (NULL for all events, within system)
787 * @set: 1 to enable, 0 to disable
788 *
789 * This is a way for other parts of the kernel to enable or disable
790 * event recording.
791 *
792 * Returns 0 on success, -EINVAL if the parameters do not match any
793 * registered events.
794 */
795int trace_set_clr_event(const char *system, const char *event, int set)
796{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400797 struct trace_array *tr = top_trace_array();
798
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +0900799 if (!tr)
800 return -ENODEV;
801
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400802 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400803}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800804EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400805
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500806/* 128 should be much more than enough */
807#define EVENT_BUF_SIZE 127
808
809static ssize_t
810ftrace_event_write(struct file *file, const char __user *ubuf,
811 size_t cnt, loff_t *ppos)
812{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200813 struct trace_parser parser;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400814 struct seq_file *m = file->private_data;
815 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800816 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500817
Li Zefan4ba79782009-09-22 13:52:20 +0800818 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500819 return 0;
820
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400821 ret = tracing_update_buffers();
822 if (ret < 0)
823 return ret;
824
jolsa@redhat.com48966362009-09-11 17:29:28 +0200825 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500826 return -ENOMEM;
827
jolsa@redhat.com48966362009-09-11 17:29:28 +0200828 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500829
Li Zefan4ba79782009-09-22 13:52:20 +0800830 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200831 int set = 1;
832
833 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500834 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500835
jolsa@redhat.com48966362009-09-11 17:29:28 +0200836 parser.buffer[parser.idx] = 0;
837
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400838 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500839 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200840 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500841 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500842
843 ret = read;
844
jolsa@redhat.com48966362009-09-11 17:29:28 +0200845 out_put:
846 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500847
848 return ret;
849}
850
851static void *
852t_next(struct seq_file *m, void *v, loff_t *pos)
853{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400854 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400855 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400856 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500857
858 (*pos)++;
859
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400860 list_for_each_entry_continue(file, &tr->events, list) {
861 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400862 /*
863 * The ftrace subsystem is for showing formats only.
864 * They can not be enabled or disabled via the event files.
865 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400866 if (call->class && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400867 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400868 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500869
Li Zefan30bd39c2009-09-18 14:07:05 +0800870 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500871}
872
873static void *t_start(struct seq_file *m, loff_t *pos)
874{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400875 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400876 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800877 loff_t l;
878
Li Zefan20c89282009-05-06 10:33:45 +0800879 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800880
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400881 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800882 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400883 file = t_next(m, file, &l);
884 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800885 break;
886 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400887 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500888}
889
890static void *
891s_next(struct seq_file *m, void *v, loff_t *pos)
892{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400893 struct trace_event_file *file = v;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400894 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500895
896 (*pos)++;
897
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400898 list_for_each_entry_continue(file, &tr->events, list) {
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400899 if (file->flags & EVENT_FILE_FL_ENABLED)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400900 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500901 }
902
Li Zefan30bd39c2009-09-18 14:07:05 +0800903 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500904}
905
906static void *s_start(struct seq_file *m, loff_t *pos)
907{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400908 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400909 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800910 loff_t l;
911
Li Zefan20c89282009-05-06 10:33:45 +0800912 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800913
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400914 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800915 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400916 file = s_next(m, file, &l);
917 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800918 break;
919 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400920 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500921}
922
923static int t_show(struct seq_file *m, void *v)
924{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400925 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400926 struct trace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500927
Steven Rostedt8f082012010-04-20 10:47:33 -0400928 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
929 seq_printf(m, "%s:", call->class->system);
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400930 seq_printf(m, "%s\n", trace_event_name(call));
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500931
932 return 0;
933}
934
935static void t_stop(struct seq_file *m, void *p)
936{
Li Zefan20c89282009-05-06 10:33:45 +0800937 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500938}
939
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400940static void *p_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)fb662282015-10-26 03:45:22 -0400941 __acquires(RCU)
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400942{
943 struct trace_pid_list *pid_list;
944 struct trace_array *tr = m->private;
945
946 /*
947 * Grab the mutex, to keep calls to p_next() having the same
948 * tr->filtered_pids as p_start() has.
949 * If we just passed the tr->filtered_pids around, then RCU would
950 * have been enough, but doing that makes things more complex.
951 */
952 mutex_lock(&event_mutex);
953 rcu_read_lock_sched();
954
955 pid_list = rcu_dereference_sched(tr->filtered_pids);
956
957 if (!pid_list || *pos >= pid_list->nr_pids)
958 return NULL;
959
960 return (void *)&pid_list->pids[*pos];
961}
962
963static void p_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)fb662282015-10-26 03:45:22 -0400964 __releases(RCU)
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400965{
966 rcu_read_unlock_sched();
967 mutex_unlock(&event_mutex);
968}
969
970static void *
971p_next(struct seq_file *m, void *v, loff_t *pos)
972{
973 struct trace_array *tr = m->private;
974 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
975
976 (*pos)++;
977
978 if (*pos >= pid_list->nr_pids)
979 return NULL;
980
981 return (void *)&pid_list->pids[*pos];
982}
983
984static int p_show(struct seq_file *m, void *v)
985{
986 pid_t *pid = v;
987
988 seq_printf(m, "%d\n", *pid);
989 return 0;
990}
991
Steven Rostedt1473e442009-02-24 14:15:08 -0500992static ssize_t
993event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
994 loff_t *ppos)
995{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400996 struct trace_event_file *file;
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200997 unsigned long flags;
Tom Zanussia4390592013-06-29 00:08:04 -0500998 char buf[4] = "0";
Steven Rostedt1473e442009-02-24 14:15:08 -0500999
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +02001000 mutex_lock(&event_mutex);
1001 file = event_file_data(filp);
1002 if (likely(file))
1003 flags = file->flags;
1004 mutex_unlock(&event_mutex);
1005
1006 if (!file)
1007 return -ENODEV;
1008
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001009 if (flags & EVENT_FILE_FL_ENABLED &&
1010 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussia4390592013-06-29 00:08:04 -05001011 strcpy(buf, "1");
1012
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001013 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1014 flags & EVENT_FILE_FL_SOFT_MODE)
Tom Zanussia4390592013-06-29 00:08:04 -05001015 strcat(buf, "*");
1016
1017 strcat(buf, "\n");
Steven Rostedt1473e442009-02-24 14:15:08 -05001018
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -04001019 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -05001020}
1021
1022static ssize_t
1023event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1024 loff_t *ppos)
1025{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001026 struct trace_event_file *file;
Steven Rostedt1473e442009-02-24 14:15:08 -05001027 unsigned long val;
1028 int ret;
1029
Peter Huewe22fe9b52011-06-07 21:58:27 +02001030 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1031 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -05001032 return ret;
1033
Steven Rostedt1852fcc2009-03-11 14:33:00 -04001034 ret = tracing_update_buffers();
1035 if (ret < 0)
1036 return ret;
1037
Steven Rostedt1473e442009-02-24 14:15:08 -05001038 switch (val) {
1039 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -05001040 case 1:
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +02001041 ret = -ENODEV;
Steven Rostedt11a241a2009-03-02 11:49:04 -05001042 mutex_lock(&event_mutex);
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +02001043 file = event_file_data(filp);
1044 if (likely(file))
1045 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -05001046 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -05001047 break;
1048
1049 default:
1050 return -EINVAL;
1051 }
1052
1053 *ppos += cnt;
1054
Li Zefan3b8e4272009-12-08 11:14:52 +08001055 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -05001056}
1057
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001058static ssize_t
1059system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1060 loff_t *ppos)
1061{
Li Zefanc142b152009-05-08 10:32:05 +08001062 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001063 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001064 struct event_subsystem *system = dir->subsystem;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001065 struct trace_event_call *call;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001066 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001067 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001068 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +08001069 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001070 int ret;
1071
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001072 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001073 list_for_each_entry(file, &tr->events, list) {
1074 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001075 if (!trace_event_name(call) || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001076 continue;
1077
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001078 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001079 continue;
1080
1081 /*
1082 * We need to find out if all the events are set
1083 * or if all events or cleared, or if we have
1084 * a mixture.
1085 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001086 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +08001087
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001088 /*
1089 * If we have a mixture, no need to look further.
1090 */
Li Zefanc142b152009-05-08 10:32:05 +08001091 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001092 break;
1093 }
1094 mutex_unlock(&event_mutex);
1095
Li Zefanc142b152009-05-08 10:32:05 +08001096 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001097 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001098
1099 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1100
1101 return ret;
1102}
1103
1104static ssize_t
1105system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1106 loff_t *ppos)
1107{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001108 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001109 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001110 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001111 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001112 ssize_t ret;
1113
Peter Huewe22fe9b52011-06-07 21:58:27 +02001114 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1115 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001116 return ret;
1117
1118 ret = tracing_update_buffers();
1119 if (ret < 0)
1120 return ret;
1121
Li Zefan8f31bfe2009-05-08 10:31:42 +08001122 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001123 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001124
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001125 /*
1126 * Opening of "enable" adds a ref count to system,
1127 * so the name is safe to use.
1128 */
1129 if (system)
1130 name = system->name;
1131
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001132 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001133 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +08001134 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001135
1136 ret = cnt;
1137
Li Zefan8f31bfe2009-05-08 10:31:42 +08001138out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001139 *ppos += cnt;
1140
1141 return ret;
1142}
1143
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001144enum {
1145 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +08001146 FORMAT_FIELD_SEPERATOR = 2,
1147 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001148};
1149
1150static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -05001151{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001152 struct trace_event_call *call = event_file_data(m->private);
Li Zefan86397dc2010-08-17 13:53:06 +08001153 struct list_head *common_head = &ftrace_common_fields;
1154 struct list_head *head = trace_get_fields(call);
Oleg Nesterov7710b632013-07-18 20:47:10 +02001155 struct list_head *node = v;
Steven Rostedt981d0812009-03-02 13:53:59 -05001156
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001157 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001158
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001159 switch ((unsigned long)v) {
1160 case FORMAT_HEADER:
Oleg Nesterov7710b632013-07-18 20:47:10 +02001161 node = common_head;
1162 break;
Li Zefan86397dc2010-08-17 13:53:06 +08001163
1164 case FORMAT_FIELD_SEPERATOR:
Oleg Nesterov7710b632013-07-18 20:47:10 +02001165 node = head;
1166 break;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001167
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001168 case FORMAT_PRINTFMT:
1169 /* all done */
1170 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001171 }
1172
Oleg Nesterov7710b632013-07-18 20:47:10 +02001173 node = node->prev;
1174 if (node == common_head)
Li Zefan86397dc2010-08-17 13:53:06 +08001175 return (void *)FORMAT_FIELD_SEPERATOR;
Oleg Nesterov7710b632013-07-18 20:47:10 +02001176 else if (node == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001177 return (void *)FORMAT_PRINTFMT;
Oleg Nesterov7710b632013-07-18 20:47:10 +02001178 else
1179 return node;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001180}
1181
1182static int f_show(struct seq_file *m, void *v)
1183{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001184 struct trace_event_call *call = event_file_data(m->private);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001185 struct ftrace_event_field *field;
1186 const char *array_descriptor;
1187
1188 switch ((unsigned long)v) {
1189 case FORMAT_HEADER:
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001190 seq_printf(m, "name: %s\n", trace_event_name(call));
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001191 seq_printf(m, "ID: %d\n", call->event.type);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001192 seq_puts(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +08001193 return 0;
1194
Li Zefan86397dc2010-08-17 13:53:06 +08001195 case FORMAT_FIELD_SEPERATOR:
1196 seq_putc(m, '\n');
1197 return 0;
1198
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001199 case FORMAT_PRINTFMT:
1200 seq_printf(m, "\nprint fmt: %s\n",
1201 call->print_fmt);
1202 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001203 }
1204
Oleg Nesterov7710b632013-07-18 20:47:10 +02001205 field = list_entry(v, struct ftrace_event_field, link);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001206 /*
1207 * Smartly shows the array type(except dynamic array).
1208 * Normal:
1209 * field:TYPE VAR
1210 * If TYPE := TYPE[LEN], it is shown:
1211 * field:TYPE VAR[LEN]
1212 */
1213 array_descriptor = strchr(field->type, '[');
1214
1215 if (!strncmp(field->type, "__data_loc", 10))
1216 array_descriptor = NULL;
1217
1218 if (!array_descriptor)
1219 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1220 field->type, field->name, field->offset,
1221 field->size, !!field->is_signed);
1222 else
1223 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1224 (int)(array_descriptor - field->type),
1225 field->type, field->name,
1226 array_descriptor, field->offset,
1227 field->size, !!field->is_signed);
1228
1229 return 0;
1230}
1231
Oleg Nesterov7710b632013-07-18 20:47:10 +02001232static void *f_start(struct seq_file *m, loff_t *pos)
1233{
1234 void *p = (void *)FORMAT_HEADER;
1235 loff_t l = 0;
1236
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001237 /* ->stop() is called even if ->start() fails */
1238 mutex_lock(&event_mutex);
1239 if (!event_file_data(m->private))
1240 return ERR_PTR(-ENODEV);
1241
Oleg Nesterov7710b632013-07-18 20:47:10 +02001242 while (l < *pos && p)
1243 p = f_next(m, p, &l);
1244
1245 return p;
1246}
1247
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001248static void f_stop(struct seq_file *m, void *p)
1249{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001250 mutex_unlock(&event_mutex);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001251}
1252
1253static const struct seq_operations trace_format_seq_ops = {
1254 .start = f_start,
1255 .next = f_next,
1256 .stop = f_stop,
1257 .show = f_show,
1258};
1259
1260static int trace_format_open(struct inode *inode, struct file *file)
1261{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001262 struct seq_file *m;
1263 int ret;
1264
1265 ret = seq_open(file, &trace_format_seq_ops);
1266 if (ret < 0)
1267 return ret;
1268
1269 m = file->private_data;
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001270 m->private = file;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001271
1272 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001273}
1274
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001275static ssize_t
1276event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1277{
Oleg Nesterov1a111262013-07-26 19:25:32 +02001278 int id = (long)event_file_data(filp);
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001279 char buf[32];
1280 int len;
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001281
1282 if (*ppos)
1283 return 0;
1284
Oleg Nesterov1a111262013-07-26 19:25:32 +02001285 if (unlikely(!id))
1286 return -ENODEV;
1287
1288 len = sprintf(buf, "%d\n", id);
1289
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001290 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001291}
1292
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001293static ssize_t
1294event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1295 loff_t *ppos)
1296{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001297 struct trace_event_file *file;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001298 struct trace_seq *s;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001299 int r = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001300
1301 if (*ppos)
1302 return 0;
1303
1304 s = kmalloc(sizeof(*s), GFP_KERNEL);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001305
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001306 if (!s)
1307 return -ENOMEM;
1308
1309 trace_seq_init(s);
1310
Oleg Nesterove2912b02013-07-26 19:25:40 +02001311 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001312 file = event_file_data(filp);
1313 if (file)
1314 print_event_filter(file, s);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001315 mutex_unlock(&event_mutex);
1316
Tom Zanussif306cc82013-10-24 08:34:17 -05001317 if (file)
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001318 r = simple_read_from_buffer(ubuf, cnt, ppos,
1319 s->buffer, trace_seq_used(s));
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001320
1321 kfree(s);
1322
1323 return r;
1324}
1325
1326static ssize_t
1327event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1328 loff_t *ppos)
1329{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001330 struct trace_event_file *file;
Tom Zanussi8b372562009-04-28 03:04:59 -05001331 char *buf;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001332 int err = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001333
Tom Zanussi8b372562009-04-28 03:04:59 -05001334 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001335 return -EINVAL;
1336
Tom Zanussi8b372562009-04-28 03:04:59 -05001337 buf = (char *)__get_free_page(GFP_TEMPORARY);
1338 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001339 return -ENOMEM;
1340
Tom Zanussi8b372562009-04-28 03:04:59 -05001341 if (copy_from_user(buf, ubuf, cnt)) {
1342 free_page((unsigned long) buf);
1343 return -EFAULT;
1344 }
1345 buf[cnt] = '\0';
1346
Oleg Nesterove2912b02013-07-26 19:25:40 +02001347 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001348 file = event_file_data(filp);
1349 if (file)
1350 err = apply_event_filter(file, buf);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001351 mutex_unlock(&event_mutex);
1352
Tom Zanussi8b372562009-04-28 03:04:59 -05001353 free_page((unsigned long) buf);
1354 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001355 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -05001356
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001357 *ppos += cnt;
1358
1359 return cnt;
1360}
1361
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001362static LIST_HEAD(event_subsystems);
1363
1364static int subsystem_open(struct inode *inode, struct file *filp)
1365{
1366 struct event_subsystem *system = NULL;
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001367 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001368 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001369 int ret;
1370
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001371 if (tracing_is_disabled())
1372 return -ENODEV;
1373
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001374 /* Make sure the system still exists */
Alexander Z Lama8227412013-07-01 19:37:54 -07001375 mutex_lock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001376 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001377 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1378 list_for_each_entry(dir, &tr->systems, list) {
1379 if (dir == inode->i_private) {
1380 /* Don't open systems with no events */
1381 if (dir->nr_events) {
1382 __get_system_dir(dir);
1383 system = dir->subsystem;
1384 }
1385 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001386 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001387 }
1388 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001389 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001390 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001391 mutex_unlock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001392
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001393 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001394 return -ENODEV;
1395
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001396 /* Some versions of gcc think dir can be uninitialized here */
1397 WARN_ON(!dir);
1398
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001399 /* Still need to increment the ref count of the system */
1400 if (trace_array_get(tr) < 0) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001401 put_system(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001402 return -ENODEV;
1403 }
1404
1405 ret = tracing_open_generic(inode, filp);
1406 if (ret < 0) {
1407 trace_array_put(tr);
1408 put_system(dir);
1409 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001410
1411 return ret;
1412}
1413
1414static int system_tr_open(struct inode *inode, struct file *filp)
1415{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001416 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001417 struct trace_array *tr = inode->i_private;
1418 int ret;
1419
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001420 if (tracing_is_disabled())
1421 return -ENODEV;
1422
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001423 if (trace_array_get(tr) < 0)
1424 return -ENODEV;
1425
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001426 /* Make a temporary dir that has no system but points to tr */
1427 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001428 if (!dir) {
1429 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001430 return -ENOMEM;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001431 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001432
1433 dir->tr = tr;
1434
1435 ret = tracing_open_generic(inode, filp);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001436 if (ret < 0) {
1437 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001438 kfree(dir);
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001439 return ret;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001440 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001441
1442 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001443
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001444 return 0;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001445}
1446
1447static int subsystem_release(struct inode *inode, struct file *file)
1448{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001449 struct trace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001450
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001451 trace_array_put(dir->tr);
1452
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001453 /*
1454 * If dir->subsystem is NULL, then this is a temporary
1455 * descriptor that was made for a trace_array to enable
1456 * all subsystems.
1457 */
1458 if (dir->subsystem)
1459 put_system(dir);
1460 else
1461 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001462
1463 return 0;
1464}
1465
Tom Zanussicfb180f2009-03-22 03:31:17 -05001466static ssize_t
1467subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1468 loff_t *ppos)
1469{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001470 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001471 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001472 struct trace_seq *s;
1473 int r;
1474
1475 if (*ppos)
1476 return 0;
1477
1478 s = kmalloc(sizeof(*s), GFP_KERNEL);
1479 if (!s)
1480 return -ENOMEM;
1481
1482 trace_seq_init(s);
1483
Tom Zanussi8b372562009-04-28 03:04:59 -05001484 print_subsystem_event_filter(system, s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001485 r = simple_read_from_buffer(ubuf, cnt, ppos,
1486 s->buffer, trace_seq_used(s));
Tom Zanussicfb180f2009-03-22 03:31:17 -05001487
1488 kfree(s);
1489
1490 return r;
1491}
1492
1493static ssize_t
1494subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1495 loff_t *ppos)
1496{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001497 struct trace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001498 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001499 int err;
1500
Tom Zanussi8b372562009-04-28 03:04:59 -05001501 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001502 return -EINVAL;
1503
Tom Zanussi8b372562009-04-28 03:04:59 -05001504 buf = (char *)__get_free_page(GFP_TEMPORARY);
1505 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001506 return -ENOMEM;
1507
Tom Zanussi8b372562009-04-28 03:04:59 -05001508 if (copy_from_user(buf, ubuf, cnt)) {
1509 free_page((unsigned long) buf);
1510 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001511 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001512 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001513
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001514 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001515 free_page((unsigned long) buf);
1516 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001517 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001518
1519 *ppos += cnt;
1520
1521 return cnt;
1522}
1523
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001524static ssize_t
1525show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1526{
1527 int (*func)(struct trace_seq *s) = filp->private_data;
1528 struct trace_seq *s;
1529 int r;
1530
1531 if (*ppos)
1532 return 0;
1533
1534 s = kmalloc(sizeof(*s), GFP_KERNEL);
1535 if (!s)
1536 return -ENOMEM;
1537
1538 trace_seq_init(s);
1539
1540 func(s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001541 r = simple_read_from_buffer(ubuf, cnt, ppos,
1542 s->buffer, trace_seq_used(s));
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001543
1544 kfree(s);
1545
1546 return r;
1547}
1548
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001549static int max_pids(struct trace_pid_list *pid_list)
1550{
1551 return (PAGE_SIZE << pid_list->order) / sizeof(pid_t);
1552}
1553
Steven Rostedt (Red Hat)8ca532a2015-10-21 15:27:36 -04001554static void ignore_task_cpu(void *data)
1555{
1556 struct trace_array *tr = data;
1557 struct trace_pid_list *pid_list;
1558
1559 /*
1560 * This function is called by on_each_cpu() while the
1561 * event_mutex is held.
1562 */
1563 pid_list = rcu_dereference_protected(tr->filtered_pids,
1564 mutex_is_locked(&event_mutex));
1565
1566 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1567 check_ignore_pid(pid_list, current));
1568}
1569
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001570static ssize_t
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001571ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001572 size_t cnt, loff_t *ppos)
1573{
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001574 struct seq_file *m = filp->private_data;
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001575 struct trace_array *tr = m->private;
1576 struct trace_pid_list *filtered_pids = NULL;
1577 struct trace_pid_list *pid_list = NULL;
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001578 struct trace_event_file *file;
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001579 struct trace_parser parser;
1580 unsigned long val;
1581 loff_t this_pos;
1582 ssize_t read = 0;
1583 ssize_t ret = 0;
1584 pid_t pid;
1585 int i;
1586
1587 if (!cnt)
1588 return 0;
1589
1590 ret = tracing_update_buffers();
1591 if (ret < 0)
1592 return ret;
1593
1594 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1595 return -ENOMEM;
1596
1597 mutex_lock(&event_mutex);
1598 /*
1599 * Load as many pids into the array before doing a
1600 * swap from the tr->filtered_pids to the new list.
1601 */
1602 while (cnt > 0) {
1603
1604 this_pos = 0;
1605
1606 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1607 if (ret < 0 || !trace_parser_loaded(&parser))
1608 break;
1609
1610 read += ret;
1611 ubuf += ret;
1612 cnt -= ret;
1613
1614 parser.buffer[parser.idx] = 0;
1615
1616 ret = -EINVAL;
1617 if (kstrtoul(parser.buffer, 0, &val))
1618 break;
1619 if (val > INT_MAX)
1620 break;
1621
1622 pid = (pid_t)val;
1623
1624 ret = -ENOMEM;
1625 if (!pid_list) {
1626 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1627 if (!pid_list)
1628 break;
1629
1630 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1631 lockdep_is_held(&event_mutex));
1632 if (filtered_pids)
1633 pid_list->order = filtered_pids->order;
1634 else
1635 pid_list->order = 0;
1636
1637 pid_list->pids = (void *)__get_free_pages(GFP_KERNEL,
1638 pid_list->order);
1639 if (!pid_list->pids)
1640 break;
1641
1642 if (filtered_pids) {
1643 pid_list->nr_pids = filtered_pids->nr_pids;
1644 memcpy(pid_list->pids, filtered_pids->pids,
1645 pid_list->nr_pids * sizeof(pid_t));
1646 } else
1647 pid_list->nr_pids = 0;
1648 }
1649
1650 if (pid_list->nr_pids >= max_pids(pid_list)) {
1651 pid_t *pid_page;
1652
1653 pid_page = (void *)__get_free_pages(GFP_KERNEL,
1654 pid_list->order + 1);
1655 if (!pid_page)
1656 break;
1657 memcpy(pid_page, pid_list->pids,
1658 pid_list->nr_pids * sizeof(pid_t));
1659 free_pages((unsigned long)pid_list->pids, pid_list->order);
1660
1661 pid_list->order++;
1662 pid_list->pids = pid_page;
1663 }
1664
1665 pid_list->pids[pid_list->nr_pids++] = pid;
1666 trace_parser_clear(&parser);
1667 ret = 0;
1668 }
1669 trace_parser_put(&parser);
1670
1671 if (ret < 0) {
1672 if (pid_list)
1673 free_pages((unsigned long)pid_list->pids, pid_list->order);
1674 kfree(pid_list);
1675 mutex_unlock(&event_mutex);
1676 return ret;
1677 }
1678
1679 if (!pid_list) {
1680 mutex_unlock(&event_mutex);
1681 return ret;
1682 }
1683
1684 sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL);
1685
1686 /* Remove duplicates */
1687 for (i = 1; i < pid_list->nr_pids; i++) {
1688 int start = i;
1689
1690 while (i < pid_list->nr_pids &&
1691 pid_list->pids[i - 1] == pid_list->pids[i])
1692 i++;
1693
1694 if (start != i) {
1695 if (i < pid_list->nr_pids) {
1696 memmove(&pid_list->pids[start], &pid_list->pids[i],
1697 (pid_list->nr_pids - i) * sizeof(pid_t));
1698 pid_list->nr_pids -= i - start;
1699 i = start;
1700 } else
1701 pid_list->nr_pids = start;
1702 }
1703 }
1704
1705 rcu_assign_pointer(tr->filtered_pids, pid_list);
1706
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001707 list_for_each_entry(file, &tr->events, list) {
1708 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1709 }
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001710
1711 if (filtered_pids) {
1712 synchronize_sched();
1713
1714 free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
1715 kfree(filtered_pids);
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001716 } else {
1717 /*
1718 * Register a probe that is called before all other probes
1719 * to set ignore_pid if next or prev do not match.
1720 * Register a probe this is called after all other probes
1721 * to only keep ignore_pid set if next pid matches.
1722 */
1723 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1724 tr, INT_MAX);
1725 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1726 tr, 0);
1727
1728 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1729 tr, INT_MAX);
1730 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1731 tr, 0);
Steven Rostedt (Red Hat)8ca532a2015-10-21 15:27:36 -04001732
1733 /*
1734 * Ignoring of pids is done at task switch. But we have to
1735 * check for those tasks that are currently running.
1736 */
1737 on_each_cpu(ignore_task_cpu, tr, 1);
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001738 }
1739
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001740 mutex_unlock(&event_mutex);
1741
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001742 ret = read;
1743 *ppos += read;
1744
1745 return ret;
1746}
1747
Steven Rostedt15075ca2012-05-03 14:57:28 -04001748static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1749static int ftrace_event_set_open(struct inode *inode, struct file *file);
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001750static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001751static int ftrace_event_release(struct inode *inode, struct file *file);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001752
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001753static const struct seq_operations show_event_seq_ops = {
1754 .start = t_start,
1755 .next = t_next,
1756 .show = t_show,
1757 .stop = t_stop,
1758};
1759
1760static const struct seq_operations show_set_event_seq_ops = {
1761 .start = s_start,
1762 .next = s_next,
1763 .show = t_show,
1764 .stop = t_stop,
1765};
1766
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001767static const struct seq_operations show_set_pid_seq_ops = {
1768 .start = p_start,
1769 .next = p_next,
1770 .show = p_show,
1771 .stop = p_stop,
1772};
1773
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001774static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001775 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001776 .read = seq_read,
1777 .llseek = seq_lseek,
1778 .release = seq_release,
1779};
1780
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001781static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001782 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001783 .read = seq_read,
1784 .write = ftrace_event_write,
1785 .llseek = seq_lseek,
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001786 .release = ftrace_event_release,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001787};
1788
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001789static const struct file_operations ftrace_set_event_pid_fops = {
1790 .open = ftrace_event_set_pid_open,
1791 .read = seq_read,
1792 .write = ftrace_event_pid_write,
1793 .llseek = seq_lseek,
1794 .release = ftrace_event_release,
1795};
1796
Steven Rostedt1473e442009-02-24 14:15:08 -05001797static const struct file_operations ftrace_enable_fops = {
Oleg Nesterovbf682c32013-07-28 20:35:27 +02001798 .open = tracing_open_generic,
Steven Rostedt1473e442009-02-24 14:15:08 -05001799 .read = event_enable_read,
1800 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001801 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001802};
1803
Steven Rostedt981d0812009-03-02 13:53:59 -05001804static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001805 .open = trace_format_open,
1806 .read = seq_read,
1807 .llseek = seq_lseek,
1808 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001809};
1810
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001811static const struct file_operations ftrace_event_id_fops = {
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001812 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001813 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001814};
1815
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001816static const struct file_operations ftrace_event_filter_fops = {
1817 .open = tracing_open_generic,
1818 .read = event_filter_read,
1819 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001820 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001821};
1822
Tom Zanussicfb180f2009-03-22 03:31:17 -05001823static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001824 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001825 .read = subsystem_filter_read,
1826 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001827 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001828 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001829};
1830
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001831static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001832 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001833 .read = system_enable_read,
1834 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001835 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001836 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001837};
1838
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001839static const struct file_operations ftrace_tr_enable_fops = {
1840 .open = system_tr_open,
1841 .read = system_enable_read,
1842 .write = system_enable_write,
1843 .llseek = default_llseek,
1844 .release = subsystem_release,
1845};
1846
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001847static const struct file_operations ftrace_show_header_fops = {
1848 .open = tracing_open_generic,
1849 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001850 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001851};
1852
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001853static int
1854ftrace_event_open(struct inode *inode, struct file *file,
1855 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001856{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001857 struct seq_file *m;
1858 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001859
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001860 ret = seq_open(file, seq_ops);
1861 if (ret < 0)
1862 return ret;
1863 m = file->private_data;
1864 /* copy tr over to seq ops */
1865 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001866
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001867 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001868}
1869
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001870static int ftrace_event_release(struct inode *inode, struct file *file)
1871{
1872 struct trace_array *tr = inode->i_private;
1873
1874 trace_array_put(tr);
1875
1876 return seq_release(inode, file);
1877}
1878
Steven Rostedt15075ca2012-05-03 14:57:28 -04001879static int
1880ftrace_event_avail_open(struct inode *inode, struct file *file)
1881{
1882 const struct seq_operations *seq_ops = &show_event_seq_ops;
1883
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001884 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001885}
1886
1887static int
1888ftrace_event_set_open(struct inode *inode, struct file *file)
1889{
1890 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001891 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001892 int ret;
1893
1894 if (trace_array_get(tr) < 0)
1895 return -ENODEV;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001896
1897 if ((file->f_mode & FMODE_WRITE) &&
1898 (file->f_flags & O_TRUNC))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001899 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001900
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001901 ret = ftrace_event_open(inode, file, seq_ops);
1902 if (ret < 0)
1903 trace_array_put(tr);
1904 return ret;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001905}
1906
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001907static int
1908ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1909{
1910 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1911 struct trace_array *tr = inode->i_private;
1912 int ret;
1913
1914 if (trace_array_get(tr) < 0)
1915 return -ENODEV;
1916
1917 if ((file->f_mode & FMODE_WRITE) &&
1918 (file->f_flags & O_TRUNC))
1919 ftrace_clear_event_pids(tr);
1920
1921 ret = ftrace_event_open(inode, file, seq_ops);
1922 if (ret < 0)
1923 trace_array_put(tr);
1924 return ret;
1925}
1926
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001927static struct event_subsystem *
1928create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001929{
1930 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001931
1932 /* need to create new entry */
1933 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001934 if (!system)
1935 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001936
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001937 system->ref_count = 1;
Steven Rostedt6e94a782013-06-27 10:58:31 -04001938
1939 /* Only allocate if dynamic (kprobes and modules) */
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001940 system->name = kstrdup_const(name, GFP_KERNEL);
1941 if (!system->name)
1942 goto out_free;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001943
Tom Zanussi30e673b2009-04-28 03:04:47 -05001944 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001945
Tom Zanussi8b372562009-04-28 03:04:59 -05001946 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001947 if (!system->filter)
1948 goto out_free;
1949
1950 list_add(&system->list, &event_subsystems);
1951
1952 return system;
1953
1954 out_free:
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001955 kfree_const(system->name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001956 kfree(system);
1957 return NULL;
1958}
1959
1960static struct dentry *
1961event_subsystem_dir(struct trace_array *tr, const char *name,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001962 struct trace_event_file *file, struct dentry *parent)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001963{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001964 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001965 struct event_subsystem *system;
1966 struct dentry *entry;
1967
1968 /* First see if we did not already create this dir */
1969 list_for_each_entry(dir, &tr->systems, list) {
1970 system = dir->subsystem;
1971 if (strcmp(system->name, name) == 0) {
1972 dir->nr_events++;
1973 file->system = dir;
1974 return dir->entry;
1975 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001976 }
1977
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001978 /* Now see if the system itself exists. */
1979 list_for_each_entry(system, &event_subsystems, list) {
1980 if (strcmp(system->name, name) == 0)
1981 break;
1982 }
1983 /* Reset system variable when not found */
1984 if (&system->list == &event_subsystems)
1985 system = NULL;
1986
1987 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1988 if (!dir)
1989 goto out_fail;
1990
1991 if (!system) {
1992 system = create_new_subsystem(name);
1993 if (!system)
1994 goto out_free;
1995 } else
1996 __get_system(system);
1997
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001998 dir->entry = tracefs_create_dir(name, parent);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001999 if (!dir->entry) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002000 pr_warn("Failed to create system directory %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002001 __put_system(system);
2002 goto out_free;
2003 }
2004
2005 dir->tr = tr;
2006 dir->ref_count = 1;
2007 dir->nr_events = 1;
2008 dir->subsystem = system;
2009 file->system = dir;
2010
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002011 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05002012 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05002013 if (!entry) {
2014 kfree(system->filter);
2015 system->filter = NULL;
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002016 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05002017 }
Tom Zanussie1112b42009-03-31 00:48:49 -05002018
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002019 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02002020 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04002021
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002022 list_add(&dir->list, &tr->systems);
2023
2024 return dir->entry;
2025
2026 out_free:
2027 kfree(dir);
2028 out_fail:
2029 /* Only print this message if failed on memory allocation */
2030 if (!dir || !system)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002031 pr_warn("No memory to create event subsystem %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002032 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05002033}
2034
Steven Rostedt1473e442009-02-24 14:15:08 -05002035static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002036event_create_dir(struct dentry *parent, struct trace_event_file *file)
Steven Rostedt1473e442009-02-24 14:15:08 -05002037{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002038 struct trace_event_call *call = file->event_call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002039 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04002040 struct list_head *head;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002041 struct dentry *d_events;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002042 const char *name;
Steven Rostedtfd994982009-02-28 02:41:25 -05002043 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05002044
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05002045 /*
2046 * If the trace point header did not define TRACE_SYSTEM
2047 * then the system would be called "TRACE_SYSTEM".
2048 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002049 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2050 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2051 if (!d_events)
2052 return -ENOMEM;
2053 } else
2054 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05002055
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002056 name = trace_event_name(call);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002057 file->dir = tracefs_create_dir(name, d_events);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002058 if (!file->dir) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002059 pr_warn("Could not create tracefs '%s' directory\n", name);
Steven Rostedt1473e442009-02-24 14:15:08 -05002060 return -1;
2061 }
2062
Steven Rostedt9b637762012-05-10 15:55:43 -04002063 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002064 trace_create_file("enable", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002065 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05002066
Steven Rostedt22392912010-04-21 12:27:06 -04002067#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04002068 if (call->event.type && call->class->reg)
Oleg Nesterov1a111262013-07-26 19:25:32 +02002069 trace_create_file("id", 0444, file->dir,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002070 (void *)(long)call->event.type,
2071 &ftrace_event_id_fops);
Steven Rostedt22392912010-04-21 12:27:06 -04002072#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01002073
Li Zefanc9d932c2010-05-24 16:24:28 +08002074 /*
2075 * Other events may have the same class. Only update
2076 * the fields if they are not already defined.
2077 */
2078 head = trace_get_fields(call);
2079 if (list_empty(head)) {
2080 ret = call->class->define_fields(call);
2081 if (ret < 0) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002082 pr_warn("Could not initialize trace point events/%s\n",
2083 name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002084 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05002085 }
2086 }
Tom Zanussif306cc82013-10-24 08:34:17 -05002087 trace_create_file("filter", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002088 &ftrace_event_filter_fops);
Tom Zanussicf027f62009-03-22 03:30:39 -05002089
Tom Zanussi85f2b082013-10-24 08:59:24 -05002090 trace_create_file("trigger", 0644, file->dir, file,
2091 &event_trigger_fops);
2092
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002093 trace_create_file("format", 0444, file->dir, call,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002094 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -05002095
Steven Rostedt1473e442009-02-24 14:15:08 -05002096 return 0;
2097}
2098
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002099static void remove_event_from_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002100{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002101 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002102 struct trace_array *tr;
2103
2104 do_for_each_event_file_safe(tr, file) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002105 if (file->event_call != call)
2106 continue;
2107
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002108 remove_event_file_dir(file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002109 /*
2110 * The do_for_each_event_file_safe() is
2111 * a double loop. After finding the call for this
2112 * trace_array, we use break to jump to the next
2113 * trace_array.
2114 */
2115 break;
2116 } while_for_each_event_file();
2117}
2118
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002119static void event_remove(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03002120{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002121 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002122 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002123
2124 do_for_each_event_file(tr, file) {
2125 if (file->event_call != call)
2126 continue;
2127 ftrace_event_enable_disable(file, 0);
2128 /*
2129 * The do_for_each_event_file() is
2130 * a double loop. After finding the call for this
2131 * trace_array, we use break to jump to the next
2132 * trace_array.
2133 */
2134 break;
2135 } while_for_each_event_file();
2136
Ezequiel Garcia87819152012-09-12 11:47:57 -03002137 if (call->event.funcs)
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04002138 __unregister_trace_event(&call->event);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002139 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002140 list_del(&call->list);
2141}
2142
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002143static int event_init(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03002144{
2145 int ret = 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002146 const char *name;
Ezequiel Garcia87819152012-09-12 11:47:57 -03002147
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002148 name = trace_event_name(call);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002149 if (WARN_ON(!name))
Ezequiel Garcia87819152012-09-12 11:47:57 -03002150 return -EINVAL;
2151
2152 if (call->class->raw_init) {
2153 ret = call->class->raw_init(call);
2154 if (ret < 0 && ret != -ENOSYS)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002155 pr_warn("Could not initialize trace events/%s\n", name);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002156 }
2157
2158 return ret;
2159}
2160
Li Zefan67ead0a2010-05-24 16:25:13 +08002161static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002162__register_event(struct trace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002163{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002164 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04002165
Ezequiel Garcia87819152012-09-12 11:47:57 -03002166 ret = event_init(call);
2167 if (ret < 0)
2168 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04002169
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002170 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08002171 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07002172
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002173 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002174}
2175
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002176static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2177{
2178 int rlen;
2179 int elen;
2180
2181 /* Find the length of the enum value as a string */
2182 elen = snprintf(ptr, 0, "%ld", map->enum_value);
2183 /* Make sure there's enough room to replace the string with the value */
2184 if (len < elen)
2185 return NULL;
2186
2187 snprintf(ptr, elen + 1, "%ld", map->enum_value);
2188
2189 /* Get the rest of the string of ptr */
2190 rlen = strlen(ptr + len);
2191 memmove(ptr + elen, ptr + len, rlen);
2192 /* Make sure we end the new string */
2193 ptr[elen + rlen] = 0;
2194
2195 return ptr + elen;
2196}
2197
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002198static void update_event_printk(struct trace_event_call *call,
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002199 struct trace_enum_map *map)
2200{
2201 char *ptr;
2202 int quote = 0;
2203 int len = strlen(map->enum_string);
2204
2205 for (ptr = call->print_fmt; *ptr; ptr++) {
2206 if (*ptr == '\\') {
2207 ptr++;
2208 /* paranoid */
2209 if (!*ptr)
2210 break;
2211 continue;
2212 }
2213 if (*ptr == '"') {
2214 quote ^= 1;
2215 continue;
2216 }
2217 if (quote)
2218 continue;
2219 if (isdigit(*ptr)) {
2220 /* skip numbers */
2221 do {
2222 ptr++;
2223 /* Check for alpha chars like ULL */
2224 } while (isalnum(*ptr));
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002225 if (!*ptr)
2226 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002227 /*
2228 * A number must have some kind of delimiter after
2229 * it, and we can ignore that too.
2230 */
2231 continue;
2232 }
2233 if (isalpha(*ptr) || *ptr == '_') {
2234 if (strncmp(map->enum_string, ptr, len) == 0 &&
2235 !isalnum(ptr[len]) && ptr[len] != '_') {
2236 ptr = enum_replace(ptr, map, len);
2237 /* Hmm, enum string smaller than value */
2238 if (WARN_ON_ONCE(!ptr))
2239 return;
2240 /*
2241 * No need to decrement here, as enum_replace()
2242 * returns the pointer to the character passed
2243 * the enum, and two enums can not be placed
2244 * back to back without something in between.
2245 * We can skip that something in between.
2246 */
2247 continue;
2248 }
2249 skip_more:
2250 do {
2251 ptr++;
2252 } while (isalnum(*ptr) || *ptr == '_');
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002253 if (!*ptr)
2254 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002255 /*
2256 * If what comes after this variable is a '.' or
2257 * '->' then we can continue to ignore that string.
2258 */
2259 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2260 ptr += *ptr == '.' ? 1 : 2;
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002261 if (!*ptr)
2262 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002263 goto skip_more;
2264 }
2265 /*
2266 * Once again, we can skip the delimiter that came
2267 * after the string.
2268 */
2269 continue;
2270 }
2271 }
2272}
2273
2274void trace_event_enum_update(struct trace_enum_map **map, int len)
2275{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002276 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002277 const char *last_system = NULL;
2278 int last_i;
2279 int i;
2280
2281 down_write(&trace_event_sem);
2282 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2283 /* events are usually grouped together with systems */
2284 if (!last_system || call->class->system != last_system) {
2285 last_i = 0;
2286 last_system = call->class->system;
2287 }
2288
2289 for (i = last_i; i < len; i++) {
2290 if (call->class->system == map[i]->system) {
2291 /* Save the first system if need be */
2292 if (!last_i)
2293 last_i = i;
2294 update_event_printk(call, map[i]);
2295 }
2296 }
2297 }
2298 up_write(&trace_event_sem);
2299}
2300
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002301static struct trace_event_file *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002302trace_create_new_event(struct trace_event_call *call,
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002303 struct trace_array *tr)
2304{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002305 struct trace_event_file *file;
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002306
2307 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2308 if (!file)
2309 return NULL;
2310
2311 file->event_call = call;
2312 file->tr = tr;
2313 atomic_set(&file->sm_ref, 0);
Tom Zanussi85f2b082013-10-24 08:59:24 -05002314 atomic_set(&file->tm_ref, 0);
2315 INIT_LIST_HEAD(&file->triggers);
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002316 list_add(&file->list, &tr->events);
2317
2318 return file;
2319}
2320
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002321/* Add an event to a trace directory */
2322static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002323__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002324{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002325 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002326
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002327 file = trace_create_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002328 if (!file)
2329 return -ENOMEM;
2330
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002331 return event_create_dir(tr->event_dir, file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002332}
2333
Steven Rostedt77248222013-02-27 16:28:06 -05002334/*
2335 * Just create a decriptor for early init. A descriptor is required
2336 * for enabling events at boot. We want to enable events before
2337 * the filesystem is initialized.
2338 */
2339static __init int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002340__trace_early_add_new_event(struct trace_event_call *call,
Steven Rostedt77248222013-02-27 16:28:06 -05002341 struct trace_array *tr)
2342{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002343 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002344
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002345 file = trace_create_new_event(call, tr);
Steven Rostedt77248222013-02-27 16:28:06 -05002346 if (!file)
2347 return -ENOMEM;
2348
Steven Rostedt77248222013-02-27 16:28:06 -05002349 return 0;
2350}
2351
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002352struct ftrace_module_file_ops;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002353static void __add_event_to_tracers(struct trace_event_call *call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002354
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002355/* Add an additional event_call dynamically */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002356int trace_add_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002357{
2358 int ret;
Alexander Z Lama8227412013-07-01 19:37:54 -07002359 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002360 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002361
2362 ret = __register_event(call, NULL);
2363 if (ret >= 0)
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002364 __add_event_to_tracers(call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002365
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002366 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002367 mutex_unlock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002368 return ret;
2369}
Steven Rostedt701970b2009-04-24 23:11:22 -04002370
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04002371/*
Alexander Z Lama8227412013-07-01 19:37:54 -07002372 * Must be called under locking of trace_types_lock, event_mutex and
2373 * trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04002374 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002375static void __trace_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002376{
Ezequiel Garcia87819152012-09-12 11:47:57 -03002377 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002378 trace_destroy_fields(call);
Oleg Nesterov57375742014-07-15 20:48:16 +02002379 free_event_filter(call->filter);
2380 call->filter = NULL;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002381}
2382
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002383static int probe_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002384{
Oleg Nesterov2816c552013-07-29 19:50:33 +02002385 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002386 struct trace_event_file *file;
Oleg Nesterov2816c552013-07-29 19:50:33 +02002387
2388#ifdef CONFIG_PERF_EVENTS
2389 if (call->perf_refcount)
2390 return -EBUSY;
2391#endif
2392 do_for_each_event_file(tr, file) {
2393 if (file->event_call != call)
2394 continue;
2395 /*
2396 * We can't rely on ftrace_event_enable_disable(enable => 0)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002397 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
Oleg Nesterov2816c552013-07-29 19:50:33 +02002398 * TRACE_REG_UNREGISTER.
2399 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002400 if (file->flags & EVENT_FILE_FL_ENABLED)
Oleg Nesterov2816c552013-07-29 19:50:33 +02002401 return -EBUSY;
Steven Rostedt (Red Hat)2ba64032013-07-31 13:16:22 -04002402 /*
2403 * The do_for_each_event_file_safe() is
2404 * a double loop. After finding the call for this
2405 * trace_array, we use break to jump to the next
2406 * trace_array.
2407 */
Oleg Nesterov2816c552013-07-29 19:50:33 +02002408 break;
2409 } while_for_each_event_file();
2410
2411 __trace_remove_event_call(call);
2412
2413 return 0;
2414}
2415
2416/* Remove an event_call */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002417int trace_remove_event_call(struct trace_event_call *call)
Oleg Nesterov2816c552013-07-29 19:50:33 +02002418{
2419 int ret;
2420
Alexander Z Lama8227412013-07-01 19:37:54 -07002421 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002422 mutex_lock(&event_mutex);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002423 down_write(&trace_event_sem);
Oleg Nesterov2816c552013-07-29 19:50:33 +02002424 ret = probe_remove_event_call(call);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002425 up_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002426 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002427 mutex_unlock(&trace_types_lock);
Oleg Nesterov2816c552013-07-29 19:50:33 +02002428
2429 return ret;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002430}
2431
2432#define for_each_event(event, start, end) \
2433 for (event = start; \
2434 (unsigned long)event < (unsigned long)end; \
2435 event++)
2436
2437#ifdef CONFIG_MODULES
2438
Steven Rostedt6d723732009-04-10 14:53:50 -04002439static void trace_module_add_events(struct module *mod)
2440{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002441 struct trace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04002442
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -05002443 if (!mod->num_trace_events)
2444 return;
2445
2446 /* Don't add infrastructure for mods without tracepoints */
2447 if (trace_module_has_bad_taint(mod)) {
2448 pr_err("%s: module has bad taint, not creating trace events\n",
2449 mod->name);
2450 return;
2451 }
2452
Steven Rostedt6d723732009-04-10 14:53:50 -04002453 start = mod->trace_events;
2454 end = mod->trace_events + mod->num_trace_events;
2455
Steven Rostedt6d723732009-04-10 14:53:50 -04002456 for_each_event(call, start, end) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002457 __register_event(*call, mod);
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002458 __add_event_to_tracers(*call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002459 }
2460}
2461
2462static void trace_module_remove_events(struct module *mod)
2463{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002464 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002465 bool clear_trace = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04002466
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002467 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04002468 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2469 if (call->mod == mod) {
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002470 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2471 clear_trace = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002472 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002473 }
2474 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002475 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002476
2477 /*
2478 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002479 * registered any events that were used. The only worry is if
2480 * a new module gets loaded, and takes on the same id as the events
2481 * of this module. When printing out the buffer, traced events left
2482 * over from this module may be passed to the new module events and
2483 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002484 */
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002485 if (clear_trace)
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002486 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04002487}
2488
Steven Rostedt61f919a2009-04-14 18:22:32 -04002489static int trace_module_notify(struct notifier_block *self,
2490 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04002491{
2492 struct module *mod = data;
2493
Alexander Z Lama8227412013-07-01 19:37:54 -07002494 mutex_lock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002495 mutex_lock(&event_mutex);
2496 switch (val) {
2497 case MODULE_STATE_COMING:
2498 trace_module_add_events(mod);
2499 break;
2500 case MODULE_STATE_GOING:
2501 trace_module_remove_events(mod);
2502 break;
2503 }
2504 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002505 mutex_unlock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002506
2507 return 0;
2508}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05002509
Oleg Nesterov836d4812013-07-31 19:31:37 +02002510static struct notifier_block trace_module_nb = {
2511 .notifier_call = trace_module_notify,
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002512 .priority = 1, /* higher than trace.c module notify */
Oleg Nesterov836d4812013-07-31 19:31:37 +02002513};
Steven Rostedt61f919a2009-04-14 18:22:32 -04002514#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04002515
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002516/* Create a new event directory structure for a trace directory. */
2517static void
2518__trace_add_event_dirs(struct trace_array *tr)
2519{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002520 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002521 int ret;
2522
2523 list_for_each_entry(call, &ftrace_events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002524 ret = __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002525 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002526 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002527 trace_event_name(call));
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002528 }
2529}
2530
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002531struct trace_event_file *
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002532find_event_file(struct trace_array *tr, const char *system, const char *event)
2533{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002534 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002535 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002536 const char *name;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002537
2538 list_for_each_entry(file, &tr->events, list) {
2539
2540 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002541 name = trace_event_name(call);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002542
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002543 if (!name || !call->class || !call->class->reg)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002544 continue;
2545
2546 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2547 continue;
2548
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002549 if (strcmp(event, name) == 0 &&
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002550 strcmp(system, call->class->system) == 0)
2551 return file;
2552 }
2553 return NULL;
2554}
2555
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002556#ifdef CONFIG_DYNAMIC_FTRACE
2557
2558/* Avoid typos */
2559#define ENABLE_EVENT_STR "enable_event"
2560#define DISABLE_EVENT_STR "disable_event"
2561
2562struct event_probe_data {
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002563 struct trace_event_file *file;
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002564 unsigned long count;
2565 int ref;
2566 bool enable;
2567};
2568
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002569static void
2570event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2571{
2572 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2573 struct event_probe_data *data = *pdata;
2574
2575 if (!data)
2576 return;
2577
2578 if (data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002579 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002580 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002581 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002582}
2583
2584static void
2585event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2586{
2587 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2588 struct event_probe_data *data = *pdata;
2589
2590 if (!data)
2591 return;
2592
2593 if (!data->count)
2594 return;
2595
2596 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002597 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002598 return;
2599
2600 if (data->count != -1)
2601 (data->count)--;
2602
2603 event_enable_probe(ip, parent_ip, _data);
2604}
2605
2606static int
2607event_enable_print(struct seq_file *m, unsigned long ip,
2608 struct ftrace_probe_ops *ops, void *_data)
2609{
2610 struct event_probe_data *data = _data;
2611
2612 seq_printf(m, "%ps:", (void *)ip);
2613
2614 seq_printf(m, "%s:%s:%s",
2615 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2616 data->file->event_call->class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002617 trace_event_name(data->file->event_call));
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002618
2619 if (data->count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002620 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002621 else
2622 seq_printf(m, ":count=%ld\n", data->count);
2623
2624 return 0;
2625}
2626
2627static int
2628event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2629 void **_data)
2630{
2631 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2632 struct event_probe_data *data = *pdata;
2633
2634 data->ref++;
2635 return 0;
2636}
2637
2638static void
2639event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2640 void **_data)
2641{
2642 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2643 struct event_probe_data *data = *pdata;
2644
2645 if (WARN_ON_ONCE(data->ref <= 0))
2646 return;
2647
2648 data->ref--;
2649 if (!data->ref) {
2650 /* Remove the SOFT_MODE flag */
2651 __ftrace_event_enable_disable(data->file, 0, 1);
2652 module_put(data->file->event_call->mod);
2653 kfree(data);
2654 }
2655 *pdata = NULL;
2656}
2657
2658static struct ftrace_probe_ops event_enable_probe_ops = {
2659 .func = event_enable_probe,
2660 .print = event_enable_print,
2661 .init = event_enable_init,
2662 .free = event_enable_free,
2663};
2664
2665static struct ftrace_probe_ops event_enable_count_probe_ops = {
2666 .func = event_enable_count_probe,
2667 .print = event_enable_print,
2668 .init = event_enable_init,
2669 .free = event_enable_free,
2670};
2671
2672static struct ftrace_probe_ops event_disable_probe_ops = {
2673 .func = event_enable_probe,
2674 .print = event_enable_print,
2675 .init = event_enable_init,
2676 .free = event_enable_free,
2677};
2678
2679static struct ftrace_probe_ops event_disable_count_probe_ops = {
2680 .func = event_enable_count_probe,
2681 .print = event_enable_print,
2682 .init = event_enable_init,
2683 .free = event_enable_free,
2684};
2685
2686static int
2687event_enable_func(struct ftrace_hash *hash,
2688 char *glob, char *cmd, char *param, int enabled)
2689{
2690 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002691 struct trace_event_file *file;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002692 struct ftrace_probe_ops *ops;
2693 struct event_probe_data *data;
2694 const char *system;
2695 const char *event;
2696 char *number;
2697 bool enable;
2698 int ret;
2699
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002700 if (!tr)
2701 return -ENODEV;
2702
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002703 /* hash funcs only work with set_ftrace_filter */
Harsh Prateek Bora8092e802013-05-24 12:52:17 +05302704 if (!enabled || !param)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002705 return -EINVAL;
2706
2707 system = strsep(&param, ":");
2708 if (!param)
2709 return -EINVAL;
2710
2711 event = strsep(&param, ":");
2712
2713 mutex_lock(&event_mutex);
2714
2715 ret = -EINVAL;
2716 file = find_event_file(tr, system, event);
2717 if (!file)
2718 goto out;
2719
2720 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2721
2722 if (enable)
2723 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2724 else
2725 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2726
2727 if (glob[0] == '!') {
2728 unregister_ftrace_function_probe_func(glob+1, ops);
2729 ret = 0;
2730 goto out;
2731 }
2732
2733 ret = -ENOMEM;
2734 data = kzalloc(sizeof(*data), GFP_KERNEL);
2735 if (!data)
2736 goto out;
2737
2738 data->enable = enable;
2739 data->count = -1;
2740 data->file = file;
2741
2742 if (!param)
2743 goto out_reg;
2744
2745 number = strsep(&param, ":");
2746
2747 ret = -EINVAL;
2748 if (!strlen(number))
2749 goto out_free;
2750
2751 /*
2752 * We use the callback data field (which is a pointer)
2753 * as our counter.
2754 */
2755 ret = kstrtoul(number, 0, &data->count);
2756 if (ret)
2757 goto out_free;
2758
2759 out_reg:
2760 /* Don't let event modules unload while probe registered */
2761 ret = try_module_get(file->event_call->mod);
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002762 if (!ret) {
2763 ret = -EBUSY;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002764 goto out_free;
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002765 }
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002766
2767 ret = __ftrace_event_enable_disable(file, 1, 1);
2768 if (ret < 0)
2769 goto out_put;
2770 ret = register_ftrace_function_probe(glob, ops, data);
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002771 /*
2772 * The above returns on success the # of functions enabled,
2773 * but if it didn't find any functions it returns zero.
2774 * Consider no functions a failure too.
2775 */
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002776 if (!ret) {
2777 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002778 goto out_disable;
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002779 } else if (ret < 0)
2780 goto out_disable;
2781 /* Just return zero, not the number of enabled functions */
2782 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002783 out:
2784 mutex_unlock(&event_mutex);
2785 return ret;
2786
2787 out_disable:
2788 __ftrace_event_enable_disable(file, 0, 1);
2789 out_put:
2790 module_put(file->event_call->mod);
2791 out_free:
2792 kfree(data);
2793 goto out;
2794}
2795
2796static struct ftrace_func_command event_enable_cmd = {
2797 .name = ENABLE_EVENT_STR,
2798 .func = event_enable_func,
2799};
2800
2801static struct ftrace_func_command event_disable_cmd = {
2802 .name = DISABLE_EVENT_STR,
2803 .func = event_enable_func,
2804};
2805
2806static __init int register_event_cmds(void)
2807{
2808 int ret;
2809
2810 ret = register_ftrace_command(&event_enable_cmd);
2811 if (WARN_ON(ret < 0))
2812 return ret;
2813 ret = register_ftrace_command(&event_disable_cmd);
2814 if (WARN_ON(ret < 0))
2815 unregister_ftrace_command(&event_enable_cmd);
2816 return ret;
2817}
2818#else
2819static inline int register_event_cmds(void) { return 0; }
2820#endif /* CONFIG_DYNAMIC_FTRACE */
2821
Steven Rostedt77248222013-02-27 16:28:06 -05002822/*
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002823 * The top level array has already had its trace_event_file
Steven Rostedt77248222013-02-27 16:28:06 -05002824 * descriptors created in order to allow for early events to
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002825 * be recorded. This function is called after the tracefs has been
Steven Rostedt77248222013-02-27 16:28:06 -05002826 * initialized, and we now have to create the files associated
2827 * to the events.
2828 */
2829static __init void
2830__trace_early_add_event_dirs(struct trace_array *tr)
2831{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002832 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002833 int ret;
2834
2835
2836 list_for_each_entry(file, &tr->events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002837 ret = event_create_dir(tr->event_dir, file);
Steven Rostedt77248222013-02-27 16:28:06 -05002838 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002839 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002840 trace_event_name(file->event_call));
Steven Rostedt77248222013-02-27 16:28:06 -05002841 }
2842}
2843
2844/*
2845 * For early boot up, the top trace array requires to have
2846 * a list of events that can be enabled. This must be done before
2847 * the filesystem is set up in order to allow events to be traced
2848 * early.
2849 */
2850static __init void
2851__trace_early_add_events(struct trace_array *tr)
2852{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002853 struct trace_event_call *call;
Steven Rostedt77248222013-02-27 16:28:06 -05002854 int ret;
2855
2856 list_for_each_entry(call, &ftrace_events, list) {
2857 /* Early boot up should not have any modules loaded */
2858 if (WARN_ON_ONCE(call->mod))
2859 continue;
2860
2861 ret = __trace_early_add_new_event(call, tr);
2862 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002863 pr_warn("Could not create early event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002864 trace_event_name(call));
Steven Rostedt77248222013-02-27 16:28:06 -05002865 }
2866}
2867
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002868/* Remove the event directory structure for a trace directory. */
2869static void
2870__trace_remove_event_dirs(struct trace_array *tr)
2871{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002872 struct trace_event_file *file, *next;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002873
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002874 list_for_each_entry_safe(file, next, &tr->events, list)
2875 remove_event_file_dir(file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002876}
2877
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002878static void __add_event_to_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002879{
2880 struct trace_array *tr;
2881
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002882 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2883 __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002884}
2885
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002886extern struct trace_event_call *__start_ftrace_events[];
2887extern struct trace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002888
Li Zefan020e5f82009-07-01 10:47:05 +08002889static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2890
2891static __init int setup_trace_event(char *str)
2892{
2893 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002894 ring_buffer_expanded = true;
2895 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002896
2897 return 1;
2898}
2899__setup("trace_event=", setup_trace_event);
2900
Steven Rostedt77248222013-02-27 16:28:06 -05002901/* Expects to have event_mutex held when called */
2902static int
2903create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002904{
2905 struct dentry *d_events;
2906 struct dentry *entry;
2907
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002908 entry = tracefs_create_file("set_event", 0644, parent,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002909 tr, &ftrace_set_event_fops);
2910 if (!entry) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002911 pr_warn("Could not create tracefs 'set_event' entry\n");
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002912 return -ENOMEM;
2913 }
2914
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002915 d_events = tracefs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002916 if (!d_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002917 pr_warn("Could not create tracefs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002918 return -ENOMEM;
2919 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002920
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04002921 entry = tracefs_create_file("set_event_pid", 0644, parent,
2922 tr, &ftrace_set_event_pid_fops);
2923
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002924 /* ring buffer internal formats */
2925 trace_create_file("header_page", 0444, d_events,
2926 ring_buffer_print_page_header,
2927 &ftrace_show_header_fops);
2928
2929 trace_create_file("header_event", 0444, d_events,
2930 ring_buffer_print_entry_header,
2931 &ftrace_show_header_fops);
2932
2933 trace_create_file("enable", 0644, d_events,
2934 tr, &ftrace_tr_enable_fops);
2935
2936 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002937
2938 return 0;
2939}
2940
2941/**
2942 * event_trace_add_tracer - add a instance of a trace_array to events
2943 * @parent: The parent dentry to place the files/directories for events in
2944 * @tr: The trace array associated with these events
2945 *
2946 * When a new instance is created, it needs to set up its events
2947 * directory, as well as other files associated with events. It also
2948 * creates the event hierachry in the @parent/events directory.
2949 *
2950 * Returns 0 on success.
2951 */
2952int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2953{
2954 int ret;
2955
2956 mutex_lock(&event_mutex);
2957
2958 ret = create_event_toplevel_files(parent, tr);
2959 if (ret)
2960 goto out_unlock;
2961
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002962 down_write(&trace_event_sem);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002963 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002964 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002965
Steven Rostedt77248222013-02-27 16:28:06 -05002966 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04002967 mutex_unlock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002968
Steven Rostedt77248222013-02-27 16:28:06 -05002969 return ret;
2970}
2971
2972/*
2973 * The top trace array already had its file descriptors created.
2974 * Now the files themselves need to be created.
2975 */
2976static __init int
2977early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2978{
2979 int ret;
2980
2981 mutex_lock(&event_mutex);
2982
2983 ret = create_event_toplevel_files(parent, tr);
2984 if (ret)
2985 goto out_unlock;
2986
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002987 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002988 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002989 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002990
2991 out_unlock:
2992 mutex_unlock(&event_mutex);
2993
2994 return ret;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002995}
2996
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002997int event_trace_del_tracer(struct trace_array *tr)
2998{
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002999 mutex_lock(&event_mutex);
3000
Tom Zanussi85f2b082013-10-24 08:59:24 -05003001 /* Disable any event triggers and associated soft-disabled events */
3002 clear_event_triggers(tr);
3003
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04003004 /* Clear the pid list */
3005 __ftrace_clear_event_pids(tr);
3006
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -04003007 /* Disable any running events */
3008 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3009
Steven Rostedt3ccb0122013-12-03 12:41:20 -05003010 /* Access to events are within rcu_read_lock_sched() */
3011 synchronize_sched();
3012
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08003013 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04003014 __trace_remove_event_dirs(tr);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05003015 tracefs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08003016 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04003017
3018 tr->event_dir = NULL;
3019
3020 mutex_unlock(&event_mutex);
3021
3022 return 0;
3023}
3024
Steven Rostedtd1a29142013-02-27 20:23:57 -05003025static __init int event_trace_memsetup(void)
3026{
3027 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04003028 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
Steven Rostedtd1a29142013-02-27 20:23:57 -05003029 return 0;
3030}
3031
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003032static __init void
3033early_enable_events(struct trace_array *tr, bool disable_first)
3034{
3035 char *buf = bootup_event_buf;
3036 char *token;
3037 int ret;
3038
3039 while (true) {
3040 token = strsep(&buf, ",");
3041
3042 if (!token)
3043 break;
3044 if (!*token)
3045 continue;
3046
3047 /* Restarting syscalls requires that we stop them first */
3048 if (disable_first)
3049 ftrace_set_clr_event(tr, token, 0);
3050
3051 ret = ftrace_set_clr_event(tr, token, 1);
3052 if (ret)
3053 pr_warn("Failed to enable trace event: %s\n", token);
3054
3055 /* Put back the comma to allow this to be called again */
3056 if (buf)
3057 *(buf - 1) = ',';
3058 }
3059}
3060
Ezequiel Garcia87819152012-09-12 11:47:57 -03003061static __init int event_trace_enable(void)
3062{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003063 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003064 struct trace_event_call **iter, *call;
Ezequiel Garcia87819152012-09-12 11:47:57 -03003065 int ret;
3066
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09003067 if (!tr)
3068 return -ENODEV;
3069
Ezequiel Garcia87819152012-09-12 11:47:57 -03003070 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3071
3072 call = *iter;
3073 ret = event_init(call);
3074 if (!ret)
3075 list_add(&call->list, &ftrace_events);
3076 }
3077
Steven Rostedt77248222013-02-27 16:28:06 -05003078 /*
3079 * We need the top trace array to have a working set of trace
3080 * points at early init, before the debug files and directories
3081 * are created. Create the file entries now, and attach them
3082 * to the actual file dentries later.
3083 */
3084 __trace_early_add_events(tr);
3085
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003086 early_enable_events(tr, false);
Steven Rostedt81698832012-10-11 10:15:05 -04003087
3088 trace_printk_start_comm();
3089
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04003090 register_event_cmds();
3091
Tom Zanussi85f2b082013-10-24 08:59:24 -05003092 register_trigger_cmds();
3093
Ezequiel Garcia87819152012-09-12 11:47:57 -03003094 return 0;
3095}
3096
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003097/*
3098 * event_trace_enable() is called from trace_event_init() first to
3099 * initialize events and perhaps start any events that are on the
3100 * command line. Unfortunately, there are some events that will not
3101 * start this early, like the system call tracepoints that need
3102 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3103 * is called before pid 1 starts, and this flag is never set, making
3104 * the syscall tracepoint never get reached, but the event is enabled
3105 * regardless (and not doing anything).
3106 */
3107static __init int event_trace_enable_again(void)
3108{
3109 struct trace_array *tr;
3110
3111 tr = top_trace_array();
3112 if (!tr)
3113 return -ENODEV;
3114
3115 early_enable_events(tr, true);
3116
3117 return 0;
3118}
3119
3120early_initcall(event_trace_enable_again);
3121
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003122static __init int event_trace_init(void)
3123{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003124 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003125 struct dentry *d_tracer;
3126 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04003127 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003128
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003129 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09003130 if (!tr)
3131 return -ENODEV;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003132
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003133 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05003134 if (IS_ERR(d_tracer))
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003135 return 0;
3136
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05003137 entry = tracefs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003138 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04003139 if (!entry)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05003140 pr_warn("Could not create tracefs 'available_events' entry\n");
Steven Rostedt2314c4a2009-03-10 12:04:02 -04003141
Daniel Wagner9f616682015-08-10 14:35:46 +02003142 if (trace_define_generic_fields())
3143 pr_warn("tracing: Failed to allocated generic fields");
3144
Li Zefan8728fe52010-05-24 16:22:49 +08003145 if (trace_define_common_fields())
Fabian Frederick3448bac2014-06-07 13:43:08 +02003146 pr_warn("tracing: Failed to allocate common fields");
Li Zefan8728fe52010-05-24 16:22:49 +08003147
Steven Rostedt77248222013-02-27 16:28:06 -05003148 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003149 if (ret)
3150 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08003151
Oleg Nesterov836d4812013-07-31 19:31:37 +02003152#ifdef CONFIG_MODULES
Steven Rostedt6d723732009-04-10 14:53:50 -04003153 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08003154 if (ret)
Fabian Frederick3448bac2014-06-07 13:43:08 +02003155 pr_warn("Failed to register trace events module notifier\n");
Oleg Nesterov836d4812013-07-31 19:31:37 +02003156#endif
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003157 return 0;
3158}
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05003159
3160void __init trace_event_init(void)
3161{
3162 event_trace_memsetup();
3163 init_ftrace_syscalls();
3164 event_trace_enable();
3165}
3166
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003167fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04003168
3169#ifdef CONFIG_FTRACE_STARTUP_TEST
3170
3171static DEFINE_SPINLOCK(test_spinlock);
3172static DEFINE_SPINLOCK(test_spinlock_irq);
3173static DEFINE_MUTEX(test_mutex);
3174
3175static __init void test_work(struct work_struct *dummy)
3176{
3177 spin_lock(&test_spinlock);
3178 spin_lock_irq(&test_spinlock_irq);
3179 udelay(1);
3180 spin_unlock_irq(&test_spinlock_irq);
3181 spin_unlock(&test_spinlock);
3182
3183 mutex_lock(&test_mutex);
3184 msleep(1);
3185 mutex_unlock(&test_mutex);
3186}
3187
3188static __init int event_test_thread(void *unused)
3189{
3190 void *test_malloc;
3191
3192 test_malloc = kmalloc(1234, GFP_KERNEL);
3193 if (!test_malloc)
3194 pr_info("failed to kmalloc\n");
3195
3196 schedule_on_each_cpu(test_work);
3197
3198 kfree(test_malloc);
3199
3200 set_current_state(TASK_INTERRUPTIBLE);
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02003201 while (!kthread_should_stop()) {
Steven Rostedte6187002009-04-15 13:36:40 -04003202 schedule();
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02003203 set_current_state(TASK_INTERRUPTIBLE);
3204 }
3205 __set_current_state(TASK_RUNNING);
Steven Rostedte6187002009-04-15 13:36:40 -04003206
3207 return 0;
3208}
3209
3210/*
3211 * Do various things that may trigger events.
3212 */
3213static __init void event_test_stuff(void)
3214{
3215 struct task_struct *test_thread;
3216
3217 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3218 msleep(1);
3219 kthread_stop(test_thread);
3220}
3221
3222/*
3223 * For every trace event defined, we will test each trace point separately,
3224 * and then by groups, and finally all trace points.
3225 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003226static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04003227{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04003228 struct trace_subsystem_dir *dir;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04003229 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003230 struct trace_event_call *call;
Steven Rostedte6187002009-04-15 13:36:40 -04003231 struct event_subsystem *system;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003232 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04003233 int ret;
3234
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003235 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09003236 if (!tr)
3237 return;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003238
Steven Rostedte6187002009-04-15 13:36:40 -04003239 pr_info("Running tests on trace events:\n");
3240
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003241 list_for_each_entry(file, &tr->events, list) {
3242
3243 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04003244
Steven Rostedt22392912010-04-21 12:27:06 -04003245 /* Only test those that have a probe */
3246 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04003247 continue;
3248
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04003249/*
3250 * Testing syscall events here is pretty useless, but
3251 * we still do it if configured. But this is time consuming.
3252 * What we really need is a user thread to perform the
3253 * syscalls as we test.
3254 */
3255#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04003256 if (call->class->system &&
3257 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04003258 continue;
3259#endif
3260
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04003261 pr_info("Testing event %s: ", trace_event_name(call));
Steven Rostedte6187002009-04-15 13:36:40 -04003262
3263 /*
3264 * If an event is already enabled, someone is using
3265 * it and the self test should not be on.
3266 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04003267 if (file->flags & EVENT_FILE_FL_ENABLED) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003268 pr_warn("Enabled event during self test!\n");
Steven Rostedte6187002009-04-15 13:36:40 -04003269 WARN_ON_ONCE(1);
3270 continue;
3271 }
3272
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003273 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003274 event_test_stuff();
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003275 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04003276
3277 pr_cont("OK\n");
3278 }
3279
3280 /* Now test at the sub system level */
3281
3282 pr_info("Running tests on trace event systems:\n");
3283
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003284 list_for_each_entry(dir, &tr->systems, list) {
3285
3286 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04003287
3288 /* the ftrace system is special, skip it */
3289 if (strcmp(system->name, "ftrace") == 0)
3290 continue;
3291
3292 pr_info("Testing event system %s: ", system->name);
3293
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003294 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003295 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003296 pr_warn("error enabling system %s\n",
3297 system->name);
Steven Rostedte6187002009-04-15 13:36:40 -04003298 continue;
3299 }
3300
3301 event_test_stuff();
3302
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003303 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08003304 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003305 pr_warn("error disabling system %s\n",
3306 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08003307 continue;
3308 }
Steven Rostedte6187002009-04-15 13:36:40 -04003309
3310 pr_cont("OK\n");
3311 }
3312
3313 /* Test with all events enabled */
3314
3315 pr_info("Running tests on all trace events:\n");
3316 pr_info("Testing all events: ");
3317
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003318 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003319 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003320 pr_warn("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003321 return;
Steven Rostedte6187002009-04-15 13:36:40 -04003322 }
3323
3324 event_test_stuff();
3325
3326 /* reset sysname */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003327 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04003328 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003329 pr_warn("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003330 return;
Steven Rostedte6187002009-04-15 13:36:40 -04003331 }
3332
3333 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003334}
3335
3336#ifdef CONFIG_FUNCTION_TRACER
3337
Tejun Heo245b2e72009-06-24 15:13:48 +09003338static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003339
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04003340static struct trace_array *event_tr;
3341
3342static void __init
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003343function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003344 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003345{
3346 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04003347 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003348 struct ftrace_entry *entry;
3349 unsigned long flags;
3350 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003351 int cpu;
3352 int pc;
3353
3354 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04003355 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003356 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09003357 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003358
3359 if (disabled != 1)
3360 goto out;
3361
3362 local_save_flags(flags);
3363
Steven Rostedte77405a2009-09-02 14:17:06 -04003364 event = trace_current_buffer_lock_reserve(&buffer,
3365 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003366 flags, pc);
3367 if (!event)
3368 goto out;
3369 entry = ring_buffer_event_data(event);
3370 entry->ip = ip;
3371 entry->parent_ip = parent_ip;
3372
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04003373 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003374
3375 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09003376 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04003377 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003378}
3379
3380static struct ftrace_ops trace_ops __initdata =
3381{
3382 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04003383 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003384};
3385
3386static __init void event_trace_self_test_with_function(void)
3387{
Steven Rostedt17bb6152011-05-23 15:27:46 -04003388 int ret;
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003389 event_tr = top_trace_array();
3390 if (WARN_ON(!event_tr))
3391 return;
Steven Rostedt17bb6152011-05-23 15:27:46 -04003392 ret = register_ftrace_function(&trace_ops);
3393 if (WARN_ON(ret < 0)) {
3394 pr_info("Failed to enable function tracer for event tests\n");
3395 return;
3396 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003397 pr_info("Running tests again, along with the function tracer\n");
3398 event_trace_self_tests();
3399 unregister_ftrace_function(&trace_ops);
3400}
3401#else
3402static __init void event_trace_self_test_with_function(void)
3403{
3404}
3405#endif
3406
3407static __init int event_trace_self_tests_init(void)
3408{
Li Zefan020e5f82009-07-01 10:47:05 +08003409 if (!tracing_selftest_disabled) {
3410 event_trace_self_tests();
3411 event_trace_self_test_with_function();
3412 }
Steven Rostedte6187002009-04-15 13:36:40 -04003413
3414 return 0;
3415}
3416
Steven Rostedt28d20e22009-04-20 12:12:44 -04003417late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04003418
3419#endif