From f4a3ae900294759eb79307137a1efa9e2fbe2a10 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 23 Mar 2025 11:10:06 +0100 Subject: debugger(posix): Refactor LibDebugger POSIX engine: rename, namespace update, API cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Renamed `IDebuggerEngine.h` → `POSIX.h` and `DebuggerEnginePOSIX.cc` → `POSIX.cc` - Moved `IDebuggerEngine` class into `LibDebugger::POSIX` namespace and renamed it to `Debugger` - Simplified method names: - `SetBreakpoint` → `Break` - `ContinueExecution` → `Continue` - Replaced generic return codes with `EXIT_SUCCESS` / `EXIT_FAILURE` - Updated includes and symbol usage to reflect changes across the codebase Signed-off-by: Amlal El Mahrouss --- dev/LibDebugger/IDebuggerEngine.h | 119 ---------------------------- dev/LibDebugger/POSIX.h | 121 +++++++++++++++++++++++++++++ dev/LibDebugger/src/DebuggerEnginePOSIX.cc | 70 ----------------- dev/LibDebugger/src/POSIX.cc | 75 ++++++++++++++++++ 4 files changed, 196 insertions(+), 189 deletions(-) delete mode 100644 dev/LibDebugger/IDebuggerEngine.h create mode 100644 dev/LibDebugger/POSIX.h delete mode 100644 dev/LibDebugger/src/DebuggerEnginePOSIX.cc create mode 100644 dev/LibDebugger/src/POSIX.cc (limited to 'dev/LibDebugger') diff --git a/dev/LibDebugger/IDebuggerEngine.h b/dev/LibDebugger/IDebuggerEngine.h deleted file mode 100644 index 45b7da3..0000000 --- a/dev/LibDebugger/IDebuggerEngine.h +++ /dev/null @@ -1,119 +0,0 @@ -/*** - (C) 2025 Amlal El Mahrouss - */ - -#include -#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 -{ -#ifdef __APPLE__ - typedef caddr_t CAddr; -#else - typedef char* CAddr; -#endif - - /// \brief Debugger engine interface class in C++ - /// \author Amlal El Mahrouss - class IDebuggerEngine final - { - public: - explicit IDebuggerEngine() = default; - ~IDebuggerEngine() = default; - - public: - IDebuggerEngine& operator=(const IDebuggerEngine&) = default; - IDebuggerEngine(const IDebuggerEngine&) = default; - - public: - void Attach(pid_t pid) - { - if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1) - { - perror("dbg: Attach"); - return; - } - - this->m_pid = pid; - - waitpid(m_pid, nullptr, 0); - - std::cout << "[+] Attached to process: " << m_pid << std::endl; - } - - void SetBreakpoint(CAddr addr) - { - uintptr_t original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0); - - if (original_data == -1) - { - perror("dbg: Peek"); - return; - } - - uintptr_t data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC) - - if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) - { - perror("dbg: Poke"); - return; - } - - std::cout << "[+] Breakpoint set at: " << addr << std::endl; - - m_breakpoints[reinterpret_cast(addr)] = original_data; // Store original data - } - - void ContinueExecution() - { - if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1) - { - perror("dbg: Cont"); - return; - } - - int status; - waitpid(m_pid, &status, 0); - - if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) - { - std::cout << "[!] Breakpoint hit." << std::endl; - } - } - - void Detach() - { - if (ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1) - { - perror("dbg: Detach"); - return; - } - - std::cout << "[-] Detached from process: " << m_pid << std::endl; - } - - std::unordered_map& Breakpoints() - { - return m_breakpoints; - } - - private: - pid_t m_pid; - std::unordered_map m_breakpoints; - }; -} // namespace LibDebugger diff --git a/dev/LibDebugger/POSIX.h b/dev/LibDebugger/POSIX.h new file mode 100644 index 0000000..d859868 --- /dev/null +++ b/dev/LibDebugger/POSIX.h @@ -0,0 +1,121 @@ +/*** + (C) 2025 Amlal El Mahrouss + */ + +#pragma once + +#include +#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 +{ +#ifdef __APPLE__ + typedef caddr_t CAddr; +#else + typedef char* CAddr; +#endif + + /// \brief Debugger engine interface class in C++ + /// \author Amlal El Mahrouss + class Debugger final + { + public: + explicit Debugger() = default; + ~Debugger() = default; + + public: + Debugger& operator=(const Debugger&) = default; + Debugger(const Debugger&) = default; + + public: + void Attach(pid_t pid) + { + if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1) + { + perror("dbg: Attach"); + return; + } + + this->m_pid = pid; + + waitpid(m_pid, nullptr, 0); + + std::cout << "[+] Attached to process: " << m_pid << std::endl; + } + + void Break(CAddr addr) + { + uintptr_t original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0); + + if (original_data == -1) + { + perror("dbg: Peek"); + return; + } + + uintptr_t data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC) + + if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) + { + perror("dbg: Poke"); + return; + } + + std::cout << "[+] Breakpoint set at: " << addr << std::endl; + + m_breakpoints[reinterpret_cast(addr)] = original_data; // Store original data + } + + void Continue() + { + if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1) + { + perror("dbg: Cont"); + return; + } + + int status; + waitpid(m_pid, &status, 0); + + if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) + { + std::cout << "[!] Breakpoint hit." << std::endl; + } + } + + void Detach() + { + if (ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1) + { + perror("dbg: Detach"); + return; + } + + std::cout << "[-] Detached from process: " << m_pid << std::endl; + } + + std::unordered_map& Breakpoints() + { + return m_breakpoints; + } + + private: + pid_t m_pid; + std::unordered_map m_breakpoints; + }; +} // namespace LibDebugger::POSIX diff --git a/dev/LibDebugger/src/DebuggerEnginePOSIX.cc b/dev/LibDebugger/src/DebuggerEnginePOSIX.cc deleted file mode 100644 index b8695fb..0000000 --- a/dev/LibDebugger/src/DebuggerEnginePOSIX.cc +++ /dev/null @@ -1,70 +0,0 @@ -/*** - (C) 2025 Amlal El Mahrouss - */ - -#include -#include - -#ifndef _WIN32 - -LIBCOMPILER_MODULE(DebuggerPOSIX) -{ - LibDebugger::IDebuggerEngine debugger; - pid_t pid = 0L; - - if (argc >= 3 && std::string(argv[1]) == "-p" && - argv[2] != nullptr) - { - pid = std::stoi(argv[2]); - debugger.Attach(pid); - } - - while (YES) - { - std::string cmd; - std::getline(std::cin, cmd); - - if (cmd == "c" || - cmd == "cont") - debugger.ContinueExecution(); - - if (cmd == "d" || - cmd == "detach") - debugger.Detach(); - - if (cmd == "attach") - { - std::cout << "[?] Enter a PID to attach on: "; - - std::getline(std::cin, cmd); - pid = std::stoi(cmd.c_str()); - - debugger.Attach(pid); - } - - if (cmd == "exit") - { - if (pid > 0) - debugger.Detach(); - - break; - } - - if (cmd == "break" || - cmd == "b") - { - std::cout << "[?] Enter an address to add a breakpoint on: "; - - std::getline(std::cin, cmd); - - LibDebugger::CAddr breakpoint_addr = reinterpret_cast(std::stoul(cmd.c_str(), nullptr, 16)); - - if (breakpoint_addr) - debugger.SetBreakpoint(breakpoint_addr); - } - } - - return 0; -} - -#endif \ No newline at end of file diff --git a/dev/LibDebugger/src/POSIX.cc b/dev/LibDebugger/src/POSIX.cc new file mode 100644 index 0000000..21e122c --- /dev/null +++ b/dev/LibDebugger/src/POSIX.cc @@ -0,0 +1,75 @@ +/*** + (C) 2025 Amlal El Mahrouss + */ + +#include +#include + +#ifndef _WIN32 + +LIBCOMPILER_MODULE(DebuggerPOSIX) +{ + if (argc < 1) + { + return EXIT_FAILURE; + } + + LibDebugger::POSIX::Debugger debugger; + pid_t pid = 0L; + + if (argc >= 3 && std::string(argv[1]) == "-p" && + argv[2] != nullptr) + { + pid = std::stoi(argv[2]); + debugger.Attach(pid); + } + + while (YES) + { + std::string cmd; + std::getline(std::cin, cmd); + + if (cmd == "c" || + cmd == "cont") + debugger.Continue(); + + if (cmd == "d" || + cmd == "detach") + debugger.Detach(); + + if (cmd == "attach") + { + std::cout << "[?] Enter a PID to attach on: "; + + std::getline(std::cin, cmd); + pid = std::stoi(cmd.c_str()); + + debugger.Attach(pid); + } + + if (cmd == "exit") + { + if (pid > 0) + debugger.Detach(); + + break; + } + + if (cmd == "break" || + cmd == "b") + { + std::cout << "[?] Enter an address to add a breakpoint on: "; + + std::getline(std::cin, cmd); + + LibDebugger::POSIX::CAddr breakpoint_addr = reinterpret_cast(std::stoul(cmd.c_str(), nullptr, 16)); + + if (breakpoint_addr) + debugger.Break(breakpoint_addr); + } + } + + return EXIT_SUCCESS; +} + +#endif \ No newline at end of file -- cgit v1.2.3