ElectRocnic
ElectRocnic

Reputation: 1305

How can I detach a gdb session from outside?

I start a gdb session in the background with a command like this:

gdb --batch --command=/tmp/my_automated_breakpoints.gdb -p pid_of_proces> &> /tmp/gdb-results.log &

The & at the end lets it run in the background (and the shell is immediately closed afterwards as this command is issued by a single ssh command).

I can find out the pid of the gdb session with ps -aux | grep gdb.

However: How can I gracefully detach this gdb session from the running process just like I would if I had the terminal session in front of me with the (gdb) detach command?

When I kill the gdb session (and not the running process itself) with kill -9 gdb_pid, I get unwanted SIGABRTs afterwards in the running program.

A restart of the service is too time consuming for my purpose.

In case of a successful debugging session with this automated script I could use a detach command inside the batch script. This is however not my case: I want to detach/quit the running gdb session when there are some errors during the session, so I would like to gracefully detach gdb by hand from within another terminal session.

Upvotes: 6

Views: 1631

Answers (1)

Daniel Trugman
Daniel Trugman

Reputation: 8501

If you run the gdb command from terminal #1 in the background, you can always bring gdb back into foreground by running the command fg. Then, you can simply CTRL+C and detach as always to stop the debugging session gracefully.

Assuming that terminal #1 is now occupied by something else and you cannot use it, You can send a SIGHUP signal to the gdb process to detach it:

sudo kill -s SIGHUP $(pidof gdb)

(Replace the $(pidof gdb) with the actual PID if you have more than one gdb instance)

Upvotes: 4

Related Questions