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