无法使用 GDB 附加到分叉的子进程

Unable to attach to forked child process with GDB

我有

gdbserver --multi :2345

运行 在远程系统上。

myfile.c 的代码摘录如下:

    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;
    }

那我正在连接

(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
 = 10459

那么如果我这样做

(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

而且我在 ps 中没有看到任何进程。在中间我可以看到

# 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>

所以,我无法附加到子项目。我还能这样做吗?

啊,我需要在 fork 之前做 continue,所以过程中没有停止。