valgrind 自不必說
1. Address Sanitize
很好有,只需要在gcc編譯的時候,加上選項 -fsanitize=address
它的工程:https://github.com/google/sanitizers/wiki/AddressSanitizer
我的測試例子: https://github.com/tony-caotong/knickknack/tree/master/test/sanitize-address
運行的時候,遇見內存問題會直接退出,包括前邊越界和后邊越界,如下:
https://wizardforcel.gitbooks.io/100-gcc-tips/content/address-sanitizer.html
需要主要的是,在CentOS7里面,需要單獨安裝依賴庫
[root@dpdk sanitize-address]# gcc --version gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@dpdk sanitize-address]# yum search asan Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.bit.edu.cn * epel: mirrors.ustc.edu.cn * extras: mirrors.aliyun.com * rpmfusion-free-updates: mirrors.ustc.edu.cn * rpmfusion-nonfree-updates: mirrors.ustc.edu.cn * updates: mirrors.shuosc.org ===================================================================================== N/S matched: asan ===================================================================================== libasan.i686 : The Address Sanitizer runtime library libasan.x86_64 : The Address Sanitizer runtime library libasan-static.i686 : The Address Sanitizer static library libasan-static.x86_64 : The Address Sanitizer static library oflb-asana-math-fonts.noarch : An OpenType font with a MATH table Name and summary matches only, use "search all" for everything. [root@dpdk sanitize-address]# yum install libasan
使用gdb在報錯的地方,打斷點:
https://github.com/google/sanitizers/wiki/AddressSanitizerAndDebugger
也就是
(gdb)break __sanitizer::Die
但是, 在CentOS7里竟然不好使,因為這個庫竟然沒有symbol,我相信,有symbol的庫定能成功break
[root@dpdk anthropoid]# nm /lib64/libasan.so.0.0.0 nm: /lib64/libasan.so.0.0.0: no symbols [root@dpdk anthropoid]#
comment @2018-01-26 當時的我還真是太傻太天真呢,還你相信。。。 正確的解釋,請看:
[daily] 在CentOS7中使用 sanitizer-address 發現內存問題 / CentOS7使用SCLo軟件源安裝devtoolset軟件
2. mprotect
http://man7.org/linux/man-pages/man2/mprotect.2.html
linux系統API,可以在內存的前后扇區開始處,打上標簽。當該標簽被讀寫時,系統會給出提示。
3. 自己在內存前后打標記 --!!!
參考:http://www.cnblogs.com/djinmusic/archive/2013/02/04/2891753.html
后續:[daily] 在CentOS7中使用 sanitizer-address 發現內存問題 / CentOS7使用SCLo軟件源安裝devtoolset軟件