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
);
142 #elif defined(_M_AMD64)
143 _dump_context(PCONTEXT pc
)
145 DbgPrint("CS:EIP %x:%I64x\n", pc
->SegCs
&0xffff, pc
->Rip
);
146 DbgPrint("DS %x ES %x FS %x GS %x\n", pc
->SegDs
&0xffff, pc
->SegEs
&0xffff,
147 pc
->SegFs
&0xffff, pc
->SegGs
&0xfff);
148 DbgPrint("RAX: %I64x RBX: %I64x RCX: %I64x RDI: %I64x\n", pc
->Rax
, pc
->Rbx
, pc
->Rcx
, pc
->Rdi
);
149 DbgPrint("RDX: %I64x RBP: %I64x RSI: %I64x RSP: %I64x\n", pc
->Rdx
, pc
->Rbp
, pc
->Rsi
, pc
->Rsp
);
150 DbgPrint("R8: %I64x R9: %I64x R10: %I64x R11: %I64x\n", pc
->R8
, pc
->R9
, pc
->R10
, pc
->R11
);
151 DbgPrint("R12: %I64x R13: %I64x R14: %I64x R15: %I64x\n", pc
->R12
, pc
->R13
, pc
->R14
, pc
->R15
);
152 DbgPrint("EFLAGS: %.8x\n", pc
->EFlags
);
155 #warning Unknown architecture
157 _dump_context(PCONTEXT pc
)
163 BasepCheckForReadOnlyResource(IN PVOID Ptr
)
166 ULONG Size
, OldProtect
;
167 MEMORY_BASIC_INFORMATION mbi
;
169 LONG Ret
= EXCEPTION_CONTINUE_SEARCH
;
171 /* Check if it was an attempt to write to a read-only image section! */
172 Status
= NtQueryVirtualMemory(NtCurrentProcess(),
174 MemoryBasicInformation
,
178 if (NT_SUCCESS(Status
) &&
179 mbi
.Protect
== PAGE_READONLY
&& mbi
.Type
== MEM_IMAGE
)
181 /* Attempt to treat it as a resource section. We need to
182 use SEH here because we don't know if it's actually a
187 Data
= RtlImageDirectoryEntryToData(mbi
.AllocationBase
,
189 IMAGE_DIRECTORY_ENTRY_RESOURCE
,
193 (ULONG_PTR
)Ptr
>= (ULONG_PTR
)Data
&&
194 (ULONG_PTR
)Ptr
< (ULONG_PTR
)Data
+ Size
)
196 /* The user tried to write into the resources. Make the page
199 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
204 if (NT_SUCCESS(Status
))
206 Ret
= EXCEPTION_CONTINUE_EXECUTION
;
223 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
226 HANDLE DebugPort
= NULL
;
228 ULONG ErrorParameters
[4];
231 if (ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
232 ExceptionInfo
->ExceptionRecord
->NumberParameters
>= 2)
234 switch(ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0])
236 case EXCEPTION_WRITE_FAULT
:
237 /* Change the protection on some write attempts, some InstallShield setups
239 RetValue
= BasepCheckForReadOnlyResource(
240 (PVOID
)ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
241 if (RetValue
== EXCEPTION_CONTINUE_EXECUTION
)
242 return EXCEPTION_CONTINUE_EXECUTION
;
244 case EXCEPTION_EXECUTE_FAULT
:
250 /* Is there a debugger running ? */
251 ErrCode
= NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort
,
252 &DebugPort
, sizeof(HANDLE
), NULL
);
253 if (!NT_SUCCESS(ErrCode
) && ErrCode
!= STATUS_NOT_IMPLEMENTED
)
255 SetLastErrorByStatus(ErrCode
);
256 return EXCEPTION_EXECUTE_HANDLER
;
261 /* Pass the exception to debugger. */
262 DPRINT("Passing exception to debugger\n");
263 return EXCEPTION_CONTINUE_SEARCH
;
266 if (GlobalTopLevelExceptionFilter
)
268 LONG ret
= GlobalTopLevelExceptionFilter( ExceptionInfo
);
269 if (ret
!= EXCEPTION_CONTINUE_SEARCH
)
273 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX
) == 0)
279 CHAR szMod
[128] = "";
281 /* Print a stack trace. */
282 DbgPrint("Unhandled exception\n");
283 DbgPrint("ExceptionCode: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionCode
);
284 if (ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
285 ExceptionInfo
->ExceptionRecord
->NumberParameters
== 2)
287 DbgPrint("Faulting Address: %8x\n", ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1]);
289 DbgPrint("Address: %8x %s\n",
290 ExceptionInfo
->ExceptionRecord
->ExceptionAddress
,
291 _module_name_from_addr(ExceptionInfo
->ExceptionRecord
->ExceptionAddress
, &StartAddr
, szMod
, sizeof(szMod
)));
292 _dump_context ( ExceptionInfo
->ContextRecord
);
294 DbgPrint("Frames:\n");
297 Frame
= (PULONG
)ExceptionInfo
->ContextRecord
->Ebp
;
298 while (Frame
[1] != 0 && Frame
[1] != 0xdeadbeef)
300 if (IsBadReadPtr((PVOID
)Frame
[1], 4)) {
301 DbgPrint(" %8x%9s %s\n", Frame
[1], "<invalid address>"," ");
303 _module_name_from_addr((const void*)Frame
[1], &StartAddr
,
304 szMod
, sizeof(szMod
));
305 DbgPrint(" %8x+%-8x %s\n",
307 (ULONG_PTR
)Frame
[1] - (ULONG_PTR
)StartAddr
, szMod
);
309 if (IsBadReadPtr((PVOID
)Frame
[0], sizeof(*Frame
) * 2)) {
312 Frame
= (PULONG
)Frame
[0];
317 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH_GetExceptionCode());
323 /* Save exception code and address */
324 ErrorParameters
[0] = (ULONG
)ExceptionInfo
->ExceptionRecord
->ExceptionCode
;
325 ErrorParameters
[1] = (ULONG
)ExceptionInfo
->ExceptionRecord
->ExceptionAddress
;
327 if (ExceptionInfo
->ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
)
329 /* get the type of operation that caused the access violation */
330 ErrorParameters
[2] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[0];
334 ErrorParameters
[2] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[2];
337 /* Save faulting address */
338 ErrorParameters
[3] = ExceptionInfo
->ExceptionRecord
->ExceptionInformation
[1];
340 /* Raise the harderror */
341 ErrCode
= NtRaiseHardError(STATUS_UNHANDLED_EXCEPTION
| 0x10000000,
342 4, 0, ErrorParameters
, OptionOkCancel
, &ErrorResponse
);
344 if (NT_SUCCESS(ErrCode
) && (ErrorResponse
== ResponseCancel
))
346 /* FIXME: Check the result, if the "Cancel" button was
347 clicked run a debugger */
348 DPRINT1("Debugging is not implemented yet\n");
352 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
353 * the __except block will be executed. Normally this will end up in a
357 return EXCEPTION_EXECUTE_HANDLER
;
365 RaiseException(IN DWORD dwExceptionCode
,
366 IN DWORD dwExceptionFlags
,
367 IN DWORD nNumberOfArguments
,
368 IN CONST ULONG_PTR
*lpArguments OPTIONAL
)
370 EXCEPTION_RECORD ExceptionRecord
;
372 /* Setup the exception record */
373 ExceptionRecord
.ExceptionCode
= dwExceptionCode
;
374 ExceptionRecord
.ExceptionRecord
= NULL
;
375 ExceptionRecord
.ExceptionAddress
= (PVOID
)RaiseException
;
376 ExceptionRecord
.ExceptionFlags
= dwExceptionFlags
& EXCEPTION_NONCONTINUABLE
;
378 /* Check if we have arguments */
382 ExceptionRecord
.NumberParameters
= 0;
386 /* We do, normalize the count */
387 if (nNumberOfArguments
> EXCEPTION_MAXIMUM_PARAMETERS
)
389 nNumberOfArguments
= EXCEPTION_MAXIMUM_PARAMETERS
;
392 /* Set the count of parameters */
393 ExceptionRecord
.NumberParameters
= nNumberOfArguments
;
395 /* Loop each parameter */
396 for (nNumberOfArguments
= 0;
397 (nNumberOfArguments
< ExceptionRecord
.NumberParameters
);
398 nNumberOfArguments
++)
400 /* Copy the exception information */
401 ExceptionRecord
.ExceptionInformation
[nNumberOfArguments
] =
406 if (dwExceptionCode
== 0xeedface)
408 DPRINT1("Delphi Exception at address: %p\n", ExceptionRecord
.ExceptionInformation
[0]);
409 DPRINT1("Exception-Object: %p\n", ExceptionRecord
.ExceptionInformation
[1]);
410 DPRINT1("Exception text: %s\n", ExceptionRecord
.ExceptionInformation
[2]);
413 /* Raise the exception */
414 RtlRaiseException(&ExceptionRecord
);