trace_selftest.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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_array *tr, 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(tr->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_array *tr, 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(tr->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(tr, 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, &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, &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. trace_selftest_recursion_cnt++;
  345. DYN_FTRACE_TEST_NAME();
  346. }
  347. static void trace_selftest_test_recursion_safe_func(unsigned long ip,
  348. unsigned long pip,
  349. struct ftrace_ops *op,
  350. struct pt_regs *pt_regs)
  351. {
  352. /*
  353. * We said we would provide our own recursion. By calling
  354. * this function again, we should recurse back into this function
  355. * and count again. But this only happens if the arch supports
  356. * all of ftrace features and nothing else is using the function
  357. * tracing utility.
  358. */
  359. if (trace_selftest_recursion_cnt++)
  360. return;
  361. DYN_FTRACE_TEST_NAME();
  362. }
  363. static struct ftrace_ops test_rec_probe = {
  364. .func = trace_selftest_test_recursion_func,
  365. };
  366. static struct ftrace_ops test_recsafe_probe = {
  367. .func = trace_selftest_test_recursion_safe_func,
  368. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  369. };
  370. static int
  371. trace_selftest_function_recursion(void)
  372. {
  373. int save_ftrace_enabled = ftrace_enabled;
  374. char *func_name;
  375. int len;
  376. int ret;
  377. int cnt;
  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. /*
  420. * If arch supports all ftrace features, and no other task
  421. * was on the list, we should be fine.
  422. */
  423. if (!ftrace_nr_registered_ops() && !FTRACE_FORCE_LIST_FUNC)
  424. cnt = 2; /* Should have recursed */
  425. else
  426. cnt = 1;
  427. ret = -1;
  428. if (trace_selftest_recursion_cnt != cnt) {
  429. pr_cont("*callback not called expected %d times (%d)* ",
  430. cnt, trace_selftest_recursion_cnt);
  431. goto out;
  432. }
  433. ret = 0;
  434. out:
  435. ftrace_enabled = save_ftrace_enabled;
  436. return ret;
  437. }
  438. #else
  439. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  440. # define trace_selftest_function_recursion() ({ 0; })
  441. #endif /* CONFIG_DYNAMIC_FTRACE */
  442. static enum {
  443. TRACE_SELFTEST_REGS_START,
  444. TRACE_SELFTEST_REGS_FOUND,
  445. TRACE_SELFTEST_REGS_NOT_FOUND,
  446. } trace_selftest_regs_stat;
  447. static void trace_selftest_test_regs_func(unsigned long ip,
  448. unsigned long pip,
  449. struct ftrace_ops *op,
  450. struct pt_regs *pt_regs)
  451. {
  452. if (pt_regs)
  453. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_FOUND;
  454. else
  455. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_NOT_FOUND;
  456. }
  457. static struct ftrace_ops test_regs_probe = {
  458. .func = trace_selftest_test_regs_func,
  459. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_SAVE_REGS,
  460. };
  461. static int
  462. trace_selftest_function_regs(void)
  463. {
  464. int save_ftrace_enabled = ftrace_enabled;
  465. char *func_name;
  466. int len;
  467. int ret;
  468. int supported = 0;
  469. #ifdef ARCH_SUPPORTS_FTRACE_SAVE_REGS
  470. supported = 1;
  471. #endif
  472. /* The previous test PASSED */
  473. pr_cont("PASSED\n");
  474. pr_info("Testing ftrace regs%s: ",
  475. !supported ? "(no arch support)" : "");
  476. /* enable tracing, and record the filter function */
  477. ftrace_enabled = 1;
  478. /* Handle PPC64 '.' name */
  479. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  480. len = strlen(func_name);
  481. ret = ftrace_set_filter(&test_regs_probe, func_name, len, 1);
  482. /*
  483. * If DYNAMIC_FTRACE is not set, then we just trace all functions.
  484. * This test really doesn't care.
  485. */
  486. if (ret && ret != -ENODEV) {
  487. pr_cont("*Could not set filter* ");
  488. goto out;
  489. }
  490. ret = register_ftrace_function(&test_regs_probe);
  491. /*
  492. * Now if the arch does not support passing regs, then this should
  493. * have failed.
  494. */
  495. if (!supported) {
  496. if (!ret) {
  497. pr_cont("*registered save-regs without arch support* ");
  498. goto out;
  499. }
  500. test_regs_probe.flags |= FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
  501. ret = register_ftrace_function(&test_regs_probe);
  502. }
  503. if (ret) {
  504. pr_cont("*could not register callback* ");
  505. goto out;
  506. }
  507. DYN_FTRACE_TEST_NAME();
  508. unregister_ftrace_function(&test_regs_probe);
  509. ret = -1;
  510. switch (trace_selftest_regs_stat) {
  511. case TRACE_SELFTEST_REGS_START:
  512. pr_cont("*callback never called* ");
  513. goto out;
  514. case TRACE_SELFTEST_REGS_FOUND:
  515. if (supported)
  516. break;
  517. pr_cont("*callback received regs without arch support* ");
  518. goto out;
  519. case TRACE_SELFTEST_REGS_NOT_FOUND:
  520. if (!supported)
  521. break;
  522. pr_cont("*callback received NULL regs* ");
  523. goto out;
  524. }
  525. ret = 0;
  526. out:
  527. ftrace_enabled = save_ftrace_enabled;
  528. return ret;
  529. }
  530. /*
  531. * Simple verification test of ftrace function tracer.
  532. * Enable ftrace, sleep 1/10 second, and then read the trace
  533. * buffer to see if all is in order.
  534. */
  535. int
  536. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  537. {
  538. int save_ftrace_enabled = ftrace_enabled;
  539. unsigned long count;
  540. int ret;
  541. /* make sure msleep has been recorded */
  542. msleep(1);
  543. /* start the tracing */
  544. ftrace_enabled = 1;
  545. ret = tracer_init(trace, tr);
  546. if (ret) {
  547. warn_failed_init_tracer(trace, ret);
  548. goto out;
  549. }
  550. /* Sleep for a 1/10 of a second */
  551. msleep(100);
  552. /* stop the tracing. */
  553. tracing_stop();
  554. ftrace_enabled = 0;
  555. /* check the trace buffer */
  556. ret = trace_test_buffer(tr, &count);
  557. trace->reset(tr);
  558. tracing_start();
  559. if (!ret && !count) {
  560. printk(KERN_CONT ".. no entries found ..");
  561. ret = -1;
  562. goto out;
  563. }
  564. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  565. DYN_FTRACE_TEST_NAME);
  566. if (ret)
  567. goto out;
  568. ret = trace_selftest_function_recursion();
  569. if (ret)
  570. goto out;
  571. ret = trace_selftest_function_regs();
  572. out:
  573. ftrace_enabled = save_ftrace_enabled;
  574. /* kill ftrace totally if we failed */
  575. if (ret)
  576. ftrace_kill();
  577. return ret;
  578. }
  579. #endif /* CONFIG_FUNCTION_TRACER */
  580. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  581. /* Maximum number of functions to trace before diagnosing a hang */
  582. #define GRAPH_MAX_FUNC_TEST 100000000
  583. static void
  584. __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode);
  585. static unsigned int graph_hang_thresh;
  586. /* Wrap the real function entry probe to avoid possible hanging */
  587. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  588. {
  589. /* This is harmlessly racy, we want to approximately detect a hang */
  590. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  591. ftrace_graph_stop();
  592. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  593. if (ftrace_dump_on_oops)
  594. __ftrace_dump(false, DUMP_ALL);
  595. return 0;
  596. }
  597. return trace_graph_entry(trace);
  598. }
  599. /*
  600. * Pretty much the same than for the function tracer from which the selftest
  601. * has been borrowed.
  602. */
  603. int
  604. trace_selftest_startup_function_graph(struct tracer *trace,
  605. struct trace_array *tr)
  606. {
  607. int ret;
  608. unsigned long count;
  609. /*
  610. * Simulate the init() callback but we attach a watchdog callback
  611. * to detect and recover from possible hangs
  612. */
  613. tracing_reset_online_cpus(tr);
  614. set_graph_array(tr);
  615. ret = register_ftrace_graph(&trace_graph_return,
  616. &trace_graph_entry_watchdog);
  617. if (ret) {
  618. warn_failed_init_tracer(trace, ret);
  619. goto out;
  620. }
  621. tracing_start_cmdline_record();
  622. /* Sleep for a 1/10 of a second */
  623. msleep(100);
  624. /* Have we just recovered from a hang? */
  625. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  626. tracing_selftest_disabled = true;
  627. ret = -1;
  628. goto out;
  629. }
  630. tracing_stop();
  631. /* check the trace buffer */
  632. ret = trace_test_buffer(tr, &count);
  633. trace->reset(tr);
  634. tracing_start();
  635. if (!ret && !count) {
  636. printk(KERN_CONT ".. no entries found ..");
  637. ret = -1;
  638. goto out;
  639. }
  640. /* Don't test dynamic tracing, the function tracer already did */
  641. out:
  642. /* Stop it if we failed */
  643. if (ret)
  644. ftrace_graph_stop();
  645. return ret;
  646. }
  647. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  648. #ifdef CONFIG_IRQSOFF_TRACER
  649. int
  650. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  651. {
  652. unsigned long save_max = tracing_max_latency;
  653. unsigned long count;
  654. int ret;
  655. /* start the tracing */
  656. ret = tracer_init(trace, tr);
  657. if (ret) {
  658. warn_failed_init_tracer(trace, ret);
  659. return ret;
  660. }
  661. /* reset the max latency */
  662. tracing_max_latency = 0;
  663. /* disable interrupts for a bit */
  664. local_irq_disable();
  665. udelay(100);
  666. local_irq_enable();
  667. /*
  668. * Stop the tracer to avoid a warning subsequent
  669. * to buffer flipping failure because tracing_stop()
  670. * disables the tr and max buffers, making flipping impossible
  671. * in case of parallels max irqs off latencies.
  672. */
  673. trace->stop(tr);
  674. /* stop the tracing. */
  675. tracing_stop();
  676. /* check both trace buffers */
  677. ret = trace_test_buffer(tr, NULL);
  678. if (!ret)
  679. ret = trace_test_buffer(&max_tr, &count);
  680. trace->reset(tr);
  681. tracing_start();
  682. if (!ret && !count) {
  683. printk(KERN_CONT ".. no entries found ..");
  684. ret = -1;
  685. }
  686. tracing_max_latency = save_max;
  687. return ret;
  688. }
  689. #endif /* CONFIG_IRQSOFF_TRACER */
  690. #ifdef CONFIG_PREEMPT_TRACER
  691. int
  692. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  693. {
  694. unsigned long save_max = tracing_max_latency;
  695. unsigned long count;
  696. int ret;
  697. /*
  698. * Now that the big kernel lock is no longer preemptable,
  699. * and this is called with the BKL held, it will always
  700. * fail. If preemption is already disabled, simply
  701. * pass the test. When the BKL is removed, or becomes
  702. * preemptible again, we will once again test this,
  703. * so keep it in.
  704. */
  705. if (preempt_count()) {
  706. printk(KERN_CONT "can not test ... force ");
  707. return 0;
  708. }
  709. /* start the tracing */
  710. ret = tracer_init(trace, tr);
  711. if (ret) {
  712. warn_failed_init_tracer(trace, ret);
  713. return ret;
  714. }
  715. /* reset the max latency */
  716. tracing_max_latency = 0;
  717. /* disable preemption for a bit */
  718. preempt_disable();
  719. udelay(100);
  720. preempt_enable();
  721. /*
  722. * Stop the tracer to avoid a warning subsequent
  723. * to buffer flipping failure because tracing_stop()
  724. * disables the tr and max buffers, making flipping impossible
  725. * in case of parallels max preempt off latencies.
  726. */
  727. trace->stop(tr);
  728. /* stop the tracing. */
  729. tracing_stop();
  730. /* check both trace buffers */
  731. ret = trace_test_buffer(tr, NULL);
  732. if (!ret)
  733. ret = trace_test_buffer(&max_tr, &count);
  734. trace->reset(tr);
  735. tracing_start();
  736. if (!ret && !count) {
  737. printk(KERN_CONT ".. no entries found ..");
  738. ret = -1;
  739. }
  740. tracing_max_latency = save_max;
  741. return ret;
  742. }
  743. #endif /* CONFIG_PREEMPT_TRACER */
  744. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  745. int
  746. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  747. {
  748. unsigned long save_max = tracing_max_latency;
  749. unsigned long count;
  750. int ret;
  751. /*
  752. * Now that the big kernel lock is no longer preemptable,
  753. * and this is called with the BKL held, it will always
  754. * fail. If preemption is already disabled, simply
  755. * pass the test. When the BKL is removed, or becomes
  756. * preemptible again, we will once again test this,
  757. * so keep it in.
  758. */
  759. if (preempt_count()) {
  760. printk(KERN_CONT "can not test ... force ");
  761. return 0;
  762. }
  763. /* start the tracing */
  764. ret = tracer_init(trace, tr);
  765. if (ret) {
  766. warn_failed_init_tracer(trace, ret);
  767. goto out_no_start;
  768. }
  769. /* reset the max latency */
  770. tracing_max_latency = 0;
  771. /* disable preemption and interrupts for a bit */
  772. preempt_disable();
  773. local_irq_disable();
  774. udelay(100);
  775. preempt_enable();
  776. /* reverse the order of preempt vs irqs */
  777. local_irq_enable();
  778. /*
  779. * Stop the tracer to avoid a warning subsequent
  780. * to buffer flipping failure because tracing_stop()
  781. * disables the tr and max buffers, making flipping impossible
  782. * in case of parallels max irqs/preempt off latencies.
  783. */
  784. trace->stop(tr);
  785. /* stop the tracing. */
  786. tracing_stop();
  787. /* check both trace buffers */
  788. ret = trace_test_buffer(tr, NULL);
  789. if (ret)
  790. goto out;
  791. ret = trace_test_buffer(&max_tr, &count);
  792. if (ret)
  793. goto out;
  794. if (!ret && !count) {
  795. printk(KERN_CONT ".. no entries found ..");
  796. ret = -1;
  797. goto out;
  798. }
  799. /* do the test by disabling interrupts first this time */
  800. tracing_max_latency = 0;
  801. tracing_start();
  802. trace->start(tr);
  803. preempt_disable();
  804. local_irq_disable();
  805. udelay(100);
  806. preempt_enable();
  807. /* reverse the order of preempt vs irqs */
  808. local_irq_enable();
  809. trace->stop(tr);
  810. /* stop the tracing. */
  811. tracing_stop();
  812. /* check both trace buffers */
  813. ret = trace_test_buffer(tr, NULL);
  814. if (ret)
  815. goto out;
  816. ret = trace_test_buffer(&max_tr, &count);
  817. if (!ret && !count) {
  818. printk(KERN_CONT ".. no entries found ..");
  819. ret = -1;
  820. goto out;
  821. }
  822. out:
  823. tracing_start();
  824. out_no_start:
  825. trace->reset(tr);
  826. tracing_max_latency = save_max;
  827. return ret;
  828. }
  829. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  830. #ifdef CONFIG_NOP_TRACER
  831. int
  832. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  833. {
  834. /* What could possibly go wrong? */
  835. return 0;
  836. }
  837. #endif
  838. #ifdef CONFIG_SCHED_TRACER
  839. static int trace_wakeup_test_thread(void *data)
  840. {
  841. /* Make this a RT thread, doesn't need to be too high */
  842. static const struct sched_param param = { .sched_priority = 5 };
  843. struct completion *x = data;
  844. sched_setscheduler(current, SCHED_FIFO, &param);
  845. /* Make it know we have a new prio */
  846. complete(x);
  847. /* now go to sleep and let the test wake us up */
  848. set_current_state(TASK_INTERRUPTIBLE);
  849. schedule();
  850. complete(x);
  851. /* we are awake, now wait to disappear */
  852. while (!kthread_should_stop()) {
  853. /*
  854. * This is an RT task, do short sleeps to let
  855. * others run.
  856. */
  857. msleep(100);
  858. }
  859. return 0;
  860. }
  861. int
  862. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  863. {
  864. unsigned long save_max = tracing_max_latency;
  865. struct task_struct *p;
  866. struct completion isrt;
  867. unsigned long count;
  868. int ret;
  869. init_completion(&isrt);
  870. /* create a high prio thread */
  871. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  872. if (IS_ERR(p)) {
  873. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  874. return -1;
  875. }
  876. /* make sure the thread is running at an RT prio */
  877. wait_for_completion(&isrt);
  878. /* start the tracing */
  879. ret = tracer_init(trace, tr);
  880. if (ret) {
  881. warn_failed_init_tracer(trace, ret);
  882. return ret;
  883. }
  884. /* reset the max latency */
  885. tracing_max_latency = 0;
  886. while (p->on_rq) {
  887. /*
  888. * Sleep to make sure the RT thread is asleep too.
  889. * On virtual machines we can't rely on timings,
  890. * but we want to make sure this test still works.
  891. */
  892. msleep(100);
  893. }
  894. init_completion(&isrt);
  895. wake_up_process(p);
  896. /* Wait for the task to wake up */
  897. wait_for_completion(&isrt);
  898. /* stop the tracing. */
  899. tracing_stop();
  900. /* check both trace buffers */
  901. ret = trace_test_buffer(tr, NULL);
  902. printk("ret = %d\n", ret);
  903. if (!ret)
  904. ret = trace_test_buffer(&max_tr, &count);
  905. trace->reset(tr);
  906. tracing_start();
  907. tracing_max_latency = save_max;
  908. /* kill the thread */
  909. kthread_stop(p);
  910. if (!ret && !count) {
  911. printk(KERN_CONT ".. no entries found ..");
  912. ret = -1;
  913. }
  914. return ret;
  915. }
  916. #endif /* CONFIG_SCHED_TRACER */
  917. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  918. int
  919. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  920. {
  921. unsigned long count;
  922. int ret;
  923. /* start the tracing */
  924. ret = tracer_init(trace, tr);
  925. if (ret) {
  926. warn_failed_init_tracer(trace, ret);
  927. return ret;
  928. }
  929. /* Sleep for a 1/10 of a second */
  930. msleep(100);
  931. /* stop the tracing. */
  932. tracing_stop();
  933. /* check the trace buffer */
  934. ret = trace_test_buffer(tr, &count);
  935. trace->reset(tr);
  936. tracing_start();
  937. if (!ret && !count) {
  938. printk(KERN_CONT ".. no entries found ..");
  939. ret = -1;
  940. }
  941. return ret;
  942. }
  943. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  944. #ifdef CONFIG_BRANCH_TRACER
  945. int
  946. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  947. {
  948. unsigned long count;
  949. int ret;
  950. /* start the tracing */
  951. ret = tracer_init(trace, tr);
  952. if (ret) {
  953. warn_failed_init_tracer(trace, ret);
  954. return ret;
  955. }
  956. /* Sleep for a 1/10 of a second */
  957. msleep(100);
  958. /* stop the tracing. */
  959. tracing_stop();
  960. /* check the trace buffer */
  961. ret = trace_test_buffer(tr, &count);
  962. trace->reset(tr);
  963. tracing_start();
  964. if (!ret && !count) {
  965. printk(KERN_CONT ".. no entries found ..");
  966. ret = -1;
  967. }
  968. return ret;
  969. }
  970. #endif /* CONFIG_BRANCH_TRACER */