Writing Device Drivers

Use ASSERT(9F) to Catch Invalid Assumptions

void ASSERT(EXPRESSION)

ASSERT(9F) is a macro used to halt the execution of the kernel if a condition expected to be true is actually false. ASSERT provides a way for the programmer to validate the assumptions made by a piece of code.

ASSERT is defined only when the compilation symbol DEBUG is defined; ASSERT is compiled out of the code if DEBUG is not defined and therefore has no effect.

For example, if a driver pointer should be non-NULL and is not, the following assertion could be used to check the code:

ASSERT(ptr != NULL);

If the driver is compiled with DEBUG defined and the assertion fails, a message is printed to the console and the system panics:

panic: assertion failed: ptr != NULL, file: driver.c, line: 56

Note -

Because ASSERT(9F) uses the DEBUG compilation symbol, any conditional debugging code should also be based on DEBUG.


Assertions are an extremely valuable form of active documentation.