valgrind使用方法

一.Valgrind是什么?Valgrind是一个提供程序调试及性能分析的工具集。

其包含的工具主要有Memcheck,Cachegrind,Callgrind,Massif等。

其中,最为常用的是Memcheck,其主要用来检查程序heap上的内存使用情况。

本文档主要介绍Memcheck的用法和一些使用技巧。

其官方网站是:/二.Valgrind能干什么不能干什么?Valgrind主要用来检查程序中可能出现的以下问题:e of uninitialised memory2.Reading/writing memory after it has been free’d3.Reading/writing off the end of malloc’d block4.Memory leaks -- where pointers to malloc’d blocks are lost foreve5.Mismatched use of malloc/new/new [] vs free/delete/delete []6.Overlapping src and dst pointers in memcpy() and related functions其功能约束如下:1.只能检查heap上的错误,不能检查出static和stack内存的使用,如数组越界等。

2.不能指出为什么泄漏,也不能指出在哪内存泄漏3.指出的错误并非100%正确,但建议在编译时至少以warning的心态对待它们。

三.Valgrind的安装与部署若有root权限,其安装方式如下:1.从官网上下载valgrind 安装包:/downloads/valgrind-3.3.0.tar.bz22.用bzip2及tar命令解压压缩包。

3.进入解压目录,运行./configure4.运行“make”命令5.运行“make install”命令6.运行“valgrind ls- l”测试valgrind是否已经正确安装到计算机上。

若正确安装,则会出现类似第四部分的报错信息。

若没有root权限,则在第3步时,可以用--prefix指定安装的目录./configure –prefix=/home/work/yangfenqiang/以下步骤相同。

四.Valgrind使用示例及报错信息说明编写程序test.cpp如下:1 #include <iostream>2 using namespace std;34 int main()5 {6 int *a = new int[10];7 a[11] = 0;8 cout << a[11]<< endl;9 return 0;10 }11编译该程序:gcc –g –o test test.cpp注意加入-g参数,便于valgrind读入符号表之类的信息以提供更丰富的错误定位信息。

不推荐加入-O等优化参数,因为优化后的代码易于让valgrind解释错误。

运行“valgrind --tool=memcheck --leak-check=yes --show-reachable=yes test”,显示如下信息:==2051== Memcheck, a memory error detector.==2051== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.==2051== Using LibVEX rev 1804, a library for dynamic binary translation.==2051== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.==2051== Using valgrind-3.3.0, a dynamic binary instrumentation framework.==2051== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.==2051== For more details, rerun with: -v==2051====2051== Invalid write of size 4==2051== at 0x4009C6: main (test.cpp:7)==2051== Address 0x4a2005c is 4 bytes after a block of size 40 alloc'd==2051== at 0x490581B: operator new[](unsigned long) (vg_replace_malloc.c:274)==2051== by 0x4009B9: main (test.cpp:6)==2051====2051== Invalid read of size 4==2051== at 0x4009D4: main (test.cpp:8)==2051== Address 0x4a2005c is 4 bytes after a block of size 40 alloc'd==2051== at 0x490581B: operator new[](unsigned long) (vg_replace_malloc.c:274)==2051== by 0x4009B9: main (test.cpp:6)==2051====2051== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 9 from 4) ==2051== malloc/free: in use at exit: 40 bytes in 1 blocks.==2051== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.==2051== For counts of detected errors, rerun with: -v==2051== searching for pointers to 1 not-freed blocks.==2051== checked 198,560 bytes.==2051====2051====2051== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1==2051== at 0x490581B: operator new[](unsigned long) (vg_replace_malloc.c:274)==2051== by 0x4009B9: main (test.cpp:6)==2051====2051== LEAK SUMMARY:==2051== definitely lost: 40 bytes in 1 blocks.==2051== possibly lost: 0 bytes in 0 blocks.==2051== still reachable: 0 bytes in 0 blocks.==2051== suppressed: 0 bytes in 0 blocks.其中:1.==2014== 表示进程号信息,基本没用。

2.接下来是Memcheck的版权声明信息。

3.详细的报错信息,如at 0x4009C6: main (test.cpp:7) Address 0x4a2005c is 4bytes after a block of size 40 alloc'd说明test.cpp的第7行发生内存访问越界,越界的位移为4。

4.ERROR SUMMARY下面为错误汇总信息。

5.接着是内存泄漏信息。

说明有40byte的内存泄漏。

6.LEAK SUMMARY为内存泄漏信息。

在LEAK SUMMARY中:●definitely lost:表明没有任何指针指向该区域,已经造成了内存泄漏。

●possibly lost:存在指针指向内存中的某个位置,valgrind认为你有可能是在做一些其他的高级应用(将指针放在申请的内存块中间)●still reachable:仍有指针引用该内存块,只是没有释放而已,可以通过设置—show-reachable=yes来报错。

五.Valgrind常用命令参数1.--tool=<name> [default=memcheck]--tool参数指明所要使用valgrind的哪一个工具,默认的为memcheck。

因为大多数情况下我们只会用到memcheck工具,因此该参数可以不写。

2.--leak-check=<no|summary|yes|full>[default:summary]在退出时检查是否有泄漏。

Summary只是告诉我们有多少次泄漏,yes或full 会告诉我们每次泄漏的详细信息。

3.--show-reachable=<yes|no>[default:no]通过设定该参数为yes,则显示still reachable类型的内存泄漏信息。

其他更多的运行参数信息可以查看《valgrind使用指南》及《valgrind manual》。

合集下载

valgrind的使用及错误信息分析

valgrind的使用及错误信息分析

valgrind的使⽤及错误信息分析这⾥记录⼀下使⽤valgrind查找你的应⽤程序中的各种潜在的错误信息,并举例说明。

经常使⽤valgrind查找⼀下你的代码的内存有关错误,对移植到嵌⼊系统后的系统稳定性来说有着重要的意义。

usagex86 平台先编译你⾃⼰的应⽤程序命令⾏:valgrind --log-file=1 --tool=memcheck ./a.outerror specification⼀、有malloc,但未freecode#include <stdio.h>#include <stdlib.h>void main(){char *p = malloc(20);sprintf(p, "%s", "test");fprintf(stderr, "p:%s/n", p);}分析:⽂件后部,总体来看,有确定⽆疑的lost 20字节。

如下:==26512== LEAK SUMMARY:==26512== definitely lost: 20 bytes in 1 blocks.在⽂件之前描述的内容,细节可以看出,有1个malloc,但未去free。

如下:==26512== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)==26512== malloc/free: in use at exit: 20 bytes in 1 blocks.==26512== malloc/free: 1 allocs, 0 frees, 20 bytes allocated.⼆、free⼀个未malloc的指针codevoid main(){char p[] = "hello";fprintf(stderr, "p:%s/n", p);free(p);}分析:⽂件后部,总体来看,有1个错误,0个malloc,1个free。

valgrind中文手册

valgrind中文手册

/* valgrind-3.5.0 编译和安装技巧* author: lblong* date : 20100530**/安装步骤:1、从valgrind官网上获得代码(也可以通过下载tar包获得源代码,可以点击这里下载)/downloads/current.html#current2、进入源代码目录3、运行./autogen.sh设置环境(需要标准的autoconf工具)4、运行./configure配置V algrind,具体参数信息详见INSTALL文件。

