blob: 44ac83614c3d05ed044e910890aec8a747743fcc [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 Rostedtae63b31e2012-05-03 23:09:03 -040044/* Double loops, do not use break, only goto's work */
45#define do_for_each_event_file(tr, file) \
46 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
47 list_for_each_entry(file, &tr->events, list)
48
49#define do_for_each_event_file_safe(tr, file) \
50 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
51 struct ftrace_event_file *___n; \
52 list_for_each_entry_safe(file, ___n, &tr->events, list)
53
54#define while_for_each_event_file() \
55 }
56
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080057static struct list_head *
Steven Rostedt2e33af02010-04-22 10:35:55 -040058trace_get_fields(struct ftrace_event_call *event_call)
59{
60 if (!event_call->class->get_fields)
61 return &event_call->class->fields;
62 return event_call->class->get_fields(event_call);
63}
64
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080065static struct ftrace_event_field *
66__find_event_field(struct list_head *head, char *name)
67{
68 struct ftrace_event_field *field;
69
70 list_for_each_entry(field, head, link) {
71 if (!strcmp(field->name, name))
72 return field;
73 }
74
75 return NULL;
76}
77
78struct ftrace_event_field *
79trace_find_event_field(struct ftrace_event_call *call, char *name)
80{
81 struct ftrace_event_field *field;
82 struct list_head *head;
83
84 field = __find_event_field(&ftrace_common_fields, name);
85 if (field)
86 return field;
87
88 head = trace_get_fields(call);
89 return __find_event_field(head, name);
90}
91
Li Zefan8728fe52010-05-24 16:22:49 +080092static int __trace_define_field(struct list_head *head, const char *type,
93 const char *name, int offset, int size,
94 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -050095{
96 struct ftrace_event_field *field;
97
Steven Rostedtd1a29142013-02-27 20:23:57 -050098 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -050099 if (!field)
100 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100101
Steven Rostedt92edca02013-02-27 20:41:37 -0500102 field->name = name;
103 field->type = type;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100104
Li Zefan43b51ea2009-08-07 10:33:22 +0800105 if (filter_type == FILTER_OTHER)
106 field->filter_type = filter_assign_type(type);
107 else
108 field->filter_type = filter_type;
109
Tom Zanussicf027f62009-03-22 03:30:39 -0500110 field->offset = offset;
111 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -0500112 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +0800113
Steven Rostedt2e33af02010-04-22 10:35:55 -0400114 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -0500115
116 return 0;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100117
Tom Zanussicf027f62009-03-22 03:30:39 -0500118err:
Steven Rostedtd1a29142013-02-27 20:23:57 -0500119 kmem_cache_free(field_cachep, field);
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100120
Tom Zanussicf027f62009-03-22 03:30:39 -0500121 return -ENOMEM;
122}
Li Zefan8728fe52010-05-24 16:22:49 +0800123
124int trace_define_field(struct ftrace_event_call *call, const char *type,
125 const char *name, int offset, int size, int is_signed,
126 int filter_type)
127{
128 struct list_head *head;
129
130 if (WARN_ON(!call->class))
131 return 0;
132
133 head = trace_get_fields(call);
134 return __trace_define_field(head, type, name, offset, size,
135 is_signed, filter_type);
136}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400137EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500138
Li Zefane647d6b2009-08-19 15:54:32 +0800139#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800140 ret = __trace_define_field(&ftrace_common_fields, #type, \
141 "common_" #item, \
142 offsetof(typeof(ent), item), \
143 sizeof(ent.item), \
144 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800145 if (ret) \
146 return ret;
147
Li Zefan8728fe52010-05-24 16:22:49 +0800148static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800149{
150 int ret;
151 struct trace_entry ent;
152
153 __common_field(unsigned short, type);
154 __common_field(unsigned char, flags);
155 __common_field(unsigned char, preempt_count);
156 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800157
158 return ret;
159}
160
zhangwei(Jovi)ad7067ce2013-03-11 15:13:46 +0800161static void trace_destroy_fields(struct ftrace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800162{
163 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400164 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800165
Steven Rostedt2e33af02010-04-22 10:35:55 -0400166 head = trace_get_fields(call);
167 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800168 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500169 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800170 }
171}
172
Li Zefan87d9b4e2009-12-08 11:14:20 +0800173int trace_event_raw_init(struct ftrace_event_call *call)
174{
175 int id;
176
Steven Rostedt80decc72010-04-23 10:00:22 -0400177 id = register_ftrace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800178 if (!id)
179 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800180
181 return 0;
182}
183EXPORT_SYMBOL_GPL(trace_event_raw_init);
184
Jiri Olsaceec0b62012-02-15 15:51:49 +0100185int ftrace_event_reg(struct ftrace_event_call *call,
186 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400187{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400188 struct ftrace_event_file *file = data;
189
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400190 switch (type) {
191 case TRACE_REG_REGISTER:
192 return tracepoint_probe_register(call->name,
193 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400194 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400195 case TRACE_REG_UNREGISTER:
196 tracepoint_probe_unregister(call->name,
197 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400198 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400199 return 0;
200
201#ifdef CONFIG_PERF_EVENTS
202 case TRACE_REG_PERF_REGISTER:
203 return tracepoint_probe_register(call->name,
204 call->class->perf_probe,
205 call);
206 case TRACE_REG_PERF_UNREGISTER:
207 tracepoint_probe_unregister(call->name,
208 call->class->perf_probe,
209 call);
210 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100211 case TRACE_REG_PERF_OPEN:
212 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100213 case TRACE_REG_PERF_ADD:
214 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100215 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400216#endif
217 }
218 return 0;
219}
220EXPORT_SYMBOL_GPL(ftrace_event_reg);
221
Li Zefane870e9a2010-07-02 11:07:32 +0800222void trace_event_enable_cmd_record(bool enable)
223{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400224 struct ftrace_event_file *file;
225 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800226
227 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400228 do_for_each_event_file(tr, file) {
229
230 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800231 continue;
232
233 if (enable) {
234 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400235 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800236 } else {
237 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400238 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800239 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400240 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800241 mutex_unlock(&event_mutex);
242}
243
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400244static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
245 int enable, int soft_disable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500246{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400247 struct ftrace_event_call *call = file->event_call;
Li Zefan3b8e4272009-12-08 11:14:52 +0800248 int ret = 0;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400249 int disable;
Li Zefan3b8e4272009-12-08 11:14:52 +0800250
Steven Rostedtfd994982009-02-28 02:41:25 -0500251 switch (enable) {
252 case 0:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400253 /*
254 * When soft_disable is set and enable is cleared, we want
255 * to clear the SOFT_DISABLED flag but leave the event in the
256 * state that it was. That is, if the event was enabled and
257 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
258 * is set we do not want the event to be enabled before we
259 * clear the bit.
260 *
261 * When soft_disable is not set but the SOFT_MODE flag is,
262 * we do nothing. Do not disable the tracepoint, otherwise
263 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
264 */
265 if (soft_disable) {
266 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
267 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
268 } else
269 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
270
271 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
272 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400273 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800274 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400275 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800276 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400277 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500278 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400279 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
280 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
281 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500282 break;
283 case 1:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400284 /*
285 * When soft_disable is set and enable is set, we want to
286 * register the tracepoint for the event, but leave the event
287 * as is. That means, if the event was already enabled, we do
288 * nothing (but set SOFT_MODE). If the event is disabled, we
289 * set SOFT_DISABLED before enabling the event tracepoint, so
290 * it still seems to be disabled.
291 */
292 if (!soft_disable)
293 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
294 else
295 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
296
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400297 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400298
299 /* Keep the event disabled, when going to SOFT_MODE. */
300 if (soft_disable)
301 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
302
Li Zefane870e9a2010-07-02 11:07:32 +0800303 if (trace_flags & TRACE_ITER_RECORD_CMD) {
304 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400305 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800306 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400307 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800308 if (ret) {
309 tracing_stop_cmdline_record();
310 pr_info("event trace: Could not enable event "
311 "%s\n", call->name);
312 break;
313 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400314 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -0500315
316 /* WAS_ENABLED gets set but never cleared. */
317 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
Steven Rostedtfd994982009-02-28 02:41:25 -0500318 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500319 break;
320 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800321
322 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500323}
324
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400325static int ftrace_event_enable_disable(struct ftrace_event_file *file,
326 int enable)
327{
328 return __ftrace_event_enable_disable(file, enable, 0);
329}
330
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400331static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800332{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400333 struct ftrace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800334
335 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400336 list_for_each_entry(file, &tr->events, list) {
337 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800338 }
339 mutex_unlock(&event_mutex);
340}
341
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400342static void __put_system(struct event_subsystem *system)
343{
344 struct event_filter *filter = system->filter;
345
346 WARN_ON_ONCE(system->ref_count == 0);
347 if (--system->ref_count)
348 return;
349
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400350 list_del(&system->list);
351
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400352 if (filter) {
353 kfree(filter->filter_string);
354 kfree(filter);
355 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400356 kfree(system);
357}
358
359static void __get_system(struct event_subsystem *system)
360{
361 WARN_ON_ONCE(system->ref_count == 0);
362 system->ref_count++;
363}
364
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400365static void __get_system_dir(struct ftrace_subsystem_dir *dir)
366{
367 WARN_ON_ONCE(dir->ref_count == 0);
368 dir->ref_count++;
369 __get_system(dir->subsystem);
370}
371
372static void __put_system_dir(struct ftrace_subsystem_dir *dir)
373{
374 WARN_ON_ONCE(dir->ref_count == 0);
375 /* If the subsystem is about to be freed, the dir must be too */
376 WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
377
378 __put_system(dir->subsystem);
379 if (!--dir->ref_count)
380 kfree(dir);
381}
382
383static void put_system(struct ftrace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400384{
385 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400386 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400387 mutex_unlock(&event_mutex);
388}
389
Li Zefan8f31bfe2009-05-08 10:31:42 +0800390/*
391 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
392 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400393static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
394 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500395{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400396 struct ftrace_event_file *file;
Steven Rostedta59fd602009-04-10 13:52:20 -0400397 struct ftrace_event_call *call;
Steven Rostedt29f93942009-05-08 16:06:47 -0400398 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500399
Steven Rostedt11a241a2009-03-02 11:49:04 -0500400 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400401 list_for_each_entry(file, &tr->events, list) {
402
403 call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500404
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400405 if (!call->name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500406 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500407
Steven Rostedt9b637762012-05-10 15:55:43 -0400408 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
409 continue;
410
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500411 if (match &&
412 strcmp(match, call->name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400413 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500414 continue;
415
Steven Rostedt8f082012010-04-20 10:47:33 -0400416 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500417 continue;
418
419 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500420 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500421
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400422 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500423
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500424 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500425 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500426 mutex_unlock(&event_mutex);
427
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500428 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500429}
430
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400431static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800432{
433 char *event = NULL, *sub = NULL, *match;
434
435 /*
436 * The buf format can be <subsystem>:<event-name>
437 * *:<event-name> means any event by that name.
438 * :<event-name> is the same.
439 *
440 * <subsystem>:* means all events in that subsystem
441 * <subsystem>: means the same.
442 *
443 * <name> (no ':') means all events in a subsystem with
444 * the name <name> or any event that matches <name>
445 */
446
447 match = strsep(&buf, ":");
448 if (buf) {
449 sub = match;
450 event = buf;
451 match = NULL;
452
453 if (!strlen(sub) || strcmp(sub, "*") == 0)
454 sub = NULL;
455 if (!strlen(event) || strcmp(event, "*") == 0)
456 event = NULL;
457 }
458
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400459 return __ftrace_set_clr_event(tr, match, sub, event, set);
Li Zefan8f31bfe2009-05-08 10:31:42 +0800460}
461
Steven Rostedt4671c792009-05-08 16:27:41 -0400462/**
463 * trace_set_clr_event - enable or disable an event
464 * @system: system name to match (NULL for any system)
465 * @event: event name to match (NULL for all events, within system)
466 * @set: 1 to enable, 0 to disable
467 *
468 * This is a way for other parts of the kernel to enable or disable
469 * event recording.
470 *
471 * Returns 0 on success, -EINVAL if the parameters do not match any
472 * registered events.
473 */
474int trace_set_clr_event(const char *system, const char *event, int set)
475{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400476 struct trace_array *tr = top_trace_array();
477
478 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400479}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800480EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400481
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500482/* 128 should be much more than enough */
483#define EVENT_BUF_SIZE 127
484
485static ssize_t
486ftrace_event_write(struct file *file, const char __user *ubuf,
487 size_t cnt, loff_t *ppos)
488{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200489 struct trace_parser parser;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400490 struct seq_file *m = file->private_data;
491 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800492 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500493
Li Zefan4ba79782009-09-22 13:52:20 +0800494 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500495 return 0;
496
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400497 ret = tracing_update_buffers();
498 if (ret < 0)
499 return ret;
500
jolsa@redhat.com48966362009-09-11 17:29:28 +0200501 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500502 return -ENOMEM;
503
jolsa@redhat.com48966362009-09-11 17:29:28 +0200504 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500505
Li Zefan4ba79782009-09-22 13:52:20 +0800506 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200507 int set = 1;
508
509 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500510 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500511
jolsa@redhat.com48966362009-09-11 17:29:28 +0200512 parser.buffer[parser.idx] = 0;
513
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400514 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500515 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200516 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500517 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500518
519 ret = read;
520
jolsa@redhat.com48966362009-09-11 17:29:28 +0200521 out_put:
522 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500523
524 return ret;
525}
526
527static void *
528t_next(struct seq_file *m, void *v, loff_t *pos)
529{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400530 struct ftrace_event_file *file = v;
531 struct ftrace_event_call *call;
532 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500533
534 (*pos)++;
535
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400536 list_for_each_entry_continue(file, &tr->events, list) {
537 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400538 /*
539 * The ftrace subsystem is for showing formats only.
540 * They can not be enabled or disabled via the event files.
541 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400542 if (call->class && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400543 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400544 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500545
Li Zefan30bd39c2009-09-18 14:07:05 +0800546 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500547}
548
549static void *t_start(struct seq_file *m, loff_t *pos)
550{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400551 struct ftrace_event_file *file;
552 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800553 loff_t l;
554
Li Zefan20c89282009-05-06 10:33:45 +0800555 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800556
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400557 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800558 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400559 file = t_next(m, file, &l);
560 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800561 break;
562 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400563 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500564}
565
566static void *
567s_next(struct seq_file *m, void *v, loff_t *pos)
568{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400569 struct ftrace_event_file *file = v;
570 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500571
572 (*pos)++;
573
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400574 list_for_each_entry_continue(file, &tr->events, list) {
575 if (file->flags & FTRACE_EVENT_FL_ENABLED)
576 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500577 }
578
Li Zefan30bd39c2009-09-18 14:07:05 +0800579 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500580}
581
582static void *s_start(struct seq_file *m, loff_t *pos)
583{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400584 struct ftrace_event_file *file;
585 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800586 loff_t l;
587
Li Zefan20c89282009-05-06 10:33:45 +0800588 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800589
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400590 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800591 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400592 file = s_next(m, file, &l);
593 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800594 break;
595 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400596 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500597}
598
599static int t_show(struct seq_file *m, void *v)
600{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400601 struct ftrace_event_file *file = v;
602 struct ftrace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500603
Steven Rostedt8f082012010-04-20 10:47:33 -0400604 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
605 seq_printf(m, "%s:", call->class->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500606 seq_printf(m, "%s\n", call->name);
607
608 return 0;
609}
610
611static void t_stop(struct seq_file *m, void *p)
612{
Li Zefan20c89282009-05-06 10:33:45 +0800613 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500614}
615
Steven Rostedt1473e442009-02-24 14:15:08 -0500616static ssize_t
617event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
618 loff_t *ppos)
619{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400620 struct ftrace_event_file *file = filp->private_data;
Steven Rostedt1473e442009-02-24 14:15:08 -0500621 char *buf;
622
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400623 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
624 if (file->flags & FTRACE_EVENT_FL_SOFT_DISABLED)
625 buf = "0*\n";
626 else
627 buf = "1\n";
628 } else
Steven Rostedt1473e442009-02-24 14:15:08 -0500629 buf = "0\n";
630
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400631 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -0500632}
633
634static ssize_t
635event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
636 loff_t *ppos)
637{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400638 struct ftrace_event_file *file = filp->private_data;
Steven Rostedt1473e442009-02-24 14:15:08 -0500639 unsigned long val;
640 int ret;
641
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400642 if (!file)
643 return -EINVAL;
644
Peter Huewe22fe9b52011-06-07 21:58:27 +0200645 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
646 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -0500647 return ret;
648
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400649 ret = tracing_update_buffers();
650 if (ret < 0)
651 return ret;
652
Steven Rostedt1473e442009-02-24 14:15:08 -0500653 switch (val) {
654 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500655 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500656 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400657 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500658 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500659 break;
660
661 default:
662 return -EINVAL;
663 }
664
665 *ppos += cnt;
666
Li Zefan3b8e4272009-12-08 11:14:52 +0800667 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500668}
669
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400670static ssize_t
671system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
672 loff_t *ppos)
673{
Li Zefanc142b152009-05-08 10:32:05 +0800674 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400675 struct ftrace_subsystem_dir *dir = filp->private_data;
676 struct event_subsystem *system = dir->subsystem;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400677 struct ftrace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400678 struct ftrace_event_file *file;
679 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400680 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800681 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400682 int ret;
683
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400684 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400685 list_for_each_entry(file, &tr->events, list) {
686 call = file->event_call;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400687 if (!call->name || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400688 continue;
689
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400690 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400691 continue;
692
693 /*
694 * We need to find out if all the events are set
695 * or if all events or cleared, or if we have
696 * a mixture.
697 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400698 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +0800699
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400700 /*
701 * If we have a mixture, no need to look further.
702 */
Li Zefanc142b152009-05-08 10:32:05 +0800703 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400704 break;
705 }
706 mutex_unlock(&event_mutex);
707
Li Zefanc142b152009-05-08 10:32:05 +0800708 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400709 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400710
711 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
712
713 return ret;
714}
715
716static ssize_t
717system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
718 loff_t *ppos)
719{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400720 struct ftrace_subsystem_dir *dir = filp->private_data;
721 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400722 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400723 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400724 ssize_t ret;
725
Peter Huewe22fe9b52011-06-07 21:58:27 +0200726 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
727 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400728 return ret;
729
730 ret = tracing_update_buffers();
731 if (ret < 0)
732 return ret;
733
Li Zefan8f31bfe2009-05-08 10:31:42 +0800734 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400735 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400736
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400737 /*
738 * Opening of "enable" adds a ref count to system,
739 * so the name is safe to use.
740 */
741 if (system)
742 name = system->name;
743
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400744 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400745 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800746 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400747
748 ret = cnt;
749
Li Zefan8f31bfe2009-05-08 10:31:42 +0800750out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400751 *ppos += cnt;
752
753 return ret;
754}
755
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400756enum {
757 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +0800758 FORMAT_FIELD_SEPERATOR = 2,
759 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400760};
761
762static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -0500763{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400764 struct ftrace_event_call *call = m->private;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800765 struct ftrace_event_field *field;
Li Zefan86397dc2010-08-17 13:53:06 +0800766 struct list_head *common_head = &ftrace_common_fields;
767 struct list_head *head = trace_get_fields(call);
Steven Rostedt981d0812009-03-02 13:53:59 -0500768
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400769 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800770
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400771 switch ((unsigned long)v) {
772 case FORMAT_HEADER:
Li Zefan86397dc2010-08-17 13:53:06 +0800773 if (unlikely(list_empty(common_head)))
774 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800775
Li Zefan86397dc2010-08-17 13:53:06 +0800776 field = list_entry(common_head->prev,
777 struct ftrace_event_field, link);
778 return field;
779
780 case FORMAT_FIELD_SEPERATOR:
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400781 if (unlikely(list_empty(head)))
782 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800783
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400784 field = list_entry(head->prev, struct ftrace_event_field, link);
785 return field;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800786
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400787 case FORMAT_PRINTFMT:
788 /* all done */
789 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800790 }
791
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400792 field = v;
Li Zefan86397dc2010-08-17 13:53:06 +0800793 if (field->link.prev == common_head)
794 return (void *)FORMAT_FIELD_SEPERATOR;
795 else if (field->link.prev == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400796 return (void *)FORMAT_PRINTFMT;
797
798 field = list_entry(field->link.prev, struct ftrace_event_field, link);
799
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400800 return field;
Li Zefan8728fe52010-05-24 16:22:49 +0800801}
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800802
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400803static void *f_start(struct seq_file *m, loff_t *pos)
Li Zefan8728fe52010-05-24 16:22:49 +0800804{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400805 loff_t l = 0;
806 void *p;
Li Zefan8728fe52010-05-24 16:22:49 +0800807
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400808 /* Start by showing the header */
809 if (!*pos)
810 return (void *)FORMAT_HEADER;
811
812 p = (void *)FORMAT_HEADER;
813 do {
814 p = f_next(m, p, &l);
815 } while (p && l < *pos);
816
817 return p;
818}
819
820static int f_show(struct seq_file *m, void *v)
821{
822 struct ftrace_event_call *call = m->private;
823 struct ftrace_event_field *field;
824 const char *array_descriptor;
825
826 switch ((unsigned long)v) {
827 case FORMAT_HEADER:
828 seq_printf(m, "name: %s\n", call->name);
829 seq_printf(m, "ID: %d\n", call->event.type);
830 seq_printf(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +0800831 return 0;
832
Li Zefan86397dc2010-08-17 13:53:06 +0800833 case FORMAT_FIELD_SEPERATOR:
834 seq_putc(m, '\n');
835 return 0;
836
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400837 case FORMAT_PRINTFMT:
838 seq_printf(m, "\nprint fmt: %s\n",
839 call->print_fmt);
840 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500841 }
842
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400843 field = v;
844
845 /*
846 * Smartly shows the array type(except dynamic array).
847 * Normal:
848 * field:TYPE VAR
849 * If TYPE := TYPE[LEN], it is shown:
850 * field:TYPE VAR[LEN]
851 */
852 array_descriptor = strchr(field->type, '[');
853
854 if (!strncmp(field->type, "__data_loc", 10))
855 array_descriptor = NULL;
856
857 if (!array_descriptor)
858 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
859 field->type, field->name, field->offset,
860 field->size, !!field->is_signed);
861 else
862 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
863 (int)(array_descriptor - field->type),
864 field->type, field->name,
865 array_descriptor, field->offset,
866 field->size, !!field->is_signed);
867
868 return 0;
869}
870
871static void f_stop(struct seq_file *m, void *p)
872{
873}
874
875static const struct seq_operations trace_format_seq_ops = {
876 .start = f_start,
877 .next = f_next,
878 .stop = f_stop,
879 .show = f_show,
880};
881
882static int trace_format_open(struct inode *inode, struct file *file)
883{
884 struct ftrace_event_call *call = inode->i_private;
885 struct seq_file *m;
886 int ret;
887
888 ret = seq_open(file, &trace_format_seq_ops);
889 if (ret < 0)
890 return ret;
891
892 m = file->private_data;
893 m->private = call;
894
895 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500896}
897
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100898static ssize_t
899event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
900{
901 struct ftrace_event_call *call = filp->private_data;
902 struct trace_seq *s;
903 int r;
904
905 if (*ppos)
906 return 0;
907
908 s = kmalloc(sizeof(*s), GFP_KERNEL);
909 if (!s)
910 return -ENOMEM;
911
912 trace_seq_init(s);
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400913 trace_seq_printf(s, "%d\n", call->event.type);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100914
915 r = simple_read_from_buffer(ubuf, cnt, ppos,
916 s->buffer, s->len);
917 kfree(s);
918 return r;
919}
920
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500921static ssize_t
922event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
923 loff_t *ppos)
924{
925 struct ftrace_event_call *call = filp->private_data;
926 struct trace_seq *s;
927 int r;
928
929 if (*ppos)
930 return 0;
931
932 s = kmalloc(sizeof(*s), GFP_KERNEL);
933 if (!s)
934 return -ENOMEM;
935
936 trace_seq_init(s);
937
Tom Zanussi8b372562009-04-28 03:04:59 -0500938 print_event_filter(call, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500939 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500940
941 kfree(s);
942
943 return r;
944}
945
946static ssize_t
947event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
948 loff_t *ppos)
949{
950 struct ftrace_event_call *call = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500951 char *buf;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500952 int err;
953
Tom Zanussi8b372562009-04-28 03:04:59 -0500954 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500955 return -EINVAL;
956
Tom Zanussi8b372562009-04-28 03:04:59 -0500957 buf = (char *)__get_free_page(GFP_TEMPORARY);
958 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500959 return -ENOMEM;
960
Tom Zanussi8b372562009-04-28 03:04:59 -0500961 if (copy_from_user(buf, ubuf, cnt)) {
962 free_page((unsigned long) buf);
963 return -EFAULT;
964 }
965 buf[cnt] = '\0';
966
967 err = apply_event_filter(call, buf);
968 free_page((unsigned long) buf);
969 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500970 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500971
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500972 *ppos += cnt;
973
974 return cnt;
975}
976
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400977static LIST_HEAD(event_subsystems);
978
979static int subsystem_open(struct inode *inode, struct file *filp)
980{
981 struct event_subsystem *system = NULL;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400982 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
983 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400984 int ret;
985
986 /* Make sure the system still exists */
987 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400988 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
989 list_for_each_entry(dir, &tr->systems, list) {
990 if (dir == inode->i_private) {
991 /* Don't open systems with no events */
992 if (dir->nr_events) {
993 __get_system_dir(dir);
994 system = dir->subsystem;
995 }
996 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400997 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400998 }
999 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001000 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001001 mutex_unlock(&event_mutex);
1002
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001003 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001004 return -ENODEV;
1005
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001006 /* Some versions of gcc think dir can be uninitialized here */
1007 WARN_ON(!dir);
1008
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001009 ret = tracing_open_generic(inode, filp);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001010 if (ret < 0)
1011 put_system(dir);
1012
1013 return ret;
1014}
1015
1016static int system_tr_open(struct inode *inode, struct file *filp)
1017{
1018 struct ftrace_subsystem_dir *dir;
1019 struct trace_array *tr = inode->i_private;
1020 int ret;
1021
1022 /* Make a temporary dir that has no system but points to tr */
1023 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1024 if (!dir)
1025 return -ENOMEM;
1026
1027 dir->tr = tr;
1028
1029 ret = tracing_open_generic(inode, filp);
1030 if (ret < 0)
1031 kfree(dir);
1032
1033 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001034
1035 return ret;
1036}
1037
1038static int subsystem_release(struct inode *inode, struct file *file)
1039{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001040 struct ftrace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001041
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001042 /*
1043 * If dir->subsystem is NULL, then this is a temporary
1044 * descriptor that was made for a trace_array to enable
1045 * all subsystems.
1046 */
1047 if (dir->subsystem)
1048 put_system(dir);
1049 else
1050 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001051
1052 return 0;
1053}
1054
Tom Zanussicfb180f2009-03-22 03:31:17 -05001055static ssize_t
1056subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1057 loff_t *ppos)
1058{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001059 struct ftrace_subsystem_dir *dir = filp->private_data;
1060 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001061 struct trace_seq *s;
1062 int r;
1063
1064 if (*ppos)
1065 return 0;
1066
1067 s = kmalloc(sizeof(*s), GFP_KERNEL);
1068 if (!s)
1069 return -ENOMEM;
1070
1071 trace_seq_init(s);
1072
Tom Zanussi8b372562009-04-28 03:04:59 -05001073 print_subsystem_event_filter(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -05001074 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -05001075
1076 kfree(s);
1077
1078 return r;
1079}
1080
1081static ssize_t
1082subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1083 loff_t *ppos)
1084{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001085 struct ftrace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001086 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001087 int err;
1088
Tom Zanussi8b372562009-04-28 03:04:59 -05001089 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001090 return -EINVAL;
1091
Tom Zanussi8b372562009-04-28 03:04:59 -05001092 buf = (char *)__get_free_page(GFP_TEMPORARY);
1093 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001094 return -ENOMEM;
1095
Tom Zanussi8b372562009-04-28 03:04:59 -05001096 if (copy_from_user(buf, ubuf, cnt)) {
1097 free_page((unsigned long) buf);
1098 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001099 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001100 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001101
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001102 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001103 free_page((unsigned long) buf);
1104 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001105 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001106
1107 *ppos += cnt;
1108
1109 return cnt;
1110}
1111
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001112static ssize_t
1113show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1114{
1115 int (*func)(struct trace_seq *s) = filp->private_data;
1116 struct trace_seq *s;
1117 int r;
1118
1119 if (*ppos)
1120 return 0;
1121
1122 s = kmalloc(sizeof(*s), GFP_KERNEL);
1123 if (!s)
1124 return -ENOMEM;
1125
1126 trace_seq_init(s);
1127
1128 func(s);
1129 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1130
1131 kfree(s);
1132
1133 return r;
1134}
1135
Steven Rostedt15075ca2012-05-03 14:57:28 -04001136static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1137static int ftrace_event_set_open(struct inode *inode, struct file *file);
1138
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001139static const struct seq_operations show_event_seq_ops = {
1140 .start = t_start,
1141 .next = t_next,
1142 .show = t_show,
1143 .stop = t_stop,
1144};
1145
1146static const struct seq_operations show_set_event_seq_ops = {
1147 .start = s_start,
1148 .next = s_next,
1149 .show = t_show,
1150 .stop = t_stop,
1151};
1152
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001153static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001154 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001155 .read = seq_read,
1156 .llseek = seq_lseek,
1157 .release = seq_release,
1158};
1159
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001160static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001161 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001162 .read = seq_read,
1163 .write = ftrace_event_write,
1164 .llseek = seq_lseek,
1165 .release = seq_release,
1166};
1167
Steven Rostedt1473e442009-02-24 14:15:08 -05001168static const struct file_operations ftrace_enable_fops = {
1169 .open = tracing_open_generic,
1170 .read = event_enable_read,
1171 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001172 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001173};
1174
Steven Rostedt981d0812009-03-02 13:53:59 -05001175static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001176 .open = trace_format_open,
1177 .read = seq_read,
1178 .llseek = seq_lseek,
1179 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001180};
1181
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001182static const struct file_operations ftrace_event_id_fops = {
1183 .open = tracing_open_generic,
1184 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001185 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001186};
1187
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001188static const struct file_operations ftrace_event_filter_fops = {
1189 .open = tracing_open_generic,
1190 .read = event_filter_read,
1191 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001192 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001193};
1194
Tom Zanussicfb180f2009-03-22 03:31:17 -05001195static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001196 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001197 .read = subsystem_filter_read,
1198 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001199 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001200 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001201};
1202
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001203static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001204 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001205 .read = system_enable_read,
1206 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001207 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001208 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001209};
1210
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001211static const struct file_operations ftrace_tr_enable_fops = {
1212 .open = system_tr_open,
1213 .read = system_enable_read,
1214 .write = system_enable_write,
1215 .llseek = default_llseek,
1216 .release = subsystem_release,
1217};
1218
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001219static const struct file_operations ftrace_show_header_fops = {
1220 .open = tracing_open_generic,
1221 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001222 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001223};
1224
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001225static int
1226ftrace_event_open(struct inode *inode, struct file *file,
1227 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001228{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001229 struct seq_file *m;
1230 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001231
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001232 ret = seq_open(file, seq_ops);
1233 if (ret < 0)
1234 return ret;
1235 m = file->private_data;
1236 /* copy tr over to seq ops */
1237 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001238
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001239 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001240}
1241
Steven Rostedt15075ca2012-05-03 14:57:28 -04001242static int
1243ftrace_event_avail_open(struct inode *inode, struct file *file)
1244{
1245 const struct seq_operations *seq_ops = &show_event_seq_ops;
1246
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001247 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001248}
1249
1250static int
1251ftrace_event_set_open(struct inode *inode, struct file *file)
1252{
1253 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001254 struct trace_array *tr = inode->i_private;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001255
1256 if ((file->f_mode & FMODE_WRITE) &&
1257 (file->f_flags & O_TRUNC))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001258 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001259
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001260 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001261}
1262
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001263static struct event_subsystem *
1264create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001265{
1266 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001267
1268 /* need to create new entry */
1269 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001270 if (!system)
1271 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001272
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001273 system->ref_count = 1;
Steven Rostedt92edca02013-02-27 20:41:37 -05001274 system->name = name;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001275
Tom Zanussi30e673b2009-04-28 03:04:47 -05001276 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001277
Tom Zanussi8b372562009-04-28 03:04:59 -05001278 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001279 if (!system->filter)
1280 goto out_free;
1281
1282 list_add(&system->list, &event_subsystems);
1283
1284 return system;
1285
1286 out_free:
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001287 kfree(system);
1288 return NULL;
1289}
1290
1291static struct dentry *
1292event_subsystem_dir(struct trace_array *tr, const char *name,
1293 struct ftrace_event_file *file, struct dentry *parent)
1294{
1295 struct ftrace_subsystem_dir *dir;
1296 struct event_subsystem *system;
1297 struct dentry *entry;
1298
1299 /* First see if we did not already create this dir */
1300 list_for_each_entry(dir, &tr->systems, list) {
1301 system = dir->subsystem;
1302 if (strcmp(system->name, name) == 0) {
1303 dir->nr_events++;
1304 file->system = dir;
1305 return dir->entry;
1306 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001307 }
1308
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001309 /* Now see if the system itself exists. */
1310 list_for_each_entry(system, &event_subsystems, list) {
1311 if (strcmp(system->name, name) == 0)
1312 break;
1313 }
1314 /* Reset system variable when not found */
1315 if (&system->list == &event_subsystems)
1316 system = NULL;
1317
1318 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1319 if (!dir)
1320 goto out_fail;
1321
1322 if (!system) {
1323 system = create_new_subsystem(name);
1324 if (!system)
1325 goto out_free;
1326 } else
1327 __get_system(system);
1328
1329 dir->entry = debugfs_create_dir(name, parent);
1330 if (!dir->entry) {
1331 pr_warning("Failed to create system directory %s\n", name);
1332 __put_system(system);
1333 goto out_free;
1334 }
1335
1336 dir->tr = tr;
1337 dir->ref_count = 1;
1338 dir->nr_events = 1;
1339 dir->subsystem = system;
1340 file->system = dir;
1341
1342 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001343 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001344 if (!entry) {
1345 kfree(system->filter);
1346 system->filter = NULL;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001347 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001348 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001349
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001350 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001351 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001352
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001353 list_add(&dir->list, &tr->systems);
1354
1355 return dir->entry;
1356
1357 out_free:
1358 kfree(dir);
1359 out_fail:
1360 /* Only print this message if failed on memory allocation */
1361 if (!dir || !system)
1362 pr_warning("No memory to create event subsystem %s\n",
1363 name);
1364 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001365}
1366
Steven Rostedt1473e442009-02-24 14:15:08 -05001367static int
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001368event_create_dir(struct dentry *parent,
1369 struct ftrace_event_file *file,
Steven Rostedt701970b2009-04-24 23:11:22 -04001370 const struct file_operations *id,
1371 const struct file_operations *enable,
1372 const struct file_operations *filter,
1373 const struct file_operations *format)
Steven Rostedt1473e442009-02-24 14:15:08 -05001374{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001375 struct ftrace_event_call *call = file->event_call;
1376 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001377 struct list_head *head;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001378 struct dentry *d_events;
Steven Rostedtfd994982009-02-28 02:41:25 -05001379 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001380
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001381 /*
1382 * If the trace point header did not define TRACE_SYSTEM
1383 * then the system would be called "TRACE_SYSTEM".
1384 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001385 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1386 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1387 if (!d_events)
1388 return -ENOMEM;
1389 } else
1390 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001391
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001392 file->dir = debugfs_create_dir(call->name, d_events);
1393 if (!file->dir) {
1394 pr_warning("Could not create debugfs '%s' directory\n",
1395 call->name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001396 return -1;
1397 }
1398
Steven Rostedt9b637762012-05-10 15:55:43 -04001399 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001400 trace_create_file("enable", 0644, file->dir, file,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001401 enable);
Steven Rostedt1473e442009-02-24 14:15:08 -05001402
Steven Rostedt22392912010-04-21 12:27:06 -04001403#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001404 if (call->event.type && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001405 trace_create_file("id", 0444, file->dir, call,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001406 id);
Steven Rostedt22392912010-04-21 12:27:06 -04001407#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001408
Li Zefanc9d932c2010-05-24 16:24:28 +08001409 /*
1410 * Other events may have the same class. Only update
1411 * the fields if they are not already defined.
1412 */
1413 head = trace_get_fields(call);
1414 if (list_empty(head)) {
1415 ret = call->class->define_fields(call);
1416 if (ret < 0) {
1417 pr_warning("Could not initialize trace point"
1418 " events/%s\n", call->name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001419 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001420 }
1421 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001422 trace_create_file("filter", 0644, file->dir, call,
Li Zefanc9d932c2010-05-24 16:24:28 +08001423 filter);
Tom Zanussicf027f62009-03-22 03:30:39 -05001424
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001425 trace_create_file("format", 0444, file->dir, call,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001426 format);
Steven Rostedtfd994982009-02-28 02:41:25 -05001427
Steven Rostedt1473e442009-02-24 14:15:08 -05001428 return 0;
1429}
1430
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001431static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1432{
1433 if (!dir)
1434 return;
1435
1436 if (!--dir->nr_events) {
1437 debugfs_remove_recursive(dir->entry);
1438 list_del(&dir->list);
1439 __put_system_dir(dir);
1440 }
1441}
1442
1443static void remove_event_from_tracers(struct ftrace_event_call *call)
1444{
1445 struct ftrace_event_file *file;
1446 struct trace_array *tr;
1447
1448 do_for_each_event_file_safe(tr, file) {
1449
1450 if (file->event_call != call)
1451 continue;
1452
1453 list_del(&file->list);
1454 debugfs_remove_recursive(file->dir);
1455 remove_subsystem(file->system);
Steven Rostedtd1a29142013-02-27 20:23:57 -05001456 kmem_cache_free(file_cachep, file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001457
1458 /*
1459 * The do_for_each_event_file_safe() is
1460 * a double loop. After finding the call for this
1461 * trace_array, we use break to jump to the next
1462 * trace_array.
1463 */
1464 break;
1465 } while_for_each_event_file();
1466}
1467
Ezequiel Garcia87819152012-09-12 11:47:57 -03001468static void event_remove(struct ftrace_event_call *call)
1469{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001470 struct trace_array *tr;
1471 struct ftrace_event_file *file;
1472
1473 do_for_each_event_file(tr, file) {
1474 if (file->event_call != call)
1475 continue;
1476 ftrace_event_enable_disable(file, 0);
1477 /*
1478 * The do_for_each_event_file() is
1479 * a double loop. After finding the call for this
1480 * trace_array, we use break to jump to the next
1481 * trace_array.
1482 */
1483 break;
1484 } while_for_each_event_file();
1485
Ezequiel Garcia87819152012-09-12 11:47:57 -03001486 if (call->event.funcs)
1487 __unregister_ftrace_event(&call->event);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001488 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001489 list_del(&call->list);
1490}
1491
1492static int event_init(struct ftrace_event_call *call)
1493{
1494 int ret = 0;
1495
1496 if (WARN_ON(!call->name))
1497 return -EINVAL;
1498
1499 if (call->class->raw_init) {
1500 ret = call->class->raw_init(call);
1501 if (ret < 0 && ret != -ENOSYS)
1502 pr_warn("Could not initialize trace events/%s\n",
1503 call->name);
1504 }
1505
1506 return ret;
1507}
1508
Li Zefan67ead0a2010-05-24 16:25:13 +08001509static int
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001510__register_event(struct ftrace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001511{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001512 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001513
Ezequiel Garcia87819152012-09-12 11:47:57 -03001514 ret = event_init(call);
1515 if (ret < 0)
1516 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04001517
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001518 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08001519 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001520
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001521 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001522}
1523
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001524/* Add an event to a trace directory */
1525static int
1526__trace_add_new_event(struct ftrace_event_call *call,
1527 struct trace_array *tr,
1528 const struct file_operations *id,
1529 const struct file_operations *enable,
1530 const struct file_operations *filter,
1531 const struct file_operations *format)
1532{
1533 struct ftrace_event_file *file;
1534
Steven Rostedtd1a29142013-02-27 20:23:57 -05001535 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001536 if (!file)
1537 return -ENOMEM;
1538
1539 file->event_call = call;
1540 file->tr = tr;
1541 list_add(&file->list, &tr->events);
1542
1543 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1544}
1545
Steven Rostedt77248222013-02-27 16:28:06 -05001546/*
1547 * Just create a decriptor for early init. A descriptor is required
1548 * for enabling events at boot. We want to enable events before
1549 * the filesystem is initialized.
1550 */
1551static __init int
1552__trace_early_add_new_event(struct ftrace_event_call *call,
1553 struct trace_array *tr)
1554{
1555 struct ftrace_event_file *file;
1556
Steven Rostedtd1a29142013-02-27 20:23:57 -05001557 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
Steven Rostedt77248222013-02-27 16:28:06 -05001558 if (!file)
1559 return -ENOMEM;
1560
1561 file->event_call = call;
1562 file->tr = tr;
1563 list_add(&file->list, &tr->events);
1564
1565 return 0;
1566}
1567
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001568struct ftrace_module_file_ops;
1569static void __add_event_to_tracers(struct ftrace_event_call *call,
1570 struct ftrace_module_file_ops *file_ops);
1571
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001572/* Add an additional event_call dynamically */
1573int trace_add_event_call(struct ftrace_event_call *call)
1574{
1575 int ret;
1576 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001577
1578 ret = __register_event(call, NULL);
1579 if (ret >= 0)
1580 __add_event_to_tracers(call, NULL);
1581
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001582 mutex_unlock(&event_mutex);
1583 return ret;
1584}
Steven Rostedt701970b2009-04-24 23:11:22 -04001585
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001586/*
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001587 * Must be called under locking both of event_mutex and trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001588 */
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001589static void __trace_remove_event_call(struct ftrace_event_call *call)
1590{
Ezequiel Garcia87819152012-09-12 11:47:57 -03001591 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001592 trace_destroy_fields(call);
1593 destroy_preds(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001594}
1595
1596/* Remove an event_call */
1597void trace_remove_event_call(struct ftrace_event_call *call)
1598{
1599 mutex_lock(&event_mutex);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001600 down_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001601 __trace_remove_event_call(call);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001602 up_write(&trace_event_sem);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001603 mutex_unlock(&event_mutex);
1604}
1605
1606#define for_each_event(event, start, end) \
1607 for (event = start; \
1608 (unsigned long)event < (unsigned long)end; \
1609 event++)
1610
1611#ifdef CONFIG_MODULES
1612
1613static LIST_HEAD(ftrace_module_file_list);
1614
1615/*
1616 * Modules must own their file_operations to keep up with
1617 * reference counting.
1618 */
1619struct ftrace_module_file_ops {
1620 struct list_head list;
1621 struct module *mod;
1622 struct file_operations id;
1623 struct file_operations enable;
1624 struct file_operations format;
1625 struct file_operations filter;
1626};
1627
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001628static struct ftrace_module_file_ops *
1629find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001630{
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001631 /*
1632 * As event_calls are added in groups by module,
1633 * when we find one file_ops, we don't need to search for
1634 * each call in that module, as the rest should be the
1635 * same. Only search for a new one if the last one did
1636 * not match.
1637 */
1638 if (file_ops && mod == file_ops->mod)
1639 return file_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001640
1641 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1642 if (file_ops->mod == mod)
1643 return file_ops;
1644 }
1645 return NULL;
1646}
1647
Steven Rostedt701970b2009-04-24 23:11:22 -04001648static struct ftrace_module_file_ops *
1649trace_create_file_ops(struct module *mod)
1650{
1651 struct ftrace_module_file_ops *file_ops;
1652
1653 /*
1654 * This is a bit of a PITA. To allow for correct reference
1655 * counting, modules must "own" their file_operations.
1656 * To do this, we allocate the file operations that will be
1657 * used in the event directory.
1658 */
1659
1660 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1661 if (!file_ops)
1662 return NULL;
1663
1664 file_ops->mod = mod;
1665
1666 file_ops->id = ftrace_event_id_fops;
1667 file_ops->id.owner = mod;
1668
1669 file_ops->enable = ftrace_enable_fops;
1670 file_ops->enable.owner = mod;
1671
1672 file_ops->filter = ftrace_event_filter_fops;
1673 file_ops->filter.owner = mod;
1674
1675 file_ops->format = ftrace_event_format_fops;
1676 file_ops->format.owner = mod;
1677
1678 list_add(&file_ops->list, &ftrace_module_file_list);
1679
1680 return file_ops;
1681}
1682
Steven Rostedt6d723732009-04-10 14:53:50 -04001683static void trace_module_add_events(struct module *mod)
1684{
Steven Rostedt701970b2009-04-24 23:11:22 -04001685 struct ftrace_module_file_ops *file_ops = NULL;
Steven Rostedte4a9ea52011-01-27 09:15:30 -05001686 struct ftrace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04001687
1688 start = mod->trace_events;
1689 end = mod->trace_events + mod->num_trace_events;
1690
1691 if (start == end)
1692 return;
1693
Li Zefan67ead0a2010-05-24 16:25:13 +08001694 file_ops = trace_create_file_ops(mod);
1695 if (!file_ops)
Steven Rostedt6d723732009-04-10 14:53:50 -04001696 return;
1697
1698 for_each_event(call, start, end) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001699 __register_event(*call, mod);
1700 __add_event_to_tracers(*call, file_ops);
Steven Rostedt6d723732009-04-10 14:53:50 -04001701 }
1702}
1703
1704static void trace_module_remove_events(struct module *mod)
1705{
Steven Rostedt701970b2009-04-24 23:11:22 -04001706 struct ftrace_module_file_ops *file_ops;
Steven Rostedt6d723732009-04-10 14:53:50 -04001707 struct ftrace_event_call *call, *p;
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001708 bool clear_trace = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001709
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001710 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04001711 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1712 if (call->mod == mod) {
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001713 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1714 clear_trace = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001715 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001716 }
1717 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001718
1719 /* Now free the file_operations */
1720 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1721 if (file_ops->mod == mod)
1722 break;
1723 }
1724 if (&file_ops->list != &ftrace_module_file_list) {
1725 list_del(&file_ops->list);
1726 kfree(file_ops);
1727 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08001728 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001729
1730 /*
1731 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001732 * registered any events that were used. The only worry is if
1733 * a new module gets loaded, and takes on the same id as the events
1734 * of this module. When printing out the buffer, traced events left
1735 * over from this module may be passed to the new module events and
1736 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001737 */
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -05001738 if (clear_trace)
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001739 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04001740}
1741
Steven Rostedt61f919a2009-04-14 18:22:32 -04001742static int trace_module_notify(struct notifier_block *self,
1743 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001744{
1745 struct module *mod = data;
1746
1747 mutex_lock(&event_mutex);
1748 switch (val) {
1749 case MODULE_STATE_COMING:
1750 trace_module_add_events(mod);
1751 break;
1752 case MODULE_STATE_GOING:
1753 trace_module_remove_events(mod);
1754 break;
1755 }
1756 mutex_unlock(&event_mutex);
1757
1758 return 0;
1759}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001760
1761static int
1762__trace_add_new_mod_event(struct ftrace_event_call *call,
1763 struct trace_array *tr,
1764 struct ftrace_module_file_ops *file_ops)
1765{
1766 return __trace_add_new_event(call, tr,
1767 &file_ops->id, &file_ops->enable,
1768 &file_ops->filter, &file_ops->format);
1769}
1770
Steven Rostedt61f919a2009-04-14 18:22:32 -04001771#else
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001772static inline struct ftrace_module_file_ops *
1773find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001774{
1775 return NULL;
1776}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001777static inline int trace_module_notify(struct notifier_block *self,
1778 unsigned long val, void *data)
Steven Rostedt61f919a2009-04-14 18:22:32 -04001779{
1780 return 0;
1781}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001782static inline int
1783__trace_add_new_mod_event(struct ftrace_event_call *call,
1784 struct trace_array *tr,
1785 struct ftrace_module_file_ops *file_ops)
1786{
1787 return -ENODEV;
1788}
Steven Rostedt61f919a2009-04-14 18:22:32 -04001789#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001790
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001791/* Create a new event directory structure for a trace directory. */
1792static void
1793__trace_add_event_dirs(struct trace_array *tr)
1794{
1795 struct ftrace_module_file_ops *file_ops = NULL;
1796 struct ftrace_event_call *call;
1797 int ret;
1798
1799 list_for_each_entry(call, &ftrace_events, list) {
1800 if (call->mod) {
1801 /*
1802 * Directories for events by modules need to
1803 * keep module ref counts when opened (as we don't
1804 * want the module to disappear when reading one
1805 * of these files). The file_ops keep account of
1806 * the module ref count.
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001807 */
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001808 file_ops = find_ftrace_file_ops(file_ops, call->mod);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001809 if (!file_ops)
1810 continue; /* Warn? */
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001811 ret = __trace_add_new_mod_event(call, tr, file_ops);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001812 if (ret < 0)
1813 pr_warning("Could not create directory for event %s\n",
1814 call->name);
1815 continue;
1816 }
1817 ret = __trace_add_new_event(call, tr,
1818 &ftrace_event_id_fops,
1819 &ftrace_enable_fops,
1820 &ftrace_event_filter_fops,
1821 &ftrace_event_format_fops);
1822 if (ret < 0)
1823 pr_warning("Could not create directory for event %s\n",
1824 call->name);
1825 }
1826}
1827
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04001828#ifdef CONFIG_DYNAMIC_FTRACE
1829
1830/* Avoid typos */
1831#define ENABLE_EVENT_STR "enable_event"
1832#define DISABLE_EVENT_STR "disable_event"
1833
1834struct event_probe_data {
1835 struct ftrace_event_file *file;
1836 unsigned long count;
1837 int ref;
1838 bool enable;
1839};
1840
1841static struct ftrace_event_file *
1842find_event_file(struct trace_array *tr, const char *system, const char *event)
1843{
1844 struct ftrace_event_file *file;
1845 struct ftrace_event_call *call;
1846
1847 list_for_each_entry(file, &tr->events, list) {
1848
1849 call = file->event_call;
1850
1851 if (!call->name || !call->class || !call->class->reg)
1852 continue;
1853
1854 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1855 continue;
1856
1857 if (strcmp(event, call->name) == 0 &&
1858 strcmp(system, call->class->system) == 0)
1859 return file;
1860 }
1861 return NULL;
1862}
1863
1864static void
1865event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1866{
1867 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1868 struct event_probe_data *data = *pdata;
1869
1870 if (!data)
1871 return;
1872
1873 if (data->enable)
1874 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1875 else
1876 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1877}
1878
1879static void
1880event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1881{
1882 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1883 struct event_probe_data *data = *pdata;
1884
1885 if (!data)
1886 return;
1887
1888 if (!data->count)
1889 return;
1890
1891 /* Skip if the event is in a state we want to switch to */
1892 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1893 return;
1894
1895 if (data->count != -1)
1896 (data->count)--;
1897
1898 event_enable_probe(ip, parent_ip, _data);
1899}
1900
1901static int
1902event_enable_print(struct seq_file *m, unsigned long ip,
1903 struct ftrace_probe_ops *ops, void *_data)
1904{
1905 struct event_probe_data *data = _data;
1906
1907 seq_printf(m, "%ps:", (void *)ip);
1908
1909 seq_printf(m, "%s:%s:%s",
1910 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1911 data->file->event_call->class->system,
1912 data->file->event_call->name);
1913
1914 if (data->count == -1)
1915 seq_printf(m, ":unlimited\n");
1916 else
1917 seq_printf(m, ":count=%ld\n", data->count);
1918
1919 return 0;
1920}
1921
1922static int
1923event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1924 void **_data)
1925{
1926 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1927 struct event_probe_data *data = *pdata;
1928
1929 data->ref++;
1930 return 0;
1931}
1932
1933static void
1934event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1935 void **_data)
1936{
1937 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1938 struct event_probe_data *data = *pdata;
1939
1940 if (WARN_ON_ONCE(data->ref <= 0))
1941 return;
1942
1943 data->ref--;
1944 if (!data->ref) {
1945 /* Remove the SOFT_MODE flag */
1946 __ftrace_event_enable_disable(data->file, 0, 1);
1947 module_put(data->file->event_call->mod);
1948 kfree(data);
1949 }
1950 *pdata = NULL;
1951}
1952
1953static struct ftrace_probe_ops event_enable_probe_ops = {
1954 .func = event_enable_probe,
1955 .print = event_enable_print,
1956 .init = event_enable_init,
1957 .free = event_enable_free,
1958};
1959
1960static struct ftrace_probe_ops event_enable_count_probe_ops = {
1961 .func = event_enable_count_probe,
1962 .print = event_enable_print,
1963 .init = event_enable_init,
1964 .free = event_enable_free,
1965};
1966
1967static struct ftrace_probe_ops event_disable_probe_ops = {
1968 .func = event_enable_probe,
1969 .print = event_enable_print,
1970 .init = event_enable_init,
1971 .free = event_enable_free,
1972};
1973
1974static struct ftrace_probe_ops event_disable_count_probe_ops = {
1975 .func = event_enable_count_probe,
1976 .print = event_enable_print,
1977 .init = event_enable_init,
1978 .free = event_enable_free,
1979};
1980
1981static int
1982event_enable_func(struct ftrace_hash *hash,
1983 char *glob, char *cmd, char *param, int enabled)
1984{
1985 struct trace_array *tr = top_trace_array();
1986 struct ftrace_event_file *file;
1987 struct ftrace_probe_ops *ops;
1988 struct event_probe_data *data;
1989 const char *system;
1990 const char *event;
1991 char *number;
1992 bool enable;
1993 int ret;
1994
1995 /* hash funcs only work with set_ftrace_filter */
1996 if (!enabled)
1997 return -EINVAL;
1998
1999 if (!param)
2000 return -EINVAL;
2001
2002 system = strsep(&param, ":");
2003 if (!param)
2004 return -EINVAL;
2005
2006 event = strsep(&param, ":");
2007
2008 mutex_lock(&event_mutex);
2009
2010 ret = -EINVAL;
2011 file = find_event_file(tr, system, event);
2012 if (!file)
2013 goto out;
2014
2015 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2016
2017 if (enable)
2018 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2019 else
2020 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2021
2022 if (glob[0] == '!') {
2023 unregister_ftrace_function_probe_func(glob+1, ops);
2024 ret = 0;
2025 goto out;
2026 }
2027
2028 ret = -ENOMEM;
2029 data = kzalloc(sizeof(*data), GFP_KERNEL);
2030 if (!data)
2031 goto out;
2032
2033 data->enable = enable;
2034 data->count = -1;
2035 data->file = file;
2036
2037 if (!param)
2038 goto out_reg;
2039
2040 number = strsep(&param, ":");
2041
2042 ret = -EINVAL;
2043 if (!strlen(number))
2044 goto out_free;
2045
2046 /*
2047 * We use the callback data field (which is a pointer)
2048 * as our counter.
2049 */
2050 ret = kstrtoul(number, 0, &data->count);
2051 if (ret)
2052 goto out_free;
2053
2054 out_reg:
2055 /* Don't let event modules unload while probe registered */
2056 ret = try_module_get(file->event_call->mod);
2057 if (!ret)
2058 goto out_free;
2059
2060 ret = __ftrace_event_enable_disable(file, 1, 1);
2061 if (ret < 0)
2062 goto out_put;
2063 ret = register_ftrace_function_probe(glob, ops, data);
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002064 if (!ret) {
2065 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002066 goto out_disable;
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002067 } else
2068 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002069 out:
2070 mutex_unlock(&event_mutex);
2071 return ret;
2072
2073 out_disable:
2074 __ftrace_event_enable_disable(file, 0, 1);
2075 out_put:
2076 module_put(file->event_call->mod);
2077 out_free:
2078 kfree(data);
2079 goto out;
2080}
2081
2082static struct ftrace_func_command event_enable_cmd = {
2083 .name = ENABLE_EVENT_STR,
2084 .func = event_enable_func,
2085};
2086
2087static struct ftrace_func_command event_disable_cmd = {
2088 .name = DISABLE_EVENT_STR,
2089 .func = event_enable_func,
2090};
2091
2092static __init int register_event_cmds(void)
2093{
2094 int ret;
2095
2096 ret = register_ftrace_command(&event_enable_cmd);
2097 if (WARN_ON(ret < 0))
2098 return ret;
2099 ret = register_ftrace_command(&event_disable_cmd);
2100 if (WARN_ON(ret < 0))
2101 unregister_ftrace_command(&event_enable_cmd);
2102 return ret;
2103}
2104#else
2105static inline int register_event_cmds(void) { return 0; }
2106#endif /* CONFIG_DYNAMIC_FTRACE */
2107
Steven Rostedt77248222013-02-27 16:28:06 -05002108/*
2109 * The top level array has already had its ftrace_event_file
2110 * descriptors created in order to allow for early events to
2111 * be recorded. This function is called after the debugfs has been
2112 * initialized, and we now have to create the files associated
2113 * to the events.
2114 */
2115static __init void
2116__trace_early_add_event_dirs(struct trace_array *tr)
2117{
2118 struct ftrace_event_file *file;
2119 int ret;
2120
2121
2122 list_for_each_entry(file, &tr->events, list) {
2123 ret = event_create_dir(tr->event_dir, file,
2124 &ftrace_event_id_fops,
2125 &ftrace_enable_fops,
2126 &ftrace_event_filter_fops,
2127 &ftrace_event_format_fops);
2128 if (ret < 0)
2129 pr_warning("Could not create directory for event %s\n",
2130 file->event_call->name);
2131 }
2132}
2133
2134/*
2135 * For early boot up, the top trace array requires to have
2136 * a list of events that can be enabled. This must be done before
2137 * the filesystem is set up in order to allow events to be traced
2138 * early.
2139 */
2140static __init void
2141__trace_early_add_events(struct trace_array *tr)
2142{
2143 struct ftrace_event_call *call;
2144 int ret;
2145
2146 list_for_each_entry(call, &ftrace_events, list) {
2147 /* Early boot up should not have any modules loaded */
2148 if (WARN_ON_ONCE(call->mod))
2149 continue;
2150
2151 ret = __trace_early_add_new_event(call, tr);
2152 if (ret < 0)
2153 pr_warning("Could not create early event %s\n",
2154 call->name);
2155 }
2156}
2157
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002158/* Remove the event directory structure for a trace directory. */
2159static void
2160__trace_remove_event_dirs(struct trace_array *tr)
2161{
2162 struct ftrace_event_file *file, *next;
2163
2164 list_for_each_entry_safe(file, next, &tr->events, list) {
2165 list_del(&file->list);
2166 debugfs_remove_recursive(file->dir);
2167 remove_subsystem(file->system);
Steven Rostedtd1a29142013-02-27 20:23:57 -05002168 kmem_cache_free(file_cachep, file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002169 }
2170}
2171
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002172static void
2173__add_event_to_tracers(struct ftrace_event_call *call,
2174 struct ftrace_module_file_ops *file_ops)
2175{
2176 struct trace_array *tr;
2177
2178 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2179 if (file_ops)
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05002180 __trace_add_new_mod_event(call, tr, file_ops);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002181 else
2182 __trace_add_new_event(call, tr,
2183 &ftrace_event_id_fops,
2184 &ftrace_enable_fops,
2185 &ftrace_event_filter_fops,
2186 &ftrace_event_format_fops);
2187 }
2188}
2189
Steven Rostedtec827c72009-09-14 10:50:23 -04002190static struct notifier_block trace_module_nb = {
Steven Rostedt6d723732009-04-10 14:53:50 -04002191 .notifier_call = trace_module_notify,
2192 .priority = 0,
2193};
2194
Steven Rostedte4a9ea52011-01-27 09:15:30 -05002195extern struct ftrace_event_call *__start_ftrace_events[];
2196extern struct ftrace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002197
Li Zefan020e5f82009-07-01 10:47:05 +08002198static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2199
2200static __init int setup_trace_event(char *str)
2201{
2202 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002203 ring_buffer_expanded = true;
2204 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002205
2206 return 1;
2207}
2208__setup("trace_event=", setup_trace_event);
2209
Steven Rostedt77248222013-02-27 16:28:06 -05002210/* Expects to have event_mutex held when called */
2211static int
2212create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002213{
2214 struct dentry *d_events;
2215 struct dentry *entry;
2216
2217 entry = debugfs_create_file("set_event", 0644, parent,
2218 tr, &ftrace_set_event_fops);
2219 if (!entry) {
2220 pr_warning("Could not create debugfs 'set_event' entry\n");
2221 return -ENOMEM;
2222 }
2223
2224 d_events = debugfs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002225 if (!d_events) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002226 pr_warning("Could not create debugfs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002227 return -ENOMEM;
2228 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002229
2230 /* ring buffer internal formats */
2231 trace_create_file("header_page", 0444, d_events,
2232 ring_buffer_print_page_header,
2233 &ftrace_show_header_fops);
2234
2235 trace_create_file("header_event", 0444, d_events,
2236 ring_buffer_print_entry_header,
2237 &ftrace_show_header_fops);
2238
2239 trace_create_file("enable", 0644, d_events,
2240 tr, &ftrace_tr_enable_fops);
2241
2242 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002243
2244 return 0;
2245}
2246
2247/**
2248 * event_trace_add_tracer - add a instance of a trace_array to events
2249 * @parent: The parent dentry to place the files/directories for events in
2250 * @tr: The trace array associated with these events
2251 *
2252 * When a new instance is created, it needs to set up its events
2253 * directory, as well as other files associated with events. It also
2254 * creates the event hierachry in the @parent/events directory.
2255 *
2256 * Returns 0 on success.
2257 */
2258int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2259{
2260 int ret;
2261
2262 mutex_lock(&event_mutex);
2263
2264 ret = create_event_toplevel_files(parent, tr);
2265 if (ret)
2266 goto out_unlock;
2267
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002268 down_write(&trace_event_sem);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002269 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002270 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002271
Steven Rostedt77248222013-02-27 16:28:06 -05002272 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04002273 mutex_unlock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002274
Steven Rostedt77248222013-02-27 16:28:06 -05002275 return ret;
2276}
2277
2278/*
2279 * The top trace array already had its file descriptors created.
2280 * Now the files themselves need to be created.
2281 */
2282static __init int
2283early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2284{
2285 int ret;
2286
2287 mutex_lock(&event_mutex);
2288
2289 ret = create_event_toplevel_files(parent, tr);
2290 if (ret)
2291 goto out_unlock;
2292
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002293 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002294 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002295 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05002296
2297 out_unlock:
2298 mutex_unlock(&event_mutex);
2299
2300 return ret;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002301}
2302
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002303int event_trace_del_tracer(struct trace_array *tr)
2304{
2305 /* Disable any running events */
2306 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2307
2308 mutex_lock(&event_mutex);
2309
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002310 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002311 __trace_remove_event_dirs(tr);
2312 debugfs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002313 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002314
2315 tr->event_dir = NULL;
2316
2317 mutex_unlock(&event_mutex);
2318
2319 return 0;
2320}
2321
Steven Rostedtd1a29142013-02-27 20:23:57 -05002322static __init int event_trace_memsetup(void)
2323{
2324 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2325 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2326 return 0;
2327}
2328
Ezequiel Garcia87819152012-09-12 11:47:57 -03002329static __init int event_trace_enable(void)
2330{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002331 struct trace_array *tr = top_trace_array();
Ezequiel Garcia87819152012-09-12 11:47:57 -03002332 struct ftrace_event_call **iter, *call;
2333 char *buf = bootup_event_buf;
2334 char *token;
2335 int ret;
2336
2337 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2338
2339 call = *iter;
2340 ret = event_init(call);
2341 if (!ret)
2342 list_add(&call->list, &ftrace_events);
2343 }
2344
Steven Rostedt77248222013-02-27 16:28:06 -05002345 /*
2346 * We need the top trace array to have a working set of trace
2347 * points at early init, before the debug files and directories
2348 * are created. Create the file entries now, and attach them
2349 * to the actual file dentries later.
2350 */
2351 __trace_early_add_events(tr);
2352
Ezequiel Garcia87819152012-09-12 11:47:57 -03002353 while (true) {
2354 token = strsep(&buf, ",");
2355
2356 if (!token)
2357 break;
2358 if (!*token)
2359 continue;
2360
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002361 ret = ftrace_set_clr_event(tr, token, 1);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002362 if (ret)
2363 pr_warn("Failed to enable trace event: %s\n", token);
2364 }
Steven Rostedt81698832012-10-11 10:15:05 -04002365
2366 trace_printk_start_comm();
2367
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002368 register_event_cmds();
2369
Ezequiel Garcia87819152012-09-12 11:47:57 -03002370 return 0;
2371}
2372
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002373static __init int event_trace_init(void)
2374{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002375 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002376 struct dentry *d_tracer;
2377 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04002378 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002379
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002380 tr = top_trace_array();
2381
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002382 d_tracer = tracing_init_dentry();
2383 if (!d_tracer)
2384 return 0;
2385
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002386 entry = debugfs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002387 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002388 if (!entry)
2389 pr_warning("Could not create debugfs "
2390 "'available_events' entry\n");
2391
Li Zefan8728fe52010-05-24 16:22:49 +08002392 if (trace_define_common_fields())
2393 pr_warning("tracing: Failed to allocate common fields");
2394
Steven Rostedt77248222013-02-27 16:28:06 -05002395 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002396 if (ret)
2397 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08002398
Steven Rostedt6d723732009-04-10 14:53:50 -04002399 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08002400 if (ret)
Steven Rostedt6d723732009-04-10 14:53:50 -04002401 pr_warning("Failed to register trace events module notifier\n");
2402
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002403 return 0;
2404}
Steven Rostedtd1a29142013-02-27 20:23:57 -05002405early_initcall(event_trace_memsetup);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002406core_initcall(event_trace_enable);
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002407fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002408
2409#ifdef CONFIG_FTRACE_STARTUP_TEST
2410
2411static DEFINE_SPINLOCK(test_spinlock);
2412static DEFINE_SPINLOCK(test_spinlock_irq);
2413static DEFINE_MUTEX(test_mutex);
2414
2415static __init void test_work(struct work_struct *dummy)
2416{
2417 spin_lock(&test_spinlock);
2418 spin_lock_irq(&test_spinlock_irq);
2419 udelay(1);
2420 spin_unlock_irq(&test_spinlock_irq);
2421 spin_unlock(&test_spinlock);
2422
2423 mutex_lock(&test_mutex);
2424 msleep(1);
2425 mutex_unlock(&test_mutex);
2426}
2427
2428static __init int event_test_thread(void *unused)
2429{
2430 void *test_malloc;
2431
2432 test_malloc = kmalloc(1234, GFP_KERNEL);
2433 if (!test_malloc)
2434 pr_info("failed to kmalloc\n");
2435
2436 schedule_on_each_cpu(test_work);
2437
2438 kfree(test_malloc);
2439
2440 set_current_state(TASK_INTERRUPTIBLE);
2441 while (!kthread_should_stop())
2442 schedule();
2443
2444 return 0;
2445}
2446
2447/*
2448 * Do various things that may trigger events.
2449 */
2450static __init void event_test_stuff(void)
2451{
2452 struct task_struct *test_thread;
2453
2454 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2455 msleep(1);
2456 kthread_stop(test_thread);
2457}
2458
2459/*
2460 * For every trace event defined, we will test each trace point separately,
2461 * and then by groups, and finally all trace points.
2462 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002463static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04002464{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002465 struct ftrace_subsystem_dir *dir;
2466 struct ftrace_event_file *file;
Steven Rostedte6187002009-04-15 13:36:40 -04002467 struct ftrace_event_call *call;
2468 struct event_subsystem *system;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002469 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04002470 int ret;
2471
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002472 tr = top_trace_array();
2473
Steven Rostedte6187002009-04-15 13:36:40 -04002474 pr_info("Running tests on trace events:\n");
2475
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002476 list_for_each_entry(file, &tr->events, list) {
2477
2478 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04002479
Steven Rostedt22392912010-04-21 12:27:06 -04002480 /* Only test those that have a probe */
2481 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04002482 continue;
2483
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002484/*
2485 * Testing syscall events here is pretty useless, but
2486 * we still do it if configured. But this is time consuming.
2487 * What we really need is a user thread to perform the
2488 * syscalls as we test.
2489 */
2490#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04002491 if (call->class->system &&
2492 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002493 continue;
2494#endif
2495
Steven Rostedte6187002009-04-15 13:36:40 -04002496 pr_info("Testing event %s: ", call->name);
2497
2498 /*
2499 * If an event is already enabled, someone is using
2500 * it and the self test should not be on.
2501 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002502 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
Steven Rostedte6187002009-04-15 13:36:40 -04002503 pr_warning("Enabled event during self test!\n");
2504 WARN_ON_ONCE(1);
2505 continue;
2506 }
2507
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002508 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002509 event_test_stuff();
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002510 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002511
2512 pr_cont("OK\n");
2513 }
2514
2515 /* Now test at the sub system level */
2516
2517 pr_info("Running tests on trace event systems:\n");
2518
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002519 list_for_each_entry(dir, &tr->systems, list) {
2520
2521 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04002522
2523 /* the ftrace system is special, skip it */
2524 if (strcmp(system->name, "ftrace") == 0)
2525 continue;
2526
2527 pr_info("Testing event system %s: ", system->name);
2528
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002529 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002530 if (WARN_ON_ONCE(ret)) {
2531 pr_warning("error enabling system %s\n",
2532 system->name);
2533 continue;
2534 }
2535
2536 event_test_stuff();
2537
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002538 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002539 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04002540 pr_warning("error disabling system %s\n",
2541 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002542 continue;
2543 }
Steven Rostedte6187002009-04-15 13:36:40 -04002544
2545 pr_cont("OK\n");
2546 }
2547
2548 /* Test with all events enabled */
2549
2550 pr_info("Running tests on all trace events:\n");
2551 pr_info("Testing all events: ");
2552
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002553 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002554 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04002555 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002556 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002557 }
2558
2559 event_test_stuff();
2560
2561 /* reset sysname */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002562 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002563 if (WARN_ON_ONCE(ret)) {
2564 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002565 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002566 }
2567
2568 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002569}
2570
2571#ifdef CONFIG_FUNCTION_TRACER
2572
Tejun Heo245b2e72009-06-24 15:13:48 +09002573static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002574
2575static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002576function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002577 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002578{
2579 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002580 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002581 struct ftrace_entry *entry;
2582 unsigned long flags;
2583 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002584 int cpu;
2585 int pc;
2586
2587 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002588 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002589 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09002590 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002591
2592 if (disabled != 1)
2593 goto out;
2594
2595 local_save_flags(flags);
2596
Steven Rostedte77405a2009-09-02 14:17:06 -04002597 event = trace_current_buffer_lock_reserve(&buffer,
2598 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002599 flags, pc);
2600 if (!event)
2601 goto out;
2602 entry = ring_buffer_event_data(event);
2603 entry->ip = ip;
2604 entry->parent_ip = parent_ip;
2605
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002606 trace_buffer_unlock_commit(buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002607
2608 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09002609 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04002610 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002611}
2612
2613static struct ftrace_ops trace_ops __initdata =
2614{
2615 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04002616 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002617};
2618
2619static __init void event_trace_self_test_with_function(void)
2620{
Steven Rostedt17bb6152011-05-23 15:27:46 -04002621 int ret;
2622 ret = register_ftrace_function(&trace_ops);
2623 if (WARN_ON(ret < 0)) {
2624 pr_info("Failed to enable function tracer for event tests\n");
2625 return;
2626 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002627 pr_info("Running tests again, along with the function tracer\n");
2628 event_trace_self_tests();
2629 unregister_ftrace_function(&trace_ops);
2630}
2631#else
2632static __init void event_trace_self_test_with_function(void)
2633{
2634}
2635#endif
2636
2637static __init int event_trace_self_tests_init(void)
2638{
Li Zefan020e5f82009-07-01 10:47:05 +08002639 if (!tracing_selftest_disabled) {
2640 event_trace_self_tests();
2641 event_trace_self_test_with_function();
2642 }
Steven Rostedte6187002009-04-15 13:36:40 -04002643
2644 return 0;
2645}
2646
Steven Rostedt28d20e22009-04-20 12:12:44 -04002647late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002648
2649#endif