blob: acc0f55742c321c15c332b6ceed79403c411c8dc [file] [log] [blame]
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001/*
2 * event tracer
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
Steven Rostedt981d0812009-03-02 13:53:59 -05006 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 *
Steven Rostedtb77e38a2009-02-24 10:21:36 -05009 */
10
Steven Rostedte6187002009-04-15 13:36:40 -040011#include <linux/workqueue.h>
12#include <linux/spinlock.h>
13#include <linux/kthread.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050014#include <linux/debugfs.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Steven Rostedte6187002009-04-15 13:36:40 -040019#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050020
Li Zefan020e5f82009-07-01 10:47:05 +080021#include <asm/setup.h>
22
Steven Rostedt91729ef92009-03-02 15:03:01 -050023#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050024
Steven Rostedt4e5292e2009-09-12 19:26:21 -040025#undef TRACE_SYSTEM
Steven Rostedtb628b3e2009-02-27 23:32:58 -050026#define TRACE_SYSTEM "TRACE_SYSTEM"
27
Li Zefan20c89282009-05-06 10:33:45 +080028DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050029
Steven Rostedta59fd602009-04-10 13:52:20 -040030LIST_HEAD(ftrace_events);
31
Steven Rostedt2e33af02010-04-22 10:35:55 -040032struct list_head *
33trace_get_fields(struct ftrace_event_call *event_call)
34{
35 if (!event_call->class->get_fields)
36 return &event_call->class->fields;
37 return event_call->class->get_fields(event_call);
38}
39
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +020040int trace_define_field(struct ftrace_event_call *call, const char *type,
41 const char *name, int offset, int size, int is_signed,
Li Zefan43b51ea2009-08-07 10:33:22 +080042 int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -050043{
44 struct ftrace_event_field *field;
Steven Rostedt2e33af02010-04-22 10:35:55 -040045 struct list_head *head;
46
47 if (WARN_ON(!call->class))
48 return 0;
Tom Zanussicf027f62009-03-22 03:30:39 -050049
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010050 field = kzalloc(sizeof(*field), GFP_KERNEL);
Tom Zanussicf027f62009-03-22 03:30:39 -050051 if (!field)
52 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010053
Tom Zanussicf027f62009-03-22 03:30:39 -050054 field->name = kstrdup(name, GFP_KERNEL);
55 if (!field->name)
56 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010057
Tom Zanussicf027f62009-03-22 03:30:39 -050058 field->type = kstrdup(type, GFP_KERNEL);
59 if (!field->type)
60 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010061
Li Zefan43b51ea2009-08-07 10:33:22 +080062 if (filter_type == FILTER_OTHER)
63 field->filter_type = filter_assign_type(type);
64 else
65 field->filter_type = filter_type;
66
Tom Zanussicf027f62009-03-22 03:30:39 -050067 field->offset = offset;
68 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -050069 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +080070
Steven Rostedt2e33af02010-04-22 10:35:55 -040071 head = trace_get_fields(call);
72 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -050073
74 return 0;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010075
Tom Zanussicf027f62009-03-22 03:30:39 -050076err:
Wenji Huang7b60997f2010-02-24 15:40:26 +080077 if (field)
Tom Zanussicf027f62009-03-22 03:30:39 -050078 kfree(field->name);
Tom Zanussicf027f62009-03-22 03:30:39 -050079 kfree(field);
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010080
Tom Zanussicf027f62009-03-22 03:30:39 -050081 return -ENOMEM;
82}
Steven Rostedt17c873e2009-04-10 18:12:50 -040083EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -050084
Li Zefane647d6b2009-08-19 15:54:32 +080085#define __common_field(type, item) \
86 ret = trace_define_field(call, #type, "common_" #item, \
87 offsetof(typeof(ent), item), \
88 sizeof(ent.item), \
Li Zefan43b51ea2009-08-07 10:33:22 +080089 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +080090 if (ret) \
91 return ret;
92
Li Zefan614a71a2009-12-08 11:14:36 +080093static int trace_define_common_fields(struct ftrace_event_call *call)
Li Zefane647d6b2009-08-19 15:54:32 +080094{
95 int ret;
96 struct trace_entry ent;
97
98 __common_field(unsigned short, type);
99 __common_field(unsigned char, flags);
100 __common_field(unsigned char, preempt_count);
101 __common_field(int, pid);
Steven Rostedt637e7e82009-09-11 13:55:35 -0400102 __common_field(int, lock_depth);
Li Zefane647d6b2009-08-19 15:54:32 +0800103
104 return ret;
105}
106
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400107void trace_destroy_fields(struct ftrace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800108{
109 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400110 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800111
Steven Rostedt2e33af02010-04-22 10:35:55 -0400112 head = trace_get_fields(call);
113 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800114 list_del(&field->link);
115 kfree(field->type);
116 kfree(field->name);
117 kfree(field);
118 }
119}
120
Li Zefan87d9b4e2009-12-08 11:14:20 +0800121int trace_event_raw_init(struct ftrace_event_call *call)
122{
123 int id;
124
125 id = register_ftrace_event(call->event);
126 if (!id)
127 return -ENODEV;
128 call->id = id;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800129
130 return 0;
131}
132EXPORT_SYMBOL_GPL(trace_event_raw_init);
133
Li Zefan3b8e4272009-12-08 11:14:52 +0800134static int ftrace_event_enable_disable(struct ftrace_event_call *call,
Steven Rostedtfd994982009-02-28 02:41:25 -0500135 int enable)
136{
Li Zefan3b8e4272009-12-08 11:14:52 +0800137 int ret = 0;
138
Steven Rostedtfd994982009-02-28 02:41:25 -0500139 switch (enable) {
140 case 0:
141 if (call->enabled) {
142 call->enabled = 0;
Zhaoleib11c53e2009-05-25 18:11:59 +0800143 tracing_stop_cmdline_record();
Steven Rostedt22392912010-04-21 12:27:06 -0400144 if (call->class->reg)
145 call->class->reg(call, TRACE_REG_UNREGISTER);
146 else
147 tracepoint_probe_unregister(call->name,
148 call->class->probe,
149 call);
Steven Rostedtfd994982009-02-28 02:41:25 -0500150 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500151 break;
152 case 1:
Steven Rostedtda4d0302009-03-09 17:14:30 -0400153 if (!call->enabled) {
Zhaoleib11c53e2009-05-25 18:11:59 +0800154 tracing_start_cmdline_record();
Steven Rostedt22392912010-04-21 12:27:06 -0400155 if (call->class->reg)
156 ret = call->class->reg(call, TRACE_REG_REGISTER);
157 else
158 ret = tracepoint_probe_register(call->name,
159 call->class->probe,
160 call);
Li Zefan3b8e4272009-12-08 11:14:52 +0800161 if (ret) {
162 tracing_stop_cmdline_record();
163 pr_info("event trace: Could not enable event "
164 "%s\n", call->name);
165 break;
166 }
167 call->enabled = 1;
Steven Rostedtfd994982009-02-28 02:41:25 -0500168 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500169 break;
170 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800171
172 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500173}
174
Zhaolei0e907c92009-05-25 18:13:59 +0800175static void ftrace_clear_events(void)
176{
177 struct ftrace_event_call *call;
178
179 mutex_lock(&event_mutex);
180 list_for_each_entry(call, &ftrace_events, list) {
181 ftrace_event_enable_disable(call, 0);
182 }
183 mutex_unlock(&event_mutex);
184}
185
Li Zefan8f31bfe2009-05-08 10:31:42 +0800186/*
187 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
188 */
189static int __ftrace_set_clr_event(const char *match, const char *sub,
190 const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500191{
Steven Rostedta59fd602009-04-10 13:52:20 -0400192 struct ftrace_event_call *call;
Steven Rostedt29f93942009-05-08 16:06:47 -0400193 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500194
Steven Rostedt11a241a2009-03-02 11:49:04 -0500195 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -0400196 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500197
Steven Rostedt22392912010-04-21 12:27:06 -0400198 if (!call->name || !call->class ||
199 (!call->class->probe && !call->class->reg))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500200 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500201
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500202 if (match &&
203 strcmp(match, call->name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400204 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500205 continue;
206
Steven Rostedt8f082012010-04-20 10:47:33 -0400207 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500208 continue;
209
210 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500211 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500212
Steven Rostedtfd994982009-02-28 02:41:25 -0500213 ftrace_event_enable_disable(call, set);
214
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500215 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500216 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500217 mutex_unlock(&event_mutex);
218
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500219 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500220}
221
Li Zefan8f31bfe2009-05-08 10:31:42 +0800222static int ftrace_set_clr_event(char *buf, int set)
223{
224 char *event = NULL, *sub = NULL, *match;
225
226 /*
227 * The buf format can be <subsystem>:<event-name>
228 * *:<event-name> means any event by that name.
229 * :<event-name> is the same.
230 *
231 * <subsystem>:* means all events in that subsystem
232 * <subsystem>: means the same.
233 *
234 * <name> (no ':') means all events in a subsystem with
235 * the name <name> or any event that matches <name>
236 */
237
238 match = strsep(&buf, ":");
239 if (buf) {
240 sub = match;
241 event = buf;
242 match = NULL;
243
244 if (!strlen(sub) || strcmp(sub, "*") == 0)
245 sub = NULL;
246 if (!strlen(event) || strcmp(event, "*") == 0)
247 event = NULL;
248 }
249
250 return __ftrace_set_clr_event(match, sub, event, set);
251}
252
Steven Rostedt4671c792009-05-08 16:27:41 -0400253/**
254 * trace_set_clr_event - enable or disable an event
255 * @system: system name to match (NULL for any system)
256 * @event: event name to match (NULL for all events, within system)
257 * @set: 1 to enable, 0 to disable
258 *
259 * This is a way for other parts of the kernel to enable or disable
260 * event recording.
261 *
262 * Returns 0 on success, -EINVAL if the parameters do not match any
263 * registered events.
264 */
265int trace_set_clr_event(const char *system, const char *event, int set)
266{
267 return __ftrace_set_clr_event(NULL, system, event, set);
268}
269
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500270/* 128 should be much more than enough */
271#define EVENT_BUF_SIZE 127
272
273static ssize_t
274ftrace_event_write(struct file *file, const char __user *ubuf,
275 size_t cnt, loff_t *ppos)
276{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200277 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +0800278 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500279
Li Zefan4ba79782009-09-22 13:52:20 +0800280 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500281 return 0;
282
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400283 ret = tracing_update_buffers();
284 if (ret < 0)
285 return ret;
286
jolsa@redhat.com48966362009-09-11 17:29:28 +0200287 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500288 return -ENOMEM;
289
jolsa@redhat.com48966362009-09-11 17:29:28 +0200290 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500291
Li Zefan4ba79782009-09-22 13:52:20 +0800292 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200293 int set = 1;
294
295 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500296 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500297
jolsa@redhat.com48966362009-09-11 17:29:28 +0200298 parser.buffer[parser.idx] = 0;
299
300 ret = ftrace_set_clr_event(parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500301 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200302 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500303 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500304
305 ret = read;
306
jolsa@redhat.com48966362009-09-11 17:29:28 +0200307 out_put:
308 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500309
310 return ret;
311}
312
313static void *
314t_next(struct seq_file *m, void *v, loff_t *pos)
315{
Li Zefan30bd39c2009-09-18 14:07:05 +0800316 struct ftrace_event_call *call = v;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500317
318 (*pos)++;
319
Li Zefan30bd39c2009-09-18 14:07:05 +0800320 list_for_each_entry_continue(call, &ftrace_events, list) {
Steven Rostedt40e26812009-03-10 11:32:40 -0400321 /*
322 * The ftrace subsystem is for showing formats only.
323 * They can not be enabled or disabled via the event files.
324 */
Steven Rostedt22392912010-04-21 12:27:06 -0400325 if (call->class && (call->class->probe || call->class->reg))
Li Zefan30bd39c2009-09-18 14:07:05 +0800326 return call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400327 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500328
Li Zefan30bd39c2009-09-18 14:07:05 +0800329 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500330}
331
332static void *t_start(struct seq_file *m, loff_t *pos)
333{
Li Zefan30bd39c2009-09-18 14:07:05 +0800334 struct ftrace_event_call *call;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800335 loff_t l;
336
Li Zefan20c89282009-05-06 10:33:45 +0800337 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800338
Li Zefan30bd39c2009-09-18 14:07:05 +0800339 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800340 for (l = 0; l <= *pos; ) {
Li Zefan30bd39c2009-09-18 14:07:05 +0800341 call = t_next(m, call, &l);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800342 if (!call)
343 break;
344 }
345 return call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500346}
347
348static void *
349s_next(struct seq_file *m, void *v, loff_t *pos)
350{
Li Zefan30bd39c2009-09-18 14:07:05 +0800351 struct ftrace_event_call *call = v;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500352
353 (*pos)++;
354
Li Zefan30bd39c2009-09-18 14:07:05 +0800355 list_for_each_entry_continue(call, &ftrace_events, list) {
356 if (call->enabled)
357 return call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500358 }
359
Li Zefan30bd39c2009-09-18 14:07:05 +0800360 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500361}
362
363static void *s_start(struct seq_file *m, loff_t *pos)
364{
Li Zefan30bd39c2009-09-18 14:07:05 +0800365 struct ftrace_event_call *call;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800366 loff_t l;
367
Li Zefan20c89282009-05-06 10:33:45 +0800368 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800369
Li Zefan30bd39c2009-09-18 14:07:05 +0800370 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800371 for (l = 0; l <= *pos; ) {
Li Zefan30bd39c2009-09-18 14:07:05 +0800372 call = s_next(m, call, &l);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800373 if (!call)
374 break;
375 }
376 return call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500377}
378
379static int t_show(struct seq_file *m, void *v)
380{
381 struct ftrace_event_call *call = v;
382
Steven Rostedt8f082012010-04-20 10:47:33 -0400383 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
384 seq_printf(m, "%s:", call->class->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500385 seq_printf(m, "%s\n", call->name);
386
387 return 0;
388}
389
390static void t_stop(struct seq_file *m, void *p)
391{
Li Zefan20c89282009-05-06 10:33:45 +0800392 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500393}
394
395static int
396ftrace_event_seq_open(struct inode *inode, struct file *file)
397{
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500398 const struct seq_operations *seq_ops;
399
400 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -0400401 (file->f_flags & O_TRUNC))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500402 ftrace_clear_events();
403
404 seq_ops = inode->i_private;
Li Zefan20c89282009-05-06 10:33:45 +0800405 return seq_open(file, seq_ops);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500406}
407
Steven Rostedt1473e442009-02-24 14:15:08 -0500408static ssize_t
409event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
410 loff_t *ppos)
411{
412 struct ftrace_event_call *call = filp->private_data;
413 char *buf;
414
Steven Rostedtda4d0302009-03-09 17:14:30 -0400415 if (call->enabled)
Steven Rostedt1473e442009-02-24 14:15:08 -0500416 buf = "1\n";
417 else
418 buf = "0\n";
419
420 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
421}
422
423static ssize_t
424event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
425 loff_t *ppos)
426{
427 struct ftrace_event_call *call = filp->private_data;
428 char buf[64];
429 unsigned long val;
430 int ret;
431
432 if (cnt >= sizeof(buf))
433 return -EINVAL;
434
435 if (copy_from_user(&buf, ubuf, cnt))
436 return -EFAULT;
437
438 buf[cnt] = 0;
439
440 ret = strict_strtoul(buf, 10, &val);
441 if (ret < 0)
442 return ret;
443
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400444 ret = tracing_update_buffers();
445 if (ret < 0)
446 return ret;
447
Steven Rostedt1473e442009-02-24 14:15:08 -0500448 switch (val) {
449 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500450 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500451 mutex_lock(&event_mutex);
Li Zefan3b8e4272009-12-08 11:14:52 +0800452 ret = ftrace_event_enable_disable(call, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500453 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500454 break;
455
456 default:
457 return -EINVAL;
458 }
459
460 *ppos += cnt;
461
Li Zefan3b8e4272009-12-08 11:14:52 +0800462 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -0500463}
464
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400465static ssize_t
466system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
467 loff_t *ppos)
468{
Li Zefanc142b152009-05-08 10:32:05 +0800469 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400470 const char *system = filp->private_data;
471 struct ftrace_event_call *call;
472 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +0800473 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400474 int ret;
475
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400476 mutex_lock(&event_mutex);
477 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedt22392912010-04-21 12:27:06 -0400478 if (!call->name || !call->class ||
479 (!call->class->probe && !call->class->reg))
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400480 continue;
481
Steven Rostedt8f082012010-04-20 10:47:33 -0400482 if (system && strcmp(call->class->system, system) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400483 continue;
484
485 /*
486 * We need to find out if all the events are set
487 * or if all events or cleared, or if we have
488 * a mixture.
489 */
Li Zefanc142b152009-05-08 10:32:05 +0800490 set |= (1 << !!call->enabled);
491
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400492 /*
493 * If we have a mixture, no need to look further.
494 */
Li Zefanc142b152009-05-08 10:32:05 +0800495 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400496 break;
497 }
498 mutex_unlock(&event_mutex);
499
Li Zefanc142b152009-05-08 10:32:05 +0800500 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400501 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400502
503 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
504
505 return ret;
506}
507
508static ssize_t
509system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
510 loff_t *ppos)
511{
512 const char *system = filp->private_data;
513 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400514 char buf[64];
515 ssize_t ret;
516
517 if (cnt >= sizeof(buf))
518 return -EINVAL;
519
520 if (copy_from_user(&buf, ubuf, cnt))
521 return -EFAULT;
522
523 buf[cnt] = 0;
524
525 ret = strict_strtoul(buf, 10, &val);
526 if (ret < 0)
527 return ret;
528
529 ret = tracing_update_buffers();
530 if (ret < 0)
531 return ret;
532
Li Zefan8f31bfe2009-05-08 10:31:42 +0800533 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400534 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400535
Li Zefan8f31bfe2009-05-08 10:31:42 +0800536 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400537 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800538 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400539
540 ret = cnt;
541
Li Zefan8f31bfe2009-05-08 10:31:42 +0800542out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400543 *ppos += cnt;
544
545 return ret;
546}
547
Steven Rostedt981d0812009-03-02 13:53:59 -0500548static ssize_t
549event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
550 loff_t *ppos)
551{
552 struct ftrace_event_call *call = filp->private_data;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800553 struct ftrace_event_field *field;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400554 struct list_head *head;
Steven Rostedt981d0812009-03-02 13:53:59 -0500555 struct trace_seq *s;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800556 int common_field_count = 5;
Steven Rostedt981d0812009-03-02 13:53:59 -0500557 char *buf;
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800558 int r = 0;
Steven Rostedt981d0812009-03-02 13:53:59 -0500559
Tom Zanussic269fc82009-03-17 01:20:59 -0500560 if (*ppos)
561 return 0;
562
Steven Rostedt981d0812009-03-02 13:53:59 -0500563 s = kmalloc(sizeof(*s), GFP_KERNEL);
564 if (!s)
565 return -ENOMEM;
566
567 trace_seq_init(s);
568
Steven Rostedtc5e4e192009-03-02 15:10:02 -0500569 trace_seq_printf(s, "name: %s\n", call->name);
570 trace_seq_printf(s, "ID: %d\n", call->id);
571 trace_seq_printf(s, "format:\n");
Steven Rostedt91729ef92009-03-02 15:03:01 -0500572
Steven Rostedt2e33af02010-04-22 10:35:55 -0400573 head = trace_get_fields(call);
574 list_for_each_entry_reverse(field, head, link) {
Lai Jiangshan5a65e952009-12-15 15:39:53 +0800575 /*
576 * Smartly shows the array type(except dynamic array).
577 * Normal:
578 * field:TYPE VAR
579 * If TYPE := TYPE[LEN], it is shown:
580 * field:TYPE VAR[LEN]
581 */
582 const char *array_descriptor = strchr(field->type, '[');
583
584 if (!strncmp(field->type, "__data_loc", 10))
585 array_descriptor = NULL;
586
587 if (!array_descriptor) {
588 r = trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
589 "\tsize:%u;\tsigned:%d;\n",
590 field->type, field->name, field->offset,
591 field->size, !!field->is_signed);
592 } else {
593 r = trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
594 "\tsize:%u;\tsigned:%d;\n",
595 (int)(array_descriptor - field->type),
596 field->type, field->name,
597 array_descriptor, field->offset,
598 field->size, !!field->is_signed);
599 }
600
601 if (--common_field_count == 0)
602 r = trace_seq_printf(s, "\n");
603
604 if (!r)
605 break;
606 }
607
608 if (r)
609 r = trace_seq_printf(s, "\nprint fmt: %s\n",
610 call->print_fmt);
611
Steven Rostedt981d0812009-03-02 13:53:59 -0500612 if (!r) {
613 /*
614 * ug! The format output is bigger than a PAGE!!
615 */
616 buf = "FORMAT TOO BIG\n";
617 r = simple_read_from_buffer(ubuf, cnt, ppos,
618 buf, strlen(buf));
619 goto out;
620 }
621
622 r = simple_read_from_buffer(ubuf, cnt, ppos,
623 s->buffer, s->len);
624 out:
625 kfree(s);
626 return r;
627}
628
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100629static ssize_t
630event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
631{
632 struct ftrace_event_call *call = filp->private_data;
633 struct trace_seq *s;
634 int r;
635
636 if (*ppos)
637 return 0;
638
639 s = kmalloc(sizeof(*s), GFP_KERNEL);
640 if (!s)
641 return -ENOMEM;
642
643 trace_seq_init(s);
644 trace_seq_printf(s, "%d\n", call->id);
645
646 r = simple_read_from_buffer(ubuf, cnt, ppos,
647 s->buffer, s->len);
648 kfree(s);
649 return r;
650}
651
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500652static ssize_t
653event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
654 loff_t *ppos)
655{
656 struct ftrace_event_call *call = filp->private_data;
657 struct trace_seq *s;
658 int r;
659
660 if (*ppos)
661 return 0;
662
663 s = kmalloc(sizeof(*s), GFP_KERNEL);
664 if (!s)
665 return -ENOMEM;
666
667 trace_seq_init(s);
668
Tom Zanussi8b372562009-04-28 03:04:59 -0500669 print_event_filter(call, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500670 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500671
672 kfree(s);
673
674 return r;
675}
676
677static ssize_t
678event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
679 loff_t *ppos)
680{
681 struct ftrace_event_call *call = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500682 char *buf;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500683 int err;
684
Tom Zanussi8b372562009-04-28 03:04:59 -0500685 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500686 return -EINVAL;
687
Tom Zanussi8b372562009-04-28 03:04:59 -0500688 buf = (char *)__get_free_page(GFP_TEMPORARY);
689 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500690 return -ENOMEM;
691
Tom Zanussi8b372562009-04-28 03:04:59 -0500692 if (copy_from_user(buf, ubuf, cnt)) {
693 free_page((unsigned long) buf);
694 return -EFAULT;
695 }
696 buf[cnt] = '\0';
697
698 err = apply_event_filter(call, buf);
699 free_page((unsigned long) buf);
700 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500701 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500702
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500703 *ppos += cnt;
704
705 return cnt;
706}
707
Tom Zanussicfb180f2009-03-22 03:31:17 -0500708static ssize_t
709subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
710 loff_t *ppos)
711{
712 struct event_subsystem *system = filp->private_data;
713 struct trace_seq *s;
714 int r;
715
716 if (*ppos)
717 return 0;
718
719 s = kmalloc(sizeof(*s), GFP_KERNEL);
720 if (!s)
721 return -ENOMEM;
722
723 trace_seq_init(s);
724
Tom Zanussi8b372562009-04-28 03:04:59 -0500725 print_subsystem_event_filter(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500726 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500727
728 kfree(s);
729
730 return r;
731}
732
733static ssize_t
734subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
735 loff_t *ppos)
736{
737 struct event_subsystem *system = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500738 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500739 int err;
740
Tom Zanussi8b372562009-04-28 03:04:59 -0500741 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500742 return -EINVAL;
743
Tom Zanussi8b372562009-04-28 03:04:59 -0500744 buf = (char *)__get_free_page(GFP_TEMPORARY);
745 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500746 return -ENOMEM;
747
Tom Zanussi8b372562009-04-28 03:04:59 -0500748 if (copy_from_user(buf, ubuf, cnt)) {
749 free_page((unsigned long) buf);
750 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500751 }
Tom Zanussi8b372562009-04-28 03:04:59 -0500752 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -0500753
Tom Zanussi8b372562009-04-28 03:04:59 -0500754 err = apply_subsystem_event_filter(system, buf);
755 free_page((unsigned long) buf);
756 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +0800757 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500758
759 *ppos += cnt;
760
761 return cnt;
762}
763
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400764static ssize_t
765show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
766{
767 int (*func)(struct trace_seq *s) = filp->private_data;
768 struct trace_seq *s;
769 int r;
770
771 if (*ppos)
772 return 0;
773
774 s = kmalloc(sizeof(*s), GFP_KERNEL);
775 if (!s)
776 return -ENOMEM;
777
778 trace_seq_init(s);
779
780 func(s);
781 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
782
783 kfree(s);
784
785 return r;
786}
787
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500788static const struct seq_operations show_event_seq_ops = {
789 .start = t_start,
790 .next = t_next,
791 .show = t_show,
792 .stop = t_stop,
793};
794
795static const struct seq_operations show_set_event_seq_ops = {
796 .start = s_start,
797 .next = s_next,
798 .show = t_show,
799 .stop = t_stop,
800};
801
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400802static const struct file_operations ftrace_avail_fops = {
803 .open = ftrace_event_seq_open,
804 .read = seq_read,
805 .llseek = seq_lseek,
806 .release = seq_release,
807};
808
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500809static const struct file_operations ftrace_set_event_fops = {
810 .open = ftrace_event_seq_open,
811 .read = seq_read,
812 .write = ftrace_event_write,
813 .llseek = seq_lseek,
814 .release = seq_release,
815};
816
Steven Rostedt1473e442009-02-24 14:15:08 -0500817static const struct file_operations ftrace_enable_fops = {
818 .open = tracing_open_generic,
819 .read = event_enable_read,
820 .write = event_enable_write,
821};
822
Steven Rostedt981d0812009-03-02 13:53:59 -0500823static const struct file_operations ftrace_event_format_fops = {
824 .open = tracing_open_generic,
825 .read = event_format_read,
826};
827
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100828static const struct file_operations ftrace_event_id_fops = {
829 .open = tracing_open_generic,
830 .read = event_id_read,
831};
832
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500833static const struct file_operations ftrace_event_filter_fops = {
834 .open = tracing_open_generic,
835 .read = event_filter_read,
836 .write = event_filter_write,
837};
838
Tom Zanussicfb180f2009-03-22 03:31:17 -0500839static const struct file_operations ftrace_subsystem_filter_fops = {
840 .open = tracing_open_generic,
841 .read = subsystem_filter_read,
842 .write = subsystem_filter_write,
843};
844
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400845static const struct file_operations ftrace_system_enable_fops = {
846 .open = tracing_open_generic,
847 .read = system_enable_read,
848 .write = system_enable_write,
849};
850
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400851static const struct file_operations ftrace_show_header_fops = {
852 .open = tracing_open_generic,
853 .read = show_header,
854};
855
Steven Rostedt1473e442009-02-24 14:15:08 -0500856static struct dentry *event_trace_events_dir(void)
857{
858 static struct dentry *d_tracer;
859 static struct dentry *d_events;
860
861 if (d_events)
862 return d_events;
863
864 d_tracer = tracing_init_dentry();
865 if (!d_tracer)
866 return NULL;
867
868 d_events = debugfs_create_dir("events", d_tracer);
869 if (!d_events)
870 pr_warning("Could not create debugfs "
871 "'events' directory\n");
872
873 return d_events;
874}
875
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500876static LIST_HEAD(event_subsystems);
877
878static struct dentry *
879event_subsystem_dir(const char *name, struct dentry *d_events)
880{
881 struct event_subsystem *system;
Tom Zanussie1112b42009-03-31 00:48:49 -0500882 struct dentry *entry;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500883
884 /* First see if we did not already create this dir */
885 list_for_each_entry(system, &event_subsystems, list) {
Xiao Guangrongdc82ec98a2009-07-09 16:22:22 +0800886 if (strcmp(system->name, name) == 0) {
887 system->nr_events++;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500888 return system->entry;
Xiao Guangrongdc82ec98a2009-07-09 16:22:22 +0800889 }
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500890 }
891
892 /* need to create new entry */
893 system = kmalloc(sizeof(*system), GFP_KERNEL);
894 if (!system) {
895 pr_warning("No memory to create event subsystem %s\n",
896 name);
897 return d_events;
898 }
899
900 system->entry = debugfs_create_dir(name, d_events);
901 if (!system->entry) {
902 pr_warning("Could not create event subsystem %s\n",
903 name);
904 kfree(system);
905 return d_events;
906 }
907
Xiao Guangrongdc82ec98a2009-07-09 16:22:22 +0800908 system->nr_events = 1;
Steven Rostedt6d723732009-04-10 14:53:50 -0400909 system->name = kstrdup(name, GFP_KERNEL);
910 if (!system->name) {
911 debugfs_remove(system->entry);
912 kfree(system);
913 return d_events;
914 }
915
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500916 list_add(&system->list, &event_subsystems);
917
Tom Zanussi30e673b2009-04-28 03:04:47 -0500918 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500919
Tom Zanussi8b372562009-04-28 03:04:59 -0500920 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
921 if (!system->filter) {
922 pr_warning("Could not allocate filter for subsystem "
923 "'%s'\n", name);
924 return system->entry;
925 }
926
Tom Zanussie1112b42009-03-31 00:48:49 -0500927 entry = debugfs_create_file("filter", 0644, system->entry, system,
928 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -0500929 if (!entry) {
930 kfree(system->filter);
931 system->filter = NULL;
Tom Zanussie1112b42009-03-31 00:48:49 -0500932 pr_warning("Could not create debugfs "
933 "'%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -0500934 }
Tom Zanussie1112b42009-03-31 00:48:49 -0500935
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +0200936 trace_create_file("enable", 0644, system->entry,
937 (void *)system->name,
938 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400939
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500940 return system->entry;
941}
942
Steven Rostedt1473e442009-02-24 14:15:08 -0500943static int
Steven Rostedt701970b2009-04-24 23:11:22 -0400944event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
945 const struct file_operations *id,
946 const struct file_operations *enable,
947 const struct file_operations *filter,
948 const struct file_operations *format)
Steven Rostedt1473e442009-02-24 14:15:08 -0500949{
Steven Rostedt2e33af02010-04-22 10:35:55 -0400950 struct list_head *head;
Steven Rostedtfd994982009-02-28 02:41:25 -0500951 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -0500952
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500953 /*
954 * If the trace point header did not define TRACE_SYSTEM
955 * then the system would be called "TRACE_SYSTEM".
956 */
Steven Rostedt8f082012010-04-20 10:47:33 -0400957 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
958 d_events = event_subsystem_dir(call->class->system, d_events);
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500959
Steven Rostedt1473e442009-02-24 14:15:08 -0500960 call->dir = debugfs_create_dir(call->name, d_events);
961 if (!call->dir) {
962 pr_warning("Could not create debugfs "
963 "'%s' directory\n", call->name);
964 return -1;
965 }
966
Steven Rostedt22392912010-04-21 12:27:06 -0400967 if (call->class->probe || call->class->reg)
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +0200968 trace_create_file("enable", 0644, call->dir, call,
969 enable);
Steven Rostedt1473e442009-02-24 14:15:08 -0500970
Steven Rostedt22392912010-04-21 12:27:06 -0400971#ifdef CONFIG_PERF_EVENTS
972 if (call->id && (call->class->perf_probe || call->class->reg))
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +0200973 trace_create_file("id", 0444, call->dir, call,
974 id);
Steven Rostedt22392912010-04-21 12:27:06 -0400975#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100976
Steven Rostedt2e33af02010-04-22 10:35:55 -0400977 if (call->class->define_fields) {
978 /*
979 * Other events may have the same class. Only update
980 * the fields if they are not already defined.
981 */
982 head = trace_get_fields(call);
983 if (list_empty(head)) {
984 ret = trace_define_common_fields(call);
985 if (!ret)
986 ret = call->class->define_fields(call);
987 if (ret < 0) {
988 pr_warning("Could not initialize trace point"
989 " events/%s\n", call->name);
990 return ret;
991 }
Tom Zanussicf027f62009-03-22 03:30:39 -0500992 }
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +0200993 trace_create_file("filter", 0644, call->dir, call,
994 filter);
Tom Zanussicf027f62009-03-22 03:30:39 -0500995 }
996
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +0200997 trace_create_file("format", 0444, call->dir, call,
998 format);
Steven Rostedtfd994982009-02-28 02:41:25 -0500999
Steven Rostedt1473e442009-02-24 14:15:08 -05001000 return 0;
1001}
1002
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001003static int __trace_add_event_call(struct ftrace_event_call *call)
1004{
1005 struct dentry *d_events;
1006 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001007
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001008 if (!call->name)
1009 return -EINVAL;
Steven Rostedt701970b2009-04-24 23:11:22 -04001010
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001011 if (call->raw_init) {
1012 ret = call->raw_init(call);
1013 if (ret < 0) {
1014 if (ret != -ENOSYS)
1015 pr_warning("Could not initialize trace "
1016 "events/%s\n", call->name);
1017 return ret;
1018 }
1019 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001020
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001021 d_events = event_trace_events_dir();
1022 if (!d_events)
1023 return -ENOENT;
1024
Masami Hiramatsu588bebb2009-09-16 11:42:55 -04001025 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001026 &ftrace_enable_fops, &ftrace_event_filter_fops,
1027 &ftrace_event_format_fops);
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001028 if (!ret)
1029 list_add(&call->list, &ftrace_events);
1030
Masami Hiramatsu588bebb2009-09-16 11:42:55 -04001031 return ret;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001032}
1033
1034/* Add an additional event_call dynamically */
1035int trace_add_event_call(struct ftrace_event_call *call)
1036{
1037 int ret;
1038 mutex_lock(&event_mutex);
1039 ret = __trace_add_event_call(call);
1040 mutex_unlock(&event_mutex);
1041 return ret;
1042}
Steven Rostedt701970b2009-04-24 23:11:22 -04001043
Frederic Weisbeckera2ca5e02009-08-06 07:32:21 +02001044static void remove_subsystem_dir(const char *name)
1045{
1046 struct event_subsystem *system;
1047
1048 if (strcmp(name, TRACE_SYSTEM) == 0)
1049 return;
1050
1051 list_for_each_entry(system, &event_subsystems, list) {
1052 if (strcmp(system->name, name) == 0) {
1053 if (!--system->nr_events) {
1054 struct event_filter *filter = system->filter;
1055
1056 debugfs_remove_recursive(system->entry);
1057 list_del(&system->list);
1058 if (filter) {
1059 kfree(filter->filter_string);
1060 kfree(filter);
1061 }
1062 kfree(system->name);
1063 kfree(system);
1064 }
1065 break;
1066 }
1067 }
1068}
1069
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001070/*
1071 * Must be called under locking both of event_mutex and trace_event_mutex.
1072 */
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001073static void __trace_remove_event_call(struct ftrace_event_call *call)
1074{
1075 ftrace_event_enable_disable(call, 0);
1076 if (call->event)
1077 __unregister_ftrace_event(call->event);
1078 debugfs_remove_recursive(call->dir);
1079 list_del(&call->list);
1080 trace_destroy_fields(call);
1081 destroy_preds(call);
Steven Rostedt8f082012010-04-20 10:47:33 -04001082 remove_subsystem_dir(call->class->system);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001083}
1084
1085/* Remove an event_call */
1086void trace_remove_event_call(struct ftrace_event_call *call)
1087{
1088 mutex_lock(&event_mutex);
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001089 down_write(&trace_event_mutex);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001090 __trace_remove_event_call(call);
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04001091 up_write(&trace_event_mutex);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001092 mutex_unlock(&event_mutex);
1093}
1094
1095#define for_each_event(event, start, end) \
1096 for (event = start; \
1097 (unsigned long)event < (unsigned long)end; \
1098 event++)
1099
1100#ifdef CONFIG_MODULES
1101
1102static LIST_HEAD(ftrace_module_file_list);
1103
1104/*
1105 * Modules must own their file_operations to keep up with
1106 * reference counting.
1107 */
1108struct ftrace_module_file_ops {
1109 struct list_head list;
1110 struct module *mod;
1111 struct file_operations id;
1112 struct file_operations enable;
1113 struct file_operations format;
1114 struct file_operations filter;
1115};
1116
Steven Rostedt701970b2009-04-24 23:11:22 -04001117static struct ftrace_module_file_ops *
1118trace_create_file_ops(struct module *mod)
1119{
1120 struct ftrace_module_file_ops *file_ops;
1121
1122 /*
1123 * This is a bit of a PITA. To allow for correct reference
1124 * counting, modules must "own" their file_operations.
1125 * To do this, we allocate the file operations that will be
1126 * used in the event directory.
1127 */
1128
1129 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1130 if (!file_ops)
1131 return NULL;
1132
1133 file_ops->mod = mod;
1134
1135 file_ops->id = ftrace_event_id_fops;
1136 file_ops->id.owner = mod;
1137
1138 file_ops->enable = ftrace_enable_fops;
1139 file_ops->enable.owner = mod;
1140
1141 file_ops->filter = ftrace_event_filter_fops;
1142 file_ops->filter.owner = mod;
1143
1144 file_ops->format = ftrace_event_format_fops;
1145 file_ops->format.owner = mod;
1146
1147 list_add(&file_ops->list, &ftrace_module_file_list);
1148
1149 return file_ops;
1150}
1151
Steven Rostedt6d723732009-04-10 14:53:50 -04001152static void trace_module_add_events(struct module *mod)
1153{
Steven Rostedt701970b2009-04-24 23:11:22 -04001154 struct ftrace_module_file_ops *file_ops = NULL;
Steven Rostedt6d723732009-04-10 14:53:50 -04001155 struct ftrace_event_call *call, *start, *end;
1156 struct dentry *d_events;
Jason Baronf744bd52009-08-10 16:52:39 -04001157 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04001158
1159 start = mod->trace_events;
1160 end = mod->trace_events + mod->num_trace_events;
1161
1162 if (start == end)
1163 return;
1164
1165 d_events = event_trace_events_dir();
1166 if (!d_events)
1167 return;
1168
1169 for_each_event(call, start, end) {
1170 /* The linker may leave blanks */
1171 if (!call->name)
1172 continue;
Jason Baronf744bd52009-08-10 16:52:39 -04001173 if (call->raw_init) {
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001174 ret = call->raw_init(call);
Jason Baronf744bd52009-08-10 16:52:39 -04001175 if (ret < 0) {
1176 if (ret != -ENOSYS)
1177 pr_warning("Could not initialize trace "
1178 "point events/%s\n", call->name);
1179 continue;
1180 }
1181 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001182 /*
1183 * This module has events, create file ops for this module
1184 * if not already done.
1185 */
1186 if (!file_ops) {
1187 file_ops = trace_create_file_ops(mod);
1188 if (!file_ops)
1189 return;
1190 }
Steven Rostedt6d723732009-04-10 14:53:50 -04001191 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001192 ret = event_create_dir(call, d_events,
1193 &file_ops->id, &file_ops->enable,
1194 &file_ops->filter, &file_ops->format);
1195 if (!ret)
1196 list_add(&call->list, &ftrace_events);
Steven Rostedt6d723732009-04-10 14:53:50 -04001197 }
1198}
1199
1200static void trace_module_remove_events(struct module *mod)
1201{
Steven Rostedt701970b2009-04-24 23:11:22 -04001202 struct ftrace_module_file_ops *file_ops;
Steven Rostedt6d723732009-04-10 14:53:50 -04001203 struct ftrace_event_call *call, *p;
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001204 bool found = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001205
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001206 down_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001207 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1208 if (call->mod == mod) {
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001209 found = true;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001210 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001211 }
1212 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001213
1214 /* Now free the file_operations */
1215 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1216 if (file_ops->mod == mod)
1217 break;
1218 }
1219 if (&file_ops->list != &ftrace_module_file_list) {
1220 list_del(&file_ops->list);
1221 kfree(file_ops);
1222 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001223
1224 /*
1225 * It is safest to reset the ring buffer if the module being unloaded
1226 * registered any events.
1227 */
1228 if (found)
1229 tracing_reset_current_online_cpus();
Steven Rostedt110bf2b2009-06-09 17:29:07 -04001230 up_write(&trace_event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04001231}
1232
Steven Rostedt61f919a2009-04-14 18:22:32 -04001233static int trace_module_notify(struct notifier_block *self,
1234 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001235{
1236 struct module *mod = data;
1237
1238 mutex_lock(&event_mutex);
1239 switch (val) {
1240 case MODULE_STATE_COMING:
1241 trace_module_add_events(mod);
1242 break;
1243 case MODULE_STATE_GOING:
1244 trace_module_remove_events(mod);
1245 break;
1246 }
1247 mutex_unlock(&event_mutex);
1248
1249 return 0;
1250}
Steven Rostedt61f919a2009-04-14 18:22:32 -04001251#else
1252static int trace_module_notify(struct notifier_block *self,
1253 unsigned long val, void *data)
1254{
1255 return 0;
1256}
1257#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001258
Steven Rostedtec827c72009-09-14 10:50:23 -04001259static struct notifier_block trace_module_nb = {
Steven Rostedt6d723732009-04-10 14:53:50 -04001260 .notifier_call = trace_module_notify,
1261 .priority = 0,
1262};
1263
Steven Rostedta59fd602009-04-10 13:52:20 -04001264extern struct ftrace_event_call __start_ftrace_events[];
1265extern struct ftrace_event_call __stop_ftrace_events[];
1266
Li Zefan020e5f82009-07-01 10:47:05 +08001267static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1268
1269static __init int setup_trace_event(char *str)
1270{
1271 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1272 ring_buffer_expanded = 1;
1273 tracing_selftest_disabled = 1;
1274
1275 return 1;
1276}
1277__setup("trace_event=", setup_trace_event);
1278
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001279static __init int event_trace_init(void)
1280{
Steven Rostedta59fd602009-04-10 13:52:20 -04001281 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001282 struct dentry *d_tracer;
1283 struct dentry *entry;
Steven Rostedt1473e442009-02-24 14:15:08 -05001284 struct dentry *d_events;
Steven Rostedt6d723732009-04-10 14:53:50 -04001285 int ret;
Li Zefan020e5f82009-07-01 10:47:05 +08001286 char *buf = bootup_event_buf;
1287 char *token;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001288
1289 d_tracer = tracing_init_dentry();
1290 if (!d_tracer)
1291 return 0;
1292
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001293 entry = debugfs_create_file("available_events", 0444, d_tracer,
1294 (void *)&show_event_seq_ops,
1295 &ftrace_avail_fops);
1296 if (!entry)
1297 pr_warning("Could not create debugfs "
1298 "'available_events' entry\n");
1299
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001300 entry = debugfs_create_file("set_event", 0644, d_tracer,
1301 (void *)&show_set_event_seq_ops,
1302 &ftrace_set_event_fops);
1303 if (!entry)
1304 pr_warning("Could not create debugfs "
1305 "'set_event' entry\n");
1306
Steven Rostedt1473e442009-02-24 14:15:08 -05001307 d_events = event_trace_events_dir();
1308 if (!d_events)
1309 return 0;
1310
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001311 /* ring buffer internal formats */
1312 trace_create_file("header_page", 0444, d_events,
1313 ring_buffer_print_page_header,
1314 &ftrace_show_header_fops);
1315
1316 trace_create_file("header_event", 0444, d_events,
1317 ring_buffer_print_entry_header,
1318 &ftrace_show_header_fops);
1319
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001320 trace_create_file("enable", 0644, d_events,
Li Zefan8f31bfe2009-05-08 10:31:42 +08001321 NULL, &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001322
Steven Rostedt6d723732009-04-10 14:53:50 -04001323 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
Steven Rostedt1473e442009-02-24 14:15:08 -05001324 /* The linker may leave blanks */
1325 if (!call->name)
1326 continue;
Jason Baronf744bd52009-08-10 16:52:39 -04001327 if (call->raw_init) {
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04001328 ret = call->raw_init(call);
Jason Baronf744bd52009-08-10 16:52:39 -04001329 if (ret < 0) {
1330 if (ret != -ENOSYS)
1331 pr_warning("Could not initialize trace "
1332 "point events/%s\n", call->name);
1333 continue;
1334 }
1335 }
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07001336 ret = event_create_dir(call, d_events, &ftrace_event_id_fops,
1337 &ftrace_enable_fops,
1338 &ftrace_event_filter_fops,
1339 &ftrace_event_format_fops);
1340 if (!ret)
1341 list_add(&call->list, &ftrace_events);
Steven Rostedt1473e442009-02-24 14:15:08 -05001342 }
1343
Li Zefan020e5f82009-07-01 10:47:05 +08001344 while (true) {
1345 token = strsep(&buf, ",");
1346
1347 if (!token)
1348 break;
1349 if (!*token)
1350 continue;
1351
1352 ret = ftrace_set_clr_event(token, 1);
1353 if (ret)
1354 pr_warning("Failed to enable trace event: %s\n", token);
1355 }
1356
Steven Rostedt6d723732009-04-10 14:53:50 -04001357 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08001358 if (ret)
Steven Rostedt6d723732009-04-10 14:53:50 -04001359 pr_warning("Failed to register trace events module notifier\n");
1360
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001361 return 0;
1362}
1363fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001364
1365#ifdef CONFIG_FTRACE_STARTUP_TEST
1366
1367static DEFINE_SPINLOCK(test_spinlock);
1368static DEFINE_SPINLOCK(test_spinlock_irq);
1369static DEFINE_MUTEX(test_mutex);
1370
1371static __init void test_work(struct work_struct *dummy)
1372{
1373 spin_lock(&test_spinlock);
1374 spin_lock_irq(&test_spinlock_irq);
1375 udelay(1);
1376 spin_unlock_irq(&test_spinlock_irq);
1377 spin_unlock(&test_spinlock);
1378
1379 mutex_lock(&test_mutex);
1380 msleep(1);
1381 mutex_unlock(&test_mutex);
1382}
1383
1384static __init int event_test_thread(void *unused)
1385{
1386 void *test_malloc;
1387
1388 test_malloc = kmalloc(1234, GFP_KERNEL);
1389 if (!test_malloc)
1390 pr_info("failed to kmalloc\n");
1391
1392 schedule_on_each_cpu(test_work);
1393
1394 kfree(test_malloc);
1395
1396 set_current_state(TASK_INTERRUPTIBLE);
1397 while (!kthread_should_stop())
1398 schedule();
1399
1400 return 0;
1401}
1402
1403/*
1404 * Do various things that may trigger events.
1405 */
1406static __init void event_test_stuff(void)
1407{
1408 struct task_struct *test_thread;
1409
1410 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1411 msleep(1);
1412 kthread_stop(test_thread);
1413}
1414
1415/*
1416 * For every trace event defined, we will test each trace point separately,
1417 * and then by groups, and finally all trace points.
1418 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001419static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04001420{
1421 struct ftrace_event_call *call;
1422 struct event_subsystem *system;
Steven Rostedte6187002009-04-15 13:36:40 -04001423 int ret;
1424
1425 pr_info("Running tests on trace events:\n");
1426
1427 list_for_each_entry(call, &ftrace_events, list) {
1428
Steven Rostedt22392912010-04-21 12:27:06 -04001429 /* Only test those that have a probe */
1430 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04001431 continue;
1432
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04001433/*
1434 * Testing syscall events here is pretty useless, but
1435 * we still do it if configured. But this is time consuming.
1436 * What we really need is a user thread to perform the
1437 * syscalls as we test.
1438 */
1439#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04001440 if (call->class->system &&
1441 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04001442 continue;
1443#endif
1444
Steven Rostedte6187002009-04-15 13:36:40 -04001445 pr_info("Testing event %s: ", call->name);
1446
1447 /*
1448 * If an event is already enabled, someone is using
1449 * it and the self test should not be on.
1450 */
1451 if (call->enabled) {
1452 pr_warning("Enabled event during self test!\n");
1453 WARN_ON_ONCE(1);
1454 continue;
1455 }
1456
Zhaolei0e907c92009-05-25 18:13:59 +08001457 ftrace_event_enable_disable(call, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001458 event_test_stuff();
Zhaolei0e907c92009-05-25 18:13:59 +08001459 ftrace_event_enable_disable(call, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001460
1461 pr_cont("OK\n");
1462 }
1463
1464 /* Now test at the sub system level */
1465
1466 pr_info("Running tests on trace event systems:\n");
1467
1468 list_for_each_entry(system, &event_subsystems, list) {
1469
1470 /* the ftrace system is special, skip it */
1471 if (strcmp(system->name, "ftrace") == 0)
1472 continue;
1473
1474 pr_info("Testing event system %s: ", system->name);
1475
Li Zefan8f31bfe2009-05-08 10:31:42 +08001476 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001477 if (WARN_ON_ONCE(ret)) {
1478 pr_warning("error enabling system %s\n",
1479 system->name);
1480 continue;
1481 }
1482
1483 event_test_stuff();
1484
Li Zefan8f31bfe2009-05-08 10:31:42 +08001485 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001486 if (WARN_ON_ONCE(ret))
1487 pr_warning("error disabling system %s\n",
1488 system->name);
1489
1490 pr_cont("OK\n");
1491 }
1492
1493 /* Test with all events enabled */
1494
1495 pr_info("Running tests on all trace events:\n");
1496 pr_info("Testing all events: ");
1497
Li Zefan8f31bfe2009-05-08 10:31:42 +08001498 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04001499 if (WARN_ON_ONCE(ret)) {
Steven Rostedte6187002009-04-15 13:36:40 -04001500 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001501 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001502 }
1503
1504 event_test_stuff();
1505
1506 /* reset sysname */
Li Zefan8f31bfe2009-05-08 10:31:42 +08001507 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04001508 if (WARN_ON_ONCE(ret)) {
1509 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001510 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001511 }
1512
1513 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001514}
1515
1516#ifdef CONFIG_FUNCTION_TRACER
1517
Tejun Heo245b2e72009-06-24 15:13:48 +09001518static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001519
1520static void
1521function_test_events_call(unsigned long ip, unsigned long parent_ip)
1522{
1523 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001524 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001525 struct ftrace_entry *entry;
1526 unsigned long flags;
1527 long disabled;
1528 int resched;
1529 int cpu;
1530 int pc;
1531
1532 pc = preempt_count();
1533 resched = ftrace_preempt_disable();
1534 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09001535 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001536
1537 if (disabled != 1)
1538 goto out;
1539
1540 local_save_flags(flags);
1541
Steven Rostedte77405a2009-09-02 14:17:06 -04001542 event = trace_current_buffer_lock_reserve(&buffer,
1543 TRACE_FN, sizeof(*entry),
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001544 flags, pc);
1545 if (!event)
1546 goto out;
1547 entry = ring_buffer_event_data(event);
1548 entry->ip = ip;
1549 entry->parent_ip = parent_ip;
1550
Steven Rostedte77405a2009-09-02 14:17:06 -04001551 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001552
1553 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09001554 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001555 ftrace_preempt_enable(resched);
1556}
1557
1558static struct ftrace_ops trace_ops __initdata =
1559{
1560 .func = function_test_events_call,
1561};
1562
1563static __init void event_trace_self_test_with_function(void)
1564{
1565 register_ftrace_function(&trace_ops);
1566 pr_info("Running tests again, along with the function tracer\n");
1567 event_trace_self_tests();
1568 unregister_ftrace_function(&trace_ops);
1569}
1570#else
1571static __init void event_trace_self_test_with_function(void)
1572{
1573}
1574#endif
1575
1576static __init int event_trace_self_tests_init(void)
1577{
Li Zefan020e5f82009-07-01 10:47:05 +08001578 if (!tracing_selftest_disabled) {
1579 event_trace_self_tests();
1580 event_trace_self_test_with_function();
1581 }
Steven Rostedte6187002009-04-15 13:36:40 -04001582
1583 return 0;
1584}
1585
Steven Rostedt28d20e22009-04-20 12:12:44 -04001586late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001587
1588#endif