Valgrind通常用來成分析程序性能及程序中的內存洩露錯誤
一 Valgrind工具集簡紹
Valgrind包含下列工具:
1、memcheck:檢查程序中的內存問題,如洩漏、越界、非法指針等。
2、callgrind:檢測程序代碼的運行時間和調用過程,以及分析程序性能。
3、cachegrind:分析CPU的cache命中率、丟失率,用於進行代碼優化。
4、helgrind:用於檢查多線程程序的競態條件。
5、massif:堆棧分析器,指示程序中使用了多少堆內存等信息。
6、lackey:
7、nulgrind:
這幾個工具的使用是通過命令:valgrand --tool=name 程序名來分別調用的,當不指定tool參數時默認是 --tool=memcheck
二 Valgrind工具詳解
1.Memcheck
最常用的工具,用來檢測程序中出現的內存問題,所有對內存的讀寫都會被檢測到,一切對malloc、free、new、delete的調用都會被捕獲。所以,它能檢測以下問題:
1、對未初始化內存的使用;
2、讀/寫釋放後的內存塊;
3、讀/寫超出malloc分配的內存塊;
4、讀/寫不適當的棧中內存塊;
5、內存洩漏,指向一塊內存的指針永遠丟失;
6、不正確的malloc/free或new/delete匹配;
7、memcpy()相關函數中的dst和src指針重疊。
這些問題往往是C/C++程序員最頭疼的問題,Memcheck能在這裡幫上大忙。
例如:
- #include <stdlib.h>
- #include <malloc.h>
- #include <string.h>
-
- void test()
- {
- int *ptr = malloc(sizeof(int)*10);
-
- ptr[10] = 7; // 內存越界
-
- memcpy(ptr +1, ptr, 5); // 踩內存
-
-
- free(ptr);
- free(ptr);// 重復釋放
-
- int *p1;
- *p1 = 1; // 非法指針
- }
-
- int main(void)
- {
- test();
- return 0;
- }
將程序編譯生成可執行文件後執行:valgrind --leak-check=full ./程序名
輸出結果如下:
- ==4832== Memcheck, a memory error detector
- ==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
- ==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
- ==4832== Command: ./tmp
- ==4832==
- ==4832== Invalid write of size 4 // 內存越界
- ==4832== at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd
- ==4832== at 0x4026864: malloc (vg_replace_malloc.c:236)
- ==4832== by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩內存
- ==4832== at 0x4027BD6: memcpy (mc_replace_strmem.c:635)
- ==4832== by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== Invalid free() / delete / delete[] // 重復釋放
- ==4832== at 0x4025BF0: free (vg_replace_malloc.c:366)
- ==4832== by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== Address 0x41a6028 is 0 bytes inside a block of size 40 free'd
- ==4832== at 0x4025BF0: free (vg_replace_malloc.c:366)
- ==4832== by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== Use of uninitialised value of size 4 // 非法指針
- ==4832== at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832==
- ==4832== Process terminating with default action of signal 11 (SIGSEGV) //由於非法指針賦值導致的程序崩潰
- ==4832== Bad permissions for mapped region at address 0x419FFF4
- ==4832== at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== HEAP SUMMARY:
- ==4832== in use at exit: 0 bytes in 0 blocks
- ==4832== total heap usage: 1 allocs, 2 frees, 40 bytes allocated
- ==4832==
- ==4832== All heap blocks were freed -- no leaks are possible
- ==4832==
- ==4832== For counts of detected and suppressed errors, rerun with: -v
- ==4832== Use --track-origins=yes to see where uninitialised values come from
- ==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)
- Segmentation fault
從valgrind的檢測輸出結果看,這幾個錯誤都找了出來。
2.Callgrind
和gprof類似的分析工具,但它對程序的運行觀察更是入微,能給我們提供更多的信息。和gprof不同,它不需要在編譯源代碼時附加特殊選項,但加上調試選項是推薦的。Callgrind收集程序運行時的一些數據,建立函數調用關系圖,還可以有選擇地進行cache模擬。在運行結束時,它會把分析數據寫入一個文件。callgrind_annotate可以把這個文件的內容轉化成可讀的形式。
生成可視化的圖形需要下載gprof2dot:http://http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py
這是個python腳本,把它下載之後修改其權限chmod +7 gprof2dot.py ,並把這個腳本添加到$PATH路徑中的任一文件夾下,我是將它放到了/usr/bin目錄下,這樣就可以直接在終端下執行gprof2dot.py了。
Callgrind可以生成程序性能分析的圖形,首先來說說程序性能分析的工具吧,通常可以使用gnu自帶的gprof,它的使用方法是:在編譯程序時添加-pg參數,例如:
- #include <stdio.h>
- #include <malloc.h>
- void test()
- {
- sleep(1);
- }
- void f()
- {
- int i;
- for( i = 0; i < 5; i ++)
- test();
- }
- int main()
- {
- f();
- printf("process is over!\n");
- return 0;
- }
首先執行 gcc -pg -o tmp tmp.c,然後運行該程序./tmp,程序運行完成後會在當前目錄下生成gmon.out文件(這個文件gprof在分析程序時需要),
再執行gprof ./tmp | gprof2dot.py |dot -Tpng -o report.png,打開report.png結果:
顯示test被調用了5次,程序中耗時所占百分比最多的是test函數。
再來看 Callgrind的生成調用圖過程吧,執行:valgrind --tool=callgrind ./tmp,執行完成後在目錄下生成"callgrind.out.XXX"的文件這是分析文件,可以直接利用:callgrind_annotate callgrind.out.XXX 打印結果,也可以使用:gprof2dot.py -f callgrind callgrind.out.XXX |dot -Tpng -o report.png 來生成圖形化結果:
它生成的結果非常詳細,甚至連函數入口,及庫函數調用都標識出來了。