一般只需要设置--prefix=/where/you/want/it/installed5、make,编译V algrind6、make install,安装V algrind详细:1. linux 环境下执行./configuretelstar:/sybase/telstar/user/lblong/memory/valgrind-3.5.0 > ./configurechecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking whether to enable maintainer-specific portions of Makefiles... nochecking whether ln -s works... yeschecking for gcc... ccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables...checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether cc accepts -g... yeschecking for cc option to accept ANSI C... none neededchecking for style of include used by make... GNUchecking dependency style of cc... gcc3checking whether cc understands -c and -o together... yeschecking how to run the C preprocessor... cc -Echecking for g++... g++checking whether we are using the GNU C++ compiler... yeschecking whether g++ accepts -g... yeschecking dependency style of g++... gcc3checking for ranlib... ranlibchecking for ar... /usr/bin/archecking for perl... /usr/bin/perlchecking for gdb... /usr/bin/gdbchecking for diff -u... yeschecking for a supported version of gcc... ok (4.1.2)checking build system type... i686-intel-linuxchecking host system type... i686-intel-linuxchecking for a supported CPU... ok (i686)checking for a 64-bit only build... nochecking for a 32-bit only build... nochecking for a supported OS... ok (linux)checking for the kernel version... 2.6 family (2.6.18-128.el5xen)checking for a supported CPU/OS combination... ok (x86-linux)checking for use as an inner Valgrind... nochecking for egrep... grep -Echecking the GLIBC_VERSION version... 2.5 familychecking for CLOCK_MONOTONIC... yeschecking for PTHREAD_MUTEX_ADAPTIVE_NP... yeschecking for PTHREAD_MUTEX_ERRORCHECK_NP... yeschecking for PTHREAD_MUTEX_RECURSIVE_NP... yeschecking for PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP... yes checking for pthread_mutex_t::__m_kind... nochecking for pthread_mutex_t::__data.__kind... yeschecking for Altivec... nochecking for pthread_create@GLIBC2.0()... yeschecking for eventfd()... nochecking if gcc accepts -m32... yeschecking if gcc accepts -maix32... nochecking if gcc accepts -m64... nochecking if gcc accepts -maix64... nochecking if gcc accepts -mmmx... yeschecking if gcc accepts -msse... yeschecking if gcc accepts -mpreferred-stack-boundary... yeschecking if gcc accepts -Wno-pointer-sign... yeschecking if gcc accepts -Wdeclaration-after-statement... yeschecking if gcc accepts -Wno-empty-body... nochecking if gcc accepts -Wno-format-zero-length... yeschecking if gcc accepts -Wno-uninitialized... yeschecking if gcc accepts -Wextra or -W... -W extrachecking if gcc accepts -fno-stack-protector... yes checking if gcc accepts --param inline-unit-growth... yes checking if gcc supports __builtin_expect... yes checking if ppc32/64 as supports mtocrf/mfocrf... no checking if x86/amd64 assembler speaks SSE3... yes checking if x86/amd64 assembler speaks SSSE3... no checking for TLS support... yeschecking for /proc/self/fd... yeschecking for /proc/self/exe... yeschecking for /proc/self/maps... yeschecking for ANSI C header files... yeschecking for sys/types.h... yeschecking for sys/stat.h... yeschecking for stdlib.h... yeschecking for string.h... yeschecking for memory.h... yeschecking for strings.h... yeschecking for inttypes.h... yeschecking for stdint.h... yeschecking for unistd.h... yeschecking asm/unistd.h usability... yeschecking asm/unistd.h presence... yeschecking for asm/unistd.h... yeschecking endian.h usability... yeschecking endian.h presence... yeschecking for endian.h... yeschecking mqueue.h usability... yeschecking mqueue.h presence... yeschecking for mqueue.h... yeschecking sys/endian.h usability... nochecking sys/endian.h presence... nochecking for sys/endian.h... nochecking sys/epoll.h usability... yeschecking sys/epoll.h presence... yeschecking for sys/epoll.h... yeschecking sys/eventfd.h usability... nochecking sys/eventfd.h presence... nochecking for sys/eventfd.h... nochecking sys/klog.h usability... yeschecking sys/klog.h presence... yeschecking for sys/klog.h... yeschecking sys/poll.h usability... yeschecking sys/poll.h presence... yeschecking for sys/poll.h... yeschecking sys/signal.h usability... yeschecking sys/signal.h presence... yeschecking for sys/signal.h... yeschecking sys/signalfd.h usability... nochecking sys/signalfd.h presence... nochecking for sys/signalfd.h... nochecking sys/syscall.h usability... yeschecking sys/syscall.h presence... yeschecking for sys/syscall.h... yeschecking sys/time.h usability... yeschecking sys/time.h presence... yeschecking for sys/time.h... yeschecking for sys/types.h... (cached) yeschecking for uid_t in sys/types.h... yeschecking for off_t... yeschecking for size_t... yeschecking whether time.h and sys/time.h may both be included... yes checking for working memcmp... yeschecking for stdlib.h... (cached) yeschecking for unistd.h... (cached) yeschecking for getpagesize... yeschecking for working mmap... yeschecking return type of signal handlers... voidchecking for clock_gettime in -lrt... yeschecking for clock_gettime... yeschecking for epoll_create... yeschecking for epoll_pwait... nochecking for floor... nochecking for klogctl... yeschecking for mallinfo... yeschecking for memchr... yeschecking for memset... yeschecking for mkdir... yeschecking for mremap... yeschecking for ppoll... yeschecking for pthread_barrier_init... yeschecking for pthread_condattr_setclock... yeschecking for pthread_mutex_timedlock... yeschecking for pthread_rwlock_timedrdlock... yeschecking for pthread_rwlock_timedwrlock... yeschecking for pthread_spin_lock... yeschecking for semtimedop... yeschecking for signalfd... nochecking for sigwaitinfo... yeschecking for syscall... yeschecking for strchr... yeschecking for strdup... yeschecking for strpbrk... yeschecking for strrchr... yeschecking for strstr... yeschecking for timerfd... nochecking for utimensat... nochecking primary target for usable MPI2-compliant C compiler and mpi.h... nochecking secondary target for usable MPI2-compliant C compiler and mpi.h... noconfigure: W ARNING: pkg-config has not been installed or is too old.configure: W ARNING: Detection of Qt4 will be skipped.checking for boost... nochecking for OpenMP... yeschecking if gcc supports __sync_bool_compare_and_swap... noconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating VEX/Makefileconfig.status: creating valgrind.specconfig.status: creating valgrind.pcconfig.status: creating glibc-2.X.suppconfig.status: creating docs/Makefileconfig.status: creating tests/Makefileconfig.status: creating tests/vg_regtestconfig.status: creating perf/Makefileconfig.status: creating perf/vg_perfconfig.status: creating include/Makefileconfig.status: creating auxprogs/Makefileconfig.status: creating mpi/Makefileconfig.status: creating coregrind/Makefileconfig.status: creating memcheck/Makefileconfig.status: creating memcheck/tests/Makefileconfig.status: creating memcheck/tests/amd64/Makefileconfig.status: creating memcheck/tests/x86/Makefileconfig.status: creating memcheck/tests/linux/Makefileconfig.status: creating memcheck/tests/darwin/Makefileconfig.status: creating memcheck/tests/x86-linux/Makefileconfig.status: creating memcheck/perf/Makefileconfig.status: creating cachegrind/Makefileconfig.status: creating cachegrind/tests/Makefileconfig.status: creating cachegrind/tests/x86/Makefileconfig.status: creating cachegrind/cg_annotateconfig.status: creating callgrind/Makefileconfig.status: creating callgrind/callgrind_annotateconfig.status: creating callgrind/callgrind_controlconfig.status: creating callgrind/tests/Makefileconfig.status: creating helgrind/Makefileconfig.status: creating helgrind/tests/Makefileconfig.status: creating massif/Makefileconfig.status: creating massif/tests/Makefileconfig.status: creating massif/perf/Makefileconfig.status: creating massif/ms_printconfig.status: creating lackey/Makefileconfig.status: creating lackey/tests/Makefileconfig.status: creating none/Makefileconfig.status: creating none/tests/Makefileconfig.status: creating none/tests/amd64/Makefileconfig.status: creating none/tests/ppc32/Makefileconfig.status: creating none/tests/ppc64/Makefileconfig.status: creating none/tests/x86/Makefileconfig.status: creating none/tests/linux/Makefileconfig.status: creating none/tests/darwin/Makefileconfig.status: creating none/tests/x86-linux/Makefileconfig.status: creating exp-ptrcheck/Makefileconfig.status: creating exp-ptrcheck/tests/Makefileconfig.status: creating drd/Makefileconfig.status: creating drd/scripts/download-and-build-splash2config.status: creating drd/tests/Makefileconfig.status: creating exp-bbv/Makefileconfig.status: creating exp-bbv/tests/Makefileconfig.status: creating exp-bbv/tests/x86/Makefileconfig.status: creating exp-bbv/tests/x86-linux/Makefileconfig.status: creating exp-bbv/tests/amd64-linux/Makefileconfig.status: creating exp-bbv/tests/ppc32-linux/Makefileconfig.status: creating config.hconfig.status: executing depfiles commandsMaximum build arch: x86Primary build arch: x86Secondary build arch:Build OS: linuxPrimary build target: X86_LINUXSecondary build target:Default supp files: exp-ptrcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.5.supp2. 直接在当期目录下执行maketelstar:/sybase/telstar/user/lblong/memory/valgrind-3.5.0 > makeecho "# This is a generated file, composed of the following suppression rules:" > default.supp echo "# " exp-ptrcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.5.supp >> default.suppcat exp-ptrcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.5.supp >> default.suppmake all-recursivemake[1]: Entering directory `/sybase/telstar/user/lblong/memory/valgrind-3.5.0'Making all in includemake[2]: Entering directory `/sybase/telstar/user/lblong/memory/valgrind-3.5.0/include'make[2]: Nothing to be done for `all'.make[2]: Leaving directory `/sybase/telstar/user/lblong/memory/valgrind-3.5.0/include'Making all in VEXmake[2]: Entering directory `/sybase/telstar/user/lblong/memory/valgrind-3.5.0/VEX'make all-ammake[3]: Entering directory `/sybase/telstar/user/lblong/memory/valgrind-3.5.0/VEX'if cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-main_globals.o -MD -MP -MF ".deps/libvex_x86_linux_a-main_globals.Tpo" -c -o libvex_x86_linux_a-main_globals.o `test -f 'priv/main_globals.c' || echo './'`priv/main_globals.c; \then mv -f ".deps/libvex_x86_linux_a-main_globals.Tpo" ".deps/libvex_x86_linux_a-main_globals.Po"; else rm -f ".deps/libvex_x86_linux_a-main_globals.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-main_main.o -MD -MP -MF ".deps/libvex_x86_linux_a-main_main.Tpo" -c -o libvex_x86_linux_a-main_main.o `test -f 'priv/main_main.c' || echo './'`priv/main_main.c; \then mv -f ".deps/libvex_x86_linux_a-main_main.Tpo" ".deps/libvex_x86_linux_a-main_main.Po"; else rm -f ".deps/libvex_x86_linux_a-main_main.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations-Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-main_util.o -MD -MP -MF ".deps/libvex_x86_linux_a-main_util.Tpo" -c -o libvex_x86_linux_a-main_util.o `test -f 'priv/main_util.c' || echo './'`priv/main_util.c; \then mv -f ".deps/libvex_x86_linux_a-main_util.Tpo" ".deps/libvex_x86_linux_a-main_util.Po"; else rm -f ".deps/libvex_x86_linux_a-main_util.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-ir_defs.o -MD -MP -MF ".deps/libvex_x86_linux_a-ir_defs.Tpo" -c -o libvex_x86_linux_a-ir_defs.o `test -f 'priv/ir_defs.c' || echo './'`priv/ir_defs.c; \then mv -f ".deps/libvex_x86_linux_a-ir_defs.Tpo" ".deps/libvex_x86_linux_a-ir_defs.Po"; else rm -f ".deps/libvex_x86_linux_a-ir_defs.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-ir_match.o -MD -MP -MF ".deps/libvex_x86_linux_a-ir_match.Tpo" -c -o libvex_x86_linux_a-ir_match.o `test -f 'priv/ir_match.c' || echo './'`priv/ir_match.c; \then mv -f ".deps/libvex_x86_linux_a-ir_match.Tpo" ".deps/libvex_x86_linux_a-ir_match.Po"; else rm -f ".deps/libvex_x86_linux_a-ir_match.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-ir_opt.o -MD -MP -MF ".deps/libvex_x86_linux_a-ir_opt.Tpo" -c -o libvex_x86_linux_a-ir_opt.o `test -f 'priv/ir_opt.c' || echo './'`priv/ir_opt.c; \then mv -f ".deps/libvex_x86_linux_a-ir_opt.Tpo" ".deps/libvex_x86_linux_a-ir_opt.Po"; else rm -f ".deps/libvex_x86_linux_a-ir_opt.Tpo"; exit 1; fi if cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align-fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_generic_bb_to_IR.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_generic_bb_to_IR.Tpo" -c -o libvex_x86_linux_a-guest_generic_bb_to_IR.o `test -f 'priv/guest_generic_bb_to_IR.c' || echo './'`priv/guest_generic_bb_to_IR.c; \then mv -f ".deps/libvex_x86_linux_a-guest_generic_bb_to_IR.Tpo" ".deps/libvex_x86_linux_a-guest_generic_bb_to_IR.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_generic_bb_to_IR.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_generic_x87.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_generic_x87.Tpo" -c -o libvex_x86_linux_a-guest_generic_x87.o `test -f 'priv/guest_generic_x87.c' || echo './'`priv/guest_generic_x87.c; \then mv -f ".deps/libvex_x86_linux_a-guest_generic_x87.Tpo" ".deps/libvex_x86_linux_a-guest_generic_x87.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_generic_x87.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_x86_helpers.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_x86_helpers.Tpo" -c -o libvex_x86_linux_a-guest_x86_helpers.o `test -f 'priv/guest_x86_helpers.c' || echo './'`priv/guest_x86_helpers.c; \then mv -f ".deps/libvex_x86_linux_a-guest_x86_helpers.Tpo" ".deps/libvex_x86_linux_a-guest_x86_helpers.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_x86_helpers.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_x86_toIR.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_x86_toIR.Tpo" -c -o libvex_x86_linux_a-guest_x86_toIR.o `test -f 'priv/guest_x86_toIR.c' || echo './'`priv/guest_x86_toIR.c; \then mv -f ".deps/libvex_x86_linux_a-guest_x86_toIR.Tpo" ".deps/libvex_x86_linux_a-guest_x86_toIR.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_x86_toIR.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1-DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_amd64_helpers.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_amd64_helpers.Tpo"-c -o libvex_x86_linux_a-guest_amd64_helpers.o `test -f 'priv/guest_amd64_helpers.c' || echo './'`priv/guest_amd64_helpers.c; \then mv -f ".deps/libvex_x86_linux_a-guest_amd64_helpers.Tpo" ".deps/libvex_x86_linux_a-guest_amd64_helpers.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_amd64_helpers.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_amd64_toIR.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_amd64_toIR.Tpo" -c -o libvex_x86_linux_a-guest_amd64_toIR.o `test -f 'priv/guest_amd64_toIR.c' || echo './'`priv/guest_amd64_toIR.c; \then mv -f ".deps/libvex_x86_linux_a-guest_amd64_toIR.Tpo" ".deps/libvex_x86_linux_a-guest_amd64_toIR.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_amd64_toIR.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_ppc_helpers.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_ppc_helpers.Tpo" -c -o libvex_x86_linux_a-guest_ppc_helpers.o `test -f 'priv/guest_ppc_helpers.c' || echo './'`priv/guest_ppc_helpers.c; \then mv -f ".deps/libvex_x86_linux_a-guest_ppc_helpers.Tpo" ".deps/libvex_x86_linux_a-guest_ppc_helpers.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_ppc_helpers.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_ppc_toIR.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_ppc_toIR.Tpo" -c -o libvex_x86_linux_a-guest_ppc_toIR.o `test -f 'priv/guest_ppc_toIR.c' || echo './'`priv/guest_ppc_toIR.c; \then mv -f ".deps/libvex_x86_linux_a-guest_ppc_toIR.Tpo"".deps/libvex_x86_linux_a-guest_ppc_toIR.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_ppc_toIR.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_arm_helpers.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_arm_helpers.Tpo" -c -o libvex_x86_linux_a-guest_arm_helpers.o `test -f 'priv/guest_arm_helpers.c' || echo './'`priv/guest_arm_helpers.c; \then mv -f ".deps/libvex_x86_linux_a-guest_arm_helpers.Tpo" ".deps/libvex_x86_linux_a-guest_arm_helpers.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_arm_helpers.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-guest_arm_toIR.o -MD -MP -MF ".deps/libvex_x86_linux_a-guest_arm_toIR.Tpo" -c -o libvex_x86_linux_a-guest_arm_toIR.o `test -f 'priv/guest_arm_toIR.c' || echo './'`priv/guest_arm_toIR.c; \then mv -f ".deps/libvex_x86_linux_a-guest_arm_toIR.Tpo" ".deps/libvex_x86_linux_a-guest_arm_toIR.Po"; else rm -f ".deps/libvex_x86_linux_a-guest_arm_toIR.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-host_generic_regs.o -MD -MP -MF ".deps/libvex_x86_linux_a-host_generic_regs.Tpo" -c -o libvex_x86_linux_a-host_generic_regs.o `test -f 'priv/host_generic_regs.c' || echo './'`priv/host_generic_regs.c; \then mv -f ".deps/libvex_x86_linux_a-host_generic_regs.Tpo" ".deps/libvex_x86_linux_a-host_generic_regs.Po"; else rm -f ".deps/libvex_x86_linux_a-host_generic_regs.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-host_generic_simd64.o -MD -MP -MF ".deps/libvex_x86_linux_a-host_generic_simd64.Tpo" -c -o libvex_x86_linux_a-host_generic_simd64.o `test -f 'priv/host_generic_simd64.c' || echo'./'`priv/host_generic_simd64.c; \then mv -f ".deps/libvex_x86_linux_a-host_generic_simd64.Tpo" ".deps/libvex_x86_linux_a-host_generic_simd64.Po"; else rm -f ".deps/libvex_x86_linux_a-host_generic_simd64.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-host_generic_reg_alloc2.o -MD -MP -MF ".deps/libvex_x86_linux_a-host_generic_reg_alloc2.Tpo" -c -o libvex_x86_linux_a-host_generic_reg_alloc2.o `test -f 'priv/host_generic_reg_alloc2.c' || echo './'`priv/host_generic_reg_alloc2.c; \then mv -f ".deps/libvex_x86_linux_a-host_generic_reg_alloc2.Tpo" ".deps/libvex_x86_linux_a-host_generic_reg_alloc2.Po"; else rm -f ".deps/libvex_x86_linux_a-host_generic_reg_alloc2.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-host_x86_defs.o -MD -MP -MF ".deps/libvex_x86_linux_a-host_x86_defs.Tpo" -c -o libvex_x86_linux_a-host_x86_defs.o `test -f 'priv/host_x86_defs.c' || echo './'`priv/host_x86_defs.c; \then mv -f ".deps/libvex_x86_linux_a-host_x86_defs.Tpo" ".deps/libvex_x86_linux_a-host_x86_defs.Po"; else rm -f ".deps/libvex_x86_linux_a-host_x86_defs.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-host_x86_isel.o -MD -MP -MF ".deps/libvex_x86_linux_a-host_x86_isel.Tpo" -c -o libvex_x86_linux_a-host_x86_isel.o `test -f 'priv/host_x86_isel.c' || echo './'`priv/host_x86_isel.c; \then mv -f ".deps/libvex_x86_linux_a-host_x86_isel.Tpo" ".deps/libvex_x86_linux_a-host_x86_isel.Po"; else rm -f ".deps/libvex_x86_linux_a-host_x86_isel.Tpo"; exit 1; fiif cc -DHA VE_CONFIG_H -I. -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_x86=1 -DVGO_linux=1 -DVGP_x86_linux=1 -Ipriv -m32 -mpreferred-stack-boundary=2 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wbad-function-cast -Wcast-qual -Wcast-align -fstrict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -MT libvex_x86_linux_a-host_amd64_defs.o -MD -MP -MF。

