Release 980315
[wine/multimedia.git] / if1632 / signal.c
blob06a36d3ac4f442323975dd866f4c4c282e260c10
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 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__svr4__) || defined(_SCO_DS) || defined(__EMX__)
21 # if !defined(_SCO_DS) && !defined(__EMX__)
22 # include <sys/syscall.h>
23 # endif
24 # include <sys/param.h>
25 #else
26 # include <syscall.h>
27 #endif
29 #include "debugger.h"
30 #include "options.h"
31 #include "sig_context.h"
32 #include "miscemu.h"
33 #include "thread.h"
36 extern void SIGNAL_SetHandler( int sig, void (*func)(), int flags );
37 extern BOOL32 INSTR_EmulateInstruction( SIGCONTEXT *context );
40 /**********************************************************************
41 * SIGNAL_break
43 * Handle Ctrl-C and such
45 static HANDLER_DEF(SIGNAL_break)
47 HANDLER_INIT();
48 if (Options.debug)
49 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
50 else exit(0);
54 /**********************************************************************
55 * SIGNAL_trap
57 * SIGTRAP handler.
59 static HANDLER_DEF(SIGNAL_trap)
61 HANDLER_INIT();
62 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
66 /**********************************************************************
67 * SIGNAL_fault
69 * Segfault handler.
71 static HANDLER_DEF(SIGNAL_fault)
73 WORD cs;
74 GET_CS(cs);
75 HANDLER_INIT();
76 if (CS_sig(HANDLER_CONTEXT) == cs)
78 fprintf( stderr, "Segmentation fault in 32-bit code (0x%08lx).\n",
79 EIP_sig(HANDLER_CONTEXT) );
81 else
83 if (INSTR_EmulateInstruction( HANDLER_CONTEXT )) return;
84 fprintf( stderr, "Segmentation fault in 16-bit code (%04x:%04lx).\n",
85 (WORD)CS_sig(HANDLER_CONTEXT), EIP_sig(HANDLER_CONTEXT) );
87 wine_debug( signal, HANDLER_CONTEXT );
91 /***********************************************************************
92 * SIGNAL_SetContext
94 * Set the register values from a sigcontext.
96 static void SIGNAL_SetSigContext( const SIGCONTEXT *sigcontext,
97 CONTEXT *context )
99 EAX_reg(context) = EAX_sig(sigcontext);
100 EBX_reg(context) = EBX_sig(sigcontext);
101 ECX_reg(context) = ECX_sig(sigcontext);
102 EDX_reg(context) = EDX_sig(sigcontext);
103 ESI_reg(context) = ESI_sig(sigcontext);
104 EDI_reg(context) = EDI_sig(sigcontext);
105 EBP_reg(context) = EBP_sig(sigcontext);
106 EFL_reg(context) = EFL_sig(sigcontext);
107 EIP_reg(context) = EIP_sig(sigcontext);
108 ESP_reg(context) = ESP_sig(sigcontext);
109 CS_reg(context) = LOWORD(CS_sig(sigcontext));
110 DS_reg(context) = LOWORD(DS_sig(sigcontext));
111 ES_reg(context) = LOWORD(ES_sig(sigcontext));
112 SS_reg(context) = LOWORD(SS_sig(sigcontext));
113 #ifdef FS_sig
114 FS_reg(context) = LOWORD(FS_sig(sigcontext));
115 #else
116 GET_FS( FS_reg(&DEBUG_context) );
117 FS_reg(context) &= 0xffff;
118 #endif
119 #ifdef GS_sig
120 GS_reg(context) = LOWORD(GS_sig(sigcontext));
121 #else
122 GET_GS( GS_reg(&DEBUG_context) );
123 GS_reg(context) &= 0xffff;
124 #endif
128 /***********************************************************************
129 * SIGNAL_GetSigContext
131 * Build a sigcontext from the register values.
133 static void SIGNAL_GetSigContext( SIGCONTEXT *sigcontext,
134 const CONTEXT *context )
136 EAX_sig(sigcontext) = EAX_reg(context);
137 EBX_sig(sigcontext) = EBX_reg(context);
138 ECX_sig(sigcontext) = ECX_reg(context);
139 EDX_sig(sigcontext) = EDX_reg(context);
140 ESI_sig(sigcontext) = ESI_reg(context);
141 EDI_sig(sigcontext) = EDI_reg(context);
142 EBP_sig(sigcontext) = EBP_reg(context);
143 EFL_sig(sigcontext) = EFL_reg(context);
144 EIP_sig(sigcontext) = EIP_reg(context);
145 ESP_sig(sigcontext) = ESP_reg(context);
146 CS_sig(sigcontext) = CS_reg(context);
147 DS_sig(sigcontext) = DS_reg(context);
148 ES_sig(sigcontext) = ES_reg(context);
149 SS_sig(sigcontext) = SS_reg(context);
150 #ifdef FS_sig
151 FS_sig(sigcontext) = FS_reg(context);
152 #else
153 SET_FS( FS_reg(&DEBUG_context) );
154 #endif
155 #ifdef GS_sig
156 GS_sig(sigcontext) = GS_reg(context);
157 #else
158 SET_GS( GS_reg(&DEBUG_context) );
159 #endif
163 /***********************************************************************
164 * SIGNAL_InfoRegisters
166 * Display registers information.
168 void SIGNAL_InfoRegisters( CONTEXT *context )
170 fprintf( stderr," CS:%04x SS:%04x DS:%04x ES:%04x FS:%04x GS:%04x",
171 (WORD)CS_reg(context), (WORD)SS_reg(context),
172 (WORD)DS_reg(context), (WORD)ES_reg(context),
173 (WORD)FS_reg(context), (WORD)GS_reg(context) );
174 fprintf( stderr, "\n EIP:%08lx ESP:%08lx EBP:%08lx EFLAGS:%08lx\n",
175 EIP_reg(context), ESP_reg(context),
176 EBP_reg(context), EFL_reg(context) );
177 fprintf( stderr, " EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n",
178 EAX_reg(context), EBX_reg(context),
179 ECX_reg(context), EDX_reg(context) );
180 fprintf( stderr, " ESI:%08lx EDI:%08lx\n",
181 ESI_reg(context), EDI_reg(context) );
185 /**********************************************************************
186 * SIGNAL_InitEmulator
188 * Initialize emulator signals.
190 BOOL32 SIGNAL_InitEmulator(void)
192 SIGNAL_SetHandler( SIGINT, (void (*)())SIGNAL_break, 1);
193 SIGNAL_SetHandler( SIGSEGV, (void (*)())SIGNAL_fault, 1);
194 SIGNAL_SetHandler( SIGILL, (void (*)())SIGNAL_fault, 1);
195 SIGNAL_SetHandler( SIGFPE, (void (*)())SIGNAL_fault, 1);
196 SIGNAL_SetHandler( SIGTRAP, (void (*)())SIGNAL_trap, 1); /* debugger */
197 SIGNAL_SetHandler( SIGHUP, (void (*)())SIGNAL_trap, 1); /* forced break*/
198 #ifdef SIGBUS
199 SIGNAL_SetHandler( SIGBUS, (void (*)())SIGNAL_fault, 1);
200 #endif
201 return TRUE;