【问题标题】:Debugging child process not working with set detach-on-fork off调试子进程不使用 set detach-on-fork off
【发布时间】:2019-07-14 01:44:33
【问题描述】:

我有以下简单的程序。

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

#define CHILD 0

int main()
{
    pid_t pid;
    printf(" My pid = %d \n", getpid());
    getchar();
    pid = fork();
    if( pid == CHILD){
        printf(" child: My pid = %d \n", getpid());
        while(1);
    }
    else{
        printf(" My pid = %d \n", getpid());
        printf(" Newly created child pid = %d \n", pid);
        while(1);
    }
}

我正在尝试使用 gdb 对其进行调试。我想调试父进程和子进程。我使用了以下命令

Reading symbols from ./1...done.
(gdb) b 12
Breakpoint 1 at 0x73f: file 1.c, line 12.
(gdb) set detach-on-fork off
(gdb) r
Starting program: ./1 
 My pid = 121710 


Breakpoint 1, main () at 1.c:12
12      pid = fork();
(gdb) n
[New process 121715]
Reading symbols from ./1...done.
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x73f

(gdb) info inferior
  Num  Description       Executable        
* 1    process 121710    ./1 
  2    process 121715    ./1 
(gdb) bt
#0  0x00007ffff7ac8b1c in __libc_fork () at ../sysdeps/nptl/fork.c:135
#1  0x0000555555554744 in main () at 1.c:12
(gdb) n
[New process 121715]
Reading symbols from ./1...done.
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x73f
Cannot insert breakpoint 1.
Cannot access memory at address 0x73f

你们能帮我解决我在上述 gdb 命令中所犯的错误吗?

【问题讨论】:

  • “我想同时调试父进程和子进程” - 我认为你不能两者都做,但我可能是错的。我认为您需要选择父母或孩子。您可能还需要set follow-fork-mode parentset follow-fork-mode child。另请参阅 GDB 手册中的4.11 Debugging Forks
  • 我不是指同时调试两者。我想停止其中一个进程,然后使用劣质命令在它们之间移动
  • 从地址来看,你很可能使用的是 Linux/x86_64。我无法重现您使用 gdb-8.2 或 8.3 观察到的行为。
  • 你在gdb中运行了上面的代码以及同样的命令

标签: c linux debugging gdb fork


【解决方案1】:
     i got the same  problem  when i use gdb 8.1 to debug fork(), with same debug option.
     one of  solution is that delete all  breakpoint before calling fork(). for example ,when you create a breakpoint in previous source line of fork() for stepping to fork(),

你应该删除当前inferior停在这个断点之后的所有断点,然后currentferior可以继续成功运行,不会出现'cannot insert breakpoint……'这样的错误提示。

【讨论】:

  • 欢迎来到 StackOverflow。 StackOverflow 不是讨论形式。如果您要发布答案,请确保它是一个答案。请阅读this article 以更好地了解答案是什么。也请浏览网站tour
猜你喜欢
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 2018-05-16
  • 1970-01-01
相关资源
最近更新 更多