Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-03-2005, 12:00 AM   #1
LQ Newbie
Registered: Feb 2003
Posts: 22

Rep: Reputation: 15
How to use gdb to debug child process?

I am developing a two process program on RH linux 9. I am trying to debug the child process by following:

In the main:
int main(){
if ((child_pid = fork()) < 0)
printf("######### error\n");
else if (child_pid == 0) {


void do_sth(){...}

After I start the program, I find out the child process id, and start gdb in another window.

# gdb
GNU gdb 6.2.1
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you ......

(gdb) attach 19495
Attaching to process 19495
Using host libthread_db library "/lib/tls/".
Reading symbols from /usr/lib/
Loaded symbols for /usr/lib/
Reading symbols from /lib/tls/
[Thread debugging using libthread_db enabled]
[New Thread 1077073120 (LWP 19492)]
Error while reading shared library symbols:
Can't attach LWP 19492: No such process

Reading symbols from /usr/lib/
Loaded symbols for /usr/lib/
0xffffe002 in ?? ()
(gdb) b main.cpp:200 //this is in do_sth()
Breakpoint 1 at 0x809b7cd: file ../../main.cpp, line 200.


In the other window which the program is started, the process just hang there. Could anyone let me know what could be the problem and what should be done? Any suggestion is appreciated.
Old 05-03-2005, 12:30 PM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
The parent is not necessarily hung.

Use the step command to get the parent to execute the fork().

Attach to the child.

You should then be able to go back the the parent (in gdb) and step along inside it.
Old 05-03-2005, 01:50 PM   #3
LQ Newbie
Registered: Feb 2003
Posts: 22

Original Poster
Rep: Reputation: 15
Thank you very much for your reply. I am now trying to get the example below from the url to work first.


#include <sys/resource.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/file.h>
#include <string>

int main(){
int pid;
if ((pid = fork()) < 0) /* _Always_ check this */
else if (pid == 0) { /* child */
int PauseMode = 1;

while (PauseMode)
sleep(5); /* Wait until someone attaches to us */
int i=0;
printf("i=%d\n", i);
} else {
int j=0;
printf("In parent\n");

return 0;


What I did was:
1. compile .cpp to testchild using g++ -o testchild testchild.cpp
2. In one window, type in
So the program start to run. At this time, parent exited.
3. In same window, use ps -ef to find out child pid (i.e 15000)
4. Start another window, type in
attach 15000
Attaching to process 17224
Reading symbols from /home/szou/program/testchild...done.
Using host libthread_db library "/lib/tls/".
Reading symbols from /usr/lib/
Loaded symbols for /usr/lib/
Reading symbols from /lib/tls/
Loaded symbols for /lib/tls/
Reading symbols from /lib/
Loaded symbols for /lib/
Reading symbols from /lib/tls/
Loaded symbols for /lib/tls/
Reading symbols from /lib/
Loaded symbols for /lib/
0xffffe002 in ?? ()
(gdb) print PauseMode
No symbol "PauseMode" in current context.
(gdb) step
Cannot find bounds of current function


I don't know how to go from here. When should I set breakpoint, etc. Thank you for your help in advance.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Using gdb to debug different arhitectures george_mercury Linux - Software 1 01-07-2005 06:19 PM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
how to debug multithread using GDB? ryanux Programming 1 05-11-2004 12:58 AM
How to debug a child process? koyi Programming 3 09-11-2003 05:23 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration