|
NAME
SYNOPSIS
DESCRIPTIONAssertions are widely used within the FreeBSD kernel to verify programmatic assumptions. For violations of run-time assumptions and invariants, it is desirable to fail as soon and as loudly as possible. Assertions are optional code; for non-recoverable error conditions an explicit call to panic(9) is usually preferred. The
In a kernel that is built without The
Assertion GuidelinesWhen adding new assertions, keep in mind their primary purpose: to aid in identifying and debugging of complex error conditions. 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. EXAMPLESA hypothetical struct foo object must not
have its 'active' flag set when calling
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 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));
HISTORYThe AUTHORSThis manual page was written by Jonathan M.
Bresler
<jmb@FreeBSD.org> and
|