trace_selftest.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /* Include in trace.c */
  2. #include <linux/stringify.h>
  3. #include <linux/kthread.h>
  4. #include <linux/delay.h>
  5. #include <linux/slab.h>
  6. static inline int trace_valid_entry(struct trace_entry *entry)
  7. {
  8. switch (entry->type) {
  9. case TRACE_FN:
  10. case TRACE_CTX:
  11. case TRACE_WAKE:
  12. case TRACE_STACK:
  13. case TRACE_PRINT:
  14. case TRACE_BRANCH:
  15. case TRACE_GRAPH_ENT:
  16. case TRACE_GRAPH_RET:
  17. return 1;
  18. }
  19. return 0;
  20. }
  21. static int trace_test_buffer_cpu(struct trace_buffer *buf, int cpu)
  22. {
  23. struct ring_buffer_event *event;
  24. struct trace_entry *entry;
  25. unsigned int loops = 0;
  26. while ((event = ring_buffer_consume(buf->buffer, cpu, NULL, NULL))) {
  27. entry = ring_buffer_event_data(event);
  28. /*
  29. * The ring buffer is a size of trace_buf_size, if
  30. * we loop more than the size, there's something wrong
  31. * with the ring buffer.
  32. */
  33. if (loops++ > trace_buf_size) {
  34. printk(KERN_CONT ".. bad ring buffer ");
  35. goto failed;
  36. }
  37. if (!trace_valid_entry(entry)) {
  38. printk(KERN_CONT ".. invalid entry %d ",
  39. entry->type);
  40. goto failed;
  41. }
  42. }
  43. return 0;
  44. failed:
  45. /* disable tracing */
  46. tracing_disabled = 1;
  47. printk(KERN_CONT ".. corrupted trace buffer .. ");
  48. return -1;
  49. }
  50. /*
  51. * Test the trace buffer to see if all the elements
  52. * are still sane.
  53. */
  54. static int trace_test_buffer(struct trace_buffer *buf, unsigned long *count)
  55. {
  56. unsigned long flags, cnt = 0;
  57. int cpu, ret = 0;
  58. /* Don't allow flipping of max traces now */
  59. local_irq_save(flags);
  60. arch_spin_lock(&ftrace_max_lock);
  61. cnt = ring_buffer_entries(buf->buffer);
  62. /*
  63. * The trace_test_buffer_cpu runs a while loop to consume all data.
  64. * If the calling tracer is broken, and is constantly filling
  65. * the buffer, this will run forever, and hard lock the box.
  66. * We disable the ring buffer while we do this test to prevent
  67. * a hard lock up.
  68. */
  69. tracing_off();
  70. for_each_possible_cpu(cpu) {
  71. ret = trace_test_buffer_cpu(buf, cpu);
  72. if (ret)
  73. break;
  74. }
  75. tracing_on();
  76. arch_spin_unlock(&ftrace_max_lock);
  77. local_irq_restore(flags);
  78. if (count)
  79. *count = cnt;
  80. return ret;
  81. }
  82. static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
  83. {
  84. printk(KERN_WARNING "Failed to init %s tracer, init returned %d\n",
  85. trace->name, init_ret);
  86. }
  87. #ifdef CONFIG_FUNCTION_TRACER
  88. #ifdef CONFIG_DYNAMIC_FTRACE
  89. static int trace_selftest_test_probe1_cnt;
  90. static void trace_selftest_test_probe1_func(unsigned long ip,
  91. unsigned long pip,
  92. struct ftrace_ops *op,
  93. struct pt_regs *pt_regs)
  94. {
  95. trace_selftest_test_probe1_cnt++;
  96. }
  97. static int trace_selftest_test_probe2_cnt;
  98. static void trace_selftest_test_probe2_func(unsigned long ip,
  99. unsigned long pip,
  100. struct ftrace_ops *op,
  101. struct pt_regs *pt_regs)
  102. {
  103. trace_selftest_test_probe2_cnt++;
  104. }
  105. static int trace_selftest_test_probe3_cnt;
  106. static void trace_selftest_test_probe3_func(unsigned long ip,
  107. unsigned long pip,
  108. struct ftrace_ops *op,
  109. struct pt_regs *pt_regs)
  110. {
  111. trace_selftest_test_probe3_cnt++;
  112. }
  113. static int trace_selftest_test_global_cnt;
  114. static void trace_selftest_test_global_func(unsigned long ip,
  115. unsigned long pip,
  116. struct ftrace_ops *op,
  117. struct pt_regs *pt_regs)
  118. {
  119. trace_selftest_test_global_cnt++;
  120. }
  121. static int trace_selftest_test_dyn_cnt;
  122. static void trace_selftest_test_dyn_func(unsigned long ip,
  123. unsigned long pip,
  124. struct ftrace_ops *op,
  125. struct pt_regs *pt_regs)
  126. {
  127. trace_selftest_test_dyn_cnt++;
  128. }
  129. static struct ftrace_ops test_probe1 = {
  130. .func = trace_selftest_test_probe1_func,
  131. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  132. };
  133. static struct ftrace_ops test_probe2 = {
  134. .func = trace_selftest_test_probe2_func,
  135. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  136. };
  137. static struct ftrace_ops test_probe3 = {
  138. .func = trace_selftest_test_probe3_func,
  139. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  140. };
  141. static struct ftrace_ops test_global = {
  142. .func = trace_selftest_test_global_func,
  143. .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
  144. };
  145. static void print_counts(void)
  146. {
  147. printk("(%d %d %d %d %d) ",
  148. trace_selftest_test_probe1_cnt,
  149. trace_selftest_test_probe2_cnt,
  150. trace_selftest_test_probe3_cnt,
  151. trace_selftest_test_global_cnt,
  152. trace_selftest_test_dyn_cnt);
  153. }
  154. static void reset_counts(void)
  155. {
  156. trace_selftest_test_probe1_cnt = 0;
  157. trace_selftest_test_probe2_cnt = 0;
  158. trace_selftest_test_probe3_cnt = 0;
  159. trace_selftest_test_global_cnt = 0;
  160. trace_selftest_test_dyn_cnt = 0;
  161. }
  162. static int trace_selftest_ops(int cnt)
  163. {
  164. int save_ftrace_enabled = ftrace_enabled;
  165. struct ftrace_ops *dyn_ops;
  166. char *func1_name;
  167. char *func2_name;
  168. int len1;
  169. int len2;
  170. int ret = -1;
  171. printk(KERN_CONT "PASSED\n");
  172. pr_info("Testing dynamic ftrace ops #%d: ", cnt);
  173. ftrace_enabled = 1;
  174. reset_counts();
  175. /* Handle PPC64 '.' name */
  176. func1_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  177. func2_name = "*" __stringify(DYN_FTRACE_TEST_NAME2);
  178. len1 = strlen(func1_name);
  179. len2 = strlen(func2_name);
  180. /*
  181. * Probe 1 will trace function 1.
  182. * Probe 2 will trace function 2.
  183. * Probe 3 will trace functions 1 and 2.
  184. */
  185. ftrace_set_filter(&test_probe1, func1_name, len1, 1);
  186. ftrace_set_filter(&test_probe2, func2_name, len2, 1);
  187. ftrace_set_filter(&test_probe3, func1_name, len1, 1);
  188. ftrace_set_filter(&test_probe3, func2_name, len2, 0);
  189. register_ftrace_function(&test_probe1);
  190. register_ftrace_function(&test_probe2);
  191. register_ftrace_function(&test_probe3);
  192. register_ftrace_function(&test_global);
  193. DYN_FTRACE_TEST_NAME();
  194. print_counts();
  195. if (trace_selftest_test_probe1_cnt != 1)
  196. goto out;
  197. if (trace_selftest_test_probe2_cnt != 0)
  198. goto out;
  199. if (trace_selftest_test_probe3_cnt != 1)
  200. goto out;
  201. if (trace_selftest_test_global_cnt == 0)
  202. goto out;
  203. DYN_FTRACE_TEST_NAME2();
  204. print_counts();
  205. if (trace_selftest_test_probe1_cnt != 1)
  206. goto out;
  207. if (trace_selftest_test_probe2_cnt != 1)
  208. goto out;
  209. if (trace_selftest_test_probe3_cnt != 2)
  210. goto out;
  211. /* Add a dynamic probe */
  212. dyn_ops = kzalloc(sizeof(*dyn_ops), GFP_KERNEL);
  213. if (!dyn_ops) {
  214. printk("MEMORY ERROR ");
  215. goto out;
  216. }
  217. dyn_ops->func = trace_selftest_test_dyn_func;
  218. register_ftrace_function(dyn_ops);
  219. trace_selftest_test_global_cnt = 0;
  220. DYN_FTRACE_TEST_NAME();
  221. print_counts();
  222. if (trace_selftest_test_probe1_cnt != 2)
  223. goto out_free;
  224. if (trace_selftest_test_probe2_cnt != 1)
  225. goto out_free;
  226. if (trace_selftest_test_probe3_cnt != 3)
  227. goto out_free;
  228. if (trace_selftest_test_global_cnt == 0)
  229. goto out;
  230. if (trace_selftest_test_dyn_cnt == 0)
  231. goto out_free;
  232. DYN_FTRACE_TEST_NAME2();
  233. print_counts();
  234. if (trace_selftest_test_probe1_cnt != 2)
  235. goto out_free;
  236. if (trace_selftest_test_probe2_cnt != 2)
  237. goto out_free;
  238. if (trace_selftest_test_probe3_cnt != 4)
  239. goto out_free;
  240. ret = 0;
  241. out_free:
  242. unregister_ftrace_function(dyn_ops);
  243. kfree(dyn_ops);
  244. out:
  245. /* Purposely unregister in the same order */
  246. unregister_ftrace_function(&test_probe1);
  247. unregister_ftrace_function(&test_probe2);
  248. unregister_ftrace_function(&test_probe3);
  249. unregister_ftrace_function(&test_global);
  250. /* Make sure everything is off */
  251. reset_counts();
  252. DYN_FTRACE_TEST_NAME();
  253. DYN_FTRACE_TEST_NAME();
  254. if (trace_selftest_test_probe1_cnt ||
  255. trace_selftest_test_probe2_cnt ||
  256. trace_selftest_test_probe3_cnt ||
  257. trace_selftest_test_global_cnt ||
  258. trace_selftest_test_dyn_cnt)
  259. ret = -1;
  260. ftrace_enabled = save_ftrace_enabled;
  261. return ret;
  262. }
  263. /* Test dynamic code modification and ftrace filters */
  264. int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
  265. struct trace_array *tr,
  266. int (*func)(void))
  267. {
  268. int save_ftrace_enabled = ftrace_enabled;
  269. unsigned long count;
  270. char *func_name;
  271. int ret;
  272. /* The ftrace test PASSED */
  273. printk(KERN_CONT "PASSED\n");
  274. pr_info("Testing dynamic ftrace: ");
  275. /* enable tracing, and record the filter function */
  276. ftrace_enabled = 1;
  277. /* passed in by parameter to fool gcc from optimizing */
  278. func();
  279. /*
  280. * Some archs *cough*PowerPC*cough* add characters to the
  281. * start of the function names. We simply put a '*' to
  282. * accommodate them.
  283. */
  284. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  285. /* filter only on our function */
  286. ftrace_set_global_filter(func_name, strlen(func_name), 1);
  287. /* enable tracing */
  288. ret = tracer_init(trace, tr);
  289. if (ret) {
  290. warn_failed_init_tracer(trace, ret);
  291. goto out;
  292. }
  293. /* Sleep for a 1/10 of a second */
  294. msleep(100);
  295. /* we should have nothing in the buffer */
  296. ret = trace_test_buffer(&tr->trace_buffer, &count);
  297. if (ret)
  298. goto out;
  299. if (count) {
  300. ret = -1;
  301. printk(KERN_CONT ".. filter did not filter .. ");
  302. goto out;
  303. }
  304. /* call our function again */
  305. func();
  306. /* sleep again */
  307. msleep(100);
  308. /* stop the tracing. */
  309. tracing_stop();
  310. ftrace_enabled = 0;
  311. /* check the trace buffer */
  312. ret = trace_test_buffer(&tr->trace_buffer, &count);
  313. tracing_start();
  314. /* we should only have one item */
  315. if (!ret && count != 1) {
  316. trace->reset(tr);
  317. printk(KERN_CONT ".. filter failed count=%ld ..", count);
  318. ret = -1;
  319. goto out;
  320. }
  321. /* Test the ops with global tracing running */
  322. ret = trace_selftest_ops(1);
  323. trace->reset(tr);
  324. out:
  325. ftrace_enabled = save_ftrace_enabled;
  326. /* Enable tracing on all functions again */
  327. ftrace_set_global_filter(NULL, 0, 1);
  328. /* Test the ops with global tracing off */
  329. if (!ret)
  330. ret = trace_selftest_ops(2);
  331. return ret;
  332. }
  333. static int trace_selftest_recursion_cnt;
  334. static void trace_selftest_test_recursion_func(unsigned long ip,
  335. unsigned long pip,
  336. struct ftrace_ops *op,
  337. struct pt_regs *pt_regs)
  338. {
  339. /*
  340. * This function is registered without the recursion safe flag.
  341. * The ftrace infrastructure should provide the recursion
  342. * protection. If not, this will crash the kernel!
  343. */
  344. if (trace_selftest_recursion_cnt++ > 10)
  345. return;
  346. DYN_FTRACE_TEST_NAME();
  347. }
  348. static void trace_selftest_test_recursion_safe_func(unsigned long ip,
  349. unsigned long pip,
  350. struct ftrace_ops *op,
  351. struct pt_regs *pt_regs)
  352. {
  353. /*
  354. * We said we would provide our own recursion. By calling
  355. * this function again, we should recurse back into this function
  356. * and count again. But this only happens if the arch supports
  357. * all of ftrace features and nothing else is using the function
  358. * tracing utility.
  359. */
  360. if (trace_selftest_recursion_cnt++)
  361. return;
  362. DYN_FTRACE_TEST_NAME();
  363. }
  364. static struct ftrace_ops test_rec_probe = {
  365. .func = trace_selftest_test_recursion_func,
  366. };
  367. static struct ftrace_ops test_recsafe_probe = {
  368. .func = trace_selftest_test_recursion_safe_func,
  369. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  370. };
  371. static int
  372. trace_selftest_function_recursion(void)
  373. {
  374. int save_ftrace_enabled = ftrace_enabled;
  375. char *func_name;
  376. int len;
  377. int ret;
  378. /* The previous test PASSED */
  379. pr_cont("PASSED\n");
  380. pr_info("Testing ftrace recursion: ");
  381. /* enable tracing, and record the filter function */
  382. ftrace_enabled = 1;
  383. /* Handle PPC64 '.' name */
  384. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  385. len = strlen(func_name);
  386. ret = ftrace_set_filter(&test_rec_probe, func_name, len, 1);
  387. if (ret) {
  388. pr_cont("*Could not set filter* ");
  389. goto out;
  390. }
  391. ret = register_ftrace_function(&test_rec_probe);
  392. if (ret) {
  393. pr_cont("*could not register callback* ");
  394. goto out;
  395. }
  396. DYN_FTRACE_TEST_NAME();
  397. unregister_ftrace_function(&test_rec_probe);
  398. ret = -1;
  399. if (trace_selftest_recursion_cnt != 1) {
  400. pr_cont("*callback not called once (%d)* ",
  401. trace_selftest_recursion_cnt);
  402. goto out;
  403. }
  404. trace_selftest_recursion_cnt = 1;
  405. pr_cont("PASSED\n");
  406. pr_info("Testing ftrace recursion safe: ");
  407. ret = ftrace_set_filter(&test_recsafe_probe, func_name, len, 1);
  408. if (ret) {
  409. pr_cont("*Could not set filter* ");
  410. goto out;
  411. }
  412. ret = register_ftrace_function(&test_recsafe_probe);
  413. if (ret) {
  414. pr_cont("*could not register callback* ");
  415. goto out;
  416. }
  417. DYN_FTRACE_TEST_NAME();
  418. unregister_ftrace_function(&test_recsafe_probe);
  419. ret = -1;
  420. if (trace_selftest_recursion_cnt != 2) {
  421. pr_cont("*callback not called expected 2 times (%d)* ",
  422. trace_selftest_recursion_cnt);
  423. goto out;
  424. }
  425. ret = 0;
  426. out:
  427. ftrace_enabled = save_ftrace_enabled;
  428. return ret;
  429. }
  430. #else
  431. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  432. # define trace_selftest_function_recursion() ({ 0; })
  433. #endif /* CONFIG_DYNAMIC_FTRACE */
  434. static enum {
  435. TRACE_SELFTEST_REGS_START,
  436. TRACE_SELFTEST_REGS_FOUND,
  437. TRACE_SELFTEST_REGS_NOT_FOUND,
  438. } trace_selftest_regs_stat;
  439. static void trace_selftest_test_regs_func(unsigned long ip,
  440. unsigned long pip,
  441. struct ftrace_ops *op,
  442. struct pt_regs *pt_regs)
  443. {
  444. if (pt_regs)
  445. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_FOUND;
  446. else
  447. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_NOT_FOUND;
  448. }
  449. static struct ftrace_ops test_regs_probe = {
  450. .func = trace_selftest_test_regs_func,
  451. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_SAVE_REGS,
  452. };
  453. static int
  454. trace_selftest_function_regs(void)
  455. {
  456. int save_ftrace_enabled = ftrace_enabled;
  457. char *func_name;
  458. int len;
  459. int ret;
  460. int supported = 0;
  461. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  462. supported = 1;
  463. #endif
  464. /* The previous test PASSED */
  465. pr_cont("PASSED\n");
  466. pr_info("Testing ftrace regs%s: ",
  467. !supported ? "(no arch support)" : "");
  468. /* enable tracing, and record the filter function */
  469. ftrace_enabled = 1;
  470. /* Handle PPC64 '.' name */
  471. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  472. len = strlen(func_name);
  473. ret = ftrace_set_filter(&test_regs_probe, func_name, len, 1);
  474. /*
  475. * If DYNAMIC_FTRACE is not set, then we just trace all functions.
  476. * This test really doesn't care.
  477. */
  478. if (ret && ret != -ENODEV) {
  479. pr_cont("*Could not set filter* ");
  480. goto out;
  481. }
  482. ret = register_ftrace_function(&test_regs_probe);
  483. /*
  484. * Now if the arch does not support passing regs, then this should
  485. * have failed.
  486. */
  487. if (!supported) {
  488. if (!ret) {
  489. pr_cont("*registered save-regs without arch support* ");
  490. goto out;
  491. }
  492. test_regs_probe.flags |= FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
  493. ret = register_ftrace_function(&test_regs_probe);
  494. }
  495. if (ret) {
  496. pr_cont("*could not register callback* ");
  497. goto out;
  498. }
  499. DYN_FTRACE_TEST_NAME();
  500. unregister_ftrace_function(&test_regs_probe);
  501. ret = -1;
  502. switch (trace_selftest_regs_stat) {
  503. case TRACE_SELFTEST_REGS_START:
  504. pr_cont("*callback never called* ");
  505. goto out;
  506. case TRACE_SELFTEST_REGS_FOUND:
  507. if (supported)
  508. break;
  509. pr_cont("*callback received regs without arch support* ");
  510. goto out;
  511. case TRACE_SELFTEST_REGS_NOT_FOUND:
  512. if (!supported)
  513. break;
  514. pr_cont("*callback received NULL regs* ");
  515. goto out;
  516. }
  517. ret = 0;
  518. out:
  519. ftrace_enabled = save_ftrace_enabled;
  520. return ret;
  521. }
  522. /*
  523. * Simple verification test of ftrace function tracer.
  524. * Enable ftrace, sleep 1/10 second, and then read the trace
  525. * buffer to see if all is in order.
  526. */
  527. int
  528. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  529. {
  530. int save_ftrace_enabled = ftrace_enabled;
  531. unsigned long count;
  532. int ret;
  533. /* make sure msleep has been recorded */
  534. msleep(1);
  535. /* start the tracing */
  536. ftrace_enabled = 1;
  537. ret = tracer_init(trace, tr);
  538. if (ret) {
  539. warn_failed_init_tracer(trace, ret);
  540. goto out;
  541. }
  542. /* Sleep for a 1/10 of a second */
  543. msleep(100);
  544. /* stop the tracing. */
  545. tracing_stop();
  546. ftrace_enabled = 0;
  547. /* check the trace buffer */
  548. ret = trace_test_buffer(&tr->trace_buffer, &count);
  549. trace->reset(tr);
  550. tracing_start();
  551. if (!ret && !count) {
  552. printk(KERN_CONT ".. no entries found ..");
  553. ret = -1;
  554. goto out;
  555. }
  556. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  557. DYN_FTRACE_TEST_NAME);
  558. if (ret)
  559. goto out;
  560. ret = trace_selftest_function_recursion();
  561. if (ret)
  562. goto out;
  563. ret = trace_selftest_function_regs();
  564. out:
  565. ftrace_enabled = save_ftrace_enabled;
  566. /* kill ftrace totally if we failed */
  567. if (ret)
  568. ftrace_kill();
  569. return ret;
  570. }
  571. #endif /* CONFIG_FUNCTION_TRACER */
  572. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  573. /* Maximum number of functions to trace before diagnosing a hang */
  574. #define GRAPH_MAX_FUNC_TEST 100000000
  575. static unsigned int graph_hang_thresh;
  576. /* Wrap the real function entry probe to avoid possible hanging */
  577. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  578. {
  579. /* This is harmlessly racy, we want to approximately detect a hang */
  580. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  581. ftrace_graph_stop();
  582. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  583. if (ftrace_dump_on_oops) {
  584. ftrace_dump(DUMP_ALL);
  585. /* ftrace_dump() disables tracing */
  586. tracing_on();
  587. }
  588. return 0;
  589. }
  590. return trace_graph_entry(trace);
  591. }
  592. /*
  593. * Pretty much the same than for the function tracer from which the selftest
  594. * has been borrowed.
  595. */
  596. int
  597. trace_selftest_startup_function_graph(struct tracer *trace,
  598. struct trace_array *tr)
  599. {
  600. int ret;
  601. unsigned long count;
  602. /*
  603. * Simulate the init() callback but we attach a watchdog callback
  604. * to detect and recover from possible hangs
  605. */
  606. tracing_reset_online_cpus(&tr->trace_buffer);
  607. set_graph_array(tr);
  608. ret = register_ftrace_graph(&trace_graph_return,
  609. &trace_graph_entry_watchdog);
  610. if (ret) {
  611. warn_failed_init_tracer(trace, ret);
  612. goto out;
  613. }
  614. tracing_start_cmdline_record();
  615. /* Sleep for a 1/10 of a second */
  616. msleep(100);
  617. /* Have we just recovered from a hang? */
  618. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  619. tracing_selftest_disabled = true;
  620. ret = -1;
  621. goto out;
  622. }
  623. tracing_stop();
  624. /* check the trace buffer */
  625. ret = trace_test_buffer(&tr->trace_buffer, &count);
  626. trace->reset(tr);
  627. tracing_start();
  628. if (!ret && !count) {
  629. printk(KERN_CONT ".. no entries found ..");
  630. ret = -1;
  631. goto out;
  632. }
  633. /* Don't test dynamic tracing, the function tracer already did */
  634. out:
  635. /* Stop it if we failed */
  636. if (ret)
  637. ftrace_graph_stop();
  638. return ret;
  639. }
  640. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  641. #ifdef CONFIG_IRQSOFF_TRACER
  642. int
  643. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  644. {
  645. unsigned long save_max = tracing_max_latency;
  646. unsigned long count;
  647. int ret;
  648. /* start the tracing */
  649. ret = tracer_init(trace, tr);
  650. if (ret) {
  651. warn_failed_init_tracer(trace, ret);
  652. return ret;
  653. }
  654. /* reset the max latency */
  655. tracing_max_latency = 0;
  656. /* disable interrupts for a bit */
  657. local_irq_disable();
  658. udelay(100);
  659. local_irq_enable();
  660. /*
  661. * Stop the tracer to avoid a warning subsequent
  662. * to buffer flipping failure because tracing_stop()
  663. * disables the tr and max buffers, making flipping impossible
  664. * in case of parallels max irqs off latencies.
  665. */
  666. trace->stop(tr);
  667. /* stop the tracing. */
  668. tracing_stop();
  669. /* check both trace buffers */
  670. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  671. if (!ret)
  672. ret = trace_test_buffer(&tr->max_buffer, &count);
  673. trace->reset(tr);
  674. tracing_start();
  675. if (!ret && !count) {
  676. printk(KERN_CONT ".. no entries found ..");
  677. ret = -1;
  678. }
  679. tracing_max_latency = save_max;
  680. return ret;
  681. }
  682. #endif /* CONFIG_IRQSOFF_TRACER */
  683. #ifdef CONFIG_PREEMPT_TRACER
  684. int
  685. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  686. {
  687. unsigned long save_max = tracing_max_latency;
  688. unsigned long count;
  689. int ret;
  690. /*
  691. * Now that the big kernel lock is no longer preemptable,
  692. * and this is called with the BKL held, it will always
  693. * fail. If preemption is already disabled, simply
  694. * pass the test. When the BKL is removed, or becomes
  695. * preemptible again, we will once again test this,
  696. * so keep it in.
  697. */
  698. if (preempt_count()) {
  699. printk(KERN_CONT "can not test ... force ");
  700. return 0;
  701. }
  702. /* start the tracing */
  703. ret = tracer_init(trace, tr);
  704. if (ret) {
  705. warn_failed_init_tracer(trace, ret);
  706. return ret;
  707. }
  708. /* reset the max latency */
  709. tracing_max_latency = 0;
  710. /* disable preemption for a bit */
  711. preempt_disable();
  712. udelay(100);
  713. preempt_enable();
  714. /*
  715. * Stop the tracer to avoid a warning subsequent
  716. * to buffer flipping failure because tracing_stop()
  717. * disables the tr and max buffers, making flipping impossible
  718. * in case of parallels max preempt off latencies.
  719. */
  720. trace->stop(tr);
  721. /* stop the tracing. */
  722. tracing_stop();
  723. /* check both trace buffers */
  724. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  725. if (!ret)
  726. ret = trace_test_buffer(&tr->max_buffer, &count);
  727. trace->reset(tr);
  728. tracing_start();
  729. if (!ret && !count) {
  730. printk(KERN_CONT ".. no entries found ..");
  731. ret = -1;
  732. }
  733. tracing_max_latency = save_max;
  734. return ret;
  735. }
  736. #endif /* CONFIG_PREEMPT_TRACER */
  737. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  738. int
  739. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  740. {
  741. unsigned long save_max = tracing_max_latency;
  742. unsigned long count;
  743. int ret;
  744. /*
  745. * Now that the big kernel lock is no longer preemptable,
  746. * and this is called with the BKL held, it will always
  747. * fail. If preemption is already disabled, simply
  748. * pass the test. When the BKL is removed, or becomes
  749. * preemptible again, we will once again test this,
  750. * so keep it in.
  751. */
  752. if (preempt_count()) {
  753. printk(KERN_CONT "can not test ... force ");
  754. return 0;
  755. }
  756. /* start the tracing */
  757. ret = tracer_init(trace, tr);
  758. if (ret) {
  759. warn_failed_init_tracer(trace, ret);
  760. goto out_no_start;
  761. }
  762. /* reset the max latency */
  763. tracing_max_latency = 0;
  764. /* disable preemption and interrupts for a bit */
  765. preempt_disable();
  766. local_irq_disable();
  767. udelay(100);
  768. preempt_enable();
  769. /* reverse the order of preempt vs irqs */
  770. local_irq_enable();
  771. /*
  772. * Stop the tracer to avoid a warning subsequent
  773. * to buffer flipping failure because tracing_stop()
  774. * disables the tr and max buffers, making flipping impossible
  775. * in case of parallels max irqs/preempt off latencies.
  776. */
  777. trace->stop(tr);
  778. /* stop the tracing. */
  779. tracing_stop();
  780. /* check both trace buffers */
  781. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  782. if (ret)
  783. goto out;
  784. ret = trace_test_buffer(&tr->max_buffer, &count);
  785. if (ret)
  786. goto out;
  787. if (!ret && !count) {
  788. printk(KERN_CONT ".. no entries found ..");
  789. ret = -1;
  790. goto out;
  791. }
  792. /* do the test by disabling interrupts first this time */
  793. tracing_max_latency = 0;
  794. tracing_start();
  795. trace->start(tr);
  796. preempt_disable();
  797. local_irq_disable();
  798. udelay(100);
  799. preempt_enable();
  800. /* reverse the order of preempt vs irqs */
  801. local_irq_enable();
  802. trace->stop(tr);
  803. /* stop the tracing. */
  804. tracing_stop();
  805. /* check both trace buffers */
  806. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  807. if (ret)
  808. goto out;
  809. ret = trace_test_buffer(&tr->max_buffer, &count);
  810. if (!ret && !count) {
  811. printk(KERN_CONT ".. no entries found ..");
  812. ret = -1;
  813. goto out;
  814. }
  815. out:
  816. tracing_start();
  817. out_no_start:
  818. trace->reset(tr);
  819. tracing_max_latency = save_max;
  820. return ret;
  821. }
  822. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  823. #ifdef CONFIG_NOP_TRACER
  824. int
  825. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  826. {
  827. /* What could possibly go wrong? */
  828. return 0;
  829. }
  830. #endif
  831. #ifdef CONFIG_SCHED_TRACER
  832. static int trace_wakeup_test_thread(void *data)
  833. {
  834. /* Make this a RT thread, doesn't need to be too high */
  835. static const struct sched_param param = { .sched_priority = 5 };
  836. struct completion *x = data;
  837. sched_setscheduler(current, SCHED_FIFO, &param);
  838. /* Make it know we have a new prio */
  839. complete(x);
  840. /* now go to sleep and let the test wake us up */
  841. set_current_state(TASK_INTERRUPTIBLE);
  842. schedule();
  843. complete(x);
  844. /* we are awake, now wait to disappear */
  845. while (!kthread_should_stop()) {
  846. /*
  847. * This is an RT task, do short sleeps to let
  848. * others run.
  849. */
  850. msleep(100);
  851. }
  852. return 0;
  853. }
  854. int
  855. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  856. {
  857. unsigned long save_max = tracing_max_latency;
  858. struct task_struct *p;
  859. struct completion isrt;
  860. unsigned long count;
  861. int ret;
  862. init_completion(&isrt);
  863. /* create a high prio thread */
  864. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  865. if (IS_ERR(p)) {
  866. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  867. return -1;
  868. }
  869. /* make sure the thread is running at an RT prio */
  870. wait_for_completion(&isrt);
  871. /* start the tracing */
  872. ret = tracer_init(trace, tr);
  873. if (ret) {
  874. warn_failed_init_tracer(trace, ret);
  875. return ret;
  876. }
  877. /* reset the max latency */
  878. tracing_max_latency = 0;
  879. while (p->on_rq) {
  880. /*
  881. * Sleep to make sure the RT thread is asleep too.
  882. * On virtual machines we can't rely on timings,
  883. * but we want to make sure this test still works.
  884. */
  885. msleep(100);
  886. }
  887. init_completion(&isrt);
  888. wake_up_process(p);
  889. /* Wait for the task to wake up */
  890. wait_for_completion(&isrt);
  891. /* stop the tracing. */
  892. tracing_stop();
  893. /* check both trace buffers */
  894. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  895. printk("ret = %d\n", ret);
  896. if (!ret)
  897. ret = trace_test_buffer(&tr->max_buffer, &count);
  898. trace->reset(tr);
  899. tracing_start();
  900. tracing_max_latency = save_max;
  901. /* kill the thread */
  902. kthread_stop(p);
  903. if (!ret && !count) {
  904. printk(KERN_CONT ".. no entries found ..");
  905. ret = -1;
  906. }
  907. return ret;
  908. }
  909. #endif /* CONFIG_SCHED_TRACER */
  910. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  911. int
  912. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  913. {
  914. unsigned long count;
  915. int ret;
  916. /* start the tracing */
  917. ret = tracer_init(trace, tr);
  918. if (ret) {
  919. warn_failed_init_tracer(trace, ret);
  920. return ret;
  921. }
  922. /* Sleep for a 1/10 of a second */
  923. msleep(100);
  924. /* stop the tracing. */
  925. tracing_stop();
  926. /* check the trace buffer */
  927. ret = trace_test_buffer(&tr->trace_buffer, &count);
  928. trace->reset(tr);
  929. tracing_start();
  930. if (!ret && !count) {
  931. printk(KERN_CONT ".. no entries found ..");
  932. ret = -1;
  933. }
  934. return ret;
  935. }
  936. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  937. #ifdef CONFIG_BRANCH_TRACER
  938. int
  939. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  940. {
  941. unsigned long count;
  942. int ret;
  943. /* start the tracing */
  944. ret = tracer_init(trace, tr);
  945. if (ret) {
  946. warn_failed_init_tracer(trace, ret);
  947. return ret;
  948. }
  949. /* Sleep for a 1/10 of a second */
  950. msleep(100);
  951. /* stop the tracing. */
  952. tracing_stop();
  953. /* check the trace buffer */
  954. ret = trace_test_buffer(tr, &count);
  955. trace->reset(tr);
  956. tracing_start();
  957. if (!ret && !count) {
  958. printk(KERN_CONT ".. no entries found ..");
  959. ret = -1;
  960. }
  961. return ret;
  962. }
  963. #endif /* CONFIG_BRANCH_TRACER */