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