内存调试工具 valgrind

内存调试工具 valgrind

内存调试工具valgrind来源::linux下面用c++写代码,在所难免会遇到segmentation fault(段错误)。

个人在编写ns扩展模块时候,遇到过很多段错误,虽然运行时刻经常由程序抛出段错误,但是段错误的发生的程序级别的原因多种多样,不过归结到系统级别上,段错误都是由于内存原因引起的(个人总结)。

会造成内存错误的程序级别的原因,也就是我们程序员所经常犯的错误大致可以分为以下几个方面:1,使用未初始化的指针-这是必然的,指针指空的东西,必然出错。

2,重复删除一个指针-必然,再次删除就会删除一个已经分配给别的程序的数据或者其他。

3,内存冲突-由于程序声明的两块数据区域重叠,造成混乱。

4,混杂的指针错误-只能具体问题具体分析,情况比较复杂。

对于一位刚开始用c++在linux编程的人来说,最常遇到的应该的就是1与2了。

当工程规模比较大,程序由小组完成而后整合等的情况下,很容易出现2,3,4的情况。

这时候的调试比较麻烦,也需要很多耐心。

我在做的wimax mesh的项目就是这样。

对于一个timer的使用,没有初始化,造成的段错误,一目了然。

工程进展非常顺利。

当工程做到50%时候(11.08号),遇到了一个段错误,结果调试到12.02号才调出来!我就来说一下我的调试历程吧!真是一波三折阿!开始的时候以为是1或者2的情况,反复检查,不是这样。

然后怀疑3或者4,结果由于没有使用任何工具,只是在代码中加打印信息,这时候只能把错误定位到transmit(p)这个函数上。

但是这个函数我只写了一行,就是transmit(p){downtarget_-recv(p,(Handle*)NULL);}程序在这个地方出错实在让人摸不到头绪,因为再往下执行就不是我代码的问题了,而是下层已有代码甚至是系统代码的问题阿!非常困扰!然后开始用gdb调试,gdb是一个很好的很强大的调试工具,我用的命令行的,所能完成的功能和vc下的调试工具差不多,只是需要看什么变量就是要用print×来看罢了,不过功能决不比它差。

Linux 内存调试工具- Valgrind 使用初探

Linux 内存调试工具- Valgrind 使用初探

Linux 内存调试工具- Valgrind 使用初探Valgrind 是在linux系统下开发应用程序时用于调试内存问题的工具。

它尤其擅长发现内存管理的问题,它可以检查程序运行时的内存泄漏问题。

它的官方网址是/ 下载最新版本的Valgrind,目前是3.2.0。

wget/downloads/valValgrind 是在linux系统下开发应用程序时用于调试内存问题的工具。

它尤其擅长发现内存管理的问题,它可以检查程序运行时的内存泄漏问题。

它的官方网址是/下载最新版本的Valgrind,目前是3.2.0。

