Valgrind簡介:
Valgrind是動態分析工具的架構。有很多Valgrind工具可以自動的檢測許多記憶體管理和多進程/線程的bugs,在細節上剖析你的程式。你也可以利用Valgrind架構來實現自己的工具。
Valgrind通常包括6個工具:一個記憶體錯誤偵測工具,兩個線程錯誤偵測工具,cache和分支預測的分析工具,堆的分析工具。
Valgrind的使用與CPU OS以及編譯器和C庫都有關係。目前支援下面的平台:
- x86/Linux
- AMD64/Linux
- PPC32/Linux
- PPC64/Linux
- ARM/Linux
- x86/MacOSX
- AMD64/MacOSX
Valgrind是GNU v2下的開源軟體,你可以從http://valgrind.org下載最新的原始碼。
Valgrind的安裝:
1.從http://valgrind.org下載最新的valgrind-3.7.0.tar.bz2d,用tar -xfvalgrind-3.7.0.tar.bz2解壓安裝包。
2.執行./configure,檢查安裝要求的配置。
3.執行make。
4.執行make install,最好是用root許可權。
5.試著valgrind ls -l來檢測是否正常工作。
Valgrind的概述:
Valgrind時建立動態分析工具的架構。它有一系列用於調試分析的工具。Valgrind的架構是組件化的,所以可以方便的添加新的工具而不影響當前的結構。
下面的工具是安裝時的標準配置:
Memcheck:用於檢測記憶體錯誤。它協助c和c++的程式更正確。
Cachegrind:用於分析cache和分支預測。它協助程式執行得更快。
Callgrind:用於函數調用的分析。
Helgrind:用於分析多線程。
DRD:也用於分析多線程。與Helgrind類似,但是用不同的分析技術,所以可以檢測不同的問題。
Massif:用於分析堆。它協助程式精簡記憶體的使用。
SGcheck:檢測棧和全域數組溢出的實驗性工具,它和Memcheck互補使用。
Valgrind的使用:
1.準備好程式:
編譯器時用-g,這樣編譯後的檔案包含調試資訊,那Memcheck的分析資訊中就包含正確的行號。最好使用-O0的最佳化等級,使用-O2及以上的最佳化等級使用時可能會有問題。
2.在Memcheck下運行程式:
如果你的程式執行如下:
myprog arg1 arg2
那麼使用如下:
valgrind --leak-check=yes myprog arg1 arg2
Memcheck是預設的工具。--leak-check開啟記憶體流失檢測的細節。
在上面的命令中運行程式會使得程式運行很慢,而且佔用大量的記憶體。Memcheck會顯示記憶體錯誤和檢測到的記憶體流失。
3.如何查看Memcheck的輸出:
這裡有一個執行個體c代碼(a.c),有一個記憶體錯誤和一個記憶體流失。
#include <stdlib.h>
void f(void)
{
int*x = (int *)malloc(10 * sizeof(int));
x[10]= 0;
//problem 1: heap block overrun
} //problem 2: memory leak -- x not freed
int main(void)
{
f();
return0;
}
運行如下:
huerjia@huerjia:~/NFS/valg/test$ valgrind--leak-check=yes ./a
==24780== Memcheck, a memory error detector
==24780== Copyright (C) 2002-2011, and GNUGPL'd, by Julian Seward et al.
==24780== Using Valgrind-3.7.0 and LibVEX;rerun with -h for copyright info
==24780== Command: ./a
==24780==
==24780== Invalid write of size 4
==24780== at 0x80484DF: f() (a.c:5)
==24780== by 0x80484F1: main (a.c:11)
==24780== Address 0x42d3050 is 0 bytes after a block of size 40 alloc'd
==24780== at 0x4026444: malloc (vg_replace_malloc.c:263)
==24780== by 0x80484D5: f() (a.c:4)
==24780== by 0x80484F1: main (a.c:11)
==24780==
==24780==
==24780== HEAP SUMMARY:
==24780== in use at exit: 40 bytes in 1 blocks
==24780== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==24780==
==24780== 40 bytes in 1 blocks aredefinitely lost in loss record 1 of 1
==24780== at 0x4026444: malloc (vg_replace_malloc.c:263)
==24780== by 0x80484D5: f() (a.c:4)
==24780== by 0x80484F1: main (a.c:11)
==24780==
==24780== LEAK SUMMARY:
==24780== definitely lost: 40 bytes in 1 blocks
==24780== indirectly lost: 0 bytes in 0 blocks
==24780== possibly lost: 0 bytes in 0 blocks
==24780== still reachable: 0 bytes in 0 blocks
==24780== suppressed: 0 bytes in 0 blocks
==24780==
==24780== For counts of detected andsuppressed errors, rerun with: -v
==24780== ERROR SUMMARY: 2 errors from 2contexts (suppressed: 17 from 6)
如何來閱讀這個輸出結果:
==24780== Memcheck, a memory error detector
==24780== Copyright (C) 2002-2011, and GNUGPL'd, by Julian Seward et al.
==24780== Using Valgrind-3.7.0 and LibVEX;rerun with -h for copyright info
==24780== Command: ./a
這一部分是顯示使用的工具以及版本資訊。其中24780是Process ID。
==24780== Invalid write of size 4
==24780== at 0x80484DF: f() (a.c:5)
==24780== by 0x80484F1: main (a.c:11)
==24780== Address 0x42d3050 is 0 bytes after a block of size 40 alloc'd
==24780== at 0x4026444: malloc (vg_replace_malloc.c:263)
==24780== by 0x80484D5: f() (a.c:4)
==24780== by 0x80484F1: main (a.c:11)
這部分指出了錯誤:Invalid write。後面的幾行顯示了函數堆棧。
==24780== HEAP SUMMARY:
==24780== in use at exit: 40 bytes in 1 blocks
==24780== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==24780==
==24780== 40 bytes in 1 blocks aredefinitely lost in loss record 1 of 1
==24780== at 0x4026444: malloc (vg_replace_malloc.c:263)
==24780== by 0x80484D5: f() (a.c:4)
==24780== by 0x80484F1: main (a.c:11)
==24780==
==24780== LEAK SUMMARY:
==24780== definitely lost: 40 bytes in 1 blocks
==24780== indirectly lost: 0 bytes in 0 blocks
==24780== possibly lost: 0 bytes in 0 blocks
==24780== still reachable: 0 bytes in 0 blocks
==24780== suppressed: 0 bytes in 0 blocks
這部分是對堆和泄漏的總結,可以看出記憶體流失的錯誤。
==24780== For counts of detected andsuppressed errors, rerun with: -v
==24780== ERROR SUMMARY: 2 errors from 2contexts (suppressed: 17 from 6)
這部分是堆所有檢測到的錯誤的總結。代碼中的兩個錯誤都檢測到了。
Helgrind:線程錯誤偵測工具
若使用這個工具,在Valgrind的命令中添加--tool=helgrind。
Helgrind用於c,c++下使用POSIXpthreads的程式的線程同步錯誤。
Helgrind可以檢測下面三類錯誤:
1.POSIX pthreads API的錯誤使用
2.由加鎖和解鎖順序引起的潛在的死結
3.資料競態--在沒有鎖或者同步機制下訪問記憶體
以資料競態為例來說明Helgrind的用法:
在不使用合適的鎖或者其他同步機制來保證單線程訪問時,兩個或者多個線程訪問同一塊記憶體就可能引發資料競態。
一個簡單的資料競態的例子:
#include <pthread.h>
int var = 0;
void* child_fn ( void* arg ) {
var++;/* Unprotected relative to parent */ /* this is line 6 */
returnNULL;
}
int main ( void ) {
pthread_tchild;
pthread_create(&child,NULL, child_fn, NULL);
var++;/* Unprotected relative to child */ /* this is line 13 */
pthread_join(child,NULL);
return0;
}
運行如下:
huerjia@huerjia:~/NFS/valg/test$ valgrind--tool=helgrind ./b
==25449== Helgrind, a thread error detector
==25449== Copyright (C) 2007-2011, and GNUGPL'd, by OpenWorks LLP et al.
==25449== Using Valgrind-3.7.0 and LibVEX;rerun with -h for copyright info
==25449== Command: ./b
==25449==
==25449==---Thread-Announcement------------------------------------------
==25449==
==25449== Thread #1 is the program's rootthread
==25449==
==25449== ---Thread-Announcement------------------------------------------
==25449==
==25449== Thread #2 was created
==25449== at 0x4123A38: clone (in /lib/tls/i686/cmov/libc-2.11.1.so)
==25449== by 0x40430EA: pthread_create@@GLIBC_2.1 (in /lib/tls/i686/cmov/libpthread-2.11.1.so)
==25449== by 0x402A9AD: pthread_create_WRK (hg_intercepts.c:255)
==25449== by 0x402AA85: pthread_create@* (hg_intercepts.c:286)
==25449== by 0x80484E1: main (b.c:11)
==25449==
==25449==----------------------------------------------------------------
==25449==
==25449== Possible data race during read ofsize 4 at 0x804A020 by thread #1
==25449== Locks held: none
==25449== at 0x80484E2: main (b.c:12)
==25449==
==25449== This conflicts with a previouswrite of size 4 by thread #2
==25449== Locks held: none
==25449== at 0x80484A7: child_fn (b.c:6)
==25449== by 0x402AB04: mythread_wrapper (hg_intercepts.c:219)
==25449== by 0x404296D: start_thread (in /lib/tls/i686/cmov/libpthread-2.11.1.so)
==25449== by 0x4123A4D: clone (in /lib/tls/i686/cmov/libc-2.11.1.so)
==25449==
==25449==----------------------------------------------------------------
==25449==
==25449== Possible data race during writeof size 4 at 0x804A020 by thread #1
==25449== Locks held: none
==25449== at 0x80484E2: main (b.c:12)
==25449==
==25449== This conflicts with a previouswrite of size 4 by thread #2
==25449== Locks held: none
==25449== at 0x80484A7: child_fn (b.c:6)
==25449== by 0x402AB04: mythread_wrapper (hg_intercepts.c:219)
==25449== by 0x404296D: start_thread (in /lib/tls/i686/cmov/libpthread-2.11.1.so)
==25449== by 0x4123A4D: clone (in /lib/tls/i686/cmov/libc-2.11.1.so)
==25449==
==25449==
==25449== For counts of detected andsuppressed errors, rerun with: -v
==25449== Use --history-level=approx or=none to gain increased speed, at
==25449== the cost of reduced accuracy ofconflicting-access information
==25449== ERROR SUMMARY: 2 errors from 2contexts (suppressed: 0 from 0)
錯誤資訊從“Possible data race during write of size 4 at 0x804A020 by thread #1
”開始,這條資訊你可以看到競態訪問的地址和大小,還有調用的堆棧資訊。
第二條呼叫堆疊從“This conflicts with a previous write of size 4 by thread #2
”開始,這表明這裡與第一個呼叫堆疊有競態。
一旦你找到兩個呼叫堆疊,如何找到競態的根源:
首先通過每個呼叫堆疊檢查代碼,它們都會顯示對同一個位置或者變數的訪問。
現在考慮如何改正來使得多線程訪問安全:
1.使用鎖或者其他的同步機制,保證同一時間只有獨立的訪問。
2.使用條件變數等方法,確定多次訪問的次序。
本文介紹了valgrind的體繫結構,並重點介紹了其應用最廣泛的工具:memcheck和helgrind。闡述了memcheck和helgrind的基本使用方法。在項目中儘早的發現記憶體問題和多進程同步問題,能夠極大地提高開發效率,valgrind就是能夠協助你實現這一目標的出色工具。
轉自:http://blog.csdn.net/dndxhej/article/details/7855520