如何在 GDB 中调试 fork-exec 进程的入口点? [英] How to debug the entry-point of fork-exec process in GDB?

查看:18
本文介绍了如何在 GDB 中调试 fork-exec 进程的入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C linux 应用程序 (A),它在启动时会生成另一个进程 (P).当我想调试 P 时,我像往常一样启动 A,然后用 ddd/gdb 连接到 P.

I have a C linux application (A) that spawns another process (P) when it is started. When I want to debug P I start A as usual and I connect with ddd/gdb to P.

当我想调试 P 的入口点(main 的开始)时出现问题.如果我在将调试器连接到 P 时遵循通常的方法已经为时已晚.我找到的解决方案是在 P 的 main 开头插入一个 sleep ,这样我就有时间连接 gdb 但这不是一个非常优雅的解决方案.

Problems appear when I want to debug the entry-point (start of main) of P. If I follow the usual approach when I connect the debugger to P is already to late. The solution I've found was to insert a sleep at the begining of the main of P so I have time to connect with gdb but this is not a very elegant solution.

我也尝试过使用 asm("int $3") 但它似乎不起作用.

I've also tried using asm("int $3") but it doesn't seems to work.

你知道我该如何解决这个问题吗?(最好不要改变A或P的代码)

Do you have any idea how I could solve this problem? (preferably without altering the code of A or P)

推荐答案

你应该使用这个选项:

set follow-fork-mode mode

其中 modeparentchildask 之一.

Where mode is one of parent, child or ask.

要跟随父级(这是默认设置)使用:

To follow the parent (this is the default) use:

set follow-fork-mode parent

跟随孩子:

set follow-fork-mode child

让调试器每次都询问您:

To have the debugger ask you each time:

set follow-fork-mode ask

所以基本上你会开始将 gdb 连接到 A,然后设置 gdb 跟随孩子,然后当 A 生成 P 时,gdb 将连接到 P 并与 A 分离.

So basically you'd start out connecting gdb to A, then set gdb to follow the child, and then when A spawns P, gdb will connect to P and detach from A.

这篇关于如何在 GDB 中调试 fork-exec 进程的入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