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