2015-11-19 64 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