gpt4 book ai didi

c - 如何在 gdb 中 fork() ed 的不同进程之间切换?

转载 作者:太空狗 更新时间:2023-10-29 16:47:12 26 4
gpt4 key购买 nike

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

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

最佳答案

  1. 您可以让子进程休眠,然后将一个新的 GDB 实例附加到它。 GDB User Manual描述这个过程如下(强调是我的):

    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.

    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 的程序时,GDB 将保持与父进程的连接(尽管您可以跟随子进程,而不是使用 set follow-fork-模式 child )。通过让另一个进程休眠,您也可以让一个新的 GDB 实例连接到它。

  2. 使用 set detach-on-fork off 将两个进程都置于 gdb 的控制之下。默认情况下,父进程将像往常一样被调试,子进程将被挂起,但是通过调用 set follow-fork-mode child 您可以更改此行为(这样子进程将被调试和往常一样,家长将被停职)。 GDB User Manual描述这个过程如下:

    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).

    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.

关于c - 如何在 gdb 中 fork() ed 的不同进程之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6223786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com