wget/downloads/valkyrie-1.2.0.tar.bz2执行常规的安装步骤:./confgure && make && make install。

注意:系统必须安装QT的开发包。

即便这样在make 时还是出现qplatformdefs.h这个文件找不到的情况,导致make失败。

查找系统中的qplatformdefs.h 之后,发现没有存在于qt的标准头文件目录/usr/lib/qt-3.3/include。

如是将/usr/lib/qt-3.3/mkspecs /linux-g++/ 目录下该头文件复制标准头文件目录,重新make ,后面一切OK。

初次使用编译如下代码: gcc -Wall example.c -g -o example#include <stdlib.h>void f(void){int* x = malloc(10 * sizeof(int));x[10] = 0; // problem 1: heap block overrun} // problem 2: memory leak -- x not freedint main(void){f();return 0;}注意:gcc 的-g 选项让Valgrind调试输出时指出相应信息的代码所在的行号。

valgrind内存查看工具介绍

valgrind内存查看工具介绍

Valgrind内存工具学习目录一. VALGRIND工具简介 (1)1.1V ALGRIND体系结构概述 (1)1.2L INUX内存空间布局 (2)二. VALGRIND的安装 (4)2.1在L INUX系统下的安装 (4)2.2在设备中的安装 (4)三. MEMCHECK模块使用 (4)3.1使用未初始化的内存 (5)3.2内存读写越界 (6)3.3内存覆盖 (7)3.4动态内存管理错误 (8)3.5内存泄露 (9)3.6内存管理规则 (12)四. MASSIF模块的使用 (13)4.1问题引出 (13)4.2运行MASSIF模块 (14)4.3运行MS_PRINT (14)4.4.OUT文件声明 (14)4.5内存分配趋势图说明 (15)4.6内存分配详细说明 (16)4.6.1 内存分配快照表 (16)4.6.2 分配函数内存详细 (17)4.7参数说明 (17)五. CACHEGRIND使用 (18)5.1使用命令: (18)5.2结果分析 (18)六. HELGRIND模块使用 (20)七. 附录 (20)一. Valgrind工具简介用于定位应用程序开发中的内存问题。

