https://sourceware.org/gdb/onlinedocs/gdb/Forks.html
https://sourceware.org/gdb/onlinedocs/gdb/Forks.html
Next: Checkpoint/Restart, Previous: Threads, Up: Running [Contents][Index]
4.11 Debugging Forks
On most systems, GDB has no special support for debugging programs which create additional processes using the fork
function. When a program forks, GDB will continue to debug the parent process and the child process will run unimpeded. If you have set a breakpoint in any code which the child then executes, the child will get a SIGTRAP
signal which (unless it catches the signal) will cause it to terminate.
However, if you want to debug the child process there is a workaround which isn’t too painful. Put a call to sleep
in the code which 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 need not 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. Then tell GDB (a new invocation of GDB if you are also debugging the parent process) to attach to the child process (see Attach). From that point on you can debug the child process just like any other process which you attached to.
On some systems, GDB provides support for debugging programs that create additional processes using the fork
or vfork
functions. On GNU/Linux platforms, this feature is supported with kernel version 2.5.46 and later.
The fork debugging commands are supported in native mode and when connected to gdbserver
in either target remote
mode or target extended-remote
mode.
By default, when a program forks, GDB will continue to debug the parent process and the child process will run unimpeded.
If you want to follow the child process instead of the parent process, use the command set follow-fork-mode
.
set follow-fork-mode mode
-
Set the debugger response to a program call of
fork
orvfork
. A call tofork
orvfork
creates a new process. The mode argument can be:parent
-
The original process is debugged after a fork. The child process runs unimpeded. This is the default.
child
-
The new process is debugged after a fork. The parent process runs unimpeded.
show follow-fork-mode
-
Display the current debugger response to a
fork
orvfork
call.
On Linux, if you want to debug both the parent and child processes, use the command set detach-on-fork
.
set detach-on-fork mode
-
Tells gdb whether to detach one of the processes after a fork, or retain debugger control over them both.
on
-
The child process (or parent process, depending on the value of
follow-fork-mode
) will be detached and allowed to run independently. This is the default. off
-
Both processes will be held under the control of GDB. One process (child or parent, depending on the value of
follow-fork-mode
) is debugged as usual, while the other is held suspended.
show detach-on-fork
-
Show whether detach-on-fork mode is on/off.
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 (see Debugging Multiple Inferiors and Programs).
To quit debugging one of the forked processes, you can either detach from it by using the detach inferiors
command (allowing it to run independently), or kill it using the kill inferiors
command. See Debugging Multiple Inferiors and Programs.
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 in the new target. If you have a breakpoint set on main
in your original program, the breakpoint will also be set on the child process’s main
.
On some systems, when a child process is spawned by vfork
, you cannot 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. By default, after an exec
call executes, GDB discards the symbols of the previous executable image. You can change this behaviour with the set follow-exec-mode
command.
set follow-exec-mode mode
-
Set debugger response to a program call of
exec
. Anexec
call replaces the program image of a process.follow-exec-mode
can be:new
-
GDB creates a new inferior and rebinds the process to this new inferior. The program the process was running before the
exec
call can be restarted afterwards by restarting the original inferior.For example:
(gdb) info inferiors
(gdb) info inferior
Id Description Executable
* 1 <null> prog1
(gdb) run
process 12020 is executing new program: prog2
Program exited normally.
(gdb) info inferiors
Id Description Executable
1 <null> prog1
* 2 <null> prog2 same
-
GDB keeps the process bound to the same inferior. The new executable image replaces the previous executable loaded in the inferior. Restarting the inferior after the
exec
call, with e.g., therun
command, restarts the executable the process was running after theexec
call. This is the default mode.For example:
(gdb) info inferiors
Id Description Executable
* 1 <null> prog1
(gdb) run
process 12020 is executing new program: prog2
Program exited normally.
(gdb) info inferiors
Id Description Executable
* 1 <null> prog2
follow-exec-mode
is supported in native mode and target extended-remote
mode.
You can use the catch
command to make GDB stop whenever a fork
, vfork
, or exec
call is made. See Setting Catchpoints.
Next: Checkpoint/Restart, Previous: Threads, Up: Running [Contents][Index]
https://sourceware.org/gdb/onlinedocs/gdb/Forks.html的更多相关文章
- 【转载】【GDB】GDB with Python
作者:薛定谔的喵链接:https://zhuanlan.zhihu.com/p/152274203来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 你还在用GDB调试程序 ...
- GDB和GDB Server
gdb是linux c编程标配的调试工具,平时接触比较多的可能是本机随gcc一起安装的调试工具.但是,即使是本机的gdb,也经常被printf代替,所以接触也仅限于知道. 简单程序固然可以用print ...
- 27 Debugging Go Code with GDB 使用GDB调试go代码
Debugging Go Code with GDB 使用GDB调试go代码 Introduction Common Operations Go Extensions Known Issues Tu ...
- linux包之gdb之gdb命令与core文件产生
gdb-7.2-64.el6_5.2.x86_64/usr/bin/gcore/usr/bin/gdb/usr/bin/gdb-add-index/usr/bin/gdbtui/usr/bin/gst ...
- Debugging with GDB 用GDB调试多线程程序
Debugging with GDB http://www.delorie.com/gnu/docs/gdb/gdb_25.html GDB调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为 ...
- GDB Core,gdb 调试大全,core文件调试
编译: gcc -g -o hello hello.c gdb 调试: 基本 gdb 命令. 命 令 描 述 小结:常用的gdb命令 backtrace 显示程序中的当前位置和表示如何到达当前位置的栈 ...
- 如何写gdb命令脚本
作为UNIX/Linux下使用广泛的调试器,gdb不仅提供了丰富的命令,还引入了对脚本的支持:一种是对已存在的脚本语言支持,比如python,用户可以直接书写python脚本,由gdb调用python ...
- [Z] 用GDB调试程序
原文:http://blog.csdn.net/haoel/article/details/2879 用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工 ...
- Android gdb so
gdb debug an android application 1.gdb 要有gdbserver 一般模拟器默认装有gdbserver,如2.3.3的模拟器,看一下有没有: D:\Develope ...
随机推荐
- 用Zebra打造Linux下小型路由器
用Zebra打造Linux下小型路由器 现在的Internet网络相当庞大,不可能在不同的网络之间建立直接的连接,所以这时就必须用路由器为不同网络之间的通信提供路径选择.Linux下搭建路由器价格非常 ...
- React项目编译node内存溢出
坑爹的node 内存溢出 react开发项目 安装一个插件依赖 ,然后就报错了 报错如下(自己的没有截图出来 这是从别人的截图---报错基本差不多) 之前因为项目大而且旧的原因 使用 过 ...
- BZOJ3676: [Apio2014]回文串(SAM+Manacher/PAM)
Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...
- django 简单会议室预约(6)
后台完了现在来看前端,前端用了一个bootstrap框架,看起来能好看点 先看一下文件结构:在djapp里创建了两个文件夹templates和static templates里面是要显示的页面,sta ...
- asp.net Code学习二(使用vs 2015 update 3)
1.在vs 2015上搭建asp.net core: 安装 .Net core sdk.vs2015 tool 即可使用vs 2015开发asp.net core. 2.Net core中国学习小组 ...
- activity-栈相关属性
1.启动任务栈 第一种,动作设置为“android.intent.action.MAIN”,类别设置为“android.intent.category.LAUNCHER”,可以使这个ACT(activ ...
- JS学习笔记 - fgm练习 - 数字自增 定时器 数字比大小Math.max
<script> window.onload = function(){ var oP = document.getElementsByTagName('p')[0]; var i = 0 ...
- 【CS Round #43 D】Bad Triplet
[链接]点击打开链接 [题意] 给你n个点m条边的无权无向联通图; 让你找3个点A,B,C 使得A->B=B->C=A->C 这里X->Y表示点X到点Y的最短路长度. [题解] ...
- HTML中select的option设置selected="selected"无效的解决方式
今天遇到了一个奇葩问题,写HTML时有个select控件.通过设置option的selected="selected"竟然无效,可是在其它浏览器是能够的.问了一下Google大神, ...
- php字符串函数分类总结
php字符串函数分类总结 一.总结 explode str_split str_word_count strtolower 二.php字符串函数分类总结 php内置了98个字符串函数(除了基于正 ...