Commit | Line | Data |
---|---|---|
b77e38aa SR |
1 | /* |
2 | * event tracer | |
3 | * | |
4 | * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> | |
5 | * | |
981d081e SR |
6 | * - Added format output of fields of the trace point. |
7 | * This was based off of work by Tom Zanussi <tzanussi@gmail.com>. | |
8 | * | |
b77e38aa SR |
9 | */ |
10 | ||
e6187007 SR |
11 | #include <linux/workqueue.h> |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/kthread.h> | |
b77e38aa SR |
14 | #include <linux/debugfs.h> |
15 | #include <linux/uaccess.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/ctype.h> | |
5a0e3ad6 | 18 | #include <linux/slab.h> |
e6187007 | 19 | #include <linux/delay.h> |
b77e38aa | 20 | |
020e5f85 LZ |
21 | #include <asm/setup.h> |
22 | ||
91729ef9 | 23 | #include "trace_output.h" |
b77e38aa | 24 | |
4e5292ea | 25 | #undef TRACE_SYSTEM |
b628b3e6 SR |
26 | #define TRACE_SYSTEM "TRACE_SYSTEM" |
27 | ||
20c8928a | 28 | DEFINE_MUTEX(event_mutex); |
11a241a3 | 29 | |
a59fd602 | 30 | LIST_HEAD(ftrace_events); |
8728fe50 | 31 | LIST_HEAD(ftrace_common_fields); |
a59fd602 | 32 | |
2e33af02 SR |
33 | struct list_head * |
34 | trace_get_fields(struct ftrace_event_call *event_call) | |
35 | { | |
36 | if (!event_call->class->get_fields) | |
37 | return &event_call->class->fields; | |
38 | return event_call->class->get_fields(event_call); | |
39 | } | |
40 | ||
8728fe50 LZ |
41 | static int __trace_define_field(struct list_head *head, const char *type, |
42 | const char *name, int offset, int size, | |
43 | int is_signed, int filter_type) | |
cf027f64 TZ |
44 | { |
45 | struct ftrace_event_field *field; | |
46 | ||
fe9f57f2 | 47 | field = kzalloc(sizeof(*field), GFP_KERNEL); |
cf027f64 TZ |
48 | if (!field) |
49 | goto err; | |
fe9f57f2 | 50 | |
cf027f64 TZ |
51 | field->name = kstrdup(name, GFP_KERNEL); |
52 | if (!field->name) | |
53 | goto err; | |
fe9f57f2 | 54 | |
cf027f64 TZ |
55 | field->type = kstrdup(type, GFP_KERNEL); |
56 | if (!field->type) | |
57 | goto err; | |
fe9f57f2 | 58 | |
43b51ead LZ |
59 | if (filter_type == FILTER_OTHER) |
60 | field->filter_type = filter_assign_type(type); | |
61 | else | |
62 | field->filter_type = filter_type; | |
63 | ||
cf027f64 TZ |
64 | field->offset = offset; |
65 | field->size = size; | |
a118e4d1 | 66 | field->is_signed = is_signed; |
aa38e9fc | 67 | |
2e33af02 | 68 | list_add(&field->link, head); |
cf027f64 TZ |
69 | |
70 | return 0; | |
fe9f57f2 | 71 | |
cf027f64 | 72 | err: |
7b60997f | 73 | if (field) |
cf027f64 | 74 | kfree(field->name); |
cf027f64 | 75 | kfree(field); |
fe9f57f2 | 76 | |
cf027f64 TZ |
77 | return -ENOMEM; |
78 | } | |
8728fe50 LZ |
79 | |
80 | int trace_define_field(struct ftrace_event_call *call, const char *type, | |
81 | const char *name, int offset, int size, int is_signed, | |
82 | int filter_type) | |
83 | { | |
84 | struct list_head *head; | |
85 | ||
86 | if (WARN_ON(!call->class)) | |
87 | return 0; | |
88 | ||
89 | head = trace_get_fields(call); | |
90 | return __trace_define_field(head, type, name, offset, size, | |
91 | is_signed, filter_type); | |
92 | } | |
17c873ec | 93 | EXPORT_SYMBOL_GPL(trace_define_field); |
cf027f64 | 94 | |
e647d6b3 | 95 | #define __common_field(type, item) \ |
8728fe50 LZ |
96 | ret = __trace_define_field(&ftrace_common_fields, #type, \ |
97 | "common_" #item, \ | |
98 | offsetof(typeof(ent), item), \ | |
99 | sizeof(ent.item), \ | |
100 | is_signed_type(type), FILTER_OTHER); \ | |
e647d6b3 LZ |
101 | if (ret) \ |
102 | return ret; | |
103 | ||
8728fe50 | 104 | static int trace_define_common_fields(void) |
e647d6b3 LZ |
105 | { |
106 | int ret; | |
107 | struct trace_entry ent; | |
108 | ||
109 | __common_field(unsigned short, type); | |
110 | __common_field(unsigned char, flags); | |
111 | __common_field(unsigned char, preempt_count); | |
112 | __common_field(int, pid); | |
637e7e86 | 113 | __common_field(int, lock_depth); |
e647d6b3 LZ |
114 | |
115 | return ret; | |
116 | } | |
117 | ||
bd1a5c84 | 118 | void trace_destroy_fields(struct ftrace_event_call *call) |
2df75e41 LZ |
119 | { |
120 | struct ftrace_event_field *field, *next; | |
2e33af02 | 121 | struct list_head *head; |
2df75e41 | 122 | |
2e33af02 SR |
123 | head = trace_get_fields(call); |
124 | list_for_each_entry_safe(field, next, head, link) { | |
2df75e41 LZ |
125 | list_del(&field->link); |
126 | kfree(field->type); | |
127 | kfree(field->name); | |
128 | kfree(field); | |
129 | } | |
130 | } | |
131 | ||
87d9b4e1 LZ |
132 | int trace_event_raw_init(struct ftrace_event_call *call) |
133 | { | |
134 | int id; | |
135 | ||
80decc70 | 136 | id = register_ftrace_event(&call->event); |
87d9b4e1 LZ |
137 | if (!id) |
138 | return -ENODEV; | |
87d9b4e1 LZ |
139 | |
140 | return 0; | |
141 | } | |
142 | EXPORT_SYMBOL_GPL(trace_event_raw_init); | |
143 | ||
a1d0ce82 SR |
144 | int ftrace_event_reg(struct ftrace_event_call *call, enum trace_reg type) |
145 | { | |
146 | switch (type) { | |
147 | case TRACE_REG_REGISTER: | |
148 | return tracepoint_probe_register(call->name, | |
149 | call->class->probe, | |
150 | call); | |
151 | case TRACE_REG_UNREGISTER: | |
152 | tracepoint_probe_unregister(call->name, | |
153 | call->class->probe, | |
154 | call); | |
155 | return 0; | |
156 | ||
157 | #ifdef CONFIG_PERF_EVENTS | |
158 | case TRACE_REG_PERF_REGISTER: | |
159 | return tracepoint_probe_register(call->name, | |
160 | call->class->perf_probe, | |
161 | call); | |
162 | case TRACE_REG_PERF_UNREGISTER: | |
163 | tracepoint_probe_unregister(call->name, | |
164 | call->class->perf_probe, | |
165 | call); | |
166 | return 0; | |
167 | #endif | |
168 | } | |
169 | return 0; | |
170 | } | |
171 | EXPORT_SYMBOL_GPL(ftrace_event_reg); | |
172 | ||
e870e9a1 LZ |
173 | void trace_event_enable_cmd_record(bool enable) |
174 | { | |
175 | struct ftrace_event_call *call; | |
176 | ||
177 | mutex_lock(&event_mutex); | |
178 | list_for_each_entry(call, &ftrace_events, list) { | |
179 | if (!(call->flags & TRACE_EVENT_FL_ENABLED)) | |
180 | continue; | |
181 | ||
182 | if (enable) { | |
183 | tracing_start_cmdline_record(); | |
184 | call->flags |= TRACE_EVENT_FL_RECORDED_CMD; | |
185 | } else { | |
186 | tracing_stop_cmdline_record(); | |
187 | call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD; | |
188 | } | |
189 | } | |
190 | mutex_unlock(&event_mutex); | |
191 | } | |
192 | ||
3b8e4273 | 193 | static int ftrace_event_enable_disable(struct ftrace_event_call *call, |
fd994989 SR |
194 | int enable) |
195 | { | |
3b8e4273 LZ |
196 | int ret = 0; |
197 | ||
fd994989 SR |
198 | switch (enable) { |
199 | case 0: | |
553552ce SR |
200 | if (call->flags & TRACE_EVENT_FL_ENABLED) { |
201 | call->flags &= ~TRACE_EVENT_FL_ENABLED; | |
e870e9a1 LZ |
202 | if (call->flags & TRACE_EVENT_FL_RECORDED_CMD) { |
203 | tracing_stop_cmdline_record(); | |
204 | call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD; | |
205 | } | |
a1d0ce82 | 206 | call->class->reg(call, TRACE_REG_UNREGISTER); |
fd994989 | 207 | } |
fd994989 SR |
208 | break; |
209 | case 1: | |
553552ce | 210 | if (!(call->flags & TRACE_EVENT_FL_ENABLED)) { |
e870e9a1 LZ |
211 | if (trace_flags & TRACE_ITER_RECORD_CMD) { |
212 | tracing_start_cmdline_record(); | |
213 | call->flags |= TRACE_EVENT_FL_RECORDED_CMD; | |
214 | } | |
a1d0ce82 | 215 | ret = call->class->reg(call, TRACE_REG_REGISTER); |
3b8e4273 LZ |
216 | if (ret) { |
217 | tracing_stop_cmdline_record(); | |
218 | pr_info("event trace: Could not enable event " | |
219 | "%s\n", call->name); | |
220 | break; | |
221 | } | |
553552ce | 222 | call->flags |= TRACE_EVENT_FL_ENABLED; |
fd994989 | 223 | } |
fd994989 SR |
224 | break; |
225 | } | |
3b8e4273 LZ |
226 | |
227 | return ret; | |
fd994989 SR |
228 | } |
229 | ||
0e907c99 Z |
230 | static void ftrace_clear_events(void) |
231 | { | |
232 | struct ftrace_event_call *call; | |
233 | ||
234 | mutex_lock(&event_mutex); | |
235 | list_for_each_entry(call, &ftrace_events, list) { | |
236 | ftrace_event_enable_disable(call, 0); | |
237 | } | |
238 | mutex_unlock(&event_mutex); | |
239 | } | |
240 | ||
8f31bfe5 LZ |
241 | /* |
242 | * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events. | |
243 | */ | |
244 | static int __ftrace_set_clr_event(const char *match, const char *sub, | |
245 | const char *event, int set) | |
b77e38aa | 246 | { |
a59fd602 | 247 | struct ftrace_event_call *call; |
29f93943 | 248 | int ret = -EINVAL; |
8f31bfe5 LZ |
249 | |
250 | mutex_lock(&event_mutex); | |
251 | list_for_each_entry(call, &ftrace_events, list) { | |
252 | ||
a1d0ce82 | 253 | if (!call->name || !call->class || !call->class->reg) |
8f31bfe5 LZ |
254 | continue; |
255 | ||
256 | if (match && | |
257 | strcmp(match, call->name) != 0 && | |
8f082018 | 258 | strcmp(match, call->class->system) != 0) |
8f31bfe5 LZ |
259 | continue; |
260 | ||
8f082018 | 261 | if (sub && strcmp(sub, call->class->system) != 0) |
8f31bfe5 LZ |
262 | continue; |
263 | ||
264 | if (event && strcmp(event, call->name) != 0) | |
265 | continue; | |
266 | ||
267 | ftrace_event_enable_disable(call, set); | |
268 | ||
269 | ret = 0; | |
270 | } | |
271 | mutex_unlock(&event_mutex); | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
276 | static int ftrace_set_clr_event(char *buf, int set) | |
277 | { | |
b628b3e6 | 278 | char *event = NULL, *sub = NULL, *match; |
b628b3e6 SR |
279 | |
280 | /* | |
281 | * The buf format can be <subsystem>:<event-name> | |
282 | * *:<event-name> means any event by that name. | |
283 | * :<event-name> is the same. | |
284 | * | |
285 | * <subsystem>:* means all events in that subsystem | |
286 | * <subsystem>: means the same. | |
287 | * | |
288 | * <name> (no ':') means all events in a subsystem with | |
289 | * the name <name> or any event that matches <name> | |
290 | */ | |
291 | ||
292 | match = strsep(&buf, ":"); | |
293 | if (buf) { | |
294 | sub = match; | |
295 | event = buf; | |
296 | match = NULL; | |
297 | ||
298 | if (!strlen(sub) || strcmp(sub, "*") == 0) | |
299 | sub = NULL; | |
300 | if (!strlen(event) || strcmp(event, "*") == 0) | |
301 | event = NULL; | |
302 | } | |
b77e38aa | 303 | |
8f31bfe5 | 304 | return __ftrace_set_clr_event(match, sub, event, set); |
b77e38aa SR |
305 | } |
306 | ||
4671c794 SR |
307 | /** |
308 | * trace_set_clr_event - enable or disable an event | |
309 | * @system: system name to match (NULL for any system) | |
310 | * @event: event name to match (NULL for all events, within system) | |
311 | * @set: 1 to enable, 0 to disable | |
312 | * | |
313 | * This is a way for other parts of the kernel to enable or disable | |
314 | * event recording. | |
315 | * | |
316 | * Returns 0 on success, -EINVAL if the parameters do not match any | |
317 | * registered events. | |
318 | */ | |
319 | int trace_set_clr_event(const char *system, const char *event, int set) | |
320 | { | |
321 | return __ftrace_set_clr_event(NULL, system, event, set); | |
322 | } | |
323 | ||
b77e38aa SR |
324 | /* 128 should be much more than enough */ |
325 | #define EVENT_BUF_SIZE 127 | |
326 | ||
327 | static ssize_t | |
328 | ftrace_event_write(struct file *file, const char __user *ubuf, | |
329 | size_t cnt, loff_t *ppos) | |
330 | { | |
48966364 | 331 | struct trace_parser parser; |
4ba7978e | 332 | ssize_t read, ret; |
b77e38aa | 333 | |
4ba7978e | 334 | if (!cnt) |
b77e38aa SR |
335 | return 0; |
336 | ||
1852fcce SR |
337 | ret = tracing_update_buffers(); |
338 | if (ret < 0) | |
339 | return ret; | |
340 | ||
48966364 | 341 | if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1)) |
b77e38aa SR |
342 | return -ENOMEM; |
343 | ||
48966364 | 344 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
345 | ||
4ba7978e | 346 | if (read >= 0 && trace_parser_loaded((&parser))) { |
48966364 | 347 | int set = 1; |
b77e38aa | 348 | |
48966364 | 349 | if (*parser.buffer == '!') |
b77e38aa | 350 | set = 0; |
b77e38aa | 351 | |
48966364 | 352 | parser.buffer[parser.idx] = 0; |
353 | ||
354 | ret = ftrace_set_clr_event(parser.buffer + !set, set); | |
b77e38aa | 355 | if (ret) |
48966364 | 356 | goto out_put; |
b77e38aa | 357 | } |
b77e38aa SR |
358 | |
359 | ret = read; | |
360 | ||
48966364 | 361 | out_put: |
362 | trace_parser_put(&parser); | |
b77e38aa SR |
363 | |
364 | return ret; | |
365 | } | |
366 | ||
367 | static void * | |
368 | t_next(struct seq_file *m, void *v, loff_t *pos) | |
369 | { | |
30bd39cd | 370 | struct ftrace_event_call *call = v; |
b77e38aa SR |
371 | |
372 | (*pos)++; | |
373 | ||
30bd39cd | 374 | list_for_each_entry_continue(call, &ftrace_events, list) { |
40e26815 SR |
375 | /* |
376 | * The ftrace subsystem is for showing formats only. | |
377 | * They can not be enabled or disabled via the event files. | |
378 | */ | |
a1d0ce82 | 379 | if (call->class && call->class->reg) |
30bd39cd | 380 | return call; |
40e26815 | 381 | } |
b77e38aa | 382 | |
30bd39cd | 383 | return NULL; |
b77e38aa SR |
384 | } |
385 | ||
386 | static void *t_start(struct seq_file *m, loff_t *pos) | |
387 | { | |
30bd39cd | 388 | struct ftrace_event_call *call; |
e1c7e2a6 LZ |
389 | loff_t l; |
390 | ||
20c8928a | 391 | mutex_lock(&event_mutex); |
e1c7e2a6 | 392 | |
30bd39cd | 393 | call = list_entry(&ftrace_events, struct ftrace_event_call, list); |
e1c7e2a6 | 394 | for (l = 0; l <= *pos; ) { |
30bd39cd | 395 | call = t_next(m, call, &l); |
e1c7e2a6 LZ |
396 | if (!call) |
397 | break; | |
398 | } | |
399 | return call; | |
b77e38aa SR |
400 | } |
401 | ||
402 | static void * | |
403 | s_next(struct seq_file *m, void *v, loff_t *pos) | |
404 | { | |
30bd39cd | 405 | struct ftrace_event_call *call = v; |
b77e38aa SR |
406 | |
407 | (*pos)++; | |
408 | ||
30bd39cd | 409 | list_for_each_entry_continue(call, &ftrace_events, list) { |
553552ce | 410 | if (call->flags & TRACE_EVENT_FL_ENABLED) |
30bd39cd | 411 | return call; |
b77e38aa SR |
412 | } |
413 | ||
30bd39cd | 414 | return NULL; |
b77e38aa SR |
415 | } |
416 | ||
417 | static void *s_start(struct seq_file *m, loff_t *pos) | |
418 | { | |
30bd39cd | 419 | struct ftrace_event_call *call; |
e1c7e2a6 LZ |
420 | loff_t l; |
421 | ||
20c8928a | 422 | mutex_lock(&event_mutex); |
e1c7e2a6 | 423 | |
30bd39cd | 424 | call = list_entry(&ftrace_events, struct ftrace_event_call, list); |
e1c7e2a6 | 425 | for (l = 0; l <= *pos; ) { |
30bd39cd | 426 | call = s_next(m, call, &l); |
e1c7e2a6 LZ |
427 | if (!call) |
428 | break; | |
429 | } | |
430 | return call; | |
b77e38aa SR |
431 | } |
432 | ||
433 | static int t_show(struct seq_file *m, void *v) | |
434 | { | |
435 | struct ftrace_event_call *call = v; | |
436 | ||
8f082018 SR |
437 | if (strcmp(call->class->system, TRACE_SYSTEM) != 0) |
438 | seq_printf(m, "%s:", call->class->system); | |
b77e38aa SR |
439 | seq_printf(m, "%s\n", call->name); |
440 | ||
441 | return 0; | |
442 | } | |
443 | ||
444 | static void t_stop(struct seq_file *m, void *p) | |
445 | { | |
20c8928a | 446 | mutex_unlock(&event_mutex); |
b77e38aa SR |
447 | } |
448 | ||
449 | static int | |
450 | ftrace_event_seq_open(struct inode *inode, struct file *file) | |
451 | { | |
b77e38aa SR |
452 | const struct seq_operations *seq_ops; |
453 | ||
454 | if ((file->f_mode & FMODE_WRITE) && | |
8650ae32 | 455 | (file->f_flags & O_TRUNC)) |
b77e38aa SR |
456 | ftrace_clear_events(); |
457 | ||
458 | seq_ops = inode->i_private; | |
20c8928a | 459 | return seq_open(file, seq_ops); |
b77e38aa SR |
460 | } |
461 | ||
1473e441 SR |
462 | static ssize_t |
463 | event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, | |
464 | loff_t *ppos) | |
465 | { | |
466 | struct ftrace_event_call *call = filp->private_data; | |
467 | char *buf; | |
468 | ||
553552ce | 469 | if (call->flags & TRACE_EVENT_FL_ENABLED) |
1473e441 SR |
470 | buf = "1\n"; |
471 | else | |
472 | buf = "0\n"; | |
473 | ||
474 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); | |
475 | } | |
476 | ||
477 | static ssize_t | |
478 | event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
479 | loff_t *ppos) | |
480 | { | |
481 | struct ftrace_event_call *call = filp->private_data; | |
482 | char buf[64]; | |
483 | unsigned long val; | |
484 | int ret; | |
485 | ||
486 | if (cnt >= sizeof(buf)) | |
487 | return -EINVAL; | |
488 | ||
489 | if (copy_from_user(&buf, ubuf, cnt)) | |
490 | return -EFAULT; | |
491 | ||
492 | buf[cnt] = 0; | |
493 | ||
494 | ret = strict_strtoul(buf, 10, &val); | |
495 | if (ret < 0) | |
496 | return ret; | |
497 | ||
1852fcce SR |
498 | ret = tracing_update_buffers(); |
499 | if (ret < 0) | |
500 | return ret; | |
501 | ||
1473e441 SR |
502 | switch (val) { |
503 | case 0: | |
1473e441 | 504 | case 1: |
11a241a3 | 505 | mutex_lock(&event_mutex); |
3b8e4273 | 506 | ret = ftrace_event_enable_disable(call, val); |
11a241a3 | 507 | mutex_unlock(&event_mutex); |
1473e441 SR |
508 | break; |
509 | ||
510 | default: | |
511 | return -EINVAL; | |
512 | } | |
513 | ||
514 | *ppos += cnt; | |
515 | ||
3b8e4273 | 516 | return ret ? ret : cnt; |
1473e441 SR |
517 | } |
518 | ||
8ae79a13 SR |
519 | static ssize_t |
520 | system_enable_read(struct file *filp, char __user *ubuf, size_t cnt, | |
521 | loff_t *ppos) | |
522 | { | |
c142b15d | 523 | const char set_to_char[4] = { '?', '0', '1', 'X' }; |
8ae79a13 SR |
524 | const char *system = filp->private_data; |
525 | struct ftrace_event_call *call; | |
526 | char buf[2]; | |
c142b15d | 527 | int set = 0; |
8ae79a13 SR |
528 | int ret; |
529 | ||
8ae79a13 SR |
530 | mutex_lock(&event_mutex); |
531 | list_for_each_entry(call, &ftrace_events, list) { | |
a1d0ce82 | 532 | if (!call->name || !call->class || !call->class->reg) |
8ae79a13 SR |
533 | continue; |
534 | ||
8f082018 | 535 | if (system && strcmp(call->class->system, system) != 0) |
8ae79a13 SR |
536 | continue; |
537 | ||
538 | /* | |
539 | * We need to find out if all the events are set | |
540 | * or if all events or cleared, or if we have | |
541 | * a mixture. | |
542 | */ | |
553552ce | 543 | set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED)); |
c142b15d | 544 | |
8ae79a13 SR |
545 | /* |
546 | * If we have a mixture, no need to look further. | |
547 | */ | |
c142b15d | 548 | if (set == 3) |
8ae79a13 SR |
549 | break; |
550 | } | |
551 | mutex_unlock(&event_mutex); | |
552 | ||
c142b15d | 553 | buf[0] = set_to_char[set]; |
8ae79a13 | 554 | buf[1] = '\n'; |
8ae79a13 SR |
555 | |
556 | ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); | |
557 | ||
558 | return ret; | |
559 | } | |
560 | ||
561 | static ssize_t | |
562 | system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
563 | loff_t *ppos) | |
564 | { | |
565 | const char *system = filp->private_data; | |
566 | unsigned long val; | |
8ae79a13 SR |
567 | char buf[64]; |
568 | ssize_t ret; | |
569 | ||
570 | if (cnt >= sizeof(buf)) | |
571 | return -EINVAL; | |
572 | ||
573 | if (copy_from_user(&buf, ubuf, cnt)) | |
574 | return -EFAULT; | |
575 | ||
576 | buf[cnt] = 0; | |
577 | ||
578 | ret = strict_strtoul(buf, 10, &val); | |
579 | if (ret < 0) | |
580 | return ret; | |
581 | ||
582 | ret = tracing_update_buffers(); | |
583 | if (ret < 0) | |
584 | return ret; | |
585 | ||
8f31bfe5 | 586 | if (val != 0 && val != 1) |
8ae79a13 | 587 | return -EINVAL; |
8ae79a13 | 588 | |
8f31bfe5 | 589 | ret = __ftrace_set_clr_event(NULL, system, NULL, val); |
8ae79a13 | 590 | if (ret) |
8f31bfe5 | 591 | goto out; |
8ae79a13 SR |
592 | |
593 | ret = cnt; | |
594 | ||
8f31bfe5 | 595 | out: |
8ae79a13 SR |
596 | *ppos += cnt; |
597 | ||
598 | return ret; | |
599 | } | |
600 | ||
2a37a3df SR |
601 | enum { |
602 | FORMAT_HEADER = 1, | |
86397dc3 LZ |
603 | FORMAT_FIELD_SEPERATOR = 2, |
604 | FORMAT_PRINTFMT = 3, | |
2a37a3df SR |
605 | }; |
606 | ||
607 | static void *f_next(struct seq_file *m, void *v, loff_t *pos) | |
981d081e | 608 | { |
2a37a3df | 609 | struct ftrace_event_call *call = m->private; |
5a65e956 | 610 | struct ftrace_event_field *field; |
86397dc3 LZ |
611 | struct list_head *common_head = &ftrace_common_fields; |
612 | struct list_head *head = trace_get_fields(call); | |
981d081e | 613 | |
2a37a3df | 614 | (*pos)++; |
5a65e956 | 615 | |
2a37a3df SR |
616 | switch ((unsigned long)v) { |
617 | case FORMAT_HEADER: | |
86397dc3 LZ |
618 | if (unlikely(list_empty(common_head))) |
619 | return NULL; | |
620 | ||
621 | field = list_entry(common_head->prev, | |
622 | struct ftrace_event_field, link); | |
623 | return field; | |
5a65e956 | 624 | |
86397dc3 | 625 | case FORMAT_FIELD_SEPERATOR: |
2a37a3df SR |
626 | if (unlikely(list_empty(head))) |
627 | return NULL; | |
5a65e956 | 628 | |
2a37a3df SR |
629 | field = list_entry(head->prev, struct ftrace_event_field, link); |
630 | return field; | |
5a65e956 | 631 | |
2a37a3df SR |
632 | case FORMAT_PRINTFMT: |
633 | /* all done */ | |
634 | return NULL; | |
5a65e956 LJ |
635 | } |
636 | ||
2a37a3df | 637 | field = v; |
86397dc3 LZ |
638 | if (field->link.prev == common_head) |
639 | return (void *)FORMAT_FIELD_SEPERATOR; | |
640 | else if (field->link.prev == head) | |
2a37a3df SR |
641 | return (void *)FORMAT_PRINTFMT; |
642 | ||
643 | field = list_entry(field->link.prev, struct ftrace_event_field, link); | |
644 | ||
2a37a3df | 645 | return field; |
8728fe50 | 646 | } |
5a65e956 | 647 | |
2a37a3df | 648 | static void *f_start(struct seq_file *m, loff_t *pos) |
8728fe50 | 649 | { |
2a37a3df SR |
650 | loff_t l = 0; |
651 | void *p; | |
5a65e956 | 652 | |
2a37a3df SR |
653 | /* Start by showing the header */ |
654 | if (!*pos) | |
655 | return (void *)FORMAT_HEADER; | |
656 | ||
657 | p = (void *)FORMAT_HEADER; | |
658 | do { | |
659 | p = f_next(m, p, &l); | |
660 | } while (p && l < *pos); | |
661 | ||
662 | return p; | |
663 | } | |
664 | ||
665 | static int f_show(struct seq_file *m, void *v) | |
666 | { | |
667 | struct ftrace_event_call *call = m->private; | |
668 | struct ftrace_event_field *field; | |
669 | const char *array_descriptor; | |
670 | ||
671 | switch ((unsigned long)v) { | |
672 | case FORMAT_HEADER: | |
673 | seq_printf(m, "name: %s\n", call->name); | |
674 | seq_printf(m, "ID: %d\n", call->event.type); | |
675 | seq_printf(m, "format:\n"); | |
8728fe50 | 676 | return 0; |
5a65e956 | 677 | |
86397dc3 LZ |
678 | case FORMAT_FIELD_SEPERATOR: |
679 | seq_putc(m, '\n'); | |
680 | return 0; | |
681 | ||
2a37a3df SR |
682 | case FORMAT_PRINTFMT: |
683 | seq_printf(m, "\nprint fmt: %s\n", | |
684 | call->print_fmt); | |
685 | return 0; | |
981d081e | 686 | } |
8728fe50 | 687 | |
2a37a3df | 688 | field = v; |
8728fe50 | 689 | |
2a37a3df SR |
690 | /* |
691 | * Smartly shows the array type(except dynamic array). | |
692 | * Normal: | |
693 | * field:TYPE VAR | |
694 | * If TYPE := TYPE[LEN], it is shown: | |
695 | * field:TYPE VAR[LEN] | |
696 | */ | |
697 | array_descriptor = strchr(field->type, '['); | |
8728fe50 | 698 | |
2a37a3df SR |
699 | if (!strncmp(field->type, "__data_loc", 10)) |
700 | array_descriptor = NULL; | |
8728fe50 | 701 | |
2a37a3df SR |
702 | if (!array_descriptor) |
703 | seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", | |
704 | field->type, field->name, field->offset, | |
705 | field->size, !!field->is_signed); | |
706 | else | |
707 | seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", | |
708 | (int)(array_descriptor - field->type), | |
709 | field->type, field->name, | |
710 | array_descriptor, field->offset, | |
711 | field->size, !!field->is_signed); | |
8728fe50 | 712 | |
2a37a3df SR |
713 | return 0; |
714 | } | |
5a65e956 | 715 | |
2a37a3df SR |
716 | static void f_stop(struct seq_file *m, void *p) |
717 | { | |
718 | } | |
981d081e | 719 | |
2a37a3df SR |
720 | static const struct seq_operations trace_format_seq_ops = { |
721 | .start = f_start, | |
722 | .next = f_next, | |
723 | .stop = f_stop, | |
724 | .show = f_show, | |
725 | }; | |
726 | ||
727 | static int trace_format_open(struct inode *inode, struct file *file) | |
728 | { | |
729 | struct ftrace_event_call *call = inode->i_private; | |
730 | struct seq_file *m; | |
731 | int ret; | |
732 | ||
733 | ret = seq_open(file, &trace_format_seq_ops); | |
734 | if (ret < 0) | |
735 | return ret; | |
736 | ||
737 | m = file->private_data; | |
738 | m->private = call; | |
739 | ||
740 | return 0; | |
981d081e SR |
741 | } |
742 | ||
23725aee PZ |
743 | static ssize_t |
744 | event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) | |
745 | { | |
746 | struct ftrace_event_call *call = filp->private_data; | |
747 | struct trace_seq *s; | |
748 | int r; | |
749 | ||
750 | if (*ppos) | |
751 | return 0; | |
752 | ||
753 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
754 | if (!s) | |
755 | return -ENOMEM; | |
756 | ||
757 | trace_seq_init(s); | |
32c0edae | 758 | trace_seq_printf(s, "%d\n", call->event.type); |
23725aee PZ |
759 | |
760 | r = simple_read_from_buffer(ubuf, cnt, ppos, | |
761 | s->buffer, s->len); | |
762 | kfree(s); | |
763 | return r; | |
764 | } | |
765 | ||
7ce7e424 TZ |
766 | static ssize_t |
767 | event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, | |
768 | loff_t *ppos) | |
769 | { | |
770 | struct ftrace_event_call *call = filp->private_data; | |
771 | struct trace_seq *s; | |
772 | int r; | |
773 | ||
774 | if (*ppos) | |
775 | return 0; | |
776 | ||
777 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
778 | if (!s) | |
779 | return -ENOMEM; | |
780 | ||
781 | trace_seq_init(s); | |
782 | ||
8b372562 | 783 | print_event_filter(call, s); |
4bda2d51 | 784 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
7ce7e424 TZ |
785 | |
786 | kfree(s); | |
787 | ||
788 | return r; | |
789 | } | |
790 | ||
791 | static ssize_t | |
792 | event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
793 | loff_t *ppos) | |
794 | { | |
795 | struct ftrace_event_call *call = filp->private_data; | |
8b372562 | 796 | char *buf; |
7ce7e424 TZ |
797 | int err; |
798 | ||
8b372562 | 799 | if (cnt >= PAGE_SIZE) |
7ce7e424 TZ |
800 | return -EINVAL; |
801 | ||
8b372562 TZ |
802 | buf = (char *)__get_free_page(GFP_TEMPORARY); |
803 | if (!buf) | |
7ce7e424 TZ |
804 | return -ENOMEM; |
805 | ||
8b372562 TZ |
806 | if (copy_from_user(buf, ubuf, cnt)) { |
807 | free_page((unsigned long) buf); | |
808 | return -EFAULT; | |
7ce7e424 | 809 | } |
8b372562 | 810 | buf[cnt] = '\0'; |
7ce7e424 | 811 | |
8b372562 TZ |
812 | err = apply_event_filter(call, buf); |
813 | free_page((unsigned long) buf); | |
814 | if (err < 0) | |
44e9c8b7 | 815 | return err; |
0a19e53c | 816 | |
7ce7e424 TZ |
817 | *ppos += cnt; |
818 | ||
819 | return cnt; | |
820 | } | |
821 | ||
cfb180f3 TZ |
822 | static ssize_t |
823 | subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, | |
824 | loff_t *ppos) | |
825 | { | |
826 | struct event_subsystem *system = filp->private_data; | |
827 | struct trace_seq *s; | |
828 | int r; | |
829 | ||
830 | if (*ppos) | |
831 | return 0; | |
832 | ||
833 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
834 | if (!s) | |
835 | return -ENOMEM; | |
836 | ||
837 | trace_seq_init(s); | |
838 | ||
8b372562 | 839 | print_subsystem_event_filter(system, s); |
4bda2d51 | 840 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
cfb180f3 TZ |
841 | |
842 | kfree(s); | |
843 | ||
844 | return r; | |
845 | } | |
846 | ||
847 | static ssize_t | |
848 | subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
849 | loff_t *ppos) | |
850 | { | |
851 | struct event_subsystem *system = filp->private_data; | |
8b372562 | 852 | char *buf; |
cfb180f3 TZ |
853 | int err; |
854 | ||
8b372562 | 855 | if (cnt >= PAGE_SIZE) |
cfb180f3 TZ |
856 | return -EINVAL; |
857 | ||
8b372562 TZ |
858 | buf = (char *)__get_free_page(GFP_TEMPORARY); |
859 | if (!buf) | |
cfb180f3 TZ |
860 | return -ENOMEM; |
861 | ||
8b372562 TZ |
862 | if (copy_from_user(buf, ubuf, cnt)) { |
863 | free_page((unsigned long) buf); | |
864 | return -EFAULT; | |
cfb180f3 | 865 | } |
8b372562 | 866 | buf[cnt] = '\0'; |
cfb180f3 | 867 | |
8b372562 TZ |
868 | err = apply_subsystem_event_filter(system, buf); |
869 | free_page((unsigned long) buf); | |
870 | if (err < 0) | |
44e9c8b7 | 871 | return err; |
cfb180f3 TZ |
872 | |
873 | *ppos += cnt; | |
874 | ||
875 | return cnt; | |
876 | } | |
877 | ||
d1b182a8 SR |
878 | static ssize_t |
879 | show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) | |
880 | { | |
881 | int (*func)(struct trace_seq *s) = filp->private_data; | |
882 | struct trace_seq *s; | |
883 | int r; | |
884 | ||
885 | if (*ppos) | |
886 | return 0; | |
887 | ||
888 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
889 | if (!s) | |
890 | return -ENOMEM; | |
891 | ||
892 | trace_seq_init(s); | |
893 | ||
894 | func(s); | |
895 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); | |
896 | ||
897 | kfree(s); | |
898 | ||
899 | return r; | |
900 | } | |
901 | ||
b77e38aa SR |
902 | static const struct seq_operations show_event_seq_ops = { |
903 | .start = t_start, | |
904 | .next = t_next, | |
905 | .show = t_show, | |
906 | .stop = t_stop, | |
907 | }; | |
908 | ||
909 | static const struct seq_operations show_set_event_seq_ops = { | |
910 | .start = s_start, | |
911 | .next = s_next, | |
912 | .show = t_show, | |
913 | .stop = t_stop, | |
914 | }; | |
915 | ||
2314c4ae SR |
916 | static const struct file_operations ftrace_avail_fops = { |
917 | .open = ftrace_event_seq_open, | |
918 | .read = seq_read, | |
919 | .llseek = seq_lseek, | |
920 | .release = seq_release, | |
921 | }; | |
922 | ||
b77e38aa SR |
923 | static const struct file_operations ftrace_set_event_fops = { |
924 | .open = ftrace_event_seq_open, | |
925 | .read = seq_read, | |
926 | .write = ftrace_event_write, | |
927 | .llseek = seq_lseek, | |
928 | .release = seq_release, | |
929 | }; | |
930 | ||
1473e441 SR |
931 | static const struct file_operations ftrace_enable_fops = { |
932 | .open = tracing_open_generic, | |
933 | .read = event_enable_read, | |
934 | .write = event_enable_write, | |
6038f373 | 935 | .llseek = default_llseek, |
1473e441 SR |
936 | }; |
937 | ||
981d081e | 938 | static const struct file_operations ftrace_event_format_fops = { |
2a37a3df SR |
939 | .open = trace_format_open, |
940 | .read = seq_read, | |
941 | .llseek = seq_lseek, | |
942 | .release = seq_release, | |
981d081e SR |
943 | }; |
944 | ||
23725aee PZ |
945 | static const struct file_operations ftrace_event_id_fops = { |
946 | .open = tracing_open_generic, | |
947 | .read = event_id_read, | |
6038f373 | 948 | .llseek = default_llseek, |
23725aee PZ |
949 | }; |
950 | ||
7ce7e424 TZ |
951 | static const struct file_operations ftrace_event_filter_fops = { |
952 | .open = tracing_open_generic, | |
953 | .read = event_filter_read, | |
954 | .write = event_filter_write, | |
6038f373 | 955 | .llseek = default_llseek, |
7ce7e424 TZ |
956 | }; |
957 | ||
cfb180f3 TZ |
958 | static const struct file_operations ftrace_subsystem_filter_fops = { |
959 | .open = tracing_open_generic, | |
960 | .read = subsystem_filter_read, | |
961 | .write = subsystem_filter_write, | |
6038f373 | 962 | .llseek = default_llseek, |
cfb180f3 TZ |
963 | }; |
964 | ||
8ae79a13 SR |
965 | static const struct file_operations ftrace_system_enable_fops = { |
966 | .open = tracing_open_generic, | |
967 | .read = system_enable_read, | |
968 | .write = system_enable_write, | |
6038f373 | 969 | .llseek = default_llseek, |
8ae79a13 SR |
970 | }; |
971 | ||
d1b182a8 SR |
972 | static const struct file_operations ftrace_show_header_fops = { |
973 | .open = tracing_open_generic, | |
974 | .read = show_header, | |
6038f373 | 975 | .llseek = default_llseek, |
d1b182a8 SR |
976 | }; |
977 | ||
1473e441 SR |
978 | static struct dentry *event_trace_events_dir(void) |
979 | { | |
980 | static struct dentry *d_tracer; | |
981 | static struct dentry *d_events; | |
982 | ||
983 | if (d_events) | |
984 | return d_events; | |
985 | ||
986 | d_tracer = tracing_init_dentry(); | |
987 | if (!d_tracer) | |
988 | return NULL; | |
989 | ||
990 | d_events = debugfs_create_dir("events", d_tracer); | |
991 | if (!d_events) | |
992 | pr_warning("Could not create debugfs " | |
993 | "'events' directory\n"); | |
994 | ||
995 | return d_events; | |
996 | } | |
997 | ||
6ecc2d1c SR |
998 | static LIST_HEAD(event_subsystems); |
999 | ||
1000 | static struct dentry * | |
1001 | event_subsystem_dir(const char *name, struct dentry *d_events) | |
1002 | { | |
1003 | struct event_subsystem *system; | |
e1112b4d | 1004 | struct dentry *entry; |
6ecc2d1c SR |
1005 | |
1006 | /* First see if we did not already create this dir */ | |
1007 | list_for_each_entry(system, &event_subsystems, list) { | |
dc82ec98 XG |
1008 | if (strcmp(system->name, name) == 0) { |
1009 | system->nr_events++; | |
6ecc2d1c | 1010 | return system->entry; |
dc82ec98 | 1011 | } |
6ecc2d1c SR |
1012 | } |
1013 | ||
1014 | /* need to create new entry */ | |
1015 | system = kmalloc(sizeof(*system), GFP_KERNEL); | |
1016 | if (!system) { | |
1017 | pr_warning("No memory to create event subsystem %s\n", | |
1018 | name); | |
1019 | return d_events; | |
1020 | } | |
1021 | ||
1022 | system->entry = debugfs_create_dir(name, d_events); | |
1023 | if (!system->entry) { | |
1024 | pr_warning("Could not create event subsystem %s\n", | |
1025 | name); | |
1026 | kfree(system); | |
1027 | return d_events; | |
1028 | } | |
1029 | ||
dc82ec98 | 1030 | system->nr_events = 1; |
6d723736 SR |
1031 | system->name = kstrdup(name, GFP_KERNEL); |
1032 | if (!system->name) { | |
1033 | debugfs_remove(system->entry); | |
1034 | kfree(system); | |
1035 | return d_events; | |
1036 | } | |
1037 | ||
6ecc2d1c SR |
1038 | list_add(&system->list, &event_subsystems); |
1039 | ||
30e673b2 | 1040 | system->filter = NULL; |
cfb180f3 | 1041 | |
8b372562 TZ |
1042 | system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL); |
1043 | if (!system->filter) { | |
1044 | pr_warning("Could not allocate filter for subsystem " | |
1045 | "'%s'\n", name); | |
1046 | return system->entry; | |
1047 | } | |
1048 | ||
e1112b4d TZ |
1049 | entry = debugfs_create_file("filter", 0644, system->entry, system, |
1050 | &ftrace_subsystem_filter_fops); | |
8b372562 TZ |
1051 | if (!entry) { |
1052 | kfree(system->filter); | |
1053 | system->filter = NULL; | |
e1112b4d TZ |
1054 | pr_warning("Could not create debugfs " |
1055 | "'%s/filter' entry\n", name); | |
8b372562 | 1056 | } |
e1112b4d | 1057 | |
f3f3f009 FW |
1058 | trace_create_file("enable", 0644, system->entry, |
1059 | (void *)system->name, | |
1060 | &ftrace_system_enable_fops); | |
8ae79a13 | 1061 | |
6ecc2d1c SR |
1062 | return system->entry; |
1063 | } | |
1064 | ||
1473e441 | 1065 | static int |
701970b3 SR |
1066 | event_create_dir(struct ftrace_event_call *call, struct dentry *d_events, |
1067 | const struct file_operations *id, | |
1068 | const struct file_operations *enable, | |
1069 | const struct file_operations *filter, | |
1070 | const struct file_operations *format) | |
1473e441 | 1071 | { |
2e33af02 | 1072 | struct list_head *head; |
fd994989 | 1073 | int ret; |
1473e441 | 1074 | |
6ecc2d1c SR |
1075 | /* |
1076 | * If the trace point header did not define TRACE_SYSTEM | |
1077 | * then the system would be called "TRACE_SYSTEM". | |
1078 | */ | |
8f082018 SR |
1079 | if (strcmp(call->class->system, TRACE_SYSTEM) != 0) |
1080 | d_events = event_subsystem_dir(call->class->system, d_events); | |
6ecc2d1c | 1081 | |
1473e441 SR |
1082 | call->dir = debugfs_create_dir(call->name, d_events); |
1083 | if (!call->dir) { | |
1084 | pr_warning("Could not create debugfs " | |
1085 | "'%s' directory\n", call->name); | |
1086 | return -1; | |
1087 | } | |
1088 | ||
a1d0ce82 | 1089 | if (call->class->reg) |
f3f3f009 FW |
1090 | trace_create_file("enable", 0644, call->dir, call, |
1091 | enable); | |
1473e441 | 1092 | |
2239291a | 1093 | #ifdef CONFIG_PERF_EVENTS |
a1d0ce82 | 1094 | if (call->event.type && call->class->reg) |
f3f3f009 FW |
1095 | trace_create_file("id", 0444, call->dir, call, |
1096 | id); | |
2239291a | 1097 | #endif |
23725aee | 1098 | |
c9d932cf LZ |
1099 | /* |
1100 | * Other events may have the same class. Only update | |
1101 | * the fields if they are not already defined. | |
1102 | */ | |
1103 | head = trace_get_fields(call); | |
1104 | if (list_empty(head)) { | |
1105 | ret = call->class->define_fields(call); | |
1106 | if (ret < 0) { | |
1107 | pr_warning("Could not initialize trace point" | |
1108 | " events/%s\n", call->name); | |
1109 | return ret; | |
cf027f64 TZ |
1110 | } |
1111 | } | |
c9d932cf LZ |
1112 | trace_create_file("filter", 0644, call->dir, call, |
1113 | filter); | |
cf027f64 | 1114 | |
f3f3f009 FW |
1115 | trace_create_file("format", 0444, call->dir, call, |
1116 | format); | |
6d723736 SR |
1117 | |
1118 | return 0; | |
1119 | } | |
1120 | ||
67ead0a6 LZ |
1121 | static int |
1122 | __trace_add_event_call(struct ftrace_event_call *call, struct module *mod, | |
1123 | const struct file_operations *id, | |
1124 | const struct file_operations *enable, | |
1125 | const struct file_operations *filter, | |
1126 | const struct file_operations *format) | |
bd1a5c84 MH |
1127 | { |
1128 | struct dentry *d_events; | |
1129 | int ret; | |
6d723736 | 1130 | |
67ead0a6 | 1131 | /* The linker may leave blanks */ |
bd1a5c84 MH |
1132 | if (!call->name) |
1133 | return -EINVAL; | |
701970b3 | 1134 | |
0405ab80 SR |
1135 | if (call->class->raw_init) { |
1136 | ret = call->class->raw_init(call); | |
bd1a5c84 MH |
1137 | if (ret < 0) { |
1138 | if (ret != -ENOSYS) | |
67ead0a6 LZ |
1139 | pr_warning("Could not initialize trace events/%s\n", |
1140 | call->name); | |
bd1a5c84 MH |
1141 | return ret; |
1142 | } | |
1143 | } | |
701970b3 | 1144 | |
bd1a5c84 MH |
1145 | d_events = event_trace_events_dir(); |
1146 | if (!d_events) | |
1147 | return -ENOENT; | |
1148 | ||
67ead0a6 | 1149 | ret = event_create_dir(call, d_events, id, enable, filter, format); |
88f70d75 MH |
1150 | if (!ret) |
1151 | list_add(&call->list, &ftrace_events); | |
67ead0a6 | 1152 | call->mod = mod; |
88f70d75 | 1153 | |
588bebb7 | 1154 | return ret; |
bd1a5c84 MH |
1155 | } |
1156 | ||
1157 | /* Add an additional event_call dynamically */ | |
1158 | int trace_add_event_call(struct ftrace_event_call *call) | |
1159 | { | |
1160 | int ret; | |
1161 | mutex_lock(&event_mutex); | |
67ead0a6 LZ |
1162 | ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops, |
1163 | &ftrace_enable_fops, | |
1164 | &ftrace_event_filter_fops, | |
1165 | &ftrace_event_format_fops); | |
bd1a5c84 MH |
1166 | mutex_unlock(&event_mutex); |
1167 | return ret; | |
1168 | } | |
701970b3 | 1169 | |
a2ca5e03 FW |
1170 | static void remove_subsystem_dir(const char *name) |
1171 | { | |
1172 | struct event_subsystem *system; | |
1173 | ||
1174 | if (strcmp(name, TRACE_SYSTEM) == 0) | |
1175 | return; | |
1176 | ||
1177 | list_for_each_entry(system, &event_subsystems, list) { | |
1178 | if (strcmp(system->name, name) == 0) { | |
1179 | if (!--system->nr_events) { | |
1180 | struct event_filter *filter = system->filter; | |
1181 | ||
1182 | debugfs_remove_recursive(system->entry); | |
1183 | list_del(&system->list); | |
1184 | if (filter) { | |
1185 | kfree(filter->filter_string); | |
1186 | kfree(filter); | |
1187 | } | |
1188 | kfree(system->name); | |
1189 | kfree(system); | |
1190 | } | |
1191 | break; | |
1192 | } | |
1193 | } | |
1194 | } | |
1195 | ||
4fead8e4 MH |
1196 | /* |
1197 | * Must be called under locking both of event_mutex and trace_event_mutex. | |
1198 | */ | |
bd1a5c84 MH |
1199 | static void __trace_remove_event_call(struct ftrace_event_call *call) |
1200 | { | |
1201 | ftrace_event_enable_disable(call, 0); | |
80decc70 SR |
1202 | if (call->event.funcs) |
1203 | __unregister_ftrace_event(&call->event); | |
bd1a5c84 MH |
1204 | debugfs_remove_recursive(call->dir); |
1205 | list_del(&call->list); | |
1206 | trace_destroy_fields(call); | |
1207 | destroy_preds(call); | |
8f082018 | 1208 | remove_subsystem_dir(call->class->system); |
bd1a5c84 MH |
1209 | } |
1210 | ||
1211 | /* Remove an event_call */ | |
1212 | void trace_remove_event_call(struct ftrace_event_call *call) | |
1213 | { | |
1214 | mutex_lock(&event_mutex); | |
4fead8e4 | 1215 | down_write(&trace_event_mutex); |
bd1a5c84 | 1216 | __trace_remove_event_call(call); |
4fead8e4 | 1217 | up_write(&trace_event_mutex); |
bd1a5c84 MH |
1218 | mutex_unlock(&event_mutex); |
1219 | } | |
1220 | ||
1221 | #define for_each_event(event, start, end) \ | |
1222 | for (event = start; \ | |
1223 | (unsigned long)event < (unsigned long)end; \ | |
1224 | event++) | |
1225 | ||
1226 | #ifdef CONFIG_MODULES | |
1227 | ||
1228 | static LIST_HEAD(ftrace_module_file_list); | |
1229 | ||
1230 | /* | |
1231 | * Modules must own their file_operations to keep up with | |
1232 | * reference counting. | |
1233 | */ | |
1234 | struct ftrace_module_file_ops { | |
1235 | struct list_head list; | |
1236 | struct module *mod; | |
1237 | struct file_operations id; | |
1238 | struct file_operations enable; | |
1239 | struct file_operations format; | |
1240 | struct file_operations filter; | |
1241 | }; | |
1242 | ||
701970b3 SR |
1243 | static struct ftrace_module_file_ops * |
1244 | trace_create_file_ops(struct module *mod) | |
1245 | { | |
1246 | struct ftrace_module_file_ops *file_ops; | |
1247 | ||
1248 | /* | |
1249 | * This is a bit of a PITA. To allow for correct reference | |
1250 | * counting, modules must "own" their file_operations. | |
1251 | * To do this, we allocate the file operations that will be | |
1252 | * used in the event directory. | |
1253 | */ | |
1254 | ||
1255 | file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL); | |
1256 | if (!file_ops) | |
1257 | return NULL; | |
1258 | ||
1259 | file_ops->mod = mod; | |
1260 | ||
1261 | file_ops->id = ftrace_event_id_fops; | |
1262 | file_ops->id.owner = mod; | |
1263 | ||
1264 | file_ops->enable = ftrace_enable_fops; | |
1265 | file_ops->enable.owner = mod; | |
1266 | ||
1267 | file_ops->filter = ftrace_event_filter_fops; | |
1268 | file_ops->filter.owner = mod; | |
1269 | ||
1270 | file_ops->format = ftrace_event_format_fops; | |
1271 | file_ops->format.owner = mod; | |
1272 | ||
1273 | list_add(&file_ops->list, &ftrace_module_file_list); | |
1274 | ||
1275 | return file_ops; | |
1276 | } | |
1277 | ||
6d723736 SR |
1278 | static void trace_module_add_events(struct module *mod) |
1279 | { | |
701970b3 | 1280 | struct ftrace_module_file_ops *file_ops = NULL; |
6d723736 | 1281 | struct ftrace_event_call *call, *start, *end; |
6d723736 SR |
1282 | |
1283 | start = mod->trace_events; | |
1284 | end = mod->trace_events + mod->num_trace_events; | |
1285 | ||
1286 | if (start == end) | |
1287 | return; | |
1288 | ||
67ead0a6 LZ |
1289 | file_ops = trace_create_file_ops(mod); |
1290 | if (!file_ops) | |
6d723736 SR |
1291 | return; |
1292 | ||
1293 | for_each_event(call, start, end) { | |
67ead0a6 | 1294 | __trace_add_event_call(call, mod, |
88f70d75 MH |
1295 | &file_ops->id, &file_ops->enable, |
1296 | &file_ops->filter, &file_ops->format); | |
6d723736 SR |
1297 | } |
1298 | } | |
1299 | ||
1300 | static void trace_module_remove_events(struct module *mod) | |
1301 | { | |
701970b3 | 1302 | struct ftrace_module_file_ops *file_ops; |
6d723736 | 1303 | struct ftrace_event_call *call, *p; |
9456f0fa | 1304 | bool found = false; |
6d723736 | 1305 | |
110bf2b7 | 1306 | down_write(&trace_event_mutex); |
6d723736 SR |
1307 | list_for_each_entry_safe(call, p, &ftrace_events, list) { |
1308 | if (call->mod == mod) { | |
9456f0fa | 1309 | found = true; |
bd1a5c84 | 1310 | __trace_remove_event_call(call); |
6d723736 SR |
1311 | } |
1312 | } | |
701970b3 SR |
1313 | |
1314 | /* Now free the file_operations */ | |
1315 | list_for_each_entry(file_ops, &ftrace_module_file_list, list) { | |
1316 | if (file_ops->mod == mod) | |
1317 | break; | |
1318 | } | |
1319 | if (&file_ops->list != &ftrace_module_file_list) { | |
1320 | list_del(&file_ops->list); | |
1321 | kfree(file_ops); | |
1322 | } | |
9456f0fa SR |
1323 | |
1324 | /* | |
1325 | * It is safest to reset the ring buffer if the module being unloaded | |
1326 | * registered any events. | |
1327 | */ | |
1328 | if (found) | |
1329 | tracing_reset_current_online_cpus(); | |
110bf2b7 | 1330 | up_write(&trace_event_mutex); |
6d723736 SR |
1331 | } |
1332 | ||
61f919a1 SR |
1333 | static int trace_module_notify(struct notifier_block *self, |
1334 | unsigned long val, void *data) | |
6d723736 SR |
1335 | { |
1336 | struct module *mod = data; | |
1337 | ||
1338 | mutex_lock(&event_mutex); | |
1339 | switch (val) { | |
1340 | case MODULE_STATE_COMING: | |
1341 | trace_module_add_events(mod); | |
1342 | break; | |
1343 | case MODULE_STATE_GOING: | |
1344 | trace_module_remove_events(mod); | |
1345 | break; | |
1346 | } | |
1347 | mutex_unlock(&event_mutex); | |
fd994989 | 1348 | |
1473e441 SR |
1349 | return 0; |
1350 | } | |
61f919a1 SR |
1351 | #else |
1352 | static int trace_module_notify(struct notifier_block *self, | |
1353 | unsigned long val, void *data) | |
1354 | { | |
1355 | return 0; | |
1356 | } | |
1357 | #endif /* CONFIG_MODULES */ | |
1473e441 | 1358 | |
ec827c7e | 1359 | static struct notifier_block trace_module_nb = { |
6d723736 SR |
1360 | .notifier_call = trace_module_notify, |
1361 | .priority = 0, | |
1362 | }; | |
1363 | ||
a59fd602 SR |
1364 | extern struct ftrace_event_call __start_ftrace_events[]; |
1365 | extern struct ftrace_event_call __stop_ftrace_events[]; | |
1366 | ||
020e5f85 LZ |
1367 | static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; |
1368 | ||
1369 | static __init int setup_trace_event(char *str) | |
1370 | { | |
1371 | strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE); | |
1372 | ring_buffer_expanded = 1; | |
1373 | tracing_selftest_disabled = 1; | |
1374 | ||
1375 | return 1; | |
1376 | } | |
1377 | __setup("trace_event=", setup_trace_event); | |
1378 | ||
b77e38aa SR |
1379 | static __init int event_trace_init(void) |
1380 | { | |
a59fd602 | 1381 | struct ftrace_event_call *call; |
b77e38aa SR |
1382 | struct dentry *d_tracer; |
1383 | struct dentry *entry; | |
1473e441 | 1384 | struct dentry *d_events; |
6d723736 | 1385 | int ret; |
020e5f85 LZ |
1386 | char *buf = bootup_event_buf; |
1387 | char *token; | |
b77e38aa SR |
1388 | |
1389 | d_tracer = tracing_init_dentry(); | |
1390 | if (!d_tracer) | |
1391 | return 0; | |
1392 | ||
2314c4ae SR |
1393 | entry = debugfs_create_file("available_events", 0444, d_tracer, |
1394 | (void *)&show_event_seq_ops, | |
1395 | &ftrace_avail_fops); | |
1396 | if (!entry) | |
1397 | pr_warning("Could not create debugfs " | |
1398 | "'available_events' entry\n"); | |
1399 | ||
b77e38aa SR |
1400 | entry = debugfs_create_file("set_event", 0644, d_tracer, |
1401 | (void *)&show_set_event_seq_ops, | |
1402 | &ftrace_set_event_fops); | |
1403 | if (!entry) | |
1404 | pr_warning("Could not create debugfs " | |
1405 | "'set_event' entry\n"); | |
1406 | ||
1473e441 SR |
1407 | d_events = event_trace_events_dir(); |
1408 | if (!d_events) | |
1409 | return 0; | |
1410 | ||
d1b182a8 SR |
1411 | /* ring buffer internal formats */ |
1412 | trace_create_file("header_page", 0444, d_events, | |
1413 | ring_buffer_print_page_header, | |
1414 | &ftrace_show_header_fops); | |
1415 | ||
1416 | trace_create_file("header_event", 0444, d_events, | |
1417 | ring_buffer_print_entry_header, | |
1418 | &ftrace_show_header_fops); | |
1419 | ||
8ae79a13 | 1420 | trace_create_file("enable", 0644, d_events, |
8f31bfe5 | 1421 | NULL, &ftrace_system_enable_fops); |
8ae79a13 | 1422 | |
8728fe50 LZ |
1423 | if (trace_define_common_fields()) |
1424 | pr_warning("tracing: Failed to allocate common fields"); | |
1425 | ||
6d723736 | 1426 | for_each_event(call, __start_ftrace_events, __stop_ftrace_events) { |
67ead0a6 | 1427 | __trace_add_event_call(call, NULL, &ftrace_event_id_fops, |
88f70d75 MH |
1428 | &ftrace_enable_fops, |
1429 | &ftrace_event_filter_fops, | |
1430 | &ftrace_event_format_fops); | |
1473e441 SR |
1431 | } |
1432 | ||
020e5f85 LZ |
1433 | while (true) { |
1434 | token = strsep(&buf, ","); | |
1435 | ||
1436 | if (!token) | |
1437 | break; | |
1438 | if (!*token) | |
1439 | continue; | |
1440 | ||
1441 | ret = ftrace_set_clr_event(token, 1); | |
1442 | if (ret) | |
1443 | pr_warning("Failed to enable trace event: %s\n", token); | |
1444 | } | |
1445 | ||
6d723736 | 1446 | ret = register_module_notifier(&trace_module_nb); |
55379376 | 1447 | if (ret) |
6d723736 SR |
1448 | pr_warning("Failed to register trace events module notifier\n"); |
1449 | ||
b77e38aa SR |
1450 | return 0; |
1451 | } | |
1452 | fs_initcall(event_trace_init); | |
e6187007 SR |
1453 | |
1454 | #ifdef CONFIG_FTRACE_STARTUP_TEST | |
1455 | ||
1456 | static DEFINE_SPINLOCK(test_spinlock); | |
1457 | static DEFINE_SPINLOCK(test_spinlock_irq); | |
1458 | static DEFINE_MUTEX(test_mutex); | |
1459 | ||
1460 | static __init void test_work(struct work_struct *dummy) | |
1461 | { | |
1462 | spin_lock(&test_spinlock); | |
1463 | spin_lock_irq(&test_spinlock_irq); | |
1464 | udelay(1); | |
1465 | spin_unlock_irq(&test_spinlock_irq); | |
1466 | spin_unlock(&test_spinlock); | |
1467 | ||
1468 | mutex_lock(&test_mutex); | |
1469 | msleep(1); | |
1470 | mutex_unlock(&test_mutex); | |
1471 | } | |
1472 | ||
1473 | static __init int event_test_thread(void *unused) | |
1474 | { | |
1475 | void *test_malloc; | |
1476 | ||
1477 | test_malloc = kmalloc(1234, GFP_KERNEL); | |
1478 | if (!test_malloc) | |
1479 | pr_info("failed to kmalloc\n"); | |
1480 | ||
1481 | schedule_on_each_cpu(test_work); | |
1482 | ||
1483 | kfree(test_malloc); | |
1484 | ||
1485 | set_current_state(TASK_INTERRUPTIBLE); | |
1486 | while (!kthread_should_stop()) | |
1487 | schedule(); | |
1488 | ||
1489 | return 0; | |
1490 | } | |
1491 | ||
1492 | /* | |
1493 | * Do various things that may trigger events. | |
1494 | */ | |
1495 | static __init void event_test_stuff(void) | |
1496 | { | |
1497 | struct task_struct *test_thread; | |
1498 | ||
1499 | test_thread = kthread_run(event_test_thread, NULL, "test-events"); | |
1500 | msleep(1); | |
1501 | kthread_stop(test_thread); | |
1502 | } | |
1503 | ||
1504 | /* | |
1505 | * For every trace event defined, we will test each trace point separately, | |
1506 | * and then by groups, and finally all trace points. | |
1507 | */ | |
9ea21c1e | 1508 | static __init void event_trace_self_tests(void) |
e6187007 SR |
1509 | { |
1510 | struct ftrace_event_call *call; | |
1511 | struct event_subsystem *system; | |
e6187007 SR |
1512 | int ret; |
1513 | ||
1514 | pr_info("Running tests on trace events:\n"); | |
1515 | ||
1516 | list_for_each_entry(call, &ftrace_events, list) { | |
1517 | ||
2239291a SR |
1518 | /* Only test those that have a probe */ |
1519 | if (!call->class || !call->class->probe) | |
e6187007 SR |
1520 | continue; |
1521 | ||
1f5a6b45 SR |
1522 | /* |
1523 | * Testing syscall events here is pretty useless, but | |
1524 | * we still do it if configured. But this is time consuming. | |
1525 | * What we really need is a user thread to perform the | |
1526 | * syscalls as we test. | |
1527 | */ | |
1528 | #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS | |
8f082018 SR |
1529 | if (call->class->system && |
1530 | strcmp(call->class->system, "syscalls") == 0) | |
1f5a6b45 SR |
1531 | continue; |
1532 | #endif | |
1533 | ||
e6187007 SR |
1534 | pr_info("Testing event %s: ", call->name); |
1535 | ||
1536 | /* | |
1537 | * If an event is already enabled, someone is using | |
1538 | * it and the self test should not be on. | |
1539 | */ | |
553552ce | 1540 | if (call->flags & TRACE_EVENT_FL_ENABLED) { |
e6187007 SR |
1541 | pr_warning("Enabled event during self test!\n"); |
1542 | WARN_ON_ONCE(1); | |
1543 | continue; | |
1544 | } | |
1545 | ||
0e907c99 | 1546 | ftrace_event_enable_disable(call, 1); |
e6187007 | 1547 | event_test_stuff(); |
0e907c99 | 1548 | ftrace_event_enable_disable(call, 0); |
e6187007 SR |
1549 | |
1550 | pr_cont("OK\n"); | |
1551 | } | |
1552 | ||
1553 | /* Now test at the sub system level */ | |
1554 | ||
1555 | pr_info("Running tests on trace event systems:\n"); | |
1556 | ||
1557 | list_for_each_entry(system, &event_subsystems, list) { | |
1558 | ||
1559 | /* the ftrace system is special, skip it */ | |
1560 | if (strcmp(system->name, "ftrace") == 0) | |
1561 | continue; | |
1562 | ||
1563 | pr_info("Testing event system %s: ", system->name); | |
1564 | ||
8f31bfe5 | 1565 | ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1); |
e6187007 SR |
1566 | if (WARN_ON_ONCE(ret)) { |
1567 | pr_warning("error enabling system %s\n", | |
1568 | system->name); | |
1569 | continue; | |
1570 | } | |
1571 | ||
1572 | event_test_stuff(); | |
1573 | ||
8f31bfe5 | 1574 | ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0); |
e6187007 SR |
1575 | if (WARN_ON_ONCE(ret)) |
1576 | pr_warning("error disabling system %s\n", | |
1577 | system->name); | |
1578 | ||
1579 | pr_cont("OK\n"); | |
1580 | } | |
1581 | ||
1582 | /* Test with all events enabled */ | |
1583 | ||
1584 | pr_info("Running tests on all trace events:\n"); | |
1585 | pr_info("Testing all events: "); | |
1586 | ||
8f31bfe5 | 1587 | ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1); |
e6187007 | 1588 | if (WARN_ON_ONCE(ret)) { |
e6187007 | 1589 | pr_warning("error enabling all events\n"); |
9ea21c1e | 1590 | return; |
e6187007 SR |
1591 | } |
1592 | ||
1593 | event_test_stuff(); | |
1594 | ||
1595 | /* reset sysname */ | |
8f31bfe5 | 1596 | ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0); |
e6187007 SR |
1597 | if (WARN_ON_ONCE(ret)) { |
1598 | pr_warning("error disabling all events\n"); | |
9ea21c1e | 1599 | return; |
e6187007 SR |
1600 | } |
1601 | ||
1602 | pr_cont("OK\n"); | |
9ea21c1e SR |
1603 | } |
1604 | ||
1605 | #ifdef CONFIG_FUNCTION_TRACER | |
1606 | ||
245b2e70 | 1607 | static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); |
9ea21c1e SR |
1608 | |
1609 | static void | |
1610 | function_test_events_call(unsigned long ip, unsigned long parent_ip) | |
1611 | { | |
1612 | struct ring_buffer_event *event; | |
e77405ad | 1613 | struct ring_buffer *buffer; |
9ea21c1e SR |
1614 | struct ftrace_entry *entry; |
1615 | unsigned long flags; | |
1616 | long disabled; | |
9ea21c1e SR |
1617 | int cpu; |
1618 | int pc; | |
1619 | ||
1620 | pc = preempt_count(); | |
5168ae50 | 1621 | preempt_disable_notrace(); |
9ea21c1e | 1622 | cpu = raw_smp_processor_id(); |
245b2e70 | 1623 | disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); |
9ea21c1e SR |
1624 | |
1625 | if (disabled != 1) | |
1626 | goto out; | |
1627 | ||
1628 | local_save_flags(flags); | |
1629 | ||
e77405ad SR |
1630 | event = trace_current_buffer_lock_reserve(&buffer, |
1631 | TRACE_FN, sizeof(*entry), | |
9ea21c1e SR |
1632 | flags, pc); |
1633 | if (!event) | |
1634 | goto out; | |
1635 | entry = ring_buffer_event_data(event); | |
1636 | entry->ip = ip; | |
1637 | entry->parent_ip = parent_ip; | |
1638 | ||
e77405ad | 1639 | trace_nowake_buffer_unlock_commit(buffer, event, flags, pc); |
9ea21c1e SR |
1640 | |
1641 | out: | |
245b2e70 | 1642 | atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); |
5168ae50 | 1643 | preempt_enable_notrace(); |
9ea21c1e SR |
1644 | } |
1645 | ||
1646 | static struct ftrace_ops trace_ops __initdata = | |
1647 | { | |
1648 | .func = function_test_events_call, | |
1649 | }; | |
1650 | ||
1651 | static __init void event_trace_self_test_with_function(void) | |
1652 | { | |
1653 | register_ftrace_function(&trace_ops); | |
1654 | pr_info("Running tests again, along with the function tracer\n"); | |
1655 | event_trace_self_tests(); | |
1656 | unregister_ftrace_function(&trace_ops); | |
1657 | } | |
1658 | #else | |
1659 | static __init void event_trace_self_test_with_function(void) | |
1660 | { | |
1661 | } | |
1662 | #endif | |
1663 | ||
1664 | static __init int event_trace_self_tests_init(void) | |
1665 | { | |
020e5f85 LZ |
1666 | if (!tracing_selftest_disabled) { |
1667 | event_trace_self_tests(); | |
1668 | event_trace_self_test_with_function(); | |
1669 | } | |
e6187007 SR |
1670 | |
1671 | return 0; | |
1672 | } | |
1673 | ||
28d20e2d | 1674 | late_initcall(event_trace_self_tests_init); |
e6187007 SR |
1675 | |
1676 | #endif |