mirror of
https://github.com/yanfeizhang/coder-kung-fu.git
synced 2026-02-03 02:23:33 +08:00
feat: add first ebpf program
This commit is contained in:
49
tests/ebpf/test01/hello.c
Normal file
49
tests/ebpf/test01/hello.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <bpf/libbpf.h>
|
||||
#include "hello_kprobe.skel.h"
|
||||
|
||||
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) {
|
||||
return vfprintf(stderr, format, args);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
struct hello_kprobe_bpf *skel;
|
||||
int err;
|
||||
|
||||
// 设置 libbpf 的日志输出函数
|
||||
libbpf_set_print(libbpf_print_fn);
|
||||
|
||||
// 打开并加载 eBPF 对象
|
||||
skel = hello_kprobe_bpf__open();
|
||||
if (!skel) {
|
||||
fprintf(stderr, "Failed to open BPF skeleton\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 加载 eBPF 对象
|
||||
err = hello_kprobe_bpf__load(skel);
|
||||
if (err) {
|
||||
fprintf(stderr, "Failed to load BPF skeleton: %d\n", err);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// 附加 kprobe
|
||||
err = hello_kprobe_bpf__attach(skel);
|
||||
if (err) {
|
||||
fprintf(stderr, "Failed to attach BPF skeleton: %d\n", err);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
printf("Kprobe attached successfully. Press Ctrl+C to exit.\n");
|
||||
|
||||
// 保持程序运行,直到用户中断
|
||||
while (1) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
cleanup:
|
||||
// 清理资源
|
||||
hello_kprobe_bpf__destroy(skel);
|
||||
return err < 0 ? -err : 0;
|
||||
}
|
||||
12
tests/ebpf/test01/hello_kprobe.bpf.c
Normal file
12
tests/ebpf/test01/hello_kprobe.bpf.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <linux/bpf.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
// 定义一个打印信息的函数
|
||||
SEC("kprobe/handle_mm_fault")
|
||||
int bpf_probe_handle_mm_fault() {
|
||||
// 打印一条消息到内核调试日志
|
||||
bpf_printk("Hello, World from kprobe!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
298
tests/ebpf/test01/hello_kprobe.skel.h
Normal file
298
tests/ebpf/test01/hello_kprobe.skel.h
Normal file
@@ -0,0 +1,298 @@
|
||||
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
||||
|
||||
/* THIS FILE IS AUTOGENERATED! */
|
||||
#ifndef __HELLO_KPROBE_BPF_SKEL_H__
|
||||
#define __HELLO_KPROBE_BPF_SKEL_H__
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <bpf/libbpf.h>
|
||||
|
||||
struct hello_kprobe_bpf {
|
||||
struct bpf_object_skeleton *skeleton;
|
||||
struct bpf_object *obj;
|
||||
struct {
|
||||
struct bpf_map *rodata;
|
||||
} maps;
|
||||
struct {
|
||||
struct bpf_program *bpf_probe_handle_mm_fault;
|
||||
} progs;
|
||||
struct {
|
||||
struct bpf_link *bpf_probe_handle_mm_fault;
|
||||
} links;
|
||||
struct hello_kprobe_bpf__rodata {
|
||||
} *rodata;
|
||||
};
|
||||
|
||||
static void
|
||||
hello_kprobe_bpf__destroy(struct hello_kprobe_bpf *obj)
|
||||
{
|
||||
if (!obj)
|
||||
return;
|
||||
if (obj->skeleton)
|
||||
bpf_object__destroy_skeleton(obj->skeleton);
|
||||
free(obj);
|
||||
}
|
||||
|
||||
static inline int
|
||||
hello_kprobe_bpf__create_skeleton(struct hello_kprobe_bpf *obj);
|
||||
|
||||
static inline struct hello_kprobe_bpf *
|
||||
hello_kprobe_bpf__open_opts(const struct bpf_object_open_opts *opts)
|
||||
{
|
||||
struct hello_kprobe_bpf *obj;
|
||||
int err;
|
||||
|
||||
obj = (struct hello_kprobe_bpf *)calloc(1, sizeof(*obj));
|
||||
if (!obj) {
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = hello_kprobe_bpf__create_skeleton(obj);
|
||||
err = err ?: bpf_object__open_skeleton(obj->skeleton, opts);
|
||||
if (err)
|
||||
goto err_out;
|
||||
|
||||
return obj;
|
||||
err_out:
|
||||
hello_kprobe_bpf__destroy(obj);
|
||||
errno = -err;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline struct hello_kprobe_bpf *
|
||||
hello_kprobe_bpf__open(void)
|
||||
{
|
||||
return hello_kprobe_bpf__open_opts(NULL);
|
||||
}
|
||||
|
||||
static inline int
|
||||
hello_kprobe_bpf__load(struct hello_kprobe_bpf *obj)
|
||||
{
|
||||
return bpf_object__load_skeleton(obj->skeleton);
|
||||
}
|
||||
|
||||
static inline struct hello_kprobe_bpf *
|
||||
hello_kprobe_bpf__open_and_load(void)
|
||||
{
|
||||
struct hello_kprobe_bpf *obj;
|
||||
int err;
|
||||
|
||||
obj = hello_kprobe_bpf__open();
|
||||
if (!obj)
|
||||
return NULL;
|
||||
err = hello_kprobe_bpf__load(obj);
|
||||
if (err) {
|
||||
hello_kprobe_bpf__destroy(obj);
|
||||
errno = -err;
|
||||
return NULL;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
static inline int
|
||||
hello_kprobe_bpf__attach(struct hello_kprobe_bpf *obj)
|
||||
{
|
||||
return bpf_object__attach_skeleton(obj->skeleton);
|
||||
}
|
||||
|
||||
static inline void
|
||||
hello_kprobe_bpf__detach(struct hello_kprobe_bpf *obj)
|
||||
{
|
||||
return bpf_object__detach_skeleton(obj->skeleton);
|
||||
}
|
||||
|
||||
static inline int
|
||||
hello_kprobe_bpf__create_skeleton(struct hello_kprobe_bpf *obj)
|
||||
{
|
||||
struct bpf_object_skeleton *s;
|
||||
|
||||
s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
goto err;
|
||||
|
||||
s->sz = sizeof(*s);
|
||||
s->name = "hello_kprobe_bpf";
|
||||
s->obj = &obj->obj;
|
||||
|
||||
/* maps */
|
||||
s->map_cnt = 1;
|
||||
s->map_skel_sz = sizeof(*s->maps);
|
||||
s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);
|
||||
if (!s->maps)
|
||||
goto err;
|
||||
|
||||
s->maps[0].name = "hello_kp.rodata";
|
||||
s->maps[0].map = &obj->maps.rodata;
|
||||
s->maps[0].mmaped = (void **)&obj->rodata;
|
||||
|
||||
/* programs */
|
||||
s->prog_cnt = 1;
|
||||
s->prog_skel_sz = sizeof(*s->progs);
|
||||
s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);
|
||||
if (!s->progs)
|
||||
goto err;
|
||||
|
||||
s->progs[0].name = "bpf_probe_handle_mm_fault";
|
||||
s->progs[0].prog = &obj->progs.bpf_probe_handle_mm_fault;
|
||||
s->progs[0].link = &obj->links.bpf_probe_handle_mm_fault;
|
||||
|
||||
s->data_sz = 4200;
|
||||
s->data = (void *)"\
|
||||
\x7f\x45\x4c\x46\x02\x01\x01\0\0\0\0\0\0\0\0\0\x01\0\xf7\0\x01\0\0\0\0\0\0\0\0\
|
||||
\0\0\0\0\0\0\0\0\0\0\0\x28\x0b\0\0\0\0\0\0\0\0\0\0\x40\0\0\0\0\0\x40\0\x15\0\
|
||||
\x01\0\x18\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xb7\x02\0\0\x1b\0\0\0\x85\0\0\0\x06\
|
||||
\0\0\0\xb7\0\0\0\0\0\0\0\x95\0\0\0\0\0\0\0\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\
|
||||
\x72\x6c\x64\x20\x66\x72\x6f\x6d\x20\x6b\x70\x72\x6f\x62\x65\x21\x0a\0\x47\x50\
|
||||
\x4c\0\x01\x11\x01\x25\x0e\x13\x05\x03\x0e\x10\x17\x1b\x0e\x11\x01\x12\x06\0\0\
|
||||
\x02\x2e\x01\x11\x01\x12\x06\x40\x18\x97\x42\x19\x03\x0e\x3a\x0b\x3b\x0b\x49\
|
||||
\x13\x3f\x19\0\0\x03\x34\0\x03\x0e\x49\x13\x3a\x0b\x3b\x0b\x02\x18\0\0\x04\x01\
|
||||
\x01\x49\x13\0\0\x05\x21\0\x49\x13\x37\x0b\0\0\x06\x26\0\x49\x13\0\0\x07\x24\0\
|
||||
\x03\x0e\x3e\x0b\x0b\x0b\0\0\x08\x24\0\x03\x0e\x0b\x0b\x3e\x0b\0\0\x09\x34\0\
|
||||
\x03\x0e\x49\x13\x3a\x0b\x3b\x0b\0\0\x0a\x0f\0\x49\x13\0\0\x0b\x15\x01\x49\x13\
|
||||
\x27\x19\0\0\x0c\x05\0\x49\x13\0\0\x0d\x18\0\0\0\x0e\x16\0\x49\x13\x03\x0e\x3a\
|
||||
\x0b\x3b\x0b\0\0\x0f\x34\0\x03\x0e\x49\x13\x3f\x19\x3a\x0b\x3b\x0b\x02\x18\0\0\
|
||||
\0\xe1\0\0\0\x04\0\0\0\0\0\x08\x01\0\0\0\0\x0c\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
|
||||
\0\0\0\0\0\x30\0\0\0\x02\0\0\0\0\0\0\0\0\x30\0\0\0\x01\x5a\0\0\0\0\x01\x06\xdd\
|
||||
\0\0\0\x03\0\0\0\0\x59\0\0\0\x01\x08\x09\x03\0\0\0\0\0\0\0\0\0\x04\x65\0\0\0\
|
||||
\x05\x71\0\0\0\x1b\0\x06\x6a\0\0\0\x07\0\0\0\0\x06\x01\x08\0\0\0\0\x08\x07\x09\
|
||||
\0\0\0\0\x83\0\0\0\x03\xb9\x06\x88\0\0\0\x0a\x8d\0\0\0\x0b\x9e\0\0\0\x0c\xa5\0\
|
||||
\0\0\x0c\xaa\0\0\0\x0d\0\x07\0\0\0\0\x05\x08\x0a\x65\0\0\0\x0e\xb5\0\0\0\0\0\0\
|
||||
\0\x02\x1b\x07\0\0\0\0\x07\x04\x0f\0\0\0\0\xd1\0\0\0\x01\x0c\x09\x03\0\0\0\0\0\
|
||||
\0\0\0\x04\x6a\0\0\0\x05\x71\0\0\0\x04\0\x07\0\0\0\0\x05\x04\0\x44\x65\x62\x69\
|
||||
\x61\x6e\x20\x63\x6c\x61\x6e\x67\x20\x76\x65\x72\x73\x69\x6f\x6e\x20\x31\x31\
|
||||
\x2e\x30\x2e\x31\x2d\x32\x7e\x64\x65\x62\x31\x30\x75\x31\0\x68\x65\x6c\x6c\x6f\
|
||||
\x5f\x6b\x70\x72\x6f\x62\x65\x2e\x62\x70\x66\x2e\x63\0\x2f\x68\x6f\x6d\x65\x2f\
|
||||
\x7a\x68\x61\x6e\x67\x79\x61\x6e\x66\x65\x69\x2e\x61\x6c\x6c\x65\x6e\x2f\x77\
|
||||
\x6f\x72\x6b\x5f\x6d\x79\x2f\x70\x65\x72\x66\x6f\x72\x6d\x61\x6e\x63\x65\x2f\
|
||||
\x74\x65\x73\x74\x73\x2f\x65\x62\x70\x66\x2f\x74\x6f\x70\x69\x63\x30\x30\x37\0\
|
||||
\x5f\x5f\x5f\x5f\x66\x6d\x74\0\x63\x68\x61\x72\0\x5f\x5f\x41\x52\x52\x41\x59\
|
||||
\x5f\x53\x49\x5a\x45\x5f\x54\x59\x50\x45\x5f\x5f\0\x62\x70\x66\x5f\x74\x72\x61\
|
||||
\x63\x65\x5f\x70\x72\x69\x6e\x74\x6b\0\x6c\x6f\x6e\x67\x20\x69\x6e\x74\0\x75\
|
||||
\x6e\x73\x69\x67\x6e\x65\x64\x20\x69\x6e\x74\0\x5f\x5f\x75\x33\x32\0\x5f\x6c\
|
||||
\x69\x63\x65\x6e\x73\x65\0\x62\x70\x66\x5f\x70\x72\x6f\x62\x65\x5f\x68\x61\x6e\
|
||||
\x64\x6c\x65\x5f\x6d\x6d\x5f\x66\x61\x75\x6c\x74\0\x69\x6e\x74\0\x9f\xeb\x01\0\
|
||||
\x18\0\0\0\0\0\0\0\xd4\0\0\0\xd4\0\0\0\x1a\x01\0\0\0\0\0\0\0\0\0\x0d\x02\0\0\0\
|
||||
\x01\0\0\0\0\0\0\x01\x04\0\0\0\x20\0\0\x01\x05\0\0\0\x01\0\0\x0c\x01\0\0\0\0\0\
|
||||
\0\0\0\0\0\x0a\x05\0\0\0\xc6\0\0\0\0\0\0\x01\x01\0\0\0\x08\0\0\x01\0\0\0\0\0\0\
|
||||
\0\x03\0\0\0\0\x04\0\0\0\x07\0\0\0\x1b\0\0\0\xcb\0\0\0\0\0\0\x01\x04\0\0\0\x20\
|
||||
\0\0\0\xdf\0\0\0\0\0\0\x0e\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x03\0\0\0\0\x05\0\0\
|
||||
\0\x07\0\0\0\x04\0\0\0\x01\x01\0\0\0\0\0\x0e\x09\0\0\0\x01\0\0\0\x0a\x01\0\0\
|
||||
\x01\0\0\x0f\0\0\0\0\x08\0\0\0\0\0\0\0\x1b\0\0\0\x12\x01\0\0\x01\0\0\x0f\0\0\0\
|
||||
\0\x0a\0\0\0\0\0\0\0\x04\0\0\0\0\x69\x6e\x74\0\x62\x70\x66\x5f\x70\x72\x6f\x62\
|
||||
\x65\x5f\x68\x61\x6e\x64\x6c\x65\x5f\x6d\x6d\x5f\x66\x61\x75\x6c\x74\0\x6b\x70\
|
||||
\x72\x6f\x62\x65\x2f\x68\x61\x6e\x64\x6c\x65\x5f\x6d\x6d\x5f\x66\x61\x75\x6c\
|
||||
\x74\0\x2f\x68\x6f\x6d\x65\x2f\x7a\x68\x61\x6e\x67\x79\x61\x6e\x66\x65\x69\x2e\
|
||||
\x61\x6c\x6c\x65\x6e\x2f\x77\x6f\x72\x6b\x5f\x6d\x79\x2f\x70\x65\x72\x66\x6f\
|
||||
\x72\x6d\x61\x6e\x63\x65\x2f\x74\x65\x73\x74\x73\x2f\x65\x62\x70\x66\x2f\x74\
|
||||
\x6f\x70\x69\x63\x30\x30\x37\x2f\x68\x65\x6c\x6c\x6f\x5f\x6b\x70\x72\x6f\x62\
|
||||
\x65\x2e\x62\x70\x66\x2e\x63\0\x20\x20\x20\x20\x62\x70\x66\x5f\x70\x72\x69\x6e\
|
||||
\x74\x6b\x28\x22\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x20\x66\x72\
|
||||
\x6f\x6d\x20\x6b\x70\x72\x6f\x62\x65\x21\x5c\x6e\x22\x29\x3b\0\x20\x20\x20\x20\
|
||||
\x72\x65\x74\x75\x72\x6e\x20\x30\x3b\0\x63\x68\x61\x72\0\x5f\x5f\x41\x52\x52\
|
||||
\x41\x59\x5f\x53\x49\x5a\x45\x5f\x54\x59\x50\x45\x5f\x5f\0\x62\x70\x66\x5f\x70\
|
||||
\x72\x6f\x62\x65\x5f\x68\x61\x6e\x64\x6c\x65\x5f\x6d\x6d\x5f\x66\x61\x75\x6c\
|
||||
\x74\x2e\x5f\x5f\x5f\x5f\x66\x6d\x74\0\x5f\x6c\x69\x63\x65\x6e\x73\x65\0\x2e\
|
||||
\x72\x6f\x64\x61\x74\x61\0\x6c\x69\x63\x65\x6e\x73\x65\0\x9f\xeb\x01\0\x20\0\0\
|
||||
\0\0\0\0\0\x14\0\0\0\x14\0\0\0\x2c\0\0\0\x40\0\0\0\0\0\0\0\x08\0\0\0\x1f\0\0\0\
|
||||
\x01\0\0\0\0\0\0\0\x03\0\0\0\x10\0\0\0\x1f\0\0\0\x02\0\0\0\0\0\0\0\x36\0\0\0\
|
||||
\x89\0\0\0\x05\x20\0\0\x20\0\0\0\x36\0\0\0\xb8\0\0\0\x05\x24\0\0\0\x0c\0\0\0\
|
||||
\xff\xff\xff\xff\x04\0\x08\0\x08\x7c\x0b\0\x14\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
|
||||
\x30\0\0\0\0\0\0\0\x93\0\0\0\x04\0\x77\0\0\0\x08\x01\x01\xfb\x0e\x0d\0\x01\x01\
|
||||
\x01\x01\0\0\0\x01\0\0\x01\x2f\x75\x73\x72\x2f\x69\x6e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x62\x65\x5f\x68\x61\x6e\x64\x6c\x65\x5f\x6d\x6d\x5f\x66\x61\
|
||||
\x75\x6c\x74\0\x2e\x72\x65\x6c\x6b\x70\x72\x6f\x62\x65\x2f\x68\x61\x6e\x64\x6c\
|
||||
\x65\x5f\x6d\x6d\x5f\x66\x61\x75\x6c\x74\0\x2e\x64\x65\x62\x75\x67\x5f\x73\x74\
|
||||
\x72\0\x2e\x72\x65\x6c\x2e\x64\x65\x62\x75\x67\x5f\x69\x6e\x66\x6f\0\x2e\x6c\
|
||||
\x6c\x76\x6d\x5f\x61\x64\x64\x72\x73\x69\x67\0\x5f\x6c\x69\x63\x65\x6e\x73\x65\
|
||||
\0\x2e\x72\x65\x6c\x2e\x64\x65\x62\x75\x67\x5f\x6c\x69\x6e\x65\0\x2e\x72\x65\
|
||||
\x6c\x2e\x64\x65\x62\x75\x67\x5f\x66\x72\x61\x6d\x65\0\x68\x65\x6c\x6c\x6f\x5f\
|
||||
\x6b\x70\x72\x6f\x62\x65\x2e\x62\x70\x66\x2e\x63\0\x2e\x73\x74\x72\x74\x61\x62\
|
||||
\0\x2e\x73\x79\x6d\x74\x61\x62\0\x2e\x72\x6f\x64\x61\x74\x61\0\x2e\x72\x65\x6c\
|
||||
\x2e\x42\x54\x46\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
|
||||
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
|
||||
\xdf\0\0\0\x03\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x23\x0a\0\0\0\0\0\0\0\x01\
|
||||
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x0f\0\0\0\x01\0\
|
||||
\0\0\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x40\0\0\0\0\0\0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0\0\0\0\0\0\0\0\0\
|
||||
\xf0\x09\0\0\0\0\0\0\x20\0\0\0\0\0\0\0\x14\0\0\0\x0f\0\0\0\x08\0\0\0\0\0\0\0\
|
||||
\x10\0\0\0\0\0\0\0\xaf\0\0\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xa0\x05\
|
||||
\0\0\0\0\0\0\x97\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
|
||||
\0\xab\0\0\0\x09\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x10\x0a\0\0\0\0\0\0\x10\
|
||||
\0\0\0\0\0\0\0\x14\0\0\0\x11\0\0\0\x08\0\0\0\0\0\0\0\x10\0\0\0\0\0\0\0\x94\0\0\
|
||||
\0\x03\x4c\xff\x6f\0\0\0\x80\0\0\0\0\0\0\0\0\0\0\0\0\x20\x0a\0\0\0\0\0\0\x03\0\
|
||||
\0\0\0\0\0\0\x14\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xe7\0\0\0\x02\
|
||||
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x38\x06\0\0\0\0\0\0\x28\x02\0\0\0\0\0\0\
|
||||
\x01\0\0\0\x15\0\0\0\x08\0\0\0\0\0\0\0\x18\0\0\0\0\0\0\0";
|
||||
|
||||
obj->skeleton = s;
|
||||
return 0;
|
||||
err:
|
||||
bpf_object__destroy_skeleton(s);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
#endif /* __HELLO_KPROBE_BPF_SKEL_H__ */
|
||||
61
tests/ebpf/test01/index.md
Normal file
61
tests/ebpf/test01/index.md
Normal file
@@ -0,0 +1,61 @@
|
||||
## 环境准备
|
||||
|
||||
安装 clang 10 以上,我安装的是 clang-11
|
||||
|
||||
```sh
|
||||
sudo apt install clang-11
|
||||
```
|
||||
|
||||
安装 libbpf
|
||||
```c
|
||||
git clone https://github.com/libbpf/libbpf
|
||||
cd libbpf/src
|
||||
make
|
||||
make install
|
||||
INSTALL bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h bpf_helpers.h bpf_helper_defs.h bpf_tracing.h bpf_endian.h bpf_core_read.h skel_internal.h libbpf_version.h usdt.bpf.h
|
||||
INSTALL ./libbpf.pc
|
||||
INSTALL ./libbpf.a ./libbpf.so ./libbpf.so.1 ./libbpf.so.1.6.0
|
||||
```
|
||||
|
||||
## 编译运行程序
|
||||
|
||||
第一步,编译内核态程序 hello_kprobe.bpf.c
|
||||
|
||||
```sh
|
||||
|
||||
clang-11 -g -O2 -target bpf -D__TARGET_ARCH_x86_64 -I/usr/include/x86_64-linux-gnu -I. -c hello_kprobe.bpf.c -o hello_kprobe.bpf.o
|
||||
```
|
||||
|
||||
第二步,基于可重定位未见生成 libbpf 脚手架代码
|
||||
|
||||
```c
|
||||
bpftool gen skeleton hello_kprobe.bpf.o > hello_kprobe.skel.h
|
||||
```
|
||||
|
||||
第三步,遍历链接用户态程序
|
||||
|
||||
```sh
|
||||
clang-11 -g -O2 -Wall -I . -c hello.c -o hello.o
|
||||
clang-11 -Wall -O2 -g hello.o -static -lbpf -lelf -lz -o hello
|
||||
```
|
||||
|
||||
第四步,执行 eBPF 程序
|
||||
|
||||
```c
|
||||
# sudo ./hello
|
||||
```
|
||||
|
||||
当程序运行起来后,就可以通过 trace_pipe 查看日志输出了
|
||||
|
||||
```sh
|
||||
#sudo cat /sys/kernel/debug/tracing/trace_pipe
|
||||
pidof-2599356 [005] .... 1056938.433919: 0: Hello, World from kprobe!
|
||||
pidof-2599356 [005] .... 1056938.434294: 0: Hello, World from kprobe!
|
||||
pidof-2599356 [005] .... 1056938.434358: 0: Hello, World from kprobe!
|
||||
sudo-2599331 [014] .... 1056938.543342: 0: Hello, World from kprobe!
|
||||
sudo-2599331 [014] .... 1056938.543368: 0: Hello, World from kprobe!
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -24,3 +24,5 @@
|
||||
- [手工模拟实现一个可以和外部通信的容器网络](tests/network/test07)
|
||||
- [socket端口复用SO_REUSEPORT测试](tests/network/test08)
|
||||
|
||||
## eBPF 程序
|
||||
- [使用 libbpf 开发的 hello world eBPF 程序](tests/ebpf/test01)
|
||||
Reference in New Issue
Block a user