1. 程式人生 > >【linux】Valgrind工具集詳解(五):命令列詳解

【linux】Valgrind工具集詳解(五):命令列詳解

一、使用方法

usage: valgrind [options] prog-and-args
使用方法:valgrind [引數選項] 程式和引數

二、選擇工具

tool-selection option, with default in [ ]:
工具選擇選項,預設值在[]中:
–tool= use the Valgrind tool named [memcheck]
name取值如下:
1、memcheck:檢查程式中的記憶體問題,如洩漏、越界、非法指標等。
2、callgrind:檢測程式程式碼覆蓋,以及分析程式效能。
3、cachegrind:分析CPU的cache命中率、丟失率,用於進行程式碼優化。
4、helgrind:用於檢查多執行緒程式的競態條件。
5、massif:堆疊分析器,指示程式中使用了多少堆記憶體等資訊。
6、lackey:Lackey是小型工具,很少用到
7、nulgrind:Nulgrind只是為開發者展示如何建立一個工具

三、工具集基本選項

basic user options for all Valgrind tools, with defaults in [ ]:
針對Valgrind工具集的基本選項,預設值在[]中:
-h --help show this message
–help-debug show this message, plus debugging options
–version show version
-q --quiet run silently; only print error msgs //安靜的執行,只打印錯誤
-v --verbose be more verbose – show misc extra info//更加冗長。提供有關程式各個方面的額外資訊,例如:載入的共享物件,使用的抑制,檢測和執行引擎的進度以及有關異常行為的警告。重複該選項會增加詳細程度。
–trace-children=no|yes [no] //如果是yes,被調程式若用exec開啟了一個子程序,那valgrind將會追蹤此子程序的執行情況。
預設是no,但無論如何,valgrind都還是追蹤fork產生的子程序。
–trace-children-skip=patt1,patt2,… //在上面選項設為yes後,這個選項則標識了哪些子程序是不要被追蹤的,這些子程序(名字)由patt1,patt2…來決定,pattn可包含”?””*”等萬用字元。注意,valgrind將停止追蹤pattn所指程序下可能產生的所有子程序。
–trace-children-skip-by-arg=patt1,patt2,… //與–trace-childrn-skip選項的不同之處是,跳過的子程序是由給子程序的引數patt1,patt2…來決定,而非程式名字。
–child-silent-after-fork=no|yes omit child output between fork & exec? [no] //若是yes,則不顯示由fork呼叫產生的子程序產生的任何除錯或log資訊。當除錯資訊以XML格式輸出時(–xml=yes),強烈建議開啟此選項。預設是no。
–vgdb=no|yes|full 啟用gdb除錯? [yes]//若為yes或full,valgrind允許在其上執行的程式,用GDB去除錯它(開啟gdbsever)。預設是yes。full比較慢,但是提供了精確的觀察點/步驟
–vgdb-error= invoke gdbserver after errors [999999999]
to get started quickly, use --vgdb-error=0
and follow the on-screen directions
//在開啟了gdbsever後,有用。報錯工具在等待有number個錯誤報出後,會凍結程式並等你將它連上GDB。因此,當number=0時,在你的程式執行前,gdbserver就開始運行了。典型應用場景是,在執行前插入GDB斷點,還有使用那些不報錯的工具的情況,如Massif。預設是999999999。
–vgdb-stop-at=event1,event2,… invoke gdbserver for given events [none]
where event is one of:
startup exit valgrindabexit all none
–track-fds=no|yes track open file descriptors? [no] //若是yes,在程式退出時,將列印一系列的程式開啟的檔案描述符的資訊。包括檔案是在哪開啟的,檔名字或socket細節等。預設是no。
–time-stamp=no|yes add timestamps to log messages? [no] //若 yes,每條資訊前將掛個時間資訊,指示自程式開始,過去的時間量。
–log-fd= log messages to file descriptor [2=stderr] //將vaolgrind的輸出資訊輸向由number這個檔案描述符指定的檔案,預設是2,即stderr。注意,這可能與使用者自己向stderr輸出的東西相互交織。
–log-file= log messages to //將資訊輸向右filename指定的檔案。若filename是空,則會引發終止。filename中可有三種格式資訊;1,%p將被替換為當前程序的ID。當—trace-children=yes,而沒用%p時,所有程序的資訊都輸向同一個檔案,會比較混亂,資訊也可能不全,最好檔名中包含%p。2,%q{FOO}被環境變數FOO的值代替,若FOO的內容奇怪的話也可能引發異常。一般不用這種格式,除了極少情況,如基於MPI(一種並行程式開發庫)的程式。若用了此種格式,FOO不能為空,否則也引發異常。一些shell裡面,”{””}”可能需要反斜槓轉義。3,%%被代替為%,%不能後接任何其他字元,否則會引發異常。
–log-socket=ipaddr:port log messages to socket ipaddr:port //將資訊輸向指定的IP地址。若省了port-number,則預設用1500埠號。若這個IP地址無法接受資訊,資訊將被寫會到stderr。

四、報錯相關的錯誤

user options for Valgrind tools that report errors:
下面是與報錯相關的選項。這些選項適用於所有能報錯的工具,如memcheck,而cachegrind就用不了。
–xml=yes emit error output in XML (some tools only) //若為yes,啟用後,輸出的重要部分(例如工具錯誤訊息)將採用XML格式而不是純文字格式。不重要的資訊(非錯誤資訊)被列印在純文字中。XML輸出目標由–xml-fd或–xml-file或—xml-socket指定,而純文字資訊輸出目標則由前面的—log-fd或—log-file或—log-socket指定。輸出格式由docs/internals/xml-output-protocol4.txt設定。
–xml-fd= XML output to file descriptor //將XML資訊輸出到由檔案描述符number指定的檔案中,必須要–xml=yes。
–xml-file= XML output to //與—log-file類似。也必須要—xml=yes。
–xml-socket=ipaddr:port XML output to socket ipaddr:port //與—log-socket類似,也必須要—xml=yes。
–xml-user-comment=STR copy STR verbatim into XML output //在輸出的XML檔案開頭,加的註釋資訊,沒有—xml=yes的話,將被忽略。
–demangle=no|yes automatically demangle C++ names? [yes] //這個選項開啟的話,會試圖還原目的碼中的C++符號名,使其與原始碼中的相關符號名字儘量一致。預設是yes。需要注意的是,抑制檔案中提到的函式名稱應該是錯誤的形式。在搜尋適用的抑制時,Valgrind不會對函式名稱進行解碼,因為否則會使抑制檔案內容依賴於Valgrind的demangling機制的狀態,並且還會降低抑制匹配的速度。
–num-callers= show callers in stack traces [12] //定義了在堆疊追蹤過程中顯示的最大巢狀呼叫數。注意,valgrind只顯示四層巢狀呼叫的錯誤資訊,故這個選項不影響最終報道的錯誤資訊量。Number最大值是50,預設值是12。
–error-limit=no|yes stop showing new errors if too many? [yes] //若為yes,則當報道出的錯誤總數超過10000000或有1000個不同型別的,則停止報錯。這麼多錯誤的程式也就沒必要再除錯了,預設是yes。
–error-exitcode= exit code to return if errors found [0=disable] //設定在發現任何錯誤資訊時的valgrind的返回碼。
預設是0,這樣返回碼就是被調程式的返回碼。若設為非0值,則此值將代替預設的返回碼。如果Valgrind在執行中報告任何錯誤,則指定要返回的備用退出程式碼。設定為預設值(零)時,Valgrind的返回值將始終是正在模擬的過程的返回值。設定為非零值時,如果Valgrind檢測到任何錯誤,則返回該值。這對於將Valgrind用作自動化測試套件的一部分非常有用,因為只需檢查返回程式碼,就可以輕鬆檢測Valgrind報告錯誤的測試用例。
–exit-on-first-error=<yes|no> [default: no]
如果啟用此選項,Valgrind將在第一個錯誤時退出。必須使用–error-exitcode選項定義非零退出值 。如果您正在執行迴歸測試或使用其他一些自動測試機器,則非常有用
–show-below-main=no|yes continue stack traces below main() [no] //預設是no,追蹤堆疊錯誤時,不顯示在那些在main函式下一層那些函式呼叫的錯誤資訊。
–default-suppressions=yes|no load default suppressions [yes] //抑制錯誤
–suppressions= suppress errors described in //從其他檔案新增抑制
–gen-suppressions=no|yes|all print suppressions for errors? [no] //若選擇yes,則每顯示一條error,valgrind就暫停,並列印一行:----Print suppression ?—[Return/N/n?Y/y/C/c]—(y=yes,n=no,c=cancle)這條提示資訊與下面的–db-attach選項相同,選y,則列印針對這個error的suppressions。若該選項為all,則列印每個error的suppression,不在詢問了。此選項對於C ++程式特別有用,因為它根據需要打印出帶有錯位名稱的抑制。
–db-attach=no|yes start debugger when errors detected? [no] 注意:棄用功能//若選yes,則每顯示一條error,valgrind就暫停,並列印一行:----Attach to debugger ?—[RegurnN/n/Y/y/C/c]—。若選y,則在此處啟動偵錯程式,除錯完了要退出偵錯程式,valgrind才可繼續。若你用的是GDB,則選項–vgdb=yes或ful會使得偵錯程式功能更強(它啟動了valgrind內部的gdbsever,幾乎模擬了 GDB的全部功能)。
–db-command= command to start debugger [/usr/bin/gdb -nw %f %p] //開啟–db-attach選項時,實際用的命令,預設是"gdb -nw %f %p",其中%f是被除錯的程式名,%p是被調程序的ID,預設的偵錯程式是valgrind安裝時它發現的,一般是/usr/bin/gdb。command應該放在雙引號內。
–input-fd= file descriptor for input [0=stdin] //當–db-attach=yes或–gen-suppressions=yes時,valgrind在發現錯誤時會等待鍵盤輸入即便進行下一步操作,即number預設是0(stdin),通過修改number,可以使得valgrind讀取指定檔案來執行下一步操作,在關閉了stdin時,這個選項有用。
–dsymutil=no|yes run dsymutil on Mac OS X when helpful? [no] //這個選項只在MacOS上跑valgrind才有用。
–max-stackframe= assume stack switch for SP changes larger than bytes [2000000] //該選項規定了被調程式能使用的最大棧幀空間。預設是2000000。這個選項一般在valgrind的除錯輸出建議你用時,再用。其實若被valgrind建議了,說明你的程式有問題,最好別在棧上分配太大的資料結構,大的資料最好在堆上分配。
–main-stacksize= set size of main thread’s stack (in bytes) [min(max(current ‘ulimit’ value,1MB),16MB)] //規定了主執行緒的棧大小。預設情況用ulimit值,一般是16MB或低一點。一般用8~16M能滿足絕大部分應用程式的需求。Linux上可最大申請2GB。若valgrid無法分配這麼多空間,便會終止。這個選項隻影響initial thread,對其他執行緒棧無影響。

五、與malloc()函式有關的選項

user options for Valgrind tools that replace malloc:
與malloc()函式有關的選項:
–alignment= set minimum alignment of heap allocations [16]
–redzone-size= set minimum size of redzones added before/after
heap blocks (in bytes). [16]

六、不常見選項

uncommon user options for all Valgrind tools:
不常見選項
–fullpath-after= (with nothing after the ‘=’)
show full source paths in call stacks
–fullpath-after=string like --fullpath-after=, but only show the
part of the path after ‘string’. Allows removal
of path prefixes. Use this flag multiple times
to specify a set of prefixes to remove.
–extra-debuginfo-path=path absolute path to search for additional
debug symbols, in addition to existing default
well known search paths.
–debuginfo-server=ipaddr:port also query this server
(valgrind-di-server) for debug symbols
–allow-mismatched-debuginfo=no|yes [no]
for the above two flags only, accept debuginfo
objects that don’t “match” the main object
–smc-check=none|stack|all|all-non-file [stack]
checks for self-modifying code: none, only for
code found in stacks, for all code, or for all
code except that from file-backed mappings
–read-inline-info=yes|no read debug info about inlined function calls
and use it to do better stack traces. [yes]
on Linux/Android for Memcheck/Helgrind/DRD
only. [no] for all other tools and platforms.
–read-var-info=yes|no read debug info on stack and global variables
and use it to print better error messages in
tools that make use of it (Memcheck, Helgrind,
DRD) [no]
–vgdb-poll= gdbserver poll max every basic blocks [5000]
–vgdb-shadow-registers=no|yes let gdb see the shadow registers [no]
–vgdb-prefix= prefix for vgdb FIFOs [/tmp/vgdb-pipe]
–run-libc-freeres=no|yes free up glibc memory at exit on Linux? [yes]
–sim-hints=hint1,hint2,… activate unusual sim behaviours [none]
where hint is one of:
lax-ioctls fuse-compatible enable-outer
no-inner-prefix no-nptl-pthread-stackcache none
–fair-sched=no|yes|try schedule threads fairly on multicore systems [no]
–kernel-variant=variant1,variant2,…
handle non-standard kernel variants [none]
where variant is one of:
bproc android-no-hw-tls
android-gpu-sgx5xx android-gpu-adreno3xx none
–merge-recursive-frames= merge frames between identical
program counters in max frames) [0]
–num-transtab-sectors= size of translated code cache [16]
more sectors may increase performance, but use more memory.
–aspace-minaddr=0xPP avoid mapping memory below 0xPP [guessed]
–show-emwarns=no|yes show warnings about emulation limits? [no]
–require-text-symbol=:sonamepattern:symbolpattern abort run if the
stated shared object doesn’t have the stated
text symbol. Patterns can contain ? and *.
–soname-synonyms=syn1=pattern1,syn2=pattern2,… synonym soname
specify patterns for function wrapping or replacement.
To use a non-libc malloc library that is
in the main exe: --soname-synonyms=somalloc=NONE
in libxyzzy.so: --soname-synonyms=somalloc=libxyzzy.so
–sigill-diagnostics=yes|no warn about illegal instructions? [yes]
–unw-stack-scan-thresh= Enable stack-scan unwind if fewer
than good frames found [0, meaning “disabled”]
NOTE: stack scanning is only available on arm-linux.
–unw-stack-scan-frames= Max number of frames that can be
recovered by stack scanning [5]

