Alex Rivera | Logout

wait for gdb to attach

Asked 2012-06-29T01:40:16.300
23

I've been using gdb normally for 1 or 2 projects. I.e. I invoke gdb --args prog args . gdb runs in the same tty as the program I'm debugging.

However my latest project is modifying the dtach utility. This is a program like screen, so the tty's are redirected elsewhere, thus I have to use gdb's attach functionality.

The problem with gdb attach is that obviously you can't attach from the very beginning as you need to run the program first in order to get a pid to attach to.

Is there any way I can get a program to wait at a point until gdb is attached?

I can't use gdbserver as I'm on cygwin. Also I tried using pause(), but that just hung when I tried to continue.

Edit
Report

1 Answer

1

Some platforms may have wait-for-debugger instructions or traps.

More portably, you could make the program wait on some externally satisfied condition, such as a connection to a socket or writing some data into a fifo. You could then make the connection or send dummy data from a third terminal.

Or you could put an infinite loop in the program, testing the value of some volatile variable which you would modify with the debugger in order to allow it to continue.

If I recall you can use windows apis in cygwin programs, and some web searching seems to indicate one for detecting if the program is being debugged, so you might be able to loop until that returns to.

answered 2012-06-29T02:55:09.833

Your Answer