From 733edfd7f7ea2dee27a177127730d04821e313db Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 26 Mar 2025 10:12:50 +0100 Subject: feat(dbg): made it work on OS X, using mach API. Signed-off-by: Amlal El Mahrouss --- dev/LibDebugger/POSIXContract.h | 106 ----------------------- dev/LibDebugger/POSIXMachContract.h | 144 +++++++++++++++++++++++++++++++ dev/LibDebugger/src/POSIXContract.cc | 128 --------------------------- dev/LibDebugger/src/POSIXMachContract.cc | 129 +++++++++++++++++++++++++++ 4 files changed, 273 insertions(+), 234 deletions(-) delete mode 100644 dev/LibDebugger/POSIXContract.h create mode 100644 dev/LibDebugger/POSIXMachContract.h delete mode 100644 dev/LibDebugger/src/POSIXContract.cc create mode 100644 dev/LibDebugger/src/POSIXMachContract.cc (limited to 'dev/LibDebugger') diff --git a/dev/LibDebugger/POSIXContract.h b/dev/LibDebugger/POSIXContract.h deleted file mode 100644 index 5a8ae48..0000000 --- a/dev/LibDebugger/POSIXContract.h +++ /dev/null @@ -1,106 +0,0 @@ -/*** - (C) 2025 Amlal El Mahrouss - */ - -#pragma once - -#ifdef _WIN32 -#error Windows doesn't have a POSIX subsystem, please combine with windows instead. -#endif - -#include - -#include -#include -#include -#include -#include -#include - -#ifdef __APPLE__ -#define PTRACE_ATTACH PT_ATTACHEXC -#define PTRACE_DETACH PT_DETACH -#define PTRACE_POKETEXT PT_WRITE_I -#define PTRACE_CONT PT_CONTINUE -#define PTRACE_PEEKTEXT PT_READ_I -#endif - -namespace LibDebugger::POSIX -{ - /// \brief POSIXDebuggerContract engine interface class in C++ - /// \author Amlal El Mahrouss - class POSIXDebuggerContract final : public DebuggerContract - { - public: - explicit POSIXDebuggerContract() = default; - ~POSIXDebuggerContract() override = default; - - public: - POSIXDebuggerContract& operator=(const POSIXDebuggerContract&) = default; - POSIXDebuggerContract(const POSIXDebuggerContract&) = default; - - public: - bool Attach(ProcessID pid) noexcept override - { - if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1) - { - return false; - } - - this->m_pid = pid; - - waitpid(m_pid, nullptr, 0); - - return true; - } - - bool Break(CAddress addr) noexcept override - { - uintptr_t original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0); - - if (original_data == -1) - { - return false; - } - - constexpr uint8_t kInt3x86 = 0xCC; - - uintptr_t data_with_int3 = (original_data & ~0xFF) | kInt3x86; // Insert INT3 (0xCC) - - if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) - { - return false; - } - - m_breakpoints[reinterpret_cast(addr)] = original_data; // Store original data - - return true; - } - - bool Continue() noexcept override - { - if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1) - { - return false; - } - - int status; - waitpid(m_pid, &status, 0); - - return WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP; - } - - bool Detach() noexcept override - { - if (ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1) - { - return false; - } - - return true; - } - - private: - pid_t m_pid; - }; -} // namespace LibDebugger::POSIX diff --git a/dev/LibDebugger/POSIXMachContract.h b/dev/LibDebugger/POSIXMachContract.h new file mode 100644 index 0000000..6ff083f --- /dev/null +++ b/dev/LibDebugger/POSIXMachContract.h @@ -0,0 +1,144 @@ +/*** + (C) 2025 Amlal El Mahrouss + */ + +#pragma once + +#ifdef _WIN32 +#error Windows doesn't have a POSIX/Mach subsystem, please combine with windows instead. +#endif + +/// @file POSIXMachContract.h +/// @brief POSIX/Mach debugger. + +#include + +#include +#include +#include +#include +#include +#include + +#ifdef __APPLE__ +#include +#include + +#define PTRACE_ATTACH PT_ATTACHEXC +#define PTRACE_DETACH PT_DETACH +#define PTRACE_POKETEXT PT_WRITE_I +#define PTRACE_CONT PT_CONTINUE +#define PTRACE_PEEKTEXT PT_READ_I +#endif + +namespace LibDebugger::POSIX +{ + /// \brief POSIXMachContract engine interface class in C++ + /// \author Amlal El Mahrouss + class POSIXMachContract final : public DebuggerContract + { + public: + explicit POSIXMachContract() = default; + ~POSIXMachContract() override = default; + + public: + POSIXMachContract& operator=(const POSIXMachContract&) = default; + POSIXMachContract(const POSIXMachContract&) = default; + + public: + bool Attach(ProcessID pid) noexcept override + { +#ifdef __APPLE__ + if (pid == 0) + return false; + + this->m_pid = pid; + return true; +#else + + if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1) + { + return false; + } + + this->m_pid = pid; + + waitpid(m_pid, nullptr, 0); + + return true; +#endif + } + + bool Break(CAddress addr) noexcept override + { +#ifdef __APPLE__ + task_read_t task; + task_for_pid(mach_task_self(), m_pid, &task); + kern_return_t ret = task_suspend(task); + + return ret == KERN_SUCCESS; +#else + uintptr_t original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0); + + if (original_data == -1) + { + return false; + } + + constexpr uint8_t kInt3x86 = 0xCC; + + uintptr_t data_with_int3 = (original_data & ~0xFF) | kInt3x86; // Insert INT3 (0xCC) + + if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) + { + return false; + } + + m_breakpoints[reinterpret_cast(addr)] = original_data; // Store original data + + return true; +#endif + } + + bool Continue() noexcept override + { +#ifdef __APPLE__ + task_read_t task; + task_for_pid(mach_task_self(), m_pid, &task); + kern_return_t ret = task_resume(task); + + return ret == KERN_SUCCESS; +#else + if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1) + { + + return false; + } + + int status; + waitpid(m_pid, &status, 0); + + return WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP; +#endif + } + + bool Detach() noexcept override + { +#ifdef __APPLE__ + this->Continue(); + + task_read_t task; + task_for_pid(mach_task_self(), m_pid, &task); + + kern_return_t kr = mach_port_deallocate(mach_task_self(), task); + + return kr = KERN_SUCCESS; +#else + return ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1; +#endif + } + + private: + ProcessID m_pid{0}; + }; +} // namespace LibDebugger::POSIX diff --git a/dev/LibDebugger/src/POSIXContract.cc b/dev/LibDebugger/src/POSIXContract.cc deleted file mode 100644 index 3caeabf..0000000 --- a/dev/LibDebugger/src/POSIXContract.cc +++ /dev/null @@ -1,128 +0,0 @@ -/*** - (C) 2025 Amlal El Mahrouss - */ - -#include -#include -#include -#include -#include - -#ifndef _WIN32 - -static bool kKeepRunning = false; -static LibDebugger::POSIX::POSIXDebuggerContract kDebugger; -static pid_t kPID = 0L; - -/// @internal -/// @brief Handles CTRL-C signal on debugger. -static void dbgl_ctrlc_handler(std::int32_t _) -{ - if (!kPID) - { - return; - } - - auto list = kDebugger.Get(); - - LibDebugger::CAddress addr = (LibDebugger::CAddress)list[list.size() - 1]; - - if (!addr) - { - pfd::notify("Debugger Event", "Invalid breakpoint at: " + std::to_string(list[list.size() - 1])); - return; - } - - kDebugger.Break(addr); - - pfd::notify("Debugger Event", "Breakpoint at: " + std::to_string(list[list.size() - 1])); - - kKeepRunning = false; -} - -LIBCOMPILER_MODULE(DebuggerPOSIX) -{ - pfd::notify("Debugger Event", "NeKernel Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); - - if (argc >= 3 && std::string(argv[1]) == "-p" && - argv[2] != nullptr) - { - kPID = std::stoi(argv[2]); - kDebugger.Attach(kPID); - } - - ::signal(SIGINT, dbgl_ctrlc_handler); - - while (YES) - { - if (kKeepRunning) - { - continue; - } - - std::string cmd; - std::getline(std::cin, cmd); - - if (cmd == "c" || - cmd == "cont") - { - kDebugger.Continue(); - kKeepRunning = true; - - std::cout << "[+] Continuing...\n"; - pfd::notify("Debugger Event", "Continuing..."); - } - - if (cmd == "d" || - cmd == "detach") - kDebugger.Detach(); - - if (cmd == "attach") - { - std::cout << "[?] Enter a PID to attach on: "; - - std::getline(std::cin, cmd); - kPID = std::stoi(cmd.c_str()); - - pfd::notify("Debugger Event", "Attach process: " + std::to_string(kPID)); - - kDebugger.Attach(kPID); - } - - if (cmd == "exit") - { - if (kPID > 0) - kDebugger.Detach(); - - break; - } - - if (cmd == "break" || - cmd == "b") - { - std::cout << "[?] Enter an address/symbol to add a break on: "; - - std::getline(std::cin, cmd); - - auto addr = std::stoul(cmd.c_str(), nullptr, 16); - - try - { - pfd::notify("Debugger Event", "Add Breakpoint at: " + std::to_string(addr)); - } - catch (...) - { - pfd::notify("Debugger Event", "Add Breakpoint at: " + cmd); - } - - LibDebugger::CAddress breakpoint_addr = reinterpret_cast(addr); - - if (breakpoint_addr) - kDebugger.Break(breakpoint_addr); - } - } - - return EXIT_SUCCESS; -} - -#endif \ No newline at end of file diff --git a/dev/LibDebugger/src/POSIXMachContract.cc b/dev/LibDebugger/src/POSIXMachContract.cc new file mode 100644 index 0000000..4e7212c --- /dev/null +++ b/dev/LibDebugger/src/POSIXMachContract.cc @@ -0,0 +1,129 @@ +/*** + (C) 2025 Amlal El Mahrouss + */ + +#include +#include +#include +#include +#include + +#ifndef _WIN32 + +static bool kKeepRunning = false; +static LibDebugger::POSIX::POSIXMachContract kDebugger; +static pid_t kPID = 0L; +static LibDebugger::CAddress kActiveAddress = nullptr; + +/// @internal +/// @brief Handles CTRL-C signal on debugger. +static void dbgi_ctrlc_handler(std::int32_t _) +{ + if (!kPID) + { + return; + } + + auto list = kDebugger.Get(); + + kDebugger.Break(kActiveAddress); + + pfd::notify("Debugger Event", "Breakpoint hit!"); + + kKeepRunning = false; +} + +LIBCOMPILER_MODULE(DebuggerPOSIX) +{ + pfd::notify("Debugger Event", "NeKernel Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); + + if (argc >= 3 && std::string(argv[1]) == "-p" && + argv[2] != nullptr) + { + kPID = std::stoi(argv[2]); + kDebugger.Attach(kPID); + } + + ::signal(SIGINT, dbgi_ctrlc_handler); + + while (YES) + { + if (kKeepRunning) + { + continue; + } + + std::string cmd; + std::getline(std::cin, cmd); + + if (cmd == "c" || + cmd == "cont" || + cmd == "continue") + { + kDebugger.Continue(); + kKeepRunning = true; + + std::cout << "[+] Continuing...\n"; + pfd::notify("Debugger Event", "Continuing..."); + } + + if (cmd == "d" || + cmd == "detach") + kDebugger.Detach(); + + if (cmd == "attach" || + cmd == "pid" || + cmd == "a") + { + std::cout << "[?] Enter a PID to attach on: "; + + std::getline(std::cin, cmd); + kPID = std::stoi(cmd.c_str()); + + pfd::notify("Debugger Event", "Attach process: " + std::to_string(kPID)); + + kDebugger.Attach(kPID); + } + + if (cmd == "exit") + { + if (kPID > 0) + kDebugger.Detach(); + + break; + } + +#ifndef __APPLE__ + if (cmd == "break" || + cmd == "b") + { + std::cout << "[?] Enter an address/symbol to add a break on: "; + + std::getline(std::cin, cmd); + + auto addr = std::stoul(cmd.c_str(), nullptr, 16); + + try + { + pfd::notify("Debugger Event", "Add Breakpoint at: " + std::to_string(addr)); + } + catch (...) + { + pfd::notify("Debugger Event", "Add Breakpoint at: " + cmd); + } + + LibDebugger::CAddress breakpoint_addr = reinterpret_cast(addr); + + if (breakpoint_addr) + { + kActiveAddress = breakpoint_addr; + kDebugger.Break(kActiveAddress); + } + } +#endif // ifndef __APPLE__ + } + + return EXIT_SUCCESS; +} + +#endif \ No newline at end of file -- cgit v1.2.3