简体   繁体   English

Linux上的gdb fork()exec

[英]gdb fork() exec on Linux

I found the similar question on this site, but the solution doesn't work for me. 我在这个网站上发现了类似的问题,但解决方案对我不起作用。 In my main(), I have 在我的主()中,我有

if(fork() == 0) execl(program b, args); if(fork()== 0)execl(program b,args);

I set couple break points at every beginning. 我在每个开始都设置了几个断点。 Then I use "set follow-fork-mode" and run. 然后我使用“set follow-fork-mode”并运行。

After fork, gdb attached to the child process, showing "Attaching after fork to child process 29730." fork之后,gdb附加到子进程,显示“在fork到子进程29730之后附加”。 But I won't have any chance to set break points in program b anymore, although I have 15 seconds sleep in program b. 但是我不会再有机会在程序b中设置断点,虽然我在程序b中有15秒睡眠。 It executed until end or break. 它执行到结束或休息。

How to set the break point in program b? 如何在程序b中设置断点?

Thanks! 谢谢!

But I won't have any chance to set break points in program b anymore 但是我再也没有机会在程序b中设置断点了

The trick is to set a breakpoint in the child as a delayed breakpoint. 诀窍是将子节点中的断点设置为延迟断点。 When the child is execl() d, GDB will set the breakpoint in it. 当子execl()execl() d时,GDB会在其中设置断点。 Example: 例:

// a.c
#include <unistd.h>

int main()
{
  if (0 == fork()) execl("./b.out", "b.out", (char*)0);
  return 0;
}


// b.c
int foo() { return 0; }

int main() { return foo(); }


gcc -g a.c; gcc -g b.c -o b.out


gdb -nx -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) b foo
Function "foo" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (foo) pending.

The breakpoint is pending because there is no foo() in a.out (but there is foo() in b.out ). 断点正在等待,因为a.out没有foo() (但是b.outfoo() )。

(gdb) set follow-fork-mode child
(gdb) run
Starting program: /tmp/a.out 
[New process 18759]
process 18759 is executing new program: /tmp/b.out
[Switching to process 18759]

Breakpoint 1, foo () at b.c:1
1   int foo() { return 0; }
(gdb) bt
#0  foo () at b.c:1
#1  0x00000000004004dd in main () at b.c:3
(gdb) quit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM