diff options
Diffstat (limited to 'include/DebuggerKit')
| -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 |
5 files changed, 156 insertions, 37 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 |