Valgrind是linux下开源的内存问题检测工具。

1.1 Valgrind体系结构概述Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合。

Valgrind由内核(core)以及基于内核的其他调试工具组成。

内核类似于一个框架(framework),它模拟了一个CPU环境,并提供服务给其他工具;而其他工具则类似于插件(plug-in),利用内核提供的服务完成各种特定的内存调试任务。

Valgrind的体系结构如下图所示:图 1.1 Valgrind 体系结构Valgrind包括如下一些工具:Memcheck。

这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。

valgrind的使用方法-详细手册

valgrind的使用方法-详细手册

valgrind的使用Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析。

你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的malloc和free或者 C++中的new和 delete。

使用Valgrind的工具包,你可以自动的检测许多内存管理和线程的bug,避免花费太多的时间在bug寻找上,使得你的程序更加稳固。

Valgrind的主要功能Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。

下面分别介绍个工具的作用:Memcheck 工具主要检查下面的程序错误:∙使用未初始化的内存 (Use of uninitialised memory)∙使用已经释放了的内存(Reading/writing memory after it has been free’d) ∙使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)∙对堆栈的非法访问 (Reading/writing inappropriate areas on the stack) ∙申请的空间是否有释放 (Memory leaks –where pointers to malloc’d blocks are lost forever)∙malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])∙src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)CallgrindCallgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。

valgrind 使用手册之一

valgrind 使用手册之一2011-07-28 17:37valgrindValgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management andthreading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.下载:svn co svn:///valgrind/trunk valgrind安装:cd valgrind./autogen.sh./configure --prefix=...makesudo make install使用方法:要检测的程序必须加-g选项,推荐使用-O0,不优化内存检测:如果你运行程序时命令是这样的:myprog arg1 arg2检测时改成这样:valgrind --leak-check=yes myprog arg1 arg2 ,默认是memcheckvalgrind --tool=cachegrind myprog arg1 arg2 ,这个调用的则是cachegrind,输出结果在当前目录下的cachegrind.out.<pid> pid为程序的pid Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif,你可以用上面的--tool=来指定,指定时没有大写exp-ptrcheck, a heap, stack and global array overrun detectorvalgrind --tool=exp-ptrcheck --log-file=val.log ./fbv -g/windows/F/photos/102_4085.JPG输出如下:==17627== exp-ptrcheck, a heap, stack and global array overrun detector ==17627== NOTE: This is an Experimental-Class Valgrind Tool==17627== Copyright (C) 2003-2010, and GNU GPL'd, by OpenWorks Ltd et al. ==17627== Using Valgrind-3.7.0.SVN and LibVEX; rerun with -h for copyright info==17627== Command: ./fbv -g /windows/F/photos/102_4085.JPG==17627== Parent PID: 2448==17627====17627====17627== For counts of detected and suppressed errors, rerun with: -v ==17627== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)valgrind --tool=callgrind ./fbv -g /windows/F/photos/102_4085.JPG 同时另一个命令行下运行:callgrind_control -b输出如下:PID 17678: ./fbv -g /windows/F/photos/102_4085.JPG [requesting'Status'...]Frame: Backtrace for Thread 1[ 0] rotate (1 x)[ 1] show_image (1 x)[ 2] main (1 x)[ 3] (below main) (1 x)[ 4] 0x080495a0 (1 x)[ 5] 0x00000850程序结束后,产生文件callgrind.out.<pid>callgrind_annotate [options] callgrind.out.<pid> 产生较精简的函数summaryMassif is a heap profiler. It measures how much heap memory your program uses. This includes both the useful space,and the extra bytes allocated for book-keeping and alignment purposes. It can also measure the size of yourprogram's stack(s), although it does not do so by default.valgrind --tool=massif ./fbv -g /windows/F/photos/102_4085.JPG 生成堆使用情况的logms_print massif.out.19747 对log做些处理产生可易理解的信息--------------------------------------------------------------------------------n time(i) total(B) useful-heap(B)extra-heap(B) stacks(B)--------------------------------------------------------------------------------46 444,727,063 12,190,488 12,190,464 24 047 444,727,955 12,715,808 12,715,776 32 048 446,465,375 12,190,488 12,190,464 24 049 446,466,273 15,336,224 15,336,192 32 050 456,720,045 15,336,224 15,336,192 32 0100.00% (15,336,192B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.->61.54% (9,437,184B) 0x8049820: show_image (in/home/zengming/workspace/fbv/fbv)| ->61.54% (9,437,184B) 0x804A87D: main (in/home/zengming/workspace/fbv/fbv)............DHAT is a tool for examining how programs use their heap allocations. valgrind --tool=exp-dhat ./fbv -g /windows/F/photos/102_4085.JPG==19813====19813== ======== SUMMARY STATISTICS ==========19813====19813== guest_insns: 456,751,347==19813====19813== max_live: 15,336,192 in 4 blocks==19813====19813== tot_alloc: 16,539,148 in 25 blocks==19813====19813== insns per allocated byte: 27==19813====19813====19813== ======== ORDERED BY decreasing "max-bytes-live": top 10 allocators ==========19813====19813== -------------------- 1 of 10 --------------------==19813== max-live: 9,437,184 in 1 blocks==19813== tot-alloc: 9,437,184 in 1 blocks (avg size 9437184.00)==19813== deaths: 1, at avg age 456,008,575 (99.83% of prog lifetime)==19813== acc-ratios: 0.25 rd, 1.00 wr (2,359,296 b-read, 9,437,184 b-written)==19813== at 0x4026BB4: malloc (vg_replace_malloc.c:236)==19813== by 0x8049821: show_image (in/home/zengming/workspace/fbv/fbv)==19813== by 0x804A87E: main (in /home/zengming/workspace/fbv/fbv) ...............英文说明:/docs/manual/manual.html=============================使用valgrind进行后台服务器的性能优化2008-04-30一. Valgrind安装说明:先从/上将安装包down下来(使用3.2.0及以上版本),然后进行安装:./configuremakemake install (as "root" if needed)二.使用valgrind进行性能瓶颈定位:1.先将服务器运行环境搭建好,确定服务器程序能正常运行及响应请求。

