nativeMaschine: s/unsigned int/ptrdiff_t/g
[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);
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
37         ptrdiff_t eip = (ptrdiff_t) mctx->gregs[REG_EIP];
38         ptrdiff_t eax = (ptrdiff_t) mctx->gregs[REG_EAX];
39         ptrdiff_t ebx = (ptrdiff_t) mctx->gregs[REG_EBX];
40         ptrdiff_t esp = (ptrdiff_t) mctx->gregs[REG_ESP];
41         dprintf("trap: type %d, eip 0x%08x, eax 0x%08x, ebx 0x%08x, "
42                         "esp 0x%08x, *esp 0x%08x\n", nSignal, eip,
43                         eax, ebx, esp, *(ptrdiff_t*) esp);
44
45         mctx->gregs[REG_EIP] = mateHandler(eip, eax, ebx, esp);
46 }
47
48 void register_signal(void)
49 {
50         struct sigaction illaction;
51         illaction.sa_sigaction = chandler;
52         sigemptyset(&illaction.sa_mask);
53         illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
54         sigaction(SIGILL, &illaction, NULL);
55
56         struct sigaction segvaction;
57         segvaction.sa_sigaction = chandler;
58         sigemptyset(&segvaction.sa_mask);
59         segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
60         sigaction(SIGSEGV, &segvaction, NULL);
61 }