簡體   English   中英

如何使用 ebpf python 從打開的系統調用打印文件路徑?

[英]How to print the file path from an open syscall using ebpf python?

我使用 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()

簡單:
在內核代碼中插入:

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM