1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun #include <linux/module.h>
3*4882a593Smuzhiyun #include <linux/kthread.h>
4*4882a593Smuzhiyun #include <linux/trace.h>
5*4882a593Smuzhiyun #include <linux/trace_events.h>
6*4882a593Smuzhiyun #include <linux/timer.h>
7*4882a593Smuzhiyun #include <linux/err.h>
8*4882a593Smuzhiyun #include <linux/jiffies.h>
9*4882a593Smuzhiyun #include <linux/workqueue.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * Any file that uses trace points, must include the header.
13*4882a593Smuzhiyun * But only one file, must include the header by defining
14*4882a593Smuzhiyun * CREATE_TRACE_POINTS first. This will make the C code that
15*4882a593Smuzhiyun * creates the handles for the trace points.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
18*4882a593Smuzhiyun #include "sample-trace-array.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun struct trace_array *tr;
21*4882a593Smuzhiyun static void mytimer_handler(struct timer_list *unused);
22*4882a593Smuzhiyun static struct task_struct *simple_tsk;
23*4882a593Smuzhiyun
trace_work_fn(struct work_struct * work)24*4882a593Smuzhiyun static void trace_work_fn(struct work_struct *work)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun * Disable tracing for event "sample_event".
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun trace_array_set_clr_event(tr, "sample-subsystem", "sample_event",
30*4882a593Smuzhiyun false);
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun static DECLARE_WORK(trace_work, trace_work_fn);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun * mytimer: Timer setup to disable tracing for event "sample_event". This
36*4882a593Smuzhiyun * timer is only for the purposes of the sample module to demonstrate access of
37*4882a593Smuzhiyun * Ftrace instances from within kernel.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun static DEFINE_TIMER(mytimer, mytimer_handler);
40*4882a593Smuzhiyun
mytimer_handler(struct timer_list * unused)41*4882a593Smuzhiyun static void mytimer_handler(struct timer_list *unused)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun schedule_work(&trace_work);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
simple_thread_func(int count)46*4882a593Smuzhiyun static void simple_thread_func(int count)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun set_current_state(TASK_INTERRUPTIBLE);
49*4882a593Smuzhiyun schedule_timeout(HZ);
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * Printing count value using trace_array_printk() - trace_printk()
53*4882a593Smuzhiyun * equivalent for the instance buffers.
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun trace_array_printk(tr, _THIS_IP_, "trace_array_printk: count=%d\n",
56*4882a593Smuzhiyun count);
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * Tracepoint for event "sample_event". This will print the
59*4882a593Smuzhiyun * current value of count and current jiffies.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun trace_sample_event(count, jiffies);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
simple_thread(void * arg)64*4882a593Smuzhiyun static int simple_thread(void *arg)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun int count = 0;
67*4882a593Smuzhiyun unsigned long delay = msecs_to_jiffies(5000);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * Enable tracing for "sample_event".
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", true);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * Adding timer - mytimer. This timer will disable tracing after
76*4882a593Smuzhiyun * delay seconds.
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun add_timer(&mytimer);
80*4882a593Smuzhiyun mod_timer(&mytimer, jiffies+delay);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun while (!kthread_should_stop())
83*4882a593Smuzhiyun simple_thread_func(count++);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun del_timer(&mytimer);
86*4882a593Smuzhiyun cancel_work_sync(&trace_work);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * trace_array_put() decrements the reference counter associated with
90*4882a593Smuzhiyun * the trace array - "tr". We are done using the trace array, hence
91*4882a593Smuzhiyun * decrement the reference counter so that it can be destroyed using
92*4882a593Smuzhiyun * trace_array_destroy().
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun trace_array_put(tr);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun return 0;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
sample_trace_array_init(void)99*4882a593Smuzhiyun static int __init sample_trace_array_init(void)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * Return a pointer to the trace array with name "sample-instance" if it
103*4882a593Smuzhiyun * exists, else create a new trace array.
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * NOTE: This function increments the reference counter
106*4882a593Smuzhiyun * associated with the trace array - "tr".
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun tr = trace_array_get_by_name("sample-instance");
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun if (!tr)
111*4882a593Smuzhiyun return -1;
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun * If context specific per-cpu buffers havent already been allocated.
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun trace_printk_init_buffers();
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun simple_tsk = kthread_run(simple_thread, NULL, "sample-instance");
118*4882a593Smuzhiyun if (IS_ERR(simple_tsk)) {
119*4882a593Smuzhiyun trace_array_put(tr);
120*4882a593Smuzhiyun trace_array_destroy(tr);
121*4882a593Smuzhiyun return -1;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
sample_trace_array_exit(void)127*4882a593Smuzhiyun static void __exit sample_trace_array_exit(void)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun kthread_stop(simple_tsk);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * We are unloading our module and no longer require the trace array.
133*4882a593Smuzhiyun * Remove/destroy "tr" using trace_array_destroy()
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun trace_array_destroy(tr);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun module_init(sample_trace_array_init);
139*4882a593Smuzhiyun module_exit(sample_trace_array_exit);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun MODULE_AUTHOR("Divya Indi");
142*4882a593Smuzhiyun MODULE_DESCRIPTION("Sample module for kernel access to Ftrace instances");
143*4882a593Smuzhiyun MODULE_LICENSE("GPL");
144