trace-events-sample.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/kthread.h>
  4. /*
  5. * Any file that uses trace points, must include the header.
  6. * But only one file, must include the header by defining
  7. * CREATE_TRACE_POINTS first. This will make the C code that
  8. * creates the handles for the trace points.
  9. */
  10. #define CREATE_TRACE_POINTS
  11. #include "trace-events-sample.h"
  12. static const char *random_strings[] = {
  13. "Mother Goose",
  14. "Snoopy",
  15. "Gandalf",
  16. "Frodo",
  17. "One ring to rule them all"
  18. };
  19. static void do_simple_thread_func(int cnt, const char *fmt, ...)
  20. {
  21. unsigned long bitmask[1] = {0xdeadbeefUL};
  22. va_list va;
  23. int array[6];
  24. int len = cnt % 5;
  25. int i;
  26. set_current_state(TASK_INTERRUPTIBLE);
  27. schedule_timeout(HZ);
  28. for (i = 0; i < len; i++)
  29. array[i] = i + 1;
  30. array[i] = 0;
  31. va_start(va, fmt);
  32. /* Silly tracepoints */
  33. trace_foo_bar("hello", cnt, array, random_strings[len],
  34. current->cpus_ptr, fmt, &va);
  35. va_end(va);
  36. trace_foo_with_template_simple("HELLO", cnt);
  37. trace_foo_bar_with_cond("Some times print", cnt);
  38. trace_foo_with_template_cond("prints other times", cnt);
  39. trace_foo_with_template_print("I have to be different", cnt);
  40. trace_foo_rel_loc("Hello __rel_loc", cnt, bitmask);
  41. }
  42. static void simple_thread_func(int cnt)
  43. {
  44. do_simple_thread_func(cnt, "iter=%d", cnt);
  45. }
  46. static int simple_thread(void *arg)
  47. {
  48. int cnt = 0;
  49. while (!kthread_should_stop())
  50. simple_thread_func(cnt++);
  51. return 0;
  52. }
  53. static struct task_struct *simple_tsk;
  54. static struct task_struct *simple_tsk_fn;
  55. static void simple_thread_func_fn(int cnt)
  56. {
  57. set_current_state(TASK_INTERRUPTIBLE);
  58. schedule_timeout(HZ);
  59. /* More silly tracepoints */
  60. trace_foo_bar_with_fn("Look at me", cnt);
  61. trace_foo_with_template_fn("Look at me too", cnt);
  62. }
  63. static int simple_thread_fn(void *arg)
  64. {
  65. int cnt = 0;
  66. while (!kthread_should_stop())
  67. simple_thread_func_fn(cnt++);
  68. return 0;
  69. }
  70. static DEFINE_MUTEX(thread_mutex);
  71. static int simple_thread_cnt;
  72. int foo_bar_reg(void)
  73. {
  74. mutex_lock(&thread_mutex);
  75. if (simple_thread_cnt++)
  76. goto out;
  77. pr_info("Starting thread for foo_bar_fn\n");
  78. /*
  79. * We shouldn't be able to start a trace when the module is
  80. * unloading (there's other locks to prevent that). But
  81. * for consistency sake, we still take the thread_mutex.
  82. */
  83. simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
  84. out:
  85. mutex_unlock(&thread_mutex);
  86. return 0;
  87. }
  88. void foo_bar_unreg(void)
  89. {
  90. mutex_lock(&thread_mutex);
  91. if (--simple_thread_cnt)
  92. goto out;
  93. pr_info("Killing thread for foo_bar_fn\n");
  94. if (simple_tsk_fn)
  95. kthread_stop(simple_tsk_fn);
  96. simple_tsk_fn = NULL;
  97. out:
  98. mutex_unlock(&thread_mutex);
  99. }
  100. static int __init trace_event_init(void)
  101. {
  102. simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
  103. if (IS_ERR(simple_tsk))
  104. return -1;
  105. return 0;
  106. }
  107. static void __exit trace_event_exit(void)
  108. {
  109. kthread_stop(simple_tsk);
  110. mutex_lock(&thread_mutex);
  111. if (simple_tsk_fn)
  112. kthread_stop(simple_tsk_fn);
  113. simple_tsk_fn = NULL;
  114. mutex_unlock(&thread_mutex);
  115. }
  116. module_init(trace_event_init);
  117. module_exit(trace_event_exit);
  118. MODULE_AUTHOR("Steven Rostedt");
  119. MODULE_DESCRIPTION("trace-events-sample");
  120. MODULE_LICENSE("GPL");