Moved X11 DIB stuff to x11drv.
[wine.git] / if1632 / signal.c
blob29ade19212f93ff8ab402d00c045db43463f83c9
1 /*
2 * Emulator signal handling
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <time.h>
12 #include <setjmp.h>
14 #include <sys/time.h>
15 #include <sys/timeb.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
19 #include "debugger.h"
20 #include "options.h"
21 #include "sig_context.h"
22 #include "miscemu.h"
23 #include "dosexe.h"
24 #include "thread.h"
25 #include "debug.h"
27 static const char * const SIGNAL_traps[] =
29 "Division by zero exception", /* 0 */
30 "Debug exception", /* 1 */
31 "NMI interrupt", /* 2 */
32 "Breakpoint exception", /* 3 */
33 "Overflow exception", /* 4 */
34 "Bound range exception", /* 5 */
35 "Invalid opcode exception", /* 6 */
36 "Device not available exception", /* 7 */
37 "Double fault exception", /* 8 */
38 "Coprocessor segment overrun", /* 9 */
39 "Invalid TSS exception", /* 10 */
40 "Segment not present exception", /* 11 */
41 "Stack fault", /* 12 */
42 "General protection fault", /* 13 */
43 "Page fault", /* 14 */
44 "Unknown exception", /* 15 */
45 "Floating point exception", /* 16 */
46 "Alignment check exception", /* 17 */
47 "Machine check exception" /* 18 */
50 #define NB_TRAPS (sizeof(SIGNAL_traps) / sizeof(SIGNAL_traps[0]))
52 extern void SIGNAL_SetHandler( int sig, void (*func)(), int flags );
53 extern BOOL32 INSTR_EmulateInstruction( SIGCONTEXT *context );
56 /**********************************************************************
57 * SIGNAL_break
59 * Handle Ctrl-C and such
61 static HANDLER_DEF(SIGNAL_break)
63 HANDLER_INIT();
64 if (Options.debug)
65 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
66 else exit(0);
70 /**********************************************************************
71 * SIGNAL_trap
73 * SIGTRAP handler.
75 static HANDLER_DEF(SIGNAL_trap)
77 HANDLER_INIT();
78 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
82 /**********************************************************************
83 * SIGNAL_fault
85 * Segfault handler.
87 static HANDLER_DEF(SIGNAL_fault)
89 const char *fault = "Segmentation fault";
91 HANDLER_INIT();
92 if (INSTR_EmulateInstruction( HANDLER_CONTEXT )) return;
93 #ifdef TRAP_sig
94 if (TRAP_sig( HANDLER_CONTEXT ) < NB_TRAPS)
95 fault = SIGNAL_traps[TRAP_sig( HANDLER_CONTEXT )];
96 #endif
97 if (IS_SELECTOR_SYSTEM(CS_sig(HANDLER_CONTEXT)))
99 MSG("%s in 32-bit code (0x%08lx).\n", fault, EIP_sig(HANDLER_CONTEXT));
101 else
103 MSG("%s in 16-bit code (%04x:%04lx).\n", fault,
104 (WORD)CS_sig(HANDLER_CONTEXT), EIP_sig(HANDLER_CONTEXT) );
106 #ifdef CR2_sig
107 MSG("Fault address is 0x%08lx\n",CR2_sig(HANDLER_CONTEXT));
108 #endif
109 wine_debug( signal, HANDLER_CONTEXT );
113 /***********************************************************************
114 * SIGNAL_SetContext
116 * Set the register values from a sigcontext.
118 #ifdef UNUSED_FUNCTIONS
119 static void SIGNAL_SetSigContext( const SIGCONTEXT *sigcontext,
120 CONTEXT *context )
122 EAX_reg(context) = EAX_sig(sigcontext);
123 EBX_reg(context) = EBX_sig(sigcontext);
124 ECX_reg(context) = ECX_sig(sigcontext);
125 EDX_reg(context) = EDX_sig(sigcontext);
126 ESI_reg(context) = ESI_sig(sigcontext);
127 EDI_reg(context) = EDI_sig(sigcontext);
128 EBP_reg(context) = EBP_sig(sigcontext);
129 EFL_reg(context) = EFL_sig(sigcontext);
130 EIP_reg(context) = EIP_sig(sigcontext);
131 ESP_reg(context) = ESP_sig(sigcontext);
132 CS_reg(context) = LOWORD(CS_sig(sigcontext));
133 DS_reg(context) = LOWORD(DS_sig(sigcontext));
134 ES_reg(context) = LOWORD(ES_sig(sigcontext));
135 SS_reg(context) = LOWORD(SS_sig(sigcontext));
136 #ifdef FS_sig
137 FS_reg(context) = LOWORD(FS_sig(sigcontext));
138 #else
139 GET_FS( FS_reg(&DEBUG_context) );
140 FS_reg(context) &= 0xffff;
141 #endif
142 #ifdef GS_sig
143 GS_reg(context) = LOWORD(GS_sig(sigcontext));
144 #else
145 GET_GS( GS_reg(&DEBUG_context) );
146 GS_reg(context) &= 0xffff;
147 #endif
149 #endif
152 /***********************************************************************
153 * SIGNAL_GetSigContext
155 * Build a sigcontext from the register values.
157 #ifdef UNUSED_FUNCTIONS
158 static void SIGNAL_GetSigContext( SIGCONTEXT *sigcontext,
159 const CONTEXT *context )
161 EAX_sig(sigcontext) = EAX_reg(context);
162 EBX_sig(sigcontext) = EBX_reg(context);
163 ECX_sig(sigcontext) = ECX_reg(context);
164 EDX_sig(sigcontext) = EDX_reg(context);
165 ESI_sig(sigcontext) = ESI_reg(context);
166 EDI_sig(sigcontext) = EDI_reg(context);
167 EBP_sig(sigcontext) = EBP_reg(context);
168 EFL_sig(sigcontext) = EFL_reg(context);
169 EIP_sig(sigcontext) = EIP_reg(context);
170 ESP_sig(sigcontext) = ESP_reg(context);
171 CS_sig(sigcontext) = CS_reg(context);
172 DS_sig(sigcontext) = DS_reg(context);
173 ES_sig(sigcontext) = ES_reg(context);
174 SS_sig(sigcontext) = SS_reg(context);
175 #ifdef FS_sig
176 FS_sig(sigcontext) = FS_reg(context);
177 #else
178 SET_FS( FS_reg(&DEBUG_context) );
179 #endif
180 #ifdef GS_sig
181 GS_sig(sigcontext) = GS_reg(context);
182 #else
183 SET_GS( GS_reg(&DEBUG_context) );
184 #endif
186 #endif
189 /***********************************************************************
190 * SIGNAL_InfoRegisters
192 * Display registers information.
194 void SIGNAL_InfoRegisters( CONTEXT *context )
196 MSG(" CS:%04x SS:%04x DS:%04x ES:%04x FS:%04x GS:%04x",
197 (WORD)CS_reg(context), (WORD)SS_reg(context),
198 (WORD)DS_reg(context), (WORD)ES_reg(context),
199 (WORD)FS_reg(context), (WORD)GS_reg(context) );
200 MSG( "\n EIP:%08lx ESP:%08lx EBP:%08lx EFLAGS:%08lx\n",
201 EIP_reg(context), ESP_reg(context),
202 EBP_reg(context), EFL_reg(context) );
203 MSG( " EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n",
204 EAX_reg(context), EBX_reg(context),
205 ECX_reg(context), EDX_reg(context) );
206 MSG( " ESI:%08lx EDI:%08lx\n",
207 ESI_reg(context), EDI_reg(context) );
211 /**********************************************************************
212 * SIGNAL_InitEmulator
214 * Initialize emulator signals.
216 BOOL32 SIGNAL_InitEmulator(void)
218 SIGNAL_SetHandler( SIGINT, (void (*)())SIGNAL_break, 1);
219 SIGNAL_SetHandler( SIGSEGV, (void (*)())SIGNAL_fault, 1);
220 SIGNAL_SetHandler( SIGILL, (void (*)())SIGNAL_fault, 1);
221 SIGNAL_SetHandler( SIGFPE, (void (*)())SIGNAL_fault, 1);
222 SIGNAL_SetHandler( SIGTRAP, (void (*)())SIGNAL_trap, 1); /* debugger */
223 SIGNAL_SetHandler( SIGHUP, (void (*)())SIGNAL_trap, 1); /* forced break*/
224 #ifdef SIGBUS
225 SIGNAL_SetHandler( SIGBUS, (void (*)())SIGNAL_fault, 1);
226 #endif
227 instr_emu_call = INSTR_EmulateInstruction;
228 return TRUE;