Sevenforge Sevenforge by Curtis Spencer home

Attaching to Child Processes using gdb attach

September 30, 2008

I was working on a deviantly fishy segfault today that was introduced after I added daemonization to an existing service of mine. It turned out that the daemonizing code chdir’ed to the root folder, so the relative paths in the service no longer worked. However, it was hard to detect this in the child process as it silently failed.

Gdb Attach to the rescue!

Short story even shorter. Simply run your daemonizing process, then do a ps to find out the PID, then use

(gdb) attach <pid> 

to connect to the process. Then you can easily see the backtrace when it ends up failing on you.

Secondly, if you are having trouble attaching fast enough, just add a sleep(10) somewhere at the beginning the child process, so you can connect fast enough.

Fork me on GitHub