Loading...
Searching...
No Matches
assert.h File Reference

POSIX.1-2008 compliant version of the assert macro. More...

Detailed Description

POSIX.1-2008 compliant version of the assert macro.

Author
Oliver Hahm olive.nosp@m.r.ha.nosp@m.hm@in.nosp@m.ria..nosp@m.fr
René Kijewski rene..nosp@m.kije.nosp@m.wski@.nosp@m.fu-b.nosp@m.erlin.nosp@m..de
Martine Lenders m.len.nosp@m.ders.nosp@m.@fu-b.nosp@m.erli.nosp@m.n.de

Definition in file assert.h.

#include <stdint.h>
+ Include dependency graph for assert.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

#define DEBUG_ASSERT_VERBOSE
 Activate verbose output for assert() when defined.
 
#define DEBUG_ASSERT_BREAKPOINT
 Activate breakpoints for assert() when defined.
 
#define __NORETURN
 hidden (__) NORETURN definition
 
#define assert(cond)   (_likely(cond) ? (void)0 : _assert_failure(__FILE__, __LINE__))
 abort the program if assertion is false
 
#define static_assert(cond, ...)    { enum { static_assert_failed_on_div_by_0 = 1 / (!!(cond)) }; }
 static_assert for c-version < c11
 
#define DEBUG_ASSERT_NO_PANIC   (1)
 Don't panic on a failed assertion, just halt the running thread.
 
__NORETURN void _assert_failure (const char *file, unsigned line)
 Function to handle failed assertion.
 

Macro Definition Documentation

◆ __NORETURN

#define __NORETURN

hidden (__) NORETURN definition

Definition at line 81 of file assert.h.

◆ assert

#define assert (   cond)    (_likely(cond) ? (void)0 : _assert_failure(__FILE__, __LINE__))

abort the program if assertion is false

If the macro NDEBUG was defined at the moment <assert.h> was last included, the macro assert() generates no code, and hence does nothing at all.

Otherwise, the macro assert() prints an error message to standard error and terminates the application by calling core_panic().

The purpose of this macro is to help programmers find bugs in their programs.

A failed assertion generates output similar to:

0x89abcdef => FAILED ASSERTION.

Where 0x89abcdef is an address. This address can be used with tools like addr2line (or e.g. arm-none-eabi-addr2line for ARM-based code), objdump, or gdb (with the command info line *(0x89abcdef)) to identify the line the assertion failed in.

With DEBUG_ASSERT_VERBOSE defined this will instead directly print the file and the line the assertion failed in:

example/file.c:42 => FAILED ASSERTION.

If the backtrace module is enabled (and implemented for the architecture in use) a backtrace will be printed in addition to the location of the failed assertion.

If DEBUG_ASSERT_BREAKPOINT is defined, the execution will stop on a failed assertion instead of producing the above output. If the architecture defines the macro DEBUG_BREAKPOINT, a breakpoint is inserted and the execution is stopped directly in the debugger. Otherwise the execution stops in an endless while loop.

See also
http://pubs.opengroup.org/onlinepubs/9699919799/functions/assert.html

Definition at line 136 of file assert.h.

◆ DEBUG_ASSERT_BREAKPOINT

#define DEBUG_ASSERT_BREAKPOINT

Activate breakpoints for assert() when defined.

Without this macro defined the assert() macro will just print some information about the failed assertion, see assert() and DEBUG_ASSERT_VERBOSE. If DEBUG_ASSERT_BREAKPOINT is defined, the execution will stop on a failed assertion instead of producing the output. If the architecture defines the macro DEBUG_BREAKPOINT, a breakpoint is inserted and the execution is stopped directly in the debugger. Otherwise the execution stops in an endless while loop.

Definition at line 62 of file assert.h.

◆ DEBUG_ASSERT_NO_PANIC

#define DEBUG_ASSERT_NO_PANIC   (1)

Don't panic on a failed assertion, just halt the running thread.

If the assertion failed in an interrupt, the system will still panic.

Definition at line 165 of file assert.h.

◆ DEBUG_ASSERT_VERBOSE

#define DEBUG_ASSERT_VERBOSE

Activate verbose output for assert() when defined.

Without this macro defined, assert() will just print the address of the code line the assertion failed in. With this macro defined, assert() will also print the file and the code line of the failed assertion.

Enabling verbose output will on the other hand lead to an increased size of the binary, since it needs to contain the names of all files with assertions.

To define just add it to your CFLAGS in your application's Makefile:

CFLAGS += -DDEBUG_ASSERT_VERBOSE

Definition at line 48 of file assert.h.

◆ static_assert

#define static_assert (   cond,
  ... 
)     { enum { static_assert_failed_on_div_by_0 = 1 / (!!(cond)) }; }

static_assert for c-version < c11

Generates a division by zero compile error when cond is false

Definition at line 154 of file assert.h.

Function Documentation

◆ _assert_failure()

__NORETURN void _assert_failure ( const char *  file,
unsigned  line 
)

Function to handle failed assertion.

Note
This function was introduced for memory size optimization
Warning
this function NEVER returns!
Parameters
[in]fileThe file name of the file the assertion failed in
[in]lineThe code line of file the assertion failed in