首页 > 解决方案 > 如何在长时间运行的服务器程序上使用 asan?

问题描述

我有一个长时间运行的服务器程序,我想使用 asan 来检测这个程序的内存泄漏。我的解决方案:

CXXFLAGS+="-fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address"
LDFLAGS+="-lasan"
# start the program
LD_PRELOAD=libasan.so.5 ASAN_OPTIONS=halt_on_error=false:alloc_dealloc_mismatch=0 ./bin/server > asan_report 2>&1 &

但它似乎只报告一次我希望它定期报告,怎么做?

标签: c++memory-leaksg++address-sanitizer

解决方案


我希望它定期报告

您可以__lsan_do_recoverable_leak_check();在调度循环中的适当时间调用。

lsan_interface.h

  // Check for leaks now. Returns zero if no leaks have been found or if leak
  // detection is disabled, non-zero otherwise.
  // This function may be called repeatedly, e.g. to periodically check a
  // long-running process. It prints a leak report if appropriate, but does not
  // terminate the process. It does not affect the behavior of
  // __lsan_do_leak_check() or the end-of-process leak check, and is not
  // affected by them.

这将重复打印相同的泄漏(没有“仅打印自上次调用以来的新泄漏”功能)。

PS由于你的程序已经链接了libasan,所以没有任何理由LD_PRELOAD,并且有很多缺点。


推荐阅读