[U-Boot] [PATCH v2 3/5] Introduce CONFIG_ENABLE_BUG_CHECKS to disable BUG{_ON} by default

Masahiro Yamada yamada.masahiro at socionext.com
Mon Dec 4 03:37:02 UTC 2017


BUG() and BUG_ON() are generally used to test a condition that should
never happen.  If it does, it is a bug.

Linux always enables them, but doing so in U-Boot causes image size
problems on some platforms.  Introduce CONFIG_ENABLE_BUG_CHECKS to
make them no-op by default.  Platforms without image size constraint
are free to enable this option to catch bugs easily.

Likewise, silence WARN_ON() unless this option is enabled.

Suggested-by: Tom Rini <trini at konsulko.com>
Signed-off-by: Masahiro Yamada <yamada.masahiro at socionext.com>
---

Changes in v2:
  - Newly added

 include/linux/bug.h | 9 ++++++++-
 lib/Kconfig         | 7 +++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/linux/bug.h b/include/linux/bug.h
index f07bb71..ac1c7de 100644
--- a/include/linux/bug.h
+++ b/include/linux/bug.h
@@ -6,17 +6,24 @@
 #include <linux/compiler.h>
 #include <linux/printk.h>
 
+#ifdef CONFIG_ENABLE_BUG_CHECKS
 #define BUG() do { \
 	printk("BUG at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
 	panic("BUG!"); \
 } while (0)
+#define __WARN() 	\
+	printk("WARNING at %s:%d/%s()!\n", __FILE__, __LINE__, __func__)
+#else
+#define BUG()
+#define __WARN()
+#endif
 
 #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
 
 #define WARN_ON(condition) ({						\
 	int __ret_warn_on = !!(condition);				\
 	if (unlikely(__ret_warn_on))					\
-		printk("WARNING at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
+		__WARN();						\
 	unlikely(__ret_warn_on);					\
 })
 
diff --git a/lib/Kconfig b/lib/Kconfig
index 00ac650..36b1b3b 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -45,6 +45,13 @@ config USE_TINY_PRINTF
 
 	  The supported format specifiers are %c, %s, %u/%d and %x.
 
+config ENABLE_BUG_CHECKS
+	bool "Enable BUG/BUG_ON() checks and WARN_ON() logs"
+	help
+	  BUG() and BUG_ON() are no-op by default.  This option enables
+	  them to print noisy messages, then reboot or halt the system.
+	  It also enables WARN_ON() messages.
+
 config PANIC_HANG
 	bool "Do not reset the system on fatal error"
 	help
-- 
2.7.4



More information about the U-Boot mailing list