0

我使用来自 python bcc 模块的 bpf,我希望我的探测函数将打印当前文件的文件路径(一种自定义的简化 opensnoop)。我怎样才能做到这一点?

这是我到目前为止所拥有的:

b = BPF(text="""
#include <linux/ptrace.h>      
#include<linux/sched.h>

BPF_HASH(last);      

int trace_entry(struct pt_regs *ctx)      
{
    char fileName[200] = {0};
    bpf_probe_read(fileName, sizeof(fileName), &PT_REGS_PARM1(ctx));  
    bpf_trace_printk("File Opened<%s>\\n", fileName);      
    return 0;
}
""")

print("Tracing for open... Ctrl-C to end")
b.attach_kprobe(event="do_sys_open", fn_name="trace_entry")
#b.attach_kprobe(event=b.get_syscall_fnname("open"), fn_name='funcky')
b.trace_print()
4

1 回答 1

0

简单:
将其插入内核代码:

// Nicer way to call bpf_trace_printk()
#define bpf_custom_printk(fmt, ...)                     \
        ({                                              \
            char ____fmt[] = fmt;                       \
            bpf_trace_printk(____fmt, sizeof(____fmt),  \
                    ##__VA_ARGS__);                     \
        })
struct pair {
    uint32_t lip; // local IP
    uint32_t rip; // remote IP
};

并在内核代码中插入打印代码。此函数调用printf与所有格式和占位符完全一样...

bpf_custom_printk("This year is %d\n", 2020);

打印输出:

sudo cat /sys/kernel/debug/tracing/trace_pipe
于 2020-03-11T11:50:20.213 回答