75d4c1729081a95d8ce857084d153b9eae472763
[mate.git] / ffi / trap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4
5 #include "../debug.h"
6
7 /* TODO(bernhard): use {u,}int* types */
8
9 #define __USE_GNU
10 // Note by hs: my signal.h includes sys/uconctext which conflicts with
11 // asm/ucontext - this hack kinda solves the problem for me ;-) 
12 // so feel free to blame me for that s**t
13 #if defined __USE_XOPEN2K8
14 #undef __USE_XOPEN2K8
15 #define RESTORE
16 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
17 #endif
18 #include <signal.h>
19 #ifdef RESTORE
20 #define __USE_XOPEN2K8
21 #endif
22
23 #include <sys/ucontext.h>
24
25 ptrdiff_t mateHandler(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t);
26
27 #ifdef DBG_TRAP
28 #define dprintf(args...) do { printf (args); } while (0);
29 #else
30 #define dprintf(args...)
31 #endif
32
33 void chandler(int nSignal, siginfo_t *info, void *ctx)
34 {
35         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
36         greg_t *regs = mctx->gregs;
37
38         ptrdiff_t eip = (ptrdiff_t) regs[REG_EIP];
39         ptrdiff_t eax = (ptrdiff_t) regs[REG_EAX];
40         ptrdiff_t ebx = (ptrdiff_t) regs[REG_EBX];
41         ptrdiff_t esp = (ptrdiff_t) regs[REG_ESP];
42         ptrdiff_t esi = (ptrdiff_t) regs[REG_ESI];
43         dprintf("trap: type %d, eip 0x%08x, eax 0x%08x, ebx 0x%08x, "
44                         "esp 0x%08x, *esp 0x%08x\n", nSignal, eip,
45                         eax, ebx, esp, *(ptrdiff_t*) esp);
46
47         ptrdiff_t ret = mateHandler(eip, eax, ebx, esp, esi);
48         if (ret == -1) {
49                 dprintf("regdump @ EIP: 0x%08x\n", regs[REG_EIP]);
50                 dprintf("\tEAX: 0x%08lx EBX: 0x%08lx ECX: 0x%08lx EDX: 0x%08lx\n",
51                         regs[REG_EAX], regs[REG_EBX], regs[REG_ECX], regs[REG_EDX]);
52                 dprintf("\tESI: 0x%08lx EDI: 0x%08lx EBP: 0x%08lx ESP: 0x%08lx\n",
53                         regs[REG_ESI], regs[REG_EDI], regs[REG_EBP], regs[REG_ESP]);
54                 mctx->gregs[REG_EIP] = eip + 6;
55         } else {
56                 mctx->gregs[REG_EIP] = ret;
57         }
58 }
59
60 void register_signal(void)
61 {
62         struct sigaction illaction;
63         illaction.sa_sigaction = chandler;
64         sigemptyset(&illaction.sa_mask);
65         illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
66         sigaction(SIGILL, &illaction, NULL);
67
68         struct sigaction segvaction;
69         segvaction.sa_sigaction = chandler;
70         sigemptyset(&segvaction.sa_mask);
71         segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
72         sigaction(SIGSEGV, &segvaction, NULL);
73 }