trace_selftest.c 26 KB

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