aboutsummaryrefslogtreecommitdiffstats
path: root/samples/kprobes/kretprobe_example.c
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2008-03-18 02:44:31 -0400
committerDavid S. Miller <davem@davemloft.net>2008-03-18 02:44:31 -0400
commit2f633928cbba8a5858bb39b11e7219a41b0fbef5 (patch)
tree9a82f4b7f2c3afe4b0208d8e44ea61bae90a7d22 /samples/kprobes/kretprobe_example.c
parent5e226e4d9016daee170699f8a4188a5505021756 (diff)
parentbde4f8fa8db2abd5ac9c542d76012d0fedab050f (diff)
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
Diffstat (limited to 'samples/kprobes/kretprobe_example.c')
-rw-r--r--samples/kprobes/kretprobe_example.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/samples/kprobes/kretprobe_example.c b/samples/kprobes/kretprobe_example.c
new file mode 100644
index 000000000000..4e764b317d61
--- /dev/null
+++ b/samples/kprobes/kretprobe_example.c
@@ -0,0 +1,106 @@
1/*
2 * kretprobe_example.c
3 *
4 * Here's a sample kernel module showing the use of return probes to
5 * report the return value and total time taken for probed function
6 * to run.
7 *
8 * usage: insmod kretprobe_example.ko func=<func_name>
9 *
10 * If no func_name is specified, do_fork is instrumented
11 *
12 * For more information on theory of operation of kretprobes, see
13 * Documentation/kprobes.txt
14 *
15 * Build and insert the kernel module as done in the kprobe example.
16 * You will see the trace data in /var/log/messages and on the console
17 * whenever the probed function returns. (Some messages may be suppressed
18 * if syslogd is configured to eliminate duplicate messages.)
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/kprobes.h>
24#include <linux/ktime.h>
25#include <linux/limits.h>
26
27static char func_name[NAME_MAX] = "do_fork";
28module_param_string(func, func_name, NAME_MAX, S_IRUGO);
29MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
30 " function's execution time");
31
32/* per-instance private data */
33struct my_data {
34 ktime_t entry_stamp;
35};
36
37/* Here we use the entry_hanlder to timestamp function entry */
38static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
39{
40 struct my_data *data;
41
42 if (!current->mm)
43 return 1; /* Skip kernel threads */
44
45 data = (struct my_data *)ri->data;
46 data->entry_stamp = ktime_get();
47 return 0;
48}
49
50/*
51 * Return-probe handler: Log the return value and duration. Duration may turn
52 * out to be zero consistently, depending upon the granularity of time
53 * accounting on the platform.
54 */
55static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
56{
57 int retval = regs_return_value(regs);
58 struct my_data *data = (struct my_data *)ri->data;
59 s64 delta;
60 ktime_t now;
61
62 now = ktime_get();
63 delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
64 printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
65 func_name, retval, (long long)delta);
66 return 0;
67}
68
69static struct kretprobe my_kretprobe = {
70 .handler = ret_handler,
71 .entry_handler = entry_handler,
72 .data_size = sizeof(struct my_data),
73 /* Probe up to 20 instances concurrently. */
74 .maxactive = 20,
75};
76
77static int __init kretprobe_init(void)
78{
79 int ret;
80
81 my_kretprobe.kp.symbol_name = func_name;
82 ret = register_kretprobe(&my_kretprobe);
83 if (ret < 0) {
84 printk(KERN_INFO "register_kretprobe failed, returned %d\n",
85 ret);
86 return -1;
87 }
88 printk(KERN_INFO "Planted return probe at %s: %p\n",
89 my_kretprobe.kp.symbol_name, my_kretprobe.kp.addr);
90 return 0;
91}
92
93static void __exit kretprobe_exit(void)
94{
95 unregister_kretprobe(&my_kretprobe);
96 printk(KERN_INFO "kretprobe at %p unregistered\n",
97 my_kretprobe.kp.addr);
98
99 /* nmissed > 0 suggests that maxactive was set too low. */
100 printk(KERN_INFO "Missed probing %d instances of %s\n",
101 my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);
102}
103
104module_init(kretprobe_init)
105module_exit(kretprobe_exit)
106MODULE_LICENSE("GPL");