diff --git a/Breakpoint.cpp b/Breakpoint.cpp new file mode 100644 index 0000000..77568b3 --- /dev/null +++ b/Breakpoint.cpp @@ -0,0 +1,65 @@ +// Breakpoint.cpp +#include "Breakpoint.hpp" +#include +#include +#include +#include +#include +#include +#include + +Breakpoint::Breakpoint(pid_t pid, std::intptr_t addr) + : m_pid(pid), m_addr(addr) {} + +void Breakpoint::enable() { + if (m_addr % 4 != 0) { + throw std::runtime_error("[!] Breakpoint address is not 4-byte aligned (AArch64 requires alignment)."); + } + + errno = 0; + long data = ptrace(PTRACE_PEEKDATA, m_pid, m_addr, nullptr); + if (data == -1 && errno != 0) { + perror("ptrace PEEKDATA failed"); + throw std::runtime_error("ptrace PEEKDATA failed"); + } + + std::memcpy(&m_saved_data, &data, sizeof(uint32_t)); + + uint32_t brk_instr = 0xD4200000; + long data_with_brk; + std::memcpy(&data_with_brk, &brk_instr, sizeof(uint32_t)); + data_with_brk |= (data & ~0xFFFFFFFF); + + if (ptrace(PTRACE_POKEDATA, m_pid, m_addr, data_with_brk) == -1) { + perror("ptrace POKEDATA failed"); + throw std::runtime_error("ptrace POKEDATA failed"); + } + + m_enabled = true; +} + +void Breakpoint::disable() { + errno = 0; + long data = ptrace(PTRACE_PEEKDATA, m_pid, m_addr, nullptr); + if (data == -1 && errno != 0) { + perror("ptrace PEEKDATA failed"); + throw std::runtime_error("ptrace PEEKDATA failed"); + } + + long restored_data = (data & ~0xFFFFFFFF) | m_saved_data; + if (ptrace(PTRACE_POKEDATA, m_pid, m_addr, restored_data) == -1) { + perror("ptrace POKEDATA failed"); + throw std::runtime_error("ptrace POKEDATA failed"); + } + + m_enabled = false; +} + +std::intptr_t Breakpoint::get_address() const { + return m_addr; +} + +bool Breakpoint::is_enabled() const { + return m_enabled; +} + diff --git a/Breakpoint.hpp b/Breakpoint.hpp new file mode 100644 index 0000000..cb98cb5 --- /dev/null +++ b/Breakpoint.hpp @@ -0,0 +1,23 @@ +// Breakpoint.hpp +#pragma once +#include +#include + +class Breakpoint { +public: + Breakpoint(pid_t pid, std::intptr_t addr); + + void enable(); + void disable(); + + std::intptr_t get_address() const; + + bool is_enabled() const; + +private: + pid_t m_pid; + std::intptr_t m_addr; + bool m_enabled = false; + uint8_t m_saved_data; +}; + diff --git a/Makefile b/Makefile index b283e8b..f8e6b15 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CXX = g++ CXXFLAGS = -Wall -Wextra -Werror -SRC = main.cpp Debugger.cpp +SRC = main.cpp Debugger.cpp Breakpoint.cpp OBJ = $(SRC:.cpp=.o) TARGET = nullDBG