From 654f083f60d0ab21d818b3c9cee556b49dc43682 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 23 Jul 2024 03:34:31 +0300 Subject: [PATCH] add debug function to print all registers --- include/debug.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ src/process.c | 8 +++++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/include/debug.h b/include/debug.h index b017058..9ca326b 100644 --- a/include/debug.h +++ b/include/debug.h @@ -3,9 +3,70 @@ #ifdef DEBUG #include +#include #define TRACE(fmt, ...) fprintf(stderr, "%s:%d:%s\tTRACE\t" fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__) +static void print_registers(const struct user_regs_struct* regs) +{ + fprintf(stderr, + "r15\t%p\n" + "r14\t%p\n" + "r13\t%p\n" + "r12\t%p\n" + "rbp\t%p\n" + "rbx\t%p\n" + "r11\t%p\n" + "r10\t%p\n" + "r9\t%p\n" + "r8\t%p\n" + "rax\t%p\n" + "rcx\t%p\n" + "rdx\t%p\n" + "rsi\t%p\n" + "rdi\t%p\n" + "orig_rax\t%p\n" + "rip\t%p\n" + "cs\t%p\n" + "eflags\t%p\n" + "rsp\t%p\n" + "ss\t%p\n" + "fs_base\t%p\n" + "gs_base\t%p\n" + "ds\t%p\n" + "es\t%p\n" + "fs\t%p\n" + "gs\t%p\n", + (void*)regs->r15, + (void*)regs->r14, + (void*)regs->r13, + (void*)regs->r12, + (void*)regs->rbp, + (void*)regs->rbx, + (void*)regs->r11, + (void*)regs->r10, + (void*)regs->r9, + (void*)regs->r8, + (void*)regs->rax, + (void*)regs->rcx, + (void*)regs->rdx, + (void*)regs->rsi, + (void*)regs->rdi, + (void*)regs->orig_rax, + (void*)regs->rip, + (void*)regs->cs, + (void*)regs->eflags, + (void*)regs->rsp, + (void*)regs->ss, + (void*)regs->fs_base, + (void*)regs->gs_base, + (void*)regs->ds, + (void*)regs->es, + (void*)regs->fs, + (void*)regs->gs + ); +} #else #define TRACE(fmt, ...) +#define print_registers(regs) #endif #endif \ No newline at end of file diff --git a/src/process.c b/src/process.c index f2dc965..a2813f8 100644 --- a/src/process.c +++ b/src/process.c @@ -290,7 +290,9 @@ int process_read_registers(process_status_t* thread, struct user_regs_struct* re }; memset(regs, '\0', sizeof(struct user_regs_struct)); - return ptrace(PTRACE_GETREGSET, thread->pid, NT_PRSTATUS, &data) < 0; + long ret = ptrace(PTRACE_GETREGSET, thread->pid, NT_PRSTATUS, &data); + print_registers(regs); + return ret < 0; } int process_write_registers(process_status_t* thread, const struct user_regs_struct* regs) @@ -300,5 +302,7 @@ int process_write_registers(process_status_t* thread, const struct user_regs_str .iov_len = sizeof(struct user_regs_struct) }; - return ptrace(PTRACE_SETREGSET, thread->pid, NT_PRSTATUS, &data) < 0; + long ret = ptrace(PTRACE_SETREGSET, thread->pid, NT_PRSTATUS, &data); + print_registers(regs); + return ret < 0; } \ No newline at end of file