调试时的信号

发布于 2024-11-09 16:37:31 字数 826 浏览 5 评论 0原文

我正在 Linux 上用 C++ 开发一个应用程序(实际上是一个服务/守护进程),它需要与硬件交互。如果我的程序在终止时没有干净地释放该硬件的资源,那么我必须重新加载设备驱动程序,这个过程大约需要 10 分钟,并且出于明显的原因,必须在程序的每次测试之间等待 10 分钟令人沮丧。

因此,我使用 sigaction() 函数来捕获 SIGINT(ctrl-c),以便我的程序在完成后可以干净地关闭。从控制台运行程序时,效果很好。但是,在 Netbeans 或 Eclipse(我都尝试过)中调试时,事情不起作用。

  • 在 Eclipse 中,如果我在它提供的控制台中按 ctrl-c,它似乎不会注册曾经发生过的 SIGINT
  • 在 Eclipse 中,如果我在调试模式下运行程序,然后使用 kill -SIGINT;,程序就像遇到断点一样中断,
  • 当我在控制台中按 ctrl-c 时,Netbeans 实际上似乎意识到已经发送了一个信号,并弹出一个对话框,询问我是否要将其转发到应用。单击“前进并继续”似乎会破坏程序,并且应用程序不会收到信号。它还说我可以在 Debug -> 中配置这些东西Dbx 配置,一个不存在的菜单项
  • 在 Netbeans 中,如果我在调试模式下运行程序,然后使用 kill -SIGINT,行为与上面
  • 我添加的 相同SIGQUIT 处理程序,并在 Netbeans 中调试时尝试通过 kill 发送该处理程序。这次,不会出现任何对话框,信号处理程序也不会被触发。

我需要某种方法在调试时彻底关闭我的应用程序。有什么想法吗?

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.

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.

  • 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.

I need some way to cleanly shutdown my app while I'm debugging. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小红帽 2024-11-16 16:37:31

事实证明,该问题与 Netbeans 或 Eclipse 无关,而与 gdb 有关。

gdb 可以配置为以多种方式处理信号。如果您运行:

gdb

,然后输入:

info Signals

您将获得信号列表以及 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

等等...

我的临时解决方法是使用 SIGALRM,gdb 默认情况下不会中断并发送到进程。但是,您还可以通过创建 .gdbinit 文件来自定义默认 gdb 设置,您可以在其中设置这些

It turns out the problem had nothing to do with Netbeans or Eclipse, but rather gdb.

gdb can be configured to handle signals in a variety of ways. If you run:

gdb

then type:

info signals

You'll get a list of signals and gdb actions on what to do if it receives that signal:

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

妞丶爷亲个 2024-11-16 16:37:31

即使这篇文章很旧,希望它可以帮助其他人。

为了防止 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

在 Eclipse 配置中,您可以定义要在调试配置中使用的 .gdbinit 文件的位置

日食捕获

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

#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 capture

猫腻 2024-11-16 16:37:31

简单的解决方案..尝试使用 DEBUG 宏来处理您的情况。

// Register the signal handler to stop service.
#ifdef _DEBUG
        signal(SIGKILL, <your signal handler>);
#endif

另外,您可以尝试在退出之前清理您的应用程序。

Simple solution.. Try using DEBUG macros to handle your situation.

// 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文