简体   繁体   English

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

[英]How can I switch between different processes fork() ed in gdb?

I'm debugging such a multiple process application,我正在调试这样一个多进程应用程序,

how can I switch between the fork() ed processes?如何在fork() ed 进程之间切换?

  1. You can put the child process to sleep and then attach a new instance of GDB to it.您可以让子进程进入睡眠状态,然后将 GDB 的新实例附加到它。 The GDB User Manual describes this process as follows (emphasis is mine): GDB用户手册描述了这个过程如下(重点是我的):

    On most systems, gdb has no special support for debugging programs which create additional processes using the fork function.在大多数系统上,gdb 对使用分支 function 创建额外进程的调试程序没有特别支持。 When a program forks, gdb will continue to debug the parent process and the child process will run unimpeded.当一个程序fork时,gdb会继续调试父进程,子进程会畅通无阻地运行。 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.如果您在子程序执行的任何代码中设置了断点,子程序将获得一个 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.在 fork 之后子进程执行的代码中调用 sleep 。 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.仅当设置了某个环境变量或存在某个文件时才休眠可能很有用,这样当您不想在子节点上运行 gdb 时就不会出现延迟。 While the child is sleeping, use the ps program to get its process ID.在孩子睡觉时,使用 ps 程序获取其进程 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).然后告诉 gdb(如果您也在调试父进程,则新调用 gdb )附加到子进程(请参阅附加)。 From that point on you can debug the child process just like any other process which you attached to.从那时起,您可以像您附加的任何其他进程一样调试子进程。

    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 ).总而言之,当您启动一个稍后分叉的程序时,GDB 将保持与父进程的连接(尽管您可以通过使用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 实例。

  2. Use set detach-on-fork off to hold both processes under the control of gdb.使用set detach-on-fork off将两个进程保持在 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).默认情况下,父进程将照常调试,子进程将暂停,但通过调用set follow-fork-mode child您可以更改此行为(这样子进程将照常调试,父进程将暂停)。 The GDB User Manual describes this process as follows: GDB用户手册描述了这个过程如下:

    gdb will retain control of all forked processes (including nested forks). gdb 将保留对所有分叉进程(包括嵌套分叉)的控制。 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 ).您可以使用info inferiors命令列出 gdb 控制下的分叉进程,并使用 lower 命令从一个分支切换到另一个分支(请参阅调试多个inferior程序和程序)。

    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.要退出调试其中一个分叉的进程,您可以使用detach inferiors命令从它分离(允许它独立运行),或者使用kill inferiors命令将其终止。 See Debugging Multiple Inferiors and Programs .请参阅调试多个劣质程序和程序

Show all the processes.显示所有进程。

(gdb) info inferiors 
  Num  Description       Executable        
  1    process 1000      /tmp/a.out 
* 2    <null>            /tmp/a.out  # current attach inferior

Switch between different processes.在不同的进程之间切换。

(gdb) inferior 1
[Switching to inferior 1 [process 1000] (/tmp/a.out)]
[Switching to thread 1.1 (LWP 1000)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM