- Revert 49927 "Update to trunk" as it breaks KsStudio (again)
[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
128 static VOID
129 _dump_context(PCONTEXT pc)
130 {
131 #ifdef _M_IX86
132 /*
133 * Print out the CPU registers
134 */
135 DbgPrint("CS:EIP %x:%x\n", pc->SegCs&0xffff, pc->Eip );
136 DbgPrint("DS %x ES %x FS %x GS %x\n", pc->SegDs&0xffff, pc->SegEs&0xffff,
137 pc->SegFs&0xffff, pc->SegGs&0xfff);
138 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", pc->Eax, pc->Ebx, pc->Ecx);
139 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", pc->Edx,
140 pc->Ebp, pc->Esi, pc->Esp);
141 DbgPrint("EDI: %.8x EFLAGS: %.8x\n", pc->Edi, pc->EFlags);
142 #elif defined(_M_AMD64)
143 DbgPrint("CS:RIP %x:%I64x\n", pc->SegCs&0xffff, pc->Rip );
144 DbgPrint("DS %x ES %x FS %x GS %x\n", pc->SegDs&0xffff, pc->SegEs&0xffff,
145 pc->SegFs&0xffff, pc->SegGs&0xfff);
146 DbgPrint("RAX: %I64x RBX: %I64x RCX: %I64x RDI: %I64x\n", pc->Rax, pc->Rbx, pc->Rcx, pc->Rdi);
147 DbgPrint("RDX: %I64x RBP: %I64x RSI: %I64x RSP: %I64x\n", pc->Rdx, pc->Rbp, pc->Rsi, pc->Rsp);
148 DbgPrint("R8: %I64x R9: %I64x R10: %I64x R11: %I64x\n", pc->R8, pc->R9, pc->R10, pc->R11);
149 DbgPrint("R12: %I64x R13: %I64x R14: %I64x R15: %I64x\n", pc->R12, pc->R13, pc->R14, pc->R15);
150 DbgPrint("EFLAGS: %.8x\n", pc->EFlags);
151 #else
152 #warning Unknown architecture
153 #endif
154 }
155
156 static LONG
157 BasepCheckForReadOnlyResource(IN PVOID Ptr)
158 {
159 PVOID Data;
160 ULONG Size, OldProtect;
161 MEMORY_BASIC_INFORMATION mbi;
162 NTSTATUS Status;
163 LONG Ret = EXCEPTION_CONTINUE_SEARCH;
164
165 /* Check if it was an attempt to write to a read-only image section! */
166 Status = NtQueryVirtualMemory(NtCurrentProcess(),
167 Ptr,
168 MemoryBasicInformation,
169 &mbi,
170 sizeof(mbi),
171 NULL);
172 if (NT_SUCCESS(Status) &&
173 mbi.Protect == PAGE_READONLY && mbi.Type == MEM_IMAGE)
174 {
175 /* Attempt to treat it as a resource section. We need to
176 use SEH here because we don't know if it's actually a
177 resource mapping */
178
179 _SEH2_TRY
180 {
181 Data = RtlImageDirectoryEntryToData(mbi.AllocationBase,
182 TRUE,
183 IMAGE_DIRECTORY_ENTRY_RESOURCE,
184 &Size);
185
186 if (Data != NULL &&
187 (ULONG_PTR)Ptr >= (ULONG_PTR)Data &&
188 (ULONG_PTR)Ptr < (ULONG_PTR)Data + Size)
189 {
190 /* The user tried to write into the resources. Make the page
191 writable... */
192 Size = 1;
193 Status = NtProtectVirtualMemory(NtCurrentProcess(),
194 &Ptr,
195 &Size,
196 PAGE_READWRITE,
197 &OldProtect);
198 if (NT_SUCCESS(Status))
199 {
200 Ret = EXCEPTION_CONTINUE_EXECUTION;
201 }
202 }
203 }
204 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
205 {
206 }
207 _SEH2_END;
208 }
209
210 return Ret;
211 }
212
213 static VOID
214 PrintStackTrace(struct _EXCEPTION_POINTERS *ExceptionInfo)
215 {
216 PVOID StartAddr;
217 CHAR szMod[128] = "";
218 PEXCEPTION_RECORD ExceptionRecord = ExceptionInfo->ExceptionRecord;
219 PCONTEXT ContextRecord = ExceptionInfo->ContextRecord;
220
221 /* Print a stack trace. */
222 DbgPrint("Unhandled exception\n");
223 DbgPrint("ExceptionCode: %8x\n", ExceptionRecord->ExceptionCode);
224
225 if ((NTSTATUS)ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
226 ExceptionRecord->NumberParameters == 2)
227 {
228 DbgPrint("Faulting Address: %8x\n", ExceptionRecord->ExceptionInformation[1]);
229 }
230
231 _dump_context (ContextRecord);
232 _module_name_from_addr(ExceptionRecord->ExceptionAddress, &StartAddr, szMod, sizeof(szMod));
233 DbgPrint("Address:\n %8x+%-8x %s\n",
234 (PVOID)StartAddr,
235 (ULONG_PTR)ExceptionRecord->ExceptionAddress - (ULONG_PTR)StartAddr,
236 szMod);
237 #ifdef _M_IX86
238 DbgPrint("Frames:\n");
239
240 _SEH2_TRY
241 {
242 UINT i;
243 PULONG Frame = (PULONG)ContextRecord->Ebp;
244
245 for (i = 0; Frame[1] != 0 && Frame[1] != 0xdeadbeef && i < 128; i++)
246 {
247 if (IsBadReadPtr((PVOID)Frame[1], 4))
248 {
249 DbgPrint(" %8x%9s %s\n", Frame[1], "<invalid address>"," ");
250 }
251 else
252 {
253 _module_name_from_addr((const void*)Frame[1], &StartAddr,
254 szMod, sizeof(szMod));
255 DbgPrint(" %8x+%-8x %s\n",
256 (PVOID)StartAddr,
257 (ULONG_PTR)Frame[1] - (ULONG_PTR)StartAddr,
258 szMod);
259 }
260
261 if (IsBadReadPtr((PVOID)Frame[0], sizeof(*Frame) * 2))
262 break;
263
264 Frame = (PULONG)Frame[0];
265 }
266 }
267 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
268 {
269 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH2_GetExceptionCode());
270 }
271 _SEH2_END;
272 #endif
273 }
274
275 /*
276 * @implemented
277 */
278 LONG WINAPI
279 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo)
280 {
281 LONG RetValue;
282 HANDLE DebugPort = NULL;
283 NTSTATUS ErrCode;
284 ULONG_PTR ErrorParameters[4];
285 ULONG ErrorResponse;
286 PEXCEPTION_RECORD ExceptionRecord = ExceptionInfo->ExceptionRecord;
287
288 if ((NTSTATUS)ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
289 ExceptionRecord->NumberParameters >= 2)
290 {
291 switch(ExceptionRecord->ExceptionInformation[0])
292 {
293 case EXCEPTION_WRITE_FAULT:
294 /* Change the protection on some write attempts, some InstallShield setups
295 have this bug */
296 RetValue = BasepCheckForReadOnlyResource(
297 (PVOID)ExceptionRecord->ExceptionInformation[1]);
298 if (RetValue == EXCEPTION_CONTINUE_EXECUTION)
299 return EXCEPTION_CONTINUE_EXECUTION;
300 break;
301 case EXCEPTION_EXECUTE_FAULT:
302 /* FIXME */
303 break;
304 }
305 }
306
307 /* Is there a debugger running ? */
308 ErrCode = NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort,
309 &DebugPort, sizeof(HANDLE), NULL);
310 if (!NT_SUCCESS(ErrCode) && ErrCode != STATUS_NOT_IMPLEMENTED)
311 {
312 SetLastErrorByStatus(ErrCode);
313 return EXCEPTION_EXECUTE_HANDLER;
314 }
315
316 if (DebugPort)
317 {
318 /* Pass the exception to debugger. */
319 DPRINT("Passing exception to debugger\n");
320 return EXCEPTION_CONTINUE_SEARCH;
321 }
322
323 if (GlobalTopLevelExceptionFilter)
324 {
325 LONG ret = GlobalTopLevelExceptionFilter(ExceptionInfo);
326 if (ret != EXCEPTION_CONTINUE_SEARCH)
327 return ret;
328 }
329
330 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX) == 0)
331 PrintStackTrace(ExceptionInfo);
332
333 /* Save exception code and address */
334 ErrorParameters[0] = (ULONG)ExceptionRecord->ExceptionCode;
335 ErrorParameters[1] = (ULONG_PTR)ExceptionRecord->ExceptionAddress;
336
337 if ((NTSTATUS)ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION)
338 {
339 /* get the type of operation that caused the access violation */
340 ErrorParameters[2] = ExceptionRecord->ExceptionInformation[0];
341 }
342 else
343 {
344 ErrorParameters[2] = ExceptionRecord->ExceptionInformation[2];
345 }
346
347 /* Save faulting address */
348 ErrorParameters[3] = ExceptionRecord->ExceptionInformation[1];
349
350 /* Raise the harderror */
351 ErrCode = NtRaiseHardError(STATUS_UNHANDLED_EXCEPTION | 0x10000000,
352 4, 0, ErrorParameters, OptionOkCancel, &ErrorResponse);
353
354 if (NT_SUCCESS(ErrCode) && (ErrorResponse == ResponseCancel))
355 {
356 /* FIXME: Check the result, if the "Cancel" button was
357 clicked run a debugger */
358 DPRINT1("Debugging is not implemented yet\n");
359 }
360
361 /*
362 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
363 * the __except block will be executed. Normally this will end up in a
364 * Terminate process.
365 */
366
367 return EXCEPTION_EXECUTE_HANDLER;
368 }
369
370 /*
371 * @implemented
372 */
373 VOID
374 WINAPI
375 RaiseException(IN DWORD dwExceptionCode,
376 IN DWORD dwExceptionFlags,
377 IN DWORD nNumberOfArguments,
378 IN CONST ULONG_PTR *lpArguments OPTIONAL)
379 {
380 EXCEPTION_RECORD ExceptionRecord;
381
382 /* Setup the exception record */
383 ExceptionRecord.ExceptionCode = dwExceptionCode;
384 ExceptionRecord.ExceptionRecord = NULL;
385 ExceptionRecord.ExceptionAddress = (PVOID)RaiseException;
386 ExceptionRecord.ExceptionFlags = dwExceptionFlags & EXCEPTION_NONCONTINUABLE;
387
388 /* Check if we have arguments */
389 if (!lpArguments)
390 {
391 /* We don't */
392 ExceptionRecord.NumberParameters = 0;
393 }
394 else
395 {
396 /* We do, normalize the count */
397 if (nNumberOfArguments > EXCEPTION_MAXIMUM_PARAMETERS)
398 {
399 nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
400 }
401
402 /* Set the count of parameters */
403 ExceptionRecord.NumberParameters = nNumberOfArguments;
404
405 /* Loop each parameter */
406 for (nNumberOfArguments = 0;
407 (nNumberOfArguments < ExceptionRecord.NumberParameters);
408 nNumberOfArguments ++)
409 {
410 /* Copy the exception information */
411 ExceptionRecord.ExceptionInformation[nNumberOfArguments] =
412 *lpArguments++;
413 }
414 }
415
416 if (dwExceptionCode == 0xeedface)
417 {
418 DPRINT1("Delphi Exception at address: %p\n", ExceptionRecord.ExceptionInformation[0]);
419 DPRINT1("Exception-Object: %p\n", ExceptionRecord.ExceptionInformation[1]);
420 DPRINT1("Exception text: %s\n", ExceptionRecord.ExceptionInformation[2]);
421 }
422
423 /* Trace the wine special error and show the modulename and functionname */
424 if (dwExceptionCode == 0x80000100 /*EXCEPTION_WINE_STUB*/)
425 {
426 /* Numbers of parameter must be equal to two */
427 if (ExceptionRecord.NumberParameters == 2)
428 {
429 DPRINT1("Missing function in : %s\n", ExceptionRecord.ExceptionInformation[0]);
430 DPRINT1("with the functionname : %s\n", ExceptionRecord.ExceptionInformation[1]);
431 }
432 }
433
434 /* Raise the exception */
435 RtlRaiseException(&ExceptionRecord);
436 }
437
438 /* EOF */