2015-11-19 36 views
1

嗨我正在从高级编程Unix系统的一些练习。我感兴趣的是分叉execlp功能的作品。从作者指定的文字分叉创建一个新的过程。它被父母和子女调用一次 - 由父母调用 - 但返回两次。调试劣质进程

所以返回一个非负pid给父母,0给孩子。我想用GDB逐步调用这一系列调用,但是我的断点会导致孩子不运行或中断造成父母终止的系统调用。

1 - 如果我设置了一个断点 - 否则if(pid == 0) - >该进程不运行。

2 - 如果我设置了一个断点 - execlp(buf,buf,(char *)0);

我将得到以下错误:

waitpid函数错误:中断系统调用 [劣质1(处理461)退出,代码为01]

做什么选择我都设置在GDB调试父母和孩子?应该在哪里设置断点?

int main(int argc, char *argv[]) 
{ 
    char buf[MAXLINE]; 
    pid_t pid; 
    int status; 

    printf("%% "); 

    while(fgets(buf, MAXLINE, stdin) != NULL) 
    { 
     if(buf[strlen(buf) - 1] == '\n') 
      buf[strlen(buf) - 1] = 0; 
     if((pid = fork()) < 0) 
     { 
      err_sys("fork error"); 
     } 
     else if(pid == 0) 
     { 
      execlp(buf, buf, (char *)0); 
      err_ret("could'nt execute: %s", buf); 
      exit(127); 
     } 
     if((pid = waitpid(pid, &status, 0)) < 0) 
      err_sys("waitpid error"); 
     printf("%% "); 
    } 
    exit(0); 
} 

回答

0

您可能会发现GDB文档中一些帮助: https://sourceware.org/gdb/onlinedocs/gdb/Forks.html

我想你可以设置set detach-on-fork off跟踪的子进程也是如此。

你便可以把断点fork的同时看到通话结束

这里我的输出:

$ gdb ./a.out 
GNU gdb (GDB) 7.6-6.mga4 (Mageia release 4) 
Copyright (C) 2013 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "x86_64-mageia-linux-gnu". 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>... 
Reading symbols from /tmp/a.out...done. 
(gdb) set detach-on-fork off 
(gdb) b fork 
Breakpoint 1 at 0x400710 
(gdb) r 
Starting program: /tmp/a.out 
% dddd 

Breakpoint 1, 0x00007ffff7ae0e04 in fork() from /lib64/libc.so.6 
Missing separate debuginfos, use: debuginfo-install glibc-2.18-9.11.mga4.x86_64 
(gdb) bt 
#0 0x00007ffff7ae0e04 in fork() from /lib64/libc.so.6 
#1 0x0000000000400880 in main (argc=1, argv=0x7fffffffdc38) at delme.c:19 
(gdb) info inferior 
    Num Description  Executable   
* 1 process 8272  /tmp/a.out   
(gdb) n 
Single stepping until exit from function fork, 
which has no line number information. 
[New process 8287] 
main (argc=1, argv=0x7fffffffdc38) at delme.c:23 
23    else if(pid == 0) 
Missing separate debuginfos, use: debuginfo-install glibc-2.18-9.11.mga4.x86_64 
(gdb) info inferior 
    Num Description  Executable   
    2 process 8287  /tmp/a.out   
* 1 process 8272  /tmp/a.out   
(gdb) p pid 
$1 = 8287 
(gdb) inferior 2 
[Switching to inferior 2 [process 8287] (/tmp/a.out)] 
[Switching to thread 2 (process 8287)] 
#0 0x00007ffff7ae0eac in fork() from /lib64/libc.so.6 
(gdb) n 
Single stepping until exit from function fork, 
which has no line number information. 
main (argc=1, argv=0x7fffffffdc38) at delme.c:23 
23    else if(pid == 0) 
(gdb) p pid 
$2 = 0 
+0

感谢OznOg ......你的建议工程完全在我的Linux虚拟机,但它不工作时,我在OS X环境中。从文档中我看到以下内容:“在大多数系统上,gdb没有特别的支持来调试使用fork函数创建额外进程的程序。当程序分叉时,gdb将继续调试父进程并且子进程不受阻碍地运行。如果你已经在任何代码中设置了一个断点,那么这个孩子将会得到一个SIGTRAP信号(除非它捕获到信号)将导致它终止。“ – dcrearer

+0

不客气,很遗憾地看到它在osX上无法正常工作。 – OznOg