网络推荐



本广告位招租!

推荐给好友 上一篇 | 下一篇

Valgrind 使用简单说明

BSD爱好者乐园X%Leb(hc

Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析。你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的malloc和free或者 C++中的new和 delete。使用Valgrind的工具包,你可以自动的检测许多内存管理和线程的bug,避免花费太多的时间在bug寻找上,使得你的程序更加稳固。

2a t x Q2De ~ d xValgrind 是在linux系统下开发应用程序时用于调试内存问题的工具。它尤其擅长发现内存管理的问题,它可以检查程序运行时的内存泄漏问题。BSD爱好者乐园*b u T/P^1nQ

   它的官方网址是http://www.valgrind.org/BSD爱好者乐园/cteB0M

BSD爱好者乐园V#nwy[}M ~]^

   下载最新版本的Valgrind,目前是3.2.0。 wgethttp://www.valgrind.org/downloads/valkyrie-1.2.0.tar.bz2BSD爱好者乐园:pK%A0v)Z#[*nJI

@ Hm(W+C7L"Ea{8s   执行常规的安装步骤:./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。

X(` x_xrl
初次使用
编译如下代码:  gcc -Wall example.c -g -o example

'D9F"]p.A BSD爱好者乐园+]qUJ&S8w

#include <stdlib.h>BSD爱好者乐园!|v{!bFsAox

,}3c9CT7h0M"Mc5Lvoid f(void)BSD爱好者乐园SDP~f)R.A
{BSD爱好者乐园&s?'m lo\6jo)X:X
int* x = malloc(10 * sizeof(int));BSD爱好者乐园GW9z(od~ D
x[10] = 0; // problem 1: heap block overrun
}/_c.HUY:{K1P5Xq} // problem 2: memory leak -- x not freed
QhT5kG#HwBSD爱好者乐园M;C+p XA*Q[6`'u j
int main(void)
a-[!fX7Y@` n!qC{BSD爱好者乐园:_`dK x!b Mh
f();BSD爱好者乐园C+G2ot x#~[
return 0;BSD爱好者乐园,K oT'RKf:r9Q
}

%JN-Uc;CD     注意:gcc 的-g 选项让Valgrind调试输出时指出相应信息的代码所在的行号。

,P~#Q/@6u w_S8H

3{],V-F4UOZ7WfZ 

_T#U,aes{@Ps
valgrind --tool=memcheck --leak-check=yes ./example

;p^}-{%r8F~ BSD爱好者乐园s1@7F Q"xG4p

==6742== Memcheck, a memory error detector for x86-linux.
A@9m1V0Q r==6742== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.BSD爱好者乐园7a(RApe
==6742== Using valgrind-2.2.0, a program supervision framework for x86-linux.
Y&bw0TiP$n2a _==6742== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
Qg F j%i==6742== For more details, rerun with: -vBSD爱好者乐园k)J(r,V!f g E4W
==6742==BSD爱好者乐园4^,fN:p PK1TmT5p
==6742== Invalid write of size 4
+L"X?8e&RnN==6742==    at 0x8048384: f (example.c:6)BSD爱好者乐园 I:j M'PV;Ze]
==6742==    by 0x80483AC: main (example.c:12)BSD爱好者乐园&E6h2R5J-V
==6742== Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
6` }+B(h&G!HU \==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)BSD爱好者乐园3c0d/e9@l,[J @l
==6742==    by 0x8048377: f (example.c:5)BSD爱好者乐园nR:Y5`4r!oN5q.rE
==6742==    by 0x80483AC: main (example.c:12)
F$]h.oU @.d9Y4_==6742==
xt c6f%\F8Pd==6742== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
RX0u k4|'fd@-e==6742== malloc/free: in use at exit: 40 bytes in 1 blocks.
X0d%ha^%R==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
2}2m/AE%L4iZ==6742== For counts of detected errors, rerun with: -v
9Z dh%g$}2?5y.M.c==6742== searching for pointers to 1 not-freed blocks.BSD爱好者乐园%^6{MHAMydM
==6742== checked 1360800 bytes.BSD爱好者乐园9J E syD)X
==6742==BSD爱好者乐园C/RIl ufB
==6742==
!W"eYA ]S*f Jt-BM==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
2L+DW)y GrE==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)BSD爱好者乐园"A`3XCMO!UL x
==6742==    by 0x8048377: f (example.c:5)BSD爱好者乐园:~%} d9y\!n%G
==6742==    by 0x80483AC: main (example.c:12)
'{*hjf#@bwg==6742==
n1S;_*yT2} K%s==6742== LEAK SUMMARY:BSD爱好者乐园p }l Q,fD
==6742==    definitely lost: 40 bytes in 1 blocks.
2k6z%@ I"zyQ!I==6742==    possibly lost:   0 bytes in 0 blocks.
['gSRRt9M3r#~==6742==    still reachable: 0 bytes in 0 blocks.BSD爱好者乐园4bp GzbX;A
==6742==         suppressed: 0 bytes in 0 blocks.
2[&i p9S.t]==6742== Reachable blocks (those to which a pointer was found) are not shown.
"q+Ej%} H9H!Ai4gWB==6742== To see them, rerun with: --show-reachable=yes

/\ A0~x"T+V   上面的C程序存在两个错误:1. 数组下标越界;2. 分配的内存没有释放,存在内存泄露的问题。对于错误1,看Valgrind的调试信息片断

5y2ag-n2N-U N\+p
==6742== Invalid write of size 4
kQy FZ#}:S==6742==    at 0x8048384: f (example.c:6)
-i\rH3Z.fo'H8R==6742==    by 0x80483AC: main (example.c:12)BSD爱好者乐园q%x R&D%P%Y5q6e9O ]
==6742== Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
*}8aq W5Vg==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)BSD爱好者乐园B,k6R BZCj6?xs0o
==6742==    by 0x8048377: f (example.c:5)
对于错误2,看这个BSD爱好者乐园3]"o xi Dj&X#`Z

==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.

u|u~{]~)OBSD爱好者乐园W |2z3G3q.ln+K

......

C,X8^ w!s6_,K1i/fBSD爱好者乐园7ONqq*Bn7Q&T)K

==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
h)REx/p~/Q_1m==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
%G'yoI8D;c==6742==    by 0x8048377: f (example.c:5)BSD爱好者乐园,w F.Gz&atlQ^6s
==6742==    by 0x80483AC: main (example.c:12)

)h!M2Af+kbVuIj)y8WBSD爱好者乐园JK.RbHc+y
BSD爱好者乐园U1g\(h`p,L%E

Valgrind的主要功能

BSD爱好者乐园4nx0o!V5d;`#P

Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:BSD爱好者乐园 k pe!~)?

BSD爱好者乐园6kt[F6_x ^ k

Memcheck 工具主要检查下面的程序错误:BSD爱好者乐园?(^D%Q1x ^G

  • 使用未初始化的内存 (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)

Callgrind

BSD爱好者乐园"V/r}7nAv eY;Q4W"Q

Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。

SSdxO

Cachegrind

.|Gjc+\C它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。

wk2jje9W+F+D\

Helgrind

.Jgp{z.F它主要用来检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为” Eraser” 的竞争检测算法,并做了进一步改进,减少了报告错误的次数。BSD爱好者乐园8z-Lk o$D(UG~

Massif

b,vC/vZ7gq^t4G堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。

ET ] NW'v

Valgrind安装

1、 到www.valgrind.org下载最新版valgrind-3.2.3.tar.bz2
j1V(sR |3B)o2、 解压安装包:tar –jxvfvalgrind-3.2.3.tar.bz2BSD爱好者乐园E(xgR]0m(l6w#]+B
3、 解压后生成目录valgrind-3.2.3BSD爱好者乐园!SJ|,Ur8ze5D'L
4、 cdvalgrind-3.2.3
6q!hgf8rDN#J5、 ./configureBSD爱好者乐园UGl3UHI!jA _-}Q`
6、 Make;make install

Valgrind使用

BSD爱好者乐园2|u#o/g,~"`N

用法:valgrind[options] prog-and-args [options]: 常用选项,适用于所有Valgrind工具BSD爱好者乐园/F)W _M7{ ~R2nP

  1. -tool=<name> 最常用的选项。运行valgrind中名为toolname的工具。默认memcheck。
  2. h –help 显示帮助信息。
  3. -version 显示valgrind内核的版本,每个工具都有各自的版本。
  4. q –quiet 安静地运行,只打印错误信息。
  5. v –verbose 更详细的信息, 增加错误数统计。
  6. -trace-children=no|yes 跟踪子线程? [no]
  7. -track-fds=no|yes 跟踪打开的文件描述?[no]
  8. -time-stamp=no|yes 增加时间戳到LOG信息? [no]
  9. -log-fd=<number> 输出LOG到描述符文件 [2=stderr]
  10. -log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
  11. -log-file-exactly=<file> 输出LOG信息到 file
  12. -log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]
  13. -log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port
BSD爱好者乐园SW1t t5SQB

LOG信息输出

G;FL|,D0i6x
  1. -xml=yes 将信息以xml格式输出,只有memcheck可用
  2. -num-callers=<number> show <number> callers in stack traces [12]
  3. -error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]
  4. -error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]
  5. -db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]
  6. -db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p]
BSD爱好者乐园5n4S3dOwT

