Sleep in the sub-process, then attach up.
GDB--pid=123456
The ID,GDB attach process number of the PS out child process.
Http://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/index.html
In fact, GDB does not provide direct support for debugging multi-process programs. For example, using GDB to debug a process, if the process fork a child process, GDB will continue to debug the process, and the child process will run undisturbed. If you set a breakpoint in the child process code beforehand, the child process receives the SIGTRAP signal and terminates. So how do you debug a child process? In fact, we can use the features of GDB or some other auxiliary means to achieve the purpose. In addition, GDB also joins some multi-process debugging support on newer kernels.
Next we introduce several methods in detail, namely Follow-fork-mode method, attach Subprocess method and GDB wrapper method.
Follow-fork-mode
In the 2.5.60 Linux kernel and later, GDB provides the Follow-fork-mode option to support multi-process debugging for programs that use Fork/vfork to create child processes.
The usage of Follow-fork-mode is:
Set Follow-fork-mode [Parent|child]
Parent:fork continues to debug the parent process after the child process is not affected.
Child:fork after the child process is debugged, the parent process is unaffected.
So if you need to debug a child process, after you start gdb:
(GDB) Set Follow-fork-mode child
and set breakpoints in the child Process code.
There is also the Detach-on-fork parameter, which instructs GDB whether to disconnect (detach) The debug of a process after fork, or to be controlled by GDB:
Set detach-on-fork [On|off]
On: Disconnects the specified process from debug Follow-fork-mode.
Off:gdb will control the parent and child processes. Follow-fork-mode The specified process is debugged and the other process is placed in a paused (suspended) state.
Note that it is best to use GDB 6.6 or later, if you are using GDB6.4, there is only Follow-fork-mode mode.
The use of Follow-fork-mode/detach-on-fork is still relatively simple, but due to the/GDB version limitation of the system kernel, we can only use it on a system that meets the requirements. Moreover, since Follow-fork-mode's debugging must start from the parent process, it is not convenient to debug a system that has multiple fork so that the grandchild process or great-grandchild process, such as the 3 process system, is debugged.
Attach Child process
As is known to all, GDB has the ability to attach (attach) to a running process, namely attach <pid> commands. So we can use this command to attach the child process and then debug it.
For example, to debug a process rim_oracle_agent.9i, we first get the PID of the process
[Email protected] tianq]# Ps-ef|grep rim_oracle_agent.9i
Nobody 6722 6721 0 05:57? 00:00:00 rim_oracle_agent.9i
Root 7541 27816 0 06:10 pts/3 00:00:00 grep-i rim_oracle_agent.9i
As can be seen through Pstree, this is a three-process system, Oserv is the parent process of Rim_oracle_prog, Rim_oracle_prog is also the parent process of rim_oracle_agent.9i.
[Email protected] root]# pstree-h 6722
Now it's time to debug. A new problem is that the sub-process has been running, attach to go up and do not know where to run. Is there any way to solve it?
One way is to debug the child process in the initial code, such as the beginning of the main function, add a special code, so that the child process when a condition is set up to loop the sleep wait, attach to the process after the code snippet after the breakpoint, and then set the conditions to cancel, so that the code can continue to execute.
As for the criteria used in this code, look at your preferences. For example, we can check the value of a specified environment variable, or check that a particular file does not exist. Take the file as an example, in the form of the following:
void Debug_wait (char *tag_file)
{
while (1)
{
if (Tag_file exists)
Sleep for some time;
Else
Break
}
}
After the attach to the process, set a breakpoint after the code, and then delete the file is OK. Of course you can also use other conditions or forms, as long as this condition can be set/detected.
The attach process approach is also convenient, and it can handle a variety of complex process systems, such as the grandson/grandchild process, such as the daemon (daemon process), and the only thing needed is to add a small piece of code.
Debugging a multi-process program with GDB