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

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

问题描述

我正在开发一个应用程序,父母派出一个孩子来处理某些任务.我遇到了一个问题,我已将 gdb 配置为 follow-fork-mode child,但在 fork 之后,到达断点后,它会发送一个 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.

如何才能成功调试子进程?

How can I successfully debug the child process?

推荐答案

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

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

在 fork 之后,尝试在 fork 后子进程执行的代码位置安装 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()执行完毕后,在child执行的代码中放一个sleep()调用,使用ps获取child的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

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

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