Skip to content

Instantly share code, notes, and snippets.

@ice799
Created December 9, 2011 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ice799/1449701 to your computer and use it in GitHub Desktop.
Save ice799/1449701 to your computer and use it in GitHub Desktop.
/* there are bugs in this this is just copy and pasted, but might be a good reference */
static void
dump_registers(void *context)
{
ucontext_t *uc = context;
#if defined(linux)
#if defined(BUILD_ARCH_X86_64)
traceEvent(TRACE_ERROR, "RBP = 0x%0.16lx, RSP = 0x%0.16lx, RIP = 0x%0.16lx",
uc->uc_mcontext.gregs[REG_RBP], uc->uc_mcontext.gregs[REG_RSP],
uc->uc_mcontext.gregs[REG_RIP]);
traceEvent(TRACE_ERROR, "RAX = 0x%0.16lx, RBX = 0x%0.16lx, RCX = 0x%0.16lx, RDX = 0x%0.16lx",
uc->uc_mcontext.gregs[REG_RAX], uc->uc_mcontext.gregs[REG_RBX],
uc->uc_mcontext.gregs[REG_RCX], uc->uc_mcontext.gregs[REG_RDX]);
traceEvent(TRACE_ERROR, "RSI = 0x%0.16lx, RDI = 0x%0.16lx, R8 = 0x%0.16lx, R9 = 0x%0.16lx",
uc->uc_mcontext.gregs[REG_RSI], uc->uc_mcontext.gregs[REG_RDI],
uc->uc_mcontext.gregs[REG_R8], uc->uc_mcontext.gregs[REG_R9]);
traceEvent(TRACE_ERROR, "R10 = 0x%0.16lx, R11 = 0x%0.16lx, R12 = 0x%0.16lx, R13 = 0x%0.16lx",
uc->uc_mcontext.gregs[REG_R10], uc->uc_mcontext.gregs[REG_R11],
uc->uc_mcontext.gregs[REG_R12], uc->uc_mcontext.gregs[REG_R13]);
traceEvent(TRACE_ERROR, "R14 = 0x%0.16lx, R15 = 0x%0.16lx, RFLAGS = 0x%0.16lx",
uc->uc_mcontext.gregs[REG_R14], uc->uc_mcontext.gregs[REG_R15], uc->uc_mcontext.gregs[REG_EFL]);
#elif defined(BUILD_ARCH_X86)
traceEvent(TRACE_ERROR, "EBP = 0x%0.8lx, ESP = 0x%0.8lx, EIP = 0x%0.8lx",
uc->uc_mcontext.gregs[REG_EBP], uc->uc_mcontext.gregs[REG_ESP],
uc->uc_mcontext.gregs[REG_EIP]);
traceEvent(TRACE_ERROR, "EAX = 0x%0.8lx, EBX = 0x%0.8lx, ECX = 0x%0.8lx, EDX = 0x%0.8lx",
uc->uc_mcontext.gregs[REG_EAX], uc->uc_mcontext.gregs[REG_EBX],
uc->uc_mcontext.gregs[REG_ECX], uc->uc_mcontext.gregs[REG_EDX]);
traceEvent(TRACE_ERROR, "ESI = 0x%0.8lx, EDI = 0x%0.8lx, EFLAGS = 0x%0.8lx",
uc->uc_mcontext.gregs[REG_ESI], uc->uc_mcontext.gregs[REG_EDI], uc->uc_mcontext.gregs[REG_EFL]);
#endif /* defined(BUILD_ARCH_X86_64) */
#endif
traceEvent(TRACE_ERROR, "\n\n");
return;
}
void
sighandler(int signo, siginfo_t *siginfo, void *context)
{
if (signo == SIGBUS || signo == SIGSEGV ||
signo == SIGILL || signo == SIGFPE) {
traceEvent(TRACE_ERROR, "Faulting address: 0x%0.16lx, code: %d",
si->si_addr,
si->si_code);
}
dump_registers(context);
/* use kill to actually redeliver the signal to the process so it dies, not exit. */
exit(-1);
}
void
install_handler()
{
stack_t sigstack;
sigstack.ss_sp = malloc(SIGSTKSZ);
sigstack.ss_size = SIGSTKSZ;
sigstack.ss_flags = 0;
sigaltstack(&sigstack, NULL);
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
sigaction(SIGILL, &act, NULL);
sigaction(SIGSEGV, &act, NULL);
sigaction(SIGBUS, &act, NULL);
sigaction(SIGABRT, &act, NULL);
sigaction(SIGFPE, &act, NULL);
act.sa_flags = (SA_SIGINFO | SA_ONSTACK);
act.sa_sigaction = sighandler;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment