Release 980503
[wine.git] / if1632 / signal.c
blobe07d41a8104d52fb745f7046f5c79746778ae078
1 /*
2 * Emulator signal handling
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <time.h>
13 #include <setjmp.h>
15 #include <sys/time.h>
16 #include <sys/timeb.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
20 #include "debugger.h"
21 #include "options.h"
22 #include "sig_context.h"
23 #include "miscemu.h"
24 #include "thread.h"
25 #include "debug.h"
28 extern void SIGNAL_SetHandler( int sig, void (*func)(), int flags );
29 extern BOOL32 INSTR_EmulateInstruction( SIGCONTEXT *context );
32 /**********************************************************************
33 * SIGNAL_break
35 * Handle Ctrl-C and such
37 static HANDLER_DEF(SIGNAL_break)
39 HANDLER_INIT();
40 if (Options.debug)
41 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
42 else exit(0);
46 /**********************************************************************
47 * SIGNAL_trap
49 * SIGTRAP handler.
51 static HANDLER_DEF(SIGNAL_trap)
53 HANDLER_INIT();
54 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
58 /**********************************************************************
59 * SIGNAL_fault
61 * Segfault handler.
63 static HANDLER_DEF(SIGNAL_fault)
65 HANDLER_INIT();
66 if (INSTR_EmulateInstruction( HANDLER_CONTEXT )) return;
67 if (IS_SELECTOR_SYSTEM(CS_sig(HANDLER_CONTEXT)))
69 MSG("Segmentation fault in 32-bit code (0x%08lx).\n",
70 EIP_sig(HANDLER_CONTEXT) );
72 else
74 MSG("Segmentation fault in 16-bit code (%04x:%04lx).\n",
75 (WORD)CS_sig(HANDLER_CONTEXT), EIP_sig(HANDLER_CONTEXT) );
77 #ifdef CR2_sig
78 fprintf(stderr,"Fault address is 0x%08lx\n",CR2_sig(HANDLER_CONTEXT));
79 #endif
80 wine_debug( signal, HANDLER_CONTEXT );
84 /***********************************************************************
85 * SIGNAL_SetContext
87 * Set the register values from a sigcontext.
89 static void SIGNAL_SetSigContext( const SIGCONTEXT *sigcontext,
90 CONTEXT *context )
92 EAX_reg(context) = EAX_sig(sigcontext);
93 EBX_reg(context) = EBX_sig(sigcontext);
94 ECX_reg(context) = ECX_sig(sigcontext);
95 EDX_reg(context) = EDX_sig(sigcontext);
96 ESI_reg(context) = ESI_sig(sigcontext);
97 EDI_reg(context) = EDI_sig(sigcontext);
98 EBP_reg(context) = EBP_sig(sigcontext);
99 EFL_reg(context) = EFL_sig(sigcontext);
100 EIP_reg(context) = EIP_sig(sigcontext);
101 ESP_reg(context) = ESP_sig(sigcontext);
102 CS_reg(context) = LOWORD(CS_sig(sigcontext));
103 DS_reg(context) = LOWORD(DS_sig(sigcontext));
104 ES_reg(context) = LOWORD(ES_sig(sigcontext));
105 SS_reg(context) = LOWORD(SS_sig(sigcontext));
106 #ifdef FS_sig
107 FS_reg(context) = LOWORD(FS_sig(sigcontext));
108 #else
109 GET_FS( FS_reg(&DEBUG_context) );
110 FS_reg(context) &= 0xffff;
111 #endif
112 #ifdef GS_sig
113 GS_reg(context) = LOWORD(GS_sig(sigcontext));
114 #else
115 GET_GS( GS_reg(&DEBUG_context) );
116 GS_reg(context) &= 0xffff;
117 #endif
121 /***********************************************************************
122 * SIGNAL_GetSigContext
124 * Build a sigcontext from the register values.
126 static void SIGNAL_GetSigContext( SIGCONTEXT *sigcontext,
127 const CONTEXT *context )
129 EAX_sig(sigcontext) = EAX_reg(context);
130 EBX_sig(sigcontext) = EBX_reg(context);
131 ECX_sig(sigcontext) = ECX_reg(context);
132 EDX_sig(sigcontext) = EDX_reg(context);
133 ESI_sig(sigcontext) = ESI_reg(context);
134 EDI_sig(sigcontext) = EDI_reg(context);
135 EBP_sig(sigcontext) = EBP_reg(context);
136 EFL_sig(sigcontext) = EFL_reg(context);
137 EIP_sig(sigcontext) = EIP_reg(context);
138 ESP_sig(sigcontext) = ESP_reg(context);
139 CS_sig(sigcontext) = CS_reg(context);
140 DS_sig(sigcontext) = DS_reg(context);
141 ES_sig(sigcontext) = ES_reg(context);
142 SS_sig(sigcontext) = SS_reg(context);
143 #ifdef FS_sig
144 FS_sig(sigcontext) = FS_reg(context);
145 #else
146 SET_FS( FS_reg(&DEBUG_context) );
147 #endif
148 #ifdef GS_sig
149 GS_sig(sigcontext) = GS_reg(context);
150 #else
151 SET_GS( GS_reg(&DEBUG_context) );
152 #endif
156 /***********************************************************************
157 * SIGNAL_InfoRegisters
159 * Display registers information.
161 void SIGNAL_InfoRegisters( CONTEXT *context )
163 MSG(" CS:%04x SS:%04x DS:%04x ES:%04x FS:%04x GS:%04x",
164 (WORD)CS_reg(context), (WORD)SS_reg(context),
165 (WORD)DS_reg(context), (WORD)ES_reg(context),
166 (WORD)FS_reg(context), (WORD)GS_reg(context) );
167 MSG( "\n EIP:%08lx ESP:%08lx EBP:%08lx EFLAGS:%08lx\n",
168 EIP_reg(context), ESP_reg(context),
169 EBP_reg(context), EFL_reg(context) );
170 MSG( " EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n",
171 EAX_reg(context), EBX_reg(context),
172 ECX_reg(context), EDX_reg(context) );
173 MSG( " ESI:%08lx EDI:%08lx\n",
174 ESI_reg(context), EDI_reg(context) );
178 /**********************************************************************
179 * SIGNAL_InitEmulator
181 * Initialize emulator signals.
183 BOOL32 SIGNAL_InitEmulator(void)
185 SIGNAL_SetHandler( SIGINT, (void (*)())SIGNAL_break, 1);
186 SIGNAL_SetHandler( SIGSEGV, (void (*)())SIGNAL_fault, 1);
187 SIGNAL_SetHandler( SIGILL, (void (*)())SIGNAL_fault, 1);
188 SIGNAL_SetHandler( SIGFPE, (void (*)())SIGNAL_fault, 1);
189 SIGNAL_SetHandler( SIGTRAP, (void (*)())SIGNAL_trap, 1); /* debugger */
190 SIGNAL_SetHandler( SIGHUP, (void (*)())SIGNAL_trap, 1); /* forced break*/
191 #ifdef SIGBUS
192 SIGNAL_SetHandler( SIGBUS, (void (*)())SIGNAL_fault, 1);
193 #endif
194 return TRUE;