Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait for gdb to attach [duplicate]

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.

like image 805
rhlee Avatar asked Jun 29 '12 01:06

rhlee


People also ask

What is Backtrace in gdb?

A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack.

What does set do in gdb?

This command sets the values of a debugger variable, memory address, or expression that is accessible according to the scope and visibility rules of the language. The expression can be any expression that is valid in the current context. The set variable command evaluates the specified expression.

How do you stop a process in gdb?

To exit GDB, use the quit command (abbreviated q ), or type an end-of-file character (usually C-d ). If you do not supply expression , GDB will terminate normally; otherwise it will terminate using the result of expression as the error code.


1 Answers

At least with LLDB making the process send SIGSTOP to itself should do the trick. Debugger continue command will then issue the SIGCONT. This should work with GDB too. Alternatively try SIGINT instead of SIGSTOP.

Include header

#include <signal.h>
#include <csignal> // or C++ style alternative

then

raise(SIGSTOP)
like image 92
Peter Lamberg Avatar answered Sep 17 '22 15:09

Peter Lamberg