GDB Debug Fork Program settings tracking

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.