(Ubuntu64)valgrind安装+编译64位 32位版本

本文详细介绍了如何在Ubuntu64位系统上安装Valgrind 3.12.0,包括下载、编译、安装到环境变量配置的步骤。此外,还讲解了在运行32位程序时遇到的问题,如需安装i386库以避免错误,并展示了如何解决overwrite失败的问题。最后,通过示例展示了如何使用Valgrind进行内存检测,包括64位和32位程序的运行结果。

安装编译步骤:

wget http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2

 

tar xvf valgrind-3.12.0.tar.bz2

 

cd valgrind-3.12.0

 

sudo apt-get install automake

 

./autogen.sh

 

./configure --prefix=/<pathto>/valgrind-3.12.0

 

make

 

make install

 

添加至环境变量:

sudo gedit ~/.bashrc

添加:

export PATH=$PATH:/<pathto>/valgrind-3.12.0/bin

export VALGRIND_LIB="/<pathto>/valgrind-3.12.0/lib/valgrind"

source .bashrc

检测是否成功编译:

valgrind --version

测试用例:

#include <stdlib.h>

int main()

{

int * pArray =malloc(sizeof(int) * 5);

pArray[5] = 1;

int * pInt ;

int x = *pInt;

* pInt = 2;

return 0;

}

编译:gcc -g -o test main.c

检测:valgrind --leak-check=yes ./test

检测32位程序的时候需要安装对应的i386库否则会提示错误:

==3062== Memcheck, a memory error detector
==3062== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3062== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==3062== Command: ./test
==3062== 

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.so.2
valgrind:  was not found whilst processing
valgrind:  symbols from the object with soname: ld-linux.so.2
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.

安装对应的i386库:sudo apt-get install  libc6-dbg:i386=2.23-0ubuntu10

安装失败,提示:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  libc6-dbg:i386
0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
Need to get 3,125 kB of archives.
After this operation, 18.4 MB of additional disk space will be used.
Get:1 http://ubuntu.cn99.com/ubuntu xenial-updates/main i386 libc6-dbg i386 2.23-0ubuntu10 [3,125 kB]
Fetched 3,125 kB in 0s (8,161 kB/s)  
(Reading database ... 222636 files and directories currently installed.)
Preparing to unpack .../libc6-dbg_2.23-0ubuntu10_i386.deb ...
Unpacking libc6-dbg:i386 (2.23-0ubuntu10) ...
dpkg: error processing archive /var/cache/apt/archives/libc6-dbg_2.23-0ubuntu10_i386.deb (--unpack):
 trying to overwrite shared '/usr/share/doc/libc6-dbg/copyright', which is different from other instances of package libc6-dbg:i386
E: Sub-process /usr/bin/dpkg returned an error code (1)

该提示指明overwrite失败,将该文件删除即可安装成功

@ubuntu:~$ gcc -o test main.c -m32

@ubuntu:~$ file test
test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=909e63f33bc79673b7132e3c267d6b10b4d83653, not stripped
@ubuntu:~$ valgrind ./test 
==17467== Memcheck, a memory error detector
==17467== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==17467== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==17467== Command: ./test
==17467== 
==17467== 
==17467== Process terminating with default action of signal 11 (SIGSEGV)
==17467==  Bad permissions for mapped region at address 0x420F000
==17467==    at 0x80486F7: main (in /home/libao/test)
==17467== 
==17467== HEAP SUMMARY:
==17467==     in use at exit: 9,216 bytes in 1 blocks
==17467==   total heap usage: 1 allocs, 0 frees, 9,216 bytes allocated
==17467== 
==17467== LEAK SUMMARY:
==17467==    definitely lost: 0 bytes in 0 blocks
==17467==    indirectly lost: 0 bytes in 0 blocks
==17467==      possibly lost: 0 bytes in 0 blocks
==17467==    still reachable: 9,216 bytes in 1 blocks
==17467==         suppressed: 0 bytes in 0 blocks
==17467== Rerun with --leak-check=full to see details of leaked memory
==17467== 
==17467== For counts of detected and suppressed errors, rerun with: -v
==17467== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Segmentation fault

@ubuntu:~$ gcc -o test main.c
@ubuntu:~$ file test
test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=b5fe262e820d68bba90092588323f21dcd2d819a, not stripped
@ubuntu:~$ valgrind ./test
==17478== Memcheck, a memory error detector
==17478== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==17478== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==17478== Command: ./test
==17478== 
==17478== 
==17478== Process terminating with default action of signal 11 (SIGSEGV)
==17478==  Bad permissions for mapped region at address 0x5205000
==17478==    at 0x4008D7: main (in /home/libao/test)
==17478== 
==17478== HEAP SUMMARY:
==17478==     in use at exit: 9,216 bytes in 1 blocks
==17478==   total heap usage: 1 allocs, 0 frees, 9,216 bytes allocated
==17478== 
==17478== LEAK SUMMARY:
==17478==    definitely lost: 0 bytes in 0 blocks
==17478==    indirectly lost: 0 bytes in 0 blocks
==17478==      possibly lost: 0 bytes in 0 blocks
==17478==    still reachable: 9,216 bytes in 1 blocks
==17478==         suppressed: 0 bytes in 0 blocks
==17478== Rerun with --leak-check=full to see details of leaked memory
==17478== 
==17478== For counts of detected and suppressed errors, rerun with: -v
==17478== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Segmentation fault


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值