歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

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的檢測輸出結果看,這幾個錯誤都找了出來。

2.Callgrind

和gprof類似的分析工具,但它對程序的運行觀察更是入微,能給我們提供更多的信息。和gprof不同,它不需要在編譯源代碼時附加特殊選項,但加上調試選項是推薦的。Callgrind收集程序運行時的一些數據,建立函數調用關系圖,還可以有選擇地進行cache模擬。在運行結束時,它會把分析數據寫入一個文件。callgrind_annotate可以把這個文件的內容轉化成可讀的形式。

生成可視化的圖形需要下載gprof2dot:http://http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py

這是個python腳本,把它下載之後修改其權限chmod +7 gprof2dot.py ,並把這個腳本添加到$PATH路徑中的任一文件夾下,我是將它放到了/usr/bin目錄下,這樣就可以直接在終端下執行gprof2dot.py了。

Callgrind可以生成程序性能分析的圖形,首先來說說程序性能分析的工具吧,通常可以使用gnu自帶的gprof,它的使用方法是:在編譯程序時添加-pg參數,例如:

  1. #include <stdio.h>  
  2. #include <malloc.h>   
  3. void test()  
  4. {  
  5.     sleep(1);  
  6. }  
  7. void f()  
  8. {  
  9.     int i;  
  10.     for( i = 0; i < 5; i ++)  
  11.         test();  
  12. }  
  13. int main()  
  14. {  
  15.     f();  
  16.     printf("process is over!\n");  
  17.     return 0;  
  18. }  
首先執行 gcc -pg -o tmp tmp.c,然後運行該程序./tmp,程序運行完成後會在當前目錄下生成gmon.out文件(這個文件gprof在分析程序時需要),
再執行gprof ./tmp | gprof2dot.py |dot -Tpng -o report.png,打開report.png結果:


顯示test被調用了5次,程序中耗時所占百分比最多的是test函數。

再來看 Callgrind的生成調用圖過程吧,執行:valgrind --tool=callgrind ./tmp,執行完成後在目錄下生成"callgrind.out.XXX"的文件這是分析文件,可以直接利用:callgrind_annotate callgrind.out.XXX 打印結果,也可以使用:gprof2dot.py -f callgrind callgrind.out.XXX |dot -Tpng -o report.png 來生成圖形化結果:


它生成的結果非常詳細,甚至連函數入口,及庫函數調用都標識出來了。

Copyright © Linux教程網 All Rights Reserved