LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use gdb to debug child process? (https://www.linuxquestions.org/questions/programming-9/how-to-use-gdb-to-debug-child-process-319584/)

ariana 05-03-2005 12:00 AM

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.

jim mcnamara 05-03-2005 12:30 PM

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.

ariana 05-03-2005 01:50 PM

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.


All times are GMT -5. The time now is 10:14 AM.