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

后端 未结 2 1177
不思量自难忘°
不思量自难忘° 2021-02-04 13:15

I\'m debugging such a multiple process application,

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

相关标签:
2条回答
  • 2021-02-04 13:24
    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):

      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.

      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.

    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:

      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.

    0 讨论(0)
  • 2021-02-04 13:35

    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)]
    
    0 讨论(0)
提交回复
热议问题