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:
25 static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
27 struct ring_buffer_event *event;
28 struct trace_entry *entry;
29 unsigned int loops = 0;
31 while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
32 entry = ring_buffer_event_data(event);
35 * The ring buffer is a size of trace_buf_size, if
36 * we loop more than the size, there's something wrong
37 * with the ring buffer.
39 if (loops++ > trace_buf_size) {
40 printk(KERN_CONT ".. bad ring buffer ");
43 if (!trace_valid_entry(entry)) {
44 printk(KERN_CONT ".. invalid entry %d ",
54 printk(KERN_CONT ".. corrupted trace buffer .. ");
59 * Test the trace buffer to see if all the elements
62 static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
64 unsigned long flags, cnt = 0;
67 /* Don't allow flipping of max traces now */
68 local_irq_save(flags);
69 __raw_spin_lock(&ftrace_max_lock);
71 cnt = ring_buffer_entries(tr->buffer);
74 * The trace_test_buffer_cpu runs a while loop to consume all data.
75 * If the calling tracer is broken, and is constantly filling
76 * the buffer, this will run forever, and hard lock the box.
77 * We disable the ring buffer while we do this test to prevent
81 for_each_possible_cpu(cpu) {
82 ret = trace_test_buffer_cpu(tr, cpu);
87 __raw_spin_unlock(&ftrace_max_lock);
88 local_irq_restore(flags);
96 static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
98 printk(KERN_WARNING "Failed to init %s tracer, init returned %d\n",
99 trace->name, init_ret);
101 #ifdef CONFIG_FUNCTION_TRACER
103 #ifdef CONFIG_DYNAMIC_FTRACE
105 /* Test dynamic code modification and ftrace filters */
106 int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
107 struct trace_array *tr,
110 int save_ftrace_enabled = ftrace_enabled;
111 int save_tracer_enabled = tracer_enabled;
116 /* The ftrace test PASSED */
117 printk(KERN_CONT "PASSED\n");
118 pr_info("Testing dynamic ftrace: ");
120 /* enable tracing, and record the filter function */
124 /* passed in by parameter to fool gcc from optimizing */
128 * Some archs *cough*PowerPC*cough* add characters to the
129 * start of the function names. We simply put a '*' to
132 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
134 /* filter only on our function */
135 ftrace_set_filter(func_name, strlen(func_name), 1);
138 ret = tracer_init(trace, tr);
140 warn_failed_init_tracer(trace, ret);
144 /* Sleep for a 1/10 of a second */
147 /* we should have nothing in the buffer */
148 ret = trace_test_buffer(tr, &count);
154 printk(KERN_CONT ".. filter did not filter .. ");
158 /* call our function again */
164 /* stop the tracing. */
168 /* check the trace buffer */
169 ret = trace_test_buffer(tr, &count);
173 /* we should only have one item */
174 if (!ret && count != 1) {
175 printk(KERN_CONT ".. filter failed count=%ld ..", count);
181 ftrace_enabled = save_ftrace_enabled;
182 tracer_enabled = save_tracer_enabled;
184 /* Enable tracing on all functions again */
185 ftrace_set_filter(NULL, 0, 1);
190 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
191 #endif /* CONFIG_DYNAMIC_FTRACE */
194 * Simple verification test of ftrace function tracer.
195 * Enable ftrace, sleep 1/10 second, and then read the trace
196 * buffer to see if all is in order.
199 trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
201 int save_ftrace_enabled = ftrace_enabled;
202 int save_tracer_enabled = tracer_enabled;
206 /* make sure msleep has been recorded */
209 /* start the tracing */
213 ret = tracer_init(trace, tr);
215 warn_failed_init_tracer(trace, ret);
219 /* Sleep for a 1/10 of a second */
221 /* stop the tracing. */
225 /* check the trace buffer */
226 ret = trace_test_buffer(tr, &count);
230 if (!ret && !count) {
231 printk(KERN_CONT ".. no entries found ..");
236 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
237 DYN_FTRACE_TEST_NAME);
240 ftrace_enabled = save_ftrace_enabled;
241 tracer_enabled = save_tracer_enabled;
243 /* kill ftrace totally if we failed */
249 #endif /* CONFIG_FUNCTION_TRACER */
252 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
254 /* Maximum number of functions to trace before diagnosing a hang */
255 #define GRAPH_MAX_FUNC_TEST 100000000
257 static void __ftrace_dump(bool disable_tracing);
258 static unsigned int graph_hang_thresh;
260 /* Wrap the real function entry probe to avoid possible hanging */
261 static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
263 /* This is harmlessly racy, we want to approximately detect a hang */
264 if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
266 printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
267 if (ftrace_dump_on_oops)
268 __ftrace_dump(false);
272 return trace_graph_entry(trace);
276 * Pretty much the same than for the function tracer from which the selftest
280 trace_selftest_startup_function_graph(struct tracer *trace,
281 struct trace_array *tr)
287 * Simulate the init() callback but we attach a watchdog callback
288 * to detect and recover from possible hangs
290 tracing_reset_online_cpus(tr);
291 ret = register_ftrace_graph(&trace_graph_return,
292 &trace_graph_entry_watchdog);
294 warn_failed_init_tracer(trace, ret);
297 tracing_start_cmdline_record();
299 /* Sleep for a 1/10 of a second */
302 /* Have we just recovered from a hang? */
303 if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
304 tracing_selftest_disabled = true;
311 /* check the trace buffer */
312 ret = trace_test_buffer(tr, &count);
317 if (!ret && !count) {
318 printk(KERN_CONT ".. no entries found ..");
323 /* Don't test dynamic tracing, the function tracer already did */
326 /* Stop it if we failed */
332 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
335 #ifdef CONFIG_IRQSOFF_TRACER
337 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
339 unsigned long save_max = tracing_max_latency;
343 /* start the tracing */
344 ret = tracer_init(trace, tr);
346 warn_failed_init_tracer(trace, ret);
350 /* reset the max latency */
351 tracing_max_latency = 0;
352 /* disable interrupts for a bit */
358 * Stop the tracer to avoid a warning subsequent
359 * to buffer flipping failure because tracing_stop()
360 * disables the tr and max buffers, making flipping impossible
361 * in case of parallels max irqs off latencies.
364 /* stop the tracing. */
366 /* check both trace buffers */
367 ret = trace_test_buffer(tr, NULL);
369 ret = trace_test_buffer(&max_tr, &count);
373 if (!ret && !count) {
374 printk(KERN_CONT ".. no entries found ..");
378 tracing_max_latency = save_max;
382 #endif /* CONFIG_IRQSOFF_TRACER */
384 #ifdef CONFIG_PREEMPT_TRACER
386 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
388 unsigned long save_max = tracing_max_latency;
393 * Now that the big kernel lock is no longer preemptable,
394 * and this is called with the BKL held, it will always
395 * fail. If preemption is already disabled, simply
396 * pass the test. When the BKL is removed, or becomes
397 * preemptible again, we will once again test this,
400 if (preempt_count()) {
401 printk(KERN_CONT "can not test ... force ");
405 /* start the tracing */
406 ret = tracer_init(trace, tr);
408 warn_failed_init_tracer(trace, ret);
412 /* reset the max latency */
413 tracing_max_latency = 0;
414 /* disable preemption for a bit */
420 * Stop the tracer to avoid a warning subsequent
421 * to buffer flipping failure because tracing_stop()
422 * disables the tr and max buffers, making flipping impossible
423 * in case of parallels max preempt off latencies.
426 /* stop the tracing. */
428 /* check both trace buffers */
429 ret = trace_test_buffer(tr, NULL);
431 ret = trace_test_buffer(&max_tr, &count);
435 if (!ret && !count) {
436 printk(KERN_CONT ".. no entries found ..");
440 tracing_max_latency = save_max;
444 #endif /* CONFIG_PREEMPT_TRACER */
446 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
448 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
450 unsigned long save_max = tracing_max_latency;
455 * Now that the big kernel lock is no longer preemptable,
456 * and this is called with the BKL held, it will always
457 * fail. If preemption is already disabled, simply
458 * pass the test. When the BKL is removed, or becomes
459 * preemptible again, we will once again test this,
462 if (preempt_count()) {
463 printk(KERN_CONT "can not test ... force ");
467 /* start the tracing */
468 ret = tracer_init(trace, tr);
470 warn_failed_init_tracer(trace, ret);
474 /* reset the max latency */
475 tracing_max_latency = 0;
477 /* disable preemption and interrupts for a bit */
482 /* reverse the order of preempt vs irqs */
486 * Stop the tracer to avoid a warning subsequent
487 * to buffer flipping failure because tracing_stop()
488 * disables the tr and max buffers, making flipping impossible
489 * in case of parallels max irqs/preempt off latencies.
492 /* stop the tracing. */
494 /* check both trace buffers */
495 ret = trace_test_buffer(tr, NULL);
499 ret = trace_test_buffer(&max_tr, &count);
503 if (!ret && !count) {
504 printk(KERN_CONT ".. no entries found ..");
509 /* do the test by disabling interrupts first this time */
510 tracing_max_latency = 0;
518 /* reverse the order of preempt vs irqs */
522 /* stop the tracing. */
524 /* check both trace buffers */
525 ret = trace_test_buffer(tr, NULL);
529 ret = trace_test_buffer(&max_tr, &count);
531 if (!ret && !count) {
532 printk(KERN_CONT ".. no entries found ..");
541 tracing_max_latency = save_max;
545 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
547 #ifdef CONFIG_NOP_TRACER
549 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
551 /* What could possibly go wrong? */
556 #ifdef CONFIG_SCHED_TRACER
557 static int trace_wakeup_test_thread(void *data)
559 /* Make this a RT thread, doesn't need to be too high */
560 struct sched_param param = { .sched_priority = 5 };
561 struct completion *x = data;
563 sched_setscheduler(current, SCHED_FIFO, ¶m);
565 /* Make it know we have a new prio */
568 /* now go to sleep and let the test wake us up */
569 set_current_state(TASK_INTERRUPTIBLE);
572 /* we are awake, now wait to disappear */
573 while (!kthread_should_stop()) {
575 * This is an RT task, do short sleeps to let
585 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
587 unsigned long save_max = tracing_max_latency;
588 struct task_struct *p;
589 struct completion isrt;
593 init_completion(&isrt);
595 /* create a high prio thread */
596 p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
598 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
602 /* make sure the thread is running at an RT prio */
603 wait_for_completion(&isrt);
605 /* start the tracing */
606 ret = tracer_init(trace, tr);
608 warn_failed_init_tracer(trace, ret);
612 /* reset the max latency */
613 tracing_max_latency = 0;
615 /* sleep to let the RT thread sleep too */
619 * Yes this is slightly racy. It is possible that for some
620 * strange reason that the RT thread we created, did not
621 * call schedule for 100ms after doing the completion,
622 * and we do a wakeup on a task that already is awake.
623 * But that is extremely unlikely, and the worst thing that
624 * happens in such a case, is that we disable tracing.
625 * Honestly, if this race does happen something is horrible
626 * wrong with the system.
631 /* give a little time to let the thread wake up */
634 /* stop the tracing. */
636 /* check both trace buffers */
637 ret = trace_test_buffer(tr, NULL);
639 ret = trace_test_buffer(&max_tr, &count);
645 tracing_max_latency = save_max;
647 /* kill the thread */
650 if (!ret && !count) {
651 printk(KERN_CONT ".. no entries found ..");
657 #endif /* CONFIG_SCHED_TRACER */
659 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
661 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
666 /* start the tracing */
667 ret = tracer_init(trace, tr);
669 warn_failed_init_tracer(trace, ret);
673 /* Sleep for a 1/10 of a second */
675 /* stop the tracing. */
677 /* check the trace buffer */
678 ret = trace_test_buffer(tr, &count);
682 if (!ret && !count) {
683 printk(KERN_CONT ".. no entries found ..");
689 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
691 #ifdef CONFIG_SYSPROF_TRACER
693 trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
698 /* start the tracing */
699 ret = tracer_init(trace, tr);
701 warn_failed_init_tracer(trace, ret);
705 /* Sleep for a 1/10 of a second */
707 /* stop the tracing. */
709 /* check the trace buffer */
710 ret = trace_test_buffer(tr, &count);
714 if (!ret && !count) {
715 printk(KERN_CONT ".. no entries found ..");
721 #endif /* CONFIG_SYSPROF_TRACER */
723 #ifdef CONFIG_BRANCH_TRACER
725 trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
730 /* start the tracing */
731 ret = tracer_init(trace, tr);
733 warn_failed_init_tracer(trace, ret);
737 /* Sleep for a 1/10 of a second */
739 /* stop the tracing. */
741 /* check the trace buffer */
742 ret = trace_test_buffer(tr, &count);
746 if (!ret && !count) {
747 printk(KERN_CONT ".. no entries found ..");
753 #endif /* CONFIG_BRANCH_TRACER */
755 #ifdef CONFIG_HW_BRANCH_TRACER
757 trace_selftest_startup_hw_branches(struct tracer *trace,
758 struct trace_array *tr)
760 struct trace_iterator *iter;
761 struct tracer tracer;
766 printk(KERN_CONT "missing open function...");
770 ret = tracer_init(trace, tr);
772 warn_failed_init_tracer(trace, ret);
777 * The hw-branch tracer needs to collect the trace from the various
778 * cpu trace buffers - before tracing is stopped.
780 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
784 memcpy(&tracer, trace, sizeof(tracer));
786 iter->trace = &tracer;
789 mutex_init(&iter->mutex);
793 mutex_destroy(&iter->mutex);
798 ret = trace_test_buffer(tr, &count);
802 if (!ret && !count) {
803 printk(KERN_CONT "no entries found..");
809 #endif /* CONFIG_HW_BRANCH_TRACER */