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
)
19 case TRACE_HW_BRANCHES
:
26 static int trace_test_buffer_cpu(struct trace_array
*tr
, int cpu
)
28 struct ring_buffer_event
*event
;
29 struct trace_entry
*entry
;
30 unsigned int loops
= 0;
32 while ((event
= ring_buffer_consume(tr
->buffer
, cpu
, NULL
))) {
33 entry
= ring_buffer_event_data(event
);
36 * The ring buffer is a size of trace_buf_size, if
37 * we loop more than the size, there's something wrong
38 * with the ring buffer.
40 if (loops
++ > trace_buf_size
) {
41 printk(KERN_CONT
".. bad ring buffer ");
44 if (!trace_valid_entry(entry
)) {
45 printk(KERN_CONT
".. invalid entry %d ",
55 printk(KERN_CONT
".. corrupted trace buffer .. ");
60 * Test the trace buffer to see if all the elements
63 static int trace_test_buffer(struct trace_array
*tr
, unsigned long *count
)
65 unsigned long flags
, cnt
= 0;
68 /* Don't allow flipping of max traces now */
69 local_irq_save(flags
);
70 arch_spin_lock(&ftrace_max_lock
);
72 cnt
= ring_buffer_entries(tr
->buffer
);
75 * The trace_test_buffer_cpu runs a while loop to consume all data.
76 * If the calling tracer is broken, and is constantly filling
77 * the buffer, this will run forever, and hard lock the box.
78 * We disable the ring buffer while we do this test to prevent
82 for_each_possible_cpu(cpu
) {
83 ret
= trace_test_buffer_cpu(tr
, cpu
);
88 arch_spin_unlock(&ftrace_max_lock
);
89 local_irq_restore(flags
);
97 static inline void warn_failed_init_tracer(struct tracer
*trace
, int init_ret
)
99 printk(KERN_WARNING
"Failed to init %s tracer, init returned %d\n",
100 trace
->name
, init_ret
);
102 #ifdef CONFIG_FUNCTION_TRACER
104 #ifdef CONFIG_DYNAMIC_FTRACE
106 /* Test dynamic code modification and ftrace filters */
107 int trace_selftest_startup_dynamic_tracing(struct tracer
*trace
,
108 struct trace_array
*tr
,
111 int save_ftrace_enabled
= ftrace_enabled
;
112 int save_tracer_enabled
= tracer_enabled
;
117 /* The ftrace test PASSED */
118 printk(KERN_CONT
"PASSED\n");
119 pr_info("Testing dynamic ftrace: ");
121 /* enable tracing, and record the filter function */
125 /* passed in by parameter to fool gcc from optimizing */
129 * Some archs *cough*PowerPC*cough* add characters to the
130 * start of the function names. We simply put a '*' to
133 func_name
= "*" __stringify(DYN_FTRACE_TEST_NAME
);
135 /* filter only on our function */
136 ftrace_set_filter(func_name
, strlen(func_name
), 1);
139 ret
= tracer_init(trace
, tr
);
141 warn_failed_init_tracer(trace
, ret
);
145 /* Sleep for a 1/10 of a second */
148 /* we should have nothing in the buffer */
149 ret
= trace_test_buffer(tr
, &count
);
155 printk(KERN_CONT
".. filter did not filter .. ");
159 /* call our function again */
165 /* stop the tracing. */
169 /* check the trace buffer */
170 ret
= trace_test_buffer(tr
, &count
);
174 /* we should only have one item */
175 if (!ret
&& count
!= 1) {
176 printk(KERN_CONT
".. filter failed count=%ld ..", count
);
182 ftrace_enabled
= save_ftrace_enabled
;
183 tracer_enabled
= save_tracer_enabled
;
185 /* Enable tracing on all functions again */
186 ftrace_set_filter(NULL
, 0, 1);
191 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
192 #endif /* CONFIG_DYNAMIC_FTRACE */
195 * Simple verification test of ftrace function tracer.
196 * Enable ftrace, sleep 1/10 second, and then read the trace
197 * buffer to see if all is in order.
200 trace_selftest_startup_function(struct tracer
*trace
, struct trace_array
*tr
)
202 int save_ftrace_enabled
= ftrace_enabled
;
203 int save_tracer_enabled
= tracer_enabled
;
207 /* make sure msleep has been recorded */
210 /* start the tracing */
214 ret
= tracer_init(trace
, tr
);
216 warn_failed_init_tracer(trace
, ret
);
220 /* Sleep for a 1/10 of a second */
222 /* stop the tracing. */
226 /* check the trace buffer */
227 ret
= trace_test_buffer(tr
, &count
);
231 if (!ret
&& !count
) {
232 printk(KERN_CONT
".. no entries found ..");
237 ret
= trace_selftest_startup_dynamic_tracing(trace
, tr
,
238 DYN_FTRACE_TEST_NAME
);
241 ftrace_enabled
= save_ftrace_enabled
;
242 tracer_enabled
= save_tracer_enabled
;
244 /* kill ftrace totally if we failed */
250 #endif /* CONFIG_FUNCTION_TRACER */
253 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
255 /* Maximum number of functions to trace before diagnosing a hang */
256 #define GRAPH_MAX_FUNC_TEST 100000000
258 static void __ftrace_dump(bool disable_tracing
);
259 static unsigned int graph_hang_thresh
;
261 /* Wrap the real function entry probe to avoid possible hanging */
262 static int trace_graph_entry_watchdog(struct ftrace_graph_ent
*trace
)
264 /* This is harmlessly racy, we want to approximately detect a hang */
265 if (unlikely(++graph_hang_thresh
> GRAPH_MAX_FUNC_TEST
)) {
267 printk(KERN_WARNING
"BUG: Function graph tracer hang!\n");
268 if (ftrace_dump_on_oops
)
269 __ftrace_dump(false);
273 return trace_graph_entry(trace
);
277 * Pretty much the same than for the function tracer from which the selftest
281 trace_selftest_startup_function_graph(struct tracer
*trace
,
282 struct trace_array
*tr
)
288 * Simulate the init() callback but we attach a watchdog callback
289 * to detect and recover from possible hangs
291 tracing_reset_online_cpus(tr
);
293 ret
= register_ftrace_graph(&trace_graph_return
,
294 &trace_graph_entry_watchdog
);
296 warn_failed_init_tracer(trace
, ret
);
299 tracing_start_cmdline_record();
301 /* Sleep for a 1/10 of a second */
304 /* Have we just recovered from a hang? */
305 if (graph_hang_thresh
> GRAPH_MAX_FUNC_TEST
) {
306 tracing_selftest_disabled
= true;
313 /* check the trace buffer */
314 ret
= trace_test_buffer(tr
, &count
);
319 if (!ret
&& !count
) {
320 printk(KERN_CONT
".. no entries found ..");
325 /* Don't test dynamic tracing, the function tracer already did */
328 /* Stop it if we failed */
334 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
337 #ifdef CONFIG_IRQSOFF_TRACER
339 trace_selftest_startup_irqsoff(struct tracer
*trace
, struct trace_array
*tr
)
341 unsigned long save_max
= tracing_max_latency
;
345 /* start the tracing */
346 ret
= tracer_init(trace
, tr
);
348 warn_failed_init_tracer(trace
, ret
);
352 /* reset the max latency */
353 tracing_max_latency
= 0;
354 /* disable interrupts for a bit */
360 * Stop the tracer to avoid a warning subsequent
361 * to buffer flipping failure because tracing_stop()
362 * disables the tr and max buffers, making flipping impossible
363 * in case of parallels max irqs off latencies.
366 /* stop the tracing. */
368 /* check both trace buffers */
369 ret
= trace_test_buffer(tr
, NULL
);
371 ret
= trace_test_buffer(&max_tr
, &count
);
375 if (!ret
&& !count
) {
376 printk(KERN_CONT
".. no entries found ..");
380 tracing_max_latency
= save_max
;
384 #endif /* CONFIG_IRQSOFF_TRACER */
386 #ifdef CONFIG_PREEMPT_TRACER
388 trace_selftest_startup_preemptoff(struct tracer
*trace
, struct trace_array
*tr
)
390 unsigned long save_max
= tracing_max_latency
;
395 * Now that the big kernel lock is no longer preemptable,
396 * and this is called with the BKL held, it will always
397 * fail. If preemption is already disabled, simply
398 * pass the test. When the BKL is removed, or becomes
399 * preemptible again, we will once again test this,
402 if (preempt_count()) {
403 printk(KERN_CONT
"can not test ... force ");
407 /* start the tracing */
408 ret
= tracer_init(trace
, tr
);
410 warn_failed_init_tracer(trace
, ret
);
414 /* reset the max latency */
415 tracing_max_latency
= 0;
416 /* disable preemption for a bit */
422 * Stop the tracer to avoid a warning subsequent
423 * to buffer flipping failure because tracing_stop()
424 * disables the tr and max buffers, making flipping impossible
425 * in case of parallels max preempt off latencies.
428 /* stop the tracing. */
430 /* check both trace buffers */
431 ret
= trace_test_buffer(tr
, NULL
);
433 ret
= trace_test_buffer(&max_tr
, &count
);
437 if (!ret
&& !count
) {
438 printk(KERN_CONT
".. no entries found ..");
442 tracing_max_latency
= save_max
;
446 #endif /* CONFIG_PREEMPT_TRACER */
448 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
450 trace_selftest_startup_preemptirqsoff(struct tracer
*trace
, struct trace_array
*tr
)
452 unsigned long save_max
= tracing_max_latency
;
457 * Now that the big kernel lock is no longer preemptable,
458 * and this is called with the BKL held, it will always
459 * fail. If preemption is already disabled, simply
460 * pass the test. When the BKL is removed, or becomes
461 * preemptible again, we will once again test this,
464 if (preempt_count()) {
465 printk(KERN_CONT
"can not test ... force ");
469 /* start the tracing */
470 ret
= tracer_init(trace
, tr
);
472 warn_failed_init_tracer(trace
, ret
);
476 /* reset the max latency */
477 tracing_max_latency
= 0;
479 /* disable preemption and interrupts for a bit */
484 /* reverse the order of preempt vs irqs */
488 * Stop the tracer to avoid a warning subsequent
489 * to buffer flipping failure because tracing_stop()
490 * disables the tr and max buffers, making flipping impossible
491 * in case of parallels max irqs/preempt off latencies.
494 /* stop the tracing. */
496 /* check both trace buffers */
497 ret
= trace_test_buffer(tr
, NULL
);
501 ret
= trace_test_buffer(&max_tr
, &count
);
505 if (!ret
&& !count
) {
506 printk(KERN_CONT
".. no entries found ..");
511 /* do the test by disabling interrupts first this time */
512 tracing_max_latency
= 0;
520 /* reverse the order of preempt vs irqs */
524 /* stop the tracing. */
526 /* check both trace buffers */
527 ret
= trace_test_buffer(tr
, NULL
);
531 ret
= trace_test_buffer(&max_tr
, &count
);
533 if (!ret
&& !count
) {
534 printk(KERN_CONT
".. no entries found ..");
543 tracing_max_latency
= save_max
;
547 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
549 #ifdef CONFIG_NOP_TRACER
551 trace_selftest_startup_nop(struct tracer
*trace
, struct trace_array
*tr
)
553 /* What could possibly go wrong? */
558 #ifdef CONFIG_SCHED_TRACER
559 static int trace_wakeup_test_thread(void *data
)
561 /* Make this a RT thread, doesn't need to be too high */
562 struct sched_param param
= { .sched_priority
= 5 };
563 struct completion
*x
= data
;
565 sched_setscheduler(current
, SCHED_FIFO
, ¶m
);
567 /* Make it know we have a new prio */
570 /* now go to sleep and let the test wake us up */
571 set_current_state(TASK_INTERRUPTIBLE
);
574 /* we are awake, now wait to disappear */
575 while (!kthread_should_stop()) {
577 * This is an RT task, do short sleeps to let
587 trace_selftest_startup_wakeup(struct tracer
*trace
, struct trace_array
*tr
)
589 unsigned long save_max
= tracing_max_latency
;
590 struct task_struct
*p
;
591 struct completion isrt
;
595 init_completion(&isrt
);
597 /* create a high prio thread */
598 p
= kthread_run(trace_wakeup_test_thread
, &isrt
, "ftrace-test");
600 printk(KERN_CONT
"Failed to create ftrace wakeup test thread ");
604 /* make sure the thread is running at an RT prio */
605 wait_for_completion(&isrt
);
607 /* start the tracing */
608 ret
= tracer_init(trace
, tr
);
610 warn_failed_init_tracer(trace
, ret
);
614 /* reset the max latency */
615 tracing_max_latency
= 0;
617 /* sleep to let the RT thread sleep too */
621 * Yes this is slightly racy. It is possible that for some
622 * strange reason that the RT thread we created, did not
623 * call schedule for 100ms after doing the completion,
624 * and we do a wakeup on a task that already is awake.
625 * But that is extremely unlikely, and the worst thing that
626 * happens in such a case, is that we disable tracing.
627 * Honestly, if this race does happen something is horrible
628 * wrong with the system.
633 /* give a little time to let the thread wake up */
636 /* stop the tracing. */
638 /* check both trace buffers */
639 ret
= trace_test_buffer(tr
, NULL
);
641 ret
= trace_test_buffer(&max_tr
, &count
);
647 tracing_max_latency
= save_max
;
649 /* kill the thread */
652 if (!ret
&& !count
) {
653 printk(KERN_CONT
".. no entries found ..");
659 #endif /* CONFIG_SCHED_TRACER */
661 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
663 trace_selftest_startup_sched_switch(struct tracer
*trace
, struct trace_array
*tr
)
668 /* start the tracing */
669 ret
= tracer_init(trace
, tr
);
671 warn_failed_init_tracer(trace
, ret
);
675 /* Sleep for a 1/10 of a second */
677 /* stop the tracing. */
679 /* check the trace buffer */
680 ret
= trace_test_buffer(tr
, &count
);
684 if (!ret
&& !count
) {
685 printk(KERN_CONT
".. no entries found ..");
691 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
693 #ifdef CONFIG_SYSPROF_TRACER
695 trace_selftest_startup_sysprof(struct tracer
*trace
, struct trace_array
*tr
)
700 /* start the tracing */
701 ret
= tracer_init(trace
, tr
);
703 warn_failed_init_tracer(trace
, ret
);
707 /* Sleep for a 1/10 of a second */
709 /* stop the tracing. */
711 /* check the trace buffer */
712 ret
= trace_test_buffer(tr
, &count
);
716 if (!ret
&& !count
) {
717 printk(KERN_CONT
".. no entries found ..");
723 #endif /* CONFIG_SYSPROF_TRACER */
725 #ifdef CONFIG_BRANCH_TRACER
727 trace_selftest_startup_branch(struct tracer
*trace
, struct trace_array
*tr
)
732 /* start the tracing */
733 ret
= tracer_init(trace
, tr
);
735 warn_failed_init_tracer(trace
, ret
);
739 /* Sleep for a 1/10 of a second */
741 /* stop the tracing. */
743 /* check the trace buffer */
744 ret
= trace_test_buffer(tr
, &count
);
748 if (!ret
&& !count
) {
749 printk(KERN_CONT
".. no entries found ..");
755 #endif /* CONFIG_BRANCH_TRACER */
757 #ifdef CONFIG_HW_BRANCH_TRACER
759 trace_selftest_startup_hw_branches(struct tracer
*trace
,
760 struct trace_array
*tr
)
762 struct trace_iterator
*iter
;
763 struct tracer tracer
;
768 printk(KERN_CONT
"missing open function...");
772 ret
= tracer_init(trace
, tr
);
774 warn_failed_init_tracer(trace
, ret
);
779 * The hw-branch tracer needs to collect the trace from the various
780 * cpu trace buffers - before tracing is stopped.
782 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
786 memcpy(&tracer
, trace
, sizeof(tracer
));
788 iter
->trace
= &tracer
;
791 mutex_init(&iter
->mutex
);
795 mutex_destroy(&iter
->mutex
);
800 ret
= trace_test_buffer(tr
, &count
);
804 if (!ret
&& !count
) {
805 printk(KERN_CONT
"no entries found..");
811 #endif /* CONFIG_HW_BRANCH_TRACER */
813 #ifdef CONFIG_KSYM_TRACER
814 static int ksym_selftest_dummy
;
817 trace_selftest_startup_ksym(struct tracer
*trace
, struct trace_array
*tr
)
822 /* start the tracing */
823 ret
= tracer_init(trace
, tr
);
825 warn_failed_init_tracer(trace
, ret
);
829 ksym_selftest_dummy
= 0;
830 /* Register the read-write tracing request */
832 ret
= process_new_ksym_entry("ksym_selftest_dummy",
833 HW_BREAKPOINT_R
| HW_BREAKPOINT_W
,
834 (unsigned long)(&ksym_selftest_dummy
));
837 printk(KERN_CONT
"ksym_trace read-write startup test failed\n");
840 /* Perform a read and a write operation over the dummy variable to
843 if (ksym_selftest_dummy
== 0)
844 ksym_selftest_dummy
++;
846 /* stop the tracing. */
848 /* check the trace buffer */
849 ret
= trace_test_buffer(tr
, &count
);
853 /* read & write operations - one each is performed on the dummy variable
854 * triggering two entries in the trace buffer
856 if (!ret
&& count
!= 2) {
857 printk(KERN_CONT
"Ksym tracer startup test failed");
864 #endif /* CONFIG_KSYM_TRACER */