blob: 2ad7014707eec009cc3bbc0af4e5565f45035bb6 [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
Li Zefan020e5f82009-07-01 10:47:05 +080025#include <asm/setup.h>
26
Steven Rostedt91729ef92009-03-02 15:03:01 -050027#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050028
Steven Rostedt4e5292e2009-09-12 19:26:21 -040029#undef TRACE_SYSTEM
Steven Rostedtb628b3e2009-02-27 23:32:58 -050030#define TRACE_SYSTEM "TRACE_SYSTEM"
31
Li Zefan20c89282009-05-06 10:33:45 +080032DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050033
Steven Rostedta59fd602009-04-10 13:52:20 -040034LIST_HEAD(ftrace_events);
Daniel Wagner9f616682015-08-10 14:35:46 +020035static LIST_HEAD(ftrace_generic_fields);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080036static LIST_HEAD(ftrace_common_fields);
Steven Rostedta59fd602009-04-10 13:52:20 -040037
Steven Rostedtd1a29142013-02-27 20:23:57 -050038#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
39
40static struct kmem_cache *field_cachep;
41static struct kmem_cache *file_cachep;
42
Steven Rostedt6e94a782013-06-27 10:58:31 -040043static inline int system_refcount(struct event_subsystem *system)
44{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020045 return system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040046}
47
48static int system_refcount_inc(struct event_subsystem *system)
49{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020050 return system->ref_count++;
Steven Rostedt6e94a782013-06-27 10:58:31 -040051}
52
53static int system_refcount_dec(struct event_subsystem *system)
54{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020055 return --system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040056}
57
Steven Rostedtae63b31e2012-05-03 23:09:03 -040058/* Double loops, do not use break, only goto's work */
59#define do_for_each_event_file(tr, file) \
60 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
61 list_for_each_entry(file, &tr->events, list)
62
63#define do_for_each_event_file_safe(tr, file) \
64 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040065 struct trace_event_file *___n; \
Steven Rostedtae63b31e2012-05-03 23:09:03 -040066 list_for_each_entry_safe(file, ___n, &tr->events, list)
67
68#define while_for_each_event_file() \
69 }
70
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080071static struct list_head *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040072trace_get_fields(struct trace_event_call *event_call)
Steven Rostedt2e33af02010-04-22 10:35:55 -040073{
74 if (!event_call->class->get_fields)
75 return &event_call->class->fields;
76 return event_call->class->get_fields(event_call);
77}
78
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080079static struct ftrace_event_field *
80__find_event_field(struct list_head *head, char *name)
81{
82 struct ftrace_event_field *field;
83
84 list_for_each_entry(field, head, link) {
85 if (!strcmp(field->name, name))
86 return field;
87 }
88
89 return NULL;
90}
91
92struct ftrace_event_field *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040093trace_find_event_field(struct trace_event_call *call, char *name)
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080094{
95 struct ftrace_event_field *field;
96 struct list_head *head;
97
Daniel Wagner9f616682015-08-10 14:35:46 +020098 field = __find_event_field(&ftrace_generic_fields, name);
99 if (field)
100 return field;
101
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +0800102 field = __find_event_field(&ftrace_common_fields, name);
103 if (field)
104 return field;
105
106 head = trace_get_fields(call);
107 return __find_event_field(head, name);
108}
109
Li Zefan8728fe52010-05-24 16:22:49 +0800110static int __trace_define_field(struct list_head *head, const char *type,
111 const char *name, int offset, int size,
112 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -0500113{
114 struct ftrace_event_field *field;
115
Steven Rostedtd1a29142013-02-27 20:23:57 -0500116 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -0500117 if (!field)
Namhyung Kimaaf6ac02013-06-07 15:07:48 +0900118 return -ENOMEM;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100119
Steven Rostedt92edca02013-02-27 20:41:37 -0500120 field->name = name;
121 field->type = type;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100122
Li Zefan43b51ea2009-08-07 10:33:22 +0800123 if (filter_type == FILTER_OTHER)
124 field->filter_type = filter_assign_type(type);
125 else
126 field->filter_type = filter_type;
127
Tom Zanussicf027f62009-03-22 03:30:39 -0500128 field->offset = offset;
129 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -0500130 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +0800131
Steven Rostedt2e33af02010-04-22 10:35:55 -0400132 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -0500133
134 return 0;
Tom Zanussicf027f62009-03-22 03:30:39 -0500135}
Li Zefan8728fe52010-05-24 16:22:49 +0800136
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400137int trace_define_field(struct trace_event_call *call, const char *type,
Li Zefan8728fe52010-05-24 16:22:49 +0800138 const char *name, int offset, int size, int is_signed,
139 int filter_type)
140{
141 struct list_head *head;
142
143 if (WARN_ON(!call->class))
144 return 0;
145
146 head = trace_get_fields(call);
147 return __trace_define_field(head, type, name, offset, size,
148 is_signed, filter_type);
149}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400150EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500151
Daniel Wagner9f616682015-08-10 14:35:46 +0200152#define __generic_field(type, item, filter_type) \
153 ret = __trace_define_field(&ftrace_generic_fields, #type, \
154 #item, 0, 0, is_signed_type(type), \
155 filter_type); \
156 if (ret) \
157 return ret;
158
Li Zefane647d6b2009-08-19 15:54:32 +0800159#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800160 ret = __trace_define_field(&ftrace_common_fields, #type, \
161 "common_" #item, \
162 offsetof(typeof(ent), item), \
163 sizeof(ent.item), \
164 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800165 if (ret) \
166 return ret;
167
Daniel Wagner9f616682015-08-10 14:35:46 +0200168static int trace_define_generic_fields(void)
169{
170 int ret;
171
172 __generic_field(int, cpu, FILTER_OTHER);
173 __generic_field(char *, comm, FILTER_PTR_STRING);
174
175 return ret;
176}
177
Li Zefan8728fe52010-05-24 16:22:49 +0800178static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800179{
180 int ret;
181 struct trace_entry ent;
182
183 __common_field(unsigned short, type);
184 __common_field(unsigned char, flags);
185 __common_field(unsigned char, preempt_count);
186 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800187
188 return ret;
189}
190
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400191static void trace_destroy_fields(struct trace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800192{
193 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400194 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800195
Steven Rostedt2e33af02010-04-22 10:35:55 -0400196 head = trace_get_fields(call);
197 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800198 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500199 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800200 }
201}
202
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400203int trace_event_raw_init(struct trace_event_call *call)
Li Zefan87d9b4e2009-12-08 11:14:20 +0800204{
205 int id;
206
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400207 id = register_trace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800208 if (!id)
209 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800210
211 return 0;
212}
213EXPORT_SYMBOL_GPL(trace_event_raw_init);
214
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400215void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
216 struct trace_event_file *trace_file,
217 unsigned long len)
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400218{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400219 struct trace_event_call *event_call = trace_file->event_call;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400220
221 local_save_flags(fbuffer->flags);
222 fbuffer->pc = preempt_count();
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400223 fbuffer->trace_file = trace_file;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400224
225 fbuffer->event =
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400226 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400227 event_call->event.type, len,
228 fbuffer->flags, fbuffer->pc);
229 if (!fbuffer->event)
230 return NULL;
231
232 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
233 return fbuffer->entry;
234}
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400235EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400236
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500237static DEFINE_SPINLOCK(tracepoint_iter_lock);
238
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400239static void output_printk(struct trace_event_buffer *fbuffer)
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500240{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400241 struct trace_event_call *event_call;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500242 struct trace_event *event;
243 unsigned long flags;
244 struct trace_iterator *iter = tracepoint_print_iter;
245
246 if (!iter)
247 return;
248
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400249 event_call = fbuffer->trace_file->event_call;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500250 if (!event_call || !event_call->event.funcs ||
251 !event_call->event.funcs->trace)
252 return;
253
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400254 event = &fbuffer->trace_file->event_call->event;
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500255
256 spin_lock_irqsave(&tracepoint_iter_lock, flags);
257 trace_seq_init(&iter->seq);
258 iter->ent = fbuffer->entry;
259 event_call->event.funcs->trace(iter, 0, event);
260 trace_seq_putc(&iter->seq, 0);
261 printk("%s", iter->seq.buffer);
262
263 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
264}
265
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400266void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400267{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500268 if (tracepoint_printk)
269 output_printk(fbuffer);
270
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400271 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400272 fbuffer->event, fbuffer->entry,
273 fbuffer->flags, fbuffer->pc);
274}
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400275EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400276
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400277int trace_event_reg(struct trace_event_call *call,
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400278 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400279{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400280 struct trace_event_file *file = data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400281
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400282 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400283 switch (type) {
284 case TRACE_REG_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400285 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400286 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400287 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400288 case TRACE_REG_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400289 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400290 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400291 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400292 return 0;
293
294#ifdef CONFIG_PERF_EVENTS
295 case TRACE_REG_PERF_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400296 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400297 call->class->perf_probe,
298 call);
299 case TRACE_REG_PERF_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400300 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400301 call->class->perf_probe,
302 call);
303 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100304 case TRACE_REG_PERF_OPEN:
305 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100306 case TRACE_REG_PERF_ADD:
307 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100308 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400309#endif
310 }
311 return 0;
312}
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400313EXPORT_SYMBOL_GPL(trace_event_reg);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400314
Li Zefane870e9a2010-07-02 11:07:32 +0800315void trace_event_enable_cmd_record(bool enable)
316{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400317 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400318 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800319
320 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400321 do_for_each_event_file(tr, file) {
322
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400323 if (!(file->flags & EVENT_FILE_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800324 continue;
325
326 if (enable) {
327 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400328 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800329 } else {
330 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400331 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800332 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400333 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800334 mutex_unlock(&event_mutex);
335}
336
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400337static int __ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400338 int enable, int soft_disable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500339{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400340 struct trace_event_call *call = file->event_call;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400341 struct trace_array *tr = file->tr;
Li Zefan3b8e4272009-12-08 11:14:52 +0800342 int ret = 0;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400343 int disable;
Li Zefan3b8e4272009-12-08 11:14:52 +0800344
Steven Rostedtfd994982009-02-28 02:41:25 -0500345 switch (enable) {
346 case 0:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400347 /*
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900348 * When soft_disable is set and enable is cleared, the sm_ref
349 * reference counter is decremented. If it reaches 0, we want
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400350 * to clear the SOFT_DISABLED flag but leave the event in the
351 * state that it was. That is, if the event was enabled and
352 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
353 * is set we do not want the event to be enabled before we
354 * clear the bit.
355 *
356 * When soft_disable is not set but the SOFT_MODE flag is,
357 * we do nothing. Do not disable the tracepoint, otherwise
358 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
359 */
360 if (soft_disable) {
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900361 if (atomic_dec_return(&file->sm_ref) > 0)
362 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400363 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
364 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400365 } else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400366 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400367
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400368 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
369 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
370 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800371 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400372 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800373 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400374 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500375 }
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500376 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400377 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
378 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500379 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400380 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500381 break;
382 case 1:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400383 /*
384 * When soft_disable is set and enable is set, we want to
385 * register the tracepoint for the event, but leave the event
386 * as is. That means, if the event was already enabled, we do
387 * nothing (but set SOFT_MODE). If the event is disabled, we
388 * set SOFT_DISABLED before enabling the event tracepoint, so
389 * it still seems to be disabled.
390 */
391 if (!soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400392 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900393 else {
394 if (atomic_inc_return(&file->sm_ref) > 1)
395 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400396 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900397 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400398
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400399 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400400
401 /* Keep the event disabled, when going to SOFT_MODE. */
402 if (soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400403 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400404
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400405 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800406 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400407 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800408 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400409 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800410 if (ret) {
411 tracing_stop_cmdline_record();
412 pr_info("event trace: Could not enable event "
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400413 "%s\n", trace_event_name(call));
Li Zefan3b8e4272009-12-08 11:14:52 +0800414 break;
415 }
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400416 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -0500417
418 /* WAS_ENABLED gets set but never cleared. */
419 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
Steven Rostedtfd994982009-02-28 02:41:25 -0500420 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500421 break;
422 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800423
424 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500425}
426
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400427int trace_event_enable_disable(struct trace_event_file *file,
Tom Zanussi85f2b082013-10-24 08:59:24 -0500428 int enable, int soft_disable)
429{
430 return __ftrace_event_enable_disable(file, enable, soft_disable);
431}
432
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400433static int ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400434 int enable)
435{
436 return __ftrace_event_enable_disable(file, enable, 0);
437}
438
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400439static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800440{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400441 struct trace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800442
443 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400444 list_for_each_entry(file, &tr->events, list) {
445 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800446 }
447 mutex_unlock(&event_mutex);
448}
449
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400450static int cmp_pid(const void *key, const void *elt)
451{
452 const pid_t *search_pid = key;
453 const pid_t *pid = elt;
454
455 if (*search_pid == *pid)
456 return 0;
457 if (*search_pid < *pid)
458 return -1;
459 return 1;
460}
461
462static void __ftrace_clear_event_pids(struct trace_array *tr)
463{
464 struct trace_pid_list *pid_list;
465
466 pid_list = rcu_dereference_protected(tr->filtered_pids,
467 lockdep_is_held(&event_mutex));
468 if (!pid_list)
469 return;
470
471 rcu_assign_pointer(tr->filtered_pids, NULL);
472
473 /* Wait till all users are no longer using pid filtering */
474 synchronize_sched();
475
476 free_pages((unsigned long)pid_list->pids, pid_list->order);
477 kfree(pid_list);
478}
479
480static void ftrace_clear_event_pids(struct trace_array *tr)
481{
482 mutex_lock(&event_mutex);
483 __ftrace_clear_event_pids(tr);
484 mutex_unlock(&event_mutex);
485}
486
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400487static void __put_system(struct event_subsystem *system)
488{
489 struct event_filter *filter = system->filter;
490
Steven Rostedt6e94a782013-06-27 10:58:31 -0400491 WARN_ON_ONCE(system_refcount(system) == 0);
492 if (system_refcount_dec(system))
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400493 return;
494
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400495 list_del(&system->list);
496
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400497 if (filter) {
498 kfree(filter->filter_string);
499 kfree(filter);
500 }
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +0200501 kfree_const(system->name);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400502 kfree(system);
503}
504
505static void __get_system(struct event_subsystem *system)
506{
Steven Rostedt6e94a782013-06-27 10:58:31 -0400507 WARN_ON_ONCE(system_refcount(system) == 0);
508 system_refcount_inc(system);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400509}
510
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400511static void __get_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400512{
513 WARN_ON_ONCE(dir->ref_count == 0);
514 dir->ref_count++;
515 __get_system(dir->subsystem);
516}
517
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400518static void __put_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400519{
520 WARN_ON_ONCE(dir->ref_count == 0);
521 /* If the subsystem is about to be freed, the dir must be too */
Steven Rostedt6e94a782013-06-27 10:58:31 -0400522 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400523
524 __put_system(dir->subsystem);
525 if (!--dir->ref_count)
526 kfree(dir);
527}
528
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400529static void put_system(struct trace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400530{
531 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400532 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400533 mutex_unlock(&event_mutex);
534}
535
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400536static void remove_subsystem(struct trace_subsystem_dir *dir)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200537{
538 if (!dir)
539 return;
540
541 if (!--dir->nr_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500542 tracefs_remove_recursive(dir->entry);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200543 list_del(&dir->list);
544 __put_system_dir(dir);
545 }
546}
547
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400548static void remove_event_file_dir(struct trace_event_file *file)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200549{
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200550 struct dentry *dir = file->dir;
551 struct dentry *child;
552
553 if (dir) {
554 spin_lock(&dir->d_lock); /* probably unneeded */
Al Viro946e51f2014-10-26 19:19:16 -0400555 list_for_each_entry(child, &dir->d_subdirs, d_child) {
David Howells7682c912015-03-17 22:26:16 +0000556 if (d_really_is_positive(child)) /* probably unneeded */
557 d_inode(child)->i_private = NULL;
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200558 }
559 spin_unlock(&dir->d_lock);
560
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500561 tracefs_remove_recursive(dir);
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200562 }
563
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200564 list_del(&file->list);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200565 remove_subsystem(file->system);
Oleg Nesterov2448e342014-07-11 21:06:38 +0200566 free_event_filter(file->filter);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200567 kmem_cache_free(file_cachep, file);
568}
569
Li Zefan8f31bfe2009-05-08 10:31:42 +0800570/*
571 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
572 */
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400573static int
574__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
575 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500576{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400577 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400578 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400579 const char *name;
Steven Rostedt29f93942009-05-08 16:06:47 -0400580 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500581
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400582 list_for_each_entry(file, &tr->events, list) {
583
584 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400585 name = trace_event_name(call);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500586
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400587 if (!name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500588 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500589
Steven Rostedt9b637762012-05-10 15:55:43 -0400590 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
591 continue;
592
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500593 if (match &&
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400594 strcmp(match, name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400595 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500596 continue;
597
Steven Rostedt8f082012010-04-20 10:47:33 -0400598 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500599 continue;
600
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400601 if (event && strcmp(event, name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500602 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500603
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400604 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500605
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500606 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500607 }
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400608
609 return ret;
610}
611
612static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
613 const char *sub, const char *event, int set)
614{
615 int ret;
616
617 mutex_lock(&event_mutex);
618 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500619 mutex_unlock(&event_mutex);
620
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500621 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500622}
623
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400624static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800625{
626 char *event = NULL, *sub = NULL, *match;
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900627 int ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800628
629 /*
630 * The buf format can be <subsystem>:<event-name>
631 * *:<event-name> means any event by that name.
632 * :<event-name> is the same.
633 *
634 * <subsystem>:* means all events in that subsystem
635 * <subsystem>: means the same.
636 *
637 * <name> (no ':') means all events in a subsystem with
638 * the name <name> or any event that matches <name>
639 */
640
641 match = strsep(&buf, ":");
642 if (buf) {
643 sub = match;
644 event = buf;
645 match = NULL;
646
647 if (!strlen(sub) || strcmp(sub, "*") == 0)
648 sub = NULL;
649 if (!strlen(event) || strcmp(event, "*") == 0)
650 event = NULL;
651 }
652
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900653 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
654
655 /* Put back the colon to allow this to be called again */
656 if (buf)
657 *(buf - 1) = ':';
658
659 return ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800660}
661
Steven Rostedt4671c792009-05-08 16:27:41 -0400662/**
663 * trace_set_clr_event - enable or disable an event
664 * @system: system name to match (NULL for any system)
665 * @event: event name to match (NULL for all events, within system)
666 * @set: 1 to enable, 0 to disable
667 *
668 * This is a way for other parts of the kernel to enable or disable
669 * event recording.
670 *
671 * Returns 0 on success, -EINVAL if the parameters do not match any
672 * registered events.
673 */
674int trace_set_clr_event(const char *system, const char *event, int set)
675{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400676 struct trace_array *tr = top_trace_array();
677
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +0900678 if (!tr)
679 return -ENODEV;
680
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400681 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400682}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800683EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400684
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500685/* 128 should be much more than enough */
686#define EVENT_BUF_SIZE 127
687
688static ssize_t
689ftrace_event_write(struct file *file, const char __user *ubuf,
690 size_t cnt, loff_t *ppos)
691{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200692 struct trace_parser parser;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400693 struct seq_file *m = file->private_data;
694 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800695 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500696
Li Zefan4ba79782009-09-22 13:52:20 +0800697 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500698 return 0;
699
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400700 ret = tracing_update_buffers();
701 if (ret < 0)
702 return ret;
703
jolsa@redhat.com48966362009-09-11 17:29:28 +0200704 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500705 return -ENOMEM;
706
jolsa@redhat.com48966362009-09-11 17:29:28 +0200707 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500708
Li Zefan4ba79782009-09-22 13:52:20 +0800709 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200710 int set = 1;
711
712 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500713 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500714
jolsa@redhat.com48966362009-09-11 17:29:28 +0200715 parser.buffer[parser.idx] = 0;
716
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400717 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500718 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200719 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500720 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500721
722 ret = read;
723
jolsa@redhat.com48966362009-09-11 17:29:28 +0200724 out_put:
725 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500726
727 return ret;
728}
729
730static void *
731t_next(struct seq_file *m, void *v, loff_t *pos)
732{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400733 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400734 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400735 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500736
737 (*pos)++;
738
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400739 list_for_each_entry_continue(file, &tr->events, list) {
740 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400741 /*
742 * The ftrace subsystem is for showing formats only.
743 * They can not be enabled or disabled via the event files.
744 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400745 if (call->class && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400746 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400747 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500748
Li Zefan30bd39c2009-09-18 14:07:05 +0800749 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500750}
751
752static void *t_start(struct seq_file *m, loff_t *pos)
753{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400754 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400755 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800756 loff_t l;
757
Li Zefan20c89282009-05-06 10:33:45 +0800758 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800759
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400760 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800761 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400762 file = t_next(m, file, &l);
763 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800764 break;
765 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400766 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500767}
768
769static void *
770s_next(struct seq_file *m, void *v, loff_t *pos)
771{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400772 struct trace_event_file *file = v;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400773 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500774
775 (*pos)++;
776
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400777 list_for_each_entry_continue(file, &tr->events, list) {
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400778 if (file->flags & EVENT_FILE_FL_ENABLED)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400779 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500780 }
781
Li Zefan30bd39c2009-09-18 14:07:05 +0800782 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500783}
784
785static void *s_start(struct seq_file *m, loff_t *pos)
786{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400787 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400788 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800789 loff_t l;
790
Li Zefan20c89282009-05-06 10:33:45 +0800791 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800792
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400793 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800794 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400795 file = s_next(m, file, &l);
796 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800797 break;
798 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400799 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500800}
801
802static int t_show(struct seq_file *m, void *v)
803{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400804 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400805 struct trace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500806
Steven Rostedt8f082012010-04-20 10:47:33 -0400807 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
808 seq_printf(m, "%s:", call->class->system);
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400809 seq_printf(m, "%s\n", trace_event_name(call));
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500810
811 return 0;
812}
813
814static void t_stop(struct seq_file *m, void *p)
815{
Li Zefan20c89282009-05-06 10:33:45 +0800816 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500817}
818
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400819static void *p_start(struct seq_file *m, loff_t *pos)
820{
821 struct trace_pid_list *pid_list;
822 struct trace_array *tr = m->private;
823
824 /*
825 * Grab the mutex, to keep calls to p_next() having the same
826 * tr->filtered_pids as p_start() has.
827 * If we just passed the tr->filtered_pids around, then RCU would
828 * have been enough, but doing that makes things more complex.
829 */
830 mutex_lock(&event_mutex);
831 rcu_read_lock_sched();
832
833 pid_list = rcu_dereference_sched(tr->filtered_pids);
834
835 if (!pid_list || *pos >= pid_list->nr_pids)
836 return NULL;
837
838 return (void *)&pid_list->pids[*pos];
839}
840
841static void p_stop(struct seq_file *m, void *p)
842{
843 rcu_read_unlock_sched();
844 mutex_unlock(&event_mutex);
845}
846
847static void *
848p_next(struct seq_file *m, void *v, loff_t *pos)
849{
850 struct trace_array *tr = m->private;
851 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
852
853 (*pos)++;
854
855 if (*pos >= pid_list->nr_pids)
856 return NULL;
857
858 return (void *)&pid_list->pids[*pos];
859}
860
861static int p_show(struct seq_file *m, void *v)
862{
863 pid_t *pid = v;
864
865 seq_printf(m, "%d\n", *pid);
866 return 0;
867}
868
Steven Rostedt1473e442009-02-24 14:15:08 -0500869static ssize_t
870event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
871 loff_t *ppos)
872{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400873 struct trace_event_file *file;
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200874 unsigned long flags;
Tom Zanussia4390592013-06-29 00:08:04 -0500875 char buf[4] = "0";
Steven Rostedt1473e442009-02-24 14:15:08 -0500876
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200877 mutex_lock(&event_mutex);
878 file = event_file_data(filp);
879 if (likely(file))
880 flags = file->flags;
881 mutex_unlock(&event_mutex);
882
883 if (!file)
884 return -ENODEV;
885
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400886 if (flags & EVENT_FILE_FL_ENABLED &&
887 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussia4390592013-06-29 00:08:04 -0500888 strcpy(buf, "1");
889
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400890 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
891 flags & EVENT_FILE_FL_SOFT_MODE)
Tom Zanussia4390592013-06-29 00:08:04 -0500892 strcat(buf, "*");
893
894 strcat(buf, "\n");
Steven Rostedt1473e442009-02-24 14:15:08 -0500895
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400896 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -0500897}
898
899static ssize_t
900event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
901 loff_t *ppos)
902{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400903 struct trace_event_file *file;
Steven Rostedt1473e442009-02-24 14:15:08 -0500904 unsigned long val;
905 int ret;
906
Peter Huewe22fe9b52011-06-07 21:58:27 +0200907 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
908 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -0500909 return ret;
910
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400911 ret = tracing_update_buffers();
912 if (ret < 0)
913 return ret;
914
Steven Rostedt1473e442009-02-24 14:15:08 -0500915 switch (val) {
916 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500917 case 1:
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200918 ret = -ENODEV;
Steven Rostedt11a241a2009-03-02 11:49:04 -0500919 mutex_lock(&event_mutex);
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200920 file = event_file_data(filp);
921 if (likely(file))
922 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500923 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500924 break;
925
926 default:
927 return -EINVAL;
928 }
929
930 *ppos += cnt;
931
Li Zefan3b8e4272009-12-08 11:14:52 +0800932 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500933}
934
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400935static ssize_t
936system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
937 loff_t *ppos)
938{
Li Zefanc142b152009-05-08 10:32:05 +0800939 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400940 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400941 struct event_subsystem *system = dir->subsystem;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400942 struct trace_event_call *call;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400943 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400944 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400945 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800946 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400947 int ret;
948
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400949 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400950 list_for_each_entry(file, &tr->events, list) {
951 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400952 if (!trace_event_name(call) || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400953 continue;
954
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400955 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400956 continue;
957
958 /*
959 * We need to find out if all the events are set
960 * or if all events or cleared, or if we have
961 * a mixture.
962 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400963 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +0800964
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400965 /*
966 * If we have a mixture, no need to look further.
967 */
Li Zefanc142b152009-05-08 10:32:05 +0800968 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400969 break;
970 }
971 mutex_unlock(&event_mutex);
972
Li Zefanc142b152009-05-08 10:32:05 +0800973 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400974 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400975
976 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
977
978 return ret;
979}
980
981static ssize_t
982system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
983 loff_t *ppos)
984{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400985 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400986 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400987 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400988 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400989 ssize_t ret;
990
Peter Huewe22fe9b52011-06-07 21:58:27 +0200991 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
992 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400993 return ret;
994
995 ret = tracing_update_buffers();
996 if (ret < 0)
997 return ret;
998
Li Zefan8f31bfe2009-05-08 10:31:42 +0800999 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001000 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001001
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001002 /*
1003 * Opening of "enable" adds a ref count to system,
1004 * so the name is safe to use.
1005 */
1006 if (system)
1007 name = system->name;
1008
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001009 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001010 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +08001011 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001012
1013 ret = cnt;
1014
Li Zefan8f31bfe2009-05-08 10:31:42 +08001015out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001016 *ppos += cnt;
1017
1018 return ret;
1019}
1020
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001021enum {
1022 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +08001023 FORMAT_FIELD_SEPERATOR = 2,
1024 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001025};
1026
1027static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -05001028{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001029 struct trace_event_call *call = event_file_data(m->private);
Li Zefan86397dc2010-08-17 13:53:06 +08001030 struct list_head *common_head = &ftrace_common_fields;
1031 struct list_head *head = trace_get_fields(call);
Oleg Nesterov7710b632013-07-18 20:47:10 +02001032 struct list_head *node = v;
Steven Rostedt981d0812009-03-02 13:53:59 -05001033
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001034 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001035
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001036 switch ((unsigned long)v) {
1037 case FORMAT_HEADER:
Oleg Nesterov7710b632013-07-18 20:47:10 +02001038 node = common_head;
1039 break;
Li Zefan86397dc2010-08-17 13:53:06 +08001040
1041 case FORMAT_FIELD_SEPERATOR:
Oleg Nesterov7710b632013-07-18 20:47:10 +02001042 node = head;
1043 break;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001044
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001045 case FORMAT_PRINTFMT:
1046 /* all done */
1047 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001048 }
1049
Oleg Nesterov7710b632013-07-18 20:47:10 +02001050 node = node->prev;
1051 if (node == common_head)
Li Zefan86397dc2010-08-17 13:53:06 +08001052 return (void *)FORMAT_FIELD_SEPERATOR;
Oleg Nesterov7710b632013-07-18 20:47:10 +02001053 else if (node == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001054 return (void *)FORMAT_PRINTFMT;
Oleg Nesterov7710b632013-07-18 20:47:10 +02001055 else
1056 return node;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001057}
1058
1059static int f_show(struct seq_file *m, void *v)
1060{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001061 struct trace_event_call *call = event_file_data(m->private);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001062 struct ftrace_event_field *field;
1063 const char *array_descriptor;
1064
1065 switch ((unsigned long)v) {
1066 case FORMAT_HEADER:
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001067 seq_printf(m, "name: %s\n", trace_event_name(call));
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001068 seq_printf(m, "ID: %d\n", call->event.type);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001069 seq_puts(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +08001070 return 0;
1071
Li Zefan86397dc2010-08-17 13:53:06 +08001072 case FORMAT_FIELD_SEPERATOR:
1073 seq_putc(m, '\n');
1074 return 0;
1075
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001076 case FORMAT_PRINTFMT:
1077 seq_printf(m, "\nprint fmt: %s\n",
1078 call->print_fmt);
1079 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001080 }
1081
Oleg Nesterov7710b632013-07-18 20:47:10 +02001082 field = list_entry(v, struct ftrace_event_field, link);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001083 /*
1084 * Smartly shows the array type(except dynamic array).
1085 * Normal:
1086 * field:TYPE VAR
1087 * If TYPE := TYPE[LEN], it is shown:
1088 * field:TYPE VAR[LEN]
1089 */
1090 array_descriptor = strchr(field->type, '[');
1091
1092 if (!strncmp(field->type, "__data_loc", 10))
1093 array_descriptor = NULL;
1094
1095 if (!array_descriptor)
1096 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1097 field->type, field->name, field->offset,
1098 field->size, !!field->is_signed);
1099 else
1100 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1101 (int)(array_descriptor - field->type),
1102 field->type, field->name,
1103 array_descriptor, field->offset,
1104 field->size, !!field->is_signed);
1105
1106 return 0;
1107}
1108
Oleg Nesterov7710b632013-07-18 20:47:10 +02001109static void *f_start(struct seq_file *m, loff_t *pos)
1110{
1111 void *p = (void *)FORMAT_HEADER;
1112 loff_t l = 0;
1113
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001114 /* ->stop() is called even if ->start() fails */
1115 mutex_lock(&event_mutex);
1116 if (!event_file_data(m->private))
1117 return ERR_PTR(-ENODEV);
1118
Oleg Nesterov7710b632013-07-18 20:47:10 +02001119 while (l < *pos && p)
1120 p = f_next(m, p, &l);
1121
1122 return p;
1123}
1124
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001125static void f_stop(struct seq_file *m, void *p)
1126{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001127 mutex_unlock(&event_mutex);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001128}
1129
1130static const struct seq_operations trace_format_seq_ops = {
1131 .start = f_start,
1132 .next = f_next,
1133 .stop = f_stop,
1134 .show = f_show,
1135};
1136
1137static int trace_format_open(struct inode *inode, struct file *file)
1138{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001139 struct seq_file *m;
1140 int ret;
1141
1142 ret = seq_open(file, &trace_format_seq_ops);
1143 if (ret < 0)
1144 return ret;
1145
1146 m = file->private_data;
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001147 m->private = file;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001148
1149 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001150}
1151
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001152static ssize_t
1153event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1154{
Oleg Nesterov1a111262013-07-26 19:25:32 +02001155 int id = (long)event_file_data(filp);
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001156 char buf[32];
1157 int len;
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001158
1159 if (*ppos)
1160 return 0;
1161
Oleg Nesterov1a111262013-07-26 19:25:32 +02001162 if (unlikely(!id))
1163 return -ENODEV;
1164
1165 len = sprintf(buf, "%d\n", id);
1166
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001167 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001168}
1169
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001170static ssize_t
1171event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1172 loff_t *ppos)
1173{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001174 struct trace_event_file *file;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001175 struct trace_seq *s;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001176 int r = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001177
1178 if (*ppos)
1179 return 0;
1180
1181 s = kmalloc(sizeof(*s), GFP_KERNEL);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001182
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001183 if (!s)
1184 return -ENOMEM;
1185
1186 trace_seq_init(s);
1187
Oleg Nesterove2912b02013-07-26 19:25:40 +02001188 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001189 file = event_file_data(filp);
1190 if (file)
1191 print_event_filter(file, s);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001192 mutex_unlock(&event_mutex);
1193
Tom Zanussif306cc82013-10-24 08:34:17 -05001194 if (file)
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001195 r = simple_read_from_buffer(ubuf, cnt, ppos,
1196 s->buffer, trace_seq_used(s));
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001197
1198 kfree(s);
1199
1200 return r;
1201}
1202
1203static ssize_t
1204event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1205 loff_t *ppos)
1206{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001207 struct trace_event_file *file;
Tom Zanussi8b372562009-04-28 03:04:59 -05001208 char *buf;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001209 int err = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001210
Tom Zanussi8b372562009-04-28 03:04:59 -05001211 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001212 return -EINVAL;
1213
Tom Zanussi8b372562009-04-28 03:04:59 -05001214 buf = (char *)__get_free_page(GFP_TEMPORARY);
1215 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001216 return -ENOMEM;
1217
Tom Zanussi8b372562009-04-28 03:04:59 -05001218 if (copy_from_user(buf, ubuf, cnt)) {
1219 free_page((unsigned long) buf);
1220 return -EFAULT;
1221 }
1222 buf[cnt] = '\0';
1223
Oleg Nesterove2912b02013-07-26 19:25:40 +02001224 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001225 file = event_file_data(filp);
1226 if (file)
1227 err = apply_event_filter(file, buf);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001228 mutex_unlock(&event_mutex);
1229
Tom Zanussi8b372562009-04-28 03:04:59 -05001230 free_page((unsigned long) buf);
1231 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001232 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -05001233
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001234 *ppos += cnt;
1235
1236 return cnt;
1237}
1238
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001239static LIST_HEAD(event_subsystems);
1240
1241static int subsystem_open(struct inode *inode, struct file *filp)
1242{
1243 struct event_subsystem *system = NULL;
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001244 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001245 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001246 int ret;
1247
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001248 if (tracing_is_disabled())
1249 return -ENODEV;
1250
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001251 /* Make sure the system still exists */
Alexander Z Lama8227412013-07-01 19:37:54 -07001252 mutex_lock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001253 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001254 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1255 list_for_each_entry(dir, &tr->systems, list) {
1256 if (dir == inode->i_private) {
1257 /* Don't open systems with no events */
1258 if (dir->nr_events) {
1259 __get_system_dir(dir);
1260 system = dir->subsystem;
1261 }
1262 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001263 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001264 }
1265 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001266 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001267 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001268 mutex_unlock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001269
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001270 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001271 return -ENODEV;
1272
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001273 /* Some versions of gcc think dir can be uninitialized here */
1274 WARN_ON(!dir);
1275
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001276 /* Still need to increment the ref count of the system */
1277 if (trace_array_get(tr) < 0) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001278 put_system(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001279 return -ENODEV;
1280 }
1281
1282 ret = tracing_open_generic(inode, filp);
1283 if (ret < 0) {
1284 trace_array_put(tr);
1285 put_system(dir);
1286 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001287
1288 return ret;
1289}
1290
1291static int system_tr_open(struct inode *inode, struct file *filp)
1292{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001293 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001294 struct trace_array *tr = inode->i_private;
1295 int ret;
1296
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001297 if (tracing_is_disabled())
1298 return -ENODEV;
1299
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001300 if (trace_array_get(tr) < 0)
1301 return -ENODEV;
1302
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001303 /* Make a temporary dir that has no system but points to tr */
1304 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001305 if (!dir) {
1306 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001307 return -ENOMEM;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001308 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001309
1310 dir->tr = tr;
1311
1312 ret = tracing_open_generic(inode, filp);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001313 if (ret < 0) {
1314 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001315 kfree(dir);
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001316 return ret;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001317 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001318
1319 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001320
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001321 return 0;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001322}
1323
1324static int subsystem_release(struct inode *inode, struct file *file)
1325{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001326 struct trace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001327
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001328 trace_array_put(dir->tr);
1329
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001330 /*
1331 * If dir->subsystem is NULL, then this is a temporary
1332 * descriptor that was made for a trace_array to enable
1333 * all subsystems.
1334 */
1335 if (dir->subsystem)
1336 put_system(dir);
1337 else
1338 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001339
1340 return 0;
1341}
1342
Tom Zanussicfb180f2009-03-22 03:31:17 -05001343static ssize_t
1344subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1345 loff_t *ppos)
1346{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001347 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001348 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001349 struct trace_seq *s;
1350 int r;
1351
1352 if (*ppos)
1353 return 0;
1354
1355 s = kmalloc(sizeof(*s), GFP_KERNEL);
1356 if (!s)
1357 return -ENOMEM;
1358
1359 trace_seq_init(s);
1360
Tom Zanussi8b372562009-04-28 03:04:59 -05001361 print_subsystem_event_filter(system, s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001362 r = simple_read_from_buffer(ubuf, cnt, ppos,
1363 s->buffer, trace_seq_used(s));
Tom Zanussicfb180f2009-03-22 03:31:17 -05001364
1365 kfree(s);
1366
1367 return r;
1368}
1369
1370static ssize_t
1371subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1372 loff_t *ppos)
1373{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001374 struct trace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001375 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001376 int err;
1377
Tom Zanussi8b372562009-04-28 03:04:59 -05001378 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001379 return -EINVAL;
1380
Tom Zanussi8b372562009-04-28 03:04:59 -05001381 buf = (char *)__get_free_page(GFP_TEMPORARY);
1382 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001383 return -ENOMEM;
1384
Tom Zanussi8b372562009-04-28 03:04:59 -05001385 if (copy_from_user(buf, ubuf, cnt)) {
1386 free_page((unsigned long) buf);
1387 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001388 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001389 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001390
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001391 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001392 free_page((unsigned long) buf);
1393 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001394 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001395
1396 *ppos += cnt;
1397
1398 return cnt;
1399}
1400
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001401static ssize_t
1402show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1403{
1404 int (*func)(struct trace_seq *s) = filp->private_data;
1405 struct trace_seq *s;
1406 int r;
1407
1408 if (*ppos)
1409 return 0;
1410
1411 s = kmalloc(sizeof(*s), GFP_KERNEL);
1412 if (!s)
1413 return -ENOMEM;
1414
1415 trace_seq_init(s);
1416
1417 func(s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001418 r = simple_read_from_buffer(ubuf, cnt, ppos,
1419 s->buffer, trace_seq_used(s));
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001420
1421 kfree(s);
1422
1423 return r;
1424}
1425
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001426static int max_pids(struct trace_pid_list *pid_list)
1427{
1428 return (PAGE_SIZE << pid_list->order) / sizeof(pid_t);
1429}
1430
1431static ssize_t
1432ftrace_event_pid_write(struct file *file, const char __user *ubuf,
1433 size_t cnt, loff_t *ppos)
1434{
1435 struct seq_file *m = file->private_data;
1436 struct trace_array *tr = m->private;
1437 struct trace_pid_list *filtered_pids = NULL;
1438 struct trace_pid_list *pid_list = NULL;
1439 struct trace_parser parser;
1440 unsigned long val;
1441 loff_t this_pos;
1442 ssize_t read = 0;
1443 ssize_t ret = 0;
1444 pid_t pid;
1445 int i;
1446
1447 if (!cnt)
1448 return 0;
1449
1450 ret = tracing_update_buffers();
1451 if (ret < 0)
1452 return ret;
1453
1454 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1455 return -ENOMEM;
1456
1457 mutex_lock(&event_mutex);
1458 /*
1459 * Load as many pids into the array before doing a
1460 * swap from the tr->filtered_pids to the new list.
1461 */
1462 while (cnt > 0) {
1463
1464 this_pos = 0;
1465
1466 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1467 if (ret < 0 || !trace_parser_loaded(&parser))
1468 break;
1469
1470 read += ret;
1471 ubuf += ret;
1472 cnt -= ret;
1473
1474 parser.buffer[parser.idx] = 0;
1475
1476 ret = -EINVAL;
1477 if (kstrtoul(parser.buffer, 0, &val))
1478 break;
1479 if (val > INT_MAX)
1480 break;
1481
1482 pid = (pid_t)val;
1483
1484 ret = -ENOMEM;
1485 if (!pid_list) {
1486 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1487 if (!pid_list)
1488 break;
1489
1490 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1491 lockdep_is_held(&event_mutex));
1492 if (filtered_pids)
1493 pid_list->order = filtered_pids->order;
1494 else
1495 pid_list->order = 0;
1496
1497 pid_list->pids = (void *)__get_free_pages(GFP_KERNEL,
1498 pid_list->order);
1499 if (!pid_list->pids)
1500 break;
1501
1502 if (filtered_pids) {
1503 pid_list->nr_pids = filtered_pids->nr_pids;
1504 memcpy(pid_list->pids, filtered_pids->pids,
1505 pid_list->nr_pids * sizeof(pid_t));
1506 } else
1507 pid_list->nr_pids = 0;
1508 }
1509
1510 if (pid_list->nr_pids >= max_pids(pid_list)) {
1511 pid_t *pid_page;
1512
1513 pid_page = (void *)__get_free_pages(GFP_KERNEL,
1514 pid_list->order + 1);
1515 if (!pid_page)
1516 break;
1517 memcpy(pid_page, pid_list->pids,
1518 pid_list->nr_pids * sizeof(pid_t));
1519 free_pages((unsigned long)pid_list->pids, pid_list->order);
1520
1521 pid_list->order++;
1522 pid_list->pids = pid_page;
1523 }
1524
1525 pid_list->pids[pid_list->nr_pids++] = pid;
1526 trace_parser_clear(&parser);
1527 ret = 0;
1528 }
1529 trace_parser_put(&parser);
1530
1531 if (ret < 0) {
1532 if (pid_list)
1533 free_pages((unsigned long)pid_list->pids, pid_list->order);
1534 kfree(pid_list);
1535 mutex_unlock(&event_mutex);
1536 return ret;
1537 }
1538
1539 if (!pid_list) {
1540 mutex_unlock(&event_mutex);
1541 return ret;
1542 }
1543
1544 sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL);
1545
1546 /* Remove duplicates */
1547 for (i = 1; i < pid_list->nr_pids; i++) {
1548 int start = i;
1549
1550 while (i < pid_list->nr_pids &&
1551 pid_list->pids[i - 1] == pid_list->pids[i])
1552 i++;
1553
1554 if (start != i) {
1555 if (i < pid_list->nr_pids) {
1556 memmove(&pid_list->pids[start], &pid_list->pids[i],
1557 (pid_list->nr_pids - i) * sizeof(pid_t));
1558 pid_list->nr_pids -= i - start;
1559 i = start;
1560 } else
1561 pid_list->nr_pids = start;
1562 }
1563 }
1564
1565 rcu_assign_pointer(tr->filtered_pids, pid_list);
1566
1567 mutex_unlock(&event_mutex);
1568
1569 if (filtered_pids) {
1570 synchronize_sched();
1571
1572 free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
1573 kfree(filtered_pids);
1574 }
1575
1576 ret = read;
1577 *ppos += read;
1578
1579 return ret;
1580}
1581
Steven Rostedt15075ca2012-05-03 14:57:28 -04001582static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1583static int ftrace_event_set_open(struct inode *inode, struct file *file);
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001584static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001585static int ftrace_event_release(struct inode *inode, struct file *file);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001586
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001587static const struct seq_operations show_event_seq_ops = {
1588 .start = t_start,
1589 .next = t_next,
1590 .show = t_show,
1591 .stop = t_stop,
1592};
1593
1594static const struct seq_operations show_set_event_seq_ops = {
1595 .start = s_start,
1596 .next = s_next,
1597 .show = t_show,
1598 .stop = t_stop,
1599};
1600
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001601static const struct seq_operations show_set_pid_seq_ops = {
1602 .start = p_start,
1603 .next = p_next,
1604 .show = p_show,
1605 .stop = p_stop,
1606};
1607
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001608static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001609 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001610 .read = seq_read,
1611 .llseek = seq_lseek,
1612 .release = seq_release,
1613};
1614
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001615static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001616 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001617 .read = seq_read,
1618 .write = ftrace_event_write,
1619 .llseek = seq_lseek,
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001620 .release = ftrace_event_release,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001621};
1622
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001623static const struct file_operations ftrace_set_event_pid_fops = {
1624 .open = ftrace_event_set_pid_open,
1625 .read = seq_read,
1626 .write = ftrace_event_pid_write,
1627 .llseek = seq_lseek,
1628 .release = ftrace_event_release,
1629};
1630
Steven Rostedt1473e442009-02-24 14:15:08 -05001631static const struct file_operations ftrace_enable_fops = {
Oleg Nesterovbf682c32013-07-28 20:35:27 +02001632 .open = tracing_open_generic,
Steven Rostedt1473e442009-02-24 14:15:08 -05001633 .read = event_enable_read,
1634 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001635 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001636};
1637
Steven Rostedt981d0812009-03-02 13:53:59 -05001638static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001639 .open = trace_format_open,
1640 .read = seq_read,
1641 .llseek = seq_lseek,
1642 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001643};
1644
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001645static const struct file_operations ftrace_event_id_fops = {
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001646 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001647 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001648};
1649
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001650static const struct file_operations ftrace_event_filter_fops = {
1651 .open = tracing_open_generic,
1652 .read = event_filter_read,
1653 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001654 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001655};
1656
Tom Zanussicfb180f2009-03-22 03:31:17 -05001657static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001658 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001659 .read = subsystem_filter_read,
1660 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001661 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001662 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001663};
1664
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001665static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001666 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001667 .read = system_enable_read,
1668 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001669 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001670 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001671};
1672
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001673static const struct file_operations ftrace_tr_enable_fops = {
1674 .open = system_tr_open,
1675 .read = system_enable_read,
1676 .write = system_enable_write,
1677 .llseek = default_llseek,
1678 .release = subsystem_release,
1679};
1680
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001681static const struct file_operations ftrace_show_header_fops = {
1682 .open = tracing_open_generic,
1683 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001684 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001685};
1686
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001687static int
1688ftrace_event_open(struct inode *inode, struct file *file,
1689 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001690{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001691 struct seq_file *m;
1692 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001693
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001694 ret = seq_open(file, seq_ops);
1695 if (ret < 0)
1696 return ret;
1697 m = file->private_data;
1698 /* copy tr over to seq ops */
1699 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001700
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001701 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001702}
1703
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001704static int ftrace_event_release(struct inode *inode, struct file *file)
1705{
1706 struct trace_array *tr = inode->i_private;
1707
1708 trace_array_put(tr);
1709
1710 return seq_release(inode, file);
1711}
1712
Steven Rostedt15075ca2012-05-03 14:57:28 -04001713static int
1714ftrace_event_avail_open(struct inode *inode, struct file *file)
1715{
1716 const struct seq_operations *seq_ops = &show_event_seq_ops;
1717
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001718 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001719}
1720
1721static int
1722ftrace_event_set_open(struct inode *inode, struct file *file)
1723{
1724 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001725 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001726 int ret;
1727
1728 if (trace_array_get(tr) < 0)
1729 return -ENODEV;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001730
1731 if ((file->f_mode & FMODE_WRITE) &&
1732 (file->f_flags & O_TRUNC))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001733 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001734
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001735 ret = ftrace_event_open(inode, file, seq_ops);
1736 if (ret < 0)
1737 trace_array_put(tr);
1738 return ret;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001739}
1740
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001741static int
1742ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1743{
1744 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1745 struct trace_array *tr = inode->i_private;
1746 int ret;
1747
1748 if (trace_array_get(tr) < 0)
1749 return -ENODEV;
1750
1751 if ((file->f_mode & FMODE_WRITE) &&
1752 (file->f_flags & O_TRUNC))
1753 ftrace_clear_event_pids(tr);
1754
1755 ret = ftrace_event_open(inode, file, seq_ops);
1756 if (ret < 0)
1757 trace_array_put(tr);
1758 return ret;
1759}
1760
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001761static struct event_subsystem *
1762create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001763{
1764 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001765
1766 /* need to create new entry */
1767 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001768 if (!system)
1769 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001770
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001771 system->ref_count = 1;
Steven Rostedt6e94a782013-06-27 10:58:31 -04001772
1773 /* Only allocate if dynamic (kprobes and modules) */
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001774 system->name = kstrdup_const(name, GFP_KERNEL);
1775 if (!system->name)
1776 goto out_free;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001777
Tom Zanussi30e673b2009-04-28 03:04:47 -05001778 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001779
Tom Zanussi8b372562009-04-28 03:04:59 -05001780 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001781 if (!system->filter)
1782 goto out_free;
1783
1784 list_add(&system->list, &event_subsystems);
1785
1786 return system;
1787
1788 out_free:
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001789 kfree_const(system->name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001790 kfree(system);
1791 return NULL;
1792}
1793
1794static struct dentry *
1795event_subsystem_dir(struct trace_array *tr, const char *name,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001796 struct trace_event_file *file, struct dentry *parent)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001797{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001798 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001799 struct event_subsystem *system;
1800 struct dentry *entry;
1801
1802 /* First see if we did not already create this dir */
1803 list_for_each_entry(dir, &tr->systems, list) {
1804 system = dir->subsystem;
1805 if (strcmp(system->name, name) == 0) {
1806 dir->nr_events++;
1807 file->system = dir;
1808 return dir->entry;
1809 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001810 }
1811
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001812 /* Now see if the system itself exists. */
1813 list_for_each_entry(system, &event_subsystems, list) {
1814 if (strcmp(system->name, name) == 0)
1815 break;
1816 }
1817 /* Reset system variable when not found */
1818 if (&system->list == &event_subsystems)
1819 system = NULL;
1820
1821 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1822 if (!dir)
1823 goto out_fail;
1824
1825 if (!system) {
1826 system = create_new_subsystem(name);
1827 if (!system)
1828 goto out_free;
1829 } else
1830 __get_system(system);
1831
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001832 dir->entry = tracefs_create_dir(name, parent);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001833 if (!dir->entry) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001834 pr_warn("Failed to create system directory %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001835 __put_system(system);
1836 goto out_free;
1837 }
1838
1839 dir->tr = tr;
1840 dir->ref_count = 1;
1841 dir->nr_events = 1;
1842 dir->subsystem = system;
1843 file->system = dir;
1844
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001845 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001846 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001847 if (!entry) {
1848 kfree(system->filter);
1849 system->filter = NULL;
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001850 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001851 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001852
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001853 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001854 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001855
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001856 list_add(&dir->list, &tr->systems);
1857
1858 return dir->entry;
1859
1860 out_free:
1861 kfree(dir);
1862 out_fail:
1863 /* Only print this message if failed on memory allocation */
1864 if (!dir || !system)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001865 pr_warn("No memory to create event subsystem %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001866 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001867}
1868
Steven Rostedt1473e442009-02-24 14:15:08 -05001869static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001870event_create_dir(struct dentry *parent, struct trace_event_file *file)
Steven Rostedt1473e442009-02-24 14:15:08 -05001871{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001872 struct trace_event_call *call = file->event_call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001873 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001874 struct list_head *head;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001875 struct dentry *d_events;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001876 const char *name;
Steven Rostedtfd994982009-02-28 02:41:25 -05001877 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001878
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001879 /*
1880 * If the trace point header did not define TRACE_SYSTEM
1881 * then the system would be called "TRACE_SYSTEM".
1882 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001883 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1884 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1885 if (!d_events)
1886 return -ENOMEM;
1887 } else
1888 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001889
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001890 name = trace_event_name(call);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001891 file->dir = tracefs_create_dir(name, d_events);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001892 if (!file->dir) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001893 pr_warn("Could not create tracefs '%s' directory\n", name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001894 return -1;
1895 }
1896
Steven Rostedt9b637762012-05-10 15:55:43 -04001897 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001898 trace_create_file("enable", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001899 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001900
Steven Rostedt22392912010-04-21 12:27:06 -04001901#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001902 if (call->event.type && call->class->reg)
Oleg Nesterov1a111262013-07-26 19:25:32 +02001903 trace_create_file("id", 0444, file->dir,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001904 (void *)(long)call->event.type,
1905 &ftrace_event_id_fops);
Steven Rostedt22392912010-04-21 12:27:06 -04001906#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001907
Li Zefanc9d932c2010-05-24 16:24:28 +08001908 /*
1909 * Other events may have the same class. Only update
1910 * the fields if they are not already defined.
1911 */
1912 head = trace_get_fields(call);
1913 if (list_empty(head)) {
1914 ret = call->class->define_fields(call);
1915 if (ret < 0) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001916 pr_warn("Could not initialize trace point events/%s\n",
1917 name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001918 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001919 }
1920 }
Tom Zanussif306cc82013-10-24 08:34:17 -05001921 trace_create_file("filter", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001922 &ftrace_event_filter_fops);
Tom Zanussicf027f62009-03-22 03:30:39 -05001923
Tom Zanussi85f2b082013-10-24 08:59:24 -05001924 trace_create_file("trigger", 0644, file->dir, file,
1925 &event_trigger_fops);
1926
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001927 trace_create_file("format", 0444, file->dir, call,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001928 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -05001929
Steven Rostedt1473e442009-02-24 14:15:08 -05001930 return 0;
1931}
1932
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001933static void remove_event_from_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001934{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001935 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001936 struct trace_array *tr;
1937
1938 do_for_each_event_file_safe(tr, file) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001939 if (file->event_call != call)
1940 continue;
1941
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02001942 remove_event_file_dir(file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001943 /*
1944 * The do_for_each_event_file_safe() is
1945 * a double loop. After finding the call for this
1946 * trace_array, we use break to jump to the next
1947 * trace_array.
1948 */
1949 break;
1950 } while_for_each_event_file();
1951}
1952
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001953static void event_remove(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03001954{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001955 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001956 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001957
1958 do_for_each_event_file(tr, file) {
1959 if (file->event_call != call)
1960 continue;
1961 ftrace_event_enable_disable(file, 0);
1962 /*
1963 * The do_for_each_event_file() is
1964 * a double loop. After finding the call for this
1965 * trace_array, we use break to jump to the next
1966 * trace_array.
1967 */
1968 break;
1969 } while_for_each_event_file();
1970
Ezequiel Garcia87819152012-09-12 11:47:57 -03001971 if (call->event.funcs)
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001972 __unregister_trace_event(&call->event);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001973 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001974 list_del(&call->list);
1975}
1976
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001977static int event_init(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03001978{
1979 int ret = 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001980 const char *name;
Ezequiel Garcia87819152012-09-12 11:47:57 -03001981
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001982 name = trace_event_name(call);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001983 if (WARN_ON(!name))
Ezequiel Garcia87819152012-09-12 11:47:57 -03001984 return -EINVAL;
1985
1986 if (call->class->raw_init) {
1987 ret = call->class->raw_init(call);
1988 if (ret < 0 && ret != -ENOSYS)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001989 pr_warn("Could not initialize trace events/%s\n", name);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001990 }
1991
1992 return ret;
1993}
1994
Li Zefan67ead0a2010-05-24 16:25:13 +08001995static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001996__register_event(struct trace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001997{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001998 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001999
Ezequiel Garcia87819152012-09-12 11:47:57 -03002000 ret = event_init(call);
2001 if (ret < 0)
2002 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04002003
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002004 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08002005 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07002006
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002007 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002008}
2009
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002010static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2011{
2012 int rlen;
2013 int elen;
2014
2015 /* Find the length of the enum value as a string */
2016 elen = snprintf(ptr, 0, "%ld", map->enum_value);
2017 /* Make sure there's enough room to replace the string with the value */
2018 if (len < elen)
2019 return NULL;
2020
2021 snprintf(ptr, elen + 1, "%ld", map->enum_value);
2022
2023 /* Get the rest of the string of ptr */
2024 rlen = strlen(ptr + len);
2025 memmove(ptr + elen, ptr + len, rlen);
2026 /* Make sure we end the new string */
2027 ptr[elen + rlen] = 0;
2028
2029 return ptr + elen;
2030}
2031
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002032static void update_event_printk(struct trace_event_call *call,
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002033 struct trace_enum_map *map)
2034{
2035 char *ptr;
2036 int quote = 0;
2037 int len = strlen(map->enum_string);
2038
2039 for (ptr = call->print_fmt; *ptr; ptr++) {
2040 if (*ptr == '\\') {
2041 ptr++;
2042 /* paranoid */
2043 if (!*ptr)
2044 break;
2045 continue;
2046 }
2047 if (*ptr == '"') {
2048 quote ^= 1;
2049 continue;
2050 }
2051 if (quote)
2052 continue;
2053 if (isdigit(*ptr)) {
2054 /* skip numbers */
2055 do {
2056 ptr++;
2057 /* Check for alpha chars like ULL */
2058 } while (isalnum(*ptr));
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002059 if (!*ptr)
2060 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002061 /*
2062 * A number must have some kind of delimiter after
2063 * it, and we can ignore that too.
2064 */
2065 continue;
2066 }
2067 if (isalpha(*ptr) || *ptr == '_') {
2068 if (strncmp(map->enum_string, ptr, len) == 0 &&
2069 !isalnum(ptr[len]) && ptr[len] != '_') {
2070 ptr = enum_replace(ptr, map, len);
2071 /* Hmm, enum string smaller than value */
2072 if (WARN_ON_ONCE(!ptr))
2073 return;
2074 /*
2075 * No need to decrement here, as enum_replace()
2076 * returns the pointer to the character passed
2077 * the enum, and two enums can not be placed
2078 * back to back without something in between.
2079 * We can skip that something in between.
2080 */
2081 continue;
2082 }
2083 skip_more:
2084 do {
2085 ptr++;
2086 } while (isalnum(*ptr) || *ptr == '_');
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002087 if (!*ptr)
2088 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002089 /*
2090 * If what comes after this variable is a '.' or
2091 * '->' then we can continue to ignore that string.
2092 */
2093 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2094 ptr += *ptr == '.' ? 1 : 2;
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002095 if (!*ptr)
2096 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002097 goto skip_more;
2098 }
2099 /*
2100 * Once again, we can skip the delimiter that came
2101 * after the string.
2102 */
2103 continue;
2104 }
2105 }
2106}
2107
2108void trace_event_enum_update(struct trace_enum_map **map, int len)
2109{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002110 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002111 const char *last_system = NULL;
2112 int last_i;
2113 int i;
2114
2115 down_write(&trace_event_sem);
2116 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2117 /* events are usually grouped together with systems */
2118 if (!last_system || call->class->system != last_system) {
2119 last_i = 0;
2120 last_system = call->class->system;
2121 }
2122
2123 for (i = last_i; i < len; i++) {
2124 if (call->class->system == map[i]->system) {
2125 /* Save the first system if need be */
2126 if (!last_i)
2127 last_i = i;
2128 update_event_printk(call, map[i]);
2129 }
2130 }
2131 }
2132 up_write(&trace_event_sem);
2133}
2134
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002135static struct trace_event_file *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002136trace_create_new_event(struct trace_event_call *call,
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002137 struct trace_array *tr)
2138{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002139 struct trace_event_file *file;
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002140
2141 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2142 if (!file)
2143 return NULL;
2144
2145 file->event_call = call;
2146 file->tr = tr;
2147 atomic_set(&file->sm_ref, 0);
Tom Zanussi85f2b082013-10-24 08:59:24 -05002148 atomic_set(&file->tm_ref, 0);
2149 INIT_LIST_HEAD(&file->triggers);
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002150 list_add(&file->list, &tr->events);
2151
2152 return file;
2153}
2154
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002155/* Add an event to a trace directory */
2156static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002157__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002158{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002159 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002160
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002161 file = trace_create_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002162 if (!file)
2163 return -ENOMEM;
2164
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002165 return event_create_dir(tr->event_dir, file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002166}
2167
Steven Rostedt77248222013-02-27 16:28:06 -05002168/*
2169 * Just create a decriptor for early init. A descriptor is required
2170 * for enabling events at boot. We want to enable events before
2171 * the filesystem is initialized.
2172 */
2173static __init int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002174__trace_early_add_new_event(struct trace_event_call *call,
Steven Rostedt77248222013-02-27 16:28:06 -05002175 struct trace_array *tr)
2176{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002177 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002178
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002179 file = trace_create_new_event(call, tr);
Steven Rostedt77248222013-02-27 16:28:06 -05002180 if (!file)
2181 return -ENOMEM;
2182
Steven Rostedt77248222013-02-27 16:28:06 -05002183 return 0;
2184}
2185
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002186struct ftrace_module_file_ops;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002187static void __add_event_to_tracers(struct trace_event_call *call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002188
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002189/* Add an additional event_call dynamically */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002190int trace_add_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002191{
2192 int ret;
Alexander Z Lama8227412013-07-01 19:37:54 -07002193 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002194 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002195
2196 ret = __register_event(call, NULL);
2197 if (ret >= 0)
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002198 __add_event_to_tracers(call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002199
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002200 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002201 mutex_unlock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002202 return ret;
2203}
Steven Rostedt701970b2009-04-24 23:11:22 -04002204
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04002205/*
Alexander Z Lama8227412013-07-01 19:37:54 -07002206 * Must be called under locking of trace_types_lock, event_mutex and
2207 * trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04002208 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002209static void __trace_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002210{
Ezequiel Garcia87819152012-09-12 11:47:57 -03002211 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002212 trace_destroy_fields(call);
Oleg Nesterov57375742014-07-15 20:48:16 +02002213 free_event_filter(call->filter);
2214 call->filter = NULL;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002215}
2216
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002217static int probe_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002218{
Oleg Nesterov2816c552013-07-29 19:50:33 +02002219 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002220 struct trace_event_file *file;
Oleg Nesterov2816c552013-07-29 19:50:33 +02002221
2222#ifdef CONFIG_PERF_EVENTS
2223 if (call->perf_refcount)
2224 return -EBUSY;
2225#endif
2226 do_for_each_event_file(tr, file) {
2227 if (file->event_call != call)
2228 continue;
2229 /*
2230 * We can't rely on ftrace_event_enable_disable(enable => 0)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002231 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
Oleg Nesterov2816c552013-07-29 19:50:33 +02002232 * TRACE_REG_UNREGISTER.
2233 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002234 if (file->flags & EVENT_FILE_FL_ENABLED)
Oleg Nesterov2816c552013-07-29 19:50:33 +02002235 return -EBUSY;
Steven Rostedt (Red Hat)2ba64032013-07-31 13:16:22 -04002236 /*
2237 * The do_for_each_event_file_safe() is
2238 * a double loop. After finding the call for this
2239 * trace_array, we use break to jump to the next
2240 * trace_array.
2241 */
Oleg Nesterov2816c552013-07-29 19:50:33 +02002242 break;
2243 } while_for_each_event_file();
2244
2245 __trace_remove_event_call(call);
2246
2247 return 0;
2248}
2249
2250/* Remove an event_call */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002251int trace_remove_event_call(struct trace_event_call *call)
Oleg Nesterov2816c552013-07-29 19:50:33 +02002252{
2253 int ret;
2254
Alexander Z Lama8227412013-07-01 19:37:54 -07002255 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002256 mutex_lock(&event_mutex);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002257 down_write(&trace_event_sem);
Oleg Nesterov2816c552013-07-29 19:50:33 +02002258 ret = probe_remove_event_call(call);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002259 up_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002260 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002261 mutex_unlock(&trace_types_lock);
Oleg Nesterov2816c552013-07-29 19:50:33 +02002262
2263 return ret;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002264}
2265
2266#define for_each_event(event, start, end) \
2267 for (event = start; \
2268 (unsigned long)event < (unsigned long)end; \
2269 event++)
2270
2271#ifdef CONFIG_MODULES
2272
Steven Rostedt6d723732009-04-10 14:53:50 -04002273static void trace_module_add_events(struct module *mod)
2274{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002275 struct trace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04002276
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -05002277 if (!mod->num_trace_events)
2278 return;
2279
2280 /* Don't add infrastructure for mods without tracepoints */
2281 if (trace_module_has_bad_taint(mod)) {
2282 pr_err("%s: module has bad taint, not creating trace events\n",
2283 mod->name);
2284 return;
2285 }
2286
Steven Rostedt6d723732009-04-10 14:53:50 -04002287 start = mod->trace_events;
2288 end = mod->trace_events + mod->num_trace_events;
2289
Steven Rostedt6d723732009-04-10 14:53:50 -04002290 for_each_event(call, start, end) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002291 __register_event(*call, mod);
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002292 __add_event_to_tracers(*call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002293 }
2294}
2295
2296static void trace_module_remove_events(struct module *mod)
2297{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002298 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002299 bool clear_trace = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04002300
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002301 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04002302 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2303 if (call->mod == mod) {
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002304 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2305 clear_trace = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002306 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002307 }
2308 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002309 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002310
2311 /*
2312 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002313 * registered any events that were used. The only worry is if
2314 * a new module gets loaded, and takes on the same id as the events
2315 * of this module. When printing out the buffer, traced events left
2316 * over from this module may be passed to the new module events and
2317 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002318 */
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002319 if (clear_trace)
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002320 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04002321}
2322
Steven Rostedt61f919a2009-04-14 18:22:32 -04002323static int trace_module_notify(struct notifier_block *self,
2324 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04002325{
2326 struct module *mod = data;
2327
Alexander Z Lama8227412013-07-01 19:37:54 -07002328 mutex_lock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002329 mutex_lock(&event_mutex);
2330 switch (val) {
2331 case MODULE_STATE_COMING:
2332 trace_module_add_events(mod);
2333 break;
2334 case MODULE_STATE_GOING:
2335 trace_module_remove_events(mod);
2336 break;
2337 }
2338 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002339 mutex_unlock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002340
2341 return 0;
2342}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05002343
Oleg Nesterov836d4812013-07-31 19:31:37 +02002344static struct notifier_block trace_module_nb = {
2345 .notifier_call = trace_module_notify,
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002346 .priority = 1, /* higher than trace.c module notify */
Oleg Nesterov836d4812013-07-31 19:31:37 +02002347};
Steven Rostedt61f919a2009-04-14 18:22:32 -04002348#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04002349
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002350/* Create a new event directory structure for a trace directory. */
2351static void
2352__trace_add_event_dirs(struct trace_array *tr)
2353{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002354 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002355 int ret;
2356
2357 list_for_each_entry(call, &ftrace_events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002358 ret = __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002359 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002360 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002361 trace_event_name(call));
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002362 }
2363}
2364
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002365struct trace_event_file *
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002366find_event_file(struct trace_array *tr, const char *system, const char *event)
2367{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002368 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002369 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002370 const char *name;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002371
2372 list_for_each_entry(file, &tr->events, list) {
2373
2374 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002375 name = trace_event_name(call);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002376
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002377 if (!name || !call->class || !call->class->reg)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002378 continue;
2379
2380 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2381 continue;
2382
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002383 if (strcmp(event, name) == 0 &&
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002384 strcmp(system, call->class->system) == 0)
2385 return file;
2386 }
2387 return NULL;
2388}
2389
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002390#ifdef CONFIG_DYNAMIC_FTRACE
2391
2392/* Avoid typos */
2393#define ENABLE_EVENT_STR "enable_event"
2394#define DISABLE_EVENT_STR "disable_event"
2395
2396struct event_probe_data {
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002397 struct trace_event_file *file;
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002398 unsigned long count;
2399 int ref;
2400 bool enable;
2401};
2402
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002403static void
2404event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2405{
2406 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2407 struct event_probe_data *data = *pdata;
2408
2409 if (!data)
2410 return;
2411
2412 if (data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002413 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002414 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002415 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002416}
2417
2418static void
2419event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2420{
2421 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2422 struct event_probe_data *data = *pdata;
2423
2424 if (!data)
2425 return;
2426
2427 if (!data->count)
2428 return;
2429
2430 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002431 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002432 return;
2433
2434 if (data->count != -1)
2435 (data->count)--;
2436
2437 event_enable_probe(ip, parent_ip, _data);
2438}
2439
2440static int
2441event_enable_print(struct seq_file *m, unsigned long ip,
2442 struct ftrace_probe_ops *ops, void *_data)
2443{
2444 struct event_probe_data *data = _data;
2445
2446 seq_printf(m, "%ps:", (void *)ip);
2447
2448 seq_printf(m, "%s:%s:%s",
2449 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2450 data->file->event_call->class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002451 trace_event_name(data->file->event_call));
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002452
2453 if (data->count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002454 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002455 else
2456 seq_printf(m, ":count=%ld\n", data->count);
2457
2458 return 0;
2459}
2460
2461static int
2462event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2463 void **_data)
2464{
2465 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2466 struct event_probe_data *data = *pdata;
2467
2468 data->ref++;
2469 return 0;
2470}
2471
2472static void
2473event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2474 void **_data)
2475{
2476 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2477 struct event_probe_data *data = *pdata;
2478
2479 if (WARN_ON_ONCE(data->ref <= 0))
2480 return;
2481
2482 data->ref--;
2483 if (!data->ref) {
2484 /* Remove the SOFT_MODE flag */
2485 __ftrace_event_enable_disable(data->file, 0, 1);
2486 module_put(data->file->event_call->mod);
2487 kfree(data);
2488 }
2489 *pdata = NULL;
2490}
2491
2492static struct ftrace_probe_ops event_enable_probe_ops = {
2493 .func = event_enable_probe,
2494 .print = event_enable_print,
2495 .init = event_enable_init,
2496 .free = event_enable_free,
2497};
2498
2499static struct ftrace_probe_ops event_enable_count_probe_ops = {
2500 .func = event_enable_count_probe,
2501 .print = event_enable_print,
2502 .init = event_enable_init,
2503 .free = event_enable_free,
2504};
2505
2506static struct ftrace_probe_ops event_disable_probe_ops = {
2507 .func = event_enable_probe,
2508 .print = event_enable_print,
2509 .init = event_enable_init,
2510 .free = event_enable_free,
2511};
2512
2513static struct ftrace_probe_ops event_disable_count_probe_ops = {
2514 .func = event_enable_count_probe,
2515 .print = event_enable_print,
2516 .init = event_enable_init,
2517 .free = event_enable_free,
2518};
2519
2520static int
2521event_enable_func(struct ftrace_hash *hash,
2522 char *glob, char *cmd, char *param, int enabled)
2523{
2524 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002525 struct trace_event_file *file;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002526 struct ftrace_probe_ops *ops;
2527 struct event_probe_data *data;
2528 const char *system;
2529 const char *event;
2530 char *number;
2531 bool enable;
2532 int ret;
2533
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002534 if (!tr)
2535 return -ENODEV;
2536
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002537 /* hash funcs only work with set_ftrace_filter */
Harsh Prateek Bora8092e802013-05-24 12:52:17 +05302538 if (!enabled || !param)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002539 return -EINVAL;
2540
2541 system = strsep(&param, ":");
2542 if (!param)
2543 return -EINVAL;
2544
2545 event = strsep(&param, ":");
2546
2547 mutex_lock(&event_mutex);
2548
2549 ret = -EINVAL;
2550 file = find_event_file(tr, system, event);
2551 if (!file)
2552 goto out;
2553
2554 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2555
2556 if (enable)
2557 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2558 else
2559 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2560
2561 if (glob[0] == '!') {
2562 unregister_ftrace_function_probe_func(glob+1, ops);
2563 ret = 0;
2564 goto out;
2565 }
2566
2567 ret = -ENOMEM;
2568 data = kzalloc(sizeof(*data), GFP_KERNEL);
2569 if (!data)
2570 goto out;
2571
2572 data->enable = enable;
2573 data->count = -1;
2574 data->file = file;
2575
2576 if (!param)
2577 goto out_reg;
2578
2579 number = strsep(&param, ":");
2580
2581 ret = -EINVAL;
2582 if (!strlen(number))
2583 goto out_free;
2584
2585 /*
2586 * We use the callback data field (which is a pointer)
2587 * as our counter.
2588 */
2589 ret = kstrtoul(number, 0, &data->count);
2590 if (ret)
2591 goto out_free;
2592
2593 out_reg:
2594 /* Don't let event modules unload while probe registered */
2595 ret = try_module_get(file->event_call->mod);
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002596 if (!ret) {
2597 ret = -EBUSY;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002598 goto out_free;
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002599 }
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002600
2601 ret = __ftrace_event_enable_disable(file, 1, 1);
2602 if (ret < 0)
2603 goto out_put;
2604 ret = register_ftrace_function_probe(glob, ops, data);
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002605 /*
2606 * The above returns on success the # of functions enabled,
2607 * but if it didn't find any functions it returns zero.
2608 * Consider no functions a failure too.
2609 */
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002610 if (!ret) {
2611 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002612 goto out_disable;
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002613 } else if (ret < 0)
2614 goto out_disable;
2615 /* Just return zero, not the number of enabled functions */
2616 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002617 out:
2618 mutex_unlock(&event_mutex);
2619 return ret;
2620
2621 out_disable:
2622 __ftrace_event_enable_disable(file, 0, 1);
2623 out_put:
2624 module_put(file->event_call->mod);
2625 out_free:
2626 kfree(data);
2627 goto out;
2628}
2629
2630static struct ftrace_func_command event_enable_cmd = {
2631 .name = ENABLE_EVENT_STR,
2632 .func = event_enable_func,
2633};
2634
2635static struct ftrace_func_command event_disable_cmd = {
2636 .name = DISABLE_EVENT_STR,
2637 .func = event_enable_func,
2638};
2639
2640static __init int register_event_cmds(void)
2641{
2642 int ret;
2643
2644 ret = register_ftrace_command(&event_enable_cmd);
2645 if (WARN_ON(ret < 0))
2646 return ret;
2647 ret = register_ftrace_command(&event_disable_cmd);
2648 if (WARN_ON(ret < 0))
2649 unregister_ftrace_command(&event_enable_cmd);
2650 return ret;
2651}
2652#else
2653static inline int register_event_cmds(void) { return 0; }
2654#endif /* CONFIG_DYNAMIC_FTRACE */
2655
Steven Rostedt77248222013-02-27 16:28:06 -05002656/*
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002657 * The top level array has already had its trace_event_file
Steven Rostedt77248222013-02-27 16:28:06 -05002658 * descriptors created in order to allow for early events to
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002659 * be recorded. This function is called after the tracefs has been
Steven Rostedt77248222013-02-27 16:28:06 -05002660 * initialized, and we now have to create the files associated
2661 * to the events.
2662 */
2663static __init void
2664__trace_early_add_event_dirs(struct trace_array *tr)
2665{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002666 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002667 int ret;
2668
2669
2670 list_for_each_entry(file, &tr->events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002671 ret = event_create_dir(tr->event_dir, file);
Steven Rostedt77248222013-02-27 16:28:06 -05002672 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002673 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002674 trace_event_name(file->event_call));
Steven Rostedt77248222013-02-27 16:28:06 -05002675 }
2676}
2677
2678/*
2679 * For early boot up, the top trace array requires to have
2680 * a list of events that can be enabled. This must be done before
2681 * the filesystem is set up in order to allow events to be traced
2682 * early.
2683 */
2684static __init void
2685__trace_early_add_events(struct trace_array *tr)
2686{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002687 struct trace_event_call *call;
Steven Rostedt77248222013-02-27 16:28:06 -05002688 int ret;
2689
2690 list_for_each_entry(call, &ftrace_events, list) {
2691 /* Early boot up should not have any modules loaded */
2692 if (WARN_ON_ONCE(call->mod))
2693 continue;
2694
2695 ret = __trace_early_add_new_event(call, tr);
2696 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002697 pr_warn("Could not create early event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002698 trace_event_name(call));
Steven Rostedt77248222013-02-27 16:28:06 -05002699 }
2700}
2701
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002702/* Remove the event directory structure for a trace directory. */
2703static void
2704__trace_remove_event_dirs(struct trace_array *tr)
2705{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002706 struct trace_event_file *file, *next;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002707
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002708 list_for_each_entry_safe(file, next, &tr->events, list)
2709 remove_event_file_dir(file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002710}
2711
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002712static void __add_event_to_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002713{
2714 struct trace_array *tr;
2715
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002716 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2717 __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002718}
2719
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002720extern struct trace_event_call *__start_ftrace_events[];
2721extern struct trace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002722
Li Zefan020e5f82009-07-01 10:47:05 +08002723static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2724
2725static __init int setup_trace_event(char *str)
2726{
2727 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002728 ring_buffer_expanded = true;
2729 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002730
2731 return 1;
2732}
2733__setup("trace_event=", setup_trace_event);
2734
Steven Rostedt77248222013-02-27 16:28:06 -05002735/* Expects to have event_mutex held when called */
2736static int
2737create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002738{
2739 struct dentry *d_events;
2740 struct dentry *entry;
2741
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002742 entry = tracefs_create_file("set_event", 0644, parent,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002743 tr, &ftrace_set_event_fops);
2744 if (!entry) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002745 pr_warn("Could not create tracefs 'set_event' entry\n");
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002746 return -ENOMEM;
2747 }
2748
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002749 d_events = tracefs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002750 if (!d_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002751 pr_warn("Could not create tracefs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002752 return -ENOMEM;
2753 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002754
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04002755 entry = tracefs_create_file("set_event_pid", 0644, parent,
2756 tr, &ftrace_set_event_pid_fops);
2757
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002758 /* ring buffer internal formats */
2759 trace_create_file("header_page", 0444, d_events,
2760 ring_buffer_print_page_header,
2761 &ftrace_show_header_fops);
2762
2763 trace_create_file("header_event", 0444, d_events,
2764 ring_buffer_print_entry_header,
2765 &ftrace_show_header_fops);
2766
2767 trace_create_file("enable", 0644, d_events,
2768 tr, &ftrace_tr_enable_fops);
2769
2770 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002771
2772 return 0;
2773}
2774
2775/**
2776 * event_trace_add_tracer - add a instance of a trace_array to events
2777 * @parent: The parent dentry to place the files/directories for events in
2778 * @tr: The trace array associated with these events
2779 *
2780 * When a new instance is created, it needs to set up its events
2781 * directory, as well as other files associated with events. It also
2782 * creates the event hierachry in the @parent/events directory.
2783 *
2784 * Returns 0 on success.
2785 */
2786int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2787{
2788 int ret;
2789
2790 mutex_lock(&event_mutex);
2791
2792 ret = create_event_toplevel_files(parent, tr);
2793 if (ret)
2794 goto out_unlock;
2795
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002796 down_write(&trace_event_sem);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002797 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002798 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002799
Steven Rostedt77248222013-02-27 16:28:06 -05002800 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04002801 mutex_unlock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002802
Steven Rostedt77248222013-02-27 16:28:06 -05002803 return ret;
2804}
2805
2806/*
2807 * The top trace array already had its file descriptors created.
2808 * Now the files themselves need to be created.
2809 */
2810static __init int
2811early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2812{
2813 int ret;
2814
2815 mutex_lock(&event_mutex);
2816
2817 ret = create_event_toplevel_files(parent, tr);
2818 if (ret)
2819 goto out_unlock;
2820
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002821 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002822 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002823 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002824
2825 out_unlock:
2826 mutex_unlock(&event_mutex);
2827
2828 return ret;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002829}
2830
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002831int event_trace_del_tracer(struct trace_array *tr)
2832{
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002833 mutex_lock(&event_mutex);
2834
Tom Zanussi85f2b082013-10-24 08:59:24 -05002835 /* Disable any event triggers and associated soft-disabled events */
2836 clear_event_triggers(tr);
2837
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04002838 /* Clear the pid list */
2839 __ftrace_clear_event_pids(tr);
2840
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -04002841 /* Disable any running events */
2842 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2843
Steven Rostedt3ccb0122013-12-03 12:41:20 -05002844 /* Access to events are within rcu_read_lock_sched() */
2845 synchronize_sched();
2846
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002847 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002848 __trace_remove_event_dirs(tr);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002849 tracefs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002850 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002851
2852 tr->event_dir = NULL;
2853
2854 mutex_unlock(&event_mutex);
2855
2856 return 0;
2857}
2858
Steven Rostedtd1a29142013-02-27 20:23:57 -05002859static __init int event_trace_memsetup(void)
2860{
2861 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002862 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
Steven Rostedtd1a29142013-02-27 20:23:57 -05002863 return 0;
2864}
2865
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002866static __init void
2867early_enable_events(struct trace_array *tr, bool disable_first)
2868{
2869 char *buf = bootup_event_buf;
2870 char *token;
2871 int ret;
2872
2873 while (true) {
2874 token = strsep(&buf, ",");
2875
2876 if (!token)
2877 break;
2878 if (!*token)
2879 continue;
2880
2881 /* Restarting syscalls requires that we stop them first */
2882 if (disable_first)
2883 ftrace_set_clr_event(tr, token, 0);
2884
2885 ret = ftrace_set_clr_event(tr, token, 1);
2886 if (ret)
2887 pr_warn("Failed to enable trace event: %s\n", token);
2888
2889 /* Put back the comma to allow this to be called again */
2890 if (buf)
2891 *(buf - 1) = ',';
2892 }
2893}
2894
Ezequiel Garcia87819152012-09-12 11:47:57 -03002895static __init int event_trace_enable(void)
2896{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002897 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002898 struct trace_event_call **iter, *call;
Ezequiel Garcia87819152012-09-12 11:47:57 -03002899 int ret;
2900
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002901 if (!tr)
2902 return -ENODEV;
2903
Ezequiel Garcia87819152012-09-12 11:47:57 -03002904 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2905
2906 call = *iter;
2907 ret = event_init(call);
2908 if (!ret)
2909 list_add(&call->list, &ftrace_events);
2910 }
2911
Steven Rostedt77248222013-02-27 16:28:06 -05002912 /*
2913 * We need the top trace array to have a working set of trace
2914 * points at early init, before the debug files and directories
2915 * are created. Create the file entries now, and attach them
2916 * to the actual file dentries later.
2917 */
2918 __trace_early_add_events(tr);
2919
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002920 early_enable_events(tr, false);
Steven Rostedt81698832012-10-11 10:15:05 -04002921
2922 trace_printk_start_comm();
2923
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002924 register_event_cmds();
2925
Tom Zanussi85f2b082013-10-24 08:59:24 -05002926 register_trigger_cmds();
2927
Ezequiel Garcia87819152012-09-12 11:47:57 -03002928 return 0;
2929}
2930
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002931/*
2932 * event_trace_enable() is called from trace_event_init() first to
2933 * initialize events and perhaps start any events that are on the
2934 * command line. Unfortunately, there are some events that will not
2935 * start this early, like the system call tracepoints that need
2936 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2937 * is called before pid 1 starts, and this flag is never set, making
2938 * the syscall tracepoint never get reached, but the event is enabled
2939 * regardless (and not doing anything).
2940 */
2941static __init int event_trace_enable_again(void)
2942{
2943 struct trace_array *tr;
2944
2945 tr = top_trace_array();
2946 if (!tr)
2947 return -ENODEV;
2948
2949 early_enable_events(tr, true);
2950
2951 return 0;
2952}
2953
2954early_initcall(event_trace_enable_again);
2955
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002956static __init int event_trace_init(void)
2957{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002958 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002959 struct dentry *d_tracer;
2960 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04002961 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002962
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002963 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002964 if (!tr)
2965 return -ENODEV;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002966
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002967 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05002968 if (IS_ERR(d_tracer))
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002969 return 0;
2970
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002971 entry = tracefs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002972 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002973 if (!entry)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002974 pr_warn("Could not create tracefs 'available_events' entry\n");
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002975
Daniel Wagner9f616682015-08-10 14:35:46 +02002976 if (trace_define_generic_fields())
2977 pr_warn("tracing: Failed to allocated generic fields");
2978
Li Zefan8728fe52010-05-24 16:22:49 +08002979 if (trace_define_common_fields())
Fabian Frederick3448bac2014-06-07 13:43:08 +02002980 pr_warn("tracing: Failed to allocate common fields");
Li Zefan8728fe52010-05-24 16:22:49 +08002981
Steven Rostedt77248222013-02-27 16:28:06 -05002982 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002983 if (ret)
2984 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08002985
Oleg Nesterov836d4812013-07-31 19:31:37 +02002986#ifdef CONFIG_MODULES
Steven Rostedt6d723732009-04-10 14:53:50 -04002987 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08002988 if (ret)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002989 pr_warn("Failed to register trace events module notifier\n");
Oleg Nesterov836d4812013-07-31 19:31:37 +02002990#endif
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002991 return 0;
2992}
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05002993
2994void __init trace_event_init(void)
2995{
2996 event_trace_memsetup();
2997 init_ftrace_syscalls();
2998 event_trace_enable();
2999}
3000
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003001fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04003002
3003#ifdef CONFIG_FTRACE_STARTUP_TEST
3004
3005static DEFINE_SPINLOCK(test_spinlock);
3006static DEFINE_SPINLOCK(test_spinlock_irq);
3007static DEFINE_MUTEX(test_mutex);
3008
3009static __init void test_work(struct work_struct *dummy)
3010{
3011 spin_lock(&test_spinlock);
3012 spin_lock_irq(&test_spinlock_irq);
3013 udelay(1);
3014 spin_unlock_irq(&test_spinlock_irq);
3015 spin_unlock(&test_spinlock);
3016
3017 mutex_lock(&test_mutex);
3018 msleep(1);
3019 mutex_unlock(&test_mutex);
3020}
3021
3022static __init int event_test_thread(void *unused)
3023{
3024 void *test_malloc;
3025
3026 test_malloc = kmalloc(1234, GFP_KERNEL);
3027 if (!test_malloc)
3028 pr_info("failed to kmalloc\n");
3029
3030 schedule_on_each_cpu(test_work);
3031
3032 kfree(test_malloc);
3033
3034 set_current_state(TASK_INTERRUPTIBLE);
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02003035 while (!kthread_should_stop()) {
Steven Rostedte6187002009-04-15 13:36:40 -04003036 schedule();
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02003037 set_current_state(TASK_INTERRUPTIBLE);
3038 }
3039 __set_current_state(TASK_RUNNING);
Steven Rostedte6187002009-04-15 13:36:40 -04003040
3041 return 0;
3042}
3043
3044/*
3045 * Do various things that may trigger events.
3046 */
3047static __init void event_test_stuff(void)
3048{
3049 struct task_struct *test_thread;
3050
3051 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3052 msleep(1);
3053 kthread_stop(test_thread);
3054}
3055
3056/*
3057 * For every trace event defined, we will test each trace point separately,
3058 * and then by groups, and finally all trace points.
3059 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003060static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04003061{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04003062 struct trace_subsystem_dir *dir;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04003063 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003064 struct trace_event_call *call;
Steven Rostedte6187002009-04-15 13:36:40 -04003065 struct event_subsystem *system;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003066 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04003067 int ret;
3068
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003069 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09003070 if (!tr)
3071 return;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003072
Steven Rostedte6187002009-04-15 13:36:40 -04003073 pr_info("Running tests on trace events:\n");
3074
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003075 list_for_each_entry(file, &tr->events, list) {
3076
3077 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04003078
Steven Rostedt22392912010-04-21 12:27:06 -04003079 /* Only test those that have a probe */
3080 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04003081 continue;
3082
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04003083/*
3084 * Testing syscall events here is pretty useless, but
3085 * we still do it if configured. But this is time consuming.
3086 * What we really need is a user thread to perform the
3087 * syscalls as we test.
3088 */
3089#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04003090 if (call->class->system &&
3091 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04003092 continue;
3093#endif
3094
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04003095 pr_info("Testing event %s: ", trace_event_name(call));
Steven Rostedte6187002009-04-15 13:36:40 -04003096
3097 /*
3098 * If an event is already enabled, someone is using
3099 * it and the self test should not be on.
3100 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04003101 if (file->flags & EVENT_FILE_FL_ENABLED) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003102 pr_warn("Enabled event during self test!\n");
Steven Rostedte6187002009-04-15 13:36:40 -04003103 WARN_ON_ONCE(1);
3104 continue;
3105 }
3106
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003107 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003108 event_test_stuff();
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003109 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04003110
3111 pr_cont("OK\n");
3112 }
3113
3114 /* Now test at the sub system level */
3115
3116 pr_info("Running tests on trace event systems:\n");
3117
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003118 list_for_each_entry(dir, &tr->systems, list) {
3119
3120 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04003121
3122 /* the ftrace system is special, skip it */
3123 if (strcmp(system->name, "ftrace") == 0)
3124 continue;
3125
3126 pr_info("Testing event system %s: ", system->name);
3127
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003128 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003129 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003130 pr_warn("error enabling system %s\n",
3131 system->name);
Steven Rostedte6187002009-04-15 13:36:40 -04003132 continue;
3133 }
3134
3135 event_test_stuff();
3136
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003137 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08003138 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003139 pr_warn("error disabling system %s\n",
3140 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08003141 continue;
3142 }
Steven Rostedte6187002009-04-15 13:36:40 -04003143
3144 pr_cont("OK\n");
3145 }
3146
3147 /* Test with all events enabled */
3148
3149 pr_info("Running tests on all trace events:\n");
3150 pr_info("Testing all events: ");
3151
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003152 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003153 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003154 pr_warn("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003155 return;
Steven Rostedte6187002009-04-15 13:36:40 -04003156 }
3157
3158 event_test_stuff();
3159
3160 /* reset sysname */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003161 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04003162 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003163 pr_warn("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003164 return;
Steven Rostedte6187002009-04-15 13:36:40 -04003165 }
3166
3167 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003168}
3169
3170#ifdef CONFIG_FUNCTION_TRACER
3171
Tejun Heo245b2e72009-06-24 15:13:48 +09003172static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003173
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04003174static struct trace_array *event_tr;
3175
3176static void __init
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003177function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003178 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003179{
3180 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04003181 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003182 struct ftrace_entry *entry;
3183 unsigned long flags;
3184 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003185 int cpu;
3186 int pc;
3187
3188 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04003189 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003190 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09003191 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003192
3193 if (disabled != 1)
3194 goto out;
3195
3196 local_save_flags(flags);
3197
Steven Rostedte77405a2009-09-02 14:17:06 -04003198 event = trace_current_buffer_lock_reserve(&buffer,
3199 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003200 flags, pc);
3201 if (!event)
3202 goto out;
3203 entry = ring_buffer_event_data(event);
3204 entry->ip = ip;
3205 entry->parent_ip = parent_ip;
3206
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04003207 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003208
3209 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09003210 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04003211 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003212}
3213
3214static struct ftrace_ops trace_ops __initdata =
3215{
3216 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04003217 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003218};
3219
3220static __init void event_trace_self_test_with_function(void)
3221{
Steven Rostedt17bb6152011-05-23 15:27:46 -04003222 int ret;
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003223 event_tr = top_trace_array();
3224 if (WARN_ON(!event_tr))
3225 return;
Steven Rostedt17bb6152011-05-23 15:27:46 -04003226 ret = register_ftrace_function(&trace_ops);
3227 if (WARN_ON(ret < 0)) {
3228 pr_info("Failed to enable function tracer for event tests\n");
3229 return;
3230 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003231 pr_info("Running tests again, along with the function tracer\n");
3232 event_trace_self_tests();
3233 unregister_ftrace_function(&trace_ops);
3234}
3235#else
3236static __init void event_trace_self_test_with_function(void)
3237{
3238}
3239#endif
3240
3241static __init int event_trace_self_tests_init(void)
3242{
Li Zefan020e5f82009-07-01 10:47:05 +08003243 if (!tracing_selftest_disabled) {
3244 event_trace_self_tests();
3245 event_trace_self_test_with_function();
3246 }
Steven Rostedte6187002009-04-15 13:36:40 -04003247
3248 return 0;
3249}
3250
Steven Rostedt28d20e22009-04-20 12:12:44 -04003251late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04003252
3253#endif