trace_selftest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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))) {
  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 __ftrace_dump(bool disable_tracing);
  213. static unsigned int graph_hang_thresh;
  214. /* Wrap the real function entry probe to avoid possible hanging */
  215. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  216. {
  217. /* This is harmlessly racy, we want to approximately detect a hang */
  218. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  219. ftrace_graph_stop();
  220. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  221. if (ftrace_dump_on_oops)
  222. __ftrace_dump(false);
  223. return 0;
  224. }
  225. return trace_graph_entry(trace);
  226. }
  227. /*
  228. * Pretty much the same than for the function tracer from which the selftest
  229. * has been borrowed.
  230. */
  231. int
  232. trace_selftest_startup_function_graph(struct tracer *trace,
  233. struct trace_array *tr)
  234. {
  235. int ret;
  236. unsigned long count;
  237. /*
  238. * Simulate the init() callback but we attach a watchdog callback
  239. * to detect and recover from possible hangs
  240. */
  241. tracing_reset_online_cpus(tr);
  242. set_graph_array(tr);
  243. ret = register_ftrace_graph(&trace_graph_return,
  244. &trace_graph_entry_watchdog);
  245. if (ret) {
  246. warn_failed_init_tracer(trace, ret);
  247. goto out;
  248. }
  249. tracing_start_cmdline_record();
  250. /* Sleep for a 1/10 of a second */
  251. msleep(100);
  252. /* Have we just recovered from a hang? */
  253. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  254. tracing_selftest_disabled = true;
  255. ret = -1;
  256. goto out;
  257. }
  258. tracing_stop();
  259. /* check the trace buffer */
  260. ret = trace_test_buffer(tr, &count);
  261. trace->reset(tr);
  262. tracing_start();
  263. if (!ret && !count) {
  264. printk(KERN_CONT ".. no entries found ..");
  265. ret = -1;
  266. goto out;
  267. }
  268. /* Don't test dynamic tracing, the function tracer already did */
  269. out:
  270. /* Stop it if we failed */
  271. if (ret)
  272. ftrace_graph_stop();
  273. return ret;
  274. }
  275. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  276. #ifdef CONFIG_IRQSOFF_TRACER
  277. int
  278. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  279. {
  280. unsigned long save_max = tracing_max_latency;
  281. unsigned long count;
  282. int ret;
  283. /* start the tracing */
  284. ret = tracer_init(trace, tr);
  285. if (ret) {
  286. warn_failed_init_tracer(trace, ret);
  287. return ret;
  288. }
  289. /* reset the max latency */
  290. tracing_max_latency = 0;
  291. /* disable interrupts for a bit */
  292. local_irq_disable();
  293. udelay(100);
  294. local_irq_enable();
  295. /*
  296. * Stop the tracer to avoid a warning subsequent
  297. * to buffer flipping failure because tracing_stop()
  298. * disables the tr and max buffers, making flipping impossible
  299. * in case of parallels max irqs off latencies.
  300. */
  301. trace->stop(tr);
  302. /* stop the tracing. */
  303. tracing_stop();
  304. /* check both trace buffers */
  305. ret = trace_test_buffer(tr, NULL);
  306. if (!ret)
  307. ret = trace_test_buffer(&max_tr, &count);
  308. trace->reset(tr);
  309. tracing_start();
  310. if (!ret && !count) {
  311. printk(KERN_CONT ".. no entries found ..");
  312. ret = -1;
  313. }
  314. tracing_max_latency = save_max;
  315. return ret;
  316. }
  317. #endif /* CONFIG_IRQSOFF_TRACER */
  318. #ifdef CONFIG_PREEMPT_TRACER
  319. int
  320. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  321. {
  322. unsigned long save_max = tracing_max_latency;
  323. unsigned long count;
  324. int ret;
  325. /*
  326. * Now that the big kernel lock is no longer preemptable,
  327. * and this is called with the BKL held, it will always
  328. * fail. If preemption is already disabled, simply
  329. * pass the test. When the BKL is removed, or becomes
  330. * preemptible again, we will once again test this,
  331. * so keep it in.
  332. */
  333. if (preempt_count()) {
  334. printk(KERN_CONT "can not test ... force ");
  335. return 0;
  336. }
  337. /* start the tracing */
  338. ret = tracer_init(trace, tr);
  339. if (ret) {
  340. warn_failed_init_tracer(trace, ret);
  341. return ret;
  342. }
  343. /* reset the max latency */
  344. tracing_max_latency = 0;
  345. /* disable preemption for a bit */
  346. preempt_disable();
  347. udelay(100);
  348. preempt_enable();
  349. /*
  350. * Stop the tracer to avoid a warning subsequent
  351. * to buffer flipping failure because tracing_stop()
  352. * disables the tr and max buffers, making flipping impossible
  353. * in case of parallels max preempt off latencies.
  354. */
  355. trace->stop(tr);
  356. /* stop the tracing. */
  357. tracing_stop();
  358. /* check both trace buffers */
  359. ret = trace_test_buffer(tr, NULL);
  360. if (!ret)
  361. ret = trace_test_buffer(&max_tr, &count);
  362. trace->reset(tr);
  363. tracing_start();
  364. if (!ret && !count) {
  365. printk(KERN_CONT ".. no entries found ..");
  366. ret = -1;
  367. }
  368. tracing_max_latency = save_max;
  369. return ret;
  370. }
  371. #endif /* CONFIG_PREEMPT_TRACER */
  372. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  373. int
  374. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  375. {
  376. unsigned long save_max = tracing_max_latency;
  377. unsigned long count;
  378. int ret;
  379. /*
  380. * Now that the big kernel lock is no longer preemptable,
  381. * and this is called with the BKL held, it will always
  382. * fail. If preemption is already disabled, simply
  383. * pass the test. When the BKL is removed, or becomes
  384. * preemptible again, we will once again test this,
  385. * so keep it in.
  386. */
  387. if (preempt_count()) {
  388. printk(KERN_CONT "can not test ... force ");
  389. return 0;
  390. }
  391. /* start the tracing */
  392. ret = tracer_init(trace, tr);
  393. if (ret) {
  394. warn_failed_init_tracer(trace, ret);
  395. goto out_no_start;
  396. }
  397. /* reset the max latency */
  398. tracing_max_latency = 0;
  399. /* disable preemption and interrupts for a bit */
  400. preempt_disable();
  401. local_irq_disable();
  402. udelay(100);
  403. preempt_enable();
  404. /* reverse the order of preempt vs irqs */
  405. local_irq_enable();
  406. /*
  407. * Stop the tracer to avoid a warning subsequent
  408. * to buffer flipping failure because tracing_stop()
  409. * disables the tr and max buffers, making flipping impossible
  410. * in case of parallels max irqs/preempt off latencies.
  411. */
  412. trace->stop(tr);
  413. /* stop the tracing. */
  414. tracing_stop();
  415. /* check both trace buffers */
  416. ret = trace_test_buffer(tr, NULL);
  417. if (ret)
  418. goto out;
  419. ret = trace_test_buffer(&max_tr, &count);
  420. if (ret)
  421. goto out;
  422. if (!ret && !count) {
  423. printk(KERN_CONT ".. no entries found ..");
  424. ret = -1;
  425. goto out;
  426. }
  427. /* do the test by disabling interrupts first this time */
  428. tracing_max_latency = 0;
  429. tracing_start();
  430. trace->start(tr);
  431. preempt_disable();
  432. local_irq_disable();
  433. udelay(100);
  434. preempt_enable();
  435. /* reverse the order of preempt vs irqs */
  436. local_irq_enable();
  437. trace->stop(tr);
  438. /* stop the tracing. */
  439. tracing_stop();
  440. /* check both trace buffers */
  441. ret = trace_test_buffer(tr, NULL);
  442. if (ret)
  443. goto out;
  444. ret = trace_test_buffer(&max_tr, &count);
  445. if (!ret && !count) {
  446. printk(KERN_CONT ".. no entries found ..");
  447. ret = -1;
  448. goto out;
  449. }
  450. out:
  451. tracing_start();
  452. out_no_start:
  453. trace->reset(tr);
  454. tracing_max_latency = save_max;
  455. return ret;
  456. }
  457. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  458. #ifdef CONFIG_NOP_TRACER
  459. int
  460. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  461. {
  462. /* What could possibly go wrong? */
  463. return 0;
  464. }
  465. #endif
  466. #ifdef CONFIG_SCHED_TRACER
  467. static int trace_wakeup_test_thread(void *data)
  468. {
  469. /* Make this a RT thread, doesn't need to be too high */
  470. struct sched_param param = { .sched_priority = 5 };
  471. struct completion *x = data;
  472. sched_setscheduler(current, SCHED_FIFO, &param);
  473. /* Make it know we have a new prio */
  474. complete(x);
  475. /* now go to sleep and let the test wake us up */
  476. set_current_state(TASK_INTERRUPTIBLE);
  477. schedule();
  478. /* we are awake, now wait to disappear */
  479. while (!kthread_should_stop()) {
  480. /*
  481. * This is an RT task, do short sleeps to let
  482. * others run.
  483. */
  484. msleep(100);
  485. }
  486. return 0;
  487. }
  488. int
  489. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  490. {
  491. unsigned long save_max = tracing_max_latency;
  492. struct task_struct *p;
  493. struct completion isrt;
  494. unsigned long count;
  495. int ret;
  496. init_completion(&isrt);
  497. /* create a high prio thread */
  498. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  499. if (IS_ERR(p)) {
  500. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  501. return -1;
  502. }
  503. /* make sure the thread is running at an RT prio */
  504. wait_for_completion(&isrt);
  505. /* start the tracing */
  506. ret = tracer_init(trace, tr);
  507. if (ret) {
  508. warn_failed_init_tracer(trace, ret);
  509. return ret;
  510. }
  511. /* reset the max latency */
  512. tracing_max_latency = 0;
  513. /* sleep to let the RT thread sleep too */
  514. msleep(100);
  515. /*
  516. * Yes this is slightly racy. It is possible that for some
  517. * strange reason that the RT thread we created, did not
  518. * call schedule for 100ms after doing the completion,
  519. * and we do a wakeup on a task that already is awake.
  520. * But that is extremely unlikely, and the worst thing that
  521. * happens in such a case, is that we disable tracing.
  522. * Honestly, if this race does happen something is horrible
  523. * wrong with the system.
  524. */
  525. wake_up_process(p);
  526. /* give a little time to let the thread wake up */
  527. msleep(100);
  528. /* stop the tracing. */
  529. tracing_stop();
  530. /* check both trace buffers */
  531. ret = trace_test_buffer(tr, NULL);
  532. if (!ret)
  533. ret = trace_test_buffer(&max_tr, &count);
  534. trace->reset(tr);
  535. tracing_start();
  536. tracing_max_latency = save_max;
  537. /* kill the thread */
  538. kthread_stop(p);
  539. if (!ret && !count) {
  540. printk(KERN_CONT ".. no entries found ..");
  541. ret = -1;
  542. }
  543. return ret;
  544. }
  545. #endif /* CONFIG_SCHED_TRACER */
  546. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  547. int
  548. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  549. {
  550. unsigned long count;
  551. int ret;
  552. /* start the tracing */
  553. ret = tracer_init(trace, tr);
  554. if (ret) {
  555. warn_failed_init_tracer(trace, ret);
  556. return ret;
  557. }
  558. /* Sleep for a 1/10 of a second */
  559. msleep(100);
  560. /* stop the tracing. */
  561. tracing_stop();
  562. /* check the trace buffer */
  563. ret = trace_test_buffer(tr, &count);
  564. trace->reset(tr);
  565. tracing_start();
  566. if (!ret && !count) {
  567. printk(KERN_CONT ".. no entries found ..");
  568. ret = -1;
  569. }
  570. return ret;
  571. }
  572. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  573. #ifdef CONFIG_SYSPROF_TRACER
  574. int
  575. trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
  576. {
  577. unsigned long count;
  578. int ret;
  579. /* start the tracing */
  580. ret = tracer_init(trace, tr);
  581. if (ret) {
  582. warn_failed_init_tracer(trace, ret);
  583. return ret;
  584. }
  585. /* Sleep for a 1/10 of a second */
  586. msleep(100);
  587. /* stop the tracing. */
  588. tracing_stop();
  589. /* check the trace buffer */
  590. ret = trace_test_buffer(tr, &count);
  591. trace->reset(tr);
  592. tracing_start();
  593. if (!ret && !count) {
  594. printk(KERN_CONT ".. no entries found ..");
  595. ret = -1;
  596. }
  597. return ret;
  598. }
  599. #endif /* CONFIG_SYSPROF_TRACER */
  600. #ifdef CONFIG_BRANCH_TRACER
  601. int
  602. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  603. {
  604. unsigned long count;
  605. int ret;
  606. /* start the tracing */
  607. ret = tracer_init(trace, tr);
  608. if (ret) {
  609. warn_failed_init_tracer(trace, ret);
  610. return ret;
  611. }
  612. /* Sleep for a 1/10 of a second */
  613. msleep(100);
  614. /* stop the tracing. */
  615. tracing_stop();
  616. /* check the trace buffer */
  617. ret = trace_test_buffer(tr, &count);
  618. trace->reset(tr);
  619. tracing_start();
  620. if (!ret && !count) {
  621. printk(KERN_CONT ".. no entries found ..");
  622. ret = -1;
  623. }
  624. return ret;
  625. }
  626. #endif /* CONFIG_BRANCH_TRACER */
  627. #ifdef CONFIG_HW_BRANCH_TRACER
  628. int
  629. trace_selftest_startup_hw_branches(struct tracer *trace,
  630. struct trace_array *tr)
  631. {
  632. struct trace_iterator *iter;
  633. struct tracer tracer;
  634. unsigned long count;
  635. int ret;
  636. if (!trace->open) {
  637. printk(KERN_CONT "missing open function...");
  638. return -1;
  639. }
  640. ret = tracer_init(trace, tr);
  641. if (ret) {
  642. warn_failed_init_tracer(trace, ret);
  643. return ret;
  644. }
  645. /*
  646. * The hw-branch tracer needs to collect the trace from the various
  647. * cpu trace buffers - before tracing is stopped.
  648. */
  649. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  650. if (!iter)
  651. return -ENOMEM;
  652. memcpy(&tracer, trace, sizeof(tracer));
  653. iter->trace = &tracer;
  654. iter->tr = tr;
  655. iter->pos = -1;
  656. mutex_init(&iter->mutex);
  657. trace->open(iter);
  658. mutex_destroy(&iter->mutex);
  659. kfree(iter);
  660. tracing_stop();
  661. ret = trace_test_buffer(tr, &count);
  662. trace->reset(tr);
  663. tracing_start();
  664. if (!ret && !count) {
  665. printk(KERN_CONT "no entries found..");
  666. ret = -1;
  667. }
  668. return ret;
  669. }
  670. #endif /* CONFIG_HW_BRANCH_TRACER */
  671. #ifdef CONFIG_KSYM_TRACER
  672. static int ksym_selftest_dummy;
  673. int
  674. trace_selftest_startup_ksym(struct tracer *trace, struct trace_array *tr)
  675. {
  676. unsigned long count;
  677. int ret;
  678. /* start the tracing */
  679. ret = tracer_init(trace, tr);
  680. if (ret) {
  681. warn_failed_init_tracer(trace, ret);
  682. return ret;
  683. }
  684. ksym_selftest_dummy = 0;
  685. /* Register the read-write tracing request */
  686. ret = process_new_ksym_entry("ksym_selftest_dummy",
  687. HW_BREAKPOINT_R | HW_BREAKPOINT_W,
  688. (unsigned long)(&ksym_selftest_dummy));
  689. if (ret < 0) {
  690. printk(KERN_CONT "ksym_trace read-write startup test failed\n");
  691. goto ret_path;
  692. }
  693. /* Perform a read and a write operation over the dummy variable to
  694. * trigger the tracer
  695. */
  696. if (ksym_selftest_dummy == 0)
  697. ksym_selftest_dummy++;
  698. /* stop the tracing. */
  699. tracing_stop();
  700. /* check the trace buffer */
  701. ret = trace_test_buffer(tr, &count);
  702. trace->reset(tr);
  703. tracing_start();
  704. /* read & write operations - one each is performed on the dummy variable
  705. * triggering two entries in the trace buffer
  706. */
  707. if (!ret && count != 2) {
  708. printk(KERN_CONT "Ksym tracer startup test failed");
  709. ret = -1;
  710. }
  711. ret_path:
  712. return ret;
  713. }
  714. #endif /* CONFIG_KSYM_TRACER */