- Merge from trunk up to r45543
[reactos.git] / dll / win32 / kernel32 / except / except.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/misc/except.c
6 * PURPOSE: Exception functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * modified from WINE [ Onno Hovers, (onno@stack.urc.tue.nl) ]
9 * UPDATE HISTORY:
10 * Created 01/11/98
11 */
12
13 #include <k32.h>
14
15 #define NDEBUG
16 #include <debug.h>
17
18 LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter = NULL;
19
20 UINT
21 WINAPI
22 GetErrorMode(VOID)
23 {
24 NTSTATUS Status;
25 UINT ErrMode;
26
27 /* Query the current setting */
28 Status = NtQueryInformationProcess(NtCurrentProcess(),
29 ProcessDefaultHardErrorMode,
30 (PVOID)&ErrMode,
31 sizeof(ErrMode),
32 NULL);
33 if (!NT_SUCCESS(Status))
34 {
35 /* Fail if we couldn't query */
36 SetLastErrorByStatus(Status);
37 return 0;
38 }
39
40 /* Check if NOT failing critical errors was requested */
41 if (ErrMode & SEM_FAILCRITICALERRORS)
42 {
43 /* Mask it out, since the native API works differently */
44 ErrMode &= ~SEM_FAILCRITICALERRORS;
45 }
46 else
47 {
48 /* OR it if the caller didn't, due to different native semantics */
49 ErrMode |= SEM_FAILCRITICALERRORS;
50 }
51
52 /* Return the mode */
53 return ErrMode;
54 }
55
56 /*
57 * @implemented
58 */
59 UINT
60 WINAPI
61 SetErrorMode(IN UINT uMode)
62 {
63 UINT PrevErrMode, NewMode;
64 NTSTATUS Status;
65
66 /* Get the previous mode */
67 PrevErrMode = GetErrorMode();
68 NewMode = uMode;
69
70 /* Check if failing critical errors was requested */
71 if (NewMode & SEM_FAILCRITICALERRORS)
72 {
73 /* Mask it out, since the native API works differently */
74 NewMode &= ~SEM_FAILCRITICALERRORS;
75 }
76 else
77 {
78 /* OR it if the caller didn't, due to different native semantics */
79 NewMode |= SEM_FAILCRITICALERRORS;
80 }
81
82 /* Set the new mode */
83 Status = NtSetInformationProcess(NtCurrentProcess(),
84 ProcessDefaultHardErrorMode,
85 (PVOID)&NewMode,
86 sizeof(NewMode));
87 if(!NT_SUCCESS(Status)) SetLastErrorByStatus(Status);
88
89 /* Return the previous mode */
90 return PrevErrMode;
91 }
92
93 /*
94 * @implemented
95 */
96 LPTOP_LEVEL_EXCEPTION_FILTER
97 WINAPI
98 SetUnhandledExceptionFilter(
99 IN LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
100 {
101 return InterlockedExchangePointer(&GlobalTopLevelExceptionFilter,
102 lpTopLevelExceptionFilter);
103 }
104
105 /*
106 * Private helper function to lookup the module name from a given address.
107 * The address can point to anywhere within the module.
108 */
109 static const char*
110 _module_name_from_addr(const void* addr, void **module_start_addr,
111 char* psz, size_t nChars)
112 {
113 MEMORY_BASIC_INFORMATION mbi;
114 if (VirtualQuery(addr, &mbi, sizeof(mbi)) != sizeof(mbi) ||
115 !GetModuleFileNameA((HMODULE)mbi.AllocationBase, psz, nChars))
116 {
117 psz[0] = '\0';
118 *module_start_addr = 0;
119 }
120 else
121 {
122 *module_start_addr = (void *)mbi.AllocationBase;
123 }
124 return psz;
125 }
126
127 #ifdef _M_IX86
128 static VOID
129 _dump_context(PCONTEXT pc)
130 {
131 /*
132 * Print out the CPU registers
133 */
134 DbgPrint("CS:EIP %x:%x\n", pc->SegCs&0xffff, pc->Eip );
135 DbgPrint("DS %x ES %x FS %x GS %x\n", pc->SegDs&0xffff, pc->SegEs&0xffff,
136 pc->SegFs&0xffff, pc->SegGs&0xfff);
137 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", pc->Eax, pc->Ebx, pc->Ecx);
138 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", pc->Edx,
139 pc->Ebp, pc->Esi, pc->Esp);
140 DbgPrint("EDI: %.8x EFLAGS: %.8x\n", pc->Edi, pc->EFlags);
141 }
142 #else
143 #warning Unknown architecture
144 static VOID
145 _dump_context(PCONTEXT pc)
146 {
147 }
148 #endif
149
150 static LONG
151 BasepCheckForReadOnlyResource(IN PVOID Ptr)
152 {
153 PVOID Data;
154 ULONG Size, OldProtect;
155 MEMORY_BASIC_INFORMATION mbi;
156 NTSTATUS Status;
157 LONG Ret = EXCEPTION_CONTINUE_SEARCH;
158
159 /* Check if it was an attempt to write to a read-only image section! */
160 Status = NtQueryVirtualMemory(NtCurrentProcess(),
161 Ptr,
162 MemoryBasicInformation,
163 &mbi,
164 sizeof(mbi),
165 NULL);
166 if (NT_SUCCESS(Status) &&
167 mbi.Protect == PAGE_READONLY && mbi.Type == MEM_IMAGE)
168 {
169 /* Attempt to treat it as a resource section. We need to
170 use SEH here because we don't know if it's actually a
171 resource mapping */
172
173 _SEH2_TRY
174 {
175 Data = RtlImageDirectoryEntryToData(mbi.AllocationBase,
176 TRUE,
177 IMAGE_DIRECTORY_ENTRY_RESOURCE,
178 &Size);
179
180 if (Data != NULL &&
181 (ULONG_PTR)Ptr >= (ULONG_PTR)Data &&
182 (ULONG_PTR)Ptr < (ULONG_PTR)Data + Size)
183 {
184 /* The user tried to write into the resources. Make the page
185 writable... */
186 Size = 1;
187 Status = NtProtectVirtualMemory(NtCurrentProcess(),
188 &Ptr,
189 &Size,
190 PAGE_READWRITE,
191 &OldProtect);
192 if (NT_SUCCESS(Status))
193 {
194 Ret = EXCEPTION_CONTINUE_EXECUTION;
195 }
196 }
197 }
198 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
199 {
200 }
201 _SEH2_END;
202 }
203
204 return Ret;
205 }
206
207 /*
208 * @implemented
209 */
210 LONG WINAPI
211 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo)
212 {
213 LONG RetValue;
214 HANDLE DebugPort = NULL;
215 NTSTATUS ErrCode;
216 ULONG ErrorParameters[4];
217 ULONG ErrorResponse;
218
219 if ((NTSTATUS)ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
220 ExceptionInfo->ExceptionRecord->NumberParameters >= 2)
221 {
222 switch(ExceptionInfo->ExceptionRecord->ExceptionInformation[0])
223 {
224 case EXCEPTION_WRITE_FAULT:
225 /* Change the protection on some write attempts, some InstallShield setups
226 have this bug */
227 RetValue = BasepCheckForReadOnlyResource(
228 (PVOID)ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
229 if (RetValue == EXCEPTION_CONTINUE_EXECUTION)
230 return EXCEPTION_CONTINUE_EXECUTION;
231 break;
232 case EXCEPTION_EXECUTE_FAULT:
233 /* FIXME */
234 break;
235 }
236 }
237
238 /* Is there a debugger running ? */
239 ErrCode = NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort,
240 &DebugPort, sizeof(HANDLE), NULL);
241 if (!NT_SUCCESS(ErrCode) && ErrCode != STATUS_NOT_IMPLEMENTED)
242 {
243 SetLastErrorByStatus(ErrCode);
244 return EXCEPTION_EXECUTE_HANDLER;
245 }
246
247 if (DebugPort)
248 {
249 /* Pass the exception to debugger. */
250 DPRINT("Passing exception to debugger\n");
251 return EXCEPTION_CONTINUE_SEARCH;
252 }
253
254 if (GlobalTopLevelExceptionFilter)
255 {
256 LONG ret = GlobalTopLevelExceptionFilter( ExceptionInfo );
257 if (ret != EXCEPTION_CONTINUE_SEARCH)
258 return ret;
259 }
260
261 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX) == 0)
262 {
263 #ifdef _X86_
264 PULONG Frame;
265 #endif
266 PVOID StartAddr;
267 CHAR szMod[128] = "";
268
269 /* Print a stack trace. */
270 DbgPrint("Unhandled exception\n");
271 DbgPrint("ExceptionCode: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionCode);
272 if ((NTSTATUS)ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
273 ExceptionInfo->ExceptionRecord->NumberParameters == 2)
274 {
275 DbgPrint("Faulting Address: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
276 }
277 _dump_context ( ExceptionInfo->ContextRecord );
278 _module_name_from_addr(ExceptionInfo->ExceptionRecord->ExceptionAddress, &StartAddr, szMod, sizeof(szMod));
279 DbgPrint("Address:\n %8x+%-8x %s\n",
280 (PVOID)StartAddr, (ULONG_PTR)ExceptionInfo->ExceptionRecord->ExceptionAddress -
281 (ULONG_PTR)StartAddr, szMod);
282
283 #ifdef _X86_
284 DbgPrint("Frames:\n");
285 _SEH2_TRY
286 {
287 Frame = (PULONG)ExceptionInfo->ContextRecord->Ebp;
288 while (Frame[1] != 0 && Frame[1] != 0xdeadbeef)
289 {
290 if (IsBadReadPtr((PVOID)Frame[1], 4)) {
291 DbgPrint(" %8x%9s %s\n", Frame[1], "<invalid address>"," ");
292 } else {
293 _module_name_from_addr((const void*)Frame[1], &StartAddr,
294 szMod, sizeof(szMod));
295 DbgPrint(" %8x+%-8x %s\n",
296 (PVOID)StartAddr,
297 (ULONG_PTR)Frame[1] - (ULONG_PTR)StartAddr, szMod);
298 }
299 if (IsBadReadPtr((PVOID)Frame[0], sizeof(*Frame) * 2)) {
300 break;
301 }
302 Frame = (PULONG)Frame[0];
303 }
304 }
305 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
306 {
307 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH2_GetExceptionCode());
308 }
309 _SEH2_END;
310 #endif
311 }
312
313 /* Save exception code and address */
314 ErrorParameters[0] = (ULONG)ExceptionInfo->ExceptionRecord->ExceptionCode;
315 ErrorParameters[1] = (ULONG)ExceptionInfo->ExceptionRecord->ExceptionAddress;
316
317 if ((NTSTATUS)ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION)
318 {
319 /* get the type of operation that caused the access violation */
320 ErrorParameters[2] = ExceptionInfo->ExceptionRecord->ExceptionInformation[0];
321 }
322 else
323 {
324 ErrorParameters[2] = ExceptionInfo->ExceptionRecord->ExceptionInformation[2];
325 }
326
327 /* Save faulting address */
328 ErrorParameters[3] = ExceptionInfo->ExceptionRecord->ExceptionInformation[1];
329
330 /* Raise the harderror */
331 ErrCode = NtRaiseHardError(STATUS_UNHANDLED_EXCEPTION | 0x10000000,
332 4, 0, ErrorParameters, OptionOkCancel, &ErrorResponse);
333
334 if (NT_SUCCESS(ErrCode) && (ErrorResponse == ResponseCancel))
335 {
336 /* FIXME: Check the result, if the "Cancel" button was
337 clicked run a debugger */
338 DPRINT1("Debugging is not implemented yet\n");
339 }
340
341 /*
342 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
343 * the __except block will be executed. Normally this will end up in a
344 * Terminate process.
345 */
346
347 return EXCEPTION_EXECUTE_HANDLER;
348 }
349
350 /*
351 * @implemented
352 */
353 VOID
354 WINAPI
355 RaiseException(IN DWORD dwExceptionCode,
356 IN DWORD dwExceptionFlags,
357 IN DWORD nNumberOfArguments,
358 IN CONST ULONG_PTR *lpArguments OPTIONAL)
359 {
360 EXCEPTION_RECORD ExceptionRecord;
361
362 /* Setup the exception record */
363 ExceptionRecord.ExceptionCode = dwExceptionCode;
364 ExceptionRecord.ExceptionRecord = NULL;
365 ExceptionRecord.ExceptionAddress = (PVOID)RaiseException;
366 ExceptionRecord.ExceptionFlags = dwExceptionFlags & EXCEPTION_NONCONTINUABLE;
367
368 /* Check if we have arguments */
369 if (!lpArguments)
370 {
371 /* We don't */
372 ExceptionRecord.NumberParameters = 0;
373 }
374 else
375 {
376 /* We do, normalize the count */
377 if (nNumberOfArguments > EXCEPTION_MAXIMUM_PARAMETERS)
378 {
379 nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
380 }
381
382 /* Set the count of parameters */
383 ExceptionRecord.NumberParameters = nNumberOfArguments;
384
385 /* Loop each parameter */
386 for (nNumberOfArguments = 0;
387 (nNumberOfArguments < ExceptionRecord.NumberParameters);
388 nNumberOfArguments ++)
389 {
390 /* Copy the exception information */
391 ExceptionRecord.ExceptionInformation[nNumberOfArguments] =
392 *lpArguments++;
393 }
394 }
395
396 if (dwExceptionCode == 0xeedface)
397 {
398 DPRINT1("Delphi Exception at address: %p\n", ExceptionRecord.ExceptionInformation[0]);
399 DPRINT1("Exception-Object: %p\n", ExceptionRecord.ExceptionInformation[1]);
400 DPRINT1("Exception text: %s\n", ExceptionRecord.ExceptionInformation[2]);
401 }
402
403 /* Trace the wine special error and show the modulename and functionname */
404 if (dwExceptionCode == 0x80000100 /*EXCEPTION_WINE_STUB*/)
405 {
406 /* Numbers of parameter must be equal to two */
407 if (ExceptionRecord.NumberParameters == 2)
408 {
409 DPRINT1("Missing function in : %s\n", ExceptionRecord.ExceptionInformation[0]);
410 DPRINT1("with the functionname : %s\n", ExceptionRecord.ExceptionInformation[1]);
411 }
412 }
413
414 /* Raise the exception */
415 RtlRaiseException(&ExceptionRecord);
416 }
417
418 /* EOF */