Debugging a child process (fork 和 exec之后怎么做)

本文介绍了使用GDB调试创建额外进程的应用程序的方法。当程序使用fork()创建子进程时,默认情况下GDB继续调试父进程。为了调试子进程,可以在子进程代码中加入sleep()调用,并使用ps获取其PID,然后让GDB附加到该进程进行调试。此外,通过设置follow-fork-mode可以改变GDB对fork()调用的响应方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Debugging a child process

On most systems, GDB has no special support for debugging programs that create additional processes using the fork() function. By default, when a program forks, GDB continues to debug the parent process, while the child process runs unimpeded. If you set a breakpoint in any code that the child then executes, the child will get a SIGTRAP signal that causes it to terminate (unless it catches the signal).

To debug the child process, include a call to sleep() in the code that the child process executes after the fork(). It may be useful to sleep only if a certain environment variable is set, or a certain file exists, so that the delay doesn't occur when you don't want to run GDB on the child. While the child is sleeping, use the ps program to get its process ID, and then instruct GDB to attach to the child process (use a new invocation of GDB if you're also debugging the parent process). From that point on, you can debug the child process like any other process that you attach to.

The modes available are:

set follow-fork-mode mode
Set the debugger response to a program call of fork() or vfork(). A call to fork() or vfork() creates a new process. If you want to follow the child process instead of the parent process, use this command. The type can be one of the following:

parent — The original process is debugged after a fork(). The child process runs unimpeded. This type is the default.

child — The new process is debugged after a fork(). The parent process runs unimpeded.

ask — The debugger will prompt you for either parent or child.

show follow-fork-mode
Display the current debugger response to a fork() or vfork() call.

If you ask to debug a child process and a vfork() is followed by an exec(), GDB executes the new target up to the first breakpoint encountered in the new target. If there's a breakpoint set on main() in your original program, the breakpoint will also be set on the main() function for the child process.(解答了:fork 和 exec之后怎么做。)

When a child process is spawned by vfork(), you can't debug the child or parent until an exec() call completes.

If you issue a run command to GDB after an exec() call executes, the new target restarts. To restart the parent process, use the file command with the parent executable name as its argument.

You can use the catch command to make GDB stop whenever a fork(), vfork(), or exec() call is made.

摘自:http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=/com.qnx.doc.ide.userguide/topic/debug_debugging_Child_Process.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值