Path: blob/master/samples/kprobes/jprobe_example.c
10818 views
/*1* Here's a sample kernel module showing the use of jprobes to dump2* the arguments of do_fork().3*4* For more information on theory of operation of jprobes, see5* Documentation/kprobes.txt6*7* Build and insert the kernel module as done in the kprobe example.8* You will see the trace data in /var/log/messages and on the9* console whenever do_fork() is invoked to create a new process.10* (Some messages may be suppressed if syslogd is configured to11* eliminate duplicate messages.)12*/1314#include <linux/kernel.h>15#include <linux/module.h>16#include <linux/kprobes.h>1718/*19* Jumper probe for do_fork.20* Mirror principle enables access to arguments of the probed routine21* from the probe handler.22*/2324/* Proxy routine having the same arguments as actual do_fork() routine */25static long jdo_fork(unsigned long clone_flags, unsigned long stack_start,26struct pt_regs *regs, unsigned long stack_size,27int __user *parent_tidptr, int __user *child_tidptr)28{29printk(KERN_INFO "jprobe: clone_flags = 0x%lx, stack_size = 0x%lx,"30" regs = 0x%p\n",31clone_flags, stack_size, regs);3233/* Always end with a call to jprobe_return(). */34jprobe_return();35return 0;36}3738static struct jprobe my_jprobe = {39.entry = jdo_fork,40.kp = {41.symbol_name = "do_fork",42},43};4445static int __init jprobe_init(void)46{47int ret;4849ret = register_jprobe(&my_jprobe);50if (ret < 0) {51printk(KERN_INFO "register_jprobe failed, returned %d\n", ret);52return -1;53}54printk(KERN_INFO "Planted jprobe at %p, handler addr %p\n",55my_jprobe.kp.addr, my_jprobe.entry);56return 0;57}5859static void __exit jprobe_exit(void)60{61unregister_jprobe(&my_jprobe);62printk(KERN_INFO "jprobe at %p unregistered\n", my_jprobe.kp.addr);63}6465module_init(jprobe_init)66module_exit(jprobe_exit)67MODULE_LICENSE("GPL");686970