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

set detach-on-fork command

Specifies whether GDB should debug both parent and child process after a call to fork() or vfork()

Syntax

set detach-on-fork on
set detach-on-fork off
show detach-on-fork

Modes

on
In this mode GDB will continue being attached to either parent or child process (depending on the set follow-fork-mode command.
off
In this mode GDB will be attached to both processes after a call to fork() or vfork(). Use the info inferiors command to show details and the inferior command to switch between them.

Default mode

The default value for the detach-on-fork setting is 'on'.

Remarks

Use the set follow-fork-mode command to control which process will be selected after gdb continues from a fork() or vfork() call.

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;
}

If we debug the program with the default setting for detach-on-fork, only one of the two processes keep on being debugged (and will trigger breakpoints). See the set follow-fork-mode description for more details.

If we set detach-on-fork to on, GDB will not detach from the child process and we will be able to switch to it using the inferior command:

(gdb) start
Temporary breakpoint 1 at 0x804848f: file forktest.cpp, line 17.
Starting program: /home/testuser/forktest

Temporary 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) set detach-on-fork off
(gdb) break func
Breakpoint 2 at 0x804844a: file forktest.cpp, line 7.
(gdb) continue
Continuing.
[New process 8133]

Breakpoint 2, func (pid=8125, ret=8133) at forktest.cpp:7
7 printf("My PID is %d, fork() returned %dpid, ret);
(gdb) continue
Continuing.
My PID is 8125, fork() returned 8133
We are in the parent process
[Inferior 1 (process 8125) exited normally]
(gdb) info inferiors
Num Description Executable
2 process 8133 /home/testuser/forktest
* 1 <null> /home/testuser/forktest
(gdb) inferior 2
[Switching to inferior 2 [process 8133] (/home/testuser/forktest)]
[Switching to thread 2 (process 8133)]
#0 0xb7fdd424 in ?? ()
(gdb) bt
#0 0xb7fdd424 in ?? ()
#1 0x08048494 in main () at forktest.cpp:17
(gdb) continue
Continuing.

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

As expected, GDB continued debugging the parent process. The child process remained suspended until we switched to it using the inferior command and resumed it using the continue command.

Now we will configure GDB to switch to the child process:

(gdb) set detach-on-fork off
(gdb) set follow-fork-mode child
(gdb) break func
Breakpoint 1 at 0x804844a: file forktest.cpp, line 7.
(gdb) run
Starting program: /home/testuser/forktest
[New process 8080]
[Switching to process 8080]

Breakpoint 1, func (pid=8080, ret=0) at forktest.cpp:7
7 printf("My PID is %d, fork() returned %dpid, ret);
(gdb) continue
Continuing.
My PID is 8080, fork() returned 0
We are in the child process[Inferior 2 (process 8080) exited normally]
(gdb) info inferiors
Num Description Executable
* 2 <null> /home/testuser/forktest
1 process 8077 /home/testuser/forktest
(gdb) inferior 1
[Switching to inferior 1 [process 8077] (/home/testuser/forktest)]
[Switching to thread 1 (process 8077)]
#0 0xb7fdd424 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fdd424 in __kernel_vsyscall ()
#1 0xb7ed4f7c in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/i386/../fork.c:131
#2 0x08048494 in main () at forktest.cpp:17
(gdb) continue
Continuing.

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

Now the parent process was suspended until we switched back to it and resumed it using the continue command.

Compatibility with VisualGDB

You can run the set detach-on-fork command under VisualGDB using the GDB Session window:

See also