- 论坛徽章:
- 0
|
碰到多进程的程序如何调试呢?默认情况下,你next下来,跟的路径都是主进程的,而你想跟的子进程路径没跑到?怎么办呢?
有几种方法,今天看了看attach方法,觉得不错,特记录如下:
原理:运行多进程程序,得到要跟的子进程的ID;然后用开gdb,用attach+ID,然后stop,为什么要stop?防止子进程自己跑完,所以要stop,然后可以设断点,观测点,什么的。设完后,可以step,下一步,向下跟。
主要原理是这样。下面举个例子:
#include
int main()
{
if(fork() == 0)
{
int b = 9;
sleep(60);
int a =1;
int c = 90;
int d =5;
printf("child\n");
}
else
{
wait(NULL);
printf("parant\n");
}
return 0;
}
怎么样跟到子进程里面去 ?
1 后台运行该程序,可以得到进程ID
2 gdb下,attach+id
3 stop,然后设置断点,观察点等等
4 step
(gdb) attach 12606
Attaching to program: /home/purerain/test/f, process 12606
Symbols already loaded for /lib/tls/libc.so.6
Symbols already loaded for /lib/ld-linux.so.2
0xffffe002 in ?? ()
(gdb) stop
(gdb) b 12
Breakpoint 8 at 0x8048402: file fork.c, line 12.
(gdb) c
Continuing.
Breakpoint 7, main () at fork.c:11
11 int d =5;
(gdb) s
12 printf("child\n");
(gdb) s
19 return 0;
(gdb) s
20 }
(gdb) s
0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) s
Single stepping until exit from function __libc_start_main,
which has no line number information.
Program exited normally.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/98354/showart_1957352.html |
|