适用于Memcheck工具的相关选项:BSD爱好者乐园0N;~`,Zq-B9}Da#G

  1. -leak-check=no|summary|full 要求对leak给出详细信息? [summary]
  2. -leak-resolution=low|med|high how much bt merging in leak check [low]
  3. -show-reachable=no|yes show reachable blocks in leak check? [no]

Valgrind使用举例

BSD爱好者乐园4l6QC0r5Wk2q

下面是一段有问题的C程序代码test.c

#^gx.]h7g
#include <stdlib.h>BSD爱好者乐园c3R0g2d'lr*G,n
void f(void)BSD爱好者乐园2T2y;b[5qr2A5DY
{BSD爱好者乐园2q;rp8V6bkt-`
int* x = malloc(10 * sizeof(int));BSD爱好者乐园Y0^iNE7g$M
x[10] = 0; //问题1: 数组下标越界
V6i v?uF2a} //问题2: 内存没有释放
int main(void)BSD爱好者乐园\e-gt0R f S&u
{BSD爱好者乐园*y(_3O E*Y?7X
f();BSD爱好者乐园 O#t(d"d4BP|
return 0;
Aj V-Un7m3`YG}
1、 编译程序test.cBSD爱好者乐园g {'K3O!]
gcc -Wall test.c -g -o test
-B L%H6P"l*Z&J2、使用Valgrind检查程序BUGBSD爱好者乐园]5MQh?iF,A@
valgrind--tool=memcheck --leak-check=full ./testBSD爱好者乐园!XeH,J L&h;Jd
3、 分析输出的调试信息
TQ/X$KV==3908== Memcheck, a memory error detector.BSD爱好者乐园!I?YU `8I
==3908== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
x)Eb,FR0`C X==3908== Using LibVEX rev 1732, a library for dynamic binary translation.BSD爱好者乐园*Jk3C[?b
==3908== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.BSD爱好者乐园wP B){"RV,B;EqI
==3908== Usingvalgrind-3.2.3, a dynamic binary instrumentation framework.
/~|'W$Mh==3908== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.BSD爱好者乐园Xv)l&fN~ x;rX6G
==3908== For more details, rerun with: -vBSD爱好者乐园iH;l FSW
==3908==BSD爱好者乐园8hg$~Po.yA
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50BSD爱好者乐园Rc-Q!^v4i,j3R
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
#b.]"W@D d5Y8e/*数组越界错误*/BSD爱好者乐园2U4f!B1t/Z
==3908==Invalid write of size 4
+Ia:} y.Y`(B==3908== at 0x8048384: f (test.c:6)BSD爱好者乐园 \(t&C3~H-^,Esb
==3908== by 0x80483AC: main (test.c:11)
#GH4W3ye.|/p{y A%o==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd
u'fk-q ?8e.g UeTf==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
#M2N`;V$Eq==3908== by 0x8048377: f (test.c:5)BSD爱好者乐园k{yu9@lE;y?n
==3908== by 0x80483AC: main (test.c:11)
K];bh.wU#?g==3908==
#Z._5t4}K~x:O1o==3908==ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)BSD爱好者乐园$E+pN,y~-v6Pc2I`3r+`
==3908== malloc/free: in use at exit: 40 bytes in 1 blocks.
t%S-L#fk3Y8@V==3908== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
j"A_(K.p D]o Z==3908== For counts of detected errors, rerun with: -vBSD爱好者乐园-g;M"V'I!\8w$J `;v
==3908== searching for pointers to 1 not-freed blocks.
F A4TR8Jkq#p"y7`==3908== checked 59,124 bytes.BSD爱好者乐园?X7u's:zm'f5gS
==3908==
C @I`t&A im==3908==
9eq6yJ3?A q/*有内存空间没有释放*/
e4hvu D L y[d5i-gQ==3908==40 bytes in 1 blocks are definitely lost in loss record 1 of 1
n$T9Z+E \4?{z
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
;X+A{3d)U]Qf==3908== by 0x8048377: f (test.c:5)
$^&?4Ks#D[uZSsp'`==3908== by 0x80483AC: main (test.c:11)BSD爱好者乐园)\ w7if's)M"GBq
==3908==
n2c-q\8v;B==3908==LEAK SUMMARY:
3y6q\nD&w==3908== definitely lost: 40 bytes in 1 blocks.BSD爱好者乐园 Ot6Ur*znI
==3908== possibly lost: 0 bytes in 0 blocks.
aGa:m Q:@==3908== still reachable: 0 bytes in 0 blocks.BSD爱好者乐园!VrV7R^%g+\
==3908== suppressed: 0 bytes in 0 blocks

[版权声明]BSD爱好者乐园站内文章,如来源不是互联网,则均系原创或翻译之作,可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。
TAG: Valgrind
 

评分:0

我来说两句

seccode