简体   繁体   中英

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

I use bpf from the python bcc module, and I want that my probe function will print the file path of the current file (kind of a custom simplified opensnoop). How can I do that?

This is what I have so far:

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()

Easy:
Insert this in kernel code:

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

and in kernel code insert print out code. This function call works exactly like printf with all format and placeholder ...

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

Print output:

sudo cat /sys/kernel/debug/tracing/trace_pipe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM