linux 下內存檢查工具 valgrind 及 sanitizer 編譯選項及靜態檢查工具


要記住,這兩個工具都是動態檢查工具,也就是程序運行時覆蓋到的代碼才會被檢查,未覆蓋的代碼是不會檢查的。

valgrind是一個模擬程序運行環境並記錄程序非法內存使用的一個程序工具。可能慢一些,因為是外部模擬。Valgrind慢,像調試器一樣,它無法擴展。如果您要處理大型數據集,則可能會花費很長時間,人們經常不理會“執行時間”,如果您處理小問題則可以不關心,但是性能是生活質量的基本要素,我認為您不應或不能在模擬的生產環境中運行valgrind。

sanitizer是google開發的一個編譯工具集(編譯選項),最初為llvm開發,后來gcc(g++) 4.9版及更新版也集成了這個編譯工具。可能快一些,因為直接編譯進去了。sanitizer更精細精確,因為有很多選項可以開關,但正因為是編譯選項,因此很多時候環境無法讓你使用sanitizer,最主要是編譯器不允許。

 

兩個都好用。

1  sanitizer

如果系統里面無法直接安裝valgrind,則可以考慮使用 sanitizer 來進行調試。為了更好的調試,系統里可能需要安裝 libasan 庫(特別是使用 Address Sanitizer的時候)。

when you build with the flag -fsanitize=address, if you are using C++ then it is worth setting -fno-omit-frame-pointer too as you will get better results.

Address Sanitizer

The Address Sanitizer (asan for short) is very similar to Valgrind’s default “memcheck” tool.  use -fsanitize=address to enable it.

Thread Sanitizer

The Thread Sanitizer (tsan for short) is designed to find race conditions in your code, very similar to the “Helgrind” tool in Valgrind. It has a typical slowdown of 5x – 15x and a memory overhead of 5x – 10x. Very much like the Address Sanitizer you compile it in using a simple flag, -fsanitize=thread. You should not try to use this with the asan flag. This won’t compile.

Undefined Behaviour Sanitizer

The Undefined Behaviour Sanitizer (ubsan for short) does exactly what it says on the tin, it detects undefined behaviour usage in your application that can lead to bugs and portability issues and it is extremely fast. There are a whole bunch of different options to turn on here but the ones I typically use are -fsanitize=undefined -fsanitize=nullability.

不知道valgrind有沒有 Undefined Behaviour Sanitizer 這個功能。經過查證,valgrind好像還沒有類似工具。

參考網址:

https://github.com/google/sanitizers

https://clang.llvm.org/docs/AddressSanitizer.html

 

2  valgrind

如果系統可以安裝 valgrind,則推薦使用valgrind,畢竟這個不影響代碼,而且可以調試 java 及 golang等很多程序,直接調試程序。

不過,這兩個工具要在打印中顯示 問題行號,則需要使用 -g -O0 來編譯。也可以加上 -g3。

Helgrind 相當於 Thread Sanitizer。

不知道valgrind有沒有 Undefined Behaviour Sanitizer 這個功能。經過查證,valgrind好像還沒有類似工具。

 

 

靜態檢查工具:

static analyzers like clang-tidy, cppcheck, PVS

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM