Can valgrind output partial reports without having to quit the profiled application?

纵饮孤独 提交于 2019-11-29 01:11:58

问题


I want to check a long running process for memory leaks with valgrind. I suspect the memory leak I'm after might happen only after several hours of execution. I can run the app under valgrind and get the valgrind log just fine, but doing so means I have to quit the application and start it again anew for a new valgrind session for which I would still have to wait several hours. Is it possible to keep valgrind and the app running and still get valgrind's (partial) data at any point during execution?


回答1:


You can do that by using the Valgrind gdbserver and GDB.

In short, you start your program with valgrind as usual, but with the --vgdb=yes switch:

$ valgrind --tool=memcheck --vgdb=yes ./a.out 

In another session, you start gdb on the same executable, and connect to valgrind. You can then issue valgrind commands:

$ 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) 

See the manual for a list of commands, here for memcheck.



来源:https://stackoverflow.com/questions/9720632/can-valgrind-output-partial-reports-without-having-to-quit-the-profiled-applicat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!