[U-Boot] [PATCH] Add assert() for debug assertions

Simon Glass sjg at chromium.org
Thu Jun 23 23:39:12 CEST 2011


On Wed, Jun 22, 2011 at 10:49 PM, V, Aneesh <aneesh at ti.com> wrote:
> On Thu, Jun 23, 2011 at 2:53 AM, Mike Frysinger <vapier at gentoo.org> wrote:
>> On Wednesday, June 22, 2011 17:04:49 Simon Glass wrote:
>>> +/*
>>> + * An assertion is run-time check done in debug mode only. If DEBUG is not
>>> + * defined then it is skipped. It does not BUG or halt U-Boot, but tries
>>> to + * continue execution in any case. It is hoped that all failing
>>> assertions + * are found before release, and after release it is hoped
>>> that they don't + * matter. But in any case these failing assertions
>>> cannot be fixed with a + * BUG-type reset (which may just do the same
>>> assertion again).
>>> + */
>>> +#define assert(x)    \
>>> +     ({ if (!(x)) printf("Assertion failure '%s' %s line %d\n", \
>>> +             #x, __FILE__, __LINE__); })
>>>  #else
>>>  #define debug(fmt,args...)
>>>  #define debugX(level,fmt,args...)
>>> +#define assert(x)
>>>  #endif       /* DEBUG */
>>
>> the trouble with ifdef magic like this is that errors/warnings can be
>> introduced when DEBUG isnt defined, and then only noticed when DEBUG is
>> defined.  so how about:

Hi Aneesh,

>
> Symbian OS, that had an array of defensive programming features, had two
> ASSERT macros:
>
> Something like ASSERT_DEBUG(x) and ASSERT_ALWAYS(x).

Symbian OS can live on in U-Boot!

>
> ASSERT_DEBUG(x) could be used more liberally because it is compiled out in
> production image and ASSERT_ALWAYS(x) could be used in more critical run-time
> errors.
>
> My rule of thumb for using these two was this:
>
> 1. ASSERT_DEBUG(x) was used for invariant checking, that's for catching errors
> that could arise out of programming errors. This was used more liberally in the
> code.
> 2. ASSERT_ALWAYS(x) was used to catch erros due to invalid run-time parameters
> that one may not be able to catch during testing.

With this patch we have:

- assert: compiled out for production code, used for debug, like your
ASSERT_DEBUG I think
- BUG_ON: halt/reset even in production code, used for production code
and critical faults where continued execution is certainly pointless
or counterproductive. Like your ASSERT_ALWAYS I think.

Regards,
Simon

>
> best regards,
> Aneesh
> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot
>


More information about the U-Boot mailing list