This source file includes following definitions.
- entry_handler
- ret_handler
- kretprobe_init
- kretprobe_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/kprobes.h>
25 #include <linux/ktime.h>
26 #include <linux/limits.h>
27 #include <linux/sched.h>
28
29 static char func_name[NAME_MAX] = "_do_fork";
30 module_param_string(func, func_name, NAME_MAX, S_IRUGO);
31 MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
32 " function's execution time");
33
34
35 struct my_data {
36 ktime_t entry_stamp;
37 };
38
39
40 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
41 {
42 struct my_data *data;
43
44 if (!current->mm)
45 return 1;
46
47 data = (struct my_data *)ri->data;
48 data->entry_stamp = ktime_get();
49 return 0;
50 }
51
52
53
54
55
56
57 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
58 {
59 unsigned long retval = regs_return_value(regs);
60 struct my_data *data = (struct my_data *)ri->data;
61 s64 delta;
62 ktime_t now;
63
64 now = ktime_get();
65 delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
66 pr_info("%s returned %lu and took %lld ns to execute\n",
67 func_name, retval, (long long)delta);
68 return 0;
69 }
70
71 static struct kretprobe my_kretprobe = {
72 .handler = ret_handler,
73 .entry_handler = entry_handler,
74 .data_size = sizeof(struct my_data),
75
76 .maxactive = 20,
77 };
78
79 static int __init kretprobe_init(void)
80 {
81 int ret;
82
83 my_kretprobe.kp.symbol_name = func_name;
84 ret = register_kretprobe(&my_kretprobe);
85 if (ret < 0) {
86 pr_err("register_kretprobe failed, returned %d\n", ret);
87 return -1;
88 }
89 pr_info("Planted return probe at %s: %p\n",
90 my_kretprobe.kp.symbol_name, my_kretprobe.kp.addr);
91 return 0;
92 }
93
94 static void __exit kretprobe_exit(void)
95 {
96 unregister_kretprobe(&my_kretprobe);
97 pr_info("kretprobe at %p unregistered\n", my_kretprobe.kp.addr);
98
99
100 pr_info("Missed probing %d instances of %s\n",
101 my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);
102 }
103
104 module_init(kretprobe_init)
105 module_exit(kretprobe_exit)
106 MODULE_LICENSE("GPL");