blob: 57c9e709772cc20edf9e2cbeb9687cd896f6653c [file] [log] [blame]
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001/*
2 * event tracer
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
Steven Rostedt981d0812009-03-02 13:53:59 -05006 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 *
Steven Rostedtb77e38a2009-02-24 10:21:36 -05009 */
10
Fabian Frederick3448bac2014-06-07 13:43:08 +020011#define pr_fmt(fmt) fmt
12
Steven Rostedte6187002009-04-15 13:36:40 -040013#include <linux/workqueue.h>
14#include <linux/spinlock.h>
15#include <linux/kthread.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050016#include <linux/tracefs.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050017#include <linux/uaccess.h>
18#include <linux/module.h>
19#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Steven Rostedte6187002009-04-15 13:36:40 -040021#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050022
Li Zefan020e5f82009-07-01 10:47:05 +080023#include <asm/setup.h>
24
Steven Rostedt91729ef92009-03-02 15:03:01 -050025#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050026
Steven Rostedt4e5292e2009-09-12 19:26:21 -040027#undef TRACE_SYSTEM
Steven Rostedtb628b3e2009-02-27 23:32:58 -050028#define TRACE_SYSTEM "TRACE_SYSTEM"
29
Li Zefan20c89282009-05-06 10:33:45 +080030DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050031
Steven Rostedta59fd602009-04-10 13:52:20 -040032LIST_HEAD(ftrace_events);
Daniel Wagner9f616682015-08-10 14:35:46 +020033static LIST_HEAD(ftrace_generic_fields);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080034static LIST_HEAD(ftrace_common_fields);
Steven Rostedta59fd602009-04-10 13:52:20 -040035
Steven Rostedtd1a29142013-02-27 20:23:57 -050036#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
37
38static struct kmem_cache *field_cachep;
39static struct kmem_cache *file_cachep;
40
Steven Rostedt6e94a782013-06-27 10:58:31 -040041#define SYSTEM_FL_FREE_NAME (1 << 31)
42
43static inline int system_refcount(struct event_subsystem *system)
44{
45 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
46}
47
48static int system_refcount_inc(struct event_subsystem *system)
49{
50 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
51}
52
53static int system_refcount_dec(struct event_subsystem *system)
54{
55 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
56}
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 Rostedte9dbfae2011-07-05 11:36:06 -0400450static void __put_system(struct event_subsystem *system)
451{
452 struct event_filter *filter = system->filter;
453
Steven Rostedt6e94a782013-06-27 10:58:31 -0400454 WARN_ON_ONCE(system_refcount(system) == 0);
455 if (system_refcount_dec(system))
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400456 return;
457
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400458 list_del(&system->list);
459
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400460 if (filter) {
461 kfree(filter->filter_string);
462 kfree(filter);
463 }
Steven Rostedt6e94a782013-06-27 10:58:31 -0400464 if (system->ref_count & SYSTEM_FL_FREE_NAME)
465 kfree(system->name);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400466 kfree(system);
467}
468
469static void __get_system(struct event_subsystem *system)
470{
Steven Rostedt6e94a782013-06-27 10:58:31 -0400471 WARN_ON_ONCE(system_refcount(system) == 0);
472 system_refcount_inc(system);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400473}
474
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400475static void __get_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400476{
477 WARN_ON_ONCE(dir->ref_count == 0);
478 dir->ref_count++;
479 __get_system(dir->subsystem);
480}
481
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400482static void __put_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400483{
484 WARN_ON_ONCE(dir->ref_count == 0);
485 /* If the subsystem is about to be freed, the dir must be too */
Steven Rostedt6e94a782013-06-27 10:58:31 -0400486 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400487
488 __put_system(dir->subsystem);
489 if (!--dir->ref_count)
490 kfree(dir);
491}
492
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400493static void put_system(struct trace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400494{
495 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400496 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400497 mutex_unlock(&event_mutex);
498}
499
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400500static void remove_subsystem(struct trace_subsystem_dir *dir)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200501{
502 if (!dir)
503 return;
504
505 if (!--dir->nr_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500506 tracefs_remove_recursive(dir->entry);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200507 list_del(&dir->list);
508 __put_system_dir(dir);
509 }
510}
511
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400512static void remove_event_file_dir(struct trace_event_file *file)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200513{
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200514 struct dentry *dir = file->dir;
515 struct dentry *child;
516
517 if (dir) {
518 spin_lock(&dir->d_lock); /* probably unneeded */
Al Viro946e51f2014-10-26 19:19:16 -0400519 list_for_each_entry(child, &dir->d_subdirs, d_child) {
David Howells7682c912015-03-17 22:26:16 +0000520 if (d_really_is_positive(child)) /* probably unneeded */
521 d_inode(child)->i_private = NULL;
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200522 }
523 spin_unlock(&dir->d_lock);
524
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500525 tracefs_remove_recursive(dir);
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200526 }
527
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200528 list_del(&file->list);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200529 remove_subsystem(file->system);
Oleg Nesterov2448e342014-07-11 21:06:38 +0200530 free_event_filter(file->filter);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200531 kmem_cache_free(file_cachep, file);
532}
533
Li Zefan8f31bfe2009-05-08 10:31:42 +0800534/*
535 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
536 */
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400537static int
538__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
539 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500540{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400541 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400542 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400543 const char *name;
Steven Rostedt29f93942009-05-08 16:06:47 -0400544 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500545
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400546 list_for_each_entry(file, &tr->events, list) {
547
548 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400549 name = trace_event_name(call);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500550
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400551 if (!name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500552 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500553
Steven Rostedt9b637762012-05-10 15:55:43 -0400554 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
555 continue;
556
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500557 if (match &&
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400558 strcmp(match, name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400559 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500560 continue;
561
Steven Rostedt8f082012010-04-20 10:47:33 -0400562 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500563 continue;
564
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400565 if (event && strcmp(event, name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500566 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500567
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400568 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500569
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500570 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500571 }
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400572
573 return ret;
574}
575
576static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
577 const char *sub, const char *event, int set)
578{
579 int ret;
580
581 mutex_lock(&event_mutex);
582 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500583 mutex_unlock(&event_mutex);
584
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500585 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500586}
587
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400588static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800589{
590 char *event = NULL, *sub = NULL, *match;
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900591 int ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800592
593 /*
594 * The buf format can be <subsystem>:<event-name>
595 * *:<event-name> means any event by that name.
596 * :<event-name> is the same.
597 *
598 * <subsystem>:* means all events in that subsystem
599 * <subsystem>: means the same.
600 *
601 * <name> (no ':') means all events in a subsystem with
602 * the name <name> or any event that matches <name>
603 */
604
605 match = strsep(&buf, ":");
606 if (buf) {
607 sub = match;
608 event = buf;
609 match = NULL;
610
611 if (!strlen(sub) || strcmp(sub, "*") == 0)
612 sub = NULL;
613 if (!strlen(event) || strcmp(event, "*") == 0)
614 event = NULL;
615 }
616
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900617 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
618
619 /* Put back the colon to allow this to be called again */
620 if (buf)
621 *(buf - 1) = ':';
622
623 return ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800624}
625
Steven Rostedt4671c792009-05-08 16:27:41 -0400626/**
627 * trace_set_clr_event - enable or disable an event
628 * @system: system name to match (NULL for any system)
629 * @event: event name to match (NULL for all events, within system)
630 * @set: 1 to enable, 0 to disable
631 *
632 * This is a way for other parts of the kernel to enable or disable
633 * event recording.
634 *
635 * Returns 0 on success, -EINVAL if the parameters do not match any
636 * registered events.
637 */
638int trace_set_clr_event(const char *system, const char *event, int set)
639{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400640 struct trace_array *tr = top_trace_array();
641
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +0900642 if (!tr)
643 return -ENODEV;
644
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400645 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400646}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800647EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400648
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500649/* 128 should be much more than enough */
650#define EVENT_BUF_SIZE 127
651
652static ssize_t
653ftrace_event_write(struct file *file, const char __user *ubuf,
654 size_t cnt, loff_t *ppos)
655{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200656 struct trace_parser parser;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400657 struct seq_file *m = file->private_data;
658 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800659 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500660
Li Zefan4ba79782009-09-22 13:52:20 +0800661 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500662 return 0;
663
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400664 ret = tracing_update_buffers();
665 if (ret < 0)
666 return ret;
667
jolsa@redhat.com48966362009-09-11 17:29:28 +0200668 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500669 return -ENOMEM;
670
jolsa@redhat.com48966362009-09-11 17:29:28 +0200671 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500672
Li Zefan4ba79782009-09-22 13:52:20 +0800673 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200674 int set = 1;
675
676 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500677 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500678
jolsa@redhat.com48966362009-09-11 17:29:28 +0200679 parser.buffer[parser.idx] = 0;
680
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400681 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500682 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200683 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500684 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500685
686 ret = read;
687
jolsa@redhat.com48966362009-09-11 17:29:28 +0200688 out_put:
689 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500690
691 return ret;
692}
693
694static void *
695t_next(struct seq_file *m, void *v, loff_t *pos)
696{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400697 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400698 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400699 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500700
701 (*pos)++;
702
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400703 list_for_each_entry_continue(file, &tr->events, list) {
704 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400705 /*
706 * The ftrace subsystem is for showing formats only.
707 * They can not be enabled or disabled via the event files.
708 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400709 if (call->class && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400710 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400711 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500712
Li Zefan30bd39c2009-09-18 14:07:05 +0800713 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500714}
715
716static void *t_start(struct seq_file *m, loff_t *pos)
717{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400718 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400719 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800720 loff_t l;
721
Li Zefan20c89282009-05-06 10:33:45 +0800722 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800723
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400724 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800725 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400726 file = t_next(m, file, &l);
727 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800728 break;
729 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400730 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500731}
732
733static void *
734s_next(struct seq_file *m, void *v, loff_t *pos)
735{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400736 struct trace_event_file *file = v;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400737 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500738
739 (*pos)++;
740
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400741 list_for_each_entry_continue(file, &tr->events, list) {
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400742 if (file->flags & EVENT_FILE_FL_ENABLED)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400743 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500744 }
745
Li Zefan30bd39c2009-09-18 14:07:05 +0800746 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500747}
748
749static void *s_start(struct seq_file *m, loff_t *pos)
750{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400751 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400752 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800753 loff_t l;
754
Li Zefan20c89282009-05-06 10:33:45 +0800755 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800756
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400757 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800758 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400759 file = s_next(m, file, &l);
760 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800761 break;
762 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400763 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500764}
765
766static int t_show(struct seq_file *m, void *v)
767{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400768 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400769 struct trace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500770
Steven Rostedt8f082012010-04-20 10:47:33 -0400771 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
772 seq_printf(m, "%s:", call->class->system);
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400773 seq_printf(m, "%s\n", trace_event_name(call));
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500774
775 return 0;
776}
777
778static void t_stop(struct seq_file *m, void *p)
779{
Li Zefan20c89282009-05-06 10:33:45 +0800780 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500781}
782
Steven Rostedt1473e442009-02-24 14:15:08 -0500783static ssize_t
784event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
785 loff_t *ppos)
786{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400787 struct trace_event_file *file;
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200788 unsigned long flags;
Tom Zanussia4390592013-06-29 00:08:04 -0500789 char buf[4] = "0";
Steven Rostedt1473e442009-02-24 14:15:08 -0500790
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200791 mutex_lock(&event_mutex);
792 file = event_file_data(filp);
793 if (likely(file))
794 flags = file->flags;
795 mutex_unlock(&event_mutex);
796
797 if (!file)
798 return -ENODEV;
799
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400800 if (flags & EVENT_FILE_FL_ENABLED &&
801 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussia4390592013-06-29 00:08:04 -0500802 strcpy(buf, "1");
803
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400804 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
805 flags & EVENT_FILE_FL_SOFT_MODE)
Tom Zanussia4390592013-06-29 00:08:04 -0500806 strcat(buf, "*");
807
808 strcat(buf, "\n");
Steven Rostedt1473e442009-02-24 14:15:08 -0500809
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400810 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -0500811}
812
813static ssize_t
814event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
815 loff_t *ppos)
816{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400817 struct trace_event_file *file;
Steven Rostedt1473e442009-02-24 14:15:08 -0500818 unsigned long val;
819 int ret;
820
Peter Huewe22fe9b52011-06-07 21:58:27 +0200821 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
822 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -0500823 return ret;
824
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400825 ret = tracing_update_buffers();
826 if (ret < 0)
827 return ret;
828
Steven Rostedt1473e442009-02-24 14:15:08 -0500829 switch (val) {
830 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500831 case 1:
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200832 ret = -ENODEV;
Steven Rostedt11a241a2009-03-02 11:49:04 -0500833 mutex_lock(&event_mutex);
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200834 file = event_file_data(filp);
835 if (likely(file))
836 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500837 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500838 break;
839
840 default:
841 return -EINVAL;
842 }
843
844 *ppos += cnt;
845
Li Zefan3b8e4272009-12-08 11:14:52 +0800846 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500847}
848
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400849static ssize_t
850system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
851 loff_t *ppos)
852{
Li Zefanc142b152009-05-08 10:32:05 +0800853 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400854 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400855 struct event_subsystem *system = dir->subsystem;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400856 struct trace_event_call *call;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400857 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400858 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400859 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800860 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400861 int ret;
862
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400863 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400864 list_for_each_entry(file, &tr->events, list) {
865 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400866 if (!trace_event_name(call) || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400867 continue;
868
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400869 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400870 continue;
871
872 /*
873 * We need to find out if all the events are set
874 * or if all events or cleared, or if we have
875 * a mixture.
876 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400877 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +0800878
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400879 /*
880 * If we have a mixture, no need to look further.
881 */
Li Zefanc142b152009-05-08 10:32:05 +0800882 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400883 break;
884 }
885 mutex_unlock(&event_mutex);
886
Li Zefanc142b152009-05-08 10:32:05 +0800887 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400888 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400889
890 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
891
892 return ret;
893}
894
895static ssize_t
896system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
897 loff_t *ppos)
898{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400899 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400900 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400901 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400902 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400903 ssize_t ret;
904
Peter Huewe22fe9b52011-06-07 21:58:27 +0200905 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
906 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400907 return ret;
908
909 ret = tracing_update_buffers();
910 if (ret < 0)
911 return ret;
912
Li Zefan8f31bfe2009-05-08 10:31:42 +0800913 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400914 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400915
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400916 /*
917 * Opening of "enable" adds a ref count to system,
918 * so the name is safe to use.
919 */
920 if (system)
921 name = system->name;
922
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400923 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400924 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800925 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400926
927 ret = cnt;
928
Li Zefan8f31bfe2009-05-08 10:31:42 +0800929out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400930 *ppos += cnt;
931
932 return ret;
933}
934
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400935enum {
936 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +0800937 FORMAT_FIELD_SEPERATOR = 2,
938 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400939};
940
941static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -0500942{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400943 struct trace_event_call *call = event_file_data(m->private);
Li Zefan86397dc2010-08-17 13:53:06 +0800944 struct list_head *common_head = &ftrace_common_fields;
945 struct list_head *head = trace_get_fields(call);
Oleg Nesterov7710b632013-07-18 20:47:10 +0200946 struct list_head *node = v;
Steven Rostedt981d0812009-03-02 13:53:59 -0500947
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400948 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800949
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400950 switch ((unsigned long)v) {
951 case FORMAT_HEADER:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200952 node = common_head;
953 break;
Li Zefan86397dc2010-08-17 13:53:06 +0800954
955 case FORMAT_FIELD_SEPERATOR:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200956 node = head;
957 break;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800958
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400959 case FORMAT_PRINTFMT:
960 /* all done */
961 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800962 }
963
Oleg Nesterov7710b632013-07-18 20:47:10 +0200964 node = node->prev;
965 if (node == common_head)
Li Zefan86397dc2010-08-17 13:53:06 +0800966 return (void *)FORMAT_FIELD_SEPERATOR;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200967 else if (node == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400968 return (void *)FORMAT_PRINTFMT;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200969 else
970 return node;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400971}
972
973static int f_show(struct seq_file *m, void *v)
974{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400975 struct trace_event_call *call = event_file_data(m->private);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400976 struct ftrace_event_field *field;
977 const char *array_descriptor;
978
979 switch ((unsigned long)v) {
980 case FORMAT_HEADER:
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400981 seq_printf(m, "name: %s\n", trace_event_name(call));
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400982 seq_printf(m, "ID: %d\n", call->event.type);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100983 seq_puts(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +0800984 return 0;
985
Li Zefan86397dc2010-08-17 13:53:06 +0800986 case FORMAT_FIELD_SEPERATOR:
987 seq_putc(m, '\n');
988 return 0;
989
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400990 case FORMAT_PRINTFMT:
991 seq_printf(m, "\nprint fmt: %s\n",
992 call->print_fmt);
993 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500994 }
995
Oleg Nesterov7710b632013-07-18 20:47:10 +0200996 field = list_entry(v, struct ftrace_event_field, link);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400997 /*
998 * Smartly shows the array type(except dynamic array).
999 * Normal:
1000 * field:TYPE VAR
1001 * If TYPE := TYPE[LEN], it is shown:
1002 * field:TYPE VAR[LEN]
1003 */
1004 array_descriptor = strchr(field->type, '[');
1005
1006 if (!strncmp(field->type, "__data_loc", 10))
1007 array_descriptor = NULL;
1008
1009 if (!array_descriptor)
1010 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1011 field->type, field->name, field->offset,
1012 field->size, !!field->is_signed);
1013 else
1014 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1015 (int)(array_descriptor - field->type),
1016 field->type, field->name,
1017 array_descriptor, field->offset,
1018 field->size, !!field->is_signed);
1019
1020 return 0;
1021}
1022
Oleg Nesterov7710b632013-07-18 20:47:10 +02001023static void *f_start(struct seq_file *m, loff_t *pos)
1024{
1025 void *p = (void *)FORMAT_HEADER;
1026 loff_t l = 0;
1027
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001028 /* ->stop() is called even if ->start() fails */
1029 mutex_lock(&event_mutex);
1030 if (!event_file_data(m->private))
1031 return ERR_PTR(-ENODEV);
1032
Oleg Nesterov7710b632013-07-18 20:47:10 +02001033 while (l < *pos && p)
1034 p = f_next(m, p, &l);
1035
1036 return p;
1037}
1038
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001039static void f_stop(struct seq_file *m, void *p)
1040{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001041 mutex_unlock(&event_mutex);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001042}
1043
1044static const struct seq_operations trace_format_seq_ops = {
1045 .start = f_start,
1046 .next = f_next,
1047 .stop = f_stop,
1048 .show = f_show,
1049};
1050
1051static int trace_format_open(struct inode *inode, struct file *file)
1052{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001053 struct seq_file *m;
1054 int ret;
1055
1056 ret = seq_open(file, &trace_format_seq_ops);
1057 if (ret < 0)
1058 return ret;
1059
1060 m = file->private_data;
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001061 m->private = file;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001062
1063 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001064}
1065
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001066static ssize_t
1067event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1068{
Oleg Nesterov1a111262013-07-26 19:25:32 +02001069 int id = (long)event_file_data(filp);
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001070 char buf[32];
1071 int len;
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001072
1073 if (*ppos)
1074 return 0;
1075
Oleg Nesterov1a111262013-07-26 19:25:32 +02001076 if (unlikely(!id))
1077 return -ENODEV;
1078
1079 len = sprintf(buf, "%d\n", id);
1080
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001081 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001082}
1083
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001084static ssize_t
1085event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1086 loff_t *ppos)
1087{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001088 struct trace_event_file *file;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001089 struct trace_seq *s;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001090 int r = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001091
1092 if (*ppos)
1093 return 0;
1094
1095 s = kmalloc(sizeof(*s), GFP_KERNEL);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001096
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001097 if (!s)
1098 return -ENOMEM;
1099
1100 trace_seq_init(s);
1101
Oleg Nesterove2912b02013-07-26 19:25:40 +02001102 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001103 file = event_file_data(filp);
1104 if (file)
1105 print_event_filter(file, s);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001106 mutex_unlock(&event_mutex);
1107
Tom Zanussif306cc82013-10-24 08:34:17 -05001108 if (file)
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001109 r = simple_read_from_buffer(ubuf, cnt, ppos,
1110 s->buffer, trace_seq_used(s));
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001111
1112 kfree(s);
1113
1114 return r;
1115}
1116
1117static ssize_t
1118event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1119 loff_t *ppos)
1120{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001121 struct trace_event_file *file;
Tom Zanussi8b372562009-04-28 03:04:59 -05001122 char *buf;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001123 int err = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001124
Tom Zanussi8b372562009-04-28 03:04:59 -05001125 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001126 return -EINVAL;
1127
Tom Zanussi8b372562009-04-28 03:04:59 -05001128 buf = (char *)__get_free_page(GFP_TEMPORARY);
1129 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001130 return -ENOMEM;
1131
Tom Zanussi8b372562009-04-28 03:04:59 -05001132 if (copy_from_user(buf, ubuf, cnt)) {
1133 free_page((unsigned long) buf);
1134 return -EFAULT;
1135 }
1136 buf[cnt] = '\0';
1137
Oleg Nesterove2912b02013-07-26 19:25:40 +02001138 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001139 file = event_file_data(filp);
1140 if (file)
1141 err = apply_event_filter(file, buf);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001142 mutex_unlock(&event_mutex);
1143
Tom Zanussi8b372562009-04-28 03:04:59 -05001144 free_page((unsigned long) buf);
1145 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001146 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -05001147
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001148 *ppos += cnt;
1149
1150 return cnt;
1151}
1152
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001153static LIST_HEAD(event_subsystems);
1154
1155static int subsystem_open(struct inode *inode, struct file *filp)
1156{
1157 struct event_subsystem *system = NULL;
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001158 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001159 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001160 int ret;
1161
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001162 if (tracing_is_disabled())
1163 return -ENODEV;
1164
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001165 /* Make sure the system still exists */
Alexander Z Lama8227412013-07-01 19:37:54 -07001166 mutex_lock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001167 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001168 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1169 list_for_each_entry(dir, &tr->systems, list) {
1170 if (dir == inode->i_private) {
1171 /* Don't open systems with no events */
1172 if (dir->nr_events) {
1173 __get_system_dir(dir);
1174 system = dir->subsystem;
1175 }
1176 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001177 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001178 }
1179 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001180 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001181 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001182 mutex_unlock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001183
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001184 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001185 return -ENODEV;
1186
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001187 /* Some versions of gcc think dir can be uninitialized here */
1188 WARN_ON(!dir);
1189
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001190 /* Still need to increment the ref count of the system */
1191 if (trace_array_get(tr) < 0) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001192 put_system(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001193 return -ENODEV;
1194 }
1195
1196 ret = tracing_open_generic(inode, filp);
1197 if (ret < 0) {
1198 trace_array_put(tr);
1199 put_system(dir);
1200 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001201
1202 return ret;
1203}
1204
1205static int system_tr_open(struct inode *inode, struct file *filp)
1206{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001207 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001208 struct trace_array *tr = inode->i_private;
1209 int ret;
1210
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001211 if (tracing_is_disabled())
1212 return -ENODEV;
1213
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001214 if (trace_array_get(tr) < 0)
1215 return -ENODEV;
1216
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001217 /* Make a temporary dir that has no system but points to tr */
1218 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001219 if (!dir) {
1220 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001221 return -ENOMEM;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001222 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001223
1224 dir->tr = tr;
1225
1226 ret = tracing_open_generic(inode, filp);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001227 if (ret < 0) {
1228 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001229 kfree(dir);
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001230 return ret;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001231 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001232
1233 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001234
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001235 return 0;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001236}
1237
1238static int subsystem_release(struct inode *inode, struct file *file)
1239{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001240 struct trace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001241
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001242 trace_array_put(dir->tr);
1243
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001244 /*
1245 * If dir->subsystem is NULL, then this is a temporary
1246 * descriptor that was made for a trace_array to enable
1247 * all subsystems.
1248 */
1249 if (dir->subsystem)
1250 put_system(dir);
1251 else
1252 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001253
1254 return 0;
1255}
1256
Tom Zanussicfb180f2009-03-22 03:31:17 -05001257static ssize_t
1258subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1259 loff_t *ppos)
1260{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001261 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001262 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001263 struct trace_seq *s;
1264 int r;
1265
1266 if (*ppos)
1267 return 0;
1268
1269 s = kmalloc(sizeof(*s), GFP_KERNEL);
1270 if (!s)
1271 return -ENOMEM;
1272
1273 trace_seq_init(s);
1274
Tom Zanussi8b372562009-04-28 03:04:59 -05001275 print_subsystem_event_filter(system, s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001276 r = simple_read_from_buffer(ubuf, cnt, ppos,
1277 s->buffer, trace_seq_used(s));
Tom Zanussicfb180f2009-03-22 03:31:17 -05001278
1279 kfree(s);
1280
1281 return r;
1282}
1283
1284static ssize_t
1285subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1286 loff_t *ppos)
1287{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001288 struct trace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001289 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001290 int err;
1291
Tom Zanussi8b372562009-04-28 03:04:59 -05001292 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001293 return -EINVAL;
1294
Tom Zanussi8b372562009-04-28 03:04:59 -05001295 buf = (char *)__get_free_page(GFP_TEMPORARY);
1296 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001297 return -ENOMEM;
1298
Tom Zanussi8b372562009-04-28 03:04:59 -05001299 if (copy_from_user(buf, ubuf, cnt)) {
1300 free_page((unsigned long) buf);
1301 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001302 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001303 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001304
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001305 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001306 free_page((unsigned long) buf);
1307 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001308 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001309
1310 *ppos += cnt;
1311
1312 return cnt;
1313}
1314
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001315static ssize_t
1316show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1317{
1318 int (*func)(struct trace_seq *s) = filp->private_data;
1319 struct trace_seq *s;
1320 int r;
1321
1322 if (*ppos)
1323 return 0;
1324
1325 s = kmalloc(sizeof(*s), GFP_KERNEL);
1326 if (!s)
1327 return -ENOMEM;
1328
1329 trace_seq_init(s);
1330
1331 func(s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001332 r = simple_read_from_buffer(ubuf, cnt, ppos,
1333 s->buffer, trace_seq_used(s));
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001334
1335 kfree(s);
1336
1337 return r;
1338}
1339
Steven Rostedt15075ca2012-05-03 14:57:28 -04001340static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1341static int ftrace_event_set_open(struct inode *inode, struct file *file);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001342static int ftrace_event_release(struct inode *inode, struct file *file);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001343
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001344static const struct seq_operations show_event_seq_ops = {
1345 .start = t_start,
1346 .next = t_next,
1347 .show = t_show,
1348 .stop = t_stop,
1349};
1350
1351static const struct seq_operations show_set_event_seq_ops = {
1352 .start = s_start,
1353 .next = s_next,
1354 .show = t_show,
1355 .stop = t_stop,
1356};
1357
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001358static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001359 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001360 .read = seq_read,
1361 .llseek = seq_lseek,
1362 .release = seq_release,
1363};
1364
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001365static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001366 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001367 .read = seq_read,
1368 .write = ftrace_event_write,
1369 .llseek = seq_lseek,
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001370 .release = ftrace_event_release,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001371};
1372
Steven Rostedt1473e442009-02-24 14:15:08 -05001373static const struct file_operations ftrace_enable_fops = {
Oleg Nesterovbf682c32013-07-28 20:35:27 +02001374 .open = tracing_open_generic,
Steven Rostedt1473e442009-02-24 14:15:08 -05001375 .read = event_enable_read,
1376 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001377 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001378};
1379
Steven Rostedt981d0812009-03-02 13:53:59 -05001380static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001381 .open = trace_format_open,
1382 .read = seq_read,
1383 .llseek = seq_lseek,
1384 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001385};
1386
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001387static const struct file_operations ftrace_event_id_fops = {
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001388 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001389 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001390};
1391
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001392static const struct file_operations ftrace_event_filter_fops = {
1393 .open = tracing_open_generic,
1394 .read = event_filter_read,
1395 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001396 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001397};
1398
Tom Zanussicfb180f2009-03-22 03:31:17 -05001399static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001400 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001401 .read = subsystem_filter_read,
1402 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001403 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001404 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001405};
1406
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001407static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001408 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001409 .read = system_enable_read,
1410 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001411 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001412 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001413};
1414
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001415static const struct file_operations ftrace_tr_enable_fops = {
1416 .open = system_tr_open,
1417 .read = system_enable_read,
1418 .write = system_enable_write,
1419 .llseek = default_llseek,
1420 .release = subsystem_release,
1421};
1422
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001423static const struct file_operations ftrace_show_header_fops = {
1424 .open = tracing_open_generic,
1425 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001426 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001427};
1428
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001429static int
1430ftrace_event_open(struct inode *inode, struct file *file,
1431 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001432{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001433 struct seq_file *m;
1434 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001435
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001436 ret = seq_open(file, seq_ops);
1437 if (ret < 0)
1438 return ret;
1439 m = file->private_data;
1440 /* copy tr over to seq ops */
1441 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001442
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001443 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001444}
1445
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001446static int ftrace_event_release(struct inode *inode, struct file *file)
1447{
1448 struct trace_array *tr = inode->i_private;
1449
1450 trace_array_put(tr);
1451
1452 return seq_release(inode, file);
1453}
1454
Steven Rostedt15075ca2012-05-03 14:57:28 -04001455static int
1456ftrace_event_avail_open(struct inode *inode, struct file *file)
1457{
1458 const struct seq_operations *seq_ops = &show_event_seq_ops;
1459
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001460 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001461}
1462
1463static int
1464ftrace_event_set_open(struct inode *inode, struct file *file)
1465{
1466 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001467 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001468 int ret;
1469
1470 if (trace_array_get(tr) < 0)
1471 return -ENODEV;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001472
1473 if ((file->f_mode & FMODE_WRITE) &&
1474 (file->f_flags & O_TRUNC))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001475 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001476
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001477 ret = ftrace_event_open(inode, file, seq_ops);
1478 if (ret < 0)
1479 trace_array_put(tr);
1480 return ret;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001481}
1482
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001483static struct event_subsystem *
1484create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001485{
1486 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001487
1488 /* need to create new entry */
1489 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001490 if (!system)
1491 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001492
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001493 system->ref_count = 1;
Steven Rostedt6e94a782013-06-27 10:58:31 -04001494
1495 /* Only allocate if dynamic (kprobes and modules) */
1496 if (!core_kernel_data((unsigned long)name)) {
1497 system->ref_count |= SYSTEM_FL_FREE_NAME;
1498 system->name = kstrdup(name, GFP_KERNEL);
1499 if (!system->name)
1500 goto out_free;
1501 } else
1502 system->name = name;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001503
Tom Zanussi30e673b2009-04-28 03:04:47 -05001504 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001505
Tom Zanussi8b372562009-04-28 03:04:59 -05001506 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001507 if (!system->filter)
1508 goto out_free;
1509
1510 list_add(&system->list, &event_subsystems);
1511
1512 return system;
1513
1514 out_free:
Steven Rostedt6e94a782013-06-27 10:58:31 -04001515 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1516 kfree(system->name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001517 kfree(system);
1518 return NULL;
1519}
1520
1521static struct dentry *
1522event_subsystem_dir(struct trace_array *tr, const char *name,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001523 struct trace_event_file *file, struct dentry *parent)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001524{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001525 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001526 struct event_subsystem *system;
1527 struct dentry *entry;
1528
1529 /* First see if we did not already create this dir */
1530 list_for_each_entry(dir, &tr->systems, list) {
1531 system = dir->subsystem;
1532 if (strcmp(system->name, name) == 0) {
1533 dir->nr_events++;
1534 file->system = dir;
1535 return dir->entry;
1536 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001537 }
1538
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001539 /* Now see if the system itself exists. */
1540 list_for_each_entry(system, &event_subsystems, list) {
1541 if (strcmp(system->name, name) == 0)
1542 break;
1543 }
1544 /* Reset system variable when not found */
1545 if (&system->list == &event_subsystems)
1546 system = NULL;
1547
1548 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1549 if (!dir)
1550 goto out_fail;
1551
1552 if (!system) {
1553 system = create_new_subsystem(name);
1554 if (!system)
1555 goto out_free;
1556 } else
1557 __get_system(system);
1558
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001559 dir->entry = tracefs_create_dir(name, parent);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001560 if (!dir->entry) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001561 pr_warn("Failed to create system directory %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001562 __put_system(system);
1563 goto out_free;
1564 }
1565
1566 dir->tr = tr;
1567 dir->ref_count = 1;
1568 dir->nr_events = 1;
1569 dir->subsystem = system;
1570 file->system = dir;
1571
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001572 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001573 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001574 if (!entry) {
1575 kfree(system->filter);
1576 system->filter = NULL;
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001577 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001578 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001579
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001580 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001581 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001582
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001583 list_add(&dir->list, &tr->systems);
1584
1585 return dir->entry;
1586
1587 out_free:
1588 kfree(dir);
1589 out_fail:
1590 /* Only print this message if failed on memory allocation */
1591 if (!dir || !system)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001592 pr_warn("No memory to create event subsystem %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001593 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001594}
1595
Steven Rostedt1473e442009-02-24 14:15:08 -05001596static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001597event_create_dir(struct dentry *parent, struct trace_event_file *file)
Steven Rostedt1473e442009-02-24 14:15:08 -05001598{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001599 struct trace_event_call *call = file->event_call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001600 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001601 struct list_head *head;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001602 struct dentry *d_events;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001603 const char *name;
Steven Rostedtfd994982009-02-28 02:41:25 -05001604 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001605
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001606 /*
1607 * If the trace point header did not define TRACE_SYSTEM
1608 * then the system would be called "TRACE_SYSTEM".
1609 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001610 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1611 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1612 if (!d_events)
1613 return -ENOMEM;
1614 } else
1615 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001616
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001617 name = trace_event_name(call);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001618 file->dir = tracefs_create_dir(name, d_events);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001619 if (!file->dir) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001620 pr_warn("Could not create tracefs '%s' directory\n", name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001621 return -1;
1622 }
1623
Steven Rostedt9b637762012-05-10 15:55:43 -04001624 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001625 trace_create_file("enable", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001626 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001627
Steven Rostedt22392912010-04-21 12:27:06 -04001628#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001629 if (call->event.type && call->class->reg)
Oleg Nesterov1a111262013-07-26 19:25:32 +02001630 trace_create_file("id", 0444, file->dir,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001631 (void *)(long)call->event.type,
1632 &ftrace_event_id_fops);
Steven Rostedt22392912010-04-21 12:27:06 -04001633#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001634
Li Zefanc9d932c2010-05-24 16:24:28 +08001635 /*
1636 * Other events may have the same class. Only update
1637 * the fields if they are not already defined.
1638 */
1639 head = trace_get_fields(call);
1640 if (list_empty(head)) {
1641 ret = call->class->define_fields(call);
1642 if (ret < 0) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001643 pr_warn("Could not initialize trace point events/%s\n",
1644 name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001645 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001646 }
1647 }
Tom Zanussif306cc82013-10-24 08:34:17 -05001648 trace_create_file("filter", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001649 &ftrace_event_filter_fops);
Tom Zanussicf027f62009-03-22 03:30:39 -05001650
Tom Zanussi85f2b082013-10-24 08:59:24 -05001651 trace_create_file("trigger", 0644, file->dir, file,
1652 &event_trigger_fops);
1653
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001654 trace_create_file("format", 0444, file->dir, call,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001655 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -05001656
Steven Rostedt1473e442009-02-24 14:15:08 -05001657 return 0;
1658}
1659
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001660static void remove_event_from_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001661{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001662 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001663 struct trace_array *tr;
1664
1665 do_for_each_event_file_safe(tr, file) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001666 if (file->event_call != call)
1667 continue;
1668
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02001669 remove_event_file_dir(file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001670 /*
1671 * The do_for_each_event_file_safe() is
1672 * a double loop. After finding the call for this
1673 * trace_array, we use break to jump to the next
1674 * trace_array.
1675 */
1676 break;
1677 } while_for_each_event_file();
1678}
1679
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001680static void event_remove(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03001681{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001682 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001683 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001684
1685 do_for_each_event_file(tr, file) {
1686 if (file->event_call != call)
1687 continue;
1688 ftrace_event_enable_disable(file, 0);
1689 /*
1690 * The do_for_each_event_file() is
1691 * a double loop. After finding the call for this
1692 * trace_array, we use break to jump to the next
1693 * trace_array.
1694 */
1695 break;
1696 } while_for_each_event_file();
1697
Ezequiel Garcia87819152012-09-12 11:47:57 -03001698 if (call->event.funcs)
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001699 __unregister_trace_event(&call->event);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001700 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001701 list_del(&call->list);
1702}
1703
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001704static int event_init(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03001705{
1706 int ret = 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001707 const char *name;
Ezequiel Garcia87819152012-09-12 11:47:57 -03001708
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001709 name = trace_event_name(call);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001710 if (WARN_ON(!name))
Ezequiel Garcia87819152012-09-12 11:47:57 -03001711 return -EINVAL;
1712
1713 if (call->class->raw_init) {
1714 ret = call->class->raw_init(call);
1715 if (ret < 0 && ret != -ENOSYS)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001716 pr_warn("Could not initialize trace events/%s\n", name);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001717 }
1718
1719 return ret;
1720}
1721
Li Zefan67ead0a2010-05-24 16:25:13 +08001722static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001723__register_event(struct trace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001724{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001725 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001726
Ezequiel Garcia87819152012-09-12 11:47:57 -03001727 ret = event_init(call);
1728 if (ret < 0)
1729 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04001730
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001731 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08001732 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001733
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001734 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001735}
1736
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001737static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
1738{
1739 int rlen;
1740 int elen;
1741
1742 /* Find the length of the enum value as a string */
1743 elen = snprintf(ptr, 0, "%ld", map->enum_value);
1744 /* Make sure there's enough room to replace the string with the value */
1745 if (len < elen)
1746 return NULL;
1747
1748 snprintf(ptr, elen + 1, "%ld", map->enum_value);
1749
1750 /* Get the rest of the string of ptr */
1751 rlen = strlen(ptr + len);
1752 memmove(ptr + elen, ptr + len, rlen);
1753 /* Make sure we end the new string */
1754 ptr[elen + rlen] = 0;
1755
1756 return ptr + elen;
1757}
1758
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001759static void update_event_printk(struct trace_event_call *call,
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001760 struct trace_enum_map *map)
1761{
1762 char *ptr;
1763 int quote = 0;
1764 int len = strlen(map->enum_string);
1765
1766 for (ptr = call->print_fmt; *ptr; ptr++) {
1767 if (*ptr == '\\') {
1768 ptr++;
1769 /* paranoid */
1770 if (!*ptr)
1771 break;
1772 continue;
1773 }
1774 if (*ptr == '"') {
1775 quote ^= 1;
1776 continue;
1777 }
1778 if (quote)
1779 continue;
1780 if (isdigit(*ptr)) {
1781 /* skip numbers */
1782 do {
1783 ptr++;
1784 /* Check for alpha chars like ULL */
1785 } while (isalnum(*ptr));
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04001786 if (!*ptr)
1787 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001788 /*
1789 * A number must have some kind of delimiter after
1790 * it, and we can ignore that too.
1791 */
1792 continue;
1793 }
1794 if (isalpha(*ptr) || *ptr == '_') {
1795 if (strncmp(map->enum_string, ptr, len) == 0 &&
1796 !isalnum(ptr[len]) && ptr[len] != '_') {
1797 ptr = enum_replace(ptr, map, len);
1798 /* Hmm, enum string smaller than value */
1799 if (WARN_ON_ONCE(!ptr))
1800 return;
1801 /*
1802 * No need to decrement here, as enum_replace()
1803 * returns the pointer to the character passed
1804 * the enum, and two enums can not be placed
1805 * back to back without something in between.
1806 * We can skip that something in between.
1807 */
1808 continue;
1809 }
1810 skip_more:
1811 do {
1812 ptr++;
1813 } while (isalnum(*ptr) || *ptr == '_');
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04001814 if (!*ptr)
1815 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001816 /*
1817 * If what comes after this variable is a '.' or
1818 * '->' then we can continue to ignore that string.
1819 */
1820 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
1821 ptr += *ptr == '.' ? 1 : 2;
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04001822 if (!*ptr)
1823 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001824 goto skip_more;
1825 }
1826 /*
1827 * Once again, we can skip the delimiter that came
1828 * after the string.
1829 */
1830 continue;
1831 }
1832 }
1833}
1834
1835void trace_event_enum_update(struct trace_enum_map **map, int len)
1836{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001837 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04001838 const char *last_system = NULL;
1839 int last_i;
1840 int i;
1841
1842 down_write(&trace_event_sem);
1843 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1844 /* events are usually grouped together with systems */
1845 if (!last_system || call->class->system != last_system) {
1846 last_i = 0;
1847 last_system = call->class->system;
1848 }
1849
1850 for (i = last_i; i < len; i++) {
1851 if (call->class->system == map[i]->system) {
1852 /* Save the first system if need be */
1853 if (!last_i)
1854 last_i = i;
1855 update_event_printk(call, map[i]);
1856 }
1857 }
1858 }
1859 up_write(&trace_event_sem);
1860}
1861
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001862static struct trace_event_file *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001863trace_create_new_event(struct trace_event_call *call,
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001864 struct trace_array *tr)
1865{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001866 struct trace_event_file *file;
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001867
1868 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1869 if (!file)
1870 return NULL;
1871
1872 file->event_call = call;
1873 file->tr = tr;
1874 atomic_set(&file->sm_ref, 0);
Tom Zanussi85f2b082013-10-24 08:59:24 -05001875 atomic_set(&file->tm_ref, 0);
1876 INIT_LIST_HEAD(&file->triggers);
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001877 list_add(&file->list, &tr->events);
1878
1879 return file;
1880}
1881
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001882/* Add an event to a trace directory */
1883static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001884__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001885{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001886 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001887
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001888 file = trace_create_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001889 if (!file)
1890 return -ENOMEM;
1891
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001892 return event_create_dir(tr->event_dir, file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001893}
1894
Steven Rostedt77248222013-02-27 16:28:06 -05001895/*
1896 * Just create a decriptor for early init. A descriptor is required
1897 * for enabling events at boot. We want to enable events before
1898 * the filesystem is initialized.
1899 */
1900static __init int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001901__trace_early_add_new_event(struct trace_event_call *call,
Steven Rostedt77248222013-02-27 16:28:06 -05001902 struct trace_array *tr)
1903{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001904 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05001905
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001906 file = trace_create_new_event(call, tr);
Steven Rostedt77248222013-02-27 16:28:06 -05001907 if (!file)
1908 return -ENOMEM;
1909
Steven Rostedt77248222013-02-27 16:28:06 -05001910 return 0;
1911}
1912
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001913struct ftrace_module_file_ops;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001914static void __add_event_to_tracers(struct trace_event_call *call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001915
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001916/* Add an additional event_call dynamically */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001917int trace_add_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001918{
1919 int ret;
Alexander Z Lama8227412013-07-01 19:37:54 -07001920 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001921 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001922
1923 ret = __register_event(call, NULL);
1924 if (ret >= 0)
Oleg Nesterov779c5e32013-07-31 19:31:32 +02001925 __add_event_to_tracers(call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001926
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001927 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001928 mutex_unlock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001929 return ret;
1930}
Steven Rostedt701970b2009-04-24 23:11:22 -04001931
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001932/*
Alexander Z Lama8227412013-07-01 19:37:54 -07001933 * Must be called under locking of trace_types_lock, event_mutex and
1934 * trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001935 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001936static void __trace_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001937{
Ezequiel Garcia87819152012-09-12 11:47:57 -03001938 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001939 trace_destroy_fields(call);
Oleg Nesterov57375742014-07-15 20:48:16 +02001940 free_event_filter(call->filter);
1941 call->filter = NULL;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001942}
1943
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001944static int probe_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001945{
Oleg Nesterov2816c552013-07-29 19:50:33 +02001946 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001947 struct trace_event_file *file;
Oleg Nesterov2816c552013-07-29 19:50:33 +02001948
1949#ifdef CONFIG_PERF_EVENTS
1950 if (call->perf_refcount)
1951 return -EBUSY;
1952#endif
1953 do_for_each_event_file(tr, file) {
1954 if (file->event_call != call)
1955 continue;
1956 /*
1957 * We can't rely on ftrace_event_enable_disable(enable => 0)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001958 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
Oleg Nesterov2816c552013-07-29 19:50:33 +02001959 * TRACE_REG_UNREGISTER.
1960 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001961 if (file->flags & EVENT_FILE_FL_ENABLED)
Oleg Nesterov2816c552013-07-29 19:50:33 +02001962 return -EBUSY;
Steven Rostedt (Red Hat)2ba64032013-07-31 13:16:22 -04001963 /*
1964 * The do_for_each_event_file_safe() is
1965 * a double loop. After finding the call for this
1966 * trace_array, we use break to jump to the next
1967 * trace_array.
1968 */
Oleg Nesterov2816c552013-07-29 19:50:33 +02001969 break;
1970 } while_for_each_event_file();
1971
1972 __trace_remove_event_call(call);
1973
1974 return 0;
1975}
1976
1977/* Remove an event_call */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001978int trace_remove_event_call(struct trace_event_call *call)
Oleg Nesterov2816c552013-07-29 19:50:33 +02001979{
1980 int ret;
1981
Alexander Z Lama8227412013-07-01 19:37:54 -07001982 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001983 mutex_lock(&event_mutex);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001984 down_write(&trace_event_sem);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001985 ret = probe_remove_event_call(call);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001986 up_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001987 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001988 mutex_unlock(&trace_types_lock);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001989
1990 return ret;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001991}
1992
1993#define for_each_event(event, start, end) \
1994 for (event = start; \
1995 (unsigned long)event < (unsigned long)end; \
1996 event++)
1997
1998#ifdef CONFIG_MODULES
1999
Steven Rostedt6d723732009-04-10 14:53:50 -04002000static void trace_module_add_events(struct module *mod)
2001{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002002 struct trace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04002003
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -05002004 if (!mod->num_trace_events)
2005 return;
2006
2007 /* Don't add infrastructure for mods without tracepoints */
2008 if (trace_module_has_bad_taint(mod)) {
2009 pr_err("%s: module has bad taint, not creating trace events\n",
2010 mod->name);
2011 return;
2012 }
2013
Steven Rostedt6d723732009-04-10 14:53:50 -04002014 start = mod->trace_events;
2015 end = mod->trace_events + mod->num_trace_events;
2016
Steven Rostedt6d723732009-04-10 14:53:50 -04002017 for_each_event(call, start, end) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002018 __register_event(*call, mod);
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002019 __add_event_to_tracers(*call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002020 }
2021}
2022
2023static void trace_module_remove_events(struct module *mod)
2024{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002025 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002026 bool clear_trace = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04002027
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002028 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04002029 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2030 if (call->mod == mod) {
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002031 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2032 clear_trace = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002033 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002034 }
2035 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002036 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002037
2038 /*
2039 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002040 * registered any events that were used. The only worry is if
2041 * a new module gets loaded, and takes on the same id as the events
2042 * of this module. When printing out the buffer, traced events left
2043 * over from this module may be passed to the new module events and
2044 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002045 */
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05002046 if (clear_trace)
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002047 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04002048}
2049
Steven Rostedt61f919a2009-04-14 18:22:32 -04002050static int trace_module_notify(struct notifier_block *self,
2051 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04002052{
2053 struct module *mod = data;
2054
Alexander Z Lama8227412013-07-01 19:37:54 -07002055 mutex_lock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002056 mutex_lock(&event_mutex);
2057 switch (val) {
2058 case MODULE_STATE_COMING:
2059 trace_module_add_events(mod);
2060 break;
2061 case MODULE_STATE_GOING:
2062 trace_module_remove_events(mod);
2063 break;
2064 }
2065 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07002066 mutex_unlock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002067
2068 return 0;
2069}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05002070
Oleg Nesterov836d4812013-07-31 19:31:37 +02002071static struct notifier_block trace_module_nb = {
2072 .notifier_call = trace_module_notify,
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002073 .priority = 1, /* higher than trace.c module notify */
Oleg Nesterov836d4812013-07-31 19:31:37 +02002074};
Steven Rostedt61f919a2009-04-14 18:22:32 -04002075#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04002076
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002077/* Create a new event directory structure for a trace directory. */
2078static void
2079__trace_add_event_dirs(struct trace_array *tr)
2080{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002081 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002082 int ret;
2083
2084 list_for_each_entry(call, &ftrace_events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002085 ret = __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002086 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002087 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002088 trace_event_name(call));
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002089 }
2090}
2091
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002092struct trace_event_file *
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002093find_event_file(struct trace_array *tr, const char *system, const char *event)
2094{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002095 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002096 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002097 const char *name;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002098
2099 list_for_each_entry(file, &tr->events, list) {
2100
2101 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002102 name = trace_event_name(call);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002103
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002104 if (!name || !call->class || !call->class->reg)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002105 continue;
2106
2107 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2108 continue;
2109
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002110 if (strcmp(event, name) == 0 &&
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002111 strcmp(system, call->class->system) == 0)
2112 return file;
2113 }
2114 return NULL;
2115}
2116
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002117#ifdef CONFIG_DYNAMIC_FTRACE
2118
2119/* Avoid typos */
2120#define ENABLE_EVENT_STR "enable_event"
2121#define DISABLE_EVENT_STR "disable_event"
2122
2123struct event_probe_data {
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002124 struct trace_event_file *file;
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002125 unsigned long count;
2126 int ref;
2127 bool enable;
2128};
2129
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002130static void
2131event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2132{
2133 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2134 struct event_probe_data *data = *pdata;
2135
2136 if (!data)
2137 return;
2138
2139 if (data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002140 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002141 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002142 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002143}
2144
2145static void
2146event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2147{
2148 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2149 struct event_probe_data *data = *pdata;
2150
2151 if (!data)
2152 return;
2153
2154 if (!data->count)
2155 return;
2156
2157 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002158 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002159 return;
2160
2161 if (data->count != -1)
2162 (data->count)--;
2163
2164 event_enable_probe(ip, parent_ip, _data);
2165}
2166
2167static int
2168event_enable_print(struct seq_file *m, unsigned long ip,
2169 struct ftrace_probe_ops *ops, void *_data)
2170{
2171 struct event_probe_data *data = _data;
2172
2173 seq_printf(m, "%ps:", (void *)ip);
2174
2175 seq_printf(m, "%s:%s:%s",
2176 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2177 data->file->event_call->class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002178 trace_event_name(data->file->event_call));
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002179
2180 if (data->count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002181 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002182 else
2183 seq_printf(m, ":count=%ld\n", data->count);
2184
2185 return 0;
2186}
2187
2188static int
2189event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2190 void **_data)
2191{
2192 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2193 struct event_probe_data *data = *pdata;
2194
2195 data->ref++;
2196 return 0;
2197}
2198
2199static void
2200event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2201 void **_data)
2202{
2203 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2204 struct event_probe_data *data = *pdata;
2205
2206 if (WARN_ON_ONCE(data->ref <= 0))
2207 return;
2208
2209 data->ref--;
2210 if (!data->ref) {
2211 /* Remove the SOFT_MODE flag */
2212 __ftrace_event_enable_disable(data->file, 0, 1);
2213 module_put(data->file->event_call->mod);
2214 kfree(data);
2215 }
2216 *pdata = NULL;
2217}
2218
2219static struct ftrace_probe_ops event_enable_probe_ops = {
2220 .func = event_enable_probe,
2221 .print = event_enable_print,
2222 .init = event_enable_init,
2223 .free = event_enable_free,
2224};
2225
2226static struct ftrace_probe_ops event_enable_count_probe_ops = {
2227 .func = event_enable_count_probe,
2228 .print = event_enable_print,
2229 .init = event_enable_init,
2230 .free = event_enable_free,
2231};
2232
2233static struct ftrace_probe_ops event_disable_probe_ops = {
2234 .func = event_enable_probe,
2235 .print = event_enable_print,
2236 .init = event_enable_init,
2237 .free = event_enable_free,
2238};
2239
2240static struct ftrace_probe_ops event_disable_count_probe_ops = {
2241 .func = event_enable_count_probe,
2242 .print = event_enable_print,
2243 .init = event_enable_init,
2244 .free = event_enable_free,
2245};
2246
2247static int
2248event_enable_func(struct ftrace_hash *hash,
2249 char *glob, char *cmd, char *param, int enabled)
2250{
2251 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002252 struct trace_event_file *file;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002253 struct ftrace_probe_ops *ops;
2254 struct event_probe_data *data;
2255 const char *system;
2256 const char *event;
2257 char *number;
2258 bool enable;
2259 int ret;
2260
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002261 if (!tr)
2262 return -ENODEV;
2263
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002264 /* hash funcs only work with set_ftrace_filter */
Harsh Prateek Bora8092e802013-05-24 12:52:17 +05302265 if (!enabled || !param)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002266 return -EINVAL;
2267
2268 system = strsep(&param, ":");
2269 if (!param)
2270 return -EINVAL;
2271
2272 event = strsep(&param, ":");
2273
2274 mutex_lock(&event_mutex);
2275
2276 ret = -EINVAL;
2277 file = find_event_file(tr, system, event);
2278 if (!file)
2279 goto out;
2280
2281 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2282
2283 if (enable)
2284 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2285 else
2286 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2287
2288 if (glob[0] == '!') {
2289 unregister_ftrace_function_probe_func(glob+1, ops);
2290 ret = 0;
2291 goto out;
2292 }
2293
2294 ret = -ENOMEM;
2295 data = kzalloc(sizeof(*data), GFP_KERNEL);
2296 if (!data)
2297 goto out;
2298
2299 data->enable = enable;
2300 data->count = -1;
2301 data->file = file;
2302
2303 if (!param)
2304 goto out_reg;
2305
2306 number = strsep(&param, ":");
2307
2308 ret = -EINVAL;
2309 if (!strlen(number))
2310 goto out_free;
2311
2312 /*
2313 * We use the callback data field (which is a pointer)
2314 * as our counter.
2315 */
2316 ret = kstrtoul(number, 0, &data->count);
2317 if (ret)
2318 goto out_free;
2319
2320 out_reg:
2321 /* Don't let event modules unload while probe registered */
2322 ret = try_module_get(file->event_call->mod);
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002323 if (!ret) {
2324 ret = -EBUSY;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002325 goto out_free;
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002326 }
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002327
2328 ret = __ftrace_event_enable_disable(file, 1, 1);
2329 if (ret < 0)
2330 goto out_put;
2331 ret = register_ftrace_function_probe(glob, ops, data);
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002332 /*
2333 * The above returns on success the # of functions enabled,
2334 * but if it didn't find any functions it returns zero.
2335 * Consider no functions a failure too.
2336 */
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002337 if (!ret) {
2338 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002339 goto out_disable;
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002340 } else if (ret < 0)
2341 goto out_disable;
2342 /* Just return zero, not the number of enabled functions */
2343 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002344 out:
2345 mutex_unlock(&event_mutex);
2346 return ret;
2347
2348 out_disable:
2349 __ftrace_event_enable_disable(file, 0, 1);
2350 out_put:
2351 module_put(file->event_call->mod);
2352 out_free:
2353 kfree(data);
2354 goto out;
2355}
2356
2357static struct ftrace_func_command event_enable_cmd = {
2358 .name = ENABLE_EVENT_STR,
2359 .func = event_enable_func,
2360};
2361
2362static struct ftrace_func_command event_disable_cmd = {
2363 .name = DISABLE_EVENT_STR,
2364 .func = event_enable_func,
2365};
2366
2367static __init int register_event_cmds(void)
2368{
2369 int ret;
2370
2371 ret = register_ftrace_command(&event_enable_cmd);
2372 if (WARN_ON(ret < 0))
2373 return ret;
2374 ret = register_ftrace_command(&event_disable_cmd);
2375 if (WARN_ON(ret < 0))
2376 unregister_ftrace_command(&event_enable_cmd);
2377 return ret;
2378}
2379#else
2380static inline int register_event_cmds(void) { return 0; }
2381#endif /* CONFIG_DYNAMIC_FTRACE */
2382
Steven Rostedt77248222013-02-27 16:28:06 -05002383/*
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002384 * The top level array has already had its trace_event_file
Steven Rostedt77248222013-02-27 16:28:06 -05002385 * descriptors created in order to allow for early events to
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002386 * be recorded. This function is called after the tracefs has been
Steven Rostedt77248222013-02-27 16:28:06 -05002387 * initialized, and we now have to create the files associated
2388 * to the events.
2389 */
2390static __init void
2391__trace_early_add_event_dirs(struct trace_array *tr)
2392{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002393 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002394 int ret;
2395
2396
2397 list_for_each_entry(file, &tr->events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002398 ret = event_create_dir(tr->event_dir, file);
Steven Rostedt77248222013-02-27 16:28:06 -05002399 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002400 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002401 trace_event_name(file->event_call));
Steven Rostedt77248222013-02-27 16:28:06 -05002402 }
2403}
2404
2405/*
2406 * For early boot up, the top trace array requires to have
2407 * a list of events that can be enabled. This must be done before
2408 * the filesystem is set up in order to allow events to be traced
2409 * early.
2410 */
2411static __init void
2412__trace_early_add_events(struct trace_array *tr)
2413{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002414 struct trace_event_call *call;
Steven Rostedt77248222013-02-27 16:28:06 -05002415 int ret;
2416
2417 list_for_each_entry(call, &ftrace_events, list) {
2418 /* Early boot up should not have any modules loaded */
2419 if (WARN_ON_ONCE(call->mod))
2420 continue;
2421
2422 ret = __trace_early_add_new_event(call, tr);
2423 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002424 pr_warn("Could not create early event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002425 trace_event_name(call));
Steven Rostedt77248222013-02-27 16:28:06 -05002426 }
2427}
2428
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002429/* Remove the event directory structure for a trace directory. */
2430static void
2431__trace_remove_event_dirs(struct trace_array *tr)
2432{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002433 struct trace_event_file *file, *next;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002434
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002435 list_for_each_entry_safe(file, next, &tr->events, list)
2436 remove_event_file_dir(file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002437}
2438
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002439static void __add_event_to_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002440{
2441 struct trace_array *tr;
2442
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002443 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2444 __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002445}
2446
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002447extern struct trace_event_call *__start_ftrace_events[];
2448extern struct trace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002449
Li Zefan020e5f82009-07-01 10:47:05 +08002450static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2451
2452static __init int setup_trace_event(char *str)
2453{
2454 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002455 ring_buffer_expanded = true;
2456 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002457
2458 return 1;
2459}
2460__setup("trace_event=", setup_trace_event);
2461
Steven Rostedt77248222013-02-27 16:28:06 -05002462/* Expects to have event_mutex held when called */
2463static int
2464create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002465{
2466 struct dentry *d_events;
2467 struct dentry *entry;
2468
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002469 entry = tracefs_create_file("set_event", 0644, parent,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002470 tr, &ftrace_set_event_fops);
2471 if (!entry) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002472 pr_warn("Could not create tracefs 'set_event' entry\n");
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002473 return -ENOMEM;
2474 }
2475
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002476 d_events = tracefs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002477 if (!d_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002478 pr_warn("Could not create tracefs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002479 return -ENOMEM;
2480 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002481
2482 /* ring buffer internal formats */
2483 trace_create_file("header_page", 0444, d_events,
2484 ring_buffer_print_page_header,
2485 &ftrace_show_header_fops);
2486
2487 trace_create_file("header_event", 0444, d_events,
2488 ring_buffer_print_entry_header,
2489 &ftrace_show_header_fops);
2490
2491 trace_create_file("enable", 0644, d_events,
2492 tr, &ftrace_tr_enable_fops);
2493
2494 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002495
2496 return 0;
2497}
2498
2499/**
2500 * event_trace_add_tracer - add a instance of a trace_array to events
2501 * @parent: The parent dentry to place the files/directories for events in
2502 * @tr: The trace array associated with these events
2503 *
2504 * When a new instance is created, it needs to set up its events
2505 * directory, as well as other files associated with events. It also
2506 * creates the event hierachry in the @parent/events directory.
2507 *
2508 * Returns 0 on success.
2509 */
2510int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2511{
2512 int ret;
2513
2514 mutex_lock(&event_mutex);
2515
2516 ret = create_event_toplevel_files(parent, tr);
2517 if (ret)
2518 goto out_unlock;
2519
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002520 down_write(&trace_event_sem);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002521 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002522 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002523
Steven Rostedt77248222013-02-27 16:28:06 -05002524 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04002525 mutex_unlock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002526
Steven Rostedt77248222013-02-27 16:28:06 -05002527 return ret;
2528}
2529
2530/*
2531 * The top trace array already had its file descriptors created.
2532 * Now the files themselves need to be created.
2533 */
2534static __init int
2535early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2536{
2537 int ret;
2538
2539 mutex_lock(&event_mutex);
2540
2541 ret = create_event_toplevel_files(parent, tr);
2542 if (ret)
2543 goto out_unlock;
2544
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002545 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002546 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002547 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002548
2549 out_unlock:
2550 mutex_unlock(&event_mutex);
2551
2552 return ret;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002553}
2554
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002555int event_trace_del_tracer(struct trace_array *tr)
2556{
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002557 mutex_lock(&event_mutex);
2558
Tom Zanussi85f2b082013-10-24 08:59:24 -05002559 /* Disable any event triggers and associated soft-disabled events */
2560 clear_event_triggers(tr);
2561
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -04002562 /* Disable any running events */
2563 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2564
Steven Rostedt3ccb0122013-12-03 12:41:20 -05002565 /* Access to events are within rcu_read_lock_sched() */
2566 synchronize_sched();
2567
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002568 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002569 __trace_remove_event_dirs(tr);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002570 tracefs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002571 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002572
2573 tr->event_dir = NULL;
2574
2575 mutex_unlock(&event_mutex);
2576
2577 return 0;
2578}
2579
Steven Rostedtd1a29142013-02-27 20:23:57 -05002580static __init int event_trace_memsetup(void)
2581{
2582 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002583 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
Steven Rostedtd1a29142013-02-27 20:23:57 -05002584 return 0;
2585}
2586
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002587static __init void
2588early_enable_events(struct trace_array *tr, bool disable_first)
2589{
2590 char *buf = bootup_event_buf;
2591 char *token;
2592 int ret;
2593
2594 while (true) {
2595 token = strsep(&buf, ",");
2596
2597 if (!token)
2598 break;
2599 if (!*token)
2600 continue;
2601
2602 /* Restarting syscalls requires that we stop them first */
2603 if (disable_first)
2604 ftrace_set_clr_event(tr, token, 0);
2605
2606 ret = ftrace_set_clr_event(tr, token, 1);
2607 if (ret)
2608 pr_warn("Failed to enable trace event: %s\n", token);
2609
2610 /* Put back the comma to allow this to be called again */
2611 if (buf)
2612 *(buf - 1) = ',';
2613 }
2614}
2615
Ezequiel Garcia87819152012-09-12 11:47:57 -03002616static __init int event_trace_enable(void)
2617{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002618 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002619 struct trace_event_call **iter, *call;
Ezequiel Garcia87819152012-09-12 11:47:57 -03002620 int ret;
2621
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002622 if (!tr)
2623 return -ENODEV;
2624
Ezequiel Garcia87819152012-09-12 11:47:57 -03002625 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2626
2627 call = *iter;
2628 ret = event_init(call);
2629 if (!ret)
2630 list_add(&call->list, &ftrace_events);
2631 }
2632
Steven Rostedt77248222013-02-27 16:28:06 -05002633 /*
2634 * We need the top trace array to have a working set of trace
2635 * points at early init, before the debug files and directories
2636 * are created. Create the file entries now, and attach them
2637 * to the actual file dentries later.
2638 */
2639 __trace_early_add_events(tr);
2640
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002641 early_enable_events(tr, false);
Steven Rostedt81698832012-10-11 10:15:05 -04002642
2643 trace_printk_start_comm();
2644
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002645 register_event_cmds();
2646
Tom Zanussi85f2b082013-10-24 08:59:24 -05002647 register_trigger_cmds();
2648
Ezequiel Garcia87819152012-09-12 11:47:57 -03002649 return 0;
2650}
2651
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05002652/*
2653 * event_trace_enable() is called from trace_event_init() first to
2654 * initialize events and perhaps start any events that are on the
2655 * command line. Unfortunately, there are some events that will not
2656 * start this early, like the system call tracepoints that need
2657 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2658 * is called before pid 1 starts, and this flag is never set, making
2659 * the syscall tracepoint never get reached, but the event is enabled
2660 * regardless (and not doing anything).
2661 */
2662static __init int event_trace_enable_again(void)
2663{
2664 struct trace_array *tr;
2665
2666 tr = top_trace_array();
2667 if (!tr)
2668 return -ENODEV;
2669
2670 early_enable_events(tr, true);
2671
2672 return 0;
2673}
2674
2675early_initcall(event_trace_enable_again);
2676
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002677static __init int event_trace_init(void)
2678{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002679 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002680 struct dentry *d_tracer;
2681 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04002682 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002683
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002684 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002685 if (!tr)
2686 return -ENODEV;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002687
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002688 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05002689 if (IS_ERR(d_tracer))
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002690 return 0;
2691
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002692 entry = tracefs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002693 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002694 if (!entry)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002695 pr_warn("Could not create tracefs 'available_events' entry\n");
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002696
Daniel Wagner9f616682015-08-10 14:35:46 +02002697 if (trace_define_generic_fields())
2698 pr_warn("tracing: Failed to allocated generic fields");
2699
Li Zefan8728fe52010-05-24 16:22:49 +08002700 if (trace_define_common_fields())
Fabian Frederick3448bac2014-06-07 13:43:08 +02002701 pr_warn("tracing: Failed to allocate common fields");
Li Zefan8728fe52010-05-24 16:22:49 +08002702
Steven Rostedt77248222013-02-27 16:28:06 -05002703 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002704 if (ret)
2705 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08002706
Oleg Nesterov836d4812013-07-31 19:31:37 +02002707#ifdef CONFIG_MODULES
Steven Rostedt6d723732009-04-10 14:53:50 -04002708 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08002709 if (ret)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002710 pr_warn("Failed to register trace events module notifier\n");
Oleg Nesterov836d4812013-07-31 19:31:37 +02002711#endif
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002712 return 0;
2713}
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05002714
2715void __init trace_event_init(void)
2716{
2717 event_trace_memsetup();
2718 init_ftrace_syscalls();
2719 event_trace_enable();
2720}
2721
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002722fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002723
2724#ifdef CONFIG_FTRACE_STARTUP_TEST
2725
2726static DEFINE_SPINLOCK(test_spinlock);
2727static DEFINE_SPINLOCK(test_spinlock_irq);
2728static DEFINE_MUTEX(test_mutex);
2729
2730static __init void test_work(struct work_struct *dummy)
2731{
2732 spin_lock(&test_spinlock);
2733 spin_lock_irq(&test_spinlock_irq);
2734 udelay(1);
2735 spin_unlock_irq(&test_spinlock_irq);
2736 spin_unlock(&test_spinlock);
2737
2738 mutex_lock(&test_mutex);
2739 msleep(1);
2740 mutex_unlock(&test_mutex);
2741}
2742
2743static __init int event_test_thread(void *unused)
2744{
2745 void *test_malloc;
2746
2747 test_malloc = kmalloc(1234, GFP_KERNEL);
2748 if (!test_malloc)
2749 pr_info("failed to kmalloc\n");
2750
2751 schedule_on_each_cpu(test_work);
2752
2753 kfree(test_malloc);
2754
2755 set_current_state(TASK_INTERRUPTIBLE);
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02002756 while (!kthread_should_stop()) {
Steven Rostedte6187002009-04-15 13:36:40 -04002757 schedule();
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02002758 set_current_state(TASK_INTERRUPTIBLE);
2759 }
2760 __set_current_state(TASK_RUNNING);
Steven Rostedte6187002009-04-15 13:36:40 -04002761
2762 return 0;
2763}
2764
2765/*
2766 * Do various things that may trigger events.
2767 */
2768static __init void event_test_stuff(void)
2769{
2770 struct task_struct *test_thread;
2771
2772 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2773 msleep(1);
2774 kthread_stop(test_thread);
2775}
2776
2777/*
2778 * For every trace event defined, we will test each trace point separately,
2779 * and then by groups, and finally all trace points.
2780 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002781static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04002782{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04002783 struct trace_subsystem_dir *dir;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002784 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002785 struct trace_event_call *call;
Steven Rostedte6187002009-04-15 13:36:40 -04002786 struct event_subsystem *system;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002787 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04002788 int ret;
2789
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002790 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002791 if (!tr)
2792 return;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002793
Steven Rostedte6187002009-04-15 13:36:40 -04002794 pr_info("Running tests on trace events:\n");
2795
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002796 list_for_each_entry(file, &tr->events, list) {
2797
2798 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04002799
Steven Rostedt22392912010-04-21 12:27:06 -04002800 /* Only test those that have a probe */
2801 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04002802 continue;
2803
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002804/*
2805 * Testing syscall events here is pretty useless, but
2806 * we still do it if configured. But this is time consuming.
2807 * What we really need is a user thread to perform the
2808 * syscalls as we test.
2809 */
2810#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04002811 if (call->class->system &&
2812 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002813 continue;
2814#endif
2815
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002816 pr_info("Testing event %s: ", trace_event_name(call));
Steven Rostedte6187002009-04-15 13:36:40 -04002817
2818 /*
2819 * If an event is already enabled, someone is using
2820 * it and the self test should not be on.
2821 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002822 if (file->flags & EVENT_FILE_FL_ENABLED) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002823 pr_warn("Enabled event during self test!\n");
Steven Rostedte6187002009-04-15 13:36:40 -04002824 WARN_ON_ONCE(1);
2825 continue;
2826 }
2827
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002828 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002829 event_test_stuff();
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002830 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002831
2832 pr_cont("OK\n");
2833 }
2834
2835 /* Now test at the sub system level */
2836
2837 pr_info("Running tests on trace event systems:\n");
2838
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002839 list_for_each_entry(dir, &tr->systems, list) {
2840
2841 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04002842
2843 /* the ftrace system is special, skip it */
2844 if (strcmp(system->name, "ftrace") == 0)
2845 continue;
2846
2847 pr_info("Testing event system %s: ", system->name);
2848
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002849 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002850 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002851 pr_warn("error enabling system %s\n",
2852 system->name);
Steven Rostedte6187002009-04-15 13:36:40 -04002853 continue;
2854 }
2855
2856 event_test_stuff();
2857
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002858 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002859 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002860 pr_warn("error disabling system %s\n",
2861 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002862 continue;
2863 }
Steven Rostedte6187002009-04-15 13:36:40 -04002864
2865 pr_cont("OK\n");
2866 }
2867
2868 /* Test with all events enabled */
2869
2870 pr_info("Running tests on all trace events:\n");
2871 pr_info("Testing all events: ");
2872
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002873 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002874 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002875 pr_warn("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002876 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002877 }
2878
2879 event_test_stuff();
2880
2881 /* reset sysname */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002882 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002883 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002884 pr_warn("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002885 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002886 }
2887
2888 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002889}
2890
2891#ifdef CONFIG_FUNCTION_TRACER
2892
Tejun Heo245b2e72009-06-24 15:13:48 +09002893static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002894
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002895static struct trace_array *event_tr;
2896
2897static void __init
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002898function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002899 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002900{
2901 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002902 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002903 struct ftrace_entry *entry;
2904 unsigned long flags;
2905 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002906 int cpu;
2907 int pc;
2908
2909 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002910 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002911 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09002912 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002913
2914 if (disabled != 1)
2915 goto out;
2916
2917 local_save_flags(flags);
2918
Steven Rostedte77405a2009-09-02 14:17:06 -04002919 event = trace_current_buffer_lock_reserve(&buffer,
2920 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002921 flags, pc);
2922 if (!event)
2923 goto out;
2924 entry = ring_buffer_event_data(event);
2925 entry->ip = ip;
2926 entry->parent_ip = parent_ip;
2927
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002928 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002929
2930 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09002931 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04002932 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002933}
2934
2935static struct ftrace_ops trace_ops __initdata =
2936{
2937 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04002938 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002939};
2940
2941static __init void event_trace_self_test_with_function(void)
2942{
Steven Rostedt17bb6152011-05-23 15:27:46 -04002943 int ret;
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002944 event_tr = top_trace_array();
2945 if (WARN_ON(!event_tr))
2946 return;
Steven Rostedt17bb6152011-05-23 15:27:46 -04002947 ret = register_ftrace_function(&trace_ops);
2948 if (WARN_ON(ret < 0)) {
2949 pr_info("Failed to enable function tracer for event tests\n");
2950 return;
2951 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002952 pr_info("Running tests again, along with the function tracer\n");
2953 event_trace_self_tests();
2954 unregister_ftrace_function(&trace_ops);
2955}
2956#else
2957static __init void event_trace_self_test_with_function(void)
2958{
2959}
2960#endif
2961
2962static __init int event_trace_self_tests_init(void)
2963{
Li Zefan020e5f82009-07-01 10:47:05 +08002964 if (!tracing_selftest_disabled) {
2965 event_trace_self_tests();
2966 event_trace_self_test_with_function();
2967 }
Steven Rostedte6187002009-04-15 13:36:40 -04002968
2969 return 0;
2970}
2971
Steven Rostedt28d20e22009-04-20 12:12:44 -04002972late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002973
2974#endif