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
)
211 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
214 HANDLE DebugPort
= NULL
;
216 ULONG ErrorParameters
[4];
219 if ((NTSTATUS
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
220 ExceptionInfo
->ExceptionRecord
->NumberParameters
>= 2)
222 switch(ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0])
224 case EXCEPTION_WRITE_FAULT
:
225 /* Change the protection on some write attempts, some InstallShield setups
227 RetValue
= BasepCheckForReadOnlyResource(
228 (PVOID
)ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
229 if (RetValue
== EXCEPTION_CONTINUE_EXECUTION
)
230 return EXCEPTION_CONTINUE_EXECUTION
;
232 case EXCEPTION_EXECUTE_FAULT
:
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
)
243 SetLastErrorByStatus(ErrCode
);
244 return EXCEPTION_EXECUTE_HANDLER
;
249 /* Pass the exception to debugger. */
250 DPRINT("Passing exception to debugger\n");
251 return EXCEPTION_CONTINUE_SEARCH
;
254 if (GlobalTopLevelExceptionFilter
)
256 LONG ret
= GlobalTopLevelExceptionFilter( ExceptionInfo
);
257 if (ret
!= EXCEPTION_CONTINUE_SEARCH
)
261 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX
) == 0)
267 CHAR szMod
[128] = "";
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)
275 DbgPrint("Faulting Address: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
277 DbgPrint("Address: %8x %s\n",
278 ExceptionInfo
->ExceptionRecord
->ExceptionAddress
,
279 _module_name_from_addr(ExceptionInfo
->ExceptionRecord
->ExceptionAddress
, &StartAddr
, szMod
, sizeof(szMod
)));
280 _dump_context ( ExceptionInfo
->ContextRecord
);
282 DbgPrint("Frames:\n");
285 Frame
= (PULONG
)ExceptionInfo
->ContextRecord
->Ebp
;
286 while (Frame
[1] != 0 && Frame
[1] != 0xdeadbeef)
288 if (IsBadReadPtr((PVOID
)Frame
[1], 4)) {
289 DbgPrint(" %8x%9s %s\n", Frame
[1], "<invalid address>"," ");
291 _module_name_from_addr((const void*)Frame
[1], &StartAddr
,
292 szMod
, sizeof(szMod
));
293 DbgPrint(" %8x+%-8x %s\n",
295 (ULONG_PTR
)Frame
[1] - (ULONG_PTR
)StartAddr
, szMod
);
297 if (IsBadReadPtr((PVOID
)Frame
[0], sizeof(*Frame
) * 2)) {
300 Frame
= (PULONG
)Frame
[0];
303 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
305 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH2_GetExceptionCode());
311 /* Save exception code and address */
312 ErrorParameters
[0] = (ULONG
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
;
313 ErrorParameters
[1] = (ULONG
)ExceptionInfo
->ExceptionRecord
->ExceptionAddress
;
315 if ((NTSTATUS
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
)
317 /* get the type of operation that caused the access violation */
318 ErrorParameters
[2] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0];
322 ErrorParameters
[2] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[2];
325 /* Save faulting address */
326 ErrorParameters
[3] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1];
328 /* Raise the harderror */
329 ErrCode
= NtRaiseHardError(STATUS_UNHANDLED_EXCEPTION
| 0x10000000,
330 4, 0, ErrorParameters
, OptionOkCancel
, &ErrorResponse
);
332 if (NT_SUCCESS(ErrCode
) && (ErrorResponse
== ResponseCancel
))
334 /* FIXME: Check the result, if the "Cancel" button was
335 clicked run a debugger */
336 DPRINT1("Debugging is not implemented yet\n");
340 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
341 * the __except block will be executed. Normally this will end up in a
345 return EXCEPTION_EXECUTE_HANDLER
;
353 RaiseException(IN DWORD dwExceptionCode
,
354 IN DWORD dwExceptionFlags
,
355 IN DWORD nNumberOfArguments
,
356 IN CONST ULONG_PTR
*lpArguments OPTIONAL
)
358 EXCEPTION_RECORD ExceptionRecord
;
360 /* Setup the exception record */
361 ExceptionRecord
.ExceptionCode
= dwExceptionCode
;
362 ExceptionRecord
.ExceptionRecord
= NULL
;
363 ExceptionRecord
.ExceptionAddress
= (PVOID
)RaiseException
;
364 ExceptionRecord
.ExceptionFlags
= dwExceptionFlags
& EXCEPTION_NONCONTINUABLE
;
366 /* Check if we have arguments */
370 ExceptionRecord
.NumberParameters
= 0;
374 /* We do, normalize the count */
375 if (nNumberOfArguments
> EXCEPTION_MAXIMUM_PARAMETERS
)
377 nNumberOfArguments
= EXCEPTION_MAXIMUM_PARAMETERS
;
380 /* Set the count of parameters */
381 ExceptionRecord
.NumberParameters
= nNumberOfArguments
;
383 /* Loop each parameter */
384 for (nNumberOfArguments
= 0;
385 (nNumberOfArguments
< ExceptionRecord
.NumberParameters
);
386 nNumberOfArguments
++)
388 /* Copy the exception information */
389 ExceptionRecord
.ExceptionInformation
[nNumberOfArguments
] =
394 if (dwExceptionCode
== 0xeedface)
396 DPRINT1("Delphi Exception at address: %p\n", ExceptionRecord
.ExceptionInformation
[0]);
397 DPRINT1("Exception-Object: %p\n", ExceptionRecord
.ExceptionInformation
[1]);
398 DPRINT1("Exception text: %s\n", ExceptionRecord
.ExceptionInformation
[2]);
401 /* Raise the exception */
402 RtlRaiseException(&ExceptionRecord
);