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
)
133 * Print out the CPU registers
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:EIP %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
);
152 #warning Unknown architecture
157 BasepCheckForReadOnlyResource(IN PVOID Ptr
)
160 ULONG Size
, OldProtect
;
161 MEMORY_BASIC_INFORMATION mbi
;
163 LONG Ret
= EXCEPTION_CONTINUE_SEARCH
;
165 /* Check if it was an attempt to write to a read-only image section! */
166 Status
= NtQueryVirtualMemory(NtCurrentProcess(),
168 MemoryBasicInformation
,
172 if (NT_SUCCESS(Status
) &&
173 mbi
.Protect
== PAGE_READONLY
&& mbi
.Type
== MEM_IMAGE
)
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
181 Data
= RtlImageDirectoryEntryToData(mbi
.AllocationBase
,
183 IMAGE_DIRECTORY_ENTRY_RESOURCE
,
187 (ULONG_PTR
)Ptr
>= (ULONG_PTR
)Data
&&
188 (ULONG_PTR
)Ptr
< (ULONG_PTR
)Data
+ Size
)
190 /* The user tried to write into the resources. Make the page
193 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
198 if (NT_SUCCESS(Status
))
200 Ret
= EXCEPTION_CONTINUE_EXECUTION
;
204 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
217 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
220 HANDLE DebugPort
= NULL
;
222 ULONG_PTR ErrorParameters
[4];
225 if ((NTSTATUS
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
226 ExceptionInfo
->ExceptionRecord
->NumberParameters
>= 2)
228 switch(ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0])
230 case EXCEPTION_WRITE_FAULT
:
231 /* Change the protection on some write attempts, some InstallShield setups
233 RetValue
= BasepCheckForReadOnlyResource(
234 (PVOID
)ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
235 if (RetValue
== EXCEPTION_CONTINUE_EXECUTION
)
236 return EXCEPTION_CONTINUE_EXECUTION
;
238 case EXCEPTION_EXECUTE_FAULT
:
244 /* Is there a debugger running ? */
245 ErrCode
= NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort
,
246 &DebugPort
, sizeof(HANDLE
), NULL
);
247 if (!NT_SUCCESS(ErrCode
) && ErrCode
!= STATUS_NOT_IMPLEMENTED
)
249 SetLastErrorByStatus(ErrCode
);
250 return EXCEPTION_EXECUTE_HANDLER
;
255 /* Pass the exception to debugger. */
256 DPRINT("Passing exception to debugger\n");
257 return EXCEPTION_CONTINUE_SEARCH
;
260 if (GlobalTopLevelExceptionFilter
)
262 LONG ret
= GlobalTopLevelExceptionFilter( ExceptionInfo
);
263 if (ret
!= EXCEPTION_CONTINUE_SEARCH
)
267 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX
) == 0)
273 CHAR szMod
[128] = "";
275 /* Print a stack trace. */
276 DbgPrint("Unhandled exception\n");
277 DbgPrint("ExceptionCode: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionCode
);
278 if ((NTSTATUS
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
279 ExceptionInfo
->ExceptionRecord
->NumberParameters
== 2)
281 DbgPrint("Faulting Address: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
283 DbgPrint("Address: %8x %s\n",
284 ExceptionInfo
->ExceptionRecord
->ExceptionAddress
,
285 _module_name_from_addr(ExceptionInfo
->ExceptionRecord
->ExceptionAddress
, &StartAddr
, szMod
, sizeof(szMod
)));
286 _dump_context ( ExceptionInfo
->ContextRecord
);
288 DbgPrint("Frames:\n");
291 Frame
= (PULONG
)ExceptionInfo
->ContextRecord
->Ebp
;
292 while (Frame
[1] != 0 && Frame
[1] != 0xdeadbeef)
294 if (IsBadReadPtr((PVOID
)Frame
[1], 4)) {
295 DbgPrint(" %8x%9s %s\n", Frame
[1], "<invalid address>"," ");
297 _module_name_from_addr((const void*)Frame
[1], &StartAddr
,
298 szMod
, sizeof(szMod
));
299 DbgPrint(" %8x+%-8x %s\n",
301 (ULONG_PTR
)Frame
[1] - (ULONG_PTR
)StartAddr
, szMod
);
303 if (IsBadReadPtr((PVOID
)Frame
[0], sizeof(*Frame
) * 2)) {
306 Frame
= (PULONG
)Frame
[0];
309 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
311 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH2_GetExceptionCode());
317 /* Save exception code and address */
318 ErrorParameters
[0] = (ULONG
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
;
319 ErrorParameters
[1] = (ULONG_PTR
)ExceptionInfo
->ExceptionRecord
->ExceptionAddress
;
321 if ((NTSTATUS
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
)
323 /* get the type of operation that caused the access violation */
324 ErrorParameters
[2] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0];
328 ErrorParameters
[2] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[2];
331 /* Save faulting address */
332 ErrorParameters
[3] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1];
334 /* Raise the harderror */
335 ErrCode
= NtRaiseHardError(STATUS_UNHANDLED_EXCEPTION
| 0x10000000,
336 4, 0, ErrorParameters
, OptionOkCancel
, &ErrorResponse
);
338 if (NT_SUCCESS(ErrCode
) && (ErrorResponse
== ResponseCancel
))
340 /* FIXME: Check the result, if the "Cancel" button was
341 clicked run a debugger */
342 DPRINT1("Debugging is not implemented yet\n");
346 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
347 * the __except block will be executed. Normally this will end up in a
351 return EXCEPTION_EXECUTE_HANDLER
;
359 RaiseException(IN DWORD dwExceptionCode
,
360 IN DWORD dwExceptionFlags
,
361 IN DWORD nNumberOfArguments
,
362 IN CONST ULONG_PTR
*lpArguments OPTIONAL
)
364 EXCEPTION_RECORD ExceptionRecord
;
366 /* Setup the exception record */
367 ExceptionRecord
.ExceptionCode
= dwExceptionCode
;
368 ExceptionRecord
.ExceptionRecord
= NULL
;
369 ExceptionRecord
.ExceptionAddress
= (PVOID
)RaiseException
;
370 ExceptionRecord
.ExceptionFlags
= dwExceptionFlags
& EXCEPTION_NONCONTINUABLE
;
372 /* Check if we have arguments */
376 ExceptionRecord
.NumberParameters
= 0;
380 /* We do, normalize the count */
381 if (nNumberOfArguments
> EXCEPTION_MAXIMUM_PARAMETERS
)
383 nNumberOfArguments
= EXCEPTION_MAXIMUM_PARAMETERS
;
386 /* Set the count of parameters */
387 ExceptionRecord
.NumberParameters
= nNumberOfArguments
;
389 /* Loop each parameter */
390 for (nNumberOfArguments
= 0;
391 (nNumberOfArguments
< ExceptionRecord
.NumberParameters
);
392 nNumberOfArguments
++)
394 /* Copy the exception information */
395 ExceptionRecord
.ExceptionInformation
[nNumberOfArguments
] =
400 if (dwExceptionCode
== 0xeedface)
402 DPRINT1("Delphi Exception at address: %p\n", ExceptionRecord
.ExceptionInformation
[0]);
403 DPRINT1("Exception-Object: %p\n", ExceptionRecord
.ExceptionInformation
[1]);
404 DPRINT1("Exception text: %s\n", ExceptionRecord
.ExceptionInformation
[2]);
407 /* Raise the exception */
408 RtlRaiseException(&ExceptionRecord
);