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