我想用valgrind检查长时间运行的内存泄漏过程。我怀疑我之后的内存泄漏可能会在执行几个小时之后才会发生。我可以在valgrind下运行应用程序并获得valgrind日志就好了,但这样做意味着我必须退出应用程序并重新启动它以重新启动一个新的valgrind会话,我仍然需要等待几个小时。是否可以保持valgrind和应用程序运行并且在执行期间的任何时候仍然获得valgrind的(部分)数据?
答案 0 :(得分:11)
您可以使用Valgrind gdbserver and GDB。
来实现简而言之,您可以像往常一样使用valgrind启动程序,但使用--vgdb=yes
开关:
$ valgrind --tool=memcheck --vgdb=yes ./a.out
在另一个会话中,您在同一个可执行文件上启动gdb,并连接到valgrind。然后,您可以发出valgrind命令:
$ gdb ./a.out
...
(gdb) target remote | vgdb
....
(gdb) monitor leak_check full reachable any
==8677== 32 bytes in 1 blocks are definitely lost in loss record 1 of 2
==8677== at 0x4C28E3D: malloc (vg_replace_malloc.c:263)
==8677== by 0x400591: foo (in /home/me/tmp/a.out)
==8677== by 0x4005A7: main (in /home/me/tmp/a.out)
==8677==
==8677== 32 bytes in 1 blocks are definitely lost in loss record 2 of 2
==8677== at 0x4C28E3D: malloc (vg_replace_malloc.c:263)
==8677== by 0x400591: foo (in /home/me/tmp/a.out)
==8677== by 0x4005AC: main (in /home/me/tmp/a.out)
==8677==
==8677== LEAK SUMMARY:
==8677== definitely lost: 64 bytes in 2 blocks
==8677== indirectly lost: 0 bytes in 0 blocks
==8677== possibly lost: 0 bytes in 0 blocks
==8677== still reachable: 0 bytes in 0 blocks
==8677== suppressed: 0 bytes in 0 blocks
==8677==
(gdb)
请参阅手册以获取命令列表,here表示memcheck。