blob: 0f1307a29fcfd6aa525461e2b76ae428d905128c [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);
Li Zefan8728fe52010-05-24 16:22:49 +080037LIST_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
Steven Rostedt2e33af02010-04-22 10:35:55 -040057struct list_head *
58trace_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
Li Zefan8728fe52010-05-24 16:22:49 +080065static int __trace_define_field(struct list_head *head, const char *type,
66 const char *name, int offset, int size,
67 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -050068{
69 struct ftrace_event_field *field;
70
Steven Rostedtd1a29142013-02-27 20:23:57 -050071 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -050072 if (!field)
73 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010074
Steven Rostedt92edca02013-02-27 20:41:37 -050075 field->name = name;
76 field->type = type;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010077
Li Zefan43b51ea2009-08-07 10:33:22 +080078 if (filter_type == FILTER_OTHER)
79 field->filter_type = filter_assign_type(type);
80 else
81 field->filter_type = filter_type;
82
Tom Zanussicf027f62009-03-22 03:30:39 -050083 field->offset = offset;
84 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -050085 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +080086
Steven Rostedt2e33af02010-04-22 10:35:55 -040087 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -050088
89 return 0;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010090
Tom Zanussicf027f62009-03-22 03:30:39 -050091err:
Steven Rostedtd1a29142013-02-27 20:23:57 -050092 kmem_cache_free(field_cachep, field);
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010093
Tom Zanussicf027f62009-03-22 03:30:39 -050094 return -ENOMEM;
95}
Li Zefan8728fe52010-05-24 16:22:49 +080096
97int trace_define_field(struct ftrace_event_call *call, const char *type,
98 const char *name, int offset, int size, int is_signed,
99 int filter_type)
100{
101 struct list_head *head;
102
103 if (WARN_ON(!call->class))
104 return 0;
105
106 head = trace_get_fields(call);
107 return __trace_define_field(head, type, name, offset, size,
108 is_signed, filter_type);
109}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400110EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500111
Li Zefane647d6b2009-08-19 15:54:32 +0800112#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800113 ret = __trace_define_field(&ftrace_common_fields, #type, \
114 "common_" #item, \
115 offsetof(typeof(ent), item), \
116 sizeof(ent.item), \
117 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800118 if (ret) \
119 return ret;
120
Li Zefan8728fe52010-05-24 16:22:49 +0800121static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800122{
123 int ret;
124 struct trace_entry ent;
125
126 __common_field(unsigned short, type);
127 __common_field(unsigned char, flags);
128 __common_field(unsigned char, preempt_count);
129 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800130
131 return ret;
132}
133
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400134void trace_destroy_fields(struct ftrace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800135{
136 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400137 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800138
Steven Rostedt2e33af02010-04-22 10:35:55 -0400139 head = trace_get_fields(call);
140 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800141 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500142 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800143 }
144}
145
Li Zefan87d9b4e2009-12-08 11:14:20 +0800146int trace_event_raw_init(struct ftrace_event_call *call)
147{
148 int id;
149
Steven Rostedt80decc72010-04-23 10:00:22 -0400150 id = register_ftrace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800151 if (!id)
152 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800153
154 return 0;
155}
156EXPORT_SYMBOL_GPL(trace_event_raw_init);
157
Jiri Olsaceec0b62012-02-15 15:51:49 +0100158int ftrace_event_reg(struct ftrace_event_call *call,
159 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400160{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400161 struct ftrace_event_file *file = data;
162
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400163 switch (type) {
164 case TRACE_REG_REGISTER:
165 return tracepoint_probe_register(call->name,
166 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400167 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400168 case TRACE_REG_UNREGISTER:
169 tracepoint_probe_unregister(call->name,
170 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400171 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400172 return 0;
173
174#ifdef CONFIG_PERF_EVENTS
175 case TRACE_REG_PERF_REGISTER:
176 return tracepoint_probe_register(call->name,
177 call->class->perf_probe,
178 call);
179 case TRACE_REG_PERF_UNREGISTER:
180 tracepoint_probe_unregister(call->name,
181 call->class->perf_probe,
182 call);
183 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100184 case TRACE_REG_PERF_OPEN:
185 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100186 case TRACE_REG_PERF_ADD:
187 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100188 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400189#endif
190 }
191 return 0;
192}
193EXPORT_SYMBOL_GPL(ftrace_event_reg);
194
Li Zefane870e9a2010-07-02 11:07:32 +0800195void trace_event_enable_cmd_record(bool enable)
196{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400197 struct ftrace_event_file *file;
198 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800199
200 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400201 do_for_each_event_file(tr, file) {
202
203 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800204 continue;
205
206 if (enable) {
207 tracing_start_cmdline_record();
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400208 file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
Li Zefane870e9a2010-07-02 11:07:32 +0800209 } else {
210 tracing_stop_cmdline_record();
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400211 file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
Li Zefane870e9a2010-07-02 11:07:32 +0800212 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400213 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800214 mutex_unlock(&event_mutex);
215}
216
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400217static int ftrace_event_enable_disable(struct ftrace_event_file *file,
218 int enable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500219{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400220 struct ftrace_event_call *call = file->event_call;
Li Zefan3b8e4272009-12-08 11:14:52 +0800221 int ret = 0;
222
Steven Rostedtfd994982009-02-28 02:41:25 -0500223 switch (enable) {
224 case 0:
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400225 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
226 file->flags &= ~FTRACE_EVENT_FL_ENABLED;
227 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800228 tracing_stop_cmdline_record();
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400229 file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
Li Zefane870e9a2010-07-02 11:07:32 +0800230 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400231 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500232 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500233 break;
234 case 1:
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400235 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
Li Zefane870e9a2010-07-02 11:07:32 +0800236 if (trace_flags & TRACE_ITER_RECORD_CMD) {
237 tracing_start_cmdline_record();
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400238 file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
Li Zefane870e9a2010-07-02 11:07:32 +0800239 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400240 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800241 if (ret) {
242 tracing_stop_cmdline_record();
243 pr_info("event trace: Could not enable event "
244 "%s\n", call->name);
245 break;
246 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400247 file->flags |= FTRACE_EVENT_FL_ENABLED;
Steven Rostedtfd994982009-02-28 02:41:25 -0500248 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500249 break;
250 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800251
252 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500253}
254
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400255static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800256{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400257 struct ftrace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800258
259 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400260 list_for_each_entry(file, &tr->events, list) {
261 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800262 }
263 mutex_unlock(&event_mutex);
264}
265
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400266static void __put_system(struct event_subsystem *system)
267{
268 struct event_filter *filter = system->filter;
269
270 WARN_ON_ONCE(system->ref_count == 0);
271 if (--system->ref_count)
272 return;
273
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400274 list_del(&system->list);
275
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400276 if (filter) {
277 kfree(filter->filter_string);
278 kfree(filter);
279 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400280 kfree(system);
281}
282
283static void __get_system(struct event_subsystem *system)
284{
285 WARN_ON_ONCE(system->ref_count == 0);
286 system->ref_count++;
287}
288
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400289static void __get_system_dir(struct ftrace_subsystem_dir *dir)
290{
291 WARN_ON_ONCE(dir->ref_count == 0);
292 dir->ref_count++;
293 __get_system(dir->subsystem);
294}
295
296static void __put_system_dir(struct ftrace_subsystem_dir *dir)
297{
298 WARN_ON_ONCE(dir->ref_count == 0);
299 /* If the subsystem is about to be freed, the dir must be too */
300 WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
301
302 __put_system(dir->subsystem);
303 if (!--dir->ref_count)
304 kfree(dir);
305}
306
307static void put_system(struct ftrace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400308{
309 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400310 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400311 mutex_unlock(&event_mutex);
312}
313
Li Zefan8f31bfe2009-05-08 10:31:42 +0800314/*
315 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
316 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400317static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
318 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500319{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400320 struct ftrace_event_file *file;
Steven Rostedta59fd602009-04-10 13:52:20 -0400321 struct ftrace_event_call *call;
Steven Rostedt29f93942009-05-08 16:06:47 -0400322 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500323
Steven Rostedt11a241a2009-03-02 11:49:04 -0500324 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400325 list_for_each_entry(file, &tr->events, list) {
326
327 call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500328
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400329 if (!call->name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500330 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500331
Steven Rostedt9b637762012-05-10 15:55:43 -0400332 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
333 continue;
334
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500335 if (match &&
336 strcmp(match, call->name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400337 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500338 continue;
339
Steven Rostedt8f082012010-04-20 10:47:33 -0400340 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500341 continue;
342
343 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500344 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500345
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400346 ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500347
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500348 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500349 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500350 mutex_unlock(&event_mutex);
351
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500352 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500353}
354
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400355static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800356{
357 char *event = NULL, *sub = NULL, *match;
358
359 /*
360 * The buf format can be <subsystem>:<event-name>
361 * *:<event-name> means any event by that name.
362 * :<event-name> is the same.
363 *
364 * <subsystem>:* means all events in that subsystem
365 * <subsystem>: means the same.
366 *
367 * <name> (no ':') means all events in a subsystem with
368 * the name <name> or any event that matches <name>
369 */
370
371 match = strsep(&buf, ":");
372 if (buf) {
373 sub = match;
374 event = buf;
375 match = NULL;
376
377 if (!strlen(sub) || strcmp(sub, "*") == 0)
378 sub = NULL;
379 if (!strlen(event) || strcmp(event, "*") == 0)
380 event = NULL;
381 }
382
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400383 return __ftrace_set_clr_event(tr, match, sub, event, set);
Li Zefan8f31bfe2009-05-08 10:31:42 +0800384}
385
Steven Rostedt4671c792009-05-08 16:27:41 -0400386/**
387 * trace_set_clr_event - enable or disable an event
388 * @system: system name to match (NULL for any system)
389 * @event: event name to match (NULL for all events, within system)
390 * @set: 1 to enable, 0 to disable
391 *
392 * This is a way for other parts of the kernel to enable or disable
393 * event recording.
394 *
395 * Returns 0 on success, -EINVAL if the parameters do not match any
396 * registered events.
397 */
398int trace_set_clr_event(const char *system, const char *event, int set)
399{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400400 struct trace_array *tr = top_trace_array();
401
402 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400403}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800404EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400405
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500406/* 128 should be much more than enough */
407#define EVENT_BUF_SIZE 127
408
409static ssize_t
410ftrace_event_write(struct file *file, const char __user *ubuf,
411 size_t cnt, loff_t *ppos)
412{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200413 struct trace_parser parser;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400414 struct seq_file *m = file->private_data;
415 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800416 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500417
Li Zefan4ba79782009-09-22 13:52:20 +0800418 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500419 return 0;
420
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400421 ret = tracing_update_buffers();
422 if (ret < 0)
423 return ret;
424
jolsa@redhat.com48966362009-09-11 17:29:28 +0200425 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500426 return -ENOMEM;
427
jolsa@redhat.com48966362009-09-11 17:29:28 +0200428 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500429
Li Zefan4ba79782009-09-22 13:52:20 +0800430 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200431 int set = 1;
432
433 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500434 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500435
jolsa@redhat.com48966362009-09-11 17:29:28 +0200436 parser.buffer[parser.idx] = 0;
437
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400438 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500439 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200440 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500441 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500442
443 ret = read;
444
jolsa@redhat.com48966362009-09-11 17:29:28 +0200445 out_put:
446 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500447
448 return ret;
449}
450
451static void *
452t_next(struct seq_file *m, void *v, loff_t *pos)
453{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400454 struct ftrace_event_file *file = v;
455 struct ftrace_event_call *call;
456 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500457
458 (*pos)++;
459
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400460 list_for_each_entry_continue(file, &tr->events, list) {
461 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400462 /*
463 * The ftrace subsystem is for showing formats only.
464 * They can not be enabled or disabled via the event files.
465 */
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400466 if (call->class && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400467 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400468 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500469
Li Zefan30bd39c2009-09-18 14:07:05 +0800470 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500471}
472
473static void *t_start(struct seq_file *m, loff_t *pos)
474{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400475 struct ftrace_event_file *file;
476 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800477 loff_t l;
478
Li Zefan20c89282009-05-06 10:33:45 +0800479 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800480
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400481 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800482 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400483 file = t_next(m, file, &l);
484 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800485 break;
486 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400487 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500488}
489
490static void *
491s_next(struct seq_file *m, void *v, loff_t *pos)
492{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400493 struct ftrace_event_file *file = v;
494 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500495
496 (*pos)++;
497
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400498 list_for_each_entry_continue(file, &tr->events, list) {
499 if (file->flags & FTRACE_EVENT_FL_ENABLED)
500 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500501 }
502
Li Zefan30bd39c2009-09-18 14:07:05 +0800503 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500504}
505
506static void *s_start(struct seq_file *m, loff_t *pos)
507{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400508 struct ftrace_event_file *file;
509 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800510 loff_t l;
511
Li Zefan20c89282009-05-06 10:33:45 +0800512 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800513
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400514 file = list_entry(&tr->events, struct ftrace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800515 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400516 file = s_next(m, file, &l);
517 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800518 break;
519 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400520 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500521}
522
523static int t_show(struct seq_file *m, void *v)
524{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400525 struct ftrace_event_file *file = v;
526 struct ftrace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500527
Steven Rostedt8f082012010-04-20 10:47:33 -0400528 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
529 seq_printf(m, "%s:", call->class->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500530 seq_printf(m, "%s\n", call->name);
531
532 return 0;
533}
534
535static void t_stop(struct seq_file *m, void *p)
536{
Li Zefan20c89282009-05-06 10:33:45 +0800537 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500538}
539
Steven Rostedt1473e442009-02-24 14:15:08 -0500540static ssize_t
541event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
542 loff_t *ppos)
543{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400544 struct ftrace_event_file *file = filp->private_data;
Steven Rostedt1473e442009-02-24 14:15:08 -0500545 char *buf;
546
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400547 if (file->flags & FTRACE_EVENT_FL_ENABLED)
Steven Rostedt1473e442009-02-24 14:15:08 -0500548 buf = "1\n";
549 else
550 buf = "0\n";
551
552 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
553}
554
555static ssize_t
556event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
557 loff_t *ppos)
558{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400559 struct ftrace_event_file *file = filp->private_data;
Steven Rostedt1473e442009-02-24 14:15:08 -0500560 unsigned long val;
561 int ret;
562
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400563 if (!file)
564 return -EINVAL;
565
Peter Huewe22fe9b52011-06-07 21:58:27 +0200566 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
567 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -0500568 return ret;
569
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400570 ret = tracing_update_buffers();
571 if (ret < 0)
572 return ret;
573
Steven Rostedt1473e442009-02-24 14:15:08 -0500574 switch (val) {
575 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500576 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500577 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400578 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500579 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500580 break;
581
582 default:
583 return -EINVAL;
584 }
585
586 *ppos += cnt;
587
Li Zefan3b8e4272009-12-08 11:14:52 +0800588 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500589}
590
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400591static ssize_t
592system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
593 loff_t *ppos)
594{
Li Zefanc142b152009-05-08 10:32:05 +0800595 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400596 struct ftrace_subsystem_dir *dir = filp->private_data;
597 struct event_subsystem *system = dir->subsystem;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400598 struct ftrace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400599 struct ftrace_event_file *file;
600 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400601 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800602 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400603 int ret;
604
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400605 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400606 list_for_each_entry(file, &tr->events, list) {
607 call = file->event_call;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400608 if (!call->name || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400609 continue;
610
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400611 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400612 continue;
613
614 /*
615 * We need to find out if all the events are set
616 * or if all events or cleared, or if we have
617 * a mixture.
618 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400619 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +0800620
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400621 /*
622 * If we have a mixture, no need to look further.
623 */
Li Zefanc142b152009-05-08 10:32:05 +0800624 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400625 break;
626 }
627 mutex_unlock(&event_mutex);
628
Li Zefanc142b152009-05-08 10:32:05 +0800629 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400630 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400631
632 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
633
634 return ret;
635}
636
637static ssize_t
638system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
639 loff_t *ppos)
640{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400641 struct ftrace_subsystem_dir *dir = filp->private_data;
642 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400643 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400644 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400645 ssize_t ret;
646
Peter Huewe22fe9b52011-06-07 21:58:27 +0200647 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
648 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400649 return ret;
650
651 ret = tracing_update_buffers();
652 if (ret < 0)
653 return ret;
654
Li Zefan8f31bfe2009-05-08 10:31:42 +0800655 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400656 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400657
Steven Rostedt40ee4df2011-07-05 14:32:51 -0400658 /*
659 * Opening of "enable" adds a ref count to system,
660 * so the name is safe to use.
661 */
662 if (system)
663 name = system->name;
664
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400665 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400666 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800667 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400668
669 ret = cnt;
670
Li Zefan8f31bfe2009-05-08 10:31:42 +0800671out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400672 *ppos += cnt;
673
674 return ret;
675}
676
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400677enum {
678 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +0800679 FORMAT_FIELD_SEPERATOR = 2,
680 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400681};
682
683static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -0500684{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400685 struct ftrace_event_call *call = m->private;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800686 struct ftrace_event_field *field;
Li Zefan86397dc2010-08-17 13:53:06 +0800687 struct list_head *common_head = &ftrace_common_fields;
688 struct list_head *head = trace_get_fields(call);
Steven Rostedt981d0812009-03-02 13:53:59 -0500689
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400690 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800691
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400692 switch ((unsigned long)v) {
693 case FORMAT_HEADER:
Li Zefan86397dc2010-08-17 13:53:06 +0800694 if (unlikely(list_empty(common_head)))
695 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800696
Li Zefan86397dc2010-08-17 13:53:06 +0800697 field = list_entry(common_head->prev,
698 struct ftrace_event_field, link);
699 return field;
700
701 case FORMAT_FIELD_SEPERATOR:
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400702 if (unlikely(list_empty(head)))
703 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800704
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400705 field = list_entry(head->prev, struct ftrace_event_field, link);
706 return field;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800707
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400708 case FORMAT_PRINTFMT:
709 /* all done */
710 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800711 }
712
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400713 field = v;
Li Zefan86397dc2010-08-17 13:53:06 +0800714 if (field->link.prev == common_head)
715 return (void *)FORMAT_FIELD_SEPERATOR;
716 else if (field->link.prev == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400717 return (void *)FORMAT_PRINTFMT;
718
719 field = list_entry(field->link.prev, struct ftrace_event_field, link);
720
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400721 return field;
Li Zefan8728fe52010-05-24 16:22:49 +0800722}
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800723
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400724static void *f_start(struct seq_file *m, loff_t *pos)
Li Zefan8728fe52010-05-24 16:22:49 +0800725{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400726 loff_t l = 0;
727 void *p;
Li Zefan8728fe52010-05-24 16:22:49 +0800728
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400729 /* Start by showing the header */
730 if (!*pos)
731 return (void *)FORMAT_HEADER;
732
733 p = (void *)FORMAT_HEADER;
734 do {
735 p = f_next(m, p, &l);
736 } while (p && l < *pos);
737
738 return p;
739}
740
741static int f_show(struct seq_file *m, void *v)
742{
743 struct ftrace_event_call *call = m->private;
744 struct ftrace_event_field *field;
745 const char *array_descriptor;
746
747 switch ((unsigned long)v) {
748 case FORMAT_HEADER:
749 seq_printf(m, "name: %s\n", call->name);
750 seq_printf(m, "ID: %d\n", call->event.type);
751 seq_printf(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +0800752 return 0;
753
Li Zefan86397dc2010-08-17 13:53:06 +0800754 case FORMAT_FIELD_SEPERATOR:
755 seq_putc(m, '\n');
756 return 0;
757
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400758 case FORMAT_PRINTFMT:
759 seq_printf(m, "\nprint fmt: %s\n",
760 call->print_fmt);
761 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500762 }
763
Steven Rostedt2a37a3d2010-06-03 15:21:34 -0400764 field = v;
765
766 /*
767 * Smartly shows the array type(except dynamic array).
768 * Normal:
769 * field:TYPE VAR
770 * If TYPE := TYPE[LEN], it is shown:
771 * field:TYPE VAR[LEN]
772 */
773 array_descriptor = strchr(field->type, '[');
774
775 if (!strncmp(field->type, "__data_loc", 10))
776 array_descriptor = NULL;
777
778 if (!array_descriptor)
779 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
780 field->type, field->name, field->offset,
781 field->size, !!field->is_signed);
782 else
783 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
784 (int)(array_descriptor - field->type),
785 field->type, field->name,
786 array_descriptor, field->offset,
787 field->size, !!field->is_signed);
788
789 return 0;
790}
791
792static void f_stop(struct seq_file *m, void *p)
793{
794}
795
796static const struct seq_operations trace_format_seq_ops = {
797 .start = f_start,
798 .next = f_next,
799 .stop = f_stop,
800 .show = f_show,
801};
802
803static int trace_format_open(struct inode *inode, struct file *file)
804{
805 struct ftrace_event_call *call = inode->i_private;
806 struct seq_file *m;
807 int ret;
808
809 ret = seq_open(file, &trace_format_seq_ops);
810 if (ret < 0)
811 return ret;
812
813 m = file->private_data;
814 m->private = call;
815
816 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500817}
818
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100819static ssize_t
820event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
821{
822 struct ftrace_event_call *call = filp->private_data;
823 struct trace_seq *s;
824 int r;
825
826 if (*ppos)
827 return 0;
828
829 s = kmalloc(sizeof(*s), GFP_KERNEL);
830 if (!s)
831 return -ENOMEM;
832
833 trace_seq_init(s);
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400834 trace_seq_printf(s, "%d\n", call->event.type);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100835
836 r = simple_read_from_buffer(ubuf, cnt, ppos,
837 s->buffer, s->len);
838 kfree(s);
839 return r;
840}
841
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500842static ssize_t
843event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
844 loff_t *ppos)
845{
846 struct ftrace_event_call *call = filp->private_data;
847 struct trace_seq *s;
848 int r;
849
850 if (*ppos)
851 return 0;
852
853 s = kmalloc(sizeof(*s), GFP_KERNEL);
854 if (!s)
855 return -ENOMEM;
856
857 trace_seq_init(s);
858
Tom Zanussi8b372562009-04-28 03:04:59 -0500859 print_event_filter(call, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500860 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500861
862 kfree(s);
863
864 return r;
865}
866
867static ssize_t
868event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
869 loff_t *ppos)
870{
871 struct ftrace_event_call *call = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500872 char *buf;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500873 int err;
874
Tom Zanussi8b372562009-04-28 03:04:59 -0500875 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500876 return -EINVAL;
877
Tom Zanussi8b372562009-04-28 03:04:59 -0500878 buf = (char *)__get_free_page(GFP_TEMPORARY);
879 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500880 return -ENOMEM;
881
Tom Zanussi8b372562009-04-28 03:04:59 -0500882 if (copy_from_user(buf, ubuf, cnt)) {
883 free_page((unsigned long) buf);
884 return -EFAULT;
885 }
886 buf[cnt] = '\0';
887
888 err = apply_event_filter(call, buf);
889 free_page((unsigned long) buf);
890 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500891 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500892
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500893 *ppos += cnt;
894
895 return cnt;
896}
897
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400898static LIST_HEAD(event_subsystems);
899
900static int subsystem_open(struct inode *inode, struct file *filp)
901{
902 struct event_subsystem *system = NULL;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400903 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
904 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400905 int ret;
906
907 /* Make sure the system still exists */
908 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400909 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
910 list_for_each_entry(dir, &tr->systems, list) {
911 if (dir == inode->i_private) {
912 /* Don't open systems with no events */
913 if (dir->nr_events) {
914 __get_system_dir(dir);
915 system = dir->subsystem;
916 }
917 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400918 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400919 }
920 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400921 exit_loop:
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400922 mutex_unlock(&event_mutex);
923
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400924 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400925 return -ENODEV;
926
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400927 /* Some versions of gcc think dir can be uninitialized here */
928 WARN_ON(!dir);
929
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400930 ret = tracing_open_generic(inode, filp);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400931 if (ret < 0)
932 put_system(dir);
933
934 return ret;
935}
936
937static int system_tr_open(struct inode *inode, struct file *filp)
938{
939 struct ftrace_subsystem_dir *dir;
940 struct trace_array *tr = inode->i_private;
941 int ret;
942
943 /* Make a temporary dir that has no system but points to tr */
944 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
945 if (!dir)
946 return -ENOMEM;
947
948 dir->tr = tr;
949
950 ret = tracing_open_generic(inode, filp);
951 if (ret < 0)
952 kfree(dir);
953
954 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400955
956 return ret;
957}
958
959static int subsystem_release(struct inode *inode, struct file *file)
960{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400961 struct ftrace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400962
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400963 /*
964 * If dir->subsystem is NULL, then this is a temporary
965 * descriptor that was made for a trace_array to enable
966 * all subsystems.
967 */
968 if (dir->subsystem)
969 put_system(dir);
970 else
971 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400972
973 return 0;
974}
975
Tom Zanussicfb180f2009-03-22 03:31:17 -0500976static ssize_t
977subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
978 loff_t *ppos)
979{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400980 struct ftrace_subsystem_dir *dir = filp->private_data;
981 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500982 struct trace_seq *s;
983 int r;
984
985 if (*ppos)
986 return 0;
987
988 s = kmalloc(sizeof(*s), GFP_KERNEL);
989 if (!s)
990 return -ENOMEM;
991
992 trace_seq_init(s);
993
Tom Zanussi8b372562009-04-28 03:04:59 -0500994 print_subsystem_event_filter(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500995 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500996
997 kfree(s);
998
999 return r;
1000}
1001
1002static ssize_t
1003subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1004 loff_t *ppos)
1005{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001006 struct ftrace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001007 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001008 int err;
1009
Tom Zanussi8b372562009-04-28 03:04:59 -05001010 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001011 return -EINVAL;
1012
Tom Zanussi8b372562009-04-28 03:04:59 -05001013 buf = (char *)__get_free_page(GFP_TEMPORARY);
1014 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001015 return -ENOMEM;
1016
Tom Zanussi8b372562009-04-28 03:04:59 -05001017 if (copy_from_user(buf, ubuf, cnt)) {
1018 free_page((unsigned long) buf);
1019 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001020 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001021 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -05001022
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001023 err = apply_subsystem_event_filter(dir, buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001024 free_page((unsigned long) buf);
1025 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001026 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001027
1028 *ppos += cnt;
1029
1030 return cnt;
1031}
1032
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001033static ssize_t
1034show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1035{
1036 int (*func)(struct trace_seq *s) = filp->private_data;
1037 struct trace_seq *s;
1038 int r;
1039
1040 if (*ppos)
1041 return 0;
1042
1043 s = kmalloc(sizeof(*s), GFP_KERNEL);
1044 if (!s)
1045 return -ENOMEM;
1046
1047 trace_seq_init(s);
1048
1049 func(s);
1050 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1051
1052 kfree(s);
1053
1054 return r;
1055}
1056
Steven Rostedt15075ca2012-05-03 14:57:28 -04001057static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1058static int ftrace_event_set_open(struct inode *inode, struct file *file);
1059
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001060static const struct seq_operations show_event_seq_ops = {
1061 .start = t_start,
1062 .next = t_next,
1063 .show = t_show,
1064 .stop = t_stop,
1065};
1066
1067static const struct seq_operations show_set_event_seq_ops = {
1068 .start = s_start,
1069 .next = s_next,
1070 .show = t_show,
1071 .stop = t_stop,
1072};
1073
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001074static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001075 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001076 .read = seq_read,
1077 .llseek = seq_lseek,
1078 .release = seq_release,
1079};
1080
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001081static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001082 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001083 .read = seq_read,
1084 .write = ftrace_event_write,
1085 .llseek = seq_lseek,
1086 .release = seq_release,
1087};
1088
Steven Rostedt1473e442009-02-24 14:15:08 -05001089static const struct file_operations ftrace_enable_fops = {
1090 .open = tracing_open_generic,
1091 .read = event_enable_read,
1092 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001093 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001094};
1095
Steven Rostedt981d0812009-03-02 13:53:59 -05001096static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001097 .open = trace_format_open,
1098 .read = seq_read,
1099 .llseek = seq_lseek,
1100 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001101};
1102
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001103static const struct file_operations ftrace_event_id_fops = {
1104 .open = tracing_open_generic,
1105 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001106 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001107};
1108
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001109static const struct file_operations ftrace_event_filter_fops = {
1110 .open = tracing_open_generic,
1111 .read = event_filter_read,
1112 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001113 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001114};
1115
Tom Zanussicfb180f2009-03-22 03:31:17 -05001116static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001117 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001118 .read = subsystem_filter_read,
1119 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001120 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001121 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001122};
1123
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001124static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001125 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001126 .read = system_enable_read,
1127 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001128 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001129 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001130};
1131
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001132static const struct file_operations ftrace_tr_enable_fops = {
1133 .open = system_tr_open,
1134 .read = system_enable_read,
1135 .write = system_enable_write,
1136 .llseek = default_llseek,
1137 .release = subsystem_release,
1138};
1139
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001140static const struct file_operations ftrace_show_header_fops = {
1141 .open = tracing_open_generic,
1142 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001143 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001144};
1145
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001146static int
1147ftrace_event_open(struct inode *inode, struct file *file,
1148 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001149{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001150 struct seq_file *m;
1151 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001152
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001153 ret = seq_open(file, seq_ops);
1154 if (ret < 0)
1155 return ret;
1156 m = file->private_data;
1157 /* copy tr over to seq ops */
1158 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001159
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001160 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001161}
1162
Steven Rostedt15075ca2012-05-03 14:57:28 -04001163static int
1164ftrace_event_avail_open(struct inode *inode, struct file *file)
1165{
1166 const struct seq_operations *seq_ops = &show_event_seq_ops;
1167
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001168 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001169}
1170
1171static int
1172ftrace_event_set_open(struct inode *inode, struct file *file)
1173{
1174 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001175 struct trace_array *tr = inode->i_private;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001176
1177 if ((file->f_mode & FMODE_WRITE) &&
1178 (file->f_flags & O_TRUNC))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001179 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001180
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001181 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001182}
1183
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001184static struct event_subsystem *
1185create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001186{
1187 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001188
1189 /* need to create new entry */
1190 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001191 if (!system)
1192 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001193
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001194 system->ref_count = 1;
Steven Rostedt92edca02013-02-27 20:41:37 -05001195 system->name = name;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001196
Tom Zanussi30e673b2009-04-28 03:04:47 -05001197 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001198
Tom Zanussi8b372562009-04-28 03:04:59 -05001199 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001200 if (!system->filter)
1201 goto out_free;
1202
1203 list_add(&system->list, &event_subsystems);
1204
1205 return system;
1206
1207 out_free:
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001208 kfree(system);
1209 return NULL;
1210}
1211
1212static struct dentry *
1213event_subsystem_dir(struct trace_array *tr, const char *name,
1214 struct ftrace_event_file *file, struct dentry *parent)
1215{
1216 struct ftrace_subsystem_dir *dir;
1217 struct event_subsystem *system;
1218 struct dentry *entry;
1219
1220 /* First see if we did not already create this dir */
1221 list_for_each_entry(dir, &tr->systems, list) {
1222 system = dir->subsystem;
1223 if (strcmp(system->name, name) == 0) {
1224 dir->nr_events++;
1225 file->system = dir;
1226 return dir->entry;
1227 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001228 }
1229
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001230 /* Now see if the system itself exists. */
1231 list_for_each_entry(system, &event_subsystems, list) {
1232 if (strcmp(system->name, name) == 0)
1233 break;
1234 }
1235 /* Reset system variable when not found */
1236 if (&system->list == &event_subsystems)
1237 system = NULL;
1238
1239 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1240 if (!dir)
1241 goto out_fail;
1242
1243 if (!system) {
1244 system = create_new_subsystem(name);
1245 if (!system)
1246 goto out_free;
1247 } else
1248 __get_system(system);
1249
1250 dir->entry = debugfs_create_dir(name, parent);
1251 if (!dir->entry) {
1252 pr_warning("Failed to create system directory %s\n", name);
1253 __put_system(system);
1254 goto out_free;
1255 }
1256
1257 dir->tr = tr;
1258 dir->ref_count = 1;
1259 dir->nr_events = 1;
1260 dir->subsystem = system;
1261 file->system = dir;
1262
1263 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001264 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001265 if (!entry) {
1266 kfree(system->filter);
1267 system->filter = NULL;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001268 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001269 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001270
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001271 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001272 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001273
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001274 list_add(&dir->list, &tr->systems);
1275
1276 return dir->entry;
1277
1278 out_free:
1279 kfree(dir);
1280 out_fail:
1281 /* Only print this message if failed on memory allocation */
1282 if (!dir || !system)
1283 pr_warning("No memory to create event subsystem %s\n",
1284 name);
1285 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001286}
1287
Steven Rostedt1473e442009-02-24 14:15:08 -05001288static int
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001289event_create_dir(struct dentry *parent,
1290 struct ftrace_event_file *file,
Steven Rostedt701970b2009-04-24 23:11:22 -04001291 const struct file_operations *id,
1292 const struct file_operations *enable,
1293 const struct file_operations *filter,
1294 const struct file_operations *format)
Steven Rostedt1473e442009-02-24 14:15:08 -05001295{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001296 struct ftrace_event_call *call = file->event_call;
1297 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001298 struct list_head *head;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001299 struct dentry *d_events;
Steven Rostedtfd994982009-02-28 02:41:25 -05001300 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001301
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001302 /*
1303 * If the trace point header did not define TRACE_SYSTEM
1304 * then the system would be called "TRACE_SYSTEM".
1305 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001306 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1307 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1308 if (!d_events)
1309 return -ENOMEM;
1310 } else
1311 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001312
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001313 file->dir = debugfs_create_dir(call->name, d_events);
1314 if (!file->dir) {
1315 pr_warning("Could not create debugfs '%s' directory\n",
1316 call->name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001317 return -1;
1318 }
1319
Steven Rostedt9b637762012-05-10 15:55:43 -04001320 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001321 trace_create_file("enable", 0644, file->dir, file,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001322 enable);
Steven Rostedt1473e442009-02-24 14:15:08 -05001323
Steven Rostedt22392912010-04-21 12:27:06 -04001324#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001325 if (call->event.type && call->class->reg)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001326 trace_create_file("id", 0444, file->dir, call,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001327 id);
Steven Rostedt22392912010-04-21 12:27:06 -04001328#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001329
Li Zefanc9d932c2010-05-24 16:24:28 +08001330 /*
1331 * Other events may have the same class. Only update
1332 * the fields if they are not already defined.
1333 */
1334 head = trace_get_fields(call);
1335 if (list_empty(head)) {
1336 ret = call->class->define_fields(call);
1337 if (ret < 0) {
1338 pr_warning("Could not initialize trace point"
1339 " events/%s\n", call->name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001340 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001341 }
1342 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001343 trace_create_file("filter", 0644, file->dir, call,
Li Zefanc9d932c2010-05-24 16:24:28 +08001344 filter);
Tom Zanussicf027f62009-03-22 03:30:39 -05001345
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001346 trace_create_file("format", 0444, file->dir, call,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001347 format);
Steven Rostedtfd994982009-02-28 02:41:25 -05001348
Steven Rostedt1473e442009-02-24 14:15:08 -05001349 return 0;
1350}
1351
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001352static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1353{
1354 if (!dir)
1355 return;
1356
1357 if (!--dir->nr_events) {
1358 debugfs_remove_recursive(dir->entry);
1359 list_del(&dir->list);
1360 __put_system_dir(dir);
1361 }
1362}
1363
1364static void remove_event_from_tracers(struct ftrace_event_call *call)
1365{
1366 struct ftrace_event_file *file;
1367 struct trace_array *tr;
1368
1369 do_for_each_event_file_safe(tr, file) {
1370
1371 if (file->event_call != call)
1372 continue;
1373
1374 list_del(&file->list);
1375 debugfs_remove_recursive(file->dir);
1376 remove_subsystem(file->system);
Steven Rostedtd1a29142013-02-27 20:23:57 -05001377 kmem_cache_free(file_cachep, file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001378
1379 /*
1380 * The do_for_each_event_file_safe() is
1381 * a double loop. After finding the call for this
1382 * trace_array, we use break to jump to the next
1383 * trace_array.
1384 */
1385 break;
1386 } while_for_each_event_file();
1387}
1388
Ezequiel Garcia87819152012-09-12 11:47:57 -03001389static void event_remove(struct ftrace_event_call *call)
1390{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001391 struct trace_array *tr;
1392 struct ftrace_event_file *file;
1393
1394 do_for_each_event_file(tr, file) {
1395 if (file->event_call != call)
1396 continue;
1397 ftrace_event_enable_disable(file, 0);
1398 /*
1399 * The do_for_each_event_file() is
1400 * a double loop. After finding the call for this
1401 * trace_array, we use break to jump to the next
1402 * trace_array.
1403 */
1404 break;
1405 } while_for_each_event_file();
1406
Ezequiel Garcia87819152012-09-12 11:47:57 -03001407 if (call->event.funcs)
1408 __unregister_ftrace_event(&call->event);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001409 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001410 list_del(&call->list);
1411}
1412
1413static int event_init(struct ftrace_event_call *call)
1414{
1415 int ret = 0;
1416
1417 if (WARN_ON(!call->name))
1418 return -EINVAL;
1419
1420 if (call->class->raw_init) {
1421 ret = call->class->raw_init(call);
1422 if (ret < 0 && ret != -ENOSYS)
1423 pr_warn("Could not initialize trace events/%s\n",
1424 call->name);
1425 }
1426
1427 return ret;
1428}
1429
Li Zefan67ead0a2010-05-24 16:25:13 +08001430static int
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001431__register_event(struct ftrace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001432{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001433 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001434
Ezequiel Garcia87819152012-09-12 11:47:57 -03001435 ret = event_init(call);
1436 if (ret < 0)
1437 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04001438
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001439 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08001440 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001441
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001442 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001443}
1444
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001445/* Add an event to a trace directory */
1446static int
1447__trace_add_new_event(struct ftrace_event_call *call,
1448 struct trace_array *tr,
1449 const struct file_operations *id,
1450 const struct file_operations *enable,
1451 const struct file_operations *filter,
1452 const struct file_operations *format)
1453{
1454 struct ftrace_event_file *file;
1455
Steven Rostedtd1a29142013-02-27 20:23:57 -05001456 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001457 if (!file)
1458 return -ENOMEM;
1459
1460 file->event_call = call;
1461 file->tr = tr;
1462 list_add(&file->list, &tr->events);
1463
1464 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1465}
1466
Steven Rostedt77248222013-02-27 16:28:06 -05001467/*
1468 * Just create a decriptor for early init. A descriptor is required
1469 * for enabling events at boot. We want to enable events before
1470 * the filesystem is initialized.
1471 */
1472static __init int
1473__trace_early_add_new_event(struct ftrace_event_call *call,
1474 struct trace_array *tr)
1475{
1476 struct ftrace_event_file *file;
1477
Steven Rostedtd1a29142013-02-27 20:23:57 -05001478 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
Steven Rostedt77248222013-02-27 16:28:06 -05001479 if (!file)
1480 return -ENOMEM;
1481
1482 file->event_call = call;
1483 file->tr = tr;
1484 list_add(&file->list, &tr->events);
1485
1486 return 0;
1487}
1488
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001489struct ftrace_module_file_ops;
1490static void __add_event_to_tracers(struct ftrace_event_call *call,
1491 struct ftrace_module_file_ops *file_ops);
1492
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001493/* Add an additional event_call dynamically */
1494int trace_add_event_call(struct ftrace_event_call *call)
1495{
1496 int ret;
1497 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001498
1499 ret = __register_event(call, NULL);
1500 if (ret >= 0)
1501 __add_event_to_tracers(call, NULL);
1502
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001503 mutex_unlock(&event_mutex);
1504 return ret;
1505}
Steven Rostedt701970b2009-04-24 23:11:22 -04001506
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001507/*
1508 * Must be called under locking both of event_mutex and trace_event_mutex.
1509 */
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001510static void __trace_remove_event_call(struct ftrace_event_call *call)
1511{
Ezequiel Garcia87819152012-09-12 11:47:57 -03001512 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001513 trace_destroy_fields(call);
1514 destroy_preds(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001515}
1516
1517/* Remove an event_call */
1518void trace_remove_event_call(struct ftrace_event_call *call)
1519{
1520 mutex_lock(&event_mutex);
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001521 down_write(&trace_event_mutex);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001522 __trace_remove_event_call(call);
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001523 up_write(&trace_event_mutex);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001524 mutex_unlock(&event_mutex);
1525}
1526
1527#define for_each_event(event, start, end) \
1528 for (event = start; \
1529 (unsigned long)event < (unsigned long)end; \
1530 event++)
1531
1532#ifdef CONFIG_MODULES
1533
1534static LIST_HEAD(ftrace_module_file_list);
1535
1536/*
1537 * Modules must own their file_operations to keep up with
1538 * reference counting.
1539 */
1540struct ftrace_module_file_ops {
1541 struct list_head list;
1542 struct module *mod;
1543 struct file_operations id;
1544 struct file_operations enable;
1545 struct file_operations format;
1546 struct file_operations filter;
1547};
1548
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001549static struct ftrace_module_file_ops *
1550find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001551{
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001552 /*
1553 * As event_calls are added in groups by module,
1554 * when we find one file_ops, we don't need to search for
1555 * each call in that module, as the rest should be the
1556 * same. Only search for a new one if the last one did
1557 * not match.
1558 */
1559 if (file_ops && mod == file_ops->mod)
1560 return file_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001561
1562 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1563 if (file_ops->mod == mod)
1564 return file_ops;
1565 }
1566 return NULL;
1567}
1568
Steven Rostedt701970b2009-04-24 23:11:22 -04001569static struct ftrace_module_file_ops *
1570trace_create_file_ops(struct module *mod)
1571{
1572 struct ftrace_module_file_ops *file_ops;
1573
1574 /*
1575 * This is a bit of a PITA. To allow for correct reference
1576 * counting, modules must "own" their file_operations.
1577 * To do this, we allocate the file operations that will be
1578 * used in the event directory.
1579 */
1580
1581 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1582 if (!file_ops)
1583 return NULL;
1584
1585 file_ops->mod = mod;
1586
1587 file_ops->id = ftrace_event_id_fops;
1588 file_ops->id.owner = mod;
1589
1590 file_ops->enable = ftrace_enable_fops;
1591 file_ops->enable.owner = mod;
1592
1593 file_ops->filter = ftrace_event_filter_fops;
1594 file_ops->filter.owner = mod;
1595
1596 file_ops->format = ftrace_event_format_fops;
1597 file_ops->format.owner = mod;
1598
1599 list_add(&file_ops->list, &ftrace_module_file_list);
1600
1601 return file_ops;
1602}
1603
Steven Rostedt6d723732009-04-10 14:53:50 -04001604static void trace_module_add_events(struct module *mod)
1605{
Steven Rostedt701970b2009-04-24 23:11:22 -04001606 struct ftrace_module_file_ops *file_ops = NULL;
Steven Rostedte4a9ea52011-01-27 09:15:30 -05001607 struct ftrace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04001608
1609 start = mod->trace_events;
1610 end = mod->trace_events + mod->num_trace_events;
1611
1612 if (start == end)
1613 return;
1614
Li Zefan67ead0a2010-05-24 16:25:13 +08001615 file_ops = trace_create_file_ops(mod);
1616 if (!file_ops)
Steven Rostedt6d723732009-04-10 14:53:50 -04001617 return;
1618
1619 for_each_event(call, start, end) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001620 __register_event(*call, mod);
1621 __add_event_to_tracers(*call, file_ops);
Steven Rostedt6d723732009-04-10 14:53:50 -04001622 }
1623}
1624
1625static void trace_module_remove_events(struct module *mod)
1626{
Steven Rostedt701970b2009-04-24 23:11:22 -04001627 struct ftrace_module_file_ops *file_ops;
Steven Rostedt6d723732009-04-10 14:53:50 -04001628 struct ftrace_event_call *call, *p;
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001629 bool found = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001630
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001631 down_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001632 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1633 if (call->mod == mod) {
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001634 found = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001635 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001636 }
1637 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001638
1639 /* Now free the file_operations */
1640 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1641 if (file_ops->mod == mod)
1642 break;
1643 }
1644 if (&file_ops->list != &ftrace_module_file_list) {
1645 list_del(&file_ops->list);
1646 kfree(file_ops);
1647 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001648
1649 /*
1650 * It is safest to reset the ring buffer if the module being unloaded
1651 * registered any events.
1652 */
1653 if (found)
1654 tracing_reset_current_online_cpus();
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001655 up_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001656}
1657
Steven Rostedt61f919a2009-04-14 18:22:32 -04001658static int trace_module_notify(struct notifier_block *self,
1659 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001660{
1661 struct module *mod = data;
1662
1663 mutex_lock(&event_mutex);
1664 switch (val) {
1665 case MODULE_STATE_COMING:
1666 trace_module_add_events(mod);
1667 break;
1668 case MODULE_STATE_GOING:
1669 trace_module_remove_events(mod);
1670 break;
1671 }
1672 mutex_unlock(&event_mutex);
1673
1674 return 0;
1675}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001676
1677static int
1678__trace_add_new_mod_event(struct ftrace_event_call *call,
1679 struct trace_array *tr,
1680 struct ftrace_module_file_ops *file_ops)
1681{
1682 return __trace_add_new_event(call, tr,
1683 &file_ops->id, &file_ops->enable,
1684 &file_ops->filter, &file_ops->format);
1685}
1686
Steven Rostedt61f919a2009-04-14 18:22:32 -04001687#else
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001688static inline struct ftrace_module_file_ops *
1689find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001690{
1691 return NULL;
1692}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001693static inline int trace_module_notify(struct notifier_block *self,
1694 unsigned long val, void *data)
Steven Rostedt61f919a2009-04-14 18:22:32 -04001695{
1696 return 0;
1697}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001698static inline int
1699__trace_add_new_mod_event(struct ftrace_event_call *call,
1700 struct trace_array *tr,
1701 struct ftrace_module_file_ops *file_ops)
1702{
1703 return -ENODEV;
1704}
Steven Rostedt61f919a2009-04-14 18:22:32 -04001705#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001706
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001707/* Create a new event directory structure for a trace directory. */
1708static void
1709__trace_add_event_dirs(struct trace_array *tr)
1710{
1711 struct ftrace_module_file_ops *file_ops = NULL;
1712 struct ftrace_event_call *call;
1713 int ret;
1714
1715 list_for_each_entry(call, &ftrace_events, list) {
1716 if (call->mod) {
1717 /*
1718 * Directories for events by modules need to
1719 * keep module ref counts when opened (as we don't
1720 * want the module to disappear when reading one
1721 * of these files). The file_ops keep account of
1722 * the module ref count.
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001723 */
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001724 file_ops = find_ftrace_file_ops(file_ops, call->mod);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001725 if (!file_ops)
1726 continue; /* Warn? */
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001727 ret = __trace_add_new_mod_event(call, tr, file_ops);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001728 if (ret < 0)
1729 pr_warning("Could not create directory for event %s\n",
1730 call->name);
1731 continue;
1732 }
1733 ret = __trace_add_new_event(call, tr,
1734 &ftrace_event_id_fops,
1735 &ftrace_enable_fops,
1736 &ftrace_event_filter_fops,
1737 &ftrace_event_format_fops);
1738 if (ret < 0)
1739 pr_warning("Could not create directory for event %s\n",
1740 call->name);
1741 }
1742}
1743
Steven Rostedt77248222013-02-27 16:28:06 -05001744/*
1745 * The top level array has already had its ftrace_event_file
1746 * descriptors created in order to allow for early events to
1747 * be recorded. This function is called after the debugfs has been
1748 * initialized, and we now have to create the files associated
1749 * to the events.
1750 */
1751static __init void
1752__trace_early_add_event_dirs(struct trace_array *tr)
1753{
1754 struct ftrace_event_file *file;
1755 int ret;
1756
1757
1758 list_for_each_entry(file, &tr->events, list) {
1759 ret = event_create_dir(tr->event_dir, file,
1760 &ftrace_event_id_fops,
1761 &ftrace_enable_fops,
1762 &ftrace_event_filter_fops,
1763 &ftrace_event_format_fops);
1764 if (ret < 0)
1765 pr_warning("Could not create directory for event %s\n",
1766 file->event_call->name);
1767 }
1768}
1769
1770/*
1771 * For early boot up, the top trace array requires to have
1772 * a list of events that can be enabled. This must be done before
1773 * the filesystem is set up in order to allow events to be traced
1774 * early.
1775 */
1776static __init void
1777__trace_early_add_events(struct trace_array *tr)
1778{
1779 struct ftrace_event_call *call;
1780 int ret;
1781
1782 list_for_each_entry(call, &ftrace_events, list) {
1783 /* Early boot up should not have any modules loaded */
1784 if (WARN_ON_ONCE(call->mod))
1785 continue;
1786
1787 ret = __trace_early_add_new_event(call, tr);
1788 if (ret < 0)
1789 pr_warning("Could not create early event %s\n",
1790 call->name);
1791 }
1792}
1793
Steven Rostedt0c8916c2012-08-07 16:14:16 -04001794/* Remove the event directory structure for a trace directory. */
1795static void
1796__trace_remove_event_dirs(struct trace_array *tr)
1797{
1798 struct ftrace_event_file *file, *next;
1799
1800 list_for_each_entry_safe(file, next, &tr->events, list) {
1801 list_del(&file->list);
1802 debugfs_remove_recursive(file->dir);
1803 remove_subsystem(file->system);
Steven Rostedtd1a29142013-02-27 20:23:57 -05001804 kmem_cache_free(file_cachep, file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04001805 }
1806}
1807
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001808static void
1809__add_event_to_tracers(struct ftrace_event_call *call,
1810 struct ftrace_module_file_ops *file_ops)
1811{
1812 struct trace_array *tr;
1813
1814 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1815 if (file_ops)
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05001816 __trace_add_new_mod_event(call, tr, file_ops);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001817 else
1818 __trace_add_new_event(call, tr,
1819 &ftrace_event_id_fops,
1820 &ftrace_enable_fops,
1821 &ftrace_event_filter_fops,
1822 &ftrace_event_format_fops);
1823 }
1824}
1825
Steven Rostedtec827c72009-09-14 10:50:23 -04001826static struct notifier_block trace_module_nb = {
Steven Rostedt6d723732009-04-10 14:53:50 -04001827 .notifier_call = trace_module_notify,
1828 .priority = 0,
1829};
1830
Steven Rostedte4a9ea52011-01-27 09:15:30 -05001831extern struct ftrace_event_call *__start_ftrace_events[];
1832extern struct ftrace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04001833
Li Zefan020e5f82009-07-01 10:47:05 +08001834static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1835
1836static __init int setup_trace_event(char *str)
1837{
1838 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1839 ring_buffer_expanded = 1;
1840 tracing_selftest_disabled = 1;
1841
1842 return 1;
1843}
1844__setup("trace_event=", setup_trace_event);
1845
Steven Rostedt77248222013-02-27 16:28:06 -05001846/* Expects to have event_mutex held when called */
1847static int
1848create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001849{
1850 struct dentry *d_events;
1851 struct dentry *entry;
1852
1853 entry = debugfs_create_file("set_event", 0644, parent,
1854 tr, &ftrace_set_event_fops);
1855 if (!entry) {
1856 pr_warning("Could not create debugfs 'set_event' entry\n");
1857 return -ENOMEM;
1858 }
1859
1860 d_events = debugfs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04001861 if (!d_events) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001862 pr_warning("Could not create debugfs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04001863 return -ENOMEM;
1864 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001865
1866 /* ring buffer internal formats */
1867 trace_create_file("header_page", 0444, d_events,
1868 ring_buffer_print_page_header,
1869 &ftrace_show_header_fops);
1870
1871 trace_create_file("header_event", 0444, d_events,
1872 ring_buffer_print_entry_header,
1873 &ftrace_show_header_fops);
1874
1875 trace_create_file("enable", 0644, d_events,
1876 tr, &ftrace_tr_enable_fops);
1877
1878 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05001879
1880 return 0;
1881}
1882
1883/**
1884 * event_trace_add_tracer - add a instance of a trace_array to events
1885 * @parent: The parent dentry to place the files/directories for events in
1886 * @tr: The trace array associated with these events
1887 *
1888 * When a new instance is created, it needs to set up its events
1889 * directory, as well as other files associated with events. It also
1890 * creates the event hierachry in the @parent/events directory.
1891 *
1892 * Returns 0 on success.
1893 */
1894int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
1895{
1896 int ret;
1897
1898 mutex_lock(&event_mutex);
1899
1900 ret = create_event_toplevel_files(parent, tr);
1901 if (ret)
1902 goto out_unlock;
1903
Steven Rostedt277ba042012-08-03 16:10:49 -04001904 down_write(&trace_event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001905 __trace_add_event_dirs(tr);
Steven Rostedt277ba042012-08-03 16:10:49 -04001906 up_write(&trace_event_mutex);
1907
Steven Rostedt77248222013-02-27 16:28:06 -05001908 out_unlock:
Steven Rostedt277ba042012-08-03 16:10:49 -04001909 mutex_unlock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001910
Steven Rostedt77248222013-02-27 16:28:06 -05001911 return ret;
1912}
1913
1914/*
1915 * The top trace array already had its file descriptors created.
1916 * Now the files themselves need to be created.
1917 */
1918static __init int
1919early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
1920{
1921 int ret;
1922
1923 mutex_lock(&event_mutex);
1924
1925 ret = create_event_toplevel_files(parent, tr);
1926 if (ret)
1927 goto out_unlock;
1928
1929 down_write(&trace_event_mutex);
1930 __trace_early_add_event_dirs(tr);
1931 up_write(&trace_event_mutex);
1932
1933 out_unlock:
1934 mutex_unlock(&event_mutex);
1935
1936 return ret;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001937}
1938
Steven Rostedt0c8916c2012-08-07 16:14:16 -04001939int event_trace_del_tracer(struct trace_array *tr)
1940{
1941 /* Disable any running events */
1942 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
1943
1944 mutex_lock(&event_mutex);
1945
1946 down_write(&trace_event_mutex);
1947 __trace_remove_event_dirs(tr);
1948 debugfs_remove_recursive(tr->event_dir);
1949 up_write(&trace_event_mutex);
1950
1951 tr->event_dir = NULL;
1952
1953 mutex_unlock(&event_mutex);
1954
1955 return 0;
1956}
1957
Steven Rostedtd1a29142013-02-27 20:23:57 -05001958static __init int event_trace_memsetup(void)
1959{
1960 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
1961 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
1962 return 0;
1963}
1964
Ezequiel Garcia87819152012-09-12 11:47:57 -03001965static __init int event_trace_enable(void)
1966{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001967 struct trace_array *tr = top_trace_array();
Ezequiel Garcia87819152012-09-12 11:47:57 -03001968 struct ftrace_event_call **iter, *call;
1969 char *buf = bootup_event_buf;
1970 char *token;
1971 int ret;
1972
1973 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
1974
1975 call = *iter;
1976 ret = event_init(call);
1977 if (!ret)
1978 list_add(&call->list, &ftrace_events);
1979 }
1980
Steven Rostedt77248222013-02-27 16:28:06 -05001981 /*
1982 * We need the top trace array to have a working set of trace
1983 * points at early init, before the debug files and directories
1984 * are created. Create the file entries now, and attach them
1985 * to the actual file dentries later.
1986 */
1987 __trace_early_add_events(tr);
1988
Ezequiel Garcia87819152012-09-12 11:47:57 -03001989 while (true) {
1990 token = strsep(&buf, ",");
1991
1992 if (!token)
1993 break;
1994 if (!*token)
1995 continue;
1996
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001997 ret = ftrace_set_clr_event(tr, token, 1);
Ezequiel Garcia87819152012-09-12 11:47:57 -03001998 if (ret)
1999 pr_warn("Failed to enable trace event: %s\n", token);
2000 }
Steven Rostedt81698832012-10-11 10:15:05 -04002001
2002 trace_printk_start_comm();
2003
Ezequiel Garcia87819152012-09-12 11:47:57 -03002004 return 0;
2005}
2006
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002007static __init int event_trace_init(void)
2008{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002009 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002010 struct dentry *d_tracer;
2011 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04002012 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002013
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002014 tr = top_trace_array();
2015
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002016 d_tracer = tracing_init_dentry();
2017 if (!d_tracer)
2018 return 0;
2019
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002020 entry = debugfs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002021 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04002022 if (!entry)
2023 pr_warning("Could not create debugfs "
2024 "'available_events' entry\n");
2025
Li Zefan8728fe52010-05-24 16:22:49 +08002026 if (trace_define_common_fields())
2027 pr_warning("tracing: Failed to allocate common fields");
2028
Steven Rostedt77248222013-02-27 16:28:06 -05002029 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002030 if (ret)
2031 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08002032
Steven Rostedt6d723732009-04-10 14:53:50 -04002033 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08002034 if (ret)
Steven Rostedt6d723732009-04-10 14:53:50 -04002035 pr_warning("Failed to register trace events module notifier\n");
2036
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002037 return 0;
2038}
Steven Rostedtd1a29142013-02-27 20:23:57 -05002039early_initcall(event_trace_memsetup);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002040core_initcall(event_trace_enable);
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002041fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002042
2043#ifdef CONFIG_FTRACE_STARTUP_TEST
2044
2045static DEFINE_SPINLOCK(test_spinlock);
2046static DEFINE_SPINLOCK(test_spinlock_irq);
2047static DEFINE_MUTEX(test_mutex);
2048
2049static __init void test_work(struct work_struct *dummy)
2050{
2051 spin_lock(&test_spinlock);
2052 spin_lock_irq(&test_spinlock_irq);
2053 udelay(1);
2054 spin_unlock_irq(&test_spinlock_irq);
2055 spin_unlock(&test_spinlock);
2056
2057 mutex_lock(&test_mutex);
2058 msleep(1);
2059 mutex_unlock(&test_mutex);
2060}
2061
2062static __init int event_test_thread(void *unused)
2063{
2064 void *test_malloc;
2065
2066 test_malloc = kmalloc(1234, GFP_KERNEL);
2067 if (!test_malloc)
2068 pr_info("failed to kmalloc\n");
2069
2070 schedule_on_each_cpu(test_work);
2071
2072 kfree(test_malloc);
2073
2074 set_current_state(TASK_INTERRUPTIBLE);
2075 while (!kthread_should_stop())
2076 schedule();
2077
2078 return 0;
2079}
2080
2081/*
2082 * Do various things that may trigger events.
2083 */
2084static __init void event_test_stuff(void)
2085{
2086 struct task_struct *test_thread;
2087
2088 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2089 msleep(1);
2090 kthread_stop(test_thread);
2091}
2092
2093/*
2094 * For every trace event defined, we will test each trace point separately,
2095 * and then by groups, and finally all trace points.
2096 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002097static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04002098{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002099 struct ftrace_subsystem_dir *dir;
2100 struct ftrace_event_file *file;
Steven Rostedte6187002009-04-15 13:36:40 -04002101 struct ftrace_event_call *call;
2102 struct event_subsystem *system;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002103 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04002104 int ret;
2105
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002106 tr = top_trace_array();
2107
Steven Rostedte6187002009-04-15 13:36:40 -04002108 pr_info("Running tests on trace events:\n");
2109
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002110 list_for_each_entry(file, &tr->events, list) {
2111
2112 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04002113
Steven Rostedt22392912010-04-21 12:27:06 -04002114 /* Only test those that have a probe */
2115 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04002116 continue;
2117
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002118/*
2119 * Testing syscall events here is pretty useless, but
2120 * we still do it if configured. But this is time consuming.
2121 * What we really need is a user thread to perform the
2122 * syscalls as we test.
2123 */
2124#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04002125 if (call->class->system &&
2126 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04002127 continue;
2128#endif
2129
Steven Rostedte6187002009-04-15 13:36:40 -04002130 pr_info("Testing event %s: ", call->name);
2131
2132 /*
2133 * If an event is already enabled, someone is using
2134 * it and the self test should not be on.
2135 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002136 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
Steven Rostedte6187002009-04-15 13:36:40 -04002137 pr_warning("Enabled event during self test!\n");
2138 WARN_ON_ONCE(1);
2139 continue;
2140 }
2141
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002142 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002143 event_test_stuff();
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002144 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002145
2146 pr_cont("OK\n");
2147 }
2148
2149 /* Now test at the sub system level */
2150
2151 pr_info("Running tests on trace event systems:\n");
2152
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002153 list_for_each_entry(dir, &tr->systems, list) {
2154
2155 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04002156
2157 /* the ftrace system is special, skip it */
2158 if (strcmp(system->name, "ftrace") == 0)
2159 continue;
2160
2161 pr_info("Testing event system %s: ", system->name);
2162
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002163 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002164 if (WARN_ON_ONCE(ret)) {
2165 pr_warning("error enabling system %s\n",
2166 system->name);
2167 continue;
2168 }
2169
2170 event_test_stuff();
2171
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002172 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002173 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04002174 pr_warning("error disabling system %s\n",
2175 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08002176 continue;
2177 }
Steven Rostedte6187002009-04-15 13:36:40 -04002178
2179 pr_cont("OK\n");
2180 }
2181
2182 /* Test with all events enabled */
2183
2184 pr_info("Running tests on all trace events:\n");
2185 pr_info("Testing all events: ");
2186
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002187 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04002188 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04002189 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002190 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002191 }
2192
2193 event_test_stuff();
2194
2195 /* reset sysname */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002196 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04002197 if (WARN_ON_ONCE(ret)) {
2198 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002199 return;
Steven Rostedte6187002009-04-15 13:36:40 -04002200 }
2201
2202 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002203}
2204
2205#ifdef CONFIG_FUNCTION_TRACER
2206
Tejun Heo245b2e72009-06-24 15:13:48 +09002207static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002208
2209static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002210function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002211 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002212{
2213 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002214 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002215 struct ftrace_entry *entry;
2216 unsigned long flags;
2217 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002218 int cpu;
2219 int pc;
2220
2221 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002222 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002223 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09002224 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002225
2226 if (disabled != 1)
2227 goto out;
2228
2229 local_save_flags(flags);
2230
Steven Rostedte77405a2009-09-02 14:17:06 -04002231 event = trace_current_buffer_lock_reserve(&buffer,
2232 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002233 flags, pc);
2234 if (!event)
2235 goto out;
2236 entry = ring_buffer_event_data(event);
2237 entry->ip = ip;
2238 entry->parent_ip = parent_ip;
2239
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002240 trace_buffer_unlock_commit(buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002241
2242 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09002243 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04002244 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002245}
2246
2247static struct ftrace_ops trace_ops __initdata =
2248{
2249 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04002250 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002251};
2252
2253static __init void event_trace_self_test_with_function(void)
2254{
Steven Rostedt17bb6152011-05-23 15:27:46 -04002255 int ret;
2256 ret = register_ftrace_function(&trace_ops);
2257 if (WARN_ON(ret < 0)) {
2258 pr_info("Failed to enable function tracer for event tests\n");
2259 return;
2260 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04002261 pr_info("Running tests again, along with the function tracer\n");
2262 event_trace_self_tests();
2263 unregister_ftrace_function(&trace_ops);
2264}
2265#else
2266static __init void event_trace_self_test_with_function(void)
2267{
2268}
2269#endif
2270
2271static __init int event_trace_self_tests_init(void)
2272{
Li Zefan020e5f82009-07-01 10:47:05 +08002273 if (!tracing_selftest_disabled) {
2274 event_trace_self_tests();
2275 event_trace_self_test_with_function();
2276 }
Steven Rostedte6187002009-04-15 13:36:40 -04002277
2278 return 0;
2279}
2280
Steven Rostedt28d20e22009-04-20 12:12:44 -04002281late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04002282
2283#endif