Linux下Valgrind的使用方法
Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,它包含一个内核──一个软件合成的CPU,和一系列的小工具,每个工具都可以完成一项任务──调试,分析,或测试等。
Valgrind可以检测内存泄漏和内存违例,还可以分析cache的使用等,灵活轻巧而又强大,能直穿程序错误的心脏,真可谓是程序员的瑞士军刀。
一、Valgrind的主要功能
Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind,Callgrind,Massif。
下面分别介绍个工具的作用:
Memcheck 工具主要检查下面的程序错误:
1.使用未初始化的内存(Use of uninitialised memory)
2.使用已经释放了的内存(Reading/writingmemory after it has been free’d)
3.使用超过malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
4.对堆栈的非法访问(Reading/writinginappropriate areas on the stack)
5.申请的空间是否有释放(Memory leaks –where pointers to malloc’d blocks are lost forever)
6.malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
7.src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions) Callgrind
Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache模拟。
在运行结束时,它会把分析数据写入一个文件。
callgrind_annotate可以把这个文件的内容转化成可读的形式。
Cachegrind
它模拟CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中cache的丢失和命中。
如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。
这对优化程序有很大的帮助。
Helgrind
它主要用来检查多线程程序中出现的竞争问题。
Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。
Helgrind实现了名为” Eraser” 的竞争检测算法,并做了进一步改进,减少了报告错误的次数。
Massif
堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。
Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率
二. 使用Valgrind
Valgrind的使用非常简单,valgrind命令的格式如下:
valgrind [valgrind-options]your-prog [your-prog options]
一些常用的选项如下:
-h --help
显示帮助信息。
--version
显示valgrind内核的版本,每个工具都有各自的版本。
-q --quiet
安静地运行,只打印错误信息。
-v --verbose
打印更详细的信息。
--tool= [default: memcheck]
最常用的选项。
运行valgrind中名为toolname的工具。
如果省略工具名,默认运行memcheck。
--db-attach= [default: no]
绑定到调试器上,便于调试错误。
我们通过例子看一下它的具体使用。
我们构造一个存在内存泄漏的C程序,如下:
#include <stdio.h>
#include <stdlib.h>
int*Test(void)
{
int* x = malloc(10 * sizeof(int));
delete x;// problem 1: heap block overrun, problem 2: memory leak --x not free, only first address
return x;
}
int main(void)
{
int count;
Test();
printf("i =%d/n", count); //problem 3: use uninitialised value.
return 0;
}
$ gcc -Wall -o Test Test.c
$ valgrind --tool=memcheck ./ Test
$valgrind --tool=memcheck --leak-check=yes ./Test
三、安装
sudo apt-get install valgrind
/book2/天火大道。
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来源::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×来看罢了,不过功能决不比它差。
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应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。
C Free 5程序调试方法
C Free 5程序调试方法程序调试是软件开发过程中非常重要的一环,它可以帮助开发人员找出程序中的错误并进行修复。
在C语言中,程序调试是一项必不可少的技能。
本文将介绍C语言中常用的5种程序调试方法,以帮助开发人员更好地调试程序。
1. 使用printf语句调试:printf语句是C语言中最常用的调试工具之一。
通过在程序中插入printf语句,可以输出程序执行过程中的变量值、状态信息等,以便于观察程序的执行流程和变量的取值情况。
例如:```cint main() {int a = 10;printf("a的值为:%d\n", a);return 0;}```通过在程序中插入printf语句,可以观察到变量a的值为10,从而判断程序是否按照预期执行。
2. 使用断点调试:断点调试是一种常用的调试方法,它可以在程序执行过程中暂停程序的执行,以便于观察程序的执行状态和变量的取值情况。
在C语言中,可以使用调试器(如GDB)设置断点。
例如,在Linux环境下使用GDB调试程序,可以按照以下步骤设置断点:- 编译程序时加上-g选项,以便生成调试信息:```gcc -g program.c -o program```- 启动GDB调试器:```gdb program```- 设置断点:```break line_number```- 运行程序:```run```- 程序执行到断点处时会暂停,可以通过命令观察变量的取值情况,以及进行单步调试、查看栈帧等操作。
3. 使用assert宏调试:assert宏是C语言中的一个调试工具,它用于检查程序中的条件是否满足,如果条件不满足,则终止程序的执行,并输出错误信息。
使用assert宏可以在程序中插入一些断言,以确保程序的正确性。
例如:```c#include <assert.h>int divide(int a, int b) {assert(b != 0);return a / b;}int main() {int result = divide(10, 0);return 0;}```在上述代码中,使用assert宏判断除数b是否为0,如果为0,则终止程序的执行,并输出错误信息。
Shell脚本中的调试技巧和工具
Shell脚本中的调试技巧和工具Shell脚本是一种用于自动化任务和批处理的脚本语言,广泛应用于Linux和Unix操作系统中。
为了确保脚本的正确性和稳定性,调试是非常重要的一环。
本文将介绍一些常用的Shell脚本调试技巧和工具,帮助开发者快速定位和解决问题。
一、使用echo语句输出调试信息在编写Shell脚本时,我们可以使用echo语句输出调试信息。
通过打印变量的值或者各个执行步骤的提示信息,可以帮助我们理解脚本的执行过程。
例如,我们可以使用以下语句在脚本某一关键点输出变量的值:```echo "变量x的值为:$x"```通过查看输出结果,我们可以确定变量x当前的取值是否正确,从而判断脚本的逻辑是否正确。
二、使用set -x命令启用调试模式在Shell脚本中,我们可以使用set -x命令来启用调试模式。
调试模式会输出每条命令的执行结果,帮助我们了解脚本的执行过程。
例如,我们可以在脚本的第一行添加以下语句:```set -x```这样,在执行脚本时,系统会输出每一条命令的执行结果,包括命令的具体语法和参数值。
通过查看这些输出,我们可以了解到脚本在某个位置发生了错误或逻辑问题。
需要注意的是,在调试完成后,一定要记得将set -x命令删除或注释掉,以避免调试信息泄露给不必要的人员。
三、使用set -e命令检测错误Shell脚本中的命令有时可能会执行失败,为了及时发现并处理这些错误,我们可以在脚本中使用set -e命令。
该命令会在遇到执行失败的命令时,立即退出脚本的执行。
例如,我们可以在脚本的第一行添加以下语句:```set -e```这样,在执行脚本时,如果某个命令执行失败,脚本会立即停止执行,并输出错误信息。
通过查看这些错误信息,我们可以快速定位问题所在,并进行相应的处理。
四、使用trap命令捕捉信号在Shell脚本中,我们可以使用trap命令捕捉信号。
当脚本接收到指定的信号时,可以执行指定的命令或函数。
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介绍
QA 陈伟 2008.9.25
一 什么是valgrind
valgrind是一款运行在linux下的,用来定位c/c++程序 中内存使用方面的错误的工具,包括:内存泄漏、使用未 初始化的内存、读/写已释放的内存、读/写内存越界、使 用malloc/new/new[]和free/delete/delete[]不匹配,等等。
Valgrind是帮助程序员寻找程序里的bug和改进程序性 能的工具。程序通过valgrind运行时,valgrind收集各种有 用的信息,通过这些信息可以找到程序中潜在的bug和性 能瓶颈。
二 valgrind的安装
• 下载 下载最新版valgrind-3.3.1.tar.bz2 • 安装 解压安装包:tar –jxvf valgrind3.3.1.tar.bz2 解压后生成目录valgrind-3.3.1 cd valgrind-3.3.1 ./configure make make install
式。
Helgrind
• 用于检测多线程竞争资源的工具 • 它主要用来检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域, 这些区域往往是线程之间失去同步的地方,而且会导致难 以发掘的错误。Helgrind实现了名为”Eraser” 的竞争检 测算法,并做了进一步改进,减少了报告错误的次数。
Valgrind使用参数
• --log-fd=<number> [default: 2, stderr] 指定Valgrind把它所有的消息都输出到一个指定的文件描述符中去。 默认值2, 是标准错误输出(stderr)。注意这可能会干扰到客户端自身 对stderr的使用, Valgrind的输出与客户程序的输出将穿插在一起输出 到stderr。 • --log-file=<filename> • 指定Valgrind把它所有的信息输出到指定的文件中。实际上,被创建 文件的文件名是由filename、‘.’和进程号连接起来的(即 <filename>.<pid>),从而每个进程创建不同的文件。 • --log-file-exactly=<filename> 类似于--log-file,但是后缀“.pid”不会被添加。如果设置了这个选项, 使用Valgrind跟踪多个进程,可能会得到一个乱七八糟的文件。 • --log-file-qualifier=<VAR> • 当和 --log-file一起使用时,日志文件名将通过环境变量$VAR来筛选, 这对于MPI程序是有益的。 • --log-socket=<ip-address:port-number> 指定Valgrind输出所有的消息到指定的IP,指定的端口。当使用1500 端口时,端口有可能被忽略。如果不能建立一个到指定端口的连接, Valgrind将输出写到标准错误(stderr)。这个选项经常和一个Valgrind 监听程序一起使用。
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可能会使程序的运行速度变慢,因此在进行性能敏感的程序时需要谨慎使用。
Linux下常用的开发工具介绍
Linux下常用的开发工具介绍Linux是一种广泛应用于各种操作系统的开源操作系统内核,具有稳定性高、性能卓越、开放源码等优势。
为了方便开发人员开展工作,Linux提供了众多的开发工具,本文将介绍一些常用的Linux开发工具,包括文本编辑器、编译器、调试器等。
一、文本编辑器文本编辑器是开发人员必备的工具之一,它用于编写代码、修改配置文件等任务。
Linux下有许多优秀的文本编辑器可供选择,以下是其中几种常用的。
1. Vim:Vim是Linux下最受欢迎的文本编辑器之一,它具有强大的编辑功能和丰富的插件支持。
Vim支持多种编程语言的语法高亮以及代码折叠等特性,可以大大提高编码效率。
2. Emacs:Emacs是另一个强大的文本编辑器,它具有高度可定制性和可扩展性。
Emacs支持多种编程语言的语法高亮、智能代码补全等功能,同时还提供了许多其他功能,如邮件客户端、日历、文件管理器等。
3. Sublime Text:Sublime Text是一款流行的跨平台文本编辑器,界面简洁美观,具有丰富的插件和主题支持。
Sublime Text支持多光标编辑、代码片段、自动补全等特性,可以大大提高编码速度。
二、编译器编译器是将源代码转换为可执行文件的工具,Linux提供了多种编译器用于不同的编程语言。
1. GCC:GCC是Linux下最常用的C/C++编译器,它是GNU编译器套装的核心组件。
GCC具有强大的优化能力和丰富的警告机制,能够生成高效的可执行文件。
2. Clang:Clang是一款基于LLVM的C/C++编译器,与GCC相比,Clang具有更快的编译速度和更丰富的错误提示信息。
Clang还支持多种编程语言,如Objective-C、Swift等。
3. Python解释器:Python是一种非常流行的脚本语言,Linux通常会预装Python解释器。
Python解释器允许开发人员直接执行Python脚本,非常方便。
linux vpddecode用法
linux vpddecode用法
vpddecode是一个用来解码Vital Product Data(VPD)的Linux 命令行工具。
VPD是存储在计算机硬件上的数据,包括制造商、型号、序列号等信息。
vpddecode可以帮助用户获取这些信息。
vpddecode的用法非常简单,只需在终端中输入以下命令即可:vpddecode
运行该命令后,vpddecode会读取系统中可用的VPD,然后将包含在其中的信息打印到终端上。
这些信息可以用于硬件配置、故障排除和硬件识别等目的。
除了基本的用法外,vpddecode还提供了一些选项,可以用来进一步扩展其功能。
下面是一些常用的选项:
- -c:仅显示计算机Vital Product Data。
- -d device:指定要读取的设备路径,而不是默认的
/sys/firmware/vpd目录。
- -l:只显示标签(Key)列表,而不显示完整的Vital Product Data。
- -i:以离散的方式显示Vital Product Data,而不是默认的一
行格式。
vpddecode还支持从文件中读取VPD。
只需使用以下命令:
vpddecode -i -f文件名
这将使vpddecode从指定的文件中读取VPD,并以离散的方式显示。
这在某些情况下可能会很有用,比如从备份文件中恢复VPD数据。
总之,vpddecode是一个方便的工具,可以帮助用户获得硬件相关的信息,进而进行配置、排除故障和识别硬件。
其简单的用法和扩展
选项使其在Linux环境下非常实用。
