From d384c7815f92d9bd411033f0bc1d92250568b1c4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 15 Apr 2019 23:58:48 +0200 Subject: [PATCH] Kernel: Make it possible to have kmalloc() dump call stacks. This can be enabled at any time using a sysctl: sysctl kmalloc_stacks=1 The stacks will go to the debugger output only. --- Kernel/FileSystem/ProcFS.cpp | 5 +++++ Kernel/FileSystem/ProcFS.h | 1 + Kernel/kmalloc.cpp | 7 +++++++ Kernel/kmalloc.h | 1 + 4 files changed, 14 insertions(+) diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 1aaf655101..2cf3c5d5db 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -1166,6 +1166,11 @@ ProcFS::ProcFS() m_entries[FI_PID_exe] = { "exe", FI_PID_exe, procfs$pid_exe }; m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, procfs$pid_cwd }; m_entries[FI_PID_fd] = { "fd", FI_PID_fd }; + + m_kmalloc_stack_helper.resource() = g_dump_kmalloc_stacks; + add_sys_bool("kmalloc_stacks", m_kmalloc_stack_helper, [this] { + g_dump_kmalloc_stacks = m_kmalloc_stack_helper.resource(); + }); } ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const diff --git a/Kernel/FileSystem/ProcFS.h b/Kernel/FileSystem/ProcFS.h index 849cff70a7..e86bf38e21 100644 --- a/Kernel/FileSystem/ProcFS.h +++ b/Kernel/FileSystem/ProcFS.h @@ -59,6 +59,7 @@ private: mutable Lock m_inodes_lock; mutable HashMap m_inodes; RetainPtr m_root_inode; + Lockable m_kmalloc_stack_helper; }; struct ProcFSInodeCustomData { diff --git a/Kernel/kmalloc.cpp b/Kernel/kmalloc.cpp index 9c952bf5cc..e7356efdb4 100644 --- a/Kernel/kmalloc.cpp +++ b/Kernel/kmalloc.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #define SANITIZE_KMALLOC @@ -35,6 +36,7 @@ volatile size_t kmalloc_sum_eternal = 0; dword g_kmalloc_call_count; dword g_kfree_call_count; +bool g_dump_kmalloc_stacks; static byte* s_next_eternal_ptr; static byte* s_end_of_eternal_range; @@ -95,6 +97,11 @@ void* kmalloc_impl(size_t size) InterruptDisabler disabler; ++g_kmalloc_call_count; + if (g_dump_kmalloc_stacks && ksyms_ready) { + dbgprintf("kmalloc(%u)\n", size); + dump_backtrace(true); + } + // We need space for the allocation_t structure at the head of the block. size_t real_size = size + sizeof(allocation_t); diff --git a/Kernel/kmalloc.h b/Kernel/kmalloc.h index 001d22da5a..d490e28ce4 100644 --- a/Kernel/kmalloc.h +++ b/Kernel/kmalloc.h @@ -20,6 +20,7 @@ extern volatile size_t kmalloc_sum_eternal; extern volatile size_t kmalloc_sum_page_aligned; extern dword g_kmalloc_call_count; extern dword g_kfree_call_count; +extern bool g_dump_kmalloc_stacks; inline void* operator new(size_t, void* p) { return p; } inline void* operator new[](size_t, void* p) { return p; }