Search code examples
gdbforkremote-debugginggdbserver

Unable to attach to forked child process with GDB


I have

gdbserver --multi :2345

running on remote system.

The code excerpt from myfile.c is following:

    159: mppts_number = index;

    //-------------------------- FORK-------------------------------------

    pid = fork();
    if (pid == -1)
    {
        //syslog(LOG_INFO,"Error: Start Daemon failed (%s)\n");

        return -1;
    }
    else if (!pid)
    {
        sleep(30);
        ...
    }
    else
    {
        return 0;
    }

Then I am connecting

(gdb) target extended-remote 192.168.10.248:2345
Remote debugging using 192.168.10.248:2345
(gdb) set remote exec-file a.out
(gdb) file a.out
Reading symbols from a.out...done.
(gdb) b 159
Breakpoint 1 at 0x11384: file myfile.c, line 159.
(gdb) r
Starting program: MYPATH/a.out
Reading ...
...
Breakpoint 1, main (argc=1, argv=0x7efff7c4) at myfile.c:159
159         mppts_number = index;
(gdb) n
163         pid = fork();
(gdb) n
164         if (pid == -1)
(gdb) p pid
$1 = 10459

Then if I do

(gdb) attach 10459
A program is being debugged already.  Kill it? (y or n) n
Not killed.
(gdb) continue
Continuing.
[Inferior 1 (process 10458) exited normally]
(gdb) attach 10459
Attaching to program: MYPATH, process 10459
Attaching to process 10459 failed

And I don't see any processes in the ps. In the middle I can see

# ps auxf | grep a.out
root     10458  0.0  0.1   1824   984 pts/0    t+   22:28   0:00  |           \_ a.out
root     10459  0.0  0.0      0     0 pts/0    Z+   22:28   0:00  |               \_ [a.out] <defunct>

So, I can't attach to child project. Can I still do this?


Solution

  • Ah, I need to do continue before fork, so no stops in the process occurred.