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

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

commit a235efc38f8cc451a3483274e68cb3a961c41fd3
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 27 04:09:47 2026 +0100

    libc/misc: Make backtrace() and dump_stack() functions for programs.
    
    <execinfo.h> defined backtrace() and dump_stack() as macros on
    sched_backtrace() and sched_dumpstack(), and it included <nuttx/sched.h>
    for them.  This broke programs in two ways:
    
    - Without CONFIG_SCHED_BACKTRACE, <sched.h> makes sched_backtrace() the
      constant 0.  A C++ call ::backtrace(buffer, size) then became ::0.
    - <nuttx/sched.h> brings the kernel list macros (list_add_tail(),
      list_entry() and others) into every program that includes
      <execinfo.h>, where they clash with the program's own names.
    
    Now a program gets backtrace() and dump_stack() as functions of the C
    library (lib_execinfo.c).  They act on the calling thread, and without
    CONFIG_SCHED_BACKTRACE backtrace() stores no address.  <execinfo.h>
    includes <sched.h> and no kernel header.  The OS code (__KERNEL__) keeps
    the macros and <nuttx/sched.h>.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/execinfo.h                                 | 25 ++++++-
 libs/libc/misc/CMakeLists.txt                      |  1 +
 libs/libc/misc/Make.defs                           |  5 +-
 .../execinfo.h => libs/libc/misc/lib_execinfo.c    | 87 ++++++++++------------
 4 files changed, 65 insertions(+), 53 deletions(-)

diff --git a/include/execinfo.h b/include/execinfo.h
index 4f4238f23bf..4d329b23bed 100644
--- a/include/execinfo.h
+++ b/include/execinfo.h
@@ -27,12 +27,24 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/sched.h>
+#include <nuttx/config.h>
 
 #include <assert.h>
+#include <errno.h>
+#include <sched.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <sys/types.h>
 
+/* The OS calls the backtrace functions of the scheduler for its own thread
+ * directly.  A program gets backtrace() and dump_stack() as functions of
+ * the C library, and no kernel header.
+ */
+
+#ifdef __KERNEL__
+#  include <nuttx/sched.h>
+#endif
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -45,8 +57,10 @@
 
 #define BACKTRACE_BUFFER_SIZE(d) (BACKTRACE_PTR_FMT_WIDTH * (d) + 1)
 
-#define backtrace(b, s) sched_backtrace(_SCHED_GETTID(), b, s, 0)
-#define dump_stack()    sched_dumpstack(_SCHED_GETTID())
+#ifdef __KERNEL__
+#  define backtrace(b, s) sched_backtrace(_SCHED_GETTID(), b, s, 0)
+#  define dump_stack()    sched_dumpstack(_SCHED_GETTID())
+#endif
 
 #ifndef CONFIG_LIBC_BACKTRACE_DEPTH
 #  define CONFIG_LIBC_BACKTRACE_DEPTH 0
