Is there any way to tell GDB to wait for a process to start and attach to it?

gdb

Solution

Here is my script called gdbwait:

#!/bin/sh
progstr=$1
progpid=`pgrep -o $progstr`
while [ "$progpid" = "" ]; do
  progpid=`pgrep -o $progstr`
done
gdb -ex continue -p $progpid

Usage:

gdbwait my_program

Sure it can be written nicer but Bourne shell script syntax is painful for me so if it works then I leave it alone. :)

If the new process launches and dies too quickly, add 1 second delay in your own program for debugging.

Problem

I have a process that is called by another process which is called by another process and so on ad nauseam. It's a child process in a long tool chain. This process is crashing. I would like to catch this process in GDB to understand why it's crashing. However, the only way I can think of is: - Start the original parent process in the commandline. - Poll `ps -C <name process I want to catch>` and get the PID. - Launch GDB, attached to that process's PID. This is cumbersome but usually does the job. The problem is that the current failure runs very quickly, and by the time I capture the PID and launch GDB, it's already passed the failure point. I would like to launch GDB and instead of: ``` (gdb) attach <pid> ``` I would like to do: ``` (gdb) attach <process name when it launches> ``` Is there any way to do this? I am using GDB 7.1 on Linux.

Original source

Related problems