blob: 53c8fd376a88a18ac2c77f5073f76194a6e26c1a [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
Steven Rostedt91729ef92009-03-02 15:03:01 -050020#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050021
Steven Rostedtb628b3e2009-02-27 23:32:58 -050022#define TRACE_SYSTEM "TRACE_SYSTEM"
23
Li Zefan20c89282009-05-06 10:33:45 +080024DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050025
Steven Rostedta59fd602009-04-10 13:52:20 -040026LIST_HEAD(ftrace_events);
27
Tom Zanussicf027f62009-03-22 03:30:39 -050028int trace_define_field(struct ftrace_event_call *call, char *type,
Tom Zanussia118e4d2009-04-28 03:04:53 -050029 char *name, int offset, int size, int is_signed)
Tom Zanussicf027f62009-03-22 03:30:39 -050030{
31 struct ftrace_event_field *field;
32
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010033 field = kzalloc(sizeof(*field), GFP_KERNEL);
Tom Zanussicf027f62009-03-22 03:30:39 -050034 if (!field)
35 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010036
Tom Zanussicf027f62009-03-22 03:30:39 -050037 field->name = kstrdup(name, GFP_KERNEL);
38 if (!field->name)
39 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010040
Tom Zanussicf027f62009-03-22 03:30:39 -050041 field->type = kstrdup(type, GFP_KERNEL);
42 if (!field->type)
43 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010044
Tom Zanussicf027f62009-03-22 03:30:39 -050045 field->offset = offset;
46 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -050047 field->is_signed = is_signed;
Tom Zanussicf027f62009-03-22 03:30:39 -050048 list_add(&field->link, &call->fields);
49
50 return 0;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010051
Tom Zanussicf027f62009-03-22 03:30:39 -050052err:
53 if (field) {
54 kfree(field->name);
55 kfree(field->type);
56 }
57 kfree(field);
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010058
Tom Zanussicf027f62009-03-22 03:30:39 -050059 return -ENOMEM;
60}
Steven Rostedt17c873e2009-04-10 18:12:50 -040061EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -050062
Li Zefan2df75e42009-05-06 10:33:04 +080063#ifdef CONFIG_MODULES
64
65static void trace_destroy_fields(struct ftrace_event_call *call)
66{
67 struct ftrace_event_field *field, *next;
68
69 list_for_each_entry_safe(field, next, &call->fields, link) {
70 list_del(&field->link);
71 kfree(field->type);
72 kfree(field->name);
73 kfree(field);
74 }
75}
76
77#endif /* CONFIG_MODULES */
78
Steven Rostedtfd994982009-02-28 02:41:25 -050079static void ftrace_event_enable_disable(struct ftrace_event_call *call,
80 int enable)
81{
Steven Rostedtfd994982009-02-28 02:41:25 -050082 switch (enable) {
83 case 0:
84 if (call->enabled) {
85 call->enabled = 0;
Zhaoleib11c53e2009-05-25 18:11:59 +080086 tracing_stop_cmdline_record();
Steven Rostedtfd994982009-02-28 02:41:25 -050087 call->unregfunc();
88 }
Steven Rostedtfd994982009-02-28 02:41:25 -050089 break;
90 case 1:
Steven Rostedtda4d0302009-03-09 17:14:30 -040091 if (!call->enabled) {
Steven Rostedtfd994982009-02-28 02:41:25 -050092 call->enabled = 1;
Zhaoleib11c53e2009-05-25 18:11:59 +080093 tracing_start_cmdline_record();
Steven Rostedtfd994982009-02-28 02:41:25 -050094 call->regfunc();
95 }
Steven Rostedtfd994982009-02-28 02:41:25 -050096 break;
97 }
98}
99
Zhaolei0e907c92009-05-25 18:13:59 +0800100static void ftrace_clear_events(void)
101{
102 struct ftrace_event_call *call;
103
104 mutex_lock(&event_mutex);
105 list_for_each_entry(call, &ftrace_events, list) {
106 ftrace_event_enable_disable(call, 0);
107 }
108 mutex_unlock(&event_mutex);
109}
110
Li Zefan8f31bfe2009-05-08 10:31:42 +0800111/*
112 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
113 */
114static int __ftrace_set_clr_event(const char *match, const char *sub,
115 const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500116{
Steven Rostedta59fd602009-04-10 13:52:20 -0400117 struct ftrace_event_call *call;
Steven Rostedt29f93942009-05-08 16:06:47 -0400118 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500119
Steven Rostedt11a241a2009-03-02 11:49:04 -0500120 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -0400121 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500122
Steven Rostedt40e26812009-03-10 11:32:40 -0400123 if (!call->name || !call->regfunc)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500124 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500125
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500126 if (match &&
127 strcmp(match, call->name) != 0 &&
128 strcmp(match, call->system) != 0)
129 continue;
130
131 if (sub && strcmp(sub, call->system) != 0)
132 continue;
133
134 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500135 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500136
Steven Rostedtfd994982009-02-28 02:41:25 -0500137 ftrace_event_enable_disable(call, set);
138
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500139 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500140 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500141 mutex_unlock(&event_mutex);
142
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500143 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500144}
145
Li Zefan8f31bfe2009-05-08 10:31:42 +0800146static int ftrace_set_clr_event(char *buf, int set)
147{
148 char *event = NULL, *sub = NULL, *match;
149
150 /*
151 * The buf format can be <subsystem>:<event-name>
152 * *:<event-name> means any event by that name.
153 * :<event-name> is the same.
154 *
155 * <subsystem>:* means all events in that subsystem
156 * <subsystem>: means the same.
157 *
158 * <name> (no ':') means all events in a subsystem with
159 * the name <name> or any event that matches <name>
160 */
161
162 match = strsep(&buf, ":");
163 if (buf) {
164 sub = match;
165 event = buf;
166 match = NULL;
167
168 if (!strlen(sub) || strcmp(sub, "*") == 0)
169 sub = NULL;
170 if (!strlen(event) || strcmp(event, "*") == 0)
171 event = NULL;
172 }
173
174 return __ftrace_set_clr_event(match, sub, event, set);
175}
176
Steven Rostedt4671c792009-05-08 16:27:41 -0400177/**
178 * trace_set_clr_event - enable or disable an event
179 * @system: system name to match (NULL for any system)
180 * @event: event name to match (NULL for all events, within system)
181 * @set: 1 to enable, 0 to disable
182 *
183 * This is a way for other parts of the kernel to enable or disable
184 * event recording.
185 *
186 * Returns 0 on success, -EINVAL if the parameters do not match any
187 * registered events.
188 */
189int trace_set_clr_event(const char *system, const char *event, int set)
190{
191 return __ftrace_set_clr_event(NULL, system, event, set);
192}
193
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500194/* 128 should be much more than enough */
195#define EVENT_BUF_SIZE 127
196
197static ssize_t
198ftrace_event_write(struct file *file, const char __user *ubuf,
199 size_t cnt, loff_t *ppos)
200{
201 size_t read = 0;
202 int i, set = 1;
203 ssize_t ret;
204 char *buf;
205 char ch;
206
207 if (!cnt || cnt < 0)
208 return 0;
209
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400210 ret = tracing_update_buffers();
211 if (ret < 0)
212 return ret;
213
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500214 ret = get_user(ch, ubuf++);
215 if (ret)
216 return ret;
217 read++;
218 cnt--;
219
220 /* skip white space */
221 while (cnt && isspace(ch)) {
222 ret = get_user(ch, ubuf++);
223 if (ret)
224 return ret;
225 read++;
226 cnt--;
227 }
228
229 /* Only white space found? */
230 if (isspace(ch)) {
231 file->f_pos += read;
232 ret = read;
233 return ret;
234 }
235
236 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
237 if (!buf)
238 return -ENOMEM;
239
240 if (cnt > EVENT_BUF_SIZE)
241 cnt = EVENT_BUF_SIZE;
242
243 i = 0;
244 while (cnt && !isspace(ch)) {
245 if (!i && ch == '!')
246 set = 0;
247 else
248 buf[i++] = ch;
249
250 ret = get_user(ch, ubuf++);
251 if (ret)
252 goto out_free;
253 read++;
254 cnt--;
255 }
256 buf[i] = 0;
257
258 file->f_pos += read;
259
260 ret = ftrace_set_clr_event(buf, set);
261 if (ret)
262 goto out_free;
263
264 ret = read;
265
266 out_free:
267 kfree(buf);
268
269 return ret;
270}
271
272static void *
273t_next(struct seq_file *m, void *v, loff_t *pos)
274{
Steven Rostedta59fd602009-04-10 13:52:20 -0400275 struct list_head *list = m->private;
276 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500277
278 (*pos)++;
279
Steven Rostedt40e26812009-03-10 11:32:40 -0400280 for (;;) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400281 if (list == &ftrace_events)
Steven Rostedt40e26812009-03-10 11:32:40 -0400282 return NULL;
283
Steven Rostedta59fd602009-04-10 13:52:20 -0400284 call = list_entry(list, struct ftrace_event_call, list);
285
Steven Rostedt40e26812009-03-10 11:32:40 -0400286 /*
287 * The ftrace subsystem is for showing formats only.
288 * They can not be enabled or disabled via the event files.
289 */
290 if (call->regfunc)
291 break;
292
Steven Rostedta59fd602009-04-10 13:52:20 -0400293 list = list->next;
Steven Rostedt40e26812009-03-10 11:32:40 -0400294 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500295
Steven Rostedta59fd602009-04-10 13:52:20 -0400296 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500297
298 return call;
299}
300
301static void *t_start(struct seq_file *m, loff_t *pos)
302{
Li Zefane1c7e2a2009-06-24 09:52:29 +0800303 struct ftrace_event_call *call = NULL;
304 loff_t l;
305
Li Zefan20c89282009-05-06 10:33:45 +0800306 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800307
308 m->private = ftrace_events.next;
309 for (l = 0; l <= *pos; ) {
310 call = t_next(m, NULL, &l);
311 if (!call)
312 break;
313 }
314 return call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500315}
316
317static void *
318s_next(struct seq_file *m, void *v, loff_t *pos)
319{
Steven Rostedta59fd602009-04-10 13:52:20 -0400320 struct list_head *list = m->private;
321 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500322
323 (*pos)++;
324
325 retry:
Steven Rostedta59fd602009-04-10 13:52:20 -0400326 if (list == &ftrace_events)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500327 return NULL;
328
Steven Rostedta59fd602009-04-10 13:52:20 -0400329 call = list_entry(list, struct ftrace_event_call, list);
330
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500331 if (!call->enabled) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400332 list = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500333 goto retry;
334 }
335
Steven Rostedta59fd602009-04-10 13:52:20 -0400336 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500337
338 return call;
339}
340
341static void *s_start(struct seq_file *m, loff_t *pos)
342{
Li Zefane1c7e2a2009-06-24 09:52:29 +0800343 struct ftrace_event_call *call = NULL;
344 loff_t l;
345
Li Zefan20c89282009-05-06 10:33:45 +0800346 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800347
348 m->private = ftrace_events.next;
349 for (l = 0; l <= *pos; ) {
350 call = s_next(m, NULL, &l);
351 if (!call)
352 break;
353 }
354 return call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500355}
356
357static int t_show(struct seq_file *m, void *v)
358{
359 struct ftrace_event_call *call = v;
360
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500361 if (strcmp(call->system, TRACE_SYSTEM) != 0)
362 seq_printf(m, "%s:", call->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500363 seq_printf(m, "%s\n", call->name);
364
365 return 0;
366}
367
368static void t_stop(struct seq_file *m, void *p)
369{
Li Zefan20c89282009-05-06 10:33:45 +0800370 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500371}
372
373static int
374ftrace_event_seq_open(struct inode *inode, struct file *file)
375{
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500376 const struct seq_operations *seq_ops;
377
378 if ((file->f_mode & FMODE_WRITE) &&
379 !(file->f_flags & O_APPEND))
380 ftrace_clear_events();
381
382 seq_ops = inode->i_private;
Li Zefan20c89282009-05-06 10:33:45 +0800383 return seq_open(file, seq_ops);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500384}
385
Steven Rostedt1473e442009-02-24 14:15:08 -0500386static ssize_t
387event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
388 loff_t *ppos)
389{
390 struct ftrace_event_call *call = filp->private_data;
391 char *buf;
392
Steven Rostedtda4d0302009-03-09 17:14:30 -0400393 if (call->enabled)
Steven Rostedt1473e442009-02-24 14:15:08 -0500394 buf = "1\n";
395 else
396 buf = "0\n";
397
398 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
399}
400
401static ssize_t
402event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
403 loff_t *ppos)
404{
405 struct ftrace_event_call *call = filp->private_data;
406 char buf[64];
407 unsigned long val;
408 int ret;
409
410 if (cnt >= sizeof(buf))
411 return -EINVAL;
412
413 if (copy_from_user(&buf, ubuf, cnt))
414 return -EFAULT;
415
416 buf[cnt] = 0;
417
418 ret = strict_strtoul(buf, 10, &val);
419 if (ret < 0)
420 return ret;
421
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400422 ret = tracing_update_buffers();
423 if (ret < 0)
424 return ret;
425
Steven Rostedt1473e442009-02-24 14:15:08 -0500426 switch (val) {
427 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500428 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500429 mutex_lock(&event_mutex);
Steven Rostedtfd994982009-02-28 02:41:25 -0500430 ftrace_event_enable_disable(call, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500431 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500432 break;
433
434 default:
435 return -EINVAL;
436 }
437
438 *ppos += cnt;
439
440 return cnt;
441}
442
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400443static ssize_t
444system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
445 loff_t *ppos)
446{
Li Zefanc142b152009-05-08 10:32:05 +0800447 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400448 const char *system = filp->private_data;
449 struct ftrace_event_call *call;
450 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800451 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400452 int ret;
453
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400454 mutex_lock(&event_mutex);
455 list_for_each_entry(call, &ftrace_events, list) {
456 if (!call->name || !call->regfunc)
457 continue;
458
Li Zefan8f31bfe2009-05-08 10:31:42 +0800459 if (system && strcmp(call->system, system) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400460 continue;
461
462 /*
463 * We need to find out if all the events are set
464 * or if all events or cleared, or if we have
465 * a mixture.
466 */
Li Zefanc142b152009-05-08 10:32:05 +0800467 set |= (1 << !!call->enabled);
468
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400469 /*
470 * If we have a mixture, no need to look further.
471 */
Li Zefanc142b152009-05-08 10:32:05 +0800472 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400473 break;
474 }
475 mutex_unlock(&event_mutex);
476
Li Zefanc142b152009-05-08 10:32:05 +0800477 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400478 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400479
480 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
481
482 return ret;
483}
484
485static ssize_t
486system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
487 loff_t *ppos)
488{
489 const char *system = filp->private_data;
490 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400491 char buf[64];
492 ssize_t ret;
493
494 if (cnt >= sizeof(buf))
495 return -EINVAL;
496
497 if (copy_from_user(&buf, ubuf, cnt))
498 return -EFAULT;
499
500 buf[cnt] = 0;
501
502 ret = strict_strtoul(buf, 10, &val);
503 if (ret < 0)
504 return ret;
505
506 ret = tracing_update_buffers();
507 if (ret < 0)
508 return ret;
509
Li Zefan8f31bfe2009-05-08 10:31:42 +0800510 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400511 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400512
Li Zefan8f31bfe2009-05-08 10:31:42 +0800513 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400514 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800515 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400516
517 ret = cnt;
518
Li Zefan8f31bfe2009-05-08 10:31:42 +0800519out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400520 *ppos += cnt;
521
522 return ret;
523}
524
Steven Rostedt75db37d2009-03-26 11:43:36 -0400525extern char *__bad_type_size(void);
526
Steven Rostedt91729ef92009-03-02 15:03:01 -0500527#undef FIELD
Steven Rostedt156b5f12009-03-06 10:50:53 -0500528#define FIELD(type, name) \
Steven Rostedt75db37d2009-03-26 11:43:36 -0400529 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
Tom Zanussicf027f62009-03-22 03:30:39 -0500530 #type, "common_" #name, offsetof(typeof(field), name), \
531 sizeof(field.name)
Steven Rostedt91729ef92009-03-02 15:03:01 -0500532
533static int trace_write_header(struct trace_seq *s)
534{
535 struct trace_entry field;
536
537 /* struct trace_entry */
538 return trace_seq_printf(s,
Steven Rostedtce8eb2b2009-03-10 10:14:35 -0400539 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
540 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
541 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
542 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
543 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
Steven Rostedt91729ef92009-03-02 15:03:01 -0500544 "\n",
Steven Rostedt89ec0de2009-03-26 11:03:29 -0400545 FIELD(unsigned short, type),
Steven Rostedt91729ef92009-03-02 15:03:01 -0500546 FIELD(unsigned char, flags),
547 FIELD(unsigned char, preempt_count),
548 FIELD(int, pid),
549 FIELD(int, tgid));
550}
Steven Rostedtda4d0302009-03-09 17:14:30 -0400551
Steven Rostedt981d0812009-03-02 13:53:59 -0500552static ssize_t
553event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
554 loff_t *ppos)
555{
556 struct ftrace_event_call *call = filp->private_data;
557 struct trace_seq *s;
558 char *buf;
559 int r;
560
Tom Zanussic269fc82009-03-17 01:20:59 -0500561 if (*ppos)
562 return 0;
563
Steven Rostedt981d0812009-03-02 13:53:59 -0500564 s = kmalloc(sizeof(*s), GFP_KERNEL);
565 if (!s)
566 return -ENOMEM;
567
568 trace_seq_init(s);
569
Steven Rostedtc5e4e192009-03-02 15:10:02 -0500570 /* If any of the first writes fail, so will the show_format. */
571
572 trace_seq_printf(s, "name: %s\n", call->name);
573 trace_seq_printf(s, "ID: %d\n", call->id);
574 trace_seq_printf(s, "format:\n");
Steven Rostedt91729ef92009-03-02 15:03:01 -0500575 trace_write_header(s);
576
Steven Rostedt981d0812009-03-02 13:53:59 -0500577 r = call->show_format(s);
578 if (!r) {
579 /*
580 * ug! The format output is bigger than a PAGE!!
581 */
582 buf = "FORMAT TOO BIG\n";
583 r = simple_read_from_buffer(ubuf, cnt, ppos,
584 buf, strlen(buf));
585 goto out;
586 }
587
588 r = simple_read_from_buffer(ubuf, cnt, ppos,
589 s->buffer, s->len);
590 out:
591 kfree(s);
592 return r;
593}
594
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100595static ssize_t
596event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
597{
598 struct ftrace_event_call *call = filp->private_data;
599 struct trace_seq *s;
600 int r;
601
602 if (*ppos)
603 return 0;
604
605 s = kmalloc(sizeof(*s), GFP_KERNEL);
606 if (!s)
607 return -ENOMEM;
608
609 trace_seq_init(s);
610 trace_seq_printf(s, "%d\n", call->id);
611
612 r = simple_read_from_buffer(ubuf, cnt, ppos,
613 s->buffer, s->len);
614 kfree(s);
615 return r;
616}
617
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500618static ssize_t
619event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
620 loff_t *ppos)
621{
622 struct ftrace_event_call *call = filp->private_data;
623 struct trace_seq *s;
624 int r;
625
626 if (*ppos)
627 return 0;
628
629 s = kmalloc(sizeof(*s), GFP_KERNEL);
630 if (!s)
631 return -ENOMEM;
632
633 trace_seq_init(s);
634
Tom Zanussi8b372562009-04-28 03:04:59 -0500635 print_event_filter(call, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500636 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500637
638 kfree(s);
639
640 return r;
641}
642
643static ssize_t
644event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
645 loff_t *ppos)
646{
647 struct ftrace_event_call *call = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500648 char *buf;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500649 int err;
650
Tom Zanussi8b372562009-04-28 03:04:59 -0500651 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500652 return -EINVAL;
653
Tom Zanussi8b372562009-04-28 03:04:59 -0500654 buf = (char *)__get_free_page(GFP_TEMPORARY);
655 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500656 return -ENOMEM;
657
Tom Zanussi8b372562009-04-28 03:04:59 -0500658 if (copy_from_user(buf, ubuf, cnt)) {
659 free_page((unsigned long) buf);
660 return -EFAULT;
661 }
662 buf[cnt] = '\0';
663
664 err = apply_event_filter(call, buf);
665 free_page((unsigned long) buf);
666 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500667 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500668
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500669 *ppos += cnt;
670
671 return cnt;
672}
673
Tom Zanussicfb180f2009-03-22 03:31:17 -0500674static ssize_t
675subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
676 loff_t *ppos)
677{
678 struct event_subsystem *system = filp->private_data;
679 struct trace_seq *s;
680 int r;
681
682 if (*ppos)
683 return 0;
684
685 s = kmalloc(sizeof(*s), GFP_KERNEL);
686 if (!s)
687 return -ENOMEM;
688
689 trace_seq_init(s);
690
Tom Zanussi8b372562009-04-28 03:04:59 -0500691 print_subsystem_event_filter(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500692 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500693
694 kfree(s);
695
696 return r;
697}
698
699static ssize_t
700subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
701 loff_t *ppos)
702{
703 struct event_subsystem *system = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500704 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500705 int err;
706
Tom Zanussi8b372562009-04-28 03:04:59 -0500707 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500708 return -EINVAL;
709
Tom Zanussi8b372562009-04-28 03:04:59 -0500710 buf = (char *)__get_free_page(GFP_TEMPORARY);
711 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500712 return -ENOMEM;
713
Tom Zanussi8b372562009-04-28 03:04:59 -0500714 if (copy_from_user(buf, ubuf, cnt)) {
715 free_page((unsigned long) buf);
716 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500717 }
Tom Zanussi8b372562009-04-28 03:04:59 -0500718 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -0500719
Tom Zanussi8b372562009-04-28 03:04:59 -0500720 err = apply_subsystem_event_filter(system, buf);
721 free_page((unsigned long) buf);
722 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +0800723 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500724
725 *ppos += cnt;
726
727 return cnt;
728}
729
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400730static ssize_t
731show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
732{
733 int (*func)(struct trace_seq *s) = filp->private_data;
734 struct trace_seq *s;
735 int r;
736
737 if (*ppos)
738 return 0;
739
740 s = kmalloc(sizeof(*s), GFP_KERNEL);
741 if (!s)
742 return -ENOMEM;
743
744 trace_seq_init(s);
745
746 func(s);
747 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
748
749 kfree(s);
750
751 return r;
752}
753
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500754static const struct seq_operations show_event_seq_ops = {
755 .start = t_start,
756 .next = t_next,
757 .show = t_show,
758 .stop = t_stop,
759};
760
761static const struct seq_operations show_set_event_seq_ops = {
762 .start = s_start,
763 .next = s_next,
764 .show = t_show,
765 .stop = t_stop,
766};
767
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400768static const struct file_operations ftrace_avail_fops = {
769 .open = ftrace_event_seq_open,
770 .read = seq_read,
771 .llseek = seq_lseek,
772 .release = seq_release,
773};
774
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500775static const struct file_operations ftrace_set_event_fops = {
776 .open = ftrace_event_seq_open,
777 .read = seq_read,
778 .write = ftrace_event_write,
779 .llseek = seq_lseek,
780 .release = seq_release,
781};
782
Steven Rostedt1473e442009-02-24 14:15:08 -0500783static const struct file_operations ftrace_enable_fops = {
784 .open = tracing_open_generic,
785 .read = event_enable_read,
786 .write = event_enable_write,
787};
788
Steven Rostedt981d0812009-03-02 13:53:59 -0500789static const struct file_operations ftrace_event_format_fops = {
790 .open = tracing_open_generic,
791 .read = event_format_read,
792};
793
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100794static const struct file_operations ftrace_event_id_fops = {
795 .open = tracing_open_generic,
796 .read = event_id_read,
797};
798
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500799static const struct file_operations ftrace_event_filter_fops = {
800 .open = tracing_open_generic,
801 .read = event_filter_read,
802 .write = event_filter_write,
803};
804
Tom Zanussicfb180f2009-03-22 03:31:17 -0500805static const struct file_operations ftrace_subsystem_filter_fops = {
806 .open = tracing_open_generic,
807 .read = subsystem_filter_read,
808 .write = subsystem_filter_write,
809};
810
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400811static const struct file_operations ftrace_system_enable_fops = {
812 .open = tracing_open_generic,
813 .read = system_enable_read,
814 .write = system_enable_write,
815};
816
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400817static const struct file_operations ftrace_show_header_fops = {
818 .open = tracing_open_generic,
819 .read = show_header,
820};
821
Steven Rostedt1473e442009-02-24 14:15:08 -0500822static struct dentry *event_trace_events_dir(void)
823{
824 static struct dentry *d_tracer;
825 static struct dentry *d_events;
826
827 if (d_events)
828 return d_events;
829
830 d_tracer = tracing_init_dentry();
831 if (!d_tracer)
832 return NULL;
833
834 d_events = debugfs_create_dir("events", d_tracer);
835 if (!d_events)
836 pr_warning("Could not create debugfs "
837 "'events' directory\n");
838
839 return d_events;
840}
841
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500842static LIST_HEAD(event_subsystems);
843
844static struct dentry *
845event_subsystem_dir(const char *name, struct dentry *d_events)
846{
847 struct event_subsystem *system;
Tom Zanussie1112b42009-03-31 00:48:49 -0500848 struct dentry *entry;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500849
850 /* First see if we did not already create this dir */
851 list_for_each_entry(system, &event_subsystems, list) {
852 if (strcmp(system->name, name) == 0)
853 return system->entry;
854 }
855
856 /* need to create new entry */
857 system = kmalloc(sizeof(*system), GFP_KERNEL);
858 if (!system) {
859 pr_warning("No memory to create event subsystem %s\n",
860 name);
861 return d_events;
862 }
863
864 system->entry = debugfs_create_dir(name, d_events);
865 if (!system->entry) {
866 pr_warning("Could not create event subsystem %s\n",
867 name);
868 kfree(system);
869 return d_events;
870 }
871
Steven Rostedt6d723732009-04-10 14:53:50 -0400872 system->name = kstrdup(name, GFP_KERNEL);
873 if (!system->name) {
874 debugfs_remove(system->entry);
875 kfree(system);
876 return d_events;
877 }
878
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500879 list_add(&system->list, &event_subsystems);
880
Tom Zanussi30e673b2009-04-28 03:04:47 -0500881 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500882
Tom Zanussi8b372562009-04-28 03:04:59 -0500883 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
884 if (!system->filter) {
885 pr_warning("Could not allocate filter for subsystem "
886 "'%s'\n", name);
887 return system->entry;
888 }
889
Tom Zanussie1112b42009-03-31 00:48:49 -0500890 entry = debugfs_create_file("filter", 0644, system->entry, system,
891 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -0500892 if (!entry) {
893 kfree(system->filter);
894 system->filter = NULL;
Tom Zanussie1112b42009-03-31 00:48:49 -0500895 pr_warning("Could not create debugfs "
896 "'%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -0500897 }
Tom Zanussie1112b42009-03-31 00:48:49 -0500898
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400899 entry = trace_create_file("enable", 0644, system->entry,
900 (void *)system->name,
901 &ftrace_system_enable_fops);
902
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500903 return system->entry;
904}
905
Steven Rostedt1473e442009-02-24 14:15:08 -0500906static int
Steven Rostedt701970b2009-04-24 23:11:22 -0400907event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
908 const struct file_operations *id,
909 const struct file_operations *enable,
910 const struct file_operations *filter,
911 const struct file_operations *format)
Steven Rostedt1473e442009-02-24 14:15:08 -0500912{
913 struct dentry *entry;
Steven Rostedtfd994982009-02-28 02:41:25 -0500914 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -0500915
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500916 /*
917 * If the trace point header did not define TRACE_SYSTEM
918 * then the system would be called "TRACE_SYSTEM".
919 */
Steven Rostedt6d723732009-04-10 14:53:50 -0400920 if (strcmp(call->system, TRACE_SYSTEM) != 0)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500921 d_events = event_subsystem_dir(call->system, d_events);
922
Steven Rostedtfd994982009-02-28 02:41:25 -0500923 if (call->raw_init) {
924 ret = call->raw_init();
925 if (ret < 0) {
926 pr_warning("Could not initialize trace point"
927 " events/%s\n", call->name);
928 return ret;
929 }
930 }
931
Steven Rostedt1473e442009-02-24 14:15:08 -0500932 call->dir = debugfs_create_dir(call->name, d_events);
933 if (!call->dir) {
934 pr_warning("Could not create debugfs "
935 "'%s' directory\n", call->name);
936 return -1;
937 }
938
Steven Rostedt6d723732009-04-10 14:53:50 -0400939 if (call->regfunc)
940 entry = trace_create_file("enable", 0644, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400941 enable);
Steven Rostedt1473e442009-02-24 14:15:08 -0500942
Steven Rostedt6d723732009-04-10 14:53:50 -0400943 if (call->id)
944 entry = trace_create_file("id", 0444, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400945 id);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100946
Tom Zanussicf027f62009-03-22 03:30:39 -0500947 if (call->define_fields) {
948 ret = call->define_fields();
949 if (ret < 0) {
950 pr_warning("Could not initialize trace point"
951 " events/%s\n", call->name);
952 return ret;
953 }
Steven Rostedt6d723732009-04-10 14:53:50 -0400954 entry = trace_create_file("filter", 0644, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400955 filter);
Tom Zanussicf027f62009-03-22 03:30:39 -0500956 }
957
Steven Rostedt981d0812009-03-02 13:53:59 -0500958 /* A trace may not want to export its format */
959 if (!call->show_format)
960 return 0;
961
Steven Rostedt6d723732009-04-10 14:53:50 -0400962 entry = trace_create_file("format", 0444, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400963 format);
Steven Rostedtfd994982009-02-28 02:41:25 -0500964
Steven Rostedt1473e442009-02-24 14:15:08 -0500965 return 0;
966}
967
Steven Rostedt6d723732009-04-10 14:53:50 -0400968#define for_each_event(event, start, end) \
969 for (event = start; \
970 (unsigned long)event < (unsigned long)end; \
971 event++)
972
Steven Rostedt61f919a2009-04-14 18:22:32 -0400973#ifdef CONFIG_MODULES
Steven Rostedt701970b2009-04-24 23:11:22 -0400974
975static LIST_HEAD(ftrace_module_file_list);
976
977/*
978 * Modules must own their file_operations to keep up with
979 * reference counting.
980 */
981struct ftrace_module_file_ops {
982 struct list_head list;
983 struct module *mod;
984 struct file_operations id;
985 struct file_operations enable;
986 struct file_operations format;
987 struct file_operations filter;
988};
989
990static struct ftrace_module_file_ops *
991trace_create_file_ops(struct module *mod)
992{
993 struct ftrace_module_file_ops *file_ops;
994
995 /*
996 * This is a bit of a PITA. To allow for correct reference
997 * counting, modules must "own" their file_operations.
998 * To do this, we allocate the file operations that will be
999 * used in the event directory.
1000 */
1001
1002 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1003 if (!file_ops)
1004 return NULL;
1005
1006 file_ops->mod = mod;
1007
1008 file_ops->id = ftrace_event_id_fops;
1009 file_ops->id.owner = mod;
1010
1011 file_ops->enable = ftrace_enable_fops;
1012 file_ops->enable.owner = mod;
1013
1014 file_ops->filter = ftrace_event_filter_fops;
1015 file_ops->filter.owner = mod;
1016
1017 file_ops->format = ftrace_event_format_fops;
1018 file_ops->format.owner = mod;
1019
1020 list_add(&file_ops->list, &ftrace_module_file_list);
1021
1022 return file_ops;
1023}
1024
Steven Rostedt6d723732009-04-10 14:53:50 -04001025static void trace_module_add_events(struct module *mod)
1026{
Steven Rostedt701970b2009-04-24 23:11:22 -04001027 struct ftrace_module_file_ops *file_ops = NULL;
Steven Rostedt6d723732009-04-10 14:53:50 -04001028 struct ftrace_event_call *call, *start, *end;
1029 struct dentry *d_events;
1030
1031 start = mod->trace_events;
1032 end = mod->trace_events + mod->num_trace_events;
1033
1034 if (start == end)
1035 return;
1036
1037 d_events = event_trace_events_dir();
1038 if (!d_events)
1039 return;
1040
1041 for_each_event(call, start, end) {
1042 /* The linker may leave blanks */
1043 if (!call->name)
1044 continue;
Steven Rostedt701970b2009-04-24 23:11:22 -04001045
1046 /*
1047 * This module has events, create file ops for this module
1048 * if not already done.
1049 */
1050 if (!file_ops) {
1051 file_ops = trace_create_file_ops(mod);
1052 if (!file_ops)
1053 return;
1054 }
Steven Rostedt6d723732009-04-10 14:53:50 -04001055 call->mod = mod;
1056 list_add(&call->list, &ftrace_events);
Steven Rostedt701970b2009-04-24 23:11:22 -04001057 event_create_dir(call, d_events,
1058 &file_ops->id, &file_ops->enable,
1059 &file_ops->filter, &file_ops->format);
Steven Rostedt6d723732009-04-10 14:53:50 -04001060 }
1061}
1062
1063static void trace_module_remove_events(struct module *mod)
1064{
Steven Rostedt701970b2009-04-24 23:11:22 -04001065 struct ftrace_module_file_ops *file_ops;
Steven Rostedt6d723732009-04-10 14:53:50 -04001066 struct ftrace_event_call *call, *p;
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001067 bool found = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001068
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001069 down_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001070 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1071 if (call->mod == mod) {
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001072 found = true;
Zhaolei0e907c92009-05-25 18:13:59 +08001073 ftrace_event_enable_disable(call, 0);
Steven Rostedt6d723732009-04-10 14:53:50 -04001074 if (call->event)
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001075 __unregister_ftrace_event(call->event);
Steven Rostedt6d723732009-04-10 14:53:50 -04001076 debugfs_remove_recursive(call->dir);
1077 list_del(&call->list);
Li Zefan2df75e42009-05-06 10:33:04 +08001078 trace_destroy_fields(call);
1079 destroy_preds(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001080 }
1081 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001082
1083 /* Now free the file_operations */
1084 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1085 if (file_ops->mod == mod)
1086 break;
1087 }
1088 if (&file_ops->list != &ftrace_module_file_list) {
1089 list_del(&file_ops->list);
1090 kfree(file_ops);
1091 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001092
1093 /*
1094 * It is safest to reset the ring buffer if the module being unloaded
1095 * registered any events.
1096 */
1097 if (found)
1098 tracing_reset_current_online_cpus();
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001099 up_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001100}
1101
Steven Rostedt61f919a2009-04-14 18:22:32 -04001102static int trace_module_notify(struct notifier_block *self,
1103 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001104{
1105 struct module *mod = data;
1106
1107 mutex_lock(&event_mutex);
1108 switch (val) {
1109 case MODULE_STATE_COMING:
1110 trace_module_add_events(mod);
1111 break;
1112 case MODULE_STATE_GOING:
1113 trace_module_remove_events(mod);
1114 break;
1115 }
1116 mutex_unlock(&event_mutex);
1117
1118 return 0;
1119}
Steven Rostedt61f919a2009-04-14 18:22:32 -04001120#else
1121static int trace_module_notify(struct notifier_block *self,
1122 unsigned long val, void *data)
1123{
1124 return 0;
1125}
1126#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001127
1128struct notifier_block trace_module_nb = {
1129 .notifier_call = trace_module_notify,
1130 .priority = 0,
1131};
1132
Steven Rostedta59fd602009-04-10 13:52:20 -04001133extern struct ftrace_event_call __start_ftrace_events[];
1134extern struct ftrace_event_call __stop_ftrace_events[];
1135
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001136static __init int event_trace_init(void)
1137{
Steven Rostedta59fd602009-04-10 13:52:20 -04001138 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001139 struct dentry *d_tracer;
1140 struct dentry *entry;
Steven Rostedt1473e442009-02-24 14:15:08 -05001141 struct dentry *d_events;
Steven Rostedt6d723732009-04-10 14:53:50 -04001142 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001143
1144 d_tracer = tracing_init_dentry();
1145 if (!d_tracer)
1146 return 0;
1147
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001148 entry = debugfs_create_file("available_events", 0444, d_tracer,
1149 (void *)&show_event_seq_ops,
1150 &ftrace_avail_fops);
1151 if (!entry)
1152 pr_warning("Could not create debugfs "
1153 "'available_events' entry\n");
1154
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001155 entry = debugfs_create_file("set_event", 0644, d_tracer,
1156 (void *)&show_set_event_seq_ops,
1157 &ftrace_set_event_fops);
1158 if (!entry)
1159 pr_warning("Could not create debugfs "
1160 "'set_event' entry\n");
1161
Steven Rostedt1473e442009-02-24 14:15:08 -05001162 d_events = event_trace_events_dir();
1163 if (!d_events)
1164 return 0;
1165
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001166 /* ring buffer internal formats */
1167 trace_create_file("header_page", 0444, d_events,
1168 ring_buffer_print_page_header,
1169 &ftrace_show_header_fops);
1170
1171 trace_create_file("header_event", 0444, d_events,
1172 ring_buffer_print_entry_header,
1173 &ftrace_show_header_fops);
1174
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001175 trace_create_file("enable", 0644, d_events,
Li Zefan8f31bfe2009-05-08 10:31:42 +08001176 NULL, &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001177
Steven Rostedt6d723732009-04-10 14:53:50 -04001178 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
Steven Rostedt1473e442009-02-24 14:15:08 -05001179 /* The linker may leave blanks */
1180 if (!call->name)
1181 continue;
Steven Rostedta59fd602009-04-10 13:52:20 -04001182 list_add(&call->list, &ftrace_events);
Steven Rostedt701970b2009-04-24 23:11:22 -04001183 event_create_dir(call, d_events, &ftrace_event_id_fops,
1184 &ftrace_enable_fops, &ftrace_event_filter_fops,
1185 &ftrace_event_format_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001186 }
1187
Steven Rostedt6d723732009-04-10 14:53:50 -04001188 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08001189 if (ret)
Steven Rostedt6d723732009-04-10 14:53:50 -04001190 pr_warning("Failed to register trace events module notifier\n");
1191
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001192 return 0;
1193}
1194fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001195
1196#ifdef CONFIG_FTRACE_STARTUP_TEST
1197
1198static DEFINE_SPINLOCK(test_spinlock);
1199static DEFINE_SPINLOCK(test_spinlock_irq);
1200static DEFINE_MUTEX(test_mutex);
1201
1202static __init void test_work(struct work_struct *dummy)
1203{
1204 spin_lock(&test_spinlock);
1205 spin_lock_irq(&test_spinlock_irq);
1206 udelay(1);
1207 spin_unlock_irq(&test_spinlock_irq);
1208 spin_unlock(&test_spinlock);
1209
1210 mutex_lock(&test_mutex);
1211 msleep(1);
1212 mutex_unlock(&test_mutex);
1213}
1214
1215static __init int event_test_thread(void *unused)
1216{
1217 void *test_malloc;
1218
1219 test_malloc = kmalloc(1234, GFP_KERNEL);
1220 if (!test_malloc)
1221 pr_info("failed to kmalloc\n");
1222
1223 schedule_on_each_cpu(test_work);
1224
1225 kfree(test_malloc);
1226
1227 set_current_state(TASK_INTERRUPTIBLE);
1228 while (!kthread_should_stop())
1229 schedule();
1230
1231 return 0;
1232}
1233
1234/*
1235 * Do various things that may trigger events.
1236 */
1237static __init void event_test_stuff(void)
1238{
1239 struct task_struct *test_thread;
1240
1241 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1242 msleep(1);
1243 kthread_stop(test_thread);
1244}
1245
1246/*
1247 * For every trace event defined, we will test each trace point separately,
1248 * and then by groups, and finally all trace points.
1249 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001250static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04001251{
1252 struct ftrace_event_call *call;
1253 struct event_subsystem *system;
Steven Rostedte6187002009-04-15 13:36:40 -04001254 int ret;
1255
1256 pr_info("Running tests on trace events:\n");
1257
1258 list_for_each_entry(call, &ftrace_events, list) {
1259
1260 /* Only test those that have a regfunc */
1261 if (!call->regfunc)
1262 continue;
1263
1264 pr_info("Testing event %s: ", call->name);
1265
1266 /*
1267 * If an event is already enabled, someone is using
1268 * it and the self test should not be on.
1269 */
1270 if (call->enabled) {
1271 pr_warning("Enabled event during self test!\n");
1272 WARN_ON_ONCE(1);
1273 continue;
1274 }
1275
Zhaolei0e907c92009-05-25 18:13:59 +08001276 ftrace_event_enable_disable(call, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001277 event_test_stuff();
Zhaolei0e907c92009-05-25 18:13:59 +08001278 ftrace_event_enable_disable(call, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001279
1280 pr_cont("OK\n");
1281 }
1282
1283 /* Now test at the sub system level */
1284
1285 pr_info("Running tests on trace event systems:\n");
1286
1287 list_for_each_entry(system, &event_subsystems, list) {
1288
1289 /* the ftrace system is special, skip it */
1290 if (strcmp(system->name, "ftrace") == 0)
1291 continue;
1292
1293 pr_info("Testing event system %s: ", system->name);
1294
Li Zefan8f31bfe2009-05-08 10:31:42 +08001295 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001296 if (WARN_ON_ONCE(ret)) {
1297 pr_warning("error enabling system %s\n",
1298 system->name);
1299 continue;
1300 }
1301
1302 event_test_stuff();
1303
Li Zefan8f31bfe2009-05-08 10:31:42 +08001304 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001305 if (WARN_ON_ONCE(ret))
1306 pr_warning("error disabling system %s\n",
1307 system->name);
1308
1309 pr_cont("OK\n");
1310 }
1311
1312 /* Test with all events enabled */
1313
1314 pr_info("Running tests on all trace events:\n");
1315 pr_info("Testing all events: ");
1316
Li Zefan8f31bfe2009-05-08 10:31:42 +08001317 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001318 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04001319 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001320 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001321 }
1322
1323 event_test_stuff();
1324
1325 /* reset sysname */
Li Zefan8f31bfe2009-05-08 10:31:42 +08001326 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001327 if (WARN_ON_ONCE(ret)) {
1328 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001329 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001330 }
1331
1332 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001333}
1334
1335#ifdef CONFIG_FUNCTION_TRACER
1336
1337static DEFINE_PER_CPU(atomic_t, test_event_disable);
1338
1339static void
1340function_test_events_call(unsigned long ip, unsigned long parent_ip)
1341{
1342 struct ring_buffer_event *event;
1343 struct ftrace_entry *entry;
1344 unsigned long flags;
1345 long disabled;
1346 int resched;
1347 int cpu;
1348 int pc;
1349
1350 pc = preempt_count();
1351 resched = ftrace_preempt_disable();
1352 cpu = raw_smp_processor_id();
1353 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1354
1355 if (disabled != 1)
1356 goto out;
1357
1358 local_save_flags(flags);
1359
1360 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1361 flags, pc);
1362 if (!event)
1363 goto out;
1364 entry = ring_buffer_event_data(event);
1365 entry->ip = ip;
1366 entry->parent_ip = parent_ip;
1367
Steven Rostedtcb4764a2009-04-20 18:16:44 -04001368 trace_nowake_buffer_unlock_commit(event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001369
1370 out:
1371 atomic_dec(&per_cpu(test_event_disable, cpu));
1372 ftrace_preempt_enable(resched);
1373}
1374
1375static struct ftrace_ops trace_ops __initdata =
1376{
1377 .func = function_test_events_call,
1378};
1379
1380static __init void event_trace_self_test_with_function(void)
1381{
1382 register_ftrace_function(&trace_ops);
1383 pr_info("Running tests again, along with the function tracer\n");
1384 event_trace_self_tests();
1385 unregister_ftrace_function(&trace_ops);
1386}
1387#else
1388static __init void event_trace_self_test_with_function(void)
1389{
1390}
1391#endif
1392
1393static __init int event_trace_self_tests_init(void)
1394{
1395
1396 event_trace_self_tests();
1397
1398 event_trace_self_test_with_function();
Steven Rostedte6187002009-04-15 13:36:40 -04001399
1400 return 0;
1401}
1402
Steven Rostedt28d20e22009-04-20 12:12:44 -04001403late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001404
1405#endif