您的位置:首页 > 其它

使用 GDB 调试多进程程序--2

2013-06-07 10:18 155 查看
程序经常使用fork/exec创建多进程程序。多进程程序有自己独立的地址空间,这是多进程调试首要注意的地方。Gdb功能强大,对调试多线程提供很多支持。

方法1:调试多进程最土的办法:attach pid

Attach是调试进程的常用办法,只要有可执行程序以及相应PID,即可工作。当然,为方便调试,可以在进程启动后,设定sleep一段时间,如30s,这样即可有充足的时间来attach。

方法2: set follow-fork-mode child + main断点

当设置set follow-fork-mode child,gdb将在fork之后直接执行子进程,知道碰到断点后停止。如何设置子进程的断点呢?在父进程中是无法知道子进程的地址空间的(只有等程序载入后方可知)。Gdb提供一个很方便的机制:main函数的断点将被子进程继承(毕竟main是任何程序的入口)。
注意:程序在main停下后,可尝试设置断点。断点是否有效,取决于gdb是否已经载入目标程序的地址空间。

方法3: set follow-fork-mode child + catch exec

Cache点是一种特殊的breakpoint。Gdb能够catch的事件很多,如
throw/catch/exception/syscall/exec/fork/vfork
等。其中和多进程关系最大的就是
exec/fork
事件。

举例:


?

说明:
catch exec
后,程序将在
fork/vfork/exec
处停下。并非每次停下后,设置断点都是有效的。如提供断点无效,需要删除,否则程序无法继续。要能够在新进程中设置断点,一定要等到新进程的地址空间被载入后,设置断点是才有效
(exec
将改变原程序的地址空间
)
。上述例子,主要想展示如何对新进程设置断点!

注意:

1)
程序地址非常重要
(
代码和数据地址一样重要
)
。使用
gdb
时,多多注意和利用地址信息。

2)
On some systems, when a child process is spawned by vfork, you cannot debug the child or parent until an exec call completes.

方法
4
info inferiors/inferiors inferiors

设置
set detach-on-fork off/set follow-exec-mode new

If you choose to set `detach-on-fork' mode off, then gdb will retain control of all forked processes (including nested forks). You can list the forked processes under the control of gdb by using the
info inferiors
command,
and switch from one fork to another by using the
inferior
command.
所使用的gdb不支持
set detach-on-fork off/set follow-exec-mode new/info inferiors
。不清楚。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: