如何在bpftrace中使用内核宏函数?

问题描述 投票:0回答:1

如何在 bpftrace 中使用内核宏函数,如 ip_hdr。

我写了下面的代码:

#include <linux/ip.h>

kprobe:icmp_echo
{
    $skb = (struct sk_buff *)arg0;
    $iph = (struct iphdr *)ip_hdr($skb);
    printf("sip:%x", $iph->saddr);
    printf("dip:%x", $iph->daddr);
}

'ip_hdr'是一个宏函数,定义在ip.h中,但是遇到错误:

bpftrace icmp1.bt
icmp1.bt:6:12-35: ERROR: Unknown function: ip_hdr
    $iph = (struct iphdr *)ip_hdr($skb);

有人知道为什么吗?谢谢。

ebpf bpftrace
1个回答
0
投票

您正在编译什么内核版本?在我的

/usr/src/kernels/6.8.11-300.fc40.x86_64/include/linux/ip.h
中,
ip_hdr
没有定义为宏,而是定义为静态内联函数,我怀疑 bpftrace 处理得不好。

static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
{
        return (struct iphdr *)skb_network_header(skb);
}
...
static inline unsigned char *skb_network_header(const struct sk_buff *skb)
{
        return skb->head + skb->network_header;
}

虽然它只是

skb_network_header
的包装器,但它也是一个内联函数。然而函数体很简单,你可以自己内联它

    $iph = (struct iphdr *)($skb->head + $skb->network_header);
© www.soinside.com 2019 - 2024. All rights reserved.