如何在 valgrind 报告中打印测试名称?

How can I print test name in valgrind report?

我正在使用 valgrind 检查内存泄漏。 我的应用程序有两个线程:

  1. 运行 测试的主线程。
  2. 等待来自主线程的消息的工作线程和 根据收到的消息执行命令。

当 valgrind 发现内存泄漏时,如果泄漏与工作线程有关,valgrind 报告会显示工作线程的堆栈跟踪。 为了修复这种泄漏,我需要知道是哪个测试导致了泄漏。测试 name/ID 来自消息队列的主线程。 是否可以将测试名称或 ID 添加到 valgrind 报告中?

Is it possible to add test name or ID to the valgrind report?

不,据我所知这是不可能的。

但是当 valgrind 发现内存泄漏时,您可以使用 gdb 附加到您的应用程序。以下是 valgrind manual 中对如何操作的描述:

If you want to debug a program with GDB when using the Memcheck tool, start Valgrind like this:

valgrind --vgdb=yes --vgdb-error=0 prog

In another shell, start GDB:

gdb prog

Then give the following command to GDB:

(gdb) target remote | vgdb

现在您可以在堆栈跟踪中找到从主线程接收消息的位置,并在 gdb 中打印测试名称、ID 或任何您想要的内容。

我们找到的解决方案是使用 valgrind 宏:VALGRIND_DO_CHANGED_LEAK_CHECKVALGRIND_COUNT_LEAKSVALGRIND_COUNT_LEAK_BLOCKS 以确定哪个测试导致泄漏