16

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
drpepper
  • 462
  • 1
  • 5
  • 16

5 Answers5

19

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);
Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • 1
    It will cause the CPU to raise interrupt 3 (http://faydoc.tripod.com/cpu/int3.htm) The debugger has an interrupt handler registered for interrupt 3, and will break the program. – Andomar Nov 12 '09 at 11:33
  • perfect! it catches a SIGTRAP event, stops on a dime, and then lets me continue! thanks a lot. – drpepper Nov 12 '09 at 12:33
  • 1
    to make it a bit more portable, i replaced the assembly with the equivalent c code: raise(SIGTRAP); works great. – drpepper Nov 12 '09 at 12:37
  • Your macro is broken in the presence of surrounding `else` – Lightness Races in Orbit Aug 15 '14 at 13:27
  • its worth noting that the int 3 is intel specific, where as raise(SIGTRAP) has worked for me in iOS and Android on ARM32/64 and MIPS, and i suspect works everywhere by virtue of being part of the standard library as well. – jheriko Jun 22 '15 at 13:33
10

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.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
2

Even better usability is achieved with

/*!
 * \file: assert_x.h
 * \brief: Usability Improving Extensions to assert.h.
 * \author: Per Nordlöw
 */

#pragma once

#include <errno.h>
#include <signal.h>
#include <assert.h>

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(NDEBUG)
#  define passert(expr)                                                 \
  if (!(expr)) {                                                        \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.",                \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expr)); raise(SIGTRAP); \
  }
#  define passert_with(expr, sig)                                       \
  if (!(expr)) {                                                        \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.",                \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expr)); raise(sig); \
  }
#  define passert_eq(expected, actual)                                  \
  if (!(expected == actual)) {                                          \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' == `%s' failed.",        \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expected), __STRING(actual)); raise(SIGTRAP); \
  }
#  define passert_neq(expected, actual)                                 \
  if (!(expected != actual)) {                                          \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' != `%s' failed.",        \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expected), __STRING(actual)); raise(SIGTRAP); \
  }
#  define passert_lt(lhs, rhs)                                          \
  if (!(lhs < rhs)) {                                                   \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' < `%s' failed.",         \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
  }
#  define passert_gt(lhs, rhs)                                          \
  if (!(lhs > rhs)) {                                                   \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' < `%s' failed.",         \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
  }
#  define passert_lte(lhs, rhs)                                         \
  if (!(lhs <= rhs)) {                                                  \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' <= `%s' failed.",        \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
  }
#  define passert_gte(lhs, rhs)                                         \
  if (!(lhs >= rhs)) {                                                  \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' >= `%s' failed.",        \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
  }
#  define passert_zero(expr)                                            \
  if (!(expr == 0)) {                                                   \
    fprintf(stderr, "%s:%d: %s: Assertion `%s' is zero failed.",        \
            __FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expr)); raise(SIGTRAP); \
  }
#else
#  define passert(expr)
#  define passert_with(expr, sig)
#  define passert_eq(expected, actual)
#  define passert_lt(lhs, rhs)
#  define passert_gt(lhs, rhs)
#  define passert_lte(lhs, rhs)
#  define passert_gte(lhs, rhs)
#  define passert_zero(expr)
#endif

#ifdef __cplusplus
}
#endif
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
1

You can replace assert with your own version which calls pause() instead of abort(). When the assertion fails, the program will pause and you can run gdb --pid $(pidof program) to examine the callstack and variables. An advantage of this approach is that the program does not need to be started under GDB.

Header file (based on /usr/include/assert.h):

#include <assert.h>

#ifndef NDEBUG
    void assert_fail(const char *assertion, const char *file, unsigned line, const char *function)
    __attribute__ ((noreturn));
    #undef assert
    #define assert(expr)            \
        ((expr)                     \
        ? __ASSERT_VOID_CAST (0)    \
        : assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
#endif /* NDEBUG */

Implementation of assert_fail (based on assert.c in glibc):

#include <stdio.h>   /* for stderr, fprintf() */
#include <stdlib.h>  /* for abort() */
#include <unistd.h>  /* for pause() */

void assert_fail(const char *assertion, const char *file, unsigned line, const char *function) {
    extern const char *__progname;
    fprintf(stderr, "%s%s%s:%u: %s%sAssertion `%s' failed.\n",
        __progname,
        __progname[0] ? ": " : "",
        file,
        line,
        function ? function : "",
        function ? ": " : "",
        assertion
    );
    pause();
    abort();
}
  • Oh, nice. Posix really dropped the ball when they specified the abort behavior when `NDBUG` is not defined. Who in there right mind thinks a self induced crash is appropriate for debugging and diagnostics.... – jww Nov 24 '13 at 23:01
  • Where is the "pause" function declared? – Brent Sep 06 '16 at 20:20
  • @Brent, good catch! `pause()` is defined in the man. sec 2., which is libc _et amici_ section. In my copy (Debian 11), `man pause.2` lists compatibility as "POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD", which says it all about API's venerable age and wide compatibility. The sample code is simply missing `#include `, I'll edit, thanks for noticing. – kkm inactive - support strike Aug 06 '21 at 02:44
1

Have you tried to send a SIGCONT signal to the process?

kill -s SIGCONT <pid>
Andomar
  • 232,371
  • 49
  • 380
  • 404