From 2c95c04f4e68bc433494ce541ce76da72292def1 Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Fri, 28 Oct 2022 11:42:13 +0800 Subject: [PATCH] sched: catch task memory leak Print all unreleased memory blocks when the task exits Signed-off-by: yinshengkai --- sched/Kconfig | 8 ++++++++ sched/task/task_exithook.c | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/sched/Kconfig b/sched/Kconfig index d151ef86309..66c768b9de0 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -595,6 +595,14 @@ config SCHED_WAITPID compliant) and will enable the waitid() and wait() interfaces as well. +config SCHED_DUMP_LEAK + bool "Enable catch task memory leak" + default n + ---help--- + When this option is enabled, the task's outstanding memory allocations + are printed using syslog. This helps catch any memory allocated by the + task that remains unreleased when the task exits. + config SCHED_USER_IDENTITY bool "Support per-task User Identity" default n diff --git a/sched/task/task_exithook.c b/sched/task/task_exithook.c index 0a1cbcbb415..77d8c55dcae 100644 --- a/sched/task/task_exithook.c +++ b/sched/task/task_exithook.c @@ -33,6 +33,7 @@ #include #include +#include #include "sched/sched.h" #include "group/group.h" @@ -505,6 +506,17 @@ void nxtask_exithook(FAR struct tcb_s *tcb, int status, bool nonblocking) nxsig_cleanup(tcb); /* Deallocate Signal lists */ +#ifdef CONFIG_SCHED_DUMP_LEAK + if ((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL) + { + kmm_memdump(tcb->pid); + } + else + { + umm_memdump(tcb->pid); + } +#endif + #ifdef CONFIG_SMP leave_critical_section(flags); #endif