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
;
128 _dump_context(PCONTEXT pc
)
131 * Print out the CPU registers
133 DbgPrint("CS:EIP %x:%x\n", pc
->SegCs
&0xffff, pc
->Eip
);
134 DbgPrint("DS %x ES %x FS %x GS %x\n", pc
->SegDs
&0xffff, pc
->SegEs
&0xffff,
135 pc
->SegFs
&0xffff, pc
->SegGs
&0xfff);
136 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", pc
->Eax
, pc
->Ebx
, pc
->Ecx
);
137 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", pc
->Edx
,
138 pc
->Ebp
, pc
->Esi
, pc
->Esp
);
139 DbgPrint("EDI: %.8x EFLAGS: %.8x\n", pc
->Edi
, pc
->EFlags
);
143 BasepCheckForReadOnlyResource(IN PVOID Ptr
)
146 ULONG Size
, OldProtect
;
147 MEMORY_BASIC_INFORMATION mbi
;
149 LONG Ret
= EXCEPTION_CONTINUE_SEARCH
;
151 /* Check if it was an attempt to write to a read-only image section! */
152 Status
= NtQueryVirtualMemory(NtCurrentProcess(),
154 MemoryBasicInformation
,
158 if (NT_SUCCESS(Status
) &&
159 mbi
.Protect
== PAGE_READONLY
&& mbi
.Type
== MEM_IMAGE
)
161 /* Attempt to treat it as a resource section. We need to
162 use SEH here because we don't know if it's actually a
167 Data
= RtlImageDirectoryEntryToData(mbi
.AllocationBase
,
169 IMAGE_DIRECTORY_ENTRY_RESOURCE
,
173 (ULONG_PTR
)Ptr
>= (ULONG_PTR
)Data
&&
174 (ULONG_PTR
)Ptr
< (ULONG_PTR
)Data
+ Size
)
176 /* The user tried to write into the resources. Make the page
179 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
184 if (NT_SUCCESS(Status
))
186 Ret
= EXCEPTION_CONTINUE_EXECUTION
;
203 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
206 HANDLE DebugPort
= NULL
;
209 if (ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
210 ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0])
212 /* Change the protection on some write attempts, some InstallShield setups
214 RetValue
= BasepCheckForReadOnlyResource(
215 (PVOID
)ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
216 if (RetValue
== EXCEPTION_CONTINUE_EXECUTION
)
217 return EXCEPTION_CONTINUE_EXECUTION
;
220 /* Is there a debugger running ? */
221 ErrCode
= NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort
,
222 &DebugPort
, sizeof(HANDLE
), NULL
);
223 if (!NT_SUCCESS(ErrCode
) && ErrCode
!= STATUS_NOT_IMPLEMENTED
)
225 SetLastErrorByStatus(ErrCode
);
226 return EXCEPTION_EXECUTE_HANDLER
;
231 /* Pass the exception to debugger. */
232 DPRINT("Passing exception to debugger\n");
233 return EXCEPTION_CONTINUE_SEARCH
;
236 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX
) == 0)
241 CHAR szMod
[128] = "";
244 /* Print a stack trace. */
245 DbgPrint("Unhandled exception\n");
246 DbgPrint("ExceptionCode: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionCode
);
247 if (ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
248 ExceptionInfo
->ExceptionRecord
->NumberParameters
== 2)
250 DbgPrint("Faulting Address: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
252 DbgPrint("Address: %8x %s\n",
253 ExceptionInfo
->ExceptionRecord
->ExceptionAddress
,
254 _module_name_from_addr(ExceptionInfo
->ExceptionRecord
->ExceptionAddress
, &StartAddr
, szMod
, sizeof(szMod
)));
255 _dump_context ( ExceptionInfo
->ContextRecord
);
257 DbgPrint("Frames:\n");
260 Frame
= (PULONG
)ExceptionInfo
->ContextRecord
->Ebp
;
261 while (Frame
[1] != 0 && Frame
[1] != 0xdeadbeef)
263 if (IsBadReadPtr((PVOID
)Frame
[1], 4)) {
264 DbgPrint(" %8x%9s %s\n", Frame
[1], "<invalid address>"," ");
266 _module_name_from_addr((const void*)Frame
[1], &StartAddr
,
267 szMod
, sizeof(szMod
));
268 DbgPrint(" %8x+%-8x %s\n",
270 (ULONG_PTR
)Frame
[1] - (ULONG_PTR
)StartAddr
, szMod
);
272 if (IsBadReadPtr((PVOID
)Frame
[0], sizeof(*Frame
) * 2)) {
275 Frame
= (PULONG
)Frame
[0];
280 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH_GetExceptionCode());
287 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
288 * the __except block will be executed. Normally this will end up in a
292 return EXCEPTION_EXECUTE_HANDLER
;
300 RaiseException(IN DWORD dwExceptionCode
,
301 IN DWORD dwExceptionFlags
,
302 IN DWORD nNumberOfArguments
,
303 IN CONST ULONG_PTR
*lpArguments OPTIONAL
)
305 EXCEPTION_RECORD ExceptionRecord
;
307 /* Setup the exception record */
308 ExceptionRecord
.ExceptionCode
= dwExceptionCode
;
309 ExceptionRecord
.ExceptionRecord
= NULL
;
310 ExceptionRecord
.ExceptionAddress
= (PVOID
)RaiseException
;
311 ExceptionRecord
.ExceptionFlags
= dwExceptionFlags
& EXCEPTION_NONCONTINUABLE
;
313 /* Check if we have arguments */
317 ExceptionRecord
.NumberParameters
= 0;
321 /* We do, normalize the count */
322 if (nNumberOfArguments
> EXCEPTION_MAXIMUM_PARAMETERS
)
324 nNumberOfArguments
= EXCEPTION_MAXIMUM_PARAMETERS
;
327 /* Set the count of parameters */
328 ExceptionRecord
.NumberParameters
= nNumberOfArguments
;
330 /* Loop each parameter */
331 for (nNumberOfArguments
= 0;
332 (nNumberOfArguments
< ExceptionRecord
.NumberParameters
);
333 nNumberOfArguments
++)
335 /* Copy the exception information */
336 ExceptionRecord
.ExceptionInformation
[nNumberOfArguments
] =
341 /* Raise the exception */
342 RtlRaiseException(&ExceptionRecord
);