@@ -61,6 +75,11 @@ extern "C"
 {
 #endif
 
+#ifndef __KERNEL__
+int backtrace(FAR void **buffer, int size);
+void dump_stack(void);
+#endif
+
 FAR char **backtrace_symbols(FAR void *const *buffer, int size);
 void backtrace_symbols_fd(FAR void *const *buffer, int size, int fd);
 int backtrace_format(FAR char *buffer, int size,
diff --git a/libs/libc/misc/CMakeLists.txt b/libs/libc/misc/CMakeLists.txt
index 49e7ad9c93c..b7db7c40f68 100644
--- a/libs/libc/misc/CMakeLists.txt
+++ b/libs/libc/misc/CMakeLists.txt
@@ -106,6 +106,7 @@ list(
   lib_crc8table.c
   lib_glob.c
   lib_backtrace.c
+  lib_execinfo.c
   lib_ftok.c
   lib_err.c
   lib_instrument.c)
diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs
index f754fa5371d..83632819034 100644
--- a/libs/libc/misc/Make.defs
+++ b/libs/libc/misc/Make.defs
@@ -55,9 +55,8 @@ CSRCS += lib_crc64.c lib_crc32.c lib_crc16.c lib_crc16ccitt.c
 CSRCS += lib_crc8.c lib_crc8ccitt.c lib_crc8table.c lib_crc8rohc.c
 CSRCS += lib_crc8h1d.c lib_crc8h2f.c lib_crc16h1021.c lib_crc16h8005.c
 CSRCS += lib_crc32h04c11db7.c lib_crc32hf4acfb13.c lib_crc64emac.c
-CSRCS += lib_backtrace.c lib_ftok.c lib_err.c lib_instrument.c lib_glob.c
-CSRCS += lib_crc16xmodem.c lib_xorsum16.c
-CSRCS += lib_fdcheck.c lib_tag.c
+CSRCS += lib_backtrace.c lib_execinfo.c lib_ftok.c lib_err.c lib_instrument.c
+CSRCS += lib_glob.c lib_crc16xmodem.c lib_xorsum16.c lib_fdcheck.c lib_tag.c
 
 # Keyboard driver encoder/decoder
 
diff --git a/include/execinfo.h b/libs/libc/misc/lib_execinfo.c
similarity index 52%
copy from include/execinfo.h
copy to libs/libc/misc/lib_execinfo.c
index 4f4238f23bf..ef0cf871559 100644
--- a/include/execinfo.h
+++ b/libs/libc/misc/lib_execinfo.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * include/execinfo.h
+ * libs/libc/misc/lib_execinfo.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,66 +20,59 @@
  *
  ****************************************************************************/
 
-#ifndef __INCLUDE_EXECINFO_H
-#define __INCLUDE_EXECINFO_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/sched.h>
-
-#include <assert.h>
-#include <stdio.h>
-#include <sys/types.h>
-
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-/* 3: ' 0x' prefix */
-
-#define BACKTRACE_PTR_FMT_WIDTH  ((int)sizeof(uintptr_t) * 2 + 3)
+#include <nuttx/config.h>
 
-/* Buffer size needed to hold formatted `depth` backtraces */
+#include <execinfo.h>
+#include <sched.h>
+#include <unistd.h>
 
-#define BACKTRACE_BUFFER_SIZE(d) (BACKTRACE_PTR_FMT_WIDTH * (d) + 1)
+/* The OS code uses the macros of <execinfo.h> (__KERNEL__): this file has
+ * the functions only for programs.
+ */
 
-#define backtrace(b, s) sched_backtrace(_SCHED_GETTID(), b, s, 0)
-#define dump_stack()    sched_dumpstack(_SCHED_GETTID())
+#ifndef __KERNEL__
 
-#ifndef CONFIG_LIBC_BACKTRACE_DEPTH
-#  define CONFIG_LIBC_BACKTRACE_DEPTH 0
-#endif
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
 
 /****************************************************************************
- * Public Function Prototypes
+ * Name: backtrace
+ *
+ * Description:
+ *   Store the return addresses of the calling thread in buffer, as the
+ *   backtrace() macro of the OS code does.  Without CONFIG_SCHED_BACKTRACE
+ *   there are none.
+ *
+ * Input Parameters:
+ *   buffer - Where the addresses go
+ *   size   - The number of entries of buffer
+ *
+ * Returned Value:
+ *   The number of addresses stored in buffer.
+ *
  ****************************************************************************/
 
-#if defined(__cplusplus)
-extern "C"
+int backtrace(FAR void **buffer, int size)
 {
-#endif
-
-FAR char **backtrace_symbols(FAR void *const *buffer, int size);
-void backtrace_symbols_fd(FAR void *const *buffer, int size, int fd);
-int backtrace_format(FAR char *buffer, int size,
-                     FAR void *backtrace[], int depth);
+  return sched_backtrace(gettid(), buffer, size, 1);
+}
 
-#if CONFIG_LIBC_BACKTRACE_DEPTH > 0
-FAR void *backtrace_record(int skip);
-void backtrace_remove(FAR void *entry);
-FAR void **backtrace_get(FAR void *entry, FAR int *size);
-void backtrace_dump(void);
-#else
-#  define backtrace_record(skip) (NULL)
-#  define backtrace_remove(entry)
-#  define backtrace_get(entry, size) (*(size)=0)
-#  define backtrace_dump()
-#endif
+/****************************************************************************
+ * Name: dump_stack
+ *
+ * Description:
+ *   Write the backtrace of the calling thread to the system log.
+ *
+ ****************************************************************************/
 
-#if defined(__cplusplus)
+void dump_stack(void)
+{
+  sched_dumpstack(gettid());
 }
-#endif
 
-#endif /* __INCLUDE_EXECINFO_H */
+#endif /* !__KERNEL__ */

Reply via email to