valgrind memcheck用法

valgrind memcheck用法Valgrind是一个开源的内存调试工具,可以帮助你检测程序中的内存错误。

Memcheck是Valgrind的一个重要组件,专门用于检测程序中的内存错误,包括使用未初始化的内存、使用已经释放的内存、越过内存边界等。

以下是使用Valgrind Memcheck的一般步骤:1.安装Valgrind:首先,你需要在你的系统上安装Valgrind。

对于大多数Linux发行版,你可以通过包管理器来安装。

例如,在Ubuntu上,你可以使用以下命令来安装Valgrind:arduinosudo apt-get install valgrind2.编译程序:在使用Valgrind运行程序之前,你需要先编译程序,以便生成可执行文件。

确保在编译时开启调试信息,以便Valgrind能够获取更多的信息。

例如,使用gcc编译器时,你可以使用以下命令编译程序:gcc -g -o myprogram myprogram.c3.使用Valgrind运行程序:一旦你有了可执行文件,就可以使用Valgrind来运行程序了。

使用以下命令来运行程序:bashvalgrind --tool=memcheck myprogram这将启动Memcheck工具来检测程序中的内存错误。

如果你的程序需要接受命令行参数,你可以将它们作为Valgrind命令的参数,例如:bashvalgrind --tool=memcheck myprogram arg1 arg2 ...4.查看结果:Valgrind会输出一个详细的报告,显示程序中潜在的内存错误。

报告将列出各种类型的错误,包括使用未初始化的内存、使用已经释放的内存、越过内存边界等。

你可以根据报告中的信息来修复程序中的错误。

以上就是使用Valgrind Memcheck的一般步骤。

请注意,Memcheck可能会使程序的运行速度变慢,因此在进行性能敏感的程序时需要谨慎使用。

valgrind基本用法

valgrind基本用法英文回答:Valgrind is a powerful tool for debugging and profiling programs. It is mainly used for memory debugging, memory leak detection, and performance profiling. Valgrind runs your program in a virtual environment and monitors all memory accesses, allowing it to detect errors and provide detailed information about memory usage.To use Valgrind, you need to first install it on your system. Once installed, you can run your program with Valgrind by using the following command:valgrind <options> <program>。

Here, `<options>` are the various options you can pass to Valgrind, such as `--leak-check` for memory leak detection or `--tool=callgrind` for performance profiling. `<program>` is the path to your program's executable.Valgrind provides a number of tools that can be usedfor different purposes. One of the most commonly used tools is Memcheck, which is the default tool and is used for memory debugging and leak detection. Memcheck can detect a wide range of memory errors, such as reading from uninitialized variables, accessing memory after it has been freed, or writing to memory that has already been freed.Here is an example of how to use Valgrind with Memcheck:valgrind --leak-check=yes ./my_program.This command will run `my_program` with Memcheckenabled and will perform memory leak checking. After the program finishes running, Valgrind will provide a detailed report of any memory errors or leaks that were detected.Another useful tool provided by Valgrind is Callgrind, which is used for performance profiling. Callgrind collects information about the program's execution, such as the number of instructions executed, cache misses, and functioncall counts. This information can be used to identify performance bottlenecks and optimize the program.Here is an example of how to use Valgrind with Callgrind:valgrind --tool=callgrind ./my_program.This command will run `my_program` with Callgrind enabled and will collect performance profiling data. After the program finishes running, Valgrind will generate a file called `callgrind.out.<pid>`, where `<pid>` is the process ID of the program. This file can be analyzed using the`kcachegrind` tool, which provides a graphical interfacefor visualizing the performance data.Valgrind is a versatile tool that can greatly aid in the debugging and profiling of programs. It is widely used in the software development industry and is considered an essential tool for any serious programmer.中文回答:Valgrind是一个强大的用于调试和性能分析程序的工具。

Valgrind的安装与简单使用

Valgrind的安装与简单使用展开全文获取源码wget /downloads/valgrind-3.14.0.tar.bz2 解压缩tar -jxvf valgrind-3.14.0.tar.bz2安装,其中/home/vmuser/valgrind是要安装的目录cd valgrind-3.14.0./configure --prefix=/home/vmuser/valgrindmake &amp;&amp; make install配置环境变量1)首先打开文件/etc/profile2)将下面内容追加到文件尾export PATH=$PATH:/home/vmuser/valgrind/bin/3)使配置生效source /etc/profile使用建立文件vi val假设想要检测的执行文件是main,并且想把结果输入到valgrind_report.log中,就在文件中写入下面的内容:valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --run-libc-freeres=yes --log-file=./valgrind_report.log ./main $@如果只想把结果打印到屏幕上,就在文件中写入下面的内容:valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --run-libc-freeres=yes ./main最后给文件val添加执行权限:sudo chmod 777 val./val执行如果有内存泄漏等问题,就会有相应的打印在日志文件中或屏幕上。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档