Not supported on windows
Supported on linux
Not supported on embedded
Not supported on android

set follow-fork-mode command

Controls the behavior of GDB when the debugged program calls fork() or vfork()

Syntax

set follow-fork-mode parent
set follow-fork-mode child
show follow-fork-mode

Modes

parent
In this mode GDB will continue debugging the parent process if a fork() or vfork() is called. This is the default mode.
child
In this mode GDB will switch to the child process if a fork() or vfork() is called.

Default mode

The default value for the follow-fork-mode setting is 'parent'.

Remarks

If you have set detach-on-fork to on, GDB will debug both the parent and the child process. Use the info inferiors command to show details and the inferior command to switch between them.

Examples

In this example we will debug the following C++ program:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

void func(int pid, int ret)
{
    printf("My PID is %d, fork() returned %d\n", pid, ret);

    if (ret)
        printf("We are in the parent process\n");
    else
        printf("We are in the child process\n");
}

int main()
{
    int r = fork();
    func(getpid(), r);
    return 0;
}

First we will debug the program with the default setting for follow-fork-mode:

(gdb) break main
Breakpoint 1 at 0x804848f: file forktest.cpp, line 17.
(gdb) run
Starting program: /home/testuser/forktest

Breakpoint 1, main () at forktest.cpp:17
17 int r = fork();
(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "parent".
(gdb) break func
Breakpoint 2 at 0x804844a: file forktest.cpp, line 7.
(gdb) continue
Continuing.

Breakpoint 2, func (pid=7975, ret=7980) at forktest.cpp:7
7 printf("My PID is %d, fork() returned %dpid, ret);
(gdb)
My PID is 7980, fork() returned 0
We are in the child process
(gdb) continue
Continuing.
My PID is 7975, fork() returned 7980
We are in the parent process
[Inferior 1 (process 7975) exited normally]

As GDB was configured to continue debugging the parent process, the child process produced the 'We are in the child process' text while GDB was stopped at a breakpoint in the parent process. When we issued the continue command, the parent process printed its message and exited.

Now we will see what happens when GDB is configured to switch to the child process:

(gdb) break main
Breakpoint 1 at 0x804848f: file forktest.cpp, line 17.
(gdb) run
Starting program: /home/testuser/forktest

Breakpoint 1, main () at forktest.cpp:17
17 int r = fork();
(gdb) set follow-fork-mode child
(gdb) break func
Breakpoint 2 at 0x804844a: file forktest.cpp, line 7.
(gdb) continue
Continuing.
My PID is 8025, fork() returned 8029
We are in the parent process[New process 8029]
[Switching to process 8029]

Breakpoint 2, func (pid=8029, ret=0) at forktest.cpp:7
7 printf("My PID is %d, fork() returned %dpid, ret);
(gdb) continue
Continuing.
My PID is 8029, fork() returned 0
We are in the child process
[Inferior 2 (process 8029) exited normally]

GDB has now switched to the child process, keeping the parent process run in the background. The value of ret in the breakpoint message was 0 indicating that we are in the child process.

Compatibility with VisualGDB

You can run the set follow-fork-mode command under VisualGDB using the GDB Session window:

See also