C语言 fork后调试子进程(follow-fork-mode child配置)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15126925/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:29:33  来源:igfitidea点击:

Debugging child process after fork (follow-fork-mode child configured)

cgdbsignalsfork

提问by ihsan

I'm developing an application which the parent forks a child to handle certain tasks. I'm having an issue where I've configured gdb to follow-fork-mode child but after fork, after reaching a breakpoint, it sends a SIGTRAP but the child somehow terminates and send SIGCHLD to the parent.

我正在开发一个应用程序,父级分叉子级来处理某些任务。我遇到了一个问题,我已将 gdb 配置为跟随 fork-mode 子级,但在 fork 之后,到达断点后,它发送一个 SIGTRAP,但子级以某种方式终止并将 SIGCHLD 发送给父级。

I've configured signal(SIGTRAP, SIG_IGN)before fork so my understanding is that the child should inherit and ignore SIGTRAP when the breakpoint is reached but it's not happening.

我已经signal(SIGTRAP, SIG_IGN)在 fork之前进行了配置,所以我的理解是当达到断点但它没有发生时,孩子应该继承并忽略 SIGTRAP。

Please help me to understand this if I'm incorrect.

如果我不正确,请帮助我理解这一点。

How can I successfully debug the child process?

如何成功调试子进程?

回答by Barath Ravikumar

The child process inherits signal handlers from the parent, but not the pending signal.

子进程从父进程继承信号处理程序,但不继承挂起的信号。

After forking try installing the signal handler for SIGTRAPat a place in code where the child process executes after forking. If you don't handle SIGTRAP, the default action is that the child is terminated.

分叉后,尝试在分叉SIGTRAP后子进程执行的代码中安装信号处理程序。如果您不处理SIGTRAP,则默认操作是终止子进程。

If you want to debug the child process, you must use follow-fork-mode. You must set the mode using

如果要调试子进程,则必须使用follow-fork-mode. 您必须使用设置模式

set follow-fork-mode child

However, now only the child can be debugged, and the parent runs unchecked.

但是,现在只能调试子级,而父级运行不受检查。

There is an alternative wayof debugging the child process.

还有一种调试子进程的替代方法

After fork()is executed, put a sleep()call in the code where the child executes, get the PID of the child using the psutility, then attach the PID.

fork()执行,把sleep()调用的代码里子执行,使用得到孩子的PIDps工具,然后装上PID。

attach <PID of child process>

Now, you can debug the child process, like any other process.

现在,您可以像调试任何其他进程一样调试子进程。

After debugging, you can detach the PID using

调试后,您可以使用分离PID

detach