From a940dda69a939f62cd686f0d2e06bb7302a13f27 Mon Sep 17 00:00:00 2001 From: zhangdengyu Date: Fri, 28 Oct 2022 16:02:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0mutex=20trace?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20Close:=20#I5YJOZ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdengyu Change-Id: Id36ed4f4d23b9aa59ae9ee8cdb17c06d7e3c151b --- Kconfig | 10 +++++++++- arch/include/los_arch.h | 14 ++++++++++++++ kal/cmsis/cmsis_liteos2.c | 10 ++++++++++ kal/posix/src/pthread_mutex.c | 32 ++++++++++++++++++++++++++++++++ kernel/include/los_mux.h | 11 ++++++++++- kernel/src/los_mux.c | 10 +++++++--- 6 files changed, 82 insertions(+), 5 deletions(-) diff --git a/Kconfig b/Kconfig index cffe28f3..17ade6bf 100644 --- a/Kconfig +++ b/Kconfig @@ -521,6 +521,14 @@ config DEBUG_QUEUE help Answer Y to enable debug queue. +config MUTEX_CREATE_TRACE + bool "Enable Mutex Trace Debugging" + default n + depends on ARCH_ARM + depends on DEBUG_KERNEL + help + Answer Y to enable debug mutex trace. + config DEBUG_DEADLOCK bool "Enable Mutex Deadlock Debugging" default n @@ -609,7 +617,7 @@ config VM_OVERLAP_CHECK depends on DEBUG_VERSION && MEM_DEBUG help Answer Y to enable vm overlap check. - + config TASK_MEM_USED bool "Enable show task mem used or not" default n diff --git a/arch/include/los_arch.h b/arch/include/los_arch.h index 258ab024..4f7311b7 100644 --- a/arch/include/los_arch.h +++ b/arch/include/los_arch.h @@ -62,6 +62,13 @@ STATIC INLINE UINTPTR ArchMspGet(VOID) __asm("mrs %0, msp" : "=r" (msp)); return msp; } + +STATIC INLINE UINTPTR ArchLRGet(VOID) +{ + UINTPTR lr; + __asm("mov %0, lr" : "=r" (lr)); + return lr; +} #elif defined(__CLANG_ARM) || defined(__GNUC__) STATIC INLINE UINTPTR ArchSpGet(VOID) { @@ -83,6 +90,13 @@ STATIC INLINE UINTPTR ArchMspGet(VOID) __asm volatile("mrs %0, msp" : "=r" (msp)); return msp; } + +STATIC INLINE UINTPTR ArchLRGet(VOID) +{ + UINTPTR lr; + __asm volatile("mov %0, lr" : "=r" (lr)); + return lr; +} #else /* Other platforms to be improved */ #endif diff --git a/kal/cmsis/cmsis_liteos2.c b/kal/cmsis/cmsis_liteos2.c index 7da55551..8ed9bb2f 100644 --- a/kal/cmsis/cmsis_liteos2.c +++ b/kal/cmsis/cmsis_liteos2.c @@ -42,6 +42,9 @@ #include "los_task.h" #include "los_timer.h" #include "los_debug.h" +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) +#include "los_arch.h" +#endif #include "string.h" #include "securec.h" @@ -1002,6 +1005,10 @@ osMutexId_t osMutexNew(const osMutexAttr_t *attr) UINT32 ret; UINT32 muxId; +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + UINTPTR regLR = ArchLRGet(); +#endif + UNUSED(attr); if (OS_INT_ACTIVE) { @@ -1010,6 +1017,9 @@ osMutexId_t osMutexNew(const osMutexAttr_t *attr) ret = LOS_MuxCreate(&muxId); if (ret == LOS_OK) { +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + OsSetMutexCreateInfo(GET_MUX(muxId), regLR); +#endif return (osMutexId_t)(GET_MUX(muxId)); } else { return (osMutexId_t)NULL; diff --git a/kal/posix/src/pthread_mutex.c b/kal/posix/src/pthread_mutex.c index ea90b76f..312b2c14 100644 --- a/kal/posix/src/pthread_mutex.c +++ b/kal/posix/src/pthread_mutex.c @@ -38,6 +38,9 @@ #include "los_debug.h" #include "los_hook.h" #include "los_sched.h" +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) +#include "los_arch.h" +#endif #define MUTEXATTR_TYPE_MASK 0x0FU #define OS_SYS_NS_PER_MSECOND 1000000 @@ -130,6 +133,10 @@ int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexA UINT32 muxHandle; UINT32 ret; +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + UINTPTR regLR = ArchLRGet(); +#endif + if (mutex == NULL) { return EINVAL; } @@ -148,6 +155,9 @@ int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexA mutex->stAttr = useAttr; mutex->magic = _MUX_MAGIC; mutex->handle = muxHandle; +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + OsSetMutexCreateInfo(GET_MUX(mutex->handle), regLR); +#endif return 0; } @@ -324,6 +334,10 @@ int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *absTi struct timespec curTime = {0}; LosMuxCB *muxPended = NULL; +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + UINTPTR regLR = ArchLRGet(); +#endif + ret = MuxPreCheck(mutex, OS_TCB_FROM_TID(LOS_CurTaskIDGet())); if (ret != 0) { return (INT32)ret; @@ -337,6 +351,9 @@ int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *absTi if (ret != LOS_OK) { return MapError(ret); } +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + OsSetMutexCreateInfo(GET_MUX(mutex->handle), regLR); +#endif } else { muxPended = GET_MUX(mutex->handle); if ((mutex->stAttr.type == PTHREAD_MUTEX_ERRORCHECK) && @@ -364,6 +381,11 @@ int pthread_mutex_lock(pthread_mutex_t *mutex) { UINT32 ret; LosMuxCB *muxPended = NULL; + +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + UINTPTR regLR = ArchLRGet(); +#endif + LosTaskCB *runTask = OS_TCB_FROM_TID(LOS_CurTaskIDGet()); ret = MuxPreCheck(mutex, runTask); @@ -376,6 +398,9 @@ int pthread_mutex_lock(pthread_mutex_t *mutex) if (ret != LOS_OK) { return MapError(ret); } +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + OsSetMutexCreateInfo(GET_MUX(mutex->handle), regLR); +#endif } else { muxPended = GET_MUX(mutex->handle); if ((mutex->stAttr.type == PTHREAD_MUTEX_ERRORCHECK) && @@ -394,6 +419,10 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex) UINT32 ret; LosMuxCB *muxPended = NULL; +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + UINTPTR regLR = ArchLRGet(); +#endif + ret = MuxPreCheck(mutex, OS_TCB_FROM_TID(LOS_CurTaskIDGet())); if (ret != 0) { return (INT32)ret; @@ -404,6 +433,9 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex) if (ret != LOS_OK) { return MapError(ret); } +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + OsSetMutexCreateInfo(GET_MUX(mutex->handle), regLR); +#endif } else { muxPended = GET_MUX(mutex->handle); if ((mutex->stAttr.type != PTHREAD_MUTEX_RECURSIVE) && (muxPended->muxCount != 0)) { diff --git a/kernel/include/los_mux.h b/kernel/include/los_mux.h index b56e7e08..2c680b45 100644 --- a/kernel/include/los_mux.h +++ b/kernel/include/los_mux.h @@ -39,7 +39,6 @@ #include "los_task.h" - #ifdef __cplusplus #if __cplusplus extern "C" { @@ -302,6 +301,9 @@ typedef struct { UINT32 muxID; /**< Handle ID */ LOS_DL_LIST muxList; /**< Mutex linked list */ LosTaskCB *owner; /**< The current thread that is locking a mutex */ +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + UINTPTR createInfo; /**< Return address of the caller */ +#endif UINT16 priority; /**< Priority of the thread that is locking a mutex */ } LosMuxCB; @@ -351,6 +353,13 @@ extern UINT32 OsMuxInit(VOID); */ #define GET_MUX_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosMuxCB, muxList) +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) +STATIC INLINE VOID OsSetMutexCreateInfo(LosMuxCB *mux, UINTPTR val) +{ + mux->createInfo = val; +} +#endif /* LOSCFG_MUTEX_CREATE_TRACE == 1 */ + #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/src/los_mux.c b/kernel/src/los_mux.c index 868b6908..51dbf352 100644 --- a/kernel/src/los_mux.c +++ b/kernel/src/los_mux.c @@ -37,7 +37,6 @@ #include "los_memory.h" #include "los_sched.h" - #if (LOSCFG_BASE_IPC_MUX == 1) LITE_OS_SEC_BSS LosMuxCB* g_allMux = NULL; @@ -71,6 +70,9 @@ LITE_OS_SEC_TEXT_INIT UINT32 OsMuxInit(VOID) muxNode->muxID = index; muxNode->owner = (LosTaskCB *)NULL; muxNode->muxStat = OS_MUX_UNUSED; +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + muxNode->createInfo = 0; +#endif LOS_ListTailInsert(&g_unusedMuxList, &muxNode->muxList); } return LOS_OK; @@ -149,7 +151,9 @@ LITE_OS_SEC_TEXT_INIT UINT32 LOS_MuxDelete(UINT32 muxHandle) LOS_ListAdd(&g_unusedMuxList, &muxDeleted->muxList); muxDeleted->muxStat = OS_MUX_UNUSED; - +#if (LOSCFG_MUTEX_CREATE_TRACE == 1) + muxDeleted->createInfo = 0; +#endif LOS_IntRestore(intSave); OsHookCall(LOS_HOOK_TYPE_MUX_DELETE, muxDeleted); @@ -320,5 +324,5 @@ LITE_OS_SEC_TEXT UINT32 LOS_MuxPost(UINT32 muxHandle) return LOS_OK; } -#endif /* (LOSCFG_BASE_IPC_MUX == 1) */ +#endif /* (LOSCFG_BASE_IPC_MUX == 1) */