Release 950817
[wine/multimedia.git] / loader / signal.c
blob82d965170498ee359fccd4854efab0f1662de1b6
1 #ifndef WINELIB
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <time.h>
8 #include <setjmp.h>
10 #if defined(__NetBSD__) || defined(__FreeBSD__)
11 #include <sys/syscall.h>
12 #include <sys/param.h>
13 #else
14 #include <syscall.h>
15 #endif
17 #include "debugger.h"
18 #include "miscemu.h"
19 #include "registers.h"
20 #include "win.h"
22 #if !defined(BSD4_4) || defined(linux) || defined(__FreeBSD__)
23 char * cstack[4096];
24 #endif
25 struct sigaction segv_act;
26 struct sigaction usr2_act;
28 #ifdef linux
29 extern void ___sig_restore();
30 extern void ___masksig_restore();
32 /* Similar to the sigaction function in libc, except it leaves alone the
33 restorer field */
35 static int
36 wine_sigaction(int sig,struct sigaction * new, struct sigaction * old)
38 __asm__("int $0x80":"=a" (sig)
39 :"0" (SYS_sigaction),"b" (sig),"c" (new),"d" (old));
40 if (sig>=0)
41 return 0;
42 errno = -sig;
43 return -1;
45 #endif
48 #ifdef linux
49 static void win_fault(int signal, struct sigcontext_struct context_struct)
51 struct sigcontext_struct *context = &context_struct;
52 #else
53 static void win_fault(int signal, int code, struct sigcontext *context)
55 #endif
56 if (signal != SIGTRAP)
58 if (CS_reg(context) == WINE_CODE_SELECTOR)
60 fprintf(stderr, "Segmentation fault in Wine program (%x:%lx)."
61 " Please debug\n",
62 CS_reg(context), EIP_reg(context) );
64 else if (INSTR_EmulateInstruction( context )) return;
65 fprintf( stderr,"In win_fault %x:%lx\n",
66 CS_reg(context), EIP_reg(context) );
68 XUngrabPointer(display, CurrentTime);
69 XUngrabServer(display);
70 XFlush(display);
71 wine_debug( signal, context ); /* Enter our debugger */
74 void init_wine_signals(void)
76 extern void stop_wait(int a);
77 #ifdef linux
78 segv_act.sa_handler = (__sighandler_t) win_fault;
79 /* Point to the top of the stack, minus 4 just in case, and make
80 it aligned */
81 segv_act.sa_restorer =
82 (void (*)()) (((unsigned int)(cstack) + sizeof(cstack) - 4) & ~3);
83 usr2_act.sa_restorer= segv_act.sa_restorer;
84 usr2_act.sa_handler = (__sighandler_t) stop_wait;
85 /* Point to the top of the stack, minus 4 just in case, and make
86 it aligned */
87 wine_sigaction(SIGSEGV, &segv_act, NULL);
88 wine_sigaction(SIGILL, &segv_act, NULL);
89 wine_sigaction(SIGFPE, &segv_act, NULL);
90 wine_sigaction(SIGUSR2, &usr2_act, NULL);
91 #ifdef SIGBUS
92 wine_sigaction(SIGBUS, &segv_act, NULL);
93 #endif
94 wine_sigaction(SIGTRAP, &segv_act, NULL); /* For breakpoints */
95 #endif
96 #if defined(__NetBSD__) || defined(__FreeBSD__)
97 sigset_t sig_mask;
98 struct sigaltstack ss;
100 #if !defined (__FreeBSD__)
101 if ((ss.ss_base = malloc(MINSIGSTKSZ)) == NULL) {
102 #else
103 if ((ss.ss_sp = malloc(MINSIGSTKSZ)) == NULL) {
104 #endif
105 fprintf(stderr, "Unable to allocate signal stack (%d bytes)\n",
106 MINSIGSTKSZ);
107 exit(1);
109 ss.ss_size = MINSIGSTKSZ;
110 ss.ss_flags = 0;
111 if (sigaltstack(&ss, NULL) < 0) {
112 perror("sigstack");
113 exit(1);
115 sigemptyset(&sig_mask);
116 segv_act.sa_handler = (void (*)) win_fault;
117 segv_act.sa_flags = SA_ONSTACK;
118 segv_act.sa_mask = sig_mask;
119 if (sigaction(SIGBUS, &segv_act, NULL) < 0) {
120 perror("sigaction: SIGBUS");
121 exit(1);
123 segv_act.sa_handler = (void (*)) win_fault;
124 segv_act.sa_flags = SA_ONSTACK;
125 segv_act.sa_mask = sig_mask;
126 if (sigaction(SIGSEGV, &segv_act, NULL) < 0) {
127 perror("sigaction: SIGSEGV");
128 exit(1);
130 segv_act.sa_handler = (void (*)) win_fault; /* For breakpoints */
131 segv_act.sa_flags = SA_ONSTACK;
132 segv_act.sa_mask = sig_mask;
133 if (sigaction(SIGTRAP, &segv_act, NULL) < 0) {
134 perror("sigaction: SIGTRAP");
135 exit(1);
137 usr2_act.sa_handler = (void (*)) stop_wait; /* For breakpoints */
138 usr2_act.sa_flags = SA_ONSTACK;
139 usr2_act.sa_mask = sig_mask;
140 if (sigaction(SIGUSR2, &usr2_act, NULL) < 0) {
141 perror("sigaction: SIGUSR2");
142 exit(1);
144 #endif
147 #endif /* ifndef WINELIB */