Set breakpoint in C or C++ code programmatically for gdb on Linux

C++CLinuxGdb

C++ Problem Overview


How can I set a breakpoint in C or C++ code programatically that will work for gdb on Linux?

I.e.:

int main(int argc, char** argv)
{
    /* set breakpoint here! */
    int a = 3;
    a++;  /*  In gdb> print a;  expect result to be 3 */
    return 0;
}

C++ Solutions


Solution 1 - C++

One way is to signal an interrupt:

#include <csignal>

// Generate an interrupt
std::raise(SIGINT);

In C:

#include <signal.h>
raise(SIGINT);

UPDATE: MSDN states that Windows doesn't really support SIGINT, so if portability is a concern, you're probably better off using SIGABRT.

Solution 2 - C++

By looking here, I found the following way:

void main(int argc, char** argv)
{
    asm("int $3");
    int a = 3;
    a++;  //  In gdb> print a;  expect result to be 3
}

This seems a touch hackish to me. And I think this only works on x86 architecture.

Solution 3 - C++

In a project I work on, we do this:

raise(SIGABRT);  /* To continue from here in GDB: "signal 0". */

(In our case we wanted to crash hard if this happened outside the debugger, generating a crash report if possible. That's one reason we used SIGABRT. Doing this portably across Windows, Mac, and Linux took several attempts. We ended up with a few #ifdefs, helpfully commented here: http://hg.mozilla.org/mozilla-central/file/98fa9c0cff7a/js/src/jsutil.cpp#l66 .)

Solution 4 - C++

Disappointing to see so many answers not using the dedicated signal for software breakpoints, SIGTRAP:

#include <signal.h>

raise(SIGTRAP); // At the location of the BP.

On MSVC/MinGW, you should use DebugBreak, or the __debugbreak intrinsic. A simple #ifdef can handle both cases (POSIX and Win32).

Solution 5 - C++

__asm__("int $3"); should work:

int main(int argc, char** argv)
{
    /* set breakpoint here! */
    int a = 3;
    __asm__("int $3");
    a++;  /*  In gdb> print a;  expect result to be 3 */
    return 0;
}

Solution 6 - C++

On OS X you can just call std::abort() (it might be the same on Linux)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJ. PolferView Question on Stackoverflow
Solution 1 - C++Håvard SView Answer on Stackoverflow
Solution 2 - C++J. PolferView Answer on Stackoverflow
Solution 3 - C++Jason OrendorffView Answer on Stackoverflow
Solution 4 - C++Benjamin Crawford Ctrl-Alt-TutView Answer on Stackoverflow
Solution 5 - C++hek2mglView Answer on Stackoverflow
Solution 6 - C++dacapView Answer on Stackoverflow