valgrind是Linux(x86)环境的内存调试工具,可以在此工具中运行程序来检查内存使用,可以自动检测到内存泄漏及内存管理的BUG,使你的程序更加健壮。
1。valgrind安装:下载地址:www.valgrind.org,安装很简单,执行configure,make,make install
2。valgrind工具集:
memcheck:检测程序中的内存管理BUG,所有的写/读操作都会被、malloc/free都会被截获
cachegrind:cache剖析器,它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。
Callgrind:同cachegrind一样,但能跟踪函数的调用返回关系,及有选择的模拟cache。
Massif:堆栈分析器,它能检测程序在堆栈中使用了多少内存,以及堆块,堆管理块和栈的大小。帮助我们减少程序对内存的使用。
Helgrind:检测使用POSIX pthread多线程同步问题。
3。valgrind命令格式:
valgrind [valgrind-options] your-prog [your-prog-options]
valgrind-options选项:
--tool=
-h --help 帮助
--version 显示版本
-q --quiet 只显示错误
-v --verbose 更多显示
-d 显示valgrind自身调试信息
--trace-children=
--child-silent-after-fork=
--track-fds=
--time-stamp=
--log-fd=
--log-file=
--log-socket=
与错误相关的选项:
--xml=
--num-callers=
--error-limit=
--error-exitcode=
--show-below-main=
--db-attach=
--db-command=
--leak-check=
--show-reachable=
其它选项不很常用。有些也不很理解,这里就省略了。
在检测前,确认使用-g选项编译你的程序,这样以便能报告错误的行数
4。valgrind错误报告例子:
memcheck
非法的读写访问,数组越界
Invalid read of size 4 at 0x40F6BBCC:
(within /usr/lib/libpng.so.2.1.0.9) by 0x40F6B804:
(within /usr/lib/libpng.so.2.1.0.9) by 0x40B07FF4: read_png_image(QImageIO *)
(kernel/qpngio. by 0x40AC751B: QImageIO::read()
(kernel/qimage.cpp:3621) Address 0xBFFFF0E0 is not stack’d, malloc’d or free’d
使用未初始化指针
Conditional jump or move depends on uninitialised value(s) at 0x402DFA94:
_IO_vfprintf (_itoa.h:49) by 0x402E8476:
_IO_printf (printf.c:36) by 0x8048472: main (tests/manuel1.c:8)
非法释放
Invalid free() at 0x4004FFDF: free (vg_clientmalloc.c:577) by 0x80484C7:
main (tests/doublefree.c:10) Address 0x3807F7B4 is 0 bytes
inside a block of size 177 free’d at 0x4004FFDF: free (vg_clientmalloc.c:577) by 0x80484C7:
main (tests/doublefree.c:10)
不适当的释放
Mismatched free() / delete / delete [] at 0x40043249: free (vg_clientfuncs.c:171) by 0x4102BB4E:
QGArray::~QGArray(void) (tools/qgarray.cpp:149) by 0x4C261C41:
PptDoc::~PptDoc(void) (include/qmemarray.h:60) by 0x4C261F0E:
PptXml::~PptXml(void) (pptxml.cc:44) Address 0x4BB292A8 is 0 bytes
inside a block of size 64 alloc’d at 0x4004318C:
operator new[](unsigned int) (vg_clientfuncs.c:152) by 0x4C21BC15: KLaola::
readSBStream(int) const (klaola.cc:314) by 0x4C21C155: KLaola::
stream(KLaola::OLENode const *) (klaola.cc:416) by 0x4C21788F:
OLEFilter::convert(QCString const &) (olefilter.cc:272)
源与目的块重叠
==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)
==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71)
==27492== by 0x804865A: main (overlap.c:40)
缺省输出报告文件名:工具名.out.pid
Helgrind线程BUG检测工具:能够报告一些线程使用的常见问题。比如:
释放一个无效mutex、释放未加锁的mutex、释放一个被其它线程持有的mutex、销毁一个无效的或者加锁的mutex、递归加锁一个非递归锁、释放内存包含加锁的mutex、必须处理使用pthread函数失败返回错误码、当线程退出时仍持有着锁,调用pthread_cond_wait时使用未申请的mutex或一个被其它线程加锁的mutex
错误报告如下:
Thread #1 unlocked a not-locked lock at 0x7FEFFFA90 at 0x4C2408D: pthread_mutex_unlock (hg_intercepts.c:492) by 0x40073A: nearly_main (tc09_bad_unlock.c:27) by 0x40079B: main (tc09_bad_unlock.c:50) Lock at 0x7FEFFFA90 was first observed at 0x4C25D01: pthread_mutex_init (hg_intercepts.c:326) by 0x40071F: nearly_main (tc09_bad_unlock.c:23) by 0x40079B: main (tc09_bad_unlock.c:50)
比较常用的是memcheck、Helgrind工具。经常使用valgrind能使你的程序更加完美。