diff options
| author | 😄 Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-25 08:36:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-25 08:36:02 +0100 |
| commit | f83660dd742b3f1706a80b574cd9038474eed9ba (patch) | |
| tree | 7d81b675fe2cb03d2e263eaa4002562188dc058f | |
| parent | 3e6fcda61e87f61fba547bed1eb239981966cb68 (diff) | |
| parent | 9b7dbb3e5c131d84730b88c4fc493665c74613fd (diff) | |
[FEAT] Refactor DebuggerKit design (BREAKING API CHANGES)
| -rw-r--r-- | include/DebuggerKit/Common.inl | 12 | ||||
| -rw-r--r-- | include/DebuggerKit/IDebugger.h (renamed from include/DebuggerKit/DebuggerContract.h) | 18 | ||||
| -rw-r--r-- | include/DebuggerKit/Mach.h (renamed from include/DebuggerKit/MachContract.h) | 34 | ||||
| -rw-r--r-- | include/DebuggerKit/NeSystem.h (renamed from include/DebuggerKit/NeKernelContract.h) | 14 | ||||
| -rw-r--r-- | include/DebuggerKit/POSIX.h | 115 | ||||
| -rw-r--r-- | src/DebuggerKit/src/Mach+CLI.cpp (renamed from src/DebuggerKit/src/MachContractCLI.cpp) | 2 | ||||
| -rw-r--r-- | src/DebuggerKit/src/Mach.cpp (renamed from src/DebuggerKit/src/MachContract.cpp) | 2 | ||||
| -rw-r--r-- | src/DebuggerKit/src/NeSystem+CLI.cpp (renamed from src/DebuggerKit/src/NeKernelContractCLI.cpp) | 3 | ||||
| -rw-r--r-- | src/DebuggerKit/src/NeSystem.cpp (renamed from src/DebuggerKit/src/NeKernelContract.cpp) | 16 | ||||
| -rw-r--r-- | src/DebuggerKit/src/POSIX+CLI.cpp (renamed from src/DebuggerKit/src/POSIXContractCLI.cpp) | 2 |
10 files changed, 168 insertions, 50 deletions
diff --git a/include/DebuggerKit/Common.inl b/include/DebuggerKit/Common.inl index b54c410..a9e5d35 100644 --- a/include/DebuggerKit/Common.inl +++ b/include/DebuggerKit/Common.inl @@ -13,9 +13,17 @@ inline bool kKeepRunning = false; #ifdef DK_NEKERNEL_DEBUGGER -inline DebuggerKit::NeKernel::NeKernelContract kKernelDebugger; +inline DebuggerKit::NeKernel::NeSystemDebugger kKernelDebugger; #else -inline DebuggerKit::POSIX::POSIXMachContract kUserDebugger; +#ifdef DK_MACH_DEBUGGER + +inline DebuggerKit::POSIX::MachDebugger kUserDebugger; + +#else + +inline DebuggerKit::POSIX::POSIXDebugger kUserDebugger; + +#endif #endif static DebuggerKit::ProcessID kPID = 0L; diff --git a/include/DebuggerKit/DebuggerContract.h b/include/DebuggerKit/IDebugger.h index adaeb6e..81fddc5 100644 --- a/include/DebuggerKit/DebuggerContract.h +++ b/include/DebuggerKit/IDebugger.h @@ -10,27 +10,27 @@ #include <DebuggerKit/Detail/Config.h> #include <unordered_map> -#define DK_DEBUGGER_CONTRACT : public ::DebuggerKit::IDebuggerContract +#define DK_DEBUGGER_CONTRACT : public ::DebuggerKit::IDebugger namespace DebuggerKit { -class IDebuggerContract; +class IDebugger; /// =========================================================== /// /// \brief Debugger contract class in C++, as per the design states. /// \author Amlal El Mahrouss /// =========================================================== /// -class IDebuggerContract { +class IDebugger { public: - explicit IDebuggerContract() = default; - virtual ~IDebuggerContract() = default; + explicit IDebugger() = default; + virtual ~IDebugger() = default; public: - IDebuggerContract& operator=(const IDebuggerContract&) = default; - IDebuggerContract(const IDebuggerContract&) = default; + IDebugger& operator=(const IDebugger&) = default; + IDebugger(const IDebugger&) = default; public: - virtual bool Attach(std::string path, std::string argv, ProcessID& pid) noexcept = 0; - virtual bool BreakAt(std::string symbol) noexcept = 0; + virtual bool Attach(const CompilerKit::STLString& path, const CompilerKit::STLString& argv, ProcessID& pid) noexcept = 0; + virtual bool BreakAt(const CompilerKit::STLString& symbol) noexcept = 0; virtual bool Break() noexcept = 0; virtual bool Continue() noexcept = 0; virtual bool Detach() noexcept = 0; diff --git a/include/DebuggerKit/MachContract.h b/include/DebuggerKit/Mach.h index bd62007..05dc37f 100644 --- a/include/DebuggerKit/MachContract.h +++ b/include/DebuggerKit/Mach.h @@ -3,26 +3,24 @@ // file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) // Official repository: https://github.com/ne-foss-org/nectar -#ifndef NECTAR_DEBUGGERKIT_POSIXMACHCONTRACT_H -#define NECTAR_DEBUGGERKIT_POSIXMACHCONTRACT_H +#ifndef NECTAR_DEBUGGERKIT_MACHCONTRACT_H +#define NECTAR_DEBUGGERKIT_MACHCONTRACT_H #ifdef DK_MACH_DEBUGGER -/// @file POSIXMachContract.h +/// @file MachDebugger.h /// @brief POSIX Mach debugger. -#include <DebuggerKit/DebuggerContract.h> +#include <DebuggerKit/IDebugger.h> #include <filesystem> #include <vector> -#ifdef __APPLE__ CK_IMPORT_C kern_return_t mach_vm_write(vm_map_t target_task, mach_vm_address_t address, vm_offset_t data, mach_msg_type_number_t dataCnt); CK_IMPORT_C kern_return_t mach_vm_protect(vm_map_t target_task, mach_vm_address_t address, mach_vm_size_t size, boolean_t set_maximum, vm_prot_t new_protection); -#endif #define PTRACE_ATTACH PT_ATTACHEXC #define PTRACE_DETACH PT_DETACH @@ -32,20 +30,20 @@ CK_IMPORT_C kern_return_t mach_vm_protect(vm_map_t target_task, mach_vm_address_ namespace DebuggerKit::POSIX { /// =========================================================== /// -/// \brief POSIXMachContract engine class in C++ +/// \brief MachDebugger engine class in C++ /// \author Amlal El Mahrouss /// =========================================================== /// -class POSIXMachContract final DK_DEBUGGER_CONTRACT { +class MachDebugger DK_DEBUGGER_CONTRACT { public: - explicit POSIXMachContract() = default; - ~POSIXMachContract() override = default; + explicit MachDebugger() = default; + ~MachDebugger() override = default; public: - POSIXMachContract& operator=(const POSIXMachContract&) = default; - POSIXMachContract(const POSIXMachContract&) = default; + MachDebugger& operator=(const MachDebugger&) = default; + MachDebugger(const MachDebugger&) = default; public: - bool Attach(CompilerKit::STLString path, CompilerKit::STLString argv, + bool Attach(const CompilerKit::STLString& path, const CompilerKit::STLString& argv, ProcessID& pid) noexcept override { pid = fork(); @@ -74,7 +72,7 @@ class POSIXMachContract final DK_DEBUGGER_CONTRACT { return true; } - void SetPath(CompilerKit::STLString path) noexcept { + void SetPath(const CompilerKit::STLString& path) noexcept { if (path.empty()) { return; } @@ -82,7 +80,7 @@ class POSIXMachContract final DK_DEBUGGER_CONTRACT { m_path = path; } - bool BreakAt(CompilerKit::STLString symbol) noexcept override { + bool BreakAt(const CompilerKit::STLString& symbol) noexcept override { if (!m_path.empty() && std::filesystem::exists(m_path) && std::filesystem::is_regular_file(m_path)) { auto handle = dlopen(m_path.c_str(), RTLD_LAZY); @@ -97,10 +95,10 @@ class POSIXMachContract final DK_DEBUGGER_CONTRACT { return false; } -#ifdef __APPLE__ task_read_t task; task_for_pid(mach_task_self(), mPid, &task); +#ifdef __x86_64__ uint32_t brk_inst = 0xD43E0000; mach_vm_protect(task, (mach_vm_address_t) addr, sizeof(uint32_t), false, @@ -115,7 +113,6 @@ class POSIXMachContract final DK_DEBUGGER_CONTRACT { return false; } -#ifdef __APPLE__ bool Break() noexcept override { task_read_t task; task_for_pid(mach_task_self(), mPid, &task); @@ -144,7 +141,6 @@ class POSIXMachContract final DK_DEBUGGER_CONTRACT { return kr = KERN_SUCCESS; } -#endif private: ProcessID mPid{0}; @@ -154,4 +150,4 @@ class POSIXMachContract final DK_DEBUGGER_CONTRACT { #endif // DK_MACH_DEBUGGER -#endif // NECTAR_DEBUGGERKIT_POSIXMACHCONTRACT_H +#endif // NECTAR_DEBUGGERKIT_MACHCONTRACT_H diff --git a/include/DebuggerKit/NeKernelContract.h b/include/DebuggerKit/NeSystem.h index 470da46..299815d 100644 --- a/include/DebuggerKit/NeKernelContract.h +++ b/include/DebuggerKit/NeSystem.h @@ -13,23 +13,23 @@ #ifdef DK_NEKERNEL_DEBUGGER #include <CompilerKit/Detail/Config.h> -#include <DebuggerKit/DebuggerContract.h> +#include <DebuggerKit/IDebugger.h> namespace DebuggerKit::NeKernel { -class NeKernelContract; +class NeSystemDebugger; /// =========================================================== /// /// \brief NeKernel Debugger Contract /// \author Amlal El Mahrouss /// =========================================================== /// -class NeKernelContract final DK_DEBUGGER_CONTRACT { +class NeSystemDebugger final DK_DEBUGGER_CONTRACT { public: - NeKernelContract(); - virtual ~NeKernelContract() override; + NeSystemDebugger(); + virtual ~NeSystemDebugger() override; public: - NeKernelContract& operator=(const NeKernelContract&) = default; - NeKernelContract(const NeKernelContract&) = default; + NeSystemDebugger& operator=(const NeSystemDebugger&) = default; + NeSystemDebugger(const NeSystemDebugger&) = default; public: bool Attach(CompilerKit::STLString path, CompilerKit::STLString arg_v, diff --git a/include/DebuggerKit/POSIX.h b/include/DebuggerKit/POSIX.h new file mode 100644 index 0000000..47f6abb --- /dev/null +++ b/include/DebuggerKit/POSIX.h @@ -0,0 +1,115 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/nectar + +#ifndef NECTAR_DEBUGGERKIT_MACHCONTRACT_H +#define NECTAR_DEBUGGERKIT_MACHCONTRACT_H + +#ifdef DK_MACH_DEBUGGER + +/// @file POSIXDebugger.h +/// @brief POSIX Mach debugger. + +#include <DebuggerKit/IDebugger.h> +#include <sys/ptrace.h> +#include <filesystem> +#include <vector> + +#define PTRACE_ATTACH PT_ATTACH +#define PTRACE_DETACH PT_DETACH +#define PTRACE_POKETEXT PT_WRITE_I +#define PTRACE_CONT PT_CONTINUE +#define PTRACE_PEEKTEXT PT_READ_I + +namespace DebuggerKit::POSIX { +/// =========================================================== /// +/// \brief POSIXDebugger engine class in C++ +/// \author Amlal El Mahrouss +/// =========================================================== /// +class POSIXDebugger DK_DEBUGGER_CONTRACT { + public: + explicit POSIXDebugger() = default; + ~POSIXDebugger() override = default; + + public: + POSIXDebugger& operator=(const POSIXDebugger&) = default; + POSIXDebugger(const POSIXDebugger&) = default; + + public: + bool Attach(const CompilerKit::STLString& path, const CompilerKit::STLString& argv, + ProcessID& pid) noexcept override { + pid = fork(); + + if (pid == 0) { + if (argv.empty()) { + ptrace(PT_TRACE_ME, 0, nullptr, 0); + kill(getpid(), SIGSTOP); + } + + std::vector<char*> argv_arr; + + argv_arr.push_back(const_cast<char*>(path.c_str())); + argv_arr.push_back(const_cast<char*>(argv.c_str())); + argv_arr.push_back(nullptr); + + execv(path.c_str(), argv_arr.data()); + + _exit(1); + } + + m_path = path; + mPid = pid; + + pid = this->mPid; + + return true; + } + + void SetPath(const CompilerKit::STLString& path) noexcept { + if (path.empty()) { + return; + } + + m_path = path; + } + + bool BreakAt(const CompilerKit::STLString& symbol) noexcept override { + if (!m_path.empty() && std::filesystem::exists(m_path) && + std::filesystem::is_regular_file(m_path)) { + auto handle = dlopen(m_path.c_str(), RTLD_LAZY); + + if (handle == nullptr) { + return false; + } + + auto addr = dlsym(handle, symbol.c_str()); + + if (addr == nullptr) { + return false; + } + + return true; + } + + return false; + } + + bool Break() noexcept override { return false; } + + bool Continue() noexcept override { return false; } + + bool Detach() noexcept override { + this->Continue(); + return false; + } + + private: + ProcessID mPid{0}; + CompilerKit::STLString m_path; +}; +} // namespace DebuggerKit::POSIX + +#endif // DK_MACH_DEBUGGER + +#endif // NECTAR_DEBUGGERKIT_MACHCONTRACT_H diff --git a/src/DebuggerKit/src/MachContractCLI.cpp b/src/DebuggerKit/src/Mach+CLI.cpp index 9bdb5b1..69000c9 100644 --- a/src/DebuggerKit/src/MachContractCLI.cpp +++ b/src/DebuggerKit/src/Mach+CLI.cpp @@ -6,7 +6,7 @@ #ifdef DK_MACH_DEBUGGER -#include <DebuggerKit/MachContract.h> +#include <DebuggerKit/Mach.h> #include <ThirdParty/Dialogs/Dialogs.h> #ifdef DK_MACH_DEBUGGER diff --git a/src/DebuggerKit/src/MachContract.cpp b/src/DebuggerKit/src/Mach.cpp index 518f61a..31ae544 100644 --- a/src/DebuggerKit/src/MachContract.cpp +++ b/src/DebuggerKit/src/Mach.cpp @@ -6,7 +6,7 @@ #ifdef DK_MACH_DEBUGGER -#include <DebuggerKit/MachContract.h> +#include <DebuggerKit/Mach.h> #include <ThirdParty/Dialogs/Dialogs.h> /// @brief a terrible way to import globals. diff --git a/src/DebuggerKit/src/NeKernelContractCLI.cpp b/src/DebuggerKit/src/NeSystem+CLI.cpp index 13144eb..39271b1 100644 --- a/src/DebuggerKit/src/NeKernelContractCLI.cpp +++ b/src/DebuggerKit/src/NeSystem+CLI.cpp @@ -6,9 +6,8 @@ #ifdef DK_NEKERNEL_DEBUGGER -#include <DebuggerKit/NeKernelContract.h> +#include <DebuggerKit/NeSystem.h> #include <ThirdParty/Dialogs/Dialogs.h> -#include <string> #include <DebuggerKit/Common.inl> diff --git a/src/DebuggerKit/src/NeKernelContract.cpp b/src/DebuggerKit/src/NeSystem.cpp index 5e7fb5e..ea3beb4 100644 --- a/src/DebuggerKit/src/NeKernelContract.cpp +++ b/src/DebuggerKit/src/NeSystem.cpp @@ -9,16 +9,16 @@ /// @author Amlal El Mahrouss /// @brief Kernel Debugger Protocol -#include <DebuggerKit/NeKernelContract.h> +#include <DebuggerKit/NeSystem.h> #include <ThirdParty/Dialogs/Dialogs.h> using namespace DebuggerKit::Detail; using namespace DebuggerKit::NeKernel; -NeKernelContract::NeKernelContract() = default; -NeKernelContract::~NeKernelContract() = default; +NeSystemDebugger::NeSystemDebugger() = default; +NeSystemDebugger::~NeSystemDebugger() = default; -bool NeKernelContract::Attach(CompilerKit::STLString path, CompilerKit::STLString argv, +bool NeSystemDebugger::Attach(CompilerKit::STLString path, CompilerKit::STLString argv, ProcessID& pid) noexcept { if (path.empty() || argv.empty()) return NO; @@ -44,7 +44,7 @@ bool NeKernelContract::Attach(CompilerKit::STLString path, CompilerKit::STLStrin return ret; } -bool NeKernelContract::BreakAt(CompilerKit::STLString symbol) noexcept { +bool NeSystemDebugger::BreakAt(CompilerKit::STLString symbol) noexcept { CompilerKit::STLString pkt = Detail::kDebugMagic; pkt += ";SYM=\""; pkt += symbol; @@ -56,7 +56,7 @@ bool NeKernelContract::BreakAt(CompilerKit::STLString symbol) noexcept { return ret; } -bool NeKernelContract::Break() noexcept { +bool NeSystemDebugger::Break() noexcept { CompilerKit::STLString pkt = Detail::kDebugMagic; pkt += ";BRK=1;\r"; @@ -64,7 +64,7 @@ bool NeKernelContract::Break() noexcept { return ret; } -bool NeKernelContract::Continue() noexcept { +bool NeSystemDebugger::Continue() noexcept { CompilerKit::STLString pkt = Detail::kDebugMagic; pkt += ";CONT=1;\r"; @@ -73,7 +73,7 @@ bool NeKernelContract::Continue() noexcept { return NO; } -bool NeKernelContract::Detach() noexcept { +bool NeSystemDebugger::Detach() noexcept { CompilerKit::STLString pkt = Detail::kDebugMagic; pkt += ";DTCH=1;\r"; diff --git a/src/DebuggerKit/src/POSIXContractCLI.cpp b/src/DebuggerKit/src/POSIX+CLI.cpp index 58aa4a1..30dbe26 100644 --- a/src/DebuggerKit/src/POSIXContractCLI.cpp +++ b/src/DebuggerKit/src/POSIX+CLI.cpp @@ -6,7 +6,7 @@ #ifdef DK_POSIX_DEBUGGER -#include <DebuggerKit/POSIXContract.h> +#include <DebuggerKit/POSIX.h> #include <ThirdParty/Dialogs/Dialogs.h> #include <DebuggerKit/Common.inl> |
