blob: 366a78a3e61e21a94c06aa96f5b5c02a72c41e78 [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 Rostedtb77e38a2009-02-24 10:21:36 -050016#include <linux/debugfs.h>
17#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);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080033static LIST_HEAD(ftrace_common_fields);
Steven Rostedta59fd602009-04-10 13:52:20 -040034
Steven Rostedtd1a29142013-02-27 20:23:57 -050035#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
36
37static struct kmem_cache *field_cachep;
38static struct kmem_cache *file_cachep;
39
Steven Rostedt6e94a782013-06-27 10:58:31 -040040#define SYSTEM_FL_FREE_NAME (1 << 31)
41
42static inline int system_refcount(struct event_subsystem *system)
43{
44 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
45}
46
47static int system_refcount_inc(struct event_subsystem *system)
48{
49 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
50}
51
52static int system_refcount_dec(struct event_subsystem *system)
53{
54 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
55}
56
Steven Rostedtae63b312012-05-03 23:09:03 -040057/* Double loops, do not use break, only goto's work */
58#define do_for_each_event_file(tr, file) \
59 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
60 list_for_each_entry(file, &tr->events, list)
61
62#define do_for_each_event_file_safe(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 struct ftrace_event_file *___n; \
65 list_for_each_entry_safe(file, ___n, &tr->events, list)
66
67#define while_for_each_event_file() \
68 }
69
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080070static struct list_head *
Steven Rostedt2e33af02010-04-22 10:35:55 -040071trace_get_fields(struct ftrace_event_call *event_call)
72{
73 if (!event_call->class->get_fields)
74 return &event_call->class->fields;
75 return event_call->class->get_fields(event_call);
76}
77
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080078static struct ftrace_event_field *
79__find_event_field(struct list_head *head, char *name)
80{
81 struct ftrace_event_field *field;
82
83 list_for_each_entry(field, head, link) {
84 if (!strcmp(field->name, name))
85 return field;
86 }
87
88 return NULL;
89}
90
91struct ftrace_event_field *
92trace_find_event_field(struct ftrace_event_call *call, char *name)
93{
94 struct ftrace_event_field *field;
95 struct list_head *head;
96
97 field = __find_event_field(&ftrace_common_fields, name);
98 if (field)
99 return field;
100
101 head = trace_get_fields(call);
102 return __find_event_field(head, name);
103}
104
Li Zefan8728fe52010-05-24 16:22:49 +0800105static int __trace_define_field(struct list_head *head, const char *type,
106 const char *name, int offset, int size,
107 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -0500108{
109 struct ftrace_event_field *field;
110
Steven Rostedtd1a29142013-02-27 20:23:57 -0500111 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -0500112 if (!field)
Namhyung Kimaaf6ac02013-06-07 15:07:48 +0900113 return -ENOMEM;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +0100114
Steven Rostedt92edca02013-02-27 20:41:37 -0500115 field->name = name;
116 field->type = type;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +0100117
Li Zefan43b51ea2009-08-07 10:33:22 +0800118 if (filter_type == FILTER_OTHER)
119 field->filter_type = filter_assign_type(type);
120 else
121 field->filter_type = filter_type;
122
Tom Zanussicf027f62009-03-22 03:30:39 -0500123 field->offset = offset;
124 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -0500125 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +0800126
Steven Rostedt2e33af02010-04-22 10:35:55 -0400127 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -0500128
129 return 0;
Tom Zanussicf027f62009-03-22 03:30:39 -0500130}
Li Zefan8728fe52010-05-24 16:22:49 +0800131
132int trace_define_field(struct ftrace_event_call *call, const char *type,
133 const char *name, int offset, int size, int is_signed,
134 int filter_type)
135{
136 struct list_head *head;
137
138 if (WARN_ON(!call->class))
139 return 0;
140
141 head = trace_get_fields(call);
142 return __trace_define_field(head, type, name, offset, size,
143 is_signed, filter_type);
144}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400145EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500146
Li Zefane647d6b2009-08-19 15:54:32 +0800147#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800148 ret = __trace_define_field(&ftrace_common_fields, #type, \
149 "common_" #item, \
150 offsetof(typeof(ent), item), \
151 sizeof(ent.item), \
152 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800153 if (ret) \
154 return ret;
155
Li Zefan8728fe52010-05-24 16:22:49 +0800156static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800157{
158 int ret;
159 struct trace_entry ent;
160
161 __common_field(unsigned short, type);
162 __common_field(unsigned char, flags);
163 __common_field(unsigned char, preempt_count);
164 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800165
166 return ret;
167}
168
zhangwei(Jovi)ad7067c2013-03-11 15:13:46 +0800169static void trace_destroy_fields(struct ftrace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800170{
171 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400172 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800173
Steven Rostedt2e33af02010-04-22 10:35:55 -0400174 head = trace_get_fields(call);
175 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800176 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500177 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800178 }
179}
180
Li Zefan87d9b4e2009-12-08 11:14:20 +0800181int trace_event_raw_init(struct ftrace_event_call *call)
182{
183 int id;
184
Steven Rostedt80decc72010-04-23 10:00:22 -0400185 id = register_ftrace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800186 if (!id)
187 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800188
189 return 0;
190}
191EXPORT_SYMBOL_GPL(trace_event_raw_init);
192
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400193void *ftrace_event_buffer_reserve(struct ftrace_event_buffer *fbuffer,
194 struct ftrace_event_file *ftrace_file,
195 unsigned long len)
196{
197 struct ftrace_event_call *event_call = ftrace_file->event_call;
198
199 local_save_flags(fbuffer->flags);
200 fbuffer->pc = preempt_count();
201 fbuffer->ftrace_file = ftrace_file;
202
203 fbuffer->event =
204 trace_event_buffer_lock_reserve(&fbuffer->buffer, ftrace_file,
205 event_call->event.type, len,
206 fbuffer->flags, fbuffer->pc);
207 if (!fbuffer->event)
208 return NULL;
209
210 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
211 return fbuffer->entry;
212}
213EXPORT_SYMBOL_GPL(ftrace_event_buffer_reserve);
214
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500215static DEFINE_SPINLOCK(tracepoint_iter_lock);
216
217static void output_printk(struct ftrace_event_buffer *fbuffer)
218{
219 struct ftrace_event_call *event_call;
220 struct trace_event *event;
221 unsigned long flags;
222 struct trace_iterator *iter = tracepoint_print_iter;
223
224 if (!iter)
225 return;
226
227 event_call = fbuffer->ftrace_file->event_call;
228 if (!event_call || !event_call->event.funcs ||
229 !event_call->event.funcs->trace)
230 return;
231
232 event = &fbuffer->ftrace_file->event_call->event;
233
234 spin_lock_irqsave(&tracepoint_iter_lock, flags);
235 trace_seq_init(&iter->seq);
236 iter->ent = fbuffer->entry;
237 event_call->event.funcs->trace(iter, 0, event);
238 trace_seq_putc(&iter->seq, 0);
239 printk("%s", iter->seq.buffer);
240
241 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
242}
243
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400244void ftrace_event_buffer_commit(struct ftrace_event_buffer *fbuffer)
245{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500246 if (tracepoint_printk)
247 output_printk(fbuffer);
248
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400249 event_trigger_unlock_commit(fbuffer->ftrace_file, fbuffer->buffer,
250 fbuffer->event, fbuffer->entry,
251 fbuffer->flags, fbuffer->pc);
252}
253EXPORT_SYMBOL_GPL(ftrace_event_buffer_commit);
254
Jiri Olsaceec0b62012-02-15 15:51:49 +0100255int ftrace_event_reg(struct ftrace_event_call *call,
256 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400257{
Steven Rostedtae63b312012-05-03 23:09:03 -0400258 struct ftrace_event_file *file = data;
259
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400260 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400261 switch (type) {
262 case TRACE_REG_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400263 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400264 call->class->probe,
Steven Rostedtae63b312012-05-03 23:09:03 -0400265 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400266 case TRACE_REG_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400267 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400268 call->class->probe,
Steven Rostedtae63b312012-05-03 23:09:03 -0400269 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400270 return 0;
271
272#ifdef CONFIG_PERF_EVENTS
273 case TRACE_REG_PERF_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400274 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400275 call->class->perf_probe,
276 call);
277 case TRACE_REG_PERF_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400278 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400279 call->class->perf_probe,
280 call);
281 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100282 case TRACE_REG_PERF_OPEN:
283 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100284 case TRACE_REG_PERF_ADD:
285 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100286 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400287#endif
288 }
289 return 0;
290}
291EXPORT_SYMBOL_GPL(ftrace_event_reg);
292
Li Zefane870e9a2010-07-02 11:07:32 +0800293void trace_event_enable_cmd_record(bool enable)
294{
Steven Rostedtae63b312012-05-03 23:09:03 -0400295 struct ftrace_event_file *file;
296 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800297
298 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400299 do_for_each_event_file(tr, file) {
300
301 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800302 continue;
303
304 if (enable) {
305 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400306 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800307 } else {
308 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400309 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800310 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400311 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800312 mutex_unlock(&event_mutex);
313}
314
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400315static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
316 int enable, int soft_disable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500317{
Steven Rostedtae63b312012-05-03 23:09:03 -0400318 struct ftrace_event_call *call = file->event_call;
Li Zefan3b8e4272009-12-08 11:14:52 +0800319 int ret = 0;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400320 int disable;
Li Zefan3b8e4272009-12-08 11:14:52 +0800321
Steven Rostedtfd994982009-02-28 02:41:25 -0500322 switch (enable) {
323 case 0:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400324 /*
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900325 * When soft_disable is set and enable is cleared, the sm_ref
326 * reference counter is decremented. If it reaches 0, we want
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400327 * to clear the SOFT_DISABLED flag but leave the event in the
328 * state that it was. That is, if the event was enabled and
329 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
330 * is set we do not want the event to be enabled before we
331 * clear the bit.
332 *
333 * When soft_disable is not set but the SOFT_MODE flag is,
334 * we do nothing. Do not disable the tracepoint, otherwise
335 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
336 */
337 if (soft_disable) {
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900338 if (atomic_dec_return(&file->sm_ref) > 0)
339 break;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400340 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
341 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
342 } else
343 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
344
345 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
346 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
Steven Rostedtae63b312012-05-03 23:09:03 -0400347 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800348 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400349 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800350 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400351 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500352 }
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500353 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400354 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
355 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500356 else
357 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500358 break;
359 case 1:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400360 /*
361 * When soft_disable is set and enable is set, we want to
362 * register the tracepoint for the event, but leave the event
363 * as is. That means, if the event was already enabled, we do
364 * nothing (but set SOFT_MODE). If the event is disabled, we
365 * set SOFT_DISABLED before enabling the event tracepoint, so
366 * it still seems to be disabled.
367 */
368 if (!soft_disable)
369 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900370 else {
371 if (atomic_inc_return(&file->sm_ref) > 1)
372 break;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400373 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900374 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400375
Steven Rostedtae63b312012-05-03 23:09:03 -0400376 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400377
378 /* Keep the event disabled, when going to SOFT_MODE. */
379 if (soft_disable)
380 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
381
Li Zefane870e9a2010-07-02 11:07:32 +0800382 if (trace_flags & TRACE_ITER_RECORD_CMD) {
383 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400384 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800385 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400386 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800387 if (ret) {
388 tracing_stop_cmdline_record();
389 pr_info("event trace: Could not enable event "
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400390 "%s\n", ftrace_event_name(call));
Li Zefan3b8e4272009-12-08 11:14:52 +0800391 break;
392 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400393 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -0500394
395 /* WAS_ENABLED gets set but never cleared. */
396 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
Steven Rostedtfd994982009-02-28 02:41:25 -0500397 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500398 break;
399 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800400
401 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500402}
403
Tom Zanussi85f2b082013-10-24 08:59:24 -0500404int trace_event_enable_disable(struct ftrace_event_file *file,
405 int enable, int soft_disable)
406{
407 return __ftrace_event_enable_disable(file, enable, soft_disable);
408}
409
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400410static int ftrace_event_enable_disable(struct ftrace_event_file *file,
411 int enable)
412{
413 return __ftrace_event_enable_disable(file, enable, 0);
414}
415
Steven Rostedtae63b312012-05-03 23:09:03 -0400416static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800417{
Steven Rostedtae63b312012-05-03 23:09:03 -0400418 struct ftrace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800419
420 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400421 list_for_each_entry(file, &tr->events, list) {
422 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800423 }
424 mutex_unlock(&event_mutex);
425}
426
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400427static void __put_system(struct event_subsystem *system)
428{
429 struct event_filter *filter = system->filter;
430
Steven Rostedt6e94a782013-06-27 10:58:31 -0400431 WARN_ON_ONCE(system_refcount(system) == 0);
432 if (system_refcount_dec(system))
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400433 return;
434
Steven Rostedtae63b312012-05-03 23:09:03 -0400435 list_del(&system->list);
436
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400437 if (filter) {
438 kfree(filter->filter_string);
439 kfree(filter);
440 }
Steven Rostedt6e94a782013-06-27 10:58:31 -0400441 if (system->ref_count & SYSTEM_FL_FREE_NAME)
442 kfree(system->name);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400443 kfree(system);
444}
445
446static void __get_system(struct event_subsystem *system)
447{
Steven Rostedt6e94a782013-06-27 10:58:31 -0400448 WARN_ON_ONCE(system_refcount(system) == 0);
449 system_refcount_inc(system);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400450}
451
Steven Rostedtae63b312012-05-03 23:09:03 -0400452static void __get_system_dir(struct ftrace_subsystem_dir *dir)
453{
454 WARN_ON_ONCE(dir->ref_count == 0);
455 dir->ref_count++;
456 __get_system(dir->subsystem);
457}
458
459static void __put_system_dir(struct ftrace_subsystem_dir *dir)
460{
461 WARN_ON_ONCE(dir->ref_count == 0);
462 /* If the subsystem is about to be freed, the dir must be too */
Steven Rostedt6e94a782013-06-27 10:58:31 -0400463 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
Steven Rostedtae63b312012-05-03 23:09:03 -0400464
465 __put_system(dir->subsystem);
466 if (!--dir->ref_count)
467 kfree(dir);
468}
469
470static void put_system(struct ftrace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400471{
472 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400473 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400474 mutex_unlock(&event_mutex);
475}
476
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200477static void remove_subsystem(struct ftrace_subsystem_dir *dir)
478{
479 if (!dir)
480 return;
481
482 if (!--dir->nr_events) {
483 debugfs_remove_recursive(dir->entry);
484 list_del(&dir->list);
485 __put_system_dir(dir);
486 }
487}
488
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200489static void remove_event_file_dir(struct ftrace_event_file *file)
490{
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200491 struct dentry *dir = file->dir;
492 struct dentry *child;
493
494 if (dir) {
495 spin_lock(&dir->d_lock); /* probably unneeded */
Al Viro946e51f2014-10-26 19:19:16 -0400496 list_for_each_entry(child, &dir->d_subdirs, d_child) {
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200497 if (child->d_inode) /* probably unneeded */
498 child->d_inode->i_private = NULL;
499 }
500 spin_unlock(&dir->d_lock);
501
502 debugfs_remove_recursive(dir);
503 }
504
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200505 list_del(&file->list);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200506 remove_subsystem(file->system);
Oleg Nesterov2448e342014-07-11 21:06:38 +0200507 free_event_filter(file->filter);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200508 kmem_cache_free(file_cachep, file);
509}
510
Li Zefan8f31bfe2009-05-08 10:31:42 +0800511/*
512 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
513 */
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400514static int
515__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
516 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500517{
Steven Rostedtae63b312012-05-03 23:09:03 -0400518 struct ftrace_event_file *file;
Steven Rostedta59fd602009-04-10 13:52:20 -0400519 struct ftrace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400520 const char *name;
Steven Rostedt29f93942009-05-08 16:06:47 -0400521 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500522
Steven Rostedtae63b312012-05-03 23:09:03 -0400523 list_for_each_entry(file, &tr->events, list) {
524
525 call = file->event_call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400526 name = ftrace_event_name(call);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500527
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400528 if (!name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500529 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500530
Steven Rostedt9b637762012-05-10 15:55:43 -0400531 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
532 continue;
533
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500534 if (match &&
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400535 strcmp(match, name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400536 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500537 continue;
538
Steven Rostedt8f082012010-04-20 10:47:33 -0400539 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500540 continue;
541
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400542 if (event && strcmp(event, name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500543 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500544
Steven Rostedtae63b312012-05-03 23:09:03 -0400545 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500546
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500547 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500548 }
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400549
550 return ret;
551}
552
553static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
554 const char *sub, const char *event, int set)
555{
556 int ret;
557
558 mutex_lock(&event_mutex);
559 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500560 mutex_unlock(&event_mutex);
561
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500562 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500563}
564
Steven Rostedtae63b312012-05-03 23:09:03 -0400565static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800566{
567 char *event = NULL, *sub = NULL, *match;
568
569 /*
570 * The buf format can be <subsystem>:<event-name>
571 * *:<event-name> means any event by that name.
572 * :<event-name> is the same.
573 *
574 * <subsystem>:* means all events in that subsystem
575 * <subsystem>: means the same.
576 *
577 * <name> (no ':') means all events in a subsystem with
578 * the name <name> or any event that matches <name>
579 */
580
581 match = strsep(&buf, ":");
582 if (buf) {
583 sub = match;
584 event = buf;
585 match = NULL;
586
587 if (!strlen(sub) || strcmp(sub, "*") == 0)
588 sub = NULL;
589 if (!strlen(event) || strcmp(event, "*") == 0)
590 event = NULL;
591 }
592
Steven Rostedtae63b312012-05-03 23:09:03 -0400593 return __ftrace_set_clr_event(tr, match, sub, event, set);
Li Zefan8f31bfe2009-05-08 10:31:42 +0800594}
595
Steven Rostedt4671c792009-05-08 16:27:41 -0400596/**
597 * trace_set_clr_event - enable or disable an event
598 * @system: system name to match (NULL for any system)
599 * @event: event name to match (NULL for all events, within system)
600 * @set: 1 to enable, 0 to disable
601 *
602 * This is a way for other parts of the kernel to enable or disable
603 * event recording.
604 *
605 * Returns 0 on success, -EINVAL if the parameters do not match any
606 * registered events.
607 */
608int trace_set_clr_event(const char *system, const char *event, int set)
609{
Steven Rostedtae63b312012-05-03 23:09:03 -0400610 struct trace_array *tr = top_trace_array();
611
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +0900612 if (!tr)
613 return -ENODEV;
614
Steven Rostedtae63b312012-05-03 23:09:03 -0400615 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400616}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800617EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400618
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500619/* 128 should be much more than enough */
620#define EVENT_BUF_SIZE 127
621
622static ssize_t
623ftrace_event_write(struct file *file, const char __user *ubuf,
624 size_t cnt, loff_t *ppos)
625{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200626 struct trace_parser parser;
Steven Rostedtae63b312012-05-03 23:09:03 -0400627 struct seq_file *m = file->private_data;
628 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800629 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500630
Li Zefan4ba79782009-09-22 13:52:20 +0800631 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500632 return 0;
633
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400634 ret = tracing_update_buffers();
635 if (ret < 0)
636 return ret;
637
jolsa@redhat.com48966362009-09-11 17:29:28 +0200638 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500639 return -ENOMEM;
640
jolsa@redhat.com48966362009-09-11 17:29:28 +0200641 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500642
Li Zefan4ba79782009-09-22 13:52:20 +0800643 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200644 int set = 1;
645
646 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500647 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500648
jolsa@redhat.com48966362009-09-11 17:29:28 +0200649 parser.buffer[parser.idx] = 0;
650
Steven Rostedtae63b312012-05-03 23:09:03 -0400651 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500652 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200653 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500654 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500655
656 ret = read;
657
jolsa@redhat.com48966362009-09-11 17:29:28 +0200658 out_put:
659 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500660
661 return ret;
662}
663
664static void *
665t_next(struct seq_file *m, void *v, loff_t *pos)
666{
Steven Rostedtae63b312012-05-03 23:09:03 -0400667 struct ftrace_event_file *file = v;
668 struct ftrace_event_call *call;
669 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500670
671 (*pos)++;
672
Steven Rostedtae63b312012-05-03 23:09:03 -0400673 list_for_each_entry_continue(file, &tr->events, list) {
674 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400675 /*
676 * The ftrace subsystem is for showing formats only.
677 * They can not be enabled or disabled via the event files.
678 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400679 if (call->class && call->class->reg)
Steven Rostedtae63b312012-05-03 23:09:03 -0400680 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400681 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500682
Li Zefan30bd39c2009-09-18 14:07:05 +0800683 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500684}
685
686static void *t_start(struct seq_file *m, loff_t *pos)
687{
Steven Rostedtae63b312012-05-03 23:09:03 -0400688 struct ftrace_event_file *file;
689 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800690 loff_t l;
691
Li Zefan20c89282009-05-06 10:33:45 +0800692 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800693
Steven Rostedtae63b312012-05-03 23:09:03 -0400694 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800695 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b312012-05-03 23:09:03 -0400696 file = t_next(m, file, &l);
697 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800698 break;
699 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400700 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500701}
702
703static void *
704s_next(struct seq_file *m, void *v, loff_t *pos)
705{
Steven Rostedtae63b312012-05-03 23:09:03 -0400706 struct ftrace_event_file *file = v;
707 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500708
709 (*pos)++;
710
Steven Rostedtae63b312012-05-03 23:09:03 -0400711 list_for_each_entry_continue(file, &tr->events, list) {
712 if (file->flags & FTRACE_EVENT_FL_ENABLED)
713 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500714 }
715
Li Zefan30bd39c2009-09-18 14:07:05 +0800716 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500717}
718
719static void *s_start(struct seq_file *m, loff_t *pos)
720{
Steven Rostedtae63b312012-05-03 23:09:03 -0400721 struct ftrace_event_file *file;
722 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800723 loff_t l;
724
Li Zefan20c89282009-05-06 10:33:45 +0800725 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800726
Steven Rostedtae63b312012-05-03 23:09:03 -0400727 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800728 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b312012-05-03 23:09:03 -0400729 file = s_next(m, file, &l);
730 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800731 break;
732 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400733 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500734}
735
736static int t_show(struct seq_file *m, void *v)
737{
Steven Rostedtae63b312012-05-03 23:09:03 -0400738 struct ftrace_event_file *file = v;
739 struct ftrace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500740
Steven Rostedt8f082012010-04-20 10:47:33 -0400741 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
742 seq_printf(m, "%s:", call->class->system);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400743 seq_printf(m, "%s\n", ftrace_event_name(call));
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500744
745 return 0;
746}
747
748static void t_stop(struct seq_file *m, void *p)
749{
Li Zefan20c89282009-05-06 10:33:45 +0800750 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500751}
752
Steven Rostedt1473e442009-02-24 14:15:08 -0500753static ssize_t
754event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
755 loff_t *ppos)
756{
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200757 struct ftrace_event_file *file;
758 unsigned long flags;
Tom Zanussia4390592013-06-29 00:08:04 -0500759 char buf[4] = "0";
Steven Rostedt1473e442009-02-24 14:15:08 -0500760
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200761 mutex_lock(&event_mutex);
762 file = event_file_data(filp);
763 if (likely(file))
764 flags = file->flags;
765 mutex_unlock(&event_mutex);
766
767 if (!file)
768 return -ENODEV;
769
770 if (flags & FTRACE_EVENT_FL_ENABLED &&
771 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
Tom Zanussia4390592013-06-29 00:08:04 -0500772 strcpy(buf, "1");
773
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200774 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
775 flags & FTRACE_EVENT_FL_SOFT_MODE)
Tom Zanussia4390592013-06-29 00:08:04 -0500776 strcat(buf, "*");
777
778 strcat(buf, "\n");
Steven Rostedt1473e442009-02-24 14:15:08 -0500779
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400780 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -0500781}
782
783static ssize_t
784event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
785 loff_t *ppos)
786{
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200787 struct ftrace_event_file *file;
Steven Rostedt1473e442009-02-24 14:15:08 -0500788 unsigned long val;
789 int ret;
790
Peter Huewe22fe9b52011-06-07 21:58:27 +0200791 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
792 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -0500793 return ret;
794
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400795 ret = tracing_update_buffers();
796 if (ret < 0)
797 return ret;
798
Steven Rostedt1473e442009-02-24 14:15:08 -0500799 switch (val) {
800 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500801 case 1:
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200802 ret = -ENODEV;
Steven Rostedt11a241a2009-03-02 11:49:04 -0500803 mutex_lock(&event_mutex);
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200804 file = event_file_data(filp);
805 if (likely(file))
806 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500807 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500808 break;
809
810 default:
811 return -EINVAL;
812 }
813
814 *ppos += cnt;
815
Li Zefan3b8e4272009-12-08 11:14:52 +0800816 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500817}
818
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400819static ssize_t
820system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
821 loff_t *ppos)
822{
Li Zefanc142b152009-05-08 10:32:05 +0800823 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedtae63b312012-05-03 23:09:03 -0400824 struct ftrace_subsystem_dir *dir = filp->private_data;
825 struct event_subsystem *system = dir->subsystem;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400826 struct ftrace_event_call *call;
Steven Rostedtae63b312012-05-03 23:09:03 -0400827 struct ftrace_event_file *file;
828 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400829 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800830 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400831 int ret;
832
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400833 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400834 list_for_each_entry(file, &tr->events, list) {
835 call = file->event_call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400836 if (!ftrace_event_name(call) || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400837 continue;
838
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400839 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400840 continue;
841
842 /*
843 * We need to find out if all the events are set
844 * or if all events or cleared, or if we have
845 * a mixture.
846 */
Steven Rostedtae63b312012-05-03 23:09:03 -0400847 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +0800848
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400849 /*
850 * If we have a mixture, no need to look further.
851 */
Li Zefanc142b152009-05-08 10:32:05 +0800852 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400853 break;
854 }
855 mutex_unlock(&event_mutex);
856
Li Zefanc142b152009-05-08 10:32:05 +0800857 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400858 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400859
860 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
861
862 return ret;
863}
864
865static ssize_t
866system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
867 loff_t *ppos)
868{
Steven Rostedtae63b312012-05-03 23:09:03 -0400869 struct ftrace_subsystem_dir *dir = filp->private_data;
870 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400871 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400872 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400873 ssize_t ret;
874
Peter Huewe22fe9b52011-06-07 21:58:27 +0200875 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
876 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400877 return ret;
878
879 ret = tracing_update_buffers();
880 if (ret < 0)
881 return ret;
882
Li Zefan8f31bfe2009-05-08 10:31:42 +0800883 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400884 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400885
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400886 /*
887 * Opening of "enable" adds a ref count to system,
888 * so the name is safe to use.
889 */
890 if (system)
891 name = system->name;
892
Steven Rostedtae63b312012-05-03 23:09:03 -0400893 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400894 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800895 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400896
897 ret = cnt;
898
Li Zefan8f31bfe2009-05-08 10:31:42 +0800899out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400900 *ppos += cnt;
901
902 return ret;
903}
904
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400905enum {
906 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +0800907 FORMAT_FIELD_SEPERATOR = 2,
908 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400909};
910
911static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -0500912{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200913 struct ftrace_event_call *call = event_file_data(m->private);
Li Zefan86397dc2010-08-17 13:53:06 +0800914 struct list_head *common_head = &ftrace_common_fields;
915 struct list_head *head = trace_get_fields(call);
Oleg Nesterov7710b632013-07-18 20:47:10 +0200916 struct list_head *node = v;
Steven Rostedt981d0812009-03-02 13:53:59 -0500917
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400918 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800919
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400920 switch ((unsigned long)v) {
921 case FORMAT_HEADER:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200922 node = common_head;
923 break;
Li Zefan86397dc2010-08-17 13:53:06 +0800924
925 case FORMAT_FIELD_SEPERATOR:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200926 node = head;
927 break;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800928
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400929 case FORMAT_PRINTFMT:
930 /* all done */
931 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800932 }
933
Oleg Nesterov7710b632013-07-18 20:47:10 +0200934 node = node->prev;
935 if (node == common_head)
Li Zefan86397dc2010-08-17 13:53:06 +0800936 return (void *)FORMAT_FIELD_SEPERATOR;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200937 else if (node == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400938 return (void *)FORMAT_PRINTFMT;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200939 else
940 return node;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400941}
942
943static int f_show(struct seq_file *m, void *v)
944{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200945 struct ftrace_event_call *call = event_file_data(m->private);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400946 struct ftrace_event_field *field;
947 const char *array_descriptor;
948
949 switch ((unsigned long)v) {
950 case FORMAT_HEADER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400951 seq_printf(m, "name: %s\n", ftrace_event_name(call));
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400952 seq_printf(m, "ID: %d\n", call->event.type);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100953 seq_puts(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +0800954 return 0;
955
Li Zefan86397dc2010-08-17 13:53:06 +0800956 case FORMAT_FIELD_SEPERATOR:
957 seq_putc(m, '\n');
958 return 0;
959
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400960 case FORMAT_PRINTFMT:
961 seq_printf(m, "\nprint fmt: %s\n",
962 call->print_fmt);
963 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500964 }
965
Oleg Nesterov7710b632013-07-18 20:47:10 +0200966 field = list_entry(v, struct ftrace_event_field, link);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400967 /*
968 * Smartly shows the array type(except dynamic array).
969 * Normal:
970 * field:TYPE VAR
971 * If TYPE := TYPE[LEN], it is shown:
972 * field:TYPE VAR[LEN]
973 */
974 array_descriptor = strchr(field->type, '[');
975
976 if (!strncmp(field->type, "__data_loc", 10))
977 array_descriptor = NULL;
978
979 if (!array_descriptor)
980 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
981 field->type, field->name, field->offset,
982 field->size, !!field->is_signed);
983 else
984 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
985 (int)(array_descriptor - field->type),
986 field->type, field->name,
987 array_descriptor, field->offset,
988 field->size, !!field->is_signed);
989
990 return 0;
991}
992
Oleg Nesterov7710b632013-07-18 20:47:10 +0200993static void *f_start(struct seq_file *m, loff_t *pos)
994{
995 void *p = (void *)FORMAT_HEADER;
996 loff_t l = 0;
997
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200998 /* ->stop() is called even if ->start() fails */
999 mutex_lock(&event_mutex);
1000 if (!event_file_data(m->private))
1001 return ERR_PTR(-ENODEV);
1002
Oleg Nesterov7710b632013-07-18 20:47:10 +02001003 while (l < *pos && p)
1004 p = f_next(m, p, &l);
1005
1006 return p;
1007}
1008
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001009static void f_stop(struct seq_file *m, void *p)
1010{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001011 mutex_unlock(&event_mutex);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001012}
1013
1014static const struct seq_operations trace_format_seq_ops = {
1015 .start = f_start,
1016 .next = f_next,
1017 .stop = f_stop,
1018 .show = f_show,
1019};
1020
1021static int trace_format_open(struct inode *inode, struct file *file)
1022{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001023 struct seq_file *m;
1024 int ret;
1025
1026 ret = seq_open(file, &trace_format_seq_ops);
1027 if (ret < 0)
1028 return ret;
1029
1030 m = file->private_data;
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001031 m->private = file;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001032
1033 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001034}
1035
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001036static ssize_t
1037event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1038{
Oleg Nesterov1a111262013-07-26 19:25:32 +02001039 int id = (long)event_file_data(filp);
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001040 char buf[32];
1041 int len;
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001042
1043 if (*ppos)
1044 return 0;
1045
Oleg Nesterov1a111262013-07-26 19:25:32 +02001046 if (unlikely(!id))
1047 return -ENODEV;
1048
1049 len = sprintf(buf, "%d\n", id);
1050
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001051 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001052}
1053
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001054static ssize_t
1055event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1056 loff_t *ppos)
1057{
Tom Zanussif306cc82013-10-24 08:34:17 -05001058 struct ftrace_event_file *file;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001059 struct trace_seq *s;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001060 int r = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001061
1062 if (*ppos)
1063 return 0;
1064
1065 s = kmalloc(sizeof(*s), GFP_KERNEL);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001066
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001067 if (!s)
1068 return -ENOMEM;
1069
1070 trace_seq_init(s);
1071
Oleg Nesterove2912b02013-07-26 19:25:40 +02001072 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001073 file = event_file_data(filp);
1074 if (file)
1075 print_event_filter(file, s);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001076 mutex_unlock(&event_mutex);
1077
Tom Zanussif306cc82013-10-24 08:34:17 -05001078 if (file)
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001079 r = simple_read_from_buffer(ubuf, cnt, ppos,
1080 s->buffer, trace_seq_used(s));
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001081
1082 kfree(s);
1083
1084 return r;
1085}
1086
1087static ssize_t
1088event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1089 loff_t *ppos)
1090{
Tom Zanussif306cc82013-10-24 08:34:17 -05001091 struct ftrace_event_file *file;
Tom Zanussi8b372562009-04-28 03:04:59 -05001092 char *buf;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001093 int err = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001094
Tom Zanussi8b372562009-04-28 03:04:59 -05001095 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001096 return -EINVAL;
1097
Tom Zanussi8b372562009-04-28 03:04:59 -05001098 buf = (char *)__get_free_page(GFP_TEMPORARY);
1099 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001100 return -ENOMEM;
1101
Tom Zanussi8b372562009-04-28 03:04:59 -05001102 if (copy_from_user(buf, ubuf, cnt)) {
1103 free_page((unsigned long) buf);
1104 return -EFAULT;
1105 }
1106 buf[cnt] = '\0';
1107
Oleg Nesterove2912b02013-07-26 19:25:40 +02001108 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001109 file = event_file_data(filp);
1110 if (file)
1111 err = apply_event_filter(file, buf);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001112 mutex_unlock(&event_mutex);
1113
Tom Zanussi8b372562009-04-28 03:04:59 -05001114 free_page((unsigned long) buf);
1115 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001116 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -05001117
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001118 *ppos += cnt;
1119
1120 return cnt;
1121}
1122
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001123static LIST_HEAD(event_subsystems);
1124
1125static int subsystem_open(struct inode *inode, struct file *filp)
1126{
1127 struct event_subsystem *system = NULL;
Steven Rostedtae63b312012-05-03 23:09:03 -04001128 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1129 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001130 int ret;
1131
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001132 if (tracing_is_disabled())
1133 return -ENODEV;
1134
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001135 /* Make sure the system still exists */
Alexander Z Lama8227412013-07-01 19:37:54 -07001136 mutex_lock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001137 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -04001138 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1139 list_for_each_entry(dir, &tr->systems, list) {
1140 if (dir == inode->i_private) {
1141 /* Don't open systems with no events */
1142 if (dir->nr_events) {
1143 __get_system_dir(dir);
1144 system = dir->subsystem;
1145 }
1146 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001147 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001148 }
1149 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001150 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001151 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001152 mutex_unlock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001153
Steven Rostedtae63b312012-05-03 23:09:03 -04001154 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001155 return -ENODEV;
1156
Steven Rostedtae63b312012-05-03 23:09:03 -04001157 /* Some versions of gcc think dir can be uninitialized here */
1158 WARN_ON(!dir);
1159
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001160 /* Still need to increment the ref count of the system */
1161 if (trace_array_get(tr) < 0) {
Steven Rostedtae63b312012-05-03 23:09:03 -04001162 put_system(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001163 return -ENODEV;
1164 }
1165
1166 ret = tracing_open_generic(inode, filp);
1167 if (ret < 0) {
1168 trace_array_put(tr);
1169 put_system(dir);
1170 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001171
1172 return ret;
1173}
1174
1175static int system_tr_open(struct inode *inode, struct file *filp)
1176{
1177 struct ftrace_subsystem_dir *dir;
1178 struct trace_array *tr = inode->i_private;
1179 int ret;
1180
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001181 if (tracing_is_disabled())
1182 return -ENODEV;
1183
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001184 if (trace_array_get(tr) < 0)
1185 return -ENODEV;
1186
Steven Rostedtae63b312012-05-03 23:09:03 -04001187 /* Make a temporary dir that has no system but points to tr */
1188 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001189 if (!dir) {
1190 trace_array_put(tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04001191 return -ENOMEM;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001192 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001193
1194 dir->tr = tr;
1195
1196 ret = tracing_open_generic(inode, filp);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001197 if (ret < 0) {
1198 trace_array_put(tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04001199 kfree(dir);
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001200 return ret;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001201 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001202
1203 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001204
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001205 return 0;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001206}
1207
1208static int subsystem_release(struct inode *inode, struct file *file)
1209{
Steven Rostedtae63b312012-05-03 23:09:03 -04001210 struct ftrace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001211
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001212 trace_array_put(dir->tr);
1213
Steven Rostedtae63b312012-05-03 23:09:03 -04001214 /*
1215 * If dir->subsystem is NULL, then this is a temporary
1216 * descriptor that was made for a trace_array to enable
1217 * all subsystems.
1218 */
1219 if (dir->subsystem)
1220 put_system(dir);
1221 else
1222 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001223
1224 return 0;
1225}
1226
Tom Zanussicfb180f2009-03-22 03:31:17 -05001227static ssize_t
1228subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1229 loff_t *ppos)
1230{
Steven Rostedtae63b312012-05-03 23:09:03 -04001231 struct ftrace_subsystem_dir *dir = filp->private_data;
1232 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001233 struct trace_seq *s;
1234 int r;
1235
1236 if (*ppos)
1237 return 0;
1238
1239 s = kmalloc(sizeof(*s), GFP_KERNEL);
1240 if (!s)
1241 return -ENOMEM;
1242
1243 trace_seq_init(s);
1244
Tom Zanussi8b372562009-04-28 03:04:59 -05001245 print_subsystem_event_filter(system, s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001246 r = simple_read_from_buffer(ubuf, cnt, ppos,
1247 s->buffer, trace_seq_used(s));
Tom Zanussicfb180f2009-03-22 03:31:17 -05001248
1249 kfree(s);
1250
1251 return r;
1252}
1253
1254static ssize_t
1255subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1256 loff_t *ppos)
1257{
Steven Rostedtae63b312012-05-03 23:09:03 -04001258 struct ftrace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001259 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001260 int err;
1261
Tom Zanussi8b372562009-04-28 03:04:59 -05001262 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001263 return -EINVAL;
1264
Tom Zanussi8b372562009-04-28 03:04:59 -05001265 buf = (char *)__get_free_page(GFP_TEMPORARY);
1266 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001267 return -ENOMEM;
1268
Tom Zanussi8b372562009-04-28 03:04:59 -05001269 if (copy_from_user(buf, ubuf, cnt)) {
1270 free_page((unsigned long) buf);
1271 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001272 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001273 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001274
Steven Rostedtae63b312012-05-03 23:09:03 -04001275 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001276 free_page((unsigned long) buf);
1277 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001278 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001279
1280 *ppos += cnt;
1281
1282 return cnt;
1283}
1284
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001285static ssize_t
1286show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1287{
1288 int (*func)(struct trace_seq *s) = filp->private_data;
1289 struct trace_seq *s;
1290 int r;
1291
1292 if (*ppos)
1293 return 0;
1294
1295 s = kmalloc(sizeof(*s), GFP_KERNEL);
1296 if (!s)
1297 return -ENOMEM;
1298
1299 trace_seq_init(s);
1300
1301 func(s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001302 r = simple_read_from_buffer(ubuf, cnt, ppos,
1303 s->buffer, trace_seq_used(s));
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001304
1305 kfree(s);
1306
1307 return r;
1308}
1309
Steven Rostedt15075ca2012-05-03 14:57:28 -04001310static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1311static int ftrace_event_set_open(struct inode *inode, struct file *file);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001312static int ftrace_event_release(struct inode *inode, struct file *file);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001313
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001314static const struct seq_operations show_event_seq_ops = {
1315 .start = t_start,
1316 .next = t_next,
1317 .show = t_show,
1318 .stop = t_stop,
1319};
1320
1321static const struct seq_operations show_set_event_seq_ops = {
1322 .start = s_start,
1323 .next = s_next,
1324 .show = t_show,
1325 .stop = t_stop,
1326};
1327
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001328static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001329 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001330 .read = seq_read,
1331 .llseek = seq_lseek,
1332 .release = seq_release,
1333};
1334
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001335static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001336 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001337 .read = seq_read,
1338 .write = ftrace_event_write,
1339 .llseek = seq_lseek,
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001340 .release = ftrace_event_release,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001341};
1342
Steven Rostedt1473e442009-02-24 14:15:08 -05001343static const struct file_operations ftrace_enable_fops = {
Oleg Nesterovbf682c32013-07-28 20:35:27 +02001344 .open = tracing_open_generic,
Steven Rostedt1473e442009-02-24 14:15:08 -05001345 .read = event_enable_read,
1346 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001347 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001348};
1349
Steven Rostedt981d0812009-03-02 13:53:59 -05001350static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001351 .open = trace_format_open,
1352 .read = seq_read,
1353 .llseek = seq_lseek,
1354 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001355};
1356
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001357static const struct file_operations ftrace_event_id_fops = {
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001358 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001359 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001360};
1361
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001362static const struct file_operations ftrace_event_filter_fops = {
1363 .open = tracing_open_generic,
1364 .read = event_filter_read,
1365 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001366 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001367};
1368
Tom Zanussicfb180f2009-03-22 03:31:17 -05001369static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001370 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001371 .read = subsystem_filter_read,
1372 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001373 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001374 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001375};
1376
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001377static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001378 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001379 .read = system_enable_read,
1380 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001381 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001382 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001383};
1384
Steven Rostedtae63b312012-05-03 23:09:03 -04001385static const struct file_operations ftrace_tr_enable_fops = {
1386 .open = system_tr_open,
1387 .read = system_enable_read,
1388 .write = system_enable_write,
1389 .llseek = default_llseek,
1390 .release = subsystem_release,
1391};
1392
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001393static const struct file_operations ftrace_show_header_fops = {
1394 .open = tracing_open_generic,
1395 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001396 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001397};
1398
Steven Rostedtae63b312012-05-03 23:09:03 -04001399static int
1400ftrace_event_open(struct inode *inode, struct file *file,
1401 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001402{
Steven Rostedtae63b312012-05-03 23:09:03 -04001403 struct seq_file *m;
1404 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001405
Steven Rostedtae63b312012-05-03 23:09:03 -04001406 ret = seq_open(file, seq_ops);
1407 if (ret < 0)
1408 return ret;
1409 m = file->private_data;
1410 /* copy tr over to seq ops */
1411 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001412
Steven Rostedtae63b312012-05-03 23:09:03 -04001413 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001414}
1415
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001416static int ftrace_event_release(struct inode *inode, struct file *file)
1417{
1418 struct trace_array *tr = inode->i_private;
1419
1420 trace_array_put(tr);
1421
1422 return seq_release(inode, file);
1423}
1424
Steven Rostedt15075ca2012-05-03 14:57:28 -04001425static int
1426ftrace_event_avail_open(struct inode *inode, struct file *file)
1427{
1428 const struct seq_operations *seq_ops = &show_event_seq_ops;
1429
Steven Rostedtae63b312012-05-03 23:09:03 -04001430 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001431}
1432
1433static int
1434ftrace_event_set_open(struct inode *inode, struct file *file)
1435{
1436 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b312012-05-03 23:09:03 -04001437 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001438 int ret;
1439
1440 if (trace_array_get(tr) < 0)
1441 return -ENODEV;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001442
1443 if ((file->f_mode & FMODE_WRITE) &&
1444 (file->f_flags & O_TRUNC))
Steven Rostedtae63b312012-05-03 23:09:03 -04001445 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001446
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001447 ret = ftrace_event_open(inode, file, seq_ops);
1448 if (ret < 0)
1449 trace_array_put(tr);
1450 return ret;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001451}
1452
Steven Rostedtae63b312012-05-03 23:09:03 -04001453static struct event_subsystem *
1454create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001455{
1456 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001457
1458 /* need to create new entry */
1459 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b312012-05-03 23:09:03 -04001460 if (!system)
1461 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001462
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001463 system->ref_count = 1;
Steven Rostedt6e94a782013-06-27 10:58:31 -04001464
1465 /* Only allocate if dynamic (kprobes and modules) */
1466 if (!core_kernel_data((unsigned long)name)) {
1467 system->ref_count |= SYSTEM_FL_FREE_NAME;
1468 system->name = kstrdup(name, GFP_KERNEL);
1469 if (!system->name)
1470 goto out_free;
1471 } else
1472 system->name = name;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001473
Tom Zanussi30e673b2009-04-28 03:04:47 -05001474 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001475
Tom Zanussi8b372562009-04-28 03:04:59 -05001476 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b312012-05-03 23:09:03 -04001477 if (!system->filter)
1478 goto out_free;
1479
1480 list_add(&system->list, &event_subsystems);
1481
1482 return system;
1483
1484 out_free:
Steven Rostedt6e94a782013-06-27 10:58:31 -04001485 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1486 kfree(system->name);
Steven Rostedtae63b312012-05-03 23:09:03 -04001487 kfree(system);
1488 return NULL;
1489}
1490
1491static struct dentry *
1492event_subsystem_dir(struct trace_array *tr, const char *name,
1493 struct ftrace_event_file *file, struct dentry *parent)
1494{
1495 struct ftrace_subsystem_dir *dir;
1496 struct event_subsystem *system;
1497 struct dentry *entry;
1498
1499 /* First see if we did not already create this dir */
1500 list_for_each_entry(dir, &tr->systems, list) {
1501 system = dir->subsystem;
1502 if (strcmp(system->name, name) == 0) {
1503 dir->nr_events++;
1504 file->system = dir;
1505 return dir->entry;
1506 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001507 }
1508
Steven Rostedtae63b312012-05-03 23:09:03 -04001509 /* Now see if the system itself exists. */
1510 list_for_each_entry(system, &event_subsystems, list) {
1511 if (strcmp(system->name, name) == 0)
1512 break;
1513 }
1514 /* Reset system variable when not found */
1515 if (&system->list == &event_subsystems)
1516 system = NULL;
1517
1518 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1519 if (!dir)
1520 goto out_fail;
1521
1522 if (!system) {
1523 system = create_new_subsystem(name);
1524 if (!system)
1525 goto out_free;
1526 } else
1527 __get_system(system);
1528
1529 dir->entry = debugfs_create_dir(name, parent);
1530 if (!dir->entry) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001531 pr_warn("Failed to create system directory %s\n", name);
Steven Rostedtae63b312012-05-03 23:09:03 -04001532 __put_system(system);
1533 goto out_free;
1534 }
1535
1536 dir->tr = tr;
1537 dir->ref_count = 1;
1538 dir->nr_events = 1;
1539 dir->subsystem = system;
1540 file->system = dir;
1541
1542 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001543 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001544 if (!entry) {
1545 kfree(system->filter);
1546 system->filter = NULL;
Fabian Frederick3448bac2014-06-07 13:43:08 +02001547 pr_warn("Could not create debugfs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001548 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001549
Steven Rostedtae63b312012-05-03 23:09:03 -04001550 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001551 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001552
Steven Rostedtae63b312012-05-03 23:09:03 -04001553 list_add(&dir->list, &tr->systems);
1554
1555 return dir->entry;
1556
1557 out_free:
1558 kfree(dir);
1559 out_fail:
1560 /* Only print this message if failed on memory allocation */
1561 if (!dir || !system)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001562 pr_warn("No memory to create event subsystem %s\n", name);
Steven Rostedtae63b312012-05-03 23:09:03 -04001563 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001564}
1565
Steven Rostedt1473e442009-02-24 14:15:08 -05001566static int
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001567event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
Steven Rostedt1473e442009-02-24 14:15:08 -05001568{
Steven Rostedtae63b312012-05-03 23:09:03 -04001569 struct ftrace_event_call *call = file->event_call;
1570 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001571 struct list_head *head;
Steven Rostedtae63b312012-05-03 23:09:03 -04001572 struct dentry *d_events;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001573 const char *name;
Steven Rostedtfd994982009-02-28 02:41:25 -05001574 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001575
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001576 /*
1577 * If the trace point header did not define TRACE_SYSTEM
1578 * then the system would be called "TRACE_SYSTEM".
1579 */
Steven Rostedtae63b312012-05-03 23:09:03 -04001580 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1581 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1582 if (!d_events)
1583 return -ENOMEM;
1584 } else
1585 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001586
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001587 name = ftrace_event_name(call);
1588 file->dir = debugfs_create_dir(name, d_events);
Steven Rostedtae63b312012-05-03 23:09:03 -04001589 if (!file->dir) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001590 pr_warn("Could not create debugfs '%s' directory\n", name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001591 return -1;
1592 }
1593
Steven Rostedt9b637762012-05-10 15:55:43 -04001594 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b312012-05-03 23:09:03 -04001595 trace_create_file("enable", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001596 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001597
Steven Rostedt22392912010-04-21 12:27:06 -04001598#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001599 if (call->event.type && call->class->reg)
Oleg Nesterov1a111262013-07-26 19:25:32 +02001600 trace_create_file("id", 0444, file->dir,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001601 (void *)(long)call->event.type,
1602 &ftrace_event_id_fops);
Steven Rostedt22392912010-04-21 12:27:06 -04001603#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001604
Li Zefanc9d932c2010-05-24 16:24:28 +08001605 /*
1606 * Other events may have the same class. Only update
1607 * the fields if they are not already defined.
1608 */
1609 head = trace_get_fields(call);
1610 if (list_empty(head)) {
1611 ret = call->class->define_fields(call);
1612 if (ret < 0) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001613 pr_warn("Could not initialize trace point events/%s\n",
1614 name);
Steven Rostedtae63b312012-05-03 23:09:03 -04001615 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001616 }
1617 }
Tom Zanussif306cc82013-10-24 08:34:17 -05001618 trace_create_file("filter", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001619 &ftrace_event_filter_fops);
Tom Zanussicf027f62009-03-22 03:30:39 -05001620
Tom Zanussi85f2b082013-10-24 08:59:24 -05001621 trace_create_file("trigger", 0644, file->dir, file,
1622 &event_trigger_fops);
1623
Steven Rostedtae63b312012-05-03 23:09:03 -04001624 trace_create_file("format", 0444, file->dir, call,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001625 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -05001626
Steven Rostedt1473e442009-02-24 14:15:08 -05001627 return 0;
1628}
1629
Steven Rostedtae63b312012-05-03 23:09:03 -04001630static void remove_event_from_tracers(struct ftrace_event_call *call)
1631{
1632 struct ftrace_event_file *file;
1633 struct trace_array *tr;
1634
1635 do_for_each_event_file_safe(tr, file) {
Steven Rostedtae63b312012-05-03 23:09:03 -04001636 if (file->event_call != call)
1637 continue;
1638
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02001639 remove_event_file_dir(file);
Steven Rostedtae63b312012-05-03 23:09:03 -04001640 /*
1641 * The do_for_each_event_file_safe() is
1642 * a double loop. After finding the call for this
1643 * trace_array, we use break to jump to the next
1644 * trace_array.
1645 */
1646 break;
1647 } while_for_each_event_file();
1648}
1649
Ezequiel Garcia87819152012-09-12 11:47:57 -03001650static void event_remove(struct ftrace_event_call *call)
1651{
Steven Rostedtae63b312012-05-03 23:09:03 -04001652 struct trace_array *tr;
1653 struct ftrace_event_file *file;
1654
1655 do_for_each_event_file(tr, file) {
1656 if (file->event_call != call)
1657 continue;
1658 ftrace_event_enable_disable(file, 0);
1659 /*
1660 * The do_for_each_event_file() is
1661 * a double loop. After finding the call for this
1662 * trace_array, we use break to jump to the next
1663 * trace_array.
1664 */
1665 break;
1666 } while_for_each_event_file();
1667
Ezequiel Garcia87819152012-09-12 11:47:57 -03001668 if (call->event.funcs)
1669 __unregister_ftrace_event(&call->event);
Steven Rostedtae63b312012-05-03 23:09:03 -04001670 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001671 list_del(&call->list);
1672}
1673
1674static int event_init(struct ftrace_event_call *call)
1675{
1676 int ret = 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001677 const char *name;
Ezequiel Garcia87819152012-09-12 11:47:57 -03001678
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001679 name = ftrace_event_name(call);
1680 if (WARN_ON(!name))
Ezequiel Garcia87819152012-09-12 11:47:57 -03001681 return -EINVAL;
1682
1683 if (call->class->raw_init) {
1684 ret = call->class->raw_init(call);
1685 if (ret < 0 && ret != -ENOSYS)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001686 pr_warn("Could not initialize trace events/%s\n", name);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001687 }
1688
1689 return ret;
1690}
1691
Li Zefan67ead0a2010-05-24 16:25:13 +08001692static int
Steven Rostedtae63b312012-05-03 23:09:03 -04001693__register_event(struct ftrace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001694{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001695 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001696
Ezequiel Garcia87819152012-09-12 11:47:57 -03001697 ret = event_init(call);
1698 if (ret < 0)
1699 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04001700
Steven Rostedtae63b312012-05-03 23:09:03 -04001701 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08001702 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001703
Steven Rostedtae63b312012-05-03 23:09:03 -04001704 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001705}
1706
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001707static struct ftrace_event_file *
1708trace_create_new_event(struct ftrace_event_call *call,
1709 struct trace_array *tr)
1710{
1711 struct ftrace_event_file *file;
1712
1713 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1714 if (!file)
1715 return NULL;
1716
1717 file->event_call = call;
1718 file->tr = tr;
1719 atomic_set(&file->sm_ref, 0);
Tom Zanussi85f2b082013-10-24 08:59:24 -05001720 atomic_set(&file->tm_ref, 0);
1721 INIT_LIST_HEAD(&file->triggers);
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001722 list_add(&file->list, &tr->events);
1723
1724 return file;
1725}
1726
Steven Rostedtae63b312012-05-03 23:09:03 -04001727/* Add an event to a trace directory */
1728static int
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001729__trace_add_new_event(struct ftrace_event_call *call, struct trace_array *tr)
Steven Rostedtae63b312012-05-03 23:09:03 -04001730{
1731 struct ftrace_event_file *file;
1732
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001733 file = trace_create_new_event(call, tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04001734 if (!file)
1735 return -ENOMEM;
1736
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001737 return event_create_dir(tr->event_dir, file);
Steven Rostedtae63b312012-05-03 23:09:03 -04001738}
1739
Steven Rostedt77248222013-02-27 16:28:06 -05001740/*
1741 * Just create a decriptor for early init. A descriptor is required
1742 * for enabling events at boot. We want to enable events before
1743 * the filesystem is initialized.
1744 */
1745static __init int
1746__trace_early_add_new_event(struct ftrace_event_call *call,
1747 struct trace_array *tr)
1748{
1749 struct ftrace_event_file *file;
1750
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001751 file = trace_create_new_event(call, tr);
Steven Rostedt77248222013-02-27 16:28:06 -05001752 if (!file)
1753 return -ENOMEM;
1754
Steven Rostedt77248222013-02-27 16:28:06 -05001755 return 0;
1756}
1757
Steven Rostedtae63b312012-05-03 23:09:03 -04001758struct ftrace_module_file_ops;
Oleg Nesterov779c5e32013-07-31 19:31:32 +02001759static void __add_event_to_tracers(struct ftrace_event_call *call);
Steven Rostedtae63b312012-05-03 23:09:03 -04001760
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001761/* Add an additional event_call dynamically */
1762int trace_add_event_call(struct ftrace_event_call *call)
1763{
1764 int ret;
Alexander Z Lama8227412013-07-01 19:37:54 -07001765 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001766 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -04001767
1768 ret = __register_event(call, NULL);
1769 if (ret >= 0)
Oleg Nesterov779c5e32013-07-31 19:31:32 +02001770 __add_event_to_tracers(call);
Steven Rostedtae63b312012-05-03 23:09:03 -04001771
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001772 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001773 mutex_unlock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001774 return ret;
1775}
Steven Rostedt701970b2009-04-24 23:11:22 -04001776
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001777/*
Alexander Z Lama8227412013-07-01 19:37:54 -07001778 * Must be called under locking of trace_types_lock, event_mutex and
1779 * trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001780 */
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001781static void __trace_remove_event_call(struct ftrace_event_call *call)
1782{
Ezequiel Garcia87819152012-09-12 11:47:57 -03001783 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001784 trace_destroy_fields(call);
Oleg Nesterov57375742014-07-15 20:48:16 +02001785 free_event_filter(call->filter);
1786 call->filter = NULL;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001787}
1788
Oleg Nesterov2816c552013-07-29 19:50:33 +02001789static int probe_remove_event_call(struct ftrace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001790{
Oleg Nesterov2816c552013-07-29 19:50:33 +02001791 struct trace_array *tr;
1792 struct ftrace_event_file *file;
1793
1794#ifdef CONFIG_PERF_EVENTS
1795 if (call->perf_refcount)
1796 return -EBUSY;
1797#endif
1798 do_for_each_event_file(tr, file) {
1799 if (file->event_call != call)
1800 continue;
1801 /*
1802 * We can't rely on ftrace_event_enable_disable(enable => 0)
1803 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1804 * TRACE_REG_UNREGISTER.
1805 */
1806 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1807 return -EBUSY;
Steven Rostedt (Red Hat)2ba64032013-07-31 13:16:22 -04001808 /*
1809 * The do_for_each_event_file_safe() is
1810 * a double loop. After finding the call for this
1811 * trace_array, we use break to jump to the next
1812 * trace_array.
1813 */
Oleg Nesterov2816c552013-07-29 19:50:33 +02001814 break;
1815 } while_for_each_event_file();
1816
1817 __trace_remove_event_call(call);
1818
1819 return 0;
1820}
1821
1822/* Remove an event_call */
1823int trace_remove_event_call(struct ftrace_event_call *call)
1824{
1825 int ret;
1826
Alexander Z Lama8227412013-07-01 19:37:54 -07001827 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001828 mutex_lock(&event_mutex);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001829 down_write(&trace_event_sem);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001830 ret = probe_remove_event_call(call);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001831 up_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001832 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001833 mutex_unlock(&trace_types_lock);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001834
1835 return ret;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001836}
1837
1838#define for_each_event(event, start, end) \
1839 for (event = start; \
1840 (unsigned long)event < (unsigned long)end; \
1841 event++)
1842
1843#ifdef CONFIG_MODULES
1844
Steven Rostedt6d723732009-04-10 14:53:50 -04001845static void trace_module_add_events(struct module *mod)
1846{
Steven Rostedte4a9ea52011-01-27 09:15:30 -05001847 struct ftrace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04001848
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -05001849 if (!mod->num_trace_events)
1850 return;
1851
1852 /* Don't add infrastructure for mods without tracepoints */
1853 if (trace_module_has_bad_taint(mod)) {
1854 pr_err("%s: module has bad taint, not creating trace events\n",
1855 mod->name);
1856 return;
1857 }
1858
Steven Rostedt6d723732009-04-10 14:53:50 -04001859 start = mod->trace_events;
1860 end = mod->trace_events + mod->num_trace_events;
1861
Steven Rostedt6d723732009-04-10 14:53:50 -04001862 for_each_event(call, start, end) {
Steven Rostedtae63b312012-05-03 23:09:03 -04001863 __register_event(*call, mod);
Oleg Nesterov779c5e32013-07-31 19:31:32 +02001864 __add_event_to_tracers(*call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001865 }
1866}
1867
1868static void trace_module_remove_events(struct module *mod)
1869{
1870 struct ftrace_event_call *call, *p;
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001871 bool clear_trace = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001872
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001873 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04001874 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1875 if (call->mod == mod) {
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001876 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1877 clear_trace = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001878 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001879 }
1880 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001881 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001882
1883 /*
1884 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001885 * registered any events that were used. The only worry is if
1886 * a new module gets loaded, and takes on the same id as the events
1887 * of this module. When printing out the buffer, traced events left
1888 * over from this module may be passed to the new module events and
1889 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001890 */
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001891 if (clear_trace)
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001892 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04001893}
1894
Steven Rostedt61f919a2009-04-14 18:22:32 -04001895static int trace_module_notify(struct notifier_block *self,
1896 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001897{
1898 struct module *mod = data;
1899
Alexander Z Lama8227412013-07-01 19:37:54 -07001900 mutex_lock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04001901 mutex_lock(&event_mutex);
1902 switch (val) {
1903 case MODULE_STATE_COMING:
1904 trace_module_add_events(mod);
1905 break;
1906 case MODULE_STATE_GOING:
1907 trace_module_remove_events(mod);
1908 break;
1909 }
1910 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001911 mutex_unlock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04001912
1913 return 0;
1914}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001915
Oleg Nesterov836d4812013-07-31 19:31:37 +02001916static struct notifier_block trace_module_nb = {
1917 .notifier_call = trace_module_notify,
1918 .priority = 0,
1919};
Steven Rostedt61f919a2009-04-14 18:22:32 -04001920#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001921
Steven Rostedtae63b312012-05-03 23:09:03 -04001922/* Create a new event directory structure for a trace directory. */
1923static void
1924__trace_add_event_dirs(struct trace_array *tr)
1925{
Steven Rostedtae63b312012-05-03 23:09:03 -04001926 struct ftrace_event_call *call;
1927 int ret;
1928
1929 list_for_each_entry(call, &ftrace_events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001930 ret = __trace_add_new_event(call, tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04001931 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001932 pr_warn("Could not create directory for event %s\n",
1933 ftrace_event_name(call));
Steven Rostedtae63b312012-05-03 23:09:03 -04001934 }
1935}
1936
Tom Zanussi7862ad12013-10-24 08:59:28 -05001937struct ftrace_event_file *
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001938find_event_file(struct trace_array *tr, const char *system, const char *event)
1939{
1940 struct ftrace_event_file *file;
1941 struct ftrace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001942 const char *name;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001943
1944 list_for_each_entry(file, &tr->events, list) {
1945
1946 call = file->event_call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001947 name = ftrace_event_name(call);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001948
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001949 if (!name || !call->class || !call->class->reg)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001950 continue;
1951
1952 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1953 continue;
1954
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001955 if (strcmp(event, name) == 0 &&
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001956 strcmp(system, call->class->system) == 0)
1957 return file;
1958 }
1959 return NULL;
1960}
1961
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05001962#ifdef CONFIG_DYNAMIC_FTRACE
1963
1964/* Avoid typos */
1965#define ENABLE_EVENT_STR "enable_event"
1966#define DISABLE_EVENT_STR "disable_event"
1967
1968struct event_probe_data {
1969 struct ftrace_event_file *file;
1970 unsigned long count;
1971 int ref;
1972 bool enable;
1973};
1974
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001975static void
1976event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1977{
1978 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1979 struct event_probe_data *data = *pdata;
1980
1981 if (!data)
1982 return;
1983
1984 if (data->enable)
1985 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1986 else
1987 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1988}
1989
1990static void
1991event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1992{
1993 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1994 struct event_probe_data *data = *pdata;
1995
1996 if (!data)
1997 return;
1998
1999 if (!data->count)
2000 return;
2001
2002 /* Skip if the event is in a state we want to switch to */
2003 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2004 return;
2005
2006 if (data->count != -1)
2007 (data->count)--;
2008
2009 event_enable_probe(ip, parent_ip, _data);
2010}
2011
2012static int
2013event_enable_print(struct seq_file *m, unsigned long ip,
2014 struct ftrace_probe_ops *ops, void *_data)
2015{
2016 struct event_probe_data *data = _data;
2017
2018 seq_printf(m, "%ps:", (void *)ip);
2019
2020 seq_printf(m, "%s:%s:%s",
2021 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2022 data->file->event_call->class->system,
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002023 ftrace_event_name(data->file->event_call));
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002024
2025 if (data->count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002026 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002027 else
2028 seq_printf(m, ":count=%ld\n", data->count);
2029
2030 return 0;
2031}
2032
2033static int
2034event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2035 void **_data)
2036{
2037 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2038 struct event_probe_data *data = *pdata;
2039
2040 data->ref++;
2041 return 0;
2042}
2043
2044static void
2045event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2046 void **_data)
2047{
2048 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2049 struct event_probe_data *data = *pdata;
2050
2051 if (WARN_ON_ONCE(data->ref <= 0))
2052 return;
2053
2054 data->ref--;
2055 if (!data->ref) {
2056 /* Remove the SOFT_MODE flag */
2057 __ftrace_event_enable_disable(data->file, 0, 1);
2058 module_put(data->file->event_call->mod);
2059 kfree(data);
2060 }
2061 *pdata = NULL;
2062}
2063
2064static struct ftrace_probe_ops event_enable_probe_ops = {
2065 .func = event_enable_probe,
2066 .print = event_enable_print,
2067 .init = event_enable_init,
2068 .free = event_enable_free,
2069};
2070
2071static struct ftrace_probe_ops event_enable_count_probe_ops = {
2072 .func = event_enable_count_probe,
2073 .print = event_enable_print,
2074 .init = event_enable_init,
2075 .free = event_enable_free,
2076};
2077
2078static struct ftrace_probe_ops event_disable_probe_ops = {
2079 .func = event_enable_probe,
2080 .print = event_enable_print,
2081 .init = event_enable_init,
2082 .free = event_enable_free,
2083};
2084
2085static struct ftrace_probe_ops event_disable_count_probe_ops = {
2086 .func = event_enable_count_probe,
2087 .print = event_enable_print,
2088 .init = event_enable_init,
2089 .free = event_enable_free,
2090};
2091
2092static int
2093event_enable_func(struct ftrace_hash *hash,
2094 char *glob, char *cmd, char *param, int enabled)
2095{
2096 struct trace_array *tr = top_trace_array();
2097 struct ftrace_event_file *file;
2098 struct ftrace_probe_ops *ops;
2099 struct event_probe_data *data;
2100 const char *system;
2101 const char *event;
2102 char *number;
2103 bool enable;
2104 int ret;
2105
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002106 if (!tr)
2107 return -ENODEV;
2108
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002109 /* hash funcs only work with set_ftrace_filter */
Harsh Prateek Bora8092e802013-05-24 12:52:17 +05302110 if (!enabled || !param)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002111 return -EINVAL;
2112
2113 system = strsep(&param, ":");
2114 if (!param)
2115 return -EINVAL;
2116
2117 event = strsep(&param, ":");
2118
2119 mutex_lock(&event_mutex);
2120
2121 ret = -EINVAL;
2122 file = find_event_file(tr, system, event);
2123 if (!file)
2124 goto out;
2125
2126 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2127
2128 if (enable)
2129 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2130 else
2131 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2132
2133 if (glob[0] == '!') {
2134 unregister_ftrace_function_probe_func(glob+1, ops);
2135 ret = 0;
2136 goto out;
2137 }
2138
2139 ret = -ENOMEM;
2140 data = kzalloc(sizeof(*data), GFP_KERNEL);
2141 if (!data)
2142 goto out;
2143
2144 data->enable = enable;
2145 data->count = -1;
2146 data->file = file;
2147
2148 if (!param)
2149 goto out_reg;
2150
2151 number = strsep(&param, ":");
2152
2153 ret = -EINVAL;
2154 if (!strlen(number))
2155 goto out_free;
2156
2157 /*
2158 * We use the callback data field (which is a pointer)
2159 * as our counter.
2160 */
2161 ret = kstrtoul(number, 0, &data->count);
2162 if (ret)
2163 goto out_free;
2164
2165 out_reg:
2166 /* Don't let event modules unload while probe registered */
2167 ret = try_module_get(file->event_call->mod);
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002168 if (!ret) {
2169 ret = -EBUSY;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002170 goto out_free;
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002171 }
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002172
2173 ret = __ftrace_event_enable_disable(file, 1, 1);
2174 if (ret < 0)
2175 goto out_put;
2176 ret = register_ftrace_function_probe(glob, ops, data);
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002177 /*
2178 * The above returns on success the # of functions enabled,
2179 * but if it didn't find any functions it returns zero.
2180 * Consider no functions a failure too.
2181 */
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002182 if (!ret) {
2183 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002184 goto out_disable;
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002185 } else if (ret < 0)
2186 goto out_disable;
2187 /* Just return zero, not the number of enabled functions */
2188 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002189 out:
2190 mutex_unlock(&event_mutex);
2191 return ret;
2192
2193 out_disable:
2194 __ftrace_event_enable_disable(file, 0, 1);
2195 out_put:
2196 module_put(file->event_call->mod);
2197 out_free:
2198 kfree(data);
2199 goto out;
2200}
2201
2202static struct ftrace_func_command event_enable_cmd = {
2203 .name = ENABLE_EVENT_STR,
2204 .func = event_enable_func,
2205};
2206
2207static struct ftrace_func_command event_disable_cmd = {
2208 .name = DISABLE_EVENT_STR,
2209 .func = event_enable_func,
2210};
2211
2212static __init int register_event_cmds(void)
2213{
2214 int ret;
2215
2216 ret = register_ftrace_command(&event_enable_cmd);
2217 if (WARN_ON(ret < 0))
2218 return ret;
2219 ret = register_ftrace_command(&event_disable_cmd);
2220 if (WARN_ON(ret < 0))
2221 unregister_ftrace_command(&event_enable_cmd);
2222 return ret;
2223}
2224#else
2225static inline int register_event_cmds(void) { return 0; }
2226#endif /* CONFIG_DYNAMIC_FTRACE */
2227
Steven Rostedt77248222013-02-27 16:28:06 -05002228/*
2229 * The top level array has already had its ftrace_event_file
2230 * descriptors created in order to allow for early events to
2231 * be recorded. This function is called after the debugfs has been
2232 * initialized, and we now have to create the files associated
2233 * to the events.
2234 */
2235static __init void
2236__trace_early_add_event_dirs(struct trace_array *tr)
2237{
2238 struct ftrace_event_file *file;
2239 int ret;
2240
2241
2242 list_for_each_entry(file, &tr->events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002243 ret = event_create_dir(tr->event_dir, file);
Steven Rostedt77248222013-02-27 16:28:06 -05002244 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002245 pr_warn("Could not create directory for event %s\n",
2246 ftrace_event_name(file->event_call));
Steven Rostedt77248222013-02-27 16:28:06 -05002247 }
2248}
2249
2250/*
2251 * For early boot up, the top trace array requires to have
2252 * a list of events that can be enabled. This must be done before
2253 * the filesystem is set up in order to allow events to be traced
2254 * early.
2255 */
2256static __init void
2257__trace_early_add_events(struct trace_array *tr)
2258{
2259 struct ftrace_event_call *call;
2260 int ret;
2261
2262 list_for_each_entry(call, &ftrace_events, list) {
2263 /* Early boot up should not have any modules loaded */
2264 if (WARN_ON_ONCE(call->mod))
2265 continue;
2266
2267 ret = __trace_early_add_new_event(call, tr);
2268 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002269 pr_warn("Could not create early event %s\n",
2270 ftrace_event_name(call));
Steven Rostedt77248222013-02-27 16:28:06 -05002271 }
2272}
2273
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002274/* Remove the event directory structure for a trace directory. */
2275static void
2276__trace_remove_event_dirs(struct trace_array *tr)
2277{
2278 struct ftrace_event_file *file, *next;
2279
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002280 list_for_each_entry_safe(file, next, &tr->events, list)
2281 remove_event_file_dir(file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002282}
2283
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002284static void __add_event_to_tracers(struct ftrace_event_call *call)
Steven Rostedtae63b312012-05-03 23:09:03 -04002285{
2286 struct trace_array *tr;
2287
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002288 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2289 __trace_add_new_event(call, tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04002290}
2291
Steven Rostedte4a9ea52011-01-27 09:15:30 -05002292extern struct ftrace_event_call *__start_ftrace_events[];
2293extern struct ftrace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002294
Li Zefan020e5f82009-07-01 10:47:05 +08002295static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2296
2297static __init int setup_trace_event(char *str)
2298{
2299 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002300 ring_buffer_expanded = true;
2301 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002302
2303 return 1;
2304}
2305__setup("trace_event=", setup_trace_event);
2306
Steven Rostedt77248222013-02-27 16:28:06 -05002307/* Expects to have event_mutex held when called */
2308static int
2309create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b312012-05-03 23:09:03 -04002310{
2311 struct dentry *d_events;
2312 struct dentry *entry;
2313
2314 entry = debugfs_create_file("set_event", 0644, parent,
2315 tr, &ftrace_set_event_fops);
2316 if (!entry) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002317 pr_warn("Could not create debugfs 'set_event' entry\n");
Steven Rostedtae63b312012-05-03 23:09:03 -04002318 return -ENOMEM;
2319 }
2320
2321 d_events = debugfs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002322 if (!d_events) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002323 pr_warn("Could not create debugfs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002324 return -ENOMEM;
2325 }
Steven Rostedtae63b312012-05-03 23:09:03 -04002326
2327 /* ring buffer internal formats */
2328 trace_create_file("header_page", 0444, d_events,
2329 ring_buffer_print_page_header,
2330 &ftrace_show_header_fops);
2331
2332 trace_create_file("header_event", 0444, d_events,
2333 ring_buffer_print_entry_header,
2334 &ftrace_show_header_fops);
2335
2336 trace_create_file("enable", 0644, d_events,
2337 tr, &ftrace_tr_enable_fops);
2338
2339 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002340
2341 return 0;
2342}
2343
2344/**
2345 * event_trace_add_tracer - add a instance of a trace_array to events
2346 * @parent: The parent dentry to place the files/directories for events in
2347 * @tr: The trace array associated with these events
2348 *
2349 * When a new instance is created, it needs to set up its events
2350 * directory, as well as other files associated with events. It also
2351 * creates the event hierachry in the @parent/events directory.
2352 *
2353 * Returns 0 on success.
2354 */
2355int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2356{
2357 int ret;
2358
2359 mutex_lock(&event_mutex);
2360
2361 ret = create_event_toplevel_files(parent, tr);
2362 if (ret)
2363 goto out_unlock;
2364
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002365 down_write(&trace_event_sem);
Steven Rostedtae63b312012-05-03 23:09:03 -04002366 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002367 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002368
Steven Rostedt77248222013-02-27 16:28:06 -05002369 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04002370 mutex_unlock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -04002371
Steven Rostedt77248222013-02-27 16:28:06 -05002372 return ret;
2373}
2374
2375/*
2376 * The top trace array already had its file descriptors created.
2377 * Now the files themselves need to be created.
2378 */
2379static __init int
2380early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2381{
2382 int ret;
2383
2384 mutex_lock(&event_mutex);
2385
2386 ret = create_event_toplevel_files(parent, tr);
2387 if (ret)
2388 goto out_unlock;
2389
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002390 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002391 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002392 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002393
2394 out_unlock:
2395 mutex_unlock(&event_mutex);
2396
2397 return ret;
Steven Rostedtae63b312012-05-03 23:09:03 -04002398}
2399
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002400int event_trace_del_tracer(struct trace_array *tr)
2401{
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002402 mutex_lock(&event_mutex);
2403
Tom Zanussi85f2b082013-10-24 08:59:24 -05002404 /* Disable any event triggers and associated soft-disabled events */
2405 clear_event_triggers(tr);
2406
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -04002407 /* Disable any running events */
2408 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2409
Steven Rostedt3ccb0122013-12-03 12:41:20 -05002410 /* Access to events are within rcu_read_lock_sched() */
2411 synchronize_sched();
2412
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002413 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002414 __trace_remove_event_dirs(tr);
2415 debugfs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002416 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002417
2418 tr->event_dir = NULL;
2419
2420 mutex_unlock(&event_mutex);
2421
2422 return 0;
2423}
2424
Steven Rostedtd1a29142013-02-27 20:23:57 -05002425static __init int event_trace_memsetup(void)
2426{
2427 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2428 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2429 return 0;
2430}
2431
Ezequiel Garcia87819152012-09-12 11:47:57 -03002432static __init int event_trace_enable(void)
2433{
Steven Rostedtae63b312012-05-03 23:09:03 -04002434 struct trace_array *tr = top_trace_array();
Ezequiel Garcia87819152012-09-12 11:47:57 -03002435 struct ftrace_event_call **iter, *call;
2436 char *buf = bootup_event_buf;
2437 char *token;
2438 int ret;
2439
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002440 if (!tr)
2441 return -ENODEV;
2442
Ezequiel Garcia87819152012-09-12 11:47:57 -03002443 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2444
2445 call = *iter;
2446 ret = event_init(call);
2447 if (!ret)
2448 list_add(&call->list, &ftrace_events);
2449 }
2450
Steven Rostedt77248222013-02-27 16:28:06 -05002451 /*
2452 * We need the top trace array to have a working set of trace
2453 * points at early init, before the debug files and directories
2454 * are created. Create the file entries now, and attach them
2455 * to the actual file dentries later.
2456 */
2457 __trace_early_add_events(tr);
2458
Ezequiel Garcia87819152012-09-12 11:47:57 -03002459 while (true) {
2460 token = strsep(&buf, ",");
2461
2462 if (!token)
2463 break;
2464 if (!*token)
2465 continue;
2466
Steven Rostedtae63b312012-05-03 23:09:03 -04002467 ret = ftrace_set_clr_event(tr, token, 1);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002468 if (ret)
2469 pr_warn("Failed to enable trace event: %s\n", token);
2470 }
Steven Rostedt81698832012-10-11 10:15:05 -04002471
2472 trace_printk_start_comm();
2473
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002474 register_event_cmds();
2475
Tom Zanussi85f2b082013-10-24 08:59:24 -05002476 register_trigger_cmds();
2477
Ezequiel Garcia87819152012-09-12 11:47:57 -03002478 return 0;
2479}
2480
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002481static __init int event_trace_init(void)
2482{
Steven Rostedtae63b312012-05-03 23:09:03 -04002483 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002484 struct dentry *d_tracer;
2485 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04002486 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002487
Steven Rostedtae63b312012-05-03 23:09:03 -04002488 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002489 if (!tr)
2490 return -ENODEV;
Steven Rostedtae63b312012-05-03 23:09:03 -04002491
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002492 d_tracer = tracing_init_dentry();
2493 if (!d_tracer)
2494 return 0;
2495
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002496 entry = debugfs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b312012-05-03 23:09:03 -04002497 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002498 if (!entry)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002499 pr_warn("Could not create debugfs 'available_events' entry\n");
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002500
Li Zefan8728fe52010-05-24 16:22:49 +08002501 if (trace_define_common_fields())
Fabian Frederick3448bac2014-06-07 13:43:08 +02002502 pr_warn("tracing: Failed to allocate common fields");
Li Zefan8728fe52010-05-24 16:22:49 +08002503
Steven Rostedt77248222013-02-27 16:28:06 -05002504 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04002505 if (ret)
2506 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08002507
Oleg Nesterov836d4812013-07-31 19:31:37 +02002508#ifdef CONFIG_MODULES
Steven Rostedt6d723732009-04-10 14:53:50 -04002509 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08002510 if (ret)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002511 pr_warn("Failed to register trace events module notifier\n");
Oleg Nesterov836d4812013-07-31 19:31:37 +02002512#endif
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002513 return 0;
2514}
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05002515
2516void __init trace_event_init(void)
2517{
2518 event_trace_memsetup();
2519 init_ftrace_syscalls();
2520 event_trace_enable();
2521}
2522
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002523fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002524
2525#ifdef CONFIG_FTRACE_STARTUP_TEST
2526
2527static DEFINE_SPINLOCK(test_spinlock);
2528static DEFINE_SPINLOCK(test_spinlock_irq);
2529static DEFINE_MUTEX(test_mutex);
2530
2531static __init void test_work(struct work_struct *dummy)
2532{
2533 spin_lock(&test_spinlock);
2534 spin_lock_irq(&test_spinlock_irq);
2535 udelay(1);
2536 spin_unlock_irq(&test_spinlock_irq);
2537 spin_unlock(&test_spinlock);
2538
2539 mutex_lock(&test_mutex);
2540 msleep(1);
2541 mutex_unlock(&test_mutex);
2542}
2543
2544static __init int event_test_thread(void *unused)
2545{
2546 void *test_malloc;
2547
2548 test_malloc = kmalloc(1234, GFP_KERNEL);
2549 if (!test_malloc)
2550 pr_info("failed to kmalloc\n");
2551
2552 schedule_on_each_cpu(test_work);
2553
2554 kfree(test_malloc);
2555
2556 set_current_state(TASK_INTERRUPTIBLE);
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02002557 while (!kthread_should_stop()) {
Steven Rostedte6187002009-04-15 13:36:40 -04002558 schedule();
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02002559 set_current_state(TASK_INTERRUPTIBLE);
2560 }
2561 __set_current_state(TASK_RUNNING);
Steven Rostedte6187002009-04-15 13:36:40 -04002562
2563 return 0;
2564}
2565
2566/*
2567 * Do various things that may trigger events.
2568 */
2569static __init void event_test_stuff(void)
2570{
2571 struct task_struct *test_thread;
2572
2573 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2574 msleep(1);
2575 kthread_stop(test_thread);
2576}
2577
2578/*
2579 * For every trace event defined, we will test each trace point separately,
2580 * and then by groups, and finally all trace points.
2581 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002582static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04002583{
Steven Rostedtae63b312012-05-03 23:09:03 -04002584 struct ftrace_subsystem_dir *dir;
2585 struct ftrace_event_file *file;
Steven Rostedte6187002009-04-15 13:36:40 -04002586 struct ftrace_event_call *call;
2587 struct event_subsystem *system;
Steven Rostedtae63b312012-05-03 23:09:03 -04002588 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04002589 int ret;
2590
Steven Rostedtae63b312012-05-03 23:09:03 -04002591 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002592 if (!tr)
2593 return;
Steven Rostedtae63b312012-05-03 23:09:03 -04002594
Steven Rostedte6187002009-04-15 13:36:40 -04002595 pr_info("Running tests on trace events:\n");
2596
Steven Rostedtae63b312012-05-03 23:09:03 -04002597 list_for_each_entry(file, &tr->events, list) {
2598
2599 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04002600
Steven Rostedt22392912010-04-21 12:27:06 -04002601 /* Only test those that have a probe */
2602 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04002603 continue;
2604
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002605/*
2606 * Testing syscall events here is pretty useless, but
2607 * we still do it if configured. But this is time consuming.
2608 * What we really need is a user thread to perform the
2609 * syscalls as we test.
2610 */
2611#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04002612 if (call->class->system &&
2613 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002614 continue;
2615#endif
2616
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002617 pr_info("Testing event %s: ", ftrace_event_name(call));
Steven Rostedte6187002009-04-15 13:36:40 -04002618
2619 /*
2620 * If an event is already enabled, someone is using
2621 * it and the self test should not be on.
2622 */
Steven Rostedtae63b312012-05-03 23:09:03 -04002623 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002624 pr_warn("Enabled event during self test!\n");
Steven Rostedte6187002009-04-15 13:36:40 -04002625 WARN_ON_ONCE(1);
2626 continue;
2627 }
2628
Steven Rostedtae63b312012-05-03 23:09:03 -04002629 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002630 event_test_stuff();
Steven Rostedtae63b312012-05-03 23:09:03 -04002631 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002632
2633 pr_cont("OK\n");
2634 }
2635
2636 /* Now test at the sub system level */
2637
2638 pr_info("Running tests on trace event systems:\n");
2639
Steven Rostedtae63b312012-05-03 23:09:03 -04002640 list_for_each_entry(dir, &tr->systems, list) {
2641
2642 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04002643
2644 /* the ftrace system is special, skip it */
2645 if (strcmp(system->name, "ftrace") == 0)
2646 continue;
2647
2648 pr_info("Testing event system %s: ", system->name);
2649
Steven Rostedtae63b312012-05-03 23:09:03 -04002650 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002651 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002652 pr_warn("error enabling system %s\n",
2653 system->name);
Steven Rostedte6187002009-04-15 13:36:40 -04002654 continue;
2655 }
2656
2657 event_test_stuff();
2658
Steven Rostedtae63b312012-05-03 23:09:03 -04002659 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002660 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002661 pr_warn("error disabling system %s\n",
2662 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002663 continue;
2664 }
Steven Rostedte6187002009-04-15 13:36:40 -04002665
2666 pr_cont("OK\n");
2667 }
2668
2669 /* Test with all events enabled */
2670
2671 pr_info("Running tests on all trace events:\n");
2672 pr_info("Testing all events: ");
2673
Steven Rostedtae63b312012-05-03 23:09:03 -04002674 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002675 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002676 pr_warn("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002677 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002678 }
2679
2680 event_test_stuff();
2681
2682 /* reset sysname */
Steven Rostedtae63b312012-05-03 23:09:03 -04002683 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002684 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02002685 pr_warn("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002686 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002687 }
2688
2689 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002690}
2691
2692#ifdef CONFIG_FUNCTION_TRACER
2693
Tejun Heo245b2e72009-06-24 15:13:48 +09002694static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002695
2696static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002697function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002698 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002699{
2700 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002701 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002702 struct ftrace_entry *entry;
2703 unsigned long flags;
2704 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002705 int cpu;
2706 int pc;
2707
2708 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002709 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002710 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09002711 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002712
2713 if (disabled != 1)
2714 goto out;
2715
2716 local_save_flags(flags);
2717
Steven Rostedte77405a2009-09-02 14:17:06 -04002718 event = trace_current_buffer_lock_reserve(&buffer,
2719 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002720 flags, pc);
2721 if (!event)
2722 goto out;
2723 entry = ring_buffer_event_data(event);
2724 entry->ip = ip;
2725 entry->parent_ip = parent_ip;
2726
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002727 trace_buffer_unlock_commit(buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002728
2729 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09002730 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04002731 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002732}
2733
2734static struct ftrace_ops trace_ops __initdata =
2735{
2736 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04002737 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002738};
2739
2740static __init void event_trace_self_test_with_function(void)
2741{
Steven Rostedt17bb6152011-05-23 15:27:46 -04002742 int ret;
2743 ret = register_ftrace_function(&trace_ops);
2744 if (WARN_ON(ret < 0)) {
2745 pr_info("Failed to enable function tracer for event tests\n");
2746 return;
2747 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002748 pr_info("Running tests again, along with the function tracer\n");
2749 event_trace_self_tests();
2750 unregister_ftrace_function(&trace_ops);
2751}
2752#else
2753static __init void event_trace_self_test_with_function(void)
2754{
2755}
2756#endif
2757
2758static __init int event_trace_self_tests_init(void)
2759{
Li Zefan020e5f82009-07-01 10:47:05 +08002760 if (!tracing_selftest_disabled) {
2761 event_trace_self_tests();
2762 event_trace_self_test_with_function();
2763 }
Steven Rostedte6187002009-04-15 13:36:40 -04002764
2765 return 0;
2766}
2767
Steven Rostedt28d20e22009-04-20 12:12:44 -04002768late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002769
2770#endif