// SPDX-License-Identifier: GPL-2.0-only1#include <linux/module.h>2#include <linux/kthread.h>3#include <linux/trace.h>4#include <linux/trace_events.h>5#include <linux/timer.h>6#include <linux/err.h>7#include <linux/jiffies.h>8#include <linux/workqueue.h>910/*11* Any file that uses trace points, must include the header.12* But only one file, must include the header by defining13* CREATE_TRACE_POINTS first. This will make the C code that14* creates the handles for the trace points.15*/16#define CREATE_TRACE_POINTS17#include "sample-trace-array.h"1819struct trace_array *tr;20static void mytimer_handler(struct timer_list *unused);21static struct task_struct *simple_tsk;2223static void trace_work_fn(struct work_struct *work)24{25/*26* Disable tracing for event "sample_event".27*/28trace_array_set_clr_event(tr, "sample-subsystem", "sample_event",29false);30}31static DECLARE_WORK(trace_work, trace_work_fn);3233/*34* mytimer: Timer setup to disable tracing for event "sample_event". This35* timer is only for the purposes of the sample module to demonstrate access of36* Ftrace instances from within kernel.37*/38static DEFINE_TIMER(mytimer, mytimer_handler);3940static void mytimer_handler(struct timer_list *unused)41{42schedule_work(&trace_work);43}4445static void simple_thread_func(int count)46{47set_current_state(TASK_INTERRUPTIBLE);48schedule_timeout(HZ);4950/*51* Printing count value using trace_array_printk() - trace_printk()52* equivalent for the instance buffers.53*/54trace_array_printk(tr, _THIS_IP_, "trace_array_printk: count=%d\n",55count);56/*57* Tracepoint for event "sample_event". This will print the58* current value of count and current jiffies.59*/60trace_sample_event(count, jiffies);61}6263static int simple_thread(void *arg)64{65int count = 0;66unsigned long delay = msecs_to_jiffies(5000);6768/*69* Enable tracing for "sample_event".70*/71trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", true);7273/*74* Adding timer - mytimer. This timer will disable tracing after75* delay seconds.76*77*/78add_timer(&mytimer);79mod_timer(&mytimer, jiffies+delay);8081while (!kthread_should_stop())82simple_thread_func(count++);8384timer_delete(&mytimer);85cancel_work_sync(&trace_work);8687/*88* trace_array_put() decrements the reference counter associated with89* the trace array - "tr". We are done using the trace array, hence90* decrement the reference counter so that it can be destroyed using91* trace_array_destroy().92*/93trace_array_put(tr);9495return 0;96}9798static int __init sample_trace_array_init(void)99{100/*101* Return a pointer to the trace array with name "sample-instance" if it102* exists, else create a new trace array.103*104* NOTE: This function increments the reference counter105* associated with the trace array - "tr".106*/107tr = trace_array_get_by_name("sample-instance", "sched,timer,kprobes");108109if (!tr)110return -1;111/*112* If context specific per-cpu buffers havent already been allocated.113*/114trace_array_init_printk(tr);115116simple_tsk = kthread_run(simple_thread, NULL, "sample-instance");117if (IS_ERR(simple_tsk)) {118trace_array_put(tr);119trace_array_destroy(tr);120return -1;121}122123return 0;124}125126static void __exit sample_trace_array_exit(void)127{128kthread_stop(simple_tsk);129130/*131* We are unloading our module and no longer require the trace array.132* Remove/destroy "tr" using trace_array_destroy()133*/134trace_array_destroy(tr);135}136137module_init(sample_trace_array_init);138module_exit(sample_trace_array_exit);139140MODULE_AUTHOR("Divya Indi");141MODULE_DESCRIPTION("Sample module for kernel access to Ftrace instances");142MODULE_LICENSE("GPL");143144145