内核层代码
#include <linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
static int (*bpf_trace_printk)(const char *fmt, int fmt_size,
...) = (void *)BPF_FUNC_trace_printk;
// 通过SEC()宏定义的数据结构和函数,会放到特定的ELF段中;
// 这样后续在加载BPF字节码时,就可以从这些段中获取所需的元数据。
SEC("tracepoint/syscalls/sys_enter_execve")
int bpf_prog(void *ctx) {
static const char msg[] = "Hello, BPF world!";
bpf_trace_printk(msg, sizeof(msg));
return 0;
}
char _license[] SEC("license") = "GPL";
//用户层代码
#include <linux/types.h>
#include <sys/resource.h>
//#include <bpf/bpf_helpers.h>
#include "bpf.skel.h"
int main() {
struct bpf1_k *skel = bpf1_k__open();
bpf1_k__load(skel);
bpf1_k__attach(skel);
// 打印内核调试文件的内容
system("cat /sys/kernel/debug/tracing/trace_pipe");
return 0;
}