blob: 78b1ed230177246349d10ba83615b99f3adcbde3 [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>
Steven Rostedte6187002009-04-15 13:36:40 -040018#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050019
Li Zefan020e5f82009-07-01 10:47:05 +080020#include <asm/setup.h>
21
Steven Rostedt91729ef92009-03-02 15:03:01 -050022#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050023
Steven Rostedtb628b3e2009-02-27 23:32:58 -050024#define TRACE_SYSTEM "TRACE_SYSTEM"
25
Li Zefan20c89282009-05-06 10:33:45 +080026DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050027
Steven Rostedta59fd602009-04-10 13:52:20 -040028LIST_HEAD(ftrace_events);
29
Li Zefan540b7b82009-08-19 15:54:51 +080030int trace_define_field(struct ftrace_event_call *call, const char *type,
Li Zefan43b51ea2009-08-07 10:33:22 +080031 const char *name, int offset, int size, int is_signed,
32 int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -050033{
34 struct ftrace_event_field *field;
35
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010036 field = kzalloc(sizeof(*field), GFP_KERNEL);
Tom Zanussicf027f62009-03-22 03:30:39 -050037 if (!field)
38 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010039
Tom Zanussicf027f62009-03-22 03:30:39 -050040 field->name = kstrdup(name, GFP_KERNEL);
41 if (!field->name)
42 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010043
Tom Zanussicf027f62009-03-22 03:30:39 -050044 field->type = kstrdup(type, GFP_KERNEL);
45 if (!field->type)
46 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010047
Li Zefan43b51ea2009-08-07 10:33:22 +080048 if (filter_type == FILTER_OTHER)
49 field->filter_type = filter_assign_type(type);
50 else
51 field->filter_type = filter_type;
52
Tom Zanussicf027f62009-03-22 03:30:39 -050053 field->offset = offset;
54 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -050055 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +080056
Tom Zanussicf027f62009-03-22 03:30:39 -050057 list_add(&field->link, &call->fields);
58
59 return 0;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010060
Tom Zanussicf027f62009-03-22 03:30:39 -050061err:
62 if (field) {
63 kfree(field->name);
64 kfree(field->type);
65 }
66 kfree(field);
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010067
Tom Zanussicf027f62009-03-22 03:30:39 -050068 return -ENOMEM;
69}
Steven Rostedt17c873e2009-04-10 18:12:50 -040070EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -050071
Li Zefane647d6b2009-08-19 15:54:32 +080072#define __common_field(type, item) \
73 ret = trace_define_field(call, #type, "common_" #item, \
74 offsetof(typeof(ent), item), \
75 sizeof(ent.item), \
Li Zefan43b51ea2009-08-07 10:33:22 +080076 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +080077 if (ret) \
78 return ret;
79
80int trace_define_common_fields(struct ftrace_event_call *call)
81{
82 int ret;
83 struct trace_entry ent;
84
85 __common_field(unsigned short, type);
86 __common_field(unsigned char, flags);
87 __common_field(unsigned char, preempt_count);
88 __common_field(int, pid);
89 __common_field(int, tgid);
90
91 return ret;
92}
Li Zefan540b7b82009-08-19 15:54:51 +080093EXPORT_SYMBOL_GPL(trace_define_common_fields);
Li Zefane647d6b2009-08-19 15:54:32 +080094
Li Zefan2df75e42009-05-06 10:33:04 +080095#ifdef CONFIG_MODULES
96
97static void trace_destroy_fields(struct ftrace_event_call *call)
98{
99 struct ftrace_event_field *field, *next;
100
101 list_for_each_entry_safe(field, next, &call->fields, link) {
102 list_del(&field->link);
103 kfree(field->type);
104 kfree(field->name);
105 kfree(field);
106 }
107}
108
109#endif /* CONFIG_MODULES */
110
Steven Rostedtfd994982009-02-28 02:41:25 -0500111static void ftrace_event_enable_disable(struct ftrace_event_call *call,
112 int enable)
113{
Steven Rostedtfd994982009-02-28 02:41:25 -0500114 switch (enable) {
115 case 0:
116 if (call->enabled) {
117 call->enabled = 0;
Zhaoleib11c53e2009-05-25 18:11:59 +0800118 tracing_stop_cmdline_record();
Jason Baron69fd4f02009-08-10 16:52:44 -0400119 call->unregfunc(call->data);
Steven Rostedtfd994982009-02-28 02:41:25 -0500120 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500121 break;
122 case 1:
Steven Rostedtda4d0302009-03-09 17:14:30 -0400123 if (!call->enabled) {
Steven Rostedtfd994982009-02-28 02:41:25 -0500124 call->enabled = 1;
Zhaoleib11c53e2009-05-25 18:11:59 +0800125 tracing_start_cmdline_record();
Jason Baron69fd4f02009-08-10 16:52:44 -0400126 call->regfunc(call->data);
Steven Rostedtfd994982009-02-28 02:41:25 -0500127 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500128 break;
129 }
130}
131
Zhaolei0e907c92009-05-25 18:13:59 +0800132static void ftrace_clear_events(void)
133{
134 struct ftrace_event_call *call;
135
136 mutex_lock(&event_mutex);
137 list_for_each_entry(call, &ftrace_events, list) {
138 ftrace_event_enable_disable(call, 0);
139 }
140 mutex_unlock(&event_mutex);
141}
142
Li Zefan8f31bfe2009-05-08 10:31:42 +0800143/*
144 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
145 */
146static int __ftrace_set_clr_event(const char *match, const char *sub,
147 const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500148{
Steven Rostedta59fd602009-04-10 13:52:20 -0400149 struct ftrace_event_call *call;
Steven Rostedt29f93942009-05-08 16:06:47 -0400150 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500151
Steven Rostedt11a241a2009-03-02 11:49:04 -0500152 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -0400153 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500154
Steven Rostedt40e26812009-03-10 11:32:40 -0400155 if (!call->name || !call->regfunc)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500156 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500157
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500158 if (match &&
159 strcmp(match, call->name) != 0 &&
160 strcmp(match, call->system) != 0)
161 continue;
162
163 if (sub && strcmp(sub, call->system) != 0)
164 continue;
165
166 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500167 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500168
Steven Rostedtfd994982009-02-28 02:41:25 -0500169 ftrace_event_enable_disable(call, set);
170
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500171 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500172 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500173 mutex_unlock(&event_mutex);
174
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500175 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500176}
177
Li Zefan8f31bfe2009-05-08 10:31:42 +0800178static int ftrace_set_clr_event(char *buf, int set)
179{
180 char *event = NULL, *sub = NULL, *match;
181
182 /*
183 * The buf format can be <subsystem>:<event-name>
184 * *:<event-name> means any event by that name.
185 * :<event-name> is the same.
186 *
187 * <subsystem>:* means all events in that subsystem
188 * <subsystem>: means the same.
189 *
190 * <name> (no ':') means all events in a subsystem with
191 * the name <name> or any event that matches <name>
192 */
193
194 match = strsep(&buf, ":");
195 if (buf) {
196 sub = match;
197 event = buf;
198 match = NULL;
199
200 if (!strlen(sub) || strcmp(sub, "*") == 0)
201 sub = NULL;
202 if (!strlen(event) || strcmp(event, "*") == 0)
203 event = NULL;
204 }
205
206 return __ftrace_set_clr_event(match, sub, event, set);
207}
208
Steven Rostedt4671c792009-05-08 16:27:41 -0400209/**
210 * trace_set_clr_event - enable or disable an event
211 * @system: system name to match (NULL for any system)
212 * @event: event name to match (NULL for all events, within system)
213 * @set: 1 to enable, 0 to disable
214 *
215 * This is a way for other parts of the kernel to enable or disable
216 * event recording.
217 *
218 * Returns 0 on success, -EINVAL if the parameters do not match any
219 * registered events.
220 */
221int trace_set_clr_event(const char *system, const char *event, int set)
222{
223 return __ftrace_set_clr_event(NULL, system, event, set);
224}
225
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500226/* 128 should be much more than enough */
227#define EVENT_BUF_SIZE 127
228
229static ssize_t
230ftrace_event_write(struct file *file, const char __user *ubuf,
231 size_t cnt, loff_t *ppos)
232{
233 size_t read = 0;
234 int i, set = 1;
235 ssize_t ret;
236 char *buf;
237 char ch;
238
239 if (!cnt || cnt < 0)
240 return 0;
241
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400242 ret = tracing_update_buffers();
243 if (ret < 0)
244 return ret;
245
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500246 ret = get_user(ch, ubuf++);
247 if (ret)
248 return ret;
249 read++;
250 cnt--;
251
252 /* skip white space */
253 while (cnt && isspace(ch)) {
254 ret = get_user(ch, ubuf++);
255 if (ret)
256 return ret;
257 read++;
258 cnt--;
259 }
260
261 /* Only white space found? */
262 if (isspace(ch)) {
263 file->f_pos += read;
264 ret = read;
265 return ret;
266 }
267
268 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
269 if (!buf)
270 return -ENOMEM;
271
272 if (cnt > EVENT_BUF_SIZE)
273 cnt = EVENT_BUF_SIZE;
274
275 i = 0;
276 while (cnt && !isspace(ch)) {
277 if (!i && ch == '!')
278 set = 0;
279 else
280 buf[i++] = ch;
281
282 ret = get_user(ch, ubuf++);
283 if (ret)
284 goto out_free;
285 read++;
286 cnt--;
287 }
288 buf[i] = 0;
289
290 file->f_pos += read;
291
292 ret = ftrace_set_clr_event(buf, set);
293 if (ret)
294 goto out_free;
295
296 ret = read;
297
298 out_free:
299 kfree(buf);
300
301 return ret;
302}
303
304static void *
305t_next(struct seq_file *m, void *v, loff_t *pos)
306{
Steven Rostedta59fd602009-04-10 13:52:20 -0400307 struct list_head *list = m->private;
308 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500309
310 (*pos)++;
311
Steven Rostedt40e26812009-03-10 11:32:40 -0400312 for (;;) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400313 if (list == &ftrace_events)
Steven Rostedt40e26812009-03-10 11:32:40 -0400314 return NULL;
315
Steven Rostedta59fd602009-04-10 13:52:20 -0400316 call = list_entry(list, struct ftrace_event_call, list);
317
Steven Rostedt40e26812009-03-10 11:32:40 -0400318 /*
319 * The ftrace subsystem is for showing formats only.
320 * They can not be enabled or disabled via the event files.
321 */
322 if (call->regfunc)
323 break;
324
Steven Rostedta59fd602009-04-10 13:52:20 -0400325 list = list->next;
Steven Rostedt40e26812009-03-10 11:32:40 -0400326 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500327
Steven Rostedta59fd602009-04-10 13:52:20 -0400328 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500329
330 return call;
331}
332
333static void *t_start(struct seq_file *m, loff_t *pos)
334{
Li Zefane1c7e2a2009-06-24 09:52:29 +0800335 struct ftrace_event_call *call = NULL;
336 loff_t l;
337
Li Zefan20c89282009-05-06 10:33:45 +0800338 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800339
340 m->private = ftrace_events.next;
341 for (l = 0; l <= *pos; ) {
342 call = t_next(m, NULL, &l);
343 if (!call)
344 break;
345 }
346 return call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500347}
348
349static void *
350s_next(struct seq_file *m, void *v, loff_t *pos)
351{
Steven Rostedta59fd602009-04-10 13:52:20 -0400352 struct list_head *list = m->private;
353 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500354
355 (*pos)++;
356
357 retry:
Steven Rostedta59fd602009-04-10 13:52:20 -0400358 if (list == &ftrace_events)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500359 return NULL;
360
Steven Rostedta59fd602009-04-10 13:52:20 -0400361 call = list_entry(list, struct ftrace_event_call, list);
362
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500363 if (!call->enabled) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400364 list = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500365 goto retry;
366 }
367
Steven Rostedta59fd602009-04-10 13:52:20 -0400368 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500369
370 return call;
371}
372
373static void *s_start(struct seq_file *m, loff_t *pos)
374{
Li Zefane1c7e2a2009-06-24 09:52:29 +0800375 struct ftrace_event_call *call = NULL;
376 loff_t l;
377
Li Zefan20c89282009-05-06 10:33:45 +0800378 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800379
380 m->private = ftrace_events.next;
381 for (l = 0; l <= *pos; ) {
382 call = s_next(m, NULL, &l);
383 if (!call)
384 break;
385 }
386 return call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500387}
388
389static int t_show(struct seq_file *m, void *v)
390{
391 struct ftrace_event_call *call = v;
392
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500393 if (strcmp(call->system, TRACE_SYSTEM) != 0)
394 seq_printf(m, "%s:", call->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500395 seq_printf(m, "%s\n", call->name);
396
397 return 0;
398}
399
400static void t_stop(struct seq_file *m, void *p)
401{
Li Zefan20c89282009-05-06 10:33:45 +0800402 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500403}
404
405static int
406ftrace_event_seq_open(struct inode *inode, struct file *file)
407{
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500408 const struct seq_operations *seq_ops;
409
410 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -0400411 (file->f_flags & O_TRUNC))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500412 ftrace_clear_events();
413
414 seq_ops = inode->i_private;
Li Zefan20c89282009-05-06 10:33:45 +0800415 return seq_open(file, seq_ops);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500416}
417
Steven Rostedt1473e442009-02-24 14:15:08 -0500418static ssize_t
419event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
420 loff_t *ppos)
421{
422 struct ftrace_event_call *call = filp->private_data;
423 char *buf;
424
Steven Rostedtda4d0302009-03-09 17:14:30 -0400425 if (call->enabled)
Steven Rostedt1473e442009-02-24 14:15:08 -0500426 buf = "1\n";
427 else
428 buf = "0\n";
429
430 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
431}
432
433static ssize_t
434event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
435 loff_t *ppos)
436{
437 struct ftrace_event_call *call = filp->private_data;
438 char buf[64];
439 unsigned long val;
440 int ret;
441
442 if (cnt >= sizeof(buf))
443 return -EINVAL;
444
445 if (copy_from_user(&buf, ubuf, cnt))
446 return -EFAULT;
447
448 buf[cnt] = 0;
449
450 ret = strict_strtoul(buf, 10, &val);
451 if (ret < 0)
452 return ret;
453
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400454 ret = tracing_update_buffers();
455 if (ret < 0)
456 return ret;
457
Steven Rostedt1473e442009-02-24 14:15:08 -0500458 switch (val) {
459 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500460 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500461 mutex_lock(&event_mutex);
Steven Rostedtfd994982009-02-28 02:41:25 -0500462 ftrace_event_enable_disable(call, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500463 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500464 break;
465
466 default:
467 return -EINVAL;
468 }
469
470 *ppos += cnt;
471
472 return cnt;
473}
474
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400475static ssize_t
476system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
477 loff_t *ppos)
478{
Li Zefanc142b152009-05-08 10:32:05 +0800479 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400480 const char *system = filp->private_data;
481 struct ftrace_event_call *call;
482 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800483 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400484 int ret;
485
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400486 mutex_lock(&event_mutex);
487 list_for_each_entry(call, &ftrace_events, list) {
488 if (!call->name || !call->regfunc)
489 continue;
490
Li Zefan8f31bfe2009-05-08 10:31:42 +0800491 if (system && strcmp(call->system, system) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400492 continue;
493
494 /*
495 * We need to find out if all the events are set
496 * or if all events or cleared, or if we have
497 * a mixture.
498 */
Li Zefanc142b152009-05-08 10:32:05 +0800499 set |= (1 << !!call->enabled);
500
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400501 /*
502 * If we have a mixture, no need to look further.
503 */
Li Zefanc142b152009-05-08 10:32:05 +0800504 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400505 break;
506 }
507 mutex_unlock(&event_mutex);
508
Li Zefanc142b152009-05-08 10:32:05 +0800509 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400510 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400511
512 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
513
514 return ret;
515}
516
517static ssize_t
518system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
519 loff_t *ppos)
520{
521 const char *system = filp->private_data;
522 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400523 char buf[64];
524 ssize_t ret;
525
526 if (cnt >= sizeof(buf))
527 return -EINVAL;
528
529 if (copy_from_user(&buf, ubuf, cnt))
530 return -EFAULT;
531
532 buf[cnt] = 0;
533
534 ret = strict_strtoul(buf, 10, &val);
535 if (ret < 0)
536 return ret;
537
538 ret = tracing_update_buffers();
539 if (ret < 0)
540 return ret;
541
Li Zefan8f31bfe2009-05-08 10:31:42 +0800542 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400543 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400544
Li Zefan8f31bfe2009-05-08 10:31:42 +0800545 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400546 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800547 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400548
549 ret = cnt;
550
Li Zefan8f31bfe2009-05-08 10:31:42 +0800551out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400552 *ppos += cnt;
553
554 return ret;
555}
556
Steven Rostedt75db37d2009-03-26 11:43:36 -0400557extern char *__bad_type_size(void);
558
Steven Rostedt91729ef92009-03-02 15:03:01 -0500559#undef FIELD
Steven Rostedt156b5f12009-03-06 10:50:53 -0500560#define FIELD(type, name) \
Steven Rostedt75db37d2009-03-26 11:43:36 -0400561 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
Tom Zanussicf027f62009-03-22 03:30:39 -0500562 #type, "common_" #name, offsetof(typeof(field), name), \
563 sizeof(field.name)
Steven Rostedt91729ef92009-03-02 15:03:01 -0500564
565static int trace_write_header(struct trace_seq *s)
566{
567 struct trace_entry field;
568
569 /* struct trace_entry */
570 return trace_seq_printf(s,
Steven Rostedtce8eb2b2009-03-10 10:14:35 -0400571 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
572 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
573 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
574 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
575 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
Steven Rostedt91729ef92009-03-02 15:03:01 -0500576 "\n",
Steven Rostedt89ec0de2009-03-26 11:03:29 -0400577 FIELD(unsigned short, type),
Steven Rostedt91729ef92009-03-02 15:03:01 -0500578 FIELD(unsigned char, flags),
579 FIELD(unsigned char, preempt_count),
580 FIELD(int, pid),
581 FIELD(int, tgid));
582}
Steven Rostedtda4d0302009-03-09 17:14:30 -0400583
Steven Rostedt981d0812009-03-02 13:53:59 -0500584static ssize_t
585event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
586 loff_t *ppos)
587{
588 struct ftrace_event_call *call = filp->private_data;
589 struct trace_seq *s;
590 char *buf;
591 int r;
592
Tom Zanussic269fc82009-03-17 01:20:59 -0500593 if (*ppos)
594 return 0;
595
Steven Rostedt981d0812009-03-02 13:53:59 -0500596 s = kmalloc(sizeof(*s), GFP_KERNEL);
597 if (!s)
598 return -ENOMEM;
599
600 trace_seq_init(s);
601
Steven Rostedtc5e4e192009-03-02 15:10:02 -0500602 /* If any of the first writes fail, so will the show_format. */
603
604 trace_seq_printf(s, "name: %s\n", call->name);
605 trace_seq_printf(s, "ID: %d\n", call->id);
606 trace_seq_printf(s, "format:\n");
Steven Rostedt91729ef92009-03-02 15:03:01 -0500607 trace_write_header(s);
608
Frederic Weisbeckere8f9f4d2009-08-11 17:42:52 +0200609 r = call->show_format(call, s);
Steven Rostedt981d0812009-03-02 13:53:59 -0500610 if (!r) {
611 /*
612 * ug! The format output is bigger than a PAGE!!
613 */
614 buf = "FORMAT TOO BIG\n";
615 r = simple_read_from_buffer(ubuf, cnt, ppos,
616 buf, strlen(buf));
617 goto out;
618 }
619
620 r = simple_read_from_buffer(ubuf, cnt, ppos,
621 s->buffer, s->len);
622 out:
623 kfree(s);
624 return r;
625}
626
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100627static ssize_t
628event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
629{
630 struct ftrace_event_call *call = filp->private_data;
631 struct trace_seq *s;
632 int r;
633
634 if (*ppos)
635 return 0;
636
637 s = kmalloc(sizeof(*s), GFP_KERNEL);
638 if (!s)
639 return -ENOMEM;
640
641 trace_seq_init(s);
642 trace_seq_printf(s, "%d\n", call->id);
643
644 r = simple_read_from_buffer(ubuf, cnt, ppos,
645 s->buffer, s->len);
646 kfree(s);
647 return r;
648}
649
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500650static ssize_t
651event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
652 loff_t *ppos)
653{
654 struct ftrace_event_call *call = filp->private_data;
655 struct trace_seq *s;
656 int r;
657
658 if (*ppos)
659 return 0;
660
661 s = kmalloc(sizeof(*s), GFP_KERNEL);
662 if (!s)
663 return -ENOMEM;
664
665 trace_seq_init(s);
666
Tom Zanussi8b372562009-04-28 03:04:59 -0500667 print_event_filter(call, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500668 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500669
670 kfree(s);
671
672 return r;
673}
674
675static ssize_t
676event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
677 loff_t *ppos)
678{
679 struct ftrace_event_call *call = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500680 char *buf;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500681 int err;
682
Tom Zanussi8b372562009-04-28 03:04:59 -0500683 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500684 return -EINVAL;
685
Tom Zanussi8b372562009-04-28 03:04:59 -0500686 buf = (char *)__get_free_page(GFP_TEMPORARY);
687 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500688 return -ENOMEM;
689
Tom Zanussi8b372562009-04-28 03:04:59 -0500690 if (copy_from_user(buf, ubuf, cnt)) {
691 free_page((unsigned long) buf);
692 return -EFAULT;
693 }
694 buf[cnt] = '\0';
695
696 err = apply_event_filter(call, buf);
697 free_page((unsigned long) buf);
698 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500699 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500700
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500701 *ppos += cnt;
702
703 return cnt;
704}
705
Tom Zanussicfb180f2009-03-22 03:31:17 -0500706static ssize_t
707subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
708 loff_t *ppos)
709{
710 struct event_subsystem *system = filp->private_data;
711 struct trace_seq *s;
712 int r;
713
714 if (*ppos)
715 return 0;
716
717 s = kmalloc(sizeof(*s), GFP_KERNEL);
718 if (!s)
719 return -ENOMEM;
720
721 trace_seq_init(s);
722
Tom Zanussi8b372562009-04-28 03:04:59 -0500723 print_subsystem_event_filter(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500724 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500725
726 kfree(s);
727
728 return r;
729}
730
731static ssize_t
732subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
733 loff_t *ppos)
734{
735 struct event_subsystem *system = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500736 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500737 int err;
738
Tom Zanussi8b372562009-04-28 03:04:59 -0500739 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500740 return -EINVAL;
741
Tom Zanussi8b372562009-04-28 03:04:59 -0500742 buf = (char *)__get_free_page(GFP_TEMPORARY);
743 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500744 return -ENOMEM;
745
Tom Zanussi8b372562009-04-28 03:04:59 -0500746 if (copy_from_user(buf, ubuf, cnt)) {
747 free_page((unsigned long) buf);
748 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500749 }
Tom Zanussi8b372562009-04-28 03:04:59 -0500750 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -0500751
Tom Zanussi8b372562009-04-28 03:04:59 -0500752 err = apply_subsystem_event_filter(system, buf);
753 free_page((unsigned long) buf);
754 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +0800755 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500756
757 *ppos += cnt;
758
759 return cnt;
760}
761
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400762static ssize_t
763show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
764{
765 int (*func)(struct trace_seq *s) = filp->private_data;
766 struct trace_seq *s;
767 int r;
768
769 if (*ppos)
770 return 0;
771
772 s = kmalloc(sizeof(*s), GFP_KERNEL);
773 if (!s)
774 return -ENOMEM;
775
776 trace_seq_init(s);
777
778 func(s);
779 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
780
781 kfree(s);
782
783 return r;
784}
785
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500786static const struct seq_operations show_event_seq_ops = {
787 .start = t_start,
788 .next = t_next,
789 .show = t_show,
790 .stop = t_stop,
791};
792
793static const struct seq_operations show_set_event_seq_ops = {
794 .start = s_start,
795 .next = s_next,
796 .show = t_show,
797 .stop = t_stop,
798};
799
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400800static const struct file_operations ftrace_avail_fops = {
801 .open = ftrace_event_seq_open,
802 .read = seq_read,
803 .llseek = seq_lseek,
804 .release = seq_release,
805};
806
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500807static const struct file_operations ftrace_set_event_fops = {
808 .open = ftrace_event_seq_open,
809 .read = seq_read,
810 .write = ftrace_event_write,
811 .llseek = seq_lseek,
812 .release = seq_release,
813};
814
Steven Rostedt1473e442009-02-24 14:15:08 -0500815static const struct file_operations ftrace_enable_fops = {
816 .open = tracing_open_generic,
817 .read = event_enable_read,
818 .write = event_enable_write,
819};
820
Steven Rostedt981d0812009-03-02 13:53:59 -0500821static const struct file_operations ftrace_event_format_fops = {
822 .open = tracing_open_generic,
823 .read = event_format_read,
824};
825
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100826static const struct file_operations ftrace_event_id_fops = {
827 .open = tracing_open_generic,
828 .read = event_id_read,
829};
830
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500831static const struct file_operations ftrace_event_filter_fops = {
832 .open = tracing_open_generic,
833 .read = event_filter_read,
834 .write = event_filter_write,
835};
836
Tom Zanussicfb180f2009-03-22 03:31:17 -0500837static const struct file_operations ftrace_subsystem_filter_fops = {
838 .open = tracing_open_generic,
839 .read = subsystem_filter_read,
840 .write = subsystem_filter_write,
841};
842
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400843static const struct file_operations ftrace_system_enable_fops = {
844 .open = tracing_open_generic,
845 .read = system_enable_read,
846 .write = system_enable_write,
847};
848
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400849static const struct file_operations ftrace_show_header_fops = {
850 .open = tracing_open_generic,
851 .read = show_header,
852};
853
Steven Rostedt1473e442009-02-24 14:15:08 -0500854static struct dentry *event_trace_events_dir(void)
855{
856 static struct dentry *d_tracer;
857 static struct dentry *d_events;
858
859 if (d_events)
860 return d_events;
861
862 d_tracer = tracing_init_dentry();
863 if (!d_tracer)
864 return NULL;
865
866 d_events = debugfs_create_dir("events", d_tracer);
867 if (!d_events)
868 pr_warning("Could not create debugfs "
869 "'events' directory\n");
870
871 return d_events;
872}
873
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500874static LIST_HEAD(event_subsystems);
875
876static struct dentry *
877event_subsystem_dir(const char *name, struct dentry *d_events)
878{
879 struct event_subsystem *system;
Tom Zanussie1112b42009-03-31 00:48:49 -0500880 struct dentry *entry;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500881
882 /* First see if we did not already create this dir */
883 list_for_each_entry(system, &event_subsystems, list) {
Xiao Guangrongdc82ec98a2009-07-09 16:22:22 +0800884 if (strcmp(system->name, name) == 0) {
885 system->nr_events++;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500886 return system->entry;
Xiao Guangrongdc82ec98a2009-07-09 16:22:22 +0800887 }
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500888 }
889
890 /* need to create new entry */
891 system = kmalloc(sizeof(*system), GFP_KERNEL);
892 if (!system) {
893 pr_warning("No memory to create event subsystem %s\n",
894 name);
895 return d_events;
896 }
897
898 system->entry = debugfs_create_dir(name, d_events);
899 if (!system->entry) {
900 pr_warning("Could not create event subsystem %s\n",
901 name);
902 kfree(system);
903 return d_events;
904 }
905
Xiao Guangrongdc82ec98a2009-07-09 16:22:22 +0800906 system->nr_events = 1;
Steven Rostedt6d723732009-04-10 14:53:50 -0400907 system->name = kstrdup(name, GFP_KERNEL);
908 if (!system->name) {
909 debugfs_remove(system->entry);
910 kfree(system);
911 return d_events;
912 }
913
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500914 list_add(&system->list, &event_subsystems);
915
Tom Zanussi30e673b2009-04-28 03:04:47 -0500916 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500917
Tom Zanussi8b372562009-04-28 03:04:59 -0500918 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
919 if (!system->filter) {
920 pr_warning("Could not allocate filter for subsystem "
921 "'%s'\n", name);
922 return system->entry;
923 }
924
Tom Zanussie1112b42009-03-31 00:48:49 -0500925 entry = debugfs_create_file("filter", 0644, system->entry, system,
926 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -0500927 if (!entry) {
928 kfree(system->filter);
929 system->filter = NULL;
Tom Zanussie1112b42009-03-31 00:48:49 -0500930 pr_warning("Could not create debugfs "
931 "'%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -0500932 }
Tom Zanussie1112b42009-03-31 00:48:49 -0500933
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400934 entry = trace_create_file("enable", 0644, system->entry,
935 (void *)system->name,
936 &ftrace_system_enable_fops);
937
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500938 return system->entry;
939}
940
Steven Rostedt1473e442009-02-24 14:15:08 -0500941static int
Steven Rostedt701970b2009-04-24 23:11:22 -0400942event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
943 const struct file_operations *id,
944 const struct file_operations *enable,
945 const struct file_operations *filter,
946 const struct file_operations *format)
Steven Rostedt1473e442009-02-24 14:15:08 -0500947{
948 struct dentry *entry;
Steven Rostedtfd994982009-02-28 02:41:25 -0500949 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -0500950
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500951 /*
952 * If the trace point header did not define TRACE_SYSTEM
953 * then the system would be called "TRACE_SYSTEM".
954 */
Steven Rostedt6d723732009-04-10 14:53:50 -0400955 if (strcmp(call->system, TRACE_SYSTEM) != 0)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500956 d_events = event_subsystem_dir(call->system, d_events);
957
Steven Rostedt1473e442009-02-24 14:15:08 -0500958 call->dir = debugfs_create_dir(call->name, d_events);
959 if (!call->dir) {
960 pr_warning("Could not create debugfs "
961 "'%s' directory\n", call->name);
962 return -1;
963 }
964
Steven Rostedt6d723732009-04-10 14:53:50 -0400965 if (call->regfunc)
966 entry = trace_create_file("enable", 0644, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400967 enable);
Steven Rostedt1473e442009-02-24 14:15:08 -0500968
Peter Zijlstraaf6af302009-08-05 20:41:04 +0200969 if (call->id && call->profile_enable)
Steven Rostedt6d723732009-04-10 14:53:50 -0400970 entry = trace_create_file("id", 0444, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400971 id);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100972
Tom Zanussicf027f62009-03-22 03:30:39 -0500973 if (call->define_fields) {
Li Zefan14be96c2009-08-19 15:53:52 +0800974 ret = call->define_fields(call);
Tom Zanussicf027f62009-03-22 03:30:39 -0500975 if (ret < 0) {
976 pr_warning("Could not initialize trace point"
977 " events/%s\n", call->name);
978 return ret;
979 }
Steven Rostedt6d723732009-04-10 14:53:50 -0400980 entry = trace_create_file("filter", 0644, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400981 filter);
Tom Zanussicf027f62009-03-22 03:30:39 -0500982 }
983
Steven Rostedt981d0812009-03-02 13:53:59 -0500984 /* A trace may not want to export its format */
985 if (!call->show_format)
986 return 0;
987
Steven Rostedt6d723732009-04-10 14:53:50 -0400988 entry = trace_create_file("format", 0444, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400989 format);
Steven Rostedtfd994982009-02-28 02:41:25 -0500990
Steven Rostedt1473e442009-02-24 14:15:08 -0500991 return 0;
992}
993
Steven Rostedt6d723732009-04-10 14:53:50 -0400994#define for_each_event(event, start, end) \
995 for (event = start; \
996 (unsigned long)event < (unsigned long)end; \
997 event++)
998
Steven Rostedt61f919a2009-04-14 18:22:32 -0400999#ifdef CONFIG_MODULES
Steven Rostedt701970b2009-04-24 23:11:22 -04001000
1001static LIST_HEAD(ftrace_module_file_list);
1002
1003/*
1004 * Modules must own their file_operations to keep up with
1005 * reference counting.
1006 */
1007struct ftrace_module_file_ops {
1008 struct list_head list;
1009 struct module *mod;
1010 struct file_operations id;
1011 struct file_operations enable;
1012 struct file_operations format;
1013 struct file_operations filter;
1014};
1015
Frederic Weisbeckera2ca5e02009-08-06 07:32:21 +02001016static void remove_subsystem_dir(const char *name)
1017{
1018 struct event_subsystem *system;
1019
1020 if (strcmp(name, TRACE_SYSTEM) == 0)
1021 return;
1022
1023 list_for_each_entry(system, &event_subsystems, list) {
1024 if (strcmp(system->name, name) == 0) {
1025 if (!--system->nr_events) {
1026 struct event_filter *filter = system->filter;
1027
1028 debugfs_remove_recursive(system->entry);
1029 list_del(&system->list);
1030 if (filter) {
1031 kfree(filter->filter_string);
1032 kfree(filter);
1033 }
1034 kfree(system->name);
1035 kfree(system);
1036 }
1037 break;
1038 }
1039 }
1040}
1041
Steven Rostedt701970b2009-04-24 23:11:22 -04001042static struct ftrace_module_file_ops *
1043trace_create_file_ops(struct module *mod)
1044{
1045 struct ftrace_module_file_ops *file_ops;
1046
1047 /*
1048 * This is a bit of a PITA. To allow for correct reference
1049 * counting, modules must "own" their file_operations.
1050 * To do this, we allocate the file operations that will be
1051 * used in the event directory.
1052 */
1053
1054 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1055 if (!file_ops)
1056 return NULL;
1057
1058 file_ops->mod = mod;
1059
1060 file_ops->id = ftrace_event_id_fops;
1061 file_ops->id.owner = mod;
1062
1063 file_ops->enable = ftrace_enable_fops;
1064 file_ops->enable.owner = mod;
1065
1066 file_ops->filter = ftrace_event_filter_fops;
1067 file_ops->filter.owner = mod;
1068
1069 file_ops->format = ftrace_event_format_fops;
1070 file_ops->format.owner = mod;
1071
1072 list_add(&file_ops->list, &ftrace_module_file_list);
1073
1074 return file_ops;
1075}
1076
Steven Rostedt6d723732009-04-10 14:53:50 -04001077static void trace_module_add_events(struct module *mod)
1078{
Steven Rostedt701970b2009-04-24 23:11:22 -04001079 struct ftrace_module_file_ops *file_ops = NULL;
Steven Rostedt6d723732009-04-10 14:53:50 -04001080 struct ftrace_event_call *call, *start, *end;
1081 struct dentry *d_events;
Jason Baronf744bd52009-08-10 16:52:39 -04001082 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001083
1084 start = mod->trace_events;
1085 end = mod->trace_events + mod->num_trace_events;
1086
1087 if (start == end)
1088 return;
1089
1090 d_events = event_trace_events_dir();
1091 if (!d_events)
1092 return;
1093
1094 for_each_event(call, start, end) {
1095 /* The linker may leave blanks */
1096 if (!call->name)
1097 continue;
Jason Baronf744bd52009-08-10 16:52:39 -04001098 if (call->raw_init) {
1099 ret = call->raw_init();
1100 if (ret < 0) {
1101 if (ret != -ENOSYS)
1102 pr_warning("Could not initialize trace "
1103 "point events/%s\n", call->name);
1104 continue;
1105 }
1106 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001107 /*
1108 * This module has events, create file ops for this module
1109 * if not already done.
1110 */
1111 if (!file_ops) {
1112 file_ops = trace_create_file_ops(mod);
1113 if (!file_ops)
1114 return;
1115 }
Steven Rostedt6d723732009-04-10 14:53:50 -04001116 call->mod = mod;
1117 list_add(&call->list, &ftrace_events);
Steven Rostedt701970b2009-04-24 23:11:22 -04001118 event_create_dir(call, d_events,
1119 &file_ops->id, &file_ops->enable,
1120 &file_ops->filter, &file_ops->format);
Steven Rostedt6d723732009-04-10 14:53:50 -04001121 }
1122}
1123
1124static void trace_module_remove_events(struct module *mod)
1125{
Steven Rostedt701970b2009-04-24 23:11:22 -04001126 struct ftrace_module_file_ops *file_ops;
Steven Rostedt6d723732009-04-10 14:53:50 -04001127 struct ftrace_event_call *call, *p;
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001128 bool found = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001129
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001130 down_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001131 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1132 if (call->mod == mod) {
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001133 found = true;
Zhaolei0e907c92009-05-25 18:13:59 +08001134 ftrace_event_enable_disable(call, 0);
Steven Rostedt6d723732009-04-10 14:53:50 -04001135 if (call->event)
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001136 __unregister_ftrace_event(call->event);
Steven Rostedt6d723732009-04-10 14:53:50 -04001137 debugfs_remove_recursive(call->dir);
1138 list_del(&call->list);
Li Zefan2df75e42009-05-06 10:33:04 +08001139 trace_destroy_fields(call);
1140 destroy_preds(call);
Xiao Guangrongdc82ec98a2009-07-09 16:22:22 +08001141 remove_subsystem_dir(call->system);
Steven Rostedt6d723732009-04-10 14:53:50 -04001142 }
1143 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001144
1145 /* Now free the file_operations */
1146 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1147 if (file_ops->mod == mod)
1148 break;
1149 }
1150 if (&file_ops->list != &ftrace_module_file_list) {
1151 list_del(&file_ops->list);
1152 kfree(file_ops);
1153 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001154
1155 /*
1156 * It is safest to reset the ring buffer if the module being unloaded
1157 * registered any events.
1158 */
1159 if (found)
1160 tracing_reset_current_online_cpus();
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001161 up_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001162}
1163
Steven Rostedt61f919a2009-04-14 18:22:32 -04001164static int trace_module_notify(struct notifier_block *self,
1165 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001166{
1167 struct module *mod = data;
1168
1169 mutex_lock(&event_mutex);
1170 switch (val) {
1171 case MODULE_STATE_COMING:
1172 trace_module_add_events(mod);
1173 break;
1174 case MODULE_STATE_GOING:
1175 trace_module_remove_events(mod);
1176 break;
1177 }
1178 mutex_unlock(&event_mutex);
1179
1180 return 0;
1181}
Steven Rostedt61f919a2009-04-14 18:22:32 -04001182#else
1183static int trace_module_notify(struct notifier_block *self,
1184 unsigned long val, void *data)
1185{
1186 return 0;
1187}
1188#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001189
1190struct notifier_block trace_module_nb = {
1191 .notifier_call = trace_module_notify,
1192 .priority = 0,
1193};
1194
Steven Rostedta59fd602009-04-10 13:52:20 -04001195extern struct ftrace_event_call __start_ftrace_events[];
1196extern struct ftrace_event_call __stop_ftrace_events[];
1197
Li Zefan020e5f82009-07-01 10:47:05 +08001198static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1199
1200static __init int setup_trace_event(char *str)
1201{
1202 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1203 ring_buffer_expanded = 1;
1204 tracing_selftest_disabled = 1;
1205
1206 return 1;
1207}
1208__setup("trace_event=", setup_trace_event);
1209
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001210static __init int event_trace_init(void)
1211{
Steven Rostedta59fd602009-04-10 13:52:20 -04001212 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001213 struct dentry *d_tracer;
1214 struct dentry *entry;
Steven Rostedt1473e442009-02-24 14:15:08 -05001215 struct dentry *d_events;
Steven Rostedt6d723732009-04-10 14:53:50 -04001216 int ret;
Li Zefan020e5f82009-07-01 10:47:05 +08001217 char *buf = bootup_event_buf;
1218 char *token;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001219
1220 d_tracer = tracing_init_dentry();
1221 if (!d_tracer)
1222 return 0;
1223
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001224 entry = debugfs_create_file("available_events", 0444, d_tracer,
1225 (void *)&show_event_seq_ops,
1226 &ftrace_avail_fops);
1227 if (!entry)
1228 pr_warning("Could not create debugfs "
1229 "'available_events' entry\n");
1230
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001231 entry = debugfs_create_file("set_event", 0644, d_tracer,
1232 (void *)&show_set_event_seq_ops,
1233 &ftrace_set_event_fops);
1234 if (!entry)
1235 pr_warning("Could not create debugfs "
1236 "'set_event' entry\n");
1237
Steven Rostedt1473e442009-02-24 14:15:08 -05001238 d_events = event_trace_events_dir();
1239 if (!d_events)
1240 return 0;
1241
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001242 /* ring buffer internal formats */
1243 trace_create_file("header_page", 0444, d_events,
1244 ring_buffer_print_page_header,
1245 &ftrace_show_header_fops);
1246
1247 trace_create_file("header_event", 0444, d_events,
1248 ring_buffer_print_entry_header,
1249 &ftrace_show_header_fops);
1250
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001251 trace_create_file("enable", 0644, d_events,
Li Zefan8f31bfe2009-05-08 10:31:42 +08001252 NULL, &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001253
Steven Rostedt6d723732009-04-10 14:53:50 -04001254 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
Steven Rostedt1473e442009-02-24 14:15:08 -05001255 /* The linker may leave blanks */
1256 if (!call->name)
1257 continue;
Jason Baronf744bd52009-08-10 16:52:39 -04001258 if (call->raw_init) {
1259 ret = call->raw_init();
1260 if (ret < 0) {
1261 if (ret != -ENOSYS)
1262 pr_warning("Could not initialize trace "
1263 "point events/%s\n", call->name);
1264 continue;
1265 }
1266 }
Steven Rostedta59fd602009-04-10 13:52:20 -04001267 list_add(&call->list, &ftrace_events);
Steven Rostedt701970b2009-04-24 23:11:22 -04001268 event_create_dir(call, d_events, &ftrace_event_id_fops,
1269 &ftrace_enable_fops, &ftrace_event_filter_fops,
1270 &ftrace_event_format_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001271 }
1272
Li Zefan020e5f82009-07-01 10:47:05 +08001273 while (true) {
1274 token = strsep(&buf, ",");
1275
1276 if (!token)
1277 break;
1278 if (!*token)
1279 continue;
1280
1281 ret = ftrace_set_clr_event(token, 1);
1282 if (ret)
1283 pr_warning("Failed to enable trace event: %s\n", token);
1284 }
1285
Steven Rostedt6d723732009-04-10 14:53:50 -04001286 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08001287 if (ret)
Steven Rostedt6d723732009-04-10 14:53:50 -04001288 pr_warning("Failed to register trace events module notifier\n");
1289
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001290 return 0;
1291}
1292fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001293
1294#ifdef CONFIG_FTRACE_STARTUP_TEST
1295
1296static DEFINE_SPINLOCK(test_spinlock);
1297static DEFINE_SPINLOCK(test_spinlock_irq);
1298static DEFINE_MUTEX(test_mutex);
1299
1300static __init void test_work(struct work_struct *dummy)
1301{
1302 spin_lock(&test_spinlock);
1303 spin_lock_irq(&test_spinlock_irq);
1304 udelay(1);
1305 spin_unlock_irq(&test_spinlock_irq);
1306 spin_unlock(&test_spinlock);
1307
1308 mutex_lock(&test_mutex);
1309 msleep(1);
1310 mutex_unlock(&test_mutex);
1311}
1312
1313static __init int event_test_thread(void *unused)
1314{
1315 void *test_malloc;
1316
1317 test_malloc = kmalloc(1234, GFP_KERNEL);
1318 if (!test_malloc)
1319 pr_info("failed to kmalloc\n");
1320
1321 schedule_on_each_cpu(test_work);
1322
1323 kfree(test_malloc);
1324
1325 set_current_state(TASK_INTERRUPTIBLE);
1326 while (!kthread_should_stop())
1327 schedule();
1328
1329 return 0;
1330}
1331
1332/*
1333 * Do various things that may trigger events.
1334 */
1335static __init void event_test_stuff(void)
1336{
1337 struct task_struct *test_thread;
1338
1339 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1340 msleep(1);
1341 kthread_stop(test_thread);
1342}
1343
1344/*
1345 * For every trace event defined, we will test each trace point separately,
1346 * and then by groups, and finally all trace points.
1347 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001348static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04001349{
1350 struct ftrace_event_call *call;
1351 struct event_subsystem *system;
Steven Rostedte6187002009-04-15 13:36:40 -04001352 int ret;
1353
1354 pr_info("Running tests on trace events:\n");
1355
1356 list_for_each_entry(call, &ftrace_events, list) {
1357
1358 /* Only test those that have a regfunc */
1359 if (!call->regfunc)
1360 continue;
1361
1362 pr_info("Testing event %s: ", call->name);
1363
1364 /*
1365 * If an event is already enabled, someone is using
1366 * it and the self test should not be on.
1367 */
1368 if (call->enabled) {
1369 pr_warning("Enabled event during self test!\n");
1370 WARN_ON_ONCE(1);
1371 continue;
1372 }
1373
Zhaolei0e907c92009-05-25 18:13:59 +08001374 ftrace_event_enable_disable(call, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001375 event_test_stuff();
Zhaolei0e907c92009-05-25 18:13:59 +08001376 ftrace_event_enable_disable(call, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001377
1378 pr_cont("OK\n");
1379 }
1380
1381 /* Now test at the sub system level */
1382
1383 pr_info("Running tests on trace event systems:\n");
1384
1385 list_for_each_entry(system, &event_subsystems, list) {
1386
1387 /* the ftrace system is special, skip it */
1388 if (strcmp(system->name, "ftrace") == 0)
1389 continue;
1390
1391 pr_info("Testing event system %s: ", system->name);
1392
Li Zefan8f31bfe2009-05-08 10:31:42 +08001393 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001394 if (WARN_ON_ONCE(ret)) {
1395 pr_warning("error enabling system %s\n",
1396 system->name);
1397 continue;
1398 }
1399
1400 event_test_stuff();
1401
Li Zefan8f31bfe2009-05-08 10:31:42 +08001402 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001403 if (WARN_ON_ONCE(ret))
1404 pr_warning("error disabling system %s\n",
1405 system->name);
1406
1407 pr_cont("OK\n");
1408 }
1409
1410 /* Test with all events enabled */
1411
1412 pr_info("Running tests on all trace events:\n");
1413 pr_info("Testing all events: ");
1414
Li Zefan8f31bfe2009-05-08 10:31:42 +08001415 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001416 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04001417 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001418 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001419 }
1420
1421 event_test_stuff();
1422
1423 /* reset sysname */
Li Zefan8f31bfe2009-05-08 10:31:42 +08001424 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001425 if (WARN_ON_ONCE(ret)) {
1426 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001427 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001428 }
1429
1430 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001431}
1432
1433#ifdef CONFIG_FUNCTION_TRACER
1434
1435static DEFINE_PER_CPU(atomic_t, test_event_disable);
1436
1437static void
1438function_test_events_call(unsigned long ip, unsigned long parent_ip)
1439{
1440 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001441 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001442 struct ftrace_entry *entry;
1443 unsigned long flags;
1444 long disabled;
1445 int resched;
1446 int cpu;
1447 int pc;
1448
1449 pc = preempt_count();
1450 resched = ftrace_preempt_disable();
1451 cpu = raw_smp_processor_id();
1452 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1453
1454 if (disabled != 1)
1455 goto out;
1456
1457 local_save_flags(flags);
1458
Steven Rostedte77405a2009-09-02 14:17:06 -04001459 event = trace_current_buffer_lock_reserve(&buffer,
1460 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001461 flags, pc);
1462 if (!event)
1463 goto out;
1464 entry = ring_buffer_event_data(event);
1465 entry->ip = ip;
1466 entry->parent_ip = parent_ip;
1467
Steven Rostedte77405a2009-09-02 14:17:06 -04001468 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001469
1470 out:
1471 atomic_dec(&per_cpu(test_event_disable, cpu));
1472 ftrace_preempt_enable(resched);
1473}
1474
1475static struct ftrace_ops trace_ops __initdata =
1476{
1477 .func = function_test_events_call,
1478};
1479
1480static __init void event_trace_self_test_with_function(void)
1481{
1482 register_ftrace_function(&trace_ops);
1483 pr_info("Running tests again, along with the function tracer\n");
1484 event_trace_self_tests();
1485 unregister_ftrace_function(&trace_ops);
1486}
1487#else
1488static __init void event_trace_self_test_with_function(void)
1489{
1490}
1491#endif
1492
1493static __init int event_trace_self_tests_init(void)
1494{
Li Zefan020e5f82009-07-01 10:47:05 +08001495 if (!tracing_selftest_disabled) {
1496 event_trace_self_tests();
1497 event_trace_self_test_with_function();
1498 }
Steven Rostedte6187002009-04-15 13:36:40 -04001499
1500 return 0;
1501}
1502
Steven Rostedt28d20e22009-04-20 12:12:44 -04001503late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001504
1505#endif