Valgrind,內存調試工具

Valgrind是一款用於內存調試、內存泄漏檢測以及性能分析的軟件開發工具

官網:http://valgrind.org/ 用戶開發手冊地址:http://valgrind.org/docs/manual/manual.htmlhtml

下載安裝步驟:

① git clone git@github.com:meihao1203/Valgrind<br> 或:git clone https://github.com/meihao1203/Valgrind<br> ② tar -xvzf valgrind-3.13.0.tar.gz<br> ③ cd valgrind-3.13.0<br> ④ make<br> ⑤ sudo make install<br>ios

查看是否安裝成功:

valgrind --version<br> 顯示 valgrind-3.13.0 即爲成功git

查看幫助:

valgrind --helpgithub

Demo小例子
1  ///
2  /// @file    uninit.cpp
3  /// @author  meihao1203(meihao19931203@outlook.com)
4  /// @date    2018-07-12 19:59:17
5  ///
6 
7  #include<iostream>
8  using namespace std;
9  int main(int argc,char** argv) 
10 {
11	int* arr = new int[5];  //沒有釋放,內存泄露
12	return 0;
13 }
編譯:

g++ -g uninit.cpp緩存

使用valgrind調試,顯示出詳細的內存泄露信息,以及錯誤發生在哪一行:

valgrind --tool=memcheck --leak-check=full ./a.out多線程

//==18626== Memcheck, a memory error detector
//==18626== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
//==18626== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
//==18626== Command: ./a.out
//==18626== 
//==18626== 
//==18626== HEAP SUMMARY:
//==18626==     in use at exit: 72,724 bytes in 2 blocks
//==18626==   total heap usage: 2 allocs, 0 frees, 72,724 bytes allocated
//==18626== 
//==18626== 20 bytes in 1 blocks are definitely lost in loss record 1 of 2
//==18626==    at 0x4C2E8BB: operator new[](unsigned long) (vg_replace_malloc.c:423)
//==18626==    by 0x400717: main (uninit.cpp:11)
//==18626== 
//==18626== LEAK SUMMARY:
//==18626==    definitely lost: 20 bytes in 1 blocks
//==18626==    indirectly lost: 0 bytes in 0 blocks
//==18626==      possibly lost: 0 bytes in 0 blocks
//==18626==    still reachable: 72,704 bytes in 1 blocks
//==18626==         suppressed: 0 bytes in 0 blocks
//==18626== Reachable blocks (those to which a pointer was found) are not shown.
//==18626== To see them, rerun with: --leak-check=full --show-leak-kinds=all
//==18626== 
//==18626== For counts of detected and suppressed errors, rerun with: -v
//==18626== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
valgrind --help對應的中文:
用法: valgrind [options] prog-and-args
[options]: 經常使用選項,適用於全部Valgrind工具

    -tool=<name> 最經常使用的選項。運行 valgrind中名爲toolname的工具。默認memcheck。
        memcheck ------> 這是valgrind應用最普遍的工具,一個重量級的內存檢查器,可以發現開發中絕大多數內存錯誤使用狀況,好比:使用未初始化的內存,使用已經釋放了的內存,內存訪問越界等。
        callgrind ------> 它主要用來檢查程序中函數調用過程當中出現的問題。
        cachegrind ------> 它主要用來檢查程序中緩存使用出現的問題。
        helgrind ------> 它主要用來檢查多線程程序中出現的競爭問題。
        massif ------> 它主要用來檢查程序中堆棧使用中出現的問題。
        extension ------> 能夠利用core提供的功能,本身編寫特定的內存調試工具

    -h –help 顯示幫助信息。
    -version 顯示valgrind內核的版本,每一個工具都有各自的版本。
    -q –quiet 安靜地運行,只打印錯誤信息。
    -v –verbose 更詳細的信息, 增長錯誤數統計。
    -trace-children=no|yes 跟蹤子線程? [no]
    -track-fds=no|yes 跟蹤打開的文件描述?[no]
    -time-stamp=no|yes 增長時間戳到LOG信息? [no]
    -log-fd=<number> 輸出LOG到描述符文件 [2=stderr]
    -log-file=<file> 將輸出的信息寫入到filename.PID的文件裏,PID是運行程序的進行ID
    -log-file-exactly=<file> 輸出LOG信息到 file
    -log-file-qualifier=<VAR> 取得環境變量的值來作爲輸出信息的文件名。 [none]
    -log-socket=ipaddr:port 輸出LOG到socket ,ipaddr:port

LOG信息輸出

    -xml=yes 將信息以xml格式輸出,只有memcheck可用
    -num-callers=<number> show <number> callers in stack traces [12]
    -error-limit=no|yes 若是太多錯誤,則中止顯示新錯誤? [yes]
    -error-exitcode=<number> 若是發現錯誤則返回錯誤代碼 [0=disable]
    -db-attach=no|yes 當出現錯誤,valgrind會自動啓動調試器gdb。[no]
    -db-command=<command> 啓動調試器的命令行選項[gdb -nw %f %p]

適用於Memcheck工具的相關選項:

    -leak-check=no|summary|full 要求對leak給出詳細信息? [summary]
    -leak-resolution=low|med|high how much bt merging in leak check [low]
    -show-reachable=no|yes show reachable blocks in leak check? [no]
相關文章
相關標籤/搜索