Skip to contentSkip to conversation

Add assert() for debug assertions

PATCH Superseded Simon Glass 3 participants 16 messages Jun 29, 2011 – Sep 12, 2011

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Add assert() for debug assertions

assert() is like BUG_ON() but compiles to nothing unless DEBUG is defined. This is useful when a condition is an error but a board reset is unlikely to fix it, so it is better to soldier on in hope. Assertion failures should be caught during development/test.

It turns out that assert() is defined separately in a few places in U-Boot with various meanings. This patch cleans up some of these.

Build errors exposed by this change (and defining DEBUG) are also fixed in this patch.

---
Changes in v2:
- Changed macros so that all code is compiled even if DEBUG is disabled

Changes in v3:
- Use panic() instead of printf()
- Use separate __assert_fail() function to reduce memory footprint
- Changed output format to match assert(3)

 common/dlmalloc.c |    7 -------
 include/common.h  |   21 +++++++++++++++++++++
 include/malloc.h  |    8 --------
 lib/qsort.c       |    5 -----
 lib/vsprintf.c    |    8 ++++++++
 5 files changed, 29 insertions(+), 20 deletions(-)
common/dlmalloc.c +0 -7
index e9bab09..f2080c6 100644
@@ -286,13 +286,6 @@ extern "C" {
 
 */
 
-#ifdef DEBUG
-#include <assert.h>
-#else
-#define assert(x) ((void)0)
-#endif
-
-
 /*
   INTERNAL_SIZE_T is the word-size used for internal bookkeeping
   of chunk sizes. On a 64-bit machine, you can reduce malloc
include/common.h +21 -0
index 1e21b7a..0956b93 100644
@@ -124,6 +124,27 @@ typedef volatile unsigned char	vu_char;
 #define debugX(level,fmt,args...)
 #endif	/* DEBUG */
 
+#ifdef DEBUG
+# define _DEBUG 1
+#else
+# define _DEBUG 0
+#endif
+
+/*
+ * An assertion is run-time check done in debug mode only. If DEBUG is not
+ * defined then it is skipped. If DEBUG is defined and the assertion fails,
+ * then it calls panic*( which may or may not reset/halt U-Boot (see
+ * CONFIG_PANIC_HANG), 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 reset (which
+ * may just do the same assertion again).
+ */
+void __assert_fail(const char *assertion, const char *file, unsigned line,
+		   const char *function);
+#define assert(x) \
+	({ if (!(x) && _DEBUG) \
+		__assert_fail(#x, __FILE__, __LINE__, __func__); })
+
 #define error(fmt, args...) do {					\
 		printf("ERROR: " fmt "\nat %s:%d/%s()\n",		\
 			##args, __FILE__, __LINE__, __func__);		\
include/malloc.h +0 -8
index 3e145ad..ecf3c67 100644
@@ -285,14 +285,6 @@ extern "C" {
 
 */
 
-#ifdef DEBUG
-/* #include <assert.h> */
-#define assert(x) ((void)0)
-#else
-#define assert(x) ((void)0)
-#endif
-
-
 /*
   INTERNAL_SIZE_T is the word-size used for internal bookkeeping
   of chunk sizes. On a 64-bit machine, you can reduce malloc
lib/qsort.c +0 -5
index 1cc0d31..86c392c 100644
@@ -17,11 +17,6 @@
 
 #include <linux/types.h>
 #include <exports.h>
-#if 0
-#include <assert.h>
-#else
-#define assert(arg)
-#endif
 
 void qsort(void  *base,
 	   size_t nel,
lib/vsprintf.c +8 -0
index 3b924ec..1f01b70 100644
@@ -728,3 +728,11 @@ void panic(const char *fmt, ...)
 	do_reset (NULL, 0, 0, NULL);
 #endif
 }
+
+void __assert_fail(const char *assertion, const char *file, unsigned line,
+		   const char *function)
+{
+	/* This will not return */
+	panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
+	      assertion);
+}
Signature · 1 line
1.7.3.1
#1 lore raw

PATCH v3 1/1 Add assert() for debug assertions

Quoted 1 line · On Wednesday, June 29, 2011 15:49:34 Simon Glass wrote:

- Use separate __assert_fail() function to reduce memory footprint

do we really care about that when people are using #define DEBUG ? i'd say this is an unnecessary indirection ...
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20110629/6f140b0b/attachment.pgp

#2 lore raw

PATCH v3 1/1 Add assert() for debug assertions

Dear Mike Frysinger,

Quoted 3 lines · In message <[email protected]> you wrote:

do we really care about that when people are using #define DEBUG ? i'd say this is an unnecessary indirection ...

Yes, we do care about the memory footprint - no matter whether with or without DEBUG.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Those who do not  understand  Unix  are  condemned  to  reinvent  it,
poorly.              - Henry Spencer, University of Toronto Unix hack
#3 lore raw

PATCH v3 1/1 Add assert() for debug assertions

Quoted 6 lines · On Wednesday, June 29, 2011 17:01:23 Wolfgang Denk wrote:
Quoted 2 lines · Mike Frysinger

do we really care about that when people are using #define DEBUG ? i'd say this is an unnecessary indirection ...

Yes, we do care about the memory footprint - no matter whether with or without DEBUG.

there's caring about it, and there's nitpicking over ~10 bytes per call site. -mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20110629/70affcb3/attachment.pgp

#4 lore raw

PATCH v3 1/1 Add assert() for debug assertions

Did this go anywhere? I will resend... - Simon

Quoted 11 lines · Mike Frysinger
Quoted 6 lines · On Wednesday, June 29, 2011 17:01:23 Wolfgang Denk wrote:
Quoted 2 lines · Mike Frysinger

do we really care about that when people are using #define DEBUG ? ?i'd say this is an unnecessary indirection ...

Yes, we do care about the memory footprint - no matter whether with or without DEBUG.

there's caring about it, and there's nitpicking over ~10 bytes per call site. -mike

#5 lore raw

PATCH v3 1/1 Add assert() for debug assertions

Quoted 6 lines · On Wednesday, September 07, 2011 18:29:16 Simon Glass wrote:
Quoted 5 lines · Mike Frysinger
Quoted 4 lines · On Wednesday, June 29, 2011 17:01:23 Wolfgang Denk wrote:
Quoted 2 lines · Mike Frysinger

do we really care about that when people are using #define DEBUG ?
i'd say this is an unnecessary indirection ...

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Yes, we do care about the memory footprint - no matter whether with or without DEBUG.

there's caring about it, and there's nitpicking over ~10 bytes per call site.

Did this go anywhere? I will resend...

i dont care enough to fight wolfgang over it
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20110908/a3566187/attachment.pgp

#6 lore raw

PATCH v3 1/1 Add assert() for debug assertions

Hi Mike / Wolfgang,

Quoted 10 lines · Mike Frysinger
Quoted 9 lines · On Wednesday, September 07, 2011 18:29:16 Simon Glass wrote:
Quoted 8 lines · Mike Frysinger
Quoted 6 lines · On Wednesday, June 29, 2011 17:01:23 Wolfgang Denk wrote:
Quoted 2 lines · Mike Frysinger

do we really care about that when people are using #define DEBUG ? ?i'd say this is an unnecessary indirection ...

Yes, we do care about the memory footprint - no matter whether with or without DEBUG.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

there's caring about it, and there's nitpicking over ~10 bytes per call site.

Did this go anywhere? I will resend...

i dont care enough to fight wolfgang over it

OK. Any other comments on this Wolfgang or is this finished?

Regards,
Simon

Quoted 2 lines

-mike

#7 lore raw

PATCH v3 1/1 Add assert() for debug assertions

In reply to Simon Glass

Dear Simon Glass,

Quoted 21 lines · In message <[email protected]> you wrote:

assert() is like BUG_ON() but compiles to nothing unless DEBUG is defined. This is useful when a condition is an error but a board reset is unlikely to fix it, so it is better to soldier on in hope. Assertion failures should be caught during development/test.

It turns out that assert() is defined separately in a few places in U-Boot with various meanings. This patch cleans up some of these.

Build errors exposed by this change (and defining DEBUG) are also fixed in this patch.

---
Changes in v2:
- Changed macros so that all code is compiled even if DEBUG is disabled

Changes in v3:
- Use panic() instead of printf()
- Use separate __assert_fail() function to reduce memory footprint
- Changed output format to match assert(3)

Quoted 6 lines
 common/dlmalloc.c |    7 -------
 include/common.h  |   21 +++++++++++++++++++++
 include/malloc.h  |    8 --------
 lib/qsort.c       |    5 -----
 lib/vsprintf.c    |    8 ++++++++
 5 files changed, 29 insertions(+), 20 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
How can you tell when sour cream goes bad?
#8 lore raw

PATCH 1/1 Fix warning: "assert" redefined

In reply to Simon Glass

Commit 21726a7 "Add assert() for debug assertions" caused build warnings for many systems:

In file included from bedbug.c:6:
/home/wd/git/u-boot/work/include/bedbug/bedbug.h:24:1: warning: "assert" redefined
In file included from bedbug.c:3:
/home/wd/git/u-boot/work/include/common.h:144:1: warning: this is the location of the previous definition In file included from cmd_bedbug.c:10:
/home/wd/git/u-boot/work/include/bedbug/bedbug.h:24:1: warning: "assert" redefined
In file included from cmd_bedbug.c:5:
/home/wd/git/u-boot/work/include/common.h:144:1: warning: this is the location of the previous definition

---
 include/bedbug/bedbug.h |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
include/bedbug/bedbug.h +0 -2
index 471215e..0c5d687 100644
@@ -21,8 +21,6 @@
 #endif
 #endif
 
-#define assert( condition ) if( (condition) ) _exit(0)
-
 #endif /* _BEDBUG_H */
 
 
Signature · 1 line
1.7.6
#9 lore raw

PATCH 1/1 Fix warning: "assert" redefined

Hi Wolfgang,

Quoted 12 lines · Wolfgang Denk

Commit 21726a7 "Add assert() for debug assertions" caused build warnings for many systems:

In file included from bedbug.c:6:
/home/wd/git/u-boot/work/include/bedbug/bedbug.h:24:1: warning: "assert" redefined
In file included from bedbug.c:3:
/home/wd/git/u-boot/work/include/common.h:144:1: warning: this is the location of the previous definition In file included from cmd_bedbug.c:10:
/home/wd/git/u-boot/work/include/bedbug/bedbug.h:24:1: warning: "assert" redefined
In file included from cmd_bedbug.c:5:
/home/wd/git/u-boot/work/include/common.h:144:1: warning: this is the location of the previous definition

I was wondering about that one :-) Thank you. I also ignored ubifs since it seemed to have its own system, and one in yaffs which was commented out. If it helps:

Regards,
Simon

Quoted 23 lines

---
?include/bedbug/bedbug.h | ? ?2 --
?1 files changed, 0 insertions(+), 2 deletions(-)

include/bedbug/bedbug.h +0 -0
index 471215e..0c5d687 100644
@@ -21,8 +21,6 @@

?#endif
?#endif

include/bedbug/bedbug.h +0 -2
-#define assert( condition ) if( (condition) ) _exit(0)
-

?#endif /* _BEDBUG_H */

--
1.7.6

#10 lore raw

PATCH 1/1 Fix warning: "assert" redefined

In reply to Wolfgang Denk

Dear Wolfgang Denk,

Quoted 12 lines · In message <[email protected]> you wrote:

Commit 21726a7 "Add assert() for debug assertions" caused build warnings for many systems:

In file included from bedbug.c:6:
/home/wd/git/u-boot/work/include/bedbug/bedbug.h:24:1: warning: "assert" redefined
In file included from bedbug.c:3:
/home/wd/git/u-boot/work/include/common.h:144:1: warning: this is the location of the previous definition In file included from cmd_bedbug.c:10:
/home/wd/git/u-boot/work/include/bedbug/bedbug.h:24:1: warning: "assert" redefined
In file included from cmd_bedbug.c:5:
/home/wd/git/u-boot/work/include/common.h:144:1: warning: this is the location of the previous definition

Quoted 5 lines
---
 include/bedbug/bedbug.h |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 9 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
I've been programming for (35-9=) 24 years myself,  and  I  find  the
best way to learn a new language is to *read* every example snippet I
can find, with a reference book close by. Eventually, the *rhythm* of
the language starts pounding in my head... and my fluency is close at
hand. That's how I learned Perl, and now I'm one of the drummers. :-)
         -- Randal L. Schwartz in <[email protected]>
#11 lore raw

PATCH 1/1 utx8245: fix build breakage due to assert()

In reply to Simon Glass

Commit 21726a7 "Add assert() for debug assertions" broke building the utx8245 board:

dlmalloc.c: In function 'do_check_chunk':
dlmalloc.c:1660: error: 'sz' undeclared (first use in this function) dlmalloc.c:1660: error: (Each undeclared identifier is reported only once dlmalloc.c:1660: error: for each function it appears in.) dlmalloc.c: In function 'do_check_free_chunk':
dlmalloc.c:1689: error: 'next' undeclared (first use in this function) dlmalloc.c: In function 'do_check_malloced_chunk':
dlmalloc.c:1748: error: 'sz' undeclared (first use in this function) dlmalloc.c:1750: error: 'room' undeclared (first use in this function)

---
 common/dlmalloc.c |    6 ------
 1 files changed, 0 insertions(+), 6 deletions(-)
common/dlmalloc.c +0 -6
index f2080c6..c645d73 100644
@@ -1647,9 +1647,7 @@ static void do_check_chunk(mchunkptr p)
 static void do_check_chunk(p) mchunkptr p;
 #endif
 {
-#if 0	/* causes warnings because assert() is off */
   INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
-#endif	/* 0 */
 
   /* No checkable chunk is mmapped */
   assert(!chunk_is_mmapped(p));
@@ -1671,9 +1669,7 @@ static void do_check_free_chunk(p) mchunkptr p;
 #endif
 {
   INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
-#if 0	/* causes warnings because assert() is off */
   mchunkptr next = chunk_at_offset(p, sz);
-#endif	/* 0 */
 
   do_check_chunk(p);
 
@@ -1737,10 +1733,8 @@ static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
 static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
 #endif
 {
-#if 0	/* causes warnings because assert() is off */
   INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
   long room = sz - s;
-#endif	/* 0 */
 
   do_check_inuse_chunk(p);
 
Signature · 1 line
1.7.6
#12 lore raw

PATCH 1/1 utx8245: fix build breakage due to assert()

Dear Wolfgang Denk,

Quoted 13 lines · In message <[email protected]> you wrote:

Commit 21726a7 "Add assert() for debug assertions" broke building the utx8245 board:

dlmalloc.c: In function 'do_check_chunk':
dlmalloc.c:1660: error: 'sz' undeclared (first use in this function) dlmalloc.c:1660: error: (Each undeclared identifier is reported only once dlmalloc.c:1660: error: for each function it appears in.) dlmalloc.c: In function 'do_check_free_chunk':
dlmalloc.c:1689: error: 'next' undeclared (first use in this function) dlmalloc.c: In function 'do_check_malloced_chunk':
dlmalloc.c:1748: error: 'sz' undeclared (first use in this function) dlmalloc.c:1750: error: 'room' undeclared (first use in this function)

Quoted 5 lines
---
 common/dlmalloc.c |    6 ------
 1 files changed, 0 insertions(+), 6 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
You can't evaluate a man by logic alone.
	-- McCoy, "I, Mudd", stardate 4513.3
#13 lore raw

PATCH 1/1 tegra2: fix warning: "assert" redefined

In reply to Simon Glass

Commit 21726a7 "Add assert() for debug assertions" caused build warnings for all tegra2 based boards:

clock.c:36:1: warning: "assert" redefined
In file included from clock.c:29:
include/common.h:144:1: warning: this is the location of the previous definition

---
 arch/arm/cpu/armv7/tegra2/clock.c |    8 --------
 1 files changed, 0 insertions(+), 8 deletions(-)
arch/arm/cpu/armv7/tegra2/clock.c +0 -8
index 67eed14..0aaed7d 100644
@@ -28,14 +28,6 @@
 #include <asm/arch/tegra2.h>
 #include <common.h>
 
-#ifdef DEBUG
-#define assert(x)	\
-	({ if (!(x)) printf("Assertion failure '%s' %s line %d\n", \
-		#x, __FILE__, __LINE__); })
-#else
-#define assert(x)
-#endif
-
 /*
  * Get the oscillator frequency, from the corresponding hardware configuration
  * field.
Signature · 1 line
1.7.6
#14 lore raw

PATCH 1/1 tegra2: fix warning: "assert" redefined

Dear Wolfgang Denk,

Quoted 7 lines · In message <[email protected]> you wrote:

Commit 21726a7 "Add assert() for debug assertions" caused build warnings for all tegra2 based boards:

clock.c:36:1: warning: "assert" redefined
In file included from clock.c:29:
include/common.h:144:1: warning: this is the location of the previous definition

Quoted 5 lines
---
 arch/arm/cpu/armv7/tegra2/clock.c |    8 --------
 1 files changed, 0 insertions(+), 8 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"More software projects have gone awry for lack of calendar time than
for all other causes combined."
                         - Fred Brooks, Jr., _The Mythical Man Month_
#15 lore raw

PATCH 1/1 tegra2: fix warning: "assert" redefined

Hi Wolfgang,

Quoted 9 lines · Wolfgang Denk

Dear Wolfgang Denk,

Quoted 6 lines · In message <[email protected]> you wrote:

Commit 21726a7 "Add assert() for debug assertions" caused build warnings for all tegra2 based boards:

clock.c:36:1: warning: "assert" redefined
In file included from clock.c:29:
include/common.h:144:1: warning: this is the location of the previous definition

Thanks again. The patch set I just sent out modifies the assert() in this file, but I will remove it in V2.

Regards,
Simon

Quoted 21 lines
Quoted 6 lines

---
?arch/arm/cpu/armv7/tegra2/clock.c | ? ?8 -------- ?1 files changed, 0 insertions(+), 8 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

--
DENX Software Engineering GmbH, ? ? MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de "More software projects have gone awry for lack of calendar time than for all other causes combined."
? ? ? ? ? ? ? ? ? ? ? ? - Fred Brooks, Jr., _The Mythical Man Month_