很多人估计有需求使用kretprobe,但是苦于传递的参数保存在寄存中,不知道其对应关系,这里简单给了一个例子,方便大家使用,平台是X86
/*
* * kretprobe_example.c
* *
* * Here's a sample kernel module showing the use of return probes to
* * report the return value and total time taken for probed function
* * to run.
* *
* * usage: insmod kretprobe_example.ko func=<func_name>
* *
* * If no func_name is specified, do_fork is instrumented
* *
* * For more information on theory of operation of kretprobes, see
* * Documentation/kprobes.txt
* *
* * Build and insert the kernel module as done in the kprobe example.
* * You will see the trace data in /var/log/messages and on the console
* * whenever the probed function returns. (Some messages may be suppressed
* * if syslogd is configured to eliminate duplicate messages.)
* */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/ktime.h>
#include <linux/limits.h>
#include <linux/sched.h>
#include <linux/tcp.h>
#include <net/tcp.h>
static char func_name[NAME_MAX] = "tcp_rcv_established";
module_param_string(func, func_name, NAME_MAX, S_IRUGO);
MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
" function's execution time");
#if 0
struct pt_regs {
unsigned long r15;
unsigned long r14;
unsigned long r13;
unsigned long r12;
unsigned long bp;
unsigned long bx;
/* arguments: non interrupts/non tracing syscalls only save up to here*/
unsigned long r11;
unsigned long r10;
unsigned long r9;//第六个参数
unsigned long r8;//第五个参数
unsigned long ax;//返回值
unsigned long cx;//第四个参数
unsigned long dx;//第三个参数
unsigned long si;//第二个参数
unsigned long di;//第一个参数
unsigned long orig_ax;
/* end of arguments */
/* cpu exception frame or undefined */
unsigned long ip;
unsigned long cs;
unsigned long flags;
unsigned long sp;
unsigned long ss;
/* top of stack page */
};
#endif
/* per-instance private data */
struct my_data {
ktime_t entry_stamp;
};
/* Here we use the entry_hanlder to timestamp function entry */
static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{
struct sock *sk;
struct sk_buff *skb;
const struct tcphdr *th;
const struct iphdr *iph;
unsigned int sip = 0;
unsigned int dip = 0;
unsigned short sport = 0;
unsigned short dport = 0;
unsigned int seq = 0;
unsigned int len = 0;
unsigned int ssk_saddr = 0;
unsigned int ssk_daddr = 0;
unsigned short ssk_sport = 0;
unsigned short ssk_dport = 0;
unsigned int ack_seq = 0;
unsigned int seq_end = 0;
if (!current->mm)
return 1; /* Skip kernel threads */
sk = (struct sock *)(regs->di);
skb = (struct sk_buff *)(regs->si);
th = (struct tcphdr *)(regs->dx);
iph = ip_hdr(skb);
len = (int)(regs->cx);
if (sk == NULL || skb == NULL || th == NULL || iph == NULL)
return 0;
sport = ntohs(th->source);
dport = ntohs(th->dest);
sip = iph->saddr;
dip = iph->daddr;
seq = ntohl(th->seq);
seq_end = (TCP_SKB_CB(skb)->seq + th->syn + th->fin + skb->len - th->doff * 4);
ack_seq = ntohl(th->ack_seq);
ssk_daddr = sk->sk_rcv_saddr;
ssk_dport = ntohs(sk->sk_dport);
ssk_saddr = sk->sk_daddr;
ssk_sport = sk->sk_num;
if (sport == 80 && len > sizeof(struct tcphdr))
printk("%s %pI4 %u --> %pI4 %u seq %u-%u len %u ack %u\n",
__func__, &sip, sport, &dip, dport,
seq, seq_end , (skb->len - th->doff * 4) , ack_seq);
return 0;
}
/*
* * Return-probe handler: Log the return value and duration. Duration may turn
* * out to be zero consistently, depending upon the granularity of time
* * accounting on the platform.
* */
static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{
int retval = regs_return_value(regs);
return 0;
}
static struct kretprobe my_kretprobe = {
.handler = ret_handler,
.entry_handler = entry_handler,
.data_size = sizeof(struct my_data),
/* Probe up to 20 instances concurrently. */
.maxactive = 20,
};
static int __init kretprobe_init(void)
{
int ret;
my_kretprobe.kp.symbol_name = func_name;
ret = register_kretprobe(&my_kretprobe);
if (ret < 0) {
printk(KERN_INFO "register_kretprobe failed, returned %d\n",
ret);
return -1;
}
printk(KERN_INFO "Planted return probe at %s: %p\n",
my_kretprobe.kp.symbol_name, my_kretprobe.kp.addr);
return 0;
}
static void __exit kretprobe_exit(void)
{
unregister_kretprobe(&my_kretprobe);
printk(KERN_INFO "kretprobe at %p unregistered\n",
my_kretprobe.kp.addr);
/* nmissed > 0 suggests that maxactive was set too low. */
printk(KERN_INFO "Missed probing %d instances of %s\n",
my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);
}
module_init(kretprobe_init)
module_exit(kretprobe_exit)
MODULE_LICENSE("GPL");