【gdb】为fork调用设置catchpoint
为fork调用设置catchpoint
1.例子:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
exit(1);
}
else if (pid > 0)
{
exit(0);
}
printf("hello world\n");
return 0;
}
使用gdb调试程序时,可以用catch fork
命令为fork
调用设置catchpoint
,以上面程序为例:
[root@node01 demo]# gcc demo.c -g
[root@node01 demo]# gdb a.out -q
Reading symbols from /root/demo/a.out...done.
(gdb) catch fork
Catchpoint 1 (fork)
(gdb) r
Starting program: /root/demo/a.out
Catchpoint 1 (forked process 3242844), 0x00007ffff7ad2b12 in __libc_fork ()
at ../nptl/sysdeps/unix/sysv/linux/fork.c:136
136 pid = ARCH_FORK ();
(gdb) bt
#0 0x00007ffff7ad2b12 in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/fork.c:136
#1 0x00000000004005ca in main () at demo.c:10
(gdb)