diff --git a/Makefile b/Makefile index a352be0..44a288a 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ DEPS=$(patsubst %.c,%.d,$(SRCS)) all: debugus test test: test.o - $(CC) -o $@ $^ + $(CC) -gdwarf -o $@ $^ debugus: $(OBJS) ./mujs/build/debug/libmujs.o $(CC) -o $@ $^ $(LDFLAGS) diff --git a/debugus.c b/debugus.c index c5f3e84..f068771 100644 --- a/debugus.c +++ b/debugus.c @@ -1,3 +1,4 @@ +#define _XOPEN_SOURCE 600 #include #include #include @@ -9,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -164,10 +166,46 @@ typedef struct { HashTable js_descs; } Dbg; +siginfo_t dbg_get_siginfo(Dbg *dbg) +{ + siginfo_t i; + ptrace(PTRACE_GETSIGINFO, dbg->pid, NULL, &i); + return i; +} + +void dbg_handle_sigsegv(Dbg *dbg, siginfo_t info) +{ + unused(dbg); + LOG_ERR("Caught a segfault %d. SKILL ISSUE BRO\n", info.si_code); +} + +void dbg_handle_sigtrap(Dbg *dbg, siginfo_t info) +{ + void dbg_set_rip(Dbg *dbg, uint64_t v); + uint64_t dbg_get_rip(Dbg *dbg); + + switch (info.si_code) { + case SI_KERNEL: + case TRAP_BRKPT: + dbg_set_rip(dbg, dbg_get_rip(dbg) - 1); + LOG_INF("Hit breakpoint at 0x%"PRIxPTR"\n", dbg_get_rip(dbg)); + return; + case TRAP_TRACE: + return; + } +} + void dbg_wait(Dbg *dbg) { int status, options = 0; waitpid(dbg->pid, &status, options); + + siginfo_t info = dbg_get_siginfo(dbg); + switch (info.si_signo) { + case SIGTRAP: dbg_handle_sigtrap(dbg, info); break; + case SIGSEGV: dbg_handle_sigsegv(dbg, info); break; + default: LOG_INF("Signal %d\n!!", info.si_signo); break; + } } // Memory @@ -238,13 +276,11 @@ void dbg_set_rip(Dbg *dbg, uint64_t v) void dbg_step_brk(Dbg *dbg) { - uint64_t loc = dbg_get_rip(dbg) - 1; + uint64_t loc = dbg_get_rip(dbg); char key[20]; snprintf(key, sizeof(key), "0x%"PRIxPTR, (uintptr_t)loc); Brk *brk = hashtable_get(&dbg->brks, key); if ((brk != NULL && brk->enabled)) { - uint64_t prev_instr = loc; - dbg_set_rip(dbg, prev_instr); brk_disable(brk); ptrace(PTRACE_SINGLESTEP, brk->pid, NULL, NULL); dbg_wait(dbg);