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 adb81a2805a38006ca5aa0cc900821f8e4023d25 Author: wangzhi16 <[email protected]> AuthorDate: Sun Sep 28 19:10:40 2025 +0800 sched/spinlock: Add backtrace information of lock holder in spinlock debugging. Add CONFIG_SPINLOCK_BACKTRACE: when enabled, record the full backtrace of the task that locked the spinlock and print it on misuse, replacing the single caller frame. Mark/clear the recorded backtrace in the spinlock mark_locked/mark_unlocked paths, and make the backtrace and pid-hash locks no-trace so the debug paths do not recurse into the spinlock mark machinery. Adapted for master: - sched/sched_gettcb.c: no g_pidhashlock in the master tree (already lock-free), the no-trace conversion has no target. - keeps the otherwise identical vela shape (stack field, conditional SPINLOCK_DEBUG_INIT, CONFIG_SPINLOCK_BACKTRACE). Assisted-By: mimo-v2.6-pro Co-authored-by: ouyangxiangzhen <[email protected]> Signed-off-by: wangzhi16 <[email protected]> Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/spinlock_type.h | 10 +++++++-- libs/libc/misc/lib_backtrace.c | 4 ++-- sched/Kconfig | 7 +++++++ sched/irq/irq_spinlock.c | 46 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/include/nuttx/spinlock_type.h b/include/nuttx/spinlock_type.h index 334d7d7eae9..d7287a17550 100644 --- a/include/nuttx/spinlock_type.h +++ b/include/nuttx/spinlock_type.h @@ -47,10 +47,16 @@ typedef struct spinlock_debug_s { sq_entry_t flink; FAR struct tcb_s *holder; - FAR void *caller; +# if defined(CONFIG_SPINLOCK_BACKTRACE) + FAR void *stack; +# endif } spinlock_debug_t; # define SPINLOCK_DEBUG_INFO spinlock_debug_t info; -# define SPINLOCK_DEBUG_INIT {{NULL}, NULL, NULL} +# if defined(CONFIG_SPINLOCK_BACKTRACE) +# define SPINLOCK_DEBUG_INIT {{NULL}, NULL, NULL}, +# else +# define SPINLOCK_DEBUG_INIT {{NULL}, NULL}, +# endif #else # define SPINLOCK_DEBUG_INFO # define SPINLOCK_DEBUG_INIT diff --git a/libs/libc/misc/lib_backtrace.c b/libs/libc/misc/lib_backtrace.c index e4f697acaf1..808fabbfaa3 100644 --- a/libs/libc/misc/lib_backtrace.c +++ b/libs/libc/misc/lib_backtrace.c @@ -43,8 +43,8 @@ ****************************************************************************/ #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) -# define backtrace_lock(bp) spin_lock_irqsave(&(bp)->lock) -# define backtrace_unlock(bp, flags) spin_unlock_irqrestore(&(bp)->lock, flags) +# define backtrace_lock(bp) spin_lock_irqsave_notrace(&(bp)->lock) +# define backtrace_unlock(bp, flags) spin_unlock_irqrestore_notrace(&(bp)->lock, flags) #else # define backtrace_lock(bp) ((irqstate_t)nxmutex_lock(&(bp)->lock)) # define backtrace_unlock(bp, flags) \ diff --git a/sched/Kconfig b/sched/Kconfig index 94782ac8720..820b6b811ac 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -326,6 +326,13 @@ config SPINLOCK_DEBUG ---help--- Enable to support debug tools for spinlock. +config SPINLOCK_BACKTRACE + bool "Enable spinlock backtrace" + depends on SPINLOCK_DEBUG && LIBC_BACKTRACE_DEPTH > 0 + default n + ---help--- + Enable to record backtrace when a task holds a spinlock. + config SPINLOCK_INLINE bool "Inline spinlock helpers" default !DEFAULT_SMALL diff --git a/sched/irq/irq_spinlock.c b/sched/irq/irq_spinlock.c index 34805a05d18..9b4ec811408 100644 --- a/sched/irq/irq_spinlock.c +++ b/sched/irq/irq_spinlock.c @@ -41,6 +41,7 @@ #include <debug.h> #include <sys/types.h> #include <arch/irq.h> +#include <execinfo.h> #include "sched/sched.h" #include "irq/irq.h" @@ -53,6 +54,34 @@ * Public Functions ****************************************************************************/ +#ifdef CONFIG_SPINLOCK_BACKTRACE + +/**************************************************************************** + * Name: spinlock_add_backtrace / spinlock_remove_backtrace + * + * Description: + * Record/remove the backtrace of the caller who locks the spinlock. + * + * NOTE: backtrace_record is relatively complex and involves malloc + * (which may cause context switching). + * + ****************************************************************************/ + +static void spinlock_add_backtrace(FAR spinlock_debug_t *info) +{ + info->stack = backtrace_record(0); +} + +static void spinlock_remove_backtrace(FAR spinlock_debug_t *info) +{ + backtrace_remove(info->stack); + info->stack = NULL; +} +#else +# define spinlock_add_backtrace(info) +# define spinlock_remove_backtrace(info) +#endif + #ifdef CONFIG_SPINLOCK_DEBUG static void dump_spinlock(FAR sq_queue_t *hold_spinlock) { @@ -61,8 +90,17 @@ static void dump_spinlock(FAR sq_queue_t *hold_spinlock) sq_for_every(hold_spinlock, entry) { FAR spinlock_debug_t *info = (FAR spinlock_debug_t *)entry; - - _alert("Spinlock(%p) has been locked by %p\n", info, info->caller); +# ifdef CONFIG_SPINLOCK_BACKTRACE + char buf[BACKTRACE_BUFFER_SIZE(CONFIG_LIBC_BACKTRACE_DEPTH)]; + FAR void **stack; + int depth; + + stack = backtrace_get(info->stack, &depth); + backtrace_format(buf, sizeof(buf), stack, depth); + _alert("Spinlock(%p) backtrace: %s\n", info, buf); +# else + _alert("Spinlock(%p) has been locked\n", info); +# endif } } @@ -157,7 +195,7 @@ void spinlock_mark_locked(FAR spinlock_debug_t *info) /* Set info and enqueue. */ info->holder = rtcb; - info->caller = return_address(0); + spinlock_add_backtrace(info); sq_addlast(&info->flink, &rtcb->hold_spinlock); /* Reset wait_spinlock. */ @@ -211,7 +249,7 @@ void spinlock_mark_unlocked(FAR spinlock_debug_t *info) /* Reset info and dequeue. */ info->holder = NULL; - info->caller = NULL; + spinlock_remove_backtrace(info); sq_remlast(&rtcb->hold_spinlock); }
