xref: /OK3568_Linux_fs/kernel/samples/kprobes/kretprobe_example.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * kretprobe_example.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Here's a sample kernel module showing the use of return probes to
6*4882a593Smuzhiyun  * report the return value and total time taken for probed function
7*4882a593Smuzhiyun  * to run.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * usage: insmod kretprobe_example.ko func=<func_name>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * If no func_name is specified, kernel_clone is instrumented
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * For more information on theory of operation of kretprobes, see
14*4882a593Smuzhiyun  * Documentation/trace/kprobes.rst
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Build and insert the kernel module as done in the kprobe example.
17*4882a593Smuzhiyun  * You will see the trace data in /var/log/messages and on the console
18*4882a593Smuzhiyun  * whenever the probed function returns. (Some messages may be suppressed
19*4882a593Smuzhiyun  * if syslogd is configured to eliminate duplicate messages.)
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/kernel.h>
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/kprobes.h>
25*4882a593Smuzhiyun #include <linux/ktime.h>
26*4882a593Smuzhiyun #include <linux/limits.h>
27*4882a593Smuzhiyun #include <linux/sched.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static char func_name[NAME_MAX] = "kernel_clone";
30*4882a593Smuzhiyun module_param_string(func, func_name, NAME_MAX, S_IRUGO);
31*4882a593Smuzhiyun MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
32*4882a593Smuzhiyun 			" function's execution time");
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* per-instance private data */
35*4882a593Smuzhiyun struct my_data {
36*4882a593Smuzhiyun 	ktime_t entry_stamp;
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /* Here we use the entry_hanlder to timestamp function entry */
entry_handler(struct kretprobe_instance * ri,struct pt_regs * regs)40*4882a593Smuzhiyun static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	struct my_data *data;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	if (!current->mm)
45*4882a593Smuzhiyun 		return 1;	/* Skip kernel threads */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	data = (struct my_data *)ri->data;
48*4882a593Smuzhiyun 	data->entry_stamp = ktime_get();
49*4882a593Smuzhiyun 	return 0;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun NOKPROBE_SYMBOL(entry_handler);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * Return-probe handler: Log the return value and duration. Duration may turn
55*4882a593Smuzhiyun  * out to be zero consistently, depending upon the granularity of time
56*4882a593Smuzhiyun  * accounting on the platform.
57*4882a593Smuzhiyun  */
ret_handler(struct kretprobe_instance * ri,struct pt_regs * regs)58*4882a593Smuzhiyun static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	unsigned long retval = regs_return_value(regs);
61*4882a593Smuzhiyun 	struct my_data *data = (struct my_data *)ri->data;
62*4882a593Smuzhiyun 	s64 delta;
63*4882a593Smuzhiyun 	ktime_t now;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	now = ktime_get();
66*4882a593Smuzhiyun 	delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
67*4882a593Smuzhiyun 	pr_info("%s returned %lu and took %lld ns to execute\n",
68*4882a593Smuzhiyun 			func_name, retval, (long long)delta);
69*4882a593Smuzhiyun 	return 0;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun NOKPROBE_SYMBOL(ret_handler);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun static struct kretprobe my_kretprobe = {
74*4882a593Smuzhiyun 	.handler		= ret_handler,
75*4882a593Smuzhiyun 	.entry_handler		= entry_handler,
76*4882a593Smuzhiyun 	.data_size		= sizeof(struct my_data),
77*4882a593Smuzhiyun 	/* Probe up to 20 instances concurrently. */
78*4882a593Smuzhiyun 	.maxactive		= 20,
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun 
kretprobe_init(void)81*4882a593Smuzhiyun static int __init kretprobe_init(void)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	int ret;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	my_kretprobe.kp.symbol_name = func_name;
86*4882a593Smuzhiyun 	ret = register_kretprobe(&my_kretprobe);
87*4882a593Smuzhiyun 	if (ret < 0) {
88*4882a593Smuzhiyun 		pr_err("register_kretprobe failed, returned %d\n", ret);
89*4882a593Smuzhiyun 		return ret;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 	pr_info("Planted return probe at %s: %p\n",
92*4882a593Smuzhiyun 			my_kretprobe.kp.symbol_name, my_kretprobe.kp.addr);
93*4882a593Smuzhiyun 	return 0;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
kretprobe_exit(void)96*4882a593Smuzhiyun static void __exit kretprobe_exit(void)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	unregister_kretprobe(&my_kretprobe);
99*4882a593Smuzhiyun 	pr_info("kretprobe at %p unregistered\n", my_kretprobe.kp.addr);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* nmissed > 0 suggests that maxactive was set too low. */
102*4882a593Smuzhiyun 	pr_info("Missed probing %d instances of %s\n",
103*4882a593Smuzhiyun 		my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun module_init(kretprobe_init)
107*4882a593Smuzhiyun module_exit(kretprobe_exit)
108*4882a593Smuzhiyun MODULE_LICENSE("GPL");
109