5

I would like to use this stacktrace method #4 implementation upon assertion failure.

So if there are no signals triggered, could you suggest a way to detect an assertion failure before the executable exits?

Community
  • 1
  • 1
kfmfe04
  • 14,936
  • 14
  • 74
  • 140

1 Answers1

7

On Linux, an assert failure (if not disabled with -DNDEBUG) is doing (from /usr/include/assert.h)

# define assert(expr)                                                   \
  ((expr)                                                               \
   ? __ASSERT_VOID_CAST (0)                                             \
   : __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))

and the internal __assert_fail routine is calling abort, which sends a SIGABRT to the process, which you might catch if you really wanted to.

But a simpler way is to have your own "assert"-like macro. This is what many free software (GTK with g_assert, GCC with gcc_assert, ...) are actually doing.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • tyvm - I will look into gcc_assert – kfmfe04 Nov 24 '11 at 13:44
  • **No**, `gcc_assert` is only useful for those developing **inside the GCC compiler** (or some GCC compiler plugin), not for GCC users. I mention it only as an example of good practice. Don't use it, but **define your own `kfme4_assert` macro** do to what you want. – Basile Starynkevitch Nov 24 '11 at 13:48
  • So it sounds like I should just drop that backtrace()/backtrace_symbols() (in the link above) implementation into my own macro. – kfmfe04 Nov 24 '11 at 13:52
  • 1
    Yes, your `kfme4_assert` macro would call a `kfme4_assertfail` function which would do the `backtrace` trick. – Basile Starynkevitch Nov 24 '11 at 13:56