终止在valgrind内运行的进程

35

结束 valgrind 进程本身不会留下有关内部进程执行的报告。

是否可以向在 valgrind 中运行的进程发送终止信号?

1个回答

33

没有所谓的“内部进程”,因为Valgrind本身和正在运行它的客户端程序在单个进程中执行。

发送到该进程的信号将像正常情况下一样被传递给客户端程序。如果该信号导致进程终止,则Valgrind的正常退出处理程序将运行并报告任何泄漏情况。

例如,如果我们在sleep命令上启动Valgrind:

bericote [~] % valgrind sleep 240
==9774== Memcheck, a memory error detector
==9774== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==9774== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==9774== Command: sleep 240
==9774== 

然后终止该命令:

bericote [~] % kill -TERM 9774

那么进程将退出,并且valgrind的退出处理程序将运行:

==9774== 
==9774== HEAP SUMMARY:
==9774==     in use at exit: 0 bytes in 0 blocks
==9774==   total heap usage: 30 allocs, 30 frees, 3,667 bytes allocated
==9774== 
==9774== All heap blocks were freed -- no leaks are possible
==9774== 
==9774== For counts of detected and suppressed errors, rerun with: -v
==9774== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
[1]    9774 terminated  valgrind sleep 240

唯一的例外是 kill -9,因为在这种情况下,进程被内核杀死并且没有通知信号,所以 valgrind 没有机会做任何事情。


1
我在问之前尝试了使用kill -SIGTERM命令,但是等待valgrind停止的时间太久了(约10分钟),所以我怀疑它不会起作用。感谢你的回答。 - Antonio Pérez
延迟是因为它在扫描所有已分配的内存,试图确定是否存在任何泄漏 - 这可能比正常情况下需要更长的时间,因为正在运行中的程序很可能比正常退出的程序具有更多的内存分配。 - TomH
8
在OS X 10.7.5和valgrind-3.8.1上,这并不是真的。Valgrind会快乐地忽略kill命令。 - Kyle Maxwell
这在我使用的Ubuntu 14和Valgrind 3.8上似乎无法工作。该进程未能生成适当的日志便被终止了。 - Bill Yan
Centos:kill -SIGTERM <pid>(或SIGUSR1、SIGUSR2)停止了带有总结的valgrind。 - Prashanth Sriram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接