如何在gdb中的fork()ed之间切换?

时间:2022-09-06 16:36:30

I'm debugging such a multiple process application,

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

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

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

1 个解决方案

#1


15  

  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实例附加到该进程。 GDB用户手册描述了这个过程如下(重点是我的):

    On most systems, gdb has no special support for debugging programs which create additional processes using the fork function. When a program forks, gdb will continue to debug the parent process and the child process will run unimpeded. If you have set a breakpoint in any code which the child then executes, the child will get a SIGTRAP signal which (unless it catches the signal) will cause it to terminate.

    在大多数系统上,gdb对调试程序没有特殊支持,这些程序使用fork函数创建其他进程。当程序分叉时,gdb将继续调试父进程,子进程将无阻碍地运行。如果您在子进程执行的任何代码中设置了断点,则子进程将获得SIGTRAP信号(除非它捕获信号)将导致它终止。

    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.

    但是,如果要调试子进程,则有一种不太痛苦的解决方法。在fork之后执行子进程执行的代码中调用sleep。仅当设置了某个环境变量或存在某个文件时才睡眠,这样当您不想在子项上运行gdb时就不会发生延迟。当孩子正在睡觉时,使用ps程序获取其进程ID。然后告诉gdb(如果你也在调试父进程,则调用gdb的新方法)以附加到子进程(请参阅Attach)。从那时起,您可以调试子进程,就像您附加到的任何其他进程一样。

    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.

    它的长短是因为当你启动一个稍后分叉的程序时,GDB将保持与父进程的连接(尽管你可以遵循子进程,而不是使用set follow-fork-mode子进程)。通过将其他进程置于休眠状态,您也可以将新的GDB实例连接到它。

  2. 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:

    使用set detach-on-fork off将这两个进程保持在gdb的控制之下。默认情况下,父进程将照常调试并且子进程将被暂停,但是通过调用set follow-fork-mode子进程可以更改此行为(以便子进程将像往常一样进行调试,父进程将是暂停)。 GDB用户手册描述了这个过程如下:

    gdb will retain control of all forked processes (including nested forks). You can list the forked processes under the control of gdb by using the info inferiors command, and switch from one fork to another by using the inferior command (see Debugging Multiple Inferiors and Programs).

    gdb将保留对所有分叉进程(包括嵌套分支)的控制。您可以使用info inferiors命令在gdb的控制下列出分叉进程,并使用inferior命令从一个fork切换到另一个fork(请参阅调试多个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.

    要退出调试其中一个分叉进程,可以使用detach inferiors命令(允许它独立运行)从中分离,或使用kill inferiors命令将其终止。请参阅调试多个Inferiors和程序。

#1


15  

  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实例附加到该进程。 GDB用户手册描述了这个过程如下(重点是我的):

    On most systems, gdb has no special support for debugging programs which create additional processes using the fork function. When a program forks, gdb will continue to debug the parent process and the child process will run unimpeded. If you have set a breakpoint in any code which the child then executes, the child will get a SIGTRAP signal which (unless it catches the signal) will cause it to terminate.

    在大多数系统上,gdb对调试程序没有特殊支持,这些程序使用fork函数创建其他进程。当程序分叉时,gdb将继续调试父进程,子进程将无阻碍地运行。如果您在子进程执行的任何代码中设置了断点,则子进程将获得SIGTRAP信号(除非它捕获信号)将导致它终止。

    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.

    但是,如果要调试子进程,则有一种不太痛苦的解决方法。在fork之后执行子进程执行的代码中调用sleep。仅当设置了某个环境变量或存在某个文件时才睡眠,这样当您不想在子项上运行gdb时就不会发生延迟。当孩子正在睡觉时,使用ps程序获取其进程ID。然后告诉gdb(如果你也在调试父进程,则调用gdb的新方法)以附加到子进程(请参阅Attach)。从那时起,您可以调试子进程,就像您附加到的任何其他进程一样。

    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.

    它的长短是因为当你启动一个稍后分叉的程序时,GDB将保持与父进程的连接(尽管你可以遵循子进程,而不是使用set follow-fork-mode子进程)。通过将其他进程置于休眠状态,您也可以将新的GDB实例连接到它。

  2. 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:

    使用set detach-on-fork off将这两个进程保持在gdb的控制之下。默认情况下,父进程将照常调试并且子进程将被暂停,但是通过调用set follow-fork-mode子进程可以更改此行为(以便子进程将像往常一样进行调试,父进程将是暂停)。 GDB用户手册描述了这个过程如下:

    gdb will retain control of all forked processes (including nested forks). You can list the forked processes under the control of gdb by using the info inferiors command, and switch from one fork to another by using the inferior command (see Debugging Multiple Inferiors and Programs).

    gdb将保留对所有分叉进程(包括嵌套分支)的控制。您可以使用info inferiors命令在gdb的控制下列出分叉进程,并使用inferior命令从一个fork切换到另一个fork(请参阅调试多个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.

    要退出调试其中一个分叉进程,可以使用detach inferiors命令(允许它独立运行)从中分离,或使用kill inferiors命令将其终止。请参阅调试多个Inferiors和程序。