4 * Copyright 1995 Alexandre Julliard
18 #include <sys/timeb.h>
19 #include <sys/types.h>
22 #ifdef HAVE_SYS_PARAM_H
23 # include <sys/param.h>
28 # ifdef HAVE_SYS_SYSCALL_H
29 # include <sys/syscall.h>
42 void (*fnWINE_Debugger)(int,SIGCONTEXT*) = NULL;
43 void (*ctx_debug_call)(int sig,CONTEXT*ctx)=NULL;
44 BOOL32 (*fnINSTR_EmulateInstruction)(SIGCONTEXT*ctx)=NULL;
48 /* i386 specific faults */
49 static const char * const SIGNAL_traps[] =
51 "Division by zero exception", /* 0 */
52 "Debug exception", /* 1 */
53 "NMI interrupt", /* 2 */
54 "Breakpoint exception", /* 3 */
55 "Overflow exception", /* 4 */
56 "Bound range exception", /* 5 */
57 "Invalid opcode exception", /* 6 */
58 "Device not available exception", /* 7 */
59 "Double fault exception", /* 8 */
60 "Coprocessor segment overrun", /* 9 */
61 "Invalid TSS exception", /* 10 */
62 "Segment not present exception", /* 11 */
63 "Stack fault", /* 12 */
64 "General protection fault", /* 13 */
65 "Page fault", /* 14 */
66 "Unknown exception", /* 15 */
67 "Floating point exception", /* 16 */
68 "Alignment check exception", /* 17 */
69 "Machine check exception" /* 18 */
71 #define NB_TRAPS (sizeof(SIGNAL_traps) / sizeof(SIGNAL_traps[0]))
74 /* Linux sigaction function */
76 #if defined(linux) && defined(__i386__)
77 /* This is the sigaction structure from the Linux 2.1.20 kernel. */
79 struct kernel_sigaction
82 unsigned long sa_mask;
83 unsigned long sa_flags;
84 void (*sa_restorer)();
87 /* Similar to the sigaction function in libc, except it leaves alone the
88 restorer field, which is used to specify the signal stack address */
89 static __inline__ int wine_sigaction( int sig, struct kernel_sigaction *new,
90 struct kernel_sigaction *old )
93 __asm__ __volatile__( "pushl %%ebx\n\t"
98 : "0" (SYS_sigaction),
103 __asm__ __volatile__( "int $0x80"
105 : "0" (SYS_sigaction),
115 #endif /* linux && __i386__ */
119 static char SIGNAL_Stack[16384];
120 static sigset_t async_signal_set;
122 /**********************************************************************
125 * wait4 terminated child processes
127 static HANDLER_DEF(SIGNAL_child)
131 wait4( 0, NULL, WNOHANG, NULL);
132 #elif defined (HAVE_WAITPID)
133 /* I am sort-of guessing that this is the same as the wait4 call. */
134 waitpid (0, NULL, WNOHANG);
141 /**********************************************************************
144 void SIGNAL_SetHandler( int sig, void (*func)(), int flags )
148 #if defined(linux) && defined(__i386__)
150 struct kernel_sigaction sig_act;
151 sig_act.sa_handler = func;
152 sig_act.sa_flags = SA_RESTART | (flags) ? SA_NOMASK : 0;
154 /* Point to the top of the stack, minus 4 just in case, and make
156 sig_act.sa_restorer =
157 (void (*)())((int)(SIGNAL_Stack + sizeof(SIGNAL_Stack) - 4) & ~3);
158 ret = wine_sigaction( sig, &sig_act, NULL );
160 #else /* linux && __i386__ */
162 struct sigaction sig_act;
163 sig_act.sa_handler = func;
164 sigemptyset( &sig_act.sa_mask );
166 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
167 sig_act.sa_flags = SA_ONSTACK;
168 # elif defined (__svr4__) || defined(_SCO_DS)
169 sig_act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
170 # elif defined(__EMX__)
171 sig_act.sa_flags = 0; /* FIXME: EMX has only SA_ACK and SA_SYSV */
173 sig_act.sa_flags = 0;
175 ret = sigaction( sig, &sig_act, NULL );
177 #endif /* linux && __i386__ */
181 perror( "sigaction" );
186 extern void stop_wait(int a);
187 extern void WINSOCK_sigio(int a);
188 extern void ASYNC_sigio(int a);
191 /**********************************************************************
192 * SIGNAL_MaskAsyncEvents
194 void SIGNAL_MaskAsyncEvents( BOOL32 flag )
196 sigprocmask( (flag) ? SIG_BLOCK : SIG_UNBLOCK , &async_signal_set, NULL);
199 extern void SIGNAL_SetHandler( int sig, void (*func)(), int flags );
201 /**********************************************************************
204 * Handle Ctrl-C and such
206 static HANDLER_DEF(SIGNAL_break)
209 if (Options.debug && fnWINE_Debugger)
210 fnWINE_Debugger( signal, HANDLER_CONTEXT ); /* Enter our debugger */
215 /**********************************************************************
220 static HANDLER_DEF(SIGNAL_trap)
224 fnWINE_Debugger( signal, HANDLER_CONTEXT ); /* Enter our debugger */
228 /**********************************************************************
233 static HANDLER_DEF(SIGNAL_fault)
235 const char *fault = "Segmentation fault";
240 #if defined(TRAP_sig) && defined(CR2_sig)
241 if (TRAP_sig(HANDLER_CONTEXT) == 0x0e
242 && VIRTUAL_HandleFault( (LPVOID)CR2_sig(HANDLER_CONTEXT) ))
246 if (fnINSTR_EmulateInstruction &&
247 fnINSTR_EmulateInstruction( HANDLER_CONTEXT )
252 if (TRAP_sig( HANDLER_CONTEXT ) < NB_TRAPS)
253 fault = SIGNAL_traps[TRAP_sig( HANDLER_CONTEXT )];
255 if (IS_SELECTOR_SYSTEM(CS_sig(HANDLER_CONTEXT)))
257 MSG("%s in 32-bit code (0x%08lx).\n", fault, EIP_sig(HANDLER_CONTEXT));
261 MSG("%s in 16-bit code (%04x:%04lx).\n", fault,
262 (WORD)CS_sig(HANDLER_CONTEXT), EIP_sig(HANDLER_CONTEXT) );
265 MSG("Fault address is 0x%08lx\n",CR2_sig(HANDLER_CONTEXT));
270 fnWINE_Debugger( signal, HANDLER_CONTEXT );
274 /**********************************************************************
275 * SIGNAL_InitHandlers
277 void SIGNAL_InitHandlers(void)
279 SIGNAL_SetHandler( SIGINT, (void (*)())SIGNAL_break, 1);
280 SIGNAL_SetHandler( SIGSEGV, (void (*)())SIGNAL_fault, 1);
281 SIGNAL_SetHandler( SIGILL, (void (*)())SIGNAL_fault, 1);
282 SIGNAL_SetHandler( SIGFPE, (void (*)())SIGNAL_fault, 1);
283 SIGNAL_SetHandler( SIGTRAP, (void (*)())SIGNAL_trap, 1); /* debugger */
284 SIGNAL_SetHandler( SIGHUP, (void (*)())SIGNAL_trap, 1); /* forced break*/
286 SIGNAL_SetHandler( SIGBUS, (void (*)())SIGNAL_fault, 1);
291 /**********************************************************************
294 BOOL32 SIGNAL_Init(void)
296 #ifdef HAVE_WORKING_SIGALTSTACK
297 struct sigaltstack ss;
298 ss.ss_sp = SIGNAL_Stack;
299 ss.ss_size = sizeof(SIGNAL_Stack);
301 if (sigaltstack(&ss, NULL) < 0)
306 #endif /* HAVE_SIGALTSTACK */
308 sigemptyset(&async_signal_set);
310 SIGNAL_SetHandler( SIGCHLD, (void (*)())SIGNAL_child, 1);
312 sigaddset(&async_signal_set, SIGUSR2);
313 SIGNAL_SetHandler( SIGUSR2, (void (*)())stop_wait, 1); /* For IPC */
316 sigaddset(&async_signal_set, SIGIO);
317 /* SIGNAL_SetHandler( SIGIO, (void (*)())WINSOCK_sigio, 0); */
318 SIGNAL_SetHandler( SIGIO, (void (*)())ASYNC_sigio, 0);
320 sigaddset(&async_signal_set, SIGALRM);
322 /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead */
323 signal (SIGPIPE, SIG_IGN);
324 SIGNAL_InitHandlers();