内存检查工具valgrind,可以检测内存泄漏,非法地址读写等。
valgrind下载
官网:https://www.valgrind.org/
链接: https://www.valgrind.org/downloads/.
编译安装
(可以直接查看README文档来确认安装过程)
这里是编译的ARM64,所以看README.aarch64
Building
cross compiling
export CC=aarch64-linux-gnu-gcc
export LD=aarch64-linux-gnu-ld
export AR=aarch64-linux-gnu-ar
./autogen.sh
./configure --prefix=`pwd`/Inst --host=aarch64-linux-gnu \
--enable-only64bit
make -j4
make -j4 install
NOTE:Doing this assumes that the install path (`pwd`/Inst) is valid on
both host and target, which isn't normally the case. To avoid
this limitation, do instead:
./configure --prefix=/install/path/on/target \
--host=aarch64-linux-gnu \
--enable-only64bit
make -j4
make -j4 install DESTDIR=/a/temp/dir/on/host
# and then copy the contents of DESTDIR to the target.
比如我的实际编译是:
export CC=/opt/aarch64/bin/aarch64-linux-gnu-gcc
export LD=/opt/aarch64/bin/aarch64-linux-gnu-ld
export AR=/opt/aarch64/bin/aarch64-linux-gnu-ar
./autogen.sh
./configure --prefix=/tmp/valgrind_aarch64/ --host=aarch64-linux-gnu
make -j4
make -j4 install DESTDIR=/
(实际的install目录就是://tmp/valgrind_aarch64/)
修改产品编译脚本(为了打包到版本文件里)
先把编译好的valgrind:bin+libexec+share+include+lib目录拷贝到产品编译目录,再如下修改编译脚本:
build.sh或其他编译脚本名称
cp -rf $BUILD_DIR/script/valgrind/* $RBSP_ROOTFS_DIR/usr/local/ros/bin
修改产品启动脚本(升级版本后使用valgrind启动待检测程序)
cp -rf /usr/local/ros/bin/bin /usr/local/ros/bin/libexec /tmp/
/tmp/bin/valgrind --leak-check=full --show-reachable=yes -q ./rnet &
sleep 120
特定私有产品修改监控
valgrind启动协议栈进程时,进程名不再是原来的名称,而是Valgrind或者memcheck,所以需要修改监控进程。避免启动不了。
启动脚本名称比如start.sh,监控脚本比如sysmon.sh
修改启动脚本用户进程信息APP_NAME,不再监控rnet。直接删掉即可。
再编译产品升级版本即可
略
valgrind报告内存问题解读
查看官方文档:
4.2. Explanation of error messages from Memcheck
链接: https://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.errormsgs.