全部博文(41)
分类: LINUX
2009-01-09 18:13:49
Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析。你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的malloc和free或者 C++中的new和 delete。使用Valgrind的工具包,你可以自动的检测许多内存管理和线程的bug,避免花费太多的时间在bug寻找上,使得你的程序更加稳固。
Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:
Memcheck 工具主要检查下面的程序错误:
Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。
它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。
它主要用来检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为” Eraser” 的竞争检测算法,并做了进一步改进,减少了报告错误的次数。
堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。
1、 到www.valgrind.org下载最新版valgrind-3.2.3.tar.bz2
2、 解压安装包:tar –jxvf valgrind-3.2.3.tar.bz2
3、 解压后生成目录valgrind-3.2.3
4、 cd valgrind-3.2.3
5、 ./configure
6、 Make;make install
用法: valgrind [options] prog-and-args [options]: 常用选项,适用于所有Valgrind工具
LOG信息输出
适用于Memcheck工具的相关选项:
下面是一段有问题的C程序代码test.c
#i nclude
void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; //问题1: 数组下标越界
} //问题2: 内存没有释放
int main(void)
{
f();
return 0;
}
1、 编译程序test.c
gcc -Wall test.c -g -o test
2、 使用Valgrind检查程序BUG
valgrind --tool=memcheck --leak-check=full ./test
3、 分析输出的调试信息
==3908== Memcheck, a memory error detector.
==3908== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==3908== Using LibVEX rev 1732, a library for dynamic binary translation.
==3908== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==3908== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==3908== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==3908== For more details, rerun with: -v
==3908==
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
/*数组越界错误*/
==3908== Invalid write of size 4
==3908== at 0x8048384: f (test.c:6)
==3908== by 0x80483AC: main (test.c:11)
==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)
==3908== malloc/free: in use at exit: 40 bytes in 1 blocks.
==3908== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==3908== For counts of detected errors, rerun with: -v
==3908== searching for pointers to 1 not-freed blocks.
==3908== checked 59,124 bytes.
==3908==
==3908==
/*有内存空间没有释放*/
==3908== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== LEAK SUMMARY:
==3908== definitely lost: 40 bytes in 1 blocks.
==3908== possibly lost: 0 bytes in 0 blocks.
==3908== still reachable: 0 bytes in 0 blocks.
==3908== suppressed: 0 bytes in 0 blocks.
附内存占用表:
--------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B)
stacks(B)
--------------------------------------------------------------------------------
69 1,960,678,424 15,953,352 15,077,080 876,272
0
70 1,978,734,481 15,943,736 15,067,480 876,256
0
71 1,997,140,926 15,943,736 15,067,480 876,256
0
72 2,015,695,615 15,943,736 15,067,480 876,256
0
73 2,034,038,837 15,943,736 15,067,480 876,256
0
74 2,052,495,904 15,943,736 15,067,480 876,256
0
75 2,070,419,744 15,943,736 15,067,480 876,256
0
76 2,088,911,237 15,943,736 15,067,480 876,256
0
77 2,109,363,396 15,960,872 15,084,580 876,292
0
78 2,127,250,762 15,894,184 15,017,944 876,240
0
94.49% (15,017,944B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->74.76% (11,882,444B) 0x465FEBB: gdk_pixbuf_new
(in /usr/lib/libgdk_pixbuf-2.0.so.0.1200.9)
| ->74.75% (11,880,844B) 0x5FF8C63:
(within /usr/lib/gtk-2.0/2.10.0/loaders/libpixbufloader-png.so)
| | ->74.75% (11,880,844B) 0x466364F:
(within /usr/lib/libgdk_pixbuf-2.0.so.0.1200.9)
| | ->74.75% (11,880,844B) 0x46644CC: gdk_pixbuf_new_from_file
(in /usr/lib/libgdk_pixbuf-2.0.so.0.1200.9)
| | ->20.91% (3,323,792B) 0x805F0E2: style_keybg_draw (style.c:812)
| | |
| | ->20.20% (3,210,112B) 0x80567CE: keyboard_set_background
(keyboard.c:1398)
| | |
| | ->18.82% (2,991,240B) 0x805FE7B: style_symbol_draw (style.c:370)
| | |
| | ->11.91% (1,893,612B) 0x805E950: style_symbolkb_symbol_draw
(style.c:686)
| | |
| | ->02.12% (336,960B) 0x805F538: style_keybg_switch (style.c:608)
| | |
| | ->00.79% (125,128B) in 1+ places, all below ms_print's threshold
(01.00%)
| |
| ->00.01% (1,600B) in 1+ places, all below ms_print's threshold
(01.00%)
|
->06.40% (1,017,493B) in 746 places, all below massif's threshold
(01.00%)
|
->04.59% (729,808B) 0x402115F: posix_memalign (vg_replace_malloc.c:569)
| ->04.59% (729,808B) 0x48A2373:
(within /usr/lib/libglib-2.0.so.0.1600.6)
| ->03.99% (634,088B) 0x48A35A0: g_slice_alloc
(in /usr/lib/libglib-2.0.so.0.1600.6)
| | ->02.43% (385,888B) 0x487954C:
(within /usr/lib/libglib-2.0.so.0.1600.6)
| | | ->01.93% (307,024B) 0x43583F2:
(within /usr/lib/libgtk-x11-2.0.so.0.1200.9)
| | | |
| | | ->00.50% (78,864B) in 1+ places, all below ms_print's threshold
(01.00%)
| | |
| | ->01.56% (248,200B) in 18 places, all below massif's threshold
(01.00%)
| |
| ->00.60% (95,720B) in 1+ places, all below ms_print's threshold
(01.00%)
|
->04.01% (637,469B) 0x4E79B1C: (within /usr/lib/libfreetype.so.6.3.16)
| ->04.01% (637,469B) 0x4E7D34A: ft_mem_qalloc
(in /usr/lib/libfreetype.so.6.3.16)
| ->04.01% (637,469B) 0x4E7EFF2: ft_mem_alloc
(in /usr/lib/libfreetype.so.6.3.16)
| ->03.75% (595,667B) 0x4E7F124: ft_mem_qrealloc
(in /usr/lib/libfreetype.so.6.3.16)
| | ->03.75% (595,667B) 0x4E7F20E: ft_mem_realloc
(in /usr/lib/libfreetype.so.6.3.16)
| |
| ->00.26% (41,802B) in 1+ places, all below ms_print's threshold
(01.00%)
|
->02.11% (334,906B) 0x48A6FD0: g_strndup
(in /usr/lib/libglib-2.0.so.0.1600.6)
| ->02.11% (334,759B) 0x435771A:
(within /usr/lib/libgtk-x11-2.0.so.0.1200.9)
| | ->02.07% (329,754B) 0x435839D:
(within /usr/lib/libgtk-x11-2.0.so.0.1200.9)
| | | ->01.06% (168,149B) in 2 places, all below massif's threshold
(01.00%)
| | | |
| | | ->01.02% (161,605B) 0x435825D:
(within /usr/lib/libgtk-x11-2.0.so.0.1200.9)
| | | ->01.02% (161,605B) in 2 places, all below massif's threshold
(01.00%)
| | |
| | ->00.03% (5,005B) in 1+ places, all below ms_print's threshold
(01.00%)
| |
| ->00.00% (147B) in 1+ places, all below ms_print's threshold (01.00%)
|
->01.48% (235,020B) 0x42337FC: art_alloc
(in /usr/lib/libart_lgpl_2.so.2.3.20)
| ->01.48% (235,020B) in 30 places, all below massif's threshold
(01.00%)
|
->01.14% (180,804B) 0x8056952: keyboard_setsize (keyboard.c:1441)
->01.14% (180,804B) 0x805E526: layoutreader_readkeyboard
(layoutreader.c:314)
->01.14% (180,804B) 0x80583B0: keyboard_new (keyboard.c:1479)
->01.14% (180,804B) 0x8054D99: flo_add_extension (florence.c:364)
->01.14% (180,804B) 0x8055561: flo_set_curr_layout
(florence.c:673)
|
->00.00% (0B) in 1+ places, all below ms_print's threshold
(01.00%)
--------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B)
stacks(B)
--------------------------------------------------------------------------------
79 2,145,129,854 15,907,992 15,027,431 880,561
0
(END)