作者:sixth
用C/C++開發(fā)其中最令人頭疼的一個(gè)問題就是內(nèi)存管理,有時(shí)候?yàn)榱瞬檎乙粋€(gè)內(nèi)存泄漏或者一個(gè)內(nèi)存訪問越界,需要要花上好幾天時(shí)間,如果有一款工具能夠幫助我們做這件事情就好了,valgrind正好就是這樣的一款工具。
Valgrind是一款基于模擬linux下的程序調(diào)試器和剖析器的軟件套件,可以
運(yùn)行于x86,
amd64和ppc32架構(gòu)上。valgrind包含一個(gè)核心,它提供一個(gè)虛擬的CPU運(yùn)行程序,還有一系列的工具,它們完成調(diào)試,剖析和一些類似的任
務(wù)。valgrind是高度模塊化的,所以開發(fā)人員或者用戶可以給它添加新的工具而不會損壞己有的結(jié)構(gòu)。
valgrind的官方網(wǎng)址是:http://valgrind.org
你可以在它的網(wǎng)站上下載到最新的valgrind,它是開放源碼和免費(fèi)的。
一、介紹
valgrind包含幾個(gè)標(biāo)準(zhǔn)的工具,它們是:
1、memcheck
memcheck探測程序中內(nèi)存管理存在的問題。它檢查所有對內(nèi)存的讀/寫操作,并截取所有的malloc/new/free/delete調(diào)用。因此memcheck工具能夠探測到以下問題:
1)使用未初始化的內(nèi)存
2)讀/寫已經(jīng)被釋放的內(nèi)存
3)讀/寫內(nèi)存越界
4)讀/寫不恰當(dāng)?shù)膬?nèi)存棧空間
5)內(nèi)存泄漏
6)使用malloc/new/new[]和free/delete/delete[]不匹配。
2、cachegrind
cachegrind是一個(gè)cache剖析器。它模擬執(zhí)行CPU中的L1,
D1和L2
cache,因此它能很精確的指出代碼中的cache未命中。如果你需要,它可以打印出cache未命中的次數(shù),內(nèi)存引用和發(fā)生cache未命中的每一行
代碼,每一個(gè)函數(shù),每一個(gè)模塊和整個(gè)程序的摘要。如果你要求更細(xì)致的信息,它可以打印出每一行機(jī)器碼的未命中次數(shù)。在x86和amd64上,
cachegrind通過CPUID自動(dòng)探測機(jī)器的cache配置,所以在多數(shù)情況下它不再需要更多的配置信息了。
3、helgrind
helgrind查找多線程程序中的競爭數(shù)據(jù)。helgrind查找內(nèi)存地址,那些被多于一條線程訪問的內(nèi)存地址,但是沒有使用一致的鎖就會被查出。這表示這些地址在多線程間訪問的時(shí)候沒有進(jìn)行同步,很可能會引起很難查找的時(shí)序問題。
二、valgrind對你的程序都做了些什么
valgrind被設(shè)計(jì)成非侵入式的,它直接工作于可執(zhí)行文件上,因此在檢查前不需要重新編譯、連接和修改你的程序。要檢查一個(gè)程序很簡單,只需要執(zhí)行下面的命令就可以了
valgrind --tool=tool_name program_name
比如我們要對ls -l命令做內(nèi)存檢查,只需要執(zhí)行下面的命令就可以了
valgrind --tool=memcheck ls -l
不管是使用哪個(gè)工具,valgrind在開始之前總會先取得對你的程序的控制權(quán),從
可執(zhí)行關(guān)聯(lián)庫里讀取調(diào)試信息。然后在valgrind核心提供的虛擬CPU上運(yùn)行程序,valgrind會根據(jù)選擇的工具來處理代碼,該工具會向代碼中加
入檢測代碼,并把這些代碼作為最終代碼返回給valgrind核心,最后valgrind核心運(yùn)行這些代碼。
如果要檢查內(nèi)存泄漏,只需要增加--leak-check=yes就可以了,命令如下
valgrind --tool=memcheck --leak-check=yes ls -l
不同工具間加入的代碼變化非常的大。在每個(gè)作用域的末尾,memcheck加入代碼檢查每一片內(nèi)存的訪問和進(jìn)行值計(jì)算,代碼大小至少增加12倍,運(yùn)行速度要比平時(shí)慢25到50倍。
valgrind模擬程序中的每一條指令執(zhí)行,因此,檢查工具和剖析工具不僅僅是對你的應(yīng)用程序,還有對共享庫,GNU C庫,X的客戶端庫都起作用。
三、現(xiàn)在開始
首先,在編譯程序的時(shí)候打開調(diào)試模式(gcc編譯器的-g選項(xiàng))。如果沒有調(diào)試信
息,即使最好的valgrind工具也將中能夠猜測特定的代碼是屬于哪一個(gè)函數(shù)。打開調(diào)試選項(xiàng)進(jìn)行編譯后再用valgrind檢查,valgrind將會
給你的個(gè)詳細(xì)的報(bào)告,比如哪一行代碼出現(xiàn)了內(nèi)存泄漏。
當(dāng)檢查的是C++程序的時(shí)候,還應(yīng)該考慮另一個(gè)選項(xiàng)
-fno-inline。它使得函數(shù)調(diào)用鏈很清晰,這樣可以減少你在瀏覽大型C++程序時(shí)的混亂。比如在使用這個(gè)選項(xiàng)的時(shí)候,用memcheck檢查
openoffice就很容易。當(dāng)然,你可能不會做這項(xiàng)工作,但是使用這一選項(xiàng)使得valgrind生成更精確的錯(cuò)誤報(bào)告和減少混亂。
一些編譯優(yōu)化選項(xiàng)(比如-O2或者更高的優(yōu)化選項(xiàng)),可能會使得memcheck提交錯(cuò)誤的未初始化報(bào)告,因此,為了使得valgrind的報(bào)告更精確,在編譯的時(shí)候最好不要使用優(yōu)化選項(xiàng)。
如果程序是通過腳本啟動(dòng)的,可以修改腳本里啟動(dòng)程序的代碼,或者使用--trace-children=yes選項(xiàng)來運(yùn)行腳本。
下面是用memcheck檢查ls -l命令的輸出報(bào)告,在終端下執(zhí)行下面的命令
valgrind --tool=memcheck ls -l
程序會打印出ls -l命令的結(jié)果,最后是valgrind的檢查報(bào)告如下:
==4187==
==4187== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 2)
==4187== malloc/free: in use at exit: 15,154 bytes in 105 blocks.
==4187== malloc/free: 310 allocs, 205 frees, 60,093 bytes allocated.
==4187== For counts of detected errors, rerun with: -v
==4187== searching for pointers to 105 not-freed blocks.
==4187== checked 145,292 bytes.
==4187==
==4187== LEAK SUMMARY:
==4187== definitely lost: 0 bytes in 0 blocks.
==4187== possibly lost: 0 bytes in 0 blocks.
==4187== still reachable: 15,154 bytes in 105 blocks.
==4187== suppressed: 0 bytes in 0 blocks.
==4187== Reachable blocks (those to which a pointer was found) are not shown.
==4187== To see them, rerun with: --show-reachable=yes
這里的“4187”指的是執(zhí)行l(wèi)s -l的進(jìn)程ID,這有利于區(qū)別不同進(jìn)程的報(bào)告。memcheck會給出報(bào)告,分配置和釋放了多少內(nèi)存,有多少內(nèi)存泄漏了,還有多少內(nèi)存的訪問是可達(dá)的,檢查了多少字節(jié)的內(nèi)存。
下面舉兩個(gè)用valgrind做內(nèi)存檢查的例子
例子一 (test.c):
#include <string.h>
int main(int argc, char *argv[]) { char *ptr;
ptr = (char*) malloc(10); strcpy(ptr, "01234567890");
return 0; }
|
編譯程序
gcc -g -o test test.c
用valgrind執(zhí)行命令
valgrind --tool=memcheck --leak-check=yes ./test
報(bào)告如下
==4270== Memcheck, a memory error detector.
==4270== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==4270== Using LibVEX rev 1606, a library for dynamic binary translation.
==4270== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==4270== Using valgrind-3.2.0, a dynamic binary instrumentation framework.
==4270== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==4270== For more details, rerun with: -v
==4270==
==4270== Invalid write of size 1
==4270== at 0x4006190: strcpy (mc_replace_strmem.c:271)
==4270== by 0x80483DB: main (test.c:8)
==4270== Address 0x4023032 is 0 bytes after a block of size 10 alloc'd
==4270== at 0x40044F6: malloc (vg_replace_malloc.c:149)
==4270== by 0x80483C5: main (test.c:7)
==4270==
==4270== Invalid write of size 1
==4270== at 0x400619C: strcpy (mc_replace_strmem.c:271)
==4270== by 0x80483DB: main (test.c:8)
==4270== Address 0x4023033 is 1 bytes after a block of size 10 alloc'd
==4270== at 0x40044F6: malloc (vg_replace_malloc.c:149)
==4270== by 0x80483C5: main (test.c:7)
==4270==
==4270== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 1)
==4270== malloc/free: in use at exit: 10 bytes in 1 blocks.
==4270== malloc/free: 1 allocs, 0 frees, 10 bytes allocated.
==4270== For counts of detected errors, rerun with: -v
==4270== searching for pointers to 1 not-freed blocks.
==4270== checked 51,496 bytes.
==4270==
==4270==
==4270== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4270== at 0x40044F6: malloc (vg_replace_malloc.c:149)
==4270== by 0x80483C5: main (test.c:7)
==4270==
==4270== LEAK SUMMARY:
==4270== definitely lost: 10 bytes in 1 blocks.
==4270== possibly lost: 0 bytes in 0 blocks.
==4270== still reachable: 0 bytes in 0 blocks.
==4270== suppressed: 0 bytes in 0 blocks.
==4270== Reachable blocks (those to which a pointer was found) are not shown.
==4270== To see them, rerun with: --show-reachable=yes
從這份報(bào)告可以看出,進(jìn)程號是4270,test.c的第8行寫內(nèi)存越界了,引起寫內(nèi)存越界的是strcpy函數(shù),
第7行泄漏了10個(gè)字節(jié)的內(nèi)存,引起內(nèi)存泄漏的是malloc函數(shù)。
例子二(test2.c)
#include <stdio.h>
int foo(int x) { if (x < 0) { printf("%d ", x); }
return 0; }
int main(int argc, char *argv[]) { int x; foo(x);
return 0; }
|
編譯程序
gcc -g -o test2 test2.c
用valgrind做內(nèi)存檢查
valgrind --tool=memcheck ./test2
輸出報(bào)告如下
==4285== Memcheck, a memory error detector.
==4285== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==4285== Using LibVEX rev 1606, a library for dynamic binary translation.
==4285== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==4285== Using valgrind-3.2.0, a dynamic binary instrumentation framework.
==4285== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==4285== For more details, rerun with: -v
==4285==
==4285== Conditional jump or move depends on uninitialised value(s)
==4285== at 0x8048372: foo (test2.c:5)
==4285== by 0x80483B4: main (test2.c:16)
==4285==p p
==4285== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
==4285== malloc/free: in use at exit: 0 bytes in 0 blocks.
==4285== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==4285== For counts of detected errors, rerun with: -v
==4285== All heap blocks were freed -- no leaks are possible.
從這份報(bào)告可以看出進(jìn)程PID是4285,test2.c文件的第16行調(diào)用了foo函數(shù),在test2.c文件的第5行foo函數(shù)使用了一個(gè)未初始化的變量。
valgrind還有很多使用選項(xiàng),具體可以查看valgrind的man手冊頁和valgrind官方網(wǎng)站的在線文檔。
Windows用戶不必沮喪,雖然在Windows上沒有Valgrind可用,但是你可以試一試IBM的Purify,它在功能上和Valgrind相似。