source: trunk/src/kernel32/oslibexcept.cpp@ 4224

Last change on this file since 4224 was 4224, checked in by sandervl, 25 years ago

exception changes, implemented enumresourcelanguages + put back some old code

File size: 11.6 KB
Line 
1/* $Id: oslibexcept.cpp,v 1.3 2000-09-08 18:07:50 sandervl Exp $ */
2/*
3 * Exception handler util. procedures
4 *
5 * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
6 *
7 */
8#define INCL_BASE
9#define INCL_DOSEXCEPTIONS
10#define INCL_DOSMEMMGR
11#define INCL_DOSPROCESS
12#include <os2wrap.h> //Odin32 OS/2 api wrappers
13#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16#include <win32type.h>
17#include <misc.h>
18#include "oslibexcept.h"
19#include <exceptions.h>
20#include <wprocess.h>
21
22#define DBG_LOCALLOG DBG_oslibexcept
23#include "dbglocal.h"
24
25//******************************************************************************
26//Dispatches OS/2 exception to win32 handler
27//Returns: TRUE, win32 exception handler returned continue execution
28// FALSE, otherwise
29//******************************************************************************
30BOOL APIENTRY OSLibDispatchException(PEXCEPTIONREPORTRECORD pReportRec,
31 PEXCEPTIONREGISTRATIONRECORD pRegistrationRec,
32 PCONTEXTRECORD pContextRec, PVOID p)
33{
34 WINEXCEPTION_RECORD winreportrec;
35 WINCONTEXT wincontextrec;
36 ULONG rc;
37
38 memset(&winreportrec, 0, sizeof(winreportrec));
39 memcpy(&winreportrec, pReportRec, sizeof(*pReportRec));
40
41 switch(pReportRec->ExceptionNum) {
42 case XCPT_FLOAT_DENORMAL_OPERAND:
43 winreportrec.ExceptionCode = EXCEPTION_FLT_DENORMAL_OPERAND;
44 break;
45 case XCPT_FLOAT_DIVIDE_BY_ZERO:
46 winreportrec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
47 break;
48 case XCPT_FLOAT_INEXACT_RESULT:
49 winreportrec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
50 break;
51 case XCPT_FLOAT_INVALID_OPERATION:
52 winreportrec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
53 break;
54 case XCPT_FLOAT_OVERFLOW:
55 winreportrec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
56 break;
57 case XCPT_FLOAT_STACK_CHECK:
58 winreportrec.ExceptionCode = EXCEPTION_FLT_STACK_CHECK;
59 break;
60 case XCPT_FLOAT_UNDERFLOW:
61 winreportrec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
62 break;
63 case XCPT_INTEGER_DIVIDE_BY_ZERO:
64 winreportrec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
65 break;
66 case XCPT_INTEGER_OVERFLOW:
67 winreportrec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
68 break;
69 case XCPT_PRIVILEGED_INSTRUCTION:
70 winreportrec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
71 break;
72 case XCPT_BREAKPOINT:
73 winreportrec.ExceptionCode = EXCEPTION_BREAKPOINT;
74 break;
75 case XCPT_SINGLE_STEP:
76 winreportrec.ExceptionCode = EXCEPTION_SINGLE_STEP;
77 break;
78 case XCPT_ARRAY_BOUNDS_EXCEEDED:
79 winreportrec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
80 break;
81 case XCPT_DATATYPE_MISALIGNMENT:
82 winreportrec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
83 break;
84 case XCPT_ILLEGAL_INSTRUCTION:
85 winreportrec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
86 break;
87 case XCPT_INVALID_LOCK_SEQUENCE:
88 winreportrec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
89 break;
90 case XCPT_GUARD_PAGE_VIOLATION:
91 winreportrec.ExceptionCode = EXCEPTION_GUARD_PAGE;
92 break;
93 case XCPT_UNABLE_TO_GROW_STACK:
94 winreportrec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
95 break;
96 case XCPT_IN_PAGE_ERROR:
97 winreportrec.ExceptionCode = EXCEPTION_IN_PAGE_ERROR;
98 break;
99 case XCPT_ACCESS_VIOLATION:
100 winreportrec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
101 break;
102 default: //no other exceptions should be dispatched to win32 apps
103 return FALSE;
104 }
105 //TODO:
106 //According to the Wine folks the flags are the same in OS/2 and win32
107 //Let's assume for now the rest is identical as well
108
109 //copy context record info
110 memset(&wincontextrec, 0, sizeof(wincontextrec));
111 if(pContextRec->ContextFlags & CONTEXT_CONTROL) {
112 wincontextrec.ContextFlags |= WINCONTEXT_CONTROL;
113 wincontextrec.Ebp = pContextRec->ctx_RegEbp;
114 wincontextrec.Eip = pContextRec->ctx_RegEip;
115 wincontextrec.SegCs = pContextRec->ctx_SegCs;
116 wincontextrec.EFlags = pContextRec->ctx_EFlags;
117 wincontextrec.Esp = pContextRec->ctx_RegEsp;
118 wincontextrec.SegSs = pContextRec->ctx_SegSs;
119 }
120 if(pContextRec->ContextFlags & CONTEXT_INTEGER) {
121 wincontextrec.ContextFlags |= WINCONTEXT_INTEGER;
122 wincontextrec.Edi = pContextRec->ctx_RegEdi;
123 wincontextrec.Esi = pContextRec->ctx_RegEsi;
124 wincontextrec.Ebx = pContextRec->ctx_RegEbx;
125 wincontextrec.Edx = pContextRec->ctx_RegEdx;
126 wincontextrec.Ecx = pContextRec->ctx_RegEcx;
127 wincontextrec.Eax = pContextRec->ctx_RegEax;
128 }
129
130 TEB *winteb = GetThreadTEB();
131 THDB *thdb = (THDB *)(winteb+1);
132
133 if(pContextRec->ContextFlags & CONTEXT_SEGMENTS) {
134 wincontextrec.ContextFlags |= WINCONTEXT_SEGMENTS;
135 wincontextrec.SegGs = pContextRec->ctx_SegGs;
136// This resets FS to 0x150B - we DON'T want that!!
137// wincontextrec.SegFs = pContextRec->ctx_SegFs;
138 wincontextrec.SegFs = thdb->teb_sel;
139 wincontextrec.SegEs = pContextRec->ctx_SegEs;
140 wincontextrec.SegDs = pContextRec->ctx_SegDs;
141 }
142 if(pContextRec->ContextFlags & CONTEXT_FLOATING_POINT) {
143 wincontextrec.ContextFlags |= WINCONTEXT_FLOATING_POINT;
144 //TODO: First 7 dwords the same?
145 memcpy(&wincontextrec.FloatSave, pContextRec->ctx_env, sizeof(pContextRec->ctx_env));
146 memcpy(&wincontextrec.FloatSave.RegisterArea, pContextRec->ctx_stack, sizeof(pContextRec->ctx_stack));
147 }
148 //It doesn't seem correct if we dispatch real exceptions to win32 apps
149 //Some just call RtlUnwind and continue as if they were processing an
150 //exception thrown by C++ code. (instead of real OS exception)
151#if 1
152 // We need to reset FS to its original (Win32) value, otherwise we'll likely
153 // fuck up the Win32 exception handlers. They could end up using the wrong
154 // exception chain if they access FS:[0] directly.
155 DWORD oldsel = SetReturnFS(thdb->teb_sel);
156
157 switch(pReportRec->ExceptionNum) {
158 case XCPT_FLOAT_DENORMAL_OPERAND:
159 case XCPT_FLOAT_DIVIDE_BY_ZERO:
160 case XCPT_FLOAT_INEXACT_RESULT:
161 case XCPT_FLOAT_INVALID_OPERATION:
162 case XCPT_FLOAT_OVERFLOW:
163 case XCPT_FLOAT_STACK_CHECK:
164 case XCPT_FLOAT_UNDERFLOW:
165 rc = RtlDispatchException(&winreportrec, &wincontextrec);
166 break;
167
168 case XCPT_ACCESS_VIOLATION:
169 rc = RtlDispatchException(&winreportrec, &wincontextrec);
170 break;
171
172 case XCPT_INTEGER_DIVIDE_BY_ZERO:
173 case XCPT_INTEGER_OVERFLOW:
174 case XCPT_PRIVILEGED_INSTRUCTION:
175 case XCPT_BREAKPOINT:
176 case XCPT_SINGLE_STEP:
177 case XCPT_ARRAY_BOUNDS_EXCEEDED:
178 case XCPT_DATATYPE_MISALIGNMENT:
179 case XCPT_ILLEGAL_INSTRUCTION:
180 case XCPT_INVALID_LOCK_SEQUENCE:
181 case XCPT_GUARD_PAGE_VIOLATION:
182 case XCPT_UNABLE_TO_GROW_STACK:
183 case XCPT_IN_PAGE_ERROR:
184 SetFS(oldsel); //restore FS
185 return FALSE; //let's not dispatch those for now
186 }
187 SetFS(oldsel); //restore FS
188
189 if(rc == ExceptionContinueExecution) {
190 dprintf(("Win32 exception handler returned ExceptionContinueExecution"));
191 if(wincontextrec.ContextFlags & WINCONTEXT_CONTROL) {
192 pContextRec->ctx_RegEbp = wincontextrec.Ebp;
193 pContextRec->ctx_RegEip = wincontextrec.Eip;
194 pContextRec->ctx_SegCs = wincontextrec.SegCs;
195 pContextRec->ctx_EFlags = wincontextrec.EFlags;
196 pContextRec->ctx_RegEsp = wincontextrec.Esp;
197 pContextRec->ctx_SegSs = wincontextrec.SegSs;
198 }
199 if(wincontextrec.ContextFlags & WINCONTEXT_INTEGER) {
200 pContextRec->ctx_RegEdi = wincontextrec.Edi;
201 pContextRec->ctx_RegEsi = wincontextrec.Esi;
202 pContextRec->ctx_RegEbx = wincontextrec.Ebx;
203 pContextRec->ctx_RegEdx = wincontextrec.Edx;
204 pContextRec->ctx_RegEcx = wincontextrec.Ecx;
205 pContextRec->ctx_RegEax = wincontextrec.Eax;
206 }
207#if 0
208 //This is not a good idea
209 if(wincontextrec.ContextFlags & WINCONTEXT_SEGMENTS) {
210 pContextRec->ctx_SegGs = wincontextrec.SegGs;
211 pContextRec->ctx_SegFs = wincontextrec.SegFs;
212 pContextRec->ctx_SegEs = wincontextrec.SegEs;
213 pContextRec->ctx_SegDs = wincontextrec.SegDs;
214 }
215#endif
216 if(wincontextrec.ContextFlags & WINCONTEXT_FLOATING_POINT) {
217 //TODO: First 7 dwords the same?
218 memcpy(pContextRec->ctx_env, &wincontextrec.FloatSave, sizeof(pContextRec->ctx_env));
219 memcpy(pContextRec->ctx_stack, &wincontextrec.FloatSave.RegisterArea, sizeof(pContextRec->ctx_stack));
220 }
221 if (pContextRec->ContextFlags & CONTEXT_CONTROL) /* check flags */
222 dprintf((" SS:ESP=%04x:%08x EFLAGS=%08x\n",
223 pContextRec->ctx_SegSs,
224 pContextRec->ctx_RegEsp,
225 pContextRec->ctx_EFlags));
226 dprintf((" CS:EIP=%04x:%08x EBP =%08x\n",
227 pContextRec->ctx_SegCs,
228 pContextRec->ctx_RegEip,
229 pContextRec->ctx_RegEbp));
230
231 if (pContextRec->ContextFlags & CONTEXT_INTEGER) /* check flags */
232 dprintf((" EAX=%08x EBX=%08x ESI=%08x\n",
233 pContextRec->ctx_RegEax,
234 pContextRec->ctx_RegEbx,
235 pContextRec->ctx_RegEsi));
236 dprintf((" ECX=%08x EDX=%08x EDI=%08x\n",
237 pContextRec->ctx_RegEcx,
238 pContextRec->ctx_RegEdx,
239 pContextRec->ctx_RegEdi));
240
241 if (pContextRec->ContextFlags & CONTEXT_SEGMENTS) /* check flags */
242 dprintf((" DS=%04x ES=%08x"
243 " FS=%04x GS=%04x\n",
244 pContextRec->ctx_SegDs,
245 pContextRec->ctx_SegEs,
246 pContextRec->ctx_SegFs,
247 pContextRec->ctx_SegGs));
248
249 if (pContextRec->ContextFlags & CONTEXT_FLOATING_POINT) /* check flags */
250 {
251 ULONG ulCounter; /* temporary local counter for fp stack */
252
253 dprintf((" Env[0]=%08x Env[1]=%08x Env[2]=%08x Env[3]=%08x\n",
254 pContextRec->ctx_env[0],
255 pContextRec->ctx_env[1],
256 pContextRec->ctx_env[2],
257 pContextRec->ctx_env[3]));
258
259 dprintf((" Env[4]=%08x Env[5]=%08x Env[6]=%08x\n",
260 pContextRec->ctx_env[4],
261 pContextRec->ctx_env[5],
262 pContextRec->ctx_env[6]));
263
264 for (ulCounter = 0;
265 ulCounter < 8; /* see TOOLKIT\INCLUDE\BSEEXPT.H, _CONTEXT structure */
266 ulCounter ++)
267 dprintf((" FP-Stack[%u] losig=%08x hisig=%08x signexp=%04x\n",
268 ulCounter,
269 pContextRec->ctx_stack[0].losig,
270 pContextRec->ctx_stack[0].hisig,
271 pContextRec->ctx_stack[0].signexp));
272 }
273
274 return TRUE;
275 }
276 dprintf(("Win32 exception handler returned %x", rc));
277#endif
278 return FALSE;
279}
280//******************************************************************************
281//******************************************************************************
Note: See TracBrowser for help on using the repository browser.