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) ]
16 #include "../include/debug.h"
18 LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter
= UnhandledExceptionFilter
;
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
;
211 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
214 HANDLE DebugPort
= NULL
;
217 if (ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
218 ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0])
220 /* Change the protection on some write attempts, some InstallShield setups
222 RetValue
= BasepCheckForReadOnlyResource(
223 (PVOID
)ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
224 if (RetValue
== EXCEPTION_CONTINUE_EXECUTION
)
225 return EXCEPTION_CONTINUE_EXECUTION
;
228 /* Is there a debugger running ? */
229 ErrCode
= NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort
,
230 &DebugPort
, sizeof(HANDLE
), NULL
);
231 if (!NT_SUCCESS(ErrCode
) && ErrCode
!= STATUS_NOT_IMPLEMENTED
)
233 SetLastErrorByStatus(ErrCode
);
234 return EXCEPTION_EXECUTE_HANDLER
;
239 /* Pass the exception to debugger. */
240 DPRINT("Passing exception to debugger\n");
241 return EXCEPTION_CONTINUE_SEARCH
;
244 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX
) == 0)
250 CHAR szMod
[128] = "";
252 /* Print a stack trace. */
253 DbgPrint("Unhandled exception\n");
254 DbgPrint("ExceptionCode: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionCode
);
255 if (ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
256 ExceptionInfo
->ExceptionRecord
->NumberParameters
== 2)
258 DbgPrint("Faulting Address: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
260 DbgPrint("Address: %8x %s\n",
261 ExceptionInfo
->ExceptionRecord
->ExceptionAddress
,
262 _module_name_from_addr(ExceptionInfo
->ExceptionRecord
->ExceptionAddress
, &StartAddr
, szMod
, sizeof(szMod
)));
263 _dump_context ( ExceptionInfo
->ContextRecord
);
265 DbgPrint("Frames:\n");
268 Frame
= (PULONG
)ExceptionInfo
->ContextRecord
->Ebp
;
269 while (Frame
[1] != 0 && Frame
[1] != 0xdeadbeef)
271 if (IsBadReadPtr((PVOID
)Frame
[1], 4)) {
272 DbgPrint(" %8x%9s %s\n", Frame
[1], "<invalid address>"," ");
274 _module_name_from_addr((const void*)Frame
[1], &StartAddr
,
275 szMod
, sizeof(szMod
));
276 DbgPrint(" %8x+%-8x %s\n",
278 (ULONG_PTR
)Frame
[1] - (ULONG_PTR
)StartAddr
, szMod
);
280 if (IsBadReadPtr((PVOID
)Frame
[0], sizeof(*Frame
) * 2)) {
283 Frame
= (PULONG
)Frame
[0];
288 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH_GetExceptionCode());
295 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
296 * the __except block will be executed. Normally this will end up in a
300 return EXCEPTION_EXECUTE_HANDLER
;
308 RaiseException(IN DWORD dwExceptionCode
,
309 IN DWORD dwExceptionFlags
,
310 IN DWORD nNumberOfArguments
,
311 IN CONST ULONG_PTR
*lpArguments OPTIONAL
)
313 EXCEPTION_RECORD ExceptionRecord
;
315 /* Setup the exception record */
316 ExceptionRecord
.ExceptionCode
= dwExceptionCode
;
317 ExceptionRecord
.ExceptionRecord
= NULL
;
318 ExceptionRecord
.ExceptionAddress
= (PVOID
)RaiseException
;
319 ExceptionRecord
.ExceptionFlags
= dwExceptionFlags
& EXCEPTION_NONCONTINUABLE
;
321 /* Check if we have arguments */
325 ExceptionRecord
.NumberParameters
= 0;
329 /* We do, normalize the count */
330 if (nNumberOfArguments
> EXCEPTION_MAXIMUM_PARAMETERS
)
332 nNumberOfArguments
= EXCEPTION_MAXIMUM_PARAMETERS
;
335 /* Set the count of parameters */
336 ExceptionRecord
.NumberParameters
= nNumberOfArguments
;
338 /* Loop each parameter */
339 for (nNumberOfArguments
= 0;
340 (nNumberOfArguments
< ExceptionRecord
.NumberParameters
);
341 nNumberOfArguments
++)
343 /* Copy the exception information */
344 ExceptionRecord
.ExceptionInformation
[nNumberOfArguments
] =
349 /* Raise the exception */
350 RtlRaiseException(&ExceptionRecord
);