1. 程式人生 > >(Ubuntu64)valgrind安裝+編譯64位 32位版本

(Ubuntu64)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