Valgrind使用說明
文章原地址:https://www.cnblogs.com/wangkangluo1/archive/2011/07/20/2111248.html
Valgrind是運行在Linux上一套基於仿真技術的程序調試和分析工具,是公認的最接近Purify的產品,它包含一個內核——一個軟件合成的CPU,和一系列的小工具,每個工具都可以完成一項任務——調試,分析,或測試等。Valgrind可以檢測內存泄漏和內存越界,還可以分析cache的使用等,靈活輕巧而又強大。
一 Valgrind概觀
Valgrind的最新版是3.2.3,該版本包含下列工具:
1、memcheck:檢查程序中的內存問題,如泄漏、越界、非法指針等。
2、callgrind:檢測程序代碼覆蓋,以及分析程序性能。
3、cachegrind:分析CPU的cache命中率、丟失率,用於進行代碼優化。
4、helgrind:用於檢查多線程程序的競態條件。
5、massif:堆棧分析器,指示程序中使用了多少堆內存等信息。
6、lackey:
7、nulgrind:
二 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能在這裏幫上大忙。
2.Callgrind
和gprof類似的分析工具,但它對程序的運行觀察更是入微,能給我們提供更多的信息。和gprof不同,它不需要在編譯源代碼時附加特殊選項,但加上調試選項是推薦的。Callgrind收集程序運行時的一些數據,建立函數調用關係圖,還可以有選擇地進行cache模擬。在運行結束時,它會把分析數據寫入一個文件。callgrind_annotate可以把這個文件的內容轉化成可讀的形式。
說明:這個工具我也沒有用會,網上基本沒有找到有指導性的文檔,暫時留在後面慢慢研究吧。
3.Cachegrind
Cache分析器,它模擬CPU中的一級緩存I1,Dl和二級緩存,能夠精確地指出程序中cache的丟失和命中。如果需要,它還能夠爲我們提供cache丟失次數,內存引用次數,以及每行代碼,每個函數,每個模塊,整個程序產生的指令數。這對優化程序有很大的幫助。
作一下廣告:valgrind自身利用該工具在過去幾個月內使性能提高了25%-30%。據早先報道,kde的開發team也對valgrind在提高kde性能方面的幫助表示感謝。
4.Helgrind
它主要用來檢查多線程程序中出現的競爭問題。Helgrind尋找內存中被多個線程訪問,而又沒有一貫加鎖的區域,這些區域往往是線程之間失去同步的地方,而且會導致難以發掘的錯誤。Helgrind實現了名爲“Eraser”的競爭檢測算法,並做了進一步改進,減少了報告錯誤的次數。不過,Helgrind仍然處於實驗階段。
5. Massif
堆棧分析器,它能測量程序在堆棧中使用了多少內存,告訴我們堆塊,堆管理塊和棧的大小。Massif能幫助我們減少內存的使用,在帶有虛擬內存的現代系統中,它還能夠加速我們程序的運行,減少程序停留在交換區中的機率。
Massif對內存的分配和釋放做profile。程序開發者通過它可以深入瞭解程序的內存使用行爲,從而對內存使用進行優化。這個功能對C++尤其有用,因爲C++有很多隱藏的內存分配和釋放
此外,lackey和nulgrind也會提供。Lackey是小型工具,很少用到;Nulgrind只是爲開發者展示如何創建一個工具。我們就不做介紹了。
三 使用Valgrind
Valgrind使用起來非常簡單,你甚至不需要重新編譯你的程序就可以用它。當然如果要達到最好的效果,獲得最準確的信息,還是需要按要求重新編譯一下的。比如在使用memcheck的時候,最好關閉優化選項。
valgrind命令的格式如下:
valgrind [valgrind-options] your-prog [your-prog options]
一些常用的選項如下:
選項 | 作用 |
-h --help | 顯示幫助信息。 |
--version | 顯示valgrind內核的版本,每個工具都有各自的版本。 |
-q --quiet | 安靜地運行,只打印錯誤信息。 |
-v --verbose | 打印更詳細的信息。 |
--tool=<toolname> [default: memcheck] | 最常用的選項。運行valgrind中名爲toolname的工具。如果省略工具名,默認運行memcheck。 |
--db-attach=<yes|no> [default: no] | 綁定到調試器上,便於調試錯誤。 |
1、檢測內存泄漏
示例代碼如下:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *ptr;
ptr = (char *)malloc(10);
return 0;
}
保存爲memleak.c並編譯,然後用valgrind檢測。
$ gcc -o memleak memleak.c
(valgrind和purify最大的不同在於:valgrind只接管程序執行的過程,編譯時不需要valgrind干預,而purify會干預程序編譯過程)
$ valgrind --tool=memcheck ./memleak
我們得到如下錯誤信息:
[konten@tencent test_valgrind]$ valgrind ./memleak
==29646== Memcheck, a memory error detector.
==29646== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==29646== Using LibVEX rev 1732, a library for dynamic binary translation.
==29646== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==29646== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==29646== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==29646== For more details, rerun with: -v
==29646==
==29646==
==29646== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)
==29646== malloc/free: in use at exit: 10 bytes in 1 blocks. //指示在程序退出時,還有多少內存沒有釋放。
==29646== malloc/free: 1 allocs, 0 frees, 10 bytes allocated. // 指示該執行過程malloc和free調用的次數。
==29646== For counts of detected errors, rerun with: -v // 提示如果要更詳細的信息,用-v選項。
==29646== searching for pointers to 1 not-freed blocks.
==29646== checked 56,164 bytes.
==29646==
==29646== LEAK SUMMARY:
==29646== definitely lost: 10 bytes in 1 blocks.
==29646== possibly lost: 0 bytes in 0 blocks.
==29646== still reachable: 0 bytes in 0 blocks.
==29646== suppressed: 0 bytes in 0 blocks.
==29646== Rerun with --leak-check=full to see details of leaked memory.
[konten@tencent test_valgrind]$
以上結果中,紅色的是手工添加的說明信息,其他是valgrind的輸出。可以看到,如果我們僅僅用默認方式執行,valgrind只報告內存泄漏,但沒有顯示具體代碼中泄漏的地方。
因此我們需要使用 “--leak-check=full”選項啓動 valgrind,我們再執行一次:
[konten@tencent test_valgrind]$ valgrind --leak-check=full ./memleak
==29661== Memcheck, a memory error detector.
==29661== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==29661== Using LibVEX rev 1732, a library for dynamic binary translation.
==29661== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==29661== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==29661== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==29661== For more details, rerun with: -v
==29661==
==29661==
==29661== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)
==29661== malloc/free: in use at exit: 10 bytes in 1 blocks.
==29661== malloc/free: 1 allocs, 0 frees, 10 bytes allocated.
==29661== For counts of detected errors, rerun with: -v
==29661== searching for pointers to 1 not-freed blocks.
==29661== checked 56,164 bytes.
==29661==
==29661== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==29661== at 0x401A846: malloc (vg_replace_malloc.c:149)
==29661== by 0x804835D: main (memleak.c:6)
==29661==
==29661== LEAK SUMMARY:
==29661== definitely lost: 10 bytes in 1 blocks.
==29661== possibly lost: 0 bytes in 0 blocks.
==29661== still reachable: 0 bytes in 0 blocks.
==29661== suppressed: 0 bytes in 0 blocks.
[konten@tencent test_valgrind]$
和上次的執行結果基本相同,只是多了上面藍色的部分,指明瞭代碼中出現泄漏的具體位置。
以上就是用valgrind檢查內存泄漏的方法,用到的例子比較簡單,複雜的代碼最後結果也都一樣。
2、其他內存問題 我們下面的例子中包括常見的幾類內存問題:堆中的內存越界、踩內存、棧中的內存越界、非法指針使用、重複free。 #include <stdlib.h> #include <stdio.h> int main(void) { char *ptr = malloc(10); ptr[12] = 'a'; // 內存越界 memcpy(ptr +1, ptr, 5); // 踩內存 char a[10]; a[12] = 'i'; // 數組越界 free(ptr); // 重複釋放 free(ptr); char *p1; *p1 = '1'; // 非法指針
return 0; } 編譯: gcc -o invalidptr invalidptr.c -g 執行:valgrind --leak-check=full ./invalidptr 結果如下: [konten@tencent test_valgrind]$ valgrind --leak-check=full ./invalidptr ==29776== Memcheck, a memory error detector. ==29776== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al. ==29776== Using LibVEX rev 1732, a library for dynamic binary translation. ==29776== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP. ==29776== Using valgrind-3.2.3, a dynamic binary instrumentation framework. ==29776== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al. ==29776== For more details, rerun with: -v ==29776== ==29776== Invalid write of size 1 //堆內存越界被查出來 ==29776== at 0x80483D2: main (invalidptr.c:7) ==29776== Address 0x4159034 is 2 bytes after a block of size 10 alloc'd ==29776== at 0x401A846: malloc (vg_replace_malloc.c:149) ==29776== by 0x80483C5: main (invalidptr.c:6) ==29776== ==29776== Source and destination overlap in memcpy(0x4159029, 0x4159028, 5) //踩內存 ==29776== at 0x401C96D: memcpy (mc_replace_strmem.c:116) ==29776== by 0x80483E6: main (invalidptr.c:9) ==29776== ==29776== Invalid free() / delete / delete[] //重複釋放 ==29776== at 0x401B3FB: free (vg_replace_malloc.c:233) ==29776== by 0x8048406: main (invalidptr.c:16) ==29776== Address 0x4159028 is 0 bytes inside a block of size 10 free'd ==29776== at 0x401B3FB: free (vg_replace_malloc.c:233) ==29776== by 0x80483F8: main (invalidptr.c:15) ==29776== ==29776== Use of uninitialised value of size 4 ==29776== at 0x804840D: main (invalidptr.c:19) ==29776== //非法指針,導致coredump ==29776== Process terminating with default action of signal 11 (SIGSEGV): dumping core ==29776== Bad permissions for mapped region at address 0x80482AD ==29776== at 0x804840D: main (invalidptr.c:19) ==29776== ==29776== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 15 from 1) ==29776== malloc/free: in use at exit: 0 bytes in 0 blocks. ==29776== malloc/free: 1 allocs, 2 frees, 10 bytes allocated. ==29776== For counts of detected errors, rerun with: -v ==29776== All heap blocks were freed -- no leaks are possible. Segmentation fault [konten@tencent test_valgrind]$ 從上面的結果看出,除了棧內存越界外,其他常見的內存問題都可以用valgrind簡單的查出來。 3、顯示代碼覆蓋 用callgrind工具能方便的顯示程序執行的代碼覆蓋情況。 看如下例子:
4、顯示線程競態條件 <該版本暫不支持> 用helgrind工具可以在多線程代碼中找到可能產生競態條件的地方。 四 memcheck 工具的常用選型
五 Valgrind的編譯安裝 1、下載源代碼,下載地址http://valgrind.org/downloads/current.html#current ,截止目前爲止,最新版本是3.2.3 2、編譯,在源代碼目錄下執行: ./configure --prefix=[你自己的安裝目錄] make;make install 便好了。 3、配置缺省選項 valgrind提供3種方式用於設置缺省選項: a、
--memcheck: |