我想调试一个cgi (fork-exec)
pid = fork();
if ( pid >= 0 )
{
if ( !pid )
{
cgi(fd, url, a3, data, count); <<- debug into internal
exit(0);
}
waitpid(pid, stat_loc, 0);
++count;
}
else
{
sub_1C45((unsigned int)fd);
}
,但我只能在cgi内部断点一次,因为cgi将退出,我的gdb将终止。但是父进程是活的。那么,有没有办法让gdb在子进程退出后返回父进程呢?
您将需要阅读有关多个劣质调试,不间断模式和set detach-on-fork off
(文档)的内容。
的例子:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
void cgi() { sleep(1); }
int main()
{
int pid = fork();
if (pid == 0) {
printf("child: %dn", getpid());
cgi();
} else {
printf("parent: waiting for: %dn", pid);
waitpid(pid, NULL, 0);
printf("parent: %d exitedn", pid);
}
return 0;
}
假设我想在子进程中的cgi()
例程和父进程中的waitpid()
例程之后停止。文字记录如下:
gdb -q a.out
Reading symbols from a.out...
(gdb) set non-stop on
(gdb) set detach-on-fork off
(gdb) b cgi
Breakpoint 1 at 0x117d: file t.c, line 5.
(gdb) b 16
Breakpoint 2 at 0x11f4: file t.c, line 16.
Starting program: /tmp/a.out
[New inferior 2 (process 2766133)]
Reading symbols from /tmp/a.out...
Reading symbols from /usr/lib/debug/.build-id/7b/9682d06533ba6fb9a8099004cbc6b17c1433f4.debug...
Reading symbols from /usr/lib/debug/.build-id/3a/5d7d8c50feeea2504b130bc627fef68364d927.debug...
parent: waiting for: 2766133
child: 2766133
Thread 2.1 "a.out" hit Breakpoint 1, cgi () at t.c:5
5 void cgi() { sleep(1); }
此时,子进程(下级线程2,线程1)在断点处停止。但那不是我们目前的劣势,所以我们必须改用它:
(gdb) info inferiors
Num Description Connection Executable
* 1 process 2766129 1 (native) /tmp/a.out
2 process 2766133 1 (native) /tmp/a.out
(gdb) inferior 2
[Switching to inferior 2 [process 2766133] (/tmp/a.out)]
[Switching to thread 2.1 (process 2766133)]
#0 cgi () at t.c:5
5 void cgi() { sleep(1); }
(gdb) c
Continuing.
[Inferior 2 (process 2766133) exited normally]
(gdb)
Thread 1.1 "a.out" hit Breakpoint 2, main () at t.c:16
16 printf("parent: %d exitedn", pid);
现在我们已经到达了父进程的断点。但是父进程(不再)是当前的次等进程,我们必须切换回它:
info inferiors
Num Description Connection Executable
1 process 2766129 1 (native) /tmp/a.out
* 2 <null> /tmp/a.out
(gdb) inferior 1
[Switching to inferior 1 [process 2766129] (/tmp/a.out)]
[Switching to thread 1.1 (process 2766129)]
#0 main () at t.c:16
16 printf("parent: %d exitedn", pid);
(gdb) c
Continuing.
parent: 2766133 exited
[Inferior 1 (process 2766129) exited normally]
(gdb) q