使用valgrind分析Linux程序內存泄漏

 

1       安裝... 1

2       參數說明... 1

3       使用問題... 2

 

1         安裝

直接使用命令行安裝

sudo apt-get install valgrind

源代碼下載安裝

下載最新版本:http://valgrind.org/downloads/current.html#current
安裝命令:

tar -jxvf valgrind-3.15.0.tar.bz2.tar.bz2

cd valgrind-3.15.0     

sudo ./configure   #配置

#如果更換其他安裝目錄,則使用如下命令

#./configure --prefix=/opt/valgrind #配置安裝目錄

sudo make                  //編譯安裝

sudo make install

 

2         參數說明

用法: valgrind [options] prog-and-args [options]:

常用選項,適用於所有Valgrind工具:

-tool= 最常用的選項。運行 valgrind中名爲toolname的工具。默認memcheck。

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= 輸出LOG到描述符文件 [2=stderr]

-log-file= 將輸出的信息寫入到filename.PID的文件裏,PID是運行程序的進行ID

-log-file-exactly= 輸出LOG信息到 file

-log-file-qualifier= 取得環境變量的值來做爲輸出信息的文件名。 [none]

-log-socket=ipaddr:port 輸出LOG到socket ,ipaddr:port

 

四、LOG信息輸出

xml=yes 將信息以xml格式輸出,只有memcheck可用

-num-callers= show callers in stack traces [12]

-error-limit=no|yes 如果太多錯誤,則停止顯示新錯誤? [yes]

-error-exitcode= 如果發現錯誤則返回錯誤代碼 [0=disable]

-db-attach=no|yes 當出現錯誤,valgrind會自動啓動調試器gdb。[no]

-db-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]

3         使用問題

(1)安裝好valgrind之後,用QtCreator去分析內存泄漏

 

 

報錯:-1: error: Memcheck: Error occurred parsing Valgrind output: 遠端主機關閉了這個連接

 

 

(2)打開終端用命令行執行也會報錯

valgrind --leak-check=full --show-leak-kinds=all --log-file=output.txt  ./testprogrom

還是會報錯

==3046174== Memcheck, a memory error detector

==3046174== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.

==3046174== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info

==3046174== Command: ./AnalysisCenter

==3046174== Parent PID: 3044306

==3046174==

 

valgrind:  Fatal error at startup: a function redirection

valgrind:  which is mandatory for this platform-tool combination

valgrind:  cannot be set up.  Details of the redirection are:

valgrind: 

valgrind:  A must-be-redirected function

valgrind:  whose name matches the pattern:      strlen

valgrind:  in an object with soname matching:   ld-linux-x86-64.so.2

valgrind:  was not found whilst processing

valgrind:  symbols from the object with soname: ld-linux-x86-64.so.2

valgrind: 

valgrind:  Possible fixes: (1, short term): install glibc's debuginfo

valgrind:  package on this machine.  (2, longer term): ask the packagers

valgrind:  for your Linux distribution to please in future ship a non-

valgrind:  stripped ld.so (or whatever the dynamic linker .so is called)

valgrind:  that exports the above-named function using the standard

valgrind:  calling conventions for this platform.  The package you need

valgrind:  to install for fix (1) is called

valgrind: 

valgrind:    On Debian, Ubuntu:                 libc6-dbg

valgrind:    On SuSE, openSuSE, Fedora, RHEL:   glibc-debuginfo

valgrind: 

valgrind:  Note that if you are debugging a 32 bit process on a

valgrind:  64 bit system, you will need a corresponding 32 bit debuginfo

valgrind:  package (e.g. libc6-dbg:i386).

valgrind: 

valgrind:  Cannot continue -- exiting now.  Sorry.

 

分析說是少了libc6-dbg,需要安裝libc6-dbg就可以。

(3)使用命令安裝libc6-dbg

sudo apt-get install libc6-dbg

正在讀取軟件包列表... 完成

正在分析軟件包的依賴關係樹      

正在讀取狀態信息... 完成      

libc6-dbg 已經是最新版 (2.23-0ubuntu11.2)。

您也許需要運行“apt --fix-broken install”來修正上面的錯誤。

下列軟件包有未滿足的依賴關係:

 libc6-dbg : 依賴: libc6 (= 2.23-0ubuntu11.2) 但是 2.31-0kylin9.1k20.6 正要被安裝

E: 有未能滿足的依賴關係。請嘗試不指明軟件包的名字來運行“apt --fix-broken install”(也可以指定一個解決辦法)。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章