blob: 186315ca88b3f7c01d45bf463de15bbcd935a905 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -08002/*
3 * kretprobe_example.c
4 *
5 * Here's a sample kernel module showing the use of return probes to
6 * report the return value and total time taken for probed function
7 * to run.
8 *
9 * usage: insmod kretprobe_example.ko func=<func_name>
10 *
Petr Mladek54aea452015-10-01 15:37:11 -070011 * If no func_name is specified, _do_fork is instrumented
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080012 *
13 * For more information on theory of operation of kretprobes, see
14 * Documentation/kprobes.txt
15 *
16 * Build and insert the kernel module as done in the kprobe example.
17 * You will see the trace data in /var/log/messages and on the console
18 * whenever the probed function returns. (Some messages may be suppressed
19 * if syslogd is configured to eliminate duplicate messages.)
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>
Alexey Dobriyan8abf9192009-08-13 10:05:43 +000027#include <linux/sched.h>
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080028
Petr Mladek54aea452015-10-01 15:37:11 -070029static char func_name[NAME_MAX] = "_do_fork";
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080030module_param_string(func, func_name, NAME_MAX, S_IRUGO);
31MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
32 " function's execution time");
33
34/* per-instance private data */
35struct my_data {
36 ktime_t entry_stamp;
37};
38
39/* Here we use the entry_hanlder to timestamp function entry */
40static 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; /* Skip kernel threads */
46
47 data = (struct my_data *)ri->data;
48 data->entry_stamp = ktime_get();
49 return 0;
50}
51
52/*
53 * Return-probe handler: Log the return value and duration. Duration may turn
54 * out to be zero consistently, depending upon the granularity of time
55 * accounting on the platform.
56 */
57static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
58{
Huang Shijie57c24b22016-08-03 13:46:12 -070059 unsigned long retval = regs_return_value(regs);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080060 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));
Huang Shijie57c24b22016-08-03 13:46:12 -070066 pr_info("%s returned %lu and took %lld ns to execute\n",
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080067 func_name, retval, (long long)delta);
68 return 0;
69}
70
71static struct kretprobe my_kretprobe = {
72 .handler = ret_handler,
73 .entry_handler = entry_handler,
74 .data_size = sizeof(struct my_data),
75 /* Probe up to 20 instances concurrently. */
76 .maxactive = 20,
77};
78
79static 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) {
Huang Shijie61341312016-08-03 13:46:09 -070086 pr_err("register_kretprobe failed, returned %d\n", ret);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080087 return -1;
88 }
Huang Shijie61341312016-08-03 13:46:09 -070089 pr_info("Planted return probe at %s: %p\n",
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080090 my_kretprobe.kp.symbol_name, my_kretprobe.kp.addr);
91 return 0;
92}
93
94static void __exit kretprobe_exit(void)
95{
96 unregister_kretprobe(&my_kretprobe);
Huang Shijie61341312016-08-03 13:46:09 -070097 pr_info("kretprobe at %p unregistered\n", my_kretprobe.kp.addr);
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -080098
99 /* nmissed > 0 suggests that maxactive was set too low. */
Huang Shijie61341312016-08-03 13:46:09 -0700100 pr_info("Missed probing %d instances of %s\n",
Ananth N Mavinakayanahalli804defe2008-03-04 14:28:38 -0800101 my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);
102}
103
104module_init(kretprobe_init)
105module_exit(kretprobe_exit)
106MODULE_LICENSE("GPL");