trace_selftest.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. __init 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. #ifdef CONFIG_DYNAMIC_FTRACE
  534. if (ftrace_filter_param) {
  535. printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
  536. return 0;
  537. }
  538. #endif
  539. /* make sure msleep has been recorded */
  540. msleep(1);
  541. /* start the tracing */
  542. ftrace_enabled = 1;
  543. ret = tracer_init(trace, tr);
  544. if (ret) {
  545. warn_failed_init_tracer(trace, ret);
  546. goto out;
  547. }
  548. /* Sleep for a 1/10 of a second */
  549. msleep(100);
  550. /* stop the tracing. */
  551. tracing_stop();
  552. ftrace_enabled = 0;
  553. /* check the trace buffer */
  554. ret = trace_test_buffer(&tr->trace_buffer, &count);
  555. trace->reset(tr);
  556. tracing_start();
  557. if (!ret && !count) {
  558. printk(KERN_CONT ".. no entries found ..");
  559. ret = -1;
  560. goto out;
  561. }
  562. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  563. DYN_FTRACE_TEST_NAME);
  564. if (ret)
  565. goto out;
  566. ret = trace_selftest_function_recursion();
  567. if (ret)
  568. goto out;
  569. ret = trace_selftest_function_regs();
  570. out:
  571. ftrace_enabled = save_ftrace_enabled;
  572. /* kill ftrace totally if we failed */
  573. if (ret)
  574. ftrace_kill();
  575. return ret;
  576. }
  577. #endif /* CONFIG_FUNCTION_TRACER */
  578. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  579. /* Maximum number of functions to trace before diagnosing a hang */
  580. #define GRAPH_MAX_FUNC_TEST 100000000
  581. static unsigned int graph_hang_thresh;
  582. /* Wrap the real function entry probe to avoid possible hanging */
  583. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  584. {
  585. /* This is harmlessly racy, we want to approximately detect a hang */
  586. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  587. ftrace_graph_stop();
  588. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  589. if (ftrace_dump_on_oops) {
  590. ftrace_dump(DUMP_ALL);
  591. /* ftrace_dump() disables tracing */
  592. tracing_on();
  593. }
  594. return 0;
  595. }
  596. return trace_graph_entry(trace);
  597. }
  598. /*
  599. * Pretty much the same than for the function tracer from which the selftest
  600. * has been borrowed.
  601. */
  602. __init int
  603. trace_selftest_startup_function_graph(struct tracer *trace,
  604. struct trace_array *tr)
  605. {
  606. int ret;
  607. unsigned long count;
  608. #ifdef CONFIG_DYNAMIC_FTRACE
  609. if (ftrace_filter_param) {
  610. printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
  611. return 0;
  612. }
  613. #endif
  614. /*
  615. * Simulate the init() callback but we attach a watchdog callback
  616. * to detect and recover from possible hangs
  617. */
  618. tracing_reset_online_cpus(&tr->trace_buffer);
  619. set_graph_array(tr);
  620. ret = register_ftrace_graph(&trace_graph_return,
  621. &trace_graph_entry_watchdog);
  622. if (ret) {
  623. warn_failed_init_tracer(trace, ret);
  624. goto out;
  625. }
  626. tracing_start_cmdline_record();
  627. /* Sleep for a 1/10 of a second */
  628. msleep(100);
  629. /* Have we just recovered from a hang? */
  630. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  631. tracing_selftest_disabled = true;
  632. ret = -1;
  633. goto out;
  634. }
  635. tracing_stop();
  636. /* check the trace buffer */
  637. ret = trace_test_buffer(&tr->trace_buffer, &count);
  638. trace->reset(tr);
  639. tracing_start();
  640. if (!ret && !count) {
  641. printk(KERN_CONT ".. no entries found ..");
  642. ret = -1;
  643. goto out;
  644. }
  645. /* Don't test dynamic tracing, the function tracer already did */
  646. out:
  647. /* Stop it if we failed */
  648. if (ret)
  649. ftrace_graph_stop();
  650. return ret;
  651. }
  652. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  653. #ifdef CONFIG_IRQSOFF_TRACER
  654. int
  655. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  656. {
  657. unsigned long save_max = tracing_max_latency;
  658. unsigned long count;
  659. int ret;
  660. /* start the tracing */
  661. ret = tracer_init(trace, tr);
  662. if (ret) {
  663. warn_failed_init_tracer(trace, ret);
  664. return ret;
  665. }
  666. /* reset the max latency */
  667. tracing_max_latency = 0;
  668. /* disable interrupts for a bit */
  669. local_irq_disable();
  670. udelay(100);
  671. local_irq_enable();
  672. /*
  673. * Stop the tracer to avoid a warning subsequent
  674. * to buffer flipping failure because tracing_stop()
  675. * disables the tr and max buffers, making flipping impossible
  676. * in case of parallels max irqs off latencies.
  677. */
  678. trace->stop(tr);
  679. /* stop the tracing. */
  680. tracing_stop();
  681. /* check both trace buffers */
  682. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  683. if (!ret)
  684. ret = trace_test_buffer(&tr->max_buffer, &count);
  685. trace->reset(tr);
  686. tracing_start();
  687. if (!ret && !count) {
  688. printk(KERN_CONT ".. no entries found ..");
  689. ret = -1;
  690. }
  691. tracing_max_latency = save_max;
  692. return ret;
  693. }
  694. #endif /* CONFIG_IRQSOFF_TRACER */
  695. #ifdef CONFIG_PREEMPT_TRACER
  696. int
  697. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  698. {
  699. unsigned long save_max = tracing_max_latency;
  700. unsigned long count;
  701. int ret;
  702. /*
  703. * Now that the big kernel lock is no longer preemptable,
  704. * and this is called with the BKL held, it will always
  705. * fail. If preemption is already disabled, simply
  706. * pass the test. When the BKL is removed, or becomes
  707. * preemptible again, we will once again test this,
  708. * so keep it in.
  709. */
  710. if (preempt_count()) {
  711. printk(KERN_CONT "can not test ... force ");
  712. return 0;
  713. }
  714. /* start the tracing */
  715. ret = tracer_init(trace, tr);
  716. if (ret) {
  717. warn_failed_init_tracer(trace, ret);
  718. return ret;
  719. }
  720. /* reset the max latency */
  721. tracing_max_latency = 0;
  722. /* disable preemption for a bit */
  723. preempt_disable();
  724. udelay(100);
  725. preempt_enable();
  726. /*
  727. * Stop the tracer to avoid a warning subsequent
  728. * to buffer flipping failure because tracing_stop()
  729. * disables the tr and max buffers, making flipping impossible
  730. * in case of parallels max preempt off latencies.
  731. */
  732. trace->stop(tr);
  733. /* stop the tracing. */
  734. tracing_stop();
  735. /* check both trace buffers */
  736. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  737. if (!ret)
  738. ret = trace_test_buffer(&tr->max_buffer, &count);
  739. trace->reset(tr);
  740. tracing_start();
  741. if (!ret && !count) {
  742. printk(KERN_CONT ".. no entries found ..");
  743. ret = -1;
  744. }
  745. tracing_max_latency = save_max;
  746. return ret;
  747. }
  748. #endif /* CONFIG_PREEMPT_TRACER */
  749. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  750. int
  751. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  752. {
  753. unsigned long save_max = tracing_max_latency;
  754. unsigned long count;
  755. int ret;
  756. /*
  757. * Now that the big kernel lock is no longer preemptable,
  758. * and this is called with the BKL held, it will always
  759. * fail. If preemption is already disabled, simply
  760. * pass the test. When the BKL is removed, or becomes
  761. * preemptible again, we will once again test this,
  762. * so keep it in.
  763. */
  764. if (preempt_count()) {
  765. printk(KERN_CONT "can not test ... force ");
  766. return 0;
  767. }
  768. /* start the tracing */
  769. ret = tracer_init(trace, tr);
  770. if (ret) {
  771. warn_failed_init_tracer(trace, ret);
  772. goto out_no_start;
  773. }
  774. /* reset the max latency */
  775. tracing_max_latency = 0;
  776. /* disable preemption and interrupts for a bit */
  777. preempt_disable();
  778. local_irq_disable();
  779. udelay(100);
  780. preempt_enable();
  781. /* reverse the order of preempt vs irqs */
  782. local_irq_enable();
  783. /*
  784. * Stop the tracer to avoid a warning subsequent
  785. * to buffer flipping failure because tracing_stop()
  786. * disables the tr and max buffers, making flipping impossible
  787. * in case of parallels max irqs/preempt off latencies.
  788. */
  789. trace->stop(tr);
  790. /* stop the tracing. */
  791. tracing_stop();
  792. /* check both trace buffers */
  793. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  794. if (ret)
  795. goto out;
  796. ret = trace_test_buffer(&tr->max_buffer, &count);
  797. if (ret)
  798. goto out;
  799. if (!ret && !count) {
  800. printk(KERN_CONT ".. no entries found ..");
  801. ret = -1;
  802. goto out;
  803. }
  804. /* do the test by disabling interrupts first this time */
  805. tracing_max_latency = 0;
  806. tracing_start();
  807. trace->start(tr);
  808. preempt_disable();
  809. local_irq_disable();
  810. udelay(100);
  811. preempt_enable();
  812. /* reverse the order of preempt vs irqs */
  813. local_irq_enable();
  814. trace->stop(tr);
  815. /* stop the tracing. */
  816. tracing_stop();
  817. /* check both trace buffers */
  818. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  819. if (ret)
  820. goto out;
  821. ret = trace_test_buffer(&tr->max_buffer, &count);
  822. if (!ret && !count) {
  823. printk(KERN_CONT ".. no entries found ..");
  824. ret = -1;
  825. goto out;
  826. }
  827. out:
  828. tracing_start();
  829. out_no_start:
  830. trace->reset(tr);
  831. tracing_max_latency = save_max;
  832. return ret;
  833. }
  834. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  835. #ifdef CONFIG_NOP_TRACER
  836. int
  837. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  838. {
  839. /* What could possibly go wrong? */
  840. return 0;
  841. }
  842. #endif
  843. #ifdef CONFIG_SCHED_TRACER
  844. static int trace_wakeup_test_thread(void *data)
  845. {
  846. /* Make this a RT thread, doesn't need to be too high */
  847. static const struct sched_param param = { .sched_priority = 5 };
  848. struct completion *x = data;
  849. sched_setscheduler(current, SCHED_FIFO, &param);
  850. /* Make it know we have a new prio */
  851. complete(x);
  852. /* now go to sleep and let the test wake us up */
  853. set_current_state(TASK_INTERRUPTIBLE);
  854. schedule();
  855. complete(x);
  856. /* we are awake, now wait to disappear */
  857. while (!kthread_should_stop()) {
  858. /*
  859. * This is an RT task, do short sleeps to let
  860. * others run.
  861. */
  862. msleep(100);
  863. }
  864. return 0;
  865. }
  866. int
  867. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  868. {
  869. unsigned long save_max = tracing_max_latency;
  870. struct task_struct *p;
  871. struct completion isrt;
  872. unsigned long count;
  873. int ret;
  874. init_completion(&isrt);
  875. /* create a high prio thread */
  876. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  877. if (IS_ERR(p)) {
  878. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  879. return -1;
  880. }
  881. /* make sure the thread is running at an RT prio */
  882. wait_for_completion(&isrt);
  883. /* start the tracing */
  884. ret = tracer_init(trace, tr);
  885. if (ret) {
  886. warn_failed_init_tracer(trace, ret);
  887. return ret;
  888. }
  889. /* reset the max latency */
  890. tracing_max_latency = 0;
  891. while (p->on_rq) {
  892. /*
  893. * Sleep to make sure the RT thread is asleep too.
  894. * On virtual machines we can't rely on timings,
  895. * but we want to make sure this test still works.
  896. */
  897. msleep(100);
  898. }
  899. init_completion(&isrt);
  900. wake_up_process(p);
  901. /* Wait for the task to wake up */
  902. wait_for_completion(&isrt);
  903. /* stop the tracing. */
  904. tracing_stop();
  905. /* check both trace buffers */
  906. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  907. printk("ret = %d\n", ret);
  908. if (!ret)
  909. ret = trace_test_buffer(&tr->max_buffer, &count);
  910. trace->reset(tr);
  911. tracing_start();
  912. tracing_max_latency = save_max;
  913. /* kill the thread */
  914. kthread_stop(p);
  915. if (!ret && !count) {
  916. printk(KERN_CONT ".. no entries found ..");
  917. ret = -1;
  918. }
  919. return ret;
  920. }
  921. #endif /* CONFIG_SCHED_TRACER */
  922. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  923. int
  924. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  925. {
  926. unsigned long count;
  927. int ret;
  928. /* start the tracing */
  929. ret = tracer_init(trace, tr);
  930. if (ret) {
  931. warn_failed_init_tracer(trace, ret);
  932. return ret;
  933. }
  934. /* Sleep for a 1/10 of a second */
  935. msleep(100);
  936. /* stop the tracing. */
  937. tracing_stop();
  938. /* check the trace buffer */
  939. ret = trace_test_buffer(&tr->trace_buffer, &count);
  940. trace->reset(tr);
  941. tracing_start();
  942. if (!ret && !count) {
  943. printk(KERN_CONT ".. no entries found ..");
  944. ret = -1;
  945. }
  946. return ret;
  947. }
  948. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  949. #ifdef CONFIG_BRANCH_TRACER
  950. int
  951. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  952. {
  953. unsigned long count;
  954. int ret;
  955. /* start the tracing */
  956. ret = tracer_init(trace, tr);
  957. if (ret) {
  958. warn_failed_init_tracer(trace, ret);
  959. return ret;
  960. }
  961. /* Sleep for a 1/10 of a second */
  962. msleep(100);
  963. /* stop the tracing. */
  964. tracing_stop();
  965. /* check the trace buffer */
  966. ret = trace_test_buffer(&tr->trace_buffer, &count);
  967. trace->reset(tr);
  968. tracing_start();
  969. if (!ret && !count) {
  970. printk(KERN_CONT ".. no entries found ..");
  971. ret = -1;
  972. }
  973. return ret;
  974. }
  975. #endif /* CONFIG_BRANCH_TRACER */