From f321ca908bb28ff35b275ee32774ae44b4057d0f Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Sun, 14 Sep 2025 01:25:56 +0200 Subject: [PATCH] Refine and export kernel debugger printing --- sdk/xtdk/kdfuncs.h | 23 ++++++++++++++++ sdk/xtdk/xtdebug.h | 2 +- sdk/xtdk/xtkmapi.h | 1 + xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/includes/dbg.hh | 23 ++++++++++++++++ xtoskrnl/includes/kd/dbgio.hh | 10 +++++-- xtoskrnl/includes/xtos.hh | 1 + xtoskrnl/kd/data.cc | 6 ++-- xtoskrnl/kd/dbgio.cc | 52 +++++++++++++++++++++++++++-------- xtoskrnl/kd/exports.cc | 40 +++++++++++++++++++++++++++ xtoskrnl/ke/crash.cc | 2 +- xtoskrnl/xtoskrnl.spec | 1 + 12 files changed, 143 insertions(+), 19 deletions(-) create mode 100644 sdk/xtdk/kdfuncs.h create mode 100644 xtoskrnl/includes/dbg.hh create mode 100644 xtoskrnl/kd/exports.cc diff --git a/sdk/xtdk/kdfuncs.h b/sdk/xtdk/kdfuncs.h new file mode 100644 index 0000000..3274c14 --- /dev/null +++ b/sdk/xtdk/kdfuncs.h @@ -0,0 +1,23 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: sdk/xtdk/kdfuncs.h + * DESCRIPTION: XTOS kernel debugger routine definitions + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTDK_KDFUNCS_H +#define __XTDK_KDFUNCS_H + +#include +#include + + +/* Kernel debugger routines forward references */ +XTCLINK +XTCDECL +VOID +DbgPrint(PCWSTR Format, + ...); + +#endif /* __XTDK_KDFUNCS_H */ diff --git a/sdk/xtdk/xtdebug.h b/sdk/xtdk/xtdebug.h index 3b5eae6..862b658 100644 --- a/sdk/xtdk/xtdebug.h +++ b/sdk/xtdk/xtdebug.h @@ -20,7 +20,7 @@ /* XTOS platform debugging macros */ #ifdef DBG #define DEBUG 1 - #define DebugPrint(Format, ...) if(KdPrint) KdPrint(Format, __VA_ARGS__); + #define DebugPrint(Format, ...) DbgPrint(Format, __VA_ARGS__); #else #define DEBUG 0 #define DebugPrint(Format, ...) ((VOID)NULL) diff --git a/sdk/xtdk/xtkmapi.h b/sdk/xtdk/xtkmapi.h index d6f1d20..5989ba6 100644 --- a/sdk/xtdk/xtkmapi.h +++ b/sdk/xtdk/xtkmapi.h @@ -48,6 +48,7 @@ /* XT routines */ #include #include +#include #include #include diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index f8d613a..b933448 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -30,6 +30,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/hl/ioreg.cc ${XTOSKRNL_SOURCE_DIR}/kd/data.cc ${XTOSKRNL_SOURCE_DIR}/kd/dbgio.cc + ${XTOSKRNL_SOURCE_DIR}/kd/exports.cc ${XTOSKRNL_SOURCE_DIR}/ke/${ARCH}/irq.cc ${XTOSKRNL_SOURCE_DIR}/ke/${ARCH}/krnlinit.cc ${XTOSKRNL_SOURCE_DIR}/ke/${ARCH}/kthread.cc diff --git a/xtoskrnl/includes/dbg.hh b/xtoskrnl/includes/dbg.hh new file mode 100644 index 0000000..1448d65 --- /dev/null +++ b/xtoskrnl/includes/dbg.hh @@ -0,0 +1,23 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/dbg.hh + * DESCRIPTION: Kernel debugging support + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTOSKRNL_DBG_HH +#define __XTOSKRNL_DBG_HH + +#include + + +/* Redefine DebugPrint macro for the kernel to enable early debugging */ +#undef DebugPrint +#ifdef DBG + #define DebugPrint(Format, ...) if(KD::DebugIo::KdPrint) KD::DebugIo::KdPrint(Format, __VA_ARGS__); +#else + #define DebugPrint(Format, ...) ((VOID)NULL) +#endif + +#endif /* __XTOSKRNL_DBG_HH */ diff --git a/xtoskrnl/includes/kd/dbgio.hh b/xtoskrnl/includes/kd/dbgio.hh index 923a15f..86aeaf8 100644 --- a/xtoskrnl/includes/kd/dbgio.hh +++ b/xtoskrnl/includes/kd/dbgio.hh @@ -2,7 +2,7 @@ * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory * FILE: xtoskrnl/includes/kd/dbgio.hh - * DESCRIPTION: + * DESCRIPTION: Kernel Debugger I/O support * DEVELOPERS: Aiken Harris */ @@ -17,6 +17,9 @@ namespace KD { class DebugIo { + public: + STATIC PKD_PRINT_ROUTINE KdPrint; + private: STATIC KD_DEBUG_MODE DebugMode; STATIC PKD_INIT_ROUTINE IoProvidersInitRoutines[KDBG_PROVIDERS_COUNT]; @@ -25,7 +28,10 @@ namespace KD STATIC ULONG SerialPortList[COMPORT_COUNT]; public: - STATIC XTCDECL VOID DbgPrint(PCWSTR Format, ...); + STATIC XTCDECL VOID DbgPrint(PCWSTR Format, + ...); + STATIC XTCDECL VOID DbgPrintEx(PCWSTR Format, + VA_LIST Arguments); STATIC XTAPI XTSTATUS InitializeDebugIoProviders(VOID); STATIC XTAPI VOID SetPrintRoutine(PKD_PRINT_ROUTINE DebugPrintRoutine); diff --git a/xtoskrnl/includes/xtos.hh b/xtoskrnl/includes/xtos.hh index 3a28f57..ca7497f 100644 --- a/xtoskrnl/includes/xtos.hh +++ b/xtoskrnl/includes/xtos.hh @@ -27,6 +27,7 @@ extern "C" { #include +#include #include #include #include diff --git a/xtoskrnl/kd/data.cc b/xtoskrnl/kd/data.cc index 21b1bbb..3c2af70 100644 --- a/xtoskrnl/kd/data.cc +++ b/xtoskrnl/kd/data.cc @@ -9,9 +9,6 @@ #include -/* Pointer to DbgPrint() routine */ -PKD_PRINT_ROUTINE KdPrint = nullptr; - /* Kernel Debugger mode */ KD_DEBUG_MODE KD::DebugIo::DebugMode; @@ -21,6 +18,9 @@ PKD_INIT_ROUTINE KD::DebugIo::IoProvidersInitRoutines[KDBG_PROVIDERS_COUNT] = { InitializeSerialPortProvider }; +/* Pointer to DbgPrint() routine */ +PKD_PRINT_ROUTINE KD::DebugIo::KdPrint = nullptr; + /* List of active I/O providers */ LIST_ENTRY KD::DebugIo::Providers; diff --git a/xtoskrnl/kd/dbgio.cc b/xtoskrnl/kd/dbgio.cc index b4d71f9..febf449 100644 --- a/xtoskrnl/kd/dbgio.cc +++ b/xtoskrnl/kd/dbgio.cc @@ -24,29 +24,57 @@ */ XTCDECL VOID -KD::DebugIo::DbgPrint(PCWSTR Format, ...) +KD::DebugIo::DbgPrint(PCWSTR Format, + ...) { VA_LIST Arguments; - PLIST_ENTRY DispatchTableEntry; - PKD_DISPATCH_TABLE DispatchTable; /* Initialise the va_list */ VA_START(Arguments, Format); - DispatchTableEntry = Providers.Flink; - while(DispatchTableEntry != &Providers) - { - DispatchTable = CONTAIN_RECORD(DispatchTableEntry, KD_DISPATCH_TABLE, ListEntry); - - RTL::WideString::FormatWideString(&DispatchTable->PrintContext, (PWCHAR)Format, Arguments); - - DispatchTableEntry = DispatchTableEntry->Flink; - } + /* Call the actual debug print routine */ + DbgPrintEx(Format, Arguments); /* Clean up the va_list */ VA_END(Arguments); } +/** + * Prints a formatted string using the configured debug output providers (va_list variant). + * + * @param Format + * Supplies the format string. + * + * @param ... + * Supplies the variable argument list. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +KD::DebugIo::DbgPrintEx(PCWSTR Format, + VA_LIST Arguments) +{ + PLIST_ENTRY DispatchTableEntry; + PKD_DISPATCH_TABLE DispatchTable; + + /* Iterate over all registered debug providers */ + DispatchTableEntry = Providers.Flink; + while(DispatchTableEntry != &Providers) + { + /* Get dispatch table */ + DispatchTable = CONTAIN_RECORD(DispatchTableEntry, KD_DISPATCH_TABLE, ListEntry); + + /* Print formatted string using the provider's print context */ + RTL::WideString::FormatWideString(&DispatchTable->PrintContext, (PWCHAR)Format, Arguments); + + /* Move to the next provider */ + DispatchTableEntry = DispatchTableEntry->Flink; + } +} + /** * Detects and enables the kernel's debug ports based on the 'DEBUG' parameter passed to the kernel. * diff --git a/xtoskrnl/kd/exports.cc b/xtoskrnl/kd/exports.cc new file mode 100644 index 0000000..622fade --- /dev/null +++ b/xtoskrnl/kd/exports.cc @@ -0,0 +1,40 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/kd/exports.cc + * DESCRIPTION: C-compatible API wrappers for exported kernel functions + * DEVELOPERS: Aiken Harris + */ + +#include + + +/** + * Prints a formatted string using the configured debug output providers. + * + * @param Format + * Supplies the format string. + * + * @param ... + * Supplies the variable argument list. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCLINK +XTCDECL +VOID +DbgPrint(PCWSTR Format, + ...) +{ + VA_LIST Arguments; + + /* Initialise the va_list */ + VA_START(Arguments, Format); + + KD::DebugIo::DbgPrintEx(Format, Arguments); + + /* Clean up the va_list */ + VA_END(Arguments); +} diff --git a/xtoskrnl/ke/crash.cc b/xtoskrnl/ke/crash.cc index bf2b282..db7c873 100644 --- a/xtoskrnl/ke/crash.cc +++ b/xtoskrnl/ke/crash.cc @@ -76,7 +76,7 @@ KE::Crash::PanicEx(IN ULONG Code, IN ULONG_PTR Parameter3, IN ULONG_PTR Parameter4) { - KdPrint(L"Fatal System Error: 0x%08lx\nKernel Panic!\n\n", Code); + KD::DebugIo::KdPrint(L"Fatal System Error: 0x%08lx\nKernel Panic!\n\n", Code); HaltSystem(); } diff --git a/xtoskrnl/xtoskrnl.spec b/xtoskrnl/xtoskrnl.spec index 725e53e..8ab66b0 100644 --- a/xtoskrnl/xtoskrnl.spec +++ b/xtoskrnl/xtoskrnl.spec @@ -1,4 +1,5 @@ # XTOS kernel exports +@ cdecl DbgPrint(wstr) @ fastcall ExAcquireRundownProtection(ptr) @ fastcall ExCompleteRundownProtection(ptr) @ fastcall ExInitializeRundownProtection(ptr)