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) ]
18 LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter
= NULL
;
27 /* Query the current setting */
28 Status
= NtQueryInformationProcess(NtCurrentProcess(),
29 ProcessDefaultHardErrorMode
,
33 if (!NT_SUCCESS(Status
))
35 /* Fail if we couldn't query */
36 SetLastErrorByStatus(Status
);
40 /* Check if NOT failing critical errors was requested */
41 if (ErrMode
& SEM_FAILCRITICALERRORS
)
43 /* Mask it out, since the native API works differently */
44 ErrMode
&= ~SEM_FAILCRITICALERRORS
;
48 /* OR it if the caller didn't, due to different native semantics */
49 ErrMode
|= SEM_FAILCRITICALERRORS
;
61 SetErrorMode(IN UINT uMode
)
63 UINT PrevErrMode
, NewMode
;
66 /* Get the previous mode */
67 PrevErrMode
= GetErrorMode();
70 /* Check if failing critical errors was requested */
71 if (NewMode
& SEM_FAILCRITICALERRORS
)
73 /* Mask it out, since the native API works differently */
74 NewMode
&= ~SEM_FAILCRITICALERRORS
;
78 /* OR it if the caller didn't, due to different native semantics */
79 NewMode
|= SEM_FAILCRITICALERRORS
;
82 /* Set the new mode */
83 Status
= NtSetInformationProcess(NtCurrentProcess(),
84 ProcessDefaultHardErrorMode
,
87 if(!NT_SUCCESS(Status
)) SetLastErrorByStatus(Status
);
89 /* Return the previous mode */
96 LPTOP_LEVEL_EXCEPTION_FILTER
98 SetUnhandledExceptionFilter(
99 IN LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter
)
101 return InterlockedExchangePointer(&GlobalTopLevelExceptionFilter
,
102 lpTopLevelExceptionFilter
);
106 * Private helper function to lookup the module name from a given address.
107 * The address can point to anywhere within the module.
110 _module_name_from_addr(const void* addr
, void **module_start_addr
,
111 char* psz
, size_t nChars
)
113 MEMORY_BASIC_INFORMATION mbi
;
114 if (VirtualQuery(addr
, &mbi
, sizeof(mbi
)) != sizeof(mbi
) ||
115 !GetModuleFileNameA((HMODULE
)mbi
.AllocationBase
, psz
, nChars
))
118 *module_start_addr
= 0;
122 *module_start_addr
= (void *)mbi
.AllocationBase
;
129 _dump_context(PCONTEXT pc
)
132 * Print out the CPU registers
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
);
143 #warning Unknown architecture
145 _dump_context(PCONTEXT pc
)
151 BasepCheckForReadOnlyResource(IN PVOID Ptr
)
154 ULONG Size
, OldProtect
;
155 MEMORY_BASIC_INFORMATION mbi
;
157 LONG Ret
= EXCEPTION_CONTINUE_SEARCH
;
159 /* Check if it was an attempt to write to a read-only image section! */
160 Status
= NtQueryVirtualMemory(NtCurrentProcess(),
162 MemoryBasicInformation
,
166 if (NT_SUCCESS(Status
) &&
167 mbi
.Protect
== PAGE_READONLY
&& mbi
.Type
== MEM_IMAGE
)
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
175 Data
= RtlImageDirectoryEntryToData(mbi
.AllocationBase
,
177 IMAGE_DIRECTORY_ENTRY_RESOURCE
,
181 (ULONG_PTR
)Ptr
>= (ULONG_PTR
)Data
&&
182 (ULONG_PTR
)Ptr
< (ULONG_PTR
)Data
+ Size
)
184 /* The user tried to write into the resources. Make the page
187 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
192 if (NT_SUCCESS(Status
))
194 Ret
= EXCEPTION_CONTINUE_EXECUTION
;
198 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
208 PrintStackTrace(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
211 CHAR szMod
[128] = "";
212 PEXCEPTION_RECORD ExceptionRecord
= ExceptionInfo
->ExceptionRecord
;
213 PCONTEXT ContextRecord
= ExceptionInfo
->ContextRecord
;
215 /* Print a stack trace. */
216 DbgPrint("Unhandled exception\n");
217 DbgPrint("ExceptionCode: %8x\n", ExceptionRecord
->ExceptionCode
);
219 if ((NTSTATUS
)ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
220 ExceptionRecord
->NumberParameters
== 2)
222 DbgPrint("Faulting Address: %8x\n", ExceptionRecord
->ExceptionInformation
[1]);
225 _dump_context (ContextRecord
);
226 _module_name_from_addr(ExceptionRecord
->ExceptionAddress
, &StartAddr
, szMod
, sizeof(szMod
));
227 DbgPrint("Address:\n %8x+%-8x %s\n",
229 (ULONG_PTR
)ExceptionRecord
->ExceptionAddress
- (ULONG_PTR
)StartAddr
,
232 DbgPrint("Frames:\n");
237 PULONG Frame
= (PULONG
)ContextRecord
->Ebp
;
239 for (i
= 0; Frame
[1] != 0 && Frame
[1] != 0xdeadbeef && i
< 128; i
++)
241 if (IsBadReadPtr((PVOID
)Frame
[1], 4))
243 DbgPrint(" %8x%9s %s\n", Frame
[1], "<invalid address>"," ");
247 _module_name_from_addr((const void*)Frame
[1], &StartAddr
,
248 szMod
, sizeof(szMod
));
249 DbgPrint(" %8x+%-8x %s\n",
251 (ULONG_PTR
)Frame
[1] - (ULONG_PTR
)StartAddr
,
255 if (IsBadReadPtr((PVOID
)Frame
[0], sizeof(*Frame
) * 2))
258 Frame
= (PULONG
)Frame
[0];
261 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
263 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH2_GetExceptionCode());
273 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
276 HANDLE DebugPort
= NULL
;
278 ULONG ErrorParameters
[4];
280 PEXCEPTION_RECORD ExceptionRecord
= ExceptionInfo
->ExceptionRecord
;
282 if ((NTSTATUS
)ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
283 ExceptionRecord
->NumberParameters
>= 2)
285 switch(ExceptionRecord
->ExceptionInformation
[0])
287 case EXCEPTION_WRITE_FAULT
:
288 /* Change the protection on some write attempts, some InstallShield setups
290 RetValue
= BasepCheckForReadOnlyResource(
291 (PVOID
)ExceptionRecord
->ExceptionInformation
[1]);
292 if (RetValue
== EXCEPTION_CONTINUE_EXECUTION
)
293 return EXCEPTION_CONTINUE_EXECUTION
;
295 case EXCEPTION_EXECUTE_FAULT
:
301 /* Is there a debugger running ? */
302 ErrCode
= NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort
,
303 &DebugPort
, sizeof(HANDLE
), NULL
);
304 if (!NT_SUCCESS(ErrCode
) && ErrCode
!= STATUS_NOT_IMPLEMENTED
)
306 SetLastErrorByStatus(ErrCode
);
307 return EXCEPTION_EXECUTE_HANDLER
;
312 /* Pass the exception to debugger. */
313 DPRINT("Passing exception to debugger\n");
314 return EXCEPTION_CONTINUE_SEARCH
;
317 if (GlobalTopLevelExceptionFilter
)
319 LONG ret
= GlobalTopLevelExceptionFilter(ExceptionInfo
);
320 if (ret
!= EXCEPTION_CONTINUE_SEARCH
)
324 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX
) == 0)
325 PrintStackTrace(ExceptionInfo
);
327 /* Save exception code and address */
328 ErrorParameters
[0] = (ULONG
)ExceptionRecord
->ExceptionCode
;
329 ErrorParameters
[1] = (ULONG
)ExceptionRecord
->ExceptionAddress
;
331 if ((NTSTATUS
)ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
)
333 /* get the type of operation that caused the access violation */
334 ErrorParameters
[2] = ExceptionRecord
->ExceptionInformation
[0];
338 ErrorParameters
[2] = ExceptionRecord
->ExceptionInformation
[2];
341 /* Save faulting address */
342 ErrorParameters
[3] = ExceptionRecord
->ExceptionInformation
[1];
344 /* Raise the harderror */
345 ErrCode
= NtRaiseHardError(STATUS_UNHANDLED_EXCEPTION
| 0x10000000,
346 4, 0, ErrorParameters
, OptionOkCancel
, &ErrorResponse
);
348 if (NT_SUCCESS(ErrCode
) && (ErrorResponse
== ResponseCancel
))
350 /* FIXME: Check the result, if the "Cancel" button was
351 clicked run a debugger */
352 DPRINT1("Debugging is not implemented yet\n");
356 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
357 * the __except block will be executed. Normally this will end up in a
361 return EXCEPTION_EXECUTE_HANDLER
;
369 RaiseException(IN DWORD dwExceptionCode
,
370 IN DWORD dwExceptionFlags
,
371 IN DWORD nNumberOfArguments
,
372 IN CONST ULONG_PTR
*lpArguments OPTIONAL
)
374 EXCEPTION_RECORD ExceptionRecord
;
376 /* Setup the exception record */
377 ExceptionRecord
.ExceptionCode
= dwExceptionCode
;
378 ExceptionRecord
.ExceptionRecord
= NULL
;
379 ExceptionRecord
.ExceptionAddress
= (PVOID
)RaiseException
;
380 ExceptionRecord
.ExceptionFlags
= dwExceptionFlags
& EXCEPTION_NONCONTINUABLE
;
382 /* Check if we have arguments */
386 ExceptionRecord
.NumberParameters
= 0;
390 /* We do, normalize the count */
391 if (nNumberOfArguments
> EXCEPTION_MAXIMUM_PARAMETERS
)
393 nNumberOfArguments
= EXCEPTION_MAXIMUM_PARAMETERS
;
396 /* Set the count of parameters */
397 ExceptionRecord
.NumberParameters
= nNumberOfArguments
;
399 /* Loop each parameter */
400 for (nNumberOfArguments
= 0;
401 (nNumberOfArguments
< ExceptionRecord
.NumberParameters
);
402 nNumberOfArguments
++)
404 /* Copy the exception information */
405 ExceptionRecord
.ExceptionInformation
[nNumberOfArguments
] =
410 if (dwExceptionCode
== 0xeedface)
412 DPRINT1("Delphi Exception at address: %p\n", ExceptionRecord
.ExceptionInformation
[0]);
413 DPRINT1("Exception-Object: %p\n", ExceptionRecord
.ExceptionInformation
[1]);
414 DPRINT1("Exception text: %s\n", ExceptionRecord
.ExceptionInformation
[2]);
417 /* Trace the wine special error and show the modulename and functionname */
418 if (dwExceptionCode
== 0x80000100 /*EXCEPTION_WINE_STUB*/)
420 /* Numbers of parameter must be equal to two */
421 if (ExceptionRecord
.NumberParameters
== 2)
423 DPRINT1("Missing function in : %s\n", ExceptionRecord
.ExceptionInformation
[0]);
424 DPRINT1("with the functionname : %s\n", ExceptionRecord
.ExceptionInformation
[1]);
428 /* Raise the exception */
429 RtlRaiseException(&ExceptionRecord
);