Re: Useful KERNEL_ASSERT Macro

Jordan Mendelson (jordy@wserv.com)
Sun, 01 Aug 1999 01:21:24 -0400


Tim Hockin wrote:
>
> > The following ASSERT macro seems adequate:
> >
> > #ifdef KERNEL_ASSERT
> > #define KERNEL_ASSERT(expr) (expr || printk(KERN_DEBUG "%s/%d: Assertion failed!
> > %s\n", __FILE__, __LINE__, #expr));
> > #else
> > #define KERNEL_ASSERT(expr) 0
> > #endif
>
> /* a general purpose kernel assert macro */
> #ifdef KERNEL_ASSERT_ON
> #define KERNEL_ASSERT(expr) if (!(expr)) \
> printk(KERN_DEBUG "KASSERT: %s:%d" \
> " - Assertion failed! (%s)\n", \
> __FILE__, __LINE__, #expr)
> #else
> #define KERNEL_ASSERT(expr)
> #endif
>
> We could expand this debugging "macro library" quite a lot. I think it would
> be worthwhile, if we can convince everyone to USE it. It would cut a lot of
> redundant definitions of ASSERT and DEBUG_PRINT sorts of macros.

They both do the same thing, (expr || printk(...)) will cause the printk to only
evaluate when expr is false, it's just a more compact way of writing it.

But yeah, I think it would be a worthwhile thing to do and people shouldn't
really be worrying about only enabling it in various functions, this particular
KERNEL_ASSERT() macro should only be used to make sure a particular expression
is true... if it's false, it's a bug and someone needs to fix it. Enabling this
macro on a 100% working system should never result in any printk()'s...

So if your ethernet driver is having it's memory not allocated correctly, a
KERNEL_ASSERT() in the memory subsystem and the ethernet driver would help you
to understand why whereas enabling it just in the ethernet driver may not.

Just food for thought.

Jordan

--
Jordan Mendelson     : http://jordy.wserv.com
Web Services, Inc.   : http://www.wserv.com

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/