【问题标题】:Debug inferior process调试劣质进程
【发布时间】:2015-11-19 18:07:18
【问题描述】:

您好,我正在做一些高级编程 Unix 系统的练习。我对 forkexeclp 函数的工作原理很感兴趣。作者从文本中指定 fork 创建了一个新进程。它被调用一次 - 由父级调用 - 但返回两次 - 在父级和子级中。

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

1 - 如果我设置断点 - else 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);
}

【问题讨论】:

    标签: c debugging unix operating-system gdb


    【解决方案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
    

    【讨论】:

    • 感谢 OznOg……您的建议在我的 linux VM 上完美运行,但是当我在 OS X 环境中时它不起作用。从文档中我看到以下内容:“在大多数系统上,gdb 没有特别支持调试使用 fork 函数创建附加进程的程序。当程序分叉时,gdb 将继续调试父进程,子进程将不受阻碍地运行. 如果您在子程序执行的任何代码中设置了断点,子程序将收到一个 SIGTRAP 信号(除非它捕获该信号)将导致它终止。"
    • 不客气,很遗憾它在 osX 上不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多