当 运行 时,是否可以显示由 LeakSanitizer 管理的当前分配的缓冲区?

While running, is it possible to display the currently allocated buffers managed by LeakSanitizer?

我有一个非常大的程序(好吧,根据 cloc 只有 13,000 行代码)会泄漏。我知道,因为随着时间的推移,它使用越来越多的常驻内存。

我打开了清理程序选项,但在干净退出时,我所有的 C++ 软件都会按预期正确清理所有内容。所以我没有看到消毒剂输出有任何增长。

在这种情况下有用的是一种调用函数的方法,该函数在 运行 代码时显示已分配缓冲区的(大)列表。然后我可以查看两个这样的输出的差异,看看重新分配了什么。泄漏的缓冲区将在那里...

不过,在这一点上,我只是没有看到任何 header 具有我可以调用以查看此类列表的消毒功能。存在吗?

Lsan 界面在 sanitizer/lsan_interface.h 中可用,但据我所知,它没有 API 来打印分配信息。最好的方法是使用 Asan(也包括 Lsan)编译代码并使用 __asan_print_accumulated_stats 获取基本的分配统计信息:

$ cat tmp.c
#include <sanitizer/asan_interface.h>
#include <stdlib.h>

int main() {
  malloc(100);
  __asan_print_accumulated_stats();
  return 0;
}
$ gcc -fsanitize=address -g tmp.c && ./a.out
Stats: 0M malloced (0M for red zones) by 2 calls
Stats: 0M realloced by 0 calls
Stats: 0M freed by 0 calls
Stats: 0M really freed by 0 calls
Stats: 0M (0M-0M) mmaped; 5 maps, 0 unmaps
  mallocs by size class: 7:1; 11:1;
Stats: malloc large: 0
Stats: StackDepot: 2 ids; 0M allocated
Stats: SizeClassAllocator64: 0M mapped in 256 allocations; remains 256
  07 (112): mapped: 64K allocs: 128 frees: 0 inuse: 128 num_freed_chunks 457 avail: 585 rss: 4K releases: 0
  11 (176): mapped: 64K allocs: 128 frees: 0 inuse: 128 num_freed_chunks 244 avail: 372 rss: 4K releases: 0
Stats: LargeMmapAllocator: allocated 0 times, remains 0 (0 K) max 0 M; by size logs:

=================================================================
==15060==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 100 byte(s) in 1 object(s) allocated from:
    #0 0x7fdf2194fb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    #1 0x559ca08a7857 in main /home/yugr/tmp.c:5
    #2 0x7fdf214a1bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)

SUMMARY: AddressSanitizer: 100 byte(s) leaked in 1 allocation(s).

遗憾的是,无法打印准确的分配。