breakpoint: refactored to own file
This commit is contained in:
29
src/breakpoint.c
Normal file
29
src/breakpoint.c
Normal 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
28
src/breakpoint.h
Normal 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*);
|
@@ -8,39 +8,12 @@ debugger* new_debugger(pid_t pid) {
|
|||||||
return d;
|
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) {
|
void free_debugger(debugger* d) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < d->n_breakpoints; i++) free(d->breakpoints[i]);
|
for (i = 0; i < d->n_breakpoints; i++) free(d->breakpoints[i]);
|
||||||
free(d);
|
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) {
|
void set_breakpoint(debugger* d, void* addr) {
|
||||||
printf("Setting breakpoint at addres %p\n", addr);
|
printf("Setting breakpoint at addres %p\n", addr);
|
||||||
breakpoint* b = new_breakpoint(d->pid, addr);
|
breakpoint* b = new_breakpoint(d->pid, addr);
|
||||||
|
@@ -1,27 +1,8 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <editline/readline.h>
|
#include <editline/readline.h>
|
||||||
#include <sys/ptrace.h>
|
|
||||||
#include <sys/wait.h>
|
|
||||||
|
|
||||||
|
#include "breakpoint.h"
|
||||||
#include "util.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 {
|
typedef struct {
|
||||||
breakpoint** breakpoints;
|
breakpoint** breakpoints;
|
||||||
int n_breakpoints;
|
int n_breakpoints;
|
||||||
|
Reference in New Issue
Block a user