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
forkorvfork. A call toforkorvforkcreates 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
forkorvforkcall.
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. Anexeccall replaces the program image of a process.follow-exec-modecan be:new-
GDB creates a new inferior and rebinds the process to this new inferior. The program the process was running before the
execcall 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
execcall, with e.g., theruncommand, restarts the executable the process was running after theexeccall. 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 ...
随机推荐
- HTML5手机应用的最大优势就是可以在网页上直接调试和修改
HTML5手机应用的最大优势就是可以在网页上直接调试和修改
- ORA-10458: standby database requires recovery
搭建DG最后一步打开时报错如下: SQL> alter database open read only; alter database open read only * ERROR at l ...
- 数组-reduce方法
转自: https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/139 实现 convert 方法,把原始 list ...
- BZOJ3277: 串(后缀自动机,Parent树,Dfs序)
Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中 至少k个字符串的子串(注意包括本身). Input 第一行两个整数n, ...
- BZOJ3620: 似乎在梦中见过的样子(KMP)
Description “Madoka,不要相信 QB!”伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个噩梦,也同时是上个轮回中所发生的事.为了使 ...
- Office2010激活工具
mini-KMS Activator是一款好用Office2010激活工具,“KMS”是微软对于“大客户”(VOL或VL)提供的Microsoft产品激活方式之一.在适用此方式的Microsoft产品 ...
- 【习题 7-8 UVA-12107】Digit Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 枚举最大层数.(也即改变的数字个数 然后枚举第一个改哪个数字,第二个改哪个数字.. 一定要注意字典序问题. 每次优先 ...
- es6常用功能与异步详解(JS高级面试题)
callback hell方法的使用 可读性不友好 function loadImg(src,callback,fail){ var img = document.createElement('img ...
- [Angular] Protect The Session Id with https and http only
For the whole signup process. we need to Hash the password to create a password digest Store the use ...
- Javascript和jquery事件--鼠标事件的小结
1.鼠标事件的主要事件应该是mouseup, mousedown, mousewheel, mousemove, mouseover, moveout. <1>其中mouseup和mous ...