程序是不是需要退出 Valgrind 才能工作?

Posted

技术标签:

【中文标题】程序是不是需要退出 Valgrind 才能工作?【英文标题】:Does the program need to exit for Valgrind to work?程序是否需要退出 Valgrind 才能工作? 【发布时间】:2015-11-20 20:36:16 【问题描述】:

注意: 1. 我尝试传递标志--vgdb=yes,但在这种情况下,进程根本没有运行。 2. 我不赞成检测我的代码。

问题: 我正在尝试在永远运行的自定义操作系统(基于 linux)用户空间进程上运行 valgrind。在这种情况下,我如何获得内存泄漏的 Valgrind 统计信息,即该过程永远运行。 在当前状态下,我使用 Valgrind 启动了该过程并查看了以下日志:

==6561== Memcheck, a memory error detector                                                                                                                                                                         
==6561== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==6561== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==6561== Command: xyz_server
==6561== Parent PID: 3056
. . . . 
==6561== 
==6561== Thread 12: 
==6561== Syscall param timer_create(evp) points to uninitialised byte(s)
==6561==    at 0x84949BD: timer_create (in /lib64/librt-2.12.so)
==6561==    by 0x56D518E: api_1 (in . . .) // removed proprietary lib name
==6561==    by 0x74DDDD2: api_2 (in . . .) // removed proprietary lib name
==6561==    by 0x827A9BB: start_thread (pthread_create.c:301)
==6561==  Address 0x1fb37a50 is on thread 12's stack
==6561==   

这里没有统计数据。例如,如果我在 ls 上使用相同的 args 运行 valgrind:

$ valgrind --leak-check=full ls -l
==12584== Memcheck, a memory error detector
==12584== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==12584== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==12584== Command: ls -l
==12584==
. . .
==12584== HEAP SUMMARY:
==12584==     in use at exit: 18,424 bytes in 10 blocks
==12584==   total heap usage: 71 allocs, 61 frees, 63,312 bytes allocated
. . .
==12584== LEAK SUMMARY:
==12584==    definitely lost: 0 bytes in 0 blocks
==12584==    indirectly lost: 0 bytes in 0 blocks
==12584==      possibly lost: 18,424 bytes in 10 blocks
==12584==    still reachable: 0 bytes in 0 blocks
==12584==         suppressed: 0 bytes in 0 blocks
==12584==
==12584== For counts of detected and suppressed errors, rerun with: -v
==12584== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 2 from 2
. . .

如何获得我的程序的类似统计数据?我的进程永远不会退出。

【问题讨论】:

How to do memory check on a daemon program?的可能重复 @cad,没有回答我的问题。 @ks1322:vgdb 在我的情况下似乎不起作用。其他答案谈论的是检测代码,而不是支持。 【参考方案1】:

您可以在程序运行时从 shell 或 gdb 触发泄漏搜索。

为此,valgrind gdbserver 必须处于活动状态。 gdbserver 默认是激活的(如果你给--vgdb=no,它会被禁用)。没有理由给出 --vgdb=yes 会使您的程序无法运行。

另一方面,给出 --vgdb-error=0 意味着您的程序将等待来自 gdb/vgdb 的连接以继续。

要从 shell 触发泄漏搜索,您可以使用:

vgdb leak_search full

【讨论】:

以上是关于程序是不是需要退出 Valgrind 才能工作?的主要内容,如果未能解决你的问题,请参考以下文章

valgrind测试程序内存泄漏问题

控制台应用程序在完成工作后需要很长时间才能退出

使用gdb进行调试时,请彻底退出valgrind

在 Valgrind 中是不是需要处理仍然可访问的内存?

C++内存检查工具valgrind

C++内存检查工具valgrind