Skip to content

Commit

Permalink
debug/assert: decouple configuration of show file name feature
Browse files Browse the repository at this point in the history
add new config CONFIG_ASSERTIONS_FILENAME to library call assert(3) to decouple with CONFIG_DEBUG_ASSERTIONS

| Function         |CONFIG                            | Show filename/line |
| ---              | ---                              | ---                |
|assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=y      | Yes                |
|assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=n      | No                 |
|DEBUGASSERT()     |CONFIG_DEBUG_ASSERTIONS_FILENAME=y| Yes                |
|DEBUGASSERT()     |CONFIG_DEBUG_ASSERTIONS_FILENAME=n| No                 |

Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao committed Oct 19, 2023
1 parent eed42a1 commit 6557657
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
9 changes: 9 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,15 @@ config NDEBUG
bool "Define NDEBUG globally"
default !DEBUG_ASSERTIONS

config ASSERTIONS_FILENAME
bool "Enable library call assert(3) show the file name"
default y if DEBUG_ASSERTIONS_FILENAME
default !DEFAULT_SMALL
---help---
This option can display the file information of the library call assert(3)
function when it is enabled. This option maybe will take up a lot
of space from applications.

config DEBUG_ALERT
bool
default n
Expand Down
92 changes: 52 additions & 40 deletions include/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,68 @@
#undef DEBUGASSERT /* Like ASSERT, but only if CONFIG_DEBUG_ASSERTIONS is defined */
#undef DEBUGVERIFY /* Like VERIFY, but only if CONFIG_DEBUG_ASSERTIONS is defined */

#if !defined(CONFIG_HAVE_FILENAME) || !defined(CONFIG_DEBUG_ASSERTIONS_FILENAME)
/* Macro to define the assertions file name and file line
* | Function |CONFIG | Show name/line |
* | --- | --- | --- |
* |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=y | Yes |
* |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=n | No |
* |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=y| Yes |
* |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=n| No |
*/

#ifdef CONFIG_HAVE_FILENAME
# ifdef CONFIG_DEBUG_ASSERTIONS_FILENAME
# define __DEBUG_ASSERT_FILE__ __FILE__
# define __DEBUG_ASSERT_LINE__ __LINE__
# endif
# ifdef CONFIG_ASSERTIONS_FILENAME
# define __ASSERT_FILE__ __FILE__
# define __ASSERT_LINE__ __LINE__
# endif
#endif

#ifndef __DEBUG_ASSERT_FILE__
# define __DEBUG_ASSERT_FILE__ 0
# define __DEBUG_ASSERT_LINE__ 0
#endif

#ifndef __ASSERT_FILE__
# define __ASSERT_FILE__ 0
# define __ASSERT_LINE__ 0
#else
# define __ASSERT_FILE__ __FILE__
# define __ASSERT_LINE__ __LINE__
#endif

#define PANIC() __assert(__ASSERT_FILE__, __ASSERT_LINE__, "panic")
#define PANIC_WITH_REGS(msg, regs) _assert(__ASSERT_FILE__, \
__ASSERT_LINE__, msg, regs)

#ifdef CONFIG_DEBUG_ASSERTIONS_EXPRESSION
# define ASSERT(f) \
do \
{ \
if (predict_false(!(f))) \
__assert(__ASSERT_FILE__, \
__ASSERT_LINE__, #f); \
} \
#define __ASSERT(f, file, line, _f) \
do \
{ \
if (predict_false(!(f))) \
__assert(file, line, _f); \
} \
while (0)

# define VERIFY(f) \
do \
{ \
if (predict_false((f) < 0)) \
__assert(__ASSERT_FILE__, \
__ASSERT_LINE__, #f); \
} \
while (0)
#else
# define ASSERT(f) \
do \
{ \
if (predict_false(!(f))) \
__assert(__ASSERT_FILE__, \
__ASSERT_LINE__, 0); \
} \
#define __VERIFY(f, file, line, _f) \
do \
{ \
if (predict_false((f) < 0)) \
__assert(file, line, _f); \
} \
while (0)

# define VERIFY(f) \
do \
{ \
if (predict_false((f) < 0)) \
__assert(__ASSERT_FILE__, \
__ASSERT_LINE__, 0); \
} \
while (0)
#ifdef CONFIG_DEBUG_ASSERTIONS_EXPRESSION
# define _ASSERT(f,file,line) __ASSERT(f, file, line, #f)
# define _VERIFY(f,file,line) __VERIFY(f, file, line, #f)
#else
# define _ASSERT(f,file,line) __ASSERT(f, file, line, NULL)
# define _VERIFY(f,file,line) __VERIFY(f, file, line, NULL)
#endif

#ifdef CONFIG_DEBUG_ASSERTIONS
# define DEBUGPANIC() PANIC()
# define DEBUGASSERT(f) ASSERT(f)
# define DEBUGVERIFY(f) VERIFY(f)
# define DEBUGPANIC() __assert(__DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__, "panic")
# define DEBUGASSERT(f) _ASSERT(f, __DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__)
# define DEBUGVERIFY(f) _VERIFY(f, __DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__)
#else
# define DEBUGPANIC()
# define DEBUGASSERT(f) ((void)(1 || (f)))
Expand All @@ -109,10 +117,14 @@

#ifdef NDEBUG
# define assert(f) ((void)(1 || (f)))
# define VERIFY(f) assert(f)
#else
# define assert(f) ASSERT(f)
# define assert(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__)
# define VERIFY(f) _VERIFY(f, __ASSERT_FILE__, __ASSERT_LINE__)
#endif

#define ASSERT(f) assert(f)

/* Suppress 3rd party library redefine _assert/__assert */

#define _assert _assert
Expand Down

0 comments on commit 6557657

Please sign in to comment.