trace_selftest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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_SPECIAL:
  15. case TRACE_BRANCH:
  16. case TRACE_GRAPH_ENT:
  17. case TRACE_GRAPH_RET:
  18. case TRACE_HW_BRANCHES:
  19. case TRACE_KSYM:
  20. return 1;
  21. }
  22. return 0;
  23. }
  24. static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
  25. {
  26. struct ring_buffer_event *event;
  27. struct trace_entry *entry;
  28. unsigned int loops = 0;
  29. while ((event = ring_buffer_consume(tr->buffer, cpu, NULL, NULL))) {
  30. entry = ring_buffer_event_data(event);
  31. /*
  32. * The ring buffer is a size of trace_buf_size, if
  33. * we loop more than the size, there's something wrong
  34. * with the ring buffer.
  35. */
  36. if (loops++ > trace_buf_size) {
  37. printk(KERN_CONT ".. bad ring buffer ");
  38. goto failed;
  39. }
  40. if (!trace_valid_entry(entry)) {
  41. printk(KERN_CONT ".. invalid entry %d ",
  42. entry->type);
  43. goto failed;
  44. }
  45. }
  46. return 0;
  47. failed:
  48. /* disable tracing */
  49. tracing_disabled = 1;
  50. printk(KERN_CONT ".. corrupted trace buffer .. ");
  51. return -1;
  52. }
  53. /*
  54. * Test the trace buffer to see if all the elements
  55. * are still sane.
  56. */
  57. static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
  58. {
  59. unsigned long flags, cnt = 0;
  60. int cpu, ret = 0;
  61. /* Don't allow flipping of max traces now */
  62. local_irq_save(flags);
  63. arch_spin_lock(&ftrace_max_lock);
  64. cnt = ring_buffer_entries(tr->buffer);
  65. /*
  66. * The trace_test_buffer_cpu runs a while loop to consume all data.
  67. * If the calling tracer is broken, and is constantly filling
  68. * the buffer, this will run forever, and hard lock the box.
  69. * We disable the ring buffer while we do this test to prevent
  70. * a hard lock up.
  71. */
  72. tracing_off();
  73. for_each_possible_cpu(cpu) {
  74. ret = trace_test_buffer_cpu(tr, cpu);
  75. if (ret)
  76. break;
  77. }
  78. tracing_on();
  79. arch_spin_unlock(&ftrace_max_lock);
  80. local_irq_restore(flags);
  81. if (count)
  82. *count = cnt;
  83. return ret;
  84. }
  85. static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
  86. {
  87. printk(KERN_WARNING "Failed to init %s tracer, init returned %d\n",
  88. trace->name, init_ret);
  89. }
  90. #ifdef CONFIG_FUNCTION_TRACER
  91. #ifdef CONFIG_DYNAMIC_FTRACE
  92. /* Test dynamic code modification and ftrace filters */
  93. int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
  94. struct trace_array *tr,
  95. int (*func)(void))
  96. {
  97. int save_ftrace_enabled = ftrace_enabled;
  98. int save_tracer_enabled = tracer_enabled;
  99. unsigned long count;
  100. char *func_name;
  101. int ret;
  102. /* The ftrace test PASSED */
  103. printk(KERN_CONT "PASSED\n");
  104. pr_info("Testing dynamic ftrace: ");
  105. /* enable tracing, and record the filter function */
  106. ftrace_enabled = 1;
  107. tracer_enabled = 1;
  108. /* passed in by parameter to fool gcc from optimizing */
  109. func();
  110. /*
  111. * Some archs *cough*PowerPC*cough* add characters to the
  112. * start of the function names. We simply put a '*' to
  113. * accommodate them.
  114. */
  115. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  116. /* filter only on our function */
  117. ftrace_set_filter(func_name, strlen(func_name), 1);
  118. /* enable tracing */
  119. ret = tracer_init(trace, tr);
  120. if (ret) {
  121. warn_failed_init_tracer(trace, ret);
  122. goto out;
  123. }
  124. /* Sleep for a 1/10 of a second */
  125. msleep(100);
  126. /* we should have nothing in the buffer */
  127. ret = trace_test_buffer(tr, &count);
  128. if (ret)
  129. goto out;
  130. if (count) {
  131. ret = -1;
  132. printk(KERN_CONT ".. filter did not filter .. ");
  133. goto out;
  134. }
  135. /* call our function again */
  136. func();
  137. /* sleep again */
  138. msleep(100);
  139. /* stop the tracing. */
  140. tracing_stop();
  141. ftrace_enabled = 0;
  142. /* check the trace buffer */
  143. ret = trace_test_buffer(tr, &count);
  144. trace->reset(tr);
  145. tracing_start();
  146. /* we should only have one item */
  147. if (!ret && count != 1) {
  148. printk(KERN_CONT ".. filter failed count=%ld ..", count);
  149. ret = -1;
  150. goto out;
  151. }
  152. out:
  153. ftrace_enabled = save_ftrace_enabled;
  154. tracer_enabled = save_tracer_enabled;
  155. /* Enable tracing on all functions again */
  156. ftrace_set_filter(NULL, 0, 1);
  157. return ret;
  158. }
  159. #else
  160. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  161. #endif /* CONFIG_DYNAMIC_FTRACE */
  162. /*
  163. * Simple verification test of ftrace function tracer.
  164. * Enable ftrace, sleep 1/10 second, and then read the trace
  165. * buffer to see if all is in order.
  166. */
  167. int
  168. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  169. {
  170. int save_ftrace_enabled = ftrace_enabled;
  171. int save_tracer_enabled = tracer_enabled;
  172. unsigned long count;
  173. int ret;
  174. /* make sure msleep has been recorded */
  175. msleep(1);
  176. /* start the tracing */
  177. ftrace_enabled = 1;
  178. tracer_enabled = 1;
  179. ret = tracer_init(trace, tr);
  180. if (ret) {
  181. warn_failed_init_tracer(trace, ret);
  182. goto out;
  183. }
  184. /* Sleep for a 1/10 of a second */
  185. msleep(100);
  186. /* stop the tracing. */
  187. tracing_stop();
  188. ftrace_enabled = 0;
  189. /* check the trace buffer */
  190. ret = trace_test_buffer(tr, &count);
  191. trace->reset(tr);
  192. tracing_start();
  193. if (!ret && !count) {
  194. printk(KERN_CONT ".. no entries found ..");
  195. ret = -1;
  196. goto out;
  197. }
  198. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  199. DYN_FTRACE_TEST_NAME);
  200. out:
  201. ftrace_enabled = save_ftrace_enabled;
  202. tracer_enabled = save_tracer_enabled;
  203. /* kill ftrace totally if we failed */
  204. if (ret)
  205. ftrace_kill();
  206. return ret;
  207. }
  208. #endif /* CONFIG_FUNCTION_TRACER */
  209. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  210. /* Maximum number of functions to trace before diagnosing a hang */
  211. #define GRAPH_MAX_FUNC_TEST 100000000
  212. static void
  213. __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode);
  214. static unsigned int graph_hang_thresh;
  215. /* Wrap the real function entry probe to avoid possible hanging */
  216. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  217. {
  218. /* This is harmlessly racy, we want to approximately detect a hang */
  219. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  220. ftrace_graph_stop();
  221. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  222. if (ftrace_dump_on_oops)
  223. __ftrace_dump(false, DUMP_ALL);
  224. return 0;
  225. }
  226. return trace_graph_entry(trace);
  227. }
  228. /*
  229. * Pretty much the same than for the function tracer from which the selftest
  230. * has been borrowed.
  231. */
  232. int
  233. trace_selftest_startup_function_graph(struct tracer *trace,
  234. struct trace_array *tr)
  235. {
  236. int ret;
  237. unsigned long count;
  238. /*
  239. * Simulate the init() callback but we attach a watchdog callback
  240. * to detect and recover from possible hangs
  241. */
  242. tracing_reset_online_cpus(tr);
  243. set_graph_array(tr);
  244. ret = register_ftrace_graph(&trace_graph_return,
  245. &trace_graph_entry_watchdog);
  246. if (ret) {
  247. warn_failed_init_tracer(trace, ret);
  248. goto out;
  249. }
  250. tracing_start_cmdline_record();
  251. /* Sleep for a 1/10 of a second */
  252. msleep(100);
  253. /* Have we just recovered from a hang? */
  254. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  255. tracing_selftest_disabled = true;
  256. ret = -1;
  257. goto out;
  258. }
  259. tracing_stop();
  260. /* check the trace buffer */
  261. ret = trace_test_buffer(tr, &count);
  262. trace->reset(tr);
  263. tracing_start();
  264. if (!ret && !count) {
  265. printk(KERN_CONT ".. no entries found ..");
  266. ret = -1;
  267. goto out;
  268. }
  269. /* Don't test dynamic tracing, the function tracer already did */
  270. out:
  271. /* Stop it if we failed */
  272. if (ret)
  273. ftrace_graph_stop();
  274. return ret;
  275. }
  276. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  277. #ifdef CONFIG_IRQSOFF_TRACER
  278. int
  279. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  280. {
  281. unsigned long save_max = tracing_max_latency;
  282. unsigned long count;
  283. int ret;
  284. /* start the tracing */
  285. ret = tracer_init(trace, tr);
  286. if (ret) {
  287. warn_failed_init_tracer(trace, ret);
  288. return ret;
  289. }
  290. /* reset the max latency */
  291. tracing_max_latency = 0;
  292. /* disable interrupts for a bit */
  293. local_irq_disable();
  294. udelay(100);
  295. local_irq_enable();
  296. /*
  297. * Stop the tracer to avoid a warning subsequent
  298. * to buffer flipping failure because tracing_stop()
  299. * disables the tr and max buffers, making flipping impossible
  300. * in case of parallels max irqs off latencies.
  301. */
  302. trace->stop(tr);
  303. /* stop the tracing. */
  304. tracing_stop();
  305. /* check both trace buffers */
  306. ret = trace_test_buffer(tr, NULL);
  307. if (!ret)
  308. ret = trace_test_buffer(&max_tr, &count);
  309. trace->reset(tr);
  310. tracing_start();
  311. if (!ret && !count) {
  312. printk(KERN_CONT ".. no entries found ..");
  313. ret = -1;
  314. }
  315. tracing_max_latency = save_max;
  316. return ret;
  317. }
  318. #endif /* CONFIG_IRQSOFF_TRACER */
  319. #ifdef CONFIG_PREEMPT_TRACER
  320. int
  321. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  322. {
  323. unsigned long save_max = tracing_max_latency;
  324. unsigned long count;
  325. int ret;
  326. /*
  327. * Now that the big kernel lock is no longer preemptable,
  328. * and this is called with the BKL held, it will always
  329. * fail. If preemption is already disabled, simply
  330. * pass the test. When the BKL is removed, or becomes
  331. * preemptible again, we will once again test this,
  332. * so keep it in.
  333. */
  334. if (preempt_count()) {
  335. printk(KERN_CONT "can not test ... force ");
  336. return 0;
  337. }
  338. /* start the tracing */
  339. ret = tracer_init(trace, tr);
  340. if (ret) {
  341. warn_failed_init_tracer(trace, ret);
  342. return ret;
  343. }
  344. /* reset the max latency */
  345. tracing_max_latency = 0;
  346. /* disable preemption for a bit */
  347. preempt_disable();
  348. udelay(100);
  349. preempt_enable();
  350. /*
  351. * Stop the tracer to avoid a warning subsequent
  352. * to buffer flipping failure because tracing_stop()
  353. * disables the tr and max buffers, making flipping impossible
  354. * in case of parallels max preempt off latencies.
  355. */
  356. trace->stop(tr);
  357. /* stop the tracing. */
  358. tracing_stop();
  359. /* check both trace buffers */
  360. ret = trace_test_buffer(tr, NULL);
  361. if (!ret)
  362. ret = trace_test_buffer(&max_tr, &count);
  363. trace->reset(tr);
  364. tracing_start();
  365. if (!ret && !count) {
  366. printk(KERN_CONT ".. no entries found ..");
  367. ret = -1;
  368. }
  369. tracing_max_latency = save_max;
  370. return ret;
  371. }
  372. #endif /* CONFIG_PREEMPT_TRACER */
  373. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  374. int
  375. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  376. {
  377. unsigned long save_max = tracing_max_latency;
  378. unsigned long count;
  379. int ret;
  380. /*
  381. * Now that the big kernel lock is no longer preemptable,
  382. * and this is called with the BKL held, it will always
  383. * fail. If preemption is already disabled, simply
  384. * pass the test. When the BKL is removed, or becomes
  385. * preemptible again, we will once again test this,
  386. * so keep it in.
  387. */
  388. if (preempt_count()) {
  389. printk(KERN_CONT "can not test ... force ");
  390. return 0;
  391. }
  392. /* start the tracing */
  393. ret = tracer_init(trace, tr);
  394. if (ret) {
  395. warn_failed_init_tracer(trace, ret);
  396. goto out_no_start;
  397. }
  398. /* reset the max latency */
  399. tracing_max_latency = 0;
  400. /* disable preemption and interrupts for a bit */
  401. preempt_disable();
  402. local_irq_disable();
  403. udelay(100);
  404. preempt_enable();
  405. /* reverse the order of preempt vs irqs */
  406. local_irq_enable();
  407. /*
  408. * Stop the tracer to avoid a warning subsequent
  409. * to buffer flipping failure because tracing_stop()
  410. * disables the tr and max buffers, making flipping impossible
  411. * in case of parallels max irqs/preempt off latencies.
  412. */
  413. trace->stop(tr);
  414. /* stop the tracing. */
  415. tracing_stop();
  416. /* check both trace buffers */
  417. ret = trace_test_buffer(tr, NULL);
  418. if (ret)
  419. goto out;
  420. ret = trace_test_buffer(&max_tr, &count);
  421. if (ret)
  422. goto out;
  423. if (!ret && !count) {
  424. printk(KERN_CONT ".. no entries found ..");
  425. ret = -1;
  426. goto out;
  427. }
  428. /* do the test by disabling interrupts first this time */
  429. tracing_max_latency = 0;
  430. tracing_start();
  431. trace->start(tr);
  432. preempt_disable();
  433. local_irq_disable();
  434. udelay(100);
  435. preempt_enable();
  436. /* reverse the order of preempt vs irqs */
  437. local_irq_enable();
  438. trace->stop(tr);
  439. /* stop the tracing. */
  440. tracing_stop();
  441. /* check both trace buffers */
  442. ret = trace_test_buffer(tr, NULL);
  443. if (ret)
  444. goto out;
  445. ret = trace_test_buffer(&max_tr, &count);
  446. if (!ret && !count) {
  447. printk(KERN_CONT ".. no entries found ..");
  448. ret = -1;
  449. goto out;
  450. }
  451. out:
  452. tracing_start();
  453. out_no_start:
  454. trace->reset(tr);
  455. tracing_max_latency = save_max;
  456. return ret;
  457. }
  458. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  459. #ifdef CONFIG_NOP_TRACER
  460. int
  461. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  462. {
  463. /* What could possibly go wrong? */
  464. return 0;
  465. }
  466. #endif
  467. #ifdef CONFIG_SCHED_TRACER
  468. static int trace_wakeup_test_thread(void *data)
  469. {
  470. /* Make this a RT thread, doesn't need to be too high */
  471. struct sched_param param = { .sched_priority = 5 };
  472. struct completion *x = data;
  473. sched_setscheduler(current, SCHED_FIFO, &param);
  474. /* Make it know we have a new prio */
  475. complete(x);
  476. /* now go to sleep and let the test wake us up */
  477. set_current_state(TASK_INTERRUPTIBLE);
  478. schedule();
  479. /* we are awake, now wait to disappear */
  480. while (!kthread_should_stop()) {
  481. /*
  482. * This is an RT task, do short sleeps to let
  483. * others run.
  484. */
  485. msleep(100);
  486. }
  487. return 0;
  488. }
  489. int
  490. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  491. {
  492. unsigned long save_max = tracing_max_latency;
  493. struct task_struct *p;
  494. struct completion isrt;
  495. unsigned long count;
  496. int ret;
  497. init_completion(&isrt);
  498. /* create a high prio thread */
  499. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  500. if (IS_ERR(p)) {
  501. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  502. return -1;
  503. }
  504. /* make sure the thread is running at an RT prio */
  505. wait_for_completion(&isrt);
  506. /* start the tracing */
  507. ret = tracer_init(trace, tr);
  508. if (ret) {
  509. warn_failed_init_tracer(trace, ret);
  510. return ret;
  511. }
  512. /* reset the max latency */
  513. tracing_max_latency = 0;
  514. /* sleep to let the RT thread sleep too */
  515. msleep(100);
  516. /*
  517. * Yes this is slightly racy. It is possible that for some
  518. * strange reason that the RT thread we created, did not
  519. * call schedule for 100ms after doing the completion,
  520. * and we do a wakeup on a task that already is awake.
  521. * But that is extremely unlikely, and the worst thing that
  522. * happens in such a case, is that we disable tracing.
  523. * Honestly, if this race does happen something is horrible
  524. * wrong with the system.
  525. */
  526. wake_up_process(p);
  527. /* give a little time to let the thread wake up */
  528. msleep(100);
  529. /* stop the tracing. */
  530. tracing_stop();
  531. /* check both trace buffers */
  532. ret = trace_test_buffer(tr, NULL);
  533. if (!ret)
  534. ret = trace_test_buffer(&max_tr, &count);
  535. trace->reset(tr);
  536. tracing_start();
  537. tracing_max_latency = save_max;
  538. /* kill the thread */
  539. kthread_stop(p);
  540. if (!ret && !count) {
  541. printk(KERN_CONT ".. no entries found ..");
  542. ret = -1;
  543. }
  544. return ret;
  545. }
  546. #endif /* CONFIG_SCHED_TRACER */
  547. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  548. int
  549. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  550. {
  551. unsigned long count;
  552. int ret;
  553. /* start the tracing */
  554. ret = tracer_init(trace, tr);
  555. if (ret) {
  556. warn_failed_init_tracer(trace, ret);
  557. return ret;
  558. }
  559. /* Sleep for a 1/10 of a second */
  560. msleep(100);
  561. /* stop the tracing. */
  562. tracing_stop();
  563. /* check the trace buffer */
  564. ret = trace_test_buffer(tr, &count);
  565. trace->reset(tr);
  566. tracing_start();
  567. if (!ret && !count) {
  568. printk(KERN_CONT ".. no entries found ..");
  569. ret = -1;
  570. }
  571. return ret;
  572. }
  573. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  574. #ifdef CONFIG_SYSPROF_TRACER
  575. int
  576. trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
  577. {
  578. unsigned long count;
  579. int ret;
  580. /* start the tracing */
  581. ret = tracer_init(trace, tr);
  582. if (ret) {
  583. warn_failed_init_tracer(trace, ret);
  584. return ret;
  585. }
  586. /* Sleep for a 1/10 of a second */
  587. msleep(100);
  588. /* stop the tracing. */
  589. tracing_stop();
  590. /* check the trace buffer */
  591. ret = trace_test_buffer(tr, &count);
  592. trace->reset(tr);
  593. tracing_start();
  594. if (!ret && !count) {
  595. printk(KERN_CONT ".. no entries found ..");
  596. ret = -1;
  597. }
  598. return ret;
  599. }
  600. #endif /* CONFIG_SYSPROF_TRACER */
  601. #ifdef CONFIG_BRANCH_TRACER
  602. int
  603. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  604. {
  605. unsigned long count;
  606. int ret;
  607. /* start the tracing */
  608. ret = tracer_init(trace, tr);
  609. if (ret) {
  610. warn_failed_init_tracer(trace, ret);
  611. return ret;
  612. }
  613. /* Sleep for a 1/10 of a second */
  614. msleep(100);
  615. /* stop the tracing. */
  616. tracing_stop();
  617. /* check the trace buffer */
  618. ret = trace_test_buffer(tr, &count);
  619. trace->reset(tr);
  620. tracing_start();
  621. if (!ret && !count) {
  622. printk(KERN_CONT ".. no entries found ..");
  623. ret = -1;
  624. }
  625. return ret;
  626. }
  627. #endif /* CONFIG_BRANCH_TRACER */
  628. #ifdef CONFIG_HW_BRANCH_TRACER
  629. int
  630. trace_selftest_startup_hw_branches(struct tracer *trace,
  631. struct trace_array *tr)
  632. {
  633. struct trace_iterator *iter;
  634. struct tracer tracer;
  635. unsigned long count;
  636. int ret;
  637. if (!trace->open) {
  638. printk(KERN_CONT "missing open function...");
  639. return -1;
  640. }
  641. ret = tracer_init(trace, tr);
  642. if (ret) {
  643. warn_failed_init_tracer(trace, ret);
  644. return ret;
  645. }
  646. /*
  647. * The hw-branch tracer needs to collect the trace from the various
  648. * cpu trace buffers - before tracing is stopped.
  649. */
  650. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  651. if (!iter)
  652. return -ENOMEM;
  653. memcpy(&tracer, trace, sizeof(tracer));
  654. iter->trace = &tracer;
  655. iter->tr = tr;
  656. iter->pos = -1;
  657. mutex_init(&iter->mutex);
  658. trace->open(iter);
  659. mutex_destroy(&iter->mutex);
  660. kfree(iter);
  661. tracing_stop();
  662. ret = trace_test_buffer(tr, &count);
  663. trace->reset(tr);
  664. tracing_start();
  665. if (!ret && !count) {
  666. printk(KERN_CONT "no entries found..");
  667. ret = -1;
  668. }
  669. return ret;
  670. }
  671. #endif /* CONFIG_HW_BRANCH_TRACER */
  672. #ifdef CONFIG_KSYM_TRACER
  673. static int ksym_selftest_dummy;
  674. int
  675. trace_selftest_startup_ksym(struct tracer *trace, struct trace_array *tr)
  676. {
  677. unsigned long count;
  678. int ret;
  679. /* start the tracing */
  680. ret = tracer_init(trace, tr);
  681. if (ret) {
  682. warn_failed_init_tracer(trace, ret);
  683. return ret;
  684. }
  685. ksym_selftest_dummy = 0;
  686. /* Register the read-write tracing request */
  687. ret = process_new_ksym_entry("ksym_selftest_dummy",
  688. HW_BREAKPOINT_R | HW_BREAKPOINT_W,
  689. (unsigned long)(&ksym_selftest_dummy));
  690. if (ret < 0) {
  691. printk(KERN_CONT "ksym_trace read-write startup test failed\n");
  692. goto ret_path;
  693. }
  694. /* Perform a read and a write operation over the dummy variable to
  695. * trigger the tracer
  696. */
  697. if (ksym_selftest_dummy == 0)
  698. ksym_selftest_dummy++;
  699. /* stop the tracing. */
  700. tracing_stop();
  701. /* check the trace buffer */
  702. ret = trace_test_buffer(tr, &count);
  703. trace->reset(tr);
  704. tracing_start();
  705. /* read & write operations - one each is performed on the dummy variable
  706. * triggering two entries in the trace buffer
  707. */
  708. if (!ret && count != 2) {
  709. printk(KERN_CONT "Ksym tracer startup test failed");
  710. ret = -1;
  711. }
  712. ret_path:
  713. return ret;
  714. }
  715. #endif /* CONFIG_KSYM_TRACER */