Files
ipxe/src/include/assert.h
Michael Brown 46719f2264 [libc] Allow assertions to be globally enabled or disabled
Assertions are enabled for objects built with any debug level
(including an explicit debug level of zero).  It is sometimes useful
to be able to enable assertions across all objects; this currently
requires manually hacking include/assert.h.

Allow assertions to be globally enabled by adding ASSERT=1 to the
build command line.  For example:

  make bin/8086100e.mrom ASSERT=1

Similarly, allow assertions to be globally disabled by adding ASSERT=0
to the build command line.  If no ASSERT=... is specified on the
build command line, then only objects mentioned in DEBUG=... will have
assertions enabled (as is currently the case).

Note than globally enabling assertions imposes a relatively heavy
runtime penalty, primarily due to the various sanity checks performed
by list_add(), list_for_each_entry(), etc.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
2016-07-05 13:28:51 +01:00

75 lines
1.9 KiB
C

#ifndef _ASSERT_H
#define _ASSERT_H
/** @file
*
* Assertions
*
* This file provides two assertion macros: assert() (for run-time
* assertions) and linker_assert() (for link-time assertions).
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#ifndef ASSERTING
#ifdef NDEBUG
#define ASSERTING 0
#else
#define ASSERTING 1
#endif
#endif
extern unsigned int assertion_failures;
#define ASSERTED ( ASSERTING && ( assertion_failures != 0 ) )
/** printf() for assertions
*
* This function exists so that the assert() macro can expand to
* printf() calls without dragging the printf() prototype into scope.
*
* As far as the compiler is concerned, assert_printf() and printf() are
* completely unrelated calls; it's only at the assembly stage that
* references to the assert_printf symbol are collapsed into references
* to the printf symbol.
*/
extern int __attribute__ (( format ( printf, 1, 2 ) ))
assert_printf ( const char *fmt, ... ) asm ( "printf" );
/**
* Assert a condition at run-time.
*
* If the condition is not true, a debug message will be printed.
* Assertions only take effect in debug-enabled builds (see DBG()).
*
* @todo Make an assertion failure abort the program
*
*/
#define assert( condition ) \
do { \
if ( ASSERTING && ! (condition) ) { \
assertion_failures++; \
assert_printf ( "assert(%s) failed at %s line %d\n", \
#condition, __FILE__, __LINE__ ); \
} \
} while ( 0 )
/**
* Assert a condition at link-time.
*
* If the condition is not true, the link will fail with an unresolved
* symbol (error_symbol).
*
* This macro is iPXE-specific. Do not use this macro in code
* intended to be portable.
*
*/
#define linker_assert( condition, error_symbol ) \
if ( ! (condition) ) { \
extern void error_symbol ( void ); \
error_symbol(); \
}
#endif /* _ASSERT_H */