交叉编译trace-cmd
1. 背景 在查看瑞芯微《Thermal开发指南》时,看到了可以使用trace-cmd来进行调试,出于对于类似调试工具的好奇,尝试在本地机器上交叉编译trace-cmd并上传到Android设备上使用,中间过程比较折腾,最终出了本文。同时,本文也可以作为类似工作的一个参考。
2. 编译过程 2.1 依赖 根据官方文档,trace-cmd用到了2个库,它们需要被先编译
libtracceevent
libtracefs
2.2 下载源码并编译 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 mkdir ~/git/ git clone https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/ cd libtraceeventCC=aarch64-linux-gnu-gcc CROSS_COMPILE=aarch64-linux-gnu- make DESTDIR=../build install sed -i -e 's@prefix=/usr/local@prefix=/home/jiaxi/git/build/usr/local@g' ../build/usr/local /lib/x86_64-linux-gnu/pkgconfig/libtraceevent.pc cd ../git clone https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ cd libtracefsPKG_CONFIG_PATH=../build/usr/local /lib/x86_64-linux-gnu/pkgconfig CC=aarch64-linux-gnu-gcc CROSS_COMPILE=aarch64-linux-gnu- make DESTDIR=../build install sed -i -e 's@prefix=/usr/local@prefix=/home/jiaxi/git/build/usr/local@g' ../build/usr/local /lib/x86_64-linux-gnu/pkgconfig/libtracefs.pc cd ../git clone git://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git cd trace-cmdPKG_CONFIG_PATH=../build/usr/local /lib/x86_64-linux-gnu/pkgconfig CC=aarch64-linux-gnu-gcc CROSS_COMPILE=aarch64-linux-gnu- LDFLAGS=-static make DESTDIR=../build install cd ../build/usr/local /binadb push trace-cmd /system/bin
关键注意点:
替换.pc文件中的prefix ,不然pkg-config无法找到依赖库正确的路径
最终编译trace-cmd时,要加上LDFLAGS=-static ,使用静态链接生成trace-cmd,这样,只需要上传trace-cmd这一个文件就可以了,不然在执行时会报”No such file or directory”
2.3 关键编译参数 1 2 3 4 5 6 7 8 9 10 CC=aarch64-linux-gnu-gcc CROSS_COMPILE=aarch64-linux-gnu- DESTDIR=../build PKG_CONFIG_PATH=../build/usr/local /lib/x86_64-linux-gnu/pkgconfig LDFLAGS=-static
tips: 使用pkg-config 获取libtraceevent的头文件和库文件位置
1 2 3 4 ~ PKG_CONFIG_PATH=../build/usr/local /lib/x86_64-linux-gnu/pkgconfig pkg-config --cflags libtraceevent -I/home/jiaxi/git/build/usr/local /include/traceevent ~ PKG_CONFIG_PATH=../build/usr/local /lib/x86_64-linux-gnu/pkgconfig pkg-config --libs libtraceevent -L/home/jiaxi/git/build/usr/local /lib64 -ltraceevent
3. 运行效果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 rk3568_r:/data/local Hit Ctrl^C to stop recording ^C CPU0 data recorded at offset=0x507000 0 bytes in size CPU1 data recorded at offset=0x507000 0 bytes in size CPU2 data recorded at offset=0x507000 4096 bytes in size CPU3 data recorded at offset=0x508000 0 bytes in size rk3568_r:/data/local trace.dat adb pull /data/local /trace.dat /tmp trace-cmd report /tmp/trace.dat > /tmp/trace.txt
4. 扩展资料 trace-cmd 作为ftrace的前端,是一个易于使用,且特性众多、可用来追踪内核函数的命令。
使用 trace-cmd 追踪内核
通过 ftrace 来分析 Linux 内核