trace_selftest.c 18 KB

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