AM335X 交叉编译移植valgrind工具

Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。

1、下载

> 官网 http://valgrind.org/downloads/  下载源码包
> 
> tar xvf valgrind-3.15.0.tar.bz2
> 
> cd valgrind-3.15.0
> 
> apt-get install automake
> 
> ./autogen.sh

2、解压后进行配置

sudo ./configure --host=arm-linux-gnueabihf --prefix=/usr/local/valgrind CC=arm-linux-gnueabihf-gcc CPP=arm-linux-gnueabihf-cpp CXX=arm-linux-gnueabihf-g++

–prefix=/usr/local/valgrind指定的目录要与开发板上放置的目录一致,不然运行valgrind时可能会出现“valgrind: failed to start tool ‘memcheck’ for platform ‘arm-linux’: No such file or directory”错误。

配置报错:

checking for a supported CPU… no (arm)
configure: error: Unsupported host architecture. Sorry

解决方法,修改configure:
armv7*) 改成 armv7*|arm)

3、编译

make

make install

/usr/local/valgrind目录下生成好的valgrind,包括4个文件夹:bin,include,lib,share

4、删除不需要的工具,只留下内存检查工具

需要删除 lib/valgrind 目录下的文件 以及 整个 share 目录,最后精简到 12M 左右

精简后的lib/valgrind 目录下所有文件:

32bit-core-valgrind-s1.xml   32bit-sse.xml                arm-with-vfpv3.xml
32bit-core-valgrind-s2.xml   arm-core-valgrind-s1.xml     default.supp
32bit-core.xml               arm-core-valgrind-s2.xml     getoff-arm-linux
32bit-linux-valgrind-s1.xml  arm-core.xml                 memcheck-arm-linux
32bit-linux-valgrind-s2.xml  arm-vfpv3-valgrind-s1.xml    vgpreload_core-arm-linux.so
32bit-linux.xml              arm-vfpv3-valgrind-s2.xml    vgpreload_memcheck-arm-linux.so
32bit-sse-valgrind-s1.xml    arm-vfpv3.xml
32bit-sse-valgrind-s2.xml    arm-with-vfpv3-valgrind.xml

5、运行

把安装目录下的文件valgrind下载到目标板的/usr/local目录下,修改bin目录下的执行权限

chrom -R +x /usr/local/valgrind/bin/

带全路径执行 /usr/local/valgrind/bin/valgrind --help 出现下面的信息说明移植成功

usage: valgrind [options] prog-and-args

扫描二维码关注公众号,回复: 12523153 查看本文章

tool-selection option, with default in [ ]:
–tool= use the Valgrind tool named [memcheck]

basic user options for all Valgrind tools, with defaults in [ ]:
下面一堆功能参数…

4、验证功能是否可用

执行/usr/local/valgrind/bin/valgrind ls -l报错:

#./valgrind ls -l
 ==1298== Memcheck, a memory error detector
 ==1298== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
 ==1298== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
 ==1298== Command: ls -l
 ==1298== 

 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:      memcpy
 valgrind:  in an object with soname matching:   ld-linux.so.3
 valgrind:  was not found whilst processing
 valgrind:  symbols from the object with soname: ld-linux.so.3
 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:  Cannot continue -- exiting now.  Sorry.

上面出现了问题,说明大概是说我的glibc是个strip后的版本,valgrind需要debug版,debug版的名字是libc6-dbg。

参考网上其他博主的说明,解决方式是去glibc官网下载源码并重新交叉编译glibc(版本需要跟原来的保持一致,且在编译时需要加入-g选项),将编译完生成的 ld-linux.so.3 替换掉目标板内的文件

所以首先需要查看自己用的glibc的版本,运行/lib/libc.so.6,出现下面的信息:

root@am335x-evm:~# /lib/libc.so.6 
GNU C Library (GNU libc) stable release version 2.25, by Roland McGrath et al.
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 7.2.1 20171011.
Available extensions:
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
libc ABIs: UNIQUE
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

交叉编译glibc-2.25

1、下载

我用的是glibc-2.25.tar.gz,官网地址:http://www.gnu.org/software/libc/

2、解压和准备

我所有操作都放在/tmp目录下。
移动到/tmp目录下
解压
创建glibc_config目录作为配置目录
创建glibc_build目录作为编译后目标文件放置目录

/tmp$ cp /mnt/share/glibc-2.25.tar.gz
/tmp$ tar zxvf glibc-2.20.tar.gz
/tmp$ mkdir glibc_config
/tmp$ mkdir glibc_build

3、配置构建

构建配置需要在单独的目录中构建,而不能直接在源码目录下构建,这就是创建glibc_config目录的原因。
•进入glibc_config/
•执行配置文件

/tmp$ cd glibc_config/ /tmp/glibc_config$ …/glibc-2.25/configure
CC="/opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc" CFLAGS=" -g -O2 -march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a7" --prefix="/tmp/glibc_build" --host=“arm-linux-gnueabihf”

4、编译及安装

make
make install

5、遇到问题

在编译时遇到以下问题
1、’#error “glibc cannot be compiled without optimization”’
编译时要加上优化开关,export CFLAG="-g -O2"

6、测试运行

编译安装完成后,在glibc_build目录下会生成8个文件夹。

只需要将/lib目录下的ld-2.25.so替换到开发板上,并修改权限。

然后自己编写一个简单的测试demo,/usr/local/valgrind/bin/valgrind ./test执行输出如下说行移植成功:

root@am335x-evm:~# /usr/local/valgrind/bin/valgrind ./test 
==1176== Memcheck, a memory error detector
==1176== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1176== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==1176== Command: ./test
==1176== 
==1176== Invalid write of size 4
==1176==    at 0x1043A: main (test.c:14)
==1176==  Address 0x4944038 is 12 bytes after a block of size 4 alloc'd
==1176==    at 0x483D48C: malloc (vg_replace_malloc.c:299)
==1176==    by 0x10413: main (test.c:7)
==1176== 
==1176== Invalid read of size 4
==1176==    at 0x1043E: main (test.c:15)
==1176==  Address 0x4944038 is 12 bytes after a block of size 4 alloc'd
==1176==    at 0x483D48C: malloc (vg_replace_malloc.c:299)
==1176==    by 0x10413: main (test.c:7)
==1176== 
the value of p is==1176== 
==1176== HEAP SUMMARY:
==1176==     in use at exit: 4 bytes in 1 blocks
==1176==   total heap usage: 2 allocs, 1 frees, 4,100 bytes allocated
==1176== 
==1176== LEAK SUMMARY:
==1176==    definitely lost: 4 bytes in 1 blocks
==1176==    indirectly lost: 0 bytes in 0 blocks
==1176==      possibly lost: 0 bytes in 0 blocks
==1176==    still reachable: 0 bytes in 0 blocks
==1176==         suppressed: 0 bytes in 0 blocks
==1176== Rerun with --leak-check=full to see details of leaked memory
==1176== 
==1176== For counts of detected and suppressed errors, rerun with: -v
==1176== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

猜你喜欢

转载自blog.csdn.net/qq_34743935/article/details/105682653