调试劣质流程

时间:2015-11-19 18:07:19

标签: c debugging unix operating-system gdb

您好我正在通过Advanced Programming Unix System进行一些练习。我对 fork execlp 功能的工作原理感兴趣。在文本中,作者指定 fork 创建一个新进程。它被父级调用一次 - 但在父级和子级中返回两次。

所以 fork 会向父级返回非负pid,并向子级返回0。我想通过GDB逐步调用这一系列的调用,但是我的断点会导致子进程无法运行或中断导致父进程终止的系统调用。

1 - 如果我设置了一个断点 - 否则if(pid == 0) - >这个过程没有运行。

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

我得到以下错误:

waitpid错误:系统调用中断 [Inferior 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);
}

1 个答案:

答案 0 :(得分:1)

您可以在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