前言
這是一篇失敗結論的實踐過程,不涉及eBPF。主要是使用cuttlefish來實踐eBPF。
編譯Android內核
AOSP版本:QP1A.190711.019
手機:pixel 1
環境:Ubuntu 20
查看內核版本
sailfish:/ $ cat /proc/version
Linux version 3.18.137-g382d7256ce44 (android-build@abfarm700) (gcc version 4.9.x 20150123 (prerelease) (GCC) ) #1 SMP PREEMPT Fri Jul 12 06:00:07 UTC 2019
同步AOSP源碼
mkdir aosp10r1 && cd aosp10r1
repo init -u https://android.googlesource.com/platform/manifest -b android-10.0.0_r1
repo sync
編譯AOSP源碼
下載驅動文件
https://developers.google.com/android/drivers
Pixel
機型且build id爲QP1A.190711.019
對應的驅動是下面兩個文件. PS: 這裏驅動版本不要選錯.
下載並解壓, 得到兩個腳本文件
extract-google_devices-sailfish.sh
extract-qcom-sailfish.sh
編譯
// 初始化編譯環境
source build/envsetup.sh// 選擇與設備對應的編譯版本
lunch aosp_sailfish-userdebug// 選用8個線程並行編譯 (或者make clean; make -j8)
m
同步內核源碼
cd msm
git checkout 382d7256ce44
這個源碼中有一個文件有問題
vim drivers/thermal/thermal_core.c
將其中的<../base/base.h>改成"../base/base.h"
環境配置
因爲某些奇怪問題找不到目錄,把toolchain從aosp源碼裏拷出來了,放在了/opt下:
sudo cp -r ~/BUILD/android-10.0.0_r17_pixel/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9 /opt
sudo cp -r ~/BUILD/android-10.0.0_r17_pixel/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9 /opt
依然要配置CROSS_COMPILE_ARM32、 CROSS_COMPILE 兩個。
tips:在zsh下操作的
export PATH=$PATH:/opt/arm-linux-androideabi-4.9/bin:/opt/aarch64-linux-android-4.9/bin
export ARCH="arm64"
export CROSS_COMPILE="aarch64-linux-android-"
export CROSS_COMPILE_ARM32="arm-linux-androideabi-"
sudo apt-get install liblz4-tool
編譯
make clean
make mrproper
make marlin_defconfig
make -j8
刷機
臨時刷機
adb reboot bootloader
fastboot boot arch/arm64/boot/Image.lz4-dtb
編譯帶有eBPF的Android內核
按照Google官方文檔的信息,只能在內核版本爲4.9或更高且最初搭載了Android P版本的Android設備才能使用eBPF。https://source.android.com/devices/tech/datausage/ebpf-traffic-monitor
所以需要pixel 3系列以上的設備。
ecapture在上述系統中的實踐
ecapture需要Linux 內核4.18,所以正常的手機ROM自帶的內核無法適配,需要更換ROM的內核版本。
參考這個文章:https://blog.senyuuri.info/2021/06/30/ebpf-bcc-android-instrumentation/ 。這篇文章是使用Google 的cuttlefish來安裝eBPF並測試的,我們也按照這個方式來做。
製作cuttlefish虛擬機來運行鏡像
如果是VMware中使用Ubuntu環境的話,需要在CPU一欄中選擇打開
可以參考官方文檔(https://android.googlesource.com/device/google/cuttlefish/)來製作cuttlefish,其中一點需要注意的是debuild -i -us -uc -b -d
命令可能會失敗,因爲某個go包拒絕連接了,就像下面這樣,
這個時候使用
go env -w GOPROXY=https://goproxy.cn,direct
就可以了
啓動cuttlefish
HOME=$PWD ./bin/launch_cvd --start_webrtc=true --kernel_path=/home/tg/Desktop/aosp_cf_x86_64_img/bzImage --initramfs_path= /home/tg/Desktop/aosp_cf_x86_64_img/initramfs.img
使用Cuttlefish運行aosp最新版本
同步最新AOSP源碼以及編譯
mkdir aosp
cd aosp
proxychains repo init -u https://android.googlesource.com/platform/manifest
proxychains repo sync
source build/envsetup.sh
lunch # 選擇aosp_cf_x86_64_phone-userdebug
m -j16
cuttlefish運行AOSP
參考:https://android.googlesource.com/device/google/cuttlefish/
不再贅述。
在最新版AOSP源碼中使用ecapture
從ecapture的release頁面下載最新的,可執行程序:Releases · ehids/ecapture
adb push ecapture /data/local/tmp
在Android系統內運行ecapture:
我用小紅書app爲例:
小紅書 7.35.2.1
都不用打開app的,試一下./ecapture tls
就不行了,Linux跟Android 內部的區別還是有的。
還需要改他的代碼,可能一部分邏輯不一樣吧,主要改的核心還是tls命令這塊功能,他的MySQL啥的我就不涉及變動了,Android內部也不需要運行MySQL這種的大型的數據庫。
項目:https://github.com/TUGOhost/ecapture
問了作者,可以指定libssl.so路徑:
./ecapture tls --libssl="/system/lib64/libssl.so"
但是cuttlefish沒有網絡,這個還需要解決一下。
參考
- ecapture:https://github.com/ehids/ecapture
- Extending the Kernel with eBPF:https://source.android.com/devices/architecture/kernel/bpf
- https://source.android.com/setup/build/building-kernels?hl=zh-cn
- https://www.daimajiaoliu.com/daima/476261aff1003f0
- https://www.yuque.com/docs/share/f097540e-34e5-4473-beee-29e811a151a6?#
- https://blog.senyuuri.info/2021/06/30/ebpf-bcc-android-instrumentation/
- https://www.youtube.com/watch?v=eaPUdDkE91g
- https://android.googlesource.com/platform/tools/acloud/+/refs/heads/master/README.md
- https://www.cnblogs.com/zknublx/p/9156560.html
- https://android.googlesource.com/device/google/cuttlefish/