Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gdb

I have a process that is called by another process which is called by another process and so on ad nauseum. 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:

  1. start the original parent process in the commandline.
  2. poll ps -C <name process I want to catch> and get the PID.
  3. 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

like image 801
Nathan Fellman Avatar asked Dec 07 '10 22:12

Nathan Fellman


People also ask

Does GDB attach stop the process?

2. Attaching GDB to a Running Process. Attaching GDB to a process pauses it, and the user can issue GDB commands before continuing its execution.

How do I attach a GDB to a running thread?

Just run a program with s few threads, run gdb and before running attach PROCESS_PID run strace in another console. You must see ptrace (PTRACE_ATTACH) for each thread. Show activity on this post. Show activity on this post.

How do I detach a process in GDB?

When you have finished debugging the attached process, you can use the detach command to release it from GDB control. Detaching the process continues its execution. After the detach command, that process and GDB become completely independent once more, and you are ready to attach another process or start one with run .


2 Answers

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 quick, add 1 second delay in your own program for debugging ...

like image 114
fchen Avatar answered Nov 11 '22 21:11

fchen


You can attach to a parent process and set follow-fork-mode child. This will make gdb debug child process instead of parent after forking. Also catch fork will be useful. This will make gdb stop after each fork. See docs.

like image 43
ks1322 Avatar answered Nov 11 '22 21:11

ks1322