Re: [PATCH 2/7] Add assertion checking macros

From: Jiri Slaby
Date: Wed Oct 12 2011 - 13:45:32 EST


On 10/12/2011 06:47 PM, David Howells wrote:
> Add a range of ASSERT* macros to linux/assert.h for performing runtime
> assertions. These will use assertion_failure() to cause an annotated oops if
> the check fails.
>
> The checks are only enabled under two circumstances:
>
> (1) CONFIG_DEBUG_ENABLE_ASSERTIONS=y
>
> (2) ENABLE_ASSERTIONS is defined prior to the #inclusion of <linux/assert.h>
>
> There are five macros provided:
>
> (a) ASSERT(X)
>
> Issue an assertion failure error if X is false. In other words, require
> the expression X to be true. For example:
>
> ASSERT(val != 0);
>
> There is no need to display val here in the case the expression fails
> since it can only be 0. If this fails, it produces an error like the
> following:
>
> ------------[ cut here ]------------
> ASSERTION FAILED at fs/fscache/main.c:109!
> invalid opcode: 0000 [#1] SMP
>
> (b) ASSERTCMP(X, OP, Y)
>
> Issue an assertion failure error if the expression X OP Y is false. For
> example:
>
> ASSERTCMP(x, >, 12)
>
>
> If an oops is produced, then the values of X and Y will be displayed in
> hex, along with OP:
>
> ------------[ cut here ]------------
> ASSERTION FAILED at fs/fscache/main.c:109!
> Check 2 > c is false
> invalid opcode: 0000 [#1] SMP
>
> (c) ASSERTRANGE(X, OP, Y, OP2, Z)
>
> Issue an assertion failure error if the expression X OP Y or if the
> expression Y OP2 Z is false. Typically OP and OP2 would be < or <=,
> looking something like:
>
> ASSERTRANGE(11, <, x, <=, 13);
>
> and giving the following error:
>
> ------------[ cut here ]------------
> ASSERTION FAILED at fs/fscache/main.c:109!
> Check b < 2 <= d is false
> invalid opcode: 0000 [#1] SMP

Hmm, but why not have a single something-like-"ASSERT" doing the same as
in userspace:
#define ASSERT(X) do { \
if (unlikely(!(X))) \
cond_assertion_failed("Assertion '" #X "' failed"); \
} while (0)

You would not need zillion of sub-macros then.

thanks,
--
js
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/