LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hi,
I am developing a two process program on RH linux 9. I am trying to debug the child process by following:
http://freebsd.active-venture.com/de...debugging.html

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

sleep(30);
do_sth();
exit(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/libthread_db.so.1".
Reading symbols from /usr/lib/libodbc.so.1...done.
Loaded symbols for /usr/lib/libodbc.so.1
............
Reading symbols from /lib/tls/libpthread.so.0...done.
[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/libz.so.1...done.
Loaded symbols for /usr/lib/libz.so.1
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
Member
 
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
ariana
LQ Newbie
 
Registered: Feb 2003
Posts: 22

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

testchild.cpp

#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 */
printf("error\n");
else if (pid == 0) { /* child */
int PauseMode = 1;

while (PauseMode)
sleep(5); /* Wait until someone attaches to us */
int i=0;
i++;
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
testchild
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
gdb
attach 15000
----output:
Attaching to process 17224
Reading symbols from /home/szou/program/testchild...done.
Using host libthread_db library "/lib/tls/libthread_db.so.1".
Reading symbols from /usr/lib/libstdc++.so.5...done.
Loaded symbols for /usr/lib/libstdc++.so.5
Reading symbols from /lib/tls/libm.so.6...done.
Loaded symbols for /lib/tls/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
0xffffe002 in ?? ()
(gdb) print PauseMode
No symbol "PauseMode" in current context.
(gdb) step
Cannot find bounds of current function
(gdb)

-------

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 AM.

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