Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue to debug after failed assertion on Linux?

Tags:

c++

c

linux

assert

gdb

When an assertion fails with Visual C++ on Windows, the debugger stops, displays the message, and then lets you continue (or, if no debugging session is running, offers to launch visual studio for you).

On Linux, it seems that the default behavior of assert() is to display the error and quit the program. Since all my asserts go through macros, I tried to use signals to get around this problem, like

#define ASSERT(TEST) if(!(TEST)) raise(SIGSTOP);

But although GDB (through KDevelop) stops at the correct point, I can't seem to continue past the signal, and sending the signal manually within GDB just leaves me hanging, with control of neither GDB nor the debugged process.

like image 509
drpepper Avatar asked Nov 12 '09 11:11

drpepper


People also ask

What does debug assertion failed mean?

An assertion statement specifies a condition that you expect to hold true at some particular point in your program. If that condition does not hold true, the assertion fails, execution of your program is interrupted, and this dialog box appears. Click. To. Retry.

What is assertion in debugging?

What is an Assertion? Assertions provide a useful debugging tool that allows you to document an assumption within your code and have this assumption checked automatically at run-time. Every assertion includes a predicate, or Boolean condition, that you expect will always evaluate as true.

What is a failed assertion?

An assertion failure occurs when the database server cannot continue normal processing and must shut down. You can correct some of the problems that cause assertion failures, such as disk issues.


2 Answers

You really want to recreate the behavior of DebugBreak. This stops the program in the debugger.

My googling of "DebugBreak linux" has turned up several references to this piece of inline assembly which is supposed to do the same.

#define DEBUG_BREAK asm("int $3")

Then your assert can become

#define ASSERT(TEST) if(!(TEST)) asm("int $3");

According to Andomar int 3 causes the cpu to raise interrupt 3. According to drpepper a more portable way to do this would be to call:

 raise(SIGTRAP);
like image 107
Doug T. Avatar answered Sep 29 '22 16:09

Doug T.


You can configure gdb to handle specific signals in a different way. For example, the following will cause SIGSTOP not to be treated as a stoppable event.

handle SIGSTOP nostop noprint pass

help handle within gdb will give you more information.

like image 27
Jeff Foster Avatar answered Sep 29 '22 18:09

Jeff Foster