diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-03-26 09:29:01 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-03-26 09:29:15 +0100 |
| commit | bc03b2dbee8a7458a3ed89abd643bf004f0f403b (patch) | |
| tree | 57d85ffb36d336e07c2c51364ed09dc5d4a6e15d /dev/LibDebugger/POSIX.h | |
| parent | 2916cbbcded4a0ca516fb94f3018e6181db4988a (diff) | |
feat(debugger): Better design for LibDebugger, and a patch for the POSIX
CLI.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/LibDebugger/POSIX.h')
| -rw-r--r-- | dev/LibDebugger/POSIX.h | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/dev/LibDebugger/POSIX.h b/dev/LibDebugger/POSIX.h deleted file mode 100644 index b863f58..0000000 --- a/dev/LibDebugger/POSIX.h +++ /dev/null @@ -1,124 +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 <iostream> -#include <unordered_map> - -#include <sys/ptrace.h> -#include <sys/types.h> -#include <sys/wait.h> -#include <sys/user.h> -#include <unistd.h> -#include <stdint.h> - -#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 LocalDebuggerPOSIX engine interface class in C++ - /// \author Amlal El Mahrouss - class LocalDebuggerPOSIX final - { - public: - explicit LocalDebuggerPOSIX() = default; - ~LocalDebuggerPOSIX() = default; - - public: - LocalDebuggerPOSIX& operator=(const LocalDebuggerPOSIX&) = default; - LocalDebuggerPOSIX(const LocalDebuggerPOSIX&) = default; - - public: - bool Attach(pid_t pid) - { - if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1) - { - return false; - } - - this->m_pid = pid; - - waitpid(m_pid, nullptr, 0); - - return true; - } - - bool Break(CAddr addr) - { - 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<uintptr_t>(addr)] = original_data; // Store original data - - return true; - } - - bool Continue() - { - if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1) - { - return false; - } - - int status; - waitpid(m_pid, &status, 0); - - if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) - { - std::cout << "[!] Breakpoint has been hit!" << std::endl; - } - - return true; - } - - bool Detach() - { - if (ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1) - { - return false; - } - - return true; - } - - std::unordered_map<uintptr_t, uintptr_t>& Get() - { - return m_breakpoints; - } - - private: - pid_t m_pid; - std::unordered_map<uintptr_t, uintptr_t> m_breakpoints; - }; -} // namespace LibDebugger::POSIX |
