如何在gdb中由fork

如何在gdb中由fork

本文介绍了如何在gdb中由fork()编辑的不同进程之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试这样的多进程应用程序,

I'm debugging such a multiple process application,

如何在fork()个进程之间切换?

how can I switch between the fork()ed processes?

推荐答案

  1. 您可以使子进程进入睡眠状态,然后将新的GDB实例附加到该进程. GDB用户手册对该过程的描述如下(重点是我的):

  1. You can put the child process to sleep and then attach a new instance of GDB to it. The GDB User Manual describes this process as follows (emphasis is mine):

但是,如果您要调试 子进程有一个解决方法 这不是很痛苦.打电话给 睡在那个孩子的密码里 进程在派生之后执行. 它 可能只有在以下情况下睡觉才有用 设置了某些环境变量, 或某个文件存在,以便 当你不这样做的时候就不需要延迟 想要在孩子上运行gdb. 孩子在睡觉,用ps 程序以获取其进程ID.然后 告诉gdb(如果gdb的新调用 你也在调试父母 过程)附加到孩子身上 处理(请参阅附件).从那一点开始 您可以调试子进程 就像您执行的其他任何流程一样 附着.

However, if you want to debug the child process there is a workaround which isn't too painful. Put a call to sleep in the code which the child process executes after the fork. It may be useful to sleep only if a certain environment variable is set, or a certain file exists, so that the delay need not occur when you don't want to run gdb on the child. While the child is sleeping, use the ps program to get its process ID. Then tell gdb (a new invocation of gdb if you are also debugging the parent process) to attach to the child process (see Attach). From that point on you can debug the child process just like any other process which you attached to.

总的来说,当您启动一个以后进行分叉的程序时,GDB将保持与父进程的连接(尽管您可以通过set follow-fork-mode child跟随子进程).通过使其他进程进入休眠状态,您也可以将新的GDB实例连接到该实例.

The long and the short of it is that when you start a program that later forks, GDB will stay connected to the parent process (though you can follow the child process, instead, by using set follow-fork-mode child). By putting the other process to sleep, you can have a new instance of GDB connect to it, as well.

使用set detach-on-fork off将两个进程置于gdb的控制之下.默认情况下,父进程将像往常一样被调试,子进程将保持挂起状态,但是通过调用set follow-fork-mode child您可以更改此行为(这样子进程将像往常一样被调试,父进程将保持挂起状态). GDB用户手册描述了以下过程:

Use set detach-on-fork off to hold both processes under the control of gdb. By default, the parent process will be debugged as usual and the child will be held suspended, but by calling set follow-fork-mode child you can change this behavior (so that the child process will be debugged as usual and the parent will be held suspended). The GDB User Manual describes this process as follows:

退出调试之一 流程,您可以从 通过使用detach inferiors 命令(允许它运行 独立地),或使用 kill inferiors命令.请参见调试 多个下级和程序.

To quit debugging one of the forked processes, you can either detach from it by using the detach inferiors command (allowing it to run independently), or kill it using the kill inferiors command. See Debugging Multiple Inferiors and Programs.

这篇关于如何在gdb中由fork()编辑的不同进程之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:21