六、記憶體洩漏

user options for Memcheck:
–leak-check=no|summary|full search for memory leaks at exit? [summary] //在退出時搜尋記憶體洩漏
–leak-resolution=low|med|high differentiation of leak stack traces [high]
//在做記憶體洩漏檢查時,確定memcheck將怎麼樣考慮不同的棧是否是相同的情況。當設定為low時,只需要前兩層棧匹配就認為是相同的情況;當設定為med,必須要四層棧匹配,當設定為high時,所有層次的棧都必須匹配。
對於hardcore記憶體洩漏檢查,你很可能需要使用–leak-resolution=high和–num-callers=40或者更大的數字。注意這將產生巨量的資訊,這就是為什麼預設選項是四個呼叫者匹配和低解析度的匹配。注意–leak-resolution= 設定並不影響memcheck查詢記憶體洩漏的能力。它只是改變了結果如何輸出。
–show-leak-kinds=kind1,kind2,… which leak kinds to show? [definite,possible]
–errors-for-leak-kinds=kind1,kind2,… which leak kinds are errors? [definite,possible]
where kind is one of:
definite indirect possible reachable all none
–leak-check-heuristics=heur1,heur2,… which heuristics to use for
improving leak search false positive [none]
where heur is one of:
stdstring length64 newarray multipleinheritance all none
–show-reachable=yes same as --show-leak-kinds=all
–show-reachable=no --show-possibly-lost=yes same as --show-leak-kinds=definite,possible
–show-reachable=no --show-possibly-lost=no same as --show-leak-kinds=definite
–undef-value-errors=no|yes check for undefined value errors [yes] //檢查未定義的值錯誤
–track-origins=no|yes show origins of undefined values? [no] //顯示未定義值的起源
–partial-loads-ok=no|yes too hard to explain here; see manual [no]
–freelist-vol= volume of freed blocks queue [20000000] //釋放塊佇列的容量
–freelist-big-blocks= releases first blocks with size>= [1000000] //釋放第一個具有大小的塊
–workaround-gcc296-bugs=no|yes self explanatory [no]
–ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] assume given addresses are OK //假設給定的地址是可以的
–malloc-fill= fill malloc’d areas with given value //用給定的值填充malloc區域
–free-fill= fill free’d areas with given value //用給定的值填充空閒區域
–keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none
stack trace(s) to keep for malloc’d/free’d areas [alloc-then-free] //為malloc’d/free’區域保留堆疊跟蹤
–show-mismatched-frees=no|yes show frees that don’t match the allocator? [yes] //顯示不匹配分配器的釋放

Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc

Memcheck is Copyright © 2002-2013, and GNU GPL’d, by Julian Seward et al.
Valgrind is Copyright © 2000-2013, and GNU GPL’d, by Julian Seward et al.
LibVEX is Copyright © 2004-2013, and GNU GPL’d, by OpenWorks LLP et al.

Bug reports, feedback, admiration, abuse, etc, to: www.valgrind.org.