Valgrind使用说明
生活随笔
收集整理的這篇文章主要介紹了
Valgrind使用说明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from:http://bbs.rosoo.net/thread-726-1-1.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檢查內存泄漏的方法,用到的例子比較簡單,復雜的代碼最后結果也都一樣。
總結
以上是生活随笔為你收集整理的Valgrind使用说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推荐系统中所使用的混合技术介绍
- 下一篇: inotify-tools使用方法介绍