原文地址:https://www.cnblogs.com/liqinglucky/p/wireshark_memory_check.html
在ubuntu系统wireshark源码编译文中已经会编译wireshark了。现在对wireshark的CMakeLists.txt中添加内存检测工具address sanitizer。这样就可以对wireshark程序代码做内存检测功能。
一 添加编译参数
改动很简单,只需在wireshark的CMakeLists.txt中添加address sanitizer编译参数
add_compile_options(-fsanitize=address -fsanitize-recover=all -fsanitize=leak)
add_link_options(-fsanitize=address -fsanitize-recover=all -fsanitize=leak)
git diff如下
wireshark-4.0.2# ls
CMakeLists.txt
wireshark-4.0.2# git diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 092dc7b..a9cad7a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1452,6 +1452,9 @@ if (QT_FOUND)
endif()
endif()
+add_compile_options(-fsanitize=address -fsanitize-recover=all -fsanitize=leak)
+add_link_options(-fsanitize=address -fsanitize-recover=all -fsanitize=leak)
+
if(ENABLE_CHECKHF_CONFLICT)
set(ENABLE_CHECK_FILTER 1)
endif()
然后编译。
二 测试内存检测功能
可以在wireshark源代码中加测试代码,也可以启动wireshark后,直接用窗口的关闭按钮退出窗口就会触发内存泄漏
测试日志如下:
wireshark-4.0.2/build# run/wireshark
** (wireshark:472217) 06:50:13.504868 [Capture MESSAGE] -- Capture Start ...
** (wireshark:472217) 06:50:14.219331 [Capture MESSAGE] -- Capture started
** (wireshark:472217) 06:50:14.219555 [Capture MESSAGE] -- File: "/tmp/wireshark_any57OV01.pcapng"
** (wireshark:472217) 06:51:03.763713 [Capture MESSAGE] -- Capture Stop ...
** (wireshark:472217) 06:51:04.131232 [Capture MESSAGE] -- Capture stopped.
=================================================================
==472217==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 256 byte(s) in 1 object(s) allocated from:
#0 0x7f9b05b65808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x7f9ae55692f4 (/lib/x86_64-linux-gnu/libfontconfig.so.1+0x212f4)
Direct leak of 8 byte(s) in 1 object(s) allocated from:
#0 0x7f9b05b67587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104
#1 0x55d8a024e8e3 in FunnelStatistics::FunnelStatistics(QObject*, CaptureFile&) /var/lte/wireshark/wireshark-4.0.2/ui/qt/funnel_statistics.cpp:122
Indirect leak of 32 byte(s) in 1 object(s) allocated from:
#0 0x7f9b05b65a06 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:153
#1 0x7f9ae556993c (/lib/x86_64-linux-gnu/libfontconfig.so.1+0x2193c)
SUMMARY: AddressSanitizer: 296 byte(s) leaked in 3 allocation(s).
标签:Capture,..,fsanitize,leak,sanitizer,address,wireshark
From: https://www.cnblogs.com/liqinglucky/p/wireshark_memory_check.html