blob: 29a7ebcfb42621d05cb9a74a7bcf291515b38461 [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
Steven Rostedte6187002009-04-15 13:36:40 -040011#include <linux/workqueue.h>
12#include <linux/spinlock.h>
13#include <linux/kthread.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050014#include <linux/debugfs.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Steven Rostedte6187002009-04-15 13:36:40 -040019#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050020
Li Zefan020e5f82009-07-01 10:47:05 +080021#include <asm/setup.h>
22
Steven Rostedt91729ef92009-03-02 15:03:01 -050023#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050024
Steven Rostedt4e5292e2009-09-12 19:26:21 -040025#undef TRACE_SYSTEM
Steven Rostedtb628b3e2009-02-27 23:32:58 -050026#define TRACE_SYSTEM "TRACE_SYSTEM"
27
Li Zefan20c89282009-05-06 10:33:45 +080028DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050029
Steven Rostedt04295782010-11-12 22:32:11 -050030DEFINE_MUTEX(event_storage_mutex);
31EXPORT_SYMBOL_GPL(event_storage_mutex);
32
33char event_storage[EVENT_STORAGE_SIZE];
34EXPORT_SYMBOL_GPL(event_storage);
35
Steven Rostedta59fd602009-04-10 13:52:20 -040036LIST_HEAD(ftrace_events);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080037static LIST_HEAD(ftrace_common_fields);
Steven Rostedta59fd602009-04-10 13:52:20 -040038
Steven Rostedtd1a29142013-02-27 20:23:57 -050039#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
40
41static struct kmem_cache *field_cachep;
42static struct kmem_cache *file_cachep;
43
Steven Rostedt6e94a782013-06-27 10:58:31 -040044#define SYSTEM_FL_FREE_NAME (1 << 31)
45
46static inline int system_refcount(struct event_subsystem *system)
47{
48 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
49}
50
51static int system_refcount_inc(struct event_subsystem *system)
52{
53 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
54}
55
56static int system_refcount_dec(struct event_subsystem *system)
57{
58 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
59}
60
Steven Rostedtae63b312012-05-03 23:09:03 -040061/* Double loops, do not use break, only goto's work */
62#define do_for_each_event_file(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 list_for_each_entry(file, &tr->events, list)
65
66#define do_for_each_event_file_safe(tr, file) \
67 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
68 struct ftrace_event_file *___n; \
69 list_for_each_entry_safe(file, ___n, &tr->events, list)
70
71#define while_for_each_event_file() \
72 }
73
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080074static struct list_head *
Steven Rostedt2e33af02010-04-22 10:35:55 -040075trace_get_fields(struct ftrace_event_call *event_call)
76{
77 if (!event_call->class->get_fields)
78 return &event_call->class->fields;
79 return event_call->class->get_fields(event_call);
80}
81
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080082static struct ftrace_event_field *
83__find_event_field(struct list_head *head, char *name)
84{
85 struct ftrace_event_field *field;
86
87 list_for_each_entry(field, head, link) {
88 if (!strcmp(field->name, name))
89 return field;
90 }
91
92 return NULL;
93}
94
95struct ftrace_event_field *
96trace_find_event_field(struct ftrace_event_call *call, char *name)
97{
98 struct ftrace_event_field *field;
99 struct list_head *head;
100
101 field = __find_event_field(&ftrace_common_fields, name);
102 if (field)
103 return field;
104
105 head = trace_get_fields(call);
106 return __find_event_field(head, name);
107}
108
Li Zefan8728fe52010-05-24 16:22:49 +0800109static int __trace_define_field(struct list_head *head, const char *type,
110 const char *name, int offset, int size,
111 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -0500112{
113 struct ftrace_event_field *field;
114
Steven Rostedtd1a29142013-02-27 20:23:57 -0500115 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -0500116 if (!field)
Namhyung Kimaaf6ac02013-06-07 15:07:48 +0900117 return -ENOMEM;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +0100118
Steven Rostedt92edca02013-02-27 20:41:37 -0500119 field->name = name;
120 field->type = type;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +0100121
Li Zefan43b51ea2009-08-07 10:33:22 +0800122 if (filter_type == FILTER_OTHER)
123 field->filter_type = filter_assign_type(type);
124 else
125 field->filter_type = filter_type;
126
Tom Zanussicf027f62009-03-22 03:30:39 -0500127 field->offset = offset;
128 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -0500129 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +0800130
Steven Rostedt2e33af02010-04-22 10:35:55 -0400131 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -0500132
133 return 0;
Tom Zanussicf027f62009-03-22 03:30:39 -0500134}
Li Zefan8728fe52010-05-24 16:22:49 +0800135
136int trace_define_field(struct ftrace_event_call *call, const char *type,
137 const char *name, int offset, int size, int is_signed,
138 int filter_type)
139{
140 struct list_head *head;
141
142 if (WARN_ON(!call->class))
143 return 0;
144
145 head = trace_get_fields(call);
146 return __trace_define_field(head, type, name, offset, size,
147 is_signed, filter_type);
148}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400149EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500150
Li Zefane647d6b2009-08-19 15:54:32 +0800151#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800152 ret = __trace_define_field(&ftrace_common_fields, #type, \
153 "common_" #item, \
154 offsetof(typeof(ent), item), \
155 sizeof(ent.item), \
156 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800157 if (ret) \
158 return ret;
159
Li Zefan8728fe52010-05-24 16:22:49 +0800160static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800161{
162 int ret;
163 struct trace_entry ent;
164
165 __common_field(unsigned short, type);
166 __common_field(unsigned char, flags);
167 __common_field(unsigned char, preempt_count);
168 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800169
170 return ret;
171}
172
zhangwei(Jovi)ad7067c2013-03-11 15:13:46 +0800173static void trace_destroy_fields(struct ftrace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800174{
175 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400176 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800177
Steven Rostedt2e33af02010-04-22 10:35:55 -0400178 head = trace_get_fields(call);
179 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800180 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500181 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800182 }
183}
184
Li Zefan87d9b4e2009-12-08 11:14:20 +0800185int trace_event_raw_init(struct ftrace_event_call *call)
186{
187 int id;
188
Steven Rostedt80decc72010-04-23 10:00:22 -0400189 id = register_ftrace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800190 if (!id)
191 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800192
193 return 0;
194}
195EXPORT_SYMBOL_GPL(trace_event_raw_init);
196
Jiri Olsaceec0b62012-02-15 15:51:49 +0100197int ftrace_event_reg(struct ftrace_event_call *call,
198 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400199{
Steven Rostedtae63b312012-05-03 23:09:03 -0400200 struct ftrace_event_file *file = data;
201
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400202 switch (type) {
203 case TRACE_REG_REGISTER:
204 return tracepoint_probe_register(call->name,
205 call->class->probe,
Steven Rostedtae63b312012-05-03 23:09:03 -0400206 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400207 case TRACE_REG_UNREGISTER:
208 tracepoint_probe_unregister(call->name,
209 call->class->probe,
Steven Rostedtae63b312012-05-03 23:09:03 -0400210 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400211 return 0;
212
213#ifdef CONFIG_PERF_EVENTS
214 case TRACE_REG_PERF_REGISTER:
215 return tracepoint_probe_register(call->name,
216 call->class->perf_probe,
217 call);
218 case TRACE_REG_PERF_UNREGISTER:
219 tracepoint_probe_unregister(call->name,
220 call->class->perf_probe,
221 call);
222 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100223 case TRACE_REG_PERF_OPEN:
224 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100225 case TRACE_REG_PERF_ADD:
226 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100227 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400228#endif
229 }
230 return 0;
231}
232EXPORT_SYMBOL_GPL(ftrace_event_reg);
233
Li Zefane870e9a2010-07-02 11:07:32 +0800234void trace_event_enable_cmd_record(bool enable)
235{
Steven Rostedtae63b312012-05-03 23:09:03 -0400236 struct ftrace_event_file *file;
237 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800238
239 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400240 do_for_each_event_file(tr, file) {
241
242 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800243 continue;
244
245 if (enable) {
246 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400247 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800248 } else {
249 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400250 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800251 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400252 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800253 mutex_unlock(&event_mutex);
254}
255
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400256static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
257 int enable, int soft_disable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500258{
Steven Rostedtae63b312012-05-03 23:09:03 -0400259 struct ftrace_event_call *call = file->event_call;
Li Zefan3b8e4272009-12-08 11:14:52 +0800260 int ret = 0;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400261 int disable;
Li Zefan3b8e4272009-12-08 11:14:52 +0800262
Steven Rostedtfd994982009-02-28 02:41:25 -0500263 switch (enable) {
264 case 0:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400265 /*
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900266 * When soft_disable is set and enable is cleared, the sm_ref
267 * reference counter is decremented. If it reaches 0, we want
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400268 * to clear the SOFT_DISABLED flag but leave the event in the
269 * state that it was. That is, if the event was enabled and
270 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
271 * is set we do not want the event to be enabled before we
272 * clear the bit.
273 *
274 * When soft_disable is not set but the SOFT_MODE flag is,
275 * we do nothing. Do not disable the tracepoint, otherwise
276 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
277 */
278 if (soft_disable) {
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900279 if (atomic_dec_return(&file->sm_ref) > 0)
280 break;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400281 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
282 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
283 } else
284 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
285
286 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
287 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
Steven Rostedtae63b312012-05-03 23:09:03 -0400288 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800289 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400290 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800291 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400292 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500293 }
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500294 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400295 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
296 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500297 else
298 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500299 break;
300 case 1:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400301 /*
302 * When soft_disable is set and enable is set, we want to
303 * register the tracepoint for the event, but leave the event
304 * as is. That means, if the event was already enabled, we do
305 * nothing (but set SOFT_MODE). If the event is disabled, we
306 * set SOFT_DISABLED before enabling the event tracepoint, so
307 * it still seems to be disabled.
308 */
309 if (!soft_disable)
310 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900311 else {
312 if (atomic_inc_return(&file->sm_ref) > 1)
313 break;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400314 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900315 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400316
Steven Rostedtae63b312012-05-03 23:09:03 -0400317 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400318
319 /* Keep the event disabled, when going to SOFT_MODE. */
320 if (soft_disable)
321 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
322
Li Zefane870e9a2010-07-02 11:07:32 +0800323 if (trace_flags & TRACE_ITER_RECORD_CMD) {
324 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400325 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800326 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400327 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800328 if (ret) {
329 tracing_stop_cmdline_record();
330 pr_info("event trace: Could not enable event "
331 "%s\n", call->name);
332 break;
333 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400334 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -0500335
336 /* WAS_ENABLED gets set but never cleared. */
337 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
Steven Rostedtfd994982009-02-28 02:41:25 -0500338 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500339 break;
340 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800341
342 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500343}
344
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400345static int ftrace_event_enable_disable(struct ftrace_event_file *file,
346 int enable)
347{
348 return __ftrace_event_enable_disable(file, enable, 0);
349}
350
Steven Rostedtae63b312012-05-03 23:09:03 -0400351static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800352{
Steven Rostedtae63b312012-05-03 23:09:03 -0400353 struct ftrace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800354
355 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400356 list_for_each_entry(file, &tr->events, list) {
357 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800358 }
359 mutex_unlock(&event_mutex);
360}
361
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400362static void __put_system(struct event_subsystem *system)
363{
364 struct event_filter *filter = system->filter;
365
Steven Rostedt6e94a782013-06-27 10:58:31 -0400366 WARN_ON_ONCE(system_refcount(system) == 0);
367 if (system_refcount_dec(system))
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400368 return;
369
Steven Rostedtae63b312012-05-03 23:09:03 -0400370 list_del(&system->list);
371
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400372 if (filter) {
373 kfree(filter->filter_string);
374 kfree(filter);
375 }
Steven Rostedt6e94a782013-06-27 10:58:31 -0400376 if (system->ref_count & SYSTEM_FL_FREE_NAME)
377 kfree(system->name);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400378 kfree(system);
379}
380
381static void __get_system(struct event_subsystem *system)
382{
Steven Rostedt6e94a782013-06-27 10:58:31 -0400383 WARN_ON_ONCE(system_refcount(system) == 0);
384 system_refcount_inc(system);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400385}
386
Steven Rostedtae63b312012-05-03 23:09:03 -0400387static void __get_system_dir(struct ftrace_subsystem_dir *dir)
388{
389 WARN_ON_ONCE(dir->ref_count == 0);
390 dir->ref_count++;
391 __get_system(dir->subsystem);
392}
393
394static void __put_system_dir(struct ftrace_subsystem_dir *dir)
395{
396 WARN_ON_ONCE(dir->ref_count == 0);
397 /* If the subsystem is about to be freed, the dir must be too */
Steven Rostedt6e94a782013-06-27 10:58:31 -0400398 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
Steven Rostedtae63b312012-05-03 23:09:03 -0400399
400 __put_system(dir->subsystem);
401 if (!--dir->ref_count)
402 kfree(dir);
403}
404
405static void put_system(struct ftrace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400406{
407 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400408 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400409 mutex_unlock(&event_mutex);
410}
411
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200412static void remove_subsystem(struct ftrace_subsystem_dir *dir)
413{
414 if (!dir)
415 return;
416
417 if (!--dir->nr_events) {
418 debugfs_remove_recursive(dir->entry);
419 list_del(&dir->list);
420 __put_system_dir(dir);
421 }
422}
423
Oleg Nesterov1a111262013-07-26 19:25:32 +0200424static void *event_file_data(struct file *filp)
425{
426 return ACCESS_ONCE(file_inode(filp)->i_private);
427}
428
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200429static void remove_event_file_dir(struct ftrace_event_file *file)
430{
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200431 struct dentry *dir = file->dir;
432 struct dentry *child;
433
434 if (dir) {
435 spin_lock(&dir->d_lock); /* probably unneeded */
436 list_for_each_entry(child, &dir->d_subdirs, d_u.d_child) {
437 if (child->d_inode) /* probably unneeded */
438 child->d_inode->i_private = NULL;
439 }
440 spin_unlock(&dir->d_lock);
441
442 debugfs_remove_recursive(dir);
443 }
444
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200445 list_del(&file->list);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200446 remove_subsystem(file->system);
447 kmem_cache_free(file_cachep, file);
448}
449
Li Zefan8f31bfe2009-05-08 10:31:42 +0800450/*
451 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
452 */
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400453static int
454__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
455 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500456{
Steven Rostedtae63b312012-05-03 23:09:03 -0400457 struct ftrace_event_file *file;
Steven Rostedta59fd602009-04-10 13:52:20 -0400458 struct ftrace_event_call *call;
Steven Rostedt29f93942009-05-08 16:06:47 -0400459 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500460
Steven Rostedtae63b312012-05-03 23:09:03 -0400461 list_for_each_entry(file, &tr->events, list) {
462
463 call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500464
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400465 if (!call->name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500466 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500467
Steven Rostedt9b637762012-05-10 15:55:43 -0400468 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
469 continue;
470
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500471 if (match &&
472 strcmp(match, call->name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400473 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500474 continue;
475
Steven Rostedt8f082012010-04-20 10:47:33 -0400476 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500477 continue;
478
479 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500480 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500481
Steven Rostedtae63b312012-05-03 23:09:03 -0400482 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500483
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500484 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500485 }
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400486
487 return ret;
488}
489
490static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
491 const char *sub, const char *event, int set)
492{
493 int ret;
494
495 mutex_lock(&event_mutex);
496 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500497 mutex_unlock(&event_mutex);
498
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500499 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500500}
501
Steven Rostedtae63b312012-05-03 23:09:03 -0400502static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800503{
504 char *event = NULL, *sub = NULL, *match;
505
506 /*
507 * The buf format can be <subsystem>:<event-name>
508 * *:<event-name> means any event by that name.
509 * :<event-name> is the same.
510 *
511 * <subsystem>:* means all events in that subsystem
512 * <subsystem>: means the same.
513 *
514 * <name> (no ':') means all events in a subsystem with
515 * the name <name> or any event that matches <name>
516 */
517
518 match = strsep(&buf, ":");
519 if (buf) {
520 sub = match;
521 event = buf;
522 match = NULL;
523
524 if (!strlen(sub) || strcmp(sub, "*") == 0)
525 sub = NULL;
526 if (!strlen(event) || strcmp(event, "*") == 0)
527 event = NULL;
528 }
529
Steven Rostedtae63b312012-05-03 23:09:03 -0400530 return __ftrace_set_clr_event(tr, match, sub, event, set);
Li Zefan8f31bfe2009-05-08 10:31:42 +0800531}
532
Steven Rostedt4671c792009-05-08 16:27:41 -0400533/**
534 * trace_set_clr_event - enable or disable an event
535 * @system: system name to match (NULL for any system)
536 * @event: event name to match (NULL for all events, within system)
537 * @set: 1 to enable, 0 to disable
538 *
539 * This is a way for other parts of the kernel to enable or disable
540 * event recording.
541 *
542 * Returns 0 on success, -EINVAL if the parameters do not match any
543 * registered events.
544 */
545int trace_set_clr_event(const char *system, const char *event, int set)
546{
Steven Rostedtae63b312012-05-03 23:09:03 -0400547 struct trace_array *tr = top_trace_array();
548
549 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400550}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800551EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400552
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500553/* 128 should be much more than enough */
554#define EVENT_BUF_SIZE 127
555
556static ssize_t
557ftrace_event_write(struct file *file, const char __user *ubuf,
558 size_t cnt, loff_t *ppos)
559{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200560 struct trace_parser parser;
Steven Rostedtae63b312012-05-03 23:09:03 -0400561 struct seq_file *m = file->private_data;
562 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800563 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500564
Li Zefan4ba79782009-09-22 13:52:20 +0800565 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500566 return 0;
567
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400568 ret = tracing_update_buffers();
569 if (ret < 0)
570 return ret;
571
jolsa@redhat.com48966362009-09-11 17:29:28 +0200572 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500573 return -ENOMEM;
574
jolsa@redhat.com48966362009-09-11 17:29:28 +0200575 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500576
Li Zefan4ba79782009-09-22 13:52:20 +0800577 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200578 int set = 1;
579
580 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500581 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500582
jolsa@redhat.com48966362009-09-11 17:29:28 +0200583 parser.buffer[parser.idx] = 0;
584
Steven Rostedtae63b312012-05-03 23:09:03 -0400585 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500586 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200587 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500588 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500589
590 ret = read;
591
jolsa@redhat.com48966362009-09-11 17:29:28 +0200592 out_put:
593 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500594
595 return ret;
596}
597
598static void *
599t_next(struct seq_file *m, void *v, loff_t *pos)
600{
Steven Rostedtae63b312012-05-03 23:09:03 -0400601 struct ftrace_event_file *file = v;
602 struct ftrace_event_call *call;
603 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500604
605 (*pos)++;
606
Steven Rostedtae63b312012-05-03 23:09:03 -0400607 list_for_each_entry_continue(file, &tr->events, list) {
608 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400609 /*
610 * The ftrace subsystem is for showing formats only.
611 * They can not be enabled or disabled via the event files.
612 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400613 if (call->class && call->class->reg)
Steven Rostedtae63b312012-05-03 23:09:03 -0400614 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400615 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500616
Li Zefan30bd39c2009-09-18 14:07:05 +0800617 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500618}
619
620static void *t_start(struct seq_file *m, loff_t *pos)
621{
Steven Rostedtae63b312012-05-03 23:09:03 -0400622 struct ftrace_event_file *file;
623 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800624 loff_t l;
625
Li Zefan20c89282009-05-06 10:33:45 +0800626 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800627
Steven Rostedtae63b312012-05-03 23:09:03 -0400628 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800629 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b312012-05-03 23:09:03 -0400630 file = t_next(m, file, &l);
631 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800632 break;
633 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400634 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500635}
636
637static void *
638s_next(struct seq_file *m, void *v, loff_t *pos)
639{
Steven Rostedtae63b312012-05-03 23:09:03 -0400640 struct ftrace_event_file *file = v;
641 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500642
643 (*pos)++;
644
Steven Rostedtae63b312012-05-03 23:09:03 -0400645 list_for_each_entry_continue(file, &tr->events, list) {
646 if (file->flags & FTRACE_EVENT_FL_ENABLED)
647 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500648 }
649
Li Zefan30bd39c2009-09-18 14:07:05 +0800650 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500651}
652
653static void *s_start(struct seq_file *m, loff_t *pos)
654{
Steven Rostedtae63b312012-05-03 23:09:03 -0400655 struct ftrace_event_file *file;
656 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800657 loff_t l;
658
Li Zefan20c89282009-05-06 10:33:45 +0800659 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800660
Steven Rostedtae63b312012-05-03 23:09:03 -0400661 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800662 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b312012-05-03 23:09:03 -0400663 file = s_next(m, file, &l);
664 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800665 break;
666 }
Steven Rostedtae63b312012-05-03 23:09:03 -0400667 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500668}
669
670static int t_show(struct seq_file *m, void *v)
671{
Steven Rostedtae63b312012-05-03 23:09:03 -0400672 struct ftrace_event_file *file = v;
673 struct ftrace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500674
Steven Rostedt8f082012010-04-20 10:47:33 -0400675 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
676 seq_printf(m, "%s:", call->class->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500677 seq_printf(m, "%s\n", call->name);
678
679 return 0;
680}
681
682static void t_stop(struct seq_file *m, void *p)
683{
Li Zefan20c89282009-05-06 10:33:45 +0800684 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500685}
686
Steven Rostedt1473e442009-02-24 14:15:08 -0500687static ssize_t
688event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
689 loff_t *ppos)
690{
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200691 struct ftrace_event_file *file;
692 unsigned long flags;
Tom Zanussia4390592013-06-29 00:08:04 -0500693 char buf[4] = "0";
Steven Rostedt1473e442009-02-24 14:15:08 -0500694
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200695 mutex_lock(&event_mutex);
696 file = event_file_data(filp);
697 if (likely(file))
698 flags = file->flags;
699 mutex_unlock(&event_mutex);
700
701 if (!file)
702 return -ENODEV;
703
704 if (flags & FTRACE_EVENT_FL_ENABLED &&
705 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
Tom Zanussia4390592013-06-29 00:08:04 -0500706 strcpy(buf, "1");
707
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200708 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
709 flags & FTRACE_EVENT_FL_SOFT_MODE)
Tom Zanussia4390592013-06-29 00:08:04 -0500710 strcat(buf, "*");
711
712 strcat(buf, "\n");
Steven Rostedt1473e442009-02-24 14:15:08 -0500713
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400714 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -0500715}
716
717static ssize_t
718event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
719 loff_t *ppos)
720{
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200721 struct ftrace_event_file *file;
Steven Rostedt1473e442009-02-24 14:15:08 -0500722 unsigned long val;
723 int ret;
724
Peter Huewe22fe9b52011-06-07 21:58:27 +0200725 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
726 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -0500727 return ret;
728
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400729 ret = tracing_update_buffers();
730 if (ret < 0)
731 return ret;
732
Steven Rostedt1473e442009-02-24 14:15:08 -0500733 switch (val) {
734 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500735 case 1:
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200736 ret = -ENODEV;
Steven Rostedt11a241a2009-03-02 11:49:04 -0500737 mutex_lock(&event_mutex);
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +0200738 file = event_file_data(filp);
739 if (likely(file))
740 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500741 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500742 break;
743
744 default:
745 return -EINVAL;
746 }
747
748 *ppos += cnt;
749
Li Zefan3b8e4272009-12-08 11:14:52 +0800750 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500751}
752
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400753static ssize_t
754system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
755 loff_t *ppos)
756{
Li Zefanc142b152009-05-08 10:32:05 +0800757 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedtae63b312012-05-03 23:09:03 -0400758 struct ftrace_subsystem_dir *dir = filp->private_data;
759 struct event_subsystem *system = dir->subsystem;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400760 struct ftrace_event_call *call;
Steven Rostedtae63b312012-05-03 23:09:03 -0400761 struct ftrace_event_file *file;
762 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400763 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800764 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400765 int ret;
766
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400767 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -0400768 list_for_each_entry(file, &tr->events, list) {
769 call = file->event_call;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400770 if (!call->name || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400771 continue;
772
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400773 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400774 continue;
775
776 /*
777 * We need to find out if all the events are set
778 * or if all events or cleared, or if we have
779 * a mixture.
780 */
Steven Rostedtae63b312012-05-03 23:09:03 -0400781 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +0800782
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400783 /*
784 * If we have a mixture, no need to look further.
785 */
Li Zefanc142b152009-05-08 10:32:05 +0800786 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400787 break;
788 }
789 mutex_unlock(&event_mutex);
790
Li Zefanc142b152009-05-08 10:32:05 +0800791 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400792 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400793
794 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
795
796 return ret;
797}
798
799static ssize_t
800system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
801 loff_t *ppos)
802{
Steven Rostedtae63b312012-05-03 23:09:03 -0400803 struct ftrace_subsystem_dir *dir = filp->private_data;
804 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400805 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400806 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400807 ssize_t ret;
808
Peter Huewe22fe9b52011-06-07 21:58:27 +0200809 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
810 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400811 return ret;
812
813 ret = tracing_update_buffers();
814 if (ret < 0)
815 return ret;
816
Li Zefan8f31bfe2009-05-08 10:31:42 +0800817 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400818 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400819
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400820 /*
821 * Opening of "enable" adds a ref count to system,
822 * so the name is safe to use.
823 */
824 if (system)
825 name = system->name;
826
Steven Rostedtae63b312012-05-03 23:09:03 -0400827 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400828 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800829 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400830
831 ret = cnt;
832
Li Zefan8f31bfe2009-05-08 10:31:42 +0800833out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400834 *ppos += cnt;
835
836 return ret;
837}
838
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400839enum {
840 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +0800841 FORMAT_FIELD_SEPERATOR = 2,
842 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400843};
844
845static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -0500846{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200847 struct ftrace_event_call *call = event_file_data(m->private);
Li Zefan86397dc2010-08-17 13:53:06 +0800848 struct list_head *common_head = &ftrace_common_fields;
849 struct list_head *head = trace_get_fields(call);
Oleg Nesterov7710b632013-07-18 20:47:10 +0200850 struct list_head *node = v;
Steven Rostedt981d0812009-03-02 13:53:59 -0500851
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400852 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800853
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400854 switch ((unsigned long)v) {
855 case FORMAT_HEADER:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200856 node = common_head;
857 break;
Li Zefan86397dc2010-08-17 13:53:06 +0800858
859 case FORMAT_FIELD_SEPERATOR:
Oleg Nesterov7710b632013-07-18 20:47:10 +0200860 node = head;
861 break;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800862
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400863 case FORMAT_PRINTFMT:
864 /* all done */
865 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800866 }
867
Oleg Nesterov7710b632013-07-18 20:47:10 +0200868 node = node->prev;
869 if (node == common_head)
Li Zefan86397dc2010-08-17 13:53:06 +0800870 return (void *)FORMAT_FIELD_SEPERATOR;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200871 else if (node == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400872 return (void *)FORMAT_PRINTFMT;
Oleg Nesterov7710b632013-07-18 20:47:10 +0200873 else
874 return node;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400875}
876
877static int f_show(struct seq_file *m, void *v)
878{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200879 struct ftrace_event_call *call = event_file_data(m->private);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400880 struct ftrace_event_field *field;
881 const char *array_descriptor;
882
883 switch ((unsigned long)v) {
884 case FORMAT_HEADER:
885 seq_printf(m, "name: %s\n", call->name);
886 seq_printf(m, "ID: %d\n", call->event.type);
887 seq_printf(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +0800888 return 0;
889
Li Zefan86397dc2010-08-17 13:53:06 +0800890 case FORMAT_FIELD_SEPERATOR:
891 seq_putc(m, '\n');
892 return 0;
893
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400894 case FORMAT_PRINTFMT:
895 seq_printf(m, "\nprint fmt: %s\n",
896 call->print_fmt);
897 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500898 }
899
Oleg Nesterov7710b632013-07-18 20:47:10 +0200900 field = list_entry(v, struct ftrace_event_field, link);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400901 /*
902 * Smartly shows the array type(except dynamic array).
903 * Normal:
904 * field:TYPE VAR
905 * If TYPE := TYPE[LEN], it is shown:
906 * field:TYPE VAR[LEN]
907 */
908 array_descriptor = strchr(field->type, '[');
909
910 if (!strncmp(field->type, "__data_loc", 10))
911 array_descriptor = NULL;
912
913 if (!array_descriptor)
914 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
915 field->type, field->name, field->offset,
916 field->size, !!field->is_signed);
917 else
918 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
919 (int)(array_descriptor - field->type),
920 field->type, field->name,
921 array_descriptor, field->offset,
922 field->size, !!field->is_signed);
923
924 return 0;
925}
926
Oleg Nesterov7710b632013-07-18 20:47:10 +0200927static void *f_start(struct seq_file *m, loff_t *pos)
928{
929 void *p = (void *)FORMAT_HEADER;
930 loff_t l = 0;
931
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200932 /* ->stop() is called even if ->start() fails */
933 mutex_lock(&event_mutex);
934 if (!event_file_data(m->private))
935 return ERR_PTR(-ENODEV);
936
Oleg Nesterov7710b632013-07-18 20:47:10 +0200937 while (l < *pos && p)
938 p = f_next(m, p, &l);
939
940 return p;
941}
942
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400943static void f_stop(struct seq_file *m, void *p)
944{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200945 mutex_unlock(&event_mutex);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400946}
947
948static const struct seq_operations trace_format_seq_ops = {
949 .start = f_start,
950 .next = f_next,
951 .stop = f_stop,
952 .show = f_show,
953};
954
955static int trace_format_open(struct inode *inode, struct file *file)
956{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400957 struct seq_file *m;
958 int ret;
959
960 ret = seq_open(file, &trace_format_seq_ops);
961 if (ret < 0)
962 return ret;
963
964 m = file->private_data;
Oleg Nesterovc5a44a12013-07-26 19:25:43 +0200965 m->private = file;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400966
967 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500968}
969
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100970static ssize_t
971event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
972{
Oleg Nesterov1a111262013-07-26 19:25:32 +0200973 int id = (long)event_file_data(filp);
Oleg Nesterovcd458ba2013-07-18 20:47:12 +0200974 char buf[32];
975 int len;
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100976
977 if (*ppos)
978 return 0;
979
Oleg Nesterov1a111262013-07-26 19:25:32 +0200980 if (unlikely(!id))
981 return -ENODEV;
982
983 len = sprintf(buf, "%d\n", id);
984
Oleg Nesterovcd458ba2013-07-18 20:47:12 +0200985 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100986}
987
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500988static ssize_t
989event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
990 loff_t *ppos)
991{
Oleg Nesterove2912b02013-07-26 19:25:40 +0200992 struct ftrace_event_call *call;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500993 struct trace_seq *s;
Oleg Nesterove2912b02013-07-26 19:25:40 +0200994 int r = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500995
996 if (*ppos)
997 return 0;
998
999 s = kmalloc(sizeof(*s), GFP_KERNEL);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001000
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001001 if (!s)
1002 return -ENOMEM;
1003
1004 trace_seq_init(s);
1005
Oleg Nesterove2912b02013-07-26 19:25:40 +02001006 mutex_lock(&event_mutex);
1007 call = event_file_data(filp);
1008 if (call)
1009 print_event_filter(call, s);
1010 mutex_unlock(&event_mutex);
1011
1012 if (call)
1013 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001014
1015 kfree(s);
1016
1017 return r;
1018}
1019
1020static ssize_t
1021event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1022 loff_t *ppos)
1023{
Oleg Nesterove2912b02013-07-26 19:25:40 +02001024 struct ftrace_event_call *call;
Tom Zanussi8b372562009-04-28 03:04:59 -05001025 char *buf;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001026 int err = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001027
Tom Zanussi8b372562009-04-28 03:04:59 -05001028 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001029 return -EINVAL;
1030
Tom Zanussi8b372562009-04-28 03:04:59 -05001031 buf = (char *)__get_free_page(GFP_TEMPORARY);
1032 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001033 return -ENOMEM;
1034
Tom Zanussi8b372562009-04-28 03:04:59 -05001035 if (copy_from_user(buf, ubuf, cnt)) {
1036 free_page((unsigned long) buf);
1037 return -EFAULT;
1038 }
1039 buf[cnt] = '\0';
1040
Oleg Nesterove2912b02013-07-26 19:25:40 +02001041 mutex_lock(&event_mutex);
1042 call = event_file_data(filp);
1043 if (call)
1044 err = apply_event_filter(call, buf);
1045 mutex_unlock(&event_mutex);
1046
Tom Zanussi8b372562009-04-28 03:04:59 -05001047 free_page((unsigned long) buf);
1048 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001049 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -05001050
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001051 *ppos += cnt;
1052
1053 return cnt;
1054}
1055
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001056static LIST_HEAD(event_subsystems);
1057
1058static int subsystem_open(struct inode *inode, struct file *filp)
1059{
1060 struct event_subsystem *system = NULL;
Steven Rostedtae63b312012-05-03 23:09:03 -04001061 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1062 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001063 int ret;
1064
1065 /* Make sure the system still exists */
Alexander Z Lama8227412013-07-01 19:37:54 -07001066 mutex_lock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001067 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -04001068 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1069 list_for_each_entry(dir, &tr->systems, list) {
1070 if (dir == inode->i_private) {
1071 /* Don't open systems with no events */
1072 if (dir->nr_events) {
1073 __get_system_dir(dir);
1074 system = dir->subsystem;
1075 }
1076 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001077 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001078 }
1079 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001080 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001081 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001082 mutex_unlock(&trace_types_lock);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001083
Steven Rostedtae63b312012-05-03 23:09:03 -04001084 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001085 return -ENODEV;
1086
Steven Rostedtae63b312012-05-03 23:09:03 -04001087 /* Some versions of gcc think dir can be uninitialized here */
1088 WARN_ON(!dir);
1089
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001090 /* Still need to increment the ref count of the system */
1091 if (trace_array_get(tr) < 0) {
Steven Rostedtae63b312012-05-03 23:09:03 -04001092 put_system(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001093 return -ENODEV;
1094 }
1095
1096 ret = tracing_open_generic(inode, filp);
1097 if (ret < 0) {
1098 trace_array_put(tr);
1099 put_system(dir);
1100 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001101
1102 return ret;
1103}
1104
1105static int system_tr_open(struct inode *inode, struct file *filp)
1106{
1107 struct ftrace_subsystem_dir *dir;
1108 struct trace_array *tr = inode->i_private;
1109 int ret;
1110
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001111 if (trace_array_get(tr) < 0)
1112 return -ENODEV;
1113
Steven Rostedtae63b312012-05-03 23:09:03 -04001114 /* Make a temporary dir that has no system but points to tr */
1115 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001116 if (!dir) {
1117 trace_array_put(tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04001118 return -ENOMEM;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001119 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001120
1121 dir->tr = tr;
1122
1123 ret = tracing_open_generic(inode, filp);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001124 if (ret < 0) {
1125 trace_array_put(tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04001126 kfree(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001127 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001128
1129 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001130
1131 return ret;
1132}
1133
1134static int subsystem_release(struct inode *inode, struct file *file)
1135{
Steven Rostedtae63b312012-05-03 23:09:03 -04001136 struct ftrace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001137
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001138 trace_array_put(dir->tr);
1139
Steven Rostedtae63b312012-05-03 23:09:03 -04001140 /*
1141 * If dir->subsystem is NULL, then this is a temporary
1142 * descriptor that was made for a trace_array to enable
1143 * all subsystems.
1144 */
1145 if (dir->subsystem)
1146 put_system(dir);
1147 else
1148 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001149
1150 return 0;
1151}
1152
Tom Zanussicfb180f2009-03-22 03:31:17 -05001153static ssize_t
1154subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1155 loff_t *ppos)
1156{
Steven Rostedtae63b312012-05-03 23:09:03 -04001157 struct ftrace_subsystem_dir *dir = filp->private_data;
1158 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001159 struct trace_seq *s;
1160 int r;
1161
1162 if (*ppos)
1163 return 0;
1164
1165 s = kmalloc(sizeof(*s), GFP_KERNEL);
1166 if (!s)
1167 return -ENOMEM;
1168
1169 trace_seq_init(s);
1170
Tom Zanussi8b372562009-04-28 03:04:59 -05001171 print_subsystem_event_filter(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -05001172 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -05001173
1174 kfree(s);
1175
1176 return r;
1177}
1178
1179static ssize_t
1180subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1181 loff_t *ppos)
1182{
Steven Rostedtae63b312012-05-03 23:09:03 -04001183 struct ftrace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001184 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001185 int err;
1186
Tom Zanussi8b372562009-04-28 03:04:59 -05001187 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001188 return -EINVAL;
1189
Tom Zanussi8b372562009-04-28 03:04:59 -05001190 buf = (char *)__get_free_page(GFP_TEMPORARY);
1191 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001192 return -ENOMEM;
1193
Tom Zanussi8b372562009-04-28 03:04:59 -05001194 if (copy_from_user(buf, ubuf, cnt)) {
1195 free_page((unsigned long) buf);
1196 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001197 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001198 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001199
Steven Rostedtae63b312012-05-03 23:09:03 -04001200 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001201 free_page((unsigned long) buf);
1202 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001203 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001204
1205 *ppos += cnt;
1206
1207 return cnt;
1208}
1209
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001210static ssize_t
1211show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1212{
1213 int (*func)(struct trace_seq *s) = filp->private_data;
1214 struct trace_seq *s;
1215 int r;
1216
1217 if (*ppos)
1218 return 0;
1219
1220 s = kmalloc(sizeof(*s), GFP_KERNEL);
1221 if (!s)
1222 return -ENOMEM;
1223
1224 trace_seq_init(s);
1225
1226 func(s);
1227 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1228
1229 kfree(s);
1230
1231 return r;
1232}
1233
Steven Rostedt15075ca2012-05-03 14:57:28 -04001234static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1235static int ftrace_event_set_open(struct inode *inode, struct file *file);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001236static int ftrace_event_release(struct inode *inode, struct file *file);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001237
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001238static const struct seq_operations show_event_seq_ops = {
1239 .start = t_start,
1240 .next = t_next,
1241 .show = t_show,
1242 .stop = t_stop,
1243};
1244
1245static const struct seq_operations show_set_event_seq_ops = {
1246 .start = s_start,
1247 .next = s_next,
1248 .show = t_show,
1249 .stop = t_stop,
1250};
1251
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001252static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001253 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001254 .read = seq_read,
1255 .llseek = seq_lseek,
1256 .release = seq_release,
1257};
1258
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001259static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001260 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001261 .read = seq_read,
1262 .write = ftrace_event_write,
1263 .llseek = seq_lseek,
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001264 .release = ftrace_event_release,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001265};
1266
Steven Rostedt1473e442009-02-24 14:15:08 -05001267static const struct file_operations ftrace_enable_fops = {
Oleg Nesterovbf682c32013-07-28 20:35:27 +02001268 .open = tracing_open_generic,
Steven Rostedt1473e442009-02-24 14:15:08 -05001269 .read = event_enable_read,
1270 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001271 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001272};
1273
Steven Rostedt981d0812009-03-02 13:53:59 -05001274static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001275 .open = trace_format_open,
1276 .read = seq_read,
1277 .llseek = seq_lseek,
1278 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001279};
1280
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001281static const struct file_operations ftrace_event_id_fops = {
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001282 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001283 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001284};
1285
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001286static const struct file_operations ftrace_event_filter_fops = {
1287 .open = tracing_open_generic,
1288 .read = event_filter_read,
1289 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001290 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001291};
1292
Tom Zanussicfb180f2009-03-22 03:31:17 -05001293static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001294 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001295 .read = subsystem_filter_read,
1296 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001297 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001298 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001299};
1300
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001301static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001302 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001303 .read = system_enable_read,
1304 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001305 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001306 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001307};
1308
Steven Rostedtae63b312012-05-03 23:09:03 -04001309static const struct file_operations ftrace_tr_enable_fops = {
1310 .open = system_tr_open,
1311 .read = system_enable_read,
1312 .write = system_enable_write,
1313 .llseek = default_llseek,
1314 .release = subsystem_release,
1315};
1316
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001317static const struct file_operations ftrace_show_header_fops = {
1318 .open = tracing_open_generic,
1319 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001320 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001321};
1322
Steven Rostedtae63b312012-05-03 23:09:03 -04001323static int
1324ftrace_event_open(struct inode *inode, struct file *file,
1325 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001326{
Steven Rostedtae63b312012-05-03 23:09:03 -04001327 struct seq_file *m;
1328 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001329
Steven Rostedtae63b312012-05-03 23:09:03 -04001330 ret = seq_open(file, seq_ops);
1331 if (ret < 0)
1332 return ret;
1333 m = file->private_data;
1334 /* copy tr over to seq ops */
1335 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001336
Steven Rostedtae63b312012-05-03 23:09:03 -04001337 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001338}
1339
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001340static int ftrace_event_release(struct inode *inode, struct file *file)
1341{
1342 struct trace_array *tr = inode->i_private;
1343
1344 trace_array_put(tr);
1345
1346 return seq_release(inode, file);
1347}
1348
Steven Rostedt15075ca2012-05-03 14:57:28 -04001349static int
1350ftrace_event_avail_open(struct inode *inode, struct file *file)
1351{
1352 const struct seq_operations *seq_ops = &show_event_seq_ops;
1353
Steven Rostedtae63b312012-05-03 23:09:03 -04001354 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001355}
1356
1357static int
1358ftrace_event_set_open(struct inode *inode, struct file *file)
1359{
1360 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b312012-05-03 23:09:03 -04001361 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001362 int ret;
1363
1364 if (trace_array_get(tr) < 0)
1365 return -ENODEV;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001366
1367 if ((file->f_mode & FMODE_WRITE) &&
1368 (file->f_flags & O_TRUNC))
Steven Rostedtae63b312012-05-03 23:09:03 -04001369 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001370
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001371 ret = ftrace_event_open(inode, file, seq_ops);
1372 if (ret < 0)
1373 trace_array_put(tr);
1374 return ret;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001375}
1376
Steven Rostedtae63b312012-05-03 23:09:03 -04001377static struct event_subsystem *
1378create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001379{
1380 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001381
1382 /* need to create new entry */
1383 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b312012-05-03 23:09:03 -04001384 if (!system)
1385 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001386
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001387 system->ref_count = 1;
Steven Rostedt6e94a782013-06-27 10:58:31 -04001388
1389 /* Only allocate if dynamic (kprobes and modules) */
1390 if (!core_kernel_data((unsigned long)name)) {
1391 system->ref_count |= SYSTEM_FL_FREE_NAME;
1392 system->name = kstrdup(name, GFP_KERNEL);
1393 if (!system->name)
1394 goto out_free;
1395 } else
1396 system->name = name;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001397
Tom Zanussi30e673b2009-04-28 03:04:47 -05001398 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001399
Tom Zanussi8b372562009-04-28 03:04:59 -05001400 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b312012-05-03 23:09:03 -04001401 if (!system->filter)
1402 goto out_free;
1403
1404 list_add(&system->list, &event_subsystems);
1405
1406 return system;
1407
1408 out_free:
Steven Rostedt6e94a782013-06-27 10:58:31 -04001409 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1410 kfree(system->name);
Steven Rostedtae63b312012-05-03 23:09:03 -04001411 kfree(system);
1412 return NULL;
1413}
1414
1415static struct dentry *
1416event_subsystem_dir(struct trace_array *tr, const char *name,
1417 struct ftrace_event_file *file, struct dentry *parent)
1418{
1419 struct ftrace_subsystem_dir *dir;
1420 struct event_subsystem *system;
1421 struct dentry *entry;
1422
1423 /* First see if we did not already create this dir */
1424 list_for_each_entry(dir, &tr->systems, list) {
1425 system = dir->subsystem;
1426 if (strcmp(system->name, name) == 0) {
1427 dir->nr_events++;
1428 file->system = dir;
1429 return dir->entry;
1430 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001431 }
1432
Steven Rostedtae63b312012-05-03 23:09:03 -04001433 /* Now see if the system itself exists. */
1434 list_for_each_entry(system, &event_subsystems, list) {
1435 if (strcmp(system->name, name) == 0)
1436 break;
1437 }
1438 /* Reset system variable when not found */
1439 if (&system->list == &event_subsystems)
1440 system = NULL;
1441
1442 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1443 if (!dir)
1444 goto out_fail;
1445
1446 if (!system) {
1447 system = create_new_subsystem(name);
1448 if (!system)
1449 goto out_free;
1450 } else
1451 __get_system(system);
1452
1453 dir->entry = debugfs_create_dir(name, parent);
1454 if (!dir->entry) {
1455 pr_warning("Failed to create system directory %s\n", name);
1456 __put_system(system);
1457 goto out_free;
1458 }
1459
1460 dir->tr = tr;
1461 dir->ref_count = 1;
1462 dir->nr_events = 1;
1463 dir->subsystem = system;
1464 file->system = dir;
1465
1466 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001467 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001468 if (!entry) {
1469 kfree(system->filter);
1470 system->filter = NULL;
Steven Rostedtae63b312012-05-03 23:09:03 -04001471 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001472 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001473
Steven Rostedtae63b312012-05-03 23:09:03 -04001474 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001475 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001476
Steven Rostedtae63b312012-05-03 23:09:03 -04001477 list_add(&dir->list, &tr->systems);
1478
1479 return dir->entry;
1480
1481 out_free:
1482 kfree(dir);
1483 out_fail:
1484 /* Only print this message if failed on memory allocation */
1485 if (!dir || !system)
1486 pr_warning("No memory to create event subsystem %s\n",
1487 name);
1488 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001489}
1490
Steven Rostedt1473e442009-02-24 14:15:08 -05001491static int
Steven Rostedtae63b312012-05-03 23:09:03 -04001492event_create_dir(struct dentry *parent,
1493 struct ftrace_event_file *file,
Steven Rostedt701970b2009-04-24 23:11:22 -04001494 const struct file_operations *id,
1495 const struct file_operations *enable,
1496 const struct file_operations *filter,
1497 const struct file_operations *format)
Steven Rostedt1473e442009-02-24 14:15:08 -05001498{
Steven Rostedtae63b312012-05-03 23:09:03 -04001499 struct ftrace_event_call *call = file->event_call;
1500 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001501 struct list_head *head;
Steven Rostedtae63b312012-05-03 23:09:03 -04001502 struct dentry *d_events;
Steven Rostedtfd994982009-02-28 02:41:25 -05001503 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001504
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001505 /*
1506 * If the trace point header did not define TRACE_SYSTEM
1507 * then the system would be called "TRACE_SYSTEM".
1508 */
Steven Rostedtae63b312012-05-03 23:09:03 -04001509 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1510 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1511 if (!d_events)
1512 return -ENOMEM;
1513 } else
1514 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001515
Steven Rostedtae63b312012-05-03 23:09:03 -04001516 file->dir = debugfs_create_dir(call->name, d_events);
1517 if (!file->dir) {
1518 pr_warning("Could not create debugfs '%s' directory\n",
1519 call->name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001520 return -1;
1521 }
1522
Steven Rostedt9b637762012-05-10 15:55:43 -04001523 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b312012-05-03 23:09:03 -04001524 trace_create_file("enable", 0644, file->dir, file,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001525 enable);
Steven Rostedt1473e442009-02-24 14:15:08 -05001526
Steven Rostedt22392912010-04-21 12:27:06 -04001527#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001528 if (call->event.type && call->class->reg)
Oleg Nesterov1a111262013-07-26 19:25:32 +02001529 trace_create_file("id", 0444, file->dir,
1530 (void *)(long)call->event.type, id);
Steven Rostedt22392912010-04-21 12:27:06 -04001531#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001532
Li Zefanc9d932c2010-05-24 16:24:28 +08001533 /*
1534 * Other events may have the same class. Only update
1535 * the fields if they are not already defined.
1536 */
1537 head = trace_get_fields(call);
1538 if (list_empty(head)) {
1539 ret = call->class->define_fields(call);
1540 if (ret < 0) {
1541 pr_warning("Could not initialize trace point"
1542 " events/%s\n", call->name);
Steven Rostedtae63b312012-05-03 23:09:03 -04001543 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001544 }
1545 }
Steven Rostedtae63b312012-05-03 23:09:03 -04001546 trace_create_file("filter", 0644, file->dir, call,
Li Zefanc9d932c2010-05-24 16:24:28 +08001547 filter);
Tom Zanussicf027f62009-03-22 03:30:39 -05001548
Steven Rostedtae63b312012-05-03 23:09:03 -04001549 trace_create_file("format", 0444, file->dir, call,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001550 format);
Steven Rostedtfd994982009-02-28 02:41:25 -05001551
Steven Rostedt1473e442009-02-24 14:15:08 -05001552 return 0;
1553}
1554
Steven Rostedtae63b312012-05-03 23:09:03 -04001555static void remove_event_from_tracers(struct ftrace_event_call *call)
1556{
1557 struct ftrace_event_file *file;
1558 struct trace_array *tr;
1559
1560 do_for_each_event_file_safe(tr, file) {
Steven Rostedtae63b312012-05-03 23:09:03 -04001561 if (file->event_call != call)
1562 continue;
1563
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02001564 remove_event_file_dir(file);
Steven Rostedtae63b312012-05-03 23:09:03 -04001565 /*
1566 * The do_for_each_event_file_safe() is
1567 * a double loop. After finding the call for this
1568 * trace_array, we use break to jump to the next
1569 * trace_array.
1570 */
1571 break;
1572 } while_for_each_event_file();
1573}
1574
Ezequiel Garcia87819152012-09-12 11:47:57 -03001575static void event_remove(struct ftrace_event_call *call)
1576{
Steven Rostedtae63b312012-05-03 23:09:03 -04001577 struct trace_array *tr;
1578 struct ftrace_event_file *file;
1579
1580 do_for_each_event_file(tr, file) {
1581 if (file->event_call != call)
1582 continue;
1583 ftrace_event_enable_disable(file, 0);
1584 /*
1585 * The do_for_each_event_file() is
1586 * a double loop. After finding the call for this
1587 * trace_array, we use break to jump to the next
1588 * trace_array.
1589 */
1590 break;
1591 } while_for_each_event_file();
1592
Ezequiel Garcia87819152012-09-12 11:47:57 -03001593 if (call->event.funcs)
1594 __unregister_ftrace_event(&call->event);
Steven Rostedtae63b312012-05-03 23:09:03 -04001595 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001596 list_del(&call->list);
1597}
1598
1599static int event_init(struct ftrace_event_call *call)
1600{
1601 int ret = 0;
1602
1603 if (WARN_ON(!call->name))
1604 return -EINVAL;
1605
1606 if (call->class->raw_init) {
1607 ret = call->class->raw_init(call);
1608 if (ret < 0 && ret != -ENOSYS)
1609 pr_warn("Could not initialize trace events/%s\n",
1610 call->name);
1611 }
1612
1613 return ret;
1614}
1615
Li Zefan67ead0a2010-05-24 16:25:13 +08001616static int
Steven Rostedtae63b312012-05-03 23:09:03 -04001617__register_event(struct ftrace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001618{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001619 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001620
Ezequiel Garcia87819152012-09-12 11:47:57 -03001621 ret = event_init(call);
1622 if (ret < 0)
1623 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04001624
Steven Rostedtae63b312012-05-03 23:09:03 -04001625 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08001626 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001627
Steven Rostedtae63b312012-05-03 23:09:03 -04001628 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001629}
1630
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001631static struct ftrace_event_file *
1632trace_create_new_event(struct ftrace_event_call *call,
1633 struct trace_array *tr)
1634{
1635 struct ftrace_event_file *file;
1636
1637 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1638 if (!file)
1639 return NULL;
1640
1641 file->event_call = call;
1642 file->tr = tr;
1643 atomic_set(&file->sm_ref, 0);
1644 list_add(&file->list, &tr->events);
1645
1646 return file;
1647}
1648
Steven Rostedtae63b312012-05-03 23:09:03 -04001649/* Add an event to a trace directory */
1650static int
1651__trace_add_new_event(struct ftrace_event_call *call,
1652 struct trace_array *tr,
1653 const struct file_operations *id,
1654 const struct file_operations *enable,
1655 const struct file_operations *filter,
1656 const struct file_operations *format)
1657{
1658 struct ftrace_event_file *file;
1659
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001660 file = trace_create_new_event(call, tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04001661 if (!file)
1662 return -ENOMEM;
1663
Steven Rostedtae63b312012-05-03 23:09:03 -04001664 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1665}
1666
Steven Rostedt77248222013-02-27 16:28:06 -05001667/*
1668 * Just create a decriptor for early init. A descriptor is required
1669 * for enabling events at boot. We want to enable events before
1670 * the filesystem is initialized.
1671 */
1672static __init int
1673__trace_early_add_new_event(struct ftrace_event_call *call,
1674 struct trace_array *tr)
1675{
1676 struct ftrace_event_file *file;
1677
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04001678 file = trace_create_new_event(call, tr);
Steven Rostedt77248222013-02-27 16:28:06 -05001679 if (!file)
1680 return -ENOMEM;
1681
Steven Rostedt77248222013-02-27 16:28:06 -05001682 return 0;
1683}
1684
Steven Rostedtae63b312012-05-03 23:09:03 -04001685struct ftrace_module_file_ops;
1686static void __add_event_to_tracers(struct ftrace_event_call *call,
1687 struct ftrace_module_file_ops *file_ops);
1688
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001689/* Add an additional event_call dynamically */
1690int trace_add_event_call(struct ftrace_event_call *call)
1691{
1692 int ret;
Alexander Z Lama8227412013-07-01 19:37:54 -07001693 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001694 mutex_lock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -04001695
1696 ret = __register_event(call, NULL);
1697 if (ret >= 0)
1698 __add_event_to_tracers(call, NULL);
1699
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001700 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001701 mutex_unlock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001702 return ret;
1703}
Steven Rostedt701970b2009-04-24 23:11:22 -04001704
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001705/*
Alexander Z Lama8227412013-07-01 19:37:54 -07001706 * Must be called under locking of trace_types_lock, event_mutex and
1707 * trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001708 */
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001709static void __trace_remove_event_call(struct ftrace_event_call *call)
1710{
Ezequiel Garcia87819152012-09-12 11:47:57 -03001711 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001712 trace_destroy_fields(call);
1713 destroy_preds(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001714}
1715
Oleg Nesterov2816c552013-07-29 19:50:33 +02001716static int probe_remove_event_call(struct ftrace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001717{
Oleg Nesterov2816c552013-07-29 19:50:33 +02001718 struct trace_array *tr;
1719 struct ftrace_event_file *file;
1720
1721#ifdef CONFIG_PERF_EVENTS
1722 if (call->perf_refcount)
1723 return -EBUSY;
1724#endif
1725 do_for_each_event_file(tr, file) {
1726 if (file->event_call != call)
1727 continue;
1728 /*
1729 * We can't rely on ftrace_event_enable_disable(enable => 0)
1730 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1731 * TRACE_REG_UNREGISTER.
1732 */
1733 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1734 return -EBUSY;
Steven Rostedt (Red Hat)2ba64032013-07-31 13:16:22 -04001735 /*
1736 * The do_for_each_event_file_safe() is
1737 * a double loop. After finding the call for this
1738 * trace_array, we use break to jump to the next
1739 * trace_array.
1740 */
Oleg Nesterov2816c552013-07-29 19:50:33 +02001741 break;
1742 } while_for_each_event_file();
1743
1744 __trace_remove_event_call(call);
1745
1746 return 0;
1747}
1748
1749/* Remove an event_call */
1750int trace_remove_event_call(struct ftrace_event_call *call)
1751{
1752 int ret;
1753
Alexander Z Lama8227412013-07-01 19:37:54 -07001754 mutex_lock(&trace_types_lock);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001755 mutex_lock(&event_mutex);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001756 down_write(&trace_event_sem);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001757 ret = probe_remove_event_call(call);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001758 up_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001759 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001760 mutex_unlock(&trace_types_lock);
Oleg Nesterov2816c552013-07-29 19:50:33 +02001761
1762 return ret;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001763}
1764
1765#define for_each_event(event, start, end) \
1766 for (event = start; \
1767 (unsigned long)event < (unsigned long)end; \
1768 event++)
1769
1770#ifdef CONFIG_MODULES
1771
1772static LIST_HEAD(ftrace_module_file_list);
1773
1774/*
1775 * Modules must own their file_operations to keep up with
1776 * reference counting.
1777 */
1778struct ftrace_module_file_ops {
1779 struct list_head list;
1780 struct module *mod;
1781 struct file_operations id;
1782 struct file_operations enable;
1783 struct file_operations format;
1784 struct file_operations filter;
1785};
1786
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001787static struct ftrace_module_file_ops *
1788find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
Steven Rostedtae63b312012-05-03 23:09:03 -04001789{
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001790 /*
1791 * As event_calls are added in groups by module,
1792 * when we find one file_ops, we don't need to search for
1793 * each call in that module, as the rest should be the
1794 * same. Only search for a new one if the last one did
1795 * not match.
1796 */
1797 if (file_ops && mod == file_ops->mod)
1798 return file_ops;
Steven Rostedtae63b312012-05-03 23:09:03 -04001799
1800 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1801 if (file_ops->mod == mod)
1802 return file_ops;
1803 }
1804 return NULL;
1805}
1806
Steven Rostedt701970b2009-04-24 23:11:22 -04001807static struct ftrace_module_file_ops *
1808trace_create_file_ops(struct module *mod)
1809{
1810 struct ftrace_module_file_ops *file_ops;
1811
1812 /*
1813 * This is a bit of a PITA. To allow for correct reference
1814 * counting, modules must "own" their file_operations.
1815 * To do this, we allocate the file operations that will be
1816 * used in the event directory.
1817 */
1818
1819 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1820 if (!file_ops)
1821 return NULL;
1822
1823 file_ops->mod = mod;
1824
1825 file_ops->id = ftrace_event_id_fops;
1826 file_ops->id.owner = mod;
1827
1828 file_ops->enable = ftrace_enable_fops;
1829 file_ops->enable.owner = mod;
1830
1831 file_ops->filter = ftrace_event_filter_fops;
1832 file_ops->filter.owner = mod;
1833
1834 file_ops->format = ftrace_event_format_fops;
1835 file_ops->format.owner = mod;
1836
1837 list_add(&file_ops->list, &ftrace_module_file_list);
1838
1839 return file_ops;
1840}
1841
Steven Rostedt6d723732009-04-10 14:53:50 -04001842static void trace_module_add_events(struct module *mod)
1843{
Steven Rostedt701970b2009-04-24 23:11:22 -04001844 struct ftrace_module_file_ops *file_ops = NULL;
Steven Rostedte4a9ea52011-01-27 09:15:30 -05001845 struct ftrace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04001846
1847 start = mod->trace_events;
1848 end = mod->trace_events + mod->num_trace_events;
1849
1850 if (start == end)
1851 return;
1852
Li Zefan67ead0a2010-05-24 16:25:13 +08001853 file_ops = trace_create_file_ops(mod);
1854 if (!file_ops)
Steven Rostedt6d723732009-04-10 14:53:50 -04001855 return;
1856
1857 for_each_event(call, start, end) {
Steven Rostedtae63b312012-05-03 23:09:03 -04001858 __register_event(*call, mod);
1859 __add_event_to_tracers(*call, file_ops);
Steven Rostedt6d723732009-04-10 14:53:50 -04001860 }
1861}
1862
1863static void trace_module_remove_events(struct module *mod)
1864{
Steven Rostedt701970b2009-04-24 23:11:22 -04001865 struct ftrace_module_file_ops *file_ops;
Steven Rostedt6d723732009-04-10 14:53:50 -04001866 struct ftrace_event_call *call, *p;
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001867 bool clear_trace = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001868
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001869 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04001870 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1871 if (call->mod == mod) {
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001872 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1873 clear_trace = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001874 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001875 }
1876 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001877
1878 /* Now free the file_operations */
1879 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1880 if (file_ops->mod == mod)
1881 break;
1882 }
1883 if (&file_ops->list != &ftrace_module_file_list) {
1884 list_del(&file_ops->list);
1885 kfree(file_ops);
1886 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001887 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001888
1889 /*
1890 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001891 * registered any events that were used. The only worry is if
1892 * a new module gets loaded, and takes on the same id as the events
1893 * of this module. When printing out the buffer, traced events left
1894 * over from this module may be passed to the new module events and
1895 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001896 */
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001897 if (clear_trace)
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001898 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04001899}
1900
Steven Rostedt61f919a2009-04-14 18:22:32 -04001901static int trace_module_notify(struct notifier_block *self,
1902 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001903{
1904 struct module *mod = data;
1905
Alexander Z Lama8227412013-07-01 19:37:54 -07001906 mutex_lock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04001907 mutex_lock(&event_mutex);
1908 switch (val) {
1909 case MODULE_STATE_COMING:
1910 trace_module_add_events(mod);
1911 break;
1912 case MODULE_STATE_GOING:
1913 trace_module_remove_events(mod);
1914 break;
1915 }
1916 mutex_unlock(&event_mutex);
Alexander Z Lama8227412013-07-01 19:37:54 -07001917 mutex_unlock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04001918
1919 return 0;
1920}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001921
1922static int
1923__trace_add_new_mod_event(struct ftrace_event_call *call,
1924 struct trace_array *tr,
1925 struct ftrace_module_file_ops *file_ops)
1926{
1927 return __trace_add_new_event(call, tr,
1928 &file_ops->id, &file_ops->enable,
1929 &file_ops->filter, &file_ops->format);
1930}
1931
Steven Rostedt61f919a2009-04-14 18:22:32 -04001932#else
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001933static inline struct ftrace_module_file_ops *
1934find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
Steven Rostedtae63b312012-05-03 23:09:03 -04001935{
1936 return NULL;
1937}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001938static inline int trace_module_notify(struct notifier_block *self,
1939 unsigned long val, void *data)
Steven Rostedt61f919a2009-04-14 18:22:32 -04001940{
1941 return 0;
1942}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001943static inline int
1944__trace_add_new_mod_event(struct ftrace_event_call *call,
1945 struct trace_array *tr,
1946 struct ftrace_module_file_ops *file_ops)
1947{
1948 return -ENODEV;
1949}
Steven Rostedt61f919a2009-04-14 18:22:32 -04001950#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001951
Steven Rostedtae63b312012-05-03 23:09:03 -04001952/* Create a new event directory structure for a trace directory. */
1953static void
1954__trace_add_event_dirs(struct trace_array *tr)
1955{
1956 struct ftrace_module_file_ops *file_ops = NULL;
1957 struct ftrace_event_call *call;
1958 int ret;
1959
1960 list_for_each_entry(call, &ftrace_events, list) {
1961 if (call->mod) {
1962 /*
1963 * Directories for events by modules need to
1964 * keep module ref counts when opened (as we don't
1965 * want the module to disappear when reading one
1966 * of these files). The file_ops keep account of
1967 * the module ref count.
Steven Rostedtae63b312012-05-03 23:09:03 -04001968 */
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001969 file_ops = find_ftrace_file_ops(file_ops, call->mod);
Steven Rostedtae63b312012-05-03 23:09:03 -04001970 if (!file_ops)
1971 continue; /* Warn? */
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001972 ret = __trace_add_new_mod_event(call, tr, file_ops);
Steven Rostedtae63b312012-05-03 23:09:03 -04001973 if (ret < 0)
1974 pr_warning("Could not create directory for event %s\n",
1975 call->name);
1976 continue;
1977 }
1978 ret = __trace_add_new_event(call, tr,
1979 &ftrace_event_id_fops,
1980 &ftrace_enable_fops,
1981 &ftrace_event_filter_fops,
1982 &ftrace_event_format_fops);
1983 if (ret < 0)
1984 pr_warning("Could not create directory for event %s\n",
1985 call->name);
1986 }
1987}
1988
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001989#ifdef CONFIG_DYNAMIC_FTRACE
1990
1991/* Avoid typos */
1992#define ENABLE_EVENT_STR "enable_event"
1993#define DISABLE_EVENT_STR "disable_event"
1994
1995struct event_probe_data {
1996 struct ftrace_event_file *file;
1997 unsigned long count;
1998 int ref;
1999 bool enable;
2000};
2001
2002static struct ftrace_event_file *
2003find_event_file(struct trace_array *tr, const char *system, const char *event)
2004{
2005 struct ftrace_event_file *file;
2006 struct ftrace_event_call *call;
2007
2008 list_for_each_entry(file, &tr->events, list) {
2009
2010 call = file->event_call;
2011
2012 if (!call->name || !call->class || !call->class->reg)
2013 continue;
2014
2015 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2016 continue;
2017
2018 if (strcmp(event, call->name) == 0 &&
2019 strcmp(system, call->class->system) == 0)
2020 return file;
2021 }
2022 return NULL;
2023}
2024
2025static void
2026event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2027{
2028 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2029 struct event_probe_data *data = *pdata;
2030
2031 if (!data)
2032 return;
2033
2034 if (data->enable)
2035 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2036 else
2037 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2038}
2039
2040static void
2041event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2042{
2043 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2044 struct event_probe_data *data = *pdata;
2045
2046 if (!data)
2047 return;
2048
2049 if (!data->count)
2050 return;
2051
2052 /* Skip if the event is in a state we want to switch to */
2053 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2054 return;
2055
2056 if (data->count != -1)
2057 (data->count)--;
2058
2059 event_enable_probe(ip, parent_ip, _data);
2060}
2061
2062static int
2063event_enable_print(struct seq_file *m, unsigned long ip,
2064 struct ftrace_probe_ops *ops, void *_data)
2065{
2066 struct event_probe_data *data = _data;
2067
2068 seq_printf(m, "%ps:", (void *)ip);
2069
2070 seq_printf(m, "%s:%s:%s",
2071 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2072 data->file->event_call->class->system,
2073 data->file->event_call->name);
2074
2075 if (data->count == -1)
2076 seq_printf(m, ":unlimited\n");
2077 else
2078 seq_printf(m, ":count=%ld\n", data->count);
2079
2080 return 0;
2081}
2082
2083static int
2084event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2085 void **_data)
2086{
2087 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2088 struct event_probe_data *data = *pdata;
2089
2090 data->ref++;
2091 return 0;
2092}
2093
2094static void
2095event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2096 void **_data)
2097{
2098 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2099 struct event_probe_data *data = *pdata;
2100
2101 if (WARN_ON_ONCE(data->ref <= 0))
2102 return;
2103
2104 data->ref--;
2105 if (!data->ref) {
2106 /* Remove the SOFT_MODE flag */
2107 __ftrace_event_enable_disable(data->file, 0, 1);
2108 module_put(data->file->event_call->mod);
2109 kfree(data);
2110 }
2111 *pdata = NULL;
2112}
2113
2114static struct ftrace_probe_ops event_enable_probe_ops = {
2115 .func = event_enable_probe,
2116 .print = event_enable_print,
2117 .init = event_enable_init,
2118 .free = event_enable_free,
2119};
2120
2121static struct ftrace_probe_ops event_enable_count_probe_ops = {
2122 .func = event_enable_count_probe,
2123 .print = event_enable_print,
2124 .init = event_enable_init,
2125 .free = event_enable_free,
2126};
2127
2128static struct ftrace_probe_ops event_disable_probe_ops = {
2129 .func = event_enable_probe,
2130 .print = event_enable_print,
2131 .init = event_enable_init,
2132 .free = event_enable_free,
2133};
2134
2135static struct ftrace_probe_ops event_disable_count_probe_ops = {
2136 .func = event_enable_count_probe,
2137 .print = event_enable_print,
2138 .init = event_enable_init,
2139 .free = event_enable_free,
2140};
2141
2142static int
2143event_enable_func(struct ftrace_hash *hash,
2144 char *glob, char *cmd, char *param, int enabled)
2145{
2146 struct trace_array *tr = top_trace_array();
2147 struct ftrace_event_file *file;
2148 struct ftrace_probe_ops *ops;
2149 struct event_probe_data *data;
2150 const char *system;
2151 const char *event;
2152 char *number;
2153 bool enable;
2154 int ret;
2155
2156 /* hash funcs only work with set_ftrace_filter */
Harsh Prateek Bora8092e802013-05-24 12:52:17 +05302157 if (!enabled || !param)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002158 return -EINVAL;
2159
2160 system = strsep(&param, ":");
2161 if (!param)
2162 return -EINVAL;
2163
2164 event = strsep(&param, ":");
2165
2166 mutex_lock(&event_mutex);
2167
2168 ret = -EINVAL;
2169 file = find_event_file(tr, system, event);
2170 if (!file)
2171 goto out;
2172
2173 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2174
2175 if (enable)
2176 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2177 else
2178 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2179
2180 if (glob[0] == '!') {
2181 unregister_ftrace_function_probe_func(glob+1, ops);
2182 ret = 0;
2183 goto out;
2184 }
2185
2186 ret = -ENOMEM;
2187 data = kzalloc(sizeof(*data), GFP_KERNEL);
2188 if (!data)
2189 goto out;
2190
2191 data->enable = enable;
2192 data->count = -1;
2193 data->file = file;
2194
2195 if (!param)
2196 goto out_reg;
2197
2198 number = strsep(&param, ":");
2199
2200 ret = -EINVAL;
2201 if (!strlen(number))
2202 goto out_free;
2203
2204 /*
2205 * We use the callback data field (which is a pointer)
2206 * as our counter.
2207 */
2208 ret = kstrtoul(number, 0, &data->count);
2209 if (ret)
2210 goto out_free;
2211
2212 out_reg:
2213 /* Don't let event modules unload while probe registered */
2214 ret = try_module_get(file->event_call->mod);
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002215 if (!ret) {
2216 ret = -EBUSY;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002217 goto out_free;
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002218 }
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002219
2220 ret = __ftrace_event_enable_disable(file, 1, 1);
2221 if (ret < 0)
2222 goto out_put;
2223 ret = register_ftrace_function_probe(glob, ops, data);
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002224 /*
2225 * The above returns on success the # of functions enabled,
2226 * but if it didn't find any functions it returns zero.
2227 * Consider no functions a failure too.
2228 */
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002229 if (!ret) {
2230 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002231 goto out_disable;
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002232 } else if (ret < 0)
2233 goto out_disable;
2234 /* Just return zero, not the number of enabled functions */
2235 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002236 out:
2237 mutex_unlock(&event_mutex);
2238 return ret;
2239
2240 out_disable:
2241 __ftrace_event_enable_disable(file, 0, 1);
2242 out_put:
2243 module_put(file->event_call->mod);
2244 out_free:
2245 kfree(data);
2246 goto out;
2247}
2248
2249static struct ftrace_func_command event_enable_cmd = {
2250 .name = ENABLE_EVENT_STR,
2251 .func = event_enable_func,
2252};
2253
2254static struct ftrace_func_command event_disable_cmd = {
2255 .name = DISABLE_EVENT_STR,
2256 .func = event_enable_func,
2257};
2258
2259static __init int register_event_cmds(void)
2260{
2261 int ret;
2262
2263 ret = register_ftrace_command(&event_enable_cmd);
2264 if (WARN_ON(ret < 0))
2265 return ret;
2266 ret = register_ftrace_command(&event_disable_cmd);
2267 if (WARN_ON(ret < 0))
2268 unregister_ftrace_command(&event_enable_cmd);
2269 return ret;
2270}
2271#else
2272static inline int register_event_cmds(void) { return 0; }
2273#endif /* CONFIG_DYNAMIC_FTRACE */
2274
Steven Rostedt77248222013-02-27 16:28:06 -05002275/*
2276 * The top level array has already had its ftrace_event_file
2277 * descriptors created in order to allow for early events to
2278 * be recorded. This function is called after the debugfs has been
2279 * initialized, and we now have to create the files associated
2280 * to the events.
2281 */
2282static __init void
2283__trace_early_add_event_dirs(struct trace_array *tr)
2284{
2285 struct ftrace_event_file *file;
2286 int ret;
2287
2288
2289 list_for_each_entry(file, &tr->events, list) {
2290 ret = event_create_dir(tr->event_dir, file,
2291 &ftrace_event_id_fops,
2292 &ftrace_enable_fops,
2293 &ftrace_event_filter_fops,
2294 &ftrace_event_format_fops);
2295 if (ret < 0)
2296 pr_warning("Could not create directory for event %s\n",
2297 file->event_call->name);
2298 }
2299}
2300
2301/*
2302 * For early boot up, the top trace array requires to have
2303 * a list of events that can be enabled. This must be done before
2304 * the filesystem is set up in order to allow events to be traced
2305 * early.
2306 */
2307static __init void
2308__trace_early_add_events(struct trace_array *tr)
2309{
2310 struct ftrace_event_call *call;
2311 int ret;
2312
2313 list_for_each_entry(call, &ftrace_events, list) {
2314 /* Early boot up should not have any modules loaded */
2315 if (WARN_ON_ONCE(call->mod))
2316 continue;
2317
2318 ret = __trace_early_add_new_event(call, tr);
2319 if (ret < 0)
2320 pr_warning("Could not create early event %s\n",
2321 call->name);
2322 }
2323}
2324
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002325/* Remove the event directory structure for a trace directory. */
2326static void
2327__trace_remove_event_dirs(struct trace_array *tr)
2328{
2329 struct ftrace_event_file *file, *next;
2330
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002331 list_for_each_entry_safe(file, next, &tr->events, list)
2332 remove_event_file_dir(file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002333}
2334
Steven Rostedtae63b312012-05-03 23:09:03 -04002335static void
2336__add_event_to_tracers(struct ftrace_event_call *call,
2337 struct ftrace_module_file_ops *file_ops)
2338{
2339 struct trace_array *tr;
2340
2341 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2342 if (file_ops)
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05002343 __trace_add_new_mod_event(call, tr, file_ops);
Steven Rostedtae63b312012-05-03 23:09:03 -04002344 else
2345 __trace_add_new_event(call, tr,
2346 &ftrace_event_id_fops,
2347 &ftrace_enable_fops,
2348 &ftrace_event_filter_fops,
2349 &ftrace_event_format_fops);
2350 }
2351}
2352
Steven Rostedtec827c72009-09-14 10:50:23 -04002353static struct notifier_block trace_module_nb = {
Steven Rostedt6d723732009-04-10 14:53:50 -04002354 .notifier_call = trace_module_notify,
2355 .priority = 0,
2356};
2357
Steven Rostedte4a9ea52011-01-27 09:15:30 -05002358extern struct ftrace_event_call *__start_ftrace_events[];
2359extern struct ftrace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002360
Li Zefan020e5f82009-07-01 10:47:05 +08002361static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2362
2363static __init int setup_trace_event(char *str)
2364{
2365 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002366 ring_buffer_expanded = true;
2367 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002368
2369 return 1;
2370}
2371__setup("trace_event=", setup_trace_event);
2372
Steven Rostedt77248222013-02-27 16:28:06 -05002373/* Expects to have event_mutex held when called */
2374static int
2375create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b312012-05-03 23:09:03 -04002376{
2377 struct dentry *d_events;
2378 struct dentry *entry;
2379
2380 entry = debugfs_create_file("set_event", 0644, parent,
2381 tr, &ftrace_set_event_fops);
2382 if (!entry) {
2383 pr_warning("Could not create debugfs 'set_event' entry\n");
2384 return -ENOMEM;
2385 }
2386
2387 d_events = debugfs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002388 if (!d_events) {
Steven Rostedtae63b312012-05-03 23:09:03 -04002389 pr_warning("Could not create debugfs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002390 return -ENOMEM;
2391 }
Steven Rostedtae63b312012-05-03 23:09:03 -04002392
2393 /* ring buffer internal formats */
2394 trace_create_file("header_page", 0444, d_events,
2395 ring_buffer_print_page_header,
2396 &ftrace_show_header_fops);
2397
2398 trace_create_file("header_event", 0444, d_events,
2399 ring_buffer_print_entry_header,
2400 &ftrace_show_header_fops);
2401
2402 trace_create_file("enable", 0644, d_events,
2403 tr, &ftrace_tr_enable_fops);
2404
2405 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002406
2407 return 0;
2408}
2409
2410/**
2411 * event_trace_add_tracer - add a instance of a trace_array to events
2412 * @parent: The parent dentry to place the files/directories for events in
2413 * @tr: The trace array associated with these events
2414 *
2415 * When a new instance is created, it needs to set up its events
2416 * directory, as well as other files associated with events. It also
2417 * creates the event hierachry in the @parent/events directory.
2418 *
2419 * Returns 0 on success.
2420 */
2421int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2422{
2423 int ret;
2424
2425 mutex_lock(&event_mutex);
2426
2427 ret = create_event_toplevel_files(parent, tr);
2428 if (ret)
2429 goto out_unlock;
2430
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002431 down_write(&trace_event_sem);
Steven Rostedtae63b312012-05-03 23:09:03 -04002432 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002433 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002434
Steven Rostedt77248222013-02-27 16:28:06 -05002435 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04002436 mutex_unlock(&event_mutex);
Steven Rostedtae63b312012-05-03 23:09:03 -04002437
Steven Rostedt77248222013-02-27 16:28:06 -05002438 return ret;
2439}
2440
2441/*
2442 * The top trace array already had its file descriptors created.
2443 * Now the files themselves need to be created.
2444 */
2445static __init int
2446early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2447{
2448 int ret;
2449
2450 mutex_lock(&event_mutex);
2451
2452 ret = create_event_toplevel_files(parent, tr);
2453 if (ret)
2454 goto out_unlock;
2455
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002456 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002457 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002458 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002459
2460 out_unlock:
2461 mutex_unlock(&event_mutex);
2462
2463 return ret;
Steven Rostedtae63b312012-05-03 23:09:03 -04002464}
2465
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002466int event_trace_del_tracer(struct trace_array *tr)
2467{
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002468 mutex_lock(&event_mutex);
2469
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -04002470 /* Disable any running events */
2471 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2472
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002473 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002474 __trace_remove_event_dirs(tr);
2475 debugfs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002476 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002477
2478 tr->event_dir = NULL;
2479
2480 mutex_unlock(&event_mutex);
2481
2482 return 0;
2483}
2484
Steven Rostedtd1a29142013-02-27 20:23:57 -05002485static __init int event_trace_memsetup(void)
2486{
2487 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2488 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2489 return 0;
2490}
2491
Ezequiel Garcia87819152012-09-12 11:47:57 -03002492static __init int event_trace_enable(void)
2493{
Steven Rostedtae63b312012-05-03 23:09:03 -04002494 struct trace_array *tr = top_trace_array();
Ezequiel Garcia87819152012-09-12 11:47:57 -03002495 struct ftrace_event_call **iter, *call;
2496 char *buf = bootup_event_buf;
2497 char *token;
2498 int ret;
2499
2500 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2501
2502 call = *iter;
2503 ret = event_init(call);
2504 if (!ret)
2505 list_add(&call->list, &ftrace_events);
2506 }
2507
Steven Rostedt77248222013-02-27 16:28:06 -05002508 /*
2509 * We need the top trace array to have a working set of trace
2510 * points at early init, before the debug files and directories
2511 * are created. Create the file entries now, and attach them
2512 * to the actual file dentries later.
2513 */
2514 __trace_early_add_events(tr);
2515
Ezequiel Garcia87819152012-09-12 11:47:57 -03002516 while (true) {
2517 token = strsep(&buf, ",");
2518
2519 if (!token)
2520 break;
2521 if (!*token)
2522 continue;
2523
Steven Rostedtae63b312012-05-03 23:09:03 -04002524 ret = ftrace_set_clr_event(tr, token, 1);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002525 if (ret)
2526 pr_warn("Failed to enable trace event: %s\n", token);
2527 }
Steven Rostedt81698832012-10-11 10:15:05 -04002528
2529 trace_printk_start_comm();
2530
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002531 register_event_cmds();
2532
Ezequiel Garcia87819152012-09-12 11:47:57 -03002533 return 0;
2534}
2535
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002536static __init int event_trace_init(void)
2537{
Steven Rostedtae63b312012-05-03 23:09:03 -04002538 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002539 struct dentry *d_tracer;
2540 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04002541 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002542
Steven Rostedtae63b312012-05-03 23:09:03 -04002543 tr = top_trace_array();
2544
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002545 d_tracer = tracing_init_dentry();
2546 if (!d_tracer)
2547 return 0;
2548
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002549 entry = debugfs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b312012-05-03 23:09:03 -04002550 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002551 if (!entry)
2552 pr_warning("Could not create debugfs "
2553 "'available_events' entry\n");
2554
Li Zefan8728fe52010-05-24 16:22:49 +08002555 if (trace_define_common_fields())
2556 pr_warning("tracing: Failed to allocate common fields");
2557
Steven Rostedt77248222013-02-27 16:28:06 -05002558 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b312012-05-03 23:09:03 -04002559 if (ret)
2560 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08002561
Steven Rostedt6d723732009-04-10 14:53:50 -04002562 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08002563 if (ret)
Steven Rostedt6d723732009-04-10 14:53:50 -04002564 pr_warning("Failed to register trace events module notifier\n");
2565
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002566 return 0;
2567}
Steven Rostedtd1a29142013-02-27 20:23:57 -05002568early_initcall(event_trace_memsetup);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002569core_initcall(event_trace_enable);
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002570fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002571
2572#ifdef CONFIG_FTRACE_STARTUP_TEST
2573
2574static DEFINE_SPINLOCK(test_spinlock);
2575static DEFINE_SPINLOCK(test_spinlock_irq);
2576static DEFINE_MUTEX(test_mutex);
2577
2578static __init void test_work(struct work_struct *dummy)
2579{
2580 spin_lock(&test_spinlock);
2581 spin_lock_irq(&test_spinlock_irq);
2582 udelay(1);
2583 spin_unlock_irq(&test_spinlock_irq);
2584 spin_unlock(&test_spinlock);
2585
2586 mutex_lock(&test_mutex);
2587 msleep(1);
2588 mutex_unlock(&test_mutex);
2589}
2590
2591static __init int event_test_thread(void *unused)
2592{
2593 void *test_malloc;
2594
2595 test_malloc = kmalloc(1234, GFP_KERNEL);
2596 if (!test_malloc)
2597 pr_info("failed to kmalloc\n");
2598
2599 schedule_on_each_cpu(test_work);
2600
2601 kfree(test_malloc);
2602
2603 set_current_state(TASK_INTERRUPTIBLE);
2604 while (!kthread_should_stop())
2605 schedule();
2606
2607 return 0;
2608}
2609
2610/*
2611 * Do various things that may trigger events.
2612 */
2613static __init void event_test_stuff(void)
2614{
2615 struct task_struct *test_thread;
2616
2617 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2618 msleep(1);
2619 kthread_stop(test_thread);
2620}
2621
2622/*
2623 * For every trace event defined, we will test each trace point separately,
2624 * and then by groups, and finally all trace points.
2625 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002626static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04002627{
Steven Rostedtae63b312012-05-03 23:09:03 -04002628 struct ftrace_subsystem_dir *dir;
2629 struct ftrace_event_file *file;
Steven Rostedte6187002009-04-15 13:36:40 -04002630 struct ftrace_event_call *call;
2631 struct event_subsystem *system;
Steven Rostedtae63b312012-05-03 23:09:03 -04002632 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04002633 int ret;
2634
Steven Rostedtae63b312012-05-03 23:09:03 -04002635 tr = top_trace_array();
2636
Steven Rostedte6187002009-04-15 13:36:40 -04002637 pr_info("Running tests on trace events:\n");
2638
Steven Rostedtae63b312012-05-03 23:09:03 -04002639 list_for_each_entry(file, &tr->events, list) {
2640
2641 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04002642
Steven Rostedt22392912010-04-21 12:27:06 -04002643 /* Only test those that have a probe */
2644 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04002645 continue;
2646
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002647/*
2648 * Testing syscall events here is pretty useless, but
2649 * we still do it if configured. But this is time consuming.
2650 * What we really need is a user thread to perform the
2651 * syscalls as we test.
2652 */
2653#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04002654 if (call->class->system &&
2655 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002656 continue;
2657#endif
2658
Steven Rostedte6187002009-04-15 13:36:40 -04002659 pr_info("Testing event %s: ", call->name);
2660
2661 /*
2662 * If an event is already enabled, someone is using
2663 * it and the self test should not be on.
2664 */
Steven Rostedtae63b312012-05-03 23:09:03 -04002665 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
Steven Rostedte6187002009-04-15 13:36:40 -04002666 pr_warning("Enabled event during self test!\n");
2667 WARN_ON_ONCE(1);
2668 continue;
2669 }
2670
Steven Rostedtae63b312012-05-03 23:09:03 -04002671 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002672 event_test_stuff();
Steven Rostedtae63b312012-05-03 23:09:03 -04002673 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002674
2675 pr_cont("OK\n");
2676 }
2677
2678 /* Now test at the sub system level */
2679
2680 pr_info("Running tests on trace event systems:\n");
2681
Steven Rostedtae63b312012-05-03 23:09:03 -04002682 list_for_each_entry(dir, &tr->systems, list) {
2683
2684 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04002685
2686 /* the ftrace system is special, skip it */
2687 if (strcmp(system->name, "ftrace") == 0)
2688 continue;
2689
2690 pr_info("Testing event system %s: ", system->name);
2691
Steven Rostedtae63b312012-05-03 23:09:03 -04002692 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002693 if (WARN_ON_ONCE(ret)) {
2694 pr_warning("error enabling system %s\n",
2695 system->name);
2696 continue;
2697 }
2698
2699 event_test_stuff();
2700
Steven Rostedtae63b312012-05-03 23:09:03 -04002701 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002702 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04002703 pr_warning("error disabling system %s\n",
2704 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002705 continue;
2706 }
Steven Rostedte6187002009-04-15 13:36:40 -04002707
2708 pr_cont("OK\n");
2709 }
2710
2711 /* Test with all events enabled */
2712
2713 pr_info("Running tests on all trace events:\n");
2714 pr_info("Testing all events: ");
2715
Steven Rostedtae63b312012-05-03 23:09:03 -04002716 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002717 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04002718 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002719 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002720 }
2721
2722 event_test_stuff();
2723
2724 /* reset sysname */
Steven Rostedtae63b312012-05-03 23:09:03 -04002725 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002726 if (WARN_ON_ONCE(ret)) {
2727 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002728 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002729 }
2730
2731 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002732}
2733
2734#ifdef CONFIG_FUNCTION_TRACER
2735
Tejun Heo245b2e72009-06-24 15:13:48 +09002736static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002737
2738static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002739function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002740 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002741{
2742 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002743 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002744 struct ftrace_entry *entry;
2745 unsigned long flags;
2746 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002747 int cpu;
2748 int pc;
2749
2750 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002751 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002752 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09002753 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002754
2755 if (disabled != 1)
2756 goto out;
2757
2758 local_save_flags(flags);
2759
Steven Rostedte77405a2009-09-02 14:17:06 -04002760 event = trace_current_buffer_lock_reserve(&buffer,
2761 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002762 flags, pc);
2763 if (!event)
2764 goto out;
2765 entry = ring_buffer_event_data(event);
2766 entry->ip = ip;
2767 entry->parent_ip = parent_ip;
2768
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002769 trace_buffer_unlock_commit(buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002770
2771 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09002772 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04002773 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002774}
2775
2776static struct ftrace_ops trace_ops __initdata =
2777{
2778 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04002779 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002780};
2781
2782static __init void event_trace_self_test_with_function(void)
2783{
Steven Rostedt17bb6152011-05-23 15:27:46 -04002784 int ret;
2785 ret = register_ftrace_function(&trace_ops);
2786 if (WARN_ON(ret < 0)) {
2787 pr_info("Failed to enable function tracer for event tests\n");
2788 return;
2789 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002790 pr_info("Running tests again, along with the function tracer\n");
2791 event_trace_self_tests();
2792 unregister_ftrace_function(&trace_ops);
2793}
2794#else
2795static __init void event_trace_self_test_with_function(void)
2796{
2797}
2798#endif
2799
2800static __init int event_trace_self_tests_init(void)
2801{
Li Zefan020e5f82009-07-01 10:47:05 +08002802 if (!tracing_selftest_disabled) {
2803 event_trace_self_tests();
2804 event_trace_self_test_with_function();
2805 }
Steven Rostedte6187002009-04-15 13:36:40 -04002806
2807 return 0;
2808}
2809
Steven Rostedt28d20e22009-04-20 12:12:44 -04002810late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002811
2812#endif