You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gu...@apache.org on 2023/01/24 18:00:27 UTC

[nuttx] 01/02: assert: Log the assertion expression in case of fail

This is an automated email from the ASF dual-hosted git repository.

gustavonihei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 43e7b13697195c37166de09e508b71fe6cbb424a
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Jan 22 19:31:32 2023 +0800

    assert: Log the assertion expression in case of fail
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 arch/arm/src/nrf52/nrf52_sdc.c |  6 ++----
 include/assert.h               | 17 +++++++++--------
 include/sys/syscall_lookup.h   |  2 +-
 libs/libc/assert/lib_assert.c  |  4 ++--
 libs/libc/libc.csv             |  2 +-
 sched/misc/assert.c            | 19 ++++++++++---------
 syscall/syscall.csv            |  2 +-
 7 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/arch/arm/src/nrf52/nrf52_sdc.c b/arch/arm/src/nrf52/nrf52_sdc.c
index cdf3d99488..12a9f7e2ef 100644
--- a/arch/arm/src/nrf52/nrf52_sdc.c
+++ b/arch/arm/src/nrf52/nrf52_sdc.c
@@ -237,8 +237,7 @@ static int bt_hci_send(struct bt_driver_s *btdev,
 
 static void sdc_fault_handler(const char *file, const uint32_t line)
 {
-  _alert("SoftDevice Controller Fault\n");
-  _assert(file, line);
+  _assert(file, line, "SoftDevice Controller Fault");
 }
 
 /****************************************************************************
@@ -247,8 +246,7 @@ static void sdc_fault_handler(const char *file, const uint32_t line)
 
 static void mpsl_assert_handler(const char *const file, const uint32_t line)
 {
-  _alert("MPSL assertion failed\n");
-  _assert(file, line);
+  _assert(file, line, "MPSL assertion failed");
 }
 
 /****************************************************************************
diff --git a/include/assert.h b/include/assert.h
index eabf6163bc..32c760679d 100644
--- a/include/assert.h
+++ b/include/assert.h
@@ -42,14 +42,14 @@
 #undef DEBUGASSERT  /* Like ASSERT, but only if CONFIG_DEBUG_ASSERTIONS is defined */
 #undef DEBUGVERIFY  /* Like VERIFY, but only if CONFIG_DEBUG_ASSERTIONS is defined */
 
-#ifdef CONFIG_HAVE_FILENAME
-#  define PANIC()        __assert(__FILE__, __LINE__)
-#else
-#  define PANIC()        __assert("unknown", 0)
+#ifndef CONFIG_HAVE_FILENAME
+#  define __FILE__       "unknown"
+#  define __LINE__       0
 #endif
 
-#define ASSERT(f)        do { if (!(f)) PANIC(); } while (0)
-#define VERIFY(f)        do { if ((f) < 0) PANIC(); } while (0)
+#define PANIC()          __assert(__FILE__, __LINE__, "panic")
+#define ASSERT(f)        do { if (!(f)) __assert(__FILE__, __LINE__, #f); } while (0)
+#define VERIFY(f)        do { if ((f) < 0) __assert(__FILE__, __LINE__, #f); } while (0)
 
 #ifdef CONFIG_DEBUG_ASSERTIONS
 #  define DEBUGPANIC()   PANIC()
@@ -115,7 +115,7 @@ extern "C"
  *
  ****************************************************************************/
 
-void _assert(FAR const char *filename, int linenum);
+void _assert(FAR const char *filename, int linenum, FAR const char *msg);
 
 /****************************************************************************
  * Name: __assert
@@ -125,7 +125,8 @@ void _assert(FAR const char *filename, int linenum);
  *
  ****************************************************************************/
 
-void __assert(FAR const char *filename, int linenum) noreturn_function;
+void __assert(FAR const char *filename, int linenum,
+              FAR const char *msg) noreturn_function;
 
 #undef EXTERN
 #ifdef __cplusplus
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index 9ad6ef3d03..e1bd2efa1b 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -25,7 +25,7 @@
  */
 
 SYSCALL_LOOKUP1(_exit,                     1)
-SYSCALL_LOOKUP(_assert,                    2)
+SYSCALL_LOOKUP(_assert,                    3)
 SYSCALL_LOOKUP(getpid,                     0)
 SYSCALL_LOOKUP(gettid,                     0)
 SYSCALL_LOOKUP(prctl,                      2)
diff --git a/libs/libc/assert/lib_assert.c b/libs/libc/assert/lib_assert.c
index 7a0d96f39e..5ff55c86a3 100644
--- a/libs/libc/assert/lib_assert.c
+++ b/libs/libc/assert/lib_assert.c
@@ -31,8 +31,8 @@
  * Public Functions
  ****************************************************************************/
 
-void __assert(FAR const char *filename, int linenum)
+void __assert(FAR const char *filename, int linenum, FAR const char *msg)
 {
-  _assert(filename, linenum);
+  _assert(filename, linenum, msg);
   exit(EXIT_FAILURE);
 }
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index a6403a2ad0..5059f905cb 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -1,7 +1,7 @@
 "__errno","errno.h","defined(CONFIG_BUILD_FLAT)","FAR int *"
 "__stack_chk_fail","ssp/ssp.h","defined(CONFIG_STACK_CANARIES)","void","void"
 "_alert","debug.h","!defined(CONFIG_CPP_HAVE_VARARGS) && defined(CONFIG_DEBUG_ERROR)","void","FAR const char *","..."
-"_assert","assert.h","","void","FAR const char *","int"
+"__assert","assert.h","","void","FAR const char *","int","FAR const char *"
 "_err","debug.h","!defined(CONFIG_CPP_HAVE_VARARGS) && defined(CONFIG_DEBUG_ERROR)","void","FAR const char *","..."
 "_info","debug.h","!defined(CONFIG_CPP_HAVE_VARARGS) && defined(CONFIG_DEBUG_INFO)","void","FAR const char *","..."
 "_warn","debug.h","!defined(CONFIG_CPP_HAVE_VARARGS) && defined(CONFIG_DEBUG_WARN)","void","FAR const char *","..."
diff --git a/sched/misc/assert.c b/sched/misc/assert.c
index 3360a182e1..034b0a028a 100644
--- a/sched/misc/assert.c
+++ b/sched/misc/assert.c
@@ -442,7 +442,7 @@ static void show_tasks(void)
  * Public Functions
  ****************************************************************************/
 
-void _assert(FAR const char *filename, int linenum)
+void _assert(FAR const char *filename, int linenum, FAR const char *msg)
 {
   FAR struct tcb_s *rtcb = running_task();
   struct utsname name;
@@ -471,19 +471,20 @@ void _assert(FAR const char *filename, int linenum)
 
 #ifdef CONFIG_SMP
 #  if CONFIG_TASK_NAME_SIZE > 0
-  _alert("Assertion failed CPU%d at file: %s:%d task: %s %p\n",
-         up_cpu_index(), filename, linenum, rtcb->name, rtcb->entry.main);
+  _alert("Assertion failed: %s at file: %s:%d task(CPU%d): %s %p\n",
+         msg, filename, linenum, up_cpu_index(), rtcb->name,
+         rtcb->entry.main);
 #  else
-  _alert("Assertion failed CPU%d at file: %s:%d task: %p\n",
-         up_cpu_index(), filename, linenum, rtcb->entry.main);
+  _alert("Assertion failed: %s at file: %s:%d task(CPU%d): %p\n",
+         msg, filename, linenum, up_cpu_index(), rtcb->entry.main);
 #  endif
 #else
 #  if CONFIG_TASK_NAME_SIZE > 0
-  _alert("Assertion failed at file: %s:%d task: %s %p\n",
-         filename, linenum, rtcb->name, rtcb->entry.main);
+  _alert("Assertion failed: %s at file: %s:%d task: %s %p\n",
+         msg, filename, linenum, rtcb->name, rtcb->entry.main);
 #  else
-  _alert("Assertion failed at file: %s:%d task: %p\n",
-         filename, linenum, rtcb->entry.main);
+  _alert("Assertion failed: %s at file: %s:%d task: %p\n",
+         msg, filename, linenum, rtcb->entry.main);
 #  endif
 #endif
 
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 66a982c18f..01a2d17b20 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -1,5 +1,5 @@
 "_exit","unistd.h","","noreturn","int"
-"_assert","assert.h","","void","FAR const char *","int"
+"_assert","assert.h","","void","FAR const char *","int","FAR const char *"
 "accept4","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct sockaddr *","FAR socklen_t *","int"
 "adjtime","sys/time.h","defined(CONFIG_CLOCK_TIMEKEEPING)","int","FAR const struct timeval *","FAR struct timeval *"
 "aio_cancel","aio.h","defined(CONFIG_FS_AIO)","int","int","FAR struct aiocb *"