Release 980104
[wine/multimedia.git] / if1632 / signal.c
blobd1df7c56247a2d7451bb5063e4a63643386eb2e2
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 if (Options.debug)
48 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
49 else exit(0);
53 /**********************************************************************
54 * SIGNAL_trap
56 * SIGTRAP handler.
58 static HANDLER_DEF(SIGNAL_trap)
60 wine_debug( signal, HANDLER_CONTEXT ); /* Enter our debugger */
64 /**********************************************************************
65 * SIGNAL_fault
67 * Segfault handler.
69 static HANDLER_DEF(SIGNAL_fault)
71 WORD cs;
72 GET_CS(cs);
73 if (CS_sig(HANDLER_CONTEXT) == cs)
75 fprintf( stderr, "Segmentation fault in 32-bit code (0x%08lx).\n",
76 EIP_sig(HANDLER_CONTEXT) );
78 else
80 if (INSTR_EmulateInstruction( HANDLER_CONTEXT )) return;
81 fprintf( stderr, "Segmentation fault in 16-bit code (%04x:%04lx).\n",
82 (WORD)CS_sig(HANDLER_CONTEXT), EIP_sig(HANDLER_CONTEXT) );
84 wine_debug( signal, HANDLER_CONTEXT );
88 /***********************************************************************
89 * SIGNAL_SetContext
91 * Set the register values from a sigcontext.
93 static void SIGNAL_SetSigContext( const SIGCONTEXT *sigcontext,
94 CONTEXT *context )
96 EAX_reg(context) = EAX_sig(sigcontext);
97 EBX_reg(context) = EBX_sig(sigcontext);
98 ECX_reg(context) = ECX_sig(sigcontext);
99 EDX_reg(context) = EDX_sig(sigcontext);
100 ESI_reg(context) = ESI_sig(sigcontext);
101 EDI_reg(context) = EDI_sig(sigcontext);
102 EBP_reg(context) = EBP_sig(sigcontext);
103 EFL_reg(context) = EFL_sig(sigcontext);
104 EIP_reg(context) = EIP_sig(sigcontext);
105 ESP_reg(context) = ESP_sig(sigcontext);
106 CS_reg(context) = LOWORD(CS_sig(sigcontext));
107 DS_reg(context) = LOWORD(DS_sig(sigcontext));
108 ES_reg(context) = LOWORD(ES_sig(sigcontext));
109 SS_reg(context) = LOWORD(SS_sig(sigcontext));
110 #ifdef FS_sig
111 FS_reg(context) = LOWORD(FS_sig(sigcontext));
112 #else
113 GET_FS( FS_reg(&DEBUG_context) );
114 FS_reg(context) &= 0xffff;
115 #endif
116 #ifdef GS_sig
117 GS_reg(context) = LOWORD(GS_sig(sigcontext));
118 #else
119 GET_GS( GS_reg(&DEBUG_context) );
120 GS_reg(context) &= 0xffff;
121 #endif
125 /***********************************************************************
126 * SIGNAL_GetSigContext
128 * Build a sigcontext from the register values.
130 static void SIGNAL_GetSigContext( SIGCONTEXT *sigcontext,
131 const CONTEXT *context )
133 EAX_sig(sigcontext) = EAX_reg(context);
134 EBX_sig(sigcontext) = EBX_reg(context);
135 ECX_sig(sigcontext) = ECX_reg(context);
136 EDX_sig(sigcontext) = EDX_reg(context);
137 ESI_sig(sigcontext) = ESI_reg(context);
138 EDI_sig(sigcontext) = EDI_reg(context);
139 EBP_sig(sigcontext) = EBP_reg(context);
140 EFL_sig(sigcontext) = EFL_reg(context);
141 EIP_sig(sigcontext) = EIP_reg(context);
142 ESP_sig(sigcontext) = ESP_reg(context);
143 CS_sig(sigcontext) = CS_reg(context);
144 DS_sig(sigcontext) = DS_reg(context);
145 ES_sig(sigcontext) = ES_reg(context);
146 SS_sig(sigcontext) = SS_reg(context);
147 #ifdef FS_sig
148 FS_sig(sigcontext) = FS_reg(context);
149 #else
150 SET_FS( FS_reg(&DEBUG_context) );
151 #endif
152 #ifdef GS_sig
153 GS_sig(sigcontext) = GS_reg(context);
154 #else
155 SET_GS( GS_reg(&DEBUG_context) );
156 #endif
160 /***********************************************************************
161 * SIGNAL_InfoRegisters
163 * Display registers information.
165 void SIGNAL_InfoRegisters( CONTEXT *context )
167 fprintf( stderr," CS:%04x SS:%04x DS:%04x ES:%04x FS:%04x GS:%04x",
168 (WORD)CS_reg(context), (WORD)SS_reg(context),
169 (WORD)DS_reg(context), (WORD)ES_reg(context),
170 (WORD)FS_reg(context), (WORD)GS_reg(context) );
171 fprintf( stderr, "\n EIP:%08lx ESP:%08lx EBP:%08lx EFLAGS:%08lx\n",
172 EIP_reg(context), ESP_reg(context),
173 EBP_reg(context), EFL_reg(context) );
174 fprintf( stderr, " EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n",
175 EAX_reg(context), EBX_reg(context),
176 ECX_reg(context), EDX_reg(context) );
177 fprintf( stderr, " ESI:%08lx EDI:%08lx\n",
178 ESI_reg(context), EDI_reg(context) );
182 /**********************************************************************
183 * SIGNAL_InitEmulator
185 * Initialize emulator signals.
187 BOOL32 SIGNAL_InitEmulator(void)
189 SIGNAL_SetHandler( SIGINT, (void (*)())SIGNAL_break, 1);
190 SIGNAL_SetHandler( SIGSEGV, (void (*)())SIGNAL_fault, 1);
191 SIGNAL_SetHandler( SIGILL, (void (*)())SIGNAL_fault, 1);
192 SIGNAL_SetHandler( SIGFPE, (void (*)())SIGNAL_fault, 1);
193 SIGNAL_SetHandler( SIGTRAP, (void (*)())SIGNAL_trap, 1); /* debugger */
194 SIGNAL_SetHandler( SIGHUP, (void (*)())SIGNAL_trap, 1); /* forced break*/
195 #ifdef SIGBUS
196 SIGNAL_SetHandler( SIGBUS, (void (*)())SIGNAL_fault, 1);
197 #endif
198 return TRUE;