Noeud:Attaching to an Already Running Program, Noeud « Next »:, Noeud « Up »:An example debugging session using gdb



Attaching to an Already Running Program

It is possible that the program is not started directly from the command line - it might be run from another program, or be created by an incoming network request.

In this case it may not be possible to run it independently and still observe its behaviour. All is not lost, however. If you have the process ID of the process you want to debug, then you can just use gdb executable-name process-id and gdb will attach itself to the running process and stop it wherever it is, ready for your commands.

Alternatively you can start gdb as if you were about to debug the executable normally, with gdb executable-name, then type attach at the command line.

This will give you a menu of all the processes of that name on the system so you can select the one you want FIXME

Tip: if the process starts up then immediately crashes, then you can add a line such as sleep(30); immediately after the start of main() which will give you 30 seconds to get the PID and attach to the process. Remember to take this sleep() out again before releasing the code. DAMHIKT. FIXME