When using GDB for debugging, GDB can only track one process.
You can set the GDB debugging tool to track the parent or child processes by using directives before the fork function call.
By default, GDB tracks the parent process.
The Set follow-fork-mode Child command sets GDB to track the subprocess after fork.
The set Follow-fork-mode parent setting tracks the parental process.
Note: Be sure to set before the fork function call.
Program test:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (void)
{
pid_t pid;
printf ("You are my apple peeling");
PID = fork ();
if (pid==-1) {
Perror ("fork Error");
Exit (1);
}else if (pid = = 0) {
printf ("The child are here.") PID =%u,ppid =%u\n ", Getpid (), Getppid ());
}else{
printf ("Parent is here.") PID =%u,ppid =%u\n ", Getpid (), Getppid ());
Sleep (1);
}
return 0;
}
Compile, Debug:
Yu@ubuntu:~/cplusplus/fork Create process $ gcc fork.c-o fork-g
Yu@ubuntu:~/cplusplus/fork Create process $ gdb fork
To start debugging, set up the tracking child process:
(GDB) Set Follow-fork-mode child
(GDB) Start
Temporarybreakpoint 1 at 0x40071f:file fork.c, line 7.
Starting program:/home/yu/cplusplus/fork Create Process/fork
Temporary breakpoint 1, Main () at Fork.c:7
7 printf ("You are my apple peeling");
(GDB) n
You're my peeling apple.
9 pid = fork ();
(GDB) n
Parentis here. PID = 2170, Ppid = 2168
[Newprocess 2174]
[Switching to process 2174]///here has shifted the tracking subprocess program
Main () at Fork.c:10
Ten if (pid==-1) {
(GDB) n
}else if (pid = = 0) {
(GDB) n
printf ("The child are here.") PID =%u,ppid =%u\n ", Getpid (), Getppid ());
(GDB) C
Continuing.
The child are here. PID = 2174, Ppid = 1
[Inferior2 (Process 2174) exited normally]