valgrind在光猫上的使用

Valgrind调试器可以帮助查找软件中可能内存泄漏,内存覆盖,数组越界

1.编译

进入sdk根目录,执行命令

make valgrind 

2. 编译生成的可执行文件

valgrind编译生成的可执行文件有多个在‘apps/public/valgrind-3.15.0/.in_place/’,每一个分别对应varlgind的检测功能。

apps/public/valgrind-3.15.0/.in_place/memcheck-arm64-linux
apps/public/valgrind-3.15.0/.in_place/drd-arm64-linux
apps/public/valgrind-3.15.0/.in_place/exp-arm64-linux
apps/public/valgrind-3.15.0/.in_place/callgrind-arm64-linux
apps/public/valgrind-3.15.0/.in_place/lacky-arm64-linux
apps/public/valgrind-3.15.0/.in_place/default.supp

valgrind编译生成执行文件是'apps/public/valgrind-3.15.0/coregrind/valgrind',可以认为valgrind是一个入口,通过这个入口调用不同的‘valgrind-3.15.0/.in_place/*-arm64-linux’内存检测工具tool

3. 把编译生成的valgrind可执行文件和tool从编译服务器导出

cp apps/public/valgrind-3.15.0/.in_place/default.supp .
cp apps/public/valgrind-3.15.0/.in_place/memcheck-arm64-linux .
cp apps/public/valgrind-3.15.0/.in_place/vgpreload_memcheck-arm64-linux.so .
cp apps/public/valgrind-3.15.0/.in_place/vgpreload_core-arm64-linux.so .
cp apps/public/valgrind-3.15.0/coregrind/valgrind .

4. 使用tftp把导出的valgrind和tool导入到开发板'/tmp'目录


cd /tmp
tftp 192.168.1.100 -g -r default.supp
tftp 192.168.1.100 -g -r vgpreload_core-arm64-linux.so
tftp 192.168.1.100 -g -r vgpreload_memcheck-arm64-linux.so
tftp 192.168.1.100 -g -r memcheck-arm64-linux
tftp 192.168.1.100 -g -r valgrind
mkdir -p lib/valgrind
chmod +x valgrind
chmod +x memcheck-arm64-linux
mv memcheck-arm64-linux lib/valgrind
mv default.supp lib/valgrind
mv vgpreload_memcheck-arm64-linux.so lib/valgrind
mv vgpreload_core-arm64-linux.so lib/valgrind

5. 执行 valgrind 命令

使用 --memcheck --leak-check=full 命令选项,指定使用 memcheck tool 

/tmp/valgrind --tool=memcheck --leak-check=full  you_app_to_run

6. 提示运行失败,解决方法

valgrind 运行失败,提示开发板上的 ld-2.29.so 是strpped的。valgrind要求一个没有stripped到 ld-2.29.so 库。一般在检查编译链的目录里会有没有stripped过的 ld2.29.so 文件。

解决方法,在编译目录的 toolchain 里一般放的有没有stripped过的 libc.-2.29.so 文件。

root@xx:/tmp # /tmp/valgrind --tool=memcheck --leak-check=full /usr/bin/gccli 
==1845== Memcheck, a memory error detector
==1845== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1845== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==1845== Command: /usr/bin/gccli
==1845==

valgrind: Fatal error at startup: a function redirection
valgrind: which is mandatory for this platform-tool combination
valgrind: cannot be set up. Details of the redirection are:
valgrind: 
valgrind: A must-be-redirected function
valgrind: whose name matches the pattern: strlen
valgrind: in an object with soname matching: ld-linux-aarch64.so.1
valgrind: was not found whilst processing
valgrind: symbols from the object with soname: ld-linux-aarch64.so.1
valgrind: 
valgrind: Possible fixes: (1, short term): install glibc's debuginfo
valgrind: package on this machine. (2, longer term): ask the packagers
valgrind: for your Linux distribution to please in future ship a non-
valgrind: stripped ld.so (or whatever the dynamic linker .so is called)
valgrind: that exports the above-named function using the standard
valgrind: calling conventions for this platform. The package you need
valgrind: to install for fix (1) is called
valgrind: 
valgrind: On Debian, Ubuntu: libc6-dbg
valgrind: On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo
valgrind: 
valgrind: Note that if you are debugging a 32 bit process on a
valgrind: 64 bit system, you will need a corresponding 32 bit debuginfo
valgrind: package (e.g. libc6-dbg:i386).
valgrind: 
valgrind: Cannot continue -- exiting now. Sorry.

root@xx:/tmp #

以xx ARM64 为例

grep CROSS .config 

find hosttools/toolchain/xxxx/ | grep lib/libc-.*.so
find hosttools/toolchain/xxxx/ | grep lib/ld-.*.so

cp xxxx/libc-2.29.so .
cp xxxx/ld-2.29.so .

把 libc-2.29.so 从编译服务器传出,把 ld-2.29.so 从编译服务器传出

使用tftp把导出的valgrind和tool导入到开发板'/tmp'目录

tftp 192.168.1.100 -g -r ld-2.29.so
chmod +x ld-2.29.so
ln -s ld-2.29.so ld-linux-aarch64.so.1
tftp 192.168.1.100 -g -r libc-2.29.so
chmod +x libc-2.29.so
ln -s libc-2.29.so libc.so.6
LD_LIBRARY_PATH=/tmp
export LD_LIBRARY_PATH

7. 动态链接器 ld-2.29.so 是可执行文件

ld-2.29.so 是动态链接可执行文件的连接加载器,虽然 ld-2.29.so 是一个共享库文件,但ld-2.29.so 是可以单独执行的。

/lib/ld-2.29.so  ,命令显示帮助

root@xx:/tmp # /lib/ld-2.29.so 
Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
You have invoked `ld.so', the helper program for shared library executables.
This program usually lives in the file `/lib/ld.so', and special directives
in executable files using ELF shared libraries tell the system's program
loader to load the helper program from this file. This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it. You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run. This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

--list list all dependencies and how they are resolved
--verify verify that given object really is a dynamically linked
object we can handle
--inhibit-cache Do not use /etc/ld.so.cache
--library-path PATH use given PATH instead of content of the environment
variable LD_LIBRARY_PATH
--inhibit-rpath LIST ignore RUNPATH and RPATH information in object names
in LIST
--audit LIST use objects named in LIST as auditors
root@xx:/tmp #

使用 /lib/ld-2.29.so 运行某个可执行文件,这样做的效果是指定了可执行文件的动态链接加载器。

root@xx:/tmp # /lib/ld-2.29.so /usr/bin/cli 
Valid commands for /usr/bin/gccli are:
epon gpon sys switch bob wlan port 
mirror image osgi voice consoleRedir dnsmasq help
root@xx:/tmp #

使用/tmp/目录下的动态链接加载器 /tmp/ld-2.29.so 

root@xx:/tmp # /lib/ld-2.29.so --list /usr/bin/cli 
linux-vdso.so.1 (0x0000007fbbaea000)
libgc.so => /lib64/libgc.so (0x0000007fbba7a000)
libm.so.6 => /lib64/libm.so.6 (0x0000007fbb9d0000)
libjson-c.so.4 => /lib64/libjson-c.so.4 (0x0000007fbb9b1000)
libosgimgt.so => /lib64/libosgimgt.so (0x0000007fbb99c000)
libgcvoip_api.so => /lib64/libgcvoip_api.so (0x0000007fbb989000)
libc.so.6 => /tmp/libc.so.6 (0x0000007fbb81d000)
/lib/ld-linux-aarch64.so.1 => /lib/ld-2.29.so (0x0000007fbbabc000)
root@xx:/tmp #

8. 执行valgrind,指定 /tmp/ld-2.29.so 作为动态链接器

/tmp/valgrind --tool=memcheck --leakcheck=full /tmp/ld-2.29.so  xxxx_you_app_to_run_xxx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值