breakpoint: refactored to own file

This commit is contained in:
2017-09-15 14:41:21 +02:00
parent 4a41273987
commit 3650929fa0
4 changed files with 58 additions and 47 deletions

29
src/breakpoint.c Normal file
View File

@@ -0,0 +1,29 @@
#include "breakpoint.h"
breakpoint* new_breakpoint(pid_t pid, void* addr) {
breakpoint* b = malloc(sizeof(breakpoint));
b->pid = pid;
b->addr = addr;
b->enabled = 0;
b->data = 0;
return b;
}
void enable(breakpoint* b) {
long data = ptrace(PTRACE_PEEKDATA, b->pid, b->addr, 0);
b->data = data & 0xff;
uint64_t int3 = 0xcc;
uint64_t data_with_int3 = ((data & ~0xff) | int3);
ptrace(PTRACE_POKEDATA, b->pid, b->addr, data_with_int3);
b->enabled = 1;
}
void disable(breakpoint* b) {
long data = ptrace(PTRACE_PEEKDATA, b->pid, b->addr, 0);
long restored = ((data & ~0xff) | b->data);
ptrace(PTRACE_POKEDATA, b->pid, b->addr, restored);
b->enabled = 0;
}

28
src/breakpoint.h Normal file
View File

@@ -0,0 +1,28 @@
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
/* has to come here because of caddr_t on OS X */
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#ifdef __APPLE__
#define PTRACE_TRACEME PT_TRACE_ME
#define PTRACE_CONT PT_CONTINUE
#define PTRACE_PEEKDATA PT_READ_D
#define PTRACE_POKEDATA PT_WRITE_D
#else
#endif
typedef struct {
short enabled;
uint8_t data;
pid_t pid;
void* addr;
} breakpoint;
breakpoint* new_breakpoint(pid_t, void*);
void enable(breakpoint*);
void disable(breakpoint*);

View File

@@ -8,39 +8,12 @@ debugger* new_debugger(pid_t pid) {
return d;
}
breakpoint* new_breakpoint(pid_t pid, void* addr) {
breakpoint* b = malloc(sizeof(breakpoint));
b->pid = pid;
b->addr = addr;
b->enabled = 0;
b->data = 0;
return b;
}
void free_debugger(debugger* d) {
int i;
for (i = 0; i < d->n_breakpoints; i++) free(d->breakpoints[i]);
free(d);
}
void enable(breakpoint* b) {
long data = ptrace(PTRACE_PEEKDATA, b->pid, b->addr, 0);
b->data = data & 0xff;
uint64_t int3 = 0xcc;
uint64_t data_with_int3 = ((data & ~0xff) | int3);
ptrace(PTRACE_POKEDATA, b->pid, b->addr, data_with_int3);
b->enabled = 1;
}
void disable(breakpoint* b) {
long data = ptrace(PTRACE_PEEKDATA, b->pid, b->addr, 0);
long restored = ((data & ~0xff) | b->data);
ptrace(PTRACE_POKEDATA, b->pid, b->addr, restored);
b->enabled = 0;
}
void set_breakpoint(debugger* d, void* addr) {
printf("Setting breakpoint at addres %p\n", addr);
breakpoint* b = new_breakpoint(d->pid, addr);

View File

@@ -1,27 +1,8 @@
#include <stdint.h>
#include <unistd.h>
#include <editline/readline.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include "breakpoint.h"
#include "util.h"
#ifdef __APPLE__
#define PTRACE_TRACEME PT_TRACE_ME
#define PTRACE_CONT PT_CONTINUE
#define PTRACE_PEEKDATA PT_READ_D
#define PTRACE_POKEDATA PT_WRITE_D
#else
#endif
typedef struct {
short enabled;
uint8_t data;
pid_t pid;
void* addr;
} breakpoint;
typedef struct {
breakpoint** breakpoints;
int n_breakpoints;