Ext.: http://blog.csdn.net/wangeen/article/details/14230171
Attach is an important debug mode of GDB, which plays an important role in the MPI program Debug.
The following steps are performed:
1) First add the following code to the beginning of the program, note that this is a dead loop, the reason why the death cycle is to keep him waiting attch
{ int i = 0; Char hostname[256]; GetHostName (hostname, sizeof (hostname)); printf ("PID%d on%s ready for attach\n", Getpid (), hostname); Fflush (stdout); while (0 = = i) sleep (5);}
The startup program will get the following statement
PID 15425 on XXX ready for attach
PID 15426 on XXX ready for attach
2) Start GDB
$gdb
(gdb) Attach 15425
(GDB) up//The up here is very important, see Misc
(GDB) Set Var i=1
This jumps out of the dead loop and continues.
3) Misc
About GDB's use
We used to think that GDB was very difficult to use, and many of the debug messages were not shown, especially in the case of multithreading, when I used GDB attach MPI program to find the reason, because GDB is always trying to pause the program to grab the contents of the stack to frame, If a situation like sleep is encountered, GDB does not get any information, in which case we need to use up to toggle the frame.
[Go] GDB Attach