blob: 593f52b73551c2406b244e2dd772eba6d6d247b3 [file] [log] [blame]
Steven Rostedt60a11772008-05-12 21:20:44 +02001/* Include in trace.c */
2
Steven Rostedt9cc26a22009-03-09 16:00:22 -04003#include <linux/stringify.h>
Steven Rostedt60a11772008-05-12 21:20:44 +02004#include <linux/kthread.h>
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Steven Rostedt60a11772008-05-12 21:20:44 +02007
Ingo Molnare309b412008-05-12 21:20:51 +02008static inline int trace_valid_entry(struct trace_entry *entry)
Steven Rostedt60a11772008-05-12 21:20:44 +02009{
10 switch (entry->type) {
11 case TRACE_FN:
12 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +020013 case TRACE_WAKE:
Steven Rostedt06fa75a2008-05-12 21:20:54 +020014 case TRACE_STACK:
Steven Rostedtdd0e5452008-08-01 12:26:41 -040015 case TRACE_PRINT:
Steven Rostedt80e5ea42008-11-12 15:24:24 -050016 case TRACE_BRANCH:
Frederic Weisbecker7447dce2009-02-07 21:33:57 +010017 case TRACE_GRAPH_ENT:
18 case TRACE_GRAPH_RET:
Steven Rostedt60a11772008-05-12 21:20:44 +020019 return 1;
20 }
21 return 0;
22}
23
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050024static int trace_test_buffer_cpu(struct trace_buffer *buf, int cpu)
Steven Rostedt60a11772008-05-12 21:20:44 +020025{
Steven Rostedt3928a8a2008-09-29 23:02:41 -040026 struct ring_buffer_event *event;
27 struct trace_entry *entry;
Steven Rostedt4b3e3d22009-02-18 22:50:01 -050028 unsigned int loops = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +020029
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050030 while ((event = ring_buffer_consume(buf->buffer, cpu, NULL, NULL))) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -040031 entry = ring_buffer_event_data(event);
Steven Rostedt60a11772008-05-12 21:20:44 +020032
Steven Rostedt4b3e3d22009-02-18 22:50:01 -050033 /*
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.
37 */
38 if (loops++ > trace_buf_size) {
39 printk(KERN_CONT ".. bad ring buffer ");
40 goto failed;
41 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -040042 if (!trace_valid_entry(entry)) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +020043 printk(KERN_CONT ".. invalid entry %d ",
Steven Rostedt3928a8a2008-09-29 23:02:41 -040044 entry->type);
Steven Rostedt60a11772008-05-12 21:20:44 +020045 goto failed;
46 }
Steven Rostedt60a11772008-05-12 21:20:44 +020047 }
Steven Rostedt60a11772008-05-12 21:20:44 +020048 return 0;
49
50 failed:
Steven Rostedt08bafa02008-05-12 21:20:45 +020051 /* disable tracing */
52 tracing_disabled = 1;
Steven Rostedt60a11772008-05-12 21:20:44 +020053 printk(KERN_CONT ".. corrupted trace buffer .. ");
54 return -1;
55}
56
57/*
58 * Test the trace buffer to see if all the elements
59 * are still sane.
60 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050061static int trace_test_buffer(struct trace_buffer *buf, unsigned long *count)
Steven Rostedt60a11772008-05-12 21:20:44 +020062{
Steven Rostedt30afdcb2008-05-12 21:20:56 +020063 unsigned long flags, cnt = 0;
64 int cpu, ret = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +020065
Steven Rostedt30afdcb2008-05-12 21:20:56 +020066 /* Don't allow flipping of max traces now */
Steven Rostedtd51ad7a2008-11-15 15:48:29 -050067 local_irq_save(flags);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -050068 arch_spin_lock(&buf->tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -040069
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050070 cnt = ring_buffer_entries(buf->buffer);
Steven Rostedt3928a8a2008-09-29 23:02:41 -040071
Steven Rostedt0c5119c2009-02-18 18:33:57 -050072 /*
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
77 * a hard lock up.
78 */
79 tracing_off();
Steven Rostedt60a11772008-05-12 21:20:44 +020080 for_each_possible_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050081 ret = trace_test_buffer_cpu(buf, cpu);
Steven Rostedt60a11772008-05-12 21:20:44 +020082 if (ret)
83 break;
84 }
Steven Rostedt0c5119c2009-02-18 18:33:57 -050085 tracing_on();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -050086 arch_spin_unlock(&buf->tr->max_lock);
Steven Rostedtd51ad7a2008-11-15 15:48:29 -050087 local_irq_restore(flags);
Steven Rostedt60a11772008-05-12 21:20:44 +020088
89 if (count)
90 *count = cnt;
91
92 return ret;
93}
94
Frederic Weisbecker1c800252008-11-16 05:57:26 +010095static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
96{
97 printk(KERN_WARNING "Failed to init %s tracer, init returned %d\n",
98 trace->name, init_ret);
99}
Steven Rostedt606576c2008-10-06 19:06:12 -0400100#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt77a2b372008-05-12 21:20:45 +0200101
102#ifdef CONFIG_DYNAMIC_FTRACE
103
Steven Rostedt95950c22011-05-06 00:08:51 -0400104static int trace_selftest_test_probe1_cnt;
105static void trace_selftest_test_probe1_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400106 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400107 struct ftrace_ops *op,
108 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400109{
110 trace_selftest_test_probe1_cnt++;
111}
112
113static int trace_selftest_test_probe2_cnt;
114static void trace_selftest_test_probe2_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400115 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400116 struct ftrace_ops *op,
117 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400118{
119 trace_selftest_test_probe2_cnt++;
120}
121
122static int trace_selftest_test_probe3_cnt;
123static void trace_selftest_test_probe3_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400124 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400125 struct ftrace_ops *op,
126 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400127{
128 trace_selftest_test_probe3_cnt++;
129}
130
131static int trace_selftest_test_global_cnt;
132static void trace_selftest_test_global_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400133 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400134 struct ftrace_ops *op,
135 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400136{
137 trace_selftest_test_global_cnt++;
138}
139
140static int trace_selftest_test_dyn_cnt;
141static void trace_selftest_test_dyn_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400142 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400143 struct ftrace_ops *op,
144 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400145{
146 trace_selftest_test_dyn_cnt++;
147}
148
149static struct ftrace_ops test_probe1 = {
150 .func = trace_selftest_test_probe1_func,
Steven Rostedt47409742012-07-20 11:04:44 -0400151 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt95950c22011-05-06 00:08:51 -0400152};
153
154static struct ftrace_ops test_probe2 = {
155 .func = trace_selftest_test_probe2_func,
Steven Rostedt47409742012-07-20 11:04:44 -0400156 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt95950c22011-05-06 00:08:51 -0400157};
158
159static struct ftrace_ops test_probe3 = {
160 .func = trace_selftest_test_probe3_func,
Steven Rostedt47409742012-07-20 11:04:44 -0400161 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt95950c22011-05-06 00:08:51 -0400162};
163
Steven Rostedt95950c22011-05-06 00:08:51 -0400164static void print_counts(void)
165{
166 printk("(%d %d %d %d %d) ",
167 trace_selftest_test_probe1_cnt,
168 trace_selftest_test_probe2_cnt,
169 trace_selftest_test_probe3_cnt,
170 trace_selftest_test_global_cnt,
171 trace_selftest_test_dyn_cnt);
172}
173
174static void reset_counts(void)
175{
176 trace_selftest_test_probe1_cnt = 0;
177 trace_selftest_test_probe2_cnt = 0;
178 trace_selftest_test_probe3_cnt = 0;
179 trace_selftest_test_global_cnt = 0;
180 trace_selftest_test_dyn_cnt = 0;
181}
182
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500183static int trace_selftest_ops(struct trace_array *tr, int cnt)
Steven Rostedt95950c22011-05-06 00:08:51 -0400184{
185 int save_ftrace_enabled = ftrace_enabled;
186 struct ftrace_ops *dyn_ops;
187 char *func1_name;
188 char *func2_name;
189 int len1;
190 int len2;
191 int ret = -1;
192
193 printk(KERN_CONT "PASSED\n");
194 pr_info("Testing dynamic ftrace ops #%d: ", cnt);
195
196 ftrace_enabled = 1;
197 reset_counts();
198
199 /* Handle PPC64 '.' name */
200 func1_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
201 func2_name = "*" __stringify(DYN_FTRACE_TEST_NAME2);
202 len1 = strlen(func1_name);
203 len2 = strlen(func2_name);
204
205 /*
206 * Probe 1 will trace function 1.
207 * Probe 2 will trace function 2.
208 * Probe 3 will trace functions 1 and 2.
209 */
210 ftrace_set_filter(&test_probe1, func1_name, len1, 1);
211 ftrace_set_filter(&test_probe2, func2_name, len2, 1);
212 ftrace_set_filter(&test_probe3, func1_name, len1, 1);
213 ftrace_set_filter(&test_probe3, func2_name, len2, 0);
214
215 register_ftrace_function(&test_probe1);
216 register_ftrace_function(&test_probe2);
217 register_ftrace_function(&test_probe3);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500218 /* First time we are running with main function */
219 if (cnt > 1) {
220 ftrace_init_array_ops(tr, trace_selftest_test_global_func);
221 register_ftrace_function(tr->ops);
222 }
Steven Rostedt95950c22011-05-06 00:08:51 -0400223
224 DYN_FTRACE_TEST_NAME();
225
226 print_counts();
227
228 if (trace_selftest_test_probe1_cnt != 1)
229 goto out;
230 if (trace_selftest_test_probe2_cnt != 0)
231 goto out;
232 if (trace_selftest_test_probe3_cnt != 1)
233 goto out;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500234 if (cnt > 1) {
235 if (trace_selftest_test_global_cnt == 0)
236 goto out;
237 }
Steven Rostedt95950c22011-05-06 00:08:51 -0400238
239 DYN_FTRACE_TEST_NAME2();
240
241 print_counts();
242
243 if (trace_selftest_test_probe1_cnt != 1)
244 goto out;
245 if (trace_selftest_test_probe2_cnt != 1)
246 goto out;
247 if (trace_selftest_test_probe3_cnt != 2)
248 goto out;
249
250 /* Add a dynamic probe */
251 dyn_ops = kzalloc(sizeof(*dyn_ops), GFP_KERNEL);
252 if (!dyn_ops) {
253 printk("MEMORY ERROR ");
254 goto out;
255 }
256
257 dyn_ops->func = trace_selftest_test_dyn_func;
258
259 register_ftrace_function(dyn_ops);
260
261 trace_selftest_test_global_cnt = 0;
262
263 DYN_FTRACE_TEST_NAME();
264
265 print_counts();
266
267 if (trace_selftest_test_probe1_cnt != 2)
268 goto out_free;
269 if (trace_selftest_test_probe2_cnt != 1)
270 goto out_free;
271 if (trace_selftest_test_probe3_cnt != 3)
272 goto out_free;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500273 if (cnt > 1) {
274 if (trace_selftest_test_global_cnt == 0)
275 goto out;
276 }
Steven Rostedt95950c22011-05-06 00:08:51 -0400277 if (trace_selftest_test_dyn_cnt == 0)
278 goto out_free;
279
280 DYN_FTRACE_TEST_NAME2();
281
282 print_counts();
283
284 if (trace_selftest_test_probe1_cnt != 2)
285 goto out_free;
286 if (trace_selftest_test_probe2_cnt != 2)
287 goto out_free;
288 if (trace_selftest_test_probe3_cnt != 4)
289 goto out_free;
290
291 ret = 0;
292 out_free:
293 unregister_ftrace_function(dyn_ops);
294 kfree(dyn_ops);
295
296 out:
297 /* Purposely unregister in the same order */
298 unregister_ftrace_function(&test_probe1);
299 unregister_ftrace_function(&test_probe2);
300 unregister_ftrace_function(&test_probe3);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500301 if (cnt > 1)
302 unregister_ftrace_function(tr->ops);
303 ftrace_reset_array_ops(tr);
Steven Rostedt95950c22011-05-06 00:08:51 -0400304
305 /* Make sure everything is off */
306 reset_counts();
307 DYN_FTRACE_TEST_NAME();
308 DYN_FTRACE_TEST_NAME();
309
310 if (trace_selftest_test_probe1_cnt ||
311 trace_selftest_test_probe2_cnt ||
312 trace_selftest_test_probe3_cnt ||
313 trace_selftest_test_global_cnt ||
314 trace_selftest_test_dyn_cnt)
315 ret = -1;
316
317 ftrace_enabled = save_ftrace_enabled;
318
319 return ret;
320}
321
Steven Rostedt77a2b372008-05-12 21:20:45 +0200322/* Test dynamic code modification and ftrace filters */
Fabian Frederickad1438a2014-04-17 21:44:42 +0200323static int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
324 struct trace_array *tr,
325 int (*func)(void))
Steven Rostedt77a2b372008-05-12 21:20:45 +0200326{
Steven Rostedt77a2b372008-05-12 21:20:45 +0200327 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400328 unsigned long count;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400329 char *func_name;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400330 int ret;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200331
332 /* The ftrace test PASSED */
333 printk(KERN_CONT "PASSED\n");
334 pr_info("Testing dynamic ftrace: ");
335
336 /* enable tracing, and record the filter function */
337 ftrace_enabled = 1;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200338
339 /* passed in by parameter to fool gcc from optimizing */
340 func();
341
Steven Rostedt4e491d12008-05-14 23:49:44 -0400342 /*
Wenji Huang73d8b8b2009-02-17 01:10:02 -0500343 * Some archs *cough*PowerPC*cough* add characters to the
Steven Rostedt4e491d12008-05-14 23:49:44 -0400344 * start of the function names. We simply put a '*' to
Wenji Huang73d8b8b2009-02-17 01:10:02 -0500345 * accommodate them.
Steven Rostedt4e491d12008-05-14 23:49:44 -0400346 */
Steven Rostedt9cc26a22009-03-09 16:00:22 -0400347 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400348
Steven Rostedt77a2b372008-05-12 21:20:45 +0200349 /* filter only on our function */
Steven Rostedt936e0742011-05-05 22:54:01 -0400350 ftrace_set_global_filter(func_name, strlen(func_name), 1);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200351
352 /* enable tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200353 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100354 if (ret) {
355 warn_failed_init_tracer(trace, ret);
356 goto out;
357 }
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400358
Steven Rostedt77a2b372008-05-12 21:20:45 +0200359 /* Sleep for a 1/10 of a second */
360 msleep(100);
361
362 /* we should have nothing in the buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500363 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200364 if (ret)
365 goto out;
366
367 if (count) {
368 ret = -1;
369 printk(KERN_CONT ".. filter did not filter .. ");
370 goto out;
371 }
372
373 /* call our function again */
374 func();
375
376 /* sleep again */
377 msleep(100);
378
379 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500380 tracing_stop();
Steven Rostedt77a2b372008-05-12 21:20:45 +0200381 ftrace_enabled = 0;
382
383 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500384 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500385 tracing_start();
Steven Rostedt77a2b372008-05-12 21:20:45 +0200386
387 /* we should only have one item */
388 if (!ret && count != 1) {
Steven Rostedt95950c22011-05-06 00:08:51 -0400389 trace->reset(tr);
Steven Rostedt06fa75a2008-05-12 21:20:54 +0200390 printk(KERN_CONT ".. filter failed count=%ld ..", count);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200391 ret = -1;
392 goto out;
393 }
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500394
Steven Rostedt95950c22011-05-06 00:08:51 -0400395 /* Test the ops with global tracing running */
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500396 ret = trace_selftest_ops(tr, 1);
Steven Rostedt95950c22011-05-06 00:08:51 -0400397 trace->reset(tr);
398
Steven Rostedt77a2b372008-05-12 21:20:45 +0200399 out:
400 ftrace_enabled = save_ftrace_enabled;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200401
402 /* Enable tracing on all functions again */
Steven Rostedt936e0742011-05-05 22:54:01 -0400403 ftrace_set_global_filter(NULL, 0, 1);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200404
Steven Rostedt95950c22011-05-06 00:08:51 -0400405 /* Test the ops with global tracing off */
406 if (!ret)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500407 ret = trace_selftest_ops(tr, 2);
Steven Rostedt95950c22011-05-06 00:08:51 -0400408
Steven Rostedt77a2b372008-05-12 21:20:45 +0200409 return ret;
410}
Steven Rostedtea701f12012-07-20 13:08:05 -0400411
412static int trace_selftest_recursion_cnt;
413static void trace_selftest_test_recursion_func(unsigned long ip,
414 unsigned long pip,
415 struct ftrace_ops *op,
416 struct pt_regs *pt_regs)
417{
418 /*
419 * This function is registered without the recursion safe flag.
420 * The ftrace infrastructure should provide the recursion
421 * protection. If not, this will crash the kernel!
422 */
Steven Rostedt96403882012-11-02 17:01:20 -0400423 if (trace_selftest_recursion_cnt++ > 10)
424 return;
Steven Rostedtea701f12012-07-20 13:08:05 -0400425 DYN_FTRACE_TEST_NAME();
426}
427
428static void trace_selftest_test_recursion_safe_func(unsigned long ip,
429 unsigned long pip,
430 struct ftrace_ops *op,
431 struct pt_regs *pt_regs)
432{
433 /*
434 * We said we would provide our own recursion. By calling
435 * this function again, we should recurse back into this function
436 * and count again. But this only happens if the arch supports
437 * all of ftrace features and nothing else is using the function
438 * tracing utility.
439 */
440 if (trace_selftest_recursion_cnt++)
441 return;
442 DYN_FTRACE_TEST_NAME();
443}
444
445static struct ftrace_ops test_rec_probe = {
446 .func = trace_selftest_test_recursion_func,
447};
448
449static struct ftrace_ops test_recsafe_probe = {
450 .func = trace_selftest_test_recursion_safe_func,
451 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
452};
453
454static int
455trace_selftest_function_recursion(void)
456{
457 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtea701f12012-07-20 13:08:05 -0400458 char *func_name;
459 int len;
460 int ret;
Steven Rostedtea701f12012-07-20 13:08:05 -0400461
462 /* The previous test PASSED */
463 pr_cont("PASSED\n");
464 pr_info("Testing ftrace recursion: ");
465
466
467 /* enable tracing, and record the filter function */
468 ftrace_enabled = 1;
Steven Rostedtea701f12012-07-20 13:08:05 -0400469
470 /* Handle PPC64 '.' name */
471 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
472 len = strlen(func_name);
473
474 ret = ftrace_set_filter(&test_rec_probe, func_name, len, 1);
475 if (ret) {
476 pr_cont("*Could not set filter* ");
477 goto out;
478 }
479
480 ret = register_ftrace_function(&test_rec_probe);
481 if (ret) {
482 pr_cont("*could not register callback* ");
483 goto out;
484 }
485
486 DYN_FTRACE_TEST_NAME();
487
488 unregister_ftrace_function(&test_rec_probe);
489
490 ret = -1;
491 if (trace_selftest_recursion_cnt != 1) {
492 pr_cont("*callback not called once (%d)* ",
493 trace_selftest_recursion_cnt);
494 goto out;
495 }
496
497 trace_selftest_recursion_cnt = 1;
498
499 pr_cont("PASSED\n");
500 pr_info("Testing ftrace recursion safe: ");
501
502 ret = ftrace_set_filter(&test_recsafe_probe, func_name, len, 1);
503 if (ret) {
504 pr_cont("*Could not set filter* ");
505 goto out;
506 }
507
508 ret = register_ftrace_function(&test_recsafe_probe);
509 if (ret) {
510 pr_cont("*could not register callback* ");
511 goto out;
512 }
513
514 DYN_FTRACE_TEST_NAME();
515
516 unregister_ftrace_function(&test_recsafe_probe);
517
Steven Rostedtea701f12012-07-20 13:08:05 -0400518 ret = -1;
Steven Rostedt05cbbf62013-01-22 23:35:11 -0500519 if (trace_selftest_recursion_cnt != 2) {
520 pr_cont("*callback not called expected 2 times (%d)* ",
521 trace_selftest_recursion_cnt);
Steven Rostedtea701f12012-07-20 13:08:05 -0400522 goto out;
523 }
524
525 ret = 0;
526out:
527 ftrace_enabled = save_ftrace_enabled;
Steven Rostedtea701f12012-07-20 13:08:05 -0400528
529 return ret;
530}
Steven Rostedt77a2b372008-05-12 21:20:45 +0200531#else
532# define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
Steven Rostedtea701f12012-07-20 13:08:05 -0400533# define trace_selftest_function_recursion() ({ 0; })
Steven Rostedt77a2b372008-05-12 21:20:45 +0200534#endif /* CONFIG_DYNAMIC_FTRACE */
Ingo Molnare9a22d12009-03-13 11:54:40 +0100535
Steven Rostedtad977722012-07-20 13:45:59 -0400536static enum {
537 TRACE_SELFTEST_REGS_START,
538 TRACE_SELFTEST_REGS_FOUND,
539 TRACE_SELFTEST_REGS_NOT_FOUND,
540} trace_selftest_regs_stat;
541
542static void trace_selftest_test_regs_func(unsigned long ip,
543 unsigned long pip,
544 struct ftrace_ops *op,
545 struct pt_regs *pt_regs)
546{
547 if (pt_regs)
548 trace_selftest_regs_stat = TRACE_SELFTEST_REGS_FOUND;
549 else
550 trace_selftest_regs_stat = TRACE_SELFTEST_REGS_NOT_FOUND;
551}
552
553static struct ftrace_ops test_regs_probe = {
554 .func = trace_selftest_test_regs_func,
555 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_SAVE_REGS,
556};
557
558static int
559trace_selftest_function_regs(void)
560{
561 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtad977722012-07-20 13:45:59 -0400562 char *func_name;
563 int len;
564 int ret;
565 int supported = 0;
566
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900567#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedtad977722012-07-20 13:45:59 -0400568 supported = 1;
569#endif
570
571 /* The previous test PASSED */
572 pr_cont("PASSED\n");
573 pr_info("Testing ftrace regs%s: ",
574 !supported ? "(no arch support)" : "");
575
576 /* enable tracing, and record the filter function */
577 ftrace_enabled = 1;
Steven Rostedtad977722012-07-20 13:45:59 -0400578
579 /* Handle PPC64 '.' name */
580 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
581 len = strlen(func_name);
582
583 ret = ftrace_set_filter(&test_regs_probe, func_name, len, 1);
584 /*
585 * If DYNAMIC_FTRACE is not set, then we just trace all functions.
586 * This test really doesn't care.
587 */
588 if (ret && ret != -ENODEV) {
589 pr_cont("*Could not set filter* ");
590 goto out;
591 }
592
593 ret = register_ftrace_function(&test_regs_probe);
594 /*
595 * Now if the arch does not support passing regs, then this should
596 * have failed.
597 */
598 if (!supported) {
599 if (!ret) {
600 pr_cont("*registered save-regs without arch support* ");
601 goto out;
602 }
603 test_regs_probe.flags |= FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
604 ret = register_ftrace_function(&test_regs_probe);
605 }
606 if (ret) {
607 pr_cont("*could not register callback* ");
608 goto out;
609 }
610
611
612 DYN_FTRACE_TEST_NAME();
613
614 unregister_ftrace_function(&test_regs_probe);
615
616 ret = -1;
617
618 switch (trace_selftest_regs_stat) {
619 case TRACE_SELFTEST_REGS_START:
620 pr_cont("*callback never called* ");
621 goto out;
622
623 case TRACE_SELFTEST_REGS_FOUND:
624 if (supported)
625 break;
626 pr_cont("*callback received regs without arch support* ");
627 goto out;
628
629 case TRACE_SELFTEST_REGS_NOT_FOUND:
630 if (!supported)
631 break;
632 pr_cont("*callback received NULL regs* ");
633 goto out;
634 }
635
636 ret = 0;
637out:
638 ftrace_enabled = save_ftrace_enabled;
Steven Rostedtad977722012-07-20 13:45:59 -0400639
640 return ret;
641}
642
Steven Rostedt60a11772008-05-12 21:20:44 +0200643/*
644 * Simple verification test of ftrace function tracer.
645 * Enable ftrace, sleep 1/10 second, and then read the trace
646 * buffer to see if all is in order.
647 */
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400648__init int
Steven Rostedt60a11772008-05-12 21:20:44 +0200649trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
650{
Steven Rostedt77a2b372008-05-12 21:20:45 +0200651 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400652 unsigned long count;
653 int ret;
Steven Rostedt60a11772008-05-12 21:20:44 +0200654
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400655#ifdef CONFIG_DYNAMIC_FTRACE
656 if (ftrace_filter_param) {
657 printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
658 return 0;
659 }
660#endif
661
Steven Rostedt77a2b372008-05-12 21:20:45 +0200662 /* make sure msleep has been recorded */
663 msleep(1);
664
Steven Rostedt60a11772008-05-12 21:20:44 +0200665 /* start the tracing */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200666 ftrace_enabled = 1;
667
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200668 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100669 if (ret) {
670 warn_failed_init_tracer(trace, ret);
671 goto out;
672 }
673
Steven Rostedt60a11772008-05-12 21:20:44 +0200674 /* Sleep for a 1/10 of a second */
675 msleep(100);
676 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500677 tracing_stop();
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200678 ftrace_enabled = 0;
679
Steven Rostedt60a11772008-05-12 21:20:44 +0200680 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500681 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +0200682 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500683 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +0200684
685 if (!ret && !count) {
686 printk(KERN_CONT ".. no entries found ..");
687 ret = -1;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200688 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +0200689 }
690
Steven Rostedt77a2b372008-05-12 21:20:45 +0200691 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
692 DYN_FTRACE_TEST_NAME);
Steven Rostedtea701f12012-07-20 13:08:05 -0400693 if (ret)
694 goto out;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200695
Steven Rostedtea701f12012-07-20 13:08:05 -0400696 ret = trace_selftest_function_recursion();
Steven Rostedtad977722012-07-20 13:45:59 -0400697 if (ret)
698 goto out;
699
700 ret = trace_selftest_function_regs();
Steven Rostedt77a2b372008-05-12 21:20:45 +0200701 out:
702 ftrace_enabled = save_ftrace_enabled;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200703
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200704 /* kill ftrace totally if we failed */
705 if (ret)
706 ftrace_kill();
707
Steven Rostedt60a11772008-05-12 21:20:44 +0200708 return ret;
709}
Steven Rostedt606576c2008-10-06 19:06:12 -0400710#endif /* CONFIG_FUNCTION_TRACER */
Steven Rostedt60a11772008-05-12 21:20:44 +0200711
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100712
713#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100714
715/* Maximum number of functions to trace before diagnosing a hang */
716#define GRAPH_MAX_FUNC_TEST 100000000
717
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100718static unsigned int graph_hang_thresh;
719
720/* Wrap the real function entry probe to avoid possible hanging */
721static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
722{
723 /* This is harmlessly racy, we want to approximately detect a hang */
724 if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
725 ftrace_graph_stop();
726 printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -0400727 if (ftrace_dump_on_oops) {
728 ftrace_dump(DUMP_ALL);
729 /* ftrace_dump() disables tracing */
730 tracing_on();
731 }
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100732 return 0;
733 }
734
735 return trace_graph_entry(trace);
736}
737
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100738/*
739 * Pretty much the same than for the function tracer from which the selftest
740 * has been borrowed.
741 */
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400742__init int
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100743trace_selftest_startup_function_graph(struct tracer *trace,
744 struct trace_array *tr)
745{
746 int ret;
747 unsigned long count;
748
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400749#ifdef CONFIG_DYNAMIC_FTRACE
750 if (ftrace_filter_param) {
751 printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
752 return 0;
753 }
754#endif
755
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100756 /*
757 * Simulate the init() callback but we attach a watchdog callback
758 * to detect and recover from possible hangs
759 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500760 tracing_reset_online_cpus(&tr->trace_buffer);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200761 set_graph_array(tr);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100762 ret = register_ftrace_graph(&trace_graph_return,
763 &trace_graph_entry_watchdog);
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100764 if (ret) {
765 warn_failed_init_tracer(trace, ret);
766 goto out;
767 }
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100768 tracing_start_cmdline_record();
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100769
770 /* Sleep for a 1/10 of a second */
771 msleep(100);
772
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100773 /* Have we just recovered from a hang? */
774 if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
Frederic Weisbecker0cf53ff2009-03-22 15:13:07 +0100775 tracing_selftest_disabled = true;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100776 ret = -1;
777 goto out;
778 }
779
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100780 tracing_stop();
781
782 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500783 ret = trace_test_buffer(&tr->trace_buffer, &count);
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100784
785 trace->reset(tr);
786 tracing_start();
787
788 if (!ret && !count) {
789 printk(KERN_CONT ".. no entries found ..");
790 ret = -1;
791 goto out;
792 }
793
794 /* Don't test dynamic tracing, the function tracer already did */
795
796out:
797 /* Stop it if we failed */
798 if (ret)
799 ftrace_graph_stop();
800
801 return ret;
802}
803#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
804
805
Steven Rostedt60a11772008-05-12 21:20:44 +0200806#ifdef CONFIG_IRQSOFF_TRACER
807int
808trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
809{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500810 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +0200811 unsigned long count;
812 int ret;
813
814 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200815 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100816 if (ret) {
817 warn_failed_init_tracer(trace, ret);
818 return ret;
819 }
820
Steven Rostedt60a11772008-05-12 21:20:44 +0200821 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500822 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +0200823 /* disable interrupts for a bit */
824 local_irq_disable();
825 udelay(100);
826 local_irq_enable();
Frederic Weisbecker49036202009-03-17 22:38:58 +0100827
828 /*
829 * Stop the tracer to avoid a warning subsequent
830 * to buffer flipping failure because tracing_stop()
831 * disables the tr and max buffers, making flipping impossible
832 * in case of parallels max irqs off latencies.
833 */
834 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +0200835 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500836 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +0200837 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500838 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +0200839 if (!ret)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500840 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +0200841 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500842 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +0200843
844 if (!ret && !count) {
845 printk(KERN_CONT ".. no entries found ..");
846 ret = -1;
847 }
848
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500849 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +0200850
851 return ret;
852}
853#endif /* CONFIG_IRQSOFF_TRACER */
854
855#ifdef CONFIG_PREEMPT_TRACER
856int
857trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
858{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500859 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +0200860 unsigned long count;
861 int ret;
862
Steven Rostedt769c48e2008-11-07 22:36:02 -0500863 /*
864 * Now that the big kernel lock is no longer preemptable,
865 * and this is called with the BKL held, it will always
866 * fail. If preemption is already disabled, simply
867 * pass the test. When the BKL is removed, or becomes
868 * preemptible again, we will once again test this,
869 * so keep it in.
870 */
871 if (preempt_count()) {
872 printk(KERN_CONT "can not test ... force ");
873 return 0;
874 }
875
Steven Rostedt60a11772008-05-12 21:20:44 +0200876 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200877 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100878 if (ret) {
879 warn_failed_init_tracer(trace, ret);
880 return ret;
881 }
882
Steven Rostedt60a11772008-05-12 21:20:44 +0200883 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500884 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +0200885 /* disable preemption for a bit */
886 preempt_disable();
887 udelay(100);
888 preempt_enable();
Frederic Weisbecker49036202009-03-17 22:38:58 +0100889
890 /*
891 * Stop the tracer to avoid a warning subsequent
892 * to buffer flipping failure because tracing_stop()
893 * disables the tr and max buffers, making flipping impossible
894 * in case of parallels max preempt off latencies.
895 */
896 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +0200897 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500898 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +0200899 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500900 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +0200901 if (!ret)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500902 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +0200903 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500904 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +0200905
906 if (!ret && !count) {
907 printk(KERN_CONT ".. no entries found ..");
908 ret = -1;
909 }
910
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500911 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +0200912
913 return ret;
914}
915#endif /* CONFIG_PREEMPT_TRACER */
916
917#if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
918int
919trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
920{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500921 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +0200922 unsigned long count;
923 int ret;
924
Steven Rostedt769c48e2008-11-07 22:36:02 -0500925 /*
926 * Now that the big kernel lock is no longer preemptable,
927 * and this is called with the BKL held, it will always
928 * fail. If preemption is already disabled, simply
929 * pass the test. When the BKL is removed, or becomes
930 * preemptible again, we will once again test this,
931 * so keep it in.
932 */
933 if (preempt_count()) {
934 printk(KERN_CONT "can not test ... force ");
935 return 0;
936 }
937
Steven Rostedt60a11772008-05-12 21:20:44 +0200938 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200939 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100940 if (ret) {
941 warn_failed_init_tracer(trace, ret);
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +0100942 goto out_no_start;
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100943 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200944
945 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500946 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +0200947
948 /* disable preemption and interrupts for a bit */
949 preempt_disable();
950 local_irq_disable();
951 udelay(100);
952 preempt_enable();
953 /* reverse the order of preempt vs irqs */
954 local_irq_enable();
955
Frederic Weisbecker49036202009-03-17 22:38:58 +0100956 /*
957 * Stop the tracer to avoid a warning subsequent
958 * to buffer flipping failure because tracing_stop()
959 * disables the tr and max buffers, making flipping impossible
960 * in case of parallels max irqs/preempt off latencies.
961 */
962 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +0200963 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500964 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +0200965 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500966 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +0100967 if (ret)
Steven Rostedt60a11772008-05-12 21:20:44 +0200968 goto out;
969
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500970 ret = trace_test_buffer(&tr->max_buffer, &count);
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +0100971 if (ret)
Steven Rostedt60a11772008-05-12 21:20:44 +0200972 goto out;
973
974 if (!ret && !count) {
975 printk(KERN_CONT ".. no entries found ..");
976 ret = -1;
977 goto out;
978 }
979
980 /* do the test by disabling interrupts first this time */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500981 tr->max_latency = 0;
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500982 tracing_start();
Frederic Weisbecker49036202009-03-17 22:38:58 +0100983 trace->start(tr);
984
Steven Rostedt60a11772008-05-12 21:20:44 +0200985 preempt_disable();
986 local_irq_disable();
987 udelay(100);
988 preempt_enable();
989 /* reverse the order of preempt vs irqs */
990 local_irq_enable();
991
Frederic Weisbecker49036202009-03-17 22:38:58 +0100992 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +0200993 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500994 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +0200995 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500996 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +0200997 if (ret)
998 goto out;
999
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001000 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +02001001
1002 if (!ret && !count) {
1003 printk(KERN_CONT ".. no entries found ..");
1004 ret = -1;
1005 goto out;
1006 }
1007
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +01001008out:
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001009 tracing_start();
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +01001010out_no_start:
1011 trace->reset(tr);
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001012 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +02001013
1014 return ret;
1015}
1016#endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
1017
Steven Noonanfb1b6d82008-09-19 03:06:43 -07001018#ifdef CONFIG_NOP_TRACER
1019int
1020trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
1021{
1022 /* What could possibly go wrong? */
1023 return 0;
1024}
1025#endif
1026
Steven Rostedt60a11772008-05-12 21:20:44 +02001027#ifdef CONFIG_SCHED_TRACER
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001028
1029struct wakeup_test_data {
1030 struct completion is_ready;
1031 int go;
1032};
1033
Steven Rostedt60a11772008-05-12 21:20:44 +02001034static int trace_wakeup_test_thread(void *data)
1035{
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001036 /* Make this a -deadline thread */
1037 static const struct sched_attr attr = {
1038 .sched_policy = SCHED_DEADLINE,
1039 .sched_runtime = 100000ULL,
1040 .sched_deadline = 10000000ULL,
1041 .sched_period = 10000000ULL
1042 };
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001043 struct wakeup_test_data *x = data;
Steven Rostedt60a11772008-05-12 21:20:44 +02001044
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001045 sched_setattr(current, &attr);
Steven Rostedt60a11772008-05-12 21:20:44 +02001046
1047 /* Make it know we have a new prio */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001048 complete(&x->is_ready);
Steven Rostedt60a11772008-05-12 21:20:44 +02001049
1050 /* now go to sleep and let the test wake us up */
1051 set_current_state(TASK_INTERRUPTIBLE);
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001052 while (!x->go) {
1053 schedule();
1054 set_current_state(TASK_INTERRUPTIBLE);
1055 }
Steven Rostedt60a11772008-05-12 21:20:44 +02001056
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001057 complete(&x->is_ready);
1058
1059 set_current_state(TASK_INTERRUPTIBLE);
Steven Rostedt3c18c102012-07-31 10:23:37 -04001060
Steven Rostedt60a11772008-05-12 21:20:44 +02001061 /* we are awake, now wait to disappear */
1062 while (!kthread_should_stop()) {
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001063 schedule();
1064 set_current_state(TASK_INTERRUPTIBLE);
Steven Rostedt60a11772008-05-12 21:20:44 +02001065 }
1066
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001067 __set_current_state(TASK_RUNNING);
1068
Steven Rostedt60a11772008-05-12 21:20:44 +02001069 return 0;
1070}
Steven Rostedt60a11772008-05-12 21:20:44 +02001071int
1072trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
1073{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001074 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +02001075 struct task_struct *p;
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001076 struct wakeup_test_data data;
Steven Rostedt60a11772008-05-12 21:20:44 +02001077 unsigned long count;
1078 int ret;
1079
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001080 memset(&data, 0, sizeof(data));
1081
1082 init_completion(&data.is_ready);
Steven Rostedt60a11772008-05-12 21:20:44 +02001083
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001084 /* create a -deadline thread */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001085 p = kthread_run(trace_wakeup_test_thread, &data, "ftrace-test");
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001086 if (IS_ERR(p)) {
Steven Rostedt60a11772008-05-12 21:20:44 +02001087 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
1088 return -1;
1089 }
1090
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001091 /* make sure the thread is running at -deadline policy */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001092 wait_for_completion(&data.is_ready);
Steven Rostedt60a11772008-05-12 21:20:44 +02001093
1094 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02001095 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01001096 if (ret) {
1097 warn_failed_init_tracer(trace, ret);
1098 return ret;
1099 }
1100
Steven Rostedt60a11772008-05-12 21:20:44 +02001101 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001102 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02001103
Steven Rostedt3c18c102012-07-31 10:23:37 -04001104 while (p->on_rq) {
1105 /*
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001106 * Sleep to make sure the -deadline thread is asleep too.
Steven Rostedt3c18c102012-07-31 10:23:37 -04001107 * On virtual machines we can't rely on timings,
1108 * but we want to make sure this test still works.
1109 */
1110 msleep(100);
1111 }
Steven Rostedt60a11772008-05-12 21:20:44 +02001112
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001113 init_completion(&data.is_ready);
1114
1115 data.go = 1;
1116 /* memory barrier is in the wake_up_process() */
Steven Rostedt60a11772008-05-12 21:20:44 +02001117
1118 wake_up_process(p);
1119
Steven Rostedt3c18c102012-07-31 10:23:37 -04001120 /* Wait for the task to wake up */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001121 wait_for_completion(&data.is_ready);
Steven Rostedt5aa60c62008-09-29 23:02:37 -04001122
Steven Rostedt60a11772008-05-12 21:20:44 +02001123 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001124 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +02001125 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001126 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +02001127 if (!ret)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001128 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +02001129
1130
1131 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001132 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +02001133
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001134 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +02001135
1136 /* kill the thread */
1137 kthread_stop(p);
1138
1139 if (!ret && !count) {
1140 printk(KERN_CONT ".. no entries found ..");
1141 ret = -1;
1142 }
1143
1144 return ret;
1145}
1146#endif /* CONFIG_SCHED_TRACER */
1147
1148#ifdef CONFIG_CONTEXT_SWITCH_TRACER
1149int
1150trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
1151{
1152 unsigned long count;
1153 int ret;
1154
1155 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02001156 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01001157 if (ret) {
1158 warn_failed_init_tracer(trace, ret);
1159 return ret;
1160 }
1161
Steven Rostedt60a11772008-05-12 21:20:44 +02001162 /* Sleep for a 1/10 of a second */
1163 msleep(100);
1164 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001165 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +02001166 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001167 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +02001168 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001169 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +02001170
1171 if (!ret && !count) {
1172 printk(KERN_CONT ".. no entries found ..");
1173 ret = -1;
1174 }
1175
1176 return ret;
1177}
1178#endif /* CONFIG_CONTEXT_SWITCH_TRACER */
Ingo Molnara6dd24f2008-05-12 21:20:47 +02001179
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001180#ifdef CONFIG_BRANCH_TRACER
1181int
1182trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
1183{
1184 unsigned long count;
1185 int ret;
1186
1187 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02001188 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01001189 if (ret) {
1190 warn_failed_init_tracer(trace, ret);
1191 return ret;
1192 }
1193
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001194 /* Sleep for a 1/10 of a second */
1195 msleep(100);
1196 /* stop the tracing. */
1197 tracing_stop();
1198 /* check the trace buffer */
Steven Rostedt (Red Hat)0184d502013-05-29 15:56:49 -04001199 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001200 trace->reset(tr);
1201 tracing_start();
1202
Wenji Huangd2ef7c22009-02-17 01:09:47 -05001203 if (!ret && !count) {
1204 printk(KERN_CONT ".. no entries found ..");
1205 ret = -1;
1206 }
1207
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001208 return ret;
1209}
1210#endif /* CONFIG_BRANCH_TRACER */
Markus Metzger321bb5e2009-03-13 10:50:27 +01001211