分叉后调试子进程(配置了分叉模式子进程) [英] Debugging child process after fork (follow-fork-mode child configured)

查看:67
本文介绍了分叉后调试子进程(配置了分叉模式子进程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,父母可以派生一个孩子来处理某些任务.我遇到了一个问题,我已将gdb配置为跟随叉子模式的子级,但是在派生后,到达断点后,它会发送SIGTRAP,但是该子级会终止并将SIGCHLD发送给父级.

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.

我在fork之前配置了signal(SIGTRAP, SIG_IGN),因此我的理解是,子对象应该在到达断点但没有发生断点时继承并忽略SIGTRAP.

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.

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

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

如何成功调试子进程?

推荐答案

子进程从父级继承信号处理程序,但未继承未处理的信号.

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

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

After forking try installing the signal handler for SIGTRAP at 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.

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

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

set follow-fork-mode child

但是,现在只能调试子级,而父级可以不受检查地运行.

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

调试子进程的另一种方法.

执行fork()后,在执行子代的代码中调用sleep(),使用ps实用程序获取子代的PID,然后附加PID.

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

attach <PID of child process>

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

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

调试后,您可以使用以下方式分离PID:

After debugging, you can detach the PID using

detach

这篇关于分叉后调试子进程(配置了分叉模式子进程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