c - 调试劣质进程

标签 c debugging unix operating-system gdb

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

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

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

最佳答案

您可能会在 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

关于c - 调试劣质进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33811272/

相关文章:

c++ - Win32 中的全局(进程范围)属性

c - 为什么我转换为float时gcc总是报错?

php - 当使用 CPPFLAGS 指定目录时./configure 无法找到头文件

perl - 将相对路径转换为绝对路径?

javascript - 我的 useState 有问题吗?我觉得我的 Array ReactNative 中的值不正确

unix - powershell中等效的unix test -f命令是什么?

c - 双重释放或损坏(fasttop)错误

C 程序 - 求 2 的指数的最简单方法是什么

c - GDB调试中的问题

c - 如果 vector 太大,我的程序会崩溃吗?