1. 程式人生 > >Centos下Valgrind使用與安裝

Centos下Valgrind使用與安裝

第一步:

獲取Valgrind 包 可以先使用瀏覽器訪問http://valgrind.org/downloads/檢視當前版本

wget http://valgrind.org/downloads/valgrind-3.10.1.tar.bz2

第二步:

將下載的檔案解壓

tar -jxvf valgrind-3.10.1.tar.bz2

第三步:

安裝和配置
./autogen.sh
./configure

make; make install

 

Linux下利用Valgrind工具進行記憶體洩露檢測和效能分析

 

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能在這裡幫上大忙。
例如:

  1. #include <stdlib.h>  
  2. #include <malloc.h>  
  3. #include <string.h>  
  4.   
  5. void test()  
  6. {  
  7.     int *ptr = malloc(sizeof(int)*10);  
  8.   
  9.     ptr[10] = 7; // 記憶體越界  
  10.   
  11.     memcpy(ptr +1, ptr, 5); // 踩記憶體  
  12.   
  13.   
  14.     free(ptr);   
  15.     free(ptr);// 重複釋放  
  16.   
  17.     int *p1;  
  18.     *p1 = 1; // 非法指標  
  19. }  
  20.   
  21. int main(void)  
  22. {  
  23.     test();  
  24.     return 0;  
  25. }  

將程式編譯生成可執行檔案後執行:valgrind --leak-check=full ./程式名

 

輸出結果如下:

  1. ==4832== Memcheck, a memory error detector  
  2. ==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.  
  3. ==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info  
  4. ==4832== Command: ./tmp  
  5. ==4832==   
  6. ==4832== Invalid write of size 4      // 記憶體越界  
  7. ==4832==    at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  8. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  9. ==4832==  Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd  
  10. ==4832==    at 0x4026864: malloc (vg_replace_malloc.c:236)  
  11. ==4832==    by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  12. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  13. ==4832==   
  14. ==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩記憶體  
  15. ==4832==    at 0x4027BD6: memcpy (mc_replace_strmem.c:635)  
  16. ==4832==    by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  17. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  18. ==4832==   
  19. ==4832== Invalid free() / delete / delete[] // 重複釋放  
  20. ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
  21. ==4832==    by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  22. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  23. ==4832==  Address 0x41a6028 is 0 bytes inside a block of size 40 free'd  
  24. ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
  25. ==4832==    by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  26. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  27. ==4832==   
  28. ==4832== Use of uninitialised value of size 4 // 非法指標  
  29. ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  30. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  31. ==4832==   
  32. ==4832==   
  33. ==4832== Process terminating with default action of signal 11 (SIGSEGV) //由於非法指標賦值導致的程式崩潰  
  34. ==4832==  Bad permissions for mapped region at address 0x419FFF4  
  35. ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  36. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  37. ==4832==   
  38. ==4832== HEAP SUMMARY:  
  39. ==4832==     in use at exit: 0 bytes in 0 blocks  
  40. ==4832==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated  
  41. ==4832==   
  42. ==4832== All heap blocks were freed -- no leaks are possible  
  43. ==4832==   
  44. ==4832== For counts of detected and suppressed errors, rerun with: -v  
  45. ==4832== Use --track-origins=yes to see where uninitialised values come from  
  46. ==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)  
  47. Segmentation fault  

從valgrind的檢測輸出結果看,這幾個錯誤都找了出來。