1 /* Include in trace.c */
3 #include <linux/stringify.h>
4 #include <linux/kthread.h>
5 #include <linux/delay.h>
7 static inline int trace_valid_entry(struct trace_entry
*entry
)
24 static int trace_test_buffer_cpu(struct trace_array
*tr
, int cpu
)
26 struct ring_buffer_event
*event
;
27 struct trace_entry
*entry
;
28 unsigned int loops
= 0;
30 while ((event
= ring_buffer_consume(tr
->buffer
, cpu
, NULL
))) {
31 entry
= ring_buffer_event_data(event
);
34 * The ring buffer is a size of trace_buf_size, if
35 * we loop more than the size, there's something wrong
36 * with the ring buffer.
38 if (loops
++ > trace_buf_size
) {
39 printk(KERN_CONT
".. bad ring buffer ");
42 if (!trace_valid_entry(entry
)) {
43 printk(KERN_CONT
".. invalid entry %d ",
53 printk(KERN_CONT
".. corrupted trace buffer .. ");
58 * Test the trace buffer to see if all the elements
61 static int trace_test_buffer(struct trace_array
*tr
, unsigned long *count
)
63 unsigned long flags
, cnt
= 0;
66 /* Don't allow flipping of max traces now */
67 local_irq_save(flags
);
68 __raw_spin_lock(&ftrace_max_lock
);
70 cnt
= ring_buffer_entries(tr
->buffer
);
73 * The trace_test_buffer_cpu runs a while loop to consume all data.
74 * If the calling tracer is broken, and is constantly filling
75 * the buffer, this will run forever, and hard lock the box.
76 * We disable the ring buffer while we do this test to prevent
80 for_each_possible_cpu(cpu
) {
81 ret
= trace_test_buffer_cpu(tr
, cpu
);
86 __raw_spin_unlock(&ftrace_max_lock
);
87 local_irq_restore(flags
);
95 static inline void warn_failed_init_tracer(struct tracer
*trace
, int init_ret
)
97 printk(KERN_WARNING
"Failed to init %s tracer, init returned %d\n",
98 trace
->name
, init_ret
);
100 #ifdef CONFIG_FUNCTION_TRACER
102 #ifdef CONFIG_DYNAMIC_FTRACE
104 /* Test dynamic code modification and ftrace filters */
105 int trace_selftest_startup_dynamic_tracing(struct tracer
*trace
,
106 struct trace_array
*tr
,
109 int save_ftrace_enabled
= ftrace_enabled
;
110 int save_tracer_enabled
= tracer_enabled
;
115 /* The ftrace test PASSED */
116 printk(KERN_CONT
"PASSED\n");
117 pr_info("Testing dynamic ftrace: ");
119 /* enable tracing, and record the filter function */
123 /* passed in by parameter to fool gcc from optimizing */
127 * Some archs *cough*PowerPC*cough* add characters to the
128 * start of the function names. We simply put a '*' to
131 func_name
= "*" __stringify(DYN_FTRACE_TEST_NAME
);
133 /* filter only on our function */
134 ftrace_set_filter(func_name
, strlen(func_name
), 1);
137 ret
= tracer_init(trace
, tr
);
139 warn_failed_init_tracer(trace
, ret
);
143 /* Sleep for a 1/10 of a second */
146 /* we should have nothing in the buffer */
147 ret
= trace_test_buffer(tr
, &count
);
153 printk(KERN_CONT
".. filter did not filter .. ");
157 /* call our function again */
163 /* stop the tracing. */
167 /* check the trace buffer */
168 ret
= trace_test_buffer(tr
, &count
);
172 /* we should only have one item */
173 if (!ret
&& count
!= 1) {
174 printk(KERN_CONT
".. filter failed count=%ld ..", count
);
180 ftrace_enabled
= save_ftrace_enabled
;
181 tracer_enabled
= save_tracer_enabled
;
183 /* Enable tracing on all functions again */
184 ftrace_set_filter(NULL
, 0, 1);
189 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
190 #endif /* CONFIG_DYNAMIC_FTRACE */
192 * Simple verification test of ftrace function tracer.
193 * Enable ftrace, sleep 1/10 second, and then read the trace
194 * buffer to see if all is in order.
197 trace_selftest_startup_function(struct tracer
*trace
, struct trace_array
*tr
)
199 int save_ftrace_enabled
= ftrace_enabled
;
200 int save_tracer_enabled
= tracer_enabled
;
204 /* make sure msleep has been recorded */
207 /* start the tracing */
211 ret
= tracer_init(trace
, tr
);
213 warn_failed_init_tracer(trace
, ret
);
217 /* Sleep for a 1/10 of a second */
219 /* stop the tracing. */
223 /* check the trace buffer */
224 ret
= trace_test_buffer(tr
, &count
);
228 if (!ret
&& !count
) {
229 printk(KERN_CONT
".. no entries found ..");
234 ret
= trace_selftest_startup_dynamic_tracing(trace
, tr
,
235 DYN_FTRACE_TEST_NAME
);
238 ftrace_enabled
= save_ftrace_enabled
;
239 tracer_enabled
= save_tracer_enabled
;
241 /* kill ftrace totally if we failed */
247 #endif /* CONFIG_FUNCTION_TRACER */
250 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
252 /* Maximum number of functions to trace before diagnosing a hang */
253 #define GRAPH_MAX_FUNC_TEST 100000000
255 static void __ftrace_dump(bool disable_tracing
);
256 static unsigned int graph_hang_thresh
;
258 /* Wrap the real function entry probe to avoid possible hanging */
259 static int trace_graph_entry_watchdog(struct ftrace_graph_ent
*trace
)
261 /* This is harmlessly racy, we want to approximately detect a hang */
262 if (unlikely(++graph_hang_thresh
> GRAPH_MAX_FUNC_TEST
)) {
264 printk(KERN_WARNING
"BUG: Function graph tracer hang!\n");
265 if (ftrace_dump_on_oops
)
266 __ftrace_dump(false);
270 return trace_graph_entry(trace
);
274 * Pretty much the same than for the function tracer from which the selftest
278 trace_selftest_startup_function_graph(struct tracer
*trace
,
279 struct trace_array
*tr
)
285 * Simulate the init() callback but we attach a watchdog callback
286 * to detect and recover from possible hangs
288 tracing_reset_online_cpus(tr
);
289 ret
= register_ftrace_graph(&trace_graph_return
,
290 &trace_graph_entry_watchdog
);
292 warn_failed_init_tracer(trace
, ret
);
295 tracing_start_cmdline_record();
297 /* Sleep for a 1/10 of a second */
300 /* Have we just recovered from a hang? */
301 if (graph_hang_thresh
> GRAPH_MAX_FUNC_TEST
) {
302 tracing_selftest_disabled
= true;
309 /* check the trace buffer */
310 ret
= trace_test_buffer(tr
, &count
);
315 if (!ret
&& !count
) {
316 printk(KERN_CONT
".. no entries found ..");
321 /* Don't test dynamic tracing, the function tracer already did */
324 /* Stop it if we failed */
330 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
333 #ifdef CONFIG_IRQSOFF_TRACER
335 trace_selftest_startup_irqsoff(struct tracer
*trace
, struct trace_array
*tr
)
337 unsigned long save_max
= tracing_max_latency
;
341 /* start the tracing */
342 ret
= tracer_init(trace
, tr
);
344 warn_failed_init_tracer(trace
, ret
);
348 /* reset the max latency */
349 tracing_max_latency
= 0;
350 /* disable interrupts for a bit */
356 * Stop the tracer to avoid a warning subsequent
357 * to buffer flipping failure because tracing_stop()
358 * disables the tr and max buffers, making flipping impossible
359 * in case of parallels max irqs off latencies.
362 /* stop the tracing. */
364 /* check both trace buffers */
365 ret
= trace_test_buffer(tr
, NULL
);
367 ret
= trace_test_buffer(&max_tr
, &count
);
371 if (!ret
&& !count
) {
372 printk(KERN_CONT
".. no entries found ..");
376 tracing_max_latency
= save_max
;
380 #endif /* CONFIG_IRQSOFF_TRACER */
382 #ifdef CONFIG_PREEMPT_TRACER
384 trace_selftest_startup_preemptoff(struct tracer
*trace
, struct trace_array
*tr
)
386 unsigned long save_max
= tracing_max_latency
;
391 * Now that the big kernel lock is no longer preemptable,
392 * and this is called with the BKL held, it will always
393 * fail. If preemption is already disabled, simply
394 * pass the test. When the BKL is removed, or becomes
395 * preemptible again, we will once again test this,
398 if (preempt_count()) {
399 printk(KERN_CONT
"can not test ... force ");
403 /* start the tracing */
404 ret
= tracer_init(trace
, tr
);
406 warn_failed_init_tracer(trace
, ret
);
410 /* reset the max latency */
411 tracing_max_latency
= 0;
412 /* disable preemption for a bit */
418 * Stop the tracer to avoid a warning subsequent
419 * to buffer flipping failure because tracing_stop()
420 * disables the tr and max buffers, making flipping impossible
421 * in case of parallels max preempt off latencies.
424 /* stop the tracing. */
426 /* check both trace buffers */
427 ret
= trace_test_buffer(tr
, NULL
);
429 ret
= trace_test_buffer(&max_tr
, &count
);
433 if (!ret
&& !count
) {
434 printk(KERN_CONT
".. no entries found ..");
438 tracing_max_latency
= save_max
;
442 #endif /* CONFIG_PREEMPT_TRACER */
444 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
446 trace_selftest_startup_preemptirqsoff(struct tracer
*trace
, struct trace_array
*tr
)
448 unsigned long save_max
= tracing_max_latency
;
453 * Now that the big kernel lock is no longer preemptable,
454 * and this is called with the BKL held, it will always
455 * fail. If preemption is already disabled, simply
456 * pass the test. When the BKL is removed, or becomes
457 * preemptible again, we will once again test this,
460 if (preempt_count()) {
461 printk(KERN_CONT
"can not test ... force ");
465 /* start the tracing */
466 ret
= tracer_init(trace
, tr
);
468 warn_failed_init_tracer(trace
, ret
);
472 /* reset the max latency */
473 tracing_max_latency
= 0;
475 /* disable preemption and interrupts for a bit */
480 /* reverse the order of preempt vs irqs */
484 * Stop the tracer to avoid a warning subsequent
485 * to buffer flipping failure because tracing_stop()
486 * disables the tr and max buffers, making flipping impossible
487 * in case of parallels max irqs/preempt off latencies.
490 /* stop the tracing. */
492 /* check both trace buffers */
493 ret
= trace_test_buffer(tr
, NULL
);
497 ret
= trace_test_buffer(&max_tr
, &count
);
501 if (!ret
&& !count
) {
502 printk(KERN_CONT
".. no entries found ..");
507 /* do the test by disabling interrupts first this time */
508 tracing_max_latency
= 0;
516 /* reverse the order of preempt vs irqs */
520 /* stop the tracing. */
522 /* check both trace buffers */
523 ret
= trace_test_buffer(tr
, NULL
);
527 ret
= trace_test_buffer(&max_tr
, &count
);
529 if (!ret
&& !count
) {
530 printk(KERN_CONT
".. no entries found ..");
539 tracing_max_latency
= save_max
;
543 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
545 #ifdef CONFIG_NOP_TRACER
547 trace_selftest_startup_nop(struct tracer
*trace
, struct trace_array
*tr
)
549 /* What could possibly go wrong? */
554 #ifdef CONFIG_SCHED_TRACER
555 static int trace_wakeup_test_thread(void *data
)
557 /* Make this a RT thread, doesn't need to be too high */
558 struct sched_param param
= { .sched_priority
= 5 };
559 struct completion
*x
= data
;
561 sched_setscheduler(current
, SCHED_FIFO
, ¶m
);
563 /* Make it know we have a new prio */
566 /* now go to sleep and let the test wake us up */
567 set_current_state(TASK_INTERRUPTIBLE
);
570 /* we are awake, now wait to disappear */
571 while (!kthread_should_stop()) {
573 * This is an RT task, do short sleeps to let
583 trace_selftest_startup_wakeup(struct tracer
*trace
, struct trace_array
*tr
)
585 unsigned long save_max
= tracing_max_latency
;
586 struct task_struct
*p
;
587 struct completion isrt
;
591 init_completion(&isrt
);
593 /* create a high prio thread */
594 p
= kthread_run(trace_wakeup_test_thread
, &isrt
, "ftrace-test");
596 printk(KERN_CONT
"Failed to create ftrace wakeup test thread ");
600 /* make sure the thread is running at an RT prio */
601 wait_for_completion(&isrt
);
603 /* start the tracing */
604 ret
= tracer_init(trace
, tr
);
606 warn_failed_init_tracer(trace
, ret
);
610 /* reset the max latency */
611 tracing_max_latency
= 0;
613 /* sleep to let the RT thread sleep too */
617 * Yes this is slightly racy. It is possible that for some
618 * strange reason that the RT thread we created, did not
619 * call schedule for 100ms after doing the completion,
620 * and we do a wakeup on a task that already is awake.
621 * But that is extremely unlikely, and the worst thing that
622 * happens in such a case, is that we disable tracing.
623 * Honestly, if this race does happen something is horrible
624 * wrong with the system.
629 /* give a little time to let the thread wake up */
632 /* stop the tracing. */
634 /* check both trace buffers */
635 ret
= trace_test_buffer(tr
, NULL
);
637 ret
= trace_test_buffer(&max_tr
, &count
);
643 tracing_max_latency
= save_max
;
645 /* kill the thread */
648 if (!ret
&& !count
) {
649 printk(KERN_CONT
".. no entries found ..");
655 #endif /* CONFIG_SCHED_TRACER */
657 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
659 trace_selftest_startup_sched_switch(struct tracer
*trace
, struct trace_array
*tr
)
664 /* start the tracing */
665 ret
= tracer_init(trace
, tr
);
667 warn_failed_init_tracer(trace
, ret
);
671 /* Sleep for a 1/10 of a second */
673 /* stop the tracing. */
675 /* check the trace buffer */
676 ret
= trace_test_buffer(tr
, &count
);
680 if (!ret
&& !count
) {
681 printk(KERN_CONT
".. no entries found ..");
687 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
689 #ifdef CONFIG_SYSPROF_TRACER
691 trace_selftest_startup_sysprof(struct tracer
*trace
, struct trace_array
*tr
)
696 /* start the tracing */
697 ret
= tracer_init(trace
, tr
);
699 warn_failed_init_tracer(trace
, ret
);
703 /* Sleep for a 1/10 of a second */
705 /* stop the tracing. */
707 /* check the trace buffer */
708 ret
= trace_test_buffer(tr
, &count
);
712 if (!ret
&& !count
) {
713 printk(KERN_CONT
".. no entries found ..");
719 #endif /* CONFIG_SYSPROF_TRACER */
721 #ifdef CONFIG_BRANCH_TRACER
723 trace_selftest_startup_branch(struct tracer
*trace
, struct trace_array
*tr
)
728 /* start the tracing */
729 ret
= tracer_init(trace
, tr
);
731 warn_failed_init_tracer(trace
, ret
);
735 /* Sleep for a 1/10 of a second */
737 /* stop the tracing. */
739 /* check the trace buffer */
740 ret
= trace_test_buffer(tr
, &count
);
744 if (!ret
&& !count
) {
745 printk(KERN_CONT
".. no entries found ..");
751 #endif /* CONFIG_BRANCH_TRACER */