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-pointertoo as you will get better results.
Address Sanitizer
TheAddress Sanitizer(asan for short) is very similar to Valgrind’s default “memcheck” tool. use-fsanitize=address to enable it.
Thread Sanitizer
TheThread 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
TheUndefined 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
2valgrind
如果系統可以安裝 valgrind,則推薦使用valgrind,畢竟這個不影響代碼,而且可以調試 java 及 golang等很多程序,直接調試程序。
不過,這兩個工具要在打印中顯示 問題行號,則需要使用 -g -O0 來編譯。也可以加上 -g3。
Helgrind 相當于 Thread Sanitizer。
不知道valgrind有沒有Undefined Behaviour Sanitizer 這個功能。經過查證,valgrind好像還沒有類似工具。
靜態檢查工具:
static analyzers like clang-tidy, cppcheck, PVS
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
微信公眾號: 共鳴圈
歡迎討論,郵件: 924948$qq.com 請把$改成@
QQ群:263132197
QQ:924948
良辰美景補天漏,風雨雷電洗地塵
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
總結
以上是生活随笔為你收集整理的linux 下内存检查工具 valgrind 及 sanitizer 编译选项及静态检查工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奇咔咔机器人教育_奇咔咔机器人教育,提升
- 下一篇: centos7创建asm磁盘_Oracl