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

How can I set a breakpoint in C or C ++ code programmatically, which 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; } 
+89
c ++ c linux gdb
Dec 01 '10 at 16:17
source share
5 answers

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 really does not support SIGINT , so if portability is a problem, you are probably better off using SIGABRT .

+90
Dec 01 '10 at 16:22
source share

In the project I'm working on, we do the following:

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

(In our case, we wanted to work hard if it happened outside the debugger, and if possible, generated a crash report. This is one of the reasons we used SIGABRT. This operation through Windows, Mac and Linux took several attempts. with several #ifdefs, commented here: http://hg.mozilla.org/mozilla-central/file/98fa9c0cff7a/js/src/jsutil.cpp#l66 .)

+25
Dec 01 2018-10-01
source share

Looking here , I found the following method:

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

It seems to me that this is awkward. And I think this only works on x86 architecture.

+22
Dec 01 '10 at 16:19
source share

__asm__("int $3"); must 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; } 
+10
Mar 16 '15 at 1:13
source share

On OS X, you can just call std::abort() (on Linux, this may be the same)

+1
Jun 25 '16 at 16:49
source share



All Articles