| Main index | Section 9 | Options | 
#include <sys/param.h>
#include <sys/systm.h>
The KASSERT() macro tests the given boolean expression. If expression evaluates to false, and the kernel is compiled with options INVARIANTS, the panic(9) function is called. This terminates the running system at the point of the error, possibly dropping into the kernel debugger or initiating a kernel core dump. The second argument, msg, is a printf(9) format string and its arguments, enclosed in parentheses. The formatted string will become the panic string.
In a kernel that is built without options INVARIANTS, the assertion macros are defined to be no-ops. This eliminates the runtime overhead of widespread assertions from release builds of the kernel. Therefore, checks which can be performed in a constant amount of time can be added as assertions without concern about their performance impact. More expensive checks, such as those that output to console, or verify the integrity of a chain of objects are generally best hidden behind the DIAGNOSTIC kernel option.
The MPASS() macro (read as: "must-pass") is a convenience wrapper around KASSERT() that automatically generates a simple assertion message including file and line information.
The panic messages resulting from assertion failures should be useful without the resulting kernel dump; the message may be included in a bug report, and should contain the relevant information needed to discern how the assertion was violated. This is especially important when the error condition is difficult or impossible for the developer to reproduce locally.
Therefore, assertions should adhere to the following guidelines:
Combined, this gives greater clarity into the exact cause of an assertion panic; see EXAMPLES below.
void
foo_dealloc(struct foo *fp)
{
        KASSERT((fp->foo_flags & FOO_ACTIVE) == 0,
            ("%s: fp %p is still active, flags=%x", __func__, fp,
            fp->foo_flags));
        ...
}
This assertion provides the full flag set for the object, as well as the memory pointer, which may be used by a debugger to examine the object in detail ( for example with a 'show foo' command in ddb(4) ).
The assertion
MPASS(td == curthread);
located on line 87 of a file named foo.c would generate the following panic message:
panic: Assertion td == curthread failed at foo.c:87
This is a simple condition, and the message provides enough information to investigate the failure.
The assertion
MPASS(td == curthread && (sz >= SIZE_MIN && sz <= SIZE_MAX));
is NOT useful enough. The message doesn't indicate which part of the assertion was violated, nor does it report the value of sz, which may be critical to understanding why the assertion failed.
According to the guidelines above, this would be correctly expressed as:
MPASS(td == curthread);
KASSERT(sz >= SIZE_MIN && sz <= SIZE_MAX,
    ("invalid size argument: %u", sz));
| KASSERT (9) | March 19, 2024 | 
| Main index | Section 9 | Options | 
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
