[Add][kernel/thread]Add thread overflow hook and testcases:thread_overflow_tc.c.

This commit is contained in:
Rbb666
2025-09-02 16:46:55 +08:00
committed by R b b666
parent f8a37bc42e
commit 3c17851fe2
4 changed files with 315 additions and 1 deletions
@@ -43,6 +43,7 @@ if GetDepend(['UTEST_MAILBOX_TC']):
if GetDepend(['UTEST_THREAD_TC']):
src += ['thread_tc.c']
src += ['thread_overflow_tc.c']
if GetDepend(['UTEST_DEVICE_TC']):
src += ['device_tc.c']
File diff suppressed because it is too large Load Diff
+1
View File
@@ -239,6 +239,7 @@ void rt_exit_critical_safe(rt_base_t critical_level);
rt_uint16_t rt_critical_level(void);
#ifdef RT_USING_HOOK
void rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread));
void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to));
void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid));
#endif /* RT_USING_HOOK */
+43 -1
View File
@@ -8,6 +8,7 @@
* Change Logs:
* Date Author Notes
* 2024-01-18 Shell Separate scheduling related codes from thread.c, scheduler_.*
* 2025-09-01 Rbb666 Add thread stack overflow hook.
*/
#define DBG_TAG "kernel.sched"
@@ -411,6 +412,35 @@ rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t pri
}
#ifdef RT_USING_OVERFLOW_CHECK
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
static rt_err_t (*rt_stack_overflow_hook)(struct rt_thread *thread);
/**
* @brief Set a hook function to be called when stack overflow is detected
*
* @param hook The function pointer to be called when stack overflow is detected.
* Pass RT_NULL to disable the hook.
* The hook function should return RT_EOK if overflow is handled,
* otherwise the system will halt in an infinite loop.
*
* @note The hook function must be simple and never be blocked or suspended.
* This function is typically used for error logging, recovery, or graceful shutdown.
*
* @details Hook function behavior:
* - Return RT_EOK: System continues execution after overflow handling
* - Return any other value: System enters infinite loop (halt)
* - Hook is called from rt_scheduler_stack_check() when overflow is detected
* - Hook execution context depends on when stack check is performed
*
* @see rt_scheduler_stack_check()
*/
void rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread))
{
rt_stack_overflow_hook = hook;
}
#endif /* RT_USING_HOOK */
/**
* @brief Check thread stack for overflow or near-overflow conditions
*
@@ -452,10 +482,22 @@ void rt_scheduler_stack_check(struct rt_thread *thread)
(rt_uintptr_t)thread->stack_addr + (rt_uintptr_t)thread->stack_size)
{
rt_base_t dummy = 1;
rt_err_t hook_result = -RT_ERROR;
LOG_E("thread:%s stack overflow\n", thread->parent.name);
while (dummy);
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
if (rt_stack_overflow_hook != RT_NULL)
{
hook_result = rt_stack_overflow_hook(thread);
}
#endif /* RT_USING_HOOK */
/* If hook handled the overflow successfully, don't enter infinite loop */
if (hook_result != RT_EOK)
{
while (dummy);
}
}
#endif /* RT_USING_HW_STACK_GUARD */
#ifdef ARCH_CPU_STACK_GROWS_UPWARD