Linux 调试时的信号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6136513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Signals when debugging
提问by Frederik
I'm developing an application (a service/daemon, really) on Linux in C++ that needs to interface with a piece of hardware. If my program doesn't release the resources for this peice of hardware cleanly when terminating, then I have to reload the device driver, a process that takes about 10 minutes and for obvious reasons having to wait 10 minutes between each test of the program would be frustrating.
我正在用 C++ 在 Linux 上开发一个应用程序(实际上是一个服务/守护进程),它需要与硬件接口。如果我的程序在终止时没有干净地释放这个硬件的资源,那么我必须重新加载设备驱动程序,这个过程大约需要 10 分钟,并且由于显而易见的原因必须在每次程序测试之间等待 10 分钟令人沮丧。
So I have used the sigaction()
function to catch a SIGINT (a ctrl-c) so that my program can cleanly shutdown when I'm finished with it. When running the program from the console, this works fine. However, when debugging in Netbeans or Eclipse (I've tried both) things don't work.
因此,我使用该sigaction()
函数来捕获 SIGINT(一个 ctrl-c),以便我的程序在完成后可以干净地关闭。从控制台运行程序时,这工作正常。但是,在 Netbeans 或 Eclipse 中调试(我都试过)时,事情不起作用。
- In Eclipse, if I hit ctrl-c in the console it provides, it doesn't seem to register that a SIGINT ever occurred
- In Eclipse, if I run the program in debug mode and then use
kill -SIGINT <pid>
, the program just breaks as if it hit a breakpoint - Netbeans actually seems to realise a signal has been sent when I hit ctrl-c in the console, and pops up a dialog asking if I want to forward it to the application. Clicking "Forward and continue" just seems to break the program and the signal is not received by the application. It also says I can configure this stuff in Debug -> Dbx configure, a menu item that doesn't exist
- In Netbeans, if I run the program in debug mode and then use
kill -SIGINT <pid>
, the behaviour is the same as above - I then added a SIGQUIT handler and tried sending that via
kill
when debugging in Netbeans. This time, no dialog appears and the signal handler is never tripped.
- 在 Eclipse 中,如果我在它提供的控制台中按 ctrl-c,它似乎没有注册 SIGINT 曾经发生过
- 在 Eclipse 中,如果我在调试模式下运行程序然后使用
kill -SIGINT <pid>
,程序就会中断,就像它遇到断点一样 - 当我在控制台中按 ctrl-c 时,Netbeans 实际上似乎意识到已发送信号,并弹出一个对话框询问我是否要将其转发给应用程序。单击“前进并继续”似乎只是中断了程序,应用程序也收不到信号。它还说我可以在 Debug -> Dbx configure 中配置这些东西,一个不存在的菜单项
- 在 Netbeans 中,如果我在调试模式下运行程序然后使用
kill -SIGINT <pid>
,则行为与上述相同 - 然后我添加了一个 SIGQUIT 处理程序并尝试
kill
在 Netbeans 中调试时发送它。这一次,不会出现对话框,信号处理程序也不会被触发。
I need someway to cleanly shutdown my app while I'm debugging. Any ideas?
我需要某种方法在调试时干净地关闭我的应用程序。有任何想法吗?
采纳答案by Frederik
It turns out the problem had nothing to do with Netbeans or Eclipse, but rather gdb.
事实证明,问题与 Netbeans 或 Eclipse 无关,而与 gdb 有关。
gdb can be configured to handle signals in a variety of ways. If you run:
gdb 可以配置为以多种方式处理信号。如果你运行:
gdb
gdb
then type:
然后输入:
info signals
info signals
You'll get a list of signals and gdb actions on what to do if it receives that signal:
如果收到该信号,您将获得信号列表和 gdb 操作:
Signal Stop Print Pass to program Description
SIGHUP Yes Yes Yes Hangup
SIGINT Yes Yes No Interrupt
SIGQUIT Yes Yes Yes Quit
SIGILL Yes Yes Yes Illegal instruction
SIGTRAP Yes Yes No Trace/breakpoint trap
etc...
等等...
My temporary work around has been to use SIGALRM which gdb defaults to not breaking and sending to the process. However, you can also customise the default gdb settings by creating a .gdbinit file where you can set these
我的临时解决方法是使用 SIGALRM,gdb 默认不会破坏并发送到进程。但是,您也可以通过创建一个 .gdbinit 文件来自定义默认的 gdb 设置,您可以在其中设置这些
回答by Frederik
Simple solution.. Try using DEBUG macros to handle your situation.
简单的解决方案.. 尝试使用 DEBUG 宏来处理您的情况。
// Register the signal handler to stop service.
#ifdef _DEBUG
signal(SIGKILL, <your signal handler>);
#endif
Also, you may try to clean up your app before exiting.
此外,您可以尝试在退出前清理您的应用程序。
回答by lgm42
Even this post is old, hope it can help others.
即使这个帖子很旧,希望它可以帮助其他人。
To prevent Eclipse from catching the Ctrl+C, you can configure your gdb using .gbdinit file. You create a .gdinit with this content
为了防止 Eclipse 捕获 Ctrl+C,您可以使用 .gbdinit 文件配置您的 gdb。你用这个内容创建一个 .gdinit
#we want Ctrl+C to be no break, pass to application and printed by the debugger
handle SIGINT nostop
handle SIGINT pass
handle SIGINT print
In your eclipse configuration, you can define where is your .gdbinit file to use in your Debug configuration
在您的 Eclipse 配置中,您可以定义 .gdbinit 文件在您的 Debug 配置中使用的位置