2.1 Android ebpf编程规则

写在前面

    有很多开源的库和框架可用于编写eBPF程序。而Android当然也支持BCC,不过这部分放到最后再讲。因为Android也实现了自己的eBPF加载器。从本章开始我们通过实例hello world一步步剖析在Android傻姑娘ebpf的编程,编译,加载以及在内核中实际的运行原理。加载器这一部分,我们也会独立成章,详细进行拆解。

    在Android中,ebpf程序给予android构建系统(Android.bp),然后通过使用C程序编写构建而成一个二进制文件,该二进制文件保存在/system/etc/bpf目录下,并打包成为system镜像中一部分。

一,Android eBPF C程序语法格式及示例

    在Android中,eBPF c程序必须使用以下格式:

#include <bpf_helpers.h>

/* Define one or more maps in the maps section, for example
 * define a map of type array int -> uint32_t, with 10 entries
 */
DEFINE_BPF_MAP(name_of_my_map, ARRAY, int, uint32_t, 10);

/* this will also define type-safe accessors:
 *   value * bpf_name_of_my_map_lookup_elem(&key);
 *   int bpf_name_of_my_map_update_elem(&key, &value, flags);
 *   int bpf_name_of_my_map_delete_elem(&key);
 * as such it is heavily suggested to use l

猜你喜欢

转载自blog.csdn.net/huangyabin001/article/details/130997737
2.1