]>
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, | |
603 | FORMAT_PRINTFMT = 2, | |
604 | }; | |
605 | ||
606 | static void *f_next(struct seq_file *m, void *v, loff_t *pos) | |
981d081e | 607 | { |
2a37a3df | 608 | struct ftrace_event_call *call = m->private; |
5a65e956 | 609 | struct ftrace_event_field *field; |
2e33af02 | 610 | struct list_head *head; |
981d081e | 611 | |
2a37a3df | 612 | (*pos)++; |
5a65e956 | 613 | |
2a37a3df SR |
614 | switch ((unsigned long)v) { |
615 | case FORMAT_HEADER: | |
d244b6bd | 616 | head = &ftrace_common_fields; |
5a65e956 | 617 | |
2a37a3df SR |
618 | if (unlikely(list_empty(head))) |
619 | return NULL; | |
5a65e956 | 620 | |
2a37a3df SR |
621 | field = list_entry(head->prev, struct ftrace_event_field, link); |
622 | return field; | |
5a65e956 | 623 | |
2a37a3df SR |
624 | case FORMAT_PRINTFMT: |
625 | /* all done */ | |
626 | return NULL; | |
5a65e956 LJ |
627 | } |
628 | ||
d244b6bd SR |
629 | head = trace_get_fields(call); |
630 | ||
2a37a3df SR |
631 | /* |
632 | * To separate common fields from event fields, the | |
633 | * LSB is set on the first event field. Clear it in case. | |
634 | */ | |
635 | v = (void *)((unsigned long)v & ~1L); | |
5a65e956 | 636 | |
2a37a3df | 637 | field = v; |
d244b6bd SR |
638 | /* |
639 | * If this is a common field, and at the end of the list, then | |
640 | * continue with main list. | |
641 | */ | |
642 | if (field->link.prev == &ftrace_common_fields) { | |
643 | if (unlikely(list_empty(head))) | |
644 | return NULL; | |
645 | field = list_entry(head->prev, struct ftrace_event_field, link); | |
646 | /* Set the LSB to notify f_show to print an extra newline */ | |
647 | field = (struct ftrace_event_field *) | |
648 | ((unsigned long)field | 1); | |
649 | return field; | |
8728fe50 | 650 | } |
d244b6bd SR |
651 | |
652 | /* If we are done tell f_show to print the format */ | |
2a37a3df SR |
653 | if (field->link.prev == head) |
654 | return (void *)FORMAT_PRINTFMT; | |
655 | ||
656 | field = list_entry(field->link.prev, struct ftrace_event_field, link); | |
657 | ||
2a37a3df | 658 | return field; |
8728fe50 | 659 | } |
5a65e956 | 660 | |
2a37a3df | 661 | static void *f_start(struct seq_file *m, loff_t *pos) |
8728fe50 | 662 | { |
2a37a3df SR |
663 | loff_t l = 0; |
664 | void *p; | |
5a65e956 | 665 | |
2a37a3df SR |
666 | /* Start by showing the header */ |
667 | if (!*pos) | |
668 | return (void *)FORMAT_HEADER; | |
669 | ||
670 | p = (void *)FORMAT_HEADER; | |
671 | do { | |
672 | p = f_next(m, p, &l); | |
673 | } while (p && l < *pos); | |
674 | ||
675 | return p; | |
676 | } | |
677 | ||
678 | static int f_show(struct seq_file *m, void *v) | |
679 | { | |
680 | struct ftrace_event_call *call = m->private; | |
681 | struct ftrace_event_field *field; | |
682 | const char *array_descriptor; | |
683 | ||
684 | switch ((unsigned long)v) { | |
685 | case FORMAT_HEADER: | |
686 | seq_printf(m, "name: %s\n", call->name); | |
687 | seq_printf(m, "ID: %d\n", call->event.type); | |
688 | seq_printf(m, "format:\n"); | |
8728fe50 | 689 | return 0; |
5a65e956 | 690 | |
2a37a3df SR |
691 | case FORMAT_PRINTFMT: |
692 | seq_printf(m, "\nprint fmt: %s\n", | |
693 | call->print_fmt); | |
694 | return 0; | |
981d081e | 695 | } |
8728fe50 | 696 | |
2a37a3df SR |
697 | /* |
698 | * To separate common fields from event fields, the | |
699 | * LSB is set on the first event field. Clear it and | |
700 | * print a newline if it is set. | |
701 | */ | |
702 | if ((unsigned long)v & 1) { | |
703 | seq_putc(m, '\n'); | |
704 | v = (void *)((unsigned long)v & ~1L); | |
705 | } | |
8728fe50 | 706 | |
2a37a3df | 707 | field = v; |
8728fe50 | 708 | |
2a37a3df SR |
709 | /* |
710 | * Smartly shows the array type(except dynamic array). | |
711 | * Normal: | |
712 | * field:TYPE VAR | |
713 | * If TYPE := TYPE[LEN], it is shown: | |
714 | * field:TYPE VAR[LEN] | |
715 | */ | |
716 | array_descriptor = strchr(field->type, '['); | |
8728fe50 | 717 | |
2a37a3df SR |
718 | if (!strncmp(field->type, "__data_loc", 10)) |
719 | array_descriptor = NULL; | |
8728fe50 | 720 | |
2a37a3df SR |
721 | if (!array_descriptor) |
722 | seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", | |
723 | field->type, field->name, field->offset, | |
724 | field->size, !!field->is_signed); | |
725 | else | |
726 | seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", | |
727 | (int)(array_descriptor - field->type), | |
728 | field->type, field->name, | |
729 | array_descriptor, field->offset, | |
730 | field->size, !!field->is_signed); | |
8728fe50 | 731 | |
2a37a3df SR |
732 | return 0; |
733 | } | |
5a65e956 | 734 | |
2a37a3df SR |
735 | static void f_stop(struct seq_file *m, void *p) |
736 | { | |
737 | } | |
981d081e | 738 | |
2a37a3df SR |
739 | static const struct seq_operations trace_format_seq_ops = { |
740 | .start = f_start, | |
741 | .next = f_next, | |
742 | .stop = f_stop, | |
743 | .show = f_show, | |
744 | }; | |
745 | ||
746 | static int trace_format_open(struct inode *inode, struct file *file) | |
747 | { | |
748 | struct ftrace_event_call *call = inode->i_private; | |
749 | struct seq_file *m; | |
750 | int ret; | |
751 | ||
752 | ret = seq_open(file, &trace_format_seq_ops); | |
753 | if (ret < 0) | |
754 | return ret; | |
755 | ||
756 | m = file->private_data; | |
757 | m->private = call; | |
758 | ||
759 | return 0; | |
981d081e SR |
760 | } |
761 | ||
23725aee PZ |
762 | static ssize_t |
763 | event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) | |
764 | { | |
765 | struct ftrace_event_call *call = filp->private_data; | |
766 | struct trace_seq *s; | |
767 | int r; | |
768 | ||
769 | if (*ppos) | |
770 | return 0; | |
771 | ||
772 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
773 | if (!s) | |
774 | return -ENOMEM; | |
775 | ||
776 | trace_seq_init(s); | |
32c0edae | 777 | trace_seq_printf(s, "%d\n", call->event.type); |
23725aee PZ |
778 | |
779 | r = simple_read_from_buffer(ubuf, cnt, ppos, | |
780 | s->buffer, s->len); | |
781 | kfree(s); | |
782 | return r; | |
783 | } | |
784 | ||
7ce7e424 TZ |
785 | static ssize_t |
786 | event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, | |
787 | loff_t *ppos) | |
788 | { | |
789 | struct ftrace_event_call *call = filp->private_data; | |
790 | struct trace_seq *s; | |
791 | int r; | |
792 | ||
793 | if (*ppos) | |
794 | return 0; | |
795 | ||
796 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
797 | if (!s) | |
798 | return -ENOMEM; | |
799 | ||
800 | trace_seq_init(s); | |
801 | ||
8b372562 | 802 | print_event_filter(call, s); |
4bda2d51 | 803 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
7ce7e424 TZ |
804 | |
805 | kfree(s); | |
806 | ||
807 | return r; | |
808 | } | |
809 | ||
810 | static ssize_t | |
811 | event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
812 | loff_t *ppos) | |
813 | { | |
814 | struct ftrace_event_call *call = filp->private_data; | |
8b372562 | 815 | char *buf; |
7ce7e424 TZ |
816 | int err; |
817 | ||
8b372562 | 818 | if (cnt >= PAGE_SIZE) |
7ce7e424 TZ |
819 | return -EINVAL; |
820 | ||
8b372562 TZ |
821 | buf = (char *)__get_free_page(GFP_TEMPORARY); |
822 | if (!buf) | |
7ce7e424 TZ |
823 | return -ENOMEM; |
824 | ||
8b372562 TZ |
825 | if (copy_from_user(buf, ubuf, cnt)) { |
826 | free_page((unsigned long) buf); | |
827 | return -EFAULT; | |
7ce7e424 | 828 | } |
8b372562 | 829 | buf[cnt] = '\0'; |
7ce7e424 | 830 | |
8b372562 TZ |
831 | err = apply_event_filter(call, buf); |
832 | free_page((unsigned long) buf); | |
833 | if (err < 0) | |
44e9c8b7 | 834 | return err; |
0a19e53c | 835 | |
7ce7e424 TZ |
836 | *ppos += cnt; |
837 | ||
838 | return cnt; | |
839 | } | |
840 | ||
cfb180f3 TZ |
841 | static ssize_t |
842 | subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, | |
843 | loff_t *ppos) | |
844 | { | |
845 | struct event_subsystem *system = filp->private_data; | |
846 | struct trace_seq *s; | |
847 | int r; | |
848 | ||
849 | if (*ppos) | |
850 | return 0; | |
851 | ||
852 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
853 | if (!s) | |
854 | return -ENOMEM; | |
855 | ||
856 | trace_seq_init(s); | |
857 | ||
8b372562 | 858 | print_subsystem_event_filter(system, s); |
4bda2d51 | 859 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
cfb180f3 TZ |
860 | |
861 | kfree(s); | |
862 | ||
863 | return r; | |
864 | } | |
865 | ||
866 | static ssize_t | |
867 | subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
868 | loff_t *ppos) | |
869 | { | |
870 | struct event_subsystem *system = filp->private_data; | |
8b372562 | 871 | char *buf; |
cfb180f3 TZ |
872 | int err; |
873 | ||
8b372562 | 874 | if (cnt >= PAGE_SIZE) |
cfb180f3 TZ |
875 | return -EINVAL; |
876 | ||
8b372562 TZ |
877 | buf = (char *)__get_free_page(GFP_TEMPORARY); |
878 | if (!buf) | |
cfb180f3 TZ |
879 | return -ENOMEM; |
880 | ||
8b372562 TZ |
881 | if (copy_from_user(buf, ubuf, cnt)) { |
882 | free_page((unsigned long) buf); | |
883 | return -EFAULT; | |
cfb180f3 | 884 | } |
8b372562 | 885 | buf[cnt] = '\0'; |
cfb180f3 | 886 | |
8b372562 TZ |
887 | err = apply_subsystem_event_filter(system, buf); |
888 | free_page((unsigned long) buf); | |
889 | if (err < 0) | |
44e9c8b7 | 890 | return err; |
cfb180f3 TZ |
891 | |
892 | *ppos += cnt; | |
893 | ||
894 | return cnt; | |
895 | } | |
896 | ||
d1b182a8 SR |
897 | static ssize_t |
898 | show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) | |
899 | { | |
900 | int (*func)(struct trace_seq *s) = filp->private_data; | |
901 | struct trace_seq *s; | |
902 | int r; | |
903 | ||
904 | if (*ppos) | |
905 | return 0; | |
906 | ||
907 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
908 | if (!s) | |
909 | return -ENOMEM; | |
910 | ||
911 | trace_seq_init(s); | |
912 | ||
913 | func(s); | |
914 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); | |
915 | ||
916 | kfree(s); | |
917 | ||
918 | return r; | |
919 | } | |
920 | ||
b77e38aa SR |
921 | static const struct seq_operations show_event_seq_ops = { |
922 | .start = t_start, | |
923 | .next = t_next, | |
924 | .show = t_show, | |
925 | .stop = t_stop, | |
926 | }; | |
927 | ||
928 | static const struct seq_operations show_set_event_seq_ops = { | |
929 | .start = s_start, | |
930 | .next = s_next, | |
931 | .show = t_show, | |
932 | .stop = t_stop, | |
933 | }; | |
934 | ||
2314c4ae SR |
935 | static const struct file_operations ftrace_avail_fops = { |
936 | .open = ftrace_event_seq_open, | |
937 | .read = seq_read, | |
938 | .llseek = seq_lseek, | |
939 | .release = seq_release, | |
940 | }; | |
941 | ||
b77e38aa SR |
942 | static const struct file_operations ftrace_set_event_fops = { |
943 | .open = ftrace_event_seq_open, | |
944 | .read = seq_read, | |
945 | .write = ftrace_event_write, | |
946 | .llseek = seq_lseek, | |
947 | .release = seq_release, | |
948 | }; | |
949 | ||
1473e441 SR |
950 | static const struct file_operations ftrace_enable_fops = { |
951 | .open = tracing_open_generic, | |
952 | .read = event_enable_read, | |
953 | .write = event_enable_write, | |
6038f373 | 954 | .llseek = default_llseek, |
1473e441 SR |
955 | }; |
956 | ||
981d081e | 957 | static const struct file_operations ftrace_event_format_fops = { |
2a37a3df SR |
958 | .open = trace_format_open, |
959 | .read = seq_read, | |
960 | .llseek = seq_lseek, | |
961 | .release = seq_release, | |
981d081e SR |
962 | }; |
963 | ||
23725aee PZ |
964 | static const struct file_operations ftrace_event_id_fops = { |
965 | .open = tracing_open_generic, | |
966 | .read = event_id_read, | |
6038f373 | 967 | .llseek = default_llseek, |
23725aee PZ |
968 | }; |
969 | ||
7ce7e424 TZ |
970 | static const struct file_operations ftrace_event_filter_fops = { |
971 | .open = tracing_open_generic, | |
972 | .read = event_filter_read, | |
973 | .write = event_filter_write, | |
6038f373 | 974 | .llseek = default_llseek, |
7ce7e424 TZ |
975 | }; |
976 | ||
cfb180f3 TZ |
977 | static const struct file_operations ftrace_subsystem_filter_fops = { |
978 | .open = tracing_open_generic, | |
979 | .read = subsystem_filter_read, | |
980 | .write = subsystem_filter_write, | |
6038f373 | 981 | .llseek = default_llseek, |
cfb180f3 TZ |
982 | }; |
983 | ||
8ae79a13 SR |
984 | static const struct file_operations ftrace_system_enable_fops = { |
985 | .open = tracing_open_generic, | |
986 | .read = system_enable_read, | |
987 | .write = system_enable_write, | |
6038f373 | 988 | .llseek = default_llseek, |
8ae79a13 SR |
989 | }; |
990 | ||
d1b182a8 SR |
991 | static const struct file_operations ftrace_show_header_fops = { |
992 | .open = tracing_open_generic, | |
993 | .read = show_header, | |
6038f373 | 994 | .llseek = default_llseek, |
d1b182a8 SR |
995 | }; |
996 | ||
1473e441 SR |
997 | static struct dentry *event_trace_events_dir(void) |
998 | { | |
999 | static struct dentry *d_tracer; | |
1000 | static struct dentry *d_events; | |
1001 | ||
1002 | if (d_events) | |
1003 | return d_events; | |
1004 | ||
1005 | d_tracer = tracing_init_dentry(); | |
1006 | if (!d_tracer) | |
1007 | return NULL; | |
1008 | ||
1009 | d_events = debugfs_create_dir("events", d_tracer); | |
1010 | if (!d_events) | |
1011 | pr_warning("Could not create debugfs " | |
1012 | "'events' directory\n"); | |
1013 | ||
1014 | return d_events; | |
1015 | } | |
1016 | ||
6ecc2d1c SR |
1017 | static LIST_HEAD(event_subsystems); |
1018 | ||
1019 | static struct dentry * | |
1020 | event_subsystem_dir(const char *name, struct dentry *d_events) | |
1021 | { | |
1022 | struct event_subsystem *system; | |
e1112b4d | 1023 | struct dentry *entry; |
6ecc2d1c SR |
1024 | |
1025 | /* First see if we did not already create this dir */ | |
1026 | list_for_each_entry(system, &event_subsystems, list) { | |
dc82ec98 XG |
1027 | if (strcmp(system->name, name) == 0) { |
1028 | system->nr_events++; | |
6ecc2d1c | 1029 | return system->entry; |
dc82ec98 | 1030 | } |
6ecc2d1c SR |
1031 | } |
1032 | ||
1033 | /* need to create new entry */ | |
1034 | system = kmalloc(sizeof(*system), GFP_KERNEL); | |
1035 | if (!system) { | |
1036 | pr_warning("No memory to create event subsystem %s\n", | |
1037 | name); | |
1038 | return d_events; | |
1039 | } | |
1040 | ||
1041 | system->entry = debugfs_create_dir(name, d_events); | |
1042 | if (!system->entry) { | |
1043 | pr_warning("Could not create event subsystem %s\n", | |
1044 | name); | |
1045 | kfree(system); | |
1046 | return d_events; | |
1047 | } | |
1048 | ||
dc82ec98 | 1049 | system->nr_events = 1; |
6d723736 SR |
1050 | system->name = kstrdup(name, GFP_KERNEL); |
1051 | if (!system->name) { | |
1052 | debugfs_remove(system->entry); | |
1053 | kfree(system); | |
1054 | return d_events; | |
1055 | } | |
1056 | ||
6ecc2d1c SR |
1057 | list_add(&system->list, &event_subsystems); |
1058 | ||
30e673b2 | 1059 | system->filter = NULL; |
cfb180f3 | 1060 | |
8b372562 TZ |
1061 | system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL); |
1062 | if (!system->filter) { | |
1063 | pr_warning("Could not allocate filter for subsystem " | |
1064 | "'%s'\n", name); | |
1065 | return system->entry; | |
1066 | } | |
1067 | ||
e1112b4d TZ |
1068 | entry = debugfs_create_file("filter", 0644, system->entry, system, |
1069 | &ftrace_subsystem_filter_fops); | |
8b372562 TZ |
1070 | if (!entry) { |
1071 | kfree(system->filter); | |
1072 | system->filter = NULL; | |
e1112b4d TZ |
1073 | pr_warning("Could not create debugfs " |
1074 | "'%s/filter' entry\n", name); | |
8b372562 | 1075 | } |
e1112b4d | 1076 | |
f3f3f009 FW |
1077 | trace_create_file("enable", 0644, system->entry, |
1078 | (void *)system->name, | |
1079 | &ftrace_system_enable_fops); | |
8ae79a13 | 1080 | |
6ecc2d1c SR |
1081 | return system->entry; |
1082 | } | |
1083 | ||
1473e441 | 1084 | static int |
701970b3 SR |
1085 | event_create_dir(struct ftrace_event_call *call, struct dentry *d_events, |
1086 | const struct file_operations *id, | |
1087 | const struct file_operations *enable, | |
1088 | const struct file_operations *filter, | |
1089 | const struct file_operations *format) | |
1473e441 | 1090 | { |
2e33af02 | 1091 | struct list_head *head; |
fd994989 | 1092 | int ret; |
1473e441 | 1093 | |
6ecc2d1c SR |
1094 | /* |
1095 | * If the trace point header did not define TRACE_SYSTEM | |
1096 | * then the system would be called "TRACE_SYSTEM". | |
1097 | */ | |
8f082018 SR |
1098 | if (strcmp(call->class->system, TRACE_SYSTEM) != 0) |
1099 | d_events = event_subsystem_dir(call->class->system, d_events); | |
6ecc2d1c | 1100 | |
1473e441 SR |
1101 | call->dir = debugfs_create_dir(call->name, d_events); |
1102 | if (!call->dir) { | |
1103 | pr_warning("Could not create debugfs " | |
1104 | "'%s' directory\n", call->name); | |
1105 | return -1; | |
1106 | } | |
1107 | ||
a1d0ce82 | 1108 | if (call->class->reg) |
f3f3f009 FW |
1109 | trace_create_file("enable", 0644, call->dir, call, |
1110 | enable); | |
1473e441 | 1111 | |
2239291a | 1112 | #ifdef CONFIG_PERF_EVENTS |
a1d0ce82 | 1113 | if (call->event.type && call->class->reg) |
f3f3f009 FW |
1114 | trace_create_file("id", 0444, call->dir, call, |
1115 | id); | |
2239291a | 1116 | #endif |
23725aee | 1117 | |
c9d932cf LZ |
1118 | /* |
1119 | * Other events may have the same class. Only update | |
1120 | * the fields if they are not already defined. | |
1121 | */ | |
1122 | head = trace_get_fields(call); | |
1123 | if (list_empty(head)) { | |
1124 | ret = call->class->define_fields(call); | |
1125 | if (ret < 0) { | |
1126 | pr_warning("Could not initialize trace point" | |
1127 | " events/%s\n", call->name); | |
1128 | return ret; | |
cf027f64 TZ |
1129 | } |
1130 | } | |
c9d932cf LZ |
1131 | trace_create_file("filter", 0644, call->dir, call, |
1132 | filter); | |
cf027f64 | 1133 | |
f3f3f009 FW |
1134 | trace_create_file("format", 0444, call->dir, call, |
1135 | format); | |
6d723736 SR |
1136 | |
1137 | return 0; | |
1138 | } | |
1139 | ||
67ead0a6 LZ |
1140 | static int |
1141 | __trace_add_event_call(struct ftrace_event_call *call, struct module *mod, | |
1142 | const struct file_operations *id, | |
1143 | const struct file_operations *enable, | |
1144 | const struct file_operations *filter, | |
1145 | const struct file_operations *format) | |
bd1a5c84 MH |
1146 | { |
1147 | struct dentry *d_events; | |
1148 | int ret; | |
6d723736 | 1149 | |
67ead0a6 | 1150 | /* The linker may leave blanks */ |
bd1a5c84 MH |
1151 | if (!call->name) |
1152 | return -EINVAL; | |
701970b3 | 1153 | |
0405ab80 SR |
1154 | if (call->class->raw_init) { |
1155 | ret = call->class->raw_init(call); | |
bd1a5c84 MH |
1156 | if (ret < 0) { |
1157 | if (ret != -ENOSYS) | |
67ead0a6 LZ |
1158 | pr_warning("Could not initialize trace events/%s\n", |
1159 | call->name); | |
bd1a5c84 MH |
1160 | return ret; |
1161 | } | |
1162 | } | |
701970b3 | 1163 | |
bd1a5c84 MH |
1164 | d_events = event_trace_events_dir(); |
1165 | if (!d_events) | |
1166 | return -ENOENT; | |
1167 | ||
67ead0a6 | 1168 | ret = event_create_dir(call, d_events, id, enable, filter, format); |
88f70d75 MH |
1169 | if (!ret) |
1170 | list_add(&call->list, &ftrace_events); | |
67ead0a6 | 1171 | call->mod = mod; |
88f70d75 | 1172 | |
588bebb7 | 1173 | return ret; |
bd1a5c84 MH |
1174 | } |
1175 | ||
1176 | /* Add an additional event_call dynamically */ | |
1177 | int trace_add_event_call(struct ftrace_event_call *call) | |
1178 | { | |
1179 | int ret; | |
1180 | mutex_lock(&event_mutex); | |
67ead0a6 LZ |
1181 | ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops, |
1182 | &ftrace_enable_fops, | |
1183 | &ftrace_event_filter_fops, | |
1184 | &ftrace_event_format_fops); | |
bd1a5c84 MH |
1185 | mutex_unlock(&event_mutex); |
1186 | return ret; | |
1187 | } | |
701970b3 | 1188 | |
a2ca5e03 FW |
1189 | static void remove_subsystem_dir(const char *name) |
1190 | { | |
1191 | struct event_subsystem *system; | |
1192 | ||
1193 | if (strcmp(name, TRACE_SYSTEM) == 0) | |
1194 | return; | |
1195 | ||
1196 | list_for_each_entry(system, &event_subsystems, list) { | |
1197 | if (strcmp(system->name, name) == 0) { | |
1198 | if (!--system->nr_events) { | |
1199 | struct event_filter *filter = system->filter; | |
1200 | ||
1201 | debugfs_remove_recursive(system->entry); | |
1202 | list_del(&system->list); | |
1203 | if (filter) { | |
1204 | kfree(filter->filter_string); | |
1205 | kfree(filter); | |
1206 | } | |
1207 | kfree(system->name); | |
1208 | kfree(system); | |
1209 | } | |
1210 | break; | |
1211 | } | |
1212 | } | |
1213 | } | |
1214 | ||
4fead8e4 MH |
1215 | /* |
1216 | * Must be called under locking both of event_mutex and trace_event_mutex. | |
1217 | */ | |
bd1a5c84 MH |
1218 | static void __trace_remove_event_call(struct ftrace_event_call *call) |
1219 | { | |
1220 | ftrace_event_enable_disable(call, 0); | |
80decc70 SR |
1221 | if (call->event.funcs) |
1222 | __unregister_ftrace_event(&call->event); | |
bd1a5c84 MH |
1223 | debugfs_remove_recursive(call->dir); |
1224 | list_del(&call->list); | |
1225 | trace_destroy_fields(call); | |
1226 | destroy_preds(call); | |
8f082018 | 1227 | remove_subsystem_dir(call->class->system); |
bd1a5c84 MH |
1228 | } |
1229 | ||
1230 | /* Remove an event_call */ | |
1231 | void trace_remove_event_call(struct ftrace_event_call *call) | |
1232 | { | |
1233 | mutex_lock(&event_mutex); | |
4fead8e4 | 1234 | down_write(&trace_event_mutex); |
bd1a5c84 | 1235 | __trace_remove_event_call(call); |
4fead8e4 | 1236 | up_write(&trace_event_mutex); |
bd1a5c84 MH |
1237 | mutex_unlock(&event_mutex); |
1238 | } | |
1239 | ||
1240 | #define for_each_event(event, start, end) \ | |
1241 | for (event = start; \ | |
1242 | (unsigned long)event < (unsigned long)end; \ | |
1243 | event++) | |
1244 | ||
1245 | #ifdef CONFIG_MODULES | |
1246 | ||
1247 | static LIST_HEAD(ftrace_module_file_list); | |
1248 | ||
1249 | /* | |
1250 | * Modules must own their file_operations to keep up with | |
1251 | * reference counting. | |
1252 | */ | |
1253 | struct ftrace_module_file_ops { | |
1254 | struct list_head list; | |
1255 | struct module *mod; | |
1256 | struct file_operations id; | |
1257 | struct file_operations enable; | |
1258 | struct file_operations format; | |
1259 | struct file_operations filter; | |
1260 | }; | |
1261 | ||
701970b3 SR |
1262 | static struct ftrace_module_file_ops * |
1263 | trace_create_file_ops(struct module *mod) | |
1264 | { | |
1265 | struct ftrace_module_file_ops *file_ops; | |
1266 | ||
1267 | /* | |
1268 | * This is a bit of a PITA. To allow for correct reference | |
1269 | * counting, modules must "own" their file_operations. | |
1270 | * To do this, we allocate the file operations that will be | |
1271 | * used in the event directory. | |
1272 | */ | |
1273 | ||
1274 | file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL); | |
1275 | if (!file_ops) | |
1276 | return NULL; | |
1277 | ||
1278 | file_ops->mod = mod; | |
1279 | ||
1280 | file_ops->id = ftrace_event_id_fops; | |
1281 | file_ops->id.owner = mod; | |
1282 | ||
1283 | file_ops->enable = ftrace_enable_fops; | |
1284 | file_ops->enable.owner = mod; | |
1285 | ||
1286 | file_ops->filter = ftrace_event_filter_fops; | |
1287 | file_ops->filter.owner = mod; | |
1288 | ||
1289 | file_ops->format = ftrace_event_format_fops; | |
1290 | file_ops->format.owner = mod; | |
1291 | ||
1292 | list_add(&file_ops->list, &ftrace_module_file_list); | |
1293 | ||
1294 | return file_ops; | |
1295 | } | |
1296 | ||
6d723736 SR |
1297 | static void trace_module_add_events(struct module *mod) |
1298 | { | |
701970b3 | 1299 | struct ftrace_module_file_ops *file_ops = NULL; |
6d723736 | 1300 | struct ftrace_event_call *call, *start, *end; |
6d723736 SR |
1301 | |
1302 | start = mod->trace_events; | |
1303 | end = mod->trace_events + mod->num_trace_events; | |
1304 | ||
1305 | if (start == end) | |
1306 | return; | |
1307 | ||
67ead0a6 LZ |
1308 | file_ops = trace_create_file_ops(mod); |
1309 | if (!file_ops) | |
6d723736 SR |
1310 | return; |
1311 | ||
1312 | for_each_event(call, start, end) { | |
67ead0a6 | 1313 | __trace_add_event_call(call, mod, |
88f70d75 MH |
1314 | &file_ops->id, &file_ops->enable, |
1315 | &file_ops->filter, &file_ops->format); | |
6d723736 SR |
1316 | } |
1317 | } | |
1318 | ||
1319 | static void trace_module_remove_events(struct module *mod) | |
1320 | { | |
701970b3 | 1321 | struct ftrace_module_file_ops *file_ops; |
6d723736 | 1322 | struct ftrace_event_call *call, *p; |
9456f0fa | 1323 | bool found = false; |
6d723736 | 1324 | |
110bf2b7 | 1325 | down_write(&trace_event_mutex); |
6d723736 SR |
1326 | list_for_each_entry_safe(call, p, &ftrace_events, list) { |
1327 | if (call->mod == mod) { | |
9456f0fa | 1328 | found = true; |
bd1a5c84 | 1329 | __trace_remove_event_call(call); |
6d723736 SR |
1330 | } |
1331 | } | |
701970b3 SR |
1332 | |
1333 | /* Now free the file_operations */ | |
1334 | list_for_each_entry(file_ops, &ftrace_module_file_list, list) { | |
1335 | if (file_ops->mod == mod) | |
1336 | break; | |
1337 | } | |
1338 | if (&file_ops->list != &ftrace_module_file_list) { | |
1339 | list_del(&file_ops->list); | |
1340 | kfree(file_ops); | |
1341 | } | |
9456f0fa SR |
1342 | |
1343 | /* | |
1344 | * It is safest to reset the ring buffer if the module being unloaded | |
1345 | * registered any events. | |
1346 | */ | |
1347 | if (found) | |
1348 | tracing_reset_current_online_cpus(); | |
110bf2b7 | 1349 | up_write(&trace_event_mutex); |
6d723736 SR |
1350 | } |
1351 | ||
61f919a1 SR |
1352 | static int trace_module_notify(struct notifier_block *self, |
1353 | unsigned long val, void *data) | |
6d723736 SR |
1354 | { |
1355 | struct module *mod = data; | |
1356 | ||
1357 | mutex_lock(&event_mutex); | |
1358 | switch (val) { | |
1359 | case MODULE_STATE_COMING: | |
1360 | trace_module_add_events(mod); | |
1361 | break; | |
1362 | case MODULE_STATE_GOING: | |
1363 | trace_module_remove_events(mod); | |
1364 | break; | |
1365 | } | |
1366 | mutex_unlock(&event_mutex); | |
fd994989 | 1367 | |
1473e441 SR |
1368 | return 0; |
1369 | } | |
61f919a1 SR |
1370 | #else |
1371 | static int trace_module_notify(struct notifier_block *self, | |
1372 | unsigned long val, void *data) | |
1373 | { | |
1374 | return 0; | |
1375 | } | |
1376 | #endif /* CONFIG_MODULES */ | |
1473e441 | 1377 | |
ec827c7e | 1378 | static struct notifier_block trace_module_nb = { |
6d723736 SR |
1379 | .notifier_call = trace_module_notify, |
1380 | .priority = 0, | |
1381 | }; | |
1382 | ||
a59fd602 SR |
1383 | extern struct ftrace_event_call __start_ftrace_events[]; |
1384 | extern struct ftrace_event_call __stop_ftrace_events[]; | |
1385 | ||
020e5f85 LZ |
1386 | static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; |
1387 | ||
1388 | static __init int setup_trace_event(char *str) | |
1389 | { | |
1390 | strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE); | |
1391 | ring_buffer_expanded = 1; | |
1392 | tracing_selftest_disabled = 1; | |
1393 | ||
1394 | return 1; | |
1395 | } | |
1396 | __setup("trace_event=", setup_trace_event); | |
1397 | ||
b77e38aa SR |
1398 | static __init int event_trace_init(void) |
1399 | { | |
a59fd602 | 1400 | struct ftrace_event_call *call; |
b77e38aa SR |
1401 | struct dentry *d_tracer; |
1402 | struct dentry *entry; | |
1473e441 | 1403 | struct dentry *d_events; |
6d723736 | 1404 | int ret; |
020e5f85 LZ |
1405 | char *buf = bootup_event_buf; |
1406 | char *token; | |
b77e38aa SR |
1407 | |
1408 | d_tracer = tracing_init_dentry(); | |
1409 | if (!d_tracer) | |
1410 | return 0; | |
1411 | ||
2314c4ae SR |
1412 | entry = debugfs_create_file("available_events", 0444, d_tracer, |
1413 | (void *)&show_event_seq_ops, | |
1414 | &ftrace_avail_fops); | |
1415 | if (!entry) | |
1416 | pr_warning("Could not create debugfs " | |
1417 | "'available_events' entry\n"); | |
1418 | ||
b77e38aa SR |
1419 | entry = debugfs_create_file("set_event", 0644, d_tracer, |
1420 | (void *)&show_set_event_seq_ops, | |
1421 | &ftrace_set_event_fops); | |
1422 | if (!entry) | |
1423 | pr_warning("Could not create debugfs " | |
1424 | "'set_event' entry\n"); | |
1425 | ||
1473e441 SR |
1426 | d_events = event_trace_events_dir(); |
1427 | if (!d_events) | |
1428 | return 0; | |
1429 | ||
d1b182a8 SR |
1430 | /* ring buffer internal formats */ |
1431 | trace_create_file("header_page", 0444, d_events, | |
1432 | ring_buffer_print_page_header, | |
1433 | &ftrace_show_header_fops); | |
1434 | ||
1435 | trace_create_file("header_event", 0444, d_events, | |
1436 | ring_buffer_print_entry_header, | |
1437 | &ftrace_show_header_fops); | |
1438 | ||
8ae79a13 | 1439 | trace_create_file("enable", 0644, d_events, |
8f31bfe5 | 1440 | NULL, &ftrace_system_enable_fops); |
8ae79a13 | 1441 | |
8728fe50 LZ |
1442 | if (trace_define_common_fields()) |
1443 | pr_warning("tracing: Failed to allocate common fields"); | |
1444 | ||
6d723736 | 1445 | for_each_event(call, __start_ftrace_events, __stop_ftrace_events) { |
67ead0a6 | 1446 | __trace_add_event_call(call, NULL, &ftrace_event_id_fops, |
88f70d75 MH |
1447 | &ftrace_enable_fops, |
1448 | &ftrace_event_filter_fops, | |
1449 | &ftrace_event_format_fops); | |
1473e441 SR |
1450 | } |
1451 | ||
020e5f85 LZ |
1452 | while (true) { |
1453 | token = strsep(&buf, ","); | |
1454 | ||
1455 | if (!token) | |
1456 | break; | |
1457 | if (!*token) | |
1458 | continue; | |
1459 | ||
1460 | ret = ftrace_set_clr_event(token, 1); | |
1461 | if (ret) | |
1462 | pr_warning("Failed to enable trace event: %s\n", token); | |
1463 | } | |
1464 | ||
6d723736 | 1465 | ret = register_module_notifier(&trace_module_nb); |
55379376 | 1466 | if (ret) |
6d723736 SR |
1467 | pr_warning("Failed to register trace events module notifier\n"); |
1468 | ||
b77e38aa SR |
1469 | return 0; |
1470 | } | |
1471 | fs_initcall(event_trace_init); | |
e6187007 SR |
1472 | |
1473 | #ifdef CONFIG_FTRACE_STARTUP_TEST | |
1474 | ||
1475 | static DEFINE_SPINLOCK(test_spinlock); | |
1476 | static DEFINE_SPINLOCK(test_spinlock_irq); | |
1477 | static DEFINE_MUTEX(test_mutex); | |
1478 | ||
1479 | static __init void test_work(struct work_struct *dummy) | |
1480 | { | |
1481 | spin_lock(&test_spinlock); | |
1482 | spin_lock_irq(&test_spinlock_irq); | |
1483 | udelay(1); | |
1484 | spin_unlock_irq(&test_spinlock_irq); | |
1485 | spin_unlock(&test_spinlock); | |
1486 | ||
1487 | mutex_lock(&test_mutex); | |
1488 | msleep(1); | |
1489 | mutex_unlock(&test_mutex); | |
1490 | } | |
1491 | ||
1492 | static __init int event_test_thread(void *unused) | |
1493 | { | |
1494 | void *test_malloc; | |
1495 | ||
1496 | test_malloc = kmalloc(1234, GFP_KERNEL); | |
1497 | if (!test_malloc) | |
1498 | pr_info("failed to kmalloc\n"); | |
1499 | ||
1500 | schedule_on_each_cpu(test_work); | |
1501 | ||
1502 | kfree(test_malloc); | |
1503 | ||
1504 | set_current_state(TASK_INTERRUPTIBLE); | |
1505 | while (!kthread_should_stop()) | |
1506 | schedule(); | |
1507 | ||
1508 | return 0; | |
1509 | } | |
1510 | ||
1511 | /* | |
1512 | * Do various things that may trigger events. | |
1513 | */ | |
1514 | static __init void event_test_stuff(void) | |
1515 | { | |
1516 | struct task_struct *test_thread; | |
1517 | ||
1518 | test_thread = kthread_run(event_test_thread, NULL, "test-events"); | |
1519 | msleep(1); | |
1520 | kthread_stop(test_thread); | |
1521 | } | |
1522 | ||
1523 | /* | |
1524 | * For every trace event defined, we will test each trace point separately, | |
1525 | * and then by groups, and finally all trace points. | |
1526 | */ | |
9ea21c1e | 1527 | static __init void event_trace_self_tests(void) |
e6187007 SR |
1528 | { |
1529 | struct ftrace_event_call *call; | |
1530 | struct event_subsystem *system; | |
e6187007 SR |
1531 | int ret; |
1532 | ||
1533 | pr_info("Running tests on trace events:\n"); | |
1534 | ||
1535 | list_for_each_entry(call, &ftrace_events, list) { | |
1536 | ||
2239291a SR |
1537 | /* Only test those that have a probe */ |
1538 | if (!call->class || !call->class->probe) | |
e6187007 SR |
1539 | continue; |
1540 | ||
1f5a6b45 SR |
1541 | /* |
1542 | * Testing syscall events here is pretty useless, but | |
1543 | * we still do it if configured. But this is time consuming. | |
1544 | * What we really need is a user thread to perform the | |
1545 | * syscalls as we test. | |
1546 | */ | |
1547 | #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS | |
8f082018 SR |
1548 | if (call->class->system && |
1549 | strcmp(call->class->system, "syscalls") == 0) | |
1f5a6b45 SR |
1550 | continue; |
1551 | #endif | |
1552 | ||
e6187007 SR |
1553 | pr_info("Testing event %s: ", call->name); |
1554 | ||
1555 | /* | |
1556 | * If an event is already enabled, someone is using | |
1557 | * it and the self test should not be on. | |
1558 | */ | |
553552ce | 1559 | if (call->flags & TRACE_EVENT_FL_ENABLED) { |
e6187007 SR |
1560 | pr_warning("Enabled event during self test!\n"); |
1561 | WARN_ON_ONCE(1); | |
1562 | continue; | |
1563 | } | |
1564 | ||
0e907c99 | 1565 | ftrace_event_enable_disable(call, 1); |
e6187007 | 1566 | event_test_stuff(); |
0e907c99 | 1567 | ftrace_event_enable_disable(call, 0); |
e6187007 SR |
1568 | |
1569 | pr_cont("OK\n"); | |
1570 | } | |
1571 | ||
1572 | /* Now test at the sub system level */ | |
1573 | ||
1574 | pr_info("Running tests on trace event systems:\n"); | |
1575 | ||
1576 | list_for_each_entry(system, &event_subsystems, list) { | |
1577 | ||
1578 | /* the ftrace system is special, skip it */ | |
1579 | if (strcmp(system->name, "ftrace") == 0) | |
1580 | continue; | |
1581 | ||
1582 | pr_info("Testing event system %s: ", system->name); | |
1583 | ||
8f31bfe5 | 1584 | ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1); |
e6187007 SR |
1585 | if (WARN_ON_ONCE(ret)) { |
1586 | pr_warning("error enabling system %s\n", | |
1587 | system->name); | |
1588 | continue; | |
1589 | } | |
1590 | ||
1591 | event_test_stuff(); | |
1592 | ||
8f31bfe5 | 1593 | ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0); |
e6187007 SR |
1594 | if (WARN_ON_ONCE(ret)) |
1595 | pr_warning("error disabling system %s\n", | |
1596 | system->name); | |
1597 | ||
1598 | pr_cont("OK\n"); | |
1599 | } | |
1600 | ||
1601 | /* Test with all events enabled */ | |
1602 | ||
1603 | pr_info("Running tests on all trace events:\n"); | |
1604 | pr_info("Testing all events: "); | |
1605 | ||
8f31bfe5 | 1606 | ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1); |
e6187007 | 1607 | if (WARN_ON_ONCE(ret)) { |
e6187007 | 1608 | pr_warning("error enabling all events\n"); |
9ea21c1e | 1609 | return; |
e6187007 SR |
1610 | } |
1611 | ||
1612 | event_test_stuff(); | |
1613 | ||
1614 | /* reset sysname */ | |
8f31bfe5 | 1615 | ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0); |
e6187007 SR |
1616 | if (WARN_ON_ONCE(ret)) { |
1617 | pr_warning("error disabling all events\n"); | |
9ea21c1e | 1618 | return; |
e6187007 SR |
1619 | } |
1620 | ||
1621 | pr_cont("OK\n"); | |
9ea21c1e SR |
1622 | } |
1623 | ||
1624 | #ifdef CONFIG_FUNCTION_TRACER | |
1625 | ||
245b2e70 | 1626 | static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); |
9ea21c1e SR |
1627 | |
1628 | static void | |
1629 | function_test_events_call(unsigned long ip, unsigned long parent_ip) | |
1630 | { | |
1631 | struct ring_buffer_event *event; | |
e77405ad | 1632 | struct ring_buffer *buffer; |
9ea21c1e SR |
1633 | struct ftrace_entry *entry; |
1634 | unsigned long flags; | |
1635 | long disabled; | |
9ea21c1e SR |
1636 | int cpu; |
1637 | int pc; | |
1638 | ||
1639 | pc = preempt_count(); | |
5168ae50 | 1640 | preempt_disable_notrace(); |
9ea21c1e | 1641 | cpu = raw_smp_processor_id(); |
245b2e70 | 1642 | disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); |
9ea21c1e SR |
1643 | |
1644 | if (disabled != 1) | |
1645 | goto out; | |
1646 | ||
1647 | local_save_flags(flags); | |
1648 | ||
e77405ad SR |
1649 | event = trace_current_buffer_lock_reserve(&buffer, |
1650 | TRACE_FN, sizeof(*entry), | |
9ea21c1e SR |
1651 | flags, pc); |
1652 | if (!event) | |
1653 | goto out; | |
1654 | entry = ring_buffer_event_data(event); | |
1655 | entry->ip = ip; | |
1656 | entry->parent_ip = parent_ip; | |
1657 | ||
e77405ad | 1658 | trace_nowake_buffer_unlock_commit(buffer, event, flags, pc); |
9ea21c1e SR |
1659 | |
1660 | out: | |
245b2e70 | 1661 | atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); |
5168ae50 | 1662 | preempt_enable_notrace(); |
9ea21c1e SR |
1663 | } |
1664 | ||
1665 | static struct ftrace_ops trace_ops __initdata = | |
1666 | { | |
1667 | .func = function_test_events_call, | |
1668 | }; | |
1669 | ||
1670 | static __init void event_trace_self_test_with_function(void) | |
1671 | { | |
1672 | register_ftrace_function(&trace_ops); | |
1673 | pr_info("Running tests again, along with the function tracer\n"); | |
1674 | event_trace_self_tests(); | |
1675 | unregister_ftrace_function(&trace_ops); | |
1676 | } | |
1677 | #else | |
1678 | static __init void event_trace_self_test_with_function(void) | |
1679 | { | |
1680 | } | |
1681 | #endif | |
1682 | ||
1683 | static __init int event_trace_self_tests_init(void) | |
1684 | { | |
020e5f85 LZ |
1685 | if (!tracing_selftest_disabled) { |
1686 | event_trace_self_tests(); | |
1687 | event_trace_self_test_with_function(); | |
1688 | } | |
e6187007 SR |
1689 | |
1690 | return 0; | |
1691 | } | |
1692 | ||
28d20e2d | 1693 | late_initcall(event_trace_self_tests_init); |
e6187007 SR |
1694 | |
1695 | #endif |