ajout de breakpoint

This commit is contained in:
YANNIS
2025-05-23 02:09:22 +02:00
parent 7bae3bcc57
commit e97e1cbaf5
3 changed files with 89 additions and 1 deletions
+65
View File
@@ -0,0 +1,65 @@
// Breakpoint.cpp
#include "Breakpoint.hpp"
#include <sys/ptrace.h>
#include <unistd.h>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <cerrno>
#include <cstdio>
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;
}
+23
View File
@@ -0,0 +1,23 @@
// Breakpoint.hpp
#pragma once
#include <sys/types.h>
#include <cstdint>
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;
};
+1 -1
View File
@@ -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