Path: blob/master/samples/kprobes/kretprobe_example.c
10818 views
/*1* kretprobe_example.c2*3* Here's a sample kernel module showing the use of return probes to4* report the return value and total time taken for probed function5* to run.6*7* usage: insmod kretprobe_example.ko func=<func_name>8*9* If no func_name is specified, do_fork is instrumented10*11* For more information on theory of operation of kretprobes, see12* Documentation/kprobes.txt13*14* Build and insert the kernel module as done in the kprobe example.15* You will see the trace data in /var/log/messages and on the console16* whenever the probed function returns. (Some messages may be suppressed17* if syslogd is configured to eliminate duplicate messages.)18*/1920#include <linux/kernel.h>21#include <linux/module.h>22#include <linux/kprobes.h>23#include <linux/ktime.h>24#include <linux/limits.h>25#include <linux/sched.h>2627static 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");3132/* per-instance private data */33struct my_data {34ktime_t entry_stamp;35};3637/* Here we use the entry_hanlder to timestamp function entry */38static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)39{40struct my_data *data;4142if (!current->mm)43return 1; /* Skip kernel threads */4445data = (struct my_data *)ri->data;46data->entry_stamp = ktime_get();47return 0;48}4950/*51* Return-probe handler: Log the return value and duration. Duration may turn52* out to be zero consistently, depending upon the granularity of time53* accounting on the platform.54*/55static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)56{57int retval = regs_return_value(regs);58struct my_data *data = (struct my_data *)ri->data;59s64 delta;60ktime_t now;6162now = ktime_get();63delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));64printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",65func_name, retval, (long long)delta);66return 0;67}6869static 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};7677static int __init kretprobe_init(void)78{79int ret;8081my_kretprobe.kp.symbol_name = func_name;82ret = register_kretprobe(&my_kretprobe);83if (ret < 0) {84printk(KERN_INFO "register_kretprobe failed, returned %d\n",85ret);86return -1;87}88printk(KERN_INFO "Planted return probe at %s: %p\n",89my_kretprobe.kp.symbol_name, my_kretprobe.kp.addr);90return 0;91}9293static void __exit kretprobe_exit(void)94{95unregister_kretprobe(&my_kretprobe);96printk(KERN_INFO "kretprobe at %p unregistered\n",97my_kretprobe.kp.addr);9899/* nmissed > 0 suggests that maxactive was set too low. */100printk(KERN_INFO "Missed probing %d instances of %s\n",101my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);102}103104module_init(kretprobe_init)105module_exit(kretprobe_exit)106MODULE_LICENSE("GPL");107108109