Discussion:
debugging child process with GDB
(too old to reply)
Roman Mashak
2005-07-29 01:38:14 UTC
Permalink
Hello, All!

Investigating 'gdb' work I ran across the problem. My simple application
forks the child and I'd like to debug the child with GDB.
So, I run it as 'gdb child PID'. set breakpoint for a function from GDB
console, but I can't make single stepping. I get the error: "Cannot find
bounds of current function".

Should I do something else?

Thanks.

With best regards, Roman Mashak. E-mail: ***@tusur.ru
Maxim Yegorushkin
2005-07-29 07:10:02 UTC
Permalink
Post by Roman Mashak
Hello, All!
Investigating 'gdb' work I ran across the problem. My simple application
forks the child and I'd like to debug the child with GDB.
So, I run it as 'gdb child PID'. set breakpoint for a function from GDB
console, but I can't make single stepping. I get the error: "Cannot find
bounds of current function".
Should I do something else?
Reading info gdb "4.10 Debugging programs with multiple processes"
might help you.
See follow-fork-mode.
Roman Mashak
2005-07-29 09:00:00 UTC
Permalink
Hello, Maxim!
You wrote on 29 Jul 2005 00:10:02 -0700:

MY> Reading info gdb "4.10 Debugging programs with multiple processes"
MY> might help you.
MY> See follow-fork-mode

Thanks for reply, I checked through the documentation, but setting
'follow-fork-mode' didn't work for me, may be problem is version of GDB
(it's 5.3post-0.20021129.18rh) ? I still can't follow the child's code
after fork(). Even with this simple example:

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int pid; /* process PID */
int child, parent;
int status;

/* create new process to handle client */
if ( (pid = fork()) == -1 ) {
perror("fork() error");
exit(1);
}

/* here is child process running */
if (pid == 0) {
printf("child process, pid=%d ppid=%d\n", getpid(), getppid());
int i;
while (i) {
fprintf( stdout, "i=%d\n",(i == 100000) ? i-- : i++ );
}
exit(10);
}

/* here is parent process running */
else {
/* wait until child has exited */
pid = wait(&status);
printf("child process %d terminated with status %d\n", pid,
WEXITSTATUS(status));
}

return 0;
}


With best regards, Roman Mashak. E-mail: ***@tusur.ru
Maxim Yegorushkin
2005-07-29 11:28:42 UTC
Permalink
Post by Roman Mashak
Hello, Maxim!
MY> Reading info gdb "4.10 Debugging programs with multiple processes"
MY> might help you.
MY> See follow-fork-mode
Thanks for reply, I checked through the documentation, but setting
'follow-fork-mode' didn't work for me, may be problem is version of GDB
(it's 5.3post-0.20021129.18rh) ? I still can't follow the child's code
It may be your gdb.

I compiled it

$ g++ -Wall -ggdb -c exp.cpp -o exp.o

and run

$ gdb exp

Current directory is /home/max/src/exp/
GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host
libthread_db library "/lib/libthread_db.so.1".

(gdb) l main
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7
8 int main(void)
9 {
10 int pid; /* process PID */
11 int child, parent;
12 int status;
(gdb)
13
14
15 /* create new process to handle client */
16 if ( (pid = fork()) == -1 ) {
17 perror("fork() error");
18 exit(1);
19 }
20
21
22 /* here is child process running */
(gdb)
23 if (pid == 0) {
24 printf("child process, pid=%d ppid=%d\n", getpid(), getppid());
25 int i;
26 while (i) {
27 fprintf( stdout, "i=%d\n",(i == 100000) ? i-- : i++ );
28 }
29 exit(10);
30 }
31
32
(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "parent".
(gdb) set follow-fork-mode child
(gdb) b 24
Breakpoint 1 at 0x8048610: file exp.cpp, line 24.
(gdb) r
Starting program: /home/max/src/exp/exp
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0xa42000
Attaching after fork to child process 9999.
[Switching to process 9999]

Breakpoint 1, main () at exp.cpp:24
(gdb) q
The program is running. Exit anyway? (y or n) y

Debugger finished
Roman Mashak
2005-07-29 12:01:09 UTC
Permalink
Hello, Maxim!
You wrote on 29 Jul 2005 04:28:42 -0700:


MY> It may be your gdb.

MY> I compiled it

MY> $ g++ -Wall -ggdb -c exp.cpp -o exp.o

MY> and run

MY> $ gdb exp

MY> Current directory is /home/max/src/exp/
MY> GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
MY> Copyright 2004 Free Software Foundation, Inc.
[skip]

I've got two questions:
1) did you use any special options to build your version of GDB
2) as I understood yyou put breakpoint in my example in the child code,
right?

With best regards, Roman Mashak. E-mail: ***@tusur.ru
Maxim Yegorushkin
2005-07-29 14:22:57 UTC
Permalink
Post by Roman Mashak
Hello, Maxim!
MY> It may be your gdb.
MY> I compiled it
MY> $ g++ -Wall -ggdb -c exp.cpp -o exp.o
MY> and run
MY> $ gdb exp
MY> Current directory is /home/max/src/exp/
MY> GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
MY> Copyright 2004 Free Software Foundation, Inc.
[skip]
1) did you use any special options to build your version of GDB
No, I did not build it, I just made full Fedora Core 4 install. :)
Post by Roman Mashak
2) as I understood yyou put breakpoint in my example in the child code,
right?
Exactly.
Roman Mashak
2005-07-30 00:08:07 UTC
Permalink
Hello, Maxim!
You wrote on 29 Jul 2005 07:22:57 -0700:

MY> No, I did not build it, I just made full Fedora Core 4 install. :)

??>> 2) as I understood yyou put breakpoint in my example in the child
??>> code, right?
As I suspected, problem was with my version of GDB. After I upgraded it to
6.3 everything is fine. Thanks for tip ;)

With best regards, Roman Mashak. E-mail: ***@tusur.ru

Loading...