Branch data Line data Source code
1 : : /*
2 : : * event tracer
3 : : *
4 : : * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 : : *
6 : : * - Added format output of fields of the trace point.
7 : : * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 : : *
9 : : */
10 : :
11 : : #include <linux/workqueue.h>
12 : : #include <linux/spinlock.h>
13 : : #include <linux/kthread.h>
14 : : #include <linux/debugfs.h>
15 : : #include <linux/uaccess.h>
16 : : #include <linux/module.h>
17 : : #include <linux/ctype.h>
18 : : #include <linux/slab.h>
19 : : #include <linux/delay.h>
20 : :
21 : : #include <asm/setup.h>
22 : :
23 : : #include "trace_output.h"
24 : :
25 : : #undef TRACE_SYSTEM
26 : : #define TRACE_SYSTEM "TRACE_SYSTEM"
27 : :
28 : : DEFINE_MUTEX(event_mutex);
29 : :
30 : : LIST_HEAD(ftrace_events);
31 : : static LIST_HEAD(ftrace_common_fields);
32 : :
33 : : #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
34 : :
35 : : static struct kmem_cache *field_cachep;
36 : : static struct kmem_cache *file_cachep;
37 : :
38 : : #define SYSTEM_FL_FREE_NAME (1 << 31)
39 : :
40 : : static inline int system_refcount(struct event_subsystem *system)
41 : : {
42 : 0 : return system->ref_count & ~SYSTEM_FL_FREE_NAME;
43 : : }
44 : :
45 : : static int system_refcount_inc(struct event_subsystem *system)
46 : : {
47 : 0 : return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
48 : : }
49 : :
50 : : static int system_refcount_dec(struct event_subsystem *system)
51 : : {
52 : 0 : return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
53 : : }
54 : :
55 : : /* Double loops, do not use break, only goto's work */
56 : : #define do_for_each_event_file(tr, file) \
57 : : list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
58 : : list_for_each_entry(file, &tr->events, list)
59 : :
60 : : #define do_for_each_event_file_safe(tr, file) \
61 : : list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
62 : : struct ftrace_event_file *___n; \
63 : : list_for_each_entry_safe(file, ___n, &tr->events, list)
64 : :
65 : : #define while_for_each_event_file() \
66 : : }
67 : :
68 : : static struct list_head *
69 : : trace_get_fields(struct ftrace_event_call *event_call)
70 : : {
71 [ # # ][ # # ]: 0 : if (!event_call->class->get_fields)
[ # # ][ # # ]
[ # # ]
72 : 0 : return &event_call->class->fields;
73 : 0 : return event_call->class->get_fields(event_call);
74 : : }
75 : :
76 : : static struct ftrace_event_field *
77 : 0 : __find_event_field(struct list_head *head, char *name)
78 : : {
79 : : struct ftrace_event_field *field;
80 : :
81 [ # # ]: 0 : list_for_each_entry(field, head, link) {
82 [ # # ]: 0 : if (!strcmp(field->name, name))
83 : : return field;
84 : : }
85 : :
86 : : return NULL;
87 : : }
88 : :
89 : : struct ftrace_event_field *
90 : 0 : trace_find_event_field(struct ftrace_event_call *call, char *name)
91 : : {
92 : : struct ftrace_event_field *field;
93 : : struct list_head *head;
94 : :
95 : 0 : field = __find_event_field(&ftrace_common_fields, name);
96 [ # # ]: 0 : if (field)
97 : : return field;
98 : :
99 : : head = trace_get_fields(call);
100 : 0 : return __find_event_field(head, name);
101 : : }
102 : :
103 : 0 : static int __trace_define_field(struct list_head *head, const char *type,
104 : : const char *name, int offset, int size,
105 : : int is_signed, int filter_type)
106 : : {
107 : : struct ftrace_event_field *field;
108 : :
109 : 0 : field = kmem_cache_alloc(field_cachep, GFP_TRACE);
110 [ # # ]: 0 : if (!field)
111 : : return -ENOMEM;
112 : :
113 : 0 : field->name = name;
114 : 0 : field->type = type;
115 : :
116 [ # # ]: 0 : if (filter_type == FILTER_OTHER)
117 : 0 : field->filter_type = filter_assign_type(type);
118 : : else
119 : 0 : field->filter_type = filter_type;
120 : :
121 : 0 : field->offset = offset;
122 : 0 : field->size = size;
123 : 0 : field->is_signed = is_signed;
124 : :
125 : 0 : list_add(&field->link, head);
126 : :
127 : 0 : return 0;
128 : : }
129 : :
130 : 0 : int trace_define_field(struct ftrace_event_call *call, const char *type,
131 : : const char *name, int offset, int size, int is_signed,
132 : : int filter_type)
133 : : {
134 : : struct list_head *head;
135 : :
136 [ # # ][ # # ]: 0 : if (WARN_ON(!call->class))
137 : : return 0;
138 : :
139 : : head = trace_get_fields(call);
140 : 0 : return __trace_define_field(head, type, name, offset, size,
141 : : is_signed, filter_type);
142 : : }
143 : : EXPORT_SYMBOL_GPL(trace_define_field);
144 : :
145 : : #define __common_field(type, item) \
146 : : ret = __trace_define_field(&ftrace_common_fields, #type, \
147 : : "common_" #item, \
148 : : offsetof(typeof(ent), item), \
149 : : sizeof(ent.item), \
150 : : is_signed_type(type), FILTER_OTHER); \
151 : : if (ret) \
152 : : return ret;
153 : :
154 : 0 : static int trace_define_common_fields(void)
155 : : {
156 : : int ret;
157 : : struct trace_entry ent;
158 : :
159 [ # # ]: 0 : __common_field(unsigned short, type);
160 [ # # ]: 0 : __common_field(unsigned char, flags);
161 [ # # ]: 0 : __common_field(unsigned char, preempt_count);
162 : 0 : __common_field(int, pid);
163 : :
164 : : return ret;
165 : : }
166 : :
167 : 0 : static void trace_destroy_fields(struct ftrace_event_call *call)
168 : : {
169 : : struct ftrace_event_field *field, *next;
170 : : struct list_head *head;
171 : :
172 : : head = trace_get_fields(call);
173 [ # # ]: 0 : list_for_each_entry_safe(field, next, head, link) {
174 : : list_del(&field->link);
175 : 0 : kmem_cache_free(field_cachep, field);
176 : : }
177 : 0 : }
178 : :
179 : 0 : int trace_event_raw_init(struct ftrace_event_call *call)
180 : : {
181 : : int id;
182 : :
183 : 0 : id = register_ftrace_event(&call->event);
184 [ # # ]: 0 : if (!id)
185 : : return -ENODEV;
186 : :
187 : 0 : return 0;
188 : : }
189 : : EXPORT_SYMBOL_GPL(trace_event_raw_init);
190 : :
191 : 0 : int ftrace_event_reg(struct ftrace_event_call *call,
192 : : enum trace_reg type, void *data)
193 : : {
194 : : struct ftrace_event_file *file = data;
195 : :
196 [ # # # # : 0 : switch (type) {
# ]
197 : : case TRACE_REG_REGISTER:
198 : 0 : return tracepoint_probe_register(call->name,
199 : 0 : call->class->probe,
200 : : file);
201 : : case TRACE_REG_UNREGISTER:
202 : 0 : tracepoint_probe_unregister(call->name,
203 : 0 : call->class->probe,
204 : : file);
205 : 0 : return 0;
206 : :
207 : : #ifdef CONFIG_PERF_EVENTS
208 : : case TRACE_REG_PERF_REGISTER:
209 : 0 : return tracepoint_probe_register(call->name,
210 : 0 : call->class->perf_probe,
211 : : call);
212 : : case TRACE_REG_PERF_UNREGISTER:
213 : 0 : tracepoint_probe_unregister(call->name,
214 : 0 : call->class->perf_probe,
215 : : call);
216 : 0 : return 0;
217 : : case TRACE_REG_PERF_OPEN:
218 : : case TRACE_REG_PERF_CLOSE:
219 : : case TRACE_REG_PERF_ADD:
220 : : case TRACE_REG_PERF_DEL:
221 : : return 0;
222 : : #endif
223 : : }
224 : : return 0;
225 : : }
226 : : EXPORT_SYMBOL_GPL(ftrace_event_reg);
227 : :
228 : 0 : void trace_event_enable_cmd_record(bool enable)
229 : : {
230 : : struct ftrace_event_file *file;
231 : : struct trace_array *tr;
232 : :
233 : 0 : mutex_lock(&event_mutex);
234 [ # # ][ # # ]: 0 : do_for_each_event_file(tr, file) {
235 : :
236 [ # # ]: 0 : if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
237 : 0 : continue;
238 : :
239 [ # # ]: 0 : if (enable) {
240 : 0 : tracing_start_cmdline_record();
241 : 0 : set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
242 : : } else {
243 : 0 : tracing_stop_cmdline_record();
244 : 0 : clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
245 : : }
246 : : } while_for_each_event_file();
247 : 0 : mutex_unlock(&event_mutex);
248 : 0 : }
249 : :
250 : 0 : static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
251 : : int enable, int soft_disable)
252 : : {
253 : 0 : struct ftrace_event_call *call = file->event_call;
254 : : int ret = 0;
255 : : int disable;
256 : :
257 [ # # # ]: 0 : switch (enable) {
258 : : case 0:
259 : : /*
260 : : * When soft_disable is set and enable is cleared, the sm_ref
261 : : * reference counter is decremented. If it reaches 0, we want
262 : : * to clear the SOFT_DISABLED flag but leave the event in the
263 : : * state that it was. That is, if the event was enabled and
264 : : * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
265 : : * is set we do not want the event to be enabled before we
266 : : * clear the bit.
267 : : *
268 : : * When soft_disable is not set but the SOFT_MODE flag is,
269 : : * we do nothing. Do not disable the tracepoint, otherwise
270 : : * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
271 : : */
272 [ # # ]: 0 : if (soft_disable) {
273 [ # # ]: 0 : if (atomic_dec_return(&file->sm_ref) > 0)
274 : : break;
275 : 0 : disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
276 : 0 : clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
277 : : } else
278 : 0 : disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
279 : :
280 [ # # ][ # # ]: 0 : if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
281 : 0 : clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
282 [ # # ]: 0 : if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
283 : 0 : tracing_stop_cmdline_record();
284 : 0 : clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
285 : : }
286 : 0 : call->class->reg(call, TRACE_REG_UNREGISTER, file);
287 : : }
288 : : /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
289 [ # # ]: 0 : if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
290 : 0 : set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
291 : : else
292 : 0 : clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
293 : : break;
294 : : case 1:
295 : : /*
296 : : * When soft_disable is set and enable is set, we want to
297 : : * register the tracepoint for the event, but leave the event
298 : : * as is. That means, if the event was already enabled, we do
299 : : * nothing (but set SOFT_MODE). If the event is disabled, we
300 : : * set SOFT_DISABLED before enabling the event tracepoint, so
301 : : * it still seems to be disabled.
302 : : */
303 [ # # ]: 0 : if (!soft_disable)
304 : 0 : clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
305 : : else {
306 [ # # ]: 0 : if (atomic_inc_return(&file->sm_ref) > 1)
307 : : break;
308 : 0 : set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
309 : : }
310 : :
311 [ # # ]: 0 : if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
312 : :
313 : : /* Keep the event disabled, when going to SOFT_MODE. */
314 [ # # ]: 0 : if (soft_disable)
315 : 0 : set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
316 : :
317 [ # # ]: 0 : if (trace_flags & TRACE_ITER_RECORD_CMD) {
318 : 0 : tracing_start_cmdline_record();
319 : 0 : set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
320 : : }
321 : 0 : ret = call->class->reg(call, TRACE_REG_REGISTER, file);
322 [ # # ]: 0 : if (ret) {
323 : 0 : tracing_stop_cmdline_record();
324 : 0 : pr_info("event trace: Could not enable event "
325 : : "%s\n", call->name);
326 : 0 : break;
327 : : }
328 : 0 : set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
329 : :
330 : : /* WAS_ENABLED gets set but never cleared. */
331 : 0 : call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
332 : : }
333 : : break;
334 : : }
335 : :
336 : 0 : return ret;
337 : : }
338 : :
339 : 0 : int trace_event_enable_disable(struct ftrace_event_file *file,
340 : : int enable, int soft_disable)
341 : : {
342 : 0 : return __ftrace_event_enable_disable(file, enable, soft_disable);
343 : : }
344 : :
345 : : static int ftrace_event_enable_disable(struct ftrace_event_file *file,
346 : : int enable)
347 : : {
348 : 0 : return __ftrace_event_enable_disable(file, enable, 0);
349 : : }
350 : :
351 : 0 : static void ftrace_clear_events(struct trace_array *tr)
352 : : {
353 : : struct ftrace_event_file *file;
354 : :
355 : 0 : mutex_lock(&event_mutex);
356 [ # # ]: 0 : list_for_each_entry(file, &tr->events, list) {
357 : : ftrace_event_enable_disable(file, 0);
358 : : }
359 : 0 : mutex_unlock(&event_mutex);
360 : 0 : }
361 : :
362 : 0 : static void __put_system(struct event_subsystem *system)
363 : : {
364 : 0 : struct event_filter *filter = system->filter;
365 : :
366 [ # # ][ # # ]: 0 : WARN_ON_ONCE(system_refcount(system) == 0);
[ # # ]
367 [ # # ]: 0 : if (system_refcount_dec(system))
368 : 0 : return;
369 : :
370 : : list_del(&system->list);
371 : :
372 [ # # ]: 0 : if (filter) {
373 : 0 : kfree(filter->filter_string);
374 : 0 : kfree(filter);
375 : : }
376 [ # # ]: 0 : if (system->ref_count & SYSTEM_FL_FREE_NAME)
377 : 0 : kfree(system->name);
378 : 0 : kfree(system);
379 : : }
380 : :
381 : 0 : static void __get_system(struct event_subsystem *system)
382 : : {
383 [ # # ][ # # ]: 0 : WARN_ON_ONCE(system_refcount(system) == 0);
[ # # ]
384 : : system_refcount_inc(system);
385 : 0 : }
386 : :
387 : 0 : static void __get_system_dir(struct ftrace_subsystem_dir *dir)
388 : : {
389 [ # # ][ # # ]: 0 : WARN_ON_ONCE(dir->ref_count == 0);
[ # # ]
390 : 0 : dir->ref_count++;
391 : 0 : __get_system(dir->subsystem);
392 : 0 : }
393 : :
394 : 0 : static void __put_system_dir(struct ftrace_subsystem_dir *dir)
395 : : {
396 [ # # ][ # # ]: 0 : WARN_ON_ONCE(dir->ref_count == 0);
[ # # ]
397 : : /* If the subsystem is about to be freed, the dir must be too */
398 [ # # ][ # # ]: 0 : WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
[ # # ][ # # ]
[ # # ]
399 : :
400 : 0 : __put_system(dir->subsystem);
401 [ # # ]: 0 : if (!--dir->ref_count)
402 : 0 : kfree(dir);
403 : 0 : }
404 : :
405 : 0 : static void put_system(struct ftrace_subsystem_dir *dir)
406 : : {
407 : 0 : mutex_lock(&event_mutex);
408 : 0 : __put_system_dir(dir);
409 : 0 : mutex_unlock(&event_mutex);
410 : 0 : }
411 : :
412 : 0 : static void remove_subsystem(struct ftrace_subsystem_dir *dir)
413 : : {
414 [ # # ]: 0 : if (!dir)
415 : 0 : return;
416 : :
417 [ # # ]: 0 : if (!--dir->nr_events) {
418 : 0 : debugfs_remove_recursive(dir->entry);
419 : : list_del(&dir->list);
420 : 0 : __put_system_dir(dir);
421 : : }
422 : : }
423 : :
424 : 0 : static void remove_event_file_dir(struct ftrace_event_file *file)
425 : : {
426 : 0 : struct dentry *dir = file->dir;
427 : : struct dentry *child;
428 : :
429 [ # # ]: 0 : if (dir) {
430 : : spin_lock(&dir->d_lock); /* probably unneeded */
431 [ # # ]: 0 : list_for_each_entry(child, &dir->d_subdirs, d_u.d_child) {
432 [ # # ]: 0 : if (child->d_inode) /* probably unneeded */
433 : 0 : child->d_inode->i_private = NULL;
434 : : }
435 : : spin_unlock(&dir->d_lock);
436 : :
437 : 0 : debugfs_remove_recursive(dir);
438 : : }
439 : :
440 : : list_del(&file->list);
441 : 0 : remove_subsystem(file->system);
442 : 0 : kmem_cache_free(file_cachep, file);
443 : 0 : }
444 : :
445 : : /*
446 : : * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
447 : : */
448 : : static int
449 : 0 : __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
450 : : const char *sub, const char *event, int set)
451 : : {
452 : : struct ftrace_event_file *file;
453 : : struct ftrace_event_call *call;
454 : : int ret = -EINVAL;
455 : :
456 [ # # ]: 0 : list_for_each_entry(file, &tr->events, list) {
457 : :
458 : 0 : call = file->event_call;
459 : :
460 [ # # ][ # # ]: 0 : if (!call->name || !call->class || !call->class->reg)
[ # # ]
461 : 0 : continue;
462 : :
463 [ # # ]: 0 : if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
464 : 0 : continue;
465 : :
466 [ # # ][ # # ]: 0 : if (match &&
467 [ # # ]: 0 : strcmp(match, call->name) != 0 &&
468 : 0 : strcmp(match, call->class->system) != 0)
469 : 0 : continue;
470 : :
471 [ # # ][ # # ]: 0 : if (sub && strcmp(sub, call->class->system) != 0)
472 : 0 : continue;
473 : :
474 [ # # ][ # # ]: 0 : if (event && strcmp(event, call->name) != 0)
475 : 0 : continue;
476 : :
477 : : ftrace_event_enable_disable(file, set);
478 : :
479 : : ret = 0;
480 : : }
481 : :
482 : 0 : return ret;
483 : : }
484 : :
485 : 0 : static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
486 : : const char *sub, const char *event, int set)
487 : : {
488 : : int ret;
489 : :
490 : 0 : mutex_lock(&event_mutex);
491 : 0 : ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
492 : 0 : mutex_unlock(&event_mutex);
493 : :
494 : 0 : return ret;
495 : : }
496 : :
497 : 0 : static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
498 : : {
499 : : char *event = NULL, *sub = NULL, *match;
500 : :
501 : : /*
502 : : * The buf format can be <subsystem>:<event-name>
503 : : * *:<event-name> means any event by that name.
504 : : * :<event-name> is the same.
505 : : *
506 : : * <subsystem>:* means all events in that subsystem
507 : : * <subsystem>: means the same.
508 : : *
509 : : * <name> (no ':') means all events in a subsystem with
510 : : * the name <name> or any event that matches <name>
511 : : */
512 : :
513 : 0 : match = strsep(&buf, ":");
514 [ # # ]: 0 : if (buf) {
515 : : sub = match;
516 : : event = buf;
517 : : match = NULL;
518 : :
519 [ # # ][ # # ]: 0 : if (!strlen(sub) || strcmp(sub, "*") == 0)
520 : : sub = NULL;
521 [ # # ][ # # ]: 0 : if (!strlen(event) || strcmp(event, "*") == 0)
522 : : event = NULL;
523 : : }
524 : :
525 : 0 : return __ftrace_set_clr_event(tr, match, sub, event, set);
526 : : }
527 : :
528 : : /**
529 : : * trace_set_clr_event - enable or disable an event
530 : : * @system: system name to match (NULL for any system)
531 : : * @event: event name to match (NULL for all events, within system)
532 : : * @set: 1 to enable, 0 to disable
533 : : *
534 : : * This is a way for other parts of the kernel to enable or disable
535 : : * event recording.
536 : : *
537 : : * Returns 0 on success, -EINVAL if the parameters do not match any
538 : : * registered events.
539 : : */
540 : 0 : int trace_set_clr_event(const char *system, const char *event, int set)
541 : : {
542 : : struct trace_array *tr = top_trace_array();
543 : :
544 : 0 : return __ftrace_set_clr_event(tr, NULL, system, event, set);
545 : : }
546 : : EXPORT_SYMBOL_GPL(trace_set_clr_event);
547 : :
548 : : /* 128 should be much more than enough */
549 : : #define EVENT_BUF_SIZE 127
550 : :
551 : : static ssize_t
552 : 0 : ftrace_event_write(struct file *file, const char __user *ubuf,
553 : : size_t cnt, loff_t *ppos)
554 : : {
555 : : struct trace_parser parser;
556 : 0 : struct seq_file *m = file->private_data;
557 : 0 : struct trace_array *tr = m->private;
558 : : ssize_t read, ret;
559 : :
560 [ # # ]: 0 : if (!cnt)
561 : : return 0;
562 : :
563 : 0 : ret = tracing_update_buffers();
564 [ # # ]: 0 : if (ret < 0)
565 : : return ret;
566 : :
567 [ # # ]: 0 : if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
568 : : return -ENOMEM;
569 : :
570 : 0 : read = trace_get_user(&parser, ubuf, cnt, ppos);
571 : :
572 [ # # ][ # # ]: 0 : if (read >= 0 && trace_parser_loaded((&parser))) {
573 : : int set = 1;
574 : :
575 [ # # ]: 0 : if (*parser.buffer == '!')
576 : : set = 0;
577 : :
578 : 0 : parser.buffer[parser.idx] = 0;
579 : :
580 : 0 : ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
581 [ # # ]: 0 : if (ret)
582 : : goto out_put;
583 : : }
584 : :
585 : : ret = read;
586 : :
587 : : out_put:
588 : 0 : trace_parser_put(&parser);
589 : :
590 : 0 : return ret;
591 : : }
592 : :
593 : : static void *
594 : 0 : t_next(struct seq_file *m, void *v, loff_t *pos)
595 : : {
596 : : struct ftrace_event_file *file = v;
597 : : struct ftrace_event_call *call;
598 : 0 : struct trace_array *tr = m->private;
599 : :
600 : 0 : (*pos)++;
601 : :
602 [ # # ][ # # ]: 0 : list_for_each_entry_continue(file, &tr->events, list) {
603 : 0 : call = file->event_call;
604 : : /*
605 : : * The ftrace subsystem is for showing formats only.
606 : : * They can not be enabled or disabled via the event files.
607 : : */
608 [ # # ][ # # ]: 0 : if (call->class && call->class->reg)
[ # # ][ # # ]
609 : : return file;
610 : : }
611 : :
612 : : return NULL;
613 : : }
614 : :
615 : 0 : static void *t_start(struct seq_file *m, loff_t *pos)
616 : : {
617 : : struct ftrace_event_file *file;
618 : 0 : struct trace_array *tr = m->private;
619 : : loff_t l;
620 : :
621 : 0 : mutex_lock(&event_mutex);
622 : :
623 : 0 : file = list_entry(&tr->events, struct ftrace_event_file, list);
624 [ # # ]: 0 : for (l = 0; l <= *pos; ) {
625 : : file = t_next(m, file, &l);
626 [ # # ]: 0 : if (!file)
627 : : break;
628 : : }
629 : 0 : return file;
630 : : }
631 : :
632 : : static void *
633 : 0 : s_next(struct seq_file *m, void *v, loff_t *pos)
634 : : {
635 : : struct ftrace_event_file *file = v;
636 : 0 : struct trace_array *tr = m->private;
637 : :
638 : 0 : (*pos)++;
639 : :
640 [ # # ][ # # ]: 0 : list_for_each_entry_continue(file, &tr->events, list) {
641 [ # # ][ # # ]: 0 : if (file->flags & FTRACE_EVENT_FL_ENABLED)
642 : : return file;
643 : : }
644 : :
645 : : return NULL;
646 : : }
647 : :
648 : 0 : static void *s_start(struct seq_file *m, loff_t *pos)
649 : : {
650 : : struct ftrace_event_file *file;
651 : 0 : struct trace_array *tr = m->private;
652 : : loff_t l;
653 : :
654 : 0 : mutex_lock(&event_mutex);
655 : :
656 : 0 : file = list_entry(&tr->events, struct ftrace_event_file, list);
657 [ # # ]: 0 : for (l = 0; l <= *pos; ) {
658 : : file = s_next(m, file, &l);
659 [ # # ]: 0 : if (!file)
660 : : break;
661 : : }
662 : 0 : return file;
663 : : }
664 : :
665 : 0 : static int t_show(struct seq_file *m, void *v)
666 : : {
667 : : struct ftrace_event_file *file = v;
668 : 0 : struct ftrace_event_call *call = file->event_call;
669 : :
670 [ # # ]: 0 : if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
671 : 0 : seq_printf(m, "%s:", call->class->system);
672 : 0 : seq_printf(m, "%s\n", call->name);
673 : :
674 : 0 : return 0;
675 : : }
676 : :
677 : 0 : static void t_stop(struct seq_file *m, void *p)
678 : : {
679 : 0 : mutex_unlock(&event_mutex);
680 : 0 : }
681 : :
682 : : static ssize_t
683 : 0 : event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
684 : : loff_t *ppos)
685 : : {
686 : : struct ftrace_event_file *file;
687 : : unsigned long flags;
688 : 0 : char buf[4] = "0";
689 : :
690 : 0 : mutex_lock(&event_mutex);
691 : : file = event_file_data(filp);
692 [ # # ]: 0 : if (likely(file))
693 : 0 : flags = file->flags;
694 : 0 : mutex_unlock(&event_mutex);
695 : :
696 [ # # ]: 0 : if (!file)
697 : : return -ENODEV;
698 : :
699 [ # # ]: 0 : if (flags & FTRACE_EVENT_FL_ENABLED &&
700 : : !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
701 : 0 : strcpy(buf, "1");
702 : :
703 [ # # ]: 0 : if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
704 : : flags & FTRACE_EVENT_FL_SOFT_MODE)
705 : 0 : strcat(buf, "*");
706 : :
707 : 0 : strcat(buf, "\n");
708 : :
709 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
710 : : }
711 : :
712 : : static ssize_t
713 : 0 : event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
714 : : loff_t *ppos)
715 : : {
716 : : struct ftrace_event_file *file;
717 : : unsigned long val;
718 : : int ret;
719 : :
720 : 0 : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
721 [ # # ]: 0 : if (ret)
722 : : return ret;
723 : :
724 : 0 : ret = tracing_update_buffers();
725 [ # # ]: 0 : if (ret < 0)
726 : : return ret;
727 : :
728 [ # # ]: 0 : switch (val) {
729 : : case 0:
730 : : case 1:
731 : : ret = -ENODEV;
732 : 0 : mutex_lock(&event_mutex);
733 : : file = event_file_data(filp);
734 [ # # ]: 0 : if (likely(file))
735 : 0 : ret = ftrace_event_enable_disable(file, val);
736 : 0 : mutex_unlock(&event_mutex);
737 : : break;
738 : :
739 : : default:
740 : : return -EINVAL;
741 : : }
742 : :
743 : 0 : *ppos += cnt;
744 : :
745 [ # # ]: 0 : return ret ? ret : cnt;
746 : : }
747 : :
748 : : static ssize_t
749 : 0 : system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
750 : : loff_t *ppos)
751 : : {
752 : 0 : const char set_to_char[4] = { '?', '0', '1', 'X' };
753 : 0 : struct ftrace_subsystem_dir *dir = filp->private_data;
754 : 0 : struct event_subsystem *system = dir->subsystem;
755 : : struct ftrace_event_call *call;
756 : : struct ftrace_event_file *file;
757 : 0 : struct trace_array *tr = dir->tr;
758 : : char buf[2];
759 : : int set = 0;
760 : : int ret;
761 : :
762 : 0 : mutex_lock(&event_mutex);
763 [ # # ]: 0 : list_for_each_entry(file, &tr->events, list) {
764 : 0 : call = file->event_call;
765 [ # # ][ # # ]: 0 : if (!call->name || !call->class || !call->class->reg)
[ # # ]
766 : 0 : continue;
767 : :
768 [ # # ][ # # ]: 0 : if (system && strcmp(call->class->system, system->name) != 0)
769 : 0 : continue;
770 : :
771 : : /*
772 : : * We need to find out if all the events are set
773 : : * or if all events or cleared, or if we have
774 : : * a mixture.
775 : : */
776 [ # # ]: 0 : set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
777 : :
778 : : /*
779 : : * If we have a mixture, no need to look further.
780 : : */
781 [ # # ]: 0 : if (set == 3)
782 : : break;
783 : : }
784 : 0 : mutex_unlock(&event_mutex);
785 : :
786 : 0 : buf[0] = set_to_char[set];
787 : 0 : buf[1] = '\n';
788 : :
789 : 0 : ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
790 : :
791 : 0 : return ret;
792 : : }
793 : :
794 : : static ssize_t
795 : 0 : system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
796 : : loff_t *ppos)
797 : : {
798 : 0 : struct ftrace_subsystem_dir *dir = filp->private_data;
799 : 0 : struct event_subsystem *system = dir->subsystem;
800 : : const char *name = NULL;
801 : : unsigned long val;
802 : : ssize_t ret;
803 : :
804 : 0 : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
805 [ # # ]: 0 : if (ret)
806 : : return ret;
807 : :
808 : 0 : ret = tracing_update_buffers();
809 [ # # ]: 0 : if (ret < 0)
810 : : return ret;
811 : :
812 [ # # ]: 0 : if (val != 0 && val != 1)
813 : : return -EINVAL;
814 : :
815 : : /*
816 : : * Opening of "enable" adds a ref count to system,
817 : : * so the name is safe to use.
818 : : */
819 [ # # ]: 0 : if (system)
820 : 0 : name = system->name;
821 : :
822 : 0 : ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
823 [ # # ]: 0 : if (ret)
824 : : goto out;
825 : :
826 : 0 : ret = cnt;
827 : :
828 : : out:
829 : 0 : *ppos += cnt;
830 : :
831 : 0 : return ret;
832 : : }
833 : :
834 : : enum {
835 : : FORMAT_HEADER = 1,
836 : : FORMAT_FIELD_SEPERATOR = 2,
837 : : FORMAT_PRINTFMT = 3,
838 : : };
839 : :
840 : 0 : static void *f_next(struct seq_file *m, void *v, loff_t *pos)
841 : : {
842 : 0 : struct ftrace_event_call *call = event_file_data(m->private);
843 : : struct list_head *common_head = &ftrace_common_fields;
844 : : struct list_head *head = trace_get_fields(call);
845 : : struct list_head *node = v;
846 : :
847 : 0 : (*pos)++;
848 : :
849 [ # # # # ]: 0 : switch ((unsigned long)v) {
850 : : case FORMAT_HEADER:
851 : : node = common_head;
852 : 0 : break;
853 : :
854 : : case FORMAT_FIELD_SEPERATOR:
855 : : node = head;
856 : 0 : break;
857 : :
858 : : case FORMAT_PRINTFMT:
859 : : /* all done */
860 : : return NULL;
861 : : }
862 : :
863 : 0 : node = node->prev;
864 [ # # ]: 0 : if (node == common_head)
865 : : return (void *)FORMAT_FIELD_SEPERATOR;
866 [ # # ]: 0 : else if (node == head)
867 : : return (void *)FORMAT_PRINTFMT;
868 : : else
869 : 0 : return node;
870 : : }
871 : :
872 : 0 : static int f_show(struct seq_file *m, void *v)
873 : : {
874 : 0 : struct ftrace_event_call *call = event_file_data(m->private);
875 : : struct ftrace_event_field *field;
876 : : const char *array_descriptor;
877 : :
878 [ # # # # ]: 0 : switch ((unsigned long)v) {
879 : : case FORMAT_HEADER:
880 : 0 : seq_printf(m, "name: %s\n", call->name);
881 : 0 : seq_printf(m, "ID: %d\n", call->event.type);
882 : 0 : seq_printf(m, "format:\n");
883 : 0 : return 0;
884 : :
885 : : case FORMAT_FIELD_SEPERATOR:
886 : 0 : seq_putc(m, '\n');
887 : 0 : return 0;
888 : :
889 : : case FORMAT_PRINTFMT:
890 : 0 : seq_printf(m, "\nprint fmt: %s\n",
891 : : call->print_fmt);
892 : 0 : return 0;
893 : : }
894 : :
895 : : field = list_entry(v, struct ftrace_event_field, link);
896 : : /*
897 : : * Smartly shows the array type(except dynamic array).
898 : : * Normal:
899 : : * field:TYPE VAR
900 : : * If TYPE := TYPE[LEN], it is shown:
901 : : * field:TYPE VAR[LEN]
902 : : */
903 : 0 : array_descriptor = strchr(field->type, '[');
904 : :
905 [ # # ]: 0 : if (!strncmp(field->type, "__data_loc", 10))
906 : : array_descriptor = NULL;
907 : :
908 [ # # ]: 0 : if (!array_descriptor)
909 : 0 : seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
910 : : field->type, field->name, field->offset,
911 : 0 : field->size, !!field->is_signed);
912 : : else
913 : 0 : seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
914 : : (int)(array_descriptor - field->type),
915 : : field->type, field->name,
916 : : array_descriptor, field->offset,
917 : 0 : field->size, !!field->is_signed);
918 : :
919 : : return 0;
920 : : }
921 : :
922 : 0 : static void *f_start(struct seq_file *m, loff_t *pos)
923 : : {
924 : : void *p = (void *)FORMAT_HEADER;
925 : 0 : loff_t l = 0;
926 : :
927 : : /* ->stop() is called even if ->start() fails */
928 : 0 : mutex_lock(&event_mutex);
929 [ # # ]: 0 : if (!event_file_data(m->private))
930 : : return ERR_PTR(-ENODEV);
931 : :
932 [ # # ][ # # ]: 0 : while (l < *pos && p)
933 : 0 : p = f_next(m, p, &l);
934 : :
935 : : return p;
936 : : }
937 : :
938 : 0 : static void f_stop(struct seq_file *m, void *p)
939 : : {
940 : 0 : mutex_unlock(&event_mutex);
941 : 0 : }
942 : :
943 : : static const struct seq_operations trace_format_seq_ops = {
944 : : .start = f_start,
945 : : .next = f_next,
946 : : .stop = f_stop,
947 : : .show = f_show,
948 : : };
949 : :
950 : 0 : static int trace_format_open(struct inode *inode, struct file *file)
951 : : {
952 : : struct seq_file *m;
953 : : int ret;
954 : :
955 : 0 : ret = seq_open(file, &trace_format_seq_ops);
956 [ # # ]: 0 : if (ret < 0)
957 : : return ret;
958 : :
959 : 0 : m = file->private_data;
960 : 0 : m->private = file;
961 : :
962 : 0 : return 0;
963 : : }
964 : :
965 : : static ssize_t
966 : 0 : event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
967 : : {
968 : 0 : int id = (long)event_file_data(filp);
969 : : char buf[32];
970 : : int len;
971 : :
972 [ # # ]: 0 : if (*ppos)
973 : : return 0;
974 : :
975 [ # # ]: 0 : if (unlikely(!id))
976 : : return -ENODEV;
977 : :
978 : 0 : len = sprintf(buf, "%d\n", id);
979 : :
980 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
981 : : }
982 : :
983 : : static ssize_t
984 : 0 : event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
985 : : loff_t *ppos)
986 : : {
987 : : struct ftrace_event_file *file;
988 : : struct trace_seq *s;
989 : : int r = -ENODEV;
990 : :
991 [ # # ]: 0 : if (*ppos)
992 : : return 0;
993 : :
994 : : s = kmalloc(sizeof(*s), GFP_KERNEL);
995 : :
996 [ # # ]: 0 : if (!s)
997 : : return -ENOMEM;
998 : :
999 : : trace_seq_init(s);
1000 : :
1001 : 0 : mutex_lock(&event_mutex);
1002 : : file = event_file_data(filp);
1003 [ # # ]: 0 : if (file)
1004 : 0 : print_event_filter(file, s);
1005 : 0 : mutex_unlock(&event_mutex);
1006 : :
1007 [ # # ]: 0 : if (file)
1008 : 0 : r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1009 : :
1010 : 0 : kfree(s);
1011 : :
1012 : 0 : return r;
1013 : : }
1014 : :
1015 : : static ssize_t
1016 : 0 : event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1017 : : loff_t *ppos)
1018 : : {
1019 : : struct ftrace_event_file *file;
1020 : : char *buf;
1021 : : int err = -ENODEV;
1022 : :
1023 [ # # ]: 0 : if (cnt >= PAGE_SIZE)
1024 : : return -EINVAL;
1025 : :
1026 : 0 : buf = (char *)__get_free_page(GFP_TEMPORARY);
1027 [ # # ]: 0 : if (!buf)
1028 : : return -ENOMEM;
1029 : :
1030 [ # # ]: 0 : if (copy_from_user(buf, ubuf, cnt)) {
1031 : 0 : free_page((unsigned long) buf);
1032 : 0 : return -EFAULT;
1033 : : }
1034 : 0 : buf[cnt] = '\0';
1035 : :
1036 : 0 : mutex_lock(&event_mutex);
1037 : : file = event_file_data(filp);
1038 [ # # ]: 0 : if (file)
1039 : 0 : err = apply_event_filter(file, buf);
1040 : 0 : mutex_unlock(&event_mutex);
1041 : :
1042 : 0 : free_page((unsigned long) buf);
1043 [ # # ]: 0 : if (err < 0)
1044 : : return err;
1045 : :
1046 : 0 : *ppos += cnt;
1047 : :
1048 : 0 : return cnt;
1049 : : }
1050 : :
1051 : : static LIST_HEAD(event_subsystems);
1052 : :
1053 : 0 : static int subsystem_open(struct inode *inode, struct file *filp)
1054 : : {
1055 : : struct event_subsystem *system = NULL;
1056 : : struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1057 : : struct trace_array *tr;
1058 : : int ret;
1059 : :
1060 [ # # ]: 0 : if (tracing_is_disabled())
1061 : : return -ENODEV;
1062 : :
1063 : : /* Make sure the system still exists */
1064 : 0 : mutex_lock(&trace_types_lock);
1065 : 0 : mutex_lock(&event_mutex);
1066 [ # # ]: 0 : list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1067 [ # # ]: 0 : list_for_each_entry(dir, &tr->systems, list) {
1068 [ # # ]: 0 : if (dir == inode->i_private) {
1069 : : /* Don't open systems with no events */
1070 [ # # ]: 0 : if (dir->nr_events) {
1071 : 0 : __get_system_dir(dir);
1072 : 0 : system = dir->subsystem;
1073 : : }
1074 : : goto exit_loop;
1075 : : }
1076 : : }
1077 : : }
1078 : : exit_loop:
1079 : 0 : mutex_unlock(&event_mutex);
1080 : 0 : mutex_unlock(&trace_types_lock);
1081 : :
1082 [ # # ]: 0 : if (!system)
1083 : : return -ENODEV;
1084 : :
1085 : : /* Some versions of gcc think dir can be uninitialized here */
1086 [ # # ]: 0 : WARN_ON(!dir);
1087 : :
1088 : : /* Still need to increment the ref count of the system */
1089 [ # # ]: 0 : if (trace_array_get(tr) < 0) {
1090 : 0 : put_system(dir);
1091 : 0 : return -ENODEV;
1092 : : }
1093 : :
1094 : 0 : ret = tracing_open_generic(inode, filp);
1095 [ # # ]: 0 : if (ret < 0) {
1096 : 0 : trace_array_put(tr);
1097 : 0 : put_system(dir);
1098 : : }
1099 : :
1100 : 0 : return ret;
1101 : : }
1102 : :
1103 : 0 : static int system_tr_open(struct inode *inode, struct file *filp)
1104 : : {
1105 : : struct ftrace_subsystem_dir *dir;
1106 : 0 : struct trace_array *tr = inode->i_private;
1107 : : int ret;
1108 : :
1109 [ # # ]: 0 : if (tracing_is_disabled())
1110 : : return -ENODEV;
1111 : :
1112 [ # # ]: 0 : if (trace_array_get(tr) < 0)
1113 : : return -ENODEV;
1114 : :
1115 : : /* Make a temporary dir that has no system but points to tr */
1116 : : dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1117 [ # # ]: 0 : if (!dir) {
1118 : 0 : trace_array_put(tr);
1119 : 0 : return -ENOMEM;
1120 : : }
1121 : :
1122 : 0 : dir->tr = tr;
1123 : :
1124 : 0 : ret = tracing_open_generic(inode, filp);
1125 [ # # ]: 0 : if (ret < 0) {
1126 : 0 : trace_array_put(tr);
1127 : 0 : kfree(dir);
1128 : 0 : return ret;
1129 : : }
1130 : :
1131 : 0 : filp->private_data = dir;
1132 : :
1133 : 0 : return 0;
1134 : : }
1135 : :
1136 : 0 : static int subsystem_release(struct inode *inode, struct file *file)
1137 : : {
1138 : 0 : struct ftrace_subsystem_dir *dir = file->private_data;
1139 : :
1140 : 0 : trace_array_put(dir->tr);
1141 : :
1142 : : /*
1143 : : * If dir->subsystem is NULL, then this is a temporary
1144 : : * descriptor that was made for a trace_array to enable
1145 : : * all subsystems.
1146 : : */
1147 [ # # ]: 0 : if (dir->subsystem)
1148 : 0 : put_system(dir);
1149 : : else
1150 : 0 : kfree(dir);
1151 : :
1152 : 0 : return 0;
1153 : : }
1154 : :
1155 : : static ssize_t
1156 : 0 : subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1157 : : loff_t *ppos)
1158 : : {
1159 : 0 : struct ftrace_subsystem_dir *dir = filp->private_data;
1160 : 0 : struct event_subsystem *system = dir->subsystem;
1161 : : struct trace_seq *s;
1162 : : int r;
1163 : :
1164 [ # # ]: 0 : if (*ppos)
1165 : : return 0;
1166 : :
1167 : : s = kmalloc(sizeof(*s), GFP_KERNEL);
1168 [ # # ]: 0 : if (!s)
1169 : : return -ENOMEM;
1170 : :
1171 : : trace_seq_init(s);
1172 : :
1173 : 0 : print_subsystem_event_filter(system, s);
1174 : 0 : r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1175 : :
1176 : 0 : kfree(s);
1177 : :
1178 : 0 : return r;
1179 : : }
1180 : :
1181 : : static ssize_t
1182 : 0 : subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1183 : : loff_t *ppos)
1184 : : {
1185 : 0 : struct ftrace_subsystem_dir *dir = filp->private_data;
1186 : : char *buf;
1187 : : int err;
1188 : :
1189 [ # # ]: 0 : if (cnt >= PAGE_SIZE)
1190 : : return -EINVAL;
1191 : :
1192 : 0 : buf = (char *)__get_free_page(GFP_TEMPORARY);
1193 [ # # ]: 0 : if (!buf)
1194 : : return -ENOMEM;
1195 : :
1196 [ # # ]: 0 : if (copy_from_user(buf, ubuf, cnt)) {
1197 : 0 : free_page((unsigned long) buf);
1198 : 0 : return -EFAULT;
1199 : : }
1200 : 0 : buf[cnt] = '\0';
1201 : :
1202 : 0 : err = apply_subsystem_event_filter(dir, buf);
1203 : 0 : free_page((unsigned long) buf);
1204 [ # # ]: 0 : if (err < 0)
1205 : : return err;
1206 : :
1207 : 0 : *ppos += cnt;
1208 : :
1209 : 0 : return cnt;
1210 : : }
1211 : :
1212 : : static ssize_t
1213 : 0 : show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1214 : : {
1215 : 0 : int (*func)(struct trace_seq *s) = filp->private_data;
1216 : : struct trace_seq *s;
1217 : : int r;
1218 : :
1219 [ # # ]: 0 : if (*ppos)
1220 : : return 0;
1221 : :
1222 : : s = kmalloc(sizeof(*s), GFP_KERNEL);
1223 [ # # ]: 0 : if (!s)
1224 : : return -ENOMEM;
1225 : :
1226 : : trace_seq_init(s);
1227 : :
1228 : 0 : func(s);
1229 : 0 : r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1230 : :
1231 : 0 : kfree(s);
1232 : :
1233 : 0 : return r;
1234 : : }
1235 : :
1236 : : static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1237 : : static int ftrace_event_set_open(struct inode *inode, struct file *file);
1238 : : static int ftrace_event_release(struct inode *inode, struct file *file);
1239 : :
1240 : : static const struct seq_operations show_event_seq_ops = {
1241 : : .start = t_start,
1242 : : .next = t_next,
1243 : : .show = t_show,
1244 : : .stop = t_stop,
1245 : : };
1246 : :
1247 : : static const struct seq_operations show_set_event_seq_ops = {
1248 : : .start = s_start,
1249 : : .next = s_next,
1250 : : .show = t_show,
1251 : : .stop = t_stop,
1252 : : };
1253 : :
1254 : : static const struct file_operations ftrace_avail_fops = {
1255 : : .open = ftrace_event_avail_open,
1256 : : .read = seq_read,
1257 : : .llseek = seq_lseek,
1258 : : .release = seq_release,
1259 : : };
1260 : :
1261 : : static const struct file_operations ftrace_set_event_fops = {
1262 : : .open = ftrace_event_set_open,
1263 : : .read = seq_read,
1264 : : .write = ftrace_event_write,
1265 : : .llseek = seq_lseek,
1266 : : .release = ftrace_event_release,
1267 : : };
1268 : :
1269 : : static const struct file_operations ftrace_enable_fops = {
1270 : : .open = tracing_open_generic,
1271 : : .read = event_enable_read,
1272 : : .write = event_enable_write,
1273 : : .llseek = default_llseek,
1274 : : };
1275 : :
1276 : : static const struct file_operations ftrace_event_format_fops = {
1277 : : .open = trace_format_open,
1278 : : .read = seq_read,
1279 : : .llseek = seq_lseek,
1280 : : .release = seq_release,
1281 : : };
1282 : :
1283 : : static const struct file_operations ftrace_event_id_fops = {
1284 : : .read = event_id_read,
1285 : : .llseek = default_llseek,
1286 : : };
1287 : :
1288 : : static const struct file_operations ftrace_event_filter_fops = {
1289 : : .open = tracing_open_generic,
1290 : : .read = event_filter_read,
1291 : : .write = event_filter_write,
1292 : : .llseek = default_llseek,
1293 : : };
1294 : :
1295 : : static const struct file_operations ftrace_subsystem_filter_fops = {
1296 : : .open = subsystem_open,
1297 : : .read = subsystem_filter_read,
1298 : : .write = subsystem_filter_write,
1299 : : .llseek = default_llseek,
1300 : : .release = subsystem_release,
1301 : : };
1302 : :
1303 : : static const struct file_operations ftrace_system_enable_fops = {
1304 : : .open = subsystem_open,
1305 : : .read = system_enable_read,
1306 : : .write = system_enable_write,
1307 : : .llseek = default_llseek,
1308 : : .release = subsystem_release,
1309 : : };
1310 : :
1311 : : static const struct file_operations ftrace_tr_enable_fops = {
1312 : : .open = system_tr_open,
1313 : : .read = system_enable_read,
1314 : : .write = system_enable_write,
1315 : : .llseek = default_llseek,
1316 : : .release = subsystem_release,
1317 : : };
1318 : :
1319 : : static const struct file_operations ftrace_show_header_fops = {
1320 : : .open = tracing_open_generic,
1321 : : .read = show_header,
1322 : : .llseek = default_llseek,
1323 : : };
1324 : :
1325 : : static int
1326 : : ftrace_event_open(struct inode *inode, struct file *file,
1327 : : const struct seq_operations *seq_ops)
1328 : : {
1329 : : struct seq_file *m;
1330 : : int ret;
1331 : :
1332 : 0 : ret = seq_open(file, seq_ops);
1333 [ # # # # ]: 0 : if (ret < 0)
1334 : : return ret;
1335 : 0 : m = file->private_data;
1336 : : /* copy tr over to seq ops */
1337 : 0 : m->private = inode->i_private;
1338 : :
1339 : : return ret;
1340 : : }
1341 : :
1342 : 0 : static int ftrace_event_release(struct inode *inode, struct file *file)
1343 : : {
1344 : 0 : struct trace_array *tr = inode->i_private;
1345 : :
1346 : 0 : trace_array_put(tr);
1347 : :
1348 : 0 : return seq_release(inode, file);
1349 : : }
1350 : :
1351 : : static int
1352 : 0 : ftrace_event_avail_open(struct inode *inode, struct file *file)
1353 : : {
1354 : : const struct seq_operations *seq_ops = &show_event_seq_ops;
1355 : :
1356 : 0 : return ftrace_event_open(inode, file, seq_ops);
1357 : : }
1358 : :
1359 : : static int
1360 : 0 : ftrace_event_set_open(struct inode *inode, struct file *file)
1361 : : {
1362 : : const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1363 : 0 : struct trace_array *tr = inode->i_private;
1364 : : int ret;
1365 : :
1366 [ # # ]: 0 : if (trace_array_get(tr) < 0)
1367 : : return -ENODEV;
1368 : :
1369 [ # # ][ # # ]: 0 : if ((file->f_mode & FMODE_WRITE) &&
1370 : 0 : (file->f_flags & O_TRUNC))
1371 : 0 : ftrace_clear_events(tr);
1372 : :
1373 : : ret = ftrace_event_open(inode, file, seq_ops);
1374 [ # # ]: 0 : if (ret < 0)
1375 : 0 : trace_array_put(tr);
1376 : 0 : return ret;
1377 : : }
1378 : :
1379 : : static struct event_subsystem *
1380 : 0 : create_new_subsystem(const char *name)
1381 : : {
1382 : : struct event_subsystem *system;
1383 : :
1384 : : /* need to create new entry */
1385 : : system = kmalloc(sizeof(*system), GFP_KERNEL);
1386 [ # # ]: 0 : if (!system)
1387 : : return NULL;
1388 : :
1389 : 0 : system->ref_count = 1;
1390 : :
1391 : : /* Only allocate if dynamic (kprobes and modules) */
1392 [ # # ]: 0 : if (!core_kernel_data((unsigned long)name)) {
1393 : 0 : system->ref_count |= SYSTEM_FL_FREE_NAME;
1394 : 0 : system->name = kstrdup(name, GFP_KERNEL);
1395 [ # # ]: 0 : if (!system->name)
1396 : : goto out_free;
1397 : : } else
1398 : 0 : system->name = name;
1399 : :
1400 : 0 : system->filter = NULL;
1401 : :
1402 : 0 : system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1403 [ # # ]: 0 : if (!system->filter)
1404 : : goto out_free;
1405 : :
1406 : 0 : list_add(&system->list, &event_subsystems);
1407 : :
1408 : 0 : return system;
1409 : :
1410 : : out_free:
1411 [ # # ]: 0 : if (system->ref_count & SYSTEM_FL_FREE_NAME)
1412 : 0 : kfree(system->name);
1413 : 0 : kfree(system);
1414 : 0 : return NULL;
1415 : : }
1416 : :
1417 : : static struct dentry *
1418 : 0 : event_subsystem_dir(struct trace_array *tr, const char *name,
1419 : : struct ftrace_event_file *file, struct dentry *parent)
1420 : : {
1421 : : struct ftrace_subsystem_dir *dir;
1422 : : struct event_subsystem *system;
1423 : : struct dentry *entry;
1424 : :
1425 : : /* First see if we did not already create this dir */
1426 [ # # ]: 0 : list_for_each_entry(dir, &tr->systems, list) {
1427 : 0 : system = dir->subsystem;
1428 [ # # ]: 0 : if (strcmp(system->name, name) == 0) {
1429 : 0 : dir->nr_events++;
1430 : 0 : file->system = dir;
1431 : 0 : return dir->entry;
1432 : : }
1433 : : }
1434 : :
1435 : : /* Now see if the system itself exists. */
1436 [ # # ]: 0 : list_for_each_entry(system, &event_subsystems, list) {
1437 [ # # ]: 0 : if (strcmp(system->name, name) == 0)
1438 : : break;
1439 : : }
1440 : : /* Reset system variable when not found */
1441 [ # # ]: 0 : if (&system->list == &event_subsystems)
1442 : : system = NULL;
1443 : :
1444 : : dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1445 [ # # ]: 0 : if (!dir)
1446 : : goto out_fail;
1447 : :
1448 [ # # ]: 0 : if (!system) {
1449 : 0 : system = create_new_subsystem(name);
1450 [ # # ]: 0 : if (!system)
1451 : : goto out_free;
1452 : : } else
1453 : 0 : __get_system(system);
1454 : :
1455 : 0 : dir->entry = debugfs_create_dir(name, parent);
1456 [ # # ]: 0 : if (!dir->entry) {
1457 : 0 : pr_warning("Failed to create system directory %s\n", name);
1458 : 0 : __put_system(system);
1459 : : goto out_free;
1460 : : }
1461 : :
1462 : 0 : dir->tr = tr;
1463 : 0 : dir->ref_count = 1;
1464 : 0 : dir->nr_events = 1;
1465 : 0 : dir->subsystem = system;
1466 : 0 : file->system = dir;
1467 : :
1468 : 0 : entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1469 : : &ftrace_subsystem_filter_fops);
1470 [ # # ]: 0 : if (!entry) {
1471 : 0 : kfree(system->filter);
1472 : 0 : system->filter = NULL;
1473 : 0 : pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1474 : : }
1475 : :
1476 : 0 : trace_create_file("enable", 0644, dir->entry, dir,
1477 : : &ftrace_system_enable_fops);
1478 : :
1479 : 0 : list_add(&dir->list, &tr->systems);
1480 : :
1481 : 0 : return dir->entry;
1482 : :
1483 : : out_free:
1484 : 0 : kfree(dir);
1485 : : out_fail:
1486 : : /* Only print this message if failed on memory allocation */
1487 [ # # ]: 0 : if (!dir || !system)
1488 : 0 : pr_warning("No memory to create event subsystem %s\n",
1489 : : name);
1490 : : return NULL;
1491 : : }
1492 : :
1493 : : static int
1494 : 0 : event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
1495 : : {
1496 : 0 : struct ftrace_event_call *call = file->event_call;
1497 : 0 : struct trace_array *tr = file->tr;
1498 : : struct list_head *head;
1499 : : struct dentry *d_events;
1500 : : int ret;
1501 : :
1502 : : /*
1503 : : * If the trace point header did not define TRACE_SYSTEM
1504 : : * then the system would be called "TRACE_SYSTEM".
1505 : : */
1506 [ # # ]: 0 : if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1507 : 0 : d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1508 [ # # ]: 0 : if (!d_events)
1509 : : return -ENOMEM;
1510 : : } else
1511 : : d_events = parent;
1512 : :
1513 : 0 : file->dir = debugfs_create_dir(call->name, d_events);
1514 [ # # ]: 0 : if (!file->dir) {
1515 : 0 : pr_warning("Could not create debugfs '%s' directory\n",
1516 : : call->name);
1517 : 0 : return -1;
1518 : : }
1519 : :
1520 [ # # ][ # # ]: 0 : if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1521 : 0 : trace_create_file("enable", 0644, file->dir, file,
1522 : : &ftrace_enable_fops);
1523 : :
1524 : : #ifdef CONFIG_PERF_EVENTS
1525 [ # # ][ # # ]: 0 : if (call->event.type && call->class->reg)
1526 : 0 : trace_create_file("id", 0444, file->dir,
1527 : : (void *)(long)call->event.type,
1528 : : &ftrace_event_id_fops);
1529 : : #endif
1530 : :
1531 : : /*
1532 : : * Other events may have the same class. Only update
1533 : : * the fields if they are not already defined.
1534 : : */
1535 : : head = trace_get_fields(call);
1536 [ # # ]: 0 : if (list_empty(head)) {
1537 : 0 : ret = call->class->define_fields(call);
1538 [ # # ]: 0 : if (ret < 0) {
1539 : 0 : pr_warning("Could not initialize trace point"
1540 : : " events/%s\n", call->name);
1541 : 0 : return -1;
1542 : : }
1543 : : }
1544 : 0 : trace_create_file("filter", 0644, file->dir, file,
1545 : : &ftrace_event_filter_fops);
1546 : :
1547 : 0 : trace_create_file("trigger", 0644, file->dir, file,
1548 : : &event_trigger_fops);
1549 : :
1550 : 0 : trace_create_file("format", 0444, file->dir, call,
1551 : : &ftrace_event_format_fops);
1552 : :
1553 : 0 : return 0;
1554 : : }
1555 : :
1556 : 0 : static void remove_event_from_tracers(struct ftrace_event_call *call)
1557 : : {
1558 : : struct ftrace_event_file *file;
1559 : : struct trace_array *tr;
1560 : :
1561 [ # # ][ # # ]: 0 : do_for_each_event_file_safe(tr, file) {
1562 [ # # ]: 0 : if (file->event_call != call)
1563 : 0 : continue;
1564 : :
1565 : 0 : remove_event_file_dir(file);
1566 : : /*
1567 : : * The do_for_each_event_file_safe() is
1568 : : * a double loop. After finding the call for this
1569 : : * trace_array, we use break to jump to the next
1570 : : * trace_array.
1571 : : */
1572 : 0 : break;
1573 : : } while_for_each_event_file();
1574 : 0 : }
1575 : :
1576 : 0 : static void event_remove(struct ftrace_event_call *call)
1577 : : {
1578 : : struct trace_array *tr;
1579 : : struct ftrace_event_file *file;
1580 : :
1581 [ # # ][ # # ]: 0 : do_for_each_event_file(tr, file) {
1582 [ # # ]: 0 : if (file->event_call != call)
1583 : 0 : continue;
1584 : : ftrace_event_enable_disable(file, 0);
1585 : 0 : destroy_preds(file);
1586 : : /*
1587 : : * The do_for_each_event_file() is
1588 : : * a double loop. After finding the call for this
1589 : : * trace_array, we use break to jump to the next
1590 : : * trace_array.
1591 : : */
1592 : 0 : break;
1593 : : } while_for_each_event_file();
1594 : :
1595 [ # # ]: 0 : if (call->event.funcs)
1596 : 0 : __unregister_ftrace_event(&call->event);
1597 : 0 : remove_event_from_tracers(call);
1598 : : list_del(&call->list);
1599 : 0 : }
1600 : :
1601 : 0 : static int event_init(struct ftrace_event_call *call)
1602 : : {
1603 : : int ret = 0;
1604 : :
1605 [ # # ][ # # ]: 0 : if (WARN_ON(!call->name))
1606 : : return -EINVAL;
1607 : :
1608 [ # # ]: 0 : if (call->class->raw_init) {
1609 : 0 : ret = call->class->raw_init(call);
1610 [ # # ]: 0 : if (ret < 0 && ret != -ENOSYS)
1611 : 0 : pr_warn("Could not initialize trace events/%s\n",
1612 : : call->name);
1613 : : }
1614 : :
1615 : 0 : return ret;
1616 : : }
1617 : :
1618 : : static int
1619 : 0 : __register_event(struct ftrace_event_call *call, struct module *mod)
1620 : : {
1621 : : int ret;
1622 : :
1623 : 0 : ret = event_init(call);
1624 [ # # ]: 0 : if (ret < 0)
1625 : : return ret;
1626 : :
1627 : 0 : list_add(&call->list, &ftrace_events);
1628 : 0 : call->mod = mod;
1629 : :
1630 : 0 : return 0;
1631 : : }
1632 : :
1633 : : static struct ftrace_event_file *
1634 : 0 : trace_create_new_event(struct ftrace_event_call *call,
1635 : : struct trace_array *tr)
1636 : : {
1637 : : struct ftrace_event_file *file;
1638 : :
1639 : 0 : file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1640 [ # # ]: 0 : if (!file)
1641 : : return NULL;
1642 : :
1643 : 0 : file->event_call = call;
1644 : 0 : file->tr = tr;
1645 : 0 : atomic_set(&file->sm_ref, 0);
1646 : 0 : atomic_set(&file->tm_ref, 0);
1647 : 0 : INIT_LIST_HEAD(&file->triggers);
1648 : 0 : list_add(&file->list, &tr->events);
1649 : :
1650 : 0 : return file;
1651 : : }
1652 : :
1653 : : /* Add an event to a trace directory */
1654 : : static int
1655 : 0 : __trace_add_new_event(struct ftrace_event_call *call, struct trace_array *tr)
1656 : : {
1657 : : struct ftrace_event_file *file;
1658 : :
1659 : 0 : file = trace_create_new_event(call, tr);
1660 [ # # ]: 0 : if (!file)
1661 : : return -ENOMEM;
1662 : :
1663 : 0 : return event_create_dir(tr->event_dir, file);
1664 : : }
1665 : :
1666 : : /*
1667 : : * Just create a decriptor for early init. A descriptor is required
1668 : : * for enabling events at boot. We want to enable events before
1669 : : * the filesystem is initialized.
1670 : : */
1671 : : static __init int
1672 : 0 : __trace_early_add_new_event(struct ftrace_event_call *call,
1673 : : struct trace_array *tr)
1674 : : {
1675 : : struct ftrace_event_file *file;
1676 : :
1677 : 0 : file = trace_create_new_event(call, tr);
1678 [ # # ]: 0 : if (!file)
1679 : : return -ENOMEM;
1680 : :
1681 : 0 : return 0;
1682 : : }
1683 : :
1684 : : struct ftrace_module_file_ops;
1685 : : static void __add_event_to_tracers(struct ftrace_event_call *call);
1686 : :
1687 : : /* Add an additional event_call dynamically */
1688 : 0 : int trace_add_event_call(struct ftrace_event_call *call)
1689 : : {
1690 : : int ret;
1691 : 0 : mutex_lock(&trace_types_lock);
1692 : 0 : mutex_lock(&event_mutex);
1693 : :
1694 : 0 : ret = __register_event(call, NULL);
1695 [ # # ]: 0 : if (ret >= 0)
1696 : : __add_event_to_tracers(call);
1697 : :
1698 : 0 : mutex_unlock(&event_mutex);
1699 : 0 : mutex_unlock(&trace_types_lock);
1700 : 0 : return ret;
1701 : : }
1702 : :
1703 : : /*
1704 : : * Must be called under locking of trace_types_lock, event_mutex and
1705 : : * trace_event_sem.
1706 : : */
1707 : 0 : static void __trace_remove_event_call(struct ftrace_event_call *call)
1708 : : {
1709 : 0 : event_remove(call);
1710 : 0 : trace_destroy_fields(call);
1711 : 0 : destroy_call_preds(call);
1712 : 0 : }
1713 : :
1714 : 0 : static int probe_remove_event_call(struct ftrace_event_call *call)
1715 : : {
1716 : : struct trace_array *tr;
1717 : : struct ftrace_event_file *file;
1718 : :
1719 : : #ifdef CONFIG_PERF_EVENTS
1720 [ # # ]: 0 : if (call->perf_refcount)
1721 : : return -EBUSY;
1722 : : #endif
1723 [ # # ][ # # ]: 0 : do_for_each_event_file(tr, file) {
1724 [ # # ]: 0 : if (file->event_call != call)
1725 : 0 : continue;
1726 : : /*
1727 : : * We can't rely on ftrace_event_enable_disable(enable => 0)
1728 : : * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1729 : : * TRACE_REG_UNREGISTER.
1730 : : */
1731 [ # # ]: 0 : if (file->flags & FTRACE_EVENT_FL_ENABLED)
1732 : : return -EBUSY;
1733 : : /*
1734 : : * The do_for_each_event_file_safe() is
1735 : : * a double loop. After finding the call for this
1736 : : * trace_array, we use break to jump to the next
1737 : : * trace_array.
1738 : : */
1739 : : break;
1740 : : } while_for_each_event_file();
1741 : :
1742 : 0 : __trace_remove_event_call(call);
1743 : :
1744 : 0 : return 0;
1745 : : }
1746 : :
1747 : : /* Remove an event_call */
1748 : 0 : int trace_remove_event_call(struct ftrace_event_call *call)
1749 : : {
1750 : : int ret;
1751 : :
1752 : 0 : mutex_lock(&trace_types_lock);
1753 : 0 : mutex_lock(&event_mutex);
1754 : 0 : down_write(&trace_event_sem);
1755 : 0 : ret = probe_remove_event_call(call);
1756 : 0 : up_write(&trace_event_sem);
1757 : 0 : mutex_unlock(&event_mutex);
1758 : 0 : mutex_unlock(&trace_types_lock);
1759 : :
1760 : 0 : return ret;
1761 : : }
1762 : :
1763 : : #define for_each_event(event, start, end) \
1764 : : for (event = start; \
1765 : : (unsigned long)event < (unsigned long)end; \
1766 : : event++)
1767 : :
1768 : : #ifdef CONFIG_MODULES
1769 : :
1770 : 0 : static void trace_module_add_events(struct module *mod)
1771 : : {
1772 : : struct ftrace_event_call **call, **start, **end;
1773 : :
1774 [ # # ]: 0 : if (!mod->num_trace_events)
1775 : : return;
1776 : :
1777 : : /* Don't add infrastructure for mods without tracepoints */
1778 [ # # ]: 0 : if (trace_module_has_bad_taint(mod)) {
1779 : 0 : pr_err("%s: module has bad taint, not creating trace events\n",
1780 : : mod->name);
1781 : 0 : return;
1782 : : }
1783 : :
1784 : 0 : start = mod->trace_events;
1785 : 0 : end = mod->trace_events + mod->num_trace_events;
1786 : :
1787 [ # # ]: 0 : for_each_event(call, start, end) {
1788 : 0 : __register_event(*call, mod);
1789 : 0 : __add_event_to_tracers(*call);
1790 : : }
1791 : : }
1792 : :
1793 : 0 : static void trace_module_remove_events(struct module *mod)
1794 : : {
1795 : : struct ftrace_event_call *call, *p;
1796 : : bool clear_trace = false;
1797 : :
1798 : 0 : down_write(&trace_event_sem);
1799 [ # # ]: 0 : list_for_each_entry_safe(call, p, &ftrace_events, list) {
1800 [ # # ]: 0 : if (call->mod == mod) {
1801 [ # # ]: 0 : if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1802 : : clear_trace = true;
1803 : 0 : __trace_remove_event_call(call);
1804 : : }
1805 : : }
1806 : 0 : up_write(&trace_event_sem);
1807 : :
1808 : : /*
1809 : : * It is safest to reset the ring buffer if the module being unloaded
1810 : : * registered any events that were used. The only worry is if
1811 : : * a new module gets loaded, and takes on the same id as the events
1812 : : * of this module. When printing out the buffer, traced events left
1813 : : * over from this module may be passed to the new module events and
1814 : : * unexpected results may occur.
1815 : : */
1816 [ # # ]: 0 : if (clear_trace)
1817 : 0 : tracing_reset_all_online_cpus();
1818 : 0 : }
1819 : :
1820 : 0 : static int trace_module_notify(struct notifier_block *self,
1821 : : unsigned long val, void *data)
1822 : : {
1823 : : struct module *mod = data;
1824 : :
1825 : 0 : mutex_lock(&trace_types_lock);
1826 : 0 : mutex_lock(&event_mutex);
1827 [ # # # ]: 0 : switch (val) {
1828 : : case MODULE_STATE_COMING:
1829 : 0 : trace_module_add_events(mod);
1830 : 0 : break;
1831 : : case MODULE_STATE_GOING:
1832 : 0 : trace_module_remove_events(mod);
1833 : 0 : break;
1834 : : }
1835 : 0 : mutex_unlock(&event_mutex);
1836 : 0 : mutex_unlock(&trace_types_lock);
1837 : :
1838 : 0 : return 0;
1839 : : }
1840 : :
1841 : : static struct notifier_block trace_module_nb = {
1842 : : .notifier_call = trace_module_notify,
1843 : : .priority = 0,
1844 : : };
1845 : : #endif /* CONFIG_MODULES */
1846 : :
1847 : : /* Create a new event directory structure for a trace directory. */
1848 : : static void
1849 : 0 : __trace_add_event_dirs(struct trace_array *tr)
1850 : : {
1851 : : struct ftrace_event_call *call;
1852 : : int ret;
1853 : :
1854 [ # # ]: 0 : list_for_each_entry(call, &ftrace_events, list) {
1855 : 0 : ret = __trace_add_new_event(call, tr);
1856 [ # # ]: 0 : if (ret < 0)
1857 : 0 : pr_warning("Could not create directory for event %s\n",
1858 : : call->name);
1859 : : }
1860 : 0 : }
1861 : :
1862 : : struct ftrace_event_file *
1863 : 0 : find_event_file(struct trace_array *tr, const char *system, const char *event)
1864 : : {
1865 : : struct ftrace_event_file *file;
1866 : : struct ftrace_event_call *call;
1867 : :
1868 [ # # ]: 0 : list_for_each_entry(file, &tr->events, list) {
1869 : :
1870 : 0 : call = file->event_call;
1871 : :
1872 [ # # ][ # # ]: 0 : if (!call->name || !call->class || !call->class->reg)
[ # # ]
1873 : 0 : continue;
1874 : :
1875 [ # # ]: 0 : if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1876 : 0 : continue;
1877 : :
1878 [ # # ][ # # ]: 0 : if (strcmp(event, call->name) == 0 &&
1879 : 0 : strcmp(system, call->class->system) == 0)
1880 : : return file;
1881 : : }
1882 : : return NULL;
1883 : : }
1884 : :
1885 : : #ifdef CONFIG_DYNAMIC_FTRACE
1886 : :
1887 : : /* Avoid typos */
1888 : : #define ENABLE_EVENT_STR "enable_event"
1889 : : #define DISABLE_EVENT_STR "disable_event"
1890 : :
1891 : : struct event_probe_data {
1892 : : struct ftrace_event_file *file;
1893 : : unsigned long count;
1894 : : int ref;
1895 : : bool enable;
1896 : : };
1897 : :
1898 : : static void
1899 : 0 : event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1900 : : {
1901 : : struct event_probe_data **pdata = (struct event_probe_data **)_data;
1902 : 0 : struct event_probe_data *data = *pdata;
1903 : :
1904 [ # # ]: 0 : if (!data)
1905 : 0 : return;
1906 : :
1907 [ # # ]: 0 : if (data->enable)
1908 : 0 : clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1909 : : else
1910 : 0 : set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1911 : : }
1912 : :
1913 : : static void
1914 : 0 : event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1915 : : {
1916 : : struct event_probe_data **pdata = (struct event_probe_data **)_data;
1917 : 0 : struct event_probe_data *data = *pdata;
1918 : :
1919 [ # # ]: 0 : if (!data)
1920 : : return;
1921 : :
1922 [ # # ]: 0 : if (!data->count)
1923 : : return;
1924 : :
1925 : : /* Skip if the event is in a state we want to switch to */
1926 [ # # ]: 0 : if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1927 : : return;
1928 : :
1929 [ # # ]: 0 : if (data->count != -1)
1930 : 0 : (data->count)--;
1931 : :
1932 : 0 : event_enable_probe(ip, parent_ip, _data);
1933 : : }
1934 : :
1935 : : static int
1936 : 0 : event_enable_print(struct seq_file *m, unsigned long ip,
1937 : : struct ftrace_probe_ops *ops, void *_data)
1938 : : {
1939 : : struct event_probe_data *data = _data;
1940 : :
1941 : 0 : seq_printf(m, "%ps:", (void *)ip);
1942 : :
1943 [ # # ]: 0 : seq_printf(m, "%s:%s:%s",
1944 : 0 : data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1945 : 0 : data->file->event_call->class->system,
1946 : : data->file->event_call->name);
1947 : :
1948 [ # # ]: 0 : if (data->count == -1)
1949 : 0 : seq_printf(m, ":unlimited\n");
1950 : : else
1951 : 0 : seq_printf(m, ":count=%ld\n", data->count);
1952 : :
1953 : 0 : return 0;
1954 : : }
1955 : :
1956 : : static int
1957 : 0 : event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1958 : : void **_data)
1959 : : {
1960 : : struct event_probe_data **pdata = (struct event_probe_data **)_data;
1961 : 0 : struct event_probe_data *data = *pdata;
1962 : :
1963 : 0 : data->ref++;
1964 : 0 : return 0;
1965 : : }
1966 : :
1967 : : static void
1968 : 0 : event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1969 : : void **_data)
1970 : : {
1971 : : struct event_probe_data **pdata = (struct event_probe_data **)_data;
1972 : 0 : struct event_probe_data *data = *pdata;
1973 : :
1974 [ # # ][ # # ]: 0 : if (WARN_ON_ONCE(data->ref <= 0))
[ # # ][ # # ]
1975 : 0 : return;
1976 : :
1977 : 0 : data->ref--;
1978 [ # # ]: 0 : if (!data->ref) {
1979 : : /* Remove the SOFT_MODE flag */
1980 : 0 : __ftrace_event_enable_disable(data->file, 0, 1);
1981 : 0 : module_put(data->file->event_call->mod);
1982 : 0 : kfree(data);
1983 : : }
1984 : 0 : *pdata = NULL;
1985 : : }
1986 : :
1987 : : static struct ftrace_probe_ops event_enable_probe_ops = {
1988 : : .func = event_enable_probe,
1989 : : .print = event_enable_print,
1990 : : .init = event_enable_init,
1991 : : .free = event_enable_free,
1992 : : };
1993 : :
1994 : : static struct ftrace_probe_ops event_enable_count_probe_ops = {
1995 : : .func = event_enable_count_probe,
1996 : : .print = event_enable_print,
1997 : : .init = event_enable_init,
1998 : : .free = event_enable_free,
1999 : : };
2000 : :
2001 : : static struct ftrace_probe_ops event_disable_probe_ops = {
2002 : : .func = event_enable_probe,
2003 : : .print = event_enable_print,
2004 : : .init = event_enable_init,
2005 : : .free = event_enable_free,
2006 : : };
2007 : :
2008 : : static struct ftrace_probe_ops event_disable_count_probe_ops = {
2009 : : .func = event_enable_count_probe,
2010 : : .print = event_enable_print,
2011 : : .init = event_enable_init,
2012 : : .free = event_enable_free,
2013 : : };
2014 : :
2015 : : static int
2016 : 0 : event_enable_func(struct ftrace_hash *hash,
2017 : : char *glob, char *cmd, char *param, int enabled)
2018 : : {
2019 : : struct trace_array *tr = top_trace_array();
2020 : : struct ftrace_event_file *file;
2021 : : struct ftrace_probe_ops *ops;
2022 : : struct event_probe_data *data;
2023 : : const char *system;
2024 : : const char *event;
2025 : : char *number;
2026 : : bool enable;
2027 : : int ret;
2028 : :
2029 : : /* hash funcs only work with set_ftrace_filter */
2030 [ # # ]: 0 : if (!enabled || !param)
2031 : : return -EINVAL;
2032 : :
2033 : 0 : system = strsep(¶m, ":");
2034 [ # # ]: 0 : if (!param)
2035 : : return -EINVAL;
2036 : :
2037 : 0 : event = strsep(¶m, ":");
2038 : :
2039 : 0 : mutex_lock(&event_mutex);
2040 : :
2041 : : ret = -EINVAL;
2042 : 0 : file = find_event_file(tr, system, event);
2043 [ # # ]: 0 : if (!file)
2044 : : goto out;
2045 : :
2046 : 0 : enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2047 : :
2048 [ # # ]: 0 : if (enable)
2049 [ # # ]: 0 : ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2050 : : else
2051 [ # # ]: 0 : ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2052 : :
2053 [ # # ]: 0 : if (glob[0] == '!') {
2054 : 0 : unregister_ftrace_function_probe_func(glob+1, ops);
2055 : : ret = 0;
2056 : 0 : goto out;
2057 : : }
2058 : :
2059 : : ret = -ENOMEM;
2060 : : data = kzalloc(sizeof(*data), GFP_KERNEL);
2061 [ # # ]: 0 : if (!data)
2062 : : goto out;
2063 : :
2064 : 0 : data->enable = enable;
2065 : 0 : data->count = -1;
2066 : 0 : data->file = file;
2067 : :
2068 [ # # ]: 0 : if (!param)
2069 : : goto out_reg;
2070 : :
2071 : 0 : number = strsep(¶m, ":");
2072 : :
2073 : : ret = -EINVAL;
2074 [ # # ]: 0 : if (!strlen(number))
2075 : : goto out_free;
2076 : :
2077 : : /*
2078 : : * We use the callback data field (which is a pointer)
2079 : : * as our counter.
2080 : : */
2081 : 0 : ret = kstrtoul(number, 0, &data->count);
2082 [ # # ]: 0 : if (ret)
2083 : : goto out_free;
2084 : :
2085 : : out_reg:
2086 : : /* Don't let event modules unload while probe registered */
2087 : 0 : ret = try_module_get(file->event_call->mod);
2088 [ # # ]: 0 : if (!ret) {
2089 : : ret = -EBUSY;
2090 : : goto out_free;
2091 : : }
2092 : :
2093 : 0 : ret = __ftrace_event_enable_disable(file, 1, 1);
2094 [ # # ]: 0 : if (ret < 0)
2095 : : goto out_put;
2096 : 0 : ret = register_ftrace_function_probe(glob, ops, data);
2097 : : /*
2098 : : * The above returns on success the # of functions enabled,
2099 : : * but if it didn't find any functions it returns zero.
2100 : : * Consider no functions a failure too.
2101 : : */
2102 [ # # ]: 0 : if (!ret) {
2103 : : ret = -ENOENT;
2104 : : goto out_disable;
2105 [ # # ]: 0 : } else if (ret < 0)
2106 : : goto out_disable;
2107 : : /* Just return zero, not the number of enabled functions */
2108 : : ret = 0;
2109 : : out:
2110 : 0 : mutex_unlock(&event_mutex);
2111 : 0 : return ret;
2112 : :
2113 : : out_disable:
2114 : 0 : __ftrace_event_enable_disable(file, 0, 1);
2115 : : out_put:
2116 : 0 : module_put(file->event_call->mod);
2117 : : out_free:
2118 : 0 : kfree(data);
2119 : 0 : goto out;
2120 : : }
2121 : :
2122 : : static struct ftrace_func_command event_enable_cmd = {
2123 : : .name = ENABLE_EVENT_STR,
2124 : : .func = event_enable_func,
2125 : : };
2126 : :
2127 : : static struct ftrace_func_command event_disable_cmd = {
2128 : : .name = DISABLE_EVENT_STR,
2129 : : .func = event_enable_func,
2130 : : };
2131 : :
2132 : 0 : static __init int register_event_cmds(void)
2133 : : {
2134 : : int ret;
2135 : :
2136 : 0 : ret = register_ftrace_command(&event_enable_cmd);
2137 [ # # ][ # # ]: 0 : if (WARN_ON(ret < 0))
2138 : : return ret;
2139 : 0 : ret = register_ftrace_command(&event_disable_cmd);
2140 [ # # ][ # # ]: 0 : if (WARN_ON(ret < 0))
2141 : 0 : unregister_ftrace_command(&event_enable_cmd);
2142 : 0 : return ret;
2143 : : }
2144 : : #else
2145 : : static inline int register_event_cmds(void) { return 0; }
2146 : : #endif /* CONFIG_DYNAMIC_FTRACE */
2147 : :
2148 : : /*
2149 : : * The top level array has already had its ftrace_event_file
2150 : : * descriptors created in order to allow for early events to
2151 : : * be recorded. This function is called after the debugfs has been
2152 : : * initialized, and we now have to create the files associated
2153 : : * to the events.
2154 : : */
2155 : : static __init void
2156 : 0 : __trace_early_add_event_dirs(struct trace_array *tr)
2157 : : {
2158 : : struct ftrace_event_file *file;
2159 : : int ret;
2160 : :
2161 : :
2162 [ # # ]: 0 : list_for_each_entry(file, &tr->events, list) {
2163 : 0 : ret = event_create_dir(tr->event_dir, file);
2164 [ # # ]: 0 : if (ret < 0)
2165 : 0 : pr_warning("Could not create directory for event %s\n",
2166 : : file->event_call->name);
2167 : : }
2168 : 0 : }
2169 : :
2170 : : /*
2171 : : * For early boot up, the top trace array requires to have
2172 : : * a list of events that can be enabled. This must be done before
2173 : : * the filesystem is set up in order to allow events to be traced
2174 : : * early.
2175 : : */
2176 : : static __init void
2177 : 0 : __trace_early_add_events(struct trace_array *tr)
2178 : : {
2179 : : struct ftrace_event_call *call;
2180 : : int ret;
2181 : :
2182 [ # # ]: 0 : list_for_each_entry(call, &ftrace_events, list) {
2183 : : /* Early boot up should not have any modules loaded */
2184 [ # # ][ # # ]: 0 : if (WARN_ON_ONCE(call->mod))
[ # # ][ # # ]
2185 : 0 : continue;
2186 : :
2187 : 0 : ret = __trace_early_add_new_event(call, tr);
2188 [ # # ]: 0 : if (ret < 0)
2189 : 0 : pr_warning("Could not create early event %s\n",
2190 : : call->name);
2191 : : }
2192 : 0 : }
2193 : :
2194 : : /* Remove the event directory structure for a trace directory. */
2195 : : static void
2196 : : __trace_remove_event_dirs(struct trace_array *tr)
2197 : : {
2198 : : struct ftrace_event_file *file, *next;
2199 : :
2200 [ # # ]: 0 : list_for_each_entry_safe(file, next, &tr->events, list)
2201 : 0 : remove_event_file_dir(file);
2202 : : }
2203 : :
2204 : : static void __add_event_to_tracers(struct ftrace_event_call *call)
2205 : : {
2206 : : struct trace_array *tr;
2207 : :
2208 [ # # ][ # # ]: 0 : list_for_each_entry(tr, &ftrace_trace_arrays, list)
2209 : 0 : __trace_add_new_event(call, tr);
2210 : : }
2211 : :
2212 : : extern struct ftrace_event_call *__start_ftrace_events[];
2213 : : extern struct ftrace_event_call *__stop_ftrace_events[];
2214 : :
2215 : : static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2216 : :
2217 : 0 : static __init int setup_trace_event(char *str)
2218 : : {
2219 : 0 : strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2220 : 0 : ring_buffer_expanded = true;
2221 : 0 : tracing_selftest_disabled = true;
2222 : :
2223 : 0 : return 1;
2224 : : }
2225 : : __setup("trace_event=", setup_trace_event);
2226 : :
2227 : : /* Expects to have event_mutex held when called */
2228 : : static int
2229 : 0 : create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2230 : : {
2231 : : struct dentry *d_events;
2232 : : struct dentry *entry;
2233 : :
2234 : 0 : entry = debugfs_create_file("set_event", 0644, parent,
2235 : : tr, &ftrace_set_event_fops);
2236 [ # # ]: 0 : if (!entry) {
2237 : 0 : pr_warning("Could not create debugfs 'set_event' entry\n");
2238 : 0 : return -ENOMEM;
2239 : : }
2240 : :
2241 : 0 : d_events = debugfs_create_dir("events", parent);
2242 [ # # ]: 0 : if (!d_events) {
2243 : 0 : pr_warning("Could not create debugfs 'events' directory\n");
2244 : 0 : return -ENOMEM;
2245 : : }
2246 : :
2247 : : /* ring buffer internal formats */
2248 : 0 : trace_create_file("header_page", 0444, d_events,
2249 : : ring_buffer_print_page_header,
2250 : : &ftrace_show_header_fops);
2251 : :
2252 : 0 : trace_create_file("header_event", 0444, d_events,
2253 : : ring_buffer_print_entry_header,
2254 : : &ftrace_show_header_fops);
2255 : :
2256 : 0 : trace_create_file("enable", 0644, d_events,
2257 : : tr, &ftrace_tr_enable_fops);
2258 : :
2259 : 0 : tr->event_dir = d_events;
2260 : :
2261 : 0 : return 0;
2262 : : }
2263 : :
2264 : : /**
2265 : : * event_trace_add_tracer - add a instance of a trace_array to events
2266 : : * @parent: The parent dentry to place the files/directories for events in
2267 : : * @tr: The trace array associated with these events
2268 : : *
2269 : : * When a new instance is created, it needs to set up its events
2270 : : * directory, as well as other files associated with events. It also
2271 : : * creates the event hierachry in the @parent/events directory.
2272 : : *
2273 : : * Returns 0 on success.
2274 : : */
2275 : 0 : int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2276 : : {
2277 : : int ret;
2278 : :
2279 : 0 : mutex_lock(&event_mutex);
2280 : :
2281 : 0 : ret = create_event_toplevel_files(parent, tr);
2282 [ # # ]: 0 : if (ret)
2283 : : goto out_unlock;
2284 : :
2285 : 0 : down_write(&trace_event_sem);
2286 : 0 : __trace_add_event_dirs(tr);
2287 : 0 : up_write(&trace_event_sem);
2288 : :
2289 : : out_unlock:
2290 : 0 : mutex_unlock(&event_mutex);
2291 : :
2292 : 0 : return ret;
2293 : : }
2294 : :
2295 : : /*
2296 : : * The top trace array already had its file descriptors created.
2297 : : * Now the files themselves need to be created.
2298 : : */
2299 : : static __init int
2300 : 0 : early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2301 : : {
2302 : : int ret;
2303 : :
2304 : 0 : mutex_lock(&event_mutex);
2305 : :
2306 : 0 : ret = create_event_toplevel_files(parent, tr);
2307 [ # # ]: 0 : if (ret)
2308 : : goto out_unlock;
2309 : :
2310 : 0 : down_write(&trace_event_sem);
2311 : 0 : __trace_early_add_event_dirs(tr);
2312 : 0 : up_write(&trace_event_sem);
2313 : :
2314 : : out_unlock:
2315 : 0 : mutex_unlock(&event_mutex);
2316 : :
2317 : 0 : return ret;
2318 : : }
2319 : :
2320 : 0 : int event_trace_del_tracer(struct trace_array *tr)
2321 : : {
2322 : 0 : mutex_lock(&event_mutex);
2323 : :
2324 : : /* Disable any event triggers and associated soft-disabled events */
2325 : 0 : clear_event_triggers(tr);
2326 : :
2327 : : /* Disable any running events */
2328 : 0 : __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2329 : :
2330 : : /* Access to events are within rcu_read_lock_sched() */
2331 : 0 : synchronize_sched();
2332 : :
2333 : 0 : down_write(&trace_event_sem);
2334 : : __trace_remove_event_dirs(tr);
2335 : 0 : debugfs_remove_recursive(tr->event_dir);
2336 : 0 : up_write(&trace_event_sem);
2337 : :
2338 : 0 : tr->event_dir = NULL;
2339 : :
2340 : 0 : mutex_unlock(&event_mutex);
2341 : :
2342 : 0 : return 0;
2343 : : }
2344 : :
2345 : 0 : static __init int event_trace_memsetup(void)
2346 : : {
2347 : 0 : field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2348 : 0 : file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2349 : 0 : return 0;
2350 : : }
2351 : :
2352 : 0 : static __init int event_trace_enable(void)
2353 : : {
2354 : : struct trace_array *tr = top_trace_array();
2355 : : struct ftrace_event_call **iter, *call;
2356 : 0 : char *buf = bootup_event_buf;
2357 : : char *token;
2358 : : int ret;
2359 : :
2360 [ # # ]: 0 : for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2361 : :
2362 : 0 : call = *iter;
2363 : 0 : ret = event_init(call);
2364 [ # # ]: 0 : if (!ret)
2365 : 0 : list_add(&call->list, &ftrace_events);
2366 : : }
2367 : :
2368 : : /*
2369 : : * We need the top trace array to have a working set of trace
2370 : : * points at early init, before the debug files and directories
2371 : : * are created. Create the file entries now, and attach them
2372 : : * to the actual file dentries later.
2373 : : */
2374 : 0 : __trace_early_add_events(tr);
2375 : :
2376 : : while (true) {
2377 : 0 : token = strsep(&buf, ",");
2378 : :
2379 [ # # ]: 0 : if (!token)
2380 : : break;
2381 [ # # ]: 0 : if (!*token)
2382 : 0 : continue;
2383 : :
2384 : 0 : ret = ftrace_set_clr_event(tr, token, 1);
2385 [ # # ]: 0 : if (ret)
2386 : 0 : pr_warn("Failed to enable trace event: %s\n", token);
2387 : : }
2388 : :
2389 : 0 : trace_printk_start_comm();
2390 : :
2391 : 0 : register_event_cmds();
2392 : :
2393 : 0 : register_trigger_cmds();
2394 : :
2395 : 0 : return 0;
2396 : : }
2397 : :
2398 : 0 : static __init int event_trace_init(void)
2399 : : {
2400 : : struct trace_array *tr;
2401 : : struct dentry *d_tracer;
2402 : : struct dentry *entry;
2403 : : int ret;
2404 : :
2405 : : tr = top_trace_array();
2406 : :
2407 : 0 : d_tracer = tracing_init_dentry();
2408 [ # # ]: 0 : if (!d_tracer)
2409 : : return 0;
2410 : :
2411 : 0 : entry = debugfs_create_file("available_events", 0444, d_tracer,
2412 : : tr, &ftrace_avail_fops);
2413 [ # # ]: 0 : if (!entry)
2414 : 0 : pr_warning("Could not create debugfs "
2415 : : "'available_events' entry\n");
2416 : :
2417 [ # # ]: 0 : if (trace_define_common_fields())
2418 : 0 : pr_warning("tracing: Failed to allocate common fields");
2419 : :
2420 : 0 : ret = early_event_add_tracer(d_tracer, tr);
2421 [ # # ]: 0 : if (ret)
2422 : : return ret;
2423 : :
2424 : : #ifdef CONFIG_MODULES
2425 : 0 : ret = register_module_notifier(&trace_module_nb);
2426 [ # # ]: 0 : if (ret)
2427 : 0 : pr_warning("Failed to register trace events module notifier\n");
2428 : : #endif
2429 : : return 0;
2430 : : }
2431 : : early_initcall(event_trace_memsetup);
2432 : : core_initcall(event_trace_enable);
2433 : : fs_initcall(event_trace_init);
2434 : :
2435 : : #ifdef CONFIG_FTRACE_STARTUP_TEST
2436 : :
2437 : : static DEFINE_SPINLOCK(test_spinlock);
2438 : : static DEFINE_SPINLOCK(test_spinlock_irq);
2439 : : static DEFINE_MUTEX(test_mutex);
2440 : :
2441 : : static __init void test_work(struct work_struct *dummy)
2442 : : {
2443 : : spin_lock(&test_spinlock);
2444 : : spin_lock_irq(&test_spinlock_irq);
2445 : : udelay(1);
2446 : : spin_unlock_irq(&test_spinlock_irq);
2447 : : spin_unlock(&test_spinlock);
2448 : :
2449 : : mutex_lock(&test_mutex);
2450 : : msleep(1);
2451 : : mutex_unlock(&test_mutex);
2452 : : }
2453 : :
2454 : : static __init int event_test_thread(void *unused)
2455 : : {
2456 : : void *test_malloc;
2457 : :
2458 : : test_malloc = kmalloc(1234, GFP_KERNEL);
2459 : : if (!test_malloc)
2460 : : pr_info("failed to kmalloc\n");
2461 : :
2462 : : schedule_on_each_cpu(test_work);
2463 : :
2464 : : kfree(test_malloc);
2465 : :
2466 : : set_current_state(TASK_INTERRUPTIBLE);
2467 : : while (!kthread_should_stop())
2468 : : schedule();
2469 : :
2470 : : return 0;
2471 : : }
2472 : :
2473 : : /*
2474 : : * Do various things that may trigger events.
2475 : : */
2476 : : static __init void event_test_stuff(void)
2477 : : {
2478 : : struct task_struct *test_thread;
2479 : :
2480 : : test_thread = kthread_run(event_test_thread, NULL, "test-events");
2481 : : msleep(1);
2482 : : kthread_stop(test_thread);
2483 : : }
2484 : :
2485 : : /*
2486 : : * For every trace event defined, we will test each trace point separately,
2487 : : * and then by groups, and finally all trace points.
2488 : : */
2489 : : static __init void event_trace_self_tests(void)
2490 : : {
2491 : : struct ftrace_subsystem_dir *dir;
2492 : : struct ftrace_event_file *file;
2493 : : struct ftrace_event_call *call;
2494 : : struct event_subsystem *system;
2495 : : struct trace_array *tr;
2496 : : int ret;
2497 : :
2498 : : tr = top_trace_array();
2499 : :
2500 : : pr_info("Running tests on trace events:\n");
2501 : :
2502 : : list_for_each_entry(file, &tr->events, list) {
2503 : :
2504 : : call = file->event_call;
2505 : :
2506 : : /* Only test those that have a probe */
2507 : : if (!call->class || !call->class->probe)
2508 : : continue;
2509 : :
2510 : : /*
2511 : : * Testing syscall events here is pretty useless, but
2512 : : * we still do it if configured. But this is time consuming.
2513 : : * What we really need is a user thread to perform the
2514 : : * syscalls as we test.
2515 : : */
2516 : : #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2517 : : if (call->class->system &&
2518 : : strcmp(call->class->system, "syscalls") == 0)
2519 : : continue;
2520 : : #endif
2521 : :
2522 : : pr_info("Testing event %s: ", call->name);
2523 : :
2524 : : /*
2525 : : * If an event is already enabled, someone is using
2526 : : * it and the self test should not be on.
2527 : : */
2528 : : if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2529 : : pr_warning("Enabled event during self test!\n");
2530 : : WARN_ON_ONCE(1);
2531 : : continue;
2532 : : }
2533 : :
2534 : : ftrace_event_enable_disable(file, 1);
2535 : : event_test_stuff();
2536 : : ftrace_event_enable_disable(file, 0);
2537 : :
2538 : : pr_cont("OK\n");
2539 : : }
2540 : :
2541 : : /* Now test at the sub system level */
2542 : :
2543 : : pr_info("Running tests on trace event systems:\n");
2544 : :
2545 : : list_for_each_entry(dir, &tr->systems, list) {
2546 : :
2547 : : system = dir->subsystem;
2548 : :
2549 : : /* the ftrace system is special, skip it */
2550 : : if (strcmp(system->name, "ftrace") == 0)
2551 : : continue;
2552 : :
2553 : : pr_info("Testing event system %s: ", system->name);
2554 : :
2555 : : ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2556 : : if (WARN_ON_ONCE(ret)) {
2557 : : pr_warning("error enabling system %s\n",
2558 : : system->name);
2559 : : continue;
2560 : : }
2561 : :
2562 : : event_test_stuff();
2563 : :
2564 : : ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2565 : : if (WARN_ON_ONCE(ret)) {
2566 : : pr_warning("error disabling system %s\n",
2567 : : system->name);
2568 : : continue;
2569 : : }
2570 : :
2571 : : pr_cont("OK\n");
2572 : : }
2573 : :
2574 : : /* Test with all events enabled */
2575 : :
2576 : : pr_info("Running tests on all trace events:\n");
2577 : : pr_info("Testing all events: ");
2578 : :
2579 : : ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2580 : : if (WARN_ON_ONCE(ret)) {
2581 : : pr_warning("error enabling all events\n");
2582 : : return;
2583 : : }
2584 : :
2585 : : event_test_stuff();
2586 : :
2587 : : /* reset sysname */
2588 : : ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2589 : : if (WARN_ON_ONCE(ret)) {
2590 : : pr_warning("error disabling all events\n");
2591 : : return;
2592 : : }
2593 : :
2594 : : pr_cont("OK\n");
2595 : : }
2596 : :
2597 : : #ifdef CONFIG_FUNCTION_TRACER
2598 : :
2599 : : static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2600 : :
2601 : : static void
2602 : : function_test_events_call(unsigned long ip, unsigned long parent_ip,
2603 : : struct ftrace_ops *op, struct pt_regs *pt_regs)
2604 : : {
2605 : : struct ring_buffer_event *event;
2606 : : struct ring_buffer *buffer;
2607 : : struct ftrace_entry *entry;
2608 : : unsigned long flags;
2609 : : long disabled;
2610 : : int cpu;
2611 : : int pc;
2612 : :
2613 : : pc = preempt_count();
2614 : : preempt_disable_notrace();
2615 : : cpu = raw_smp_processor_id();
2616 : : disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2617 : :
2618 : : if (disabled != 1)
2619 : : goto out;
2620 : :
2621 : : local_save_flags(flags);
2622 : :
2623 : : event = trace_current_buffer_lock_reserve(&buffer,
2624 : : TRACE_FN, sizeof(*entry),
2625 : : flags, pc);
2626 : : if (!event)
2627 : : goto out;
2628 : : entry = ring_buffer_event_data(event);
2629 : : entry->ip = ip;
2630 : : entry->parent_ip = parent_ip;
2631 : :
2632 : : trace_buffer_unlock_commit(buffer, event, flags, pc);
2633 : :
2634 : : out:
2635 : : atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2636 : : preempt_enable_notrace();
2637 : : }
2638 : :
2639 : : static struct ftrace_ops trace_ops __initdata =
2640 : : {
2641 : : .func = function_test_events_call,
2642 : : .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2643 : : };
2644 : :
2645 : : static __init void event_trace_self_test_with_function(void)
2646 : : {
2647 : : int ret;
2648 : : ret = register_ftrace_function(&trace_ops);
2649 : : if (WARN_ON(ret < 0)) {
2650 : : pr_info("Failed to enable function tracer for event tests\n");
2651 : : return;
2652 : : }
2653 : : pr_info("Running tests again, along with the function tracer\n");
2654 : : event_trace_self_tests();
2655 : : unregister_ftrace_function(&trace_ops);
2656 : : }
2657 : : #else
2658 : : static __init void event_trace_self_test_with_function(void)
2659 : : {
2660 : : }
2661 : : #endif
2662 : :
2663 : : static __init int event_trace_self_tests_init(void)
2664 : : {
2665 : : if (!tracing_selftest_disabled) {
2666 : : event_trace_self_tests();
2667 : : event_trace_self_test_with_function();
2668 : : }
2669 : :
2670 : : return 0;
2671 : : }
2672 : :
2673 : : late_initcall(event_trace_self_tests_init);
2674 : :
2675 : : #endif
|