From 94ceccd5acda2fd035eb55235126b944b0915576 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 May 2025 14:27:43 +0200 Subject: dev(kdbg): Working on NeKernel Debugger. how: - Load vmkrnl.efi. - Keep track of IP, Stack, and debugging information related to the kernel. - Make use of breakpoints and provide lldb grade experience. Signed-off-by: Amlal El Mahrouss --- dev/LibDebugger/src/NeKernelContract.cc | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 dev/LibDebugger/src/NeKernelContract.cc (limited to 'dev/LibDebugger/src/NeKernelContract.cc') diff --git a/dev/LibDebugger/src/NeKernelContract.cc b/dev/LibDebugger/src/NeKernelContract.cc new file mode 100644 index 0000000..e653d12 --- /dev/null +++ b/dev/LibDebugger/src/NeKernelContract.cc @@ -0,0 +1,34 @@ +/*** + LibDebugger + (C) 2025 Amlal El Mahrouss + File: NeKernelContract.cc + Purpose: NeKernel Debugger +*/ + +#ifdef LD_NEKERNEL_DEBUGGER + +#include +#include +#include + +#include +#include +#include + +using namespace LibDebugger::NeKernel; + +NeKernelContract::NeKernelContract() = default; + +NeKernelContract::~NeKernelContract() = default; + +bool NeKernelContract::Attach(std::string path, std::string argv, ProcessID& pid) noexcept { return false; } + +bool NeKernelContract::Breakpoint(std::string symbol) noexcept { return false; } + +bool NeKernelContract::Break() noexcept { return false; } + +bool NeKernelContract::Continue() noexcept { return false; } + +bool NeKernelContract::Detach() noexcept { return false; } + +#endif // LD_NEKERNEL_DEBUGGER \ No newline at end of file -- cgit v1.2.3 From 9f031e69aace747feb5bac78eccb9a1d5df81f74 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 May 2025 17:49:19 +0200 Subject: feat(cc): Rename Parser.h to CompilerFrontend.h, refactor codebase accordingly. why: - To make its intent clearer, and avoid future confusions. also: - Ran ./format.sh to the codebase. Signed-off-by: Amlal El Mahrouss --- dev/LibCompiler/CompilerFrontend.h | 149 +++++++++++++++++++++ dev/LibCompiler/Detail/AsmUtils.h | 2 +- dev/LibCompiler/Detail/ClUtils.h | 2 +- dev/LibCompiler/Parser.h | 149 --------------------- dev/LibCompiler/src/Assembler32x0.cc | 2 +- dev/LibCompiler/src/Assembler64x0.cc | 2 +- dev/LibCompiler/src/AssemblerAMD64.cc | 2 +- dev/LibCompiler/src/AssemblerARM64.cc | 2 +- dev/LibCompiler/src/AssemblerPowerPC.cc | 2 +- dev/LibCompiler/src/CCompiler64x0.cc | 2 +- dev/LibCompiler/src/CCompilerARM64.cc | 2 +- dev/LibCompiler/src/CCompilerPower64.cc | 2 +- dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc | 2 +- .../src/CPlusPlusCompilerPreProcessor.cc | 2 +- dev/LibDebugger/CommonCLI.inl | 8 +- dev/LibDebugger/NeKernelContract.h | 2 +- dev/LibDebugger/src/NeKernelContract.cc | 22 ++- dev/LibDebugger/src/NeKernelContractCLI.cc | 21 ++- tests/example.cc | 6 +- 19 files changed, 195 insertions(+), 186 deletions(-) create mode 100644 dev/LibCompiler/CompilerFrontend.h delete mode 100644 dev/LibCompiler/Parser.h (limited to 'dev/LibDebugger/src/NeKernelContract.cc') diff --git a/dev/LibCompiler/CompilerFrontend.h b/dev/LibCompiler/CompilerFrontend.h new file mode 100644 index 0000000..858473b --- /dev/null +++ b/dev/LibCompiler/CompilerFrontend.h @@ -0,0 +1,149 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include + +namespace LibCompiler { +inline auto kInvalidFrontend = "NA"; + +/// @brief Compiler backend, implements a frontend, such as C, C++... +/// See Toolchain, for some examples. +class ICompilerFrontend { + public: + explicit ICompilerFrontend() = default; + virtual ~ICompilerFrontend() = default; + + LIBCOMPILER_COPY_DEFAULT(ICompilerFrontend); + + // NOTE: cast this to your user defined ast. + typedef void* AstType; + + //! @brief Compile a syntax tree ouf of the text. + //! Also takes the source file name for metadata. + + virtual bool Compile(std::string text, std::string file) = 0; + + //! @brief What language are we dealing with? + virtual const char* Language() { return kInvalidFrontend; } + + virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } +}; + +struct SyntaxLeafList; +struct SyntaxLeafList; +struct CompilerKeyword; + +/// we want to do that because to separate keywords. +enum KeywordKind { + kKeywordKindNamespace, + kKeywordKindFunctionStart, + kKeywordKindFunctionEnd, + kKeywordKindVariable, + kKeywordKindVariablePtr, + kKeywordKindType, + kKeywordKindTypePtr, + kKeywordKindExpressionBegin, + kKeywordKindExpressionEnd, + kKeywordKindArgSeparator, + kKeywordKindBodyStart, + kKeywordKindBodyEnd, + kKeywordKindClass, + kKeywordKindPtrAccess, + kKeywordKindAccess, + kKeywordKindIf, + kKeywordKindElse, + kKeywordKindElseIf, + kKeywordKindVariableAssign, + kKeywordKindVariableDec, + kKeywordKindVariableInc, + kKeywordKindConstant, + kKeywordKindTypedef, + kKeywordKindEndInstr, + kKeywordKindSpecifier, + kKeywordKindInvalid, + kKeywordKindReturn, + kKeywordKindCommentInline, + kKeywordKindCommentMultiLineStart, + kKeywordKindCommentMultiLineEnd, + kKeywordKindEq, + kKeywordKindNotEq, + kKeywordKindGreaterEq, + kKeywordKindLessEq, + kKeywordKindPtr, +}; + +/// \brief Compiler keyword information struct. +struct CompilerKeyword { + std::string keyword_name; + KeywordKind keyword_kind = kKeywordKindInvalid; +}; +struct SyntaxLeafList final { + struct SyntaxLeaf final { + Int32 fUserType; +#ifdef LC_USE_STRUCTS + CompilerKeyword fUserData; +#else + std::string fUserData; +#endif + + SyntaxLeaf() = default; + + std::string fUserValue; + struct SyntaxLeaf* fNext; + }; + + std::vector fLeafList; + SizeType fNumLeafs; + + size_t SizeOf() { return fNumLeafs; } + std::vector& Get() { return fLeafList; } + SyntaxLeaf& At(size_t index) { return fLeafList[index]; } +}; + +/// find the perfect matching word in a haystack. +/// \param haystack base string +/// \param needle the string we search for. +/// \return if we found it or not. +inline bool find_word(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + // declare lambda + auto not_part_of_word = [&](int index) { + if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; + + if (index <= 0 || index >= haystack.size()) return true; + + return false; + }; + + return not_part_of_word(index - 1) && not_part_of_word(index + needle.size()); +} + +/// find a word within strict conditions and returns a range of it. +/// \param haystack +/// \param needle +/// \return position of needle. +inline std::size_t find_word_range(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + if (!isalnum((haystack[index + needle.size() + 1])) && + !isdigit(haystack[index + needle.size() + 1]) && + !isalnum((haystack[index - needle.size() - 1])) && + !isdigit(haystack[index - needle.size() - 1])) { + return index; + } + + return std::string::npos; +} +} // namespace LibCompiler diff --git a/dev/LibCompiler/Detail/AsmUtils.h b/dev/LibCompiler/Detail/AsmUtils.h index 559df5f..77805c1 100644 --- a/dev/LibCompiler/Detail/AsmUtils.h +++ b/dev/LibCompiler/Detail/AsmUtils.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include diff --git a/dev/LibCompiler/Detail/ClUtils.h b/dev/LibCompiler/Detail/ClUtils.h index f47101a..a809cdf 100644 --- a/dev/LibCompiler/Detail/ClUtils.h +++ b/dev/LibCompiler/Detail/ClUtils.h @@ -7,8 +7,8 @@ #pragma once #include +#include #include -#include #include #define kZero64Section ".zero64" diff --git a/dev/LibCompiler/Parser.h b/dev/LibCompiler/Parser.h deleted file mode 100644 index 6baff7e..0000000 --- a/dev/LibCompiler/Parser.h +++ /dev/null @@ -1,149 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved - -------------------------------------------- */ - -#pragma once - -#include - -namespace LibCompiler { -inline auto kInvalidFrontend = "NA"; - -/// @brief Compiler backend, implements a frontend, such as C, C++... -/// See Toolchain, for some examples. -class ICompilerFrontend { - public: - explicit ICompilerFrontend() = default; - virtual ~ICompilerFrontend() = default; - - LIBCOMPILER_COPY_DEFAULT(ICompilerFrontend); - - // NOTE: cast this to your user defined ast. - typedef void* AstType; - - //! @brief Compile a syntax tree ouf of the text. - //! Also takes the source file name for metadata. - - virtual bool Compile(std::string text, std::string file) = 0; - - //! @brief What language are we dealing with? - virtual const char* Language() { return kInvalidFrontend; } - - virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } -}; - -struct SyntaxLeafList; -struct SyntaxLeafList; -struct CompilerKeyword; - -/// we want to do that because to separate keywords. -enum KeywordKind { - kKeywordKindNamespace, - kKeywordKindFunctionStart, - kKeywordKindFunctionEnd, - kKeywordKindVariable, - kKeywordKindVariablePtr, - kKeywordKindType, - kKeywordKindTypePtr, - kKeywordKindExpressionBegin, - kKeywordKindExpressionEnd, - kKeywordKindArgSeparator, - kKeywordKindBodyStart, - kKeywordKindBodyEnd, - kKeywordKindClass, - kKeywordKindPtrAccess, - kKeywordKindAccess, - kKeywordKindIf, - kKeywordKindElse, - kKeywordKindElseIf, - kKeywordKindVariableAssign, - kKeywordKindVariableDec, - kKeywordKindVariableInc, - kKeywordKindConstant, - kKeywordKindTypedef, - kKeywordKindEndInstr, - kKeywordKindSpecifier, - kKeywordKindInvalid, - kKeywordKindReturn, - kKeywordKindCommentInline, - kKeywordKindCommentMultiLineStart, - kKeywordKindCommentMultiLineEnd, - kKeywordKindEq, - kKeywordKindNotEq, - kKeywordKindGreaterEq, - kKeywordKindLessEq, - kKeywordKindPtr, -}; - -/// \brief Compiler keyword information struct. -struct CompilerKeyword { - std::string keyword_name; - KeywordKind keyword_kind = kKeywordKindInvalid; -}; -struct SyntaxLeafList final { - struct SyntaxLeaf final { - Int32 fUserType; -#ifdef LC_USE_STRUCTS - CompilerKeyword fUserData; -#else - std::string fUserData; -#endif - - SyntaxLeaf() = default; - - std::string fUserValue; - struct SyntaxLeaf* fNext; - }; - - std::vector fLeafList; - SizeType fNumLeafs; - - size_t SizeOf() { return fNumLeafs; } - std::vector& Get() { return fLeafList; } - SyntaxLeaf& At(size_t index) { return fLeafList[index]; } -}; - -/// find the perfect matching word in a haystack. -/// \param haystack base string -/// \param needle the string we search for. -/// \return if we found it or not. -inline bool find_word(std::string haystack, std::string needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == std::string::npos) return false; - - // declare lambda - auto not_part_of_word = [&](int index) { - if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; - - if (index <= 0 || index >= haystack.size()) return true; - - return false; - }; - - return not_part_of_word(index - 1) && not_part_of_word(index + needle.size()); -} - -/// find a word within strict conditions and returns a range of it. -/// \param haystack -/// \param needle -/// \return position of needle. -inline std::size_t find_word_range(std::string haystack, std::string needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == std::string::npos) return false; - - if (!isalnum((haystack[index + needle.size() + 1])) && - !isdigit(haystack[index + needle.size() + 1]) && - !isalnum((haystack[index - needle.size() - 1])) && - !isdigit(haystack[index - needle.size() - 1])) { - return index; - } - - return false; -} -} // namespace LibCompiler diff --git a/dev/LibCompiler/src/Assembler32x0.cc b/dev/LibCompiler/src/Assembler32x0.cc index ac24946..44850f6 100644 --- a/dev/LibCompiler/src/Assembler32x0.cc +++ b/dev/LibCompiler/src/Assembler32x0.cc @@ -21,8 +21,8 @@ #include #include +#include #include -#include ///////////////////// diff --git a/dev/LibCompiler/src/Assembler64x0.cc b/dev/LibCompiler/src/Assembler64x0.cc index 8483e9e..a7c3efc 100644 --- a/dev/LibCompiler/src/Assembler64x0.cc +++ b/dev/LibCompiler/src/Assembler64x0.cc @@ -21,9 +21,9 @@ #include #include +#include #include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/AssemblerAMD64.cc b/dev/LibCompiler/src/AssemblerAMD64.cc index 5e9a7cd..3fdb197 100644 --- a/dev/LibCompiler/src/AssemblerAMD64.cc +++ b/dev/LibCompiler/src/AssemblerAMD64.cc @@ -28,8 +28,8 @@ #include #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/AssemblerARM64.cc b/dev/LibCompiler/src/AssemblerARM64.cc index 8686edb..c8b66f7 100644 --- a/dev/LibCompiler/src/AssemblerARM64.cc +++ b/dev/LibCompiler/src/AssemblerARM64.cc @@ -19,10 +19,10 @@ #include #include +#include #include #include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/AssemblerPowerPC.cc b/dev/LibCompiler/src/AssemblerPowerPC.cc index f2c3be3..a438aed 100644 --- a/dev/LibCompiler/src/AssemblerPowerPC.cc +++ b/dev/LibCompiler/src/AssemblerPowerPC.cc @@ -19,10 +19,10 @@ #include #include +#include #include #include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CCompiler64x0.cc b/dev/LibCompiler/src/CCompiler64x0.cc index 79d32ce..e43e16b 100644 --- a/dev/LibCompiler/src/CCompiler64x0.cc +++ b/dev/LibCompiler/src/CCompiler64x0.cc @@ -11,8 +11,8 @@ /// TODO: none #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CCompilerARM64.cc b/dev/LibCompiler/src/CCompilerARM64.cc index a5ddf43..b60ef4f 100644 --- a/dev/LibCompiler/src/CCompilerARM64.cc +++ b/dev/LibCompiler/src/CCompilerARM64.cc @@ -11,8 +11,8 @@ /// TODO: none #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CCompilerPower64.cc b/dev/LibCompiler/src/CCompilerPower64.cc index f2eba43..30a1ab3 100644 --- a/dev/LibCompiler/src/CCompilerPower64.cc +++ b/dev/LibCompiler/src/CCompilerPower64.cc @@ -8,8 +8,8 @@ */ #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc index 051529d..92bf9ad 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc @@ -17,8 +17,8 @@ // extern_segment, @autodelete { ... }, fn foo() -> auto { ... } #include +#include #include -#include #include #include diff --git a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc b/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc index 5d035da..9981f37 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc @@ -9,8 +9,8 @@ /// BUGS: 0 +#include #include -#include #include #include #include diff --git a/dev/LibDebugger/CommonCLI.inl b/dev/LibDebugger/CommonCLI.inl index 0660bb3..c917295 100644 --- a/dev/LibDebugger/CommonCLI.inl +++ b/dev/LibDebugger/CommonCLI.inl @@ -1,5 +1,5 @@ -static BOOL kKeepRunning = false; +static BOOL kKeepRunning = false; #ifdef LD_NEKERNEL_DEBUGGER static LibDebugger::NeKernel::NeKernelContract kKernelDebugger; @@ -7,9 +7,9 @@ static LibDebugger::NeKernel::NeKernelContract kKernelDebugger; static LibDebugger::POSIX::POSIXMachContract kDebugger; #endif -static LibDebugger::ProcessID kPID = 0L; -static LibDebugger::CAddress kActiveAddress = nullptr; -static std::string kPath = ""; +static LibDebugger::ProcessID kPID = 0L; +static LibDebugger::CAddress kActiveAddress = nullptr; +static std::string kPath = ""; #define kBlank "\e[0;30m" #define kRed "\e[0;31m" diff --git a/dev/LibDebugger/NeKernelContract.h b/dev/LibDebugger/NeKernelContract.h index b63dcb9..78140e7 100644 --- a/dev/LibDebugger/NeKernelContract.h +++ b/dev/LibDebugger/NeKernelContract.h @@ -68,6 +68,6 @@ class NeKernelContract : public DebuggerContract { }; } // namespace LibDebugger::NeKernel -#endif // ifdef LD_NEKERNEL_DEBUGGER +#endif // ifdef LD_NEKERNEL_DEBUGGER #endif // LD_NEKERNEL_CONTRACT_H \ No newline at end of file diff --git a/dev/LibDebugger/src/NeKernelContract.cc b/dev/LibDebugger/src/NeKernelContract.cc index e653d12..33c0c48 100644 --- a/dev/LibDebugger/src/NeKernelContract.cc +++ b/dev/LibDebugger/src/NeKernelContract.cc @@ -21,14 +21,24 @@ NeKernelContract::NeKernelContract() = default; NeKernelContract::~NeKernelContract() = default; -bool NeKernelContract::Attach(std::string path, std::string argv, ProcessID& pid) noexcept { return false; } +bool NeKernelContract::Attach(std::string path, std::string argv, ProcessID& pid) noexcept { + return false; +} -bool NeKernelContract::Breakpoint(std::string symbol) noexcept { return false; } +bool NeKernelContract::Breakpoint(std::string symbol) noexcept { + return false; +} -bool NeKernelContract::Break() noexcept { return false; } +bool NeKernelContract::Break() noexcept { + return false; +} -bool NeKernelContract::Continue() noexcept { return false; } +bool NeKernelContract::Continue() noexcept { + return false; +} -bool NeKernelContract::Detach() noexcept { return false; } +bool NeKernelContract::Detach() noexcept { + return false; +} -#endif // LD_NEKERNEL_DEBUGGER \ No newline at end of file +#endif // LD_NEKERNEL_DEBUGGER \ No newline at end of file diff --git a/dev/LibDebugger/src/NeKernelContractCLI.cc b/dev/LibDebugger/src/NeKernelContractCLI.cc index 1f05ff2..dddefda 100644 --- a/dev/LibDebugger/src/NeKernelContractCLI.cc +++ b/dev/LibDebugger/src/NeKernelContractCLI.cc @@ -20,16 +20,15 @@ using namespace LibDebugger::NeKernel; LIBCOMPILER_MODULE(DebuggerNeKernel) { - pfd::notify("Debugger Event", - "Kernel Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); - - if (argc >= 3 && std::string(argv[1]) == "-p" && argv[2] != nullptr) { - kPath = argv[2]; - kStdOut << "[+] Kernel image set to: " << kPath << "\n"; - } - - - return EXIT_SUCCESS; + pfd::notify("Debugger Event", + "Kernel Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); + + if (argc >= 3 && std::string(argv[1]) == "-p" && argv[2] != nullptr) { + kPath = argv[2]; + kStdOut << "[+] Kernel image set to: " << kPath << "\n"; + } + + return EXIT_SUCCESS; } -#endif // LD_NEKERNEL_DEBUGGER \ No newline at end of file +#endif // LD_NEKERNEL_DEBUGGER \ No newline at end of file diff --git a/tests/example.cc b/tests/example.cc index 2e2a364..9ee2a30 100644 --- a/tests/example.cc +++ b/tests/example.cc @@ -1,5 +1,5 @@ -#define AppMain __ImageStart -#warning TestCase #1 +#define main __ImageStart +#warning test macro warning #1 int bar() { int yyy = 100; @@ -11,6 +11,6 @@ int foo() { return bar(); } -int AppMain() { +int main() { return foo(); } -- cgit v1.2.3