https://bpf.plus/android
Android 13 的内核版本为 5.10 和 5.15,包含了许多 BPF 特性,而且启用了 CONFIG_DEBUG_INFO_BTF 选项,让使用 BPF 工具变得简单。本文介绍使用 ARM 64 机器为 Android 编译基于 libbpf 的工具。
本文使用的编译环境为 ARM 64 虚拟机,发行版为 Ubuntu 20.04.2 LTS,内核版本为 5.4.0-104-generic,LLVM/clang 版本为 16。运行环境为使用 Android Studio 创建的虚拟机,版本为 Android 13。
受限于运行环境,所有工具都需要静态链接。
bpftool
bpftool 的功能无需赘述,下载代码仓库并切换到 v6.8.0 版本:
$ git clone https://github.com/libbpf/bpftool.git
$ git checkout v6.8.0
$ git submodule update --init --recursive
应用以下变更:
diff --git a/src/Makefile b/src/Makefile
index b657502..3e64f3d 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -125,7 +125,8 @@ all: $(OUTPUT)bpftool
BFD_SRCS = jit_disasm.c
-SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
+# SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
+SRCS = $(filter-out $(BFD_SRCS) pids.c,$(wildcard *.c))
ifeq ($(feature-libbfd),1)
LIBS += -lbfd -ldl -lopcodes
执行以下命令构建:
$ CFLAGS=--static make V=1
如果你使用的编译环境有 libbfd,链接阶段会失败,需要手动调整一下链接库的顺序:
-lelf -lbfd -ldl -lopcodes -lz -liberty
libbpf-bootstrap
下载仓库并更新依赖:
https://github.com/libbpf/libbpf-bootstrap.git
git submodule update --init --recursive
应用以下变更:
diff --git a/examples/c/uprobe.bpf.c b/examples/c/uprobe.bpf.c
index c5c9ba0..e508314 100644
--- a/examples/c/uprobe.bpf.c
+++ b/examples/c/uprobe.bpf.c
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/* Copyright (c) 2020 Facebook */
-
-
+
编译构建工具:
$ EXTRA_LDFLAGS=-static make -j
libbpf-tools
BCC 仓库里的 libbpf-tools 最简单,没有什么需要修改的:
clone https://github.com/iovisor/bcc.git git
git submodule update --init --recursive
EXTRA_LDFLAGS=-static make -j
以上工具编译完成后,使用 adb 推送到虚拟机中即可运行。