From 3a1c22bb9331f4f54794cdb9b7168c8db7061173 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 4 May 2023 23:32:48 +0200 Subject: [PATCH] kernel: Add keyboard combinations for debug information dumps Ctrl+Alt+E: Dump threads Ctrl+Alt+M: Dump memory usage Ctrl+Alt+H: Dump heap state --- kernel/src/fs/devices/ConsoleDevice.cpp | 24 ++++++++++++++++++++++++ kernel/src/thread/Scheduler.cpp | 22 ++++++++++++++++++++++ kernel/src/thread/Scheduler.h | 2 ++ 3 files changed, 48 insertions(+) diff --git a/kernel/src/fs/devices/ConsoleDevice.cpp b/kernel/src/fs/devices/ConsoleDevice.cpp index dda6d021..2449e1c0 100644 --- a/kernel/src/fs/devices/ConsoleDevice.cpp +++ b/kernel/src/fs/devices/ConsoleDevice.cpp @@ -1,10 +1,13 @@ #include "fs/devices/ConsoleDevice.h" +#include "Log.h" #include "arch/Keyboard.h" #include "memory/MemoryManager.h" +#include "thread/Scheduler.h" #include "video/TextConsole.h" #include #include #include +#include #include static Buffer g_console_input; @@ -63,6 +66,27 @@ void ConsoleDevice::did_press_key(char key) return; } + if (key == 'e' && (Keyboard::modifiers() & (Keyboard::LeftAlt | Keyboard::LeftControl))) + { + Scheduler::dump_state(); + return; + } + + if (key == 'm' && (Keyboard::modifiers() & (Keyboard::LeftAlt | Keyboard::LeftControl))) + { + kinfoln("Total memory: %s", to_dynamic_unit(MemoryManager::total()).release_value().chars()); + kinfoln("Free memory: %s", to_dynamic_unit(MemoryManager::free()).release_value().chars()); + kinfoln("Used memory: %s", to_dynamic_unit(MemoryManager::used()).release_value().chars()); + kinfoln("Reserved memory: %s", to_dynamic_unit(MemoryManager::reserved()).release_value().chars()); + return; + } + + if (key == 'h' && (Keyboard::modifiers() & (Keyboard::LeftAlt | Keyboard::LeftControl))) + { + dump_heap_usage(); + return; + } + g_temp_input.try_append((u8)key).value(); if (key == '\n') diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 8aa5a71a..72800046 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -327,6 +327,28 @@ namespace Scheduler return result; } + + void dump_state() + { + CPU::disable_interrupts(); + + kdbgln("--- BEGIN SCHEDULER DUMP ---"); + kdbgln("current at %p, id = %zu", g_current, g_current->id); + + for (const auto* thread : g_threads) + { + kdbgln("%p %c [%-20s] %4zu, parent = (%-18p,%zu), state = %d, ticks: (t:%04zu,k:%04zu,u:%04zu), status = " + "%d, cwd = %s", + thread, thread->is_kernel ? 'k' : 'u', thread->name.chars(), thread->id, thread->parent, + thread->parent ? thread->parent->id : 0, (int)thread->state, thread->ticks, thread->ticks_in_kernel, + thread->ticks_in_user, thread->status, + thread->current_directory_path.is_empty() ? "/" : thread->current_directory_path.chars()); + } + + kdbgln("--- END SCHEDULER DUMP ---"); + + CPU::enable_interrupts(); + } } void kernel_sleep(u64 ms) diff --git a/kernel/src/thread/Scheduler.h b/kernel/src/thread/Scheduler.h index 5dbc19cc..f34a731d 100644 --- a/kernel/src/thread/Scheduler.h +++ b/kernel/src/thread/Scheduler.h @@ -45,6 +45,8 @@ namespace Scheduler } } + void dump_state(); + bool has_children(Thread* thread); Option find_exited_child(Thread* thread);